From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20454 invoked by alias); 13 Aug 2017 16:12:17 -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: X-Seq: 41535 Received: (qmail 19523 invoked by uid 1010); 13 Aug 2017 16:12:17 -0000 X-Qmail-Scanner-Diagnostics: from mail-wm0-f45.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(74.125.82.45):SA:0(-0.0/5.0):. Processed in 3.644399 secs); 13 Aug 2017 16:12:17 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_PASS, T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.1 X-Envelope-From: stephane.chazelas@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mail-followup-to:mime-version :content-disposition:user-agent; bh=M5LeXyDHCJDWebxSUmCxyva5hozA3TM31DEkZkguvTs=; b=CBgSFonv1K/p2eyPCu3NmvYEpjbdz8yjkEyV57400yT+jHI3Gg97mIZny7HityeE6f q8Q+MGRLhIDFVbntXVUA5pM/02cIdgT7l+yFcW+N8H8UZ0RE0SFRsojPEuqCgFtYuOGB i83MOv6CvktQdvqnIUkVaJlxbyQf6YigMnkyRYf2UwvXcn56vCj/5mnSFVgTIM3zaaaB JnotAcfc7zWxMgXyYHlUELtUHO0TbeIIqofzyUYiclNObujLw/kTun7EyQuEzFtSPqWj 5CbaRkIM+0HOBTxQpGDEmb9mdDznU7JPxBnztyAQQkTyQ9OYwOGaAgoPFkcrPHtItoxZ 5skg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mail-followup-to :mime-version:content-disposition:user-agent; bh=M5LeXyDHCJDWebxSUmCxyva5hozA3TM31DEkZkguvTs=; b=LyyuC/3B08yGLDdlQV7PcU02XShxy80GHV5kRqo+3w1ptvt2JVQowGrpUtwTmqlNhp eI/BqMQkh44FOeflFZpNpmmykIh23OVP7xdAZUeUpn/GrgHH0BRLVurN/Q4I8Rzrd68W +hIfmusW7917RmLHogfu3xfuyZOwJOi2GxSfDcGdBgtt0VtKHiLJM9DphysiluQaFPiD e4IKsXyKhwC1gxVW0dMziV5Vc/NsdUFJ6AUTy+eZTm/VnNcmoMIsrQFgBRx+5Z2f3/Cr nl7TubTGchekxSUOc65eCO4B5mzpx8hfTvVGeDYItgCwPXsUhWE10+sRaja1SAHFA7Z+ ugfg== X-Gm-Message-State: AHYfb5gZaZp95Krjs40LQe3LS+JldGWdIA1I5KC4jMTjaGlb6+jwuaNh CaasEOle3Qw62vie X-Received: by 10.28.203.78 with SMTP id b75mr2488767wmg.50.1502640728598; Sun, 13 Aug 2017 09:12:08 -0700 (PDT) Date: Sun, 13 Aug 2017 17:12:07 +0100 From: Stephane Chazelas To: Zsh hackers list Subject: fd used for saving redirected fds leaked to child processes Message-ID: <20170813161207.GA6530@chaz.gmail.com> Mail-Followup-To: Zsh hackers list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) In: mkfifo fifo zsh -c '{ echo GO > fifo & echo $!; } > pid; echo done' | cat "cat" hangs until some process open the fifo in read mode, even though that "echo GO > fifo" command was run in background and "done" is output straight away. What we see is the child zsh process opening the "fifo" having a fd open on the pipe to cat: zsh 28400 chazelas 12w FIFO 0,10 0t0 125690 pipe 28399,cat,0r I think that fd was the one that was dupped from stdout by its parent to /save/ stdout before doing the > pid redirection (so it can be used to restore stdout after the command group returns). That fd is not needed/used in the child so should be closed there. (here, the work around and more obvious way to do it is with zsh -c 'echo GO > fifo & echo $! > pid; echo done' | cat that was just an example to illustrate the problem). bash and dash seem to also have the problem. pdksh, ksh93 and yash are OK. Looking at strace outputs, they seem to be closing those fds in the children. The Bourne shell and rc are OK, but only because they don't use a dupped fd to "save" stdout when redirecting compound commands. They run the compound command in a subshell. Note that those fds have the CLOEXEC flag, so the circumstances in which it becomes a problem are few. -- Stephane