2
|
1 |
/*
|
|
2 |
* Copyright 1995-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
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 {
|
|
37 |
private FileDescriptor stdin_fd;
|
|
38 |
private FileDescriptor stdout_fd;
|
|
39 |
private FileDescriptor stderr_fd;
|
|
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 |
|
|
51 |
private native int forkAndExec(byte[] prog,
|
|
52 |
byte[] argBlock, int argc,
|
|
53 |
byte[] envBlock, int envc,
|
|
54 |
byte[] dir,
|
|
55 |
boolean redirectErrorStream,
|
|
56 |
FileDescriptor stdin_fd,
|
|
57 |
FileDescriptor stdout_fd,
|
|
58 |
FileDescriptor stderr_fd)
|
|
59 |
throws IOException;
|
|
60 |
|
|
61 |
/* In the process constructor we wait on this gate until the process */
|
|
62 |
/* has been created. Then we return from the constructor. */
|
|
63 |
/* fork() is called by the same thread which later waits for the process */
|
|
64 |
/* to terminate */
|
|
65 |
|
|
66 |
private static class Gate {
|
|
67 |
|
|
68 |
private boolean exited = false;
|
|
69 |
private IOException savedException;
|
|
70 |
|
|
71 |
synchronized void exit() { /* Opens the gate */
|
|
72 |
exited = true;
|
|
73 |
this.notify();
|
|
74 |
}
|
|
75 |
|
|
76 |
synchronized void waitForExit() { /* wait until the gate is open */
|
|
77 |
boolean interrupted = false;
|
|
78 |
while (!exited) {
|
|
79 |
try {
|
|
80 |
this.wait();
|
|
81 |
} catch (InterruptedException e) {
|
|
82 |
interrupted = true;
|
|
83 |
}
|
|
84 |
}
|
|
85 |
if (interrupted) {
|
|
86 |
Thread.currentThread().interrupt();
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
void setException (IOException e) {
|
|
91 |
savedException = e;
|
|
92 |
}
|
|
93 |
|
|
94 |
IOException getException() {
|
|
95 |
return savedException;
|
|
96 |
}
|
|
97 |
}
|
|
98 |
|
|
99 |
UNIXProcess(final byte[] prog,
|
|
100 |
final byte[] argBlock, final int argc,
|
|
101 |
final byte[] envBlock, final int envc,
|
|
102 |
final byte[] dir,
|
|
103 |
final boolean redirectErrorStream)
|
|
104 |
throws IOException {
|
|
105 |
stdin_fd = new FileDescriptor();
|
|
106 |
stdout_fd = new FileDescriptor();
|
|
107 |
stderr_fd = new FileDescriptor();
|
|
108 |
|
|
109 |
final Gate gate = new Gate();
|
|
110 |
/*
|
|
111 |
* For each subprocess forked a corresponding reaper thread
|
|
112 |
* is started. That thread is the only thread which waits
|
|
113 |
* for the subprocess to terminate and it doesn't hold any
|
|
114 |
* locks while doing so. This design allows waitFor() and
|
|
115 |
* exitStatus() to be safely executed in parallel (and they
|
|
116 |
* need no native code).
|
|
117 |
*/
|
|
118 |
|
|
119 |
java.security.AccessController.doPrivileged(
|
|
120 |
new java.security.PrivilegedAction() {
|
|
121 |
public Object run() {
|
|
122 |
Thread t = new Thread("process reaper") {
|
|
123 |
public void run() {
|
|
124 |
try {
|
|
125 |
pid = forkAndExec(prog,
|
|
126 |
argBlock, argc,
|
|
127 |
envBlock, envc,
|
|
128 |
dir,
|
|
129 |
redirectErrorStream,
|
|
130 |
stdin_fd, stdout_fd, stderr_fd);
|
|
131 |
} catch (IOException e) {
|
|
132 |
gate.setException(e); /*remember to rethrow later*/
|
|
133 |
gate.exit();
|
|
134 |
return;
|
|
135 |
}
|
|
136 |
java.security.AccessController.doPrivileged(
|
|
137 |
new java.security.PrivilegedAction() {
|
|
138 |
public Object run() {
|
|
139 |
stdin_stream = new BufferedOutputStream(new
|
|
140 |
FileOutputStream(stdin_fd));
|
|
141 |
stdout_stream = new BufferedInputStream(new
|
|
142 |
FileInputStream(stdout_fd));
|
|
143 |
stderr_stream = new FileInputStream(stderr_fd);
|
|
144 |
return null;
|
|
145 |
}
|
|
146 |
});
|
|
147 |
gate.exit(); /* exit from constructor */
|
|
148 |
int res = waitForProcessExit(pid);
|
|
149 |
synchronized (UNIXProcess.this) {
|
|
150 |
hasExited = true;
|
|
151 |
exitcode = res;
|
|
152 |
UNIXProcess.this.notifyAll();
|
|
153 |
}
|
|
154 |
}
|
|
155 |
};
|
|
156 |
t.setDaemon(true);
|
|
157 |
t.start();
|
|
158 |
return null;
|
|
159 |
}
|
|
160 |
});
|
|
161 |
gate.waitForExit();
|
|
162 |
IOException e = gate.getException();
|
|
163 |
if (e != null)
|
|
164 |
throw new IOException(e.toString());
|
|
165 |
}
|
|
166 |
|
|
167 |
public OutputStream getOutputStream() {
|
|
168 |
return stdin_stream;
|
|
169 |
}
|
|
170 |
|
|
171 |
public InputStream getInputStream() {
|
|
172 |
return stdout_stream;
|
|
173 |
}
|
|
174 |
|
|
175 |
public InputStream getErrorStream() {
|
|
176 |
return stderr_stream;
|
|
177 |
}
|
|
178 |
|
|
179 |
public synchronized int waitFor() throws InterruptedException {
|
|
180 |
while (!hasExited) {
|
|
181 |
wait();
|
|
182 |
}
|
|
183 |
return exitcode;
|
|
184 |
}
|
|
185 |
|
|
186 |
public synchronized int exitValue() {
|
|
187 |
if (!hasExited) {
|
|
188 |
throw new IllegalThreadStateException("process hasn't exited");
|
|
189 |
}
|
|
190 |
return exitcode;
|
|
191 |
}
|
|
192 |
|
|
193 |
private static native void destroyProcess(int pid);
|
|
194 |
public void destroy() {
|
|
195 |
// There is a risk that pid will be recycled, causing us to
|
|
196 |
// kill the wrong process! So we only terminate processes
|
|
197 |
// that appear to still be running. Even with this check,
|
|
198 |
// there is an unavoidable race condition here, but the window
|
|
199 |
// is very small, and OSes try hard to not recycle pids too
|
|
200 |
// soon, so this is quite safe.
|
|
201 |
synchronized (this) {
|
|
202 |
if (!hasExited)
|
|
203 |
destroyProcess(pid);
|
|
204 |
}
|
|
205 |
try {
|
|
206 |
stdin_stream.close();
|
|
207 |
stdout_stream.close();
|
|
208 |
stderr_stream.close();
|
|
209 |
} catch (IOException e) {
|
|
210 |
// ignore
|
|
211 |
}
|
|
212 |
}
|
|
213 |
|
|
214 |
/* This routine initializes JNI field offsets for the class */
|
|
215 |
private static native void initIDs();
|
|
216 |
|
|
217 |
static {
|
|
218 |
initIDs();
|
|
219 |
}
|
|
220 |
}
|