app_lua_mod.c 8.9 KB

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