blob: 5532d03a27a4c9b3f2ea54778523907bb20b340a [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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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
Guido van Rossumae94cf21998-05-08 21:52:55 +0000291998/04/28 (Sean Reifschneider)
30 - When facility not specified to syslog() method, use default from openlog()
31 (This is how it was claimed to work in the documentation)
32 - Potential resource leak of o_ident, now cleaned up in closelog()
33 - Minor comment accuracy fix.
34
Guido van Rossumc1822a41995-10-11 16:15:28 +00003595/06/29 (Steve Clift)
36 - Changed arg parsing to use PyArg_ParseTuple.
37 - Added PyErr_Clear() call(s) where needed.
38 - Fix core dumps if user message contains format specifiers.
Thomas Wouters7e474022000-07-16 12:04:32 +000039 - Change openlog arg defaults to match normal syslog behavior.
Guido van Rossumc1822a41995-10-11 16:15:28 +000040 - Plug memory leak in openlog().
41 - Fix setlogmask() to return previous mask value.
42
43******************************************************************/
44
Guido van Rossume44e3731994-07-14 13:56:50 +000045/* syslog module */
Guido van Rossume44e3731994-07-14 13:56:50 +000046
Guido van Rossuma597dde1995-01-10 20:56:29 +000047#include "Python.h"
Guido van Rossume44e3731994-07-14 13:56:50 +000048
49#include <syslog.h>
50
Guido van Rossumae94cf21998-05-08 21:52:55 +000051/* only one instance, only one syslog, so globals should be ok */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000052static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */
Guido van Rossumae94cf21998-05-08 21:52:55 +000053
54
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000055static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +000056syslog_openlog(PyObject * self, PyObject * args)
Guido van Rossume44e3731994-07-14 13:56:50 +000057{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000058 long logopt = 0;
59 long facility = LOG_USER;
60 PyObject *new_S_ident_o;
61 const char *ident;
Guido van Rossumc1822a41995-10-11 16:15:28 +000062
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000063 if (!PyArg_ParseTuple(args,
64 "U|ll;ident string [, logoption [, facility]]",
65 &new_S_ident_o, &logopt, &facility))
66 return NULL;
Guido van Rossumc1822a41995-10-11 16:15:28 +000067
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000068 /* This is needed because openlog() does NOT make a copy
69 * and syslog() later uses it.. cannot trash it.
70 */
71 Py_XDECREF(S_ident_o);
72 S_ident_o = new_S_ident_o;
73 Py_INCREF(S_ident_o);
Guido van Rossumc1822a41995-10-11 16:15:28 +000074
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000075 ident = _PyUnicode_AsString(S_ident_o);
76 if (ident == NULL)
77 return NULL;
78 openlog(ident, logopt, facility);
Barry Warsaw43a476a1997-01-09 23:51:21 +000079
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000080 Py_INCREF(Py_None);
81 return Py_None;
Guido van Rossume44e3731994-07-14 13:56:50 +000082}
83
Barry Warsaw43a476a1997-01-09 23:51:21 +000084
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000085static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +000086syslog_syslog(PyObject * self, PyObject * args)
Guido van Rossume44e3731994-07-14 13:56:50 +000087{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000088 PyObject *message_object;
89 const char *message;
90 int priority = LOG_INFO;
Guido van Rossume44e3731994-07-14 13:56:50 +000091
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000092 if (!PyArg_ParseTuple(args, "iU;[priority,] message string",
93 &priority, &message_object)) {
94 PyErr_Clear();
95 if (!PyArg_ParseTuple(args, "U;[priority,] message string",
96 &message_object))
97 return NULL;
98 }
Guido van Rossumae94cf21998-05-08 21:52:55 +000099
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000100 message = _PyUnicode_AsString(message_object);
101 if (message == NULL)
102 return NULL;
103 Py_BEGIN_ALLOW_THREADS;
104 syslog(priority, "%s", message);
105 Py_END_ALLOW_THREADS;
106 Py_RETURN_NONE;
Guido van Rossume44e3731994-07-14 13:56:50 +0000107}
108
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000109static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000110syslog_closelog(PyObject *self, PyObject *unused)
Guido van Rossume44e3731994-07-14 13:56:50 +0000111{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000112 closelog();
113 Py_XDECREF(S_ident_o);
114 S_ident_o = NULL;
115 Py_INCREF(Py_None);
116 return Py_None;
Guido van Rossume44e3731994-07-14 13:56:50 +0000117}
118
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000119static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000120syslog_setlogmask(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000121{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000122 long maskpri, omaskpri;
Guido van Rossumc1822a41995-10-11 16:15:28 +0000123
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000124 if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
125 return NULL;
126 omaskpri = setlogmask(maskpri);
127 return PyLong_FromLong(omaskpri);
Guido van Rossume44e3731994-07-14 13:56:50 +0000128}
129
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000130static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000131syslog_log_mask(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000132{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000133 long mask;
134 long pri;
135 if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
136 return NULL;
137 mask = LOG_MASK(pri);
138 return PyLong_FromLong(mask);
Guido van Rossume44e3731994-07-14 13:56:50 +0000139}
140
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000141static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000142syslog_log_upto(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000143{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000144 long mask;
145 long pri;
146 if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
147 return NULL;
148 mask = LOG_UPTO(pri);
149 return PyLong_FromLong(mask);
Guido van Rossume44e3731994-07-14 13:56:50 +0000150}
151
152/* List of functions defined in the module */
153
Guido van Rossumf6971e21994-08-30 12:25:20 +0000154static PyMethodDef syslog_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000155 {"openlog", syslog_openlog, METH_VARARGS},
156 {"closelog", syslog_closelog, METH_NOARGS},
157 {"syslog", syslog_syslog, METH_VARARGS},
158 {"setlogmask", syslog_setlogmask, METH_VARARGS},
159 {"LOG_MASK", syslog_log_mask, METH_VARARGS},
160 {"LOG_UPTO", syslog_log_upto, METH_VARARGS},
161 {NULL, NULL, 0}
Guido van Rossume44e3731994-07-14 13:56:50 +0000162};
163
Guido van Rossumae94cf21998-05-08 21:52:55 +0000164/* Initialization function for the module */
Guido van Rossumc1822a41995-10-11 16:15:28 +0000165
Martin v. Löwis1a214512008-06-11 05:26:20 +0000166
167static struct PyModuleDef syslogmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000168 PyModuleDef_HEAD_INIT,
169 "syslog",
170 NULL,
171 -1,
172 syslog_methods,
173 NULL,
174 NULL,
175 NULL,
176 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000177};
178
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000179PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000180PyInit_syslog(void)
Guido van Rossume44e3731994-07-14 13:56:50 +0000181{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000182 PyObject *m;
Guido van Rossume44e3731994-07-14 13:56:50 +0000183
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000184 /* Create the module and add the functions */
185 m = PyModule_Create(&syslogmodule);
186 if (m == NULL)
187 return NULL;
Guido van Rossume44e3731994-07-14 13:56:50 +0000188
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000189 /* Add some symbolic constants to the module */
Guido van Rossumc1822a41995-10-11 16:15:28 +0000190
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000191 /* Priorities */
192 PyModule_AddIntConstant(m, "LOG_EMERG", LOG_EMERG);
193 PyModule_AddIntConstant(m, "LOG_ALERT", LOG_ALERT);
194 PyModule_AddIntConstant(m, "LOG_CRIT", LOG_CRIT);
195 PyModule_AddIntConstant(m, "LOG_ERR", LOG_ERR);
196 PyModule_AddIntConstant(m, "LOG_WARNING", LOG_WARNING);
197 PyModule_AddIntConstant(m, "LOG_NOTICE", LOG_NOTICE);
198 PyModule_AddIntConstant(m, "LOG_INFO", LOG_INFO);
199 PyModule_AddIntConstant(m, "LOG_DEBUG", LOG_DEBUG);
Guido van Rossumc1822a41995-10-11 16:15:28 +0000200
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000201 /* openlog() option flags */
202 PyModule_AddIntConstant(m, "LOG_PID", LOG_PID);
203 PyModule_AddIntConstant(m, "LOG_CONS", LOG_CONS);
204 PyModule_AddIntConstant(m, "LOG_NDELAY", LOG_NDELAY);
Guido van Rossumbcc20741998-08-04 22:53:56 +0000205#ifdef LOG_NOWAIT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000206 PyModule_AddIntConstant(m, "LOG_NOWAIT", LOG_NOWAIT);
Guido van Rossumbcc20741998-08-04 22:53:56 +0000207#endif
Guido van Rossumc1822a41995-10-11 16:15:28 +0000208#ifdef LOG_PERROR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000209 PyModule_AddIntConstant(m, "LOG_PERROR", LOG_PERROR);
Guido van Rossumc1822a41995-10-11 16:15:28 +0000210#endif
211
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000212 /* Facilities */
213 PyModule_AddIntConstant(m, "LOG_KERN", LOG_KERN);
214 PyModule_AddIntConstant(m, "LOG_USER", LOG_USER);
215 PyModule_AddIntConstant(m, "LOG_MAIL", LOG_MAIL);
216 PyModule_AddIntConstant(m, "LOG_DAEMON", LOG_DAEMON);
217 PyModule_AddIntConstant(m, "LOG_AUTH", LOG_AUTH);
218 PyModule_AddIntConstant(m, "LOG_LPR", LOG_LPR);
219 PyModule_AddIntConstant(m, "LOG_LOCAL0", LOG_LOCAL0);
220 PyModule_AddIntConstant(m, "LOG_LOCAL1", LOG_LOCAL1);
221 PyModule_AddIntConstant(m, "LOG_LOCAL2", LOG_LOCAL2);
222 PyModule_AddIntConstant(m, "LOG_LOCAL3", LOG_LOCAL3);
223 PyModule_AddIntConstant(m, "LOG_LOCAL4", LOG_LOCAL4);
224 PyModule_AddIntConstant(m, "LOG_LOCAL5", LOG_LOCAL5);
225 PyModule_AddIntConstant(m, "LOG_LOCAL6", LOG_LOCAL6);
226 PyModule_AddIntConstant(m, "LOG_LOCAL7", LOG_LOCAL7);
Guido van Rossume44e3731994-07-14 13:56:50 +0000227
Guido van Rossumae94cf21998-05-08 21:52:55 +0000228#ifndef LOG_SYSLOG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000229#define LOG_SYSLOG LOG_DAEMON
Guido van Rossumae94cf21998-05-08 21:52:55 +0000230#endif
231#ifndef LOG_NEWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000232#define LOG_NEWS LOG_MAIL
Guido van Rossumae94cf21998-05-08 21:52:55 +0000233#endif
234#ifndef LOG_UUCP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000235#define LOG_UUCP LOG_MAIL
Guido van Rossumae94cf21998-05-08 21:52:55 +0000236#endif
237#ifndef LOG_CRON
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000238#define LOG_CRON LOG_DAEMON
Guido van Rossumae94cf21998-05-08 21:52:55 +0000239#endif
240
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000241 PyModule_AddIntConstant(m, "LOG_SYSLOG", LOG_SYSLOG);
242 PyModule_AddIntConstant(m, "LOG_CRON", LOG_CRON);
243 PyModule_AddIntConstant(m, "LOG_UUCP", LOG_UUCP);
244 PyModule_AddIntConstant(m, "LOG_NEWS", LOG_NEWS);
245 return m;
Guido van Rossume44e3731994-07-14 13:56:50 +0000246}