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"
39 /* Folks without pthread will need to disable this plugin. */
42 #include <sys/socket.h>
49 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
52 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
57 /* valid configuration file keys */
58 static const char *config_keys[] =
65 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
69 /* socket configuration */
70 static int sock_fd = -1;
71 static char *sock_file = NULL;
72 static char *sock_group = NULL;
73 static int sock_perms = S_IRWXU | S_IRWXG;
74 static _Bool delete_socket = 0;
76 static pthread_t listen_thread = (pthread_t) 0;
81 static int us_open_socket (void)
83 struct sockaddr_un sa;
86 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
90 ERROR ("unixsock plugin: socket failed: %s",
91 sstrerror (errno, errbuf, sizeof (errbuf)));
95 memset (&sa, '\0', sizeof (sa));
96 sa.sun_family = AF_UNIX;
97 sstrncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
98 sizeof (sa.sun_path));
100 DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
105 status = unlink (sa.sun_path);
106 if ((status != 0) && (errno != ENOENT))
109 WARNING ("unixsock plugin: Deleting socket file \"%s\" failed: %s",
111 sstrerror (errno, errbuf, sizeof (errbuf)));
113 else if (status == 0)
115 INFO ("unixsock plugin: Successfully deleted socket file \"%s\".",
120 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
124 sstrerror (errno, errbuf, sizeof (errbuf));
125 ERROR ("unixsock plugin: bind failed: %s", errbuf);
131 chmod (sa.sun_path, sock_perms);
133 status = listen (sock_fd, 8);
137 ERROR ("unixsock plugin: listen failed: %s",
138 sstrerror (errno, errbuf, sizeof (errbuf)));
151 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
154 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
158 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
159 sstrerror (errno, errbuf, sizeof (errbuf)));
164 WARNING ("unixsock plugin: No such group: `%s'",
169 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
170 (uid_t) -1, g->gr_gid) != 0)
173 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
174 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
176 sstrerror (errno, errbuf, sizeof (errbuf)));
181 } /* int us_open_socket */
183 static void *us_handle_client (void *arg)
189 fdin = *((int *) arg);
193 DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
199 ERROR ("unixsock plugin: dup failed: %s",
200 sstrerror (errno, errbuf, sizeof (errbuf)));
202 pthread_exit ((void *) 1);
205 fhin = fdopen (fdin, "r");
209 ERROR ("unixsock plugin: fdopen failed: %s",
210 sstrerror (errno, errbuf, sizeof (errbuf)));
213 pthread_exit ((void *) 1);
217 fhout = fdopen (fdout, "w");
221 ERROR ("unixsock plugin: fdopen failed: %s",
222 sstrerror (errno, errbuf, sizeof (errbuf)));
223 fclose (fhin); /* this closes fdin as well */
225 pthread_exit ((void *) 1);
229 /* change output buffer to line buffered mode */
230 if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
233 ERROR ("unixsock plugin: setvbuf failed: %s",
234 sstrerror (errno, errbuf, sizeof (errbuf)));
237 pthread_exit ((void *) 1);
244 char buffer_copy[1024];
250 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
252 if ((errno == EINTR) || (errno == EAGAIN))
258 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
260 sstrerror (errno, errbuf, sizeof (errbuf)));
265 len = strlen (buffer);
267 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
268 buffer[--len] = '\0';
273 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
275 fields_num = strsplit (buffer_copy, fields,
276 sizeof (fields) / sizeof (fields[0]));
279 fprintf (fhout, "-1 Internal error\n");
282 pthread_exit ((void *) 1);
286 if (strcasecmp (fields[0], "getval") == 0)
288 handle_getval (fhout, buffer);
290 else if (strcasecmp (fields[0], "getthreshold") == 0)
292 handle_getthreshold (fhout, buffer);
294 else if (strcasecmp (fields[0], "putval") == 0)
296 handle_putval (fhout, buffer);
298 else if (strcasecmp (fields[0], "listval") == 0)
300 handle_listval (fhout, buffer);
302 else if (strcasecmp (fields[0], "putnotif") == 0)
304 handle_putnotif (fhout, buffer);
306 else if (strcasecmp (fields[0], "flush") == 0)
308 handle_flush (fhout, buffer);
312 if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
315 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
317 sstrerror (errno, errbuf, sizeof (errbuf)));
321 } /* while (fgets) */
323 DEBUG ("unixsock plugin: us_handle_client: Exiting..");
327 pthread_exit ((void *) 0);
329 } /* void *us_handle_client */
331 static void *us_server_thread (void __attribute__((unused)) *arg)
336 pthread_attr_t th_attr;
338 pthread_attr_init (&th_attr);
339 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
341 if (us_open_socket () != 0)
342 pthread_exit ((void *) 1);
346 DEBUG ("unixsock plugin: Calling accept..");
347 status = accept (sock_fd, NULL, NULL);
355 ERROR ("unixsock plugin: accept failed: %s",
356 sstrerror (errno, errbuf, sizeof (errbuf)));
359 pthread_attr_destroy (&th_attr);
360 pthread_exit ((void *) 1);
363 remote_fd = (int *) malloc (sizeof (int));
364 if (remote_fd == NULL)
367 WARNING ("unixsock plugin: malloc failed: %s",
368 sstrerror (errno, errbuf, sizeof (errbuf)));
374 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
376 status = plugin_thread_create (&th, &th_attr,
377 us_handle_client, (void *) remote_fd);
381 WARNING ("unixsock plugin: pthread_create failed: %s",
382 sstrerror (errno, errbuf, sizeof (errbuf)));
391 pthread_attr_destroy (&th_attr);
393 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
397 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
398 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
399 sstrerror (errno, errbuf, sizeof (errbuf)));
403 } /* void *us_server_thread */
405 static int us_config (const char *key, const char *val)
407 if (strcasecmp (key, "SocketFile") == 0)
409 char *new_sock_file = strdup (val);
410 if (new_sock_file == NULL)
414 sock_file = new_sock_file;
416 else if (strcasecmp (key, "SocketGroup") == 0)
418 char *new_sock_group = strdup (val);
419 if (new_sock_group == NULL)
423 sock_group = new_sock_group;
425 else if (strcasecmp (key, "SocketPerms") == 0)
427 sock_perms = (int) strtol (val, NULL, 8);
429 else if (strcasecmp (key, "DeleteSocket") == 0)
442 } /* int us_config */
444 static int us_init (void)
446 static int have_init = 0;
450 /* Initialize only once. */
457 status = plugin_thread_create (&listen_thread, NULL,
458 us_server_thread, NULL);
462 ERROR ("unixsock plugin: pthread_create failed: %s",
463 sstrerror (errno, errbuf, sizeof (errbuf)));
470 static int us_shutdown (void)
476 if (listen_thread != (pthread_t) 0)
478 pthread_kill (listen_thread, SIGTERM);
479 pthread_join (listen_thread, &ret);
480 listen_thread = (pthread_t) 0;
483 plugin_unregister_init ("unixsock");
484 plugin_unregister_shutdown ("unixsock");
487 } /* int us_shutdown */
489 void module_register (void)
491 plugin_register_config ("unixsock", us_config,
492 config_keys, config_keys_num);
493 plugin_register_init ("unixsock", us_init);
494 plugin_register_shutdown ("unixsock", us_shutdown);
495 } /* void module_register (void) */
497 /* vim: set sw=4 ts=4 sts=4 tw=78 : */