src/java.base/unix/classes/java/lang/ProcessImpl.java
author ihse
Mon, 19 Feb 2018 23:09:18 +0100
branchihse-cflags-rewrite-branch
changeset 56154 af8654dcd46a
parent 47216 71c04702a3d5
child 50817 fa1e04811ff6
permissions -rw-r--r--
Fix syntax errors.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44640
590dec7cadb4 8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents: 42753
diff changeset
     2
 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    28
import java.lang.ProcessBuilder.Redirect;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    29
import java.io.BufferedInputStream;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    30
import java.io.BufferedOutputStream;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    31
import java.io.ByteArrayInputStream;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    32
import java.io.FileDescriptor;
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    33
import java.io.FileInputStream;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    34
import java.io.FileOutputStream;
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    35
import java.io.IOException;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    36
import java.io.InputStream;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    37
import java.io.OutputStream;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    38
import java.util.Arrays;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    39
import java.util.EnumSet;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    40
import java.util.Locale;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    41
import java.util.Set;
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
    42
import java.util.concurrent.CompletableFuture;
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    43
import java.util.concurrent.TimeUnit;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    44
import java.security.AccessController;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    45
import static java.security.AccessController.doPrivileged;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    46
import java.security.PrivilegedAction;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    47
import java.security.PrivilegedActionException;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    48
import java.security.PrivilegedExceptionAction;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33826
diff changeset
    49
import java.util.Properties;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31682
diff changeset
    50
import jdk.internal.misc.JavaIOFileDescriptorAccess;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31682
diff changeset
    51
import jdk.internal.misc.SharedSecrets;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33826
diff changeset
    52
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
    55
 * java.lang.Process subclass in the UNIX environment.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    57
 * @author Mario Wolczko and Ross Knippel.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    58
 * @author Konstantin Kladko (ported to Linux and Bsd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Martin Buchholz
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    60
 * @author Volker Simonis (ported to AIX)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    63
final class ProcessImpl extends Process {
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31682
diff changeset
    64
    private static final JavaIOFileDescriptorAccess fdAccess
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31682
diff changeset
    65
        = SharedSecrets.getJavaIOFileDescriptorAccess();
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    66
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
    67
    // Linux platforms support a normal (non-forcible) kill signal.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
    68
    static final boolean SUPPORTS_NORMAL_TERMINATION = true;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
    69
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    70
    private final int pid;
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 31182
diff changeset
    71
    private final ProcessHandleImpl processHandle;
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    72
    private int exitcode;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    73
    private boolean hasExited;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    74
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    75
    private /* final */ OutputStream stdin;
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
    76
    private /* final */ InputStream  stdout;
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    77
    private /* final */ InputStream  stderr;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    78
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    79
    // only used on Solaris
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    80
    private /* final */ DeferredCloseInputStream stdout_inner_stream;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    81
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    82
    private static enum LaunchMechanism {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    83
        // order IS important!
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    84
        FORK,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    85
        POSIX_SPAWN,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    86
        VFORK
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    87
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    88
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    89
    private static enum Platform {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    90
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    91
        LINUX(LaunchMechanism.VFORK, LaunchMechanism.FORK),
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    92
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    93
        BSD(LaunchMechanism.POSIX_SPAWN, LaunchMechanism.FORK),
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    94
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    95
        SOLARIS(LaunchMechanism.POSIX_SPAWN, LaunchMechanism.FORK),
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    96
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    97
        AIX(LaunchMechanism.POSIX_SPAWN, LaunchMechanism.FORK);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    98
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
    99
        final LaunchMechanism defaultLaunchMechanism;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   100
        final Set<LaunchMechanism> validLaunchMechanisms;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   101
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   102
        Platform(LaunchMechanism ... launchMechanisms) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   103
            this.defaultLaunchMechanism = launchMechanisms[0];
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   104
            this.validLaunchMechanisms =
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   105
                EnumSet.copyOf(Arrays.asList(launchMechanisms));
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   106
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   107
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   108
        @SuppressWarnings("fallthrough")
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   109
        private String helperPath(String javahome, String osArch) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   110
            switch (this) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   111
                case SOLARIS:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   112
                    // fall through...
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   113
                case LINUX:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   114
                case AIX:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   115
                case BSD:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   116
                    return javahome + "/lib/jspawnhelper";
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   117
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   118
                default:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   119
                    throw new AssertionError("Unsupported platform: " + this);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   120
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   121
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   122
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   123
        String helperPath() {
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   124
            Properties props = GetPropertyAction.privilegedGetProperties();
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33826
diff changeset
   125
            return helperPath(props.getProperty("java.home"),
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33826
diff changeset
   126
                              props.getProperty("os.arch"));
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   127
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   128
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   129
        LaunchMechanism launchMechanism() {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   130
            return AccessController.doPrivileged(
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   131
                (PrivilegedAction<LaunchMechanism>) () -> {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   132
                    String s = System.getProperty(
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   133
                        "jdk.lang.Process.launchMechanism");
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   134
                    LaunchMechanism lm;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   135
                    if (s == null) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   136
                        lm = defaultLaunchMechanism;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   137
                        s = lm.name().toLowerCase(Locale.ENGLISH);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   138
                    } else {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   139
                        try {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   140
                            lm = LaunchMechanism.valueOf(
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   141
                                s.toUpperCase(Locale.ENGLISH));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   142
                        } catch (IllegalArgumentException e) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   143
                            lm = null;
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   144
                        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   145
                    }
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   146
                    if (lm == null || !validLaunchMechanisms.contains(lm)) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   147
                        throw new Error(
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   148
                            s + " is not a supported " +
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   149
                            "process launch mechanism on this platform."
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   150
                        );
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   151
                    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   152
                    return lm;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   153
                }
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   154
            );
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   155
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   156
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   157
        static Platform get() {
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   158
            String osName = GetPropertyAction.privilegedGetProperty("os.name");
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   159
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   160
            if (osName.equals("Linux")) { return LINUX; }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   161
            if (osName.contains("OS X")) { return BSD; }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   162
            if (osName.equals("SunOS")) { return SOLARIS; }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   163
            if (osName.equals("AIX")) { return AIX; }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   164
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   165
            throw new Error(osName + " is not a supported OS platform.");
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   166
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   167
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   168
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   169
    private static final Platform platform = Platform.get();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   170
    private static final LaunchMechanism launchMechanism = platform.launchMechanism();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   171
    private static final byte[] helperpath = toCString(platform.helperPath());
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   172
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private static byte[] toCString(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (s == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        byte[] bytes = s.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        byte[] result = new byte[bytes.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        System.arraycopy(bytes, 0,
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   179
                         result, 0,
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   180
                         bytes.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        result[result.length-1] = (byte)0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // Only for use by ProcessBuilder.start()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    static Process start(String[] cmdarray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                         java.util.Map<String,String> environment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                         String dir,
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   189
                         ProcessBuilder.Redirect[] redirects,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                         boolean redirectErrorStream)
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   191
            throws IOException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        assert cmdarray != null && cmdarray.length > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // Convert arguments to a contiguous block; it's easier to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // memory management in Java than in C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        byte[][] args = new byte[cmdarray.length-1][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        int size = args.length; // For added NUL bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        for (int i = 0; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            args[i] = cmdarray[i+1].getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            size += args[i].length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        byte[] argBlock = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        for (byte[] arg : args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            System.arraycopy(arg, 0, argBlock, i, arg.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            i += arg.length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            // No need to write NUL bytes explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int[] envc = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        byte[] envBlock = ProcessEnvironment.toEnvironmentBlock(environment, envc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   214
        int[] std_fds;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   215
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   216
        FileInputStream  f0 = null;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   217
        FileOutputStream f1 = null;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   218
        FileOutputStream f2 = null;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   219
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   220
        try {
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   221
            boolean forceNullOutputStream = false;
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   222
            if (redirects == null) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   223
                std_fds = new int[] { -1, -1, -1 };
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   224
            } else {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   225
                std_fds = new int[3];
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   226
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   227
                if (redirects[0] == Redirect.PIPE) {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   228
                    std_fds[0] = -1;
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   229
                } else if (redirects[0] == Redirect.INHERIT) {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   230
                    std_fds[0] = 0;
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   231
                } else if (redirects[0] instanceof ProcessBuilder.RedirectPipeImpl) {
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   232
                    std_fds[0] = fdAccess.get(((ProcessBuilder.RedirectPipeImpl) redirects[0]).getFd());
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   233
                } else {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   234
                    f0 = new FileInputStream(redirects[0].file());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   235
                    std_fds[0] = fdAccess.get(f0.getFD());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   236
                }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   237
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   238
                if (redirects[1] == Redirect.PIPE) {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   239
                    std_fds[1] = -1;
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   240
                } else if (redirects[1] == Redirect.INHERIT) {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   241
                    std_fds[1] = 1;
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   242
                } else if (redirects[1] instanceof ProcessBuilder.RedirectPipeImpl) {
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   243
                    std_fds[1] = fdAccess.get(((ProcessBuilder.RedirectPipeImpl) redirects[1]).getFd());
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   244
                    // Force getInputStream to return a null stream,
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   245
                    // the fd is directly assigned to the next process.
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   246
                    forceNullOutputStream = true;
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   247
                } else {
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   248
                    f1 = new FileOutputStream(redirects[1].file(),
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   249
                            redirects[1].append());
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   250
                    std_fds[1] = fdAccess.get(f1.getFD());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   251
                }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   252
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   253
                if (redirects[2] == Redirect.PIPE) {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   254
                    std_fds[2] = -1;
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   255
                } else if (redirects[2] == Redirect.INHERIT) {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   256
                    std_fds[2] = 2;
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   257
                } else if (redirects[2] instanceof ProcessBuilder.RedirectPipeImpl) {
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   258
                    std_fds[2] = fdAccess.get(((ProcessBuilder.RedirectPipeImpl) redirects[2]).getFd());
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   259
                } else {
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   260
                    f2 = new FileOutputStream(redirects[2].file(),
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   261
                            redirects[2].append());
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   262
                    std_fds[2] = fdAccess.get(f2.getFD());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   263
                }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   264
            }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   265
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   266
            Process p = new ProcessImpl
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   267
                    (toCString(cmdarray[0]),
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   268
                            argBlock, args.length,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   269
                            envBlock, envc[0],
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   270
                            toCString(dir),
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   271
                            std_fds,
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   272
                            forceNullOutputStream,
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   273
                            redirectErrorStream);
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   274
            if (redirects != null) {
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   275
                // Copy the fd's if they are to be redirected to another process
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   276
                if (std_fds[0] >= 0 &&
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   277
                        redirects[0] instanceof ProcessBuilder.RedirectPipeImpl) {
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   278
                    fdAccess.set(((ProcessBuilder.RedirectPipeImpl) redirects[0]).getFd(), std_fds[0]);
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   279
                }
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   280
                if (std_fds[1] >= 0 &&
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   281
                        redirects[1] instanceof ProcessBuilder.RedirectPipeImpl) {
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   282
                    fdAccess.set(((ProcessBuilder.RedirectPipeImpl) redirects[1]).getFd(), std_fds[1]);
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   283
                }
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   284
                if (std_fds[2] >= 0 &&
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   285
                        redirects[2] instanceof ProcessBuilder.RedirectPipeImpl) {
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   286
                    fdAccess.set(((ProcessBuilder.RedirectPipeImpl) redirects[2]).getFd(), std_fds[2]);
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   287
                }
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   288
            }
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   289
            return p;
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   290
        } finally {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   291
            // In theory, close() can throw IOException
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   292
            // (although it is rather unlikely to happen here)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   293
            try { if (f0 != null) f0.close(); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   294
            finally {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   295
                try { if (f1 != null) f1.close(); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   296
                finally { if (f2 != null) f2.close(); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   297
            }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   298
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   300
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   301
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   302
    /**
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   303
     * Creates a process. Depending on the {@code mode} flag, this is done by
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   304
     * one of the following mechanisms:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   305
     * <pre>
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   306
     *   1 - fork(2) and exec(2)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   307
     *   2 - posix_spawn(3P)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   308
     *   3 - vfork(2) and exec(2)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   309
     * </pre>
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   310
     * @param fds an array of three file descriptors.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   311
     *        Indexes 0, 1, and 2 correspond to standard input,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   312
     *        standard output and standard error, respectively.  On
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   313
     *        input, a value of -1 means to create a pipe to connect
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   314
     *        child and parent processes.  On output, a value which
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   315
     *        is not -1 is the parent pipe fd corresponding to the
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   316
     *        pipe which has been created.  An element of this array
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   317
     *        is -1 on input if and only if it is <em>not</em> -1 on
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   318
     *        output.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   319
     * @return the pid of the subprocess
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   320
     */
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   321
    private native int forkAndExec(int mode, byte[] helperpath,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   322
                                   byte[] prog,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   323
                                   byte[] argBlock, int argc,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   324
                                   byte[] envBlock, int envc,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   325
                                   byte[] dir,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   326
                                   int[] fds,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   327
                                   boolean redirectErrorStream)
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   328
        throws IOException;
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   329
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   330
    private ProcessImpl(final byte[] prog,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   331
                final byte[] argBlock, final int argc,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   332
                final byte[] envBlock, final int envc,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   333
                final byte[] dir,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   334
                final int[] fds,
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   335
                final boolean forceNullOutputStream,
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   336
                final boolean redirectErrorStream)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   337
            throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   338
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   339
        pid = forkAndExec(launchMechanism.ordinal() + 1,
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   340
                          helperpath,
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   341
                          prog,
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   342
                          argBlock, argc,
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   343
                          envBlock, envc,
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   344
                          dir,
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   345
                          fds,
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   346
                          redirectErrorStream);
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 31182
diff changeset
   347
        processHandle = ProcessHandleImpl.getInternal(pid);
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   348
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   349
        try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   350
            doPrivileged((PrivilegedExceptionAction<Void>) () -> {
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   351
                initStreams(fds, forceNullOutputStream);
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   352
                return null;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   353
            });
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   354
        } catch (PrivilegedActionException ex) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   355
            throw (IOException) ex.getException();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   356
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   357
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   358
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   359
    static FileDescriptor newFileDescriptor(int fd) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   360
        FileDescriptor fileDescriptor = new FileDescriptor();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   361
        fdAccess.set(fileDescriptor, fd);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   362
        return fileDescriptor;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   363
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   364
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   365
    /**
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   366
     * Initialize the streams from the file descriptors.
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   367
     * @param fds array of stdin, stdout, stderr fds
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   368
     * @param forceNullOutputStream true if the stdout is being directed to
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   369
     *        a subsequent process. The stdout stream should be a null output stream .
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   370
     * @throws IOException
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   371
     */
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   372
    void initStreams(int[] fds, boolean forceNullOutputStream) throws IOException {
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   373
        switch (platform) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   374
            case LINUX:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   375
            case BSD:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   376
                stdin = (fds[0] == -1) ?
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   377
                        ProcessBuilder.NullOutputStream.INSTANCE :
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   378
                        new ProcessPipeOutputStream(fds[0]);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   379
33826
a9e5f1b8ea57 8132394: (process) ProcessBuilder support for a pipeline of processes
rriggs
parents: 32834
diff changeset
   380
                stdout = (fds[1] == -1 || forceNullOutputStream) ?
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   381
                         ProcessBuilder.NullInputStream.INSTANCE :
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   382
                         new ProcessPipeInputStream(fds[1]);
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   383
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   384
                stderr = (fds[2] == -1) ?
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   385
                         ProcessBuilder.NullInputStream.INSTANCE :
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   386
                         new ProcessPipeInputStream(fds[2]);
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   387
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   388
                ProcessHandleImpl.completion(pid, true).handle((exitcode, throwable) -> {
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   389
                    synchronized (this) {
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   390
                        this.exitcode = (exitcode == null) ? -1 : exitcode.intValue();
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   391
                        this.hasExited = true;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   392
                        this.notifyAll();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   393
                    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   394
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   395
                    if (stdout instanceof ProcessPipeInputStream)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   396
                        ((ProcessPipeInputStream) stdout).processExited();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   397
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   398
                    if (stderr instanceof ProcessPipeInputStream)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   399
                        ((ProcessPipeInputStream) stderr).processExited();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   400
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   401
                    if (stdin instanceof ProcessPipeOutputStream)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   402
                        ((ProcessPipeOutputStream) stdin).processExited();
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   403
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   404
                    return null;
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   405
                });
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   406
                break;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   407
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   408
            case SOLARIS:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   409
                stdin = (fds[0] == -1) ?
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   410
                        ProcessBuilder.NullOutputStream.INSTANCE :
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   411
                        new BufferedOutputStream(
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   412
                            new FileOutputStream(newFileDescriptor(fds[0])));
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   413
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   414
                stdout = (fds[1] == -1) ?
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   415
                         ProcessBuilder.NullInputStream.INSTANCE :
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   416
                         new BufferedInputStream(
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   417
                             stdout_inner_stream =
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   418
                                 new DeferredCloseInputStream(
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   419
                                     newFileDescriptor(fds[1])));
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   420
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   421
                stderr = (fds[2] == -1) ?
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   422
                         ProcessBuilder.NullInputStream.INSTANCE :
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   423
                         new DeferredCloseInputStream(newFileDescriptor(fds[2]));
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   424
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   425
                /*
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   426
                 * For each subprocess forked a corresponding reaper task
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   427
                 * is submitted.  That task is the only thread which waits
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   428
                 * for the subprocess to terminate and it doesn't hold any
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   429
                 * locks while doing so.  This design allows waitFor() and
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   430
                 * exitStatus() to be safely executed in parallel (and they
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   431
                 * need no native code).
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   432
                 */
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   433
                ProcessHandleImpl.completion(pid, true).handle((exitcode, throwable) -> {
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   434
                    synchronized (this) {
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   435
                        this.exitcode = (exitcode == null) ? -1 : exitcode.intValue();
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   436
                        this.hasExited = true;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   437
                        this.notifyAll();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   438
                    }
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   439
                    return null;
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   440
                });
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   441
                break;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   442
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   443
            case AIX:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   444
                stdin = (fds[0] == -1) ?
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   445
                        ProcessBuilder.NullOutputStream.INSTANCE :
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   446
                        new ProcessPipeOutputStream(fds[0]);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   447
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   448
                stdout = (fds[1] == -1) ?
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   449
                         ProcessBuilder.NullInputStream.INSTANCE :
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   450
                         new DeferredCloseProcessPipeInputStream(fds[1]);
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   451
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   452
                stderr = (fds[2] == -1) ?
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   453
                         ProcessBuilder.NullInputStream.INSTANCE :
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   454
                         new DeferredCloseProcessPipeInputStream(fds[2]);
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   455
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   456
                ProcessHandleImpl.completion(pid, true).handle((exitcode, throwable) -> {
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   457
                    synchronized (this) {
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   458
                        this.exitcode = (exitcode == null) ? -1 : exitcode.intValue();
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   459
                        this.hasExited = true;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   460
                        this.notifyAll();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   461
                    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   462
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   463
                    if (stdout instanceof DeferredCloseProcessPipeInputStream)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   464
                        ((DeferredCloseProcessPipeInputStream) stdout).processExited();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   465
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   466
                    if (stderr instanceof DeferredCloseProcessPipeInputStream)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   467
                        ((DeferredCloseProcessPipeInputStream) stderr).processExited();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   468
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   469
                    if (stdin instanceof ProcessPipeOutputStream)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   470
                        ((ProcessPipeOutputStream) stdin).processExited();
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   471
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   472
                    return null;
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   473
                });
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   474
                break;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   475
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   476
            default: throw new AssertionError("Unsupported platform: " + platform);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   477
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   478
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   479
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   480
    public OutputStream getOutputStream() {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   481
        return stdin;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   482
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   483
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   484
    public InputStream getInputStream() {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   485
        return stdout;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   486
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   487
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   488
    public InputStream getErrorStream() {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   489
        return stderr;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   490
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   491
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   492
    public synchronized int waitFor() throws InterruptedException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   493
        while (!hasExited) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   494
            wait();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   495
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   496
        return exitcode;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   497
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   498
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   499
    @Override
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   500
    public synchronized boolean waitFor(long timeout, TimeUnit unit)
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   501
        throws InterruptedException
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   502
    {
29604
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 28665
diff changeset
   503
        long remainingNanos = unit.toNanos(timeout);    // throw NPE before other conditions
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   504
        if (hasExited) return true;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   505
        if (timeout <= 0) return false;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   506
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   507
        long deadline = System.nanoTime() + remainingNanos;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   508
        do {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   509
            // Round up to next millisecond
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   510
            wait(TimeUnit.NANOSECONDS.toMillis(remainingNanos + 999_999L));
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   511
            if (hasExited) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   512
                return true;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   513
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   514
            remainingNanos = deadline - System.nanoTime();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   515
        } while (remainingNanos > 0);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   516
        return hasExited;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   517
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   518
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   519
    public synchronized int exitValue() {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   520
        if (!hasExited) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   521
            throw new IllegalThreadStateException("process hasn't exited");
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   522
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   523
        return exitcode;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   524
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   525
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   526
    private void destroy(boolean force) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   527
        switch (platform) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   528
            case LINUX:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   529
            case BSD:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   530
            case AIX:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   531
                // There is a risk that pid will be recycled, causing us to
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   532
                // kill the wrong process!  So we only terminate processes
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   533
                // that appear to still be running.  Even with this check,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   534
                // there is an unavoidable race condition here, but the window
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   535
                // is very small, and OSes try hard to not recycle pids too
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   536
                // soon, so this is quite safe.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   537
                synchronized (this) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   538
                    if (!hasExited)
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 31182
diff changeset
   539
                        processHandle.destroyProcess(force);
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   540
                }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   541
                try { stdin.close();  } catch (IOException ignored) {}
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   542
                try { stdout.close(); } catch (IOException ignored) {}
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   543
                try { stderr.close(); } catch (IOException ignored) {}
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   544
                break;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   545
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   546
            case SOLARIS:
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   547
                // There is a risk that pid will be recycled, causing us to
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   548
                // kill the wrong process!  So we only terminate processes
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   549
                // that appear to still be running.  Even with this check,
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   550
                // there is an unavoidable race condition here, but the window
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   551
                // is very small, and OSes try hard to not recycle pids too
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   552
                // soon, so this is quite safe.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   553
                synchronized (this) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   554
                    if (!hasExited)
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 31182
diff changeset
   555
                        processHandle.destroyProcess(force);
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   556
                    try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   557
                        stdin.close();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   558
                        if (stdout_inner_stream != null)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   559
                            stdout_inner_stream.closeDeferred(stdout);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   560
                        if (stderr instanceof DeferredCloseInputStream)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   561
                            ((DeferredCloseInputStream) stderr)
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   562
                                .closeDeferred(stderr);
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   563
                    } catch (IOException e) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   564
                        // ignore
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   565
                    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   566
                }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   567
                break;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   568
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   569
            default: throw new AssertionError("Unsupported platform: " + platform);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   570
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   571
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   572
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   573
    @Override
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   574
    public CompletableFuture<Process> onExit() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   575
        return ProcessHandleImpl.completion(pid, false)
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 31182
diff changeset
   576
                .handleAsync((unusedExitStatus, unusedThrowable) -> {
31182
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   577
                    boolean interrupted = false;
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   578
                    while (true) {
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   579
                        // Ensure that the concurrent task setting the exit status has completed
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   580
                        try {
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   581
                            waitFor();
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   582
                            break;
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   583
                        } catch (InterruptedException ie) {
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   584
                            interrupted = true;
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   585
                        }
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   586
                    }
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   587
                    if (interrupted) {
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   588
                        Thread.currentThread().interrupt();
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   589
                    }
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   590
                    return this;
07956922e5d8 8086208: java/lang/ProcessHandle/OnExitTest.java: IllegalThreadStateException: process hasn't exited
rriggs
parents: 30899
diff changeset
   591
                });
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   592
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   593
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   594
    @Override
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   595
    public ProcessHandle toHandle() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   596
        SecurityManager sm = System.getSecurityManager();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   597
        if (sm != null) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   598
            sm.checkPermission(new RuntimePermission("manageProcess"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   599
        }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   600
        return processHandle;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   601
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   602
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   603
    @Override
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   604
    public boolean supportsNormalTermination() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   605
        return ProcessImpl.SUPPORTS_NORMAL_TERMINATION;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   606
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   607
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   608
    @Override
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   609
    public void destroy() {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   610
        destroy(false);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   611
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   612
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   613
    @Override
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   614
    public Process destroyForcibly() {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   615
        destroy(true);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   616
        return this;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   617
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   618
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   619
    @Override
44640
590dec7cadb4 8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents: 42753
diff changeset
   620
    public long pid() {
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   621
        return pid;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   622
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   623
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   624
    @Override
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   625
    public synchronized boolean isAlive() {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   626
        return !hasExited;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   627
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   628
40738
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 39061
diff changeset
   629
    /**
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 39061
diff changeset
   630
     * The {@code toString} method returns a string consisting of
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 39061
diff changeset
   631
     * the native process ID of the process and the exit value of the process.
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 39061
diff changeset
   632
     *
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 39061
diff changeset
   633
     * @return a string representation of the object.
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 39061
diff changeset
   634
     */
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 39061
diff changeset
   635
    @Override
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 39061
diff changeset
   636
    public String toString() {
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 39061
diff changeset
   637
        return new StringBuilder("Process[pid=").append(pid)
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 39061
diff changeset
   638
                .append(", exitValue=").append(hasExited ? exitcode : "\"not exited\"")
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 39061
diff changeset
   639
                .append("]").toString();
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 39061
diff changeset
   640
    }
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 39061
diff changeset
   641
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   642
    private static native void init();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   643
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   644
    static {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   645
        init();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   646
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   647
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   648
    /**
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   649
     * A buffered input stream for a subprocess pipe file descriptor
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   650
     * that allows the underlying file descriptor to be reclaimed when
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   651
     * the process exits, via the processExited hook.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   652
     *
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   653
     * This is tricky because we do not want the user-level InputStream to be
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   654
     * closed until the user invokes close(), and we need to continue to be
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   655
     * able to read any buffered data lingering in the OS pipe buffer.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   656
     */
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   657
    private static class ProcessPipeInputStream extends BufferedInputStream {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   658
        private final Object closeLock = new Object();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   659
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   660
        ProcessPipeInputStream(int fd) {
39061
0a7abfc4f4b0 8155808: Process.getInputStream().skip() method is faulty
rriggs
parents: 37781
diff changeset
   661
            super(new PipeInputStream(newFileDescriptor(fd)));
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   662
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   663
        private static byte[] drainInputStream(InputStream in)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   664
                throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   665
            int n = 0;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   666
            int j;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   667
            byte[] a = null;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   668
            while ((j = in.available()) > 0) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   669
                a = (a == null) ? new byte[j] : Arrays.copyOf(a, n + j);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   670
                n += in.read(a, n, j);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   671
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   672
            return (a == null || n == a.length) ? a : Arrays.copyOf(a, n);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   673
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   674
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   675
        /** Called by the process reaper thread when the process exits. */
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   676
        synchronized void processExited() {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   677
            synchronized (closeLock) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   678
                try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   679
                    InputStream in = this.in;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   680
                    // this stream is closed if and only if: in == null
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   681
                    if (in != null) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   682
                        byte[] stragglers = drainInputStream(in);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   683
                        in.close();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   684
                        this.in = (stragglers == null) ?
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   685
                            ProcessBuilder.NullInputStream.INSTANCE :
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   686
                            new ByteArrayInputStream(stragglers);
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   687
                    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   688
                } catch (IOException ignored) {}
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   689
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   690
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   691
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   692
        @Override
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   693
        public void close() throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   694
            // BufferedInputStream#close() is not synchronized unlike most other
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   695
            // methods. Synchronizing helps avoid race with processExited().
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   696
            synchronized (closeLock) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   697
                super.close();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   698
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   699
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   700
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   701
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   702
    /**
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   703
     * A buffered output stream for a subprocess pipe file descriptor
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   704
     * that allows the underlying file descriptor to be reclaimed when
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   705
     * the process exits, via the processExited hook.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   706
     */
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   707
    private static class ProcessPipeOutputStream extends BufferedOutputStream {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   708
        ProcessPipeOutputStream(int fd) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   709
            super(new FileOutputStream(newFileDescriptor(fd)));
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   710
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   711
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   712
        /** Called by the process reaper thread when the process exits. */
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   713
        synchronized void processExited() {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   714
            OutputStream out = this.out;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   715
            if (out != null) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   716
                try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   717
                    out.close();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   718
                } catch (IOException ignored) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   719
                    // We know of no reason to get an IOException, but if
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   720
                    // we do, there's nothing else to do but carry on.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   721
                }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   722
                this.out = ProcessBuilder.NullOutputStream.INSTANCE;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   723
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   724
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   725
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   726
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   727
    // A FileInputStream that supports the deferment of the actual close
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   728
    // operation until the last pending I/O operation on the stream has
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   729
    // finished.  This is required on Solaris because we must close the stdin
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   730
    // and stdout streams in the destroy method in order to reclaim the
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   731
    // underlying file descriptors.  Doing so, however, causes any thread
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   732
    // currently blocked in a read on one of those streams to receive an
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   733
    // IOException("Bad file number"), which is incompatible with historical
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   734
    // behavior.  By deferring the close we allow any pending reads to see -1
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   735
    // (EOF) as they did before.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   736
    //
39061
0a7abfc4f4b0 8155808: Process.getInputStream().skip() method is faulty
rriggs
parents: 37781
diff changeset
   737
    private static class DeferredCloseInputStream extends PipeInputStream {
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   738
        DeferredCloseInputStream(FileDescriptor fd) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   739
            super(fd);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   740
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   741
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   742
        private Object lock = new Object();     // For the following fields
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   743
        private boolean closePending = false;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   744
        private int useCount = 0;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   745
        private InputStream streamToClose;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   746
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   747
        private void raise() {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   748
            synchronized (lock) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   749
                useCount++;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   750
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   751
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   752
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   753
        private void lower() throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   754
            synchronized (lock) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   755
                useCount--;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   756
                if (useCount == 0 && closePending) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   757
                    streamToClose.close();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   758
                }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   759
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   760
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   761
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   762
        // stc is the actual stream to be closed; it might be this object, or
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   763
        // it might be an upstream object for which this object is downstream.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   764
        //
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   765
        private void closeDeferred(InputStream stc) throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   766
            synchronized (lock) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   767
                if (useCount == 0) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   768
                    stc.close();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   769
                } else {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   770
                    closePending = true;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   771
                    streamToClose = stc;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   772
                }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   773
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   774
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   775
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   776
        public void close() throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   777
            synchronized (lock) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   778
                useCount = 0;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   779
                closePending = false;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   780
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   781
            super.close();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   782
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   783
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   784
        public int read() throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   785
            raise();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   786
            try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   787
                return super.read();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   788
            } finally {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   789
                lower();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   790
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   791
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   792
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   793
        public int read(byte[] b) throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   794
            raise();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   795
            try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   796
                return super.read(b);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   797
            } finally {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   798
                lower();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   799
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   800
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   801
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   802
        public int read(byte[] b, int off, int len) throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   803
            raise();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   804
            try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   805
                return super.read(b, off, len);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   806
            } finally {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   807
                lower();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   808
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   809
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   810
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   811
        public long skip(long n) throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   812
            raise();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   813
            try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   814
                return super.skip(n);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   815
            } finally {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   816
                lower();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   817
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   818
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   819
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   820
        public int available() throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   821
            raise();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   822
            try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   823
                return super.available();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   824
            } finally {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   825
                lower();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   826
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   827
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   828
    }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   829
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   830
    /**
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   831
     * A buffered input stream for a subprocess pipe file descriptor
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   832
     * that allows the underlying file descriptor to be reclaimed when
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   833
     * the process exits, via the processExited hook.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   834
     *
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   835
     * This is tricky because we do not want the user-level InputStream to be
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   836
     * closed until the user invokes close(), and we need to continue to be
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   837
     * able to read any buffered data lingering in the OS pipe buffer.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   838
     *
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   839
     * On AIX this is especially tricky, because the 'close()' system call
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   840
     * will block if another thread is at the same time blocked in a file
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   841
     * operation (e.g. 'read()') on the same file descriptor. We therefore
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   842
     * combine 'ProcessPipeInputStream' approach used on Linux and Bsd
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   843
     * with the DeferredCloseInputStream approach used on Solaris. This means
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   844
     * that every potentially blocking operation on the file descriptor
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   845
     * increments a counter before it is executed and decrements it once it
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   846
     * finishes. The 'close()' operation will only be executed if there are
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   847
     * no pending operations. Otherwise it is deferred after the last pending
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   848
     * operation has finished.
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   849
     *
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   850
     */
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   851
    private static class DeferredCloseProcessPipeInputStream
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 29724
diff changeset
   852
        extends BufferedInputStream {
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   853
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   854
        private final Object closeLock = new Object();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   855
        private int useCount = 0;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   856
        private boolean closePending = false;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   857
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   858
        DeferredCloseProcessPipeInputStream(int fd) {
39061
0a7abfc4f4b0 8155808: Process.getInputStream().skip() method is faulty
rriggs
parents: 37781
diff changeset
   859
            super(new PipeInputStream(newFileDescriptor(fd)));
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   860
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   861
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   862
        private InputStream drainInputStream(InputStream in)
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   863
                throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   864
            int n = 0;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   865
            int j;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   866
            byte[] a = null;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   867
            synchronized (closeLock) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   868
                if (buf == null) // asynchronous close()?
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   869
                    return null; // discard
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   870
                j = in.available();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   871
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   872
            while (j > 0) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   873
                a = (a == null) ? new byte[j] : Arrays.copyOf(a, n + j);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   874
                synchronized (closeLock) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   875
                    if (buf == null) // asynchronous close()?
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   876
                        return null; // discard
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   877
                    n += in.read(a, n, j);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   878
                    j = in.available();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   879
                }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   880
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   881
            return (a == null) ?
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   882
                    ProcessBuilder.NullInputStream.INSTANCE :
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   883
                    new ByteArrayInputStream(n == a.length ? a : Arrays.copyOf(a, n));
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   884
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   885
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   886
        /** Called by the process reaper thread when the process exits. */
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   887
        synchronized void processExited() {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   888
            try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   889
                InputStream in = this.in;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   890
                if (in != null) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   891
                    InputStream stragglers = drainInputStream(in);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   892
                    in.close();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   893
                    this.in = stragglers;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   894
                }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   895
            } catch (IOException ignored) { }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   896
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   897
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   898
        private void raise() {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   899
            synchronized (closeLock) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   900
                useCount++;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   901
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   902
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   903
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   904
        private void lower() throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   905
            synchronized (closeLock) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   906
                useCount--;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   907
                if (useCount == 0 && closePending) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   908
                    closePending = false;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   909
                    super.close();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   910
                }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   911
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   912
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   913
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   914
        @Override
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   915
        public int read() throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   916
            raise();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   917
            try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   918
                return super.read();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   919
            } finally {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   920
                lower();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   921
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   922
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   923
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   924
        @Override
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   925
        public int read(byte[] b) throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   926
            raise();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   927
            try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   928
                return super.read(b);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   929
            } finally {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   930
                lower();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   931
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   932
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   933
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   934
        @Override
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   935
        public int read(byte[] b, int off, int len) throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   936
            raise();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   937
            try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   938
                return super.read(b, off, len);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   939
            } finally {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   940
                lower();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   941
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   942
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   943
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   944
        @Override
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   945
        public long skip(long n) throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   946
            raise();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   947
            try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   948
                return super.skip(n);
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   949
            } finally {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   950
                lower();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   951
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   952
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   953
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   954
        @Override
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   955
        public int available() throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   956
            raise();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   957
            try {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   958
                return super.available();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   959
            } finally {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   960
                lower();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   961
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   962
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   963
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   964
        @Override
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   965
        public void close() throws IOException {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   966
            // BufferedInputStream#close() is not synchronized unlike most other
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   967
            // methods. Synchronizing helps avoid racing with drainInputStream().
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   968
            synchronized (closeLock) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   969
                if (useCount == 0) {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   970
                    super.close();
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   971
                }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   972
                else {
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   973
                    closePending = true;
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   974
                }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   975
            }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   976
        }
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 25859
diff changeset
   977
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
}