syslogmodule.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /***********************************************************
  2. Copyright 1994 by Lance Ellinghouse,
  3. Cathedral City, California Republic, United States of America.
  4. All Rights Reserved
  5. Permission to use, copy, modify, and distribute this software and its
  6. documentation for any purpose and without fee is hereby granted,
  7. provided that the above copyright notice appear in all copies and that
  8. both that copyright notice and this permission notice appear in
  9. supporting documentation, and that the name of Lance Ellinghouse
  10. not be used in advertising or publicity pertaining to distribution
  11. of the software without specific, written prior permission.
  12. LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
  13. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  14. FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL,
  15. INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  16. FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  17. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  18. WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. ******************************************************************/
  20. /******************************************************************
  21. Revision history:
  22. 2010/04/20 (Sean Reifschneider)
  23. - Use basename(sys.argv[0]) for the default "ident".
  24. - Arguments to openlog() are now keyword args and are all optional.
  25. - syslog() calls openlog() if it hasn't already been called.
  26. 1998/04/28 (Sean Reifschneider)
  27. - When facility not specified to syslog() method, use default from openlog()
  28. (This is how it was claimed to work in the documentation)
  29. - Potential resource leak of o_ident, now cleaned up in closelog()
  30. - Minor comment accuracy fix.
  31. 95/06/29 (Steve Clift)
  32. - Changed arg parsing to use PyArg_ParseTuple.
  33. - Added PyErr_Clear() call(s) where needed.
  34. - Fix core dumps if user message contains format specifiers.
  35. - Change openlog arg defaults to match normal syslog behavior.
  36. - Plug memory leak in openlog().
  37. - Fix setlogmask() to return previous mask value.
  38. ******************************************************************/
  39. /* syslog module */
  40. #include "Python.h"
  41. #include "osdefs.h" // SEP
  42. #include <syslog.h>
  43. /*[clinic input]
  44. module syslog
  45. [clinic start generated code]*/
  46. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=478f4ac94a1d4cae]*/
  47. #include "clinic/syslogmodule.c.h"
  48. /* only one instance, only one syslog, so globals should be ok,
  49. * these fields are writable from the main interpreter only. */
  50. static PyObject *S_ident_o = NULL; // identifier, held by openlog()
  51. static char S_log_open = 0;
  52. static inline int
  53. is_main_interpreter(void)
  54. {
  55. return (PyInterpreterState_Get() == PyInterpreterState_Main());
  56. }
  57. static PyObject *
  58. syslog_get_argv(void)
  59. {
  60. /* Figure out what to use for as the program "ident" for openlog().
  61. * This swallows exceptions and continues rather than failing out,
  62. * because the syslog module can still be used because openlog(3)
  63. * is optional.
  64. */
  65. Py_ssize_t argv_len, scriptlen;
  66. PyObject *scriptobj;
  67. Py_ssize_t slash;
  68. PyObject *argv = PySys_GetObject("argv");
  69. if (argv == NULL) {
  70. return(NULL);
  71. }
  72. argv_len = PyList_Size(argv);
  73. if (argv_len == -1) {
  74. PyErr_Clear();
  75. return(NULL);
  76. }
  77. if (argv_len == 0) {
  78. return(NULL);
  79. }
  80. scriptobj = PyList_GetItem(argv, 0);
  81. if (scriptobj == NULL) {
  82. PyErr_Clear();
  83. return NULL;
  84. }
  85. if (!PyUnicode_Check(scriptobj)) {
  86. return(NULL);
  87. }
  88. scriptlen = PyUnicode_GET_LENGTH(scriptobj);
  89. if (scriptlen == 0) {
  90. return(NULL);
  91. }
  92. slash = PyUnicode_FindChar(scriptobj, SEP, 0, scriptlen, -1);
  93. if (slash == -2) {
  94. PyErr_Clear();
  95. return NULL;
  96. }
  97. if (slash != -1) {
  98. return PyUnicode_Substring(scriptobj, slash + 1, scriptlen);
  99. } else {
  100. Py_INCREF(scriptobj);
  101. return(scriptobj);
  102. }
  103. }
  104. /*[clinic input]
  105. syslog.openlog
  106. ident: unicode = NULL
  107. logoption as logopt: long = 0
  108. facility: long(c_default="LOG_USER") = LOG_USER
  109. Set logging options of subsequent syslog() calls.
  110. [clinic start generated code]*/
  111. static PyObject *
  112. syslog_openlog_impl(PyObject *module, PyObject *ident, long logopt,
  113. long facility)
  114. /*[clinic end generated code: output=5476c12829b6eb75 input=8a987a96a586eee7]*/
  115. {
  116. // Since the sys.openlog changes the process level state of syslog library,
  117. // this operation is only allowed for the main interpreter.
  118. if (!is_main_interpreter()) {
  119. PyErr_SetString(PyExc_RuntimeError, "subinterpreter can't use syslog.openlog()");
  120. return NULL;
  121. }
  122. const char *ident_str = NULL;
  123. if (ident) {
  124. Py_INCREF(ident);
  125. }
  126. else {
  127. /* get sys.argv[0] or NULL if we can't for some reason */
  128. ident = syslog_get_argv();
  129. }
  130. /* At this point, ident should be INCREF()ed. openlog(3) does not
  131. * make a copy, and syslog(3) later uses it. We can't garbagecollect it.
  132. * If NULL, just let openlog figure it out (probably using C argv[0]).
  133. */
  134. if (ident) {
  135. ident_str = PyUnicode_AsUTF8(ident);
  136. if (ident_str == NULL) {
  137. Py_DECREF(ident);
  138. return NULL;
  139. }
  140. }
  141. if (PySys_Audit("syslog.openlog", "Oll", ident ? ident : Py_None, logopt, facility) < 0) {
  142. Py_DECREF(ident);
  143. return NULL;
  144. }
  145. openlog(ident_str, logopt, facility);
  146. S_log_open = 1;
  147. Py_XSETREF(S_ident_o, ident);
  148. Py_RETURN_NONE;
  149. }
  150. /*[clinic input]
  151. syslog.syslog
  152. [
  153. priority: int(c_default="LOG_INFO") = LOG_INFO
  154. ]
  155. message: str
  156. /
  157. Send the string message to the system logger.
  158. [clinic start generated code]*/
  159. static PyObject *
  160. syslog_syslog_impl(PyObject *module, int group_left_1, int priority,
  161. const char *message)
  162. /*[clinic end generated code: output=c3dbc73445a0e078 input=ac83d92b12ea3d4e]*/
  163. {
  164. if (PySys_Audit("syslog.syslog", "is", priority, message) < 0) {
  165. return NULL;
  166. }
  167. /* if log is not opened, open it now */
  168. if (!S_log_open) {
  169. if (!is_main_interpreter()) {
  170. PyErr_SetString(PyExc_RuntimeError, "subinterpreter can't use syslog.syslog() "
  171. "until the syslog is opened by the main interpreter");
  172. return NULL;
  173. }
  174. PyObject *openlog_ret = syslog_openlog_impl(module, NULL, 0, LOG_USER);
  175. if (openlog_ret == NULL) {
  176. return NULL;
  177. }
  178. Py_DECREF(openlog_ret);
  179. }
  180. /* Incref ident, because it can be decrefed if syslog.openlog() is
  181. * called when the GIL is released.
  182. */
  183. PyObject *ident = Py_XNewRef(S_ident_o);
  184. #ifdef __APPLE__
  185. // gh-98178: On macOS, libc syslog() is not thread-safe
  186. syslog(priority, "%s", message);
  187. #else
  188. Py_BEGIN_ALLOW_THREADS;
  189. syslog(priority, "%s", message);
  190. Py_END_ALLOW_THREADS;
  191. #endif
  192. Py_XDECREF(ident);
  193. Py_RETURN_NONE;
  194. }
  195. /*[clinic input]
  196. syslog.closelog
  197. Reset the syslog module values and call the system library closelog().
  198. [clinic start generated code]*/
  199. static PyObject *
  200. syslog_closelog_impl(PyObject *module)
  201. /*[clinic end generated code: output=97890a80a24b1b84 input=fb77a54d447acf07]*/
  202. {
  203. // Since the sys.closelog changes the process level state of syslog library,
  204. // this operation is only allowed for the main interpreter.
  205. if (!is_main_interpreter()) {
  206. PyErr_SetString(PyExc_RuntimeError, "sunbinterpreter can't use syslog.closelog()");
  207. return NULL;
  208. }
  209. if (PySys_Audit("syslog.closelog", NULL) < 0) {
  210. return NULL;
  211. }
  212. if (S_log_open) {
  213. closelog();
  214. Py_CLEAR(S_ident_o);
  215. S_log_open = 0;
  216. }
  217. Py_RETURN_NONE;
  218. }
  219. /*[clinic input]
  220. syslog.setlogmask -> long
  221. maskpri: long
  222. /
  223. Set the priority mask to maskpri and return the previous mask value.
  224. [clinic start generated code]*/
  225. static long
  226. syslog_setlogmask_impl(PyObject *module, long maskpri)
  227. /*[clinic end generated code: output=d6ed163917b434bf input=adff2c2b76c7629c]*/
  228. {
  229. if (PySys_Audit("syslog.setlogmask", "l", maskpri) < 0) {
  230. return -1;
  231. }
  232. return setlogmask(maskpri);
  233. }
  234. /*[clinic input]
  235. syslog.LOG_MASK -> long
  236. pri: long
  237. /
  238. Calculates the mask for the individual priority pri.
  239. [clinic start generated code]*/
  240. static long
  241. syslog_LOG_MASK_impl(PyObject *module, long pri)
  242. /*[clinic end generated code: output=c4a5bbfcc74c7c94 input=534829cb7fb5f7d2]*/
  243. {
  244. return LOG_MASK(pri);
  245. }
  246. /*[clinic input]
  247. syslog.LOG_UPTO -> long
  248. pri: long
  249. /
  250. Calculates the mask for all priorities up to and including pri.
  251. [clinic start generated code]*/
  252. static long
  253. syslog_LOG_UPTO_impl(PyObject *module, long pri)
  254. /*[clinic end generated code: output=9eab083c90601d7e input=5e906d6c406b7458]*/
  255. {
  256. return LOG_UPTO(pri);
  257. }
  258. /* List of functions defined in the module */
  259. static PyMethodDef syslog_methods[] = {
  260. SYSLOG_OPENLOG_METHODDEF
  261. SYSLOG_CLOSELOG_METHODDEF
  262. SYSLOG_SYSLOG_METHODDEF
  263. SYSLOG_SETLOGMASK_METHODDEF
  264. SYSLOG_LOG_MASK_METHODDEF
  265. SYSLOG_LOG_UPTO_METHODDEF
  266. {NULL, NULL, 0}
  267. };
  268. static int
  269. syslog_exec(PyObject *module)
  270. {
  271. #define ADD_INT_MACRO(module, macro) \
  272. do { \
  273. if (PyModule_AddIntConstant(module, #macro, macro) < 0) { \
  274. return -1; \
  275. } \
  276. } while (0)
  277. /* Priorities */
  278. ADD_INT_MACRO(module, LOG_EMERG);
  279. ADD_INT_MACRO(module, LOG_ALERT);
  280. ADD_INT_MACRO(module, LOG_CRIT);
  281. ADD_INT_MACRO(module, LOG_ERR);
  282. ADD_INT_MACRO(module, LOG_WARNING);
  283. ADD_INT_MACRO(module, LOG_NOTICE);
  284. ADD_INT_MACRO(module, LOG_INFO);
  285. ADD_INT_MACRO(module, LOG_DEBUG);
  286. /* openlog() option flags */
  287. ADD_INT_MACRO(module, LOG_PID);
  288. ADD_INT_MACRO(module, LOG_CONS);
  289. ADD_INT_MACRO(module, LOG_NDELAY);
  290. #ifdef LOG_ODELAY
  291. ADD_INT_MACRO(module, LOG_ODELAY);
  292. #endif
  293. #ifdef LOG_NOWAIT
  294. ADD_INT_MACRO(module, LOG_NOWAIT);
  295. #endif
  296. #ifdef LOG_PERROR
  297. ADD_INT_MACRO(module, LOG_PERROR);
  298. #endif
  299. /* Facilities */
  300. ADD_INT_MACRO(module, LOG_KERN);
  301. ADD_INT_MACRO(module, LOG_USER);
  302. ADD_INT_MACRO(module, LOG_MAIL);
  303. ADD_INT_MACRO(module, LOG_DAEMON);
  304. ADD_INT_MACRO(module, LOG_AUTH);
  305. ADD_INT_MACRO(module, LOG_LPR);
  306. ADD_INT_MACRO(module, LOG_LOCAL0);
  307. ADD_INT_MACRO(module, LOG_LOCAL1);
  308. ADD_INT_MACRO(module, LOG_LOCAL2);
  309. ADD_INT_MACRO(module, LOG_LOCAL3);
  310. ADD_INT_MACRO(module, LOG_LOCAL4);
  311. ADD_INT_MACRO(module, LOG_LOCAL5);
  312. ADD_INT_MACRO(module, LOG_LOCAL6);
  313. ADD_INT_MACRO(module, LOG_LOCAL7);
  314. #ifndef LOG_SYSLOG
  315. #define LOG_SYSLOG LOG_DAEMON
  316. #endif
  317. #ifndef LOG_NEWS
  318. #define LOG_NEWS LOG_MAIL
  319. #endif
  320. #ifndef LOG_UUCP
  321. #define LOG_UUCP LOG_MAIL
  322. #endif
  323. #ifndef LOG_CRON
  324. #define LOG_CRON LOG_DAEMON
  325. #endif
  326. ADD_INT_MACRO(module, LOG_SYSLOG);
  327. ADD_INT_MACRO(module, LOG_CRON);
  328. ADD_INT_MACRO(module, LOG_UUCP);
  329. ADD_INT_MACRO(module, LOG_NEWS);
  330. #ifdef LOG_AUTHPRIV
  331. ADD_INT_MACRO(module, LOG_AUTHPRIV);
  332. #endif
  333. return 0;
  334. }
  335. static PyModuleDef_Slot syslog_slots[] = {
  336. {Py_mod_exec, syslog_exec},
  337. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  338. {0, NULL}
  339. };
  340. /* Initialization function for the module */
  341. static struct PyModuleDef syslogmodule = {
  342. PyModuleDef_HEAD_INIT,
  343. .m_name = "syslog",
  344. .m_size = 0,
  345. .m_methods = syslog_methods,
  346. .m_slots = syslog_slots,
  347. };
  348. PyMODINIT_FUNC
  349. PyInit_syslog(void)
  350. {
  351. return PyModuleDef_Init(&syslogmodule);
  352. }