Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | |
| 17 | #include "ServiceManagerHost.h" |
| 18 | |
| 19 | #include <android-base/parseint.h> |
| 20 | #include <android-base/strings.h> |
| 21 | #include <binder/IServiceManager.h> |
| 22 | #include <binder/RpcSession.h> |
| 23 | |
| 24 | #include "UtilsHost.h" |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | const void* kDeviceServiceExtraId = "DeviceServiceExtra"; |
| 31 | |
| 32 | // Parse stdout of program execution to string. If any error, return 0. |
| 33 | unsigned int parsePortNumber(const std::string& out, const std::string& what) { |
| 34 | auto trimmed = android::base::Trim(out); |
| 35 | unsigned int port = 0; |
| 36 | if (!android::base::ParseUint(trimmed, &port)) { |
| 37 | int savedErrno = errno; |
| 38 | ALOGE("%s is not a valid %s: %s", trimmed.c_str(), what.c_str(), strerror(savedErrno)); |
| 39 | return 0; |
| 40 | } |
| 41 | if (port == 0) { |
| 42 | ALOGE("0 is not a valid %s", what.c_str()); |
| 43 | return 0; // explicitly |
| 44 | } |
| 45 | return port; |
| 46 | } |
| 47 | |
| 48 | // RAII object for adb forwarding |
| 49 | class AdbForwarder { |
| 50 | public: |
| 51 | AdbForwarder() = default; |
| 52 | static std::optional<AdbForwarder> forward(unsigned int devicePort); |
| 53 | AdbForwarder(AdbForwarder&& other) noexcept { (*this) = std::move(other); } |
| 54 | AdbForwarder& operator=(AdbForwarder&&) noexcept; |
| 55 | ~AdbForwarder(); |
| 56 | [[nodiscard]] const std::optional<unsigned int>& hostPort() const { return mPort; } |
| 57 | |
| 58 | private: |
Tomasz Wasilczyk | df07f94 | 2023-11-02 15:07:45 -0700 | [diff] [blame] | 59 | AdbForwarder(const AdbForwarder&) = delete; |
| 60 | void operator=(const AdbForwarder&) = delete; |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 61 | explicit AdbForwarder(unsigned int port) : mPort(port) {} |
| 62 | std::optional<unsigned int> mPort; |
| 63 | }; |
| 64 | std::optional<AdbForwarder> AdbForwarder::forward(unsigned int devicePort) { |
| 65 | auto result = |
| 66 | execute({"adb", "forward", "tcp:0", "tcp:" + std::to_string(devicePort)}, nullptr); |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 67 | if (!result.has_value()) { |
| 68 | ALOGE("Unable to run `adb forward tcp:0 tcp:%d`", devicePort); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 69 | return std::nullopt; |
| 70 | } |
| 71 | // Must end with exit code 0 (`has_value() && value() == 0`) |
| 72 | if (result->exitCode.value_or(1) != 0) { |
| 73 | ALOGE("Unable to run `adb forward tcp:0 tcp:%d`, command exits with %s", devicePort, |
| 74 | result->toString().c_str()); |
| 75 | return std::nullopt; |
| 76 | } |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 77 | if (!result->stderrStr.empty()) { |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 78 | LOG_HOST("`adb forward tcp:0 tcp:%d` writes to stderr: %s", devicePort, |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 79 | result->stderrStr.c_str()); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 82 | unsigned int hostPort = parsePortNumber(result->stdoutStr, "host port"); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 83 | if (hostPort == 0) return std::nullopt; |
| 84 | |
| 85 | return AdbForwarder(hostPort); |
| 86 | } |
| 87 | |
| 88 | AdbForwarder& AdbForwarder::operator=(AdbForwarder&& other) noexcept { |
| 89 | std::swap(mPort, other.mPort); |
| 90 | return *this; |
| 91 | } |
| 92 | |
| 93 | AdbForwarder::~AdbForwarder() { |
| 94 | if (!mPort.has_value()) return; |
| 95 | |
| 96 | auto result = execute({"adb", "forward", "--remove", "tcp:" + std::to_string(*mPort)}, nullptr); |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 97 | if (!result.has_value()) { |
| 98 | ALOGE("Unable to run `adb forward --remove tcp:%d`", *mPort); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | // Must end with exit code 0 (`has_value() && value() == 0`) |
| 102 | if (result->exitCode.value_or(1) != 0) { |
| 103 | ALOGE("Unable to run `adb forward --remove tcp:%d`, command exits with %s", *mPort, |
| 104 | result->toString().c_str()); |
| 105 | return; |
| 106 | } |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 107 | if (!result->stderrStr.empty()) { |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 108 | LOG_HOST("`adb forward --remove tcp:%d` writes to stderr: %s", *mPort, |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 109 | result->stderrStr.c_str()); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | LOG_HOST("Successfully run `adb forward --remove tcp:%d`", *mPort); |
| 113 | } |
| 114 | |
| 115 | void cleanupCommandResult(const void* id, void* obj, void* /* cookie */) { |
| 116 | LOG_ALWAYS_FATAL_IF(id != kDeviceServiceExtraId, |
| 117 | "cleanupCommandResult invoked with mismatched ID %p, " |
| 118 | "expected %p", |
| 119 | id, kDeviceServiceExtraId); |
| 120 | auto ptr = static_cast<CommandResult*>(obj); |
| 121 | delete ptr; |
| 122 | } |
| 123 | |
| 124 | } // namespace |
| 125 | |
Yifan Hong | 5a05ef7 | 2021-10-08 17:33:47 -0700 | [diff] [blame] | 126 | sp<IBinder> getDeviceService(std::vector<std::string>&& serviceDispatcherArgs, |
| 127 | const RpcDelegateServiceManagerOptions& options) { |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 128 | std::vector<std::string> prefix{"adb", "shell", "servicedispatcher"}; |
| 129 | serviceDispatcherArgs.insert(serviceDispatcherArgs.begin(), prefix.begin(), prefix.end()); |
| 130 | |
| 131 | auto result = execute(std::move(serviceDispatcherArgs), &CommandResult::stdoutEndsWithNewLine); |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 132 | if (!result.has_value()) { |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 133 | return nullptr; |
| 134 | } |
| 135 | |
| 136 | // `servicedispatcher` process must be alive to keep the port open. |
| 137 | if (result->exitCode.has_value()) { |
| 138 | ALOGE("Command exits with: %s", result->toString().c_str()); |
| 139 | return nullptr; |
| 140 | } |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 141 | if (!result->stderrStr.empty()) { |
| 142 | LOG_HOST("servicedispatcher writes to stderr: %s", result->stderrStr.c_str()); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if (!result->stdoutEndsWithNewLine()) { |
| 146 | ALOGE("Unexpected command result: %s", result->toString().c_str()); |
| 147 | return nullptr; |
| 148 | } |
| 149 | |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 150 | unsigned int devicePort = parsePortNumber(result->stdoutStr, "device port"); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 151 | if (devicePort == 0) return nullptr; |
| 152 | |
| 153 | auto forwardResult = AdbForwarder::forward(devicePort); |
| 154 | if (!forwardResult.has_value()) { |
| 155 | return nullptr; |
| 156 | } |
| 157 | LOG_ALWAYS_FATAL_IF(!forwardResult->hostPort().has_value()); |
| 158 | |
| 159 | auto rpcSession = RpcSession::make(); |
Steven Moreland | feb13e8 | 2023-03-01 01:25:33 +0000 | [diff] [blame] | 160 | if (options.maxOutgoingConnections.has_value()) { |
| 161 | rpcSession->setMaxOutgoingConnections(*options.maxOutgoingConnections); |
Yifan Hong | 5a05ef7 | 2021-10-08 17:33:47 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 164 | if (status_t status = rpcSession->setupInetClient("127.0.0.1", *forwardResult->hostPort()); |
| 165 | status != OK) { |
| 166 | ALOGE("Unable to set up inet client on host port %u: %s", *forwardResult->hostPort(), |
| 167 | statusToString(status).c_str()); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 168 | return nullptr; |
| 169 | } |
| 170 | auto binder = rpcSession->getRootObject(); |
| 171 | if (binder == nullptr) { |
| 172 | ALOGE("RpcSession::getRootObject returns nullptr"); |
| 173 | return nullptr; |
| 174 | } |
Steven Moreland | 9234c43 | 2021-06-29 23:01:10 +0000 | [diff] [blame] | 175 | |
| 176 | LOG_ALWAYS_FATAL_IF( |
| 177 | nullptr != |
| 178 | binder->attachObject(kDeviceServiceExtraId, |
| 179 | static_cast<void*>(new CommandResult(std::move(*result))), nullptr, |
| 180 | &cleanupCommandResult)); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 181 | return binder; |
| 182 | } |
| 183 | |
| 184 | } // namespace android |