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