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