Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #define LOG_TAG "RpcConnection" |
| 18 | |
| 19 | #include <binder/RpcConnection.h> |
| 20 | |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame^] | 21 | #include <arpa/inet.h> |
| 22 | #include <netdb.h> |
| 23 | #include <netinet/in.h> |
| 24 | #include <sys/socket.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/un.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <string_view> |
| 30 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 31 | #include <binder/Parcel.h> |
| 32 | #include <binder/Stability.h> |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 33 | #include <utils/String8.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 34 | |
| 35 | #include "RpcState.h" |
| 36 | #include "RpcWireFormat.h" |
| 37 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 38 | #ifdef __GLIBC__ |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 39 | extern "C" pid_t gettid(); |
| 40 | #endif |
| 41 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 42 | #ifdef __BIONIC__ |
| 43 | #include <linux/vm_sockets.h> |
| 44 | #endif |
| 45 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 46 | namespace android { |
| 47 | |
| 48 | using base::unique_fd; |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame^] | 49 | using AddrInfo = std::unique_ptr<addrinfo, decltype(&freeaddrinfo)>; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 50 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 51 | RpcConnection::SocketAddress::~SocketAddress() {} |
| 52 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 53 | RpcConnection::RpcConnection() { |
| 54 | LOG_RPC_DETAIL("RpcConnection created %p", this); |
| 55 | |
| 56 | mState = std::make_unique<RpcState>(); |
| 57 | } |
| 58 | RpcConnection::~RpcConnection() { |
| 59 | LOG_RPC_DETAIL("RpcConnection destroyed %p", this); |
| 60 | } |
| 61 | |
| 62 | sp<RpcConnection> RpcConnection::make() { |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 63 | return sp<RpcConnection>::make(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 66 | class UnixSocketAddress : public RpcConnection::SocketAddress { |
| 67 | public: |
| 68 | explicit UnixSocketAddress(const char* path) : mAddr({.sun_family = AF_UNIX}) { |
| 69 | unsigned int pathLen = strlen(path) + 1; |
Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 70 | LOG_ALWAYS_FATAL_IF(pathLen > sizeof(mAddr.sun_path), "Socket path is too long: %u %s", |
| 71 | pathLen, path); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 72 | memcpy(mAddr.sun_path, path, pathLen); |
| 73 | } |
| 74 | virtual ~UnixSocketAddress() {} |
| 75 | std::string toString() const override { |
| 76 | return String8::format("path '%.*s'", static_cast<int>(sizeof(mAddr.sun_path)), |
| 77 | mAddr.sun_path) |
| 78 | .c_str(); |
| 79 | } |
| 80 | const sockaddr* addr() const override { return reinterpret_cast<const sockaddr*>(&mAddr); } |
| 81 | size_t addrSize() const override { return sizeof(mAddr); } |
| 82 | |
| 83 | private: |
| 84 | sockaddr_un mAddr; |
| 85 | }; |
| 86 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 87 | bool RpcConnection::setupUnixDomainServer(const char* path) { |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 88 | return setupSocketServer(UnixSocketAddress(path)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool RpcConnection::addUnixDomainClient(const char* path) { |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 92 | return addSocketClient(UnixSocketAddress(path)); |
Steven Moreland | 5358354 | 2021-03-30 00:25:41 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 95 | #ifdef __BIONIC__ |
| 96 | |
| 97 | class VsockSocketAddress : public RpcConnection::SocketAddress { |
| 98 | public: |
| 99 | VsockSocketAddress(unsigned int cid, unsigned int port) |
| 100 | : mAddr({ |
| 101 | .svm_family = AF_VSOCK, |
| 102 | .svm_port = port, |
| 103 | .svm_cid = cid, |
| 104 | }) {} |
| 105 | virtual ~VsockSocketAddress() {} |
| 106 | std::string toString() const override { |
Yifan Hong | 4c79153 | 2021-04-14 12:38:46 -0700 | [diff] [blame] | 107 | return String8::format("cid %u port %u", mAddr.svm_cid, mAddr.svm_port).c_str(); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 108 | } |
| 109 | const sockaddr* addr() const override { return reinterpret_cast<const sockaddr*>(&mAddr); } |
| 110 | size_t addrSize() const override { return sizeof(mAddr); } |
| 111 | |
| 112 | private: |
| 113 | sockaddr_vm mAddr; |
| 114 | }; |
| 115 | |
| 116 | bool RpcConnection::setupVsockServer(unsigned int port) { |
| 117 | // realizing value w/ this type at compile time to avoid ubsan abort |
| 118 | constexpr unsigned int kAnyCid = VMADDR_CID_ANY; |
| 119 | |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 120 | return setupSocketServer(VsockSocketAddress(kAnyCid, port)); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool RpcConnection::addVsockClient(unsigned int cid, unsigned int port) { |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 124 | return addSocketClient(VsockSocketAddress(cid, port)); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | #endif // __BIONIC__ |
| 128 | |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame^] | 129 | class SocketAddressImpl : public RpcConnection::SocketAddress { |
| 130 | public: |
| 131 | SocketAddressImpl(const sockaddr* addr, size_t size, const String8& desc) |
| 132 | : mAddr(addr), mSize(size), mDesc(desc) {} |
| 133 | [[nodiscard]] std::string toString() const override { |
| 134 | return std::string(mDesc.c_str(), mDesc.size()); |
| 135 | } |
| 136 | [[nodiscard]] const sockaddr* addr() const override { return mAddr; } |
| 137 | [[nodiscard]] size_t addrSize() const override { return mSize; } |
| 138 | void set(const sockaddr* addr, size_t size) { |
| 139 | mAddr = addr; |
| 140 | mSize = size; |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | const sockaddr* mAddr = nullptr; |
| 145 | size_t mSize = 0; |
| 146 | String8 mDesc; |
| 147 | }; |
| 148 | |
| 149 | AddrInfo GetAddrInfo(const char* addr, unsigned int port) { |
| 150 | addrinfo hint{ |
| 151 | .ai_flags = 0, |
| 152 | .ai_family = AF_UNSPEC, |
| 153 | .ai_socktype = SOCK_STREAM, |
| 154 | .ai_protocol = 0, |
| 155 | }; |
| 156 | addrinfo* aiStart = nullptr; |
| 157 | if (int rc = getaddrinfo(addr, std::to_string(port).data(), &hint, &aiStart); 0 != rc) { |
| 158 | ALOGE("Unable to resolve %s:%u: %s", addr, port, gai_strerror(rc)); |
| 159 | return AddrInfo(nullptr, nullptr); |
| 160 | } |
| 161 | if (aiStart == nullptr) { |
| 162 | ALOGE("Unable to resolve %s:%u: getaddrinfo returns null", addr, port); |
| 163 | return AddrInfo(nullptr, nullptr); |
| 164 | } |
| 165 | return AddrInfo(aiStart, &freeaddrinfo); |
| 166 | } |
| 167 | |
| 168 | bool RpcConnection::setupInetServer(unsigned int port) { |
| 169 | auto aiStart = GetAddrInfo("127.0.0.1", port); |
| 170 | if (aiStart == nullptr) return false; |
| 171 | SocketAddressImpl socketAddress(nullptr, 0, String8::format("127.0.0.1:%u", port)); |
| 172 | for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { |
| 173 | socketAddress.set(ai->ai_addr, ai->ai_addrlen); |
| 174 | if (setupSocketServer(socketAddress)) return true; |
| 175 | } |
| 176 | ALOGE("None of the socket address resolved for 127.0.0.1:%u can be set up as inet server.", |
| 177 | port); |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | bool RpcConnection::addInetClient(const char* addr, unsigned int port) { |
| 182 | auto aiStart = GetAddrInfo(addr, port); |
| 183 | if (aiStart == nullptr) return false; |
| 184 | SocketAddressImpl socketAddress(nullptr, 0, String8::format("%s:%u", addr, port)); |
| 185 | for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { |
| 186 | socketAddress.set(ai->ai_addr, ai->ai_addrlen); |
| 187 | if (addSocketClient(socketAddress)) return true; |
| 188 | } |
| 189 | ALOGE("None of the socket address resolved for %s:%u can be added as inet client.", addr, port); |
| 190 | return false; |
| 191 | } |
| 192 | |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 193 | bool RpcConnection::addNullDebuggingClient() { |
| 194 | unique_fd serverFd(TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY | O_CLOEXEC))); |
| 195 | |
| 196 | if (serverFd == -1) { |
| 197 | ALOGE("Could not connect to /dev/null: %s", strerror(errno)); |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | addClient(std::move(serverFd)); |
| 202 | return true; |
| 203 | } |
| 204 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 205 | sp<IBinder> RpcConnection::getRootObject() { |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 206 | ExclusiveSocket socket(sp<RpcConnection>::fromExisting(this), SocketUse::CLIENT); |
| 207 | return state()->getRootObject(socket.fd(), sp<RpcConnection>::fromExisting(this)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | status_t RpcConnection::transact(const RpcAddress& address, uint32_t code, const Parcel& data, |
| 211 | Parcel* reply, uint32_t flags) { |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 212 | ExclusiveSocket socket(sp<RpcConnection>::fromExisting(this), |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 213 | (flags & IBinder::FLAG_ONEWAY) ? SocketUse::CLIENT_ASYNC |
| 214 | : SocketUse::CLIENT); |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 215 | return state()->transact(socket.fd(), address, code, data, |
| 216 | sp<RpcConnection>::fromExisting(this), reply, flags); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | status_t RpcConnection::sendDecStrong(const RpcAddress& address) { |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 220 | ExclusiveSocket socket(sp<RpcConnection>::fromExisting(this), SocketUse::CLIENT_REFCOUNT); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 221 | return state()->sendDecStrong(socket.fd(), address); |
| 222 | } |
| 223 | |
| 224 | void RpcConnection::join() { |
| 225 | // establish a connection |
| 226 | { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 227 | unique_fd clientFd( |
| 228 | TEMP_FAILURE_RETRY(accept4(mServer.get(), nullptr, 0 /*length*/, SOCK_CLOEXEC))); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 229 | if (clientFd < 0) { |
| 230 | // If this log becomes confusing, should save more state from setupUnixDomainServer |
| 231 | // in order to output here. |
| 232 | ALOGE("Could not accept4 socket: %s", strerror(errno)); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | LOG_RPC_DETAIL("accept4 on fd %d yields fd %d", mServer.get(), clientFd.get()); |
| 237 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 238 | assignServerToThisThread(std::move(clientFd)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // We may not use the connection we just established (two threads might |
| 242 | // establish connections for each other), but for now, just use one |
| 243 | // server/socket connection. |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 244 | ExclusiveSocket socket(sp<RpcConnection>::fromExisting(this), SocketUse::SERVER); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 245 | |
| 246 | while (true) { |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 247 | status_t error = |
| 248 | state()->getAndExecuteCommand(socket.fd(), sp<RpcConnection>::fromExisting(this)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 249 | |
| 250 | if (error != OK) { |
| 251 | ALOGI("Binder socket thread closing w/ status %s", statusToString(error).c_str()); |
| 252 | return; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void RpcConnection::setForServer(const wp<RpcServer>& server) { |
| 258 | mForServer = server; |
| 259 | } |
| 260 | |
| 261 | wp<RpcServer> RpcConnection::server() { |
| 262 | return mForServer; |
| 263 | } |
| 264 | |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 265 | bool RpcConnection::setupSocketServer(const SocketAddress& addr) { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 266 | LOG_ALWAYS_FATAL_IF(mServer.get() != -1, "Each RpcConnection can only have one server."); |
| 267 | |
| 268 | unique_fd serverFd( |
| 269 | TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); |
| 270 | if (serverFd == -1) { |
| 271 | ALOGE("Could not create socket: %s", strerror(errno)); |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | if (0 != TEMP_FAILURE_RETRY(bind(serverFd.get(), addr.addr(), addr.addrSize()))) { |
| 276 | int savedErrno = errno; |
| 277 | ALOGE("Could not bind socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | if (0 != TEMP_FAILURE_RETRY(listen(serverFd.get(), 1 /*backlog*/))) { |
| 282 | int savedErrno = errno; |
| 283 | ALOGE("Could not listen socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | mServer = std::move(serverFd); |
| 288 | return true; |
Steven Moreland | 5358354 | 2021-03-30 00:25:41 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 291 | bool RpcConnection::addSocketClient(const SocketAddress& addr) { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 292 | unique_fd serverFd( |
| 293 | TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); |
| 294 | if (serverFd == -1) { |
| 295 | int savedErrno = errno; |
| 296 | ALOGE("Could not create socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) { |
| 301 | int savedErrno = errno; |
| 302 | ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get()); |
| 307 | |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 308 | addClient(std::move(serverFd)); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 309 | return true; |
| 310 | } |
| 311 | |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 312 | void RpcConnection::addClient(unique_fd&& fd) { |
| 313 | std::lock_guard<std::mutex> _l(mSocketMutex); |
| 314 | sp<ConnectionSocket> connection = sp<ConnectionSocket>::make(); |
| 315 | connection->fd = std::move(fd); |
| 316 | mClients.push_back(connection); |
| 317 | } |
| 318 | |
| 319 | void RpcConnection::assignServerToThisThread(unique_fd&& fd) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 320 | std::lock_guard<std::mutex> _l(mSocketMutex); |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 321 | sp<ConnectionSocket> connection = sp<ConnectionSocket>::make(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 322 | connection->fd = std::move(fd); |
| 323 | mServers.push_back(connection); |
| 324 | } |
| 325 | |
| 326 | RpcConnection::ExclusiveSocket::ExclusiveSocket(const sp<RpcConnection>& connection, SocketUse use) |
| 327 | : mConnection(connection) { |
| 328 | pid_t tid = gettid(); |
| 329 | std::unique_lock<std::mutex> _l(mConnection->mSocketMutex); |
| 330 | |
| 331 | mConnection->mWaitingThreads++; |
| 332 | while (true) { |
| 333 | sp<ConnectionSocket> exclusive; |
| 334 | sp<ConnectionSocket> available; |
| 335 | |
| 336 | // CHECK FOR DEDICATED CLIENT SOCKET |
| 337 | // |
| 338 | // A server/looper should always use a dedicated connection. |
| 339 | if (use != SocketUse::SERVER) { |
| 340 | findSocket(tid, &exclusive, &available, mConnection->mClients, |
| 341 | mConnection->mClientsOffset); |
| 342 | |
| 343 | // WARNING: this assumes a server cannot request its client to send |
| 344 | // a transaction, as mServers is excluded below. |
| 345 | // |
| 346 | // Imagine we have more than one thread in play, and a single thread |
| 347 | // sends a synchronous, then an asynchronous command. Imagine the |
| 348 | // asynchronous command is sent on the first client socket. Then, if |
| 349 | // we naively send a synchronous command to that same socket, the |
| 350 | // thread on the far side might be busy processing the asynchronous |
| 351 | // command. So, we move to considering the second available thread |
| 352 | // for subsequent calls. |
| 353 | if (use == SocketUse::CLIENT_ASYNC && (exclusive != nullptr || available != nullptr)) { |
| 354 | mConnection->mClientsOffset = |
| 355 | (mConnection->mClientsOffset + 1) % mConnection->mClients.size(); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // USE SERVING SOCKET (to start serving or for nested transaction) |
| 360 | // |
| 361 | // asynchronous calls cannot be nested |
| 362 | if (use != SocketUse::CLIENT_ASYNC) { |
| 363 | // servers should start serving on an available thread only |
| 364 | // otherwise, this should only be a nested call |
| 365 | bool useAvailable = use == SocketUse::SERVER; |
| 366 | |
| 367 | findSocket(tid, &exclusive, (useAvailable ? &available : nullptr), |
| 368 | mConnection->mServers, 0 /* index hint */); |
| 369 | } |
| 370 | |
| 371 | // if our thread is already using a connection, prioritize using that |
| 372 | if (exclusive != nullptr) { |
| 373 | mSocket = exclusive; |
| 374 | mReentrant = true; |
| 375 | break; |
| 376 | } else if (available != nullptr) { |
| 377 | mSocket = available; |
| 378 | mSocket->exclusiveTid = tid; |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | LOG_ALWAYS_FATAL_IF(use == SocketUse::SERVER, "Must create connection to join one."); |
| 383 | |
| 384 | // in regular binder, this would usually be a deadlock :) |
| 385 | LOG_ALWAYS_FATAL_IF(mConnection->mClients.size() == 0, |
| 386 | "Not a client of any connection. You must create a connection to an " |
| 387 | "RPC server to make any non-nested (e.g. oneway or on another thread) " |
| 388 | "calls."); |
| 389 | |
| 390 | LOG_RPC_DETAIL("No available connection (have %zu clients and %zu servers). Waiting...", |
| 391 | mConnection->mClients.size(), mConnection->mServers.size()); |
| 392 | mConnection->mSocketCv.wait(_l); |
| 393 | } |
| 394 | mConnection->mWaitingThreads--; |
| 395 | } |
| 396 | |
| 397 | void RpcConnection::ExclusiveSocket::findSocket(pid_t tid, sp<ConnectionSocket>* exclusive, |
| 398 | sp<ConnectionSocket>* available, |
| 399 | std::vector<sp<ConnectionSocket>>& sockets, |
| 400 | size_t socketsIndexHint) { |
| 401 | LOG_ALWAYS_FATAL_IF(sockets.size() > 0 && socketsIndexHint >= sockets.size(), |
| 402 | "Bad index %zu >= %zu", socketsIndexHint, sockets.size()); |
| 403 | |
| 404 | if (*exclusive != nullptr) return; // consistent with break below |
| 405 | |
| 406 | for (size_t i = 0; i < sockets.size(); i++) { |
| 407 | sp<ConnectionSocket>& socket = sockets[(i + socketsIndexHint) % sockets.size()]; |
| 408 | |
| 409 | // take first available connection (intuition = caching) |
| 410 | if (available && *available == nullptr && socket->exclusiveTid == std::nullopt) { |
| 411 | *available = socket; |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | // though, prefer to take connection which is already inuse by this thread |
| 416 | // (nested transactions) |
| 417 | if (exclusive && socket->exclusiveTid == tid) { |
| 418 | *exclusive = socket; |
| 419 | break; // consistent with return above |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | RpcConnection::ExclusiveSocket::~ExclusiveSocket() { |
| 425 | // reentrant use of a connection means something less deep in the call stack |
| 426 | // is using this fd, and it retains the right to it. So, we don't give up |
| 427 | // exclusive ownership, and no thread is freed. |
| 428 | if (!mReentrant) { |
| 429 | std::unique_lock<std::mutex> _l(mConnection->mSocketMutex); |
| 430 | mSocket->exclusiveTid = std::nullopt; |
| 431 | if (mConnection->mWaitingThreads > 0) { |
| 432 | _l.unlock(); |
| 433 | mConnection->mSocketCv.notify_one(); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | } // namespace android |