]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
eve: factor thread context creation/free for reuse
authorJason Ish <jason.ish@oisf.net>
Thu, 15 Apr 2021 07:33:43 +0000 (01:33 -0600)
committerJason Ish <jason.ish@oisf.net>
Thu, 15 Apr 2021 20:33:05 +0000 (14:33 -0600)
src/output-json-common.c
src/output-json.h

index c7ee6f188da0031687744a199b0ec63c0b961693..c97d8b1089e611ea671a1814ef42f2b04f1240ad 100644 (file)
 #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;
 }
index 2ae21c51288a34afeb1d3069d8ea99059cd662b3..0ecd5e7b8d9b64be249d486a5474ef14ab49eda2 100644 (file)
@@ -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__ */