src/solaris/classes/java/lang/UNIXProcess.java.solaris

Print this page




   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.*;


  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 static final sun.misc.JavaIOFileDescriptorAccess fdAccess
  37         = sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess();
  38 
  39     private final int pid;
  40     private int exitcode;
  41     private boolean hasExited;
  42 
  43     private OutputStream stdin_stream;
  44     private InputStream stdout_stream;
  45     private DeferredCloseInputStream stdout_inner_stream;
  46     private InputStream stderr_stream;
  47 



















































  48     /* this is for the reaping thread */
  49     private native int waitForProcessExit(int pid);
  50 
  51     /**
  52      * Create a process using fork(2) and exec(2).



  53      *
  54      * @param std_fds array of file descriptors.  Indexes 0, 1, and
  55      *        2 correspond to standard input, standard output and
  56      *        standard error, respectively.  On input, a value of -1
  57      *        means to create a pipe to connect child and parent
  58      *        processes.  On output, a value which is not -1 is the
  59      *        parent pipe fd corresponding to the pipe which has
  60      *        been created.  An element of this array is -1 on input
  61      *        if and only if it is <em>not</em> -1 on output.
  62      * @return the pid of the subprocess
  63      */
  64     private native int forkAndExec(byte[] prog,

  65                                    byte[] argBlock, int argc,
  66                                    byte[] envBlock, int envc,
  67                                    byte[] dir,
  68                                    int[] std_fds,
  69                                    boolean redirectErrorStream)
  70         throws IOException;
  71 
  72     UNIXProcess(final byte[] prog,
  73                 final byte[] argBlock, int argc,
  74                 final byte[] envBlock, int envc,
  75                 final byte[] dir,
  76                 final int[] std_fds,
  77                 final boolean redirectErrorStream)
  78     throws IOException {
  79         pid = forkAndExec(prog,


  80                           argBlock, argc,
  81                           envBlock, envc,
  82                           dir,
  83                           std_fds,
  84                           redirectErrorStream);
  85 
  86         java.security.AccessController.doPrivileged(
  87         new java.security.PrivilegedAction<Void>() { public Void run() {
  88             if (std_fds[0] == -1)
  89                 stdin_stream = ProcessBuilder.NullOutputStream.INSTANCE;
  90             else {
  91                 FileDescriptor stdin_fd = new FileDescriptor();
  92                 fdAccess.set(stdin_fd, std_fds[0]);
  93                 stdin_stream = new BufferedOutputStream(
  94                     new FileOutputStream(stdin_fd));
  95             }
  96 
  97             if (std_fds[1] == -1)
  98                 stdout_stream = ProcessBuilder.NullInputStream.INSTANCE;
  99             else {


 277         public long skip(long n) throws IOException {
 278             raise();
 279             try {
 280                 return super.skip(n);
 281             } finally {
 282                 lower();
 283             }
 284         }
 285 
 286         public int available() throws IOException {
 287             raise();
 288             try {
 289                 return super.available();
 290             } finally {
 291                 lower();
 292             }
 293         }
 294 
 295     }
 296 
 297     /* This routine initializes JNI field offsets for the class */
 298     private static native void initIDs();
 299 
 300     static {
 301         initIDs();
 302     }
 303 }


   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.*;
  29 import java.security.AccessController;
  30 import java.security.PrivilegedAction;
  31 
  32 /* java.lang.Process subclass in the UNIX environment.
  33  *
  34  * @author Mario Wolczko and Ross Knippel.
  35  */
  36 
  37 final class UNIXProcess extends Process {
  38     private static final sun.misc.JavaIOFileDescriptorAccess fdAccess
  39         = sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess();
  40 
  41     private final int pid;
  42     private int exitcode;
  43     private boolean hasExited;
  44 
  45     private OutputStream stdin_stream;
  46     private InputStream stdout_stream;
  47     private DeferredCloseInputStream stdout_inner_stream;
  48     private InputStream stderr_stream;
  49 
  50     private static enum LaunchMechanism {
  51         FORK(1),
  52         POSIX_SPAWN(2);
  53 
  54         private int value;
  55         LaunchMechanism(int x) {value = x;}
  56     };
  57 
  58     /* On Solaris, the default is to spawn */
  59     private static final LaunchMechanism launchMechanism;
  60     private static byte[] helperpath;
  61 
  62     private static byte[] toCString(String s) {
  63         if (s == null)
  64             return null;
  65         byte[] bytes = s.getBytes();
  66         byte[] result = new byte[bytes.length + 1];
  67         System.arraycopy(bytes, 0,
  68                          result, 0,
  69                          bytes.length);
  70         result[result.length-1] = (byte)0;
  71         return result;
  72     }
  73 
  74     static {
  75         launchMechanism = AccessController.doPrivileged(
  76                 new PrivilegedAction<LaunchMechanism>()
  77         {
  78             public LaunchMechanism run() {
  79                 String javahome = System.getProperty("java.home");
  80                 String osArch = System.getProperty("os.arch");
  81                 if (osArch.equals("x86")) {
  82                     osArch = "i386";
  83                 } else if (osArch.equals("x86_64")) {
  84                     osArch = "amd64";
  85                 }
  86 
  87                 helperpath = toCString(javahome + "/lib/" + osArch + "/jspawnhelper");
  88                 String s = System.getProperty(
  89                     "jdk.lang.Process.launchMechanism", "fork");
  90 
  91                 try {
  92                     return LaunchMechanism.valueOf(s.toUpperCase());
  93                 } catch (IllegalArgumentException e) {
  94                     throw new Error(s + " is not a supported " +
  95                         "process launch mechanism on this platform.");
  96                 }
  97             }
  98         });
  99     }
 100 
 101     /* this is for the reaping thread */
 102     private native int waitForProcessExit(int pid);
 103 
 104     /**
 105      * Create a process. Depending on the mode flag, this is done by
 106      * one of the following mechanisms.
 107      * - fork(2) and exec(2)
 108      * - posix_spawn(2)
 109      *
 110      * @param std_fds array of file descriptors.  Indexes 0, 1, and
 111      *        2 correspond to standard input, standard output and
 112      *        standard error, respectively.  On input, a value of -1
 113      *        means to create a pipe to connect child and parent
 114      *        processes.  On output, a value which is not -1 is the
 115      *        parent pipe fd corresponding to the pipe which has
 116      *        been created.  An element of this array is -1 on input
 117      *        if and only if it is <em>not</em> -1 on output.
 118      * @return the pid of the subprocess
 119      */
 120     private native int forkAndExec(int mode, byte[] helperpath,
 121                                    byte[] prog,
 122                                    byte[] argBlock, int argc,
 123                                    byte[] envBlock, int envc,
 124                                    byte[] dir,
 125                                    int[] std_fds,
 126                                    boolean redirectErrorStream)
 127         throws IOException;
 128 
 129     UNIXProcess(final byte[] prog,
 130                 final byte[] argBlock, int argc,
 131                 final byte[] envBlock, int envc,
 132                 final byte[] dir,
 133                 final int[] std_fds,
 134                 final boolean redirectErrorStream)
 135     throws IOException {
 136         pid = forkAndExec(launchMechanism.value,
 137                           helperpath,
 138                           prog,
 139                           argBlock, argc,
 140                           envBlock, envc,
 141                           dir,
 142                           std_fds,
 143                           redirectErrorStream);
 144 
 145         java.security.AccessController.doPrivileged(
 146         new java.security.PrivilegedAction<Void>() { public Void run() {
 147             if (std_fds[0] == -1)
 148                 stdin_stream = ProcessBuilder.NullOutputStream.INSTANCE;
 149             else {
 150                 FileDescriptor stdin_fd = new FileDescriptor();
 151                 fdAccess.set(stdin_fd, std_fds[0]);
 152                 stdin_stream = new BufferedOutputStream(
 153                     new FileOutputStream(stdin_fd));
 154             }
 155 
 156             if (std_fds[1] == -1)
 157                 stdout_stream = ProcessBuilder.NullInputStream.INSTANCE;
 158             else {


 336         public long skip(long n) throws IOException {
 337             raise();
 338             try {
 339                 return super.skip(n);
 340             } finally {
 341                 lower();
 342             }
 343         }
 344 
 345         public int available() throws IOException {
 346             raise();
 347             try {
 348                 return super.available();
 349             } finally {
 350                 lower();
 351             }
 352         }
 353 
 354     }
 355 
 356     private static native void init();

 357 
 358     static {
 359         init();
 360     }
 361 }