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
119 struct callback_flush_data_s
125 typedef struct callback_flush_data_s callback_flush_data_t;
132 typedef enum queue_side_e queue_side_t;
137 static listen_socket_t *listen_fds = NULL;
138 static size_t listen_fds_num = 0;
140 static int do_shutdown = 0;
142 static pthread_t queue_thread;
144 static pthread_t *connetion_threads = NULL;
145 static pthread_mutex_t connetion_threads_lock = PTHREAD_MUTEX_INITIALIZER;
146 static int connetion_threads_num = 0;
149 static GTree *cache_tree = NULL;
150 static cache_item_t *cache_queue_head = NULL;
151 static cache_item_t *cache_queue_tail = NULL;
152 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
153 static pthread_cond_t cache_cond = PTHREAD_COND_INITIALIZER;
155 static pthread_cond_t flush_cond = PTHREAD_COND_INITIALIZER;
157 static int config_write_interval = 300;
158 static int config_flush_interval = 3600;
159 static char *config_pid_file = NULL;
160 static char *config_base_dir = NULL;
162 static char **config_listen_address_list = NULL;
163 static int config_listen_address_list_len = 0;
165 static uint64_t stats_queue_length = 0;
166 static uint64_t stats_updates_total = 0;
167 static uint64_t stats_values_total = 0;
168 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
173 static void sig_int_handler (int s __attribute__((unused))) /* {{{ */
176 } /* }}} void sig_int_handler */
178 static void sig_term_handler (int s __attribute__((unused))) /* {{{ */
181 } /* }}} void sig_term_handler */
183 static int write_pidfile (void) /* {{{ */
191 file = (config_pid_file != NULL)
193 : LOCALSTATEDIR "/run/rrdcached.pid";
195 fh = fopen (file, "w");
198 RRDD_LOG (LOG_ERR, "write_pidfile: Opening `%s' failed.", file);
202 fprintf (fh, "%i\n", (int) pid);
206 } /* }}} int write_pidfile */
208 static int remove_pidfile (void) /* {{{ */
213 file = (config_pid_file != NULL)
215 : LOCALSTATEDIR "/run/rrdcached.pid";
217 status = unlink (file);
221 } /* }}} int remove_pidfile */
224 * enqueue_cache_item:
225 * `cache_lock' must be acquired before calling this function!
227 static int enqueue_cache_item (cache_item_t *ci, /* {{{ */
232 RRDD_LOG (LOG_DEBUG, "enqueue_cache_item: Adding %s to the update queue.",
238 if (ci->values_num == 0)
243 if ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
245 assert (ci->next == NULL);
246 ci->next = cache_queue_head;
247 cache_queue_head = ci;
249 if (cache_queue_tail == NULL)
250 cache_queue_tail = cache_queue_head;
254 else if (cache_queue_head == ci)
258 else /* enqueued, but not first entry */
262 /* find previous entry */
263 for (prev = cache_queue_head; prev != NULL; prev = prev->next)
264 if (prev->next == ci)
266 assert (prev != NULL);
268 /* move to the front */
269 prev->next = ci->next;
270 ci->next = cache_queue_head;
271 cache_queue_head = ci;
273 /* check if we need to adapt the tail */
274 if (cache_queue_tail == ci)
275 cache_queue_tail = prev;
278 else /* (side == TAIL) */
280 /* We don't move values back in the list.. */
281 if ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
284 assert (ci->next == NULL);
286 if (cache_queue_tail == NULL)
287 cache_queue_head = ci;
289 cache_queue_tail->next = ci;
290 cache_queue_tail = ci;
295 ci->flags |= CI_FLAGS_IN_QUEUE;
299 pthread_mutex_lock (&stats_lock);
300 stats_queue_length++;
301 pthread_mutex_unlock (&stats_lock);
305 } /* }}} int enqueue_cache_item */
308 * tree_callback_flush:
309 * Called via `g_tree_foreach' in `queue_thread_main'. `cache_lock' is held
310 * while this is in progress.
312 static gboolean tree_callback_flush (gpointer key, gpointer value, /* {{{ */
316 callback_flush_data_t *cfd;
318 ci = (cache_item_t *) value;
319 cfd = (callback_flush_data_t *) data;
321 if (((cfd->now - ci->last_flush_time) >= config_write_interval)
322 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
323 && (ci->values_num > 0))
325 enqueue_cache_item (ci, TAIL);
327 else if (((cfd->now - ci->last_flush_time) >= config_flush_interval)
328 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
329 && (ci->values_num <= 0))
333 temp = (char **) realloc (cfd->keys,
334 sizeof (char *) * (cfd->keys_num + 1));
337 RRDD_LOG (LOG_ERR, "tree_callback_flush: realloc failed.");
341 /* Make really sure this points to the _same_ place */
342 assert ((char *) key == ci->file);
343 cfd->keys[cfd->keys_num] = (char *) key;
348 } /* }}} gboolean tree_callback_flush */
350 static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
353 struct timespec next_flush;
355 gettimeofday (&now, NULL);
356 next_flush.tv_sec = now.tv_sec + config_flush_interval;
357 next_flush.tv_nsec = 1000 * now.tv_usec;
359 pthread_mutex_lock (&cache_lock);
360 while ((do_shutdown == 0) || (cache_queue_head != NULL))
369 /* First, check if it's time to do the cache flush. */
370 gettimeofday (&now, NULL);
371 if ((now.tv_sec > next_flush.tv_sec)
372 || ((now.tv_sec == next_flush.tv_sec)
373 && ((1000 * now.tv_usec) > next_flush.tv_nsec)))
375 callback_flush_data_t cfd;
378 memset (&cfd, 0, sizeof (cfd));
379 /* Pass the current time as user data so that we don't need to call
380 * `time' for each node. */
381 cfd.now = time (NULL);
385 /* `tree_callback_flush' will return the keys of all values that haven't
386 * been touched in the last `config_flush_interval' seconds in `cfd'.
387 * The char*'s in this array point to the same memory as ci->file, so we
388 * don't need to free them separately. */
389 g_tree_foreach (cache_tree, tree_callback_flush, (gpointer) &cfd);
391 for (k = 0; k < cfd.keys_num; k++)
393 /* This must not fail. */
394 ci = (cache_item_t *) g_tree_lookup (cache_tree, cfd.keys[k]);
397 /* If we end up here with values available, something's seriously
399 assert (ci->values_num == 0);
401 /* Remove the node from the tree */
402 g_tree_remove (cache_tree, cfd.keys[k]);
405 /* Now free and clean up `ci'. */
410 } /* for (k = 0; k < cfd.keys_num; k++) */
412 if (cfd.keys != NULL)
418 /* Determine the time of the next cache flush. */
419 while (next_flush.tv_sec < now.tv_sec)
420 next_flush.tv_sec += config_flush_interval;
423 /* Now, check if there's something to store away. If not, wait until
424 * something comes in or it's time to do the cache flush. */
425 if (cache_queue_head == NULL)
427 status = pthread_cond_timedwait (&cache_cond, &cache_lock, &next_flush);
428 if ((status != 0) && (status != ETIMEDOUT))
430 RRDD_LOG (LOG_ERR, "queue_thread_main: "
431 "pthread_cond_timedwait returned %i.", status);
435 /* Check if a value has arrived. This may be NULL if we timed out or there
436 * was an interrupt such as a signal. */
437 if (cache_queue_head == NULL)
440 ci = cache_queue_head;
442 /* copy the relevant parts */
443 file = strdup (ci->file);
446 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
451 values_num = ci->values_num;
456 ci->last_flush_time = time (NULL);
457 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
459 cache_queue_head = ci->next;
460 if (cache_queue_head == NULL)
461 cache_queue_tail = NULL;
464 pthread_mutex_lock (&stats_lock);
465 assert (stats_queue_length > 0);
466 stats_queue_length--;
467 pthread_mutex_unlock (&stats_lock);
469 pthread_mutex_unlock (&cache_lock);
471 RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
472 file, values_num, (void *) values);
474 status = rrd_update_r (file, NULL, values_num, (void *) values);
477 RRDD_LOG (LOG_ERR, "queue_thread_main: "
478 "rrd_update_r failed with status %i.",
483 for (i = 0; i < values_num; i++)
486 pthread_mutex_lock (&stats_lock);
487 stats_updates_total++;
488 stats_values_total += values_num;
489 pthread_mutex_unlock (&stats_lock);
491 pthread_mutex_lock (&cache_lock);
492 pthread_cond_broadcast (&flush_cond);
493 } /* while (do_shutdown == 0) */
494 pthread_mutex_unlock (&cache_lock);
496 RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
499 } /* }}} void *queue_thread_main */
501 static int buffer_get_field (char **buffer_ret, /* {{{ */
502 size_t *buffer_size_ret, char **field_ret)
511 buffer = *buffer_ret;
513 buffer_size = *buffer_size_ret;
517 /* This is ensured by `handle_request'. */
518 assert (buffer[buffer_size - 1] == ' ');
521 while (buffer_pos < buffer_size)
523 /* Check for end-of-field or end-of-buffer */
524 if (buffer[buffer_pos] == ' ')
526 field[field_size] = 0;
532 /* Handle escaped characters. */
533 else if (buffer[buffer_pos] == '\\')
535 if (buffer_pos >= (buffer_size - 1))
538 field[field_size] = buffer[buffer_pos];
542 /* Normal operation */
545 field[field_size] = buffer[buffer_pos];
549 } /* while (buffer_pos < buffer_size) */
554 *buffer_ret = buffer + buffer_pos;
555 *buffer_size_ret = buffer_size - buffer_pos;
559 } /* }}} int buffer_get_field */
561 static int flush_file (const char *filename) /* {{{ */
565 pthread_mutex_lock (&cache_lock);
567 ci = (cache_item_t *) g_tree_lookup (cache_tree, filename);
570 pthread_mutex_unlock (&cache_lock);
574 /* Enqueue at head */
575 enqueue_cache_item (ci, HEAD);
576 pthread_cond_signal (&cache_cond);
578 while ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
582 pthread_cond_wait (&flush_cond, &cache_lock);
584 ci = g_tree_lookup (cache_tree, filename);
587 RRDD_LOG (LOG_ERR, "flush_file: Tree node went away "
588 "while waiting for flush.");
589 pthread_mutex_unlock (&cache_lock);
594 pthread_mutex_unlock (&cache_lock);
596 } /* }}} int flush_file */
598 static int handle_request_flush (int fd, /* {{{ */
599 char *buffer, size_t buffer_size)
605 status = buffer_get_field (&buffer, &buffer_size, &file);
608 RRDD_LOG (LOG_INFO, "handle_request_flush: Cannot get file name.");
612 status = flush_file (file);
614 snprintf (result, sizeof (result), "0 Successfully flushed %s.\n", file);
615 else if (status == ENOENT)
616 snprintf (result, sizeof (result), "-1 No such file: %s.\n", file);
618 strncpy (result, "-1 Internal error.\n", sizeof (result));
620 snprintf (result, sizeof (result), "-1 Failed with status %i.\n", status);
621 result[sizeof (result) - 1] = 0;
623 status = write (fd, result, strlen (result));
627 RRDD_LOG (LOG_INFO, "handle_request_flush: write(2) returned an error.");
632 } /* }}} int handle_request_flush */
634 static int handle_request_update (int fd, /* {{{ */
635 char *buffer, size_t buffer_size)
648 status = buffer_get_field (&buffer, &buffer_size, &file);
651 RRDD_LOG (LOG_INFO, "handle_request_update: Cannot get file name.");
655 pthread_mutex_lock (&cache_lock);
657 ci = g_tree_lookup (cache_tree, file);
658 if (ci == NULL) /* {{{ */
660 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
663 pthread_mutex_unlock (&cache_lock);
664 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
667 memset (ci, 0, sizeof (cache_item_t));
669 ci->file = strdup (file);
670 if (ci->file == NULL)
672 pthread_mutex_unlock (&cache_lock);
673 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
680 ci->last_flush_time = now;
681 ci->flags = CI_FLAGS_IN_TREE;
683 g_tree_insert (cache_tree, (void *) ci->file, (void *) ci);
685 RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new tree node %s.",
690 while (buffer_size > 0)
695 status = buffer_get_field (&buffer, &buffer_size, &value);
698 RRDD_LOG (LOG_INFO, "handle_request_update: Error reading field.");
702 temp = (char **) realloc (ci->values,
703 sizeof (char *) * (ci->values_num + 1));
706 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
711 ci->values[ci->values_num] = strdup (value);
712 if (ci->values[ci->values_num] == NULL)
714 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
722 if (((now - ci->last_flush_time) >= config_write_interval)
723 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
724 && (ci->values_num > 0))
726 enqueue_cache_item (ci, TAIL);
727 pthread_cond_signal (&cache_cond);
730 pthread_mutex_unlock (&cache_lock);
732 snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
733 answer[sizeof (answer) - 1] = 0;
735 status = write (fd, answer, strlen (answer));
739 RRDD_LOG (LOG_INFO, "handle_request_update: write(2) returned an error.");
744 } /* }}} int handle_request_update */
746 static int handle_request (int fd) /* {{{ */
754 status = read (fd, buffer, sizeof (buffer));
761 RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
764 buffer_size = status;
765 assert (((size_t) buffer_size) <= sizeof (buffer));
767 if (buffer[buffer_size - 1] != '\n')
769 RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
773 /* Accept Windows style line endings, too */
774 if ((buffer_size > 2) && (buffer[buffer_size - 2] == '\r'))
777 buffer[buffer_size - 1] = '\n';
780 /* Place the normal field separator at the end to simplify
781 * `buffer_get_field's work. */
782 buffer[buffer_size - 1] = ' ';
786 status = buffer_get_field (&buffer_ptr, &buffer_size, &command);
789 RRDD_LOG (LOG_INFO, "handle_request: Unable parse command.");
793 if (strcmp (command, "update") == 0)
795 return (handle_request_update (fd, buffer_ptr, buffer_size));
797 else if (strcmp (command, "flush") == 0)
799 return (handle_request_flush (fd, buffer_ptr, buffer_size));
803 RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
806 } /* }}} int handle_request */
808 static void *connection_thread_main (void *args /* {{{ */
809 __attribute__((unused)))
815 fd = *((int *) args);
817 pthread_mutex_lock (&connetion_threads_lock);
821 temp = (pthread_t *) realloc (connetion_threads,
822 sizeof (pthread_t) * (connetion_threads_num + 1));
825 RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
829 connetion_threads = temp;
830 connetion_threads[connetion_threads_num] = pthread_self ();
831 connetion_threads_num++;
834 pthread_mutex_unlock (&connetion_threads_lock);
836 while (do_shutdown == 0)
838 struct pollfd pollfd;
842 pollfd.events = POLLIN | POLLPRI;
845 status = poll (&pollfd, 1, /* timeout = */ 500);
846 if (status == 0) /* timeout */
848 else if (status < 0) /* error */
853 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
857 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
862 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
864 RRDD_LOG (LOG_WARNING, "connection_thread_main: "
865 "poll(2) returned something unexpected: %#04hx",
871 status = handle_request (fd);
879 self = pthread_self ();
880 /* Remove this thread from the connection threads list */
881 pthread_mutex_lock (&connetion_threads_lock);
882 /* Find out own index in the array */
883 for (i = 0; i < connetion_threads_num; i++)
884 if (pthread_equal (connetion_threads[i], self) != 0)
886 assert (i < connetion_threads_num);
888 /* Move the trailing threads forward. */
889 if (i < (connetion_threads_num - 1))
891 memmove (connetion_threads + i,
892 connetion_threads + i + 1,
893 sizeof (pthread_t) * (connetion_threads_num - i - 1));
896 connetion_threads_num--;
897 pthread_mutex_unlock (&connetion_threads_lock);
901 } /* }}} void *connection_thread_main */
903 static int open_listen_socket_unix (const char *path) /* {{{ */
906 struct sockaddr_un sa;
907 listen_socket_t *temp;
910 temp = (listen_socket_t *) realloc (listen_fds,
911 sizeof (listen_fds[0]) * (listen_fds_num + 1));
914 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: realloc failed.");
918 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
920 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
923 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: socket(2) failed.");
927 memset (&sa, 0, sizeof (sa));
928 sa.sun_family = AF_UNIX;
929 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
931 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
934 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: bind(2) failed.");
940 status = listen (fd, /* backlog = */ 10);
943 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: listen(2) failed.");
949 listen_fds[listen_fds_num].fd = fd;
950 snprintf (listen_fds[listen_fds_num].path,
951 sizeof (listen_fds[listen_fds_num].path) - 1,
956 } /* }}} int open_listen_socket_unix */
958 static int open_listen_socket (const char *addr) /* {{{ */
960 struct addrinfo ai_hints;
961 struct addrinfo *ai_res;
962 struct addrinfo *ai_ptr;
965 assert (addr != NULL);
967 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
968 return (open_listen_socket_unix (addr + strlen ("unix:")));
969 else if (addr[0] == '/')
970 return (open_listen_socket_unix (addr));
972 memset (&ai_hints, 0, sizeof (ai_hints));
973 ai_hints.ai_flags = 0;
975 ai_hints.ai_flags |= AI_ADDRCONFIG;
977 ai_hints.ai_family = AF_UNSPEC;
978 ai_hints.ai_socktype = SOCK_STREAM;
981 status = getaddrinfo (addr, RRDCACHED_DEFAULT_PORT, &ai_hints, &ai_res);
984 RRDD_LOG (LOG_ERR, "open_listen_socket: getaddrinfo(%s) failed: "
985 "%s", addr, gai_strerror (status));
989 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
992 listen_socket_t *temp;
994 temp = (listen_socket_t *) realloc (listen_fds,
995 sizeof (listen_fds[0]) * (listen_fds_num + 1));
998 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
1002 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
1004 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1007 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
1011 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1014 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
1019 status = listen (fd, /* backlog = */ 10);
1022 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
1027 listen_fds[listen_fds_num].fd = fd;
1028 strncpy (listen_fds[listen_fds_num].path, addr,
1029 sizeof (listen_fds[listen_fds_num].path) - 1);
1031 } /* for (ai_ptr) */
1034 } /* }}} int open_listen_socket */
1036 static int close_listen_sockets (void) /* {{{ */
1040 for (i = 0; i < listen_fds_num; i++)
1042 close (listen_fds[i].fd);
1043 if (strncmp ("unix:", listen_fds[i].path, strlen ("unix:")) == 0)
1044 unlink (listen_fds[i].path + strlen ("unix:"));
1052 } /* }}} int close_listen_sockets */
1054 static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
1056 struct pollfd *pollfds;
1061 for (i = 0; i < config_listen_address_list_len; i++)
1063 RRDD_LOG (LOG_DEBUG, "listen_thread_main: config_listen_address_list[%i] "
1064 "= %s", i, config_listen_address_list[i]);
1065 open_listen_socket (config_listen_address_list[i]);
1068 if (config_listen_address_list_len < 1)
1069 open_listen_socket (RRDCACHED_DEFAULT_ADDRESS);
1071 if (listen_fds_num < 1)
1073 RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
1074 "could be opened. Sorry.");
1078 pollfds_num = listen_fds_num;
1079 pollfds = (struct pollfd *) malloc (sizeof (*pollfds) * pollfds_num);
1080 if (pollfds == NULL)
1082 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
1085 memset (pollfds, 0, sizeof (*pollfds) * pollfds_num);
1087 while (do_shutdown == 0)
1089 assert (pollfds_num == ((int) listen_fds_num));
1090 for (i = 0; i < pollfds_num; i++)
1092 pollfds[i].fd = listen_fds[i].fd;
1093 pollfds[i].events = POLLIN | POLLPRI;
1094 pollfds[i].revents = 0;
1097 status = poll (pollfds, pollfds_num, /* timeout = */ -1);
1101 if (status != EINTR)
1103 RRDD_LOG (LOG_ERR, "listen_thread_main: poll(2) failed.");
1108 for (i = 0; i < pollfds_num; i++)
1111 struct sockaddr_storage client_sa;
1112 socklen_t client_sa_size;
1115 if (pollfds[i].revents == 0)
1118 if ((pollfds[i].revents & (POLLIN | POLLPRI)) == 0)
1120 RRDD_LOG (LOG_ERR, "listen_thread_main: "
1121 "poll(2) returned something unexpected for listen FD #%i.",
1126 client_sd = (int *) malloc (sizeof (int));
1127 if (client_sd == NULL)
1129 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
1133 client_sa_size = sizeof (client_sa);
1134 *client_sd = accept (pollfds[i].fd,
1135 (struct sockaddr *) &client_sa, &client_sa_size);
1138 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
1142 status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
1143 /* args = */ (void *) client_sd);
1146 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
1151 } /* for (pollfds_num) */
1152 } /* while (do_shutdown == 0) */
1154 close_listen_sockets ();
1156 pthread_mutex_lock (&connetion_threads_lock);
1157 while (connetion_threads_num > 0)
1161 wait_for = connetion_threads[0];
1163 pthread_mutex_unlock (&connetion_threads_lock);
1164 pthread_join (wait_for, /* retval = */ NULL);
1165 pthread_mutex_lock (&connetion_threads_lock);
1167 pthread_mutex_unlock (&connetion_threads_lock);
1169 RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
1172 } /* }}} void *listen_thread_main */
1174 static int daemonize (void) /* {{{ */
1180 /* These structures are static, because `sigaction' behaves weird if the are
1182 static struct sigaction sa_int;
1183 static struct sigaction sa_term;
1184 static struct sigaction sa_pipe;
1189 fprintf (stderr, "daemonize: fork(2) failed.\n");
1197 /* Change into the /tmp directory. */
1198 base_dir = (config_base_dir != NULL)
1201 status = chdir (base_dir);
1204 fprintf (stderr, "daemonize: chdir (%s) failed.\n", base_dir);
1208 /* Become session leader */
1211 /* Open the first three file descriptors to /dev/null */
1216 open ("/dev/null", O_RDWR);
1220 /* Install signal handlers */
1221 memset (&sa_int, 0, sizeof (sa_int));
1222 sa_int.sa_handler = sig_int_handler;
1223 sigaction (SIGINT, &sa_int, NULL);
1225 memset (&sa_term, 0, sizeof (sa_term));
1226 sa_term.sa_handler = sig_term_handler;
1227 sigaction (SIGINT, &sa_term, NULL);
1229 memset (&sa_pipe, 0, sizeof (sa_pipe));
1230 sa_pipe.sa_handler = SIG_IGN;
1231 sigaction (SIGPIPE, &sa_pipe, NULL);
1233 openlog ("rrdcached", LOG_PID, LOG_DAEMON);
1235 cache_tree = g_tree_new ((GCompareFunc) strcmp);
1236 if (cache_tree == NULL)
1238 RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed.");
1242 memset (&queue_thread, 0, sizeof (queue_thread));
1243 status = pthread_create (&queue_thread, /* attr = */ NULL,
1244 queue_thread_main, /* args = */ NULL);
1247 RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
1254 } /* }}} int daemonize */
1256 static int cleanup (void) /* {{{ */
1258 RRDD_LOG (LOG_DEBUG, "cleanup ()");
1262 RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
1263 pthread_cond_signal (&cache_cond);
1264 pthread_join (queue_thread, /* return = */ NULL);
1265 RRDD_LOG (LOG_DEBUG, "cleanup: done");
1272 } /* }}} int cleanup */
1274 static int read_options (int argc, char **argv) /* {{{ */
1279 while ((option = getopt(argc, argv, "l:f:w:b:p:h?")) != -1)
1287 temp = (char **) realloc (config_listen_address_list,
1288 sizeof (char *) * (config_listen_address_list_len + 1));
1291 fprintf (stderr, "read_options: realloc failed.\n");
1294 config_listen_address_list = temp;
1296 temp[config_listen_address_list_len] = strdup (optarg);
1297 if (temp[config_listen_address_list_len] == NULL)
1299 fprintf (stderr, "read_options: strdup failed.\n");
1302 config_listen_address_list_len++;
1310 temp = atoi (optarg);
1312 config_flush_interval = temp;
1315 fprintf (stderr, "Invalid flush interval: %s\n", optarg);
1325 temp = atoi (optarg);
1327 config_write_interval = temp;
1330 fprintf (stderr, "Invalid write interval: %s\n", optarg);
1340 if (config_base_dir != NULL)
1341 free (config_base_dir);
1342 config_base_dir = strdup (optarg);
1343 if (config_base_dir == NULL)
1345 fprintf (stderr, "read_options: strdup failed.\n");
1349 len = strlen (config_base_dir);
1350 while ((len > 0) && (config_base_dir[len - 1] == '/'))
1352 config_base_dir[len - 1] = 0;
1358 fprintf (stderr, "Invalid base directory: %s\n", optarg);
1366 if (config_pid_file != NULL)
1367 free (config_pid_file);
1368 config_pid_file = strdup (optarg);
1369 if (config_pid_file == NULL)
1371 fprintf (stderr, "read_options: strdup failed.\n");
1379 printf ("RRDd %s Copyright (C) 2008 Florian octo Forster\n"
1381 "Usage: rrdcached [options]\n"
1383 "Valid options are:\n"
1384 " -l <address> Socket address to listen to.\n"
1385 " -w <seconds> Interval in which to write data.\n"
1386 " -f <seconds> Interval in which to flush dead data.\n"
1387 " -p <file> Location of the PID-file.\n"
1388 " -b <dir> Base directory to change to.\n"
1390 "For more information and a detailed description of all options "
1392 "to the rrdcached(1) manual page.\n",
1396 } /* switch (option) */
1397 } /* while (getopt) */
1400 } /* }}} int read_options */
1402 int main (int argc, char **argv)
1406 status = read_options (argc, argv);
1414 status = daemonize ();
1417 struct sigaction sigchld;
1419 memset (&sigchld, 0, sizeof (sigchld));
1420 sigchld.sa_handler = SIG_IGN;
1421 sigaction (SIGCHLD, &sigchld, NULL);
1425 else if (status != 0)
1427 fprintf (stderr, "daemonize failed, exiting.\n");
1431 listen_thread_main (NULL);
1439 * vim: set sw=2 sts=2 ts=8 et fdm=marker :