From fffdc6e3fdbf9e3912ec5e5faf731554f1dcc90f Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Tue, 8 Nov 2016 12:02:23 -0500 Subject: [PATCH] logging: hook the application log file into rotation --- src/util-debug.c | 30 ++++++++++++++++++++++++++++++ src/util-debug.h | 3 +++ 2 files changed, 33 insertions(+) diff --git a/src/util-debug.c b/src/util-debug.c index b4a13214a2..0bf067c863 100644 --- a/src/util-debug.c +++ b/src/util-debug.c @@ -34,6 +34,7 @@ #include "detect.h" #include "packet-queue.h" #include "threadvars.h" +#include "output.h" #include "tm-queuehandlers.h" #include "tm-queues.h" @@ -154,6 +155,11 @@ static inline int SCLogMapLogLevelToSyslogLevel(int log_level) */ static inline void SCLogPrintToStream(FILE *fd, char *msg) { + /* Would only happen if the log file failed to re-open during rotation. */ + if (fd == NULL) { + return; + } + #if defined (OS_WIN32) SCMutexLock(&sc_log_stream_lock); #endif /* OS_WIN32 */ @@ -515,6 +521,24 @@ static SCError SCLogMessageGetBuffer( return SC_ERR_SPRINTF; } +static void SCLogReopen(SCLogOPIfaceCtx *op_iface_ctx) +{ + if (op_iface_ctx->file_d == NULL) { + return; + } + + if (op_iface_ctx->file == NULL) { + return; + } + + fclose(op_iface_ctx->file_d); + op_iface_ctx->file_d = fopen(op_iface_ctx->file, "a"); + if (op_iface_ctx->file_d == NULL) { + SCLogError(SC_ERR_FOPEN, "Erroring re-opening file \"%s\": %s", + op_iface_ctx->file, strerror(errno)); + } +} + /** * \brief Adds the global log_format to the outgoing buffer * @@ -569,6 +593,10 @@ SCError SCLogMessage(const SCLogLevel log_level, const char *file, log_level, file, line, function, error_code, message) == 0) { + if (op_iface_ctx->rotation_flag) { + SCLogReopen(op_iface_ctx); + op_iface_ctx->rotation_flag = 0; + } SCLogPrintToStream(op_iface_ctx->file_d, buffer); } break; @@ -702,6 +730,8 @@ static inline SCLogOPIfaceCtx *SCLogInitFileOPIface(const char *file, goto error; } + OutputRegisterFileRotationFlag(&iface_ctx->rotation_flag); + iface_ctx->log_level = log_level; return iface_ctx; diff --git a/src/util-debug.h b/src/util-debug.h index 48d9eee956..46157763e1 100644 --- a/src/util-debug.h +++ b/src/util-debug.h @@ -127,6 +127,9 @@ typedef struct SCLogOPIfaceCtx_ { /* the output file descriptor for the above file */ FILE * file_d; + /* registered to be set on a file rotation signal */ + int rotation_flag; + /* the facility code if the interface is SC_LOG_IFACE_SYSLOG */ int facility;