• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unistd.h>
18 #include <fstream>
19 #include <iostream>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include <android-base/logging.h>
26 #include <android-base/strings.h>
27 #include <gflags/gflags.h>
28 
29 #include "common/libs/fs/shared_buf.h"
30 #include "common/libs/fs/shared_fd.h"
31 #include "common/libs/utils/environment.h"
32 #include "common/libs/utils/files.h"
33 #include "common/libs/utils/network.h"
34 #include "common/libs/utils/size_utils.h"
35 #include "common/libs/utils/subprocess.h"
36 #include "common/libs/utils/tee_logging.h"
37 #include "host/commands/run_cvd/boot_state_machine.h"
38 #include "host/commands/run_cvd/launch.h"
39 #include "host/commands/run_cvd/process_monitor.h"
40 #include "host/commands/run_cvd/runner_defs.h"
41 #include "host/commands/run_cvd/server_loop.h"
42 #include "host/libs/config/cuttlefish_config.h"
43 #include "host/libs/vm_manager/host_configuration.h"
44 #include "host/libs/vm_manager/vm_manager.h"
45 
46 DEFINE_int32(reboot_notification_fd, -1,
47              "A file descriptor to notify when boot completes.");
48 
49 namespace cuttlefish {
50 
51 using vm_manager::GetVmManager;
52 using vm_manager::ValidateHostConfiguration;
53 
54 namespace {
55 
56 constexpr char kGreenColor[] = "\033[1;32m";
57 constexpr char kResetColor[] = "\033[0m";
58 
WriteCuttlefishEnvironment(const CuttlefishConfig & config)59 bool WriteCuttlefishEnvironment(const CuttlefishConfig& config) {
60   auto env = SharedFD::Open(config.cuttlefish_env_path().c_str(),
61                             O_CREAT | O_RDWR, 0755);
62   if (!env->IsOpen()) {
63     LOG(ERROR) << "Unable to create cuttlefish.env file";
64     return false;
65   }
66   auto instance = config.ForDefaultInstance();
67   std::string config_env = "export CUTTLEFISH_PER_INSTANCE_PATH=\"" +
68                            instance.PerInstancePath(".") + "\"\n";
69   config_env += "export ANDROID_SERIAL=" + instance.adb_ip_and_port() + "\n";
70   env->Write(config_env.c_str(), config_env.size());
71   return true;
72 }
73 
74 // Forks and returns the write end of a pipe to the child process. The parent
75 // process waits for boot events to come through the pipe and exits accordingly.
DaemonizeLauncher(const CuttlefishConfig & config)76 SharedFD DaemonizeLauncher(const CuttlefishConfig& config) {
77   auto instance = config.ForDefaultInstance();
78   SharedFD read_end, write_end;
79   if (!SharedFD::Pipe(&read_end, &write_end)) {
80     LOG(ERROR) << "Unable to create pipe";
81     return {}; // a closed FD
82   }
83   auto pid = fork();
84   if (pid) {
85     // Explicitly close here, otherwise we may end up reading forever if the
86     // child process dies.
87     write_end->Close();
88     RunnerExitCodes exit_code;
89     auto bytes_read = read_end->Read(&exit_code, sizeof(exit_code));
90     if (bytes_read != sizeof(exit_code)) {
91       LOG(ERROR) << "Failed to read a complete exit code, read " << bytes_read
92                  << " bytes only instead of the expected " << sizeof(exit_code);
93       exit_code = RunnerExitCodes::kPipeIOError;
94     } else if (exit_code == RunnerExitCodes::kSuccess) {
95       LOG(INFO) << "Virtual device booted successfully";
96     } else if (exit_code == RunnerExitCodes::kVirtualDeviceBootFailed) {
97       LOG(ERROR) << "Virtual device failed to boot";
98     } else {
99       LOG(ERROR) << "Unexpected exit code: " << exit_code;
100     }
101     if (exit_code == RunnerExitCodes::kSuccess) {
102       LOG(INFO) << kBootCompletedMessage;
103     } else {
104       LOG(INFO) << kBootFailedMessage;
105     }
106     std::exit(exit_code);
107   } else {
108     // The child returns the write end of the pipe
109     if (daemon(/*nochdir*/ 1, /*noclose*/ 1) != 0) {
110       LOG(ERROR) << "Failed to daemonize child process: " << strerror(errno);
111       std::exit(RunnerExitCodes::kDaemonizationError);
112     }
113     // Redirect standard I/O
114     auto log_path = instance.launcher_log_path();
115     auto log = SharedFD::Open(log_path.c_str(), O_CREAT | O_WRONLY | O_APPEND,
116                               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
117     if (!log->IsOpen()) {
118       LOG(ERROR) << "Failed to create launcher log file: " << log->StrError();
119       std::exit(RunnerExitCodes::kDaemonizationError);
120     }
121     ::android::base::SetLogger(
122         TeeLogger({{LogFileSeverity(), log, MetadataLevel::FULL}}));
123     auto dev_null = SharedFD::Open("/dev/null", O_RDONLY);
124     if (!dev_null->IsOpen()) {
125       LOG(ERROR) << "Failed to open /dev/null: " << dev_null->StrError();
126       std::exit(RunnerExitCodes::kDaemonizationError);
127     }
128     if (dev_null->UNMANAGED_Dup2(0) < 0) {
129       LOG(ERROR) << "Failed dup2 stdin: " << dev_null->StrError();
130       std::exit(RunnerExitCodes::kDaemonizationError);
131     }
132     if (log->UNMANAGED_Dup2(1) < 0) {
133       LOG(ERROR) << "Failed dup2 stdout: " << log->StrError();
134       std::exit(RunnerExitCodes::kDaemonizationError);
135     }
136     if (log->UNMANAGED_Dup2(2) < 0) {
137       LOG(ERROR) << "Failed dup2 seterr: " << log->StrError();
138       std::exit(RunnerExitCodes::kDaemonizationError);
139     }
140 
141     read_end->Close();
142     return write_end;
143   }
144 }
145 
GetConfigFilePath(const CuttlefishConfig & config)146 std::string GetConfigFilePath(const CuttlefishConfig& config) {
147   auto instance = config.ForDefaultInstance();
148   return instance.PerInstancePath("cuttlefish_config.json");
149 }
150 
PrintStreamingInformation(const CuttlefishConfig & config)151 void PrintStreamingInformation(const CuttlefishConfig& config) {
152   if (config.ForDefaultInstance().start_webrtc_sig_server()) {
153     // TODO (jemoreira): Change this when webrtc is moved to the debian package.
154     LOG(INFO) << kGreenColor << "Point your browser to https://"
155               << config.sig_server_address() << ":" << config.sig_server_port()
156               << " to interact with the device." << kResetColor;
157   } else if (config.enable_vnc_server()) {
158     LOG(INFO) << kGreenColor << "VNC server started on port "
159               << config.ForDefaultInstance().vnc_server_port() << kResetColor;
160   }
161   // When WebRTC is enabled but an operator other than the one launched by
162   // run_cvd is used there is no way to know the url to which to point the
163   // browser to.
164 }
165 
166 }  // namespace
167 
RunCvdMain(int argc,char ** argv)168 int RunCvdMain(int argc, char** argv) {
169   setenv("ANDROID_LOG_TAGS", "*:v", /* overwrite */ 0);
170   ::android::base::InitLogging(argv, android::base::StderrLogger);
171   google::ParseCommandLineFlags(&argc, &argv, false);
172 
173   if (isatty(0)) {
174     LOG(FATAL) << "stdin was a tty, expected to be passed the output of a previous stage. "
175                << "Did you mean to run launch_cvd?";
176     return RunnerExitCodes::kInvalidHostConfiguration;
177   } else {
178     int error_num = errno;
179     if (error_num == EBADF) {
180       LOG(FATAL) << "stdin was not a valid file descriptor, expected to be passed the output "
181                  << "of assemble_cvd. Did you mean to run launch_cvd?";
182       return RunnerExitCodes::kInvalidHostConfiguration;
183     }
184   }
185 
186   std::string input_files_str;
187   {
188     auto input_fd = SharedFD::Dup(0);
189     auto bytes_read = ReadAll(input_fd, &input_files_str);
190     if (bytes_read < 0) {
191       LOG(FATAL) << "Failed to read input files. Error was \"" << input_fd->StrError() << "\"";
192     }
193   }
194   std::vector<std::string> input_files = android::base::Split(input_files_str, "\n");
195   bool found_config = false;
196   for (const auto& file : input_files) {
197     if (file.find("cuttlefish_config.json") != std::string::npos) {
198       found_config = true;
199       setenv(kCuttlefishConfigEnvVarName, file.c_str(), /* overwrite */ false);
200     }
201   }
202   if (!found_config) {
203     return RunnerExitCodes::kCuttlefishConfigurationInitError;
204   }
205 
206   auto config = CuttlefishConfig::Get();
207   auto instance = config->ForDefaultInstance();
208 
209   auto log_path = instance.launcher_log_path();
210 
211   {
212     std::ofstream launcher_log_ofstream(log_path.c_str());
213     auto assembly_path = config->AssemblyPath("assemble_cvd.log");
214     std::ifstream assembly_log_ifstream(assembly_path);
215     if (assembly_log_ifstream) {
216       auto assemble_log = ReadFile(assembly_path);
217       launcher_log_ofstream << assemble_log;
218     }
219   }
220   ::android::base::SetLogger(LogToStderrAndFiles({log_path}));
221 
222   // Change working directory to the instance directory as early as possible to
223   // ensure all host processes have the same working dir. This helps stop_cvd
224   // find the running processes when it can't establish a communication with the
225   // launcher.
226   auto chdir_ret = chdir(instance.instance_dir().c_str());
227   if (chdir_ret != 0) {
228     auto error = errno;
229     LOG(ERROR) << "Unable to change dir into instance directory ("
230                << instance.instance_dir() << "): " << strerror(error);
231     return RunnerExitCodes::kInstanceDirCreationError;
232   }
233 
234   auto used_tap_devices = TapInterfacesInUse();
235   if (used_tap_devices.count(instance.wifi_tap_name())) {
236     LOG(ERROR) << "Wifi TAP device already in use";
237     return RunnerExitCodes::kTapDeviceInUse;
238   } else if (used_tap_devices.count(instance.mobile_tap_name())) {
239     LOG(ERROR) << "Mobile TAP device already in use";
240     return RunnerExitCodes::kTapDeviceInUse;
241   } else if (config->ethernet() &&
242              used_tap_devices.count(instance.ethernet_tap_name())) {
243     LOG(ERROR) << "Ethernet TAP device already in use";
244   }
245 
246   auto vm_manager = GetVmManager(config->vm_manager(), config->target_arch());
247 
248 #ifndef __ANDROID__
249   // Check host configuration
250   std::vector<std::string> config_commands;
251   if (!ValidateHostConfiguration(&config_commands)) {
252     LOG(ERROR) << "Validation of user configuration failed";
253     std::cout << "Execute the following to correctly configure:" << std::endl;
254     for (auto& command : config_commands) {
255       std::cout << "  " << command << std::endl;
256     }
257     std::cout << "You may need to logout for the changes to take effect"
258               << std::endl;
259     return RunnerExitCodes::kInvalidHostConfiguration;
260   }
261 #endif
262 
263   if (!WriteCuttlefishEnvironment(*config)) {
264     LOG(ERROR) << "Unable to write cuttlefish environment file";
265   }
266 
267   PrintStreamingInformation(*config);
268 
269   if (config->console()) {
270     LOG(INFO) << kGreenColor << "To access the console run: screen "
271               << instance.console_path() << kResetColor;
272   } else {
273     LOG(INFO) << kGreenColor
274               << "Serial console is disabled; use -console=true to enable it"
275               << kResetColor;
276   }
277 
278   LOG(INFO) << kGreenColor
279             << "The following files contain useful debugging information:"
280             << kResetColor;
281   LOG(INFO) << kGreenColor
282             << "  Launcher log: " << instance.launcher_log_path()
283             << kResetColor;
284   LOG(INFO) << kGreenColor
285             << "  Android's logcat output: " << instance.logcat_path()
286             << kResetColor;
287   LOG(INFO) << kGreenColor
288             << "  Kernel log: " << instance.PerInstancePath("kernel.log")
289             << kResetColor;
290   LOG(INFO) << kGreenColor
291             << "  Instance configuration: " << GetConfigFilePath(*config)
292             << kResetColor;
293   LOG(INFO) << kGreenColor
294             << "  Instance environment: " << config->cuttlefish_env_path()
295             << kResetColor;
296 
297   auto launcher_monitor_path = instance.launcher_monitor_socket_path();
298   auto launcher_monitor_socket = SharedFD::SocketLocalServer(
299       launcher_monitor_path.c_str(), false, SOCK_STREAM, 0666);
300   if (!launcher_monitor_socket->IsOpen()) {
301     LOG(ERROR) << "Error when opening launcher server: "
302                << launcher_monitor_socket->StrError();
303     return RunnerExitCodes::kMonitorCreationFailed;
304   }
305   SharedFD foreground_launcher_pipe;
306   if (config->run_as_daemon()) {
307     foreground_launcher_pipe = DaemonizeLauncher(*config);
308     if (!foreground_launcher_pipe->IsOpen()) {
309       return RunnerExitCodes::kDaemonizationError;
310     }
311   } else {
312     // Make sure the launcher runs in its own process group even when running in
313     // foreground
314     if (getsid(0) != getpid()) {
315       int retval = setpgid(0, 0);
316       if (retval) {
317         LOG(ERROR) << "Failed to create new process group: " << strerror(errno);
318         std::exit(RunnerExitCodes::kProcessGroupError);
319       }
320     }
321   }
322 
323   SharedFD reboot_notification;
324   if (FLAGS_reboot_notification_fd >= 0) {
325     reboot_notification = SharedFD::Dup(FLAGS_reboot_notification_fd);
326     close(FLAGS_reboot_notification_fd);
327   }
328 
329   // Monitor and restart host processes supporting the CVD
330   ProcessMonitor process_monitor(config->restart_subprocesses());
331 
332   if (config->enable_metrics() == CuttlefishConfig::kYes) {
333     process_monitor.AddCommands(LaunchMetrics());
334   }
335   process_monitor.AddCommands(LaunchModemSimulatorIfEnabled(*config));
336 
337   auto kernel_log_monitor = LaunchKernelLogMonitor(*config, 3);
338   SharedFD boot_events_pipe = kernel_log_monitor.pipes[0];
339   SharedFD adbd_events_pipe = kernel_log_monitor.pipes[1];
340   SharedFD webrtc_events_pipe = kernel_log_monitor.pipes[2];
341   kernel_log_monitor.pipes.clear();
342   process_monitor.AddCommands(std::move(kernel_log_monitor.commands));
343 
344   CvdBootStateMachine boot_state_machine(foreground_launcher_pipe,
345                                          reboot_notification, boot_events_pipe);
346 
347   process_monitor.AddCommands(LaunchRootCanal(*config));
348   process_monitor.AddCommands(LaunchLogcatReceiver(*config));
349   process_monitor.AddCommands(LaunchConfigServer(*config));
350   process_monitor.AddCommands(LaunchTombstoneReceiver(*config));
351   process_monitor.AddCommands(LaunchGnssGrpcProxyServerIfEnabled(*config));
352   process_monitor.AddCommands(LaunchSecureEnvironment(*config));
353   if (config->enable_host_bluetooth()) {
354     process_monitor.AddCommands(LaunchBluetoothConnector(*config));
355   }
356   process_monitor.AddCommands(LaunchVehicleHalServerIfEnabled(*config));
357   process_monitor.AddCommands(LaunchConsoleForwarderIfEnabled(*config));
358 
359   // The streamer needs to launch before the VMM because it serves on several
360   // sockets (input devices, vsock frame server) when using crosvm.
361   if (config->enable_vnc_server()) {
362     process_monitor.AddCommands(LaunchVNCServer(*config));
363   }
364   if (config->enable_webrtc()) {
365     process_monitor.AddCommands(LaunchWebRTC(*config, webrtc_events_pipe));
366   }
367 
368   // Start the guest VM
369   process_monitor.AddCommands(vm_manager->StartCommands(*config));
370 
371   // Start other host processes
372   process_monitor.AddCommands(
373       LaunchSocketVsockProxyIfEnabled(*config, adbd_events_pipe));
374   process_monitor.AddCommands(LaunchAdbConnectorIfEnabled(*config));
375 
376   CHECK(process_monitor.StartAndMonitorProcesses())
377       << "Could not start subprocesses";
378 
379   ServerLoop(launcher_monitor_socket, &process_monitor); // Should not return
380   LOG(ERROR) << "The server loop returned, it should never happen!!";
381 
382   return RunnerExitCodes::kServerError;
383 }
384 
385 } // namespace cuttlefish
386 
main(int argc,char ** argv)387 int main(int argc, char** argv) {
388   return cuttlefish::RunCvdMain(argc, argv);
389 }
390