• 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 "host/commands/run_cvd/server_loop.h"
18 
19 #include <fruit/fruit.h>
20 #include <gflags/gflags.h>
21 #include <unistd.h>
22 #include <string>
23 
24 #include "common/libs/fs/shared_buf.h"
25 #include "common/libs/utils/files.h"
26 #include "common/libs/utils/subprocess.h"
27 #include "host/commands/run_cvd/runner_defs.h"
28 #include "host/libs/config/cuttlefish_config.h"
29 #include "host/libs/config/data_image.h"
30 #include "host/libs/config/feature.h"
31 
32 namespace cuttlefish {
33 
34 namespace {
35 
CreateQcowOverlay(const std::string & crosvm_path,const std::string & backing_file,const std::string & output_overlay_path)36 bool CreateQcowOverlay(const std::string& crosvm_path,
37                        const std::string& backing_file,
38                        const std::string& output_overlay_path) {
39   Command crosvm_qcow2_cmd(crosvm_path);
40   crosvm_qcow2_cmd.AddParameter("create_qcow2");
41   crosvm_qcow2_cmd.AddParameter("--backing_file=", backing_file);
42   crosvm_qcow2_cmd.AddParameter(output_overlay_path);
43   int success = crosvm_qcow2_cmd.Start().Wait();
44   if (success != 0) {
45     LOG(ERROR) << "Unable to run crosvm create_qcow2. Exited with status "
46                << success;
47     return false;
48   }
49   return true;
50 }
51 
52 class ServerLoopImpl : public ServerLoop, public SetupFeature {
53  public:
INJECT(ServerLoopImpl (const CuttlefishConfig & config,const CuttlefishConfig::InstanceSpecific & instance))54   INJECT(ServerLoopImpl(const CuttlefishConfig& config,
55                         const CuttlefishConfig::InstanceSpecific& instance))
56       : config_(config), instance_(instance) {}
57 
58   // ServerLoop
Run(ProcessMonitor & process_monitor)59   void Run(ProcessMonitor& process_monitor) override {
60     while (true) {
61       // TODO: use select to handle simultaneous connections.
62       auto client = SharedFD::Accept(*server_);
63       LauncherAction action;
64       while (client->IsOpen() && client->Read(&action, sizeof(action)) > 0) {
65         switch (action) {
66           case LauncherAction::kStop: {
67             auto stop = process_monitor.StopMonitoredProcesses();
68             if (stop.ok()) {
69               auto response = LauncherResponse::kSuccess;
70               client->Write(&response, sizeof(response));
71               std::exit(0);
72             } else {
73               LOG(ERROR) << "Failed to stop subprocesses:\n" << stop.error();
74               auto response = LauncherResponse::kError;
75               client->Write(&response, sizeof(response));
76             }
77             break;
78           }
79           case LauncherAction::kStatus: {
80             // TODO(schuffelen): Return more information on a side channel
81             auto response = LauncherResponse::kSuccess;
82             client->Write(&response, sizeof(response));
83             break;
84           }
85           case LauncherAction::kPowerwash: {
86             LOG(INFO) << "Received a Powerwash request from the monitor socket";
87             auto stop = process_monitor.StopMonitoredProcesses();
88             if (!stop.ok()) {
89               LOG(ERROR) << "Stopping processes failed:\n" << stop.error();
90               auto response = LauncherResponse::kError;
91               client->Write(&response, sizeof(response));
92               break;
93             }
94             if (!PowerwashFiles()) {
95               LOG(ERROR) << "Powerwashing files failed.";
96               auto response = LauncherResponse::kError;
97               client->Write(&response, sizeof(response));
98               break;
99             }
100             auto response = LauncherResponse::kSuccess;
101             client->Write(&response, sizeof(response));
102 
103             RestartRunCvd(client->UNMANAGED_Dup());
104             // RestartRunCvd should not return, so something went wrong.
105             response = LauncherResponse::kError;
106             client->Write(&response, sizeof(response));
107             LOG(FATAL) << "run_cvd in a bad state";
108             break;
109           }
110           case LauncherAction::kRestart: {
111             auto stop = process_monitor.StopMonitoredProcesses();
112             if (!stop.ok()) {
113               LOG(ERROR) << "Stopping processes failed:\n" << stop.error();
114               auto response = LauncherResponse::kError;
115               client->Write(&response, sizeof(response));
116               break;
117             }
118             DeleteFifos();
119 
120             auto response = LauncherResponse::kSuccess;
121             client->Write(&response, sizeof(response));
122             RestartRunCvd(client->UNMANAGED_Dup());
123             // RestartRunCvd should not return, so something went wrong.
124             response = LauncherResponse::kError;
125             client->Write(&response, sizeof(response));
126             LOG(FATAL) << "run_cvd in a bad state";
127             break;
128           }
129           default:
130             LOG(ERROR) << "Unrecognized launcher action: "
131                        << static_cast<char>(action);
132             auto response = LauncherResponse::kError;
133             client->Write(&response, sizeof(response));
134         }
135       }
136     }
137   }
138 
139   // SetupFeature
Name() const140   std::string Name() const override { return "ServerLoop"; }
141 
142  private:
Enabled() const143   bool Enabled() const override { return true; }
Dependencies() const144   std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
Setup()145   bool Setup() {
146     auto launcher_monitor_path = instance_.launcher_monitor_socket_path();
147     server_ = SharedFD::SocketLocalServer(launcher_monitor_path.c_str(), false,
148                                           SOCK_STREAM, 0666);
149     if (!server_->IsOpen()) {
150       LOG(ERROR) << "Error when opening launcher server: "
151                  << server_->StrError();
152       return false;
153     }
154     return true;
155   }
156 
DeleteFifos()157   void DeleteFifos() {
158     // TODO(schuffelen): Create these FIFOs in assemble_cvd instead of run_cvd.
159     std::vector<std::string> pipes = {
160         instance_.kernel_log_pipe_name(),
161         instance_.console_in_pipe_name(),
162         instance_.console_out_pipe_name(),
163         instance_.logcat_pipe_name(),
164         instance_.PerInstanceInternalPath("keymaster_fifo_vm.in"),
165         instance_.PerInstanceInternalPath("keymaster_fifo_vm.out"),
166         instance_.PerInstanceInternalPath("gatekeeper_fifo_vm.in"),
167         instance_.PerInstanceInternalPath("gatekeeper_fifo_vm.out"),
168         instance_.PerInstanceInternalPath("bt_fifo_vm.in"),
169         instance_.PerInstanceInternalPath("bt_fifo_vm.out"),
170         instance_.PerInstanceInternalPath("gnsshvc_fifo_vm.in"),
171         instance_.PerInstanceInternalPath("gnsshvc_fifo_vm.out"),
172         instance_.PerInstanceInternalPath("locationhvc_fifo_vm.in"),
173         instance_.PerInstanceInternalPath("locationhvc_fifo_vm.out"),
174     };
175     for (const auto& pipe : pipes) {
176       unlink(pipe.c_str());
177     }
178   }
179 
PowerwashFiles()180   bool PowerwashFiles() {
181     DeleteFifos();
182 
183     // TODO(schuffelen): Clean up duplication with assemble_cvd
184     unlink(instance_.PerInstancePath("NVChip").c_str());
185 
186     auto kregistry_path = instance_.access_kregistry_path();
187     unlink(kregistry_path.c_str());
188     CreateBlankImage(kregistry_path, 2 /* mb */, "none");
189 
190     auto hwcomposer_pmem_path = instance_.hwcomposer_pmem_path();
191     unlink(hwcomposer_pmem_path.c_str());
192     CreateBlankImage(hwcomposer_pmem_path, 2 /* mb */, "none");
193 
194     auto pstore_path = instance_.pstore_path();
195     unlink(pstore_path.c_str());
196     CreateBlankImage(pstore_path, 2 /* mb */, "none");
197 
198     auto sdcard_path = instance_.sdcard_path();
199     auto sdcard_size = FileSize(sdcard_path);
200     unlink(sdcard_path.c_str());
201     // round up
202     auto sdcard_mb_size = (sdcard_size + (1 << 20) - 1) / (1 << 20);
203     LOG(DEBUG) << "Size in mb is " << sdcard_mb_size;
204     CreateBlankImage(sdcard_path, sdcard_mb_size, "sdcard");
205     std::vector<std::string> overlay_files{"overlay.img"};
206     if (instance_.start_ap()) {
207       overlay_files.emplace_back("ap_overlay.img");
208     }
209     for (auto overlay_file : {"overlay.img", "ap_overlay.img"}) {
210       auto overlay_path = instance_.PerInstancePath(overlay_file);
211       unlink(overlay_path.c_str());
212       if (!CreateQcowOverlay(config_.crosvm_binary(),
213                              config_.os_composite_disk_path(), overlay_path)) {
214         LOG(ERROR) << "CreateQcowOverlay failed";
215         return false;
216       }
217     }
218     return true;
219   }
220 
RestartRunCvd(int notification_fd)221   void RestartRunCvd(int notification_fd) {
222     auto config_path = config_.AssemblyPath("cuttlefish_config.json");
223     auto followup_stdin = SharedFD::MemfdCreate("pseudo_stdin");
224     WriteAll(followup_stdin, config_path + "\n");
225     followup_stdin->LSeek(0, SEEK_SET);
226     followup_stdin->UNMANAGED_Dup2(0);
227 
228     auto argv_vec = gflags::GetArgvs();
229     char** argv = new char*[argv_vec.size() + 2];
230     for (size_t i = 0; i < argv_vec.size(); i++) {
231       argv[i] = argv_vec[i].data();
232     }
233     // Will take precedence over any earlier arguments.
234     std::string reboot_notification =
235         "-reboot_notification_fd=" + std::to_string(notification_fd);
236     argv[argv_vec.size()] = reboot_notification.data();
237     argv[argv_vec.size() + 1] = nullptr;
238 
239     execv("/proc/self/exe", argv);
240     // execve should not return, so something went wrong.
241     PLOG(ERROR) << "execv returned: ";
242   }
243 
244   const CuttlefishConfig& config_;
245   const CuttlefishConfig::InstanceSpecific& instance_;
246   SharedFD server_;
247 };
248 
249 }  // namespace
250 
251 ServerLoop::~ServerLoop() = default;
252 
253 fruit::Component<fruit::Required<const CuttlefishConfig,
254                                  const CuttlefishConfig::InstanceSpecific>,
255                  ServerLoop>
serverLoopComponent()256 serverLoopComponent() {
257   return fruit::createComponent()
258       .bind<ServerLoop, ServerLoopImpl>()
259       .addMultibinding<SetupFeature, ServerLoopImpl>();
260 }
261 
262 }  // namespace cuttlefish
263