src/windows/classes/java/lang/ProcessImpl.java

Print this page


   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.IOException;
  29 import java.io.File;
  30 import java.io.InputStream;
  31 import java.io.OutputStream;
  32 import java.io.FileInputStream;
  33 import java.io.FileOutputStream;
  34 import java.io.FileDescriptor;
  35 import java.io.BufferedInputStream;
  36 import java.io.BufferedOutputStream;
  37 import java.lang.ProcessBuilder.Redirect;
  38 import java.security.AccessController;
  39 import java.security.PrivilegedAction;

  40 
  41 /* This class is for the exclusive use of ProcessBuilder.start() to
  42  * create new processes.
  43  *
  44  * @author Martin Buchholz
  45  * @since   1.5
  46  */
  47 
  48 final class ProcessImpl extends Process {
  49     private static final sun.misc.JavaIOFileDescriptorAccess fdAccess
  50         = sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess();
  51 
  52     /**
  53      * Open a file for writing. If {@code append} is {@code true} then the file
  54      * is opened for atomic append directly and a FileOutputStream constructed
  55      * with the resulting handle. This is because a FileOutputStream created
  56      * to append to a file does not open the file in a manner that guarantees
  57      * that writes by the child process will be atomic.
  58      */
  59     private static FileOutputStream newFileOutputStream(File f, boolean append)


 238     }
 239 
 240     private static final int STILL_ACTIVE = getStillActive();
 241     private static native int getStillActive();
 242 
 243     public int exitValue() {
 244         int exitCode = getExitCodeProcess(handle);
 245         if (exitCode == STILL_ACTIVE)
 246             throw new IllegalThreadStateException("process has not exited");
 247         return exitCode;
 248     }
 249     private static native int getExitCodeProcess(long handle);
 250 
 251     public int waitFor() throws InterruptedException {
 252         waitForInterruptibly(handle);
 253         if (Thread.interrupted())
 254             throw new InterruptedException();
 255         return exitValue();
 256     }
 257     private static native void waitForInterruptibly(long handle);





 258 








 259     public void destroy() { terminateProcess(handle); }





 260     private static native void terminateProcess(long handle);
 261 






 262     /**
 263      * Create a process using the win32 function CreateProcess.
 264      *
 265      * @param cmdstr the Windows commandline
 266      * @param envblock NUL-separated, double-NUL-terminated list of
 267      *        environment strings in VAR=VALUE form
 268      * @param dir the working directory of the process, or null if
 269      *        inheriting the current directory from the parent process
 270      * @param stdHandles array of windows HANDLEs.  Indexes 0, 1, and
 271      *        2 correspond to standard input, standard output and
 272      *        standard error, respectively.  On input, a value of -1
 273      *        means to create a pipe to connect child and parent
 274      *        processes.  On output, a value which is not -1 is the
 275      *        parent pipe handle corresponding to the pipe which has
 276      *        been created.  An element of this array is -1 on input
 277      *        if and only if it is <em>not</em> -1 on output.
 278      * @param redirectErrorStream redirectErrorStream attribute
 279      * @return the native subprocess HANDLE returned by CreateProcess
 280      */
 281     private static native long create(String cmdstr,
 282                                       String envblock,
 283                                       String dir,
 284                                       long[] stdHandles,
 285                                       boolean redirectErrorStream)
 286         throws IOException;
 287 
 288     /**
 289      * Opens a file for atomic append. The file is created if it doesn't
 290      * already exist.
 291      *
 292      * @param file the file to open or create
 293      * @return the native HANDLE
 294      */
 295     private static native long openForAtomicAppend(String path)
 296         throws IOException;
 297 
 298     private static native boolean closeHandle(long handle);
 299 }

   1 /*
   2  * Copyright (c) 1995, 2012, 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.IOException;
  29 import java.io.File;
  30 import java.io.InputStream;
  31 import java.io.OutputStream;
  32 import java.io.FileInputStream;
  33 import java.io.FileOutputStream;
  34 import java.io.FileDescriptor;
  35 import java.io.BufferedInputStream;
  36 import java.io.BufferedOutputStream;
  37 import java.lang.ProcessBuilder.Redirect;
  38 import java.security.AccessController;
  39 import java.security.PrivilegedAction;
  40 import java.util.concurrent.TimeUnit;
  41 
  42 /* This class is for the exclusive use of ProcessBuilder.start() to
  43  * create new processes.
  44  *
  45  * @author Martin Buchholz
  46  * @since   1.5
  47  */
  48 
  49 final class ProcessImpl extends Process {
  50     private static final sun.misc.JavaIOFileDescriptorAccess fdAccess
  51         = sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess();
  52 
  53     /**
  54      * Open a file for writing. If {@code append} is {@code true} then the file
  55      * is opened for atomic append directly and a FileOutputStream constructed
  56      * with the resulting handle. This is because a FileOutputStream created
  57      * to append to a file does not open the file in a manner that guarantees
  58      * that writes by the child process will be atomic.
  59      */
  60     private static FileOutputStream newFileOutputStream(File f, boolean append)


 239     }
 240 
 241     private static final int STILL_ACTIVE = getStillActive();
 242     private static native int getStillActive();
 243 
 244     public int exitValue() {
 245         int exitCode = getExitCodeProcess(handle);
 246         if (exitCode == STILL_ACTIVE)
 247             throw new IllegalThreadStateException("process has not exited");
 248         return exitCode;
 249     }
 250     private static native int getExitCodeProcess(long handle);
 251 
 252     public int waitFor() throws InterruptedException {
 253         waitForInterruptibly(handle);
 254         if (Thread.interrupted())
 255             throw new InterruptedException();
 256         return exitValue();
 257     }
 258     private static native void waitForInterruptibly(long handle);
 259     @Override
 260     public boolean waitFor(long timeout, TimeUnit unit) 
 261         throws InterruptedException {
 262         if (getExitCodeProcess(handle) != STILL_ACTIVE) return true;
 263         if (timeout <= 0) return false;
 264         
 265         waitForTimeoutInterruptibly(handle, unit.toMillis(timeout));
 266         if (Thread.interrupted())
 267             throw new InterruptedException();
 268         return (getExitCodeProcess(handle) != STILL_ACTIVE);
 269     }
 270     private static native void waitForTimeoutInterruptibly(
 271         long handle, long timeout);
 272 
 273     public void destroy() { terminateProcess(handle); }
 274     @Override
 275     public Process destroyForcibly() { 
 276         destroy();
 277         return this;
 278     }
 279     private static native void terminateProcess(long handle);
 280 
 281     @Override
 282     public boolean isAlive() {
 283         return isProcessAlive(handle);
 284     }
 285     private static native boolean isProcessAlive(long handle);
 286 
 287     /**
 288      * Create a process using the win32 function CreateProcess.
 289      *
 290      * @param cmdstr the Windows commandline
 291      * @param envblock NUL-separated, double-NUL-terminated list of
 292      *        environment strings in VAR=VALUE form
 293      * @param dir the working directory of the process, or null if
 294      *        inheriting the current directory from the parent process
 295      * @param stdHandles array of windows HANDLEs.  Indexes 0, 1, and
 296      *        2 correspond to standard input, standard output and
 297      *        standard error, respectively.  On input, a value of -1
 298      *        means to create a pipe to connect child and parent
 299      *        processes.  On output, a value which is not -1 is the
 300      *        parent pipe handle corresponding to the pipe which has
 301      *        been created.  An element of this array is -1 on input
 302      *        if and only if it is <em>not</em> -1 on output.
 303      * @param redirectErrorStream redirectErrorStream attribute
 304      * @return the native subprocess HANDLE returned by CreateProcess
 305      */
 306     private static native long create(String cmdstr,
 307                                       String envblock,
 308                                       String dir,
 309                                       long[] stdHandles,
 310                                       boolean redirectErrorStream)
 311         throws IOException;
 312 
 313     /**
 314      * Opens a file for atomic append. The file is created if it doesn't
 315      * already exist.
 316      *
 317      * @param file the file to open or create
 318      * @return the native HANDLE
 319      */
 320     private static native long openForAtomicAppend(String path)
 321         throws IOException;
 322 
 323     private static native boolean closeHandle(long handle);
 324 }
 325