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;
151 static char *config_pid_file = NULL;
152 static char *config_base_dir = NULL;
154 static char **config_listen_address_list = NULL;
155 static int config_listen_address_list_len = 0;
160 static void sig_int_handler (int s __attribute__((unused))) /* {{{ */
163 } /* }}} void sig_int_handler */
165 static void sig_term_handler (int s __attribute__((unused))) /* {{{ */
168 } /* }}} void sig_term_handler */
170 static int write_pidfile (void) /* {{{ */
178 file = (config_pid_file != NULL)
180 : LOCALSTATEDIR "/run/rrdcached.pid";
182 fh = fopen (file, "w");
185 RRDD_LOG (LOG_ERR, "write_pidfile: Opening `%s' failed.", file);
189 fprintf (fh, "%i\n", (int) pid);
193 } /* }}} int write_pidfile */
195 static int remove_pidfile (void) /* {{{ */
200 file = (config_pid_file != NULL)
202 : LOCALSTATEDIR "/run/rrdcached.pid";
204 status = unlink (file);
208 } /* }}} int remove_pidfile */
211 * enqueue_cache_item:
212 * `cache_lock' must be acquired before calling this function!
214 static int enqueue_cache_item (cache_item_t *ci, /* {{{ */
217 RRDD_LOG (LOG_DEBUG, "enqueue_cache_item: Adding %s to the update queue.",
223 if (ci->values_num == 0)
228 if ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
230 assert (ci->next == NULL);
231 ci->next = cache_queue_head;
232 cache_queue_head = ci;
234 if (cache_queue_tail == NULL)
235 cache_queue_tail = cache_queue_head;
237 else if (cache_queue_head == ci)
241 else /* enqueued, but not first entry */
245 /* find previous entry */
246 for (prev = cache_queue_head; prev != NULL; prev = prev->next)
247 if (prev->next == ci)
249 assert (prev != NULL);
251 /* move to the front */
252 prev->next = ci->next;
253 ci->next = cache_queue_head;
254 cache_queue_head = ci;
256 /* check if we need to adapt the tail */
257 if (cache_queue_tail == ci)
258 cache_queue_tail = prev;
261 else /* (side == TAIL) */
263 /* We don't move values back in the list.. */
264 if ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
267 assert (ci->next == NULL);
269 if (cache_queue_tail == NULL)
270 cache_queue_head = ci;
272 cache_queue_tail->next = ci;
273 cache_queue_tail = ci;
276 ci->flags |= CI_FLAGS_IN_QUEUE;
279 } /* }}} int enqueue_cache_item */
282 * tree_callback_flush:
283 * Called via `g_tree_foreach' in `queue_thread_main'. `cache_lock' is held
284 * while this is in progress.
286 static gboolean tree_callback_flush (gpointer key /* {{{ */
287 __attribute__((unused)), gpointer value, gpointer data)
292 key = NULL; /* make compiler happy */
294 ci = (cache_item_t *) value;
295 now = *((time_t *) data);
297 if (((now - ci->last_flush_time) >= config_write_interval)
298 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
299 && (ci->values_num > 0))
300 enqueue_cache_item (ci, TAIL);
303 } /* }}} gboolean tree_callback_flush */
305 static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
308 struct timespec next_flush;
310 gettimeofday (&now, NULL);
311 next_flush.tv_sec = now.tv_sec + config_flush_interval;
312 next_flush.tv_nsec = 1000 * now.tv_usec;
314 pthread_mutex_lock (&cache_lock);
315 while ((do_shutdown == 0) || (cache_queue_head != NULL))
324 /* First, check if it's time to do the cache flush. */
325 gettimeofday (&now, NULL);
326 if ((now.tv_sec > next_flush.tv_sec)
327 || ((now.tv_sec == next_flush.tv_sec)
328 && ((1000 * now.tv_usec) > next_flush.tv_nsec)))
332 /* Pass the current time as user data so that we don't need to call
333 * `time' for each node. */
334 time_now = time (NULL);
336 g_tree_foreach (cache_tree, tree_callback_flush, (gpointer) &time_now);
338 /* Determine the time of the next cache flush. */
339 while (next_flush.tv_sec < now.tv_sec)
340 next_flush.tv_sec += config_flush_interval;
343 /* Now, check if there's something to store away. If not, wait until
344 * something comes in or it's time to do the cache flush. */
345 if (cache_queue_head == NULL)
347 status = pthread_cond_timedwait (&cache_cond, &cache_lock, &next_flush);
348 if ((status != 0) && (status != ETIMEDOUT))
350 RRDD_LOG (LOG_ERR, "queue_thread_main: "
351 "pthread_cond_timedwait returned %i.", status);
355 /* Check if a value has arrived. This may be NULL if we timed out or there
356 * was an interrupt such as a signal. */
357 if (cache_queue_head == NULL)
360 ci = cache_queue_head;
362 /* copy the relevant parts */
363 file = strdup (ci->file);
366 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
371 values_num = ci->values_num;
376 ci->last_flush_time = time (NULL);
377 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
379 cache_queue_head = ci->next;
380 if (cache_queue_head == NULL)
381 cache_queue_tail = NULL;
384 pthread_mutex_unlock (&cache_lock);
386 RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
387 file, values_num, (void *) values);
389 status = rrd_update_r (file, NULL, values_num, (void *) values);
392 RRDD_LOG (LOG_ERR, "queue_thread_main: "
393 "rrd_update_r failed with status %i.",
398 for (i = 0; i < values_num; i++)
401 pthread_mutex_lock (&cache_lock);
402 pthread_cond_broadcast (&flush_cond);
403 } /* while (do_shutdown == 0) */
404 pthread_mutex_unlock (&cache_lock);
406 RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
409 } /* }}} void *queue_thread_main */
411 static int buffer_get_field (char **buffer_ret, /* {{{ */
412 size_t *buffer_size_ret, char **field_ret)
421 buffer = *buffer_ret;
423 buffer_size = *buffer_size_ret;
427 /* This is ensured by `handle_request'. */
428 assert (buffer[buffer_size - 1] == ' ');
431 while (buffer_pos < buffer_size)
433 /* Check for end-of-field or end-of-buffer */
434 if (buffer[buffer_pos] == ' ')
436 field[field_size] = 0;
442 /* Handle escaped characters. */
443 else if (buffer[buffer_pos] == '\\')
445 if (buffer_pos >= (buffer_size - 1))
448 field[field_size] = buffer[buffer_pos];
452 /* Normal operation */
455 field[field_size] = buffer[buffer_pos];
459 } /* while (buffer_pos < buffer_size) */
464 *buffer_ret = buffer + buffer_pos;
465 *buffer_size_ret = buffer_size - buffer_pos;
469 } /* }}} int buffer_get_field */
471 static int flush_file (const char *filename) /* {{{ */
475 pthread_mutex_lock (&cache_lock);
477 ci = g_tree_lookup (cache_tree, filename);
480 pthread_mutex_unlock (&cache_lock);
484 /* Enqueue at head */
485 enqueue_cache_item (ci, HEAD);
486 pthread_cond_signal (&cache_cond);
488 while ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
492 pthread_cond_wait (&flush_cond, &cache_lock);
494 ci = g_tree_lookup (cache_tree, filename);
497 RRDD_LOG (LOG_ERR, "flush_file: Tree node went away "
498 "while waiting for flush.");
499 pthread_mutex_unlock (&cache_lock);
504 pthread_mutex_unlock (&cache_lock);
506 } /* }}} int flush_file */
508 static int handle_request_flush (int fd, /* {{{ */
509 char *buffer, size_t buffer_size)
515 status = buffer_get_field (&buffer, &buffer_size, &file);
518 RRDD_LOG (LOG_INFO, "handle_request_flush: Cannot get file name.");
522 status = flush_file (file);
524 snprintf (result, sizeof (result), "0 Successfully flushed %s.\n", file);
525 else if (status == ENOENT)
526 snprintf (result, sizeof (result), "-1 No such file: %s.\n", file);
528 strncpy (result, "-1 Internal error.\n", sizeof (result));
530 snprintf (result, sizeof (result), "-1 Failed with status %i.\n", status);
531 result[sizeof (result) - 1] = 0;
533 status = write (fd, result, strlen (result));
537 RRDD_LOG (LOG_INFO, "handle_request_flush: write(2) returned an error.");
542 } /* }}} int handle_request_flush */
544 static int handle_request_update (int fd, /* {{{ */
545 char *buffer, size_t buffer_size)
558 status = buffer_get_field (&buffer, &buffer_size, &file);
561 RRDD_LOG (LOG_INFO, "handle_request_update: Cannot get file name.");
565 pthread_mutex_lock (&cache_lock);
567 ci = g_tree_lookup (cache_tree, file);
568 if (ci == NULL) /* {{{ */
570 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
573 pthread_mutex_unlock (&cache_lock);
574 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
577 memset (ci, 0, sizeof (cache_item_t));
579 ci->file = strdup (file);
580 if (ci->file == NULL)
582 pthread_mutex_unlock (&cache_lock);
583 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
590 ci->last_flush_time = now;
591 ci->flags = CI_FLAGS_IN_TREE;
593 g_tree_insert (cache_tree, (void *) ci->file, (void *) ci);
595 RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new tree node %s.",
600 while (buffer_size > 0)
605 status = buffer_get_field (&buffer, &buffer_size, &value);
608 RRDD_LOG (LOG_INFO, "handle_request_update: Error reading field.");
612 temp = (char **) realloc (ci->values,
613 sizeof (char *) * (ci->values_num + 1));
616 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
621 ci->values[ci->values_num] = strdup (value);
622 if (ci->values[ci->values_num] == NULL)
624 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
632 if (((now - ci->last_flush_time) >= config_write_interval)
633 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
634 && (ci->values_num > 0))
636 enqueue_cache_item (ci, TAIL);
637 pthread_cond_signal (&cache_cond);
640 pthread_mutex_unlock (&cache_lock);
642 snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
643 answer[sizeof (answer) - 1] = 0;
645 status = write (fd, answer, strlen (answer));
649 RRDD_LOG (LOG_INFO, "handle_request_update: write(2) returned an error.");
654 } /* }}} int handle_request_update */
656 static int handle_request (int fd) /* {{{ */
664 status = read (fd, buffer, sizeof (buffer));
671 RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
674 buffer_size = status;
675 assert (((size_t) buffer_size) <= sizeof (buffer));
677 if (buffer[buffer_size - 1] != '\n')
679 RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
683 /* Accept Windows style line endings, too */
684 if ((buffer_size > 2) && (buffer[buffer_size - 2] == '\r'))
687 buffer[buffer_size - 1] = '\n';
690 /* Place the normal field separator at the end to simplify
691 * `buffer_get_field's work. */
692 buffer[buffer_size - 1] = ' ';
696 status = buffer_get_field (&buffer_ptr, &buffer_size, &command);
699 RRDD_LOG (LOG_INFO, "handle_request: Unable parse command.");
703 if (strcmp (command, "update") == 0)
705 return (handle_request_update (fd, buffer_ptr, buffer_size));
707 else if (strcmp (command, "flush") == 0)
709 return (handle_request_flush (fd, buffer_ptr, buffer_size));
713 RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
716 } /* }}} int handle_request */
718 static void *connection_thread_main (void *args /* {{{ */
719 __attribute__((unused)))
725 fd = *((int *) args);
727 pthread_mutex_lock (&connetion_threads_lock);
731 temp = (pthread_t *) realloc (connetion_threads,
732 sizeof (pthread_t) * (connetion_threads_num + 1));
735 RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
739 connetion_threads = temp;
740 connetion_threads[connetion_threads_num] = pthread_self ();
741 connetion_threads_num++;
744 pthread_mutex_unlock (&connetion_threads_lock);
746 while (do_shutdown == 0)
748 struct pollfd pollfd;
752 pollfd.events = POLLIN | POLLPRI;
755 status = poll (&pollfd, 1, /* timeout = */ 500);
756 if (status == 0) /* timeout */
758 else if (status < 0) /* error */
763 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
767 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
772 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
774 RRDD_LOG (LOG_WARNING, "connection_thread_main: "
775 "poll(2) returned something unexpected: %#04hx",
781 status = handle_request (fd);
789 self = pthread_self ();
790 /* Remove this thread from the connection threads list */
791 pthread_mutex_lock (&connetion_threads_lock);
792 /* Find out own index in the array */
793 for (i = 0; i < connetion_threads_num; i++)
794 if (pthread_equal (connetion_threads[i], self) != 0)
796 assert (i < connetion_threads_num);
798 /* Move the trailing threads forward. */
799 if (i < (connetion_threads_num - 1))
801 memmove (connetion_threads + i,
802 connetion_threads + i + 1,
803 sizeof (pthread_t) * (connetion_threads_num - i - 1));
806 connetion_threads_num--;
807 pthread_mutex_unlock (&connetion_threads_lock);
811 } /* }}} void *connection_thread_main */
813 static int open_listen_socket_unix (const char *path) /* {{{ */
816 struct sockaddr_un sa;
817 listen_socket_t *temp;
820 temp = (listen_socket_t *) realloc (listen_fds,
821 sizeof (listen_fds[0]) * (listen_fds_num + 1));
824 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: realloc failed.");
828 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
830 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
833 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: socket(2) failed.");
837 memset (&sa, 0, sizeof (sa));
838 sa.sun_family = AF_UNIX;
839 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
841 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
844 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: bind(2) failed.");
850 status = listen (fd, /* backlog = */ 10);
853 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: listen(2) failed.");
859 listen_fds[listen_fds_num].fd = fd;
860 snprintf (listen_fds[listen_fds_num].path,
861 sizeof (listen_fds[listen_fds_num].path) - 1,
866 } /* }}} int open_listen_socket_unix */
868 static int open_listen_socket (const char *addr) /* {{{ */
870 struct addrinfo ai_hints;
871 struct addrinfo *ai_res;
872 struct addrinfo *ai_ptr;
875 assert (addr != NULL);
877 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
878 return (open_listen_socket_unix (addr + strlen ("unix:")));
879 else if (addr[0] == '/')
880 return (open_listen_socket_unix (addr));
882 memset (&ai_hints, 0, sizeof (ai_hints));
883 ai_hints.ai_flags = 0;
885 ai_hints.ai_flags |= AI_ADDRCONFIG;
887 ai_hints.ai_family = AF_UNSPEC;
888 ai_hints.ai_socktype = SOCK_STREAM;
891 status = getaddrinfo (addr, RRDCACHED_DEFAULT_PORT, &ai_hints, &ai_res);
894 RRDD_LOG (LOG_ERR, "open_listen_socket: getaddrinfo(%s) failed: "
895 "%s", addr, gai_strerror (status));
899 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
902 listen_socket_t *temp;
904 temp = (listen_socket_t *) realloc (listen_fds,
905 sizeof (listen_fds[0]) * (listen_fds_num + 1));
908 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
912 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
914 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
917 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
921 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
924 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
929 status = listen (fd, /* backlog = */ 10);
932 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
937 listen_fds[listen_fds_num].fd = fd;
938 strncpy (listen_fds[listen_fds_num].path, addr,
939 sizeof (listen_fds[listen_fds_num].path) - 1);
944 } /* }}} int open_listen_socket */
946 static int close_listen_sockets (void) /* {{{ */
950 for (i = 0; i < listen_fds_num; i++)
952 close (listen_fds[i].fd);
953 if (strncmp ("unix:", listen_fds[i].path, strlen ("unix:")) == 0)
954 unlink (listen_fds[i].path + strlen ("unix:"));
962 } /* }}} int close_listen_sockets */
964 static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
966 struct pollfd *pollfds;
971 for (i = 0; i < config_listen_address_list_len; i++)
973 RRDD_LOG (LOG_DEBUG, "listen_thread_main: config_listen_address_list[%i] "
974 "= %s", i, config_listen_address_list[i]);
975 open_listen_socket (config_listen_address_list[i]);
978 if (config_listen_address_list_len < 1)
979 open_listen_socket (RRDCACHED_DEFAULT_ADDRESS);
981 if (listen_fds_num < 1)
983 RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
984 "could be opened. Sorry.");
988 pollfds_num = listen_fds_num;
989 pollfds = (struct pollfd *) malloc (sizeof (*pollfds) * pollfds_num);
992 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
995 memset (pollfds, 0, sizeof (*pollfds) * pollfds_num);
997 while (do_shutdown == 0)
999 assert (pollfds_num == ((int) listen_fds_num));
1000 for (i = 0; i < pollfds_num; i++)
1002 pollfds[i].fd = listen_fds[i].fd;
1003 pollfds[i].events = POLLIN | POLLPRI;
1004 pollfds[i].revents = 0;
1007 status = poll (pollfds, pollfds_num, /* timeout = */ -1);
1011 if (status != EINTR)
1013 RRDD_LOG (LOG_ERR, "listen_thread_main: poll(2) failed.");
1018 for (i = 0; i < pollfds_num; i++)
1021 struct sockaddr_storage client_sa;
1022 socklen_t client_sa_size;
1025 if (pollfds[i].revents == 0)
1028 if ((pollfds[i].revents & (POLLIN | POLLPRI)) == 0)
1030 RRDD_LOG (LOG_ERR, "listen_thread_main: "
1031 "poll(2) returned something unexpected for listen FD #%i.",
1036 client_sd = (int *) malloc (sizeof (int));
1037 if (client_sd == NULL)
1039 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
1043 client_sa_size = sizeof (client_sa);
1044 *client_sd = accept (pollfds[i].fd,
1045 (struct sockaddr *) &client_sa, &client_sa_size);
1048 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
1052 status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
1053 /* args = */ (void *) client_sd);
1056 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
1061 } /* for (pollfds_num) */
1062 } /* while (do_shutdown == 0) */
1064 close_listen_sockets ();
1066 pthread_mutex_lock (&connetion_threads_lock);
1067 while (connetion_threads_num > 0)
1071 wait_for = connetion_threads[0];
1073 pthread_mutex_unlock (&connetion_threads_lock);
1074 pthread_join (wait_for, /* retval = */ NULL);
1075 pthread_mutex_lock (&connetion_threads_lock);
1077 pthread_mutex_unlock (&connetion_threads_lock);
1079 RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
1082 } /* }}} void *listen_thread_main */
1084 static int daemonize (void) /* {{{ */
1090 /* These structures are static, because `sigaction' behaves weird if the are
1092 static struct sigaction sa_int;
1093 static struct sigaction sa_term;
1094 static struct sigaction sa_pipe;
1099 fprintf (stderr, "daemonize: fork(2) failed.\n");
1107 /* Change into the /tmp directory. */
1108 base_dir = (config_base_dir != NULL)
1111 status = chdir (base_dir);
1114 fprintf (stderr, "daemonize: chdir (%s) failed.\n", base_dir);
1118 /* Become session leader */
1121 /* Open the first three file descriptors to /dev/null */
1126 open ("/dev/null", O_RDWR);
1130 /* Install signal handlers */
1131 memset (&sa_int, 0, sizeof (sa_int));
1132 sa_int.sa_handler = sig_int_handler;
1133 sigaction (SIGINT, &sa_int, NULL);
1135 memset (&sa_term, 0, sizeof (sa_term));
1136 sa_term.sa_handler = sig_term_handler;
1137 sigaction (SIGINT, &sa_term, NULL);
1139 memset (&sa_pipe, 0, sizeof (sa_pipe));
1140 sa_pipe.sa_handler = SIG_IGN;
1141 sigaction (SIGPIPE, &sa_pipe, NULL);
1143 openlog ("rrdcached", LOG_PID, LOG_DAEMON);
1145 cache_tree = g_tree_new ((GCompareFunc) strcmp);
1146 if (cache_tree == NULL)
1148 RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed.");
1152 memset (&queue_thread, 0, sizeof (queue_thread));
1153 status = pthread_create (&queue_thread, /* attr = */ NULL,
1154 queue_thread_main, /* args = */ NULL);
1157 RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
1164 } /* }}} int daemonize */
1166 static int cleanup (void) /* {{{ */
1168 RRDD_LOG (LOG_DEBUG, "cleanup ()");
1172 RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
1173 pthread_cond_signal (&cache_cond);
1174 pthread_join (queue_thread, /* return = */ NULL);
1175 RRDD_LOG (LOG_DEBUG, "cleanup: done");
1182 } /* }}} int cleanup */
1184 static int read_options (int argc, char **argv) /* {{{ */
1189 while ((option = getopt(argc, argv, "l:f:w:b:p:h?")) != -1)
1197 temp = (char **) realloc (config_listen_address_list,
1198 sizeof (char *) * (config_listen_address_list_len + 1));
1201 fprintf (stderr, "read_options: realloc failed.\n");
1204 config_listen_address_list = temp;
1206 temp[config_listen_address_list_len] = strdup (optarg);
1207 if (temp[config_listen_address_list_len] == NULL)
1209 fprintf (stderr, "read_options: strdup failed.\n");
1212 config_listen_address_list_len++;
1220 temp = atoi (optarg);
1222 config_flush_interval = temp;
1225 fprintf (stderr, "Invalid flush interval: %s\n", optarg);
1235 temp = atoi (optarg);
1237 config_write_interval = temp;
1240 fprintf (stderr, "Invalid write interval: %s\n", optarg);
1250 if (config_base_dir != NULL)
1251 free (config_base_dir);
1252 config_base_dir = strdup (optarg);
1253 if (config_base_dir == NULL)
1255 fprintf (stderr, "read_options: strdup failed.\n");
1259 len = strlen (config_base_dir);
1260 while ((len > 0) && (config_base_dir[len - 1] == '/'))
1262 config_base_dir[len - 1] = 0;
1268 fprintf (stderr, "Invalid base directory: %s\n", optarg);
1276 if (config_pid_file != NULL)
1277 free (config_pid_file);
1278 config_pid_file = strdup (optarg);
1279 if (config_pid_file == NULL)
1281 fprintf (stderr, "read_options: strdup failed.\n");
1289 printf ("RRDd %s Copyright (C) 2008 Florian octo Forster\n"
1291 "Usage: rrdcached [options]\n"
1293 "Valid options are:\n"
1294 " -l <address> Socket address to listen to.\n"
1295 " -w <seconds> Interval in which to write data.\n"
1296 " -f <seconds> Interval in which to flush dead data.\n"
1297 " -p <file> Location of the PID-file.\n"
1298 " -b <dir> Base directory to change to.\n"
1300 "For more information and a detailed description of all options "
1302 "to the rrdcached(1) manual page.\n",
1306 } /* switch (option) */
1307 } /* while (getopt) */
1310 } /* }}} int read_options */
1312 int main (int argc, char **argv)
1316 status = read_options (argc, argv);
1324 status = daemonize ();
1327 struct sigaction sigchld;
1329 memset (&sigchld, 0, sizeof (sigchld));
1330 sigchld.sa_handler = SIG_IGN;
1331 sigaction (SIGCHLD, &sigchld, NULL);
1335 else if (status != 0)
1337 fprintf (stderr, "daemonize failed, exiting.\n");
1341 listen_thread_main (NULL);
1349 * vim: set sw=2 sts=2 ts=8 et fdm=marker :