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 <utility>
23
24 #include "common/libs/fs/shared_fd.h"
25 #include "common/libs/utils/subprocess.h"
26 #include "host/libs/config/cuttlefish_config.h"
27 #include "host/libs/config/known_paths.h"
28
29 namespace cuttlefish {
30
StopModemSimulator()31 static bool StopModemSimulator() {
32 auto config = CuttlefishConfig::Get();
33 auto instance = config->ForDefaultInstance();
34
35 std::string monitor_socket_name = "modem_simulator";
36 std::stringstream ss;
37 ss << instance.host_port();
38 monitor_socket_name.append(ss.str());
39 auto monitor_sock = SharedFD::SocketLocalClient(monitor_socket_name.c_str(),
40 true, SOCK_STREAM);
41 if (!monitor_sock->IsOpen()) {
42 LOG(ERROR) << "The connection to modem simulator is closed";
43 return false;
44 }
45 std::string msg("STOP");
46 if (monitor_sock->Write(msg.data(), msg.size()) < 0) {
47 monitor_sock->Close();
48 LOG(ERROR) << "Failed to send 'STOP' to modem simulator";
49 return false;
50 }
51 char buf[64] = {0};
52 if (monitor_sock->Read(buf, sizeof(buf)) <= 0) {
53 monitor_sock->Close();
54 LOG(ERROR) << "Failed to read message from modem simulator";
55 return false;
56 }
57 if (strcmp(buf, "OK")) {
58 monitor_sock->Close();
59 LOG(ERROR) << "Read '" << buf << "' instead of 'OK' from modem simulator";
60 return false;
61 }
62
63 return true;
64 }
65
LaunchModemSimulatorIfEnabled(const CuttlefishConfig & config)66 std::vector<Command> LaunchModemSimulatorIfEnabled(
67 const CuttlefishConfig& config) {
68 if (!config.enable_modem_simulator()) {
69 LOG(DEBUG) << "Modem simulator not enabled";
70 return {};
71 }
72
73 int instance_number = config.modem_simulator_instance_number();
74 if (instance_number > 3 /* max value */ || instance_number < 0) {
75 LOG(ERROR)
76 << "Modem simulator instance number should range between 1 and 3";
77 return {};
78 }
79
80 Command cmd(ModemSimulatorBinary(), [](Subprocess* proc) {
81 auto stopped = StopModemSimulator();
82 if (stopped) {
83 return true;
84 }
85 LOG(WARNING) << "Failed to stop modem simulator nicely, "
86 << "attempting to KILL";
87 return KillSubprocess(proc);
88 });
89
90 auto sim_type = config.modem_simulator_sim_type();
91 cmd.AddParameter(std::string{"-sim_type="} + std::to_string(sim_type));
92
93 auto instance = config.ForDefaultInstance();
94 auto ports = instance.modem_simulator_ports();
95 cmd.AddParameter("-server_fds=");
96 for (int i = 0; i < instance_number; ++i) {
97 auto pos = ports.find(',');
98 auto temp = (pos != std::string::npos) ? ports.substr(0, pos) : ports;
99 auto port = std::stoi(temp);
100 ports = ports.substr(pos + 1);
101
102 auto socket = SharedFD::VsockServer(port, SOCK_STREAM);
103 CHECK(socket->IsOpen())
104 << "Unable to create modem simulator server socket: "
105 << socket->StrError();
106 if (i > 0) {
107 cmd.AppendToLastParameter(",");
108 }
109 cmd.AppendToLastParameter(socket);
110 }
111
112 std::vector<Command> commands;
113 commands.emplace_back(std::move(cmd));
114 return commands;
115 }
116
117 } // namespace cuttlefish
118