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