From e4788cd45ccb5266ffb5132a63292a1024f2e74f Mon Sep 17 00:00:00 2001 From: laforge Date: Mon, 18 Apr 2005 14:21:17 +0000 Subject: add syslog logging (not for packets, but ulogd logfile) --- ulogd/doc/ulogd.sgml | 3 +-- ulogd/ulogd.c | 73 +++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/ulogd/doc/ulogd.sgml b/ulogd/doc/ulogd.sgml index 6748ed8..4741a9d 100644 --- a/ulogd/doc/ulogd.sgml +++ b/ulogd/doc/ulogd.sgml @@ -189,8 +189,7 @@ The following configuration parameters are available: The netlink multicast group, which ulgogd should bind to. This is the same as given with the '--ulog-nlgroup' option to iptables. logfile -The main logfile, where ulogd reports any errors, warnings and other unexpected -conditions. +The main logfile, where ulogd reports any errors, warnings and other unexpected conditions. Apart from a regular filename, the following special values can be used; ``syslog'' to log via the unix syslog(3) mechanism. ``stdout'' to log to stdout. loglevel This specifies, how verbose the logging to logfile is. Currently defined loglevels are: 1=debug information, 3=informational messages, 5=noticable diff --git a/ulogd/ulogd.c b/ulogd/ulogd.c index 19ccdf3..5f3d60b 100644 --- a/ulogd/ulogd.c +++ b/ulogd/ulogd.c @@ -53,6 +53,7 @@ #include #include #include +#include #include #include #include @@ -369,6 +370,32 @@ void register_output(ulog_output_t *me) * MAIN PROGRAM ***********************************************************************/ +static FILE syslog_dummy; + +static inline int ulogd2syslog_level(int level) +{ + int syslog_level = LOG_WARNING; + + switch (level) { + case ULOGD_DEBUG: + syslog_level = LOG_DEBUG; + break; + case ULOGD_INFO: + syslog_level = LOG_INFO; + break; + case ULOGD_NOTICE: + syslog_level = LOG_NOTICE; + break; + case ULOGD_ERROR: + syslog_level = LOG_ERR; + break; + case ULOGD_FATAL: + syslog_level = LOG_CRIT; + break; + } + return syslog_level; +} + /* log message to the logfile */ void __ulogd_log(int level, char *file, int line, const char *format, ...) { @@ -381,23 +408,30 @@ void __ulogd_log(int level, char *file, int line, const char *format, ...) if (level < loglevel) return; - if (logfile) - outfd = logfile; - else - outfd = stderr; + if (logfile == &syslog_dummy) { + /* FIXME: this omit's the 'file' string */ + va_start(ap, format); + vsyslog(ulogd2syslog_level(level), format, ap); + va_end(ap); + } else { + if (logfile) + outfd = logfile; + else + outfd = stderr; - va_start(ap, format); + va_start(ap, format); - tm = time(NULL); - timestr = ctime(&tm); - timestr[strlen(timestr)-1] = '\0'; - fprintf(outfd, "%s <%1.1d> %s:%d ", timestr, level, file, line); - - vfprintf(outfd, format, ap); - va_end(ap); + tm = time(NULL); + timestr = ctime(&tm); + timestr[strlen(timestr)-1] = '\0'; + fprintf(outfd, "%s <%1.1d> %s:%d ", timestr, level, file, line); + + vfprintf(outfd, format, ap); + va_end(ap); - /* flush glibc's buffer */ - fflush(outfd); + /* flush glibc's buffer */ + fflush(outfd); + } } /* propagate results to all registered output plugins */ @@ -474,9 +508,12 @@ static int load_plugin(char *file) /* open the logfile */ static int logfile_open(const char *name) { - if (!strcmp(name,"stdout")) { + if (!strcmp(name, "syslog")) { + openlog("ulogd", LOG_PID, LOG_DAEMON); + logfile = &syslog_dummy; + } if (!strcmp(name,"stdout")) logfile = stdout; - } else { + else { logfile = fopen(name, "a"); if (!logfile) { fprintf(stderr, "ERROR: can't open logfile %s: %s\n", @@ -560,7 +597,7 @@ static void sigterm_handler(int signal) ipulog_destroy_handle(libulog_h); free(libulog_buf); - if (logfile != stdout) + if (logfile != stdout && logfile != &syslog_dummy) fclose(logfile); for (p = ulogd_outputs; p; p = p->next) { @@ -575,7 +612,7 @@ static void sighup_handler(int signal) { ulog_output_t *p; - if (logfile != stdout) { + if (logfile != stdout && logfile != &syslog_dummy) { fclose(logfile); logfile = fopen(logf_ce.u.string, "a"); if (!logfile) -- cgit v1.2.3