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