From 25cbf36d40164c0b5c126d809e9010526ec99452 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Thu, 13 Mar 2014 16:24:51 +0100 Subject: [PATCH] lua/luajit: use HAVE_LUA mostly Only use HAVE_LUAJIT if things are done differently from HAVE_LUA, like in the states pool. --- src/detect-engine-content-inspection.c | 10 +++--- src/detect-luajit-extensions.c | 4 +-- src/detect-luajit-extensions.h | 4 +-- src/detect-luajit.c | 47 ++++++++++++++++---------- src/detect-luajit.h | 5 +-- src/detect-parse.c | 2 +- src/suricata.c | 3 ++ 7 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/detect-engine-content-inspection.c b/src/detect-engine-content-inspection.c index bc05e8eb44..69d8b0192b 100644 --- a/src/detect-engine-content-inspection.c +++ b/src/detect-engine-content-inspection.c @@ -529,10 +529,10 @@ int DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx det_ctx->discontinue_matching = 0; goto no_match; -#ifdef HAVE_LUAJIT +#ifdef HAVE_LUA } else if (sm->type == DETECT_LUAJIT) { - SCLogDebug("luajit starting"); + SCLogDebug("lua starting"); /* for flowvar gets and sets we need to know the flow's lock status */ int need_flow_lock = 0; if (inspection_mode <= DETECT_ENGINE_CONTENT_INSPECTION_MODE_STREAM) @@ -541,12 +541,12 @@ int DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx if (DetectLuajitMatchBuffer(det_ctx, s, sm, buffer, buffer_len, det_ctx->buffer_offset, f, need_flow_lock) != 1) { - SCLogDebug("luajit no_match"); + SCLogDebug("lua no_match"); goto no_match; } - SCLogDebug("luajit match"); + SCLogDebug("lua match"); goto match; -#endif +#endif /* HAVE_LUA */ } else { SCLogDebug("sm->type %u", sm->type); #ifdef DEBUG diff --git a/src/detect-luajit-extensions.c b/src/detect-luajit-extensions.c index 10b74d4237..0fa3aced21 100644 --- a/src/detect-luajit-extensions.c +++ b/src/detect-luajit-extensions.c @@ -58,7 +58,7 @@ #include "queue.h" #include "util-cpu.h" -#ifdef HAVE_LUAJIT +#ifdef HAVE_LUA static const char luaext_key_ld[] = "suricata:luajitdata"; static const char luaext_key_det_ctx[] = "suricata:det_ctx"; @@ -658,4 +658,4 @@ int LuajitRegisterExtensions(lua_State *lua_state) { return 0; } -#endif /* HAVE_LUAJIT */ +#endif /* HAVE_LUA */ diff --git a/src/detect-luajit-extensions.h b/src/detect-luajit-extensions.h index 87f3c056ad..c8e678d750 100644 --- a/src/detect-luajit-extensions.h +++ b/src/detect-luajit-extensions.h @@ -24,12 +24,12 @@ #ifndef __DETECT_LUAJIT_EXT_H__ #define __DETECT_LUAJIT_EXT_H__ -#ifdef HAVE_LUAJIT +#ifdef HAVE_LUA int LuajitRegisterExtensions(lua_State *); void LuajitExtensionsMatchSetup(lua_State *lua_state, DetectLuajitData *, DetectEngineThreadCtx *det_ctx, Flow *f, int need_flow_lock); -#endif /* HAVE_LUAJIT */ +#endif /* HAVE_LUA */ #endif diff --git a/src/detect-luajit.c b/src/detect-luajit.c index bae31fcdfa..0d1aff6176 100644 --- a/src/detect-luajit.c +++ b/src/detect-luajit.c @@ -59,7 +59,7 @@ #include "util-cpu.h" #include "util-var-name.h" -#ifndef HAVE_LUAJIT +#ifndef HAVE_LUA static int DetectLuajitSetupNoSupport (DetectEngineCtx *a, Signature *b, char *c) { SCLogError(SC_ERR_NO_LUAJIT_SUPPORT, "no LuaJIT support built in, needed for luajit keyword"); @@ -81,10 +81,28 @@ void DetectLuajitRegister(void) { return; } -#else /* HAVE_LUAJIT */ +#else /* HAVE_LUA */ +#ifdef HAVE_LUAJIT #include "util-pool.h" +/** \brief lua_State pool + * + * Luajit requires states to be alloc'd in memory <2GB. For this reason we + * prealloc the states early during engine startup so we have a better chance + * of getting the states. We protect the pool with a lock as the detect + * threads access it during their init and cleanup. + * + * Pool size is automagically determined based on number of keyword occurences, + * cpus/cores and rule reloads being enabled or not. + * + * Alternatively, the "detect-engine.luajit-states" var can be set. + */ +static Pool *luajit_states = NULL; +static pthread_mutex_t luajit_states_lock = SCMUTEX_INITIALIZER; + +#endif /* HAVE_LUAJIT */ + static int DetectLuajitMatch (ThreadVars *, DetectEngineThreadCtx *, Packet *, Signature *, SigMatch *); static int DetectLuajitSetup (DetectEngineCtx *, Signature *, char *); @@ -108,21 +126,6 @@ void DetectLuajitRegister(void) { return; } -/** \brief lua_State pool - * - * Luajit requires states to be alloc'd in memory <2GB. For this reason we - * prealloc the states early during engine startup so we have a better chance - * of getting the states. We protect the pool with a lock as the detect - * threads access it during their init and cleanup. - * - * Pool size is automagically determined based on number of keyword occurences, - * cpus/cores and rule reloads being enabled or not. - * - * Alternatively, the "detect-engine.luajit-states" var can be set. - */ -static Pool *luajit_states = NULL; -static pthread_mutex_t luajit_states_lock = SCMUTEX_INITIALIZER; - #define DATATYPE_PACKET (1<<0) #define DATATYPE_PAYLOAD (1<<1) #define DATATYPE_STREAM (1<<2) @@ -144,6 +147,7 @@ static pthread_mutex_t luajit_states_lock = SCMUTEX_INITIALIZER; #define DATATYPE_HTTP_RESPONSE_HEADERS (1<<13) #define DATATYPE_HTTP_RESPONSE_HEADERS_RAW (1<<14) +#ifdef HAVE_LUAJIT static void *LuaStatePoolAlloc(void) { return luaL_newstate(); } @@ -192,22 +196,31 @@ int DetectLuajitSetupStatesPool(int num, int reloads) { pthread_mutex_unlock(&luajit_states_lock); return retval; } +#endif /* HAVE_LUAJIT */ static lua_State *DetectLuajitGetState(void) { lua_State *s = NULL; +#ifdef HAVE_LUAJIT pthread_mutex_lock(&luajit_states_lock); if (luajit_states != NULL) s = (lua_State *)PoolGet(luajit_states); pthread_mutex_unlock(&luajit_states_lock); +#else + s = luaL_newstate(); +#endif return s; } static void DetectLuajitReturnState(lua_State *s) { if (s != NULL) { +#ifdef HAVE_LUAJIT pthread_mutex_lock(&luajit_states_lock); PoolReturn(luajit_states, (void *)s); pthread_mutex_unlock(&luajit_states_lock); +#else + lua_close(s); +#endif } } diff --git a/src/detect-luajit.h b/src/detect-luajit.h index 00ce650608..55eb9bde89 100644 --- a/src/detect-luajit.h +++ b/src/detect-luajit.h @@ -24,7 +24,7 @@ #ifndef __DETECT_LUAJIT_H__ #define __DETECT_LUAJIT_H__ -#ifdef HAVE_LUAJIT +#ifdef HAVE_LUA #include #include @@ -54,7 +54,8 @@ typedef struct DetectLuajitData { uint32_t rev; uint32_t gid; } DetectLuajitData; -#endif + +#endif /* HAVE_LUA */ /* prototypes */ void DetectLuajitRegister (void); diff --git a/src/detect-parse.c b/src/detect-parse.c index f147d0c798..77f50e9f36 100644 --- a/src/detect-parse.c +++ b/src/detect-parse.c @@ -1301,7 +1301,7 @@ int SigValidate(DetectEngineCtx *de_ctx, Signature *s) { } } -#ifdef HAVE_LUAJIT +#ifdef HAVE_LUA DetectLuajitPostSetup(s); #endif diff --git a/src/suricata.c b/src/suricata.c index 013fac6194..b1691cfc17 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -669,6 +669,9 @@ void SCPrintBuildInfo(void) { #ifdef HAVE_NSS strlcat(features, "HAVE_NSS ", sizeof(features)); #endif +#ifdef HAVE_LUA + strlcat(features, "HAVE_LUA ", sizeof(features)); +#endif #ifdef HAVE_LUAJIT strlcat(features, "HAVE_LUAJIT ", sizeof(features)); #endif