From f43659a976b735d98b888578b6239053c557008c Mon Sep 17 00:00:00 2001 From: oetiker Date: Fri, 26 Sep 2008 05:11:32 +0000 Subject: [PATCH] create the pid file before forking, so we can complain to stderr -- kevin brintnall git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk@1528 a5681a0c-68f1-0310-ab6d-d61299d08faa --- program/src/rrd_daemon.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/program/src/rrd_daemon.c b/program/src/rrd_daemon.c index 1c02a495..d30893ed 100644 --- a/program/src/rrd_daemon.c +++ b/program/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: fopen() failed."); close(fd); return (-1); } @@ -1816,6 +1819,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 +1827,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 +1893,7 @@ static int daemonize (void) /* {{{ */ return (-1); } - status = write_pidfile (); + status = write_pidfile (fd); return status; } /* }}} int daemonize */ -- 2.30.2