blob: bdf1bbef02469f003eca35fd4876dc0485049699 [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
Yifan Hong0f9c5c72021-06-29 18:44:56 -070021#include <dlfcn.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000022#include <inttypes.h>
Steven Moreland4ec3c432021-05-20 00:32:47 +000023#include <poll.h>
Yifan Hong0f9c5c72021-06-29 18:44:56 -070024#include <pthread.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000025#include <unistd.h>
26
27#include <string_view>
28
Steven Moreland4ec3c432021-05-20 00:32:47 +000029#include <android-base/macros.h>
Yifan Hong0f9c5c72021-06-29 18:44:56 -070030#include <android_runtime/threads.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000031#include <binder/Parcel.h>
Steven Morelandee78e762021-05-05 21:12:51 +000032#include <binder/RpcServer.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000033#include <binder/Stability.h>
34#include <utils/String8.h>
35
36#include "RpcSocketAddress.h"
37#include "RpcState.h"
38#include "RpcWireFormat.h"
39
40#ifdef __GLIBC__
41extern "C" pid_t gettid();
42#endif
43
44namespace android {
45
46using base::unique_fd;
47
48RpcSession::RpcSession() {
49 LOG_RPC_DETAIL("RpcSession created %p", this);
50
51 mState = std::make_unique<RpcState>();
52}
53RpcSession::~RpcSession() {
54 LOG_RPC_DETAIL("RpcSession destroyed %p", this);
55
56 std::lock_guard<std::mutex> _l(mMutex);
Steven Moreland19fc9f72021-06-10 03:57:30 +000057 LOG_ALWAYS_FATAL_IF(mIncomingConnections.size() != 0,
Steven Morelandbdb53ab2021-05-05 17:57:41 +000058 "Should not be able to destroy a session with servers in use.");
59}
60
61sp<RpcSession> RpcSession::make() {
62 return sp<RpcSession>::make();
63}
64
Steven Moreland103424e2021-06-02 18:16:19 +000065void RpcSession::setMaxThreads(size_t threads) {
66 std::lock_guard<std::mutex> _l(mMutex);
Steven Moreland19fc9f72021-06-10 03:57:30 +000067 LOG_ALWAYS_FATAL_IF(!mOutgoingConnections.empty() || !mIncomingConnections.empty(),
Steven Moreland103424e2021-06-02 18:16:19 +000068 "Must set max threads before setting up connections, but has %zu client(s) "
69 "and %zu server(s)",
Steven Moreland19fc9f72021-06-10 03:57:30 +000070 mOutgoingConnections.size(), mIncomingConnections.size());
Steven Moreland103424e2021-06-02 18:16:19 +000071 mMaxThreads = threads;
72}
73
74size_t RpcSession::getMaxThreads() {
75 std::lock_guard<std::mutex> _l(mMutex);
76 return mMaxThreads;
Steven Moreland659416d2021-05-11 00:47:50 +000077}
78
Steven Morelandbdb53ab2021-05-05 17:57:41 +000079bool RpcSession::setupUnixDomainClient(const char* path) {
80 return setupSocketClient(UnixSocketAddress(path));
81}
82
Steven Morelandbdb53ab2021-05-05 17:57:41 +000083bool RpcSession::setupVsockClient(unsigned int cid, unsigned int port) {
84 return setupSocketClient(VsockSocketAddress(cid, port));
85}
86
Steven Morelandbdb53ab2021-05-05 17:57:41 +000087bool RpcSession::setupInetClient(const char* addr, unsigned int port) {
88 auto aiStart = InetSocketAddress::getAddrInfo(addr, port);
89 if (aiStart == nullptr) return false;
90 for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) {
91 InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, addr, port);
92 if (setupSocketClient(socketAddress)) return true;
93 }
94 ALOGE("None of the socket address resolved for %s:%u can be added as inet client.", addr, port);
95 return false;
96}
97
98bool RpcSession::addNullDebuggingClient() {
99 unique_fd serverFd(TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY | O_CLOEXEC)));
100
101 if (serverFd == -1) {
102 ALOGE("Could not connect to /dev/null: %s", strerror(errno));
103 return false;
104 }
105
Steven Morelandb86e26b2021-06-12 00:35:58 +0000106 return addOutgoingConnection(std::move(serverFd), false);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000107}
108
109sp<IBinder> RpcSession::getRootObject() {
Steven Moreland195edb82021-06-08 02:44:39 +0000110 ExclusiveConnection connection;
111 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
112 ConnectionUse::CLIENT, &connection);
113 if (status != OK) return nullptr;
Steven Moreland5ae62562021-06-10 03:21:42 +0000114 return state()->getRootObject(connection.get(), sp<RpcSession>::fromExisting(this));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000115}
116
Steven Moreland1be91352021-05-11 22:12:15 +0000117status_t RpcSession::getRemoteMaxThreads(size_t* maxThreads) {
Steven Moreland195edb82021-06-08 02:44:39 +0000118 ExclusiveConnection connection;
119 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
120 ConnectionUse::CLIENT, &connection);
121 if (status != OK) return status;
Steven Moreland5ae62562021-06-10 03:21:42 +0000122 return state()->getMaxThreads(connection.get(), sp<RpcSession>::fromExisting(this), maxThreads);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000123}
124
Steven Morelandc9d7b532021-06-04 20:57:41 +0000125bool RpcSession::shutdownAndWait(bool wait) {
Steven Moreland659416d2021-05-11 00:47:50 +0000126 std::unique_lock<std::mutex> _l(mMutex);
Steven Moreland659416d2021-05-11 00:47:50 +0000127 LOG_ALWAYS_FATAL_IF(mShutdownTrigger == nullptr, "Shutdown trigger not installed");
Steven Moreland659416d2021-05-11 00:47:50 +0000128
129 mShutdownTrigger->trigger();
Steven Moreland659416d2021-05-11 00:47:50 +0000130
Steven Morelandc9d7b532021-06-04 20:57:41 +0000131 if (wait) {
132 LOG_ALWAYS_FATAL_IF(mShutdownListener == nullptr, "Shutdown listener not installed");
133 mShutdownListener->waitForShutdown(_l);
134 LOG_ALWAYS_FATAL_IF(!mThreads.empty(), "Shutdown failed");
135 }
136
137 _l.unlock();
138 mState->clear();
139
Steven Moreland659416d2021-05-11 00:47:50 +0000140 return true;
141}
142
Steven Morelandf5174272021-05-25 00:39:28 +0000143status_t RpcSession::transact(const sp<IBinder>& binder, uint32_t code, const Parcel& data,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000144 Parcel* reply, uint32_t flags) {
Steven Moreland195edb82021-06-08 02:44:39 +0000145 ExclusiveConnection connection;
146 status_t status =
147 ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
148 (flags & IBinder::FLAG_ONEWAY) ? ConnectionUse::CLIENT_ASYNC
149 : ConnectionUse::CLIENT,
150 &connection);
151 if (status != OK) return status;
Steven Moreland5ae62562021-06-10 03:21:42 +0000152 return state()->transact(connection.get(), binder, code, data,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000153 sp<RpcSession>::fromExisting(this), reply, flags);
154}
155
156status_t RpcSession::sendDecStrong(const RpcAddress& address) {
Steven Moreland195edb82021-06-08 02:44:39 +0000157 ExclusiveConnection connection;
158 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
159 ConnectionUse::CLIENT_REFCOUNT, &connection);
160 if (status != OK) return status;
Steven Moreland5ae62562021-06-10 03:21:42 +0000161 return state()->sendDecStrong(connection.get(), sp<RpcSession>::fromExisting(this), address);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000162}
163
Steven Morelande47511f2021-05-20 00:07:41 +0000164std::unique_ptr<RpcSession::FdTrigger> RpcSession::FdTrigger::make() {
165 auto ret = std::make_unique<RpcSession::FdTrigger>();
Steven Morelanda8b44292021-06-08 01:27:53 +0000166 if (!android::base::Pipe(&ret->mRead, &ret->mWrite)) {
167 ALOGE("Could not create pipe %s", strerror(errno));
168 return nullptr;
169 }
Steven Morelande47511f2021-05-20 00:07:41 +0000170 return ret;
171}
172
173void RpcSession::FdTrigger::trigger() {
174 mWrite.reset();
175}
176
Steven Morelanda8b44292021-06-08 01:27:53 +0000177bool RpcSession::FdTrigger::isTriggered() {
178 return mWrite == -1;
179}
180
Steven Moreland2b4f3802021-05-22 01:46:27 +0000181status_t RpcSession::FdTrigger::triggerablePollRead(base::borrowed_fd fd) {
Steven Moreland4ec3c432021-05-20 00:32:47 +0000182 while (true) {
Steven Morelanddfe3be92021-05-22 00:24:29 +0000183 pollfd pfd[]{{.fd = fd.get(), .events = POLLIN | POLLHUP, .revents = 0},
Steven Moreland4ec3c432021-05-20 00:32:47 +0000184 {.fd = mRead.get(), .events = POLLHUP, .revents = 0}};
185 int ret = TEMP_FAILURE_RETRY(poll(pfd, arraysize(pfd), -1));
186 if (ret < 0) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000187 return -errno;
Steven Moreland4ec3c432021-05-20 00:32:47 +0000188 }
189 if (ret == 0) {
190 continue;
191 }
192 if (pfd[1].revents & POLLHUP) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000193 return -ECANCELED;
Steven Moreland4ec3c432021-05-20 00:32:47 +0000194 }
Steven Moreland2b4f3802021-05-22 01:46:27 +0000195 return pfd[0].revents & POLLIN ? OK : DEAD_OBJECT;
Steven Moreland4ec3c432021-05-20 00:32:47 +0000196 }
197}
198
Steven Moreland2b4f3802021-05-22 01:46:27 +0000199status_t RpcSession::FdTrigger::interruptableReadFully(base::borrowed_fd fd, void* data,
200 size_t size) {
Steven Moreland9d11b922021-05-20 01:22:58 +0000201 uint8_t* buffer = reinterpret_cast<uint8_t*>(data);
202 uint8_t* end = buffer + size;
203
Steven Morelandb8176792021-06-22 20:29:21 +0000204 MAYBE_WAIT_IN_FLAKE_MODE;
205
Steven Moreland2b4f3802021-05-22 01:46:27 +0000206 status_t status;
207 while ((status = triggerablePollRead(fd)) == OK) {
Steven Moreland9d11b922021-05-20 01:22:58 +0000208 ssize_t readSize = TEMP_FAILURE_RETRY(recv(fd.get(), buffer, end - buffer, MSG_NOSIGNAL));
Steven Moreland2b4f3802021-05-22 01:46:27 +0000209 if (readSize == 0) return DEAD_OBJECT; // EOF
Steven Morelanddfe3be92021-05-22 00:24:29 +0000210
Steven Moreland9d11b922021-05-20 01:22:58 +0000211 if (readSize < 0) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000212 return -errno;
Steven Moreland9d11b922021-05-20 01:22:58 +0000213 }
214 buffer += readSize;
Steven Moreland2b4f3802021-05-22 01:46:27 +0000215 if (buffer == end) return OK;
Steven Moreland9d11b922021-05-20 01:22:58 +0000216 }
Steven Moreland2b4f3802021-05-22 01:46:27 +0000217 return status;
Steven Moreland9d11b922021-05-20 01:22:58 +0000218}
219
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000220status_t RpcSession::readId() {
221 {
222 std::lock_guard<std::mutex> _l(mMutex);
223 LOG_ALWAYS_FATAL_IF(mForServer != nullptr, "Can only update ID for client.");
224 }
225
Steven Moreland195edb82021-06-08 02:44:39 +0000226 ExclusiveConnection connection;
227 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
228 ConnectionUse::CLIENT, &connection);
229 if (status != OK) return status;
230
Steven Moreland01a6bad2021-06-11 00:59:20 +0000231 mId = RpcAddress::zero();
232 status = state()->getSessionId(connection.get(), sp<RpcSession>::fromExisting(this),
233 &mId.value());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000234 if (status != OK) return status;
235
Steven Moreland01a6bad2021-06-11 00:59:20 +0000236 LOG_RPC_DETAIL("RpcSession %p has id %s", this, mId->toString().c_str());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000237 return OK;
238}
239
Steven Moreland19fc9f72021-06-10 03:57:30 +0000240void RpcSession::WaitForShutdownListener::onSessionLockedAllIncomingThreadsEnded(
Steven Moreland659416d2021-05-11 00:47:50 +0000241 const sp<RpcSession>& session) {
242 (void)session;
243 mShutdown = true;
244}
245
Steven Moreland19fc9f72021-06-10 03:57:30 +0000246void RpcSession::WaitForShutdownListener::onSessionIncomingThreadEnded() {
Steven Moreland659416d2021-05-11 00:47:50 +0000247 mCv.notify_all();
248}
249
250void RpcSession::WaitForShutdownListener::waitForShutdown(std::unique_lock<std::mutex>& lock) {
251 while (!mShutdown) {
252 if (std::cv_status::timeout == mCv.wait_for(lock, std::chrono::seconds(1))) {
253 ALOGE("Waiting for RpcSession to shut down (1s w/o progress).");
254 }
255 }
256}
257
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000258void RpcSession::preJoinThreadOwnership(std::thread thread) {
Steven Morelanda63ff932021-05-12 00:03:15 +0000259 LOG_ALWAYS_FATAL_IF(thread.get_id() != std::this_thread::get_id(), "Must own this thread");
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000260
Steven Morelanda63ff932021-05-12 00:03:15 +0000261 {
262 std::lock_guard<std::mutex> _l(mMutex);
263 mThreads[thread.get_id()] = std::move(thread);
264 }
Steven Moreland5802c2b2021-05-12 20:13:04 +0000265}
Steven Morelanda63ff932021-05-12 00:03:15 +0000266
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000267RpcSession::PreJoinSetupResult RpcSession::preJoinSetup(base::unique_fd fd) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000268 // must be registered to allow arbitrary client code executing commands to
269 // be able to do nested calls (we can't only read from it)
Steven Moreland19fc9f72021-06-10 03:57:30 +0000270 sp<RpcConnection> connection = assignIncomingConnectionToThisThread(std::move(fd));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000271
Steven Moreland5ae62562021-06-10 03:21:42 +0000272 status_t status = mState->readConnectionInit(connection, sp<RpcSession>::fromExisting(this));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000273
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000274 return PreJoinSetupResult{
275 .connection = std::move(connection),
276 .status = status,
277 };
278}
279
Yifan Hong0f9c5c72021-06-29 18:44:56 -0700280namespace {
281// RAII object for attaching / detaching current thread to JVM if Android Runtime exists. If
282// Android Runtime doesn't exist, no-op.
283class JavaThreadAttacher {
284public:
285 JavaThreadAttacher() {
286 // Use dlsym to find androidJavaAttachThread because libandroid_runtime is loaded after
287 // libbinder.
288 static auto attachFn = reinterpret_cast<decltype(&androidJavaAttachThread)>(
289 dlsym(RTLD_DEFAULT, "androidJavaAttachThread"));
290 if (attachFn == nullptr) return;
291
292 char buf[16];
293 const char* threadName = "UnknownRpcSessionThread"; // default thread name
294 if (0 == pthread_getname_np(pthread_self(), buf, sizeof(buf))) {
295 threadName = buf;
296 }
297 LOG_RPC_DETAIL("Attaching current thread %s to JVM", threadName);
298 LOG_ALWAYS_FATAL_IF(!attachFn(threadName), "Cannot attach thread %s to JVM", threadName);
299 mAttached = true;
300 }
301 ~JavaThreadAttacher() {
302 if (!mAttached) return;
303 static auto detachFn = reinterpret_cast<decltype(&androidJavaDetachThread)>(
304 dlsym(RTLD_DEFAULT, "androidJavaDetachThread"));
305 LOG_ALWAYS_FATAL_IF(detachFn == nullptr,
306 "androidJavaAttachThread exists but androidJavaDetachThread doesn't");
307
308 LOG_RPC_DETAIL("Detaching current thread from JVM");
309 if (detachFn()) {
310 mAttached = false;
311 } else {
312 ALOGW("Unable to detach current thread from JVM");
313 }
314 }
315
316private:
317 DISALLOW_COPY_AND_ASSIGN(JavaThreadAttacher);
318 bool mAttached = false;
319};
320} // namespace
321
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000322void RpcSession::join(sp<RpcSession>&& session, PreJoinSetupResult&& setupResult) {
323 sp<RpcConnection>& connection = setupResult.connection;
324
325 if (setupResult.status == OK) {
Yifan Hong0f9c5c72021-06-29 18:44:56 -0700326 JavaThreadAttacher javaThreadAttacher;
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000327 while (true) {
Steven Moreland5ae62562021-06-10 03:21:42 +0000328 status_t status = session->state()->getAndExecuteCommand(connection, session,
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000329 RpcState::CommandType::ANY);
330 if (status != OK) {
331 LOG_RPC_DETAIL("Binder connection thread closing w/ status %s",
332 statusToString(status).c_str());
333 break;
334 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000335 }
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000336 } else {
337 ALOGE("Connection failed to init, closing with status %s",
338 statusToString(setupResult.status).c_str());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000339 }
340
Steven Moreland19fc9f72021-06-10 03:57:30 +0000341 LOG_ALWAYS_FATAL_IF(!session->removeIncomingConnection(connection),
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000342 "bad state: connection object guaranteed to be in list");
Steven Morelanda63ff932021-05-12 00:03:15 +0000343
Steven Moreland659416d2021-05-11 00:47:50 +0000344 sp<RpcSession::EventListener> listener;
Steven Morelanda63ff932021-05-12 00:03:15 +0000345 {
Steven Moreland659416d2021-05-11 00:47:50 +0000346 std::lock_guard<std::mutex> _l(session->mMutex);
347 auto it = session->mThreads.find(std::this_thread::get_id());
348 LOG_ALWAYS_FATAL_IF(it == session->mThreads.end());
Steven Morelanda63ff932021-05-12 00:03:15 +0000349 it->second.detach();
Steven Moreland659416d2021-05-11 00:47:50 +0000350 session->mThreads.erase(it);
Steven Morelandee3f4662021-05-22 01:07:33 +0000351
Steven Moreland659416d2021-05-11 00:47:50 +0000352 listener = session->mEventListener.promote();
Steven Morelandee3f4662021-05-22 01:07:33 +0000353 }
354
Steven Moreland659416d2021-05-11 00:47:50 +0000355 session = nullptr;
356
357 if (listener != nullptr) {
Steven Moreland19fc9f72021-06-10 03:57:30 +0000358 listener->onSessionIncomingThreadEnded();
Steven Morelandee78e762021-05-05 21:12:51 +0000359 }
360}
361
Steven Moreland7b8bc4c2021-06-10 22:50:27 +0000362sp<RpcServer> RpcSession::server() {
363 RpcServer* unsafeServer = mForServer.unsafe_get();
364 sp<RpcServer> server = mForServer.promote();
365
366 LOG_ALWAYS_FATAL_IF((unsafeServer == nullptr) != (server == nullptr),
367 "wp<> is to avoid strong cycle only");
368 return server;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000369}
370
371bool RpcSession::setupSocketClient(const RpcSocketAddress& addr) {
372 {
373 std::lock_guard<std::mutex> _l(mMutex);
Steven Moreland19fc9f72021-06-10 03:57:30 +0000374 LOG_ALWAYS_FATAL_IF(mOutgoingConnections.size() != 0,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000375 "Must only setup session once, but already has %zu clients",
Steven Moreland19fc9f72021-06-10 03:57:30 +0000376 mOutgoingConnections.size());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000377 }
378
Steven Moreland01a6bad2021-06-11 00:59:20 +0000379 if (!setupOneSocketConnection(addr, RpcAddress::zero(), false /*reverse*/)) return false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000380
Steven Morelanda5036f02021-06-08 02:26:57 +0000381 // TODO(b/189955605): we should add additional sessions dynamically
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000382 // instead of all at once.
383 // TODO(b/186470974): first risk of blocking
384 size_t numThreadsAvailable;
Steven Moreland1be91352021-05-11 22:12:15 +0000385 if (status_t status = getRemoteMaxThreads(&numThreadsAvailable); status != OK) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000386 ALOGE("Could not get max threads after initial session to %s: %s", addr.toString().c_str(),
387 statusToString(status).c_str());
388 return false;
389 }
390
391 if (status_t status = readId(); status != OK) {
392 ALOGE("Could not get session id after initial session to %s; %s", addr.toString().c_str(),
393 statusToString(status).c_str());
394 return false;
395 }
396
397 // we've already setup one client
398 for (size_t i = 0; i + 1 < numThreadsAvailable; i++) {
Steven Morelanda5036f02021-06-08 02:26:57 +0000399 // TODO(b/189955605): shutdown existing connections?
Steven Moreland659416d2021-05-11 00:47:50 +0000400 if (!setupOneSocketConnection(addr, mId.value(), false /*reverse*/)) return false;
401 }
402
Steven Morelanda5036f02021-06-08 02:26:57 +0000403 // TODO(b/189955605): we should add additional sessions dynamically
Steven Moreland659416d2021-05-11 00:47:50 +0000404 // instead of all at once - the other side should be responsible for setting
405 // up additional connections. We need to create at least one (unless 0 are
406 // requested to be set) in order to allow the other side to reliably make
407 // any requests at all.
408
Steven Moreland103424e2021-06-02 18:16:19 +0000409 for (size_t i = 0; i < mMaxThreads; i++) {
Steven Moreland659416d2021-05-11 00:47:50 +0000410 if (!setupOneSocketConnection(addr, mId.value(), true /*reverse*/)) return false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000411 }
412
413 return true;
414}
415
Steven Moreland01a6bad2021-06-11 00:59:20 +0000416bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, const RpcAddress& id,
417 bool reverse) {
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000418 for (size_t tries = 0; tries < 5; tries++) {
419 if (tries > 0) usleep(10000);
420
421 unique_fd serverFd(
422 TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0)));
423 if (serverFd == -1) {
424 int savedErrno = errno;
425 ALOGE("Could not create socket at %s: %s", addr.toString().c_str(),
426 strerror(savedErrno));
427 return false;
428 }
429
430 if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) {
431 if (errno == ECONNRESET) {
432 ALOGW("Connection reset on %s", addr.toString().c_str());
433 continue;
434 }
435 int savedErrno = errno;
436 ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(),
437 strerror(savedErrno));
438 return false;
439 }
440
Steven Moreland01a6bad2021-06-11 00:59:20 +0000441 RpcConnectionHeader header{.options = 0};
442 memcpy(&header.sessionId, &id.viewRawEmbedded(), sizeof(RpcWireAddress));
443
Steven Moreland659416d2021-05-11 00:47:50 +0000444 if (reverse) header.options |= RPC_CONNECTION_OPTION_REVERSE;
445
446 if (sizeof(header) != TEMP_FAILURE_RETRY(write(serverFd.get(), &header, sizeof(header)))) {
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000447 int savedErrno = errno;
Steven Moreland659416d2021-05-11 00:47:50 +0000448 ALOGE("Could not write connection header to socket at %s: %s", addr.toString().c_str(),
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000449 strerror(savedErrno));
450 return false;
451 }
452
453 LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get());
454
Steven Moreland659416d2021-05-11 00:47:50 +0000455 if (reverse) {
456 std::mutex mutex;
457 std::condition_variable joinCv;
458 std::unique_lock<std::mutex> lock(mutex);
459 std::thread thread;
460 sp<RpcSession> thiz = sp<RpcSession>::fromExisting(this);
461 bool ownershipTransferred = false;
462 thread = std::thread([&]() {
463 std::unique_lock<std::mutex> threadLock(mutex);
464 unique_fd fd = std::move(serverFd);
465 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
466 sp<RpcSession> session = thiz;
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000467 session->preJoinThreadOwnership(std::move(thread));
Steven Moreland659416d2021-05-11 00:47:50 +0000468
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000469 // only continue once we have a response or the connection fails
470 auto setupResult = session->preJoinSetup(std::move(fd));
471
472 ownershipTransferred = true;
Steven Moreland659416d2021-05-11 00:47:50 +0000473 threadLock.unlock();
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000474 joinCv.notify_one();
Steven Moreland659416d2021-05-11 00:47:50 +0000475 // do not use & vars below
476
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000477 RpcSession::join(std::move(session), std::move(setupResult));
Steven Moreland659416d2021-05-11 00:47:50 +0000478 });
479 joinCv.wait(lock, [&] { return ownershipTransferred; });
480 LOG_ALWAYS_FATAL_IF(!ownershipTransferred);
481 return true;
482 } else {
Steven Morelandb86e26b2021-06-12 00:35:58 +0000483 return addOutgoingConnection(std::move(serverFd), true);
Steven Moreland659416d2021-05-11 00:47:50 +0000484 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000485 }
486
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000487 ALOGE("Ran out of retries to connect to %s", addr.toString().c_str());
488 return false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000489}
490
Steven Morelandb86e26b2021-06-12 00:35:58 +0000491bool RpcSession::addOutgoingConnection(unique_fd fd, bool init) {
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000492 sp<RpcConnection> connection = sp<RpcConnection>::make();
493 {
494 std::lock_guard<std::mutex> _l(mMutex);
Steven Morelandee3f4662021-05-22 01:07:33 +0000495
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000496 // first client connection added, but setForServer not called, so
497 // initializaing for a client.
498 if (mShutdownTrigger == nullptr) {
499 mShutdownTrigger = FdTrigger::make();
500 mEventListener = mShutdownListener = sp<WaitForShutdownListener>::make();
501 if (mShutdownTrigger == nullptr) return false;
502 }
503
504 connection->fd = std::move(fd);
505 connection->exclusiveTid = gettid();
Steven Moreland19fc9f72021-06-10 03:57:30 +0000506 mOutgoingConnections.push_back(connection);
Steven Morelandee3f4662021-05-22 01:07:33 +0000507 }
508
Steven Morelandb86e26b2021-06-12 00:35:58 +0000509 status_t status = OK;
510 if (init) {
511 mState->sendConnectionInit(connection, sp<RpcSession>::fromExisting(this));
512 }
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000513
514 {
515 std::lock_guard<std::mutex> _l(mMutex);
516 connection->exclusiveTid = std::nullopt;
517 }
518
519 return status == OK;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000520}
521
Steven Morelanda8b44292021-06-08 01:27:53 +0000522bool RpcSession::setForServer(const wp<RpcServer>& server, const wp<EventListener>& eventListener,
Steven Moreland01a6bad2021-06-11 00:59:20 +0000523 const RpcAddress& sessionId) {
Steven Moreland659416d2021-05-11 00:47:50 +0000524 LOG_ALWAYS_FATAL_IF(mForServer != nullptr);
525 LOG_ALWAYS_FATAL_IF(server == nullptr);
526 LOG_ALWAYS_FATAL_IF(mEventListener != nullptr);
527 LOG_ALWAYS_FATAL_IF(eventListener == nullptr);
Steven Morelandee3f4662021-05-22 01:07:33 +0000528 LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr);
Steven Morelanda8b44292021-06-08 01:27:53 +0000529
530 mShutdownTrigger = FdTrigger::make();
531 if (mShutdownTrigger == nullptr) return false;
Steven Morelandee3f4662021-05-22 01:07:33 +0000532
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000533 mId = sessionId;
534 mForServer = server;
Steven Moreland659416d2021-05-11 00:47:50 +0000535 mEventListener = eventListener;
Steven Morelanda8b44292021-06-08 01:27:53 +0000536 return true;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000537}
538
Steven Moreland19fc9f72021-06-10 03:57:30 +0000539sp<RpcSession::RpcConnection> RpcSession::assignIncomingConnectionToThisThread(unique_fd fd) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000540 std::lock_guard<std::mutex> _l(mMutex);
541 sp<RpcConnection> session = sp<RpcConnection>::make();
542 session->fd = std::move(fd);
543 session->exclusiveTid = gettid();
Steven Moreland19fc9f72021-06-10 03:57:30 +0000544 mIncomingConnections.push_back(session);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000545
546 return session;
547}
548
Steven Moreland19fc9f72021-06-10 03:57:30 +0000549bool RpcSession::removeIncomingConnection(const sp<RpcConnection>& connection) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000550 std::lock_guard<std::mutex> _l(mMutex);
Steven Moreland19fc9f72021-06-10 03:57:30 +0000551 if (auto it = std::find(mIncomingConnections.begin(), mIncomingConnections.end(), connection);
552 it != mIncomingConnections.end()) {
553 mIncomingConnections.erase(it);
554 if (mIncomingConnections.size() == 0) {
Steven Moreland659416d2021-05-11 00:47:50 +0000555 sp<EventListener> listener = mEventListener.promote();
556 if (listener) {
Steven Moreland19fc9f72021-06-10 03:57:30 +0000557 listener->onSessionLockedAllIncomingThreadsEnded(
558 sp<RpcSession>::fromExisting(this));
Steven Morelanda86e8fe2021-05-26 22:52:35 +0000559 }
Steven Morelandee78e762021-05-05 21:12:51 +0000560 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000561 return true;
562 }
563 return false;
564}
565
Steven Moreland195edb82021-06-08 02:44:39 +0000566status_t RpcSession::ExclusiveConnection::find(const sp<RpcSession>& session, ConnectionUse use,
567 ExclusiveConnection* connection) {
568 connection->mSession = session;
569 connection->mConnection = nullptr;
570 connection->mReentrant = false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000571
Steven Moreland195edb82021-06-08 02:44:39 +0000572 pid_t tid = gettid();
573 std::unique_lock<std::mutex> _l(session->mMutex);
574
575 session->mWaitingThreads++;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000576 while (true) {
577 sp<RpcConnection> exclusive;
578 sp<RpcConnection> available;
579
580 // CHECK FOR DEDICATED CLIENT SOCKET
581 //
Steven Moreland85e067b2021-05-26 17:43:53 +0000582 // A server/looper should always use a dedicated connection if available
Steven Moreland19fc9f72021-06-10 03:57:30 +0000583 findConnection(tid, &exclusive, &available, session->mOutgoingConnections,
584 session->mOutgoingConnectionsOffset);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000585
586 // WARNING: this assumes a server cannot request its client to send
Steven Moreland19fc9f72021-06-10 03:57:30 +0000587 // a transaction, as mIncomingConnections is excluded below.
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000588 //
589 // Imagine we have more than one thread in play, and a single thread
590 // sends a synchronous, then an asynchronous command. Imagine the
591 // asynchronous command is sent on the first client connection. Then, if
592 // we naively send a synchronous command to that same connection, the
593 // thread on the far side might be busy processing the asynchronous
594 // command. So, we move to considering the second available thread
595 // for subsequent calls.
596 if (use == ConnectionUse::CLIENT_ASYNC && (exclusive != nullptr || available != nullptr)) {
Steven Moreland19fc9f72021-06-10 03:57:30 +0000597 session->mOutgoingConnectionsOffset = (session->mOutgoingConnectionsOffset + 1) %
598 session->mOutgoingConnections.size();
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000599 }
600
Steven Morelandc7d40132021-06-10 03:42:11 +0000601 // USE SERVING SOCKET (e.g. nested transaction)
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000602 if (use != ConnectionUse::CLIENT_ASYNC) {
Steven Moreland19fc9f72021-06-10 03:57:30 +0000603 sp<RpcConnection> exclusiveIncoming;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000604 // server connections are always assigned to a thread
Steven Moreland19fc9f72021-06-10 03:57:30 +0000605 findConnection(tid, &exclusiveIncoming, nullptr /*available*/,
606 session->mIncomingConnections, 0 /* index hint */);
Steven Morelandc7d40132021-06-10 03:42:11 +0000607
608 // asynchronous calls cannot be nested, we currently allow ref count
609 // calls to be nested (so that you can use this without having extra
610 // threads). Note 'drainCommands' is used so that these ref counts can't
611 // build up.
Steven Moreland19fc9f72021-06-10 03:57:30 +0000612 if (exclusiveIncoming != nullptr) {
613 if (exclusiveIncoming->allowNested) {
Steven Morelandc7d40132021-06-10 03:42:11 +0000614 // guaranteed to be processed as nested command
Steven Moreland19fc9f72021-06-10 03:57:30 +0000615 exclusive = exclusiveIncoming;
Steven Morelandc7d40132021-06-10 03:42:11 +0000616 } else if (use == ConnectionUse::CLIENT_REFCOUNT && available == nullptr) {
617 // prefer available socket, but if we don't have one, don't
618 // wait for one
Steven Moreland19fc9f72021-06-10 03:57:30 +0000619 exclusive = exclusiveIncoming;
Steven Morelandc7d40132021-06-10 03:42:11 +0000620 }
621 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000622 }
623
Steven Moreland85e067b2021-05-26 17:43:53 +0000624 // if our thread is already using a connection, prioritize using that
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000625 if (exclusive != nullptr) {
Steven Moreland195edb82021-06-08 02:44:39 +0000626 connection->mConnection = exclusive;
627 connection->mReentrant = true;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000628 break;
629 } else if (available != nullptr) {
Steven Moreland195edb82021-06-08 02:44:39 +0000630 connection->mConnection = available;
631 connection->mConnection->exclusiveTid = tid;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000632 break;
633 }
634
Steven Moreland19fc9f72021-06-10 03:57:30 +0000635 if (session->mOutgoingConnections.size() == 0) {
Steven Moreland195edb82021-06-08 02:44:39 +0000636 ALOGE("Session has no client connections. This is required for an RPC server to make "
637 "any non-nested (e.g. oneway or on another thread) calls. Use: %d. Server "
638 "connections: %zu",
Steven Moreland19fc9f72021-06-10 03:57:30 +0000639 static_cast<int>(use), session->mIncomingConnections.size());
Steven Moreland195edb82021-06-08 02:44:39 +0000640 return WOULD_BLOCK;
641 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000642
Steven Moreland85e067b2021-05-26 17:43:53 +0000643 LOG_RPC_DETAIL("No available connections (have %zu clients and %zu servers). Waiting...",
Steven Moreland19fc9f72021-06-10 03:57:30 +0000644 session->mOutgoingConnections.size(), session->mIncomingConnections.size());
Steven Moreland195edb82021-06-08 02:44:39 +0000645 session->mAvailableConnectionCv.wait(_l);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000646 }
Steven Moreland195edb82021-06-08 02:44:39 +0000647 session->mWaitingThreads--;
648
649 return OK;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000650}
651
652void RpcSession::ExclusiveConnection::findConnection(pid_t tid, sp<RpcConnection>* exclusive,
653 sp<RpcConnection>* available,
654 std::vector<sp<RpcConnection>>& sockets,
655 size_t socketsIndexHint) {
656 LOG_ALWAYS_FATAL_IF(sockets.size() > 0 && socketsIndexHint >= sockets.size(),
657 "Bad index %zu >= %zu", socketsIndexHint, sockets.size());
658
659 if (*exclusive != nullptr) return; // consistent with break below
660
661 for (size_t i = 0; i < sockets.size(); i++) {
662 sp<RpcConnection>& socket = sockets[(i + socketsIndexHint) % sockets.size()];
663
Steven Moreland85e067b2021-05-26 17:43:53 +0000664 // take first available connection (intuition = caching)
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000665 if (available && *available == nullptr && socket->exclusiveTid == std::nullopt) {
666 *available = socket;
667 continue;
668 }
669
Steven Moreland85e067b2021-05-26 17:43:53 +0000670 // though, prefer to take connection which is already inuse by this thread
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000671 // (nested transactions)
672 if (exclusive && socket->exclusiveTid == tid) {
673 *exclusive = socket;
674 break; // consistent with return above
675 }
676 }
677}
678
679RpcSession::ExclusiveConnection::~ExclusiveConnection() {
Steven Moreland85e067b2021-05-26 17:43:53 +0000680 // reentrant use of a connection means something less deep in the call stack
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000681 // is using this fd, and it retains the right to it. So, we don't give up
682 // exclusive ownership, and no thread is freed.
Steven Moreland195edb82021-06-08 02:44:39 +0000683 if (!mReentrant && mConnection != nullptr) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000684 std::unique_lock<std::mutex> _l(mSession->mMutex);
685 mConnection->exclusiveTid = std::nullopt;
686 if (mSession->mWaitingThreads > 0) {
687 _l.unlock();
688 mSession->mAvailableConnectionCv.notify_one();
689 }
690 }
691}
692
693} // namespace android