summaryrefslogtreecommitdiff
path: root/libjava/classpath/vm/reference/java/lang/VMProcess.java
blob: c41d7cc0c088529ceaa65c84c34f21e6d8da5da9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* java.lang.VMProcess -- VM implementation of java.lang.Process
   Copyright (C) 2004, 2005 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package java.lang;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Represents one external process. Each instance of this class is in
 * one of three states: INITIAL, RUNNING, or TERMINATED. The instance
 * is {@link Object#notifyAll notifyAll()}'d each time the state changes.
 * The state of all instances is managed by a single dedicated thread
 * which does the actual fork()/exec() and wait() system calls. User
 * threads {@link Object#wait()} on the instance when creating the
 * process or waiting for it to terminate.
 *
 * <p>
 * See
 * <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11801">GCC bug
 * #11801</a> for the motivation behind the design of this class.
 *
 * @author Archie Cobbs
 * @see Process
 * @see Runtime#exec(String)
 */
final class VMProcess extends Process
{

  // Possible states for a VMProcess
  private static final int INITIAL = 0;
  private static final int RUNNING = 1;
  private static final int TERMINATED = 2;

  // Dedicated thread that does all the fork()'ing and wait()'ing.
  static Thread processThread;

  // New processes waiting to be spawned by processThread.
  static final LinkedList workList = new LinkedList();

  // Return values set by nativeReap() when a child is reaped.
  // These are only accessed by processThread so no locking required.
  static long reapedPid;
  static int reapedExitValue;

  // Information about this process
  int state;                                   // current state of process
  final String[] cmd;                          // copied from Runtime.exec()
  final String[] env;                          // copied from Runtime.exec()
  final File dir;                              // copied from Runtime.exec()
  Throwable exception;                         // if process failed to start
  long pid;                                    // process id
  OutputStream stdin;                          // process input stream
  InputStream stdout;                          // process output stream
  InputStream stderr;                          // process error stream
  int exitValue;                               // process exit value
  boolean redirect;                            // redirect stderr -> stdout

  //
  // Dedicated thread that does all the fork()'ing and wait()'ing
  // for external processes. This is needed because some systems like
  // Linux use a process-per-thread model, which means the same thread
  // that did the fork()/exec() must also do the wait().
  //
  private static class ProcessThread extends Thread
  {

    // Max time (in ms) we'll delay before trying to reap another child.
    private static final int MAX_REAP_DELAY = 1000;

    // Processes created but not yet terminated; maps Long(pid) -> VMProcess
    // Only used in run() and spawn() method from this Thread, so no locking.
    private final HashMap activeMap = new HashMap();

    // We have an explicit constructor, because the default
    // constructor will be private, which means the compiler will have
    // to generate a second package-private constructor, which is
    // bogus.
    ProcessThread ()
    {
    }

    public void run()
    {
      final LinkedList workList = VMProcess.workList;
      while (true)
        {

          // Get the next process to spawn (if any) and spawn it. Spawn
          // at most one at a time before checking for reapable children.
          VMProcess process = null;
          synchronized (workList)
            {
              if (!workList.isEmpty())
                process = (VMProcess)workList.removeFirst();
            }

          if (process != null)
            spawn(process);


          // Check for termination of active child processes
          while (!activeMap.isEmpty() && VMProcess.nativeReap())
            {
              long pid = VMProcess.reapedPid;
              int exitValue = VMProcess.reapedExitValue;
              process = (VMProcess)activeMap.remove(new Long(pid));
              if (process != null)
                {
                  synchronized (process)
                    {
                      process.exitValue = exitValue;
                      process.state = TERMINATED;
                      process.notify();
                    }
                }
              else
                System.err.println("VMProcess WARNING reaped unknown process: "
                                   + pid);
            }


          // If there are more new processes to create, go do that now.
          // If there is nothing left to do, exit this thread. Otherwise,
          // sleep a little while, and then check again for reapable children.
          // We will get woken up immediately if there are new processes to
          // spawn, but not if there are new children to reap. So we only
          // sleep a short time, in effect polling while processes are active.
          synchronized (workList)
            {
              if (!workList.isEmpty())
                continue;
              if (activeMap.isEmpty())
                {
                  processThread = null;
                  break;
                }

              try
                {
                  workList.wait(MAX_REAP_DELAY);
                }
              catch (InterruptedException e)
                {
                  /* ignore */
                }
            }
        }
    }

    // Spawn a process
    private void spawn(VMProcess process)
    {

      // Spawn the process and put it in our active map indexed by pid.
      // If the spawn operation fails, store the exception with the process.
      // In either case, wake up thread that created the process.
      synchronized (process)
        {
          try
            {
              process.nativeSpawn(process.cmd, process.env, process.dir,
                                  process.redirect);
              process.state = RUNNING;
              activeMap.put(new Long(process.pid), process);
            }
          catch (ThreadDeath death)
            {
              throw death;
            }
          catch (Throwable t)
            {
              process.state = TERMINATED;
              process.exception = t;
            }
          process.notify();
        }
    }
  }

  // Constructor
  private VMProcess(String[] cmd, String[] env, File dir, boolean redirect)
    throws IOException
  {

    // Initialize this process
    this.state = INITIAL;
    this.cmd = cmd;
    this.env = env;
    this.dir = dir;
    this.redirect = redirect;

    // Add process to the new process work list and wakeup processThread
    synchronized (workList)
      {
        workList.add(this);
        if (processThread == null)
          {
            processThread = new ProcessThread();
            processThread.setDaemon(true);
            processThread.start();
          }
        else
          {
            workList.notify();
          }
      }

    // Wait for processThread to spawn this process and update its state
    synchronized (this)
      {
        while (state == INITIAL)
          {
            try
              {
                wait();
              }
            catch (InterruptedException e)
              {
                /* ignore */
              }
          }
      }

    // If spawning failed, rethrow the exception in this thread
    if (exception != null)
      {
        exception.fillInStackTrace();
        if (exception instanceof IOException)
          throw (IOException)exception;

        if (exception instanceof Error)
          throw (Error)exception;

        if (exception instanceof RuntimeException)
          throw (RuntimeException)exception;

        throw new RuntimeException(exception);
      }
  }

  // Invoked by native code (from nativeSpawn()) to record process info.
  private void setProcessInfo(OutputStream stdin,
                              InputStream stdout, InputStream stderr, long pid)
  {
    this.stdin = stdin;
    this.stdout = stdout;
    if (stderr == null)
      this.stderr = new InputStream()
        {
          public int read() throws IOException
          {
            return -1;
          }
        };
    else
      this.stderr = stderr;
    this.pid = pid;
  }

  /**
   * Entry point from Runtime.exec().
   */
  static Process exec(String[] cmd, String[] env, File dir) throws IOException
  {
    return new VMProcess(cmd, env, dir, false);
  }

  static Process exec(List cmd, Map env,
                      File dir, boolean redirect) throws IOException
  {
    String[] acmd = (String[]) cmd.toArray(new String[cmd.size()]);
    String[] aenv = new String[env.size()];

    int i = 0;
    Iterator iter = env.entrySet().iterator();
    while (iter.hasNext())
      {
        Map.Entry entry = (Map.Entry) iter.next();
        aenv[i++] = entry.getKey() + "=" + entry.getValue();
      }

    return new VMProcess(acmd, aenv, dir, redirect);
  }

  public OutputStream getOutputStream()
  {
    return stdin;
  }

  public InputStream getInputStream()
  {
    return stdout;
  }

  public InputStream getErrorStream()
  {
    return stderr;
  }

  public synchronized int waitFor() throws InterruptedException
  {
    while (state != TERMINATED)
      wait();
    return exitValue;
  }

  public synchronized int exitValue()
  {
    if (state != TERMINATED)
      throw new IllegalThreadStateException();
    return exitValue;
  }

  public synchronized void destroy()
  {
    if (state == TERMINATED)
      return;

    nativeKill(pid);

    while (state != TERMINATED)
      {
        try
          {
            wait();
          }
        catch (InterruptedException e)
          {
            /* ignore */
          }
      }
  }

  /**
   * Does the fork()/exec() thing to create the O/S process.
   * Must invoke setProcessInfo() before returning successfully.
   * This method is only invoked by processThread.
   *
   * @throws IOException if the O/S process could not be created.
   */
  native void nativeSpawn(String[] cmd, String[] env, File dir,
                          boolean redirect)
    throws IOException;

  /**
   * Test for a reapable child process, and reap if so. Does not block.
   * If a child was reaped, this method must set reapedPid and
   * reapedExitValue appropriately before returning.
   * This method is only invoked by processThread.
   *
   * @return true if a child was reaped, otherwise false
   */
  // This is not private as it is called from an inner class.
  static native boolean nativeReap();

  /**
   * Kill a process. This sends it a fatal signal but does not reap it.
   */
  private static native void nativeKill(long pid);
}