output/rotate: Serialize rotation flag handling

Issue: 3436

Serialize rotation flag handling to avoid corruption.
pull/13035/head
Jeff Lucovsky 1 year ago committed by Victor Julien
parent bda0890834
commit 33445d01b3

@ -126,6 +126,8 @@ typedef struct OutputFileRolloverFlag_ {
TAILQ_ENTRY(OutputFileRolloverFlag_) entries; TAILQ_ENTRY(OutputFileRolloverFlag_) entries;
} OutputFileRolloverFlag; } OutputFileRolloverFlag;
static SCMutex output_file_rotation_mutex = SCMUTEX_INITIALIZER;
TAILQ_HEAD(, OutputFileRolloverFlag_) output_file_rotation_flags = TAILQ_HEAD(, OutputFileRolloverFlag_) output_file_rotation_flags =
TAILQ_HEAD_INITIALIZER(output_file_rotation_flags); TAILQ_HEAD_INITIALIZER(output_file_rotation_flags);
@ -672,7 +674,9 @@ void OutputRegisterFileRotationFlag(int *flag)
return; return;
} }
flag_entry->flag = flag; flag_entry->flag = flag;
SCMutexLock(&output_file_rotation_mutex);
TAILQ_INSERT_TAIL(&output_file_rotation_flags, flag_entry, entries); 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) void OutputUnregisterFileRotationFlag(int *flag)
{ {
OutputFileRolloverFlag *entry, *next; OutputFileRolloverFlag *entry, *next;
SCMutexLock(&output_file_rotation_mutex);
for (entry = TAILQ_FIRST(&output_file_rotation_flags); entry != NULL; for (entry = TAILQ_FIRST(&output_file_rotation_flags); entry != NULL;
entry = next) { entry = next) {
next = TAILQ_NEXT(entry, entries); next = TAILQ_NEXT(entry, entries);
if (entry->flag == flag) { if (entry->flag == flag) {
TAILQ_REMOVE(&output_file_rotation_flags, entry, entries); TAILQ_REMOVE(&output_file_rotation_flags, entry, entries);
SCMutexUnlock(&output_file_rotation_mutex);
SCFree(entry); SCFree(entry);
break; return;
} }
} }
SCMutexUnlock(&output_file_rotation_mutex);
} }
/** /**
* \brief Notifies all registered file rotation notification flags. * \brief Notifies all registered file rotation notification flags.
*/ */
void OutputNotifyFileRotation(void) { void OutputNotifyFileRotation(void) {
OutputFileRolloverFlag *flag; OutputFileRolloverFlag *flag = NULL;
TAILQ_FOREACH(flag, &output_file_rotation_flags, entries) { OutputFileRolloverFlag *tflag;
SCMutexLock(&output_file_rotation_mutex);
TAILQ_FOREACH_SAFE (flag, &output_file_rotation_flags, entries, tflag) {
*(flag->flag) = 1; *(flag->flag) = 1;
} }
SCMutexUnlock(&output_file_rotation_mutex);
} }
TmEcode OutputLoggerFlush(ThreadVars *tv, Packet *p, void *thread_data) TmEcode OutputLoggerFlush(ThreadVars *tv, Packet *p, void *thread_data)

Loading…
Cancel
Save