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