bug#1061: [BusyBox] bug#1061: remote logging a bit broken

Gyepi SAM gyepi at praxis-sw.com
Sat Oct 14 04:52:09 UTC 2000


The attached patch fixes the problem where klogd slurps up
and sends all buffered log messages to the remote logger as a single
log item.
It now separates and sends each log entry individually.

It also increases busybox size by 160 bytes. Sorry about that.

-Gyepi

On Thu, Oct 12, 2000 at 11:44:08AM +0200, Gergely Madarasz wrote:
> On Wed, Oct 11, 2000 at 08:41:33PM -0400, Gyepi SAM wrote:
> > > it should split the message into lines, and it should remove the
> > > priorities (KERN_INFO, etc, which are in <>)
> the problem seems to be in the klogd part of the
> busybox syslogd, it reads the kernel log when started in one part.

-------------- next part --------------
--- syslogd.c.orig	Fri Oct 13 22:18:30 2000
+++ syslogd.c	Sat Oct 14 00:38:09 2000
@@ -7,6 +7,8 @@
  *
  * Copyright (C) 2000 by Karl M. Hegbloom <karlheg at debian.org>
  *
+ * Copyright (C) 2000 by Gyepi Sam <gyepi at praxis-sw.com>
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -171,7 +173,7 @@
           struct iovec iov[IOV_COUNT];
           struct iovec *v = iov;
 
-          bzero(&res, sizeof(res));
+	  memset(&res, 0, sizeof(res));
           snprintf(res, sizeof(res), "<%d>", pri);
           v->iov_base = res ;
           v->iov_len = strlen(res);          
@@ -255,8 +257,7 @@
   struct hostent *hostinfo;
   int len = sizeof(remoteaddr);
 
-  bzero(&remoteaddr, len);
-  
+  memset(&remoteaddr, 0, len);
   remotefd = socket(AF_INET, SOCK_DGRAM, 0);
 
   if (remotefd < 0) {
@@ -388,6 +389,54 @@
 
 #ifdef BB_FEATURE_KLOGD
 
+char *logline(char *bufptr, int *priority)
+{
+  char w[3] = { "", "", "" };	/* sliding character window */
+
+  if (bufptr == NULL){
+    return bufptr;
+  }
+
+  while (*bufptr) {
+    if (w[0] == '<' && w[2] == '>') {
+      switch (w[1]) {
+      case '0':
+	*priority = LOG_EMERG;
+	break;
+      case '1':
+	*priority = LOG_ALERT;
+	break;
+      case '2':
+	*priority = LOG_CRIT;
+	break;
+      case '3':
+	*priority = LOG_ERR;
+	break;
+      case '4':
+	*priority = LOG_WARNING;
+	break;
+      case '5':
+	*priority = LOG_NOTICE;
+	break;
+      case '6':
+	*priority = LOG_INFO;
+	break;
+      case '7':
+      default:
+	*priority = LOG_DEBUG;
+      }
+
+      return bufptr;
+
+    } else {
+      w[0] = w[1];
+      w[1] = w[2];
+      w[2] = *(bufptr++);
+    }
+  }
+  return (char *) NULL;
+}
+
 static void klogd_signal(int sig)
 {
 	klogctl(7, NULL, 0);
@@ -399,9 +448,11 @@
 static void doKlogd (void) __attribute__ ((noreturn));
 static void doKlogd (void)
 {
-	int priority = LOG_INFO;
+
 	char log_buffer[4096];
-	char *logp;
+	int start_priority = LOG_INFO;
+	int end_priority = LOG_INFO;
+	char *startptr, *endptr;
 
 	/* Set up sig handlers */
 	signal(SIGINT, klogd_signal);
@@ -433,39 +484,34 @@
 			logMessage(LOG_SYSLOG | LOG_ERR, message);
 			exit(1);
 		}
-		logp = log_buffer;
-		if (*log_buffer == '<') {
-			switch (*(log_buffer + 1)) {
-			case '0':
-				priority = LOG_EMERG;
-				break;
-			case '1':
-				priority = LOG_ALERT;
-				break;
-			case '2':
-				priority = LOG_CRIT;
-				break;
-			case '3':
-				priority = LOG_ERR;
-				break;
-			case '4':
-				priority = LOG_WARNING;
-				break;
-			case '5':
-				priority = LOG_NOTICE;
-				break;
-			case '6':
-				priority = LOG_INFO;
-				break;
-			case '7':
-			default:
-				priority = LOG_DEBUG;
+		
+		startptr = logline(log_buffer, &start_priority);
+
+		if (startptr > log_buffer + 3) {
+		  /* 
+		     deal with case where buffer does not begin with 
+		     priority code .
+		     I think it is better to have badly formatted data
+		     than to lose data. -- gsam
+		  */
+		  startptr = log_buffer;
+		  start_priority = LOG_INFO;
 			}
-			logp += 3;
+
+		while (startptr) {
+		  /* 
+		     Find out where this log entry ends by looking 
+		     for the next entry.
+		  */
+		  endptr = logline(startptr, &end_priority);
+		  if (endptr) {
+		    *(endptr - 3) = '\0';
+		  }
+		  logMessage(LOG_KERN | start_priority, startptr);
+		  startptr = endptr;
+		  start_priority = end_priority;
 		}
-		logMessage(LOG_KERN | priority, logp);
 	}
-
 }
 
 #endif


More information about the busybox mailing list