Bug 30581

Summary: top(1) races when it loses its terminal
Product: Base System Reporter: Peter Avalos <peter>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   

Description Peter Avalos 2001-09-15 01:40:00 UTC
	top(1) uses up all the cpu it can when it loses its terminal.

Fix: 

I'm not sure. It looks like there's some read(2)'s that don't
get their return values checked throughout the code, but I'm not sure
if that will fix it.
How-To-Repeat: 	Disconnect from an ssh session while running top as root. I can't
get this to happen when run as a normal user.

	Everything working normally (terminal connected):
root     23936  1.5  0.2  2096 1312  p8  S+    5:22PM   0:00.21 top

	Now disconnect from the ssh session:
root     23936 94.8  0.2  2096 1312  p8- R     5:22PM   3:12.76 top
Comment 1 edwin 2001-10-12 07:21:40 UTC
A fix for this is this. I've forward this to the author of top
also, can please somebody in the mean time submit this to the
FreeBSD source?

--- /usr/src/contrib/top/top.c.old	Fri Oct 12 16:13:52 2001
+++ /usr/src/contrib/top/top.c	Fri Oct 12 16:16:44 2001
@@ -721,7 +721,11 @@
 
 		    /* 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)==0) {
+			/* this happens if the controlling terminal */
+			/* has disappeared. Maybe due to network problems? */
+			quit(0);
+		    }
 		    if ((iptr = strchr(command_chars, ch)) == NULL)
 		    {
 			if (ch != '\r' && ch != '\n')

-- 
Edwin Groothuis   |              Personal website: http://www.MavEtJu.org
edwin@mavetju.org |           Interested in MUDs? Visit Fatal Dimensions:
------------------+                       http://www.FatalDimensions.org/
Comment 2 dwmalone freebsd_committer freebsd_triage 2001-11-04 21:08:21 UTC
State Changed
From-To: open->closed

I'm abou to commit the patch in PR 30939, which is similar to 
Edwin's patch in this PR.