blob: 1842dc95e8a8e0510cad6f70310402b2031c376a [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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 Py_ssize_t argv_len;
72 PyObject *scriptobj;
73 char *atslash;
74 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 }
93 if (PyUnicode_GET_SIZE(scriptobj) == 0) {
94 return(NULL);
95 }
Sean Reifscheider13daf122010-04-23 09:29:52 +000096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 atslash = strrchr(_PyUnicode_AsString(scriptobj), SEP);
98 if (atslash) {
99 return(PyUnicode_FromString(atslash + 1));
100 } else {
101 Py_INCREF(scriptobj);
102 return(scriptobj);
103 }
Sean Reifscheider13daf122010-04-23 09:29:52 +0000104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 return(NULL);
Sean Reifscheider13daf122010-04-23 09:29:52 +0000106}
107
108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109static PyObject *
Sean Reifscheider13daf122010-04-23 09:29:52 +0000110syslog_openlog(PyObject * self, PyObject * args, PyObject *kwds)
Guido van Rossume44e3731994-07-14 13:56:50 +0000111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 long logopt = 0;
113 long facility = LOG_USER;
114 PyObject *new_S_ident_o = NULL;
115 static char *keywords[] = {"ident", "logoption", "facility", 0};
Guido van Rossumc1822a41995-10-11 16:15:28 +0000116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 if (!PyArg_ParseTupleAndKeywords(args, kwds,
118 "|Ull:openlog", keywords, &new_S_ident_o, &logopt, &facility))
119 return NULL;
Guido van Rossumc1822a41995-10-11 16:15:28 +0000120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 if (new_S_ident_o) {
122 Py_INCREF(new_S_ident_o);
123 }
Sean Reifscheider13daf122010-04-23 09:29:52 +0000124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 /* get sys.argv[0] or NULL if we can't for some reason */
126 if (!new_S_ident_o) {
127 new_S_ident_o = syslog_get_argv();
128 }
Sean Reifscheider13daf122010-04-23 09:29:52 +0000129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 Py_XDECREF(S_ident_o);
131 S_ident_o = new_S_ident_o;
Guido van Rossumc1822a41995-10-11 16:15:28 +0000132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 /* At this point, S_ident_o should be INCREF()ed. openlog(3) does not
134 * make a copy, and syslog(3) later uses it. We can't garbagecollect it
135 * If NULL, just let openlog figure it out (probably using C argv[0]).
136 */
Barry Warsaw43a476a1997-01-09 23:51:21 +0000137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 openlog(S_ident_o ? _PyUnicode_AsString(S_ident_o) : NULL, logopt, facility);
139 S_log_open = 1;
140
141 Py_INCREF(Py_None);
142 return Py_None;
Guido van Rossume44e3731994-07-14 13:56:50 +0000143}
144
Barry Warsaw43a476a1997-01-09 23:51:21 +0000145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000147syslog_syslog(PyObject * self, PyObject * args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 PyObject *message_object;
150 const char *message;
151 int priority = LOG_INFO;
Guido van Rossume44e3731994-07-14 13:56:50 +0000152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (!PyArg_ParseTuple(args, "iU;[priority,] message string",
154 &priority, &message_object)) {
155 PyErr_Clear();
156 if (!PyArg_ParseTuple(args, "U;[priority,] message string",
157 &message_object))
158 return NULL;
159 }
Guido van Rossumae94cf21998-05-08 21:52:55 +0000160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 message = _PyUnicode_AsString(message_object);
162 if (message == NULL)
163 return NULL;
Sean Reifscheider13daf122010-04-23 09:29:52 +0000164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 /* if log is not opened, open it now */
166 if (!S_log_open) {
167 PyObject *openargs;
Sean Reifscheider13daf122010-04-23 09:29:52 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 /* Continue even if PyTuple_New fails, because openlog(3) is optional.
170 * So, we can still do loggin in the unlikely event things are so hosed
171 * that we can't do this tuple.
172 */
173 if ((openargs = PyTuple_New(0))) {
174 PyObject *openlog_ret = syslog_openlog(self, openargs, NULL);
175 Py_XDECREF(openlog_ret);
176 Py_DECREF(openargs);
177 }
178 }
Sean Reifscheider13daf122010-04-23 09:29:52 +0000179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 Py_BEGIN_ALLOW_THREADS;
181 syslog(priority, "%s", message);
182 Py_END_ALLOW_THREADS;
183 Py_RETURN_NONE;
Guido van Rossume44e3731994-07-14 13:56:50 +0000184}
185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000187syslog_closelog(PyObject *self, PyObject *unused)
Guido van Rossume44e3731994-07-14 13:56:50 +0000188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 if (S_log_open) {
190 closelog();
191 Py_XDECREF(S_ident_o);
192 S_ident_o = NULL;
193 S_log_open = 0;
194 }
195 Py_INCREF(Py_None);
196 return Py_None;
Guido van Rossume44e3731994-07-14 13:56:50 +0000197}
198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000200syslog_setlogmask(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 long maskpri, omaskpri;
Guido van Rossumc1822a41995-10-11 16:15:28 +0000203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
205 return NULL;
206 omaskpri = setlogmask(maskpri);
207 return PyLong_FromLong(omaskpri);
Guido van Rossume44e3731994-07-14 13:56:50 +0000208}
209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000211syslog_log_mask(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 long mask;
214 long pri;
215 if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
216 return NULL;
217 mask = LOG_MASK(pri);
218 return PyLong_FromLong(mask);
Guido van Rossume44e3731994-07-14 13:56:50 +0000219}
220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000222syslog_log_upto(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 long mask;
225 long pri;
226 if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
227 return NULL;
228 mask = LOG_UPTO(pri);
229 return PyLong_FromLong(mask);
Guido van Rossume44e3731994-07-14 13:56:50 +0000230}
231
232/* List of functions defined in the module */
233
Guido van Rossumf6971e21994-08-30 12:25:20 +0000234static PyMethodDef syslog_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 {"openlog", (PyCFunction) syslog_openlog, METH_VARARGS | METH_KEYWORDS},
236 {"closelog", syslog_closelog, METH_NOARGS},
237 {"syslog", syslog_syslog, METH_VARARGS},
238 {"setlogmask", syslog_setlogmask, METH_VARARGS},
239 {"LOG_MASK", syslog_log_mask, METH_VARARGS},
240 {"LOG_UPTO", syslog_log_upto, METH_VARARGS},
241 {NULL, NULL, 0}
Guido van Rossume44e3731994-07-14 13:56:50 +0000242};
243
Guido van Rossumae94cf21998-05-08 21:52:55 +0000244/* Initialization function for the module */
Guido van Rossumc1822a41995-10-11 16:15:28 +0000245
Martin v. Löwis1a214512008-06-11 05:26:20 +0000246
247static struct PyModuleDef syslogmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 PyModuleDef_HEAD_INIT,
249 "syslog",
250 NULL,
251 -1,
252 syslog_methods,
253 NULL,
254 NULL,
255 NULL,
256 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000257};
258
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000259PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000260PyInit_syslog(void)
Guido van Rossume44e3731994-07-14 13:56:50 +0000261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 PyObject *m;
Guido van Rossume44e3731994-07-14 13:56:50 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* Create the module and add the functions */
265 m = PyModule_Create(&syslogmodule);
266 if (m == NULL)
267 return NULL;
Guido van Rossume44e3731994-07-14 13:56:50 +0000268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 /* Add some symbolic constants to the module */
Guido van Rossumc1822a41995-10-11 16:15:28 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 /* Priorities */
272 PyModule_AddIntConstant(m, "LOG_EMERG", LOG_EMERG);
273 PyModule_AddIntConstant(m, "LOG_ALERT", LOG_ALERT);
274 PyModule_AddIntConstant(m, "LOG_CRIT", LOG_CRIT);
275 PyModule_AddIntConstant(m, "LOG_ERR", LOG_ERR);
276 PyModule_AddIntConstant(m, "LOG_WARNING", LOG_WARNING);
277 PyModule_AddIntConstant(m, "LOG_NOTICE", LOG_NOTICE);
278 PyModule_AddIntConstant(m, "LOG_INFO", LOG_INFO);
279 PyModule_AddIntConstant(m, "LOG_DEBUG", LOG_DEBUG);
Guido van Rossumc1822a41995-10-11 16:15:28 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 /* openlog() option flags */
282 PyModule_AddIntConstant(m, "LOG_PID", LOG_PID);
283 PyModule_AddIntConstant(m, "LOG_CONS", LOG_CONS);
284 PyModule_AddIntConstant(m, "LOG_NDELAY", LOG_NDELAY);
Guido van Rossumbcc20741998-08-04 22:53:56 +0000285#ifdef LOG_NOWAIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 PyModule_AddIntConstant(m, "LOG_NOWAIT", LOG_NOWAIT);
Guido van Rossumbcc20741998-08-04 22:53:56 +0000287#endif
Guido van Rossumc1822a41995-10-11 16:15:28 +0000288#ifdef LOG_PERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 PyModule_AddIntConstant(m, "LOG_PERROR", LOG_PERROR);
Guido van Rossumc1822a41995-10-11 16:15:28 +0000290#endif
291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 /* Facilities */
293 PyModule_AddIntConstant(m, "LOG_KERN", LOG_KERN);
294 PyModule_AddIntConstant(m, "LOG_USER", LOG_USER);
295 PyModule_AddIntConstant(m, "LOG_MAIL", LOG_MAIL);
296 PyModule_AddIntConstant(m, "LOG_DAEMON", LOG_DAEMON);
297 PyModule_AddIntConstant(m, "LOG_AUTH", LOG_AUTH);
298 PyModule_AddIntConstant(m, "LOG_LPR", LOG_LPR);
299 PyModule_AddIntConstant(m, "LOG_LOCAL0", LOG_LOCAL0);
300 PyModule_AddIntConstant(m, "LOG_LOCAL1", LOG_LOCAL1);
301 PyModule_AddIntConstant(m, "LOG_LOCAL2", LOG_LOCAL2);
302 PyModule_AddIntConstant(m, "LOG_LOCAL3", LOG_LOCAL3);
303 PyModule_AddIntConstant(m, "LOG_LOCAL4", LOG_LOCAL4);
304 PyModule_AddIntConstant(m, "LOG_LOCAL5", LOG_LOCAL5);
305 PyModule_AddIntConstant(m, "LOG_LOCAL6", LOG_LOCAL6);
306 PyModule_AddIntConstant(m, "LOG_LOCAL7", LOG_LOCAL7);
Guido van Rossume44e3731994-07-14 13:56:50 +0000307
Guido van Rossumae94cf21998-05-08 21:52:55 +0000308#ifndef LOG_SYSLOG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309#define LOG_SYSLOG LOG_DAEMON
Guido van Rossumae94cf21998-05-08 21:52:55 +0000310#endif
311#ifndef LOG_NEWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312#define LOG_NEWS LOG_MAIL
Guido van Rossumae94cf21998-05-08 21:52:55 +0000313#endif
314#ifndef LOG_UUCP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315#define LOG_UUCP LOG_MAIL
Guido van Rossumae94cf21998-05-08 21:52:55 +0000316#endif
317#ifndef LOG_CRON
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318#define LOG_CRON LOG_DAEMON
Guido van Rossumae94cf21998-05-08 21:52:55 +0000319#endif
320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 PyModule_AddIntConstant(m, "LOG_SYSLOG", LOG_SYSLOG);
322 PyModule_AddIntConstant(m, "LOG_CRON", LOG_CRON);
323 PyModule_AddIntConstant(m, "LOG_UUCP", LOG_UUCP);
324 PyModule_AddIntConstant(m, "LOG_NEWS", LOG_NEWS);
325 return m;
Guido van Rossume44e3731994-07-14 13:56:50 +0000326}