• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2019 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 #include "host/commands/run_cvd/launch.h"
17 
18 #include <android-base/logging.h>
19 #include <string.h>
20 #include <sstream>
21 #include <string>
22 #include <unordered_set>
23 #include <utility>
24 
25 #include "common/libs/fs/shared_fd.h"
26 #include "common/libs/utils/result.h"
27 #include "common/libs/utils/subprocess.h"
28 #include "host/libs/config/cuttlefish_config.h"
29 #include "host/libs/config/known_paths.h"
30 
31 namespace cuttlefish {
32 
StopModemSimulator(int id)33 static bool StopModemSimulator(int id) {
34   std::string socket_name = "modem_simulator" + std::to_string(id);
35   auto monitor_sock =
36       SharedFD::SocketLocalClient(socket_name, true, SOCK_STREAM);
37   if (!monitor_sock->IsOpen()) {
38     LOG(ERROR) << "The connection to modem simulator is closed";
39     return false;
40   }
41   std::string msg("STOP");
42   if (monitor_sock->Write(msg.data(), msg.size()) < 0) {
43     monitor_sock->Close();
44     LOG(ERROR) << "Failed to send 'STOP' to modem simulator";
45     return false;
46   }
47   char buf[64] = {0};
48   if (monitor_sock->Read(buf, sizeof(buf)) <= 0) {
49     monitor_sock->Close();
50     LOG(ERROR) << "Failed to read message from modem simulator";
51     return false;
52   }
53   if (strcmp(buf, "OK")) {
54     monitor_sock->Close();
55     LOG(ERROR) << "Read '" << buf << "' instead of 'OK' from modem simulator";
56     return false;
57   }
58 
59   return true;
60 }
61 
62 class ModemSimulator : public CommandSource {
63  public:
INJECT(ModemSimulator (const CuttlefishConfig & config,const CuttlefishConfig::InstanceSpecific & instance))64   INJECT(ModemSimulator(const CuttlefishConfig& config,
65                         const CuttlefishConfig::InstanceSpecific& instance))
66       : config_(config), instance_(instance) {}
67 
68   // CommandSource
Commands()69   std::vector<Command> Commands() override {
70     Command cmd(ModemSimulatorBinary(), [this](Subprocess* proc) {
71       auto stopped = StopModemSimulator(instance_.modem_simulator_host_id());
72       if (stopped) {
73         return StopperResult::kStopSuccess;
74       }
75       LOG(WARNING) << "Failed to stop modem simulator nicely, "
76                    << "attempting to KILL";
77       return KillSubprocess(proc) == StopperResult::kStopSuccess
78                  ? StopperResult::kStopCrash
79                  : StopperResult::kStopFailure;
80     });
81 
82     auto sim_type = config_.modem_simulator_sim_type();
83     cmd.AddParameter(std::string{"-sim_type="} + std::to_string(sim_type));
84     cmd.AddParameter("-server_fds=");
85     bool first_socket = true;
86     for (const auto& socket : sockets_) {
87       if (!first_socket) {
88         cmd.AppendToLastParameter(",");
89       }
90       cmd.AppendToLastParameter(socket);
91       first_socket = false;
92     }
93 
94     std::vector<Command> commands;
95     commands.emplace_back(std::move(cmd));
96     return commands;
97   }
98 
99   // SetupFeature
Name() const100   std::string Name() const override { return "ModemSimulator"; }
Enabled() const101   bool Enabled() const override {
102     if (!config_.enable_modem_simulator()) {
103       LOG(DEBUG) << "Modem simulator not enabled";
104     }
105     return config_.enable_modem_simulator();
106   }
107 
108  private:
Dependencies() const109   std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
ResultSetup()110   Result<void> ResultSetup() override {
111     int instance_number = config_.modem_simulator_instance_number();
112     CF_EXPECT(instance_number >= 0 && instance_number < 4,
113               "Modem simulator instance number should range between 0 and 3");
114     auto ports = instance_.modem_simulator_ports();
115     for (int i = 0; i < instance_number; ++i) {
116       auto pos = ports.find(',');
117       auto temp = (pos != std::string::npos) ? ports.substr(0, pos) : ports;
118       auto port = std::stoi(temp);
119       ports = ports.substr(pos + 1);
120 
121       auto modem_sim_socket = SharedFD::VsockServer(port, SOCK_STREAM);
122       CF_EXPECT(modem_sim_socket->IsOpen(), modem_sim_socket->StrError());
123       sockets_.emplace_back(std::move(modem_sim_socket));
124     }
125     return {};
126   }
127 
128   const CuttlefishConfig& config_;
129   const CuttlefishConfig::InstanceSpecific& instance_;
130   std::vector<SharedFD> sockets_;
131 };
132 
133 fruit::Component<fruit::Required<const CuttlefishConfig,
134                                  const CuttlefishConfig::InstanceSpecific>>
launchModemComponent()135 launchModemComponent() {
136   return fruit::createComponent()
137       .addMultibinding<CommandSource, ModemSimulator>()
138       .addMultibinding<SetupFeature, ModemSimulator>();
139 }
140 
141 }  // namespace cuttlefish
142