python_mod.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* $Id$
  2. *
  3. * Copyright (C) 2009 Sippy Software, Inc., http://www.sippysoft.com
  4. *
  5. * This file is part of SIP-Router, a free SIP server.
  6. *
  7. * SIP-Router is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * SIP-Router is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <Python.h>
  23. #include <libgen.h>
  24. #include "../../str.h"
  25. #include "../../sr_module.h"
  26. #include "python_exec.h"
  27. #include "python_iface.h"
  28. #include "python_msgobj.h"
  29. #include "python_support.h"
  30. MODULE_VERSION
  31. static int mod_init(void);
  32. static int child_init(int rank);
  33. static void mod_destroy(void);
  34. static str script_name = {.s = "/usr/local/etc/sip-router/handler.py", .len = 0};
  35. static str mod_init_fname = { .s = "mod_init", .len = 0};
  36. static str child_init_mname = { .s = "child_init", .len = 0};
  37. PyObject *handler_obj;
  38. PyObject *format_exc_obj;
  39. PyThreadState *myThreadState;
  40. /** module parameters */
  41. static param_export_t params[]={
  42. {"script_name", STR_PARAM, &script_name },
  43. {"mod_init_function", STR_PARAM, &mod_init_fname },
  44. {"child_init_method", STR_PARAM, &child_init_mname },
  45. {0,0,0}
  46. };
  47. /*
  48. * Exported functions
  49. */
  50. static cmd_export_t cmds[] = {
  51. { "python_exec", (cmd_function)python_exec1, 1, NULL, 0,
  52. REQUEST_ROUTE | FAILURE_ROUTE
  53. | ONREPLY_ROUTE | BRANCH_ROUTE },
  54. { "python_exec", (cmd_function)python_exec2, 2, NULL, 0,
  55. REQUEST_ROUTE | FAILURE_ROUTE
  56. | ONREPLY_ROUTE | BRANCH_ROUTE },
  57. { 0, 0, 0, 0, 0, 0 }
  58. };
  59. /** module exports */
  60. struct module_exports exports = {
  61. "app_python", /* module name */
  62. RTLD_NOW | RTLD_GLOBAL, /* dlopen flags */
  63. cmds, /* exported functions */
  64. params, /* exported parameters */
  65. 0, /* exported statistics */
  66. 0, /* exported MI functions */
  67. 0, /* exported pseudo-variables */
  68. 0, /* extra processes */
  69. mod_init, /* module initialization function */
  70. (response_function) NULL, /* response handling function */
  71. (destroy_function) mod_destroy, /* destroy function */
  72. child_init /* per-child init function */
  73. };
  74. static int mod_init(void)
  75. {
  76. char *dname, *bname, *dname_src, *bname_src;
  77. int i;
  78. PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs, *m;
  79. PyThreadState *mainThreadState;
  80. if (script_name.len == 0) {
  81. script_name.len = strlen(script_name.s);
  82. }
  83. if (mod_init_fname.len == 0) {
  84. mod_init_fname.len = strlen(mod_init_fname.s);
  85. }
  86. if (child_init_mname.len == 0) {
  87. child_init_mname.len = strlen(child_init_mname.s);
  88. }
  89. dname_src = as_asciiz(&script_name);
  90. bname_src = as_asciiz(&script_name);
  91. if(dname_src==NULL || bname_src==NULL)
  92. {
  93. LM_ERR("no more pkg memory\n");
  94. return -1;
  95. }
  96. dname = dirname(dname_src);
  97. if (strlen(dname) == 0)
  98. dname = ".";
  99. bname = basename(bname_src);
  100. i = strlen(bname);
  101. if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
  102. i -= 1;
  103. if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
  104. bname[i - 3] = '\0';
  105. } else {
  106. LM_ERR("%s: script_name doesn't look like a python script\n",
  107. script_name.s);
  108. return -1;
  109. }
  110. Py_Initialize();
  111. PyEval_InitThreads();
  112. mainThreadState = PyThreadState_Get();
  113. m = Py_InitModule("Router", RouterMethods);
  114. if (python_msgobj_init() != 0) {
  115. LM_ERR("python_msgobj_init() has failed\n");
  116. PyEval_ReleaseLock();
  117. return -1;
  118. }
  119. RouterAddProperties(m);
  120. sys_path = PySys_GetObject("path");
  121. /* PySys_GetObject doesn't pass reference! No need to DEREF */
  122. if (sys_path == NULL) {
  123. LM_ERR("cannot import sys.path\n");
  124. PyEval_ReleaseLock();
  125. return -1;
  126. }
  127. pDir = PyString_FromString(dname);
  128. if (pDir == NULL) {
  129. LM_ERR("PyString_FromString() has filed\n");
  130. PyEval_ReleaseLock();
  131. return -1;
  132. }
  133. PyList_Insert(sys_path, 0, pDir);
  134. Py_DECREF(pDir);
  135. pModule = PyImport_ImportModule(bname);
  136. if (pModule == NULL) {
  137. LM_ERR("cannot import %s\n", bname);
  138. PyEval_ReleaseLock();
  139. return -1;
  140. }
  141. pkg_free(dname_src);
  142. pkg_free(bname_src);
  143. pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
  144. Py_DECREF(pModule);
  145. /* pFunc is a new reference */
  146. if (pFunc == NULL || !PyCallable_Check(pFunc)) {
  147. LM_ERR("cannot locate %s function in %s module\n",
  148. mod_init_fname.s, script_name.s);
  149. Py_XDECREF(pFunc);
  150. PyEval_ReleaseLock();
  151. return -1;
  152. }
  153. pModule = PyImport_ImportModule("traceback");
  154. if (pModule == NULL) {
  155. LM_ERR("cannot import traceback module\n");
  156. Py_DECREF(pFunc);
  157. PyEval_ReleaseLock();
  158. return -1;
  159. }
  160. format_exc_obj = PyObject_GetAttrString(pModule, "format_exception");
  161. Py_DECREF(pModule);
  162. if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj)) {
  163. LM_ERR("cannot locate format_exception function in" \
  164. " traceback module\n");
  165. Py_XDECREF(format_exc_obj);
  166. Py_DECREF(pFunc);
  167. PyEval_ReleaseLock();
  168. return -1;
  169. }
  170. pArgs = PyTuple_New(0);
  171. if (pArgs == NULL) {
  172. LM_ERR("PyTuple_New() has failed\n");
  173. Py_DECREF(pFunc);
  174. Py_DECREF(format_exc_obj);
  175. PyEval_ReleaseLock();
  176. return -1;
  177. }
  178. handler_obj = PyObject_CallObject(pFunc, pArgs);
  179. Py_DECREF(pFunc);
  180. Py_DECREF(pArgs);
  181. if (PyErr_Occurred()) {
  182. python_handle_exception("mod_init");
  183. Py_XDECREF(handler_obj);
  184. Py_DECREF(format_exc_obj);
  185. PyEval_ReleaseLock();
  186. return -1;
  187. }
  188. if (handler_obj == NULL) {
  189. LM_ERR("%s function has not returned object\n",
  190. mod_init_fname.s);
  191. Py_DECREF(format_exc_obj);
  192. PyEval_ReleaseLock();
  193. return -1;
  194. }
  195. myThreadState = PyThreadState_New(mainThreadState->interp);
  196. PyEval_ReleaseLock();
  197. return 0;
  198. }
  199. static int child_init(int rank)
  200. {
  201. PyObject *pFunc, *pArgs, *pValue, *pResult;
  202. int rval;
  203. PyEval_AcquireLock();
  204. PyThreadState_Swap(myThreadState);
  205. pFunc = PyObject_GetAttrString(handler_obj, child_init_mname.s);
  206. if (pFunc == NULL || !PyCallable_Check(pFunc)) {
  207. LM_ERR("cannot locate %s function\n", child_init_mname.s);
  208. if (pFunc != NULL) {
  209. Py_DECREF(pFunc);
  210. }
  211. PyThreadState_Swap(NULL);
  212. PyEval_ReleaseLock();
  213. return -1;
  214. }
  215. pArgs = PyTuple_New(1);
  216. if (pArgs == NULL) {
  217. LM_ERR("PyTuple_New() has failed\n");
  218. Py_DECREF(pFunc);
  219. PyThreadState_Swap(NULL);
  220. PyEval_ReleaseLock();
  221. return -1;
  222. }
  223. pValue = PyInt_FromLong(rank);
  224. if (pValue == NULL) {
  225. LM_ERR("PyInt_FromLong() has failed\n");
  226. Py_DECREF(pArgs);
  227. Py_DECREF(pFunc);
  228. PyThreadState_Swap(NULL);
  229. PyEval_ReleaseLock();
  230. return -1;
  231. }
  232. PyTuple_SetItem(pArgs, 0, pValue);
  233. /* pValue has been stolen */
  234. pResult = PyObject_CallObject(pFunc, pArgs);
  235. Py_DECREF(pFunc);
  236. Py_DECREF(pArgs);
  237. if (PyErr_Occurred()) {
  238. python_handle_exception("child_init");
  239. Py_XDECREF(pResult);
  240. PyThreadState_Swap(NULL);
  241. PyEval_ReleaseLock();
  242. return -1;
  243. }
  244. if (pResult == NULL) {
  245. LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
  246. PyThreadState_Swap(NULL);
  247. PyEval_ReleaseLock();
  248. return -1;
  249. }
  250. rval = PyInt_AsLong(pResult);
  251. Py_DECREF(pResult);
  252. PyThreadState_Swap(NULL);
  253. PyEval_ReleaseLock();
  254. return rval;
  255. }
  256. static void mod_destroy(void)
  257. {
  258. return;
  259. }