natWin32Process.cc [plain text]
#include <config.h>
#include <platform.h>
#undef STRICT
#include <java/lang/ConcreteProcess.h>
#include <java/lang/IllegalThreadStateException.h>
#include <java/lang/InterruptedException.h>
#include <java/lang/NullPointerException.h>
#include <java/lang/Thread.h>
#include <java/io/File.h>
#include <java/io/FileDescriptor.h>
#include <java/io/FileInputStream.h>
#include <java/io/FileOutputStream.h>
#include <java/io/IOException.h>
#include <java/lang/OutOfMemoryError.h>
#include <gnu/java/nio/channels/FileChannelImpl.h>
using gnu::java::nio::channels::FileChannelImpl;
void
java::lang::ConcreteProcess::cleanup (void)
{
inputStream = NULL;
outputStream = NULL;
errorStream = NULL;
if (procHandle)
{
CloseHandle((HANDLE) procHandle);
procHandle = (jint) INVALID_HANDLE_VALUE;
}
}
void
java::lang::ConcreteProcess::destroy (void)
{
if (! hasExited ())
{
TerminateProcess ((HANDLE) procHandle, 0);
exitCode = 0;
cleanup ();
}
}
jboolean
java::lang::ConcreteProcess::hasExited (void)
{
DWORD exitStatus;
if (GetExitCodeProcess ((HANDLE) procHandle, &exitStatus) != 0)
{
if (exitStatus == STILL_ACTIVE)
return false;
else
{
cleanup ();
exitCode = exitStatus;
return true;
}
}
else
return true;
}
jint
java::lang::ConcreteProcess::waitFor (void)
{
if (! hasExited ())
{
DWORD exitStatus = 0UL;
HANDLE arh[2];
arh[0] = (HANDLE) procHandle;
arh[1] = _Jv_Win32GetInterruptEvent ();
DWORD rval = WaitForMultipleObjects (2, arh, 0, INFINITE);
bool bInterrupted = rval == (WAIT_OBJECT_0 + 1);
if (bInterrupted)
{
Thread::interrupted();
cleanup ();
throw new InterruptedException ();
}
GetExitCodeProcess ((HANDLE) procHandle, &exitStatus);
exitCode = exitStatus;
cleanup ();
}
return exitCode;
}
class ChildProcessPipe
{
public:
enum EType {INPUT, OUTPUT};
ChildProcessPipe(EType eType);
~ChildProcessPipe();
HANDLE getParentHandle();
HANDLE getChildHandle();
private:
EType m_eType;
HANDLE m_hRead, m_hWrite;
};
ChildProcessPipe::ChildProcessPipe(EType eType):
m_eType(eType)
{
SECURITY_ATTRIBUTES sAttrs;
sAttrs.nLength = sizeof (SECURITY_ATTRIBUTES);
sAttrs.bInheritHandle = 1;
sAttrs.lpSecurityDescriptor = NULL;
if (CreatePipe (&m_hRead, &m_hWrite, &sAttrs, 0) == 0)
{
DWORD dwErrorCode = GetLastError ();
throw new java::io::IOException (
_Jv_WinStrError (_T("Error creating pipe"), dwErrorCode));
}
HANDLE& rhStd = m_eType==INPUT ? m_hWrite : m_hRead;
_Jv_platform_close_on_exec (rhStd);
}
ChildProcessPipe::~ChildProcessPipe()
{
CloseHandle(getChildHandle());
}
HANDLE ChildProcessPipe::getParentHandle()
{
return m_eType==INPUT ? m_hWrite : m_hRead;
}
HANDLE ChildProcessPipe::getChildHandle()
{
return m_eType==INPUT ? m_hRead : m_hWrite;
}
void
java::lang::ConcreteProcess::startProcess (jstringArray progarray,
jstringArray envp,
java::io::File *dir)
{
using namespace java::io;
procHandle = (jint) INVALID_HANDLE_VALUE;
jstring *elts = elements (progarray);
int cmdLineLen = 0;
for (int i = 0; i < progarray->length; ++i)
cmdLineLen += (elts[i]->length() + 1);
LPTSTR cmdLine = (LPTSTR) _Jv_Malloc ((cmdLineLen + 1) * sizeof(TCHAR));
LPTSTR cmdLineCurPos = cmdLine;
for (int i = 0; i < progarray->length; ++i)
{
if (i > 0)
*cmdLineCurPos++ = _T(' ');
jint len = elts[i]->length();
JV_TEMP_STRING_WIN32(thiselt, elts[i]);
_tcscpy(cmdLineCurPos, thiselt);
cmdLineCurPos += len;
}
*cmdLineCurPos = _T('\0');
LPTSTR env = NULL;
if (envp)
{
elts = elements (envp);
int envLen = 0;
for (int i = 0; i < envp->length; ++i)
envLen += (elts[i]->length() + 1);
env = (LPTSTR) _Jv_Malloc ((envLen + 1) * sizeof(TCHAR));
int j = 0;
for (int i = 0; i < envp->length; ++i)
{
jint len = elts[i]->length();
JV_TEMP_STRING_WIN32(thiselt, elts[i]);
_tcscpy(env + j, thiselt);
j += len;
j++;
}
*(env + j) = _T('\0');
}
JV_TEMP_STRING_WIN32 (wdir, dir ? dir->getPath () : 0);
errorStream = NULL;
inputStream = NULL;
outputStream = NULL;
java::lang::Throwable *exc = NULL;
try
{
ChildProcessPipe aChildStdIn(ChildProcessPipe::INPUT);
ChildProcessPipe aChildStdOut(ChildProcessPipe::OUTPUT);
ChildProcessPipe aChildStdErr(ChildProcessPipe::OUTPUT);
outputStream = new FileOutputStream (new FileChannelImpl (
(jint) aChildStdIn.getParentHandle (),
FileChannelImpl::WRITE));
inputStream = new FileInputStream (new FileChannelImpl (
(jint) aChildStdOut.getParentHandle (),
FileChannelImpl::READ));
errorStream = new FileInputStream (new FileChannelImpl (
(jint) aChildStdErr.getParentHandle (),
FileChannelImpl::READ));
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory (&pi, sizeof (PROCESS_INFORMATION));
ZeroMemory (&si, sizeof (STARTUPINFO));
si.cb = sizeof (STARTUPINFO);
si.dwFlags |= STARTF_USESTDHANDLES;
si.hStdInput = aChildStdIn.getChildHandle();
si.hStdOutput = aChildStdOut.getChildHandle();
si.hStdError = aChildStdErr.getChildHandle();
if (CreateProcess (NULL,
cmdLine,
NULL,
NULL,
1,
CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT,
env,
wdir,
&si,
&pi) == 0)
{
DWORD dwErrorCode = GetLastError ();
throw new IOException (
_Jv_WinStrError (_T("Error creating child process"), dwErrorCode));
}
procHandle = (jint ) pi.hProcess;
_Jv_Free (cmdLine);
if (env != NULL)
_Jv_Free (env);
}
catch (java::lang::Throwable *thrown)
{
cleanup ();
exc = thrown;
}
if (exc != NULL)
throw exc;
}