Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +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 "RpcServer" |
| 18 | |
Steven Moreland | c503204 | 2021-09-30 15:40:27 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 20 | #include <poll.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 21 | #include <sys/socket.h> |
| 22 | #include <sys/un.h> |
| 23 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 24 | #include <thread> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 27 | #include <android-base/file.h> |
| 28 | #include <android-base/hex.h> |
Steven Moreland | 5802c2b | 2021-05-12 20:13:04 +0000 | [diff] [blame] | 29 | #include <android-base/scopeguard.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 30 | #include <binder/Parcel.h> |
| 31 | #include <binder/RpcServer.h> |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 32 | #include <binder/RpcTransportRaw.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 33 | #include <log/log.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 34 | |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 35 | #include "FdTrigger.h" |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 36 | #include "RpcSocketAddress.h" |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 37 | #include "RpcState.h" |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 38 | #include "RpcWireFormat.h" |
| 39 | |
| 40 | namespace android { |
| 41 | |
Steven Moreland | c503204 | 2021-09-30 15:40:27 -0700 | [diff] [blame] | 42 | constexpr size_t kSessionIdBytes = 32; |
| 43 | |
Steven Moreland | 5802c2b | 2021-05-12 20:13:04 +0000 | [diff] [blame] | 44 | using base::ScopeGuard; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 45 | using base::unique_fd; |
| 46 | |
Yifan Hong | ecf937d | 2021-08-11 17:29:28 -0700 | [diff] [blame] | 47 | RpcServer::RpcServer(std::unique_ptr<RpcTransportCtx> ctx) : mCtx(std::move(ctx)) {} |
Yifan Hong | 436f0e6 | 2021-05-19 15:25:34 -0700 | [diff] [blame] | 48 | RpcServer::~RpcServer() { |
| 49 | (void)shutdown(); |
| 50 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 51 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 52 | sp<RpcServer> RpcServer::make(std::unique_ptr<RpcTransportCtxFactory> rpcTransportCtxFactory) { |
| 53 | // Default is without TLS. |
| 54 | if (rpcTransportCtxFactory == nullptr) |
| 55 | rpcTransportCtxFactory = RpcTransportCtxFactoryRaw::make(); |
Yifan Hong | ecf937d | 2021-08-11 17:29:28 -0700 | [diff] [blame] | 56 | auto ctx = rpcTransportCtxFactory->newServerCtx(); |
| 57 | if (ctx == nullptr) return nullptr; |
| 58 | return sp<RpcServer>::make(std::move(ctx)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 61 | status_t RpcServer::setupUnixDomainServer(const char* path) { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 62 | return setupSocketServer(UnixSocketAddress(path)); |
| 63 | } |
| 64 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 65 | status_t RpcServer::setupVsockServer(unsigned int port) { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 66 | // realizing value w/ this type at compile time to avoid ubsan abort |
| 67 | constexpr unsigned int kAnyCid = VMADDR_CID_ANY; |
| 68 | |
| 69 | return setupSocketServer(VsockSocketAddress(kAnyCid, port)); |
| 70 | } |
| 71 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 72 | status_t RpcServer::setupInetServer(const char* address, unsigned int port, |
| 73 | unsigned int* assignedPort) { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 74 | if (assignedPort != nullptr) *assignedPort = 0; |
Devin Moore | f3b9c4f | 2021-08-03 15:50:13 +0000 | [diff] [blame] | 75 | auto aiStart = InetSocketAddress::getAddrInfo(address, port); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 76 | if (aiStart == nullptr) return UNKNOWN_ERROR; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 77 | for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { |
Devin Moore | f3b9c4f | 2021-08-03 15:50:13 +0000 | [diff] [blame] | 78 | InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, address, port); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 79 | if (status_t status = setupSocketServer(socketAddress); status != OK) { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 80 | continue; |
| 81 | } |
| 82 | |
| 83 | LOG_ALWAYS_FATAL_IF(socketAddress.addr()->sa_family != AF_INET, "expecting inet"); |
| 84 | sockaddr_in addr{}; |
| 85 | socklen_t len = sizeof(addr); |
| 86 | if (0 != getsockname(mServer.get(), reinterpret_cast<sockaddr*>(&addr), &len)) { |
| 87 | int savedErrno = errno; |
| 88 | ALOGE("Could not getsockname at %s: %s", socketAddress.toString().c_str(), |
| 89 | strerror(savedErrno)); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 90 | return -savedErrno; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 91 | } |
| 92 | LOG_ALWAYS_FATAL_IF(len != sizeof(addr), "Wrong socket type: len %zu vs len %zu", |
| 93 | static_cast<size_t>(len), sizeof(addr)); |
| 94 | unsigned int realPort = ntohs(addr.sin_port); |
| 95 | LOG_ALWAYS_FATAL_IF(port != 0 && realPort != port, |
| 96 | "Requesting inet server on %s but it is set up on %u.", |
| 97 | socketAddress.toString().c_str(), realPort); |
| 98 | |
| 99 | if (assignedPort != nullptr) { |
| 100 | *assignedPort = realPort; |
| 101 | } |
| 102 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 103 | return OK; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 104 | } |
Devin Moore | f3b9c4f | 2021-08-03 15:50:13 +0000 | [diff] [blame] | 105 | ALOGE("None of the socket address resolved for %s:%u can be set up as inet server.", address, |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 106 | port); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 107 | return UNKNOWN_ERROR; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 110 | void RpcServer::setMaxThreads(size_t threads) { |
| 111 | LOG_ALWAYS_FATAL_IF(threads <= 0, "RpcServer is useless without threads"); |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 112 | LOG_ALWAYS_FATAL_IF(mJoinThreadRunning, "Cannot set max threads while running"); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 113 | mMaxThreads = threads; |
| 114 | } |
| 115 | |
| 116 | size_t RpcServer::getMaxThreads() { |
| 117 | return mMaxThreads; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 120 | void RpcServer::setProtocolVersion(uint32_t version) { |
| 121 | mProtocolVersion = version; |
| 122 | } |
| 123 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 124 | void RpcServer::setRootObject(const sp<IBinder>& binder) { |
Steven Moreland | ebafe33 | 2021-04-24 00:24:35 +0000 | [diff] [blame] | 125 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 126 | mRootObjectFactory = nullptr; |
Yifan Hong | 4ffb0c7 | 2021-05-07 18:35:14 -0700 | [diff] [blame] | 127 | mRootObjectWeak = mRootObject = binder; |
| 128 | } |
| 129 | |
| 130 | void RpcServer::setRootObjectWeak(const wp<IBinder>& binder) { |
| 131 | std::lock_guard<std::mutex> _l(mLock); |
| 132 | mRootObject.clear(); |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 133 | mRootObjectFactory = nullptr; |
Yifan Hong | 4ffb0c7 | 2021-05-07 18:35:14 -0700 | [diff] [blame] | 134 | mRootObjectWeak = binder; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 135 | } |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 136 | void RpcServer::setPerSessionRootObject( |
| 137 | std::function<sp<IBinder>(const sockaddr*, socklen_t)>&& makeObject) { |
| 138 | std::lock_guard<std::mutex> _l(mLock); |
| 139 | mRootObject.clear(); |
| 140 | mRootObjectWeak.clear(); |
| 141 | mRootObjectFactory = std::move(makeObject); |
| 142 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 143 | |
| 144 | sp<IBinder> RpcServer::getRootObject() { |
Steven Moreland | ebafe33 | 2021-04-24 00:24:35 +0000 | [diff] [blame] | 145 | std::lock_guard<std::mutex> _l(mLock); |
Yifan Hong | 4ffb0c7 | 2021-05-07 18:35:14 -0700 | [diff] [blame] | 146 | bool hasWeak = mRootObjectWeak.unsafe_get(); |
| 147 | sp<IBinder> ret = mRootObjectWeak.promote(); |
| 148 | ALOGW_IF(hasWeak && ret == nullptr, "RpcServer root object is freed, returning nullptr"); |
| 149 | return ret; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 152 | std::vector<uint8_t> RpcServer::getCertificate(RpcCertificateFormat format) { |
Yifan Hong | ecf937d | 2021-08-11 17:29:28 -0700 | [diff] [blame] | 153 | std::lock_guard<std::mutex> _l(mLock); |
| 154 | return mCtx->getCertificate(format); |
| 155 | } |
| 156 | |
Yifan Hong | 326afd1 | 2021-05-19 15:24:54 -0700 | [diff] [blame] | 157 | static void joinRpcServer(sp<RpcServer>&& thiz) { |
| 158 | thiz->join(); |
| 159 | } |
| 160 | |
| 161 | void RpcServer::start() { |
Yifan Hong | 326afd1 | 2021-05-19 15:24:54 -0700 | [diff] [blame] | 162 | std::lock_guard<std::mutex> _l(mLock); |
| 163 | LOG_ALWAYS_FATAL_IF(mJoinThread.get(), "Already started!"); |
| 164 | mJoinThread = std::make_unique<std::thread>(&joinRpcServer, sp<RpcServer>::fromExisting(this)); |
| 165 | } |
| 166 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 167 | void RpcServer::join() { |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 168 | |
| 169 | { |
| 170 | std::lock_guard<std::mutex> _l(mLock); |
| 171 | LOG_ALWAYS_FATAL_IF(!mServer.ok(), "RpcServer must be setup to join."); |
| 172 | LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr, "Already joined"); |
| 173 | mJoinThreadRunning = true; |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 174 | mShutdownTrigger = FdTrigger::make(); |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 175 | LOG_ALWAYS_FATAL_IF(mShutdownTrigger == nullptr, "Cannot create join signaler"); |
Steven Moreland | d539fbf | 2021-05-05 23:40:25 +0000 | [diff] [blame] | 176 | } |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 177 | |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 178 | status_t status; |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 179 | while ((status = mShutdownTrigger->triggerablePoll(mServer, POLLIN)) == OK) { |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 180 | sockaddr_storage addr; |
| 181 | socklen_t addrLen = sizeof(addr); |
| 182 | |
| 183 | unique_fd clientFd( |
| 184 | TEMP_FAILURE_RETRY(accept4(mServer.get(), reinterpret_cast<sockaddr*>(&addr), |
| 185 | &addrLen, SOCK_CLOEXEC | SOCK_NONBLOCK))); |
| 186 | |
| 187 | LOG_ALWAYS_FATAL_IF(addrLen > static_cast<socklen_t>(sizeof(addr)), "Truncated address"); |
Steven Moreland | 410325a | 2021-06-02 18:37:42 +0000 | [diff] [blame] | 188 | |
| 189 | if (clientFd < 0) { |
| 190 | ALOGE("Could not accept4 socket: %s", strerror(errno)); |
| 191 | continue; |
| 192 | } |
| 193 | LOG_RPC_DETAIL("accept4 on fd %d yields fd %d", mServer.get(), clientFd.get()); |
| 194 | |
| 195 | { |
| 196 | std::lock_guard<std::mutex> _l(mLock); |
| 197 | std::thread thread = |
| 198 | std::thread(&RpcServer::establishConnection, sp<RpcServer>::fromExisting(this), |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 199 | std::move(clientFd), addr, addrLen); |
Steven Moreland | 410325a | 2021-06-02 18:37:42 +0000 | [diff] [blame] | 200 | mConnectingThreads[thread.get_id()] = std::move(thread); |
| 201 | } |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 202 | } |
Steven Moreland | 2b4f380 | 2021-05-22 01:46:27 +0000 | [diff] [blame] | 203 | LOG_RPC_DETAIL("RpcServer::join exiting with %s", statusToString(status).c_str()); |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 204 | |
| 205 | { |
| 206 | std::lock_guard<std::mutex> _l(mLock); |
| 207 | mJoinThreadRunning = false; |
| 208 | } |
| 209 | mShutdownCv.notify_all(); |
Steven Moreland | d539fbf | 2021-05-05 23:40:25 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 212 | bool RpcServer::shutdown() { |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 213 | std::unique_lock<std::mutex> _l(mLock); |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 214 | if (mShutdownTrigger == nullptr) { |
Steven Moreland | 1c943ec | 2021-07-13 23:57:56 +0000 | [diff] [blame] | 215 | LOG_RPC_DETAIL("Cannot shutdown. No shutdown trigger installed (already shutdown?)"); |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 216 | return false; |
| 217 | } |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 218 | |
| 219 | mShutdownTrigger->trigger(); |
Steven Moreland | ab3f442 | 2021-09-27 18:38:20 -0700 | [diff] [blame] | 220 | |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 221 | for (auto& [id, session] : mSessions) { |
| 222 | (void)id; |
Steven Moreland | ab3f442 | 2021-09-27 18:38:20 -0700 | [diff] [blame] | 223 | // server lock is a more general lock |
| 224 | std::lock_guard<std::mutex> _lSession(session->mMutex); |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 225 | session->mShutdownTrigger->trigger(); |
| 226 | } |
| 227 | |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 228 | while (mJoinThreadRunning || !mConnectingThreads.empty() || !mSessions.empty()) { |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 229 | if (std::cv_status::timeout == mShutdownCv.wait_for(_l, std::chrono::seconds(1))) { |
| 230 | ALOGE("Waiting for RpcServer to shut down (1s w/o progress). Join thread running: %d, " |
| 231 | "Connecting threads: " |
| 232 | "%zu, Sessions: %zu. Is your server deadlocked?", |
| 233 | mJoinThreadRunning, mConnectingThreads.size(), mSessions.size()); |
| 234 | } |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 235 | } |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 236 | |
Yifan Hong | 326afd1 | 2021-05-19 15:24:54 -0700 | [diff] [blame] | 237 | // At this point, we know join() is about to exit, but the thread that calls |
| 238 | // join() may not have exited yet. |
| 239 | // If RpcServer owns the join thread (aka start() is called), make sure the thread exits; |
| 240 | // otherwise ~thread() may call std::terminate(), which may crash the process. |
| 241 | // If RpcServer does not own the join thread (aka join() is called directly), |
| 242 | // then the owner of RpcServer is responsible for cleaning up that thread. |
| 243 | if (mJoinThread.get()) { |
| 244 | mJoinThread->join(); |
| 245 | mJoinThread.reset(); |
| 246 | } |
| 247 | |
Steven Moreland | 1c943ec | 2021-07-13 23:57:56 +0000 | [diff] [blame] | 248 | LOG_RPC_DETAIL("Finished waiting on shutdown."); |
| 249 | |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 250 | mShutdownTrigger = nullptr; |
| 251 | return true; |
| 252 | } |
| 253 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 254 | std::vector<sp<RpcSession>> RpcServer::listSessions() { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 255 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 256 | std::vector<sp<RpcSession>> sessions; |
| 257 | for (auto& [id, session] : mSessions) { |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 258 | (void)id; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 259 | sessions.push_back(session); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 260 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 261 | return sessions; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Steven Moreland | d539fbf | 2021-05-05 23:40:25 +0000 | [diff] [blame] | 264 | size_t RpcServer::numUninitializedSessions() { |
| 265 | std::lock_guard<std::mutex> _l(mLock); |
| 266 | return mConnectingThreads.size(); |
| 267 | } |
| 268 | |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 269 | void RpcServer::establishConnection(sp<RpcServer>&& server, base::unique_fd clientFd, |
| 270 | const sockaddr_storage addr, socklen_t addrLen) { |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 271 | // mShutdownTrigger can only be cleared once connection threads have joined. |
| 272 | // It must be set before this thread is started |
| 273 | LOG_ALWAYS_FATAL_IF(server->mShutdownTrigger == nullptr); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 274 | LOG_ALWAYS_FATAL_IF(server->mCtx == nullptr); |
| 275 | |
| 276 | status_t status = OK; |
| 277 | |
| 278 | int clientFdForLog = clientFd.get(); |
Yifan Hong | f6d4229 | 2021-08-05 23:43:05 -0700 | [diff] [blame] | 279 | auto client = server->mCtx->newTransport(std::move(clientFd), server->mShutdownTrigger.get()); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 280 | if (client == nullptr) { |
| 281 | ALOGE("Dropping accept4()-ed socket because sslAccept fails"); |
| 282 | status = DEAD_OBJECT; |
| 283 | // still need to cleanup before we can return |
| 284 | } else { |
| 285 | LOG_RPC_DETAIL("Created RpcTransport %p for client fd %d", client.get(), clientFdForLog); |
| 286 | } |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 287 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 288 | RpcConnectionHeader header; |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 289 | if (status == OK) { |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 290 | iovec iov{&header, sizeof(header)}; |
| 291 | status = client->interruptableReadFully(server->mShutdownTrigger.get(), &iov, 1, {}); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 292 | if (status != OK) { |
| 293 | ALOGE("Failed to read ID for client connecting to RPC server: %s", |
| 294 | statusToString(status).c_str()); |
| 295 | // still need to cleanup before we can return |
| 296 | } |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 297 | } |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 298 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 299 | std::vector<uint8_t> sessionId; |
| 300 | if (status == OK) { |
| 301 | if (header.sessionIdSize > 0) { |
Steven Moreland | c503204 | 2021-09-30 15:40:27 -0700 | [diff] [blame] | 302 | if (header.sessionIdSize == kSessionIdBytes) { |
| 303 | sessionId.resize(header.sessionIdSize); |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 304 | iovec iov{sessionId.data(), sessionId.size()}; |
| 305 | status = |
| 306 | client->interruptableReadFully(server->mShutdownTrigger.get(), &iov, 1, {}); |
Steven Moreland | c503204 | 2021-09-30 15:40:27 -0700 | [diff] [blame] | 307 | if (status != OK) { |
| 308 | ALOGE("Failed to read session ID for client connecting to RPC server: %s", |
| 309 | statusToString(status).c_str()); |
| 310 | // still need to cleanup before we can return |
| 311 | } |
| 312 | } else { |
| 313 | ALOGE("Malformed session ID. Expecting session ID of size %zu but got %" PRIu16, |
| 314 | kSessionIdBytes, header.sessionIdSize); |
| 315 | status = BAD_VALUE; |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 320 | bool incoming = false; |
| 321 | uint32_t protocolVersion = 0; |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 322 | bool requestingNewSession = false; |
| 323 | |
| 324 | if (status == OK) { |
| 325 | incoming = header.options & RPC_CONNECTION_OPTION_INCOMING; |
| 326 | protocolVersion = std::min(header.version, |
| 327 | server->mProtocolVersion.value_or(RPC_WIRE_PROTOCOL_VERSION)); |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 328 | requestingNewSession = sessionId.empty(); |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 329 | |
| 330 | if (requestingNewSession) { |
| 331 | RpcNewSessionResponse response{ |
| 332 | .version = protocolVersion, |
| 333 | }; |
| 334 | |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 335 | iovec iov{&response, sizeof(response)}; |
| 336 | status = client->interruptableWriteFully(server->mShutdownTrigger.get(), &iov, 1, {}); |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 337 | if (status != OK) { |
| 338 | ALOGE("Failed to send new session response: %s", statusToString(status).c_str()); |
| 339 | // still need to cleanup before we can return |
| 340 | } |
| 341 | } |
| 342 | } |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 343 | |
| 344 | std::thread thisThread; |
| 345 | sp<RpcSession> session; |
| 346 | { |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 347 | std::unique_lock<std::mutex> _l(server->mLock); |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 348 | |
Yifan Hong | b300550 | 2021-05-19 15:37:00 -0700 | [diff] [blame] | 349 | auto threadId = server->mConnectingThreads.find(std::this_thread::get_id()); |
| 350 | LOG_ALWAYS_FATAL_IF(threadId == server->mConnectingThreads.end(), |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 351 | "Must establish connection on owned thread"); |
| 352 | thisThread = std::move(threadId->second); |
Steven Moreland | adc5dca | 2021-05-25 02:06:03 +0000 | [diff] [blame] | 353 | ScopeGuard detachGuard = [&]() { |
| 354 | thisThread.detach(); |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 355 | _l.unlock(); |
| 356 | server->mShutdownCv.notify_all(); |
| 357 | }; |
Steven Moreland | adc5dca | 2021-05-25 02:06:03 +0000 | [diff] [blame] | 358 | server->mConnectingThreads.erase(threadId); |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame] | 359 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 360 | if (status != OK || server->mShutdownTrigger->isTriggered()) { |
Steven Moreland | 5802c2b | 2021-05-12 20:13:04 +0000 | [diff] [blame] | 361 | return; |
| 362 | } |
| 363 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 364 | if (requestingNewSession) { |
Steven Moreland | 1b30429 | 2021-07-15 22:59:34 +0000 | [diff] [blame] | 365 | if (incoming) { |
| 366 | ALOGE("Cannot create a new session with an incoming connection, would leak"); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 367 | return; |
| 368 | } |
| 369 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 370 | // Uniquely identify session at the application layer. Even if a |
| 371 | // client/server use the same certificates, if they create multiple |
| 372 | // sessions, we still want to distinguish between them. |
Steven Moreland | c503204 | 2021-09-30 15:40:27 -0700 | [diff] [blame] | 373 | sessionId.resize(kSessionIdBytes); |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 374 | size_t tries = 0; |
| 375 | do { |
| 376 | // don't block if there is some entropy issue |
| 377 | if (tries++ > 5) { |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 378 | ALOGE("Cannot find new address: %s", |
| 379 | base::HexString(sessionId.data(), sessionId.size()).c_str()); |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 380 | return; |
| 381 | } |
| 382 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 383 | base::unique_fd fd(TEMP_FAILURE_RETRY( |
| 384 | open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW))); |
| 385 | if (!base::ReadFully(fd, sessionId.data(), sessionId.size())) { |
| 386 | ALOGE("Could not read from /dev/urandom to create session ID"); |
| 387 | return; |
| 388 | } |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 389 | } while (server->mSessions.end() != server->mSessions.find(sessionId)); |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 390 | |
| 391 | session = RpcSession::make(); |
Yifan Hong | 1042306 | 2021-10-08 16:26:32 -0700 | [diff] [blame] | 392 | session->setMaxIncomingThreads(server->mMaxThreads); |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 393 | if (!session->setProtocolVersion(protocolVersion)) return; |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 394 | |
| 395 | // if null, falls back to server root |
| 396 | sp<IBinder> sessionSpecificRoot; |
| 397 | if (server->mRootObjectFactory != nullptr) { |
| 398 | sessionSpecificRoot = |
| 399 | server->mRootObjectFactory(reinterpret_cast<const sockaddr*>(&addr), |
| 400 | addrLen); |
| 401 | if (sessionSpecificRoot == nullptr) { |
| 402 | ALOGE("Warning: server returned null from root object factory"); |
| 403 | } |
| 404 | } |
| 405 | |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 406 | if (!session->setForServer(server, |
| 407 | sp<RpcServer::EventListener>::fromExisting( |
| 408 | static_cast<RpcServer::EventListener*>( |
| 409 | server.get())), |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 410 | sessionId, sessionSpecificRoot)) { |
Steven Moreland | a8b4429 | 2021-06-08 01:27:53 +0000 | [diff] [blame] | 411 | ALOGE("Failed to attach server to session"); |
| 412 | return; |
| 413 | } |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 414 | |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 415 | server->mSessions[sessionId] = session; |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 416 | } else { |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 417 | auto it = server->mSessions.find(sessionId); |
Yifan Hong | b300550 | 2021-05-19 15:37:00 -0700 | [diff] [blame] | 418 | if (it == server->mSessions.end()) { |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 419 | ALOGE("Cannot add thread, no record of session with ID %s", |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 420 | base::HexString(sessionId.data(), sessionId.size()).c_str()); |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 421 | return; |
| 422 | } |
| 423 | session = it->second; |
| 424 | } |
Steven Moreland | 5802c2b | 2021-05-12 20:13:04 +0000 | [diff] [blame] | 425 | |
Steven Moreland | 1b30429 | 2021-07-15 22:59:34 +0000 | [diff] [blame] | 426 | if (incoming) { |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 427 | LOG_ALWAYS_FATAL_IF(OK != session->addOutgoingConnection(std::move(client), true), |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 428 | "server state must already be initialized"); |
| 429 | return; |
| 430 | } |
| 431 | |
Steven Moreland | 5802c2b | 2021-05-12 20:13:04 +0000 | [diff] [blame] | 432 | detachGuard.Disable(); |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 433 | session->preJoinThreadOwnership(std::move(thisThread)); |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 436 | auto setupResult = session->preJoinSetup(std::move(client)); |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 437 | |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 438 | // avoid strong cycle |
| 439 | server = nullptr; |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 440 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 441 | RpcSession::join(std::move(session), std::move(setupResult)); |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 444 | status_t RpcServer::setupSocketServer(const RpcSocketAddress& addr) { |
Steven Moreland | 704fc1a | 2021-05-04 23:13:14 +0000 | [diff] [blame] | 445 | LOG_RPC_DETAIL("Setting up socket server %s", addr.toString().c_str()); |
Yifan Hong | 0eb5a67 | 2021-05-12 18:00:25 -0700 | [diff] [blame] | 446 | LOG_ALWAYS_FATAL_IF(hasServer(), "Each RpcServer can only have one server."); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 447 | |
Yifan Hong | b675ffe | 2021-08-05 16:37:17 -0700 | [diff] [blame] | 448 | unique_fd serverFd(TEMP_FAILURE_RETRY( |
| 449 | socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0))); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 450 | if (serverFd == -1) { |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 451 | int savedErrno = errno; |
| 452 | ALOGE("Could not create socket: %s", strerror(savedErrno)); |
| 453 | return -savedErrno; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | if (0 != TEMP_FAILURE_RETRY(bind(serverFd.get(), addr.addr(), addr.addrSize()))) { |
| 457 | int savedErrno = errno; |
| 458 | ALOGE("Could not bind socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 459 | return -savedErrno; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Yifan Hong | e96a1f1 | 2021-07-13 16:08:28 -0700 | [diff] [blame] | 462 | // Right now, we create all threads at once, making accept4 slow. To avoid hanging the client, |
| 463 | // the backlog is increased to a large number. |
| 464 | // TODO(b/189955605): Once we create threads dynamically & lazily, the backlog can be reduced |
| 465 | // to 1. |
| 466 | if (0 != TEMP_FAILURE_RETRY(listen(serverFd.get(), 50 /*backlog*/))) { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 467 | int savedErrno = errno; |
| 468 | ALOGE("Could not listen socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 469 | return -savedErrno; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Steven Moreland | 704fc1a | 2021-05-04 23:13:14 +0000 | [diff] [blame] | 472 | LOG_RPC_DETAIL("Successfully setup socket server %s", addr.toString().c_str()); |
| 473 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 474 | if (status_t status = setupExternalServer(std::move(serverFd)); status != OK) { |
Yifan Hong | c276f8d | 2021-05-13 17:13:44 -0700 | [diff] [blame] | 475 | ALOGE("Another thread has set up server while calling setupSocketServer. Race?"); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 476 | return status; |
Yifan Hong | c276f8d | 2021-05-13 17:13:44 -0700 | [diff] [blame] | 477 | } |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 478 | return OK; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Steven Moreland | dd67b94 | 2021-07-23 17:15:41 -0700 | [diff] [blame] | 481 | void RpcServer::onSessionAllIncomingThreadsEnded(const sp<RpcSession>& session) { |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 482 | const std::vector<uint8_t>& id = session->mId; |
| 483 | LOG_ALWAYS_FATAL_IF(id.empty(), "Server sessions must be initialized with ID"); |
| 484 | LOG_RPC_DETAIL("Dropping session with address %s", |
| 485 | base::HexString(id.data(), id.size()).c_str()); |
Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 486 | |
| 487 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 488 | auto it = mSessions.find(id); |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 489 | LOG_ALWAYS_FATAL_IF(it == mSessions.end(), "Bad state, unknown session id %s", |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 490 | base::HexString(id.data(), id.size()).c_str()); |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 491 | LOG_ALWAYS_FATAL_IF(it->second != session, "Bad state, session has id mismatch %s", |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 492 | base::HexString(id.data(), id.size()).c_str()); |
Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 493 | (void)mSessions.erase(it); |
| 494 | } |
| 495 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 496 | void RpcServer::onSessionIncomingThreadEnded() { |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 497 | mShutdownCv.notify_all(); |
| 498 | } |
| 499 | |
Yifan Hong | 0eb5a67 | 2021-05-12 18:00:25 -0700 | [diff] [blame] | 500 | bool RpcServer::hasServer() { |
| 501 | std::lock_guard<std::mutex> _l(mLock); |
| 502 | return mServer.ok(); |
| 503 | } |
| 504 | |
Yifan Hong | 00aeb76 | 2021-05-12 17:07:36 -0700 | [diff] [blame] | 505 | unique_fd RpcServer::releaseServer() { |
Yifan Hong | 00aeb76 | 2021-05-12 17:07:36 -0700 | [diff] [blame] | 506 | std::lock_guard<std::mutex> _l(mLock); |
| 507 | return std::move(mServer); |
| 508 | } |
| 509 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 510 | status_t RpcServer::setupExternalServer(base::unique_fd serverFd) { |
Yifan Hong | 00aeb76 | 2021-05-12 17:07:36 -0700 | [diff] [blame] | 511 | std::lock_guard<std::mutex> _l(mLock); |
| 512 | if (mServer.ok()) { |
| 513 | ALOGE("Each RpcServer can only have one server."); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 514 | return INVALID_OPERATION; |
Yifan Hong | 00aeb76 | 2021-05-12 17:07:36 -0700 | [diff] [blame] | 515 | } |
| 516 | mServer = std::move(serverFd); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 517 | return OK; |
Yifan Hong | 00aeb76 | 2021-05-12 17:07:36 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 520 | } // namespace android |