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, boolean force);
 223     public void destroy(boolean force) {
 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, force);
 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     public void destroy() {
 240         destroy(false);
 241     }
 242 
 243     public Process destroyForcibly() {
 244         destroy(true);
 245         return this;
 246     }
 247 
 248     @Override
 249     public boolean isAlive() {
 250         synchronized (this) {
 251             return !hasExited;
 252         }
 253     }
 254 
 255     /* This routine initializes JNI field offsets for the class */
 256     private static native void initIDs();
 257 
 258     static {
 259         initIDs();
 260     }
 261 
 262     /**
 263      * A buffered input stream for a subprocess pipe file descriptor
 264      * that allows the underlying file descriptor to be reclaimed when
 265      * the process exits, via the processExited hook.
 266      *
 267      * This is tricky because we do not want the user-level InputStream to be
 268      * closed until the user invokes close(), and we need to continue to be
 269      * able to read any buffered data lingering in the OS pipe buffer.
 270      */
 271     static class ProcessPipeInputStream extends BufferedInputStream {
 272         ProcessPipeInputStream(int fd) {
 273             super(new FileInputStream(newFileDescriptor(fd)));
 274         }
 275 
 276         private static byte[] drainInputStream(InputStream in)
 277                 throws IOException {
 278             if (in == null) return null;
 279             int n = 0;
 280             int j;
 281             byte[] a = null;
 282             while ((j = in.available()) > 0) {
 283                 a = (a == null) ? new byte[j] : Arrays.copyOf(a, n + j);
 284                 n += in.read(a, n, j);
 285             }
 286             return (a == null || n == a.length) ? a : Arrays.copyOf(a, n);
 287         }
 288 
 289         /** Called by the process reaper thread when the process exits. */
 290         synchronized void processExited() {
 291             // Most BufferedInputStream methods are synchronized, but close()
 292             // is not, and so we have to handle concurrent racing close().
 293             try {
 294                 InputStream in = this.in;
 295                 if (in != null) {
 296                     byte[] stragglers = drainInputStream(in);
 297                     in.close();
 298                     this.in = (stragglers == null) ?
 299                         ProcessBuilder.NullInputStream.INSTANCE :
 300                         new ByteArrayInputStream(stragglers);
 301                     if (buf == null) // asynchronous close()?
 302                         this.in = null;
 303                 }
 304             } catch (IOException ignored) {
 305                 // probably an asynchronous close().
 306             }
 307         }
 308     }
 309 
 310     /**
 311      * A buffered output stream for a subprocess pipe file descriptor
 312      * that allows the underlying file descriptor to be reclaimed when
 313      * the process exits, via the processExited hook.
 314      */
 315     static class ProcessPipeOutputStream extends BufferedOutputStream {
 316         ProcessPipeOutputStream(int fd) {
 317             super(new FileOutputStream(newFileDescriptor(fd)));
 318         }
 319 
 320         /** Called by the process reaper thread when the process exits. */
 321         synchronized void processExited() {
 322             OutputStream out = this.out;
 323             if (out != null) {
 324                 try {
 325                     out.close();
 326                 } catch (IOException ignored) {
 327                     // We know of no reason to get an IOException, but if
 328                     // we do, there's nothing else to do but carry on.
 329                 }
 330                 this.out = ProcessBuilder.NullOutputStream.INSTANCE;
 331             }
 332         }
 333     }
 334 }
--- EOF ---