Browse code

app_perl: work only with my_perl global variable

- some macros link to it

Daniel-Constantin Mierla authored on 08/07/2022 10:26:11
Showing 1 changed files
... ...
@@ -183,24 +183,28 @@ EXTERN_C void xs_init(pTHX) {
183 183
  * Initialize the perl interpreter.
184 184
  * This might later be used to reinit the module.
185 185
  */
186
-PerlInterpreter *parser_init(void) {
186
+int parser_init(void) {
187 187
 	int argc = 0;
188 188
 	char *argv[MAX_LIB_PATHS + 3];
189
-	PerlInterpreter *new_perl = NULL;
190 189
 	char *entry, *stop, *end;
191 190
 	int modpathset_start = 0;
192 191
 	int modpathset_end = 0;
193 192
 	int i;
194 193
 	int pr;
195 194
 
196
-	new_perl = perl_alloc();
195
+	if (my_perl) {
196
+		LM_ERR("perl interpreter already initialized\n");
197
+		return 1;
198
+	}
199
+
200
+	my_perl = perl_alloc();
197 201
 
198
-	if (!new_perl) {
199
-		LM_ERR("could not allocate perl.\n");
200
-		return NULL;
202
+	if (!my_perl) {
203
+		LM_ERR("could not allocate perl interpreter\n");
204
+		return -1;
201 205
 	}
202 206
 
203
-	perl_construct(new_perl);
207
+	perl_construct(my_perl);
204 208
 
205 209
 	argv[0] = ""; argc++; /* First param _needs_ to be empty */
206 210
 
... ...
@@ -220,7 +224,10 @@ PerlInterpreter *parser_init(void) {
220 224
 					argv[argc] = pkg_malloc(strlen(entry)+20);
221 225
 					if (!argv[argc]) {
222 226
 						PKG_MEM_ERROR;
223
-						return NULL;
227
+						perl_destruct(my_perl);
228
+						perl_free(my_perl);
229
+						my_perl = NULL;
230
+						return -1;
224 231
 					}
225 232
 					snprintf(argv[argc], strlen(entry)+20, "-I%s", entry);
226 233
 					modpathset_end = argc;
... ...
@@ -236,7 +243,7 @@ PerlInterpreter *parser_init(void) {
236 243
 	argv[argc] = filename; /* The script itself */
237 244
 	argc++;
238 245
 
239
-	pr=perl_parse(new_perl, xs_init, argc, argv, NULL);
246
+	pr=perl_parse(my_perl, xs_init, argc, argv, NULL);
240 247
 
241 248
 	if (pr) {
242 249
 		if(_ap_parse_mode==0) {
... ...
@@ -250,9 +257,9 @@ PerlInterpreter *parser_init(void) {
250 257
 					pkg_free(argv[i]);
251 258
 				}
252 259
 			}
253
-			perl_destruct(new_perl);
254
-			perl_free(new_perl);
255
-			return NULL;
260
+			perl_destruct(my_perl);
261
+			perl_free(my_perl);
262
+			my_perl = NULL;
256 263
 		}
257 264
 	} else {
258 265
 		LM_INFO("successfully parsed perl file \"%s\"\n", argv[argc-1]);
... ...
@@ -263,21 +270,26 @@ PerlInterpreter *parser_init(void) {
263 270
 			pkg_free(argv[i]);
264 271
 		}
265 272
 	}
266
-	pr = perl_run(new_perl);
273
+	pr = perl_run(my_perl);
267 274
 	LM_INFO("perl run return code %d\n", pr);
268 275
 
269
-	return new_perl;
276
+	return 0;
270 277
 
271 278
 }
272 279
 
273 280
 /*
274 281
  *
275 282
  */
276
-int unload_perl(PerlInterpreter *p) {
283
+int unload_perl(void) {
284
+	if (!my_perl) {
285
+		LM_ERR("perl interpreter not initialized\n");
286
+		return -1;
287
+	}
277 288
 	/* clean and reset everything */
278 289
 	PL_perl_destruct_level = 1;
279
-	perl_destruct(p);
280
-	perl_free(p);
290
+	perl_destruct(my_perl);
291
+	perl_free(my_perl);
292
+	my_perl = NULL;
281 293
 
282 294
 	return 0;
283 295
 }
... ...
@@ -291,9 +303,9 @@ int unload_perl(PerlInterpreter *p) {
291 303
 int perl_reload(void)
292 304
 {
293 305
 	if(my_perl) {
294
-		unload_perl(my_perl);
306
+		unload_perl();
295 307
 	}
296
-	my_perl = parser_init();
308
+	parser_init();
297 309
 
298 310
 	if(my_perl) {
299 311
 		LM_DBG("new perl interpreter initialized\n");
... ...
@@ -353,7 +365,7 @@ static int mod_init(void) {
353 365
 	PERL_SYS_INIT3(&argc, &argv, &environ);
354 366
 
355 367
 	gettimeofday(&t1, NULL);
356
-	my_perl = parser_init();
368
+	parser_init();
357 369
 	gettimeofday(&t2, NULL);
358 370
 
359 371
 	if (my_perl==NULL)
... ...
@@ -389,7 +401,7 @@ static void destroy(void)
389 401
 
390 402
 	if(my_perl==NULL)
391 403
 		return;
392
-	unload_perl(my_perl);
404
+	unload_perl();
393 405
 	PERL_SYS_TERM();
394 406
 	my_perl = NULL;
395 407
 }