2 * collectd - src/unixsock.c
3 * Copyright (C) 2007,2008 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 #include "configfile.h"
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"
34 /* Folks without pthread will need to disable this plugin. */
37 #include <sys/socket.h>
44 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
47 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
52 /* valid configuration file keys */
53 static const char *config_keys[] =
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
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;
71 static pthread_t listen_thread = (pthread_t) 0;
76 static int us_open_socket (void)
78 struct sockaddr_un sa;
81 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
85 ERROR ("unixsock plugin: socket failed: %s",
86 sstrerror (errno, errbuf, sizeof (errbuf)));
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));
95 DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
100 status = unlink (sa.sun_path);
101 if ((status != 0) && (errno != ENOENT))
104 WARNING ("unixsock plugin: Deleting socket file \"%s\" failed: %s",
106 sstrerror (errno, errbuf, sizeof (errbuf)));
108 else if (status == 0)
110 INFO ("unixsock plugin: Successfully deleted socket file \"%s\".",
115 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
119 sstrerror (errno, errbuf, sizeof (errbuf));
120 ERROR ("unixsock plugin: bind failed: %s", errbuf);
126 chmod (sa.sun_path, sock_perms);
128 status = listen (sock_fd, 8);
132 ERROR ("unixsock plugin: listen failed: %s",
133 sstrerror (errno, errbuf, sizeof (errbuf)));
146 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
149 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
153 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
154 sstrerror (errno, errbuf, sizeof (errbuf)));
159 WARNING ("unixsock plugin: No such group: `%s'",
164 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
165 (uid_t) -1, g->gr_gid) != 0)
168 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
169 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
171 sstrerror (errno, errbuf, sizeof (errbuf)));
176 } /* int us_open_socket */
178 static void *us_handle_client (void *arg)
184 fdin = *((int *) arg);
188 DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
194 ERROR ("unixsock plugin: dup failed: %s",
195 sstrerror (errno, errbuf, sizeof (errbuf)));
197 pthread_exit ((void *) 1);
200 fhin = fdopen (fdin, "r");
204 ERROR ("unixsock plugin: fdopen failed: %s",
205 sstrerror (errno, errbuf, sizeof (errbuf)));
208 pthread_exit ((void *) 1);
211 fhout = fdopen (fdout, "w");
215 ERROR ("unixsock plugin: fdopen failed: %s",
216 sstrerror (errno, errbuf, sizeof (errbuf)));
217 fclose (fhin); /* this closes fdin as well */
219 pthread_exit ((void *) 1);
222 /* change output buffer to line buffered mode */
223 if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
226 ERROR ("unixsock plugin: setvbuf failed: %s",
227 sstrerror (errno, errbuf, sizeof (errbuf)));
230 pthread_exit ((void *) 1);
236 char buffer_copy[1024];
242 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
247 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
249 sstrerror (errno, errbuf, sizeof (errbuf)));
254 len = strlen (buffer);
256 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
257 buffer[--len] = '\0';
262 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
264 fields_num = strsplit (buffer_copy, fields,
265 sizeof (fields) / sizeof (fields[0]));
268 fprintf (fhout, "-1 Internal error\n");
271 pthread_exit ((void *) 1);
274 if (strcasecmp (fields[0], "getval") == 0)
276 handle_getval (fhout, buffer);
278 else if (strcasecmp (fields[0], "getthreshold") == 0)
280 handle_getthreshold (fhout, buffer);
282 else if (strcasecmp (fields[0], "putval") == 0)
284 handle_putval (fhout, buffer);
286 else if (strcasecmp (fields[0], "listval") == 0)
288 handle_listval (fhout, buffer);
290 else if (strcasecmp (fields[0], "putnotif") == 0)
292 handle_putnotif (fhout, buffer);
294 else if (strcasecmp (fields[0], "flush") == 0)
296 handle_flush (fhout, buffer);
300 if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
303 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
305 sstrerror (errno, errbuf, sizeof (errbuf)));
309 } /* while (fgets) */
311 DEBUG ("unixsock plugin: us_handle_client: Exiting..");
315 pthread_exit ((void *) 0);
317 } /* void *us_handle_client */
319 static void *us_server_thread (void __attribute__((unused)) *arg)
324 pthread_attr_t th_attr;
326 if (us_open_socket () != 0)
327 pthread_exit ((void *) 1);
331 DEBUG ("unixsock plugin: Calling accept..");
332 status = accept (sock_fd, NULL, NULL);
340 ERROR ("unixsock plugin: accept failed: %s",
341 sstrerror (errno, errbuf, sizeof (errbuf)));
344 pthread_exit ((void *) 1);
347 remote_fd = (int *) malloc (sizeof (int));
348 if (remote_fd == NULL)
351 WARNING ("unixsock plugin: malloc failed: %s",
352 sstrerror (errno, errbuf, sizeof (errbuf)));
358 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
360 pthread_attr_init (&th_attr);
361 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
363 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
367 WARNING ("unixsock plugin: pthread_create failed: %s",
368 sstrerror (errno, errbuf, sizeof (errbuf)));
378 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
382 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
383 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
384 sstrerror (errno, errbuf, sizeof (errbuf)));
388 } /* void *us_server_thread */
390 static int us_config (const char *key, const char *val)
392 if (strcasecmp (key, "SocketFile") == 0)
394 char *new_sock_file = strdup (val);
395 if (new_sock_file == NULL)
399 sock_file = new_sock_file;
401 else if (strcasecmp (key, "SocketGroup") == 0)
403 char *new_sock_group = strdup (val);
404 if (new_sock_group == NULL)
408 sock_group = new_sock_group;
410 else if (strcasecmp (key, "SocketPerms") == 0)
412 sock_perms = (int) strtol (val, NULL, 8);
414 else if (strcasecmp (key, "DeleteSocket") == 0)
427 } /* int us_config */
429 static int us_init (void)
431 static int have_init = 0;
435 /* Initialize only once. */
442 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
446 ERROR ("unixsock plugin: pthread_create failed: %s",
447 sstrerror (errno, errbuf, sizeof (errbuf)));
454 static int us_shutdown (void)
460 if (listen_thread != (pthread_t) 0)
462 pthread_kill (listen_thread, SIGTERM);
463 pthread_join (listen_thread, &ret);
464 listen_thread = (pthread_t) 0;
467 plugin_unregister_init ("unixsock");
468 plugin_unregister_shutdown ("unixsock");
471 } /* int us_shutdown */
473 void module_register (void)
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) */
481 /* vim: set sw=4 ts=4 sts=4 tw=78 : */