Skip to content

Commit

Permalink
move common_init to global_init
Browse files Browse the repository at this point in the history
Signed-off-by: Colin McCabe <[email protected]>
  • Loading branch information
cmccabe committed Jun 20, 2011
1 parent c46814d commit 63bbadd
Show file tree
Hide file tree
Showing 42 changed files with 319 additions and 259 deletions.
3 changes: 2 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down Expand Up @@ -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\
Expand Down
4 changes: 2 additions & 2 deletions src/cauthtool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/cconf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/cfuse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<const char*> nargs;
Expand Down
8 changes: 4 additions & 4 deletions src/cmds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
8 changes: 4 additions & 4 deletions src/cmon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -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 =
Expand Down
1 change: 1 addition & 0 deletions src/common/ceph_argparse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
167 changes: 0 additions & 167 deletions src/common/common_init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <errno.h>
#include <deque>
#include <syslog.h>

#define _STR(x) #x
#define STRINGIFY(x) _STR(x)
Expand Down Expand Up @@ -87,168 +82,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)
{
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<std::string> 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 [email protected] 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)
Expand Down
20 changes: 14 additions & 6 deletions src/common/common_init.h
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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<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();
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
2 changes: 1 addition & 1 deletion src/common/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/common/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class md_config_t {
// 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.
Expand Down
Loading

0 comments on commit 63bbadd

Please sign in to comment.