1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
15 #include "common/ceph_argparse.h"
16 #include "common/common_init.h"
17 #include "common/config.h"
18 #include "include/str_list.h"
19 #include "include/stringify.h"
20 #include "osd/osd_types.h"
21 #include "common/errno.h"
22 #include "common/hostname.h"
24 #include <boost/type_traits.hpp>
26 /* Don't use standard Ceph logging in this file.
27 * We can't use logging until it's initialized, and a lot of the necessary
28 * initialization happens here.
40 using std::ostringstream
;
44 const char *CEPH_CONF_FILE_DEFAULT
= "$data_dir/config, /etc/ceph/$cluster.conf, ~/.ceph/$cluster.conf, $cluster.conf"
45 #if defined(__FreeBSD__)
46 ", /usr/local/etc/ceph/$cluster.conf"
51 #define STRINGIFY(x) _STR(x)
53 int ceph_resolve_file_search(const std::string
& filename_list
,
57 get_str_list(filename_list
, ls
);
60 list
<string
>::iterator iter
;
61 for (iter
= ls
.begin(); iter
!= ls
.end(); ++iter
) {
62 int fd
= ::open(iter
->c_str(), O_RDONLY
);
77 md_config_t::md_config_t(bool is_daemon
)
79 lock("md_config_t", true, false)
83 // Load the compile-time list of Option into
84 // a map so that we can resolve keys quickly.
85 for (const auto &i
: ceph_options
) {
86 if (schema
.count(i
.name
)) {
87 // We may be instantiated pre-logging so send
88 std::cerr
<< "Duplicate config key in schema: '" << i
.name
<< "'"
92 schema
.insert({i
.name
, i
});
95 // Populate list of legacy_values according to the OPTION() definitions
96 // Note that this is just setting up our map of name->member ptr. The
97 // default values etc will get loaded in along with new-style data,
98 // as all loads write to both the values map, and the legacy
99 // members if present.
101 #define OPTION(name, type) \
102 {std::string(STRINGIFY(name)), &md_config_t::name},
103 #define SAFE_OPTION(name, type) OPTION(name, type)
104 #include "common/legacy_config_opts.h"
111 // Load default values from the schema
112 for (const auto &i
: schema
) {
113 const Option
&opt
= i
.second
;
114 bool has_daemon_default
= !boost::get
<boost::blank
>(&opt
.daemon_value
);
115 Option::value_t default_val
;
116 if (is_daemon
&& has_daemon_default
) {
117 default_val
= opt
.daemon_value
;
119 default_val
= opt
.value
;
122 if (opt
.type
== Option::TYPE_STR
) {
123 // We call pre_validate as a sanity check, but also to get any
124 // side effect (value modification) from the validator.
125 std::string
*def_str
= boost::get
<std::string
>(&default_val
);
127 if (opt
.pre_validate(def_str
, &err
) != 0) {
128 std::cerr
<< "Default value " << opt
.name
<< "=" << *def_str
<< " is "
129 "invalid: " << err
<< std::endl
;
131 // This is the compiled-in default that is failing its own option's
132 // validation, so this is super-invalid and should never make it
133 // past a pull request: crash out.
138 values
[i
.first
] = default_val
;
141 // Copy out values (defaults) into any legacy (C struct member) fields
142 for (const auto &i
: legacy_values
) {
143 const auto &name
= i
.first
;
144 const auto &option
= schema
.at(name
);
147 update_legacy_val(option
, ptr
);
152 * Sanity check schema. Assert out on failures, to ensure any bad changes
153 * cannot possibly pass any testing and make it into a release.
155 void md_config_t::validate_schema()
157 for (const auto &i
: schema
) {
158 const auto &opt
= i
.second
;
159 for (const auto &see_also_key
: opt
.see_also
) {
160 if (schema
.count(see_also_key
) == 0) {
161 std::cerr
<< "Non-existent see-also key '" << see_also_key
162 << "' on option '" << opt
.name
<< "'" << std::endl
;
168 for (const auto &i
: legacy_values
) {
169 if (schema
.count(i
.first
) == 0) {
170 std::cerr
<< "Schema is missing legacy field '" << i
.first
<< "'"
177 void md_config_t::init_subsys()
179 #define SUBSYS(name, log, gather) \
180 subsys.add(ceph_subsys_##name, STRINGIFY(name), log, gather);
181 #define DEFAULT_SUBSYS(log, gather) \
182 subsys.add(ceph_subsys_, "none", log, gather);
183 #include "common/subsys.h"
185 #undef DEFAULT_SUBSYS
188 md_config_t::~md_config_t()
192 void md_config_t::add_observer(md_config_obs_t
* observer_
)
194 Mutex::Locker
l(lock
);
195 const char **keys
= observer_
->get_tracked_conf_keys();
196 for (const char ** k
= keys
; *k
; ++k
) {
197 obs_map_t::value_type
val(*k
, observer_
);
198 observers
.insert(val
);
202 void md_config_t::remove_observer(md_config_obs_t
* observer_
)
204 Mutex::Locker
l(lock
);
205 bool found_obs
= false;
206 for (obs_map_t::iterator o
= observers
.begin(); o
!= observers
.end(); ) {
207 if (o
->second
== observer_
) {
208 observers
.erase(o
++);
218 int md_config_t::parse_config_files(const char *conf_files
,
219 std::ostream
*warnings
,
222 Mutex::Locker
l(lock
);
224 if (internal_safe_to_start_threads
)
227 if (!cluster
.size() && !conf_files
) {
229 * set the cluster name to 'ceph' when neither cluster name nor
230 * configuration file are specified.
236 const char *c
= getenv("CEPH_CONF");
241 if (flags
& CINIT_FLAG_NO_DEFAULT_CONFIG_FILE
)
243 conf_files
= CEPH_CONF_FILE_DEFAULT
;
247 std::list
<std::string
> cfl
;
248 get_str_list(conf_files
, cfl
);
250 auto p
= cfl
.begin();
251 while (p
!= cfl
.end()) {
254 if (s
.find("$data_dir") != string::npos
) {
255 if (data_dir_option
.length()) {
256 list
<const Option
*> stack
;
257 expand_meta(s
, NULL
, stack
, warnings
);
260 cfl
.erase(p
++); // ignore this item
266 return parse_config_files_impl(cfl
, warnings
);
269 int md_config_t::parse_config_files_impl(const std::list
<std::string
> &conf_files
,
270 std::ostream
*warnings
)
272 assert(lock
.is_locked());
275 list
<string
>::const_iterator c
;
276 for (c
= conf_files
.begin(); c
!= conf_files
.end(); ++c
) {
279 expand_meta(fn
, warnings
);
280 int ret
= cf
.parse_file(fn
.c_str(), &parse_errors
, warnings
);
283 else if (ret
!= -ENOENT
)
286 // it must have been all ENOENTs, that's the only way we got here
287 if (c
== conf_files
.end())
290 if (cluster
.size() == 0) {
292 * If cluster name is not set yet, use the prefix of the
293 * basename of configuration file as cluster name.
295 auto start
= c
->rfind('/') + 1;
296 auto end
= c
->find(".conf", start
);
297 if (end
== c
->npos
) {
299 * If the configuration file does not follow $cluster.conf
300 * convention, we do the last try and assign the cluster to
305 cluster
= c
->substr(start
, end
- start
);
309 std::vector
<std::string
> my_sections
;
310 _get_my_sections(my_sections
);
311 for (const auto &i
: schema
) {
312 const auto &opt
= i
.second
;
314 int ret
= _get_val_from_conf_file(my_sections
, opt
.name
, val
, false);
316 std::string error_message
;
317 int r
= set_val_impl(val
, opt
, &error_message
);
318 if (warnings
!= nullptr && (r
!= 0 || !error_message
.empty())) {
319 *warnings
<< "parse error setting '" << opt
.name
<< "' to '" << val
321 if (!error_message
.empty()) {
322 *warnings
<< " (" << error_message
<< ")";
324 *warnings
<< std::endl
;
330 for (size_t o
= 0; o
< subsys
.get_num(); o
++) {
331 std::string
as_option("debug_");
332 as_option
+= subsys
.get_name(o
);
334 int ret
= _get_val_from_conf_file(my_sections
, as_option
.c_str(), val
, false);
337 int r
= sscanf(val
.c_str(), "%d/%d", &log
, &gather
);
341 // cout << "config subsys " << subsys.get_name(o) << " log " << log << " gather " << gather << std::endl;
342 subsys
.set_log_level(o
, log
);
343 subsys
.set_gather_level(o
, gather
);
348 // Warn about section names that look like old-style section names
349 std::deque
< std::string
> old_style_section_names
;
350 for (ConfFile::const_section_iter_t s
= cf
.sections_begin();
351 s
!= cf
.sections_end(); ++s
) {
352 const string
&str(s
->first
);
353 if (((str
.find("mds") == 0) || (str
.find("mon") == 0) ||
354 (str
.find("osd") == 0)) && (str
.size() > 3) && (str
[3] != '.')) {
355 old_style_section_names
.push_back(str
);
358 if (!old_style_section_names
.empty()) {
360 cerr
<< "ERROR! old-style section name(s) found: ";
362 for (std::deque
< std::string
>::const_iterator os
= old_style_section_names
.begin();
363 os
!= old_style_section_names
.end(); ++os
) {
367 cerr
<< ". Please use the new style section names that include a period.";
372 void md_config_t::parse_env()
374 Mutex::Locker
l(lock
);
375 if (internal_safe_to_start_threads
)
377 if (getenv("CEPH_KEYRING")) {
378 set_val_or_die("keyring", getenv("CEPH_KEYRING"));
382 void md_config_t::show_config(std::ostream
& out
)
384 Mutex::Locker
l(lock
);
385 _show_config(&out
, NULL
);
388 void md_config_t::show_config(Formatter
*f
)
390 Mutex::Locker
l(lock
);
391 _show_config(NULL
, f
);
394 void md_config_t::_show_config(std::ostream
*out
, Formatter
*f
)
397 *out
<< "name = " << name
<< std::endl
;
398 *out
<< "cluster = " << cluster
<< std::endl
;
401 f
->dump_string("name", stringify(name
));
402 f
->dump_string("cluster", cluster
);
404 for (size_t o
= 0; o
< subsys
.get_num(); o
++) {
406 *out
<< "debug_" << subsys
.get_name(o
)
407 << " = " << subsys
.get_log_level(o
)
408 << "/" << subsys
.get_gather_level(o
) << std::endl
;
411 std::string debug_name
= "debug_";
412 debug_name
+= subsys
.get_name(o
);
413 ss
<< subsys
.get_log_level(o
)
414 << "/" << subsys
.get_gather_level(o
);
415 f
->dump_string(debug_name
.c_str(), ss
.str());
418 for (const auto& i
: schema
) {
419 const Option
&opt
= i
.second
;
421 _get_val(opt
.name
, &buf
, -1);
423 *out
<< opt
.name
<< " = " << buf
<< std::endl
;
425 f
->dump_string(opt
.name
.c_str(), buf
);
430 int md_config_t::parse_argv(std::vector
<const char*>& args
)
432 Mutex::Locker
l(lock
);
433 if (internal_safe_to_start_threads
) {
437 bool show_config
= false;
438 bool show_config_value
= false;
439 string show_config_value_arg
;
441 // In this function, don't change any parts of the configuration directly.
442 // Instead, use set_val to set them. This will allow us to send the proper
443 // observer notifications later.
445 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end(); ) {
446 if (strcmp(*i
, "--") == 0) {
447 /* Normally we would use ceph_argparse_double_dash. However, in this
448 * function we *don't* want to remove the double dash, because later
449 * argument parses will still need to see it. */
452 else if (ceph_argparse_flag(args
, i
, "--show_conf", (char*)NULL
)) {
453 cerr
<< cf
<< std::endl
;
456 else if (ceph_argparse_flag(args
, i
, "--show_config", (char*)NULL
)) {
459 else if (ceph_argparse_witharg(args
, i
, &val
, "--show_config_value", (char*)NULL
)) {
460 show_config_value
= true;
461 show_config_value_arg
= val
;
463 else if (ceph_argparse_flag(args
, i
, "--foreground", "-f", (char*)NULL
)) {
464 set_val_or_die("daemonize", "false");
466 else if (ceph_argparse_flag(args
, i
, "-d", (char*)NULL
)) {
467 set_val_or_die("daemonize", "false");
468 set_val_or_die("log_file", "");
469 set_val_or_die("log_to_stderr", "true");
470 set_val_or_die("err_to_stderr", "true");
471 set_val_or_die("log_to_syslog", "false");
473 // Some stuff that we wanted to give universal single-character options for
474 // Careful: you can burn through the alphabet pretty quickly by adding
476 else if (ceph_argparse_witharg(args
, i
, &val
, "--monmap", "-M", (char*)NULL
)) {
477 set_val_or_die("monmap", val
.c_str());
479 else if (ceph_argparse_witharg(args
, i
, &val
, "--mon_host", "-m", (char*)NULL
)) {
480 set_val_or_die("mon_host", val
.c_str());
482 else if (ceph_argparse_witharg(args
, i
, &val
, "--bind", (char*)NULL
)) {
483 set_val_or_die("public_addr", val
.c_str());
485 else if (ceph_argparse_witharg(args
, i
, &val
, "--keyfile", "-K", (char*)NULL
)) {
486 set_val_or_die("keyfile", val
.c_str());
488 else if (ceph_argparse_witharg(args
, i
, &val
, "--keyring", "-k", (char*)NULL
)) {
489 set_val_or_die("keyring", val
.c_str());
491 else if (ceph_argparse_witharg(args
, i
, &val
, "--client_mountpoint", "-r", (char*)NULL
)) {
492 set_val_or_die("client_mountpoint", val
.c_str());
495 int r
= parse_option(args
, i
, NULL
);
504 _show_config(&cout
, NULL
);
508 if (show_config_value
) {
510 int r
= _get_val(show_config_value_arg
.c_str(), &buf
, -1);
513 std::cerr
<< "failed to get config option '" <<
514 show_config_value_arg
<< "': option not found" << std::endl
;
516 std::cerr
<< "failed to get config option '" <<
517 show_config_value_arg
<< "': " << cpp_strerror(r
) << std::endl
;
521 expand_meta(s
, &std::cerr
);
522 std::cout
<< s
<< std::endl
;
529 int md_config_t::parse_option(std::vector
<const char*>& args
,
530 std::vector
<const char*>::iterator
& i
,
538 for (o
= 0; o
< subsys
.get_num(); o
++) {
539 std::string
as_option("--");
540 as_option
+= "debug_";
541 as_option
+= subsys
.get_name(o
);
543 if (ceph_argparse_witharg(args
, i
, &val
, err
,
544 as_option
.c_str(), (char*)NULL
)) {
553 int r
= sscanf(val
.c_str(), "%d/%d", &log
, &gather
);
557 // cout << "subsys " << subsys.get_name(o) << " log " << log << " gather " << gather << std::endl;
558 subsys
.set_log_level(o
, log
);
559 subsys
.set_gather_level(o
, gather
);
561 *oss
<< "debug_" << subsys
.get_name(o
) << "=" << log
<< "/" << gather
<< " ";
566 if (o
< subsys
.get_num()) {
570 std::string option_name
;
571 std::string error_message
;
573 for (const auto& opt_iter
: schema
) {
574 const Option
&opt
= opt_iter
.second
;
576 std::string
as_option("--");
577 as_option
+= opt
.name
;
578 option_name
= opt
.name
;
579 if (opt
.type
== Option::TYPE_BOOL
) {
581 if (ceph_argparse_binary_flag(args
, i
, &res
, oss
, as_option
.c_str(),
584 ret
= set_val_impl("false", opt
, &error_message
);
586 ret
= set_val_impl("true", opt
, &error_message
);
591 std::string
no("--no-");
593 if (ceph_argparse_flag(args
, i
, no
.c_str(), (char*)NULL
)) {
594 ret
= set_val_impl("false", opt
, &error_message
);
598 } else if (ceph_argparse_witharg(args
, i
, &val
, err
,
599 as_option
.c_str(), (char*)NULL
)) {
600 if (!err
.str().empty()) {
601 error_message
= err
.str();
605 if (oss
&& ((!opt
.is_safe()) &&
606 (observers
.find(opt
.name
) == observers
.end()))) {
607 *oss
<< "You cannot change " << opt
.name
<< " using injectargs.\n";
610 ret
= set_val_impl(val
, opt
, &error_message
);
616 if (ret
!= 0 || !error_message
.empty()) {
617 assert(!option_name
.empty());
619 *oss
<< "Parse error setting " << option_name
<< " to '"
620 << val
<< "' using injectargs";
621 if (!error_message
.empty()) {
622 *oss
<< " (" << error_message
<< ")";
626 cerr
<< "parse error setting '" << option_name
<< "' to '"
628 if (!error_message
.empty()) {
629 cerr
<< " (" << error_message
<< ")";
631 cerr
<< "\n" << std::endl
;
635 if (o
== schema
.size()) {
642 int md_config_t::parse_injectargs(std::vector
<const char*>& args
,
645 assert(lock
.is_locked());
647 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end(); ) {
648 int r
= parse_option(args
, i
, oss
);
655 void md_config_t::apply_changes(std::ostream
*oss
)
657 Mutex::Locker
l(lock
);
659 * apply changes until the cluster name is assigned
665 bool md_config_t::_internal_field(const string
& s
)
667 if (s
== "internal_safe_to_start_threads")
672 void md_config_t::_apply_changes(std::ostream
*oss
)
674 /* Maps observers to the configuration options that they care about which
676 typedef std::map
< md_config_obs_t
*, std::set
<std::string
> > rev_obs_map_t
;
680 // expand_all_meta could have modified anything. Copy it all out again.
681 for (const auto &i
: legacy_values
) {
682 const auto &name
= i
.first
;
683 const auto &option
= schema
.at(name
);
686 update_legacy_val(option
, ptr
);
689 // create the reverse observer mapping, mapping observers to the set of
690 // changed keys that they'll get.
692 std::set
<std::string
> empty_set
;
694 char *bufptr
= (char*)buf
;
695 for (changed_set_t::const_iterator c
= changed
.begin();
696 c
!= changed
.end(); ++c
) {
697 const std::string
&key(*c
);
698 pair
< obs_map_t::iterator
, obs_map_t::iterator
>
699 range(observers
.equal_range(key
));
701 (!_get_val(key
.c_str(), &bufptr
, sizeof(buf
))) &&
702 !_internal_field(key
)) {
703 (*oss
) << key
<< " = '" << buf
<< "' ";
704 if (range
.first
== range
.second
) {
705 (*oss
) << "(not observed, change may require restart) ";
708 for (obs_map_t::iterator r
= range
.first
; r
!= range
.second
; ++r
) {
709 rev_obs_map_t::value_type
robs_val(r
->second
, empty_set
);
710 pair
< rev_obs_map_t::iterator
, bool > robs_ret(robs
.insert(robs_val
));
711 std::set
<std::string
> &keys(robs_ret
.first
->second
);
718 // Make any pending observer callbacks
719 for (rev_obs_map_t::const_iterator r
= robs
.begin(); r
!= robs
.end(); ++r
) {
720 md_config_obs_t
*obs
= r
->first
;
721 obs
->handle_conf_change(this, r
->second
);
726 void md_config_t::call_all_observers()
728 std::map
<md_config_obs_t
*,std::set
<std::string
> > obs
;
730 Mutex::Locker
l(lock
);
734 for (auto r
= observers
.begin(); r
!= observers
.end(); ++r
) {
735 obs
[r
->second
].insert(r
->first
);
738 for (auto p
= obs
.begin();
741 p
->first
->handle_conf_change(this, p
->second
);
745 int md_config_t::injectargs(const std::string
& s
, std::ostream
*oss
)
748 Mutex::Locker
l(lock
);
749 char b
[s
.length()+1];
750 strcpy(b
, s
.c_str());
751 std::vector
<const char*> nargs
;
755 while (*p
&& *p
!= ' ') p
++;
759 while (*p
&& *p
== ' ') p
++;
761 ret
= parse_injectargs(nargs
, oss
);
762 if (!nargs
.empty()) {
763 *oss
<< " failed to parse arguments: ";
765 for (std::vector
<const char*>::const_iterator i
= nargs
.begin();
766 i
!= nargs
.end(); ++i
) {
767 *oss
<< prefix
<< *i
;
777 void md_config_t::set_val_or_die(const std::string
&key
,
778 const std::string
&val
,
781 std::stringstream err
;
782 int ret
= set_val(key
, val
, meta
, &err
);
784 std::cerr
<< "set_val_or_die(" << key
<< "): " << err
.str();
789 int md_config_t::set_val(const std::string
&key
, const char *val
,
790 bool meta
, std::stringstream
*err_ss
)
792 Mutex::Locker
l(lock
);
794 if (err_ss
) *err_ss
<< "No key specified";
803 expand_meta(v
, &std::cerr
);
805 string
k(ConfFile::normalize_key_name(key
));
808 if (strncmp(k
.c_str(), "debug_", 6) == 0) {
809 for (size_t o
= 0; o
< subsys
.get_num(); o
++) {
810 std::string as_option
= "debug_" + subsys
.get_name(o
);
811 if (k
== as_option
) {
813 int r
= sscanf(v
.c_str(), "%d/%d", &log
, &gather
);
818 subsys
.set_log_level(o
, log
);
819 subsys
.set_gather_level(o
, gather
);
820 if (err_ss
) *err_ss
<< "Set " << k
<< " to " << log
<< "/" << gather
;
824 *err_ss
<< "Invalid debug level, should be <int> or <int>/<int>";
831 const auto &opt_iter
= schema
.find(k
);
832 if (opt_iter
!= schema
.end()) {
833 const Option
&opt
= opt_iter
->second
;
834 if ((!opt
.is_safe()) && internal_safe_to_start_threads
) {
835 // If threads have been started and the option is not thread safe
836 if (observers
.find(opt
.name
) == observers
.end()) {
837 // And there is no observer to safely change it...
839 if (err_ss
) *err_ss
<< "Configuration option '" << key
<< "' may "
840 "not be modified at runtime";
845 std::string error_message
;
846 int r
= set_val_impl(v
, opt
, &error_message
);
848 if (err_ss
) *err_ss
<< "Set " << opt
.name
<< " to " << v
;
850 if (err_ss
) *err_ss
<< error_message
;
855 if (err_ss
) *err_ss
<< "Configuration option not found: '" << key
<< "'";
860 int md_config_t::get_val(const std::string
&key
, char **buf
, int len
) const
862 Mutex::Locker
l(lock
);
863 return _get_val(key
, buf
,len
);
866 Option::value_t
md_config_t::get_val_generic(const std::string
&key
) const
868 Mutex::Locker
l(lock
);
869 return _get_val(key
);
872 Option::value_t
md_config_t::_get_val(const std::string
&key
) const
874 assert(lock
.is_locked());
877 return Option::value_t(boost::blank());
880 // In key names, leading and trailing whitespace are not significant.
881 string
k(ConfFile::normalize_key_name(key
));
883 const auto &opt_iter
= schema
.find(k
);
884 if (opt_iter
!= schema
.end()) {
885 // Using .at() is safe because all keys in the schema always have
886 // entries in ::values
889 return Option::value_t(boost::blank());
893 int md_config_t::_get_val(const std::string
&key
, std::string
*value
) const {
894 assert(lock
.is_locked());
896 std::string
normalized_key(ConfFile::normalize_key_name(key
));
897 Option::value_t config_value
= _get_val(normalized_key
.c_str());
898 if (!boost::get
<boost::blank
>(&config_value
)) {
900 if (bool *flag
= boost::get
<bool>(&config_value
)) {
901 oss
<< (*flag
? "true" : "false");
902 } else if (double *dp
= boost::get
<double>(&config_value
)) {
903 oss
<< std::fixed
<< *dp
;
913 int md_config_t::_get_val(const std::string
&key
, char **buf
, int len
) const
915 assert(lock
.is_locked());
921 if (_get_val(key
, &val
) == 0) {
922 int l
= val
.length() + 1;
924 *buf
= (char*)malloc(l
);
927 strncpy(*buf
, val
.c_str(), l
);
930 snprintf(*buf
, len
, "%s", val
.c_str());
931 return (l
> len
) ? -ENAMETOOLONG
: 0;
934 string
k(ConfFile::normalize_key_name(key
));
936 for (size_t o
= 0; o
< subsys
.get_num(); o
++) {
937 std::string as_option
= "debug_" + subsys
.get_name(o
);
938 if (k
== as_option
) {
940 *buf
= (char*)malloc(20);
943 int l
= snprintf(*buf
, len
, "%d/%d", subsys
.get_log_level(o
), subsys
.get_gather_level(o
));
944 return (l
== len
) ? -ENAMETOOLONG
: 0;
948 // couldn't find a configuration option with key 'k'
952 void md_config_t::get_all_keys(std::vector
<std::string
> *keys
) const {
953 const std::string
negative_flag_prefix("no_");
956 keys
->reserve(schema
.size());
957 for (const auto &i
: schema
) {
958 const Option
&opt
= i
.second
;
959 keys
->push_back(opt
.name
);
960 if (opt
.type
== Option::TYPE_BOOL
) {
961 keys
->push_back(negative_flag_prefix
+ opt
.name
);
964 for (size_t i
= 0; i
< subsys
.get_num(); ++i
) {
965 keys
->push_back("debug_" + subsys
.get_name(i
));
969 /* The order of the sections here is important. The first section in the
970 * vector is the "highest priority" section; if we find it there, we'll stop
971 * looking. The lowest priority section is the one we look in only if all
972 * others had nothing. This should always be the global section.
974 void md_config_t::get_my_sections(std::vector
<std::string
> §ions
) const
976 Mutex::Locker
l(lock
);
977 _get_my_sections(sections
);
980 void md_config_t::_get_my_sections(std::vector
<std::string
> §ions
) const
982 assert(lock
.is_locked());
983 sections
.push_back(name
.to_str());
985 sections
.push_back(name
.get_type_name());
987 sections
.push_back("global");
990 // Return a list of all sections
991 int md_config_t::get_all_sections(std::vector
<std::string
> §ions
) const
993 Mutex::Locker
l(lock
);
994 for (ConfFile::const_section_iter_t s
= cf
.sections_begin();
995 s
!= cf
.sections_end(); ++s
) {
996 sections
.push_back(s
->first
);
1001 int md_config_t::get_val_from_conf_file(const std::vector
<std::string
> §ions
,
1002 const std::string
&key
, std::string
&out
, bool emeta
) const
1004 Mutex::Locker
l(lock
);
1005 return _get_val_from_conf_file(sections
, key
, out
, emeta
);
1008 int md_config_t::_get_val_from_conf_file(const std::vector
<std::string
> §ions
,
1009 const std::string
&key
, std::string
&out
, bool emeta
) const
1011 assert(lock
.is_locked());
1012 std::vector
<std::string
>::const_iterator s
= sections
.begin();
1013 std::vector
<std::string
>::const_iterator s_end
= sections
.end();
1014 for (; s
!= s_end
; ++s
) {
1015 int ret
= cf
.read(s
->c_str(), key
, out
);
1018 expand_meta(out
, &std::cerr
);
1021 else if (ret
!= -ENOENT
)
1027 int md_config_t::set_val_impl(const std::string
&raw_val
, const Option
&opt
,
1028 std::string
*error_message
)
1030 assert(lock
.is_locked());
1032 std::string val
= raw_val
;
1034 int r
= opt
.pre_validate(&val
, error_message
);
1039 Option::value_t new_value
;
1040 if (opt
.type
== Option::TYPE_INT
) {
1041 int64_t f
= strict_si_cast
<int64_t>(val
.c_str(), error_message
);
1042 if (!error_message
->empty()) {
1046 } else if (opt
.type
== Option::TYPE_UINT
) {
1047 uint64_t f
= strict_si_cast
<uint64_t>(val
.c_str(), error_message
);
1048 if (!error_message
->empty()) {
1052 } else if (opt
.type
== Option::TYPE_STR
) {
1054 } else if (opt
.type
== Option::TYPE_FLOAT
) {
1055 double f
= strict_strtod(val
.c_str(), error_message
);
1056 if (!error_message
->empty()) {
1061 } else if (opt
.type
== Option::TYPE_BOOL
) {
1062 if (strcasecmp(val
.c_str(), "false") == 0) {
1064 } else if (strcasecmp(val
.c_str(), "true") == 0) {
1067 int b
= strict_strtol(val
.c_str(), 10, error_message
);
1068 if (!error_message
->empty()) {
1073 } else if (opt
.type
== Option::TYPE_ADDR
) {
1075 if (!addr
.parse(val
.c_str())){
1079 } else if (opt
.type
== Option::TYPE_UUID
) {
1081 if (!uuid
.parse(val
.c_str())) {
1089 r
= opt
.validate(new_value
, error_message
);
1095 // Apply the value to its entry in the `values` map
1096 values
[opt
.name
] = new_value
;
1098 // Apply the value to its legacy field, if it has one
1099 auto legacy_ptr_iter
= legacy_values
.find(std::string(opt
.name
));
1100 if (legacy_ptr_iter
!= legacy_values
.end()) {
1101 update_legacy_val(opt
, legacy_ptr_iter
->second
);
1104 changed
.insert(opt
.name
);
1109 * Handles assigning from a variant-of-types to a variant-of-pointers-to-types
1111 class assign_visitor
: public boost::static_visitor
<>
1114 Option::value_t val
;
1117 assign_visitor(md_config_t
*conf_
, Option::value_t val_
)
1118 : conf(conf_
), val(val_
)
1121 template <typename T
>
1122 void operator()( T
md_config_t::* ptr
) const
1124 T
*member
= const_cast<T
*>(&(conf
->*(boost::get
<const T
md_config_t::*>(ptr
))));
1126 *member
= boost::get
<T
>(val
);
1130 void md_config_t::update_legacy_val(const Option
&opt
,
1131 md_config_t::member_ptr_t member_ptr
)
1133 if (boost::get
<boost::blank
>(&values
.at(opt
.name
))) {
1134 // This shouldn't happen, but if it does then just don't even
1135 // try to assign to the legacy field.
1139 boost::apply_visitor(assign_visitor(this, values
.at(opt
.name
)), member_ptr
);
1143 static const char *CONF_METAVARIABLES
[] = {
1144 "data_dir", // put this first: it may contain some of the others
1145 "cluster", "type", "name", "host", "num", "id", "pid", "cctid"
1147 static const int NUM_CONF_METAVARIABLES
=
1148 (sizeof(CONF_METAVARIABLES
) / sizeof(CONF_METAVARIABLES
[0]));
1150 void md_config_t::expand_all_meta()
1152 // Expand all metavariables
1154 for (const auto &i
: schema
) {
1155 const Option
&opt
= i
.second
;
1157 if (opt
.type
== Option::TYPE_STR
) {
1158 list
<const Option
*> stack
;
1159 std::string
*str
= boost::get
<std::string
>(&(values
.at(opt
.name
)));
1160 assert(str
!= nullptr); // Non-string values should never get in
1161 expand_meta(*str
, &opt
, stack
, &oss
);
1167 bool md_config_t::expand_meta(std::string
&val
,
1168 std::ostream
*oss
) const
1170 list
<const Option
*> stack
;
1171 return expand_meta(val
, NULL
, stack
, oss
);
1174 bool md_config_t::expand_meta(std::string
&origval
,
1176 std::list
<const Option
*> stack
,
1177 std::ostream
*oss
) const
1179 assert(lock
.is_locked());
1181 // no $ means no variable expansion is necessary
1182 if (origval
.find("$") == string::npos
)
1185 // ignore an expansion loop and create a human readable
1188 for (const auto stack_ptr
: stack
) {
1189 if (opt
->name
== stack_ptr
->name
) {
1190 *oss
<< "variable expansion loop at "
1191 << opt
->name
<< "=" << origval
<< std::endl
;
1192 *oss
<< "expansion stack: " << std::endl
;
1193 for (const auto j
: stack
) {
1195 _get_val(j
->name
, &val
);
1196 *oss
<< j
->name
<< "=" << val
<< std::endl
;
1202 stack
.push_front(opt
);
1205 bool found_meta
= false;
1207 string val
= origval
;
1208 for (string::size_type s
= 0; s
< val
.size(); ) {
1209 if (val
[s
] != '$') {
1214 // try to parse the variable name into var, either \$\{(.+)\} or
1216 const char *valid_chars
= "abcdefghijklmnopqrstuvwxyz_";
1219 if (val
[s
+1] == '{') {
1221 endpos
= val
.find_first_not_of(valid_chars
, s
+2);
1222 if (endpos
!= std::string::npos
&&
1223 val
[endpos
] == '}') {
1224 var
= val
.substr(s
+2, endpos
-s
-2);
1229 endpos
= val
.find_first_not_of(valid_chars
, s
+1);
1230 if (endpos
!= std::string::npos
)
1231 var
= val
.substr(s
+1, endpos
-s
-1);
1233 var
= val
.substr(s
+1);
1236 bool expanded
= false;
1238 // special metavariable?
1239 for (int i
= 0; i
< NUM_CONF_METAVARIABLES
; ++i
) {
1240 if (var
!= CONF_METAVARIABLES
[i
])
1242 //cout << " meta match of " << var << " " << CONF_METAVARIABLES[i] << std::endl;
1244 out
+= name
.get_type_name();
1245 else if (var
== "cluster")
1247 else if (var
== "name")
1248 out
+= name
.to_cstr();
1249 else if (var
== "host")
1252 out
+= ceph_get_short_hostname();
1256 else if (var
== "num")
1257 out
+= name
.get_id().c_str();
1258 else if (var
== "id")
1259 out
+= name
.get_id().c_str();
1260 else if (var
== "pid")
1261 out
+= stringify(getpid());
1262 else if (var
== "cctid")
1263 out
+= stringify((unsigned long long)this);
1264 else if (var
== "data_dir") {
1265 if (data_dir_option
.length()) {
1267 _get_val(data_dir_option
.c_str(), &vv
, -1);
1270 expand_meta(tmp
, NULL
, stack
, oss
);
1273 // this isn't really right, but it'll result in a mangled
1274 // non-existent path that will fail any search list
1278 ceph_abort(); // unreachable
1284 const auto other_opt_iter
= schema
.find(var
);
1285 if (other_opt_iter
!= schema
.end()) {
1286 const Option
&other_opt
= other_opt_iter
->second
;
1287 if (other_opt
.type
== Option::TYPE_STR
) {
1288 // The referenced option is a string, it may need substitution
1289 // before inserting.
1290 Option::value_t
*other_val_ptr
= const_cast<Option::value_t
*>(&(values
.at(other_opt
.name
)));
1291 std::string
*other_opt_val
= boost::get
<std::string
>(other_val_ptr
);
1292 expand_meta(*other_opt_val
, &other_opt
, stack
, oss
);
1293 out
+= *other_opt_val
;
1295 // The referenced option is not a string: retrieve and insert
1296 // its stringized form.
1298 _get_val(other_opt
.name
, &vv
, -1);
1314 // override the original value with the expanded value
1319 void md_config_t::diff(
1320 const md_config_t
*other
,
1321 map
<string
, pair
<string
, string
> > *diff
,
1322 set
<string
> *unknown
)
1324 diff_helper(other
, diff
, unknown
);
1326 void md_config_t::diff(
1327 const md_config_t
*other
,
1328 map
<string
, pair
<string
, string
> > *diff
,
1329 set
<string
> *unknown
, const string
& setting
)
1331 diff_helper(other
, diff
, unknown
, setting
);
1334 void md_config_t::diff_helper(
1335 const md_config_t
*other
,
1336 map
<string
,pair
<string
,string
> > *diff
,
1337 set
<string
> *unknown
, const string
& setting
)
1339 Mutex::Locker
l(lock
);
1341 char local_buf
[4096];
1342 char other_buf
[4096];
1343 for (const auto &i
: schema
) {
1344 const Option
&opt
= i
.second
;
1345 if (!setting
.empty()) {
1346 if (setting
!= opt
.name
) {
1350 memset(local_buf
, 0, sizeof(local_buf
));
1351 memset(other_buf
, 0, sizeof(other_buf
));
1353 char *other_val
= other_buf
;
1354 int err
= other
->get_val(opt
.name
, &other_val
, sizeof(other_buf
));
1356 if (err
== -ENOENT
) {
1357 unknown
->insert(opt
.name
);
1362 char *local_val
= local_buf
;
1363 err
= _get_val(opt
.name
, &local_val
, sizeof(local_buf
));
1367 if (strcmp(local_val
, other_val
))
1368 diff
->insert(make_pair(opt
.name
, make_pair(local_val
, other_val
)));
1369 else if (!setting
.empty()) {
1370 diff
->insert(make_pair(opt
.name
, make_pair(local_val
, other_val
)));
1376 void md_config_t::complain_about_parse_errors(CephContext
*cct
)
1378 ::complain_about_parse_errors(cct
, &parse_errors
);