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_listval.h"
30 #include "utils_cmd_putval.h"
31 #include "utils_cmd_putnotif.h"
33 /* Folks without pthread will need to disable this plugin. */
36 #include <sys/socket.h>
43 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
46 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
51 /* valid configuration file keys */
52 static const char *config_keys[] =
59 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
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;
70 static pthread_t listen_thread = (pthread_t) 0;
75 static int us_open_socket (void)
77 struct sockaddr_un sa;
80 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
84 ERROR ("unixsock plugin: socket failed: %s",
85 sstrerror (errno, errbuf, sizeof (errbuf)));
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));
94 DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
99 status = unlink (sa.sun_path);
100 if ((status != 0) && (errno != ENOENT))
103 WARNING ("unixsock plugin: Deleting socket file \"%s\" failed: %s",
105 sstrerror (errno, errbuf, sizeof (errbuf)));
107 else if (status == 0)
109 INFO ("unixsock plugin: Successfully deleted socket file \"%s\".",
114 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
118 sstrerror (errno, errbuf, sizeof (errbuf));
119 ERROR ("unixsock plugin: bind failed: %s", errbuf);
125 chmod (sa.sun_path, sock_perms);
127 status = listen (sock_fd, 8);
131 ERROR ("unixsock plugin: listen failed: %s",
132 sstrerror (errno, errbuf, sizeof (errbuf)));
145 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
148 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
152 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
153 sstrerror (errno, errbuf, sizeof (errbuf)));
158 WARNING ("unixsock plugin: No such group: `%s'",
163 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
164 (uid_t) -1, g->gr_gid) != 0)
167 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
168 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
170 sstrerror (errno, errbuf, sizeof (errbuf)));
175 } /* int us_open_socket */
177 static void *us_handle_client (void *arg)
183 fdin = *((int *) arg);
187 DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
193 ERROR ("unixsock plugin: dup failed: %s",
194 sstrerror (errno, errbuf, sizeof (errbuf)));
196 pthread_exit ((void *) 1);
199 fhin = fdopen (fdin, "r");
203 ERROR ("unixsock plugin: fdopen failed: %s",
204 sstrerror (errno, errbuf, sizeof (errbuf)));
207 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);
223 /* change output buffer to line buffered mode */
224 if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
227 ERROR ("unixsock plugin: setvbuf failed: %s",
228 sstrerror (errno, errbuf, sizeof (errbuf)));
231 pthread_exit ((void *) 1);
238 char buffer_copy[1024];
244 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
246 if ((errno == EINTR) || (errno == EAGAIN))
252 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
254 sstrerror (errno, errbuf, sizeof (errbuf)));
259 len = strlen (buffer);
261 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
262 buffer[--len] = '\0';
267 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
269 fields_num = strsplit (buffer_copy, fields,
270 sizeof (fields) / sizeof (fields[0]));
273 fprintf (fhout, "-1 Internal error\n");
276 pthread_exit ((void *) 1);
280 if (strcasecmp (fields[0], "getval") == 0)
282 handle_getval (fhout, buffer);
284 else if (strcasecmp (fields[0], "putval") == 0)
286 handle_putval (fhout, buffer);
288 else if (strcasecmp (fields[0], "listval") == 0)
290 handle_listval (fhout, buffer);
292 else if (strcasecmp (fields[0], "putnotif") == 0)
294 handle_putnotif (fhout, buffer);
296 else if (strcasecmp (fields[0], "flush") == 0)
298 handle_flush (fhout, buffer);
302 if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
305 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
307 sstrerror (errno, errbuf, sizeof (errbuf)));
311 } /* while (fgets) */
313 DEBUG ("unixsock plugin: us_handle_client: Exiting..");
317 pthread_exit ((void *) 0);
319 } /* void *us_handle_client */
321 static void *us_server_thread (void __attribute__((unused)) *arg)
326 pthread_attr_t th_attr;
328 pthread_attr_init (&th_attr);
329 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
331 if (us_open_socket () != 0)
332 pthread_exit ((void *) 1);
336 DEBUG ("unixsock plugin: Calling accept..");
337 status = accept (sock_fd, NULL, NULL);
345 ERROR ("unixsock plugin: accept failed: %s",
346 sstrerror (errno, errbuf, sizeof (errbuf)));
349 pthread_attr_destroy (&th_attr);
350 pthread_exit ((void *) 1);
353 remote_fd = (int *) malloc (sizeof (int));
354 if (remote_fd == NULL)
357 WARNING ("unixsock plugin: malloc failed: %s",
358 sstrerror (errno, errbuf, sizeof (errbuf)));
364 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
366 status = plugin_thread_create (&th, &th_attr,
367 us_handle_client, (void *) remote_fd);
371 WARNING ("unixsock plugin: pthread_create failed: %s",
372 sstrerror (errno, errbuf, sizeof (errbuf)));
381 pthread_attr_destroy (&th_attr);
383 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
387 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
388 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
389 sstrerror (errno, errbuf, sizeof (errbuf)));
393 } /* void *us_server_thread */
395 static int us_config (const char *key, const char *val)
397 if (strcasecmp (key, "SocketFile") == 0)
399 char *new_sock_file = strdup (val);
400 if (new_sock_file == NULL)
404 sock_file = new_sock_file;
406 else if (strcasecmp (key, "SocketGroup") == 0)
408 char *new_sock_group = strdup (val);
409 if (new_sock_group == NULL)
413 sock_group = new_sock_group;
415 else if (strcasecmp (key, "SocketPerms") == 0)
417 sock_perms = (int) strtol (val, NULL, 8);
419 else if (strcasecmp (key, "DeleteSocket") == 0)
432 } /* int us_config */
434 static int us_init (void)
436 static int have_init = 0;
440 /* Initialize only once. */
447 status = plugin_thread_create (&listen_thread, NULL,
448 us_server_thread, NULL);
452 ERROR ("unixsock plugin: pthread_create failed: %s",
453 sstrerror (errno, errbuf, sizeof (errbuf)));
460 static int us_shutdown (void)
466 if (listen_thread != (pthread_t) 0)
468 pthread_kill (listen_thread, SIGTERM);
469 pthread_join (listen_thread, &ret);
470 listen_thread = (pthread_t) 0;
473 plugin_unregister_init ("unixsock");
474 plugin_unregister_shutdown ("unixsock");
477 } /* int us_shutdown */
479 void module_register (void)
481 plugin_register_config ("unixsock", us_config,
482 config_keys, config_keys_num);
483 plugin_register_init ("unixsock", us_init);
484 plugin_register_shutdown ("unixsock", us_shutdown);
485 } /* void module_register (void) */
487 /* vim: set sw=4 ts=4 sts=4 tw=78 : */