jdk/src/solaris/native/java/lang/UNIXProcess_md.c
author ngmr
Tue, 31 Jan 2012 10:31:19 +0000
changeset 11691 ff8aec7600fe
parent 5506 202f599c92aa
child 12047 320a714614e9
permissions -rw-r--r--
7133301: (process) UNIXProcess_md.c should include sys/wait.h rather than wait.h Reviewed-by: alanb Contributed-by: Jonathan Lu <luchsh@linux.vnet.ibm.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
2948
7f4d7737d38f 6853336: (process) disable or remove clone-exec feature (6850720)
tbell
parents: 2946
diff changeset
    52
/*
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    53
 * 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
    54
 *
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    55
 * - 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
    56
 *   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
    57
 *   /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
    58
 *   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
    59
 *   process starts a small subprocess.
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    60
 *
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    61
 * - 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
    62
 *   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
    63
 *   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
    64
 *   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
    65
 *   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
    66
 *   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
    67
 *   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
    68
 *   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
    69
 *   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
    70
 *   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
    71
 *   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
    72
 *   file descriptors.
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    73
 *
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    74
 * - 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
    75
 *   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
    76
 *   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
    77
 *   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
    78
 *   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
    79
 *   the simple program
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    80
 *     Runtime.getRuntime().exec("/bin/true").waitFor();
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    81
 *   with:
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    82
 *     #  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
    83
 *     #  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
    84
 *   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
    85
 *     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
    86
 *   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
    87
 *
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    88
 * 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
    89
 * 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
    90
 * remains.
2948
7f4d7737d38f 6853336: (process) disable or remove clone-exec feature (6850720)
tbell
parents: 2946
diff changeset
    91
 */
3430
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
#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
    94
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    95
#ifndef START_CHILD_USE_CLONE
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    96
  #ifdef __linux__
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    97
    #define START_CHILD_USE_CLONE 1
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    98
  #else
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
    99
    #define START_CHILD_USE_CLONE 0
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   100
  #endif
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   101
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   102
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   103
/* By default, use vfork() on Linux. */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   104
#ifndef START_CHILD_USE_VFORK
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   105
  #ifdef __linux__
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   106
    #define START_CHILD_USE_VFORK 1
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   107
  #else
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   108
    #define START_CHILD_USE_VFORK 0
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   109
  #endif
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   110
#endif
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   111
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   112
#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
   113
#include <sched.h>
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   114
#define START_CHILD_SYSTEM_CALL "clone"
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   115
#elif START_CHILD_USE_VFORK
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   116
#define START_CHILD_SYSTEM_CALL "vfork"
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   117
#else
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   118
#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
   119
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   120
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
#ifndef STDIN_FILENO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
#define STDIN_FILENO 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
#ifndef STDOUT_FILENO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
#define STDOUT_FILENO 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
#ifndef STDERR_FILENO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
#define STDERR_FILENO 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
#ifndef SA_NOCLDSTOP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
#define SA_NOCLDSTOP 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
#ifndef SA_RESTART
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
#define SA_RESTART 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
#define FAIL_FILENO (STDERR_FILENO + 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   143
/* TODO: Refactor. */
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   144
#define RESTARTABLE(_cmd, _result) do { \
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   145
  do { \
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   146
    _result = _cmd; \
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   147
  } while((_result == -1) && (errno == EINTR)); \
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   148
} while(0)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   149
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   150
/* 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
   151
 * 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
   152
 * 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
   153
 * 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
   154
 * declaration is to be found.  See:
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   155
 * 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
   156
 * 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
   157
 *
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   158
 * "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
   159
 * 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
   160
 */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   161
extern char **environ;
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
setSIGCHLDHandler(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /* There is a subtle difference between having the signal handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * for SIGCHLD be SIG_DFL and SIG_IGN.  We cannot obtain process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * termination information for child processes if the signal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * handler is SIG_IGN.  It must be SIG_DFL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * We used to set the SIGCHLD handler only on Linux, but it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * safest to set it unconditionally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Consider what happens if java's parent process sets the SIGCHLD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * handler to SIG_IGN.  Normally signal handlers are inherited by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * children, but SIGCHLD is a controversial case.  Solaris appears
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * to always reset it to SIG_DFL, but this behavior may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * non-standard-compliant, and we shouldn't rely on it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * References:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * http://www.opengroup.org/onlinepubs/7908799/xsh/exec.html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * http://www.pasc.org/interps/unofficial/db/p1003.1/pasc-1003.1-132.html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    struct sigaction sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    sa.sa_handler = SIG_DFL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    sigemptyset(&sa.sa_mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    if (sigaction(SIGCHLD, &sa, NULL) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        JNU_ThrowInternalError(env, "Can't set SIGCHLD handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
static void*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
xmalloc(JNIEnv *env, size_t size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    void *p = malloc(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    if (p == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        JNU_ThrowOutOfMemoryError(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
#define NEW(type, n) ((type *) xmalloc(env, (n) * sizeof(type)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * If PATH is not defined, the OS provides some default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * Unfortunately, there's no portable way to get this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * Fortunately, it's only needed if the child has PATH while we do not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
static const char*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
defaultPath(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /* These really are the Solaris defaults! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    return (geteuid() == 0 || getuid() == 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        "/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin:/usr/sbin" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        "/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    return ":/bin:/usr/bin";    /* glibc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
static const char*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
effectivePath(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    const char *s = getenv("PATH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    return (s != NULL) ? s : defaultPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
countOccurrences(const char *s, char c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    for (count = 0; *s != '\0'; s++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        count += (*s == c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
static const char * const *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
splitPath(JNIEnv *env, const char *path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    const char *p, *q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    char **pathv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    int count = countOccurrences(path, ':') + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    pathv = NEW(char*, count+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    pathv[count] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    for (p = path, i = 0; i < count; i++, p = q + 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        for (q = p; (*q != ':') && (*q != '\0'); q++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (q == p)             /* empty PATH component => "." */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            pathv[i] = "./";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            int addSlash = ((*(q - 1)) != '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            pathv[i] = NEW(char, q - p + addSlash + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            memcpy(pathv[i], p, q - p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (addSlash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                pathv[i][q - p] = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            pathv[i][q - p + addSlash] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    return (const char * const *) pathv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * Cached value of JVM's effective PATH.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 * (We don't support putenv("PATH=...") in native code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
static const char *parentPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 * Split, canonicalized version of parentPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
static const char * const *parentPathv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
static jfieldID field_exitcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
Java_java_lang_UNIXProcess_initIDs(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    field_exitcode = (*env)->GetFieldID(env, clazz, "exitcode", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    parentPath  = effectivePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    parentPathv = splitPath(env, parentPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    setSIGCHLDHandler(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
#ifndef WIFEXITED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
#define WIFEXITED(status) (((status)&0xFF) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
#ifndef WEXITSTATUS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
#define WEXITSTATUS(status) (((status)>>8)&0xFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
#ifndef WIFSIGNALED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
#define WIFSIGNALED(status) (((status)&0xFF) > 0 && ((status)&0xFF00) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
#ifndef WTERMSIG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
#define WTERMSIG(status) ((status)&0x7F)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
/* Block until a child process exits and return its exit code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
   Note, can only be called once for any given pid. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
Java_java_lang_UNIXProcess_waitForProcessExit(JNIEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                              jobject junk,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                              jint pid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /* We used to use waitid() on Solaris, waitpid() on Linux, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * waitpid() is more standard, so use it on all POSIX platforms. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    int status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /* Wait for the child process to exit.  This returns immediately if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
       the child has already exited. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    while (waitpid(pid, &status, 0) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        switch (errno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        case ECHILD: return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        case EINTR: break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        default: return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    if (WIFEXITED(status)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * The child exited normally; get its exit code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return WEXITSTATUS(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    } else if (WIFSIGNALED(status)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        /* The child exited because of a signal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * The best value to return is 0x80 + signal number,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         * because that is what all Unix shells do, and because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         * it allows callers to distinguish between process exit and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         * process death by signal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * Unfortunately, the historical behavior on Solaris is to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * the signal number, and we preserve this for compatibility. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return WTERMSIG(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return 0x80 + WTERMSIG(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         * Unknown exit code; pass it through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   352
static ssize_t
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   353
restartableWrite(int fd, const void *buf, size_t count)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   354
{
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   355
    ssize_t result;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   356
    RESTARTABLE(write(fd, buf, count), result);
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   357
    return result;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   358
}
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
static int
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   361
restartableDup2(int fd_from, int fd_to)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   362
{
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   363
    int err;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   364
    RESTARTABLE(dup2(fd_from, fd_to), err);
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   365
    return err;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   366
}
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
static int
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   369
restartableClose(int fd)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   370
{
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   371
    int err;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   372
    RESTARTABLE(close(fd), err);
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   373
    return err;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   374
}
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
static int
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   377
closeSafely(int fd)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   378
{
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   379
    return (fd == -1) ? 0 : restartableClose(fd);
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
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
   383
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
   384
{
66e7f98f4dde 6798822: (process) Non-portable use of isdigit in src/solaris/native/java/lang/UNIXProcess_md.c
martin
parents: 50
diff changeset
   385
  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
   386
}
66e7f98f4dde 6798822: (process) Non-portable use of isdigit in src/solaris/native/java/lang/UNIXProcess_md.c
martin
parents: 50
diff changeset
   387
66e7f98f4dde 6798822: (process) Non-portable use of isdigit in src/solaris/native/java/lang/UNIXProcess_md.c
martin
parents: 50
diff changeset
   388
static int
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
closeDescriptors(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    DIR *dp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    struct dirent64 *dirp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    int from_fd = FAIL_FILENO + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /* We're trying to close all file descriptors, but opendir() might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * itself be implemented using a file descriptor, and we certainly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * don't want to close that while it's in use.  We assume that if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * opendir() is implemented using a file descriptor, then it uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * the lowest numbered file descriptor, just like open().  So we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * close a couple explicitly.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   402
    restartableClose(from_fd);          /* for possible use by opendir() */
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   403
    restartableClose(from_fd + 1);      /* another one for good luck */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    if ((dp = opendir("/proc/self/fd")) == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /* We use readdir64 instead of readdir to work around Solaris bug
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * 6395699: /proc/self/fd fails to report file descriptors >= 1024 on Solaris 9
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    while ((dirp = readdir64(dp)) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        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
   413
        if (isAsciiDigit(dirp->d_name[0]) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            (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
   415
            restartableClose(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    closedir(dp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   423
static int
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
moveDescriptor(int fd_from, int fd_to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    if (fd_from != fd_to) {
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   427
        if ((restartableDup2(fd_from, fd_to) == -1) ||
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   428
            (restartableClose(fd_from) == -1))
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   429
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   431
    return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
static const char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
getBytes(JNIEnv *env, jbyteArray arr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    return arr == NULL ? NULL :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        (const char*) (*env)->GetByteArrayElements(env, arr, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
releaseBytes(JNIEnv *env, jbyteArray arr, const char* parr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    if (parr != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        (*env)->ReleaseByteArrayElements(env, arr, (jbyte*) parr, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
initVectorFromBlock(const char**vector, const char* block, int count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    const char *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    for (i = 0, p = block; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        /* Invariant: p always points to the start of a C string. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        vector[i] = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        while (*(p++));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    vector[count] = NULL;
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
throwIOException(JNIEnv *env, int errnum, const char *defaultDetail)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    static const char * const format = "error=%d, %s";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    const char *detail = defaultDetail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    char *errmsg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    jstring s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    if (errnum != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        const char *s = strerror(errnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (strcmp(s, "Unknown error") != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            detail = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /* ASCII Decimal representation uses 2.4 times as many bits as binary. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    errmsg = NEW(char, strlen(format) + strlen(detail) + 3 * sizeof(errnum));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    sprintf(errmsg, format, errnum, detail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    s = JNU_NewStringPlatform(env, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    if (s != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        jobject x = JNU_NewObjectByName(env, "java/io/IOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                        "(Ljava/lang/String;)V", s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (x != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            (*env)->Throw(env, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    free(errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
#ifdef DEBUG_PROCESS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
/* Debugging process code is difficult; where to write debug output? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
debugPrint(char *format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    FILE *tty = fopen("/dev/tty", "w");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    va_list ap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    va_start(ap, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    vfprintf(tty, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    va_end(ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    fclose(tty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
#endif /* DEBUG_PROCESS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   501
/**
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   502
 * 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
   503
 * 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
   504
 * 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
   505
 * 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
   506
 */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   507
static void
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   508
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
   509
                                   const char *argv[],
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   510
                                   const char *const envp[])
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
{
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   512
    /* 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
   513
    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
   514
    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
   515
    while (*end != NULL)
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   516
        ++end;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   517
    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
   518
    argv[0] = "/bin/sh";
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   519
    argv[1] = file;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   520
    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
   521
    /* 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
   522
    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
   523
    argv[0] = argv0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   526
/**
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   527
 * 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
   528
 * 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
   529
 */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   530
static void
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   531
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
   532
                           const char *argv[],
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   533
                           const char *const envp[])
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   534
{
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   535
#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
   536
    /* 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
   537
    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
   538
    if (errno == ENOEXEC)
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   539
        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
   540
#else
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   541
    /* 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
   542
    environ = (char **) envp;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   543
    execvp(file, (char **) argv);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   544
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   545
}
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   546
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   547
/**
3421
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   548
 * '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
   549
 * 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
   550
 *
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   551
 * 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
   552
 * 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
   553
 */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   554
static void
3421
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   555
JDK_execvpe(const char *file,
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   556
            const char *argv[],
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   557
            const char *const envp[])
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
{
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   559
    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
   560
        execvp(file, (char **) argv);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   561
        return;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   562
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   564
    if (*file == '\0') {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   565
        errno = ENOENT;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   566
        return;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   567
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   569
    if (strchr(file, '/') != NULL) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   570
        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
   571
    } else {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   572
        /* 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
   573
        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
   574
        int filelen = strlen(file);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   575
        int sticky_errno = 0;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   576
        const char * const * dirs;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   577
        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
   578
            const char * dir = *dirs;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   579
            int dirlen = strlen(dir);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   580
            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
   581
                errno = ENAMETOOLONG;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   582
                continue;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   583
            }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   584
            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
   585
            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
   586
            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
   587
            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
   588
            /* 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
   589
             * 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
   590
             * 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
   591
             *
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   592
             * From exec(3):
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   593
             *
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   594
             * 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
   595
             * 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
   596
             * 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
   597
             * 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
   598
             * 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
   599
             */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   600
            switch (errno) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   601
            case EACCES:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   602
                sticky_errno = errno;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   603
                /* FALLTHRU */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   604
            case ENOENT:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   605
            case ENOTDIR:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   606
#ifdef ELOOP
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   607
            case ELOOP:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   608
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   609
#ifdef ESTALE
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   610
            case ESTALE:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   611
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   612
#ifdef ENODEV
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   613
            case ENODEV:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   614
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   615
#ifdef ETIMEDOUT
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   616
            case ETIMEDOUT:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   617
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   618
                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
   619
            default:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   620
                return;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   621
            }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   622
        }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   623
        if (sticky_errno != 0)
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   624
            errno = sticky_errno;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   625
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
50
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   628
/*
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   629
 * 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
   630
 * 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
   631
 *
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   632
 * 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
   633
 * 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
   634
 */
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   635
static ssize_t
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   636
readFully(int fd, void *buf, size_t nbyte)
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   637
{
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   638
    ssize_t remaining = nbyte;
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   639
    for (;;) {
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   640
        ssize_t n = read(fd, buf, remaining);
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   641
        if (n == 0) {
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   642
            return nbyte - remaining;
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   643
        } else if (n > 0) {
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   644
            remaining -= n;
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   645
            if (remaining <= 0)
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   646
                return nbyte;
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   647
            /* 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
   648
             * Unlikely, but possible. */
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   649
            buf = (void *) (((char *)buf) + n);
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   650
        } else if (errno == EINTR) {
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   651
            /* 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
   652
             * See http://www.dreamsongs.com/WorseIsBetter.html */
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   653
        } else {
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   654
            return -1;
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   655
        }
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   656
    }
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   657
}
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   658
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   659
typedef struct _ChildStuff
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   660
{
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   661
    int in[2];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   662
    int out[2];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   663
    int err[2];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   664
    int fail[2];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   665
    int fds[3];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   666
    const char **argv;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   667
    const char **envv;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   668
    const char *pdir;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   669
    jboolean redirectErrorStream;
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   670
#if START_CHILD_USE_CLONE
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   671
    void *clone_stack;
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   672
#endif
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   673
} ChildStuff;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   674
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   675
static void
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   676
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
   677
{
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   678
    to[0] = from[0];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   679
    to[1] = from[1];
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   680
}
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   681
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   682
/**
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   683
 * 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
   684
 * 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
   685
 * 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
   686
 * 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
   687
 */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   688
static int
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   689
childProcess(void *arg)
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
    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
   692
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   693
    /* 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
   694
       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
   695
       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
   696
    if ((closeSafely(p->in[1])   == -1) ||
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   697
        (closeSafely(p->out[0])  == -1) ||
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   698
        (closeSafely(p->err[0])  == -1) ||
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   699
        (closeSafely(p->fail[0]) == -1))
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   700
        goto WhyCantJohnnyExec;
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   701
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   702
    /* 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
   703
    /* Note: it is possible for in[0] == 0 */
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   704
    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
   705
                        STDIN_FILENO) == -1) ||
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   706
        (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
   707
                        STDOUT_FILENO) == -1))
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   708
        goto WhyCantJohnnyExec;
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   709
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   710
    if (p->redirectErrorStream) {
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   711
        if ((closeSafely(p->err[1]) == -1) ||
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   712
            (restartableDup2(STDOUT_FILENO, STDERR_FILENO) == -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
    } else {
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   715
        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
   716
                           STDERR_FILENO) == -1)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   717
            goto WhyCantJohnnyExec;
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   718
    }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   719
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   720
    if (moveDescriptor(p->fail[1], FAIL_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
    /* close everything */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   724
    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
   725
        int max_fd = (int)sysconf(_SC_OPEN_MAX);
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   726
        int fd;
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   727
        for (fd = FAIL_FILENO + 1; fd < max_fd; fd++)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   728
            if (restartableClose(fd) == -1 && errno != EBADF)
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   729
                goto WhyCantJohnnyExec;
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   730
    }
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
    /* 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
   733
    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
   734
        goto WhyCantJohnnyExec;
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
    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
   737
        goto WhyCantJohnnyExec;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   738
3421
e3af585364e7 6866719: Rename execvpe to avoid symbol clash with glibc 2.10
martin
parents: 2948
diff changeset
   739
    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
   740
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   741
 WhyCantJohnnyExec:
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   742
    /* 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
   743
     * 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
   744
     * 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
   745
     * 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
   746
     * 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
   747
     * 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
   748
     * 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
   749
     * 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
   750
     * 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
   751
     */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   752
    {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   753
        int errnum = errno;
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   754
        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
   755
    }
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   756
    restartableClose(FAIL_FILENO);
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   757
    _exit(-1);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   758
    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
   759
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   761
/**
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   762
 * 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
   763
 * 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
   764
 * 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
   765
 * 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
   766
 */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   767
#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
   768
__attribute_noinline__
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   769
#endif
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   770
static pid_t
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   771
startChild(ChildStuff *c) {
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   772
#if START_CHILD_USE_CLONE
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   773
#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
   774
    /*
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   775
     * See clone(2).
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   776
     * 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
   777
     * 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
   778
     */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   779
    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
   780
        /* errno will be set to ENOMEM */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   781
        return -1;
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   782
    return clone(childProcess,
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   783
                 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
   784
                 CLONE_VFORK | CLONE_VM | SIGCHLD, c);
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   785
#else
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   786
  #if START_CHILD_USE_VFORK
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
     * 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
   789
     * 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
   790
     * 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
   791
     *  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
   792
     */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   793
    volatile pid_t resultPid = vfork();
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   794
  #else
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   795
    /*
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   796
     * 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
   797
     * 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
   798
     * 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
   799
     * behavior for fork().
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
    pid_t resultPid = fork();
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   802
  #endif
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   803
    if (resultPid == 0)
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   804
        childProcess(c);
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   805
    assert(resultPid != 0);  /* childProcess never returns */
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   806
    return resultPid;
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   807
#endif /* ! START_CHILD_USE_CLONE */
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
Java_java_lang_UNIXProcess_forkAndExec(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                                       jobject process,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                                       jbyteArray prog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                                       jbyteArray argBlock, jint argc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                                       jbyteArray envBlock, jint envc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                                       jbyteArray dir,
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   817
                                       jintArray std_fds,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   818
                                       jboolean redirectErrorStream)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    int errnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    int resultPid = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    int in[2], out[2], err[2], fail[2];
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   823
    jint *fds = NULL;
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   824
    const char *pprog = NULL;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   825
    const char *pargBlock = NULL;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   826
    const char *penvBlock = NULL;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   827
    ChildStuff *c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    in[0] = in[1] = out[0] = out[1] = err[0] = err[1] = fail[0] = fail[1] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   831
    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
   832
    c->argv = NULL;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   833
    c->envv = NULL;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   834
    c->pdir = NULL;
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   835
#if START_CHILD_USE_CLONE
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   836
    c->clone_stack = NULL;
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   837
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   839
    /* 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
   840
     * 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
   841
     * 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
   842
     */
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   843
    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
   844
    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
   845
    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
   846
    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
   847
    c->argv[0] = pprog;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   848
    initVectorFromBlock(c->argv+1, pargBlock, argc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    if (envBlock != NULL) {
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   851
        /* 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
   852
        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
   853
        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
   854
        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
   855
    }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   856
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   857
    if (dir != NULL) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   858
        if ((c->pdir = getBytes(env, dir)) == NULL) goto Catch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   861
    assert(std_fds != NULL);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   862
    fds = (*env)->GetIntArrayElements(env, std_fds, NULL);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   863
    if (fds == NULL) goto Catch;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   864
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   865
    if ((fds[0] == -1 && pipe(in)  < 0) ||
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   866
        (fds[1] == -1 && pipe(out) < 0) ||
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   867
        (fds[2] == -1 && pipe(err) < 0) ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        (pipe(fail) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        throwIOException(env, errno, "Bad file descriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   872
    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
   873
    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
   874
    c->fds[2] = fds[2];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   876
    copyPipe(in,   c->in);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   877
    copyPipe(out,  c->out);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   878
    copyPipe(err,  c->err);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   879
    copyPipe(fail, c->fail);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   880
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   881
    c->redirectErrorStream = redirectErrorStream;
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   882
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   883
    resultPid = startChild(c);
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   884
    assert(resultPid != 0);
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   885
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    if (resultPid < 0) {
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   887
        throwIOException(env, errno, START_CHILD_SYSTEM_CALL " failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
3431
e9cb95a39e3e 6856590: (process) Use RESTARTABLE in UNIXProcess_md.c
martin
parents: 3430
diff changeset
   891
    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
   892
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   893
    switch (readFully(fail[0], &errnum, sizeof(errnum))) {
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   894
    case 0: break; /* Exec succeeded */
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   895
    case sizeof(errnum):
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        waitpid(resultPid, NULL, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        throwIOException(env, errnum, "Exec failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        goto Catch;
50
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   899
    default:
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   900
        throwIOException(env, errno, "Read failed");
a437b3f9d7f4 6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents: 48
diff changeset
   901
        goto Catch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   904
    fds[0] = (in [1] != -1) ? in [1] : -1;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   905
    fds[1] = (out[0] != -1) ? out[0] : -1;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   906
    fds[2] = (err[0] != -1) ? err[0] : -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
 Finally:
3430
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   909
#if START_CHILD_USE_CLONE
8d6d03175761 6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents: 3421
diff changeset
   910
    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
   911
#endif
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   912
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    /* Always clean up the child's side of the pipes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    closeSafely(in [0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    closeSafely(out[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    closeSafely(err[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    /* Always clean up fail descriptors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    closeSafely(fail[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    closeSafely(fail[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    releaseBytes(env, prog,     pprog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    releaseBytes(env, argBlock, pargBlock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    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
   925
    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
   926
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   927
    free(c->argv);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   928
    free(c->envv);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 1936
diff changeset
   929
    free(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   931
    if (fds != NULL)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   932
        (*env)->ReleaseIntArrayElements(env, std_fds, fds, 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   933
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    return resultPid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
 Catch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    /* Clean up the parent's side of the pipes in case of failure only */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    closeSafely(in [1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    closeSafely(out[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    closeSafely(err[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    goto Finally;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
Java_java_lang_UNIXProcess_destroyProcess(JNIEnv *env, jobject junk, jint pid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    kill(pid, SIGTERM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
}