Merge pull request #37785 from lxbsz/conf1

common/config: do not expand the pid for socket admin

Reviewed-by: Kefu Chai <kchai@redhat.com>
This commit is contained in:
Kefu Chai 2020-12-11 16:04:51 +08:00 committed by GitHub
commit 8453857907
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 4 deletions

View File

@ -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);
}

View File

@ -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 <<EOF
> [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 <<EOF
> [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

View File

@ -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<std::string, std::string>
maybe_override_name_pid(vector<const char*> 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<const char*> 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<std::string,std::string> defaults = {{"log_to_file", "false"}};
return global_init(&defaults, args, CEPH_ENTITY_TYPE_CLIENT,
CODE_ENVIRONMENT_DAEMON,