python_mod.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright (C) 2009 Sippy Software, Inc., http://www.sippysoft.com
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. #include <Python.h>
  22. #include <libgen.h>
  23. #include "../../str.h"
  24. #include "../../sr_module.h"
  25. #include "python_exec.h"
  26. #include "python_iface.h"
  27. #include "python_msgobj.h"
  28. #include "python_support.h"
  29. #include "python_mod.h"
  30. #include "mod_Router.h"
  31. #include "mod_Core.h"
  32. #include "mod_Ranks.h"
  33. #include "mod_Logger.h"
  34. MODULE_VERSION
  35. static str script_name = str_init("/usr/local/etc/sip-router/handler.py");
  36. static str mod_init_fname = str_init("mod_init");
  37. static str child_init_mname = str_init("child_init");
  38. static int mod_init(void);
  39. static int child_init(int rank);
  40. static void mod_destroy(void);
  41. PyObject *handler_obj;
  42. char *dname = NULL, *bname = NULL;
  43. PyThreadState *myThreadState;
  44. /** module parameters */
  45. static param_export_t params[]={
  46. {"script_name", PARAM_STR, &script_name },
  47. {"mod_init_function", PARAM_STR, &mod_init_fname },
  48. {"child_init_method", PARAM_STR, &child_init_mname },
  49. {0,0,0}
  50. };
  51. /*
  52. * Exported functions
  53. */
  54. static cmd_export_t cmds[] = {
  55. { "python_exec", (cmd_function)python_exec1, 1, NULL, 0, ANY_ROUTE },
  56. { "python_exec", (cmd_function)python_exec2, 2, NULL, 0, ANY_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_src, *bname_src;
  77. int i;
  78. PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
  79. PyThreadState *mainThreadState;
  80. dname_src = as_asciiz(&script_name);
  81. bname_src = as_asciiz(&script_name);
  82. if(dname_src==NULL || bname_src==NULL)
  83. {
  84. LM_ERR("no more pkg memory\n");
  85. return -1;
  86. }
  87. dname = strdup(dirname(dname_src));
  88. if (strlen(dname) == 0)
  89. dname = ".";
  90. bname = strdup(basename(bname_src));
  91. i = strlen(bname);
  92. if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
  93. i -= 1;
  94. if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
  95. bname[i - 3] = '\0';
  96. } else {
  97. LM_ERR("%s: script_name doesn't look like a python script\n",
  98. script_name.s);
  99. return -1;
  100. }
  101. Py_Initialize();
  102. PyEval_InitThreads();
  103. mainThreadState = PyThreadState_Get();
  104. format_exc_obj = InitTracebackModule();
  105. if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj))
  106. {
  107. Py_XDECREF(format_exc_obj);
  108. PyEval_ReleaseLock();
  109. return -1;
  110. }
  111. sys_path = PySys_GetObject("path");
  112. /* PySys_GetObject doesn't pass reference! No need to DEREF */
  113. if (sys_path == NULL) {
  114. if (!PyErr_Occurred())
  115. PyErr_Format(PyExc_AttributeError, "'module' object 'sys' has no attribute 'path'");
  116. python_handle_exception("mod_init");
  117. Py_DECREF(format_exc_obj);
  118. PyEval_ReleaseLock();
  119. return -1;
  120. }
  121. pDir = PyString_FromString(dname);
  122. if (pDir == NULL) {
  123. if (!PyErr_Occurred())
  124. PyErr_Format(PyExc_AttributeError, "PyString_FromString() has failed");
  125. python_handle_exception("mod_init");
  126. Py_DECREF(format_exc_obj);
  127. PyEval_ReleaseLock();
  128. return -1;
  129. }
  130. PyList_Insert(sys_path, 0, pDir);
  131. Py_DECREF(pDir);
  132. if (ap_init_modules() != 0) {
  133. if (!PyErr_Occurred())
  134. PyErr_SetString(PyExc_AttributeError, "init_modules() has failed");
  135. python_handle_exception("mod_init");
  136. Py_DECREF(format_exc_obj);
  137. PyEval_ReleaseLock();
  138. return -1;
  139. }
  140. if (python_msgobj_init() != 0) {
  141. if (!PyErr_Occurred())
  142. PyErr_SetString(PyExc_AttributeError, "python_msgobj_init() has failed");
  143. python_handle_exception("mod_init");
  144. Py_DECREF(format_exc_obj);
  145. PyEval_ReleaseLock();
  146. return -1;
  147. }
  148. pModule = PyImport_ImportModule(bname);
  149. if (pModule == NULL) {
  150. if (!PyErr_Occurred())
  151. PyErr_Format(PyExc_ImportError, "No module named '%s'", bname);
  152. python_handle_exception("mod_init");
  153. Py_DECREF(format_exc_obj);
  154. PyEval_ReleaseLock();
  155. return -1;
  156. }
  157. pkg_free(dname_src);
  158. pkg_free(bname_src);
  159. pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
  160. Py_DECREF(pModule);
  161. /* pFunc is a new reference */
  162. if (pFunc == NULL) {
  163. if (!PyErr_Occurred())
  164. PyErr_Format(PyExc_AttributeError, "'module' object '%s' has no attribute '%s'", bname, mod_init_fname.s);
  165. python_handle_exception("mod_init");
  166. Py_DECREF(format_exc_obj);
  167. Py_XDECREF(pFunc);
  168. PyEval_ReleaseLock();
  169. return -1;
  170. }
  171. if (!PyCallable_Check(pFunc)) {
  172. if (!PyErr_Occurred())
  173. PyErr_Format(PyExc_AttributeError, "module object '%s' has is not callable attribute '%s'", bname, mod_init_fname.s);
  174. python_handle_exception("mod_init");
  175. Py_DECREF(format_exc_obj);
  176. Py_XDECREF(pFunc);
  177. PyEval_ReleaseLock();
  178. return -1;
  179. }
  180. pArgs = PyTuple_New(0);
  181. if (pArgs == NULL) {
  182. python_handle_exception("mod_init");
  183. Py_DECREF(format_exc_obj);
  184. Py_DECREF(pFunc);
  185. PyEval_ReleaseLock();
  186. return -1;
  187. }
  188. handler_obj = PyObject_CallObject(pFunc, pArgs);
  189. Py_XDECREF(pFunc);
  190. Py_XDECREF(pArgs);
  191. if (handler_obj == Py_None) {
  192. if (!PyErr_Occurred())
  193. PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned None. Should be a class instance.", mod_init_fname.s, bname);
  194. python_handle_exception("mod_init");
  195. Py_DECREF(format_exc_obj);
  196. PyEval_ReleaseLock();
  197. return -1;
  198. }
  199. if (PyErr_Occurred()) {
  200. python_handle_exception("mod_init");
  201. Py_XDECREF(handler_obj);
  202. Py_DECREF(format_exc_obj);
  203. PyEval_ReleaseLock();
  204. return -1;
  205. }
  206. if (handler_obj == NULL) {
  207. LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
  208. if (!PyErr_Occurred())
  209. PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned not returned object. Should be a class instance.", mod_init_fname.s, bname);
  210. python_handle_exception("mod_init");
  211. Py_DECREF(format_exc_obj);
  212. PyEval_ReleaseLock();
  213. return -1;
  214. }
  215. myThreadState = PyThreadState_New(mainThreadState->interp);
  216. PyEval_ReleaseLock();
  217. return 0;
  218. }
  219. static int child_init(int rank)
  220. {
  221. PyObject *pFunc, *pArgs, *pValue, *pResult;
  222. int rval;
  223. char *classname;
  224. PyEval_AcquireLock();
  225. PyThreadState_Swap(myThreadState);
  226. // get instance class name
  227. classname = get_instance_class_name(handler_obj);
  228. if (classname == NULL)
  229. {
  230. if (!PyErr_Occurred())
  231. PyErr_Format(PyExc_AttributeError, "'module' instance has no class name");
  232. python_handle_exception("child_init");
  233. Py_DECREF(format_exc_obj);
  234. PyThreadState_Swap(NULL);
  235. PyEval_ReleaseLock();
  236. return -1;
  237. }
  238. pFunc = PyObject_GetAttrString(handler_obj, child_init_mname.s);
  239. if (pFunc == NULL) {
  240. python_handle_exception("child_init");
  241. Py_XDECREF(pFunc);
  242. Py_DECREF(format_exc_obj);
  243. PyThreadState_Swap(NULL);
  244. PyEval_ReleaseLock();
  245. return -1;
  246. }
  247. if (!PyCallable_Check(pFunc)) {
  248. if (!PyErr_Occurred())
  249. PyErr_Format(PyExc_AttributeError, "class object '%s' has is not callable attribute '%s'", !classname ? "None" : classname, mod_init_fname.s);
  250. python_handle_exception("child_init");
  251. Py_DECREF(format_exc_obj);
  252. Py_XDECREF(pFunc);
  253. PyThreadState_Swap(NULL);
  254. PyEval_ReleaseLock();
  255. return -1;
  256. }
  257. pArgs = PyTuple_New(1);
  258. if (pArgs == NULL) {
  259. python_handle_exception("child_init");
  260. Py_DECREF(format_exc_obj);
  261. Py_DECREF(pFunc);
  262. PyThreadState_Swap(NULL);
  263. PyEval_ReleaseLock();
  264. return -1;
  265. }
  266. pValue = PyInt_FromLong((long)rank);
  267. if (pValue == NULL) {
  268. python_handle_exception("child_init");
  269. Py_DECREF(format_exc_obj);
  270. Py_DECREF(pArgs);
  271. Py_DECREF(pFunc);
  272. PyThreadState_Swap(NULL);
  273. PyEval_ReleaseLock();
  274. return -1;
  275. }
  276. PyTuple_SetItem(pArgs, 0, pValue);
  277. /* pValue has been stolen */
  278. pResult = PyObject_CallObject(pFunc, pArgs);
  279. Py_DECREF(pFunc);
  280. Py_DECREF(pArgs);
  281. if (PyErr_Occurred()) {
  282. python_handle_exception("child_init");
  283. Py_DECREF(format_exc_obj);
  284. Py_XDECREF(pResult);
  285. PyThreadState_Swap(NULL);
  286. PyEval_ReleaseLock();
  287. return -1;
  288. }
  289. if (pResult == NULL) {
  290. LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
  291. PyThreadState_Swap(NULL);
  292. PyEval_ReleaseLock();
  293. return -1;
  294. }
  295. if (!PyInt_Check(pResult))
  296. {
  297. if (!PyErr_Occurred())
  298. PyErr_Format(PyExc_TypeError, "method '%s' of class '%s' should return 'int' type", child_init_mname.s, !classname ? "None" : classname);
  299. python_handle_exception("child_init");
  300. Py_DECREF(format_exc_obj);
  301. Py_XDECREF(pResult);
  302. PyThreadState_Swap(NULL);
  303. PyEval_ReleaseLock();
  304. return -1;
  305. }
  306. rval = PyInt_AsLong(pResult);
  307. Py_DECREF(pResult);
  308. PyThreadState_Swap(NULL);
  309. PyEval_ReleaseLock();
  310. return rval;
  311. }
  312. static void mod_destroy(void)
  313. {
  314. if (dname)
  315. free(dname); // dname was strdup'ed
  316. if (bname)
  317. free(bname); // bname was strdup'ed
  318. destroy_mod_Core();
  319. destroy_mod_Ranks();
  320. destroy_mod_Logger();
  321. destroy_mod_Router();
  322. }