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 | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 29 | #include <android-base/hex.h> |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 30 | #include <android-base/macros.h> |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 31 | #include <android_runtime/vm.h> |
Steven Moreland | 4f622fe | 2021-09-13 17:38:09 -0700 | [diff] [blame^] | 32 | #include <binder/BpBinder.h> |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 33 | #include <binder/Parcel.h> |
Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 34 | #include <binder/RpcServer.h> |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 35 | #include <binder/RpcTransportRaw.h> |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 36 | #include <binder/Stability.h> |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 37 | #include <jni.h> |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 38 | #include <utils/String8.h> |
| 39 | |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 40 | #include "FdTrigger.h" |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 41 | #include "RpcSocketAddress.h" |
| 42 | #include "RpcState.h" |
| 43 | #include "RpcWireFormat.h" |
Yifan Hong | b675ffe | 2021-08-05 16:37:17 -0700 | [diff] [blame] | 44 | #include "Utils.h" |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 45 | |
| 46 | #ifdef __GLIBC__ |
| 47 | extern "C" pid_t gettid(); |
| 48 | #endif |
| 49 | |
| 50 | namespace android { |
| 51 | |
| 52 | using base::unique_fd; |
| 53 | |
Yifan Hong | ecf937d | 2021-08-11 17:29:28 -0700 | [diff] [blame] | 54 | RpcSession::RpcSession(std::unique_ptr<RpcTransportCtx> ctx) : mCtx(std::move(ctx)) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 55 | LOG_RPC_DETAIL("RpcSession created %p", this); |
| 56 | |
| 57 | mState = std::make_unique<RpcState>(); |
| 58 | } |
| 59 | RpcSession::~RpcSession() { |
| 60 | LOG_RPC_DETAIL("RpcSession destroyed %p", this); |
| 61 | |
| 62 | std::lock_guard<std::mutex> _l(mMutex); |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 63 | LOG_ALWAYS_FATAL_IF(mIncomingConnections.size() != 0, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 64 | "Should not be able to destroy a session with servers in use."); |
| 65 | } |
| 66 | |
Yifan Hong | ecf937d | 2021-08-11 17:29:28 -0700 | [diff] [blame] | 67 | sp<RpcSession> RpcSession::make() { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 68 | // Default is without TLS. |
Yifan Hong | ecf937d | 2021-08-11 17:29:28 -0700 | [diff] [blame] | 69 | return make(RpcTransportCtxFactoryRaw::make(), std::nullopt, std::nullopt); |
| 70 | } |
| 71 | |
| 72 | sp<RpcSession> RpcSession::make(std::unique_ptr<RpcTransportCtxFactory> rpcTransportCtxFactory, |
| 73 | std::optional<CertificateFormat> serverCertificateFormat, |
| 74 | std::optional<std::string> serverCertificate) { |
| 75 | auto ctx = rpcTransportCtxFactory->newClientCtx(); |
| 76 | if (ctx == nullptr) return nullptr; |
| 77 | LOG_ALWAYS_FATAL_IF(serverCertificateFormat.has_value() != serverCertificate.has_value()); |
| 78 | if (serverCertificateFormat.has_value() && serverCertificate.has_value()) { |
| 79 | status_t status = |
| 80 | ctx->addTrustedPeerCertificate(*serverCertificateFormat, *serverCertificate); |
| 81 | if (status != OK) { |
| 82 | ALOGE("Cannot add trusted server certificate: %s", statusToString(status).c_str()); |
| 83 | return nullptr; |
| 84 | } |
| 85 | } |
| 86 | return sp<RpcSession>::make(std::move(ctx)); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 89 | void RpcSession::setMaxThreads(size_t threads) { |
| 90 | std::lock_guard<std::mutex> _l(mMutex); |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 91 | LOG_ALWAYS_FATAL_IF(!mOutgoingConnections.empty() || !mIncomingConnections.empty(), |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 92 | "Must set max threads before setting up connections, but has %zu client(s) " |
| 93 | "and %zu server(s)", |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 94 | mOutgoingConnections.size(), mIncomingConnections.size()); |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 95 | mMaxThreads = threads; |
| 96 | } |
| 97 | |
| 98 | size_t RpcSession::getMaxThreads() { |
| 99 | std::lock_guard<std::mutex> _l(mMutex); |
| 100 | return mMaxThreads; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 103 | bool RpcSession::setProtocolVersion(uint32_t version) { |
| 104 | if (version >= RPC_WIRE_PROTOCOL_VERSION_NEXT && |
| 105 | version != RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL) { |
| 106 | ALOGE("Cannot start RPC session with version %u which is unknown (current protocol version " |
| 107 | "is %u).", |
| 108 | version, RPC_WIRE_PROTOCOL_VERSION); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | std::lock_guard<std::mutex> _l(mMutex); |
Steven Moreland | 40b736e | 2021-07-30 14:37:10 -0700 | [diff] [blame] | 113 | if (mProtocolVersion && version > *mProtocolVersion) { |
| 114 | ALOGE("Cannot upgrade explicitly capped protocol version %u to newer version %u", |
| 115 | *mProtocolVersion, version); |
| 116 | return false; |
| 117 | } |
| 118 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 119 | mProtocolVersion = version; |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | std::optional<uint32_t> RpcSession::getProtocolVersion() { |
| 124 | std::lock_guard<std::mutex> _l(mMutex); |
| 125 | return mProtocolVersion; |
| 126 | } |
| 127 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 128 | status_t RpcSession::setupUnixDomainClient(const char* path) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 129 | return setupSocketClient(UnixSocketAddress(path)); |
| 130 | } |
| 131 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 132 | status_t RpcSession::setupVsockClient(unsigned int cid, unsigned int port) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 133 | return setupSocketClient(VsockSocketAddress(cid, port)); |
| 134 | } |
| 135 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 136 | status_t RpcSession::setupInetClient(const char* addr, unsigned int port) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 137 | auto aiStart = InetSocketAddress::getAddrInfo(addr, port); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 138 | if (aiStart == nullptr) return UNKNOWN_ERROR; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 139 | for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { |
| 140 | InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, addr, port); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 141 | if (status_t status = setupSocketClient(socketAddress); status == OK) return OK; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 142 | } |
| 143 | ALOGE("None of the socket address resolved for %s:%u can be added as inet client.", addr, port); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 144 | return NAME_NOT_FOUND; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 147 | status_t RpcSession::setupPreconnectedClient(unique_fd fd, std::function<unique_fd()>&& request) { |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 148 | return setupClient([&](const std::vector<uint8_t>& sessionId, bool incoming) -> status_t { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 149 | // std::move'd from fd becomes -1 (!ok()) |
| 150 | if (!fd.ok()) { |
| 151 | fd = request(); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 152 | if (!fd.ok()) return BAD_VALUE; |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 153 | } |
Yifan Hong | b675ffe | 2021-08-05 16:37:17 -0700 | [diff] [blame] | 154 | if (auto res = setNonBlocking(fd); !res.ok()) { |
| 155 | ALOGE("setupPreconnectedClient: %s", res.error().message().c_str()); |
| 156 | return res.error().code() == 0 ? UNKNOWN_ERROR : -res.error().code(); |
| 157 | } |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 158 | return initAndAddConnection(std::move(fd), sessionId, incoming); |
| 159 | }); |
| 160 | } |
| 161 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 162 | status_t RpcSession::addNullDebuggingClient() { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 163 | // Note: only works on raw sockets. |
Yifan Hong | 832521e | 2021-08-05 14:55:40 -0700 | [diff] [blame] | 164 | if (auto status = initShutdownTrigger(); status != OK) return status; |
| 165 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 166 | unique_fd serverFd(TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY | O_CLOEXEC))); |
| 167 | |
| 168 | if (serverFd == -1) { |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 169 | int savedErrno = errno; |
| 170 | ALOGE("Could not connect to /dev/null: %s", strerror(savedErrno)); |
| 171 | return -savedErrno; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Yifan Hong | ecf937d | 2021-08-11 17:29:28 -0700 | [diff] [blame] | 174 | auto server = mCtx->newTransport(std::move(serverFd), mShutdownTrigger.get()); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 175 | if (server == nullptr) { |
| 176 | ALOGE("Unable to set up RpcTransport"); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 177 | return UNKNOWN_ERROR; |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 178 | } |
| 179 | return addOutgoingConnection(std::move(server), false); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | sp<IBinder> RpcSession::getRootObject() { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 183 | ExclusiveConnection connection; |
| 184 | status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this), |
| 185 | ConnectionUse::CLIENT, &connection); |
| 186 | if (status != OK) return nullptr; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 187 | return state()->getRootObject(connection.get(), sp<RpcSession>::fromExisting(this)); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Steven Moreland | 1be9135 | 2021-05-11 22:12:15 +0000 | [diff] [blame] | 190 | status_t RpcSession::getRemoteMaxThreads(size_t* maxThreads) { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 191 | ExclusiveConnection connection; |
| 192 | status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this), |
| 193 | ConnectionUse::CLIENT, &connection); |
| 194 | if (status != OK) return status; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 195 | return state()->getMaxThreads(connection.get(), sp<RpcSession>::fromExisting(this), maxThreads); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 198 | bool RpcSession::shutdownAndWait(bool wait) { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 199 | std::unique_lock<std::mutex> _l(mMutex); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 200 | LOG_ALWAYS_FATAL_IF(mShutdownTrigger == nullptr, "Shutdown trigger not installed"); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 201 | |
| 202 | mShutdownTrigger->trigger(); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 203 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 204 | if (wait) { |
| 205 | LOG_ALWAYS_FATAL_IF(mShutdownListener == nullptr, "Shutdown listener not installed"); |
Steven Moreland | 791e466 | 2021-09-13 15:22:58 -0700 | [diff] [blame] | 206 | mShutdownListener->waitForShutdown(_l, sp<RpcSession>::fromExisting(this)); |
Steven Moreland | dd67b94 | 2021-07-23 17:15:41 -0700 | [diff] [blame] | 207 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 208 | LOG_ALWAYS_FATAL_IF(!mThreads.empty(), "Shutdown failed"); |
| 209 | } |
| 210 | |
| 211 | _l.unlock(); |
| 212 | mState->clear(); |
| 213 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 214 | return true; |
| 215 | } |
| 216 | |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 217 | 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] | 218 | Parcel* reply, uint32_t flags) { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 219 | ExclusiveConnection connection; |
| 220 | status_t status = |
| 221 | ExclusiveConnection::find(sp<RpcSession>::fromExisting(this), |
| 222 | (flags & IBinder::FLAG_ONEWAY) ? ConnectionUse::CLIENT_ASYNC |
| 223 | : ConnectionUse::CLIENT, |
| 224 | &connection); |
| 225 | if (status != OK) return status; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 226 | return state()->transact(connection.get(), binder, code, data, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 227 | sp<RpcSession>::fromExisting(this), reply, flags); |
| 228 | } |
| 229 | |
Steven Moreland | 4f622fe | 2021-09-13 17:38:09 -0700 | [diff] [blame^] | 230 | status_t RpcSession::sendDecStrong(const BpBinder* binder) { |
| 231 | return sendDecStrong(binder->getPrivateAccessor().rpcAddress()); |
| 232 | } |
| 233 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 234 | status_t RpcSession::sendDecStrong(uint64_t address) { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 235 | ExclusiveConnection connection; |
| 236 | status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this), |
| 237 | ConnectionUse::CLIENT_REFCOUNT, &connection); |
| 238 | if (status != OK) return status; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 239 | return state()->sendDecStrong(connection.get(), sp<RpcSession>::fromExisting(this), address); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | status_t RpcSession::readId() { |
| 243 | { |
| 244 | std::lock_guard<std::mutex> _l(mMutex); |
| 245 | LOG_ALWAYS_FATAL_IF(mForServer != nullptr, "Can only update ID for client."); |
| 246 | } |
| 247 | |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 248 | ExclusiveConnection connection; |
| 249 | status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this), |
| 250 | ConnectionUse::CLIENT, &connection); |
| 251 | if (status != OK) return status; |
| 252 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 253 | status = state()->getSessionId(connection.get(), sp<RpcSession>::fromExisting(this), &mId); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 254 | if (status != OK) return status; |
| 255 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 256 | LOG_RPC_DETAIL("RpcSession %p has id %s", this, |
| 257 | base::HexString(mId.data(), mId.size()).c_str()); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 258 | return OK; |
| 259 | } |
| 260 | |
Steven Moreland | dd67b94 | 2021-07-23 17:15:41 -0700 | [diff] [blame] | 261 | void RpcSession::WaitForShutdownListener::onSessionAllIncomingThreadsEnded( |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 262 | const sp<RpcSession>& session) { |
| 263 | (void)session; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 266 | void RpcSession::WaitForShutdownListener::onSessionIncomingThreadEnded() { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 267 | mCv.notify_all(); |
| 268 | } |
| 269 | |
Steven Moreland | 791e466 | 2021-09-13 15:22:58 -0700 | [diff] [blame] | 270 | void RpcSession::WaitForShutdownListener::waitForShutdown(std::unique_lock<std::mutex>& lock, |
| 271 | const sp<RpcSession>& session) { |
| 272 | while (session->mIncomingConnections.size() > 0) { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 273 | if (std::cv_status::timeout == mCv.wait_for(lock, std::chrono::seconds(1))) { |
Steven Moreland | 791e466 | 2021-09-13 15:22:58 -0700 | [diff] [blame] | 274 | ALOGE("Waiting for RpcSession to shut down (1s w/o progress): %zu incoming connections " |
| 275 | "still.", |
| 276 | session->mIncomingConnections.size()); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 281 | void RpcSession::preJoinThreadOwnership(std::thread thread) { |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 282 | 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] | 283 | |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 284 | { |
| 285 | std::lock_guard<std::mutex> _l(mMutex); |
| 286 | mThreads[thread.get_id()] = std::move(thread); |
| 287 | } |
Steven Moreland | 5802c2b | 2021-05-12 20:13:04 +0000 | [diff] [blame] | 288 | } |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 289 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 290 | RpcSession::PreJoinSetupResult RpcSession::preJoinSetup( |
| 291 | std::unique_ptr<RpcTransport> rpcTransport) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 292 | // must be registered to allow arbitrary client code executing commands to |
| 293 | // be able to do nested calls (we can't only read from it) |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 294 | sp<RpcConnection> connection = assignIncomingConnectionToThisThread(std::move(rpcTransport)); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 295 | |
Steven Moreland | dd67b94 | 2021-07-23 17:15:41 -0700 | [diff] [blame] | 296 | status_t status; |
| 297 | |
| 298 | if (connection == nullptr) { |
| 299 | status = DEAD_OBJECT; |
| 300 | } else { |
| 301 | status = mState->readConnectionInit(connection, sp<RpcSession>::fromExisting(this)); |
| 302 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 303 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 304 | return PreJoinSetupResult{ |
| 305 | .connection = std::move(connection), |
| 306 | .status = status, |
| 307 | }; |
| 308 | } |
| 309 | |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 310 | namespace { |
| 311 | // RAII object for attaching / detaching current thread to JVM if Android Runtime exists. If |
| 312 | // Android Runtime doesn't exist, no-op. |
| 313 | class JavaThreadAttacher { |
| 314 | public: |
| 315 | JavaThreadAttacher() { |
| 316 | // Use dlsym to find androidJavaAttachThread because libandroid_runtime is loaded after |
| 317 | // libbinder. |
| 318 | auto vm = getJavaVM(); |
| 319 | if (vm == nullptr) return; |
| 320 | |
| 321 | char threadName[16]; |
| 322 | if (0 != pthread_getname_np(pthread_self(), threadName, sizeof(threadName))) { |
| 323 | constexpr const char* defaultThreadName = "UnknownRpcSessionThread"; |
| 324 | memcpy(threadName, defaultThreadName, |
| 325 | std::min<size_t>(sizeof(threadName), strlen(defaultThreadName) + 1)); |
| 326 | } |
| 327 | LOG_RPC_DETAIL("Attaching current thread %s to JVM", threadName); |
| 328 | JavaVMAttachArgs args; |
| 329 | args.version = JNI_VERSION_1_2; |
| 330 | args.name = threadName; |
| 331 | args.group = nullptr; |
| 332 | JNIEnv* env; |
| 333 | |
| 334 | LOG_ALWAYS_FATAL_IF(vm->AttachCurrentThread(&env, &args) != JNI_OK, |
| 335 | "Cannot attach thread %s to JVM", threadName); |
| 336 | mAttached = true; |
| 337 | } |
| 338 | ~JavaThreadAttacher() { |
| 339 | if (!mAttached) return; |
| 340 | auto vm = getJavaVM(); |
| 341 | LOG_ALWAYS_FATAL_IF(vm == nullptr, |
| 342 | "Unable to detach thread. No JavaVM, but it was present before!"); |
| 343 | |
| 344 | LOG_RPC_DETAIL("Detaching current thread from JVM"); |
| 345 | if (vm->DetachCurrentThread() != JNI_OK) { |
| 346 | mAttached = false; |
| 347 | } else { |
| 348 | ALOGW("Unable to detach current thread from JVM"); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | private: |
| 353 | DISALLOW_COPY_AND_ASSIGN(JavaThreadAttacher); |
| 354 | bool mAttached = false; |
| 355 | |
| 356 | static JavaVM* getJavaVM() { |
| 357 | static auto fn = reinterpret_cast<decltype(&AndroidRuntimeGetJavaVM)>( |
| 358 | dlsym(RTLD_DEFAULT, "AndroidRuntimeGetJavaVM")); |
| 359 | if (fn == nullptr) return nullptr; |
| 360 | return fn(); |
| 361 | } |
| 362 | }; |
| 363 | } // namespace |
| 364 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 365 | void RpcSession::join(sp<RpcSession>&& session, PreJoinSetupResult&& setupResult) { |
| 366 | sp<RpcConnection>& connection = setupResult.connection; |
| 367 | |
| 368 | if (setupResult.status == OK) { |
Steven Moreland | dd67b94 | 2021-07-23 17:15:41 -0700 | [diff] [blame] | 369 | LOG_ALWAYS_FATAL_IF(!connection, "must have connection if setup succeeded"); |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 370 | JavaThreadAttacher javaThreadAttacher; |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 371 | while (true) { |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 372 | status_t status = session->state()->getAndExecuteCommand(connection, session, |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 373 | RpcState::CommandType::ANY); |
| 374 | if (status != OK) { |
| 375 | LOG_RPC_DETAIL("Binder connection thread closing w/ status %s", |
| 376 | statusToString(status).c_str()); |
| 377 | break; |
| 378 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 379 | } |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 380 | } else { |
| 381 | ALOGE("Connection failed to init, closing with status %s", |
| 382 | statusToString(setupResult.status).c_str()); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 385 | sp<RpcSession::EventListener> listener; |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 386 | { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 387 | std::lock_guard<std::mutex> _l(session->mMutex); |
| 388 | auto it = session->mThreads.find(std::this_thread::get_id()); |
| 389 | LOG_ALWAYS_FATAL_IF(it == session->mThreads.end()); |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 390 | it->second.detach(); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 391 | session->mThreads.erase(it); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 392 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 393 | listener = session->mEventListener.promote(); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Steven Moreland | dd67b94 | 2021-07-23 17:15:41 -0700 | [diff] [blame] | 396 | // done after all cleanup, since session shutdown progresses via callbacks here |
| 397 | if (connection != nullptr) { |
| 398 | LOG_ALWAYS_FATAL_IF(!session->removeIncomingConnection(connection), |
| 399 | "bad state: connection object guaranteed to be in list"); |
| 400 | } |
| 401 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 402 | session = nullptr; |
| 403 | |
| 404 | if (listener != nullptr) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 405 | listener->onSessionIncomingThreadEnded(); |
Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Steven Moreland | 7b8bc4c | 2021-06-10 22:50:27 +0000 | [diff] [blame] | 409 | sp<RpcServer> RpcSession::server() { |
| 410 | RpcServer* unsafeServer = mForServer.unsafe_get(); |
| 411 | sp<RpcServer> server = mForServer.promote(); |
| 412 | |
| 413 | LOG_ALWAYS_FATAL_IF((unsafeServer == nullptr) != (server == nullptr), |
| 414 | "wp<> is to avoid strong cycle only"); |
| 415 | return server; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 418 | status_t RpcSession::setupClient(const std::function<status_t(const std::vector<uint8_t>& sessionId, |
| 419 | bool incoming)>& connectAndInit) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 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 | } |
Yifan Hong | 832521e | 2021-08-05 14:55:40 -0700 | [diff] [blame] | 426 | if (auto status = initShutdownTrigger(); status != OK) return status; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 427 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 428 | if (status_t status = connectAndInit({}, false /*incoming*/); status != OK) return status; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 429 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 430 | { |
| 431 | ExclusiveConnection connection; |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 432 | if (status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this), |
| 433 | ConnectionUse::CLIENT, &connection); |
| 434 | status != OK) |
| 435 | return status; |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 436 | |
| 437 | uint32_t version; |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 438 | if (status_t status = |
| 439 | state()->readNewSessionResponse(connection.get(), |
| 440 | sp<RpcSession>::fromExisting(this), &version); |
| 441 | status != OK) |
| 442 | return status; |
| 443 | if (!setProtocolVersion(version)) return BAD_VALUE; |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Steven Moreland | a5036f0 | 2021-06-08 02:26:57 +0000 | [diff] [blame] | 446 | // TODO(b/189955605): we should add additional sessions dynamically |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 447 | // instead of all at once. |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 448 | size_t numThreadsAvailable; |
Steven Moreland | 1be9135 | 2021-05-11 22:12:15 +0000 | [diff] [blame] | 449 | if (status_t status = getRemoteMaxThreads(&numThreadsAvailable); status != OK) { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 450 | ALOGE("Could not get max threads after initial session setup: %s", |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 451 | statusToString(status).c_str()); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 452 | return status; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | if (status_t status = readId(); status != OK) { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 456 | ALOGE("Could not get session id after initial session setup: %s", |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 457 | statusToString(status).c_str()); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 458 | return status; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Steven Moreland | a5036f0 | 2021-06-08 02:26:57 +0000 | [diff] [blame] | 461 | // TODO(b/189955605): we should add additional sessions dynamically |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 462 | // instead of all at once - the other side should be responsible for setting |
| 463 | // up additional connections. We need to create at least one (unless 0 are |
| 464 | // requested to be set) in order to allow the other side to reliably make |
| 465 | // any requests at all. |
| 466 | |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 467 | // we've already setup one client |
| 468 | for (size_t i = 0; i + 1 < numThreadsAvailable; i++) { |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 469 | if (status_t status = connectAndInit(mId, false /*incoming*/); status != OK) return status; |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 472 | for (size_t i = 0; i < mMaxThreads; i++) { |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 473 | if (status_t status = connectAndInit(mId, true /*incoming*/); status != OK) return status; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 476 | return OK; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 479 | status_t RpcSession::setupSocketClient(const RpcSocketAddress& addr) { |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 480 | return setupClient([&](const std::vector<uint8_t>& sessionId, bool incoming) { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 481 | return setupOneSocketConnection(addr, sessionId, incoming); |
| 482 | }); |
| 483 | } |
| 484 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 485 | status_t RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 486 | const std::vector<uint8_t>& sessionId, |
| 487 | bool incoming) { |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 488 | for (size_t tries = 0; tries < 5; tries++) { |
| 489 | if (tries > 0) usleep(10000); |
| 490 | |
Yifan Hong | b675ffe | 2021-08-05 16:37:17 -0700 | [diff] [blame] | 491 | unique_fd serverFd(TEMP_FAILURE_RETRY( |
| 492 | socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0))); |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 493 | if (serverFd == -1) { |
| 494 | int savedErrno = errno; |
| 495 | ALOGE("Could not create socket at %s: %s", addr.toString().c_str(), |
| 496 | strerror(savedErrno)); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 497 | return -savedErrno; |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) { |
Yifan Hong | 95d15e5 | 2021-08-25 17:15:15 -0700 | [diff] [blame] | 501 | int connErrno = errno; |
| 502 | if (connErrno == EAGAIN || connErrno == EINPROGRESS) { |
| 503 | // For non-blocking sockets, connect() may return EAGAIN (for unix domain socket) or |
| 504 | // EINPROGRESS (for others). Call poll() and getsockopt() to get the error. |
| 505 | status_t pollStatus = mShutdownTrigger->triggerablePoll(serverFd, POLLOUT); |
| 506 | if (pollStatus != OK) { |
| 507 | ALOGE("Could not POLLOUT after connect() on non-blocking socket: %s", |
| 508 | statusToString(pollStatus).c_str()); |
| 509 | return pollStatus; |
| 510 | } |
| 511 | // Set connErrno to the errno that connect() would have set if the fd were blocking. |
| 512 | socklen_t connErrnoLen = sizeof(connErrno); |
| 513 | int ret = |
| 514 | getsockopt(serverFd.get(), SOL_SOCKET, SO_ERROR, &connErrno, &connErrnoLen); |
| 515 | if (ret == -1) { |
| 516 | int savedErrno = errno; |
| 517 | ALOGE("Could not getsockopt() after connect() on non-blocking socket: %s. " |
| 518 | "(Original error from connect() is: %s)", |
| 519 | strerror(savedErrno), strerror(connErrno)); |
| 520 | return -savedErrno; |
| 521 | } |
| 522 | // Retrieved the real connErrno as if connect() was called with a blocking socket |
| 523 | // fd. Continue checking connErrno. |
| 524 | } |
| 525 | if (connErrno == ECONNRESET) { |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 526 | ALOGW("Connection reset on %s", addr.toString().c_str()); |
| 527 | continue; |
| 528 | } |
Yifan Hong | 95d15e5 | 2021-08-25 17:15:15 -0700 | [diff] [blame] | 529 | // connErrno could be zero if getsockopt determines so. Hence zero-check again. |
| 530 | if (connErrno != 0) { |
Yifan Hong | d9f8cef | 2021-08-05 15:17:31 -0700 | [diff] [blame] | 531 | ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(), |
Yifan Hong | 95d15e5 | 2021-08-25 17:15:15 -0700 | [diff] [blame] | 532 | strerror(connErrno)); |
| 533 | return -connErrno; |
Yifan Hong | d9f8cef | 2021-08-05 15:17:31 -0700 | [diff] [blame] | 534 | } |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 535 | } |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 536 | LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get()); |
| 537 | |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 538 | return initAndAddConnection(std::move(serverFd), sessionId, incoming); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 541 | ALOGE("Ran out of retries to connect to %s", addr.toString().c_str()); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 542 | return UNKNOWN_ERROR; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 545 | status_t RpcSession::initAndAddConnection(unique_fd fd, const std::vector<uint8_t>& sessionId, |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 546 | bool incoming) { |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 547 | LOG_ALWAYS_FATAL_IF(mShutdownTrigger == nullptr); |
Yifan Hong | ecf937d | 2021-08-11 17:29:28 -0700 | [diff] [blame] | 548 | auto server = mCtx->newTransport(std::move(fd), mShutdownTrigger.get()); |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 549 | if (server == nullptr) { |
Yifan Hong | ecf937d | 2021-08-11 17:29:28 -0700 | [diff] [blame] | 550 | ALOGE("%s: Unable to set up RpcTransport", __PRETTY_FUNCTION__); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 551 | return UNKNOWN_ERROR; |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | LOG_RPC_DETAIL("Socket at client with RpcTransport %p", server.get()); |
| 555 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 556 | if (sessionId.size() > std::numeric_limits<uint16_t>::max()) { |
| 557 | ALOGE("Session ID too big %zu", sessionId.size()); |
| 558 | return BAD_VALUE; |
| 559 | } |
| 560 | |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 561 | RpcConnectionHeader header{ |
| 562 | .version = mProtocolVersion.value_or(RPC_WIRE_PROTOCOL_VERSION), |
| 563 | .options = 0, |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 564 | .sessionIdSize = static_cast<uint16_t>(sessionId.size()), |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 565 | }; |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 566 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 567 | if (incoming) { |
| 568 | header.options |= RPC_CONNECTION_OPTION_INCOMING; |
| 569 | } |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 570 | |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 571 | auto sendHeaderStatus = |
| 572 | server->interruptableWriteFully(mShutdownTrigger.get(), &header, sizeof(header)); |
| 573 | if (sendHeaderStatus != OK) { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 574 | ALOGE("Could not write connection header to socket: %s", |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 575 | statusToString(sendHeaderStatus).c_str()); |
| 576 | return sendHeaderStatus; |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 579 | if (sessionId.size() > 0) { |
| 580 | auto sendSessionIdStatus = |
| 581 | server->interruptableWriteFully(mShutdownTrigger.get(), sessionId.data(), |
| 582 | sessionId.size()); |
| 583 | if (sendSessionIdStatus != OK) { |
| 584 | ALOGE("Could not write session ID ('%s') to socket: %s", |
| 585 | base::HexString(sessionId.data(), sessionId.size()).c_str(), |
| 586 | statusToString(sendSessionIdStatus).c_str()); |
| 587 | return sendSessionIdStatus; |
| 588 | } |
| 589 | } |
| 590 | |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 591 | LOG_RPC_DETAIL("Socket at client: header sent"); |
| 592 | |
| 593 | if (incoming) { |
| 594 | return addIncomingConnection(std::move(server)); |
| 595 | } else { |
| 596 | return addOutgoingConnection(std::move(server), true /*init*/); |
| 597 | } |
| 598 | } |
| 599 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 600 | status_t RpcSession::addIncomingConnection(std::unique_ptr<RpcTransport> rpcTransport) { |
Steven Moreland | fba6f77 | 2021-07-15 22:45:09 +0000 | [diff] [blame] | 601 | std::mutex mutex; |
| 602 | std::condition_variable joinCv; |
| 603 | std::unique_lock<std::mutex> lock(mutex); |
| 604 | std::thread thread; |
| 605 | sp<RpcSession> thiz = sp<RpcSession>::fromExisting(this); |
| 606 | bool ownershipTransferred = false; |
| 607 | thread = std::thread([&]() { |
| 608 | std::unique_lock<std::mutex> threadLock(mutex); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 609 | std::unique_ptr<RpcTransport> movedRpcTransport = std::move(rpcTransport); |
Steven Moreland | fba6f77 | 2021-07-15 22:45:09 +0000 | [diff] [blame] | 610 | // NOLINTNEXTLINE(performance-unnecessary-copy-initialization) |
| 611 | sp<RpcSession> session = thiz; |
| 612 | session->preJoinThreadOwnership(std::move(thread)); |
| 613 | |
| 614 | // only continue once we have a response or the connection fails |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 615 | auto setupResult = session->preJoinSetup(std::move(movedRpcTransport)); |
Steven Moreland | fba6f77 | 2021-07-15 22:45:09 +0000 | [diff] [blame] | 616 | |
| 617 | ownershipTransferred = true; |
| 618 | threadLock.unlock(); |
| 619 | joinCv.notify_one(); |
| 620 | // do not use & vars below |
| 621 | |
| 622 | RpcSession::join(std::move(session), std::move(setupResult)); |
| 623 | }); |
| 624 | joinCv.wait(lock, [&] { return ownershipTransferred; }); |
| 625 | LOG_ALWAYS_FATAL_IF(!ownershipTransferred); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 626 | return OK; |
Steven Moreland | fba6f77 | 2021-07-15 22:45:09 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Yifan Hong | 832521e | 2021-08-05 14:55:40 -0700 | [diff] [blame] | 629 | status_t RpcSession::initShutdownTrigger() { |
| 630 | // first client connection added, but setForServer not called, so |
| 631 | // initializaing for a client. |
| 632 | if (mShutdownTrigger == nullptr) { |
| 633 | mShutdownTrigger = FdTrigger::make(); |
| 634 | mEventListener = mShutdownListener = sp<WaitForShutdownListener>::make(); |
| 635 | if (mShutdownTrigger == nullptr) return INVALID_OPERATION; |
| 636 | } |
| 637 | return OK; |
| 638 | } |
| 639 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 640 | status_t RpcSession::addOutgoingConnection(std::unique_ptr<RpcTransport> rpcTransport, bool init) { |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 641 | sp<RpcConnection> connection = sp<RpcConnection>::make(); |
| 642 | { |
| 643 | std::lock_guard<std::mutex> _l(mMutex); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 644 | connection->rpcTransport = std::move(rpcTransport); |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 645 | connection->exclusiveTid = gettid(); |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 646 | mOutgoingConnections.push_back(connection); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Steven Moreland | b86e26b | 2021-06-12 00:35:58 +0000 | [diff] [blame] | 649 | status_t status = OK; |
| 650 | if (init) { |
| 651 | mState->sendConnectionInit(connection, sp<RpcSession>::fromExisting(this)); |
| 652 | } |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 653 | |
| 654 | { |
| 655 | std::lock_guard<std::mutex> _l(mMutex); |
| 656 | connection->exclusiveTid = std::nullopt; |
| 657 | } |
| 658 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 659 | return status; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 662 | bool RpcSession::setForServer(const wp<RpcServer>& server, const wp<EventListener>& eventListener, |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 663 | const std::vector<uint8_t>& sessionId) { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 664 | LOG_ALWAYS_FATAL_IF(mForServer != nullptr); |
| 665 | LOG_ALWAYS_FATAL_IF(server == nullptr); |
| 666 | LOG_ALWAYS_FATAL_IF(mEventListener != nullptr); |
| 667 | LOG_ALWAYS_FATAL_IF(eventListener == nullptr); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 668 | LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr); |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 669 | |
| 670 | mShutdownTrigger = FdTrigger::make(); |
| 671 | if (mShutdownTrigger == nullptr) return false; |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 672 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 673 | mId = sessionId; |
| 674 | mForServer = server; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 675 | mEventListener = eventListener; |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 676 | return true; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 679 | sp<RpcSession::RpcConnection> RpcSession::assignIncomingConnectionToThisThread( |
| 680 | std::unique_ptr<RpcTransport> rpcTransport) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 681 | std::lock_guard<std::mutex> _l(mMutex); |
Steven Moreland | dd67b94 | 2021-07-23 17:15:41 -0700 | [diff] [blame] | 682 | |
Steven Moreland | 132d5bf | 2021-08-03 16:13:24 -0700 | [diff] [blame] | 683 | if (mIncomingConnections.size() >= mMaxThreads) { |
| 684 | ALOGE("Cannot add thread to session with %zu threads (max is set to %zu)", |
| 685 | mIncomingConnections.size(), mMaxThreads); |
| 686 | return nullptr; |
| 687 | } |
| 688 | |
Steven Moreland | dd67b94 | 2021-07-23 17:15:41 -0700 | [diff] [blame] | 689 | // Don't accept any more connections, some have shutdown. Usually this |
| 690 | // happens when new connections are still being established as part of a |
| 691 | // very short-lived session which shuts down after it already started |
| 692 | // accepting new connections. |
| 693 | if (mIncomingConnections.size() < mMaxIncomingConnections) { |
| 694 | return nullptr; |
| 695 | } |
| 696 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 697 | sp<RpcConnection> session = sp<RpcConnection>::make(); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 698 | session->rpcTransport = std::move(rpcTransport); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 699 | session->exclusiveTid = gettid(); |
Steven Moreland | dd67b94 | 2021-07-23 17:15:41 -0700 | [diff] [blame] | 700 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 701 | mIncomingConnections.push_back(session); |
Steven Moreland | dd67b94 | 2021-07-23 17:15:41 -0700 | [diff] [blame] | 702 | mMaxIncomingConnections = mIncomingConnections.size(); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 703 | |
| 704 | return session; |
| 705 | } |
| 706 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 707 | bool RpcSession::removeIncomingConnection(const sp<RpcConnection>& connection) { |
Steven Moreland | dd67b94 | 2021-07-23 17:15:41 -0700 | [diff] [blame] | 708 | std::unique_lock<std::mutex> _l(mMutex); |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 709 | if (auto it = std::find(mIncomingConnections.begin(), mIncomingConnections.end(), connection); |
| 710 | it != mIncomingConnections.end()) { |
| 711 | mIncomingConnections.erase(it); |
| 712 | if (mIncomingConnections.size() == 0) { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 713 | sp<EventListener> listener = mEventListener.promote(); |
| 714 | if (listener) { |
Steven Moreland | dd67b94 | 2021-07-23 17:15:41 -0700 | [diff] [blame] | 715 | _l.unlock(); |
| 716 | listener->onSessionAllIncomingThreadsEnded(sp<RpcSession>::fromExisting(this)); |
Steven Moreland | a86e8fe | 2021-05-26 22:52:35 +0000 | [diff] [blame] | 717 | } |
Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 718 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 719 | return true; |
| 720 | } |
| 721 | return false; |
| 722 | } |
| 723 | |
Yifan Hong | ecf937d | 2021-08-11 17:29:28 -0700 | [diff] [blame] | 724 | std::string RpcSession::getCertificate(CertificateFormat format) { |
| 725 | return mCtx->getCertificate(format); |
| 726 | } |
| 727 | |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 728 | status_t RpcSession::ExclusiveConnection::find(const sp<RpcSession>& session, ConnectionUse use, |
| 729 | ExclusiveConnection* connection) { |
| 730 | connection->mSession = session; |
| 731 | connection->mConnection = nullptr; |
| 732 | connection->mReentrant = false; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 733 | |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 734 | pid_t tid = gettid(); |
| 735 | std::unique_lock<std::mutex> _l(session->mMutex); |
| 736 | |
| 737 | session->mWaitingThreads++; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 738 | while (true) { |
| 739 | sp<RpcConnection> exclusive; |
| 740 | sp<RpcConnection> available; |
| 741 | |
| 742 | // CHECK FOR DEDICATED CLIENT SOCKET |
| 743 | // |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 744 | // A server/looper should always use a dedicated connection if available |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 745 | findConnection(tid, &exclusive, &available, session->mOutgoingConnections, |
| 746 | session->mOutgoingConnectionsOffset); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 747 | |
| 748 | // WARNING: this assumes a server cannot request its client to send |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 749 | // a transaction, as mIncomingConnections is excluded below. |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 750 | // |
| 751 | // Imagine we have more than one thread in play, and a single thread |
| 752 | // sends a synchronous, then an asynchronous command. Imagine the |
| 753 | // asynchronous command is sent on the first client connection. Then, if |
| 754 | // we naively send a synchronous command to that same connection, the |
| 755 | // thread on the far side might be busy processing the asynchronous |
| 756 | // command. So, we move to considering the second available thread |
| 757 | // for subsequent calls. |
| 758 | if (use == ConnectionUse::CLIENT_ASYNC && (exclusive != nullptr || available != nullptr)) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 759 | session->mOutgoingConnectionsOffset = (session->mOutgoingConnectionsOffset + 1) % |
| 760 | session->mOutgoingConnections.size(); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 763 | // USE SERVING SOCKET (e.g. nested transaction) |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 764 | if (use != ConnectionUse::CLIENT_ASYNC) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 765 | sp<RpcConnection> exclusiveIncoming; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 766 | // server connections are always assigned to a thread |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 767 | findConnection(tid, &exclusiveIncoming, nullptr /*available*/, |
| 768 | session->mIncomingConnections, 0 /* index hint */); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 769 | |
| 770 | // asynchronous calls cannot be nested, we currently allow ref count |
| 771 | // calls to be nested (so that you can use this without having extra |
| 772 | // threads). Note 'drainCommands' is used so that these ref counts can't |
| 773 | // build up. |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 774 | if (exclusiveIncoming != nullptr) { |
| 775 | if (exclusiveIncoming->allowNested) { |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 776 | // guaranteed to be processed as nested command |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 777 | exclusive = exclusiveIncoming; |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 778 | } else if (use == ConnectionUse::CLIENT_REFCOUNT && available == nullptr) { |
| 779 | // prefer available socket, but if we don't have one, don't |
| 780 | // wait for one |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 781 | exclusive = exclusiveIncoming; |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 782 | } |
| 783 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 786 | // if our thread is already using a connection, prioritize using that |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 787 | if (exclusive != nullptr) { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 788 | connection->mConnection = exclusive; |
| 789 | connection->mReentrant = true; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 790 | break; |
| 791 | } else if (available != nullptr) { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 792 | connection->mConnection = available; |
| 793 | connection->mConnection->exclusiveTid = tid; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 794 | break; |
| 795 | } |
| 796 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 797 | if (session->mOutgoingConnections.size() == 0) { |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 798 | ALOGE("Session has no client connections. This is required for an RPC server to make " |
| 799 | "any non-nested (e.g. oneway or on another thread) calls. Use: %d. Server " |
| 800 | "connections: %zu", |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 801 | static_cast<int>(use), session->mIncomingConnections.size()); |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 802 | return WOULD_BLOCK; |
| 803 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 804 | |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 805 | 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] | 806 | session->mOutgoingConnections.size(), session->mIncomingConnections.size()); |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 807 | session->mAvailableConnectionCv.wait(_l); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 808 | } |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 809 | session->mWaitingThreads--; |
| 810 | |
| 811 | return OK; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | void RpcSession::ExclusiveConnection::findConnection(pid_t tid, sp<RpcConnection>* exclusive, |
| 815 | sp<RpcConnection>* available, |
| 816 | std::vector<sp<RpcConnection>>& sockets, |
| 817 | size_t socketsIndexHint) { |
| 818 | LOG_ALWAYS_FATAL_IF(sockets.size() > 0 && socketsIndexHint >= sockets.size(), |
| 819 | "Bad index %zu >= %zu", socketsIndexHint, sockets.size()); |
| 820 | |
| 821 | if (*exclusive != nullptr) return; // consistent with break below |
| 822 | |
| 823 | for (size_t i = 0; i < sockets.size(); i++) { |
| 824 | sp<RpcConnection>& socket = sockets[(i + socketsIndexHint) % sockets.size()]; |
| 825 | |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 826 | // take first available connection (intuition = caching) |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 827 | if (available && *available == nullptr && socket->exclusiveTid == std::nullopt) { |
| 828 | *available = socket; |
| 829 | continue; |
| 830 | } |
| 831 | |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 832 | // though, prefer to take connection which is already inuse by this thread |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 833 | // (nested transactions) |
| 834 | if (exclusive && socket->exclusiveTid == tid) { |
| 835 | *exclusive = socket; |
| 836 | break; // consistent with return above |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | RpcSession::ExclusiveConnection::~ExclusiveConnection() { |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 842 | // 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] | 843 | // is using this fd, and it retains the right to it. So, we don't give up |
| 844 | // exclusive ownership, and no thread is freed. |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 845 | if (!mReentrant && mConnection != nullptr) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 846 | std::unique_lock<std::mutex> _l(mSession->mMutex); |
| 847 | mConnection->exclusiveTid = std::nullopt; |
| 848 | if (mSession->mWaitingThreads > 0) { |
| 849 | _l.unlock(); |
| 850 | mSession->mAvailableConnectionCv.notify_one(); |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | } // namespace android |