]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
Preforker: Add child process exit status check
authorHaomai Wang <haomaiwang@gmail.com>
Fri, 1 May 2015 05:02:37 +0000 (13:02 +0800)
committerHaomai Wang <haomaiwang@gmail.com>
Fri, 1 May 2015 07:05:23 +0000 (15:05 +0800)
Signed-off-by: Haomai Wang <haomaiwang@gmail.com>
src/common/Preforker.h

index 72f992435dbae216f53e5ffa3690b3f88507c26e..5970700df1f2ec11a7ac694681321df878dd40d2 100644 (file)
@@ -12,6 +12,9 @@
 #ifdef WITH_LTTNG
 #include <lttng/ust.h>
 #endif
+#include <sstream>
+#include <string>
+
 #include "include/assert.h"
 #include "common/safe_io.h"
 #include "common/errno.h"
@@ -39,12 +42,14 @@ public:
       forked(false)
   {}
 
-  void prefork() {
+  int prefork(std::string &err) {
     assert(!forked);
     int r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
     if (r < 0) {
-      cerr << "[" << getpid() << "]: unable to create socketpair: " << cpp_strerror(errno) << std::endl;
-      exit(errno);
+      std::ostringstream oss;
+      oss << "[" << getpid() << "]: unable to create socketpair: " << cpp_strerror(errno);
+      err = oss.str();
+      return r;
     }
 
 #ifdef WITH_LTTNG
@@ -54,11 +59,18 @@ public:
     forked = true;
 
     childpid = fork();
+    if (childpid < 0) {
+      r = -errno;
+      oss << "[" << getpid() << "]: unable to fork: " << cpp_strerror(errno);
+      err = oss.str();
+      return r;
+    }
     if (childpid == 0) {
       child_after_fork();
     } else {
       parent_after_fork();
     }
+    return 0;
   }
 
   bool is_child() {
@@ -69,10 +81,11 @@ public:
     return childpid != 0;
   }
 
-  int parent_wait() {
+  int parent_wait(std::string &err) {
     assert(forked);
 
     int r = -1;
+    std::ostringstream oss;
     int err = safe_read_exact(fd[0], &r, sizeof(r));
     if (err == 0 && r == -1) {
       // daemonize
@@ -81,12 +94,25 @@ public:
       ::close(2);
       r = 0;
     } else if (err) {
-      cerr << "[" << getpid() << "]: " << cpp_strerror(err) << std::endl;
+      oss << "[" << getpid() << "]: " << cpp_strerror(err);
     } else {
       // wait for child to exit
-      r = waitpid(childpid, NULL, 0);
+      int status;
+      err = waitpid(childpid, &status, 0);
+      if (err < 0) {
+        oss << "[" << getpid() << "]" << " waitpid error: " << cpp_strerror(err);
+      } else if (WIFSIGNALED(status)) {
+        oss << "[" << getpid() << "]" << " exited with a signal";
+      } else if (!WIFEXITED(status)) {
+        oss << "[" << getpid() << "]" << " did not exit normally";
+      } else {
+        err = WEXITSTATUS(status);
+        if (err != 0)
+         oss << "[" << getpid() << "]" << " returned exit_status " << cpp_strerror(err);
+      }
     }
-    return r;
+    err = oss.str();
+    return err;
   }
 
   int signal_exit(int r) {