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