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