2 * collectd - src/unixsock.c
3 * Copyright (C) 2007,2008 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
30 #include "configfile.h"
32 #include "utils_cmd_flush.h"
33 #include "utils_cmd_getval.h"
34 #include "utils_cmd_getthreshold.h"
35 #include "utils_cmd_listval.h"
36 #include "utils_cmd_putval.h"
37 #include "utils_cmd_putnotif.h"
45 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
48 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
53 /* valid configuration file keys */
54 static const char *config_keys[] =
61 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
65 /* socket configuration */
66 static int sock_fd = -1;
67 static char *sock_file = NULL;
68 static char *sock_group = NULL;
69 static int sock_perms = S_IRWXU | S_IRWXG;
70 static _Bool delete_socket = 0;
72 static pthread_t listen_thread = (pthread_t) 0;
77 static int us_open_socket (void)
79 struct sockaddr_un sa;
82 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
86 ERROR ("unixsock plugin: socket failed: %s",
87 sstrerror (errno, errbuf, sizeof (errbuf)));
91 memset (&sa, '\0', sizeof (sa));
92 sa.sun_family = AF_UNIX;
93 sstrncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
94 sizeof (sa.sun_path));
96 DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
101 status = unlink (sa.sun_path);
102 if ((status != 0) && (errno != ENOENT))
105 WARNING ("unixsock plugin: Deleting socket file \"%s\" failed: %s",
107 sstrerror (errno, errbuf, sizeof (errbuf)));
109 else if (status == 0)
111 INFO ("unixsock plugin: Successfully deleted socket file \"%s\".",
116 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
120 sstrerror (errno, errbuf, sizeof (errbuf));
121 ERROR ("unixsock plugin: bind failed: %s", errbuf);
127 status = chmod (sa.sun_path, sock_perms);
131 ERROR ("unixsock plugin: chmod failed: %s",
132 sstrerror (errno, errbuf, sizeof (errbuf)));
138 status = listen (sock_fd, 8);
142 ERROR ("unixsock plugin: listen failed: %s",
143 sstrerror (errno, errbuf, sizeof (errbuf)));
156 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
159 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
163 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
164 sstrerror (errno, errbuf, sizeof (errbuf)));
169 WARNING ("unixsock plugin: No such group: `%s'",
174 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
175 (uid_t) -1, g->gr_gid) != 0)
178 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
179 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
181 sstrerror (errno, errbuf, sizeof (errbuf)));
186 } /* int us_open_socket */
188 static void *us_handle_client (void *arg)
194 fdin = *((int *) arg);
198 DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
204 ERROR ("unixsock plugin: dup failed: %s",
205 sstrerror (errno, errbuf, sizeof (errbuf)));
207 pthread_exit ((void *) 1);
210 fhin = fdopen (fdin, "r");
214 ERROR ("unixsock plugin: fdopen failed: %s",
215 sstrerror (errno, errbuf, sizeof (errbuf)));
218 pthread_exit ((void *) 1);
222 fhout = fdopen (fdout, "w");
226 ERROR ("unixsock plugin: fdopen failed: %s",
227 sstrerror (errno, errbuf, sizeof (errbuf)));
228 fclose (fhin); /* this closes fdin as well */
230 pthread_exit ((void *) 1);
234 /* change output buffer to line buffered mode */
235 if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
238 ERROR ("unixsock plugin: setvbuf failed: %s",
239 sstrerror (errno, errbuf, sizeof (errbuf)));
242 pthread_exit ((void *) 1);
249 char buffer_copy[1024];
255 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
257 if ((errno == EINTR) || (errno == EAGAIN))
263 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
265 sstrerror (errno, errbuf, sizeof (errbuf)));
270 len = strlen (buffer);
272 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
273 buffer[--len] = '\0';
278 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
280 fields_num = strsplit (buffer_copy, fields,
281 sizeof (fields) / sizeof (fields[0]));
284 fprintf (fhout, "-1 Internal error\n");
287 pthread_exit ((void *) 1);
291 if (strcasecmp (fields[0], "getval") == 0)
293 handle_getval (fhout, buffer);
295 else if (strcasecmp (fields[0], "getthreshold") == 0)
297 handle_getthreshold (fhout, buffer);
299 else if (strcasecmp (fields[0], "putval") == 0)
301 handle_putval (fhout, buffer);
303 else if (strcasecmp (fields[0], "listval") == 0)
305 handle_listval (fhout, buffer);
307 else if (strcasecmp (fields[0], "putnotif") == 0)
309 handle_putnotif (fhout, buffer);
311 else if (strcasecmp (fields[0], "flush") == 0)
313 handle_flush (fhout, buffer);
317 if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
320 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
322 sstrerror (errno, errbuf, sizeof (errbuf)));
326 } /* while (fgets) */
328 DEBUG ("unixsock plugin: us_handle_client: Exiting..");
332 pthread_exit ((void *) 0);
334 } /* void *us_handle_client */
336 static void *us_server_thread (void __attribute__((unused)) *arg)
341 pthread_attr_t th_attr;
343 pthread_attr_init (&th_attr);
344 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
346 if (us_open_socket () != 0)
347 pthread_exit ((void *) 1);
351 DEBUG ("unixsock plugin: Calling accept..");
352 status = accept (sock_fd, NULL, NULL);
360 ERROR ("unixsock plugin: accept failed: %s",
361 sstrerror (errno, errbuf, sizeof (errbuf)));
364 pthread_attr_destroy (&th_attr);
365 pthread_exit ((void *) 1);
368 remote_fd = malloc (sizeof (*remote_fd));
369 if (remote_fd == NULL)
372 WARNING ("unixsock plugin: malloc failed: %s",
373 sstrerror (errno, errbuf, sizeof (errbuf)));
379 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
381 status = plugin_thread_create (&th, &th_attr,
382 us_handle_client, (void *) remote_fd);
386 WARNING ("unixsock plugin: pthread_create failed: %s",
387 sstrerror (errno, errbuf, sizeof (errbuf)));
396 pthread_attr_destroy (&th_attr);
398 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
402 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
403 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
404 sstrerror (errno, errbuf, sizeof (errbuf)));
408 } /* void *us_server_thread */
410 static int us_config (const char *key, const char *val)
412 if (strcasecmp (key, "SocketFile") == 0)
414 char *new_sock_file = strdup (val);
415 if (new_sock_file == NULL)
419 sock_file = new_sock_file;
421 else if (strcasecmp (key, "SocketGroup") == 0)
423 char *new_sock_group = strdup (val);
424 if (new_sock_group == NULL)
428 sock_group = new_sock_group;
430 else if (strcasecmp (key, "SocketPerms") == 0)
432 sock_perms = (int) strtol (val, NULL, 8);
434 else if (strcasecmp (key, "DeleteSocket") == 0)
447 } /* int us_config */
449 static int us_init (void)
451 static int have_init = 0;
455 /* Initialize only once. */
462 status = plugin_thread_create (&listen_thread, NULL,
463 us_server_thread, NULL);
467 ERROR ("unixsock plugin: pthread_create failed: %s",
468 sstrerror (errno, errbuf, sizeof (errbuf)));
475 static int us_shutdown (void)
481 if (listen_thread != (pthread_t) 0)
483 pthread_kill (listen_thread, SIGTERM);
484 pthread_join (listen_thread, &ret);
485 listen_thread = (pthread_t) 0;
488 plugin_unregister_init ("unixsock");
489 plugin_unregister_shutdown ("unixsock");
492 } /* int us_shutdown */
494 void module_register (void)
496 plugin_register_config ("unixsock", us_config,
497 config_keys, config_keys_num);
498 plugin_register_init ("unixsock", us_init);
499 plugin_register_shutdown ("unixsock", us_shutdown);
500 } /* void module_register (void) */
502 /* vim: set sw=4 ts=4 sts=4 tw=78 : */