]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
code cleanup for live swap
authorAnoop Saldanha <poonaatsoc@gmail.com>
Tue, 26 Jun 2012 11:38:04 +0000 (17:08 +0530)
committerVictor Julien <victor@inliniac.net>
Thu, 28 Jun 2012 09:26:15 +0000 (11:26 +0200)
src/detect-engine.c
src/detect.c
src/detect.h
src/util-atomic.c
src/util-classification-config.c
src/util-reference-config.c
src/util-signal.h
src/util-threshold-config.c

index db08aff23deeafd4005cfe9a98ad32d8357abeee..f9a74daa9877b40105953099c8d7dd7229515be0 100644 (file)
@@ -231,7 +231,7 @@ static void *DetectEngineLiveRuleSwap(void *arg)
 
     for (i = 0; i < no_of_detect_tvs; i++) {
         int break_out = 0;
-        while (new_det_ctx[i]->so_far_used_by_detect != 1) {
+        while (SC_ATOMIC_GET(new_det_ctx[i]->so_far_used_by_detect) != 1) {
             if (suricata_ctl_flags != 0) {
                 break_out = 1;
                 break;
@@ -244,6 +244,11 @@ static void *DetectEngineLiveRuleSwap(void *arg)
         SCLogDebug("new_det_ctx - %p used by detect engine", new_det_ctx[i]);
     }
 
+    /* this is to make sure that if someone initiated shutdown during a live
+     * rule swap, the live rule swap won't clean up the old det_ctx and
+     * de_ctx, till all detect threads have stopped working and sitting
+     * silently after setting RUNNING_DONE flag and while waiting for
+     * THV_DEINIT flag */
     if (i != no_of_detect_tvs) {
         ThreadVars *tv = tv_root[TVT_PPT];
         while (tv) {
@@ -742,6 +747,8 @@ TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data) {
         return TM_ECODE_FAILED;
     }
 
+    SC_ATOMIC_INIT(det_ctx->so_far_used_by_detect);
+
     *data = (void *)det_ctx;
 
     return TM_ECODE_OK;
@@ -820,6 +827,8 @@ static TmEcode DetectEngineThreadCtxInitForLiveRuleSwap(ThreadVars *tv, void *in
         return TM_ECODE_FAILED;
     }
 
+    SC_ATOMIC_INIT(det_ctx->so_far_used_by_detect);
+
     *data = (void *)det_ctx;
 
     return TM_ECODE_OK;
index fdf5f759dacd331057c9b8a056677c6513763e2f..aa0c7d9f4a62af6987b556c9c024f6728ac104a3 100644 (file)
@@ -1938,8 +1938,8 @@ TmEcode Detect(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq, PacketQue
         goto error;
     }
 
-    if (det_ctx->so_far_used_by_detect == 0) {
-        det_ctx->so_far_used_by_detect = 1;
+    if (SC_ATOMIC_GET(det_ctx->so_far_used_by_detect) == 0) {
+        SC_ATOMIC_SET(det_ctx->so_far_used_by_detect, 1);
         SCLogDebug("Detect Engine using new det_ctx - %p and de_ctx - %p",
                   det_ctx, de_ctx);
     }
index 53635ecc7578e72924900363ed23ba877619bae8..5c042011963d7ae03fe11ec8311f2fff99346f17 100644 (file)
@@ -736,7 +736,7 @@ typedef struct DetectionEngineThreadCtx_ {
     /** ID of the transaction currently being inspected. */
     uint16_t tx_id;
 
-    uint16_t so_far_used_by_detect;
+    SC_ATOMIC_DECLARE(uint16_t, so_far_used_by_detect);
 
     /* holds the current recursion depth on content inspection */
     int inspection_recursion_counter;
index de85c3162a1fef4f94ed73e4fdfe3c448ea1a72b..c79f126e296e8792b4d56b8f7149deb2d570a9c6 100644 (file)
@@ -26,6 +26,8 @@
 #include "util-atomic.h"
 #include "util-unittest.h"
 
+#ifdef UNITTESTS
+
 static int SCAtomicTest01(void)
 {
     int result = 0;
@@ -59,9 +61,13 @@ static int SCAtomicTest01(void)
     return result;
 }
 
+#endif /* UNITTESTS */
+
 void SCAtomicRegisterTests(void)
 {
+#ifdef UNITTESTS
     UtRegisterTest("SCAtomicTest01", SCAtomicTest01, 1);
+#endif
 
     return;
 }
index 7cea53ef00a5c5d7ec18f38e064ae33a004ed582..d9d5e54e3cff90d0764646bb5a9133de60121c3b 100644 (file)
@@ -123,8 +123,14 @@ int SCClassConfInitContextAndLocalResources(DetectEngineCtx *de_ctx)
         fd = NULL;
     }
 
-    regex = NULL;
-    regex_study = NULL;
+    if (regex != NULL) {
+        pcre_free(regex);
+        regex = NULL;
+    }
+    if (regex_study != NULL) {
+        //pcre_free_study(regex_study);
+        regex_study = NULL;
+    }
 
     return -1;
 }
@@ -159,8 +165,14 @@ static void SCClassConfDeInitLocalResources(DetectEngineCtx *de_ctx)
     fclose(fd);
     default_file_path = SC_CLASS_CONF_DEF_CONF_FILEPATH;
     fd = NULL;
-    regex = NULL;
-    regex_study = NULL;
+    if (regex != NULL) {
+        pcre_free(regex_study);
+        regex = NULL;
+    }
+    if (regex_study != NULL) {
+        //pcre_free_study(regex_study);
+        regex_study = NULL;
+    }
 
     return;
 }
@@ -530,7 +542,7 @@ SCClassConfClasstype *SCClassConfGetClasstype(const char *ct_name,
     SCClassConfClasstype *ct_info = SCClassConfAllocClasstype(0, ct_name, NULL,
                                                               0);
     if (ct_info == NULL)
-        exit(EXIT_FAILURE);
+        return NULL;
     SCClassConfClasstype *lookup_ct_info = HashTableLookup(de_ctx->class_conf_ht,
                                                            ct_info, 0);
 
index 2caf4ac7e0446eacf7abe07aaa6fb6ecadb91718..989f9b5a1aae004e348b231d7788015eac74fd52 100644 (file)
@@ -124,8 +124,14 @@ static int SCRConfInitContextAndLocalResources(DetectEngineCtx *de_ctx)
         fd = NULL;
     }
 
-    regex = NULL;
-    regex_study = NULL;
+    if (regex != NULL) {
+        pcre_free(regex);
+        regex = NULL;
+    }
+    if (regex_study != NULL) {
+        //pcre_free_study(regex_study);
+        regex_study = NULL;
+    }
 
     return -1;
 }
@@ -156,8 +162,15 @@ static void SCRConfDeInitLocalResources(DetectEngineCtx *de_ctx)
         fclose(fd);
     file_path = SC_RCONF_DEFAULT_FILE_PATH;
     fd = NULL;
-    regex = NULL;
-    regex_study = NULL;
+
+    if (regex != NULL) {
+        pcre_free(regex);
+        regex = NULL;
+    }
+    if (regex_study != NULL) {
+        //pcre_free_study(regex_study);
+        regex_study = NULL;
+    }
 
     return;
 }
index 3a51d56faa7b1552ea1818a9def0a7400b34a5c8..7209b3da79bf29f97e01fe94f65010fca8319e3f 100644 (file)
  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
  */
 
-#ifndef __UTIL_STRING_H__
-#define __UTIL_STRING_H__
+#ifndef __UTIL_SIGNAL_H__
+#define __UTIL_SIGNAL_H__
 
 int UtilSignalBlock(int);
 void UtilSignalHandlerSetup(int, void (*handler)());;
 int UtilSignalIsHandler(int sig, void (*handler)());
 
-#endif /* __UTIL_STRING_H__ */
+#endif /* __UTIL_SIGNAL_H__ */
index a5add030fc120eb6e7712d4e7ac357772da57d73..47a4c35c7fdf0658d57ca61bc96ab4f800fa7b6b 100644 (file)
@@ -211,17 +211,41 @@ void SCThresholdConfDeInitContext(DetectEngineCtx *de_ctx, FILE *fd)
     if (fd != NULL)
         fclose(fd);
 
-    regex_base = NULL;
-    regex_base_study = NULL;
+    if (regex_base != NULL) {
+        pcre_free(regex_base);
+        regex_base = NULL;
+    }
+    if (regex_base_study != NULL) {
+        //pcre_free_study(regex_base_study);
+        regex_base_study = NULL;
+    }
 
-    regex_threshold = NULL;
-    regex_threshold_study = NULL;
+    if (regex_threshold != NULL) {
+        pcre_free(regex_threshold);
+        regex_threshold = NULL;
+    }
+    if (regex_threshold_study != NULL) {
+        //pcre_free_study(regex_threshold_study);
+        regex_threshold_study = NULL;
+    }
 
-    regex_rate = NULL;
-    regex_rate_study = NULL;
+    if (regex_rate != NULL) {
+        pcre_free(regex_rate);
+        regex_rate = NULL;
+    }
+    if (regex_rate_study != NULL) {
+        //pcre_free_study(regex_rate_study);
+        regex_rate_study = NULL;
+    }
 
-    regex_suppress = NULL;
-    regex_suppress_study = NULL;
+    if (regex_suppress != NULL) {
+        pcre_free(regex_suppress);
+        regex_suppress = NULL;
+    }
+    if (regex_suppress_study != NULL) {
+        //pcre_free_study(regex_suppress_study);
+        regex_suppress_study = NULL;
+    }
 
     return;
 }