author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 7515 | 43202796198e |
child 10903 | dfb253adb28f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 1995, 2010, 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 |
||
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
28 |
import java.io.IOException; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
29 |
import java.io.File; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
30 |
import java.io.InputStream; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
31 |
import java.io.OutputStream; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
32 |
import java.io.FileInputStream; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
33 |
import java.io.FileOutputStream; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
34 |
import java.io.FileDescriptor; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
35 |
import java.io.BufferedInputStream; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
36 |
import java.io.BufferedOutputStream; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
37 |
import java.lang.ProcessBuilder.Redirect; |
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
38 |
import java.security.AccessController; |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
39 |
import java.security.PrivilegedAction; |
2 | 40 |
|
41 |
/* This class is for the exclusive use of ProcessBuilder.start() to |
|
42 |
* create new processes. |
|
43 |
* |
|
44 |
* @author Martin Buchholz |
|
45 |
* @since 1.5 |
|
46 |
*/ |
|
47 |
||
48 |
final class ProcessImpl extends Process { |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
49 |
private static final sun.misc.JavaIOFileDescriptorAccess fdAccess |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
50 |
= sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess(); |
2 | 51 |
|
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
52 |
/** |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
53 |
* Open a file for writing. If {@code append} is {@code true} then the file |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
54 |
* is opened for atomic append directly and a FileOutputStream constructed |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
55 |
* with the resulting handle. This is because a FileOutputStream created |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
56 |
* to append to a file does not open the file in a manner that guarantees |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
57 |
* that writes by the child process will be atomic. |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
58 |
*/ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
59 |
private static FileOutputStream newFileOutputStream(File f, boolean append) |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
60 |
throws IOException |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
61 |
{ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
62 |
if (append) { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
63 |
SecurityManager sm = System.getSecurityManager(); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
64 |
if (sm != null) |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
65 |
sm.checkWrite(f.getPath()); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
66 |
long handle = openForAtomicAppend(f.getPath()); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
67 |
final FileDescriptor fd = new FileDescriptor(); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
68 |
fdAccess.setHandle(fd, handle); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
69 |
return AccessController.doPrivileged( |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
70 |
new PrivilegedAction<FileOutputStream>() { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
71 |
public FileOutputStream run() { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
72 |
return new FileOutputStream(fd); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
73 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
74 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
75 |
); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
76 |
} else { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
77 |
return new FileOutputStream(f); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
78 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
79 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
80 |
|
2 | 81 |
// System-dependent portion of ProcessBuilder.start() |
82 |
static Process start(String cmdarray[], |
|
83 |
java.util.Map<String,String> environment, |
|
84 |
String dir, |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
85 |
ProcessBuilder.Redirect[] redirects, |
2 | 86 |
boolean redirectErrorStream) |
87 |
throws IOException |
|
88 |
{ |
|
89 |
String envblock = ProcessEnvironment.toEnvironmentBlock(environment); |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
90 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
91 |
FileInputStream f0 = null; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
92 |
FileOutputStream f1 = null; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
93 |
FileOutputStream f2 = null; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
94 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
95 |
try { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
96 |
long[] stdHandles; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
97 |
if (redirects == null) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
98 |
stdHandles = new long[] { -1L, -1L, -1L }; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
99 |
} else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
100 |
stdHandles = new long[3]; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
101 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
102 |
if (redirects[0] == Redirect.PIPE) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
103 |
stdHandles[0] = -1L; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
104 |
else if (redirects[0] == Redirect.INHERIT) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
105 |
stdHandles[0] = fdAccess.getHandle(FileDescriptor.in); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
106 |
else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
107 |
f0 = new FileInputStream(redirects[0].file()); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
108 |
stdHandles[0] = fdAccess.getHandle(f0.getFD()); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
109 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
110 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
111 |
if (redirects[1] == Redirect.PIPE) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
112 |
stdHandles[1] = -1L; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
113 |
else if (redirects[1] == Redirect.INHERIT) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
114 |
stdHandles[1] = fdAccess.getHandle(FileDescriptor.out); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
115 |
else { |
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
116 |
f1 = newFileOutputStream(redirects[1].file(), |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
117 |
redirects[1].append()); |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
118 |
stdHandles[1] = fdAccess.getHandle(f1.getFD()); |
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 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
121 |
if (redirects[2] == Redirect.PIPE) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
122 |
stdHandles[2] = -1L; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
123 |
else if (redirects[2] == Redirect.INHERIT) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
124 |
stdHandles[2] = fdAccess.getHandle(FileDescriptor.err); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
125 |
else { |
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
126 |
f2 = newFileOutputStream(redirects[2].file(), |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
127 |
redirects[2].append()); |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
128 |
stdHandles[2] = fdAccess.getHandle(f2.getFD()); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
129 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
130 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
131 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
132 |
return new ProcessImpl(cmdarray, envblock, dir, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
133 |
stdHandles, redirectErrorStream); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
134 |
} finally { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
135 |
// In theory, close() can throw IOException |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
136 |
// (although it is rather unlikely to happen here) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
137 |
try { if (f0 != null) f0.close(); } |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
138 |
finally { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
139 |
try { if (f1 != null) f1.close(); } |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
140 |
finally { if (f2 != null) f2.close(); } |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
141 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
142 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
143 |
|
2 | 144 |
} |
145 |
||
146 |
private long handle = 0; |
|
147 |
private OutputStream stdin_stream; |
|
148 |
private InputStream stdout_stream; |
|
149 |
private InputStream stderr_stream; |
|
150 |
||
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
151 |
private ProcessImpl(final String cmd[], |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
152 |
final String envblock, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
153 |
final String path, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
154 |
final long[] stdHandles, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
155 |
final boolean redirectErrorStream) |
2 | 156 |
throws IOException |
157 |
{ |
|
158 |
// Win32 CreateProcess requires cmd[0] to be normalized |
|
159 |
cmd[0] = new File(cmd[0]).getPath(); |
|
160 |
||
161 |
StringBuilder cmdbuf = new StringBuilder(80); |
|
162 |
for (int i = 0; i < cmd.length; i++) { |
|
163 |
if (i > 0) { |
|
164 |
cmdbuf.append(' '); |
|
165 |
} |
|
166 |
String s = cmd[i]; |
|
167 |
if (s.indexOf(' ') >= 0 || s.indexOf('\t') >= 0) { |
|
168 |
if (s.charAt(0) != '"') { |
|
169 |
cmdbuf.append('"'); |
|
170 |
cmdbuf.append(s); |
|
171 |
if (s.endsWith("\\")) { |
|
172 |
cmdbuf.append("\\"); |
|
173 |
} |
|
174 |
cmdbuf.append('"'); |
|
175 |
} else if (s.endsWith("\"")) { |
|
176 |
/* The argument has already been quoted. */ |
|
177 |
cmdbuf.append(s); |
|
178 |
} else { |
|
179 |
/* Unmatched quote for the argument. */ |
|
180 |
throw new IllegalArgumentException(); |
|
181 |
} |
|
182 |
} else { |
|
183 |
cmdbuf.append(s); |
|
184 |
} |
|
185 |
} |
|
186 |
String cmdstr = cmdbuf.toString(); |
|
187 |
||
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
188 |
handle = create(cmdstr, envblock, path, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
189 |
stdHandles, redirectErrorStream); |
2 | 190 |
|
191 |
java.security.AccessController.doPrivileged( |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
192 |
new java.security.PrivilegedAction<Void>() { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
193 |
public Void run() { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
194 |
if (stdHandles[0] == -1L) |
5787
a0af7b8e80ed
6960898: Regression due to src/share/classes/java/lang/ProcessBuilder.java changes
martin
parents:
5506
diff
changeset
|
195 |
stdin_stream = ProcessBuilder.NullOutputStream.INSTANCE; |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
196 |
else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
197 |
FileDescriptor stdin_fd = new FileDescriptor(); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
198 |
fdAccess.setHandle(stdin_fd, stdHandles[0]); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
199 |
stdin_stream = new BufferedOutputStream( |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
200 |
new FileOutputStream(stdin_fd)); |
2 | 201 |
} |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
202 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
203 |
if (stdHandles[1] == -1L) |
5787
a0af7b8e80ed
6960898: Regression due to src/share/classes/java/lang/ProcessBuilder.java changes
martin
parents:
5506
diff
changeset
|
204 |
stdout_stream = ProcessBuilder.NullInputStream.INSTANCE; |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
205 |
else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
206 |
FileDescriptor stdout_fd = new FileDescriptor(); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
207 |
fdAccess.setHandle(stdout_fd, stdHandles[1]); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
208 |
stdout_stream = new BufferedInputStream( |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
209 |
new FileInputStream(stdout_fd)); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
210 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
211 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
212 |
if (stdHandles[2] == -1L) |
5787
a0af7b8e80ed
6960898: Regression due to src/share/classes/java/lang/ProcessBuilder.java changes
martin
parents:
5506
diff
changeset
|
213 |
stderr_stream = ProcessBuilder.NullInputStream.INSTANCE; |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
214 |
else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
215 |
FileDescriptor stderr_fd = new FileDescriptor(); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
216 |
fdAccess.setHandle(stderr_fd, stdHandles[2]); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
217 |
stderr_stream = new FileInputStream(stderr_fd); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
218 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
219 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
220 |
return null; }}); |
2 | 221 |
} |
222 |
||
223 |
public OutputStream getOutputStream() { |
|
224 |
return stdin_stream; |
|
225 |
} |
|
226 |
||
227 |
public InputStream getInputStream() { |
|
228 |
return stdout_stream; |
|
229 |
} |
|
230 |
||
231 |
public InputStream getErrorStream() { |
|
232 |
return stderr_stream; |
|
233 |
} |
|
234 |
||
235 |
public void finalize() { |
|
236 |
closeHandle(handle); |
|
237 |
} |
|
238 |
||
239 |
private static final int STILL_ACTIVE = getStillActive(); |
|
240 |
private static native int getStillActive(); |
|
241 |
||
242 |
public int exitValue() { |
|
243 |
int exitCode = getExitCodeProcess(handle); |
|
244 |
if (exitCode == STILL_ACTIVE) |
|
245 |
throw new IllegalThreadStateException("process has not exited"); |
|
246 |
return exitCode; |
|
247 |
} |
|
248 |
private static native int getExitCodeProcess(long handle); |
|
249 |
||
250 |
public int waitFor() throws InterruptedException { |
|
251 |
waitForInterruptibly(handle); |
|
252 |
if (Thread.interrupted()) |
|
253 |
throw new InterruptedException(); |
|
254 |
return exitValue(); |
|
255 |
} |
|
256 |
private static native void waitForInterruptibly(long handle); |
|
257 |
||
258 |
public void destroy() { terminateProcess(handle); } |
|
259 |
private static native void terminateProcess(long handle); |
|
260 |
||
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
261 |
/** |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
262 |
* Create a process using the win32 function CreateProcess. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
263 |
* |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
264 |
* @param cmdstr the Windows commandline |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
265 |
* @param envblock NUL-separated, double-NUL-terminated list of |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
266 |
* environment strings in VAR=VALUE form |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
267 |
* @param dir the working directory of the process, or null if |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
268 |
* inheriting the current directory from the parent process |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
269 |
* @param stdHandles array of windows HANDLEs. Indexes 0, 1, and |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
270 |
* 2 correspond to standard input, standard output and |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
271 |
* standard error, respectively. On input, a value of -1 |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
272 |
* means to create a pipe to connect child and parent |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
273 |
* 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
|
274 |
* parent pipe handle corresponding to the pipe which has |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
275 |
* 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
|
276 |
* 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
|
277 |
* @param redirectErrorStream redirectErrorStream attribute |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
278 |
* @return the native subprocess HANDLE returned by CreateProcess |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
279 |
*/ |
2 | 280 |
private static native long create(String cmdstr, |
281 |
String envblock, |
|
282 |
String dir, |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
283 |
long[] stdHandles, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
284 |
boolean redirectErrorStream) |
2 | 285 |
throws IOException; |
286 |
||
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
287 |
/** |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
288 |
* Opens a file for atomic append. The file is created if it doesn't |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
289 |
* already exist. |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
290 |
* |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
291 |
* @param file the file to open or create |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
292 |
* @return the native HANDLE |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
293 |
*/ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
294 |
private static native long openForAtomicAppend(String path) |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
295 |
throws IOException; |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5787
diff
changeset
|
296 |
|
2 | 297 |
private static native boolean closeHandle(long handle); |
298 |
} |