|
27 | 27 | #include <vector> |
28 | 28 |
|
29 | 29 | #include "absl/log/log.h" |
30 | | -#include "absl/strings/str_join.h" |
31 | 30 | #include "android-base/file.h" |
32 | 31 | #include "fmt/core.h" |
33 | 32 | #include "fmt/ranges.h" |
@@ -63,22 +62,6 @@ static Command CreateStopCvdCommand( |
63 | 62 | return command; |
64 | 63 | } |
65 | 64 |
|
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 | | - |
82 | 65 | Result<void> RunStopCvdAll(bool clear_runtime_dirs) { |
83 | 66 | std::vector<GroupProcInfo> group_infos = CF_EXPECT(CollectRunCvdGroups()); |
84 | 67 | LOG(INFO) << "Found " << group_infos.size() |
@@ -250,45 +233,46 @@ Result<void> ForcefullyStopGroup(const uid_t any_id_in_group) { |
250 | 233 | } |
251 | 234 |
|
252 | 235 | 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; |
256 | 236 | // 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 | + |
264 | 247 | std::string wait_flag = |
265 | 248 | fmt::format("--wait_for_launcher={}", params.wait_for_launcher_secs); |
266 | | - args.push_back(wait_flag); |
| 249 | + std::vector<std::string> args = {wait_flag}; |
267 | 250 | if (params.clear_runtime_dirs) { |
268 | | - args.push_back("--clear_instance_dirs=true"); |
| 251 | + args.emplace_back("--clear_instance_dirs=true"); |
269 | 252 | } |
270 | 253 | 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, ","))); |
273 | 256 | } |
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)); |
275 | 260 | if (cmd_res.ok()) { |
276 | 261 | return {}; |
277 | 262 | } |
278 | 263 | /** |
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. |
282 | 267 | */ |
283 | | - if (!params.clear_runtime_dirs) { |
| 268 | + if (!params.clear_runtime_dirs && !params.instance_nums.empty()) { |
284 | 269 | CF_EXPECT(std::move(cmd_res)); |
285 | 270 | } |
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."; |
289 | 272 | LOG(ERROR) << "Trying again without it"; |
290 | 273 |
|
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}))); |
292 | 276 | return {}; |
293 | 277 | } |
294 | 278 | } // namespace cuttlefish |
0 commit comments