diff --git a/src/modules/app_perl/app_perl_mod.c b/src/modules/app_perl/app_perl_mod.c index 2e3fc3683dc..ce3eff3699e 100644 --- a/src/modules/app_perl/app_perl_mod.c +++ b/src/modules/app_perl/app_perl_mod.c @@ -183,24 +183,28 @@ EXTERN_C void xs_init(pTHX) { * Initialize the perl interpreter. * This might later be used to reinit the module. */ -PerlInterpreter *parser_init(void) { +int parser_init(void) { int argc = 0; char *argv[MAX_LIB_PATHS + 3]; - PerlInterpreter *new_perl = NULL; char *entry, *stop, *end; int modpathset_start = 0; int modpathset_end = 0; int i; int pr; - new_perl = perl_alloc(); + if (my_perl) { + LM_ERR("perl interpreter already initialized\n"); + return 1; + } + + my_perl = perl_alloc(); - if (!new_perl) { - LM_ERR("could not allocate perl.\n"); - return NULL; + if (!my_perl) { + LM_ERR("could not allocate perl interpreter\n"); + return -1; } - perl_construct(new_perl); + perl_construct(my_perl); argv[0] = ""; argc++; /* First param _needs_ to be empty */ @@ -220,7 +224,10 @@ PerlInterpreter *parser_init(void) { argv[argc] = pkg_malloc(strlen(entry)+20); if (!argv[argc]) { PKG_MEM_ERROR; - return NULL; + perl_destruct(my_perl); + perl_free(my_perl); + my_perl = NULL; + return -1; } snprintf(argv[argc], strlen(entry)+20, "-I%s", entry); modpathset_end = argc; @@ -236,7 +243,7 @@ PerlInterpreter *parser_init(void) { argv[argc] = filename; /* The script itself */ argc++; - pr=perl_parse(new_perl, xs_init, argc, argv, NULL); + pr=perl_parse(my_perl, xs_init, argc, argv, NULL); if (pr) { if(_ap_parse_mode==0) { @@ -250,9 +257,9 @@ PerlInterpreter *parser_init(void) { pkg_free(argv[i]); } } - perl_destruct(new_perl); - perl_free(new_perl); - return NULL; + perl_destruct(my_perl); + perl_free(my_perl); + my_perl = NULL; } } else { LM_INFO("successfully parsed perl file \"%s\"\n", argv[argc-1]); @@ -263,21 +270,26 @@ PerlInterpreter *parser_init(void) { pkg_free(argv[i]); } } - pr = perl_run(new_perl); + pr = perl_run(my_perl); LM_INFO("perl run return code %d\n", pr); - return new_perl; + return 0; } /* * */ -int unload_perl(PerlInterpreter *p) { +int unload_perl(void) { + if (!my_perl) { + LM_ERR("perl interpreter not initialized\n"); + return -1; + } /* clean and reset everything */ PL_perl_destruct_level = 1; - perl_destruct(p); - perl_free(p); + perl_destruct(my_perl); + perl_free(my_perl); + my_perl = NULL; return 0; } @@ -291,9 +303,9 @@ int unload_perl(PerlInterpreter *p) { int perl_reload(void) { if(my_perl) { - unload_perl(my_perl); + unload_perl(); } - my_perl = parser_init(); + parser_init(); if(my_perl) { LM_DBG("new perl interpreter initialized\n"); @@ -353,7 +365,7 @@ static int mod_init(void) { PERL_SYS_INIT3(&argc, &argv, &environ); gettimeofday(&t1, NULL); - my_perl = parser_init(); + parser_init(); gettimeofday(&t2, NULL); if (my_perl==NULL) @@ -389,7 +401,7 @@ static void destroy(void) if(my_perl==NULL) return; - unload_perl(my_perl); + unload_perl(); PERL_SYS_TERM(); my_perl = NULL; }