From 72773853901832e4728e42588570d69c93976ce1 Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Thu, 30 Sep 2021 20:55:27 +1000 Subject: [PATCH] ctdb-common: Add support for reopening logs Now that CTDB uses Samba's file logging it is possible to reopen the logs, so that log rotation can be supported. Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- ctdb/common/logging.c | 75 +++++++++++++++++++++++++++++++++++++++++++ ctdb/common/logging.h | 7 ++++ 2 files changed, 82 insertions(+) diff --git a/ctdb/common/logging.c b/ctdb/common/logging.c index 296b4ab590e..1b91cdcc92b 100644 --- a/ctdb/common/logging.c +++ b/ctdb/common/logging.c @@ -667,3 +667,78 @@ int logging_init(TALLOC_CTX *mem_ctx, const char *logging, talloc_free(option); return ret; } + +bool logging_reopen_logs(void) +{ + bool status; + + status = reopen_logs_internal(); + + return status; +} + +struct logging_reopen_logs_data { + void (*hook)(void *private_data); + void *private_data; +}; + +static void logging_sig_hup_handler(struct tevent_context *ev, + struct tevent_signal *se, + int signum, + int count, + void *dont_care, + void *private_data) +{ + bool status; + + if (private_data != NULL) { + struct logging_reopen_logs_data *data = talloc_get_type_abort( + private_data, struct logging_reopen_logs_data); + + if (data->hook != NULL) { + data->hook(data->private_data); + } + } + + status = logging_reopen_logs(); + if (!status) { + D_WARNING("Failed to reopen logs\n"); + return; + } + + D_NOTICE("Reopened logs\n"); + +} + +bool logging_setup_sighup_handler(struct tevent_context *ev, + TALLOC_CTX *talloc_ctx, + void (*hook)(void *private_data), + void *private_data) +{ + struct logging_reopen_logs_data *data = NULL; + struct tevent_signal *se; + + if (hook != NULL) { + data = talloc(talloc_ctx, struct logging_reopen_logs_data); + if (data == NULL) { + return false; + } + + data->hook = hook; + data->private_data = private_data; + } + + + se = tevent_add_signal(ev, + talloc_ctx, + SIGHUP, + 0, + logging_sig_hup_handler, + data); + if (se == NULL) { + talloc_free(data); + return false; + } + + return true; +} diff --git a/ctdb/common/logging.h b/ctdb/common/logging.h index 368171fe406..542b4a3723d 100644 --- a/ctdb/common/logging.h +++ b/ctdb/common/logging.h @@ -21,6 +21,7 @@ #define __CTDB_LOGGING_H__ #include +#include #include "lib/util/debug.h" #define DEBUG_ERR DBGLVL_ERR @@ -41,4 +42,10 @@ bool logging_validate(const char *logging); int logging_init(TALLOC_CTX *mem_ctx, const char *logging, const char *debuglevel, const char *app_name); +bool logging_reopen_logs(void); +bool logging_setup_sighup_handler(struct tevent_context *ev, + TALLOC_CTX *talloc_ctx, + void (*hook)(void *private_data), + void *private_data); + #endif /* __CTDB_LOGGING_H__ */