1 /*
   2  * Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #undef  _LARGEFILE64_SOURCE
  27 #define _LARGEFILE64_SOURCE 1
  28 
  29 #include "jdk_strerror.h"
  30 #include "jni.h"
  31 #include "jvm.h"
  32 #include "jvm_md.h"
  33 #include "jni_util.h"
  34 #include "io_util.h"
  35 
  36 /*
  37  * Platform-specific support for java.lang.Process
  38  */
  39 #include <assert.h>
  40 #include <stddef.h>
  41 #include <stdlib.h>
  42 #include <sys/types.h>
  43 #include <ctype.h>
  44 #include <sys/wait.h>
  45 #include <signal.h>
  46 #include <string.h>
  47 
  48 #if defined(__solaris__) || defined(_ALLBSD_SOURCE) || defined(_AIX)
  49 #include <spawn.h>
  50 #endif
  51 
  52 #include "childproc.h"
  53 
  54 /*
  55  * There are 4 possible strategies we might use to "fork":
  56  *
  57  * - fork(2).  Very portable and reliable but subject to
  58  *   failure due to overcommit (see the documentation on
  59  *   /proc/sys/vm/overcommit_memory in Linux proc(5)).
  60  *   This is the ancient problem of spurious failure whenever a large
  61  *   process starts a small subprocess.
  62  *
  63  * - vfork().  Using this is scary because all relevant man pages
  64  *   contain dire warnings, e.g. Linux vfork(2).  But at least it's
  65  *   documented in the glibc docs and is standardized by XPG4.
  66  *   http://www.opengroup.org/onlinepubs/000095399/functions/vfork.html
  67  *   On Linux, one might think that vfork() would be implemented using
  68  *   the clone system call with flag CLONE_VFORK, but in fact vfork is
  69  *   a separate system call (which is a good sign, suggesting that
  70  *   vfork will continue to be supported at least on Linux).
  71  *   Another good sign is that glibc implements posix_spawn using
  72  *   vfork whenever possible.  Note that we cannot use posix_spawn
  73  *   ourselves because there's no reliable way to close all inherited
  74  *   file descriptors.
  75  *
  76  * - clone() with flags CLONE_VM but not CLONE_THREAD.  clone() is
  77  *   Linux-specific, but this ought to work - at least the glibc
  78  *   sources contain code to handle different combinations of CLONE_VM
  79  *   and CLONE_THREAD.  However, when this was implemented, it
  80  *   appeared to fail on 32-bit i386 (but not 64-bit x86_64) Linux with
  81  *   the simple program
  82  *     Runtime.getRuntime().exec("/bin/true").waitFor();
  83  *   with:
  84  *     #  Internal Error (os_linux_x86.cpp:683), pid=19940, tid=2934639536
  85  *     #  Error: pthread_getattr_np failed with errno = 3 (ESRCH)
  86  *   We believe this is a glibc bug, reported here:
  87  *     http://sources.redhat.com/bugzilla/show_bug.cgi?id=10311
  88  *   but the glibc maintainers closed it as WONTFIX.
  89  *
  90  * - posix_spawn(). While posix_spawn() is a fairly elaborate and
  91  *   complicated system call, it can't quite do everything that the old
  92  *   fork()/exec() combination can do, so the only feasible way to do
  93  *   this, is to use posix_spawn to launch a new helper executable
  94  *   "jprochelper", which in turn execs the target (after cleaning
  95  *   up file-descriptors etc.) The end result is the same as before,
  96  *   a child process linked to the parent in the same way, but it
  97  *   avoids the problem of duplicating the parent (VM) process
  98  *   address space temporarily, before launching the target command.
  99  *
 100  * Based on the above analysis, we are currently using vfork() on
 101  * Linux and posix_spawn() on other Unix systems.
 102  */
 103 
 104 
 105 static void
 106 setSIGCHLDHandler(JNIEnv *env)
 107 {
 108     /* There is a subtle difference between having the signal handler
 109      * for SIGCHLD be SIG_DFL and SIG_IGN.  We cannot obtain process
 110      * termination information for child processes if the signal
 111      * handler is SIG_IGN.  It must be SIG_DFL.
 112      *
 113      * We used to set the SIGCHLD handler only on Linux, but it's
 114      * safest to set it unconditionally.
 115      *
 116      * Consider what happens if java's parent process sets the SIGCHLD
 117      * handler to SIG_IGN.  Normally signal handlers are inherited by
 118      * children, but SIGCHLD is a controversial case.  Solaris appears
 119      * to always reset it to SIG_DFL, but this behavior may be
 120      * non-standard-compliant, and we shouldn't rely on it.
 121      *
 122      * References:
 123      * http://www.opengroup.org/onlinepubs/7908799/xsh/exec.html
 124      * http://www.pasc.org/interps/unofficial/db/p1003.1/pasc-1003.1-132.html
 125      */
 126     struct sigaction sa;
 127     sa.sa_handler = SIG_DFL;
 128     sigemptyset(&sa.sa_mask);
 129     sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
 130     if (sigaction(SIGCHLD, &sa, NULL) < 0)
 131         JNU_ThrowInternalError(env, "Can't set SIGCHLD handler");
 132 }
 133 
 134 static void*
 135 xmalloc(JNIEnv *env, size_t size)
 136 {
 137     void *p = malloc(size);
 138     if (p == NULL)
 139         JNU_ThrowOutOfMemoryError(env, NULL);
 140     return p;
 141 }
 142 
 143 #define NEW(type, n) ((type *) xmalloc(env, (n) * sizeof(type)))
 144 
 145 /**
 146  * If PATH is not defined, the OS provides some default value.
 147  * Unfortunately, there's no portable way to get this value.
 148  * Fortunately, it's only needed if the child has PATH while we do not.
 149  */
 150 static const char*
 151 defaultPath(void)
 152 {
 153 #ifdef __solaris__
 154     /* These really are the Solaris defaults! */
 155     return (geteuid() == 0 || getuid() == 0) ?
 156         "/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin:/usr/sbin" :
 157         "/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin:";
 158 #else
 159     return ":/bin:/usr/bin";    /* glibc */
 160 #endif
 161 }
 162 
 163 static const char*
 164 effectivePath(void)
 165 {
 166     const char *s = getenv("PATH");
 167     return (s != NULL) ? s : defaultPath();
 168 }
 169 
 170 static int
 171 countOccurrences(const char *s, char c)
 172 {
 173     int count;
 174     for (count = 0; *s != '\0'; s++)
 175         count += (*s == c);
 176     return count;
 177 }
 178 
 179 static const char * const *
 180 effectivePathv(JNIEnv *env)
 181 {
 182     char *p;
 183     int i;
 184     const char *path = effectivePath();
 185     int count = countOccurrences(path, ':') + 1;
 186     size_t pathvsize = sizeof(const char *) * (count+1);
 187     size_t pathsize = strlen(path) + 1;
 188     const char **pathv = (const char **) xmalloc(env, pathvsize + pathsize);
 189 
 190     if (pathv == NULL)
 191         return NULL;
 192     p = (char *) pathv + pathvsize;
 193     memcpy(p, path, pathsize);
 194     /* split PATH by replacing ':' with NULs; empty components => "." */
 195     for (i = 0; i < count; i++) {
 196         char *q = p + strcspn(p, ":");
 197         pathv[i] = (p == q) ? "." : p;
 198         *q = '\0';
 199         p = q + 1;
 200     }
 201     pathv[count] = NULL;
 202     return pathv;
 203 }
 204 
 205 JNIEXPORT void JNICALL
 206 Java_java_lang_ProcessImpl_init(JNIEnv *env, jclass clazz)
 207 {
 208     parentPathv = effectivePathv(env);
 209     CHECK_NULL(parentPathv);
 210     setSIGCHLDHandler(env);
 211 }
 212 
 213 
 214 #ifndef WIFEXITED
 215 #define WIFEXITED(status) (((status)&0xFF) == 0)
 216 #endif
 217 
 218 #ifndef WEXITSTATUS
 219 #define WEXITSTATUS(status) (((status)>>8)&0xFF)
 220 #endif
 221 
 222 #ifndef WIFSIGNALED
 223 #define WIFSIGNALED(status) (((status)&0xFF) > 0 && ((status)&0xFF00) == 0)
 224 #endif
 225 
 226 #ifndef WTERMSIG
 227 #define WTERMSIG(status) ((status)&0x7F)
 228 #endif
 229 
 230 static const char *
 231 getBytes(JNIEnv *env, jbyteArray arr)
 232 {
 233     return arr == NULL ? NULL :
 234         (const char*) (*env)->GetByteArrayElements(env, arr, NULL);
 235 }
 236 
 237 static void
 238 releaseBytes(JNIEnv *env, jbyteArray arr, const char* parr)
 239 {
 240     if (parr != NULL)
 241         (*env)->ReleaseByteArrayElements(env, arr, (jbyte*) parr, JNI_ABORT);
 242 }
 243 
 244 #define IOE_FORMAT "error=%d, %s"
 245 
 246 static void
 247 throwIOException(JNIEnv *env, int errnum, const char *defaultDetail)
 248 {
 249     const char *detail = defaultDetail;
 250     char *errmsg;
 251     size_t fmtsize;
 252     char buf[1024];
 253     jstring s;
 254 
 255     if (errnum != 0) {
 256         jdk_strerror(errnum, buf, (size_t) 1024);
 257         if (strncmp(buf, "Unknown error", 13) != 0)
 258             detail = buf;
 259     }
 260     /* ASCII Decimal representation uses 2.4 times as many bits as binary. */
 261     fmtsize = sizeof(IOE_FORMAT) + strlen(detail) + 3 * sizeof(errnum);
 262     errmsg = NEW(char, fmtsize);
 263     if (errmsg == NULL)
 264         return;
 265 
 266     snprintf(errmsg, fmtsize, IOE_FORMAT, errnum, detail);
 267     s = JNU_NewStringPlatform(env, errmsg);
 268     if (s != NULL) {
 269         jobject x = JNU_NewObjectByName(env, "java/io/IOException",
 270                                         "(Ljava/lang/String;)V", s);
 271         if (x != NULL)
 272             (*env)->Throw(env, x);
 273     }
 274     free(errmsg);
 275 }
 276 
 277 #ifdef DEBUG_PROCESS
 278 /* Debugging process code is difficult; where to write debug output? */
 279 static void
 280 debugPrint(char *format, ...)
 281 {
 282     FILE *tty = fopen("/dev/tty", "w");
 283     va_list ap;
 284     va_start(ap, format);
 285     vfprintf(tty, format, ap);
 286     va_end(ap);
 287     fclose(tty);
 288 }
 289 #endif /* DEBUG_PROCESS */
 290 
 291 static void
 292 copyPipe(int from[2], int to[2])
 293 {
 294     to[0] = from[0];
 295     to[1] = from[1];
 296 }
 297 
 298 /* arg is an array of pointers to 0 terminated strings. array is terminated
 299  * by a null element.
 300  *
 301  * *nelems and *nbytes receive the number of elements of array (incl 0)
 302  * and total number of bytes (incl. 0)
 303  * Note. An empty array will have one null element
 304  * But if arg is null, then *nelems set to 0, and *nbytes to 0
 305  */
 306 static void arraysize(const char * const *arg, int *nelems, int *nbytes)
 307 {
 308     int i, bytes, count;
 309     const char * const *a = arg;
 310     char *p;
 311     int *q;
 312     if (arg == 0) {
 313         *nelems = 0;
 314         *nbytes = 0;
 315         return;
 316     }
 317     /* count the array elements and number of bytes */
 318     for (count=0, bytes=0; *a != 0; count++, a++) {
 319         bytes += strlen(*a)+1;
 320     }
 321     *nbytes = bytes;
 322     *nelems = count+1;
 323 }
 324 
 325 /* copy the strings from arg[] into buf, starting at given offset
 326  * return new offset to next free byte
 327  */
 328 static int copystrings(char *buf, int offset, const char * const *arg) {
 329     char *p;
 330     const char * const *a;
 331     int count=0;
 332 
 333     if (arg == 0) {
 334         return offset;
 335     }
 336     for (p=buf+offset, a=arg; *a != 0; a++) {
 337         int len = strlen(*a) +1;
 338         memcpy(p, *a, len);
 339         p += len;
 340         count += len;
 341     }
 342     return offset+count;
 343 }
 344 
 345 /**
 346  * We are unusually paranoid; use of vfork is
 347  * especially likely to tickle gcc/glibc bugs.
 348  */
 349 #ifdef __attribute_noinline__  /* See: sys/cdefs.h */
 350 __attribute_noinline__
 351 #endif
 352 
 353 static pid_t
 354 vforkChild(ChildStuff *c) {
 355     volatile pid_t resultPid;
 356 
 357     /*
 358      * We separate the call to vfork into a separate function to make
 359      * very sure to keep stack of child from corrupting stack of parent,
 360      * as suggested by the scary gcc warning:
 361      *  warning: variable 'foo' might be clobbered by 'longjmp' or 'vfork'
 362      */
 363     resultPid = vfork();
 364 
 365     if (resultPid == 0) {
 366         childProcess(c);
 367     }
 368     assert(resultPid != 0);  /* childProcess never returns */
 369     return resultPid;
 370 }
 371 
 372 static pid_t
 373 forkChild(ChildStuff *c) {
 374     pid_t resultPid;
 375 
 376     /*
 377      * From Solaris fork(2): In Solaris 10, a call to fork() is
 378      * identical to a call to fork1(); only the calling thread is
 379      * replicated in the child process. This is the POSIX-specified
 380      * behavior for fork().
 381      */
 382     resultPid = fork();
 383 
 384     if (resultPid == 0) {
 385         childProcess(c);
 386     }
 387     assert(resultPid != 0);  /* childProcess never returns */
 388     return resultPid;
 389 }
 390 
 391 #if defined(__solaris__) || defined(_ALLBSD_SOURCE) || defined(_AIX)
 392 static pid_t
 393 spawnChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath) {
 394     pid_t resultPid;
 395     jboolean isCopy;
 396     int i, offset, rval, bufsize, magic;
 397     char *buf, buf1[16];
 398     char *hlpargs[2];
 399     SpawnInfo sp;
 400 
 401     /* need to tell helper which fd is for receiving the childstuff
 402      * and which fd to send response back on
 403      */
 404     snprintf(buf1, sizeof(buf1), "%d:%d", c->childenv[0], c->fail[1]);
 405     /* put the fd string as argument to the helper cmd */
 406     hlpargs[0] = buf1;
 407     hlpargs[1] = 0;
 408 
 409     /* Following items are sent down the pipe to the helper
 410      * after it is spawned.
 411      * All strings are null terminated. All arrays of strings
 412      * have an empty string for termination.
 413      * - the ChildStuff struct
 414      * - the SpawnInfo struct
 415      * - the argv strings array
 416      * - the envv strings array
 417      * - the home directory string
 418      * - the parentPath string
 419      * - the parentPathv array
 420      */
 421     /* First calculate the sizes */
 422     arraysize(c->argv, &sp.nargv, &sp.argvBytes);
 423     bufsize = sp.argvBytes;
 424     arraysize(c->envv, &sp.nenvv, &sp.envvBytes);
 425     bufsize += sp.envvBytes;
 426     sp.dirlen = c->pdir == 0 ? 0 : strlen(c->pdir)+1;
 427     bufsize += sp.dirlen;
 428     arraysize(parentPathv, &sp.nparentPathv, &sp.parentPathvBytes);
 429     bufsize += sp.parentPathvBytes;
 430     /* We need to clear FD_CLOEXEC if set in the fds[].
 431      * Files are created FD_CLOEXEC in Java.
 432      * Otherwise, they will be closed when the target gets exec'd */
 433     for (i=0; i<3; i++) {
 434         if (c->fds[i] != -1) {
 435             int flags = fcntl(c->fds[i], F_GETFD);
 436             if (flags & FD_CLOEXEC) {
 437                 fcntl(c->fds[i], F_SETFD, flags & (~1));
 438             }
 439         }
 440     }
 441 
 442     rval = posix_spawn(&resultPid, helperpath, 0, 0, (char * const *) hlpargs, environ);
 443 
 444     if (rval != 0) {
 445         return -1;
 446     }
 447 
 448     /* now the lengths are known, copy the data */
 449     buf = NEW(char, bufsize);
 450     if (buf == 0) {
 451         return -1;
 452     }
 453     offset = copystrings(buf, 0, &c->argv[0]);
 454     offset = copystrings(buf, offset, &c->envv[0]);
 455     memcpy(buf+offset, c->pdir, sp.dirlen);
 456     offset += sp.dirlen;
 457     offset = copystrings(buf, offset, parentPathv);
 458     assert(offset == bufsize);
 459 
 460     magic = magicNumber();
 461 
 462     /* write the two structs and the data buffer */
 463     write(c->childenv[1], (char *)&magic, sizeof(magic)); // magic number first
 464     write(c->childenv[1], (char *)c, sizeof(*c));
 465     write(c->childenv[1], (char *)&sp, sizeof(sp));
 466     write(c->childenv[1], buf, bufsize);
 467     free(buf);
 468 
 469     /* In this mode an external main() in invoked which calls back into
 470      * childProcess() in this file, rather than directly
 471      * via the statement below */
 472     return resultPid;
 473 }
 474 #endif
 475 
 476 /*
 477  * Start a child process running function childProcess.
 478  * This function only returns in the parent.
 479  */
 480 static pid_t
 481 startChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath) {
 482     switch (c->mode) {
 483       case MODE_VFORK:
 484         return vforkChild(c);
 485       case MODE_FORK:
 486         return forkChild(c);
 487 #if defined(__solaris__) || defined(_ALLBSD_SOURCE) || defined(_AIX)
 488       case MODE_POSIX_SPAWN:
 489         return spawnChild(env, process, c, helperpath);
 490 #endif
 491       default:
 492         return -1;
 493     }
 494 }
 495 
 496 JNIEXPORT jint JNICALL
 497 Java_java_lang_ProcessImpl_forkAndExec(JNIEnv *env,
 498                                        jobject process,
 499                                        jint mode,
 500                                        jbyteArray helperpath,
 501                                        jbyteArray prog,
 502                                        jbyteArray argBlock, jint argc,
 503                                        jbyteArray envBlock, jint envc,
 504                                        jbyteArray dir,
 505                                        jintArray std_fds,
 506                                        jboolean redirectErrorStream)
 507 {
 508     int errnum;
 509     int resultPid = -1;
 510     int in[2], out[2], err[2], fail[2], childenv[2];
 511     jint *fds = NULL;
 512     const char *phelperpath = NULL;
 513     const char *pprog = NULL;
 514     const char *pargBlock = NULL;
 515     const char *penvBlock = NULL;
 516     ChildStuff *c;
 517 
 518     in[0] = in[1] = out[0] = out[1] = err[0] = err[1] = fail[0] = fail[1] = -1;
 519     childenv[0] = childenv[1] = -1;
 520 
 521     if ((c = NEW(ChildStuff, 1)) == NULL) return -1;
 522     c->argv = NULL;
 523     c->envv = NULL;
 524     c->pdir = NULL;
 525 
 526     /* Convert prog + argBlock into a char ** argv.
 527      * Add one word room for expansion of argv for use by
 528      * execve_as_traditional_shell_script.
 529      * This word is also used when using posix_spawn mode
 530      */
 531     assert(prog != NULL && argBlock != NULL);
 532     if ((phelperpath = getBytes(env, helperpath))   == NULL) goto Catch;
 533     if ((pprog       = getBytes(env, prog))         == NULL) goto Catch;
 534     if ((pargBlock   = getBytes(env, argBlock))     == NULL) goto Catch;
 535     if ((c->argv     = NEW(const char *, argc + 3)) == NULL) goto Catch;
 536     c->argv[0] = pprog;
 537     c->argc = argc + 2;
 538     initVectorFromBlock(c->argv+1, pargBlock, argc);
 539 
 540     if (envBlock != NULL) {
 541         /* Convert envBlock into a char ** envv */
 542         if ((penvBlock = getBytes(env, envBlock))   == NULL) goto Catch;
 543         if ((c->envv = NEW(const char *, envc + 1)) == NULL) goto Catch;
 544         initVectorFromBlock(c->envv, penvBlock, envc);
 545     }
 546 
 547     if (dir != NULL) {
 548         if ((c->pdir = getBytes(env, dir)) == NULL) goto Catch;
 549     }
 550 
 551     assert(std_fds != NULL);
 552     fds = (*env)->GetIntArrayElements(env, std_fds, NULL);
 553     if (fds == NULL) goto Catch;
 554 
 555     if ((fds[0] == -1 && pipe(in)  < 0) ||
 556         (fds[1] == -1 && pipe(out) < 0) ||
 557         (fds[2] == -1 && pipe(err) < 0) ||
 558         (pipe(childenv) < 0) ||
 559         (pipe(fail) < 0)) {
 560         throwIOException(env, errno, "Bad file descriptor");
 561         goto Catch;
 562     }
 563     c->fds[0] = fds[0];
 564     c->fds[1] = fds[1];
 565     c->fds[2] = fds[2];
 566 
 567     copyPipe(in,   c->in);
 568     copyPipe(out,  c->out);
 569     copyPipe(err,  c->err);
 570     copyPipe(fail, c->fail);
 571     copyPipe(childenv, c->childenv);
 572 
 573     c->redirectErrorStream = redirectErrorStream;
 574     c->mode = mode;
 575 
 576     resultPid = startChild(env, process, c, phelperpath);
 577     assert(resultPid != 0);
 578 
 579     if (resultPid < 0) {
 580         switch (c->mode) {
 581           case MODE_VFORK:
 582             throwIOException(env, errno, "vfork failed");
 583             break;
 584           case MODE_FORK:
 585             throwIOException(env, errno, "fork failed");
 586             break;
 587           case MODE_POSIX_SPAWN:
 588             throwIOException(env, errno, "posix_spawn failed");
 589             break;
 590         }
 591         goto Catch;
 592     }
 593     close(fail[1]); fail[1] = -1; /* See: WhyCantJohnnyExec  (childproc.c)  */
 594 
 595     switch (readFully(fail[0], &errnum, sizeof(errnum))) {
 596     case 0: break; /* Exec succeeded */
 597     case sizeof(errnum):
 598         waitpid(resultPid, NULL, 0);
 599         throwIOException(env, errnum, "Exec failed");
 600         goto Catch;
 601     default:
 602         throwIOException(env, errno, "Read failed");
 603         goto Catch;
 604     }
 605 
 606     fds[0] = (in [1] != -1) ? in [1] : -1;
 607     fds[1] = (out[0] != -1) ? out[0] : -1;
 608     fds[2] = (err[0] != -1) ? err[0] : -1;
 609 
 610  Finally:
 611     /* Always clean up the child's side of the pipes */
 612     closeSafely(in [0]);
 613     closeSafely(out[1]);
 614     closeSafely(err[1]);
 615 
 616     /* Always clean up fail and childEnv descriptors */
 617     closeSafely(fail[0]);
 618     closeSafely(fail[1]);
 619     closeSafely(childenv[0]);
 620     closeSafely(childenv[1]);
 621 
 622     releaseBytes(env, helperpath, phelperpath);
 623     releaseBytes(env, prog,       pprog);
 624     releaseBytes(env, argBlock,   pargBlock);
 625     releaseBytes(env, envBlock,   penvBlock);
 626     releaseBytes(env, dir,        c->pdir);
 627 
 628     free(c->argv);
 629     free(c->envv);
 630     free(c);
 631 
 632     if (fds != NULL)
 633         (*env)->ReleaseIntArrayElements(env, std_fds, fds, 0);
 634 
 635     return resultPid;
 636 
 637  Catch:
 638     /* Clean up the parent's side of the pipes in case of failure only */
 639     closeSafely(in [1]); in[1] = -1;
 640     closeSafely(out[0]); out[0] = -1;
 641     closeSafely(err[0]); err[0] = -1;
 642     goto Finally;
 643 }
 644