[PATCH xorg-gtest 1/2] process: Terminate/Kill can use a common helper function

Peter Hutterer peter.hutterer at who-t.net
Wed Jul 11 17:24:37 PDT 2012


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 <peter.hutterer at who-t.net>
---
We could also just reduce this into one Kill(signal) function and just ask the
caller to provide SIGTERM/SIGKILL as argument.

 include/xorg/gtest/xorg-gtest-process.h |    1 +
 src/process.cpp                         |   27 ++++++++++-----------------
 2 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/include/xorg/gtest/xorg-gtest-process.h b/include/xorg/gtest/xorg-gtest-process.h
index 0b721ce..e8c8d14 100644
--- a/include/xorg/gtest/xorg-gtest-process.h
+++ b/include/xorg/gtest/xorg-gtest-process.h
@@ -174,6 +174,7 @@ class Process {
   /* Disable copy constructor, assignment operator */
   Process(const Process&);
   Process& operator=(const Process&);
+  bool KillSelf(int signal);
 };
 
 } // testing
diff --git a/src/process.cpp b/src/process.cpp
index 7b60afc..be3bbda 100644
--- a/src/process.cpp
+++ b/src/process.cpp
@@ -93,30 +93,15 @@ void xorg::testing::Process::Start(const std::string& program, ...) {
   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) {
     return false;
   } else if (d_->pid == 0) {
     /* Child */
     throw std::runtime_error("Child process tried to kill itself");
   } else { /* Parent */
-    if (kill(d_->pid, SIGKILL) < 0) {
+    if (kill(d_->pid, signal) < 0) {
       d_->pid = -1;
       return false;
     }
@@ -125,6 +110,14 @@ bool xorg::testing::Process::Kill() {
   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,
                                     const std::string& value, bool overwrite) {
   if (setenv(name.c_str(), value.c_str(), overwrite) != 0)
-- 
1.7.10.4



More information about the xorg-devel mailing list