Fix a race condition in getting the process exit code on Win32.
authorDaniel Dunbar <daniel@zuster.org>
Mon, 3 Aug 2009 05:02:46 +0000 (05:02 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Mon, 3 Aug 2009 05:02:46 +0000 (05:02 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77953 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/System/Program.h
lib/System/Unix/Program.inc
lib/System/Win32/Program.inc

index 177fa7cf7ee779090a6ceb5c7dd04872f3a03b33..7f96245130f51fde901c79c0583283db17326ad6 100644 (file)
@@ -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_; }
 
index 3c5a54cffc1a88102b958b0fb9260045a5230c0f..3a562e45d41d5df83e5c2b506929c3fa5d370ffb 100644 (file)
 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) {
index 963946a985ee220f4f8ef1c1ecc1c0ebe462d24c..005f63157c03d98267c5c2c14031c92e6566794f 100644 (file)
 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)