diff --git a/src/common/config.cc b/src/common/config.cc index 9cd42c6a5d8..1815c9ff4e7 100644 --- a/src/common/config.cc +++ b/src/common/config.cc @@ -1218,7 +1218,12 @@ Option::value_t md_config_t::_expand_meta( } else if (var == "id") { out += values.name.get_id(); } else if (var == "pid") { - out += stringify(getpid()); + char *_pid = getenv("PID"); + if (_pid) { + out += _pid; + } else { + out += stringify(getpid()); + } if (o) { may_reexpand_meta.push_back(o->name); } diff --git a/src/test/cli/ceph-conf/show-config-value.t b/src/test/cli/ceph-conf/show-config-value.t index 5750de8e5a5..a0ab4cbdfce 100644 --- a/src/test/cli/ceph-conf/show-config-value.t +++ b/src/test/cli/ceph-conf/show-config-value.t @@ -8,9 +8,12 @@ $ ceph-conf -n osd.0 --show-config-value INVALID -c /dev/null failed to get config option 'INVALID': option not found [1] - $ echo '[global]' > $TESTDIR/ceph.conf - $ echo 'mon_host=$public_network' >> $TESTDIR/ceph.conf - $ echo 'public_network=$mon_host' >> $TESTDIR/ceph.conf + + $ cat > $TESTDIR/ceph.conf < [global] + > mon_host = \$public_network + > public_network = \$mon_host + > EOF $ ceph-conf --show-config-value mon_host -c $TESTDIR/ceph.conf variable expansion loop at mon_host=$public_network expansion stack: @@ -18,3 +21,19 @@ mon_host=$public_network $mon_host $ rm $TESTDIR/ceph.conf + +Name option test to strip the PID +================================= + $ cat > $TESTDIR/ceph.conf < [client] + > admin socket = \$name.\$pid.asok + > [global] + > admin socket = \$name.asok + > EOF + $ ceph-conf --name client.admin.133423 --show-config-value admin_socket -c $TESTDIR/ceph.conf + client.admin.133423.asok + $ ceph-conf --name mds.a --show-config-value admin_socket -c $TESTDIR/ceph.conf + mds.a.asok + $ ceph-conf --name osd.0 --show-config-value admin_socket -c $TESTDIR/ceph.conf + osd.0.asok + $ rm $TESTDIR/ceph.conf diff --git a/src/tools/ceph_conf.cc b/src/tools/ceph_conf.cc index df5384a9e91..755bb90233a 100644 --- a/src/tools/ceph_conf.cc +++ b/src/tools/ceph_conf.cc @@ -163,6 +163,47 @@ static int dump_all(const string& format) } } +bool is_name_pid(std::string name, std::string& id) +{ + if (id.empty()) { + return false; + } + static const char* daemon_types[] = {"mon", "osd", "mds", "mgr"}; + if (std::find(std::begin(daemon_types), std::end(daemon_types), name) != + std::end(daemon_types)) { + // only override name and pid for non-daemon names + return false; + } + try { + std::stoi(id); + } catch (const std::logic_error&) { + // only override pid for $id which looks like pid + return false; + } + return true; +} + +std::pair +maybe_override_name_pid(vector args) +{ + for (auto i = args.begin(); i != args.end(); ++i) { + string val; + if (ceph_argparse_witharg(args, i, &val, "--name", "-n", (char*)NULL)) { + size_t dot_pos = val.rfind('.'); + if (dot_pos != val.npos) { + string name = val.substr(0, dot_pos); + string id = val.substr(dot_pos + 1); + if (is_name_pid(name, id)) { + // override name + return {name, id}; + } + } + return {val, ""}; + } + } + return {}; +} + int main(int argc, const char **argv) { vector args; @@ -176,8 +217,19 @@ int main(int argc, const char **argv) std::string dump_format; argv_to_vec(argc, argv, args); + auto orig_args = args; auto cct = [&args] { + // override the name and PID for non-daemon names + auto [name, pid] = maybe_override_name_pid(args); + if (!name.empty()) { + // push the name option back + args.push_back("--name"); + args.push_back(name.c_str()); + } + if (!pid.empty()) { + setenv("PID", pid.c_str(), 1); + } std::map defaults = {{"log_to_file", "false"}}; return global_init(&defaults, args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_DAEMON,