blob: 2281c1deef63ebad332ab22970f5d573585f3448 [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
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both 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
12not be used in advertising or publicity pertaining to distribution
13of 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
Guido van Rossumf6971e21994-08-30 12:25:20 +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
21WITH 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 */
58static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */
59
60
Sean Reifscheider13daf122010-04-23 09:29:52 +000061static PyObject *
62syslog_get_argv(void)
63{
64 /* Figure out what to use for as the program "ident" for openlog().
65 * This swallows exceptions and continues rather than failing out,
66 * because the syslog module can still be used because openlog(3)
67 * is optional.
68 */
69
70 Py_ssize_t argv_len;
71 PyObject *scriptobj;
72 char *atslash;
73 PyObject *argv = PySys_GetObject("argv");
74
75 if (argv == NULL) {
76 return(NULL);
77 }
78
79 argv_len = PyList_Size(argv);
80 if (argv_len == -1) {
81 PyErr_Clear();
82 return(NULL);
83 }
84 if (argv_len == 0) {
85 return(NULL);
86 }
87
88 scriptobj = PyList_GetItem(argv, 0);
89 if (!PyUnicode_Check(scriptobj)) {
90 return(NULL);
91 }
92 if (PyUnicode_GET_SIZE(scriptobj) == 0) {
93 return(NULL);
94 }
95
96 atslash = strrchr(_PyUnicode_AsString(scriptobj), SEP);
97 if (atslash) {
98 return(PyUnicode_FromString(atslash + 1));
99 } else {
100 Py_INCREF(scriptobj);
101 return(scriptobj);
102 }
103
104 return(NULL);
105}
106
107
Guido van Rossumf6971e21994-08-30 12:25:20 +0000108static PyObject *
Sean Reifscheider13daf122010-04-23 09:29:52 +0000109syslog_openlog(PyObject * self, PyObject * args, PyObject *kwds)
Guido van Rossume44e3731994-07-14 13:56:50 +0000110{
Barry Warsaw43a476a1997-01-09 23:51:21 +0000111 long logopt = 0;
112 long facility = LOG_USER;
Sean Reifscheider13daf122010-04-23 09:29:52 +0000113 PyObject *new_S_ident_o = NULL;
114 static char *keywords[] = {"ident", "logoption", "facility", 0};
Guido van Rossumc1822a41995-10-11 16:15:28 +0000115
Sean Reifscheider13daf122010-04-23 09:29:52 +0000116 if (!PyArg_ParseTupleAndKeywords(args, kwds,
117 "|Ull:openlog", keywords, &new_S_ident_o, &logopt, &facility))
Barry Warsaw43a476a1997-01-09 23:51:21 +0000118 return NULL;
Guido van Rossumc1822a41995-10-11 16:15:28 +0000119
Sean Reifscheider13daf122010-04-23 09:29:52 +0000120 if (new_S_ident_o) {
121 Py_INCREF(new_S_ident_o);
122 }
123
124 /* get sys.argv[0] or NULL if we can't for some reason */
125 if (!new_S_ident_o) {
126 new_S_ident_o = syslog_get_argv();
127 }
128
Raymond Hettinger6f5b7412004-12-16 23:52:04 +0000129 Py_XDECREF(S_ident_o);
130 S_ident_o = new_S_ident_o;
Guido van Rossumc1822a41995-10-11 16:15:28 +0000131
Sean Reifscheider13daf122010-04-23 09:29:52 +0000132 /* At this point, S_ident_o should be INCREF()ed. openlog(3) does not
133 * make a copy, and syslog(3) later uses it. We can't garbagecollect it
134 * If NULL, just let openlog figure it out (probably using C argv[0]).
135 */
136
137 openlog(S_ident_o ? _PyUnicode_AsString(S_ident_o) : NULL, logopt, facility);
Barry Warsaw43a476a1997-01-09 23:51:21 +0000138
139 Py_INCREF(Py_None);
140 return Py_None;
Guido van Rossume44e3731994-07-14 13:56:50 +0000141}
142
Barry Warsaw43a476a1997-01-09 23:51:21 +0000143
Guido van Rossumf6971e21994-08-30 12:25:20 +0000144static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000145syslog_syslog(PyObject * self, PyObject * args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000146{
Neal Norwitz8d3654d2007-08-25 00:21:36 +0000147 PyObject *message_object;
148 const char *message;
Guido van Rossumae94cf21998-05-08 21:52:55 +0000149 int priority = LOG_INFO;
Guido van Rossume44e3731994-07-14 13:56:50 +0000150
Neal Norwitz8d3654d2007-08-25 00:21:36 +0000151 if (!PyArg_ParseTuple(args, "iU;[priority,] message string",
Neal Norwitzfa06e5f2007-08-25 00:29:58 +0000152 &priority, &message_object)) {
Barry Warsaw43a476a1997-01-09 23:51:21 +0000153 PyErr_Clear();
Neal Norwitz8d3654d2007-08-25 00:21:36 +0000154 if (!PyArg_ParseTuple(args, "U;[priority,] message string",
Neal Norwitzfa06e5f2007-08-25 00:29:58 +0000155 &message_object))
Barry Warsaw43a476a1997-01-09 23:51:21 +0000156 return NULL;
157 }
Guido van Rossumae94cf21998-05-08 21:52:55 +0000158
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000159 message = _PyUnicode_AsString(message_object);
Neal Norwitz8d3654d2007-08-25 00:21:36 +0000160 if (message == NULL)
161 return NULL;
Sean Reifscheider13daf122010-04-23 09:29:52 +0000162
163 /* call openlog if no current identifier */
164 if (!S_ident_o) {
165 PyObject *openargs;
166
167 /* Continue even if PyTuple_New fails, because openlog(3) is optional.
168 * So, we can still do loggin in the unlikely event things are so hosed
169 * that we can't do this tuple.
170 */
171 if ((openargs = PyTuple_New(0))) {
172 PyObject *openlog_ret = syslog_openlog(self, openargs, NULL);
173 Py_XDECREF(openlog_ret);
174 Py_DECREF(openargs);
175 }
176 }
177
Christian Heimes05e8be12008-02-23 18:30:17 +0000178 Py_BEGIN_ALLOW_THREADS;
Barry Warsaw43a476a1997-01-09 23:51:21 +0000179 syslog(priority, "%s", message);
Christian Heimes05e8be12008-02-23 18:30:17 +0000180 Py_END_ALLOW_THREADS;
181 Py_RETURN_NONE;
Guido van Rossume44e3731994-07-14 13:56:50 +0000182}
183
Guido van Rossumf6971e21994-08-30 12:25:20 +0000184static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000185syslog_closelog(PyObject *self, PyObject *unused)
Guido van Rossume44e3731994-07-14 13:56:50 +0000186{
Guido van Rossume44e3731994-07-14 13:56:50 +0000187 closelog();
Guido van Rossumae94cf21998-05-08 21:52:55 +0000188 Py_XDECREF(S_ident_o);
189 S_ident_o = NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000190 Py_INCREF(Py_None);
191 return Py_None;
Guido van Rossume44e3731994-07-14 13:56:50 +0000192}
193
Guido van Rossumf6971e21994-08-30 12:25:20 +0000194static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000195syslog_setlogmask(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000196{
Barry Warsaw43a476a1997-01-09 23:51:21 +0000197 long maskpri, omaskpri;
Guido van Rossumc1822a41995-10-11 16:15:28 +0000198
Barry Warsaw43a476a1997-01-09 23:51:21 +0000199 if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
200 return NULL;
201 omaskpri = setlogmask(maskpri);
Christian Heimes217cfd12007-12-02 14:31:20 +0000202 return PyLong_FromLong(omaskpri);
Guido van Rossume44e3731994-07-14 13:56:50 +0000203}
204
Guido van Rossumf6971e21994-08-30 12:25:20 +0000205static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000206syslog_log_mask(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000207{
Barry Warsaw43a476a1997-01-09 23:51:21 +0000208 long mask;
209 long pri;
Guido van Rossum43713e52000-02-29 13:59:29 +0000210 if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
Barry Warsaw43a476a1997-01-09 23:51:21 +0000211 return NULL;
212 mask = LOG_MASK(pri);
Christian Heimes217cfd12007-12-02 14:31:20 +0000213 return PyLong_FromLong(mask);
Guido van Rossume44e3731994-07-14 13:56:50 +0000214}
215
Guido van Rossumf6971e21994-08-30 12:25:20 +0000216static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000217syslog_log_upto(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000218{
Barry Warsaw43a476a1997-01-09 23:51:21 +0000219 long mask;
220 long pri;
Guido van Rossum43713e52000-02-29 13:59:29 +0000221 if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
Barry Warsaw43a476a1997-01-09 23:51:21 +0000222 return NULL;
223 mask = LOG_UPTO(pri);
Christian Heimes217cfd12007-12-02 14:31:20 +0000224 return PyLong_FromLong(mask);
Guido van Rossume44e3731994-07-14 13:56:50 +0000225}
226
227/* List of functions defined in the module */
228
Guido van Rossumf6971e21994-08-30 12:25:20 +0000229static PyMethodDef syslog_methods[] = {
Sean Reifscheider13daf122010-04-23 09:29:52 +0000230 {"openlog", (PyCFunction) syslog_openlog, METH_VARARGS | METH_KEYWORDS},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000231 {"closelog", syslog_closelog, METH_NOARGS},
Guido van Rossumc1822a41995-10-11 16:15:28 +0000232 {"syslog", syslog_syslog, METH_VARARGS},
233 {"setlogmask", syslog_setlogmask, METH_VARARGS},
234 {"LOG_MASK", syslog_log_mask, METH_VARARGS},
235 {"LOG_UPTO", syslog_log_upto, METH_VARARGS},
236 {NULL, NULL, 0}
Guido van Rossume44e3731994-07-14 13:56:50 +0000237};
238
Guido van Rossumae94cf21998-05-08 21:52:55 +0000239/* Initialization function for the module */
Guido van Rossumc1822a41995-10-11 16:15:28 +0000240
Martin v. Löwis1a214512008-06-11 05:26:20 +0000241
242static struct PyModuleDef syslogmodule = {
243 PyModuleDef_HEAD_INIT,
244 "syslog",
245 NULL,
246 -1,
247 syslog_methods,
248 NULL,
249 NULL,
250 NULL,
251 NULL
252};
253
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000254PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000255PyInit_syslog(void)
Guido van Rossume44e3731994-07-14 13:56:50 +0000256{
Fred Drake4baedc12002-04-01 14:53:37 +0000257 PyObject *m;
Guido van Rossume44e3731994-07-14 13:56:50 +0000258
259 /* Create the module and add the functions */
Martin v. Löwis1a214512008-06-11 05:26:20 +0000260 m = PyModule_Create(&syslogmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000261 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000262 return NULL;
Guido van Rossume44e3731994-07-14 13:56:50 +0000263
264 /* Add some symbolic constants to the module */
Guido van Rossumc1822a41995-10-11 16:15:28 +0000265
266 /* Priorities */
Fred Drake4baedc12002-04-01 14:53:37 +0000267 PyModule_AddIntConstant(m, "LOG_EMERG", LOG_EMERG);
268 PyModule_AddIntConstant(m, "LOG_ALERT", LOG_ALERT);
269 PyModule_AddIntConstant(m, "LOG_CRIT", LOG_CRIT);
270 PyModule_AddIntConstant(m, "LOG_ERR", LOG_ERR);
271 PyModule_AddIntConstant(m, "LOG_WARNING", LOG_WARNING);
272 PyModule_AddIntConstant(m, "LOG_NOTICE", LOG_NOTICE);
273 PyModule_AddIntConstant(m, "LOG_INFO", LOG_INFO);
274 PyModule_AddIntConstant(m, "LOG_DEBUG", LOG_DEBUG);
Guido van Rossumc1822a41995-10-11 16:15:28 +0000275
276 /* openlog() option flags */
Fred Drake4baedc12002-04-01 14:53:37 +0000277 PyModule_AddIntConstant(m, "LOG_PID", LOG_PID);
278 PyModule_AddIntConstant(m, "LOG_CONS", LOG_CONS);
279 PyModule_AddIntConstant(m, "LOG_NDELAY", LOG_NDELAY);
Guido van Rossumbcc20741998-08-04 22:53:56 +0000280#ifdef LOG_NOWAIT
Fred Drake4baedc12002-04-01 14:53:37 +0000281 PyModule_AddIntConstant(m, "LOG_NOWAIT", LOG_NOWAIT);
Guido van Rossumbcc20741998-08-04 22:53:56 +0000282#endif
Guido van Rossumc1822a41995-10-11 16:15:28 +0000283#ifdef LOG_PERROR
Fred Drake4baedc12002-04-01 14:53:37 +0000284 PyModule_AddIntConstant(m, "LOG_PERROR", LOG_PERROR);
Guido van Rossumc1822a41995-10-11 16:15:28 +0000285#endif
286
287 /* Facilities */
Fred Drake4baedc12002-04-01 14:53:37 +0000288 PyModule_AddIntConstant(m, "LOG_KERN", LOG_KERN);
289 PyModule_AddIntConstant(m, "LOG_USER", LOG_USER);
290 PyModule_AddIntConstant(m, "LOG_MAIL", LOG_MAIL);
291 PyModule_AddIntConstant(m, "LOG_DAEMON", LOG_DAEMON);
292 PyModule_AddIntConstant(m, "LOG_AUTH", LOG_AUTH);
293 PyModule_AddIntConstant(m, "LOG_LPR", LOG_LPR);
294 PyModule_AddIntConstant(m, "LOG_LOCAL0", LOG_LOCAL0);
295 PyModule_AddIntConstant(m, "LOG_LOCAL1", LOG_LOCAL1);
296 PyModule_AddIntConstant(m, "LOG_LOCAL2", LOG_LOCAL2);
297 PyModule_AddIntConstant(m, "LOG_LOCAL3", LOG_LOCAL3);
298 PyModule_AddIntConstant(m, "LOG_LOCAL4", LOG_LOCAL4);
299 PyModule_AddIntConstant(m, "LOG_LOCAL5", LOG_LOCAL5);
300 PyModule_AddIntConstant(m, "LOG_LOCAL6", LOG_LOCAL6);
301 PyModule_AddIntConstant(m, "LOG_LOCAL7", LOG_LOCAL7);
Guido van Rossume44e3731994-07-14 13:56:50 +0000302
Guido van Rossumae94cf21998-05-08 21:52:55 +0000303#ifndef LOG_SYSLOG
304#define LOG_SYSLOG LOG_DAEMON
305#endif
306#ifndef LOG_NEWS
307#define LOG_NEWS LOG_MAIL
308#endif
309#ifndef LOG_UUCP
310#define LOG_UUCP LOG_MAIL
311#endif
312#ifndef LOG_CRON
313#define LOG_CRON LOG_DAEMON
314#endif
315
Fred Drake4baedc12002-04-01 14:53:37 +0000316 PyModule_AddIntConstant(m, "LOG_SYSLOG", LOG_SYSLOG);
317 PyModule_AddIntConstant(m, "LOG_CRON", LOG_CRON);
318 PyModule_AddIntConstant(m, "LOG_UUCP", LOG_UUCP);
319 PyModule_AddIntConstant(m, "LOG_NEWS", LOG_NEWS);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000320 return m;
Guido van Rossume44e3731994-07-14 13:56:50 +0000321}