app_lua_api.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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_boolean(lua_State *L, int b)
  247. {
  248. if(b==SRLUA_FALSE)
  249. lua_pushboolean(L, SRLUA_FALSE);
  250. else
  251. lua_pushboolean(L, SRLUA_TRUE);
  252. return 1;
  253. }
  254. /**
  255. *
  256. */
  257. int app_lua_return_false(lua_State *L)
  258. {
  259. lua_pushboolean(L, SRLUA_FALSE);
  260. return 1;
  261. }
  262. /**
  263. *
  264. */
  265. int app_lua_return_true(lua_State *L)
  266. {
  267. lua_pushboolean(L, SRLUA_TRUE);
  268. return 1;
  269. }
  270. /**
  271. *
  272. */
  273. int app_lua_dostring(struct sip_msg *msg, char *script)
  274. {
  275. int ret;
  276. char *txt;
  277. LM_DBG("executing Lua string: [[%s]]\n", script);
  278. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.L));
  279. _sr_L_env.msg = msg;
  280. ret = luaL_dostring(_sr_L_env.L, script);
  281. if(ret!=0)
  282. {
  283. txt = (char*)lua_tostring(_sr_L_env.L, -1);
  284. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  285. lua_pop (_sr_L_env.L, 1);
  286. }
  287. _sr_L_env.msg = 0;
  288. return (ret==0)?1:-1;
  289. }
  290. /**
  291. *
  292. */
  293. int app_lua_dofile(struct sip_msg *msg, char *script)
  294. {
  295. int ret;
  296. char *txt;
  297. LM_DBG("executing Lua file: [[%s]]\n", script);
  298. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.L));
  299. _sr_L_env.msg = msg;
  300. ret = luaL_dofile(_sr_L_env.L, script);
  301. if(ret!=0)
  302. {
  303. txt = (char*)lua_tostring(_sr_L_env.L, -1);
  304. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  305. lua_pop(_sr_L_env.L, 1);
  306. }
  307. _sr_L_env.msg = 0;
  308. return (ret==0)?1:-1;
  309. }
  310. /**
  311. *
  312. */
  313. int app_lua_runstring(struct sip_msg *msg, char *script)
  314. {
  315. int ret;
  316. char *txt;
  317. if(_sr_L_env.LL==NULL)
  318. {
  319. LM_ERR("lua loading state not initialized (call: %s)\n", script);
  320. return -1;
  321. }
  322. LM_DBG("running Lua string: [[%s]]\n", script);
  323. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.LL));
  324. _sr_L_env.msg = msg;
  325. ret = luaL_dostring(_sr_L_env.LL, script);
  326. if(ret!=0)
  327. {
  328. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  329. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  330. lua_pop (_sr_L_env.LL, 1);
  331. }
  332. _sr_L_env.msg = 0;
  333. return (ret==0)?1:-1;
  334. }
  335. /**
  336. *
  337. */
  338. int app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
  339. char *p3)
  340. {
  341. int n;
  342. int ret;
  343. char *txt;
  344. if(_sr_L_env.LL==NULL)
  345. {
  346. LM_ERR("lua loading state not initialized (call: %s)\n", func);
  347. return -1;
  348. }
  349. LM_DBG("executing Lua function: [[%s]]\n", func);
  350. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.LL));
  351. lua_getglobal(_sr_L_env.LL, func);
  352. if(!lua_isfunction(_sr_L_env.LL, -1))
  353. {
  354. LM_ERR("no such function [%s] in lua scripts\n", func);
  355. LM_ERR("top stack type [%d - %s]\n",
  356. lua_type(_sr_L_env.LL, -1),
  357. lua_typename(_sr_L_env.LL,lua_type(_sr_L_env.LL, -1)));
  358. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  359. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  360. return -1;
  361. }
  362. n = 0;
  363. if(p1!=NULL)
  364. {
  365. lua_pushstring(_sr_L_env.LL, p1);
  366. n++;
  367. if(p2!=NULL)
  368. {
  369. lua_pushstring(_sr_L_env.LL, p2);
  370. n++;
  371. if(p3!=NULL)
  372. {
  373. lua_pushstring(_sr_L_env.LL, p3);
  374. n++;
  375. }
  376. }
  377. }
  378. _sr_L_env.msg = msg;
  379. ret = lua_pcall(_sr_L_env.LL, n, 0, 0);
  380. _sr_L_env.msg = 0;
  381. if(ret!=0)
  382. {
  383. LM_ERR("error executing: %s (err: %d)\n", func, ret);
  384. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  385. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  386. lua_pop(_sr_L_env.LL, 1);
  387. return -1;
  388. }
  389. return 1;
  390. }
  391. void app_lua_dump_stack(lua_State *L)
  392. {
  393. int i;
  394. int t;
  395. int top;
  396. top = lua_gettop(L);
  397. LM_DBG("lua stack top index: %d\n", top);
  398. for (i = 1; i <= top; i++)
  399. {
  400. t = lua_type(L, i);
  401. switch (t)
  402. {
  403. case LUA_TSTRING: /* strings */
  404. LM_DBG("[%i:s> %s\n", i, lua_tostring(L, i));
  405. break;
  406. case LUA_TBOOLEAN: /* booleans */
  407. LM_DBG("[%i:b> %s\n", i,
  408. lua_toboolean(L, i) ? "true" : "false");
  409. break;
  410. case LUA_TNUMBER: /* numbers */
  411. LM_DBG("[%i:n> %g\n", i, lua_tonumber(L, i));
  412. break;
  413. default: /* other values */
  414. LM_DBG("[%i:t> %s\n", i, lua_typename(L, t));
  415. break;
  416. }
  417. }
  418. }