From: Anoop Saldanha Date: Tue, 26 Jun 2012 11:38:04 +0000 (+0530) Subject: code cleanup for live swap X-Git-Tag: suricata-1.3rc1~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0c24bbab0cad9cb0116424aa61b6e1bc753c5a5a;p=thirdparty%2Fsuricata.git code cleanup for live swap --- diff --git a/src/detect-engine.c b/src/detect-engine.c index db08aff23d..f9a74daa98 100644 --- a/src/detect-engine.c +++ b/src/detect-engine.c @@ -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; diff --git a/src/detect.c b/src/detect.c index fdf5f759da..aa0c7d9f4a 100644 --- a/src/detect.c +++ b/src/detect.c @@ -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); } diff --git a/src/detect.h b/src/detect.h index 53635ecc75..5c04201196 100644 --- a/src/detect.h +++ b/src/detect.h @@ -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; diff --git a/src/util-atomic.c b/src/util-atomic.c index de85c3162a..c79f126e29 100644 --- a/src/util-atomic.c +++ b/src/util-atomic.c @@ -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; } diff --git a/src/util-classification-config.c b/src/util-classification-config.c index 7cea53ef00..d9d5e54e3c 100644 --- a/src/util-classification-config.c +++ b/src/util-classification-config.c @@ -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); diff --git a/src/util-reference-config.c b/src/util-reference-config.c index 2caf4ac7e0..989f9b5a1a 100644 --- a/src/util-reference-config.c +++ b/src/util-reference-config.c @@ -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; } diff --git a/src/util-signal.h b/src/util-signal.h index 3a51d56faa..7209b3da79 100644 --- a/src/util-signal.h +++ b/src/util-signal.h @@ -21,11 +21,11 @@ * \author Anoop Saldanha */ -#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__ */ diff --git a/src/util-threshold-config.c b/src/util-threshold-config.c index a5add030fc..47a4c35c7f 100644 --- a/src/util-threshold-config.c +++ b/src/util-threshold-config.c @@ -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; }