app_perl_mod.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. /* Function to be called before perl interpreter instance is destroyed
  55. * when attempting reinit */
  56. static char *perl_destroy_func = NULL;
  57. /* Allow unsafe module functions - functions with fixups. This will create
  58. * memory leaks, the variable thus is not documented! */
  59. int unsafemodfnc = 0;
  60. /* number of execution cycles after which perl interpreter is reset */
  61. int _ap_reset_cycles_init = 0;
  62. int _ap_exec_cycles = 0;
  63. int *_ap_reset_cycles = 0;
  64. /* Reference to the running Perl interpreter instance */
  65. PerlInterpreter *my_perl = NULL;
  66. /** SL API structure */
  67. sl_api_t slb;
  68. static int ap_init_rpc(void);
  69. /*
  70. * Module destroy function prototype
  71. */
  72. static void destroy(void);
  73. /* environment pointer needed to init perl interpreter */
  74. extern char **environ;
  75. /*
  76. * Module initialization function prototype
  77. */
  78. static int mod_init(void);
  79. /*
  80. * Reload perl interpreter - reload perl script. Forward declaration.
  81. */
  82. struct mi_root* perl_mi_reload(struct mi_root *cmd_tree, void *param);
  83. /*
  84. * Exported functions
  85. */
  86. static cmd_export_t cmds[] = {
  87. { "perl_exec_simple", (cmd_function)perl_exec_simple1, 1, NULL, 0,
  88. REQUEST_ROUTE | FAILURE_ROUTE
  89. | ONREPLY_ROUTE | BRANCH_ROUTE },
  90. { "perl_exec_simple", (cmd_function)perl_exec_simple2, 2, NULL, 0,
  91. REQUEST_ROUTE | FAILURE_ROUTE
  92. | ONREPLY_ROUTE | BRANCH_ROUTE },
  93. { "perl_exec", (cmd_function)perl_exec1, 1, NULL, 0,
  94. REQUEST_ROUTE | FAILURE_ROUTE
  95. | ONREPLY_ROUTE | BRANCH_ROUTE },
  96. { "perl_exec", (cmd_function)perl_exec2, 2, NULL, 0,
  97. REQUEST_ROUTE | FAILURE_ROUTE
  98. | ONREPLY_ROUTE | BRANCH_ROUTE },
  99. { 0, 0, 0, 0, 0, 0 }
  100. };
  101. /*
  102. * Exported parameters
  103. */
  104. static param_export_t params[] = {
  105. {"filename", PARAM_STRING, &filename},
  106. {"modpath", PARAM_STRING, &modpath},
  107. {"unsafemodfnc", INT_PARAM, &unsafemodfnc},
  108. {"reset_cycles", INT_PARAM, &_ap_reset_cycles_init},
  109. {"perl_destroy_func", PARAM_STRING, &perl_destroy_func},
  110. { 0, 0, 0 }
  111. };
  112. /*
  113. * Exported MI functions
  114. */
  115. static mi_export_t mi_cmds[] = {
  116. /* FIXME This does not yet work...
  117. { "perl_reload", perl_mi_reload, MI_NO_INPUT_FLAG, 0, 0 },*/
  118. { 0, 0, 0, 0, 0}
  119. };
  120. /*
  121. * Module info
  122. */
  123. #ifndef RTLD_NOW
  124. /* for openbsd */
  125. #define RTLD_NOW DL_LAZY
  126. #endif
  127. #ifndef RTLD_GLOBAL
  128. /* Unsupported! */
  129. #define RTLD_GLOBAL 0
  130. #endif
  131. /*
  132. * Module interface
  133. */
  134. struct module_exports exports = {
  135. "app_perl",
  136. RTLD_NOW | RTLD_GLOBAL,
  137. cmds, /* Exported functions */
  138. params, /* Exported parameters */
  139. 0, /* exported statistics */
  140. mi_cmds, /* exported MI functions */
  141. 0, /* exported pseudo-variables */
  142. 0, /* extra processes */
  143. mod_init, /* module initialization function */
  144. 0, /* response function */
  145. destroy, /* destroy function */
  146. 0 /* child initialization function */
  147. };
  148. EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
  149. EXTERN_C void boot_Kamailio(pTHX_ CV* cv);
  150. /*
  151. * This is output by perl -MExtUtils::Embed -e xsinit
  152. * and complemented by the Kamailio bootstrapping
  153. */
  154. EXTERN_C void xs_init(pTHX) {
  155. char *file = __FILE__;
  156. dXSUB_SYS;
  157. newXS("Kamailio::bootstrap", boot_Kamailio, file);
  158. newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
  159. }
  160. /*
  161. * Initialize the perl interpreter.
  162. * This might later be used to reinit the module.
  163. */
  164. PerlInterpreter *parser_init(void) {
  165. int argc = 0;
  166. char *argv[MAX_LIB_PATHS + 3];
  167. PerlInterpreter *new_perl = NULL;
  168. char *entry, *stop, *end;
  169. int modpathset_start = 0;
  170. int modpathset_end = 0;
  171. int i;
  172. int pr;
  173. new_perl = perl_alloc();
  174. if (!new_perl) {
  175. LM_ERR("could not allocate perl.\n");
  176. return NULL;
  177. }
  178. perl_construct(new_perl);
  179. argv[0] = ""; argc++; /* First param _needs_ to be empty */
  180. /* Possible Include path extension by modparam */
  181. if (modpath && (strlen(modpath) > 0)) {
  182. modpathset_start = argc;
  183. entry = modpath;
  184. stop = modpath + strlen(modpath);
  185. for (end = modpath; end <= stop; end++) {
  186. if ( (end[0] == ':') || (end[0] == '\0') ) {
  187. end[0] = '\0';
  188. if (argc > MAX_LIB_PATHS) {
  189. LM_ERR("too many lib paths, skipping lib path: '%s'\n", entry);
  190. } else {
  191. LM_INFO("setting lib path: '%s'\n", entry);
  192. argv[argc] = pkg_malloc(strlen(entry)+20);
  193. if (!argv[argc]) {
  194. LM_ERR("not enough pkg mem\n");
  195. return NULL;
  196. }
  197. sprintf(argv[argc], "-I%s", entry);
  198. modpathset_end = argc;
  199. argc++;
  200. }
  201. entry = end + 1;
  202. }
  203. }
  204. }
  205. argv[argc] = "-M"DEFAULTMODULE; argc++; /* Always "use" Kamailio.pm */
  206. argv[argc] = filename; /* The script itself */
  207. argc++;
  208. pr=perl_parse(new_perl, xs_init, argc, argv, NULL);
  209. if (pr) {
  210. LM_ERR("failed to load perl file \"%s\" with code %d.\n", argv[argc-1], pr);
  211. if (modpathset_start) {
  212. for (i = modpathset_start; i <= modpathset_end; i++) {
  213. pkg_free(argv[i]);
  214. }
  215. }
  216. return NULL;
  217. } else {
  218. LM_INFO("successfully loaded perl file \"%s\"\n", argv[argc-1]);
  219. }
  220. if (modpathset_start) {
  221. for (i = modpathset_start; i <= modpathset_end; i++) {
  222. pkg_free(argv[i]);
  223. }
  224. }
  225. perl_run(new_perl);
  226. return new_perl;
  227. }
  228. /*
  229. *
  230. */
  231. int unload_perl(PerlInterpreter *p) {
  232. perl_destruct(p);
  233. perl_free(p);
  234. return 0;
  235. }
  236. /*
  237. * reload function.
  238. * Reinitializes the interpreter. Works, but execution for _all_
  239. * children is difficult.
  240. */
  241. int perl_reload(void)
  242. {
  243. PerlInterpreter *new_perl;
  244. new_perl = parser_init();
  245. if (new_perl) {
  246. unload_perl(my_perl);
  247. my_perl = new_perl;
  248. #ifdef PERL_EXIT_DESTRUCT_END
  249. PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
  250. #else
  251. #warning Perl 5.8.x should be used. Please upgrade.
  252. #warning This binary will be unsupported.
  253. PL_exit_flags |= PERL_EXIT_EXPECTED;
  254. #endif
  255. return 0;
  256. } else {
  257. return -1;
  258. }
  259. }
  260. /*
  261. * Reinit through fifo.
  262. * Currently does not seem to work :((
  263. */
  264. struct mi_root* perl_mi_reload(struct mi_root *cmd_tree, void *param)
  265. {
  266. if (perl_reload()<0) {
  267. return init_mi_tree( 500, "Perl reload failed", 18);
  268. } else {
  269. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  270. }
  271. }
  272. /*
  273. * mod_init
  274. * Called by kamailio at init time
  275. */
  276. static int mod_init(void) {
  277. int argc = 1;
  278. char *argt[] = { MOD_NAME, NULL };
  279. char **argv;
  280. struct timeval t1;
  281. struct timeval t2;
  282. if(register_mi_mod(exports.name, mi_cmds)!=0)
  283. {
  284. LM_ERR("failed to register MI commands\n");
  285. return -1;
  286. }
  287. if(ap_init_rpc()<0)
  288. {
  289. LM_ERR("failed to register RPC commands\n");
  290. return -1;
  291. }
  292. if (!filename) {
  293. LM_ERR("insufficient module parameters. Module not loaded.\n");
  294. return -1;
  295. }
  296. /* bind the SL API */
  297. if (sl_load_api(&slb)!=0) {
  298. LM_ERR("cannot bind to SL API\n");
  299. return -1;
  300. }
  301. _ap_reset_cycles = shm_malloc(sizeof(int));
  302. if(_ap_reset_cycles == NULL) {
  303. LM_ERR("no more shared memory\n");
  304. return -1;
  305. }
  306. *_ap_reset_cycles = _ap_reset_cycles_init;
  307. argv = argt;
  308. PERL_SYS_INIT3(&argc, &argv, &environ);
  309. gettimeofday(&t1, NULL);
  310. my_perl = parser_init();
  311. gettimeofday(&t2, NULL);
  312. if (my_perl==NULL)
  313. goto error;
  314. LM_INFO("perl interpreter has been initialized (%d.%06d => %d.%06d)\n",
  315. (int)t1.tv_sec, (int)t1.tv_usec,
  316. (int)t2.tv_sec, (int)t2.tv_usec);
  317. #ifdef PERL_EXIT_DESTRUCT_END
  318. PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
  319. #else
  320. PL_exit_flags |= PERL_EXIT_EXPECTED;
  321. #endif
  322. return 0;
  323. error:
  324. if(_ap_reset_cycles!=NULL)
  325. shm_free(_ap_reset_cycles);
  326. _ap_reset_cycles = NULL;
  327. return -1;
  328. }
  329. /*
  330. * destroy
  331. * called by kamailio at exit time
  332. */
  333. static void destroy(void)
  334. {
  335. if(_ap_reset_cycles!=NULL)
  336. shm_free(_ap_reset_cycles);
  337. _ap_reset_cycles = NULL;
  338. if(my_perl==NULL)
  339. return;
  340. unload_perl(my_perl);
  341. PERL_SYS_TERM();
  342. my_perl = NULL;
  343. }
  344. /**
  345. * count executions and rest interpreter
  346. *
  347. */
  348. int app_perl_reset_interpreter(void)
  349. {
  350. struct timeval t1;
  351. struct timeval t2;
  352. char *args[] = { NULL };
  353. if(*_ap_reset_cycles==0)
  354. return 0;
  355. _ap_exec_cycles++;
  356. LM_DBG("perl interpreter exec cycle [%d/%d]\n",
  357. _ap_exec_cycles, *_ap_reset_cycles);
  358. if(_ap_exec_cycles<=*_ap_reset_cycles)
  359. return 0;
  360. if(perl_destroy_func)
  361. call_argv(perl_destroy_func, G_DISCARD | G_NOARGS, args);
  362. gettimeofday(&t1, NULL);
  363. if (perl_reload()<0) {
  364. LM_ERR("perl interpreter cannot be reset [%d/%d]\n",
  365. _ap_exec_cycles, *_ap_reset_cycles);
  366. return -1;
  367. }
  368. gettimeofday(&t2, NULL);
  369. LM_INFO("perl interpreter has been reset [%d/%d] (%d.%06d => %d.%06d)\n",
  370. _ap_exec_cycles, *_ap_reset_cycles,
  371. (int)t1.tv_sec, (int)t1.tv_usec,
  372. (int)t2.tv_sec, (int)t2.tv_usec);
  373. _ap_exec_cycles = 0;
  374. return 0;
  375. }
  376. /*** RPC implementation ***/
  377. static const char* app_perl_rpc_set_reset_cycles_doc[3] = {
  378. "Set the value for reset_cycles",
  379. "Has one parmeter with int value",
  380. 0
  381. };
  382. /*
  383. * RPC command to set the value for reset_cycles
  384. */
  385. static void app_perl_rpc_set_reset_cycles(rpc_t* rpc, void* ctx)
  386. {
  387. int rsv;
  388. if(rpc->scan(ctx, "d", &rsv)<1)
  389. {
  390. rpc->fault(ctx, 500, "Invalid Parameters");
  391. return;
  392. }
  393. if(rsv<=0)
  394. rsv = 0;
  395. LM_DBG("new reset cycle value is %d\n", rsv);
  396. *_ap_reset_cycles = rsv;
  397. return;
  398. }
  399. static const char* app_perl_rpc_get_reset_cycles_doc[2] = {
  400. "Get the value for reset_cycles",
  401. 0
  402. };
  403. /*
  404. * RPC command to set the value for reset_cycles
  405. */
  406. static void app_perl_rpc_get_reset_cycles(rpc_t* rpc, void* ctx)
  407. {
  408. int rsv;
  409. void* th;
  410. rsv = *_ap_reset_cycles;
  411. /* add entry node */
  412. if (rpc->add(ctx, "{", &th) < 0)
  413. {
  414. rpc->fault(ctx, 500, "Internal error root reply");
  415. return;
  416. }
  417. if(rpc->struct_add(th, "d", "reset_cycles", rsv)<0)
  418. {
  419. rpc->fault(ctx, 500, "Internal error adding reset cycles");
  420. return;
  421. }
  422. LM_DBG("reset cycle value is %d\n", rsv);
  423. return;
  424. }
  425. rpc_export_t app_perl_rpc_cmds[] = {
  426. {"app_perl.set_reset_cycles", app_perl_rpc_set_reset_cycles,
  427. app_perl_rpc_set_reset_cycles_doc, 0},
  428. {"app_perl.get_reset_cycles", app_perl_rpc_get_reset_cycles,
  429. app_perl_rpc_get_reset_cycles_doc, 0},
  430. {0, 0, 0, 0}
  431. };
  432. /**
  433. * register RPC commands
  434. */
  435. static int ap_init_rpc(void)
  436. {
  437. if (rpc_register_array(app_perl_rpc_cmds)!=0)
  438. {
  439. LM_ERR("failed to register RPC commands\n");
  440. return -1;
  441. }
  442. return 0;
  443. }