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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
|
// -*- 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) 2004-2006 Sage Weil <sage@newdream.net>
*
* 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 "auth/Auth.h"
#include "common/ConfUtils.h"
#include "common/ceph_argparse.h"
#include "common/common_init.h"
#include "common/config.h"
#include "common/static_assert.h"
#include "common/strtol.h"
#include "common/version.h"
#include "include/str_list.h"
#include "include/types.h"
#include "include/stringify.h"
#include "msg/msg_types.h"
#include "osd/osd_types.h"
#include "common/errno.h"
#include "include/assert.h"
#include <errno.h>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
/* Don't use standard Ceph logging in this file.
* We can't use logging until it's initialized, and a lot of the necessary
* initialization happens here.
*/
#undef dout
#undef ldout
#undef pdout
#undef derr
#undef lderr
#undef generic_dout
#undef dendl
using std::map;
using std::list;
using std::multimap;
using std::ostringstream;
using std::pair;
using std::set;
using std::string;
const char *CEPH_CONF_FILE_DEFAULT = "/etc/ceph/$cluster.conf, ~/.ceph/$cluster.conf, $cluster.conf";
// file layouts
struct ceph_file_layout g_default_file_layout = {
fl_stripe_unit: init_le32(1<<22),
fl_stripe_count: init_le32(1),
fl_object_size: init_le32(1<<22),
fl_cas_hash: init_le32(0),
fl_object_stripe_unit: init_le32(0),
fl_unused: init_le32(-1),
fl_pg_pool : init_le32(-1),
};
#define _STR(x) #x
#define STRINGIFY(x) _STR(x)
void *config_option::conf_ptr(md_config_t *conf) const
{
void *v = (void*)(((char*)conf) + md_conf_off);
return v;
}
const void *config_option::conf_ptr(const md_config_t *conf) const
{
const void *v = (const void*)(((const char*)conf) + md_conf_off);
return v;
}
struct config_option config_optionsp[] = {
#define OPTION(name, type, def_val) \
{ STRINGIFY(name), type, offsetof(struct md_config_t, name) },
#define SUBSYS(name, log, gather)
#define DEFAULT_SUBSYS(log, gather)
#include "common/config_opts.h"
#undef OPTION
#undef SUBSYS
#undef DEFAULT_SUBSYS
};
const int NUM_CONFIG_OPTIONS = sizeof(config_optionsp) / sizeof(config_option);
bool ceph_resolve_file_search(const std::string& filename_list,
std::string& result)
{
list<string> ls;
get_str_list(filename_list, ls);
list<string>::iterator iter;
for (iter = ls.begin(); iter != ls.end(); ++iter) {
int fd = ::open(iter->c_str(), O_RDONLY);
if (fd < 0)
continue;
close(fd);
result = *iter;
return true;
}
return false;
}
md_config_t::md_config_t()
: cluster("ceph"),
#define OPTION_OPT_INT(name, def_val) name(def_val),
#define OPTION_OPT_LONGLONG(name, def_val) name((1LL) * def_val),
#define OPTION_OPT_STR(name, def_val) name(def_val),
#define OPTION_OPT_DOUBLE(name, def_val) name(def_val),
#define OPTION_OPT_FLOAT(name, def_val) name(def_val),
#define OPTION_OPT_BOOL(name, def_val) name(def_val),
#define OPTION_OPT_ADDR(name, def_val) name(def_val),
#define OPTION_OPT_U32(name, def_val) name(def_val),
#define OPTION_OPT_U64(name, def_val) name(((uint64_t)1) * def_val),
#define OPTION_OPT_UUID(name, def_val) name(def_val),
#define OPTION(name, type, def_val) OPTION_##type(name, def_val)
#define SUBSYS(name, log, gather)
#define DEFAULT_SUBSYS(log, gather)
#include "common/config_opts.h"
#undef OPTION_OPT_INT
#undef OPTION_OPT_LONGLONG
#undef OPTION_OPT_STR
#undef OPTION_OPT_DOUBLE
#undef OPTION_OPT_FLOAT
#undef OPTION_OPT_BOOL
#undef OPTION_OPT_ADDR
#undef OPTION_OPT_U32
#undef OPTION_OPT_U64
#undef OPTION_OPT_UUID
#undef OPTION
#undef SUBSYS
#undef DEFAULT_SUBSYS
lock("md_config_t", true)
{
init_subsys();
}
void md_config_t::init_subsys()
{
#define SUBSYS(name, log, gather) \
subsys.add(ceph_subsys_##name, STRINGIFY(name), log, gather);
#define DEFAULT_SUBSYS(log, gather) \
subsys.add(ceph_subsys_, "none", log, gather);
#define OPTION(a, b, c)
#include "common/config_opts.h"
#undef OPTION
#undef SUBSYS
#undef DEFAULT_SUBSYS
}
md_config_t::~md_config_t()
{
}
void md_config_t::add_observer(md_config_obs_t* observer_)
{
Mutex::Locker l(lock);
const char **keys = observer_->get_tracked_conf_keys();
for (const char ** k = keys; *k; ++k) {
obs_map_t::value_type val(*k, observer_);
observers.insert(val);
}
}
void md_config_t::remove_observer(md_config_obs_t* observer_)
{
Mutex::Locker l(lock);
bool found_obs = false;
for (obs_map_t::iterator o = observers.begin(); o != observers.end(); ) {
if (o->second == observer_) {
observers.erase(o++);
found_obs = true;
}
else {
++o;
}
}
assert(found_obs);
}
int md_config_t::parse_config_files(const char *conf_files,
std::deque<std::string> *parse_errors,
std::ostream *warnings,
int flags)
{
Mutex::Locker l(lock);
if (internal_safe_to_start_threads)
return -ENOSYS;
if (!conf_files) {
const char *c = getenv("CEPH_CONF");
if (c) {
conf_files = c;
}
else {
if (flags & CINIT_FLAG_NO_DEFAULT_CONFIG_FILE)
return 0;
conf_files = CEPH_CONF_FILE_DEFAULT;
}
}
std::list<std::string> cfl;
get_str_list(conf_files, cfl);
return parse_config_files_impl(cfl, parse_errors, warnings);
}
int md_config_t::parse_config_files_impl(const std::list<std::string> &conf_files,
std::deque<std::string> *parse_errors,
std::ostream *warnings)
{
assert(lock.is_locked());
// open new conf
list<string>::const_iterator c;
for (c = conf_files.begin(); c != conf_files.end(); ++c) {
cf.clear();
string fn = *c;
expand_meta(fn, warnings);
int ret = cf.parse_file(fn.c_str(), parse_errors, warnings);
if (ret == 0)
break;
else if (ret != -ENOENT)
return ret;
}
if (c == conf_files.end())
return -EINVAL;
std::vector <std::string> my_sections;
_get_my_sections(my_sections);
for (int i = 0; i < NUM_CONFIG_OPTIONS; i++) {
config_option *opt = &config_optionsp[i];
std::string val;
int ret = _get_val_from_conf_file(my_sections, opt->name, val, false);
if (ret == 0) {
set_val_impl(val.c_str(), opt);
}
}
// subsystems?
for (int o = 0; o < subsys.get_num(); o++) {
std::string as_option("debug_");
as_option += subsys.get_name(o);
std::string val;
int ret = _get_val_from_conf_file(my_sections, as_option.c_str(), val, false);
if (ret == 0) {
int log, gather;
int r = sscanf(val.c_str(), "%d/%d", &log, &gather);
if (r >= 1) {
if (r < 2)
gather = log;
// cout << "config subsys " << subsys.get_name(o) << " log " << log << " gather " << gather << std::endl;
subsys.set_log_level(o, log);
subsys.set_gather_level(o, gather);
}
}
}
// Warn about section names that look like old-style section names
std::deque < std::string > old_style_section_names;
for (ConfFile::const_section_iter_t s = cf.sections_begin();
s != cf.sections_end(); ++s) {
const string &str(s->first);
if (((str.find("mds") == 0) || (str.find("mon") == 0) ||
(str.find("osd") == 0)) && (str.size() > 3) && (str[3] != '.')) {
old_style_section_names.push_back(str);
}
}
if (!old_style_section_names.empty()) {
ostringstream oss;
oss << "ERROR! old-style section name(s) found: ";
string sep;
for (std::deque < std::string >::const_iterator os = old_style_section_names.begin();
os != old_style_section_names.end(); ++os) {
oss << sep << *os;
sep = ", ";
}
oss << ". Please use the new style section names that include a period.";
parse_errors->push_back(oss.str());
}
return 0;
}
void md_config_t::parse_env()
{
Mutex::Locker l(lock);
if (internal_safe_to_start_threads)
return;
if (getenv("CEPH_KEYRING")) {
set_val_or_die("keyring", getenv("CEPH_KEYRING"));
}
}
void md_config_t::show_config(std::ostream& out)
{
Mutex::Locker l(lock);
_show_config(&out, NULL);
}
void md_config_t::show_config(Formatter *f)
{
Mutex::Locker l(lock);
_show_config(NULL, f);
}
void md_config_t::_show_config(std::ostream *out, Formatter *f)
{
if (out) {
*out << "name = " << name << std::endl;
*out << "cluster = " << cluster << std::endl;
}
if (f) {
f->dump_string("name", stringify(name));
f->dump_string("cluster", cluster);
}
for (int o = 0; o < subsys.get_num(); o++) {
if (out)
*out << "debug_" << subsys.get_name(o)
<< " = " << subsys.get_log_level(o)
<< "/" << subsys.get_gather_level(o) << std::endl;
if (f) {
ostringstream ss;
std::string debug_name = "debug_";
debug_name += subsys.get_name(o);
ss << subsys.get_log_level(o)
<< "/" << subsys.get_gather_level(o);
f->dump_string(debug_name.c_str(), ss.str());
}
}
for (int i = 0; i < NUM_CONFIG_OPTIONS; i++) {
config_option *opt = config_optionsp + i;
char *buf;
_get_val(opt->name, &buf, -1);
if (out)
*out << opt->name << " = " << buf << std::endl;
if (f)
f->dump_string(opt->name, buf);
free(buf);
}
}
int md_config_t::parse_argv(std::vector<const char*>& args)
{
Mutex::Locker l(lock);
if (internal_safe_to_start_threads) {
return -ENOSYS;
}
bool show_config = false;
bool show_config_value = false;
string show_config_value_arg;
// In this function, don't change any parts of the configuration directly.
// Instead, use set_val to set them. This will allow us to send the proper
// observer notifications later.
std::string val;
for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
if (strcmp(*i, "--") == 0) {
/* Normally we would use ceph_argparse_double_dash. However, in this
* function we *don't* want to remove the double dash, because later
* argument parses will still need to see it. */
break;
}
else if (ceph_argparse_flag(args, i, "--show_conf", (char*)NULL)) {
cerr << cf << std::endl;
_exit(0);
}
else if (ceph_argparse_flag(args, i, "--show_config", (char*)NULL)) {
show_config = true;
}
else if (ceph_argparse_witharg(args, i, &val, "--show_config_value", (char*)NULL)) {
show_config_value = true;
show_config_value_arg = val;
}
else if (ceph_argparse_flag(args, i, "--foreground", "-f", (char*)NULL)) {
set_val_or_die("daemonize", "false");
}
else if (ceph_argparse_flag(args, i, "-d", (char*)NULL)) {
set_val_or_die("daemonize", "false");
set_val_or_die("log_file", "");
set_val_or_die("log_to_stderr", "true");
set_val_or_die("err_to_stderr", "true");
set_val_or_die("log_to_syslog", "false");
}
// Some stuff that we wanted to give universal single-character options for
// Careful: you can burn through the alphabet pretty quickly by adding
// to this list.
else if (ceph_argparse_witharg(args, i, &val, "--monmap", "-M", (char*)NULL)) {
set_val_or_die("monmap", val.c_str());
}
else if (ceph_argparse_witharg(args, i, &val, "--mon_host", "-m", (char*)NULL)) {
set_val_or_die("mon_host", val.c_str());
}
else if (ceph_argparse_witharg(args, i, &val, "--bind", (char*)NULL)) {
set_val_or_die("public_addr", val.c_str());
}
else if (ceph_argparse_witharg(args, i, &val, "--keyfile", "-K", (char*)NULL)) {
set_val_or_die("keyfile", val.c_str());
}
else if (ceph_argparse_witharg(args, i, &val, "--keyring", "-k", (char*)NULL)) {
set_val_or_die("keyring", val.c_str());
}
else if (ceph_argparse_witharg(args, i, &val, "--client_mountpoint", "-r", (char*)NULL)) {
set_val_or_die("client_mountpoint", val.c_str());
}
else {
parse_option(args, i, NULL);
}
}
if (show_config) {
expand_all_meta();
_show_config(&cout, NULL);
_exit(0);
}
if (show_config_value) {
char *buf = 0;
int r = _get_val(show_config_value_arg.c_str(), &buf, -1);
if (r < 0) {
if (r == -ENOENT)
std::cerr << "failed to get config option '" <<
show_config_value_arg << "': option not found" << std::endl;
else
std::cerr << "failed to get config option '" <<
show_config_value_arg << "': " << cpp_strerror(r) << std::endl;
_exit(1);
}
string s = buf;
expand_meta(s, &std::cerr);
std::cout << s << std::endl;
_exit(0);
}
return 0;
}
int md_config_t::parse_option(std::vector<const char*>& args,
std::vector<const char*>::iterator& i,
ostream *oss)
{
int ret = 0;
int o;
std::string val;
// subsystems?
for (o = 0; o < subsys.get_num(); o++) {
std::string as_option("--");
as_option += "debug_";
as_option += subsys.get_name(o);
if (ceph_argparse_witharg(args, i, &val,
as_option.c_str(), (char*)NULL)) {
int log, gather;
int r = sscanf(val.c_str(), "%d/%d", &log, &gather);
if (r >= 1) {
if (r < 2)
gather = log;
// cout << "subsys " << subsys.get_name(o) << " log " << log << " gather " << gather << std::endl;
subsys.set_log_level(o, log);
subsys.set_gather_level(o, gather);
if (oss)
*oss << "debug_" << subsys.get_name(o) << "=" << log << "/" << gather << " ";
}
break;
}
}
if (o < subsys.get_num()) {
return ret;
}
for (o = 0; o < NUM_CONFIG_OPTIONS; ++o) {
const config_option *opt = config_optionsp + o;
std::string as_option("--");
as_option += opt->name;
if (opt->type == OPT_BOOL) {
int res;
if (ceph_argparse_binary_flag(args, i, &res, oss, as_option.c_str(),
(char*)NULL)) {
if (res == 0)
set_val_impl("false", opt);
else if (res == 1)
set_val_impl("true", opt);
else
ret = res;
break;
} else {
std::string no("--no-");
no += opt->name;
if (ceph_argparse_flag(args, i, no.c_str(), (char*)NULL)) {
set_val_impl("false", opt);
break;
}
}
}
else if (ceph_argparse_witharg(args, i, &val,
as_option.c_str(), (char*)NULL)) {
if (oss && (
((opt->type == OPT_STR) || (opt->type == OPT_ADDR) ||
(opt->type == OPT_UUID)) &&
(observers.find(opt->name) == observers.end()))) {
*oss << "You cannot change " << opt->name << " using injectargs.\n";
ret = -ENOSYS;
break;
}
int res = set_val_impl(val.c_str(), opt);
if (res) {
if (oss) {
*oss << "Parse error setting " << opt->name << " to '"
<< val << "' using injectargs.\n";
ret = res;
break;
} else {
cerr << "parse error setting '" << opt->name << "' to '"
<< val << "'\n" << std::endl;
}
}
break;
}
}
if (o == NUM_CONFIG_OPTIONS) {
// ignore
++i;
}
return ret;
}
int md_config_t::parse_injectargs(std::vector<const char*>& args,
std::ostream *oss)
{
assert(lock.is_locked());
int ret = 0;
for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
int r = parse_option(args, i, oss);
if (r < 0)
ret = r;
}
return ret;
}
void md_config_t::apply_changes(std::ostream *oss)
{
Mutex::Locker l(lock);
_apply_changes(oss);
}
bool md_config_t::_internal_field(const string& s)
{
if (s == "internal_safe_to_start_threads")
return true;
return false;
}
void md_config_t::_apply_changes(std::ostream *oss)
{
/* Maps observers to the configuration options that they care about which
* have changed. */
typedef std::map < md_config_obs_t*, std::set <std::string> > rev_obs_map_t;
expand_all_meta();
// create the reverse observer mapping, mapping observers to the set of
// changed keys that they'll get.
rev_obs_map_t robs;
std::set <std::string> empty_set;
char buf[128];
char *bufptr = (char*)buf;
for (changed_set_t::const_iterator c = changed.begin();
c != changed.end(); ++c) {
const std::string &key(*c);
if ((oss) &&
(!_get_val(key.c_str(), &bufptr, sizeof(buf))) &&
!_internal_field(key)) {
(*oss) << key << " = '" << buf << "' ";
}
pair < obs_map_t::iterator, obs_map_t::iterator >
range(observers.equal_range(key));
for (obs_map_t::iterator r = range.first; r != range.second; ++r) {
rev_obs_map_t::value_type robs_val(r->second, empty_set);
pair < rev_obs_map_t::iterator, bool > robs_ret(robs.insert(robs_val));
std::set <std::string> &keys(robs_ret.first->second);
keys.insert(key);
}
}
// Make any pending observer callbacks
for (rev_obs_map_t::const_iterator r = robs.begin(); r != robs.end(); ++r) {
md_config_obs_t *obs = r->first;
obs->handle_conf_change(this, r->second);
}
changed.clear();
}
void md_config_t::call_all_observers()
{
Mutex::Locker l(lock);
expand_all_meta();
std::map<md_config_obs_t*,std::set<std::string> > obs;
for (obs_map_t::iterator r = observers.begin(); r != observers.end(); ++r)
obs[r->second].insert(r->first);
for (std::map<md_config_obs_t*,std::set<std::string> >::iterator p = obs.begin();
p != obs.end();
++p)
p->first->handle_conf_change(this, p->second);
}
int md_config_t::injectargs(const std::string& s, std::ostream *oss)
{
int ret;
Mutex::Locker l(lock);
char b[s.length()+1];
strcpy(b, s.c_str());
std::vector<const char*> nargs;
char *p = b;
while (*p) {
nargs.push_back(p);
while (*p && *p != ' ') p++;
if (!*p)
break;
*p++ = 0;
while (*p && *p == ' ') p++;
}
ret = parse_injectargs(nargs, oss);
if (!nargs.empty()) {
*oss << " failed to parse arguments: ";
std::string prefix;
for (std::vector<const char*>::const_iterator i = nargs.begin();
i != nargs.end(); ++i) {
*oss << prefix << *i;
prefix = ",";
}
*oss << "\n";
ret = -EINVAL;
}
_apply_changes(oss);
return ret;
}
void md_config_t::set_val_or_die(const char *key, const char *val)
{
int ret = set_val(key, val);
assert(ret == 0);
}
int md_config_t::set_val(const char *key, const char *val, bool meta)
{
Mutex::Locker l(lock);
if (!key)
return -EINVAL;
if (!val)
return -EINVAL;
std::string v(val);
if (meta)
expand_meta(v, &std::cerr);
string k(ConfFile::normalize_key_name(key));
// subsystems?
if (strncmp(k.c_str(), "debug_", 6) == 0) {
for (int o = 0; o < subsys.get_num(); o++) {
std::string as_option = "debug_" + subsys.get_name(o);
if (k == as_option) {
int log, gather;
int r = sscanf(v.c_str(), "%d/%d", &log, &gather);
if (r >= 1) {
if (r < 2)
gather = log;
// cout << "subsys " << subsys.get_name(o) << " log " << log << " gather " << gather << std::endl;
subsys.set_log_level(o, log);
subsys.set_gather_level(o, gather);
return 0;
}
return -EINVAL;
}
}
}
for (int i = 0; i < NUM_CONFIG_OPTIONS; ++i) {
config_option *opt = &config_optionsp[i];
if (strcmp(opt->name, k.c_str()) == 0) {
if (internal_safe_to_start_threads) {
// If threads have been started...
if ((opt->type == OPT_STR) || (opt->type == OPT_ADDR) ||
(opt->type == OPT_UUID)) {
// And this is NOT an integer valued variable....
if (observers.find(opt->name) == observers.end()) {
// And there is no observer to safely change it...
// You lose.
return -ENOSYS;
}
}
}
return set_val_impl(v.c_str(), opt);
}
}
// couldn't find a configuration option with key 'key'
return -ENOENT;
}
int md_config_t::get_val(const char *key, char **buf, int len) const
{
Mutex::Locker l(lock);
return _get_val(key, buf,len);
}
int md_config_t::_get_val(const char *key, char **buf, int len) const
{
assert(lock.is_locked());
if (!key)
return -EINVAL;
// In key names, leading and trailing whitespace are not significant.
string k(ConfFile::normalize_key_name(key));
for (int i = 0; i < NUM_CONFIG_OPTIONS; ++i) {
const config_option *opt = &config_optionsp[i];
if (strcmp(opt->name, k.c_str()))
continue;
ostringstream oss;
switch (opt->type) {
case OPT_INT:
oss << *(int*)opt->conf_ptr(this);
break;
case OPT_LONGLONG:
oss << *(long long*)opt->conf_ptr(this);
break;
case OPT_STR:
oss << *((std::string*)opt->conf_ptr(this));
break;
case OPT_FLOAT:
oss << *(float*)opt->conf_ptr(this);
break;
case OPT_DOUBLE:
oss << *(double*)opt->conf_ptr(this);
break;
case OPT_BOOL: {
bool b = *(bool*)opt->conf_ptr(this);
oss << (b ? "true" : "false");
}
break;
case OPT_U32:
oss << *(uint32_t*)opt->conf_ptr(this);
break;
case OPT_U64:
oss << *(uint64_t*)opt->conf_ptr(this);
break;
case OPT_ADDR:
oss << *(entity_addr_t*)opt->conf_ptr(this);
break;
case OPT_UUID:
oss << *(uuid_d*)opt->conf_ptr(this);
break;
}
string str(oss.str());
int l = strlen(str.c_str()) + 1;
if (len == -1) {
*buf = (char*)malloc(l);
if (!*buf)
return -ENOMEM;
strcpy(*buf, str.c_str());
return 0;
}
snprintf(*buf, len, "%s", str.c_str());
return (l > len) ? -ENAMETOOLONG : 0;
}
// subsys?
for (int o = 0; o < subsys.get_num(); o++) {
std::string as_option = "debug_" + subsys.get_name(o);
if (k == as_option) {
if (len == -1) {
*buf = (char*)malloc(20);
len = 20;
}
int l = snprintf(*buf, len, "%d/%d", subsys.get_log_level(o), subsys.get_gather_level(o));
return (l == len) ? -ENAMETOOLONG : 0;
}
}
// couldn't find a configuration option with key 'k'
return -ENOENT;
}
/* The order of the sections here is important. The first section in the
* vector is the "highest priority" section; if we find it there, we'll stop
* looking. The lowest priority section is the one we look in only if all
* others had nothing. This should always be the global section.
*/
void md_config_t::get_my_sections(std::vector <std::string> §ions) const
{
Mutex::Locker l(lock);
_get_my_sections(sections);
}
void md_config_t::_get_my_sections(std::vector <std::string> §ions) const
{
assert(lock.is_locked());
sections.push_back(name.to_str());
sections.push_back(name.get_type_name());
sections.push_back("global");
}
// Return a list of all sections
int md_config_t::get_all_sections(std::vector <std::string> §ions) const
{
Mutex::Locker l(lock);
for (ConfFile::const_section_iter_t s = cf.sections_begin();
s != cf.sections_end(); ++s) {
sections.push_back(s->first);
}
return 0;
}
int md_config_t::get_val_from_conf_file(const std::vector <std::string> §ions,
const char *key, std::string &out, bool emeta) const
{
Mutex::Locker l(lock);
return _get_val_from_conf_file(sections, key, out, emeta);
}
int md_config_t::_get_val_from_conf_file(const std::vector <std::string> §ions,
const char *key, std::string &out, bool emeta) const
{
assert(lock.is_locked());
std::vector <std::string>::const_iterator s = sections.begin();
std::vector <std::string>::const_iterator s_end = sections.end();
for (; s != s_end; ++s) {
int ret = cf.read(s->c_str(), key, out);
if (ret == 0) {
if (emeta)
expand_meta(out, &std::cerr);
return 0;
}
else if (ret != -ENOENT)
return ret;
}
return -ENOENT;
}
int md_config_t::set_val_impl(const char *val, const config_option *opt)
{
assert(lock.is_locked());
int ret = set_val_raw(val, opt);
if (ret)
return ret;
changed.insert(opt->name);
return 0;
}
int md_config_t::set_val_raw(const char *val, const config_option *opt)
{
assert(lock.is_locked());
switch (opt->type) {
case OPT_INT: {
std::string err;
int f = strict_sistrtoll(val, &err);
if (!err.empty())
return -EINVAL;
*(int*)opt->conf_ptr(this) = f;
return 0;
}
case OPT_LONGLONG: {
std::string err;
long long f = strict_sistrtoll(val, &err);
if (!err.empty())
return -EINVAL;
*(long long*)opt->conf_ptr(this) = f;
return 0;
}
case OPT_STR:
*(std::string*)opt->conf_ptr(this) = val ? val : "";
return 0;
case OPT_FLOAT:
*(float*)opt->conf_ptr(this) = atof(val);
return 0;
case OPT_DOUBLE:
*(double*)opt->conf_ptr(this) = atof(val);
return 0;
case OPT_BOOL:
if (strcasecmp(val, "false") == 0)
*(bool*)opt->conf_ptr(this) = false;
else if (strcasecmp(val, "true") == 0)
*(bool*)opt->conf_ptr(this) = true;
else {
std::string err;
int b = strict_strtol(val, 10, &err);
if (!err.empty())
return -EINVAL;
*(bool*)opt->conf_ptr(this) = !!b;
}
return 0;
case OPT_U32: {
std::string err;
int f = strict_sistrtoll(val, &err);
if (!err.empty())
return -EINVAL;
*(uint32_t*)opt->conf_ptr(this) = f;
return 0;
}
case OPT_U64: {
std::string err;
long long f = strict_sistrtoll(val, &err);
if (!err.empty())
return -EINVAL;
*(uint64_t*)opt->conf_ptr(this) = f;
return 0;
}
case OPT_ADDR: {
entity_addr_t *addr = (entity_addr_t*)opt->conf_ptr(this);
if (!addr->parse(val)) {
return -EINVAL;
}
return 0;
}
case OPT_UUID: {
uuid_d *u = (uuid_d*)opt->conf_ptr(this);
if (!u->parse(val))
return -EINVAL;
return 0;
}
}
return -ENOSYS;
}
static const char *CONF_METAVARIABLES[] =
{ "cluster", "type", "name", "host", "num", "id", "pid", "cctid" };
static const int NUM_CONF_METAVARIABLES =
(sizeof(CONF_METAVARIABLES) / sizeof(CONF_METAVARIABLES[0]));
void md_config_t::expand_all_meta()
{
// Expand all metavariables
ostringstream oss;
for (int i = 0; i < NUM_CONFIG_OPTIONS; i++) {
config_option *opt = config_optionsp + i;
if (opt->type == OPT_STR) {
std::string *str = (std::string *)opt->conf_ptr(this);
list<config_option *> stack;
expand_meta(*str, opt, stack, &oss);
}
}
cerr << oss.str();
}
bool md_config_t::expand_meta(std::string &origval,
std::ostream *oss) const
{
list<config_option *> stack;
return expand_meta(origval, NULL, stack, oss);
}
bool md_config_t::expand_meta(std::string &origval,
config_option *opt,
std::list<config_option *> stack,
std::ostream *oss) const
{
assert(lock.is_locked());
// no $ means no variable expansion is necessary
if (origval.find("$") == string::npos)
return false;
// ignore an expansion loop and create a human readable
// message about it
if (opt) {
for (list<config_option *>::iterator i = stack.begin();
i != stack.end();
++i) {
if (strcmp(opt->name, (*i)->name) == 0) {
*oss << "variable expansion loop at "
<< opt->name << "=" << origval << std::endl;
*oss << "expansion stack: " << std::endl;
for (list<config_option *>::iterator j = stack.begin();
j != stack.end();
++j) {
*oss << (*j)->name << "=" << *(string *)(*j)->conf_ptr(this) << std::endl;
}
return false;
}
}
}
if (opt)
stack.push_front(opt);
bool found_meta = false;
string out;
string val = origval;
for (string::size_type s = 0; s < val.size(); ) {
if (val[s] != '$') {
out += val[s++];
continue;
}
// try to parse the variable name into var, either \$\{(.+)\} or
// \$[a-z\_]+
const char *valid_chars = "abcdefghijklmnopqrstuvwxyz_";
string var;
size_t endpos = 0;
if (val[s+1] == '{') {
// ...${foo_bar}...
endpos = val.find_first_not_of(valid_chars, s+2);
if (endpos != std::string::npos &&
val[endpos] == '}') {
var = val.substr(s+2, endpos-s-2);
endpos++;
}
} else {
// ...$foo...
endpos = val.find_first_not_of(valid_chars, s+1);
if (endpos != std::string::npos)
var = val.substr(s+1, endpos-s-1);
else
var = val.substr(s+1);
}
bool expanded = false;
if (var.length()) {
// special metavariable?
for (int i = 0; i < NUM_CONF_METAVARIABLES; ++i) {
if (var != CONF_METAVARIABLES[i])
continue;
//cout << " meta match of " << var << " " << CONF_METAVARIABLES[i] << std::endl;
if (var == "type")
out += name.get_type_name();
else if (var == "cluster")
out += cluster;
else if (var == "name")
out += name.to_cstr();
else if (var == "host")
out += host;
else if (var == "num")
out += name.get_id().c_str();
else if (var == "id")
out += name.get_id().c_str();
else if (var == "pid")
out += stringify(getpid());
else if (var == "cctid")
out += stringify((unsigned long long)this);
else
assert(0); // unreachable
expanded = true;
}
if (!expanded) {
// config option?
for (int i = 0; i < NUM_CONFIG_OPTIONS; i++) {
config_option *opt = &config_optionsp[i];
if (var == opt->name) {
if (opt->type == OPT_STR) {
string *origval = (string *)opt->conf_ptr(this);
expand_meta(*origval, opt, stack, oss);
out += *origval;
} else {
char *vv = NULL;
_get_val(opt->name, &vv, -1);
out += vv;
free(vv);
}
expanded = true;
break;
}
}
}
}
if (expanded) {
found_meta = true;
s = endpos;
} else {
out += val[s++];
}
}
// override the original value with the expanded value
origval = out;
return found_meta;
}
md_config_obs_t::
~md_config_obs_t()
{
}
|