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
124 typedef enum queue_side_e queue_side_t;
129 static listen_socket_t *listen_fds = NULL;
130 static size_t listen_fds_num = 0;
132 static int do_shutdown = 0;
134 static pthread_t queue_thread;
136 static pthread_t *connetion_threads = NULL;
137 static pthread_mutex_t connetion_threads_lock = PTHREAD_MUTEX_INITIALIZER;
138 static int connetion_threads_num = 0;
141 static GTree *cache_tree = NULL;
142 static cache_item_t *cache_queue_head = NULL;
143 static cache_item_t *cache_queue_tail = NULL;
144 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
145 static pthread_cond_t cache_cond = PTHREAD_COND_INITIALIZER;
147 static pthread_cond_t flush_cond = PTHREAD_COND_INITIALIZER;
149 static int config_write_interval = 300;
150 static int config_flush_interval = 3600;
152 static char **config_listen_address_list = NULL;
153 static int config_listen_address_list_len = 0;
158 static void sig_int_handler (int s __attribute__((unused))) /* {{{ */
161 } /* }}} void sig_int_handler */
163 static void sig_term_handler (int s __attribute__((unused))) /* {{{ */
166 } /* }}} void sig_term_handler */
169 * enqueue_cache_item:
170 * `cache_lock' must be acquired before calling this function!
172 static int enqueue_cache_item (cache_item_t *ci, /* {{{ */
175 RRDD_LOG (LOG_DEBUG, "enqueue_cache_item: Adding %s to the update queue.",
181 if (ci->values_num == 0)
186 if ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
188 assert (ci->next == NULL);
189 ci->next = cache_queue_head;
190 cache_queue_head = ci;
192 if (cache_queue_tail == NULL)
193 cache_queue_tail = cache_queue_head;
195 else if (cache_queue_head == ci)
199 else /* enqueued, but not first entry */
203 /* find previous entry */
204 for (prev = cache_queue_head; prev != NULL; prev = prev->next)
205 if (prev->next == ci)
207 assert (prev != NULL);
209 /* move to the front */
210 prev->next = ci->next;
211 ci->next = cache_queue_head;
212 cache_queue_head = ci;
214 /* check if we need to adapt the tail */
215 if (cache_queue_tail == ci)
216 cache_queue_tail = prev;
219 else /* (side == TAIL) */
221 /* We don't move values back in the list.. */
222 if ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
225 assert (ci->next == NULL);
227 if (cache_queue_tail == NULL)
228 cache_queue_head = ci;
230 cache_queue_tail->next = ci;
231 cache_queue_tail = ci;
234 ci->flags |= CI_FLAGS_IN_QUEUE;
237 } /* }}} int enqueue_cache_item */
240 * tree_callback_flush:
241 * Called via `g_tree_foreach' in `queue_thread_main'. `cache_lock' is held
242 * while this is in progress.
244 static gboolean tree_callback_flush (gpointer key /* {{{ */
245 __attribute__((unused)), gpointer value, gpointer data)
250 key = NULL; /* make compiler happy */
252 ci = (cache_item_t *) value;
253 now = *((time_t *) data);
255 if (((now - ci->last_flush_time) >= config_write_interval)
256 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
257 && (ci->values_num > 0))
258 enqueue_cache_item (ci, TAIL);
261 } /* }}} gboolean tree_callback_flush */
263 static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
266 struct timespec next_flush;
268 gettimeofday (&now, NULL);
269 next_flush.tv_sec = now.tv_sec + config_flush_interval;
270 next_flush.tv_nsec = 1000 * now.tv_usec;
272 pthread_mutex_lock (&cache_lock);
273 while ((do_shutdown == 0) || (cache_queue_head != NULL))
282 /* First, check if it's time to do the cache flush. */
283 gettimeofday (&now, NULL);
284 if ((now.tv_sec > next_flush.tv_sec)
285 || ((now.tv_sec == next_flush.tv_sec)
286 && ((1000 * now.tv_usec) > next_flush.tv_nsec)))
290 /* Pass the current time as user data so that we don't need to call
291 * `time' for each node. */
292 time_now = time (NULL);
294 g_tree_foreach (cache_tree, tree_callback_flush, (gpointer) &time_now);
296 /* Determine the time of the next cache flush. */
297 while (next_flush.tv_sec < now.tv_sec)
298 next_flush.tv_sec += config_flush_interval;
301 /* Now, check if there's something to store away. If not, wait until
302 * something comes in or it's time to do the cache flush. */
303 if (cache_queue_head == NULL)
305 status = pthread_cond_timedwait (&cache_cond, &cache_lock, &next_flush);
306 if ((status != 0) && (status != ETIMEDOUT))
308 RRDD_LOG (LOG_ERR, "queue_thread_main: "
309 "pthread_cond_timedwait returned %i.", status);
313 /* Check if a value has arrived. This may be NULL if we timed out or there
314 * was an interrupt such as a signal. */
315 if (cache_queue_head == NULL)
318 ci = cache_queue_head;
320 /* copy the relevant parts */
321 file = strdup (ci->file);
324 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
329 values_num = ci->values_num;
334 ci->last_flush_time = time (NULL);
335 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
337 cache_queue_head = ci->next;
338 if (cache_queue_head == NULL)
339 cache_queue_tail = NULL;
342 pthread_mutex_unlock (&cache_lock);
344 RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
345 file, values_num, (void *) values);
347 status = rrd_update_r (file, NULL, values_num, (void *) values);
350 RRDD_LOG (LOG_ERR, "queue_thread_main: "
351 "rrd_update_r failed with status %i.",
356 for (i = 0; i < values_num; i++)
359 pthread_mutex_lock (&cache_lock);
360 pthread_cond_broadcast (&flush_cond);
361 } /* while (do_shutdown == 0) */
362 pthread_mutex_unlock (&cache_lock);
364 RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
367 } /* }}} void *queue_thread_main */
369 static int buffer_get_field (char **buffer_ret, /* {{{ */
370 size_t *buffer_size_ret, char **field_ret)
379 buffer = *buffer_ret;
381 buffer_size = *buffer_size_ret;
385 /* This is ensured by `handle_request'. */
386 assert (buffer[buffer_size - 1] == ' ');
389 while (buffer_pos < buffer_size)
391 /* Check for end-of-field or end-of-buffer */
392 if (buffer[buffer_pos] == ' ')
394 field[field_size] = 0;
400 /* Handle escaped characters. */
401 else if (buffer[buffer_pos] == '\\')
403 if (buffer_pos >= (buffer_size - 1))
406 field[field_size] = buffer[buffer_pos];
410 /* Normal operation */
413 field[field_size] = buffer[buffer_pos];
417 } /* while (buffer_pos < buffer_size) */
422 *buffer_ret = buffer + buffer_pos;
423 *buffer_size_ret = buffer_size - buffer_pos;
427 } /* }}} int buffer_get_field */
429 static int flush_file (const char *filename) /* {{{ */
433 pthread_mutex_lock (&cache_lock);
435 ci = g_tree_lookup (cache_tree, filename);
438 pthread_mutex_unlock (&cache_lock);
442 /* Enqueue at head */
443 enqueue_cache_item (ci, HEAD);
444 pthread_cond_signal (&cache_cond);
446 while ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
450 pthread_cond_wait (&flush_cond, &cache_lock);
452 ci = g_tree_lookup (cache_tree, filename);
455 RRDD_LOG (LOG_ERR, "flush_file: Tree node went away "
456 "while waiting for flush.");
457 pthread_mutex_unlock (&cache_lock);
462 pthread_mutex_unlock (&cache_lock);
464 } /* }}} int flush_file */
466 static int handle_request_flush (int fd, /* {{{ */
467 char *buffer, size_t buffer_size)
473 status = buffer_get_field (&buffer, &buffer_size, &file);
476 RRDD_LOG (LOG_INFO, "handle_request_flush: Cannot get file name.");
480 status = flush_file (file);
482 snprintf (result, sizeof (result), "0 Successfully flushed %s.\n", file);
483 else if (status == ENOENT)
484 snprintf (result, sizeof (result), "-1 No such file: %s.\n", file);
486 strncpy (result, "-1 Internal error.\n", sizeof (result));
488 snprintf (result, sizeof (result), "-1 Failed with status %i.\n", status);
489 result[sizeof (result) - 1] = 0;
491 status = write (fd, result, strlen (result));
495 RRDD_LOG (LOG_INFO, "handle_request_flush: write(2) returned an error.");
500 } /* }}} int handle_request_flush */
502 static int handle_request_update (int fd, /* {{{ */
503 char *buffer, size_t buffer_size)
516 status = buffer_get_field (&buffer, &buffer_size, &file);
519 RRDD_LOG (LOG_INFO, "handle_request_update: Cannot get file name.");
523 pthread_mutex_lock (&cache_lock);
525 ci = g_tree_lookup (cache_tree, file);
526 if (ci == NULL) /* {{{ */
528 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
531 pthread_mutex_unlock (&cache_lock);
532 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
535 memset (ci, 0, sizeof (cache_item_t));
537 ci->file = strdup (file);
538 if (ci->file == NULL)
540 pthread_mutex_unlock (&cache_lock);
541 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
548 ci->last_flush_time = now;
549 ci->flags = CI_FLAGS_IN_TREE;
551 g_tree_insert (cache_tree, (void *) ci->file, (void *) ci);
553 RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new tree node %s.",
558 while (buffer_size > 0)
563 status = buffer_get_field (&buffer, &buffer_size, &value);
566 RRDD_LOG (LOG_INFO, "handle_request_update: Error reading field.");
570 temp = (char **) realloc (ci->values,
571 sizeof (char *) * (ci->values_num + 1));
574 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
579 ci->values[ci->values_num] = strdup (value);
580 if (ci->values[ci->values_num] == NULL)
582 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
590 if (((now - ci->last_flush_time) >= config_write_interval)
591 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
592 && (ci->values_num > 0))
594 enqueue_cache_item (ci, TAIL);
595 pthread_cond_signal (&cache_cond);
598 pthread_mutex_unlock (&cache_lock);
600 snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
601 answer[sizeof (answer) - 1] = 0;
603 status = write (fd, answer, strlen (answer));
607 RRDD_LOG (LOG_INFO, "handle_request_update: write(2) returned an error.");
612 } /* }}} int handle_request_update */
614 static int handle_request (int fd) /* {{{ */
622 status = read (fd, buffer, sizeof (buffer));
629 RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
632 buffer_size = status;
633 assert (((size_t) buffer_size) <= sizeof (buffer));
635 if (buffer[buffer_size - 1] != '\n')
637 RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
641 /* Accept Windows style line endings, too */
642 if ((buffer_size > 2) && (buffer[buffer_size - 2] == '\r'))
645 buffer[buffer_size - 1] = '\n';
648 /* Place the normal field separator at the end to simplify
649 * `buffer_get_field's work. */
650 buffer[buffer_size - 1] = ' ';
654 status = buffer_get_field (&buffer_ptr, &buffer_size, &command);
657 RRDD_LOG (LOG_INFO, "handle_request: Unable parse command.");
661 if (strcmp (command, "update") == 0)
663 return (handle_request_update (fd, buffer_ptr, buffer_size));
665 else if (strcmp (command, "flush") == 0)
667 return (handle_request_flush (fd, buffer_ptr, buffer_size));
671 RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
674 } /* }}} int handle_request */
676 static void *connection_thread_main (void *args /* {{{ */
677 __attribute__((unused)))
683 fd = *((int *) args);
685 pthread_mutex_lock (&connetion_threads_lock);
689 temp = (pthread_t *) realloc (connetion_threads,
690 sizeof (pthread_t) * (connetion_threads_num + 1));
693 RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
697 connetion_threads = temp;
698 connetion_threads[connetion_threads_num] = pthread_self ();
699 connetion_threads_num++;
702 pthread_mutex_unlock (&connetion_threads_lock);
704 while (do_shutdown == 0)
706 struct pollfd pollfd;
710 pollfd.events = POLLIN | POLLPRI;
713 status = poll (&pollfd, 1, /* timeout = */ 500);
714 if (status == 0) /* timeout */
716 else if (status < 0) /* error */
721 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
725 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
730 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
732 RRDD_LOG (LOG_WARNING, "connection_thread_main: "
733 "poll(2) returned something unexpected: %#04hx",
739 status = handle_request (fd);
747 self = pthread_self ();
748 /* Remove this thread from the connection threads list */
749 pthread_mutex_lock (&connetion_threads_lock);
750 /* Find out own index in the array */
751 for (i = 0; i < connetion_threads_num; i++)
752 if (pthread_equal (connetion_threads[i], self) != 0)
754 assert (i < connetion_threads_num);
756 /* Move the trailing threads forward. */
757 if (i < (connetion_threads_num - 1))
759 memmove (connetion_threads + i,
760 connetion_threads + i + 1,
761 sizeof (pthread_t) * (connetion_threads_num - i - 1));
764 connetion_threads_num--;
765 pthread_mutex_unlock (&connetion_threads_lock);
769 } /* }}} void *connection_thread_main */
771 static int open_listen_socket_unix (const char *path) /* {{{ */
774 struct sockaddr_un sa;
775 listen_socket_t *temp;
778 temp = (listen_socket_t *) realloc (listen_fds,
779 sizeof (listen_fds[0]) * (listen_fds_num + 1));
782 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: realloc failed.");
786 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
788 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
791 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: socket(2) failed.");
795 memset (&sa, 0, sizeof (sa));
796 sa.sun_family = AF_UNIX;
797 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
799 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
802 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: bind(2) failed.");
808 status = listen (fd, /* backlog = */ 10);
811 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: listen(2) failed.");
817 listen_fds[listen_fds_num].fd = fd;
818 snprintf (listen_fds[listen_fds_num].path,
819 sizeof (listen_fds[listen_fds_num].path) - 1,
824 } /* }}} int open_listen_socket_unix */
826 static int open_listen_socket (const char *addr) /* {{{ */
828 struct addrinfo ai_hints;
829 struct addrinfo *ai_res;
830 struct addrinfo *ai_ptr;
833 assert (addr != NULL);
835 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
836 return (open_listen_socket_unix (addr + strlen ("unix:")));
837 else if (addr[0] == '/')
838 return (open_listen_socket_unix (addr));
840 memset (&ai_hints, 0, sizeof (ai_hints));
841 ai_hints.ai_flags = 0;
843 ai_hints.ai_flags |= AI_ADDRCONFIG;
845 ai_hints.ai_family = AF_UNSPEC;
846 ai_hints.ai_socktype = SOCK_STREAM;
849 status = getaddrinfo (addr, DEFAULT_PORT, &ai_hints, &ai_res);
852 RRDD_LOG (LOG_ERR, "open_listen_socket: getaddrinfo(%s) failed: "
853 "%s", addr, gai_strerror (status));
857 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
860 listen_socket_t *temp;
862 temp = (listen_socket_t *) realloc (listen_fds,
863 sizeof (listen_fds[0]) * (listen_fds_num + 1));
866 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
870 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
872 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
875 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
879 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
882 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
887 status = listen (fd, /* backlog = */ 10);
890 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
895 listen_fds[listen_fds_num].fd = fd;
896 strncpy (listen_fds[listen_fds_num].path, addr,
897 sizeof (listen_fds[listen_fds_num].path) - 1);
902 } /* }}} int open_listen_socket */
904 static int close_listen_sockets (void) /* {{{ */
908 for (i = 0; i < listen_fds_num; i++)
910 close (listen_fds[i].fd);
911 if (strncmp ("unix:", listen_fds[i].path, strlen ("unix:")) == 0)
912 unlink (listen_fds[i].path + strlen ("unix:"));
920 } /* }}} int close_listen_sockets */
922 static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
924 struct pollfd *pollfds;
929 for (i = 0; i < config_listen_address_list_len; i++)
931 RRDD_LOG (LOG_DEBUG, "listen_thread_main: config_listen_address_list[%i] "
932 "= %s", i, config_listen_address_list[i]);
933 open_listen_socket (config_listen_address_list[i]);
936 if (config_listen_address_list_len < 1)
937 open_listen_socket (RRDD_SOCK_PATH);
939 if (listen_fds_num < 1)
941 RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
942 "could be opened. Sorry.");
946 pollfds_num = listen_fds_num;
947 pollfds = (struct pollfd *) malloc (sizeof (*pollfds) * pollfds_num);
950 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
953 memset (pollfds, 0, sizeof (*pollfds) * pollfds_num);
955 while (do_shutdown == 0)
957 assert (pollfds_num == ((int) listen_fds_num));
958 for (i = 0; i < pollfds_num; i++)
960 pollfds[i].fd = listen_fds[i].fd;
961 pollfds[i].events = POLLIN | POLLPRI;
962 pollfds[i].revents = 0;
965 status = poll (pollfds, pollfds_num, /* timeout = */ -1);
971 RRDD_LOG (LOG_ERR, "listen_thread_main: poll(2) failed.");
976 for (i = 0; i < pollfds_num; i++)
979 struct sockaddr_storage client_sa;
980 socklen_t client_sa_size;
983 if (pollfds[i].revents == 0)
986 if ((pollfds[i].revents & (POLLIN | POLLPRI)) == 0)
988 RRDD_LOG (LOG_ERR, "listen_thread_main: "
989 "poll(2) returned something unexpected for listen FD #%i.",
994 client_sd = (int *) malloc (sizeof (int));
995 if (client_sd == NULL)
997 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
1001 client_sa_size = sizeof (client_sa);
1002 *client_sd = accept (pollfds[i].fd,
1003 (struct sockaddr *) &client_sa, &client_sa_size);
1006 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
1010 status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
1011 /* args = */ (void *) client_sd);
1014 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
1019 } /* for (pollfds_num) */
1020 } /* while (do_shutdown == 0) */
1022 close_listen_sockets ();
1024 pthread_mutex_lock (&connetion_threads_lock);
1025 while (connetion_threads_num > 0)
1029 wait_for = connetion_threads[0];
1031 pthread_mutex_unlock (&connetion_threads_lock);
1032 pthread_join (wait_for, /* retval = */ NULL);
1033 pthread_mutex_lock (&connetion_threads_lock);
1035 pthread_mutex_unlock (&connetion_threads_lock);
1037 RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
1040 } /* }}} void *listen_thread_main */
1042 static int daemonize (void) /* {{{ */
1050 fprintf (stderr, "daemonize: fork(2) failed.\n");
1058 /* Change into the /tmp directory. */
1061 /* Become session leader */
1064 /* Open the first three file descriptors to /dev/null */
1069 open ("/dev/null", O_RDWR);
1074 struct sigaction sa;
1076 memset (&sa, 0, sizeof (sa));
1077 sa.sa_handler = sig_int_handler;
1078 sigaction (SIGINT, &sa, NULL);
1080 memset (&sa, 0, sizeof (sa));
1081 sa.sa_handler = sig_term_handler;
1082 sigaction (SIGINT, &sa, NULL);
1084 memset (&sa, 0, sizeof (sa));
1085 sa.sa_handler = SIG_IGN;
1086 sigaction (SIGPIPE, &sa, NULL);
1089 openlog ("rrdcached", LOG_PID, LOG_DAEMON);
1091 cache_tree = g_tree_new ((GCompareFunc) strcmp);
1092 if (cache_tree == NULL)
1094 RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed.");
1098 memset (&queue_thread, 0, sizeof (queue_thread));
1099 status = pthread_create (&queue_thread, /* attr = */ NULL,
1100 queue_thread_main, /* args = */ NULL);
1103 RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
1108 } /* }}} int daemonize */
1110 static int cleanup (void) /* {{{ */
1112 RRDD_LOG (LOG_DEBUG, "cleanup ()");
1116 RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
1117 pthread_cond_signal (&cache_cond);
1118 pthread_join (queue_thread, /* return = */ NULL);
1119 RRDD_LOG (LOG_DEBUG, "cleanup: done");
1124 } /* }}} int cleanup */
1126 static int read_options (int argc, char **argv) /* {{{ */
1131 while ((option = getopt(argc, argv, "l:f:w:h?")) != -1)
1139 temp = (char **) realloc (config_listen_address_list,
1140 sizeof (char *) * (config_listen_address_list_len + 1));
1143 fprintf (stderr, "read_options: realloc failed.\n");
1146 config_listen_address_list = temp;
1148 temp[config_listen_address_list_len] = strdup (optarg);
1149 if (temp[config_listen_address_list_len] == NULL)
1151 fprintf (stderr, "read_options: strdup failed.\n");
1154 config_listen_address_list_len++;
1162 temp = atoi (optarg);
1164 config_flush_interval = temp;
1167 fprintf (stderr, "Invalid flush interval: %s\n", optarg);
1177 temp = atoi (optarg);
1179 config_write_interval = temp;
1182 fprintf (stderr, "Invalid write interval: %s\n", optarg);
1190 printf ("RRDd %s Copyright (C) 2008 Florian octo Forster\n"
1192 "Usage: rrdcached [options]\n"
1194 "Valid options are:\n"
1195 " -l <address> Socket address to listen to.\n"
1196 " -w <seconds> Interval in which to write data.\n"
1197 " -f <seconds> Interval in which to flush dead data.\n"
1199 "For more information and a detailed description of all options "
1201 "to the rrdcached(1) manual page.\n",
1205 } /* switch (option) */
1206 } /* while (getopt) */
1209 } /* }}} int read_options */
1211 int main (int argc, char **argv)
1215 status = read_options (argc, argv);
1223 status = daemonize ();
1226 struct sigaction sigchld;
1228 memset (&sigchld, 0, sizeof (sigchld));
1229 sigchld.sa_handler = SIG_IGN;
1230 sigaction (SIGCHLD, &sigchld, NULL);
1234 else if (status != 0)
1236 fprintf (stderr, "daemonize failed, exiting.\n");
1240 listen_thread_main (NULL);
1248 * vim: set sw=2 sts=2 ts=8 et fdm=marker :