1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2021 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #include "ServiceManagerHost.h"
18*38e8c45fSAndroid Build Coastguard Worker
19*38e8c45fSAndroid Build Coastguard Worker #include <android-base/parseint.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <android-base/strings.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <binder/IServiceManager.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <binder/RpcSession.h>
23*38e8c45fSAndroid Build Coastguard Worker
24*38e8c45fSAndroid Build Coastguard Worker #include "UtilsHost.h"
25*38e8c45fSAndroid Build Coastguard Worker
26*38e8c45fSAndroid Build Coastguard Worker namespace android {
27*38e8c45fSAndroid Build Coastguard Worker
28*38e8c45fSAndroid Build Coastguard Worker namespace {
29*38e8c45fSAndroid Build Coastguard Worker
30*38e8c45fSAndroid Build Coastguard Worker const void* kDeviceServiceExtraId = "DeviceServiceExtra";
31*38e8c45fSAndroid Build Coastguard Worker
32*38e8c45fSAndroid Build Coastguard Worker // Parse stdout of program execution to string. If any error, return 0.
parsePortNumber(const std::string & out,const std::string & what)33*38e8c45fSAndroid Build Coastguard Worker unsigned int parsePortNumber(const std::string& out, const std::string& what) {
34*38e8c45fSAndroid Build Coastguard Worker auto trimmed = android::base::Trim(out);
35*38e8c45fSAndroid Build Coastguard Worker unsigned int port = 0;
36*38e8c45fSAndroid Build Coastguard Worker if (!android::base::ParseUint(trimmed, &port)) {
37*38e8c45fSAndroid Build Coastguard Worker int savedErrno = errno;
38*38e8c45fSAndroid Build Coastguard Worker ALOGE("%s is not a valid %s: %s", trimmed.c_str(), what.c_str(), strerror(savedErrno));
39*38e8c45fSAndroid Build Coastguard Worker return 0;
40*38e8c45fSAndroid Build Coastguard Worker }
41*38e8c45fSAndroid Build Coastguard Worker if (port == 0) {
42*38e8c45fSAndroid Build Coastguard Worker ALOGE("0 is not a valid %s", what.c_str());
43*38e8c45fSAndroid Build Coastguard Worker return 0; // explicitly
44*38e8c45fSAndroid Build Coastguard Worker }
45*38e8c45fSAndroid Build Coastguard Worker return port;
46*38e8c45fSAndroid Build Coastguard Worker }
47*38e8c45fSAndroid Build Coastguard Worker
48*38e8c45fSAndroid Build Coastguard Worker // RAII object for adb forwarding
49*38e8c45fSAndroid Build Coastguard Worker class AdbForwarder {
50*38e8c45fSAndroid Build Coastguard Worker public:
51*38e8c45fSAndroid Build Coastguard Worker AdbForwarder() = default;
52*38e8c45fSAndroid Build Coastguard Worker static std::optional<AdbForwarder> forward(unsigned int devicePort);
AdbForwarder(AdbForwarder && other)53*38e8c45fSAndroid Build Coastguard Worker AdbForwarder(AdbForwarder&& other) noexcept { (*this) = std::move(other); }
54*38e8c45fSAndroid Build Coastguard Worker AdbForwarder& operator=(AdbForwarder&&) noexcept;
55*38e8c45fSAndroid Build Coastguard Worker ~AdbForwarder();
hostPort() const56*38e8c45fSAndroid Build Coastguard Worker [[nodiscard]] const std::optional<unsigned int>& hostPort() const { return mPort; }
57*38e8c45fSAndroid Build Coastguard Worker
58*38e8c45fSAndroid Build Coastguard Worker private:
59*38e8c45fSAndroid Build Coastguard Worker AdbForwarder(const AdbForwarder&) = delete;
60*38e8c45fSAndroid Build Coastguard Worker void operator=(const AdbForwarder&) = delete;
AdbForwarder(unsigned int port)61*38e8c45fSAndroid Build Coastguard Worker explicit AdbForwarder(unsigned int port) : mPort(port) {}
62*38e8c45fSAndroid Build Coastguard Worker std::optional<unsigned int> mPort;
63*38e8c45fSAndroid Build Coastguard Worker };
forward(unsigned int devicePort)64*38e8c45fSAndroid Build Coastguard Worker std::optional<AdbForwarder> AdbForwarder::forward(unsigned int devicePort) {
65*38e8c45fSAndroid Build Coastguard Worker auto result =
66*38e8c45fSAndroid Build Coastguard Worker execute({"adb", "forward", "tcp:0", "tcp:" + std::to_string(devicePort)}, nullptr);
67*38e8c45fSAndroid Build Coastguard Worker if (!result.has_value()) {
68*38e8c45fSAndroid Build Coastguard Worker ALOGE("Unable to run `adb forward tcp:0 tcp:%d`", devicePort);
69*38e8c45fSAndroid Build Coastguard Worker return std::nullopt;
70*38e8c45fSAndroid Build Coastguard Worker }
71*38e8c45fSAndroid Build Coastguard Worker // Must end with exit code 0 (`has_value() && value() == 0`)
72*38e8c45fSAndroid Build Coastguard Worker if (result->exitCode.value_or(1) != 0) {
73*38e8c45fSAndroid Build Coastguard Worker ALOGE("Unable to run `adb forward tcp:0 tcp:%d`, command exits with %s", devicePort,
74*38e8c45fSAndroid Build Coastguard Worker result->toString().c_str());
75*38e8c45fSAndroid Build Coastguard Worker return std::nullopt;
76*38e8c45fSAndroid Build Coastguard Worker }
77*38e8c45fSAndroid Build Coastguard Worker if (!result->stderrStr.empty()) {
78*38e8c45fSAndroid Build Coastguard Worker LOG_HOST("`adb forward tcp:0 tcp:%d` writes to stderr: %s", devicePort,
79*38e8c45fSAndroid Build Coastguard Worker result->stderrStr.c_str());
80*38e8c45fSAndroid Build Coastguard Worker }
81*38e8c45fSAndroid Build Coastguard Worker
82*38e8c45fSAndroid Build Coastguard Worker unsigned int hostPort = parsePortNumber(result->stdoutStr, "host port");
83*38e8c45fSAndroid Build Coastguard Worker if (hostPort == 0) return std::nullopt;
84*38e8c45fSAndroid Build Coastguard Worker
85*38e8c45fSAndroid Build Coastguard Worker return AdbForwarder(hostPort);
86*38e8c45fSAndroid Build Coastguard Worker }
87*38e8c45fSAndroid Build Coastguard Worker
operator =(AdbForwarder && other)88*38e8c45fSAndroid Build Coastguard Worker AdbForwarder& AdbForwarder::operator=(AdbForwarder&& other) noexcept {
89*38e8c45fSAndroid Build Coastguard Worker std::swap(mPort, other.mPort);
90*38e8c45fSAndroid Build Coastguard Worker return *this;
91*38e8c45fSAndroid Build Coastguard Worker }
92*38e8c45fSAndroid Build Coastguard Worker
~AdbForwarder()93*38e8c45fSAndroid Build Coastguard Worker AdbForwarder::~AdbForwarder() {
94*38e8c45fSAndroid Build Coastguard Worker if (!mPort.has_value()) return;
95*38e8c45fSAndroid Build Coastguard Worker
96*38e8c45fSAndroid Build Coastguard Worker auto result = execute({"adb", "forward", "--remove", "tcp:" + std::to_string(*mPort)}, nullptr);
97*38e8c45fSAndroid Build Coastguard Worker if (!result.has_value()) {
98*38e8c45fSAndroid Build Coastguard Worker ALOGE("Unable to run `adb forward --remove tcp:%d`", *mPort);
99*38e8c45fSAndroid Build Coastguard Worker return;
100*38e8c45fSAndroid Build Coastguard Worker }
101*38e8c45fSAndroid Build Coastguard Worker // Must end with exit code 0 (`has_value() && value() == 0`)
102*38e8c45fSAndroid Build Coastguard Worker if (result->exitCode.value_or(1) != 0) {
103*38e8c45fSAndroid Build Coastguard Worker ALOGE("Unable to run `adb forward --remove tcp:%d`, command exits with %s", *mPort,
104*38e8c45fSAndroid Build Coastguard Worker result->toString().c_str());
105*38e8c45fSAndroid Build Coastguard Worker return;
106*38e8c45fSAndroid Build Coastguard Worker }
107*38e8c45fSAndroid Build Coastguard Worker if (!result->stderrStr.empty()) {
108*38e8c45fSAndroid Build Coastguard Worker LOG_HOST("`adb forward --remove tcp:%d` writes to stderr: %s", *mPort,
109*38e8c45fSAndroid Build Coastguard Worker result->stderrStr.c_str());
110*38e8c45fSAndroid Build Coastguard Worker }
111*38e8c45fSAndroid Build Coastguard Worker
112*38e8c45fSAndroid Build Coastguard Worker LOG_HOST("Successfully run `adb forward --remove tcp:%d`", *mPort);
113*38e8c45fSAndroid Build Coastguard Worker }
114*38e8c45fSAndroid Build Coastguard Worker
cleanupCommandResult(const void * id,void * obj,void *)115*38e8c45fSAndroid Build Coastguard Worker void cleanupCommandResult(const void* id, void* obj, void* /* cookie */) {
116*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(id != kDeviceServiceExtraId,
117*38e8c45fSAndroid Build Coastguard Worker "cleanupCommandResult invoked with mismatched ID %p, "
118*38e8c45fSAndroid Build Coastguard Worker "expected %p",
119*38e8c45fSAndroid Build Coastguard Worker id, kDeviceServiceExtraId);
120*38e8c45fSAndroid Build Coastguard Worker auto ptr = static_cast<CommandResult*>(obj);
121*38e8c45fSAndroid Build Coastguard Worker delete ptr;
122*38e8c45fSAndroid Build Coastguard Worker }
123*38e8c45fSAndroid Build Coastguard Worker
124*38e8c45fSAndroid Build Coastguard Worker } // namespace
125*38e8c45fSAndroid Build Coastguard Worker
getDeviceService(std::vector<std::string> && serviceDispatcherArgs,const RpcDelegateServiceManagerOptions & options)126*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> getDeviceService(std::vector<std::string>&& serviceDispatcherArgs,
127*38e8c45fSAndroid Build Coastguard Worker const RpcDelegateServiceManagerOptions& options) {
128*38e8c45fSAndroid Build Coastguard Worker std::vector<std::string> prefix{"adb", "shell", "servicedispatcher"};
129*38e8c45fSAndroid Build Coastguard Worker serviceDispatcherArgs.insert(serviceDispatcherArgs.begin(), prefix.begin(), prefix.end());
130*38e8c45fSAndroid Build Coastguard Worker
131*38e8c45fSAndroid Build Coastguard Worker auto result = execute(std::move(serviceDispatcherArgs), &CommandResult::stdoutEndsWithNewLine);
132*38e8c45fSAndroid Build Coastguard Worker if (!result.has_value()) {
133*38e8c45fSAndroid Build Coastguard Worker return nullptr;
134*38e8c45fSAndroid Build Coastguard Worker }
135*38e8c45fSAndroid Build Coastguard Worker
136*38e8c45fSAndroid Build Coastguard Worker // `servicedispatcher` process must be alive to keep the port open.
137*38e8c45fSAndroid Build Coastguard Worker if (result->exitCode.has_value()) {
138*38e8c45fSAndroid Build Coastguard Worker ALOGE("Command exits with: %s", result->toString().c_str());
139*38e8c45fSAndroid Build Coastguard Worker return nullptr;
140*38e8c45fSAndroid Build Coastguard Worker }
141*38e8c45fSAndroid Build Coastguard Worker if (!result->stderrStr.empty()) {
142*38e8c45fSAndroid Build Coastguard Worker LOG_HOST("servicedispatcher writes to stderr: %s", result->stderrStr.c_str());
143*38e8c45fSAndroid Build Coastguard Worker }
144*38e8c45fSAndroid Build Coastguard Worker
145*38e8c45fSAndroid Build Coastguard Worker if (!result->stdoutEndsWithNewLine()) {
146*38e8c45fSAndroid Build Coastguard Worker ALOGE("Unexpected command result: %s", result->toString().c_str());
147*38e8c45fSAndroid Build Coastguard Worker return nullptr;
148*38e8c45fSAndroid Build Coastguard Worker }
149*38e8c45fSAndroid Build Coastguard Worker
150*38e8c45fSAndroid Build Coastguard Worker unsigned int devicePort = parsePortNumber(result->stdoutStr, "device port");
151*38e8c45fSAndroid Build Coastguard Worker if (devicePort == 0) return nullptr;
152*38e8c45fSAndroid Build Coastguard Worker
153*38e8c45fSAndroid Build Coastguard Worker auto forwardResult = AdbForwarder::forward(devicePort);
154*38e8c45fSAndroid Build Coastguard Worker if (!forwardResult.has_value()) {
155*38e8c45fSAndroid Build Coastguard Worker return nullptr;
156*38e8c45fSAndroid Build Coastguard Worker }
157*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(!forwardResult->hostPort().has_value());
158*38e8c45fSAndroid Build Coastguard Worker
159*38e8c45fSAndroid Build Coastguard Worker auto rpcSession = RpcSession::make();
160*38e8c45fSAndroid Build Coastguard Worker if (options.maxOutgoingConnections.has_value()) {
161*38e8c45fSAndroid Build Coastguard Worker rpcSession->setMaxOutgoingConnections(*options.maxOutgoingConnections);
162*38e8c45fSAndroid Build Coastguard Worker }
163*38e8c45fSAndroid Build Coastguard Worker
164*38e8c45fSAndroid Build Coastguard Worker if (status_t status = rpcSession->setupInetClient("127.0.0.1", *forwardResult->hostPort());
165*38e8c45fSAndroid Build Coastguard Worker status != OK) {
166*38e8c45fSAndroid Build Coastguard Worker ALOGE("Unable to set up inet client on host port %u: %s", *forwardResult->hostPort(),
167*38e8c45fSAndroid Build Coastguard Worker statusToString(status).c_str());
168*38e8c45fSAndroid Build Coastguard Worker return nullptr;
169*38e8c45fSAndroid Build Coastguard Worker }
170*38e8c45fSAndroid Build Coastguard Worker auto binder = rpcSession->getRootObject();
171*38e8c45fSAndroid Build Coastguard Worker if (binder == nullptr) {
172*38e8c45fSAndroid Build Coastguard Worker ALOGE("RpcSession::getRootObject returns nullptr");
173*38e8c45fSAndroid Build Coastguard Worker return nullptr;
174*38e8c45fSAndroid Build Coastguard Worker }
175*38e8c45fSAndroid Build Coastguard Worker
176*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(
177*38e8c45fSAndroid Build Coastguard Worker nullptr !=
178*38e8c45fSAndroid Build Coastguard Worker binder->attachObject(kDeviceServiceExtraId,
179*38e8c45fSAndroid Build Coastguard Worker static_cast<void*>(new CommandResult(std::move(*result))), nullptr,
180*38e8c45fSAndroid Build Coastguard Worker &cleanupCommandResult));
181*38e8c45fSAndroid Build Coastguard Worker return binder;
182*38e8c45fSAndroid Build Coastguard Worker }
183*38e8c45fSAndroid Build Coastguard Worker
184*38e8c45fSAndroid Build Coastguard Worker } // namespace android
185