author | uta |
Wed, 20 Feb 2013 16:39:35 +0400 | |
changeset 16016 | 70e2e77626ec |
parent 14177 | 007c2f91d22b |
child 19032 | e31afe87fb92 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, 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 |
static void |
|
44 |
win32Error(JNIEnv *env, const char *functionName) |
|
45 |
{ |
|
46 |
static const char * const format = "%s error=%d, %s"; |
|
47 |
static const char * const fallbackFormat = "%s failed, error=%d"; |
|
48 |
char buf[256]; |
|
49 |
char errmsg[sizeof(buf) + 100]; |
|
50 |
const int errnum = GetLastError(); |
|
51 |
const int n = JVM_GetLastErrorString(buf, sizeof(buf)); |
|
52 |
if (n > 0) |
|
53 |
sprintf(errmsg, format, functionName, errnum, buf); |
|
54 |
else |
|
55 |
sprintf(errmsg, fallbackFormat, functionName, errnum); |
|
56 |
JNU_ThrowIOException(env, errmsg); |
|
57 |
} |
|
58 |
||
59 |
static void |
|
60 |
closeSafely(HANDLE handle) |
|
61 |
{ |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
62 |
if (handle != INVALID_HANDLE_VALUE) |
2 | 63 |
CloseHandle(handle); |
64 |
} |
|
65 |
||
66 |
JNIEXPORT jlong JNICALL |
|
67 |
Java_java_lang_ProcessImpl_create(JNIEnv *env, jclass ignored, |
|
68 |
jstring cmd, |
|
69 |
jstring envBlock, |
|
70 |
jstring dir, |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
71 |
jlongArray stdHandles, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
72 |
jboolean redirectErrorStream) |
2 | 73 |
{ |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
74 |
HANDLE inRead = INVALID_HANDLE_VALUE; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
75 |
HANDLE inWrite = INVALID_HANDLE_VALUE; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
76 |
HANDLE outRead = INVALID_HANDLE_VALUE; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
77 |
HANDLE outWrite = INVALID_HANDLE_VALUE; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
78 |
HANDLE errRead = INVALID_HANDLE_VALUE; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
79 |
HANDLE errWrite = INVALID_HANDLE_VALUE; |
2 | 80 |
SECURITY_ATTRIBUTES sa; |
81 |
PROCESS_INFORMATION pi; |
|
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
82 |
STARTUPINFOW si; |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
83 |
const jchar* pcmd = NULL; |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
84 |
const jchar* pdir = NULL; |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
85 |
const jchar* penvBlock = NULL; |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
86 |
jlong *handles = NULL; |
2 | 87 |
jlong ret = 0; |
88 |
DWORD processFlag; |
|
89 |
||
90 |
assert(cmd != NULL); |
|
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
91 |
pcmd = (*env)->GetStringChars(env, cmd, NULL); |
2 | 92 |
if (pcmd == NULL) goto Catch; |
93 |
||
94 |
if (dir != 0) { |
|
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
95 |
pdir = (*env)->GetStringChars(env, dir, NULL); |
2 | 96 |
if (pdir == NULL) goto Catch; |
97 |
} |
|
98 |
if (envBlock != NULL) { |
|
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
99 |
penvBlock = ((*env)->GetStringChars(env, envBlock, NULL)); |
2 | 100 |
if (penvBlock == NULL) goto Catch; |
101 |
} |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
102 |
assert(stdHandles != NULL); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
103 |
handles = (*env)->GetLongArrayElements(env, stdHandles, NULL); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
104 |
if (handles == NULL) goto Catch; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
105 |
|
2 | 106 |
memset(&si, 0, sizeof(si)); |
107 |
si.cb = sizeof(si); |
|
108 |
si.dwFlags = STARTF_USESTDHANDLES; |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
109 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
110 |
sa.nLength = sizeof(sa); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
111 |
sa.lpSecurityDescriptor = 0; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
112 |
sa.bInheritHandle = TRUE; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
113 |
|
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
114 |
if (handles[0] != (jlong) -1) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
115 |
si.hStdInput = (HANDLE) handles[0]; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
116 |
handles[0] = (jlong) -1; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
117 |
} else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
118 |
if (! CreatePipe(&inRead, &inWrite, &sa, PIPE_SIZE)) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
119 |
win32Error(env, "CreatePipe"); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
120 |
goto Catch; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
121 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
122 |
si.hStdInput = inRead; |
16016
70e2e77626ec
8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents:
14177
diff
changeset
|
123 |
SetHandleInformation(inWrite, HANDLE_FLAG_INHERIT, 0); |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
124 |
handles[0] = (jlong) inWrite; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
125 |
} |
16016
70e2e77626ec
8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents:
14177
diff
changeset
|
126 |
SetHandleInformation(si.hStdInput, |
70e2e77626ec
8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents:
14177
diff
changeset
|
127 |
HANDLE_FLAG_INHERIT, |
70e2e77626ec
8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents:
14177
diff
changeset
|
128 |
HANDLE_FLAG_INHERIT); |
2 | 129 |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
130 |
if (handles[1] != (jlong) -1) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
131 |
si.hStdOutput = (HANDLE) handles[1]; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
132 |
handles[1] = (jlong) -1; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
133 |
} else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
134 |
if (! CreatePipe(&outRead, &outWrite, &sa, PIPE_SIZE)) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
135 |
win32Error(env, "CreatePipe"); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
136 |
goto Catch; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
137 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
138 |
si.hStdOutput = outWrite; |
16016
70e2e77626ec
8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents:
14177
diff
changeset
|
139 |
SetHandleInformation(outRead, HANDLE_FLAG_INHERIT, 0); |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
140 |
handles[1] = (jlong) outRead; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
141 |
} |
16016
70e2e77626ec
8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents:
14177
diff
changeset
|
142 |
SetHandleInformation(si.hStdOutput, |
70e2e77626ec
8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents:
14177
diff
changeset
|
143 |
HANDLE_FLAG_INHERIT, |
70e2e77626ec
8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents:
14177
diff
changeset
|
144 |
HANDLE_FLAG_INHERIT); |
2 | 145 |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
146 |
if (redirectErrorStream) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
147 |
si.hStdError = si.hStdOutput; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
148 |
handles[2] = (jlong) -1; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
149 |
} else if (handles[2] != (jlong) -1) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
150 |
si.hStdError = (HANDLE) handles[2]; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
151 |
handles[2] = (jlong) -1; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
152 |
} else { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
153 |
if (! CreatePipe(&errRead, &errWrite, &sa, PIPE_SIZE)) { |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
154 |
win32Error(env, "CreatePipe"); |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
155 |
goto Catch; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
156 |
} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
157 |
si.hStdError = errWrite; |
16016
70e2e77626ec
8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents:
14177
diff
changeset
|
158 |
SetHandleInformation(errRead, HANDLE_FLAG_INHERIT, 0); |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
159 |
handles[2] = (jlong) errRead; |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
160 |
} |
16016
70e2e77626ec
8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents:
14177
diff
changeset
|
161 |
SetHandleInformation(si.hStdError, |
70e2e77626ec
8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents:
14177
diff
changeset
|
162 |
HANDLE_FLAG_INHERIT, |
70e2e77626ec
8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents:
14177
diff
changeset
|
163 |
HANDLE_FLAG_INHERIT); |
2 | 164 |
|
14177 | 165 |
processFlag = CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT; |
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
166 |
ret = CreateProcessW(0, /* executable name */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
167 |
(LPWSTR)pcmd, /* command line */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
168 |
0, /* process security attribute */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
169 |
0, /* thread security attribute */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
170 |
TRUE, /* inherits system handles */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
171 |
processFlag, /* selected based on exe type */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
172 |
(LPVOID)penvBlock,/* environment block */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
173 |
(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
|
174 |
&si, /* (in) startup information */ |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
175 |
&pi); /* (out) process information */ |
2 | 176 |
if (!ret) { |
177 |
win32Error(env, "CreateProcess"); |
|
178 |
goto Catch; |
|
179 |
} |
|
180 |
||
181 |
CloseHandle(pi.hThread); |
|
182 |
ret = (jlong)pi.hProcess; |
|
183 |
||
184 |
Finally: |
|
185 |
/* Always clean up the child's side of the pipes */ |
|
186 |
closeSafely(inRead); |
|
187 |
closeSafely(outWrite); |
|
188 |
closeSafely(errWrite); |
|
189 |
||
190 |
if (pcmd != NULL) |
|
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
191 |
(*env)->ReleaseStringChars(env, cmd, pcmd); |
2 | 192 |
if (pdir != NULL) |
5168
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
193 |
(*env)->ReleaseStringChars(env, dir, pdir); |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
194 |
if (penvBlock != NULL) |
41e46b5d9b15
4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents:
715
diff
changeset
|
195 |
(*env)->ReleaseStringChars(env, envBlock, penvBlock); |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
196 |
if (handles != NULL) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
43
diff
changeset
|
197 |
(*env)->ReleaseLongArrayElements(env, stdHandles, handles, 0); |
2 | 198 |
return ret; |
199 |
||
200 |
Catch: |
|
201 |
/* Clean up the parent's side of the pipes in case of failure only */ |
|
202 |
closeSafely(inWrite); |
|
203 |
closeSafely(outRead); |
|
204 |
closeSafely(errRead); |
|
205 |
goto Finally; |
|
206 |
} |
|
207 |
||
208 |
JNIEXPORT jint JNICALL |
|
209 |
Java_java_lang_ProcessImpl_getExitCodeProcess(JNIEnv *env, jclass ignored, jlong handle) |
|
210 |
{ |
|
211 |
DWORD exit_code; |
|
212 |
if (GetExitCodeProcess((HANDLE) handle, &exit_code) == 0) |
|
213 |
win32Error(env, "GetExitCodeProcess"); |
|
214 |
return exit_code; |
|
215 |
} |
|
216 |
||
217 |
JNIEXPORT jint JNICALL |
|
218 |
Java_java_lang_ProcessImpl_getStillActive(JNIEnv *env, jclass ignored) |
|
219 |
{ |
|
220 |
return STILL_ACTIVE; |
|
221 |
} |
|
222 |
||
223 |
JNIEXPORT void JNICALL |
|
224 |
Java_java_lang_ProcessImpl_waitForInterruptibly(JNIEnv *env, jclass ignored, jlong handle) |
|
225 |
{ |
|
226 |
HANDLE events[2]; |
|
227 |
events[0] = (HANDLE) handle; |
|
228 |
events[1] = JVM_GetThreadInterruptEvent(); |
|
229 |
||
230 |
if (WaitForMultipleObjects(sizeof(events)/sizeof(events[0]), events, |
|
231 |
FALSE, /* Wait for ANY event */ |
|
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
232 |
INFINITE) /* Wait forever */ |
2 | 233 |
== WAIT_FAILED) |
234 |
win32Error(env, "WaitForMultipleObjects"); |
|
235 |
} |
|
236 |
||
237 |
JNIEXPORT void JNICALL |
|
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
238 |
Java_java_lang_ProcessImpl_waitForTimeoutInterruptibly(JNIEnv *env, |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
239 |
jclass ignored, |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
240 |
jlong handle, |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
241 |
jlong timeout) |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
242 |
{ |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
243 |
HANDLE events[2]; |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
244 |
DWORD dwTimeout = (DWORD)timeout; |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
245 |
DWORD result; |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
246 |
events[0] = (HANDLE) handle; |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
247 |
events[1] = JVM_GetThreadInterruptEvent(); |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
248 |
result = WaitForMultipleObjects(sizeof(events)/sizeof(events[0]), events, |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
249 |
FALSE, /* Wait for ANY event */ |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
250 |
dwTimeout); /* Wait for dwTimeout */ |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
251 |
|
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
252 |
if (result == WAIT_FAILED) |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
253 |
win32Error(env, "WaitForMultipleObjects"); |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
254 |
} |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
255 |
|
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
256 |
JNIEXPORT void JNICALL |
2 | 257 |
Java_java_lang_ProcessImpl_terminateProcess(JNIEnv *env, jclass ignored, jlong handle) |
258 |
{ |
|
259 |
TerminateProcess((HANDLE) handle, 1); |
|
260 |
} |
|
261 |
||
262 |
JNIEXPORT jboolean JNICALL |
|
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
263 |
Java_java_lang_ProcessImpl_isProcessAlive(JNIEnv *env, jclass ignored, jlong handle) |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
264 |
{ |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
265 |
DWORD dwExitStatus; |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
266 |
GetExitCodeProcess(handle, &dwExitStatus); |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
267 |
return dwExitStatus == STILL_ACTIVE; |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
268 |
} |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
269 |
|
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
7668
diff
changeset
|
270 |
JNIEXPORT jboolean JNICALL |
2 | 271 |
Java_java_lang_ProcessImpl_closeHandle(JNIEnv *env, jclass ignored, jlong handle) |
272 |
{ |
|
273 |
return CloseHandle((HANDLE) handle); |
|
274 |
} |
|
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
275 |
|
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
276 |
/** |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
277 |
* 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
|
278 |
* 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
|
279 |
*/ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
280 |
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
|
281 |
WCHAR *pathbuf = NULL; |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
282 |
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
|
283 |
if (chars != NULL) { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
284 |
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
|
285 |
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
|
286 |
if (pathbuf == NULL) { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
287 |
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
|
288 |
} else { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
289 |
wcscpy(pathbuf, chars); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
290 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
291 |
(*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
|
292 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
293 |
return pathbuf; |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
294 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
295 |
|
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
296 |
JNIEXPORT jlong JNICALL |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
297 |
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
|
298 |
{ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
299 |
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
|
300 |
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
|
301 |
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
|
302 |
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
|
303 |
HANDLE h; |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
304 |
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
|
305 |
if (pathbuf == NULL) { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
306 |
/* Exception already pending */ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
307 |
return -1; |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
308 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
309 |
h = CreateFileW( |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
310 |
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
|
311 |
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
|
312 |
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
|
313 |
NULL, /* Security attributes */ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
314 |
disposition, /* creation disposition */ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
315 |
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
|
316 |
NULL); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
317 |
free(pathbuf); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
318 |
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
|
319 |
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
|
320 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
321 |
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
|
322 |
} |