dovecot-1.0: Allow registering signal handlers even before lib_s...

dovecot at dovecot.org dovecot at dovecot.org
Wed Jun 27 23:12:39 EEST 2007


details:   http://hg.dovecot.org/dovecot-1.0/rev/a098e94cd318
changeset: 5329:a098e94cd318
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Jun 27 23:12:34 2007 +0300
description:
Allow registering signal handlers even before lib_signals_init() is called.
The signals won't be effective until then though, unless they're ignored.

diffstat:

1 file changed, 36 insertions(+), 19 deletions(-)
src/lib/lib-signals.c |   55 ++++++++++++++++++++++++++++++++-----------------

diffs (94 lines):

diff -r 5d841dcab6ab -r a098e94cd318 src/lib/lib-signals.c
--- a/src/lib/lib-signals.c	Wed Jun 27 22:46:15 2007 +0300
+++ b/src/lib/lib-signals.c	Wed Jun 27 23:12:34 2007 +0300
@@ -21,10 +21,11 @@ struct signal_handler {
 /* Remember that these are accessed inside signal handler which may be called
    even while we're initializing/deinitializing. Try hard to keep everything
    in consistent state. */
-static struct signal_handler *signal_handlers[MAX_SIGNAL_VALUE+1];
-static int sig_pipe_fd[2];
-
-static struct io *io_sig;
+static struct signal_handler *signal_handlers[MAX_SIGNAL_VALUE+1] = { NULL, };
+static int sig_pipe_fd[2] = { -1, -1 };
+
+static bool signals_initialized = FALSE;
+static struct io *io_sig = NULL;
 
 static void sig_handler(int signo)
 {
@@ -99,6 +100,18 @@ static void signal_read(void *context __
 	}
 }
 
+static void lib_signals_set(int signo, bool ignore)
+{
+	struct sigaction act;
+
+	if (sigemptyset(&act.sa_mask) < 0)
+		i_fatal("sigemptyset(): %m");
+	act.sa_flags = 0;
+	act.sa_handler = ignore ? sig_ignore : sig_handler;
+	if (sigaction(signo, &act, NULL) < 0)
+		i_fatal("sigaction(%d): %m", signo);
+}
+
 void lib_signals_set_handler(int signo, bool delayed,
 			     signal_handler_t *handler, void *context)
 {
@@ -109,17 +122,10 @@ void lib_signals_set_handler(int signo, 
 			signo, MAX_SIGNAL_VALUE);
 	}
 
-	if (signal_handlers[signo] == NULL) {
+	if (signal_handlers[signo] == NULL &&
+	    (handler == NULL || signals_initialized)) {
 		/* first handler for this signal */
-		struct sigaction act;
-
-		if (sigemptyset(&act.sa_mask) < 0)
-			i_fatal("sigemptyset(): %m");
-		act.sa_flags = 0;
-		act.sa_handler = handler != NULL ? sig_handler : sig_ignore;
-		if (sigaction(signo, &act, NULL) < 0)
-			i_fatal("sigaction(%d): %m", signo);
-
+		lib_signals_set(signo, handler == NULL);
 		if (handler == NULL) {
 			/* we're ignoring the handler, just return */
 			return;
@@ -133,7 +139,10 @@ void lib_signals_set_handler(int signo, 
 			i_fatal("pipe() failed: %m");
 		fd_close_on_exec(sig_pipe_fd[0], TRUE);
 		fd_close_on_exec(sig_pipe_fd[1], TRUE);
-		io_sig = io_add(sig_pipe_fd[0], IO_READ, signal_read, NULL);
+		if (signals_initialized) {
+			io_sig = io_add(sig_pipe_fd[0], IO_READ,
+					signal_read, NULL);
+		}
 	}
 
 	h = i_new(struct signal_handler, 1);
@@ -186,10 +195,18 @@ void lib_signals_unset_handler(int signo
 
 void lib_signals_init(void)
 {
-        sig_pipe_fd[0] = sig_pipe_fd[1] = -1;
-	io_sig = NULL;
-
-	memset(signal_handlers, 0, sizeof(signal_handlers));
+	int i;
+
+	signals_initialized = TRUE;
+
+	/* add signals that were already registered */
+	for (i = 0; i < MAX_SIGNAL_VALUE; i++) {
+		if (signal_handlers[i] != NULL)
+			lib_signals_set(i, FALSE);
+	}
+
+	if (sig_pipe_fd[0] != -1)
+		io_sig = io_add(sig_pipe_fd[0], IO_READ, signal_read, NULL);
 }
 
 void lib_signals_deinit(void)


More information about the dovecot-cvs mailing list