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 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 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 |
| 12 | not be used in advertising or publicity pertaining to distribution |
| 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 |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +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 |
| 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 | |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 29 | 1998/04/28 (Sean Reifschneider) |
| 30 | - When facility not specified to syslog() method, use default from openlog() |
| 31 | (This is how it was claimed to work in the documentation) |
| 32 | - Potential resource leak of o_ident, now cleaned up in closelog() |
| 33 | - Minor comment accuracy fix. |
| 34 | |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 35 | 95/06/29 (Steve Clift) |
| 36 | - Changed arg parsing to use PyArg_ParseTuple. |
| 37 | - Added PyErr_Clear() call(s) where needed. |
| 38 | - Fix core dumps if user message contains format specifiers. |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 39 | - Change openlog arg defaults to match normal syslog behavior. |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 40 | - Plug memory leak in openlog(). |
| 41 | - Fix setlogmask() to return previous mask value. |
| 42 | |
| 43 | ******************************************************************/ |
| 44 | |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 45 | /* syslog module */ |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 47 | #include "Python.h" |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 48 | |
| 49 | #include <syslog.h> |
| 50 | |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 51 | /* only one instance, only one syslog, so globals should be ok */ |
| 52 | static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */ |
| 53 | |
| 54 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 55 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 56 | syslog_openlog(PyObject * self, PyObject * args) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 57 | { |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 58 | long logopt = 0; |
| 59 | long facility = LOG_USER; |
Raymond Hettinger | 6f5b741 | 2004-12-16 23:52:04 +0000 | [diff] [blame] | 60 | PyObject *new_S_ident_o; |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 61 | |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 62 | if (!PyArg_ParseTuple(args, |
| 63 | "S|ll;ident string [, logoption [, facility]]", |
Raymond Hettinger | 6f5b741 | 2004-12-16 23:52:04 +0000 | [diff] [blame] | 64 | &new_S_ident_o, &logopt, &facility)) |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 65 | return NULL; |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 66 | |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 67 | /* This is needed because openlog() does NOT make a copy |
| 68 | * and syslog() later uses it.. cannot trash it. |
| 69 | */ |
Raymond Hettinger | 6f5b741 | 2004-12-16 23:52:04 +0000 | [diff] [blame] | 70 | Py_XDECREF(S_ident_o); |
| 71 | S_ident_o = new_S_ident_o; |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 72 | Py_INCREF(S_ident_o); |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 73 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame^] | 74 | openlog(PyString_AsString(S_ident_o), logopt, facility); |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 75 | |
| 76 | Py_INCREF(Py_None); |
| 77 | return Py_None; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 81 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 82 | syslog_syslog(PyObject * self, PyObject * args) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 83 | { |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 84 | char *message; |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 85 | int priority = LOG_INFO; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 86 | |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 87 | if (!PyArg_ParseTuple(args, "is;[priority,] message string", |
| 88 | &priority, &message)) { |
| 89 | PyErr_Clear(); |
| 90 | if (!PyArg_ParseTuple(args, "s;[priority,] message string", |
| 91 | &message)) |
| 92 | return NULL; |
| 93 | } |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 94 | |
Christian Heimes | f0476e8 | 2008-02-23 17:42:31 +0000 | [diff] [blame] | 95 | Py_BEGIN_ALLOW_THREADS; |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 96 | syslog(priority, "%s", message); |
Christian Heimes | f0476e8 | 2008-02-23 17:42:31 +0000 | [diff] [blame] | 97 | Py_END_ALLOW_THREADS; |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 98 | Py_INCREF(Py_None); |
| 99 | return Py_None; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 102 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 103 | syslog_closelog(PyObject *self, PyObject *unused) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 104 | { |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 105 | closelog(); |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 106 | Py_XDECREF(S_ident_o); |
| 107 | S_ident_o = NULL; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 108 | Py_INCREF(Py_None); |
| 109 | return Py_None; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 112 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 113 | syslog_setlogmask(PyObject *self, PyObject *args) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 114 | { |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 115 | long maskpri, omaskpri; |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 116 | |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 117 | if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri)) |
| 118 | return NULL; |
| 119 | omaskpri = setlogmask(maskpri); |
| 120 | return PyInt_FromLong(omaskpri); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 123 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 124 | syslog_log_mask(PyObject *self, PyObject *args) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 125 | { |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 126 | long mask; |
| 127 | long pri; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 128 | if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri)) |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 129 | return NULL; |
| 130 | mask = LOG_MASK(pri); |
| 131 | return PyInt_FromLong(mask); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 134 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 135 | syslog_log_upto(PyObject *self, PyObject *args) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 136 | { |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 137 | long mask; |
| 138 | long pri; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 139 | if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri)) |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 140 | return NULL; |
| 141 | mask = LOG_UPTO(pri); |
| 142 | return PyInt_FromLong(mask); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* List of functions defined in the module */ |
| 146 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 147 | static PyMethodDef syslog_methods[] = { |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 148 | {"openlog", syslog_openlog, METH_VARARGS}, |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 149 | {"closelog", syslog_closelog, METH_NOARGS}, |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 150 | {"syslog", syslog_syslog, METH_VARARGS}, |
| 151 | {"setlogmask", syslog_setlogmask, METH_VARARGS}, |
| 152 | {"LOG_MASK", syslog_log_mask, METH_VARARGS}, |
| 153 | {"LOG_UPTO", syslog_log_upto, METH_VARARGS}, |
| 154 | {NULL, NULL, 0} |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 157 | /* Initialization function for the module */ |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 158 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 159 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 160 | initsyslog(void) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 161 | { |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 162 | PyObject *m; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 163 | |
| 164 | /* Create the module and add the functions */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 165 | m = Py_InitModule("syslog", syslog_methods); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 166 | if (m == NULL) |
| 167 | return; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 168 | |
| 169 | /* Add some symbolic constants to the module */ |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 170 | |
| 171 | /* Priorities */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 172 | PyModule_AddIntConstant(m, "LOG_EMERG", LOG_EMERG); |
| 173 | PyModule_AddIntConstant(m, "LOG_ALERT", LOG_ALERT); |
| 174 | PyModule_AddIntConstant(m, "LOG_CRIT", LOG_CRIT); |
| 175 | PyModule_AddIntConstant(m, "LOG_ERR", LOG_ERR); |
| 176 | PyModule_AddIntConstant(m, "LOG_WARNING", LOG_WARNING); |
| 177 | PyModule_AddIntConstant(m, "LOG_NOTICE", LOG_NOTICE); |
| 178 | PyModule_AddIntConstant(m, "LOG_INFO", LOG_INFO); |
| 179 | PyModule_AddIntConstant(m, "LOG_DEBUG", LOG_DEBUG); |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 180 | |
| 181 | /* openlog() option flags */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 182 | PyModule_AddIntConstant(m, "LOG_PID", LOG_PID); |
| 183 | PyModule_AddIntConstant(m, "LOG_CONS", LOG_CONS); |
| 184 | PyModule_AddIntConstant(m, "LOG_NDELAY", LOG_NDELAY); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 185 | #ifdef LOG_NOWAIT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 186 | PyModule_AddIntConstant(m, "LOG_NOWAIT", LOG_NOWAIT); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 187 | #endif |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 188 | #ifdef LOG_PERROR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 189 | PyModule_AddIntConstant(m, "LOG_PERROR", LOG_PERROR); |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 190 | #endif |
| 191 | |
| 192 | /* Facilities */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 193 | PyModule_AddIntConstant(m, "LOG_KERN", LOG_KERN); |
| 194 | PyModule_AddIntConstant(m, "LOG_USER", LOG_USER); |
| 195 | PyModule_AddIntConstant(m, "LOG_MAIL", LOG_MAIL); |
| 196 | PyModule_AddIntConstant(m, "LOG_DAEMON", LOG_DAEMON); |
| 197 | PyModule_AddIntConstant(m, "LOG_AUTH", LOG_AUTH); |
| 198 | PyModule_AddIntConstant(m, "LOG_LPR", LOG_LPR); |
| 199 | PyModule_AddIntConstant(m, "LOG_LOCAL0", LOG_LOCAL0); |
| 200 | PyModule_AddIntConstant(m, "LOG_LOCAL1", LOG_LOCAL1); |
| 201 | PyModule_AddIntConstant(m, "LOG_LOCAL2", LOG_LOCAL2); |
| 202 | PyModule_AddIntConstant(m, "LOG_LOCAL3", LOG_LOCAL3); |
| 203 | PyModule_AddIntConstant(m, "LOG_LOCAL4", LOG_LOCAL4); |
| 204 | PyModule_AddIntConstant(m, "LOG_LOCAL5", LOG_LOCAL5); |
| 205 | PyModule_AddIntConstant(m, "LOG_LOCAL6", LOG_LOCAL6); |
| 206 | PyModule_AddIntConstant(m, "LOG_LOCAL7", LOG_LOCAL7); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 208 | #ifndef LOG_SYSLOG |
| 209 | #define LOG_SYSLOG LOG_DAEMON |
| 210 | #endif |
| 211 | #ifndef LOG_NEWS |
| 212 | #define LOG_NEWS LOG_MAIL |
| 213 | #endif |
| 214 | #ifndef LOG_UUCP |
| 215 | #define LOG_UUCP LOG_MAIL |
| 216 | #endif |
| 217 | #ifndef LOG_CRON |
| 218 | #define LOG_CRON LOG_DAEMON |
| 219 | #endif |
| 220 | |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 221 | PyModule_AddIntConstant(m, "LOG_SYSLOG", LOG_SYSLOG); |
| 222 | PyModule_AddIntConstant(m, "LOG_CRON", LOG_CRON); |
| 223 | PyModule_AddIntConstant(m, "LOG_UUCP", LOG_UUCP); |
| 224 | PyModule_AddIntConstant(m, "LOG_NEWS", LOG_NEWS); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 225 | } |