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