Add a Kill() function to the Program class.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@81246 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Mikhail Glushenkov 2009-09-08 19:50:27 +00:00
parent 8e371aa7b6
commit a607202a68
3 changed files with 44 additions and 12 deletions

View File

@ -30,7 +30,7 @@ namespace sys {
/// @brief An abstraction for finding and executing programs.
class Program {
/// Opaque handle for target specific data.
void *Data;
void *Data;
unsigned Pid_;
@ -43,8 +43,8 @@ namespace sys {
public:
Program();
~Program();
~Program();
/// Return process ID of this program.
unsigned GetPid() { return Pid_; }
@ -103,6 +103,17 @@ namespace sys {
///< program.
);
/// This function terminates the program.
/// @returns true if an error occured.
/// @see Execute
/// @brief Terminates the program.
bool Kill
( std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
///< instance in which error messages will be returned. If the string
///< is non-empty upon return an error occurred while invoking the
///< program.
);
/// This static constructor (factory) will attempt to locate a program in
/// the operating system's file system using some pre-determined set of
/// locations to search (e.g. the PATH on Unix).

View File

@ -283,6 +283,16 @@ Program::Wait(unsigned secondsToWait,
}
bool
Program::Kill(std::string* ErrMsg) {
if (Pid_ == 0) {
MakeErrMsg(ErrMsg, "Process not started!");
return true;
}
return (kill(Pid_, SIGKILL) == 0);
}
bool Program::ChangeStdinToBinary(){
// Do nothing, as Unix doesn't differentiate between text and binary.
return false;

View File

@ -28,11 +28,11 @@ using namespace sys;
Program::Program() : Pid_(0), Data(0) {}
Program::~Program() {
if (Data) {
HANDLE hProcess = (HANDLE) Data;
CloseHandle(hProcess);
Data = 0;
}
if (Data) {
HANDLE hProcess = (HANDLE) Data;
CloseHandle(hProcess);
Data = 0;
}
}
// This function just uses the PATH environment variable to find the program.
@ -121,7 +121,7 @@ static HANDLE RedirectIO(const Path *path, int fd, std::string* ErrMsg) {
/// ArgNeedsQuotes - Check whether argument needs to be quoted when calling
/// CreateProcess.
static bool ArgNeedsQuotes(const char *Str) {
static bool ArgNeedsQuotes(const char *Str) {
return Str[0] == '\0' || strchr(Str, ' ') != 0;
}
@ -137,7 +137,7 @@ Program::Execute(const Path& path,
CloseHandle(Data);
Data = 0;
}
if (!path.canExecute()) {
if (ErrMsg)
*ErrMsg = "program not executable";
@ -266,7 +266,7 @@ Program::Execute(const Path& path,
}
Pid_ = pi.dwProcessId;
Data = pi.hProcess;
// Make sure these get closed no matter what.
AutoHandle hThread(pi.hThread);
@ -307,7 +307,7 @@ Program::Wait(unsigned secondsToWait,
}
HANDLE hProcess = (HANDLE) Data;
// Wait for the process to terminate.
DWORD millisecondsToWait = INFINITE;
if (secondsToWait > 0)
@ -335,6 +335,17 @@ Program::Wait(unsigned secondsToWait,
return status;
}
bool
Program::Kill(std::string* ErrMsg) {
if (Data == 0) {
MakeErrMsg(ErrMsg, "Process not started!");
return true;
}
HANDLE hProcess = reinterpret_cast<HANDLE>(Data);
return TerminateProcess(hProcess, 1);
}
bool Program::ChangeStdinToBinary(){
int result = _setmode( _fileno(stdin), _O_BINARY );
return result == -1;