blob: a759ae36c370c0492293863b7b9fb0d25ac5999a [file] [log] [blame]
Steven Morelandbdb53ab2021-05-05 17:57:41 +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 "RpcSession"
18
19#include <binder/RpcSession.h>
20
21#include <inttypes.h>
Steven Moreland4ec3c432021-05-20 00:32:47 +000022#include <poll.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000023#include <unistd.h>
24
25#include <string_view>
26
Steven Moreland4ec3c432021-05-20 00:32:47 +000027#include <android-base/macros.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000028#include <binder/Parcel.h>
Steven Morelandee78e762021-05-05 21:12:51 +000029#include <binder/RpcServer.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000030#include <binder/Stability.h>
31#include <utils/String8.h>
32
33#include "RpcSocketAddress.h"
34#include "RpcState.h"
35#include "RpcWireFormat.h"
36
37#ifdef __GLIBC__
38extern "C" pid_t gettid();
39#endif
40
41namespace android {
42
43using base::unique_fd;
44
45RpcSession::RpcSession() {
46 LOG_RPC_DETAIL("RpcSession created %p", this);
47
48 mState = std::make_unique<RpcState>();
49}
50RpcSession::~RpcSession() {
51 LOG_RPC_DETAIL("RpcSession destroyed %p", this);
52
53 std::lock_guard<std::mutex> _l(mMutex);
Steven Morelandbb543a82021-05-11 02:31:50 +000054 LOG_ALWAYS_FATAL_IF(mServerConnections.size() != 0,
Steven Morelandbdb53ab2021-05-05 17:57:41 +000055 "Should not be able to destroy a session with servers in use.");
56}
57
58sp<RpcSession> RpcSession::make() {
59 return sp<RpcSession>::make();
60}
61
Steven Moreland103424e2021-06-02 18:16:19 +000062void RpcSession::setMaxThreads(size_t threads) {
63 std::lock_guard<std::mutex> _l(mMutex);
64 LOG_ALWAYS_FATAL_IF(!mClientConnections.empty() || !mServerConnections.empty(),
65 "Must set max threads before setting up connections, but has %zu client(s) "
66 "and %zu server(s)",
67 mClientConnections.size(), mServerConnections.size());
68 mMaxThreads = threads;
69}
70
71size_t RpcSession::getMaxThreads() {
72 std::lock_guard<std::mutex> _l(mMutex);
73 return mMaxThreads;
Steven Moreland659416d2021-05-11 00:47:50 +000074}
75
Steven Morelandbdb53ab2021-05-05 17:57:41 +000076bool RpcSession::setupUnixDomainClient(const char* path) {
77 return setupSocketClient(UnixSocketAddress(path));
78}
79
Steven Morelandbdb53ab2021-05-05 17:57:41 +000080bool RpcSession::setupVsockClient(unsigned int cid, unsigned int port) {
81 return setupSocketClient(VsockSocketAddress(cid, port));
82}
83
Steven Morelandbdb53ab2021-05-05 17:57:41 +000084bool RpcSession::setupInetClient(const char* addr, unsigned int port) {
85 auto aiStart = InetSocketAddress::getAddrInfo(addr, port);
86 if (aiStart == nullptr) return false;
87 for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) {
88 InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, addr, port);
89 if (setupSocketClient(socketAddress)) return true;
90 }
91 ALOGE("None of the socket address resolved for %s:%u can be added as inet client.", addr, port);
92 return false;
93}
94
95bool RpcSession::addNullDebuggingClient() {
96 unique_fd serverFd(TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY | O_CLOEXEC)));
97
98 if (serverFd == -1) {
99 ALOGE("Could not connect to /dev/null: %s", strerror(errno));
100 return false;
101 }
102
Steven Morelandb0dd1182021-05-25 01:56:46 +0000103 return addClientConnection(std::move(serverFd));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000104}
105
106sp<IBinder> RpcSession::getRootObject() {
Steven Moreland195edb82021-06-08 02:44:39 +0000107 ExclusiveConnection connection;
108 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
109 ConnectionUse::CLIENT, &connection);
110 if (status != OK) return nullptr;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000111 return state()->getRootObject(connection.fd(), sp<RpcSession>::fromExisting(this));
112}
113
Steven Moreland1be91352021-05-11 22:12:15 +0000114status_t RpcSession::getRemoteMaxThreads(size_t* maxThreads) {
Steven Moreland195edb82021-06-08 02:44:39 +0000115 ExclusiveConnection connection;
116 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
117 ConnectionUse::CLIENT, &connection);
118 if (status != OK) return status;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000119 return state()->getMaxThreads(connection.fd(), sp<RpcSession>::fromExisting(this), maxThreads);
120}
121
Steven Morelandc9d7b532021-06-04 20:57:41 +0000122bool RpcSession::shutdownAndWait(bool wait) {
Steven Moreland659416d2021-05-11 00:47:50 +0000123 std::unique_lock<std::mutex> _l(mMutex);
Steven Moreland659416d2021-05-11 00:47:50 +0000124 LOG_ALWAYS_FATAL_IF(mShutdownTrigger == nullptr, "Shutdown trigger not installed");
Steven Moreland659416d2021-05-11 00:47:50 +0000125
126 mShutdownTrigger->trigger();
Steven Moreland659416d2021-05-11 00:47:50 +0000127
Steven Morelandc9d7b532021-06-04 20:57:41 +0000128 if (wait) {
129 LOG_ALWAYS_FATAL_IF(mShutdownListener == nullptr, "Shutdown listener not installed");
130 mShutdownListener->waitForShutdown(_l);
131 LOG_ALWAYS_FATAL_IF(!mThreads.empty(), "Shutdown failed");
132 }
133
134 _l.unlock();
135 mState->clear();
136
Steven Moreland659416d2021-05-11 00:47:50 +0000137 return true;
138}
139
Steven Morelandf5174272021-05-25 00:39:28 +0000140status_t RpcSession::transact(const sp<IBinder>& binder, uint32_t code, const Parcel& data,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000141 Parcel* reply, uint32_t flags) {
Steven Moreland195edb82021-06-08 02:44:39 +0000142 ExclusiveConnection connection;
143 status_t status =
144 ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
145 (flags & IBinder::FLAG_ONEWAY) ? ConnectionUse::CLIENT_ASYNC
146 : ConnectionUse::CLIENT,
147 &connection);
148 if (status != OK) return status;
Steven Morelandf5174272021-05-25 00:39:28 +0000149 return state()->transact(connection.fd(), binder, code, data,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000150 sp<RpcSession>::fromExisting(this), reply, flags);
151}
152
153status_t RpcSession::sendDecStrong(const RpcAddress& address) {
Steven Moreland195edb82021-06-08 02:44:39 +0000154 ExclusiveConnection connection;
155 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
156 ConnectionUse::CLIENT_REFCOUNT, &connection);
157 if (status != OK) return status;
Steven Morelandc9d7b532021-06-04 20:57:41 +0000158 return state()->sendDecStrong(connection.fd(), sp<RpcSession>::fromExisting(this), address);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000159}
160
Steven Morelande47511f2021-05-20 00:07:41 +0000161std::unique_ptr<RpcSession::FdTrigger> RpcSession::FdTrigger::make() {
162 auto ret = std::make_unique<RpcSession::FdTrigger>();
Steven Morelanda8b44292021-06-08 01:27:53 +0000163 if (!android::base::Pipe(&ret->mRead, &ret->mWrite)) {
164 ALOGE("Could not create pipe %s", strerror(errno));
165 return nullptr;
166 }
Steven Morelande47511f2021-05-20 00:07:41 +0000167 return ret;
168}
169
170void RpcSession::FdTrigger::trigger() {
171 mWrite.reset();
172}
173
Steven Morelanda8b44292021-06-08 01:27:53 +0000174bool RpcSession::FdTrigger::isTriggered() {
175 return mWrite == -1;
176}
177
Steven Moreland2b4f3802021-05-22 01:46:27 +0000178status_t RpcSession::FdTrigger::triggerablePollRead(base::borrowed_fd fd) {
Steven Moreland4ec3c432021-05-20 00:32:47 +0000179 while (true) {
Steven Morelanddfe3be92021-05-22 00:24:29 +0000180 pollfd pfd[]{{.fd = fd.get(), .events = POLLIN | POLLHUP, .revents = 0},
Steven Moreland4ec3c432021-05-20 00:32:47 +0000181 {.fd = mRead.get(), .events = POLLHUP, .revents = 0}};
182 int ret = TEMP_FAILURE_RETRY(poll(pfd, arraysize(pfd), -1));
183 if (ret < 0) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000184 return -errno;
Steven Moreland4ec3c432021-05-20 00:32:47 +0000185 }
186 if (ret == 0) {
187 continue;
188 }
189 if (pfd[1].revents & POLLHUP) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000190 return -ECANCELED;
Steven Moreland4ec3c432021-05-20 00:32:47 +0000191 }
Steven Moreland2b4f3802021-05-22 01:46:27 +0000192 return pfd[0].revents & POLLIN ? OK : DEAD_OBJECT;
Steven Moreland4ec3c432021-05-20 00:32:47 +0000193 }
194}
195
Steven Moreland2b4f3802021-05-22 01:46:27 +0000196status_t RpcSession::FdTrigger::interruptableReadFully(base::borrowed_fd fd, void* data,
197 size_t size) {
Steven Moreland9d11b922021-05-20 01:22:58 +0000198 uint8_t* buffer = reinterpret_cast<uint8_t*>(data);
199 uint8_t* end = buffer + size;
200
Steven Moreland2b4f3802021-05-22 01:46:27 +0000201 status_t status;
202 while ((status = triggerablePollRead(fd)) == OK) {
Steven Moreland9d11b922021-05-20 01:22:58 +0000203 ssize_t readSize = TEMP_FAILURE_RETRY(recv(fd.get(), buffer, end - buffer, MSG_NOSIGNAL));
Steven Moreland2b4f3802021-05-22 01:46:27 +0000204 if (readSize == 0) return DEAD_OBJECT; // EOF
Steven Morelanddfe3be92021-05-22 00:24:29 +0000205
Steven Moreland9d11b922021-05-20 01:22:58 +0000206 if (readSize < 0) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000207 return -errno;
Steven Moreland9d11b922021-05-20 01:22:58 +0000208 }
209 buffer += readSize;
Steven Moreland2b4f3802021-05-22 01:46:27 +0000210 if (buffer == end) return OK;
Steven Moreland9d11b922021-05-20 01:22:58 +0000211 }
Steven Moreland2b4f3802021-05-22 01:46:27 +0000212 return status;
Steven Moreland9d11b922021-05-20 01:22:58 +0000213}
214
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000215status_t RpcSession::readId() {
216 {
217 std::lock_guard<std::mutex> _l(mMutex);
218 LOG_ALWAYS_FATAL_IF(mForServer != nullptr, "Can only update ID for client.");
219 }
220
221 int32_t id;
222
Steven Moreland195edb82021-06-08 02:44:39 +0000223 ExclusiveConnection connection;
224 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
225 ConnectionUse::CLIENT, &connection);
226 if (status != OK) return status;
227
228 status = state()->getSessionId(connection.fd(), sp<RpcSession>::fromExisting(this), &id);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000229 if (status != OK) return status;
230
231 LOG_RPC_DETAIL("RpcSession %p has id %d", this, id);
232 mId = id;
233 return OK;
234}
235
Steven Moreland659416d2021-05-11 00:47:50 +0000236void RpcSession::WaitForShutdownListener::onSessionLockedAllServerThreadsEnded(
237 const sp<RpcSession>& session) {
238 (void)session;
239 mShutdown = true;
240}
241
242void RpcSession::WaitForShutdownListener::onSessionServerThreadEnded() {
243 mCv.notify_all();
244}
245
246void RpcSession::WaitForShutdownListener::waitForShutdown(std::unique_lock<std::mutex>& lock) {
247 while (!mShutdown) {
248 if (std::cv_status::timeout == mCv.wait_for(lock, std::chrono::seconds(1))) {
249 ALOGE("Waiting for RpcSession to shut down (1s w/o progress).");
250 }
251 }
252}
253
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000254void RpcSession::preJoinThreadOwnership(std::thread thread) {
Steven Morelanda63ff932021-05-12 00:03:15 +0000255 LOG_ALWAYS_FATAL_IF(thread.get_id() != std::this_thread::get_id(), "Must own this thread");
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000256
Steven Morelanda63ff932021-05-12 00:03:15 +0000257 {
258 std::lock_guard<std::mutex> _l(mMutex);
259 mThreads[thread.get_id()] = std::move(thread);
260 }
Steven Moreland5802c2b2021-05-12 20:13:04 +0000261}
Steven Morelanda63ff932021-05-12 00:03:15 +0000262
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000263RpcSession::PreJoinSetupResult RpcSession::preJoinSetup(base::unique_fd fd) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000264 // must be registered to allow arbitrary client code executing commands to
265 // be able to do nested calls (we can't only read from it)
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000266 sp<RpcConnection> connection = assignServerToThisThread(std::move(fd));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000267
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000268 status_t status =
269 mState->readConnectionInit(connection->fd, sp<RpcSession>::fromExisting(this));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000270
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000271 return PreJoinSetupResult{
272 .connection = std::move(connection),
273 .status = status,
274 };
275}
276
277void RpcSession::join(sp<RpcSession>&& session, PreJoinSetupResult&& setupResult) {
278 sp<RpcConnection>& connection = setupResult.connection;
279
280 if (setupResult.status == OK) {
281 while (true) {
282 status_t status = session->state()->getAndExecuteCommand(connection->fd, session,
283 RpcState::CommandType::ANY);
284 if (status != OK) {
285 LOG_RPC_DETAIL("Binder connection thread closing w/ status %s",
286 statusToString(status).c_str());
287 break;
288 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000289 }
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000290 } else {
291 ALOGE("Connection failed to init, closing with status %s",
292 statusToString(setupResult.status).c_str());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000293 }
294
Steven Moreland659416d2021-05-11 00:47:50 +0000295 LOG_ALWAYS_FATAL_IF(!session->removeServerConnection(connection),
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000296 "bad state: connection object guaranteed to be in list");
Steven Morelanda63ff932021-05-12 00:03:15 +0000297
Steven Moreland659416d2021-05-11 00:47:50 +0000298 sp<RpcSession::EventListener> listener;
Steven Morelanda63ff932021-05-12 00:03:15 +0000299 {
Steven Moreland659416d2021-05-11 00:47:50 +0000300 std::lock_guard<std::mutex> _l(session->mMutex);
301 auto it = session->mThreads.find(std::this_thread::get_id());
302 LOG_ALWAYS_FATAL_IF(it == session->mThreads.end());
Steven Morelanda63ff932021-05-12 00:03:15 +0000303 it->second.detach();
Steven Moreland659416d2021-05-11 00:47:50 +0000304 session->mThreads.erase(it);
Steven Morelandee3f4662021-05-22 01:07:33 +0000305
Steven Moreland659416d2021-05-11 00:47:50 +0000306 listener = session->mEventListener.promote();
Steven Morelandee3f4662021-05-22 01:07:33 +0000307 }
308
Steven Moreland659416d2021-05-11 00:47:50 +0000309 session = nullptr;
310
311 if (listener != nullptr) {
312 listener->onSessionServerThreadEnded();
Steven Morelandee78e762021-05-05 21:12:51 +0000313 }
314}
315
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000316wp<RpcServer> RpcSession::server() {
317 return mForServer;
318}
319
320bool RpcSession::setupSocketClient(const RpcSocketAddress& addr) {
321 {
322 std::lock_guard<std::mutex> _l(mMutex);
Steven Morelandbb543a82021-05-11 02:31:50 +0000323 LOG_ALWAYS_FATAL_IF(mClientConnections.size() != 0,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000324 "Must only setup session once, but already has %zu clients",
Steven Morelandbb543a82021-05-11 02:31:50 +0000325 mClientConnections.size());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000326 }
327
Steven Moreland659416d2021-05-11 00:47:50 +0000328 if (!setupOneSocketConnection(addr, RPC_SESSION_ID_NEW, false /*reverse*/)) return false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000329
Steven Morelanda5036f02021-06-08 02:26:57 +0000330 // TODO(b/189955605): we should add additional sessions dynamically
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000331 // instead of all at once.
332 // TODO(b/186470974): first risk of blocking
333 size_t numThreadsAvailable;
Steven Moreland1be91352021-05-11 22:12:15 +0000334 if (status_t status = getRemoteMaxThreads(&numThreadsAvailable); status != OK) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000335 ALOGE("Could not get max threads after initial session to %s: %s", addr.toString().c_str(),
336 statusToString(status).c_str());
337 return false;
338 }
339
340 if (status_t status = readId(); status != OK) {
341 ALOGE("Could not get session id after initial session to %s; %s", addr.toString().c_str(),
342 statusToString(status).c_str());
343 return false;
344 }
345
346 // we've already setup one client
347 for (size_t i = 0; i + 1 < numThreadsAvailable; i++) {
Steven Morelanda5036f02021-06-08 02:26:57 +0000348 // TODO(b/189955605): shutdown existing connections?
Steven Moreland659416d2021-05-11 00:47:50 +0000349 if (!setupOneSocketConnection(addr, mId.value(), false /*reverse*/)) return false;
350 }
351
Steven Morelanda5036f02021-06-08 02:26:57 +0000352 // TODO(b/189955605): we should add additional sessions dynamically
Steven Moreland659416d2021-05-11 00:47:50 +0000353 // instead of all at once - the other side should be responsible for setting
354 // up additional connections. We need to create at least one (unless 0 are
355 // requested to be set) in order to allow the other side to reliably make
356 // any requests at all.
357
Steven Moreland103424e2021-06-02 18:16:19 +0000358 for (size_t i = 0; i < mMaxThreads; i++) {
Steven Moreland659416d2021-05-11 00:47:50 +0000359 if (!setupOneSocketConnection(addr, mId.value(), true /*reverse*/)) return false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000360 }
361
362 return true;
363}
364
Steven Moreland659416d2021-05-11 00:47:50 +0000365bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, int32_t id, bool reverse) {
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000366 for (size_t tries = 0; tries < 5; tries++) {
367 if (tries > 0) usleep(10000);
368
369 unique_fd serverFd(
370 TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0)));
371 if (serverFd == -1) {
372 int savedErrno = errno;
373 ALOGE("Could not create socket at %s: %s", addr.toString().c_str(),
374 strerror(savedErrno));
375 return false;
376 }
377
378 if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) {
379 if (errno == ECONNRESET) {
380 ALOGW("Connection reset on %s", addr.toString().c_str());
381 continue;
382 }
383 int savedErrno = errno;
384 ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(),
385 strerror(savedErrno));
386 return false;
387 }
388
Steven Moreland659416d2021-05-11 00:47:50 +0000389 RpcConnectionHeader header{
390 .sessionId = id,
391 };
392 if (reverse) header.options |= RPC_CONNECTION_OPTION_REVERSE;
393
394 if (sizeof(header) != TEMP_FAILURE_RETRY(write(serverFd.get(), &header, sizeof(header)))) {
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000395 int savedErrno = errno;
Steven Moreland659416d2021-05-11 00:47:50 +0000396 ALOGE("Could not write connection header to socket at %s: %s", addr.toString().c_str(),
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000397 strerror(savedErrno));
398 return false;
399 }
400
401 LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get());
402
Steven Moreland659416d2021-05-11 00:47:50 +0000403 if (reverse) {
404 std::mutex mutex;
405 std::condition_variable joinCv;
406 std::unique_lock<std::mutex> lock(mutex);
407 std::thread thread;
408 sp<RpcSession> thiz = sp<RpcSession>::fromExisting(this);
409 bool ownershipTransferred = false;
410 thread = std::thread([&]() {
411 std::unique_lock<std::mutex> threadLock(mutex);
412 unique_fd fd = std::move(serverFd);
413 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
414 sp<RpcSession> session = thiz;
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000415 session->preJoinThreadOwnership(std::move(thread));
Steven Moreland659416d2021-05-11 00:47:50 +0000416
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000417 // only continue once we have a response or the connection fails
418 auto setupResult = session->preJoinSetup(std::move(fd));
419
420 ownershipTransferred = true;
Steven Moreland659416d2021-05-11 00:47:50 +0000421 threadLock.unlock();
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000422 joinCv.notify_one();
Steven Moreland659416d2021-05-11 00:47:50 +0000423 // do not use & vars below
424
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000425 RpcSession::join(std::move(session), std::move(setupResult));
Steven Moreland659416d2021-05-11 00:47:50 +0000426 });
427 joinCv.wait(lock, [&] { return ownershipTransferred; });
428 LOG_ALWAYS_FATAL_IF(!ownershipTransferred);
429 return true;
430 } else {
431 return addClientConnection(std::move(serverFd));
432 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000433 }
434
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000435 ALOGE("Ran out of retries to connect to %s", addr.toString().c_str());
436 return false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000437}
438
Steven Morelandb0dd1182021-05-25 01:56:46 +0000439bool RpcSession::addClientConnection(unique_fd fd) {
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000440 sp<RpcConnection> connection = sp<RpcConnection>::make();
441 {
442 std::lock_guard<std::mutex> _l(mMutex);
Steven Morelandee3f4662021-05-22 01:07:33 +0000443
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000444 // first client connection added, but setForServer not called, so
445 // initializaing for a client.
446 if (mShutdownTrigger == nullptr) {
447 mShutdownTrigger = FdTrigger::make();
448 mEventListener = mShutdownListener = sp<WaitForShutdownListener>::make();
449 if (mShutdownTrigger == nullptr) return false;
450 }
451
452 connection->fd = std::move(fd);
453 connection->exclusiveTid = gettid();
454 mClientConnections.push_back(connection);
Steven Morelandee3f4662021-05-22 01:07:33 +0000455 }
456
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000457 status_t status =
458 mState->sendConnectionInit(connection->fd, sp<RpcSession>::fromExisting(this));
459
460 {
461 std::lock_guard<std::mutex> _l(mMutex);
462 connection->exclusiveTid = std::nullopt;
463 }
464
465 return status == OK;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000466}
467
Steven Morelanda8b44292021-06-08 01:27:53 +0000468bool RpcSession::setForServer(const wp<RpcServer>& server, const wp<EventListener>& eventListener,
469 int32_t sessionId) {
Steven Moreland659416d2021-05-11 00:47:50 +0000470 LOG_ALWAYS_FATAL_IF(mForServer != nullptr);
471 LOG_ALWAYS_FATAL_IF(server == nullptr);
472 LOG_ALWAYS_FATAL_IF(mEventListener != nullptr);
473 LOG_ALWAYS_FATAL_IF(eventListener == nullptr);
Steven Morelandee3f4662021-05-22 01:07:33 +0000474 LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr);
Steven Morelanda8b44292021-06-08 01:27:53 +0000475
476 mShutdownTrigger = FdTrigger::make();
477 if (mShutdownTrigger == nullptr) return false;
Steven Morelandee3f4662021-05-22 01:07:33 +0000478
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000479 mId = sessionId;
480 mForServer = server;
Steven Moreland659416d2021-05-11 00:47:50 +0000481 mEventListener = eventListener;
Steven Morelanda8b44292021-06-08 01:27:53 +0000482 return true;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000483}
484
485sp<RpcSession::RpcConnection> RpcSession::assignServerToThisThread(unique_fd fd) {
486 std::lock_guard<std::mutex> _l(mMutex);
487 sp<RpcConnection> session = sp<RpcConnection>::make();
488 session->fd = std::move(fd);
489 session->exclusiveTid = gettid();
Steven Morelandbb543a82021-05-11 02:31:50 +0000490 mServerConnections.push_back(session);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000491
492 return session;
493}
494
495bool RpcSession::removeServerConnection(const sp<RpcConnection>& connection) {
496 std::lock_guard<std::mutex> _l(mMutex);
Steven Morelandbb543a82021-05-11 02:31:50 +0000497 if (auto it = std::find(mServerConnections.begin(), mServerConnections.end(), connection);
498 it != mServerConnections.end()) {
499 mServerConnections.erase(it);
500 if (mServerConnections.size() == 0) {
Steven Moreland659416d2021-05-11 00:47:50 +0000501 sp<EventListener> listener = mEventListener.promote();
502 if (listener) {
503 listener->onSessionLockedAllServerThreadsEnded(sp<RpcSession>::fromExisting(this));
Steven Morelanda86e8fe2021-05-26 22:52:35 +0000504 }
Steven Morelandee78e762021-05-05 21:12:51 +0000505 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000506 return true;
507 }
508 return false;
509}
510
Steven Moreland195edb82021-06-08 02:44:39 +0000511status_t RpcSession::ExclusiveConnection::find(const sp<RpcSession>& session, ConnectionUse use,
512 ExclusiveConnection* connection) {
513 connection->mSession = session;
514 connection->mConnection = nullptr;
515 connection->mReentrant = false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000516
Steven Moreland195edb82021-06-08 02:44:39 +0000517 pid_t tid = gettid();
518 std::unique_lock<std::mutex> _l(session->mMutex);
519
520 session->mWaitingThreads++;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000521 while (true) {
522 sp<RpcConnection> exclusive;
523 sp<RpcConnection> available;
524
525 // CHECK FOR DEDICATED CLIENT SOCKET
526 //
Steven Moreland85e067b2021-05-26 17:43:53 +0000527 // A server/looper should always use a dedicated connection if available
Steven Moreland195edb82021-06-08 02:44:39 +0000528 findConnection(tid, &exclusive, &available, session->mClientConnections,
529 session->mClientConnectionsOffset);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000530
531 // WARNING: this assumes a server cannot request its client to send
Steven Morelandbb543a82021-05-11 02:31:50 +0000532 // a transaction, as mServerConnections is excluded below.
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000533 //
534 // Imagine we have more than one thread in play, and a single thread
535 // sends a synchronous, then an asynchronous command. Imagine the
536 // asynchronous command is sent on the first client connection. Then, if
537 // we naively send a synchronous command to that same connection, the
538 // thread on the far side might be busy processing the asynchronous
539 // command. So, we move to considering the second available thread
540 // for subsequent calls.
541 if (use == ConnectionUse::CLIENT_ASYNC && (exclusive != nullptr || available != nullptr)) {
Steven Moreland195edb82021-06-08 02:44:39 +0000542 session->mClientConnectionsOffset =
543 (session->mClientConnectionsOffset + 1) % session->mClientConnections.size();
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000544 }
545
546 // USE SERVING SOCKET (for nested transaction)
547 //
548 // asynchronous calls cannot be nested
549 if (use != ConnectionUse::CLIENT_ASYNC) {
550 // server connections are always assigned to a thread
Steven Moreland195edb82021-06-08 02:44:39 +0000551 findConnection(tid, &exclusive, nullptr /*available*/, session->mServerConnections,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000552 0 /* index hint */);
553 }
554
Steven Moreland85e067b2021-05-26 17:43:53 +0000555 // if our thread is already using a connection, prioritize using that
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000556 if (exclusive != nullptr) {
Steven Moreland195edb82021-06-08 02:44:39 +0000557 connection->mConnection = exclusive;
558 connection->mReentrant = true;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000559 break;
560 } else if (available != nullptr) {
Steven Moreland195edb82021-06-08 02:44:39 +0000561 connection->mConnection = available;
562 connection->mConnection->exclusiveTid = tid;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000563 break;
564 }
565
Steven Moreland195edb82021-06-08 02:44:39 +0000566 if (session->mClientConnections.size() == 0) {
567 ALOGE("Session has no client connections. This is required for an RPC server to make "
568 "any non-nested (e.g. oneway or on another thread) calls. Use: %d. Server "
569 "connections: %zu",
570 static_cast<int>(use), session->mServerConnections.size());
571 return WOULD_BLOCK;
572 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000573
Steven Moreland85e067b2021-05-26 17:43:53 +0000574 LOG_RPC_DETAIL("No available connections (have %zu clients and %zu servers). Waiting...",
Steven Moreland195edb82021-06-08 02:44:39 +0000575 session->mClientConnections.size(), session->mServerConnections.size());
576 session->mAvailableConnectionCv.wait(_l);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000577 }
Steven Moreland195edb82021-06-08 02:44:39 +0000578 session->mWaitingThreads--;
579
580 return OK;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000581}
582
583void RpcSession::ExclusiveConnection::findConnection(pid_t tid, sp<RpcConnection>* exclusive,
584 sp<RpcConnection>* available,
585 std::vector<sp<RpcConnection>>& sockets,
586 size_t socketsIndexHint) {
587 LOG_ALWAYS_FATAL_IF(sockets.size() > 0 && socketsIndexHint >= sockets.size(),
588 "Bad index %zu >= %zu", socketsIndexHint, sockets.size());
589
590 if (*exclusive != nullptr) return; // consistent with break below
591
592 for (size_t i = 0; i < sockets.size(); i++) {
593 sp<RpcConnection>& socket = sockets[(i + socketsIndexHint) % sockets.size()];
594
Steven Moreland85e067b2021-05-26 17:43:53 +0000595 // take first available connection (intuition = caching)
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000596 if (available && *available == nullptr && socket->exclusiveTid == std::nullopt) {
597 *available = socket;
598 continue;
599 }
600
Steven Moreland85e067b2021-05-26 17:43:53 +0000601 // though, prefer to take connection which is already inuse by this thread
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000602 // (nested transactions)
603 if (exclusive && socket->exclusiveTid == tid) {
604 *exclusive = socket;
605 break; // consistent with return above
606 }
607 }
608}
609
610RpcSession::ExclusiveConnection::~ExclusiveConnection() {
Steven Moreland85e067b2021-05-26 17:43:53 +0000611 // reentrant use of a connection means something less deep in the call stack
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000612 // is using this fd, and it retains the right to it. So, we don't give up
613 // exclusive ownership, and no thread is freed.
Steven Moreland195edb82021-06-08 02:44:39 +0000614 if (!mReentrant && mConnection != nullptr) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000615 std::unique_lock<std::mutex> _l(mSession->mMutex);
616 mConnection->exclusiveTid = std::nullopt;
617 if (mSession->mWaitingThreads > 0) {
618 _l.unlock();
619 mSession->mAvailableConnectionCv.notify_one();
620 }
621 }
622}
623
624} // namespace android