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