Index: busybox/sysklogd/klogd.c
===================================================================
--- busybox/sysklogd/klogd.c	(revision 1855)
+++ busybox/sysklogd/klogd.c	(working copy)
@@ -44,6 +44,8 @@
 	int i = 0;
 	char *start;
 	int opt;
+	int priority = LOG_INFO;
+	int used = 0;
 
 	opt = getopt32(argv, "c:n", &start);
 	if (opt & OPT_LEVEL) {
@@ -72,16 +74,14 @@
 
 	syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
 
-	/* Note: this code does not detect incomplete messages
-	 * (messages not ending with '\n' or just when kernel
-	 * generates too many messages for us to keep up)
-	 * and will split them in two separate lines */
+	/* Initially null terminate the buffer in case of a very long line */
+	log_buffer[KLOGD_LOGBUF_SIZE - 1] = '\0';
+
 	while (1) {
 		int n;
-		int priority;
 
 		/* "2 -- Read from the log." */
-		n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
+		n = klogctl(2, log_buffer + used, KLOGD_LOGBUF_SIZE - used - 1);
 		if (n < 0) {
 			if (errno == EINTR)
 				continue;
@@ -89,32 +89,48 @@
 					errno);
 			break;
 		}
-		log_buffer[n] = '\n';
-		i = 0;
-		while (i < n) {
-			priority = LOG_INFO;
-			start = &log_buffer[i];
-			if (log_buffer[i] == '<') {
-				i++;
-				// kernel never ganerates multi-digit prios
-				//priority = 0;
-				//while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
-				//	priority = priority * 10 + (log_buffer[i] - '0');
-				//	i++;
-				//}
-				if (isdigit(log_buffer[i])) {
-					priority = (log_buffer[i] - '0');
-					i++;
+
+		/* klogctl buffer parsing modelled after code in dmesg.c */
+		start = &log_buffer[0];
+
+		/* Process each newline-terminated line in the buffer */
+		while (1) {
+			char *newline = memchr(start, '\n', n);
+
+			if (newline) {
+				*newline++ = 0;
+				n -= (newline - start);
+			}
+
+			/* Extract the priority */
+			if (*start == '<') {
+				priority = LOG_INFO;
+				start++;
+				if (*start) {
+					/* kernel never generates multi-digit prios */
+					priority = (*start - '0');
+					start++;
 				}
-				if (log_buffer[i] == '>')
-					i++;
-				start = &log_buffer[i];
+				if (*start == '>') {
+					start++;
+				}
 			}
-			while (log_buffer[i] != '\n')
-				i++;
-			log_buffer[i] = '\0';
-			syslog(priority, "%s", start);
-			i++;
+
+			if (*start) {
+				syslog(priority, "%s", start);
+			}
+
+			if (newline) {
+				start = newline;
+			}
+			else {
+				if (start != log_buffer) {
+					/* This line is incomplete, so move it to the front of the buffer */
+					memmove(log_buffer, start, n);
+					used = n;
+				}
+				break;
+			}
 		}
 	}
 
