2 * collectd - src/netcmd.c
3 * Copyright (C) 2007-2011 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_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 nc_peer_s nc_peer_t;
66 /* socket configuration */
67 static nc_peer_t *peers = NULL;
68 static size_t peers_num;
70 static struct pollfd *pollfd = NULL;
71 static size_t pollfd_num;
73 static int listen_thread_loop = 0;
74 static int listen_thread_running = 0;
75 static pthread_t listen_thread;
80 static int nc_register_fd (int fd, const char *path) /* {{{ */
84 tmp = realloc (pollfd, (pollfd_num + 1) * sizeof (*pollfd));
87 ERROR ("netcmd plugin: realloc failed.");
92 memset (&pollfd[pollfd_num], 0, sizeof (pollfd[pollfd_num]));
93 pollfd[pollfd_num].fd = fd;
94 pollfd[pollfd_num].events = POLLIN | POLLPRI;
95 pollfd[pollfd_num].revents = 0;
100 } /* }}} int nc_register_fd */
102 static int nc_open_socket (nc_peer_t *peer) /* {{{ */
104 struct addrinfo ai_hints;
105 struct addrinfo *ai_list;
106 struct addrinfo *ai_ptr;
109 const char *node = NULL;
110 const char *service = NULL;
115 service = peer->service;
119 service = NC_DEFAULT_PORT;
121 memset (&ai_hints, 0, sizeof (ai_hints));
123 ai_hints.ai_flags |= AI_PASSIVE;
126 ai_hints.ai_flags |= AI_ADDRCONFIG;
128 ai_hints.ai_family = AF_UNSPEC;
129 ai_hints.ai_socktype = SOCK_STREAM;
134 service = NC_DEFAULT_PORT;
136 status = getaddrinfo (node, service, &ai_hints, &ai_list);
139 ERROR ("netcmd plugin: getaddrinfo failed: %s",
140 gai_strerror (status));
144 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
149 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
150 ai_ptr->ai_protocol);
153 ERROR ("netcmd plugin: socket(2) failed: %s",
154 sstrerror (errno, errbuf, sizeof (errbuf)));
158 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
162 ERROR ("netcmd plugin: bind(2) failed: %s",
163 sstrerror (errno, errbuf, sizeof (errbuf)));
167 status = listen (fd, /* backlog = */ 8);
171 ERROR ("netcmd plugin: listen(2) failed: %s",
172 sstrerror (errno, errbuf, sizeof (errbuf)));
176 status = nc_register_fd (fd, /* path = */ NULL);
182 } /* for (ai_next) */
184 freeaddrinfo (ai_list);
187 } /* }}} int nc_open_socket */
189 static void *nc_handle_client (void *arg) /* {{{ */
198 DEBUG ("netcmd plugin: nc_handle_client: Reading from fd #%i", fd);
200 fhin = fdopen (fd, "r");
203 ERROR ("netcmd plugin: fdopen failed: %s",
204 sstrerror (errno, errbuf, sizeof (errbuf)));
206 pthread_exit ((void *) 1);
209 fhout = fdopen (fd, "w");
212 ERROR ("netcmd plugin: fdopen failed: %s",
213 sstrerror (errno, errbuf, sizeof (errbuf)));
214 fclose (fhin); /* this closes fd as well */
215 pthread_exit ((void *) 1);
218 /* change output buffer to line buffered mode */
219 if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
221 ERROR ("netcmd plugin: setvbuf failed: %s",
222 sstrerror (errno, errbuf, sizeof (errbuf)));
225 pthread_exit ((void *) 1);
231 char buffer_copy[1024];
237 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
241 WARNING ("netcmd plugin: failed to read from socket #%i: %s",
243 sstrerror (errno, errbuf, sizeof (errbuf)));
248 len = strlen (buffer);
250 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
251 buffer[--len] = '\0';
256 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
258 fields_num = strsplit (buffer_copy, fields,
259 sizeof (fields) / sizeof (fields[0]));
267 if (strcasecmp (fields[0], "getval") == 0)
269 handle_getval (fhout, buffer);
271 else if (strcasecmp (fields[0], "putval") == 0)
273 handle_putval (fhout, buffer);
275 else if (strcasecmp (fields[0], "listval") == 0)
277 handle_listval (fhout, buffer);
279 else if (strcasecmp (fields[0], "putnotif") == 0)
281 handle_putnotif (fhout, buffer);
283 else if (strcasecmp (fields[0], "flush") == 0)
285 handle_flush (fhout, buffer);
289 if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
291 WARNING ("netcmd plugin: failed to write to socket #%i: %s",
293 sstrerror (errno, errbuf, sizeof (errbuf)));
297 } /* while (fgets) */
299 DEBUG ("netcmd plugin: nc_handle_client: Exiting..");
303 pthread_exit ((void *) 0);
305 } /* }}} void *nc_handle_client */
307 static void *nc_server_thread (void __attribute__((unused)) *arg) /* {{{ */
311 pthread_attr_t th_attr;
315 for (i = 0; i < peers_num; i++)
316 nc_open_socket (peers + i);
319 nc_open_socket (NULL);
323 ERROR ("netcmd plugin: No sockets could be opened.");
324 pthread_exit ((void *) -1);
327 while (listen_thread_loop != 0)
329 status = poll (pollfd, (nfds_t) pollfd_num, /* timeout = */ -1);
332 if ((errno == EINTR) || (errno == EAGAIN))
335 ERROR ("netcmd plugin: poll(2) failed: %s",
336 sstrerror (errno, errbuf, sizeof (errbuf)));
337 listen_thread_loop = 0;
341 for (i = 0; i < pollfd_num; i++)
345 if (pollfd[i].revents == 0)
349 else if ((pollfd[i].revents & (POLLERR | POLLHUP | POLLNVAL))
352 WARNING ("netcmd plugin: File descriptor %i failed.",
354 close (pollfd[i].fd);
356 pollfd[i].events = 0;
357 pollfd[i].revents = 0;
360 pollfd[i].revents = 0;
362 status = accept (pollfd[i].fd,
363 /* sockaddr = */ NULL,
364 /* sockaddr_len = */ NULL);
370 ERROR ("netcmd plugin: accept failed: %s",
371 sstrerror (errno, errbuf, sizeof (errbuf)));
375 client_fd = malloc (sizeof (*client_fd));
376 if (client_fd == NULL)
378 ERROR ("netcmd plugin: malloc failed.");
384 DEBUG ("Spawning child to handle connection on fd %i", *client_fd);
386 pthread_attr_init (&th_attr);
387 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
389 status = pthread_create (&th, &th_attr, nc_handle_client,
393 WARNING ("netcmd plugin: pthread_create failed: %s",
394 sstrerror (errno, errbuf, sizeof (errbuf)));
399 } /* while (listen_thread_loop) */
401 for (i = 0; i < pollfd_num; i++)
403 if (pollfd[i].fd < 0)
406 close (pollfd[i].fd);
408 pollfd[i].events = 0;
409 pollfd[i].revents = 0;
416 } /* }}} void *nc_server_thread */
423 * TLSCertFile "/path/to/cert"
424 * TLSKeyFile "/path/to/key"
425 * TLSCAFile "/path/to/ca"
426 * TLSCRLFile "/path/to/crl"
431 static int nc_config_peer (const oconfig_item_t *ci)
436 p = realloc (peers, sizeof (*peers) * (peers_num + 1));
439 ERROR ("netcmd plugin: realloc failed.");
443 p = peers + peers_num;
444 memset (p, 0, sizeof (*p));
448 for (i = 0; i < ci->children_num; i++)
450 oconfig_item_t *child = ci->children + i;
452 if (strcasecmp ("Address", child->key) == 0)
453 cf_util_get_string (child, &p->node);
454 else if (strcasecmp ("Port", child->key) == 0)
455 cf_util_get_string (child, &p->service);
457 WARNING ("netcmd plugin: The option \"%s\" is not recognized within "
458 "a \"%s\" block.", child->key, ci->key);
462 } /* }}} int nc_config_peer */
464 static int nc_config (oconfig_item_t *ci)
468 for (i = 0; i < ci->children_num; i++)
470 oconfig_item_t *child = ci->children + i;
472 if (strcasecmp ("Listen", child->key) == 0)
473 nc_config_peer (child);
475 WARNING ("netcmd plugin: The option \"%s\" is not recognized.",
480 } /* int nc_config */
482 static int nc_init (void)
484 static int have_init = 0;
488 /* Initialize only once. */
493 listen_thread_loop = 1;
495 status = pthread_create (&listen_thread, NULL, nc_server_thread, NULL);
499 listen_thread_loop = 0;
500 listen_thread_running = 0;
501 ERROR ("netcmd plugin: pthread_create failed: %s",
502 sstrerror (errno, errbuf, sizeof (errbuf)));
506 listen_thread_running = 1;
510 static int nc_shutdown (void)
514 listen_thread_loop = 0;
516 if (listen_thread != (pthread_t) 0)
518 pthread_kill (listen_thread, SIGTERM);
519 pthread_join (listen_thread, &ret);
520 listen_thread = (pthread_t) 0;
523 plugin_unregister_init ("netcmd");
524 plugin_unregister_shutdown ("netcmd");
527 } /* int nc_shutdown */
529 void module_register (void)
531 plugin_register_complex_config ("netcmd", nc_config);
532 plugin_register_init ("netcmd", nc_init);
533 plugin_register_shutdown ("netcmd", nc_shutdown);
534 } /* void module_register (void) */
536 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */