Skip to content

Commit ae94cf2

Browse files
committed
Fix by Sean Reifschneider:
- When facility not specified to syslog() method, use default from openlog() (This is how it was claimed to work in the documentation) - Potential resource leak of o_ident, now cleaned up in closelog() - Minor comment accuracy fix.
1 parent 27c225e commit ae94cf2

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

Modules/syslogmodule.c

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2626
2727
Revision history:
2828
29+
1998/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+
2935
95/06/29 (Steve Clift)
3036
- Changed arg parsing to use PyArg_ParseTuple.
3137
- Added PyErr_Clear() call(s) where needed.
@@ -42,6 +48,10 @@ Revision history:
4248

4349
#include <syslog.h>
4450

51+
/* only one instance, only one syslog, so globals should be ok */
52+
static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */
53+
54+
4555
static PyObject *
4656
syslog_openlog(self, args)
4757
PyObject * self;
@@ -50,20 +60,19 @@ syslog_openlog(self, args)
5060
long logopt = 0;
5161
long facility = LOG_USER;
5262

53-
static PyObject *ident_o = NULL;
5463

55-
Py_XDECREF(ident_o);
64+
Py_XDECREF(S_ident_o);
5665
if (!PyArg_ParseTuple(args,
5766
"S|ll;ident string [, logoption [, facility]]",
58-
&ident_o, &logopt, &facility))
67+
&S_ident_o, &logopt, &facility))
5968
return NULL;
6069

6170
/* This is needed because openlog() does NOT make a copy
6271
* and syslog() later uses it.. cannot trash it.
6372
*/
64-
Py_INCREF(ident_o);
73+
Py_INCREF(S_ident_o);
6574

66-
openlog(PyString_AsString(ident_o), logopt, facility);
75+
openlog(PyString_AsString(S_ident_o), logopt, facility);
6776

6877
Py_INCREF(Py_None);
6978
return Py_None;
@@ -76,7 +85,7 @@ syslog_syslog(self, args)
7685
PyObject * args;
7786
{
7887
char *message;
79-
int priority = LOG_INFO | LOG_USER;
88+
int priority = LOG_INFO;
8089

8190
if (!PyArg_ParseTuple(args, "is;[priority,] message string",
8291
&priority, &message)) {
@@ -85,6 +94,7 @@ syslog_syslog(self, args)
8594
&message))
8695
return NULL;
8796
}
97+
8898
syslog(priority, "%s", message);
8999
Py_INCREF(Py_None);
90100
return Py_None;
@@ -98,6 +108,8 @@ syslog_closelog(self, args)
98108
if (!PyArg_ParseTuple(args, ""))
99109
return NULL;
100110
closelog();
111+
Py_XDECREF(S_ident_o);
112+
S_ident_o = NULL;
101113
Py_INCREF(Py_None);
102114
return Py_None;
103115
}
@@ -153,7 +165,7 @@ static PyMethodDef syslog_methods[] = {
153165
{NULL, NULL, 0}
154166
};
155167

156-
/* Initialization function for the module */
168+
/* helper function for initialization function */
157169

158170
static void
159171
ins(d, s, x)
@@ -168,6 +180,7 @@ ins(d, s, x)
168180
}
169181
}
170182

183+
/* Initialization function for the module */
171184

172185
void
173186
initsyslog()
@@ -205,25 +218,7 @@ initsyslog()
205218
ins(d, "LOG_MAIL", LOG_MAIL);
206219
ins(d, "LOG_DAEMON", LOG_DAEMON);
207220
ins(d, "LOG_AUTH", LOG_AUTH);
208-
#ifdef LOG_SYSLOG
209-
ins(d, "LOG_SYSLOG", LOG_SYSLOG);
210-
#endif
211221
ins(d, "LOG_LPR", LOG_LPR);
212-
#ifdef LOG_NEWS
213-
ins(d, "LOG_NEWS", LOG_NEWS);
214-
#else
215-
ins(d, "LOG_NEWS", LOG_MAIL);
216-
#endif
217-
#ifdef LOG_UUCP
218-
ins(d, "LOG_UUCP", LOG_UUCP);
219-
#else
220-
ins(d, "LOG_UUCP", LOG_MAIL);
221-
#endif
222-
#ifdef LOG_CRON
223-
ins(d, "LOG_CRON", LOG_CRON);
224-
#else
225-
ins(d, "LOG_CRON", LOG_DAEMON);
226-
#endif
227222
ins(d, "LOG_LOCAL0", LOG_LOCAL0);
228223
ins(d, "LOG_LOCAL1", LOG_LOCAL1);
229224
ins(d, "LOG_LOCAL2", LOG_LOCAL2);
@@ -233,6 +228,24 @@ initsyslog()
233228
ins(d, "LOG_LOCAL6", LOG_LOCAL6);
234229
ins(d, "LOG_LOCAL7", LOG_LOCAL7);
235230

231+
#ifndef LOG_SYSLOG
232+
#define LOG_SYSLOG LOG_DAEMON
233+
#endif
234+
#ifndef LOG_NEWS
235+
#define LOG_NEWS LOG_MAIL
236+
#endif
237+
#ifndef LOG_UUCP
238+
#define LOG_UUCP LOG_MAIL
239+
#endif
240+
#ifndef LOG_CRON
241+
#define LOG_CRON LOG_DAEMON
242+
#endif
243+
244+
ins(d, "LOG_SYSLOG", LOG_SYSLOG);
245+
ins(d, "LOG_CRON", LOG_CRON);
246+
ins(d, "LOG_UUCP", LOG_UUCP);
247+
ins(d, "LOG_NEWS", LOG_NEWS);
248+
236249
/* Check for errors */
237250
if (PyErr_Occurred())
238251
Py_FatalError("can't initialize module syslog");

0 commit comments

Comments
 (0)