src/solaris/native/java/lang/UNIXProcess_md.c

Print this page

        

@@ -47,17 +47,23 @@
 #include <dirent.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <limits.h>
 
+#if defined(__solaris__) || defined(__APPLE__)
+#include <spawn.h>
+#endif
+
+#include "unixprocess_md.h"
+
 #ifdef __APPLE__
 #include <crt_externs.h>
 #define environ (*_NSGetEnviron())
 #endif
 
 /*
- * There are 3 possible strategies we might use to "fork":
+ * There are 4 possible strategies we might use to "fork":
  *
  * - fork(2).  Very portable and reliable but subject to
  *   failure due to overcommit (see the documentation on
  *   /proc/sys/vm/overcommit_memory in Linux proc(5)).
  *   This is the ancient problem of spurious failure whenever a large

@@ -88,41 +94,27 @@
  *     #  Error: pthread_getattr_np failed with errno = 3 (ESRCH)
  *   We believe this is a glibc bug, reported here:
  *     http://sources.redhat.com/bugzilla/show_bug.cgi?id=10311
  *   but the glibc maintainers closed it as WONTFIX.
  *
+ * - posix_spawn(). While posix_spawn() is a fairly elaborate and 
+ *   complicated system call, it can't quite do everything that the old 
+ *   fork()/exec() combination can do, so the only feasible way to do 
+ *   this, is to use posix_spawn to launch a new helper executable 
+ *   "jprochelper", which in turn execs the target (after cleaning 
+ *   up file-descriptors etc.) The end result is the same as before, 
+ *   a child process linked to the parent in the same way, but it 
+ *   avoids the problem of duplicating the parent (VM) process
+ *   address space temporarily, before launching the target command.
+ *
  * Based on the above analysis, we are currently using vfork() on
- * Linux and fork() on other Unix systems, but the code to use clone()
- * remains.
+ * Linux and spawn() on other Unix systems, but the code to use clone()
+ * and fork() remains.
  */
 
-#define START_CHILD_USE_CLONE 0  /* clone() currently disabled; see above. */
-
-#ifndef START_CHILD_USE_CLONE
-  #ifdef __linux__
-    #define START_CHILD_USE_CLONE 1
-  #else
-    #define START_CHILD_USE_CLONE 0
-  #endif
-#endif
-
-/* By default, use vfork() on Linux. */
-#ifndef START_CHILD_USE_VFORK
-  #ifdef __linux__
-    #define START_CHILD_USE_VFORK 1
-  #else
-    #define START_CHILD_USE_VFORK 0
-  #endif
-#endif
-
-#if START_CHILD_USE_CLONE
+#ifdef __linux__
 #include <sched.h>
-#define START_CHILD_SYSTEM_CALL "clone"
-#elif START_CHILD_USE_VFORK
-#define START_CHILD_SYSTEM_CALL "vfork"
-#else
-#define START_CHILD_SYSTEM_CALL "fork"
 #endif
 
 #ifndef STDIN_FILENO
 #define STDIN_FILENO 0
 #endif

@@ -212,18 +204,29 @@
  * Fortunately, it's only needed if the child has PATH while we do not.
  */
 static const char*
 defaultPath(void)
 {
+#ifdef _CS_PATH
+    char *pathbuf;
+    size_t n;
+    n = confstr(_CS_PATH,NULL,(size_t) 0);
+    pathbuf = malloc(n);
+    if (pathbuf == NULL)
+        abort();
+    confstr(_CS_PATH, pathbuf, n);
+    return pathbuf;
+#else
 #ifdef __solaris__
     /* These really are the Solaris defaults! */
     return (geteuid() == 0 || getuid() == 0) ?
         "/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin:/usr/sbin" :
         "/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin:";
 #else
     return ":/bin:/usr/bin";    /* glibc */
 #endif
+#endif
 }
 
 static const char*
 effectivePath(void)
 {

@@ -269,27 +272,29 @@
 
 /**
  * Cached value of JVM's effective PATH.
  * (We don't support putenv("PATH=...") in native code)
  */
-static const char *parentPath;
+const char *xx_parentPath;
 
 /**
- * Split, canonicalized version of parentPath
+ * Split, canonicalized version of xx_parentPath
  */
-static const char * const *parentPathv;
+const char * const *xx_parentPathv;
 
 static jfieldID field_exitcode;
+static jfieldID helperfield;
 
 JNIEXPORT void JNICALL
 Java_java_lang_UNIXProcess_initIDs(JNIEnv *env, jclass clazz)
 {
     field_exitcode = (*env)->GetFieldID(env, clazz, "exitcode", "I");
+    helperfield = (*env)->GetStaticFieldID(env, clazz,
+                      "helperpath", "Ljava/lang/String;");
+    xx_parentPath  = effectivePath();
+    xx_parentPathv = splitPath(env, xx_parentPath);
 
-    parentPath  = effectivePath();
-    parentPathv = splitPath(env, parentPath);
-
     setSIGCHLDHandler(env);
 }
 
 
 #ifndef WIFEXITED

@@ -316,10 +321,11 @@
                                               jint pid)
 {
     /* We used to use waitid() on Solaris, waitpid() on Linux, but
      * waitpid() is more standard, so use it on all POSIX platforms. */
     int status;
+
     /* Wait for the child process to exit.  This returns immediately if
        the child has already exited. */
     while (waitpid(pid, &status, 0) < 0) {
         switch (errno) {
         case ECHILD: return 0;

@@ -436,13 +442,14 @@
 static int
 moveDescriptor(int fd_from, int fd_to)
 {
     if (fd_from != fd_to) {
         if ((restartableDup2(fd_from, fd_to) == -1) ||
-            (restartableClose(fd_from) == -1))
+            (restartableClose(fd_from) == -1)) {
             return -1;
     }
+    }
     return 0;
 }
 
 static const char *
 getBytes(JNIEnv *env, jbyteArray arr)

@@ -456,18 +463,20 @@
 {
     if (parr != NULL)
         (*env)->ReleaseByteArrayElements(env, arr, (jbyte*) parr, JNI_ABORT);
 }
 
-static void
-initVectorFromBlock(const char**vector, const char* block, int count)
+void
+xx_initVectorFromBlock(const char**vector, const char* block, int count)
 {
     int i;
     const char *p;
+
     for (i = 0, p = block; i < count; i++) {
         /* Invariant: p always points to the start of a C string. */
         vector[i] = p;
+
         while (*(p++));
     }
     vector[count] = NULL;
 }
 

@@ -539,24 +548,24 @@
 /**
  * Like execve(2), except that in case of ENOEXEC, FILE is assumed to
  * be a shell script and the system default shell is invoked to run it.
  */
 static void
-execve_with_shell_fallback(const char *file,
+execve_with_shell_fallback(int mode, const char *file,
                            const char *argv[],
                            const char *const envp[])
 {
-#if START_CHILD_USE_CLONE || START_CHILD_USE_VFORK
+    if (mode == MODE_CLONE || mode == MODE_VFORK) {
     /* shared address space; be very careful. */
     execve(file, (char **) argv, (char **) envp);
     if (errno == ENOEXEC)
         execve_as_traditional_shell_script(file, argv, envp);
-#else
+    } else {
     /* unshared address space; we can mutate environ. */
     environ = (char **) envp;
     execvp(file, (char **) argv);
-#endif
+    }
 }
 
 /**
  * 'execvpe' should have been included in the Unix standards,
  * and is a GNU extension in glibc 2.10.

@@ -563,11 +572,11 @@
  *
  * JDK_execvpe is identical to execvp, except that the child environment is
  * specified via the 3rd argument instead of being inherited from environ.
  */
 static void
-JDK_execvpe(const char *file,
+JDK_execvpe(int mode, const char *file,
             const char *argv[],
             const char *const envp[])
 {
     if (envp == NULL || (char **) envp == environ) {
         execvp(file, (char **) argv);

@@ -578,28 +587,29 @@
         errno = ENOENT;
         return;
     }
 
     if (strchr(file, '/') != NULL) {
-        execve_with_shell_fallback(file, argv, envp);
+        execve_with_shell_fallback(mode, file, argv, envp);
     } else {
         /* We must search PATH (parent's, not child's) */
         char expanded_file[PATH_MAX];
         int filelen = strlen(file);
         int sticky_errno = 0;
         const char * const * dirs;
-        for (dirs = parentPathv; *dirs; dirs++) {
+
+        for (dirs = xx_parentPathv; *dirs; dirs++) {
             const char * dir = *dirs;
             int dirlen = strlen(dir);
             if (filelen + dirlen + 1 >= PATH_MAX) {
                 errno = ENAMETOOLONG;
                 continue;
             }
             memcpy(expanded_file, dir, dirlen);
             memcpy(expanded_file + dirlen, file, filelen);
             expanded_file[dirlen + filelen] = '\0';
-            execve_with_shell_fallback(expanded_file, argv, envp);
+            execve_with_shell_fallback(mode, expanded_file, argv, envp);
             /* There are 3 responses to various classes of errno:
              * return immediately, continue (especially for ENOENT),
              * or continue with "sticky" errno.
              *
              * From exec(3):

@@ -637,18 +647,18 @@
             errno = sticky_errno;
     }
 }
 
 /*
- * Reads nbyte bytes from file descriptor fd into buf,
+ * Reads nbyte bytes from file descriptor fd into buf.
  * The read operation is retried in case of EINTR or partial reads.
  *
  * Returns number of bytes read (normally nbyte, but may be less in
  * case of EOF).  In case of read errors, returns -1 and sets errno.
  */
-static ssize_t
-readFully(int fd, void *buf, size_t nbyte)
+ssize_t
+xx_readFully(int fd, void *buf, size_t nbyte)
 {
     ssize_t remaining = nbyte;
     for (;;) {
         ssize_t n = read(fd, buf, remaining);
         if (n == 0) {

@@ -667,60 +677,53 @@
             return -1;
         }
     }
 }
 
-typedef struct _ChildStuff
-{
-    int in[2];
-    int out[2];
-    int err[2];
-    int fail[2];
-    int fds[3];
-    const char **argv;
-    const char **envv;
-    const char *pdir;
-    jboolean redirectErrorStream;
-#if START_CHILD_USE_CLONE
-    void *clone_stack;
-#endif
-} ChildStuff;
-
 static void
 copyPipe(int from[2], int to[2])
 {
     to[0] = from[0];
     to[1] = from[1];
 }
 
 /**
- * Child process after a successful fork() or clone().
+ * Child process after a successful v/fork(), clone() or posix_spawn().
  * This function must not return, and must be prepared for either all
  * of its address space to be shared with its parent, or to be a copy.
  * It must not modify global variables such as "environ".
  */
-static int
-childProcess(void *arg)
+int
+xx_childProcess(void *arg)
 {
-    const ChildStuff* p = (const ChildStuff*) arg;
+    ChildStuff* p = (ChildStuff*) arg;
 
     /* Close the parent sides of the pipes.
        Closing pipe fds here is redundant, since closeDescriptors()
-       would do it anyways, but a little paranoia is a good thing. */
+       would do it anyways, but a little paranoia is a good thing.
+       We close both sides of the childenv pipe since in spawn
+       mode we are finished with it now, and it's not used at all
+       in the other modes */
     if ((closeSafely(p->in[1])   == -1) ||
         (closeSafely(p->out[0])  == -1) ||
         (closeSafely(p->err[0])  == -1) ||
+        (closeSafely(p->childenv[0])  == -1) ||
+        (closeSafely(p->childenv[1])  == -1) ||
         (closeSafely(p->fail[0]) == -1))
         goto WhyCantJohnnyExec;
 
+    p->in[1] = -1; p->out[0] = -1; p->err[0] = -1;
+    p->childenv[0] = -1; p->childenv[1] = -1; p->fail[0] = -1;
+
     /* Give the child sides of the pipes the right fileno's. */
     /* Note: it is possible for in[0] == 0 */
     if ((moveDescriptor(p->in[0] != -1 ?  p->in[0] : p->fds[0],
                         STDIN_FILENO) == -1) ||
         (moveDescriptor(p->out[1]!= -1 ? p->out[1] : p->fds[1],
-                        STDOUT_FILENO) == -1))
+                        STDOUT_FILENO) == -1)) {
         goto WhyCantJohnnyExec;
+    }
 
     if (p->redirectErrorStream) {
         if ((closeSafely(p->err[1]) == -1) ||
             (restartableDup2(STDOUT_FILENO, STDERR_FILENO) == -1))
             goto WhyCantJohnnyExec;

@@ -747,11 +750,11 @@
         goto WhyCantJohnnyExec;
 
     if (fcntl(FAIL_FILENO, F_SETFD, FD_CLOEXEC) == -1)
         goto WhyCantJohnnyExec;
 
-    JDK_execvpe(p->argv[0], p->argv, p->envv);
+    JDK_execvpe(p->mode, p->argv[0], p->argv, p->envv);
 
  WhyCantJohnnyExec:
     /* We used to go to an awful lot of trouble to predict whether the
      * child would fail, but there is no reliable way to predict the
      * success of an operation without *trying* it, and there's no way

@@ -769,104 +772,282 @@
     restartableClose(FAIL_FILENO);
     _exit(-1);
     return 0;  /* Suppress warning "no return value from function" */
 }
 
+/* arg is an array of pointers to 0 terminated strings. array is terminated
+ * by a null element.
+ *
+ * *nelems and *nbytes receive the number of elements of array (incl 0)
+ * and total number of bytes (incl. 0)
+ * Note. An empty array will have one null element
+ * But if arg is null, then *nelems set to 0, and *nbytes to 0
+ */
+static void arraysize (const char * const *arg, int *nelems, int *nbytes)
+{
+    int i, bytes, count;
+    const char * const *a = arg;
+    char *p;
+    int *q;
+    if (arg == 0) {
+        *nelems = 0;
+        *nbytes = 0;
+        return;
+    }
+    /* count the array elements and number of bytes */
+    for (count=0, bytes=0; *a != 0; count++, a++) {
+        bytes += strlen (*a)+1;
+    }
+    *nbytes = bytes;
+    *nelems = count+1;
+}
+
+/* copy the strings from arg[] into buf, starting at given offset
+ * return new offset to next free byte
+ */
+static int copystrings (char *buf, int offset, const char * const *arg) {
+    char *p;
+    const char * const *a;
+    int count=0;
+
+    if (arg == 0) {
+        return offset;
+    }
+    for (p=buf+offset, a=arg; *a != 0; a++) {
+        int len = strlen (*a) +1;
+        memcpy (p, *a, len);
+        p += len;
+        count += len;
+    }
+    return offset+count;
+}
+
 /**
- * Start a child process running function childProcess.
+ * Start a child process running function xx_childProcess.
  * This function only returns in the parent.
  * We are unusually paranoid; use of clone/vfork is
  * especially likely to tickle gcc/glibc bugs.
  */
 #ifdef __attribute_noinline__  /* See: sys/cdefs.h */
 __attribute_noinline__
 #endif
+
 static pid_t
-startChild(ChildStuff *c) {
-#if START_CHILD_USE_CLONE
+cloneChild(ChildStuff *c) {
+#ifdef __linux__
 #define START_CHILD_CLONE_STACK_SIZE (64 * 1024)
+
     /*
      * See clone(2).
      * Instead of worrying about which direction the stack grows, just
      * allocate twice as much and start the stack in the middle.
      */
     if ((c->clone_stack = malloc(2 * START_CHILD_CLONE_STACK_SIZE)) == NULL)
         /* errno will be set to ENOMEM */
         return -1;
-    return clone(childProcess,
+    return clone(xx_childProcess,
                  c->clone_stack + START_CHILD_CLONE_STACK_SIZE,
                  CLONE_VFORK | CLONE_VM | SIGCHLD, c);
 #else
-  #if START_CHILD_USE_VFORK
+        /* not available on Solaris / Mac */
+        assert(0);
+#endif
+}
+
+static pid_t
+vforkChild(ChildStuff *c) {
+    volatile pid_t resultPid;
+
     /*
      * We separate the call to vfork into a separate function to make
      * very sure to keep stack of child from corrupting stack of parent,
      * as suggested by the scary gcc warning:
      *  warning: variable 'foo' might be clobbered by 'longjmp' or 'vfork'
      */
-    volatile pid_t resultPid = vfork();
-  #else
+    resultPid = vfork();
+
+    if (resultPid == 0) {
+        xx_childProcess(c);
+    }
+    assert(resultPid != 0);  /* xx_childProcess never returns */
+    return resultPid;
+}
+
+static pid_t
+forkChild(ChildStuff *c) {
+    pid_t resultPid;
+
     /*
      * From Solaris fork(2): In Solaris 10, a call to fork() is
      * identical to a call to fork1(); only the calling thread is
      * replicated in the child process. This is the POSIX-specified
      * behavior for fork().
      */
-    pid_t resultPid = fork();
-  #endif
-    if (resultPid == 0)
-        childProcess(c);
-    assert(resultPid != 0);  /* childProcess never returns */
+    resultPid = fork();
+
+    if (resultPid == 0) {
+        xx_childProcess(c);
+    }
+    assert(resultPid != 0);  /* xx_childProcess never returns */
     return resultPid;
-#endif /* ! START_CHILD_USE_CLONE */
 }
 
+static pid_t
+spawnChild(JNIEnv *env, jobject process, ChildStuff *c) {
+    pid_t resultPid;
+    const char *helperpath;
+    jobject helperobj;
+    jboolean isCopy;
+    int i, offset, rval, bufsize;
+    char *buf, buf1[16];
+    char *hlpargs[2];
+    SpawnInfo sp;
+
+    helperobj = (*env)->GetStaticObjectField(
+        env, (*env)->GetObjectClass (env, process),
+        helperfield
+    );
+    if (helperobj == NULL) {
+        return -1;
+    }
+    helperpath = GetStringPlatformChars (env, helperobj, &isCopy);
+    /* need to tell helper which fd is for receiving the childstuff
+     * and which fd to send response back on
+     */
+    sprintf (buf1, "%d:%d", c->childenv[0], c->fail[1]);
+    /* put the fd string as argument to the helper cmd */
+    hlpargs[0] = buf1;
+    hlpargs[1] = 0;
+
+    /* Following items are sent down the pipe to the helper
+     * after it is spawned.
+     * All strings are null terminated. All arrays of strings
+     * have an empty string for termination.
+     * - the ChildStuff struct
+     * - the SpawnInfo struct
+     * - the argv strings array
+     * - the envv strings array
+     * - the home directory string
+     * - the parentPath string
+     * - the parentPathv array
+     */
+     /* First calculate the sizes */
+    arraysize (c->argv, &sp.nargv, &sp.argvBytes);
+    bufsize = sp.argvBytes;
+    arraysize (c->envv, &sp.nenvv, &sp.envvBytes);
+    bufsize += sp.envvBytes;
+    sp.dirlen = c->pdir == 0 ? 0 : strlen (c->pdir)+1;
+    bufsize += sp.dirlen;
+    sp.parentPathLen = xx_parentPath == 0 ? 0 : strlen (xx_parentPath)+1;
+    bufsize += sp.parentPathLen;
+    arraysize (xx_parentPathv, &sp.nparentPathv, &sp.parentPathvBytes);
+    bufsize += sp.parentPathvBytes;
+    /* We need to clear FD_CLOEXEC if set in the fds[].
+     * Files are created FD_CLOEXEC in Java.
+     * Otherwise, they will be closed when the target gets exec'd */
+    for (i=0; i<3; i++) {
+        if (c->fds[i] != -1) {
+            int flags = fcntl(c->fds[i], F_GETFD);
+            if (flags & FD_CLOEXEC) {
+                fcntl (c->fds[i], F_SETFD, flags & (~1));
+            }
+        }
+    }
+
+    rval = posix_spawn (&resultPid, helperpath, 0, 0, (char * const *) hlpargs, environ);
+
+    if (rval != 0) {
+        return -1;
+    }
+
+    /* now the lengths are known, copy the data */
+    buf = NEW (char, bufsize);
+    if (buf == 0) {
+        return -1;
+    }
+    offset = copystrings (buf, 0, &c->argv[0]);
+    offset = copystrings (buf, offset, &c->envv[0]);
+    memcpy (buf+offset, c->pdir, sp.dirlen);
+    offset += sp.dirlen;
+    memcpy (buf+offset, xx_parentPath, sp.parentPathLen);
+    offset += sp.parentPathLen;
+    offset = copystrings (buf, offset, xx_parentPathv);
+    assert (offset == bufsize);
+
+    /* write the two structs and the data buffer */
+    write (c->childenv[1], (char *)c, sizeof(*c));
+    write (c->childenv[1], (char *)&sp, sizeof(sp));
+    write (c->childenv[1], buf, bufsize);
+    free (buf);
+
+    /* In this mode an external main() in invoked which calls back into
+     * xx_childProcess() in this file, rather than directly
+     * via the statement below */
+    return resultPid;
+}
+
+static pid_t
+startChild(JNIEnv *env, jobject process, ChildStuff *c) {
+    switch (c->mode) {
+      case MODE_CLONE:
+        return cloneChild(c);
+      case MODE_VFORK:
+        return vforkChild(c);
+      case MODE_FORK:
+        return forkChild(c);
+      case MODE_SPAWN:
+        return spawnChild(env, process, c);
+    }
+}
+
 JNIEXPORT jint JNICALL
 Java_java_lang_UNIXProcess_forkAndExec(JNIEnv *env,
                                        jobject process,
+                                       jint mode,
                                        jbyteArray prog,
                                        jbyteArray argBlock, jint argc,
                                        jbyteArray envBlock, jint envc,
                                        jbyteArray dir,
                                        jintArray std_fds,
                                        jboolean redirectErrorStream)
 {
     int errnum;
     int resultPid = -1;
-    int in[2], out[2], err[2], fail[2];
+    int in[2], out[2], err[2], fail[2], childenv[2];
     jint *fds = NULL;
     const char *pprog = NULL;
     const char *pargBlock = NULL;
     const char *penvBlock = NULL;
     ChildStuff *c;
 
     in[0] = in[1] = out[0] = out[1] = err[0] = err[1] = fail[0] = fail[1] = -1;
+    childenv[0] = childenv[1] = -1;
 
     if ((c = NEW(ChildStuff, 1)) == NULL) return -1;
     c->argv = NULL;
     c->envv = NULL;
     c->pdir = NULL;
-#if START_CHILD_USE_CLONE
     c->clone_stack = NULL;
-#endif
 
     /* Convert prog + argBlock into a char ** argv.
      * Add one word room for expansion of argv for use by
      * execve_as_traditional_shell_script.
+     * This word also used when using spawn mode
      */
     assert(prog != NULL && argBlock != NULL);
     if ((pprog     = getBytes(env, prog))       == NULL) goto Catch;
     if ((pargBlock = getBytes(env, argBlock))   == NULL) goto Catch;
     if ((c->argv = NEW(const char *, argc + 3)) == NULL) goto Catch;
     c->argv[0] = pprog;
-    initVectorFromBlock(c->argv+1, pargBlock, argc);
+    c->argc = argc + 2;
+    xx_initVectorFromBlock(c->argv+1, pargBlock, argc);
 
     if (envBlock != NULL) {
         /* Convert envBlock into a char ** envv */
         if ((penvBlock = getBytes(env, envBlock))   == NULL) goto Catch;
         if ((c->envv = NEW(const char *, envc + 1)) == NULL) goto Catch;
-        initVectorFromBlock(c->envv, penvBlock, envc);
+        xx_initVectorFromBlock(c->envv, penvBlock, envc);
     }
 
     if (dir != NULL) {
         if ((c->pdir = getBytes(env, dir)) == NULL) goto Catch;
     }

@@ -876,10 +1057,11 @@
     if (fds == NULL) goto Catch;
 
     if ((fds[0] == -1 && pipe(in)  < 0) ||
         (fds[1] == -1 && pipe(out) < 0) ||
         (fds[2] == -1 && pipe(err) < 0) ||
+        (pipe(childenv) < 0) ||
         (pipe(fail) < 0)) {
         throwIOException(env, errno, "Bad file descriptor");
         goto Catch;
     }
     c->fds[0] = fds[0];

@@ -888,24 +1070,34 @@
 
     copyPipe(in,   c->in);
     copyPipe(out,  c->out);
     copyPipe(err,  c->err);
     copyPipe(fail, c->fail);
+    copyPipe(childenv, c->childenv);
 
     c->redirectErrorStream = redirectErrorStream;
+    c->mode = mode;
 
-    resultPid = startChild(c);
+    resultPid = startChild(env, process, c);
     assert(resultPid != 0);
 
     if (resultPid < 0) {
-        throwIOException(env, errno, START_CHILD_SYSTEM_CALL " failed");
+        switch (c->mode) {
+          case MODE_CLONE:
+            throwIOException(env, errno, "clone failed");
+          case MODE_VFORK:
+            throwIOException(env, errno, "vfork failed");
+          case MODE_FORK:
+            throwIOException(env, errno, "fork failed");
+          case MODE_SPAWN:
+            throwIOException(env, errno, "spawn failed");
+        }
         goto Catch;
     }
-
     restartableClose(fail[1]); fail[1] = -1; /* See: WhyCantJohnnyExec */
 
-    switch (readFully(fail[0], &errnum, sizeof(errnum))) {
+    switch (xx_readFully(fail[0], &errnum, sizeof(errnum))) {
     case 0: break; /* Exec succeeded */
     case sizeof(errnum):
         waitpid(resultPid, NULL, 0);
         throwIOException(env, errnum, "Exec failed");
         goto Catch;

@@ -917,22 +1109,22 @@
     fds[0] = (in [1] != -1) ? in [1] : -1;
     fds[1] = (out[0] != -1) ? out[0] : -1;
     fds[2] = (err[0] != -1) ? err[0] : -1;
 
  Finally:
-#if START_CHILD_USE_CLONE
     free(c->clone_stack);
-#endif
 
     /* Always clean up the child's side of the pipes */
     closeSafely(in [0]);
     closeSafely(out[1]);
     closeSafely(err[1]);
 
-    /* Always clean up fail descriptors */
+    /* Always clean up fail and childEnv descriptors */
     closeSafely(fail[0]);
     closeSafely(fail[1]);
+    closeSafely(childenv[0]);
+    closeSafely(childenv[1]);
 
     releaseBytes(env, prog,     pprog);
     releaseBytes(env, argBlock, pargBlock);
     releaseBytes(env, envBlock, penvBlock);
     releaseBytes(env, dir,      c->pdir);

@@ -946,13 +1138,13 @@
 
     return resultPid;
 
  Catch:
     /* Clean up the parent's side of the pipes in case of failure only */
-    closeSafely(in [1]);
-    closeSafely(out[0]);
-    closeSafely(err[0]);
+    closeSafely(in [1]); in[1] = -1;
+    closeSafely(out[0]); out[0] = -1;
+    closeSafely(err[0]); err[0] = -1;
     goto Finally;
 }
 
 JNIEXPORT void JNICALL
 Java_java_lang_UNIXProcess_destroyProcess(JNIEnv *env,