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. |
| 39 | - Change openlog arg defaults to match normal syslog behaviour. |
| 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 * |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 56 | syslog_openlog(self, args) |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 57 | PyObject * self; |
| 58 | PyObject * args; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 59 | { |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 60 | long logopt = 0; |
| 61 | long facility = LOG_USER; |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 64 | Py_XDECREF(S_ident_o); |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 65 | if (!PyArg_ParseTuple(args, |
| 66 | "S|ll;ident string [, logoption [, facility]]", |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 67 | &S_ident_o, &logopt, &facility)) |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 68 | return NULL; |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 69 | |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 70 | /* This is needed because openlog() does NOT make a copy |
| 71 | * and syslog() later uses it.. cannot trash it. |
| 72 | */ |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 73 | Py_INCREF(S_ident_o); |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 75 | openlog(PyString_AsString(S_ident_o), logopt, facility); |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 76 | |
| 77 | Py_INCREF(Py_None); |
| 78 | return Py_None; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 82 | static PyObject * |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 83 | syslog_syslog(self, args) |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 84 | PyObject * self; |
| 85 | PyObject * args; |
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 | char *message; |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 88 | int priority = LOG_INFO; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 89 | |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 90 | if (!PyArg_ParseTuple(args, "is;[priority,] message string", |
| 91 | &priority, &message)) { |
| 92 | PyErr_Clear(); |
| 93 | if (!PyArg_ParseTuple(args, "s;[priority,] message string", |
| 94 | &message)) |
| 95 | return NULL; |
| 96 | } |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 97 | |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 98 | syslog(priority, "%s", message); |
| 99 | Py_INCREF(Py_None); |
| 100 | return Py_None; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 103 | static PyObject * |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 104 | syslog_closelog(self, args) |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 105 | PyObject * self; |
| 106 | PyObject * args; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 107 | { |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 108 | if (!PyArg_ParseTuple(args, "")) |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 109 | return NULL; |
| 110 | closelog(); |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 111 | Py_XDECREF(S_ident_o); |
| 112 | S_ident_o = NULL; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 113 | Py_INCREF(Py_None); |
| 114 | return Py_None; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 117 | static PyObject * |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 118 | syslog_setlogmask(self, args) |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 119 | PyObject * self; |
| 120 | PyObject * args; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 121 | { |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 122 | long maskpri, omaskpri; |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 123 | |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 124 | if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri)) |
| 125 | return NULL; |
| 126 | omaskpri = setlogmask(maskpri); |
| 127 | return PyInt_FromLong(omaskpri); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 130 | static PyObject * |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 131 | syslog_log_mask(self, args) |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 132 | PyObject * self; |
| 133 | PyObject * args; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 134 | { |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 135 | long mask; |
| 136 | long pri; |
| 137 | if (!PyArg_ParseTuple(args, "l", &pri)) |
| 138 | return NULL; |
| 139 | mask = LOG_MASK(pri); |
| 140 | return PyInt_FromLong(mask); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 143 | static PyObject * |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 144 | syslog_log_upto(self, args) |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 145 | PyObject * self; |
| 146 | PyObject * args; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 147 | { |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 148 | long mask; |
| 149 | long pri; |
| 150 | if (!PyArg_ParseTuple(args, "l", &pri)) |
| 151 | return NULL; |
| 152 | mask = LOG_UPTO(pri); |
| 153 | return PyInt_FromLong(mask); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* List of functions defined in the module */ |
| 157 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 158 | static PyMethodDef syslog_methods[] = { |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 159 | {"openlog", syslog_openlog, METH_VARARGS}, |
| 160 | {"closelog", syslog_closelog, METH_VARARGS}, |
| 161 | {"syslog", syslog_syslog, METH_VARARGS}, |
| 162 | {"setlogmask", syslog_setlogmask, METH_VARARGS}, |
| 163 | {"LOG_MASK", syslog_log_mask, METH_VARARGS}, |
| 164 | {"LOG_UPTO", syslog_log_upto, METH_VARARGS}, |
| 165 | {NULL, NULL, 0} |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 168 | /* helper function for initialization function */ |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 169 | |
Barry Warsaw | e886ea9 | 1997-01-17 00:01:33 +0000 | [diff] [blame] | 170 | static void |
| 171 | ins(d, s, x) |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 172 | PyObject *d; |
| 173 | char *s; |
| 174 | long x; |
| 175 | { |
Barry Warsaw | e886ea9 | 1997-01-17 00:01:33 +0000 | [diff] [blame] | 176 | PyObject *v = PyInt_FromLong(x); |
| 177 | if (v) { |
| 178 | PyDict_SetItemString(d, s, v); |
| 179 | Py_DECREF(v); |
| 180 | } |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 183 | /* Initialization function for the module */ |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 185 | void |
| 186 | initsyslog() |
| 187 | { |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 188 | PyObject *m, *d; |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 189 | |
| 190 | /* Create the module and add the functions */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 191 | m = Py_InitModule("syslog", syslog_methods); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 192 | |
| 193 | /* Add some symbolic constants to the module */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 194 | d = PyModule_GetDict(m); |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 195 | |
| 196 | /* Priorities */ |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 197 | ins(d, "LOG_EMERG", LOG_EMERG); |
| 198 | ins(d, "LOG_ALERT", LOG_ALERT); |
| 199 | ins(d, "LOG_CRIT", LOG_CRIT); |
| 200 | ins(d, "LOG_ERR", LOG_ERR); |
| 201 | ins(d, "LOG_WARNING", LOG_WARNING); |
| 202 | ins(d, "LOG_NOTICE", LOG_NOTICE); |
| 203 | ins(d, "LOG_INFO", LOG_INFO); |
| 204 | ins(d, "LOG_DEBUG", LOG_DEBUG); |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 205 | |
| 206 | /* openlog() option flags */ |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 207 | ins(d, "LOG_PID", LOG_PID); |
| 208 | ins(d, "LOG_CONS", LOG_CONS); |
| 209 | ins(d, "LOG_NDELAY", LOG_NDELAY); |
| 210 | ins(d, "LOG_NOWAIT", LOG_NOWAIT); |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 211 | #ifdef LOG_PERROR |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 212 | ins(d, "LOG_PERROR", LOG_PERROR); |
Guido van Rossum | c1822a4 | 1995-10-11 16:15:28 +0000 | [diff] [blame] | 213 | #endif |
| 214 | |
| 215 | /* Facilities */ |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 216 | ins(d, "LOG_KERN", LOG_KERN); |
| 217 | ins(d, "LOG_USER", LOG_USER); |
| 218 | ins(d, "LOG_MAIL", LOG_MAIL); |
| 219 | ins(d, "LOG_DAEMON", LOG_DAEMON); |
| 220 | ins(d, "LOG_AUTH", LOG_AUTH); |
| 221 | ins(d, "LOG_LPR", LOG_LPR); |
Barry Warsaw | 43a476a | 1997-01-09 23:51:21 +0000 | [diff] [blame] | 222 | ins(d, "LOG_LOCAL0", LOG_LOCAL0); |
| 223 | ins(d, "LOG_LOCAL1", LOG_LOCAL1); |
| 224 | ins(d, "LOG_LOCAL2", LOG_LOCAL2); |
| 225 | ins(d, "LOG_LOCAL3", LOG_LOCAL3); |
| 226 | ins(d, "LOG_LOCAL4", LOG_LOCAL4); |
| 227 | ins(d, "LOG_LOCAL5", LOG_LOCAL5); |
| 228 | ins(d, "LOG_LOCAL6", LOG_LOCAL6); |
| 229 | ins(d, "LOG_LOCAL7", LOG_LOCAL7); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | ae94cf2 | 1998-05-08 21:52:55 +0000 | [diff] [blame] | 231 | #ifndef LOG_SYSLOG |
| 232 | #define LOG_SYSLOG LOG_DAEMON |
| 233 | #endif |
| 234 | #ifndef LOG_NEWS |
| 235 | #define LOG_NEWS LOG_MAIL |
| 236 | #endif |
| 237 | #ifndef LOG_UUCP |
| 238 | #define LOG_UUCP LOG_MAIL |
| 239 | #endif |
| 240 | #ifndef LOG_CRON |
| 241 | #define LOG_CRON LOG_DAEMON |
| 242 | #endif |
| 243 | |
| 244 | ins(d, "LOG_SYSLOG", LOG_SYSLOG); |
| 245 | ins(d, "LOG_CRON", LOG_CRON); |
| 246 | ins(d, "LOG_UUCP", LOG_UUCP); |
| 247 | ins(d, "LOG_NEWS", LOG_NEWS); |
| 248 | |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 249 | /* Check for errors */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 250 | if (PyErr_Occurred()) |
| 251 | Py_FatalError("can't initialize module syslog"); |
Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 252 | } |