Browse Source

app_lua: do basic Lua probing before fork

- check if Lua files given to load parameter exist
Daniel-Constantin Mierla 15 năm trước cách đây
mục cha
commit
d098c129dc

+ 2 - 1
modules/app_lua/Makefile

@@ -10,7 +10,8 @@ auto_gen=
 NAME=app_lua.so
 LIBS= -llua5.1
 
-DEFS+=-DOPENSER_MOD_INTERFACE -I/usr/include/lua5.1/
+DEFS+=-I/usr/include/lua5.1
+DEFS+=-DOPENSER_MOD_INTERFACE
 
 SERLIBPATH=../../lib
 SER_LIBS+=$(SERLIBPATH)/kcore/kcore

+ 63 - 22
modules/app_lua/app_lua_api.c

@@ -120,6 +120,57 @@ int lua_sr_init_mod(void)
 	return 0;
 }
 
+/**
+ *
+ */
+int lua_sr_init_probe(void)
+{
+	lua_State *L;
+	char *txt;
+	sr_lua_load_t *li;
+	struct stat sbuf;
+
+	L = lua_open();
+	if(L==NULL)
+	{
+		LM_ERR("cannot open lua\n");
+		return -1;
+	}
+	luaL_openlibs(L);
+	lua_sr_openlibs(L);
+
+	/* force loading lua lib now */
+	if(luaL_dostring(L, "sr.probe()")!=0)
+	{
+		txt = (char*)lua_tostring(L, -1);
+		LM_ERR("error initializing Lua: %s\n", (txt)?txt:"unknown");
+		lua_pop(L, 1);
+		lua_close(L);
+		return -1;
+	}
+
+	/* test if files to be loaded exist */
+	if(_sr_lua_load_list != NULL)
+	{
+		li = _sr_lua_load_list;
+		while(li)
+		{
+			if(stat(li->script, &sbuf)!=0)
+			{
+				/* file does not exist */
+				LM_ERR("cannot find script: %s (wrong path?)\n",
+						li->script);
+				lua_close(L);
+				return -1;
+			}
+			li = li->next;
+		}
+	}
+	lua_close(L);
+	LM_DBG("Lua probe was ok!\n");
+	return 0;
+}
+
 /**
  *
  */
@@ -128,7 +179,6 @@ int lua_sr_init_child(void)
 	sr_lua_load_t *li;
 	int ret;
 	char *txt;
-	struct stat sbuf;
 
 	memset(&_sr_L_env, 0, sizeof(sr_lua_env_t));
 	_sr_L_env.L = lua_open();
@@ -147,7 +197,7 @@ int lua_sr_init_child(void)
 
 	if(_sr_lua_load_list != NULL)
 	{
-		_sr_L_env.LL = lua_open();
+		_sr_L_env.LL = luaL_newstate();
 		if(_sr_L_env.LL==NULL)
 		{
 			LM_ERR("cannot open lua loading state\n");
@@ -161,18 +211,20 @@ int lua_sr_init_child(void)
 		lua_pushstring(_sr_L_env.LL, SRVERSION);
 		lua_settable(_sr_L_env.LL, LUA_GLOBALSINDEX);
 
+		/* force loading lua lib now */
+		if(luaL_dostring(_sr_L_env.LL, "sr.probe()")!=0)
+		{
+			txt = (char*)lua_tostring(_sr_L_env.LL, -1);
+			LM_ERR("error initializing Lua: %s\n", (txt)?txt:"unknown");
+			lua_pop(_sr_L_env.LL, 1);
+			lua_sr_destroy();
+			return -1;
+		}
+
 		li = _sr_lua_load_list;
 		while(li)
 		{
-			if(stat(li->script, &sbuf)!=0)
-			{
-				/* file does not exist */
-				LM_ERR("cannot find script: %s (wrong path?)\n",
-						li->script);
-				lua_sr_destroy();
-				return -1;
-			}
- 			ret = luaL_loadfile(_sr_L_env.LL, (const char*)li->script);
+			ret = luaL_dofile(_sr_L_env.LL, (const char*)li->script);
 			if(ret!=0)
 			{
 				LM_ERR("failed to load Lua script: %s (err: %d)\n",
@@ -183,17 +235,6 @@ int lua_sr_init_child(void)
 				lua_sr_destroy();
 				return -1;
 			}
-			ret = lua_pcall(_sr_L_env.LL, 0, 0, 0);
-			if(ret!=0)
-			{
-				LM_ERR("failed to init Lua script: %s (err: %d)\n",
-						li->script, ret);
-				txt = (char*)lua_tostring(_sr_L_env.LL, -1);
-				LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
-				lua_pop(_sr_L_env.LL, 1);
-				lua_sr_destroy();
-				return -1;
-			}
 			li = li->next;
 		}
 	}

+ 1 - 0
modules/app_lua/app_lua_api.h

@@ -44,6 +44,7 @@ int lua_sr_initialized(void);
 int lua_sr_init_mod(void);
 int lua_sr_init_child(void);
 void lua_sr_destroy(void);
+int lua_sr_init_probe(void);
 
 int sr_lua_load_script(char *script);
 int sr_lua_register_module(char *mname);

+ 9 - 2
modules/app_lua/app_lua_mod.c

@@ -86,7 +86,7 @@ static cmd_export_t cmds[]={
 
 struct module_exports exports = {
 	"app_lua",
-	DEFAULT_DLFLAGS, /* dlopen flags */
+	RTLD_NOW | RTLD_GLOBAL, /* dlopen flags */
 	cmds,
 	params,
 	0,
@@ -114,9 +114,16 @@ static int mod_init(void)
 /* each child get a new connection to the database */
 static int child_init(int rank)
 {
-	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+	if(rank==PROC_MAIN || rank==PROC_TCP_MAIN)
 		return 0; /* do nothing for the main process */
 
+	if (rank==PROC_INIT)
+	{
+		/* do a probe before forking */
+		if(lua_sr_init_probe()!=0)
+			return -1;
+		return 0;
+	}
 	return lua_sr_init_child();
 }
 

+ 10 - 0
modules/app_lua/app_lua_sr.c

@@ -37,6 +37,15 @@
 #include "app_lua_api.h"
 #include "app_lua_sr.h"
 
+/**
+ *
+ */
+static int lua_sr_probe (lua_State *L)
+{
+	LM_DBG("someone probing from lua\n");
+	return 0;
+}
+
 /**
  *
  */
@@ -92,6 +101,7 @@ static int lua_sr_log (lua_State *L)
  *
  */
 static const luaL_reg _sr_core_Map [] = {
+	{"probe", lua_sr_probe},
 	{"dbg", lua_sr_dbg},
 	{"err", lua_sr_err},
 	{"log", lua_sr_log},