From nobody@FreeBSD.org  Sun Feb 17 13:11:40 2008
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 15C1616A41A
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 17 Feb 2008 13:11:40 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 0697913C45E
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 17 Feb 2008 13:11:40 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.2/8.14.2) with ESMTP id m1HD9Egx008652
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 17 Feb 2008 13:09:14 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.2/8.14.1/Submit) id m1HD9EXK008651;
	Sun, 17 Feb 2008 13:09:14 GMT
	(envelope-from nobody)
Message-Id: <200802171309.m1HD9EXK008651@www.freebsd.org>
Date: Sun, 17 Feb 2008 13:09:14 GMT
From: Piotr Koper <piotr.koper@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Zombie jails (jailed child process exits while non-jailed parent is alive)
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         120753
>Category:       kern
>Synopsis:       [jail] Zombie jails (jailed child process exits while non-jailed parent is alive)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bz
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Feb 17 13:20:02 UTC 2008
>Closed-Date:    Sat Nov 29 17:37:30 UTC 2008
>Last-Modified:  Sat Nov 29 17:37:30 UTC 2008
>Originator:     Piotr Koper
>Release:        6.2-RELEASE, 6.3-RELEASE
>Organization:
>Environment:
FreeBSD node01 6.3-RELEASE FreeBSD 6.3-RELEASE #7: Sun Feb  3 03:56:47 CET 2008     root@node01:/usr/obj/usr/src/sys/NODE  i386

>Description:
1. fork ()
2. child: jail (...)
3. child: execv (...)
4. parent: sleep (...)

So, when parent sleeps, child has already died, but:

$ ps auxw | grep J
root          21566  0.0  0.0     0     0  p2  ZJ    1:53PM   0:00.00 <defunct>
$ jls
   JID  IP Address      Hostname                      Path
     7  127.0.0.1       jail-fork                     /
$

When the parent exits, the jail disappears.

$ jls
$

But when you double fork the jail disappears as soon as the child process dies:
1. fork ()
2. child: fork ()
3. child: parent: exit ()
4. child: child: jail (...)
5. child: child: execv (...)
6. parent: sleep (10)

$ jls
$

See "How to repeat".
>How-To-Repeat:
Compile the code bellow and try two scenario:

1. $ ./jail-fork & sleep 1 ; jls ; ps auxw | grep J
2. $ ./jail-fork double-fork & sleep 1 ; jls ; ps auxw | grep J

For 1: you sees:
root          24113  0.0  0.0     0     0  p2  ZJ    2:06PM   0:00.00 <defunct>

For 2: everything is ok - no zombie process

<!-- file:jail-fork.c --!>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/jail.h>
#include <unistd.h>

int
main (int argc, char *argv[])
{
        struct in_addr in;
        struct jail j;
        char *j_argv[] = { "/bin/ls", "/COPYRIGHT" };

        j.version = 0;
        j.path = "/";
        j.hostname = "jail-fork";

        if (inet_aton ("127.0.0.1", &in) == 0)
                perror ("inet_addr");

        j.ip_number = ntohl (in.s_addr);

        /* Just fork, we'd like to create a background jailed process. */
        if (fork () == 0)
        {
                /* Fork again if additional arguments supplied */
                if (argc > 1 && fork () > 0)
                                exit (0);

                if (jail (&j) < 0)
                        perror ("jail");

                execv (j_argv[0], j_argv);
                perror ("execv");
        }

        sleep (3);

        return 0;
}

>Fix:


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->freebsd-jail 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Sun Feb 17 16:05:40 UTC 2008 
Responsible-Changed-Why:  
Over to maintainer(s). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=120753 
State-Changed-From-To: open->closed 
State-Changed-By: bz 
State-Changed-When: Sat Nov 29 17:33:15 UTC 2008 
State-Changed-Why:  
This is not a bug. This is the way processes are tracked. 
Note that in your second exmaple your parent exists before 
your jail call but after your fork, so there is no parent 
to "collect" the dead child later so the jail can go away 
instantly. 


Responsible-Changed-From-To: freebsd-jail->bz 
Responsible-Changed-By: bz 
Responsible-Changed-When: Sat Nov 29 17:33:15 UTC 2008 
Responsible-Changed-Why:  
Assign to me in case of possible follow-ups. 

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