]> git.ipfire.org Git - thirdparty/ulogd2.git/commitdiff
- fixes segfault bug when packet received but no interpreters registered (reported...
authorlaforge <laforge>
Sun, 4 Feb 2001 10:15:19 +0000 (10:15 +0000)
committerlaforge <laforge>
Sun, 4 Feb 2001 10:15:19 +0000 (10:15 +0000)
- fixes logfile bug: wrong filename and line number displayed
- fixes logfile bug: logfile is fflush()ed after each line (reported by Drori Ghiora)

include/ulogd/ulogd.h
ulogd.c
ulogd.conf

index b3468ce5a526bfac1c37ff912b5e663d838f573f..1eb2320bfc919b68522f8da2b0b5e9f4ec619548 100644 (file)
@@ -1,6 +1,6 @@
 #ifndef _ULOGD_H
 #define _ULOGD_H
-/* ulogd, Version $Revision: 1.9 $
+/* ulogd, Version $Revision: 1.10 $
  *
  * userspace logging daemon for netfilter ULOG target
  * of the linux 2.4 netfilter subsystem.
@@ -9,7 +9,7 @@
  *
  * this code is released under the terms of GNU GPL
  *
- * $Id: ulogd.h,v 1.9 2000/11/16 21:15:30 laforge Exp $
+ * $Id: ulogd.h,v 1.10 2000/11/20 11:43:22 laforge Exp $
  */
 
 #include <libipulog/libipulog.h>
@@ -131,7 +131,10 @@ void register_output(ulog_output_t *me);
 ulog_iret_t *alloc_ret(const u_int16_t type, const char*);
 
 /* write a message to the daemons' logfile */
-void ulogd_log(int level, const char *message, ...);
+void __ulogd_log(int level, char *file, int line, const char *message, ...);
+/* macro for logging including filename and line number */
+#define ulogd_log(level, format, ...) \
+       __ulogd_log(level, __FILE__, __LINE__, ## format)
 /* backwards compatibility */
 #define ulogd_error(format, args...) ulogd_log(ULOGD_ERROR, format, ## args)
 
diff --git a/ulogd.c b/ulogd.c
index faced73af3cde2c47b8eeeed56182054c55d46dc..a091b6f2f7dd2d3a35e5309ba1bed91c76a4d167 100644 (file)
--- a/ulogd.c
+++ b/ulogd.c
@@ -1,4 +1,4 @@
-/* ulogd, Version $Revision: 1.13 $
+/* ulogd, Version $Revision: 1.14 $
  *
  * userspace logging daemon for the netfilter ULOG target
  * of the linux 2.4 netfilter subsystem.
@@ -7,7 +7,7 @@
  *
  * this code is released under the terms of GNU GPL
  *
- * $Id: ulogd.c,v 1.13 2000/11/20 11:43:22 laforge Exp $
+ * $Id: ulogd.c,v 1.14 2001/01/29 11:45:22 laforge Exp $
  */
 
 #include <stdio.h>
@@ -15,6 +15,7 @@
 #include <string.h>
 #include <stdarg.h>
 #include <time.h>
+#include <signal.h>
 #include <dlfcn.h>
 #include <sys/types.h>
 #include <dirent.h>
 #define ULOGD_CONFIGFILE       "/etc/ulogd.conf"
 #endif
 
-
-FILE *logfile = NULL;
-int loglevel = 1;
+/* global variables */
+static struct ipulog_handle *libulog_h;        /* our libipulog handle */
+static unsigned char* libulog_buf;     /* the receive buffer */
+static FILE *logfile = NULL;           /* logfile pointer */
+static int loglevel = 1;               /* current loglevel */
 
 /* linked list for all registered interpreters */
 static ulog_interpreter_t *ulogd_interpreters;
@@ -319,10 +322,10 @@ void register_output(ulog_output_t *me)
 
 /***********************************************************************
  * MAIN PROGRAM
- ***********************************************************************
+ ***********************************************************************/
 
 /* log message to the logfile */
-void ulogd_log(int level, const char *format, ...)
+void __ulogd_log(int level, char *file, int line, const char *format, ...)
 {
        char *timestr;
        va_list ap;
@@ -343,10 +346,13 @@ void ulogd_log(int level, const char *format, ...)
        tm = time(NULL);
        timestr = ctime(&tm);
        timestr[strlen(timestr)-1] = '\0';
-       fprintf(outfd, "%s <%1.1d> %s:%d ", timestr, level, __FILE__, __LINE__);
+       fprintf(outfd, "%s <%1.1d> %s:%d ", timestr, level, file, line);
        
        vfprintf(outfd, format, ap);
        va_end(ap);
+
+       /* flush glibc's buffer */
+       fflush(outfd);
 }
 
 /* propagate results to all registered output plugins */
@@ -382,6 +388,14 @@ static void handle_packet(ulog_packet_msg_t *pkt)
 
        unsigned int i,j;
 
+       /* If there are no interpreters registered yet,
+        * ignore this packet */
+       if (!ulogd_interh_ids) {
+               ulogd_log(ULOGD_NOTICE, 
+                         "packet received, but no interpreters found\n");
+               return;
+       }
+
        for (i = 1; i <= ulogd_interh_ids; i++) {
                ip = ulogd_interh[i];
                /* call interpreter */
@@ -483,10 +497,18 @@ static int init_conffile(char *file)
        return parse_conffile(0);
 }
 
+static void sigterm_handler(int signal)
+{
+       ulogd_log(ULOGD_NOTICE, "sigterm received, exiting\n");
+
+       ipulog_destroy_handle(libulog_h);
+       free(libulog_buf);
+       fclose(logfile);
+       exit(0);
+}
+
 int main(int argc, char* argv[])
 {
-       struct ipulog_handle *h;
-       unsigned char* buf;
        size_t len;
        ulog_packet_msg_t *upkt;
 
@@ -510,11 +532,13 @@ int main(int argc, char* argv[])
 #endif
 
        /* allocate a receive buffer */
-       buf = (unsigned char *) malloc(MYBUFSIZ);
+       libulog_buf = (unsigned char *) malloc(MYBUFSIZ);
        
        /* create ipulog handle */
-       h = ipulog_create_handle(ipulog_group2gmask(nlgroup_ce.u.value));
-       if (!h) {
+       libulog_h = 
+               ipulog_create_handle(ipulog_group2gmask(nlgroup_ce.u.value));
+
+       if (!libulog_h) {
                /* if some error occurrs, print it to stderr */
                ulogd_log(ULOGD_FATAL, "unable to create ipulogd handle\n");
                ipulog_perror(NULL);
@@ -527,21 +551,24 @@ int main(int argc, char* argv[])
                fclose(stdout);
                fclose(stderr);
 #endif
+               signal(SIGTERM, &sigterm_handler);
+
+               ulogd_log(ULOGD_NOTICE, 
+                         "initialization finished, entering main loop\n");
 
                /* endless loop receiving packets and handling them over to
                 * handle_packet */
-               while(1) {
-                       len = ipulog_read(h, buf, MYBUFSIZ, 1);
-                       while(upkt = ipulog_get_packet(h, buf, len)) {
+               while(len = ipulog_read(libulog_h, libulog_buf, MYBUFSIZ, 1)) {
+                       while(upkt = ipulog_get_packet(libulog_h,
+                                                      libulog_buf, len)) {
                                DEBUGP("==> packet received\n");
                                handle_packet(upkt);
                        }
                }
-       
-               /* just to give it a cleaner look */
-               ipulog_destroy_handle(h);
-               free(buf);
-               fclose(logfile);
+
+               /* hackish, but result is the same */
+               sigterm_handler(SIGHUP);        
+
 #ifndef DEBUG
        } else {
                exit(0);
index 5246aa91722ed1a86b7a4100927a390720ffaa61..8a026b2cbdfef1efa4d4d8ddd683a882e64d45d8 100644 (file)
@@ -12,6 +12,6 @@ mysqlpass changeme
 mysqluser laforge
 mysqldb ulogd
 mysqlhost localhost
-# plugins: first load BASE interpreter, then syslog emulation
+# plugins: always load interpreter plugins _before_ any output plugins
 plugin /usr/local/lib/ulogd/ulogd_BASE.so
 plugin /usr/local/lib/ulogd/ulogd_LOGEMU.so