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