[PATCH xorg-gtest 3/8] process: on termination, check if the process exited and set the error code

Peter Hutterer peter.hutterer at who-t.net
Tue Oct 9 22:14:28 PDT 2012


This changes the meaning of Process::TERMINATED to "currently in termination
but we're not sure what happened to it"

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 include/xorg/gtest/xorg-gtest-process.h |  5 ++++-
 src/process.cpp                         | 17 ++++++++++++-----
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/include/xorg/gtest/xorg-gtest-process.h b/include/xorg/gtest/xorg-gtest-process.h
index f1fc0ec..8c581db 100644
--- a/include/xorg/gtest/xorg-gtest-process.h
+++ b/include/xorg/gtest/xorg-gtest-process.h
@@ -70,6 +70,8 @@ class Process {
     * * A process in state ERROR or NONE will fail to Kill() or Terminate()
     * * A process in state FINISHED_SUCCESS or FINISHED_FAILURE will always
     * succeed to Kill() or Terminate()
+    * * A process in state TERMINATED may change state to FINISHED_SUCCESS
+    * or FINISHED_FAILURE when queried again.
     */
    enum State {
      ERROR,             /**< An error has occured, state is now unknown */
@@ -77,7 +79,8 @@ class Process {
      RUNNING,           /**< The process has been started */
      FINISHED_SUCCESS,  /**< The process finished with an exit code of 0 */
      FINISHED_FAILURE,  /**< The process finished with a non-zero exit code */
-     TERMINATED,        /**< The process was successfully terminated by this library */
+     TERMINATED,        /**< The process was successfully terminated by this
+                             library but it's state is currently unknown */
    };
 
   /**
diff --git a/src/process.cpp b/src/process.cpp
index a9c041e..0b0ddc3 100644
--- a/src/process.cpp
+++ b/src/process.cpp
@@ -131,9 +131,15 @@ bool xorg::testing::Process::WaitForExit(unsigned int timeout) {
     int status;
     int pid = waitpid(Pid(), &status, WNOHANG);
 
-    if (pid == Pid())
-      return true;
-    else if (pid == -1)
+    if (pid == Pid()) {
+      if (WIFEXITED(status)) {
+        d_->state = WEXITSTATUS(status) ? FINISHED_FAILURE : FINISHED_SUCCESS;
+        return true;
+      } else if (WIFSIGNALED(status)) {
+        d_->state = FINISHED_FAILURE;
+        return true;
+      }
+    } else if (pid == -1)
       return errno == ECHILD;
 
       usleep(10);
@@ -171,8 +177,9 @@ bool xorg::testing::Process::KillSelf(int signal, unsigned int timeout) {
       bool wait_success = true;
 
       wait_success = WaitForExit(timeout);
-      if (!wait_success)
-        return false;
+      if (wait_success)
+        d_->pid = -1;
+      return wait_success;
     }
     d_->pid = -1;
   }
-- 
1.7.11.4



More information about the xorg-devel mailing list