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