From 7369674787d91c40f8eff7bc71f6d73dab341315 Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Wed, 12 Dec 2018 12:45:11 +0100 Subject: [PATCH] debug: factor out logfile size check The new function will also be used for upcoming per-debug-class logfiles. Signed-off-by: Ralph Boehme Reviewed-by: Jeremy Allison --- lib/util/debug.c | 73 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 19 deletions(-) diff --git a/lib/util/debug.c b/lib/util/debug.c index 4318bffa732..2174add2a6c 100644 --- a/lib/util/debug.c +++ b/lib/util/debug.c @@ -1174,10 +1174,61 @@ bool need_to_check_log_size(void) Check to see if the log has grown to be too big. **************************************************************************/ -void check_log_size( void ) +static void do_one_check_log_size(off_t maxlog, int *_fd, const char *logfile) { - int maxlog; + char name[strlen(logfile) + 5]; struct stat st; + int fd = *_fd; + int ret; + bool ok; + + if (maxlog == 0) { + return; + } + + ret = fstat(fd, &st); + if (ret != 0) { + return; + } + if (st.st_size < maxlog ) { + return; + } + + /* reopen_logs_internal() modifies *_fd */ + (void)reopen_logs_internal(); + fd = *_fd; + + if (fd <= 2) { + return; + } + ret = fstat(fd, &st); + if (ret != 0) { + return; + } + if (st.st_size < maxlog) { + return; + } + + snprintf(name, sizeof(name), "%s.old", logfile); + + (void)rename(logfile, name); + + ok = reopen_logs_internal(); + if (ok) { + return; + } + /* We failed to reopen a log - continue using the old name. */ + (void)rename(name, logfile); +} + +static void do_check_log_size(off_t maxlog) +{ + do_one_check_log_size(maxlog, &state.fd, state.debugf); +} + +void check_log_size( void ) +{ + off_t maxlog; /* * We need to be root to check/change log-file, skip this and let the main @@ -1207,23 +1258,7 @@ void check_log_size( void ) (void)reopen_logs_internal(); } - if (maxlog && (fstat(state.fd, &st) == 0 - && st.st_size > maxlog )) { - (void)reopen_logs_internal(); - if (state.fd > 2 && (fstat(state.fd, &st) == 0 - && st.st_size > maxlog)) { - char name[strlen(state.debugf) + 5]; - - snprintf(name, sizeof(name), "%s.old", state.debugf); - - (void)rename(state.debugf, name); - - if (!reopen_logs_internal()) { - /* We failed to reopen a log - continue using the old name. */ - (void)rename(name, state.debugf); - } - } - } + do_check_log_size(maxlog); /* * Here's where we need to panic if state.fd == 0 or -1 (invalid values) -- 2.34.1