Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh Use fork() rather than vfork() when forking to run a ...



details:   https://anonhg.NetBSD.org/src/rev/1d9d8eeb7afe
branches:  trunk
changeset: 967772:1d9d8eeb7afe
user:      kre <kre%NetBSD.org@localhost>
date:      Sat Dec 21 18:54:15 2019 +0000

description:
Use fork() rather than vfork() when forking to run a background
process with redirects.   If we use vfork() and a redirect hangs
(eg: opening a fifo) which the parent was intended to unhang,
then the parent never gets to continue to unhang the child.

eg:  mkfifo f; cat <f &; echo foo>f

The parent should not be waiting for a background process, even
just for its exec() to complete.   if there are no redirects there
is (should be) nothing left that might be done that will cause any
noticeable delay, so vfork() should be safe in all other cases.

diffstat:

 bin/sh/eval.c |  9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)

diffs (38 lines):

diff -r 46288dd163fe -r 1d9d8eeb7afe bin/sh/eval.c
--- a/bin/sh/eval.c     Sat Dec 21 16:10:20 2019 +0000
+++ b/bin/sh/eval.c     Sat Dec 21 18:54:15 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: eval.c,v 1.176 2019/12/09 00:14:24 kre Exp $   */
+/*     $NetBSD: eval.c,v 1.177 2019/12/21 18:54:15 kre Exp $   */
 
 /*-
  * Copyright (c) 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)eval.c     8.9 (Berkeley) 6/8/95";
 #else
-__RCSID("$NetBSD: eval.c,v 1.176 2019/12/09 00:14:24 kre Exp $");
+__RCSID("$NetBSD: eval.c,v 1.177 2019/12/21 18:54:15 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -552,8 +552,8 @@
            forkshell(jp = makejob(n, 1), n, backgnd?FORK_BG:FORK_FG) == 0) {
                if (backgnd)
                        flags &=~ EV_TESTED;
+               INTON;
                redirect(n->nredir.redirect, REDIR_KEEP);
-               INTON;
                evaltree(n->nredir.n, flags | EV_EXIT);   /* never returns */
        } else if (backgnd)
                exitstatus = 0;
@@ -1086,7 +1086,8 @@
                 * child's address space is actually shared with the parent as
                 * we rely on this.
                 */
-               if (usefork == 0 && cmdentry.cmdtype == CMDNORMAL) {
+               if (usefork == 0 && cmdentry.cmdtype == CMDNORMAL &&
+                   (!cmd->ncmd.backgnd || cmd->ncmd.redirect == NULL)) {
                        pid_t   pid;
                        int serrno;
 



Home | Main Index | Thread Index | Old Index