mirror of
https://github.com/ntop/ntopng.git
synced 2026-05-23 12:22:02 +00:00
Modified LuaJIT userdata handling
This commit is contained in:
parent
861d7662ab
commit
dc2b7fd269
6 changed files with 49 additions and 32 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue