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