| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +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 "RpcSession" | 
|  | 18 |  | 
|  | 19 | #include <binder/RpcSession.h> | 
|  | 20 |  | 
|  | 21 | #include <inttypes.h> | 
|  | 22 | #include <unistd.h> | 
|  | 23 |  | 
|  | 24 | #include <string_view> | 
|  | 25 |  | 
|  | 26 | #include <binder/Parcel.h> | 
| Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 27 | #include <binder/RpcServer.h> | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 28 | #include <binder/Stability.h> | 
|  | 29 | #include <utils/String8.h> | 
|  | 30 |  | 
|  | 31 | #include "RpcSocketAddress.h" | 
|  | 32 | #include "RpcState.h" | 
|  | 33 | #include "RpcWireFormat.h" | 
|  | 34 |  | 
|  | 35 | #ifdef __GLIBC__ | 
|  | 36 | extern "C" pid_t gettid(); | 
|  | 37 | #endif | 
|  | 38 |  | 
|  | 39 | namespace android { | 
|  | 40 |  | 
|  | 41 | using base::unique_fd; | 
|  | 42 |  | 
|  | 43 | RpcSession::RpcSession() { | 
|  | 44 | LOG_RPC_DETAIL("RpcSession created %p", this); | 
|  | 45 |  | 
|  | 46 | mState = std::make_unique<RpcState>(); | 
|  | 47 | } | 
|  | 48 | RpcSession::~RpcSession() { | 
|  | 49 | LOG_RPC_DETAIL("RpcSession destroyed %p", this); | 
|  | 50 |  | 
|  | 51 | std::lock_guard<std::mutex> _l(mMutex); | 
| Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 52 | LOG_ALWAYS_FATAL_IF(mServerConnections.size() != 0, | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 53 | "Should not be able to destroy a session with servers in use."); | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | sp<RpcSession> RpcSession::make() { | 
|  | 57 | return sp<RpcSession>::make(); | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | bool RpcSession::setupUnixDomainClient(const char* path) { | 
|  | 61 | return setupSocketClient(UnixSocketAddress(path)); | 
|  | 62 | } | 
|  | 63 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 64 | bool RpcSession::setupVsockClient(unsigned int cid, unsigned int port) { | 
|  | 65 | return setupSocketClient(VsockSocketAddress(cid, port)); | 
|  | 66 | } | 
|  | 67 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 68 | bool RpcSession::setupInetClient(const char* addr, unsigned int port) { | 
|  | 69 | auto aiStart = InetSocketAddress::getAddrInfo(addr, port); | 
|  | 70 | if (aiStart == nullptr) return false; | 
|  | 71 | for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { | 
|  | 72 | InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, addr, port); | 
|  | 73 | if (setupSocketClient(socketAddress)) return true; | 
|  | 74 | } | 
|  | 75 | ALOGE("None of the socket address resolved for %s:%u can be added as inet client.", addr, port); | 
|  | 76 | return false; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | bool RpcSession::addNullDebuggingClient() { | 
|  | 80 | unique_fd serverFd(TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY | O_CLOEXEC))); | 
|  | 81 |  | 
|  | 82 | if (serverFd == -1) { | 
|  | 83 | ALOGE("Could not connect to /dev/null: %s", strerror(errno)); | 
|  | 84 | return false; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | addClient(std::move(serverFd)); | 
|  | 88 | return true; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | sp<IBinder> RpcSession::getRootObject() { | 
|  | 92 | ExclusiveConnection connection(sp<RpcSession>::fromExisting(this), ConnectionUse::CLIENT); | 
|  | 93 | return state()->getRootObject(connection.fd(), sp<RpcSession>::fromExisting(this)); | 
|  | 94 | } | 
|  | 95 |  | 
| Steven Moreland | 1be9135 | 2021-05-11 22:12:15 +0000 | [diff] [blame] | 96 | status_t RpcSession::getRemoteMaxThreads(size_t* maxThreads) { | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 97 | ExclusiveConnection connection(sp<RpcSession>::fromExisting(this), ConnectionUse::CLIENT); | 
|  | 98 | return state()->getMaxThreads(connection.fd(), sp<RpcSession>::fromExisting(this), maxThreads); | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | status_t RpcSession::transact(const RpcAddress& address, uint32_t code, const Parcel& data, | 
|  | 102 | Parcel* reply, uint32_t flags) { | 
|  | 103 | ExclusiveConnection connection(sp<RpcSession>::fromExisting(this), | 
|  | 104 | (flags & IBinder::FLAG_ONEWAY) ? ConnectionUse::CLIENT_ASYNC | 
|  | 105 | : ConnectionUse::CLIENT); | 
|  | 106 | return state()->transact(connection.fd(), address, code, data, | 
|  | 107 | sp<RpcSession>::fromExisting(this), reply, flags); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | status_t RpcSession::sendDecStrong(const RpcAddress& address) { | 
|  | 111 | ExclusiveConnection connection(sp<RpcSession>::fromExisting(this), | 
|  | 112 | ConnectionUse::CLIENT_REFCOUNT); | 
|  | 113 | return state()->sendDecStrong(connection.fd(), address); | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | status_t RpcSession::readId() { | 
|  | 117 | { | 
|  | 118 | std::lock_guard<std::mutex> _l(mMutex); | 
|  | 119 | LOG_ALWAYS_FATAL_IF(mForServer != nullptr, "Can only update ID for client."); | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | int32_t id; | 
|  | 123 |  | 
|  | 124 | ExclusiveConnection connection(sp<RpcSession>::fromExisting(this), ConnectionUse::CLIENT); | 
|  | 125 | status_t status = | 
|  | 126 | state()->getSessionId(connection.fd(), sp<RpcSession>::fromExisting(this), &id); | 
|  | 127 | if (status != OK) return status; | 
|  | 128 |  | 
|  | 129 | LOG_RPC_DETAIL("RpcSession %p has id %d", this, id); | 
|  | 130 | mId = id; | 
|  | 131 | return OK; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | void RpcSession::startThread(unique_fd client) { | 
|  | 135 | std::lock_guard<std::mutex> _l(mMutex); | 
|  | 136 | sp<RpcSession> holdThis = sp<RpcSession>::fromExisting(this); | 
|  | 137 | int fd = client.release(); | 
|  | 138 | auto thread = std::thread([=] { | 
|  | 139 | holdThis->join(unique_fd(fd)); | 
|  | 140 | { | 
|  | 141 | std::lock_guard<std::mutex> _l(holdThis->mMutex); | 
| Steven Moreland | 2ff0d47 | 2021-05-05 22:20:40 +0000 | [diff] [blame] | 142 | auto it = mThreads.find(std::this_thread::get_id()); | 
|  | 143 | LOG_ALWAYS_FATAL_IF(it == mThreads.end()); | 
|  | 144 | it->second.detach(); | 
|  | 145 | mThreads.erase(it); | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 146 | } | 
|  | 147 | }); | 
|  | 148 | mThreads[thread.get_id()] = std::move(thread); | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | void RpcSession::join(unique_fd client) { | 
|  | 152 | // must be registered to allow arbitrary client code executing commands to | 
|  | 153 | // be able to do nested calls (we can't only read from it) | 
|  | 154 | sp<RpcConnection> connection = assignServerToThisThread(std::move(client)); | 
|  | 155 |  | 
|  | 156 | while (true) { | 
|  | 157 | status_t error = | 
|  | 158 | state()->getAndExecuteCommand(connection->fd, sp<RpcSession>::fromExisting(this)); | 
|  | 159 |  | 
|  | 160 | if (error != OK) { | 
|  | 161 | ALOGI("Binder connection thread closing w/ status %s", statusToString(error).c_str()); | 
|  | 162 | break; | 
|  | 163 | } | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | LOG_ALWAYS_FATAL_IF(!removeServerConnection(connection), | 
|  | 167 | "bad state: connection object guaranteed to be in list"); | 
|  | 168 | } | 
|  | 169 |  | 
| Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 170 | void RpcSession::terminateLocked() { | 
|  | 171 | // TODO(b/185167543): | 
|  | 172 | // - kindly notify other side of the connection of termination (can't be | 
|  | 173 | // locked) | 
|  | 174 | // - prevent new client/servers from being added | 
|  | 175 | // - stop all threads which are currently reading/writing | 
|  | 176 | // - terminate RpcState? | 
|  | 177 |  | 
|  | 178 | if (mTerminated) return; | 
|  | 179 |  | 
|  | 180 | sp<RpcServer> server = mForServer.promote(); | 
|  | 181 | if (server) { | 
|  | 182 | server->onSessionTerminating(sp<RpcSession>::fromExisting(this)); | 
|  | 183 | } | 
|  | 184 | } | 
|  | 185 |  | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 186 | wp<RpcServer> RpcSession::server() { | 
|  | 187 | return mForServer; | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | bool RpcSession::setupSocketClient(const RpcSocketAddress& addr) { | 
|  | 191 | { | 
|  | 192 | std::lock_guard<std::mutex> _l(mMutex); | 
| Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 193 | LOG_ALWAYS_FATAL_IF(mClientConnections.size() != 0, | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 194 | "Must only setup session once, but already has %zu clients", | 
| Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 195 | mClientConnections.size()); | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
|  | 198 | if (!setupOneSocketClient(addr, RPC_SESSION_ID_NEW)) return false; | 
|  | 199 |  | 
|  | 200 | // TODO(b/185167543): we should add additional sessions dynamically | 
|  | 201 | // instead of all at once. | 
|  | 202 | // TODO(b/186470974): first risk of blocking | 
|  | 203 | size_t numThreadsAvailable; | 
| Steven Moreland | 1be9135 | 2021-05-11 22:12:15 +0000 | [diff] [blame] | 204 | if (status_t status = getRemoteMaxThreads(&numThreadsAvailable); status != OK) { | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 205 | ALOGE("Could not get max threads after initial session to %s: %s", addr.toString().c_str(), | 
|  | 206 | statusToString(status).c_str()); | 
|  | 207 | return false; | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | if (status_t status = readId(); status != OK) { | 
|  | 211 | ALOGE("Could not get session id after initial session to %s; %s", addr.toString().c_str(), | 
|  | 212 | statusToString(status).c_str()); | 
|  | 213 | return false; | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | // we've already setup one client | 
|  | 217 | for (size_t i = 0; i + 1 < numThreadsAvailable; i++) { | 
| Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 218 | // TODO(b/185167543): shutdown existing connections? | 
|  | 219 | if (!setupOneSocketClient(addr, mId.value())) return false; | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 220 | } | 
|  | 221 |  | 
|  | 222 | return true; | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 | bool RpcSession::setupOneSocketClient(const RpcSocketAddress& addr, int32_t id) { | 
| Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 226 | for (size_t tries = 0; tries < 5; tries++) { | 
|  | 227 | if (tries > 0) usleep(10000); | 
|  | 228 |  | 
|  | 229 | unique_fd serverFd( | 
|  | 230 | TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); | 
|  | 231 | if (serverFd == -1) { | 
|  | 232 | int savedErrno = errno; | 
|  | 233 | ALOGE("Could not create socket at %s: %s", addr.toString().c_str(), | 
|  | 234 | strerror(savedErrno)); | 
|  | 235 | return false; | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 | if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) { | 
|  | 239 | if (errno == ECONNRESET) { | 
|  | 240 | ALOGW("Connection reset on %s", addr.toString().c_str()); | 
|  | 241 | continue; | 
|  | 242 | } | 
|  | 243 | int savedErrno = errno; | 
|  | 244 | ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(), | 
|  | 245 | strerror(savedErrno)); | 
|  | 246 | return false; | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | if (sizeof(id) != TEMP_FAILURE_RETRY(write(serverFd.get(), &id, sizeof(id)))) { | 
|  | 250 | int savedErrno = errno; | 
|  | 251 | ALOGE("Could not write id to socket at %s: %s", addr.toString().c_str(), | 
|  | 252 | strerror(savedErrno)); | 
|  | 253 | return false; | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get()); | 
|  | 257 |  | 
|  | 258 | addClient(std::move(serverFd)); | 
|  | 259 | return true; | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 260 | } | 
|  | 261 |  | 
| Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 262 | ALOGE("Ran out of retries to connect to %s", addr.toString().c_str()); | 
|  | 263 | return false; | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 264 | } | 
|  | 265 |  | 
|  | 266 | void RpcSession::addClient(unique_fd fd) { | 
|  | 267 | std::lock_guard<std::mutex> _l(mMutex); | 
|  | 268 | sp<RpcConnection> session = sp<RpcConnection>::make(); | 
|  | 269 | session->fd = std::move(fd); | 
| Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 270 | mClientConnections.push_back(session); | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 271 | } | 
|  | 272 |  | 
|  | 273 | void RpcSession::setForServer(const wp<RpcServer>& server, int32_t sessionId) { | 
|  | 274 | mId = sessionId; | 
|  | 275 | mForServer = server; | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | sp<RpcSession::RpcConnection> RpcSession::assignServerToThisThread(unique_fd fd) { | 
|  | 279 | std::lock_guard<std::mutex> _l(mMutex); | 
|  | 280 | sp<RpcConnection> session = sp<RpcConnection>::make(); | 
|  | 281 | session->fd = std::move(fd); | 
|  | 282 | session->exclusiveTid = gettid(); | 
| Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 283 | mServerConnections.push_back(session); | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 284 |  | 
|  | 285 | return session; | 
|  | 286 | } | 
|  | 287 |  | 
|  | 288 | bool RpcSession::removeServerConnection(const sp<RpcConnection>& connection) { | 
|  | 289 | std::lock_guard<std::mutex> _l(mMutex); | 
| Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 290 | if (auto it = std::find(mServerConnections.begin(), mServerConnections.end(), connection); | 
|  | 291 | it != mServerConnections.end()) { | 
|  | 292 | mServerConnections.erase(it); | 
|  | 293 | if (mServerConnections.size() == 0) { | 
| Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 294 | terminateLocked(); | 
|  | 295 | } | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 296 | return true; | 
|  | 297 | } | 
|  | 298 | return false; | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | RpcSession::ExclusiveConnection::ExclusiveConnection(const sp<RpcSession>& session, | 
|  | 302 | ConnectionUse use) | 
|  | 303 | : mSession(session) { | 
|  | 304 | pid_t tid = gettid(); | 
|  | 305 | std::unique_lock<std::mutex> _l(mSession->mMutex); | 
|  | 306 |  | 
|  | 307 | mSession->mWaitingThreads++; | 
|  | 308 | while (true) { | 
|  | 309 | sp<RpcConnection> exclusive; | 
|  | 310 | sp<RpcConnection> available; | 
|  | 311 |  | 
|  | 312 | // CHECK FOR DEDICATED CLIENT SOCKET | 
|  | 313 | // | 
|  | 314 | // A server/looper should always use a dedicated session if available | 
| Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 315 | findConnection(tid, &exclusive, &available, mSession->mClientConnections, | 
|  | 316 | mSession->mClientConnectionsOffset); | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 317 |  | 
|  | 318 | // WARNING: this assumes a server cannot request its client to send | 
| Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 319 | // a transaction, as mServerConnections is excluded below. | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 320 | // | 
|  | 321 | // Imagine we have more than one thread in play, and a single thread | 
|  | 322 | // sends a synchronous, then an asynchronous command. Imagine the | 
|  | 323 | // asynchronous command is sent on the first client connection. Then, if | 
|  | 324 | // we naively send a synchronous command to that same connection, the | 
|  | 325 | // thread on the far side might be busy processing the asynchronous | 
|  | 326 | // command. So, we move to considering the second available thread | 
|  | 327 | // for subsequent calls. | 
|  | 328 | if (use == ConnectionUse::CLIENT_ASYNC && (exclusive != nullptr || available != nullptr)) { | 
| Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 329 | mSession->mClientConnectionsOffset = | 
|  | 330 | (mSession->mClientConnectionsOffset + 1) % mSession->mClientConnections.size(); | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 331 | } | 
|  | 332 |  | 
|  | 333 | // USE SERVING SOCKET (for nested transaction) | 
|  | 334 | // | 
|  | 335 | // asynchronous calls cannot be nested | 
|  | 336 | if (use != ConnectionUse::CLIENT_ASYNC) { | 
|  | 337 | // server connections are always assigned to a thread | 
| Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 338 | findConnection(tid, &exclusive, nullptr /*available*/, mSession->mServerConnections, | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 339 | 0 /* index hint */); | 
|  | 340 | } | 
|  | 341 |  | 
|  | 342 | // if our thread is already using a session, prioritize using that | 
|  | 343 | if (exclusive != nullptr) { | 
|  | 344 | mConnection = exclusive; | 
|  | 345 | mReentrant = true; | 
|  | 346 | break; | 
|  | 347 | } else if (available != nullptr) { | 
|  | 348 | mConnection = available; | 
|  | 349 | mConnection->exclusiveTid = tid; | 
|  | 350 | break; | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | // in regular binder, this would usually be a deadlock :) | 
| Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 354 | LOG_ALWAYS_FATAL_IF(mSession->mClientConnections.size() == 0, | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 355 | "Not a client of any session. You must create a session to an " | 
|  | 356 | "RPC server to make any non-nested (e.g. oneway or on another thread) " | 
|  | 357 | "calls."); | 
|  | 358 |  | 
|  | 359 | LOG_RPC_DETAIL("No available session (have %zu clients and %zu servers). Waiting...", | 
| Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 360 | mSession->mClientConnections.size(), mSession->mServerConnections.size()); | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 361 | mSession->mAvailableConnectionCv.wait(_l); | 
|  | 362 | } | 
|  | 363 | mSession->mWaitingThreads--; | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | void RpcSession::ExclusiveConnection::findConnection(pid_t tid, sp<RpcConnection>* exclusive, | 
|  | 367 | sp<RpcConnection>* available, | 
|  | 368 | std::vector<sp<RpcConnection>>& sockets, | 
|  | 369 | size_t socketsIndexHint) { | 
|  | 370 | LOG_ALWAYS_FATAL_IF(sockets.size() > 0 && socketsIndexHint >= sockets.size(), | 
|  | 371 | "Bad index %zu >= %zu", socketsIndexHint, sockets.size()); | 
|  | 372 |  | 
|  | 373 | if (*exclusive != nullptr) return; // consistent with break below | 
|  | 374 |  | 
|  | 375 | for (size_t i = 0; i < sockets.size(); i++) { | 
|  | 376 | sp<RpcConnection>& socket = sockets[(i + socketsIndexHint) % sockets.size()]; | 
|  | 377 |  | 
|  | 378 | // take first available session (intuition = caching) | 
|  | 379 | if (available && *available == nullptr && socket->exclusiveTid == std::nullopt) { | 
|  | 380 | *available = socket; | 
|  | 381 | continue; | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | // though, prefer to take session which is already inuse by this thread | 
|  | 385 | // (nested transactions) | 
|  | 386 | if (exclusive && socket->exclusiveTid == tid) { | 
|  | 387 | *exclusive = socket; | 
|  | 388 | break; // consistent with return above | 
|  | 389 | } | 
|  | 390 | } | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | RpcSession::ExclusiveConnection::~ExclusiveConnection() { | 
|  | 394 | // reentrant use of a session means something less deep in the call stack | 
|  | 395 | // is using this fd, and it retains the right to it. So, we don't give up | 
|  | 396 | // exclusive ownership, and no thread is freed. | 
|  | 397 | if (!mReentrant) { | 
|  | 398 | std::unique_lock<std::mutex> _l(mSession->mMutex); | 
|  | 399 | mConnection->exclusiveTid = std::nullopt; | 
|  | 400 | if (mSession->mWaitingThreads > 0) { | 
|  | 401 | _l.unlock(); | 
|  | 402 | mSession->mAvailableConnectionCv.notify_one(); | 
|  | 403 | } | 
|  | 404 | } | 
|  | 405 | } | 
|  | 406 |  | 
|  | 407 | } // namespace android |