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..
66 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) && !defined(HAVE_CONFIG_H)
67 #include "../win32/config.h"
70 #include "../rrd_config.h"
75 #include "rrd_client.h"
84 # include <sys/socket.h>
92 #include <sys/types.h>
103 #include <sys/time.h>
106 #include <glib-2.0/glib.h>
109 #define RRDD_LOG(severity, ...) syslog ((severity), __VA_ARGS__)
112 # define __attribute__(x) /**/
124 typedef enum { RESP_ERR = -1, RESP_OK = 0 } response_code;
126 struct listen_socket_s
129 char addr[PATH_MAX + 1];
131 socket_privilege privilege;
133 /* state for BATCH processing */
145 typedef struct listen_socket_s listen_socket_t;
148 /* note: guard against "unused" warnings in the handlers */
149 #define DISPATCH_PROTO listen_socket_t *sock __attribute__((unused)),\
150 time_t now __attribute__((unused)),\
151 char *buffer __attribute__((unused)),\
152 size_t buffer_size __attribute__((unused))
154 #define HANDLER_PROTO struct command *cmd __attribute__((unused)),\
159 int (*handler)(HANDLER_PROTO);
160 socket_privilege min_priv;
162 char context; /* where we expect to see it */
163 #define CMD_CONTEXT_CLIENT (1<<0)
164 #define CMD_CONTEXT_BATCH (1<<1)
165 #define CMD_CONTEXT_JOURNAL (1<<2)
166 #define CMD_CONTEXT_ANY (0x7f)
173 typedef struct cache_item_s cache_item_t;
179 time_t last_flush_time;
180 time_t last_update_stamp;
181 #define CI_FLAGS_IN_TREE (1<<0)
182 #define CI_FLAGS_IN_QUEUE (1<<1)
184 pthread_cond_t flushed;
189 struct callback_flush_data_s
196 typedef struct callback_flush_data_s callback_flush_data_t;
203 typedef enum queue_side_e queue_side_t;
205 /* max length of socket command or response */
207 #define RBUF_SIZE (CMD_MAX*2)
212 static int stay_foreground = 0;
213 static uid_t daemon_uid;
215 static listen_socket_t *listen_fds = NULL;
216 static size_t listen_fds_num = 0;
218 static int do_shutdown = 0;
220 static pthread_t *queue_threads;
221 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
222 static int config_queue_threads = 4;
224 static pthread_t flush_thread;
225 static pthread_cond_t flush_cond = PTHREAD_COND_INITIALIZER;
227 static pthread_mutex_t connection_threads_lock = PTHREAD_MUTEX_INITIALIZER;
228 static pthread_cond_t connection_threads_done = PTHREAD_COND_INITIALIZER;
229 static int connection_threads_num = 0;
232 static GTree *cache_tree = NULL;
233 static cache_item_t *cache_queue_head = NULL;
234 static cache_item_t *cache_queue_tail = NULL;
235 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
237 static int config_write_interval = 300;
238 static int config_write_jitter = 0;
239 static int config_flush_interval = 3600;
240 static int config_flush_at_shutdown = 0;
241 static char *config_pid_file = NULL;
242 static char *config_base_dir = NULL;
243 static size_t _config_base_dir_len = 0;
244 static int config_write_base_only = 0;
246 static listen_socket_t **config_listen_address_list = NULL;
247 static size_t config_listen_address_list_len = 0;
249 static uint64_t stats_queue_length = 0;
250 static uint64_t stats_updates_received = 0;
251 static uint64_t stats_flush_received = 0;
252 static uint64_t stats_updates_written = 0;
253 static uint64_t stats_data_sets_written = 0;
254 static uint64_t stats_journal_bytes = 0;
255 static uint64_t stats_journal_rotate = 0;
256 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
258 /* Journaled updates */
259 static char *journal_cur = NULL;
260 static char *journal_old = NULL;
261 static FILE *journal_fh = NULL;
262 static pthread_mutex_t journal_lock = PTHREAD_MUTEX_INITIALIZER;
263 static int journal_write(char *cmd, char *args);
264 static void journal_done(void);
265 static void journal_rotate(void);
267 /* prototypes for forward refernces */
268 static int handle_request_help (HANDLER_PROTO);
273 static void sig_common (const char *sig) /* {{{ */
275 RRDD_LOG(LOG_NOTICE, "caught SIG%s", sig);
277 pthread_cond_broadcast(&flush_cond);
278 pthread_cond_broadcast(&queue_cond);
279 } /* }}} void sig_common */
281 static void sig_int_handler (int s __attribute__((unused))) /* {{{ */
284 } /* }}} void sig_int_handler */
286 static void sig_term_handler (int s __attribute__((unused))) /* {{{ */
289 } /* }}} void sig_term_handler */
291 static void sig_usr1_handler (int s __attribute__((unused))) /* {{{ */
293 config_flush_at_shutdown = 1;
295 } /* }}} void sig_usr1_handler */
297 static void sig_usr2_handler (int s __attribute__((unused))) /* {{{ */
299 config_flush_at_shutdown = 0;
301 } /* }}} void sig_usr2_handler */
303 static void install_signal_handlers(void) /* {{{ */
305 /* These structures are static, because `sigaction' behaves weird if the are
307 static struct sigaction sa_int;
308 static struct sigaction sa_term;
309 static struct sigaction sa_pipe;
310 static struct sigaction sa_usr1;
311 static struct sigaction sa_usr2;
313 /* Install signal handlers */
314 memset (&sa_int, 0, sizeof (sa_int));
315 sa_int.sa_handler = sig_int_handler;
316 sigaction (SIGINT, &sa_int, NULL);
318 memset (&sa_term, 0, sizeof (sa_term));
319 sa_term.sa_handler = sig_term_handler;
320 sigaction (SIGTERM, &sa_term, NULL);
322 memset (&sa_pipe, 0, sizeof (sa_pipe));
323 sa_pipe.sa_handler = SIG_IGN;
324 sigaction (SIGPIPE, &sa_pipe, NULL);
326 memset (&sa_pipe, 0, sizeof (sa_usr1));
327 sa_usr1.sa_handler = sig_usr1_handler;
328 sigaction (SIGUSR1, &sa_usr1, NULL);
330 memset (&sa_usr2, 0, sizeof (sa_usr2));
331 sa_usr2.sa_handler = sig_usr2_handler;
332 sigaction (SIGUSR2, &sa_usr2, NULL);
334 } /* }}} void install_signal_handlers */
336 static int open_pidfile(char *action, int oflag) /* {{{ */
341 file = (config_pid_file != NULL)
343 : LOCALSTATEDIR "/run/rrdcached.pid";
345 fd = open(file, oflag, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
347 fprintf(stderr, "rrdcached: can't %s pid file '%s' (%s)\n",
348 action, file, rrd_strerror(errno));
351 } /* }}} static int open_pidfile */
353 /* check existing pid file to see whether a daemon is running */
354 static int check_pidfile(void)
360 pid_fd = open_pidfile("open", O_RDWR);
364 if (read(pid_fd, pid_str, sizeof(pid_str)) <= 0)
371 /* another running process that we can signal COULD be
372 * a competing rrdcached */
373 if (pid != getpid() && kill(pid, 0) == 0)
376 "FATAL: Another rrdcached daemon is running?? (pid %d)\n", pid);
381 lseek(pid_fd, 0, SEEK_SET);
382 if (ftruncate(pid_fd, 0) == -1)
385 "FATAL: Faild to truncate stale PID file. (pid %d)\n", pid);
391 "rrdcached: removed stale PID file (no rrdcached on pid %d)\n"
392 "rrdcached: starting normally.\n", pid);
395 } /* }}} static int check_pidfile */
397 static int write_pidfile (int fd) /* {{{ */
404 fh = fdopen (fd, "w");
407 RRDD_LOG (LOG_ERR, "write_pidfile: fdopen() failed.");
412 fprintf (fh, "%i\n", (int) pid);
416 } /* }}} int write_pidfile */
418 static int remove_pidfile (void) /* {{{ */
423 file = (config_pid_file != NULL)
425 : LOCALSTATEDIR "/run/rrdcached.pid";
427 status = unlink (file);
431 } /* }}} int remove_pidfile */
433 static char *next_cmd (listen_socket_t *sock, ssize_t *len) /* {{{ */
437 eol = memchr(sock->rbuf + sock->next_cmd, '\n',
438 sock->next_read - sock->next_cmd);
442 /* no commands left, move remainder back to front of rbuf */
443 memmove(sock->rbuf, sock->rbuf + sock->next_cmd,
444 sock->next_read - sock->next_cmd);
445 sock->next_read -= sock->next_cmd;
452 char *cmd = sock->rbuf + sock->next_cmd;
455 sock->next_cmd = eol - sock->rbuf + 1;
457 if (eol > sock->rbuf && *(eol-1) == '\r')
458 *(--eol) = '\0'; /* handle "\r\n" EOL */
469 /* add the characters directly to the write buffer */
470 static int add_to_wbuf(listen_socket_t *sock, char *str, size_t len) /* {{{ */
474 assert(sock != NULL);
476 new_buf = rrd_realloc(sock->wbuf, sock->wbuf_len + len + 1);
479 RRDD_LOG(LOG_ERR, "add_to_wbuf: realloc failed");
483 strncpy(new_buf + sock->wbuf_len, str, len + 1);
485 sock->wbuf = new_buf;
486 sock->wbuf_len += len;
489 } /* }}} static int add_to_wbuf */
491 /* add the text to the "extra" info that's sent after the status line */
492 static int add_response_info(listen_socket_t *sock, char *fmt, ...) /* {{{ */
495 char buffer[CMD_MAX];
498 if (sock == NULL) return 0; /* journal replay mode */
499 if (sock->batch_start) return 0; /* no extra info returned when in BATCH */
502 #ifdef HAVE_VSNPRINTF
503 len = vsnprintf(buffer, sizeof(buffer), fmt, argp);
505 len = vsprintf(buffer, fmt, argp);
510 RRDD_LOG(LOG_ERR, "add_response_info: vnsprintf failed");
514 return add_to_wbuf(sock, buffer, len);
515 } /* }}} static int add_response_info */
517 static int count_lines(char *str) /* {{{ */
523 while ((str = strchr(str, '\n')) != NULL)
531 } /* }}} static int count_lines */
533 /* send the response back to the user.
534 * returns 0 on success, -1 on error
535 * write buffer is always zeroed after this call */
536 static int send_response (listen_socket_t *sock, response_code rc,
537 char *fmt, ...) /* {{{ */
540 char buffer[CMD_MAX];
545 if (sock == NULL) return rc; /* journal replay mode */
547 if (sock->batch_start)
550 return rc; /* no response on success during BATCH */
551 lines = sock->batch_cmd;
553 else if (rc == RESP_OK)
554 lines = count_lines(sock->wbuf);
558 rclen = sprintf(buffer, "%d ", lines);
560 #ifdef HAVE_VSNPRINTF
561 len = vsnprintf(buffer+rclen, sizeof(buffer)-rclen, fmt, argp);
563 len = vsprintf(buffer+rclen, fmt, argp);
571 /* append the result to the wbuf, don't write to the user */
572 if (sock->batch_start)
573 return add_to_wbuf(sock, buffer, len);
575 /* first write must be complete */
576 if (len != write(sock->fd, buffer, len))
578 RRDD_LOG(LOG_INFO, "send_response: could not write status message");
582 if (sock->wbuf != NULL && rc == RESP_OK)
585 while (wrote < sock->wbuf_len)
587 ssize_t wb = write(sock->fd, sock->wbuf + wrote, sock->wbuf_len - wrote);
590 RRDD_LOG(LOG_INFO, "send_response: could not write results");
597 free(sock->wbuf); sock->wbuf = NULL;
603 static void wipe_ci_values(cache_item_t *ci, time_t when)
608 ci->last_flush_time = when;
609 if (config_write_jitter > 0)
610 ci->last_flush_time += (rrd_random() % config_write_jitter);
614 * remove a "cache_item_t" item from the queue.
615 * must hold 'cache_lock' when calling this
617 static void remove_from_queue(cache_item_t *ci) /* {{{ */
619 if (ci == NULL) return;
620 if ((ci->flags & CI_FLAGS_IN_QUEUE) == 0) return; /* not queued */
622 if (ci->prev == NULL)
623 cache_queue_head = ci->next; /* reset head */
625 ci->prev->next = ci->next;
627 if (ci->next == NULL)
628 cache_queue_tail = ci->prev; /* reset the tail */
630 ci->next->prev = ci->prev;
632 ci->next = ci->prev = NULL;
633 ci->flags &= ~CI_FLAGS_IN_QUEUE;
635 pthread_mutex_lock (&stats_lock);
636 assert (stats_queue_length > 0);
637 stats_queue_length--;
638 pthread_mutex_unlock (&stats_lock);
640 } /* }}} static void remove_from_queue */
642 /* free the resources associated with the cache_item_t
643 * must hold cache_lock when calling this function
645 static void *free_cache_item(cache_item_t *ci) /* {{{ */
647 if (ci == NULL) return NULL;
649 remove_from_queue(ci);
651 for (size_t i=0; i < ci->values_num; i++)
657 /* in case anyone is waiting */
658 pthread_cond_broadcast(&ci->flushed);
659 pthread_cond_destroy(&ci->flushed);
664 } /* }}} static void *free_cache_item */
667 * enqueue_cache_item:
668 * `cache_lock' must be acquired before calling this function!
670 static int enqueue_cache_item (cache_item_t *ci, /* {{{ */
676 if (ci->values_num == 0)
681 if (cache_queue_head == ci)
684 /* remove if further down in queue */
685 remove_from_queue(ci);
688 ci->next = cache_queue_head;
689 if (ci->next != NULL)
691 cache_queue_head = ci;
693 if (cache_queue_tail == NULL)
694 cache_queue_tail = cache_queue_head;
696 else /* (side == TAIL) */
698 /* We don't move values back in the list.. */
699 if (ci->flags & CI_FLAGS_IN_QUEUE)
702 assert (ci->next == NULL);
703 assert (ci->prev == NULL);
705 ci->prev = cache_queue_tail;
707 if (cache_queue_tail == NULL)
708 cache_queue_head = ci;
710 cache_queue_tail->next = ci;
712 cache_queue_tail = ci;
715 ci->flags |= CI_FLAGS_IN_QUEUE;
717 pthread_cond_signal(&queue_cond);
718 pthread_mutex_lock (&stats_lock);
719 stats_queue_length++;
720 pthread_mutex_unlock (&stats_lock);
723 } /* }}} int enqueue_cache_item */
726 * tree_callback_flush:
727 * Called via `g_tree_foreach' in `flush_thread_main'. `cache_lock' is held
728 * while this is in progress.
730 static gboolean tree_callback_flush (gpointer key, gpointer value, /* {{{ */
734 callback_flush_data_t *cfd;
736 ci = (cache_item_t *) value;
737 cfd = (callback_flush_data_t *) data;
739 if (ci->flags & CI_FLAGS_IN_QUEUE)
742 if ((ci->last_flush_time <= cfd->abs_timeout)
743 && (ci->values_num > 0))
745 enqueue_cache_item (ci, TAIL);
747 else if ((do_shutdown != 0)
748 && (ci->values_num > 0))
750 enqueue_cache_item (ci, TAIL);
752 else if (((cfd->now - ci->last_flush_time) >= config_flush_interval)
753 && (ci->values_num <= 0))
755 assert ((char *) key == ci->file);
756 if (!rrd_add_ptr((void ***)&cfd->keys, &cfd->keys_num, (void *)key))
758 RRDD_LOG (LOG_ERR, "tree_callback_flush: rrd_add_ptrs failed.");
764 } /* }}} gboolean tree_callback_flush */
766 static int flush_old_values (int max_age)
768 callback_flush_data_t cfd;
771 memset (&cfd, 0, sizeof (cfd));
772 /* Pass the current time as user data so that we don't need to call
773 * `time' for each node. */
774 cfd.now = time (NULL);
779 cfd.abs_timeout = cfd.now - max_age;
781 cfd.abs_timeout = cfd.now + 2*config_write_jitter + 1;
783 /* `tree_callback_flush' will return the keys of all values that haven't
784 * been touched in the last `config_flush_interval' seconds in `cfd'.
785 * The char*'s in this array point to the same memory as ci->file, so we
786 * don't need to free them separately. */
787 g_tree_foreach (cache_tree, tree_callback_flush, (gpointer) &cfd);
789 for (k = 0; k < cfd.keys_num; k++)
791 /* should never fail, since we have held the cache_lock
793 assert( g_tree_remove(cache_tree, cfd.keys[k]) == TRUE );
796 if (cfd.keys != NULL)
803 } /* int flush_old_values */
805 static void *flush_thread_main (void *args __attribute__((unused))) /* {{{ */
808 struct timespec next_flush;
811 gettimeofday (&now, NULL);
812 next_flush.tv_sec = now.tv_sec + config_flush_interval;
813 next_flush.tv_nsec = 1000 * now.tv_usec;
815 pthread_mutex_lock(&cache_lock);
819 gettimeofday (&now, NULL);
820 if ((now.tv_sec > next_flush.tv_sec)
821 || ((now.tv_sec == next_flush.tv_sec)
822 && ((1000 * now.tv_usec) > next_flush.tv_nsec)))
824 /* Flush all values that haven't been written in the last
825 * `config_write_interval' seconds. */
826 flush_old_values (config_write_interval);
828 /* Determine the time of the next cache flush. */
830 now.tv_sec + next_flush.tv_sec % config_flush_interval;
832 /* unlock the cache while we rotate so we don't block incoming
833 * updates if the fsync() blocks on disk I/O */
834 pthread_mutex_unlock(&cache_lock);
836 pthread_mutex_lock(&cache_lock);
839 status = pthread_cond_timedwait(&flush_cond, &cache_lock, &next_flush);
840 if (status != 0 && status != ETIMEDOUT)
842 RRDD_LOG (LOG_ERR, "flush_thread_main: "
843 "pthread_cond_timedwait returned %i.", status);
847 if (config_flush_at_shutdown)
848 flush_old_values (-1); /* flush everything */
850 pthread_mutex_unlock(&cache_lock);
853 } /* void *flush_thread_main */
855 static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
857 pthread_mutex_lock (&cache_lock);
860 || (cache_queue_head != NULL && config_flush_at_shutdown))
868 /* Now, check if there's something to store away. If not, wait until
869 * something comes in. if we are shutting down, do not wait around. */
870 if (cache_queue_head == NULL && !do_shutdown)
872 status = pthread_cond_wait (&queue_cond, &cache_lock);
873 if ((status != 0) && (status != ETIMEDOUT))
875 RRDD_LOG (LOG_ERR, "queue_thread_main: "
876 "pthread_cond_wait returned %i.", status);
880 /* Check if a value has arrived. This may be NULL if we timed out or there
881 * was an interrupt such as a signal. */
882 if (cache_queue_head == NULL)
885 ci = cache_queue_head;
887 /* copy the relevant parts */
888 file = strdup (ci->file);
891 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
895 assert(ci->values != NULL);
896 assert(ci->values_num > 0);
899 values_num = ci->values_num;
901 wipe_ci_values(ci, time(NULL));
902 remove_from_queue(ci);
904 pthread_mutex_unlock (&cache_lock);
907 status = rrd_update_r (file, NULL, (int) values_num, (void *) values);
910 RRDD_LOG (LOG_NOTICE, "queue_thread_main: "
911 "rrd_update_r (%s) failed with status %i. (%s)",
912 file, status, rrd_get_error());
915 journal_write("wrote", file);
917 /* Search again in the tree. It's possible someone issued a "FORGET"
918 * while we were writing the update values. */
919 pthread_mutex_lock(&cache_lock);
920 ci = (cache_item_t *) g_tree_lookup(cache_tree, file);
922 pthread_cond_broadcast(&ci->flushed);
923 pthread_mutex_unlock(&cache_lock);
925 rrd_free_ptrs((void ***) &values, &values_num);
930 pthread_mutex_lock (&stats_lock);
931 stats_updates_written++;
932 stats_data_sets_written += values_num;
933 pthread_mutex_unlock (&stats_lock);
936 pthread_mutex_lock (&cache_lock);
938 pthread_mutex_unlock (&cache_lock);
941 } /* }}} void *queue_thread_main */
943 static int buffer_get_field (char **buffer_ret, /* {{{ */
944 size_t *buffer_size_ret, char **field_ret)
953 buffer = *buffer_ret;
955 buffer_size = *buffer_size_ret;
959 if (buffer_size <= 0)
962 /* This is ensured by `handle_request'. */
963 assert (buffer[buffer_size - 1] == '\0');
966 while (buffer_pos < buffer_size)
968 /* Check for end-of-field or end-of-buffer */
969 if (buffer[buffer_pos] == ' ' || buffer[buffer_pos] == '\0')
971 field[field_size] = 0;
977 /* Handle escaped characters. */
978 else if (buffer[buffer_pos] == '\\')
980 if (buffer_pos >= (buffer_size - 1))
983 field[field_size] = buffer[buffer_pos];
987 /* Normal operation */
990 field[field_size] = buffer[buffer_pos];
994 } /* while (buffer_pos < buffer_size) */
999 *buffer_ret = buffer + buffer_pos;
1000 *buffer_size_ret = buffer_size - buffer_pos;
1004 } /* }}} int buffer_get_field */
1006 /* if we're restricting writes to the base directory,
1007 * check whether the file falls within the dir
1008 * returns 1 if OK, otherwise 0
1010 static int check_file_access (const char *file, listen_socket_t *sock) /* {{{ */
1012 assert(file != NULL);
1014 if (!config_write_base_only
1015 || sock == NULL /* journal replay */
1016 || config_base_dir == NULL)
1019 if (strstr(file, "../") != NULL) goto err;
1021 /* relative paths without "../" are ok */
1022 if (*file != '/') return 1;
1024 /* file must be of the format base + "/" + <1+ char filename> */
1025 if (strlen(file) < _config_base_dir_len + 2) goto err;
1026 if (strncmp(file, config_base_dir, _config_base_dir_len) != 0) goto err;
1027 if (*(file + _config_base_dir_len) != '/') goto err;
1032 if (sock != NULL && sock->fd >= 0)
1033 send_response(sock, RESP_ERR, "%s\n", rrd_strerror(EACCES));
1036 } /* }}} static int check_file_access */
1038 /* when using a base dir, convert relative paths to absolute paths.
1039 * if necessary, modifies the "filename" pointer to point
1040 * to the new path created in "tmp". "tmp" is provided
1041 * by the caller and sizeof(tmp) must be >= PATH_MAX.
1043 * this allows us to optimize for the expected case (absolute path)
1046 static void get_abs_path(char **filename, char *tmp)
1048 assert(tmp != NULL);
1049 assert(filename != NULL && *filename != NULL);
1051 if (config_base_dir == NULL || **filename == '/')
1054 snprintf(tmp, PATH_MAX, "%s/%s", config_base_dir, *filename);
1056 } /* }}} static int get_abs_path */
1058 /* returns 1 if we have the required privilege level,
1059 * otherwise issue an error to the user on sock */
1060 static int has_privilege (listen_socket_t *sock, /* {{{ */
1061 socket_privilege priv)
1063 if (sock == NULL) /* journal replay */
1066 if (sock->privilege >= priv)
1069 return send_response(sock, RESP_ERR, "%s\n", rrd_strerror(EACCES));
1070 } /* }}} static int has_privilege */
1072 static int flush_file (const char *filename) /* {{{ */
1076 pthread_mutex_lock (&cache_lock);
1078 ci = (cache_item_t *) g_tree_lookup (cache_tree, filename);
1081 pthread_mutex_unlock (&cache_lock);
1085 if (ci->values_num > 0)
1087 /* Enqueue at head */
1088 enqueue_cache_item (ci, HEAD);
1089 pthread_cond_wait(&ci->flushed, &cache_lock);
1092 /* DO NOT DO ANYTHING WITH ci HERE!! The entry
1093 * may have been purged during our cond_wait() */
1095 pthread_mutex_unlock(&cache_lock);
1098 } /* }}} int flush_file */
1100 static int syntax_error(listen_socket_t *sock, struct command *cmd) /* {{{ */
1102 char *err = "Syntax error.\n";
1104 if (cmd && cmd->syntax)
1107 return send_response(sock, RESP_ERR, "Usage: %s", err);
1108 } /* }}} static int syntax_error() */
1110 static int handle_request_stats (HANDLER_PROTO) /* {{{ */
1112 uint64_t copy_queue_length;
1113 uint64_t copy_updates_received;
1114 uint64_t copy_flush_received;
1115 uint64_t copy_updates_written;
1116 uint64_t copy_data_sets_written;
1117 uint64_t copy_journal_bytes;
1118 uint64_t copy_journal_rotate;
1120 uint64_t tree_nodes_number;
1121 uint64_t tree_depth;
1123 pthread_mutex_lock (&stats_lock);
1124 copy_queue_length = stats_queue_length;
1125 copy_updates_received = stats_updates_received;
1126 copy_flush_received = stats_flush_received;
1127 copy_updates_written = stats_updates_written;
1128 copy_data_sets_written = stats_data_sets_written;
1129 copy_journal_bytes = stats_journal_bytes;
1130 copy_journal_rotate = stats_journal_rotate;
1131 pthread_mutex_unlock (&stats_lock);
1133 pthread_mutex_lock (&cache_lock);
1134 tree_nodes_number = (uint64_t) g_tree_nnodes (cache_tree);
1135 tree_depth = (uint64_t) g_tree_height (cache_tree);
1136 pthread_mutex_unlock (&cache_lock);
1138 add_response_info(sock,
1139 "QueueLength: %"PRIu64"\n", copy_queue_length);
1140 add_response_info(sock,
1141 "UpdatesReceived: %"PRIu64"\n", copy_updates_received);
1142 add_response_info(sock,
1143 "FlushesReceived: %"PRIu64"\n", copy_flush_received);
1144 add_response_info(sock,
1145 "UpdatesWritten: %"PRIu64"\n", copy_updates_written);
1146 add_response_info(sock,
1147 "DataSetsWritten: %"PRIu64"\n", copy_data_sets_written);
1148 add_response_info(sock, "TreeNodesNumber: %"PRIu64"\n", tree_nodes_number);
1149 add_response_info(sock, "TreeDepth: %"PRIu64"\n", tree_depth);
1150 add_response_info(sock, "JournalBytes: %"PRIu64"\n", copy_journal_bytes);
1151 add_response_info(sock, "JournalRotate: %"PRIu64"\n", copy_journal_rotate);
1153 send_response(sock, RESP_OK, "Statistics follow\n");
1156 } /* }}} int handle_request_stats */
1158 static int handle_request_flush (HANDLER_PROTO) /* {{{ */
1160 char *file, file_tmp[PATH_MAX];
1163 status = buffer_get_field (&buffer, &buffer_size, &file);
1166 return syntax_error(sock,cmd);
1170 pthread_mutex_lock(&stats_lock);
1171 stats_flush_received++;
1172 pthread_mutex_unlock(&stats_lock);
1174 get_abs_path(&file, file_tmp);
1175 if (!check_file_access(file, sock)) return 0;
1177 status = flush_file (file);
1179 return send_response(sock, RESP_OK, "Successfully flushed %s.\n", file);
1180 else if (status == ENOENT)
1182 /* no file in our tree; see whether it exists at all */
1183 struct stat statbuf;
1185 memset(&statbuf, 0, sizeof(statbuf));
1186 if (stat(file, &statbuf) == 0 && S_ISREG(statbuf.st_mode))
1187 return send_response(sock, RESP_OK, "Nothing to flush: %s.\n", file);
1189 return send_response(sock, RESP_ERR, "No such file: %s.\n", file);
1191 else if (status < 0)
1192 return send_response(sock, RESP_ERR, "Internal error.\n");
1194 return send_response(sock, RESP_ERR, "Failed with status %i.\n", status);
1199 } /* }}} int handle_request_flush */
1201 static int handle_request_flushall(HANDLER_PROTO) /* {{{ */
1203 RRDD_LOG(LOG_DEBUG, "Received FLUSHALL");
1205 pthread_mutex_lock(&cache_lock);
1206 flush_old_values(-1);
1207 pthread_mutex_unlock(&cache_lock);
1209 return send_response(sock, RESP_OK, "Started flush.\n");
1210 } /* }}} static int handle_request_flushall */
1212 static int handle_request_pending(HANDLER_PROTO) /* {{{ */
1215 char *file, file_tmp[PATH_MAX];
1218 status = buffer_get_field(&buffer, &buffer_size, &file);
1220 return syntax_error(sock,cmd);
1222 get_abs_path(&file, file_tmp);
1224 pthread_mutex_lock(&cache_lock);
1225 ci = g_tree_lookup(cache_tree, file);
1228 pthread_mutex_unlock(&cache_lock);
1229 return send_response(sock, RESP_ERR, "%s\n", rrd_strerror(ENOENT));
1232 for (size_t i=0; i < ci->values_num; i++)
1233 add_response_info(sock, "%s\n", ci->values[i]);
1235 pthread_mutex_unlock(&cache_lock);
1236 return send_response(sock, RESP_OK, "updates pending\n");
1237 } /* }}} static int handle_request_pending */
1239 static int handle_request_forget(HANDLER_PROTO) /* {{{ */
1243 char *file, file_tmp[PATH_MAX];
1245 status = buffer_get_field(&buffer, &buffer_size, &file);
1247 return syntax_error(sock,cmd);
1249 get_abs_path(&file, file_tmp);
1250 if (!check_file_access(file, sock)) return 0;
1252 pthread_mutex_lock(&cache_lock);
1253 found = g_tree_remove(cache_tree, file);
1254 pthread_mutex_unlock(&cache_lock);
1259 journal_write("forget", file);
1261 return send_response(sock, RESP_OK, "Gone!\n");
1264 return send_response(sock, RESP_ERR, "%s\n", rrd_strerror(ENOENT));
1268 } /* }}} static int handle_request_forget */
1270 static int handle_request_queue (HANDLER_PROTO) /* {{{ */
1274 pthread_mutex_lock(&cache_lock);
1276 ci = cache_queue_head;
1279 add_response_info(sock, "%d %s\n", ci->values_num, ci->file);
1283 pthread_mutex_unlock(&cache_lock);
1285 return send_response(sock, RESP_OK, "in queue.\n");
1286 } /* }}} int handle_request_queue */
1288 static int handle_request_update (HANDLER_PROTO) /* {{{ */
1290 char *file, file_tmp[PATH_MAX];
1293 char orig_buf[CMD_MAX];
1297 /* save it for the journal later */
1298 strncpy(orig_buf, buffer, sizeof(orig_buf)-1);
1300 status = buffer_get_field (&buffer, &buffer_size, &file);
1302 return syntax_error(sock,cmd);
1304 pthread_mutex_lock(&stats_lock);
1305 stats_updates_received++;
1306 pthread_mutex_unlock(&stats_lock);
1308 get_abs_path(&file, file_tmp);
1309 if (!check_file_access(file, sock)) return 0;
1311 pthread_mutex_lock (&cache_lock);
1312 ci = g_tree_lookup (cache_tree, file);
1314 if (ci == NULL) /* {{{ */
1316 struct stat statbuf;
1319 /* don't hold the lock while we setup; stat(2) might block */
1320 pthread_mutex_unlock(&cache_lock);
1322 memset (&statbuf, 0, sizeof (statbuf));
1323 status = stat (file, &statbuf);
1326 RRDD_LOG (LOG_NOTICE, "handle_request_update: stat (%s) failed.", file);
1329 if (status == ENOENT)
1330 return send_response(sock, RESP_ERR, "No such file: %s\n", file);
1332 return send_response(sock, RESP_ERR,
1333 "stat failed with error %i.\n", status);
1335 if (!S_ISREG (statbuf.st_mode))
1336 return send_response(sock, RESP_ERR, "Not a regular file: %s\n", file);
1338 if (access(file, R_OK|W_OK) != 0)
1339 return send_response(sock, RESP_ERR, "Cannot read/write %s: %s\n",
1340 file, rrd_strerror(errno));
1342 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
1345 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
1347 return send_response(sock, RESP_ERR, "malloc failed.\n");
1349 memset (ci, 0, sizeof (cache_item_t));
1351 ci->file = strdup (file);
1352 if (ci->file == NULL)
1355 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
1357 return send_response(sock, RESP_ERR, "strdup failed.\n");
1360 wipe_ci_values(ci, now);
1361 ci->flags = CI_FLAGS_IN_TREE;
1362 pthread_cond_init(&ci->flushed, NULL);
1364 pthread_mutex_lock(&cache_lock);
1366 /* another UPDATE might have added this entry in the meantime */
1367 tmp = g_tree_lookup (cache_tree, file);
1369 g_tree_replace (cache_tree, (void *) ci->file, (void *) ci);
1372 free_cache_item (ci);
1376 assert (ci != NULL);
1378 /* don't re-write updates in replay mode */
1380 journal_write("update", orig_buf);
1382 while (buffer_size > 0)
1388 status = buffer_get_field (&buffer, &buffer_size, &value);
1391 RRDD_LOG (LOG_INFO, "handle_request_update: Error reading field.");
1395 /* make sure update time is always moving forward */
1396 stamp = strtol(value, &eostamp, 10);
1397 if (eostamp == value || eostamp == NULL || *eostamp != ':')
1399 pthread_mutex_unlock(&cache_lock);
1400 return send_response(sock, RESP_ERR,
1401 "Cannot find timestamp in '%s'!\n", value);
1403 else if (stamp <= ci->last_update_stamp)
1405 pthread_mutex_unlock(&cache_lock);
1406 return send_response(sock, RESP_ERR,
1407 "illegal attempt to update using time %ld when last"
1408 " update time is %ld (minimum one second step)\n",
1409 stamp, ci->last_update_stamp);
1412 ci->last_update_stamp = stamp;
1414 if (!rrd_add_strdup(&ci->values, &ci->values_num, value))
1416 RRDD_LOG (LOG_ERR, "handle_request_update: rrd_add_strdup failed.");
1423 if (((now - ci->last_flush_time) >= config_write_interval)
1424 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
1425 && (ci->values_num > 0))
1427 enqueue_cache_item (ci, TAIL);
1430 pthread_mutex_unlock (&cache_lock);
1433 return send_response(sock, RESP_ERR, "No values updated.\n");
1435 return send_response(sock, RESP_OK,
1436 "errors, enqueued %i value(s).\n", values_num);
1441 } /* }}} int handle_request_update */
1443 /* we came across a "WROTE" entry during journal replay.
1444 * throw away any values that we have accumulated for this file
1446 static int handle_request_wrote (HANDLER_PROTO) /* {{{ */
1449 const char *file = buffer;
1451 pthread_mutex_lock(&cache_lock);
1453 ci = g_tree_lookup(cache_tree, file);
1456 pthread_mutex_unlock(&cache_lock);
1461 rrd_free_ptrs((void ***) &ci->values, &ci->values_num);
1463 wipe_ci_values(ci, now);
1464 remove_from_queue(ci);
1466 pthread_mutex_unlock(&cache_lock);
1468 } /* }}} int handle_request_wrote */
1470 /* start "BATCH" processing */
1471 static int batch_start (HANDLER_PROTO) /* {{{ */
1474 if (sock->batch_start)
1475 return send_response(sock, RESP_ERR, "Already in BATCH\n");
1477 status = send_response(sock, RESP_OK,
1478 "Go ahead. End with dot '.' on its own line.\n");
1479 sock->batch_start = time(NULL);
1480 sock->batch_cmd = 0;
1483 } /* }}} static int batch_start */
1485 /* finish "BATCH" processing and return results to the client */
1486 static int batch_done (HANDLER_PROTO) /* {{{ */
1488 assert(sock->batch_start);
1489 sock->batch_start = 0;
1490 sock->batch_cmd = 0;
1491 return send_response(sock, RESP_OK, "errors\n");
1492 } /* }}} static int batch_done */
1494 static int handle_request_quit (HANDLER_PROTO) /* {{{ */
1497 } /* }}} static int handle_request_quit */
1499 struct command COMMANDS[] = {
1502 handle_request_update,
1505 "UPDATE <filename> <values> [<values> ...]\n"
1507 "Adds the given file to the internal cache if it is not yet known and\n"
1508 "appends the given value(s) to the entry. See the rrdcached(1) manpage\n"
1511 "Each <values> has the following form:\n"
1512 " <values> = <time>:<value>[:<value>[...]]\n"
1513 "See the rrdupdate(1) manpage for details.\n"
1517 handle_request_wrote,
1519 CMD_CONTEXT_JOURNAL,
1525 handle_request_flush,
1527 CMD_CONTEXT_CLIENT | CMD_CONTEXT_BATCH,
1528 "FLUSH <filename>\n"
1530 "Adds the given filename to the head of the update queue and returns\n"
1531 "after it has been dequeued.\n"
1535 handle_request_flushall,
1540 "Triggers writing of all pending updates. Returns immediately.\n"
1544 handle_request_pending,
1547 "PENDING <filename>\n"
1549 "Shows any 'pending' updates for a file, in order.\n"
1550 "The updates shown have not yet been written to the underlying RRD file.\n"
1554 handle_request_forget,
1557 "FORGET <filename>\n"
1559 "Removes the file completely from the cache.\n"
1560 "Any pending updates for the file will be lost.\n"
1564 handle_request_queue,
1569 "Shows all files in the output queue.\n"
1570 "The output is zero or more lines in the following format:\n"
1571 "(where <num_vals> is the number of values to be written)\n"
1573 "<num_vals> <filename>\n"
1577 handle_request_stats,
1582 "Returns some performance counters, see the rrdcached(1) manpage for\n"
1583 "a description of the values.\n"
1587 handle_request_help,
1590 "HELP [<command>]\n",
1591 NULL, /* special! */
1600 "The 'BATCH' command permits the client to initiate a bulk load\n"
1601 " of commands to rrdcached.\n"
1606 " server: 0 Go ahead. End with dot '.' on its own line.\n"
1607 " client: command #1\n"
1608 " client: command #2\n"
1609 " client: ... and so on\n"
1611 " server: 2 errors\n"
1612 " server: 7 message for command #7\n"
1613 " server: 9 message for command #9\n"
1615 "For more information, consult the rrdcached(1) documentation.\n"
1618 ".", /* BATCH terminator */
1627 handle_request_quit,
1629 CMD_CONTEXT_CLIENT | CMD_CONTEXT_BATCH,
1632 "Disconnect from rrdcached.\n"
1634 {NULL,NULL,0,0,NULL,NULL} /* LAST ENTRY */
1637 static struct command *find_command(char *cmd)
1639 struct command *c = COMMANDS;
1641 while (c->cmd != NULL)
1643 if (strcasecmp(cmd, c->cmd) == 0)
1654 /* check whether commands are received in the expected context */
1655 static int command_check_context(listen_socket_t *sock, struct command *cmd)
1658 return (cmd->context & CMD_CONTEXT_JOURNAL);
1659 else if (sock->batch_start)
1660 return (cmd->context & CMD_CONTEXT_BATCH);
1662 return (cmd->context & CMD_CONTEXT_CLIENT);
1668 static int handle_request_help (HANDLER_PROTO) /* {{{ */
1673 struct command *help = NULL;
1675 status = buffer_get_field (&buffer, &buffer_size, &cmd_str);
1677 help = find_command(cmd_str);
1679 if (help && (help->syntax || help->help))
1683 snprintf(tmp, sizeof(tmp)-1, "Help for %s\n", help->cmd);
1687 add_response_info(sock, "Usage: %s\n", help->syntax);
1690 add_response_info(sock, "%s\n", help->help);
1695 resp_txt = "Command overview\n";
1700 add_response_info(sock, "%s", help->syntax);
1705 return send_response(sock, RESP_OK, resp_txt);
1706 } /* }}} int handle_request_help */
1708 /* if sock==NULL, we are in journal replay mode */
1709 static int handle_request (DISPATCH_PROTO) /* {{{ */
1711 char *buffer_ptr = buffer;
1712 char *cmd_str = NULL;
1713 struct command *cmd = NULL;
1716 assert (buffer[buffer_size - 1] == '\0');
1718 status = buffer_get_field (&buffer_ptr, &buffer_size, &cmd_str);
1721 RRDD_LOG (LOG_INFO, "handle_request: Unable parse command.");
1725 if (sock != NULL && sock->batch_start)
1728 cmd = find_command(cmd_str);
1730 return send_response(sock, RESP_ERR, "Unknown command: %s\n", cmd_str);
1732 status = has_privilege(sock, cmd->min_priv);
1736 if (!command_check_context(sock, cmd))
1737 return send_response(sock, RESP_ERR, "Can't use '%s' here.\n", cmd_str);
1739 return cmd->handler(cmd, sock, now, buffer_ptr, buffer_size);
1740 } /* }}} int handle_request */
1742 /* MUST NOT hold journal_lock before calling this */
1743 static void journal_rotate(void) /* {{{ */
1745 FILE *old_fh = NULL;
1748 if (journal_cur == NULL || journal_old == NULL)
1751 pthread_mutex_lock(&journal_lock);
1753 /* we rotate this way (rename before close) so that the we can release
1754 * the journal lock as fast as possible. Journal writes to the new
1755 * journal can proceed immediately after the new file is opened. The
1756 * fclose can then block without affecting new updates.
1758 if (journal_fh != NULL)
1760 old_fh = journal_fh;
1762 rename(journal_cur, journal_old);
1763 ++stats_journal_rotate;
1766 new_fd = open(journal_cur, O_WRONLY|O_CREAT|O_APPEND,
1767 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
1770 journal_fh = fdopen(new_fd, "a");
1771 if (journal_fh == NULL)
1775 pthread_mutex_unlock(&journal_lock);
1780 if (journal_fh == NULL)
1783 "JOURNALING DISABLED: Cannot open journal file '%s' : (%s)",
1784 journal_cur, rrd_strerror(errno));
1787 "JOURNALING DISABLED: All values will be flushed at shutdown");
1788 config_flush_at_shutdown = 1;
1791 } /* }}} static void journal_rotate */
1793 static void journal_done(void) /* {{{ */
1795 if (journal_cur == NULL)
1798 pthread_mutex_lock(&journal_lock);
1799 if (journal_fh != NULL)
1805 if (config_flush_at_shutdown)
1807 RRDD_LOG(LOG_INFO, "removing journals");
1808 unlink(journal_old);
1809 unlink(journal_cur);
1813 RRDD_LOG(LOG_INFO, "expedited shutdown; "
1814 "journals will be used at next startup");
1817 pthread_mutex_unlock(&journal_lock);
1819 } /* }}} static void journal_done */
1821 static int journal_write(char *cmd, char *args) /* {{{ */
1825 if (journal_fh == NULL)
1828 pthread_mutex_lock(&journal_lock);
1829 chars = fprintf(journal_fh, "%s %s\n", cmd, args);
1830 pthread_mutex_unlock(&journal_lock);
1834 pthread_mutex_lock(&stats_lock);
1835 stats_journal_bytes += chars;
1836 pthread_mutex_unlock(&stats_lock);
1840 } /* }}} static int journal_write */
1842 static int journal_replay (const char *file) /* {{{ */
1848 char entry[CMD_MAX];
1851 if (file == NULL) return 0;
1854 char *reason = "unknown error";
1856 struct stat statbuf;
1858 memset(&statbuf, 0, sizeof(statbuf));
1859 if (stat(file, &statbuf) != 0)
1861 if (errno == ENOENT)
1864 reason = "stat error";
1867 else if (!S_ISREG(statbuf.st_mode))
1869 reason = "not a regular file";
1872 if (statbuf.st_uid != daemon_uid)
1874 reason = "not owned by daemon user";
1877 if (statbuf.st_mode & (S_IWGRP|S_IWOTH))
1879 reason = "must not be user/group writable";
1885 RRDD_LOG(LOG_ERR, "journal_replay: %s : %s (%s)",
1886 file, rrd_strerror(status), reason);
1891 fh = fopen(file, "r");
1894 if (errno != ENOENT)
1895 RRDD_LOG(LOG_ERR, "journal_replay: cannot open journal file: '%s' (%s)",
1896 file, rrd_strerror(errno));
1900 RRDD_LOG(LOG_NOTICE, "replaying from journal: %s", file);
1909 if (fgets(entry, sizeof(entry), fh) == NULL)
1911 entry_len = strlen(entry);
1913 /* check \n termination in case journal writing crashed mid-line */
1916 else if (entry[entry_len - 1] != '\n')
1918 RRDD_LOG(LOG_NOTICE, "Malformed journal entry at line %"PRIu64, line);
1923 entry[entry_len - 1] = '\0';
1925 if (handle_request(NULL, now, entry, entry_len) == 0)
1933 RRDD_LOG(LOG_INFO, "Replayed %d entries (%d failures)",
1934 entry_cnt, fail_cnt);
1936 return entry_cnt > 0 ? 1 : 0;
1937 } /* }}} static int journal_replay */
1939 static void journal_init(void) /* {{{ */
1941 int had_journal = 0;
1943 if (journal_cur == NULL) return;
1945 pthread_mutex_lock(&journal_lock);
1947 RRDD_LOG(LOG_INFO, "checking for journal files");
1949 had_journal += journal_replay(journal_old);
1950 had_journal += journal_replay(journal_cur);
1952 /* it must have been a crash. start a flush */
1953 if (had_journal && config_flush_at_shutdown)
1954 flush_old_values(-1);
1956 pthread_mutex_unlock(&journal_lock);
1959 RRDD_LOG(LOG_INFO, "journal processing complete");
1961 } /* }}} static void journal_init */
1963 static void free_listen_socket(listen_socket_t *sock) /* {{{ */
1965 assert(sock != NULL);
1967 free(sock->rbuf); sock->rbuf = NULL;
1968 free(sock->wbuf); sock->wbuf = NULL;
1970 } /* }}} void free_listen_socket */
1972 static void close_connection(listen_socket_t *sock) /* {{{ */
1980 free_listen_socket(sock);
1982 } /* }}} void close_connection */
1984 static void *connection_thread_main (void *args) /* {{{ */
1986 listen_socket_t *sock;
1989 sock = (listen_socket_t *) args;
1992 /* init read buffers */
1993 sock->next_read = sock->next_cmd = 0;
1994 sock->rbuf = malloc(RBUF_SIZE);
1995 if (sock->rbuf == NULL)
1997 RRDD_LOG(LOG_ERR, "connection_thread_main: cannot malloc read buffer");
1998 close_connection(sock);
2002 pthread_mutex_lock (&connection_threads_lock);
2003 connection_threads_num++;
2004 pthread_mutex_unlock (&connection_threads_lock);
2006 while (do_shutdown == 0)
2013 struct pollfd pollfd;
2017 pollfd.events = POLLIN | POLLPRI;
2020 status = poll (&pollfd, 1, /* timeout = */ 500);
2023 else if (status == 0) /* timeout */
2025 else if (status < 0) /* error */
2028 if (status != EINTR)
2029 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
2033 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
2035 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
2037 RRDD_LOG (LOG_WARNING, "connection_thread_main: "
2038 "poll(2) returned something unexpected: %#04hx",
2043 rbytes = read(fd, sock->rbuf + sock->next_read,
2044 RBUF_SIZE - sock->next_read);
2047 RRDD_LOG(LOG_ERR, "connection_thread_main: read() failed.");
2050 else if (rbytes == 0)
2053 sock->next_read += rbytes;
2055 if (sock->batch_start)
2056 now = sock->batch_start;
2060 while ((cmd = next_cmd(sock, &cmd_len)) != NULL)
2062 status = handle_request (sock, now, cmd, cmd_len+1);
2069 close_connection(sock);
2071 /* Remove this thread from the connection threads list */
2072 pthread_mutex_lock (&connection_threads_lock);
2073 connection_threads_num--;
2074 if (connection_threads_num <= 0)
2075 pthread_cond_broadcast(&connection_threads_done);
2076 pthread_mutex_unlock (&connection_threads_lock);
2079 } /* }}} void *connection_thread_main */
2081 static int open_listen_socket_unix (const listen_socket_t *sock) /* {{{ */
2084 struct sockaddr_un sa;
2085 listen_socket_t *temp;
2090 if (strncmp(path, "unix:", strlen("unix:")) == 0)
2091 path += strlen("unix:");
2093 temp = (listen_socket_t *) rrd_realloc (listen_fds,
2094 sizeof (listen_fds[0]) * (listen_fds_num + 1));
2097 fprintf (stderr, "rrdcached: open_listen_socket_unix: realloc failed.\n");
2101 memcpy (listen_fds + listen_fds_num, sock, sizeof (listen_fds[0]));
2103 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
2106 fprintf (stderr, "rrdcached: unix socket(2) failed: %s\n",
2107 rrd_strerror(errno));
2111 memset (&sa, 0, sizeof (sa));
2112 sa.sun_family = AF_UNIX;
2113 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
2115 /* if we've gotten this far, we own the pid file. any daemon started
2116 * with the same args must not be alive. therefore, ensure that we can
2117 * create the socket...
2121 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
2124 fprintf (stderr, "rrdcached: bind(%s) failed: %s.\n",
2125 path, rrd_strerror(errno));
2130 status = listen (fd, /* backlog = */ 10);
2133 fprintf (stderr, "rrdcached: listen(%s) failed: %s.\n",
2134 path, rrd_strerror(errno));
2140 listen_fds[listen_fds_num].fd = fd;
2141 listen_fds[listen_fds_num].family = PF_UNIX;
2142 strncpy(listen_fds[listen_fds_num].addr, path,
2143 sizeof (listen_fds[listen_fds_num].addr) - 1);
2147 } /* }}} int open_listen_socket_unix */
2149 static int open_listen_socket_network(const listen_socket_t *sock) /* {{{ */
2151 struct addrinfo ai_hints;
2152 struct addrinfo *ai_res;
2153 struct addrinfo *ai_ptr;
2154 char addr_copy[NI_MAXHOST];
2159 strncpy (addr_copy, sock->addr, sizeof(addr_copy)-1);
2160 addr_copy[sizeof (addr_copy) - 1] = 0;
2163 memset (&ai_hints, 0, sizeof (ai_hints));
2164 ai_hints.ai_flags = 0;
2165 #ifdef AI_ADDRCONFIG
2166 ai_hints.ai_flags |= AI_ADDRCONFIG;
2168 ai_hints.ai_family = AF_UNSPEC;
2169 ai_hints.ai_socktype = SOCK_STREAM;
2172 if (*addr == '[') /* IPv6+port format */
2174 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
2177 port = strchr (addr, ']');
2180 fprintf (stderr, "rrdcached: Malformed address: %s\n", sock->addr);
2188 else if (*port == 0)
2192 fprintf (stderr, "rrdcached: Garbage after address: %s\n", port);
2195 } /* if (*addr = ']') */
2196 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
2198 port = rindex(addr, ':');
2206 status = getaddrinfo (addr,
2207 port == NULL ? RRDCACHED_DEFAULT_PORT : port,
2208 &ai_hints, &ai_res);
2211 fprintf (stderr, "rrdcached: getaddrinfo(%s) failed: %s\n",
2212 addr, gai_strerror (status));
2216 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2219 listen_socket_t *temp;
2222 temp = (listen_socket_t *) rrd_realloc (listen_fds,
2223 sizeof (listen_fds[0]) * (listen_fds_num + 1));
2227 "rrdcached: open_listen_socket_network: realloc failed.\n");
2231 memcpy (listen_fds + listen_fds_num, sock, sizeof (listen_fds[0]));
2233 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
2236 fprintf (stderr, "rrdcached: network socket(2) failed: %s.\n",
2237 rrd_strerror(errno));
2241 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
2243 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2246 fprintf (stderr, "rrdcached: bind(%s) failed: %s.\n",
2247 sock->addr, rrd_strerror(errno));
2252 status = listen (fd, /* backlog = */ 10);
2255 fprintf (stderr, "rrdcached: listen(%s) failed: %s\n.",
2256 sock->addr, rrd_strerror(errno));
2258 freeaddrinfo(ai_res);
2262 listen_fds[listen_fds_num].fd = fd;
2263 listen_fds[listen_fds_num].family = ai_ptr->ai_family;
2265 } /* for (ai_ptr) */
2267 freeaddrinfo(ai_res);
2269 } /* }}} static int open_listen_socket_network */
2271 static int open_listen_socket (const listen_socket_t *sock) /* {{{ */
2273 assert(sock != NULL);
2274 assert(sock->addr != NULL);
2276 if (strncmp ("unix:", sock->addr, strlen ("unix:")) == 0
2277 || sock->addr[0] == '/')
2278 return (open_listen_socket_unix(sock));
2280 return (open_listen_socket_network(sock));
2281 } /* }}} int open_listen_socket */
2283 static int close_listen_sockets (void) /* {{{ */
2287 for (i = 0; i < listen_fds_num; i++)
2289 close (listen_fds[i].fd);
2291 if (listen_fds[i].family == PF_UNIX)
2292 unlink(listen_fds[i].addr);
2300 } /* }}} int close_listen_sockets */
2302 static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
2304 struct pollfd *pollfds;
2309 if (listen_fds_num < 1)
2311 RRDD_LOG(LOG_ERR, "listen_thread_main: no listen_fds !");
2315 pollfds_num = listen_fds_num;
2316 pollfds = (struct pollfd *) malloc (sizeof (*pollfds) * pollfds_num);
2317 if (pollfds == NULL)
2319 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
2322 memset (pollfds, 0, sizeof (*pollfds) * pollfds_num);
2324 RRDD_LOG(LOG_INFO, "listening for connections");
2326 while (do_shutdown == 0)
2328 for (i = 0; i < pollfds_num; i++)
2330 pollfds[i].fd = listen_fds[i].fd;
2331 pollfds[i].events = POLLIN | POLLPRI;
2332 pollfds[i].revents = 0;
2335 status = poll (pollfds, pollfds_num, /* timeout = */ 1000);
2338 else if (status == 0) /* timeout */
2340 else if (status < 0) /* error */
2343 if (status != EINTR)
2345 RRDD_LOG (LOG_ERR, "listen_thread_main: poll(2) failed.");
2350 for (i = 0; i < pollfds_num; i++)
2352 listen_socket_t *client_sock;
2353 struct sockaddr_storage client_sa;
2354 socklen_t client_sa_size;
2356 pthread_attr_t attr;
2358 if (pollfds[i].revents == 0)
2361 if ((pollfds[i].revents & (POLLIN | POLLPRI)) == 0)
2363 RRDD_LOG (LOG_ERR, "listen_thread_main: "
2364 "poll(2) returned something unexpected for listen FD #%i.",
2369 client_sock = (listen_socket_t *) malloc (sizeof (listen_socket_t));
2370 if (client_sock == NULL)
2372 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
2375 memcpy(client_sock, &listen_fds[i], sizeof(listen_fds[0]));
2377 client_sa_size = sizeof (client_sa);
2378 client_sock->fd = accept (pollfds[i].fd,
2379 (struct sockaddr *) &client_sa, &client_sa_size);
2380 if (client_sock->fd < 0)
2382 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
2387 pthread_attr_init (&attr);
2388 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
2390 status = pthread_create (&tid, &attr, connection_thread_main,
2394 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
2395 close_connection(client_sock);
2398 } /* for (pollfds_num) */
2399 } /* while (do_shutdown == 0) */
2401 RRDD_LOG(LOG_INFO, "starting shutdown");
2403 close_listen_sockets ();
2405 pthread_mutex_lock (&connection_threads_lock);
2406 while (connection_threads_num > 0)
2407 pthread_cond_wait(&connection_threads_done, &connection_threads_lock);
2408 pthread_mutex_unlock (&connection_threads_lock);
2413 } /* }}} void *listen_thread_main */
2415 static int daemonize (void) /* {{{ */
2420 daemon_uid = geteuid();
2422 pid_fd = open_pidfile("create", O_CREAT|O_EXCL|O_WRONLY);
2424 pid_fd = check_pidfile();
2428 /* open all the listen sockets */
2429 if (config_listen_address_list_len > 0)
2431 for (size_t i = 0; i < config_listen_address_list_len; i++)
2432 open_listen_socket (config_listen_address_list[i]);
2434 rrd_free_ptrs((void ***) &config_listen_address_list,
2435 &config_listen_address_list_len);
2439 listen_socket_t sock;
2440 memset(&sock, 0, sizeof(sock));
2441 strncpy(sock.addr, RRDCACHED_DEFAULT_ADDRESS, sizeof(sock.addr)-1);
2442 open_listen_socket (&sock);
2445 if (listen_fds_num < 1)
2447 fprintf (stderr, "rrdcached: FATAL: cannot open any listen sockets\n");
2451 if (!stay_foreground)
2458 fprintf (stderr, "daemonize: fork(2) failed.\n");
2464 /* Become session leader */
2467 /* Open the first three file descriptors to /dev/null */
2472 open ("/dev/null", O_RDWR);
2473 if (dup(0) == -1 || dup(0) == -1){
2474 RRDD_LOG (LOG_ERR, "faild to run dup.\n");
2476 } /* if (!stay_foreground) */
2478 /* Change into the /tmp directory. */
2479 base_dir = (config_base_dir != NULL)
2483 if (chdir (base_dir) != 0)
2485 fprintf (stderr, "daemonize: chdir (%s) failed.\n", base_dir);
2489 install_signal_handlers();
2491 openlog ("rrdcached", LOG_PID, LOG_DAEMON);
2492 RRDD_LOG(LOG_INFO, "starting up");
2494 cache_tree = g_tree_new_full ((GCompareDataFunc) strcmp, NULL, NULL,
2495 (GDestroyNotify) free_cache_item);
2496 if (cache_tree == NULL)
2498 RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed.");
2502 return write_pidfile (pid_fd);
2507 } /* }}} int daemonize */
2509 static int cleanup (void) /* {{{ */
2513 pthread_cond_broadcast (&flush_cond);
2514 pthread_join (flush_thread, NULL);
2516 pthread_cond_broadcast (&queue_cond);
2517 for (int i = 0; i < config_queue_threads; i++)
2518 pthread_join (queue_threads[i], NULL);
2520 if (config_flush_at_shutdown)
2522 assert(cache_queue_head == NULL);
2523 RRDD_LOG(LOG_INFO, "clean shutdown; all RRDs flushed");
2529 free(queue_threads);
2530 free(config_base_dir);
2531 free(config_pid_file);
2535 pthread_mutex_lock(&cache_lock);
2536 g_tree_destroy(cache_tree);
2538 RRDD_LOG(LOG_INFO, "goodbye");
2542 } /* }}} int cleanup */
2544 static int read_options (int argc, char **argv) /* {{{ */
2549 while ((option = getopt(argc, argv, "gl:L:f:w:z:t:Bb:p:Fj:h?")) != -1)
2560 listen_socket_t *new;
2562 new = malloc(sizeof(listen_socket_t));
2565 fprintf(stderr, "read_options: malloc failed.\n");
2568 memset(new, 0, sizeof(listen_socket_t));
2570 strncpy(new->addr, optarg, sizeof(new->addr)-1);
2571 new->privilege = (option == 'l') ? PRIV_HIGH : PRIV_LOW;
2573 if (!rrd_add_ptr((void ***)&config_listen_address_list,
2574 &config_listen_address_list_len, new))
2576 fprintf(stderr, "read_options: rrd_add_ptr failed.\n");
2586 temp = atoi (optarg);
2588 config_flush_interval = temp;
2591 fprintf (stderr, "Invalid flush interval: %s\n", optarg);
2601 temp = atoi (optarg);
2603 config_write_interval = temp;
2606 fprintf (stderr, "Invalid write interval: %s\n", optarg);
2616 temp = atoi(optarg);
2618 config_write_jitter = temp;
2621 fprintf (stderr, "Invalid write jitter: -z %s\n", optarg);
2631 threads = atoi(optarg);
2633 config_queue_threads = threads;
2636 fprintf (stderr, "Invalid thread count: -t %s\n", optarg);
2643 config_write_base_only = 1;
2649 char base_realpath[PATH_MAX];
2651 if (config_base_dir != NULL)
2652 free (config_base_dir);
2653 config_base_dir = strdup (optarg);
2654 if (config_base_dir == NULL)
2656 fprintf (stderr, "read_options: strdup failed.\n");
2660 /* make sure that the base directory is not resolved via
2661 * symbolic links. this makes some performance-enhancing
2662 * assumptions possible (we don't have to resolve paths
2663 * that start with a "/")
2665 if (realpath(config_base_dir, base_realpath) == NULL)
2667 fprintf (stderr, "Invalid base directory '%s'.\n", config_base_dir);
2670 else if (strncmp(config_base_dir,
2671 base_realpath, sizeof(base_realpath)) != 0)
2674 "Base directory (-b) resolved via file system links!\n"
2675 "Please consult rrdcached '-b' documentation!\n"
2676 "Consider specifying the real directory (%s)\n",
2681 len = strlen (config_base_dir);
2682 while ((len > 0) && (config_base_dir[len - 1] == '/'))
2684 config_base_dir[len - 1] = 0;
2690 fprintf (stderr, "Invalid base directory: %s\n", optarg);
2694 _config_base_dir_len = len;
2700 if (config_pid_file != NULL)
2701 free (config_pid_file);
2702 config_pid_file = strdup (optarg);
2703 if (config_pid_file == NULL)
2705 fprintf (stderr, "read_options: strdup failed.\n");
2712 config_flush_at_shutdown = 1;
2717 struct stat statbuf;
2718 const char *dir = optarg;
2720 status = stat(dir, &statbuf);
2723 fprintf(stderr, "Cannot stat '%s' : %s\n", dir, rrd_strerror(errno));
2727 if (!S_ISDIR(statbuf.st_mode)
2728 || access(dir, R_OK|W_OK|X_OK) != 0)
2730 fprintf(stderr, "Must specify a writable directory with -j! (%s)\n",
2731 errno ? rrd_strerror(errno) : "");
2735 journal_cur = malloc(PATH_MAX + 1);
2736 journal_old = malloc(PATH_MAX + 1);
2737 if (journal_cur == NULL || journal_old == NULL)
2739 fprintf(stderr, "malloc failure for journal files\n");
2744 snprintf(journal_cur, PATH_MAX, "%s/rrd.journal", dir);
2745 snprintf(journal_old, PATH_MAX, "%s/rrd.journal.old", dir);
2752 printf ("RRDCacheD %s Copyright (C) 2008 Florian octo Forster\n"
2754 "Usage: rrdcached [options]\n"
2756 "Valid options are:\n"
2757 " -l <address> Socket address to listen to.\n"
2758 " -L <address> Socket address to listen to ('FLUSH' only).\n"
2759 " -w <seconds> Interval in which to write data.\n"
2760 " -z <delay> Delay writes up to <delay> seconds to spread load\n"
2761 " -t <threads> Number of write threads.\n"
2762 " -f <seconds> Interval in which to flush dead data.\n"
2763 " -p <file> Location of the PID-file.\n"
2764 " -b <dir> Base directory to change to.\n"
2765 " -B Restrict file access to paths within -b <dir>\n"
2766 " -g Do not fork and run in the foreground.\n"
2767 " -j <dir> Directory in which to create the journal files.\n"
2768 " -F Always flush all updates at shutdown\n"
2770 "For more information and a detailed description of all options "
2772 "to the rrdcached(1) manual page.\n",
2776 } /* switch (option) */
2777 } /* while (getopt) */
2779 /* advise the user when values are not sane */
2780 if (config_flush_interval < 2 * config_write_interval)
2781 fprintf(stderr, "WARNING: flush interval (-f) should be at least"
2782 " 2x write interval (-w) !\n");
2783 if (config_write_jitter > config_write_interval)
2784 fprintf(stderr, "WARNING: write delay (-z) should NOT be larger than"
2785 " write interval (-w) !\n");
2787 if (config_write_base_only && config_base_dir == NULL)
2788 fprintf(stderr, "WARNING: -B does not make sense without -b!\n"
2789 " Consult the rrdcached documentation\n");
2791 if (journal_cur == NULL)
2792 config_flush_at_shutdown = 1;
2795 } /* }}} int read_options */
2797 int main (int argc, char **argv)
2801 status = read_options (argc, argv);
2809 status = daemonize ();
2812 fprintf (stderr, "rrdcached: daemonize failed, exiting.\n");
2818 /* start the queue threads */
2819 queue_threads = calloc(config_queue_threads, sizeof(*queue_threads));
2820 if (queue_threads == NULL)
2822 RRDD_LOG (LOG_ERR, "FATAL: cannot calloc queue threads");
2826 for (int i = 0; i < config_queue_threads; i++)
2828 memset (&queue_threads[i], 0, sizeof (*queue_threads));
2829 status = pthread_create (&queue_threads[i], NULL, queue_thread_main, NULL);
2832 RRDD_LOG (LOG_ERR, "FATAL: cannot create queue thread");
2838 /* start the flush thread */
2839 memset(&flush_thread, 0, sizeof(flush_thread));
2840 status = pthread_create (&flush_thread, NULL, flush_thread_main, NULL);
2843 RRDD_LOG (LOG_ERR, "FATAL: cannot create flush thread");
2848 listen_thread_main (NULL);
2855 * vim: set sw=2 sts=2 ts=8 et fdm=marker :