forked from OSchip/llvm-project
Add a Kill() function to the Program class.
llvm-svn: 81246
This commit is contained in:
parent
b17a190186
commit
4a91b7605b
|
@ -30,7 +30,7 @@ namespace sys {
|
||||||
/// @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.
|
/// Opaque handle for target specific data.
|
||||||
void *Data;
|
void *Data;
|
||||||
|
|
||||||
unsigned Pid_;
|
unsigned Pid_;
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ namespace sys {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Program();
|
Program();
|
||||||
~Program();
|
~Program();
|
||||||
|
|
||||||
/// Return process ID of this program.
|
/// Return process ID of this program.
|
||||||
unsigned GetPid() { return Pid_; }
|
unsigned GetPid() { return Pid_; }
|
||||||
|
@ -103,6 +103,17 @@ namespace sys {
|
||||||
///< program.
|
///< 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
|
/// This static constructor (factory) will attempt to locate a program in
|
||||||
/// the operating system's file system using some pre-determined set of
|
/// the operating system's file system using some pre-determined set of
|
||||||
/// locations to search (e.g. the PATH on Unix).
|
/// locations to search (e.g. the PATH on Unix).
|
||||||
|
|
|
@ -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(){
|
bool Program::ChangeStdinToBinary(){
|
||||||
// Do nothing, as Unix doesn't differentiate between text and binary.
|
// Do nothing, as Unix doesn't differentiate between text and binary.
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -28,11 +28,11 @@ using namespace sys;
|
||||||
Program::Program() : Pid_(0), Data(0) {}
|
Program::Program() : Pid_(0), Data(0) {}
|
||||||
|
|
||||||
Program::~Program() {
|
Program::~Program() {
|
||||||
if (Data) {
|
if (Data) {
|
||||||
HANDLE hProcess = (HANDLE) Data;
|
HANDLE hProcess = (HANDLE) Data;
|
||||||
CloseHandle(hProcess);
|
CloseHandle(hProcess);
|
||||||
Data = 0;
|
Data = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function just uses the PATH environment variable to find the program.
|
// This function just uses the PATH environment variable to find the program.
|
||||||
|
@ -335,6 +335,17 @@ Program::Wait(unsigned secondsToWait,
|
||||||
return status;
|
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(){
|
bool Program::ChangeStdinToBinary(){
|
||||||
int result = _setmode( _fileno(stdin), _O_BINARY );
|
int result = _setmode( _fileno(stdin), _O_BINARY );
|
||||||
return result == -1;
|
return result == -1;
|
||||||
|
|
Loading…
Reference in New Issue