From owner-freebsd-bugs@FreeBSD.ORG Fri Nov 26 08:10:19 2004 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 908F516A4CE for ; Fri, 26 Nov 2004 08:10:19 +0000 (GMT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 5C8AB43D60 for ; Fri, 26 Nov 2004 08:10:19 +0000 (GMT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.13.1/8.13.1) with ESMTP id iAQ8AJKt051660 for ; Fri, 26 Nov 2004 08:10:19 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.13.1/8.13.1/Submit) id iAQ8AJv9051659; Fri, 26 Nov 2004 08:10:19 GMT (envelope-from gnats) Resent-Date: Fri, 26 Nov 2004 08:10:19 GMT Resent-Message-Id: <200411260810.iAQ8AJv9051659@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Mike Silbersack Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E6DD416A4CE for ; Fri, 26 Nov 2004 08:05:51 +0000 (GMT) Received: from patrocles.silby.com (adsl-68-249-2-11.dsl.milwwi.ameritech.net [68.249.2.11]) by mx1.FreeBSD.org (Postfix) with ESMTP id 5631A43D46 for ; Fri, 26 Nov 2004 08:05:51 +0000 (GMT) (envelope-from silby@patrocles.silby.com) Received: from patrocles.silby.com (localhost [127.0.0.1]) by patrocles.silby.com (8.13.1/8.13.1) with ESMTP id iAQ85vp7004920 for ; Fri, 26 Nov 2004 02:05:57 -0600 (CST) (envelope-from silby@patrocles.silby.com) Received: (from silby@localhost) by patrocles.silby.com (8.13.1/8.13.1/Submit) id iAQ85uFk004919; Fri, 26 Nov 2004 02:05:56 -0600 (CST) (envelope-from silby) Message-Id: <200411260805.iAQ85uFk004919@patrocles.silby.com> Date: Fri, 26 Nov 2004 02:05:56 -0600 (CST) From: Mike Silbersack To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.113 Subject: bin/74404: sh does not handle signals to subshells properly and/or $! is broken X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: Mike Silbersack List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Nov 2004 08:10:19 -0000 >Number: 74404 >Category: bin >Synopsis: sh does not handle signals to subshells properly and/or $! is broken >Confidential: no >Severity: non-critical >Priority: medium >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Fri Nov 26 08:10:18 GMT 2004 >Closed-Date: >Last-Modified: >Originator: Mike Silbersack >Release: FreeBSD 6.0-CURRENT i386 >Organization: >Environment: System: FreeBSD patrocles.silby.com 6.0-CURRENT FreeBSD 6.0-CURRENT #2: Thu Nov 25 01:13:18 CST 2004 silby@patrocles.silby.com:/usr/obj/usr/src/sys/PATROCLES i386 Description: /bin/sh in the base system has an oddity in how it handles signals to subshells. If you run the supplied script (known as badsh.sh), you will find that the subshell will not be terminated by the script, and the "You didn't get me!" line will be printed. Under bash, the subshell is killed as expected. The reason for this issue appears to be due to a few things: 1. sh spawns forks TWO copies of itself when creating the subshell. 2. The first subshell's PID is returned by $!; this subshell then forks again and creates the second subshell, which actually does the sleep and echo. 3. The first subshell does not pass on signals to the second subshell. As a result, the kill signal is never delivered to the second subshell, and it is not terminated. This is frustrating behavior because aside from kill $! + 1 (which would be a gross hack), the only way to kill the subshell would be to use ps | grep something or other > kill. So, the best fix would be for the 1st subshell to simply pass on the kill signal to the second subshell before it dies. This behavior happens on 6.0 and 4.10; I have not tested other OS versions. Since bash handles it in what appears to be the correct fashion, this seems to be a bug in sh. How-To-Repeat: This test program will help you see the issue: #!/bin/sh echo $$ ( sleep 10 echo "You didn't get me!" exit 1 )& echo $! kill $! exit 1 Immediately after running it, use ps and see how many /bin/sh processes are running. Comment out the kill line and compare. Fix: None at this time... using bash instead could be an alternative. >Description: >How-To-Repeat: >Fix: >Release-Note: >Audit-Trail: >Unformatted: