]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
output-api: cleanup handling 926/head
authorVictor Julien <victor@inliniac.net>
Thu, 10 Apr 2014 07:45:21 +0000 (09:45 +0200)
committerVictor Julien <victor@inliniac.net>
Thu, 10 Apr 2014 08:03:33 +0000 (10:03 +0200)
Add output 'free list' that contains all the output ctx' that need
cleanup at shutdown. It differs from the runmode output list in that
it will also contain a 'parent' for the submodules that share the
context of it's parent.

src/runmodes.c

index 6256819fd6267e49542b3db63ac72f1fc2320ed7..06eec0d459ff08126e3bf5eed5378316f26b6be5 100644 (file)
@@ -84,6 +84,16 @@ static RunModes runmodes[RUNMODE_USER_MAX];
 
 static char *active_runmode;
 
+/* free list for our outputs */
+typedef struct OutputFreeList_ {
+    TmModule *tm_module;
+    OutputCtx *output_ctx;
+
+    TAILQ_ENTRY(OutputFreeList_) entries;
+} OutputFreeList;
+TAILQ_HEAD(, OutputFreeList_) output_free_list =
+    TAILQ_HEAD_INITIALIZER(output_free_list);
+
 /**
  * \internal
  * \brief Translate a runmode mode to a printale string.
@@ -394,24 +404,46 @@ void RunModeRegisterNewRunMode(int runmode, const char *name,
     return;
 }
 
+/**
+ * Setup the outputs for this run mode.
+ *
+ * \param tv The ThreadVars for the thread the outputs will be
+ * appended to.
+ */
+void RunOutputFreeList(void)
+{
+    OutputFreeList *output;
+    while ((output = TAILQ_FIRST(&output_free_list))) {
+        SCLogDebug("output %s %p %p", output->tm_module->name, output, output->output_ctx);
+
+        if (output->output_ctx != NULL && output->output_ctx->DeInit != NULL)
+            output->output_ctx->DeInit(output->output_ctx);
+
+        TAILQ_REMOVE(&output_free_list, output, entries);
+        SCFree(output);
+    }
+}
+
 /**
  * Cleanup the run mode.
  */
 void RunModeShutDown(void)
 {
+    RunOutputFreeList();
+
+    OutputPacketShutdown();
+    OutputTxShutdown();
+    OutputFileShutdown();
+    OutputFiledataShutdown();
+
     /* Close any log files. */
     RunModeOutput *output;
     while ((output = TAILQ_FIRST(&RunModeOutputs))) {
         SCLogDebug("Shutting down output %s.", output->tm_module->name);
         TAILQ_REMOVE(&RunModeOutputs, output, entries);
-        if (output->output_ctx != NULL && output->output_ctx->DeInit != NULL)
-            output->output_ctx->DeInit(output->output_ctx);
         SCFree(output);
     }
 
-    OutputPacketShutdown();
-    OutputTxShutdown();
-    OutputFileShutdown();
 }
 
 static TmModule *pkt_logger_module = NULL;
@@ -419,6 +451,25 @@ static TmModule *tx_logger_module = NULL;
 static TmModule *file_logger_module = NULL;
 static TmModule *filedata_logger_module = NULL;
 
+/** \internal
+ *  \brief add Sub RunModeOutput to list for Submodule so we can free
+ *         the output ctx at shutdown and unix socket reload */
+static void AddOutputToFreeList(OutputModule *module, OutputCtx *output_ctx)
+{
+    TmModule *tm_module = TmModuleGetByName(module->name);
+    if (tm_module == NULL) {
+        SCLogError(SC_ERR_INVALID_ARGUMENT,
+                "TmModuleGetByName for %s failed", module->name);
+        exit(EXIT_FAILURE);
+    }
+    OutputFreeList *fl_output = SCCalloc(1, sizeof(OutputFreeList));
+    if (unlikely(fl_output == NULL))
+        return;
+    fl_output->tm_module = tm_module;
+    fl_output->output_ctx = output_ctx;
+    TAILQ_INSERT_TAIL(&output_free_list, fl_output, entries);
+}
+
 /** \brief Turn output into thread module */
 static void SetupOutput(const char *name, OutputModule *module, OutputCtx *output_ctx)
 {
@@ -645,10 +696,16 @@ void RunModeInitializeOutputs(void)
                         continue;
                     }
 
+                    AddOutputToFreeList(sub_module, sub_output_ctx);
                     SetupOutput(sub_module->name, sub_module, sub_output_ctx);
                 }
             }
+            /* add 'eve-log' to free list as it's the owner of the
+             * main output ctx from which the sub-modules share the
+             * LogFileCtx */
+            AddOutputToFreeList(module, output_ctx);
         } else {
+            AddOutputToFreeList(module, output_ctx);
             SetupOutput(module->name, module, output_ctx);
         }
     }