blob: d9e926a9d5c4a641903d2f2428f217b88de85487 [file] [log] [blame]
Steven Moreland5553ac42020-11-11 02:14:45 +00001/*
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 Morelandc5032042021-09-30 15:40:27 -070019#include <inttypes.h>
Keith Mok442907b2023-10-11 20:36:18 +000020#include <netinet/tcp.h>
Steven Moreland798e0d12021-07-14 23:19:25 +000021#include <poll.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000022#include <sys/socket.h>
23#include <sys/un.h>
24
Steven Morelandf137de92021-04-24 01:54:26 +000025#include <thread>
Steven Moreland5553ac42020-11-11 02:14:45 +000026#include <vector>
27
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +000028#include <binder/Functional.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000029#include <binder/Parcel.h>
30#include <binder/RpcServer.h>
Yifan Hong702115c2021-06-24 15:39:18 -070031#include <binder/RpcTransportRaw.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000032#include <log/log.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000033
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000034#include "BuildFlags.h"
Yifan Hong8c950422021-08-05 17:13:55 -070035#include "FdTrigger.h"
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000036#include "OS.h"
Steven Moreland611d15f2021-05-01 01:28:27 +000037#include "RpcSocketAddress.h"
Yifan Hong1a235852021-05-13 16:07:47 -070038#include "RpcState.h"
David Brazdil21c887c2022-09-23 12:25:18 +010039#include "RpcTransportUtils.h"
Steven Moreland5553ac42020-11-11 02:14:45 +000040#include "RpcWireFormat.h"
Andrei Homescuc24c8792022-04-19 00:24:51 +000041#include "Utils.h"
Steven Moreland5553ac42020-11-11 02:14:45 +000042
43namespace android {
44
Steven Morelandc5032042021-09-30 15:40:27 -070045constexpr size_t kSessionIdBytes = 32;
46
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +000047using namespace android::binder::impl;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070048using android::binder::borrowed_fd;
49using android::binder::unique_fd;
Steven Moreland611d15f2021-05-01 01:28:27 +000050
Yifan Hongecf937d2021-08-11 17:29:28 -070051RpcServer::RpcServer(std::unique_ptr<RpcTransportCtx> ctx) : mCtx(std::move(ctx)) {}
Yifan Hong436f0e62021-05-19 15:25:34 -070052RpcServer::~RpcServer() {
David Brazdil4766a1f2022-12-19 21:58:25 +000053 RpcMutexUniqueLock _l(mLock);
54 LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr, "Must call shutdown() before destructor");
Yifan Hong436f0e62021-05-19 15:25:34 -070055}
Steven Moreland5553ac42020-11-11 02:14:45 +000056
Yifan Hong702115c2021-06-24 15:39:18 -070057sp<RpcServer> RpcServer::make(std::unique_ptr<RpcTransportCtxFactory> rpcTransportCtxFactory) {
58 // Default is without TLS.
59 if (rpcTransportCtxFactory == nullptr)
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +000060 rpcTransportCtxFactory = binder::os::makeDefaultRpcTransportCtxFactory();
Yifan Hongecf937d2021-08-11 17:29:28 -070061 auto ctx = rpcTransportCtxFactory->newServerCtx();
62 if (ctx == nullptr) return nullptr;
63 return sp<RpcServer>::make(std::move(ctx));
Steven Moreland5553ac42020-11-11 02:14:45 +000064}
65
David Brazdil21c887c2022-09-23 12:25:18 +010066status_t RpcServer::setupUnixDomainSocketBootstrapServer(unique_fd bootstrapFd) {
67 return setupExternalServer(std::move(bootstrapFd), &RpcServer::recvmsgSocketConnection);
68}
69
Steven Moreland2372f9d2021-08-05 15:42:01 -070070status_t RpcServer::setupUnixDomainServer(const char* path) {
Steven Moreland611d15f2021-05-01 01:28:27 +000071 return setupSocketServer(UnixSocketAddress(path));
72}
73
David Brazdila47dfda2022-11-22 22:52:19 +000074status_t RpcServer::setupVsockServer(unsigned int bindCid, unsigned int port) {
75 return setupSocketServer(VsockSocketAddress(bindCid, port));
Steven Moreland611d15f2021-05-01 01:28:27 +000076}
77
Steven Moreland2372f9d2021-08-05 15:42:01 -070078status_t RpcServer::setupInetServer(const char* address, unsigned int port,
79 unsigned int* assignedPort) {
Steven Moreland611d15f2021-05-01 01:28:27 +000080 if (assignedPort != nullptr) *assignedPort = 0;
Devin Mooref3b9c4f2021-08-03 15:50:13 +000081 auto aiStart = InetSocketAddress::getAddrInfo(address, port);
Steven Moreland2372f9d2021-08-05 15:42:01 -070082 if (aiStart == nullptr) return UNKNOWN_ERROR;
Steven Moreland611d15f2021-05-01 01:28:27 +000083 for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) {
zijunzhaoa2013362023-05-11 21:27:19 +000084 if (ai->ai_addr == nullptr) continue;
Devin Mooref3b9c4f2021-08-03 15:50:13 +000085 InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, address, port);
Steven Moreland2372f9d2021-08-05 15:42:01 -070086 if (status_t status = setupSocketServer(socketAddress); status != OK) {
Steven Moreland611d15f2021-05-01 01:28:27 +000087 continue;
88 }
89
90 LOG_ALWAYS_FATAL_IF(socketAddress.addr()->sa_family != AF_INET, "expecting inet");
91 sockaddr_in addr{};
92 socklen_t len = sizeof(addr);
Pawan49d74cb2022-08-03 21:19:11 +000093 if (0 != getsockname(mServer.fd.get(), reinterpret_cast<sockaddr*>(&addr), &len)) {
Steven Moreland611d15f2021-05-01 01:28:27 +000094 int savedErrno = errno;
95 ALOGE("Could not getsockname at %s: %s", socketAddress.toString().c_str(),
96 strerror(savedErrno));
Steven Moreland2372f9d2021-08-05 15:42:01 -070097 return -savedErrno;
Steven Moreland611d15f2021-05-01 01:28:27 +000098 }
99 LOG_ALWAYS_FATAL_IF(len != sizeof(addr), "Wrong socket type: len %zu vs len %zu",
100 static_cast<size_t>(len), sizeof(addr));
101 unsigned int realPort = ntohs(addr.sin_port);
102 LOG_ALWAYS_FATAL_IF(port != 0 && realPort != port,
103 "Requesting inet server on %s but it is set up on %u.",
104 socketAddress.toString().c_str(), realPort);
105
106 if (assignedPort != nullptr) {
107 *assignedPort = realPort;
108 }
109
Steven Moreland2372f9d2021-08-05 15:42:01 -0700110 return OK;
Steven Moreland611d15f2021-05-01 01:28:27 +0000111 }
Devin Mooref3b9c4f2021-08-03 15:50:13 +0000112 ALOGE("None of the socket address resolved for %s:%u can be set up as inet server.", address,
Steven Moreland611d15f2021-05-01 01:28:27 +0000113 port);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700114 return UNKNOWN_ERROR;
Steven Moreland611d15f2021-05-01 01:28:27 +0000115}
116
Steven Morelandf137de92021-04-24 01:54:26 +0000117void RpcServer::setMaxThreads(size_t threads) {
118 LOG_ALWAYS_FATAL_IF(threads <= 0, "RpcServer is useless without threads");
Yifan Hong1a235852021-05-13 16:07:47 -0700119 LOG_ALWAYS_FATAL_IF(mJoinThreadRunning, "Cannot set max threads while running");
Steven Morelandf137de92021-04-24 01:54:26 +0000120 mMaxThreads = threads;
121}
122
123size_t RpcServer::getMaxThreads() {
124 return mMaxThreads;
Steven Moreland5553ac42020-11-11 02:14:45 +0000125}
126
Steven Morelandca3f6382023-05-11 23:23:26 +0000127bool RpcServer::setProtocolVersion(uint32_t version) {
128 if (!RpcState::validateProtocolVersion(version)) {
129 return false;
130 }
131
Steven Morelandbf57bce2021-07-26 15:26:12 -0700132 mProtocolVersion = version;
Steven Morelandca3f6382023-05-11 23:23:26 +0000133 return true;
Steven Morelandbf57bce2021-07-26 15:26:12 -0700134}
135
Frederick Mayle69a0c992022-05-26 20:38:39 +0000136void RpcServer::setSupportedFileDescriptorTransportModes(
137 const std::vector<RpcSession::FileDescriptorTransportMode>& modes) {
138 mSupportedFileDescriptorTransportModes.reset();
139 for (RpcSession::FileDescriptorTransportMode mode : modes) {
140 mSupportedFileDescriptorTransportModes.set(static_cast<size_t>(mode));
141 }
142}
143
Steven Moreland5553ac42020-11-11 02:14:45 +0000144void RpcServer::setRootObject(const sp<IBinder>& binder) {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000145 RpcMutexLockGuard _l(mLock);
Steven Moreland51c44a92021-10-14 16:50:35 -0700146 mRootObjectFactory = nullptr;
Yifan Hong4ffb0c72021-05-07 18:35:14 -0700147 mRootObjectWeak = mRootObject = binder;
148}
149
150void RpcServer::setRootObjectWeak(const wp<IBinder>& binder) {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000151 RpcMutexLockGuard _l(mLock);
Yifan Hong4ffb0c72021-05-07 18:35:14 -0700152 mRootObject.clear();
Steven Moreland51c44a92021-10-14 16:50:35 -0700153 mRootObjectFactory = nullptr;
Yifan Hong4ffb0c72021-05-07 18:35:14 -0700154 mRootObjectWeak = binder;
Steven Moreland5553ac42020-11-11 02:14:45 +0000155}
Steven Moreland51c44a92021-10-14 16:50:35 -0700156void RpcServer::setPerSessionRootObject(
Steven Morelanddfb92182023-05-24 17:48:59 +0000157 std::function<sp<IBinder>(wp<RpcSession> session, const void*, size_t)>&& makeObject) {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000158 RpcMutexLockGuard _l(mLock);
Steven Moreland51c44a92021-10-14 16:50:35 -0700159 mRootObject.clear();
160 mRootObjectWeak.clear();
161 mRootObjectFactory = std::move(makeObject);
162}
Steven Moreland5553ac42020-11-11 02:14:45 +0000163
David Brazdila47dfda2022-11-22 22:52:19 +0000164void RpcServer::setConnectionFilter(std::function<bool(const void*, size_t)>&& filter) {
165 RpcMutexLockGuard _l(mLock);
166 LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr, "Already joined");
167 mConnectionFilter = std::move(filter);
168}
169
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700170void RpcServer::setServerSocketModifier(std::function<void(borrowed_fd)>&& modifier) {
Tomasz Wasilczyk7c8dd482023-03-23 15:30:03 -0700171 RpcMutexLockGuard _l(mLock);
Tomasz Wasilczykbfb13a82023-11-14 11:33:10 -0800172 LOG_ALWAYS_FATAL_IF(mServer.fd.ok(), "Already started");
Tomasz Wasilczyk7c8dd482023-03-23 15:30:03 -0700173 mServerSocketModifier = std::move(modifier);
174}
175
Steven Moreland5553ac42020-11-11 02:14:45 +0000176sp<IBinder> RpcServer::getRootObject() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000177 RpcMutexLockGuard _l(mLock);
Yifan Hong4ffb0c72021-05-07 18:35:14 -0700178 bool hasWeak = mRootObjectWeak.unsafe_get();
179 sp<IBinder> ret = mRootObjectWeak.promote();
180 ALOGW_IF(hasWeak && ret == nullptr, "RpcServer root object is freed, returning nullptr");
181 return ret;
Steven Moreland5553ac42020-11-11 02:14:45 +0000182}
183
Yifan Hong9734cfc2021-09-13 16:14:09 -0700184std::vector<uint8_t> RpcServer::getCertificate(RpcCertificateFormat format) {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000185 RpcMutexLockGuard _l(mLock);
Yifan Hongecf937d2021-08-11 17:29:28 -0700186 return mCtx->getCertificate(format);
187}
188
Yifan Hong326afd12021-05-19 15:24:54 -0700189static void joinRpcServer(sp<RpcServer>&& thiz) {
190 thiz->join();
191}
192
193void RpcServer::start() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000194 RpcMutexLockGuard _l(mLock);
Yifan Hong326afd12021-05-19 15:24:54 -0700195 LOG_ALWAYS_FATAL_IF(mJoinThread.get(), "Already started!");
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000196 mJoinThread =
197 std::make_unique<RpcMaybeThread>(&joinRpcServer, sp<RpcServer>::fromExisting(this));
198 rpcJoinIfSingleThreaded(*mJoinThread);
Yifan Hong326afd12021-05-19 15:24:54 -0700199}
200
David Brazdil21c887c2022-09-23 12:25:18 +0100201status_t RpcServer::acceptSocketConnection(const RpcServer& server, RpcTransportFd* out) {
202 RpcTransportFd clientSocket(unique_fd(TEMP_FAILURE_RETRY(
203 accept4(server.mServer.fd.get(), nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK))));
Tomasz Wasilczykbfb13a82023-11-14 11:33:10 -0800204 if (!clientSocket.fd.ok()) {
David Brazdil21c887c2022-09-23 12:25:18 +0100205 int savedErrno = errno;
206 ALOGE("Could not accept4 socket: %s", strerror(savedErrno));
207 return -savedErrno;
208 }
209
210 *out = std::move(clientSocket);
211 return OK;
212}
213
214status_t RpcServer::recvmsgSocketConnection(const RpcServer& server, RpcTransportFd* out) {
215 int zero = 0;
216 iovec iov{&zero, sizeof(zero)};
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700217 std::vector<std::variant<unique_fd, borrowed_fd>> fds;
David Brazdil21c887c2022-09-23 12:25:18 +0100218
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +0000219 ssize_t num_bytes = binder::os::receiveMessageFromSocket(server.mServer, &iov, 1, &fds);
David Brazdil8a741902022-12-17 21:41:04 +0000220 if (num_bytes < 0) {
David Brazdil21c887c2022-09-23 12:25:18 +0100221 int savedErrno = errno;
222 ALOGE("Failed recvmsg: %s", strerror(savedErrno));
223 return -savedErrno;
224 }
David Brazdil8a741902022-12-17 21:41:04 +0000225 if (num_bytes == 0) {
226 return DEAD_OBJECT;
227 }
David Brazdil21c887c2022-09-23 12:25:18 +0100228 if (fds.size() != 1) {
229 ALOGE("Expected exactly one fd from recvmsg, got %zu", fds.size());
230 return -EINVAL;
231 }
232
233 unique_fd fd(std::move(std::get<unique_fd>(fds.back())));
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -0700234 if (status_t res = binder::os::setNonBlocking(fd); res != OK) return res;
David Brazdil21c887c2022-09-23 12:25:18 +0100235
236 *out = RpcTransportFd(std::move(fd));
237 return OK;
238}
239
Steven Moreland611d15f2021-05-01 01:28:27 +0000240void RpcServer::join() {
Yifan Hong1a235852021-05-13 16:07:47 -0700241
242 {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000243 RpcMutexLockGuard _l(mLock);
Pawan49d74cb2022-08-03 21:19:11 +0000244 LOG_ALWAYS_FATAL_IF(!mServer.fd.ok(), "RpcServer must be setup to join.");
David Brazdil21c887c2022-09-23 12:25:18 +0100245 LOG_ALWAYS_FATAL_IF(mAcceptFn == nullptr, "RpcServer must have an accept() function");
Yifan Hong1a235852021-05-13 16:07:47 -0700246 LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr, "Already joined");
247 mJoinThreadRunning = true;
Yifan Hong8c950422021-08-05 17:13:55 -0700248 mShutdownTrigger = FdTrigger::make();
Yifan Hong1a235852021-05-13 16:07:47 -0700249 LOG_ALWAYS_FATAL_IF(mShutdownTrigger == nullptr, "Cannot create join signaler");
Steven Morelandd539fbf2021-05-05 23:40:25 +0000250 }
Yifan Hong1a235852021-05-13 16:07:47 -0700251
Steven Moreland2b4f3802021-05-22 01:46:27 +0000252 status_t status;
Steven Moreland798e0d12021-07-14 23:19:25 +0000253 while ((status = mShutdownTrigger->triggerablePoll(mServer, POLLIN)) == OK) {
Andrei Homescu86124ca2022-04-21 22:22:48 +0000254 std::array<uint8_t, kRpcAddressSize> addr;
255 static_assert(addr.size() >= sizeof(sockaddr_storage), "kRpcAddressSize is too small");
Andrei Homescu86124ca2022-04-21 22:22:48 +0000256 socklen_t addrLen = addr.size();
Steven Moreland51c44a92021-10-14 16:50:35 -0700257
David Brazdil21c887c2022-09-23 12:25:18 +0100258 RpcTransportFd clientSocket;
David Brazdil8a741902022-12-17 21:41:04 +0000259 if ((status = mAcceptFn(*this, &clientSocket)) != OK) {
260 if (status == DEAD_OBJECT)
261 break;
262 else
263 continue;
Steven Moreland410325a2021-06-02 18:37:42 +0000264 }
David Brazdila47dfda2022-11-22 22:52:19 +0000265
266 LOG_RPC_DETAIL("accept on fd %d yields fd %d", mServer.fd.get(), clientSocket.fd.get());
267
David Brazdil21c887c2022-09-23 12:25:18 +0100268 if (getpeername(clientSocket.fd.get(), reinterpret_cast<sockaddr*>(addr.data()),
269 &addrLen)) {
270 ALOGE("Could not getpeername socket: %s", strerror(errno));
271 continue;
272 }
273
David Brazdila47dfda2022-11-22 22:52:19 +0000274 if (mConnectionFilter != nullptr && !mConnectionFilter(addr.data(), addrLen)) {
275 ALOGE("Dropped client connection fd %d", clientSocket.fd.get());
276 continue;
277 }
Steven Moreland410325a2021-06-02 18:37:42 +0000278
279 {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000280 RpcMutexLockGuard _l(mLock);
Andrei Homescu74a54452021-12-10 05:30:21 +0000281 RpcMaybeThread thread =
282 RpcMaybeThread(&RpcServer::establishConnection,
Pawan49d74cb2022-08-03 21:19:11 +0000283 sp<RpcServer>::fromExisting(this), std::move(clientSocket), addr,
Andrei Homescu74a54452021-12-10 05:30:21 +0000284 addrLen, RpcSession::join);
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000285
286 auto& threadRef = mConnectingThreads[thread.get_id()];
287 threadRef = std::move(thread);
288 rpcJoinIfSingleThreaded(threadRef);
Steven Moreland410325a2021-06-02 18:37:42 +0000289 }
Yifan Hong1a235852021-05-13 16:07:47 -0700290 }
Steven Moreland2b4f3802021-05-22 01:46:27 +0000291 LOG_RPC_DETAIL("RpcServer::join exiting with %s", statusToString(status).c_str());
Yifan Hong1a235852021-05-13 16:07:47 -0700292
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000293 if constexpr (kEnableRpcThreads) {
294 RpcMutexLockGuard _l(mLock);
Yifan Hong1a235852021-05-13 16:07:47 -0700295 mJoinThreadRunning = false;
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000296 } else {
297 // Multi-threaded builds clear this in shutdown(), but we need it valid
298 // so the loop above exits cleanly
299 mShutdownTrigger = nullptr;
Yifan Hong1a235852021-05-13 16:07:47 -0700300 }
301 mShutdownCv.notify_all();
Steven Morelandd539fbf2021-05-05 23:40:25 +0000302}
303
Yifan Hong1a235852021-05-13 16:07:47 -0700304bool RpcServer::shutdown() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000305 RpcMutexUniqueLock _l(mLock);
Steven Moreland9d11b922021-05-20 01:22:58 +0000306 if (mShutdownTrigger == nullptr) {
Devin Moore06add172023-03-22 22:49:29 +0000307 LOG_RPC_DETAIL("Cannot shutdown. No shutdown trigger installed (already shutdown, or not "
308 "joined yet?)");
Steven Moreland9d11b922021-05-20 01:22:58 +0000309 return false;
310 }
Yifan Hong1a235852021-05-13 16:07:47 -0700311
312 mShutdownTrigger->trigger();
Steven Morelandab3f4422021-09-27 18:38:20 -0700313
Steven Morelanda8b44292021-06-08 01:27:53 +0000314 for (auto& [id, session] : mSessions) {
315 (void)id;
Steven Morelandab3f4422021-09-27 18:38:20 -0700316 // server lock is a more general lock
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000317 RpcMutexLockGuard _lSession(session->mMutex);
Steven Morelanda8b44292021-06-08 01:27:53 +0000318 session->mShutdownTrigger->trigger();
319 }
320
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000321 if constexpr (!kEnableRpcThreads) {
322 // In single-threaded mode we're done here, everything else that
323 // needs to happen should be at the end of RpcServer::join()
324 return true;
325 }
326
Steven Morelandee3f4662021-05-22 01:07:33 +0000327 while (mJoinThreadRunning || !mConnectingThreads.empty() || !mSessions.empty()) {
Steven Morelandaf4ca712021-05-24 23:22:08 +0000328 if (std::cv_status::timeout == mShutdownCv.wait_for(_l, std::chrono::seconds(1))) {
329 ALOGE("Waiting for RpcServer to shut down (1s w/o progress). Join thread running: %d, "
330 "Connecting threads: "
331 "%zu, Sessions: %zu. Is your server deadlocked?",
332 mJoinThreadRunning, mConnectingThreads.size(), mSessions.size());
333 }
Steven Moreland9d11b922021-05-20 01:22:58 +0000334 }
Yifan Hong1a235852021-05-13 16:07:47 -0700335
Yifan Hong326afd12021-05-19 15:24:54 -0700336 // At this point, we know join() is about to exit, but the thread that calls
337 // join() may not have exited yet.
338 // If RpcServer owns the join thread (aka start() is called), make sure the thread exits;
339 // otherwise ~thread() may call std::terminate(), which may crash the process.
340 // If RpcServer does not own the join thread (aka join() is called directly),
341 // then the owner of RpcServer is responsible for cleaning up that thread.
342 if (mJoinThread.get()) {
343 mJoinThread->join();
344 mJoinThread.reset();
345 }
346
Tomasz Wasilczyk7c8dd482023-03-23 15:30:03 -0700347 mServer = RpcTransportFd();
348
Steven Moreland1c943ec2021-07-13 23:57:56 +0000349 LOG_RPC_DETAIL("Finished waiting on shutdown.");
350
Yifan Hong1a235852021-05-13 16:07:47 -0700351 mShutdownTrigger = nullptr;
352 return true;
353}
354
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000355std::vector<sp<RpcSession>> RpcServer::listSessions() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000356 RpcMutexLockGuard _l(mLock);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000357 std::vector<sp<RpcSession>> sessions;
358 for (auto& [id, session] : mSessions) {
Steven Moreland736664b2021-05-01 04:27:25 +0000359 (void)id;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000360 sessions.push_back(session);
Steven Moreland736664b2021-05-01 04:27:25 +0000361 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000362 return sessions;
Steven Moreland611d15f2021-05-01 01:28:27 +0000363}
364
Steven Morelandd539fbf2021-05-05 23:40:25 +0000365size_t RpcServer::numUninitializedSessions() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000366 RpcMutexLockGuard _l(mLock);
Steven Morelandd539fbf2021-05-05 23:40:25 +0000367 return mConnectingThreads.size();
368}
369
Andrei Homescu74a54452021-12-10 05:30:21 +0000370void RpcServer::establishConnection(
Pawan3e0061c2022-08-26 21:08:34 +0000371 sp<RpcServer>&& server, RpcTransportFd clientFd, std::array<uint8_t, kRpcAddressSize> addr,
Andrei Homescu74a54452021-12-10 05:30:21 +0000372 size_t addrLen,
373 std::function<void(sp<RpcSession>&&, RpcSession::PreJoinSetupResult&&)>&& joinFn) {
Steven Moreland9d11b922021-05-20 01:22:58 +0000374 // mShutdownTrigger can only be cleared once connection threads have joined.
375 // It must be set before this thread is started
376 LOG_ALWAYS_FATAL_IF(server->mShutdownTrigger == nullptr);
Yifan Hong702115c2021-06-24 15:39:18 -0700377 LOG_ALWAYS_FATAL_IF(server->mCtx == nullptr);
378
379 status_t status = OK;
380
Pawan49d74cb2022-08-03 21:19:11 +0000381 int clientFdForLog = clientFd.fd.get();
Yifan Hongf6d42292021-08-05 23:43:05 -0700382 auto client = server->mCtx->newTransport(std::move(clientFd), server->mShutdownTrigger.get());
Yifan Hong702115c2021-06-24 15:39:18 -0700383 if (client == nullptr) {
384 ALOGE("Dropping accept4()-ed socket because sslAccept fails");
385 status = DEAD_OBJECT;
386 // still need to cleanup before we can return
387 } else {
388 LOG_RPC_DETAIL("Created RpcTransport %p for client fd %d", client.get(), clientFdForLog);
389 }
Steven Moreland9d11b922021-05-20 01:22:58 +0000390
Steven Moreland659416d2021-05-11 00:47:50 +0000391 RpcConnectionHeader header;
Yifan Hong702115c2021-06-24 15:39:18 -0700392 if (status == OK) {
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000393 iovec iov{&header, sizeof(header)};
Devin Moore695368f2022-06-03 22:29:14 +0000394 status = client->interruptableReadFully(server->mShutdownTrigger.get(), &iov, 1,
Frederick Mayleffe9ac22022-06-30 02:07:36 +0000395 std::nullopt, /*ancillaryFds=*/nullptr);
Yifan Hong702115c2021-06-24 15:39:18 -0700396 if (status != OK) {
397 ALOGE("Failed to read ID for client connecting to RPC server: %s",
398 statusToString(status).c_str());
399 // still need to cleanup before we can return
400 }
Steven Morelanda63ff932021-05-12 00:03:15 +0000401 }
Steven Morelandbf57bce2021-07-26 15:26:12 -0700402
Steven Moreland826367f2021-09-10 14:05:31 -0700403 std::vector<uint8_t> sessionId;
404 if (status == OK) {
405 if (header.sessionIdSize > 0) {
Steven Morelandc5032042021-09-30 15:40:27 -0700406 if (header.sessionIdSize == kSessionIdBytes) {
407 sessionId.resize(header.sessionIdSize);
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000408 iovec iov{sessionId.data(), sessionId.size()};
Devin Moore695368f2022-06-03 22:29:14 +0000409 status = client->interruptableReadFully(server->mShutdownTrigger.get(), &iov, 1,
Frederick Mayleffe9ac22022-06-30 02:07:36 +0000410 std::nullopt, /*ancillaryFds=*/nullptr);
Steven Morelandc5032042021-09-30 15:40:27 -0700411 if (status != OK) {
412 ALOGE("Failed to read session ID for client connecting to RPC server: %s",
413 statusToString(status).c_str());
414 // still need to cleanup before we can return
415 }
416 } else {
417 ALOGE("Malformed session ID. Expecting session ID of size %zu but got %" PRIu16,
418 kSessionIdBytes, header.sessionIdSize);
419 status = BAD_VALUE;
Steven Moreland826367f2021-09-10 14:05:31 -0700420 }
421 }
422 }
423
Steven Morelandbf57bce2021-07-26 15:26:12 -0700424 bool incoming = false;
425 uint32_t protocolVersion = 0;
Steven Morelandbf57bce2021-07-26 15:26:12 -0700426 bool requestingNewSession = false;
427
428 if (status == OK) {
429 incoming = header.options & RPC_CONNECTION_OPTION_INCOMING;
430 protocolVersion = std::min(header.version,
431 server->mProtocolVersion.value_or(RPC_WIRE_PROTOCOL_VERSION));
Steven Moreland826367f2021-09-10 14:05:31 -0700432 requestingNewSession = sessionId.empty();
Steven Morelandbf57bce2021-07-26 15:26:12 -0700433
434 if (requestingNewSession) {
435 RpcNewSessionResponse response{
436 .version = protocolVersion,
437 };
438
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000439 iovec iov{&response, sizeof(response)};
Devin Moore695368f2022-06-03 22:29:14 +0000440 status = client->interruptableWriteFully(server->mShutdownTrigger.get(), &iov, 1,
Frederick Mayle69a0c992022-05-26 20:38:39 +0000441 std::nullopt, nullptr);
Steven Morelandbf57bce2021-07-26 15:26:12 -0700442 if (status != OK) {
443 ALOGE("Failed to send new session response: %s", statusToString(status).c_str());
444 // still need to cleanup before we can return
445 }
446 }
447 }
Steven Morelanda63ff932021-05-12 00:03:15 +0000448
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000449 RpcMaybeThread thisThread;
Steven Morelanda63ff932021-05-12 00:03:15 +0000450 sp<RpcSession> session;
451 {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000452 RpcMutexUniqueLock _l(server->mLock);
Steven Morelanda63ff932021-05-12 00:03:15 +0000453
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000454 auto threadId = server->mConnectingThreads.find(rpc_this_thread::get_id());
Yifan Hongb3005502021-05-19 15:37:00 -0700455 LOG_ALWAYS_FATAL_IF(threadId == server->mConnectingThreads.end(),
Steven Morelanda63ff932021-05-12 00:03:15 +0000456 "Must establish connection on owned thread");
457 thisThread = std::move(threadId->second);
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +0000458 auto detachGuardLambda = [&]() {
Steven Morelandadc5dca2021-05-25 02:06:03 +0000459 thisThread.detach();
Steven Moreland9d11b922021-05-20 01:22:58 +0000460 _l.unlock();
461 server->mShutdownCv.notify_all();
462 };
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +0000463 auto detachGuard = make_scope_guard(std::ref(detachGuardLambda));
Steven Morelandadc5dca2021-05-25 02:06:03 +0000464 server->mConnectingThreads.erase(threadId);
Steven Moreland9d11b922021-05-20 01:22:58 +0000465
Steven Morelandbf57bce2021-07-26 15:26:12 -0700466 if (status != OK || server->mShutdownTrigger->isTriggered()) {
Steven Moreland5802c2b2021-05-12 20:13:04 +0000467 return;
468 }
469
Steven Morelandbf57bce2021-07-26 15:26:12 -0700470 if (requestingNewSession) {
Steven Moreland1b304292021-07-15 22:59:34 +0000471 if (incoming) {
472 ALOGE("Cannot create a new session with an incoming connection, would leak");
Steven Moreland659416d2021-05-11 00:47:50 +0000473 return;
474 }
475
Steven Moreland826367f2021-09-10 14:05:31 -0700476 // Uniquely identify session at the application layer. Even if a
477 // client/server use the same certificates, if they create multiple
478 // sessions, we still want to distinguish between them.
Steven Morelandc5032042021-09-30 15:40:27 -0700479 sessionId.resize(kSessionIdBytes);
Steven Moreland01a6bad2021-06-11 00:59:20 +0000480 size_t tries = 0;
481 do {
482 // don't block if there is some entropy issue
483 if (tries++ > 5) {
Steven Moreland826367f2021-09-10 14:05:31 -0700484 ALOGE("Cannot find new address: %s",
Tomasz Wasilczyk891f6b02023-10-11 18:35:42 +0000485 HexString(sessionId.data(), sessionId.size()).c_str());
Steven Moreland01a6bad2021-06-11 00:59:20 +0000486 return;
487 }
488
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +0000489 auto status = binder::os::getRandomBytes(sessionId.data(), sessionId.size());
Andrei Homescuc24c8792022-04-19 00:24:51 +0000490 if (status != OK) {
491 ALOGE("Failed to read random session ID: %s", strerror(-status));
Steven Moreland826367f2021-09-10 14:05:31 -0700492 return;
493 }
Steven Moreland01a6bad2021-06-11 00:59:20 +0000494 } while (server->mSessions.end() != server->mSessions.find(sessionId));
Steven Morelanda63ff932021-05-12 00:03:15 +0000495
Andrei Homescu5e301462022-05-12 00:52:34 +0000496 session = sp<RpcSession>::make(nullptr);
Yifan Hong10423062021-10-08 16:26:32 -0700497 session->setMaxIncomingThreads(server->mMaxThreads);
Steven Morelandbf57bce2021-07-26 15:26:12 -0700498 if (!session->setProtocolVersion(protocolVersion)) return;
Steven Moreland51c44a92021-10-14 16:50:35 -0700499
Frederick Mayle26b494e2022-07-08 22:09:58 +0000500 if (header.fileDescriptorTransportMode <
501 server->mSupportedFileDescriptorTransportModes.size() &&
502 server->mSupportedFileDescriptorTransportModes.test(
Frederick Mayle69a0c992022-05-26 20:38:39 +0000503 header.fileDescriptorTransportMode)) {
504 session->setFileDescriptorTransportMode(
505 static_cast<RpcSession::FileDescriptorTransportMode>(
506 header.fileDescriptorTransportMode));
507 } else {
508 ALOGE("Rejecting connection: FileDescriptorTransportMode is not supported: %hhu",
509 header.fileDescriptorTransportMode);
510 return;
511 }
512
Steven Moreland51c44a92021-10-14 16:50:35 -0700513 // if null, falls back to server root
514 sp<IBinder> sessionSpecificRoot;
515 if (server->mRootObjectFactory != nullptr) {
Steven Morelanddfb92182023-05-24 17:48:59 +0000516 sessionSpecificRoot =
517 server->mRootObjectFactory(wp<RpcSession>(session), addr.data(), addrLen);
Steven Moreland51c44a92021-10-14 16:50:35 -0700518 if (sessionSpecificRoot == nullptr) {
519 ALOGE("Warning: server returned null from root object factory");
520 }
521 }
522
Steven Morelanda8b44292021-06-08 01:27:53 +0000523 if (!session->setForServer(server,
524 sp<RpcServer::EventListener>::fromExisting(
525 static_cast<RpcServer::EventListener*>(
526 server.get())),
Steven Moreland51c44a92021-10-14 16:50:35 -0700527 sessionId, sessionSpecificRoot)) {
Steven Morelanda8b44292021-06-08 01:27:53 +0000528 ALOGE("Failed to attach server to session");
529 return;
530 }
Steven Morelanda63ff932021-05-12 00:03:15 +0000531
Steven Moreland01a6bad2021-06-11 00:59:20 +0000532 server->mSessions[sessionId] = session;
Steven Morelanda63ff932021-05-12 00:03:15 +0000533 } else {
Steven Moreland01a6bad2021-06-11 00:59:20 +0000534 auto it = server->mSessions.find(sessionId);
Yifan Hongb3005502021-05-19 15:37:00 -0700535 if (it == server->mSessions.end()) {
Steven Moreland01a6bad2021-06-11 00:59:20 +0000536 ALOGE("Cannot add thread, no record of session with ID %s",
Tomasz Wasilczyk891f6b02023-10-11 18:35:42 +0000537 HexString(sessionId.data(), sessionId.size()).c_str());
Steven Morelanda63ff932021-05-12 00:03:15 +0000538 return;
539 }
540 session = it->second;
541 }
Steven Moreland5802c2b2021-05-12 20:13:04 +0000542
Steven Moreland1b304292021-07-15 22:59:34 +0000543 if (incoming) {
Steven Moreland2372f9d2021-08-05 15:42:01 -0700544 LOG_ALWAYS_FATAL_IF(OK != session->addOutgoingConnection(std::move(client), true),
Steven Moreland659416d2021-05-11 00:47:50 +0000545 "server state must already be initialized");
546 return;
547 }
548
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +0000549 detachGuard.release();
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000550 session->preJoinThreadOwnership(std::move(thisThread));
Steven Morelanda63ff932021-05-12 00:03:15 +0000551 }
552
Yifan Hong702115c2021-06-24 15:39:18 -0700553 auto setupResult = session->preJoinSetup(std::move(client));
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000554
Steven Morelanda63ff932021-05-12 00:03:15 +0000555 // avoid strong cycle
556 server = nullptr;
Steven Morelanda63ff932021-05-12 00:03:15 +0000557
Andrei Homescu74a54452021-12-10 05:30:21 +0000558 joinFn(std::move(session), std::move(setupResult));
Steven Morelanda63ff932021-05-12 00:03:15 +0000559}
560
Steven Moreland2372f9d2021-08-05 15:42:01 -0700561status_t RpcServer::setupSocketServer(const RpcSocketAddress& addr) {
Steven Moreland704fc1a2021-05-04 23:13:14 +0000562 LOG_RPC_DETAIL("Setting up socket server %s", addr.toString().c_str());
Yifan Hong0eb5a672021-05-12 18:00:25 -0700563 LOG_ALWAYS_FATAL_IF(hasServer(), "Each RpcServer can only have one server.");
Steven Moreland611d15f2021-05-01 01:28:27 +0000564
Alice Wang0b138ed2022-11-15 16:22:44 +0000565 unique_fd socket_fd(TEMP_FAILURE_RETRY(
566 socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)));
567 if (!socket_fd.ok()) {
Steven Moreland2372f9d2021-08-05 15:42:01 -0700568 int savedErrno = errno;
Steven Moreland8925ebb2023-04-10 21:52:17 +0000569 ALOGE("Could not create socket at %s: %s", addr.toString().c_str(), strerror(savedErrno));
Steven Moreland2372f9d2021-08-05 15:42:01 -0700570 return -savedErrno;
Steven Moreland611d15f2021-05-01 01:28:27 +0000571 }
Tomasz Wasilczyk7c8dd482023-03-23 15:30:03 -0700572
Keith Mok442907b2023-10-11 20:36:18 +0000573 if (addr.addr()->sa_family == AF_INET || addr.addr()->sa_family == AF_INET6) {
574 int noDelay = 1;
575 int result =
576 setsockopt(socket_fd.get(), IPPROTO_TCP, TCP_NODELAY, &noDelay, sizeof(noDelay));
577 if (result < 0) {
578 int savedErrno = errno;
579 ALOGE("Could not set TCP_NODELAY on %s", strerror(savedErrno));
580 return -savedErrno;
581 }
582 }
583
Tomasz Wasilczyk7c8dd482023-03-23 15:30:03 -0700584 {
585 RpcMutexLockGuard _l(mLock);
586 if (mServerSocketModifier != nullptr) {
587 mServerSocketModifier(socket_fd);
588 }
589 }
590
Alice Wang0b138ed2022-11-15 16:22:44 +0000591 if (0 != TEMP_FAILURE_RETRY(bind(socket_fd.get(), addr.addr(), addr.addrSize()))) {
Steven Moreland611d15f2021-05-01 01:28:27 +0000592 int savedErrno = errno;
593 ALOGE("Could not bind socket at %s: %s", addr.toString().c_str(), strerror(savedErrno));
Steven Moreland2372f9d2021-08-05 15:42:01 -0700594 return -savedErrno;
Steven Moreland611d15f2021-05-01 01:28:27 +0000595 }
596
Alice Wang0b138ed2022-11-15 16:22:44 +0000597 return setupRawSocketServer(std::move(socket_fd));
Steven Moreland611d15f2021-05-01 01:28:27 +0000598}
599
Alice Wang0b138ed2022-11-15 16:22:44 +0000600status_t RpcServer::setupRawSocketServer(unique_fd socket_fd) {
Alice Wang30c204d2022-11-15 12:50:51 +0000601 LOG_ALWAYS_FATAL_IF(!socket_fd.ok(), "Socket must be setup to listen.");
Alice Wang30c204d2022-11-15 12:50:51 +0000602
Alice Wang893a9912022-10-24 10:44:09 +0000603 // Right now, we create all threads at once, making accept4 slow. To avoid hanging the client,
604 // the backlog is increased to a large number.
605 // TODO(b/189955605): Once we create threads dynamically & lazily, the backlog can be reduced
606 // to 1.
Alice Wangdf741782022-11-16 08:37:04 +0000607 if (0 != TEMP_FAILURE_RETRY(listen(socket_fd.get(), 50 /*backlog*/))) {
Alice Wang893a9912022-10-24 10:44:09 +0000608 int savedErrno = errno;
609 ALOGE("Could not listen initialized Unix socket: %s", strerror(savedErrno));
610 return -savedErrno;
611 }
Alice Wangdf741782022-11-16 08:37:04 +0000612 if (status_t status = setupExternalServer(std::move(socket_fd)); status != OK) {
Alice Wang893a9912022-10-24 10:44:09 +0000613 ALOGE("Another thread has set up server while calling setupSocketServer. Race?");
614 return status;
615 }
616 return OK;
617}
618
Steven Morelanddd67b942021-07-23 17:15:41 -0700619void RpcServer::onSessionAllIncomingThreadsEnded(const sp<RpcSession>& session) {
Steven Moreland826367f2021-09-10 14:05:31 -0700620 const std::vector<uint8_t>& id = session->mId;
621 LOG_ALWAYS_FATAL_IF(id.empty(), "Server sessions must be initialized with ID");
Tomasz Wasilczyk891f6b02023-10-11 18:35:42 +0000622 LOG_RPC_DETAIL("Dropping session with address %s", HexString(id.data(), id.size()).c_str());
Steven Morelandee78e762021-05-05 21:12:51 +0000623
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000624 RpcMutexLockGuard _l(mLock);
Steven Moreland826367f2021-09-10 14:05:31 -0700625 auto it = mSessions.find(id);
Steven Moreland01a6bad2021-06-11 00:59:20 +0000626 LOG_ALWAYS_FATAL_IF(it == mSessions.end(), "Bad state, unknown session id %s",
Tomasz Wasilczyk891f6b02023-10-11 18:35:42 +0000627 HexString(id.data(), id.size()).c_str());
Steven Moreland01a6bad2021-06-11 00:59:20 +0000628 LOG_ALWAYS_FATAL_IF(it->second != session, "Bad state, session has id mismatch %s",
Tomasz Wasilczyk891f6b02023-10-11 18:35:42 +0000629 HexString(id.data(), id.size()).c_str());
Steven Morelandee78e762021-05-05 21:12:51 +0000630 (void)mSessions.erase(it);
631}
632
Steven Moreland19fc9f72021-06-10 03:57:30 +0000633void RpcServer::onSessionIncomingThreadEnded() {
Steven Morelandee3f4662021-05-22 01:07:33 +0000634 mShutdownCv.notify_all();
635}
636
Yifan Hong0eb5a672021-05-12 18:00:25 -0700637bool RpcServer::hasServer() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000638 RpcMutexLockGuard _l(mLock);
Pawan49d74cb2022-08-03 21:19:11 +0000639 return mServer.fd.ok();
Yifan Hong0eb5a672021-05-12 18:00:25 -0700640}
641
Yifan Hong00aeb762021-05-12 17:07:36 -0700642unique_fd RpcServer::releaseServer() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000643 RpcMutexLockGuard _l(mLock);
Pawan49d74cb2022-08-03 21:19:11 +0000644 return std::move(mServer.fd);
Yifan Hong00aeb762021-05-12 17:07:36 -0700645}
646
David Brazdil21c887c2022-09-23 12:25:18 +0100647status_t RpcServer::setupExternalServer(
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700648 unique_fd serverFd, std::function<status_t(const RpcServer&, RpcTransportFd*)>&& acceptFn) {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000649 RpcMutexLockGuard _l(mLock);
Pawan49d74cb2022-08-03 21:19:11 +0000650 if (mServer.fd.ok()) {
Yifan Hong00aeb762021-05-12 17:07:36 -0700651 ALOGE("Each RpcServer can only have one server.");
Steven Moreland2372f9d2021-08-05 15:42:01 -0700652 return INVALID_OPERATION;
Yifan Hong00aeb762021-05-12 17:07:36 -0700653 }
654 mServer = std::move(serverFd);
David Brazdil21c887c2022-09-23 12:25:18 +0100655 mAcceptFn = std::move(acceptFn);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700656 return OK;
Yifan Hong00aeb762021-05-12 17:07:36 -0700657}
658
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700659status_t RpcServer::setupExternalServer(unique_fd serverFd) {
David Brazdil21c887c2022-09-23 12:25:18 +0100660 return setupExternalServer(std::move(serverFd), &RpcServer::acceptSocketConnection);
661}
662
Pawan1c24f9c2022-08-26 23:23:15 +0000663bool RpcServer::hasActiveRequests() {
664 RpcMutexLockGuard _l(mLock);
665 for (const auto& [_, session] : mSessions) {
666 if (session->hasActiveRequests()) {
667 return true;
668 }
669 }
670 return !mServer.isInPollingState();
671}
672
Steven Moreland5553ac42020-11-11 02:14:45 +0000673} // namespace android