From 7857e5cedbb4182e9e133374e26fbcfab77675f8 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 12 Oct 2021 23:56:10 +0100 Subject: [PATCH] Simplify signal handling. --- src/mosquitto.c | 23 ------------- src/mosquitto_broker_internal.h | 7 +--- src/signals.c | 60 +++++++++++++++++---------------- 3 files changed, 32 insertions(+), 58 deletions(-) diff --git a/src/mosquitto.c b/src/mosquitto.c index 7af8504c..ecab4587 100644 --- a/src/mosquitto.c +++ b/src/mosquitto.c @@ -61,11 +61,6 @@ struct mosquitto_db db; struct mosquitto__listener_sock *g_listensock = NULL; int g_listensock_count = 0; -bool flag_reload = false; -#ifdef WITH_PERSISTENCE -bool flag_db_backup = false; -#endif -bool flag_tree_print = false; int g_run = 0; #ifdef WITH_WRAP #include @@ -168,24 +163,6 @@ static void mosquitto__daemonise(void) } -static void signal__setup(void) -{ - signal(SIGINT, handle_sigint); - signal(SIGTERM, handle_sigint); -#ifdef SIGHUP - signal(SIGHUP, handle_sighup); -#endif -#ifndef WIN32 - signal(SIGUSR1, handle_sigusr1); - signal(SIGUSR2, handle_sigusr2); - signal(SIGPIPE, SIG_IGN); -#endif -#ifdef WIN32 - CreateThread(NULL, 0, SigThreadProc, NULL, 0, NULL); -#endif -} - - static int pid__write(void) { FILE *pid; diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index 23976dab..c734cf23 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -895,12 +895,7 @@ void session_expiry__send_all(void); /* ============================================================ * Signals * ============================================================ */ -void handle_sigint(int signal); -void handle_sigusr1(int signal); -void handle_sigusr2(int signal); -#ifdef SIGHUP -void handle_sighup(int signal); -#endif +void signal__setup(void); /* ============================================================ * Window service and signal related functions diff --git a/src/signals.c b/src/signals.c index 6fa9d1f6..99abe0f9 100644 --- a/src/signals.c +++ b/src/signals.c @@ -31,47 +31,49 @@ Contributors: #include "mosquitto_broker_internal.h" -#ifdef WITH_PERSISTENCE -extern bool flag_db_backup; -#endif -extern bool flag_reload; -extern bool flag_tree_print; extern int g_run; -#ifdef SIGHUP -/* Signal handler for SIGHUP - flag a config reload. */ -void handle_sighup(int signal) -{ - UNUSED(signal); - - flag_reload = true; -} +bool flag_reload = false; +#ifdef WITH_PERSISTENCE +bool flag_db_backup = false; #endif +bool flag_tree_print = false; -/* Signal handler for SIGINT and SIGTERM - just stop gracefully. */ -void handle_sigint(int signal) -{ - UNUSED(signal); - - g_run = 0; -} - -/* Signal handler for SIGUSR1 - backup the db. */ -void handle_sigusr1(int signal) +static void handle_signal(int signal) { UNUSED(signal); + if(signal == SIGINT || signal == SIGTERM){ + g_run = 0; +#ifdef SIGHUP + }else if(signal == SIGHUP){ + flag_reload = true; +#endif + }else if(signal == SIGUSR1){ #ifdef WITH_PERSISTENCE - flag_db_backup = true; + flag_db_backup = true; #endif + }else if(signal == SIGUSR2){ + flag_tree_print = true; + } } -/* Signal handler for SIGUSR2 - print subscription / retained tree. */ -void handle_sigusr2(int signal) -{ - UNUSED(signal); - flag_tree_print = true; +void signal__setup(void) +{ + signal(SIGINT, handle_signal); + signal(SIGTERM, handle_signal); +#ifdef SIGHUP + signal(SIGHUP, handle_signal); +#endif +#ifndef WIN32 + signal(SIGUSR1, handle_signal); + signal(SIGUSR2, handle_signal); + signal(SIGPIPE, SIG_IGN); +#endif +#ifdef WIN32 + CreateThread(NULL, 0, SigThreadProc, NULL, 0, NULL); +#endif } /*