From 1cc8ebb7828721874c414dc8d7cb410a4d9fd884 Mon Sep 17 00:00:00 2001 From: Roger Light Date: Wed, 13 Oct 2021 00:01:27 +0100 Subject: [PATCH] Add support for sending the SIGRTMIN signal to trigger log rotation. Closes #2337. Thanks to Evgeny S. --- ChangeLog.txt | 2 ++ man/mosquitto.8.xml | 12 +++++++ src/loop.c | 38 +---------------------- src/mosquitto_broker_internal.h | 1 + src/signals.c | 55 +++++++++++++++++++++++++++++++-- 5 files changed, 68 insertions(+), 40 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 7c33e678..ee243c30 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -80,6 +80,8 @@ Broker: - Add more efficient keepalive check. - Send DISCONNECT With session-takeover return code to MQTT v5 clients when a client connects with the same client id. Closes #2340. +- Add support for sending the SIGRTMIN signal to trigger log rotation. + Closes #2337. Client library: - Add MOSQ_OPT_DISABLE_SOCKETPAIR to allow the disabling of the socketpair diff --git a/man/mosquitto.8.xml b/man/mosquitto.8.xml index 584c9605..a01e680e 100644 --- a/man/mosquitto.8.xml +++ b/man/mosquitto.8.xml @@ -693,6 +693,18 @@ for details. If TLS certificates are in use, then mosquitto will also reload certificate on receiving a SIGHUP. + + The logs will also be closed and reopened. + + + + + SIGRTMIN + + + Upon receiving the SIGRTMIN signal, mosquitto will + close and reopen the logs to support log rotation. + diff --git a/src/loop.c b/src/loop.c index b12219e7..a5362e56 100644 --- a/src/loop.c +++ b/src/loop.c @@ -55,11 +55,6 @@ Contributors: #include "time_mosq.h" #include "util_mosq.h" -extern bool flag_reload; -#ifdef WITH_PERSISTENCE -extern bool flag_db_backup; -#endif -extern bool flag_tree_print; extern int g_run; #if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_WEBSOCKETS && LWS_LIBRARY_VERSION_NUMBER == 3002000 @@ -231,38 +226,7 @@ int mosquitto_main_loop(struct mosquitto__listener_sock *listensock, int listens } #endif -#ifdef WITH_PERSISTENCE - if(flag_db_backup){ - persist__backup(false); - flag_db_backup = false; - } -#endif - if(flag_reload){ - log__printf(NULL, MOSQ_LOG_INFO, "Reloading config."); - config__read(db.config, true); - listeners__reload_all_certificates(); - mosquitto_security_cleanup(true); - mosquitto_security_init(true); - mosquitto_security_apply(); - log__close(db.config); - log__init(db.config); - keepalive__cleanup(); - keepalive__init(); -#ifdef WITH_CJSON - broker_control__reload(); -#endif -#ifdef WITH_BRIDGE - bridge__reload(); -#endif - flag_reload = false; - } - if(flag_tree_print){ - sub__tree_print(db.subs, 0); - flag_tree_print = false; -#ifdef WITH_XTREPORT - xtreport(); -#endif - } + signal__flag_check(); #if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_LWS for(i=0; ilistener_count; i++){ /* Extremely hacky, should be using the lws provided external poll diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index c734cf23..cc2c2afa 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -896,6 +896,7 @@ void session_expiry__send_all(void); * Signals * ============================================================ */ void signal__setup(void); +void signal__flag_check(void); /* ============================================================ * Window service and signal related functions diff --git a/src/signals.c b/src/signals.c index 99abe0f9..dbf07c15 100644 --- a/src/signals.c +++ b/src/signals.c @@ -33,11 +33,12 @@ Contributors: extern int g_run; -bool flag_reload = false; +static bool flag_reload = false; +static bool flag_log_rotate = false; #ifdef WITH_PERSISTENCE -bool flag_db_backup = false; +static bool flag_db_backup = false; #endif -bool flag_tree_print = false; +static bool flag_tree_print = false; static void handle_signal(int signal) { @@ -55,6 +56,10 @@ static void handle_signal(int signal) #endif }else if(signal == SIGUSR2){ flag_tree_print = true; +#ifdef SIGRTMIN + }else if(signal == SIGRTMIN){ + flag_log_rotate = true; +#endif } } @@ -71,11 +76,55 @@ void signal__setup(void) signal(SIGUSR2, handle_signal); signal(SIGPIPE, SIG_IGN); #endif +#ifdef SIGRTMIN + signal(SIGRTMIN, handle_signal); +#endif #ifdef WIN32 CreateThread(NULL, 0, SigThreadProc, NULL, 0, NULL); #endif } +void signal__flag_check(void) +{ +#ifdef WITH_PERSISTENCE + if(flag_db_backup){ + persist__backup(false); + flag_db_backup = false; + } +#endif + if(flag_log_rotate){ + log__close(db.config); + log__init(db.config); + flag_log_rotate = false; + } + if(flag_reload){ + log__printf(NULL, MOSQ_LOG_INFO, "Reloading config."); + config__read(db.config, true); + listeners__reload_all_certificates(); + mosquitto_security_cleanup(true); + mosquitto_security_init(true); + mosquitto_security_apply(); + log__close(db.config); + log__init(db.config); + keepalive__cleanup(); + keepalive__init(); +#ifdef WITH_CJSON + broker_control__reload(); +#endif +#ifdef WITH_BRIDGE + bridge__reload(); +#endif + flag_reload = false; + } + if(flag_tree_print){ + sub__tree_print(db.subs, 0); + flag_tree_print = false; +#ifdef WITH_XTREPORT + xtreport(); +#endif + } +} + /* * * Signalling mosquitto process on Win32.