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 | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 54 | LOG_ALWAYS_FATAL_IF(mIncomingConnections.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 | |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 62 | void RpcSession::setMaxThreads(size_t threads) { |
| 63 | std::lock_guard<std::mutex> _l(mMutex); |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 64 | LOG_ALWAYS_FATAL_IF(!mOutgoingConnections.empty() || !mIncomingConnections.empty(), |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 65 | "Must set max threads before setting up connections, but has %zu client(s) " |
| 66 | "and %zu server(s)", |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 67 | mOutgoingConnections.size(), mIncomingConnections.size()); |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 68 | mMaxThreads = threads; |
| 69 | } |
| 70 | |
| 71 | size_t RpcSession::getMaxThreads() { |
| 72 | std::lock_guard<std::mutex> _l(mMutex); |
| 73 | return mMaxThreads; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 76 | bool RpcSession::setupUnixDomainClient(const char* path) { |
| 77 | return setupSocketClient(UnixSocketAddress(path)); |
| 78 | } |
| 79 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 80 | bool RpcSession::setupVsockClient(unsigned int cid, unsigned int port) { |
| 81 | return setupSocketClient(VsockSocketAddress(cid, port)); |
| 82 | } |
| 83 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 84 | bool RpcSession::setupInetClient(const char* addr, unsigned int port) { |
| 85 | auto aiStart = InetSocketAddress::getAddrInfo(addr, port); |
| 86 | if (aiStart == nullptr) return false; |
| 87 | for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { |
| 88 | InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, addr, port); |
| 89 | if (setupSocketClient(socketAddress)) return true; |
| 90 | } |
| 91 | ALOGE("None of the socket address resolved for %s:%u can be added as inet client.", addr, port); |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | bool RpcSession::addNullDebuggingClient() { |
| 96 | unique_fd serverFd(TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY | O_CLOEXEC))); |
| 97 | |
| 98 | if (serverFd == -1) { |
| 99 | ALOGE("Could not connect to /dev/null: %s", strerror(errno)); |
| 100 | return false; |
| 101 | } |
| 102 | |
Steven Moreland | b86e26b | 2021-06-12 00:35:58 +0000 | [diff] [blame] | 103 | return addOutgoingConnection(std::move(serverFd), false); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | sp<IBinder> RpcSession::getRootObject() { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 107 | ExclusiveConnection connection; |
| 108 | status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this), |
| 109 | ConnectionUse::CLIENT, &connection); |
| 110 | if (status != OK) return nullptr; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 111 | return state()->getRootObject(connection.get(), sp<RpcSession>::fromExisting(this)); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Steven Moreland | 1be9135 | 2021-05-11 22:12:15 +0000 | [diff] [blame] | 114 | status_t RpcSession::getRemoteMaxThreads(size_t* maxThreads) { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 115 | ExclusiveConnection connection; |
| 116 | status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this), |
| 117 | ConnectionUse::CLIENT, &connection); |
| 118 | if (status != OK) return status; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 119 | return state()->getMaxThreads(connection.get(), sp<RpcSession>::fromExisting(this), maxThreads); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 122 | bool RpcSession::shutdownAndWait(bool wait) { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 123 | std::unique_lock<std::mutex> _l(mMutex); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 124 | LOG_ALWAYS_FATAL_IF(mShutdownTrigger == nullptr, "Shutdown trigger not installed"); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 125 | |
| 126 | mShutdownTrigger->trigger(); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 127 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 128 | if (wait) { |
| 129 | LOG_ALWAYS_FATAL_IF(mShutdownListener == nullptr, "Shutdown listener not installed"); |
| 130 | mShutdownListener->waitForShutdown(_l); |
| 131 | LOG_ALWAYS_FATAL_IF(!mThreads.empty(), "Shutdown failed"); |
| 132 | } |
| 133 | |
| 134 | _l.unlock(); |
| 135 | mState->clear(); |
| 136 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 140 | 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] | 141 | Parcel* reply, uint32_t flags) { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 142 | ExclusiveConnection connection; |
| 143 | status_t status = |
| 144 | ExclusiveConnection::find(sp<RpcSession>::fromExisting(this), |
| 145 | (flags & IBinder::FLAG_ONEWAY) ? ConnectionUse::CLIENT_ASYNC |
| 146 | : ConnectionUse::CLIENT, |
| 147 | &connection); |
| 148 | if (status != OK) return status; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 149 | return state()->transact(connection.get(), binder, code, data, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 150 | sp<RpcSession>::fromExisting(this), reply, flags); |
| 151 | } |
| 152 | |
| 153 | status_t RpcSession::sendDecStrong(const RpcAddress& address) { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 154 | ExclusiveConnection connection; |
| 155 | status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this), |
| 156 | ConnectionUse::CLIENT_REFCOUNT, &connection); |
| 157 | if (status != OK) return status; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 158 | return state()->sendDecStrong(connection.get(), sp<RpcSession>::fromExisting(this), address); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Steven Moreland | e47511f | 2021-05-20 00:07:41 +0000 | [diff] [blame] | 161 | std::unique_ptr<RpcSession::FdTrigger> RpcSession::FdTrigger::make() { |
| 162 | auto ret = std::make_unique<RpcSession::FdTrigger>(); |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 163 | if (!android::base::Pipe(&ret->mRead, &ret->mWrite)) { |
| 164 | ALOGE("Could not create pipe %s", strerror(errno)); |
| 165 | return nullptr; |
| 166 | } |
Steven Moreland | e47511f | 2021-05-20 00:07:41 +0000 | [diff] [blame] | 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | void RpcSession::FdTrigger::trigger() { |
| 171 | mWrite.reset(); |
| 172 | } |
| 173 | |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 174 | bool RpcSession::FdTrigger::isTriggered() { |
| 175 | return mWrite == -1; |
| 176 | } |
| 177 | |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 178 | status_t RpcSession::FdTrigger::triggerablePollRead(base::borrowed_fd fd) { |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 179 | while (true) { |
Steven Moreland | dfe3be9 | 2021-05-22 00:24:29 +0000 | [diff] [blame] | 180 | pollfd pfd[]{{.fd = fd.get(), .events = POLLIN | POLLHUP, .revents = 0}, |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 181 | {.fd = mRead.get(), .events = POLLHUP, .revents = 0}}; |
| 182 | int ret = TEMP_FAILURE_RETRY(poll(pfd, arraysize(pfd), -1)); |
| 183 | if (ret < 0) { |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 184 | return -errno; |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 185 | } |
| 186 | if (ret == 0) { |
| 187 | continue; |
| 188 | } |
| 189 | if (pfd[1].revents & POLLHUP) { |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 190 | return -ECANCELED; |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 191 | } |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 192 | return pfd[0].revents & POLLIN ? OK : DEAD_OBJECT; |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 196 | status_t RpcSession::FdTrigger::interruptableReadFully(base::borrowed_fd fd, void* data, |
| 197 | size_t size) { |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 198 | uint8_t* buffer = reinterpret_cast<uint8_t*>(data); |
| 199 | uint8_t* end = buffer + size; |
| 200 | |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 201 | MAYBE_WAIT_IN_FLAKE_MODE; |
| 202 | |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 203 | status_t status; |
| 204 | while ((status = triggerablePollRead(fd)) == OK) { |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 205 | 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] | 206 | if (readSize == 0) return DEAD_OBJECT; // EOF |
Steven Moreland | dfe3be9 | 2021-05-22 00:24:29 +0000 | [diff] [blame] | 207 | |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 208 | if (readSize < 0) { |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 209 | return -errno; |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 210 | } |
| 211 | buffer += readSize; |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 212 | if (buffer == end) return OK; |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 213 | } |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 214 | return status; |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 217 | status_t RpcSession::readId() { |
| 218 | { |
| 219 | std::lock_guard<std::mutex> _l(mMutex); |
| 220 | LOG_ALWAYS_FATAL_IF(mForServer != nullptr, "Can only update ID for client."); |
| 221 | } |
| 222 | |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 223 | ExclusiveConnection connection; |
| 224 | status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this), |
| 225 | ConnectionUse::CLIENT, &connection); |
| 226 | if (status != OK) return status; |
| 227 | |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 228 | mId = RpcAddress::zero(); |
| 229 | status = state()->getSessionId(connection.get(), sp<RpcSession>::fromExisting(this), |
| 230 | &mId.value()); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 231 | if (status != OK) return status; |
| 232 | |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 233 | LOG_RPC_DETAIL("RpcSession %p has id %s", this, mId->toString().c_str()); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 234 | return OK; |
| 235 | } |
| 236 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 237 | void RpcSession::WaitForShutdownListener::onSessionLockedAllIncomingThreadsEnded( |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 238 | const sp<RpcSession>& session) { |
| 239 | (void)session; |
| 240 | mShutdown = true; |
| 241 | } |
| 242 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 243 | void RpcSession::WaitForShutdownListener::onSessionIncomingThreadEnded() { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 244 | mCv.notify_all(); |
| 245 | } |
| 246 | |
| 247 | void RpcSession::WaitForShutdownListener::waitForShutdown(std::unique_lock<std::mutex>& lock) { |
| 248 | while (!mShutdown) { |
| 249 | if (std::cv_status::timeout == mCv.wait_for(lock, std::chrono::seconds(1))) { |
| 250 | ALOGE("Waiting for RpcSession to shut down (1s w/o progress)."); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 255 | void RpcSession::preJoinThreadOwnership(std::thread thread) { |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 256 | 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] | 257 | |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 258 | { |
| 259 | std::lock_guard<std::mutex> _l(mMutex); |
| 260 | mThreads[thread.get_id()] = std::move(thread); |
| 261 | } |
Steven Moreland | 5802c2b | 2021-05-12 20:13:04 +0000 | [diff] [blame] | 262 | } |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 263 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 264 | RpcSession::PreJoinSetupResult RpcSession::preJoinSetup(base::unique_fd fd) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 265 | // must be registered to allow arbitrary client code executing commands to |
| 266 | // be able to do nested calls (we can't only read from it) |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 267 | sp<RpcConnection> connection = assignIncomingConnectionToThisThread(std::move(fd)); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 268 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 269 | status_t status = mState->readConnectionInit(connection, sp<RpcSession>::fromExisting(this)); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 270 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 271 | return PreJoinSetupResult{ |
| 272 | .connection = std::move(connection), |
| 273 | .status = status, |
| 274 | }; |
| 275 | } |
| 276 | |
| 277 | void RpcSession::join(sp<RpcSession>&& session, PreJoinSetupResult&& setupResult) { |
| 278 | sp<RpcConnection>& connection = setupResult.connection; |
| 279 | |
| 280 | if (setupResult.status == OK) { |
| 281 | while (true) { |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 282 | status_t status = session->state()->getAndExecuteCommand(connection, session, |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 283 | RpcState::CommandType::ANY); |
| 284 | if (status != OK) { |
| 285 | LOG_RPC_DETAIL("Binder connection thread closing w/ status %s", |
| 286 | statusToString(status).c_str()); |
| 287 | break; |
| 288 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 289 | } |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 290 | } else { |
| 291 | ALOGE("Connection failed to init, closing with status %s", |
| 292 | statusToString(setupResult.status).c_str()); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 295 | LOG_ALWAYS_FATAL_IF(!session->removeIncomingConnection(connection), |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 296 | "bad state: connection object guaranteed to be in list"); |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 297 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 298 | sp<RpcSession::EventListener> listener; |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 299 | { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 300 | std::lock_guard<std::mutex> _l(session->mMutex); |
| 301 | auto it = session->mThreads.find(std::this_thread::get_id()); |
| 302 | LOG_ALWAYS_FATAL_IF(it == session->mThreads.end()); |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 303 | it->second.detach(); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 304 | session->mThreads.erase(it); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 305 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 306 | listener = session->mEventListener.promote(); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 309 | session = nullptr; |
| 310 | |
| 311 | if (listener != nullptr) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 312 | listener->onSessionIncomingThreadEnded(); |
Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Steven Moreland | 7b8bc4c | 2021-06-10 22:50:27 +0000 | [diff] [blame] | 316 | sp<RpcServer> RpcSession::server() { |
| 317 | RpcServer* unsafeServer = mForServer.unsafe_get(); |
| 318 | sp<RpcServer> server = mForServer.promote(); |
| 319 | |
| 320 | LOG_ALWAYS_FATAL_IF((unsafeServer == nullptr) != (server == nullptr), |
| 321 | "wp<> is to avoid strong cycle only"); |
| 322 | return server; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | bool RpcSession::setupSocketClient(const RpcSocketAddress& addr) { |
| 326 | { |
| 327 | std::lock_guard<std::mutex> _l(mMutex); |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 328 | LOG_ALWAYS_FATAL_IF(mOutgoingConnections.size() != 0, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 329 | "Must only setup session once, but already has %zu clients", |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 330 | mOutgoingConnections.size()); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 333 | if (!setupOneSocketConnection(addr, RpcAddress::zero(), false /*reverse*/)) return false; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 334 | |
Steven Moreland | a5036f0 | 2021-06-08 02:26:57 +0000 | [diff] [blame] | 335 | // TODO(b/189955605): we should add additional sessions dynamically |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 336 | // instead of all at once. |
| 337 | // TODO(b/186470974): first risk of blocking |
| 338 | size_t numThreadsAvailable; |
Steven Moreland | 1be9135 | 2021-05-11 22:12:15 +0000 | [diff] [blame] | 339 | if (status_t status = getRemoteMaxThreads(&numThreadsAvailable); status != OK) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 340 | ALOGE("Could not get max threads after initial session to %s: %s", addr.toString().c_str(), |
| 341 | statusToString(status).c_str()); |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | if (status_t status = readId(); status != OK) { |
| 346 | ALOGE("Could not get session id after initial session to %s; %s", addr.toString().c_str(), |
| 347 | statusToString(status).c_str()); |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | // we've already setup one client |
| 352 | for (size_t i = 0; i + 1 < numThreadsAvailable; i++) { |
Steven Moreland | a5036f0 | 2021-06-08 02:26:57 +0000 | [diff] [blame] | 353 | // TODO(b/189955605): shutdown existing connections? |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 354 | if (!setupOneSocketConnection(addr, mId.value(), false /*reverse*/)) return false; |
| 355 | } |
| 356 | |
Steven Moreland | a5036f0 | 2021-06-08 02:26:57 +0000 | [diff] [blame] | 357 | // TODO(b/189955605): we should add additional sessions dynamically |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 358 | // instead of all at once - the other side should be responsible for setting |
| 359 | // up additional connections. We need to create at least one (unless 0 are |
| 360 | // requested to be set) in order to allow the other side to reliably make |
| 361 | // any requests at all. |
| 362 | |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 363 | for (size_t i = 0; i < mMaxThreads; i++) { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 364 | if (!setupOneSocketConnection(addr, mId.value(), true /*reverse*/)) return false; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | return true; |
| 368 | } |
| 369 | |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 370 | bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, const RpcAddress& id, |
| 371 | bool reverse) { |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 372 | for (size_t tries = 0; tries < 5; tries++) { |
| 373 | if (tries > 0) usleep(10000); |
| 374 | |
| 375 | unique_fd serverFd( |
| 376 | TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); |
| 377 | if (serverFd == -1) { |
| 378 | int savedErrno = errno; |
| 379 | ALOGE("Could not create socket at %s: %s", addr.toString().c_str(), |
| 380 | strerror(savedErrno)); |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) { |
| 385 | if (errno == ECONNRESET) { |
| 386 | ALOGW("Connection reset on %s", addr.toString().c_str()); |
| 387 | continue; |
| 388 | } |
| 389 | int savedErrno = errno; |
| 390 | ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(), |
| 391 | strerror(savedErrno)); |
| 392 | return false; |
| 393 | } |
| 394 | |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 395 | RpcConnectionHeader header{.options = 0}; |
| 396 | memcpy(&header.sessionId, &id.viewRawEmbedded(), sizeof(RpcWireAddress)); |
| 397 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 398 | if (reverse) header.options |= RPC_CONNECTION_OPTION_REVERSE; |
| 399 | |
| 400 | if (sizeof(header) != TEMP_FAILURE_RETRY(write(serverFd.get(), &header, sizeof(header)))) { |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 401 | int savedErrno = errno; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 402 | ALOGE("Could not write connection header to socket at %s: %s", addr.toString().c_str(), |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 403 | strerror(savedErrno)); |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get()); |
| 408 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 409 | if (reverse) { |
| 410 | std::mutex mutex; |
| 411 | std::condition_variable joinCv; |
| 412 | std::unique_lock<std::mutex> lock(mutex); |
| 413 | std::thread thread; |
| 414 | sp<RpcSession> thiz = sp<RpcSession>::fromExisting(this); |
| 415 | bool ownershipTransferred = false; |
| 416 | thread = std::thread([&]() { |
| 417 | std::unique_lock<std::mutex> threadLock(mutex); |
| 418 | unique_fd fd = std::move(serverFd); |
| 419 | // NOLINTNEXTLINE(performance-unnecessary-copy-initialization) |
| 420 | sp<RpcSession> session = thiz; |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 421 | session->preJoinThreadOwnership(std::move(thread)); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 422 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 423 | // only continue once we have a response or the connection fails |
| 424 | auto setupResult = session->preJoinSetup(std::move(fd)); |
| 425 | |
| 426 | ownershipTransferred = true; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 427 | threadLock.unlock(); |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 428 | joinCv.notify_one(); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 429 | // do not use & vars below |
| 430 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 431 | RpcSession::join(std::move(session), std::move(setupResult)); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 432 | }); |
| 433 | joinCv.wait(lock, [&] { return ownershipTransferred; }); |
| 434 | LOG_ALWAYS_FATAL_IF(!ownershipTransferred); |
| 435 | return true; |
| 436 | } else { |
Steven Moreland | b86e26b | 2021-06-12 00:35:58 +0000 | [diff] [blame] | 437 | return addOutgoingConnection(std::move(serverFd), true); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 438 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 441 | ALOGE("Ran out of retries to connect to %s", addr.toString().c_str()); |
| 442 | return false; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Steven Moreland | b86e26b | 2021-06-12 00:35:58 +0000 | [diff] [blame] | 445 | bool RpcSession::addOutgoingConnection(unique_fd fd, bool init) { |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 446 | sp<RpcConnection> connection = sp<RpcConnection>::make(); |
| 447 | { |
| 448 | std::lock_guard<std::mutex> _l(mMutex); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 449 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 450 | // first client connection added, but setForServer not called, so |
| 451 | // initializaing for a client. |
| 452 | if (mShutdownTrigger == nullptr) { |
| 453 | mShutdownTrigger = FdTrigger::make(); |
| 454 | mEventListener = mShutdownListener = sp<WaitForShutdownListener>::make(); |
| 455 | if (mShutdownTrigger == nullptr) return false; |
| 456 | } |
| 457 | |
| 458 | connection->fd = std::move(fd); |
| 459 | connection->exclusiveTid = gettid(); |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 460 | mOutgoingConnections.push_back(connection); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Steven Moreland | b86e26b | 2021-06-12 00:35:58 +0000 | [diff] [blame] | 463 | status_t status = OK; |
| 464 | if (init) { |
| 465 | mState->sendConnectionInit(connection, sp<RpcSession>::fromExisting(this)); |
| 466 | } |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 467 | |
| 468 | { |
| 469 | std::lock_guard<std::mutex> _l(mMutex); |
| 470 | connection->exclusiveTid = std::nullopt; |
| 471 | } |
| 472 | |
| 473 | return status == OK; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 476 | bool RpcSession::setForServer(const wp<RpcServer>& server, const wp<EventListener>& eventListener, |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 477 | const RpcAddress& sessionId) { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 478 | LOG_ALWAYS_FATAL_IF(mForServer != nullptr); |
| 479 | LOG_ALWAYS_FATAL_IF(server == nullptr); |
| 480 | LOG_ALWAYS_FATAL_IF(mEventListener != nullptr); |
| 481 | LOG_ALWAYS_FATAL_IF(eventListener == nullptr); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 482 | LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr); |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 483 | |
| 484 | mShutdownTrigger = FdTrigger::make(); |
| 485 | if (mShutdownTrigger == nullptr) return false; |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 486 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 487 | mId = sessionId; |
| 488 | mForServer = server; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 489 | mEventListener = eventListener; |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 490 | return true; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 493 | sp<RpcSession::RpcConnection> RpcSession::assignIncomingConnectionToThisThread(unique_fd fd) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 494 | std::lock_guard<std::mutex> _l(mMutex); |
| 495 | sp<RpcConnection> session = sp<RpcConnection>::make(); |
| 496 | session->fd = std::move(fd); |
| 497 | session->exclusiveTid = gettid(); |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 498 | mIncomingConnections.push_back(session); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 499 | |
| 500 | return session; |
| 501 | } |
| 502 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 503 | bool RpcSession::removeIncomingConnection(const sp<RpcConnection>& connection) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 504 | std::lock_guard<std::mutex> _l(mMutex); |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 505 | if (auto it = std::find(mIncomingConnections.begin(), mIncomingConnections.end(), connection); |
| 506 | it != mIncomingConnections.end()) { |
| 507 | mIncomingConnections.erase(it); |
| 508 | if (mIncomingConnections.size() == 0) { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 509 | sp<EventListener> listener = mEventListener.promote(); |
| 510 | if (listener) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 511 | listener->onSessionLockedAllIncomingThreadsEnded( |
| 512 | sp<RpcSession>::fromExisting(this)); |
Steven Moreland | a86e8fe | 2021-05-26 22:52:35 +0000 | [diff] [blame] | 513 | } |
Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 514 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 515 | return true; |
| 516 | } |
| 517 | return false; |
| 518 | } |
| 519 | |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 520 | status_t RpcSession::ExclusiveConnection::find(const sp<RpcSession>& session, ConnectionUse use, |
| 521 | ExclusiveConnection* connection) { |
| 522 | connection->mSession = session; |
| 523 | connection->mConnection = nullptr; |
| 524 | connection->mReentrant = false; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 525 | |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 526 | pid_t tid = gettid(); |
| 527 | std::unique_lock<std::mutex> _l(session->mMutex); |
| 528 | |
| 529 | session->mWaitingThreads++; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 530 | while (true) { |
| 531 | sp<RpcConnection> exclusive; |
| 532 | sp<RpcConnection> available; |
| 533 | |
| 534 | // CHECK FOR DEDICATED CLIENT SOCKET |
| 535 | // |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 536 | // A server/looper should always use a dedicated connection if available |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 537 | findConnection(tid, &exclusive, &available, session->mOutgoingConnections, |
| 538 | session->mOutgoingConnectionsOffset); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 539 | |
| 540 | // WARNING: this assumes a server cannot request its client to send |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 541 | // a transaction, as mIncomingConnections is excluded below. |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 542 | // |
| 543 | // Imagine we have more than one thread in play, and a single thread |
| 544 | // sends a synchronous, then an asynchronous command. Imagine the |
| 545 | // asynchronous command is sent on the first client connection. Then, if |
| 546 | // we naively send a synchronous command to that same connection, the |
| 547 | // thread on the far side might be busy processing the asynchronous |
| 548 | // command. So, we move to considering the second available thread |
| 549 | // for subsequent calls. |
| 550 | if (use == ConnectionUse::CLIENT_ASYNC && (exclusive != nullptr || available != nullptr)) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 551 | session->mOutgoingConnectionsOffset = (session->mOutgoingConnectionsOffset + 1) % |
| 552 | session->mOutgoingConnections.size(); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 555 | // USE SERVING SOCKET (e.g. nested transaction) |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 556 | if (use != ConnectionUse::CLIENT_ASYNC) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 557 | sp<RpcConnection> exclusiveIncoming; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 558 | // server connections are always assigned to a thread |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 559 | findConnection(tid, &exclusiveIncoming, nullptr /*available*/, |
| 560 | session->mIncomingConnections, 0 /* index hint */); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 561 | |
| 562 | // asynchronous calls cannot be nested, we currently allow ref count |
| 563 | // calls to be nested (so that you can use this without having extra |
| 564 | // threads). Note 'drainCommands' is used so that these ref counts can't |
| 565 | // build up. |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 566 | if (exclusiveIncoming != nullptr) { |
| 567 | if (exclusiveIncoming->allowNested) { |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 568 | // guaranteed to be processed as nested command |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 569 | exclusive = exclusiveIncoming; |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 570 | } else if (use == ConnectionUse::CLIENT_REFCOUNT && available == nullptr) { |
| 571 | // prefer available socket, but if we don't have one, don't |
| 572 | // wait for one |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 573 | exclusive = exclusiveIncoming; |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 578 | // if our thread is already using a connection, prioritize using that |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 579 | if (exclusive != nullptr) { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 580 | connection->mConnection = exclusive; |
| 581 | connection->mReentrant = true; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 582 | break; |
| 583 | } else if (available != nullptr) { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 584 | connection->mConnection = available; |
| 585 | connection->mConnection->exclusiveTid = tid; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 586 | break; |
| 587 | } |
| 588 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 589 | if (session->mOutgoingConnections.size() == 0) { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 590 | ALOGE("Session has no client connections. This is required for an RPC server to make " |
| 591 | "any non-nested (e.g. oneway or on another thread) calls. Use: %d. Server " |
| 592 | "connections: %zu", |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 593 | static_cast<int>(use), session->mIncomingConnections.size()); |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 594 | return WOULD_BLOCK; |
| 595 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 596 | |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 597 | LOG_RPC_DETAIL("No available connections (have %zu clients and %zu servers). Waiting...", |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 598 | session->mOutgoingConnections.size(), session->mIncomingConnections.size()); |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 599 | session->mAvailableConnectionCv.wait(_l); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 600 | } |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 601 | session->mWaitingThreads--; |
| 602 | |
| 603 | return OK; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | void RpcSession::ExclusiveConnection::findConnection(pid_t tid, sp<RpcConnection>* exclusive, |
| 607 | sp<RpcConnection>* available, |
| 608 | std::vector<sp<RpcConnection>>& sockets, |
| 609 | size_t socketsIndexHint) { |
| 610 | LOG_ALWAYS_FATAL_IF(sockets.size() > 0 && socketsIndexHint >= sockets.size(), |
| 611 | "Bad index %zu >= %zu", socketsIndexHint, sockets.size()); |
| 612 | |
| 613 | if (*exclusive != nullptr) return; // consistent with break below |
| 614 | |
| 615 | for (size_t i = 0; i < sockets.size(); i++) { |
| 616 | sp<RpcConnection>& socket = sockets[(i + socketsIndexHint) % sockets.size()]; |
| 617 | |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 618 | // take first available connection (intuition = caching) |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 619 | if (available && *available == nullptr && socket->exclusiveTid == std::nullopt) { |
| 620 | *available = socket; |
| 621 | continue; |
| 622 | } |
| 623 | |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 624 | // though, prefer to take connection which is already inuse by this thread |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 625 | // (nested transactions) |
| 626 | if (exclusive && socket->exclusiveTid == tid) { |
| 627 | *exclusive = socket; |
| 628 | break; // consistent with return above |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | RpcSession::ExclusiveConnection::~ExclusiveConnection() { |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 634 | // 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] | 635 | // is using this fd, and it retains the right to it. So, we don't give up |
| 636 | // exclusive ownership, and no thread is freed. |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 637 | if (!mReentrant && mConnection != nullptr) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 638 | std::unique_lock<std::mutex> _l(mSession->mMutex); |
| 639 | mConnection->exclusiveTid = std::nullopt; |
| 640 | if (mSession->mWaitingThreads > 0) { |
| 641 | _l.unlock(); |
| 642 | mSession->mAvailableConnectionCv.notify_one(); |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | } // namespace android |