jdk/src/solaris/classes/java/lang/UNIXProcess.java.solaris
changeset 2 90ce3da70b43
child 48 dc5744ca15ea
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /* 
       
     2  * Copyright 1995-2006 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package java.lang;
       
    27 
       
    28 import java.io.*;
       
    29 
       
    30 /* java.lang.Process subclass in the UNIX environment.
       
    31  *
       
    32  * @author Mario Wolczko and Ross Knippel.
       
    33  */
       
    34 
       
    35 final class UNIXProcess extends Process {
       
    36     private FileDescriptor stdin_fd;
       
    37     private FileDescriptor stdout_fd;
       
    38     private FileDescriptor stderr_fd;
       
    39     private int pid;
       
    40     private int exitcode;
       
    41     private boolean hasExited;
       
    42 
       
    43     private OutputStream stdin_stream;
       
    44     private BufferedInputStream stdout_stream;
       
    45     private DeferredCloseInputStream stdout_inner_stream;
       
    46     private DeferredCloseInputStream stderr_stream;
       
    47 
       
    48     /* this is for the reaping thread */
       
    49     private native int waitForProcessExit(int pid);
       
    50 
       
    51     private native int forkAndExec(byte[] prog,
       
    52 				   byte[] argBlock, int argc,
       
    53 				   byte[] envBlock, int envc,
       
    54 				   byte[] dir,
       
    55 				   boolean redirectErrorStream,
       
    56 				   FileDescriptor stdin_fd,
       
    57 				   FileDescriptor stdout_fd,
       
    58 				   FileDescriptor stderr_fd)
       
    59 	throws IOException;
       
    60 
       
    61     UNIXProcess(final byte[] prog,
       
    62 		final byte[] argBlock, int argc,
       
    63 		final byte[] envBlock, int envc,
       
    64 		final byte[] dir,
       
    65 		final boolean redirectErrorStream)
       
    66     throws IOException {
       
    67 	stdin_fd  = new FileDescriptor();
       
    68 	stdout_fd = new FileDescriptor();
       
    69 	stderr_fd = new FileDescriptor();
       
    70 
       
    71 	pid = forkAndExec(prog,
       
    72 			  argBlock, argc,
       
    73 			  envBlock, envc,
       
    74 			  dir,
       
    75 			  redirectErrorStream,
       
    76 			  stdin_fd, stdout_fd, stderr_fd);
       
    77 
       
    78 	java.security.AccessController.doPrivileged(
       
    79 				    new java.security.PrivilegedAction() {
       
    80 	    public Object run() {
       
    81 	        stdin_stream
       
    82 		    = new BufferedOutputStream(new FileOutputStream(stdin_fd));
       
    83 		stdout_inner_stream = new DeferredCloseInputStream(stdout_fd);
       
    84 	        stdout_stream = new BufferedInputStream(stdout_inner_stream);
       
    85 	        stderr_stream = new DeferredCloseInputStream(stderr_fd);
       
    86 		return null;
       
    87 	    }
       
    88 	});
       
    89 
       
    90 	/*
       
    91 	 * For each subprocess forked a corresponding reaper thread
       
    92 	 * is started.  That thread is the only thread which waits
       
    93 	 * for the subprocess to terminate and it doesn't hold any
       
    94 	 * locks while doing so.  This design allows waitFor() and
       
    95 	 * exitStatus() to be safely executed in parallel (and they
       
    96 	 * need no native code).
       
    97 	 */
       
    98 
       
    99 	java.security.AccessController.doPrivileged(
       
   100 			    new java.security.PrivilegedAction() {
       
   101 	    public Object run() {
       
   102 		Thread t = new Thread("process reaper") {
       
   103 		    public void run() {
       
   104 			int res = waitForProcessExit(pid);
       
   105 			synchronized (UNIXProcess.this) {
       
   106 			    hasExited = true;
       
   107 			    exitcode = res;
       
   108 			    UNIXProcess.this.notifyAll();
       
   109 			}
       
   110 		    }
       
   111 		};
       
   112 		t.setDaemon(true);
       
   113 		t.start();
       
   114 		return null;
       
   115 	    }
       
   116 	});
       
   117     }
       
   118 
       
   119     public OutputStream getOutputStream() {
       
   120 	return stdin_stream;
       
   121     }
       
   122 
       
   123     public InputStream getInputStream() {
       
   124 	return stdout_stream;
       
   125     }
       
   126 
       
   127     public InputStream getErrorStream() {
       
   128 	return stderr_stream;
       
   129     }
       
   130 
       
   131     public synchronized int waitFor() throws InterruptedException {
       
   132         while (!hasExited) {
       
   133 	    wait();
       
   134 	}
       
   135 	return exitcode;
       
   136     }
       
   137 
       
   138     public synchronized int exitValue() {
       
   139 	if (!hasExited) {
       
   140 	    throw new IllegalThreadStateException("process hasn't exited");
       
   141 	}
       
   142 	return exitcode;
       
   143     }
       
   144 
       
   145     private static native void destroyProcess(int pid);
       
   146     public synchronized void destroy() {
       
   147 	// There is a risk that pid will be recycled, causing us to
       
   148 	// kill the wrong process!  So we only terminate processes
       
   149 	// that appear to still be running.  Even with this check,
       
   150 	// there is an unavoidable race condition here, but the window
       
   151 	// is very small, and OSes try hard to not recycle pids too
       
   152 	// soon, so this is quite safe.
       
   153 	if (!hasExited)
       
   154 	    destroyProcess(pid);
       
   155 	try {
       
   156             stdin_stream.close();
       
   157 	    stdout_inner_stream.closeDeferred(stdout_stream);
       
   158 	    stderr_stream.closeDeferred(stderr_stream);
       
   159         } catch (IOException e) {
       
   160             // ignore
       
   161         }
       
   162     }
       
   163 
       
   164     // A FileInputStream that supports the deferment of the actual close
       
   165     // operation until the last pending I/O operation on the stream has
       
   166     // finished.  This is required on Solaris because we must close the stdin
       
   167     // and stdout streams in the destroy method in order to reclaim the
       
   168     // underlying file descriptors.  Doing so, however, causes any thread
       
   169     // currently blocked in a read on one of those streams to receive an
       
   170     // IOException("Bad file number"), which is incompatible with historical
       
   171     // behavior.  By deferring the close we allow any pending reads to see -1
       
   172     // (EOF) as they did before.
       
   173     //
       
   174     private static class DeferredCloseInputStream
       
   175 	extends FileInputStream
       
   176     {
       
   177 
       
   178 	private DeferredCloseInputStream(FileDescriptor fd) {
       
   179 	    super(fd);
       
   180 	}
       
   181 
       
   182 	private Object lock = new Object();	// For the following fields
       
   183 	private boolean closePending = false;
       
   184 	private int useCount = 0;
       
   185 	private InputStream streamToClose;
       
   186 
       
   187 	private void raise() {
       
   188 	    synchronized (lock) {
       
   189 		useCount++;
       
   190 	    }
       
   191 	}
       
   192 
       
   193 	private void lower() throws IOException {
       
   194 	    synchronized (lock) {
       
   195 		useCount--;
       
   196 		if (useCount == 0 && closePending) {
       
   197 		    streamToClose.close();
       
   198 		}
       
   199 	    }
       
   200 	}
       
   201 
       
   202 	// stc is the actual stream to be closed; it might be this object, or
       
   203 	// it might be an upstream object for which this object is downstream.
       
   204 	//
       
   205 	private void closeDeferred(InputStream stc) throws IOException {
       
   206 	    synchronized (lock) {
       
   207 		if (useCount == 0) {
       
   208 		    stc.close();
       
   209 		} else {
       
   210 		    closePending = true;
       
   211 		    streamToClose = stc;
       
   212 		}
       
   213 	    }
       
   214 	}
       
   215 
       
   216 	public void close() throws IOException {
       
   217 	    synchronized (lock) {
       
   218 		useCount = 0;
       
   219 		closePending = false;
       
   220 	    }
       
   221 	    super.close();
       
   222 	}
       
   223 
       
   224 	public int read() throws IOException {
       
   225 	    raise();
       
   226 	    try {
       
   227 		return super.read();
       
   228 	    } finally {
       
   229 		lower();
       
   230 	    }
       
   231 	}
       
   232 
       
   233 	public int read(byte[] b) throws IOException {
       
   234 	    raise();
       
   235 	    try {
       
   236 		return super.read(b);
       
   237 	    } finally {
       
   238 		lower();
       
   239 	    }
       
   240 	}
       
   241 
       
   242 	public int read(byte[] b, int off, int len) throws IOException {
       
   243 	    raise();
       
   244 	    try {
       
   245 		return super.read(b, off, len);
       
   246 	    } finally {
       
   247 		lower();
       
   248 	    }
       
   249 	}
       
   250 
       
   251 	public long skip(long n) throws IOException {
       
   252 	    raise();
       
   253 	    try {
       
   254 		return super.skip(n);
       
   255 	    } finally {
       
   256 		lower();
       
   257 	    }
       
   258 	}
       
   259 
       
   260 	public int available() throws IOException {
       
   261 	    raise();
       
   262 	    try {
       
   263 		return super.available();
       
   264 	    } finally {
       
   265 		lower();
       
   266 	    }
       
   267 	}
       
   268 
       
   269     }
       
   270 
       
   271     /* This routine initializes JNI field offsets for the class */
       
   272     private static native void initIDs();
       
   273 
       
   274     static {
       
   275 	initIDs();
       
   276     }
       
   277 }