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