2 * RRDTool - src/rrd_daemon.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>
23 * First tell the compiler to stick to the C99 and POSIX standards as close as
26 #ifndef __STRICT_ANSI__ /* {{{ */
27 # define __STRICT_ANSI__
30 #ifndef _ISOC99_SOURCE
31 # define _ISOC99_SOURCE
34 #ifdef _POSIX_C_SOURCE
35 # undef _POSIX_C_SOURCE
37 #define _POSIX_C_SOURCE 200112L
39 /* Single UNIX needed for strdup. */
43 #define _XOPEN_SOURCE 500
59 * Now for some includes..
61 #include "rrd.h" /* {{{ */
62 #include "rrd_client.h"
70 #include <sys/types.h>
74 #include <sys/socket.h>
85 #include <glib-2.0/glib.h>
88 #define RRDD_LOG(severity, ...) syslog ((severity), __VA_ARGS__)
91 # define __attribute__(x) /**/
97 struct listen_socket_s
100 char path[PATH_MAX + 1];
102 typedef struct listen_socket_s listen_socket_t;
105 typedef struct cache_item_s cache_item_t;
111 time_t last_flush_time;
112 #define CI_FLAGS_IN_TREE 0x01
113 #define CI_FLAGS_IN_QUEUE 0x02
122 static listen_socket_t *listen_fds = NULL;
123 static size_t listen_fds_num = 0;
125 static int do_shutdown = 0;
127 static pthread_t queue_thread;
129 static pthread_t *connetion_threads = NULL;
130 static pthread_mutex_t connetion_threads_lock = PTHREAD_MUTEX_INITIALIZER;
131 static int connetion_threads_num = 0;
134 static GTree *cache_tree = NULL;
135 static cache_item_t *cache_queue_head = NULL;
136 static cache_item_t *cache_queue_tail = NULL;
137 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
138 static pthread_cond_t cache_cond = PTHREAD_COND_INITIALIZER;
140 static int config_write_interval = 300;
141 static int config_flush_interval = 3600;
143 static char **config_listen_address_list = NULL;
144 static int config_listen_address_list_len = 0;
149 static void sig_int_handler (int s __attribute__((unused))) /* {{{ */
152 } /* }}} void sig_int_handler */
154 static void sig_term_handler (int s __attribute__((unused))) /* {{{ */
157 } /* }}} void sig_term_handler */
160 * enqueue_cache_item:
161 * `cache_lock' must be acquired before calling this function!
163 static int enqueue_cache_item (cache_item_t *ci) /* {{{ */
165 RRDD_LOG (LOG_DEBUG, "enqueue_cache_item: Adding %s to the update queue.",
171 if ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
174 assert (ci->next == NULL);
176 if (ci->values_num == 0)
179 if (cache_queue_tail == NULL)
180 cache_queue_head = ci;
182 cache_queue_tail->next = ci;
183 cache_queue_tail = ci;
185 ci->flags |= CI_FLAGS_IN_QUEUE;
187 ci->flags |= CI_FLAGS_IN_QUEUE;
190 } /* }}} int enqueue_cache_item */
193 * tree_callback_flush:
194 * Called via `g_tree_foreach' in `queue_thread_main'. `cache_lock' is held
195 * while this is in progress.
197 static gboolean tree_callback_flush (gpointer key /* {{{ */
198 __attribute__((unused)), gpointer value, gpointer data)
203 key = NULL; /* make compiler happy */
205 ci = (cache_item_t *) value;
206 now = *((time_t *) data);
208 if (((now - ci->last_flush_time) >= config_write_interval)
209 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
210 && (ci->values_num > 0))
211 enqueue_cache_item (ci);
214 } /* }}} gboolean tree_callback_flush */
216 static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
219 struct timespec next_flush;
221 gettimeofday (&now, NULL);
222 next_flush.tv_sec = now.tv_sec + config_flush_interval;
223 next_flush.tv_nsec = 1000 * now.tv_usec;
225 pthread_mutex_lock (&cache_lock);
226 while ((do_shutdown == 0) || (cache_queue_head != NULL))
235 /* First, check if it's time to do the cache flush. */
236 gettimeofday (&now, NULL);
237 if ((now.tv_sec > next_flush.tv_sec)
238 || ((now.tv_sec == next_flush.tv_sec)
239 && ((1000 * now.tv_usec) > next_flush.tv_nsec)))
243 /* Pass the current time as user data so that we don't need to call
244 * `time' for each node. */
245 time_now = time (NULL);
247 g_tree_foreach (cache_tree, tree_callback_flush, (gpointer) &time_now);
249 /* Determine the time of the next cache flush. */
250 while (next_flush.tv_sec < now.tv_sec)
251 next_flush.tv_sec += config_flush_interval;
254 /* Now, check if there's something to store away. If not, wait until
255 * something comes in or it's time to do the cache flush. */
256 if (cache_queue_head == NULL)
258 status = pthread_cond_timedwait (&cache_cond, &cache_lock, &next_flush);
259 if ((status != 0) && (status != ETIMEDOUT))
261 RRDD_LOG (LOG_ERR, "queue_thread_main: "
262 "pthread_cond_timedwait returned %i.", status);
266 /* Check if a value has arrived. This may be NULL if we timed out or there
267 * was an interrupt such as a signal. */
268 if (cache_queue_head == NULL)
271 ci = cache_queue_head;
273 /* copy the relevant parts */
274 file = strdup (ci->file);
277 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
282 values_num = ci->values_num;
287 ci->last_flush_time = time (NULL);
288 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
290 cache_queue_head = ci->next;
291 if (cache_queue_head == NULL)
292 cache_queue_tail = NULL;
295 pthread_mutex_unlock (&cache_lock);
297 RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
298 file, values_num, (void *) values);
300 status = rrd_update_r (file, NULL, values_num, (void *) values);
303 RRDD_LOG (LOG_ERR, "queue_thread_main: "
304 "rrd_update_r failed with status %i.",
309 for (i = 0; i < values_num; i++)
312 pthread_mutex_lock (&cache_lock);
313 } /* while (do_shutdown == 0) */
314 pthread_mutex_unlock (&cache_lock);
316 RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
319 } /* }}} void *queue_thread_main */
321 static int buffer_get_field (char **buffer_ret, /* {{{ */
322 size_t *buffer_size_ret, char **field_ret)
331 buffer = *buffer_ret;
333 buffer_size = *buffer_size_ret;
337 /* This is ensured by `handle_request'. */
338 assert (buffer[buffer_size - 1] == ' ');
341 while (buffer_pos < buffer_size)
343 /* Check for end-of-field or end-of-buffer */
344 if (buffer[buffer_pos] == ' ')
346 field[field_size] = 0;
352 /* Handle escaped characters. */
353 else if (buffer[buffer_pos] == '\\')
355 if (buffer_pos >= (buffer_size - 1))
358 field[field_size] = buffer[buffer_pos];
362 /* Normal operation */
365 field[field_size] = buffer[buffer_pos];
369 } /* while (buffer_pos < buffer_size) */
374 *buffer_ret = buffer + buffer_pos;
375 *buffer_size_ret = buffer_size - buffer_pos;
379 } /* }}} int buffer_get_field */
381 static int handle_request_update (int fd, /* {{{ */
382 char *buffer, size_t buffer_size)
395 status = buffer_get_field (&buffer, &buffer_size, &file);
398 RRDD_LOG (LOG_INFO, "handle_request_update: Cannot get file name.");
402 pthread_mutex_lock (&cache_lock);
404 ci = g_tree_lookup (cache_tree, file);
405 if (ci == NULL) /* {{{ */
407 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
410 pthread_mutex_unlock (&cache_lock);
411 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
414 memset (ci, 0, sizeof (cache_item_t));
416 ci->file = strdup (file);
417 if (ci->file == NULL)
419 pthread_mutex_unlock (&cache_lock);
420 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
427 ci->last_flush_time = now;
428 ci->flags = CI_FLAGS_IN_TREE;
430 g_tree_insert (cache_tree, (void *) ci->file, (void *) ci);
432 RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new tree node %s.",
437 while (buffer_size > 0)
442 status = buffer_get_field (&buffer, &buffer_size, &value);
445 RRDD_LOG (LOG_INFO, "handle_request_update: Error reading field.");
449 temp = (char **) realloc (ci->values,
450 sizeof (char *) * (ci->values_num + 1));
453 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
458 ci->values[ci->values_num] = strdup (value);
459 if (ci->values[ci->values_num] == NULL)
461 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
469 if (((now - ci->last_flush_time) >= config_write_interval)
470 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
471 && (ci->values_num > 0))
473 enqueue_cache_item (ci);
474 pthread_cond_signal (&cache_cond);
477 pthread_mutex_unlock (&cache_lock);
479 snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
480 answer[sizeof (answer) - 1] = 0;
482 status = write (fd, answer, strlen (answer));
486 RRDD_LOG (LOG_INFO, "handle_request_update: write(2) returned an error.");
491 } /* }}} int handle_request_update */
493 static int handle_request (int fd) /* {{{ */
501 status = read (fd, buffer, sizeof (buffer));
504 RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
507 buffer_size = status;
508 assert (((size_t) buffer_size) <= sizeof (buffer));
510 if (buffer[buffer_size - 1] != '\n')
512 RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
515 /* Place the normal field separator at the end to simplify
516 * `buffer_get_field's work. */
517 buffer[buffer_size - 1] = ' ';
521 status = buffer_get_field (&buffer_ptr, &buffer_size, &command);
524 RRDD_LOG (LOG_INFO, "handle_request: Unable parse command.");
528 if (strcmp (command, "update") == 0)
530 return (handle_request_update (fd, buffer_ptr, buffer_size));
534 RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
537 } /* }}} int handle_request */
539 static void *connection_thread_main (void *args /* {{{ */
540 __attribute__((unused)))
546 fd = *((int *) args);
548 pthread_mutex_lock (&connetion_threads_lock);
552 temp = (pthread_t *) realloc (connetion_threads,
553 sizeof (pthread_t) * (connetion_threads_num + 1));
556 RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
560 connetion_threads = temp;
561 connetion_threads[connetion_threads_num] = pthread_self ();
562 connetion_threads_num++;
565 pthread_mutex_unlock (&connetion_threads_lock);
567 while (do_shutdown == 0)
569 struct pollfd pollfd;
573 pollfd.events = POLLIN | POLLPRI;
576 status = poll (&pollfd, 1, /* timeout = */ 500);
577 if (status == 0) /* timeout */
579 else if (status < 0) /* error */
584 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
588 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
593 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
595 RRDD_LOG (LOG_WARNING, "connection_thread_main: "
596 "poll(2) returned something unexpected: %#04hx",
602 status = handle_request (fd);
610 self = pthread_self ();
611 /* Remove this thread from the connection threads list */
612 pthread_mutex_lock (&connetion_threads_lock);
613 /* Find out own index in the array */
614 for (i = 0; i < connetion_threads_num; i++)
615 if (pthread_equal (connetion_threads[i], self) != 0)
617 assert (i < connetion_threads_num);
619 /* Move the trailing threads forward. */
620 if (i < (connetion_threads_num - 1))
622 memmove (connetion_threads + i,
623 connetion_threads + i + 1,
624 sizeof (pthread_t) * (connetion_threads_num - i - 1));
627 connetion_threads_num--;
628 pthread_mutex_unlock (&connetion_threads_lock);
632 } /* }}} void *connection_thread_main */
634 static int open_listen_socket_unix (const char *path) /* {{{ */
637 struct sockaddr_un sa;
638 listen_socket_t *temp;
641 temp = (listen_socket_t *) realloc (listen_fds,
642 sizeof (listen_fds[0]) * (listen_fds_num + 1));
645 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: realloc failed.");
649 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
651 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
654 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: socket(2) failed.");
658 memset (&sa, 0, sizeof (sa));
659 sa.sun_family = AF_UNIX;
660 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
662 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
665 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: bind(2) failed.");
671 status = listen (fd, /* backlog = */ 10);
674 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: listen(2) failed.");
680 listen_fds[listen_fds_num].fd = fd;
681 snprintf (listen_fds[listen_fds_num].path,
682 sizeof (listen_fds[listen_fds_num].path) - 1,
687 } /* }}} int open_listen_socket_unix */
689 static int open_listen_socket (const char *addr) /* {{{ */
691 struct addrinfo ai_hints;
692 struct addrinfo *ai_res;
693 struct addrinfo *ai_ptr;
696 assert (addr != NULL);
698 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
699 return (open_listen_socket_unix (addr + strlen ("unix:")));
700 else if (addr[0] == '/')
701 return (open_listen_socket_unix (addr));
703 memset (&ai_hints, 0, sizeof (ai_hints));
704 ai_hints.ai_flags = 0;
706 ai_hints.ai_flags |= AI_ADDRCONFIG;
708 ai_hints.ai_family = AF_UNSPEC;
709 ai_hints.ai_socktype = SOCK_STREAM;
712 status = getaddrinfo (addr, DEFAULT_PORT, &ai_hints, &ai_res);
715 RRDD_LOG (LOG_ERR, "open_listen_socket: getaddrinfo(%s) failed: "
716 "%s", addr, gai_strerror (status));
720 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
723 listen_socket_t *temp;
725 temp = (listen_socket_t *) realloc (listen_fds,
726 sizeof (listen_fds[0]) * (listen_fds_num + 1));
729 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
733 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
735 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
738 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
742 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
745 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
750 status = listen (fd, /* backlog = */ 10);
753 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
758 listen_fds[listen_fds_num].fd = fd;
759 strncpy (listen_fds[listen_fds_num].path, addr,
760 sizeof (listen_fds[listen_fds_num].path) - 1);
765 } /* }}} int open_listen_socket */
767 static int close_listen_sockets (void) /* {{{ */
771 for (i = 0; i < listen_fds_num; i++)
773 close (listen_fds[i].fd);
774 if (strncmp ("unix:", listen_fds[i].path, strlen ("unix:")) == 0)
775 unlink (listen_fds[i].path + strlen ("unix:"));
783 } /* }}} int close_listen_sockets */
785 static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
787 struct pollfd *pollfds;
792 for (i = 0; i < config_listen_address_list_len; i++)
794 RRDD_LOG (LOG_DEBUG, "listen_thread_main: config_listen_address_list[%i] "
795 "= %s", i, config_listen_address_list[i]);
796 open_listen_socket (config_listen_address_list[i]);
799 if (config_listen_address_list_len < 1)
800 open_listen_socket (RRDD_SOCK_PATH);
802 if (listen_fds_num < 1)
804 RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
805 "could be opened. Sorry.");
809 pollfds_num = listen_fds_num;
810 pollfds = (struct pollfd *) malloc (sizeof (*pollfds) * pollfds_num);
813 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
816 memset (pollfds, 0, sizeof (*pollfds) * pollfds_num);
818 while (do_shutdown == 0)
820 assert (pollfds_num == ((int) listen_fds_num));
821 for (i = 0; i < pollfds_num; i++)
823 pollfds[i].fd = listen_fds[i].fd;
824 pollfds[i].events = POLLIN | POLLPRI;
825 pollfds[i].revents = 0;
828 status = poll (pollfds, pollfds_num, /* timeout = */ -1);
834 RRDD_LOG (LOG_ERR, "listen_thread_main: poll(2) failed.");
839 for (i = 0; i < pollfds_num; i++)
842 struct sockaddr_storage client_sa;
843 socklen_t client_sa_size;
846 if (pollfds[i].revents == 0)
849 if ((pollfds[i].revents & (POLLIN | POLLPRI)) == 0)
851 RRDD_LOG (LOG_ERR, "listen_thread_main: "
852 "poll(2) returned something unexpected for listen FD #%i.",
857 client_sd = (int *) malloc (sizeof (int));
858 if (client_sd == NULL)
860 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
864 client_sa_size = sizeof (client_sa);
865 *client_sd = accept (pollfds[i].fd,
866 (struct sockaddr *) &client_sa, &client_sa_size);
869 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
873 status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
874 /* args = */ (void *) client_sd);
877 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
882 } /* for (pollfds_num) */
883 } /* while (do_shutdown == 0) */
885 close_listen_sockets ();
887 pthread_mutex_lock (&connetion_threads_lock);
888 while (connetion_threads_num > 0)
892 wait_for = connetion_threads[0];
894 pthread_mutex_unlock (&connetion_threads_lock);
895 pthread_join (wait_for, /* retval = */ NULL);
896 pthread_mutex_lock (&connetion_threads_lock);
898 pthread_mutex_unlock (&connetion_threads_lock);
900 RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
903 } /* }}} void *listen_thread_main */
905 static int daemonize (void) /* {{{ */
913 fprintf (stderr, "daemonize: fork(2) failed.\n");
921 /* Change into the /tmp directory. */
924 /* Become session leader */
927 /* Open the first three file descriptors to /dev/null */
932 open ("/dev/null", O_RDWR);
939 memset (&sa, 0, sizeof (sa));
940 sa.sa_handler = sig_int_handler;
941 sigaction (SIGINT, &sa, NULL);
943 memset (&sa, 0, sizeof (sa));
944 sa.sa_handler = sig_term_handler;
945 sigaction (SIGINT, &sa, NULL);
947 memset (&sa, 0, sizeof (sa));
948 sa.sa_handler = SIG_IGN;
949 sigaction (SIGPIPE, &sa, NULL);
952 openlog ("rrdd", LOG_PID, LOG_DAEMON);
954 cache_tree = g_tree_new ((GCompareFunc) strcmp);
955 if (cache_tree == NULL)
957 RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed.");
961 memset (&queue_thread, 0, sizeof (queue_thread));
962 status = pthread_create (&queue_thread, /* attr = */ NULL,
963 queue_thread_main, /* args = */ NULL);
966 RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
971 } /* }}} int daemonize */
973 static int cleanup (void) /* {{{ */
975 RRDD_LOG (LOG_DEBUG, "cleanup ()");
979 RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
980 pthread_cond_signal (&cache_cond);
981 pthread_join (queue_thread, /* return = */ NULL);
982 RRDD_LOG (LOG_DEBUG, "cleanup: done");
987 } /* }}} int cleanup */
989 static int read_options (int argc, char **argv) /* {{{ */
994 while ((option = getopt(argc, argv, "l:f:w:h?")) != -1)
1002 temp = (char **) realloc (config_listen_address_list,
1003 sizeof (char *) * (config_listen_address_list_len + 1));
1006 fprintf (stderr, "read_options: realloc failed.\n");
1009 config_listen_address_list = temp;
1011 temp[config_listen_address_list_len] = strdup (optarg);
1012 if (temp[config_listen_address_list_len] == NULL)
1014 fprintf (stderr, "read_options: strdup failed.\n");
1017 config_listen_address_list_len++;
1025 temp = atoi (optarg);
1027 config_flush_interval = temp;
1030 fprintf (stderr, "Invalid flush interval: %s\n", optarg);
1040 temp = atoi (optarg);
1042 config_write_interval = temp;
1045 fprintf (stderr, "Invalid write interval: %s\n", optarg);
1053 printf ("RRDd %s Copyright (C) 2008 Florian octo Forster\n"
1055 "Usage: rrdd [options]\n"
1057 "Valid options are:\n"
1058 " -l <address> Socket address to listen to.\n"
1059 " -w <seconds> Interval in which to write data.\n"
1060 " -f <seconds> Interval in which to flush dead data.\n"
1062 "For more information and a detailed description of all options "
1064 "to the rrdd(1) manual page.\n",
1068 } /* switch (option) */
1069 } /* while (getopt) */
1072 } /* }}} int read_options */
1074 int main (int argc, char **argv)
1078 status = read_options (argc, argv);
1086 status = daemonize ();
1089 struct sigaction sigchld;
1091 memset (&sigchld, 0, sizeof (sigchld));
1092 sigchld.sa_handler = SIG_IGN;
1093 sigaction (SIGCHLD, &sigchld, NULL);
1097 else if (status != 0)
1099 fprintf (stderr, "daemonize failed, exiting.\n");
1103 listen_thread_main (NULL);
1111 * vim: set sw=2 sts=2 ts=8 et fdm=marker :