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                 return ((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                 return ((void *) 1);
221         }
222
223         /* change output buffer to line buffered mode */
224         if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
225         {
226                 char errbuf[1024];
227                 ERROR ("unixsock plugin: setvbuf failed: %s",
228                                 sstrerror (errno, errbuf, sizeof (errbuf)));
229                 fclose (fhin);
230                 fclose (fhout);
231                 pthread_exit ((void *) 1);
232                 return ((void *) 0);
233         }
234
235         while (42)
236         {
237                 char buffer[1024];
238                 char buffer_copy[1024];
239                 char *fields[128];
240                 int   fields_num;
241                 int   len;
242
243                 errno = 0;
244                 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
245                 {
246                         if ((errno == EINTR) || (errno == EAGAIN))
247                                 continue;
248
249                         if (errno != 0)
250                         {
251                                 char errbuf[1024];
252                                 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
253                                                 fileno (fhin),
254                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
255                         }
256                         break;
257                 }
258
259                 len = strlen (buffer);
260                 while ((len > 0)
261                                 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
262                         buffer[--len] = '\0';
263
264                 if (len == 0)
265                         continue;
266
267                 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
268
269                 fields_num = strsplit (buffer_copy, fields,
270                                 sizeof (fields) / sizeof (fields[0]));
271                 if (fields_num < 1)
272                 {
273                         fprintf (fhout, "-1 Internal error\n");
274                         fclose (fhin);
275                         fclose (fhout);
276                         pthread_exit ((void *) 1);
277                         return ((void *) 1);
278                 }
279
280                 if (strcasecmp (fields[0], "getval") == 0)
281                 {
282                         handle_getval (fhout, buffer);
283                 }
284                 else if (strcasecmp (fields[0], "putval") == 0)
285                 {
286                         handle_putval (fhout, buffer);
287                 }
288                 else if (strcasecmp (fields[0], "listval") == 0)
289                 {
290                         handle_listval (fhout, buffer);
291                 }
292                 else if (strcasecmp (fields[0], "putnotif") == 0)
293                 {
294                         handle_putnotif (fhout, buffer);
295                 }
296                 else if (strcasecmp (fields[0], "flush") == 0)
297                 {
298                         handle_flush (fhout, buffer);
299                 }
300                 else
301                 {
302                         if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
303                         {
304                                 char errbuf[1024];
305                                 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
306                                                 fileno (fhout),
307                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
308                                 break;
309                         }
310                 }
311         } /* while (fgets) */
312
313         DEBUG ("unixsock plugin: us_handle_client: Exiting..");
314         fclose (fhin);
315         fclose (fhout);
316
317         pthread_exit ((void *) 0);
318         return ((void *) 0);
319 } /* void *us_handle_client */
320
321 static void *us_server_thread (void __attribute__((unused)) *arg)
322 {
323         int  status;
324         int *remote_fd;
325         pthread_t th;
326         pthread_attr_t th_attr;
327
328         pthread_attr_init (&th_attr);
329         pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
330
331         if (us_open_socket () != 0)
332                 pthread_exit ((void *) 1);
333
334         while (loop != 0)
335         {
336                 DEBUG ("unixsock plugin: Calling accept..");
337                 status = accept (sock_fd, NULL, NULL);
338                 if (status < 0)
339                 {
340                         char errbuf[1024];
341
342                         if (errno == EINTR)
343                                 continue;
344
345                         ERROR ("unixsock plugin: accept failed: %s",
346                                         sstrerror (errno, errbuf, sizeof (errbuf)));
347                         close (sock_fd);
348                         sock_fd = -1;
349                         pthread_attr_destroy (&th_attr);
350                         pthread_exit ((void *) 1);
351                 }
352
353                 remote_fd = (int *) malloc (sizeof (int));
354                 if (remote_fd == NULL)
355                 {
356                         char errbuf[1024];
357                         WARNING ("unixsock plugin: malloc failed: %s",
358                                         sstrerror (errno, errbuf, sizeof (errbuf)));
359                         close (status);
360                         continue;
361                 }
362                 *remote_fd = status;
363
364                 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
365
366                 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
367                 if (status != 0)
368                 {
369                         char errbuf[1024];
370                         WARNING ("unixsock plugin: pthread_create failed: %s",
371                                         sstrerror (errno, errbuf, sizeof (errbuf)));
372                         close (*remote_fd);
373                         free (remote_fd);
374                         continue;
375                 }
376         } /* while (loop) */
377
378         close (sock_fd);
379         sock_fd = -1;
380         pthread_attr_destroy (&th_attr);
381
382         status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
383         if (status != 0)
384         {
385                 char errbuf[1024];
386                 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
387                                 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
388                                 sstrerror (errno, errbuf, sizeof (errbuf)));
389         }
390
391         return ((void *) 0);
392 } /* void *us_server_thread */
393
394 static int us_config (const char *key, const char *val)
395 {
396         if (strcasecmp (key, "SocketFile") == 0)
397         {
398                 char *new_sock_file = strdup (val);
399                 if (new_sock_file == NULL)
400                         return (1);
401
402                 sfree (sock_file);
403                 sock_file = new_sock_file;
404         }
405         else if (strcasecmp (key, "SocketGroup") == 0)
406         {
407                 char *new_sock_group = strdup (val);
408                 if (new_sock_group == NULL)
409                         return (1);
410
411                 sfree (sock_group);
412                 sock_group = new_sock_group;
413         }
414         else if (strcasecmp (key, "SocketPerms") == 0)
415         {
416                 sock_perms = (int) strtol (val, NULL, 8);
417         }
418         else if (strcasecmp (key, "DeleteSocket") == 0)
419         {
420                 if (IS_TRUE (val))
421                         delete_socket = 1;
422                 else
423                         delete_socket = 0;
424         }
425         else
426         {
427                 return (-1);
428         }
429
430         return (0);
431 } /* int us_config */
432
433 static int us_init (void)
434 {
435         static int have_init = 0;
436
437         int status;
438
439         /* Initialize only once. */
440         if (have_init != 0)
441                 return (0);
442         have_init = 1;
443
444         loop = 1;
445
446         status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
447         if (status != 0)
448         {
449                 char errbuf[1024];
450                 ERROR ("unixsock plugin: pthread_create failed: %s",
451                                 sstrerror (errno, errbuf, sizeof (errbuf)));
452                 return (-1);
453         }
454
455         return (0);
456 } /* int us_init */
457
458 static int us_shutdown (void)
459 {
460         void *ret;
461
462         loop = 0;
463
464         if (listen_thread != (pthread_t) 0)
465         {
466                 pthread_kill (listen_thread, SIGTERM);
467                 pthread_join (listen_thread, &ret);
468                 listen_thread = (pthread_t) 0;
469         }
470
471         plugin_unregister_init ("unixsock");
472         plugin_unregister_shutdown ("unixsock");
473
474         return (0);
475 } /* int us_shutdown */
476
477 void module_register (void)
478 {
479         plugin_register_config ("unixsock", us_config,
480                         config_keys, config_keys_num);
481         plugin_register_init ("unixsock", us_init);
482         plugin_register_shutdown ("unixsock", us_shutdown);
483 } /* void module_register (void) */
484
485 /* vim: set sw=4 ts=4 sts=4 tw=78 : */