ยปCore Development>Code coverage>Modules/syslogmodule.c

Python code coverage for Modules/syslogmodule.c

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