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);
212 fhout = fdopen (fdout, "w");
216 ERROR ("unixsock plugin: fdopen failed: %s",
217 sstrerror (errno, errbuf, sizeof (errbuf)));
218 fclose (fhin); /* this closes fdin as well */
220 pthread_exit ((void *) 1);
224 /* change output buffer to line buffered mode */
225 if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
228 ERROR ("unixsock plugin: setvbuf failed: %s",
229 sstrerror (errno, errbuf, sizeof (errbuf)));
232 pthread_exit ((void *) 1);
239 char buffer_copy[1024];
245 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
247 if ((errno == EINTR) || (errno == EAGAIN))
253 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
255 sstrerror (errno, errbuf, sizeof (errbuf)));
260 len = strlen (buffer);
262 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
263 buffer[--len] = '\0';
268 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
270 fields_num = strsplit (buffer_copy, fields,
271 sizeof (fields) / sizeof (fields[0]));
274 fprintf (fhout, "-1 Internal error\n");
277 pthread_exit ((void *) 1);
281 if (strcasecmp (fields[0], "getval") == 0)
283 handle_getval (fhout, buffer);
285 else if (strcasecmp (fields[0], "getthreshold") == 0)
287 handle_getthreshold (fhout, buffer);
289 else if (strcasecmp (fields[0], "putval") == 0)
291 handle_putval (fhout, buffer);
293 else if (strcasecmp (fields[0], "listval") == 0)
295 handle_listval (fhout, buffer);
297 else if (strcasecmp (fields[0], "putnotif") == 0)
299 handle_putnotif (fhout, buffer);
301 else if (strcasecmp (fields[0], "flush") == 0)
303 handle_flush (fhout, buffer);
307 if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
310 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
312 sstrerror (errno, errbuf, sizeof (errbuf)));
316 } /* while (fgets) */
318 DEBUG ("unixsock plugin: us_handle_client: Exiting..");
322 pthread_exit ((void *) 0);
324 } /* void *us_handle_client */
326 static void *us_server_thread (void __attribute__((unused)) *arg)
331 pthread_attr_t th_attr;
333 pthread_attr_init (&th_attr);
334 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
336 if (us_open_socket () != 0)
337 pthread_exit ((void *) 1);
341 DEBUG ("unixsock plugin: Calling accept..");
342 status = accept (sock_fd, NULL, NULL);
350 ERROR ("unixsock plugin: accept failed: %s",
351 sstrerror (errno, errbuf, sizeof (errbuf)));
354 pthread_attr_destroy (&th_attr);
355 pthread_exit ((void *) 1);
358 remote_fd = (int *) malloc (sizeof (int));
359 if (remote_fd == NULL)
362 WARNING ("unixsock plugin: malloc failed: %s",
363 sstrerror (errno, errbuf, sizeof (errbuf)));
369 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
371 status = plugin_thread_create (&th, &th_attr,
372 us_handle_client, (void *) remote_fd);
376 WARNING ("unixsock plugin: pthread_create failed: %s",
377 sstrerror (errno, errbuf, sizeof (errbuf)));
386 pthread_attr_destroy (&th_attr);
388 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
392 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
393 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
394 sstrerror (errno, errbuf, sizeof (errbuf)));
398 } /* void *us_server_thread */
400 static int us_config (const char *key, const char *val)
402 if (strcasecmp (key, "SocketFile") == 0)
404 char *new_sock_file = strdup (val);
405 if (new_sock_file == NULL)
409 sock_file = new_sock_file;
411 else if (strcasecmp (key, "SocketGroup") == 0)
413 char *new_sock_group = strdup (val);
414 if (new_sock_group == NULL)
418 sock_group = new_sock_group;
420 else if (strcasecmp (key, "SocketPerms") == 0)
422 sock_perms = (int) strtol (val, NULL, 8);
424 else if (strcasecmp (key, "DeleteSocket") == 0)
437 } /* int us_config */
439 static int us_init (void)
441 static int have_init = 0;
445 /* Initialize only once. */
452 status = plugin_thread_create (&listen_thread, NULL,
453 us_server_thread, NULL);
457 ERROR ("unixsock plugin: pthread_create failed: %s",
458 sstrerror (errno, errbuf, sizeof (errbuf)));
465 static int us_shutdown (void)
471 if (listen_thread != (pthread_t) 0)
473 pthread_kill (listen_thread, SIGTERM);
474 pthread_join (listen_thread, &ret);
475 listen_thread = (pthread_t) 0;
478 plugin_unregister_init ("unixsock");
479 plugin_unregister_shutdown ("unixsock");
482 } /* int us_shutdown */
484 void module_register (void)
486 plugin_register_config ("unixsock", us_config,
487 config_keys, config_keys_num);
488 plugin_register_init ("unixsock", us_init);
489 plugin_register_shutdown ("unixsock", us_shutdown);
490 } /* void module_register (void) */
492 /* vim: set sw=4 ts=4 sts=4 tw=78 : */