From cfcacb89a195da9571a7a23fe6371cabe60ed48b Mon Sep 17 00:00:00 2001 From: Joao Eduardo Luis Date: Mon, 27 Jul 2015 21:17:31 +0100 Subject: [PATCH] mon: LogMonitor: handle boolean options consistently 'mon_cluster_log_to_syslog' gets a string of key/value pairs, values in the pair being booleans, and keys being optional (one can simply specify the value). However, we weren't being consistent with the boolean behavior when handling option values. e.g., the user expects '1' and '0' to mean 'true' and 'false' respectively, and expects 'mon_cluster_log_to_syslog' to understand both '1' and '0', alongside with 'true' and 'false'. All values not 'true' or '1' will be considered 'false'. Fixes: #12325 Signed-off-by: Joao Eduardo Luis --- src/mon/LogMonitor.cc | 30 ++++++++++++++++++++++++++++++ src/mon/LogMonitor.h | 5 +---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/mon/LogMonitor.cc b/src/mon/LogMonitor.cc index c571c69f818..b5ab44712fd 100644 --- a/src/mon/LogMonitor.cc +++ b/src/mon/LogMonitor.cc @@ -12,6 +12,8 @@ * */ +#include + #include #include @@ -28,6 +30,7 @@ #include "osd/osd_types.h" #include "common/errno.h" #include "common/config.h" +#include "common/strtol.h" #include "include/assert.h" #include "include/str_list.h" #include "include/str_map.h" @@ -673,6 +676,33 @@ string LogMonitor::log_channel_info::expand_channel_meta( return s; } +bool LogMonitor::log_channel_info::do_log_to_syslog(const string &channel) { + string v = get_str_map_key(log_to_syslog, channel, + &CLOG_CONFIG_DEFAULT_KEY); + // We expect booleans, but they are in k/v pairs, kept + // as strings, in 'log_to_syslog'. We must ensure + // compatibility with existing boolean handling, and so + // we are here using a modified version of how + // md_config_t::set_val_raw() handles booleans. We will + // accept both 'true' and 'false', but will also check for + // '1' and '0'. The main distiction between this and the + // original code is that we will assume everything not '1', + // '0', 'true' or 'false' to be 'false'. + bool ret = false; + + if (boost::iequals(v, "false")) { + ret = false; + } else if (boost::iequals(v, "true")) { + ret = true; + } else { + std::string err; + int b = strict_strtol(v.c_str(), 10, &err); + ret = (err.empty() && b == 1); + } + + return ret; +} + void LogMonitor::handle_conf_change(const struct md_config_t *conf, const std::set &changed) { diff --git a/src/mon/LogMonitor.h b/src/mon/LogMonitor.h index 6dcee8b85cb..4d31b66b3d1 100644 --- a/src/mon/LogMonitor.h +++ b/src/mon/LogMonitor.h @@ -69,10 +69,7 @@ private: string expand_channel_meta(const string &input, const string &change_to); - bool do_log_to_syslog(const string &channel) { - return (get_str_map_key(log_to_syslog, channel, - &CLOG_CONFIG_DEFAULT_KEY) == "true"); - } + bool do_log_to_syslog(const string &channel); string get_facility(const string &channel) { return get_str_map_key(syslog_facility, channel,