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