-
Notifications
You must be signed in to change notification settings - Fork 564
/
perl.c
362 lines (291 loc) · 7.43 KB
/
perl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Perl module for OpenSIPS
*
* Copyright (C) 2006 Collax GmbH
* (Bastian Friedrich <bastian.friedrich@collax.com>)
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#define DEFAULTMODULE "OpenSIPS"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include "../../sr_module.h"
#include "../../mem/mem.h"
#include "../../mi/mi.h"
#include "../rr/api.h"
#include "../sl/sl_api.h"
/* lock_ops.h defines union semun, perl does not need to redefine it */
#ifdef USE_SYSV_SEM
# define HAS_UNION_SEMUN
#endif
#include "perlfunc.h"
#include "perl.h"
/* #include "perlxsi.h" function is in here... */
/* Full path to the script including executed functions */
char *filename = NULL;
/* Path to an arbitrary directory where the OpenSIPS Perl modules are
* installed */
char *modpath = NULL;
/* Reference to the running Perl interpreter instance */
PerlInterpreter *my_perl = NULL;
/** SIGNALING binds */
struct sig_binds sigb;
/*
* Module destroy function prototype
*/
static void destroy(void);
/*
* Module child-init function prototype
*/
static int child_init(int rank);
/*
* Module initialization function prototype
*/
static int mod_init(void);
/*
* Reload perl interpreter - reload perl script. Forward declaration.
*/
mi_response_t *perl_mi_reload(const mi_params_t *params,
struct mi_handler *async_hdl);
/*
* Exported functions
*/
static cmd_export_t cmds[] = {
{"perl_exec_simple", (cmd_function)perl_exec_simple, {
{CMD_PARAM_STR,0,0},
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0}, {0,0,0}},
ALL_ROUTES},
{"perl_exec", (cmd_function)perl_exec, {
{CMD_PARAM_STR,0,0},
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0}, {0,0,0}},
REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE},
{0,0,{{0,0,0}},0}
};
/*
* Exported parameters
*/
static param_export_t params[] = {
{"filename", STR_PARAM, &filename},
{"modpath", STR_PARAM, &modpath},
{ 0, 0, 0 }
};
/*
* Exported MI functions
*/
static mi_export_t mi_cmds[] = {
/* FIXME This does not yet work...
{ "perl_reload", 0,0,0, {
{perl_mi_reload, {0}},
{EMPTY_MI_RECIPE}}
},
*/
{EMPTY_MI_EXPORT}
};
static dep_export_t deps = {
{ /* OpenSIPS module dependencies */
{ MOD_TYPE_DEFAULT, "signaling", DEP_ABORT },
{ MOD_TYPE_NULL, NULL, 0 },
},
{ /* modparam dependencies */
{ NULL, NULL },
},
};
/*
* Module info
*/
#ifndef RTLD_NOW
/* for openbsd */
#define RTLD_NOW DL_LAZY
#endif
#ifndef RTLD_GLOBAL
/* Unsupported! */
#define RTLD_GLOBAL 0
#endif
/*
* Module interface
*/
struct module_exports exports = {
"perl",
MOD_TYPE_DEFAULT,/* class of this module */
MODULE_VERSION,
RTLD_NOW | RTLD_GLOBAL,
0, /* load function */
&deps, /* OpenSIPS module dependencies */
cmds, /* Exported functions */
0, /* Exported async functions */
params, /* Exported parameters */
0, /* exported statistics */
mi_cmds, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* exported transformations */
0, /* extra processes */
0, /* module pre-initialization function */
mod_init, /* module initialization function */
0, /* response function */
destroy, /* destroy function */
child_init, /* child initialization function */
0 /* reload confirm function */
};
static int child_init(int rank)
{
return 0;
}
EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
EXTERN_C void boot_OpenSIPS(pTHX_ CV* cv);
/*
* This is output by perl -MExtUtils::Embed -e xsinit
* and complemented by the OpenSIPS bootstrapping
*/
EXTERN_C void xs_init(pTHX) {
char *file = __FILE__;
dXSUB_SYS;
newXS("OpenSIPS::bootstrap", boot_OpenSIPS, file);
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
}
/*
* Initialize the perl interpreter.
* This might later be used to reinit the module.
*/
PerlInterpreter *parser_init(void) {
int argc = 0;
char *argv[9];
PerlInterpreter *new_perl = NULL;
int modpathset = 0;
new_perl = perl_alloc();
if (!new_perl) {
LM_ERR("could not allocate perl.\n");
return NULL;
}
perl_construct(new_perl);
argv[0] = ""; argc++; /* First param _needs_ to be empty */
/* Possible Include path extension by modparam */
if (modpath && (strlen(modpath) > 0)) {
modpathset = argc;
LM_INFO("setting lib path: '%s'\n", modpath);
argv[argc] = pkg_malloc(strlen(modpath)+20);
sprintf(argv[argc], "-I%s", modpath);
argc++;
}
argv[argc] = "-M"DEFAULTMODULE; argc++; /* Always "use" Opensips.pm */
argv[argc] = filename; /* The script itself */
argc++;
if (perl_parse(new_perl, xs_init, argc, argv, NULL)) {
LM_ERR("failed to load perl file \"%s\".\n", argv[argc-1]);
if (modpathset) pkg_free(argv[modpathset]);
return NULL;
} else {
LM_INFO("successfully loaded perl file \"%s\"\n", argv[argc-1]);
}
if (modpathset) pkg_free(argv[modpathset]);
perl_run(new_perl);
return new_perl;
}
/*
*
*/
int unload_perl(PerlInterpreter *p) {
perl_destruct(p);
perl_free(p);
return 0;
}
/*
* reload function.
* Reinitializes the interpreter. Works, but execution for _all_
* children is difficult.
*/
int perl_reload(struct sip_msg *m, char *a, char *b) {
PerlInterpreter *new_perl;
new_perl = parser_init();
if (new_perl) {
unload_perl(my_perl);
my_perl = new_perl;
#ifdef PERL_EXIT_DESTRUCT_END
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
#else
#warning Perl 5.8.x should be used. Please upgrade.
#warning This binary will be unsupported.
PL_exit_flags |= PERL_EXIT_EXPECTED;
#endif
return 1;
} else {
return 0;
}
}
/*
* Reinit through fifo.
* Currently does not seem to work :((
*/
mi_response_t *perl_mi_reload(const mi_params_t *params,
struct mi_handler *async_hdl)
{
if (perl_reload(NULL, NULL, NULL)) {
return init_mi_result_ok();
} else {
return init_mi_error(500, MI_SSTR("Perl reload failed"));
}
}
/*
* mod_init
* Called by opensips at init time
*/
static int mod_init(void) {
int ret = 0;
static int argc = 1;
static char *argv_name = "opensips";
static char **argv = { &argv_name };
LM_INFO("initializing...\n");
if (!filename) {
LM_ERR("insufficient module parameters. Module not loaded.\n");
return -1;
}
/**
* We will need reply() from signaling
* module for sending replies
*/
/* load SIGNALING API */
if(load_sig_api(&sigb)< 0) {
LM_ERR("can't load signaling functions\n");
return -1;
}
PERL_SYS_INIT3(&argc, &argv, &environ);
if ((my_perl = parser_init())) {
ret = 0;
#ifdef PERL_EXIT_DESTRUCT_END
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
#else
PL_exit_flags |= PERL_EXIT_EXPECTED;
#endif
} else {
ret = -1;
}
return ret;
}
/*
* destroy
* called by opensips at exit time
*/
static void destroy(void)
{
if(my_perl==NULL)
return;
unload_perl(my_perl);
PERL_SYS_TERM();
my_perl = NULL;
}