app_lua_api.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <sys/stat.h>
  27. #include "../../sr_module.h"
  28. #include "../../dprint.h"
  29. #include "../../ut.h"
  30. #include "../../mem/mem.h"
  31. #include "../../data_lump.h"
  32. #include "../../data_lump_rpl.h"
  33. #include "../../lib/kcore/cmpapi.h"
  34. #include "app_lua_api.h"
  35. #include "app_lua_sr.h"
  36. #include "app_lua_exp.h"
  37. #define SRVERSION "1.0"
  38. /**
  39. *
  40. */
  41. static sr_lua_env_t _sr_L_env;
  42. /**
  43. * @return the static Lua env
  44. */
  45. sr_lua_env_t *sr_lua_env_get(void)
  46. {
  47. return &_sr_L_env;
  48. }
  49. /**
  50. *
  51. */
  52. typedef struct _sr_lua_load
  53. {
  54. char *script;
  55. struct _sr_lua_load *next;
  56. } sr_lua_load_t;
  57. /**
  58. *
  59. */
  60. static sr_lua_load_t *_sr_lua_load_list = NULL;
  61. /**
  62. *
  63. */
  64. int sr_lua_load_script(char *script)
  65. {
  66. sr_lua_load_t *li;
  67. li = (sr_lua_load_t*)pkg_malloc(sizeof(sr_lua_load_t));
  68. if(li==NULL)
  69. {
  70. LM_ERR("no more pkg\n");
  71. return -1;
  72. }
  73. memset(li, 0, sizeof(sr_lua_load_t));
  74. li->script = script;
  75. li->next = _sr_lua_load_list;
  76. _sr_lua_load_list = li;
  77. return 0;
  78. }
  79. /**
  80. *
  81. */
  82. int sr_lua_register_module(char *mname)
  83. {
  84. if(lua_sr_exp_register_mod(mname)==0)
  85. return 0;
  86. return -1;
  87. }
  88. /**
  89. *
  90. */
  91. void lua_sr_openlibs(lua_State *L)
  92. {
  93. lua_sr_core_openlibs(L);
  94. lua_sr_exp_openlibs(L);
  95. }
  96. /**
  97. *
  98. */
  99. int lua_sr_init_mod(void)
  100. {
  101. memset(&_sr_L_env, 0, sizeof(sr_lua_env_t));
  102. if(lua_sr_exp_init_mod()<0)
  103. return -1;
  104. return 0;
  105. }
  106. /**
  107. *
  108. */
  109. int lua_sr_init_probe(void)
  110. {
  111. lua_State *L;
  112. char *txt;
  113. sr_lua_load_t *li;
  114. struct stat sbuf;
  115. L = lua_open();
  116. if(L==NULL)
  117. {
  118. LM_ERR("cannot open lua\n");
  119. return -1;
  120. }
  121. luaL_openlibs(L);
  122. lua_sr_openlibs(L);
  123. /* force loading lua lib now */
  124. if(luaL_dostring(L, "sr.probe()")!=0)
  125. {
  126. txt = (char*)lua_tostring(L, -1);
  127. LM_ERR("error initializing Lua: %s\n", (txt)?txt:"unknown");
  128. lua_pop(L, 1);
  129. lua_close(L);
  130. return -1;
  131. }
  132. /* test if files to be loaded exist */
  133. if(_sr_lua_load_list != NULL)
  134. {
  135. li = _sr_lua_load_list;
  136. while(li)
  137. {
  138. if(stat(li->script, &sbuf)!=0)
  139. {
  140. /* file does not exist */
  141. LM_ERR("cannot find script: %s (wrong path?)\n",
  142. li->script);
  143. lua_close(L);
  144. return -1;
  145. }
  146. li = li->next;
  147. }
  148. }
  149. lua_close(L);
  150. LM_DBG("Lua probe was ok!\n");
  151. return 0;
  152. }
  153. /**
  154. *
  155. */
  156. int lua_sr_init_child(void)
  157. {
  158. sr_lua_load_t *li;
  159. int ret;
  160. char *txt;
  161. memset(&_sr_L_env, 0, sizeof(sr_lua_env_t));
  162. _sr_L_env.L = lua_open();
  163. if(_sr_L_env.L==NULL)
  164. {
  165. LM_ERR("cannot open lua\n");
  166. return -1;
  167. }
  168. luaL_openlibs(_sr_L_env.L);
  169. lua_sr_openlibs(_sr_L_env.L);
  170. /* set SR lib version */
  171. lua_pushstring(_sr_L_env.L, "SRVERSION");
  172. lua_pushstring(_sr_L_env.L, SRVERSION);
  173. lua_settable(_sr_L_env.L, LUA_GLOBALSINDEX);
  174. if(_sr_lua_load_list != NULL)
  175. {
  176. _sr_L_env.LL = luaL_newstate();
  177. if(_sr_L_env.LL==NULL)
  178. {
  179. LM_ERR("cannot open lua loading state\n");
  180. return -1;
  181. }
  182. luaL_openlibs(_sr_L_env.LL);
  183. lua_sr_openlibs(_sr_L_env.LL);
  184. /* set SR lib version */
  185. lua_pushstring(_sr_L_env.LL, "SRVERSION");
  186. lua_pushstring(_sr_L_env.LL, SRVERSION);
  187. lua_settable(_sr_L_env.LL, LUA_GLOBALSINDEX);
  188. /* force loading lua lib now */
  189. if(luaL_dostring(_sr_L_env.LL, "sr.probe()")!=0)
  190. {
  191. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  192. LM_ERR("error initializing Lua: %s\n", (txt)?txt:"unknown");
  193. lua_pop(_sr_L_env.LL, 1);
  194. lua_sr_destroy();
  195. return -1;
  196. }
  197. li = _sr_lua_load_list;
  198. while(li)
  199. {
  200. ret = luaL_dofile(_sr_L_env.LL, (const char*)li->script);
  201. if(ret!=0)
  202. {
  203. LM_ERR("failed to load Lua script: %s (err: %d)\n",
  204. li->script, ret);
  205. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  206. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  207. lua_pop(_sr_L_env.LL, 1);
  208. lua_sr_destroy();
  209. return -1;
  210. }
  211. li = li->next;
  212. }
  213. }
  214. LM_DBG("Lua initialized!\n");
  215. return 0;
  216. }
  217. /**
  218. *
  219. */
  220. void lua_sr_destroy(void)
  221. {
  222. if(_sr_L_env.L!=NULL)
  223. {
  224. lua_close(_sr_L_env.L);
  225. _sr_L_env.L = NULL;
  226. }
  227. if(_sr_L_env.LL!=NULL)
  228. {
  229. lua_close(_sr_L_env.LL);
  230. _sr_L_env.LL = NULL;
  231. }
  232. memset(&_sr_L_env, 0, sizeof(sr_lua_env_t));
  233. }
  234. /**
  235. *
  236. */
  237. int lua_sr_initialized(void)
  238. {
  239. if(_sr_L_env.L==NULL)
  240. return 0;
  241. return 1;
  242. }
  243. /**
  244. *
  245. */
  246. int app_lua_return_int(lua_State *L, int v)
  247. {
  248. lua_pushinteger(L, v);
  249. return 1;
  250. }
  251. /**
  252. *
  253. */
  254. int app_lua_return_error(lua_State *L)
  255. {
  256. lua_pushinteger(L, -1);
  257. return 1;
  258. }
  259. /**
  260. *
  261. */
  262. int app_lua_return_boolean(lua_State *L, int b)
  263. {
  264. if(b==SRLUA_FALSE)
  265. lua_pushboolean(L, SRLUA_FALSE);
  266. else
  267. lua_pushboolean(L, SRLUA_TRUE);
  268. return 1;
  269. }
  270. /**
  271. *
  272. */
  273. int app_lua_return_false(lua_State *L)
  274. {
  275. lua_pushboolean(L, SRLUA_FALSE);
  276. return 1;
  277. }
  278. /**
  279. *
  280. */
  281. int app_lua_return_true(lua_State *L)
  282. {
  283. lua_pushboolean(L, SRLUA_TRUE);
  284. return 1;
  285. }
  286. /**
  287. *
  288. */
  289. int app_lua_dostring(struct sip_msg *msg, char *script)
  290. {
  291. int ret;
  292. char *txt;
  293. LM_DBG("executing Lua string: [[%s]]\n", script);
  294. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.L));
  295. _sr_L_env.msg = msg;
  296. ret = luaL_dostring(_sr_L_env.L, script);
  297. if(ret!=0)
  298. {
  299. txt = (char*)lua_tostring(_sr_L_env.L, -1);
  300. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  301. lua_pop (_sr_L_env.L, 1);
  302. }
  303. _sr_L_env.msg = 0;
  304. return (ret==0)?1:-1;
  305. }
  306. /**
  307. *
  308. */
  309. int app_lua_dofile(struct sip_msg *msg, char *script)
  310. {
  311. int ret;
  312. char *txt;
  313. LM_DBG("executing Lua file: [[%s]]\n", script);
  314. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.L));
  315. _sr_L_env.msg = msg;
  316. ret = luaL_dofile(_sr_L_env.L, script);
  317. if(ret!=0)
  318. {
  319. txt = (char*)lua_tostring(_sr_L_env.L, -1);
  320. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  321. lua_pop(_sr_L_env.L, 1);
  322. }
  323. _sr_L_env.msg = 0;
  324. return (ret==0)?1:-1;
  325. }
  326. /**
  327. *
  328. */
  329. int app_lua_runstring(struct sip_msg *msg, char *script)
  330. {
  331. int ret;
  332. char *txt;
  333. if(_sr_L_env.LL==NULL)
  334. {
  335. LM_ERR("lua loading state not initialized (call: %s)\n", script);
  336. return -1;
  337. }
  338. LM_DBG("running Lua string: [[%s]]\n", script);
  339. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.LL));
  340. _sr_L_env.msg = msg;
  341. ret = luaL_dostring(_sr_L_env.LL, script);
  342. if(ret!=0)
  343. {
  344. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  345. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  346. lua_pop (_sr_L_env.LL, 1);
  347. }
  348. _sr_L_env.msg = 0;
  349. return (ret==0)?1:-1;
  350. }
  351. /**
  352. *
  353. */
  354. int app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
  355. char *p3)
  356. {
  357. int n;
  358. int ret;
  359. char *txt;
  360. if(_sr_L_env.LL==NULL)
  361. {
  362. LM_ERR("lua loading state not initialized (call: %s)\n", func);
  363. return -1;
  364. }
  365. LM_DBG("executing Lua function: [[%s]]\n", func);
  366. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.LL));
  367. lua_getglobal(_sr_L_env.LL, func);
  368. if(!lua_isfunction(_sr_L_env.LL, -1))
  369. {
  370. LM_ERR("no such function [%s] in lua scripts\n", func);
  371. LM_ERR("top stack type [%d - %s]\n",
  372. lua_type(_sr_L_env.LL, -1),
  373. lua_typename(_sr_L_env.LL,lua_type(_sr_L_env.LL, -1)));
  374. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  375. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  376. return -1;
  377. }
  378. n = 0;
  379. if(p1!=NULL)
  380. {
  381. lua_pushstring(_sr_L_env.LL, p1);
  382. n++;
  383. if(p2!=NULL)
  384. {
  385. lua_pushstring(_sr_L_env.LL, p2);
  386. n++;
  387. if(p3!=NULL)
  388. {
  389. lua_pushstring(_sr_L_env.LL, p3);
  390. n++;
  391. }
  392. }
  393. }
  394. _sr_L_env.msg = msg;
  395. ret = lua_pcall(_sr_L_env.LL, n, 0, 0);
  396. _sr_L_env.msg = 0;
  397. if(ret!=0)
  398. {
  399. LM_ERR("error executing: %s (err: %d)\n", func, ret);
  400. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  401. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  402. lua_pop(_sr_L_env.LL, 1);
  403. return -1;
  404. }
  405. return 1;
  406. }
  407. void app_lua_dump_stack(lua_State *L)
  408. {
  409. int i;
  410. int t;
  411. int top;
  412. top = lua_gettop(L);
  413. LM_DBG("lua stack top index: %d\n", top);
  414. for (i = 1; i <= top; i++)
  415. {
  416. t = lua_type(L, i);
  417. switch (t)
  418. {
  419. case LUA_TSTRING: /* strings */
  420. LM_DBG("[%i:s> %s\n", i, lua_tostring(L, i));
  421. break;
  422. case LUA_TBOOLEAN: /* booleans */
  423. LM_DBG("[%i:b> %s\n", i,
  424. lua_toboolean(L, i) ? "true" : "false");
  425. break;
  426. case LUA_TNUMBER: /* numbers */
  427. LM_DBG("[%i:n> %g\n", i, lua_tonumber(L, i));
  428. break;
  429. default: /* other values */
  430. LM_DBG("[%i:t> %s\n", i, lua_typename(L, t));
  431. break;
  432. }
  433. }
  434. }