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 | |
| 21 | #include <inttypes.h> |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 22 | #include <poll.h> |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <string_view> |
| 26 | |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 27 | #include <android-base/macros.h> |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 28 | #include <binder/Parcel.h> |
Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 29 | #include <binder/RpcServer.h> |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 30 | #include <binder/Stability.h> |
| 31 | #include <utils/String8.h> |
| 32 | |
| 33 | #include "RpcSocketAddress.h" |
| 34 | #include "RpcState.h" |
| 35 | #include "RpcWireFormat.h" |
| 36 | |
| 37 | #ifdef __GLIBC__ |
| 38 | extern "C" pid_t gettid(); |
| 39 | #endif |
| 40 | |
| 41 | namespace android { |
| 42 | |
| 43 | using base::unique_fd; |
| 44 | |
| 45 | RpcSession::RpcSession() { |
| 46 | LOG_RPC_DETAIL("RpcSession created %p", this); |
| 47 | |
| 48 | mState = std::make_unique<RpcState>(); |
| 49 | } |
| 50 | RpcSession::~RpcSession() { |
| 51 | LOG_RPC_DETAIL("RpcSession destroyed %p", this); |
| 52 | |
| 53 | std::lock_guard<std::mutex> _l(mMutex); |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 54 | LOG_ALWAYS_FATAL_IF(mServerConnections.size() != 0, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 55 | "Should not be able to destroy a session with servers in use."); |
| 56 | } |
| 57 | |
| 58 | sp<RpcSession> RpcSession::make() { |
| 59 | return sp<RpcSession>::make(); |
| 60 | } |
| 61 | |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 62 | void RpcSession::setMaxThreads(size_t threads) { |
| 63 | std::lock_guard<std::mutex> _l(mMutex); |
| 64 | LOG_ALWAYS_FATAL_IF(!mClientConnections.empty() || !mServerConnections.empty(), |
| 65 | "Must set max threads before setting up connections, but has %zu client(s) " |
| 66 | "and %zu server(s)", |
| 67 | mClientConnections.size(), mServerConnections.size()); |
| 68 | mMaxThreads = threads; |
| 69 | } |
| 70 | |
| 71 | size_t RpcSession::getMaxThreads() { |
| 72 | std::lock_guard<std::mutex> _l(mMutex); |
| 73 | return mMaxThreads; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 76 | bool RpcSession::setupUnixDomainClient(const char* path) { |
| 77 | return setupSocketClient(UnixSocketAddress(path)); |
| 78 | } |
| 79 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 80 | bool RpcSession::setupVsockClient(unsigned int cid, unsigned int port) { |
| 81 | return setupSocketClient(VsockSocketAddress(cid, port)); |
| 82 | } |
| 83 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 84 | bool RpcSession::setupInetClient(const char* addr, unsigned int port) { |
| 85 | auto aiStart = InetSocketAddress::getAddrInfo(addr, port); |
| 86 | if (aiStart == nullptr) return false; |
| 87 | for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { |
| 88 | InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, addr, port); |
| 89 | if (setupSocketClient(socketAddress)) return true; |
| 90 | } |
| 91 | ALOGE("None of the socket address resolved for %s:%u can be added as inet client.", addr, port); |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | bool RpcSession::addNullDebuggingClient() { |
| 96 | unique_fd serverFd(TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY | O_CLOEXEC))); |
| 97 | |
| 98 | if (serverFd == -1) { |
| 99 | ALOGE("Could not connect to /dev/null: %s", strerror(errno)); |
| 100 | return false; |
| 101 | } |
| 102 | |
Steven Moreland | b0dd118 | 2021-05-25 01:56:46 +0000 | [diff] [blame] | 103 | return addClientConnection(std::move(serverFd)); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | sp<IBinder> RpcSession::getRootObject() { |
| 107 | ExclusiveConnection connection(sp<RpcSession>::fromExisting(this), ConnectionUse::CLIENT); |
| 108 | return state()->getRootObject(connection.fd(), sp<RpcSession>::fromExisting(this)); |
| 109 | } |
| 110 | |
Steven Moreland | 1be9135 | 2021-05-11 22:12:15 +0000 | [diff] [blame] | 111 | status_t RpcSession::getRemoteMaxThreads(size_t* maxThreads) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 112 | ExclusiveConnection connection(sp<RpcSession>::fromExisting(this), ConnectionUse::CLIENT); |
| 113 | return state()->getMaxThreads(connection.fd(), sp<RpcSession>::fromExisting(this), maxThreads); |
| 114 | } |
| 115 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 116 | bool RpcSession::shutdownAndWait(bool wait) { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 117 | std::unique_lock<std::mutex> _l(mMutex); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 118 | LOG_ALWAYS_FATAL_IF(mShutdownTrigger == nullptr, "Shutdown trigger not installed"); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 119 | |
| 120 | mShutdownTrigger->trigger(); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 121 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 122 | if (wait) { |
| 123 | LOG_ALWAYS_FATAL_IF(mShutdownListener == nullptr, "Shutdown listener not installed"); |
| 124 | mShutdownListener->waitForShutdown(_l); |
| 125 | LOG_ALWAYS_FATAL_IF(!mThreads.empty(), "Shutdown failed"); |
| 126 | } |
| 127 | |
| 128 | _l.unlock(); |
| 129 | mState->clear(); |
| 130 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 131 | return true; |
| 132 | } |
| 133 | |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 134 | 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] | 135 | Parcel* reply, uint32_t flags) { |
| 136 | ExclusiveConnection connection(sp<RpcSession>::fromExisting(this), |
| 137 | (flags & IBinder::FLAG_ONEWAY) ? ConnectionUse::CLIENT_ASYNC |
| 138 | : ConnectionUse::CLIENT); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 139 | return state()->transact(connection.fd(), binder, code, data, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 140 | sp<RpcSession>::fromExisting(this), reply, flags); |
| 141 | } |
| 142 | |
| 143 | status_t RpcSession::sendDecStrong(const RpcAddress& address) { |
| 144 | ExclusiveConnection connection(sp<RpcSession>::fromExisting(this), |
| 145 | ConnectionUse::CLIENT_REFCOUNT); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 146 | return state()->sendDecStrong(connection.fd(), sp<RpcSession>::fromExisting(this), address); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Steven Moreland | e47511f | 2021-05-20 00:07:41 +0000 | [diff] [blame] | 149 | std::unique_ptr<RpcSession::FdTrigger> RpcSession::FdTrigger::make() { |
| 150 | auto ret = std::make_unique<RpcSession::FdTrigger>(); |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 151 | if (!android::base::Pipe(&ret->mRead, &ret->mWrite)) { |
| 152 | ALOGE("Could not create pipe %s", strerror(errno)); |
| 153 | return nullptr; |
| 154 | } |
Steven Moreland | e47511f | 2021-05-20 00:07:41 +0000 | [diff] [blame] | 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | void RpcSession::FdTrigger::trigger() { |
| 159 | mWrite.reset(); |
| 160 | } |
| 161 | |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 162 | bool RpcSession::FdTrigger::isTriggered() { |
| 163 | return mWrite == -1; |
| 164 | } |
| 165 | |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 166 | status_t RpcSession::FdTrigger::triggerablePollRead(base::borrowed_fd fd) { |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 167 | while (true) { |
Steven Moreland | dfe3be9 | 2021-05-22 00:24:29 +0000 | [diff] [blame] | 168 | pollfd pfd[]{{.fd = fd.get(), .events = POLLIN | POLLHUP, .revents = 0}, |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 169 | {.fd = mRead.get(), .events = POLLHUP, .revents = 0}}; |
| 170 | int ret = TEMP_FAILURE_RETRY(poll(pfd, arraysize(pfd), -1)); |
| 171 | if (ret < 0) { |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 172 | return -errno; |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 173 | } |
| 174 | if (ret == 0) { |
| 175 | continue; |
| 176 | } |
| 177 | if (pfd[1].revents & POLLHUP) { |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 178 | return -ECANCELED; |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 179 | } |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 180 | return pfd[0].revents & POLLIN ? OK : DEAD_OBJECT; |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 184 | status_t RpcSession::FdTrigger::interruptableReadFully(base::borrowed_fd fd, void* data, |
| 185 | size_t size) { |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 186 | uint8_t* buffer = reinterpret_cast<uint8_t*>(data); |
| 187 | uint8_t* end = buffer + size; |
| 188 | |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 189 | status_t status; |
| 190 | while ((status = triggerablePollRead(fd)) == OK) { |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 191 | ssize_t readSize = TEMP_FAILURE_RETRY(recv(fd.get(), buffer, end - buffer, MSG_NOSIGNAL)); |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 192 | if (readSize == 0) return DEAD_OBJECT; // EOF |
Steven Moreland | dfe3be9 | 2021-05-22 00:24:29 +0000 | [diff] [blame] | 193 | |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 194 | if (readSize < 0) { |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 195 | return -errno; |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 196 | } |
| 197 | buffer += readSize; |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 198 | if (buffer == end) return OK; |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 199 | } |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 200 | return status; |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 203 | status_t RpcSession::readId() { |
| 204 | { |
| 205 | std::lock_guard<std::mutex> _l(mMutex); |
| 206 | LOG_ALWAYS_FATAL_IF(mForServer != nullptr, "Can only update ID for client."); |
| 207 | } |
| 208 | |
| 209 | int32_t id; |
| 210 | |
| 211 | ExclusiveConnection connection(sp<RpcSession>::fromExisting(this), ConnectionUse::CLIENT); |
| 212 | status_t status = |
| 213 | state()->getSessionId(connection.fd(), sp<RpcSession>::fromExisting(this), &id); |
| 214 | if (status != OK) return status; |
| 215 | |
| 216 | LOG_RPC_DETAIL("RpcSession %p has id %d", this, id); |
| 217 | mId = id; |
| 218 | return OK; |
| 219 | } |
| 220 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 221 | void RpcSession::WaitForShutdownListener::onSessionLockedAllServerThreadsEnded( |
| 222 | const sp<RpcSession>& session) { |
| 223 | (void)session; |
| 224 | mShutdown = true; |
| 225 | } |
| 226 | |
| 227 | void RpcSession::WaitForShutdownListener::onSessionServerThreadEnded() { |
| 228 | mCv.notify_all(); |
| 229 | } |
| 230 | |
| 231 | void RpcSession::WaitForShutdownListener::waitForShutdown(std::unique_lock<std::mutex>& lock) { |
| 232 | while (!mShutdown) { |
| 233 | if (std::cv_status::timeout == mCv.wait_for(lock, std::chrono::seconds(1))) { |
| 234 | ALOGE("Waiting for RpcSession to shut down (1s w/o progress)."); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 239 | void RpcSession::preJoinThreadOwnership(std::thread thread) { |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 240 | 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] | 241 | |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 242 | { |
| 243 | std::lock_guard<std::mutex> _l(mMutex); |
| 244 | mThreads[thread.get_id()] = std::move(thread); |
| 245 | } |
Steven Moreland | 5802c2b | 2021-05-12 20:13:04 +0000 | [diff] [blame] | 246 | } |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 247 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 248 | RpcSession::PreJoinSetupResult RpcSession::preJoinSetup(base::unique_fd fd) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 249 | // must be registered to allow arbitrary client code executing commands to |
| 250 | // be able to do nested calls (we can't only read from it) |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 251 | sp<RpcConnection> connection = assignServerToThisThread(std::move(fd)); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 252 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 253 | status_t status = |
| 254 | mState->readConnectionInit(connection->fd, sp<RpcSession>::fromExisting(this)); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 255 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 256 | return PreJoinSetupResult{ |
| 257 | .connection = std::move(connection), |
| 258 | .status = status, |
| 259 | }; |
| 260 | } |
| 261 | |
| 262 | void RpcSession::join(sp<RpcSession>&& session, PreJoinSetupResult&& setupResult) { |
| 263 | sp<RpcConnection>& connection = setupResult.connection; |
| 264 | |
| 265 | if (setupResult.status == OK) { |
| 266 | while (true) { |
| 267 | status_t status = session->state()->getAndExecuteCommand(connection->fd, session, |
| 268 | RpcState::CommandType::ANY); |
| 269 | if (status != OK) { |
| 270 | LOG_RPC_DETAIL("Binder connection thread closing w/ status %s", |
| 271 | statusToString(status).c_str()); |
| 272 | break; |
| 273 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 274 | } |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 275 | } else { |
| 276 | ALOGE("Connection failed to init, closing with status %s", |
| 277 | statusToString(setupResult.status).c_str()); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 280 | LOG_ALWAYS_FATAL_IF(!session->removeServerConnection(connection), |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 281 | "bad state: connection object guaranteed to be in list"); |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 282 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 283 | sp<RpcSession::EventListener> listener; |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 284 | { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 285 | std::lock_guard<std::mutex> _l(session->mMutex); |
| 286 | auto it = session->mThreads.find(std::this_thread::get_id()); |
| 287 | LOG_ALWAYS_FATAL_IF(it == session->mThreads.end()); |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 288 | it->second.detach(); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 289 | session->mThreads.erase(it); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 290 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 291 | listener = session->mEventListener.promote(); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 294 | session = nullptr; |
| 295 | |
| 296 | if (listener != nullptr) { |
| 297 | listener->onSessionServerThreadEnded(); |
Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 301 | wp<RpcServer> RpcSession::server() { |
| 302 | return mForServer; |
| 303 | } |
| 304 | |
| 305 | bool RpcSession::setupSocketClient(const RpcSocketAddress& addr) { |
| 306 | { |
| 307 | std::lock_guard<std::mutex> _l(mMutex); |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 308 | LOG_ALWAYS_FATAL_IF(mClientConnections.size() != 0, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 309 | "Must only setup session once, but already has %zu clients", |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 310 | mClientConnections.size()); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 313 | if (!setupOneSocketConnection(addr, RPC_SESSION_ID_NEW, false /*reverse*/)) return false; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 314 | |
Steven Moreland | a5036f0 | 2021-06-08 02:26:57 +0000 | [diff] [blame] | 315 | // TODO(b/189955605): we should add additional sessions dynamically |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 316 | // instead of all at once. |
| 317 | // TODO(b/186470974): first risk of blocking |
| 318 | size_t numThreadsAvailable; |
Steven Moreland | 1be9135 | 2021-05-11 22:12:15 +0000 | [diff] [blame] | 319 | if (status_t status = getRemoteMaxThreads(&numThreadsAvailable); status != OK) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 320 | ALOGE("Could not get max threads after initial session to %s: %s", addr.toString().c_str(), |
| 321 | statusToString(status).c_str()); |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | if (status_t status = readId(); status != OK) { |
| 326 | ALOGE("Could not get session id after initial session to %s; %s", addr.toString().c_str(), |
| 327 | statusToString(status).c_str()); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | // we've already setup one client |
| 332 | for (size_t i = 0; i + 1 < numThreadsAvailable; i++) { |
Steven Moreland | a5036f0 | 2021-06-08 02:26:57 +0000 | [diff] [blame] | 333 | // TODO(b/189955605): shutdown existing connections? |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 334 | if (!setupOneSocketConnection(addr, mId.value(), false /*reverse*/)) return false; |
| 335 | } |
| 336 | |
Steven Moreland | a5036f0 | 2021-06-08 02:26:57 +0000 | [diff] [blame] | 337 | // TODO(b/189955605): we should add additional sessions dynamically |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 338 | // instead of all at once - the other side should be responsible for setting |
| 339 | // up additional connections. We need to create at least one (unless 0 are |
| 340 | // requested to be set) in order to allow the other side to reliably make |
| 341 | // any requests at all. |
| 342 | |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 343 | for (size_t i = 0; i < mMaxThreads; i++) { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 344 | if (!setupOneSocketConnection(addr, mId.value(), true /*reverse*/)) return false; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | return true; |
| 348 | } |
| 349 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 350 | bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, int32_t id, bool reverse) { |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 351 | for (size_t tries = 0; tries < 5; tries++) { |
| 352 | if (tries > 0) usleep(10000); |
| 353 | |
| 354 | unique_fd serverFd( |
| 355 | TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); |
| 356 | if (serverFd == -1) { |
| 357 | int savedErrno = errno; |
| 358 | ALOGE("Could not create socket at %s: %s", addr.toString().c_str(), |
| 359 | strerror(savedErrno)); |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) { |
| 364 | if (errno == ECONNRESET) { |
| 365 | ALOGW("Connection reset on %s", addr.toString().c_str()); |
| 366 | continue; |
| 367 | } |
| 368 | int savedErrno = errno; |
| 369 | ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(), |
| 370 | strerror(savedErrno)); |
| 371 | return false; |
| 372 | } |
| 373 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 374 | RpcConnectionHeader header{ |
| 375 | .sessionId = id, |
| 376 | }; |
| 377 | if (reverse) header.options |= RPC_CONNECTION_OPTION_REVERSE; |
| 378 | |
| 379 | if (sizeof(header) != TEMP_FAILURE_RETRY(write(serverFd.get(), &header, sizeof(header)))) { |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 380 | int savedErrno = errno; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 381 | ALOGE("Could not write connection header to socket at %s: %s", addr.toString().c_str(), |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 382 | strerror(savedErrno)); |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get()); |
| 387 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 388 | if (reverse) { |
| 389 | std::mutex mutex; |
| 390 | std::condition_variable joinCv; |
| 391 | std::unique_lock<std::mutex> lock(mutex); |
| 392 | std::thread thread; |
| 393 | sp<RpcSession> thiz = sp<RpcSession>::fromExisting(this); |
| 394 | bool ownershipTransferred = false; |
| 395 | thread = std::thread([&]() { |
| 396 | std::unique_lock<std::mutex> threadLock(mutex); |
| 397 | unique_fd fd = std::move(serverFd); |
| 398 | // NOLINTNEXTLINE(performance-unnecessary-copy-initialization) |
| 399 | sp<RpcSession> session = thiz; |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 400 | session->preJoinThreadOwnership(std::move(thread)); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 401 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 402 | // only continue once we have a response or the connection fails |
| 403 | auto setupResult = session->preJoinSetup(std::move(fd)); |
| 404 | |
| 405 | ownershipTransferred = true; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 406 | threadLock.unlock(); |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 407 | joinCv.notify_one(); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 408 | // do not use & vars below |
| 409 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 410 | RpcSession::join(std::move(session), std::move(setupResult)); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 411 | }); |
| 412 | joinCv.wait(lock, [&] { return ownershipTransferred; }); |
| 413 | LOG_ALWAYS_FATAL_IF(!ownershipTransferred); |
| 414 | return true; |
| 415 | } else { |
| 416 | return addClientConnection(std::move(serverFd)); |
| 417 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 420 | ALOGE("Ran out of retries to connect to %s", addr.toString().c_str()); |
| 421 | return false; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Steven Moreland | b0dd118 | 2021-05-25 01:56:46 +0000 | [diff] [blame] | 424 | bool RpcSession::addClientConnection(unique_fd fd) { |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 425 | sp<RpcConnection> connection = sp<RpcConnection>::make(); |
| 426 | { |
| 427 | std::lock_guard<std::mutex> _l(mMutex); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 428 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 429 | // first client connection added, but setForServer not called, so |
| 430 | // initializaing for a client. |
| 431 | if (mShutdownTrigger == nullptr) { |
| 432 | mShutdownTrigger = FdTrigger::make(); |
| 433 | mEventListener = mShutdownListener = sp<WaitForShutdownListener>::make(); |
| 434 | if (mShutdownTrigger == nullptr) return false; |
| 435 | } |
| 436 | |
| 437 | connection->fd = std::move(fd); |
| 438 | connection->exclusiveTid = gettid(); |
| 439 | mClientConnections.push_back(connection); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 442 | status_t status = |
| 443 | mState->sendConnectionInit(connection->fd, sp<RpcSession>::fromExisting(this)); |
| 444 | |
| 445 | { |
| 446 | std::lock_guard<std::mutex> _l(mMutex); |
| 447 | connection->exclusiveTid = std::nullopt; |
| 448 | } |
| 449 | |
| 450 | return status == OK; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 453 | bool RpcSession::setForServer(const wp<RpcServer>& server, const wp<EventListener>& eventListener, |
| 454 | int32_t sessionId) { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 455 | LOG_ALWAYS_FATAL_IF(mForServer != nullptr); |
| 456 | LOG_ALWAYS_FATAL_IF(server == nullptr); |
| 457 | LOG_ALWAYS_FATAL_IF(mEventListener != nullptr); |
| 458 | LOG_ALWAYS_FATAL_IF(eventListener == nullptr); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 459 | LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr); |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 460 | |
| 461 | mShutdownTrigger = FdTrigger::make(); |
| 462 | if (mShutdownTrigger == nullptr) return false; |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 463 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 464 | mId = sessionId; |
| 465 | mForServer = server; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 466 | mEventListener = eventListener; |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 467 | return true; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | sp<RpcSession::RpcConnection> RpcSession::assignServerToThisThread(unique_fd fd) { |
| 471 | std::lock_guard<std::mutex> _l(mMutex); |
| 472 | sp<RpcConnection> session = sp<RpcConnection>::make(); |
| 473 | session->fd = std::move(fd); |
| 474 | session->exclusiveTid = gettid(); |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 475 | mServerConnections.push_back(session); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 476 | |
| 477 | return session; |
| 478 | } |
| 479 | |
| 480 | bool RpcSession::removeServerConnection(const sp<RpcConnection>& connection) { |
| 481 | std::lock_guard<std::mutex> _l(mMutex); |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 482 | if (auto it = std::find(mServerConnections.begin(), mServerConnections.end(), connection); |
| 483 | it != mServerConnections.end()) { |
| 484 | mServerConnections.erase(it); |
| 485 | if (mServerConnections.size() == 0) { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 486 | sp<EventListener> listener = mEventListener.promote(); |
| 487 | if (listener) { |
| 488 | listener->onSessionLockedAllServerThreadsEnded(sp<RpcSession>::fromExisting(this)); |
Steven Moreland | a86e8fe | 2021-05-26 22:52:35 +0000 | [diff] [blame] | 489 | } |
Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 490 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 491 | return true; |
| 492 | } |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | RpcSession::ExclusiveConnection::ExclusiveConnection(const sp<RpcSession>& session, |
| 497 | ConnectionUse use) |
| 498 | : mSession(session) { |
| 499 | pid_t tid = gettid(); |
| 500 | std::unique_lock<std::mutex> _l(mSession->mMutex); |
| 501 | |
| 502 | mSession->mWaitingThreads++; |
| 503 | while (true) { |
| 504 | sp<RpcConnection> exclusive; |
| 505 | sp<RpcConnection> available; |
| 506 | |
| 507 | // CHECK FOR DEDICATED CLIENT SOCKET |
| 508 | // |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 509 | // A server/looper should always use a dedicated connection if available |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 510 | findConnection(tid, &exclusive, &available, mSession->mClientConnections, |
| 511 | mSession->mClientConnectionsOffset); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 512 | |
| 513 | // WARNING: this assumes a server cannot request its client to send |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 514 | // a transaction, as mServerConnections is excluded below. |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 515 | // |
| 516 | // Imagine we have more than one thread in play, and a single thread |
| 517 | // sends a synchronous, then an asynchronous command. Imagine the |
| 518 | // asynchronous command is sent on the first client connection. Then, if |
| 519 | // we naively send a synchronous command to that same connection, the |
| 520 | // thread on the far side might be busy processing the asynchronous |
| 521 | // command. So, we move to considering the second available thread |
| 522 | // for subsequent calls. |
| 523 | if (use == ConnectionUse::CLIENT_ASYNC && (exclusive != nullptr || available != nullptr)) { |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 524 | mSession->mClientConnectionsOffset = |
| 525 | (mSession->mClientConnectionsOffset + 1) % mSession->mClientConnections.size(); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | // USE SERVING SOCKET (for nested transaction) |
| 529 | // |
| 530 | // asynchronous calls cannot be nested |
| 531 | if (use != ConnectionUse::CLIENT_ASYNC) { |
| 532 | // server connections are always assigned to a thread |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 533 | findConnection(tid, &exclusive, nullptr /*available*/, mSession->mServerConnections, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 534 | 0 /* index hint */); |
| 535 | } |
| 536 | |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 537 | // if our thread is already using a connection, prioritize using that |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 538 | if (exclusive != nullptr) { |
| 539 | mConnection = exclusive; |
| 540 | mReentrant = true; |
| 541 | break; |
| 542 | } else if (available != nullptr) { |
| 543 | mConnection = available; |
| 544 | mConnection->exclusiveTid = tid; |
| 545 | break; |
| 546 | } |
| 547 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 548 | // TODO(b/185167543): this should return an error, rather than crash a |
| 549 | // server |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 550 | // in regular binder, this would usually be a deadlock :) |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 551 | LOG_ALWAYS_FATAL_IF(mSession->mClientConnections.size() == 0, |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 552 | "Session has no client connections. This is required for an RPC server " |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame^] | 553 | "to make any non-nested (e.g. oneway or on another thread) calls. " |
| 554 | "Use: %d. Server connections: %zu", |
| 555 | static_cast<int>(use), mSession->mServerConnections.size()); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 556 | |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 557 | LOG_RPC_DETAIL("No available connections (have %zu clients and %zu servers). Waiting...", |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 558 | mSession->mClientConnections.size(), mSession->mServerConnections.size()); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 559 | mSession->mAvailableConnectionCv.wait(_l); |
| 560 | } |
| 561 | mSession->mWaitingThreads--; |
| 562 | } |
| 563 | |
| 564 | void RpcSession::ExclusiveConnection::findConnection(pid_t tid, sp<RpcConnection>* exclusive, |
| 565 | sp<RpcConnection>* available, |
| 566 | std::vector<sp<RpcConnection>>& sockets, |
| 567 | size_t socketsIndexHint) { |
| 568 | LOG_ALWAYS_FATAL_IF(sockets.size() > 0 && socketsIndexHint >= sockets.size(), |
| 569 | "Bad index %zu >= %zu", socketsIndexHint, sockets.size()); |
| 570 | |
| 571 | if (*exclusive != nullptr) return; // consistent with break below |
| 572 | |
| 573 | for (size_t i = 0; i < sockets.size(); i++) { |
| 574 | sp<RpcConnection>& socket = sockets[(i + socketsIndexHint) % sockets.size()]; |
| 575 | |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 576 | // take first available connection (intuition = caching) |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 577 | if (available && *available == nullptr && socket->exclusiveTid == std::nullopt) { |
| 578 | *available = socket; |
| 579 | continue; |
| 580 | } |
| 581 | |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 582 | // though, prefer to take connection which is already inuse by this thread |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 583 | // (nested transactions) |
| 584 | if (exclusive && socket->exclusiveTid == tid) { |
| 585 | *exclusive = socket; |
| 586 | break; // consistent with return above |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | RpcSession::ExclusiveConnection::~ExclusiveConnection() { |
Steven Moreland | 85e067b | 2021-05-26 17:43:53 +0000 | [diff] [blame] | 592 | // 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] | 593 | // is using this fd, and it retains the right to it. So, we don't give up |
| 594 | // exclusive ownership, and no thread is freed. |
| 595 | if (!mReentrant) { |
| 596 | std::unique_lock<std::mutex> _l(mSession->mMutex); |
| 597 | mConnection->exclusiveTid = std::nullopt; |
| 598 | if (mSession->mWaitingThreads > 0) { |
| 599 | _l.unlock(); |
| 600 | mSession->mAvailableConnectionCv.notify_one(); |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | } // namespace android |