1 /***********************************************************
2 Copyright 1994 by Lance Ellinghouse,
3 Cathedral City, California Republic, United States of America.
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
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.
14
15 LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
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.
22
23 ******************************************************************/
24
25 /******************************************************************
26
27 Revision history:
28
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
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
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.
44 - Change openlog arg defaults to match normal syslog behavior.
45 - Plug memory leak in openlog().
46 - Fix setlogmask() to return previous mask value.
47
48 ******************************************************************/
49
50 /* syslog module */
51
52 #include "Python.h"
53 #include "osdefs.h"
54
55 #include <syslog.h>
56
57 /* only one instance, only one syslog, so globals should be ok */
58 static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */
59 static char S_log_open = 0;
60
61
62 static PyObject *
syslog_get_argv(void)63 syslog_get_argv(void)
64 {
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 */
70
71 Py_ssize_t argv_len, scriptlen;
72 PyObject *scriptobj;
73 Py_ssize_t slash;
74 PyObject *argv = PySys_GetObject("argv");
75
76 if (argv == NULL) {
77 return(NULL);
78 }
79
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 }
88
89 scriptobj = PyList_GetItem(argv, 0);
90 if (!PyUnicode_Check(scriptobj)) {
91 return(NULL);
92 }
93 scriptlen = PyUnicode_GET_LENGTH(scriptobj);
94 if (scriptlen == 0) {
95 return(NULL);
96 }
97
98 slash = PyUnicode_FindChar(scriptobj, SEP, 0, scriptlen, -1);
99 if (slash == -2)
100 return NULL;
101 if (slash != -1) {
102 return PyUnicode_Substring(scriptobj, slash, scriptlen);
103 } else {
104 Py_INCREF(scriptobj);
105 return(scriptobj);
106 }
107
108 return(NULL);
109 }
110
111
112 static PyObject *
syslog_openlog(PyObject * self,PyObject * args,PyObject * kwds)113 syslog_openlog(PyObject * self, PyObject * args, PyObject *kwds)
114 {
115 long logopt = 0;
116 long facility = LOG_USER;
117 PyObject *new_S_ident_o = NULL;
118 static char *keywords[] = {"ident", "logoption", "facility", 0};
119 const char *ident = NULL;
120
121 if (!PyArg_ParseTupleAndKeywords(args, kwds,
122 "|Ull:openlog", keywords, &new_S_ident_o, &logopt, &facility))
123 return NULL;
124
125 if (new_S_ident_o) {
126 Py_INCREF(new_S_ident_o);
127 }
128
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();
132 }
133
134 Py_XDECREF(S_ident_o);
135 S_ident_o = new_S_ident_o;
136
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 */
141 if (S_ident_o) {
142 ident = PyUnicode_AsUTF8(S_ident_o);
143 if (ident == NULL)
144 return NULL;
145 }
146
147 openlog(ident, logopt, facility);
148 S_log_open = 1;
149
150 Py_RETURN_NONE;
151 }
152
153
154 static PyObject *
syslog_syslog(PyObject * self,PyObject * args)155 syslog_syslog(PyObject * self, PyObject * args)
156 {
157 PyObject *message_object;
158 const char *message;
159 int priority = LOG_INFO;
160
161 if (!PyArg_ParseTuple(args, "iU;[priority,] message string",
162 &priority, &message_object)) {
163 PyErr_Clear();
164 if (!PyArg_ParseTuple(args, "U;[priority,] message string",
165 &message_object))
166 return NULL;
167 }
168
169 message = PyUnicode_AsUTF8(message_object);
170 if (message == NULL)
171 return NULL;
172
173 /* if log is not opened, open it now */
174 if (!S_log_open) {
175 PyObject *openargs;
176
177 /* Continue even if PyTuple_New fails, because openlog(3) is optional.
178 * So, we can still do loggin in the unlikely event things are so hosed
179 * that we can't do this tuple.
180 */
181 if ((openargs = PyTuple_New(0))) {
182 PyObject *openlog_ret = syslog_openlog(self, openargs, NULL);
183 Py_XDECREF(openlog_ret);
184 Py_DECREF(openargs);
185 }
186 }
187
188 Py_BEGIN_ALLOW_THREADS;
189 syslog(priority, "%s", message);
190 Py_END_ALLOW_THREADS;
191 Py_RETURN_NONE;
192 }
193
194 static PyObject *
syslog_closelog(PyObject * self,PyObject * unused)195 syslog_closelog(PyObject *self, PyObject *unused)
196 {
197 if (S_log_open) {
198 closelog();
199 Py_CLEAR(S_ident_o);
200 S_log_open = 0;
201 }
202 Py_RETURN_NONE;
203 }
204
205 static PyObject *
syslog_setlogmask(PyObject * self,PyObject * args)206 syslog_setlogmask(PyObject *self, PyObject *args)
207 {
208 long maskpri, omaskpri;
209
210 if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
211 return NULL;
212 omaskpri = setlogmask(maskpri);
213 return PyLong_FromLong(omaskpri);
214 }
215
216 static PyObject *
syslog_log_mask(PyObject * self,PyObject * args)217 syslog_log_mask(PyObject *self, PyObject *args)
218 {
219 long mask;
220 long pri;
221 if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
222 return NULL;
223 mask = LOG_MASK(pri);
224 return PyLong_FromLong(mask);
225 }
226
227 static PyObject *
syslog_log_upto(PyObject * self,PyObject * args)228 syslog_log_upto(PyObject *self, PyObject *args)
229 {
230 long mask;
231 long pri;
232 if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
233 return NULL;
234 mask = LOG_UPTO(pri);
235 return PyLong_FromLong(mask);
236 }
237
238 /* List of functions defined in the module */
239
240 static PyMethodDef syslog_methods[] = {
241 {"openlog", (PyCFunction) syslog_openlog, METH_VARARGS | METH_KEYWORDS},
242 {"closelog", syslog_closelog, METH_NOARGS},
243 {"syslog", syslog_syslog, METH_VARARGS},
244 {"setlogmask", syslog_setlogmask, METH_VARARGS},
245 {"LOG_MASK", syslog_log_mask, METH_VARARGS},
246 {"LOG_UPTO", syslog_log_upto, METH_VARARGS},
247 {NULL, NULL, 0}
248 };
249
250 /* Initialization function for the module */
251
252
253 static struct PyModuleDef syslogmodule = {
254 PyModuleDef_HEAD_INIT,
255 "syslog",
256 NULL,
257 -1,
258 syslog_methods,
259 NULL,
260 NULL,
261 NULL,
262 NULL
263 };
264
265 PyMODINIT_FUNC
PyInit_syslog(void)266 PyInit_syslog(void)
267 {
268 PyObject *m;
269
270 /* Create the module and add the functions */
271 m = PyModule_Create(&syslogmodule);
272 if (m == NULL)
273 return NULL;
274
275 /* Add some symbolic constants to the module */
276
277 /* Priorities */
278 PyModule_AddIntMacro(m, LOG_EMERG);
279 PyModule_AddIntMacro(m, LOG_ALERT);
280 PyModule_AddIntMacro(m, LOG_CRIT);
281 PyModule_AddIntMacro(m, LOG_ERR);
282 PyModule_AddIntMacro(m, LOG_WARNING);
283 PyModule_AddIntMacro(m, LOG_NOTICE);
284 PyModule_AddIntMacro(m, LOG_INFO);
285 PyModule_AddIntMacro(m, LOG_DEBUG);
286
287 /* openlog() option flags */
288 PyModule_AddIntMacro(m, LOG_PID);
289 PyModule_AddIntMacro(m, LOG_CONS);
290 PyModule_AddIntMacro(m, LOG_NDELAY);
291 #ifdef LOG_ODELAY
292 PyModule_AddIntMacro(m, LOG_ODELAY);
293 #endif
294 #ifdef LOG_NOWAIT
295 PyModule_AddIntMacro(m, LOG_NOWAIT);
296 #endif
297 #ifdef LOG_PERROR
298 PyModule_AddIntMacro(m, LOG_PERROR);
299 #endif
300
301 /* Facilities */
302 PyModule_AddIntMacro(m, LOG_KERN);
303 PyModule_AddIntMacro(m, LOG_USER);
304 PyModule_AddIntMacro(m, LOG_MAIL);
305 PyModule_AddIntMacro(m, LOG_DAEMON);
306 PyModule_AddIntMacro(m, LOG_AUTH);
307 PyModule_AddIntMacro(m, LOG_LPR);
308 PyModule_AddIntMacro(m, LOG_LOCAL0);
309 PyModule_AddIntMacro(m, LOG_LOCAL1);
310 PyModule_AddIntMacro(m, LOG_LOCAL2);
311 PyModule_AddIntMacro(m, LOG_LOCAL3);
312 PyModule_AddIntMacro(m, LOG_LOCAL4);
313 PyModule_AddIntMacro(m, LOG_LOCAL5);
314 PyModule_AddIntMacro(m, LOG_LOCAL6);
315 PyModule_AddIntMacro(m, LOG_LOCAL7);
316
317 #ifndef LOG_SYSLOG
318 #define LOG_SYSLOG LOG_DAEMON
319 #endif
320 #ifndef LOG_NEWS
321 #define LOG_NEWS LOG_MAIL
322 #endif
323 #ifndef LOG_UUCP
324 #define LOG_UUCP LOG_MAIL
325 #endif
326 #ifndef LOG_CRON
327 #define LOG_CRON LOG_DAEMON
328 #endif
329
330 PyModule_AddIntMacro(m, LOG_SYSLOG);
331 PyModule_AddIntMacro(m, LOG_CRON);
332 PyModule_AddIntMacro(m, LOG_UUCP);
333 PyModule_AddIntMacro(m, LOG_NEWS);
334
335 #ifdef LOG_AUTHPRIV
336 PyModule_AddIntMacro(m, LOG_AUTHPRIV);
337 #endif
338
339 return m;
340 }
341