From: Daniel Dunbar Date: Mon, 3 Aug 2009 05:02:46 +0000 (+0000) Subject: Fix a race condition in getting the process exit code on Win32. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=defc85327a7cd0ad679cd3fe94f671d6549fe4a1;p=oota-llvm.git Fix a race condition in getting the process exit code on Win32. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77953 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/System/Program.h b/include/llvm/System/Program.h index 177fa7cf7ee..7f96245130f 100644 --- a/include/llvm/System/Program.h +++ b/include/llvm/System/Program.h @@ -29,6 +29,9 @@ namespace sys { /// @since 1.4 /// @brief An abstraction for finding and executing programs. class Program { + /// Opaque handle for target specific data. + void *Data; + unsigned Pid_; // Noncopyable. @@ -39,9 +42,9 @@ namespace sys { /// @{ public: - Program() : Pid_(0) - {} - + Program(); + ~Program(); + /// Return process ID of this program. unsigned GetPid() { return Pid_; } diff --git a/lib/System/Unix/Program.inc b/lib/System/Unix/Program.inc index 3c5a54cffc1..3a562e45d41 100644 --- a/lib/System/Unix/Program.inc +++ b/lib/System/Unix/Program.inc @@ -35,6 +35,10 @@ namespace llvm { using namespace sys; +Program::Program() : Pid_(0) {} + +Program::~Program() {} + // This function just uses the PATH environment variable to find the program. Path Program::FindProgramByName(const std::string& progName) { diff --git a/lib/System/Win32/Program.inc b/lib/System/Win32/Program.inc index 963946a985e..005f63157c0 100644 --- a/lib/System/Win32/Program.inc +++ b/lib/System/Win32/Program.inc @@ -25,6 +25,16 @@ namespace llvm { using namespace sys; +Program::Program() : Pid_(0), Data(0) {} + +Program::~Program() { + if (Data) { + HANDLE hProcess = (HANDLE) Data; + CloseHandle(hProcess); + Data = 0; + } +} + // This function just uses the PATH environment variable to find the program. Path Program::FindProgramByName(const std::string& progName) { @@ -122,6 +132,12 @@ Program::Execute(const Path& path, const Path** redirects, unsigned memoryLimit, std::string* ErrMsg) { + if (Data) { + HANDLE hProcess = (HANDLE) Data; + CloseHandle(Data); + Data = 0; + } + if (!path.canExecute()) { if (ErrMsg) *ErrMsg = "program not executable"; @@ -250,9 +266,9 @@ Program::Execute(const Path& path, return false; } Pid_ = pi.dwProcessId; - + Data = pi.hProcess; + // Make sure these get closed no matter what. - AutoHandle hProcess(pi.hProcess); AutoHandle hThread(pi.hThread); // Assign the process to a job if a memory limit is defined. @@ -286,13 +302,13 @@ Program::Execute(const Path& path, int Program::Wait(unsigned secondsToWait, std::string* ErrMsg) { - if (Pid_ == 0) { + if (Data == 0) { MakeErrMsg(ErrMsg, "Process not started!"); return -1; } - AutoHandle hProcess = OpenProcess(SYNCHRONIZE, FALSE, Pid_); - + HANDLE hProcess = (HANDLE) Data; + // Wait for the process to terminate. DWORD millisecondsToWait = INFINITE; if (secondsToWait > 0)