1 | n/a | /*********************************************************** |
---|
2 | n/a | Copyright 1994 by Lance Ellinghouse, |
---|
3 | n/a | Cathedral City, California Republic, United States of America. |
---|
4 | n/a | |
---|
5 | n/a | All Rights Reserved |
---|
6 | n/a | |
---|
7 | n/a | Permission to use, copy, modify, and distribute this software and its |
---|
8 | n/a | documentation for any purpose and without fee is hereby granted, |
---|
9 | n/a | provided that the above copyright notice appear in all copies and that |
---|
10 | n/a | both that copyright notice and this permission notice appear in |
---|
11 | n/a | supporting documentation, and that the name of Lance Ellinghouse |
---|
12 | n/a | not be used in advertising or publicity pertaining to distribution |
---|
13 | n/a | of the software without specific, written prior permission. |
---|
14 | n/a | |
---|
15 | n/a | LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO |
---|
16 | n/a | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
---|
17 | n/a | FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL, |
---|
18 | n/a | INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING |
---|
19 | n/a | FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
---|
20 | n/a | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
---|
21 | n/a | WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
22 | n/a | |
---|
23 | n/a | ******************************************************************/ |
---|
24 | n/a | |
---|
25 | n/a | /****************************************************************** |
---|
26 | n/a | |
---|
27 | n/a | Revision history: |
---|
28 | n/a | |
---|
29 | n/a | 2010/04/20 (Sean Reifschneider) |
---|
30 | n/a | - Use basename(sys.argv[0]) for the default "ident". |
---|
31 | n/a | - Arguments to openlog() are now keyword args and are all optional. |
---|
32 | n/a | - syslog() calls openlog() if it hasn't already been called. |
---|
33 | n/a | |
---|
34 | n/a | 1998/04/28 (Sean Reifschneider) |
---|
35 | n/a | - When facility not specified to syslog() method, use default from openlog() |
---|
36 | n/a | (This is how it was claimed to work in the documentation) |
---|
37 | n/a | - Potential resource leak of o_ident, now cleaned up in closelog() |
---|
38 | n/a | - Minor comment accuracy fix. |
---|
39 | n/a | |
---|
40 | n/a | 95/06/29 (Steve Clift) |
---|
41 | n/a | - Changed arg parsing to use PyArg_ParseTuple. |
---|
42 | n/a | - Added PyErr_Clear() call(s) where needed. |
---|
43 | n/a | - Fix core dumps if user message contains format specifiers. |
---|
44 | n/a | - Change openlog arg defaults to match normal syslog behavior. |
---|
45 | n/a | - Plug memory leak in openlog(). |
---|
46 | n/a | - Fix setlogmask() to return previous mask value. |
---|
47 | n/a | |
---|
48 | n/a | ******************************************************************/ |
---|
49 | n/a | |
---|
50 | n/a | /* syslog module */ |
---|
51 | n/a | |
---|
52 | n/a | #include "Python.h" |
---|
53 | n/a | #include "osdefs.h" |
---|
54 | n/a | |
---|
55 | n/a | #include <syslog.h> |
---|
56 | n/a | |
---|
57 | n/a | /* only one instance, only one syslog, so globals should be ok */ |
---|
58 | n/a | static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */ |
---|
59 | n/a | static char S_log_open = 0; |
---|
60 | n/a | |
---|
61 | n/a | |
---|
62 | n/a | static PyObject * |
---|
63 | n/a | syslog_get_argv(void) |
---|
64 | n/a | { |
---|
65 | n/a | /* Figure out what to use for as the program "ident" for openlog(). |
---|
66 | n/a | * This swallows exceptions and continues rather than failing out, |
---|
67 | n/a | * because the syslog module can still be used because openlog(3) |
---|
68 | n/a | * is optional. |
---|
69 | n/a | */ |
---|
70 | n/a | |
---|
71 | n/a | Py_ssize_t argv_len, scriptlen; |
---|
72 | n/a | PyObject *scriptobj; |
---|
73 | n/a | Py_ssize_t slash; |
---|
74 | n/a | PyObject *argv = PySys_GetObject("argv"); |
---|
75 | n/a | |
---|
76 | n/a | if (argv == NULL) { |
---|
77 | n/a | return(NULL); |
---|
78 | n/a | } |
---|
79 | n/a | |
---|
80 | n/a | argv_len = PyList_Size(argv); |
---|
81 | n/a | if (argv_len == -1) { |
---|
82 | n/a | PyErr_Clear(); |
---|
83 | n/a | return(NULL); |
---|
84 | n/a | } |
---|
85 | n/a | if (argv_len == 0) { |
---|
86 | n/a | return(NULL); |
---|
87 | n/a | } |
---|
88 | n/a | |
---|
89 | n/a | scriptobj = PyList_GetItem(argv, 0); |
---|
90 | n/a | if (!PyUnicode_Check(scriptobj)) { |
---|
91 | n/a | return(NULL); |
---|
92 | n/a | } |
---|
93 | n/a | scriptlen = PyUnicode_GET_LENGTH(scriptobj); |
---|
94 | n/a | if (scriptlen == 0) { |
---|
95 | n/a | return(NULL); |
---|
96 | n/a | } |
---|
97 | n/a | |
---|
98 | n/a | slash = PyUnicode_FindChar(scriptobj, SEP, 0, scriptlen, -1); |
---|
99 | n/a | if (slash == -2) |
---|
100 | n/a | return NULL; |
---|
101 | n/a | if (slash != -1) { |
---|
102 | n/a | return PyUnicode_Substring(scriptobj, slash, scriptlen); |
---|
103 | n/a | } else { |
---|
104 | n/a | Py_INCREF(scriptobj); |
---|
105 | n/a | return(scriptobj); |
---|
106 | n/a | } |
---|
107 | n/a | |
---|
108 | n/a | return(NULL); |
---|
109 | n/a | } |
---|
110 | n/a | |
---|
111 | n/a | |
---|
112 | n/a | static PyObject * |
---|
113 | n/a | syslog_openlog(PyObject * self, PyObject * args, PyObject *kwds) |
---|
114 | n/a | { |
---|
115 | n/a | long logopt = 0; |
---|
116 | n/a | long facility = LOG_USER; |
---|
117 | n/a | PyObject *new_S_ident_o = NULL; |
---|
118 | n/a | static char *keywords[] = {"ident", "logoption", "facility", 0}; |
---|
119 | n/a | const char *ident = NULL; |
---|
120 | n/a | |
---|
121 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
---|
122 | n/a | "|Ull:openlog", keywords, &new_S_ident_o, &logopt, &facility)) |
---|
123 | n/a | return NULL; |
---|
124 | n/a | |
---|
125 | n/a | if (new_S_ident_o) { |
---|
126 | n/a | Py_INCREF(new_S_ident_o); |
---|
127 | n/a | } |
---|
128 | n/a | |
---|
129 | n/a | /* get sys.argv[0] or NULL if we can't for some reason */ |
---|
130 | n/a | if (!new_S_ident_o) { |
---|
131 | n/a | new_S_ident_o = syslog_get_argv(); |
---|
132 | n/a | } |
---|
133 | n/a | |
---|
134 | n/a | Py_XDECREF(S_ident_o); |
---|
135 | n/a | S_ident_o = new_S_ident_o; |
---|
136 | n/a | |
---|
137 | n/a | /* At this point, S_ident_o should be INCREF()ed. openlog(3) does not |
---|
138 | n/a | * make a copy, and syslog(3) later uses it. We can't garbagecollect it |
---|
139 | n/a | * If NULL, just let openlog figure it out (probably using C argv[0]). |
---|
140 | n/a | */ |
---|
141 | n/a | if (S_ident_o) { |
---|
142 | n/a | ident = PyUnicode_AsUTF8(S_ident_o); |
---|
143 | n/a | if (ident == NULL) |
---|
144 | n/a | return NULL; |
---|
145 | n/a | } |
---|
146 | n/a | |
---|
147 | n/a | openlog(ident, logopt, facility); |
---|
148 | n/a | S_log_open = 1; |
---|
149 | n/a | |
---|
150 | n/a | Py_RETURN_NONE; |
---|
151 | n/a | } |
---|
152 | n/a | |
---|
153 | n/a | |
---|
154 | n/a | static PyObject * |
---|
155 | n/a | syslog_syslog(PyObject * self, PyObject * args) |
---|
156 | n/a | { |
---|
157 | n/a | PyObject *message_object; |
---|
158 | n/a | const char *message; |
---|
159 | n/a | int priority = LOG_INFO; |
---|
160 | n/a | |
---|
161 | n/a | if (!PyArg_ParseTuple(args, "iU;[priority,] message string", |
---|
162 | n/a | &priority, &message_object)) { |
---|
163 | n/a | PyErr_Clear(); |
---|
164 | n/a | if (!PyArg_ParseTuple(args, "U;[priority,] message string", |
---|
165 | n/a | &message_object)) |
---|
166 | n/a | return NULL; |
---|
167 | n/a | } |
---|
168 | n/a | |
---|
169 | n/a | message = PyUnicode_AsUTF8(message_object); |
---|
170 | n/a | if (message == NULL) |
---|
171 | n/a | return NULL; |
---|
172 | n/a | |
---|
173 | n/a | /* if log is not opened, open it now */ |
---|
174 | n/a | if (!S_log_open) { |
---|
175 | n/a | PyObject *openargs; |
---|
176 | n/a | |
---|
177 | n/a | /* Continue even if PyTuple_New fails, because openlog(3) is optional. |
---|
178 | n/a | * So, we can still do loggin in the unlikely event things are so hosed |
---|
179 | n/a | * that we can't do this tuple. |
---|
180 | n/a | */ |
---|
181 | n/a | if ((openargs = PyTuple_New(0))) { |
---|
182 | n/a | PyObject *openlog_ret = syslog_openlog(self, openargs, NULL); |
---|
183 | n/a | Py_XDECREF(openlog_ret); |
---|
184 | n/a | Py_DECREF(openargs); |
---|
185 | n/a | } |
---|
186 | n/a | } |
---|
187 | n/a | |
---|
188 | n/a | Py_BEGIN_ALLOW_THREADS; |
---|
189 | n/a | syslog(priority, "%s", message); |
---|
190 | n/a | Py_END_ALLOW_THREADS; |
---|
191 | n/a | Py_RETURN_NONE; |
---|
192 | n/a | } |
---|
193 | n/a | |
---|
194 | n/a | static PyObject * |
---|
195 | n/a | syslog_closelog(PyObject *self, PyObject *unused) |
---|
196 | n/a | { |
---|
197 | n/a | if (S_log_open) { |
---|
198 | n/a | closelog(); |
---|
199 | n/a | Py_CLEAR(S_ident_o); |
---|
200 | n/a | S_log_open = 0; |
---|
201 | n/a | } |
---|
202 | n/a | Py_RETURN_NONE; |
---|
203 | n/a | } |
---|
204 | n/a | |
---|
205 | n/a | static PyObject * |
---|
206 | n/a | syslog_setlogmask(PyObject *self, PyObject *args) |
---|
207 | n/a | { |
---|
208 | n/a | long maskpri, omaskpri; |
---|
209 | n/a | |
---|
210 | n/a | if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri)) |
---|
211 | n/a | return NULL; |
---|
212 | n/a | omaskpri = setlogmask(maskpri); |
---|
213 | n/a | return PyLong_FromLong(omaskpri); |
---|
214 | n/a | } |
---|
215 | n/a | |
---|
216 | n/a | static PyObject * |
---|
217 | n/a | syslog_log_mask(PyObject *self, PyObject *args) |
---|
218 | n/a | { |
---|
219 | n/a | long mask; |
---|
220 | n/a | long pri; |
---|
221 | n/a | if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri)) |
---|
222 | n/a | return NULL; |
---|
223 | n/a | mask = LOG_MASK(pri); |
---|
224 | n/a | return PyLong_FromLong(mask); |
---|
225 | n/a | } |
---|
226 | n/a | |
---|
227 | n/a | static PyObject * |
---|
228 | n/a | syslog_log_upto(PyObject *self, PyObject *args) |
---|
229 | n/a | { |
---|
230 | n/a | long mask; |
---|
231 | n/a | long pri; |
---|
232 | n/a | if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri)) |
---|
233 | n/a | return NULL; |
---|
234 | n/a | mask = LOG_UPTO(pri); |
---|
235 | n/a | return PyLong_FromLong(mask); |
---|
236 | n/a | } |
---|
237 | n/a | |
---|
238 | n/a | /* List of functions defined in the module */ |
---|
239 | n/a | |
---|
240 | n/a | static PyMethodDef syslog_methods[] = { |
---|
241 | n/a | {"openlog", (PyCFunction) syslog_openlog, METH_VARARGS | METH_KEYWORDS}, |
---|
242 | n/a | {"closelog", syslog_closelog, METH_NOARGS}, |
---|
243 | n/a | {"syslog", syslog_syslog, METH_VARARGS}, |
---|
244 | n/a | {"setlogmask", syslog_setlogmask, METH_VARARGS}, |
---|
245 | n/a | {"LOG_MASK", syslog_log_mask, METH_VARARGS}, |
---|
246 | n/a | {"LOG_UPTO", syslog_log_upto, METH_VARARGS}, |
---|
247 | n/a | {NULL, NULL, 0} |
---|
248 | n/a | }; |
---|
249 | n/a | |
---|
250 | n/a | /* Initialization function for the module */ |
---|
251 | n/a | |
---|
252 | n/a | |
---|
253 | n/a | static struct PyModuleDef syslogmodule = { |
---|
254 | n/a | PyModuleDef_HEAD_INIT, |
---|
255 | n/a | "syslog", |
---|
256 | n/a | NULL, |
---|
257 | n/a | -1, |
---|
258 | n/a | syslog_methods, |
---|
259 | n/a | NULL, |
---|
260 | n/a | NULL, |
---|
261 | n/a | NULL, |
---|
262 | n/a | NULL |
---|
263 | n/a | }; |
---|
264 | n/a | |
---|
265 | n/a | PyMODINIT_FUNC |
---|
266 | n/a | PyInit_syslog(void) |
---|
267 | n/a | { |
---|
268 | n/a | PyObject *m; |
---|
269 | n/a | |
---|
270 | n/a | /* Create the module and add the functions */ |
---|
271 | n/a | m = PyModule_Create(&syslogmodule); |
---|
272 | n/a | if (m == NULL) |
---|
273 | n/a | return NULL; |
---|
274 | n/a | |
---|
275 | n/a | /* Add some symbolic constants to the module */ |
---|
276 | n/a | |
---|
277 | n/a | /* Priorities */ |
---|
278 | n/a | PyModule_AddIntMacro(m, LOG_EMERG); |
---|
279 | n/a | PyModule_AddIntMacro(m, LOG_ALERT); |
---|
280 | n/a | PyModule_AddIntMacro(m, LOG_CRIT); |
---|
281 | n/a | PyModule_AddIntMacro(m, LOG_ERR); |
---|
282 | n/a | PyModule_AddIntMacro(m, LOG_WARNING); |
---|
283 | n/a | PyModule_AddIntMacro(m, LOG_NOTICE); |
---|
284 | n/a | PyModule_AddIntMacro(m, LOG_INFO); |
---|
285 | n/a | PyModule_AddIntMacro(m, LOG_DEBUG); |
---|
286 | n/a | |
---|
287 | n/a | /* openlog() option flags */ |
---|
288 | n/a | PyModule_AddIntMacro(m, LOG_PID); |
---|
289 | n/a | PyModule_AddIntMacro(m, LOG_CONS); |
---|
290 | n/a | PyModule_AddIntMacro(m, LOG_NDELAY); |
---|
291 | n/a | #ifdef LOG_ODELAY |
---|
292 | n/a | PyModule_AddIntMacro(m, LOG_ODELAY); |
---|
293 | n/a | #endif |
---|
294 | n/a | #ifdef LOG_NOWAIT |
---|
295 | n/a | PyModule_AddIntMacro(m, LOG_NOWAIT); |
---|
296 | n/a | #endif |
---|
297 | n/a | #ifdef LOG_PERROR |
---|
298 | n/a | PyModule_AddIntMacro(m, LOG_PERROR); |
---|
299 | n/a | #endif |
---|
300 | n/a | |
---|
301 | n/a | /* Facilities */ |
---|
302 | n/a | PyModule_AddIntMacro(m, LOG_KERN); |
---|
303 | n/a | PyModule_AddIntMacro(m, LOG_USER); |
---|
304 | n/a | PyModule_AddIntMacro(m, LOG_MAIL); |
---|
305 | n/a | PyModule_AddIntMacro(m, LOG_DAEMON); |
---|
306 | n/a | PyModule_AddIntMacro(m, LOG_AUTH); |
---|
307 | n/a | PyModule_AddIntMacro(m, LOG_LPR); |
---|
308 | n/a | PyModule_AddIntMacro(m, LOG_LOCAL0); |
---|
309 | n/a | PyModule_AddIntMacro(m, LOG_LOCAL1); |
---|
310 | n/a | PyModule_AddIntMacro(m, LOG_LOCAL2); |
---|
311 | n/a | PyModule_AddIntMacro(m, LOG_LOCAL3); |
---|
312 | n/a | PyModule_AddIntMacro(m, LOG_LOCAL4); |
---|
313 | n/a | PyModule_AddIntMacro(m, LOG_LOCAL5); |
---|
314 | n/a | PyModule_AddIntMacro(m, LOG_LOCAL6); |
---|
315 | n/a | PyModule_AddIntMacro(m, LOG_LOCAL7); |
---|
316 | n/a | |
---|
317 | n/a | #ifndef LOG_SYSLOG |
---|
318 | n/a | #define LOG_SYSLOG LOG_DAEMON |
---|
319 | n/a | #endif |
---|
320 | n/a | #ifndef LOG_NEWS |
---|
321 | n/a | #define LOG_NEWS LOG_MAIL |
---|
322 | n/a | #endif |
---|
323 | n/a | #ifndef LOG_UUCP |
---|
324 | n/a | #define LOG_UUCP LOG_MAIL |
---|
325 | n/a | #endif |
---|
326 | n/a | #ifndef LOG_CRON |
---|
327 | n/a | #define LOG_CRON LOG_DAEMON |
---|
328 | n/a | #endif |
---|
329 | n/a | |
---|
330 | n/a | PyModule_AddIntMacro(m, LOG_SYSLOG); |
---|
331 | n/a | PyModule_AddIntMacro(m, LOG_CRON); |
---|
332 | n/a | PyModule_AddIntMacro(m, LOG_UUCP); |
---|
333 | n/a | PyModule_AddIntMacro(m, LOG_NEWS); |
---|
334 | n/a | |
---|
335 | n/a | #ifdef LOG_AUTHPRIV |
---|
336 | n/a | PyModule_AddIntMacro(m, LOG_AUTHPRIV); |
---|
337 | n/a | #endif |
---|
338 | n/a | |
---|
339 | n/a | return m; |
---|
340 | n/a | } |
---|