Moved signal handler setup out of daemonize(). Coalesced common code
authoroetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa>
Sun, 28 Sep 2008 19:25:40 +0000 (19:25 +0000)
committeroetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa>
Sun, 28 Sep 2008 19:25:40 +0000 (19:25 +0000)
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

doc/rrdcached.pod
src/rrd_daemon.c

index 9254888..d8d88c5 100644 (file)
@@ -415,6 +415,16 @@ Number of times the journal has been rotated since startup.
 
 =back
 
 
 =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.
 =head1 BUGS
 
 No known bugs at the moment.
index 0e29f13..a0b8364 100644 (file)
@@ -195,20 +195,46 @@ static void journal_rotate(void);
 /* 
  * Functions
  */
 /* 
  * 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);
   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))) /* {{{ */
 {
 } /* }}} 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 */
 
 } /* }}} 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;
 static int open_pidfile(void) /* {{{ */
 {
   int fd;
@@ -1860,12 +1886,6 @@ static int daemonize (void) /* {{{ */
   int status;
   int fd;
 
   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;
 
   fd = open_pidfile();
   if (fd < 0) return fd;
 
@@ -1909,18 +1929,7 @@ static int daemonize (void) /* {{{ */
     dup (0);
   } /* 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");
 
   openlog ("rrdcached", LOG_PID, LOG_DAEMON);
   RRDD_LOG(LOG_INFO, "starting up");