25N/A/*
3261N/A * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang;
0N/A
0N/Aimport java.io.*;
0N/A
0N/A/* java.lang.Process subclass in the UNIX environment.
0N/A *
0N/A * @author Mario Wolczko and Ross Knippel.
0N/A */
0N/A
0N/Afinal class UNIXProcess extends Process {
25N/A private static final sun.misc.JavaIOFileDescriptorAccess fdAccess
25N/A = sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess();
25N/A
25N/A private final int pid;
0N/A private int exitcode;
0N/A private boolean hasExited;
0N/A
0N/A private OutputStream stdin_stream;
25N/A private InputStream stdout_stream;
0N/A private DeferredCloseInputStream stdout_inner_stream;
25N/A private InputStream stderr_stream;
0N/A
0N/A /* this is for the reaping thread */
0N/A private native int waitForProcessExit(int pid);
0N/A
25N/A /**
25N/A * Create a process using fork(2) and exec(2).
25N/A *
25N/A * @param std_fds array of file descriptors. Indexes 0, 1, and
25N/A * 2 correspond to standard input, standard output and
25N/A * standard error, respectively. On input, a value of -1
25N/A * means to create a pipe to connect child and parent
25N/A * processes. On output, a value which is not -1 is the
25N/A * parent pipe fd corresponding to the pipe which has
25N/A * been created. An element of this array is -1 on input
25N/A * if and only if it is <em>not</em> -1 on output.
25N/A * @return the pid of the subprocess
25N/A */
0N/A private native int forkAndExec(byte[] prog,
25N/A byte[] argBlock, int argc,
25N/A byte[] envBlock, int envc,
25N/A byte[] dir,
25N/A int[] std_fds,
25N/A boolean redirectErrorStream)
25N/A throws IOException;
0N/A
0N/A UNIXProcess(final byte[] prog,
25N/A final byte[] argBlock, int argc,
25N/A final byte[] envBlock, int envc,
25N/A final byte[] dir,
25N/A final int[] std_fds,
25N/A final boolean redirectErrorStream)
0N/A throws IOException {
25N/A pid = forkAndExec(prog,
25N/A argBlock, argc,
25N/A envBlock, envc,
25N/A dir,
25N/A std_fds,
25N/A redirectErrorStream);
0N/A
25N/A java.security.AccessController.doPrivileged(
25N/A new java.security.PrivilegedAction<Void>() { public Void run() {
25N/A if (std_fds[0] == -1)
2474N/A stdin_stream = ProcessBuilder.NullOutputStream.INSTANCE;
25N/A else {
25N/A FileDescriptor stdin_fd = new FileDescriptor();
25N/A fdAccess.set(stdin_fd, std_fds[0]);
25N/A stdin_stream = new BufferedOutputStream(
25N/A new FileOutputStream(stdin_fd));
25N/A }
0N/A
25N/A if (std_fds[1] == -1)
2474N/A stdout_stream = ProcessBuilder.NullInputStream.INSTANCE;
25N/A else {
25N/A FileDescriptor stdout_fd = new FileDescriptor();
25N/A fdAccess.set(stdout_fd, std_fds[1]);
25N/A stdout_inner_stream = new DeferredCloseInputStream(stdout_fd);
25N/A stdout_stream = new BufferedInputStream(stdout_inner_stream);
25N/A }
0N/A
25N/A if (std_fds[2] == -1)
2474N/A stderr_stream = ProcessBuilder.NullInputStream.INSTANCE;
25N/A else {
25N/A FileDescriptor stderr_fd = new FileDescriptor();
25N/A fdAccess.set(stderr_fd, std_fds[2]);
25N/A stderr_stream = new DeferredCloseInputStream(stderr_fd);
25N/A }
25N/A
25N/A return null; }});
0N/A
25N/A /*
25N/A * For each subprocess forked a corresponding reaper thread
25N/A * is started. That thread is the only thread which waits
25N/A * for the subprocess to terminate and it doesn't hold any
25N/A * locks while doing so. This design allows waitFor() and
25N/A * exitStatus() to be safely executed in parallel (and they
25N/A * need no native code).
25N/A */
25N/A
25N/A java.security.AccessController.doPrivileged(
25N/A new java.security.PrivilegedAction<Void>() { public Void run() {
25N/A Thread t = new Thread("process reaper") {
25N/A public void run() {
25N/A int res = waitForProcessExit(pid);
25N/A synchronized (UNIXProcess.this) {
25N/A hasExited = true;
25N/A exitcode = res;
25N/A UNIXProcess.this.notifyAll();
25N/A }
25N/A }
25N/A };
25N/A t.setDaemon(true);
25N/A t.start();
25N/A return null; }});
0N/A }
0N/A
0N/A public OutputStream getOutputStream() {
25N/A return stdin_stream;
0N/A }
0N/A
0N/A public InputStream getInputStream() {
25N/A return stdout_stream;
0N/A }
0N/A
0N/A public InputStream getErrorStream() {
25N/A return stderr_stream;
0N/A }
0N/A
0N/A public synchronized int waitFor() throws InterruptedException {
0N/A while (!hasExited) {
25N/A wait();
25N/A }
25N/A return exitcode;
0N/A }
0N/A
0N/A public synchronized int exitValue() {
25N/A if (!hasExited) {
25N/A throw new IllegalThreadStateException("process hasn't exited");
25N/A }
25N/A return exitcode;
0N/A }
0N/A
0N/A private static native void destroyProcess(int pid);
0N/A public synchronized void destroy() {
25N/A // There is a risk that pid will be recycled, causing us to
25N/A // kill the wrong process! So we only terminate processes
25N/A // that appear to still be running. Even with this check,
25N/A // there is an unavoidable race condition here, but the window
25N/A // is very small, and OSes try hard to not recycle pids too
25N/A // soon, so this is quite safe.
25N/A if (!hasExited)
25N/A destroyProcess(pid);
25N/A try {
0N/A stdin_stream.close();
25N/A if (stdout_inner_stream != null)
25N/A stdout_inner_stream.closeDeferred(stdout_stream);
25N/A if (stderr_stream instanceof DeferredCloseInputStream)
25N/A ((DeferredCloseInputStream) stderr_stream)
25N/A .closeDeferred(stderr_stream);
0N/A } catch (IOException e) {
0N/A // ignore
0N/A }
0N/A }
0N/A
0N/A // A FileInputStream that supports the deferment of the actual close
0N/A // operation until the last pending I/O operation on the stream has
0N/A // finished. This is required on Solaris because we must close the stdin
0N/A // and stdout streams in the destroy method in order to reclaim the
0N/A // underlying file descriptors. Doing so, however, causes any thread
0N/A // currently blocked in a read on one of those streams to receive an
0N/A // IOException("Bad file number"), which is incompatible with historical
0N/A // behavior. By deferring the close we allow any pending reads to see -1
0N/A // (EOF) as they did before.
0N/A //
0N/A private static class DeferredCloseInputStream
25N/A extends FileInputStream
0N/A {
0N/A
25N/A private DeferredCloseInputStream(FileDescriptor fd) {
25N/A super(fd);
25N/A }
0N/A
25N/A private Object lock = new Object(); // For the following fields
25N/A private boolean closePending = false;
25N/A private int useCount = 0;
25N/A private InputStream streamToClose;
0N/A
25N/A private void raise() {
25N/A synchronized (lock) {
25N/A useCount++;
25N/A }
25N/A }
0N/A
25N/A private void lower() throws IOException {
25N/A synchronized (lock) {
25N/A useCount--;
25N/A if (useCount == 0 && closePending) {
25N/A streamToClose.close();
25N/A }
25N/A }
25N/A }
0N/A
25N/A // stc is the actual stream to be closed; it might be this object, or
25N/A // it might be an upstream object for which this object is downstream.
25N/A //
25N/A private void closeDeferred(InputStream stc) throws IOException {
25N/A synchronized (lock) {
25N/A if (useCount == 0) {
25N/A stc.close();
25N/A } else {
25N/A closePending = true;
25N/A streamToClose = stc;
25N/A }
25N/A }
25N/A }
0N/A
25N/A public void close() throws IOException {
25N/A synchronized (lock) {
25N/A useCount = 0;
25N/A closePending = false;
25N/A }
25N/A super.close();
25N/A }
0N/A
25N/A public int read() throws IOException {
25N/A raise();
25N/A try {
25N/A return super.read();
25N/A } finally {
25N/A lower();
25N/A }
25N/A }
0N/A
25N/A public int read(byte[] b) throws IOException {
25N/A raise();
25N/A try {
25N/A return super.read(b);
25N/A } finally {
25N/A lower();
25N/A }
25N/A }
0N/A
25N/A public int read(byte[] b, int off, int len) throws IOException {
25N/A raise();
25N/A try {
25N/A return super.read(b, off, len);
25N/A } finally {
25N/A lower();
25N/A }
25N/A }
0N/A
25N/A public long skip(long n) throws IOException {
25N/A raise();
25N/A try {
25N/A return super.skip(n);
25N/A } finally {
25N/A lower();
25N/A }
25N/A }
0N/A
25N/A public int available() throws IOException {
25N/A raise();
25N/A try {
25N/A return super.available();
25N/A } finally {
25N/A lower();
25N/A }
25N/A }
0N/A
0N/A }
0N/A
0N/A /* This routine initializes JNI field offsets for the class */
0N/A private static native void initIDs();
0N/A
0N/A static {
25N/A initIDs();
0N/A }
0N/A}