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