Source-Changes-HG archive

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

[src/trunk]: src/bin/sh PR bin/48875 (is related, and ameliorated, but not ex...



details:   https://anonhg.NetBSD.org/src/rev/0b76571dc395
branches:  trunk
changeset: 834605:0b76571dc395
user:      kre <kre%NetBSD.org@localhost>
date:      Sun Aug 19 23:50:27 2018 +0000

description:
PR bin/48875 (is related, and ameliorated, but not exactly "fixed")

Import a whole set of tree evaluation enhancements from FreeBSD.

With these, before forking, the shell predicts (often) when all it will
have to do after forking (in the parent) is wait for the child and then
exit with the status from the child, and in such a case simply does not
fork, but rather allows the child to take over the parent's role.

This turns out to handle the particular test case from PR bin/48875 in
such a way that it works as hoped, rather than as it did (the delay there
was caused by an extra copy of the shell hanging around waiting for the
background child to complete ... and keeping the command substitution
stdout open, so the "real" parent had to wait in case more output appeared).

As part of doing this, redirection processing for compound commands gets
moved out of evalsubshell() and into a new evalredir(), which allows us
to properly handle errors occurring while performing those redirects,
and not mishandle (as in simply forget) fd's which had been moved out
of the way temporarily.

evaltree() has its degree of recursion reduced by making it loop to
handle the subsequent operation: that is instead of (for any binop
like ';' '&&' (etc)) where it used to
        evaltree(node->left);
        evaltree(node->right);
        return;
it now does (kind of)
        next = node;
        while ((node = next) != NULL) {
                next = NULL;

                if (node is a binary op) {
                        evaltree(node->left);
                        if appropriate /* if && test for success, etc */
                                next = node->right;
                        continue;
                }
                /* similar for loops, etc */
        }
which can be a good saving, as while the left side (now) tends to be
(usually) a simple (or simpleish) command, the right side can be many
commands (in a command sequence like a; b; c; d; ...  the node at the
top of the tree will now have "a" as its left node, and the tree for
b; c; d; ... as its right node - until now everything was evaluated
recursively so it made no difference, and the tree was constructed
the other way).

if/while/... statements are done similarly, recurse to evaluate the
condition, then if the (or one of the) body parts is to be evaluated,
set next to that, and loop (previously it recursed).

There is more to do in this area (particularly in the way that case
statements are processed - we can avoid recursion there as well) but
that can wait for another day.

While doing all of this we keep much better track of when the shell is
just going to exit once the current tree is evaluated (with a new
predicate at_eof() to tell us that we have, for sure, reached the end
of the input stream, that is, this shell will, for certain, not be reading
more command input) and use that info to avoid unneeded forks.   For that
we also need another new predicate (have_traps()) to determine of there
are any caught traps which might occur - if there are, we need to remain
to (potentially) handle them, so these optimisations will not occur (to
make the issue in PR 48875 appear again, run the same code, but with a
trap set to execute some code when a signal (or EXIT) occurs - note that
the trap must be set in the appropriate level of sub-shell to have this
effect, any caught traps are cleared in a subshell whenever one is created).

There is still work to be done to handle traps properly, whatever
weirdness they do (some of which is related to some of this.)

These changes do not need man page updates, but 48875 does - an update
to sh.1 will be forthcoming once it is decided what it should say...

Once again, all the heavy lifting for this set of changes comes directly
(with thanks) from the FreeBSD shell.

XXX pullup-8 (but not very soon)

diffstat:

 bin/sh/error.h  |    3 +-
 bin/sh/eval.c   |  356 +++++++++++++++++++++++++++++++++++--------------------
 bin/sh/input.c  |   43 ++++++-
 bin/sh/input.h  |    3 +-
 bin/sh/main.c   |    9 +-
 bin/sh/parser.c |   34 +++--
 bin/sh/trap.c   |   16 ++-
 bin/sh/trap.h   |    3 +-
 8 files changed, 313 insertions(+), 154 deletions(-)

diffs (truncated from 823 to 300 lines):

diff -r 8709546e5ca1 -r 0b76571dc395 bin/sh/error.h
--- a/bin/sh/error.h    Sun Aug 19 20:02:22 2018 +0000
+++ b/bin/sh/error.h    Sun Aug 19 23:50:27 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: error.h,v 1.20 2018/07/22 20:37:52 kre Exp $   */
+/*     $NetBSD: error.h,v 1.21 2018/08/19 23:50:27 kre Exp $   */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -70,6 +70,7 @@
 #define EXERROR 1      /* a generic error */
 #define EXSHELLPROC 2  /* execute a shell procedure */
 #define EXEXEC 3       /* command execution failed */
+#define EXEXIT 4       /* shell wants to exit(exitstatus) */
 
 
 /*
diff -r 8709546e5ca1 -r 0b76571dc395 bin/sh/eval.c
--- a/bin/sh/eval.c     Sun Aug 19 20:02:22 2018 +0000
+++ b/bin/sh/eval.c     Sun Aug 19 23:50:27 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: eval.c,v 1.158 2018/08/19 11:16:13 kre Exp $   */
+/*     $NetBSD: eval.c,v 1.159 2018/08/19 23:50:27 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.158 2018/08/19 11:16:13 kre Exp $");
+__RCSID("$NetBSD: eval.c,v 1.159 2018/08/19 23:50:27 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -114,6 +114,7 @@
 STATIC void evalcase(union node *, int);
 STATIC void evalsubshell(union node *, int);
 STATIC void expredir(union node *);
+STATIC void evalredir(union node *, int);
 STATIC void evalpipe(union node *);
 STATIC void evalcommand(union node *, int, struct backcmd *);
 STATIC void prehash(union node *);
@@ -214,18 +215,33 @@
 {
        union node *n;
        struct stackmark smark;
+       int last;
+       int any;
+
+       last = flag & EV_EXIT;
+       flag &= ~EV_EXIT;
 
        setstackmark(&smark);
        setinputstring(s, 1, line_number);
 
+       any = 0;        /* to determine if exitstatus will have been set */
        while ((n = parsecmd(0)) != NEOF) {
                XTRACE(DBG_EVAL, ("evalstring: "), showtree(n));
-               if (n && nflag == 0)
-                       evaltree(n, flag);
+               if (n && nflag == 0) {
+                       if (last && at_eof())
+                               evaltree(n, flag | EV_EXIT);
+                       else
+                               evaltree(n, flag);
+                       any = 1;
+               }
                popstackmark(&smark);
        }
        popfile();
        popstackmark(&smark);
+       if (!any)
+               exitstatus = 0;
+       if (last)
+               exraise(EXEXIT);
 }
 
 
@@ -240,6 +256,8 @@
 {
        bool do_etest;
        int sflags = flags & ~EV_EXIT;
+       union node *next;
+       struct stackmark smark;
 
        do_etest = false;
        if (n == NULL || nflag) {
@@ -247,119 +265,114 @@
                    n == NULL ? "NULL" : "-n"));
                if (nflag == 0)
                        exitstatus = 0;
-               goto out;
+               goto out2;
        }
+
+       setstackmark(&smark);
+       do {
 #ifndef SMALL
-       displayhist = 1;        /* show history substitutions done with fc */
+               displayhist = 1; /* show history substitutions done with fc */
 #endif
-       CTRACE(DBG_EVAL, ("pid %d, evaltree(%p: %s(%d), %#x) called\n",
-           getpid(), n, NODETYPENAME(n->type), n->type, flags));
-       switch (n->type) {
-       case NSEMI:
-               evaltree(n->nbinary.ch1, flags & EV_TESTED);
-               if (nflag || evalskip)
-                       goto out;
-               evaltree(n->nbinary.ch2, flags);
-               break;
-       case NAND:
-               evaltree(n->nbinary.ch1, EV_TESTED);
-               if (nflag || evalskip || exitstatus != 0)
-                       goto out;
-               evaltree(n->nbinary.ch2, flags);
-               break;
-       case NOR:
-               evaltree(n->nbinary.ch1, EV_TESTED);
-               if (nflag || evalskip || exitstatus == 0)
-                       goto out;
-               evaltree(n->nbinary.ch2, flags);
-               break;
-       case NREDIR:
-               expredir(n->nredir.redirect);
-               if (xflag && n->nredir.redirect) {
-                       union node *rn;
-
-                       outxstr(expandstr(ps4val(), line_number));
-                       outxstr("using redirections:");
-                       for (rn = n->nredir.redirect; rn; rn = rn->nfile.next)
-                               (void) outredir(outx, rn, ' ');
-                       outxstr(" do\n");
-                       flushout(outx);
-               }
-               redirect(n->nredir.redirect, REDIR_PUSH | REDIR_KEEP);
-               evaltree(n->nredir.n, flags);
-               popredir();
-               if (xflag && n->nredir.redirect) {
-                       outxstr(expandstr(ps4val(), line_number));
-                       outxstr("done\n");
-                       flushout(outx);
+               next = NULL;
+               CTRACE(DBG_EVAL, ("pid %d, evaltree(%p: %s(%d), %#x) called\n",
+                   getpid(), n, NODETYPENAME(n->type), n->type, flags));
+               switch (n->type) {
+               case NSEMI:
+                       evaltree(n->nbinary.ch1, sflags);
+                       if (nflag || evalskip)
+                               goto out1;
+                       next = n->nbinary.ch2;
+                       break;
+               case NAND:
+                       evaltree(n->nbinary.ch1, EV_TESTED);
+                       if (nflag || evalskip || exitstatus != 0)
+                               goto out1;
+                       next = n->nbinary.ch2;
+                       break;
+               case NOR:
+                       evaltree(n->nbinary.ch1, EV_TESTED);
+                       if (nflag || evalskip || exitstatus == 0)
+                               goto out1;
+                       next = n->nbinary.ch2;
+                       break;
+               case NREDIR:
+                       evalredir(n, flags);
+                       break;
+               case NSUBSHELL:
+                       evalsubshell(n, flags);
+                       do_etest = !(flags & EV_TESTED);
+                       break;
+               case NBACKGND:
+                       evalsubshell(n, flags);
+                       break;
+               case NIF: {
+                       evaltree(n->nif.test, EV_TESTED);
+                       if (nflag || evalskip)
+                               goto out1;
+                       if (exitstatus == 0)
+                               next = n->nif.ifpart;
+                       else if (n->nif.elsepart)
+                               next = n->nif.elsepart;
+                       else
+                               exitstatus = 0;
+                       break;
                }
-               break;
-       case NSUBSHELL:
-               evalsubshell(n, flags);
-               do_etest = !(flags & EV_TESTED);
-               break;
-       case NBACKGND:
-               evalsubshell(n, flags);
-               break;
-       case NIF: {
-               evaltree(n->nif.test, EV_TESTED);
-               if (nflag || evalskip)
-                       goto out;
-               if (exitstatus == 0)
-                       evaltree(n->nif.ifpart, flags);
-               else if (n->nif.elsepart)
-                       evaltree(n->nif.elsepart, flags);
-               else
+               case NWHILE:
+               case NUNTIL:
+                       evalloop(n, sflags);
+                       break;
+               case NFOR:
+                       evalfor(n, sflags);
+                       break;
+               case NCASE:
+                       evalcase(n, sflags);
+                       break;
+               case NDEFUN:
+                       CTRACE(DBG_EVAL, ("Defining fn %s @%d%s\n",
+                           n->narg.text, n->narg.lineno,
+                           fnline1 ? " LINENO=1" : ""));
+                       defun(n->narg.text, n->narg.next, n->narg.lineno);
                        exitstatus = 0;
-               break;
-       }
-       case NWHILE:
-       case NUNTIL:
-               evalloop(n, sflags);
-               break;
-       case NFOR:
-               evalfor(n, sflags);
-               break;
-       case NCASE:
-               evalcase(n, sflags);
-               break;
-       case NDEFUN:
-               CTRACE(DBG_EVAL, ("Defining fn %s @%d%s\n", n->narg.text,
-                   n->narg.lineno, fnline1 ? " LINENO=1" : ""));
-               defun(n->narg.text, n->narg.next, n->narg.lineno);
-               exitstatus = 0;
-               break;
-       case NNOT:
-               evaltree(n->nnot.com, EV_TESTED);
-               exitstatus = !exitstatus;
-               break;
-       case NDNOT:
-               evaltree(n->nnot.com, EV_TESTED);
-               if (exitstatus != 0)
-                       exitstatus = 1;
-               break;
-       case NPIPE:
-               evalpipe(n);
-               do_etest = !(flags & EV_TESTED);
-               break;
-       case NCMD:
-               evalcommand(n, flags, NULL);
-               do_etest = !(flags & EV_TESTED);
-               break;
-       default:
+                       break;
+               case NNOT:
+                       evaltree(n->nnot.com, EV_TESTED);
+                       exitstatus = !exitstatus;
+                       break;
+               case NDNOT:
+                       evaltree(n->nnot.com, EV_TESTED);
+                       if (exitstatus != 0)
+                               exitstatus = 1;
+                       break;
+               case NPIPE:
+                       evalpipe(n);
+                       do_etest = !(flags & EV_TESTED);
+                       break;
+               case NCMD:
+                       evalcommand(n, flags, NULL);
+                       do_etest = !(flags & EV_TESTED);
+                       break;
+               default:
 #ifdef NODETYPENAME
-               out1fmt("Node type = %d(%s)\n", n->type, NODETYPENAME(n->type));
+                       out1fmt("Node type = %d(%s)\n",
+                               n->type, NODETYPENAME(n->type));
 #else
-               out1fmt("Node type = %d\n", n->type);
+                       out1fmt("Node type = %d\n", n->type);
 #endif
-               flushout(&output);
-               break;
-       }
- out:
+                       flushout(&output);
+                       break;
+               }
+               n = next;
+               popstackmark(&smark);
+       } while(n != NULL);
+ out1:
+       popstackmark(&smark);
+ out2:
        if (pendingsigs)
                dotrap();
-       if ((flags & EV_EXIT) != 0 || (eflag && exitstatus != 0 && do_etest))
+       if (eflag && exitstatus != 0 && do_etest)
                exitshell(exitstatus);



Home | Main Index | Thread Index | Old Index