From 34f2ff067b1af35cbe9ca3e869db03c3d109e42b Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 13 May 2015 09:53:49 +0200 Subject: [PATCH] reference: update pcre globals use Don't update globals each time we parse, but instead do it once at startup. --- src/suricata.c | 2 + src/util-reference-config.c | 76 ++++++++++++++++++------------------- src/util-reference-config.h | 3 ++ 3 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/suricata.c b/src/suricata.c index 9582b106fb..d0971ab484 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -2258,6 +2258,7 @@ int main(int argc, char **argv) DetectEngineCtx *de_ctx = NULL; if (!suri.disabled_detect) { SCClassConfInit(); + SCReferenceConfInit(); SetupDelayedDetect(&suri); if (!suri.delayed_detect) { de_ctx = DetectEngineCtxInit(); @@ -2487,6 +2488,7 @@ int main(int argc, char **argv) DefragDestroy(); } if (!suri.disabled_detect) { + SCReferenceConfDeinit(); SCClassConfDeinit(); } MagicDeinit(); diff --git a/src/util-reference-config.c b/src/util-reference-config.c index 59aaf0a9fc..a2f09bd360 100644 --- a/src/util-reference-config.c +++ b/src/util-reference-config.c @@ -41,8 +41,6 @@ /* Default path for the reference.conf file */ #define SC_RCONF_DEFAULT_FILE_PATH CONFIG_DIR "/reference.config" -/* Holds a pointer to the default path for the reference.config file */ -static const char *file_path = SC_RCONF_DEFAULT_FILE_PATH; static pcre *regex = NULL; static pcre_extra *regex_study = NULL; @@ -55,6 +53,43 @@ void SCRConfReferenceHashFree(void *ch); /* used to get the reference.config file path */ static char *SCRConfGetConfFilename(void); +void SCReferenceConfInit(void) +{ + const char *eb = NULL; + int eo; + int opts = 0; + + regex = pcre_compile(SC_RCONF_REGEX, opts, &eb, &eo, NULL); + if (regex == NULL) { + SCLogDebug("Compile of \"%s\" failed at offset %" PRId32 ": %s", + SC_RCONF_REGEX, eo, eb); + return; + } + + regex_study = pcre_study(regex, 0, &eb); + if (eb != NULL) { + pcre_free(regex); + regex = NULL; + SCLogDebug("pcre study failed: %s", eb); + return; + } + + return; +} + +void SCReferenceConfDeinit(void) +{ + if (regex != NULL) { + pcre_free(regex); + regex = NULL; + } + if (regex_study != NULL) { + pcre_free(regex_study); + regex_study = NULL; + } +} + + /** * \brief Inits the context to be used by the Reference Config parsing API. * @@ -71,9 +106,6 @@ static char *SCRConfGetConfFilename(void); static FILE *SCRConfInitContextAndLocalResources(DetectEngineCtx *de_ctx, FILE *fd) { char *filename = NULL; - const char *eb = NULL; - int eo; - int opts = 0; /* init the hash table to be used by the reference config references */ de_ctx->reference_conf_ht = HashTableInit(128, SCRConfReferenceHashFunc, @@ -102,19 +134,6 @@ static FILE *SCRConfInitContextAndLocalResources(DetectEngineCtx *de_ctx, FILE * } } - regex = pcre_compile(SC_RCONF_REGEX, opts, &eb, &eo, NULL); - if (regex == NULL) { - SCLogDebug("Compile of \"%s\" failed at offset %" PRId32 ": %s", - SC_RCONF_REGEX, eo, eb); - goto error; - } - - regex_study = pcre_study(regex, 0, &eb); - if (eb != NULL) { - SCLogDebug("pcre study failed: %s", eb); - goto error; - } - return fd; error: @@ -127,15 +146,6 @@ static FILE *SCRConfInitContextAndLocalResources(DetectEngineCtx *de_ctx, FILE * fd = NULL; } - if (regex != NULL) { - pcre_free(regex); - regex = NULL; - } - if (regex_study != NULL) { - pcre_free(regex_study); - regex_study = NULL; - } - return NULL; } @@ -153,7 +163,7 @@ static char *SCRConfGetConfFilename(void) { char *path = NULL; if (ConfGet("reference-config-file", &path) != 1) { - return (char *)file_path; + return (char *)SC_RCONF_DEFAULT_FILE_PATH; } return path; } @@ -167,16 +177,6 @@ static void SCRConfDeInitLocalResources(DetectEngineCtx *de_ctx, FILE *fd) fclose(fd); fd = NULL; } - file_path = SC_RCONF_DEFAULT_FILE_PATH; - - if (regex != NULL) { - pcre_free(regex); - regex = NULL; - } - if (regex_study != NULL) { - pcre_free(regex_study); - regex_study = NULL; - } return; } diff --git a/src/util-reference-config.h b/src/util-reference-config.h index 98cc51b149..f7fe4cc1eb 100644 --- a/src/util-reference-config.h +++ b/src/util-reference-config.h @@ -47,4 +47,7 @@ FILE *SCRConfGenerateValidDummyReferenceConfigFD01(void); FILE *SCRConfGenerateInValidDummyReferenceConfigFD02(void); FILE *SCRConfGenerateInValidDummyReferenceConfigFD03(void); +void SCReferenceConfInit(void); +void SCReferenceConfDeinit(void); + #endif /* __UTIL_REFERENCE_CONFIG_H__ */