2 * RRDTool - src/rrd_daemon.c
3 * Copyright (C) 2008 Florian octo Forster
4 * Copyright (C) 2008 Kevin Brintnall
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
21 * kevin brintnall <kbrint@rufus.net>
26 * First tell the compiler to stick to the C99 and POSIX standards as close as
29 #ifndef __STRICT_ANSI__ /* {{{ */
30 # define __STRICT_ANSI__
33 #ifndef _ISOC99_SOURCE
34 # define _ISOC99_SOURCE
37 #ifdef _POSIX_C_SOURCE
38 # undef _POSIX_C_SOURCE
40 #define _POSIX_C_SOURCE 200112L
42 /* Single UNIX needed for strdup. */
46 #define _XOPEN_SOURCE 500
63 * Now for some includes..
65 #include "rrd.h" /* {{{ */
66 #include "rrd_client.h"
77 #include <sys/types.h>
81 #include <sys/socket.h>
92 #include <glib-2.0/glib.h>
95 #define RRDD_LOG(severity, ...) syslog ((severity), __VA_ARGS__)
98 # define __attribute__(x) /**/
104 struct listen_socket_s
107 char path[PATH_MAX + 1];
109 typedef struct listen_socket_s listen_socket_t;
112 typedef struct cache_item_s cache_item_t;
118 time_t last_flush_time;
119 #define CI_FLAGS_IN_TREE (1<<0)
120 #define CI_FLAGS_IN_QUEUE (1<<1)
126 struct callback_flush_data_s
133 typedef struct callback_flush_data_s callback_flush_data_t;
140 typedef enum queue_side_e queue_side_t;
142 /* max length of socket command or response */
148 static int stay_foreground = 0;
150 static listen_socket_t *listen_fds = NULL;
151 static size_t listen_fds_num = 0;
153 static int do_shutdown = 0;
155 static pthread_t queue_thread;
157 static pthread_t *connection_threads = NULL;
158 static pthread_mutex_t connection_threads_lock = PTHREAD_MUTEX_INITIALIZER;
159 static int connection_threads_num = 0;
162 static GTree *cache_tree = NULL;
163 static cache_item_t *cache_queue_head = NULL;
164 static cache_item_t *cache_queue_tail = NULL;
165 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
166 static pthread_cond_t cache_cond = PTHREAD_COND_INITIALIZER;
168 static pthread_cond_t flush_cond = PTHREAD_COND_INITIALIZER;
170 static int config_write_interval = 300;
171 static int config_write_jitter = 0;
172 static int config_flush_interval = 3600;
173 static char *config_pid_file = NULL;
174 static char *config_base_dir = NULL;
176 static char **config_listen_address_list = NULL;
177 static int config_listen_address_list_len = 0;
179 static uint64_t stats_queue_length = 0;
180 static uint64_t stats_updates_received = 0;
181 static uint64_t stats_flush_received = 0;
182 static uint64_t stats_updates_written = 0;
183 static uint64_t stats_data_sets_written = 0;
184 static uint64_t stats_journal_bytes = 0;
185 static uint64_t stats_journal_rotate = 0;
186 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
188 /* Journaled updates */
189 static char *journal_cur = NULL;
190 static char *journal_old = NULL;
191 static FILE *journal_fh = NULL;
192 static pthread_mutex_t journal_lock = PTHREAD_MUTEX_INITIALIZER;
193 static int journal_write(char *cmd, char *args);
194 static void journal_done(void);
195 static void journal_rotate(void);
200 static void sig_int_handler (int s __attribute__((unused))) /* {{{ */
202 RRDD_LOG(LOG_NOTICE, "caught SIGINT");
204 pthread_cond_broadcast(&cache_cond);
205 } /* }}} void sig_int_handler */
207 static void sig_term_handler (int s __attribute__((unused))) /* {{{ */
209 RRDD_LOG(LOG_NOTICE, "caught SIGTERM");
211 pthread_cond_broadcast(&cache_cond);
212 } /* }}} void sig_term_handler */
214 static int write_pidfile (void) /* {{{ */
223 file = (config_pid_file != NULL)
225 : LOCALSTATEDIR "/run/rrdcached.pid";
227 fd = open(file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IRGRP|S_IROTH);
230 RRDD_LOG(LOG_ERR, "FATAL: cannot create '%s' (%s)",
231 file, rrd_strerror(errno));
235 fh = fdopen (fd, "w");
238 RRDD_LOG (LOG_ERR, "write_pidfile: Opening `%s' failed.", file);
243 fprintf (fh, "%i\n", (int) pid);
247 } /* }}} int write_pidfile */
249 static int remove_pidfile (void) /* {{{ */
254 file = (config_pid_file != NULL)
256 : LOCALSTATEDIR "/run/rrdcached.pid";
258 status = unlink (file);
262 } /* }}} int remove_pidfile */
264 static ssize_t sread (int fd, void *buffer_void, size_t buffer_size) /* {{{ */
271 buffer = (char *) buffer_void;
273 buffer_free = buffer_size;
275 while (buffer_free > 0)
277 status = read (fd, buffer + buffer_used, buffer_free);
278 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
287 assert ((0 > status) || (buffer_free >= (size_t) status));
289 buffer_free = buffer_free - status;
290 buffer_used = buffer_used + status;
292 if (buffer[buffer_used - 1] == '\n')
296 assert (buffer_used > 0);
298 if (buffer[buffer_used - 1] != '\n')
304 buffer[buffer_used - 1] = 0;
306 /* Fix network line endings. */
307 if ((buffer_used > 1) && (buffer[buffer_used - 2] == '\r'))
310 buffer[buffer_used - 1] = 0;
313 return (buffer_used);
314 } /* }}} ssize_t sread */
316 static ssize_t swrite (int fd, const void *buf, size_t count) /* {{{ */
322 /* special case for journal replay */
323 if (fd < 0) return 0;
325 ptr = (const char *) buf;
330 status = write (fd, (const void *) ptr, nleft);
332 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
343 } /* }}} ssize_t swrite */
345 static void _wipe_ci_values(cache_item_t *ci, time_t when)
350 ci->last_flush_time = when;
351 if (config_write_jitter > 0)
352 ci->last_flush_time += (random() % config_write_jitter);
354 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
358 * enqueue_cache_item:
359 * `cache_lock' must be acquired before calling this function!
361 static int enqueue_cache_item (cache_item_t *ci, /* {{{ */
369 if (ci->values_num == 0)
374 if ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
376 assert (ci->next == NULL);
377 ci->next = cache_queue_head;
378 cache_queue_head = ci;
380 if (cache_queue_tail == NULL)
381 cache_queue_tail = cache_queue_head;
385 else if (cache_queue_head == ci)
389 else /* enqueued, but not first entry */
393 /* find previous entry */
394 for (prev = cache_queue_head; prev != NULL; prev = prev->next)
395 if (prev->next == ci)
397 assert (prev != NULL);
399 /* move to the front */
400 prev->next = ci->next;
401 ci->next = cache_queue_head;
402 cache_queue_head = ci;
404 /* check if we need to adapt the tail */
405 if (cache_queue_tail == ci)
406 cache_queue_tail = prev;
409 else /* (side == TAIL) */
411 /* We don't move values back in the list.. */
412 if ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
415 assert (ci->next == NULL);
417 if (cache_queue_tail == NULL)
418 cache_queue_head = ci;
420 cache_queue_tail->next = ci;
421 cache_queue_tail = ci;
426 ci->flags |= CI_FLAGS_IN_QUEUE;
430 pthread_mutex_lock (&stats_lock);
431 stats_queue_length++;
432 pthread_mutex_unlock (&stats_lock);
436 } /* }}} int enqueue_cache_item */
439 * tree_callback_flush:
440 * Called via `g_tree_foreach' in `queue_thread_main'. `cache_lock' is held
441 * while this is in progress.
443 static gboolean tree_callback_flush (gpointer key, gpointer value, /* {{{ */
447 callback_flush_data_t *cfd;
449 ci = (cache_item_t *) value;
450 cfd = (callback_flush_data_t *) data;
452 if ((ci->last_flush_time <= cfd->abs_timeout)
453 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
454 && (ci->values_num > 0))
456 enqueue_cache_item (ci, TAIL);
458 else if ((do_shutdown != 0)
459 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
460 && (ci->values_num > 0))
462 enqueue_cache_item (ci, TAIL);
464 else if (((cfd->now - ci->last_flush_time) >= config_flush_interval)
465 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
466 && (ci->values_num <= 0))
470 temp = (char **) realloc (cfd->keys,
471 sizeof (char *) * (cfd->keys_num + 1));
474 RRDD_LOG (LOG_ERR, "tree_callback_flush: realloc failed.");
478 /* Make really sure this points to the _same_ place */
479 assert ((char *) key == ci->file);
480 cfd->keys[cfd->keys_num] = (char *) key;
485 } /* }}} gboolean tree_callback_flush */
487 static int flush_old_values (int max_age)
489 callback_flush_data_t cfd;
492 memset (&cfd, 0, sizeof (cfd));
493 /* Pass the current time as user data so that we don't need to call
494 * `time' for each node. */
495 cfd.now = time (NULL);
500 cfd.abs_timeout = cfd.now - max_age;
502 cfd.abs_timeout = cfd.now + 1;
504 /* `tree_callback_flush' will return the keys of all values that haven't
505 * been touched in the last `config_flush_interval' seconds in `cfd'.
506 * The char*'s in this array point to the same memory as ci->file, so we
507 * don't need to free them separately. */
508 g_tree_foreach (cache_tree, tree_callback_flush, (gpointer) &cfd);
510 for (k = 0; k < cfd.keys_num; k++)
514 /* This must not fail. */
515 ci = (cache_item_t *) g_tree_lookup (cache_tree, cfd.keys[k]);
518 /* If we end up here with values available, something's seriously
520 assert (ci->values_num == 0);
522 /* Remove the node from the tree */
523 g_tree_remove (cache_tree, cfd.keys[k]);
526 /* Now free and clean up `ci'. */
531 } /* for (k = 0; k < cfd.keys_num; k++) */
533 if (cfd.keys != NULL)
540 } /* int flush_old_values */
542 static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
545 struct timespec next_flush;
547 gettimeofday (&now, NULL);
548 next_flush.tv_sec = now.tv_sec + config_flush_interval;
549 next_flush.tv_nsec = 1000 * now.tv_usec;
551 pthread_mutex_lock (&cache_lock);
552 while ((do_shutdown == 0) || (cache_queue_head != NULL))
561 /* First, check if it's time to do the cache flush. */
562 gettimeofday (&now, NULL);
563 if ((now.tv_sec > next_flush.tv_sec)
564 || ((now.tv_sec == next_flush.tv_sec)
565 && ((1000 * now.tv_usec) > next_flush.tv_nsec)))
567 /* Flush all values that haven't been written in the last
568 * `config_write_interval' seconds. */
569 flush_old_values (config_write_interval);
571 /* Determine the time of the next cache flush. */
572 while (next_flush.tv_sec <= now.tv_sec)
573 next_flush.tv_sec += config_flush_interval;
575 /* unlock the cache while we rotate so we don't block incoming
576 * updates if the fsync() blocks on disk I/O */
577 pthread_mutex_unlock(&cache_lock);
579 pthread_mutex_lock(&cache_lock);
582 /* Now, check if there's something to store away. If not, wait until
583 * something comes in or it's time to do the cache flush. */
584 if (cache_queue_head == NULL)
586 status = pthread_cond_timedwait (&cache_cond, &cache_lock, &next_flush);
587 if ((status != 0) && (status != ETIMEDOUT))
589 RRDD_LOG (LOG_ERR, "queue_thread_main: "
590 "pthread_cond_timedwait returned %i.", status);
594 /* We're about to shut down, so lets flush the entire tree. */
595 if ((do_shutdown != 0) && (cache_queue_head == NULL))
596 flush_old_values (/* max age = */ -1);
598 /* Check if a value has arrived. This may be NULL if we timed out or there
599 * was an interrupt such as a signal. */
600 if (cache_queue_head == NULL)
603 ci = cache_queue_head;
605 /* copy the relevant parts */
606 file = strdup (ci->file);
609 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
613 assert(ci->values != NULL);
614 assert(ci->values_num > 0);
617 values_num = ci->values_num;
619 _wipe_ci_values(ci, time(NULL));
621 cache_queue_head = ci->next;
622 if (cache_queue_head == NULL)
623 cache_queue_tail = NULL;
626 pthread_mutex_lock (&stats_lock);
627 assert (stats_queue_length > 0);
628 stats_queue_length--;
629 pthread_mutex_unlock (&stats_lock);
631 pthread_mutex_unlock (&cache_lock);
634 status = rrd_update_r (file, NULL, values_num, (void *) values);
637 RRDD_LOG (LOG_NOTICE, "queue_thread_main: "
638 "rrd_update_r (%s) failed with status %i. (%s)",
639 file, status, rrd_get_error());
642 journal_write("wrote", file);
644 for (i = 0; i < values_num; i++)
652 pthread_mutex_lock (&stats_lock);
653 stats_updates_written++;
654 stats_data_sets_written += values_num;
655 pthread_mutex_unlock (&stats_lock);
658 pthread_mutex_lock (&cache_lock);
659 pthread_cond_broadcast (&flush_cond);
661 /* We're about to shut down, so lets flush the entire tree. */
662 if ((do_shutdown != 0) && (cache_queue_head == NULL))
663 flush_old_values (/* max age = */ -1);
664 } /* while ((do_shutdown == 0) || (cache_queue_head != NULL)) */
665 pthread_mutex_unlock (&cache_lock);
667 assert(cache_queue_head == NULL);
668 RRDD_LOG(LOG_INFO, "clean shutdown; all RRDs flushed");
672 } /* }}} void *queue_thread_main */
674 static int buffer_get_field (char **buffer_ret, /* {{{ */
675 size_t *buffer_size_ret, char **field_ret)
684 buffer = *buffer_ret;
686 buffer_size = *buffer_size_ret;
690 if (buffer_size <= 0)
693 /* This is ensured by `handle_request'. */
694 assert (buffer[buffer_size - 1] == '\0');
697 while (buffer_pos < buffer_size)
699 /* Check for end-of-field or end-of-buffer */
700 if (buffer[buffer_pos] == ' ' || buffer[buffer_pos] == '\0')
702 field[field_size] = 0;
708 /* Handle escaped characters. */
709 else if (buffer[buffer_pos] == '\\')
711 if (buffer_pos >= (buffer_size - 1))
714 field[field_size] = buffer[buffer_pos];
718 /* Normal operation */
721 field[field_size] = buffer[buffer_pos];
725 } /* while (buffer_pos < buffer_size) */
730 *buffer_ret = buffer + buffer_pos;
731 *buffer_size_ret = buffer_size - buffer_pos;
735 } /* }}} int buffer_get_field */
737 static int flush_file (const char *filename) /* {{{ */
741 pthread_mutex_lock (&cache_lock);
743 ci = (cache_item_t *) g_tree_lookup (cache_tree, filename);
746 pthread_mutex_unlock (&cache_lock);
750 /* Enqueue at head */
751 enqueue_cache_item (ci, HEAD);
752 pthread_cond_signal (&cache_cond);
754 while ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
758 pthread_cond_wait (&flush_cond, &cache_lock);
760 ci = g_tree_lookup (cache_tree, filename);
763 RRDD_LOG (LOG_ERR, "flush_file: Tree node went away "
764 "while waiting for flush.");
765 pthread_mutex_unlock (&cache_lock);
770 pthread_mutex_unlock (&cache_lock);
772 } /* }}} int flush_file */
774 static int handle_request_help (int fd, /* {{{ */
775 char *buffer, size_t buffer_size)
779 size_t help_text_len;
785 "4 Command overview\n",
786 "FLUSH <filename>\n",
787 "HELP [<command>]\n",
788 "UPDATE <filename> <values> [<values> ...]\n",
791 size_t help_help_len = sizeof (help_help) / sizeof (help_help[0]);
795 "4 Help for FLUSH\n",
796 "Usage: FLUSH <filename>\n",
798 "Adds the given filename to the head of the update queue and returns\n",
799 "after is has been dequeued.\n"
801 size_t help_flush_len = sizeof (help_flush) / sizeof (help_flush[0]);
803 char *help_update[] =
805 "9 Help for UPDATE\n",
806 "Usage: UPDATE <filename> <values> [<values> ...]\n"
808 "Adds the given file to the internal cache if it is not yet known and\n",
809 "appends the given value(s) to the entry. See the rrdcached(1) manpage\n",
812 "Each <values> has the following form:\n",
813 " <values> = <time>:<value>[:<value>[...]]\n",
814 "See the rrdupdate(1) manpage for details.\n"
816 size_t help_update_len = sizeof (help_update) / sizeof (help_update[0]);
820 "4 Help for STATS\n",
823 "Returns some performance counters, see the rrdcached(1) manpage for\n",
824 "a description of the values.\n"
826 size_t help_stats_len = sizeof (help_stats) / sizeof (help_stats[0]);
828 status = buffer_get_field (&buffer, &buffer_size, &command);
831 help_text = help_help;
832 help_text_len = help_help_len;
836 if (strcasecmp (command, "update") == 0)
838 help_text = help_update;
839 help_text_len = help_update_len;
841 else if (strcasecmp (command, "flush") == 0)
843 help_text = help_flush;
844 help_text_len = help_flush_len;
846 else if (strcasecmp (command, "stats") == 0)
848 help_text = help_stats;
849 help_text_len = help_stats_len;
853 help_text = help_help;
854 help_text_len = help_help_len;
858 for (i = 0; i < help_text_len; i++)
860 status = swrite (fd, help_text[i], strlen (help_text[i]));
864 RRDD_LOG (LOG_ERR, "handle_request_help: swrite returned an error.");
870 } /* }}} int handle_request_help */
872 static int handle_request_stats (int fd, /* {{{ */
873 char *buffer __attribute__((unused)),
874 size_t buffer_size __attribute__((unused)))
877 char outbuf[CMD_MAX];
879 uint64_t copy_queue_length;
880 uint64_t copy_updates_received;
881 uint64_t copy_flush_received;
882 uint64_t copy_updates_written;
883 uint64_t copy_data_sets_written;
884 uint64_t copy_journal_bytes;
885 uint64_t copy_journal_rotate;
887 uint64_t tree_nodes_number;
890 pthread_mutex_lock (&stats_lock);
891 copy_queue_length = stats_queue_length;
892 copy_updates_received = stats_updates_received;
893 copy_flush_received = stats_flush_received;
894 copy_updates_written = stats_updates_written;
895 copy_data_sets_written = stats_data_sets_written;
896 copy_journal_bytes = stats_journal_bytes;
897 copy_journal_rotate = stats_journal_rotate;
898 pthread_mutex_unlock (&stats_lock);
900 pthread_mutex_lock (&cache_lock);
901 tree_nodes_number = (uint64_t) g_tree_nnodes (cache_tree);
902 tree_depth = (uint64_t) g_tree_height (cache_tree);
903 pthread_mutex_unlock (&cache_lock);
905 #define RRDD_STATS_SEND \
906 outbuf[sizeof (outbuf) - 1] = 0; \
907 status = swrite (fd, outbuf, strlen (outbuf)); \
911 RRDD_LOG (LOG_INFO, "handle_request_stats: swrite returned an error."); \
915 strncpy (outbuf, "9 Statistics follow\n", sizeof (outbuf));
918 snprintf (outbuf, sizeof (outbuf),
919 "QueueLength: %"PRIu64"\n", copy_queue_length);
922 snprintf (outbuf, sizeof (outbuf),
923 "UpdatesReceived: %"PRIu64"\n", copy_updates_received);
926 snprintf (outbuf, sizeof (outbuf),
927 "FlushesReceived: %"PRIu64"\n", copy_flush_received);
930 snprintf (outbuf, sizeof (outbuf),
931 "UpdatesWritten: %"PRIu64"\n", copy_updates_written);
934 snprintf (outbuf, sizeof (outbuf),
935 "DataSetsWritten: %"PRIu64"\n", copy_data_sets_written);
938 snprintf (outbuf, sizeof (outbuf),
939 "TreeNodesNumber: %"PRIu64"\n", tree_nodes_number);
942 snprintf (outbuf, sizeof (outbuf),
943 "TreeDepth: %"PRIu64"\n", tree_depth);
946 snprintf (outbuf, sizeof(outbuf),
947 "JournalBytes: %"PRIu64"\n", copy_journal_bytes);
950 snprintf (outbuf, sizeof(outbuf),
951 "JournalRotate: %"PRIu64"\n", copy_journal_rotate);
955 #undef RRDD_STATS_SEND
956 } /* }}} int handle_request_stats */
958 static int handle_request_flush (int fd, /* {{{ */
959 char *buffer, size_t buffer_size)
963 char result[CMD_MAX];
965 status = buffer_get_field (&buffer, &buffer_size, &file);
968 strncpy (result, "-1 Usage: flush <filename>\n", sizeof (result));
972 pthread_mutex_lock(&stats_lock);
973 stats_flush_received++;
974 pthread_mutex_unlock(&stats_lock);
976 status = flush_file (file);
978 snprintf (result, sizeof (result), "0 Successfully flushed %s.\n", file);
979 else if (status == ENOENT)
981 /* no file in our tree; see whether it exists at all */
984 memset(&statbuf, 0, sizeof(statbuf));
985 if (stat(file, &statbuf) == 0 && S_ISREG(statbuf.st_mode))
986 snprintf (result, sizeof (result), "0 Nothing to flush: %s.\n", file);
988 snprintf (result, sizeof (result), "-1 No such file: %s.\n", file);
991 strncpy (result, "-1 Internal error.\n", sizeof (result));
993 snprintf (result, sizeof (result), "-1 Failed with status %i.\n", status);
995 result[sizeof (result) - 1] = 0;
997 status = swrite (fd, result, strlen (result));
1001 RRDD_LOG (LOG_INFO, "handle_request_flush: swrite returned an error.");
1006 } /* }}} int handle_request_flush */
1008 static int handle_request_update (int fd, /* {{{ */
1009 char *buffer, size_t buffer_size)
1018 char answer[CMD_MAX];
1020 #define RRDD_UPDATE_SEND \
1021 answer[sizeof (answer) - 1] = 0; \
1022 status = swrite (fd, answer, strlen (answer)); \
1026 RRDD_LOG (LOG_INFO, "handle_request_update: swrite returned an error."); \
1032 status = buffer_get_field (&buffer, &buffer_size, &file);
1035 strncpy (answer, "-1 Usage: UPDATE <filename> <values> [<values> ...]\n",
1041 pthread_mutex_lock(&stats_lock);
1042 stats_updates_received++;
1043 pthread_mutex_unlock(&stats_lock);
1045 pthread_mutex_lock (&cache_lock);
1047 ci = g_tree_lookup (cache_tree, file);
1048 if (ci == NULL) /* {{{ */
1050 struct stat statbuf;
1052 memset (&statbuf, 0, sizeof (statbuf));
1053 status = stat (file, &statbuf);
1056 pthread_mutex_unlock (&cache_lock);
1057 RRDD_LOG (LOG_NOTICE, "handle_request_update: stat (%s) failed.", file);
1060 if (status == ENOENT)
1061 snprintf (answer, sizeof (answer), "-1 No such file: %s\n", file);
1063 snprintf (answer, sizeof (answer), "-1 stat failed with error %i.\n",
1068 if (!S_ISREG (statbuf.st_mode))
1070 pthread_mutex_unlock (&cache_lock);
1072 snprintf (answer, sizeof (answer), "-1 Not a regular file: %s\n", file);
1076 if (access(file, R_OK|W_OK) != 0)
1078 pthread_mutex_unlock (&cache_lock);
1080 snprintf (answer, sizeof (answer), "-1 Cannot read/write %s: %s\n",
1081 file, rrd_strerror(errno));
1086 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
1089 pthread_mutex_unlock (&cache_lock);
1090 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
1092 strncpy (answer, "-1 malloc failed.\n", sizeof (answer));
1096 memset (ci, 0, sizeof (cache_item_t));
1098 ci->file = strdup (file);
1099 if (ci->file == NULL)
1101 pthread_mutex_unlock (&cache_lock);
1103 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
1105 strncpy (answer, "-1 strdup failed.\n", sizeof (answer));
1110 _wipe_ci_values(ci, now);
1111 ci->flags = CI_FLAGS_IN_TREE;
1113 g_tree_insert (cache_tree, (void *) ci->file, (void *) ci);
1115 assert (ci != NULL);
1117 while (buffer_size > 0)
1122 status = buffer_get_field (&buffer, &buffer_size, &value);
1125 RRDD_LOG (LOG_INFO, "handle_request_update: Error reading field.");
1129 temp = (char **) realloc (ci->values,
1130 sizeof (char *) * (ci->values_num + 1));
1133 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
1138 ci->values[ci->values_num] = strdup (value);
1139 if (ci->values[ci->values_num] == NULL)
1141 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
1149 if (((now - ci->last_flush_time) >= config_write_interval)
1150 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
1151 && (ci->values_num > 0))
1153 enqueue_cache_item (ci, TAIL);
1154 pthread_cond_signal (&cache_cond);
1157 pthread_mutex_unlock (&cache_lock);
1161 strncpy (answer, "-1 No values updated.\n", sizeof (answer));
1165 snprintf (answer, sizeof (answer), "0 Enqueued %i value%s\n", values_num,
1166 (values_num == 1) ? "" : "s");
1170 #undef RRDD_UPDATE_SEND
1171 } /* }}} int handle_request_update */
1173 /* we came across a "WROTE" entry during journal replay.
1174 * throw away any values that we have accumulated for this file
1176 static int handle_request_wrote (int fd __attribute__((unused)), /* {{{ */
1178 size_t buffer_size __attribute__((unused)))
1182 const char *file = buffer;
1184 pthread_mutex_lock(&cache_lock);
1186 ci = g_tree_lookup(cache_tree, file);
1189 pthread_mutex_unlock(&cache_lock);
1195 for (i=0; i < ci->values_num; i++)
1196 free(ci->values[i]);
1201 _wipe_ci_values(ci, time(NULL));
1203 pthread_mutex_unlock(&cache_lock);
1205 } /* }}} int handle_request_wrote */
1207 /* if fd < 0, we are in journal replay mode */
1208 static int handle_request (int fd, char *buffer, size_t buffer_size) /* {{{ */
1214 assert (buffer[buffer_size - 1] == '\0');
1216 buffer_ptr = buffer;
1218 status = buffer_get_field (&buffer_ptr, &buffer_size, &command);
1221 RRDD_LOG (LOG_INFO, "handle_request: Unable parse command.");
1225 if (strcasecmp (command, "update") == 0)
1227 /* don't re-write updates in replay mode */
1229 journal_write(command, buffer_ptr);
1231 return (handle_request_update (fd, buffer_ptr, buffer_size));
1233 else if (strcasecmp (command, "wrote") == 0 && fd < 0)
1235 /* this is only valid in replay mode */
1236 return (handle_request_wrote (fd, buffer_ptr, buffer_size));
1238 else if (strcasecmp (command, "flush") == 0)
1240 return (handle_request_flush (fd, buffer_ptr, buffer_size));
1242 else if (strcasecmp (command, "stats") == 0)
1244 return (handle_request_stats (fd, buffer_ptr, buffer_size));
1246 else if (strcasecmp (command, "help") == 0)
1248 return (handle_request_help (fd, buffer_ptr, buffer_size));
1252 char result[CMD_MAX];
1254 snprintf (result, sizeof (result), "-1 Unknown command: %s\n", command);
1255 result[sizeof (result) - 1] = 0;
1257 status = swrite (fd, result, strlen (result));
1260 RRDD_LOG (LOG_ERR, "handle_request: swrite failed.");
1266 } /* }}} int handle_request */
1268 /* MUST NOT hold journal_lock before calling this */
1269 static void journal_rotate(void) /* {{{ */
1271 FILE *old_fh = NULL;
1273 if (journal_cur == NULL || journal_old == NULL)
1276 pthread_mutex_lock(&journal_lock);
1278 /* we rotate this way (rename before close) so that the we can release
1279 * the journal lock as fast as possible. Journal writes to the new
1280 * journal can proceed immediately after the new file is opened. The
1281 * fclose can then block without affecting new updates.
1283 if (journal_fh != NULL)
1285 old_fh = journal_fh;
1286 rename(journal_cur, journal_old);
1287 ++stats_journal_rotate;
1290 journal_fh = fopen(journal_cur, "a");
1291 pthread_mutex_unlock(&journal_lock);
1296 if (journal_fh == NULL)
1298 "JOURNALING DISABLED: Cannot open journal file '%s' : (%s)",
1299 journal_cur, rrd_strerror(errno));
1301 } /* }}} static void journal_rotate */
1303 static void journal_done(void) /* {{{ */
1305 if (journal_cur == NULL)
1308 pthread_mutex_lock(&journal_lock);
1309 if (journal_fh != NULL)
1315 RRDD_LOG(LOG_INFO, "removing journals");
1317 unlink(journal_old);
1318 unlink(journal_cur);
1319 pthread_mutex_unlock(&journal_lock);
1321 } /* }}} static void journal_done */
1323 static int journal_write(char *cmd, char *args) /* {{{ */
1327 if (journal_fh == NULL)
1330 pthread_mutex_lock(&journal_lock);
1331 chars = fprintf(journal_fh, "%s %s\n", cmd, args);
1332 pthread_mutex_unlock(&journal_lock);
1336 pthread_mutex_lock(&stats_lock);
1337 stats_journal_bytes += chars;
1338 pthread_mutex_unlock(&stats_lock);
1342 } /* }}} static int journal_write */
1344 static int journal_replay (const char *file) /* {{{ */
1350 char entry[CMD_MAX];
1352 if (file == NULL) return 0;
1354 fh = fopen(file, "r");
1357 if (errno != ENOENT)
1358 RRDD_LOG(LOG_ERR, "journal_replay: cannot open journal file: '%s' (%s)",
1359 file, rrd_strerror(errno));
1363 RRDD_LOG(LOG_NOTICE, "replaying from journal: %s", file);
1370 fgets(entry, sizeof(entry), fh);
1371 entry_len = strlen(entry);
1373 /* check \n termination in case journal writing crashed mid-line */
1376 else if (entry[entry_len - 1] != '\n')
1378 RRDD_LOG(LOG_NOTICE, "Malformed journal entry at line %"PRIu64, line);
1383 entry[entry_len - 1] = '\0';
1385 if (handle_request(-1, entry, entry_len) == 0)
1395 RRDD_LOG(LOG_INFO, "Replayed %d entries (%d failures)",
1396 entry_cnt, fail_cnt);
1402 } /* }}} static int journal_replay */
1404 static void *connection_thread_main (void *args) /* {{{ */
1410 fd = *((int *) args);
1413 pthread_mutex_lock (&connection_threads_lock);
1417 temp = (pthread_t *) realloc (connection_threads,
1418 sizeof (pthread_t) * (connection_threads_num + 1));
1421 RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
1425 connection_threads = temp;
1426 connection_threads[connection_threads_num] = pthread_self ();
1427 connection_threads_num++;
1430 pthread_mutex_unlock (&connection_threads_lock);
1432 while (do_shutdown == 0)
1434 char buffer[CMD_MAX];
1436 struct pollfd pollfd;
1440 pollfd.events = POLLIN | POLLPRI;
1443 status = poll (&pollfd, 1, /* timeout = */ 500);
1444 if (status == 0) /* timeout */
1446 else if (status < 0) /* error */
1449 if (status == EINTR)
1451 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
1455 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
1460 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
1462 RRDD_LOG (LOG_WARNING, "connection_thread_main: "
1463 "poll(2) returned something unexpected: %#04hx",
1469 status = (int) sread (fd, buffer, sizeof (buffer));
1475 RRDD_LOG(LOG_ERR, "connection_thread_main: sread failed.");
1480 status = handle_request (fd, buffer, /*buffer_size=*/ status);
1488 self = pthread_self ();
1489 /* Remove this thread from the connection threads list */
1490 pthread_mutex_lock (&connection_threads_lock);
1491 /* Find out own index in the array */
1492 for (i = 0; i < connection_threads_num; i++)
1493 if (pthread_equal (connection_threads[i], self) != 0)
1495 assert (i < connection_threads_num);
1497 /* Move the trailing threads forward. */
1498 if (i < (connection_threads_num - 1))
1500 memmove (connection_threads + i,
1501 connection_threads + i + 1,
1502 sizeof (pthread_t) * (connection_threads_num - i - 1));
1505 connection_threads_num--;
1506 pthread_mutex_unlock (&connection_threads_lock);
1509 } /* }}} void *connection_thread_main */
1511 static int open_listen_socket_unix (const char *path) /* {{{ */
1514 struct sockaddr_un sa;
1515 listen_socket_t *temp;
1518 temp = (listen_socket_t *) realloc (listen_fds,
1519 sizeof (listen_fds[0]) * (listen_fds_num + 1));
1522 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: realloc failed.");
1526 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
1528 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
1531 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: socket(2) failed.");
1535 memset (&sa, 0, sizeof (sa));
1536 sa.sun_family = AF_UNIX;
1537 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
1539 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
1542 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: bind(2) failed.");
1548 status = listen (fd, /* backlog = */ 10);
1551 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: listen(2) failed.");
1557 listen_fds[listen_fds_num].fd = fd;
1558 snprintf (listen_fds[listen_fds_num].path,
1559 sizeof (listen_fds[listen_fds_num].path) - 1,
1564 } /* }}} int open_listen_socket_unix */
1566 static int open_listen_socket (const char *addr_orig) /* {{{ */
1568 struct addrinfo ai_hints;
1569 struct addrinfo *ai_res;
1570 struct addrinfo *ai_ptr;
1571 char addr_copy[NI_MAXHOST];
1576 assert (addr_orig != NULL);
1578 strncpy (addr_copy, addr_orig, sizeof (addr_copy));
1579 addr_copy[sizeof (addr_copy) - 1] = 0;
1582 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
1583 return (open_listen_socket_unix (addr + strlen ("unix:")));
1584 else if (addr[0] == '/')
1585 return (open_listen_socket_unix (addr));
1587 memset (&ai_hints, 0, sizeof (ai_hints));
1588 ai_hints.ai_flags = 0;
1589 #ifdef AI_ADDRCONFIG
1590 ai_hints.ai_flags |= AI_ADDRCONFIG;
1592 ai_hints.ai_family = AF_UNSPEC;
1593 ai_hints.ai_socktype = SOCK_STREAM;
1596 if (*addr == '[') /* IPv6+port format */
1598 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
1601 port = strchr (addr, ']');
1604 RRDD_LOG (LOG_ERR, "open_listen_socket: Malformed address: %s",
1613 else if (*port == 0)
1617 RRDD_LOG (LOG_ERR, "open_listen_socket: Garbage after address: %s",
1621 } /* if (*addr = ']') */
1622 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
1624 port = rindex(addr, ':');
1632 status = getaddrinfo (addr,
1633 port == NULL ? RRDCACHED_DEFAULT_PORT : port,
1634 &ai_hints, &ai_res);
1637 RRDD_LOG (LOG_ERR, "open_listen_socket: getaddrinfo(%s) failed: "
1638 "%s", addr, gai_strerror (status));
1642 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1645 listen_socket_t *temp;
1648 temp = (listen_socket_t *) realloc (listen_fds,
1649 sizeof (listen_fds[0]) * (listen_fds_num + 1));
1652 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
1656 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
1658 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1661 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
1665 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
1667 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1670 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
1675 status = listen (fd, /* backlog = */ 10);
1678 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
1683 listen_fds[listen_fds_num].fd = fd;
1684 strncpy (listen_fds[listen_fds_num].path, addr,
1685 sizeof (listen_fds[listen_fds_num].path) - 1);
1687 } /* for (ai_ptr) */
1690 } /* }}} int open_listen_socket */
1692 static int close_listen_sockets (void) /* {{{ */
1696 for (i = 0; i < listen_fds_num; i++)
1698 close (listen_fds[i].fd);
1699 if (strncmp ("unix:", listen_fds[i].path, strlen ("unix:")) == 0)
1700 unlink (listen_fds[i].path + strlen ("unix:"));
1708 } /* }}} int close_listen_sockets */
1710 static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
1712 struct pollfd *pollfds;
1717 for (i = 0; i < config_listen_address_list_len; i++)
1718 open_listen_socket (config_listen_address_list[i]);
1720 if (config_listen_address_list_len < 1)
1721 open_listen_socket (RRDCACHED_DEFAULT_ADDRESS);
1723 if (listen_fds_num < 1)
1725 RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
1726 "could be opened. Sorry.");
1730 pollfds_num = listen_fds_num;
1731 pollfds = (struct pollfd *) malloc (sizeof (*pollfds) * pollfds_num);
1732 if (pollfds == NULL)
1734 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
1737 memset (pollfds, 0, sizeof (*pollfds) * pollfds_num);
1739 RRDD_LOG(LOG_INFO, "listening for connections");
1741 while (do_shutdown == 0)
1743 assert (pollfds_num == ((int) listen_fds_num));
1744 for (i = 0; i < pollfds_num; i++)
1746 pollfds[i].fd = listen_fds[i].fd;
1747 pollfds[i].events = POLLIN | POLLPRI;
1748 pollfds[i].revents = 0;
1751 status = poll (pollfds, pollfds_num, /* timeout = */ -1);
1755 if (status != EINTR)
1757 RRDD_LOG (LOG_ERR, "listen_thread_main: poll(2) failed.");
1762 for (i = 0; i < pollfds_num; i++)
1765 struct sockaddr_storage client_sa;
1766 socklen_t client_sa_size;
1768 pthread_attr_t attr;
1770 if (pollfds[i].revents == 0)
1773 if ((pollfds[i].revents & (POLLIN | POLLPRI)) == 0)
1775 RRDD_LOG (LOG_ERR, "listen_thread_main: "
1776 "poll(2) returned something unexpected for listen FD #%i.",
1781 client_sd = (int *) malloc (sizeof (int));
1782 if (client_sd == NULL)
1784 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
1788 client_sa_size = sizeof (client_sa);
1789 *client_sd = accept (pollfds[i].fd,
1790 (struct sockaddr *) &client_sa, &client_sa_size);
1793 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
1797 pthread_attr_init (&attr);
1798 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
1800 status = pthread_create (&tid, &attr, connection_thread_main,
1801 /* args = */ (void *) client_sd);
1804 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
1809 } /* for (pollfds_num) */
1810 } /* while (do_shutdown == 0) */
1812 RRDD_LOG(LOG_INFO, "starting shutdown");
1814 close_listen_sockets ();
1816 pthread_mutex_lock (&connection_threads_lock);
1817 while (connection_threads_num > 0)
1821 wait_for = connection_threads[0];
1823 pthread_mutex_unlock (&connection_threads_lock);
1824 pthread_join (wait_for, /* retval = */ NULL);
1825 pthread_mutex_lock (&connection_threads_lock);
1827 pthread_mutex_unlock (&connection_threads_lock);
1830 } /* }}} void *listen_thread_main */
1832 static int daemonize (void) /* {{{ */
1836 /* These structures are static, because `sigaction' behaves weird if the are
1838 static struct sigaction sa_int;
1839 static struct sigaction sa_term;
1840 static struct sigaction sa_pipe;
1842 if (!stay_foreground)
1850 fprintf (stderr, "daemonize: fork(2) failed.\n");
1858 /* Change into the /tmp directory. */
1859 base_dir = (config_base_dir != NULL)
1862 status = chdir (base_dir);
1865 fprintf (stderr, "daemonize: chdir (%s) failed.\n", base_dir);
1869 /* Become session leader */
1872 /* Open the first three file descriptors to /dev/null */
1877 open ("/dev/null", O_RDWR);
1880 } /* if (!stay_foreground) */
1882 /* Install signal handlers */
1883 memset (&sa_int, 0, sizeof (sa_int));
1884 sa_int.sa_handler = sig_int_handler;
1885 sigaction (SIGINT, &sa_int, NULL);
1887 memset (&sa_term, 0, sizeof (sa_term));
1888 sa_term.sa_handler = sig_term_handler;
1889 sigaction (SIGTERM, &sa_term, NULL);
1891 memset (&sa_pipe, 0, sizeof (sa_pipe));
1892 sa_pipe.sa_handler = SIG_IGN;
1893 sigaction (SIGPIPE, &sa_pipe, NULL);
1895 openlog ("rrdcached", LOG_PID, LOG_DAEMON);
1896 RRDD_LOG(LOG_INFO, "starting up");
1898 cache_tree = g_tree_new ((GCompareFunc) strcmp);
1899 if (cache_tree == NULL)
1901 RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed.");
1905 status = write_pidfile ();
1907 } /* }}} int daemonize */
1909 static int cleanup (void) /* {{{ */
1913 pthread_cond_signal (&cache_cond);
1914 pthread_join (queue_thread, /* return = */ NULL);
1918 RRDD_LOG(LOG_INFO, "goodbye");
1922 } /* }}} int cleanup */
1924 static int read_options (int argc, char **argv) /* {{{ */
1929 while ((option = getopt(argc, argv, "gl:f:w:b:z:p:j:h?")) != -1)
1941 temp = (char **) realloc (config_listen_address_list,
1942 sizeof (char *) * (config_listen_address_list_len + 1));
1945 fprintf (stderr, "read_options: realloc failed.\n");
1948 config_listen_address_list = temp;
1950 temp[config_listen_address_list_len] = strdup (optarg);
1951 if (temp[config_listen_address_list_len] == NULL)
1953 fprintf (stderr, "read_options: strdup failed.\n");
1956 config_listen_address_list_len++;
1964 temp = atoi (optarg);
1966 config_flush_interval = temp;
1969 fprintf (stderr, "Invalid flush interval: %s\n", optarg);
1979 temp = atoi (optarg);
1981 config_write_interval = temp;
1984 fprintf (stderr, "Invalid write interval: %s\n", optarg);
1994 temp = atoi(optarg);
1996 config_write_jitter = temp;
1999 fprintf (stderr, "Invalid write jitter: -z %s\n", optarg);
2010 if (config_base_dir != NULL)
2011 free (config_base_dir);
2012 config_base_dir = strdup (optarg);
2013 if (config_base_dir == NULL)
2015 fprintf (stderr, "read_options: strdup failed.\n");
2019 len = strlen (config_base_dir);
2020 while ((len > 0) && (config_base_dir[len - 1] == '/'))
2022 config_base_dir[len - 1] = 0;
2028 fprintf (stderr, "Invalid base directory: %s\n", optarg);
2036 if (config_pid_file != NULL)
2037 free (config_pid_file);
2038 config_pid_file = strdup (optarg);
2039 if (config_pid_file == NULL)
2041 fprintf (stderr, "read_options: strdup failed.\n");
2049 struct stat statbuf;
2050 const char *dir = optarg;
2052 status = stat(dir, &statbuf);
2055 fprintf(stderr, "Cannot stat '%s' : %s\n", dir, rrd_strerror(errno));
2059 if (!S_ISDIR(statbuf.st_mode)
2060 || access(dir, R_OK|W_OK|X_OK) != 0)
2062 fprintf(stderr, "Must specify a writable directory with -j! (%s)\n",
2063 errno ? rrd_strerror(errno) : "");
2067 journal_cur = malloc(PATH_MAX + 1);
2068 journal_old = malloc(PATH_MAX + 1);
2069 if (journal_cur == NULL || journal_old == NULL)
2071 fprintf(stderr, "malloc failure for journal files\n");
2076 snprintf(journal_cur, PATH_MAX, "%s/rrd.journal", dir);
2077 snprintf(journal_old, PATH_MAX, "%s/rrd.journal.old", dir);
2084 printf ("RRDCacheD %s Copyright (C) 2008 Florian octo Forster\n"
2086 "Usage: rrdcached [options]\n"
2088 "Valid options are:\n"
2089 " -l <address> Socket address to listen to.\n"
2090 " -w <seconds> Interval in which to write data.\n"
2091 " -z <delay> Delay writes up to <delay> seconds to spread load\n"
2092 " -f <seconds> Interval in which to flush dead data.\n"
2093 " -p <file> Location of the PID-file.\n"
2094 " -b <dir> Base directory to change to.\n"
2095 " -g Do not fork and run in the foreground.\n"
2096 " -j <dir> Directory in which to create the journal files.\n"
2098 "For more information and a detailed description of all options "
2100 "to the rrdcached(1) manual page.\n",
2104 } /* switch (option) */
2105 } /* while (getopt) */
2107 /* advise the user when values are not sane */
2108 if (config_flush_interval < 2 * config_write_interval)
2109 fprintf(stderr, "WARNING: flush interval (-f) should be at least"
2110 " 2x write interval (-w) !\n");
2111 if (config_write_jitter > config_write_interval)
2112 fprintf(stderr, "WARNING: write delay (-z) should NOT be larger than"
2113 " write interval (-w) !\n");
2116 } /* }}} int read_options */
2118 int main (int argc, char **argv)
2122 status = read_options (argc, argv);
2130 status = daemonize ();
2133 struct sigaction sigchld;
2135 memset (&sigchld, 0, sizeof (sigchld));
2136 sigchld.sa_handler = SIG_IGN;
2137 sigaction (SIGCHLD, &sigchld, NULL);
2141 else if (status != 0)
2143 fprintf (stderr, "daemonize failed, exiting.\n");
2147 if (journal_cur != NULL)
2149 int had_journal = 0;
2151 pthread_mutex_lock(&journal_lock);
2153 RRDD_LOG(LOG_INFO, "checking for journal files");
2155 had_journal += journal_replay(journal_old);
2156 had_journal += journal_replay(journal_cur);
2159 flush_old_values(-1);
2161 pthread_mutex_unlock(&journal_lock);
2164 RRDD_LOG(LOG_INFO, "journal processing complete");
2167 /* start the queue thread */
2168 memset (&queue_thread, 0, sizeof (queue_thread));
2169 status = pthread_create (&queue_thread,
2175 RRDD_LOG (LOG_ERR, "FATAL: cannot create queue thread");
2180 listen_thread_main (NULL);
2187 * vim: set sw=2 sts=2 ts=8 et fdm=marker :