]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
systest_runnable: Use Preforker to do fork
authorHaomai Wang <haomaiwang@gmail.com>
Fri, 1 May 2015 03:59:07 +0000 (11:59 +0800)
committerHaomai Wang <haomaiwang@gmail.com>
Fri, 1 May 2015 04:55:09 +0000 (12:55 +0800)
Signed-off-by: Haomai Wang <haomaiwang@gmail.com>
src/common/Preforker.h
src/test/system/systest_runnable.cc
src/test/system/systest_runnable.h

index ea840597f5a511445baa27105e25e77adf61b709..72f992435dbae216f53e5ffa3690b3f88507c26e 100644 (file)
@@ -7,10 +7,12 @@
 #include <sys/socket.h>
 #include <sys/wait.h>
 #include <errno.h>
+#include <stdlib.h>
 #include <unistd.h>
 #ifdef WITH_LTTNG
 #include <lttng/ust.h>
 #endif
+#include "include/assert.h"
 #include "common/safe_io.h"
 #include "common/errno.h"
 
@@ -82,7 +84,7 @@ public:
       cerr << "[" << getpid() << "]: " << cpp_strerror(err) << std::endl;
     } else {
       // wait for child to exit
-      waitpid(childpid, NULL, 0);
+      r = waitpid(childpid, NULL, 0);
     }
     return r;
   }
index 55cab0aef9dd29d3d4850798b164c62b092e07e8..32fb36b59872116ff9150af256135e56a8b186f5 100644 (file)
@@ -58,7 +58,6 @@ SysTestRunnable(int argc, const char **argv)
   m_started = false;
   m_id = m_highest_id.inc();
   memset(&m_pthread, 0, sizeof(m_pthread));
-  m_pid = 0;
   update_id_str(false);
   set_argv(argc, argv);
 }
@@ -91,20 +90,14 @@ start()
     return 0;
   }
   else {
-    pid_t pid = fork();
-    if (pid == -1) {
-      int err = errno;
-      return -err;
-    }
-    else if (pid == 0) {
+    prefork.prefork();
+
+    if (prefork.is_child()) {
       m_started = true;
-      m_pid = getpid();
       void *retptr = systest_runnable_pthread_helper(static_cast<void*>(this));
-      exit((int)(uintptr_t)retptr);
-    }
-    else {
+      prefork.exit((int)(uintptr_t)retptr);
+    } else {
       m_started = true;
-      m_pid = pid;
       return 0;
     }
   }
@@ -116,10 +109,11 @@ join()
   if (!m_started) {
     return "SysTestRunnable was never started.";
   }
+  int ret;
   bool use_threads = SysTestSettings::inst().use_threads();
   if (use_threads) {
     void *ptrretval;
-    int ret = pthread_join(m_pthread, &ptrretval);
+    ret = pthread_join(m_pthread, &ptrretval);
     if (ret) {
       ostringstream oss;
       oss << "pthread_join failed with error " << ret;
@@ -132,34 +126,13 @@ join()
       return oss.str();
     }
     return "";
-  }
-  else {
-    int status;
-    printf("waitpid(%d)\n", m_pid);
-    pid_t pid = waitpid(m_pid, &status, 0);
-    if (pid == -1) {
-      int err = errno;
+  } else {
+    ret = preforker.parent_wait();
+    if (ret < 0) {
       ostringstream oss;
       oss << get_id_str() << " waitpid error: " << cpp_strerror(err);
       return oss.str();
-    }
-    else if (WIFSIGNALED(status)) {
-      ostringstream oss;
-      oss << get_id_str() << " exited with a signal";
-      return oss.str();
-    }
-    else if (!WIFEXITED(status)) {
-      ostringstream oss;
-      oss << get_id_str() << " did not exit normally";
-      return oss.str();
-    }
-    else {
-      int exit_status = WEXITSTATUS(status);
-      if (exit_status != 0) {
-       ostringstream oss;
-       oss << get_id_str() << " returned exit_status " << exit_status;
-       return oss.str();
-      }
+    } else {
       return "";
     }
   }
index 2164fa78f9ce41fcdcf8dec33df78fed1c4bf440..bd7d2586bc31c180b35305af0c767527f40afdfb 100644 (file)
@@ -20,6 +20,8 @@
 #include <string>
 #include <vector>
 
+#include "common/Preforker.h"
+
 #define RETURN1_IF_NOT_VAL(expected, expr) \
   do {\
     int _rinv_ret = expr;\
@@ -77,11 +79,11 @@ private:
 
   friend void* systest_runnable_pthread_helper(void *arg);
 
+  Preforker preforker;
   const char **m_argv_orig;
   bool m_started;
   int m_id;
   pthread_t m_pthread;
-  int m_pid;
   char m_id_str[ID_STR_SZ];
 };