app_lua_api.c 13 KB

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