From nobody Fri Mar 25 19:13:27 2022 X-Original-To: freebsd-hackers@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 1AC6A1A2D617 for ; Fri, 25 Mar 2022 19:13:32 +0000 (UTC) (envelope-from bdrewery@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2610:1c1:1:6074::16:84]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "freefall.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KQBZh09c7z4cwP; Fri, 25 Mar 2022 19:13:32 +0000 (UTC) (envelope-from bdrewery@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1648235612; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wG2KOv6btFH4zb5ApHeXMzZdYNiLVNRIGBizc1xSI84=; b=TOEF437MNTGar47yHsWS4PQB0yEcJTPRSEc24abWRqUOxp23YAFYu6OpJMDDMt8px46Lcy UO5ERyW5tmeDGEXW/ffuMnlIsRMYTs0WwImUu5k83o9Bq+r37XJam4H+FpuVpZRIV4b0Y3 ZtFXlzxfACSEFyo/hJ1oGBLlKXnYxF9MG6Q/KmdYgxwa4SMpUnLz22ttWq1vz+LqyhxBXV qi2ea23R7ercNdYdomPp9+/DSRDn9BKR5IutnqQ31LT5In4Q4n8/gNGH1AI4Pp8o6ubEvn RshnDaMFgU/+dSZ5QR9ESsmn9TZVbM2S+LuIBG3bV1FLocdfpUvhNA6GArLenA== Received: from mail.xzibition.com (unknown [127.0.1.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by freefall.freebsd.org (Postfix) with ESMTPS id C6A91106D7; Fri, 25 Mar 2022 19:13:31 +0000 (UTC) (envelope-from bdrewery@FreeBSD.org) Received: from mail.xzibition.com (localhost [172.31.3.2]) by mail.xzibition.com (Postfix) with ESMTP id CEA4D2D961; Fri, 25 Mar 2022 12:13:30 -0700 (PDT) X-Virus-Scanned: amavisd-new at mail.xzibition.com Received: from mail.xzibition.com ([172.31.3.2]) by mail.xzibition.com (mail.xzibition.com [172.31.3.2]) (amavisd-new, port 10026) with LMTP id vpJEHxAOEH4C; Fri, 25 Mar 2022 12:13:28 -0700 (PDT) Message-ID: <243ebc92-26a1-e5e7-67fe-1477ed6b5f7a@FreeBSD.org> DKIM-Filter: OpenDKIM Filter v2.10.3 mail.xzibition.com 0B1C02D959 Date: Fri, 25 Mar 2022 12:13:27 -0700 List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@freebsd.org MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Content-Language: en-US To: Ganael Laplanche , freebsd-hackers@freebsd.org References: <48e49ad0-a12a-d10b-5867-da9736c6c1fd@martymac.org> From: Bryan Drewery Organization: FreeBSD Subject: Re: Our /bin/sh and process group IDs In-Reply-To: <48e49ad0-a12a-d10b-5867-da9736c6c1fd@martymac.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1648235612; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wG2KOv6btFH4zb5ApHeXMzZdYNiLVNRIGBizc1xSI84=; b=RG3Gc51+u36a9kAT3xeObFSWLf53+P9eboA/kBpv8SvtCPe7X6ChAnWGXAt+nK9dorfgdJ qJpgcjgYNWBSnRtNijWImAzciFMjkMfz3xaPnp0Hz+pJFTAYiKB+DWdIHx/mTAAJl+zR5G kViOJ63sNMgm0LavfXzwGLCXSdzMA+mmms+GohdLcZWmIf156LBp7s2bVifQkRzT/PG5/i ScfG+cCKLEqWbTCz9tepACbfI35fOZyKi4GaMeSKWQi97I183OQ7WBt8jBHgCOoQCm2Znr /9Cmg+MHqyxVC+4IvfcvR6YtH7Z2I4cmvER3Cdq7DnuG8Miy40m2wYj+qGqhBw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1648235612; a=rsa-sha256; cv=none; b=gsuXrJb/Qnlhtv9rkyBrO2oBJvV4mSKJ0qBRJ6l9KrurWV0Yo29VIC6QdRyH+ORW3mL8oP gi8QV9rQNXJfpIM+Hq3iLQQ9KGafym9CA9ctJJ1zfQcmtCyT+JyPWsVuPif7rGWV69V0sz sTDqa8ayv5NcElBaFhmXuEykFDA1oVoJi2sGRedmxR0Blqmy7d+XYDeoacO431vUcX2t7U Crv5bLWuVNJyAgrWQHvEtb7VU0QxeaY6pCdSITgp1YmASCUdl9R1obHBhXb7QhpkxpDFtH ph4gs84WDZvUlp9JGovcKqilTOXHDd2fJDc/eTlQU1wGgIcyAc/BkLuyoxz2QQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N On 3/24/2022 10:13 AM, Ganael Laplanche wrote: > Hello, > > I am trying to fork a sub-shell with its own process group id through a > function that must itself be executed in the background. > > It should work with the following code: > > #---- > #!/bin/sh > > # Parent shell IDs > ps -o pid,ppid,pgid,comm -p $$ > > test_func () { >   set -m >   { /bin/sh -c 'sleep 1' ; } & >   # Forked shell IDs (pgid should be different from parent, >   # but it is not) >   ps -o pid,ppid,pgid,comm -p $! set -m needs to be set from the parent process, not child. In this example test_func is a child *and* the sleep is a child which is also very racy. Here's a pattern I use that works: #! /bin/sh ps -o pid,ppid,pgid,comm -p $$ test_func() { mypid=$(sh -c 'echo $PPID') ps -o pid,ppid,pgid,comm -p $mypid } spawn() { local - # cause -m to revert back on return set -m "$@" & } spawn test_func ps -o pid,ppid,pgid,comm -p $! > } > > # The following does not work: > test_func & > # ...but it works when function is not executed in the background: > #test_func > > sleep 2 > exit 0 > #---- > > Unfortunately, with our /bin/sh, the sleeping process gets the *same* > process group ID as its parent. > > I've tested several shell implementations; it works with : > > /usr/local/bin/bash --posix 'test.sh'     # from bash-5.1.16 > /usr/local/bin/zsh --emulate sh 'test.sh' # from zsh-5.8.1 > /usr/local/bin/ksh93 'test.sh'            # from ksh93-devel-2020.06.30 > /usr/local/bin/mksh 'test.sh'             # from mksh-59c > /usr/local/bin/ksh 'test.sh'              # from pdksh-5.2.14p2_6 > > but not with : > > /bin/sh 'test.sh'                         # on 13.0-RELEASE-p8 > /usr/local/bin/dash 'test.sh'             # from dash-0.5.11.5 > > am I missing something ? > > Any help welcome :) > > Best regards, > -- Bryan Drewery