Source-Changes-HG archive

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

[src/trunk]: src/bin/sh Now we use vfork(2) instead of fork(2) when we can.



details:   https://anonhg.NetBSD.org/src/rev/3c3db691fe0f
branches:  trunk
changeset: 486133:3c3db691fe0f
user:      elric <elric%NetBSD.org@localhost>
date:      Sat May 13 20:50:14 2000 +0000

description:
Now we use vfork(2) instead of fork(2) when we can.

diffstat:

 bin/sh/eval.c  |   77 ++++++++++++++++++++++--
 bin/sh/exec.c  |   25 +++++--
 bin/sh/exec.h  |    4 +-
 bin/sh/input.c |   18 ++++-
 bin/sh/input.h |    4 +-
 bin/sh/jobs.c  |  175 ++++++++++++++++++++++++++++++++------------------------
 bin/sh/jobs.h  |    4 +-
 bin/sh/main.c  |    6 +-
 bin/sh/redir.c |   16 +++-
 bin/sh/redir.h |    5 +-
 bin/sh/shell.h |   10 ++-
 bin/sh/trap.c  |   57 +++++++++++-------
 bin/sh/trap.h  |    8 +-
 13 files changed, 273 insertions(+), 136 deletions(-)

diffs (truncated from 881 to 300 lines):

diff -r bc88df6d8dc2 -r 3c3db691fe0f bin/sh/eval.c
--- a/bin/sh/eval.c     Sat May 13 20:34:13 2000 +0000
+++ b/bin/sh/eval.c     Sat May 13 20:50:14 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: eval.c,v 1.51 2000/02/09 20:26:53 christos Exp $       */
+/*     $NetBSD: eval.c,v 1.52 2000/05/13 20:50:14 elric Exp $  */
 
 /*-
  * Copyright (c) 1993
@@ -41,10 +41,13 @@
 #if 0
 static char sccsid[] = "@(#)eval.c     8.9 (Berkeley) 6/8/95";
 #else
-__RCSID("$NetBSD: eval.c,v 1.51 2000/02/09 20:26:53 christos Exp $");
+__RCSID("$NetBSD: eval.c,v 1.52 2000/05/13 20:50:14 elric Exp $");
 #endif
 #endif /* not lint */
 
+#include <errno.h>
+#include <fcntl.h>
+#include <paths.h>
 #include <signal.h>
 #include <unistd.h>
 
@@ -52,6 +55,7 @@
  * Evaluate a command.
  */
 
+#include "main.h"
 #include "shell.h"
 #include "nodes.h"
 #include "syntax.h"
@@ -588,6 +592,7 @@
 }
 
 
+int vforked = 0;
 
 /*
  * Execute a simple command.
@@ -627,6 +632,7 @@
        (void) &flags;
 #endif
 
+       vforked = 0;
        /* First expand the arguments. */
        TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
        setstackmark(&smark);
@@ -736,8 +742,62 @@
                        if (pipe(pip) < 0)
                                error("Pipe call failed");
                }
-               if (forkshell(jp, cmd, mode) != 0)
-                       goto parent;    /* at end of routine */
+#ifdef DO_SHAREDVFORK
+               /* It is essential that if DO_SHAREDVFORK is defined that the
+                * child's address space is actually shared with the parent as
+                * we rely on this.
+                */
+               if (cmdentry.cmdtype == CMDNORMAL) {
+                       pid_t   pid;
+
+                       INTOFF;
+                       vforked = 1;
+                       switch (pid = vfork()) {
+                       case -1:
+                               TRACE(("Vfork failed, errno=%d", errno));
+                               INTON;
+                               error("Cannot vfork");
+                               break;
+                       case 0:
+                               /* Make sure that exceptions only unwind to
+                                * after the vfork(2)
+                                */
+                               if (setjmp(jmploc.loc)) {
+                                       if (exception == EXSHELLPROC) {
+                                               /* We can't progress with the vfork,
+                                                * so, set vforked = 2 so the parent
+                                                * knows, and _exit();
+                                                */
+                                               vforked = 2;
+                                               _exit(0);
+                                       } else {
+                                               _exit(exerrno);
+                                       }
+                               }
+                               savehandler = handler;
+                               handler = &jmploc;
+                               forkchild(jp, cmd, mode, vforked);
+                               break;
+                       default:
+                               handler = savehandler;  /* restore from vfork(2) */
+                               if (vforked == 2) {
+                                       vforked = 0;
+                                       waitpid(pid, NULL, 0);
+                                       /* We need to progress in a normal fork fashion */
+                                       goto normal_fork;
+                               }
+                               vforked = 0;
+                               forkparent(jp, cmd, mode, pid);
+                               goto parent;
+                       }
+               } else {
+normal_fork:
+#endif
+                       if (forkshell(jp, cmd, mode) != 0)
+                               goto parent;    /* at end of routine */
+#ifdef DO_SHAREDVFORK
+               }
+#endif
                if (flags & EV_BACKCMD) {
                        FORCEINTON;
                        close(pip[0]);
@@ -862,12 +922,12 @@
 #ifdef DEBUG
                trputs("normal command:  ");  trargs(argv);
 #endif
-               clearredir();
-               redirect(cmd->ncmd.redirect, 0);
+               clearredir(vforked?REDIR_VFORK:0);
+               redirect(cmd->ncmd.redirect, vforked?REDIR_VFORK:0);
                for (sp = varlist.list ; sp ; sp = sp->next)
                        setvareq(sp->text, VEXPORT|VSTACK);
                envp = environment();
-               shellexec(argv, envp, pathval(), cmdentry.u.index);
+               shellexec(argv, envp, pathval(), cmdentry.u.index, vforked);
        }
        goto out;
 
@@ -892,7 +952,6 @@
 }
 
 
-
 /*
  * Search for a command.  This is called before we fork so that the
  * location of the command will be available in the parent as well as
@@ -1022,7 +1081,7 @@
                optschanged();
                for (sp = cmdenviron; sp ; sp = sp->next)
                        setvareq(sp->text, VEXPORT|VSTACK);
-               shellexec(argv + 1, environment(), pathval(), 0);
+               shellexec(argv + 1, environment(), pathval(), 0, 0);
        }
        return 0;
 }
diff -r bc88df6d8dc2 -r 3c3db691fe0f bin/sh/exec.c
--- a/bin/sh/exec.c     Sat May 13 20:34:13 2000 +0000
+++ b/bin/sh/exec.c     Sat May 13 20:50:14 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: exec.c,v 1.27 1999/07/09 03:05:49 christos Exp $       */
+/*     $NetBSD: exec.c,v 1.28 2000/05/13 20:50:14 elric Exp $  */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -41,15 +41,17 @@
 #if 0
 static char sccsid[] = "@(#)exec.c     8.4 (Berkeley) 6/8/95";
 #else
-__RCSID("$NetBSD: exec.c,v 1.27 1999/07/09 03:05:49 christos Exp $");
+__RCSID("$NetBSD: exec.c,v 1.28 2000/05/13 20:50:14 elric Exp $");
 #endif
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <stdio.h>
 #include <stdlib.h>
 
 /*
@@ -102,7 +104,7 @@
 int exerrno = 0;                       /* Last exec error */
 
 
-STATIC void tryexec __P((char *, char **, char **));
+STATIC void tryexec __P((char *, char **, char **, int));
 STATIC void execinterp __P((char **, char **));
 STATIC void printentry __P((struct tblentry *, int));
 STATIC void clearcmdentry __P((int));
@@ -117,22 +119,23 @@
  */
 
 void
-shellexec(argv, envp, path, idx)
+shellexec(argv, envp, path, idx, vforked)
        char **argv, **envp;
        const char *path;
        int idx;
+       int vforked;
 {
        char *cmdname;
        int e;
 
        if (strchr(argv[0], '/') != NULL) {
-               tryexec(argv[0], argv, envp);
+               tryexec(argv[0], argv, envp, vforked);
                e = errno;
        } else {
                e = ENOENT;
                while ((cmdname = padvance(&path, argv[0])) != NULL) {
                        if (--idx < 0 && pathopt == NULL) {
-                               tryexec(cmdname, argv, envp);
+                               tryexec(cmdname, argv, envp, vforked);
                                if (errno != ENOENT && errno != ENOTDIR)
                                        e = errno;
                        }
@@ -158,10 +161,11 @@
 
 
 STATIC void
-tryexec(cmd, argv, envp)
+tryexec(cmd, argv, envp, vforked)
        char *cmd;
        char **argv;
        char **envp;
+       int vforked;
        {
        int e;
 #ifndef BSD
@@ -177,6 +181,13 @@
 #endif
        e = errno;
        if (e == ENOEXEC) {
+               if (vforked) {
+                       /* We are currently vfork(2)ed, so raise an
+                        * exception, and evalcommand will try again
+                        * with a normal fork(2).
+                        */
+                       exraise(EXSHELLPROC);
+               }
                initshellproc();
                setinputfile(cmd, 0);
                commandname = arg0 = savestr(argv[0]);
diff -r bc88df6d8dc2 -r 3c3db691fe0f bin/sh/exec.h
--- a/bin/sh/exec.h     Sat May 13 20:34:13 2000 +0000
+++ b/bin/sh/exec.h     Sat May 13 20:50:14 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: exec.h,v 1.15 1999/07/09 03:05:50 christos Exp $       */
+/*     $NetBSD: exec.h,v 1.16 2000/05/13 20:50:14 elric Exp $  */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -60,7 +60,7 @@
 extern const char *pathopt;    /* set by padvance */
 extern int exerrno;            /* last exec error */
 
-void shellexec __P((char **, char **, const char *, int))
+void shellexec __P((char **, char **, const char *, int, int))
     __attribute__((noreturn));
 char *padvance __P((const char **, const char *));
 int hashcmd __P((int, char **));
diff -r bc88df6d8dc2 -r 3c3db691fe0f bin/sh/input.c
--- a/bin/sh/input.c    Sat May 13 20:34:13 2000 +0000
+++ b/bin/sh/input.c    Sat May 13 20:50:14 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: input.c,v 1.32 1999/07/09 03:05:50 christos Exp $      */
+/*     $NetBSD: input.c,v 1.33 2000/05/13 20:50:14 elric Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -41,7 +41,7 @@
 #if 0
 static char sccsid[] = "@(#)input.c    8.3 (Berkeley) 6/9/95";
 #else
-__RCSID("$NetBSD: input.c,v 1.32 1999/07/09 03:05:50 christos Exp $");
+__RCSID("$NetBSD: input.c,v 1.33 2000/05/13 20:50:14 elric Exp $");
 #endif
 #endif /* not lint */
 
@@ -506,10 +506,22 @@
 /*
  * Close the file(s) that the shell is reading commands from.  Called
  * after a fork is done.
+ *
+ * Takes one arg, vfork, which tells it to not modify its global vars
+ * as it is still running in the parent.
  */
 
 void
-closescript() {
+closescript(int vforked) {
+       if (vforked) {
+               struct parsefile *pf;
+
+               for (pf=parsefile; pf != &basepf; pf=pf->prev)
+                       close(pf->fd);
+               if (parsefile->fd > 0)
+                       close(parsefile->fd);
+               return;
+       }
        popallfiles();
        if (parsefile->fd > 0) {
                close(parsefile->fd);
diff -r bc88df6d8dc2 -r 3c3db691fe0f bin/sh/input.h
--- a/bin/sh/input.h    Sat May 13 20:34:13 2000 +0000
+++ b/bin/sh/input.h    Sat May 13 20:50:14 2000 +0000



Home | Main Index | Thread Index | Old Index