jdk/src/solaris/native/java/lang/UNIXProcess_md.c
author michaelm
Tue, 06 Mar 2012 20:34:38 +0000
changeset 12047 320a714614e9
parent 11691 ff8aec7600fe
child 13149 27d52f97a5cc
permissions -rw-r--r--
7113349: Initial changeset for Macosx port to jdk Reviewed-by: jjh, alanb, dholmes, anthony, ohrstrom, ksrini, jrose, weijun, smarks Contributed-by: Alan Bateman <alan.bateman@oracle.com>, Alexander Potochkin <alexander.potochkin@oracle.com>, Alexander Zuev <alexander.zuev@oracle.com>, Andrew Brygin <andrew.brygin@oracle.com>, Artem Ananiev <artem.ananiev@oracle.com>, Alex Strange <astrange@apple.com>, Bino George <bino@apple.com>, Christine Lu <christine.lu@oracle.com>, David Katleman <david.katleman@oracle.com>, David Durrence <david_durrence@apple.com>, Dmitry Cherepanov <dmitry.cherepanov@oracle.com>, Greg Lewis <glewis@eyesbeyond.com>, Kevin Miller <kevin_m_miller@apple.com>, Kurt Miller <kurt@intricatesoftware.com>, Landon Fuller <landonf@plausiblelabs.com>, Leonid Romanov <leonid.romanov@oracle.com>, Loefty Walkowiak <loefty@apple.com>, Mark Reinhold <mark.reinhold@oracle.com>, Naoto Sato <naoto.sato@oracle.com>, Philip Race <philip.race@oracle.com>, Roger Hoover <rhoover@apple.com>, Scott Kovatch <scott.kovatch@oracle.com>, Sergey ByloKhov <sergey.bylokhov@oracle.com>, Mike Swingler <swingler@apple.com>, Tomas Hurka <tomas.hurka@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3431
diff changeset
     2
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3431
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3431
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3431
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3431
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3431
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#undef  _LARGEFILE64_SOURCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#define _LARGEFILE64_SOURCE 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jvm_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "io_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Platform-specific support for java.lang.Process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <stddef.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include <ctype.h>
11691
ff8aec7600fe 7133301: (process) UNIXProcess_md.c should include sys/wait.h rather than wait.h
ngmr
parents: 5506
diff changeset
    43
#include <sys/wait.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include <signal.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include <dirent.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#include <limits.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
    52
#ifdef __APPLE__
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
    53
#include <crt_externs.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
    54
#define environ (*_NSGetEnviron())
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
    55
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
    56
2948
7f4d7737d38f 6853336: (process) disable or remove clone-exec feature (6850720)
tbell
parents: 2946
diff changeset
    57
/*
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    58
 * There are 3 possible strategies we might use to "fork":
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    59
 *
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    60
 * - fork(2).  Very portable and reliable but subject to
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    61
 *   failure due to overcommit (see the documentation on
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    62
 *   /proc/sys/vm/overcommit_memory in Linux proc(5)).
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    63
 *   This is the ancient problem of spurious failure whenever a large
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    64
 *   process starts a small subprocess.
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    65
 *
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    66
 * - vfork().  Using this is scary because all relevant man pages
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    67
 *   contain dire warnings, e.g. Linux vfork(2).  But at least it's
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    68
 *   documented in the glibc docs and is standardized by XPG4.
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    69
 *   http://www.opengroup.org/onlinepubs/000095399/functions/vfork.html
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    70
 *   On Linux, one might think that vfork() would be implemented using
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    71
 *   the clone system call with flag CLONE_VFORK, but in fact vfork is
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    72
 *   a separate system call (which is a good sign, suggesting that
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    73
 *   vfork will continue to be supported at least on Linux).
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    74
 *   Another good sign is that glibc implements posix_spawn using
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    75
 *   vfork whenever possible.  Note that we cannot use posix_spawn
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    76
 *   ourselves because there's no reliable way to close all inherited
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    77
 *   file descriptors.
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    78
 *
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    79
 * - clone() with flags CLONE_VM but not CLONE_THREAD.  clone() is
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    80
 *   Linux-specific, but this ought to work - at least the glibc
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    81
 *   sources contain code to handle different combinations of CLONE_VM
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    82
 *   and CLONE_THREAD.  However, when this was implemented, it
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    83
 *   appeared to fail on 32-bit i386 (but not 64-bit x86_64) Linux with
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    84
 *   the simple program
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    85
 *     Runtime.getRuntime().exec("/bin/true").waitFor();
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    86
 *   with:
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    87
 *     #  Internal Error (os_linux_x86.cpp:683), pid=19940, tid=2934639536
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    88
 *     #  Error: pthread_getattr_np failed with errno = 3 (ESRCH)
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    89
 *   We believe this is a glibc bug, reported here:
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    90
 *     http://sources.redhat.com/bugzilla/show_bug.cgi?id=10311
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    91
 *   but the glibc maintainers closed it as WONTFIX.
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    92
 *
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    93
 * Based on the above analysis, we are currently using vfork() on
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    94
 * Linux and fork() on other Unix systems, but the code to use clone()
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    95
 * remains.
2948
7f4d7737d38f 6853336: (process) disable or remove clone-exec feature (6850720)
tbell
parents: 2946
diff changeset
    96
 */
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    97
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    98
#define START_CHILD_USE_CLONE 0  /* clone() currently disabled; see above. */
2948
7f4d7737d38f 6853336: (process) disable or remove clone-exec feature (6850720)
tbell
parents: 2946
diff changeset
    99
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   100
#ifndef START_CHILD_USE_CLONE
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   101
  #ifdef __linux__
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   102
    #define START_CHILD_USE_CLONE 1
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   103
  #else
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   104
    #define START_CHILD_USE_CLONE 0
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   105
  #endif
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   106
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   107
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   108
/* By default, use vfork() on Linux. */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   109
#ifndef START_CHILD_USE_VFORK
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   110
  #ifdef __linux__
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   111
    #define START_CHILD_USE_VFORK 1
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   112
  #else
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   113
    #define START_CHILD_USE_VFORK 0
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   114
  #endif
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   115
#endif
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   116
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   117
#if START_CHILD_USE_CLONE
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   118
#include <sched.h>
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   119
#define START_CHILD_SYSTEM_CALL "clone"
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   120
#elif START_CHILD_USE_VFORK
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   121
#define START_CHILD_SYSTEM_CALL "vfork"
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   122
#else
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   123
#define START_CHILD_SYSTEM_CALL "fork"
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   124
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   125
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
#ifndef STDIN_FILENO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
#define STDIN_FILENO 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
#ifndef STDOUT_FILENO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
#define STDOUT_FILENO 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
#ifndef STDERR_FILENO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
#define STDERR_FILENO 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
#ifndef SA_NOCLDSTOP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
#define SA_NOCLDSTOP 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
#ifndef SA_RESTART
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
#define SA_RESTART 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
#define FAIL_FILENO (STDERR_FILENO + 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   148
/* TODO: Refactor. */
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   149
#define RESTARTABLE(_cmd, _result) do { \
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   150
  do { \
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   151
    _result = _cmd; \
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   152
  } while((_result == -1) && (errno == EINTR)); \
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   153
} while(0)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   154
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   155
/* This is one of the rare times it's more portable to declare an
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   156
 * external symbol explicitly, rather than via a system header.
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   157
 * The declaration is standardized as part of UNIX98, but there is
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   158
 * no standard (not even de-facto) header file where the
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   159
 * declaration is to be found.  See:
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   160
 * http://www.opengroup.org/onlinepubs/009695399/functions/environ.html
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   161
 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_02.html
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   162
 *
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   163
 * "All identifiers in this volume of IEEE Std 1003.1-2001, except
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   164
 * environ, are defined in at least one of the headers" (!)
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   165
 */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   166
extern char **environ;
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   167
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   168
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
setSIGCHLDHandler(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /* There is a subtle difference between having the signal handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * for SIGCHLD be SIG_DFL and SIG_IGN.  We cannot obtain process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * termination information for child processes if the signal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * handler is SIG_IGN.  It must be SIG_DFL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * We used to set the SIGCHLD handler only on Linux, but it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * safest to set it unconditionally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Consider what happens if java's parent process sets the SIGCHLD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * handler to SIG_IGN.  Normally signal handlers are inherited by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * children, but SIGCHLD is a controversial case.  Solaris appears
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * to always reset it to SIG_DFL, but this behavior may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * non-standard-compliant, and we shouldn't rely on it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * References:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * http://www.opengroup.org/onlinepubs/7908799/xsh/exec.html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * http://www.pasc.org/interps/unofficial/db/p1003.1/pasc-1003.1-132.html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    struct sigaction sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    sa.sa_handler = SIG_DFL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    sigemptyset(&sa.sa_mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    if (sigaction(SIGCHLD, &sa, NULL) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        JNU_ThrowInternalError(env, "Can't set SIGCHLD handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
static void*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
xmalloc(JNIEnv *env, size_t size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    void *p = malloc(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    if (p == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        JNU_ThrowOutOfMemoryError(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
#define NEW(type, n) ((type *) xmalloc(env, (n) * sizeof(type)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * If PATH is not defined, the OS provides some default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * Unfortunately, there's no portable way to get this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * Fortunately, it's only needed if the child has PATH while we do not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
static const char*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
defaultPath(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /* These really are the Solaris defaults! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    return (geteuid() == 0 || getuid() == 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        "/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin:/usr/sbin" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        "/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    return ":/bin:/usr/bin";    /* glibc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
static const char*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
effectivePath(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    const char *s = getenv("PATH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    return (s != NULL) ? s : defaultPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
countOccurrences(const char *s, char c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    for (count = 0; *s != '\0'; s++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        count += (*s == c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
static const char * const *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
splitPath(JNIEnv *env, const char *path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    const char *p, *q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    char **pathv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    int count = countOccurrences(path, ':') + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    pathv = NEW(char*, count+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    pathv[count] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    for (p = path, i = 0; i < count; i++, p = q + 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        for (q = p; (*q != ':') && (*q != '\0'); q++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (q == p)             /* empty PATH component => "." */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            pathv[i] = "./";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            int addSlash = ((*(q - 1)) != '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            pathv[i] = NEW(char, q - p + addSlash + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            memcpy(pathv[i], p, q - p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (addSlash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                pathv[i][q - p] = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            pathv[i][q - p + addSlash] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    return (const char * const *) pathv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * Cached value of JVM's effective PATH.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 * (We don't support putenv("PATH=...") in native code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
static const char *parentPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 * Split, canonicalized version of parentPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
static const char * const *parentPathv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
static jfieldID field_exitcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
Java_java_lang_UNIXProcess_initIDs(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    field_exitcode = (*env)->GetFieldID(env, clazz, "exitcode", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    parentPath  = effectivePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    parentPathv = splitPath(env, parentPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    setSIGCHLDHandler(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
#ifndef WIFEXITED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
#define WIFEXITED(status) (((status)&0xFF) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
#ifndef WEXITSTATUS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
#define WEXITSTATUS(status) (((status)>>8)&0xFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
#ifndef WIFSIGNALED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
#define WIFSIGNALED(status) (((status)&0xFF) > 0 && ((status)&0xFF00) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
#ifndef WTERMSIG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
#define WTERMSIG(status) ((status)&0x7F)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
/* Block until a child process exits and return its exit code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
   Note, can only be called once for any given pid. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
Java_java_lang_UNIXProcess_waitForProcessExit(JNIEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                              jobject junk,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                              jint pid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /* We used to use waitid() on Solaris, waitpid() on Linux, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * waitpid() is more standard, so use it on all POSIX platforms. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    int status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /* Wait for the child process to exit.  This returns immediately if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
       the child has already exited. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    while (waitpid(pid, &status, 0) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        switch (errno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        case ECHILD: return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        case EINTR: break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        default: return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    if (WIFEXITED(status)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * The child exited normally; get its exit code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return WEXITSTATUS(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    } else if (WIFSIGNALED(status)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        /* The child exited because of a signal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * The best value to return is 0x80 + signal number,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         * because that is what all Unix shells do, and because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         * it allows callers to distinguish between process exit and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         * process death by signal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         * Unfortunately, the historical behavior on Solaris is to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         * the signal number, and we preserve this for compatibility. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return WTERMSIG(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return 0x80 + WTERMSIG(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         * Unknown exit code; pass it through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   357
static ssize_t
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   358
restartableWrite(int fd, const void *buf, size_t count)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   359
{
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   360
    ssize_t result;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   361
    RESTARTABLE(write(fd, buf, count), result);
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   362
    return result;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   363
}
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   364
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   365
static int
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   366
restartableDup2(int fd_from, int fd_to)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   367
{
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   368
    int err;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   369
    RESTARTABLE(dup2(fd_from, fd_to), err);
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   370
    return err;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   371
}
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   372
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   373
static int
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   374
restartableClose(int fd)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   375
{
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   376
    int err;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   377
    RESTARTABLE(close(fd), err);
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   378
    return err;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   379
}
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   380
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   381
static int
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   382
closeSafely(int fd)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   383
{
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   384
    return (fd == -1) ? 0 : restartableClose(fd);
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   385
}
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   386
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
static int
1936
66e7f98f4dde 6798822: (process) Non-portable use of isdigit in src/solaris/native/java/lang/UNIXProcess_md.c
martin
parents: 50
diff changeset
   388
isAsciiDigit(char c)
66e7f98f4dde 6798822: (process) Non-portable use of isdigit in src/solaris/native/java/lang/UNIXProcess_md.c
martin
parents: 50
diff changeset
   389
{
66e7f98f4dde 6798822: (process) Non-portable use of isdigit in src/solaris/native/java/lang/UNIXProcess_md.c
martin
parents: 50
diff changeset
   390
  return c >= '0' && c <= '9';
66e7f98f4dde 6798822: (process) Non-portable use of isdigit in src/solaris/native/java/lang/UNIXProcess_md.c
martin
parents: 50
diff changeset
   391
}
66e7f98f4dde 6798822: (process) Non-portable use of isdigit in src/solaris/native/java/lang/UNIXProcess_md.c
martin
parents: 50
diff changeset
   392
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
   393
#ifdef _ALLBSD_SOURCE
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
   394
#define FD_DIR "/dev/fd"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
   395
#define dirent64 dirent
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
   396
#define readdir64 readdir
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
   397
#else
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
   398
#define FD_DIR "/proc/self/fd"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
   399
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
   400
1936
66e7f98f4dde 6798822: (process) Non-portable use of isdigit in src/solaris/native/java/lang/UNIXProcess_md.c
martin
parents: 50
diff changeset
   401
static int
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
closeDescriptors(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    DIR *dp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    struct dirent64 *dirp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    int from_fd = FAIL_FILENO + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /* We're trying to close all file descriptors, but opendir() might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * itself be implemented using a file descriptor, and we certainly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * don't want to close that while it's in use.  We assume that if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * opendir() is implemented using a file descriptor, then it uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * the lowest numbered file descriptor, just like open().  So we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * close a couple explicitly.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   415
    restartableClose(from_fd);          /* for possible use by opendir() */
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   416
    restartableClose(from_fd + 1);      /* another one for good luck */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11691
diff changeset
   418
    if ((dp = opendir(FD_DIR)) == NULL)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /* We use readdir64 instead of readdir to work around Solaris bug
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * 6395699: /proc/self/fd fails to report file descriptors >= 1024 on Solaris 9
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    while ((dirp = readdir64(dp)) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        int fd;
1936
66e7f98f4dde 6798822: (process) Non-portable use of isdigit in src/solaris/native/java/lang/UNIXProcess_md.c
martin
parents: 50
diff changeset
   426
        if (isAsciiDigit(dirp->d_name[0]) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            (fd = strtol(dirp->d_name, NULL, 10)) >= from_fd + 2)
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   428
            restartableClose(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    closedir(dp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   436
static int
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
moveDescriptor(int fd_from, int fd_to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    if (fd_from != fd_to) {
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   440
        if ((restartableDup2(fd_from, fd_to) == -1) ||
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   441
            (restartableClose(fd_from) == -1))
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   442
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   444
    return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
static const char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
getBytes(JNIEnv *env, jbyteArray arr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    return arr == NULL ? NULL :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        (const char*) (*env)->GetByteArrayElements(env, arr, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
releaseBytes(JNIEnv *env, jbyteArray arr, const char* parr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    if (parr != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        (*env)->ReleaseByteArrayElements(env, arr, (jbyte*) parr, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
initVectorFromBlock(const char**vector, const char* block, int count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    const char *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    for (i = 0, p = block; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        /* Invariant: p always points to the start of a C string. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        vector[i] = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        while (*(p++));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    vector[count] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
throwIOException(JNIEnv *env, int errnum, const char *defaultDetail)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    static const char * const format = "error=%d, %s";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    const char *detail = defaultDetail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    char *errmsg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    jstring s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    if (errnum != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        const char *s = strerror(errnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (strcmp(s, "Unknown error") != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            detail = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /* ASCII Decimal representation uses 2.4 times as many bits as binary. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    errmsg = NEW(char, strlen(format) + strlen(detail) + 3 * sizeof(errnum));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    sprintf(errmsg, format, errnum, detail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    s = JNU_NewStringPlatform(env, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    if (s != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        jobject x = JNU_NewObjectByName(env, "java/io/IOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                        "(Ljava/lang/String;)V", s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (x != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            (*env)->Throw(env, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    free(errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
#ifdef DEBUG_PROCESS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
/* Debugging process code is difficult; where to write debug output? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
debugPrint(char *format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    FILE *tty = fopen("/dev/tty", "w");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    va_list ap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    va_start(ap, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    vfprintf(tty, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    va_end(ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    fclose(tty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
#endif /* DEBUG_PROCESS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   514
/**
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   515
 * Exec FILE as a traditional Bourne shell script (i.e. one without #!).
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   516
 * If we could do it over again, we would probably not support such an ancient
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   517
 * misfeature, but compatibility wins over sanity.  The original support for
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   518
 * this was imported accidentally from execvp().
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   519
 */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   520
static void
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   521
execve_as_traditional_shell_script(const char *file,
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   522
                                   const char *argv[],
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   523
                                   const char *const envp[])
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
{
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   525
    /* Use the extra word of space provided for us in argv by caller. */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   526
    const char *argv0 = argv[0];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   527
    const char *const *end = argv;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   528
    while (*end != NULL)
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   529
        ++end;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   530
    memmove(argv+2, argv+1, (end-argv) * sizeof (*end));
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   531
    argv[0] = "/bin/sh";
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   532
    argv[1] = file;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   533
    execve(argv[0], (char **) argv, (char **) envp);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   534
    /* Can't even exec /bin/sh?  Big trouble, but let's soldier on... */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   535
    memmove(argv+1, argv+2, (end-argv) * sizeof (*end));
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   536
    argv[0] = argv0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   539
/**
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   540
 * Like execve(2), except that in case of ENOEXEC, FILE is assumed to
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   541
 * be a shell script and the system default shell is invoked to run it.
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   542
 */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   543
static void
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   544
execve_with_shell_fallback(const char *file,
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   545
                           const char *argv[],
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   546
                           const char *const envp[])
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   547
{
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   548
#if START_CHILD_USE_CLONE || START_CHILD_USE_VFORK
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   549
    /* shared address space; be very careful. */
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   550
    execve(file, (char **) argv, (char **) envp);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   551
    if (errno == ENOEXEC)
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   552
        execve_as_traditional_shell_script(file, argv, envp);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   553
#else
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   554
    /* unshared address space; we can mutate environ. */
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   555
    environ = (char **) envp;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   556
    execvp(file, (char **) argv);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   557
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   558
}
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   559
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   560
/**
3421
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   561
 * 'execvpe' should have been included in the Unix standards,
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   562
 * and is a GNU extension in glibc 2.10.
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   563
 *
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   564
 * JDK_execvpe is identical to execvp, except that the child environment is
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   565
 * specified via the 3rd argument instead of being inherited from environ.
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   566
 */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   567
static void
3421
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   568
JDK_execvpe(const char *file,
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   569
            const char *argv[],
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   570
            const char *const envp[])
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
{
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   572
    if (envp == NULL || (char **) envp == environ) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   573
        execvp(file, (char **) argv);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   574
        return;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   575
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   577
    if (*file == '\0') {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   578
        errno = ENOENT;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   579
        return;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   580
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   582
    if (strchr(file, '/') != NULL) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   583
        execve_with_shell_fallback(file, argv, envp);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   584
    } else {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   585
        /* We must search PATH (parent's, not child's) */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   586
        char expanded_file[PATH_MAX];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   587
        int filelen = strlen(file);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   588
        int sticky_errno = 0;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   589
        const char * const * dirs;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   590
        for (dirs = parentPathv; *dirs; dirs++) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   591
            const char * dir = *dirs;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   592
            int dirlen = strlen(dir);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   593
            if (filelen + dirlen + 1 >= PATH_MAX) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   594
                errno = ENAMETOOLONG;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   595
                continue;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   596
            }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   597
            memcpy(expanded_file, dir, dirlen);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   598
            memcpy(expanded_file + dirlen, file, filelen);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   599
            expanded_file[dirlen + filelen] = '\0';
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   600
            execve_with_shell_fallback(expanded_file, argv, envp);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   601
            /* There are 3 responses to various classes of errno:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   602
             * return immediately, continue (especially for ENOENT),
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   603
             * or continue with "sticky" errno.
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   604
             *
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   605
             * From exec(3):
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   606
             *
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   607
             * If permission is denied for a file (the attempted
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   608
             * execve returned EACCES), these functions will continue
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   609
             * searching the rest of the search path.  If no other
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   610
             * file is found, however, they will return with the
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   611
             * global variable errno set to EACCES.
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   612
             */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   613
            switch (errno) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   614
            case EACCES:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   615
                sticky_errno = errno;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   616
                /* FALLTHRU */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   617
            case ENOENT:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   618
            case ENOTDIR:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   619
#ifdef ELOOP
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   620
            case ELOOP:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   621
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   622
#ifdef ESTALE
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   623
            case ESTALE:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   624
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   625
#ifdef ENODEV
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   626
            case ENODEV:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   627
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   628
#ifdef ETIMEDOUT
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   629
            case ETIMEDOUT:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   630
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   631
                break; /* Try other directories in PATH */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   632
            default:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   633
                return;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   634
            }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   635
        }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   636
        if (sticky_errno != 0)
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   637
            errno = sticky_errno;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   638
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
50
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   641
/*
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   642
 * Reads nbyte bytes from file descriptor fd into buf,
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   643
 * The read operation is retried in case of EINTR or partial reads.
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   644
 *
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   645
 * Returns number of bytes read (normally nbyte, but may be less in
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   646
 * case of EOF).  In case of read errors, returns -1 and sets errno.
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   647
 */
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   648
static ssize_t
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   649
readFully(int fd, void *buf, size_t nbyte)
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   650
{
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   651
    ssize_t remaining = nbyte;
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   652
    for (;;) {
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   653
        ssize_t n = read(fd, buf, remaining);
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   654
        if (n == 0) {
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   655
            return nbyte - remaining;
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   656
        } else if (n > 0) {
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   657
            remaining -= n;
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   658
            if (remaining <= 0)
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   659
                return nbyte;
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   660
            /* We were interrupted in the middle of reading the bytes.
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   661
             * Unlikely, but possible. */
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   662
            buf = (void *) (((char *)buf) + n);
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   663
        } else if (errno == EINTR) {
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   664
            /* Strange signals like SIGJVM1 are possible at any time.
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   665
             * See http://www.dreamsongs.com/WorseIsBetter.html */
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   666
        } else {
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   667
            return -1;
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   668
        }
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   669
    }
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   670
}
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   671
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   672
typedef struct _ChildStuff
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   673
{
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   674
    int in[2];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   675
    int out[2];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   676
    int err[2];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   677
    int fail[2];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   678
    int fds[3];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   679
    const char **argv;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   680
    const char **envv;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   681
    const char *pdir;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   682
    jboolean redirectErrorStream;
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   683
#if START_CHILD_USE_CLONE
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   684
    void *clone_stack;
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   685
#endif
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   686
} ChildStuff;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   687
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   688
static void
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   689
copyPipe(int from[2], int to[2])
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   690
{
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   691
    to[0] = from[0];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   692
    to[1] = from[1];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   693
}
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   694
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   695
/**
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   696
 * Child process after a successful fork() or clone().
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   697
 * This function must not return, and must be prepared for either all
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   698
 * of its address space to be shared with its parent, or to be a copy.
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   699
 * It must not modify global variables such as "environ".
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   700
 */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   701
static int
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   702
childProcess(void *arg)
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   703
{
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   704
    const ChildStuff* p = (const ChildStuff*) arg;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   705
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   706
    /* Close the parent sides of the pipes.
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   707
       Closing pipe fds here is redundant, since closeDescriptors()
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   708
       would do it anyways, but a little paranoia is a good thing. */
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   709
    if ((closeSafely(p->in[1])   == -1) ||
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   710
        (closeSafely(p->out[0])  == -1) ||
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   711
        (closeSafely(p->err[0])  == -1) ||
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   712
        (closeSafely(p->fail[0]) == -1))
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   713
        goto WhyCantJohnnyExec;
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   714
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   715
    /* Give the child sides of the pipes the right fileno's. */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   716
    /* Note: it is possible for in[0] == 0 */
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   717
    if ((moveDescriptor(p->in[0] != -1 ?  p->in[0] : p->fds[0],
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   718
                        STDIN_FILENO) == -1) ||
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   719
        (moveDescriptor(p->out[1]!= -1 ? p->out[1] : p->fds[1],
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   720
                        STDOUT_FILENO) == -1))
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   721
        goto WhyCantJohnnyExec;
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   722
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   723
    if (p->redirectErrorStream) {
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   724
        if ((closeSafely(p->err[1]) == -1) ||
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   725
            (restartableDup2(STDOUT_FILENO, STDERR_FILENO) == -1))
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   726
            goto WhyCantJohnnyExec;
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   727
    } else {
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   728
        if (moveDescriptor(p->err[1] != -1 ? p->err[1] : p->fds[2],
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   729
                           STDERR_FILENO) == -1)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   730
            goto WhyCantJohnnyExec;
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   731
    }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   732
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   733
    if (moveDescriptor(p->fail[1], FAIL_FILENO) == -1)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   734
        goto WhyCantJohnnyExec;
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   735
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   736
    /* close everything */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   737
    if (closeDescriptors() == 0) { /* failed,  close the old way */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   738
        int max_fd = (int)sysconf(_SC_OPEN_MAX);
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   739
        int fd;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   740
        for (fd = FAIL_FILENO + 1; fd < max_fd; fd++)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   741
            if (restartableClose(fd) == -1 && errno != EBADF)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   742
                goto WhyCantJohnnyExec;
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   743
    }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   744
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   745
    /* change to the new working directory */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   746
    if (p->pdir != NULL && chdir(p->pdir) < 0)
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   747
        goto WhyCantJohnnyExec;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   748
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   749
    if (fcntl(FAIL_FILENO, F_SETFD, FD_CLOEXEC) == -1)
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   750
        goto WhyCantJohnnyExec;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   751
3421
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   752
    JDK_execvpe(p->argv[0], p->argv, p->envv);
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   753
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   754
 WhyCantJohnnyExec:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   755
    /* We used to go to an awful lot of trouble to predict whether the
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   756
     * child would fail, but there is no reliable way to predict the
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   757
     * success of an operation without *trying* it, and there's no way
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   758
     * to try a chdir or exec in the parent.  Instead, all we need is a
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   759
     * way to communicate any failure back to the parent.  Easy; we just
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   760
     * send the errno back to the parent over a pipe in case of failure.
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   761
     * The tricky thing is, how do we communicate the *success* of exec?
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   762
     * We use FD_CLOEXEC together with the fact that a read() on a pipe
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   763
     * yields EOF when the write ends (we have two of them!) are closed.
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   764
     */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   765
    {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   766
        int errnum = errno;
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   767
        restartableWrite(FAIL_FILENO, &errnum, sizeof(errnum));
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   768
    }
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   769
    restartableClose(FAIL_FILENO);
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   770
    _exit(-1);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   771
    return 0;  /* Suppress warning "no return value from function" */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   772
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   774
/**
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   775
 * Start a child process running function childProcess.
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   776
 * This function only returns in the parent.
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   777
 * We are unusually paranoid; use of clone/vfork is
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   778
 * especially likely to tickle gcc/glibc bugs.
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   779
 */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   780
#ifdef __attribute_noinline__  /* See: sys/cdefs.h */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   781
__attribute_noinline__
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   782
#endif
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   783
static pid_t
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   784
startChild(ChildStuff *c) {
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   785
#if START_CHILD_USE_CLONE
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   786
#define START_CHILD_CLONE_STACK_SIZE (64 * 1024)
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   787
    /*
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   788
     * See clone(2).
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   789
     * Instead of worrying about which direction the stack grows, just
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   790
     * allocate twice as much and start the stack in the middle.
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   791
     */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   792
    if ((c->clone_stack = malloc(2 * START_CHILD_CLONE_STACK_SIZE)) == NULL)
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   793
        /* errno will be set to ENOMEM */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   794
        return -1;
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   795
    return clone(childProcess,
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   796
                 c->clone_stack + START_CHILD_CLONE_STACK_SIZE,
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   797
                 CLONE_VFORK | CLONE_VM | SIGCHLD, c);
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   798
#else
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   799
  #if START_CHILD_USE_VFORK
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   800
    /*
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   801
     * We separate the call to vfork into a separate function to make
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   802
     * very sure to keep stack of child from corrupting stack of parent,
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   803
     * as suggested by the scary gcc warning:
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   804
     *  warning: variable 'foo' might be clobbered by 'longjmp' or 'vfork'
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   805
     */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   806
    volatile pid_t resultPid = vfork();
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   807
  #else
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   808
    /*
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   809
     * From Solaris fork(2): In Solaris 10, a call to fork() is
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   810
     * identical to a call to fork1(); only the calling thread is
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   811
     * replicated in the child process. This is the POSIX-specified
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   812
     * behavior for fork().
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   813
     */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   814
    pid_t resultPid = fork();
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   815
  #endif
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   816
    if (resultPid == 0)
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   817
        childProcess(c);
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   818
    assert(resultPid != 0);  /* childProcess never returns */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   819
    return resultPid;
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   820
#endif /* ! START_CHILD_USE_CLONE */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   821
}
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   822
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
Java_java_lang_UNIXProcess_forkAndExec(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                                       jobject process,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                                       jbyteArray prog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                                       jbyteArray argBlock, jint argc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                                       jbyteArray envBlock, jint envc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                                       jbyteArray dir,
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   830
                                       jintArray std_fds,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   831
                                       jboolean redirectErrorStream)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    int errnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    int resultPid = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    int in[2], out[2], err[2], fail[2];
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   836
    jint *fds = NULL;
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   837
    const char *pprog = NULL;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   838
    const char *pargBlock = NULL;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   839
    const char *penvBlock = NULL;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   840
    ChildStuff *c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    in[0] = in[1] = out[0] = out[1] = err[0] = err[1] = fail[0] = fail[1] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   844
    if ((c = NEW(ChildStuff, 1)) == NULL) return -1;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   845
    c->argv = NULL;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   846
    c->envv = NULL;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   847
    c->pdir = NULL;
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   848
#if START_CHILD_USE_CLONE
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   849
    c->clone_stack = NULL;
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   850
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   852
    /* Convert prog + argBlock into a char ** argv.
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   853
     * Add one word room for expansion of argv for use by
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   854
     * execve_as_traditional_shell_script.
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   855
     */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   856
    assert(prog != NULL && argBlock != NULL);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   857
    if ((pprog     = getBytes(env, prog))       == NULL) goto Catch;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   858
    if ((pargBlock = getBytes(env, argBlock))   == NULL) goto Catch;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   859
    if ((c->argv = NEW(const char *, argc + 3)) == NULL) goto Catch;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   860
    c->argv[0] = pprog;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   861
    initVectorFromBlock(c->argv+1, pargBlock, argc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    if (envBlock != NULL) {
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   864
        /* Convert envBlock into a char ** envv */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   865
        if ((penvBlock = getBytes(env, envBlock))   == NULL) goto Catch;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   866
        if ((c->envv = NEW(const char *, envc + 1)) == NULL) goto Catch;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   867
        initVectorFromBlock(c->envv, penvBlock, envc);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   868
    }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   869
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   870
    if (dir != NULL) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   871
        if ((c->pdir = getBytes(env, dir)) == NULL) goto Catch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   874
    assert(std_fds != NULL);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   875
    fds = (*env)->GetIntArrayElements(env, std_fds, NULL);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   876
    if (fds == NULL) goto Catch;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   877
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   878
    if ((fds[0] == -1 && pipe(in)  < 0) ||
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   879
        (fds[1] == -1 && pipe(out) < 0) ||
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   880
        (fds[2] == -1 && pipe(err) < 0) ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        (pipe(fail) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        throwIOException(env, errno, "Bad file descriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   885
    c->fds[0] = fds[0];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   886
    c->fds[1] = fds[1];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   887
    c->fds[2] = fds[2];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   889
    copyPipe(in,   c->in);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   890
    copyPipe(out,  c->out);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   891
    copyPipe(err,  c->err);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   892
    copyPipe(fail, c->fail);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   893
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   894
    c->redirectErrorStream = redirectErrorStream;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   895
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   896
    resultPid = startChild(c);
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   897
    assert(resultPid != 0);
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   898
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    if (resultPid < 0) {
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   900
        throwIOException(env, errno, START_CHILD_SYSTEM_CALL " failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   904
    restartableClose(fail[1]); fail[1] = -1; /* See: WhyCantJohnnyExec */
50
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   905
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   906
    switch (readFully(fail[0], &errnum, sizeof(errnum))) {
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   907
    case 0: break; /* Exec succeeded */
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   908
    case sizeof(errnum):
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        waitpid(resultPid, NULL, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        throwIOException(env, errnum, "Exec failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        goto Catch;
50
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   912
    default:
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   913
        throwIOException(env, errno, "Read failed");
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   914
        goto Catch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   917
    fds[0] = (in [1] != -1) ? in [1] : -1;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   918
    fds[1] = (out[0] != -1) ? out[0] : -1;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   919
    fds[2] = (err[0] != -1) ? err[0] : -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
 Finally:
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   922
#if START_CHILD_USE_CLONE
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   923
    free(c->clone_stack);
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   924
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   925
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    /* Always clean up the child's side of the pipes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    closeSafely(in [0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    closeSafely(out[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    closeSafely(err[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    /* Always clean up fail descriptors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    closeSafely(fail[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    closeSafely(fail[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    releaseBytes(env, prog,     pprog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    releaseBytes(env, argBlock, pargBlock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    releaseBytes(env, envBlock, penvBlock);
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   938
    releaseBytes(env, dir,      c->pdir);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   939
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   940
    free(c->argv);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   941
    free(c->envv);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   942
    free(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   944
    if (fds != NULL)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   945
        (*env)->ReleaseIntArrayElements(env, std_fds, fds, 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   946
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    return resultPid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
 Catch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    /* Clean up the parent's side of the pipes in case of failure only */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    closeSafely(in [1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    closeSafely(out[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    closeSafely(err[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    goto Finally;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
Java_java_lang_UNIXProcess_destroyProcess(JNIEnv *env, jobject junk, jint pid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    kill(pid, SIGTERM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
}