Bug 30939 - top(1) behaves badly when it loses terminal
Summary: top(1) behaves badly when it loses terminal
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 4.3-RELEASE
Hardware: Any Any
: Normal Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2001-09-30 20:30 UTC by andrew
Modified: 2001-11-21 10:41 UTC (History)
0 users

See Also:


Attachments
file.diff (1.25 KB, patch)
2001-09-30 20:30 UTC, andrew
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description andrew 2001-09-30 20:30:00 UTC
After losing its control terminal top(1) eats all available CPU resources
(when running under root privileges).

Fix: Maybe it is better to just exit() instead of sleep() but anyway ...
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.
Comment 1 Giorgos Keramidas 2001-10-01 05:52:24 UTC
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
Comment 2 dwmalone 2001-10-29 16:57:03 UTC
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')
Comment 3 edwin 2001-10-29 21:45:15 UTC
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/
Comment 4 dwmalone freebsd_committer freebsd_triage 2001-11-21 10:41:25 UTC
State Changed
From-To: open->closed

Fixed in -current and -stable. Thanks.