author | simonis |
Tue, 26 Nov 2013 16:40:31 +0100 | |
changeset 22597 | 7515a991bb37 |
parent 19399 | e2e5122cd62e |
child 22641 | e47f8892133e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22597
7515a991bb37
8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
19399
diff
changeset
|
2 |
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
#undef _LARGEFILE64_SOURCE |
|
27 |
#define _LARGEFILE64_SOURCE 1 |
|
28 |
||
29 |
#include "jni.h" |
|
30 |
#include "jvm.h" |
|
31 |
#include "jvm_md.h" |
|
32 |
#include "jni_util.h" |
|
33 |
#include "io_util.h" |
|
34 |
||
35 |
/* |
|
36 |
* Platform-specific support for java.lang.Process |
|
37 |
*/ |
|
38 |
#include <assert.h> |
|
39 |
#include <stddef.h> |
|
40 |
#include <stdlib.h> |
|
41 |
#include <sys/types.h> |
|
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 | 44 |
#include <signal.h> |
45 |
#include <string.h> |
|
46 |
||
22597
7515a991bb37
8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
19399
diff
changeset
|
47 |
#if defined(__solaris__) || defined(_ALLBSD_SOURCE) || defined(_AIX) |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
48 |
#include <spawn.h> |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
11691
diff
changeset
|
49 |
#endif |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
11691
diff
changeset
|
50 |
|
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
51 |
#include "childproc.h" |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
52 |
|
2948
7f4d7737d38f
6853336: (process) disable or remove clone-exec feature (6850720)
tbell
parents:
2946
diff
changeset
|
53 |
/* |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
54 |
* There are 4 possible strategies we might use to "fork": |
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
55 |
* |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
56 |
* - 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
|
57 |
* 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
|
58 |
* /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
|
59 |
* 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
|
60 |
* process starts a small subprocess. |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
61 |
* |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
62 |
* - 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
|
63 |
* 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
|
64 |
* 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
|
65 |
* 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
|
66 |
* 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
|
67 |
* 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
|
68 |
* 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
|
69 |
* 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
|
70 |
* 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
|
71 |
* 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
|
72 |
* 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
|
73 |
* file descriptors. |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
74 |
* |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
75 |
* - 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
|
76 |
* 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
|
77 |
* 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
|
78 |
* 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
|
79 |
* 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
|
80 |
* the simple program |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
81 |
* Runtime.getRuntime().exec("/bin/true").waitFor(); |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
82 |
* with: |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
83 |
* # 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
|
84 |
* # 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
|
85 |
* 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
|
86 |
* 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
|
87 |
* 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
|
88 |
* |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
89 |
* - posix_spawn(). While posix_spawn() is a fairly elaborate and |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
90 |
* complicated system call, it can't quite do everything that the old |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
91 |
* fork()/exec() combination can do, so the only feasible way to do |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
92 |
* this, is to use posix_spawn to launch a new helper executable |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
93 |
* "jprochelper", which in turn execs the target (after cleaning |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
94 |
* up file-descriptors etc.) The end result is the same as before, |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
95 |
* a child process linked to the parent in the same way, but it |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
96 |
* avoids the problem of duplicating the parent (VM) process |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
97 |
* address space temporarily, before launching the target command. |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
98 |
* |
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
99 |
* Based on the above analysis, we are currently using vfork() on |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
100 |
* Linux and spawn() on other Unix systems, but the code to use clone() |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
101 |
* and fork() remains. |
2948
7f4d7737d38f
6853336: (process) disable or remove clone-exec feature (6850720)
tbell
parents:
2946
diff
changeset
|
102 |
*/ |
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
103 |
|
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
104 |
|
2 | 105 |
static void |
106 |
setSIGCHLDHandler(JNIEnv *env) |
|
107 |
{ |
|
108 |
/* There is a subtle difference between having the signal handler |
|
109 |
* for SIGCHLD be SIG_DFL and SIG_IGN. We cannot obtain process |
|
110 |
* termination information for child processes if the signal |
|
111 |
* handler is SIG_IGN. It must be SIG_DFL. |
|
112 |
* |
|
113 |
* We used to set the SIGCHLD handler only on Linux, but it's |
|
114 |
* safest to set it unconditionally. |
|
115 |
* |
|
116 |
* Consider what happens if java's parent process sets the SIGCHLD |
|
117 |
* handler to SIG_IGN. Normally signal handlers are inherited by |
|
118 |
* children, but SIGCHLD is a controversial case. Solaris appears |
|
119 |
* to always reset it to SIG_DFL, but this behavior may be |
|
120 |
* non-standard-compliant, and we shouldn't rely on it. |
|
121 |
* |
|
122 |
* References: |
|
123 |
* http://www.opengroup.org/onlinepubs/7908799/xsh/exec.html |
|
124 |
* http://www.pasc.org/interps/unofficial/db/p1003.1/pasc-1003.1-132.html |
|
125 |
*/ |
|
126 |
struct sigaction sa; |
|
127 |
sa.sa_handler = SIG_DFL; |
|
128 |
sigemptyset(&sa.sa_mask); |
|
129 |
sa.sa_flags = SA_NOCLDSTOP | SA_RESTART; |
|
130 |
if (sigaction(SIGCHLD, &sa, NULL) < 0) |
|
131 |
JNU_ThrowInternalError(env, "Can't set SIGCHLD handler"); |
|
132 |
} |
|
133 |
||
134 |
static void* |
|
135 |
xmalloc(JNIEnv *env, size_t size) |
|
136 |
{ |
|
137 |
void *p = malloc(size); |
|
138 |
if (p == NULL) |
|
139 |
JNU_ThrowOutOfMemoryError(env, NULL); |
|
140 |
return p; |
|
141 |
} |
|
142 |
||
143 |
#define NEW(type, n) ((type *) xmalloc(env, (n) * sizeof(type))) |
|
144 |
||
145 |
/** |
|
146 |
* If PATH is not defined, the OS provides some default value. |
|
147 |
* Unfortunately, there's no portable way to get this value. |
|
148 |
* Fortunately, it's only needed if the child has PATH while we do not. |
|
149 |
*/ |
|
150 |
static const char* |
|
151 |
defaultPath(void) |
|
152 |
{ |
|
153 |
#ifdef __solaris__ |
|
154 |
/* These really are the Solaris defaults! */ |
|
155 |
return (geteuid() == 0 || getuid() == 0) ? |
|
156 |
"/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin:/usr/sbin" : |
|
157 |
"/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin:"; |
|
158 |
#else |
|
159 |
return ":/bin:/usr/bin"; /* glibc */ |
|
160 |
#endif |
|
161 |
} |
|
162 |
||
163 |
static const char* |
|
164 |
effectivePath(void) |
|
165 |
{ |
|
166 |
const char *s = getenv("PATH"); |
|
167 |
return (s != NULL) ? s : defaultPath(); |
|
168 |
} |
|
169 |
||
170 |
static int |
|
171 |
countOccurrences(const char *s, char c) |
|
172 |
{ |
|
173 |
int count; |
|
174 |
for (count = 0; *s != '\0'; s++) |
|
175 |
count += (*s == c); |
|
176 |
return count; |
|
177 |
} |
|
178 |
||
179 |
static const char * const * |
|
16860
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
180 |
effectivePathv(JNIEnv *env) |
2 | 181 |
{ |
16860
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
182 |
char *p; |
2 | 183 |
int i; |
16860
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
184 |
const char *path = effectivePath(); |
2 | 185 |
int count = countOccurrences(path, ':') + 1; |
16860
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
186 |
size_t pathvsize = sizeof(const char *) * (count+1); |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
187 |
size_t pathsize = strlen(path) + 1; |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
188 |
const char **pathv = (const char **) xmalloc(env, pathvsize + pathsize); |
2 | 189 |
|
16860
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
190 |
if (pathv == NULL) |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
191 |
return NULL; |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
192 |
p = (char *) pathv + pathvsize; |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
193 |
memcpy(p, path, pathsize); |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
194 |
/* split PATH by replacing ':' with NULs; empty components => "." */ |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
195 |
for (i = 0; i < count; i++) { |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
196 |
char *q = p + strcspn(p, ":"); |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
197 |
pathv[i] = (p == q) ? "." : p; |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
198 |
*q = '\0'; |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
199 |
p = q + 1; |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
200 |
} |
2 | 201 |
pathv[count] = NULL; |
16860
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
202 |
return pathv; |
2 | 203 |
} |
204 |
||
205 |
JNIEXPORT void JNICALL |
|
16860
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
206 |
Java_java_lang_UNIXProcess_init(JNIEnv *env, jclass clazz) |
2 | 207 |
{ |
16860
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
208 |
parentPathv = effectivePathv(env); |
2 | 209 |
setSIGCHLDHandler(env); |
210 |
} |
|
211 |
||
212 |
||
213 |
#ifndef WIFEXITED |
|
214 |
#define WIFEXITED(status) (((status)&0xFF) == 0) |
|
215 |
#endif |
|
216 |
||
217 |
#ifndef WEXITSTATUS |
|
218 |
#define WEXITSTATUS(status) (((status)>>8)&0xFF) |
|
219 |
#endif |
|
220 |
||
221 |
#ifndef WIFSIGNALED |
|
222 |
#define WIFSIGNALED(status) (((status)&0xFF) > 0 && ((status)&0xFF00) == 0) |
|
223 |
#endif |
|
224 |
||
225 |
#ifndef WTERMSIG |
|
226 |
#define WTERMSIG(status) ((status)&0x7F) |
|
227 |
#endif |
|
228 |
||
229 |
/* Block until a child process exits and return its exit code. |
|
230 |
Note, can only be called once for any given pid. */ |
|
231 |
JNIEXPORT jint JNICALL |
|
232 |
Java_java_lang_UNIXProcess_waitForProcessExit(JNIEnv* env, |
|
233 |
jobject junk, |
|
234 |
jint pid) |
|
235 |
{ |
|
236 |
/* We used to use waitid() on Solaris, waitpid() on Linux, but |
|
237 |
* waitpid() is more standard, so use it on all POSIX platforms. */ |
|
238 |
int status; |
|
239 |
/* Wait for the child process to exit. This returns immediately if |
|
240 |
the child has already exited. */ |
|
241 |
while (waitpid(pid, &status, 0) < 0) { |
|
242 |
switch (errno) { |
|
243 |
case ECHILD: return 0; |
|
244 |
case EINTR: break; |
|
245 |
default: return -1; |
|
246 |
} |
|
247 |
} |
|
248 |
||
249 |
if (WIFEXITED(status)) { |
|
250 |
/* |
|
251 |
* The child exited normally; get its exit code. |
|
252 |
*/ |
|
253 |
return WEXITSTATUS(status); |
|
254 |
} else if (WIFSIGNALED(status)) { |
|
255 |
/* The child exited because of a signal. |
|
256 |
* The best value to return is 0x80 + signal number, |
|
257 |
* because that is what all Unix shells do, and because |
|
258 |
* it allows callers to distinguish between process exit and |
|
259 |
* process death by signal. |
|
260 |
* Unfortunately, the historical behavior on Solaris is to return |
|
261 |
* the signal number, and we preserve this for compatibility. */ |
|
262 |
#ifdef __solaris__ |
|
263 |
return WTERMSIG(status); |
|
264 |
#else |
|
265 |
return 0x80 + WTERMSIG(status); |
|
266 |
#endif |
|
267 |
} else { |
|
268 |
/* |
|
269 |
* Unknown exit code; pass it through. |
|
270 |
*/ |
|
271 |
return status; |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
static const char * |
|
276 |
getBytes(JNIEnv *env, jbyteArray arr) |
|
277 |
{ |
|
278 |
return arr == NULL ? NULL : |
|
279 |
(const char*) (*env)->GetByteArrayElements(env, arr, NULL); |
|
280 |
} |
|
281 |
||
282 |
static void |
|
283 |
releaseBytes(JNIEnv *env, jbyteArray arr, const char* parr) |
|
284 |
{ |
|
285 |
if (parr != NULL) |
|
286 |
(*env)->ReleaseByteArrayElements(env, arr, (jbyte*) parr, JNI_ABORT); |
|
287 |
} |
|
288 |
||
289 |
static void |
|
290 |
throwIOException(JNIEnv *env, int errnum, const char *defaultDetail) |
|
291 |
{ |
|
292 |
static const char * const format = "error=%d, %s"; |
|
293 |
const char *detail = defaultDetail; |
|
294 |
char *errmsg; |
|
295 |
jstring s; |
|
296 |
||
297 |
if (errnum != 0) { |
|
298 |
const char *s = strerror(errnum); |
|
299 |
if (strcmp(s, "Unknown error") != 0) |
|
300 |
detail = s; |
|
301 |
} |
|
302 |
/* ASCII Decimal representation uses 2.4 times as many bits as binary. */ |
|
303 |
errmsg = NEW(char, strlen(format) + strlen(detail) + 3 * sizeof(errnum)); |
|
16860
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
304 |
if (errmsg == NULL) |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
305 |
return; |
8fecebee12b0
8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
jzavgren
parents:
13149
diff
changeset
|
306 |
|
2 | 307 |
sprintf(errmsg, format, errnum, detail); |
308 |
s = JNU_NewStringPlatform(env, errmsg); |
|
309 |
if (s != NULL) { |
|
310 |
jobject x = JNU_NewObjectByName(env, "java/io/IOException", |
|
311 |
"(Ljava/lang/String;)V", s); |
|
312 |
if (x != NULL) |
|
313 |
(*env)->Throw(env, x); |
|
314 |
} |
|
315 |
free(errmsg); |
|
316 |
} |
|
317 |
||
318 |
#ifdef DEBUG_PROCESS |
|
319 |
/* Debugging process code is difficult; where to write debug output? */ |
|
320 |
static void |
|
321 |
debugPrint(char *format, ...) |
|
322 |
{ |
|
323 |
FILE *tty = fopen("/dev/tty", "w"); |
|
324 |
va_list ap; |
|
325 |
va_start(ap, format); |
|
326 |
vfprintf(tty, format, ap); |
|
327 |
va_end(ap); |
|
328 |
fclose(tty); |
|
329 |
} |
|
330 |
#endif /* DEBUG_PROCESS */ |
|
331 |
||
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
332 |
static void |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
333 |
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
|
334 |
{ |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
335 |
to[0] = from[0]; |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
336 |
to[1] = from[1]; |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
337 |
} |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
338 |
|
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
339 |
/* arg is an array of pointers to 0 terminated strings. array is terminated |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
340 |
* by a null element. |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
341 |
* |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
342 |
* *nelems and *nbytes receive the number of elements of array (incl 0) |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
343 |
* and total number of bytes (incl. 0) |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
344 |
* Note. An empty array will have one null element |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
345 |
* But if arg is null, then *nelems set to 0, and *nbytes to 0 |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
346 |
*/ |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
347 |
static void arraysize(const char * const *arg, int *nelems, int *nbytes) |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
348 |
{ |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
349 |
int i, bytes, count; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
350 |
const char * const *a = arg; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
351 |
char *p; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
352 |
int *q; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
353 |
if (arg == 0) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
354 |
*nelems = 0; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
355 |
*nbytes = 0; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
356 |
return; |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
357 |
} |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
358 |
/* count the array elements and number of bytes */ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
359 |
for (count=0, bytes=0; *a != 0; count++, a++) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
360 |
bytes += strlen(*a)+1; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
361 |
} |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
362 |
*nbytes = bytes; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
363 |
*nelems = count+1; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
364 |
} |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
365 |
|
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
366 |
/* copy the strings from arg[] into buf, starting at given offset |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
367 |
* return new offset to next free byte |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
368 |
*/ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
369 |
static int copystrings(char *buf, int offset, const char * const *arg) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
370 |
char *p; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
371 |
const char * const *a; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
372 |
int count=0; |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
373 |
|
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
374 |
if (arg == 0) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
375 |
return offset; |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
376 |
} |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
377 |
for (p=buf+offset, a=arg; *a != 0; a++) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
378 |
int len = strlen(*a) +1; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
379 |
memcpy(p, *a, len); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
380 |
p += len; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
381 |
count += len; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
382 |
} |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
383 |
return offset+count; |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
384 |
} |
2 | 385 |
|
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
386 |
/** |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
387 |
* 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
|
388 |
* 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
|
389 |
*/ |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
390 |
#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
|
391 |
__attribute_noinline__ |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
392 |
#endif |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
393 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
394 |
#define START_CHILD_USE_CLONE 0 /* clone() currently disabled; see above. */ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
395 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
396 |
#ifdef START_CHILD_USE_CLONE |
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
397 |
static pid_t |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
398 |
cloneChild(ChildStuff *c) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
399 |
#ifdef __linux__ |
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
400 |
#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
|
401 |
/* |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
402 |
* See clone(2). |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
403 |
* 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
|
404 |
* 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
|
405 |
*/ |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
406 |
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
|
407 |
/* errno will be set to ENOMEM */ |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
408 |
return -1; |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
409 |
return clone(childProcess, |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
410 |
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
|
411 |
CLONE_VFORK | CLONE_VM | SIGCHLD, c); |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
412 |
#else |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
413 |
/* not available on Solaris / Mac */ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
414 |
assert(0); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
415 |
return -1; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
416 |
#endif |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
417 |
} |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
418 |
#endif |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
419 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
420 |
static pid_t |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
421 |
vforkChild(ChildStuff *c) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
422 |
volatile pid_t resultPid; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
423 |
|
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
424 |
/* |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
425 |
* 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
|
426 |
* 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
|
427 |
* 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
|
428 |
* 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
|
429 |
*/ |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
430 |
resultPid = vfork(); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
431 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
432 |
if (resultPid == 0) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
433 |
childProcess(c); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
434 |
} |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
435 |
assert(resultPid != 0); /* childProcess never returns */ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
436 |
return resultPid; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
437 |
} |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
438 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
439 |
static pid_t |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
440 |
forkChild(ChildStuff *c) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
441 |
pid_t resultPid; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
442 |
|
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
443 |
/* |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
444 |
* 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
|
445 |
* 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
|
446 |
* 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
|
447 |
* behavior for fork(). |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
448 |
*/ |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
449 |
resultPid = fork(); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
450 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
451 |
if (resultPid == 0) { |
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
452 |
childProcess(c); |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
453 |
} |
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
454 |
assert(resultPid != 0); /* childProcess never returns */ |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
455 |
return resultPid; |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
456 |
} |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
457 |
|
22597
7515a991bb37
8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
19399
diff
changeset
|
458 |
#if defined(__solaris__) || defined(_ALLBSD_SOURCE) || defined(_AIX) |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
459 |
static pid_t |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
460 |
spawnChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
461 |
pid_t resultPid; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
462 |
jboolean isCopy; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
463 |
int i, offset, rval, bufsize, magic; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
464 |
char *buf, buf1[16]; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
465 |
char *hlpargs[2]; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
466 |
SpawnInfo sp; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
467 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
468 |
/* need to tell helper which fd is for receiving the childstuff |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
469 |
* and which fd to send response back on |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
470 |
*/ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
471 |
snprintf(buf1, sizeof(buf1), "%d:%d", c->childenv[0], c->fail[1]); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
472 |
/* put the fd string as argument to the helper cmd */ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
473 |
hlpargs[0] = buf1; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
474 |
hlpargs[1] = 0; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
475 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
476 |
/* Following items are sent down the pipe to the helper |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
477 |
* after it is spawned. |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
478 |
* All strings are null terminated. All arrays of strings |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
479 |
* have an empty string for termination. |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
480 |
* - the ChildStuff struct |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
481 |
* - the SpawnInfo struct |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
482 |
* - the argv strings array |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
483 |
* - the envv strings array |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
484 |
* - the home directory string |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
485 |
* - the parentPath string |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
486 |
* - the parentPathv array |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
487 |
*/ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
488 |
/* First calculate the sizes */ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
489 |
arraysize(c->argv, &sp.nargv, &sp.argvBytes); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
490 |
bufsize = sp.argvBytes; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
491 |
arraysize(c->envv, &sp.nenvv, &sp.envvBytes); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
492 |
bufsize += sp.envvBytes; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
493 |
sp.dirlen = c->pdir == 0 ? 0 : strlen(c->pdir)+1; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
494 |
bufsize += sp.dirlen; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
495 |
arraysize(parentPathv, &sp.nparentPathv, &sp.parentPathvBytes); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
496 |
bufsize += sp.parentPathvBytes; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
497 |
/* We need to clear FD_CLOEXEC if set in the fds[]. |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
498 |
* Files are created FD_CLOEXEC in Java. |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
499 |
* Otherwise, they will be closed when the target gets exec'd */ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
500 |
for (i=0; i<3; i++) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
501 |
if (c->fds[i] != -1) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
502 |
int flags = fcntl(c->fds[i], F_GETFD); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
503 |
if (flags & FD_CLOEXEC) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
504 |
fcntl(c->fds[i], F_SETFD, flags & (~1)); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
505 |
} |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
506 |
} |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
507 |
} |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
508 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
509 |
rval = posix_spawn(&resultPid, helperpath, 0, 0, (char * const *) hlpargs, environ); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
510 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
511 |
if (rval != 0) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
512 |
return -1; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
513 |
} |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
514 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
515 |
/* now the lengths are known, copy the data */ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
516 |
buf = NEW(char, bufsize); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
517 |
if (buf == 0) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
518 |
return -1; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
519 |
} |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
520 |
offset = copystrings(buf, 0, &c->argv[0]); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
521 |
offset = copystrings(buf, offset, &c->envv[0]); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
522 |
memcpy(buf+offset, c->pdir, sp.dirlen); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
523 |
offset += sp.dirlen; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
524 |
offset = copystrings(buf, offset, parentPathv); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
525 |
assert(offset == bufsize); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
526 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
527 |
magic = magicNumber(); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
528 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
529 |
/* write the two structs and the data buffer */ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
530 |
write(c->childenv[1], (char *)&magic, sizeof(magic)); // magic number first |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
531 |
write(c->childenv[1], (char *)c, sizeof(*c)); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
532 |
write(c->childenv[1], (char *)&sp, sizeof(sp)); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
533 |
write(c->childenv[1], buf, bufsize); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
534 |
free(buf); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
535 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
536 |
/* In this mode an external main() in invoked which calls back into |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
537 |
* childProcess() in this file, rather than directly |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
538 |
* via the statement below */ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
539 |
return resultPid; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
540 |
} |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
541 |
#endif |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
542 |
|
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
543 |
/* |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
544 |
* Start a child process running function childProcess. |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
545 |
* This function only returns in the parent. |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
546 |
*/ |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
547 |
static pid_t |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
548 |
startChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
549 |
switch (c->mode) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
550 |
case MODE_VFORK: |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
551 |
return vforkChild(c); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
552 |
case MODE_FORK: |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
553 |
return forkChild(c); |
22597
7515a991bb37
8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
19399
diff
changeset
|
554 |
#if defined(__solaris__) || defined(_ALLBSD_SOURCE) || defined(_AIX) |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
555 |
case MODE_POSIX_SPAWN: |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
556 |
return spawnChild(env, process, c, helperpath); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
557 |
#endif |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
558 |
default: |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
559 |
return -1; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
560 |
} |
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
561 |
} |
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
562 |
|
2 | 563 |
JNIEXPORT jint JNICALL |
564 |
Java_java_lang_UNIXProcess_forkAndExec(JNIEnv *env, |
|
565 |
jobject process, |
|
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
566 |
jint mode, |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
567 |
jbyteArray helperpath, |
2 | 568 |
jbyteArray prog, |
569 |
jbyteArray argBlock, jint argc, |
|
570 |
jbyteArray envBlock, jint envc, |
|
571 |
jbyteArray dir, |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
572 |
jintArray std_fds, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
573 |
jboolean redirectErrorStream) |
2 | 574 |
{ |
575 |
int errnum; |
|
576 |
int resultPid = -1; |
|
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
577 |
int in[2], out[2], err[2], fail[2], childenv[2]; |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
578 |
jint *fds = NULL; |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
579 |
const char *phelperpath = NULL; |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
580 |
const char *pprog = NULL; |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
581 |
const char *pargBlock = NULL; |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
582 |
const char *penvBlock = NULL; |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
583 |
ChildStuff *c; |
2 | 584 |
|
585 |
in[0] = in[1] = out[0] = out[1] = err[0] = err[1] = fail[0] = fail[1] = -1; |
|
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
586 |
childenv[0] = childenv[1] = -1; |
2 | 587 |
|
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
588 |
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
|
589 |
c->argv = NULL; |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
590 |
c->envv = NULL; |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
591 |
c->pdir = NULL; |
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
592 |
c->clone_stack = NULL; |
2 | 593 |
|
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
594 |
/* 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
|
595 |
* 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
|
596 |
* execve_as_traditional_shell_script. |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
597 |
* This word is also used when using spawn mode |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
598 |
*/ |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
599 |
assert(prog != NULL && argBlock != NULL); |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
600 |
if ((phelperpath = getBytes(env, helperpath)) == NULL) goto Catch; |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
601 |
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
|
602 |
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
|
603 |
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
|
604 |
c->argv[0] = pprog; |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
605 |
c->argc = argc + 2; |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
606 |
initVectorFromBlock(c->argv+1, pargBlock, argc); |
2 | 607 |
|
608 |
if (envBlock != NULL) { |
|
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
609 |
/* 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
|
610 |
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
|
611 |
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
|
612 |
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
|
613 |
} |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
614 |
|
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
615 |
if (dir != NULL) { |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
616 |
if ((c->pdir = getBytes(env, dir)) == NULL) goto Catch; |
2 | 617 |
} |
618 |
||
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
619 |
assert(std_fds != NULL); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
620 |
fds = (*env)->GetIntArrayElements(env, std_fds, NULL); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
621 |
if (fds == NULL) goto Catch; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
622 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
623 |
if ((fds[0] == -1 && pipe(in) < 0) || |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
624 |
(fds[1] == -1 && pipe(out) < 0) || |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
625 |
(fds[2] == -1 && pipe(err) < 0) || |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
626 |
(pipe(childenv) < 0) || |
2 | 627 |
(pipe(fail) < 0)) { |
628 |
throwIOException(env, errno, "Bad file descriptor"); |
|
629 |
goto Catch; |
|
630 |
} |
|
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
631 |
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
|
632 |
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
|
633 |
c->fds[2] = fds[2]; |
2 | 634 |
|
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
635 |
copyPipe(in, c->in); |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
636 |
copyPipe(out, c->out); |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
637 |
copyPipe(err, c->err); |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
638 |
copyPipe(fail, c->fail); |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
639 |
copyPipe(childenv, c->childenv); |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
640 |
|
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
641 |
c->redirectErrorStream = redirectErrorStream; |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
642 |
c->mode = mode; |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
643 |
|
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
644 |
resultPid = startChild(env, process, c, phelperpath); |
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
645 |
assert(resultPid != 0); |
2946
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
646 |
|
2 | 647 |
if (resultPid < 0) { |
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
648 |
switch (c->mode) { |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
649 |
case MODE_VFORK: |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
650 |
throwIOException(env, errno, "vfork failed"); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
651 |
break; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
652 |
case MODE_FORK: |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
653 |
throwIOException(env, errno, "fork failed"); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
654 |
break; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
655 |
case MODE_POSIX_SPAWN: |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
656 |
throwIOException(env, errno, "spawn failed"); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
657 |
break; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
658 |
} |
2 | 659 |
goto Catch; |
660 |
} |
|
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
661 |
close(fail[1]); fail[1] = -1; /* See: WhyCantJohnnyExec (childproc.c) */ |
50
a437b3f9d7f4
6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents:
48
diff
changeset
|
662 |
|
a437b3f9d7f4
6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents:
48
diff
changeset
|
663 |
switch (readFully(fail[0], &errnum, sizeof(errnum))) { |
a437b3f9d7f4
6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents:
48
diff
changeset
|
664 |
case 0: break; /* Exec succeeded */ |
a437b3f9d7f4
6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents:
48
diff
changeset
|
665 |
case sizeof(errnum): |
2 | 666 |
waitpid(resultPid, NULL, 0); |
667 |
throwIOException(env, errnum, "Exec failed"); |
|
668 |
goto Catch; |
|
50
a437b3f9d7f4
6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents:
48
diff
changeset
|
669 |
default: |
a437b3f9d7f4
6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents:
48
diff
changeset
|
670 |
throwIOException(env, errno, "Read failed"); |
a437b3f9d7f4
6671051: (process) Runtime.exec() hangs if signalled during fork/exec
martin
parents:
48
diff
changeset
|
671 |
goto Catch; |
2 | 672 |
} |
673 |
||
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
674 |
fds[0] = (in [1] != -1) ? in [1] : -1; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
675 |
fds[1] = (out[0] != -1) ? out[0] : -1; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
676 |
fds[2] = (err[0] != -1) ? err[0] : -1; |
2 | 677 |
|
678 |
Finally: |
|
3430
8d6d03175761
6868160: (process) Use vfork, not fork, on Linux to avoid swap exhaustion
martin
parents:
3421
diff
changeset
|
679 |
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
|
680 |
|
2 | 681 |
/* Always clean up the child's side of the pipes */ |
682 |
closeSafely(in [0]); |
|
683 |
closeSafely(out[1]); |
|
684 |
closeSafely(err[1]); |
|
685 |
||
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
686 |
/* Always clean up fail and childEnv descriptors */ |
2 | 687 |
closeSafely(fail[0]); |
688 |
closeSafely(fail[1]); |
|
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
689 |
closeSafely(childenv[0]); |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
690 |
closeSafely(childenv[1]); |
2 | 691 |
|
692 |
releaseBytes(env, prog, pprog); |
|
693 |
releaseBytes(env, argBlock, pargBlock); |
|
694 |
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
|
695 |
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
|
696 |
|
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
697 |
free(c->argv); |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
698 |
free(c->envv); |
f95752c3204a
6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents:
1936
diff
changeset
|
699 |
free(c); |
2 | 700 |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
701 |
if (fds != NULL) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
702 |
(*env)->ReleaseIntArrayElements(env, std_fds, fds, 0); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
703 |
|
2 | 704 |
return resultPid; |
705 |
||
706 |
Catch: |
|
707 |
/* Clean up the parent's side of the pipes in case of failure only */ |
|
19399
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
708 |
closeSafely(in [1]); in[1] = -1; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
709 |
closeSafely(out[0]); out[0] = -1; |
e2e5122cd62e
5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents:
16860
diff
changeset
|
710 |
closeSafely(err[0]); err[0] = -1; |
2 | 711 |
goto Finally; |
712 |
} |
|
713 |
||
714 |
JNIEXPORT void JNICALL |
|
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
12047
diff
changeset
|
715 |
Java_java_lang_UNIXProcess_destroyProcess(JNIEnv *env, |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
12047
diff
changeset
|
716 |
jobject junk, |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
12047
diff
changeset
|
717 |
jint pid, |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
12047
diff
changeset
|
718 |
jboolean force) |
2 | 719 |
{ |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
12047
diff
changeset
|
720 |
int sig = (force == JNI_TRUE) ? SIGKILL : SIGTERM; |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
12047
diff
changeset
|
721 |
kill(pid, sig); |
2 | 722 |
} |