python_mod.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "../../str.h"
  23. #include "../../sr_module.h"
  24. #include "python_exec.h"
  25. #include "python_iface.h"
  26. #include "python_msgobj.h"
  27. #include "python_support.h"
  28. #include <Python.h>
  29. #include <libgen.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
  75. mod_init(void)
  76. {
  77. char *dname, *bname;
  78. int i;
  79. PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
  80. PyThreadState *mainThreadState;
  81. if (script_name.len == 0) {
  82. script_name.len = strlen(script_name.s);
  83. }
  84. if (mod_init_fname.len == 0) {
  85. mod_init_fname.len = strlen(mod_init_fname.s);
  86. }
  87. if (child_init_mname.len == 0) {
  88. child_init_mname.len = strlen(child_init_mname.s);
  89. }
  90. dname = dirname(script_name.s);
  91. if (strlen(dname) == 0)
  92. dname = ".";
  93. bname = basename(script_name.s);
  94. i = strlen(bname);
  95. if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
  96. i -= 1;
  97. if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
  98. bname[i - 3] = '\0';
  99. } else {
  100. LM_ERR("%s: script_name doesn't look like a python script\n",
  101. script_name.s);
  102. return -1;
  103. }
  104. Py_Initialize();
  105. PyEval_InitThreads();
  106. mainThreadState = PyThreadState_Get();
  107. Py_InitModule("Router", RouterMethods);
  108. if (python_msgobj_init() != 0) {
  109. LM_ERR("python_msgobj_init() has failed\n");
  110. PyEval_ReleaseLock();
  111. return -1;
  112. }
  113. sys_path = PySys_GetObject("path");
  114. /* PySys_GetObject doesn't pass reference! No need to DEREF */
  115. if (sys_path == NULL) {
  116. LM_ERR("cannot import sys.path\n");
  117. PyEval_ReleaseLock();
  118. return -1;
  119. }
  120. pDir = PyString_FromString(dname);
  121. if (pDir == NULL) {
  122. LM_ERR("PyString_FromString() has filed\n");
  123. PyEval_ReleaseLock();
  124. return -1;
  125. }
  126. PyList_Insert(sys_path, 0, pDir);
  127. Py_DECREF(pDir);
  128. pModule = PyImport_ImportModule(bname);
  129. if (pModule == NULL) {
  130. LM_ERR("cannot import %s\n", bname);
  131. PyEval_ReleaseLock();
  132. return -1;
  133. }
  134. pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
  135. Py_DECREF(pModule);
  136. /* pFunc is a new reference */
  137. if (pFunc == NULL || !PyCallable_Check(pFunc)) {
  138. LM_ERR("cannot locate %s function in %s module\n",
  139. mod_init_fname.s, script_name.s);
  140. Py_XDECREF(pFunc);
  141. PyEval_ReleaseLock();
  142. return -1;
  143. }
  144. pModule = PyImport_ImportModule("traceback");
  145. if (pModule == NULL) {
  146. LM_ERR("cannot import traceback module\n");
  147. Py_DECREF(pFunc);
  148. PyEval_ReleaseLock();
  149. return -1;
  150. }
  151. format_exc_obj = PyObject_GetAttrString(pModule, "format_exception");
  152. Py_DECREF(pModule);
  153. if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj)) {
  154. LM_ERR("cannot locate format_exception function in" \
  155. " traceback module\n");
  156. Py_XDECREF(format_exc_obj);
  157. Py_DECREF(pFunc);
  158. PyEval_ReleaseLock();
  159. return -1;
  160. }
  161. pArgs = PyTuple_New(0);
  162. if (pArgs == NULL) {
  163. LM_ERR("PyTuple_New() has failed\n");
  164. Py_DECREF(pFunc);
  165. Py_DECREF(format_exc_obj);
  166. PyEval_ReleaseLock();
  167. return -1;
  168. }
  169. handler_obj = PyObject_CallObject(pFunc, pArgs);
  170. Py_DECREF(pFunc);
  171. Py_DECREF(pArgs);
  172. if (PyErr_Occurred()) {
  173. python_handle_exception("mod_init");
  174. Py_XDECREF(handler_obj);
  175. Py_DECREF(format_exc_obj);
  176. PyEval_ReleaseLock();
  177. return -1;
  178. }
  179. if (handler_obj == NULL) {
  180. LM_ERR("%s function has not returned object\n",
  181. mod_init_fname.s);
  182. Py_DECREF(format_exc_obj);
  183. PyEval_ReleaseLock();
  184. return -1;
  185. }
  186. myThreadState = PyThreadState_New(mainThreadState->interp);
  187. PyEval_ReleaseLock();
  188. return 0;
  189. }
  190. static int
  191. child_init(int rank)
  192. {
  193. PyObject *pFunc, *pArgs, *pValue, *pResult;
  194. int rval;
  195. PyEval_AcquireLock();
  196. PyThreadState_Swap(myThreadState);
  197. pFunc = PyObject_GetAttrString(handler_obj, child_init_mname.s);
  198. if (pFunc == NULL || !PyCallable_Check(pFunc)) {
  199. LM_ERR("cannot locate %s function\n", child_init_mname.s);
  200. if (pFunc != NULL) {
  201. Py_DECREF(pFunc);
  202. }
  203. PyThreadState_Swap(NULL);
  204. PyEval_ReleaseLock();
  205. return -1;
  206. }
  207. pArgs = PyTuple_New(1);
  208. if (pArgs == NULL) {
  209. LM_ERR("PyTuple_New() has failed\n");
  210. Py_DECREF(pFunc);
  211. PyThreadState_Swap(NULL);
  212. PyEval_ReleaseLock();
  213. return -1;
  214. }
  215. pValue = PyInt_FromLong(rank);
  216. if (pValue == NULL) {
  217. LM_ERR("PyInt_FromLong() has failed\n");
  218. Py_DECREF(pArgs);
  219. Py_DECREF(pFunc);
  220. PyThreadState_Swap(NULL);
  221. PyEval_ReleaseLock();
  222. return -1;
  223. }
  224. PyTuple_SetItem(pArgs, 0, pValue);
  225. /* pValue has been stolen */
  226. pResult = PyObject_CallObject(pFunc, pArgs);
  227. Py_DECREF(pFunc);
  228. Py_DECREF(pArgs);
  229. if (PyErr_Occurred()) {
  230. python_handle_exception("child_init");
  231. Py_XDECREF(pResult);
  232. PyThreadState_Swap(NULL);
  233. PyEval_ReleaseLock();
  234. return -1;
  235. }
  236. if (pResult == NULL) {
  237. LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
  238. PyThreadState_Swap(NULL);
  239. PyEval_ReleaseLock();
  240. return -1;
  241. }
  242. rval = PyInt_AsLong(pResult);
  243. Py_DECREF(pResult);
  244. PyThreadState_Swap(NULL);
  245. PyEval_ReleaseLock();
  246. return rval;
  247. }
  248. static void
  249. mod_destroy(void)
  250. {
  251. return;
  252. }