@@ -22,6 +22,20 @@ WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2222
2323******************************************************************/
2424
25+ /******************************************************************
26+
27+ Revision history:
28+
29+ 95/06/29 (Steve Clift)
30+ - Changed arg parsing to use PyArg_ParseTuple.
31+ - Added PyErr_Clear() call(s) where needed.
32+ - Fix core dumps if user message contains format specifiers.
33+ - Change openlog arg defaults to match normal syslog behaviour.
34+ - Plug memory leak in openlog().
35+ - Fix setlogmask() to return previous mask value.
36+
37+ ******************************************************************/
38+
2539/* syslog module */
2640
2741#include "Python.h"
@@ -33,18 +47,21 @@ syslog_openlog(self, args)
3347 PyObject * self ;
3448 PyObject * args ;
3549{
36- char * ident = "" ;
37- PyObject * ident_o ;
38- long logopt = LOG_PID ;
50+ long logopt = 0 ;
3951 long facility = LOG_USER ;
40- if (!PyArg_Parse (args , "(Sll);ident string, logoption, facility" , & ident_o , & logopt , & facility ))
41- if (!PyArg_Parse (args , "(Sl);ident string, logoption" , & ident_o , & logopt ))
42- if (!PyArg_Parse (args , "S;ident string" , & ident_o ))
52+
53+ static PyObject * ident_o = NULL ;
54+
55+ Py_XDECREF (ident_o );
56+ if (!PyArg_ParseTuple (args , "S|ll;ident string [, logoption [, facility]]" ,
57+ & ident_o , & logopt , & facility )) {
4358 return NULL ;
59+ }
4460 Py_INCREF (ident_o ); /* This is needed because openlog() does NOT make a copy
45- and syslog() later uses it.. cannot trash it. */
46- ident = PyString_AsString (ident_o );
47- openlog (ident ,logopt ,facility );
61+ and syslog() later uses it.. cannot trash it. */
62+
63+ openlog (PyString_AsString (ident_o ), logopt , facility );
64+
4865 Py_INCREF (Py_None );
4966 return Py_None ;
5067}
@@ -54,13 +71,17 @@ syslog_syslog(self, args)
5471 PyObject * self ;
5572 PyObject * args ;
5673{
57- int priority = LOG_INFO ;
58- char * message ;
74+ char * message , * s ;
75+ int priority = LOG_INFO | LOG_USER ;
5976
60- if (!PyArg_Parse (args ,"(is);priority, message string" ,& priority ,& message ))
61- if (!PyArg_Parse (args ,"s;message string" ,& message ))
77+ if (!PyArg_ParseTuple (args , "is;[priority,] message string" ,
78+ & priority , & message )) {
79+ PyErr_Clear ();
80+ if (!PyArg_ParseTuple (args , "s;[priority,] message string" , & message )) {
6281 return NULL ;
63- syslog (priority , message );
82+ }
83+ }
84+ syslog (priority , "%s" , message );
6485 Py_INCREF (Py_None );
6586 return Py_None ;
6687}
@@ -70,7 +91,7 @@ syslog_closelog(self, args)
7091 PyObject * self ;
7192 PyObject * args ;
7293{
73- if (!PyArg_NoArgs (args ))
94+ if (!PyArg_ParseTuple (args , "" ))
7495 return NULL ;
7596 closelog ();
7697 Py_INCREF (Py_None );
@@ -82,12 +103,12 @@ syslog_setlogmask(self, args)
82103 PyObject * self ;
83104 PyObject * args ;
84105{
85- long maskpri ;
86- if (!PyArg_Parse (args ,"l;mask for priority" ,& maskpri ))
106+ long maskpri , omaskpri ;
107+
108+ if (!PyArg_ParseTuple (args ,"l;mask for priority" ,& maskpri ))
87109 return NULL ;
88- setlogmask (maskpri );
89- Py_INCREF (Py_None );
90- return Py_None ;
110+ omaskpri = setlogmask (maskpri );
111+ return PyInt_FromLong (omaskpri );
91112}
92113
93114static PyObject *
@@ -97,7 +118,7 @@ syslog_log_mask(self, args)
97118{
98119 long mask ;
99120 long pri ;
100- if (!PyArg_Parse (args ,"l" ,& pri ))
121+ if (!PyArg_ParseTuple (args ,"l" ,& pri ))
101122 return NULL ;
102123 mask = LOG_MASK (pri );
103124 return PyInt_FromLong (mask );
@@ -110,7 +131,7 @@ syslog_log_upto(self, args)
110131{
111132 long mask ;
112133 long pri ;
113- if (!PyArg_Parse (args ,"l" ,& pri ))
134+ if (!PyArg_ParseTuple (args ,"l" ,& pri ))
114135 return NULL ;
115136 mask = LOG_UPTO (pri );
116137 return PyInt_FromLong (mask );
@@ -119,77 +140,68 @@ syslog_log_upto(self, args)
119140/* List of functions defined in the module */
120141
121142static PyMethodDef syslog_methods [] = {
122- {"openlog" , ( PyCFunction ) syslog_openlog },
123- {"closelog" , ( PyCFunction ) syslog_closelog },
124- {"syslog" , ( PyCFunction ) syslog_syslog },
125- {"setlogmask" , ( PyCFunction ) syslog_setlogmask },
126- {"LOG_MASK" , ( PyCFunction ) syslog_log_mask },
127- {"LOG_UPTO" , ( PyCFunction ) syslog_log_upto },
128- {NULL , NULL } /* sentinel */
143+ {"openlog" , syslog_openlog , METH_VARARGS },
144+ {"closelog" , syslog_closelog , METH_VARARGS },
145+ {"syslog" , syslog_syslog , METH_VARARGS },
146+ {"setlogmask" , syslog_setlogmask , METH_VARARGS },
147+ {"LOG_MASK" , syslog_log_mask , METH_VARARGS },
148+ {"LOG_UPTO" , syslog_log_upto , METH_VARARGS },
149+ {NULL , NULL , 0 }
129150};
130151
131152/* Initialization function for the module */
132153
154+ #define DICT_SET_INT (d , s , x ) \
155+ PyDict_SetItemString(d, s, PyInt_FromLong((long) (x)))
156+
133157void
134158initsyslog ()
135159{
136- PyObject * m , * d , * x ;
160+ PyObject * m , * d ;
137161
138162 /* Create the module and add the functions */
139163 m = Py_InitModule ("syslog" , syslog_methods );
140164
141165 /* Add some symbolic constants to the module */
142166 d = PyModule_GetDict (m );
143- x = PyInt_FromLong (LOG_EMERG );
144- PyDict_SetItemString (d , "LOG_EMERG" , x );
145- x = PyInt_FromLong (LOG_ALERT );
146- PyDict_SetItemString (d , "LOG_ALERT" , x );
147- x = PyInt_FromLong (LOG_CRIT );
148- PyDict_SetItemString (d , "LOG_CRIT" , x );
149- x = PyInt_FromLong (LOG_ERR );
150- PyDict_SetItemString (d , "LOG_ERR" , x );
151- x = PyInt_FromLong (LOG_WARNING );
152- PyDict_SetItemString (d , "LOG_WARNING" , x );
153- x = PyInt_FromLong (LOG_NOTICE );
154- PyDict_SetItemString (d , "LOG_NOTICE" , x );
155- x = PyInt_FromLong (LOG_INFO );
156- PyDict_SetItemString (d , "LOG_INFO" , x );
157- x = PyInt_FromLong (LOG_DEBUG );
158- PyDict_SetItemString (d , "LOG_DEBUG" , x );
159- x = PyInt_FromLong (LOG_PID );
160- PyDict_SetItemString (d , "LOG_PID" , x );
161- x = PyInt_FromLong (LOG_CONS );
162- PyDict_SetItemString (d , "LOG_CONS" , x );
163- x = PyInt_FromLong (LOG_NDELAY );
164- PyDict_SetItemString (d , "LOG_NDELAY" , x );
165- x = PyInt_FromLong (LOG_NOWAIT );
166- PyDict_SetItemString (d , "LOG_NOWAIT" , x );
167- x = PyInt_FromLong (LOG_KERN );
168- PyDict_SetItemString (d , "LOG_KERN" , x );
169- x = PyInt_FromLong (LOG_USER );
170- PyDict_SetItemString (d , "LOG_USER" , x );
171- x = PyInt_FromLong (LOG_MAIL );
172- PyDict_SetItemString (d , "LOG_MAIL" , x );
173- x = PyInt_FromLong (LOG_DAEMON );
174- PyDict_SetItemString (d , "LOG_DAEMON" , x );
175- x = PyInt_FromLong (LOG_LPR );
176- PyDict_SetItemString (d , "LOG_LPR" , x );
177- x = PyInt_FromLong (LOG_LOCAL0 );
178- PyDict_SetItemString (d , "LOG_LOCAL0" , x );
179- x = PyInt_FromLong (LOG_LOCAL1 );
180- PyDict_SetItemString (d , "LOG_LOCAL1" , x );
181- x = PyInt_FromLong (LOG_LOCAL2 );
182- PyDict_SetItemString (d , "LOG_LOCAL2" , x );
183- x = PyInt_FromLong (LOG_LOCAL3 );
184- PyDict_SetItemString (d , "LOG_LOCAL3" , x );
185- x = PyInt_FromLong (LOG_LOCAL4 );
186- PyDict_SetItemString (d , "LOG_LOCAL4" , x );
187- x = PyInt_FromLong (LOG_LOCAL5 );
188- PyDict_SetItemString (d , "LOG_LOCAL5" , x );
189- x = PyInt_FromLong (LOG_LOCAL6 );
190- PyDict_SetItemString (d , "LOG_LOCAL6" , x );
191- x = PyInt_FromLong (LOG_LOCAL7 );
192- PyDict_SetItemString (d , "LOG_LOCAL7" , x );
167+
168+ /* Priorities */
169+ DICT_SET_INT (d , "LOG_EMERG" , LOG_EMERG );
170+ DICT_SET_INT (d , "LOG_ALERT" , LOG_ALERT );
171+ DICT_SET_INT (d , "LOG_CRIT" , LOG_CRIT );
172+ DICT_SET_INT (d , "LOG_ERR" , LOG_ERR );
173+ DICT_SET_INT (d , "LOG_WARNING" , LOG_WARNING );
174+ DICT_SET_INT (d , "LOG_NOTICE" , LOG_NOTICE );
175+ DICT_SET_INT (d , "LOG_INFO" , LOG_INFO );
176+ DICT_SET_INT (d , "LOG_DEBUG" , LOG_DEBUG );
177+
178+ /* openlog() option flags */
179+ DICT_SET_INT (d , "LOG_PID" , LOG_PID );
180+ DICT_SET_INT (d , "LOG_CONS" , LOG_CONS );
181+ DICT_SET_INT (d , "LOG_NDELAY" , LOG_NDELAY );
182+ DICT_SET_INT (d , "LOG_NOWAIT" , LOG_NOWAIT );
183+ #ifdef LOG_PERROR
184+ DICT_SET_INT (d , "LOG_PERROR" , LOG_PERROR );
185+ #endif
186+
187+ /* Facilities */
188+ DICT_SET_INT (d , "LOG_KERN" , LOG_KERN );
189+ DICT_SET_INT (d , "LOG_USER" , LOG_USER );
190+ DICT_SET_INT (d , "LOG_MAIL" , LOG_MAIL );
191+ DICT_SET_INT (d , "LOG_DAEMON" , LOG_DAEMON );
192+ DICT_SET_INT (d , "LOG_AUTH" , LOG_AUTH );
193+ DICT_SET_INT (d , "LOG_LPR" , LOG_LPR );
194+ DICT_SET_INT (d , "LOG_NEWS" , LOG_NEWS );
195+ DICT_SET_INT (d , "LOG_UUCP" , LOG_UUCP );
196+ DICT_SET_INT (d , "LOG_CRON" , LOG_CRON );
197+ DICT_SET_INT (d , "LOG_LOCAL0" , LOG_LOCAL0 );
198+ DICT_SET_INT (d , "LOG_LOCAL1" , LOG_LOCAL1 );
199+ DICT_SET_INT (d , "LOG_LOCAL2" , LOG_LOCAL2 );
200+ DICT_SET_INT (d , "LOG_LOCAL3" , LOG_LOCAL3 );
201+ DICT_SET_INT (d , "LOG_LOCAL4" , LOG_LOCAL4 );
202+ DICT_SET_INT (d , "LOG_LOCAL5" , LOG_LOCAL5 );
203+ DICT_SET_INT (d , "LOG_LOCAL6" , LOG_LOCAL6 );
204+ DICT_SET_INT (d , "LOG_LOCAL7" , LOG_LOCAL7 );
193205
194206 /* Check for errors */
195207 if (PyErr_Occurred ())
0 commit comments