app_perl_mod.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * $Id$
  3. *
  4. * Perl module for Kamailio
  5. *
  6. * Copyright (C) 2006 Collax GmbH
  7. * (Bastian Friedrich <[email protected]>)
  8. *
  9. * This file is part of Kamailio, a free SIP server.
  10. *
  11. * Kamailio is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * Kamailio is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. */
  26. #define DEFAULTMODULE "Kamailio"
  27. #define MAX_LIB_PATHS 10
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <dlfcn.h>
  32. #include <sys/time.h>
  33. #include "../../sr_module.h"
  34. #include "../../mem/mem.h"
  35. #include "../../mem/shm_mem.h"
  36. #include "../../lib/kmi/mi.h"
  37. #include "../../modules/rr/api.h"
  38. #include "../../modules/sl/sl.h"
  39. #include "../../rpc.h"
  40. #include "../../rpc_lookup.h"
  41. /* lock_ops.h defines union semun, perl does not need to redefine it */
  42. #ifdef USE_SYSV_SEM
  43. # define HAS_UNION_SEMUN
  44. #endif
  45. #include "perlfunc.h"
  46. #include "app_perl_mod.h"
  47. /* #include "perlxsi.h" function is in here... */
  48. MODULE_VERSION
  49. /* Full path to the script including executed functions */
  50. char *filename = NULL;
  51. /* Path to an arbitrary directory where the Kamailio Perl modules are
  52. * installed */
  53. char *modpath = NULL;
  54. /* Allow unsafe module functions - functions with fixups. This will create
  55. * memory leaks, the variable thus is not documented! */
  56. int unsafemodfnc = 0;
  57. /* number of execution cycles after which perl interpreter is reset */
  58. int _ap_reset_cycles_init = 0;
  59. int _ap_exec_cycles = 0;
  60. int *_ap_reset_cycles = 0;
  61. /* Reference to the running Perl interpreter instance */
  62. PerlInterpreter *my_perl = NULL;
  63. /** SL API structure */
  64. sl_api_t slb;
  65. static int ap_init_rpc(void);
  66. /*
  67. * Module destroy function prototype
  68. */
  69. static void destroy(void);
  70. /* environment pointer needed to init perl interpreter */
  71. extern char **environ;
  72. /*
  73. * Module initialization function prototype
  74. */
  75. static int mod_init(void);
  76. /*
  77. * Reload perl interpreter - reload perl script. Forward declaration.
  78. */
  79. struct mi_root* perl_mi_reload(struct mi_root *cmd_tree, void *param);
  80. /*
  81. * Exported functions
  82. */
  83. static cmd_export_t cmds[] = {
  84. { "perl_exec_simple", (cmd_function)perl_exec_simple1, 1, NULL, 0,
  85. REQUEST_ROUTE | FAILURE_ROUTE
  86. | ONREPLY_ROUTE | BRANCH_ROUTE },
  87. { "perl_exec_simple", (cmd_function)perl_exec_simple2, 2, NULL, 0,
  88. REQUEST_ROUTE | FAILURE_ROUTE
  89. | ONREPLY_ROUTE | BRANCH_ROUTE },
  90. { "perl_exec", (cmd_function)perl_exec1, 1, NULL, 0,
  91. REQUEST_ROUTE | FAILURE_ROUTE
  92. | ONREPLY_ROUTE | BRANCH_ROUTE },
  93. { "perl_exec", (cmd_function)perl_exec2, 2, NULL, 0,
  94. REQUEST_ROUTE | FAILURE_ROUTE
  95. | ONREPLY_ROUTE | BRANCH_ROUTE },
  96. { 0, 0, 0, 0, 0, 0 }
  97. };
  98. /*
  99. * Exported parameters
  100. */
  101. static param_export_t params[] = {
  102. {"filename", STR_PARAM, &filename},
  103. {"modpath", STR_PARAM, &modpath},
  104. {"unsafemodfnc", INT_PARAM, &unsafemodfnc},
  105. {"reset_cycles", INT_PARAM, &_ap_reset_cycles_init},
  106. { 0, 0, 0 }
  107. };
  108. /*
  109. * Exported MI functions
  110. */
  111. static mi_export_t mi_cmds[] = {
  112. /* FIXME This does not yet work...
  113. { "perl_reload", perl_mi_reload, MI_NO_INPUT_FLAG, 0, 0 },*/
  114. { 0, 0, 0, 0, 0}
  115. };
  116. /*
  117. * Module info
  118. */
  119. #ifndef RTLD_NOW
  120. /* for openbsd */
  121. #define RTLD_NOW DL_LAZY
  122. #endif
  123. #ifndef RTLD_GLOBAL
  124. /* Unsupported! */
  125. #define RTLD_GLOBAL 0
  126. #endif
  127. /*
  128. * Module interface
  129. */
  130. struct module_exports exports = {
  131. "app_perl",
  132. RTLD_NOW | RTLD_GLOBAL,
  133. cmds, /* Exported functions */
  134. params, /* Exported parameters */
  135. 0, /* exported statistics */
  136. mi_cmds, /* exported MI functions */
  137. 0, /* exported pseudo-variables */
  138. 0, /* extra processes */
  139. mod_init, /* module initialization function */
  140. 0, /* response function */
  141. destroy, /* destroy function */
  142. 0 /* child initialization function */
  143. };
  144. EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
  145. EXTERN_C void boot_Kamailio(pTHX_ CV* cv);
  146. /*
  147. * This is output by perl -MExtUtils::Embed -e xsinit
  148. * and complemented by the Kamailio bootstrapping
  149. */
  150. EXTERN_C void xs_init(pTHX) {
  151. char *file = __FILE__;
  152. dXSUB_SYS;
  153. newXS("Kamailio::bootstrap", boot_Kamailio, file);
  154. newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
  155. }
  156. /*
  157. * Initialize the perl interpreter.
  158. * This might later be used to reinit the module.
  159. */
  160. PerlInterpreter *parser_init(void) {
  161. int argc = 0;
  162. char *argv[MAX_LIB_PATHS + 3];
  163. PerlInterpreter *new_perl = NULL;
  164. char *entry, *stop, *end;
  165. int modpathset_start = 0;
  166. int modpathset_end = 0;
  167. int i;
  168. int pr;
  169. new_perl = perl_alloc();
  170. if (!new_perl) {
  171. LM_ERR("could not allocate perl.\n");
  172. return NULL;
  173. }
  174. perl_construct(new_perl);
  175. argv[0] = ""; argc++; /* First param _needs_ to be empty */
  176. /* Possible Include path extension by modparam */
  177. if (modpath && (strlen(modpath) > 0)) {
  178. modpathset_start = argc;
  179. entry = modpath;
  180. stop = modpath + strlen(modpath);
  181. for (end = modpath; end <= stop; end++) {
  182. if ( (end[0] == ':') || (end[0] == '\0') ) {
  183. end[0] = '\0';
  184. if (argc > MAX_LIB_PATHS) {
  185. LM_ERR("too many lib paths, skipping lib path: '%s'\n", entry);
  186. } else {
  187. LM_INFO("setting lib path: '%s'\n", entry);
  188. argv[argc] = pkg_malloc(strlen(entry)+20);
  189. sprintf(argv[argc], "-I%s", entry);
  190. modpathset_end = argc;
  191. argc++;
  192. }
  193. entry = end + 1;
  194. }
  195. }
  196. }
  197. argv[argc] = "-M"DEFAULTMODULE; argc++; /* Always "use" Kamailio.pm */
  198. argv[argc] = filename; /* The script itself */
  199. argc++;
  200. pr=perl_parse(new_perl, xs_init, argc, argv, NULL);
  201. if (pr) {
  202. LM_ERR("failed to load perl file \"%s\" with code %d.\n", argv[argc-1], pr);
  203. if (modpathset_start) {
  204. for (i = modpathset_start; i <= modpathset_end; i++) {
  205. pkg_free(argv[i]);
  206. }
  207. }
  208. return NULL;
  209. } else {
  210. LM_INFO("successfully loaded perl file \"%s\"\n", argv[argc-1]);
  211. }
  212. if (modpathset_start) {
  213. for (i = modpathset_start; i <= modpathset_end; i++) {
  214. pkg_free(argv[i]);
  215. }
  216. }
  217. perl_run(new_perl);
  218. return new_perl;
  219. }
  220. /*
  221. *
  222. */
  223. int unload_perl(PerlInterpreter *p) {
  224. perl_destruct(p);
  225. perl_free(p);
  226. return 0;
  227. }
  228. /*
  229. * reload function.
  230. * Reinitializes the interpreter. Works, but execution for _all_
  231. * children is difficult.
  232. */
  233. int perl_reload(void)
  234. {
  235. PerlInterpreter *new_perl;
  236. new_perl = parser_init();
  237. if (new_perl) {
  238. unload_perl(my_perl);
  239. my_perl = new_perl;
  240. #ifdef PERL_EXIT_DESTRUCT_END
  241. PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
  242. #else
  243. #warning Perl 5.8.x should be used. Please upgrade.
  244. #warning This binary will be unsupported.
  245. PL_exit_flags |= PERL_EXIT_EXPECTED;
  246. #endif
  247. return 0;
  248. } else {
  249. return -1;
  250. }
  251. }
  252. /*
  253. * Reinit through fifo.
  254. * Currently does not seem to work :((
  255. */
  256. struct mi_root* perl_mi_reload(struct mi_root *cmd_tree, void *param)
  257. {
  258. if (perl_reload()<0) {
  259. return init_mi_tree( 500, "Perl reload failed", 18);
  260. } else {
  261. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  262. }
  263. }
  264. /*
  265. * mod_init
  266. * Called by kamailio at init time
  267. */
  268. static int mod_init(void) {
  269. int argc = 1;
  270. char *argt[] = { MOD_NAME, NULL };
  271. char **argv;
  272. struct timeval t1;
  273. struct timeval t2;
  274. if(register_mi_mod(exports.name, mi_cmds)!=0)
  275. {
  276. LM_ERR("failed to register MI commands\n");
  277. return -1;
  278. }
  279. if(ap_init_rpc()<0)
  280. {
  281. LM_ERR("failed to register RPC commands\n");
  282. return -1;
  283. }
  284. if (!filename) {
  285. LM_ERR("insufficient module parameters. Module not loaded.\n");
  286. return -1;
  287. }
  288. /* bind the SL API */
  289. if (sl_load_api(&slb)!=0) {
  290. LM_ERR("cannot bind to SL API\n");
  291. return -1;
  292. }
  293. _ap_reset_cycles = shm_malloc(sizeof(int));
  294. if(_ap_reset_cycles == NULL) {
  295. LM_ERR("no more shared memory\n");
  296. return -1;
  297. }
  298. *_ap_reset_cycles = _ap_reset_cycles_init;
  299. argv = argt;
  300. PERL_SYS_INIT3(&argc, &argv, &environ);
  301. gettimeofday(&t1, NULL);
  302. my_perl = parser_init();
  303. gettimeofday(&t2, NULL);
  304. if (my_perl==NULL)
  305. goto error;
  306. LM_INFO("perl interpreter has been initialized (%d.%06d => %d.%06d)\n",
  307. (int)t1.tv_sec, (int)t1.tv_usec,
  308. (int)t2.tv_sec, (int)t2.tv_usec);
  309. #ifdef PERL_EXIT_DESTRUCT_END
  310. PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
  311. #else
  312. PL_exit_flags |= PERL_EXIT_EXPECTED;
  313. #endif
  314. return 0;
  315. error:
  316. if(_ap_reset_cycles!=NULL)
  317. shm_free(_ap_reset_cycles);
  318. _ap_reset_cycles = NULL;
  319. return -1;
  320. }
  321. /*
  322. * destroy
  323. * called by kamailio at exit time
  324. */
  325. static void destroy(void)
  326. {
  327. if(_ap_reset_cycles!=NULL)
  328. shm_free(_ap_reset_cycles);
  329. _ap_reset_cycles = NULL;
  330. if(my_perl==NULL)
  331. return;
  332. unload_perl(my_perl);
  333. PERL_SYS_TERM();
  334. my_perl = NULL;
  335. }
  336. /**
  337. * count executions and rest interpreter
  338. *
  339. */
  340. int app_perl_reset_interpreter(void)
  341. {
  342. struct timeval t1;
  343. struct timeval t2;
  344. if(*_ap_reset_cycles==0)
  345. return 0;
  346. _ap_exec_cycles++;
  347. LM_DBG("perl interpreter exec cycle [%d/%d]\n",
  348. _ap_exec_cycles, *_ap_reset_cycles);
  349. if(_ap_exec_cycles<=*_ap_reset_cycles)
  350. return 0;
  351. gettimeofday(&t1, NULL);
  352. if (perl_reload()<0) {
  353. LM_ERR("perl interpreter cannot be reset [%d/%d]\n",
  354. _ap_exec_cycles, *_ap_reset_cycles);
  355. return -1;
  356. }
  357. gettimeofday(&t2, NULL);
  358. LM_INFO("perl interpreter has been reset [%d/%d] (%d.%06d => %d.%06d)\n",
  359. _ap_exec_cycles, *_ap_reset_cycles,
  360. (int)t1.tv_sec, (int)t1.tv_usec,
  361. (int)t2.tv_sec, (int)t2.tv_usec);
  362. _ap_exec_cycles = 0;
  363. return 0;
  364. }
  365. /*** RPC implementation ***/
  366. static const char* app_perl_rpc_set_reset_cycles_doc[3] = {
  367. "Set the value for reset_cycles",
  368. "Has one parmeter with int value",
  369. 0
  370. };
  371. /*
  372. * RPC command to set the value for reset_cycles
  373. */
  374. static void app_perl_rpc_set_reset_cycles(rpc_t* rpc, void* ctx)
  375. {
  376. int rsv;
  377. if(rpc->scan(ctx, "d", &rsv)<1)
  378. {
  379. rpc->fault(ctx, 500, "Invalid Parameters");
  380. return;
  381. }
  382. if(rsv<=0)
  383. rsv = 0;
  384. LM_DBG("new reset cycle value is %d\n", rsv);
  385. *_ap_reset_cycles = rsv;
  386. return;
  387. }
  388. static const char* app_perl_rpc_get_reset_cycles_doc[2] = {
  389. "Get the value for reset_cycles",
  390. 0
  391. };
  392. /*
  393. * RPC command to set the value for reset_cycles
  394. */
  395. static void app_perl_rpc_get_reset_cycles(rpc_t* rpc, void* ctx)
  396. {
  397. int rsv;
  398. void* th;
  399. rsv = *_ap_reset_cycles;
  400. /* add entry node */
  401. if (rpc->add(ctx, "{", &th) < 0)
  402. {
  403. rpc->fault(ctx, 500, "Internal error root reply");
  404. return;
  405. }
  406. if(rpc->struct_add(th, "d", "reset_cycles", rsv)<0)
  407. {
  408. rpc->fault(ctx, 500, "Internal error adding reset cycles");
  409. return;
  410. }
  411. LM_DBG("reset cycle value is %d\n", rsv);
  412. return;
  413. }
  414. rpc_export_t app_perl_rpc_cmds[] = {
  415. {"app_perl.set_reset_cycles", app_perl_rpc_set_reset_cycles,
  416. app_perl_rpc_set_reset_cycles_doc, 0},
  417. {"app_perl.get_reset_cycles", app_perl_rpc_get_reset_cycles,
  418. app_perl_rpc_get_reset_cycles_doc, 0},
  419. {0, 0, 0, 0}
  420. };
  421. /**
  422. * register RPC commands
  423. */
  424. static int ap_init_rpc(void)
  425. {
  426. if (rpc_register_array(app_perl_rpc_cmds)!=0)
  427. {
  428. LM_ERR("failed to register RPC commands\n");
  429. return -1;
  430. }
  431. return 0;
  432. }