From jilles@stack.nl  Tue May 20 10:46:18 2003
Return-Path: <jilles@stack.nl>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 8D9D037B401
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 20 May 2003 10:46:18 -0700 (PDT)
Received: from mailhost.stack.nl (vaak.stack.nl [131.155.140.140])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 845A243F93
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 20 May 2003 10:46:17 -0700 (PDT)
	(envelope-from jilles@stack.nl)
Received: by mailhost.stack.nl (Postfix, from userid 65534)
	id 591CC1F00A; Tue, 20 May 2003 19:46:16 +0200 (CEST)
Received: from turtle.stack.nl (turtle.stack.nl [2001:610:1108:5010::132])
	by mailhost.stack.nl (Postfix) with ESMTP id 5E60E1F006
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 20 May 2003 19:46:11 +0200 (CEST)
Received: by turtle.stack.nl (Postfix, from userid 1677)
	id 4C53E1CC5D; Tue, 20 May 2003 19:46:11 +0200 (CEST)
Message-Id: <20030520174611.4C53E1CC5D@turtle.stack.nl>
Date: Tue, 20 May 2003 19:46:11 +0200 (CEST)
From: Jilles Tjoelker <jilles@stack.nl>
Reply-To: Jilles Tjoelker <jilles@stack.nl>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: ^T'ing a zombie causes 'calcru: negative time' messages
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         52490
>Category:       kern
>Synopsis:       ^T'ing a zombie causes 'calcru: negative time' messages
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue May 20 10:50:08 PDT 2003
>Closed-Date:    Tue Jun 22 05:58:48 GMT 2004
>Last-Modified:  Tue Jun 22 05:58:48 GMT 2004
>Originator:     Jilles Tjoelker
>Release:        FreeBSD 5.1-BETA i386
>Organization:
>Environment:
System: FreeBSD turtle.stack.nl 5.1-BETA FreeBSD 5.1-BETA #10: Mon May 12 15:30:54 CEST 2003 jilles@turtle.stack.nl:/usr/obj/usr/src/sys/TURTLE i386

It occurs on 4.8-RELEASE and many older 4.x versions as well.
>Description:
When ^T'ing (ttyinfo) a zombie, calcru() is called and logs a 'negative time'
message. To achieve this, only zombies must be in the foreground process group
of the terminal.
>How-To-Repeat:
Compile and run the negtime program. Pressing ^T after it has printed the
message 'done' causes a log message 'calcru: negative time...'. (It may be
necessary to do stty status ^t beforehand.)
>Fix:
A patch (against 4-STABLE) is attached which prevents calcru() from being
called on zombies from ttyinfo(). I don't know in what process states calcru()
may be called exactly, but for the rest of /sys/kern it's only used for
accounting, exiting and getrusage() and therefore cannot be called on zombies.
(Perhaps this is or should be documented somewhere.)

--- negtime.c begins here ---
/* Copyright (C) 2001 by Jilles Tjoelker */

#include	<sys/types.h>
#include	<stdio.h>
#include	<signal.h>
#include	<unistd.h>

int
main(int argc, char *argv[])
{
    int pid,pgid;

    if (argc != 1)
    {
	fprintf(stderr, "Usage: %s\n", argv[0]);
	return(1);
    }

    switch (pid=fork())
    {
	case 0:
	    signal(SIGTTOU,SIG_IGN);
	    pid=getpid();
	    pgid=getpgrp();
	    if (setpgid(pid,pid)==-1)
	    {
		perror("Error in setpgid()");
		_exit(1);
	    }
	    if (tcsetpgrp(0,pid)==-1)
	    {
		setpgid(pid,pgid);
		perror("Error in tcsetpgrp()");
		_exit(2);
	    }
	    sleep(1);
	    printf("done\n");
	    _exit(127);
	    break;

	case -1:
	    fprintf(stderr, "Error in fork().\n");
	    break;

	default:
	    sleep(10);
    }
    return 0;
}

/* vim:ts=8:cin:sw=4:kp=man\ -S3\:2\:9\:1\:4\:5\:6\:7\:8\:n
    */
--- negtime.c ends here ---

--- ttyinfo_zombie.patch begins here ---
--- /usr/src/sys/kern/tty.c.orig	Mon Mar 11 02:32:31 2002
+++ /usr/src/sys/kern/tty.c	Sun Jan 19 21:40:16 2003
@@ -2323,7 +2323,7 @@
 		    pick->p_stat == SRUN ? "running" :
 		    pick->p_wmesg ? pick->p_wmesg : "iowait");
 
-		if (pick->p_flag & P_INMEM) {
+		if (pick->p_flag & P_INMEM && pick->p_stat != SZOMB) {
 			calcru(pick, &utime, &stime, NULL);
 
 			/* Print user time. */
--- ttyinfo_zombie.patch ends here ---


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: bde 
State-Changed-When: Tue Jun 22 05:56:54 GMT 2004 
State-Changed-Why:  
Fixed in a different way in rev.1.237 of kern_exit.c in -current.  ^T on 
zombies now gives correct results in -current. 

Worked around in a different way in 1.55.2.7 of kern_resource.c in 
RELENG_4.  ^T on zombies now gives slightly out of date results in RELENG_4. 

The test program in the PR was very useful for investigating the bug. 

ps and friends aren't affected by the main bug, but they display 
completely wrong times for zombies (always 0).  Resource usage is the 
main thing that is not discarded when a process exits, so it should 
be visible for zombies too. 


http://www.freebsd.org/cgi/query-pr.cgi?pr=52490 
>Unformatted:
