app_lua_mod.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 "../../sr_module.h"
  27. #include "../../dprint.h"
  28. #include "../../ut.h"
  29. #include "../../mod_fix.h"
  30. #include "../../rpc.h"
  31. #include "../../rpc_lookup.h"
  32. #include "app_lua_api.h"
  33. MODULE_VERSION
  34. /** parameters */
  35. /* List of allowed chars for a prefix*/
  36. static int mod_init(void);
  37. static void mod_destroy(void);
  38. static int child_init(int rank);
  39. static int app_lua_init_rpc(void);
  40. static int w_app_lua_dostring(struct sip_msg *msg, char *script, char *extra);
  41. static int w_app_lua_dofile(struct sip_msg *msg, char *script, char *extra);
  42. static int w_app_lua_runstring(struct sip_msg *msg, char *script, char *extra);
  43. static int w_app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
  44. char *p3);
  45. static int w_app_lua_run0(struct sip_msg *msg, char *func, char *p1, char *p2,
  46. char *p3);
  47. static int w_app_lua_run1(struct sip_msg *msg, char *func, char *p1, char *p2,
  48. char *p3);
  49. static int w_app_lua_run2(struct sip_msg *msg, char *func, char *p1, char *p2,
  50. char *p3);
  51. static int w_app_lua_run3(struct sip_msg *msg, char *func, char *p1, char *p2,
  52. char *p3);
  53. static int fixup_lua_run(void** param, int param_no);
  54. int app_lua_load_param(modparam_t type, void *val);
  55. int app_lua_register_param(modparam_t type, void *val);
  56. int app_lua_reload_param(modparam_t type, void *val);
  57. static param_export_t params[]={
  58. {"load", PARAM_STRING|USE_FUNC_PARAM, (void*)app_lua_load_param},
  59. {"register", PARAM_STRING|USE_FUNC_PARAM, (void*)app_lua_register_param},
  60. {"reload", INT_PARAM|USE_FUNC_PARAM, (void*)app_lua_reload_param},
  61. {0, 0, 0}
  62. };
  63. static cmd_export_t cmds[]={
  64. {"lua_dostring", (cmd_function)w_app_lua_dostring, 1, fixup_spve_null,
  65. 0, ANY_ROUTE},
  66. {"lua_dofile", (cmd_function)w_app_lua_dofile, 1, fixup_spve_null,
  67. 0, ANY_ROUTE},
  68. {"lua_runstring", (cmd_function)w_app_lua_runstring, 1, fixup_spve_null,
  69. 0, ANY_ROUTE},
  70. {"lua_run", (cmd_function)w_app_lua_run0, 1, fixup_lua_run,
  71. 0, ANY_ROUTE},
  72. {"lua_run", (cmd_function)w_app_lua_run1, 2, fixup_lua_run,
  73. 0, ANY_ROUTE},
  74. {"lua_run", (cmd_function)w_app_lua_run2, 3, fixup_lua_run,
  75. 0, ANY_ROUTE},
  76. {"lua_run", (cmd_function)w_app_lua_run3, 4, fixup_lua_run,
  77. 0, ANY_ROUTE},
  78. {0, 0, 0, 0, 0, 0}
  79. };
  80. struct module_exports exports = {
  81. "app_lua",
  82. DEFAULT_DLFLAGS, /* dlopen flags */
  83. cmds,
  84. params,
  85. 0,
  86. 0, /* exported MI functions */
  87. 0, /* exported pseudo-variables */
  88. 0, /* extra processes */
  89. mod_init, /* module initialization function */
  90. 0, /* response function */
  91. mod_destroy, /* destroy function */
  92. child_init /* per child init function */
  93. };
  94. int mod_register(char *path, int *dlflags, void *p1, void *p2)
  95. {
  96. *dlflags = RTLD_NOW | RTLD_GLOBAL;
  97. return 0;
  98. }
  99. /**
  100. * init module function
  101. */
  102. static int mod_init(void)
  103. {
  104. if(lua_sr_init_mod()<0)
  105. return -1;
  106. if(app_lua_init_rpc()<0)
  107. {
  108. LM_ERR("failed to register RPC commands\n");
  109. return -1;
  110. }
  111. return 0;
  112. }
  113. /* each child get a new connection to the database */
  114. static int child_init(int rank)
  115. {
  116. if(rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  117. return 0; /* do nothing for the main process */
  118. if (rank==PROC_INIT)
  119. {
  120. /* do a probe before forking */
  121. if(lua_sr_init_probe()!=0)
  122. return -1;
  123. return 0;
  124. }
  125. return lua_sr_init_child();
  126. }
  127. static void mod_destroy(void)
  128. {
  129. lua_sr_destroy();
  130. }
  131. static char _lua_buf_stack[4][512];
  132. static int w_app_lua_dostring(struct sip_msg *msg, char *script, char *extra)
  133. {
  134. str s;
  135. if(!lua_sr_initialized())
  136. {
  137. LM_ERR("Lua env not intitialized");
  138. return -1;
  139. }
  140. if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
  141. {
  142. LM_ERR("cannot get the script\n");
  143. return -1;
  144. }
  145. if(s.len>=511)
  146. {
  147. LM_ERR("script too long %d\n", s.len);
  148. return -1;
  149. }
  150. memcpy(_lua_buf_stack[0], s.s, s.len);
  151. _lua_buf_stack[0][s.len] = '\0';
  152. return app_lua_dostring(msg, _lua_buf_stack[0]);
  153. }
  154. static int w_app_lua_dofile(struct sip_msg *msg, char *script, char *extra)
  155. {
  156. str s;
  157. if(!lua_sr_initialized())
  158. {
  159. LM_ERR("Lua env not intitialized");
  160. return -1;
  161. }
  162. if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
  163. {
  164. LM_ERR("cannot get the script\n");
  165. return -1;
  166. }
  167. if(s.len>=511)
  168. {
  169. LM_ERR("script too long %d\n", s.len);
  170. return -1;
  171. }
  172. memcpy(_lua_buf_stack[0], s.s, s.len);
  173. _lua_buf_stack[0][s.len] = '\0';
  174. return app_lua_dofile(msg, _lua_buf_stack[0]);
  175. }
  176. static int w_app_lua_runstring(struct sip_msg *msg, char *script, char *extra)
  177. {
  178. str s;
  179. if(!lua_sr_initialized())
  180. {
  181. LM_ERR("Lua env not intitialized");
  182. return -1;
  183. }
  184. if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
  185. {
  186. LM_ERR("cannot get the script\n");
  187. return -1;
  188. }
  189. if(s.len>=511)
  190. {
  191. LM_ERR("script too long %d\n", s.len);
  192. return -1;
  193. }
  194. memcpy(_lua_buf_stack[0], s.s, s.len);
  195. _lua_buf_stack[0][s.len] = '\0';
  196. return app_lua_runstring(msg, _lua_buf_stack[0]);
  197. }
  198. static int w_app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
  199. char *p3)
  200. {
  201. str s;
  202. if(!lua_sr_initialized())
  203. {
  204. LM_ERR("Lua env not intitialized");
  205. return -1;
  206. }
  207. if(fixup_get_svalue(msg, (gparam_p)func, &s)<0)
  208. {
  209. LM_ERR("cannot get the function\n");
  210. return -1;
  211. }
  212. if(s.len>=511)
  213. {
  214. LM_ERR("function too long %d\n", s.len);
  215. return -1;
  216. }
  217. memcpy(_lua_buf_stack[0], s.s, s.len);
  218. _lua_buf_stack[0][s.len] = '\0';
  219. if(p1!=NULL)
  220. {
  221. if(fixup_get_svalue(msg, (gparam_p)p1, &s)<0)
  222. {
  223. LM_ERR("cannot get p1\n");
  224. return -1;
  225. }
  226. if(s.len>=511)
  227. {
  228. LM_ERR("p1 too long %d\n", s.len);
  229. return -1;
  230. }
  231. memcpy(_lua_buf_stack[1], s.s, s.len);
  232. _lua_buf_stack[1][s.len] = '\0';
  233. if(p2!=NULL)
  234. {
  235. if(fixup_get_svalue(msg, (gparam_p)p2, &s)<0)
  236. {
  237. LM_ERR("cannot get p2\n");
  238. return -1;
  239. }
  240. if(s.len>=511)
  241. {
  242. LM_ERR("p2 too long %d\n", s.len);
  243. return -1;
  244. }
  245. memcpy(_lua_buf_stack[2], s.s, s.len);
  246. _lua_buf_stack[2][s.len] = '\0';
  247. if(p3!=NULL)
  248. {
  249. if(fixup_get_svalue(msg, (gparam_p)p3, &s)<0)
  250. {
  251. LM_ERR("cannot get p3\n");
  252. return -1;
  253. }
  254. if(s.len>=511)
  255. {
  256. LM_ERR("p3 too long %d\n", s.len);
  257. return -1;
  258. }
  259. memcpy(_lua_buf_stack[3], s.s, s.len);
  260. _lua_buf_stack[3][s.len] = '\0';
  261. }
  262. } else {
  263. p3 = NULL;
  264. }
  265. } else {
  266. p2 = NULL;
  267. p3 = NULL;
  268. }
  269. return app_lua_run(msg, _lua_buf_stack[0],
  270. (p1!=NULL)?_lua_buf_stack[1]:NULL,
  271. (p2!=NULL)?_lua_buf_stack[2]:NULL,
  272. (p3!=NULL)?_lua_buf_stack[3]:NULL);
  273. }
  274. static int w_app_lua_run0(struct sip_msg *msg, char *func, char *p1, char *p2,
  275. char *p3)
  276. {
  277. return w_app_lua_run(msg, func, NULL, NULL, NULL);
  278. }
  279. static int w_app_lua_run1(struct sip_msg *msg, char *func, char *p1, char *p2,
  280. char *p3)
  281. {
  282. return w_app_lua_run(msg, func, p1, NULL, NULL);
  283. }
  284. static int w_app_lua_run2(struct sip_msg *msg, char *func, char *p1, char *p2,
  285. char *p3)
  286. {
  287. return w_app_lua_run(msg, func, p1, p2, NULL);
  288. }
  289. static int w_app_lua_run3(struct sip_msg *msg, char *func, char *p1, char *p2,
  290. char *p3)
  291. {
  292. return w_app_lua_run(msg, func, p1, p2, p3);
  293. }
  294. int app_lua_load_param(modparam_t type, void *val)
  295. {
  296. if(val==NULL)
  297. return -1;
  298. return sr_lua_load_script((char*)val);
  299. }
  300. int app_lua_register_param(modparam_t type, void *val)
  301. {
  302. if(val==NULL)
  303. return -1;
  304. return sr_lua_register_module((char*)val);
  305. }
  306. int app_lua_reload_param(modparam_t type, void *val)
  307. {
  308. return sr_lua_reload_module((unsigned int)(long) (int *)val);
  309. }
  310. static int fixup_lua_run(void** param, int param_no)
  311. {
  312. return fixup_spve_null(param, 1);
  313. }
  314. /*** RPC implementation ***/
  315. static const char* app_lua_rpc_reload_doc[2] = {
  316. "Reload lua script",
  317. 0
  318. };
  319. static const char* app_lua_rpc_list_doc[2] = {
  320. "list lua scripts",
  321. 0
  322. };
  323. static void app_lua_rpc_reload(rpc_t* rpc, void* ctx)
  324. {
  325. int pos = -1;
  326. rpc->scan(ctx, "*d", &pos);
  327. LM_DBG("selected index: %d\n", pos);
  328. if(lua_sr_reload_script(pos)<0)
  329. rpc->fault(ctx, 500, "Reload Failed");
  330. return;
  331. }
  332. static void app_lua_rpc_list(rpc_t* rpc, void* ctx)
  333. {
  334. int i;
  335. sr_lua_load_t *list = NULL, *li;
  336. if(lua_sr_list_script(&list)<0)
  337. {
  338. LM_ERR("Can't get loaded scripts\n");
  339. return;
  340. }
  341. if(list)
  342. {
  343. li = list;
  344. i = 0;
  345. while(li)
  346. {
  347. rpc->rpl_printf(ctx, "%d: [%s]", i, li->script);
  348. li = li->next;
  349. i += 1;
  350. }
  351. }
  352. else {
  353. rpc->rpl_printf(ctx,"No scripts loaded");
  354. }
  355. return;
  356. }
  357. rpc_export_t app_lua_rpc_cmds[] = {
  358. {"app_lua.reload", app_lua_rpc_reload,
  359. app_lua_rpc_reload_doc, 0},
  360. {"app_lua.list", app_lua_rpc_list,
  361. app_lua_rpc_list_doc, 0},
  362. {0, 0, 0, 0}
  363. };
  364. /**
  365. * register RPC commands
  366. */
  367. static int app_lua_init_rpc(void)
  368. {
  369. if (rpc_register_array(app_lua_rpc_cmds)!=0)
  370. {
  371. LM_ERR("failed to register RPC commands\n");
  372. return -1;
  373. }
  374. return 0;
  375. }