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 "host/commands/run_cvd/boot_state_machine.h"
18
19 #include <poll.h>
20
21 #include <memory>
22 #include <thread>
23
24 #include <android-base/logging.h>
25 #include <gflags/gflags.h>
26
27 #include "common/libs/fs/shared_fd.h"
28 #include "common/libs/utils/tee_logging.h"
29 #include "host/commands/kernel_log_monitor/kernel_log_server.h"
30 #include "host/commands/kernel_log_monitor/utils.h"
31 #include "host/commands/run_cvd/runner_defs.h"
32 #include "host/libs/config/feature.h"
33
34 DEFINE_int32(reboot_notification_fd, -1,
35 "A file descriptor to notify when boot completes.");
36
37 namespace cuttlefish {
38 namespace {
39
40 // Forks and returns the write end of a pipe to the child process. The parent
41 // process waits for boot events to come through the pipe and exits accordingly.
DaemonizeLauncher(const CuttlefishConfig & config)42 SharedFD DaemonizeLauncher(const CuttlefishConfig& config) {
43 auto instance = config.ForDefaultInstance();
44 SharedFD read_end, write_end;
45 if (!SharedFD::Pipe(&read_end, &write_end)) {
46 LOG(ERROR) << "Unable to create pipe";
47 return {}; // a closed FD
48 }
49 auto pid = fork();
50 if (pid) {
51 // Explicitly close here, otherwise we may end up reading forever if the
52 // child process dies.
53 write_end->Close();
54 RunnerExitCodes exit_code;
55 auto bytes_read = read_end->Read(&exit_code, sizeof(exit_code));
56 if (bytes_read != sizeof(exit_code)) {
57 LOG(ERROR) << "Failed to read a complete exit code, read " << bytes_read
58 << " bytes only instead of the expected " << sizeof(exit_code);
59 exit_code = RunnerExitCodes::kPipeIOError;
60 } else if (exit_code == RunnerExitCodes::kSuccess) {
61 LOG(INFO) << "Virtual device booted successfully";
62 } else if (exit_code == RunnerExitCodes::kVirtualDeviceBootFailed) {
63 LOG(ERROR) << "Virtual device failed to boot";
64 } else {
65 LOG(ERROR) << "Unexpected exit code: " << exit_code;
66 }
67 if (exit_code == RunnerExitCodes::kSuccess) {
68 LOG(INFO) << kBootCompletedMessage;
69 } else {
70 LOG(INFO) << kBootFailedMessage;
71 }
72 std::exit(exit_code);
73 } else {
74 // The child returns the write end of the pipe
75 if (daemon(/*nochdir*/ 1, /*noclose*/ 1) != 0) {
76 LOG(ERROR) << "Failed to daemonize child process: " << strerror(errno);
77 std::exit(RunnerExitCodes::kDaemonizationError);
78 }
79 // Redirect standard I/O
80 auto log_path = instance.launcher_log_path();
81 auto log = SharedFD::Open(log_path.c_str(), O_CREAT | O_WRONLY | O_APPEND,
82 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
83 if (!log->IsOpen()) {
84 LOG(ERROR) << "Failed to create launcher log file: " << log->StrError();
85 std::exit(RunnerExitCodes::kDaemonizationError);
86 }
87 ::android::base::SetLogger(
88 TeeLogger({{LogFileSeverity(), log, MetadataLevel::FULL}}));
89 auto dev_null = SharedFD::Open("/dev/null", O_RDONLY);
90 if (!dev_null->IsOpen()) {
91 LOG(ERROR) << "Failed to open /dev/null: " << dev_null->StrError();
92 std::exit(RunnerExitCodes::kDaemonizationError);
93 }
94 if (dev_null->UNMANAGED_Dup2(0) < 0) {
95 LOG(ERROR) << "Failed dup2 stdin: " << dev_null->StrError();
96 std::exit(RunnerExitCodes::kDaemonizationError);
97 }
98 if (log->UNMANAGED_Dup2(1) < 0) {
99 LOG(ERROR) << "Failed dup2 stdout: " << log->StrError();
100 std::exit(RunnerExitCodes::kDaemonizationError);
101 }
102 if (log->UNMANAGED_Dup2(2) < 0) {
103 LOG(ERROR) << "Failed dup2 seterr: " << log->StrError();
104 std::exit(RunnerExitCodes::kDaemonizationError);
105 }
106
107 read_end->Close();
108 return write_end;
109 }
110 }
111
112 class ProcessLeader : public SetupFeature {
113 public:
INJECT(ProcessLeader (const CuttlefishConfig & config))114 INJECT(ProcessLeader(const CuttlefishConfig& config)) : config_(config) {}
115
ForegroundLauncherPipe()116 SharedFD ForegroundLauncherPipe() { return foreground_launcher_pipe_; }
117
118 // SetupFeature
Name() const119 std::string Name() const override { return "ProcessLeader"; }
Enabled() const120 bool Enabled() const override { return true; }
121
122 private:
Dependencies() const123 std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
Setup()124 bool Setup() override {
125 /* These two paths result in pretty different process state, but both
126 * achieve the same goal of making the current process the leader of a
127 * process group, and are therefore grouped together. */
128 if (config_.run_as_daemon()) {
129 foreground_launcher_pipe_ = DaemonizeLauncher(config_);
130 if (!foreground_launcher_pipe_->IsOpen()) {
131 return false;
132 }
133 } else {
134 // Make sure the launcher runs in its own process group even when running
135 // in the foreground
136 if (getsid(0) != getpid()) {
137 int retval = setpgid(0, 0);
138 if (retval) {
139 PLOG(ERROR) << "Failed to create new process group: ";
140 return false;
141 }
142 }
143 }
144 return true;
145 }
146
147 const CuttlefishConfig& config_;
148 SharedFD foreground_launcher_pipe_;
149 };
150
151 // Maintains the state of the boot process, once a final state is reached
152 // (success or failure) it sends the appropriate exit code to the foreground
153 // launcher process
154 class CvdBootStateMachine : public SetupFeature {
155 public:
INJECT(CvdBootStateMachine (ProcessLeader & process_leader,KernelLogPipeProvider & kernel_log_pipe_provider))156 INJECT(CvdBootStateMachine(ProcessLeader& process_leader,
157 KernelLogPipeProvider& kernel_log_pipe_provider))
158 : process_leader_(process_leader),
159 kernel_log_pipe_provider_(kernel_log_pipe_provider),
160 state_(kBootStarted) {}
161
~CvdBootStateMachine()162 ~CvdBootStateMachine() {
163 if (interrupt_fd_->IsOpen()) {
164 CHECK(interrupt_fd_->EventfdWrite(1) >= 0);
165 }
166 if (boot_event_handler_.joinable()) {
167 boot_event_handler_.join();
168 }
169 }
170
171 // SetupFeature
Name() const172 std::string Name() const override { return "CvdBootStateMachine"; }
Enabled() const173 bool Enabled() const override { return true; }
174
175 private:
Dependencies() const176 std::unordered_set<SetupFeature*> Dependencies() const {
177 return {
178 static_cast<SetupFeature*>(&process_leader_),
179 static_cast<SetupFeature*>(&kernel_log_pipe_provider_),
180 };
181 }
Setup()182 bool Setup() override {
183 interrupt_fd_ = SharedFD::Event();
184 if (!interrupt_fd_->IsOpen()) {
185 LOG(ERROR) << "Failed to open eventfd: " << interrupt_fd_->StrError();
186 return false;
187 }
188 fg_launcher_pipe_ = process_leader_.ForegroundLauncherPipe();
189 if (FLAGS_reboot_notification_fd >= 0) {
190 reboot_notification_ = SharedFD::Dup(FLAGS_reboot_notification_fd);
191 if (!reboot_notification_->IsOpen()) {
192 LOG(ERROR) << "Could not dup fd given for reboot_notification_fd";
193 return false;
194 }
195 close(FLAGS_reboot_notification_fd);
196 }
197 SharedFD boot_events_pipe = kernel_log_pipe_provider_.KernelLogPipe();
198 if (!boot_events_pipe->IsOpen()) {
199 LOG(ERROR) << "Could not get boot events pipe";
200 return false;
201 }
202 boot_event_handler_ = std::thread(
203 [this, boot_events_pipe]() { ThreadLoop(boot_events_pipe); });
204 return true;
205 }
206
ThreadLoop(SharedFD boot_events_pipe)207 void ThreadLoop(SharedFD boot_events_pipe) {
208 while (true) {
209 std::vector<PollSharedFd> poll_shared_fd = {
210 {
211 .fd = boot_events_pipe,
212 .events = POLLIN | POLLHUP,
213 },
214 {
215 .fd = interrupt_fd_,
216 .events = POLLIN | POLLHUP,
217 }};
218 int result = SharedFD::Poll(poll_shared_fd, -1);
219 if (poll_shared_fd[1].revents & POLLIN) {
220 return;
221 }
222 if (result < 0) {
223 PLOG(FATAL) << "Failed to call Select";
224 return;
225 }
226 if (poll_shared_fd[0].revents & POLLHUP) {
227 LOG(ERROR) << "Failed to read a complete kernel log boot event.";
228 state_ |= kGuestBootFailed;
229 if (MaybeWriteNotification()) {
230 break;
231 }
232 }
233 if (!(poll_shared_fd[0].revents & POLLIN)) {
234 continue;
235 }
236 auto sent_code = OnBootEvtReceived(boot_events_pipe);
237 if (sent_code) {
238 break;
239 }
240 }
241 }
242
243 // Returns true if the machine is left in a final state
OnBootEvtReceived(SharedFD boot_events_pipe)244 bool OnBootEvtReceived(SharedFD boot_events_pipe) {
245 std::optional<monitor::ReadEventResult> read_result =
246 monitor::ReadEvent(boot_events_pipe);
247 if (!read_result) {
248 LOG(ERROR) << "Failed to read a complete kernel log boot event.";
249 state_ |= kGuestBootFailed;
250 return MaybeWriteNotification();
251 }
252
253 if (read_result->event == monitor::Event::BootCompleted) {
254 LOG(INFO) << "Virtual device booted successfully";
255 state_ |= kGuestBootCompleted;
256 } else if (read_result->event == monitor::Event::BootFailed) {
257 LOG(ERROR) << "Virtual device failed to boot";
258 state_ |= kGuestBootFailed;
259 } // Ignore the other signals
260
261 return MaybeWriteNotification();
262 }
BootCompleted() const263 bool BootCompleted() const { return state_ & kGuestBootCompleted; }
BootFailed() const264 bool BootFailed() const { return state_ & kGuestBootFailed; }
265
SendExitCode(RunnerExitCodes exit_code,SharedFD fd)266 void SendExitCode(RunnerExitCodes exit_code, SharedFD fd) {
267 fd->Write(&exit_code, sizeof(exit_code));
268 // The foreground process will exit after receiving the exit code, if we try
269 // to write again we'll get a SIGPIPE
270 fd->Close();
271 }
MaybeWriteNotification()272 bool MaybeWriteNotification() {
273 std::vector<SharedFD> fds = {reboot_notification_, fg_launcher_pipe_};
274 for (auto& fd : fds) {
275 if (fd->IsOpen()) {
276 if (BootCompleted()) {
277 SendExitCode(RunnerExitCodes::kSuccess, fd);
278 } else if (state_ & kGuestBootFailed) {
279 SendExitCode(RunnerExitCodes::kVirtualDeviceBootFailed, fd);
280 }
281 }
282 }
283 // Either we sent the code before or just sent it, in any case the state is
284 // final
285 return BootCompleted() || (state_ & kGuestBootFailed);
286 }
287
288 ProcessLeader& process_leader_;
289 KernelLogPipeProvider& kernel_log_pipe_provider_;
290
291 std::thread boot_event_handler_;
292 SharedFD fg_launcher_pipe_;
293 SharedFD reboot_notification_;
294 SharedFD interrupt_fd_;
295 int state_;
296 static const int kBootStarted = 0;
297 static const int kGuestBootCompleted = 1 << 0;
298 static const int kGuestBootFailed = 1 << 1;
299 };
300
301 } // namespace
302
303 fruit::Component<fruit::Required<const CuttlefishConfig, KernelLogPipeProvider>>
bootStateMachineComponent()304 bootStateMachineComponent() {
305 return fruit::createComponent()
306 .addMultibinding<SetupFeature, ProcessLeader>()
307 .addMultibinding<SetupFeature, CvdBootStateMachine>();
308 }
309
310 } // namespace cuttlefish
311