[BusyBox] klogd patch (addresses bug #1061)

Larry Doolittle ldoolitt at recycle.lbl.gov
Sat Dec 9 23:26:51 UTC 2000


This patch _probably_ fixes the issue with klogd parsing
of multiple lines returned from klogctl.  I really don't
have a good test setup at the moment, although I can run
some slightly better tests on Monday.

The old behavior looked wrong both for local file logging,
and sending to a remote server.  Gyepi's patch from a
month ago wasn't quite right either, it would fail if
a log message had a "<1>" in the middle of it.

If anyone else can test this, please do.

         - Larry Doolittle  <LRDoolittle at lbl.gov>


diff -ur /home/larry/cvs/busybox/syslogd.c busybox-0.48pre/syslogd.c
--- /home/larry/cvs/busybox/syslogd.c	Fri Dec  8 18:24:23 2000
+++ busybox-0.48pre/syslogd.c	Sat Dec  9 15:14:37 2000
@@ -403,7 +403,8 @@
 {
 	int priority = LOG_INFO;
 	char log_buffer[4096];
-	char *logp;
+	int i, n, lastc;
+	char *start;
 
 	/* Set up sig handlers */
 	signal(SIGINT, klogd_signal);
@@ -420,12 +421,14 @@
 	logMessage(0, "klogd started: "
 			   "BusyBox v" BB_VER " (" BB_BT ")");
 
+	/* "Open the log. Currently a NOP." */
 	klogctl(1, NULL, 0);
 
 	while (1) {
 		/* Use kernel syscalls */
 		memset(log_buffer, '\0', sizeof(log_buffer));
-		if (klogctl(2, log_buffer, sizeof(log_buffer)) < 0) {
+		n = klogctl(2, log_buffer, sizeof(log_buffer));
+		if (n < 0) {
 			char message[80];
 
 			if (errno == EINTR)
@@ -435,37 +438,29 @@
 			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':
+
+		/* klogctl buffer parsing modelled after code in dmesg.c */
+		start=&log_buffer[0];
+		lastc='\0';
+		for (i=0; i<n; i++) {
+			if (lastc == '\0' && log_buffer[i] == '<') {
+				priority = 0;
+				i++;
+				while (isdigit(log_buffer[i])) {
+					priority = priority*10+(log_buffer[i]-'0');
+					i++;
+				}
+				if (log_buffer[i] == '>') i++;
+				start = &log_buffer[i];
+			}
+			if (log_buffer[i] == '\n') {
+				log_buffer[i] = '\0';  /* zero terminate this message */
+				logMessage(LOG_KERN | priority, start);
+				start = &log_buffer[i+1];
 				priority = LOG_INFO;
-				break;
-			case '7':
-			default:
-				priority = LOG_DEBUG;
 			}
-			logp += 3;
+			lastc = log_buffer[i];
 		}
-		logMessage(LOG_KERN | priority, logp);
 	}
 
 }





More information about the busybox mailing list