[rrd-developers] [PATCH] moved signal handler setup out of daemonize(), to install_signal_handlers()

kevin brintnall kbrint at rufus.net
Sat Sep 27 00:28:31 CEST 2008


Moved signal handler setup out of daemonize().  Coalesced common code
in preparation for new signals.  Documented behavior of existing signals.

---
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 3ff9a9e..79425d2 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;
@@ -1863,12 +1889,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;
 
@@ -1912,18 +1932,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");



More information about the rrd-developers mailing list