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>
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 = { 0 };
82 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
86 ERROR ("unixsock plugin: socket failed: %s",
87 sstrerror (errno, errbuf, sizeof (errbuf)));
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 status = chmod (sa.sun_path, sock_perms);
130 ERROR ("unixsock plugin: chmod failed: %s",
131 sstrerror (errno, errbuf, sizeof (errbuf)));
137 status = listen (sock_fd, 8);
141 ERROR ("unixsock plugin: listen failed: %s",
142 sstrerror (errno, errbuf, sizeof (errbuf)));
155 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
158 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
162 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
163 sstrerror (errno, errbuf, sizeof (errbuf)));
168 WARNING ("unixsock plugin: No such group: `%s'",
173 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
174 (uid_t) -1, g->gr_gid) != 0)
177 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
178 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
180 sstrerror (errno, errbuf, sizeof (errbuf)));
185 } /* int us_open_socket */
187 static void *us_handle_client (void *arg)
193 fdin = *((int *) arg);
197 DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
203 ERROR ("unixsock plugin: dup failed: %s",
204 sstrerror (errno, errbuf, sizeof (errbuf)));
206 pthread_exit ((void *) 1);
209 fhin = fdopen (fdin, "r");
213 ERROR ("unixsock plugin: fdopen failed: %s",
214 sstrerror (errno, errbuf, sizeof (errbuf)));
217 pthread_exit ((void *) 1);
221 fhout = fdopen (fdout, "w");
225 ERROR ("unixsock plugin: fdopen failed: %s",
226 sstrerror (errno, errbuf, sizeof (errbuf)));
227 fclose (fhin); /* this closes fdin as well */
229 pthread_exit ((void *) 1);
233 /* change output buffer to line buffered mode */
234 if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
237 ERROR ("unixsock plugin: setvbuf failed: %s",
238 sstrerror (errno, errbuf, sizeof (errbuf)));
241 pthread_exit ((void *) 1);
248 char buffer_copy[1024];
254 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
256 if ((errno == EINTR) || (errno == EAGAIN))
262 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
264 sstrerror (errno, errbuf, sizeof (errbuf)));
269 len = strlen (buffer);
271 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
272 buffer[--len] = '\0';
277 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
279 fields_num = strsplit (buffer_copy, fields,
280 sizeof (fields) / sizeof (fields[0]));
283 fprintf (fhout, "-1 Internal error\n");
286 pthread_exit ((void *) 1);
290 if (strcasecmp (fields[0], "getval") == 0)
292 handle_getval (fhout, buffer);
294 else if (strcasecmp (fields[0], "getthreshold") == 0)
296 handle_getthreshold (fhout, buffer);
298 else if (strcasecmp (fields[0], "putval") == 0)
300 handle_putval (fhout, buffer);
302 else if (strcasecmp (fields[0], "listval") == 0)
304 handle_listval (fhout, buffer);
306 else if (strcasecmp (fields[0], "putnotif") == 0)
308 handle_putnotif (fhout, buffer);
310 else if (strcasecmp (fields[0], "flush") == 0)
312 handle_flush (fhout, buffer);
316 if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
319 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
321 sstrerror (errno, errbuf, sizeof (errbuf)));
325 } /* while (fgets) */
327 DEBUG ("unixsock plugin: us_handle_client: Exiting..");
331 pthread_exit ((void *) 0);
333 } /* void *us_handle_client */
335 static void *us_server_thread (void __attribute__((unused)) *arg)
340 pthread_attr_t th_attr;
342 pthread_attr_init (&th_attr);
343 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
345 if (us_open_socket () != 0)
346 pthread_exit ((void *) 1);
350 DEBUG ("unixsock plugin: Calling accept..");
351 status = accept (sock_fd, NULL, NULL);
359 ERROR ("unixsock plugin: accept failed: %s",
360 sstrerror (errno, errbuf, sizeof (errbuf)));
363 pthread_attr_destroy (&th_attr);
364 pthread_exit ((void *) 1);
367 remote_fd = malloc (sizeof (*remote_fd));
368 if (remote_fd == NULL)
371 WARNING ("unixsock plugin: malloc failed: %s",
372 sstrerror (errno, errbuf, sizeof (errbuf)));
378 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
380 status = plugin_thread_create (&th, &th_attr,
381 us_handle_client, (void *) remote_fd);
385 WARNING ("unixsock plugin: pthread_create failed: %s",
386 sstrerror (errno, errbuf, sizeof (errbuf)));
395 pthread_attr_destroy (&th_attr);
397 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
401 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
402 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
403 sstrerror (errno, errbuf, sizeof (errbuf)));
407 } /* void *us_server_thread */
409 static int us_config (const char *key, const char *val)
411 if (strcasecmp (key, "SocketFile") == 0)
413 char *new_sock_file = strdup (val);
414 if (new_sock_file == NULL)
418 sock_file = new_sock_file;
420 else if (strcasecmp (key, "SocketGroup") == 0)
422 char *new_sock_group = strdup (val);
423 if (new_sock_group == NULL)
427 sock_group = new_sock_group;
429 else if (strcasecmp (key, "SocketPerms") == 0)
431 sock_perms = (int) strtol (val, NULL, 8);
433 else if (strcasecmp (key, "DeleteSocket") == 0)
446 } /* int us_config */
448 static int us_init (void)
450 static int have_init = 0;
454 /* Initialize only once. */
461 status = plugin_thread_create (&listen_thread, NULL,
462 us_server_thread, NULL);
466 ERROR ("unixsock plugin: pthread_create failed: %s",
467 sstrerror (errno, errbuf, sizeof (errbuf)));
474 static int us_shutdown (void)
480 if (listen_thread != (pthread_t) 0)
482 pthread_kill (listen_thread, SIGTERM);
483 pthread_join (listen_thread, &ret);
484 listen_thread = (pthread_t) 0;
487 plugin_unregister_init ("unixsock");
488 plugin_unregister_shutdown ("unixsock");
491 } /* int us_shutdown */
493 void module_register (void)
495 plugin_register_config ("unixsock", us_config,
496 config_keys, config_keys_num);
497 plugin_register_init ("unixsock", us_init);
498 plugin_register_shutdown ("unixsock", us_shutdown);
499 } /* void module_register (void) */
501 /* vim: set sw=4 ts=4 sts=4 tw=78 : */