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. llvm-svn: 81826
This commit is contained in:
parent
3413f277d0
commit
0adf07eb9e
|
@ -29,22 +29,18 @@ namespace sys {
|
||||||
/// @since 1.4
|
/// @since 1.4
|
||||||
/// @brief An abstraction for finding and executing programs.
|
/// @brief An abstraction for finding and executing programs.
|
||||||
class Program {
|
class Program {
|
||||||
/// Opaque handle for target specific data.
|
|
||||||
void *Data_;
|
|
||||||
|
|
||||||
// Noncopyable.
|
unsigned Pid_;
|
||||||
Program(const Program& other);
|
|
||||||
Program& operator=(const Program& other);
|
|
||||||
|
|
||||||
/// @name Methods
|
/// @name Methods
|
||||||
/// @{
|
/// @{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Program();
|
Program() : Pid_(0) {}
|
||||||
~Program();
|
~Program() {}
|
||||||
|
|
||||||
/// Return process ID of this 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
|
/// This function executes the program using the \p arguments provided. The
|
||||||
/// invoked program will inherit the stdin, stdout, and stderr file
|
/// invoked program will inherit the stdin, stdout, and stderr file
|
||||||
|
|
|
@ -34,15 +34,6 @@
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
using namespace sys;
|
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.
|
// This function just uses the PATH environment variable to find the program.
|
||||||
Path
|
Path
|
||||||
Program::FindProgramByName(const std::string& progName) {
|
Program::FindProgramByName(const std::string& progName) {
|
||||||
|
@ -214,7 +205,7 @@ Program::Execute(const Path& path,
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Data_ = reinterpret_cast<void*>(child);
|
Pid_ = child;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -226,7 +217,7 @@ Program::Wait(unsigned secondsToWait,
|
||||||
#ifdef HAVE_SYS_WAIT_H
|
#ifdef HAVE_SYS_WAIT_H
|
||||||
struct sigaction Act, Old;
|
struct sigaction Act, Old;
|
||||||
|
|
||||||
if (Data_ == 0) {
|
if (Pid_ == 0) {
|
||||||
MakeErrMsg(ErrMsg, "Process not started!");
|
MakeErrMsg(ErrMsg, "Process not started!");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -242,8 +233,7 @@ Program::Wait(unsigned secondsToWait,
|
||||||
|
|
||||||
// Parent process: Wait for the child process to terminate.
|
// Parent process: Wait for the child process to terminate.
|
||||||
int status;
|
int status;
|
||||||
uint64_t pid = reinterpret_cast<uint64_t>(Data_);
|
pid_t child = Pid_;
|
||||||
pid_t child = static_cast<pid_t>(pid);
|
|
||||||
while (wait(&status) != child)
|
while (wait(&status) != child)
|
||||||
if (secondsToWait && errno == EINTR) {
|
if (secondsToWait && errno == EINTR) {
|
||||||
// Kill the child.
|
// Kill the child.
|
||||||
|
@ -291,15 +281,12 @@ Program::Wait(unsigned secondsToWait,
|
||||||
|
|
||||||
bool
|
bool
|
||||||
Program::Kill(std::string* ErrMsg) {
|
Program::Kill(std::string* ErrMsg) {
|
||||||
if (Data_ == 0) {
|
if (Pid_ == 0) {
|
||||||
MakeErrMsg(ErrMsg, "Process not started!");
|
MakeErrMsg(ErrMsg, "Process not started!");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t pid64 = reinterpret_cast<uint64_t>(Data_);
|
if (kill(Pid_, SIGKILL) != 0) {
|
||||||
pid_t pid = static_cast<pid_t>(pid64);
|
|
||||||
|
|
||||||
if (kill(pid, SIGKILL) != 0) {
|
|
||||||
MakeErrMsg(ErrMsg, "The process couldn't be killed!");
|
MakeErrMsg(ErrMsg, "The process couldn't be killed!");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,21 +25,6 @@
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
using namespace sys;
|
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.
|
// This function just uses the PATH environment variable to find the program.
|
||||||
Path
|
Path
|
||||||
Program::FindProgramByName(const std::string& progName) {
|
Program::FindProgramByName(const std::string& progName) {
|
||||||
|
@ -137,11 +122,6 @@ Program::Execute(const Path& path,
|
||||||
const Path** redirects,
|
const Path** redirects,
|
||||||
unsigned memoryLimit,
|
unsigned memoryLimit,
|
||||||
std::string* ErrMsg) {
|
std::string* ErrMsg) {
|
||||||
if (Data_) {
|
|
||||||
HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
|
|
||||||
CloseHandle(Data_);
|
|
||||||
Data_ = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!path.canExecute()) {
|
if (!path.canExecute()) {
|
||||||
if (ErrMsg)
|
if (ErrMsg)
|
||||||
|
@ -269,9 +249,10 @@ Program::Execute(const Path& path,
|
||||||
path.str() + "'");
|
path.str() + "'");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Data_ = reinterpret_cast<void*>(pi.hProcess);
|
Pid_ = pi.dwProcessId;
|
||||||
|
|
||||||
// Make sure these get closed no matter what.
|
// Make sure these get closed no matter what.
|
||||||
|
AutoHandle hProcess(pi.hProcess);
|
||||||
AutoHandle hThread(pi.hThread);
|
AutoHandle hThread(pi.hThread);
|
||||||
|
|
||||||
// Assign the process to a job if a memory limit is defined.
|
// Assign the process to a job if a memory limit is defined.
|
||||||
|
@ -305,12 +286,17 @@ Program::Execute(const Path& path,
|
||||||
int
|
int
|
||||||
Program::Wait(unsigned secondsToWait,
|
Program::Wait(unsigned secondsToWait,
|
||||||
std::string* ErrMsg) {
|
std::string* ErrMsg) {
|
||||||
if (Data_ == 0) {
|
if (Pid_ == 0) {
|
||||||
MakeErrMsg(ErrMsg, "Process not started!");
|
MakeErrMsg(ErrMsg, "Process not started!");
|
||||||
return -1;
|
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.
|
// Wait for the process to terminate.
|
||||||
DWORD millisecondsToWait = INFINITE;
|
DWORD millisecondsToWait = INFINITE;
|
||||||
|
@ -341,12 +327,18 @@ Program::Wait(unsigned secondsToWait,
|
||||||
|
|
||||||
bool
|
bool
|
||||||
Program::Kill(std::string* ErrMsg) {
|
Program::Kill(std::string* ErrMsg) {
|
||||||
if (Data_ == 0) {
|
if (Pid_ == 0) {
|
||||||
MakeErrMsg(ErrMsg, "Process not started!");
|
MakeErrMsg(ErrMsg, "Process not started!");
|
||||||
return true;
|
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) {
|
if (TerminateProcess(hProcess, 1) == 0) {
|
||||||
MakeErrMsg(ErrMsg, "The process couldn't be killed!");
|
MakeErrMsg(ErrMsg, "The process couldn't be killed!");
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Reference in New Issue