]> git.ipfire.org Git - people/ms/suricata.git/commitdiff
introduce fatal error macro's
authorVictor Julien <victor@inliniac.net>
Fri, 10 Jul 2015 10:19:57 +0000 (12:19 +0200)
committerVictor Julien <victor@inliniac.net>
Mon, 20 Jul 2015 09:49:14 +0000 (11:49 +0200)
Add 'FatalError' and 'FatalErrorConditonal' that will take the same
args as SCLogError.

FatalError logs the error using SCLogError and then exits with return
code EXIT_FAILURE.

FatalErrorOnInit does the same only during init and with
--init-errors-fatal enabled, otherwise it just calls SCLogWarning. So
then the macro returns to the caller.

Implement this for output setup.

src/output-lua.c
src/runmodes.c
src/util-debug.h

index 0f06450e9b6c70fbd2e1364afa4294c5368e1829..fc5a562164fe8267ac979e19f75baa872477048c 100644 (file)
@@ -736,7 +736,7 @@ static OutputCtx *OutputLuaLogInit(ConfNode *conf)
         int r = LuaScriptInit(path, &opts);
         if (r != 0) {
             SCLogError(SC_ERR_LUA_ERROR, "couldn't initialize scipt");
-            continue;
+            goto error;
         }
 
         /* create an OutputModule for this script, based
@@ -744,7 +744,7 @@ static OutputCtx *OutputLuaLogInit(ConfNode *conf)
         OutputModule *om = SCCalloc(1, sizeof(*om));
         if (om == NULL) {
             SCLogError(SC_ERR_MEM_ALLOC, "calloc() failed");
-            continue;
+            goto error;
         }
 
         om->name = MODULE_NAME;
@@ -783,13 +783,22 @@ static OutputCtx *OutputLuaLogInit(ConfNode *conf)
         } else {
             SCLogError(SC_ERR_LUA_ERROR, "failed to setup thread module");
             SCFree(om);
-            continue;
+            goto error;
         }
 
         TAILQ_INSERT_TAIL(&output_ctx->submodules, om, entries);
     }
 
     return output_ctx;
+
+error:
+
+    if (output_ctx != NULL) {
+        if (output_ctx->DeInit && output_ctx->data)
+            output_ctx->DeInit(output_ctx->data);
+        SCFree(output_ctx);
+    }
+    return NULL;
 }
 
 /** \internal
index e6ff72872b332a9d04b114a541da9f03c68ada23..5a9f9762259ddd209835d40cb8953ceaafac2d62 100644 (file)
@@ -703,9 +703,8 @@ void RunModeInitializeOutputs(void)
         output_config = ConfNodeLookupChild(output, output->val);
         if (output_config == NULL) {
             /* Shouldn't happen. */
-            SCLogError(SC_ERR_INVALID_ARGUMENT,
-                "Failed to lookup configuration child node: fast");
-            exit(1);
+            FatalError(SC_ERR_INVALID_ARGUMENT,
+                "Failed to lookup configuration child node: %s", output->val);
         }
 
         if (strcmp(output->val, "tls-store") == 0) {
@@ -754,8 +753,8 @@ void RunModeInitializeOutputs(void)
 
         OutputModule *module = OutputGetModuleByConfName(output->val);
         if (module == NULL) {
-            SCLogWarning(SC_ERR_INVALID_ARGUMENT,
-                "No output module named %s, ignoring", output->val);
+            FatalErrorOnInit(SC_ERR_INVALID_ARGUMENT,
+                "No output module named %s", output->val);
             continue;
         }
 
@@ -763,8 +762,7 @@ void RunModeInitializeOutputs(void)
         if (module->InitFunc != NULL) {
             output_ctx = module->InitFunc(output_config);
             if (output_ctx == NULL) {
-                /* In most cases the init function will have logged the
-                 * error. Maybe we should exit on init errors? */
+                FatalErrorOnInit(SC_ERR_INVALID_ARGUMENT, "output module setup failed");
                 continue;
             }
         } else if (module->InitSubFunc != NULL) {
@@ -786,20 +784,18 @@ void RunModeInitializeOutputs(void)
 
                     OutputModule *sub_module = OutputGetModuleByConfName(subname);
                     if (sub_module == NULL) {
-                        SCLogWarning(SC_ERR_INVALID_ARGUMENT,
-                                "No output module named %s, ignoring", subname);
+                        FatalErrorOnInit(SC_ERR_INVALID_ARGUMENT,
+                                "No output module named %s", subname);
                         continue;
                     }
                     if (sub_module->parent_name == NULL ||
                             strcmp(sub_module->parent_name,output->val) != 0) {
-                        SCLogWarning(SC_ERR_INVALID_ARGUMENT,
-                                "bad parent for %s, ignoring", subname);
-                        continue;
+                        FatalError(SC_ERR_INVALID_ARGUMENT,
+                                "bad parent for %s", subname);
                     }
                     if (sub_module->InitSubFunc == NULL) {
-                        SCLogWarning(SC_ERR_INVALID_ARGUMENT,
-                                "bad sub-module for %s, ignoring", subname);
-                        continue;
+                        FatalError(SC_ERR_INVALID_ARGUMENT,
+                                "bad sub-module for %s", subname);
                     }
                     ConfNode *sub_output_config = ConfNodeLookupChild(type, type->val);
                     // sub_output_config may be NULL if no config
index 02ea30e2b08b3683a5704baa5afc25cd1bf7bcb4..9c9d2e23eed311a9393ff3d16e5f819e8bfe9efc 100644 (file)
@@ -507,6 +507,24 @@ extern int sc_log_module_cleaned;
 
 #endif /* DEBUG */
 
+#define FatalError(x, ...) do {                                             \
+    SCLogError(x, __VA_ARGS__);                                             \
+    exit(EXIT_FAILURE);                                                     \
+} while(0)
+
+/** \brief Fatal error IF we're starting up, and configured to consider
+ *         errors to be fatal errors */
+#define FatalErrorOnInit(x, ...) do {                                       \
+    int init_errors_fatal = 0;                                              \
+    ConfGetBool("engine.init-failure-fatal", &init_errors_fatal);           \
+    if (init_errors_fatal && (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT))\
+    {                                                                       \
+        SCLogError(x, __VA_ARGS__);                                         \
+        exit(EXIT_FAILURE);                                                 \
+    }                                                                       \
+    SCLogWarning(x, __VA_ARGS__);                                           \
+} while(0)
+
 
 SCLogInitData *SCLogAllocLogInitData(void);