Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2 | Copyright 1994 by Lance Ellinghouse, |
| 3 | Cathedral City, California Republic, United States of America. |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 11 | supporting documentation, and that the name of Lance Ellinghouse |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 12 | not be used in advertising or publicity pertaining to distribution |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 13 | of the software without specific, written prior permission. |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 15 | LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 17 | FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL, |
| 18 | INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING |
| 19 | FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 21 | WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 25 | /****************************************************************** |
| 26 | |
| 27 | Revision history: |
| 28 | |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 29 | 2010/04/20 (Sean Reifschneider) |
| 30 | - Use basename(sys.argv[0]) for the default "ident". |
| 31 | - Arguments to openlog() are now keyword args and are all optional. |
| 32 | - syslog() calls openlog() if it hasn't already been called. |
| 33 | |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 34 | 1998/04/28 (Sean Reifschneider) |
| 35 | - When facility not specified to syslog() method, use default from openlog() |
| 36 | (This is how it was claimed to work in the documentation) |
| 37 | - Potential resource leak of o_ident, now cleaned up in closelog() |
| 38 | - Minor comment accuracy fix. |
| 39 | |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 40 | 95/06/29 (Steve Clift) |
| 41 | - Changed arg parsing to use PyArg_ParseTuple. |
| 42 | - Added PyErr_Clear() call(s) where needed. |
| 43 | - Fix core dumps if user message contains format specifiers. |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 44 | - Change openlog arg defaults to match normal syslog behavior. |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 45 | - Plug memory leak in openlog(). |
| 46 | - Fix setlogmask() to return previous mask value. |
| 47 | |
| 48 | ******************************************************************/ |
| 49 | |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 50 | /* syslog module */ |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 52 | #include "Python.h" |
Victor Stinner | 361dcdc | 2020-04-15 03:24:57 +0200 | [diff] [blame] | 53 | #include "osdefs.h" // SEP |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 54 | |
| 55 | #include <syslog.h> |
| 56 | |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 57 | /* only one instance, only one syslog, so globals should be ok */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */ |
Sean Reifscheider | 7f810cd | 2010-04-25 06:31:55 +0000 | [diff] [blame] | 59 | static char S_log_open = 0; |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 60 | |
| 61 | |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 62 | static PyObject * |
| 63 | syslog_get_argv(void) |
| 64 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | /* Figure out what to use for as the program "ident" for openlog(). |
| 66 | * This swallows exceptions and continues rather than failing out, |
| 67 | * because the syslog module can still be used because openlog(3) |
| 68 | * is optional. |
| 69 | */ |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 70 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 71 | Py_ssize_t argv_len, scriptlen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | PyObject *scriptobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 73 | Py_ssize_t slash; |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 74 | PyObject *argv = PySys_GetObject("argv"); |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 75 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | if (argv == NULL) { |
| 77 | return(NULL); |
| 78 | } |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 79 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | argv_len = PyList_Size(argv); |
| 81 | if (argv_len == -1) { |
| 82 | PyErr_Clear(); |
| 83 | return(NULL); |
| 84 | } |
| 85 | if (argv_len == 0) { |
| 86 | return(NULL); |
| 87 | } |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 88 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | scriptobj = PyList_GetItem(argv, 0); |
| 90 | if (!PyUnicode_Check(scriptobj)) { |
| 91 | return(NULL); |
| 92 | } |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 93 | scriptlen = PyUnicode_GET_LENGTH(scriptobj); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 94 | if (scriptlen == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | return(NULL); |
| 96 | } |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 97 | |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 98 | slash = PyUnicode_FindChar(scriptobj, SEP, 0, scriptlen, -1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 99 | if (slash == -2) |
| 100 | return NULL; |
| 101 | if (slash != -1) { |
Václav Bartoš | f04750b | 2020-01-14 18:57:04 +0100 | [diff] [blame] | 102 | return PyUnicode_Substring(scriptobj, slash + 1, scriptlen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | } else { |
| 104 | Py_INCREF(scriptobj); |
| 105 | return(scriptobj); |
| 106 | } |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | return(NULL); |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | static PyObject * |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 113 | syslog_openlog(PyObject * self, PyObject * args, PyObject *kwds) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | long logopt = 0; |
| 116 | long facility = LOG_USER; |
| 117 | PyObject *new_S_ident_o = NULL; |
| 118 | static char *keywords[] = {"ident", "logoption", "facility", 0}; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 119 | const char *ident = NULL; |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 122 | "|Ull:openlog", keywords, &new_S_ident_o, &logopt, &facility)) |
| 123 | return NULL; |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | if (new_S_ident_o) { |
| 126 | Py_INCREF(new_S_ident_o); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 127 | } |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | /* get sys.argv[0] or NULL if we can't for some reason */ |
| 130 | if (!new_S_ident_o) { |
| 131 | new_S_ident_o = syslog_get_argv(); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 132 | } |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | Py_XDECREF(S_ident_o); |
| 135 | S_ident_o = new_S_ident_o; |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | /* At this point, S_ident_o should be INCREF()ed. openlog(3) does not |
| 138 | * make a copy, and syslog(3) later uses it. We can't garbagecollect it |
| 139 | * If NULL, just let openlog figure it out (probably using C argv[0]). |
| 140 | */ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 141 | if (S_ident_o) { |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 142 | ident = PyUnicode_AsUTF8(S_ident_o); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 143 | if (ident == NULL) |
| 144 | return NULL; |
| 145 | } |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 146 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 147 | if (PySys_Audit("syslog.openlog", "sll", ident, logopt, facility) < 0) { |
| 148 | return NULL; |
| 149 | } |
| 150 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 151 | openlog(ident, logopt, facility); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | S_log_open = 1; |
| 153 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 154 | Py_RETURN_NONE; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 159 | syslog_syslog(PyObject * self, PyObject * args) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 160 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 161 | PyObject *message_object; |
| 162 | const char *message; |
| 163 | int priority = LOG_INFO; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | if (!PyArg_ParseTuple(args, "iU;[priority,] message string", |
| 166 | &priority, &message_object)) { |
| 167 | PyErr_Clear(); |
| 168 | if (!PyArg_ParseTuple(args, "U;[priority,] message string", |
| 169 | &message_object)) |
| 170 | return NULL; |
| 171 | } |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 172 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 173 | message = PyUnicode_AsUTF8(message_object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | if (message == NULL) |
| 175 | return NULL; |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 176 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 177 | if (PySys_Audit("syslog.syslog", "is", priority, message) < 0) { |
| 178 | return NULL; |
| 179 | } |
| 180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 181 | /* if log is not opened, open it now */ |
| 182 | if (!S_log_open) { |
| 183 | PyObject *openargs; |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | /* Continue even if PyTuple_New fails, because openlog(3) is optional. |
| 186 | * So, we can still do loggin in the unlikely event things are so hosed |
| 187 | * that we can't do this tuple. |
| 188 | */ |
| 189 | if ((openargs = PyTuple_New(0))) { |
| 190 | PyObject *openlog_ret = syslog_openlog(self, openargs, NULL); |
| 191 | Py_XDECREF(openlog_ret); |
| 192 | Py_DECREF(openargs); |
| 193 | } |
| 194 | } |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | Py_BEGIN_ALLOW_THREADS; |
| 197 | syslog(priority, "%s", message); |
| 198 | Py_END_ALLOW_THREADS; |
| 199 | Py_RETURN_NONE; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 203 | syslog_closelog(PyObject *self, PyObject *unused) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 204 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 205 | if (PySys_Audit("syslog.closelog", NULL) < 0) { |
| 206 | return NULL; |
| 207 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | if (S_log_open) { |
| 209 | closelog(); |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 210 | Py_CLEAR(S_ident_o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | S_log_open = 0; |
| 212 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 213 | Py_RETURN_NONE; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 217 | syslog_setlogmask(PyObject *self, PyObject *args) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | long maskpri, omaskpri; |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri)) |
| 222 | return NULL; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 223 | if (PySys_Audit("syslog.setlogmask", "(O)", args ? args : Py_None) < 0) { |
| 224 | return NULL; |
| 225 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | omaskpri = setlogmask(maskpri); |
| 227 | return PyLong_FromLong(omaskpri); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 231 | syslog_log_mask(PyObject *self, PyObject *args) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 232 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | long mask; |
| 234 | long pri; |
| 235 | if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri)) |
| 236 | return NULL; |
| 237 | mask = LOG_MASK(pri); |
| 238 | return PyLong_FromLong(mask); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 242 | syslog_log_upto(PyObject *self, PyObject *args) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 243 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 244 | long mask; |
| 245 | long pri; |
| 246 | if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri)) |
| 247 | return NULL; |
| 248 | mask = LOG_UPTO(pri); |
| 249 | return PyLong_FromLong(mask); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* List of functions defined in the module */ |
| 253 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 254 | static PyMethodDef syslog_methods[] = { |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 255 | {"openlog", (PyCFunction)(void(*)(void)) syslog_openlog, METH_VARARGS | METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | {"closelog", syslog_closelog, METH_NOARGS}, |
| 257 | {"syslog", syslog_syslog, METH_VARARGS}, |
| 258 | {"setlogmask", syslog_setlogmask, METH_VARARGS}, |
| 259 | {"LOG_MASK", syslog_log_mask, METH_VARARGS}, |
| 260 | {"LOG_UPTO", syslog_log_upto, METH_VARARGS}, |
| 261 | {NULL, NULL, 0} |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 262 | }; |
| 263 | |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 264 | |
Dong-hee Na | 92a98ed | 2020-05-05 10:49:46 +0900 | [diff] [blame] | 265 | static int |
| 266 | syslog_exec(PyObject *module) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 267 | { |
Dong-hee Na | 92a98ed | 2020-05-05 10:49:46 +0900 | [diff] [blame] | 268 | #define ADD_INT_MACRO(module, macro) \ |
| 269 | do { \ |
| 270 | if (PyModule_AddIntConstant(module, #macro, macro) < 0) { \ |
| 271 | return -1; \ |
| 272 | } \ |
| 273 | } while (0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | /* Priorities */ |
Dong-hee Na | 92a98ed | 2020-05-05 10:49:46 +0900 | [diff] [blame] | 275 | ADD_INT_MACRO(module, LOG_EMERG); |
| 276 | ADD_INT_MACRO(module, LOG_ALERT); |
| 277 | ADD_INT_MACRO(module, LOG_CRIT); |
| 278 | ADD_INT_MACRO(module, LOG_ERR); |
| 279 | ADD_INT_MACRO(module, LOG_WARNING); |
| 280 | ADD_INT_MACRO(module, LOG_NOTICE); |
| 281 | ADD_INT_MACRO(module, LOG_INFO); |
| 282 | ADD_INT_MACRO(module, LOG_DEBUG); |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | /* openlog() option flags */ |
Dong-hee Na | 92a98ed | 2020-05-05 10:49:46 +0900 | [diff] [blame] | 285 | ADD_INT_MACRO(module, LOG_PID); |
| 286 | ADD_INT_MACRO(module, LOG_CONS); |
| 287 | ADD_INT_MACRO(module, LOG_NDELAY); |
R David Murray | eac0939 | 2012-03-29 07:15:45 -0400 | [diff] [blame] | 288 | #ifdef LOG_ODELAY |
Dong-hee Na | 92a98ed | 2020-05-05 10:49:46 +0900 | [diff] [blame] | 289 | ADD_INT_MACRO(module, LOG_ODELAY); |
R David Murray | eac0939 | 2012-03-29 07:15:45 -0400 | [diff] [blame] | 290 | #endif |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 291 | #ifdef LOG_NOWAIT |
Dong-hee Na | 92a98ed | 2020-05-05 10:49:46 +0900 | [diff] [blame] | 292 | ADD_INT_MACRO(module, LOG_NOWAIT); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 293 | #endif |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 294 | #ifdef LOG_PERROR |
Dong-hee Na | 92a98ed | 2020-05-05 10:49:46 +0900 | [diff] [blame] | 295 | ADD_INT_MACRO(module, LOG_PERROR); |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 296 | #endif |
| 297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | /* Facilities */ |
Dong-hee Na | 92a98ed | 2020-05-05 10:49:46 +0900 | [diff] [blame] | 299 | ADD_INT_MACRO(module, LOG_KERN); |
| 300 | ADD_INT_MACRO(module, LOG_USER); |
| 301 | ADD_INT_MACRO(module, LOG_MAIL); |
| 302 | ADD_INT_MACRO(module, LOG_DAEMON); |
| 303 | ADD_INT_MACRO(module, LOG_AUTH); |
| 304 | ADD_INT_MACRO(module, LOG_LPR); |
| 305 | ADD_INT_MACRO(module, LOG_LOCAL0); |
| 306 | ADD_INT_MACRO(module, LOG_LOCAL1); |
| 307 | ADD_INT_MACRO(module, LOG_LOCAL2); |
| 308 | ADD_INT_MACRO(module, LOG_LOCAL3); |
| 309 | ADD_INT_MACRO(module, LOG_LOCAL4); |
| 310 | ADD_INT_MACRO(module, LOG_LOCAL5); |
| 311 | ADD_INT_MACRO(module, LOG_LOCAL6); |
| 312 | ADD_INT_MACRO(module, LOG_LOCAL7); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 313 | |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 314 | #ifndef LOG_SYSLOG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | #define LOG_SYSLOG LOG_DAEMON |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 316 | #endif |
| 317 | #ifndef LOG_NEWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | #define LOG_NEWS LOG_MAIL |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 319 | #endif |
| 320 | #ifndef LOG_UUCP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | #define LOG_UUCP LOG_MAIL |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 322 | #endif |
| 323 | #ifndef LOG_CRON |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | #define LOG_CRON LOG_DAEMON |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 325 | #endif |
| 326 | |
Dong-hee Na | 92a98ed | 2020-05-05 10:49:46 +0900 | [diff] [blame] | 327 | ADD_INT_MACRO(module, LOG_SYSLOG); |
| 328 | ADD_INT_MACRO(module, LOG_CRON); |
| 329 | ADD_INT_MACRO(module, LOG_UUCP); |
| 330 | ADD_INT_MACRO(module, LOG_NEWS); |
R David Murray | eac0939 | 2012-03-29 07:15:45 -0400 | [diff] [blame] | 331 | |
| 332 | #ifdef LOG_AUTHPRIV |
Dong-hee Na | 92a98ed | 2020-05-05 10:49:46 +0900 | [diff] [blame] | 333 | ADD_INT_MACRO(module, LOG_AUTHPRIV); |
R David Murray | eac0939 | 2012-03-29 07:15:45 -0400 | [diff] [blame] | 334 | #endif |
| 335 | |
Dong-hee Na | 92a98ed | 2020-05-05 10:49:46 +0900 | [diff] [blame] | 336 | return 0; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 337 | } |
Dong-hee Na | 92a98ed | 2020-05-05 10:49:46 +0900 | [diff] [blame] | 338 | |
| 339 | static PyModuleDef_Slot syslog_slots[] = { |
| 340 | {Py_mod_exec, syslog_exec}, |
| 341 | {0, NULL} |
| 342 | }; |
| 343 | |
| 344 | /* Initialization function for the module */ |
| 345 | |
| 346 | static struct PyModuleDef syslogmodule = { |
| 347 | PyModuleDef_HEAD_INIT, |
| 348 | .m_name = "syslog", |
| 349 | .m_size = 0, |
| 350 | .m_methods = syslog_methods, |
| 351 | .m_slots = syslog_slots, |
| 352 | }; |
| 353 | |
| 354 | PyMODINIT_FUNC |
| 355 | PyInit_syslog(void) |
| 356 | { |
| 357 | return PyModuleDef_Init(&syslogmodule); |
| 358 | } |