app_perl_mod.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. sprintf(argv[argc], "-I%s", entry);
  194. modpathset_end = argc;
  195. argc++;
  196. }
  197. entry = end + 1;
  198. }
  199. }
  200. }
  201. argv[argc] = "-M"DEFAULTMODULE; argc++; /* Always "use" Kamailio.pm */
  202. argv[argc] = filename; /* The script itself */
  203. argc++;
  204. pr=perl_parse(new_perl, xs_init, argc, argv, NULL);
  205. if (pr) {
  206. LM_ERR("failed to load perl file \"%s\" with code %d.\n", argv[argc-1], pr);
  207. if (modpathset_start) {
  208. for (i = modpathset_start; i <= modpathset_end; i++) {
  209. pkg_free(argv[i]);
  210. }
  211. }
  212. return NULL;
  213. } else {
  214. LM_INFO("successfully loaded perl file \"%s\"\n", argv[argc-1]);
  215. }
  216. if (modpathset_start) {
  217. for (i = modpathset_start; i <= modpathset_end; i++) {
  218. pkg_free(argv[i]);
  219. }
  220. }
  221. perl_run(new_perl);
  222. return new_perl;
  223. }
  224. /*
  225. *
  226. */
  227. int unload_perl(PerlInterpreter *p) {
  228. perl_destruct(p);
  229. perl_free(p);
  230. return 0;
  231. }
  232. /*
  233. * reload function.
  234. * Reinitializes the interpreter. Works, but execution for _all_
  235. * children is difficult.
  236. */
  237. int perl_reload(void)
  238. {
  239. PerlInterpreter *new_perl;
  240. new_perl = parser_init();
  241. if (new_perl) {
  242. unload_perl(my_perl);
  243. my_perl = new_perl;
  244. #ifdef PERL_EXIT_DESTRUCT_END
  245. PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
  246. #else
  247. #warning Perl 5.8.x should be used. Please upgrade.
  248. #warning This binary will be unsupported.
  249. PL_exit_flags |= PERL_EXIT_EXPECTED;
  250. #endif
  251. return 0;
  252. } else {
  253. return -1;
  254. }
  255. }
  256. /*
  257. * Reinit through fifo.
  258. * Currently does not seem to work :((
  259. */
  260. struct mi_root* perl_mi_reload(struct mi_root *cmd_tree, void *param)
  261. {
  262. if (perl_reload()<0) {
  263. return init_mi_tree( 500, "Perl reload failed", 18);
  264. } else {
  265. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  266. }
  267. }
  268. /*
  269. * mod_init
  270. * Called by kamailio at init time
  271. */
  272. static int mod_init(void) {
  273. int argc = 1;
  274. char *argt[] = { MOD_NAME, NULL };
  275. char **argv;
  276. struct timeval t1;
  277. struct timeval t2;
  278. if(register_mi_mod(exports.name, mi_cmds)!=0)
  279. {
  280. LM_ERR("failed to register MI commands\n");
  281. return -1;
  282. }
  283. if(ap_init_rpc()<0)
  284. {
  285. LM_ERR("failed to register RPC commands\n");
  286. return -1;
  287. }
  288. if (!filename) {
  289. LM_ERR("insufficient module parameters. Module not loaded.\n");
  290. return -1;
  291. }
  292. /* bind the SL API */
  293. if (sl_load_api(&slb)!=0) {
  294. LM_ERR("cannot bind to SL API\n");
  295. return -1;
  296. }
  297. _ap_reset_cycles = shm_malloc(sizeof(int));
  298. if(_ap_reset_cycles == NULL) {
  299. LM_ERR("no more shared memory\n");
  300. return -1;
  301. }
  302. *_ap_reset_cycles = _ap_reset_cycles_init;
  303. argv = argt;
  304. PERL_SYS_INIT3(&argc, &argv, &environ);
  305. gettimeofday(&t1, NULL);
  306. my_perl = parser_init();
  307. gettimeofday(&t2, NULL);
  308. if (my_perl==NULL)
  309. goto error;
  310. LM_INFO("perl interpreter has been initialized (%d.%06d => %d.%06d)\n",
  311. (int)t1.tv_sec, (int)t1.tv_usec,
  312. (int)t2.tv_sec, (int)t2.tv_usec);
  313. #ifdef PERL_EXIT_DESTRUCT_END
  314. PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
  315. #else
  316. PL_exit_flags |= PERL_EXIT_EXPECTED;
  317. #endif
  318. return 0;
  319. error:
  320. if(_ap_reset_cycles!=NULL)
  321. shm_free(_ap_reset_cycles);
  322. _ap_reset_cycles = NULL;
  323. return -1;
  324. }
  325. /*
  326. * destroy
  327. * called by kamailio at exit time
  328. */
  329. static void destroy(void)
  330. {
  331. if(_ap_reset_cycles!=NULL)
  332. shm_free(_ap_reset_cycles);
  333. _ap_reset_cycles = NULL;
  334. if(my_perl==NULL)
  335. return;
  336. unload_perl(my_perl);
  337. PERL_SYS_TERM();
  338. my_perl = NULL;
  339. }
  340. /**
  341. * count executions and rest interpreter
  342. *
  343. */
  344. int app_perl_reset_interpreter(void)
  345. {
  346. struct timeval t1;
  347. struct timeval t2;
  348. char *args[] = { NULL };
  349. if(*_ap_reset_cycles==0)
  350. return 0;
  351. _ap_exec_cycles++;
  352. LM_DBG("perl interpreter exec cycle [%d/%d]\n",
  353. _ap_exec_cycles, *_ap_reset_cycles);
  354. if(_ap_exec_cycles<=*_ap_reset_cycles)
  355. return 0;
  356. if(perl_destroy_func)
  357. call_argv(perl_destroy_func, G_DISCARD | G_NOARGS, args);
  358. gettimeofday(&t1, NULL);
  359. if (perl_reload()<0) {
  360. LM_ERR("perl interpreter cannot be reset [%d/%d]\n",
  361. _ap_exec_cycles, *_ap_reset_cycles);
  362. return -1;
  363. }
  364. gettimeofday(&t2, NULL);
  365. LM_INFO("perl interpreter has been reset [%d/%d] (%d.%06d => %d.%06d)\n",
  366. _ap_exec_cycles, *_ap_reset_cycles,
  367. (int)t1.tv_sec, (int)t1.tv_usec,
  368. (int)t2.tv_sec, (int)t2.tv_usec);
  369. _ap_exec_cycles = 0;
  370. return 0;
  371. }
  372. /*** RPC implementation ***/
  373. static const char* app_perl_rpc_set_reset_cycles_doc[3] = {
  374. "Set the value for reset_cycles",
  375. "Has one parmeter with int value",
  376. 0
  377. };
  378. /*
  379. * RPC command to set the value for reset_cycles
  380. */
  381. static void app_perl_rpc_set_reset_cycles(rpc_t* rpc, void* ctx)
  382. {
  383. int rsv;
  384. if(rpc->scan(ctx, "d", &rsv)<1)
  385. {
  386. rpc->fault(ctx, 500, "Invalid Parameters");
  387. return;
  388. }
  389. if(rsv<=0)
  390. rsv = 0;
  391. LM_DBG("new reset cycle value is %d\n", rsv);
  392. *_ap_reset_cycles = rsv;
  393. return;
  394. }
  395. static const char* app_perl_rpc_get_reset_cycles_doc[2] = {
  396. "Get the value for reset_cycles",
  397. 0
  398. };
  399. /*
  400. * RPC command to set the value for reset_cycles
  401. */
  402. static void app_perl_rpc_get_reset_cycles(rpc_t* rpc, void* ctx)
  403. {
  404. int rsv;
  405. void* th;
  406. rsv = *_ap_reset_cycles;
  407. /* add entry node */
  408. if (rpc->add(ctx, "{", &th) < 0)
  409. {
  410. rpc->fault(ctx, 500, "Internal error root reply");
  411. return;
  412. }
  413. if(rpc->struct_add(th, "d", "reset_cycles", rsv)<0)
  414. {
  415. rpc->fault(ctx, 500, "Internal error adding reset cycles");
  416. return;
  417. }
  418. LM_DBG("reset cycle value is %d\n", rsv);
  419. return;
  420. }
  421. rpc_export_t app_perl_rpc_cmds[] = {
  422. {"app_perl.set_reset_cycles", app_perl_rpc_set_reset_cycles,
  423. app_perl_rpc_set_reset_cycles_doc, 0},
  424. {"app_perl.get_reset_cycles", app_perl_rpc_get_reset_cycles,
  425. app_perl_rpc_get_reset_cycles_doc, 0},
  426. {0, 0, 0, 0}
  427. };
  428. /**
  429. * register RPC commands
  430. */
  431. static int ap_init_rpc(void)
  432. {
  433. if (rpc_register_array(app_perl_rpc_cmds)!=0)
  434. {
  435. LM_ERR("failed to register RPC commands\n");
  436. return -1;
  437. }
  438. return 0;
  439. }