blob: 62ea187719dc0abf79cc0caa4ad50bc0f7d59704 [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
Steven Moreland611d15f2021-05-01 01:28:27 +000064bool RpcServer::setupInetServer(unsigned int port, unsigned int* assignedPort) {
65 const char* kAddr = "127.0.0.1";
66
67 if (assignedPort != nullptr) *assignedPort = 0;
68 auto aiStart = InetSocketAddress::getAddrInfo(kAddr, port);
69 if (aiStart == nullptr) return false;
70 for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) {
71 InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, kAddr, port);
72 if (!setupSocketServer(socketAddress)) {
73 continue;
74 }
75
76 LOG_ALWAYS_FATAL_IF(socketAddress.addr()->sa_family != AF_INET, "expecting inet");
77 sockaddr_in addr{};
78 socklen_t len = sizeof(addr);
79 if (0 != getsockname(mServer.get(), reinterpret_cast<sockaddr*>(&addr), &len)) {
80 int savedErrno = errno;
81 ALOGE("Could not getsockname at %s: %s", socketAddress.toString().c_str(),
82 strerror(savedErrno));
83 return false;
84 }
85 LOG_ALWAYS_FATAL_IF(len != sizeof(addr), "Wrong socket type: len %zu vs len %zu",
86 static_cast<size_t>(len), sizeof(addr));
87 unsigned int realPort = ntohs(addr.sin_port);
88 LOG_ALWAYS_FATAL_IF(port != 0 && realPort != port,
89 "Requesting inet server on %s but it is set up on %u.",
90 socketAddress.toString().c_str(), realPort);
91
92 if (assignedPort != nullptr) {
93 *assignedPort = realPort;
94 }
95
96 return true;
97 }
98 ALOGE("None of the socket address resolved for %s:%u can be set up as inet server.", kAddr,
99 port);
100 return false;
101}
102
Steven Morelandf137de92021-04-24 01:54:26 +0000103void RpcServer::setMaxThreads(size_t threads) {
104 LOG_ALWAYS_FATAL_IF(threads <= 0, "RpcServer is useless without threads");
Yifan Hong1a235852021-05-13 16:07:47 -0700105 LOG_ALWAYS_FATAL_IF(mJoinThreadRunning, "Cannot set max threads while running");
Steven Morelandf137de92021-04-24 01:54:26 +0000106 mMaxThreads = threads;
107}
108
109size_t RpcServer::getMaxThreads() {
110 return mMaxThreads;
Steven Moreland5553ac42020-11-11 02:14:45 +0000111}
112
Steven Morelandbf57bce2021-07-26 15:26:12 -0700113void RpcServer::setProtocolVersion(uint32_t version) {
114 mProtocolVersion = version;
115}
116
Steven Moreland5553ac42020-11-11 02:14:45 +0000117void RpcServer::setRootObject(const sp<IBinder>& binder) {
Steven Morelandebafe332021-04-24 00:24:35 +0000118 std::lock_guard<std::mutex> _l(mLock);
Yifan Hong4ffb0c72021-05-07 18:35:14 -0700119 mRootObjectWeak = mRootObject = binder;
120}
121
122void RpcServer::setRootObjectWeak(const wp<IBinder>& binder) {
123 std::lock_guard<std::mutex> _l(mLock);
124 mRootObject.clear();
125 mRootObjectWeak = binder;
Steven Moreland5553ac42020-11-11 02:14:45 +0000126}
127
128sp<IBinder> RpcServer::getRootObject() {
Steven Morelandebafe332021-04-24 00:24:35 +0000129 std::lock_guard<std::mutex> _l(mLock);
Yifan Hong4ffb0c72021-05-07 18:35:14 -0700130 bool hasWeak = mRootObjectWeak.unsafe_get();
131 sp<IBinder> ret = mRootObjectWeak.promote();
132 ALOGW_IF(hasWeak && ret == nullptr, "RpcServer root object is freed, returning nullptr");
133 return ret;
Steven Moreland5553ac42020-11-11 02:14:45 +0000134}
135
Yifan Hong326afd12021-05-19 15:24:54 -0700136static void joinRpcServer(sp<RpcServer>&& thiz) {
137 thiz->join();
138}
139
140void RpcServer::start() {
141 LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");
142 std::lock_guard<std::mutex> _l(mLock);
143 LOG_ALWAYS_FATAL_IF(mJoinThread.get(), "Already started!");
144 mJoinThread = std::make_unique<std::thread>(&joinRpcServer, sp<RpcServer>::fromExisting(this));
145}
146
Steven Moreland611d15f2021-05-01 01:28:27 +0000147void RpcServer::join() {
Yifan Hong1a235852021-05-13 16:07:47 -0700148 LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");
149
150 {
151 std::lock_guard<std::mutex> _l(mLock);
152 LOG_ALWAYS_FATAL_IF(!mServer.ok(), "RpcServer must be setup to join.");
153 LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr, "Already joined");
154 mJoinThreadRunning = true;
Steven Morelande47511f2021-05-20 00:07:41 +0000155 mShutdownTrigger = RpcSession::FdTrigger::make();
Yifan Hong1a235852021-05-13 16:07:47 -0700156 LOG_ALWAYS_FATAL_IF(mShutdownTrigger == nullptr, "Cannot create join signaler");
Steven Morelandd539fbf2021-05-05 23:40:25 +0000157 }
Yifan Hong1a235852021-05-13 16:07:47 -0700158
Steven Moreland2b4f3802021-05-22 01:46:27 +0000159 status_t status;
Steven Moreland798e0d12021-07-14 23:19:25 +0000160 while ((status = mShutdownTrigger->triggerablePoll(mServer, POLLIN)) == OK) {
Steven Moreland410325a2021-06-02 18:37:42 +0000161 unique_fd clientFd(TEMP_FAILURE_RETRY(
162 accept4(mServer.get(), nullptr, nullptr /*length*/, SOCK_CLOEXEC)));
163
164 if (clientFd < 0) {
165 ALOGE("Could not accept4 socket: %s", strerror(errno));
166 continue;
167 }
168 LOG_RPC_DETAIL("accept4 on fd %d yields fd %d", mServer.get(), clientFd.get());
169
170 {
171 std::lock_guard<std::mutex> _l(mLock);
172 std::thread thread =
173 std::thread(&RpcServer::establishConnection, sp<RpcServer>::fromExisting(this),
174 std::move(clientFd));
175 mConnectingThreads[thread.get_id()] = std::move(thread);
176 }
Yifan Hong1a235852021-05-13 16:07:47 -0700177 }
Steven Moreland2b4f3802021-05-22 01:46:27 +0000178 LOG_RPC_DETAIL("RpcServer::join exiting with %s", statusToString(status).c_str());
Yifan Hong1a235852021-05-13 16:07:47 -0700179
180 {
181 std::lock_guard<std::mutex> _l(mLock);
182 mJoinThreadRunning = false;
183 }
184 mShutdownCv.notify_all();
Steven Morelandd539fbf2021-05-05 23:40:25 +0000185}
186
Yifan Hong1a235852021-05-13 16:07:47 -0700187bool RpcServer::shutdown() {
Yifan Hong1a235852021-05-13 16:07:47 -0700188 std::unique_lock<std::mutex> _l(mLock);
Steven Moreland9d11b922021-05-20 01:22:58 +0000189 if (mShutdownTrigger == nullptr) {
Steven Moreland1c943ec2021-07-13 23:57:56 +0000190 LOG_RPC_DETAIL("Cannot shutdown. No shutdown trigger installed (already shutdown?)");
Steven Moreland9d11b922021-05-20 01:22:58 +0000191 return false;
192 }
Yifan Hong1a235852021-05-13 16:07:47 -0700193
194 mShutdownTrigger->trigger();
Steven Morelanda8b44292021-06-08 01:27:53 +0000195 for (auto& [id, session] : mSessions) {
196 (void)id;
197 session->mShutdownTrigger->trigger();
198 }
199
Steven Morelandee3f4662021-05-22 01:07:33 +0000200 while (mJoinThreadRunning || !mConnectingThreads.empty() || !mSessions.empty()) {
Steven Morelandaf4ca712021-05-24 23:22:08 +0000201 if (std::cv_status::timeout == mShutdownCv.wait_for(_l, std::chrono::seconds(1))) {
202 ALOGE("Waiting for RpcServer to shut down (1s w/o progress). Join thread running: %d, "
203 "Connecting threads: "
204 "%zu, Sessions: %zu. Is your server deadlocked?",
205 mJoinThreadRunning, mConnectingThreads.size(), mSessions.size());
206 }
Steven Moreland9d11b922021-05-20 01:22:58 +0000207 }
Yifan Hong1a235852021-05-13 16:07:47 -0700208
Yifan Hong326afd12021-05-19 15:24:54 -0700209 // At this point, we know join() is about to exit, but the thread that calls
210 // join() may not have exited yet.
211 // If RpcServer owns the join thread (aka start() is called), make sure the thread exits;
212 // otherwise ~thread() may call std::terminate(), which may crash the process.
213 // If RpcServer does not own the join thread (aka join() is called directly),
214 // then the owner of RpcServer is responsible for cleaning up that thread.
215 if (mJoinThread.get()) {
216 mJoinThread->join();
217 mJoinThread.reset();
218 }
219
Steven Moreland1c943ec2021-07-13 23:57:56 +0000220 LOG_RPC_DETAIL("Finished waiting on shutdown.");
221
Yifan Hong1a235852021-05-13 16:07:47 -0700222 mShutdownTrigger = nullptr;
223 return true;
224}
225
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000226std::vector<sp<RpcSession>> RpcServer::listSessions() {
Steven Moreland611d15f2021-05-01 01:28:27 +0000227 std::lock_guard<std::mutex> _l(mLock);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000228 std::vector<sp<RpcSession>> sessions;
229 for (auto& [id, session] : mSessions) {
Steven Moreland736664b2021-05-01 04:27:25 +0000230 (void)id;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000231 sessions.push_back(session);
Steven Moreland736664b2021-05-01 04:27:25 +0000232 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000233 return sessions;
Steven Moreland611d15f2021-05-01 01:28:27 +0000234}
235
Steven Morelandd539fbf2021-05-05 23:40:25 +0000236size_t RpcServer::numUninitializedSessions() {
237 std::lock_guard<std::mutex> _l(mLock);
238 return mConnectingThreads.size();
239}
240
Steven Morelanda63ff932021-05-12 00:03:15 +0000241void RpcServer::establishConnection(sp<RpcServer>&& server, base::unique_fd clientFd) {
Steven Morelanda63ff932021-05-12 00:03:15 +0000242 // TODO(b/183988761): cannot trust this simple ID
Yifan Hongb3005502021-05-19 15:37:00 -0700243 LOG_ALWAYS_FATAL_IF(!server->mAgreedExperimental, "no!");
Steven Moreland9d11b922021-05-20 01:22:58 +0000244
245 // mShutdownTrigger can only be cleared once connection threads have joined.
246 // It must be set before this thread is started
247 LOG_ALWAYS_FATAL_IF(server->mShutdownTrigger == nullptr);
248
Steven Moreland659416d2021-05-11 00:47:50 +0000249 RpcConnectionHeader header;
250 status_t status = server->mShutdownTrigger->interruptableReadFully(clientFd.get(), &header,
251 sizeof(header));
Steven Morelandbf57bce2021-07-26 15:26:12 -0700252 if (status != OK) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000253 ALOGE("Failed to read ID for client connecting to RPC server: %s",
254 statusToString(status).c_str());
255 // still need to cleanup before we can return
Steven Morelanda63ff932021-05-12 00:03:15 +0000256 }
Steven Morelandbf57bce2021-07-26 15:26:12 -0700257
258 bool incoming = false;
259 uint32_t protocolVersion = 0;
260 RpcAddress sessionId = RpcAddress::zero();
261 bool requestingNewSession = false;
262
263 if (status == OK) {
264 incoming = header.options & RPC_CONNECTION_OPTION_INCOMING;
265 protocolVersion = std::min(header.version,
266 server->mProtocolVersion.value_or(RPC_WIRE_PROTOCOL_VERSION));
267 sessionId = RpcAddress::fromRawEmbedded(&header.sessionId);
268 requestingNewSession = sessionId.isZero();
269
270 if (requestingNewSession) {
271 RpcNewSessionResponse response{
272 .version = protocolVersion,
273 };
274
275 status = server->mShutdownTrigger->interruptableWriteFully(clientFd.get(), &response,
276 sizeof(response));
277 if (status != OK) {
278 ALOGE("Failed to send new session response: %s", statusToString(status).c_str());
279 // still need to cleanup before we can return
280 }
281 }
282 }
Steven Morelanda63ff932021-05-12 00:03:15 +0000283
284 std::thread thisThread;
285 sp<RpcSession> session;
286 {
Steven Moreland9d11b922021-05-20 01:22:58 +0000287 std::unique_lock<std::mutex> _l(server->mLock);
Steven Morelanda63ff932021-05-12 00:03:15 +0000288
Yifan Hongb3005502021-05-19 15:37:00 -0700289 auto threadId = server->mConnectingThreads.find(std::this_thread::get_id());
290 LOG_ALWAYS_FATAL_IF(threadId == server->mConnectingThreads.end(),
Steven Morelanda63ff932021-05-12 00:03:15 +0000291 "Must establish connection on owned thread");
292 thisThread = std::move(threadId->second);
Steven Morelandadc5dca2021-05-25 02:06:03 +0000293 ScopeGuard detachGuard = [&]() {
294 thisThread.detach();
Steven Moreland9d11b922021-05-20 01:22:58 +0000295 _l.unlock();
296 server->mShutdownCv.notify_all();
297 };
Steven Morelandadc5dca2021-05-25 02:06:03 +0000298 server->mConnectingThreads.erase(threadId);
Steven Moreland9d11b922021-05-20 01:22:58 +0000299
Steven Morelandbf57bce2021-07-26 15:26:12 -0700300 if (status != OK || server->mShutdownTrigger->isTriggered()) {
Steven Moreland5802c2b2021-05-12 20:13:04 +0000301 return;
302 }
303
Steven Morelandbf57bce2021-07-26 15:26:12 -0700304 if (requestingNewSession) {
Steven Moreland1b304292021-07-15 22:59:34 +0000305 if (incoming) {
306 ALOGE("Cannot create a new session with an incoming connection, would leak");
Steven Moreland659416d2021-05-11 00:47:50 +0000307 return;
308 }
309
Steven Moreland01a6bad2021-06-11 00:59:20 +0000310 size_t tries = 0;
311 do {
312 // don't block if there is some entropy issue
313 if (tries++ > 5) {
314 ALOGE("Cannot find new address: %s", sessionId.toString().c_str());
315 return;
316 }
317
318 sessionId = RpcAddress::random(true /*forServer*/);
319 } while (server->mSessions.end() != server->mSessions.find(sessionId));
Steven Morelanda63ff932021-05-12 00:03:15 +0000320
321 session = RpcSession::make();
Steven Moreland103424e2021-06-02 18:16:19 +0000322 session->setMaxThreads(server->mMaxThreads);
Steven Morelandbf57bce2021-07-26 15:26:12 -0700323 if (!session->setProtocolVersion(protocolVersion)) return;
Steven Morelanda8b44292021-06-08 01:27:53 +0000324 if (!session->setForServer(server,
325 sp<RpcServer::EventListener>::fromExisting(
326 static_cast<RpcServer::EventListener*>(
327 server.get())),
Steven Moreland01a6bad2021-06-11 00:59:20 +0000328 sessionId)) {
Steven Morelanda8b44292021-06-08 01:27:53 +0000329 ALOGE("Failed to attach server to session");
330 return;
331 }
Steven Morelanda63ff932021-05-12 00:03:15 +0000332
Steven Moreland01a6bad2021-06-11 00:59:20 +0000333 server->mSessions[sessionId] = session;
Steven Morelanda63ff932021-05-12 00:03:15 +0000334 } else {
Steven Moreland01a6bad2021-06-11 00:59:20 +0000335 auto it = server->mSessions.find(sessionId);
Yifan Hongb3005502021-05-19 15:37:00 -0700336 if (it == server->mSessions.end()) {
Steven Moreland01a6bad2021-06-11 00:59:20 +0000337 ALOGE("Cannot add thread, no record of session with ID %s",
338 sessionId.toString().c_str());
Steven Morelanda63ff932021-05-12 00:03:15 +0000339 return;
340 }
341 session = it->second;
342 }
Steven Moreland5802c2b2021-05-12 20:13:04 +0000343
Steven Moreland1b304292021-07-15 22:59:34 +0000344 if (incoming) {
Steven Morelandb86e26b2021-06-12 00:35:58 +0000345 LOG_ALWAYS_FATAL_IF(!session->addOutgoingConnection(std::move(clientFd), true),
Steven Moreland659416d2021-05-11 00:47:50 +0000346 "server state must already be initialized");
347 return;
348 }
349
Steven Moreland5802c2b2021-05-12 20:13:04 +0000350 detachGuard.Disable();
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000351 session->preJoinThreadOwnership(std::move(thisThread));
Steven Morelanda63ff932021-05-12 00:03:15 +0000352 }
353
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000354 auto setupResult = session->preJoinSetup(std::move(clientFd));
355
Steven Morelanda63ff932021-05-12 00:03:15 +0000356 // avoid strong cycle
357 server = nullptr;
Steven Morelanda63ff932021-05-12 00:03:15 +0000358
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000359 RpcSession::join(std::move(session), std::move(setupResult));
Steven Morelanda63ff932021-05-12 00:03:15 +0000360}
361
Steven Moreland611d15f2021-05-01 01:28:27 +0000362bool RpcServer::setupSocketServer(const RpcSocketAddress& addr) {
Steven Moreland704fc1a2021-05-04 23:13:14 +0000363 LOG_RPC_DETAIL("Setting up socket server %s", addr.toString().c_str());
Yifan Hong0eb5a672021-05-12 18:00:25 -0700364 LOG_ALWAYS_FATAL_IF(hasServer(), "Each RpcServer can only have one server.");
Steven Moreland611d15f2021-05-01 01:28:27 +0000365
366 unique_fd serverFd(
367 TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0)));
368 if (serverFd == -1) {
369 ALOGE("Could not create socket: %s", strerror(errno));
370 return false;
371 }
372
373 if (0 != TEMP_FAILURE_RETRY(bind(serverFd.get(), addr.addr(), addr.addrSize()))) {
374 int savedErrno = errno;
375 ALOGE("Could not bind socket at %s: %s", addr.toString().c_str(), strerror(savedErrno));
376 return false;
377 }
378
Yifan Honge96a1f12021-07-13 16:08:28 -0700379 // Right now, we create all threads at once, making accept4 slow. To avoid hanging the client,
380 // the backlog is increased to a large number.
381 // TODO(b/189955605): Once we create threads dynamically & lazily, the backlog can be reduced
382 // to 1.
383 if (0 != TEMP_FAILURE_RETRY(listen(serverFd.get(), 50 /*backlog*/))) {
Steven Moreland611d15f2021-05-01 01:28:27 +0000384 int savedErrno = errno;
385 ALOGE("Could not listen socket at %s: %s", addr.toString().c_str(), strerror(savedErrno));
386 return false;
387 }
388
Steven Moreland704fc1a2021-05-04 23:13:14 +0000389 LOG_RPC_DETAIL("Successfully setup socket server %s", addr.toString().c_str());
390
Yifan Hongc276f8d2021-05-13 17:13:44 -0700391 if (!setupExternalServer(std::move(serverFd))) {
392 ALOGE("Another thread has set up server while calling setupSocketServer. Race?");
393 return false;
394 }
Steven Moreland611d15f2021-05-01 01:28:27 +0000395 return true;
396}
397
Steven Morelanddd67b942021-07-23 17:15:41 -0700398void RpcServer::onSessionAllIncomingThreadsEnded(const sp<RpcSession>& session) {
Steven Morelandee78e762021-05-05 21:12:51 +0000399 auto id = session->mId;
400 LOG_ALWAYS_FATAL_IF(id == std::nullopt, "Server sessions must be initialized with ID");
Steven Moreland01a6bad2021-06-11 00:59:20 +0000401 LOG_RPC_DETAIL("Dropping session with address %s", id->toString().c_str());
Steven Morelandee78e762021-05-05 21:12:51 +0000402
403 std::lock_guard<std::mutex> _l(mLock);
404 auto it = mSessions.find(*id);
Steven Moreland01a6bad2021-06-11 00:59:20 +0000405 LOG_ALWAYS_FATAL_IF(it == mSessions.end(), "Bad state, unknown session id %s",
406 id->toString().c_str());
407 LOG_ALWAYS_FATAL_IF(it->second != session, "Bad state, session has id mismatch %s",
408 id->toString().c_str());
Steven Morelandee78e762021-05-05 21:12:51 +0000409 (void)mSessions.erase(it);
410}
411
Steven Moreland19fc9f72021-06-10 03:57:30 +0000412void RpcServer::onSessionIncomingThreadEnded() {
Steven Morelandee3f4662021-05-22 01:07:33 +0000413 mShutdownCv.notify_all();
414}
415
Yifan Hong0eb5a672021-05-12 18:00:25 -0700416bool RpcServer::hasServer() {
Yifan Hong00aeb762021-05-12 17:07:36 -0700417 LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");
Yifan Hong0eb5a672021-05-12 18:00:25 -0700418 std::lock_guard<std::mutex> _l(mLock);
419 return mServer.ok();
420}
421
Yifan Hong00aeb762021-05-12 17:07:36 -0700422unique_fd RpcServer::releaseServer() {
423 LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");
424 std::lock_guard<std::mutex> _l(mLock);
425 return std::move(mServer);
426}
427
428bool RpcServer::setupExternalServer(base::unique_fd serverFd) {
429 LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");
430 std::lock_guard<std::mutex> _l(mLock);
431 if (mServer.ok()) {
432 ALOGE("Each RpcServer can only have one server.");
433 return false;
434 }
435 mServer = std::move(serverFd);
436 return true;
437}
438
Steven Moreland5553ac42020-11-11 02:14:45 +0000439} // namespace android