Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rbd: fix static initialization ordering issues #6978

Merged
merged 1 commit into from
Dec 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/tools/rbd/Shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,17 @@ std::string format_option_suffix(

} // anonymous namespace

std::vector<Shell::Action *> Shell::s_actions;
std::set<std::string> Shell::s_switch_arguments;
std::vector<Shell::Action *>& Shell::get_actions() {
static std::vector<Action *> actions;

return actions;
}

std::set<std::string>& Shell::get_switch_arguments() {
static std::set<std::string> switch_arguments;

return switch_arguments;
}

int Shell::execute(int arg_count, const char **arg_values) {

Expand Down Expand Up @@ -187,9 +196,10 @@ void Shell::get_command_spec(const std::vector<std::string> &arguments,
} else if (arg[0] == '-') {
// if the option is not a switch, skip its value
if (arg.size() >= 2 &&
(arg[1] == '-' || s_switch_arguments.count(arg.substr(1, 1)) == 0) &&
(arg[1] == '-' ||
get_switch_arguments().count(arg.substr(1, 1)) == 0) &&
(arg[1] != '-' ||
s_switch_arguments.count(arg.substr(2, std::string::npos)) == 0) &&
get_switch_arguments().count(arg.substr(2, std::string::npos)) == 0) &&
at::SWITCH_ARGUMENTS.count(arg.substr(2, std::string::npos)) == 0 &&
arg.find('=') == std::string::npos) {
++i;
Expand All @@ -202,8 +212,8 @@ void Shell::get_command_spec(const std::vector<std::string> &arguments,

Shell::Action *Shell::find_action(const CommandSpec &command_spec,
CommandSpec **matching_spec) {
for (size_t i = 0; i < s_actions.size(); ++i) {
Action *action = s_actions[i];
for (size_t i = 0; i < get_actions().size(); ++i) {
Action *action = get_actions()[i];
if (action->command_spec.size() <= command_spec.size()) {
if (std::includes(action->command_spec.begin(),
action->command_spec.end(),
Expand Down Expand Up @@ -277,7 +287,7 @@ void Shell::print_help() {
<< "Command-line interface for managing Ceph RBD images."
<< std::endl << std::endl;

std::vector<Action *> actions(s_actions);
std::vector<Action *> actions(get_actions());
std::sort(actions.begin(), actions.end(),
[](Action *lhs, Action *rhs) { return lhs->command_spec <
rhs->command_spec; });
Expand Down Expand Up @@ -368,8 +378,8 @@ void Shell::print_bash_completion(const CommandSpec &command_spec) {
print_bash_completion_options(command_opts);
} else {
std::cout << "|help";
for (size_t i = 0; i < s_actions.size(); ++i) {
Action *action = s_actions[i];
for (size_t i = 0; i < get_actions().size(); ++i) {
Action *action = get_actions()[i];
std::cout << "|"
<< joinify<std::string>(action->command_spec.begin(),
action->command_spec.end(), " ");
Expand Down
8 changes: 4 additions & 4 deletions src/tools/rbd/Shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ class Shell {
: command_spec(command_spec), alias_command_spec(alias_command_spec),
description(description), help(help), get_arguments(args),
execute(execute) {
Shell::s_actions.push_back(this);
Shell::get_actions().push_back(this);
}

};

struct SwitchArguments {
SwitchArguments(const std::initializer_list<std::string> &arguments) {
Shell::s_switch_arguments.insert(arguments.begin(), arguments.end());
Shell::get_switch_arguments().insert(arguments.begin(), arguments.end());
}
};

int execute(int arg_count, const char **arg_values);

private:
static std::vector<Action *> s_actions;
static std::set<std::string> s_switch_arguments;
static std::vector<Action *>& get_actions();
static std::set<std::string>& get_switch_arguments();

void get_command_spec(const std::vector<std::string> &arguments,
std::vector<std::string> *command_spec);
Expand Down