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 <string>
21 #include <utility>
22 #include <vector>
23
24 #include <android-base/logging.h>
25
26 #include "common/libs/fs/shared_fd.h"
27 #include "common/libs/utils/result.h"
28 #include "common/libs/utils/subprocess.h"
29 #include "host/libs/config/cuttlefish_config.h"
30 #include "host/libs/config/known_paths.h"
31
32 namespace cuttlefish {
33
StopModemSimulator(int id)34 static StopperResult StopModemSimulator(int id) {
35 std::string socket_name = "modem_simulator" + std::to_string(id);
36 auto monitor_sock =
37 SharedFD::SocketLocalClient(socket_name, true, SOCK_STREAM);
38 if (!monitor_sock->IsOpen()) {
39 LOG(ERROR) << "The connection to modem simulator is closed";
40 return StopperResult::kStopFailure;
41 }
42 std::string msg("STOP");
43 if (monitor_sock->Write(msg.data(), msg.size()) < 0) {
44 monitor_sock->Close();
45 LOG(ERROR) << "Failed to send 'STOP' to modem simulator";
46 return StopperResult::kStopFailure;
47 }
48 char buf[64] = {0};
49 if (monitor_sock->Read(buf, sizeof(buf)) <= 0) {
50 monitor_sock->Close();
51 LOG(ERROR) << "Failed to read message from modem simulator";
52 return StopperResult::kStopFailure;
53 }
54 if (strcmp(buf, "OK")) {
55 monitor_sock->Close();
56 LOG(ERROR) << "Read '" << buf << "' instead of 'OK' from modem simulator";
57 return StopperResult::kStopFailure;
58 }
59
60 return StopperResult::kStopSuccess;
61 }
62
ModemSimulator(const CuttlefishConfig::InstanceSpecific & instance)63 Result<std::optional<MonitorCommand>> ModemSimulator(
64 const CuttlefishConfig::InstanceSpecific& instance) {
65 if (!instance.enable_modem_simulator()) {
66 LOG(DEBUG) << "Modem simulator not enabled";
67 return {};
68 }
69 int instance_number = instance.modem_simulator_instance_number();
70 CF_EXPECT(instance_number >= 0 && instance_number < 4,
71 "Modem simulator instance number should range between 0 and 3");
72 auto ports = instance.modem_simulator_ports();
73 std::vector<SharedFD> sockets;
74 for (int i = 0; i < instance_number; ++i) {
75 auto pos = ports.find(',');
76 auto temp = (pos != std::string::npos) ? ports.substr(0, pos) : ports;
77 auto port = std::stoi(temp);
78 ports = ports.substr(pos + 1);
79
80 auto modem_sim_socket = SharedFD::VsockServer(
81 port, SOCK_STREAM,
82 instance.vhost_user_vsock()
83 ? std::make_optional(instance.vsock_guest_cid())
84 : std::nullopt);
85 CF_EXPECT(
86 modem_sim_socket->IsOpen(),
87 modem_sim_socket->StrError()
88 << " (try `cvd reset`, or `pkill run_cvd` and `pkill crosvm`)");
89 sockets.emplace_back(std::move(modem_sim_socket));
90 }
91
92 auto id = instance.modem_simulator_host_id();
93 auto nice_stop = [id]() { return StopModemSimulator(id); };
94 Command cmd(ModemSimulatorBinary(), KillSubprocessFallback(nice_stop));
95
96 auto sim_type = instance.modem_simulator_sim_type();
97 cmd.AddParameter(std::string{"-sim_type="} + std::to_string(sim_type));
98 cmd.AddParameter("-server_fds=");
99 bool first_socket = true;
100 for (const auto& socket : sockets) {
101 if (!first_socket) {
102 cmd.AppendToLastParameter(",");
103 }
104 cmd.AppendToLastParameter(socket);
105 first_socket = false;
106 }
107 return cmd;
108 }
109
110 } // namespace cuttlefish
111