Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 27 Sep 2002 20:36:52 +0200 (CEST)
From:      Aurélien Nephtali <aurelien.nephtali@wanadoo.fr>
To:        FreeBSD-gnats-submit@FreeBSD.org
Subject:   bin/43434: New option to 'dmesg' which allow to display or not old boot messages
Message-ID:  <200209271836.g8RIaqVo091644@nebula.wanadoo.fr>

next in thread | raw e-mail | index | archive | help

>Number:         43434
>Category:       bin
>Synopsis:       New option to 'dmesg' which allow to display or not old boot messages
>Confidential:   no
>Severity:       critical
>Priority:       high
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Fri Sep 27 11:40:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     dak
>Release:        FreeBSD 4.7-RC i386
>Organization:
none
>Environment:
System: FreeBSD nebula.wanadoo.fr 4.7-RC FreeBSD 4.7-RC #168: Fri Sep 27 07:44:50 CEST 2002 dak@nebula.wanadoo.fr:/usr/src/sys/compile/NEBULA i386


	
>Description:
	Some times it could be usefull to display only the last boot message (ie: when grep'ing).
	The attached patch modifies sys/kern/subr_prf.c and dmesg.c (and the also the manual).
	It adds a tag (---<<<BOOT>>>---) at the begining of each boot message (the tag
	macro is stored in sys/sys/msgbuf.h and could also be stored in /usr/include/msgbuf.h)
	dmesg.c is modified and has a new option: -o, which when it is specified, displays the full
	content of the message buffer, otherwise only the last message is displayed).
	This patch has been tested (by me :p) without any troubles on -CURRENT.
	
>How-To-Repeat:
	
>Fix:

--- current.patch begins here ---
--- sbin/dmesg/dmesg.c	Sun Aug 18 19:57:07 2002
+++ sbin/dmesg/dmesg.c	Sun Sep 15 20:12:42 2002
@@ -66,12 +66,35 @@
 	{ NULL },
 };
 
+int getlastbootpos(char *buf, int len);
 void usage(void) __dead2;
 
 #define	KREAD(addr, var) \
 	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
 
 int
+getlastbootpos(char *buf, int len)
+{
+	int i;
+	char *ptr, *save;
+
+	/* Skip NULL bytes */
+	for(i = 0; (buf[i] == 0x0) && (i < len); i++);
+
+	if (strlen(buf) == 0)
+		return(-1);
+
+	while((ptr = (char *) strstr((buf +i), BOOT_TAG)) != NULL) {
+		save = (char *) ptr;
+		i += strlen(BOOT_TAG); /* Force strstr() to catch
+					* the next tag
+					*/
+	}
+
+	return((int) ((int) (save + (strlen(BOOT_TAG))) - (int) buf));
+}
+
+int
 main(int argc, char *argv[])
 {
 	int ch, newl, skip;
@@ -84,10 +107,12 @@
 	int pri;
 	size_t buflen;
 	int bufpos;
+	int old = 0;
+	int lastboot = 0;
 
 	(void) setlocale(LC_CTYPE, "");
 	memf = nlistf = NULL;
-	while ((ch = getopt(argc, argv, "aM:N:")) != -1)
+	while ((ch = getopt(argc, argv, "aM:N:o")) != -1)
 		switch(ch) {
 		case 'a':
 			all++;
@@ -98,6 +123,9 @@
 		case 'N':
 			nlistf = optarg;
 			break;
+		case 'o':
+			old = 1;
+			break;
 		case '?':
 		default:
 			usage();
@@ -151,11 +179,19 @@
 	 * we effectively start at the oldest data.
 	 */
 	p = bp + bufpos;
+	if (!old) {
+		lastboot = getlastbootpos(p, buflen);
+		if (lastboot != -1)
+			p = bp + lastboot;
+	}
 	ep = (bufpos == 0 ? bp + buflen : p);
 	newl = skip = 0;
 	do {
 		if (p == bp + buflen)
 			p = bp;
+		/* Skipping boot tag */
+		if (!memcmp(p, BOOT_TAG, strlen(BOOT_TAG)))
+			p += strlen(BOOT_TAG);
 		ch = *p;
 		/* Skip "\n<.*>" syslog sequences. */
 		if (skip) {
@@ -193,6 +229,6 @@
 void
 usage(void)
 {
-	(void)fprintf(stderr, "usage: dmesg [-a] [-M core] [-N system]\n");
+	(void)fprintf(stderr, "usage: dmesg [-a] [-M core] [-N system] [-o]\n");
 	exit(1);
 }
--- sbin/dmesg/dmesg.8	Sat Aug 31 21:15:54 2002
+++ sbin/dmesg/dmesg.8	Sat Aug 31 20:57:27 2002
@@ -43,6 +43,7 @@
 .Op Fl a
 .Op Fl M Ar core
 .Op Fl N Ar system
+.Op Fl o
 .Sh DESCRIPTION
 The
 .Nm
@@ -73,6 +74,8 @@
 .It Fl N
 Extract the name list from the specified system instead of the default,
 which is the kernel image the system has booted from.
+.It Fl o
+Display all boot messages, not only the last.
 .El
 .Sh FILES
 .Bl -tag -width ".Pa /var/run/dmesg.boot" -compact
--- sys/sys/msgbuf.h	Sat Aug 31 21:07:05 2002
+++ sys/sys/msgbuf.h	Sat Aug 31 21:08:40 2002
@@ -46,6 +46,10 @@
 	char * 		msg_ptr;		/* pointer to buffer */
 };
 
+#define	BOOT_TAG  "---<<BOOT>>---"	/* used by dmesg util to know where a
+					 * boot message starts.
+					 */
+
 #ifdef _KERNEL
 extern int	msgbuftrigger;
 extern struct	msgbuf *msgbufp;
--- sys/kern/subr_prf.c	Sat Aug 31 21:00:35 2002
+++ sys/kern/subr_prf.c	Sat Aug 31 21:01:01 2002
@@ -818,6 +818,9 @@
 		msgbufp->msg_size = (char *)msgbufp - cp;
 	}
 	msgbufp->msg_ptr = cp;
+	memcpy((void *) (msgbufp->msg_ptr + msgbufp->msg_bufx), BOOT_TAG,
+            strlen(BOOT_TAG));
+	msgbufp->msg_bufx += strlen(BOOT_TAG);
 	if (msgbufmapped && oldp != msgbufp)
 		msgbufcopy(oldp);
 	msgbufmapped = 1;
--- current.patch ends here ---


>Release-Note:
>Audit-Trail:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200209271836.g8RIaqVo091644>