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), POSIX_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("jdk.lang.Process.launchMechanism");
89 if (s != null && !s.equals("VFORK")) {
90 if (s.equals("FORK")) {
91 launchMechanism = LaunchMechanism.FORK;
92 } else {
93 throw new Error(s + " is not a supported " +
94 "process launch mechanism on this platform.");
95 }
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 * - clone(2) and exec(2)
110 * - vfork(2) and exec(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);
|