-
Notifications
You must be signed in to change notification settings - Fork 908
/
python_exec.c
211 lines (193 loc) · 4.73 KB
/
python_exec.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
/*
* Copyright (C) 2009 Sippy Software, Inc., http://www.sippysoft.com
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio 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
*
* Kamailio 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
*
*/
#include <Python.h>
#include "../../core/mem/mem.h"
#include "../../core/data_lump.h"
#include "../../core/parser/parse_param.h"
#include "../../core/parser/msg_parser.h"
#include "../../core/dprint.h"
#include "../../core/action.h"
#include "../../core/config.h"
#include "../../core/mod_fix.h"
#include "../../core/parser/parse_uri.h"
#include "python_exec.h"
#include "app_python_mod.h"
#include "python_msgobj.h"
#include "python_support.h"
static sr_apy_env_t _sr_apy_env = {0};
/**
*
*/
sr_apy_env_t *sr_apy_env_get()
{
return &_sr_apy_env;
}
static int _sr_apy_exec_pid = 0;
/**
*
*/
int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode)
{
PyObject *pFunc, *pArgs, *pValue, *pResult;
PyObject *pmsg;
int rval;
sip_msg_t *bmsg;
int mpid;
int locked = 0;
bmsg = _sr_apy_env.msg;
_sr_apy_env.msg = _msg;
mpid = getpid();
if(_sr_apy_exec_pid!=mpid) {
PyEval_AcquireLock();
_sr_apy_exec_pid = mpid;
PyThreadState_Swap(myThreadState);
locked = 1;
}
pFunc = PyObject_GetAttrString(_sr_apy_handler_obj, fname);
if (pFunc == NULL || !PyCallable_Check(pFunc)) {
if(emode==1) {
LM_ERR("%s not found or is not callable\n", fname);
} else {
LM_DBG("%s not found or is not callable\n", fname);
}
Py_XDECREF(pFunc);
if(locked) {
_sr_apy_exec_pid = 0;
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
}
_sr_apy_env.msg = bmsg;
if(emode==1) {
return -1;
} else {
return 1;
}
}
pmsg = newmsgobject(_msg);
if (pmsg == NULL) {
LM_ERR("can't create MSGtype instance\n");
Py_DECREF(pFunc);
if(locked) {
_sr_apy_exec_pid = 0;
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
}
_sr_apy_env.msg = bmsg;
return -1;
}
pArgs = PyTuple_New(fparam == NULL ? 1 : 2);
if (pArgs == NULL) {
LM_ERR("PyTuple_New() has failed\n");
msg_invalidate(pmsg);
Py_DECREF(pmsg);
Py_DECREF(pFunc);
if(locked) {
_sr_apy_exec_pid = 0;
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
}
_sr_apy_env.msg = bmsg;
return -1;
}
PyTuple_SetItem(pArgs, 0, pmsg);
/* Tuple steals pmsg */
if (fparam != NULL) {
pValue = PyString_FromString(fparam);
if (pValue == NULL) {
LM_ERR("PyString_FromString(%s) has failed\n", fparam);
msg_invalidate(pmsg);
Py_DECREF(pArgs);
Py_DECREF(pFunc);
if(locked) {
_sr_apy_exec_pid = 0;
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
}
_sr_apy_env.msg = bmsg;
return -1;
}
PyTuple_SetItem(pArgs, 1, pValue);
/* Tuple steals pValue */
}
pResult = PyObject_CallObject(pFunc, pArgs);
msg_invalidate(pmsg);
Py_DECREF(pArgs);
Py_DECREF(pFunc);
if (PyErr_Occurred()) {
Py_XDECREF(pResult);
python_handle_exception("python_exec2");
if(locked) {
_sr_apy_exec_pid = 0;
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
}
_sr_apy_env.msg = bmsg;
return -1;
}
if (pResult == NULL) {
LM_ERR("PyObject_CallObject() returned NULL\n");
if(locked) {
_sr_apy_exec_pid = 0;
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
}
_sr_apy_env.msg = bmsg;
return -1;
}
rval = PyInt_AsLong(pResult);
Py_DECREF(pResult);
if(locked) {
_sr_apy_exec_pid = 0;
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
}
_sr_apy_env.msg = bmsg;
return rval;
}
/**
*
*/
int python_exec1(sip_msg_t *_msg, char *method_name, char *foobar)
{
str method = STR_NULL;
if(fixup_get_svalue(_msg, (gparam_t*)method_name, &method)<0) {
LM_ERR("cannot get the python method to be executed\n");
return -1;
}
return apy_exec(_msg, method.s, NULL, 1);
}
/**
*
*/
int python_exec2(sip_msg_t *_msg, char *method_name, char *mystr)
{
str method = STR_NULL;
str param = STR_NULL;
if(fixup_get_svalue(_msg, (gparam_t*)method_name, &method)<0) {
LM_ERR("cannot get the python method to be executed\n");
return -1;
}
if(fixup_get_svalue(_msg, (gparam_t*)mystr, ¶m)<0) {
LM_ERR("cannot get the parameter of the python method\n");
return -1;
}
return apy_exec(_msg, method.s, param.s, 1);
}