From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 500c5065 for ; Tue, 2 Jul 2019 09:00:29 +0000 (UTC) Received: (qmail 25467 invoked by alias); 2 Jul 2019 09:00:19 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 44480 Received: (qmail 11670 invoked by uid 1010); 2 Jul 2019 09:00:19 -0000 X-Qmail-Scanner-Diagnostics: from mailout1.w1.samsung.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.101.2/25496. spamassassin: 3.4.2. Clear:RC:0(210.118.77.11):SA:0(-7.0/5.0):. Processed in 4.503859 secs); 02 Jul 2019 09:00:19 -0000 X-Envelope-From: p.stephenson@samsung.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.samsung.com designates 210.118.77.11 as permitted sender) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.w1.samsung.com 20190702085939euoutp017304154d5ec984a7897e47555e2a4eb6~ti3TFHFP81568415684euoutp01O DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1562057979; bh=IG9tkLa5d0mjr946Ci+JWSlEX4hg0z/zdAdGOLI4gjU=; h=Subject:From:To:Date:In-Reply-To:References:From; b=PHTxgXyAhnKHckJssR0TJ4s6iJC6stmO3U9/4p1Hd5sZ1tYHgQ3gwZ+YyWuqkNl4n oZjKB1LH6hbiCAuwLN/23DjS+XCuWTMm9nD26Rxyt6AtLxaPzDKLv9Gc0lm1qQtfnC DgrpCrp4Ji7h3+EaMe5Cj3g47kNt/yXWrZuNSxeI= X-AuditID: cbfec7f4-113ff70000001119-9f-5d1b1cfa4cf2 Message-ID: <1562057976.5684.3.camel@samsung.com> Subject: Re: <(...), >(...) and fds above 9 From: Peter Stephenson To: Zsh Hackers' List Date: Tue, 2 Jul 2019 09:59:36 +0100 In-Reply-To: <1561999973.6006.21.camel@samsung.com> X-Mailer: Evolution 3.18.5.2-0ubuntu3.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrDIsWRmVeSWpSXmKPExsWy7djPc7q/ZKRjDV680rQ42PyQyYHRY9XB D0wBjFFcNimpOZllqUX6dglcGbd7OtgLbupV7Fs4ia2BcZZ6FyMnh4SAicSiFZOZQWwhgRWM Ek8mWHUxcgHZfUwSBw9sZoVI9DJJ3HprB9Nw4NxlZoii5YwSByfvYoNwgIrmnbrADtFxmlFi 2jQziMR5Ron7PavBErwChhLXrjwB2ycsoC1x+9oyRhCbDSg+ddNsIJuDQwQo3v5RDCTMIqAi sX/ZRyYQm1PAWOLwk4lMEFdoSGy4eYwJYqSgxMmZT1hAbGYBeYnmrbPBrpMQeMwmseXhEUaI BheJK/vvQjULS7w6voUdwpaR+L9zPhNEQzujxJpJr9khnB5GiU1H70B1W0v03b4Idh2zgKbE +l36EGFHie0tx5hBwhICfBI33gpCHMEnMWnbdKgwr0RHmxBEtZrEjqatjBBhGYmnaxQmMCrN QvLBLCQfzEJYtYCReRWjeGppcW56arFRXmq5XnFibnFpXrpecn7uJkZgGjj97/iXHYy7/iQd YhTgYFTi4b3BJRUrxJpYVlyZe4hRgoNZSYR3/wrJWCHelMTKqtSi/Pii0pzU4kOM0hwsSuK8 1QwPooUE0hNLUrNTUwtSi2CyTBycUg2M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081195b2f0d133f344b7 X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20190701100058epcas2p25e5f8dbd14d048fe2be1d831f3cf60ab X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20190701100058epcas2p25e5f8dbd14d048fe2be1d831f3cf60ab References: <20190701100001.hbegs7zyu2auckhf@chaz.gmail.com> <1561975733.6006.2.camel@samsung.com> <1561994908.6006.19.camel@samsung.com> <20190701162202.5o3cxahc75e2hucz@chaz.gmail.com> <1561999973.6006.21.camel@samsung.com> On Mon, 2019-07-01 at 17:52 +0100, Peter Stephenson wrote: > {fd}-opened file descriptors are marked FDT_EXTERNAL.  We could default > to leaving those open on fork and document this.  This would then > apply to file descriptors created with zsocket, which I presume you > could think of as being in the same category. >  > sysopen file descriptors are marked FDT_INTERNAL, which we'd certainly > expect to close.  It looks like we make an exception if you use an > explicit file descriptor number rather than ask for one to be assigned > to a variable, I think because that's constrained to be 0 to 9.  So these > could be changed to FDT_EXTERNAL, too, which would probably be more > consistent. >  > As I said, this is just based on needs and documentation rather than any > definite logic. >  > Probably =(...) should use closem() consisent with <(...), too. This fixes this up --- I think for file descriptors known to and hence managed by the user the behaviour of silently closing is probably uncontroversially unhelpful.  Historically, it's really just there to ensure the shell doesn't leak its own bilge, so shouldn't really be visible to the user as an effect.  I've explicitly noted the file descriptors aren't closed (except with close-on-exec, obviously). One thing I haven't changed is module-owned file descriptors --- FDT_MODULE.  So currently, for example, file descriptors owned by zsh/net/tcp are closed when exec'ing or doing substitutions involving forks.  The rationale for these file descriptors is a little different --- they're visible to the user, but they're managed by the module rather than closed by the user.  So not sure whether to change this. If we don't it should at least be documented. pws diff --git a/Doc/Zsh/mod_socket.yo b/Doc/Zsh/mod_socket.yo index 867f6081f..78d9254e8 100644 --- a/Doc/Zsh/mod_socket.yo +++ b/Doc/Zsh/mod_socket.yo @@ -43,7 +43,8 @@ startitem()  item(tt(zsocket) tt(-l) [ tt(-v) ] [ tt(-d) var(fd) ] var(filename))(  tt(zsocket -l) will open a socket listening on var(filename).  The shell parameter tt(REPLY) will be set to the file descriptor -associated with that listener. +associated with that listener.  The file descriptor remains open in subshells +and forked external executables.    If tt(-d) is specified, its argument  will be taken as the target file descriptor for @@ -56,7 +57,8 @@ tt(zsocket -a) will accept an incoming connection  to the socket associated with var(listenfd).  The shell parameter tt(REPLY) will  be set to the file descriptor associated with -the inbound connection. +the inbound connection.  The file descriptor remains open in subshells +and forked external executables.    If tt(-d) is specified, its argument  will be taken as the target file descriptor for the diff --git a/Doc/Zsh/mod_system.yo b/Doc/Zsh/mod_system.yo index 3a85e760f..6292af071 100644 --- a/Doc/Zsh/mod_system.yo +++ b/Doc/Zsh/mod_system.yo @@ -45,7 +45,9 @@ specified as a comma-separated list. The following is a list of possible  options. Note that, depending on the system, some may not be available.  startitem()  item(tt(cloexec))( -mark file to be closed when other programs are executed +mark file to be closed when other programs are executed (else +the file descriptor remains open in subshells and forked external +executables)  )  xitem(tt(create))  item(tt(creat))( diff --git a/Doc/Zsh/redirect.yo b/Doc/Zsh/redirect.yo index 7e38cd0c3..13496d8d3 100644 --- a/Doc/Zsh/redirect.yo +++ b/Doc/Zsh/redirect.yo @@ -182,7 +182,8 @@ indent(... tt({myfd}>&1))  This opens a new file descriptor that is a duplicate of file descriptor  1 and sets the parameter tt(myfd) to the number of the file descriptor,  which will be at least 10.  The new file descriptor can be written to using -the syntax tt(>&$myfd). +the syntax tt(>&$myfd).  The file descriptor remains open in subshells +and forked external executables.    The syntax tt({)var(varid)tt(}>&-), for example tt({myfd}>&-), may be used  to close a file descriptor opened in this fashion.  Note that the diff --git a/Src/Modules/system.c b/Src/Modules/system.c index 7a4f4ee13..50de59cf9 100644 --- a/Src/Modules/system.c +++ b/Src/Modules/system.c @@ -316,7 +316,7 @@ bin_sysopen(char *nam, char **args, Options ops, UNUSED(int func))      int o, fd, moved_fd, explicit = -1;      mode_t perms = 0666;  #if defined(FD_CLOEXEC) && !defined(O_CLOEXEC) -    int fdflags; +    int fdflags = 0;  #endif        if (!OPT_ISSET(ops, 'u')) { @@ -396,8 +396,8 @@ bin_sysopen(char *nam, char **args, Options ops, UNUSED(int func))  #endif /* O_CLOEXEC */   fcntl(moved_fd, F_SETFD, FD_CLOEXEC);  #endif /* FD_CLOEXEC */ +    fdtable[moved_fd] = FDT_EXTERNAL;      if (explicit == -1) { - fdtable[moved_fd] = FDT_EXTERNAL;   setiparam(fdvar, moved_fd);   /* if setting the variable failed, close moved_fd to avoid leak */   if (errflag) diff --git a/Src/exec.c b/Src/exec.c index 60ab0acf8..2acb2c0bc 100644 --- a/Src/exec.c +++ b/Src/exec.c @@ -4407,8 +4407,10 @@ closem(int how, int all)       /*        * Process substitution needs to be visible to user;        * fd's are explicitly cleaned up by filelist handling. +      * External FDs are managed directly by the user.        */ -     (all || fdtable[i] != FDT_PROC_SUBST) && +     (all || (fdtable[i] != FDT_PROC_SUBST && +      fdtable[i] != FDT_EXTERNAL)) &&       (how == FDT_UNUSED || (fdtable[i] & FDT_TYPE_MASK) == how)) {       if (i == SHTTY)   SHTTY = -1; @@ -4823,6 +4825,7 @@ getoutputfile(char *cmd, char **eptr)      }        /* pid == 0 */ +    closem(FDT_UNUSED, 0);      redup(fd, 1);      entersubsh(ESUB_PGRP|ESUB_NOMONITOR, NULL);      cmdpush(CS_CMDSUBST);