1 /*
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
17 #include <limits.h>
18 #include <signal.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/wait.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26
27 #include <algorithm>
28 #include <functional>
29 #include <iostream>
30 #include <fstream>
31 #include <iomanip>
32 #include <memory>
33 #include <sstream>
34 #include <string>
35 #include <thread>
36 #include <vector>
37
38 #include <android-base/strings.h>
39 #include <gflags/gflags.h>
40 #include <android-base/logging.h>
41
42 #include "common/libs/fs/shared_buf.h"
43 #include "common/libs/fs/shared_fd.h"
44 #include "common/libs/fs/shared_select.h"
45 #include "common/libs/utils/environment.h"
46 #include "common/libs/utils/files.h"
47 #include "common/libs/utils/network.h"
48 #include "common/libs/utils/subprocess.h"
49 #include "common/libs/utils/size_utils.h"
50 #include "common/libs/utils/tee_logging.h"
51 #include "host/commands/run_cvd/launch.h"
52 #include "host/commands/run_cvd/runner_defs.h"
53 #include "host/commands/run_cvd/process_monitor.h"
54 #include "host/libs/config/cuttlefish_config.h"
55 #include "host/libs/config/data_image.h"
56 #include "host/libs/config/kernel_args.h"
57 #include "host/commands/kernel_log_monitor/kernel_log_server.h"
58 #include <host/libs/vm_manager/crosvm_manager.h>
59 #include "host/libs/vm_manager/vm_manager.h"
60 #include "host/libs/vm_manager/qemu_manager.h"
61
62 using cuttlefish::ForCurrentInstance;
63 using cuttlefish::RunnerExitCodes;
64 using cuttlefish::vm_manager::VmManager;
65
66 namespace {
67
GetOnSubprocessExitCallback(const cuttlefish::CuttlefishConfig & config)68 cuttlefish::OnSocketReadyCb GetOnSubprocessExitCallback(
69 const cuttlefish::CuttlefishConfig& config) {
70 if (config.restart_subprocesses()) {
71 return cuttlefish::ProcessMonitor::RestartOnExitCb;
72 } else {
73 return cuttlefish::ProcessMonitor::DoNotMonitorCb;
74 }
75 }
76
77 // Maintains the state of the boot process, once a final state is reached
78 // (success or failure) it sends the appropriate exit code to the foreground
79 // launcher process
80 class CvdBootStateMachine {
81 public:
CvdBootStateMachine(cuttlefish::SharedFD fg_launcher_pipe)82 CvdBootStateMachine(cuttlefish::SharedFD fg_launcher_pipe)
83 : fg_launcher_pipe_(fg_launcher_pipe), state_(kBootStarted) {}
84
85 // Returns true if the machine is left in a final state
OnBootEvtReceived(cuttlefish::SharedFD boot_events_pipe)86 bool OnBootEvtReceived(cuttlefish::SharedFD boot_events_pipe) {
87 monitor::BootEvent evt;
88 auto bytes_read = boot_events_pipe->Read(&evt, sizeof(evt));
89 if (bytes_read != sizeof(evt)) {
90 LOG(ERROR) << "Fail to read a complete event, read " << bytes_read
91 << " bytes only instead of the expected " << sizeof(evt);
92 state_ |= kGuestBootFailed;
93 } else if (evt == monitor::BootEvent::BootCompleted) {
94 LOG(INFO) << "Virtual device booted successfully";
95 state_ |= kGuestBootCompleted;
96 } else if (evt == monitor::BootEvent::BootFailed) {
97 LOG(ERROR) << "Virtual device failed to boot";
98 state_ |= kGuestBootFailed;
99 } // Ignore the other signals
100
101 return MaybeWriteToForegroundLauncher();
102 }
103
BootCompleted() const104 bool BootCompleted() const {
105 return state_ & kGuestBootCompleted;
106 }
107
BootFailed() const108 bool BootFailed() const {
109 return state_ & kGuestBootFailed;
110 }
111
112 private:
SendExitCode(cuttlefish::RunnerExitCodes exit_code)113 void SendExitCode(cuttlefish::RunnerExitCodes exit_code) {
114 fg_launcher_pipe_->Write(&exit_code, sizeof(exit_code));
115 // The foreground process will exit after receiving the exit code, if we try
116 // to write again we'll get a SIGPIPE
117 fg_launcher_pipe_->Close();
118 }
MaybeWriteToForegroundLauncher()119 bool MaybeWriteToForegroundLauncher() {
120 if (fg_launcher_pipe_->IsOpen()) {
121 if (BootCompleted()) {
122 SendExitCode(cuttlefish::RunnerExitCodes::kSuccess);
123 } else if (state_ & kGuestBootFailed) {
124 SendExitCode(cuttlefish::RunnerExitCodes::kVirtualDeviceBootFailed);
125 } else {
126 // No final state was reached
127 return false;
128 }
129 }
130 // Either we sent the code before or just sent it, in any case the state is
131 // final
132 return true;
133 }
134
135 cuttlefish::SharedFD fg_launcher_pipe_;
136 int state_;
137 static const int kBootStarted = 0;
138 static const int kGuestBootCompleted = 1 << 0;
139 static const int kGuestBootFailed = 1 << 1;
140 };
141
142 // Abuse the process monitor to make it call us back when boot events are ready
SetUpHandlingOfBootEvents(cuttlefish::ProcessMonitor * process_monitor,cuttlefish::SharedFD boot_events_pipe,std::shared_ptr<CvdBootStateMachine> state_machine)143 void SetUpHandlingOfBootEvents(
144 cuttlefish::ProcessMonitor* process_monitor, cuttlefish::SharedFD boot_events_pipe,
145 std::shared_ptr<CvdBootStateMachine> state_machine) {
146 process_monitor->MonitorExistingSubprocess(
147 // An unused command, so logs are desciptive
148 cuttlefish::Command("boot_events_listener"),
149 // An unused subprocess, with the boot events pipe as control socket
150 cuttlefish::Subprocess(-1, boot_events_pipe),
151 [boot_events_pipe, state_machine](cuttlefish::MonitorEntry*) {
152 auto sent_code = state_machine->OnBootEvtReceived(boot_events_pipe);
153 return !sent_code;
154 });
155 }
156
WriteCuttlefishEnvironment(const cuttlefish::CuttlefishConfig & config)157 bool WriteCuttlefishEnvironment(const cuttlefish::CuttlefishConfig& config) {
158 auto env = cuttlefish::SharedFD::Open(config.cuttlefish_env_path().c_str(),
159 O_CREAT | O_RDWR, 0755);
160 if (!env->IsOpen()) {
161 LOG(ERROR) << "Unable to create cuttlefish.env file";
162 return false;
163 }
164 auto instance = config.ForDefaultInstance();
165 std::string config_env = "export CUTTLEFISH_PER_INSTANCE_PATH=\"" +
166 instance.PerInstancePath(".") + "\"\n";
167 config_env += "export ANDROID_SERIAL=" + instance.adb_ip_and_port() + "\n";
168 env->Write(config_env.c_str(), config_env.size());
169 return true;
170 }
171
172 // Forks and returns the write end of a pipe to the child process. The parent
173 // process waits for boot events to come through the pipe and exits accordingly.
DaemonizeLauncher(const cuttlefish::CuttlefishConfig & config)174 cuttlefish::SharedFD DaemonizeLauncher(const cuttlefish::CuttlefishConfig& config) {
175 auto instance = config.ForDefaultInstance();
176 cuttlefish::SharedFD read_end, write_end;
177 if (!cuttlefish::SharedFD::Pipe(&read_end, &write_end)) {
178 LOG(ERROR) << "Unable to create pipe";
179 return cuttlefish::SharedFD(); // a closed FD
180 }
181 auto pid = fork();
182 if (pid) {
183 // Explicitly close here, otherwise we may end up reading forever if the
184 // child process dies.
185 write_end->Close();
186 RunnerExitCodes exit_code;
187 auto bytes_read = read_end->Read(&exit_code, sizeof(exit_code));
188 if (bytes_read != sizeof(exit_code)) {
189 LOG(ERROR) << "Failed to read a complete exit code, read " << bytes_read
190 << " bytes only instead of the expected " << sizeof(exit_code);
191 exit_code = RunnerExitCodes::kPipeIOError;
192 } else if (exit_code == RunnerExitCodes::kSuccess) {
193 LOG(INFO) << "Virtual device booted successfully";
194 } else if (exit_code == RunnerExitCodes::kVirtualDeviceBootFailed) {
195 LOG(ERROR) << "Virtual device failed to boot";
196 } else {
197 LOG(ERROR) << "Unexpected exit code: " << exit_code;
198 }
199 if (exit_code == RunnerExitCodes::kSuccess) {
200 LOG(INFO) << cuttlefish::kBootCompletedMessage;
201 } else {
202 LOG(INFO) << cuttlefish::kBootFailedMessage;
203 }
204 std::exit(exit_code);
205 } else {
206 // The child returns the write end of the pipe
207 if (daemon(/*nochdir*/ 1, /*noclose*/ 1) != 0) {
208 LOG(ERROR) << "Failed to daemonize child process: " << strerror(errno);
209 std::exit(RunnerExitCodes::kDaemonizationError);
210 }
211 // Redirect standard I/O
212 auto log_path = instance.launcher_log_path();
213 auto log =
214 cuttlefish::SharedFD::Open(log_path.c_str(), O_CREAT | O_WRONLY | O_APPEND,
215 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
216 if (!log->IsOpen()) {
217 LOG(ERROR) << "Failed to create launcher log file: " << log->StrError();
218 std::exit(RunnerExitCodes::kDaemonizationError);
219 }
220 ::android::base::SetLogger(cuttlefish::TeeLogger({
221 {cuttlefish::LogFileSeverity(), log},
222 }));
223 auto dev_null = cuttlefish::SharedFD::Open("/dev/null", O_RDONLY);
224 if (!dev_null->IsOpen()) {
225 LOG(ERROR) << "Failed to open /dev/null: " << dev_null->StrError();
226 std::exit(RunnerExitCodes::kDaemonizationError);
227 }
228 if (dev_null->UNMANAGED_Dup2(0) < 0) {
229 LOG(ERROR) << "Failed dup2 stdin: " << dev_null->StrError();
230 std::exit(RunnerExitCodes::kDaemonizationError);
231 }
232 if (log->UNMANAGED_Dup2(1) < 0) {
233 LOG(ERROR) << "Failed dup2 stdout: " << log->StrError();
234 std::exit(RunnerExitCodes::kDaemonizationError);
235 }
236 if (log->UNMANAGED_Dup2(2) < 0) {
237 LOG(ERROR) << "Failed dup2 seterr: " << log->StrError();
238 std::exit(RunnerExitCodes::kDaemonizationError);
239 }
240
241 read_end->Close();
242 return write_end;
243 }
244 }
245
CreateQcowOverlay(const std::string & crosvm_path,const std::string & backing_file,const std::string & output_overlay_path)246 bool CreateQcowOverlay(const std::string& crosvm_path,
247 const std::string& backing_file,
248 const std::string& output_overlay_path) {
249 cuttlefish::Command crosvm_qcow2_cmd(crosvm_path);
250 crosvm_qcow2_cmd.AddParameter("create_qcow2");
251 crosvm_qcow2_cmd.AddParameter("--backing_file=", backing_file);
252 crosvm_qcow2_cmd.AddParameter(output_overlay_path);
253 int success = crosvm_qcow2_cmd.Start().Wait();
254 if (success != 0) {
255 LOG(ERROR) << "Unable to run crosvm create_qcow2. Exited with status " << success;
256 return false;
257 }
258 return true;
259 }
260
PowerwashFiles()261 bool PowerwashFiles() {
262 auto config = cuttlefish::CuttlefishConfig::Get();
263 if (!config) {
264 LOG(ERROR) << "Could not load the config.";
265 return false;
266 }
267 using cuttlefish::CreateBlankImage;
268 auto instance = config->ForDefaultInstance();
269
270 // TODO(schuffelen): Create these FIFOs in assemble_cvd instead of run_cvd.
271 auto kernel_log_pipe = instance.kernel_log_pipe_name();
272 unlink(kernel_log_pipe.c_str());
273
274 auto console_in_pipe = instance.console_in_pipe_name();
275 unlink(console_in_pipe.c_str());
276
277 auto console_out_pipe = instance.console_out_pipe_name();
278 unlink(console_out_pipe.c_str());
279
280 auto logcat_pipe = instance.logcat_pipe_name();
281 unlink(logcat_pipe.c_str());
282
283 // TODO(schuffelen): Clean up duplication with assemble_cvd
284 auto kregistry_path = instance.access_kregistry_path();
285 unlink(kregistry_path.c_str());
286 CreateBlankImage(kregistry_path, 2 /* mb */, "none");
287
288 auto pstore_path = instance.pstore_path();
289 unlink(pstore_path.c_str());
290 CreateBlankImage(pstore_path, 2 /* mb */, "none");
291
292 auto sdcard_path = instance.sdcard_path();
293 auto sdcard_size = cuttlefish::FileSize(sdcard_path);
294 unlink(sdcard_path.c_str());
295 // round up
296 auto sdcard_mb_size = (sdcard_size + (1 << 20) - 1) / (1 << 20);
297 LOG(DEBUG) << "Size in mb is " << sdcard_mb_size;
298 CreateBlankImage(sdcard_path, sdcard_mb_size, "sdcard");
299
300 auto overlay_path = instance.PerInstancePath("overlay.img");
301 unlink(overlay_path.c_str());
302 if (!CreateQcowOverlay(
303 config->crosvm_binary(), config->composite_disk_path(), overlay_path)) {
304 LOG(ERROR) << "CreateQcowOverlay failed";
305 return false;
306 }
307 return true;
308 }
309
ServerLoop(cuttlefish::SharedFD server,cuttlefish::ProcessMonitor * process_monitor)310 void ServerLoop(cuttlefish::SharedFD server,
311 cuttlefish::ProcessMonitor* process_monitor) {
312 while (true) {
313 // TODO: use select to handle simultaneous connections.
314 auto client = cuttlefish::SharedFD::Accept(*server);
315 cuttlefish::LauncherAction action;
316 while (client->IsOpen() && client->Read(&action, sizeof(action)) > 0) {
317 switch (action) {
318 case cuttlefish::LauncherAction::kStop:
319 if (process_monitor->StopMonitoredProcesses()) {
320 auto response = cuttlefish::LauncherResponse::kSuccess;
321 client->Write(&response, sizeof(response));
322 std::exit(0);
323 } else {
324 auto response = cuttlefish::LauncherResponse::kError;
325 client->Write(&response, sizeof(response));
326 }
327 break;
328 case cuttlefish::LauncherAction::kStatus: {
329 // TODO(schuffelen): Return more information on a side channel
330 auto response = cuttlefish::LauncherResponse::kSuccess;
331 client->Write(&response, sizeof(response));
332 break;
333 }
334 case cuttlefish::LauncherAction::kPowerwash: {
335 LOG(INFO) << "Received a Powerwash request from the monitor socket";
336 if (!process_monitor->StopMonitoredProcesses()) {
337 LOG(ERROR) << "Stopping processes failed.";
338 auto response = cuttlefish::LauncherResponse::kError;
339 client->Write(&response, sizeof(response));
340 break;
341 }
342 if (!PowerwashFiles()) {
343 LOG(ERROR) << "Powerwashing files failed.";
344 auto response = cuttlefish::LauncherResponse::kError;
345 client->Write(&response, sizeof(response));
346 break;
347 }
348 auto response = cuttlefish::LauncherResponse::kSuccess;
349 client->Write(&response, sizeof(response));
350
351 auto config = cuttlefish::CuttlefishConfig::Get();
352 auto config_path = config->AssemblyPath("cuttlefish_config.json");
353 auto followup_stdin =
354 cuttlefish::SharedFD::MemfdCreate("pseudo_stdin");
355 cuttlefish::WriteAll(followup_stdin, config_path + "\n");
356 followup_stdin->LSeek(0, SEEK_SET);
357 followup_stdin->UNMANAGED_Dup2(0);
358
359 auto argv_vec = gflags::GetArgvs();
360 char** argv = new char*[argv_vec.size() + 1];
361 for (size_t i = 0; i < argv_vec.size(); i++) {
362 argv[i] = argv_vec[i].data();
363 }
364 argv[argv_vec.size()] = nullptr;
365
366 execv("/proc/self/exe", argv);
367 // execve should not return, so something went wrong.
368 PLOG(ERROR) << "execv returned: ";
369 response = cuttlefish::LauncherResponse::kError;
370 client->Write(&response, sizeof(response));
371 break;
372 }
373 default:
374 LOG(ERROR) << "Unrecognized launcher action: "
375 << static_cast<char>(action);
376 auto response = cuttlefish::LauncherResponse::kError;
377 client->Write(&response, sizeof(response));
378 }
379 }
380 }
381 }
382
GetConfigFilePath(const cuttlefish::CuttlefishConfig & config)383 std::string GetConfigFilePath(const cuttlefish::CuttlefishConfig& config) {
384 auto instance = config.ForDefaultInstance();
385 return instance.PerInstancePath("cuttlefish_config.json");
386 }
387
388 } // namespace
389
main(int argc,char ** argv)390 int main(int argc, char** argv) {
391 setenv("ANDROID_LOG_TAGS", "*:v", /* overwrite */ 0);
392 ::android::base::InitLogging(argv, android::base::StderrLogger);
393 google::ParseCommandLineFlags(&argc, &argv, false);
394
395 if (isatty(0)) {
396 LOG(FATAL) << "stdin was a tty, expected to be passed the output of a previous stage. "
397 << "Did you mean to run launch_cvd?";
398 return cuttlefish::RunnerExitCodes::kInvalidHostConfiguration;
399 } else {
400 int error_num = errno;
401 if (error_num == EBADF) {
402 LOG(FATAL) << "stdin was not a valid file descriptor, expected to be passed the output "
403 << "of assemble_cvd. Did you mean to run launch_cvd?";
404 return cuttlefish::RunnerExitCodes::kInvalidHostConfiguration;
405 }
406 }
407
408 std::string input_files_str;
409 {
410 auto input_fd = cuttlefish::SharedFD::Dup(0);
411 auto bytes_read = cuttlefish::ReadAll(input_fd, &input_files_str);
412 if (bytes_read < 0) {
413 LOG(FATAL) << "Failed to read input files. Error was \"" << input_fd->StrError() << "\"";
414 }
415 }
416 std::vector<std::string> input_files = android::base::Split(input_files_str, "\n");
417 bool found_config = false;
418 for (const auto& file : input_files) {
419 if (file.find("cuttlefish_config.json") != std::string::npos) {
420 found_config = true;
421 setenv(cuttlefish::kCuttlefishConfigEnvVarName, file.c_str(), /* overwrite */ false);
422 }
423 }
424 if (!found_config) {
425 return RunnerExitCodes::kCuttlefishConfigurationInitError;
426 }
427
428 auto config = cuttlefish::CuttlefishConfig::Get();
429 auto instance = config->ForDefaultInstance();
430
431 auto log_path = instance.launcher_log_path();
432
433 {
434 std::ofstream launcher_log_ofstream(log_path.c_str());
435 auto assemble_log = cuttlefish::ReadFile(config->AssemblyPath("assemble_cvd.log"));
436 launcher_log_ofstream << assemble_log;
437 }
438 ::android::base::SetLogger(cuttlefish::LogToStderrAndFiles({log_path}));
439
440 // Change working directory to the instance directory as early as possible to
441 // ensure all host processes have the same working dir. This helps stop_cvd
442 // find the running processes when it can't establish a communication with the
443 // launcher.
444 auto chdir_ret = chdir(instance.instance_dir().c_str());
445 if (chdir_ret != 0) {
446 auto error = errno;
447 LOG(ERROR) << "Unable to change dir into instance directory ("
448 << instance.instance_dir() << "): " << strerror(error);
449 return RunnerExitCodes::kInstanceDirCreationError;
450 }
451
452 auto used_tap_devices = cuttlefish::TapInterfacesInUse();
453 if (used_tap_devices.count(instance.wifi_tap_name())) {
454 LOG(ERROR) << "Wifi TAP device already in use";
455 return RunnerExitCodes::kTapDeviceInUse;
456 } else if (used_tap_devices.count(instance.mobile_tap_name())) {
457 LOG(ERROR) << "Mobile TAP device already in use";
458 return RunnerExitCodes::kTapDeviceInUse;
459 }
460
461 auto vm_manager = VmManager::Get(config->vm_manager(), config);
462
463 // Check host configuration
464 std::vector<std::string> config_commands;
465 if (!vm_manager->ValidateHostConfiguration(&config_commands)) {
466 LOG(ERROR) << "Validation of user configuration failed";
467 std::cout << "Execute the following to correctly configure:" << std::endl;
468 for (auto& command : config_commands) {
469 std::cout << " " << command << std::endl;
470 }
471 std::cout << "You may need to logout for the changes to take effect"
472 << std::endl;
473 return RunnerExitCodes::kInvalidHostConfiguration;
474 }
475
476 if (!WriteCuttlefishEnvironment(*config)) {
477 LOG(ERROR) << "Unable to write cuttlefish environment file";
478 }
479
480 LOG(INFO) << "The following files contain useful debugging information:";
481 if (config->run_as_daemon()) {
482 LOG(INFO) << " Launcher log: " << instance.launcher_log_path();
483 }
484 LOG(INFO) << " Android's logcat output: " << instance.logcat_path();
485 LOG(INFO) << " Kernel log: " << instance.PerInstancePath("kernel.log");
486 LOG(INFO) << " Instance configuration: " << GetConfigFilePath(*config);
487 LOG(INFO) << " Instance environment: " << config->cuttlefish_env_path();
488 LOG(INFO) << "To access the console run: screen " << instance.console_path();
489
490 auto launcher_monitor_path = instance.launcher_monitor_socket_path();
491 auto launcher_monitor_socket = cuttlefish::SharedFD::SocketLocalServer(
492 launcher_monitor_path.c_str(), false, SOCK_STREAM, 0666);
493 if (!launcher_monitor_socket->IsOpen()) {
494 LOG(ERROR) << "Error when opening launcher server: "
495 << launcher_monitor_socket->StrError();
496 return cuttlefish::RunnerExitCodes::kMonitorCreationFailed;
497 }
498 cuttlefish::SharedFD foreground_launcher_pipe;
499 if (config->run_as_daemon()) {
500 foreground_launcher_pipe = DaemonizeLauncher(*config);
501 if (!foreground_launcher_pipe->IsOpen()) {
502 return RunnerExitCodes::kDaemonizationError;
503 }
504 } else {
505 // Make sure the launcher runs in its own process group even when running in
506 // foreground
507 if (getsid(0) != getpid()) {
508 int retval = setpgid(0, 0);
509 if (retval) {
510 LOG(ERROR) << "Failed to create new process group: " << strerror(errno);
511 std::exit(RunnerExitCodes::kProcessGroupError);
512 }
513 }
514 }
515
516 auto boot_state_machine =
517 std::make_shared<CvdBootStateMachine>(foreground_launcher_pipe);
518
519 // Monitor and restart host processes supporting the CVD
520 cuttlefish::ProcessMonitor process_monitor;
521
522 if (config->enable_metrics() == cuttlefish::CuttlefishConfig::kYes) {
523 LaunchMetrics(&process_monitor, *config);
524 }
525 LaunchModemSimulatorIfEnabled(*config, &process_monitor);
526
527 auto event_pipes =
528 LaunchKernelLogMonitor(*config, &process_monitor, 2);
529 cuttlefish::SharedFD boot_events_pipe = event_pipes[0];
530 cuttlefish::SharedFD adbd_events_pipe = event_pipes[1];
531 event_pipes.clear();
532
533 SetUpHandlingOfBootEvents(&process_monitor, boot_events_pipe,
534 boot_state_machine);
535
536 LaunchLogcatReceiver(*config, &process_monitor);
537 LaunchConfigServer(*config, &process_monitor);
538 LaunchTombstoneReceiverIfEnabled(*config, &process_monitor);
539 LaunchTpm(&process_monitor, *config);
540 LaunchSecureEnvironment(&process_monitor, *config);
541
542 // The streamer needs to launch before the VMM because it serves on several
543 // sockets (input devices, vsock frame server) when using crosvm.
544 StreamerLaunchResult streamer_config;
545 if (config->enable_vnc_server()) {
546 streamer_config = LaunchVNCServer(
547 *config, &process_monitor, GetOnSubprocessExitCallback(*config));
548 }
549 if (config->enable_webrtc()) {
550 streamer_config = LaunchWebRTC(&process_monitor, *config);
551 }
552
553 auto kernel_args = KernelCommandLineFromConfig(*config);
554
555 // Start the guest VM
556 vm_manager->WithFrontend(streamer_config.launched);
557 vm_manager->WithKernelCommandLine(android::base::Join(kernel_args, " "));
558 auto vmm_commands = vm_manager->StartCommands();
559 for (auto& vmm_cmd: vmm_commands) {
560 process_monitor.StartSubprocess(std::move(vmm_cmd),
561 GetOnSubprocessExitCallback(*config));
562 }
563
564 // Start other host processes
565 LaunchSocketVsockProxyIfEnabled(&process_monitor, *config);
566 LaunchAdbConnectorIfEnabled(&process_monitor, *config, adbd_events_pipe);
567
568 ServerLoop(launcher_monitor_socket, &process_monitor); // Should not return
569 LOG(ERROR) << "The server loop returned, it should never happen!!";
570 return cuttlefish::RunnerExitCodes::kServerError;
571 }
572