@@ -22,6 +22,20 @@ WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
22
23
23
******************************************************************/
24
24
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
+
25
39
/* syslog module */
26
40
27
41
#include "Python.h"
@@ -33,18 +47,21 @@ syslog_openlog(self, args)
33
47
PyObject * self ;
34
48
PyObject * args ;
35
49
{
36
- char * ident = "" ;
37
- PyObject * ident_o ;
38
- long logopt = LOG_PID ;
50
+ long logopt = 0 ;
39
51
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 )) {
43
58
return NULL ;
59
+ }
44
60
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
+
48
65
Py_INCREF (Py_None );
49
66
return Py_None ;
50
67
}
@@ -54,13 +71,17 @@ syslog_syslog(self, args)
54
71
PyObject * self ;
55
72
PyObject * args ;
56
73
{
57
- int priority = LOG_INFO ;
58
- char * message ;
74
+ char * message , * s ;
75
+ int priority = LOG_INFO | LOG_USER ;
59
76
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 )) {
62
81
return NULL ;
63
- syslog (priority , message );
82
+ }
83
+ }
84
+ syslog (priority , "%s" , message );
64
85
Py_INCREF (Py_None );
65
86
return Py_None ;
66
87
}
@@ -70,7 +91,7 @@ syslog_closelog(self, args)
70
91
PyObject * self ;
71
92
PyObject * args ;
72
93
{
73
- if (!PyArg_NoArgs (args ))
94
+ if (!PyArg_ParseTuple (args , "" ))
74
95
return NULL ;
75
96
closelog ();
76
97
Py_INCREF (Py_None );
@@ -82,12 +103,12 @@ syslog_setlogmask(self, args)
82
103
PyObject * self ;
83
104
PyObject * args ;
84
105
{
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 ))
87
109
return NULL ;
88
- setlogmask (maskpri );
89
- Py_INCREF (Py_None );
90
- return Py_None ;
110
+ omaskpri = setlogmask (maskpri );
111
+ return PyInt_FromLong (omaskpri );
91
112
}
92
113
93
114
static PyObject *
@@ -97,7 +118,7 @@ syslog_log_mask(self, args)
97
118
{
98
119
long mask ;
99
120
long pri ;
100
- if (!PyArg_Parse (args ,"l" ,& pri ))
121
+ if (!PyArg_ParseTuple (args ,"l" ,& pri ))
101
122
return NULL ;
102
123
mask = LOG_MASK (pri );
103
124
return PyInt_FromLong (mask );
@@ -110,7 +131,7 @@ syslog_log_upto(self, args)
110
131
{
111
132
long mask ;
112
133
long pri ;
113
- if (!PyArg_Parse (args ,"l" ,& pri ))
134
+ if (!PyArg_ParseTuple (args ,"l" ,& pri ))
114
135
return NULL ;
115
136
mask = LOG_UPTO (pri );
116
137
return PyInt_FromLong (mask );
@@ -119,77 +140,68 @@ syslog_log_upto(self, args)
119
140
/* List of functions defined in the module */
120
141
121
142
static 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 }
129
150
};
130
151
131
152
/* Initialization function for the module */
132
153
154
+ #define DICT_SET_INT (d , s , x ) \
155
+ PyDict_SetItemString(d, s, PyInt_FromLong((long) (x)))
156
+
133
157
void
134
158
initsyslog ()
135
159
{
136
- PyObject * m , * d , * x ;
160
+ PyObject * m , * d ;
137
161
138
162
/* Create the module and add the functions */
139
163
m = Py_InitModule ("syslog" , syslog_methods );
140
164
141
165
/* Add some symbolic constants to the module */
142
166
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 );
193
205
194
206
/* Check for errors */
195
207
if (PyErr_Occurred ())
0 commit comments