diff --git a/src/output-json-common.c b/src/output-json-common.c index c7ee6f188d..c97d8b1089 100644 --- a/src/output-json-common.c +++ b/src/output-json-common.c @@ -41,29 +41,59 @@ #include "app-layer.h" #include "app-layer-parser.h" +OutputJsonThreadCtx *CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx) +{ + OutputJsonThreadCtx *thread = SCCalloc(1, sizeof(*thread)); + if (unlikely(thread == NULL)) { + return NULL; + } + + thread->buffer = MemBufferCreateNew(JSON_OUTPUT_BUFFER_SIZE); + if (unlikely(thread->buffer == NULL)) { + goto error; + } + + thread->file_ctx = LogFileEnsureExists(ctx->file_ctx, t->id); + if (!thread->file_ctx) { + goto error; + } + + thread->ctx = ctx; + + return thread; + +error: + if (thread->buffer) { + MemBufferFree(thread->buffer); + } + SCFree(thread); + return NULL; +} + +void FreeEveThreadCtx(OutputJsonThreadCtx *ctx) +{ + if (ctx != NULL && ctx->buffer != NULL) { + MemBufferFree(ctx->buffer); + } + if (ctx != NULL) { + SCFree(ctx); + } +} + static void OutputJsonLogDeInitCtxSub(OutputCtx *output_ctx) { - SCFree(output_ctx->data); SCFree(output_ctx); } OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx) { OutputInitResult result = { NULL, false }; - OutputJsonCtx *ajt = parent_ctx->data; - - OutputJsonCtx *log_ctx = SCCalloc(1, sizeof(*log_ctx)); - if (unlikely(log_ctx == NULL)) { - return result; - } - *log_ctx = *ajt; OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx)); if (unlikely(output_ctx == NULL)) { - SCFree(log_ctx); return result; } - output_ctx->data = log_ctx; + output_ctx->data = parent_ctx->data; output_ctx->DeInit = OutputJsonLogDeInitCtxSub; result.ctx = output_ctx; @@ -108,12 +138,6 @@ error_exit: TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data) { OutputJsonThreadCtx *thread = (OutputJsonThreadCtx *)data; - if (thread == NULL) { - return TM_ECODE_OK; - } - if (thread->buffer != NULL) { - MemBufferFree(thread->buffer); - } - SCFree(thread); + FreeEveThreadCtx(thread); return TM_ECODE_OK; } diff --git a/src/output-json.h b/src/output-json.h index 2ae21c5128..0ecd5e7b8d 100644 --- a/src/output-json.h +++ b/src/output-json.h @@ -116,4 +116,7 @@ void EveAddMetadata(const Packet *p, const Flow *f, JsonBuilder *js); int OutputJSONMemBufferCallback(const char *str, size_t size, void *data); +OutputJsonThreadCtx *CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx); +void FreeEveThreadCtx(OutputJsonThreadCtx *ctx); + #endif /* __OUTPUT_JSON_H__ */