From: Sage Weil Date: Fri, 27 Feb 2009 18:57:24 +0000 (-0800) Subject: config: optionally maintain a pid file X-Git-Tag: v0.7~119 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=c2070b4cd3f9761895c110cdd2e7370f414d5a8f;p=ceph.git config: optionally maintain a pid file Remove it when we shut down _only_ if it matches getpid(). --- diff --git a/src/config.cc b/src/config.cc index 64ded2717680..b8d447f1d25f 100644 --- a/src/config.cc +++ b/src/config.cc @@ -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& 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& 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]; diff --git a/src/config.h b/src/config.h index 368c4d14d8d4..15488e95715b 100644 --- a/src/config.h +++ b/src/config.h @@ -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; diff --git a/src/msg/SimpleMessenger.cc b/src/msg/SimpleMessenger.cc index 25d7088be0f6..741ce79d1019 100644 --- a/src/msg/SimpleMessenger.cc +++ b/src/msg/SimpleMessenger.cc @@ -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; }