Subject: Re: /kern/msgbuf & less
To: None <port-i386@netbsd.org>
From: David Laight <david@l8s.co.uk>
List: port-i386
Date: 10/11/2002 20:56:13
On Thu, Oct 10, 2002 at 11:25:25PM +0400, Valeriy E. Ushakov wrote:
> [Inadvertently deleted the thread, so sorry for an orhpaned reply.]
> 
> I think the problem is that less(1) trusts the file size returned by
> stat(2) and, as Simon noted in his reply, it seems that stat(2)
> returns the amount of free space in the msgbug, not the amount of
> space used.

Actually stat() is reporting the amount of space from the write
point in the buffer to the end of the buffer.

The following patch fixes makes stat report the correct size,
it also stops the \0  bytes being reported.

	David

Index: kern/subr_prf.c
===================================================================
RCS file: /cvsroot/syssrc/sys/kern/subr_prf.c,v
retrieving revision 1.85
diff -u -r1.85 subr_prf.c
--- kern/subr_prf.c	2002/08/26 11:34:27	1.85
+++ kern/subr_prf.c	2002/10/11 19:48:40
@@ -423,8 +423,10 @@
 			msgbufenabled = 0;
 		} else {
 			mbp->msg_bufc[mbp->msg_bufx++] = c;
-			if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
+			if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs) {
 				mbp->msg_bufx = 0;
+				msgbufwrapped = 1;
+			}
 			/* If the buffer is full, keep the most recent data. */
 			if (mbp->msg_bufr == mbp->msg_bufx) {
 				 if (++mbp->msg_bufr >= mbp->msg_bufs)
Index: kern/subr_log.c
===================================================================
RCS file: /cvsroot/syssrc/sys/kern/subr_log.c,v
retrieving revision 1.23
diff -u -r1.23 subr_log.c
--- kern/subr_log.c	2002/09/06 13:23:50	1.23
+++ kern/subr_log.c	2002/10/11 19:48:58
@@ -69,6 +69,7 @@
 int	log_open;			/* also used in log() */
 int	msgbufmapped;			/* is the message buffer mapped */
 int	msgbufenabled;			/* is logging to the buffer enabled */
+int	msgbufwrapped;			/* has the buffer wrapped */
 struct	kern_msgbuf *msgbufp;		/* the mapped buffer, itself. */
 
 dev_type_open(logopen);
@@ -109,7 +110,9 @@
 		memset(buf, 0, bufsize);
 		mbp->msg_magic = MSG_MAGIC;
 		mbp->msg_bufs = new_bufs;
-	}
+	} else
+		if (mbp->msg_bufc[mbp->msg_bufx])
+			msgbufwrapped = 1;
 
 	/* mark it as ready for use. */
 	msgbufmapped = msgbufenabled = 1;
Index: miscfs/kernfs/kernfs_vnops.c
===================================================================
RCS file: /cvsroot/syssrc/sys/miscfs/kernfs/kernfs_vnops.c,v
retrieving revision 1.83
diff -u -r1.83 kernfs_vnops.c
--- miscfs/kernfs/kernfs_vnops.c	2002/08/03 04:52:44	1.83
+++ miscfs/kernfs/kernfs_vnops.c	2002/10/11 19:49:21
@@ -251,6 +251,8 @@
 		 * message buffer header are corrupted, but that'll cause
 		 * the system to die anyway.
 		 */
+		if (!msgbufwrapped)
+			off += msgbufp->msg_bufs - msgbufp->msg_bufx;
 		if (off >= msgbufp->msg_bufs) {
 			*wrlen = 0;
 			return (0);
@@ -508,9 +510,15 @@
 		vap->va_nlink = 1;
 		vap->va_fileid = 1 + (kt - kern_targets);
 		buf = strbuf;
-		if (0 == (error = kernfs_xread(kt, 0, &buf,
-				sizeof(strbuf), &total)))
-			vap->va_size = total;
+		if (kt->kt_tag == KTT_MSGBUF && msgbufenabled) {
+			if (msgbufwrapped)
+				vap->va_size = msgbufp->msg_bufs;
+			else
+				vap->va_size = msgbufp->msg_bufx;
+		} else
+			if (0 == (error = kernfs_xread(kt, 0, &buf,
+					sizeof(strbuf), &total)))
+				vap->va_size = total;
 	}
 
 #ifdef KERNFS_DIAGNOSTIC

-- 
David Laight: david@l8s.co.uk