app_lua_mod.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 "../../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", STR_PARAM|USE_FUNC_PARAM, (void*)app_lua_load_param},
  59. {"register", STR_PARAM|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. RTLD_NOW | RTLD_GLOBAL, /* 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. /**
  95. * init module function
  96. */
  97. static int mod_init(void)
  98. {
  99. if(lua_sr_init_mod()<0)
  100. return -1;
  101. if(app_lua_init_rpc()<0)
  102. {
  103. LM_ERR("failed to register RPC commands\n");
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. /* each child get a new connection to the database */
  109. static int child_init(int rank)
  110. {
  111. if(rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  112. return 0; /* do nothing for the main process */
  113. if (rank==PROC_INIT)
  114. {
  115. /* do a probe before forking */
  116. if(lua_sr_init_probe()!=0)
  117. return -1;
  118. return 0;
  119. }
  120. return lua_sr_init_child();
  121. }
  122. static void mod_destroy(void)
  123. {
  124. lua_sr_destroy();
  125. }
  126. static char _lua_buf_stack[4][512];
  127. static int w_app_lua_dostring(struct sip_msg *msg, char *script, char *extra)
  128. {
  129. str s;
  130. if(!lua_sr_initialized())
  131. {
  132. LM_ERR("Lua env not intitialized");
  133. return -1;
  134. }
  135. if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
  136. {
  137. LM_ERR("cannot get the script\n");
  138. return -1;
  139. }
  140. if(s.len>=511)
  141. {
  142. LM_ERR("script too long %d\n", s.len);
  143. return -1;
  144. }
  145. memcpy(_lua_buf_stack[0], s.s, s.len);
  146. _lua_buf_stack[0][s.len] = '\0';
  147. return app_lua_dostring(msg, _lua_buf_stack[0]);
  148. }
  149. static int w_app_lua_dofile(struct sip_msg *msg, char *script, char *extra)
  150. {
  151. str s;
  152. if(!lua_sr_initialized())
  153. {
  154. LM_ERR("Lua env not intitialized");
  155. return -1;
  156. }
  157. if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
  158. {
  159. LM_ERR("cannot get the script\n");
  160. return -1;
  161. }
  162. if(s.len>=511)
  163. {
  164. LM_ERR("script too long %d\n", s.len);
  165. return -1;
  166. }
  167. memcpy(_lua_buf_stack[0], s.s, s.len);
  168. _lua_buf_stack[0][s.len] = '\0';
  169. return app_lua_dofile(msg, _lua_buf_stack[0]);
  170. }
  171. static int w_app_lua_runstring(struct sip_msg *msg, char *script, char *extra)
  172. {
  173. str s;
  174. if(!lua_sr_initialized())
  175. {
  176. LM_ERR("Lua env not intitialized");
  177. return -1;
  178. }
  179. if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
  180. {
  181. LM_ERR("cannot get the script\n");
  182. return -1;
  183. }
  184. if(s.len>=511)
  185. {
  186. LM_ERR("script too long %d\n", s.len);
  187. return -1;
  188. }
  189. memcpy(_lua_buf_stack[0], s.s, s.len);
  190. _lua_buf_stack[0][s.len] = '\0';
  191. return app_lua_runstring(msg, _lua_buf_stack[0]);
  192. }
  193. static int w_app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
  194. char *p3)
  195. {
  196. str s;
  197. if(!lua_sr_initialized())
  198. {
  199. LM_ERR("Lua env not intitialized");
  200. return -1;
  201. }
  202. if(fixup_get_svalue(msg, (gparam_p)func, &s)<0)
  203. {
  204. LM_ERR("cannot get the function\n");
  205. return -1;
  206. }
  207. if(s.len>=511)
  208. {
  209. LM_ERR("function too long %d\n", s.len);
  210. return -1;
  211. }
  212. memcpy(_lua_buf_stack[0], s.s, s.len);
  213. _lua_buf_stack[0][s.len] = '\0';
  214. if(p1!=NULL)
  215. {
  216. if(fixup_get_svalue(msg, (gparam_p)p1, &s)<0)
  217. {
  218. LM_ERR("cannot get p1\n");
  219. return -1;
  220. }
  221. if(s.len>=511)
  222. {
  223. LM_ERR("p1 too long %d\n", s.len);
  224. return -1;
  225. }
  226. memcpy(_lua_buf_stack[1], s.s, s.len);
  227. _lua_buf_stack[1][s.len] = '\0';
  228. if(p2!=NULL)
  229. {
  230. if(fixup_get_svalue(msg, (gparam_p)p2, &s)<0)
  231. {
  232. LM_ERR("cannot get p2\n");
  233. return -1;
  234. }
  235. if(s.len>=511)
  236. {
  237. LM_ERR("p2 too long %d\n", s.len);
  238. return -1;
  239. }
  240. memcpy(_lua_buf_stack[2], s.s, s.len);
  241. _lua_buf_stack[2][s.len] = '\0';
  242. if(p3!=NULL)
  243. {
  244. if(fixup_get_svalue(msg, (gparam_p)p3, &s)<0)
  245. {
  246. LM_ERR("cannot get p3\n");
  247. return -1;
  248. }
  249. if(s.len>=511)
  250. {
  251. LM_ERR("p3 too long %d\n", s.len);
  252. return -1;
  253. }
  254. memcpy(_lua_buf_stack[3], s.s, s.len);
  255. _lua_buf_stack[3][s.len] = '\0';
  256. }
  257. } else {
  258. p3 = NULL;
  259. }
  260. } else {
  261. p2 = NULL;
  262. p3 = NULL;
  263. }
  264. return app_lua_run(msg, _lua_buf_stack[0],
  265. (p1!=NULL)?_lua_buf_stack[1]:NULL,
  266. (p2!=NULL)?_lua_buf_stack[2]:NULL,
  267. (p3!=NULL)?_lua_buf_stack[3]:NULL);
  268. }
  269. static int w_app_lua_run0(struct sip_msg *msg, char *func, char *p1, char *p2,
  270. char *p3)
  271. {
  272. return w_app_lua_run(msg, func, NULL, NULL, NULL);
  273. }
  274. static int w_app_lua_run1(struct sip_msg *msg, char *func, char *p1, char *p2,
  275. char *p3)
  276. {
  277. return w_app_lua_run(msg, func, p1, NULL, NULL);
  278. }
  279. static int w_app_lua_run2(struct sip_msg *msg, char *func, char *p1, char *p2,
  280. char *p3)
  281. {
  282. return w_app_lua_run(msg, func, p1, p2, NULL);
  283. }
  284. static int w_app_lua_run3(struct sip_msg *msg, char *func, char *p1, char *p2,
  285. char *p3)
  286. {
  287. return w_app_lua_run(msg, func, p1, p2, p3);
  288. }
  289. int app_lua_load_param(modparam_t type, void *val)
  290. {
  291. if(val==NULL)
  292. return -1;
  293. return sr_lua_load_script((char*)val);
  294. }
  295. int app_lua_register_param(modparam_t type, void *val)
  296. {
  297. if(val==NULL)
  298. return -1;
  299. return sr_lua_register_module((char*)val);
  300. }
  301. int app_lua_reload_param(modparam_t type, void *val)
  302. {
  303. return sr_lua_reload_module((unsigned int)(long) (int *)val);
  304. }
  305. static int fixup_lua_run(void** param, int param_no)
  306. {
  307. return fixup_spve_null(param, 1);
  308. }
  309. /*** RPC implementation ***/
  310. static const char* app_lua_rpc_reload_doc[2] = {
  311. "Reload lua script",
  312. 0
  313. };
  314. static const char* app_lua_rpc_list_doc[2] = {
  315. "list lua scripts",
  316. 0
  317. };
  318. static void app_lua_rpc_reload(rpc_t* rpc, void* ctx)
  319. {
  320. int pos = -1;
  321. rpc->scan(ctx, "*d", &pos);
  322. LM_DBG("selected index: %d\n", pos);
  323. if(lua_sr_reload_script(pos)<0)
  324. rpc->fault(ctx, 500, "Reload Failed");
  325. return;
  326. }
  327. static void app_lua_rpc_list(rpc_t* rpc, void* ctx)
  328. {
  329. int i;
  330. sr_lua_load_t *list = NULL, *li;
  331. if(lua_sr_list_script(&list)<0)
  332. {
  333. LM_ERR("Can't get loaded scripts\n");
  334. return;
  335. }
  336. if(list)
  337. {
  338. li = list;
  339. i = 0;
  340. while(li)
  341. {
  342. rpc->printf(ctx, "%d: [%s]", i, li->script);
  343. li = li->next;
  344. i += 1;
  345. }
  346. }
  347. else {
  348. rpc->printf(ctx,"No scripts loaded");
  349. }
  350. return;
  351. }
  352. rpc_export_t app_lua_rpc_cmds[] = {
  353. {"app_lua.reload", app_lua_rpc_reload,
  354. app_lua_rpc_reload_doc, 0},
  355. {"app_lua.list", app_lua_rpc_list,
  356. app_lua_rpc_list_doc, 0},
  357. {0, 0, 0, 0}
  358. };
  359. /**
  360. * register RPC commands
  361. */
  362. static int app_lua_init_rpc(void)
  363. {
  364. if (rpc_register_array(app_lua_rpc_cmds)!=0)
  365. {
  366. LM_ERR("failed to register RPC commands\n");
  367. return -1;
  368. }
  369. return 0;
  370. }