]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common_init: create common_init_daemonize
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Fri, 29 Apr 2011 18:17:48 +0000 (11:17 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Fri, 29 Apr 2011 18:29:03 +0000 (11:29 -0700)
Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/common/common_init.cc
src/common/common_init.h
src/msg/SimpleMessenger.cc

index d7ab959ef0fc260690e143ca4c05469a433b6fc8..54e8f9ae5e162e8916c98e54716360fbacb6b08b 100644 (file)
 
 #include "auth/AuthSupported.h"
 #include "auth/KeyRing.h"
+#include "common/DoutStreambuf.h"
+#include "common/Thread.h"
 #include "common/ceph_argparse.h"
+#include "common/ceph_crypto.h"
 #include "common/code_environment.h"
-#include "common/DoutStreambuf.h"
+#include "common/common_init.h"
+#include "common/config.h"
+#include "common/errno.h"
+#include "common/pidfile.h"
 #include "common/safe_io.h"
 #include "common/signal.h"
 #include "common/version.h"
-#include "common/config.h"
-#include "common/common_init.h"
-#include "common/errno.h"
-#include "common/ceph_crypto.h"
 #include "include/color.h"
 
 #include <errno.h>
@@ -190,3 +192,45 @@ void common_init(std::vector < const char* >& args,
 
   ceph::crypto::init();
 }
+
+static void pidfile_remove_void(void)
+{
+  pidfile_remove();
+}
+
+void common_init_daemonize(const md_config_t *conf)
+{
+  int num_threads = Thread::get_num_threads();
+  if (num_threads > 1) {
+    derr << "common_init_daemonize: BUG: there are " << num_threads - 1
+        << " child threads already started that will now die!" << dendl;
+    exit(1);
+  }
+
+  int ret = daemon(1, 0);
+  if (ret) {
+    ret = errno;
+    derr << "common_init_daemonize: BUG: daemon error: "
+        << cpp_strerror(ret) << dendl;
+    exit(1);
+  }
+
+  if (!conf->chdir.empty()) {
+    if (::chdir(conf->chdir.c_str())) {
+      int err = errno;
+      derr << "common_init_daemonize: failed to chdir to directory: '"
+          << conf->chdir << "': " << cpp_strerror(err) << dendl;
+    }
+  }
+
+  if (atexit(pidfile_remove_void)) {
+    derr << "common_init_daemonize: failed to set pidfile_remove function "
+        << "to run at exit." << dendl;
+  }
+
+  // move these things into observers.
+  pidfile_write(&g_conf);
+  dout_handle_daemonize(&g_conf);
+
+  dout(1) << "finished common_init_daemonize" << dendl;
+}
index bbf42d6e0f556372988c99e6a44268e134a44764..fa572ca252011fd0f585685c489fc182290b6147 100644 (file)
@@ -26,5 +26,6 @@ void complain_about_parse_errors(std::deque<std::string> *parse_errors);
 void common_init(std::vector < const char* >& args,
               uint32_t module_type, code_environment_t code_env, int flags);
 void output_ceph_version();
+void common_init_daemonize(const md_config_t *conf);
 
 #endif
index 26fbafce0e828b46eb20ad7ba649213657936bba..82555a42413c80fd8a2155a9f458ca85ab41e9bd 100644 (file)
@@ -25,6 +25,7 @@
 #include <sys/user.h>
 
 #include "common/config.h"
+#include "common/common_init.h"
 
 #include "messages/MGenericMessage.h"
 
@@ -35,7 +36,6 @@
 
 #include "common/Timer.h"
 #include "common/errno.h"
-#include "common/pidfile.h"
 #include "common/safe_io.h"
 
 #define DOUT_SUBSYS ms
@@ -2333,29 +2333,8 @@ int SimpleMessenger::start(bool daemonize, uint64_t nonce)
 
   lock.Unlock();
 
-  // daemonize?
   if (daemonize) {
-    int num_threads = Thread::get_num_threads();
-    if (num_threads > 1) {
-      derr << "messenger.start BUG: there are " << num_threads - 1
-           << " child threads already started that will now die!  call messenger.start() sooner."
-          << dendl;
-    }
-
-    int r = daemon(1, 0);
-    assert(r >= 0);
-    install_standard_sighandlers();
-    pidfile_write(&g_conf);
-    if (!g_conf.chdir.empty()) {
-      if (::chdir(g_conf.chdir.c_str())) {
-       int err = errno;
-       derr << "messenger.start: failed to chdir to directory: '"
-            << g_conf.chdir << "': " << cpp_strerror(err) << dendl;
-      }
-    }
-    dout_handle_daemonize(&g_conf);
-    dout(1) << "messenger.start daemonized" << dendl;
+    common_init_daemonize(&g_conf);
   }
 
   // go!
@@ -2652,7 +2631,6 @@ void SimpleMessenger::wait()
 
   dout(10) << "wait: done." << dendl;
   dout(1) << "shutdown complete." << dendl;
-  pidfile_remove();
   started = false;
   did_bind = false;
   my_type = -1;