zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: fd used for saving redirected fds leaked to child processes
Date: Sun, 13 Aug 2017 19:49:39 +0100	[thread overview]
Message-ID: <20170813194939.7a96bc4b@ntlworld.com> (raw)
In-Reply-To: <20170813161207.GA6530@chaz.gmail.com>

On Sun, 13 Aug 2017 17:12:07 +0100
Stephane Chazelas <stephane.chazelas@gmail.com> wrote:
> 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 explanation makes sense, in which case I think this fix should be
good enough.

pws

diff --git a/Src/exec.c b/Src/exec.c
index f339dd6..61ae5e8 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -947,6 +947,14 @@ hashcmd(char *arg0, char **pp)
 int
 forklevel;
 
+/* FDs saved for possible restoring, not needed in a subshell
+ * where we will never need to restore them.  Hence if we enter
+ * a subshell these will simply be closed unconditionally.
+ *
+ * A value >= 10 indicates a valid saved fd.
+ */
+static int saved_fds[10];
+
 /* Arguments to entersubsh() */
 enum {
     /* Subshell is to be run asynchronously (else synchronously) */
@@ -972,7 +980,7 @@ enum {
 static void
 entersubsh(int flags)
 {
-    int sig, monitor, job_control_ok;
+    int i, sig, monitor, job_control_ok;
 
     if (!(flags & ESUB_KEEPTRAP))
 	for (sig = 0; sig < SIGCOUNT; sig++)
@@ -1083,6 +1091,12 @@ entersubsh(int flags)
 	opts[MONITOR] = 0;
     opts[USEZLE] = 0;
     zleactive = 0;
+    for (i = 0; i != 10; i++) {
+	if (saved_fds[i] >= 10) {
+	    close(saved_fds[i]);
+	    saved_fds[i] = 0;
+	}
+    }
     if (flags & ESUB_PGRP)
 	clearjobtab(monitor);
     get_usage();
@@ -2318,6 +2332,7 @@ addfd(int forked, int *save, struct multio **mfds, int fd1, int fd2, int rflag,
 			return;
 		    }
 		    save[fd1] = fdN;
+		    saved_fds[fd1] = fdN;
 		}
 	    }
 	}
@@ -4249,9 +4264,12 @@ fixfds(int *save)
     int old_errno = errno;
     int i;
 
-    for (i = 0; i != 10; i++)
+    for (i = 0; i != 10; i++) {
 	if (save[i] != -2)
 	    redup(save[i], i);
+	if (save[i] >= 10)
+	    saved_fds[i] = 0;
+    }
     errno = old_errno;
 }
 


  parent reply	other threads:[~2017-08-13 18:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-13 16:12 Stephane Chazelas
2017-08-13 18:49 ` Bart Schaefer
2017-08-13 18:49 ` Peter Stephenson [this message]
2017-08-13 21:45   ` Bart Schaefer
2017-08-14  9:09     ` Peter Stephenson
2017-08-14 14:49       ` Bart Schaefer
2017-08-14 15:24         ` Peter Stephenson
2017-08-14 15:31           ` Bart Schaefer
2017-08-14 19:19         ` Peter Stephenson
2017-09-29 15:06           ` Daniel Shahaf
2017-09-29 15:18             ` Peter Stephenson
2017-08-15 18:42         ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170813194939.7a96bc4b@ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).