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