2
0

python_mod.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. #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 = {.s = "/usr/local/etc/sip-router/handler.py", .len = 0};
  37. static str mod_init_fname = { .s = "mod_init", .len = 0};
  38. static str child_init_mname = { .s = "child_init", .len = 0};
  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", STR_PARAM, &script_name },
  48. {"mod_init_function", STR_PARAM, &mod_init_fname },
  49. {"child_init_method", STR_PARAM, &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. 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_src = as_asciiz(&script_name);
  91. bname_src = as_asciiz(&script_name);
  92. if(dname_src==NULL || bname_src==NULL)
  93. {
  94. LM_ERR("no more pkg memory\n");
  95. return -1;
  96. }
  97. dname = strdup(dirname(dname_src));
  98. if (strlen(dname) == 0)
  99. dname = ".";
  100. bname = strdup(basename(bname_src));
  101. i = strlen(bname);
  102. if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
  103. i -= 1;
  104. if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
  105. bname[i - 3] = '\0';
  106. } else {
  107. LM_ERR("%s: script_name doesn't look like a python script\n",
  108. script_name.s);
  109. return -1;
  110. }
  111. Py_Initialize();
  112. PyEval_InitThreads();
  113. mainThreadState = PyThreadState_Get();
  114. format_exc_obj = InitTracebackModule();
  115. if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj))
  116. {
  117. Py_XDECREF(format_exc_obj);
  118. PyEval_ReleaseLock();
  119. return -1;
  120. }
  121. sys_path = PySys_GetObject("path");
  122. /* PySys_GetObject doesn't pass reference! No need to DEREF */
  123. if (sys_path == NULL) {
  124. if (!PyErr_Occurred())
  125. PyErr_Format(PyExc_AttributeError, "'module' object 'sys' has no attribute 'path'");
  126. python_handle_exception("mod_init");
  127. Py_DECREF(format_exc_obj);
  128. PyEval_ReleaseLock();
  129. return -1;
  130. }
  131. pDir = PyString_FromString(dname);
  132. if (pDir == NULL) {
  133. if (!PyErr_Occurred())
  134. PyErr_Format(PyExc_AttributeError, "PyString_FromString() has failed");
  135. python_handle_exception("mod_init");
  136. Py_DECREF(format_exc_obj);
  137. PyEval_ReleaseLock();
  138. return -1;
  139. }
  140. PyList_Insert(sys_path, 0, pDir);
  141. Py_DECREF(pDir);
  142. if (init_modules() != 0) {
  143. if (!PyErr_Occurred())
  144. PyErr_SetString(PyExc_AttributeError, "init_modules() has failed");
  145. python_handle_exception("mod_init");
  146. Py_DECREF(format_exc_obj);
  147. PyEval_ReleaseLock();
  148. return -1;
  149. }
  150. if (python_msgobj_init() != 0) {
  151. if (!PyErr_Occurred())
  152. PyErr_SetString(PyExc_AttributeError, "python_msgobj_init() has failed");
  153. python_handle_exception("mod_init");
  154. Py_DECREF(format_exc_obj);
  155. PyEval_ReleaseLock();
  156. return -1;
  157. }
  158. pModule = PyImport_ImportModule(bname);
  159. if (pModule == NULL) {
  160. if (!PyErr_Occurred())
  161. PyErr_Format(PyExc_ImportError, "No module named '%s'", bname);
  162. python_handle_exception("mod_init");
  163. Py_DECREF(format_exc_obj);
  164. PyEval_ReleaseLock();
  165. return -1;
  166. }
  167. pkg_free(dname_src);
  168. pkg_free(bname_src);
  169. pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
  170. Py_DECREF(pModule);
  171. /* pFunc is a new reference */
  172. if (pFunc == NULL) {
  173. if (!PyErr_Occurred())
  174. PyErr_Format(PyExc_AttributeError, "'module' object '%s' has no 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. if (!PyCallable_Check(pFunc)) {
  182. if (!PyErr_Occurred())
  183. PyErr_Format(PyExc_AttributeError, "module object '%s' has is not callable attribute '%s'", bname, mod_init_fname.s);
  184. python_handle_exception("mod_init");
  185. Py_DECREF(format_exc_obj);
  186. Py_XDECREF(pFunc);
  187. PyEval_ReleaseLock();
  188. return -1;
  189. }
  190. pArgs = PyTuple_New(0);
  191. if (pArgs == NULL) {
  192. python_handle_exception("mod_init");
  193. Py_DECREF(format_exc_obj);
  194. Py_DECREF(pFunc);
  195. PyEval_ReleaseLock();
  196. return -1;
  197. }
  198. handler_obj = PyObject_CallObject(pFunc, pArgs);
  199. Py_XDECREF(pFunc);
  200. Py_XDECREF(pArgs);
  201. if (handler_obj == Py_None) {
  202. if (!PyErr_Occurred())
  203. PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned None. Should be a class instance.", mod_init_fname.s, bname);
  204. python_handle_exception("mod_init");
  205. Py_DECREF(format_exc_obj);
  206. PyEval_ReleaseLock();
  207. return -1;
  208. }
  209. if (PyErr_Occurred()) {
  210. python_handle_exception("mod_init");
  211. Py_XDECREF(handler_obj);
  212. Py_DECREF(format_exc_obj);
  213. PyEval_ReleaseLock();
  214. return -1;
  215. }
  216. if (handler_obj == NULL) {
  217. LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
  218. if (!PyErr_Occurred())
  219. PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned not returned object. Should be a class instance.", mod_init_fname.s, bname);
  220. python_handle_exception("mod_init");
  221. Py_DECREF(format_exc_obj);
  222. PyEval_ReleaseLock();
  223. return -1;
  224. }
  225. myThreadState = PyThreadState_New(mainThreadState->interp);
  226. PyEval_ReleaseLock();
  227. return 0;
  228. }
  229. static int child_init(int rank)
  230. {
  231. PyObject *pFunc, *pArgs, *pValue, *pResult;
  232. int rval;
  233. char *classname;
  234. PyEval_AcquireLock();
  235. PyThreadState_Swap(myThreadState);
  236. // get instance class name
  237. classname = get_instance_class_name(handler_obj);
  238. if (classname == NULL)
  239. {
  240. if (!PyErr_Occurred())
  241. PyErr_Format(PyExc_AttributeError, "'module' instance has no class name");
  242. python_handle_exception("child_init");
  243. Py_DECREF(format_exc_obj);
  244. PyThreadState_Swap(NULL);
  245. PyEval_ReleaseLock();
  246. return -1;
  247. }
  248. pFunc = PyObject_GetAttrString(handler_obj, child_init_mname.s);
  249. if (pFunc == NULL) {
  250. python_handle_exception("child_init");
  251. Py_XDECREF(pFunc);
  252. Py_DECREF(format_exc_obj);
  253. PyThreadState_Swap(NULL);
  254. PyEval_ReleaseLock();
  255. return -1;
  256. }
  257. if (!PyCallable_Check(pFunc)) {
  258. if (!PyErr_Occurred())
  259. PyErr_Format(PyExc_AttributeError, "class object '%s' has is not callable attribute '%s'", !classname ? "None" : classname, mod_init_fname.s);
  260. python_handle_exception("child_init");
  261. Py_DECREF(format_exc_obj);
  262. Py_XDECREF(pFunc);
  263. PyThreadState_Swap(NULL);
  264. PyEval_ReleaseLock();
  265. return -1;
  266. }
  267. pArgs = PyTuple_New(1);
  268. if (pArgs == NULL) {
  269. python_handle_exception("child_init");
  270. Py_DECREF(format_exc_obj);
  271. Py_DECREF(pFunc);
  272. PyThreadState_Swap(NULL);
  273. PyEval_ReleaseLock();
  274. return -1;
  275. }
  276. pValue = PyInt_FromLong((long)rank);
  277. if (pValue == NULL) {
  278. python_handle_exception("child_init");
  279. Py_DECREF(format_exc_obj);
  280. Py_DECREF(pArgs);
  281. Py_DECREF(pFunc);
  282. PyThreadState_Swap(NULL);
  283. PyEval_ReleaseLock();
  284. return -1;
  285. }
  286. PyTuple_SetItem(pArgs, 0, pValue);
  287. /* pValue has been stolen */
  288. pResult = PyObject_CallObject(pFunc, pArgs);
  289. Py_DECREF(pFunc);
  290. Py_DECREF(pArgs);
  291. if (PyErr_Occurred()) {
  292. python_handle_exception("child_init");
  293. Py_DECREF(format_exc_obj);
  294. Py_XDECREF(pResult);
  295. PyThreadState_Swap(NULL);
  296. PyEval_ReleaseLock();
  297. return -1;
  298. }
  299. if (pResult == NULL) {
  300. LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
  301. PyThreadState_Swap(NULL);
  302. PyEval_ReleaseLock();
  303. return -1;
  304. }
  305. if (!PyInt_Check(pResult))
  306. {
  307. if (!PyErr_Occurred())
  308. PyErr_Format(PyExc_TypeError, "method '%s' of class '%s' should return 'int' type", child_init_mname.s, !classname ? "None" : classname);
  309. python_handle_exception("child_init");
  310. Py_DECREF(format_exc_obj);
  311. Py_XDECREF(pResult);
  312. PyThreadState_Swap(NULL);
  313. PyEval_ReleaseLock();
  314. return -1;
  315. }
  316. rval = PyInt_AsLong(pResult);
  317. Py_DECREF(pResult);
  318. PyThreadState_Swap(NULL);
  319. PyEval_ReleaseLock();
  320. return rval;
  321. }
  322. static void mod_destroy(void)
  323. {
  324. if (dname)
  325. free(dname); // dname was strdup'ed
  326. if (bname)
  327. free(bname); // bname was strdup'ed
  328. destroy_mod_Core();
  329. destroy_mod_Ranks();
  330. destroy_mod_Logger();
  331. destroy_mod_Router();
  332. }