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[] =
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;
69 static pthread_t listen_thread = (pthread_t) 0;
74 static int us_open_socket (void)
76 struct sockaddr_un sa;
79 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
83 ERROR ("unixsock plugin: socket failed: %s",
84 sstrerror (errno, errbuf, sizeof (errbuf)));
88 memset (&sa, '\0', sizeof (sa));
89 sa.sun_family = AF_UNIX;
90 sstrncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
91 sizeof (sa.sun_path));
92 /* unlink (sa.sun_path); */
94 DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
96 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
100 sstrerror (errno, errbuf, sizeof (errbuf));
101 ERROR ("unixsock plugin: bind failed: %s", errbuf);
107 chmod (sa.sun_path, sock_perms);
109 status = listen (sock_fd, 8);
113 ERROR ("unixsock plugin: listen failed: %s",
114 sstrerror (errno, errbuf, sizeof (errbuf)));
127 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
130 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
134 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
135 sstrerror (errno, errbuf, sizeof (errbuf)));
140 WARNING ("unixsock plugin: No such group: `%s'",
145 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
146 (uid_t) -1, g->gr_gid) != 0)
149 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
150 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
152 sstrerror (errno, errbuf, sizeof (errbuf)));
157 } /* int us_open_socket */
159 static void *us_handle_client (void *arg)
165 fdin = *((int *) arg);
169 DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
175 ERROR ("unixsock plugin: dup failed: %s",
176 sstrerror (errno, errbuf, sizeof (errbuf)));
178 pthread_exit ((void *) 1);
181 fhin = fdopen (fdin, "r");
185 ERROR ("unixsock plugin: fdopen failed: %s",
186 sstrerror (errno, errbuf, sizeof (errbuf)));
189 pthread_exit ((void *) 1);
192 fhout = fdopen (fdout, "w");
196 ERROR ("unixsock plugin: fdopen failed: %s",
197 sstrerror (errno, errbuf, sizeof (errbuf)));
198 fclose (fhin); /* this closes fdin as well */
200 pthread_exit ((void *) 1);
203 /* change output buffer to line buffered mode */
204 if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
207 ERROR ("unixsock plugin: setvbuf failed: %s",
208 sstrerror (errno, errbuf, sizeof (errbuf)));
211 pthread_exit ((void *) 1);
217 char buffer_copy[1024];
223 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
228 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
230 sstrerror (errno, errbuf, sizeof (errbuf)));
235 len = strlen (buffer);
237 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
238 buffer[--len] = '\0';
243 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
245 fields_num = strsplit (buffer_copy, fields,
246 sizeof (fields) / sizeof (fields[0]));
249 fprintf (fhout, "-1 Internal error\n");
252 pthread_exit ((void *) 1);
255 if (strcasecmp (fields[0], "getval") == 0)
257 handle_getval (fhout, buffer);
259 else if (strcasecmp (fields[0], "getthreshold") == 0)
261 handle_getthreshold (fhout, buffer);
263 else if (strcasecmp (fields[0], "putval") == 0)
265 handle_putval (fhout, buffer);
267 else if (strcasecmp (fields[0], "listval") == 0)
269 handle_listval (fhout, buffer);
271 else if (strcasecmp (fields[0], "putnotif") == 0)
273 handle_putnotif (fhout, buffer);
275 else if (strcasecmp (fields[0], "flush") == 0)
277 handle_flush (fhout, buffer);
281 if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
284 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
286 sstrerror (errno, errbuf, sizeof (errbuf)));
290 } /* while (fgets) */
292 DEBUG ("unixsock plugin: us_handle_client: Exiting..");
296 pthread_exit ((void *) 0);
298 } /* void *us_handle_client */
300 static void *us_server_thread (void __attribute__((unused)) *arg)
305 pthread_attr_t th_attr;
307 if (us_open_socket () != 0)
308 pthread_exit ((void *) 1);
312 DEBUG ("unixsock plugin: Calling accept..");
313 status = accept (sock_fd, NULL, NULL);
321 ERROR ("unixsock plugin: accept failed: %s",
322 sstrerror (errno, errbuf, sizeof (errbuf)));
325 pthread_exit ((void *) 1);
328 remote_fd = (int *) malloc (sizeof (int));
329 if (remote_fd == NULL)
332 WARNING ("unixsock plugin: malloc failed: %s",
333 sstrerror (errno, errbuf, sizeof (errbuf)));
339 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
341 pthread_attr_init (&th_attr);
342 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
344 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
348 WARNING ("unixsock plugin: pthread_create failed: %s",
349 sstrerror (errno, errbuf, sizeof (errbuf)));
359 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
363 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
364 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
365 sstrerror (errno, errbuf, sizeof (errbuf)));
369 } /* void *us_server_thread */
371 static int us_config (const char *key, const char *val)
373 if (strcasecmp (key, "SocketFile") == 0)
375 char *new_sock_file = strdup (val);
376 if (new_sock_file == NULL)
380 sock_file = new_sock_file;
382 else if (strcasecmp (key, "SocketGroup") == 0)
384 char *new_sock_group = strdup (val);
385 if (new_sock_group == NULL)
389 sock_group = new_sock_group;
391 else if (strcasecmp (key, "SocketPerms") == 0)
393 sock_perms = (int) strtol (val, NULL, 8);
401 } /* int us_config */
403 static int us_init (void)
405 static int have_init = 0;
409 /* Initialize only once. */
416 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
420 ERROR ("unixsock plugin: pthread_create failed: %s",
421 sstrerror (errno, errbuf, sizeof (errbuf)));
428 static int us_shutdown (void)
434 if (listen_thread != (pthread_t) 0)
436 pthread_kill (listen_thread, SIGTERM);
437 pthread_join (listen_thread, &ret);
438 listen_thread = (pthread_t) 0;
441 plugin_unregister_init ("unixsock");
442 plugin_unregister_shutdown ("unixsock");
445 } /* int us_shutdown */
447 void module_register (void)
449 plugin_register_config ("unixsock", us_config,
450 config_keys, config_keys_num);
451 plugin_register_init ("unixsock", us_init);
452 plugin_register_shutdown ("unixsock", us_shutdown);
453 } /* void module_register (void) */
455 /* vim: set sw=4 ts=4 sts=4 tw=78 : */