1 /*
   2  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.BufferedInputStream;
  29 import java.io.BufferedOutputStream;
  30 import java.io.ByteArrayInputStream;
  31 import java.io.FileDescriptor;
  32 import java.io.FileInputStream;
  33 import java.io.FileOutputStream;
  34 import java.io.IOException;
  35 import java.io.InputStream;
  36 import java.io.OutputStream;
  37 import java.util.Arrays;
  38 import java.util.concurrent.Executors;
  39 import java.util.concurrent.Executor;
  40 import java.util.concurrent.ThreadFactory;
  41 import java.security.AccessController;
  42 import static java.security.AccessController.doPrivileged;
  43 import java.security.PrivilegedAction;
  44 import java.security.PrivilegedActionException;
  45 import java.security.PrivilegedExceptionAction;
  46 
  47 /**
  48  * java.lang.Process subclass in the UNIX environment.
  49  *
  50  * @author Mario Wolczko and Ross Knippel.
  51  * @author Konstantin Kladko (ported to Linux)
  52  * @author Martin Buchholz
  53  */
  54 final class UNIXProcess extends Process {
  55     private static final sun.misc.JavaIOFileDescriptorAccess fdAccess
  56         = sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess();
  57 
  58     private final int pid;
  59     private int exitcode;
  60     private boolean hasExited;
  61 
  62     private /* final */ OutputStream stdin;
  63     private /* final */ InputStream  stdout;
  64     private /* final */ InputStream  stderr;
  65 














































  66     /* this is for the reaping thread */
  67     private native int waitForProcessExit(int pid);
  68 
  69     /**
  70      * Create a process using fork(2) and exec(2).




  71      *
  72      * @param fds an array of three file descriptors.
  73      *        Indexes 0, 1, and 2 correspond to standard input,
  74      *        standard output and standard error, respectively.  On
  75      *        input, a value of -1 means to create a pipe to connect
  76      *        child and parent processes.  On output, a value which
  77      *        is not -1 is the parent pipe fd corresponding to the
  78      *        pipe which has been created.  An element of this array
  79      *        is -1 on input if and only if it is <em>not</em> -1 on
  80      *        output.
  81      * @return the pid of the subprocess
  82      */
  83     private native int forkAndExec(byte[] prog,

  84                                    byte[] argBlock, int argc,
  85                                    byte[] envBlock, int envc,
  86                                    byte[] dir,
  87                                    int[] fds,
  88                                    boolean redirectErrorStream)
  89         throws IOException;
  90 
  91     /**
  92      * The thread factory used to create "process reaper" daemon threads.
  93      */
  94     private static class ProcessReaperThreadFactory implements ThreadFactory {
  95         private final static ThreadGroup group = getRootThreadGroup();
  96 
  97         private static ThreadGroup getRootThreadGroup() {
  98             return doPrivileged(new PrivilegedAction<ThreadGroup> () {
  99                 public ThreadGroup run() {
 100                     ThreadGroup root = Thread.currentThread().getThreadGroup();
 101                     while (root.getParent() != null)
 102                         root = root.getParent();
 103                     return root;
 104                 }});
 105         }
 106 
 107         public Thread newThread(Runnable grimReaper) {
 108             // Our thread stack requirement is quite modest.
 109             Thread t = new Thread(group, grimReaper, "process reaper", 32768);
 110             t.setDaemon(true);
 111             // A small attempt (probably futile) to avoid priority inversion
 112             t.setPriority(Thread.MAX_PRIORITY);
 113             return t;
 114         }
 115     }
 116 
 117     /**
 118      * The thread pool of "process reaper" daemon threads.
 119      */
 120     private static final Executor processReaperExecutor =
 121         doPrivileged(new PrivilegedAction<Executor>() {
 122             public Executor run() {
 123                 return Executors.newCachedThreadPool
 124                     (new ProcessReaperThreadFactory());
 125             }});
 126 
 127     UNIXProcess(final byte[] prog,
 128                 final byte[] argBlock, final int argc,
 129                 final byte[] envBlock, final int envc,
 130                 final byte[] dir,
 131                 final int[] fds,
 132                 final boolean redirectErrorStream)
 133             throws IOException {
 134 
 135         pid = forkAndExec(prog,


 136                           argBlock, argc,
 137                           envBlock, envc,
 138                           dir,
 139                           fds,
 140                           redirectErrorStream);
 141 
 142         try {
 143             doPrivileged(new PrivilegedExceptionAction<Void>() {
 144                 public Void run() throws IOException {
 145                     initStreams(fds);
 146                     return null;
 147                 }});
 148         } catch (PrivilegedActionException ex) {
 149             throw (IOException) ex.getException();
 150         }
 151     }
 152 
 153     static FileDescriptor newFileDescriptor(int fd) {
 154         FileDescriptor fileDescriptor = new FileDescriptor();
 155         fdAccess.set(fileDescriptor, fd);
 156         return fileDescriptor;
 157     }
 158 
 159     void initStreams(int[] fds) throws IOException {
 160         stdin = (fds[0] == -1) ?
 161             ProcessBuilder.NullOutputStream.INSTANCE :
 162             new ProcessPipeOutputStream(fds[0]);
 163 
 164         stdout = (fds[1] == -1) ?
 165             ProcessBuilder.NullInputStream.INSTANCE :
 166             new ProcessPipeInputStream(fds[1]);
 167 
 168         stderr = (fds[2] == -1) ?
 169             ProcessBuilder.NullInputStream.INSTANCE :
 170             new ProcessPipeInputStream(fds[2]);
 171 
 172         processReaperExecutor.execute(new Runnable() {
 173             public void run() {
 174                 int exitcode = waitForProcessExit(pid);
 175                 UNIXProcess.this.processExited(exitcode);
 176             }});
 177     }
 178 
 179     void processExited(int exitcode) {
 180         synchronized (this) {
 181             this.exitcode = exitcode;
 182             hasExited = true;
 183             notifyAll();
 184         }
 185 
 186         if (stdout instanceof ProcessPipeInputStream)
 187             ((ProcessPipeInputStream) stdout).processExited();
 188 
 189         if (stderr instanceof ProcessPipeInputStream)
 190             ((ProcessPipeInputStream) stderr).processExited();
 191 
 192         if (stdin instanceof ProcessPipeOutputStream)
 193             ((ProcessPipeOutputStream) stdin).processExited();
 194     }
 195 
 196     public OutputStream getOutputStream() {
 197         return stdin;
 198     }
 199 
 200     public InputStream getInputStream() {
 201         return stdout;
 202     }
 203 
 204     public InputStream getErrorStream() {
 205         return stderr;
 206     }
 207 
 208     public synchronized int waitFor() throws InterruptedException {
 209         while (!hasExited) {
 210             wait();
 211         }
 212         return exitcode;
 213     }
 214 
 215     public synchronized int exitValue() {
 216         if (!hasExited) {
 217             throw new IllegalThreadStateException("process hasn't exited");
 218         }
 219         return exitcode;
 220     }
 221 
 222     private static native void destroyProcess(int pid);
 223     public void destroy() {
 224         // There is a risk that pid will be recycled, causing us to
 225         // kill the wrong process!  So we only terminate processes
 226         // that appear to still be running.  Even with this check,
 227         // there is an unavoidable race condition here, but the window
 228         // is very small, and OSes try hard to not recycle pids too
 229         // soon, so this is quite safe.
 230         synchronized (this) {
 231             if (!hasExited)
 232                 destroyProcess(pid);
 233         }
 234         try { stdin.close();  } catch (IOException ignored) {}
 235         try { stdout.close(); } catch (IOException ignored) {}
 236         try { stderr.close(); } catch (IOException ignored) {}
 237     }
 238 
 239     /* This routine initializes JNI field offsets for the class */
 240     private static native void initIDs();
 241 
 242     static {
 243         initIDs();
 244     }
 245 
 246     /**
 247      * A buffered input stream for a subprocess pipe file descriptor
 248      * that allows the underlying file descriptor to be reclaimed when
 249      * the process exits, via the processExited hook.
 250      *
 251      * This is tricky because we do not want the user-level InputStream to be
 252      * closed until the user invokes close(), and we need to continue to be
 253      * able to read any buffered data lingering in the OS pipe buffer.
 254      */
 255     static class ProcessPipeInputStream extends BufferedInputStream {
 256         ProcessPipeInputStream(int fd) {
 257             super(new FileInputStream(newFileDescriptor(fd)));
 258         }
 259 
 260         private static byte[] drainInputStream(InputStream in)
 261                 throws IOException {
 262             if (in == null) return null;
 263             int n = 0;
 264             int j;
 265             byte[] a = null;
 266             while ((j = in.available()) > 0) {
 267                 a = (a == null) ? new byte[j] : Arrays.copyOf(a, n + j);
 268                 n += in.read(a, n, j);
 269             }
 270             return (a == null || n == a.length) ? a : Arrays.copyOf(a, n);
 271         }
 272 
 273         /** Called by the process reaper thread when the process exits. */
 274         synchronized void processExited() {
 275             // Most BufferedInputStream methods are synchronized, but close()
 276             // is not, and so we have to handle concurrent racing close().
 277             try {
 278                 InputStream in = this.in;
 279                 if (in != null) {
 280                     byte[] stragglers = drainInputStream(in);
 281                     in.close();
 282                     this.in = (stragglers == null) ?
 283                         ProcessBuilder.NullInputStream.INSTANCE :
 284                         new ByteArrayInputStream(stragglers);
 285                     if (buf == null) // asynchronous close()?
 286                         this.in = null;
 287                 }
 288             } catch (IOException ignored) {
 289                 // probably an asynchronous close().
 290             }
 291         }
 292     }
 293 
 294     /**
 295      * A buffered output stream for a subprocess pipe file descriptor
 296      * that allows the underlying file descriptor to be reclaimed when
 297      * the process exits, via the processExited hook.
 298      */
 299     static class ProcessPipeOutputStream extends BufferedOutputStream {
 300         ProcessPipeOutputStream(int fd) {
 301             super(new FileOutputStream(newFileDescriptor(fd)));
 302         }
 303 
 304         /** Called by the process reaper thread when the process exits. */
 305         synchronized void processExited() {
 306             OutputStream out = this.out;
 307             if (out != null) {
 308                 try {
 309                     out.close();
 310                 } catch (IOException ignored) {
 311                     // We know of no reason to get an IOException, but if
 312                     // we do, there's nothing else to do but carry on.
 313                 }
 314                 this.out = ProcessBuilder.NullOutputStream.INSTANCE;
 315             }
 316         }
 317     }
 318 }
--- EOF ---