From d837562441fae6f5b3a246f26b73654d687b71d3 Mon Sep 17 00:00:00 2001 From: Eric Leblond Date: Wed, 27 May 2015 22:05:19 +0200 Subject: [PATCH] logging: fix modules ordering during logging With the previous code the order of the logging modules in the YAML were determining which module was run first. This was not wished and a consequences was that the EVE fileinfo module was not correctly displaying the key 'stored' because it was depending on a flag set alter by the filestore module. This patch adds a priority file to the TmModule structure. The higher the priority is set, the sooner the module is run in the logging process. The RunModeOutput structure has also been updated to contain the name of the original TmModule. Thus allowing to define a priority for a RunModeOutput. Currently only the filestore has a priority set. The rest of them is set to the default value of zero. --- src/log-filestore.c | 1 + src/runmodes.c | 45 +++++++++++++++++++++++++++++++++++++++------ src/tm-modules.h | 2 ++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/log-filestore.c b/src/log-filestore.c index 51ad673b9e..3caee3b7ff 100644 --- a/src/log-filestore.c +++ b/src/log-filestore.c @@ -485,6 +485,7 @@ void TmModuleLogFilestoreRegister (void) tmm_modules[TMM_FILESTORE].RegisterTests = NULL; tmm_modules[TMM_FILESTORE].cap_flags = 0; tmm_modules[TMM_FILESTORE].flags = TM_FLAG_LOGAPI_TM; + tmm_modules[TMM_FILESTORE].priority = 10; OutputRegisterFiledataModule(MODULE_NAME, "file", LogFilestoreLogInitCtx, LogFilestoreLogger); diff --git a/src/runmodes.c b/src/runmodes.c index a769b9b6ee..5f9b201d51 100644 --- a/src/runmodes.c +++ b/src/runmodes.c @@ -72,6 +72,7 @@ typedef struct RunModes_ { * A list of output modules that will be active for the run mode. */ typedef struct RunModeOutput_ { + const char *name; TmModule *tm_module; OutputCtx *output_ctx; @@ -506,6 +507,32 @@ static void AddOutputToFreeList(OutputModule *module, OutputCtx *output_ctx) TAILQ_INSERT_TAIL(&output_free_list, fl_output, entries); } + +static int GetRunModeOutputPriority(RunModeOutput *module) +{ + TmModule *tm = TmModuleGetByName(module->name); + if (tm == NULL) + return 0; + + return tm->priority; +} + +static void InsertInRunModeOutputs(RunModeOutput *runmode_output) +{ + RunModeOutput *r_output = NULL; + int output_priority = GetRunModeOutputPriority(runmode_output); + + TAILQ_FOREACH(r_output, &RunModeOutputs, entries) { + if (GetRunModeOutputPriority(r_output) < output_priority) + break; + } + if (r_output) { + TAILQ_INSERT_BEFORE(r_output, runmode_output, entries); + } else { + TAILQ_INSERT_TAIL(&RunModeOutputs, runmode_output, entries); + } +} + /** \brief Turn output into thread module */ static void SetupOutput(const char *name, OutputModule *module, OutputCtx *output_ctx) { @@ -546,9 +573,10 @@ static void SetupOutput(const char *name, OutputModule *module, OutputCtx *outpu RunModeOutput *runmode_output = SCCalloc(1, sizeof(RunModeOutput)); if (unlikely(runmode_output == NULL)) return; + runmode_output->name = module->name; runmode_output->tm_module = pkt_logger_module; runmode_output->output_ctx = NULL; - TAILQ_INSERT_TAIL(&RunModeOutputs, runmode_output, entries); + InsertInRunModeOutputs(runmode_output); SCLogDebug("__packet_logger__ added"); } } else if (module->TxLogFunc) { @@ -568,9 +596,10 @@ static void SetupOutput(const char *name, OutputModule *module, OutputCtx *outpu RunModeOutput *runmode_output = SCCalloc(1, sizeof(RunModeOutput)); if (unlikely(runmode_output == NULL)) return; + runmode_output->name = module->name; runmode_output->tm_module = tx_logger_module; runmode_output->output_ctx = NULL; - TAILQ_INSERT_TAIL(&RunModeOutputs, runmode_output, entries); + InsertInRunModeOutputs(runmode_output); SCLogDebug("__tx_logger__ added"); } } else if (module->FiledataLogFunc) { @@ -589,9 +618,10 @@ static void SetupOutput(const char *name, OutputModule *module, OutputCtx *outpu RunModeOutput *runmode_output = SCCalloc(1, sizeof(RunModeOutput)); if (unlikely(runmode_output == NULL)) return; + runmode_output->name = module->name; runmode_output->tm_module = filedata_logger_module; runmode_output->output_ctx = NULL; - TAILQ_INSERT_TAIL(&RunModeOutputs, runmode_output, entries); + InsertInRunModeOutputs(runmode_output); SCLogDebug("__filedata_logger__ added"); } } else if (module->FileLogFunc) { @@ -610,9 +640,10 @@ static void SetupOutput(const char *name, OutputModule *module, OutputCtx *outpu RunModeOutput *runmode_output = SCCalloc(1, sizeof(RunModeOutput)); if (unlikely(runmode_output == NULL)) return; + runmode_output->name = module->name; runmode_output->tm_module = file_logger_module; runmode_output->output_ctx = NULL; - TAILQ_INSERT_TAIL(&RunModeOutputs, runmode_output, entries); + InsertInRunModeOutputs(runmode_output); SCLogDebug("__file_logger__ added"); } } else if (module->StreamingLogFunc) { @@ -632,9 +663,10 @@ static void SetupOutput(const char *name, OutputModule *module, OutputCtx *outpu RunModeOutput *runmode_output = SCCalloc(1, sizeof(RunModeOutput)); if (unlikely(runmode_output == NULL)) return; + runmode_output->name = module->name; runmode_output->tm_module = streaming_logger_module; runmode_output->output_ctx = NULL; - TAILQ_INSERT_TAIL(&RunModeOutputs, runmode_output, entries); + InsertInRunModeOutputs(runmode_output); SCLogDebug("__streaming_logger__ added"); } } else { @@ -643,9 +675,10 @@ static void SetupOutput(const char *name, OutputModule *module, OutputCtx *outpu RunModeOutput *runmode_output = SCCalloc(1, sizeof(RunModeOutput)); if (unlikely(runmode_output == NULL)) return; + runmode_output->name = module->name; runmode_output->tm_module = tm_module; runmode_output->output_ctx = output_ctx; - TAILQ_INSERT_TAIL(&RunModeOutputs, runmode_output, entries); + InsertInRunModeOutputs(runmode_output); } } diff --git a/src/tm-modules.h b/src/tm-modules.h index 6502b21c10..c9d4e1f56e 100644 --- a/src/tm-modules.h +++ b/src/tm-modules.h @@ -61,6 +61,8 @@ typedef struct TmModule_ { the given TmModule */ /* Other flags used by the module */ uint8_t flags; + /* priority in the logging order, higher priority is runned first */ + uint8_t priority; } TmModule; TmModule tmm_modules[TMM_SIZE];