app_lua_api.c 13 KB

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