author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 7515 | 43202796198e |
child 13149 | 27d52f97a5cc |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 1997, 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 |
#include <assert.h> |
|
27 |
#include "java_lang_ProcessImpl.h" |
|
28 |
||
29 |
#include "jni.h" |
|
30 |
#include "jvm.h" |
|
31 |
#include "jni_util.h" |
|
32 |
#include "io_util.h" |
|
33 |
#include <windows.h> |
|
34 |
#include <io.h> |
|
35 |
||
43
bfccfd41f0fc
6631966: (process) Raise Windows pipe buffer size an extra 24 bytes (win)
martin
parents:
2
diff
changeset
|
36 |
/* We try to make sure that we can read and write 4095 bytes (the |
bfccfd41f0fc
6631966: (process) Raise Windows pipe buffer size an extra 24 bytes (win)
martin
parents:
2
diff
changeset
|
37 |
* fixed limit on Linux) to the pipe on all operating systems without |
bfccfd41f0fc
6631966: (process) Raise Windows pipe buffer size an extra 24 bytes (win)
martin
parents:
2
diff
changeset
|
38 |
* deadlock. Windows 2000 inexplicably appears to need an extra 24 |
bfccfd41f0fc
6631966: (process) Raise Windows pipe buffer size an extra 24 bytes (win)
martin
parents:
2
diff
changeset
|
39 |
* bytes of slop to avoid deadlock. |
bfccfd41f0fc
6631966: (process) Raise Windows pipe buffer size an extra 24 bytes (win)
martin
parents:
2
diff
changeset
|
40 |
*/ |
bfccfd41f0fc
6631966: (process) Raise Windows pipe buffer size an extra 24 bytes (win)
martin
parents:
2
diff
changeset
|
41 |
#define PIPE_SIZE (4096+24) |
2 | 42 |
|
43 |
char * |
|
44 |
extractExecutablePath(JNIEnv *env, char *source) |
|
45 |
{ |
|
46 |
char *p, *r; |
|
47 |
||
48 |
/* If no spaces, then use entire thing */ |
|
49 |
if ((p = strchr(source, ' ')) == NULL) |
|
50 |
return source; |
|
51 |
||
52 |
/* If no quotes, or quotes after space, return up to space */ |
|
53 |
if (((r = strchr(source, '"')) == NULL) || (r > p)) { |
|
54 |
*p = 0; |
|
55 |
return source; |
|
56 |
} |
|
57 |
||
58 |
/* Quotes before space, return up to space after next quotes */ |
|
59 |
p = strchr(r, '"'); |
|
60 |
if ((p = strchr(p, ' ')) == NULL) |
|
61 |
return source; |
|
62 |
*p = 0; |
|
63 |
return source; |
|
64 |
} |
|
65 |
||
66 |
DWORD |
|
67 |
selectProcessFlag(JNIEnv *env, jstring cmd0) |
|
68 |
{ |
|
69 |
char buf[MAX_PATH]; |
|
70 |
DWORD newFlag = 0; |
|
71 |
char *exe, *p, *name; |
|
72 |
unsigned char buffer[2]; |
|
73 |
long headerLoc = 0; |
|
74 |
int fd = 0; |
|
75 |
||
76 |
exe = (char *)JNU_GetStringPlatformChars(env, cmd0, 0); |
|
77 |
exe = extractExecutablePath(env, exe); |
|
78 |
||
79 |
if (exe != NULL) { |
|
80 |
if ((p = strchr(exe, '\\')) == NULL) { |
|
81 |
SearchPath(NULL, exe, ".exe", MAX_PATH, buf, &name); |
|
82 |
} else { |
|
83 |
p = strrchr(exe, '\\'); |
|
84 |
*p = 0; |
|
85 |
p++; |
|
86 |
SearchPath(exe, p, ".exe", MAX_PATH, buf, &name); |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
fd = _open(buf, _O_RDONLY); |
|
91 |
if (fd > 0) { |
|
92 |
_read(fd, buffer, 2); |
|
93 |
if (buffer[0] == 'M' && buffer[1] == 'Z') { |
|
94 |
_lseek(fd, 60L, SEEK_SET); |
|
95 |
_read(fd, buffer, 2); |
|
96 |
headerLoc = (long)buffer[1] << 8 | (long)buffer[0]; |
|
97 |
_lseek(fd, headerLoc, SEEK_SET); |
|
98 |
_read(fd, buffer, 2); |
|
99 |
if (buffer[0] == 'P' && buffer[1] == 'E') { |
|
100 |
newFlag = DETACHED_PROCESS; |
|
101 |
} |
|
102 |
} |
|
103 |
_close(fd); |
|
104 |
} |
|
105 |
JNU_ReleaseStringPlatformChars(env, cmd0, exe); |
|
106 |
return newFlag; |
|
107 |
} |
|
108 |
||
109 |
static void |
|
110 |
win32Error(JNIEnv *env, const char *functionName) |
|
111 |
{ |
|
112 |
static const char * const format = "%s error=%d, %s"; |
|
113 |
static const char * const fallbackFormat = "%s failed, error=%d"; |
|
114 |
char buf[256]; |
|
115 |
char errmsg[sizeof(buf) + 100]; |
|
116 |
const int errnum = GetLastError(); |
|
117 |
const int n = JVM_GetLastErrorString(buf, sizeof(buf)); |
|
118 |
if (n > 0) |
|
119 |
sprintf(errmsg, format, functionName, errnum, buf); |
|
120 |
else |
|
121 |
sprintf(errmsg, fallbackFormat, functionName, errnum); |
|
122 |
JNU_ThrowIOException(env, errmsg); |
|
123 |
} |
|
124 |
||
125 |
static void |
|
126 |
closeSafely(HANDLE handle) |
|
127 |
{ |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
128 |
if (handle != INVALID_HANDLE_VALUE) |
2 | 129 |
CloseHandle(handle); |
130 |
} |
|
131 |
||
132 |
JNIEXPORT jlong JNICALL |
|
133 |
Java_java_lang_ProcessImpl_create(JNIEnv *env, jclass ignored, |
|
134 |
jstring cmd, |
|
135 |
jstring envBlock, |
|
136 |
jstring dir, |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
137 |
jlongArray stdHandles, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
138 |
jboolean redirectErrorStream) |
2 | 139 |
{ |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
140 |
HANDLE inRead = INVALID_HANDLE_VALUE; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
141 |
HANDLE inWrite = INVALID_HANDLE_VALUE; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
142 |
HANDLE outRead = INVALID_HANDLE_VALUE; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
143 |
HANDLE outWrite = INVALID_HANDLE_VALUE; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
144 |
HANDLE errRead = INVALID_HANDLE_VALUE; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
145 |
HANDLE errWrite = INVALID_HANDLE_VALUE; |
2 | 146 |
SECURITY_ATTRIBUTES sa; |
147 |
PROCESS_INFORMATION pi; |
|
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
148 |
STARTUPINFOW si; |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
149 |
const jchar* pcmd = NULL; |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
150 |
const jchar* pdir = NULL; |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
151 |
const jchar* penvBlock = NULL; |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
152 |
jlong *handles = NULL; |
2 | 153 |
jlong ret = 0; |
154 |
OSVERSIONINFO ver; |
|
155 |
jboolean onNT = JNI_FALSE; |
|
156 |
DWORD processFlag; |
|
157 |
||
158 |
ver.dwOSVersionInfoSize = sizeof(ver); |
|
159 |
GetVersionEx(&ver); |
|
160 |
if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) |
|
161 |
onNT = JNI_TRUE; |
|
162 |
||
163 |
assert(cmd != NULL); |
|
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
164 |
pcmd = (*env)->GetStringChars(env, cmd, NULL); |
2 | 165 |
if (pcmd == NULL) goto Catch; |
166 |
||
167 |
if (dir != 0) { |
|
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
168 |
pdir = (*env)->GetStringChars(env, dir, NULL); |
2 | 169 |
if (pdir == NULL) goto Catch; |
170 |
} |
|
171 |
if (envBlock != NULL) { |
|
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
172 |
penvBlock = ((*env)->GetStringChars(env, envBlock, NULL)); |
2 | 173 |
if (penvBlock == NULL) goto Catch; |
174 |
} |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
175 |
assert(stdHandles != NULL); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
176 |
handles = (*env)->GetLongArrayElements(env, stdHandles, NULL); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
177 |
if (handles == NULL) goto Catch; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
178 |
|
2 | 179 |
memset(&si, 0, sizeof(si)); |
180 |
si.cb = sizeof(si); |
|
181 |
si.dwFlags = STARTF_USESTDHANDLES; |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
182 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
183 |
sa.nLength = sizeof(sa); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
184 |
sa.lpSecurityDescriptor = 0; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
185 |
sa.bInheritHandle = TRUE; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
186 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
187 |
if (handles[0] != (jlong) -1) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
188 |
si.hStdInput = (HANDLE) handles[0]; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
189 |
handles[0] = (jlong) -1; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
190 |
} else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
191 |
if (! CreatePipe(&inRead, &inWrite, &sa, PIPE_SIZE)) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
192 |
win32Error(env, "CreatePipe"); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
193 |
goto Catch; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
194 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
195 |
si.hStdInput = inRead; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
196 |
SetHandleInformation(inWrite, HANDLE_FLAG_INHERIT, FALSE); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
197 |
handles[0] = (jlong) inWrite; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
198 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
199 |
SetHandleInformation(si.hStdInput, HANDLE_FLAG_INHERIT, TRUE); |
2 | 200 |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
201 |
if (handles[1] != (jlong) -1) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
202 |
si.hStdOutput = (HANDLE) handles[1]; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
203 |
handles[1] = (jlong) -1; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
204 |
} else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
205 |
if (! CreatePipe(&outRead, &outWrite, &sa, PIPE_SIZE)) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
206 |
win32Error(env, "CreatePipe"); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
207 |
goto Catch; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
208 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
209 |
si.hStdOutput = outWrite; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
210 |
SetHandleInformation(outRead, HANDLE_FLAG_INHERIT, FALSE); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
211 |
handles[1] = (jlong) outRead; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
212 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
213 |
SetHandleInformation(si.hStdOutput, HANDLE_FLAG_INHERIT, TRUE); |
2 | 214 |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
215 |
if (redirectErrorStream) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
216 |
si.hStdError = si.hStdOutput; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
217 |
handles[2] = (jlong) -1; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
218 |
} else if (handles[2] != (jlong) -1) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
219 |
si.hStdError = (HANDLE) handles[2]; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
220 |
handles[2] = (jlong) -1; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
221 |
} else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
222 |
if (! CreatePipe(&errRead, &errWrite, &sa, PIPE_SIZE)) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
223 |
win32Error(env, "CreatePipe"); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
224 |
goto Catch; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
225 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
226 |
si.hStdError = errWrite; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
227 |
SetHandleInformation(errRead, HANDLE_FLAG_INHERIT, FALSE); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
228 |
handles[2] = (jlong) errRead; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
229 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
230 |
SetHandleInformation(si.hStdError, HANDLE_FLAG_INHERIT, TRUE); |
2 | 231 |
|
232 |
if (onNT) |
|
233 |
processFlag = CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT; |
|
234 |
else |
|
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
235 |
processFlag = selectProcessFlag(env, cmd) | CREATE_UNICODE_ENVIRONMENT; |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
236 |
ret = CreateProcessW(0, /* executable name */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
237 |
(LPWSTR)pcmd, /* command line */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
238 |
0, /* process security attribute */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
239 |
0, /* thread security attribute */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
240 |
TRUE, /* inherits system handles */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
241 |
processFlag, /* selected based on exe type */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
242 |
(LPVOID)penvBlock,/* environment block */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
243 |
(LPCWSTR)pdir, /* change to the new current directory */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
244 |
&si, /* (in) startup information */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
245 |
&pi); /* (out) process information */ |
2 | 246 |
if (!ret) { |
247 |
win32Error(env, "CreateProcess"); |
|
248 |
goto Catch; |
|
249 |
} |
|
250 |
||
251 |
CloseHandle(pi.hThread); |
|
252 |
ret = (jlong)pi.hProcess; |
|
253 |
||
254 |
Finally: |
|
255 |
/* Always clean up the child's side of the pipes */ |
|
256 |
closeSafely(inRead); |
|
257 |
closeSafely(outWrite); |
|
258 |
closeSafely(errWrite); |
|
259 |
||
260 |
if (pcmd != NULL) |
|
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
261 |
(*env)->ReleaseStringChars(env, cmd, pcmd); |
2 | 262 |
if (pdir != NULL) |
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
263 |
(*env)->ReleaseStringChars(env, dir, pdir); |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
264 |
if (penvBlock != NULL) |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
265 |
(*env)->ReleaseStringChars(env, envBlock, penvBlock); |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
266 |
if (handles != NULL) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
267 |
(*env)->ReleaseLongArrayElements(env, stdHandles, handles, 0); |
2 | 268 |
return ret; |
269 |
||
270 |
Catch: |
|
271 |
/* Clean up the parent's side of the pipes in case of failure only */ |
|
272 |
closeSafely(inWrite); |
|
273 |
closeSafely(outRead); |
|
274 |
closeSafely(errRead); |
|
275 |
goto Finally; |
|
276 |
} |
|
277 |
||
278 |
JNIEXPORT jint JNICALL |
|
279 |
Java_java_lang_ProcessImpl_getExitCodeProcess(JNIEnv *env, jclass ignored, jlong handle) |
|
280 |
{ |
|
281 |
DWORD exit_code; |
|
282 |
if (GetExitCodeProcess((HANDLE) handle, &exit_code) == 0) |
|
283 |
win32Error(env, "GetExitCodeProcess"); |
|
284 |
return exit_code; |
|
285 |
} |
|
286 |
||
287 |
JNIEXPORT jint JNICALL |
|
288 |
Java_java_lang_ProcessImpl_getStillActive(JNIEnv *env, jclass ignored) |
|
289 |
{ |
|
290 |
return STILL_ACTIVE; |
|
291 |
} |
|
292 |
||
293 |
JNIEXPORT void JNICALL |
|
294 |
Java_java_lang_ProcessImpl_waitForInterruptibly(JNIEnv *env, jclass ignored, jlong handle) |
|
295 |
{ |
|
296 |
HANDLE events[2]; |
|
297 |
events[0] = (HANDLE) handle; |
|
298 |
events[1] = JVM_GetThreadInterruptEvent(); |
|
299 |
||
300 |
if (WaitForMultipleObjects(sizeof(events)/sizeof(events[0]), events, |
|
301 |
FALSE, /* Wait for ANY event */ |
|
302 |
INFINITE) /* Wait forever */ |
|
303 |
== WAIT_FAILED) |
|
304 |
win32Error(env, "WaitForMultipleObjects"); |
|
305 |
} |
|
306 |
||
307 |
JNIEXPORT void JNICALL |
|
308 |
Java_java_lang_ProcessImpl_terminateProcess(JNIEnv *env, jclass ignored, jlong handle) |
|
309 |
{ |
|
310 |
TerminateProcess((HANDLE) handle, 1); |
|
311 |
} |
|
312 |
||
313 |
JNIEXPORT jboolean JNICALL |
|
314 |
Java_java_lang_ProcessImpl_closeHandle(JNIEnv *env, jclass ignored, jlong handle) |
|
315 |
{ |
|
316 |
return CloseHandle((HANDLE) handle); |
|
317 |
} |
|
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
318 |
|
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
319 |
/** |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
320 |
* Returns a copy of the Unicode characters of a string. Fow now this |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
321 |
* function doesn't handle long path names and other issues. |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
322 |
*/ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
323 |
static WCHAR* getPath(JNIEnv *env, jstring ps) { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
324 |
WCHAR *pathbuf = NULL; |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
325 |
const jchar *chars = (*(env))->GetStringChars(env, ps, NULL); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
326 |
if (chars != NULL) { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
327 |
size_t pathlen = wcslen(chars); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
328 |
pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR)); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
329 |
if (pathbuf == NULL) { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
330 |
JNU_ThrowOutOfMemoryError(env, NULL); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
331 |
} else { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
332 |
wcscpy(pathbuf, chars); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
333 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
334 |
(*env)->ReleaseStringChars(env, ps, chars); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
335 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
336 |
return pathbuf; |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
337 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
338 |
|
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
339 |
JNIEXPORT jlong JNICALL |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
340 |
Java_java_lang_ProcessImpl_openForAtomicAppend(JNIEnv *env, jclass ignored, jstring path) |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
341 |
{ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
342 |
const DWORD access = (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
343 |
const DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
344 |
const DWORD disposition = OPEN_ALWAYS; |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
345 |
const DWORD flagsAndAttributes = FILE_ATTRIBUTE_NORMAL; |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
346 |
HANDLE h; |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
347 |
WCHAR *pathbuf = getPath(env, path); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
348 |
if (pathbuf == NULL) { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
349 |
/* Exception already pending */ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
350 |
return -1; |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
351 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
352 |
h = CreateFileW( |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
353 |
pathbuf, /* Wide char path name */ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
354 |
access, /* Read and/or write permission */ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
355 |
sharing, /* File sharing flags */ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
356 |
NULL, /* Security attributes */ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
357 |
disposition, /* creation disposition */ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
358 |
flagsAndAttributes, /* flags and attributes */ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
359 |
NULL); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
360 |
free(pathbuf); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
361 |
if (h == INVALID_HANDLE_VALUE) { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
362 |
JNU_ThrowIOExceptionWithLastError(env, "CreateFileW"); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
363 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
364 |
return ptr_to_jlong(h); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
365 |
} |