Subject: Re: An improvment to ipfstat -t state top
To: NetBSD netbsd-users mailing list <netbsd-users@netbsd.org>
From: Julian Coleman <jdc@coris.org.uk>
List: netbsd-users
Date: 04/07/2004 15:53:28
> 	I want to share a modification I made to my netbsd system with the
> public.  A while ago I ran "ipfstat -t" and let it run for a while, then I
> closed the terminal window without hitting 'q' to gracefully exit the
> program.  A little while later, I noticed ipfstat was still running and
> was cpu bound.
> 
> 	This seems to be related to the curses interface not returning a
> proper status code for the printw function, and the fils.c code not
> checking for a return code anyway.  printw returns ERR upon error, however
> if I close the terminal this doesn't seem to be considered an error.  I
> searched for a way to check for a closed terminal window using the curses
> library, but I can't find one so I used isatty from the std C library.  I
> thnk its portable and won't interfere with curses during normal operation.

The way that the curses code works is that changes are made to an internal
buffer and the terminal is updated when refresh() (and friends) is called, 
So, one would expect an error return from refresh() if the terminal has gone
away.  Looking at our refresh() code, we ignore any error when we do the
terminal update.  The first appended patch fixes this.  With that in place,
the second patch should make ipfstat do the right thing.  I've not tested
the second patch though (when I run ipfstat and close that window, ipfstat
also exits).

J

  - - 8< - - - - - - - - - - - - - Cut here - - - - - - - - - - - - - >8 - -
--- /usr/src/lib/libcurses/refresh.c	2003-08-08 04:11:00.000000000 +0100
+++ refresh.c	2004-04-07 15:16:26.000000000 +0100
@@ -511,8 +511,11 @@
 
 	/* Don't leave the screen with attributes set. */
 	__unsetattr(0);
-	(void) fflush(_cursesi_screen->outfd);
-	return (OK);
+
+	if (fflush(_cursesi_screen->outfd) == EOF)
+		return (ERR);
+	else
+		return (OK);
 }
 
 /*
  - - 8< - - - - - - - - - - - - - Cut here - - - - - - - - - - - - - >8 - -
--- /usr/src/dist/ipf/fils.c	2003-08-16 04:05:17.000000000 +0100
+++ fils.c	2004-04-07 15:39:25.000000000 +0100
@@ -1204,7 +1204,8 @@
 		if (redraw)
 			clearok(stdscr,1);
 
-		refresh();
+		if (refresh() == ERR)
+			break;
 		if (redraw) {
 			clearok(stdscr,0);
 			redraw = 0;
  - - 8< - - - - - - - - - - - - - Cut here - - - - - - - - - - - - - >8 - -

-- 
  My other computer also runs NetBSD    /        Sailing at Newbiggin
        http://www.netbsd.org/        /   http://www.newbigginsailingclub.org/