From: Haomai Wang Date: Fri, 1 May 2015 05:02:37 +0000 (+0800) Subject: Preforker: Add child process exit status check X-Git-Tag: v9.0.1~26^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=9e1514cff2b528e855febfd7bf89f7e62a865928;p=ceph.git Preforker: Add child process exit status check Signed-off-by: Haomai Wang --- diff --git a/src/common/Preforker.h b/src/common/Preforker.h index 72f992435dba..5970700df1f2 100644 --- a/src/common/Preforker.h +++ b/src/common/Preforker.h @@ -12,6 +12,9 @@ #ifdef WITH_LTTNG #include #endif +#include +#include + #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) {