1 /*
2 * Copyright (C) 2021 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 <android-base/logging.h>
21 #include <gflags/gflags.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 restart.");
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, 1000, "How many seconds to wait for the device to "
38 "reboot.");
39
40 namespace cuttlefish {
41 namespace {
42
RestartCvdMain()43 Result<void> RestartCvdMain() {
44 const CuttlefishConfig* config =
45 CF_EXPECT(CuttlefishConfig::Get(), "Failed to obtain config object");
46 SharedFD monitor_socket = CF_EXPECT(
47 GetLauncherMonitor(*config, FLAGS_instance_num, FLAGS_wait_for_launcher));
48
49 LOG(INFO) << "Requesting restart";
50 CF_EXPECT(WriteLauncherAction(monitor_socket, LauncherAction::kRestart));
51 CF_EXPECT(WaitForRead(monitor_socket, FLAGS_wait_for_launcher));
52 LauncherResponse restart_response =
53 CF_EXPECT(ReadLauncherResponse(monitor_socket));
54 CF_EXPECT(
55 restart_response == LauncherResponse::kSuccess,
56 "Received `" << static_cast<char>(restart_response)
57 << "` response from launcher monitor for restart request");
58
59 LOG(INFO) << "Waiting for device to boot up again";
60 CF_EXPECT(WaitForRead(monitor_socket, FLAGS_boot_timeout));
61 RunnerExitCodes boot_exit_code = CF_EXPECT(ReadExitCode(monitor_socket));
62 CF_EXPECT(boot_exit_code != RunnerExitCodes::kVirtualDeviceBootFailed,
63 "Boot failed");
64 CF_EXPECT(boot_exit_code == RunnerExitCodes::kSuccess,
65 "Unknown response" << static_cast<int>(boot_exit_code));
66
67 LOG(INFO) << "Restart successful";
68 return {};
69 }
70
71 } // namespace
72 } // namespace cuttlefish
73
main(int argc,char ** argv)74 int main(int argc, char** argv) {
75 ::android::base::InitLogging(argv, android::base::StderrLogger);
76 google::ParseCommandLineFlags(&argc, &argv, true);
77
78 cuttlefish::Result<void> result = cuttlefish::RestartCvdMain();
79 if (!result.ok()) {
80 LOG(ERROR) << result.error().Message();
81 LOG(DEBUG) << result.error().Trace();
82 return EXIT_FAILURE;
83 }
84 return EXIT_SUCCESS;
85 }
86