diff --git a/src/rrd_daemon.c b/src/rrd_daemon.c
index 1c02a495cac793fe21f73e93f9af65ba47def510..a0b836487f8e9c3d3be5b5258cdd9042c3df459b 100644 (file)
--- a/src/rrd_daemon.c
+++ b/src/rrd_daemon.c
/*
* Functions
*/
-static void sig_int_handler (int s __attribute__((unused))) /* {{{ */
+static void sig_common (const char *sig) /* {{{ */
{
- RRDD_LOG(LOG_NOTICE, "caught SIGINT");
+ RRDD_LOG(LOG_NOTICE, "caught SIG%s", sig);
do_shutdown++;
pthread_cond_broadcast(&cache_cond);
+} /* }}} void sig_common */
+
+static void sig_int_handler (int s __attribute__((unused))) /* {{{ */
+{
+ sig_common("INT");
} /* }}} void sig_int_handler */
static void sig_term_handler (int s __attribute__((unused))) /* {{{ */
{
- RRDD_LOG(LOG_NOTICE, "caught SIGTERM");
- do_shutdown++;
- pthread_cond_broadcast(&cache_cond);
+ sig_common("TERM");
} /* }}} void sig_term_handler */
-static int write_pidfile (void) /* {{{ */
+static void install_signal_handlers(void) /* {{{ */
+{
+ /* These structures are static, because `sigaction' behaves weird if the are
+ * overwritten.. */
+ static struct sigaction sa_int;
+ static struct sigaction sa_term;
+ static struct sigaction sa_pipe;
+
+ /* Install signal handlers */
+ memset (&sa_int, 0, sizeof (sa_int));
+ sa_int.sa_handler = sig_int_handler;
+ sigaction (SIGINT, &sa_int, NULL);
+
+ memset (&sa_term, 0, sizeof (sa_term));
+ sa_term.sa_handler = sig_term_handler;
+ sigaction (SIGTERM, &sa_term, NULL);
+
+ memset (&sa_pipe, 0, sizeof (sa_pipe));
+ sa_pipe.sa_handler = SIG_IGN;
+ sigaction (SIGPIPE, &sa_pipe, NULL);
+
+} /* }}} void install_signal_handlers */
+
+static int open_pidfile(void) /* {{{ */
{
- pid_t pid;
- char *file;
int fd;
- FILE *fh;
+ char *file;
- pid = getpid ();
-
file = (config_pid_file != NULL)
? config_pid_file
: LOCALSTATEDIR "/run/rrdcached.pid";
fd = open(file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IRGRP|S_IROTH);
if (fd < 0)
- {
- RRDD_LOG(LOG_ERR, "FATAL: cannot create '%s' (%s)",
- file, rrd_strerror(errno));
- return (-1);
- }
+ fprintf(stderr, "FATAL: cannot create '%s' (%s)\n",
+ file, rrd_strerror(errno));
+
+ return(fd);
+}
+
+static int write_pidfile (int fd) /* {{{ */
+{
+ pid_t pid;
+ FILE *fh;
+
+ pid = getpid ();
fh = fdopen (fd, "w");
if (fh == NULL)
{
- RRDD_LOG (LOG_ERR, "write_pidfile: Opening `%s' failed.", file);
+ RRDD_LOG (LOG_ERR, "write_pidfile: fdopen() failed.");
close(fd);
return (-1);
}
if (did_insert)
{
+ pthread_cond_broadcast(&cache_cond);
pthread_mutex_lock (&stats_lock);
stats_queue_length++;
pthread_mutex_unlock (&stats_lock);
if (max_age > 0)
cfd.abs_timeout = cfd.now - max_age;
else
- cfd.abs_timeout = cfd.now + 1;
+ cfd.abs_timeout = cfd.now + 2*config_write_jitter + 1;
/* `tree_callback_flush' will return the keys of all values that haven't
* been touched in the last `config_flush_interval' seconds in `cfd'.
/* Enqueue at head */
enqueue_cache_item (ci, HEAD);
- pthread_cond_signal (&cache_cond);
pthread_cond_wait(&ci->flushed, &cache_lock);
pthread_mutex_unlock(&cache_lock);
char *help_help[] =
{
- "4 Command overview\n",
+ "5 Command overview\n",
"FLUSH <filename>\n",
+ "FLUSHALL\n",
"HELP [<command>]\n",
"UPDATE <filename> <values> [<values> ...]\n",
"STATS\n"
};
size_t help_flush_len = sizeof (help_flush) / sizeof (help_flush[0]);
+ char *help_flushall[] =
+ {
+ "3 Help for FLUSHALL\n",
+ "Usage: FLUSHALL\n",
+ "\n",
+ "Triggers writing of all pending updates. Returns immediately.\n"
+ };
+ size_t help_flushall_len = sizeof(help_flushall) / sizeof(help_flushall[0]);
+
char *help_update[] =
{
"9 Help for UPDATE\n",
help_text = help_flush;
help_text_len = help_flush_len;
}
+ else if (strcasecmp (command, "flushall") == 0)
+ {
+ help_text = help_flushall;
+ help_text_len = help_flushall_len;
+ }
else if (strcasecmp (command, "stats") == 0)
{
help_text = help_stats;
return (0);
} /* }}} int handle_request_flush */
+static int handle_request_flushall(int fd) /* {{{ */
+{
+ int status;
+ char answer[] ="0 Started flush.\n";
+
+ RRDD_LOG(LOG_DEBUG, "Received FLUSHALL");
+
+ pthread_mutex_lock(&cache_lock);
+ flush_old_values(-1);
+ pthread_mutex_unlock(&cache_lock);
+
+ status = swrite(fd, answer, strlen(answer));
+ if (status < 0)
+ {
+ status = errno;
+ RRDD_LOG(LOG_INFO, "handle_request_flushall: swrite returned an error.");
+ }
+
+ return (status);
+}
+
static int handle_request_update (int fd, /* {{{ */
char *buffer, size_t buffer_size)
{
&& (ci->values_num > 0))
{
enqueue_cache_item (ci, TAIL);
- pthread_cond_signal (&cache_cond);
}
pthread_mutex_unlock (&cache_lock);
@@ -1220,6 +1284,10 @@ static int handle_request (int fd, char *buffer, size_t buffer_size) /* {{{ */
{
return (handle_request_flush (fd, buffer_ptr, buffer_size));
}
+ else if (strcasecmp (command, "flushall") == 0)
+ {
+ return (handle_request_flushall(fd));
+ }
else if (strcasecmp (command, "stats") == 0)
{
return (handle_request_stats (fd, buffer_ptr, buffer_size));
@@ -1816,12 +1884,10 @@ static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
static int daemonize (void) /* {{{ */
{
int status;
+ int fd;
- /* These structures are static, because `sigaction' behaves weird if the are
- * overwritten.. */
- static struct sigaction sa_int;
- static struct sigaction sa_term;
- static struct sigaction sa_pipe;
+ fd = open_pidfile();
+ if (fd < 0) return fd;
if (!stay_foreground)
{
dup (0);
} /* if (!stay_foreground) */
- /* Install signal handlers */
- memset (&sa_int, 0, sizeof (sa_int));
- sa_int.sa_handler = sig_int_handler;
- sigaction (SIGINT, &sa_int, NULL);
-
- memset (&sa_term, 0, sizeof (sa_term));
- sa_term.sa_handler = sig_term_handler;
- sigaction (SIGTERM, &sa_term, NULL);
-
- memset (&sa_pipe, 0, sizeof (sa_pipe));
- sa_pipe.sa_handler = SIG_IGN;
- sigaction (SIGPIPE, &sa_pipe, NULL);
+ install_signal_handlers();
openlog ("rrdcached", LOG_PID, LOG_DAEMON);
RRDD_LOG(LOG_INFO, "starting up");
return (-1);
}
- status = write_pidfile ();
+ status = write_pidfile (fd);
return status;
} /* }}} int daemonize */