blob: 7afca58d0ec8c986763f6ba3980e1358a2df644b [file] [log] [blame]
Guido van Rossume44e3731994-07-14 13:56:50 +00001/***********************************************************
Guido van Rossumf6971e21994-08-30 12:25:20 +00002Copyright 1994 by Lance Ellinghouse,
3Cathedral City, California Republic, United States of America.
Guido van Rossume44e3731994-07-14 13:56:50 +00004
5 All Rights Reserved
6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossume44e3731994-07-14 13:56:50 +00009provided that the above copyright notice appear in all copies and that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf6971e21994-08-30 12:25:20 +000011supporting documentation, and that the name of Lance Ellinghouse
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000012not be used in advertising or publicity pertaining to distribution
Guido van Rossumf6971e21994-08-30 12:25:20 +000013of the software without specific, written prior permission.
Guido van Rossume44e3731994-07-14 13:56:50 +000014
Guido van Rossumf6971e21994-08-30 12:25:20 +000015LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
Guido van Rossume44e3731994-07-14 13:56:50 +000016THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL,
18INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
19FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
Guido van Rossumf6971e21994-08-30 12:25:20 +000021WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Guido van Rossume44e3731994-07-14 13:56:50 +000022
23******************************************************************/
24
Guido van Rossumc1822a41995-10-11 16:15:28 +000025/******************************************************************
26
27Revision history:
28
Sean Reifscheider13daf122010-04-23 09:29:52 +0000292010/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
Guido van Rossumae94cf21998-05-08 21:52:55 +0000341998/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
Guido van Rossumc1822a41995-10-11 16:15:28 +00004095/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.
Thomas Wouters7e474022000-07-16 12:04:32 +000044 - Change openlog arg defaults to match normal syslog behavior.
Guido van Rossumc1822a41995-10-11 16:15:28 +000045 - Plug memory leak in openlog().
46 - Fix setlogmask() to return previous mask value.
47
48******************************************************************/
49
Guido van Rossume44e3731994-07-14 13:56:50 +000050/* syslog module */
Guido van Rossume44e3731994-07-14 13:56:50 +000051
Guido van Rossuma597dde1995-01-10 20:56:29 +000052#include "Python.h"
Sean Reifscheider13daf122010-04-23 09:29:52 +000053#include "osdefs.h"
Guido van Rossume44e3731994-07-14 13:56:50 +000054
55#include <syslog.h>
56
Guido van Rossumae94cf21998-05-08 21:52:55 +000057/* only one instance, only one syslog, so globals should be ok */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */
Sean Reifscheider7f810cd2010-04-25 06:31:55 +000059static char S_log_open = 0;
Guido van Rossumae94cf21998-05-08 21:52:55 +000060
61
Sean Reifscheider13daf122010-04-23 09:29:52 +000062static PyObject *
63syslog_get_argv(void)
64{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 /* 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 */
Sean Reifscheider13daf122010-04-23 09:29:52 +000070
Alexander Belopolskye239d232010-12-08 23:31:48 +000071 Py_ssize_t argv_len, scriptlen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 PyObject *scriptobj;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020073 Py_ssize_t slash;
Victor Stinnerbd303c12013-11-07 23:07:29 +010074 PyObject *argv = PySys_GetObject("argv");
Sean Reifscheider13daf122010-04-23 09:29:52 +000075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 if (argv == NULL) {
77 return(NULL);
78 }
Sean Reifscheider13daf122010-04-23 09:29:52 +000079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 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 }
Sean Reifscheider13daf122010-04-23 09:29:52 +000088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 scriptobj = PyList_GetItem(argv, 0);
90 if (!PyUnicode_Check(scriptobj)) {
91 return(NULL);
92 }
Victor Stinnerc4f281e2011-10-11 22:11:42 +020093 scriptlen = PyUnicode_GET_LENGTH(scriptobj);
Alexander Belopolskye239d232010-12-08 23:31:48 +000094 if (scriptlen == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 return(NULL);
96 }
Sean Reifscheider13daf122010-04-23 09:29:52 +000097
Victor Stinnerc4f281e2011-10-11 22:11:42 +020098 slash = PyUnicode_FindChar(scriptobj, SEP, 0, scriptlen, -1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020099 if (slash == -2)
100 return NULL;
101 if (slash != -1) {
Victor Stinnerc4f281e2011-10-11 22:11:42 +0200102 return PyUnicode_Substring(scriptobj, slash, scriptlen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 } else {
104 Py_INCREF(scriptobj);
105 return(scriptobj);
106 }
Sean Reifscheider13daf122010-04-23 09:29:52 +0000107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 return(NULL);
Sean Reifscheider13daf122010-04-23 09:29:52 +0000109}
110
111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112static PyObject *
Sean Reifscheider13daf122010-04-23 09:29:52 +0000113syslog_openlog(PyObject * self, PyObject * args, PyObject *kwds)
Guido van Rossume44e3731994-07-14 13:56:50 +0000114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 long logopt = 0;
116 long facility = LOG_USER;
117 PyObject *new_S_ident_o = NULL;
118 static char *keywords[] = {"ident", "logoption", "facility", 0};
Alexander Belopolskye239d232010-12-08 23:31:48 +0000119 char *ident = NULL;
Guido van Rossumc1822a41995-10-11 16:15:28 +0000120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 if (!PyArg_ParseTupleAndKeywords(args, kwds,
122 "|Ull:openlog", keywords, &new_S_ident_o, &logopt, &facility))
123 return NULL;
Guido van Rossumc1822a41995-10-11 16:15:28 +0000124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 if (new_S_ident_o) {
126 Py_INCREF(new_S_ident_o);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000127 }
Sean Reifscheider13daf122010-04-23 09:29:52 +0000128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 /* 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();
Alexander Belopolskye239d232010-12-08 23:31:48 +0000132 }
Sean Reifscheider13daf122010-04-23 09:29:52 +0000133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 Py_XDECREF(S_ident_o);
135 S_ident_o = new_S_ident_o;
Guido van Rossumc1822a41995-10-11 16:15:28 +0000136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 /* 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 */
Alexander Belopolskye239d232010-12-08 23:31:48 +0000141 if (S_ident_o) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200142 ident = PyUnicode_AsUTF8(S_ident_o);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000143 if (ident == NULL)
144 return NULL;
145 }
Barry Warsaw43a476a1997-01-09 23:51:21 +0000146
Alexander Belopolskye239d232010-12-08 23:31:48 +0000147 openlog(ident, logopt, facility);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 S_log_open = 1;
149
150 Py_INCREF(Py_None);
151 return Py_None;
Guido van Rossume44e3731994-07-14 13:56:50 +0000152}
153
Barry Warsaw43a476a1997-01-09 23:51:21 +0000154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000156syslog_syslog(PyObject * self, PyObject * args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 PyObject *message_object;
159 const char *message;
160 int priority = LOG_INFO;
Guido van Rossume44e3731994-07-14 13:56:50 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 if (!PyArg_ParseTuple(args, "iU;[priority,] message string",
163 &priority, &message_object)) {
164 PyErr_Clear();
165 if (!PyArg_ParseTuple(args, "U;[priority,] message string",
166 &message_object))
167 return NULL;
168 }
Guido van Rossumae94cf21998-05-08 21:52:55 +0000169
Serhiy Storchaka06515832016-11-20 09:13:07 +0200170 message = PyUnicode_AsUTF8(message_object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (message == NULL)
172 return NULL;
Sean Reifscheider13daf122010-04-23 09:29:52 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 /* if log is not opened, open it now */
175 if (!S_log_open) {
176 PyObject *openargs;
Sean Reifscheider13daf122010-04-23 09:29:52 +0000177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 /* Continue even if PyTuple_New fails, because openlog(3) is optional.
179 * So, we can still do loggin in the unlikely event things are so hosed
180 * that we can't do this tuple.
181 */
182 if ((openargs = PyTuple_New(0))) {
183 PyObject *openlog_ret = syslog_openlog(self, openargs, NULL);
184 Py_XDECREF(openlog_ret);
185 Py_DECREF(openargs);
186 }
187 }
Sean Reifscheider13daf122010-04-23 09:29:52 +0000188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 Py_BEGIN_ALLOW_THREADS;
190 syslog(priority, "%s", message);
191 Py_END_ALLOW_THREADS;
192 Py_RETURN_NONE;
Guido van Rossume44e3731994-07-14 13:56:50 +0000193}
194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000196syslog_closelog(PyObject *self, PyObject *unused)
Guido van Rossume44e3731994-07-14 13:56:50 +0000197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 if (S_log_open) {
199 closelog();
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200200 Py_CLEAR(S_ident_o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 S_log_open = 0;
202 }
203 Py_INCREF(Py_None);
204 return Py_None;
Guido van Rossume44e3731994-07-14 13:56:50 +0000205}
206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000208syslog_setlogmask(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 long maskpri, omaskpri;
Guido van Rossumc1822a41995-10-11 16:15:28 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
213 return NULL;
214 omaskpri = setlogmask(maskpri);
215 return PyLong_FromLong(omaskpri);
Guido van Rossume44e3731994-07-14 13:56:50 +0000216}
217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000219syslog_log_mask(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 long mask;
222 long pri;
223 if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
224 return NULL;
225 mask = LOG_MASK(pri);
226 return PyLong_FromLong(mask);
Guido van Rossume44e3731994-07-14 13:56:50 +0000227}
228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000230syslog_log_upto(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 long mask;
233 long pri;
234 if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
235 return NULL;
236 mask = LOG_UPTO(pri);
237 return PyLong_FromLong(mask);
Guido van Rossume44e3731994-07-14 13:56:50 +0000238}
239
240/* List of functions defined in the module */
241
Guido van Rossumf6971e21994-08-30 12:25:20 +0000242static PyMethodDef syslog_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 {"openlog", (PyCFunction) syslog_openlog, METH_VARARGS | METH_KEYWORDS},
244 {"closelog", syslog_closelog, METH_NOARGS},
245 {"syslog", syslog_syslog, METH_VARARGS},
246 {"setlogmask", syslog_setlogmask, METH_VARARGS},
247 {"LOG_MASK", syslog_log_mask, METH_VARARGS},
248 {"LOG_UPTO", syslog_log_upto, METH_VARARGS},
249 {NULL, NULL, 0}
Guido van Rossume44e3731994-07-14 13:56:50 +0000250};
251
Guido van Rossumae94cf21998-05-08 21:52:55 +0000252/* Initialization function for the module */
Guido van Rossumc1822a41995-10-11 16:15:28 +0000253
Martin v. Löwis1a214512008-06-11 05:26:20 +0000254
255static struct PyModuleDef syslogmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 PyModuleDef_HEAD_INIT,
257 "syslog",
258 NULL,
259 -1,
260 syslog_methods,
261 NULL,
262 NULL,
263 NULL,
264 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000265};
266
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000267PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000268PyInit_syslog(void)
Guido van Rossume44e3731994-07-14 13:56:50 +0000269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 PyObject *m;
Guido van Rossume44e3731994-07-14 13:56:50 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 /* Create the module and add the functions */
273 m = PyModule_Create(&syslogmodule);
274 if (m == NULL)
275 return NULL;
Guido van Rossume44e3731994-07-14 13:56:50 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 /* Add some symbolic constants to the module */
Guido van Rossumc1822a41995-10-11 16:15:28 +0000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 /* Priorities */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200280 PyModule_AddIntMacro(m, LOG_EMERG);
281 PyModule_AddIntMacro(m, LOG_ALERT);
282 PyModule_AddIntMacro(m, LOG_CRIT);
283 PyModule_AddIntMacro(m, LOG_ERR);
284 PyModule_AddIntMacro(m, LOG_WARNING);
285 PyModule_AddIntMacro(m, LOG_NOTICE);
286 PyModule_AddIntMacro(m, LOG_INFO);
287 PyModule_AddIntMacro(m, LOG_DEBUG);
Guido van Rossumc1822a41995-10-11 16:15:28 +0000288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 /* openlog() option flags */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200290 PyModule_AddIntMacro(m, LOG_PID);
291 PyModule_AddIntMacro(m, LOG_CONS);
292 PyModule_AddIntMacro(m, LOG_NDELAY);
R David Murrayeac09392012-03-29 07:15:45 -0400293#ifdef LOG_ODELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200294 PyModule_AddIntMacro(m, LOG_ODELAY);
R David Murrayeac09392012-03-29 07:15:45 -0400295#endif
Guido van Rossumbcc20741998-08-04 22:53:56 +0000296#ifdef LOG_NOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200297 PyModule_AddIntMacro(m, LOG_NOWAIT);
Guido van Rossumbcc20741998-08-04 22:53:56 +0000298#endif
Guido van Rossumc1822a41995-10-11 16:15:28 +0000299#ifdef LOG_PERROR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200300 PyModule_AddIntMacro(m, LOG_PERROR);
Guido van Rossumc1822a41995-10-11 16:15:28 +0000301#endif
302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 /* Facilities */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200304 PyModule_AddIntMacro(m, LOG_KERN);
305 PyModule_AddIntMacro(m, LOG_USER);
306 PyModule_AddIntMacro(m, LOG_MAIL);
307 PyModule_AddIntMacro(m, LOG_DAEMON);
308 PyModule_AddIntMacro(m, LOG_AUTH);
309 PyModule_AddIntMacro(m, LOG_LPR);
310 PyModule_AddIntMacro(m, LOG_LOCAL0);
311 PyModule_AddIntMacro(m, LOG_LOCAL1);
312 PyModule_AddIntMacro(m, LOG_LOCAL2);
313 PyModule_AddIntMacro(m, LOG_LOCAL3);
314 PyModule_AddIntMacro(m, LOG_LOCAL4);
315 PyModule_AddIntMacro(m, LOG_LOCAL5);
316 PyModule_AddIntMacro(m, LOG_LOCAL6);
317 PyModule_AddIntMacro(m, LOG_LOCAL7);
Guido van Rossume44e3731994-07-14 13:56:50 +0000318
Guido van Rossumae94cf21998-05-08 21:52:55 +0000319#ifndef LOG_SYSLOG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320#define LOG_SYSLOG LOG_DAEMON
Guido van Rossumae94cf21998-05-08 21:52:55 +0000321#endif
322#ifndef LOG_NEWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323#define LOG_NEWS LOG_MAIL
Guido van Rossumae94cf21998-05-08 21:52:55 +0000324#endif
325#ifndef LOG_UUCP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326#define LOG_UUCP LOG_MAIL
Guido van Rossumae94cf21998-05-08 21:52:55 +0000327#endif
328#ifndef LOG_CRON
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329#define LOG_CRON LOG_DAEMON
Guido van Rossumae94cf21998-05-08 21:52:55 +0000330#endif
331
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200332 PyModule_AddIntMacro(m, LOG_SYSLOG);
333 PyModule_AddIntMacro(m, LOG_CRON);
334 PyModule_AddIntMacro(m, LOG_UUCP);
335 PyModule_AddIntMacro(m, LOG_NEWS);
R David Murrayeac09392012-03-29 07:15:45 -0400336
337#ifdef LOG_AUTHPRIV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200338 PyModule_AddIntMacro(m, LOG_AUTHPRIV);
R David Murrayeac09392012-03-29 07:15:45 -0400339#endif
340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 return m;
Guido van Rossume44e3731994-07-14 13:56:50 +0000342}