app_perl_mod.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. /* lock_ops.h defines union semun, perl does not need to redefine it */
  40. #ifdef USE_SYSV_SEM
  41. # define HAS_UNION_SEMUN
  42. #endif
  43. #include "perlfunc.h"
  44. #include "app_perl_mod.h"
  45. /* #include "perlxsi.h" function is in here... */
  46. MODULE_VERSION
  47. /* Full path to the script including executed functions */
  48. char *filename = NULL;
  49. /* Path to an arbitrary directory where the Kamailio Perl modules are
  50. * installed */
  51. char *modpath = NULL;
  52. /* Allow unsafe module functions - functions with fixups. This will create
  53. * memory leaks, the variable thus is not documented! */
  54. int unsafemodfnc = 0;
  55. /* number of execution cycles after which perl interpreter is reset */
  56. int _ap_reset_cycles_init = 0;
  57. int _ap_exec_cycles = 0;
  58. int *_ap_reset_cycles = 0;
  59. /* Reference to the running Perl interpreter instance */
  60. PerlInterpreter *my_perl = NULL;
  61. /** SL API structure */
  62. sl_api_t slb;
  63. /*
  64. * Module destroy function prototype
  65. */
  66. static void destroy(void);
  67. /* environment pointer needed to init perl interpreter */
  68. extern char **environ;
  69. /*
  70. * Module initialization function prototype
  71. */
  72. static int mod_init(void);
  73. /*
  74. * Reload perl interpreter - reload perl script. Forward declaration.
  75. */
  76. struct mi_root* perl_mi_reload(struct mi_root *cmd_tree, void *param);
  77. /*
  78. * Exported functions
  79. */
  80. static cmd_export_t cmds[] = {
  81. { "perl_exec_simple", (cmd_function)perl_exec_simple1, 1, NULL, 0,
  82. REQUEST_ROUTE | FAILURE_ROUTE
  83. | ONREPLY_ROUTE | BRANCH_ROUTE },
  84. { "perl_exec_simple", (cmd_function)perl_exec_simple2, 2, NULL, 0,
  85. REQUEST_ROUTE | FAILURE_ROUTE
  86. | ONREPLY_ROUTE | BRANCH_ROUTE },
  87. { "perl_exec", (cmd_function)perl_exec1, 1, NULL, 0,
  88. REQUEST_ROUTE | FAILURE_ROUTE
  89. | ONREPLY_ROUTE | BRANCH_ROUTE },
  90. { "perl_exec", (cmd_function)perl_exec2, 2, NULL, 0,
  91. REQUEST_ROUTE | FAILURE_ROUTE
  92. | ONREPLY_ROUTE | BRANCH_ROUTE },
  93. { 0, 0, 0, 0, 0, 0 }
  94. };
  95. /*
  96. * Exported parameters
  97. */
  98. static param_export_t params[] = {
  99. {"filename", STR_PARAM, &filename},
  100. {"modpath", STR_PARAM, &modpath},
  101. {"unsafemodfnc", INT_PARAM, &unsafemodfnc},
  102. {"reset_cycles", INT_PARAM, &_ap_reset_cycles_init},
  103. { 0, 0, 0 }
  104. };
  105. /*
  106. * Exported MI functions
  107. */
  108. static mi_export_t mi_cmds[] = {
  109. /* FIXME This does not yet work...
  110. { "perl_reload", perl_mi_reload, MI_NO_INPUT_FLAG, 0, 0 },*/
  111. { 0, 0, 0, 0, 0}
  112. };
  113. /*
  114. * Module info
  115. */
  116. #ifndef RTLD_NOW
  117. /* for openbsd */
  118. #define RTLD_NOW DL_LAZY
  119. #endif
  120. #ifndef RTLD_GLOBAL
  121. /* Unsupported! */
  122. #define RTLD_GLOBAL 0
  123. #endif
  124. /*
  125. * Module interface
  126. */
  127. struct module_exports exports = {
  128. "app_perl",
  129. RTLD_NOW | RTLD_GLOBAL,
  130. cmds, /* Exported functions */
  131. params, /* Exported parameters */
  132. 0, /* exported statistics */
  133. mi_cmds, /* exported MI functions */
  134. 0, /* exported pseudo-variables */
  135. 0, /* extra processes */
  136. mod_init, /* module initialization function */
  137. 0, /* response function */
  138. destroy, /* destroy function */
  139. 0 /* child initialization function */
  140. };
  141. EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
  142. EXTERN_C void boot_Kamailio(pTHX_ CV* cv);
  143. /*
  144. * This is output by perl -MExtUtils::Embed -e xsinit
  145. * and complemented by the Kamailio bootstrapping
  146. */
  147. EXTERN_C void xs_init(pTHX) {
  148. char *file = __FILE__;
  149. dXSUB_SYS;
  150. newXS("Kamailio::bootstrap", boot_Kamailio, file);
  151. newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
  152. }
  153. /*
  154. * Initialize the perl interpreter.
  155. * This might later be used to reinit the module.
  156. */
  157. PerlInterpreter *parser_init(void) {
  158. int argc = 0;
  159. char *argv[MAX_LIB_PATHS + 3];
  160. PerlInterpreter *new_perl = NULL;
  161. char *entry, *stop, *end;
  162. int modpathset_start = 0;
  163. int modpathset_end = 0;
  164. int i;
  165. new_perl = perl_alloc();
  166. if (!new_perl) {
  167. LM_ERR("could not allocate perl.\n");
  168. return NULL;
  169. }
  170. perl_construct(new_perl);
  171. argv[0] = ""; argc++; /* First param _needs_ to be empty */
  172. /* Possible Include path extension by modparam */
  173. if (modpath && (strlen(modpath) > 0)) {
  174. modpathset_start = argc;
  175. entry = modpath;
  176. stop = modpath + strlen(modpath);
  177. for (end = modpath; end <= stop; end++) {
  178. if ( (end[0] == ':') || (end[0] == '\0') ) {
  179. end[0] = '\0';
  180. if (argc > MAX_LIB_PATHS) {
  181. LM_ERR("too many lib paths, skipping lib path: '%s'\n", entry);
  182. } else {
  183. LM_INFO("setting lib path: '%s'\n", entry);
  184. argv[argc] = pkg_malloc(strlen(entry)+20);
  185. sprintf(argv[argc], "-I%s", entry);
  186. modpathset_end = argc;
  187. argc++;
  188. }
  189. entry = end + 1;
  190. }
  191. }
  192. }
  193. argv[argc] = "-M"DEFAULTMODULE; argc++; /* Always "use" Kamailio.pm */
  194. argv[argc] = filename; /* The script itself */
  195. argc++;
  196. if (perl_parse(new_perl, xs_init, argc, argv, NULL)) {
  197. LM_ERR("failed to load perl file \"%s\".\n", argv[argc-1]);
  198. if (modpathset_start) {
  199. for (i = modpathset_start; i <= modpathset_end; i++) {
  200. pkg_free(argv[i]);
  201. }
  202. }
  203. return NULL;
  204. } else {
  205. LM_INFO("successfully loaded perl file \"%s\"\n", argv[argc-1]);
  206. }
  207. if (modpathset_start) {
  208. for (i = modpathset_start; i <= modpathset_end; i++) {
  209. pkg_free(argv[i]);
  210. }
  211. }
  212. perl_run(new_perl);
  213. return new_perl;
  214. }
  215. /*
  216. *
  217. */
  218. int unload_perl(PerlInterpreter *p) {
  219. perl_destruct(p);
  220. perl_free(p);
  221. return 0;
  222. }
  223. /*
  224. * reload function.
  225. * Reinitializes the interpreter. Works, but execution for _all_
  226. * children is difficult.
  227. */
  228. int perl_reload(void)
  229. {
  230. PerlInterpreter *new_perl;
  231. new_perl = parser_init();
  232. if (new_perl) {
  233. unload_perl(my_perl);
  234. my_perl = new_perl;
  235. #ifdef PERL_EXIT_DESTRUCT_END
  236. PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
  237. #else
  238. #warning Perl 5.8.x should be used. Please upgrade.
  239. #warning This binary will be unsupported.
  240. PL_exit_flags |= PERL_EXIT_EXPECTED;
  241. #endif
  242. return 0;
  243. } else {
  244. return -1;
  245. }
  246. }
  247. /*
  248. * Reinit through fifo.
  249. * Currently does not seem to work :((
  250. */
  251. struct mi_root* perl_mi_reload(struct mi_root *cmd_tree, void *param)
  252. {
  253. if (perl_reload()<0) {
  254. return init_mi_tree( 500, "Perl reload failed", 18);
  255. } else {
  256. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  257. }
  258. }
  259. /*
  260. * mod_init
  261. * Called by kamailio at init time
  262. */
  263. static int mod_init(void) {
  264. int ret = 0;
  265. int argc = 1;
  266. char *argt[] = { MOD_NAME, NULL };
  267. char **argv;
  268. struct timeval t1;
  269. struct timeval t2;
  270. if(register_mi_mod(exports.name, mi_cmds)!=0)
  271. {
  272. LM_ERR("failed to register MI commands\n");
  273. return -1;
  274. }
  275. if (!filename) {
  276. LM_ERR("insufficient module parameters. Module not loaded.\n");
  277. return -1;
  278. }
  279. /* bind the SL API */
  280. if (sl_load_api(&slb)!=0) {
  281. LM_ERR("cannot bind to SL API\n");
  282. return -1;
  283. }
  284. _ap_reset_cycles = shm_malloc(sizeof(int));
  285. if(_ap_reset_cycles == NULL) {
  286. LM_ERR("no more shared memory\n");
  287. return -1;
  288. }
  289. *_ap_reset_cycles = _ap_reset_cycles_init;
  290. argv = argt;
  291. PERL_SYS_INIT3(&argc, &argv, &environ);
  292. gettimeofday(&t1, NULL);
  293. my_perl = parser_init();
  294. gettimeofday(&t2, NULL);
  295. if (my_perl==NULL)
  296. goto error;
  297. LM_INFO("perl interpreter has been initialized (%d.%06d => %d.%06d)\n",
  298. (int)t1.tv_sec, (int)t1.tv_usec,
  299. (int)t2.tv_sec, (int)t2.tv_usec);
  300. #ifdef PERL_EXIT_DESTRUCT_END
  301. PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
  302. #else
  303. PL_exit_flags |= PERL_EXIT_EXPECTED;
  304. #endif
  305. return 0;
  306. error:
  307. if(_ap_reset_cycles!=NULL)
  308. shm_free(_ap_reset_cycles);
  309. _ap_reset_cycles = NULL;
  310. return -1;
  311. }
  312. /*
  313. * destroy
  314. * called by kamailio at exit time
  315. */
  316. static void destroy(void)
  317. {
  318. if(my_perl==NULL)
  319. return;
  320. unload_perl(my_perl);
  321. PERL_SYS_TERM();
  322. my_perl = NULL;
  323. }
  324. /**
  325. * count executions and rest interpreter
  326. *
  327. */
  328. int app_perl_reset_interpreter(void)
  329. {
  330. struct timeval t1;
  331. struct timeval t2;
  332. if(*_ap_reset_cycles==0)
  333. return 0;
  334. _ap_exec_cycles++;
  335. LM_DBG("perl interpreter exec cycle [%d/%d]\n",
  336. _ap_exec_cycles, *_ap_reset_cycles);
  337. if(_ap_exec_cycles<=*_ap_reset_cycles)
  338. return 0;
  339. gettimeofday(&t1, NULL);
  340. if (perl_reload()<0) {
  341. LM_ERR("perl interpreter cannot be reset [%d/%d]\n",
  342. _ap_exec_cycles, *_ap_reset_cycles);
  343. return -1;
  344. }
  345. gettimeofday(&t2, NULL);
  346. LM_INFO("perl interpreter has been reset [%d/%d] (%d.%06d => %d.%06d)\n",
  347. _ap_exec_cycles, *_ap_reset_cycles,
  348. (int)t1.tv_sec, (int)t1.tv_usec,
  349. (int)t2.tv_sec, (int)t2.tv_usec);
  350. _ap_exec_cycles = 0;
  351. return 0;
  352. }