app_lua_api.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 = luaL_newstate();
  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 = luaL_newstate();
  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. #if LUA_VERSION_NUM >= 502
  248. lua_pushstring(_sr_L_env.L, SRVERSION);
  249. lua_setglobal(_sr_L_env.L, "SRVERSION");
  250. #else
  251. lua_pushstring(_sr_L_env.L, "SRVERSION");
  252. lua_pushstring(_sr_L_env.L, SRVERSION);
  253. lua_settable(_sr_L_env.L, LUA_GLOBALSINDEX);
  254. #endif
  255. if(_sr_lua_load_list != NULL)
  256. {
  257. _sr_L_env.LL = luaL_newstate();
  258. if(_sr_L_env.LL==NULL)
  259. {
  260. LM_ERR("cannot open lua loading state\n");
  261. return -1;
  262. }
  263. luaL_openlibs(_sr_L_env.LL);
  264. lua_sr_openlibs(_sr_L_env.LL);
  265. /* set SR lib version */
  266. #if LUA_VERSION_NUM >= 502
  267. lua_pushstring(_sr_L_env.L, SRVERSION);
  268. lua_setglobal(_sr_L_env.L, "SRVERSION");
  269. #else
  270. lua_pushstring(_sr_L_env.LL, "SRVERSION");
  271. lua_pushstring(_sr_L_env.LL, SRVERSION);
  272. lua_settable(_sr_L_env.LL, LUA_GLOBALSINDEX);
  273. #endif
  274. /* force loading lua lib now */
  275. if(luaL_dostring(_sr_L_env.LL, "sr.probe()")!=0)
  276. {
  277. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  278. LM_ERR("error initializing Lua: %s\n", (txt)?txt:"unknown");
  279. lua_pop(_sr_L_env.LL, 1);
  280. lua_sr_destroy();
  281. return -1;
  282. }
  283. li = _sr_lua_load_list;
  284. while(li)
  285. {
  286. ret = luaL_dofile(_sr_L_env.LL, (const char*)li->script);
  287. if(ret!=0)
  288. {
  289. LM_ERR("failed to load Lua script: %s (err: %d)\n",
  290. li->script, ret);
  291. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  292. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  293. lua_pop(_sr_L_env.LL, 1);
  294. lua_sr_destroy();
  295. return -1;
  296. }
  297. li = li->next;
  298. }
  299. }
  300. LM_DBG("Lua initialized!\n");
  301. return 0;
  302. }
  303. /**
  304. *
  305. */
  306. void lua_sr_destroy(void)
  307. {
  308. if(_sr_L_env.L!=NULL)
  309. {
  310. lua_close(_sr_L_env.L);
  311. _sr_L_env.L = NULL;
  312. }
  313. if(_sr_L_env.LL!=NULL)
  314. {
  315. lua_close(_sr_L_env.LL);
  316. _sr_L_env.LL = NULL;
  317. }
  318. memset(&_sr_L_env, 0, sizeof(sr_lua_env_t));
  319. if(sr_lua_script_ver!=NULL)
  320. {
  321. shm_free(sr_lua_script_ver->version);
  322. shm_free(sr_lua_script_ver);
  323. }
  324. if (sr_lua_locks!=NULL)
  325. {
  326. lock_set_destroy( sr_lua_locks );
  327. lock_set_dealloc( sr_lua_locks );
  328. sr_lua_locks = 0;
  329. }
  330. if(_app_lua_sv!=NULL) {
  331. pkg_free(_app_lua_sv);
  332. _app_lua_sv = 0;
  333. }
  334. }
  335. /**
  336. *
  337. */
  338. int lua_sr_list_script(sr_lua_load_t **list)
  339. {
  340. *list = _sr_lua_load_list;
  341. return 0;
  342. }
  343. /**
  344. * Mark script in pos to be reloaded
  345. * pos -1: reload all scritps
  346. */
  347. int lua_sr_reload_script(int pos)
  348. {
  349. int i, len = sr_lua_script_ver->len;
  350. if(_sr_lua_load_list!= NULL)
  351. {
  352. if (!sr_lua_script_ver)
  353. {
  354. LM_CRIT("shm for version not allocated\n");
  355. return -1;
  356. }
  357. if (_app_lua_sr_reload==0)
  358. {
  359. LM_ERR("reload is not activated\n");
  360. return -3;
  361. }
  362. if (pos<0)
  363. {
  364. // let's mark all the scripts to be reloaded
  365. for (i=0;i<len;i++)
  366. {
  367. lock_set_get(sr_lua_locks, i);
  368. sr_lua_script_ver->version[i] += 1;
  369. lock_set_release(sr_lua_locks, i);
  370. }
  371. }
  372. else
  373. {
  374. if (pos>=0 && pos<len)
  375. {
  376. lock_set_get(sr_lua_locks, pos);
  377. sr_lua_script_ver->version[pos] += 1;
  378. lock_set_release(sr_lua_locks, pos);
  379. LM_DBG("pos: %d set to reloaded\n", pos);
  380. }
  381. else
  382. {
  383. LM_ERR("pos out of range\n");
  384. return -2;
  385. }
  386. }
  387. return 0;
  388. }
  389. LM_ERR("No script loaded\n");
  390. return -1;
  391. }
  392. /**
  393. * Checks if loaded version matches the shared
  394. * counter. If not equal reloads the script.
  395. */
  396. int sr_lua_reload_script(void)
  397. {
  398. sr_lua_load_t *li = _sr_lua_load_list;
  399. int ret, i;
  400. char *txt;
  401. int sv_len = sr_lua_script_ver->len;
  402. if(li==NULL)
  403. {
  404. LM_DBG("No script loaded\n");
  405. return 0;
  406. }
  407. if(_app_lua_sv==NULL) {
  408. _app_lua_sv = (int *) pkg_malloc(sizeof(int)*sv_len);
  409. if(_app_lua_sv==NULL)
  410. {
  411. LM_ERR("no more pkg memory\n");
  412. return -1;
  413. }
  414. }
  415. for(i=0;i<sv_len;i++)
  416. {
  417. lock_set_get(sr_lua_locks, i);
  418. _app_lua_sv[i] = sr_lua_script_ver->version[i];
  419. lock_set_release(sr_lua_locks, i);
  420. if(li->version!=_app_lua_sv[i])
  421. {
  422. LM_DBG("loaded version:%d needed: %d Let's reload <%s>\n",
  423. li->version, _app_lua_sv[i], li->script);
  424. ret = luaL_dofile(_sr_L_env.LL, (const char*)li->script);
  425. if(ret!=0)
  426. {
  427. LM_ERR("failed to load Lua script: %s (err: %d)\n",
  428. li->script, ret);
  429. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  430. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  431. lua_pop(_sr_L_env.LL, 1);
  432. return -1;
  433. }
  434. li->version = _app_lua_sv[i];
  435. LM_DBG("<%s> set to version %d\n", li->script, li->version);
  436. }
  437. else LM_DBG("No need to reload [%s] is version %d\n",
  438. li->script, li->version);
  439. li = li->next;
  440. }
  441. return 1;
  442. }
  443. /**
  444. *
  445. */
  446. int lua_sr_initialized(void)
  447. {
  448. if(_sr_L_env.L==NULL)
  449. return 0;
  450. return 1;
  451. }
  452. /**
  453. *
  454. */
  455. int app_lua_return_int(lua_State *L, int v)
  456. {
  457. lua_pushinteger(L, v);
  458. return 1;
  459. }
  460. /**
  461. *
  462. */
  463. int app_lua_return_error(lua_State *L)
  464. {
  465. lua_pushinteger(L, -1);
  466. return 1;
  467. }
  468. /**
  469. *
  470. */
  471. int app_lua_return_boolean(lua_State *L, int b)
  472. {
  473. if(b==SRLUA_FALSE)
  474. lua_pushboolean(L, SRLUA_FALSE);
  475. else
  476. lua_pushboolean(L, SRLUA_TRUE);
  477. return 1;
  478. }
  479. /**
  480. *
  481. */
  482. int app_lua_return_false(lua_State *L)
  483. {
  484. lua_pushboolean(L, SRLUA_FALSE);
  485. return 1;
  486. }
  487. /**
  488. *
  489. */
  490. int app_lua_return_true(lua_State *L)
  491. {
  492. lua_pushboolean(L, SRLUA_TRUE);
  493. return 1;
  494. }
  495. /**
  496. *
  497. */
  498. int app_lua_dostring(struct sip_msg *msg, char *script)
  499. {
  500. int ret;
  501. char *txt;
  502. LM_DBG("executing Lua string: [[%s]]\n", script);
  503. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.L));
  504. _sr_L_env.msg = msg;
  505. ret = luaL_dostring(_sr_L_env.L, script);
  506. if(ret!=0)
  507. {
  508. txt = (char*)lua_tostring(_sr_L_env.L, -1);
  509. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  510. lua_pop (_sr_L_env.L, 1);
  511. }
  512. _sr_L_env.msg = 0;
  513. return (ret==0)?1:-1;
  514. }
  515. /**
  516. *
  517. */
  518. int app_lua_dofile(struct sip_msg *msg, char *script)
  519. {
  520. int ret;
  521. char *txt;
  522. LM_DBG("executing Lua file: [[%s]]\n", script);
  523. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.L));
  524. _sr_L_env.msg = msg;
  525. ret = luaL_dofile(_sr_L_env.L, script);
  526. if(ret!=0)
  527. {
  528. txt = (char*)lua_tostring(_sr_L_env.L, -1);
  529. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  530. lua_pop(_sr_L_env.L, 1);
  531. }
  532. _sr_L_env.msg = 0;
  533. return (ret==0)?1:-1;
  534. }
  535. /**
  536. *
  537. */
  538. int app_lua_runstring(struct sip_msg *msg, char *script)
  539. {
  540. int ret;
  541. char *txt;
  542. if(_sr_L_env.LL==NULL)
  543. {
  544. LM_ERR("lua loading state not initialized (call: %s)\n", script);
  545. return -1;
  546. }
  547. LM_DBG("running Lua string: [[%s]]\n", script);
  548. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.LL));
  549. _sr_L_env.msg = msg;
  550. ret = luaL_dostring(_sr_L_env.LL, script);
  551. if(ret!=0)
  552. {
  553. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  554. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  555. lua_pop (_sr_L_env.LL, 1);
  556. }
  557. _sr_L_env.msg = 0;
  558. return (ret==0)?1:-1;
  559. }
  560. /**
  561. *
  562. */
  563. int app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
  564. char *p3)
  565. {
  566. int n;
  567. int ret;
  568. char *txt;
  569. if(_sr_L_env.LL==NULL)
  570. {
  571. LM_ERR("lua loading state not initialized (call: %s)\n", func);
  572. return -1;
  573. }
  574. if(_app_lua_sr_reload!=0)
  575. {
  576. /* check the script version loaded */
  577. if(!sr_lua_reload_script())
  578. {
  579. LM_ERR("lua reload failed\n");
  580. return -1;
  581. }
  582. }
  583. else LM_DBG("reload deactivated\n");
  584. LM_DBG("executing Lua function: [[%s]]\n", func);
  585. LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.LL));
  586. lua_getglobal(_sr_L_env.LL, func);
  587. if(!lua_isfunction(_sr_L_env.LL, -1))
  588. {
  589. LM_ERR("no such function [%s] in lua scripts\n", func);
  590. LM_ERR("top stack type [%d - %s]\n",
  591. lua_type(_sr_L_env.LL, -1),
  592. lua_typename(_sr_L_env.LL,lua_type(_sr_L_env.LL, -1)));
  593. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  594. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  595. return -1;
  596. }
  597. n = 0;
  598. if(p1!=NULL)
  599. {
  600. lua_pushstring(_sr_L_env.LL, p1);
  601. n++;
  602. if(p2!=NULL)
  603. {
  604. lua_pushstring(_sr_L_env.LL, p2);
  605. n++;
  606. if(p3!=NULL)
  607. {
  608. lua_pushstring(_sr_L_env.LL, p3);
  609. n++;
  610. }
  611. }
  612. }
  613. _sr_L_env.msg = msg;
  614. ret = lua_pcall(_sr_L_env.LL, n, 0, 0);
  615. _sr_L_env.msg = 0;
  616. if(ret!=0)
  617. {
  618. LM_ERR("error executing: %s (err: %d)\n", func, ret);
  619. txt = (char*)lua_tostring(_sr_L_env.LL, -1);
  620. LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
  621. lua_pop(_sr_L_env.LL, 1);
  622. return -1;
  623. }
  624. return 1;
  625. }
  626. void app_lua_dump_stack(lua_State *L)
  627. {
  628. int i;
  629. int t;
  630. int top;
  631. top = lua_gettop(L);
  632. LM_DBG("lua stack top index: %d\n", top);
  633. for (i = 1; i <= top; i++)
  634. {
  635. t = lua_type(L, i);
  636. switch (t)
  637. {
  638. case LUA_TSTRING: /* strings */
  639. LM_DBG("[%i:s> %s\n", i, lua_tostring(L, i));
  640. break;
  641. case LUA_TBOOLEAN: /* booleans */
  642. LM_DBG("[%i:b> %s\n", i,
  643. lua_toboolean(L, i) ? "true" : "false");
  644. break;
  645. case LUA_TNUMBER: /* numbers */
  646. LM_DBG("[%i:n> %g\n", i, lua_tonumber(L, i));
  647. break;
  648. default: /* other values */
  649. LM_DBG("[%i:t> %s\n", i, lua_typename(L, t));
  650. break;
  651. }
  652. }
  653. }