app_lua_api.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 "../../locking.h"
  32. #include "../../data_lump.h"
  33. #include "../../data_lump_rpl.h"
  34. #include "../../lib/kcore/cmpapi.h"
  35. #include "app_lua_api.h"
  36. #include "app_lua_sr.h"
  37. #include "app_lua_exp.h"
  38. #define SRVERSION "1.0"
  39. /**
  40. *
  41. */
  42. static sr_lua_env_t _sr_L_env;
  43. /**
  44. * @return the static Lua env
  45. */
  46. sr_lua_env_t *sr_lua_env_get(void)
  47. {
  48. return &_sr_L_env;
  49. }
  50. /**
  51. *
  52. */
  53. static sr_lua_load_t *_sr_lua_load_list = NULL;
  54. /**
  55. * set of locks to manage the shared variable.
  56. */
  57. static gen_lock_set_t *sr_lua_locks = NULL;
  58. static sr_lua_script_ver_t *sr_lua_script_ver = NULL;
  59. int lua_sr_alloc_script_ver(void)
  60. {
  61. int size = _sr_L_env.nload;
  62. sr_lua_script_ver = (sr_lua_script_ver_t *) shm_malloc(sizeof(sr_lua_script_ver_t));
  63. if(sr_lua_script_ver==NULL)
  64. {
  65. LM_ERR("cannot allocate shm memory\n");
  66. return -1;
  67. }
  68. sr_lua_script_ver->version = (unsigned int *) shm_malloc(sizeof(unsigned int)*size);
  69. if(sr_lua_script_ver->version==NULL)
  70. {
  71. LM_ERR("cannot allocate shm memory\n");
  72. goto error;
  73. }
  74. memset(sr_lua_script_ver->version, 0, sizeof(unsigned int)*size);
  75. sr_lua_script_ver->len = size;
  76. if((sr_lua_locks=lock_set_alloc(size))==0)
  77. {
  78. LM_CRIT("failed to alloc lock set\n");
  79. goto error;
  80. }
  81. if(lock_set_init(sr_lua_locks)==0 )
  82. {
  83. LM_CRIT("failed to init lock set\n");
  84. goto error;
  85. }
  86. return 0;
  87. error:
  88. if(sr_lua_script_ver!=NULL)
  89. {
  90. if(sr_lua_script_ver->version!=NULL)
  91. {
  92. shm_free(sr_lua_script_ver->version);
  93. sr_lua_script_ver->version = NULL;
  94. }
  95. shm_free(sr_lua_script_ver);
  96. sr_lua_script_ver = NULL;
  97. }
  98. if(sr_lua_locks!=NULL)
  99. {
  100. lock_set_destroy( sr_lua_locks );
  101. lock_set_dealloc( sr_lua_locks );
  102. sr_lua_locks = NULL;
  103. }
  104. return -1;
  105. }
  106. /**
  107. *
  108. */
  109. int sr_lua_load_script(char *script)
  110. {
  111. sr_lua_load_t *li;
  112. li = (sr_lua_load_t*)pkg_malloc(sizeof(sr_lua_load_t));
  113. if(li==NULL)
  114. {
  115. LM_ERR("no more pkg\n");
  116. return -1;
  117. }
  118. memset(li, 0, sizeof(sr_lua_load_t));
  119. li->script = script;
  120. li->version = 0;
  121. li->next = _sr_lua_load_list;
  122. _sr_lua_load_list = li;
  123. _sr_L_env.nload += 1;
  124. LM_DBG("loaded script:[%s].\n", script);
  125. LM_DBG("Now there are %d scripts loaded\n", _sr_L_env.nload);
  126. return 0;
  127. }
  128. /**
  129. *
  130. */
  131. int sr_lua_register_module(char *mname)
  132. {
  133. if(lua_sr_exp_register_mod(mname)==0)
  134. return 0;
  135. return -1;
  136. }
  137. /**
  138. *
  139. */
  140. void lua_sr_openlibs(lua_State *L)
  141. {
  142. lua_sr_core_openlibs(L);
  143. lua_sr_exp_openlibs(L);
  144. }
  145. /**
  146. *
  147. */
  148. int lua_sr_init_mod(void)
  149. {
  150. /* allocate shm */
  151. if(lua_sr_alloc_script_ver()<0)
  152. {
  153. LM_CRIT("failed to alloc shm for version\n");
  154. return -1;
  155. }
  156. memset(&_sr_L_env, 0, sizeof(sr_lua_env_t));
  157. if(lua_sr_exp_init_mod()<0)
  158. return -1;
  159. return 0;
  160. }
  161. /**
  162. *
  163. */
  164. int lua_sr_init_probe(void)
  165. {
  166. lua_State *L;
  167. char *txt;
  168. sr_lua_load_t *li;
  169. struct stat sbuf;
  170. L = lua_open();
  171. if(L==NULL)
  172. {
  173. LM_ERR("cannot open lua\n");
  174. return -1;
  175. }
  176. luaL_openlibs(L);
  177. lua_sr_openlibs(L);
  178. /* force loading lua lib now */
  179. if(luaL_dostring(L, "sr.probe()")!=0)
  180. {
  181. txt = (char*)lua_tostring(L, -1);
  182. LM_ERR("error initializing Lua: %s\n", (txt)?txt:"unknown");
  183. lua_pop(L, 1);
  184. lua_close(L);
  185. return -1;
  186. }
  187. /* test if files to be loaded exist */
  188. if(_sr_lua_load_list != NULL)
  189. {
  190. li = _sr_lua_load_list;
  191. while(li)
  192. {
  193. if(stat(li->script, &sbuf)!=0)
  194. {
  195. /* file does not exist */
  196. LM_ERR("cannot find script: %s (wrong path?)\n",
  197. li->script);
  198. lua_close(L);
  199. return -1;
  200. }
  201. li = li->next;
  202. }
  203. }
  204. lua_close(L);
  205. LM_DBG("Lua probe was ok!\n");
  206. return 0;
  207. }
  208. /**
  209. *
  210. */
  211. int lua_sr_init_child(void)
  212. {
  213. sr_lua_load_t *li;
  214. int ret;
  215. char *txt;
  216. memset(&_sr_L_env, 0, sizeof(sr_lua_env_t));
  217. _sr_L_env.L = lua_open();
  218. if(_sr_L_env.L==NULL)
  219. {
  220. LM_ERR("cannot open lua\n");
  221. return -1;
  222. }
  223. luaL_openlibs(_sr_L_env.L);
  224. lua_sr_openlibs(_sr_L_env.L);
  225. /* set SR lib version */
  226. lua_pushstring(_sr_L_env.L, "SRVERSION");
  227. lua_pushstring(_sr_L_env.L, SRVERSION);
  228. lua_settable(_sr_L_env.L, LUA_GLOBALSINDEX);
  229. if(_sr_lua_load_list != NULL)
  230. {
  231. _sr_L_env.LL = luaL_newstate();
  232. if(_sr_L_env.LL==NULL)
  233. {
  234. LM_ERR("cannot open lua loading state\n");
  235. return -1;
  236. }
  237. luaL_openlibs(_sr_L_env.LL);
  238. lua_sr_openlibs(_sr_L_env.LL);
  239. /* set SR lib version */
  240. lua_pushstring(_sr_L_env.LL, "SRVERSION");
  241. lua_pushstring(_sr_L_env.LL, SRVERSION);
  242. lua_settable(_sr_L_env.LL, LUA_GLOBALSINDEX);
  243. /* force loading lua lib now */
  244. if(luaL_dostring(_sr_L_env.LL, "sr.probe()")!=0)
  245. {
  246. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  247. LM_ERR("error initializing Lua: %s\n", (txt)?txt:"unknown");
  248. lua_pop(_sr_L_env.LL, 1);
  249. lua_sr_destroy();
  250. return -1;
  251. }
  252. li = _sr_lua_load_list;
  253. while(li)
  254. {
  255. ret = luaL_dofile(_sr_L_env.LL, (const char*)li->script);
  256. if(ret!=0)
  257. {
  258. LM_ERR("failed to load Lua script: %s (err: %d)\n",
  259. li->script, ret);
  260. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  261. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  262. lua_pop(_sr_L_env.LL, 1);
  263. lua_sr_destroy();
  264. return -1;
  265. }
  266. li = li->next;
  267. }
  268. }
  269. LM_DBG("Lua initialized!\n");
  270. return 0;
  271. }
  272. /**
  273. *
  274. */
  275. void lua_sr_destroy(void)
  276. {
  277. if(_sr_L_env.L!=NULL)
  278. {
  279. lua_close(_sr_L_env.L);
  280. _sr_L_env.L = NULL;
  281. }
  282. if(_sr_L_env.LL!=NULL)
  283. {
  284. lua_close(_sr_L_env.LL);
  285. _sr_L_env.LL = NULL;
  286. }
  287. memset(&_sr_L_env, 0, sizeof(sr_lua_env_t));
  288. if(sr_lua_script_ver!=NULL)
  289. {
  290. shm_free(sr_lua_script_ver->version);
  291. shm_free(sr_lua_script_ver);
  292. }
  293. if (sr_lua_locks!=NULL)
  294. {
  295. lock_set_destroy( sr_lua_locks );
  296. lock_set_dealloc( sr_lua_locks );
  297. sr_lua_locks = 0;
  298. }
  299. }
  300. /**
  301. *
  302. */
  303. int lua_sr_list_script(sr_lua_load_t **list)
  304. {
  305. *list = _sr_lua_load_list;
  306. return 0;
  307. }
  308. /**
  309. * Mark script in pos to be reloaded
  310. * pos -1: reload all scritps
  311. */
  312. int lua_sr_reload_script(int pos)
  313. {
  314. int i, len = sr_lua_script_ver->len;
  315. if(_sr_lua_load_list!= NULL)
  316. {
  317. if (!sr_lua_script_ver)
  318. {
  319. LM_CRIT("shm for version not allocated\n");
  320. return -1;
  321. }
  322. if (pos<0)
  323. {
  324. // let's mark all the scripts to be reloaded
  325. for (i=0;i<len;i++)
  326. {
  327. lock_set_get(sr_lua_locks, i);
  328. sr_lua_script_ver->version[i] += 1;
  329. lock_set_release(sr_lua_locks, i);
  330. }
  331. }
  332. else
  333. {
  334. if (pos>=0 && pos<len)
  335. {
  336. lock_set_get(sr_lua_locks, pos);
  337. sr_lua_script_ver->version[pos] += 1;
  338. lock_set_release(sr_lua_locks, pos);
  339. LM_DBG("pos: %d set to reloaded\n", pos);
  340. }
  341. else
  342. {
  343. LM_ERR("pos out of range\n");
  344. return -2;
  345. }
  346. }
  347. return 0;
  348. }
  349. LM_ERR("No script loaded\n");
  350. return -1;
  351. }
  352. /**
  353. * Checks if loaded version matches the shared
  354. * counter. If not equal reloads the script.
  355. */
  356. int sr_lua_reload_script(void)
  357. {
  358. sr_lua_load_t *li = _sr_lua_load_list;
  359. int ret, i;
  360. char *txt;
  361. int sv_len = sr_lua_script_ver->len;
  362. int sv[sv_len];
  363. for(i=0;i<sv_len;i++)
  364. {
  365. lock_set_get(sr_lua_locks, i);
  366. sv[i] = sr_lua_script_ver->version[i];
  367. lock_set_release(sr_lua_locks, i);
  368. }
  369. if(li==NULL)
  370. {
  371. LM_DBG("No script loaded\n");
  372. return 0;
  373. }
  374. for (i=0;i<sv_len;i++)
  375. {
  376. if(li->version!=sv[i])
  377. {
  378. LM_DBG("loaded version:%d needed: %d Let's reload <%s>\n",
  379. li->version, sv[i], li->script);
  380. ret = luaL_dofile(_sr_L_env.LL, (const char*)li->script);
  381. if(ret!=0)
  382. {
  383. LM_ERR("failed to load Lua script: %s (err: %d)\n",
  384. li->script, ret);
  385. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  386. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  387. lua_pop(_sr_L_env.LL, 1);
  388. lua_sr_destroy();
  389. return -1;
  390. }
  391. li->version = sv[i];
  392. LM_DBG("<%s> set to version %d\n", li->script, li->version);
  393. }
  394. else LM_DBG("No need to reload [%s] is version %d\n",
  395. li->script, li->version);
  396. li = li->next;
  397. }
  398. return 1;
  399. }
  400. /**
  401. *
  402. */
  403. int lua_sr_initialized(void)
  404. {
  405. if(_sr_L_env.L==NULL)
  406. return 0;
  407. return 1;
  408. }
  409. /**
  410. *
  411. */
  412. int app_lua_return_int(lua_State *L, int v)
  413. {
  414. lua_pushinteger(L, v);
  415. return 1;
  416. }
  417. /**
  418. *
  419. */
  420. int app_lua_return_error(lua_State *L)
  421. {
  422. lua_pushinteger(L, -1);
  423. return 1;
  424. }
  425. /**
  426. *
  427. */
  428. int app_lua_return_boolean(lua_State *L, int b)
  429. {
  430. if(b==SRLUA_FALSE)
  431. lua_pushboolean(L, SRLUA_FALSE);
  432. else
  433. lua_pushboolean(L, SRLUA_TRUE);
  434. return 1;
  435. }
  436. /**
  437. *
  438. */
  439. int app_lua_return_false(lua_State *L)
  440. {
  441. lua_pushboolean(L, SRLUA_FALSE);
  442. return 1;
  443. }
  444. /**
  445. *
  446. */
  447. int app_lua_return_true(lua_State *L)
  448. {
  449. lua_pushboolean(L, SRLUA_TRUE);
  450. return 1;
  451. }
  452. /**
  453. *
  454. */
  455. int app_lua_dostring(struct sip_msg *msg, char *script)
  456. {
  457. int ret;
  458. char *txt;
  459. LM_DBG("executing Lua string: [[%s]]\n", script);
  460. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.L));
  461. _sr_L_env.msg = msg;
  462. ret = luaL_dostring(_sr_L_env.L, script);
  463. if(ret!=0)
  464. {
  465. txt = (char*)lua_tostring(_sr_L_env.L, -1);
  466. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  467. lua_pop (_sr_L_env.L, 1);
  468. }
  469. _sr_L_env.msg = 0;
  470. return (ret==0)?1:-1;
  471. }
  472. /**
  473. *
  474. */
  475. int app_lua_dofile(struct sip_msg *msg, char *script)
  476. {
  477. int ret;
  478. char *txt;
  479. LM_DBG("executing Lua file: [[%s]]\n", script);
  480. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.L));
  481. _sr_L_env.msg = msg;
  482. ret = luaL_dofile(_sr_L_env.L, script);
  483. if(ret!=0)
  484. {
  485. txt = (char*)lua_tostring(_sr_L_env.L, -1);
  486. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  487. lua_pop(_sr_L_env.L, 1);
  488. }
  489. _sr_L_env.msg = 0;
  490. return (ret==0)?1:-1;
  491. }
  492. /**
  493. *
  494. */
  495. int app_lua_runstring(struct sip_msg *msg, char *script)
  496. {
  497. int ret;
  498. char *txt;
  499. if(_sr_L_env.LL==NULL)
  500. {
  501. LM_ERR("lua loading state not initialized (call: %s)\n", script);
  502. return -1;
  503. }
  504. LM_DBG("running Lua string: [[%s]]\n", script);
  505. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.LL));
  506. _sr_L_env.msg = msg;
  507. ret = luaL_dostring(_sr_L_env.LL, script);
  508. if(ret!=0)
  509. {
  510. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  511. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  512. lua_pop (_sr_L_env.LL, 1);
  513. }
  514. _sr_L_env.msg = 0;
  515. return (ret==0)?1:-1;
  516. }
  517. /**
  518. *
  519. */
  520. int app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
  521. char *p3)
  522. {
  523. int n;
  524. int ret;
  525. char *txt;
  526. if(_sr_L_env.LL==NULL)
  527. {
  528. LM_ERR("lua loading state not initialized (call: %s)\n", func);
  529. return -1;
  530. }
  531. /* check the script version loaded */
  532. if(!sr_lua_reload_script())
  533. {
  534. LM_ERR("lua reload failed\n");
  535. return -1;
  536. }
  537. LM_DBG("executing Lua function: [[%s]]\n", func);
  538. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.LL));
  539. lua_getglobal(_sr_L_env.LL, func);
  540. if(!lua_isfunction(_sr_L_env.LL, -1))
  541. {
  542. LM_ERR("no such function [%s] in lua scripts\n", func);
  543. LM_ERR("top stack type [%d - %s]\n",
  544. lua_type(_sr_L_env.LL, -1),
  545. lua_typename(_sr_L_env.LL,lua_type(_sr_L_env.LL, -1)));
  546. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  547. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  548. return -1;
  549. }
  550. n = 0;
  551. if(p1!=NULL)
  552. {
  553. lua_pushstring(_sr_L_env.LL, p1);
  554. n++;
  555. if(p2!=NULL)
  556. {
  557. lua_pushstring(_sr_L_env.LL, p2);
  558. n++;
  559. if(p3!=NULL)
  560. {
  561. lua_pushstring(_sr_L_env.LL, p3);
  562. n++;
  563. }
  564. }
  565. }
  566. _sr_L_env.msg = msg;
  567. ret = lua_pcall(_sr_L_env.LL, n, 0, 0);
  568. _sr_L_env.msg = 0;
  569. if(ret!=0)
  570. {
  571. LM_ERR("error executing: %s (err: %d)\n", func, ret);
  572. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  573. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  574. lua_pop(_sr_L_env.LL, 1);
  575. return -1;
  576. }
  577. return 1;
  578. }
  579. void app_lua_dump_stack(lua_State *L)
  580. {
  581. int i;
  582. int t;
  583. int top;
  584. top = lua_gettop(L);
  585. LM_DBG("lua stack top index: %d\n", top);
  586. for (i = 1; i <= top; i++)
  587. {
  588. t = lua_type(L, i);
  589. switch (t)
  590. {
  591. case LUA_TSTRING: /* strings */
  592. LM_DBG("[%i:s> %s\n", i, lua_tostring(L, i));
  593. break;
  594. case LUA_TBOOLEAN: /* booleans */
  595. LM_DBG("[%i:b> %s\n", i,
  596. lua_toboolean(L, i) ? "true" : "false");
  597. break;
  598. case LUA_TNUMBER: /* numbers */
  599. LM_DBG("[%i:n> %g\n", i, lua_tonumber(L, i));
  600. break;
  601. default: /* other values */
  602. LM_DBG("[%i:t> %s\n", i, lua_typename(L, t));
  603. break;
  604. }
  605. }
  606. }