blob: 68c12f7d0e6fb599152176b9b8f6d99bed6874a8 [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 Moreland798e0d12021-07-14 23:19:25 +000019#include <poll.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000020#include <sys/socket.h>
21#include <sys/un.h>
22
Steven Morelandf137de92021-04-24 01:54:26 +000023#include <thread>
Steven Moreland5553ac42020-11-11 02:14:45 +000024#include <vector>
25
Steven Moreland5802c2b2021-05-12 20:13:04 +000026#include <android-base/scopeguard.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000027#include <binder/Parcel.h>
28#include <binder/RpcServer.h>
29#include <log/log.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000030
Steven Moreland611d15f2021-05-01 01:28:27 +000031#include "RpcSocketAddress.h"
Yifan Hong1a235852021-05-13 16:07:47 -070032#include "RpcState.h"
Steven Moreland5553ac42020-11-11 02:14:45 +000033#include "RpcWireFormat.h"
34
35namespace android {
36
Steven Moreland5802c2b2021-05-12 20:13:04 +000037using base::ScopeGuard;
Steven Moreland611d15f2021-05-01 01:28:27 +000038using base::unique_fd;
39
Steven Moreland5553ac42020-11-11 02:14:45 +000040RpcServer::RpcServer() {}
Yifan Hong436f0e62021-05-19 15:25:34 -070041RpcServer::~RpcServer() {
42 (void)shutdown();
43}
Steven Moreland5553ac42020-11-11 02:14:45 +000044
45sp<RpcServer> RpcServer::make() {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +000046 return sp<RpcServer>::make();
Steven Moreland5553ac42020-11-11 02:14:45 +000047}
48
49void RpcServer::iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction() {
50 mAgreedExperimental = true;
51}
52
Steven Moreland611d15f2021-05-01 01:28:27 +000053bool RpcServer::setupUnixDomainServer(const char* path) {
54 return setupSocketServer(UnixSocketAddress(path));
55}
56
Steven Moreland611d15f2021-05-01 01:28:27 +000057bool RpcServer::setupVsockServer(unsigned int port) {
58 // realizing value w/ this type at compile time to avoid ubsan abort
59 constexpr unsigned int kAnyCid = VMADDR_CID_ANY;
60
61 return setupSocketServer(VsockSocketAddress(kAnyCid, port));
62}
63
Devin Mooref3b9c4f2021-08-03 15:50:13 +000064bool RpcServer::setupInetServer(const char* address, unsigned int port,
65 unsigned int* assignedPort) {
Steven Moreland611d15f2021-05-01 01:28:27 +000066 if (assignedPort != nullptr) *assignedPort = 0;
Devin Mooref3b9c4f2021-08-03 15:50:13 +000067 auto aiStart = InetSocketAddress::getAddrInfo(address, port);
Steven Moreland611d15f2021-05-01 01:28:27 +000068 if (aiStart == nullptr) return false;
69 for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) {
Devin Mooref3b9c4f2021-08-03 15:50:13 +000070 InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, address, port);
Steven Moreland611d15f2021-05-01 01:28:27 +000071 if (!setupSocketServer(socketAddress)) {
72 continue;
73 }
74
75 LOG_ALWAYS_FATAL_IF(socketAddress.addr()->sa_family != AF_INET, "expecting inet");
76 sockaddr_in addr{};
77 socklen_t len = sizeof(addr);
78 if (0 != getsockname(mServer.get(), reinterpret_cast<sockaddr*>(&addr), &len)) {
79 int savedErrno = errno;
80 ALOGE("Could not getsockname at %s: %s", socketAddress.toString().c_str(),
81 strerror(savedErrno));
82 return false;
83 }
84 LOG_ALWAYS_FATAL_IF(len != sizeof(addr), "Wrong socket type: len %zu vs len %zu",
85 static_cast<size_t>(len), sizeof(addr));
86 unsigned int realPort = ntohs(addr.sin_port);
87 LOG_ALWAYS_FATAL_IF(port != 0 && realPort != port,
88 "Requesting inet server on %s but it is set up on %u.",
89 socketAddress.toString().c_str(), realPort);
90
91 if (assignedPort != nullptr) {
92 *assignedPort = realPort;
93 }
94
95 return true;
96 }
Devin Mooref3b9c4f2021-08-03 15:50:13 +000097 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 +000098 port);
99 return false;
100}
101
Steven Morelandf137de92021-04-24 01:54:26 +0000102void RpcServer::setMaxThreads(size_t threads) {
103 LOG_ALWAYS_FATAL_IF(threads <= 0, "RpcServer is useless without threads");
Yifan Hong1a235852021-05-13 16:07:47 -0700104 LOG_ALWAYS_FATAL_IF(mJoinThreadRunning, "Cannot set max threads while running");
Steven Morelandf137de92021-04-24 01:54:26 +0000105 mMaxThreads = threads;
106}
107
108size_t RpcServer::getMaxThreads() {
109 return mMaxThreads;
Steven Moreland5553ac42020-11-11 02:14:45 +0000110}
111
Steven Morelandbf57bce2021-07-26 15:26:12 -0700112void RpcServer::setProtocolVersion(uint32_t version) {
113 mProtocolVersion = version;
114}
115
Steven Moreland5553ac42020-11-11 02:14:45 +0000116void RpcServer::setRootObject(const sp<IBinder>& binder) {
Steven Morelandebafe332021-04-24 00:24:35 +0000117 std::lock_guard<std::mutex> _l(mLock);
Yifan Hong4ffb0c72021-05-07 18:35:14 -0700118 mRootObjectWeak = mRootObject = binder;
119}
120
121void RpcServer::setRootObjectWeak(const wp<IBinder>& binder) {
122 std::lock_guard<std::mutex> _l(mLock);
123 mRootObject.clear();
124 mRootObjectWeak = binder;
Steven Moreland5553ac42020-11-11 02:14:45 +0000125}
126
127sp<IBinder> RpcServer::getRootObject() {
Steven Morelandebafe332021-04-24 00:24:35 +0000128 std::lock_guard<std::mutex> _l(mLock);
Yifan Hong4ffb0c72021-05-07 18:35:14 -0700129 bool hasWeak = mRootObjectWeak.unsafe_get();
130 sp<IBinder> ret = mRootObjectWeak.promote();
131 ALOGW_IF(hasWeak && ret == nullptr, "RpcServer root object is freed, returning nullptr");
132 return ret;
Steven Moreland5553ac42020-11-11 02:14:45 +0000133}
134
Yifan Hong326afd12021-05-19 15:24:54 -0700135static void joinRpcServer(sp<RpcServer>&& thiz) {
136 thiz->join();
137}
138
139void RpcServer::start() {
140 LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");
141 std::lock_guard<std::mutex> _l(mLock);
142 LOG_ALWAYS_FATAL_IF(mJoinThread.get(), "Already started!");
143 mJoinThread = std::make_unique<std::thread>(&joinRpcServer, sp<RpcServer>::fromExisting(this));
144}
145
Steven Moreland611d15f2021-05-01 01:28:27 +0000146void RpcServer::join() {
Yifan Hong1a235852021-05-13 16:07:47 -0700147 LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");
148
149 {
150 std::lock_guard<std::mutex> _l(mLock);
151 LOG_ALWAYS_FATAL_IF(!mServer.ok(), "RpcServer must be setup to join.");
152 LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr, "Already joined");
153 mJoinThreadRunning = true;
Steven Morelande47511f2021-05-20 00:07:41 +0000154 mShutdownTrigger = RpcSession::FdTrigger::make();
Yifan Hong1a235852021-05-13 16:07:47 -0700155 LOG_ALWAYS_FATAL_IF(mShutdownTrigger == nullptr, "Cannot create join signaler");
Steven Morelandd539fbf2021-05-05 23:40:25 +0000156 }
Yifan Hong1a235852021-05-13 16:07:47 -0700157
Steven Moreland2b4f3802021-05-22 01:46:27 +0000158 status_t status;
Steven Moreland798e0d12021-07-14 23:19:25 +0000159 while ((status = mShutdownTrigger->triggerablePoll(mServer, POLLIN)) == OK) {
Steven Moreland410325a2021-06-02 18:37:42 +0000160 unique_fd clientFd(TEMP_FAILURE_RETRY(
161 accept4(mServer.get(), nullptr, nullptr /*length*/, SOCK_CLOEXEC)));
162
163 if (clientFd < 0) {
164 ALOGE("Could not accept4 socket: %s", strerror(errno));
165 continue;
166 }
167 LOG_RPC_DETAIL("accept4 on fd %d yields fd %d", mServer.get(), clientFd.get());
168
169 {
170 std::lock_guard<std::mutex> _l(mLock);
171 std::thread thread =
172 std::thread(&RpcServer::establishConnection, sp<RpcServer>::fromExisting(this),
173 std::move(clientFd));
174 mConnectingThreads[thread.get_id()] = std::move(thread);
175 }
Yifan Hong1a235852021-05-13 16:07:47 -0700176 }
Steven Moreland2b4f3802021-05-22 01:46:27 +0000177 LOG_RPC_DETAIL("RpcServer::join exiting with %s", statusToString(status).c_str());
Yifan Hong1a235852021-05-13 16:07:47 -0700178
179 {
180 std::lock_guard<std::mutex> _l(mLock);
181 mJoinThreadRunning = false;
182 }
183 mShutdownCv.notify_all();
Steven Morelandd539fbf2021-05-05 23:40:25 +0000184}
185
Yifan Hong1a235852021-05-13 16:07:47 -0700186bool RpcServer::shutdown() {
Yifan Hong1a235852021-05-13 16:07:47 -0700187 std::unique_lock<std::mutex> _l(mLock);
Steven Moreland9d11b922021-05-20 01:22:58 +0000188 if (mShutdownTrigger == nullptr) {
Steven Moreland1c943ec2021-07-13 23:57:56 +0000189 LOG_RPC_DETAIL("Cannot shutdown. No shutdown trigger installed (already shutdown?)");
Steven Moreland9d11b922021-05-20 01:22:58 +0000190 return false;
191 }
Yifan Hong1a235852021-05-13 16:07:47 -0700192
193 mShutdownTrigger->trigger();
Steven Morelanda8b44292021-06-08 01:27:53 +0000194 for (auto& [id, session] : mSessions) {
195 (void)id;
196 session->mShutdownTrigger->trigger();
197 }
198
Steven Morelandee3f4662021-05-22 01:07:33 +0000199 while (mJoinThreadRunning || !mConnectingThreads.empty() || !mSessions.empty()) {
Steven Morelandaf4ca712021-05-24 23:22:08 +0000200 if (std::cv_status::timeout == mShutdownCv.wait_for(_l, std::chrono::seconds(1))) {
201 ALOGE("Waiting for RpcServer to shut down (1s w/o progress). Join thread running: %d, "
202 "Connecting threads: "
203 "%zu, Sessions: %zu. Is your server deadlocked?",
204 mJoinThreadRunning, mConnectingThreads.size(), mSessions.size());
205 }
Steven Moreland9d11b922021-05-20 01:22:58 +0000206 }
Yifan Hong1a235852021-05-13 16:07:47 -0700207
Yifan Hong326afd12021-05-19 15:24:54 -0700208 // At this point, we know join() is about to exit, but the thread that calls
209 // join() may not have exited yet.
210 // If RpcServer owns the join thread (aka start() is called), make sure the thread exits;
211 // otherwise ~thread() may call std::terminate(), which may crash the process.
212 // If RpcServer does not own the join thread (aka join() is called directly),
213 // then the owner of RpcServer is responsible for cleaning up that thread.
214 if (mJoinThread.get()) {
215 mJoinThread->join();
216 mJoinThread.reset();
217 }
218
Steven Moreland1c943ec2021-07-13 23:57:56 +0000219 LOG_RPC_DETAIL("Finished waiting on shutdown.");
220
Yifan Hong1a235852021-05-13 16:07:47 -0700221 mShutdownTrigger = nullptr;
222 return true;
223}
224
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000225std::vector<sp<RpcSession>> RpcServer::listSessions() {
Steven Moreland611d15f2021-05-01 01:28:27 +0000226 std::lock_guard<std::mutex> _l(mLock);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000227 std::vector<sp<RpcSession>> sessions;
228 for (auto& [id, session] : mSessions) {
Steven Moreland736664b2021-05-01 04:27:25 +0000229 (void)id;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000230 sessions.push_back(session);
Steven Moreland736664b2021-05-01 04:27:25 +0000231 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000232 return sessions;
Steven Moreland611d15f2021-05-01 01:28:27 +0000233}
234
Steven Morelandd539fbf2021-05-05 23:40:25 +0000235size_t RpcServer::numUninitializedSessions() {
236 std::lock_guard<std::mutex> _l(mLock);
237 return mConnectingThreads.size();
238}
239
Steven Morelanda63ff932021-05-12 00:03:15 +0000240void RpcServer::establishConnection(sp<RpcServer>&& server, base::unique_fd clientFd) {
Steven Morelanda63ff932021-05-12 00:03:15 +0000241 // TODO(b/183988761): cannot trust this simple ID
Yifan Hongb3005502021-05-19 15:37:00 -0700242 LOG_ALWAYS_FATAL_IF(!server->mAgreedExperimental, "no!");
Steven Moreland9d11b922021-05-20 01:22:58 +0000243
244 // mShutdownTrigger can only be cleared once connection threads have joined.
245 // It must be set before this thread is started
246 LOG_ALWAYS_FATAL_IF(server->mShutdownTrigger == nullptr);
247
Steven Moreland659416d2021-05-11 00:47:50 +0000248 RpcConnectionHeader header;
249 status_t status = server->mShutdownTrigger->interruptableReadFully(clientFd.get(), &header,
250 sizeof(header));
Steven Morelandbf57bce2021-07-26 15:26:12 -0700251 if (status != OK) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000252 ALOGE("Failed to read ID for client connecting to RPC server: %s",
253 statusToString(status).c_str());
254 // still need to cleanup before we can return
Steven Morelanda63ff932021-05-12 00:03:15 +0000255 }
Steven Morelandbf57bce2021-07-26 15:26:12 -0700256
257 bool incoming = false;
258 uint32_t protocolVersion = 0;
259 RpcAddress sessionId = RpcAddress::zero();
260 bool requestingNewSession = false;
261
262 if (status == OK) {
263 incoming = header.options & RPC_CONNECTION_OPTION_INCOMING;
264 protocolVersion = std::min(header.version,
265 server->mProtocolVersion.value_or(RPC_WIRE_PROTOCOL_VERSION));
266 sessionId = RpcAddress::fromRawEmbedded(&header.sessionId);
267 requestingNewSession = sessionId.isZero();
268
269 if (requestingNewSession) {
270 RpcNewSessionResponse response{
271 .version = protocolVersion,
272 };
273
274 status = server->mShutdownTrigger->interruptableWriteFully(clientFd.get(), &response,
275 sizeof(response));
276 if (status != OK) {
277 ALOGE("Failed to send new session response: %s", statusToString(status).c_str());
278 // still need to cleanup before we can return
279 }
280 }
281 }
Steven Morelanda63ff932021-05-12 00:03:15 +0000282
283 std::thread thisThread;
284 sp<RpcSession> session;
285 {
Steven Moreland9d11b922021-05-20 01:22:58 +0000286 std::unique_lock<std::mutex> _l(server->mLock);
Steven Morelanda63ff932021-05-12 00:03:15 +0000287
Yifan Hongb3005502021-05-19 15:37:00 -0700288 auto threadId = server->mConnectingThreads.find(std::this_thread::get_id());
289 LOG_ALWAYS_FATAL_IF(threadId == server->mConnectingThreads.end(),
Steven Morelanda63ff932021-05-12 00:03:15 +0000290 "Must establish connection on owned thread");
291 thisThread = std::move(threadId->second);
Steven Morelandadc5dca2021-05-25 02:06:03 +0000292 ScopeGuard detachGuard = [&]() {
293 thisThread.detach();
Steven Moreland9d11b922021-05-20 01:22:58 +0000294 _l.unlock();
295 server->mShutdownCv.notify_all();
296 };
Steven Morelandadc5dca2021-05-25 02:06:03 +0000297 server->mConnectingThreads.erase(threadId);
Steven Moreland9d11b922021-05-20 01:22:58 +0000298
Steven Morelandbf57bce2021-07-26 15:26:12 -0700299 if (status != OK || server->mShutdownTrigger->isTriggered()) {
Steven Moreland5802c2b2021-05-12 20:13:04 +0000300 return;
301 }
302
Steven Morelandbf57bce2021-07-26 15:26:12 -0700303 if (requestingNewSession) {
Steven Moreland1b304292021-07-15 22:59:34 +0000304 if (incoming) {
305 ALOGE("Cannot create a new session with an incoming connection, would leak");
Steven Moreland659416d2021-05-11 00:47:50 +0000306 return;
307 }
308
Steven Moreland01a6bad2021-06-11 00:59:20 +0000309 size_t tries = 0;
310 do {
311 // don't block if there is some entropy issue
312 if (tries++ > 5) {
313 ALOGE("Cannot find new address: %s", sessionId.toString().c_str());
314 return;
315 }
316
317 sessionId = RpcAddress::random(true /*forServer*/);
318 } while (server->mSessions.end() != server->mSessions.find(sessionId));
Steven Morelanda63ff932021-05-12 00:03:15 +0000319
320 session = RpcSession::make();
Steven Moreland103424e2021-06-02 18:16:19 +0000321 session->setMaxThreads(server->mMaxThreads);
Steven Morelandbf57bce2021-07-26 15:26:12 -0700322 if (!session->setProtocolVersion(protocolVersion)) return;
Steven Morelanda8b44292021-06-08 01:27:53 +0000323 if (!session->setForServer(server,
324 sp<RpcServer::EventListener>::fromExisting(
325 static_cast<RpcServer::EventListener*>(
326 server.get())),
Steven Moreland01a6bad2021-06-11 00:59:20 +0000327 sessionId)) {
Steven Morelanda8b44292021-06-08 01:27:53 +0000328 ALOGE("Failed to attach server to session");
329 return;
330 }
Steven Morelanda63ff932021-05-12 00:03:15 +0000331
Steven Moreland01a6bad2021-06-11 00:59:20 +0000332 server->mSessions[sessionId] = session;
Steven Morelanda63ff932021-05-12 00:03:15 +0000333 } else {
Steven Moreland01a6bad2021-06-11 00:59:20 +0000334 auto it = server->mSessions.find(sessionId);
Yifan Hongb3005502021-05-19 15:37:00 -0700335 if (it == server->mSessions.end()) {
Steven Moreland01a6bad2021-06-11 00:59:20 +0000336 ALOGE("Cannot add thread, no record of session with ID %s",
337 sessionId.toString().c_str());
Steven Morelanda63ff932021-05-12 00:03:15 +0000338 return;
339 }
340 session = it->second;
341 }
Steven Moreland5802c2b2021-05-12 20:13:04 +0000342
Steven Moreland1b304292021-07-15 22:59:34 +0000343 if (incoming) {
Steven Morelandb86e26b2021-06-12 00:35:58 +0000344 LOG_ALWAYS_FATAL_IF(!session->addOutgoingConnection(std::move(clientFd), true),
Steven Moreland659416d2021-05-11 00:47:50 +0000345 "server state must already be initialized");
346 return;
347 }
348
Steven Moreland5802c2b2021-05-12 20:13:04 +0000349 detachGuard.Disable();
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000350 session->preJoinThreadOwnership(std::move(thisThread));
Steven Morelanda63ff932021-05-12 00:03:15 +0000351 }
352
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000353 auto setupResult = session->preJoinSetup(std::move(clientFd));
354
Steven Morelanda63ff932021-05-12 00:03:15 +0000355 // avoid strong cycle
356 server = nullptr;
Steven Morelanda63ff932021-05-12 00:03:15 +0000357
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000358 RpcSession::join(std::move(session), std::move(setupResult));
Steven Morelanda63ff932021-05-12 00:03:15 +0000359}
360
Steven Moreland611d15f2021-05-01 01:28:27 +0000361bool RpcServer::setupSocketServer(const RpcSocketAddress& addr) {
Steven Moreland704fc1a2021-05-04 23:13:14 +0000362 LOG_RPC_DETAIL("Setting up socket server %s", addr.toString().c_str());
Yifan Hong0eb5a672021-05-12 18:00:25 -0700363 LOG_ALWAYS_FATAL_IF(hasServer(), "Each RpcServer can only have one server.");
Steven Moreland611d15f2021-05-01 01:28:27 +0000364
365 unique_fd serverFd(
366 TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0)));
367 if (serverFd == -1) {
368 ALOGE("Could not create socket: %s", strerror(errno));
369 return false;
370 }
371
372 if (0 != TEMP_FAILURE_RETRY(bind(serverFd.get(), addr.addr(), addr.addrSize()))) {
373 int savedErrno = errno;
374 ALOGE("Could not bind socket at %s: %s", addr.toString().c_str(), strerror(savedErrno));
375 return false;
376 }
377
Yifan Honge96a1f12021-07-13 16:08:28 -0700378 // Right now, we create all threads at once, making accept4 slow. To avoid hanging the client,
379 // the backlog is increased to a large number.
380 // TODO(b/189955605): Once we create threads dynamically & lazily, the backlog can be reduced
381 // to 1.
382 if (0 != TEMP_FAILURE_RETRY(listen(serverFd.get(), 50 /*backlog*/))) {
Steven Moreland611d15f2021-05-01 01:28:27 +0000383 int savedErrno = errno;
384 ALOGE("Could not listen socket at %s: %s", addr.toString().c_str(), strerror(savedErrno));
385 return false;
386 }
387
Steven Moreland704fc1a2021-05-04 23:13:14 +0000388 LOG_RPC_DETAIL("Successfully setup socket server %s", addr.toString().c_str());
389
Yifan Hongc276f8d2021-05-13 17:13:44 -0700390 if (!setupExternalServer(std::move(serverFd))) {
391 ALOGE("Another thread has set up server while calling setupSocketServer. Race?");
392 return false;
393 }
Steven Moreland611d15f2021-05-01 01:28:27 +0000394 return true;
395}
396
Steven Morelanddd67b942021-07-23 17:15:41 -0700397void RpcServer::onSessionAllIncomingThreadsEnded(const sp<RpcSession>& session) {
Steven Morelandee78e762021-05-05 21:12:51 +0000398 auto id = session->mId;
399 LOG_ALWAYS_FATAL_IF(id == std::nullopt, "Server sessions must be initialized with ID");
Steven Moreland01a6bad2021-06-11 00:59:20 +0000400 LOG_RPC_DETAIL("Dropping session with address %s", id->toString().c_str());
Steven Morelandee78e762021-05-05 21:12:51 +0000401
402 std::lock_guard<std::mutex> _l(mLock);
403 auto it = mSessions.find(*id);
Steven Moreland01a6bad2021-06-11 00:59:20 +0000404 LOG_ALWAYS_FATAL_IF(it == mSessions.end(), "Bad state, unknown session id %s",
405 id->toString().c_str());
406 LOG_ALWAYS_FATAL_IF(it->second != session, "Bad state, session has id mismatch %s",
407 id->toString().c_str());
Steven Morelandee78e762021-05-05 21:12:51 +0000408 (void)mSessions.erase(it);
409}
410
Steven Moreland19fc9f72021-06-10 03:57:30 +0000411void RpcServer::onSessionIncomingThreadEnded() {
Steven Morelandee3f4662021-05-22 01:07:33 +0000412 mShutdownCv.notify_all();
413}
414
Yifan Hong0eb5a672021-05-12 18:00:25 -0700415bool RpcServer::hasServer() {
Yifan Hong00aeb762021-05-12 17:07:36 -0700416 LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");
Yifan Hong0eb5a672021-05-12 18:00:25 -0700417 std::lock_guard<std::mutex> _l(mLock);
418 return mServer.ok();
419}
420
Yifan Hong00aeb762021-05-12 17:07:36 -0700421unique_fd RpcServer::releaseServer() {
422 LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");
423 std::lock_guard<std::mutex> _l(mLock);
424 return std::move(mServer);
425}
426
427bool RpcServer::setupExternalServer(base::unique_fd serverFd) {
428 LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");
429 std::lock_guard<std::mutex> _l(mLock);
430 if (mServer.ok()) {
431 ALOGE("Each RpcServer can only have one server.");
432 return false;
433 }
434 mServer = std::move(serverFd);
435 return true;
436}
437
Steven Moreland5553ac42020-11-11 02:14:45 +0000438} // namespace android