OSDN Git Service

Revert "Get rid of GetProcessId in Win32/Program.inc.", this breaks
authorDaniel Dunbar <daniel@zuster.org>
Tue, 22 Sep 2009 04:44:56 +0000 (04:44 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Tue, 22 Sep 2009 04:44:56 +0000 (04:44 +0000)
ExecuteAndWait.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82522 91177308-0d34-0410-b5e6-96231b3b80d8

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

index b200edd..6799562 100644 (file)
@@ -29,18 +29,22 @@ 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.
+    Program(const Program& other);
+    Program& operator=(const Program& other);
 
     /// @name Methods
     /// @{
   public:
 
-    Program() : Pid_(0) {}
-    ~Program() {}
+    Program();
+    ~Program();
 
     /// Return process ID of this program.
-    unsigned GetPid() const { return Pid_; }
+    unsigned GetPid() const;
 
     /// This function executes the program using the \p arguments provided.  The
     /// invoked program will inherit the stdin, stdout, and stderr file
index cf6876a..56dea25 100644 (file)
 namespace llvm {
 using namespace sys;
 
+Program::Program() : Data_(0) {}
+
+Program::~Program() {}
+
+unsigned Program::GetPid() const {
+  uint64_t pid = reinterpret_cast<uint64_t>(Data_);
+  return static_cast<unsigned>(pid);
+}
+
 // This function just uses the PATH environment variable to find the program.
 Path
 Program::FindProgramByName(const std::string& progName) {
@@ -205,7 +214,7 @@ Program::Execute(const Path& path,
       break;
   }
 
-  Pid_ = child;
+  Data_ = reinterpret_cast<void*>(child);
 
   return true;
 }
@@ -217,7 +226,7 @@ Program::Wait(unsigned secondsToWait,
 #ifdef HAVE_SYS_WAIT_H
   struct sigaction Act, Old;
 
-  if (Pid_ == 0) {
+  if (Data_ == 0) {
     MakeErrMsg(ErrMsg, "Process not started!");
     return -1;
   }
@@ -233,7 +242,8 @@ Program::Wait(unsigned secondsToWait,
 
   // Parent process: Wait for the child process to terminate.
   int status;
-  pid_t child = Pid_;
+  uint64_t pid = reinterpret_cast<uint64_t>(Data_);
+  pid_t child = static_cast<pid_t>(pid);
   while (wait(&status) != child)
     if (secondsToWait && errno == EINTR) {
       // Kill the child.
@@ -281,12 +291,15 @@ Program::Wait(unsigned secondsToWait,
 
 bool
 Program::Kill(std::string* ErrMsg) {
-  if (Pid_ == 0) {
+  if (Data_ == 0) {
     MakeErrMsg(ErrMsg, "Process not started!");
     return true;
   }
 
-  if (kill(Pid_, SIGKILL) != 0) {
+  uint64_t pid64 = reinterpret_cast<uint64_t>(Data_);
+  pid_t pid = static_cast<pid_t>(pid64);
+
+  if (kill(pid, SIGKILL) != 0) {
     MakeErrMsg(ErrMsg, "The process couldn't be killed!");
     return true;
   }
index f1faff0..af6cce6 100644 (file)
 namespace llvm {
 using namespace sys;
 
+Program::Program() : Data_(0) {}
+
+Program::~Program() {
+  if (Data_) {
+    HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
+    CloseHandle(hProcess);
+    Data_ = 0;
+  }
+}
+
+unsigned Program::GetPid() const {
+  HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
+  return GetProcessId(hProcess);
+}
+
 // This function just uses the PATH environment variable to find the program.
 Path
 Program::FindProgramByName(const std::string& progName) {
@@ -122,6 +137,11 @@ Program::Execute(const Path& path,
                  const Path** redirects,
                  unsigned memoryLimit,
                  std::string* ErrMsg) {
+  if (Data_) {
+    HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
+    CloseHandle(Data_);
+    Data_ = 0;
+  }
 
   if (!path.canExecute()) {
     if (ErrMsg)
@@ -249,10 +269,9 @@ Program::Execute(const Path& path,
                path.str() + "'");
     return false;
   }
-  Pid_ = pi.dwProcessId;
+  Data_ = reinterpret_cast<void*>(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,17 +305,12 @@ 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;
   }
 
-  HANDLE hOpen = OpenProcess(SYNCHRONIZE, FALSE, Pid_);
-  if (hOpen == NULL) {
-    MakeErrMsg(ErrMsg, "OpenProcess failed!");
-    return -1;
-  }
-  AutoHandle hProcess(hOpen);
+  HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
 
   // Wait for the process to terminate.
   DWORD millisecondsToWait = INFINITE;
@@ -327,18 +341,12 @@ Program::Wait(unsigned secondsToWait,
 
 bool
 Program::Kill(std::string* ErrMsg) {
-  if (Pid_ == 0) {
+  if (Data_ == 0) {
     MakeErrMsg(ErrMsg, "Process not started!");
     return true;
   }
 
-  HANDLE hOpen = OpenProcess(PROCESS_TERMINATE, FALSE, Pid_);
-  if (hOpen == NULL) {
-    MakeErrMsg(ErrMsg, "OpenProcess failed!");
-    return true;
-  }
-  AutoHandle hProcess(hOpen);
-
+  HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
   if (TerminateProcess(hProcess, 1) == 0) {
     MakeErrMsg(ErrMsg, "The process couldn't be killed!");
     return true;