From: oetiker Date: Sun, 28 Sep 2008 19:25:40 +0000 (+0000) Subject: Moved signal handler setup out of daemonize(). Coalesced common code X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=commitdiff_plain;h=afdd543a34762dba360a103e9b17f38b64be83ff Moved signal handler setup out of daemonize(). Coalesced common code in preparation for new signals. Documented behavior of existing signals. -- kevin brintnall git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk/program@1539 a5681a0c-68f1-0310-ab6d-d61299d08faa --- diff --git a/doc/rrdcached.pod b/doc/rrdcached.pod index 9254888..d8d88c5 100644 --- a/doc/rrdcached.pod +++ b/doc/rrdcached.pod @@ -415,6 +415,16 @@ Number of times the journal has been rotated since startup. =back +=head1 SIGNALS + +=over 4 + +=item SIGINT and SIGTERM + +The daemon exits normally on receipt of either of these signals. + +=back + =head1 BUGS No known bugs at the moment. diff --git a/src/rrd_daemon.c b/src/rrd_daemon.c index 0e29f13..a0b8364 100644 --- a/src/rrd_daemon.c +++ b/src/rrd_daemon.c @@ -195,20 +195,46 @@ static void journal_rotate(void); /* * 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 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) /* {{{ */ { int fd; @@ -1860,12 +1886,6 @@ 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; @@ -1909,18 +1929,7 @@ static int daemonize (void) /* {{{ */ 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");