From 63bbadddb0b688fc4af11fc2483cff8e722b4c82 Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Thu, 16 Jun 2011 20:15:24 -0700 Subject: [PATCH] move common_init to global_init Signed-off-by: Colin McCabe --- src/Makefile.am | 3 +- src/cauthtool.cc | 4 +- src/cconf.cc | 4 +- src/cfuse.cc | 4 +- src/cmds.cc | 8 +- src/cmon.cc | 8 +- src/common/ceph_argparse.cc | 1 + src/common/common_init.cc | 167 -------------------------- src/common/common_init.h | 20 +++- src/common/config.cc | 2 +- src/common/config.h | 2 +- src/common/debug.cc | 17 --- src/cosd.cc | 10 +- src/crushtool.cc | 2 +- src/csyn.cc | 4 +- src/dupstore.cc | 4 +- src/global/global_init.cc | 200 +++++++++++++++++++++++++++++++ src/global/global_init.h | 34 ++++++ src/librados-config.cc | 4 +- src/librados.cc | 2 +- src/monmaptool.cc | 4 +- src/msg/SimpleMessenger.cc | 2 +- src/osdc/rados_bencher.h | 2 +- src/osdmaptool.cc | 4 +- src/rados.cc | 4 +- src/rados_sync.cc | 2 +- src/rbd.cc | 4 +- src/rgw/rgw_admin.cc | 4 +- src/rgw/rgw_main.cc | 4 +- src/streamtest.cc | 4 +- src/test/TestDoutStreambuf.cc | 4 +- src/test/TestSignalHandlers.cc | 4 +- src/test/TestTimers.cc | 4 +- src/test/cli/cconf/env-vs-args.t | 4 +- src/test/test_mutate.cc | 4 +- src/test_trans.cc | 4 +- src/testkeys.cc | 4 +- src/testmsgr.cc | 4 +- src/tools/ceph.cc | 4 +- src/tools/common.cc | 2 +- src/tools/gceph.cc | 4 +- src/tools/gui.h | 2 +- 42 files changed, 319 insertions(+), 259 deletions(-) delete mode 100644 src/common/debug.cc create mode 100644 src/global/global_init.cc create mode 100644 src/global/global_init.h diff --git a/src/Makefile.am b/src/Makefile.am index 932b128643e82..6b12a38e513f0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -643,6 +643,7 @@ libcommon_files = \ osd/OSDMap.cc \ mds/MDSMap.cc \ common/common_init.cc \ + global/global_init.cc \ common/ceph_argparse.cc \ common/ceph_context.cc \ common/buffer.cc \ @@ -660,7 +661,6 @@ libcommon_files = \ common/page.cc \ common/lockdep.cc \ common/DoutStreambuf.cc \ - common/debug.cc \ common/version.cc \ common/hex.cc \ common/entity_name.cc \ @@ -816,6 +816,7 @@ noinst_HEADERS = \ common/Timer.h\ common/arch.h\ common/armor.h\ + global/global_init.h \ common/common_init.h\ common/code_environment.h \ common/signal.h\ diff --git a/src/cauthtool.cc b/src/cauthtool.cc index 333f8812dec46..3da26734fe9f3 100644 --- a/src/cauthtool.cc +++ b/src/cauthtool.cc @@ -18,7 +18,7 @@ using namespace std; #include "common/ConfUtils.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "auth/Crypto.h" #include "auth/Auth.h" #include "auth/KeyRing.h" @@ -53,7 +53,7 @@ int main(int argc, const char **argv) env_to_vec(args); DEFINE_CONF_VARS(usage); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, CINIT_FLAG_NO_DEFAULT_CONFIG_FILE); common_init_finish(&g_ceph_context); EntityName ename(g_conf->name); diff --git a/src/cconf.cc b/src/cconf.cc index ccf72c7fc33b1..357864d2a9b0e 100644 --- a/src/cconf.cc +++ b/src/cconf.cc @@ -20,7 +20,7 @@ #include "mon/AuthMonitor.h" #include "common/ConfUtils.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/entity_name.h" #include "common/ceph_argparse.h" #include "common/config.h" @@ -121,7 +121,7 @@ int main(int argc, const char **argv) argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); std::string val; diff --git a/src/cfuse.cc b/src/cfuse.cc index 9b80df0c5d327..cf0db88027715 100644 --- a/src/cfuse.cc +++ b/src/cfuse.cc @@ -28,7 +28,7 @@ using namespace std; #include "common/Timer.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/safe_io.h" #ifndef DARWIN @@ -52,7 +52,7 @@ int main(int argc, const char **argv, const char *envp[]) { argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_DAEMON, + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_DAEMON, CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS); vector nargs; diff --git a/src/cmds.cc b/src/cmds.cc index 3f7f3e05e00e4..898f8487bfa55 100644 --- a/src/cmds.cc +++ b/src/cmds.cc @@ -32,7 +32,7 @@ using namespace std; #include "msg/SimpleMessenger.h" #include "common/Timer.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/ceph_argparse.h" #include "mon/MonClient.h" @@ -125,7 +125,7 @@ int main(int argc, const char **argv) argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_MDS, CODE_ENVIRONMENT_DAEMON, 0); + global_init(args, CEPH_ENTITY_TYPE_MDS, CODE_ENVIRONMENT_DAEMON, 0); // mds specific args int shadow = 0; @@ -217,14 +217,14 @@ int main(int argc, const char **argv) SimpleMessenger::Policy::stateful_server(supported, 0)); if (shadow != MDSMap::STATE_ONESHOT_REPLAY) - common_init_daemonize(&g_ceph_context, 0); + global_init_daemonize(&g_ceph_context, 0); common_init_finish(&g_ceph_context); // get monmap MonClient mc(&g_ceph_context); if (mc.build_initial_monmap() < 0) return -1; - common_init_chdir(&g_ceph_context); + global_init_chdir(&g_ceph_context); messenger->start(); diff --git a/src/cmon.cc b/src/cmon.cc index fe53d4a949286..396d7a5a2e678 100644 --- a/src/cmon.cc +++ b/src/cmon.cc @@ -33,7 +33,7 @@ using namespace std; #include "common/ceph_argparse.h" #include "common/Timer.h" -#include "common/common_init.h" +#include "global/global_init.h" extern const CompatSet::Feature ceph_mon_feature_compat[]; extern const CompatSet::Feature ceph_mon_feature_ro_compat[]; @@ -62,7 +62,7 @@ int main(int argc, const char **argv) argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_MON, CODE_ENVIRONMENT_DAEMON, 0); + global_init(args, CEPH_ENTITY_TYPE_MON, CODE_ENVIRONMENT_DAEMON, 0); FOR_EACH_ARG(args) { if (CEPH_ARGPARSE_EQ("mkfs", '\0')) { @@ -268,9 +268,9 @@ int main(int argc, const char **argv) messenger->set_default_send_priority(CEPH_MSG_PRIO_HIGH); Monitor *mon = new Monitor(&g_ceph_context, g_conf->name.get_id(), &store, messenger, &monmap); - common_init_daemonize(&g_ceph_context, 0); + global_init_daemonize(&g_ceph_context, 0); common_init_finish(&g_ceph_context); - common_init_chdir(&g_ceph_context); + global_init_chdir(&g_ceph_context); messenger->start(); uint64_t supported = diff --git a/src/common/ceph_argparse.cc b/src/common/ceph_argparse.cc index 6f13ffb6988b3..e971828391ca2 100644 --- a/src/common/ceph_argparse.cc +++ b/src/common/ceph_argparse.cc @@ -15,6 +15,7 @@ #include "auth/Auth.h" #include "common/ceph_argparse.h" #include "common/common_init.h" +#include "global/global_init.h" #include "common/ConfUtils.h" #include "common/version.h" #include "common/config.h" diff --git a/src/common/common_init.cc b/src/common/common_init.cc index da6839a016b20..551fd9f0c2e16 100644 --- a/src/common/common_init.cc +++ b/src/common/common_init.cc @@ -13,24 +13,19 @@ */ #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/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 "include/color.h" -#include "common/Thread.h" -#include "common/pidfile.h" #include #include -#include #define _STR(x) #x #define STRINGIFY(x) _STR(x) @@ -87,168 +82,6 @@ void complain_about_parse_errors(std::deque *parse_errors) } } -void common_init(std::vector < const char* >& args, - uint32_t module_type, code_environment_t code_env, int flags) -{ - CephInitParameters iparams = - ceph_argparse_early_args(args, module_type, flags); - CephContext *cct = common_preinit(iparams, code_env, flags); - md_config_t *conf = cct->_conf; - - std::deque parse_errors; - int ret = conf->parse_config_files(iparams.get_conf_files(), &parse_errors); - if (ret == -EDOM) { - dout_emergency("common_init: error parsing config file.\n"); - _exit(1); - } - else if (ret == -EINVAL) { - if (!(flags & CINIT_FLAG_NO_DEFAULT_CONFIG_FILE)) { - dout_emergency("common_init: unable to open config file.\n"); - _exit(1); - } - } - else if (ret) { - dout_emergency("common_init: error reading config file.\n"); - _exit(1); - } - - conf->parse_env(); // environment variables override - - conf->parse_argv(args); // argv override - - if (code_env == CODE_ENVIRONMENT_DAEMON) { - if (conf->log_dir.empty() && conf->log_file.empty()) { - conf->set_val_or_die("log_file", "/var/log/ceph/$name.log"); - } - } - - // Expand metavariables. Invoke configuration observers. - conf->apply_changes(); - - // Now we're ready to complain about config file parse errors - complain_about_parse_errors(&parse_errors); - - // signal stuff - int siglist[] = { SIGPIPE, 0 }; - block_signals(siglist, NULL); - install_standard_sighandlers(); - - if (code_env == CODE_ENVIRONMENT_DAEMON) { - cout << TEXT_YELLOW - << " ** WARNING: Ceph is still under development. Any feedback can be directed **" - << TEXT_NORMAL << "\n" << TEXT_YELLOW - << " ** at ceph-devel@vger.kernel.org or http://ceph.newdream.net/. **" - << TEXT_NORMAL << std::endl; - output_ceph_version(); - } - if (g_lockdep) { - cout << TEXT_YELLOW << "*** lockdep is enabled (" << g_lockdep - << ") ***" << TEXT_NORMAL << std::endl; - lockdep_register_ceph_context(cct); - } -} - -static void pidfile_remove_void(void) -{ - pidfile_remove(); -} - -/* Map stderr to /dev/null. This isn't really re-entrant; we rely on the old unix - * behavior that the file descriptor that gets assigned is the lowest - * available one. - */ -int common_init_shutdown_stderr(CephContext *cct) -{ - TEMP_FAILURE_RETRY(close(STDERR_FILENO)); - if (open("/dev/null", O_RDONLY) < 0) { - int err = errno; - derr << "common_init_shutdown_stderr: open(/dev/null) failed: error " - << err << dendl; - return 1; - } - cct->_doss->handle_stderr_shutdown(); - return 0; -} - -void common_init_daemonize(CephContext *cct, int flags) -{ - if (g_code_env != CODE_ENVIRONMENT_DAEMON) - return; - const md_config_t *conf = cct->_conf; - if (!conf->daemonize) - return; - 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, 1); - if (ret) { - ret = errno; - derr << "common_init_daemonize: BUG: daemon error: " - << cpp_strerror(ret) << dendl; - exit(1); - } - - if (atexit(pidfile_remove_void)) { - derr << "common_init_daemonize: failed to set pidfile_remove function " - << "to run at exit." << dendl; - } - - /* This is the old trick where we make file descriptors 0, 1, and possibly 2 - * point to /dev/null. - * - * We have to do this because otherwise some arbitrary call to open() later - * in the program might get back one of these file descriptors. It's hard to - * guarantee that nobody ever writes to stdout, even though they're not - * supposed to. - */ - TEMP_FAILURE_RETRY(close(STDIN_FILENO)); - if (open("/dev/null", O_RDONLY) < 0) { - int err = errno; - derr << "common_init_daemonize: open(/dev/null) failed: error " - << err << dendl; - exit(1); - } - TEMP_FAILURE_RETRY(close(STDOUT_FILENO)); - if (open("/dev/null", O_RDONLY) < 0) { - int err = errno; - derr << "common_init_daemonize: open(/dev/null) failed: error " - << err << dendl; - exit(1); - } - if (!(flags & CINIT_FLAG_NO_DEFAULT_CONFIG_FILE)) { - ret = common_init_shutdown_stderr(cct); - if (ret) { - derr << "common_init_daemonize: common_init_shutdown_stderr failed with " - << "error code " << ret << dendl; - exit(1); - } - } - pidfile_write(g_conf); - ret = cct->_doss->handle_pid_change(g_conf); - if (ret) { - derr << "common_init_daemonize: _doss->handle_pid_change failed with " - << "error code " << ret << dendl; - exit(1); - } - ldout(cct, 1) << "finished common_init_daemonize" << dendl; -} - -void common_init_chdir(const CephContext *cct) -{ - const md_config_t *conf = cct->_conf; - if (conf->chdir.empty()) - return; - if (::chdir(conf->chdir.c_str())) { - int err = errno; - derr << "common_init_chdir: failed to chdir to directory: '" - << conf->chdir << "': " << cpp_strerror(err) << dendl; - } -} - /* Please be sure that this can safely be called multiple times by the * same application. */ void common_init_finish(CephContext *cct) diff --git a/src/common/common_init.h b/src/common/common_init.h index 848236c538025..57b4aaedab9c4 100644 --- a/src/common/common_init.h +++ b/src/common/common_init.h @@ -1,3 +1,17 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2009-2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + #ifndef CEPH_COMMON_INIT_H #define CEPH_COMMON_INIT_H @@ -25,12 +39,6 @@ enum common_init_flags_t { CephContext *common_preinit(const CephInitParameters &iparams, enum code_environment_t code_env, int flags); void complain_about_parse_errors(std::deque *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(); -int common_init_shutdown_stderr(CephContext *cct); -void common_init_daemonize(CephContext *cct, int flags); void common_init_finish(CephContext *cct); -void common_init_chdir(const CephContext *cct); #endif diff --git a/src/common/config.cc b/src/common/config.cc index 59c4989bc1beb..9c6301d44d195 100644 --- a/src/common/config.cc +++ b/src/common/config.cc @@ -19,7 +19,7 @@ #include "common/DoutStreambuf.h" #include "common/ProfLogger.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/config.h" #include "common/dyn_snprintf.h" #include "common/static_assert.h" diff --git a/src/common/config.h b/src/common/config.h index 19180ad8ccbcf..b0de1b004f77f 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -57,7 +57,7 @@ public: // but it will only receive notifications for the changes that happen after // you attach it, obviously. // - // Most developers will probably attach their observers after common_init, + // Most developers will probably attach their observers after global_init, // but before anyone can call injectargs. // // The caller is responsible for allocating observers. diff --git a/src/common/debug.cc b/src/common/debug.cc deleted file mode 100644 index 1a273dcd734f1..0000000000000 --- a/src/common/debug.cc +++ /dev/null @@ -1,17 +0,0 @@ -#include "common/DoutStreambuf.h" -#include "common/code_environment.h" -#include "common/config.h" -#include "common/version.h" -#include "debug.h" - -#include -#include - -void output_ceph_version() -{ - char buf[1024]; - snprintf(buf, sizeof(buf), "ceph version %s.commit: %s. process: %s. " - "pid: %d", ceph_version_to_str(), git_version_to_str(), - get_process_name_cpp().c_str(), getpid()); - generic_dout(0) << buf << dendl; -} diff --git a/src/cosd.cc b/src/cosd.cc index ca6a60d64280c..30fd030c59cb4 100644 --- a/src/cosd.cc +++ b/src/cosd.cc @@ -32,7 +32,7 @@ using namespace std; #include "msg/SimpleMessenger.h" #include "common/Timer.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/ceph_argparse.h" #include "include/color.h" @@ -56,7 +56,7 @@ int main(int argc, const char **argv) env_to_vec(args); vector::iterator args_iter; - common_init(args, CEPH_ENTITY_TYPE_OSD, CODE_ENVIRONMENT_DAEMON, 0); + global_init(args, CEPH_ENTITY_TYPE_OSD, CODE_ENVIRONMENT_DAEMON, 0); ceph_heap_profiler_init(); // osd specific args @@ -276,12 +276,12 @@ int main(int argc, const char **argv) // Set up crypto, daemonize, etc. // Leave stderr open in case we need to report errors. - common_init_daemonize(&g_ceph_context, CINIT_FLAG_NO_CLOSE_STDERR); + global_init_daemonize(&g_ceph_context, CINIT_FLAG_NO_CLOSE_STDERR); common_init_finish(&g_ceph_context); MonClient mc(&g_ceph_context); if (mc.build_initial_monmap() < 0) return -1; - common_init_chdir(&g_ceph_context); + global_init_chdir(&g_ceph_context); OSD *osd = new OSD(whoami, cluster_messenger, client_messenger, messenger_hb, &mc, g_conf->osd_data, g_conf->osd_journal); @@ -293,7 +293,7 @@ int main(int argc, const char **argv) } // Now close the standard file descriptors - common_init_shutdown_stderr(&g_ceph_context); + global_init_shutdown_stderr(&g_ceph_context); client_messenger->start(); messenger_hb->start(); diff --git a/src/crushtool.cc b/src/crushtool.cc index bfb71fa41748d..d3d034991999b 100644 --- a/src/crushtool.cc +++ b/src/crushtool.cc @@ -23,7 +23,7 @@ #include "common/config.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "crush/CrushWrapper.h" #include "crush/grammar.h" diff --git a/src/csyn.cc b/src/csyn.cc index 1354fb5d0e4e9..5f0da24fa37de 100644 --- a/src/csyn.cc +++ b/src/csyn.cc @@ -27,7 +27,7 @@ using namespace std; #include "mon/MonClient.h" #include "common/Timer.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/ceph_argparse.h" #ifndef DARWIN @@ -46,7 +46,7 @@ int main(int argc, const char **argv, char *envp[]) vector args; argv_to_vec(argc, argv, args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); parse_syn_options(args); // for SyntheticClient diff --git a/src/dupstore.cc b/src/dupstore.cc index 3116932cf9e14..f5c5782455cae 100644 --- a/src/dupstore.cc +++ b/src/dupstore.cc @@ -15,7 +15,7 @@ #include #include "os/FileStore.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include using __gnu_cxx::hash_map; @@ -87,7 +87,7 @@ int main(int argc, const char **argv) argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); // args diff --git a/src/global/global_init.cc b/src/global/global_init.cc new file mode 100644 index 0000000000000..f1f4693e90707 --- /dev/null +++ b/src/global/global_init.cc @@ -0,0 +1,200 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include "common/DoutStreambuf.h" +#include "common/Thread.h" +#include "common/ceph_argparse.h" +#include "common/code_environment.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 "include/color.h" + +#include +#include + +static void output_ceph_version() +{ + char buf[1024]; + snprintf(buf, sizeof(buf), "ceph version %s.commit: %s. process: %s. " + "pid: %d", ceph_version_to_str(), git_version_to_str(), + get_process_name_cpp().c_str(), getpid()); + generic_dout(0) << buf << dendl; +} + +void global_init(std::vector < const char* >& args, + uint32_t module_type, code_environment_t code_env, int flags) +{ + CephInitParameters iparams = + ceph_argparse_early_args(args, module_type, flags); + CephContext *cct = common_preinit(iparams, code_env, flags); + md_config_t *conf = cct->_conf; + + std::deque parse_errors; + int ret = conf->parse_config_files(iparams.get_conf_files(), &parse_errors); + if (ret == -EDOM) { + dout_emergency("global_init: error parsing config file.\n"); + _exit(1); + } + else if (ret == -EINVAL) { + if (!(flags & CINIT_FLAG_NO_DEFAULT_CONFIG_FILE)) { + dout_emergency("global_init: unable to open config file.\n"); + _exit(1); + } + } + else if (ret) { + dout_emergency("global_init: error reading config file.\n"); + _exit(1); + } + + conf->parse_env(); // environment variables override + + conf->parse_argv(args); // argv override + + if (code_env == CODE_ENVIRONMENT_DAEMON) { + if (conf->log_dir.empty() && conf->log_file.empty()) { + conf->set_val_or_die("log_file", "/var/log/ceph/$name.log"); + } + } + + // Expand metavariables. Invoke configuration observers. + conf->apply_changes(); + + // Now we're ready to complain about config file parse errors + complain_about_parse_errors(&parse_errors); + + // signal stuff + int siglist[] = { SIGPIPE, 0 }; + block_signals(siglist, NULL); + install_standard_sighandlers(); + + if (code_env == CODE_ENVIRONMENT_DAEMON) { + cout << TEXT_YELLOW + << " ** WARNING: Ceph is still under development. Any feedback can be directed **" + << TEXT_NORMAL << "\n" << TEXT_YELLOW + << " ** at ceph-devel@vger.kernel.org or http://ceph.newdream.net/. **" + << TEXT_NORMAL << std::endl; + output_ceph_version(); + } + if (g_lockdep) { + cout << TEXT_YELLOW << "*** lockdep is enabled (" << g_lockdep + << ") ***" << TEXT_NORMAL << std::endl; + lockdep_register_ceph_context(cct); + } +} + +static void pidfile_remove_void(void) +{ + pidfile_remove(); +} + +/* Map stderr to /dev/null. This isn't really re-entrant; we rely on the old unix + * behavior that the file descriptor that gets assigned is the lowest + * available one. + */ +int global_init_shutdown_stderr(CephContext *cct) +{ + TEMP_FAILURE_RETRY(close(STDERR_FILENO)); + if (open("/dev/null", O_RDONLY) < 0) { + int err = errno; + derr << "global_init_shutdown_stderr: open(/dev/null) failed: error " + << err << dendl; + return 1; + } + cct->_doss->handle_stderr_shutdown(); + return 0; +} + +void global_init_daemonize(CephContext *cct, int flags) +{ + if (g_code_env != CODE_ENVIRONMENT_DAEMON) + return; + const md_config_t *conf = cct->_conf; + if (!conf->daemonize) + return; + int num_threads = Thread::get_num_threads(); + if (num_threads > 1) { + derr << "global_init_daemonize: BUG: there are " << num_threads - 1 + << " child threads already started that will now die!" << dendl; + exit(1); + } + + int ret = daemon(1, 1); + if (ret) { + ret = errno; + derr << "global_init_daemonize: BUG: daemon error: " + << cpp_strerror(ret) << dendl; + exit(1); + } + + if (atexit(pidfile_remove_void)) { + derr << "global_init_daemonize: failed to set pidfile_remove function " + << "to run at exit." << dendl; + } + + /* This is the old trick where we make file descriptors 0, 1, and possibly 2 + * point to /dev/null. + * + * We have to do this because otherwise some arbitrary call to open() later + * in the program might get back one of these file descriptors. It's hard to + * guarantee that nobody ever writes to stdout, even though they're not + * supposed to. + */ + TEMP_FAILURE_RETRY(close(STDIN_FILENO)); + if (open("/dev/null", O_RDONLY) < 0) { + int err = errno; + derr << "global_init_daemonize: open(/dev/null) failed: error " + << err << dendl; + exit(1); + } + TEMP_FAILURE_RETRY(close(STDOUT_FILENO)); + if (open("/dev/null", O_RDONLY) < 0) { + int err = errno; + derr << "global_init_daemonize: open(/dev/null) failed: error " + << err << dendl; + exit(1); + } + if (!(flags & CINIT_FLAG_NO_DEFAULT_CONFIG_FILE)) { + ret = global_init_shutdown_stderr(cct); + if (ret) { + derr << "global_init_daemonize: global_init_shutdown_stderr failed with " + << "error code " << ret << dendl; + exit(1); + } + } + pidfile_write(g_conf); + ret = cct->_doss->handle_pid_change(g_conf); + if (ret) { + derr << "global_init_daemonize: _doss->handle_pid_change failed with " + << "error code " << ret << dendl; + exit(1); + } + ldout(cct, 1) << "finished global_init_daemonize" << dendl; +} + +void global_init_chdir(const CephContext *cct) +{ + const md_config_t *conf = cct->_conf; + if (conf->chdir.empty()) + return; + if (::chdir(conf->chdir.c_str())) { + int err = errno; + derr << "global_init_chdir: failed to chdir to directory: '" + << conf->chdir << "': " << cpp_strerror(err) << dendl; + } +} diff --git a/src/global/global_init.h b/src/global/global_init.h new file mode 100644 index 0000000000000..6c08881e05fb6 --- /dev/null +++ b/src/global/global_init.h @@ -0,0 +1,34 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#ifndef CEPH_COMMON_GLOBAL_INIT_H +#define CEPH_COMMON_GLOBAL_INIT_H + +#include +#include +#include +#include + +#include "common/code_environment.h" +#include "common/common_init.h" + +class CephContext; + +void global_init(std::vector < const char* >& args, + uint32_t module_type, code_environment_t code_env, int flags); +int global_init_shutdown_stderr(CephContext *cct); +void global_init_daemonize(CephContext *cct, int flags); +void global_init_chdir(const CephContext *cct); + +#endif diff --git a/src/librados-config.cc b/src/librados-config.cc index 1623cde7412b8..d2016f84e74e4 100644 --- a/src/librados-config.cc +++ b/src/librados-config.cc @@ -16,7 +16,7 @@ #include "common/config.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "include/rados/librados.h" void usage() @@ -57,7 +57,7 @@ int main(int argc, const char **argv) ++i; } - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); FOR_EACH_ARG(args) { diff --git a/src/librados.cc b/src/librados.cc index 9720895e6fe19..c17c7f2fdb290 100644 --- a/src/librados.cc +++ b/src/librados.cc @@ -3133,7 +3133,7 @@ extern "C" int rados_create(rados_t *pcluster, const char * const id) } /* This function is intended for use by Ceph daemons. These daemons have - * already called common_init and want to use that particular configuration for + * already called global_init and want to use that particular configuration for * their cluster. */ extern "C" int rados_create_with_context(rados_t *pcluster, CephContext *cct_) diff --git a/src/monmaptool.cc b/src/monmaptool.cc index 3f1c7e9470e3f..de3ed9a441715 100644 --- a/src/monmaptool.cc +++ b/src/monmaptool.cc @@ -24,7 +24,7 @@ using namespace std; #include "common/config.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "mon/MonMap.h" void usage() @@ -49,7 +49,7 @@ int main(int argc, const char **argv) map add; list rm; - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, CINIT_FLAG_NO_DEFAULT_CONFIG_FILE); common_init_finish(&g_ceph_context); FOR_EACH_ARG(args) { diff --git a/src/msg/SimpleMessenger.cc b/src/msg/SimpleMessenger.cc index 083d0992cd6d2..1a388cee4b26e 100644 --- a/src/msg/SimpleMessenger.cc +++ b/src/msg/SimpleMessenger.cc @@ -25,7 +25,7 @@ #include #include "common/config.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "messages/MGenericMessage.h" diff --git a/src/osdc/rados_bencher.h b/src/osdc/rados_bencher.h index 5142222023ef3..22a435b998567 100644 --- a/src/osdc/rados_bencher.h +++ b/src/osdc/rados_bencher.h @@ -16,7 +16,7 @@ */ #include "include/rados/librados.hpp" #include "common/config.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/Cond.h" #include #include diff --git a/src/osdmaptool.cc b/src/osdmaptool.cc index 4787e256f9384..24288a4ed5343 100644 --- a/src/osdmaptool.cc +++ b/src/osdmaptool.cc @@ -28,7 +28,7 @@ using namespace std; #include "osd/OSDMap.h" #include "mon/MonMap.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" void usage() { @@ -49,7 +49,7 @@ int main(int argc, const char **argv) env_to_vec(args); DEFINE_CONF_VARS(usage); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, CINIT_FLAG_NO_DEFAULT_CONFIG_FILE); common_init_finish(&g_ceph_context); diff --git a/src/rados.cc b/src/rados.cc index 4540a5d6004c5..d99a41f078798 100644 --- a/src/rados.cc +++ b/src/rados.cc @@ -21,7 +21,7 @@ using namespace librados; #include "common/config.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/Cond.h" #include "mds/inode_backtrace.h" #include @@ -661,7 +661,7 @@ int main(int argc, const char **argv) argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); std::map < std::string, std::string > opts; diff --git a/src/rados_sync.cc b/src/rados_sync.cc index d24e12dcabb1f..f421db665460b 100644 --- a/src/rados_sync.cc +++ b/src/rados_sync.cc @@ -28,7 +28,7 @@ #include #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/config.h" #include "common/errno.h" #include "common/strtol.h" diff --git a/src/rbd.cc b/src/rbd.cc index ecbc1b8eebe6f..28a96ce2089e7 100644 --- a/src/rbd.cc +++ b/src/rbd.cc @@ -19,7 +19,7 @@ #include "common/errno.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/safe_io.h" #include "common/secret.h" #include "include/rados/librados.hpp" @@ -824,7 +824,7 @@ int main(int argc, const char **argv) env_to_vec(args); int opt_cmd = OPT_NO_CMD; - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); const char *poolname = NULL; diff --git a/src/rgw/rgw_admin.cc b/src/rgw/rgw_admin.cc index 80557d353f142..f366b7f06ebc0 100644 --- a/src/rgw/rgw_admin.cc +++ b/src/rgw/rgw_admin.cc @@ -8,7 +8,7 @@ using namespace std; #include "common/config.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/errno.h" #include "common/armor.h" @@ -347,7 +347,7 @@ int main(int argc, char **argv) argv_to_vec(argc, (const char **)argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); const char *user_id = 0; diff --git a/src/rgw/rgw_main.cc b/src/rgw/rgw_main.cc index f80997921bc03..4e1c95866030b 100644 --- a/src/rgw/rgw_main.cc +++ b/src/rgw/rgw_main.cc @@ -13,7 +13,7 @@ #include "fcgiapp.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/config.h" #include "common/errno.h" #include "rgw_common.h" @@ -74,7 +74,7 @@ int main(int argc, const char **argv) vector args; argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); if (!RGWAccess::init_storage_provider("rados", &g_ceph_context)) { diff --git a/src/streamtest.cc b/src/streamtest.cc index 8583f8d3c47d6..15bf13fc1bb77 100644 --- a/src/streamtest.cc +++ b/src/streamtest.cc @@ -14,7 +14,7 @@ #include #include "os/FileStore.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/ceph_argparse.h" #undef dout_prefix @@ -96,7 +96,7 @@ int main(int argc, const char **argv) argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); // args diff --git a/src/test/TestDoutStreambuf.cc b/src/test/TestDoutStreambuf.cc index bbad110adf773..eb72e855a4eb2 100644 --- a/src/test/TestDoutStreambuf.cc +++ b/src/test/TestDoutStreambuf.cc @@ -20,7 +20,7 @@ */ #include "common/DoutStreambuf.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/config.h" #include @@ -37,7 +37,7 @@ int main(int argc, const char **argv) argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); DoutStreambuf *dos = new DoutStreambuf(); diff --git a/src/test/TestSignalHandlers.cc b/src/test/TestSignalHandlers.cc index 7e82b2d4efbe4..bf74a248f3001 100644 --- a/src/test/TestSignalHandlers.cc +++ b/src/test/TestSignalHandlers.cc @@ -19,7 +19,7 @@ */ #include "common/DoutStreambuf.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/errno.h" #include "common/config.h" @@ -94,7 +94,7 @@ int main(int argc, const char **argv) argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); DEFINE_CONF_VARS(usage); diff --git a/src/test/TestTimers.cc b/src/test/TestTimers.cc index 79fef474c8001..6ef51a0040287 100644 --- a/src/test/TestTimers.cc +++ b/src/test/TestTimers.cc @@ -1,7 +1,7 @@ #include "common/ceph_argparse.h" #include "common/Mutex.h" #include "common/Timer.h" -#include "common/common_init.h" +#include "global/global_init.h" #include @@ -255,7 +255,7 @@ int main(int argc, const char **argv) argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); int ret; diff --git a/src/test/cli/cconf/env-vs-args.t b/src/test/cli/cconf/env-vs-args.t index d972489651497..ae9368040885c 100644 --- a/src/test/cli/cconf/env-vs-args.t +++ b/src/test/cli/cconf/env-vs-args.t @@ -1,10 +1,10 @@ # we can use CEPH_CONF to override the normal configuration file location. $ env CEPH_CONF=from-env cconf -s foo bar - common_init: unable to open config file. (re) + global_init: unable to open config file. (re) [1] # command-line arguments should override environment $ env -u CEPH_CONF cconf -c from-args - common_init: unable to open config file. (re) + global_init: unable to open config file. (re) [1] diff --git a/src/test/test_mutate.cc b/src/test/test_mutate.cc index b1b8f924ae9f9..b3faf7a86aa91 100644 --- a/src/test/test_mutate.cc +++ b/src/test/test_mutate.cc @@ -19,7 +19,7 @@ #include "include/types.h" #include "include/rados/librados.hpp" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/config.h" #include @@ -43,7 +43,7 @@ int main(int argc, const char **argv) vector args; argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); string val; diff --git a/src/test_trans.cc b/src/test_trans.cc index 7ab2ac506f027..9167983846389 100644 --- a/src/test_trans.cc +++ b/src/test_trans.cc @@ -15,7 +15,7 @@ #include #include "common/ceph_argparse.h" #include "os/FileStore.h" -#include "common/common_init.h" +#include "global/global_init.h" #undef dout_prefix #define dout_prefix *_dout @@ -35,7 +35,7 @@ int main(int argc, const char **argv) argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); // args diff --git a/src/testkeys.cc b/src/testkeys.cc index 419fa142956ce..2ad90895c39e9 100644 --- a/src/testkeys.cc +++ b/src/testkeys.cc @@ -1,6 +1,6 @@ #include "auth/cephx/CephxKeyServer.h" #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/config.h" #define AES_KEY_LEN 16 @@ -11,7 +11,7 @@ int main(int argc, const char **argv) argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); KeyServer server(&g_ceph_context); diff --git a/src/testmsgr.cc b/src/testmsgr.cc index cf7b182c0bca5..feb8d0b3171c9 100644 --- a/src/testmsgr.cc +++ b/src/testmsgr.cc @@ -25,7 +25,7 @@ using namespace std; #include "messages/MPing.h" #include "common/Timer.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/ceph_argparse.h" #ifndef DARWIN @@ -76,7 +76,7 @@ int main(int argc, const char **argv, const char *envp[]) { argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); vec_to_argv(args, argc, argv); diff --git a/src/tools/ceph.cc b/src/tools/ceph.cc index e3b12d6964b10..92cb25eecf98a 100644 --- a/src/tools/ceph.cc +++ b/src/tools/ceph.cc @@ -14,7 +14,7 @@ */ #include "common/ceph_argparse.h" -#include "common/common_init.h" +#include "global/global_init.h" #include "common/errno.h" #include "common/safe_io.h" #include "common/config.h" @@ -116,7 +116,7 @@ int main(int argc, const char **argv) argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); vec_to_argv(args, argc, argv); diff --git a/src/tools/common.cc b/src/tools/common.cc index 11db06d9fb098..23da5449075c4 100644 --- a/src/tools/common.cc +++ b/src/tools/common.cc @@ -28,7 +28,7 @@ using namespace std; #include "common/Cond.h" #include "common/Mutex.h" #include "common/Timer.h" -#include "common/common_init.h" +#include "global/global_init.h" #ifndef DARWIN #include diff --git a/src/tools/gceph.cc b/src/tools/gceph.cc index c9832d92934da..7a80fb3f33ccc 100644 --- a/src/tools/gceph.cc +++ b/src/tools/gceph.cc @@ -13,7 +13,7 @@ * */ -#include "common/common_init.h" +#include "global/global_init.h" #include "common/ceph_argparse.h" #include "common/config.h" #include "tools/common.h" @@ -78,7 +78,7 @@ int main(int argc, const char **argv) argv_to_vec(argc, argv, args); env_to_vec(args); - common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); + global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); common_init_finish(&g_ceph_context); vec_to_argv(args, argc, argv); diff --git a/src/tools/gui.h b/src/tools/gui.h index 82e8cc018799c..ee0e4beacdc93 100644 --- a/src/tools/gui.h +++ b/src/tools/gui.h @@ -22,7 +22,7 @@ #ifndef CEPH_GUI_H #define CEPH_GUI_H -#include "common/common_init.h" +#include "global/global_init.h" #include "mds/MDSMap.h" #include "mon/MonMap.h" #include "mon/PGMap.h" -- 2.39.5