From 33445d01b3e22d7973cd541691121ec0db5a65ef Mon Sep 17 00:00:00 2001 From: Jeff Lucovsky Date: Mon, 14 Apr 2025 16:06:08 -0400 Subject: [PATCH] output/rotate: Serialize rotation flag handling Issue: 3436 Serialize rotation flag handling to avoid corruption. --- src/output.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/output.c b/src/output.c index 11ae93c3f0..da2d661294 100644 --- a/src/output.c +++ b/src/output.c @@ -126,6 +126,8 @@ typedef struct OutputFileRolloverFlag_ { TAILQ_ENTRY(OutputFileRolloverFlag_) entries; } OutputFileRolloverFlag; +static SCMutex output_file_rotation_mutex = SCMUTEX_INITIALIZER; + TAILQ_HEAD(, OutputFileRolloverFlag_) output_file_rotation_flags = TAILQ_HEAD_INITIALIZER(output_file_rotation_flags); @@ -672,7 +674,9 @@ void OutputRegisterFileRotationFlag(int *flag) return; } flag_entry->flag = flag; + SCMutexLock(&output_file_rotation_mutex); TAILQ_INSERT_TAIL(&output_file_rotation_flags, flag_entry, entries); + SCMutexUnlock(&output_file_rotation_mutex); } /** @@ -688,25 +692,31 @@ void OutputRegisterFileRotationFlag(int *flag) void OutputUnregisterFileRotationFlag(int *flag) { OutputFileRolloverFlag *entry, *next; + SCMutexLock(&output_file_rotation_mutex); for (entry = TAILQ_FIRST(&output_file_rotation_flags); entry != NULL; entry = next) { next = TAILQ_NEXT(entry, entries); if (entry->flag == flag) { TAILQ_REMOVE(&output_file_rotation_flags, entry, entries); + SCMutexUnlock(&output_file_rotation_mutex); SCFree(entry); - break; + return; } } + SCMutexUnlock(&output_file_rotation_mutex); } /** * \brief Notifies all registered file rotation notification flags. */ void OutputNotifyFileRotation(void) { - OutputFileRolloverFlag *flag; - TAILQ_FOREACH(flag, &output_file_rotation_flags, entries) { + OutputFileRolloverFlag *flag = NULL; + OutputFileRolloverFlag *tflag; + SCMutexLock(&output_file_rotation_mutex); + TAILQ_FOREACH_SAFE (flag, &output_file_rotation_flags, entries, tflag) { *(flag->flag) = 1; } + SCMutexUnlock(&output_file_rotation_mutex); } TmEcode OutputLoggerFlush(ThreadVars *tv, Packet *p, void *thread_data)