Module: sip-router Branch: master Commit: d098c129dcd1f8a7d28ae998e8a0094faac4bf58 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=d098c129...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Sat Jan 30 17:05:12 2010 +0100
app_lua: do basic Lua probing before fork
- check if Lua files given to load parameter exist
---
modules/app_lua/Makefile | 3 +- modules/app_lua/app_lua_api.c | 85 ++++++++++++++++++++++++++++++----------- modules/app_lua/app_lua_api.h | 1 + modules/app_lua/app_lua_mod.c | 11 ++++- modules/app_lua/app_lua_sr.c | 10 +++++ 5 files changed, 85 insertions(+), 25 deletions(-)
diff --git a/modules/app_lua/Makefile b/modules/app_lua/Makefile index 605eb34..a466ab9 100644 --- a/modules/app_lua/Makefile +++ b/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 diff --git a/modules/app_lua/app_lua_api.c b/modules/app_lua/app_lua_api.c index 88ee832..5e35436 100644 --- a/modules/app_lua/app_lua_api.c +++ b/modules/app_lua/app_lua_api.c @@ -123,12 +123,62 @@ int lua_sr_init_mod(void) /** * */ +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; +} + +/** + * + */ 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; } } diff --git a/modules/app_lua/app_lua_api.h b/modules/app_lua/app_lua_api.h index fbf12d5..51ebc15 100644 --- a/modules/app_lua/app_lua_api.h +++ b/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); diff --git a/modules/app_lua/app_lua_mod.c b/modules/app_lua/app_lua_mod.c index bc7fc2c..f9e1a69 100644 --- a/modules/app_lua/app_lua_mod.c +++ b/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(); }
diff --git a/modules/app_lua/app_lua_sr.c b/modules/app_lua/app_lua_sr.c index aa98d74..0cf935d 100644 --- a/modules/app_lua/app_lua_sr.c +++ b/modules/app_lua/app_lua_sr.c @@ -40,6 +40,15 @@ /** * */ +static int lua_sr_probe (lua_State *L) +{ + LM_DBG("someone probing from lua\n"); + return 0; +} + +/** + * + */ static int lua_sr_dbg (lua_State *L) { char *txt; @@ -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},