app_perl_mod.c 7.7 KB

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