summaryrefslogtreecommitdiff
path: root/src/common/common_init.cc
blob: defde28b22e8207f81d9f075a5e5d844878bcbd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// -*- 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) 2010-2011 Dreamhost
 *
 * 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/ceph_argparse.h"
#include "common/ceph_context.h"
#include "common/ceph_crypto.h"
#include "common/code_environment.h"
#include "common/common_init.h"
#include "common/config.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/safe_io.h"
#include "common/version.h"
#include "include/color.h"

#include <errno.h>
#include <deque>

#define dout_subsys ceph_subsys_

#define _STR(x) #x
#define STRINGIFY(x) _STR(x)

CephContext *common_preinit(const CephInitParameters &iparams,
			  enum code_environment_t code_env, int flags)
{
  // set code environment
  g_code_env = code_env;

  // Create a configuration object
  CephContext *cct = new CephContext(iparams.module_type);

  md_config_t *conf = cct->_conf;
  // add config observers here

  // Set up our entity name.
  conf->name = iparams.name;

  // Set some defaults based on code type
  switch (code_env) {
  case CODE_ENVIRONMENT_DAEMON:
    conf->set_val_or_die("daemonize", "true");
    conf->set_val_or_die("log_to_stderr", "false");
    conf->set_val_or_die("err_to_stderr", "true");

    // different default keyring locations for osd and mds.  this is
    // for backward compatibility.  moving forward, we want all keyrings
    // in these locations.  the mon already forces $mon_data/keyring.
    if (conf->name.is_mds())
      conf->set_val("keyring", "$mds_data/keyring", false);
    else if (conf->name.is_osd())
      conf->set_val("keyring", "$osd_data/keyring", false);
    break;

  case CODE_ENVIRONMENT_LIBRARY:
    conf->set_val_or_die("log_to_stderr", "false");
    conf->set_val_or_die("err_to_stderr", "false");
    conf->set_val_or_die("log_flush_on_exit", "false");
    break;

  default:
    break;
  }

  if ((flags & CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS) ||
      code_env != CODE_ENVIRONMENT_DAEMON) {
    // no default log, pid_file, admin_socket
    conf->set_val_or_die("pid_file", "");
    conf->set_val_or_die("admin_socket", "");
    conf->set_val_or_die("log_file", "");
  }

  return cct;
}

void complain_about_parse_errors(CephContext *cct,
				 std::deque<std::string> *parse_errors)
{
  if (parse_errors->empty())
    return;
  lderr(cct) << "Errors while parsing config file!" << dendl;
  int cur_err = 0;
  static const int MAX_PARSE_ERRORS = 20;
  for (std::deque<std::string>::const_iterator p = parse_errors->begin();
       p != parse_errors->end(); ++p)
  {
    lderr(cct) << *p << dendl;
    if (cur_err == MAX_PARSE_ERRORS) {
      lderr(cct) << "Suppressed " << (parse_errors->size() - MAX_PARSE_ERRORS)
	   << " more errors." << dendl;
      break;
    }
    ++cur_err;
  }
}

/* Please be sure that this can safely be called multiple times by the
 * same application. */
void common_init_finish(CephContext *cct)
{
  ceph::crypto::init(cct);
  cct->start_service_thread();

  if (cct->_conf->lockdep) {
    g_lockdep = true;
    ldout(cct,0) << "lockdep is enabled" << dendl;
    lockdep_register_ceph_context(cct);
  }
}