1999-05-12 13:23:44 +02:00
|
|
|
// PosixProcess.java - Subclass of Process for POSIX systems.
|
2007-03-05 16:57:13 +01:00
|
|
|
/* Copyright (C) 1998, 1999, 2004, 2006, 2007 Free Software Foundation
|
1999-05-12 13:23:44 +02:00
|
|
|
|
|
|
|
This file is part of libgcj.
|
|
|
|
|
|
|
|
This software is copyrighted work licensed under the terms of the
|
|
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
package java.lang;
|
|
|
|
|
2002-08-14 03:07:59 +02:00
|
|
|
import java.io.File;
|
2004-08-12 18:20:11 +02:00
|
|
|
import java.io.IOException;
|
1999-05-12 13:23:44 +02:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2007-05-12 19:37:55 +02:00
|
|
|
import java.util.Iterator;
|
2004-08-12 18:20:11 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
|
2007-05-12 19:37:55 +02:00
|
|
|
import gnu.gcj.RawDataManaged;
|
1999-05-12 13:23:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Tom Tromey <tromey@cygnus.com>
|
|
|
|
* @date May 3, 1999
|
2004-08-12 18:20:11 +02:00
|
|
|
* @author David Daney <ddaney@avtrex.com> Rewrote using
|
|
|
|
* ProcessManager
|
1999-05-12 13:23:44 +02:00
|
|
|
*/
|
2007-01-09 20:58:05 +01:00
|
|
|
final class PosixProcess extends Process
|
1999-05-12 13:23:44 +02:00
|
|
|
{
|
2007-05-12 19:37:55 +02:00
|
|
|
static final class ProcessManager extends Thread
|
2004-08-12 18:20:11 +02:00
|
|
|
{
|
|
|
|
/**
|
2007-01-09 20:58:05 +01:00
|
|
|
* A list of {@link PosixProcess PosixProcesses} to be
|
2004-08-12 18:20:11 +02:00
|
|
|
* started. The queueLock object is used as the lock Object
|
|
|
|
* for all process related operations. To avoid dead lock
|
2007-01-09 20:58:05 +01:00
|
|
|
* ensure queueLock is obtained before PosixProcess.
|
2004-08-12 18:20:11 +02:00
|
|
|
*/
|
2007-05-12 19:37:55 +02:00
|
|
|
private LinkedList<PosixProcess> queue = new LinkedList<PosixProcess>();
|
|
|
|
private LinkedList<PosixProcess> liveProcesses =
|
|
|
|
new LinkedList<PosixProcess>();
|
2004-08-12 18:20:11 +02:00
|
|
|
private boolean ready = false;
|
2007-05-12 19:37:55 +02:00
|
|
|
|
|
|
|
static RawDataManaged nativeData;
|
2004-08-12 18:20:11 +02:00
|
|
|
|
|
|
|
ProcessManager()
|
|
|
|
{
|
Thread.java (Thread(ThreadGroup, Runnable, String)): Pass new parameter constructor.
* java/lang/Thread.java (Thread(ThreadGroup, Runnable, String)): Pass
new parameter constructor.
(Thread(ThreadGroup, Runnable, String, long)): Same.
(Thread(String, boolean)): New constructor.
(Thread(Thread, ThreadGroup, Runnable, String): Add parameter
noInheritableThreadLocal, don't call
InheritableThreadLocal.newChildThread if set.
* java/lang/PosixProcess.java(ProcessManager()): Set
noInheritableThreadLocal in super.
* java/lang/natThread.cc (_Jv_AttachCurrentThread): Pass new
parameter to Thread constructor.
(_Jv_AttachCurrentThreadAsDaemon): Same.
* java/lang/Thread.h: Regenerate.
* classpath/lib/java/lang/Thread.class: Same.
* classpath/lib/java/lang/PosixProcess$EOFInputStream.class: Same.
* classpath/lib/java/lang/PosixProcess.class: Same.
* classpath/lib/java/lang/Thread$State.class: Same.
* classpath/lib/java/lang/PosixProcess$ProcessManager.class: Same.
From-SVN: r122054
2007-02-16 22:23:10 +01:00
|
|
|
// Use package private Thread constructor to place us in the
|
|
|
|
// root ThreadGroup with no InheritableThreadLocal. If the
|
|
|
|
// InheritableThreadLocals were allowed to initialize, they could
|
|
|
|
// cause a Runtime.exec() to be called causing infinite
|
|
|
|
// recursion.
|
|
|
|
super("ProcessManager", true);
|
2004-08-12 18:20:11 +02:00
|
|
|
// Don't keep the (main) process from exiting on our account.
|
|
|
|
this.setDaemon(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-05-12 19:37:55 +02:00
|
|
|
* Add a process to the list of running processes. This must only
|
|
|
|
* be called with the queueLock held.
|
2004-08-12 18:20:11 +02:00
|
|
|
*
|
2007-01-09 20:58:05 +01:00
|
|
|
* @param p The PosixProcess.
|
2004-08-12 18:20:11 +02:00
|
|
|
*/
|
2007-05-12 19:37:55 +02:00
|
|
|
void addToLiveProcesses(PosixProcess p)
|
2004-08-12 18:20:11 +02:00
|
|
|
{
|
2007-05-12 19:37:55 +02:00
|
|
|
liveProcesses.add(p);
|
2004-08-12 18:20:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-01-09 20:58:05 +01:00
|
|
|
* Queue up the PosixProcess and awake the ProcessManager.
|
|
|
|
* The ProcessManager will start the PosixProcess from its
|
2004-08-12 18:20:11 +02:00
|
|
|
* thread so it can be reaped when it terminates.
|
|
|
|
*
|
2007-01-09 20:58:05 +01:00
|
|
|
* @param p The PosixProcess.
|
2004-08-12 18:20:11 +02:00
|
|
|
*/
|
2007-01-09 20:58:05 +01:00
|
|
|
void startExecuting(PosixProcess p)
|
2004-08-12 18:20:11 +02:00
|
|
|
{
|
|
|
|
synchronized (queueLock)
|
|
|
|
{
|
|
|
|
queue.add(p);
|
|
|
|
signalReaper(); // If blocked in waitForSignal().
|
|
|
|
queueLock.notifyAll(); // If blocked in wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block until the ProcessManager thread is ready to accept
|
|
|
|
* commands.
|
|
|
|
*/
|
|
|
|
void waitUntilReady()
|
|
|
|
{
|
|
|
|
synchronized (this)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while (! ready)
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
catch (InterruptedException ie)
|
|
|
|
{
|
|
|
|
// Ignore.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main Process starting/reaping loop.
|
|
|
|
*/
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
// Now ready to accept requests.
|
|
|
|
synchronized (this)
|
|
|
|
{
|
2007-05-12 19:37:55 +02:00
|
|
|
ready = true;
|
|
|
|
this.notifyAll();
|
2004-08-12 18:20:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2007-05-12 19:37:55 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
synchronized (queueLock)
|
|
|
|
{
|
|
|
|
Iterator<PosixProcess> processIterator =
|
|
|
|
liveProcesses.iterator();
|
|
|
|
while (processIterator.hasNext())
|
|
|
|
{
|
|
|
|
boolean reaped = reap(processIterator.next());
|
|
|
|
if (reaped)
|
|
|
|
processIterator.remove();
|
|
|
|
}
|
|
|
|
if (liveProcesses.size() == 0 && queue.size() == 0)
|
|
|
|
{
|
|
|
|
// This reaper thread could exit, but we keep it
|
|
|
|
// alive for a while in case someone wants to
|
|
|
|
// start more Processes.
|
|
|
|
try
|
|
|
|
{
|
|
|
|
queueLock.wait(1000L);
|
|
|
|
if (queue.size() == 0)
|
|
|
|
{
|
|
|
|
processManager = null;
|
|
|
|
return; // Timed out.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (InterruptedException ie)
|
|
|
|
{
|
|
|
|
// Ignore and exit the thread.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (queue.size() > 0)
|
|
|
|
{
|
|
|
|
PosixProcess p = queue.remove(0);
|
|
|
|
p.spawn(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for a SIGCHLD from either an exiting process or
|
|
|
|
// the startExecuting() method. This is done outside of
|
|
|
|
// the synchronized block to allow other threads to
|
|
|
|
// enter and submit more jobs.
|
|
|
|
waitForSignal();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
ex.printStackTrace(System.err);
|
|
|
|
}
|
2004-08-12 18:20:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup native signal handlers and other housekeeping things.
|
|
|
|
*/
|
|
|
|
private native void init();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block waiting for SIGCHLD.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private native void waitForSignal();
|
|
|
|
|
|
|
|
/**
|
2007-05-12 19:37:55 +02:00
|
|
|
* Try to reap the specified child without blocking.
|
|
|
|
*
|
|
|
|
* @param p the process to try to reap.
|
2004-08-12 18:20:11 +02:00
|
|
|
*
|
2007-05-12 19:37:55 +02:00
|
|
|
* @return true if the process terminated.
|
2004-08-12 18:20:11 +02:00
|
|
|
*
|
|
|
|
*/
|
2007-05-12 19:37:55 +02:00
|
|
|
private native boolean reap(PosixProcess p);
|
2004-08-12 18:20:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send SIGCHLD to the reaper thread.
|
|
|
|
*/
|
|
|
|
private native void signalReaper();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void destroy()
|
2001-09-24 06:51:50 +02:00
|
|
|
{
|
2004-08-12 18:20:11 +02:00
|
|
|
// Synchronized on the queueLock. This ensures that the reaper
|
|
|
|
// thread cannot be doing a wait() on the child.
|
|
|
|
// Otherwise there would be a race where the OS could
|
|
|
|
// create a process with the same pid between the wait()
|
|
|
|
// and the update of the state which would cause a kill to
|
|
|
|
// the wrong process.
|
|
|
|
synchronized (queueLock)
|
|
|
|
{
|
|
|
|
synchronized (this)
|
|
|
|
{
|
|
|
|
// If there is no ProcessManager we cannot kill.
|
|
|
|
if (state != STATE_TERMINATED)
|
|
|
|
{
|
|
|
|
if (processManager == null)
|
|
|
|
throw new InternalError();
|
|
|
|
nativeDestroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private native void nativeDestroy();
|
|
|
|
|
|
|
|
public int exitValue()
|
|
|
|
{
|
|
|
|
synchronized (this)
|
|
|
|
{
|
|
|
|
if (state != STATE_TERMINATED)
|
|
|
|
throw new IllegalThreadStateException("Process has not exited");
|
|
|
|
}
|
2001-09-24 06:51:50 +02:00
|
|
|
return status;
|
|
|
|
}
|
1999-05-12 13:23:44 +02:00
|
|
|
|
2004-08-12 18:20:11 +02:00
|
|
|
/**
|
|
|
|
* Called by native code when process exits.
|
|
|
|
*
|
|
|
|
* Already synchronized (this). Close any streams that we can to
|
|
|
|
* conserve file descriptors.
|
|
|
|
*
|
|
|
|
* The outputStream can be closed as any future writes will
|
|
|
|
* generate an IOException due to EPIPE.
|
|
|
|
*
|
|
|
|
* The inputStream and errorStream can only be closed if the user
|
|
|
|
* has not obtained a reference to them AND they have no bytes
|
|
|
|
* available. Since the process has terminated they will never have
|
|
|
|
* any more data available and can safely be replaced by
|
|
|
|
* EOFInputStreams.
|
|
|
|
*/
|
|
|
|
void processTerminationCleanup()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
outputStream.close();
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
// Ignore.
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (returnedErrorStream == null && errorStream.available() == 0)
|
|
|
|
{
|
|
|
|
errorStream.close();
|
|
|
|
errorStream = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
// Ignore.
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (returnedInputStream == null && inputStream.available() == 0)
|
|
|
|
{
|
|
|
|
inputStream.close();
|
|
|
|
inputStream = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
// Ignore.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized InputStream getErrorStream()
|
1999-05-12 13:23:44 +02:00
|
|
|
{
|
2004-08-12 18:20:11 +02:00
|
|
|
if (returnedErrorStream != null)
|
|
|
|
return returnedErrorStream;
|
|
|
|
|
|
|
|
if (errorStream == null)
|
|
|
|
returnedErrorStream = EOFInputStream.instance;
|
|
|
|
else
|
|
|
|
returnedErrorStream = errorStream;
|
2007-05-12 19:37:55 +02:00
|
|
|
|
2004-08-12 18:20:11 +02:00
|
|
|
return returnedErrorStream;
|
1999-05-12 13:23:44 +02:00
|
|
|
}
|
|
|
|
|
2004-08-12 18:20:11 +02:00
|
|
|
public synchronized InputStream getInputStream()
|
1999-05-12 13:23:44 +02:00
|
|
|
{
|
2004-08-12 18:20:11 +02:00
|
|
|
if (returnedInputStream != null)
|
|
|
|
return returnedInputStream;
|
|
|
|
|
|
|
|
if (inputStream == null)
|
|
|
|
returnedInputStream = EOFInputStream.instance;
|
|
|
|
else
|
|
|
|
returnedInputStream = inputStream;
|
2007-05-12 19:37:55 +02:00
|
|
|
|
2004-08-12 18:20:11 +02:00
|
|
|
return returnedInputStream;
|
1999-05-12 13:23:44 +02:00
|
|
|
}
|
|
|
|
|
2004-08-12 18:20:11 +02:00
|
|
|
public OutputStream getOutputStream()
|
1999-05-12 13:23:44 +02:00
|
|
|
{
|
|
|
|
return outputStream;
|
|
|
|
}
|
|
|
|
|
2004-08-12 18:20:11 +02:00
|
|
|
public int waitFor() throws InterruptedException
|
|
|
|
{
|
|
|
|
synchronized (this)
|
|
|
|
{
|
|
|
|
while (state != STATE_TERMINATED)
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
1999-05-12 13:23:44 +02:00
|
|
|
|
2004-08-12 18:20:11 +02:00
|
|
|
/**
|
|
|
|
* Start this process running. This should only be called by the
|
2007-05-12 19:37:55 +02:00
|
|
|
* ProcessManager with the queueLock held.
|
2004-08-12 18:20:11 +02:00
|
|
|
*
|
|
|
|
* @param pm The ProcessManager that made the call.
|
|
|
|
*/
|
|
|
|
void spawn(ProcessManager pm)
|
|
|
|
{
|
|
|
|
synchronized (this)
|
|
|
|
{
|
|
|
|
// Do the fork/exec magic.
|
|
|
|
nativeSpawn();
|
|
|
|
// There is no race with reap() in the pidToProcess map
|
|
|
|
// because this is always called from the same thread
|
|
|
|
// doing the reaping.
|
2007-05-12 19:37:55 +02:00
|
|
|
pm.addToLiveProcesses(this);
|
2004-08-12 18:20:11 +02:00
|
|
|
state = STATE_RUNNING;
|
|
|
|
// Notify anybody waiting on state change.
|
|
|
|
this.notifyAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do the fork and exec.
|
|
|
|
*/
|
|
|
|
private native void nativeSpawn();
|
1999-05-12 13:23:44 +02:00
|
|
|
|
2007-03-05 16:57:13 +01:00
|
|
|
PosixProcess(String[] progarray, String[] envp, File dir, boolean redirect)
|
|
|
|
throws IOException
|
1999-05-12 13:23:44 +02:00
|
|
|
{
|
2004-08-12 18:20:11 +02:00
|
|
|
// Check to ensure there is something to run, and avoid
|
|
|
|
// dereferencing null pointers in native code.
|
|
|
|
if (progarray[0] == null)
|
|
|
|
throw new NullPointerException();
|
|
|
|
|
|
|
|
this.progarray = progarray;
|
|
|
|
this.envp = envp;
|
|
|
|
this.dir = dir;
|
2007-03-05 16:57:13 +01:00
|
|
|
this.redirect = redirect;
|
2004-08-12 18:20:11 +02:00
|
|
|
|
|
|
|
// Start a ProcessManager if there is not one already running.
|
|
|
|
synchronized (queueLock)
|
|
|
|
{
|
|
|
|
if (processManager == null)
|
|
|
|
{
|
|
|
|
processManager = new ProcessManager();
|
|
|
|
processManager.start();
|
|
|
|
processManager.waitUntilReady();
|
|
|
|
}
|
|
|
|
|
2007-01-09 20:58:05 +01:00
|
|
|
// Queue this PosixProcess for starting by the ProcessManager.
|
2004-08-12 18:20:11 +02:00
|
|
|
processManager.startExecuting(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until ProcessManager has started us.
|
|
|
|
synchronized (this)
|
|
|
|
{
|
|
|
|
while (state == STATE_WAITING_TO_START)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
catch (InterruptedException ie)
|
|
|
|
{
|
|
|
|
// FIXME: What to do when interrupted while blocking in a constructor?
|
|
|
|
// Ignore.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there was a problem, re-throw it.
|
|
|
|
if (exception != null)
|
|
|
|
{
|
|
|
|
if (exception instanceof IOException)
|
|
|
|
{
|
|
|
|
IOException ioe = new IOException(exception.toString());
|
|
|
|
ioe.initCause(exception);
|
|
|
|
throw ioe;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not an IOException. Something bad happened.
|
|
|
|
InternalError ie = new InternalError(exception.toString());
|
|
|
|
ie.initCause(exception);
|
|
|
|
throw ie;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, all is well, the Process has started.
|
1999-05-12 13:23:44 +02:00
|
|
|
}
|
|
|
|
|
2004-08-12 18:20:11 +02:00
|
|
|
private String[] progarray;
|
|
|
|
private String[] envp;
|
|
|
|
private File dir;
|
2007-03-05 16:57:13 +01:00
|
|
|
private boolean redirect;
|
2004-08-12 18:20:11 +02:00
|
|
|
|
|
|
|
/** Set by the ProcessManager on problems starting. */
|
|
|
|
private Throwable exception;
|
|
|
|
|
|
|
|
/** The process id. This is cast to a pid_t on the native side. */
|
2007-05-12 19:37:55 +02:00
|
|
|
long pid;
|
1999-05-12 13:23:44 +02:00
|
|
|
|
2007-01-09 20:58:05 +01:00
|
|
|
// FIXME: Why doesn't the friend declaration in PosixProcess.h
|
|
|
|
// allow PosixProcess$ProcessManager native code access these
|
2004-08-12 18:20:11 +02:00
|
|
|
// when they are private?
|
|
|
|
|
|
|
|
/** Before the process is forked. */
|
|
|
|
static final int STATE_WAITING_TO_START = 0;
|
|
|
|
|
|
|
|
/** After the fork. */
|
|
|
|
static final int STATE_RUNNING = 1;
|
|
|
|
|
|
|
|
/** After exit code has been collected. */
|
|
|
|
static final int STATE_TERMINATED = 2;
|
1999-05-12 13:23:44 +02:00
|
|
|
|
2004-08-12 18:20:11 +02:00
|
|
|
/** One of STATE_WAITING_TO_START, STATE_RUNNING, STATE_TERMINATED. */
|
|
|
|
int state;
|
1999-05-12 13:23:44 +02:00
|
|
|
|
2004-08-12 18:20:11 +02:00
|
|
|
/** The exit status, if the child has exited. */
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/** The streams. */
|
1999-05-12 13:23:44 +02:00
|
|
|
private InputStream errorStream;
|
|
|
|
private InputStream inputStream;
|
|
|
|
private OutputStream outputStream;
|
2004-08-12 18:20:11 +02:00
|
|
|
|
|
|
|
/** InputStreams obtained by the user. Not null indicates that the
|
|
|
|
* user has obtained the stream.
|
|
|
|
*/
|
|
|
|
private InputStream returnedErrorStream;
|
|
|
|
private InputStream returnedInputStream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lock Object for all processManager related locking.
|
|
|
|
*/
|
|
|
|
private static Object queueLock = new Object();
|
|
|
|
private static ProcessManager processManager;
|
|
|
|
|
|
|
|
static class EOFInputStream extends InputStream
|
|
|
|
{
|
|
|
|
static EOFInputStream instance = new EOFInputStream();
|
|
|
|
public int read()
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
1999-05-12 13:23:44 +02:00
|
|
|
}
|