author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 48 | dc5744ca15ea |
child 5786 | f60ef38202e7 |
permissions | -rw-r--r-- |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
1 |
/* |
5506 | 2 |
* Copyright (c) 1995, 2008, 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 |
package java.lang; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
||
30 |
/* java.lang.Process subclass in the UNIX environment. |
|
31 |
* |
|
32 |
* @author Mario Wolczko and Ross Knippel. |
|
33 |
* @author Konstantin Kladko (ported to Linux) |
|
34 |
*/ |
|
35 |
||
36 |
final class UNIXProcess extends Process { |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
37 |
private static final sun.misc.JavaIOFileDescriptorAccess fdAccess |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
38 |
= sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess(); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
39 |
|
2 | 40 |
private int pid; |
41 |
private int exitcode; |
|
42 |
private boolean hasExited; |
|
43 |
||
44 |
private OutputStream stdin_stream; |
|
45 |
private InputStream stdout_stream; |
|
46 |
private InputStream stderr_stream; |
|
47 |
||
48 |
/* this is for the reaping thread */ |
|
49 |
private native int waitForProcessExit(int pid); |
|
50 |
||
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
51 |
/** |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
52 |
* Create a process using fork(2) and exec(2). |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
53 |
* |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
54 |
* @param std_fds array of file descriptors. Indexes 0, 1, and |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
55 |
* 2 correspond to standard input, standard output and |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
56 |
* standard error, respectively. On input, a value of -1 |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
57 |
* means to create a pipe to connect child and parent |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
58 |
* processes. On output, a value which is not -1 is the |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
59 |
* parent pipe fd corresponding to the pipe which has |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
60 |
* been created. An element of this array is -1 on input |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
61 |
* if and only if it is <em>not</em> -1 on output. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
62 |
* @return the pid of the subprocess |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
63 |
*/ |
2 | 64 |
private native int forkAndExec(byte[] prog, |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
65 |
byte[] argBlock, int argc, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
66 |
byte[] envBlock, int envc, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
67 |
byte[] dir, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
68 |
int[] std_fds, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
69 |
boolean redirectErrorStream) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
70 |
throws IOException; |
2 | 71 |
|
72 |
/* In the process constructor we wait on this gate until the process */ |
|
73 |
/* has been created. Then we return from the constructor. */ |
|
74 |
/* fork() is called by the same thread which later waits for the process */ |
|
75 |
/* to terminate */ |
|
76 |
||
77 |
private static class Gate { |
|
78 |
||
79 |
private boolean exited = false; |
|
80 |
private IOException savedException; |
|
81 |
||
82 |
synchronized void exit() { /* Opens the gate */ |
|
83 |
exited = true; |
|
84 |
this.notify(); |
|
85 |
} |
|
86 |
||
87 |
synchronized void waitForExit() { /* wait until the gate is open */ |
|
88 |
boolean interrupted = false; |
|
89 |
while (!exited) { |
|
90 |
try { |
|
91 |
this.wait(); |
|
92 |
} catch (InterruptedException e) { |
|
93 |
interrupted = true; |
|
94 |
} |
|
95 |
} |
|
96 |
if (interrupted) { |
|
97 |
Thread.currentThread().interrupt(); |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
void setException (IOException e) { |
|
102 |
savedException = e; |
|
103 |
} |
|
104 |
||
105 |
IOException getException() { |
|
106 |
return savedException; |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
UNIXProcess(final byte[] prog, |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
111 |
final byte[] argBlock, final int argc, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
112 |
final byte[] envBlock, final int envc, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
113 |
final byte[] dir, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
114 |
final int[] std_fds, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
115 |
final boolean redirectErrorStream) |
2 | 116 |
throws IOException { |
117 |
||
118 |
final Gate gate = new Gate(); |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
119 |
/* |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
120 |
* For each subprocess forked a corresponding reaper thread |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
121 |
* is started. That thread is the only thread which waits |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
122 |
* for the subprocess to terminate and it doesn't hold any |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
123 |
* locks while doing so. This design allows waitFor() and |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
124 |
* exitStatus() to be safely executed in parallel (and they |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
125 |
* need no native code). |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
126 |
*/ |
2 | 127 |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
128 |
java.security.AccessController.doPrivileged( |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
129 |
new java.security.PrivilegedAction<Void>() { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
130 |
public Void run() { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
131 |
Thread t = new Thread("process reaper") { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
132 |
public void run() { |
2 | 133 |
try { |
134 |
pid = forkAndExec(prog, |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
135 |
argBlock, argc, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
136 |
envBlock, envc, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
137 |
dir, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
138 |
std_fds, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
139 |
redirectErrorStream); |
2 | 140 |
} catch (IOException e) { |
141 |
gate.setException(e); /*remember to rethrow later*/ |
|
142 |
gate.exit(); |
|
143 |
return; |
|
144 |
} |
|
145 |
java.security.AccessController.doPrivileged( |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
146 |
new java.security.PrivilegedAction<Void>() { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
147 |
public Void run() { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
148 |
if (std_fds[0] == -1) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
149 |
stdin_stream = new ProcessBuilder.NullOutputStream(); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
150 |
else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
151 |
FileDescriptor stdin_fd = new FileDescriptor(); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
152 |
fdAccess.set(stdin_fd, std_fds[0]); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
153 |
stdin_stream = new BufferedOutputStream( |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
154 |
new FileOutputStream(stdin_fd)); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
155 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
156 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
157 |
if (std_fds[1] == -1) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
158 |
stdout_stream = new ProcessBuilder.NullInputStream(); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
159 |
else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
160 |
FileDescriptor stdout_fd = new FileDescriptor(); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
161 |
fdAccess.set(stdout_fd, std_fds[1]); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
162 |
stdout_stream = new BufferedInputStream( |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
163 |
new FileInputStream(stdout_fd)); |
2 | 164 |
} |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
165 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
166 |
if (std_fds[2] == -1) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
167 |
stderr_stream = new ProcessBuilder.NullInputStream(); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
168 |
else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
169 |
FileDescriptor stderr_fd = new FileDescriptor(); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
170 |
fdAccess.set(stderr_fd, std_fds[2]); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
171 |
stderr_stream = new FileInputStream(stderr_fd); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
172 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
173 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
174 |
return null; }}); |
2 | 175 |
gate.exit(); /* exit from constructor */ |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
176 |
int res = waitForProcessExit(pid); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
177 |
synchronized (UNIXProcess.this) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
178 |
hasExited = true; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
179 |
exitcode = res; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
180 |
UNIXProcess.this.notifyAll(); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
181 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
182 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
183 |
}; |
2 | 184 |
t.setDaemon(true); |
185 |
t.start(); |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
186 |
return null; }}); |
2 | 187 |
gate.waitForExit(); |
188 |
IOException e = gate.getException(); |
|
189 |
if (e != null) |
|
190 |
throw new IOException(e.toString()); |
|
191 |
} |
|
192 |
||
193 |
public OutputStream getOutputStream() { |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
194 |
return stdin_stream; |
2 | 195 |
} |
196 |
||
197 |
public InputStream getInputStream() { |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
198 |
return stdout_stream; |
2 | 199 |
} |
200 |
||
201 |
public InputStream getErrorStream() { |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
202 |
return stderr_stream; |
2 | 203 |
} |
204 |
||
205 |
public synchronized int waitFor() throws InterruptedException { |
|
206 |
while (!hasExited) { |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
207 |
wait(); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
208 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
209 |
return exitcode; |
2 | 210 |
} |
211 |
||
212 |
public synchronized int exitValue() { |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
213 |
if (!hasExited) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
214 |
throw new IllegalThreadStateException("process hasn't exited"); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
215 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
216 |
return exitcode; |
2 | 217 |
} |
218 |
||
219 |
private static native void destroyProcess(int pid); |
|
220 |
public void destroy() { |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
221 |
// There is a risk that pid will be recycled, causing us to |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
222 |
// kill the wrong process! So we only terminate processes |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
223 |
// that appear to still be running. Even with this check, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
224 |
// there is an unavoidable race condition here, but the window |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
225 |
// is very small, and OSes try hard to not recycle pids too |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
226 |
// soon, so this is quite safe. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
227 |
synchronized (this) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
228 |
if (!hasExited) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
229 |
destroyProcess(pid); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
230 |
} |
2 | 231 |
try { |
232 |
stdin_stream.close(); |
|
233 |
stdout_stream.close(); |
|
234 |
stderr_stream.close(); |
|
235 |
} catch (IOException e) { |
|
236 |
// ignore |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
/* This routine initializes JNI field offsets for the class */ |
|
241 |
private static native void initIDs(); |
|
242 |
||
243 |
static { |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
244 |
initIDs(); |
2 | 245 |
} |
246 |
} |