diff --git a/src/output-file.c b/src/output-file.c index 2747f2ed18..97aec6c2a8 100644 --- a/src/output-file.c +++ b/src/output-file.c @@ -263,10 +263,20 @@ static void OutputFileLogExitPrintStats(ThreadVars *tv, void *thread_data) } } +static uint32_t OutputFileLoggerGetActiveCount(void) +{ + uint32_t cnt = 0; + for (OutputFileLogger *p = list; p != NULL; p = p->next) { + cnt++; + } + return cnt; +} + void OutputFileLoggerRegister(void) { OutputRegisterRootLogger(OutputFileLogThreadInit, - OutputFileLogThreadDeinit, OutputFileLogExitPrintStats, OutputFileLog); + OutputFileLogThreadDeinit, OutputFileLogExitPrintStats, + OutputFileLog, OutputFileLoggerGetActiveCount); } void OutputFileShutdown(void) diff --git a/src/output-filedata.c b/src/output-filedata.c index 9467365ca5..893818234c 100644 --- a/src/output-filedata.c +++ b/src/output-filedata.c @@ -435,11 +435,20 @@ static void OutputFiledataLogExitPrintStats(ThreadVars *tv, void *thread_data) } } +static uint32_t OutputFiledataLoggerGetActiveCount(void) +{ + uint32_t cnt = 0; + for (OutputFiledataLogger *p = list; p != NULL; p = p->next) { + cnt++; + } + return cnt; +} + void OutputFiledataLoggerRegister(void) { OutputRegisterRootLogger(OutputFiledataLogThreadInit, OutputFiledataLogThreadDeinit, OutputFiledataLogExitPrintStats, - OutputFiledataLog); + OutputFiledataLog, OutputFiledataLoggerGetActiveCount); SC_ATOMIC_INIT(g_file_store_id); } diff --git a/src/output-packet.c b/src/output-packet.c index 45de33b1a1..e18bc4448b 100644 --- a/src/output-packet.c +++ b/src/output-packet.c @@ -210,11 +210,20 @@ static void OutputPacketLogExitPrintStats(ThreadVars *tv, void *thread_data) } } +static uint32_t OutputPacketLoggerGetActiveCount(void) +{ + uint32_t cnt = 0; + for (OutputPacketLogger *p = list; p != NULL; p = p->next) { + cnt++; + } + return cnt; +} + void OutputPacketLoggerRegister(void) { OutputRegisterRootLogger(OutputPacketLogThreadInit, OutputPacketLogThreadDeinit, OutputPacketLogExitPrintStats, - OutputPacketLog); + OutputPacketLog, OutputPacketLoggerGetActiveCount); } void OutputPacketShutdown(void) diff --git a/src/output-streaming.c b/src/output-streaming.c index 46dfebcf1a..d445d88837 100644 --- a/src/output-streaming.c +++ b/src/output-streaming.c @@ -448,10 +448,19 @@ static void OutputStreamingLogExitPrintStats(ThreadVars *tv, void *thread_data) } } +static uint32_t OutputStreamingLoggerGetActiveCount(void) +{ + uint32_t cnt = 0; + for (OutputStreamingLogger *p = list; p != NULL; p = p->next) { + cnt++; + } + return cnt; +} + void OutputStreamingLoggerRegister(void) { OutputRegisterRootLogger(OutputStreamingLogThreadInit, OutputStreamingLogThreadDeinit, OutputStreamingLogExitPrintStats, - OutputStreamingLog); + OutputStreamingLog, OutputStreamingLoggerGetActiveCount); } void OutputStreamingShutdown(void) diff --git a/src/output-tx.c b/src/output-tx.c index edb198ad97..cfc555fc4e 100644 --- a/src/output-tx.c +++ b/src/output-tx.c @@ -371,10 +371,20 @@ static void OutputTxLogExitPrintStats(ThreadVars *tv, void *thread_data) } } +static uint32_t OutputTxLoggerGetActiveCount(void) +{ + uint32_t cnt = 0; + for (OutputTxLogger *p = list; p != NULL; p = p->next) { + cnt++; + } + return cnt; +} + + void OutputTxLoggerRegister (void) { OutputRegisterRootLogger(OutputTxLogThreadInit, OutputTxLogThreadDeinit, - OutputTxLogExitPrintStats, OutputTxLog); + OutputTxLogExitPrintStats, OutputTxLog, OutputTxLoggerGetActiveCount); } void OutputTxShutdown(void) diff --git a/src/output.c b/src/output.c index 400b8f47ff..1f234115ab 100644 --- a/src/output.c +++ b/src/output.c @@ -85,18 +85,26 @@ #include "output-filestore.h" typedef struct RootLogger_ { + OutputLogFunc LogFunc; ThreadInitFunc ThreadInit; ThreadDeinitFunc ThreadDeinit; ThreadExitPrintStatsFunc ThreadExitPrintStats; - OutputLogFunc LogFunc; + OutputGetActiveCountFunc ActiveCntFunc; TAILQ_ENTRY(RootLogger_) entries; } RootLogger; -/* List of registered root loggers. */ +/* List of registered root loggers. These are registered at start up and + * are independent of configuration. Later we will build a list of active + * loggers based on configuration. */ static TAILQ_HEAD(, RootLogger_) registered_loggers = TAILQ_HEAD_INITIALIZER(registered_loggers); +/* List of active root loggers. This means that at least one logger is enabled + * for each root logger type in the config. */ +static TAILQ_HEAD(, RootLogger_) active_loggers = + TAILQ_HEAD_INITIALIZER(active_loggers); + typedef struct LoggerThreadStoreNode_ { void *thread_data; TAILQ_ENTRY(LoggerThreadStoreNode_) entries; @@ -918,7 +926,7 @@ void OutputNotifyFileRotation(void) { TmEcode OutputLoggerLog(ThreadVars *tv, Packet *p, void *thread_data) { LoggerThreadStore *thread_store = (LoggerThreadStore *)thread_data; - RootLogger *logger = TAILQ_FIRST(®istered_loggers); + RootLogger *logger = TAILQ_FIRST(&active_loggers); LoggerThreadStoreNode *thread_store_node = TAILQ_FIRST(thread_store); while (logger && thread_store_node) { if (logger->LogFunc != NULL) { @@ -941,7 +949,7 @@ TmEcode OutputLoggerThreadInit(ThreadVars *tv, const void *initdata, void **data *data = (void *)thread_store; RootLogger *logger; - TAILQ_FOREACH(logger, ®istered_loggers, entries) { + TAILQ_FOREACH(logger, &active_loggers, entries) { void *child_thread_data = NULL; if (logger->ThreadInit != NULL) { @@ -968,7 +976,7 @@ TmEcode OutputLoggerThreadDeinit(ThreadVars *tv, void *thread_data) return TM_ECODE_FAILED; LoggerThreadStore *thread_store = (LoggerThreadStore *)thread_data; - RootLogger *logger = TAILQ_FIRST(®istered_loggers); + RootLogger *logger = TAILQ_FIRST(&active_loggers); LoggerThreadStoreNode *thread_store_node = TAILQ_FIRST(thread_store); while (logger && thread_store_node) { if (logger->ThreadDeinit != NULL) { @@ -991,7 +999,7 @@ TmEcode OutputLoggerThreadDeinit(ThreadVars *tv, void *thread_data) void OutputLoggerExitPrintStats(ThreadVars *tv, void *thread_data) { LoggerThreadStore *thread_store = (LoggerThreadStore *)thread_data; - RootLogger *logger = TAILQ_FIRST(®istered_loggers); + RootLogger *logger = TAILQ_FIRST(&active_loggers); LoggerThreadStoreNode *thread_store_node = TAILQ_FIRST(thread_store); while (logger && thread_store_node) { if (logger->ThreadExitPrintStats != NULL) { @@ -1005,7 +1013,7 @@ void OutputLoggerExitPrintStats(ThreadVars *tv, void *thread_data) void OutputRegisterRootLogger(ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats, - OutputLogFunc LogFunc) + OutputLogFunc LogFunc, OutputGetActiveCountFunc ActiveCntFunc) { RootLogger *logger = SCCalloc(1, sizeof(*logger)); if (logger == NULL) { @@ -1015,9 +1023,46 @@ void OutputRegisterRootLogger(ThreadInitFunc ThreadInit, logger->ThreadDeinit = ThreadDeinit; logger->ThreadExitPrintStats = ThreadExitPrintStats; logger->LogFunc = LogFunc; + logger->ActiveCntFunc = ActiveCntFunc; TAILQ_INSERT_TAIL(®istered_loggers, logger, entries); } +static void OutputRegisterActiveLogger(RootLogger *reg) +{ + RootLogger *logger = SCCalloc(1, sizeof(*logger)); + if (logger == NULL) { + FatalError(SC_ERR_MEM_ALLOC, "failed to alloc root logger"); + } + logger->ThreadInit = reg->ThreadInit; + logger->ThreadDeinit = reg->ThreadDeinit; + logger->ThreadExitPrintStats = reg->ThreadExitPrintStats; + logger->LogFunc = reg->LogFunc; + logger->ActiveCntFunc = reg->ActiveCntFunc; + TAILQ_INSERT_TAIL(&active_loggers, logger, entries); +} + +void OutputSetupActiveLoggers(void) +{ + RootLogger *logger = TAILQ_FIRST(®istered_loggers); + while (logger) { + uint32_t cnt = logger->ActiveCntFunc(); + if (cnt) { + OutputRegisterActiveLogger(logger); + } + + logger = TAILQ_NEXT(logger, entries); + } +} + +void OutputClearActiveLoggers(void) +{ + RootLogger *logger; + while ((logger = TAILQ_FIRST(&active_loggers)) != NULL) { + TAILQ_REMOVE(&active_loggers, logger, entries); + SCFree(logger); + } +} + void TmModuleLoggerRegister(void) { OutputRegisterRootLoggers(); diff --git a/src/output.h b/src/output.h index d6873d076f..14bf4d61f9 100644 --- a/src/output.h +++ b/src/output.h @@ -46,6 +46,7 @@ typedef struct OutputInitResult_ { typedef OutputInitResult (*OutputInitFunc)(ConfNode *); typedef OutputInitResult (*OutputInitSubFunc)(ConfNode *, OutputCtx *); typedef TmEcode (*OutputLogFunc)(ThreadVars *, Packet *, void *); +typedef uint32_t (*OutputGetActiveCountFunc)(void); typedef struct OutputModule_ { LoggerId logger_id; @@ -196,7 +197,7 @@ void OutputNotifyFileRotation(void); void OutputRegisterRootLogger(ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats, - OutputLogFunc LogFunc); + OutputLogFunc LogFunc, OutputGetActiveCountFunc ActiveCntFunc); void TmModuleLoggerRegister(void); TmEcode OutputLoggerLog(ThreadVars *, Packet *, void *); @@ -204,4 +205,7 @@ TmEcode OutputLoggerThreadInit(ThreadVars *, const void *, void **); TmEcode OutputLoggerThreadDeinit(ThreadVars *, void *); void OutputLoggerExitPrintStats(ThreadVars *, void *); +void OutputSetupActiveLoggers(void); +void OutputClearActiveLoggers(void); + #endif /* ! __OUTPUT_H__ */ diff --git a/src/runmodes.c b/src/runmodes.c index 47f774493d..fbea455bbd 100644 --- a/src/runmodes.c +++ b/src/runmodes.c @@ -531,6 +531,8 @@ void RunModeShutDown(void) OutputStatsShutdown(); OutputFlowShutdown(); + OutputClearActiveLoggers(); + /* Reset logger counts. */ file_logger_count = 0; filedata_logger_count = 0; @@ -890,6 +892,7 @@ void RunModeInitializeOutputs(void) AppLayerParserRegisterLoggerBits(IPPROTO_UDP, a, logger_bits[a]); } + OutputSetupActiveLoggers(); } float threading_detect_ratio = 1;