Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions base/cvd/cuttlefish/host/commands/cvd/cli/commands/start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,27 +326,47 @@ Result<std::vector<Flag>> GetCvdInternalStartFlags(
return flags;
}

} // namespace

CvdStartCommandHandler::CvdStartCommandHandler(
InstanceManager& instance_manager)
: instance_manager_(instance_manager) {
own_flags_.daemon = true;
}
bool CanBypassToSingleInstance(const LocalInstance& instance,
const LocalInstanceGroup& group,
const std::vector<std::string>& subcmd_args) {
if (instance.State() != cvd::INSTANCE_STATE_STOPPED) {
return false;
}
if (group.StartTime() == TimeStamp{}) {
return false;
}

static bool HasUnsafeFlagsForBypass(const std::vector<std::string>& args) {
std::vector<std::string> args_copy = args;
std::vector<std::string> args_copy = subcmd_args;
bool daemon = true;
std::string report_anonymous = "";
std::vector<Flag> safe_flags = {
GflagsCompatFlag("daemon", daemon),
GflagsCompatFlag("report_anonymous_usage_stats", report_anonymous),
};
auto res = ConsumeFlags(safe_flags, args_copy);
if (!res.ok()) {
return true;
if (!res.ok() || !daemon || !args_copy.empty()) {
return false;
}

auto instances = group.Instances();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the style guide writes

Use type deduction only if it makes the code clearer to readers who aren't familiar with the project, or if it makes the code safer. Do not use it merely to avoid the inconvenience of writing an explicit type.

if (instances.empty()) {
return false;
}
const LocalInstance& main_instance = instances[0];
if (instance.Id() == main_instance.Id()) {
return false;
}
return !args_copy.empty();
if (main_instance.State() != cvd::INSTANCE_STATE_RUNNING) {
return false;
}

return true;
}

} // namespace

CvdStartCommandHandler::CvdStartCommandHandler(
InstanceManager& instance_manager)
: instance_manager_(instance_manager) {
own_flags_.daemon = true;
}

Result<void> CvdStartCommandHandler::Handle(const CommandRequest& request) {
Expand All @@ -364,13 +384,11 @@ Result<void> CvdStartCommandHandler::Handle(const CommandRequest& request) {
auto [instance, group] =
CF_EXPECT(selector::SelectInstance(instance_manager_, request));

if (instance.State() == cvd::INSTANCE_STATE_STOPPED &&
group.StartTime() != TimeStamp{} &&
!HasUnsafeFlagsForBypass(subcmd_args)) {
if (CanBypassToSingleInstance(instance, group, subcmd_args)) {
CF_EXPECT(LaunchSingleInstance(instance, group, request));
return {};
} else {
VLOG(1) << "Instance is not in stopped state. Proceeding with "
VLOG(1) << "Cannot bypass to single instance start. Proceeding with "
"normal group start.";
}
}
Expand Down
Loading