syslogmodule.c 6.49 KB
Newer Older
1
/***********************************************************
2 3
Copyright 1994 by Lance Ellinghouse,
Cathedral City, California Republic, United States of America.
4 5 6 7 8 9 10

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
11 12 13
supporting documentation, and that the name of Lance Ellinghouse
not be used in advertising or publicity pertaining to distribution 
of the software without specific, written prior permission.
14

15
LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
16
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 18 19 20 21
FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL, 
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 
FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 23 24

******************************************************************/

25 26 27 28
/******************************************************************

Revision history:

29 30 31 32 33 34
1998/04/28 (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.

35 36 37 38
95/06/29 (Steve Clift)
  - Changed arg parsing to use PyArg_ParseTuple.
  - Added PyErr_Clear() call(s) where needed.
  - Fix core dumps if user message contains format specifiers.
39
  - Change openlog arg defaults to match normal syslog behavior.
40 41 42 43 44
  - Plug memory leak in openlog().
  - Fix setlogmask() to return previous mask value.

******************************************************************/

45 46
/* syslog module */

47
#include "Python.h"
48 49 50

#include <syslog.h>

51 52 53 54
/*  only one instance, only one syslog, so globals should be ok  */
static PyObject *S_ident_o = NULL;			/*  identifier, held by openlog()  */


55
static PyObject * 
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
56
syslog_openlog(PyObject * self, PyObject * args)
57
{
58 59
	long logopt = 0;
	long facility = LOG_USER;
60 61


62
	Py_XDECREF(S_ident_o);
63 64
	if (!PyArg_ParseTuple(args,
			      "S|ll;ident string [, logoption [, facility]]",
65
			      &S_ident_o, &logopt, &facility))
66 67 68 69 70
		return NULL;

	/* This is needed because openlog() does NOT make a copy
	 * and syslog() later uses it.. cannot trash it.
	 */
71
	Py_INCREF(S_ident_o);
72

73
	openlog(PyString_AsString(S_ident_o), logopt, facility);
74

75 76
	Py_INCREF(Py_None);
	return Py_None;
77 78
}

79

80
static PyObject * 
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
81
syslog_syslog(PyObject * self, PyObject * args)
82
{
83
	char *message;
84
	int   priority = LOG_INFO;
85 86 87 88 89 90 91 92

	if (!PyArg_ParseTuple(args, "is;[priority,] message string",
			      &priority, &message)) {
		PyErr_Clear();
		if (!PyArg_ParseTuple(args, "s;[priority,] message string",
				      &message))
			return NULL;
	}
93

94 95 96
	syslog(priority, "%s", message);
	Py_INCREF(Py_None);
	return Py_None;
97 98
}

99
static PyObject * 
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
100
syslog_closelog(PyObject *self, PyObject *args)
101
{
102
	if (!PyArg_ParseTuple(args, ":closelog"))
103 104
		return NULL;
	closelog();
105 106
	Py_XDECREF(S_ident_o);
	S_ident_o = NULL;
107 108
	Py_INCREF(Py_None);
	return Py_None;
109 110
}

111
static PyObject * 
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
112
syslog_setlogmask(PyObject *self, PyObject *args)
113
{
114
	long maskpri, omaskpri;
115

116 117 118 119
	if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
		return NULL;
	omaskpri = setlogmask(maskpri);
	return PyInt_FromLong(omaskpri);
120 121
}

122
static PyObject * 
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
123
syslog_log_mask(PyObject *self, PyObject *args)
124
{
125 126
	long mask;
	long pri;
127
	if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
128 129 130
		return NULL;
	mask = LOG_MASK(pri);
	return PyInt_FromLong(mask);
131 132
}

133
static PyObject * 
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
134
syslog_log_upto(PyObject *self, PyObject *args)
135
{
136 137
	long mask;
	long pri;
138
	if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
139 140 141
		return NULL;
	mask = LOG_UPTO(pri);
	return PyInt_FromLong(mask);
142 143 144 145
}

/* List of functions defined in the module */

146
static PyMethodDef syslog_methods[] = {
147 148 149 150 151 152 153
	{"openlog",	syslog_openlog,		METH_VARARGS},
	{"closelog",	syslog_closelog,	METH_VARARGS},
	{"syslog",	syslog_syslog,		METH_VARARGS},
	{"setlogmask",	syslog_setlogmask,	METH_VARARGS},
	{"LOG_MASK",	syslog_log_mask,	METH_VARARGS},
	{"LOG_UPTO",	syslog_log_upto,	METH_VARARGS},
	{NULL,		NULL,			0}
154 155
};

156
/* Initialization function for the module */
157

158
PyMODINIT_FUNC
159
initsyslog(void)
160
{
161
	PyObject *m;
162 163

	/* Create the module and add the functions */
164
	m = Py_InitModule("syslog", syslog_methods);
165 166

	/* Add some symbolic constants to the module */
167 168

	/* Priorities */
169 170 171 172 173 174 175 176
	PyModule_AddIntConstant(m, "LOG_EMERG",	  LOG_EMERG);
	PyModule_AddIntConstant(m, "LOG_ALERT",	  LOG_ALERT);
	PyModule_AddIntConstant(m, "LOG_CRIT",	  LOG_CRIT);
	PyModule_AddIntConstant(m, "LOG_ERR",	  LOG_ERR);
	PyModule_AddIntConstant(m, "LOG_WARNING", LOG_WARNING);
	PyModule_AddIntConstant(m, "LOG_NOTICE",  LOG_NOTICE);
	PyModule_AddIntConstant(m, "LOG_INFO",	  LOG_INFO);
	PyModule_AddIntConstant(m, "LOG_DEBUG",	  LOG_DEBUG);
177 178

	/* openlog() option flags */
179 180 181
	PyModule_AddIntConstant(m, "LOG_PID",	  LOG_PID);
	PyModule_AddIntConstant(m, "LOG_CONS",	  LOG_CONS);
	PyModule_AddIntConstant(m, "LOG_NDELAY",  LOG_NDELAY);
182
#ifdef LOG_NOWAIT
183
	PyModule_AddIntConstant(m, "LOG_NOWAIT",  LOG_NOWAIT);
184
#endif
185
#ifdef LOG_PERROR
186
	PyModule_AddIntConstant(m, "LOG_PERROR",  LOG_PERROR);
187 188 189
#endif

	/* Facilities */
190 191 192 193 194 195 196 197 198 199 200 201 202 203
	PyModule_AddIntConstant(m, "LOG_KERN",	  LOG_KERN);
	PyModule_AddIntConstant(m, "LOG_USER",	  LOG_USER);
	PyModule_AddIntConstant(m, "LOG_MAIL",	  LOG_MAIL);
	PyModule_AddIntConstant(m, "LOG_DAEMON",  LOG_DAEMON);
	PyModule_AddIntConstant(m, "LOG_AUTH",	  LOG_AUTH);
	PyModule_AddIntConstant(m, "LOG_LPR",	  LOG_LPR);
	PyModule_AddIntConstant(m, "LOG_LOCAL0",  LOG_LOCAL0);
	PyModule_AddIntConstant(m, "LOG_LOCAL1",  LOG_LOCAL1);
	PyModule_AddIntConstant(m, "LOG_LOCAL2",  LOG_LOCAL2);
	PyModule_AddIntConstant(m, "LOG_LOCAL3",  LOG_LOCAL3);
	PyModule_AddIntConstant(m, "LOG_LOCAL4",  LOG_LOCAL4);
	PyModule_AddIntConstant(m, "LOG_LOCAL5",  LOG_LOCAL5);
	PyModule_AddIntConstant(m, "LOG_LOCAL6",  LOG_LOCAL6);
	PyModule_AddIntConstant(m, "LOG_LOCAL7",  LOG_LOCAL7);
204

205 206 207 208 209 210 211 212 213 214 215 216 217
#ifndef LOG_SYSLOG
#define LOG_SYSLOG		LOG_DAEMON
#endif
#ifndef LOG_NEWS
#define LOG_NEWS		LOG_MAIL
#endif
#ifndef LOG_UUCP
#define LOG_UUCP		LOG_MAIL
#endif
#ifndef LOG_CRON
#define LOG_CRON		LOG_DAEMON
#endif

218 219 220 221
	PyModule_AddIntConstant(m, "LOG_SYSLOG",  LOG_SYSLOG);
	PyModule_AddIntConstant(m, "LOG_CRON",	  LOG_CRON);
	PyModule_AddIntConstant(m, "LOG_UUCP",	  LOG_UUCP);
	PyModule_AddIntConstant(m, "LOG_NEWS",	  LOG_NEWS);
222
}