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

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 Linux)
  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 Linux)
  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 helperpath;
  69 
  70     enum LaunchMechanism {
  71         CLONE(1), FORK(2),
  72         VFORK(3), SPAWN(4);
  73 
  74         private int value;
  75         LaunchMechanism(int x) {value = x;}
  76     };
  77 
  78     /* default is VFORK on Linux */
  79     private static LaunchMechanism launchMechanism = LaunchMechanism.VFORK;
  80 
  81     static {
  82         AccessController.doPrivileged(new PrivilegedAction<Void>() {
  83             public Void run() {
  84                 String javahome = System.getProperty("java.home");
  85                 String osarch = System.getProperty("os.arch");
  86 
  87                 helperpath = javahome + "/lib/" + osarch + "/jspawnhelper";
  88                 String s = System.getProperty("java.lang.useFork");
  89                 if (s != null) {
  90                     launchMechanism = LaunchMechanism.FORK;
  91                 }
  92                 return null;
  93             }
  94         });
  95     }
  96 
  97     /* this is for the reaping thread */
  98     private native int waitForProcessExit(int pid);
  99 
 100     /**
 101      * Create a process. Depending on the mode flag, this is done by
 102      * one of the following mechanisms.
 103      * - fork(2) and exec(2)
 104      * - clone(2) and exec(2)
 105      * - vfork(2) and exec(2)
 106      *
 107      * @param fds an array of three file descriptors.
 108      *        Indexes 0, 1, and 2 correspond to standard input,
 109      *        standard output and standard error, respectively.  On
 110      *        input, a value of -1 means to create a pipe to connect
 111      *        child and parent processes.  On output, a value which
 112      *        is not -1 is the parent pipe fd corresponding to the
 113      *        pipe which has been created.  An element of this array
 114      *        is -1 on input if and only if it is <em>not</em> -1 on
 115      *        output.
 116      * @return the pid of the subprocess
 117      */
 118     private native int forkAndExec(int mode, byte[] prog,
 119                                    byte[] argBlock, int argc,
 120                                    byte[] envBlock, int envc,
 121                                    byte[] dir,
 122                                    int[] fds,
 123                                    boolean redirectErrorStream)
 124         throws IOException;
 125 
 126     /**
 127      * The thread factory used to create "process reaper" daemon threads.
 128      */
 129     private static class ProcessReaperThreadFactory implements ThreadFactory {
 130         private final static ThreadGroup group = getRootThreadGroup();
 131 
 132         private static ThreadGroup getRootThreadGroup() {
 133             return doPrivileged(new PrivilegedAction<ThreadGroup> () {
 134                 public ThreadGroup run() {
 135                     ThreadGroup root = Thread.currentThread().getThreadGroup();
 136                     while (root.getParent() != null)
 137                         root = root.getParent();
 138                     return root;


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