|
@@ -30,18 +30,27 @@
|
|
#include "python_iface.h"
|
|
#include "python_iface.h"
|
|
#include "python_msgobj.h"
|
|
#include "python_msgobj.h"
|
|
#include "python_support.h"
|
|
#include "python_support.h"
|
|
|
|
+#include "python_mod.h"
|
|
|
|
+
|
|
|
|
+#include "mod_Router.h"
|
|
|
|
+#include "mod_Core.h"
|
|
|
|
+#include "mod_Ranks.h"
|
|
|
|
+#include "mod_Logger.h"
|
|
|
|
|
|
MODULE_VERSION
|
|
MODULE_VERSION
|
|
|
|
|
|
-static int mod_init(void);
|
|
|
|
-static int child_init(int rank);
|
|
|
|
-static void mod_destroy(void);
|
|
|
|
|
|
|
|
static str script_name = {.s = "/usr/local/etc/sip-router/handler.py", .len = 0};
|
|
static str script_name = {.s = "/usr/local/etc/sip-router/handler.py", .len = 0};
|
|
static str mod_init_fname = { .s = "mod_init", .len = 0};
|
|
static str mod_init_fname = { .s = "mod_init", .len = 0};
|
|
static str child_init_mname = { .s = "child_init", .len = 0};
|
|
static str child_init_mname = { .s = "child_init", .len = 0};
|
|
|
|
+
|
|
|
|
+static int mod_init(void);
|
|
|
|
+static int child_init(int rank);
|
|
|
|
+static void mod_destroy(void);
|
|
|
|
+
|
|
PyObject *handler_obj;
|
|
PyObject *handler_obj;
|
|
-PyObject *format_exc_obj;
|
|
|
|
|
|
+
|
|
|
|
+char *dname = NULL, *bname = NULL;
|
|
|
|
|
|
PyThreadState *myThreadState;
|
|
PyThreadState *myThreadState;
|
|
|
|
|
|
@@ -57,12 +66,8 @@ static param_export_t params[]={
|
|
* Exported functions
|
|
* Exported functions
|
|
*/
|
|
*/
|
|
static cmd_export_t cmds[] = {
|
|
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 },
|
|
|
|
|
|
+ { "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 }
|
|
{ 0, 0, 0, 0, 0, 0 }
|
|
};
|
|
};
|
|
|
|
|
|
@@ -84,9 +89,10 @@ struct module_exports exports = {
|
|
|
|
|
|
static int mod_init(void)
|
|
static int mod_init(void)
|
|
{
|
|
{
|
|
- char *dname, *bname, *dname_src, *bname_src;
|
|
|
|
|
|
+ char *dname_src, *bname_src;
|
|
|
|
+
|
|
int i;
|
|
int i;
|
|
- PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs, *m;
|
|
|
|
|
|
+ PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
|
|
PyThreadState *mainThreadState;
|
|
PyThreadState *mainThreadState;
|
|
|
|
|
|
if (script_name.len == 0) {
|
|
if (script_name.len == 0) {
|
|
@@ -101,16 +107,17 @@ static int mod_init(void)
|
|
|
|
|
|
dname_src = as_asciiz(&script_name);
|
|
dname_src = as_asciiz(&script_name);
|
|
bname_src = as_asciiz(&script_name);
|
|
bname_src = as_asciiz(&script_name);
|
|
|
|
+
|
|
if(dname_src==NULL || bname_src==NULL)
|
|
if(dname_src==NULL || bname_src==NULL)
|
|
{
|
|
{
|
|
- LM_ERR("no more pkg memory\n");
|
|
|
|
- return -1;
|
|
|
|
|
|
+ LM_ERR("no more pkg memory\n");
|
|
|
|
+ return -1;
|
|
}
|
|
}
|
|
|
|
|
|
- dname = dirname(dname_src);
|
|
|
|
|
|
+ dname = strdup(dirname(dname_src));
|
|
if (strlen(dname) == 0)
|
|
if (strlen(dname) == 0)
|
|
dname = ".";
|
|
dname = ".";
|
|
- bname = basename(bname_src);
|
|
|
|
|
|
+ bname = strdup(basename(bname_src));
|
|
i = strlen(bname);
|
|
i = strlen(bname);
|
|
if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
|
|
if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
|
|
i -= 1;
|
|
i -= 1;
|
|
@@ -126,36 +133,63 @@ static int mod_init(void)
|
|
PyEval_InitThreads();
|
|
PyEval_InitThreads();
|
|
mainThreadState = PyThreadState_Get();
|
|
mainThreadState = PyThreadState_Get();
|
|
|
|
|
|
- m = Py_InitModule("Router", RouterMethods);
|
|
|
|
|
|
+ format_exc_obj = InitTracebackModule();
|
|
|
|
|
|
- if (python_msgobj_init() != 0) {
|
|
|
|
- LM_ERR("python_msgobj_init() has failed\n");
|
|
|
|
|
|
+ if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj))
|
|
|
|
+ {
|
|
|
|
+ Py_XDECREF(format_exc_obj);
|
|
PyEval_ReleaseLock();
|
|
PyEval_ReleaseLock();
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
- RouterAddProperties(m);
|
|
|
|
-
|
|
|
|
sys_path = PySys_GetObject("path");
|
|
sys_path = PySys_GetObject("path");
|
|
/* PySys_GetObject doesn't pass reference! No need to DEREF */
|
|
/* PySys_GetObject doesn't pass reference! No need to DEREF */
|
|
if (sys_path == NULL) {
|
|
if (sys_path == NULL) {
|
|
- LM_ERR("cannot import sys.path\n");
|
|
|
|
|
|
+ if (!PyErr_Occurred())
|
|
|
|
+ PyErr_Format(PyExc_AttributeError, "'module' object 'sys' has no attribute 'path'");
|
|
|
|
+ python_handle_exception("mod_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
PyEval_ReleaseLock();
|
|
PyEval_ReleaseLock();
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
pDir = PyString_FromString(dname);
|
|
pDir = PyString_FromString(dname);
|
|
if (pDir == NULL) {
|
|
if (pDir == NULL) {
|
|
- LM_ERR("PyString_FromString() has filed\n");
|
|
|
|
|
|
+ if (!PyErr_Occurred())
|
|
|
|
+ PyErr_Format(PyExc_AttributeError, "PyString_FromString() has failed");
|
|
|
|
+ python_handle_exception("mod_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
PyEval_ReleaseLock();
|
|
PyEval_ReleaseLock();
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
+
|
|
PyList_Insert(sys_path, 0, pDir);
|
|
PyList_Insert(sys_path, 0, pDir);
|
|
Py_DECREF(pDir);
|
|
Py_DECREF(pDir);
|
|
|
|
|
|
|
|
+ if (init_modules() != 0) {
|
|
|
|
+ if (!PyErr_Occurred())
|
|
|
|
+ PyErr_SetString(PyExc_AttributeError, "init_modules() has failed");
|
|
|
|
+ python_handle_exception("mod_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
|
|
+ PyEval_ReleaseLock();
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (python_msgobj_init() != 0) {
|
|
|
|
+ if (!PyErr_Occurred())
|
|
|
|
+ PyErr_SetString(PyExc_AttributeError, "python_msgobj_init() has failed");
|
|
|
|
+ python_handle_exception("mod_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
|
|
+ PyEval_ReleaseLock();
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
pModule = PyImport_ImportModule(bname);
|
|
pModule = PyImport_ImportModule(bname);
|
|
if (pModule == NULL) {
|
|
if (pModule == NULL) {
|
|
- LM_ERR("cannot import %s\n", bname);
|
|
|
|
|
|
+ if (!PyErr_Occurred())
|
|
|
|
+ PyErr_Format(PyExc_ImportError, "No module named '%s'", bname);
|
|
|
|
+ python_handle_exception("mod_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
PyEval_ReleaseLock();
|
|
PyEval_ReleaseLock();
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
@@ -165,46 +199,52 @@ static int mod_init(void)
|
|
|
|
|
|
pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
|
|
pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
|
|
Py_DECREF(pModule);
|
|
Py_DECREF(pModule);
|
|
|
|
+
|
|
/* pFunc is a new reference */
|
|
/* 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);
|
|
|
|
|
|
+
|
|
|
|
+ if (pFunc == NULL) {
|
|
|
|
+ if (!PyErr_Occurred())
|
|
|
|
+ PyErr_Format(PyExc_AttributeError, "'module' object '%s' has no attribute '%s'", bname, mod_init_fname.s);
|
|
|
|
+ python_handle_exception("mod_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
Py_XDECREF(pFunc);
|
|
Py_XDECREF(pFunc);
|
|
PyEval_ReleaseLock();
|
|
PyEval_ReleaseLock();
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
- pModule = PyImport_ImportModule("traceback");
|
|
|
|
- if (pModule == NULL) {
|
|
|
|
- LM_ERR("cannot import traceback module\n");
|
|
|
|
- Py_DECREF(pFunc);
|
|
|
|
|
|
+ if (!PyCallable_Check(pFunc)) {
|
|
|
|
+ if (!PyErr_Occurred())
|
|
|
|
+ PyErr_Format(PyExc_AttributeError, "module object '%s' has is not callable attribute '%s'", bname, mod_init_fname.s);
|
|
|
|
+ python_handle_exception("mod_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
|
|
+ Py_XDECREF(pFunc);
|
|
PyEval_ReleaseLock();
|
|
PyEval_ReleaseLock();
|
|
return -1;
|
|
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 = PyTuple_New(0);
|
|
if (pArgs == NULL) {
|
|
if (pArgs == NULL) {
|
|
- LM_ERR("PyTuple_New() has failed\n");
|
|
|
|
- Py_DECREF(pFunc);
|
|
|
|
|
|
+ python_handle_exception("mod_init");
|
|
Py_DECREF(format_exc_obj);
|
|
Py_DECREF(format_exc_obj);
|
|
|
|
+ Py_DECREF(pFunc);
|
|
PyEval_ReleaseLock();
|
|
PyEval_ReleaseLock();
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
handler_obj = PyObject_CallObject(pFunc, pArgs);
|
|
handler_obj = PyObject_CallObject(pFunc, pArgs);
|
|
- Py_DECREF(pFunc);
|
|
|
|
- Py_DECREF(pArgs);
|
|
|
|
|
|
+
|
|
|
|
+ Py_XDECREF(pFunc);
|
|
|
|
+ Py_XDECREF(pArgs);
|
|
|
|
+
|
|
|
|
+ if (handler_obj == Py_None) {
|
|
|
|
+ if (!PyErr_Occurred())
|
|
|
|
+ PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned None. Should be a class instance.", mod_init_fname.s, bname);
|
|
|
|
+ python_handle_exception("mod_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
|
|
+ PyEval_ReleaseLock();
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
|
|
if (PyErr_Occurred()) {
|
|
if (PyErr_Occurred()) {
|
|
python_handle_exception("mod_init");
|
|
python_handle_exception("mod_init");
|
|
@@ -215,8 +255,10 @@ static int mod_init(void)
|
|
}
|
|
}
|
|
|
|
|
|
if (handler_obj == NULL) {
|
|
if (handler_obj == NULL) {
|
|
- LM_ERR("%s function has not returned object\n",
|
|
|
|
- mod_init_fname.s);
|
|
|
|
|
|
+ LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
|
|
|
|
+ if (!PyErr_Occurred())
|
|
|
|
+ PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned not returned object. Should be a class instance.", mod_init_fname.s, bname);
|
|
|
|
+ python_handle_exception("mod_init");
|
|
Py_DECREF(format_exc_obj);
|
|
Py_DECREF(format_exc_obj);
|
|
PyEval_ReleaseLock();
|
|
PyEval_ReleaseLock();
|
|
return -1;
|
|
return -1;
|
|
@@ -232,16 +274,43 @@ static int child_init(int rank)
|
|
{
|
|
{
|
|
PyObject *pFunc, *pArgs, *pValue, *pResult;
|
|
PyObject *pFunc, *pArgs, *pValue, *pResult;
|
|
int rval;
|
|
int rval;
|
|
|
|
+ char *classname;
|
|
|
|
|
|
PyEval_AcquireLock();
|
|
PyEval_AcquireLock();
|
|
PyThreadState_Swap(myThreadState);
|
|
PyThreadState_Swap(myThreadState);
|
|
|
|
|
|
|
|
+ // get instance class name
|
|
|
|
+ classname = get_instance_class_name(handler_obj);
|
|
|
|
+ if (classname == NULL)
|
|
|
|
+ {
|
|
|
|
+ if (!PyErr_Occurred())
|
|
|
|
+ PyErr_Format(PyExc_AttributeError, "'module' instance has no class name");
|
|
|
|
+ python_handle_exception("child_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
|
|
+ Py_XDECREF(classname);
|
|
|
|
+ PyThreadState_Swap(NULL);
|
|
|
|
+ PyEval_ReleaseLock();
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
pFunc = PyObject_GetAttrString(handler_obj, child_init_mname.s);
|
|
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);
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ if (pFunc == NULL) {
|
|
|
|
+ python_handle_exception("child_init");
|
|
|
|
+ Py_XDECREF(pFunc);
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
|
|
+ PyThreadState_Swap(NULL);
|
|
|
|
+ PyEval_ReleaseLock();
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!PyCallable_Check(pFunc)) {
|
|
|
|
+ if (!PyErr_Occurred())
|
|
|
|
+ PyErr_Format(PyExc_AttributeError, "class object '%s' has is not callable attribute '%s'", !classname ? "None" : classname, mod_init_fname.s);
|
|
|
|
+ python_handle_exception("child_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
|
|
+ Py_XDECREF(classname);
|
|
|
|
+ Py_XDECREF(pFunc);
|
|
PyThreadState_Swap(NULL);
|
|
PyThreadState_Swap(NULL);
|
|
PyEval_ReleaseLock();
|
|
PyEval_ReleaseLock();
|
|
return -1;
|
|
return -1;
|
|
@@ -249,16 +318,19 @@ static int child_init(int rank)
|
|
|
|
|
|
pArgs = PyTuple_New(1);
|
|
pArgs = PyTuple_New(1);
|
|
if (pArgs == NULL) {
|
|
if (pArgs == NULL) {
|
|
- LM_ERR("PyTuple_New() has failed\n");
|
|
|
|
|
|
+ python_handle_exception("child_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
|
|
+ Py_XDECREF(classname);
|
|
Py_DECREF(pFunc);
|
|
Py_DECREF(pFunc);
|
|
PyThreadState_Swap(NULL);
|
|
PyThreadState_Swap(NULL);
|
|
PyEval_ReleaseLock();
|
|
PyEval_ReleaseLock();
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
- pValue = PyInt_FromLong(rank);
|
|
|
|
|
|
+ pValue = PyInt_FromLong((long)rank);
|
|
if (pValue == NULL) {
|
|
if (pValue == NULL) {
|
|
- LM_ERR("PyInt_FromLong() has failed\n");
|
|
|
|
|
|
+ python_handle_exception("child_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
Py_DECREF(pArgs);
|
|
Py_DECREF(pArgs);
|
|
Py_DECREF(pFunc);
|
|
Py_DECREF(pFunc);
|
|
PyThreadState_Swap(NULL);
|
|
PyThreadState_Swap(NULL);
|
|
@@ -272,8 +344,12 @@ static int child_init(int rank)
|
|
Py_DECREF(pFunc);
|
|
Py_DECREF(pFunc);
|
|
Py_DECREF(pArgs);
|
|
Py_DECREF(pArgs);
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
if (PyErr_Occurred()) {
|
|
if (PyErr_Occurred()) {
|
|
python_handle_exception("child_init");
|
|
python_handle_exception("child_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
Py_XDECREF(pResult);
|
|
Py_XDECREF(pResult);
|
|
PyThreadState_Swap(NULL);
|
|
PyThreadState_Swap(NULL);
|
|
PyEval_ReleaseLock();
|
|
PyEval_ReleaseLock();
|
|
@@ -287,14 +363,38 @@ static int child_init(int rank)
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ if (!PyInt_Check(pResult))
|
|
|
|
+ {
|
|
|
|
+ if (!PyErr_Occurred())
|
|
|
|
+ PyErr_Format(PyExc_TypeError, "method '%s' of class '%s' should return 'int' type", child_init_mname.s, !classname ? "None" : classname);
|
|
|
|
+ python_handle_exception("child_init");
|
|
|
|
+ Py_DECREF(format_exc_obj);
|
|
|
|
+ Py_XDECREF(pResult);
|
|
|
|
+ Py_XDECREF(classname);
|
|
|
|
+ PyThreadState_Swap(NULL);
|
|
|
|
+ PyEval_ReleaseLock();
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
rval = PyInt_AsLong(pResult);
|
|
rval = PyInt_AsLong(pResult);
|
|
Py_DECREF(pResult);
|
|
Py_DECREF(pResult);
|
|
|
|
+ Py_XDECREF(classname);
|
|
PyThreadState_Swap(NULL);
|
|
PyThreadState_Swap(NULL);
|
|
PyEval_ReleaseLock();
|
|
PyEval_ReleaseLock();
|
|
|
|
+
|
|
return rval;
|
|
return rval;
|
|
}
|
|
}
|
|
|
|
|
|
static void mod_destroy(void)
|
|
static void mod_destroy(void)
|
|
{
|
|
{
|
|
- return;
|
|
|
|
|
|
+ if (dname)
|
|
|
|
+ free(dname); // dname was strdup'ed
|
|
|
|
+ if (bname)
|
|
|
|
+ free(bname); // bname was strdup'ed
|
|
|
|
+
|
|
|
|
+ destroy_mod_Core();
|
|
|
|
+ destroy_mod_Ranks();
|
|
|
|
+ destroy_mod_Logger();
|
|
|
|
+ destroy_mod_Router();
|
|
}
|
|
}
|