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