2009-10-02 01:21:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
KWSys - Kitware System Library
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2003-06-10 23:46:31 +04:00
|
|
|
|
2009-10-02 01:21:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2003-12-16 00:30:46 +03:00
|
|
|
|
2009-10-02 01:21:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2003-06-10 23:46:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
On Windows9x platforms, this executable is spawned between a parent
|
|
|
|
process and the child it is invoking to work around a bug. See the
|
|
|
|
Win32 implementation file for details.
|
2004-04-02 00:28:18 +04:00
|
|
|
|
|
|
|
Future Work: This executable must be linked statically against the C
|
|
|
|
runtime library before being encoded into the library. Building it
|
|
|
|
in this way may be hard because CMake has limited abilities to build
|
|
|
|
different targets with different configurations in the same
|
|
|
|
directory. We may just have to create and encode the executable
|
|
|
|
once instead of generating it during the build. This would be an
|
|
|
|
acceptable solution because the forwarding executable should not
|
|
|
|
change very often and is pretty simple.
|
2003-06-10 23:46:31 +04:00
|
|
|
*/
|
|
|
|
|
2003-06-30 18:44:35 +04:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning (push, 1)
|
|
|
|
#endif
|
2003-06-10 23:46:31 +04:00
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
void ReportLastError(HANDLE errorPipe);
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
/* Process startup information for the real child. */
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
2003-12-05 19:37:28 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* The result of waiting for the child to exit. */
|
|
|
|
DWORD waitResult;
|
2003-12-05 19:37:28 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* The child's process return code. */
|
|
|
|
DWORD retVal;
|
|
|
|
|
|
|
|
/* The command line used to invoke this process. */
|
|
|
|
LPSTR commandLine = GetCommandLine();
|
|
|
|
|
|
|
|
/* Pointer that will be advanced to the beginning of the command
|
|
|
|
line of the real child process. */
|
|
|
|
LPSTR cmdLine = commandLine;
|
2003-12-05 19:37:28 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Handle to the error reporting pipe provided by the parent. This
|
|
|
|
is parsed off the command line. */
|
|
|
|
HANDLE errorPipe = 0;
|
2003-12-03 17:20:05 +03:00
|
|
|
HANDLE errorPipeOrig = 0;
|
|
|
|
|
|
|
|
/* Handle to the event the parent uses to tell us to resume the child.
|
|
|
|
This is parsed off the command line. */
|
|
|
|
HANDLE resumeEvent = 0;
|
2003-06-10 23:46:31 +04:00
|
|
|
|
|
|
|
/* Handle to the event the parent uses to tell us to kill the child.
|
|
|
|
This is parsed off the command line. */
|
|
|
|
HANDLE killEvent = 0;
|
2003-12-05 19:37:28 +03:00
|
|
|
|
2003-08-05 23:10:33 +04:00
|
|
|
/* Flag for whether to hide window of child process. */
|
|
|
|
int hideWindow = 0;
|
2003-12-05 19:37:28 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* An array of the handles on which we wait when the child is
|
|
|
|
running. */
|
|
|
|
HANDLE waitHandles[2] = {0, 0};
|
2003-12-05 19:37:28 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Move the pointer past the name of this executable. */
|
|
|
|
if(*cmdLine == '"')
|
|
|
|
{
|
|
|
|
++cmdLine;
|
|
|
|
while(*cmdLine && *cmdLine != '"') { ++cmdLine; }
|
|
|
|
if(*cmdLine) { ++cmdLine; }
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the error pipe handle. */
|
|
|
|
while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
|
2003-12-03 17:20:05 +03:00
|
|
|
sscanf(cmdLine, "%p", &errorPipeOrig);
|
|
|
|
|
|
|
|
/* Parse the resume event handle. */
|
|
|
|
while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
|
|
|
|
while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
|
|
|
|
sscanf(cmdLine, "%p", &resumeEvent);
|
2003-06-10 23:46:31 +04:00
|
|
|
|
|
|
|
/* Parse the kill event handle. */
|
|
|
|
while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
|
|
|
|
while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
|
2003-07-01 21:27:42 +04:00
|
|
|
sscanf(cmdLine, "%p", &killEvent);
|
2003-12-05 19:37:28 +03:00
|
|
|
|
2003-08-05 23:10:33 +04:00
|
|
|
/* Parse the hide window flag. */
|
|
|
|
while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
|
|
|
|
while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
|
|
|
|
sscanf(cmdLine, "%d", &hideWindow);
|
2003-12-05 19:37:28 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Skip to the beginning of the command line of the real child. */
|
|
|
|
while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
|
|
|
|
while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
|
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Create a non-inherited copy of the error pipe. We do not want
|
|
|
|
the child to get it. */
|
|
|
|
if(DuplicateHandle(GetCurrentProcess(), errorPipeOrig,
|
|
|
|
GetCurrentProcess(), &errorPipe,
|
|
|
|
0, FALSE, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
/* Have a non-inherited duplicate. Close the inherited one. */
|
|
|
|
CloseHandle(errorPipeOrig);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Could not duplicate handle. Report the error. */
|
|
|
|
ReportLastError(errorPipeOrig);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Create the subprocess. */
|
|
|
|
ZeroMemory(&si, sizeof(si));
|
|
|
|
ZeroMemory(&pi, sizeof(pi));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
|
2003-08-05 23:10:33 +04:00
|
|
|
si.wShowWindow = hideWindow?SW_HIDE:SW_SHOWDEFAULT;
|
2003-06-10 23:46:31 +04:00
|
|
|
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
|
2003-12-03 17:20:05 +03:00
|
|
|
if(CreateProcess(0, cmdLine, 0, 0, TRUE, CREATE_SUSPENDED, 0, 0, &si, &pi))
|
|
|
|
{
|
|
|
|
/* Process created successfully. Close the error reporting pipe
|
|
|
|
to notify the parent of success. */
|
|
|
|
CloseHandle(errorPipe);
|
|
|
|
}
|
|
|
|
else
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
/* Error creating the process. Report the error to the parent
|
|
|
|
process through the special error reporting pipe. */
|
2003-12-03 17:20:05 +03:00
|
|
|
ReportLastError(errorPipe);
|
2003-06-10 23:46:31 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Wait for resume or kill event from parent. */
|
2003-06-10 23:46:31 +04:00
|
|
|
waitHandles[0] = killEvent;
|
2003-12-03 17:20:05 +03:00
|
|
|
waitHandles[1] = resumeEvent;
|
2003-06-10 23:46:31 +04:00
|
|
|
waitResult = WaitForMultipleObjects(2, waitHandles, 0, INFINITE);
|
|
|
|
|
|
|
|
/* Check what happened. */
|
|
|
|
if(waitResult == WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
/* We were asked to kill the child. */
|
2003-06-30 18:44:35 +04:00
|
|
|
TerminateProcess(pi.hProcess, 255);
|
2003-06-10 23:46:31 +04:00
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
|
|
CloseHandle(pi.hProcess);
|
2003-12-05 19:37:28 +03:00
|
|
|
CloseHandle(pi.hThread);
|
2003-06-10 23:46:31 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2003-12-03 17:20:05 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We were asked to resume the child. */
|
|
|
|
ResumeThread(pi.hThread);
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for subprocess to exit or for kill event from parent. */
|
|
|
|
waitHandles[0] = killEvent;
|
|
|
|
waitHandles[1] = pi.hProcess;
|
|
|
|
waitResult = WaitForMultipleObjects(2, waitHandles, 0, INFINITE);
|
|
|
|
|
|
|
|
/* Check what happened. */
|
|
|
|
if(waitResult == WAIT_OBJECT_0)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
/* We were asked to kill the child. */
|
|
|
|
TerminateProcess(pi.hProcess, 255);
|
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
2003-06-10 23:46:31 +04:00
|
|
|
CloseHandle(pi.hProcess);
|
2003-12-03 17:20:05 +03:00
|
|
|
return 1;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
/* The child exited. Get the return code. */
|
|
|
|
GetExitCodeProcess(pi.hProcess, &retVal);
|
2003-06-10 23:46:31 +04:00
|
|
|
CloseHandle(pi.hProcess);
|
2003-12-03 17:20:05 +03:00
|
|
|
return retVal;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
}
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
void ReportLastError(HANDLE errorPipe)
|
|
|
|
{
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
DWORD n;
|
2003-12-05 19:37:28 +03:00
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
2003-12-03 17:20:05 +03:00
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
|
|
|
(LPTSTR) &lpMsgBuf,
|
|
|
|
0,
|
2003-12-05 19:37:28 +03:00
|
|
|
NULL
|
2003-12-03 17:20:05 +03:00
|
|
|
);
|
|
|
|
WriteFile(errorPipe, lpMsgBuf, strlen(lpMsgBuf)+1, &n, 0);
|
|
|
|
LocalFree( lpMsgBuf );
|
|
|
|
}
|