python_mod.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. #include "python_mod.h"
  31. #include "mod_Router.h"
  32. #include "mod_Core.h"
  33. #include "mod_Ranks.h"
  34. #include "mod_Logger.h"
  35. MODULE_VERSION
  36. static str script_name = str_init("/usr/local/etc/sip-router/handler.py");
  37. static str mod_init_fname = str_init("mod_init");
  38. static str child_init_mname = str_init("child_init");
  39. static int mod_init(void);
  40. static int child_init(int rank);
  41. static void mod_destroy(void);
  42. PyObject *handler_obj;
  43. char *dname = NULL, *bname = NULL;
  44. PyThreadState *myThreadState;
  45. /** module parameters */
  46. static param_export_t params[]={
  47. {"script_name", PARAM_STR, &script_name },
  48. {"mod_init_function", PARAM_STR, &mod_init_fname },
  49. {"child_init_method", PARAM_STR, &child_init_mname },
  50. {0,0,0}
  51. };
  52. /*
  53. * Exported functions
  54. */
  55. static cmd_export_t cmds[] = {
  56. { "python_exec", (cmd_function)python_exec1, 1, NULL, 0, ANY_ROUTE },
  57. { "python_exec", (cmd_function)python_exec2, 2, NULL, 0, ANY_ROUTE },
  58. { 0, 0, 0, 0, 0, 0 }
  59. };
  60. /** module exports */
  61. struct module_exports exports = {
  62. "app_python", /* module name */
  63. RTLD_NOW | RTLD_GLOBAL, /* dlopen flags */
  64. cmds, /* exported functions */
  65. params, /* exported parameters */
  66. 0, /* exported statistics */
  67. 0, /* exported MI functions */
  68. 0, /* exported pseudo-variables */
  69. 0, /* extra processes */
  70. mod_init, /* module initialization function */
  71. (response_function) NULL, /* response handling function */
  72. (destroy_function) mod_destroy, /* destroy function */
  73. child_init /* per-child init function */
  74. };
  75. static int mod_init(void)
  76. {
  77. char *dname_src, *bname_src;
  78. int i;
  79. PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
  80. PyThreadState *mainThreadState;
  81. dname_src = as_asciiz(&script_name);
  82. bname_src = as_asciiz(&script_name);
  83. if(dname_src==NULL || bname_src==NULL)
  84. {
  85. LM_ERR("no more pkg memory\n");
  86. return -1;
  87. }
  88. dname = strdup(dirname(dname_src));
  89. if (strlen(dname) == 0)
  90. dname = ".";
  91. bname = strdup(basename(bname_src));
  92. i = strlen(bname);
  93. if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
  94. i -= 1;
  95. if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
  96. bname[i - 3] = '\0';
  97. } else {
  98. LM_ERR("%s: script_name doesn't look like a python script\n",
  99. script_name.s);
  100. return -1;
  101. }
  102. Py_Initialize();
  103. PyEval_InitThreads();
  104. mainThreadState = PyThreadState_Get();
  105. format_exc_obj = InitTracebackModule();
  106. if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj))
  107. {
  108. Py_XDECREF(format_exc_obj);
  109. PyEval_ReleaseLock();
  110. return -1;
  111. }
  112. sys_path = PySys_GetObject("path");
  113. /* PySys_GetObject doesn't pass reference! No need to DEREF */
  114. if (sys_path == NULL) {
  115. if (!PyErr_Occurred())
  116. PyErr_Format(PyExc_AttributeError, "'module' object 'sys' has no attribute 'path'");
  117. python_handle_exception("mod_init");
  118. Py_DECREF(format_exc_obj);
  119. PyEval_ReleaseLock();
  120. return -1;
  121. }
  122. pDir = PyString_FromString(dname);
  123. if (pDir == NULL) {
  124. if (!PyErr_Occurred())
  125. PyErr_Format(PyExc_AttributeError, "PyString_FromString() has failed");
  126. python_handle_exception("mod_init");
  127. Py_DECREF(format_exc_obj);
  128. PyEval_ReleaseLock();
  129. return -1;
  130. }
  131. PyList_Insert(sys_path, 0, pDir);
  132. Py_DECREF(pDir);
  133. if (init_modules() != 0) {
  134. if (!PyErr_Occurred())
  135. PyErr_SetString(PyExc_AttributeError, "init_modules() has failed");
  136. python_handle_exception("mod_init");
  137. Py_DECREF(format_exc_obj);
  138. PyEval_ReleaseLock();
  139. return -1;
  140. }
  141. if (python_msgobj_init() != 0) {
  142. if (!PyErr_Occurred())
  143. PyErr_SetString(PyExc_AttributeError, "python_msgobj_init() has failed");
  144. python_handle_exception("mod_init");
  145. Py_DECREF(format_exc_obj);
  146. PyEval_ReleaseLock();
  147. return -1;
  148. }
  149. pModule = PyImport_ImportModule(bname);
  150. if (pModule == NULL) {
  151. if (!PyErr_Occurred())
  152. PyErr_Format(PyExc_ImportError, "No module named '%s'", bname);
  153. python_handle_exception("mod_init");
  154. Py_DECREF(format_exc_obj);
  155. PyEval_ReleaseLock();
  156. return -1;
  157. }
  158. pkg_free(dname_src);
  159. pkg_free(bname_src);
  160. pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
  161. Py_DECREF(pModule);
  162. /* pFunc is a new reference */
  163. if (pFunc == NULL) {
  164. if (!PyErr_Occurred())
  165. PyErr_Format(PyExc_AttributeError, "'module' object '%s' has no attribute '%s'", bname, mod_init_fname.s);
  166. python_handle_exception("mod_init");
  167. Py_DECREF(format_exc_obj);
  168. Py_XDECREF(pFunc);
  169. PyEval_ReleaseLock();
  170. return -1;
  171. }
  172. if (!PyCallable_Check(pFunc)) {
  173. if (!PyErr_Occurred())
  174. PyErr_Format(PyExc_AttributeError, "module object '%s' has is not callable attribute '%s'", bname, mod_init_fname.s);
  175. python_handle_exception("mod_init");
  176. Py_DECREF(format_exc_obj);
  177. Py_XDECREF(pFunc);
  178. PyEval_ReleaseLock();
  179. return -1;
  180. }
  181. pArgs = PyTuple_New(0);
  182. if (pArgs == NULL) {
  183. python_handle_exception("mod_init");
  184. Py_DECREF(format_exc_obj);
  185. Py_DECREF(pFunc);
  186. PyEval_ReleaseLock();
  187. return -1;
  188. }
  189. handler_obj = PyObject_CallObject(pFunc, pArgs);
  190. Py_XDECREF(pFunc);
  191. Py_XDECREF(pArgs);
  192. if (handler_obj == Py_None) {
  193. if (!PyErr_Occurred())
  194. PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned None. Should be a class instance.", mod_init_fname.s, bname);
  195. python_handle_exception("mod_init");
  196. Py_DECREF(format_exc_obj);
  197. PyEval_ReleaseLock();
  198. return -1;
  199. }
  200. if (PyErr_Occurred()) {
  201. python_handle_exception("mod_init");
  202. Py_XDECREF(handler_obj);
  203. Py_DECREF(format_exc_obj);
  204. PyEval_ReleaseLock();
  205. return -1;
  206. }
  207. if (handler_obj == NULL) {
  208. LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
  209. if (!PyErr_Occurred())
  210. PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned not returned object. Should be a class instance.", mod_init_fname.s, bname);
  211. python_handle_exception("mod_init");
  212. Py_DECREF(format_exc_obj);
  213. PyEval_ReleaseLock();
  214. return -1;
  215. }
  216. myThreadState = PyThreadState_New(mainThreadState->interp);
  217. PyEval_ReleaseLock();
  218. return 0;
  219. }
  220. static int child_init(int rank)
  221. {
  222. PyObject *pFunc, *pArgs, *pValue, *pResult;
  223. int rval;
  224. char *classname;
  225. PyEval_AcquireLock();
  226. PyThreadState_Swap(myThreadState);
  227. // get instance class name
  228. classname = get_instance_class_name(handler_obj);
  229. if (classname == NULL)
  230. {
  231. if (!PyErr_Occurred())
  232. PyErr_Format(PyExc_AttributeError, "'module' instance has no class name");
  233. python_handle_exception("child_init");
  234. Py_DECREF(format_exc_obj);
  235. PyThreadState_Swap(NULL);
  236. PyEval_ReleaseLock();
  237. return -1;
  238. }
  239. pFunc = PyObject_GetAttrString(handler_obj, child_init_mname.s);
  240. if (pFunc == NULL) {
  241. python_handle_exception("child_init");
  242. Py_XDECREF(pFunc);
  243. Py_DECREF(format_exc_obj);
  244. PyThreadState_Swap(NULL);
  245. PyEval_ReleaseLock();
  246. return -1;
  247. }
  248. if (!PyCallable_Check(pFunc)) {
  249. if (!PyErr_Occurred())
  250. PyErr_Format(PyExc_AttributeError, "class object '%s' has is not callable attribute '%s'", !classname ? "None" : classname, mod_init_fname.s);
  251. python_handle_exception("child_init");
  252. Py_DECREF(format_exc_obj);
  253. Py_XDECREF(pFunc);
  254. PyThreadState_Swap(NULL);
  255. PyEval_ReleaseLock();
  256. return -1;
  257. }
  258. pArgs = PyTuple_New(1);
  259. if (pArgs == NULL) {
  260. python_handle_exception("child_init");
  261. Py_DECREF(format_exc_obj);
  262. Py_DECREF(pFunc);
  263. PyThreadState_Swap(NULL);
  264. PyEval_ReleaseLock();
  265. return -1;
  266. }
  267. pValue = PyInt_FromLong((long)rank);
  268. if (pValue == NULL) {
  269. python_handle_exception("child_init");
  270. Py_DECREF(format_exc_obj);
  271. Py_DECREF(pArgs);
  272. Py_DECREF(pFunc);
  273. PyThreadState_Swap(NULL);
  274. PyEval_ReleaseLock();
  275. return -1;
  276. }
  277. PyTuple_SetItem(pArgs, 0, pValue);
  278. /* pValue has been stolen */
  279. pResult = PyObject_CallObject(pFunc, pArgs);
  280. Py_DECREF(pFunc);
  281. Py_DECREF(pArgs);
  282. if (PyErr_Occurred()) {
  283. python_handle_exception("child_init");
  284. Py_DECREF(format_exc_obj);
  285. Py_XDECREF(pResult);
  286. PyThreadState_Swap(NULL);
  287. PyEval_ReleaseLock();
  288. return -1;
  289. }
  290. if (pResult == NULL) {
  291. LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
  292. PyThreadState_Swap(NULL);
  293. PyEval_ReleaseLock();
  294. return -1;
  295. }
  296. if (!PyInt_Check(pResult))
  297. {
  298. if (!PyErr_Occurred())
  299. PyErr_Format(PyExc_TypeError, "method '%s' of class '%s' should return 'int' type", child_init_mname.s, !classname ? "None" : classname);
  300. python_handle_exception("child_init");
  301. Py_DECREF(format_exc_obj);
  302. Py_XDECREF(pResult);
  303. PyThreadState_Swap(NULL);
  304. PyEval_ReleaseLock();
  305. return -1;
  306. }
  307. rval = PyInt_AsLong(pResult);
  308. Py_DECREF(pResult);
  309. PyThreadState_Swap(NULL);
  310. PyEval_ReleaseLock();
  311. return rval;
  312. }
  313. static void mod_destroy(void)
  314. {
  315. if (dname)
  316. free(dname); // dname was strdup'ed
  317. if (bname)
  318. free(bname); // bname was strdup'ed
  319. destroy_mod_Core();
  320. destroy_mod_Ranks();
  321. destroy_mod_Logger();
  322. destroy_mod_Router();
  323. }