From: Florian Forster Date: Sun, 29 Jun 2008 14:16:22 +0000 (+0200) Subject: src/rrd_daemon.c: Implement PID-file and basedir options. X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=commitdiff_plain;h=ee3699d5c7339fd1025b65e6c2d3422c197e0b80 src/rrd_daemon.c: Implement PID-file and basedir options. --- diff --git a/src/Makefile.am b/src/Makefile.am index 371ebf5..7b97fff 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -100,7 +100,7 @@ rrdtool_LDADD = librrd.la rrdcached_SOURCES = rrd_daemon.c rrdcached_DEPENDENCIES = librrd.la -rrdcached_CPPFLAGS = -DVERSION='"$(VERSION)"' +rrdcached_CPPFLAGS = -DVERSION='"$(VERSION)"' -DLOCALSTATEDIR='"$(localstatedir)"' rrdcached_LDADD = librrd.la # strftime is here because we do not usually need it. unices have propper diff --git a/src/rrd_daemon.c b/src/rrd_daemon.c index 9712fcd..ac26a27 100644 --- a/src/rrd_daemon.c +++ b/src/rrd_daemon.c @@ -148,6 +148,8 @@ static pthread_cond_t flush_cond = PTHREAD_COND_INITIALIZER; static int config_write_interval = 300; static int config_flush_interval = 3600; +static char *config_pid_file = NULL; +static char *config_base_dir = NULL; static char **config_listen_address_list = NULL; static int config_listen_address_list_len = 0; @@ -165,6 +167,46 @@ static void sig_term_handler (int s __attribute__((unused))) /* {{{ */ do_shutdown++; } /* }}} void sig_term_handler */ +static int write_pidfile (void) /* {{{ */ +{ + pid_t pid; + char *file; + FILE *fh; + + pid = getpid (); + + file = (config_pid_file != NULL) + ? config_pid_file + : LOCALSTATEDIR "/run/rrdcached.pid"; + + fh = fopen (file, "w"); + if (fh == NULL) + { + RRDD_LOG (LOG_ERR, "write_pidfile: Opening `%s' failed.", file); + return (-1); + } + + fprintf (fh, "%i\n", (int) pid); + fclose (fh); + + return (0); +} /* }}} int write_pidfile */ + +static int remove_pidfile (void) /* {{{ */ +{ + char *file; + int status; + + file = (config_pid_file != NULL) + ? config_pid_file + : LOCALSTATEDIR "/run/rrdcached.pid"; + + status = unlink (file); + if (status == 0) + return (0); + return (errno); +} /* }}} int remove_pidfile */ + /* * enqueue_cache_item: * `cache_lock' must be acquired before calling this function! @@ -1056,7 +1098,15 @@ static int daemonize (void) /* {{{ */ } /* Change into the /tmp directory. */ - chdir ("/tmp"); + base_dir = (config_base_dir != NULL) + ? config_base_dir + : "/tmp"; + status = chdir (base_dir); + if (status != 0) + { + fprintf (stderr, "daemonize: chdir (%s) failed.\n", base_dir); + return (-1); + } /* Become session leader */ setsid (); @@ -1104,6 +1154,8 @@ static int daemonize (void) /* {{{ */ return (-1); } + write_pidfile (); + return (0); } /* }}} int daemonize */ @@ -1118,6 +1170,8 @@ static int cleanup (void) /* {{{ */ pthread_join (queue_thread, /* return = */ NULL); RRDD_LOG (LOG_DEBUG, "cleanup: done"); + remove_pidfile (); + closelog (); return (0); @@ -1128,7 +1182,7 @@ static int read_options (int argc, char **argv) /* {{{ */ int option; int status = 0; - while ((option = getopt(argc, argv, "l:f:w:h?")) != -1) + while ((option = getopt(argc, argv, "l:f:w:b:p:h?")) != -1) { switch (option) { @@ -1185,6 +1239,47 @@ static int read_options (int argc, char **argv) /* {{{ */ } break; + case 'b': + { + size_t len; + + if (config_base_dir != NULL) + free (config_base_dir); + config_base_dir = strdup (optarg); + if (config_base_dir == NULL) + { + fprintf (stderr, "read_options: strdup failed.\n"); + return (3); + } + + len = strlen (config_base_dir); + while ((len > 0) && (config_base_dir[len - 1] == '/')) + { + config_base_dir[len - 1] = 0; + len--; + } + + if (len < 1) + { + fprintf (stderr, "Invalid base directory: %s\n", optarg); + return (4); + } + } + break; + + case 'p': + { + if (config_pid_file != NULL) + free (config_pid_file); + config_pid_file = strdup (optarg); + if (config_pid_file == NULL) + { + fprintf (stderr, "read_options: strdup failed.\n"); + return (3); + } + } + break; + case 'h': case '?': printf ("RRDd %s Copyright (C) 2008 Florian octo Forster\n" @@ -1195,6 +1290,8 @@ static int read_options (int argc, char **argv) /* {{{ */ " -l
Socket address to listen to.\n" " -w Interval in which to write data.\n" " -f Interval in which to flush dead data.\n" + " -p Location of the PID-file.\n" + " -b Base directory to change to.\n" "\n" "For more information and a detailed description of all options " "please refer\n"