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;
168 static void sig_int_handler (int s __attribute__((unused))) /* {{{ */
171 } /* }}} void sig_int_handler */
173 static void sig_term_handler (int s __attribute__((unused))) /* {{{ */
176 } /* }}} void sig_term_handler */
178 static int write_pidfile (void) /* {{{ */
186 file = (config_pid_file != NULL)
188 : LOCALSTATEDIR "/run/rrdcached.pid";
190 fh = fopen (file, "w");
193 RRDD_LOG (LOG_ERR, "write_pidfile: Opening `%s' failed.", file);
197 fprintf (fh, "%i\n", (int) pid);
201 } /* }}} int write_pidfile */
203 static int remove_pidfile (void) /* {{{ */
208 file = (config_pid_file != NULL)
210 : LOCALSTATEDIR "/run/rrdcached.pid";
212 status = unlink (file);
216 } /* }}} int remove_pidfile */
219 * enqueue_cache_item:
220 * `cache_lock' must be acquired before calling this function!
222 static int enqueue_cache_item (cache_item_t *ci, /* {{{ */
225 RRDD_LOG (LOG_DEBUG, "enqueue_cache_item: Adding %s to the update queue.",
231 if (ci->values_num == 0)
236 if ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
238 assert (ci->next == NULL);
239 ci->next = cache_queue_head;
240 cache_queue_head = ci;
242 if (cache_queue_tail == NULL)
243 cache_queue_tail = cache_queue_head;
245 else if (cache_queue_head == ci)
249 else /* enqueued, but not first entry */
253 /* find previous entry */
254 for (prev = cache_queue_head; prev != NULL; prev = prev->next)
255 if (prev->next == ci)
257 assert (prev != NULL);
259 /* move to the front */
260 prev->next = ci->next;
261 ci->next = cache_queue_head;
262 cache_queue_head = ci;
264 /* check if we need to adapt the tail */
265 if (cache_queue_tail == ci)
266 cache_queue_tail = prev;
269 else /* (side == TAIL) */
271 /* We don't move values back in the list.. */
272 if ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
275 assert (ci->next == NULL);
277 if (cache_queue_tail == NULL)
278 cache_queue_head = ci;
280 cache_queue_tail->next = ci;
281 cache_queue_tail = ci;
284 ci->flags |= CI_FLAGS_IN_QUEUE;
287 } /* }}} int enqueue_cache_item */
290 * tree_callback_flush:
291 * Called via `g_tree_foreach' in `queue_thread_main'. `cache_lock' is held
292 * while this is in progress.
294 static gboolean tree_callback_flush (gpointer key, gpointer value, /* {{{ */
298 callback_flush_data_t *cfd;
300 ci = (cache_item_t *) value;
301 cfd = (callback_flush_data_t *) data;
303 if (((cfd->now - ci->last_flush_time) >= config_write_interval)
304 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
305 && (ci->values_num > 0))
307 enqueue_cache_item (ci, TAIL);
309 else if (((cfd->now - ci->last_flush_time) >= config_flush_interval)
310 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
311 && (ci->values_num <= 0))
315 temp = (char **) realloc (cfd->keys,
316 sizeof (char *) * (cfd->keys_num + 1));
319 RRDD_LOG (LOG_ERR, "tree_callback_flush: realloc failed.");
323 /* Make really sure this points to the _same_ place */
324 assert ((char *) key == ci->file);
325 cfd->keys[cfd->keys_num] = (char *) key;
330 } /* }}} gboolean tree_callback_flush */
332 static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
335 struct timespec next_flush;
337 gettimeofday (&now, NULL);
338 next_flush.tv_sec = now.tv_sec + config_flush_interval;
339 next_flush.tv_nsec = 1000 * now.tv_usec;
341 pthread_mutex_lock (&cache_lock);
342 while ((do_shutdown == 0) || (cache_queue_head != NULL))
351 /* First, check if it's time to do the cache flush. */
352 gettimeofday (&now, NULL);
353 if ((now.tv_sec > next_flush.tv_sec)
354 || ((now.tv_sec == next_flush.tv_sec)
355 && ((1000 * now.tv_usec) > next_flush.tv_nsec)))
357 callback_flush_data_t cfd;
360 memset (&cfd, 0, sizeof (cfd));
361 /* Pass the current time as user data so that we don't need to call
362 * `time' for each node. */
363 cfd.now = time (NULL);
367 /* `tree_callback_flush' will return the keys of all values that haven't
368 * been touched in the last `config_flush_interval' seconds in `cfd'.
369 * The char*'s in this array point to the same memory as ci->file, so we
370 * don't need to free them separately. */
371 g_tree_foreach (cache_tree, tree_callback_flush, (gpointer) &cfd);
373 for (k = 0; k < cfd.keys_num; k++)
375 /* This must not fail. */
376 ci = (cache_item_t *) g_tree_lookup (cache_tree, cfd.keys[k]);
379 /* If we end up here with values available, something's seriously
381 assert (ci->values_num == 0);
383 /* Remove the node from the tree */
384 g_tree_remove (cache_tree, cfd.keys[k]);
387 /* Now free and clean up `ci'. */
392 } /* for (k = 0; k < cfd.keys_num; k++) */
394 if (cfd.keys != NULL)
400 /* Determine the time of the next cache flush. */
401 while (next_flush.tv_sec < now.tv_sec)
402 next_flush.tv_sec += config_flush_interval;
405 /* Now, check if there's something to store away. If not, wait until
406 * something comes in or it's time to do the cache flush. */
407 if (cache_queue_head == NULL)
409 status = pthread_cond_timedwait (&cache_cond, &cache_lock, &next_flush);
410 if ((status != 0) && (status != ETIMEDOUT))
412 RRDD_LOG (LOG_ERR, "queue_thread_main: "
413 "pthread_cond_timedwait returned %i.", status);
417 /* Check if a value has arrived. This may be NULL if we timed out or there
418 * was an interrupt such as a signal. */
419 if (cache_queue_head == NULL)
422 ci = cache_queue_head;
424 /* copy the relevant parts */
425 file = strdup (ci->file);
428 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
433 values_num = ci->values_num;
438 ci->last_flush_time = time (NULL);
439 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
441 cache_queue_head = ci->next;
442 if (cache_queue_head == NULL)
443 cache_queue_tail = NULL;
446 pthread_mutex_unlock (&cache_lock);
448 RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
449 file, values_num, (void *) values);
451 status = rrd_update_r (file, NULL, values_num, (void *) values);
454 RRDD_LOG (LOG_ERR, "queue_thread_main: "
455 "rrd_update_r failed with status %i.",
460 for (i = 0; i < values_num; i++)
463 pthread_mutex_lock (&cache_lock);
464 pthread_cond_broadcast (&flush_cond);
465 } /* while (do_shutdown == 0) */
466 pthread_mutex_unlock (&cache_lock);
468 RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
471 } /* }}} void *queue_thread_main */
473 static int buffer_get_field (char **buffer_ret, /* {{{ */
474 size_t *buffer_size_ret, char **field_ret)
483 buffer = *buffer_ret;
485 buffer_size = *buffer_size_ret;
489 /* This is ensured by `handle_request'. */
490 assert (buffer[buffer_size - 1] == ' ');
493 while (buffer_pos < buffer_size)
495 /* Check for end-of-field or end-of-buffer */
496 if (buffer[buffer_pos] == ' ')
498 field[field_size] = 0;
504 /* Handle escaped characters. */
505 else if (buffer[buffer_pos] == '\\')
507 if (buffer_pos >= (buffer_size - 1))
510 field[field_size] = buffer[buffer_pos];
514 /* Normal operation */
517 field[field_size] = buffer[buffer_pos];
521 } /* while (buffer_pos < buffer_size) */
526 *buffer_ret = buffer + buffer_pos;
527 *buffer_size_ret = buffer_size - buffer_pos;
531 } /* }}} int buffer_get_field */
533 static int flush_file (const char *filename) /* {{{ */
537 pthread_mutex_lock (&cache_lock);
539 ci = (cache_item_t *) g_tree_lookup (cache_tree, filename);
542 pthread_mutex_unlock (&cache_lock);
546 /* Enqueue at head */
547 enqueue_cache_item (ci, HEAD);
548 pthread_cond_signal (&cache_cond);
550 while ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
554 pthread_cond_wait (&flush_cond, &cache_lock);
556 ci = g_tree_lookup (cache_tree, filename);
559 RRDD_LOG (LOG_ERR, "flush_file: Tree node went away "
560 "while waiting for flush.");
561 pthread_mutex_unlock (&cache_lock);
566 pthread_mutex_unlock (&cache_lock);
568 } /* }}} int flush_file */
570 static int handle_request_flush (int fd, /* {{{ */
571 char *buffer, size_t buffer_size)
577 status = buffer_get_field (&buffer, &buffer_size, &file);
580 RRDD_LOG (LOG_INFO, "handle_request_flush: Cannot get file name.");
584 status = flush_file (file);
586 snprintf (result, sizeof (result), "0 Successfully flushed %s.\n", file);
587 else if (status == ENOENT)
588 snprintf (result, sizeof (result), "-1 No such file: %s.\n", file);
590 strncpy (result, "-1 Internal error.\n", sizeof (result));
592 snprintf (result, sizeof (result), "-1 Failed with status %i.\n", status);
593 result[sizeof (result) - 1] = 0;
595 status = write (fd, result, strlen (result));
599 RRDD_LOG (LOG_INFO, "handle_request_flush: write(2) returned an error.");
604 } /* }}} int handle_request_flush */
606 static int handle_request_update (int fd, /* {{{ */
607 char *buffer, size_t buffer_size)
620 status = buffer_get_field (&buffer, &buffer_size, &file);
623 RRDD_LOG (LOG_INFO, "handle_request_update: Cannot get file name.");
627 pthread_mutex_lock (&cache_lock);
629 ci = g_tree_lookup (cache_tree, file);
630 if (ci == NULL) /* {{{ */
632 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
635 pthread_mutex_unlock (&cache_lock);
636 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
639 memset (ci, 0, sizeof (cache_item_t));
641 ci->file = strdup (file);
642 if (ci->file == NULL)
644 pthread_mutex_unlock (&cache_lock);
645 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
652 ci->last_flush_time = now;
653 ci->flags = CI_FLAGS_IN_TREE;
655 g_tree_insert (cache_tree, (void *) ci->file, (void *) ci);
657 RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new tree node %s.",
662 while (buffer_size > 0)
667 status = buffer_get_field (&buffer, &buffer_size, &value);
670 RRDD_LOG (LOG_INFO, "handle_request_update: Error reading field.");
674 temp = (char **) realloc (ci->values,
675 sizeof (char *) * (ci->values_num + 1));
678 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
683 ci->values[ci->values_num] = strdup (value);
684 if (ci->values[ci->values_num] == NULL)
686 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
694 if (((now - ci->last_flush_time) >= config_write_interval)
695 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
696 && (ci->values_num > 0))
698 enqueue_cache_item (ci, TAIL);
699 pthread_cond_signal (&cache_cond);
702 pthread_mutex_unlock (&cache_lock);
704 snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
705 answer[sizeof (answer) - 1] = 0;
707 status = write (fd, answer, strlen (answer));
711 RRDD_LOG (LOG_INFO, "handle_request_update: write(2) returned an error.");
716 } /* }}} int handle_request_update */
718 static int handle_request (int fd) /* {{{ */
726 status = read (fd, buffer, sizeof (buffer));
733 RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
736 buffer_size = status;
737 assert (((size_t) buffer_size) <= sizeof (buffer));
739 if (buffer[buffer_size - 1] != '\n')
741 RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
745 /* Accept Windows style line endings, too */
746 if ((buffer_size > 2) && (buffer[buffer_size - 2] == '\r'))
749 buffer[buffer_size - 1] = '\n';
752 /* Place the normal field separator at the end to simplify
753 * `buffer_get_field's work. */
754 buffer[buffer_size - 1] = ' ';
758 status = buffer_get_field (&buffer_ptr, &buffer_size, &command);
761 RRDD_LOG (LOG_INFO, "handle_request: Unable parse command.");
765 if (strcmp (command, "update") == 0)
767 return (handle_request_update (fd, buffer_ptr, buffer_size));
769 else if (strcmp (command, "flush") == 0)
771 return (handle_request_flush (fd, buffer_ptr, buffer_size));
775 RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
778 } /* }}} int handle_request */
780 static void *connection_thread_main (void *args /* {{{ */
781 __attribute__((unused)))
787 fd = *((int *) args);
789 pthread_mutex_lock (&connetion_threads_lock);
793 temp = (pthread_t *) realloc (connetion_threads,
794 sizeof (pthread_t) * (connetion_threads_num + 1));
797 RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
801 connetion_threads = temp;
802 connetion_threads[connetion_threads_num] = pthread_self ();
803 connetion_threads_num++;
806 pthread_mutex_unlock (&connetion_threads_lock);
808 while (do_shutdown == 0)
810 struct pollfd pollfd;
814 pollfd.events = POLLIN | POLLPRI;
817 status = poll (&pollfd, 1, /* timeout = */ 500);
818 if (status == 0) /* timeout */
820 else if (status < 0) /* error */
825 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
829 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
834 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
836 RRDD_LOG (LOG_WARNING, "connection_thread_main: "
837 "poll(2) returned something unexpected: %#04hx",
843 status = handle_request (fd);
851 self = pthread_self ();
852 /* Remove this thread from the connection threads list */
853 pthread_mutex_lock (&connetion_threads_lock);
854 /* Find out own index in the array */
855 for (i = 0; i < connetion_threads_num; i++)
856 if (pthread_equal (connetion_threads[i], self) != 0)
858 assert (i < connetion_threads_num);
860 /* Move the trailing threads forward. */
861 if (i < (connetion_threads_num - 1))
863 memmove (connetion_threads + i,
864 connetion_threads + i + 1,
865 sizeof (pthread_t) * (connetion_threads_num - i - 1));
868 connetion_threads_num--;
869 pthread_mutex_unlock (&connetion_threads_lock);
873 } /* }}} void *connection_thread_main */
875 static int open_listen_socket_unix (const char *path) /* {{{ */
878 struct sockaddr_un sa;
879 listen_socket_t *temp;
882 temp = (listen_socket_t *) realloc (listen_fds,
883 sizeof (listen_fds[0]) * (listen_fds_num + 1));
886 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: realloc failed.");
890 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
892 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
895 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: socket(2) failed.");
899 memset (&sa, 0, sizeof (sa));
900 sa.sun_family = AF_UNIX;
901 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
903 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
906 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: bind(2) failed.");
912 status = listen (fd, /* backlog = */ 10);
915 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: listen(2) failed.");
921 listen_fds[listen_fds_num].fd = fd;
922 snprintf (listen_fds[listen_fds_num].path,
923 sizeof (listen_fds[listen_fds_num].path) - 1,
928 } /* }}} int open_listen_socket_unix */
930 static int open_listen_socket (const char *addr) /* {{{ */
932 struct addrinfo ai_hints;
933 struct addrinfo *ai_res;
934 struct addrinfo *ai_ptr;
937 assert (addr != NULL);
939 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
940 return (open_listen_socket_unix (addr + strlen ("unix:")));
941 else if (addr[0] == '/')
942 return (open_listen_socket_unix (addr));
944 memset (&ai_hints, 0, sizeof (ai_hints));
945 ai_hints.ai_flags = 0;
947 ai_hints.ai_flags |= AI_ADDRCONFIG;
949 ai_hints.ai_family = AF_UNSPEC;
950 ai_hints.ai_socktype = SOCK_STREAM;
953 status = getaddrinfo (addr, RRDCACHED_DEFAULT_PORT, &ai_hints, &ai_res);
956 RRDD_LOG (LOG_ERR, "open_listen_socket: getaddrinfo(%s) failed: "
957 "%s", addr, gai_strerror (status));
961 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
964 listen_socket_t *temp;
966 temp = (listen_socket_t *) realloc (listen_fds,
967 sizeof (listen_fds[0]) * (listen_fds_num + 1));
970 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
974 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
976 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
979 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
983 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
986 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
991 status = listen (fd, /* backlog = */ 10);
994 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
999 listen_fds[listen_fds_num].fd = fd;
1000 strncpy (listen_fds[listen_fds_num].path, addr,
1001 sizeof (listen_fds[listen_fds_num].path) - 1);
1003 } /* for (ai_ptr) */
1006 } /* }}} int open_listen_socket */
1008 static int close_listen_sockets (void) /* {{{ */
1012 for (i = 0; i < listen_fds_num; i++)
1014 close (listen_fds[i].fd);
1015 if (strncmp ("unix:", listen_fds[i].path, strlen ("unix:")) == 0)
1016 unlink (listen_fds[i].path + strlen ("unix:"));
1024 } /* }}} int close_listen_sockets */
1026 static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
1028 struct pollfd *pollfds;
1033 for (i = 0; i < config_listen_address_list_len; i++)
1035 RRDD_LOG (LOG_DEBUG, "listen_thread_main: config_listen_address_list[%i] "
1036 "= %s", i, config_listen_address_list[i]);
1037 open_listen_socket (config_listen_address_list[i]);
1040 if (config_listen_address_list_len < 1)
1041 open_listen_socket (RRDCACHED_DEFAULT_ADDRESS);
1043 if (listen_fds_num < 1)
1045 RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
1046 "could be opened. Sorry.");
1050 pollfds_num = listen_fds_num;
1051 pollfds = (struct pollfd *) malloc (sizeof (*pollfds) * pollfds_num);
1052 if (pollfds == NULL)
1054 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
1057 memset (pollfds, 0, sizeof (*pollfds) * pollfds_num);
1059 while (do_shutdown == 0)
1061 assert (pollfds_num == ((int) listen_fds_num));
1062 for (i = 0; i < pollfds_num; i++)
1064 pollfds[i].fd = listen_fds[i].fd;
1065 pollfds[i].events = POLLIN | POLLPRI;
1066 pollfds[i].revents = 0;
1069 status = poll (pollfds, pollfds_num, /* timeout = */ -1);
1073 if (status != EINTR)
1075 RRDD_LOG (LOG_ERR, "listen_thread_main: poll(2) failed.");
1080 for (i = 0; i < pollfds_num; i++)
1083 struct sockaddr_storage client_sa;
1084 socklen_t client_sa_size;
1087 if (pollfds[i].revents == 0)
1090 if ((pollfds[i].revents & (POLLIN | POLLPRI)) == 0)
1092 RRDD_LOG (LOG_ERR, "listen_thread_main: "
1093 "poll(2) returned something unexpected for listen FD #%i.",
1098 client_sd = (int *) malloc (sizeof (int));
1099 if (client_sd == NULL)
1101 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
1105 client_sa_size = sizeof (client_sa);
1106 *client_sd = accept (pollfds[i].fd,
1107 (struct sockaddr *) &client_sa, &client_sa_size);
1110 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
1114 status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
1115 /* args = */ (void *) client_sd);
1118 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
1123 } /* for (pollfds_num) */
1124 } /* while (do_shutdown == 0) */
1126 close_listen_sockets ();
1128 pthread_mutex_lock (&connetion_threads_lock);
1129 while (connetion_threads_num > 0)
1133 wait_for = connetion_threads[0];
1135 pthread_mutex_unlock (&connetion_threads_lock);
1136 pthread_join (wait_for, /* retval = */ NULL);
1137 pthread_mutex_lock (&connetion_threads_lock);
1139 pthread_mutex_unlock (&connetion_threads_lock);
1141 RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
1144 } /* }}} void *listen_thread_main */
1146 static int daemonize (void) /* {{{ */
1152 /* These structures are static, because `sigaction' behaves weird if the are
1154 static struct sigaction sa_int;
1155 static struct sigaction sa_term;
1156 static struct sigaction sa_pipe;
1161 fprintf (stderr, "daemonize: fork(2) failed.\n");
1169 /* Change into the /tmp directory. */
1170 base_dir = (config_base_dir != NULL)
1173 status = chdir (base_dir);
1176 fprintf (stderr, "daemonize: chdir (%s) failed.\n", base_dir);
1180 /* Become session leader */
1183 /* Open the first three file descriptors to /dev/null */
1188 open ("/dev/null", O_RDWR);
1192 /* Install signal handlers */
1193 memset (&sa_int, 0, sizeof (sa_int));
1194 sa_int.sa_handler = sig_int_handler;
1195 sigaction (SIGINT, &sa_int, NULL);
1197 memset (&sa_term, 0, sizeof (sa_term));
1198 sa_term.sa_handler = sig_term_handler;
1199 sigaction (SIGINT, &sa_term, NULL);
1201 memset (&sa_pipe, 0, sizeof (sa_pipe));
1202 sa_pipe.sa_handler = SIG_IGN;
1203 sigaction (SIGPIPE, &sa_pipe, NULL);
1205 openlog ("rrdcached", LOG_PID, LOG_DAEMON);
1207 cache_tree = g_tree_new ((GCompareFunc) strcmp);
1208 if (cache_tree == NULL)
1210 RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed.");
1214 memset (&queue_thread, 0, sizeof (queue_thread));
1215 status = pthread_create (&queue_thread, /* attr = */ NULL,
1216 queue_thread_main, /* args = */ NULL);
1219 RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
1226 } /* }}} int daemonize */
1228 static int cleanup (void) /* {{{ */
1230 RRDD_LOG (LOG_DEBUG, "cleanup ()");
1234 RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
1235 pthread_cond_signal (&cache_cond);
1236 pthread_join (queue_thread, /* return = */ NULL);
1237 RRDD_LOG (LOG_DEBUG, "cleanup: done");
1244 } /* }}} int cleanup */
1246 static int read_options (int argc, char **argv) /* {{{ */
1251 while ((option = getopt(argc, argv, "l:f:w:b:p:h?")) != -1)
1259 temp = (char **) realloc (config_listen_address_list,
1260 sizeof (char *) * (config_listen_address_list_len + 1));
1263 fprintf (stderr, "read_options: realloc failed.\n");
1266 config_listen_address_list = temp;
1268 temp[config_listen_address_list_len] = strdup (optarg);
1269 if (temp[config_listen_address_list_len] == NULL)
1271 fprintf (stderr, "read_options: strdup failed.\n");
1274 config_listen_address_list_len++;
1282 temp = atoi (optarg);
1284 config_flush_interval = temp;
1287 fprintf (stderr, "Invalid flush interval: %s\n", optarg);
1297 temp = atoi (optarg);
1299 config_write_interval = temp;
1302 fprintf (stderr, "Invalid write interval: %s\n", optarg);
1312 if (config_base_dir != NULL)
1313 free (config_base_dir);
1314 config_base_dir = strdup (optarg);
1315 if (config_base_dir == NULL)
1317 fprintf (stderr, "read_options: strdup failed.\n");
1321 len = strlen (config_base_dir);
1322 while ((len > 0) && (config_base_dir[len - 1] == '/'))
1324 config_base_dir[len - 1] = 0;
1330 fprintf (stderr, "Invalid base directory: %s\n", optarg);
1338 if (config_pid_file != NULL)
1339 free (config_pid_file);
1340 config_pid_file = strdup (optarg);
1341 if (config_pid_file == NULL)
1343 fprintf (stderr, "read_options: strdup failed.\n");
1351 printf ("RRDd %s Copyright (C) 2008 Florian octo Forster\n"
1353 "Usage: rrdcached [options]\n"
1355 "Valid options are:\n"
1356 " -l <address> Socket address to listen to.\n"
1357 " -w <seconds> Interval in which to write data.\n"
1358 " -f <seconds> Interval in which to flush dead data.\n"
1359 " -p <file> Location of the PID-file.\n"
1360 " -b <dir> Base directory to change to.\n"
1362 "For more information and a detailed description of all options "
1364 "to the rrdcached(1) manual page.\n",
1368 } /* switch (option) */
1369 } /* while (getopt) */
1372 } /* }}} int read_options */
1374 int main (int argc, char **argv)
1378 status = read_options (argc, argv);
1386 status = daemonize ();
1389 struct sigaction sigchld;
1391 memset (&sigchld, 0, sizeof (sigchld));
1392 sigchld.sa_handler = SIG_IGN;
1393 sigaction (SIGCHLD, &sigchld, NULL);
1397 else if (status != 0)
1399 fprintf (stderr, "daemonize failed, exiting.\n");
1403 listen_thread_main (NULL);
1411 * vim: set sw=2 sts=2 ts=8 et fdm=marker :