NetBSD Problem Report #55820

From www@netbsd.org  Mon Nov 23 18:36:24 2020
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 247D41A921F
	for <gnats-bugs@gnats.NetBSD.org>; Mon, 23 Nov 2020 18:36:24 +0000 (UTC)
Message-Id: <20201123183623.195201A923F@mollari.NetBSD.org>
Date: Mon, 23 Nov 2020 18:36:23 +0000 (UTC)
From: aprogrammer188@gmail.com
Reply-To: aprogrammer188@gmail.com
To: gnats-bugs@NetBSD.org
Subject: sh sources the ENV file for non interactive shells
X-Send-Pr-Version: www-1.0

>Number:         55820
>Category:       bin
>Synopsis:       sh sources the ENV file for non interactive shells
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Nov 23 18:40:00 +0000 2020
>Last-Modified:  Mon Nov 23 22:55:01 +0000 2020
>Originator:     Andreas Scheme
>Release:        9.1
>Organization:
>Environment:
NetBSD NetBSD.local 9.1 NetBSD 9.1 (GENERIC) #0: Sun Oct 18 19:24:30 UTC 2020  mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/amd64/compile/GENERIC amd64
>Description:
NetBSD sh and ksh have a unique behavior in respect of ENV compared to shells I've tested (several Bourne like shells on OpenBSD, FreeBSD, Linux, Solaris). More over, this behavior is not conforming to POSIX.

- the file designated by ENV is source for non-interactive shell, it shouldn't according to POSIX

- when executing an "exit" command in that file, the shell exit; other shells I've tested just stop processing the file and continue their normal execution. I don't know if POSIX specify this behavior. I've met it in no other shell.
>How-To-Repeat:
Here is an execution log

$ export ENV=$(pwd)/env
$ cat env
echo Not OK
exit
$ sh -c "echo OK"
Not OK
$ 

I'd have expected to get "OK" instead. Here is a second execution log

$ export ENV=$(pwd)/env
$ cat env
echo OK1
exit
echo Not OK
$ sh -ic "echo OK2"
OK1
$ 

I'd have expected to get 

OK1
OK2

instead.
>Fix:

>Audit-Trail:
From: Robert Elz <kre@munnari.OZ.AU>
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: bin/55820: sh sources the ENV file for non interactive shells
Date: Tue, 24 Nov 2020 05:53:11 +0700

     Date:        Mon, 23 Nov 2020 18:40:00 +0000 (UTC)
     From:        aprogrammer188@gmail.com
     Message-ID:  <20201123184000.CAAB11A9241@mollari.NetBSD.org>


   | - the file designated by ENV is source for non-interactive shell,
   | it shouldn't according to POSIX

 This has been discussed (at length) before, see PR standards/42828
 We are not going to change it.

 If you prefer the POSIX behaviour, start the shell with the posix
 option turned on (either by -o posix on the command line, or with
 POSIXLY_CORRECT set in the environment that the shell inherits on startup).

 The more common way to deal with this is with something like

 	case "$-" in
 	*i*)
 		# interactive only startup commands go here
 		;;
 	esac

 The commands can be just ". some-other-file" if you want to keep the $ENV
 file short so it doesn't slow processing too much.


   | - when executing an "exit" command in that file, the shell exit;

 That is what it is supposed to do.

   | other shells I've tested just stop processing the file and continue
   | their normal execution.

 Then they are broken (not posix conformant).

   | I don't know if POSIX specify this behavior.

 It does.   For ENV it says (the current version)...

    resulting value shall be used as a pathname of a file containing shell
    commands to execute in the current environment.

 In the forthcoming version, to deal with some other issues (mostly relating
 to aliases) the text has been revised (expanded), but still says

    parse the tokens as a program (see Section 2.10), and execute the
    resulting commands in the current environment.

 That is, it is quite certain that ENV processing does not create a new
 execution environment (which is good, as if it did, commands like cd and
 umask. etc., in it would not behave as expected).

 About exit it says:

    The exit utility shall cause the shell to exit from its current
    execution environment [...]

 (what follows is about the status, and there is a whole lot about EXIT traps).

 Since while processing ENV the execution environment is the parent shell
 environment, and since exit causes that to terminate, the shell simply
 (after EXIT trap processing) terminates.

 Since there is nothing in this PR which is going to result in any
 changes, I will close it in a day or so.

 kre

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.46 2020/01/03 16:35:01 leot Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2020 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.