]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/common_init.cc
f3ca0326137818bba83446d8343a565e871d9d33
[ceph.git] / ceph / src / common / common_init.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2010-2011 Dreamhost
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #include "include/compat.h"
16 #include "common/common_init.h"
17 #include "common/admin_socket.h"
18 #include "common/ceph_argparse.h"
19 #include "common/ceph_context.h"
20 #include "common/config.h"
21 #include "common/dout.h"
22 #include "common/hostname.h"
23 #include "common/strtol.h"
24 #include "common/valgrind.h"
25 #include "common/zipkin_trace.h"
26
27 #define dout_subsys ceph_subsys_
28
29 #ifndef WITH_SEASTAR
30 CephContext *common_preinit(const CephInitParameters &iparams,
31 enum code_environment_t code_env, int flags)
32 {
33 // set code environment
34 ANNOTATE_BENIGN_RACE_SIZED(&g_code_env, sizeof(g_code_env), "g_code_env");
35 g_code_env = code_env;
36
37 // Create a configuration object
38 CephContext *cct = new CephContext(iparams.module_type, code_env, flags);
39
40 auto& conf = cct->_conf;
41 // add config observers here
42
43 // Set up our entity name.
44 conf->name = iparams.name;
45
46 // different default keyring locations for osd and mds. this is
47 // for backward compatibility. moving forward, we want all keyrings
48 // in these locations. the mon already forces $mon_data/keyring.
49 if (conf->name.is_mds()) {
50 conf.set_val_default("keyring", "$mds_data/keyring");
51 } else if (conf->name.is_osd()) {
52 conf.set_val_default("keyring", "$osd_data/keyring");
53 }
54
55 if ((flags & CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS)) {
56 // make this unique despite multiple instances by the same name.
57 conf.set_val_default("admin_socket",
58 "$run_dir/$cluster-$name.$pid.$cctid.asok");
59 }
60
61 if (code_env == CODE_ENVIRONMENT_LIBRARY ||
62 code_env == CODE_ENVIRONMENT_UTILITY_NODOUT) {
63 conf.set_val_default("log_to_stderr", "false");
64 conf.set_val_default("err_to_stderr", "false");
65 conf.set_val_default("log_flush_on_exit", "false");
66 }
67
68 conf.set_val("no_config_file", iparams.no_config_file ? "true" : "false");
69
70 if (conf->host.empty()) {
71 conf.set_val("host", ceph_get_short_hostname());
72 }
73 return cct;
74 }
75 #endif // #ifndef WITH_SEASTAR
76
77 void complain_about_parse_error(CephContext *cct,
78 const std::string& parse_error)
79 {
80 if (parse_error.empty())
81 return;
82 lderr(cct) << "Errors while parsing config file!" << dendl;
83 lderr(cct) << parse_error << dendl;
84 }
85
86 #ifndef WITH_SEASTAR
87
88 /* Please be sure that this can safely be called multiple times by the
89 * same application. */
90 void common_init_finish(CephContext *cct)
91 {
92 // only do this once per cct
93 if (cct->_finished) {
94 return;
95 }
96 cct->_finished = true;
97 cct->init_crypto();
98 ZTracer::ztrace_init();
99
100 if (!cct->_log->is_started()) {
101 cct->_log->start();
102 }
103
104 int flags = cct->get_init_flags();
105 if (!(flags & CINIT_FLAG_NO_DAEMON_ACTIONS))
106 cct->start_service_thread();
107
108 if ((flags & CINIT_FLAG_DEFER_DROP_PRIVILEGES) &&
109 (cct->get_set_uid() || cct->get_set_gid())) {
110 cct->get_admin_socket()->chown(cct->get_set_uid(), cct->get_set_gid());
111 }
112
113 const auto& conf = cct->_conf;
114
115 if (!conf->admin_socket.empty() && !conf->admin_socket_mode.empty()) {
116 int ret = 0;
117 std::string err;
118
119 ret = strict_strtol(conf->admin_socket_mode.c_str(), 8, &err);
120 if (err.empty()) {
121 if (!(ret & (~ACCESSPERMS))) {
122 cct->get_admin_socket()->chmod(static_cast<mode_t>(ret));
123 } else {
124 lderr(cct) << "Invalid octal permissions string: "
125 << conf->admin_socket_mode << dendl;
126 }
127 } else {
128 lderr(cct) << "Invalid octal string: " << err << dendl;
129 }
130 }
131 }
132
133 #endif // #ifndef WITH_SEASTAR