UNIXProcess.java.solaris revision 0
0N/A/*
1472N/A * Copyright 1995-2006 Sun Microsystems, Inc. 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
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun 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,
1472N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1472N/A *
1472N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any 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 {
0N/A private FileDescriptor stdin_fd;
0N/A private FileDescriptor stdout_fd;
0N/A private FileDescriptor stderr_fd;
0N/A private int pid;
0N/A private int exitcode;
0N/A private boolean hasExited;
0N/A
0N/A private OutputStream stdin_stream;
0N/A private BufferedInputStream stdout_stream;
0N/A private DeferredCloseInputStream stdout_inner_stream;
0N/A private DeferredCloseInputStream stderr_stream;
0N/A
0N/A /* this is for the reaping thread */
0N/A private native int waitForProcessExit(int pid);
0N/A
0N/A private native int forkAndExec(byte[] prog,
0N/A byte[] argBlock, int argc,
0N/A byte[] envBlock, int envc,
0N/A byte[] dir,
0N/A boolean redirectErrorStream,
0N/A FileDescriptor stdin_fd,
0N/A FileDescriptor stdout_fd,
0N/A FileDescriptor stderr_fd)
0N/A throws IOException;
0N/A
0N/A UNIXProcess(final byte[] prog,
0N/A final byte[] argBlock, int argc,
0N/A final byte[] envBlock, int envc,
0N/A final byte[] dir,
0N/A final boolean redirectErrorStream)
0N/A throws IOException {
0N/A stdin_fd = new FileDescriptor();
0N/A stdout_fd = new FileDescriptor();
0N/A stderr_fd = new FileDescriptor();
0N/A
0N/A pid = forkAndExec(prog,
0N/A argBlock, argc,
0N/A envBlock, envc,
0N/A dir,
0N/A redirectErrorStream,
0N/A stdin_fd, stdout_fd, stderr_fd);
0N/A
0N/A java.security.AccessController.doPrivileged(
0N/A new java.security.PrivilegedAction() {
0N/A public Object run() {
0N/A stdin_stream
0N/A = new BufferedOutputStream(new FileOutputStream(stdin_fd));
0N/A stdout_inner_stream = new DeferredCloseInputStream(stdout_fd);
0N/A stdout_stream = new BufferedInputStream(stdout_inner_stream);
0N/A stderr_stream = new DeferredCloseInputStream(stderr_fd);
0N/A return null;
1010N/A }
0N/A });
113N/A
113N/A /*
113N/A * For each subprocess forked a corresponding reaper thread
0N/A * is started. That thread is the only thread which waits
1601N/A * for the subprocess to terminate and it doesn't hold any
0N/A * locks while doing so. This design allows waitFor() and
0N/A * exitStatus() to be safely executed in parallel (and they
0N/A * need no native code).
1601N/A */
1601N/A
1601N/A java.security.AccessController.doPrivileged(
1601N/A new java.security.PrivilegedAction() {
1601N/A public Object run() {
0N/A Thread t = new Thread("process reaper") {
0N/A public void run() {
1010N/A int res = waitForProcessExit(pid);
1010N/A synchronized (UNIXProcess.this) {
1010N/A hasExited = true;
1010N/A exitcode = res;
1010N/A UNIXProcess.this.notifyAll();
1010N/A }
0N/A }
0N/A };
0N/A t.setDaemon(true);
0N/A t.start();
0N/A return null;
0N/A }
0N/A });
0N/A }
0N/A
0N/A public OutputStream getOutputStream() {
0N/A return stdin_stream;
0N/A }
0N/A
0N/A public InputStream getInputStream() {
0N/A return stdout_stream;
0N/A }
0N/A
0N/A public InputStream getErrorStream() {
0N/A return stderr_stream;
0N/A }
0N/A
0N/A public synchronized int waitFor() throws InterruptedException {
0N/A while (!hasExited) {
0N/A wait();
0N/A }
0N/A return exitcode;
1010N/A }
1010N/A
1010N/A public synchronized int exitValue() {
1010N/A if (!hasExited) {
0N/A throw new IllegalThreadStateException("process hasn't exited");
0N/A }
0N/A return exitcode;
0N/A }
0N/A
0N/A private static native void destroyProcess(int pid);
0N/A public synchronized void destroy() {
0N/A // There is a risk that pid will be recycled, causing us to
0N/A // kill the wrong process! So we only terminate processes
0N/A // that appear to still be running. Even with this check,
0N/A // there is an unavoidable race condition here, but the window
0N/A // is very small, and OSes try hard to not recycle pids too
0N/A // soon, so this is quite safe.
0N/A if (!hasExited)
0N/A destroyProcess(pid);
0N/A try {
0N/A stdin_stream.close();
0N/A stdout_inner_stream.closeDeferred(stdout_stream);
0N/A stderr_stream.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
0N/A extends FileInputStream
0N/A {
0N/A
0N/A private DeferredCloseInputStream(FileDescriptor fd) {
0N/A super(fd);
0N/A }
0N/A
0N/A private Object lock = new Object(); // For the following fields
0N/A private boolean closePending = false;
0N/A private int useCount = 0;
0N/A private InputStream streamToClose;
0N/A
0N/A private void raise() {
0N/A synchronized (lock) {
0N/A useCount++;
0N/A }
0N/A }
0N/A
0N/A private void lower() throws IOException {
0N/A synchronized (lock) {
0N/A useCount--;
0N/A if (useCount == 0 && closePending) {
0N/A streamToClose.close();
0N/A }
0N/A }
1010N/A }
1010N/A
1010N/A // stc is the actual stream to be closed; it might be this object, or
1010N/A // it might be an upstream object for which this object is downstream.
1010N/A //
1010N/A private void closeDeferred(InputStream stc) throws IOException {
0N/A synchronized (lock) {
0N/A if (useCount == 0) {
0N/A stc.close();
0N/A } else {
0N/A closePending = true;
0N/A streamToClose = stc;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void close() throws IOException {
0N/A synchronized (lock) {
0N/A useCount = 0;
0N/A closePending = false;
0N/A }
0N/A super.close();
0N/A }
0N/A
0N/A public int read() throws IOException {
0N/A raise();
0N/A try {
0N/A return super.read();
0N/A } finally {
0N/A lower();
0N/A }
0N/A }
0N/A
0N/A public int read(byte[] b) throws IOException {
0N/A raise();
0N/A try {
1010N/A return super.read(b);
0N/A } finally {
0N/A lower();
0N/A }
1010N/A }
0N/A
0N/A public int read(byte[] b, int off, int len) throws IOException {
1010N/A raise();
1010N/A try {
1010N/A return super.read(b, off, len);
1010N/A } finally {
0N/A lower();
0N/A }
0N/A }
0N/A
0N/A public long skip(long n) throws IOException {
0N/A raise();
0N/A try {
1010N/A return super.skip(n);
0N/A } finally {
0N/A lower();
0N/A }
0N/A }
0N/A
0N/A public int available() throws IOException {
0N/A raise();
0N/A try {
0N/A return super.available();
0N/A } finally {
0N/A lower();
0N/A }
0N/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 {
0N/A initIDs();
0N/A }
0N/A}
0N/A