diff options
author | Mikhail Glushenkov <foldr@codedgers.com> | 2009-09-15 03:39:45 +0000 |
---|---|---|
committer | Mikhail Glushenkov <foldr@codedgers.com> | 2009-09-15 03:39:45 +0000 |
commit | 6db0e3a98198fb47b11314935e1201f7ebf2c52d (patch) | |
tree | acce5f7a1fd097c07638ff48bd12812c67ca321e /lib | |
parent | 3d76d11866e056b861c93341cb77a6477406c7fb (diff) | |
download | llvm-6db0e3a98198fb47b11314935e1201f7ebf2c52d.tar.gz llvm-6db0e3a98198fb47b11314935e1201f7ebf2c52d.tar.bz2 llvm-6db0e3a98198fb47b11314935e1201f7ebf2c52d.tar.xz |
Get rid of GetProcessId in Win32/Program.inc.
GetProcessId was introduced only in XP. As a bonus, this change makes Program
objects copyable, since Program is now basically a PID.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@81826 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/System/Unix/Program.inc | 23 | ||||
-rw-r--r-- | lib/System/Win32/Program.inc | 42 |
2 files changed, 22 insertions, 43 deletions
diff --git a/lib/System/Unix/Program.inc b/lib/System/Unix/Program.inc index 56dea250a7..cf6876a54b 100644 --- a/lib/System/Unix/Program.inc +++ b/lib/System/Unix/Program.inc @@ -34,15 +34,6 @@ 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) { @@ -214,7 +205,7 @@ Program::Execute(const Path& path, break; } - Data_ = reinterpret_cast<void*>(child); + Pid_ = child; return true; } @@ -226,7 +217,7 @@ Program::Wait(unsigned secondsToWait, #ifdef HAVE_SYS_WAIT_H struct sigaction Act, Old; - if (Data_ == 0) { + if (Pid_ == 0) { MakeErrMsg(ErrMsg, "Process not started!"); return -1; } @@ -242,8 +233,7 @@ Program::Wait(unsigned secondsToWait, // Parent process: Wait for the child process to terminate. int status; - uint64_t pid = reinterpret_cast<uint64_t>(Data_); - pid_t child = static_cast<pid_t>(pid); + pid_t child = Pid_; while (wait(&status) != child) if (secondsToWait && errno == EINTR) { // Kill the child. @@ -291,15 +281,12 @@ Program::Wait(unsigned secondsToWait, bool Program::Kill(std::string* ErrMsg) { - if (Data_ == 0) { + if (Pid_ == 0) { MakeErrMsg(ErrMsg, "Process not started!"); return true; } - uint64_t pid64 = reinterpret_cast<uint64_t>(Data_); - pid_t pid = static_cast<pid_t>(pid64); - - if (kill(pid, SIGKILL) != 0) { + if (kill(Pid_, SIGKILL) != 0) { MakeErrMsg(ErrMsg, "The process couldn't be killed!"); return true; } diff --git a/lib/System/Win32/Program.inc b/lib/System/Win32/Program.inc index af6cce6de1..f1faff0ed1 100644 --- a/lib/System/Win32/Program.inc +++ b/lib/System/Win32/Program.inc @@ -25,21 +25,6 @@ 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) { @@ -137,11 +122,6 @@ 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) @@ -269,9 +249,10 @@ Program::Execute(const Path& path, path.str() + "'"); return false; } - Data_ = reinterpret_cast<void*>(pi.hProcess); + Pid_ = pi.dwProcessId; // 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. @@ -305,12 +286,17 @@ Program::Execute(const Path& path, int Program::Wait(unsigned secondsToWait, std::string* ErrMsg) { - if (Data_ == 0) { + if (Pid_ == 0) { MakeErrMsg(ErrMsg, "Process not started!"); return -1; } - HANDLE hProcess = reinterpret_cast<HANDLE>(Data_); + HANDLE hOpen = OpenProcess(SYNCHRONIZE, FALSE, Pid_); + if (hOpen == NULL) { + MakeErrMsg(ErrMsg, "OpenProcess failed!"); + return -1; + } + AutoHandle hProcess(hOpen); // Wait for the process to terminate. DWORD millisecondsToWait = INFINITE; @@ -341,12 +327,18 @@ Program::Wait(unsigned secondsToWait, bool Program::Kill(std::string* ErrMsg) { - if (Data_ == 0) { + if (Pid_ == 0) { MakeErrMsg(ErrMsg, "Process not started!"); return true; } - HANDLE hProcess = reinterpret_cast<HANDLE>(Data_); + HANDLE hOpen = OpenProcess(PROCESS_TERMINATE, FALSE, Pid_); + if (hOpen == NULL) { + MakeErrMsg(ErrMsg, "OpenProcess failed!"); + return true; + } + AutoHandle hProcess(hOpen); + if (TerminateProcess(hProcess, 1) == 0) { MakeErrMsg(ErrMsg, "The process couldn't be killed!"); return true; |