src/common.[ch]: Implement "parse_identifier_vl".
[collectd.git] / src / unixsock.c
1 /**
2  * collectd - src/unixsock.c
3  * Copyright (C) 2007,2008  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Author:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26
27 #include "utils_cmd_flush.h"
28 #include "utils_cmd_getval.h"
29 #include "utils_cmd_getthreshold.h"
30 #include "utils_cmd_listval.h"
31 #include "utils_cmd_putval.h"
32 #include "utils_cmd_putnotif.h"
33
34 /* Folks without pthread will need to disable this plugin. */
35 #include <pthread.h>
36
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39 #include <sys/un.h>
40
41 #include <grp.h>
42
43 #ifndef UNIX_PATH_MAX
44 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
45 #endif
46
47 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
48
49 /*
50  * Private variables
51  */
52 /* valid configuration file keys */
53 static const char *config_keys[] =
54 {
55         "SocketFile",
56         "SocketGroup",
57         "SocketPerms",
58         "DeleteSocket"
59 };
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
61
62 static int loop = 0;
63
64 /* socket configuration */
65 static int   sock_fd    = -1;
66 static char *sock_file  = NULL;
67 static char *sock_group = NULL;
68 static int   sock_perms = S_IRWXU | S_IRWXG;
69 static _Bool delete_socket = 0;
70
71 static pthread_t listen_thread = (pthread_t) 0;
72
73 /*
74  * Functions
75  */
76 static int us_open_socket (void)
77 {
78         struct sockaddr_un sa;
79         int status;
80
81         sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
82         if (sock_fd < 0)
83         {
84                 char errbuf[1024];
85                 ERROR ("unixsock plugin: socket failed: %s",
86                                 sstrerror (errno, errbuf, sizeof (errbuf)));
87                 return (-1);
88         }
89
90         memset (&sa, '\0', sizeof (sa));
91         sa.sun_family = AF_UNIX;
92         sstrncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
93                         sizeof (sa.sun_path));
94
95         DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
96
97         if (delete_socket)
98         {
99                 errno = 0;
100                 status = unlink (sa.sun_path);
101                 if ((status != 0) && (errno != ENOENT))
102                 {
103                         char errbuf[1024];
104                         WARNING ("unixsock plugin: Deleting socket file \"%s\" failed: %s",
105                                         sa.sun_path,
106                                         sstrerror (errno, errbuf, sizeof (errbuf)));
107                 }
108                 else if (status == 0)
109                 {
110                         INFO ("unixsock plugin: Successfully deleted socket file \"%s\".",
111                                         sa.sun_path);
112                 }
113         }
114
115         status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
116         if (status != 0)
117         {
118                 char errbuf[1024];
119                 sstrerror (errno, errbuf, sizeof (errbuf));
120                 ERROR ("unixsock plugin: bind failed: %s", errbuf);
121                 close (sock_fd);
122                 sock_fd = -1;
123                 return (-1);
124         }
125
126         chmod (sa.sun_path, sock_perms);
127
128         status = listen (sock_fd, 8);
129         if (status != 0)
130         {
131                 char errbuf[1024];
132                 ERROR ("unixsock plugin: listen failed: %s",
133                                 sstrerror (errno, errbuf, sizeof (errbuf)));
134                 close (sock_fd);
135                 sock_fd = -1;
136                 return (-1);
137         }
138
139         do
140         {
141                 char *grpname;
142                 struct group *g;
143                 struct group sg;
144                 char grbuf[2048];
145
146                 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
147                 g = NULL;
148
149                 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
150                 if (status != 0)
151                 {
152                         char errbuf[1024];
153                         WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
154                                         sstrerror (errno, errbuf, sizeof (errbuf)));
155                         break;
156                 }
157                 if (g == NULL)
158                 {
159                         WARNING ("unixsock plugin: No such group: `%s'",
160                                         grpname);
161                         break;
162                 }
163
164                 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
165                                         (uid_t) -1, g->gr_gid) != 0)
166                 {
167                         char errbuf[1024];
168                         WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
169                                         (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
170                                         (int) g->gr_gid,
171                                         sstrerror (errno, errbuf, sizeof (errbuf)));
172                 }
173         } while (0);
174
175         return (0);
176 } /* int us_open_socket */
177
178 static void *us_handle_client (void *arg)
179 {
180         int fdin;
181         int fdout;
182         FILE *fhin, *fhout;
183
184         fdin = *((int *) arg);
185         free (arg);
186         arg = NULL;
187
188         DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
189
190         fdout = dup (fdin);
191         if (fdout < 0)
192         {
193                 char errbuf[1024];
194                 ERROR ("unixsock plugin: dup failed: %s",
195                                 sstrerror (errno, errbuf, sizeof (errbuf)));
196                 close (fdin);
197                 pthread_exit ((void *) 1);
198         }
199
200         fhin  = fdopen (fdin, "r");
201         if (fhin == NULL)
202         {
203                 char errbuf[1024];
204                 ERROR ("unixsock plugin: fdopen failed: %s",
205                                 sstrerror (errno, errbuf, sizeof (errbuf)));
206                 close (fdin);
207                 close (fdout);
208                 pthread_exit ((void *) 1);
209         }
210
211         fhout = fdopen (fdout, "w");
212         if (fhout == NULL)
213         {
214                 char errbuf[1024];
215                 ERROR ("unixsock plugin: fdopen failed: %s",
216                                 sstrerror (errno, errbuf, sizeof (errbuf)));
217                 fclose (fhin); /* this closes fdin as well */
218                 close (fdout);
219                 pthread_exit ((void *) 1);
220         }
221
222         /* change output buffer to line buffered mode */
223         if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
224         {
225                 char errbuf[1024];
226                 ERROR ("unixsock plugin: setvbuf failed: %s",
227                                 sstrerror (errno, errbuf, sizeof (errbuf)));
228                 fclose (fhin);
229                 fclose (fhout);
230                 pthread_exit ((void *) 1);
231         }
232
233         while (42)
234         {
235                 char buffer[1024];
236                 char buffer_copy[1024];
237                 char *fields[128];
238                 int   fields_num;
239                 int   len;
240
241                 errno = 0;
242                 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
243                 {
244                         if (errno != 0)
245                         {
246                                 char errbuf[1024];
247                                 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
248                                                 fileno (fhin),
249                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
250                         }
251                         break;
252                 }
253
254                 len = strlen (buffer);
255                 while ((len > 0)
256                                 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
257                         buffer[--len] = '\0';
258
259                 if (len == 0)
260                         continue;
261
262                 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
263
264                 fields_num = strsplit (buffer_copy, fields,
265                                 sizeof (fields) / sizeof (fields[0]));
266                 if (fields_num < 1)
267                 {
268                         fprintf (fhout, "-1 Internal error\n");
269                         fclose (fhin);
270                         fclose (fhout);
271                         pthread_exit ((void *) 1);
272                 }
273
274                 if (strcasecmp (fields[0], "getval") == 0)
275                 {
276                         handle_getval (fhout, buffer);
277                 }
278                 else if (strcasecmp (fields[0], "getthreshold") == 0)
279                 {
280                         handle_getthreshold (fhout, buffer);
281                 }
282                 else if (strcasecmp (fields[0], "putval") == 0)
283                 {
284                         handle_putval (fhout, buffer);
285                 }
286                 else if (strcasecmp (fields[0], "listval") == 0)
287                 {
288                         handle_listval (fhout, buffer);
289                 }
290                 else if (strcasecmp (fields[0], "putnotif") == 0)
291                 {
292                         handle_putnotif (fhout, buffer);
293                 }
294                 else if (strcasecmp (fields[0], "flush") == 0)
295                 {
296                         handle_flush (fhout, buffer);
297                 }
298                 else
299                 {
300                         if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
301                         {
302                                 char errbuf[1024];
303                                 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
304                                                 fileno (fhout),
305                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
306                                 break;
307                         }
308                 }
309         } /* while (fgets) */
310
311         DEBUG ("unixsock plugin: us_handle_client: Exiting..");
312         fclose (fhin);
313         fclose (fhout);
314
315         pthread_exit ((void *) 0);
316         return ((void *) 0);
317 } /* void *us_handle_client */
318
319 static void *us_server_thread (void __attribute__((unused)) *arg)
320 {
321         int  status;
322         int *remote_fd;
323         pthread_t th;
324         pthread_attr_t th_attr;
325
326         if (us_open_socket () != 0)
327                 pthread_exit ((void *) 1);
328
329         while (loop != 0)
330         {
331                 DEBUG ("unixsock plugin: Calling accept..");
332                 status = accept (sock_fd, NULL, NULL);
333                 if (status < 0)
334                 {
335                         char errbuf[1024];
336
337                         if (errno == EINTR)
338                                 continue;
339
340                         ERROR ("unixsock plugin: accept failed: %s",
341                                         sstrerror (errno, errbuf, sizeof (errbuf)));
342                         close (sock_fd);
343                         sock_fd = -1;
344                         pthread_exit ((void *) 1);
345                 }
346
347                 remote_fd = (int *) malloc (sizeof (int));
348                 if (remote_fd == NULL)
349                 {
350                         char errbuf[1024];
351                         WARNING ("unixsock plugin: malloc failed: %s",
352                                         sstrerror (errno, errbuf, sizeof (errbuf)));
353                         close (status);
354                         continue;
355                 }
356                 *remote_fd = status;
357
358                 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
359
360                 pthread_attr_init (&th_attr);
361                 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
362
363                 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
364                 if (status != 0)
365                 {
366                         char errbuf[1024];
367                         WARNING ("unixsock plugin: pthread_create failed: %s",
368                                         sstrerror (errno, errbuf, sizeof (errbuf)));
369                         close (*remote_fd);
370                         free (remote_fd);
371                         continue;
372                 }
373         } /* while (loop) */
374
375         close (sock_fd);
376         sock_fd = -1;
377
378         status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
379         if (status != 0)
380         {
381                 char errbuf[1024];
382                 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
383                                 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
384                                 sstrerror (errno, errbuf, sizeof (errbuf)));
385         }
386
387         return ((void *) 0);
388 } /* void *us_server_thread */
389
390 static int us_config (const char *key, const char *val)
391 {
392         if (strcasecmp (key, "SocketFile") == 0)
393         {
394                 char *new_sock_file = strdup (val);
395                 if (new_sock_file == NULL)
396                         return (1);
397
398                 sfree (sock_file);
399                 sock_file = new_sock_file;
400         }
401         else if (strcasecmp (key, "SocketGroup") == 0)
402         {
403                 char *new_sock_group = strdup (val);
404                 if (new_sock_group == NULL)
405                         return (1);
406
407                 sfree (sock_group);
408                 sock_group = new_sock_group;
409         }
410         else if (strcasecmp (key, "SocketPerms") == 0)
411         {
412                 sock_perms = (int) strtol (val, NULL, 8);
413         }
414         else if (strcasecmp (key, "DeleteSocket") == 0)
415         {
416                 if (IS_TRUE (val))
417                         delete_socket = 1;
418                 else
419                         delete_socket = 0;
420         }
421         else
422         {
423                 return (-1);
424         }
425
426         return (0);
427 } /* int us_config */
428
429 static int us_init (void)
430 {
431         static int have_init = 0;
432
433         int status;
434
435         /* Initialize only once. */
436         if (have_init != 0)
437                 return (0);
438         have_init = 1;
439
440         loop = 1;
441
442         status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
443         if (status != 0)
444         {
445                 char errbuf[1024];
446                 ERROR ("unixsock plugin: pthread_create failed: %s",
447                                 sstrerror (errno, errbuf, sizeof (errbuf)));
448                 return (-1);
449         }
450
451         return (0);
452 } /* int us_init */
453
454 static int us_shutdown (void)
455 {
456         void *ret;
457
458         loop = 0;
459
460         if (listen_thread != (pthread_t) 0)
461         {
462                 pthread_kill (listen_thread, SIGTERM);
463                 pthread_join (listen_thread, &ret);
464                 listen_thread = (pthread_t) 0;
465         }
466
467         plugin_unregister_init ("unixsock");
468         plugin_unregister_shutdown ("unixsock");
469
470         return (0);
471 } /* int us_shutdown */
472
473 void module_register (void)
474 {
475         plugin_register_config ("unixsock", us_config,
476                         config_keys, config_keys_num);
477         plugin_register_init ("unixsock", us_init);
478         plugin_register_shutdown ("unixsock", us_shutdown);
479 } /* void module_register (void) */
480
481 /* vim: set sw=4 ts=4 sts=4 tw=78 : */