]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
config: optionally maintain a pid file
authorSage Weil <sage@newdream.net>
Fri, 27 Feb 2009 18:57:24 +0000 (10:57 -0800)
committerSage Weil <sage@newdream.net>
Fri, 27 Feb 2009 18:57:24 +0000 (10:57 -0800)
Remove it when we shut down _only_ if it matches getpid().

src/config.cc
src/config.h
src/msg/SimpleMessenger.cc

index 64ded27176807eeaea0aa3694d743c8112130188..b8d447f1d25f9145143e861fb0651d67a3b56bc4 100644 (file)
@@ -211,6 +211,8 @@ md_config_t g_conf = {
   dout_dir: INSTALL_PREFIX "/var/log/ceph",        // if daemonize == true
   dout_sym_dir: INSTALL_PREFIX "/var/log/ceph",    // if daemonize == true
   logger_dir: INSTALL_PREFIX "/var/log/ceph/stat",
+  pid_file: 0,
+
   conf_file: INSTALL_PREFIX "/etc/ceph/ceph.conf",
 
   dump_conf: false,
@@ -252,7 +254,7 @@ md_config_t g_conf = {
   debug_after: 0,
   
   // -- misc --
-  use_abspaths: false,      // make monitorstore et al use absolute path (to workaround FUSE chdir("/"))
+  use_abspaths: false, // make monitorstore et al use absolute path (to workaround FUSE chdir("/"))
 
   // --- clock ---
   clock_lock: false,
@@ -638,6 +640,7 @@ void parse_config_file(ConfFile *cf, bool auto_update)
   CF_READ_STR("global", "dout dir", dout_dir);
   CF_READ_STR("global", "dout sym dir", dout_sym_dir);
   CF_READ_STR("global", "logger dir", logger_dir);
+  CF_READ_STR("global", "pid file", pid_file);
 
   CF_READ("debug", "debug", debug);
   CF_READ("debug", "lockdep", debug_lockdep);
@@ -939,8 +942,7 @@ void parse_config_options(std::vector<const char*>& args, bool open)
     //else if (strcmp(args[i], "--fake_osd_sync") == 0) 
     //g_conf.fake_osd_sync = atoi(args[++i]);
 
-    
-    
+        
     else if (strcmp(args[i], "--dout_dir") == 0 && isarg) 
       g_conf.dout_dir = args[++i];
     else if (//strcmp(args[i], "-o") == 0 ||
@@ -948,6 +950,9 @@ void parse_config_options(std::vector<const char*>& args, bool open)
       g_conf.dout_sym_dir = args[++i];
     else if (strcmp(args[i], "--logger_dir") == 0 && isarg) 
       g_conf.logger_dir = args[++i];
+    else if ((strcmp(args[i], "--pid_file") == 0 ||
+             strcmp(args[i], "-p") == 0) && isarg) 
+      g_conf.pid_file = args[++i];
     else if (strcmp(args[i], "--conf_file") == 0 && isarg) 
       g_conf.conf_file = args[++i];
 
index 368c4d14d8d4e8c1c2fd50db57a5093e02070f0b..15488e95715b6712a0e5dbebf4b76a0f4b26e85a 100644 (file)
@@ -64,6 +64,7 @@ struct md_config_t {
   const char *dout_dir;
   const char *dout_sym_dir;
   const char *logger_dir;
+  const char *pid_file;
 
   const char *conf_file;
   bool dump_conf;
index 25d7088be0f6e4bec9372f5c820d0fe26946d41f..741ce79d1019a2692e2fdb798b1e1e112595c9aa 100644 (file)
@@ -336,6 +336,39 @@ class C_Debug : public Context {
   }
 };
 
+
+static void write_pid_file(int pid)
+{
+  if (!g_conf.pid_file)
+    return;
+
+  int fd = ::open(g_conf.pid_file, O_CREAT|O_TRUNC|O_WRONLY, 0644);
+  if (fd >= 0) {
+    char buf[20];
+    int len = sprintf(buf, "%d\n", pid);
+    ::write(fd, buf, len);
+    ::close(fd);
+  }
+}
+
+static void remove_pid_file()
+{
+  if (!g_conf.pid_file)
+    return;
+
+  // only remove it if it has OUR pid in it!
+  int fd = ::open(g_conf.pid_file, O_RDONLY);
+  if (fd >= 0) {
+    char actual[20], correct[20];
+    sprintf(correct, "%d\n", getpid());
+    ::read(fd, actual, 20);
+    ::close(fd);
+
+    if (strncmp(actual, correct, 20) == 0)
+      ::unlink(g_conf.pid_file);
+  }
+}
+
 int Rank::start(bool nodaemon)
 {
   // register at least one entity, first!
@@ -360,8 +393,20 @@ int Rank::start(bool nodaemon)
              << dendl;
     }
     dout(1) << "rank.start daemonizing" << dendl;
-    daemon(1, 0);  /* fixme.. we should chdir(/) too! */
+
+    // be polite
+    if (g_conf.use_abspaths)
+      ::chdir("/");
+
+    pid_t child = fork();
+    if (child) {
+      write_pid_file(child);
+      _exit(0);
+    }
+
     _dout_rename_output_file();
+  } else {
+    write_pid_file(getpid());
   }
 
   // some debug hackery?
@@ -595,6 +640,7 @@ void Rank::wait()
 
   dout(10) << "wait: done." << dendl;
   dout(1) << "shutdown complete." << dendl;
+  remove_pid_file();
   started = false;
   my_type = -1;
 }