jdk/src/solaris/classes/java/lang/UNIXProcess.java.linux
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 48 dc5744ca15ea
child 5786 f60ef38202e7
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 48
diff changeset
     2
 * Copyright (c) 1995, 2008, 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: 48
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: 48
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: 48
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 48
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 48
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/* java.lang.Process subclass in the UNIX environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author Mario Wolczko and Ross Knippel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @author Konstantin Kladko (ported to Linux)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
final class UNIXProcess extends Process {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    37
    private static final sun.misc.JavaIOFileDescriptorAccess fdAccess
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    38
        = sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    39
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private int pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private int exitcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private boolean hasExited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private OutputStream stdin_stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private InputStream  stdout_stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private InputStream  stderr_stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /* this is for the reaping thread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private native int waitForProcessExit(int pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    51
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    52
     * Create a process using fork(2) and exec(2).
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    53
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    54
     * @param std_fds array of file descriptors.  Indexes 0, 1, and
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    55
     *        2 correspond to standard input, standard output and
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    56
     *        standard error, respectively.  On input, a value of -1
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    57
     *        means to create a pipe to connect child and parent
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    58
     *        processes.  On output, a value which is not -1 is the
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    59
     *        parent pipe fd corresponding to the pipe which has
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    60
     *        been created.  An element of this array is -1 on input
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    61
     *        if and only if it is <em>not</em> -1 on output.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    62
     * @return the pid of the subprocess
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    63
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private native int forkAndExec(byte[] prog,
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    65
                                   byte[] argBlock, int argc,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    66
                                   byte[] envBlock, int envc,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    67
                                   byte[] dir,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    68
                                   int[] std_fds,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    69
                                   boolean redirectErrorStream)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    70
        throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /* In the process constructor we wait on this gate until the process    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /* has been created. Then we return from the constructor.               */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /* fork() is called by the same thread which later waits for the process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /* to terminate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static class Gate {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        private boolean exited = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        private IOException savedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        synchronized void exit() { /* Opens the gate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
           exited = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
           this.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        synchronized void waitForExit() { /* wait until the gate is open */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            boolean interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            while (!exited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    this.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    interrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (interrupted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        void setException (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            savedException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        IOException getException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return savedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    UNIXProcess(final byte[] prog,
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   111
                final byte[] argBlock, final int argc,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   112
                final byte[] envBlock, final int envc,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   113
                final byte[] dir,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   114
                final int[] std_fds,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   115
                final boolean redirectErrorStream)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        final Gate gate = new Gate();
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   119
        /*
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   120
         * For each subprocess forked a corresponding reaper thread
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   121
         * is started.  That thread is the only thread which waits
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   122
         * for the subprocess to terminate and it doesn't hold any
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   123
         * locks while doing so.  This design allows waitFor() and
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   124
         * exitStatus() to be safely executed in parallel (and they
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   125
         * need no native code).
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   126
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   128
        java.security.AccessController.doPrivileged(
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   129
        new java.security.PrivilegedAction<Void>() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   130
        public Void run() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   131
            Thread t = new Thread("process reaper") {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   132
                    public void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                            pid = forkAndExec(prog,
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   135
                                              argBlock, argc,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   136
                                              envBlock, envc,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   137
                                              dir,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   138
                                              std_fds,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   139
                                              redirectErrorStream);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                            gate.setException(e); /*remember to rethrow later*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                            gate.exit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        java.security.AccessController.doPrivileged(
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   146
                    new java.security.PrivilegedAction<Void>() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   147
                    public Void run() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   148
                        if (std_fds[0] == -1)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   149
                            stdin_stream = new ProcessBuilder.NullOutputStream();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   150
                        else {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   151
                            FileDescriptor stdin_fd = new FileDescriptor();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   152
                            fdAccess.set(stdin_fd, std_fds[0]);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   153
                            stdin_stream = new BufferedOutputStream(
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   154
                                new FileOutputStream(stdin_fd));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   155
                        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   156
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   157
                        if (std_fds[1] == -1)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   158
                            stdout_stream = new ProcessBuilder.NullInputStream();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   159
                        else {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   160
                            FileDescriptor stdout_fd = new FileDescriptor();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   161
                            fdAccess.set(stdout_fd, std_fds[1]);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   162
                            stdout_stream = new BufferedInputStream(
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   163
                                new FileInputStream(stdout_fd));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        }
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   165
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   166
                        if (std_fds[2] == -1)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   167
                            stderr_stream = new ProcessBuilder.NullInputStream();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   168
                        else {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   169
                            FileDescriptor stderr_fd = new FileDescriptor();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   170
                            fdAccess.set(stderr_fd, std_fds[2]);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   171
                            stderr_stream = new FileInputStream(stderr_fd);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   172
                        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   173
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   174
                        return null; }});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        gate.exit(); /* exit from constructor */
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   176
                        int res = waitForProcessExit(pid);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   177
                        synchronized (UNIXProcess.this) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   178
                            hasExited = true;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   179
                            exitcode = res;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   180
                            UNIXProcess.this.notifyAll();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   181
                        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   182
                    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   183
                };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                t.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                t.start();
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   186
                return null; }});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        gate.waitForExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        IOException e = gate.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (e != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw new IOException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public OutputStream getOutputStream() {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   194
        return stdin_stream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public InputStream getInputStream() {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   198
        return stdout_stream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public InputStream getErrorStream() {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   202
        return stderr_stream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public synchronized int waitFor() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        while (!hasExited) {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   207
            wait();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   208
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   209
        return exitcode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public synchronized int exitValue() {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   213
        if (!hasExited) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   214
            throw new IllegalThreadStateException("process hasn't exited");
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
        return exitcode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private static native void destroyProcess(int pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public void destroy() {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   221
        // There is a risk that pid will be recycled, causing us to
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   222
        // kill the wrong process!  So we only terminate processes
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   223
        // that appear to still be running.  Even with this check,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   224
        // there is an unavoidable race condition here, but the window
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   225
        // is very small, and OSes try hard to not recycle pids too
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   226
        // soon, so this is quite safe.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   227
        synchronized (this) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   228
            if (!hasExited)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   229
                destroyProcess(pid);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   230
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            stdin_stream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            stdout_stream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            stderr_stream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /* This routine initializes JNI field offsets for the class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    static {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   244
        initIDs();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
}