Commit adceadb9 authored by Peter Hutterer's avatar Peter Hutterer
Browse files

process: Terminate/Kill can use a common helper function



The only difference is the signal and the message, and we can probably live
without the message differentiation (since it throws an exception anyways).
Signed-off-by: Peter Hutterer's avatarPeter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: default avatarChase Douglas <chase.douglas@canonical.com>
parent c238cc31
...@@ -174,6 +174,7 @@ class Process { ...@@ -174,6 +174,7 @@ class Process {
/* Disable copy constructor, assignment operator */ /* Disable copy constructor, assignment operator */
Process(const Process&); Process(const Process&);
Process& operator=(const Process&); Process& operator=(const Process&);
bool KillSelf(int signal);
}; };
} // testing } // testing
......
...@@ -95,30 +95,15 @@ void xorg::testing::Process::Start(const std::string& program, ...) { ...@@ -95,30 +95,15 @@ void xorg::testing::Process::Start(const std::string& program, ...) {
va_end(list); /* Shouldn't get here */ va_end(list); /* Shouldn't get here */
} }
bool xorg::testing::Process::Terminate() {
if (d_->pid == -1) {
return false;
} else if (d_->pid == 0) {
/* Child */
throw std::runtime_error("Child process tried to terminate itself");
} else { /* Parent */
if (kill(d_->pid, SIGTERM) < 0) {
d_->pid = -1;
return false;
}
d_->pid = -1;
}
return true;
}
bool xorg::testing::Process::Kill() { bool xorg::testing::Process::KillSelf(int signal) {
if (d_->pid == -1) { if (d_->pid == -1) {
return false; return false;
} else if (d_->pid == 0) { } else if (d_->pid == 0) {
/* Child */ /* Child */
throw std::runtime_error("Child process tried to kill itself"); throw std::runtime_error("Child process tried to kill itself");
} else { /* Parent */ } else { /* Parent */
if (kill(d_->pid, SIGKILL) < 0) { if (kill(d_->pid, signal) < 0) {
d_->pid = -1; d_->pid = -1;
return false; return false;
} }
...@@ -127,6 +112,14 @@ bool xorg::testing::Process::Kill() { ...@@ -127,6 +112,14 @@ bool xorg::testing::Process::Kill() {
return true; return true;
} }
bool xorg::testing::Process::Terminate(void) {
return KillSelf(SIGTERM);
}
bool xorg::testing::Process::Kill(void) {
return KillSelf(SIGKILL);
}
void xorg::testing::Process::SetEnv(const std::string& name, void xorg::testing::Process::SetEnv(const std::string& name,
const std::string& value, bool overwrite) { const std::string& value, bool overwrite) {
if (setenv(name.c_str(), value.c_str(), overwrite) != 0) if (setenv(name.c_str(), value.c_str(), overwrite) != 0)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment