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