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/libs/command_util/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(RunLauncherAction(monitor_socket, LauncherAction::kPowerwash,
52 FLAGS_wait_for_launcher));
53
54 LOG(INFO) << "Waiting for device to boot up again";
55 CF_EXPECT(WaitForRead(monitor_socket, FLAGS_boot_timeout));
56 RunnerExitCodes boot_exit_code = CF_EXPECT(ReadExitCode(monitor_socket));
57 CF_EXPECT(boot_exit_code != RunnerExitCodes::kVirtualDeviceBootFailed,
58 "Boot failed");
59 CF_EXPECT(boot_exit_code == RunnerExitCodes::kSuccess,
60 "Unknown response" << static_cast<int>(boot_exit_code));
61
62 LOG(INFO) << "Powerwash successful";
63 return {};
64 }
65
66 } // namespace
67 } // namespace cuttlefish
68
main(int argc,char ** argv)69 int main(int argc, char** argv) {
70 ::android::base::InitLogging(argv, android::base::StderrLogger);
71 google::ParseCommandLineFlags(&argc, &argv, true);
72
73 cuttlefish::Result<void> result = cuttlefish::PowerwashCvdMain();
74 if (!result.ok()) {
75 LOG(ERROR) << result.error().FormatForEnv();
76 return EXIT_FAILURE;
77 }
78 return EXIT_SUCCESS;
79 }
80