/// @since 1.4
/// @brief An abstraction for finding and executing programs.
class Program {
- /// Opaque handle for target specific data.
- void *Data_;
- // Noncopyable.
- Program(const Program& other);
- Program& operator=(const Program& other);
+ unsigned Pid_;
/// @name Methods
/// @{
public:
- Program();
- ~Program();
+ Program() : Pid_(0) {}
+ ~Program() {}
/// Return process ID of this program.
- unsigned GetPid() const;
+ unsigned GetPid() const { return Pid_; }
/// This function executes the program using the \p arguments provided. The
/// invoked program will inherit the stdin, stdout, and stderr 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) {
break;
}
- Data_ = reinterpret_cast<void*>(child);
+ Pid_ = child;
return true;
}
#ifdef HAVE_SYS_WAIT_H
struct sigaction Act, Old;
- if (Data_ == 0) {
+ if (Pid_ == 0) {
MakeErrMsg(ErrMsg, "Process not started!");
return -1;
}
// 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.
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;
}
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) {
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)
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.
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;
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;