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