1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <cstdint>
18 #include <cstdlib>
19
20 #include <gflags/gflags.h>
21 #include <android-base/logging.h>
22
23 #include "common/libs/fs/shared_fd.h"
24 #include "common/libs/utils/result.h"
25 #include "host/commands/run_cvd/runner_defs.h"
26 #include "host/libs/command_util/util.h"
27 #include "host/libs/config/cuttlefish_config.h"
28
29 DEFINE_int32(instance_num, cuttlefish::GetInstance(),
30 "Which instance to powerwash.");
31
32 DEFINE_int32(
33 wait_for_launcher, 30,
34 "How many seconds to wait for the launcher to respond to the status "
35 "command. A value of zero means wait indefinitely.");
36
37 DEFINE_int32(boot_timeout, 500,
38 "How many seconds to wait for the device to "
39 "reboot.");
40
41 namespace cuttlefish {
42 namespace {
43
PowerwashCvdMain()44 Result<void> PowerwashCvdMain() {
45 const CuttlefishConfig* config =
46 CF_EXPECT(CuttlefishConfig::Get(), "Failed to obtain config object");
47 SharedFD monitor_socket = CF_EXPECT(
48 GetLauncherMonitor(*config, FLAGS_instance_num, FLAGS_wait_for_launcher));
49
50 LOG(INFO) << "Requesting powerwash";
51 CF_EXPECT(WriteLauncherAction(monitor_socket, LauncherAction::kPowerwash));
52 CF_EXPECT(WaitForRead(monitor_socket, FLAGS_wait_for_launcher));
53 LauncherResponse powerwash_response =
54 CF_EXPECT(ReadLauncherResponse(monitor_socket));
55 CF_EXPECT(
56 powerwash_response == LauncherResponse::kSuccess,
57 "Received `" << static_cast<char>(powerwash_response)
58 << "` response from launcher monitor for powerwash request");
59
60 LOG(INFO) << "Waiting for device to boot up again";
61 CF_EXPECT(WaitForRead(monitor_socket, FLAGS_boot_timeout));
62 RunnerExitCodes boot_exit_code = CF_EXPECT(ReadExitCode(monitor_socket));
63 CF_EXPECT(boot_exit_code != RunnerExitCodes::kVirtualDeviceBootFailed,
64 "Boot failed");
65 CF_EXPECT(boot_exit_code == RunnerExitCodes::kSuccess,
66 "Unknown response" << static_cast<int>(boot_exit_code));
67
68 LOG(INFO) << "Powerwash successful";
69 return {};
70 }
71
72 } // namespace
73 } // namespace cuttlefish
74
main(int argc,char ** argv)75 int main(int argc, char** argv) {
76 ::android::base::InitLogging(argv, android::base::StderrLogger);
77 google::ParseCommandLineFlags(&argc, &argv, true);
78
79 cuttlefish::Result<void> result = cuttlefish::PowerwashCvdMain();
80 if (!result.ok()) {
81 LOG(ERROR) << result.error().Message();
82 LOG(DEBUG) << result.error().Trace();
83 return EXIT_FAILURE;
84 }
85 return EXIT_SUCCESS;
86 }
87