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))
128 if (cache_queue_head == NULL)
129 pthread_cond_wait (&cache_cond, &cache_lock);
131 if (cache_queue_head == NULL)
134 ci = cache_queue_head;
136 /* copy the relevant parts */
137 file = strdup (ci->file);
140 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
145 values_num = ci->values_num;
150 ci->last_flush_time = time (NULL);
151 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
153 cache_queue_head = ci->next;
154 if (cache_queue_head == NULL)
155 cache_queue_tail = NULL;
158 pthread_mutex_unlock (&cache_lock);
160 RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
161 file, values_num, (void *) values);
163 status = rrd_update_r (file, NULL, values_num, values);
166 RRDD_LOG (LOG_ERR, "queue_thread_main: "
167 "rrd_update_r failed with status %i.",
172 for (i = 0; i < values_num; i++)
175 pthread_mutex_lock (&cache_lock);
176 } /* while (do_shutdown == 0) */
177 pthread_mutex_unlock (&cache_lock);
179 RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
182 } /* }}} void *queue_thread_main */
184 static int handle_request_update (int fd, /* {{{ */
185 char *buffer, int buffer_size)
195 cache_item_t ci_temp;
202 RRDD_LOG (LOG_DEBUG, "handle_request_update (%i, %p, %i)",
203 fd, (void *) buffer, buffer_size);
208 buffer_ptr += strlen (file) + 1;
212 pthread_mutex_lock (&cache_lock);
214 node = avl_search (cache_tree, (void *) &ci_temp);
217 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
220 pthread_mutex_unlock (&cache_lock);
221 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
224 memset (ci, 0, sizeof (cache_item_t));
226 ci->file = strdup (file);
227 if (ci->file == NULL)
229 pthread_mutex_unlock (&cache_lock);
230 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
237 ci->last_flush_time = now;
238 ci->flags = CI_FLAGS_IN_TREE;
240 if (avl_insert (cache_tree, (void *) ci) == NULL)
242 pthread_mutex_unlock (&cache_lock);
243 RRDD_LOG (LOG_ERR, "handle_request_update: avl_insert failed.");
249 RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new AVL node %s.",
252 else /* if (ci != NULL) */
254 ci = (cache_item_t *) node->item;
258 while (*buffer_ptr != 0)
263 buffer_ptr += strlen (value) + 1;
265 temp = (char **) realloc (ci->values,
266 sizeof (char *) * (ci->values_num + 1));
269 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
274 ci->values[ci->values_num] = strdup (value);
275 if (ci->values[ci->values_num] == NULL)
277 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
285 /* FIXME: Timeout should not be hard-coded. */
286 if (((now - ci->last_flush_time) > 300)
287 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
289 RRDD_LOG (LOG_DEBUG, "handle_request_update: Adding %s to the update queue.",
292 assert (ci->next == NULL);
294 if (cache_queue_tail == NULL)
295 cache_queue_head = ci;
297 cache_queue_tail->next = ci;
298 cache_queue_tail = ci;
300 pthread_cond_signal (&cache_cond);
303 pthread_mutex_unlock (&cache_lock);
305 snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
306 answer[sizeof (answer) - 1] = 0;
308 write (fd, answer, sizeof (answer));
311 } /* }}} int handle_request_update */
313 static int handle_request (int fd) /* {{{ */
318 RRDD_LOG (LOG_DEBUG, "handle_request (%i)", fd);
320 buffer_size = read (fd, buffer, sizeof (buffer));
323 RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
326 assert (((size_t) buffer_size) <= sizeof (buffer));
328 if ((buffer[buffer_size - 2] != 0)
329 || (buffer[buffer_size - 1] != 0))
331 RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
335 /* fields in the buffer a separated by null bytes. */
336 if (strcmp (buffer, "update") == 0)
338 int offset = strlen ("update") + 1;
339 return (handle_request_update (fd, buffer + offset,
340 buffer_size - offset));
344 RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
347 } /* }}} int handle_request */
349 static void *connection_thread_main (void *args) /* {{{ */
355 fd = *((int *) args);
357 RRDD_LOG (LOG_DEBUG, "connection_thread_main: Adding myself to "
358 "connetion_threads[]..");
359 pthread_mutex_lock (&connetion_threads_lock);
363 temp = (pthread_t *) realloc (connetion_threads,
364 sizeof (pthread_t) * (connetion_threads_num + 1));
367 RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
371 connetion_threads = temp;
372 connetion_threads[connetion_threads_num] = pthread_self ();
373 connetion_threads_num++;
376 pthread_mutex_unlock (&connetion_threads_lock);
377 RRDD_LOG (LOG_DEBUG, "connection_thread_main: done");
379 while (do_shutdown == 0)
381 struct pollfd pollfd;
385 pollfd.events = POLLIN | POLLPRI;
388 status = poll (&pollfd, 1, /* timeout = */ 500);
389 if (status == 0) /* timeout */
391 else if (status < 0) /* error */
396 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
400 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
405 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
407 RRDD_LOG (LOG_WARNING, "connection_thread_main: poll(2) returned "
408 "something unexpected.");
413 status = handle_request (fd);
421 self = pthread_self ();
422 /* Remove this thread from the connection threads list */
423 pthread_mutex_lock (&connetion_threads_lock);
424 /* Find out own index in the array */
425 for (i = 0; i < connetion_threads_num; i++)
426 if (pthread_equal (connetion_threads[i], self) != 0)
428 assert (i < connetion_threads_num);
430 /* Move the trailing threads forward. */
431 if (i < (connetion_threads_num - 1))
433 memmove (connetion_threads + i,
434 connetion_threads + i + 1,
435 sizeof (pthread_t) * (connetion_threads_num - i - 1));
438 connetion_threads_num--;
439 pthread_mutex_unlock (&connetion_threads_lock);
443 } /* }}} void *connection_thread_main */
445 static int open_listen_socket (const char *path) /* {{{ */
448 struct sockaddr_un sa;
449 listen_socket_t *temp;
452 temp = (listen_socket_t *) realloc (listen_fds,
453 sizeof (listen_fds[0]) * (listen_fds_num + 1));
456 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
460 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
462 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
465 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
469 memset (&sa, 0, sizeof (sa));
470 sa.sun_family = AF_UNIX;
471 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
473 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
476 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
482 status = listen (fd, /* backlog = */ 10);
485 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
491 listen_fds[listen_fds_num].fd = fd;
492 strncpy (listen_fds[listen_fds_num].path, path,
493 sizeof (listen_fds[listen_fds_num].path) - 1);
497 } /* }}} int open_listen_socket */
499 static int close_listen_sockets (void) /* {{{ */
503 for (i = 0; i < listen_fds_num; i++)
505 close (listen_fds[i].fd);
506 unlink (listen_fds[i].path);
514 } /* }}} int close_listen_sockets */
516 static void *listen_thread_main (void *args) /* {{{ */
522 status = open_listen_socket (RRDD_SOCK_PATH);
525 RRDD_LOG (LOG_ERR, "listen_thread_main: open_listen_socket failed.");
529 while (do_shutdown == 0)
532 struct sockaddr_un client_sa;
533 socklen_t client_sa_size;
536 client_sd = (int *) malloc (sizeof (int));
537 if (client_sd == NULL)
539 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
544 client_sa_size = sizeof (client_sa);
545 /* FIXME: Don't implement listen_fds as a list or use poll(2) here! */
546 *client_sd = accept (listen_fds[0].fd,
547 (struct sockaddr *) &client_sa, &client_sa_size);
550 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
554 RRDD_LOG (LOG_DEBUG, "listen_thread_main: accept(2) returned fd #%i.",
557 status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
558 /* args = */ (void *) client_sd);
561 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
567 RRDD_LOG (LOG_DEBUG, "listen_thread_main: pthread_create succeeded: "
569 *((unsigned long *) &tid));
570 } /* while (do_shutdown == 0) */
572 close_listen_sockets ();
574 pthread_mutex_lock (&connetion_threads_lock);
575 while (connetion_threads_num > 0)
579 wait_for = connetion_threads[0];
581 pthread_mutex_unlock (&connetion_threads_lock);
582 pthread_join (wait_for, /* retval = */ NULL);
583 pthread_mutex_lock (&connetion_threads_lock);
585 pthread_mutex_unlock (&connetion_threads_lock);
587 RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
590 } /* }}} void *listen_thread_main */
592 static int daemonize (void) /* {{{ */
601 fprintf (stderr, "daemonize: fork(2) failed.\n");
609 /* Change into the /tmp directory. */
612 /* Become session leader */
615 /* Open the first three file descriptors to /dev/null */
620 open ("/dev/null", O_RDWR);
623 #endif /* RRDD_DEBUG */
628 memset (&sa, 0, sizeof (sa));
629 sa.sa_handler = sig_int_handler;
630 sigaction (SIGINT, &sa, NULL);
632 memset (&sa, 0, sizeof (sa));
633 sa.sa_handler = sig_term_handler;
634 sigaction (SIGINT, &sa, NULL);
636 memset (&sa, 0, sizeof (sa));
637 sa.sa_handler = SIG_IGN;
638 sigaction (SIGPIPE, &sa, NULL);
641 openlog ("rrdd", LOG_PID, LOG_DAEMON);
643 cache_tree = avl_alloc_tree (cache_tree_compare, cache_tree_free);
644 if (cache_tree == NULL)
646 RRDD_LOG (LOG_ERR, "daemonize: avl_alloc_tree failed.");
650 memset (&queue_thread, 0, sizeof (queue_thread));
651 status = pthread_create (&queue_thread, /* attr = */ NULL,
652 queue_thread_main, /* args = */ NULL);
655 RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
660 } /* }}} int daemonize */
662 static int cleanup (void) /* {{{ */
664 RRDD_LOG (LOG_DEBUG, "cleanup ()");
668 RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
669 pthread_cond_signal (&cache_cond);
670 pthread_join (queue_thread, /* return = */ NULL);
671 RRDD_LOG (LOG_DEBUG, "cleanup: done");
676 } /* }}} int cleanup */
678 int main (int argc, char **argv)
682 printf ("%s by Florian Forster, Version %s\n",
683 PACKAGE_NAME, PACKAGE_VERSION);
685 status = daemonize ();
688 struct sigaction sigchld;
690 memset (&sigchld, 0, sizeof (sigchld));
691 sigchld.sa_handler = SIG_IGN;
692 sigaction (SIGCHLD, &sigchld, NULL);
696 else if (status != 0)
698 fprintf (stderr, "daemonize failed, exiting.\n");
702 listen_thread_main (NULL);
710 * vim: set sw=2 sts=2 ts=8 et fdm=marker :