2 * collectd - src/rrdd.c
3 * Copyright (C) 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>
27 # define RRDD_LOG(severity, ...) do { fprintf (stderr, __VA_ARGS__); fprintf (stderr, "\n"); } while (0)
29 # define RRDD_LOG(severity, ...) syslog ((severity), __VA_ARGS__)
35 struct listen_socket_s
38 char path[PATH_MAX + 1];
40 typedef struct listen_socket_s listen_socket_t;
43 typedef struct cache_item_s cache_item_t;
49 time_t last_flush_time;
50 #define CI_FLAGS_IN_TREE 0x01
51 #define CI_FLAGS_IN_QUEUE 0x02
60 static listen_socket_t *listen_fds = NULL;
61 static size_t listen_fds_num = 0;
63 static int do_shutdown = 0;
65 static pthread_t queue_thread;
67 static pthread_t *connetion_threads = NULL;
68 static pthread_mutex_t connetion_threads_lock = PTHREAD_MUTEX_INITIALIZER;
69 static int connetion_threads_num = 0;
72 static avl_tree_t *cache_tree = NULL;
73 static cache_item_t *cache_queue_head = NULL;
74 static cache_item_t *cache_queue_tail = NULL;
75 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
76 static pthread_cond_t cache_cond = PTHREAD_COND_INITIALIZER;
81 static void sig_int_handler (int signal) /* {{{ */
84 } /* }}} void sig_int_handler */
86 static void sig_term_handler (int signal) /* {{{ */
89 } /* }}} void sig_term_handler */
91 static int cache_tree_compare (const void *v0, const void *v1) /* {{{ */
93 cache_item_t *c0 = (cache_item_t *) v0;
94 cache_item_t *c1 = (cache_item_t *) v1;
96 assert (c0->file != NULL);
97 assert (c1->file != NULL);
99 return (strcmp (c0->file, c1->file));
100 } /* }}} int cache_tree_compare */
102 static void cache_tree_free (void *v) /* {{{ */
104 cache_item_t *c = (cache_item_t *) v;
106 assert (c->values_num == 0);
107 assert ((c->flags & CI_FLAGS_IN_TREE) != 0);
108 assert ((c->flags & CI_FLAGS_IN_QUEUE) == 0);
113 } /* }}} void cache_tree_free */
115 static void *queue_thread_main (void *args) /* {{{ */
117 pthread_mutex_lock (&cache_lock);
118 while ((do_shutdown == 0) || (cache_queue_head != NULL))
127 if (cache_queue_head == NULL)
128 pthread_cond_wait (&cache_cond, &cache_lock);
130 if (cache_queue_head == NULL)
133 ci = cache_queue_head;
135 /* copy the relevant parts */
136 file = strdup (ci->file);
139 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
144 values_num = ci->values_num;
149 ci->last_flush_time = time (NULL);
150 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
152 cache_queue_head = ci->next;
153 if (cache_queue_head == NULL)
154 cache_queue_tail = NULL;
157 pthread_mutex_unlock (&cache_lock);
159 RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
160 file, values_num, (void *) values);
163 for (i = 0; i < values_num; i++)
166 pthread_mutex_lock (&cache_lock);
167 } /* while (do_shutdown == 0) */
168 pthread_mutex_unlock (&cache_lock);
170 RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
173 } /* }}} void *queue_thread_main */
175 static int handle_request_update (int fd, /* {{{ */
176 char *buffer, int buffer_size)
186 cache_item_t ci_temp;
193 RRDD_LOG (LOG_DEBUG, "handle_request_update (%i, %p, %i)",
194 fd, (void *) buffer, buffer_size);
199 buffer_ptr += strlen (file) + 1;
203 pthread_mutex_lock (&cache_lock);
205 node = avl_search (cache_tree, (void *) &ci_temp);
208 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
211 pthread_mutex_unlock (&cache_lock);
212 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
215 memset (ci, 0, sizeof (cache_item_t));
217 ci->file = strdup (file);
218 if (ci->file == NULL)
220 pthread_mutex_unlock (&cache_lock);
221 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
228 ci->last_flush_time = now;
229 ci->flags = CI_FLAGS_IN_TREE;
231 if (avl_insert (cache_tree, (void *) ci) == NULL)
233 pthread_mutex_unlock (&cache_lock);
234 RRDD_LOG (LOG_ERR, "handle_request_update: avl_insert failed.");
240 RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new AVL node %s.",
243 else /* if (ci != NULL) */
245 ci = (cache_item_t *) node->item;
249 while (*buffer_ptr != 0)
254 buffer_ptr += strlen (value) + 1;
256 temp = (char **) realloc (ci->values,
257 sizeof (char *) * (ci->values_num + 1));
260 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
265 ci->values[ci->values_num] = strdup (value);
266 if (ci->values[ci->values_num] == NULL)
268 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
276 /* FIXME: Timeout should not be hard-coded. */
277 if (((now - ci->last_flush_time) > 300)
278 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
280 RRDD_LOG (LOG_DEBUG, "handle_request_update: Adding %s to the update queue.",
283 assert (ci->next == NULL);
285 if (cache_queue_tail == NULL)
286 cache_queue_head = ci;
288 cache_queue_tail->next = ci;
289 cache_queue_tail = ci;
291 pthread_cond_signal (&cache_cond);
294 pthread_mutex_unlock (&cache_lock);
296 snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
297 answer[sizeof (answer) - 1] = 0;
299 write (fd, answer, sizeof (answer));
302 } /* }}} int handle_request_update */
304 static int handle_request (int fd) /* {{{ */
309 RRDD_LOG (LOG_DEBUG, "handle_request (%i)", fd);
311 buffer_size = read (fd, buffer, sizeof (buffer));
314 RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
317 assert (((size_t) buffer_size) <= sizeof (buffer));
319 if ((buffer[buffer_size - 2] != 0)
320 || (buffer[buffer_size - 1] != 0))
322 RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
326 /* fields in the buffer a separated by null bytes. */
327 if (strcmp (buffer, "update") == 0)
329 int offset = strlen ("update") + 1;
330 return (handle_request_update (fd, buffer + offset,
331 buffer_size - offset));
335 RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
338 } /* }}} int handle_request */
340 static void *connection_thread_main (void *args) /* {{{ */
346 fd = *((int *) args);
348 while (do_shutdown == 0)
350 struct pollfd pollfd;
354 pollfd.events = POLLIN | POLLPRI;
357 status = poll (&pollfd, 1, /* timeout = */ 500);
358 if (status == 0) /* timeout */
360 else if (status < 0) /* error */
365 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
369 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
374 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
376 RRDD_LOG (LOG_WARNING, "connection_thread_main: poll(2) returned "
377 "something unexpected.");
382 status = handle_request (fd);
390 self = pthread_self ();
391 /* Remove this thread from the connection threads list */
392 pthread_mutex_lock (&connetion_threads_lock);
393 /* Find out own index in the array */
394 for (i = 0; i < connetion_threads_num; i++)
395 if (pthread_equal (connetion_threads[i], self) != 0)
397 assert (i < connetion_threads_num);
399 /* Move the trailing threads forward. */
400 if (i < (connetion_threads_num - 1))
402 memmove (connetion_threads + i,
403 connetion_threads + i + 1,
404 sizeof (pthread_t) * (connetion_threads_num - i - 1));
407 connetion_threads_num--;
408 pthread_mutex_unlock (&connetion_threads_lock);
412 } /* }}} void *connection_thread_main */
414 static int open_listen_socket (const char *path) /* {{{ */
417 struct sockaddr_un sa;
418 listen_socket_t *temp;
421 temp = (listen_socket_t *) realloc (listen_fds,
422 sizeof (listen_fds[0]) * (listen_fds_num + 1));
425 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
429 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
431 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
434 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
438 memset (&sa, 0, sizeof (sa));
439 sa.sun_family = AF_UNIX;
440 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
442 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
445 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
451 status = listen (fd, /* backlog = */ 10);
454 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
460 listen_fds[listen_fds_num].fd = fd;
461 strncpy (listen_fds[listen_fds_num].path, path,
462 sizeof (listen_fds[listen_fds_num].path) - 1);
466 } /* }}} int open_listen_socket */
468 static int close_listen_sockets (void) /* {{{ */
472 for (i = 0; i < listen_fds_num; i++)
474 close (listen_fds[i].fd);
475 unlink (listen_fds[i].path);
483 } /* }}} int close_listen_sockets */
485 static void *listen_thread_main (void *args) /* {{{ */
491 status = open_listen_socket (RRDD_SOCK_PATH);
494 RRDD_LOG (LOG_ERR, "listen_thread_main: open_listen_socket failed.");
498 while (do_shutdown == 0)
501 struct sockaddr_un client_sa;
502 socklen_t client_sa_size;
505 client_sd = (int *) malloc (sizeof (int));
506 if (client_sd == NULL)
508 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
513 client_sa_size = sizeof (client_sa);
514 /* FIXME: Don't implement listen_fds as a list or use poll(2) here! */
515 *client_sd = accept (listen_fds[0].fd,
516 (struct sockaddr *) &client_sa, &client_sa_size);
519 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
523 RRDD_LOG (LOG_DEBUG, "listen_thread_main: accept(2) returned fd #%i.",
526 status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
527 /* args = */ (void *) client_sd);
530 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
536 /* FIXME: bug: if the new thread is run first, it may run into the
537 * assert (i < connetion_threads_num);
539 pthread_mutex_lock (&connetion_threads_lock);
543 temp = (pthread_t *) realloc (connetion_threads,
544 sizeof (pthread_t) * (connetion_threads_num + 1));
547 RRDD_LOG (LOG_ERR, "listen_thread_main: realloc failed.");
551 connetion_threads = temp;
552 connetion_threads[connetion_threads_num] = tid;
553 connetion_threads_num++;
556 pthread_mutex_unlock (&connetion_threads_lock);
557 } /* while (do_shutdown == 0) */
559 close_listen_sockets ();
561 pthread_mutex_lock (&connetion_threads_lock);
562 while (connetion_threads_num > 0)
566 wait_for = connetion_threads[0];
568 pthread_mutex_unlock (&connetion_threads_lock);
569 pthread_join (wait_for, /* retval = */ NULL);
570 pthread_mutex_lock (&connetion_threads_lock);
572 pthread_mutex_unlock (&connetion_threads_lock);
574 RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
577 } /* }}} void *listen_thread_main */
579 static int daemonize (void) /* {{{ */
588 fprintf (stderr, "daemonize: fork(2) failed.\n");
596 /* Change into the /tmp directory. */
599 /* Become session leader */
602 /* Open the first three file descriptors to /dev/null */
607 open ("/dev/null", O_RDWR);
610 #endif /* RRDD_DEBUG */
615 memset (&sa, 0, sizeof (sa));
616 sa.sa_handler = sig_int_handler;
617 sigaction (SIGINT, &sa, NULL);
619 memset (&sa, 0, sizeof (sa));
620 sa.sa_handler = sig_term_handler;
621 sigaction (SIGINT, &sa, NULL);
624 openlog ("rrdd", LOG_PID, LOG_DAEMON);
626 cache_tree = avl_alloc_tree (cache_tree_compare, cache_tree_free);
627 if (cache_tree == NULL)
629 RRDD_LOG (LOG_ERR, "daemonize: avl_alloc_tree failed.");
633 memset (&queue_thread, 0, sizeof (queue_thread));
634 status = pthread_create (&queue_thread, /* attr = */ NULL,
635 queue_thread_main, /* args = */ NULL);
638 RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
643 } /* }}} int daemonize */
645 static int cleanup (void) /* {{{ */
647 RRDD_LOG (LOG_DEBUG, "cleanup ()");
651 pthread_join (queue_thread, /* return = */ NULL);
656 } /* }}} int cleanup */
658 int main (int argc, char **argv)
662 printf ("%s by Florian Forster, Version %s\n",
663 PACKAGE_NAME, PACKAGE_VERSION);
665 status = daemonize ();
668 struct sigaction sigchld;
670 memset (&sigchld, 0, sizeof (sigchld));
671 sigchld.sa_handler = SIG_IGN;
672 sigaction (SIGCHLD, &sigchld, NULL);
676 else if (status != 0)
678 fprintf (stderr, "daemonize failed, exiting.\n");
682 listen_thread_main (NULL);
690 * vim: set sw=2 sts=2 ts=8 et fdm=marker :