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