blob: e352393af76f525d37553d4ca409c098226fc5ff [file] [log] [blame]
Greg Hartman86b114a2017-11-03 21:04:14 -07001/*
2 * Copyright (C) 2017 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
Tomasz Wiszkowskie99c4112017-07-05 10:17:43 -070017#include <limits.h>
Jorge E. Moreira1300f0b2018-07-10 18:14:39 -070018#include <signal.h>
Greg Hartman610c03f2017-10-31 17:44:11 -070019#include <stdio.h>
Romit Dasgupta61b572a2017-06-23 17:48:22 -070020#include <stdlib.h>
Greg Hartman13ee8b32017-11-13 21:09:15 -080021#include <sys/types.h>
22#include <sys/stat.h>
23#include <sys/wait.h>
Greg Hartman6abdbb92018-05-24 09:49:00 -070024#include <fcntl.h>
Romit Dasgupta61b572a2017-06-23 17:48:22 -070025#include <unistd.h>
Tomasz Wiszkowskie99c4112017-07-05 10:17:43 -070026
Ryan Hainingb48ceaf2018-03-27 20:13:59 -070027#include <algorithm>
Jorge E. Moreira3d838dc2018-07-10 11:49:38 -070028#include <functional>
Jorge E. Moreira5020a662018-07-09 17:07:14 -070029#include <iostream>
Tomasz Wiszkowskie99c4112017-07-05 10:17:43 -070030#include <fstream>
Greg Hartman5587b282017-11-11 12:01:45 -080031#include <iomanip>
Romit Dasgupta61b572a2017-06-23 17:48:22 -070032#include <memory>
Tomasz Wiszkowski64446de2017-08-15 15:40:37 -070033#include <sstream>
Ryan Hainingb48ceaf2018-03-27 20:13:59 -070034#include <string>
Jorge E. Moreirab5a71d72018-07-22 23:01:20 -070035#include <thread>
Ryan Hainingb48ceaf2018-03-27 20:13:59 -070036#include <vector>
Romit Dasgupta61b572a2017-06-23 17:48:22 -070037
Cody Schuffelenf4a1cdb2019-11-13 16:51:16 -080038#include <android-base/strings.h>
Tomasz Wiszkowskie99c4112017-07-05 10:17:43 -070039#include <gflags/gflags.h>
Romit Dasgupta61b572a2017-06-23 17:48:22 -070040#include <glog/logging.h>
41
Cody Schuffelen147b88e2019-09-09 16:00:11 -070042#include "common/libs/fs/shared_buf.h"
Greg Hartman6abdbb92018-05-24 09:49:00 -070043#include "common/libs/fs/shared_fd.h"
Tomasz Wiszkowski525d02b2017-08-14 11:43:04 -070044#include "common/libs/fs/shared_select.h"
Jorge E. Moreira2a777f62018-06-13 17:28:10 -070045#include "common/libs/utils/environment.h"
Jorge E. Moreira02f464c2018-07-06 11:30:34 -070046#include "common/libs/utils/files.h"
Jorge E. Moreira6a9d6292018-06-11 11:52:57 -070047#include "common/libs/utils/subprocess.h"
Jorge E. Moreira22561d92018-06-22 17:17:21 -070048#include "common/libs/utils/size_utils.h"
Cody Schuffelene46ae522019-12-05 12:55:45 -080049#include "host/commands/run_cvd/kernel_args.h"
Cody Schuffelen147b88e2019-09-09 16:00:11 -070050#include "host/commands/run_cvd/launch.h"
51#include "host/commands/run_cvd/runner_defs.h"
52#include "host/commands/run_cvd/process_monitor.h"
Jorge E. Moreira577383b2018-05-24 14:17:51 -070053#include "host/libs/config/cuttlefish_config.h"
Jorge E. Moreira66e9a1a2018-07-26 17:17:47 -070054#include "host/commands/kernel_log_monitor/kernel_log_server.h"
Jorge E. Moreira7123e2e2019-09-05 13:47:29 -070055#include <host/libs/vm_manager/crosvm_manager.h>
Jorge E. Moreiraa8142f92018-06-13 17:33:55 -070056#include "host/libs/vm_manager/vm_manager.h"
Jorge E. Moreira88322832018-07-22 16:41:01 -070057#include "host/libs/vm_manager/qemu_manager.h"
Tomasz Wiszkowskibb6c1622017-09-05 14:03:11 -070058
Greg Hartman36cc7c42017-11-13 18:05:01 -080059using vsoc::GetPerInstanceDefault;
Cody Schuffelena9d9a862019-09-10 16:16:06 -070060using cvd::RunnerExitCodes;
Greg Hartman36cc7c42017-11-13 18:05:01 -080061
Tomasz Wiszkowskie99c4112017-07-05 10:17:43 -070062namespace {
Ping-Hao Wufe9f69b2018-01-10 16:43:26 -080063
Cody Schuffelend12e0c92019-01-29 15:44:45 -080064cvd::OnSocketReadyCb GetOnSubprocessExitCallback(
65 const vsoc::CuttlefishConfig& config) {
66 if (config.restart_subprocesses()) {
67 return cvd::ProcessMonitor::RestartOnExitCb;
Jorge E. Moreira3d955ae2018-11-07 23:06:44 -080068 } else {
Cody Schuffelend12e0c92019-01-29 15:44:45 -080069 return cvd::ProcessMonitor::DoNotMonitorCb;
Jorge E. Moreira3d955ae2018-11-07 23:06:44 -080070 }
71}
72
Jorge E. Moreira734e1d12018-11-07 20:30:38 -080073// Maintains the state of the boot process, once a final state is reached
74// (success or failure) it sends the appropriate exit code to the foreground
75// launcher process
76class CvdBootStateMachine {
77 public:
78 CvdBootStateMachine(cvd::SharedFD fg_launcher_pipe)
79 : fg_launcher_pipe_(fg_launcher_pipe), state_(kBootStarted) {}
80
81 // Returns true if the machine is left in a final state
82 bool OnBootEvtReceived(cvd::SharedFD boot_events_pipe) {
83 monitor::BootEvent evt;
84 auto bytes_read = boot_events_pipe->Read(&evt, sizeof(evt));
85 if (bytes_read != sizeof(evt)) {
86 LOG(ERROR) << "Fail to read a complete event, read " << bytes_read
87 << " bytes only instead of the expected " << sizeof(evt);
88 state_ |= kGuestBootFailed;
89 } else if (evt == monitor::BootEvent::BootCompleted) {
90 LOG(INFO) << "Virtual device booted successfully";
91 state_ |= kGuestBootCompleted;
92 } else if (evt == monitor::BootEvent::BootFailed) {
93 LOG(ERROR) << "Virtual device failed to boot";
94 state_ |= kGuestBootFailed;
95 } // Ignore the other signals
96
97 return MaybeWriteToForegroundLauncher();
98 }
99
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800100 bool BootCompleted() const {
Cody Schuffelene9008372019-11-19 18:51:18 -0800101 return state_ & kGuestBootCompleted;
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800102 }
103
104 bool BootFailed() const {
Cody Schuffelene9008372019-11-19 18:51:18 -0800105 return state_ & kGuestBootFailed;
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800106 }
107
108 private:
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700109 void SendExitCode(cvd::RunnerExitCodes exit_code) {
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800110 fg_launcher_pipe_->Write(&exit_code, sizeof(exit_code));
111 // The foreground process will exit after receiving the exit code, if we try
112 // to write again we'll get a SIGPIPE
113 fg_launcher_pipe_->Close();
114 }
115 bool MaybeWriteToForegroundLauncher() {
116 if (fg_launcher_pipe_->IsOpen()) {
117 if (BootCompleted()) {
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700118 SendExitCode(cvd::RunnerExitCodes::kSuccess);
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800119 } else if (state_ & kGuestBootFailed) {
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700120 SendExitCode(cvd::RunnerExitCodes::kVirtualDeviceBootFailed);
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800121 } else {
122 // No final state was reached
123 return false;
124 }
125 }
126 // Either we sent the code before or just sent it, in any case the state is
127 // final
128 return true;
129 }
130
131 cvd::SharedFD fg_launcher_pipe_;
132 int state_;
133 static const int kBootStarted = 0;
134 static const int kGuestBootCompleted = 1 << 0;
135 static const int kGuestBootFailed = 1 << 1;
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800136};
137
138// Abuse the process monitor to make it call us back when boot events are ready
139void SetUpHandlingOfBootEvents(
140 cvd::ProcessMonitor* process_monitor, cvd::SharedFD boot_events_pipe,
141 std::shared_ptr<CvdBootStateMachine> state_machine) {
142 process_monitor->MonitorExistingSubprocess(
143 // A dummy command, so logs are desciptive
144 cvd::Command("boot_events_listener"),
145 // A dummy subprocess, with the boot events pipe as control socket
146 cvd::Subprocess(-1, boot_events_pipe),
147 [boot_events_pipe, state_machine](cvd::MonitorEntry*) {
148 auto sent_code = state_machine->OnBootEvtReceived(boot_events_pipe);
149 return !sent_code;
150 });
151}
152
Jorge E. Moreiraa00584e2018-10-25 15:07:52 -0700153bool WriteCuttlefishEnvironment(const vsoc::CuttlefishConfig& config) {
154 auto env = cvd::SharedFD::Open(config.cuttlefish_env_path().c_str(),
Greg Hartman6abdbb92018-05-24 09:49:00 -0700155 O_CREAT | O_RDWR, 0755);
156 if (!env->IsOpen()) {
157 LOG(ERROR) << "Unable to create cuttlefish.env file";
158 return false;
159 }
160 std::string config_env = "export CUTTLEFISH_PER_INSTANCE_PATH=\"" +
Jorge E. Moreiraa00584e2018-10-25 15:07:52 -0700161 config.PerInstancePath(".") + "\"\n";
Cody Schuffelen3c35fd82019-12-11 18:39:46 -0800162 config_env += "export ANDROID_SERIAL=" + config.adb_ip_and_port() + "\n";
Greg Hartman6abdbb92018-05-24 09:49:00 -0700163 env->Write(config_env.c_str(), config_env.size());
164 return true;
165}
Jorge E. Moreira1300f0b2018-07-10 18:14:39 -0700166
167// Forks and returns the write end of a pipe to the child process. The parent
168// process waits for boot events to come through the pipe and exits accordingly.
Jorge E. Moreiraa00584e2018-10-25 15:07:52 -0700169cvd::SharedFD DaemonizeLauncher(const vsoc::CuttlefishConfig& config) {
Jorge E. Moreira1300f0b2018-07-10 18:14:39 -0700170 cvd::SharedFD read_end, write_end;
171 if (!cvd::SharedFD::Pipe(&read_end, &write_end)) {
172 LOG(ERROR) << "Unable to create pipe";
173 return cvd::SharedFD(); // a closed FD
174 }
175 auto pid = fork();
176 if (pid) {
177 // Explicitly close here, otherwise we may end up reading forever if the
178 // child process dies.
179 write_end->Close();
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700180 RunnerExitCodes exit_code;
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800181 auto bytes_read = read_end->Read(&exit_code, sizeof(exit_code));
182 if (bytes_read != sizeof(exit_code)) {
183 LOG(ERROR) << "Failed to read a complete exit code, read " << bytes_read
184 << " bytes only instead of the expected " << sizeof(exit_code);
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700185 exit_code = RunnerExitCodes::kPipeIOError;
186 } else if (exit_code == RunnerExitCodes::kSuccess) {
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800187 LOG(INFO) << "Virtual device booted successfully";
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700188 } else if (exit_code == RunnerExitCodes::kVirtualDeviceBootFailed) {
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800189 LOG(ERROR) << "Virtual device failed to boot";
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800190 } else {
191 LOG(ERROR) << "Unexpected exit code: " << exit_code;
Jorge E. Moreira1300f0b2018-07-10 18:14:39 -0700192 }
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700193 if (exit_code == RunnerExitCodes::kSuccess) {
Jorge E. Moreira8dad5042019-10-01 16:04:52 -0700194 LOG(INFO) << vsoc::kBootCompletedMessage;
195 } else {
196 LOG(INFO) << vsoc::kBootFailedMessage;
197 }
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800198 std::exit(exit_code);
Jorge E. Moreira1300f0b2018-07-10 18:14:39 -0700199 } else {
200 // The child returns the write end of the pipe
Jorge E. Moreira66f6ec82018-07-16 16:43:15 -0700201 if (daemon(/*nochdir*/ 1, /*noclose*/ 1) != 0) {
202 LOG(ERROR) << "Failed to daemonize child process: " << strerror(errno);
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700203 std::exit(RunnerExitCodes::kDaemonizationError);
Jorge E. Moreira66f6ec82018-07-16 16:43:15 -0700204 }
205 // Redirect standard I/O
Jorge E. Moreiraa00584e2018-10-25 15:07:52 -0700206 auto log_path = config.launcher_log_path();
Jorge E. Moreira66f6ec82018-07-16 16:43:15 -0700207 auto log =
208 cvd::SharedFD::Open(log_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC,
209 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
210 if (!log->IsOpen()) {
211 LOG(ERROR) << "Failed to create launcher log file: " << log->StrError();
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700212 std::exit(RunnerExitCodes::kDaemonizationError);
Jorge E. Moreira66f6ec82018-07-16 16:43:15 -0700213 }
214 auto dev_null = cvd::SharedFD::Open("/dev/null", O_RDONLY);
215 if (!dev_null->IsOpen()) {
216 LOG(ERROR) << "Failed to open /dev/null: " << dev_null->StrError();
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700217 std::exit(RunnerExitCodes::kDaemonizationError);
Jorge E. Moreira66f6ec82018-07-16 16:43:15 -0700218 }
219 if (dev_null->UNMANAGED_Dup2(0) < 0) {
220 LOG(ERROR) << "Failed dup2 stdin: " << dev_null->StrError();
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700221 std::exit(RunnerExitCodes::kDaemonizationError);
Jorge E. Moreira66f6ec82018-07-16 16:43:15 -0700222 }
223 if (log->UNMANAGED_Dup2(1) < 0) {
224 LOG(ERROR) << "Failed dup2 stdout: " << log->StrError();
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700225 std::exit(RunnerExitCodes::kDaemonizationError);
Jorge E. Moreira66f6ec82018-07-16 16:43:15 -0700226 }
227 if (log->UNMANAGED_Dup2(2) < 0) {
228 LOG(ERROR) << "Failed dup2 seterr: " << log->StrError();
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700229 std::exit(RunnerExitCodes::kDaemonizationError);
Jorge E. Moreira66f6ec82018-07-16 16:43:15 -0700230 }
231
Jorge E. Moreira1300f0b2018-07-10 18:14:39 -0700232 read_end->Close();
233 return write_end;
234 }
235}
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700236
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700237void ServerLoop(cvd::SharedFD server,
Jorge E. Moreira2be306d2019-08-30 11:54:32 -0700238 cvd::ProcessMonitor* process_monitor) {
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700239 while (true) {
240 // TODO: use select to handle simultaneous connections.
241 auto client = cvd::SharedFD::Accept(*server);
242 cvd::LauncherAction action;
Jorge E. Moreirabe7049a2018-07-26 18:12:13 -0700243 while (client->IsOpen() && client->Read(&action, sizeof(action)) > 0) {
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700244 switch (action) {
245 case cvd::LauncherAction::kStop:
Jorge E. Moreira7123e2e2019-09-05 13:47:29 -0700246 if (process_monitor->StopMonitoredProcesses()) {
247 auto response = cvd::LauncherResponse::kSuccess;
248 client->Write(&response, sizeof(response));
249 std::exit(0);
250 } else {
251 auto response = cvd::LauncherResponse::kError;
252 client->Write(&response, sizeof(response));
253 }
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700254 break;
Cody Schuffelen4f8e3302019-10-23 11:21:10 -0700255 case cvd::LauncherAction::kStatus: {
256 // TODO(schuffelen): Return more information on a side channel
257 auto response = cvd::LauncherResponse::kSuccess;
258 client->Write(&response, sizeof(response));
259 break;
260 }
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700261 default:
262 LOG(ERROR) << "Unrecognized launcher action: "
263 << static_cast<char>(action);
Jorge E. Moreira7123e2e2019-09-05 13:47:29 -0700264 auto response = cvd::LauncherResponse::kError;
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700265 client->Write(&response, sizeof(response));
266 }
267 }
268 }
269}
Cody Schuffelen147b88e2019-09-09 16:00:11 -0700270
271std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) {
272 return config.PerInstancePath("cuttlefish_config.json");
273}
274
Greg Hartman98384ee2018-05-24 09:49:00 -0700275} // namespace
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700276
Cody Schuffelen0b2124b2019-09-16 18:04:17 -0700277int main(int argc, char** argv) {
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700278 ::android::base::InitLogging(argv, android::base::StderrLogger);
Cody Schuffelen0b2124b2019-09-16 18:04:17 -0700279 google::ParseCommandLineFlags(&argc, &argv, false);
Jorge E. Moreira38193242018-07-06 11:12:13 -0700280
Cody Schuffelen5861bb32019-09-10 16:42:12 -0700281 if (isatty(0)) {
282 LOG(FATAL) << "stdin was a tty, expected to be passed the output of a previous stage. "
283 << "Did you mean to run launch_cvd?";
284 return cvd::RunnerExitCodes::kInvalidHostConfiguration;
285 } else {
286 int error_num = errno;
287 if (error_num == EBADF) {
288 LOG(FATAL) << "stdin was not a valid file descriptor, expected to be passed the output "
289 << "of assemble_cvd. Did you mean to run launch_cvd?";
290 return cvd::RunnerExitCodes::kInvalidHostConfiguration;
291 }
292 }
293
Cody Schuffelen147b88e2019-09-09 16:00:11 -0700294 std::string input_files_str;
295 {
296 auto input_fd = cvd::SharedFD::Dup(0);
297 auto bytes_read = cvd::ReadAll(input_fd, &input_files_str);
298 if (bytes_read < 0) {
299 LOG(FATAL) << "Failed to read input files. Error was \"" << input_fd->StrError() << "\"";
300 }
301 }
Cody Schuffelenf4a1cdb2019-11-13 16:51:16 -0800302 std::vector<std::string> input_files = android::base::Split(input_files_str, "\n");
Cody Schuffelen147b88e2019-09-09 16:00:11 -0700303 bool found_config = false;
304 for (const auto& file : input_files) {
305 if (file.find("cuttlefish_config.json") != std::string::npos) {
306 found_config = true;
307 setenv(vsoc::kCuttlefishConfigEnvVarName, file.c_str(), /* overwrite */ false);
308 }
309 }
310 if (!found_config) {
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700311 return RunnerExitCodes::kCuttlefishConfigurationInitError;
Cody Schuffelen147b88e2019-09-09 16:00:11 -0700312 }
313
314 auto config = vsoc::CuttlefishConfig::Get();
Cody Schuffelen4524bba2019-01-29 15:57:59 -0800315
Jorge E. Moreiraec10b722019-09-06 18:52:22 -0700316 // Change working directory to the instance directory as early as possible to
317 // ensure all host processes have the same working dir. This helps stop_cvd
318 // find the running processes when it can't establish a communication with the
319 // launcher.
320 auto chdir_ret = chdir(config->instance_dir().c_str());
321 if (chdir_ret != 0) {
322 auto error = errno;
323 LOG(ERROR) << "Unable to change dir into instance directory ("
324 << config->instance_dir() << "): " << strerror(error);
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700325 return RunnerExitCodes::kInstanceDirCreationError;
Jorge E. Moreiraec10b722019-09-06 18:52:22 -0700326 }
327
Jorge E. Moreira88322832018-07-22 16:41:01 -0700328 auto vm_manager = vm_manager::VmManager::Get(config->vm_manager(), config);
Jorge E. Moreira38193242018-07-06 11:12:13 -0700329
Jorge E. Moreira5020a662018-07-09 17:07:14 -0700330 // Check host configuration
331 std::vector<std::string> config_commands;
332 if (!vm_manager->ValidateHostConfiguration(&config_commands)) {
333 LOG(ERROR) << "Validation of user configuration failed";
334 std::cout << "Execute the following to correctly configure:" << std::endl;
335 for (auto& command : config_commands) {
336 std::cout << " " << command << std::endl;
337 }
338 std::cout << "You may need to logout for the changes to take effect"
339 << std::endl;
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700340 return RunnerExitCodes::kInvalidHostConfiguration;
Jorge E. Moreira5020a662018-07-09 17:07:14 -0700341 }
342
Jorge E. Moreiraa00584e2018-10-25 15:07:52 -0700343 if (!WriteCuttlefishEnvironment(*config)) {
Greg Hartman6abdbb92018-05-24 09:49:00 -0700344 LOG(ERROR) << "Unable to write cuttlefish environment file";
345 }
346
Jorge E. Moreiracb7d46c2018-07-09 18:04:53 -0700347 LOG(INFO) << "The following files contain useful debugging information:";
Cody Schuffelen50249732019-01-29 16:42:55 -0800348 if (config->run_as_daemon()) {
Jorge E. Moreira66f6ec82018-07-16 16:43:15 -0700349 LOG(INFO) << " Launcher log: " << config->launcher_log_path();
350 }
Jorge E. Moreiracb7d46c2018-07-09 18:04:53 -0700351 LOG(INFO) << " Android's logcat output: " << config->logcat_path();
352 LOG(INFO) << " Kernel log: " << config->PerInstancePath("kernel.log");
Jorge E. Moreiraa00584e2018-10-25 15:07:52 -0700353 LOG(INFO) << " Instance configuration: " << GetConfigFilePath(*config);
Jorge E. Moreiracb7d46c2018-07-09 18:04:53 -0700354 LOG(INFO) << " Instance environment: " << config->cuttlefish_env_path();
355 LOG(INFO) << "To access the console run: socat file:$(tty),raw,echo=0 "
356 << config->console_path();
357
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700358 auto launcher_monitor_path = config->launcher_monitor_socket_path();
359 auto launcher_monitor_socket = cvd::SharedFD::SocketLocalServer(
360 launcher_monitor_path.c_str(), false, SOCK_STREAM, 0666);
361 if (!launcher_monitor_socket->IsOpen()) {
Jorge E. Moreira81afca12018-07-26 16:48:49 -0700362 LOG(ERROR) << "Error when opening launcher server: "
363 << launcher_monitor_socket->StrError();
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700364 return cvd::RunnerExitCodes::kMonitorCreationFailed;
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700365 }
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800366 cvd::SharedFD foreground_launcher_pipe;
Cody Schuffelen50249732019-01-29 16:42:55 -0800367 if (config->run_as_daemon()) {
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800368 foreground_launcher_pipe = DaemonizeLauncher(*config);
369 if (!foreground_launcher_pipe->IsOpen()) {
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700370 return RunnerExitCodes::kDaemonizationError;
Jorge E. Moreira1300f0b2018-07-10 18:14:39 -0700371 }
Jorge E. Moreira23631092018-07-17 10:51:25 -0700372 } else {
373 // Make sure the launcher runs in its own process group even when running in
374 // foreground
Jorge E. Moreira66e8ff92018-07-24 11:52:23 -0700375 if (getsid(0) != getpid()) {
376 int retval = setpgid(0, 0);
377 if (retval) {
378 LOG(ERROR) << "Failed to create new process group: " << strerror(errno);
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700379 std::exit(RunnerExitCodes::kProcessGroupError);
Jorge E. Moreira66e8ff92018-07-24 11:52:23 -0700380 }
Jorge E. Moreira23631092018-07-17 10:51:25 -0700381 }
Jorge E. Moreira1300f0b2018-07-10 18:14:39 -0700382 }
383
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800384 auto boot_state_machine =
385 std::make_shared<CvdBootStateMachine>(foreground_launcher_pipe);
386
Jorge E. Moreira1a62e762018-11-05 22:05:57 -0800387 // Monitor and restart host processes supporting the CVD
388 cvd::ProcessMonitor process_monitor;
389
Jorge E. Moreira277e1c42019-05-06 17:24:27 -0700390 auto event_pipes =
391 LaunchKernelLogMonitor(*config, &process_monitor, 2);
392 cvd::SharedFD boot_events_pipe = event_pipes[0];
393 cvd::SharedFD adbd_events_pipe = event_pipes[1];
394 event_pipes.clear();
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800395
Cody Schuffelenb13869b2019-11-26 18:46:34 -0800396 std::set<std::string> extra_kernel_cmdline;
397
Jorge E. Moreira734e1d12018-11-07 20:30:38 -0800398 SetUpHandlingOfBootEvents(&process_monitor, boot_events_pipe,
399 boot_state_machine);
400
Cody Schuffeleneb5c2152019-12-05 15:44:49 -0800401 auto logcat_server = LaunchLogcatReceiverIfEnabled(*config, &process_monitor);
402 auto logcat_server_args = KernelCommandLineFromLogcatServer(logcat_server);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800403
Cody Schuffelenc9183ba2019-12-05 15:23:46 -0800404 auto config_server = LaunchConfigServer(*config, &process_monitor);
405 auto config_server_args = KernelCommandLineFromConfigServer(config_server);
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700406
Cody Schuffelend3f27b92019-11-26 19:04:43 -0800407 auto tombstone_server = LaunchTombstoneReceiverIfEnabled(*config, &process_monitor);
408 auto tombstone_kernel_args = KernelCommandLineFromTombstone(tombstone_server);
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700409
Jorge E. Moreira7e1df152019-05-14 15:05:33 -0700410 // The vnc server needs to be launched after the ivserver because it connects
411 // to it when using qemu. It needs to launch before the VMM because it serves
412 // on several sockets (input devices, vsock frame server) when using crosvm.
Cody Schuffelenb13869b2019-11-26 18:46:34 -0800413 auto vnc_server_config = LaunchVNCServerIfEnabled(
Jorge E. Moreira7e1df152019-05-14 15:05:33 -0700414 *config, &process_monitor, GetOnSubprocessExitCallback(*config));
Cody Schuffelenb13869b2019-11-26 18:46:34 -0800415 auto vnc_kernel_args = KernelCommandLineFromVnc(vnc_server_config);
416
417 auto kernel_args = KernelCommandLineFromConfig(*config);
418 kernel_args.insert(kernel_args.end(), vnc_kernel_args.begin(), vnc_kernel_args.end());
Cody Schuffelend3f27b92019-11-26 19:04:43 -0800419 kernel_args.insert(kernel_args.end(), tombstone_kernel_args.begin(),
420 tombstone_kernel_args.end());
Cody Schuffelenc9183ba2019-12-05 15:23:46 -0800421 kernel_args.insert(kernel_args.end(), config_server_args.begin(), config_server_args.end());
Cody Schuffeleneb5c2152019-12-05 15:44:49 -0800422 kernel_args.insert(kernel_args.end(), logcat_server_args.begin(), logcat_server_args.end());
Jorge E. Moreira7e1df152019-05-14 15:05:33 -0700423
Jorge E. Moreira9aeb97d2019-01-28 23:19:02 -0800424 // Start the guest VM
Cody Schuffelenb13869b2019-11-26 18:46:34 -0800425 vm_manager->WithFrontend(vnc_kernel_args.size() > 0);
Cody Schuffelen2de6ead2019-12-04 16:16:47 -0800426 vm_manager->WithKernelCommandLine(android::base::Join(kernel_args, " "));
Cody Schuffelen97359f82019-11-26 18:40:15 -0800427 auto vmm_commands = vm_manager->StartCommands();
Jorge E. Moreira13cbd982019-06-06 16:06:34 -0700428 for (auto& vmm_cmd: vmm_commands) {
429 process_monitor.StartSubprocess(std::move(vmm_cmd),
430 GetOnSubprocessExitCallback(*config));
431 }
Jorge E. Moreira4ca884e2018-01-25 16:57:48 -0800432
Jorge E. Moreira9aeb97d2019-01-28 23:19:02 -0800433 // Start other host processes
Cody Schuffelend12e0c92019-01-29 15:44:45 -0800434 LaunchSocketVsockProxyIfEnabled(&process_monitor, *config);
Jorge E. Moreira9ec15852019-05-02 17:52:58 -0700435 LaunchAdbConnectorIfEnabled(&process_monitor, *config, adbd_events_pipe);
Andreas Huber21b7af52018-03-01 10:01:55 -0800436
Jorge E. Moreira7123e2e2019-09-05 13:47:29 -0700437 ServerLoop(launcher_monitor_socket, &process_monitor); // Should not return
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700438 LOG(ERROR) << "The server loop returned, it should never happen!!";
Cody Schuffelena9d9a862019-09-10 16:16:06 -0700439 return cvd::RunnerExitCodes::kServerError;
Romit Dasgupta61b572a2017-06-23 17:48:22 -0700440}