1994-07-14 10:56:50 -03:00
|
|
|
/***********************************************************
|
1994-08-30 09:25:20 -03:00
|
|
|
Copyright 1994 by Lance Ellinghouse,
|
|
|
|
Cathedral City, California Republic, United States of America.
|
1994-07-14 10:56:50 -03:00
|
|
|
|
|
|
|
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
|
1994-08-30 09:25:20 -03:00
|
|
|
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.
|
1994-07-14 10:56:50 -03:00
|
|
|
|
1994-08-30 09:25:20 -03:00
|
|
|
LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
|
1994-07-14 10:56:50 -03:00
|
|
|
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
1994-08-30 09:25:20 -03:00
|
|
|
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.
|
1994-07-14 10:56:50 -03:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1995-10-11 13:15:28 -03:00
|
|
|
/******************************************************************
|
|
|
|
|
|
|
|
Revision history:
|
|
|
|
|
1998-05-08 18:52:55 -03:00
|
|
|
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.
|
|
|
|
|
1995-10-11 13:15:28 -03:00
|
|
|
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.
|
2000-07-16 09:04:32 -03:00
|
|
|
- Change openlog arg defaults to match normal syslog behavior.
|
1995-10-11 13:15:28 -03:00
|
|
|
- Plug memory leak in openlog().
|
|
|
|
- Fix setlogmask() to return previous mask value.
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1994-07-14 10:56:50 -03:00
|
|
|
/* syslog module */
|
|
|
|
|
1995-01-10 16:56:29 -04:00
|
|
|
#include "Python.h"
|
1994-07-14 10:56:50 -03:00
|
|
|
|
|
|
|
#include <syslog.h>
|
|
|
|
|
1998-05-08 18:52:55 -03:00
|
|
|
/* only one instance, only one syslog, so globals should be ok */
|
|
|
|
static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */
|
|
|
|
|
|
|
|
|
1994-08-30 09:25:20 -03:00
|
|
|
static PyObject *
|
2000-07-10 09:29:26 -03:00
|
|
|
syslog_openlog(PyObject * self, PyObject * args)
|
1994-07-14 10:56:50 -03:00
|
|
|
{
|
1997-01-09 19:51:21 -04:00
|
|
|
long logopt = 0;
|
|
|
|
long facility = LOG_USER;
|
2004-12-16 19:52:04 -04:00
|
|
|
PyObject *new_S_ident_o;
|
1995-10-11 13:15:28 -03:00
|
|
|
|
1997-01-09 19:51:21 -04:00
|
|
|
if (!PyArg_ParseTuple(args,
|
|
|
|
"S|ll;ident string [, logoption [, facility]]",
|
2004-12-16 19:52:04 -04:00
|
|
|
&new_S_ident_o, &logopt, &facility))
|
1997-01-09 19:51:21 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* This is needed because openlog() does NOT make a copy
|
|
|
|
* and syslog() later uses it.. cannot trash it.
|
|
|
|
*/
|
2004-12-16 19:52:04 -04:00
|
|
|
Py_XDECREF(S_ident_o);
|
|
|
|
S_ident_o = new_S_ident_o;
|
1998-05-08 18:52:55 -03:00
|
|
|
Py_INCREF(S_ident_o);
|
1995-10-11 13:15:28 -03:00
|
|
|
|
2008-05-26 09:51:38 -03:00
|
|
|
openlog(PyBytes_AsString(S_ident_o), logopt, facility);
|
1995-10-11 13:15:28 -03:00
|
|
|
|
1997-01-09 19:51:21 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-07-14 10:56:50 -03:00
|
|
|
}
|
|
|
|
|
1997-01-09 19:51:21 -04:00
|
|
|
|
1994-08-30 09:25:20 -03:00
|
|
|
static PyObject *
|
2000-07-10 09:29:26 -03:00
|
|
|
syslog_syslog(PyObject * self, PyObject * args)
|
1994-07-14 10:56:50 -03:00
|
|
|
{
|
1997-01-09 19:51:21 -04:00
|
|
|
char *message;
|
1998-05-08 18:52:55 -03:00
|
|
|
int priority = LOG_INFO;
|
1997-01-09 19:51:21 -04:00
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "is;[priority,] message string",
|
|
|
|
&priority, &message)) {
|
|
|
|
PyErr_Clear();
|
|
|
|
if (!PyArg_ParseTuple(args, "s;[priority,] message string",
|
|
|
|
&message))
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-05-08 18:52:55 -03:00
|
|
|
|
2008-02-23 13:42:31 -04:00
|
|
|
Py_BEGIN_ALLOW_THREADS;
|
1997-01-09 19:51:21 -04:00
|
|
|
syslog(priority, "%s", message);
|
2008-02-23 13:42:31 -04:00
|
|
|
Py_END_ALLOW_THREADS;
|
1997-01-09 19:51:21 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-07-14 10:56:50 -03:00
|
|
|
}
|
|
|
|
|
1994-08-30 09:25:20 -03:00
|
|
|
static PyObject *
|
2006-05-29 18:04:52 -03:00
|
|
|
syslog_closelog(PyObject *self, PyObject *unused)
|
1994-07-14 10:56:50 -03:00
|
|
|
{
|
|
|
|
closelog();
|
1998-05-08 18:52:55 -03:00
|
|
|
Py_XDECREF(S_ident_o);
|
|
|
|
S_ident_o = NULL;
|
1994-08-30 09:25:20 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-07-14 10:56:50 -03:00
|
|
|
}
|
|
|
|
|
1994-08-30 09:25:20 -03:00
|
|
|
static PyObject *
|
2000-07-10 09:29:26 -03:00
|
|
|
syslog_setlogmask(PyObject *self, PyObject *args)
|
1994-07-14 10:56:50 -03:00
|
|
|
{
|
1997-01-09 19:51:21 -04:00
|
|
|
long maskpri, omaskpri;
|
1995-10-11 13:15:28 -03:00
|
|
|
|
1997-01-09 19:51:21 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
|
|
|
|
return NULL;
|
|
|
|
omaskpri = setlogmask(maskpri);
|
|
|
|
return PyInt_FromLong(omaskpri);
|
1994-07-14 10:56:50 -03:00
|
|
|
}
|
|
|
|
|
1994-08-30 09:25:20 -03:00
|
|
|
static PyObject *
|
2000-07-10 09:29:26 -03:00
|
|
|
syslog_log_mask(PyObject *self, PyObject *args)
|
1994-07-14 10:56:50 -03:00
|
|
|
{
|
1997-01-09 19:51:21 -04:00
|
|
|
long mask;
|
|
|
|
long pri;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
|
1997-01-09 19:51:21 -04:00
|
|
|
return NULL;
|
|
|
|
mask = LOG_MASK(pri);
|
|
|
|
return PyInt_FromLong(mask);
|
1994-07-14 10:56:50 -03:00
|
|
|
}
|
|
|
|
|
1994-08-30 09:25:20 -03:00
|
|
|
static PyObject *
|
2000-07-10 09:29:26 -03:00
|
|
|
syslog_log_upto(PyObject *self, PyObject *args)
|
1994-07-14 10:56:50 -03:00
|
|
|
{
|
1997-01-09 19:51:21 -04:00
|
|
|
long mask;
|
|
|
|
long pri;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
|
1997-01-09 19:51:21 -04:00
|
|
|
return NULL;
|
|
|
|
mask = LOG_UPTO(pri);
|
|
|
|
return PyInt_FromLong(mask);
|
1994-07-14 10:56:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* List of functions defined in the module */
|
|
|
|
|
1994-08-30 09:25:20 -03:00
|
|
|
static PyMethodDef syslog_methods[] = {
|
1995-10-11 13:15:28 -03:00
|
|
|
{"openlog", syslog_openlog, METH_VARARGS},
|
2006-05-29 18:04:52 -03:00
|
|
|
{"closelog", syslog_closelog, METH_NOARGS},
|
1995-10-11 13:15:28 -03:00
|
|
|
{"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}
|
1994-07-14 10:56:50 -03:00
|
|
|
};
|
|
|
|
|
1998-05-08 18:52:55 -03:00
|
|
|
/* Initialization function for the module */
|
1995-10-11 13:15:28 -03:00
|
|
|
|
2002-08-01 23:27:13 -03:00
|
|
|
PyMODINIT_FUNC
|
2000-07-21 03:00:07 -03:00
|
|
|
initsyslog(void)
|
1994-07-14 10:56:50 -03:00
|
|
|
{
|
2002-04-01 10:53:37 -04:00
|
|
|
PyObject *m;
|
1994-07-14 10:56:50 -03:00
|
|
|
|
|
|
|
/* Create the module and add the functions */
|
1994-08-30 09:25:20 -03:00
|
|
|
m = Py_InitModule("syslog", syslog_methods);
|
2006-01-19 02:09:39 -04:00
|
|
|
if (m == NULL)
|
|
|
|
return;
|
1994-07-14 10:56:50 -03:00
|
|
|
|
|
|
|
/* Add some symbolic constants to the module */
|
1995-10-11 13:15:28 -03:00
|
|
|
|
|
|
|
/* Priorities */
|
2002-04-01 10:53:37 -04:00
|
|
|
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);
|
1995-10-11 13:15:28 -03:00
|
|
|
|
|
|
|
/* openlog() option flags */
|
2002-04-01 10:53:37 -04:00
|
|
|
PyModule_AddIntConstant(m, "LOG_PID", LOG_PID);
|
|
|
|
PyModule_AddIntConstant(m, "LOG_CONS", LOG_CONS);
|
|
|
|
PyModule_AddIntConstant(m, "LOG_NDELAY", LOG_NDELAY);
|
1998-08-04 19:53:56 -03:00
|
|
|
#ifdef LOG_NOWAIT
|
2002-04-01 10:53:37 -04:00
|
|
|
PyModule_AddIntConstant(m, "LOG_NOWAIT", LOG_NOWAIT);
|
1998-08-04 19:53:56 -03:00
|
|
|
#endif
|
1995-10-11 13:15:28 -03:00
|
|
|
#ifdef LOG_PERROR
|
2002-04-01 10:53:37 -04:00
|
|
|
PyModule_AddIntConstant(m, "LOG_PERROR", LOG_PERROR);
|
1995-10-11 13:15:28 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Facilities */
|
2002-04-01 10:53:37 -04:00
|
|
|
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);
|
1994-07-14 10:56:50 -03:00
|
|
|
|
1998-05-08 18:52:55 -03:00
|
|
|
#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
|
|
|
|
|
2002-04-01 10:53:37 -04:00
|
|
|
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);
|
1994-07-14 10:56:50 -03:00
|
|
|
}
|