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

Print this page




  47 
  48 /**
  49  * java.lang.Process subclass in the UNIX environment.
  50  *
  51  * @author Mario Wolczko and Ross Knippel.
  52  * @author Konstantin Kladko (ported to Bsd)
  53  * @author Martin Buchholz
  54  */
  55 final class UNIXProcess extends Process {
  56     private static final sun.misc.JavaIOFileDescriptorAccess fdAccess
  57         = sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess();
  58 
  59     private final int pid;
  60     private int exitcode;
  61     private boolean hasExited;
  62 
  63     private /* final */ OutputStream stdin;
  64     private /* final */ InputStream  stdout;
  65     private /* final */ InputStream  stderr;
  66 



































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




  72      *
  73      * @param fds an array of three file descriptors.
  74      *        Indexes 0, 1, and 2 correspond to standard input,
  75      *        standard output and standard error, respectively.  On
  76      *        input, a value of -1 means to create a pipe to connect
  77      *        child and parent processes.  On output, a value which
  78      *        is not -1 is the parent pipe fd corresponding to the
  79      *        pipe which has been created.  An element of this array
  80      *        is -1 on input if and only if it is <em>not</em> -1 on
  81      *        output.
  82      * @return the pid of the subprocess
  83      */
  84     private native int forkAndExec(byte[] prog,
  85                                    byte[] argBlock, int argc,
  86                                    byte[] envBlock, int envc,
  87                                    byte[] dir,
  88                                    int[] fds,
  89                                    boolean redirectErrorStream)
  90         throws IOException;
  91 
  92     /**
  93      * The thread factory used to create "process reaper" daemon threads.
  94      */
  95     private static class ProcessReaperThreadFactory implements ThreadFactory {
  96         private final static ThreadGroup group = getRootThreadGroup();
  97 
  98         private static ThreadGroup getRootThreadGroup() {
  99             return doPrivileged(new PrivilegedAction<ThreadGroup> () {
 100                 public ThreadGroup run() {
 101                     ThreadGroup root = Thread.currentThread().getThreadGroup();
 102                     while (root.getParent() != null)
 103                         root = root.getParent();
 104                     return root;


 116     }
 117 
 118     /**
 119      * The thread pool of "process reaper" daemon threads.
 120      */
 121     private static final Executor processReaperExecutor =
 122         doPrivileged(new PrivilegedAction<Executor>() {
 123             public Executor run() {
 124                 return Executors.newCachedThreadPool
 125                     (new ProcessReaperThreadFactory());
 126             }});
 127 
 128     UNIXProcess(final byte[] prog,
 129                 final byte[] argBlock, final int argc,
 130                 final byte[] envBlock, final int envc,
 131                 final byte[] dir,
 132                 final int[] fds,
 133                 final boolean redirectErrorStream)
 134             throws IOException {
 135 
 136         pid = forkAndExec(prog,

 137                           argBlock, argc,
 138                           envBlock, envc,
 139                           dir,
 140                           fds,
 141                           redirectErrorStream);
 142 
 143         try {
 144             doPrivileged(new PrivilegedExceptionAction<Void>() {
 145                 public Void run() throws IOException {
 146                     initStreams(fds);
 147                     return null;
 148                 }});
 149         } catch (PrivilegedActionException ex) {
 150             throw (IOException) ex.getException();
 151         }
 152     }
 153 
 154     static FileDescriptor newFileDescriptor(int fd) {
 155         FileDescriptor fileDescriptor = new FileDescriptor();
 156         fdAccess.set(fileDescriptor, fd);




  47 
  48 /**
  49  * java.lang.Process subclass in the UNIX environment.
  50  *
  51  * @author Mario Wolczko and Ross Knippel.
  52  * @author Konstantin Kladko (ported to Bsd)
  53  * @author Martin Buchholz
  54  */
  55 final class UNIXProcess extends Process {
  56     private static final sun.misc.JavaIOFileDescriptorAccess fdAccess
  57         = sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess();
  58 
  59     private final int pid;
  60     private int exitcode;
  61     private boolean hasExited;
  62 
  63     private /* final */ OutputStream stdin;
  64     private /* final */ InputStream  stdout;
  65     private /* final */ InputStream  stderr;
  66 
  67     private static String javahome;
  68     private static String arch;
  69     private static String helperpath;
  70 
  71     enum LaunchMechanism {
  72         fork(1),
  73         posix_spawn(2);
  74 
  75         private int value;
  76         LaunchMechanism(int x) {value = x;}
  77     };
  78 
  79     /* On BSD, the default is to spawn */
  80     private static LaunchMechanism launchMechanism;
  81 
  82     static {
  83         AccessController.doPrivileged(new PrivilegedAction<Void>() {
  84             public Void run() {
  85                 String javahome = System.getProperty("java.home");
  86 
  87                 helperpath = javahome + "/lib/jspawnhelper";
  88                 String s = System.getProperty(
  89                     "jdk.lang.Process.launchMechanism", "posix_spawn");
  90 
  91                 try {
  92                     launchMechanism = LaunchMechanism.valueOf(s);
  93                 } catch (IllegalArgumentException e) {
  94                     throw new Error(s + " is not a supported " +
  95                         "process launch mechanism on this platform.");
  96                 }
  97                 return null;
  98             }
  99         });
 100     }
 101 
 102     /* this is for the reaping thread */
 103     private native int waitForProcessExit(int pid);
 104 
 105     /**
 106      * Create a process. Depending on the mode flag, this is done by
 107      * one of the following mechanisms.
 108      * - fork(2) and exec(2)
 109      * - vfork(2) and exec(2)
 110      * - posix_spawn(2)
 111      *
 112      * @param fds an array of three file descriptors.
 113      *        Indexes 0, 1, and 2 correspond to standard input,
 114      *        standard output and standard error, respectively.  On
 115      *        input, a value of -1 means to create a pipe to connect
 116      *        child and parent processes.  On output, a value which
 117      *        is not -1 is the parent pipe fd corresponding to the
 118      *        pipe which has been created.  An element of this array
 119      *        is -1 on input if and only if it is <em>not</em> -1 on
 120      *        output.
 121      * @return the pid of the subprocess
 122      */
 123     private native int forkAndExec(int mode, byte[] prog,
 124                                    byte[] argBlock, int argc,
 125                                    byte[] envBlock, int envc,
 126                                    byte[] dir,
 127                                    int[] fds,
 128                                    boolean redirectErrorStream)
 129         throws IOException;
 130 
 131     /**
 132      * The thread factory used to create "process reaper" daemon threads.
 133      */
 134     private static class ProcessReaperThreadFactory implements ThreadFactory {
 135         private final static ThreadGroup group = getRootThreadGroup();
 136 
 137         private static ThreadGroup getRootThreadGroup() {
 138             return doPrivileged(new PrivilegedAction<ThreadGroup> () {
 139                 public ThreadGroup run() {
 140                     ThreadGroup root = Thread.currentThread().getThreadGroup();
 141                     while (root.getParent() != null)
 142                         root = root.getParent();
 143                     return root;


 155     }
 156 
 157     /**
 158      * The thread pool of "process reaper" daemon threads.
 159      */
 160     private static final Executor processReaperExecutor =
 161         doPrivileged(new PrivilegedAction<Executor>() {
 162             public Executor run() {
 163                 return Executors.newCachedThreadPool
 164                     (new ProcessReaperThreadFactory());
 165             }});
 166 
 167     UNIXProcess(final byte[] prog,
 168                 final byte[] argBlock, final int argc,
 169                 final byte[] envBlock, final int envc,
 170                 final byte[] dir,
 171                 final int[] fds,
 172                 final boolean redirectErrorStream)
 173             throws IOException {
 174 
 175         pid = forkAndExec(launchMechanism.value,
 176                           prog,
 177                           argBlock, argc,
 178                           envBlock, envc,
 179                           dir,
 180                           fds,
 181                           redirectErrorStream);
 182 
 183         try {
 184             doPrivileged(new PrivilegedExceptionAction<Void>() {
 185                 public Void run() throws IOException {
 186                     initStreams(fds);
 187                     return null;
 188                 }});
 189         } catch (PrivilegedActionException ex) {
 190             throw (IOException) ex.getException();
 191         }
 192     }
 193 
 194     static FileDescriptor newFileDescriptor(int fd) {
 195         FileDescriptor fileDescriptor = new FileDescriptor();
 196         fdAccess.set(fileDescriptor, fd);