Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15218

Re: leaks fd for internal functions but not external command

Path csiph.com!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail
From Chet Ramey <chet.ramey@case.edu>
Newsgroups gnu.bash.bug
Subject Re: leaks fd for internal functions but not external command
Date Tue, 23 Jul 2019 16:57:57 -0400
Lines 94
Approved bug-bash@gnu.org
Message-ID <mailman.2112.1563915484.2688.bug-bash@gnu.org> (permalink)
References <CAOj-5WDk=8kt=J8wO23giFVWRp5=_GbCNB2HQO87Upc4kkTg+g@mail.gmail.com> <5c34ecd5-c8b2-7000-46bc-1bbe3f71f163@case.edu> <CAOj-5WCFbUoU1x2aUvidT1opUbGeG2jFSaM__ch=SUAfzQbQ9w@mail.gmail.com> <78679ee9-fdaf-5180-d32f-81d92b936538@case.edu> <CAOj-5WBei5KMar7c967=jSwZRsW-Rz-0iL20TQMeuQoz61rfkw@mail.gmail.com> <CAOj-5WA_URW5b7a2EU+rR=T_wbTDEpOM_PSo9yNyU-Dqj+cWaA@mail.gmail.com> <f6acdcca-4c7c-b05c-004d-9b8ceab1e169@case.edu> <CAOj-5WA0b5Lv9GhXkK6gwoS1uVd2B1a0048tZ6sUNnhVHS+ixg@mail.gmail.com> <40109c28-c4a8-2ea0-cc77-e46a584dc011@case.edu> <CAOj-5WBX2h-nwO-u9ocWitmBQ-o=560dJFZw2xLeojNS5ZKs8Q@mail.gmail.com> <38bf161d-8170-69ba-77d0-638aa0852ad9@case.edu>
Reply-To chet.ramey@case.edu
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace usenet.stanford.edu 1563915484 28445 209.51.188.17 (23 Jul 2019 20:58:04 GMT)
X-Complaints-To action@cs.stanford.edu
Cc chet.ramey@case.edu, bug-bash@gnu.org, bash@packages.debian.org
To Sam Liddicott <sam@liddicott.com>
Envelope-to bug-bash@gnu.org
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=case.edu; s=smtp-primary; t=1563915478; bh=CT1UCOZhi1av7QnFJhc5mqU6VWLVzNjjspabuuxgWTw=; h=Reply-To:Cc:Subject:To:References:From:Message-ID:Date: MIME-Version:In-Reply-To:Content-Type:Content-Transfer-Encoding; b=do+sXdolaMiZFbX5HlY4/hiTO51uFBI0STS61INOX5ng690zKpoTkoM21lK3kUSsd0 Rl5OUFVoI1hREIppjK45tzi6M8Ra0arb8j/96l7lZNk4VksRO2YeS+SMBPzvfkoaslu +0HK2ecCa3+3ua+cHG+HQGTV1xjNYoVJByCgS45TF/qY7zvVSPSgQnsiRogLGRfMZDr U+G6Ljvp9SMlqKdqPQRqYxNMLKDGCmwQ+L6T1Q2LPbT8LWPtWNQtop7MLgAWHu9RMSp /Mc/48/q/plPFVK5kq81S3Lc5/KSCJxJpCDd0/zHj/3eDaa7bk2xDBPQlhBjSDYmGoh 4Gsy3o+w==
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=case.edu; s=smtp-primary; t=1563915477; bh=suinn9GC982n0jdwpp5EoOjQ1XMbSR2CbPJWMYg844c=; h=Reply-To:Cc:Subject:To:References:From:Message-ID:Date: MIME-Version:In-Reply-To:Content-Type:Content-Transfer-Encoding; b=O7GsZAFECacMxwOXjtAMqie6im9ntEKt5JwJB9/VtW6gQKTFHynGh8qjRtaWriGGv8 VL1Ffep95yyoMa8sLac3oo5qvbBKfBnaFFz4yFTn4pmD8HXfD2hfOJOVw7WCPFom1Em aKfrP2+86bsqB5r1OGkd4NTLoeEo5Itg6Zuku8C3w6zh4S4roImpujbv04THIZUr6tO NG0Fsn92SiBV1dlkcMLVVLwjB8MF8JwugwYRCcRyjvTLP0tBQKThxXrz5bGytalTvTC ESyCnuZRV5uImV38GRzEnSUYwwxareImQoX9qbQL2b8HCWak81b0wgFZPKiSoQ/QSDY wKhl13XA==
Openpgp preference=signencrypt
Autocrypt addr=chet.ramey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2yHmHWT6vEjw9Ry/hF6Pc0oh1a62USdf aKAiim0nVxxQmPmiRvtCmcJJBBgRAgAJBQJBDrBvAhsMAAoJELtYafBk6nSr43AAn2ZZFQg8 Gs/zUzvXMt7evaFqVTzcAJ0cHtKpP1i/4H4R9+OsYeQdxxWxTQ==
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.8.0
In-Reply-To <CAOj-5WBX2h-nwO-u9ocWitmBQ-o=560dJFZw2xLeojNS5ZKs8Q@mail.gmail.com>
Content-Language en-US
X-Junkmail-Status score=8/90, host=mpv2-2015.case.edu
X-Junkmail-PrAS-Raw score=8/90, refid=2.7.2:2019.7.23.201216:17:8.317, ip=, rules=DKIM_SIGNATURE, __HAS_REPLYTO, __HAS_CC_HDR, __MULTIPLE_RCPTS_CC_X2, __SUBJ_REPLY, __BOUNCE_CHALLENGE_SUBJ, __BOUNCE_NDR_SUBJ_EXEMPT, __SUBJ_ALPHA_END, __TO_MALFORMED_2, __TO_NAME, __TO_NAME_DIFF_FROM_ACC, __HAS_REFERENCES, __REFERENCES, __HAS_FROM, FROM_EDU_TLD, __HAS_MSGID, __SANE_MSGID, DATE_TZ_NA, __USER_AGENT, __MOZILLA_USER_AGENT, __MIME_VERSION, __IN_REP_TO, __CT, __CT_TEXT_PLAIN, __CTE, __REPLYTO_SAMEAS_FROM_ADDY, __REPLYTO_SAMEAS_FROM_ACC, __FROM_DOMAIN_IN_ANY_CC2, __REPLYTO_SAMEAS_FROM_DOMAIN, __DKIM_ALIGNS_1, __DKIM_ALIGNS_2, __ANY_URI, __URI_WITH_PATH, __URI_NO_WWW, __CP_NAME_BODY, __HIGHBITS, __CP_URI_IN_BODY, __FRAUD_MONEY_CURRENCY_DOLLAR, __SUBJ_ALPHA_NEGATE, __URI_IN_BODY, __URI_NOT_IMG, __FORWARDED_MSG, __BODY_NO_MAILTO, __NO_HTML_TAG_RAW, BODY_SIZE_4000_4999, __MIME_TEXT_P1, __MIME_TEXT_ONLY, __URI_NS, HTML_00_01, HTML_00_10, [TRUNCATED], so=2010-03-03 19:42:08, dmn=2016-08-03-0138
X-detected-operating-system by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic]
X-Received-From 129.22.103.227
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.23
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <https://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
X-Mailman-Original-Message-ID <38bf161d-8170-69ba-77d0-638aa0852ad9@case.edu>
X-Mailman-Original-References <CAOj-5WDk=8kt=J8wO23giFVWRp5=_GbCNB2HQO87Upc4kkTg+g@mail.gmail.com> <5c34ecd5-c8b2-7000-46bc-1bbe3f71f163@case.edu> <CAOj-5WCFbUoU1x2aUvidT1opUbGeG2jFSaM__ch=SUAfzQbQ9w@mail.gmail.com> <78679ee9-fdaf-5180-d32f-81d92b936538@case.edu> <CAOj-5WBei5KMar7c967=jSwZRsW-Rz-0iL20TQMeuQoz61rfkw@mail.gmail.com> <CAOj-5WA_URW5b7a2EU+rR=T_wbTDEpOM_PSo9yNyU-Dqj+cWaA@mail.gmail.com> <f6acdcca-4c7c-b05c-004d-9b8ceab1e169@case.edu> <CAOj-5WA0b5Lv9GhXkK6gwoS1uVd2B1a0048tZ6sUNnhVHS+ixg@mail.gmail.com> <40109c28-c4a8-2ea0-cc77-e46a584dc011@case.edu> <CAOj-5WBX2h-nwO-u9ocWitmBQ-o=560dJFZw2xLeojNS5ZKs8Q@mail.gmail.com>
Xref csiph.com gnu.bash.bug:15218

Show key headers only | View raw


On 7/23/19 12:12 PM, Sam Liddicott wrote:

> Given what you have explained as intentional, it the difference between 1
> and 2, but it is best understood as a 4-way difference, outlined here:
> 
> 1. {var} internal: fd remains open in parent
> 2. {var} external: fd closed in parent
> 3. numeric internal: fd closed in parent
> 4. numeric external: fd closed in parent
> 
> 1. {var} internal: fd remains open in parent
> bash -c 'echo 1 {_}>&2 2>&1 1>&${_} {_}<&- ; 
>         echo done ; lsof -p $$ | grep CHR'
> 
> 2. {var} external: fd closed in parent
> bash -c '/bin/echo 1 {_}>&2 2>&1 1>&${_} {_}<&- ; 
>         echo done ; lsof -p $$ | grep CHR'
> 
> 3. numeric internal: fd closed in parent
> bash -c 'echo 1 10>&2 2>&1 1>&10 10<&- ; 
>         echo done ; lsof -p $$ | grep CHR'
> 
> 4. numeric external: fd closed in parent
> bash -c '/bin/echo 1 10>&2 2>&1 1>&10 10<&- ; 
>         echo done ; lsof -p $$ | grep CHR'
> 
> You've indicated that {var} syntax leaves me an fd to do with what I wish.

Yes. The question is whether the {_}<&- should close the file descriptor
`permanently' or whether that close action should be undone because you're
not using `exec' and the file descriptor was created using the variable
assignment syntax. The latter is what bash does now.

> You've also explained what bash is doing that makes this untrue if the
> command was an external command.

Because you never open the file descriptor in the parent shell:
redirections happen in the child. The child process in case 2 (and 4)
can do what it wants with the file descriptor, and its view of the
file descriptor is the same as case 1 (and 3).

> I don't believe that this behaviour is *intended( to depend on the
> non-obvious detail of whether or not the command is external.

Why? There are plenty of things that depend on whether or not a command is
builtin, or whether it's run in the parent shell.

> 
> Given the coding pattern of wrapping external commands with functions that
> re-invoke using bash "command"; this can lead to unpredictable behaviours
> when such wrappers are active.
> 
> e.g. openssl() { LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/extra" *command*
> openssl "$@"; }
> 
> If that function is defined, I get a handle leak*, if it isn't and main
> openssl is called, I don't -- or from the other of view my handle got
> closed without my knowledge, so I can't use it as I wish.
> 
> Personally I would wish that "{var} internal" would also close the fd as it
> does for numeric fd and for external {var} fd, because if I really wanted
> to open an fd and have it hang around I would do a naked: exec {xxx}>&2 ;
> type of thing.

Sure. But there's not as good a reason to have that syntax otherwise. It's
just syntactic sugar, a way for historical shells to use file descriptors
greater than 9.

> I also think that based on your description of what bash is doing, it might
> be easier to fix by also closing in the parent, as I describe.
> 
> It would bring full consistency and avoid hard to detect and hard to
> code-around bugs.
> 
> Ultimately, unless {var} external is intended to behave different to {var}
> internal, then we have a consistency bug.

The internal/external difference doesn't really have anything to do with
the semantics of file descriptors or redirections per se; only that
redirections are performed in the child. There are multiple variations in
behavior depending on whether a command is executed in a subshell, and this
is one of them.

Ultimately, the difference is between cases 1 and 3 and retaining a handle
to the file descriptor instead of closing it when the command finishes.
Everything else is identical between the two commands. That was a design
choice. I don't see changing it now.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: leaks fd for internal functions but not external command Chet Ramey <chet.ramey@case.edu> - 2019-07-23 16:57 -0400

csiph-web