diff --git a/include/ntop_defines.h b/include/ntop_defines.h index 9072509815..49c5984208 100644 --- a/include/ntop_defines.h +++ b/include/ntop_defines.h @@ -763,7 +763,7 @@ is 5 secs, a flow aggregation duration of 12 equals to 1 minute. */ #define FLOW_AGGREGATION_DURATION 12 * 5 /* 1 minute * 5 = 5 minutes */ -#define getLuaVMUserdata(a,b) (a ? ((struct ntopngLuaContext*)a->userdata)->b : NULL) -#define getLuaVMUservalue(a,b) ((struct ntopngLuaContext*)a->userdata)->b +#define getLuaVMUserdata(a,b) (a ? ((struct ntopngLuaContext*)G(a)->userdata)->b : NULL) +#define getLuaVMUservalue(a,b) ((struct ntopngLuaContext*)G(a)->userdata)->b #endif /* _NTOP_DEFINES_H_ */ diff --git a/src/Lua.cpp b/src/Lua.cpp index 033b8da135..a245c11022 100644 --- a/src/Lua.cpp +++ b/src/Lua.cpp @@ -43,23 +43,22 @@ struct keyval string_to_replace[MAX_NUM_HTTP_REPLACEMENTS] = { { NULL, NULL } }; /* ******************************* */ Lua::Lua() { - void *userdata; - L = luaL_newstate(); - if(L) userdata = (void*)calloc(1, sizeof(struct ntopngLuaContext)); - if((L == NULL) || (userdata == NULL)) { + if (L) G(L)->userdata = NULL; + + if(L) G(L)->userdata = (void*)calloc(1, sizeof(struct ntopngLuaContext)); + if((L == NULL) || (G(L)->userdata == NULL)) { ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to create Lua interpreter"); return; - } else - lua_setuserdata(L, userdata); + } } /* ******************************* */ Lua::~Lua() { if(L) { - if(L->userdata) free(L->userdata); + if(G(L)->userdata) free(G(L)->userdata); lua_close(L); } } diff --git a/src/NetworkInterface.cpp b/src/NetworkInterface.cpp index 20727a2253..60161e544a 100644 --- a/src/NetworkInterface.cpp +++ b/src/NetworkInterface.cpp @@ -5211,6 +5211,8 @@ static const ntop_class_reg ntop_lua_reg[] = { {NULL, NULL} }; + /* ******************************************** */ + lua_State* NetworkInterface::initUserScriptsInterpreter(const char *lua_file, const char *context) { int i; char script_path[256]; @@ -5218,8 +5220,8 @@ lua_State* NetworkInterface::initUserScriptsInterpreter(const char *lua_file, co L = luaL_newstate(); - if(L) L->userdata = (void*)calloc(1, sizeof(struct ntopngLuaContext)); - if((L == NULL) || (L->userdata == NULL)) { + if(L) G(L)->userdata = (void*)calloc(1, sizeof(struct ntopngLuaContext)); + if((L == NULL) || (G(L)->userdata == NULL)) { ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to initialize lua interpreter"); return(NULL); } @@ -5245,7 +5247,7 @@ lua_State* NetworkInterface::initUserScriptsInterpreter(const char *lua_file, co if(luaL_loadfile(L, script_path) || lua_pcall(L, 0, 0, 0)) { ntop->getTrace()->traceEvent(TRACE_WARNING, "Cannot run lua file %s[%s]: %s", script_path, context, lua_tostring(L, -1)); - if(L->userdata) free(L->userdata); + if(G(L)->userdata) free(G(L)->userdata); lua_close(L); L = NULL; } else { @@ -5261,8 +5263,21 @@ lua_State* NetworkInterface::initUserScriptsInterpreter(const char *lua_file, co /* **************************************** */ void NetworkInterface::termLuaInterpreter() { - if(L_user_scripts_inline) { if(L_user_scripts_inline->userdata) free(L_user_scripts_inline->userdata); lua_close(L_user_scripts_inline); L_user_scripts_inline = NULL; } - if(L_user_scripts_periodic) { if(L_user_scripts_periodic->userdata) free(L_user_scripts_periodic->userdata); lua_close(L_user_scripts_periodic); L_user_scripts_periodic = NULL; } + if(L_user_scripts_inline) { + if(G(L_user_scripts_inline)->userdata) + free(G(L_user_scripts_inline)->userdata); + + lua_close(L_user_scripts_inline); + L_user_scripts_inline = NULL; + } + + if(L_user_scripts_periodic) { + if(G(L_user_scripts_periodic)->userdata) + free(G(L_user_scripts_periodic)->userdata); + + lua_close(L_user_scripts_periodic); + L_user_scripts_periodic = NULL; + } } /* **************************************** */ @@ -5301,8 +5316,14 @@ int NetworkInterface::luaEvalFlow(Flow *f, const LuaCallback cb) { switch(context) { case user_script_context_inline: if (user_scripts_reload_inline) { - if(L_user_scripts_inline) { if(L_user_scripts_inline->userdata) free(L_user_scripts_inline->userdata); lua_close(L_user_scripts_inline); } - L_user_scripts_inline = initUserScriptsInterpreter(CONST_USER_SCRIPTS_LOADER, CONST_USER_SCRIPTS_CONTEXT_INLINE); + if(L_user_scripts_inline) { + if(G(L_user_scripts_inline)->userdata) + free(G(L_user_scripts_inline)->userdata); + lua_close(L_user_scripts_inline); + } + + L_user_scripts_inline = initUserScriptsInterpreter(CONST_USER_SCRIPTS_LOADER, + CONST_USER_SCRIPTS_CONTEXT_INLINE); user_scripts_reload_inline = false; } @@ -5310,8 +5331,14 @@ int NetworkInterface::luaEvalFlow(Flow *f, const LuaCallback cb) { break; case user_script_context_periodic: if (user_scripts_reload_periodic) { - if(L_user_scripts_periodic) { if(L_user_scripts_periodic->userdata) free(L_user_scripts_periodic->userdata); lua_close(L_user_scripts_periodic); } - L_user_scripts_periodic = initUserScriptsInterpreter(CONST_USER_SCRIPTS_LOADER, CONST_USER_SCRIPTS_CONTEXT_PERIODIC); + if(L_user_scripts_periodic) { + if(G(L_user_scripts_periodic)->userdata) + free(G(L_user_scripts_periodic)->userdata); + lua_close(L_user_scripts_periodic); + } + + L_user_scripts_periodic = initUserScriptsInterpreter(CONST_USER_SCRIPTS_LOADER, + CONST_USER_SCRIPTS_CONTEXT_PERIODIC); user_scripts_reload_periodic = false; } @@ -5328,7 +5355,8 @@ int NetworkInterface::luaEvalFlow(Flow *f, const LuaCallback cb) { lua_getglobal(L, luaFunction); /* function to be called */ if((rc = lua_pcall(L, 0 /* 0 parameters */, 0 /* no return values */, 0)) != 0) { - ntop->getTrace()->traceEvent(TRACE_WARNING, "Error while executing %s [rc=%d][%s]", luaFunction, rc, lua_tostring(L, -1)); + ntop->getTrace()->traceEvent(TRACE_WARNING, "Error while executing %s [rc=%d][%s]", + luaFunction, rc, lua_tostring(L, -1)); } return(rc); diff --git a/third-party/LuaJIT-2.1.0-beta3/src/lj_api.c b/third-party/LuaJIT-2.1.0-beta3/src/lj_api.c index fa52cfb9d7..ed3aa56f30 100644 --- a/third-party/LuaJIT-2.1.0-beta3/src/lj_api.c +++ b/third-party/LuaJIT-2.1.0-beta3/src/lj_api.c @@ -1289,8 +1289,3 @@ LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud) g->allocd = ud; g->allocf = f; } - -/* NTOP BEGIN */ -void* lua_getuserdata(struct lua_State *vm) { return(vm->userdata); } -void lua_setuserdata(struct lua_State *vm, void *v) { vm->userdata = v; } -/* NTOP END */ diff --git a/third-party/LuaJIT-2.1.0-beta3/src/lj_obj.h b/third-party/LuaJIT-2.1.0-beta3/src/lj_obj.h index fdc20e95d6..07e1fd3b3b 100644 --- a/third-party/LuaJIT-2.1.0-beta3/src/lj_obj.h +++ b/third-party/LuaJIT-2.1.0-beta3/src/lj_obj.h @@ -621,6 +621,9 @@ typedef struct global_State { MRef jit_base; /* Current JIT code L->base or NULL. */ MRef ctype_state; /* Pointer to C type state. */ GCRef gcroot[GCROOT_MAX]; /* GC roots. */ + /* NTOP BEGIN */ + void *userdata; + /* NTOP END */ } global_State; #define mainthread(g) (&gcref(g->mainthref)->th) @@ -660,9 +663,6 @@ struct lua_State { GCRef env; /* Thread environment (table of globals). */ void *cframe; /* End of C stack frame chain. */ MSize stacksize; /* True stack size (incl. LJ_STACK_EXTRA). */ - /* NTOP BEGIN */ - void *userdata; - /* NTOP END */ }; #define G(L) (mref(L->glref, global_State)) diff --git a/third-party/LuaJIT-2.1.0-beta3/src/lua.h b/third-party/LuaJIT-2.1.0-beta3/src/lua.h index 01a2d01482..850bd796ca 100644 --- a/third-party/LuaJIT-2.1.0-beta3/src/lua.h +++ b/third-party/LuaJIT-2.1.0-beta3/src/lua.h @@ -50,11 +50,6 @@ typedef struct lua_State lua_State; -/* NTOP BEGIN */ -extern void* lua_getuserdata(struct lua_State *vm); -extern void lua_setuserdata(struct lua_State *vm, void *v); -/* NTOP END */ - typedef int (*lua_CFunction) (lua_State *L);