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