2 * collectd - src/netcmd.c
3 * Copyright (C) 2007-2009 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 verplant.org>
30 #include "configfile.h"
32 #include "utils_cmd_flush.h"
33 #include "utils_cmd_getval.h"
34 #include "utils_cmd_listval.h"
35 #include "utils_cmd_putval.h"
36 #include "utils_cmd_putnotif.h"
38 /* Folks without pthread will need to disable this plugin. */
41 #include <sys/socket.h>
49 #define NC_DEFAULT_PORT "25826"
52 * Private data structures
60 typedef struct socket_entry_s socket_entry_t;
65 /* valid configuration file keys */
66 static const char *config_keys[] =
71 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
73 /* socket configuration */
74 static socket_entry_t *sockets = NULL;
75 static size_t sockets_num;
77 static struct pollfd *pollfd = NULL;
78 static size_t pollfd_num;
80 static int listen_thread_loop = 0;
81 static int listen_thread_running = 0;
82 static pthread_t listen_thread;
84 static int unix_sock_perms = S_IRWXU | S_IRWXG;
85 static char *unix_sock_group = NULL;
87 static char **unix_sock_paths = NULL;
88 static size_t unix_sock_paths_num = 0;
93 static int nc_register_fd (int fd, const char *path) /* {{{ */
101 tmp = realloc (unix_sock_paths,
102 (unix_sock_paths_num + 1) * sizeof (*unix_sock_paths));
105 ERROR ("netcmd plugin: realloc failed.");
108 unix_sock_paths = tmp;
110 unix_sock_paths[unix_sock_paths_num] = sstrdup (path);
111 unix_sock_paths_num++;
114 tmp = realloc (pollfd, (pollfd_num + 1) * sizeof (*pollfd));
117 ERROR ("netcmd plugin: realloc failed.");
120 unix_sock_paths_num--;
121 sfree (unix_sock_paths[unix_sock_paths_num]);
128 memset (&pollfd[pollfd_num], 0, sizeof (pollfd[pollfd_num]));
129 pollfd[pollfd_num].fd = fd;
130 pollfd[pollfd_num].events = POLLIN | POLLPRI;
131 pollfd[pollfd_num].revents = 0;
136 } /* }}} int nc_register_fd */
138 static int nc_open_unix_socket (const char *path, /* {{{ */
141 struct sockaddr_un sa;
149 DEBUG ("netcmd plugin: nc_open_unix_socket (path = %s, group = %s);",
150 (path != NULL) ? path : "(null)",
151 (group != NULL) ? group : "(null)");
152 if (strncasecmp ("unix:", path, strlen ("unix:")) == 0)
153 path += strlen ("unix:");
155 fd = socket (PF_UNIX, SOCK_STREAM, 0);
158 ERROR ("netcmd plugin: socket(2) failed: %s",
159 sstrerror (errno, errbuf, sizeof (errbuf)));
163 memset (&sa, '\0', sizeof (sa));
164 sa.sun_family = AF_UNIX;
165 sstrncpy (sa.sun_path, path, sizeof (sa.sun_path));
166 /* unlink (sa.sun_path); */
168 DEBUG ("netcmd plugin: socket path = %s", sa.sun_path);
170 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
173 ERROR ("netcmd plugin: bind failed: %s",
174 sstrerror (errno, errbuf, sizeof (errbuf)));
180 /* FIXME: Copy unix_sock_perms stuff from unixsock. */
181 chmod (sa.sun_path, unix_sock_perms);
183 status = listen (fd, 8);
186 ERROR ("netcmd plugin: listen failed: %s",
187 sstrerror (errno, errbuf, sizeof (errbuf)));
193 /* If `group' is not NULL, `chown' the file. */
194 while (group != NULL) /* {{{ */
201 status = getgrnam_r (group, &sg, grbuf, sizeof (grbuf), &g);
204 WARNING ("netcmd plugin: getgrnam_r (%s) failed: %s", group,
205 sstrerror (errno, errbuf, sizeof (errbuf)));
211 WARNING ("netcmd plugin: No such group: `%s'", group);
215 status = chown (sa.sun_path, (uid_t) -1, g->gr_gid);
218 WARNING ("netcmd plugin: chown (%s, -1, %i) failed: %s",
219 sa.sun_path, (int) g->gr_gid,
220 sstrerror (errno, errbuf, sizeof (errbuf)));
224 } /* }}} while (group != NULL) */
226 status = nc_register_fd (fd, sa.sun_path);
230 unlink (sa.sun_path);
235 } /* }}} int nc_open_unix_socket */
237 static int nc_open_network_socket (const char *node, /* {{{ */
240 struct addrinfo ai_hints;
241 struct addrinfo *ai_list;
242 struct addrinfo *ai_ptr;
245 DEBUG ("netcmd plugin: nc_open_network_socket (node = %s, service = %s);",
246 (node != NULL) ? node : "(null)",
247 (service != NULL) ? service : "(null)");
249 memset (&ai_hints, 0, sizeof (ai_hints));
251 ai_hints.ai_flags |= AI_PASSIVE;
254 ai_hints.ai_flags |= AI_ADDRCONFIG;
256 ai_hints.ai_family = AF_UNSPEC;
257 ai_hints.ai_socktype = SOCK_STREAM;
262 service = NC_DEFAULT_PORT;
264 status = getaddrinfo (node, service, &ai_hints, &ai_list);
267 ERROR ("netcmd plugin: getaddrinfo failed: %s",
268 gai_strerror (status));
272 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
277 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
278 ai_ptr->ai_protocol);
281 ERROR ("netcmd plugin: socket(2) failed: %s",
282 sstrerror (errno, errbuf, sizeof (errbuf)));
286 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
290 ERROR ("netcmd plugin: bind(2) failed: %s",
291 sstrerror (errno, errbuf, sizeof (errbuf)));
295 status = listen (fd, /* backlog = */ 8);
299 ERROR ("netcmd plugin: listen(2) failed: %s",
300 sstrerror (errno, errbuf, sizeof (errbuf)));
304 status = nc_register_fd (fd, /* path = */ NULL);
310 } /* for (ai_next) */
312 freeaddrinfo (ai_list);
315 } /* }}} int nc_open_network_socket */
317 static int nc_open_socket (const char *node, /* {{{ */
321 && (strncasecmp ("unix:", node, strlen ("unix:")) == 0))
323 return (nc_open_unix_socket (node, service));
327 return (nc_open_network_socket (node, service));
329 } /* }}} int nc_open_socket */
331 static void *nc_handle_client (void *arg) /* {{{ */
340 DEBUG ("netcmd plugin: nc_handle_client: Reading from fd #%i", fd);
342 fhin = fdopen (fd, "r");
345 ERROR ("netcmd plugin: fdopen failed: %s",
346 sstrerror (errno, errbuf, sizeof (errbuf)));
348 pthread_exit ((void *) 1);
351 fhout = fdopen (fd, "w");
354 ERROR ("netcmd plugin: fdopen failed: %s",
355 sstrerror (errno, errbuf, sizeof (errbuf)));
356 fclose (fhin); /* this closes fd as well */
357 pthread_exit ((void *) 1);
360 /* change output buffer to line buffered mode */
361 if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
363 ERROR ("netcmd plugin: setvbuf failed: %s",
364 sstrerror (errno, errbuf, sizeof (errbuf)));
367 pthread_exit ((void *) 1);
373 char buffer_copy[1024];
379 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
383 WARNING ("netcmd plugin: failed to read from socket #%i: %s",
385 sstrerror (errno, errbuf, sizeof (errbuf)));
390 len = strlen (buffer);
392 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
393 buffer[--len] = '\0';
398 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
400 fields_num = strsplit (buffer_copy, fields,
401 sizeof (fields) / sizeof (fields[0]));
409 if (strcasecmp (fields[0], "getval") == 0)
411 handle_getval (fhout, buffer);
413 else if (strcasecmp (fields[0], "putval") == 0)
415 handle_putval (fhout, buffer);
417 else if (strcasecmp (fields[0], "listval") == 0)
419 handle_listval (fhout, buffer);
421 else if (strcasecmp (fields[0], "putnotif") == 0)
423 handle_putnotif (fhout, buffer);
425 else if (strcasecmp (fields[0], "flush") == 0)
427 handle_flush (fhout, buffer);
431 if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
433 WARNING ("netcmd plugin: failed to write to socket #%i: %s",
435 sstrerror (errno, errbuf, sizeof (errbuf)));
439 } /* while (fgets) */
441 DEBUG ("netcmd plugin: nc_handle_client: Exiting..");
445 pthread_exit ((void *) 0);
447 } /* }}} void *nc_handle_client */
449 static void *nc_server_thread (void __attribute__((unused)) *arg) /* {{{ */
453 pthread_attr_t th_attr;
457 for (i = 0; i < sockets_num; i++)
458 nc_open_socket (sockets[i].node, sockets[i].service);
460 if (sockets_num == 0)
461 nc_open_socket (NULL, NULL);
465 ERROR ("netcmd plugin: No sockets could be opened.");
466 pthread_exit ((void *) -1);
469 while (listen_thread_loop != 0)
471 status = poll (pollfd, (nfds_t) pollfd_num, /* timeout = */ -1);
474 if ((errno == EINTR) || (errno == EAGAIN))
477 ERROR ("netcmd plugin: poll(2) failed: %s",
478 sstrerror (errno, errbuf, sizeof (errbuf)));
479 listen_thread_loop = 0;
483 for (i = 0; i < pollfd_num; i++)
487 if (pollfd[i].revents == 0)
491 else if ((pollfd[i].revents & (POLLERR | POLLHUP | POLLNVAL))
494 WARNING ("netcmd plugin: File descriptor %i failed.",
496 close (pollfd[i].fd);
498 pollfd[i].events = 0;
499 pollfd[i].revents = 0;
502 pollfd[i].revents = 0;
504 status = accept (pollfd[i].fd,
505 /* sockaddr = */ NULL,
506 /* sockaddr_len = */ NULL);
512 ERROR ("netcmd plugin: accept failed: %s",
513 sstrerror (errno, errbuf, sizeof (errbuf)));
517 client_fd = malloc (sizeof (*client_fd));
518 if (client_fd == NULL)
520 ERROR ("netcmd plugin: malloc failed.");
526 DEBUG ("Spawning child to handle connection on fd %i", *client_fd);
528 pthread_attr_init (&th_attr);
529 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
531 status = pthread_create (&th, &th_attr, nc_handle_client,
535 WARNING ("netcmd plugin: pthread_create failed: %s",
536 sstrerror (errno, errbuf, sizeof (errbuf)));
541 } /* while (listen_thread_loop) */
543 for (i = 0; i < pollfd_num; i++)
545 if (pollfd[i].fd < 0)
548 close (pollfd[i].fd);
550 pollfd[i].events = 0;
551 pollfd[i].revents = 0;
557 for (i = 0; i < unix_sock_paths_num; i++)
559 DEBUG ("netcmd plugin: Unlinking `%s'.",
561 unlink (unix_sock_paths[i]);
562 sfree (unix_sock_paths[i]);
564 sfree (unix_sock_paths);
567 } /* }}} void *nc_server_thread */
569 static int nc_config (const char *key, const char *val)
571 if (strcasecmp ("Listen", key) == 0)
575 tmp = realloc (sockets, (sockets_num + 1) * sizeof (*sockets));
578 ERROR ("netcmd plugin: realloc failed.");
582 tmp = sockets + sockets_num;
584 memset (tmp, 0, sizeof (*tmp));
585 tmp->node = sstrdup (val);
586 tmp->service = strchr (tmp->node, ' ');
587 if (tmp->service != NULL)
589 while ((tmp->service[0] == ' ') || (tmp->service[0] == '\t'))
594 if (tmp->service[0] == 0)
601 else if (strcasecmp (key, "SocketGroup") == 0)
603 char *new_sock_group = strdup (val);
604 if (new_sock_group == NULL)
607 sfree (unix_sock_group);
608 unix_sock_group = new_sock_group;
610 else if (strcasecmp (key, "SocketPerms") == 0)
615 tmp = (int) strtol (val, NULL, 8);
616 if ((errno != 0) || (tmp == 0))
618 unix_sock_perms = tmp;
626 } /* int nc_config */
628 static int nc_init (void)
630 static int have_init = 0;
634 /* Initialize only once. */
639 listen_thread_loop = 1;
641 status = pthread_create (&listen_thread, NULL, nc_server_thread, NULL);
645 listen_thread_loop = 0;
646 listen_thread_running = 0;
647 ERROR ("netcmd plugin: pthread_create failed: %s",
648 sstrerror (errno, errbuf, sizeof (errbuf)));
652 listen_thread_running = 1;
656 static int nc_shutdown (void)
660 listen_thread_loop = 0;
662 if (listen_thread != (pthread_t) 0)
664 pthread_kill (listen_thread, SIGTERM);
665 pthread_join (listen_thread, &ret);
666 listen_thread = (pthread_t) 0;
669 plugin_unregister_init ("netcmd");
670 plugin_unregister_shutdown ("netcmd");
673 } /* int nc_shutdown */
675 void module_register (void)
677 plugin_register_config ("netcmd", nc_config,
678 config_keys, config_keys_num);
679 plugin_register_init ("netcmd", nc_init);
680 plugin_register_shutdown ("netcmd", nc_shutdown);
681 } /* void module_register (void) */
683 /* vim: set sw=4 ts=4 sts=4 tw=78 fdm=marker : */