author | jboes |
Fri, 20 Sep 2019 11:07:52 +0100 | |
changeset 58242 | 94bb65cb37d3 |
parent 47236 | bed98952fb09 |
permissions | -rw-r--r-- |
30899 | 1 |
/* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47236
diff
changeset
|
2 |
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. |
30899 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact 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. |
|
24 |
*/ |
|
25 |
package java.lang; |
|
26 |
||
44269
762b2df849b3
8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents:
37350
diff
changeset
|
27 |
import java.lang.annotation.Native; |
30899 | 28 |
import java.security.PrivilegedAction; |
29 |
import java.time.Duration; |
|
30 |
import java.time.Instant; |
|
31 |
import java.util.Arrays; |
|
32 |
import java.util.Optional; |
|
33 |
import java.util.concurrent.CompletableFuture; |
|
34 |
import java.util.concurrent.ConcurrentHashMap; |
|
35 |
import java.util.concurrent.ConcurrentMap; |
|
36 |
import java.util.concurrent.Executor; |
|
37 |
import java.util.concurrent.Executors; |
|
38 |
import java.util.concurrent.ThreadFactory; |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
39 |
import java.util.stream.IntStream; |
30899 | 40 |
import java.util.stream.Stream; |
41 |
||
42 |
import static java.security.AccessController.doPrivileged; |
|
43 |
||
44 |
/** |
|
45 |
* ProcessHandleImpl is the implementation of ProcessHandle. |
|
46 |
* |
|
47 |
* @see Process |
|
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34385
diff
changeset
|
48 |
* @since 9 |
30899 | 49 |
*/ |
50 |
final class ProcessHandleImpl implements ProcessHandle { |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
51 |
/** |
37350
944a8584d0fb
8086278: java/lang/ProcessHandle/TreeTest.java failed - ProcessReaper StackOverflowException
rriggs
parents:
36421
diff
changeset
|
52 |
* Default size of stack for reaper processes. |
944a8584d0fb
8086278: java/lang/ProcessHandle/TreeTest.java failed - ProcessReaper StackOverflowException
rriggs
parents:
36421
diff
changeset
|
53 |
*/ |
944a8584d0fb
8086278: java/lang/ProcessHandle/TreeTest.java failed - ProcessReaper StackOverflowException
rriggs
parents:
36421
diff
changeset
|
54 |
private static long REAPER_DEFAULT_STACKSIZE = 128 * 1024; |
944a8584d0fb
8086278: java/lang/ProcessHandle/TreeTest.java failed - ProcessReaper StackOverflowException
rriggs
parents:
36421
diff
changeset
|
55 |
|
944a8584d0fb
8086278: java/lang/ProcessHandle/TreeTest.java failed - ProcessReaper StackOverflowException
rriggs
parents:
36421
diff
changeset
|
56 |
/** |
44269
762b2df849b3
8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents:
37350
diff
changeset
|
57 |
* Return value from waitForProcessExit0 indicating the process is not a child. |
762b2df849b3
8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents:
37350
diff
changeset
|
58 |
*/ |
762b2df849b3
8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents:
37350
diff
changeset
|
59 |
@Native |
762b2df849b3
8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents:
37350
diff
changeset
|
60 |
private static final int NOT_A_CHILD = -2; |
762b2df849b3
8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents:
37350
diff
changeset
|
61 |
|
762b2df849b3
8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents:
37350
diff
changeset
|
62 |
/** |
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
63 |
* Cache the ProcessHandle of this process. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
64 |
*/ |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
65 |
private static final ProcessHandleImpl current; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
66 |
|
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
67 |
/** |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
68 |
* Map of pids to ExitCompletions. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
69 |
*/ |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
70 |
private static final ConcurrentMap<Long, ExitCompletion> |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
71 |
completions = new ConcurrentHashMap<>(); |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
72 |
|
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
73 |
static { |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
74 |
initNative(); |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
75 |
long pid = getCurrentPid0(); |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
76 |
current = new ProcessHandleImpl(pid, isAlive0(pid)); |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
77 |
} |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
78 |
|
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
79 |
private static native void initNative(); |
30899 | 80 |
|
81 |
/** |
|
82 |
* The thread pool of "process reaper" daemon threads. |
|
83 |
*/ |
|
84 |
private static final Executor processReaperExecutor = |
|
85 |
doPrivileged((PrivilegedAction<Executor>) () -> { |
|
86 |
||
87 |
ThreadGroup tg = Thread.currentThread().getThreadGroup(); |
|
88 |
while (tg.getParent() != null) tg = tg.getParent(); |
|
89 |
ThreadGroup systemThreadGroup = tg; |
|
37350
944a8584d0fb
8086278: java/lang/ProcessHandle/TreeTest.java failed - ProcessReaper StackOverflowException
rriggs
parents:
36421
diff
changeset
|
90 |
final long stackSize = Boolean.getBoolean("jdk.lang.processReaperUseDefaultStackSize") |
944a8584d0fb
8086278: java/lang/ProcessHandle/TreeTest.java failed - ProcessReaper StackOverflowException
rriggs
parents:
36421
diff
changeset
|
91 |
? 0 : REAPER_DEFAULT_STACKSIZE; |
30899 | 92 |
|
93 |
ThreadFactory threadFactory = grimReaper -> { |
|
37350
944a8584d0fb
8086278: java/lang/ProcessHandle/TreeTest.java failed - ProcessReaper StackOverflowException
rriggs
parents:
36421
diff
changeset
|
94 |
Thread t = new Thread(systemThreadGroup, grimReaper, |
944a8584d0fb
8086278: java/lang/ProcessHandle/TreeTest.java failed - ProcessReaper StackOverflowException
rriggs
parents:
36421
diff
changeset
|
95 |
"process reaper", stackSize, false); |
30899 | 96 |
t.setDaemon(true); |
97 |
// A small attempt (probably futile) to avoid priority inversion |
|
98 |
t.setPriority(Thread.MAX_PRIORITY); |
|
99 |
return t; |
|
100 |
}; |
|
101 |
||
102 |
return Executors.newCachedThreadPool(threadFactory); |
|
103 |
}); |
|
104 |
||
105 |
private static class ExitCompletion extends CompletableFuture<Integer> { |
|
106 |
final boolean isReaping; |
|
107 |
||
108 |
ExitCompletion(boolean isReaping) { |
|
109 |
this.isReaping = isReaping; |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Returns a CompletableFuture that completes with process exit status when |
|
115 |
* the process completes. |
|
116 |
* |
|
117 |
* @param shouldReap true if the exit value should be reaped |
|
118 |
*/ |
|
119 |
static CompletableFuture<Integer> completion(long pid, boolean shouldReap) { |
|
120 |
// check canonicalizing cache 1st |
|
121 |
ExitCompletion completion = completions.get(pid); |
|
122 |
// re-try until we get a completion that shouldReap => isReaping |
|
123 |
while (completion == null || (shouldReap && !completion.isReaping)) { |
|
124 |
ExitCompletion newCompletion = new ExitCompletion(shouldReap); |
|
125 |
if (completion == null) { |
|
126 |
completion = completions.putIfAbsent(pid, newCompletion); |
|
127 |
} else { |
|
128 |
completion = completions.replace(pid, completion, newCompletion) |
|
129 |
? null : completions.get(pid); |
|
130 |
} |
|
131 |
if (completion == null) { |
|
132 |
// newCompletion has just been installed successfully |
|
133 |
completion = newCompletion; |
|
134 |
// spawn a thread to wait for and deliver the exit value |
|
46895
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
135 |
processReaperExecutor.execute(new Runnable() { |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
136 |
// Use inner class to avoid lambda stack overhead |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
137 |
public void run() { |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
138 |
int exitValue = waitForProcessExit0(pid, shouldReap); |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
139 |
if (exitValue == NOT_A_CHILD) { |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
140 |
// pid not alive or not a child of this process |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
141 |
// If it is alive wait for it to terminate |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
142 |
long sleep = 300; // initial milliseconds to sleep |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
143 |
int incr = 30; // increment to the sleep time |
44269
762b2df849b3
8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents:
37350
diff
changeset
|
144 |
|
46895
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
145 |
long startTime = isAlive0(pid); |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
146 |
long origStart = startTime; |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
147 |
while (startTime >= 0) { |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
148 |
try { |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
149 |
Thread.sleep(Math.min(sleep, 5000L)); // no more than 5 sec |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
150 |
sleep += incr; |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
151 |
} catch (InterruptedException ie) { |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
152 |
// ignore and retry |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
153 |
} |
47236
bed98952fb09
8177932: (process) java/lang/ProcessHandle/OnExitTest.java failed with "Process A should not be alive..."
rriggs
parents:
47216
diff
changeset
|
154 |
startTime = isAlive0(pid); // recheck if it is alive |
bed98952fb09
8177932: (process) java/lang/ProcessHandle/OnExitTest.java failed with "Process A should not be alive..."
rriggs
parents:
47216
diff
changeset
|
155 |
if (startTime > 0 && origStart > 0 && startTime != origStart) { |
bed98952fb09
8177932: (process) java/lang/ProcessHandle/OnExitTest.java failed with "Process A should not be alive..."
rriggs
parents:
47216
diff
changeset
|
156 |
// start time changed (and is not zero), pid is not the same process |
46895
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
157 |
break; |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
158 |
} |
44269
762b2df849b3
8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents:
37350
diff
changeset
|
159 |
} |
46895
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
160 |
exitValue = 0; |
44269
762b2df849b3
8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents:
37350
diff
changeset
|
161 |
} |
46895
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
162 |
newCompletion.complete(exitValue); |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
163 |
// remove from cache afterwards |
16e7cacc7896
8173817: StackOverflowError in "process reaper" thread
rriggs
parents:
44640
diff
changeset
|
164 |
completions.remove(pid, newCompletion); |
44269
762b2df849b3
8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents:
37350
diff
changeset
|
165 |
} |
30899 | 166 |
}); |
167 |
} |
|
168 |
} |
|
169 |
return completion; |
|
170 |
} |
|
171 |
||
172 |
@Override |
|
173 |
public CompletableFuture<ProcessHandle> onExit() { |
|
174 |
if (this.equals(current)) { |
|
175 |
throw new IllegalStateException("onExit for current process not allowed"); |
|
176 |
} |
|
177 |
||
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
44269
diff
changeset
|
178 |
return ProcessHandleImpl.completion(pid(), false) |
30899 | 179 |
.handleAsync((exitStatus, unusedThrowable) -> this); |
180 |
} |
|
181 |
||
182 |
/** |
|
183 |
* Wait for the process to exit, return the value. |
|
184 |
* Conditionally reap the value if requested |
|
185 |
* @param pid the processId |
|
186 |
* @param reapvalue if true, the value is retrieved, |
|
187 |
* else return the value and leave the process waitable |
|
188 |
* |
|
189 |
* @return the value or -1 if an error occurs |
|
190 |
*/ |
|
191 |
private static native int waitForProcessExit0(long pid, boolean reapvalue); |
|
192 |
||
193 |
/** |
|
194 |
* The pid of this ProcessHandle. |
|
195 |
*/ |
|
196 |
private final long pid; |
|
197 |
||
198 |
/** |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
199 |
* The start time of this process. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
200 |
* If STARTTIME_ANY, the start time of the process is not available from the os. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
201 |
* If greater than zero, the start time of the process. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
202 |
*/ |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
203 |
private final long startTime; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
204 |
|
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
205 |
/* The start time should match any value. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
206 |
* Typically, this is because the OS can not supply it. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
207 |
* The process is known to exist but not the exact start time. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
208 |
*/ |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
209 |
private final long STARTTIME_ANY = 0L; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
210 |
|
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
211 |
/* The start time of a Process that does not exist. */ |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
212 |
private final long STARTTIME_PROCESS_UNKNOWN = -1; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
213 |
|
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
214 |
/** |
30899 | 215 |
* Private constructor. Instances are created by the {@code get(long)} factory. |
216 |
* @param pid the pid for this instance |
|
217 |
*/ |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
218 |
private ProcessHandleImpl(long pid, long startTime) { |
30899 | 219 |
this.pid = pid; |
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
220 |
this.startTime = startTime; |
30899 | 221 |
} |
222 |
||
223 |
/** |
|
224 |
* Returns a ProcessHandle for an existing native process. |
|
225 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47236
diff
changeset
|
226 |
* @param pid the native process identifier |
30899 | 227 |
* @return The ProcessHandle for the pid if the process is alive; |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47236
diff
changeset
|
228 |
* or {@code null} if the process ID does not exist in the native system. |
30899 | 229 |
* @throws SecurityException if RuntimePermission("manageProcess") is not granted |
230 |
*/ |
|
231 |
static Optional<ProcessHandle> get(long pid) { |
|
232 |
SecurityManager sm = System.getSecurityManager(); |
|
233 |
if (sm != null) { |
|
234 |
sm.checkPermission(new RuntimePermission("manageProcess")); |
|
235 |
} |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
236 |
long start = isAlive0(pid); |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
237 |
return (start >= 0) |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
238 |
? Optional.of(new ProcessHandleImpl(pid, start)) |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
239 |
: Optional.empty(); |
30899 | 240 |
} |
241 |
||
242 |
/** |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
243 |
* Returns a ProcessHandle for an existing native process known to be alive. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
244 |
* The startTime of the process is retrieved and stored in the ProcessHandle. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
245 |
* It does not perform a security check since it is called from ProcessImpl. |
30899 | 246 |
* @param pid of the known to exist process |
247 |
* @return a ProcessHandle corresponding to an existing Process instance |
|
248 |
*/ |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
249 |
static ProcessHandleImpl getInternal(long pid) { |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
250 |
return new ProcessHandleImpl(pid, isAlive0(pid)); |
30899 | 251 |
} |
252 |
||
253 |
/** |
|
254 |
* Returns the native process ID. |
|
255 |
* A {@code long} is used to be able to fit the system specific binary values |
|
256 |
* for the process. |
|
257 |
* |
|
258 |
* @return the native process ID |
|
259 |
*/ |
|
260 |
@Override |
|
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
44269
diff
changeset
|
261 |
public long pid() { |
30899 | 262 |
return pid; |
263 |
} |
|
264 |
||
265 |
/** |
|
266 |
* Returns the ProcessHandle for the current native process. |
|
267 |
* |
|
268 |
* @return The ProcessHandle for the OS process. |
|
269 |
* @throws SecurityException if RuntimePermission("manageProcess") is not granted |
|
270 |
*/ |
|
271 |
public static ProcessHandleImpl current() { |
|
272 |
SecurityManager sm = System.getSecurityManager(); |
|
273 |
if (sm != null) { |
|
274 |
sm.checkPermission(new RuntimePermission("manageProcess")); |
|
275 |
} |
|
276 |
return current; |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* Return the pid of the current process. |
|
281 |
* |
|
282 |
* @return the pid of the current process |
|
283 |
*/ |
|
284 |
private static native long getCurrentPid0(); |
|
285 |
||
286 |
/** |
|
287 |
* Returns a ProcessHandle for the parent process. |
|
288 |
* |
|
289 |
* @return a ProcessHandle of the parent process; {@code null} is returned |
|
290 |
* if the child process does not have a parent |
|
291 |
* @throws SecurityException if permission is not granted by the |
|
292 |
* security policy |
|
293 |
*/ |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
294 |
public Optional<ProcessHandle> parent() { |
30899 | 295 |
SecurityManager sm = System.getSecurityManager(); |
296 |
if (sm != null) { |
|
297 |
sm.checkPermission(new RuntimePermission("manageProcess")); |
|
298 |
} |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
299 |
long ppid = parent0(pid, startTime); |
30899 | 300 |
if (ppid <= 0) { |
301 |
return Optional.empty(); |
|
302 |
} |
|
303 |
return get(ppid); |
|
304 |
} |
|
305 |
||
306 |
/** |
|
307 |
* Returns the parent of the native pid argument. |
|
308 |
* |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
309 |
* @param pid the process id |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
310 |
* @param startTime the startTime of the process |
30899 | 311 |
* @return the parent of the native pid; if any, otherwise -1 |
312 |
*/ |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
313 |
private static native long parent0(long pid, long startTime); |
30899 | 314 |
|
315 |
/** |
|
316 |
* Returns the number of pids filled in to the array. |
|
317 |
* @param pid if {@code pid} equals zero, then all known processes are returned; |
|
318 |
* otherwise only direct child process pids are returned |
|
319 |
* @param pids an allocated long array to receive the pids |
|
320 |
* @param ppids an allocated long array to receive the parent pids; may be null |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
321 |
* @param starttimes an allocated long array to receive the child start times; may be null |
30899 | 322 |
* @return if greater than or equals to zero is the number of pids in the array; |
323 |
* if greater than the length of the arrays, the arrays are too small |
|
324 |
*/ |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
325 |
private static native int getProcessPids0(long pid, long[] pids, |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
326 |
long[] ppids, long[] starttimes); |
30899 | 327 |
|
328 |
/** |
|
329 |
* Destroy the process for this ProcessHandle. |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
330 |
* The native code checks the start time before sending the termination request. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
331 |
* |
30899 | 332 |
* @param force {@code true} if the process should be terminated forcibly; |
333 |
* else {@code false} for a normal termination |
|
334 |
*/ |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
335 |
boolean destroyProcess(boolean force) { |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
336 |
if (this.equals(current)) { |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
337 |
throw new IllegalStateException("destroy of current process not allowed"); |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
338 |
} |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
339 |
return destroy0(pid, startTime, force); |
30899 | 340 |
} |
341 |
||
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
342 |
/** |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
343 |
* Signal the process to terminate. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
344 |
* The process is signaled only if its start time matches the known start time. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
345 |
* |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
346 |
* @param pid process id to kill |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
347 |
* @param startTime the start time of the process |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
348 |
* @param forcibly true to forcibly terminate (SIGKILL vs SIGTERM) |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
349 |
* @return true if the process was signaled without error; false otherwise |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
350 |
*/ |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
351 |
private static native boolean destroy0(long pid, long startTime, boolean forcibly); |
30899 | 352 |
|
353 |
@Override |
|
354 |
public boolean destroy() { |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
355 |
return destroyProcess(false); |
30899 | 356 |
} |
357 |
||
358 |
@Override |
|
359 |
public boolean destroyForcibly() { |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
360 |
return destroyProcess(true); |
30899 | 361 |
} |
362 |
||
363 |
||
364 |
@Override |
|
365 |
public boolean supportsNormalTermination() { |
|
366 |
return ProcessImpl.SUPPORTS_NORMAL_TERMINATION; |
|
367 |
} |
|
368 |
||
369 |
/** |
|
370 |
* Tests whether the process represented by this {@code ProcessHandle} is alive. |
|
371 |
* |
|
372 |
* @return {@code true} if the process represented by this |
|
373 |
* {@code ProcessHandle} object has not yet terminated. |
|
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34385
diff
changeset
|
374 |
* @since 9 |
30899 | 375 |
*/ |
376 |
@Override |
|
377 |
public boolean isAlive() { |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
378 |
long start = isAlive0(pid); |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
379 |
return (start >= 0 && (start == startTime || start == 0 || startTime == 0)); |
30899 | 380 |
} |
381 |
||
382 |
/** |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
383 |
* Returns the process start time depending on whether the pid is alive. |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
384 |
* This must not reap the exitValue. |
30899 | 385 |
* |
386 |
* @param pid the pid to check |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
387 |
* @return the start time in milliseconds since 1970, |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
388 |
* 0 if the start time cannot be determined, |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
389 |
* -1 if the pid does not exist. |
30899 | 390 |
*/ |
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
391 |
private static native long isAlive0(long pid); |
30899 | 392 |
|
393 |
@Override |
|
394 |
public Stream<ProcessHandle> children() { |
|
34385
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
395 |
// The native OS code selects based on matching the requested parent pid. |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
396 |
// If the original parent exits, the pid may have been re-used for |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
397 |
// this newer process. |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
398 |
// Processes started by the original parent (now dead) will all have |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
399 |
// start times less than the start of this newer parent. |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
400 |
// Processes started by this newer parent will have start times equal |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
401 |
// or after this parent. |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
402 |
return children(pid).filter(ph -> startTime <= ((ProcessHandleImpl)ph).startTime); |
30899 | 403 |
} |
404 |
||
405 |
/** |
|
406 |
* Returns a Stream of the children of a process or all processes. |
|
407 |
* |
|
408 |
* @param pid the pid of the process for which to find the children; |
|
409 |
* 0 for all processes |
|
410 |
* @return a stream of ProcessHandles |
|
411 |
*/ |
|
412 |
static Stream<ProcessHandle> children(long pid) { |
|
413 |
SecurityManager sm = System.getSecurityManager(); |
|
414 |
if (sm != null) { |
|
415 |
sm.checkPermission(new RuntimePermission("manageProcess")); |
|
416 |
} |
|
417 |
int size = 100; |
|
418 |
long[] childpids = null; |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
419 |
long[] starttimes = null; |
30899 | 420 |
while (childpids == null || size > childpids.length) { |
421 |
childpids = new long[size]; |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
422 |
starttimes = new long[size]; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
423 |
size = getProcessPids0(pid, childpids, null, starttimes); |
30899 | 424 |
} |
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
425 |
|
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
426 |
final long[] cpids = childpids; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
427 |
final long[] stimes = starttimes; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
428 |
return IntStream.range(0, size).mapToObj(i -> new ProcessHandleImpl(cpids[i], stimes[i])); |
30899 | 429 |
} |
430 |
||
431 |
@Override |
|
33648
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32209
diff
changeset
|
432 |
public Stream<ProcessHandle> descendants() { |
30899 | 433 |
SecurityManager sm = System.getSecurityManager(); |
434 |
if (sm != null) { |
|
435 |
sm.checkPermission(new RuntimePermission("manageProcess")); |
|
436 |
} |
|
437 |
int size = 100; |
|
438 |
long[] pids = null; |
|
439 |
long[] ppids = null; |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
440 |
long[] starttimes = null; |
30899 | 441 |
while (pids == null || size > pids.length) { |
442 |
pids = new long[size]; |
|
443 |
ppids = new long[size]; |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
444 |
starttimes = new long[size]; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
445 |
size = getProcessPids0(0, pids, ppids, starttimes); |
30899 | 446 |
} |
447 |
||
448 |
int next = 0; // index of next process to check |
|
449 |
int count = -1; // count of subprocesses scanned |
|
450 |
long ppid = pid; // start looking for this parent |
|
34385
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
451 |
long ppStart = 0; |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
452 |
// Find the start time of the parent |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
453 |
for (int i = 0; i < size; i++) { |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
454 |
if (pids[i] == ppid) { |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
455 |
ppStart = starttimes[i]; |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
456 |
break; |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
457 |
} |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
458 |
} |
30899 | 459 |
do { |
34385
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
460 |
// Scan from next to size looking for ppid with child start time |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
461 |
// the same or later than the parent. |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
462 |
// If found, exchange it with index next |
30899 | 463 |
for (int i = next; i < size; i++) { |
34385
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
464 |
if (ppids[i] == ppid && |
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
465 |
ppStart <= starttimes[i]) { |
30899 | 466 |
swap(pids, i, next); |
467 |
swap(ppids, i, next); |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
468 |
swap(starttimes, i, next); |
30899 | 469 |
next++; |
470 |
} |
|
471 |
} |
|
472 |
ppid = pids[++count]; // pick up the next pid to scan for |
|
34385
0762c1d51f8a
8143876: test/java/lang/ProcessHandle/TreeTest.java failed intermittently with assertion error
rriggs
parents:
33648
diff
changeset
|
473 |
ppStart = starttimes[count]; // and its start time |
30899 | 474 |
} while (count < next); |
475 |
||
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
476 |
final long[] cpids = pids; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
477 |
final long[] stimes = starttimes; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
478 |
return IntStream.range(0, count).mapToObj(i -> new ProcessHandleImpl(cpids[i], stimes[i])); |
30899 | 479 |
} |
480 |
||
481 |
// Swap two elements in an array |
|
482 |
private static void swap(long[] array, int x, int y) { |
|
483 |
long v = array[x]; |
|
484 |
array[x] = array[y]; |
|
485 |
array[y] = v; |
|
486 |
} |
|
487 |
||
488 |
@Override |
|
489 |
public ProcessHandle.Info info() { |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
490 |
return ProcessHandleImpl.Info.info(pid, startTime); |
30899 | 491 |
} |
492 |
||
493 |
@Override |
|
494 |
public int compareTo(ProcessHandle other) { |
|
495 |
return Long.compare(pid, ((ProcessHandleImpl) other).pid); |
|
496 |
} |
|
497 |
||
498 |
@Override |
|
499 |
public String toString() { |
|
500 |
return Long.toString(pid); |
|
501 |
} |
|
502 |
||
503 |
@Override |
|
504 |
public int hashCode() { |
|
505 |
return Long.hashCode(pid); |
|
506 |
} |
|
507 |
||
508 |
@Override |
|
509 |
public boolean equals(Object obj) { |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
510 |
if (this == obj) { |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
511 |
return true; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
512 |
} |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
513 |
if (obj instanceof ProcessHandleImpl) { |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
514 |
ProcessHandleImpl other = (ProcessHandleImpl) obj; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
515 |
return (pid == other.pid) && |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
516 |
(startTime == other.startTime |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
517 |
|| startTime == 0 |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
518 |
|| other.startTime == 0); |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
519 |
} |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
520 |
return false; |
30899 | 521 |
} |
522 |
||
523 |
/** |
|
524 |
* Implementation of ProcessHandle.Info. |
|
525 |
* Information snapshot about a process. |
|
32209
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
526 |
* The attributes of a process vary by operating system and are not available |
30899 | 527 |
* in all implementations. Additionally, information about other processes |
528 |
* is limited by the operating system privileges of the process making the request. |
|
529 |
* If a value is not available, either a {@code null} or {@code -1} is stored. |
|
530 |
* The accessor methods return {@code null} if the value is not available. |
|
531 |
*/ |
|
532 |
static class Info implements ProcessHandle.Info { |
|
533 |
static { |
|
534 |
initIDs(); |
|
535 |
} |
|
536 |
||
537 |
/** |
|
538 |
* Initialization of JNI fieldIDs. |
|
539 |
*/ |
|
540 |
private static native void initIDs(); |
|
541 |
||
542 |
/** |
|
543 |
* Fill in this Info instance with information about the native process. |
|
544 |
* If values are not available the native code does not modify the field. |
|
545 |
* @param pid of the native process |
|
546 |
*/ |
|
547 |
private native void info0(long pid); |
|
548 |
||
549 |
String command; |
|
32209
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
550 |
String commandLine; |
30899 | 551 |
String[] arguments; |
552 |
long startTime; |
|
553 |
long totalTime; |
|
554 |
String user; |
|
555 |
||
556 |
Info() { |
|
557 |
command = null; |
|
32209
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
558 |
commandLine = null; |
30899 | 559 |
arguments = null; |
560 |
startTime = -1L; |
|
561 |
totalTime = -1L; |
|
562 |
user = null; |
|
563 |
} |
|
564 |
||
565 |
/** |
|
566 |
* Returns the Info object with the fields from the process. |
|
567 |
* Whatever fields are provided by native are returned. |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
568 |
* If the startTime of the process does not match the provided |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
569 |
* startTime then an empty Info is returned. |
30899 | 570 |
* |
571 |
* @param pid the native process identifier |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
572 |
* @param startTime the startTime of the process being queried |
30899 | 573 |
* @return ProcessHandle.Info non-null; individual fields may be null |
574 |
* or -1 if not available. |
|
575 |
*/ |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
576 |
public static ProcessHandle.Info info(long pid, long startTime) { |
30899 | 577 |
Info info = new Info(); |
578 |
info.info0(pid); |
|
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
579 |
if (startTime != info.startTime) { |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
580 |
info.command = null; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
581 |
info.arguments = null; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
582 |
info.startTime = -1L; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
583 |
info.totalTime = -1L; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
584 |
info.user = null; |
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
585 |
} |
30899 | 586 |
return info; |
587 |
} |
|
588 |
||
589 |
@Override |
|
590 |
public Optional<String> command() { |
|
591 |
return Optional.ofNullable(command); |
|
592 |
} |
|
593 |
||
594 |
@Override |
|
32209
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
595 |
public Optional<String> commandLine() { |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
596 |
if (command != null && arguments != null) { |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
597 |
return Optional.of(command + " " + String.join(" ", arguments)); |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
598 |
} else { |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
599 |
return Optional.ofNullable(commandLine); |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
600 |
} |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
601 |
} |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
602 |
|
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
603 |
@Override |
30899 | 604 |
public Optional<String[]> arguments() { |
605 |
return Optional.ofNullable(arguments); |
|
606 |
} |
|
607 |
||
608 |
@Override |
|
609 |
public Optional<Instant> startInstant() { |
|
610 |
return (startTime > 0) |
|
611 |
? Optional.of(Instant.ofEpochMilli(startTime)) |
|
612 |
: Optional.empty(); |
|
613 |
} |
|
614 |
||
615 |
@Override |
|
616 |
public Optional<Duration> totalCpuDuration() { |
|
617 |
return (totalTime != -1) |
|
618 |
? Optional.of(Duration.ofNanos(totalTime)) |
|
619 |
: Optional.empty(); |
|
620 |
} |
|
621 |
||
622 |
@Override |
|
623 |
public Optional<String> user() { |
|
624 |
return Optional.ofNullable(user); |
|
625 |
} |
|
626 |
||
627 |
@Override |
|
628 |
public String toString() { |
|
629 |
StringBuilder sb = new StringBuilder(60); |
|
630 |
sb.append('['); |
|
631 |
if (user != null) { |
|
632 |
sb.append("user: "); |
|
633 |
sb.append(user()); |
|
634 |
} |
|
635 |
if (command != null) { |
|
636 |
if (sb.length() != 0) sb.append(", "); |
|
637 |
sb.append("cmd: "); |
|
638 |
sb.append(command); |
|
639 |
} |
|
640 |
if (arguments != null && arguments.length > 0) { |
|
641 |
if (sb.length() != 0) sb.append(", "); |
|
642 |
sb.append("args: "); |
|
643 |
sb.append(Arrays.toString(arguments)); |
|
644 |
} |
|
32209
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
645 |
if (commandLine != null) { |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
646 |
if (sb.length() != 0) sb.append(", "); |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
647 |
sb.append("cmdLine: "); |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
648 |
sb.append(commandLine); |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31682
diff
changeset
|
649 |
} |
31682
c19dcf5e0b6d
8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents:
30899
diff
changeset
|
650 |
if (startTime > 0) { |
30899 | 651 |
if (sb.length() != 0) sb.append(", "); |
652 |
sb.append("startTime: "); |
|
653 |
sb.append(startInstant()); |
|
654 |
} |
|
655 |
if (totalTime != -1) { |
|
656 |
if (sb.length() != 0) sb.append(", "); |
|
657 |
sb.append("totalTime: "); |
|
658 |
sb.append(totalCpuDuration().toString()); |
|
659 |
} |
|
660 |
sb.append(']'); |
|
661 |
return sb.toString(); |
|
662 |
} |
|
663 |
} |
|
664 |
} |