瀏覽代碼

app_lua: implemented KSR.x.exit()

- specific extension to stop execution of lua script (similar to exit in
  kamailio.cfg)
- it's a wrapper around lua function error() with a predefined message
  handled after the return of lua_pcall()
Daniel-Constantin Mierla 9 年之前
父節點
當前提交
15499c19ba
共有 3 個文件被更改,包括 59 次插入8 次删除
  1. 40 7
      modules/app_lua/app_lua_api.c
  2. 2 0
      modules/app_lua/app_lua_api.h
  3. 17 1
      modules/app_lua/app_lua_sr.c

+ 40 - 7
modules/app_lua/app_lua_api.c

@@ -624,6 +624,19 @@ int app_lua_runstring(sip_msg_t *msg, char *script)
 	return (ret==0)?1:-1;
 }
 
+/**
+ *
+ */
+static str _sr_kemi_lua_exit_string = str_init("~~ksr~exit~~");
+
+/**
+ *
+ */
+str* sr_kemi_lua_exit_string_get(void)
+{
+	return &_sr_kemi_lua_exit_string;
+}
+
 /**
  *
  */
@@ -632,7 +645,7 @@ int app_lua_run_ex(sip_msg_t *msg, char *func, char *p1, char *p2,
 {
 	int n;
 	int ret;
-	char *txt;
+	str txt;
 	sip_msg_t *bmsg;
 
 	if(_sr_L_env.LL==NULL)
@@ -660,8 +673,8 @@ int app_lua_run_ex(sip_msg_t *msg, char *func, char *p1, char *p2,
 			LM_ERR("top stack type [%d - %s]\n",
 				lua_type(_sr_L_env.LL, -1),
 				lua_typename(_sr_L_env.LL,lua_type(_sr_L_env.LL, -1)));
-			txt = (char*)lua_tostring(_sr_L_env.LL, -1);
-			LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
+			txt.s = (char*)lua_tostring(_sr_L_env.LL, -1);
+			LM_ERR("error from Lua: %s\n", (txt.s)?txt.s:"unknown");
 			return -1;
 		} else {
 			return 1;
@@ -689,11 +702,31 @@ int app_lua_run_ex(sip_msg_t *msg, char *func, char *p1, char *p2,
 	_sr_L_env.msg = bmsg;
 	if(ret!=0)
 	{
-		LM_ERR("error executing: %s (err: %d)\n", func, ret);
-		txt = (char*)lua_tostring(_sr_L_env.LL, -1);
-		LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
+		txt.s = (char*)lua_tostring(_sr_L_env.LL, -1);
+		n = 0;
+		if(txt.s!=NULL) {
+			for(n=0; txt.s[n]!='\0' && _sr_kemi_lua_exit_string.s[n]!='\0';
+					n++) {
+				if(txt.s[n] != _sr_kemi_lua_exit_string.s[n])
+					break;
+			}
+			if(txt.s[n]!='\0' || _sr_kemi_lua_exit_string.s[n]!='\0') {
+				LM_ERR("error from Lua: %s\n", txt.s);
+				n = 0;
+			} else {
+				LM_DBG("ksr error call from Lua: %s\n", txt.s);
+				n = 1;
+			}
+		} else {
+			LM_ERR("error from Lua: unknown\n");
+		}
 		lua_pop(_sr_L_env.LL, 1);
-		return -1;
+		if(n==1) {
+			return 1;
+		} else {
+			LM_ERR("error executing: %s (err: %d)\n", func, ret);
+			return -1;
+		}
 	}
 
 	return 1;

+ 2 - 0
modules/app_lua/app_lua_api.h

@@ -87,5 +87,7 @@ int app_lua_return_error(lua_State *L);
 
 void app_lua_dump_stack(lua_State *L);
 
+str* sr_kemi_lua_exit_string_get(void);
+
 #endif
 

+ 17 - 1
modules/app_lua/app_lua_sr.c

@@ -1688,11 +1688,27 @@ int sr_kemi_exec_func(lua_State* L, str *mname, int midx, str *fname)
 	return app_lua_return_false(L);
 }
 
+/**
+ *
+ */
+static int sr_kemi_lua_exit (lua_State *L)
+{
+	str *s;
+
+	LM_DBG("script exit call\n");
+	s = sr_kemi_lua_exit_string_get();
+	lua_getglobal(L, "error");
+	lua_pushstring(L, s->s);
+	lua_call(L, 1, 0);
+	return 0;
+}
+
 /**
  *
  */
 static const luaL_Reg _sr_kemi_x_Map [] = {
-	{"modf",         lua_sr_modf},
+	{"modf",      lua_sr_modf},
+	{"exit",      sr_kemi_lua_exit},
 	{NULL, NULL}
 };