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[] =
58 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
62 /* socket configuration */
63 static int sock_fd = -1;
64 static char *sock_file = NULL;
65 static char *sock_group = NULL;
66 static int sock_perms = S_IRWXU | S_IRWXG;
68 static pthread_t listen_thread = (pthread_t) 0;
73 static int us_open_socket (void)
75 struct sockaddr_un sa;
78 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
82 ERROR ("unixsock plugin: socket failed: %s",
83 sstrerror (errno, errbuf, sizeof (errbuf)));
87 memset (&sa, '\0', sizeof (sa));
88 sa.sun_family = AF_UNIX;
89 strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
90 sizeof (sa.sun_path) - 1);
91 /* unlink (sa.sun_path); */
93 DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
95 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
99 sstrerror (errno, errbuf, sizeof (errbuf));
100 ERROR ("unixsock plugin: bind failed: %s", errbuf);
106 chmod (sa.sun_path, sock_perms);
108 status = listen (sock_fd, 8);
112 ERROR ("unixsock plugin: listen failed: %s",
113 sstrerror (errno, errbuf, sizeof (errbuf)));
126 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
129 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
133 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
134 sstrerror (errno, errbuf, sizeof (errbuf)));
139 WARNING ("unixsock plugin: No such group: `%s'",
144 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
145 (uid_t) -1, g->gr_gid) != 0)
148 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
149 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
151 sstrerror (errno, errbuf, sizeof (errbuf)));
156 } /* int us_open_socket */
158 static void *us_handle_client (void *arg)
170 DEBUG ("Reading from fd #%i", fd);
172 fhin = fdopen (fd, "r");
176 ERROR ("unixsock plugin: fdopen failed: %s",
177 sstrerror (errno, errbuf, sizeof (errbuf)));
179 pthread_exit ((void *) 1);
182 fhout = fdopen (fd, "w");
186 ERROR ("unixsock plugin: fdopen failed: %s",
187 sstrerror (errno, errbuf, sizeof (errbuf)));
188 fclose (fhin); /* this closes fd as well */
189 pthread_exit ((void *) 1);
192 while (fgets (buffer, sizeof (buffer), fhin) != NULL)
196 len = strlen (buffer);
198 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
199 buffer[--len] = '\0';
204 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
206 fields_num = strsplit (buffer, fields,
207 sizeof (fields) / sizeof (fields[0]));
215 if (strcasecmp (fields[0], "getval") == 0)
217 handle_getval (fhout, fields, fields_num);
219 else if (strcasecmp (fields[0], "putval") == 0)
221 handle_putval (fhout, fields, fields_num);
223 else if (strcasecmp (fields[0], "listval") == 0)
225 handle_listval (fhout, fields, fields_num);
227 else if (strcasecmp (fields[0], "putnotif") == 0)
229 handle_putnotif (fhout, fields, fields_num);
231 else if (strcasecmp (fields[0], "flush") == 0)
233 handle_flush (fhout, fields, fields_num);
237 fprintf (fhout, "-1 Unknown command: %s\n", fields[0]);
240 } /* while (fgets) */
246 pthread_exit ((void *) 0);
248 } /* void *us_handle_client */
250 static void *us_server_thread (void *arg)
255 pthread_attr_t th_attr;
257 if (us_open_socket () != 0)
258 pthread_exit ((void *) 1);
262 DEBUG ("unixsock plugin: Calling accept..");
263 status = accept (sock_fd, NULL, NULL);
271 ERROR ("unixsock plugin: accept failed: %s",
272 sstrerror (errno, errbuf, sizeof (errbuf)));
275 pthread_exit ((void *) 1);
278 remote_fd = (int *) malloc (sizeof (int));
279 if (remote_fd == NULL)
282 WARNING ("unixsock plugin: malloc failed: %s",
283 sstrerror (errno, errbuf, sizeof (errbuf)));
289 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
291 pthread_attr_init (&th_attr);
292 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
294 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
298 WARNING ("unixsock plugin: pthread_create failed: %s",
299 sstrerror (errno, errbuf, sizeof (errbuf)));
309 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
313 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
314 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
315 sstrerror (errno, errbuf, sizeof (errbuf)));
319 } /* void *us_server_thread */
321 static int us_config (const char *key, const char *val)
323 if (strcasecmp (key, "SocketFile") == 0)
325 char *new_sock_file = strdup (val);
326 if (new_sock_file == NULL)
330 sock_file = new_sock_file;
332 else if (strcasecmp (key, "SocketGroup") == 0)
334 char *new_sock_group = strdup (val);
335 if (new_sock_group == NULL)
339 sock_group = new_sock_group;
341 else if (strcasecmp (key, "SocketPerms") == 0)
343 sock_perms = (int) strtol (val, NULL, 8);
351 } /* int us_config */
353 static int us_init (void)
359 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
363 ERROR ("unixsock plugin: pthread_create failed: %s",
364 sstrerror (errno, errbuf, sizeof (errbuf)));
371 static int us_shutdown (void)
377 if (listen_thread != (pthread_t) 0)
379 pthread_kill (listen_thread, SIGTERM);
380 pthread_join (listen_thread, &ret);
381 listen_thread = (pthread_t) 0;
384 plugin_unregister_init ("unixsock");
385 plugin_unregister_shutdown ("unixsock");
388 } /* int us_shutdown */
390 void module_register (void)
392 plugin_register_config ("unixsock", us_config,
393 config_keys, config_keys_num);
394 plugin_register_init ("unixsock", us_init);
395 plugin_register_shutdown ("unixsock", us_shutdown);
396 } /* void module_register (void) */
398 /* vim: set sw=4 ts=4 sts=4 tw=78 : */