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