From andrew@nas.dgap.mipt.ru  Sun Sep 30 12:29:39 2001
Return-Path: <andrew@nas.dgap.mipt.ru>
Received: from nas.dgap.mipt.ru (nas.dgap.mipt.ru [194.85.81.203])
	by hub.freebsd.org (Postfix) with ESMTP id 9DA0A37B406
	for <FreeBSD-gnats-submit@freebsd.org>; Sun, 30 Sep 2001 12:29:37 -0700 (PDT)
Received: (from andrew@localhost)
	by nas.dgap.mipt.ru (8.11.3/8.11.3) id f8UJTZS24778;
	Sun, 30 Sep 2001 23:29:35 +0400 (MSD)
	(envelope-from andrew)
Message-Id: <200109301929.f8UJTZS24778@nas.dgap.mipt.ru>
Date: Sun, 30 Sep 2001 23:29:35 +0400 (MSD)
From: andrew@nas.dgap.mipt.ru
Reply-To: andr@dgap.mipt.ru
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: top(1) behaves badly when it loses terminal
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         30939
>Category:       bin
>Synopsis:       top(1) behaves badly when it loses terminal
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Sep 30 12:30:00 PDT 2001
>Closed-Date:    Wed Nov 21 02:41:25 PST 2001
>Last-Modified:  Wed Nov 21 02:41:40 PST 2001
>Originator:     Andrew L. Neporada
>Release:        FreeBSD 4.3-RELEASE i386
>Organization:
>Environment:
System: FreeBSD nas.dgap.mipt.ru 4.3-RELEASE FreeBSD 4.3-RELEASE #0: Mon May 7 20:16:53 MSD 2001 root@nas.dgap.mipt.ru:/usr/obj/usr/src/sys/NAS i386

>Description:

After losing its control terminal top(1) eats all available CPU resources
(when running under root privileges).

>How-To-Repeat:

Example session:

andrew@nas > ssh localhost
socket: Protocol not supported
andrew@localhost's password:
.
.
.
andrew@nas > su
Password:
nas# top

Then enter ~. to kill ssh and launch top or ps waux. 


>Fix:

Maybe it is better to just exit() instead of sleep() but anyway ...

--- contrib/top/top.c.orig	Sun Sep 30 21:30:08 2001
+++ contrib/top/top.c	Sun Sep 30 22:51:23 2001
@@ -157,6 +157,7 @@
     int topn = Default_TOPN;
     int delay = Default_DELAY;
     int displays = 0;		/* indicates unspecified */
+    int sel_ret = 0;
     time_t curr_time;
     char *(*get_userid)() = username;
     char *uname_field = "USERNAME";
@@ -711,7 +712,9 @@
 		}
 
 		/* wait for either input or the end of the delay period */
-		if (select(32, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timeout) > 0)
+		sel_ret = select(2, &readfds, (fd_set *)NULL, (fd_set *)NULL,
+				 &timeout);
+		if (sel_ret > 0)
 		{
 		    int newval;
 		    char *errmsg;
@@ -721,7 +724,12 @@
 
 		    /* now read it and convert to command strchr */
 		    /* (use "change" as a temporary to hold strchr) */
-		    (void) read(0, &ch, 1);
+		    /* sleep if input from stdin causes error */
+		    if (read(0, &ch, 1) != 1)
+		    {
+			ch = '\r';
+			sleep(delay);
+		    }
 		    if ((iptr = strchr(command_chars, ch)) == NULL)
 		    {
 			if (ch != '\r' && ch != '\n')
@@ -960,6 +968,11 @@
 
 		    /* flush out stuff that may have been written */
 		    fflush(stdout);
+		}
+		/* sleep if input from stdin causes error */
+		if (sel_ret < 0)
+		{
+		    sleep(delay);
 		}
 	    }
 	}
>Release-Note:
>Audit-Trail:

From: Giorgos Keramidas <charon@labs.gr>
To: andrew@nas.dgap.mipt.ru
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/30939: top(1) behaves badly when it loses terminal
Date: Mon, 1 Oct 2001 07:52:24 +0300

 andrew@nas.dgap.mipt.ru <andrew@nas.dgap.mipt.ru> wrote:
 > 
 > >Number:         30939
 > >Category:       bin
 > >Synopsis:       top(1) behaves badly when it loses terminal
 
 This looks like a duplicate of PR bin/30581.
 The old PR does not contain a lot of information though,
 so perhaps bin/30581 should be closed ?
 
 I'll test the patch of this PR (bin/30939) later tonight,
 and see what happens...
 
 -giorgos

From: David Malone <dwmalone@maths.tcd.ie>
To: freebsd-gnats-submit@FreeBSD.org
Cc: andr@dgap.mipt.ru, charon@labs.gr
Subject: Re: bin/30939: top(1) behaves badly when it loses terminal
Date: Mon, 29 Oct 2001 16:57:03 +0000

 Here is a version of Andrew's patch which calls top's quit function
 instead of sleeping. It also checks for EINTR after selecting,
 which means that top doesn't quit when you press ^Z.
 
 I've tested this by running top in an xterm and then revoking the
 tty it is running on. Without the patch both top and xterm chew
 CPU time. With the patch they both exit nicely.
 
 I think this file is off the vendor branch already, so I'll commit
 this fix later in the week.
 
 	David.
 
 Index: top.c
 ===================================================================
 RCS file: /cvs/FreeBSD-CVS/src/contrib/top/top.c,v
 retrieving revision 1.7
 diff -u -r1.7 top.c
 --- top.c	6 Aug 2001 03:19:22 -0000	1.7
 +++ top.c	29 Oct 2001 16:06:33 -0000
 @@ -32,6 +32,7 @@
   */
  
  #include "os.h"
 +#include <errno.h>
  #include <signal.h>
  #include <setjmp.h>
  #include <ctype.h>
 @@ -157,6 +158,7 @@
      int topn = Default_TOPN;
      int delay = Default_DELAY;
      int displays = 0;		/* indicates unspecified */
 +    int sel_ret = 0;
      time_t curr_time;
      char *(*get_userid)() = username;
      char *uname_field = "USERNAME";
 @@ -711,7 +713,10 @@
  		}
  
  		/* wait for either input or the end of the delay period */
 -		if (select(32, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timeout) > 0)
 +		sel_ret = select(2, &readfds, NULL, NULL, &timeout);
 +		if (sel_ret < 0 && errno != EINTR)
 +		    quit(0);
 +		if (sel_ret > 0)
  		{
  		    int newval;
  		    char *errmsg;
 @@ -721,7 +726,8 @@
  
  		    /* now read it and convert to command strchr */
  		    /* (use "change" as a temporary to hold strchr) */
 -		    (void) read(0, &ch, 1);
 +		    if (read(0, &ch, 1) != 1)
 +			quit(0);
  		    if ((iptr = strchr(command_chars, ch)) == NULL)
  		    {
  			if (ch != '\r' && ch != '\n')

From: Edwin Groothuis <edwin@mavetju.org>
To: David Malone <dwmalone@maths.tcd.ie>,
	freebsd-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: bin/30939: top(1) behaves badly when it loses terminal
Date: Tue, 30 Oct 2001 08:45:15 +1100

 On Mon, Oct 29, 2001 at 09:00:04AM -0800, David Malone wrote:
 > The following reply was made to PR bin/30939; it has been noted by GNATS.
 > 
 > From: David Malone <dwmalone@maths.tcd.ie>
 > To: freebsd-gnats-submit@FreeBSD.org
 > Cc: andr@dgap.mipt.ru, charon@labs.gr
 > Subject: Re: bin/30939: top(1) behaves badly when it loses terminal
 > Date: Mon, 29 Oct 2001 16:57:03 +0000
 
 See also pr bin/30581:
 	    top(1) races when it loses its terminal
 That one can be closed afterwards too.
 
 Edwin
 
 -- 
 Edwin Groothuis   |              Personal website: http://www.MavEtJu.org
 edwin@mavetju.org |           Interested in MUDs? Visit Fatal Dimensions:
 ------------------+                       http://www.FatalDimensions.org/
State-Changed-From-To: open->closed 
State-Changed-By: dwmalone 
State-Changed-When: Wed Nov 21 02:41:25 PST 2001 
State-Changed-Why:  
Fixed in -current and -stable. Thanks. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=30939 
>Unformatted:
