Skip to content

Commit 394eb08

Browse files
committed
Merge RunStopCvdCmd into RunStopCvd
1 parent 7a74aeb commit 394eb08

1 file changed

Lines changed: 24 additions & 40 deletions

File tree

  • base/cvd/cuttlefish/host/commands/cvd/instances

base/cvd/cuttlefish/host/commands/cvd/instances/stop.cpp

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <vector>
2828

2929
#include "absl/log/log.h"
30-
#include "absl/strings/str_join.h"
3130
#include "android-base/file.h"
3231
#include "fmt/core.h"
3332
#include "fmt/ranges.h"
@@ -63,22 +62,6 @@ static Command CreateStopCvdCommand(
6362
return command;
6463
}
6564

66-
Result<void> RunStopCvdCmd(
67-
const std::string& stopper_path,
68-
const std::unordered_map<std::string, std::string>& env,
69-
const std::vector<std::string>& args) {
70-
Command stop_cmd = CreateStopCvdCommand(stopper_path, env, args);
71-
72-
LOG(INFO) << "Running " << stop_cmd.ToString();
73-
Result<std::string> cmd_res = RunAndCaptureStdout(std::move(stop_cmd));
74-
if (!cmd_res.ok()) {
75-
LOG(ERROR) << "Failed to run " << stopper_path;
76-
CF_EXPECT(std::move(cmd_res));
77-
}
78-
VLOG(1) << "\"" << stopper_path << " successfully ";
79-
return {};
80-
}
81-
8265
Result<void> RunStopCvdAll(bool clear_runtime_dirs) {
8366
std::vector<GroupProcInfo> group_infos = CF_EXPECT(CollectRunCvdGroups());
8467
LOG(INFO) << "Found " << group_infos.size()
@@ -250,45 +233,46 @@ Result<void> ForcefullyStopGroup(const uid_t any_id_in_group) {
250233
}
251234

252235
Result<void> RunStopCvd(StopCvdParams params) {
253-
const auto& stopper_path = params.bin_path;
254-
std::unordered_map<std::string, std::string> stop_cvd_envs;
255-
stop_cvd_envs["HOME"] = params.home_dir;
256236
// stop_cvd is located at $ANDROID_HOST_OUT/bin/stop_cvd
257-
std::string android_host_out =
258-
android::base::Dirname(android::base::Dirname(stopper_path));
259-
stop_cvd_envs[kAndroidHostOut] = android_host_out;
260-
stop_cvd_envs[kAndroidSoongHostOut] = android_host_out;
261-
auto config_file_path = CF_EXPECT(GetCuttlefishConfigPath(params.home_dir));
262-
stop_cvd_envs[kCuttlefishConfigEnvVarName] = config_file_path;
263-
std::vector<std::string> args;
237+
const std::string android_host_out =
238+
android::base::Dirname(android::base::Dirname(params.bin_path));
239+
const std::unordered_map<std::string, std::string> stop_cvd_envs = {
240+
std::make_pair("HOME", params.home_dir),
241+
std::make_pair(kAndroidHostOut, android_host_out),
242+
std::make_pair(kAndroidSoongHostOut, android_host_out),
243+
std::make_pair(kCuttlefishConfigEnvVarName,
244+
CF_EXPECT(GetCuttlefishConfigPath(params.home_dir))),
245+
};
246+
264247
std::string wait_flag =
265248
fmt::format("--wait_for_launcher={}", params.wait_for_launcher_secs);
266-
args.push_back(wait_flag);
249+
std::vector<std::string> args = {wait_flag};
267250
if (params.clear_runtime_dirs) {
268-
args.push_back("--clear_instance_dirs=true");
251+
args.emplace_back("--clear_instance_dirs=true");
269252
}
270253
if (!params.instance_nums.empty()) {
271-
args.push_back(fmt::format("--instance_nums={}",
272-
absl::StrJoin(params.instance_nums, ",")));
254+
args.emplace_back(fmt::format("--instance_nums={}",
255+
fmt::join(params.instance_nums, ",")));
273256
}
274-
Result<void> cmd_res = RunStopCvdCmd(stopper_path, stop_cvd_envs, args);
257+
258+
Result<std::string> cmd_res = RunAndCaptureStdout(
259+
CreateStopCvdCommand(params.bin_path, stop_cvd_envs, args));
275260
if (cmd_res.ok()) {
276261
return {};
277262
}
278263
/**
279-
* --clear_instance_dirs may not be available in old branches. This causes
280-
* stop_cvd to terminate with a non-zero exit code due to a parsing error. Try
281-
* again without that flag.
264+
* --clear_instance_dirs or --instance_nums may not be available in old
265+
* branches. This causes stop_cvd to terminate with a non-zero exit code due
266+
* to a parsing error. Try again without that flag.
282267
*/
283-
if (!params.clear_runtime_dirs) {
268+
if (!params.clear_runtime_dirs && !params.instance_nums.empty()) {
284269
CF_EXPECT(std::move(cmd_res));
285270
}
286-
// TODO(kwstephenkim): deletes manually if `stop_cvd --clear_instance_dirs`
287-
// failed.
288-
LOG(ERROR) << "Perhaps --clear_instance_dirs is not supported.";
271+
LOG(ERROR) << "--clear_instance_dirs or --instance_nums is not supported.";
289272
LOG(ERROR) << "Trying again without it";
290273

291-
CF_EXPECT(RunStopCvdCmd(stopper_path, stop_cvd_envs, {wait_flag}));
274+
CF_EXPECT(RunAndCaptureStdout(
275+
CreateStopCvdCommand(params.bin_path, stop_cvd_envs, {wait_flag})));
292276
return {};
293277
}
294278
} // namespace cuttlefish

0 commit comments

Comments
 (0)