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 parse_option(args
, i
, NULL
);
501 _show_config(&cout
, NULL
);
505 if (show_config_value
) {
507 int r
= _get_val(show_config_value_arg
.c_str(), &buf
, -1);
510 std::cerr
<< "failed to get config option '" <<
511 show_config_value_arg
<< "': option not found" << std::endl
;
513 std::cerr
<< "failed to get config option '" <<
514 show_config_value_arg
<< "': " << cpp_strerror(r
) << std::endl
;
518 expand_meta(s
, &std::cerr
);
519 std::cout
<< s
<< std::endl
;
526 int md_config_t::parse_option(std::vector
<const char*>& args
,
527 std::vector
<const char*>::iterator
& i
,
535 for (o
= 0; o
< subsys
.get_num(); o
++) {
536 std::string
as_option("--");
537 as_option
+= "debug_";
538 as_option
+= subsys
.get_name(o
);
539 if (ceph_argparse_witharg(args
, i
, &val
,
540 as_option
.c_str(), (char*)NULL
)) {
542 int r
= sscanf(val
.c_str(), "%d/%d", &log
, &gather
);
546 // cout << "subsys " << subsys.get_name(o) << " log " << log << " gather " << gather << std::endl;
547 subsys
.set_log_level(o
, log
);
548 subsys
.set_gather_level(o
, gather
);
550 *oss
<< "debug_" << subsys
.get_name(o
) << "=" << log
<< "/" << gather
<< " ";
555 if (o
< subsys
.get_num()) {
559 std::string option_name
;
560 std::string error_message
;
562 for (const auto& opt_iter
: schema
) {
563 const Option
&opt
= opt_iter
.second
;
565 std::string
as_option("--");
566 as_option
+= opt
.name
;
567 option_name
= opt
.name
;
568 if (opt
.type
== Option::TYPE_BOOL
) {
570 if (ceph_argparse_binary_flag(args
, i
, &res
, oss
, as_option
.c_str(),
573 ret
= set_val_impl("false", opt
, &error_message
);
575 ret
= set_val_impl("true", opt
, &error_message
);
580 std::string
no("--no-");
582 if (ceph_argparse_flag(args
, i
, no
.c_str(), (char*)NULL
)) {
583 ret
= set_val_impl("false", opt
, &error_message
);
587 } else if (ceph_argparse_witharg(args
, i
, &val
, err
,
588 as_option
.c_str(), (char*)NULL
)) {
589 if (!err
.str().empty()) {
590 error_message
= err
.str();
594 if (oss
&& ((!opt
.is_safe()) &&
595 (observers
.find(opt
.name
) == observers
.end()))) {
596 *oss
<< "You cannot change " << opt
.name
<< " using injectargs.\n";
599 ret
= set_val_impl(val
, opt
, &error_message
);
605 if (ret
!= 0 || !error_message
.empty()) {
606 assert(!option_name
.empty());
608 *oss
<< "Parse error setting " << option_name
<< " to '"
609 << val
<< "' using injectargs";
610 if (!error_message
.empty()) {
611 *oss
<< " (" << error_message
<< ")";
615 cerr
<< "parse error setting '" << option_name
<< "' to '"
617 if (!error_message
.empty()) {
618 cerr
<< " (" << error_message
<< ")";
620 cerr
<< "\n" << std::endl
;
624 if (o
== schema
.size()) {
631 int md_config_t::parse_injectargs(std::vector
<const char*>& args
,
634 assert(lock
.is_locked());
636 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end(); ) {
637 int r
= parse_option(args
, i
, oss
);
644 void md_config_t::apply_changes(std::ostream
*oss
)
646 Mutex::Locker
l(lock
);
648 * apply changes until the cluster name is assigned
654 bool md_config_t::_internal_field(const string
& s
)
656 if (s
== "internal_safe_to_start_threads")
661 void md_config_t::_apply_changes(std::ostream
*oss
)
663 /* Maps observers to the configuration options that they care about which
665 typedef std::map
< md_config_obs_t
*, std::set
<std::string
> > rev_obs_map_t
;
669 // expand_all_meta could have modified anything. Copy it all out again.
670 for (const auto &i
: legacy_values
) {
671 const auto &name
= i
.first
;
672 const auto &option
= schema
.at(name
);
675 update_legacy_val(option
, ptr
);
678 // create the reverse observer mapping, mapping observers to the set of
679 // changed keys that they'll get.
681 std::set
<std::string
> empty_set
;
683 char *bufptr
= (char*)buf
;
684 for (changed_set_t::const_iterator c
= changed
.begin();
685 c
!= changed
.end(); ++c
) {
686 const std::string
&key(*c
);
687 pair
< obs_map_t::iterator
, obs_map_t::iterator
>
688 range(observers
.equal_range(key
));
690 (!_get_val(key
.c_str(), &bufptr
, sizeof(buf
))) &&
691 !_internal_field(key
)) {
692 (*oss
) << key
<< " = '" << buf
<< "' ";
693 if (range
.first
== range
.second
) {
694 (*oss
) << "(not observed, change may require restart) ";
697 for (obs_map_t::iterator r
= range
.first
; r
!= range
.second
; ++r
) {
698 rev_obs_map_t::value_type
robs_val(r
->second
, empty_set
);
699 pair
< rev_obs_map_t::iterator
, bool > robs_ret(robs
.insert(robs_val
));
700 std::set
<std::string
> &keys(robs_ret
.first
->second
);
707 // Make any pending observer callbacks
708 for (rev_obs_map_t::const_iterator r
= robs
.begin(); r
!= robs
.end(); ++r
) {
709 md_config_obs_t
*obs
= r
->first
;
710 obs
->handle_conf_change(this, r
->second
);
715 void md_config_t::call_all_observers()
717 std::map
<md_config_obs_t
*,std::set
<std::string
> > obs
;
719 Mutex::Locker
l(lock
);
723 for (auto r
= observers
.begin(); r
!= observers
.end(); ++r
) {
724 obs
[r
->second
].insert(r
->first
);
727 for (auto p
= obs
.begin();
730 p
->first
->handle_conf_change(this, p
->second
);
734 int md_config_t::injectargs(const std::string
& s
, std::ostream
*oss
)
737 Mutex::Locker
l(lock
);
738 char b
[s
.length()+1];
739 strcpy(b
, s
.c_str());
740 std::vector
<const char*> nargs
;
744 while (*p
&& *p
!= ' ') p
++;
748 while (*p
&& *p
== ' ') p
++;
750 ret
= parse_injectargs(nargs
, oss
);
751 if (!nargs
.empty()) {
752 *oss
<< " failed to parse arguments: ";
754 for (std::vector
<const char*>::const_iterator i
= nargs
.begin();
755 i
!= nargs
.end(); ++i
) {
756 *oss
<< prefix
<< *i
;
766 void md_config_t::set_val_or_die(const std::string
&key
,
767 const std::string
&val
,
770 std::stringstream err
;
771 int ret
= set_val(key
, val
, meta
, &err
);
773 std::cerr
<< "set_val_or_die(" << key
<< "): " << err
.str();
778 int md_config_t::set_val(const std::string
&key
, const char *val
,
779 bool meta
, std::stringstream
*err_ss
)
781 Mutex::Locker
l(lock
);
783 if (err_ss
) *err_ss
<< "No key specified";
792 expand_meta(v
, &std::cerr
);
794 string
k(ConfFile::normalize_key_name(key
));
797 if (strncmp(k
.c_str(), "debug_", 6) == 0) {
798 for (size_t o
= 0; o
< subsys
.get_num(); o
++) {
799 std::string as_option
= "debug_" + subsys
.get_name(o
);
800 if (k
== as_option
) {
802 int r
= sscanf(v
.c_str(), "%d/%d", &log
, &gather
);
807 subsys
.set_log_level(o
, log
);
808 subsys
.set_gather_level(o
, gather
);
809 if (err_ss
) *err_ss
<< "Set " << k
<< " to " << log
<< "/" << gather
;
813 *err_ss
<< "Invalid debug level, should be <int> or <int>/<int>";
820 const auto &opt_iter
= schema
.find(k
);
821 if (opt_iter
!= schema
.end()) {
822 const Option
&opt
= opt_iter
->second
;
823 if ((!opt
.is_safe()) && internal_safe_to_start_threads
) {
824 // If threads have been started and the option is not thread safe
825 if (observers
.find(opt
.name
) == observers
.end()) {
826 // And there is no observer to safely change it...
828 if (err_ss
) *err_ss
<< "Configuration option '" << key
<< "' may "
829 "not be modified at runtime";
834 std::string error_message
;
835 int r
= set_val_impl(v
, opt
, &error_message
);
837 if (err_ss
) *err_ss
<< "Set " << opt
.name
<< " to " << v
;
839 if (err_ss
) *err_ss
<< error_message
;
844 if (err_ss
) *err_ss
<< "Configuration option not found: '" << key
<< "'";
849 int md_config_t::get_val(const std::string
&key
, char **buf
, int len
) const
851 Mutex::Locker
l(lock
);
852 return _get_val(key
, buf
,len
);
855 Option::value_t
md_config_t::get_val_generic(const std::string
&key
) const
857 Mutex::Locker
l(lock
);
858 return _get_val(key
);
861 Option::value_t
md_config_t::_get_val(const std::string
&key
) const
863 assert(lock
.is_locked());
866 return Option::value_t(boost::blank());
869 // In key names, leading and trailing whitespace are not significant.
870 string
k(ConfFile::normalize_key_name(key
));
872 const auto &opt_iter
= schema
.find(k
);
873 if (opt_iter
!= schema
.end()) {
874 // Using .at() is safe because all keys in the schema always have
875 // entries in ::values
878 return Option::value_t(boost::blank());
882 int md_config_t::_get_val(const std::string
&key
, std::string
*value
) const {
883 assert(lock
.is_locked());
885 std::string
normalized_key(ConfFile::normalize_key_name(key
));
886 Option::value_t config_value
= _get_val(normalized_key
.c_str());
887 if (!boost::get
<boost::blank
>(&config_value
)) {
889 if (bool *flag
= boost::get
<bool>(&config_value
)) {
890 oss
<< (*flag
? "true" : "false");
891 } else if (double *dp
= boost::get
<double>(&config_value
)) {
892 oss
<< std::fixed
<< *dp
;
902 int md_config_t::_get_val(const std::string
&key
, char **buf
, int len
) const
904 assert(lock
.is_locked());
910 if (_get_val(key
, &val
) == 0) {
911 int l
= val
.length() + 1;
913 *buf
= (char*)malloc(l
);
916 strncpy(*buf
, val
.c_str(), l
);
919 snprintf(*buf
, len
, "%s", val
.c_str());
920 return (l
> len
) ? -ENAMETOOLONG
: 0;
923 string
k(ConfFile::normalize_key_name(key
));
925 for (size_t o
= 0; o
< subsys
.get_num(); o
++) {
926 std::string as_option
= "debug_" + subsys
.get_name(o
);
927 if (k
== as_option
) {
929 *buf
= (char*)malloc(20);
932 int l
= snprintf(*buf
, len
, "%d/%d", subsys
.get_log_level(o
), subsys
.get_gather_level(o
));
933 return (l
== len
) ? -ENAMETOOLONG
: 0;
937 // couldn't find a configuration option with key 'k'
941 void md_config_t::get_all_keys(std::vector
<std::string
> *keys
) const {
942 const std::string
negative_flag_prefix("no_");
945 keys
->reserve(schema
.size());
946 for (const auto &i
: schema
) {
947 const Option
&opt
= i
.second
;
948 keys
->push_back(opt
.name
);
949 if (opt
.type
== Option::TYPE_BOOL
) {
950 keys
->push_back(negative_flag_prefix
+ opt
.name
);
953 for (size_t i
= 0; i
< subsys
.get_num(); ++i
) {
954 keys
->push_back("debug_" + subsys
.get_name(i
));
958 /* The order of the sections here is important. The first section in the
959 * vector is the "highest priority" section; if we find it there, we'll stop
960 * looking. The lowest priority section is the one we look in only if all
961 * others had nothing. This should always be the global section.
963 void md_config_t::get_my_sections(std::vector
<std::string
> §ions
) const
965 Mutex::Locker
l(lock
);
966 _get_my_sections(sections
);
969 void md_config_t::_get_my_sections(std::vector
<std::string
> §ions
) const
971 assert(lock
.is_locked());
972 sections
.push_back(name
.to_str());
974 sections
.push_back(name
.get_type_name());
976 sections
.push_back("global");
979 // Return a list of all sections
980 int md_config_t::get_all_sections(std::vector
<std::string
> §ions
) const
982 Mutex::Locker
l(lock
);
983 for (ConfFile::const_section_iter_t s
= cf
.sections_begin();
984 s
!= cf
.sections_end(); ++s
) {
985 sections
.push_back(s
->first
);
990 int md_config_t::get_val_from_conf_file(const std::vector
<std::string
> §ions
,
991 const std::string
&key
, std::string
&out
, bool emeta
) const
993 Mutex::Locker
l(lock
);
994 return _get_val_from_conf_file(sections
, key
, out
, emeta
);
997 int md_config_t::_get_val_from_conf_file(const std::vector
<std::string
> §ions
,
998 const std::string
&key
, std::string
&out
, bool emeta
) const
1000 assert(lock
.is_locked());
1001 std::vector
<std::string
>::const_iterator s
= sections
.begin();
1002 std::vector
<std::string
>::const_iterator s_end
= sections
.end();
1003 for (; s
!= s_end
; ++s
) {
1004 int ret
= cf
.read(s
->c_str(), key
, out
);
1007 expand_meta(out
, &std::cerr
);
1010 else if (ret
!= -ENOENT
)
1016 int md_config_t::set_val_impl(const std::string
&raw_val
, const Option
&opt
,
1017 std::string
*error_message
)
1019 assert(lock
.is_locked());
1021 std::string val
= raw_val
;
1023 int r
= opt
.pre_validate(&val
, error_message
);
1028 Option::value_t new_value
;
1029 if (opt
.type
== Option::TYPE_INT
) {
1030 int64_t f
= strict_si_cast
<int64_t>(val
.c_str(), error_message
);
1031 if (!error_message
->empty()) {
1035 } else if (opt
.type
== Option::TYPE_UINT
) {
1036 uint64_t f
= strict_si_cast
<uint64_t>(val
.c_str(), error_message
);
1037 if (!error_message
->empty()) {
1041 } else if (opt
.type
== Option::TYPE_STR
) {
1043 } else if (opt
.type
== Option::TYPE_FLOAT
) {
1044 double f
= strict_strtod(val
.c_str(), error_message
);
1045 if (!error_message
->empty()) {
1050 } else if (opt
.type
== Option::TYPE_BOOL
) {
1051 if (strcasecmp(val
.c_str(), "false") == 0) {
1053 } else if (strcasecmp(val
.c_str(), "true") == 0) {
1056 int b
= strict_strtol(val
.c_str(), 10, error_message
);
1057 if (!error_message
->empty()) {
1062 } else if (opt
.type
== Option::TYPE_ADDR
) {
1064 if (!addr
.parse(val
.c_str())){
1068 } else if (opt
.type
== Option::TYPE_UUID
) {
1070 if (!uuid
.parse(val
.c_str())) {
1078 r
= opt
.validate(new_value
, error_message
);
1084 // Apply the value to its entry in the `values` map
1085 values
[opt
.name
] = new_value
;
1087 // Apply the value to its legacy field, if it has one
1088 auto legacy_ptr_iter
= legacy_values
.find(std::string(opt
.name
));
1089 if (legacy_ptr_iter
!= legacy_values
.end()) {
1090 update_legacy_val(opt
, legacy_ptr_iter
->second
);
1093 changed
.insert(opt
.name
);
1098 * Handles assigning from a variant-of-types to a variant-of-pointers-to-types
1100 class assign_visitor
: public boost::static_visitor
<>
1103 Option::value_t val
;
1106 assign_visitor(md_config_t
*conf_
, Option::value_t val_
)
1107 : conf(conf_
), val(val_
)
1110 template <typename T
>
1111 void operator()( T
md_config_t::* ptr
) const
1113 T
*member
= const_cast<T
*>(&(conf
->*(boost::get
<const T
md_config_t::*>(ptr
))));
1115 *member
= boost::get
<T
>(val
);
1119 void md_config_t::update_legacy_val(const Option
&opt
,
1120 md_config_t::member_ptr_t member_ptr
)
1122 if (boost::get
<boost::blank
>(&values
.at(opt
.name
))) {
1123 // This shouldn't happen, but if it does then just don't even
1124 // try to assign to the legacy field.
1128 boost::apply_visitor(assign_visitor(this, values
.at(opt
.name
)), member_ptr
);
1132 static const char *CONF_METAVARIABLES
[] = {
1133 "data_dir", // put this first: it may contain some of the others
1134 "cluster", "type", "name", "host", "num", "id", "pid", "cctid"
1136 static const int NUM_CONF_METAVARIABLES
=
1137 (sizeof(CONF_METAVARIABLES
) / sizeof(CONF_METAVARIABLES
[0]));
1139 void md_config_t::expand_all_meta()
1141 // Expand all metavariables
1143 for (const auto &i
: schema
) {
1144 const Option
&opt
= i
.second
;
1146 if (opt
.type
== Option::TYPE_STR
) {
1147 list
<const Option
*> stack
;
1148 std::string
*str
= boost::get
<std::string
>(&(values
.at(opt
.name
)));
1149 assert(str
!= nullptr); // Non-string values should never get in
1150 expand_meta(*str
, &opt
, stack
, &oss
);
1156 bool md_config_t::expand_meta(std::string
&val
,
1157 std::ostream
*oss
) const
1159 list
<const Option
*> stack
;
1160 return expand_meta(val
, NULL
, stack
, oss
);
1163 bool md_config_t::expand_meta(std::string
&origval
,
1165 std::list
<const Option
*> stack
,
1166 std::ostream
*oss
) const
1168 assert(lock
.is_locked());
1170 // no $ means no variable expansion is necessary
1171 if (origval
.find("$") == string::npos
)
1174 // ignore an expansion loop and create a human readable
1177 for (const auto stack_ptr
: stack
) {
1178 if (opt
->name
== stack_ptr
->name
) {
1179 *oss
<< "variable expansion loop at "
1180 << opt
->name
<< "=" << origval
<< std::endl
;
1181 *oss
<< "expansion stack: " << std::endl
;
1182 for (const auto j
: stack
) {
1184 _get_val(j
->name
, &val
);
1185 *oss
<< j
->name
<< "=" << val
<< std::endl
;
1191 stack
.push_front(opt
);
1194 bool found_meta
= false;
1196 string val
= origval
;
1197 for (string::size_type s
= 0; s
< val
.size(); ) {
1198 if (val
[s
] != '$') {
1203 // try to parse the variable name into var, either \$\{(.+)\} or
1205 const char *valid_chars
= "abcdefghijklmnopqrstuvwxyz_";
1208 if (val
[s
+1] == '{') {
1210 endpos
= val
.find_first_not_of(valid_chars
, s
+2);
1211 if (endpos
!= std::string::npos
&&
1212 val
[endpos
] == '}') {
1213 var
= val
.substr(s
+2, endpos
-s
-2);
1218 endpos
= val
.find_first_not_of(valid_chars
, s
+1);
1219 if (endpos
!= std::string::npos
)
1220 var
= val
.substr(s
+1, endpos
-s
-1);
1222 var
= val
.substr(s
+1);
1225 bool expanded
= false;
1227 // special metavariable?
1228 for (int i
= 0; i
< NUM_CONF_METAVARIABLES
; ++i
) {
1229 if (var
!= CONF_METAVARIABLES
[i
])
1231 //cout << " meta match of " << var << " " << CONF_METAVARIABLES[i] << std::endl;
1233 out
+= name
.get_type_name();
1234 else if (var
== "cluster")
1236 else if (var
== "name")
1237 out
+= name
.to_cstr();
1238 else if (var
== "host")
1241 out
+= ceph_get_short_hostname();
1245 else if (var
== "num")
1246 out
+= name
.get_id().c_str();
1247 else if (var
== "id")
1248 out
+= name
.get_id().c_str();
1249 else if (var
== "pid")
1250 out
+= stringify(getpid());
1251 else if (var
== "cctid")
1252 out
+= stringify((unsigned long long)this);
1253 else if (var
== "data_dir") {
1254 if (data_dir_option
.length()) {
1256 _get_val(data_dir_option
.c_str(), &vv
, -1);
1259 expand_meta(tmp
, NULL
, stack
, oss
);
1262 // this isn't really right, but it'll result in a mangled
1263 // non-existent path that will fail any search list
1267 ceph_abort(); // unreachable
1273 const auto other_opt_iter
= schema
.find(var
);
1274 if (other_opt_iter
!= schema
.end()) {
1275 const Option
&other_opt
= other_opt_iter
->second
;
1276 if (other_opt
.type
== Option::TYPE_STR
) {
1277 // The referenced option is a string, it may need substitution
1278 // before inserting.
1279 Option::value_t
*other_val_ptr
= const_cast<Option::value_t
*>(&(values
.at(other_opt
.name
)));
1280 std::string
*other_opt_val
= boost::get
<std::string
>(other_val_ptr
);
1281 expand_meta(*other_opt_val
, &other_opt
, stack
, oss
);
1282 out
+= *other_opt_val
;
1284 // The referenced option is not a string: retrieve and insert
1285 // its stringized form.
1287 _get_val(other_opt
.name
, &vv
, -1);
1303 // override the original value with the expanded value
1308 void md_config_t::diff(
1309 const md_config_t
*other
,
1310 map
<string
, pair
<string
, string
> > *diff
,
1311 set
<string
> *unknown
)
1313 diff_helper(other
, diff
, unknown
);
1315 void md_config_t::diff(
1316 const md_config_t
*other
,
1317 map
<string
, pair
<string
, string
> > *diff
,
1318 set
<string
> *unknown
, const string
& setting
)
1320 diff_helper(other
, diff
, unknown
, setting
);
1323 void md_config_t::diff_helper(
1324 const md_config_t
*other
,
1325 map
<string
,pair
<string
,string
> > *diff
,
1326 set
<string
> *unknown
, const string
& setting
)
1328 Mutex::Locker
l(lock
);
1330 char local_buf
[4096];
1331 char other_buf
[4096];
1332 for (const auto &i
: schema
) {
1333 const Option
&opt
= i
.second
;
1334 if (!setting
.empty()) {
1335 if (setting
!= opt
.name
) {
1339 memset(local_buf
, 0, sizeof(local_buf
));
1340 memset(other_buf
, 0, sizeof(other_buf
));
1342 char *other_val
= other_buf
;
1343 int err
= other
->get_val(opt
.name
, &other_val
, sizeof(other_buf
));
1345 if (err
== -ENOENT
) {
1346 unknown
->insert(opt
.name
);
1351 char *local_val
= local_buf
;
1352 err
= _get_val(opt
.name
, &local_val
, sizeof(local_buf
));
1356 if (strcmp(local_val
, other_val
))
1357 diff
->insert(make_pair(opt
.name
, make_pair(local_val
, other_val
)));
1358 else if (!setting
.empty()) {
1359 diff
->insert(make_pair(opt
.name
, make_pair(local_val
, other_val
)));
1365 void md_config_t::complain_about_parse_errors(CephContext
*cct
)
1367 ::complain_about_parse_errors(cct
, &parse_errors
);