-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_mod.c
More file actions
327 lines (285 loc) · 9.06 KB
/
python_mod.c
File metadata and controls
327 lines (285 loc) · 9.06 KB
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
/* $Id: python_mod.c,v 1.1 2009/12/09 09:28:26 root Exp $
*
* Copyright (C) 2009 Sippy Software, Inc., http://www.sippysoft.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "../../str.h"
#include "../../sr_module.h"
#include "../../mem/shm_mem.h"
#include "../tm/tm_load.h"
#include "../dialog/dlg_load.h"
#include "python_exec.h"
#include "python_iface.h"
#include "python_msgobj.h"
#include "python_support.h"
#include <Python.h>
#include <libgen.h>
static int mod_init(void);
static int child_init(int rank);
static void mod_destroy(void);
static str python_router = {.s = "pyopensips.confighelper.router", .len = 0};
static str script_name = {.s = "handler.py", .len = 0};
static str mod_init_fname = { .s = "mod_init", .len = 0};
static str child_init_mname = { .s = "child_init", .len = 0};
PyObject *handler_obj;
PyObject *format_exc_obj;
PyThreadState *myThreadState;
/* External module bindings */
struct dlg_binds *lb_dlg_binds;
struct tm_binds python_tmb;
/** module parameters */
static param_export_t params[]={
{"python_router", STR_PARAM, &python_router},
{"script_name", STR_PARAM, &script_name },
{"mod_init_function", STR_PARAM, &mod_init_fname },
{"child_init_method", STR_PARAM, &child_init_mname },
{0,0,0}
};
/*
* Exported functions
*/
static cmd_export_t cmds[] = {
{ "python_exec", (cmd_function)python_exec1, 1, NULL, 0,
REQUEST_ROUTE | FAILURE_ROUTE
| ONREPLY_ROUTE | BRANCH_ROUTE },
{ "python_exec", (cmd_function)python_exec2, 2, NULL, 0,
REQUEST_ROUTE | FAILURE_ROUTE
| ONREPLY_ROUTE | BRANCH_ROUTE },
{ 0, 0, 0, 0, 0, 0 }
};
/** module exports */
struct module_exports exports = {
"python", /* module name */
MODULE_VERSION,
RTLD_NOW | RTLD_GLOBAL, /* dlopen flags */
cmds, /* exported functions */
params, /* exported parameters */
0, /* exported statistics */
0, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* extra processes */
mod_init, /* module initialization function */
(response_function) NULL, /* response handling function */
(destroy_function) mod_destroy, /* destroy function */
child_init /* per-child init function */
};
static int
mod_init(void)
{
char *dname, *bname;
static char temp_s[1000];
int i;
PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
PyThreadState *mainThreadState;
/* Load dialog API */
lb_dlg_binds = (struct dlg_binds*)shm_malloc( sizeof(struct dlg_binds) );
if (lb_dlg_binds == 0) {
LM_CRIT("failed to get shm mem for dialog binding data struct\n");
return -1;
}
if (load_dlg_api(lb_dlg_binds) != 0) {
shm_free(lb_dlg_binds);
lb_dlg_binds = NULL;
}
/* load TM API */
if (load_tm_api(&python_tmb)!=0) {
LM_ERR("can't load TM API\n");
return -1;
}
if (script_name.len == 0) {
script_name.len = strlen(script_name.s);
}
if (mod_init_fname.len == 0) {
mod_init_fname.len = strlen(mod_init_fname.s);
}
if (child_init_mname.len == 0) {
child_init_mname.len = strlen(child_init_mname.s);
}
strncpy(temp_s, script_name.s, sizeof(temp_s));
dname = dirname(temp_s);
printf("after dirname");
if (strlen(dname) == 0)
dname = ".";
strncpy(temp_s, script_name.s, sizeof(temp_s));
bname = basename(temp_s);
i = strlen(bname);
if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
i -= 1;
if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
bname[i - 3] = '\0';
}
/*
else {
LM_ERR("%s: script_name doesn't look like a python script\n",
script_name.s);
return -1;
}
*/
Py_Initialize();
PyEval_InitThreads();
mainThreadState = PyThreadState_Get();
Py_InitModule("OpenSIPS", OpenSIPSMethods);
if (python_msgobj_init() != 0) {
LM_ERR("python_msgobj_init() has failed\n");
PyEval_ReleaseLock();
return -1;
}
sys_path = PySys_GetObject("path");
/* PySys_GetObject doesn't pass reference! No need to DEREF */
if (sys_path == NULL) {
LM_ERR("cannot import sys.path\n");
PyEval_ReleaseLock();
return -1;
}
pDir = PyString_FromString(dname);
if (pDir == NULL) {
LM_ERR("PyString_FromString() has filed\n");
PyEval_ReleaseLock();
return -1;
}
PyList_Insert(sys_path, 0, pDir);
Py_DECREF(pDir);
// pModule = PyImport_ImportModule(bname);
pModule = PyImport_ImportModule(python_router.s);
if (pModule == NULL) {
LM_ERR("cannot import %s\n", python_router.s);
PyErr_Print();
PyEval_ReleaseLock();
return -1;
}
pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
Py_DECREF(pModule);
/* pFunc is a new reference */
if (pFunc == NULL || !PyCallable_Check(pFunc)) {
LM_ERR("cannot locate %s function in %s module\n",
mod_init_fname.s, script_name.s);
Py_XDECREF(pFunc);
PyEval_ReleaseLock();
return -1;
}
pModule = PyImport_ImportModule("traceback");
if (pModule == NULL) {
LM_ERR("cannot import traceback module\n");
Py_DECREF(pFunc);
PyEval_ReleaseLock();
return -1;
}
format_exc_obj = PyObject_GetAttrString(pModule, "format_exception");
Py_DECREF(pModule);
if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj)) {
LM_ERR("cannot locate format_exception function in" \
" traceback module\n");
Py_XDECREF(format_exc_obj);
Py_DECREF(pFunc);
PyEval_ReleaseLock();
return -1;
}
//pArgs = PyTuple_New(0);
pArgs = Py_BuildValue("(s)", script_name.s);
if (pArgs == NULL) {
LM_ERR("PyTuple_New() has failed\n");
Py_DECREF(pFunc);
Py_DECREF(format_exc_obj);
PyEval_ReleaseLock();
return -1;
}
handler_obj = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pFunc);
Py_DECREF(pArgs);
if (PyErr_Occurred()) {
python_handle_exception("mod_init");
Py_XDECREF(handler_obj);
Py_DECREF(format_exc_obj);
PyEval_ReleaseLock();
return -1;
}
if (handler_obj == NULL) {
LM_ERR("%s function has not returned object\n",
mod_init_fname.s);
Py_DECREF(format_exc_obj);
PyEval_ReleaseLock();
return -1;
}
myThreadState = PyThreadState_New(mainThreadState->interp);
PyEval_ReleaseLock();
return 0;
}
static int
child_init(int rank)
{
PyObject *pFunc, *pArgs, *pValue, *pResult;
int rval;
PyEval_AcquireLock();
PyThreadState_Swap(myThreadState);
pFunc = PyObject_GetAttrString(handler_obj, child_init_mname.s);
if (pFunc == NULL || !PyCallable_Check(pFunc)) {
LM_ERR("cannot locate %s function\n", child_init_mname.s);
if (pFunc != NULL) {
Py_DECREF(pFunc);
}
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
pArgs = PyTuple_New(1);
if (pArgs == NULL) {
LM_ERR("PyTuple_New() has failed\n");
Py_DECREF(pFunc);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
pValue = PyInt_FromLong(rank);
if (pValue == NULL) {
LM_ERR("PyInt_FromLong() has failed\n");
Py_DECREF(pArgs);
Py_DECREF(pFunc);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
PyTuple_SetItem(pArgs, 0, pValue);
/* pValue has been stolen */
pResult = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pFunc);
Py_DECREF(pArgs);
if (PyErr_Occurred()) {
python_handle_exception("child_init");
Py_XDECREF(pResult);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
if (pResult == NULL) {
LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
rval = PyInt_AsLong(pResult);
Py_DECREF(pResult);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return rval;
}
static void
mod_destroy(void)
{
return;
}