more userland backporting

Chris Pressey cpressey at catseye.mine.nu
Mon Mar 1 14:03:54 PST 2004


All obtained from FreeBSD:

dmesg.c.diff
src/sbin/dmesg/dmesg.c 1.11.2.4
"don't print the oldest line in the message buffer if the buffer is
full, since that line is almost always incomplete. Make the parsing of
<%d> lines more strict, and simplify the logic."

ping.c.diff
src/sbin/ping/ping.c 1.52.2.14
"exit immediately if a second SIGINT is received, to avoid long delays
where ^C is ineffective during certain DNS lokups."

ndp.c.diff
src/usr.sbin/ndp/ndp.c 1.2.2.7
"Flush stdout when -A option is used."

-Chris
Index: sbin/dmesg/dmesg.c
===================================================================
RCS file: /home/dcvs/src/sbin/dmesg/dmesg.c,v
retrieving revision 1.5
diff -u -r1.5 dmesg.c
--- sbin/dmesg/dmesg.c	4 Feb 2004 17:39:59 -0000	1.5
+++ sbin/dmesg/dmesg.c	1 Mar 2004 13:19:54 -0000
@@ -41,11 +41,14 @@
 #include <sys/sysctl.h>
 
 #include <err.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <kvm.h>
+#include <limits.h>
 #include <locale.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <vis.h>
 #include <sys/syslog.h>
@@ -64,17 +67,14 @@
 int
 main(int argc, char **argv)
 {
-	int ch, newl, skip;
-	char *p, *ep;
 	struct msgbuf *bufp, cur;
-	char *bp, *memf, *nlistf;
+	char *bp, *ep, *memf, *nextp, *nlistf, *p, *q, *visbp;
 	kvm_t *kd;
-	char buf[5];
-	int all = 0;
-	int pri = 0;
-	size_t buflen;
-	int bufpos;
+	size_t buflen, bufpos;
+	long pri = 0;
+	int all, ch;
 
+	all = 0;
 	(void) setlocale(LC_CTYPE, "");
 	memf = nlistf = NULL;
 	while ((ch = getopt(argc, argv, "aM:N:")) != -1)
@@ -96,17 +96,18 @@
 	argv += optind;
 
 	if (memf == NULL && nlistf == NULL) {
-		/* Running kernel. Use sysctl. */
+		/*
+		 * Running kernel.  Use sysctl.  This gives an unwrapped
+		 * buffer as a side effect.
+		 */
 		if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
 			err(1, "sysctl kern.msgbuf");
-		if ((bp = malloc(buflen)) == NULL)
+		if ((bp = malloc(buflen + 2)) == NULL)
 			errx(1, "malloc failed");
 		if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
 			err(1, "sysctl kern.msgbuf");
-		/* We get a dewrapped buffer using sysctl. */
-		bufpos = 0;
 	} else {
-		/* Read in kernel message buffer, do sanity checks. */
+		/* Read in kernel message buffer and do sanity checks. */
 		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
 		if (kd == NULL)
 			exit (1);
@@ -120,63 +121,67 @@
 		if (cur.msg_magic != MSG_MAGIC)
 			errx(1, "kernel message buffer has different magic "
 			    "number");
-		bp = malloc(cur.msg_size);
-		if (!bp)
+		if ((bp = malloc(cur.msg_size + 2)) == NULL)
 			errx(1, "malloc failed");
-		if (kvm_read(kd, (long)cur.msg_ptr, bp, cur.msg_size) !=
-		    cur.msg_size)
-			errx(1, "kvm_read: %s", kvm_geterr(kd));
-		kvm_close(kd);
-		buflen = cur.msg_size;
+
+		/* Unwrap the circular buffer to start from the oldest data. */
 		bufpos = cur.msg_bufx;
 		if (bufpos >= buflen)
 			bufpos = 0;
+		if (kvm_read(kd, (long)&cur.msg_ptr[bufpos], bp,
+		    cur.msg_size - bufpos) != cur.msg_size - bufpos)
+			errx(1, "kvm_read: %s", kvm_geterr(kd));
+		if (bufpos != 0 && kvm_read(kd, (long)cur.msg_ptr,
+		    &bp[cur.msg_size - bufpos], bufpos) != bufpos)
+			errx(1, "kvm_read: %s", kvm_geterr(kd));
+		kvm_close(kd);
+		buflen = cur.msg_size;
 	}
 
 	/*
-	 * The message buffer is circular.  If the buffer has wrapped, the
-	 * write pointer points to the oldest data.  Otherwise, the write
-	 * pointer points to \0's following the data.  Read the entire
-	 * buffer starting at the write pointer and ignore nulls so that
-	 * we effectively start at the oldest data.
+	 * Ensure that the buffer ends with a newline and a \0 to avoid
+	 * complications below.  We left space above.
 	 */
-	p = bp + bufpos;
-	ep = (bufpos == 0 ? bp + buflen : p);
-	newl = skip = 0;
-	do {
-		if (p == bp + buflen)
-			p = bp;
-		ch = *p;
-		/* Skip "\n<.*>" syslog sequences. */
-		if (skip) {
-			if (ch == '\n') {
-				skip = 0;
-				newl = 1;
-			} if (ch == '>') {
-				if (LOG_FAC(pri) == LOG_KERN || all)
-					newl = skip = 0;
-			} else if (ch >= '0' && ch <= '9') {
-				pri *= 10;
-				pri += ch - '0';
+	if (buflen == 0 || bp[buflen - 1] != '\n')
+		bp[buflen++] = '\n';
+	bp[buflen] = '\0';
+
+	if ((visbp = malloc(4 * buflen + 1)) == NULL)
+		errx(1, "malloc failed");
+
+	/*
+	 * The message buffer is circular, but has been unwrapped so that
+	 * the oldest data comes first.  The data will be preceded by \0's
+	 * if the message buffer was not full.
+	 */
+	p = bp;
+	ep = &bp[buflen];
+	if (*p == '\0') {
+		/* Strip leading \0's */
+		while (*p == '\0')
+			p++;
+	} else if (!all) {
+		/* Skip the first line, since it is probably incomplete. */
+		p = memchr(p, '\n', ep - p) + 1;
+	}
+	for (; p < ep; p = nextp) {
+		nextp = memchr(p, '\n', ep - p) + 1;
+
+		/* Skip ^<[0-9]+> syslog sequences. */
+		if (*p == '<') {
+			errno = 0;
+			pri = strtol(p + 1, &q, 10);
+			if (*q == '>' && pri >= 0 && pri < INT_MAX &&
+			    errno == 0) {
+				if (LOG_FAC(pri) != LOG_KERN && !all)
+					continue;
+				p = q + 1;
 			}
-			continue;
-		}
-		if (newl && ch == '<') {
-			pri = 0;
-			skip = 1;
-			continue;
 		}
-		if (ch == '\0')
-			continue;
-		newl = ch == '\n';
-		(void)vis(buf, ch, 0, 0);
-		if (buf[1] == 0)
-			(void)putchar(buf[0]);
-		else
-			(void)printf("%s", buf);
-	} while (++p != ep);
-	if (!newl)
-		(void)putchar('\n');
+
+		(void)strvisx(visbp, p, nextp - p, 0);
+		(void)printf("%s", visbp);
+	}
 	exit(0);
 }
 
Index: sbin/ping/ping.c
===================================================================
RCS file: /home/dcvs/src/sbin/ping/ping.c,v
retrieving revision 1.3
diff -u -r1.3 ping.c
--- sbin/ping/ping.c	28 Sep 2003 14:39:20 -0000	1.3
+++ sbin/ping/ping.c	1 Mar 2004 13:32:31 -0000
@@ -727,6 +727,12 @@
 stopit(int sig __unused)
 {
 
+	/*
+	 * When doing reverse DNS lookups, the finish_up flag might not
+	 * be noticed for a while.  Just exit if we get a second SIGINT.
+	 */
+	if (!(options & F_NUMERIC) && finish_up)
+		_exit(nreceived ? 0 : 2);
 	finish_up = 1;
 }
 
Index: usr.sbin/ndp/ndp.c
===================================================================
RCS file: /home/dcvs/src/usr.sbin/ndp/ndp.c,v
retrieving revision 1.3
diff -u -r1.3 ndp.c
--- usr.sbin/ndp/ndp.c	3 Nov 2003 19:31:39 -0000	1.3
+++ usr.sbin/ndp/ndp.c	1 Mar 2004 13:56:58 -0000
@@ -744,6 +744,7 @@
 
 	if (repeat) {
 		printf("\n");
+		fflush(stdout);
 		sleep(repeat);
 		goto again;
 	}




More information about the Submit mailing list