From f35b811b86f1b0212a609b7727f947d25b0cc273 Mon Sep 17 00:00:00 2001 From: Quentin Armitage Date: Mon, 16 Nov 2015 16:15:35 +0000 Subject: Streamline signal handling code There was some duplication of the code for signal handling, and this slight restructuring avoids the duplication and makes it simpler. Signed-off-by: Quentin Armitage (cherry picked from commit 4f03193e1494cd7348c953a63f3d2dd831daaecf) --- keepalived/check/check_daemon.c | 3 +-- keepalived/core/main.c | 4 ++-- keepalived/vrrp/vrrp_daemon.c | 6 +++--- lib/signals.c | 48 ++++++++++++++--------------------------- lib/signals.h | 5 ++--- 5 files changed, 24 insertions(+), 42 deletions(-) diff --git a/keepalived/check/check_daemon.c b/keepalived/check/check_daemon.c index 6e72ed1..4b80b0c 100644 --- a/keepalived/check/check_daemon.c +++ b/keepalived/check/check_daemon.c @@ -192,8 +192,7 @@ reload_check_thread(thread_t * thread) log_message(LOG_INFO, "Got SIGHUP, reloading checker configuration"); /* Signals handling */ - signal_reset(); - signal_handler_destroy(); + signal_handler_reset(); /* Destroy master thread */ #ifdef _WITH_VRRP_ diff --git a/keepalived/core/main.c b/keepalived/core/main.c index 1dc3654..95628dc 100644 --- a/keepalived/core/main.c +++ b/keepalived/core/main.c @@ -89,7 +89,7 @@ start_keepalived(void) } /* SIGHUP/USR1/USR2 handler */ -void +static void propogate_signal(void *v, int sig) { /* Signal child process */ @@ -100,7 +100,7 @@ propogate_signal(void *v, int sig) } /* Terminate handler */ -void +static void sigend(void *v, int sig) { int status; diff --git a/keepalived/vrrp/vrrp_daemon.c b/keepalived/vrrp/vrrp_daemon.c index 8b7202b..2e876a1 100644 --- a/keepalived/vrrp/vrrp_daemon.c +++ b/keepalived/vrrp/vrrp_daemon.c @@ -243,8 +243,7 @@ reload_vrrp_thread(thread_t * thread) SET_RELOAD; /* Signal handling */ - signal_reset(); - signal_handler_destroy(); + signal_handler_reset(); /* Destroy master thread */ vrrp_dispatcher_release(vrrp_data); @@ -349,6 +348,8 @@ start_vrrp_child(void) return 0; } + signal_handler_destroy(); + /* Opening local VRRP syslog channel */ openlog(PROG_VRRP, LOG_PID | ((__test_bit(LOG_CONSOLE_BIT, &debug)) ? LOG_CONS : 0) , (log_facility==LOG_DAEMON) ? LOG_LOCAL1 : log_facility); @@ -361,7 +362,6 @@ start_vrrp_child(void) } /* Create the new master thread */ - signal_handler_destroy(); thread_destroy_master(master); master = thread_make_master(); diff --git a/lib/signals.c b/lib/signals.c index 51d5cbc..8372ec9 100644 --- a/lib/signals.c +++ b/lib/signals.c @@ -51,6 +51,7 @@ void *signal_SIGUSR2_v; static int signal_pipe[2] = { -1, -1 }; /* Local signal test */ +/* Currently unused int signal_pending(void) { @@ -65,9 +66,10 @@ signal_pending(void) return rc>0?1:0; } +*/ /* Signal flag */ -void +static void signal_handler(int sig) { if (write(signal_pipe[1], &sig, sizeof(int)) != sizeof(int)) { @@ -196,52 +198,34 @@ signal_handler_init(void) } } -void -signal_wait_handlers(void) +static void +signal_handlers_clear(void *state) { - struct sigaction sig; - - sig.sa_handler = SIG_DFL; - sigemptyset(&sig.sa_mask); - sig.sa_flags = 0; - /* Ensure no more pending signals */ - sigaction(SIGHUP, &sig, NULL); - sigaction(SIGINT, &sig, NULL); - sigaction(SIGTERM, &sig, NULL); - sigaction(SIGCHLD, &sig, NULL); - sigaction(SIGUSR1, &sig, NULL); - sigaction(SIGUSR2, &sig, NULL); + signal_set(SIGHUP, state, NULL); + signal_set(SIGINT, state, NULL); + signal_set(SIGTERM, state, NULL); + signal_set(SIGCHLD, state, NULL); + signal_set(SIGUSR1, state, NULL); + signal_set(SIGUSR2, state, NULL); - /* reset */ - signal_SIGHUP_v = NULL; - signal_SIGINT_v = NULL; - signal_SIGTERM_v = NULL; - signal_SIGCHLD_v = NULL; - signal_SIGUSR1_v = NULL; - signal_SIGUSR2_v = NULL; } -void signal_reset(void) +void +signal_handler_reset(void) { - signal_wait_handlers(); - signal_SIGHUP_handler = NULL; - signal_SIGINT_handler = NULL; - signal_SIGTERM_handler = NULL; - signal_SIGCHLD_handler = NULL; - signal_SIGUSR1_handler = NULL; - signal_SIGUSR2_handler = NULL; + signal_handlers_clear(SIG_DFL); } void signal_handler_destroy(void) { - signal_wait_handlers(); + signal_handlers_clear(SIG_IGN); close(signal_pipe[1]); close(signal_pipe[0]); signal_pipe[1] = -1; signal_pipe[0] = -1; -} +} int signal_rfd(void) diff --git a/lib/signals.h b/lib/signals.h index 59fac4a..8e28ee7 100644 --- a/lib/signals.h +++ b/lib/signals.h @@ -25,14 +25,13 @@ #define _SIGNALS_H /* Prototypes */ -extern int signal_pending(void); +/* Currently unused extern int signal_pending(void); */ extern void *signal_set(int signo, void (*func) (void *, int), void *); extern void *signal_ignore(int signo); extern void signal_handler_init(void); extern void signal_handler_destroy(void); -extern void signal_reset(void); +extern void signal_handler_reset(void); extern void signal_run_callback(void); -extern void signal_wait_handlers(void); extern int signal_rfd(void); -- 1.7.12.1