X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Frrd_daemon.c;h=f746a35c876680920dc7329aca82b26163c68ebf;hb=b70616e87e2c23d7ba1286336671946a8eeb9ccb;hp=1c02a495cac793fe21f73e93f9af65ba47def510;hpb=a78dbfc073725a76ca28f8ae9f7a7ae9df34d2d3;p=rrdtool.git diff --git a/src/rrd_daemon.c b/src/rrd_daemon.c index 1c02a49..f746a35 100644 --- a/src/rrd_daemon.c +++ b/src/rrd_daemon.c @@ -209,31 +209,34 @@ static void sig_term_handler (int s __attribute__((unused))) /* {{{ */ pthread_cond_broadcast(&cache_cond); } /* }}} void sig_term_handler */ -static int write_pidfile (void) /* {{{ */ +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); } @@ -425,6 +428,7 @@ static int enqueue_cache_item (cache_item_t *ci, /* {{{ */ if (did_insert) { + pthread_cond_broadcast(&cache_cond); pthread_mutex_lock (&stats_lock); stats_queue_length++; pthread_mutex_unlock (&stats_lock); @@ -497,7 +501,7 @@ static int flush_old_values (int max_age) 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'. @@ -747,7 +751,6 @@ static int flush_file (const char *filename) /* {{{ */ /* 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); @@ -1132,7 +1135,6 @@ static int handle_request_update (int fd, /* {{{ */ && (ci->values_num > 0)) { enqueue_cache_item (ci, TAIL); - pthread_cond_signal (&cache_cond); } pthread_mutex_unlock (&cache_lock); @@ -1816,6 +1818,7 @@ 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.. */ @@ -1823,6 +1826,9 @@ static int daemonize (void) /* {{{ */ static struct sigaction sa_term; static struct sigaction sa_pipe; + fd = open_pidfile(); + if (fd < 0) return fd; + if (!stay_foreground) { pid_t child; @@ -1886,7 +1892,7 @@ static int daemonize (void) /* {{{ */ return (-1); } - status = write_pidfile (); + status = write_pidfile (fd); return status; } /* }}} int daemonize */