blob: 151b421d34898af25d0cebfaa10d3e0b9bfd6346 [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 Hong194acf22021-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 Hong194acf22021-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 Hong194acf22021-06-29 18:44:56 -070030#include <android_runtime/vm.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>
Yifan Hong194acf22021-06-29 18:44:56 -070034#include <jni.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000035#include <utils/String8.h>
36
37#include "RpcSocketAddress.h"
38#include "RpcState.h"
39#include "RpcWireFormat.h"
40
41#ifdef __GLIBC__
42extern "C" pid_t gettid();
43#endif
44
45namespace android {
46
47using base::unique_fd;
48
49RpcSession::RpcSession() {
50 LOG_RPC_DETAIL("RpcSession created %p", this);
51
52 mState = std::make_unique<RpcState>();
53}
54RpcSession::~RpcSession() {
55 LOG_RPC_DETAIL("RpcSession destroyed %p", this);
56
57 std::lock_guard<std::mutex> _l(mMutex);
Steven Moreland19fc9f72021-06-10 03:57:30 +000058 LOG_ALWAYS_FATAL_IF(mIncomingConnections.size() != 0,
Steven Morelandbdb53ab2021-05-05 17:57:41 +000059 "Should not be able to destroy a session with servers in use.");
60}
61
62sp<RpcSession> RpcSession::make() {
63 return sp<RpcSession>::make();
64}
65
Steven Moreland103424e2021-06-02 18:16:19 +000066void RpcSession::setMaxThreads(size_t threads) {
67 std::lock_guard<std::mutex> _l(mMutex);
Steven Moreland19fc9f72021-06-10 03:57:30 +000068 LOG_ALWAYS_FATAL_IF(!mOutgoingConnections.empty() || !mIncomingConnections.empty(),
Steven Moreland103424e2021-06-02 18:16:19 +000069 "Must set max threads before setting up connections, but has %zu client(s) "
70 "and %zu server(s)",
Steven Moreland19fc9f72021-06-10 03:57:30 +000071 mOutgoingConnections.size(), mIncomingConnections.size());
Steven Moreland103424e2021-06-02 18:16:19 +000072 mMaxThreads = threads;
73}
74
75size_t RpcSession::getMaxThreads() {
76 std::lock_guard<std::mutex> _l(mMutex);
77 return mMaxThreads;
Steven Moreland659416d2021-05-11 00:47:50 +000078}
79
Steven Morelandbdb53ab2021-05-05 17:57:41 +000080bool RpcSession::setupUnixDomainClient(const char* path) {
81 return setupSocketClient(UnixSocketAddress(path));
82}
83
Steven Morelandbdb53ab2021-05-05 17:57:41 +000084bool RpcSession::setupVsockClient(unsigned int cid, unsigned int port) {
85 return setupSocketClient(VsockSocketAddress(cid, port));
86}
87
Steven Morelandbdb53ab2021-05-05 17:57:41 +000088bool RpcSession::setupInetClient(const char* addr, unsigned int port) {
89 auto aiStart = InetSocketAddress::getAddrInfo(addr, port);
90 if (aiStart == nullptr) return false;
91 for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) {
92 InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, addr, port);
93 if (setupSocketClient(socketAddress)) return true;
94 }
95 ALOGE("None of the socket address resolved for %s:%u can be added as inet client.", addr, port);
96 return false;
97}
98
99bool RpcSession::addNullDebuggingClient() {
100 unique_fd serverFd(TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY | O_CLOEXEC)));
101
102 if (serverFd == -1) {
103 ALOGE("Could not connect to /dev/null: %s", strerror(errno));
104 return false;
105 }
106
Steven Morelandb86e26b2021-06-12 00:35:58 +0000107 return addOutgoingConnection(std::move(serverFd), false);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000108}
109
110sp<IBinder> RpcSession::getRootObject() {
Steven Moreland195edb82021-06-08 02:44:39 +0000111 ExclusiveConnection connection;
112 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
113 ConnectionUse::CLIENT, &connection);
114 if (status != OK) return nullptr;
Steven Moreland5ae62562021-06-10 03:21:42 +0000115 return state()->getRootObject(connection.get(), sp<RpcSession>::fromExisting(this));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000116}
117
Steven Moreland1be91352021-05-11 22:12:15 +0000118status_t RpcSession::getRemoteMaxThreads(size_t* maxThreads) {
Steven Moreland195edb82021-06-08 02:44:39 +0000119 ExclusiveConnection connection;
120 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
121 ConnectionUse::CLIENT, &connection);
122 if (status != OK) return status;
Steven Moreland5ae62562021-06-10 03:21:42 +0000123 return state()->getMaxThreads(connection.get(), sp<RpcSession>::fromExisting(this), maxThreads);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000124}
125
Steven Morelandc9d7b532021-06-04 20:57:41 +0000126bool RpcSession::shutdownAndWait(bool wait) {
Steven Moreland659416d2021-05-11 00:47:50 +0000127 std::unique_lock<std::mutex> _l(mMutex);
Steven Moreland659416d2021-05-11 00:47:50 +0000128 LOG_ALWAYS_FATAL_IF(mShutdownTrigger == nullptr, "Shutdown trigger not installed");
Steven Moreland659416d2021-05-11 00:47:50 +0000129
130 mShutdownTrigger->trigger();
Steven Moreland659416d2021-05-11 00:47:50 +0000131
Steven Morelandc9d7b532021-06-04 20:57:41 +0000132 if (wait) {
133 LOG_ALWAYS_FATAL_IF(mShutdownListener == nullptr, "Shutdown listener not installed");
134 mShutdownListener->waitForShutdown(_l);
135 LOG_ALWAYS_FATAL_IF(!mThreads.empty(), "Shutdown failed");
136 }
137
138 _l.unlock();
139 mState->clear();
140
Steven Moreland659416d2021-05-11 00:47:50 +0000141 return true;
142}
143
Steven Morelandf5174272021-05-25 00:39:28 +0000144status_t RpcSession::transact(const sp<IBinder>& binder, uint32_t code, const Parcel& data,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000145 Parcel* reply, uint32_t flags) {
Steven Moreland195edb82021-06-08 02:44:39 +0000146 ExclusiveConnection connection;
147 status_t status =
148 ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
149 (flags & IBinder::FLAG_ONEWAY) ? ConnectionUse::CLIENT_ASYNC
150 : ConnectionUse::CLIENT,
151 &connection);
152 if (status != OK) return status;
Steven Moreland5ae62562021-06-10 03:21:42 +0000153 return state()->transact(connection.get(), binder, code, data,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000154 sp<RpcSession>::fromExisting(this), reply, flags);
155}
156
157status_t RpcSession::sendDecStrong(const RpcAddress& address) {
Steven Moreland195edb82021-06-08 02:44:39 +0000158 ExclusiveConnection connection;
159 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
160 ConnectionUse::CLIENT_REFCOUNT, &connection);
161 if (status != OK) return status;
Steven Moreland5ae62562021-06-10 03:21:42 +0000162 return state()->sendDecStrong(connection.get(), sp<RpcSession>::fromExisting(this), address);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000163}
164
Steven Morelande47511f2021-05-20 00:07:41 +0000165std::unique_ptr<RpcSession::FdTrigger> RpcSession::FdTrigger::make() {
166 auto ret = std::make_unique<RpcSession::FdTrigger>();
Steven Morelanda8b44292021-06-08 01:27:53 +0000167 if (!android::base::Pipe(&ret->mRead, &ret->mWrite)) {
168 ALOGE("Could not create pipe %s", strerror(errno));
169 return nullptr;
170 }
Steven Morelande47511f2021-05-20 00:07:41 +0000171 return ret;
172}
173
174void RpcSession::FdTrigger::trigger() {
175 mWrite.reset();
176}
177
Steven Morelanda8b44292021-06-08 01:27:53 +0000178bool RpcSession::FdTrigger::isTriggered() {
179 return mWrite == -1;
180}
181
Steven Moreland2b4f3802021-05-22 01:46:27 +0000182status_t RpcSession::FdTrigger::triggerablePollRead(base::borrowed_fd fd) {
Steven Moreland4ec3c432021-05-20 00:32:47 +0000183 while (true) {
Steven Morelanddfe3be92021-05-22 00:24:29 +0000184 pollfd pfd[]{{.fd = fd.get(), .events = POLLIN | POLLHUP, .revents = 0},
Steven Moreland4ec3c432021-05-20 00:32:47 +0000185 {.fd = mRead.get(), .events = POLLHUP, .revents = 0}};
186 int ret = TEMP_FAILURE_RETRY(poll(pfd, arraysize(pfd), -1));
187 if (ret < 0) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000188 return -errno;
Steven Moreland4ec3c432021-05-20 00:32:47 +0000189 }
190 if (ret == 0) {
191 continue;
192 }
193 if (pfd[1].revents & POLLHUP) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000194 return -ECANCELED;
Steven Moreland4ec3c432021-05-20 00:32:47 +0000195 }
Steven Moreland2b4f3802021-05-22 01:46:27 +0000196 return pfd[0].revents & POLLIN ? OK : DEAD_OBJECT;
Steven Moreland4ec3c432021-05-20 00:32:47 +0000197 }
198}
199
Steven Moreland2b4f3802021-05-22 01:46:27 +0000200status_t RpcSession::FdTrigger::interruptableReadFully(base::borrowed_fd fd, void* data,
201 size_t size) {
Steven Moreland9d11b922021-05-20 01:22:58 +0000202 uint8_t* buffer = reinterpret_cast<uint8_t*>(data);
203 uint8_t* end = buffer + size;
204
Steven Morelandb8176792021-06-22 20:29:21 +0000205 MAYBE_WAIT_IN_FLAKE_MODE;
206
Steven Moreland2b4f3802021-05-22 01:46:27 +0000207 status_t status;
208 while ((status = triggerablePollRead(fd)) == OK) {
Steven Moreland9d11b922021-05-20 01:22:58 +0000209 ssize_t readSize = TEMP_FAILURE_RETRY(recv(fd.get(), buffer, end - buffer, MSG_NOSIGNAL));
Steven Moreland2b4f3802021-05-22 01:46:27 +0000210 if (readSize == 0) return DEAD_OBJECT; // EOF
Steven Morelanddfe3be92021-05-22 00:24:29 +0000211
Steven Moreland9d11b922021-05-20 01:22:58 +0000212 if (readSize < 0) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000213 return -errno;
Steven Moreland9d11b922021-05-20 01:22:58 +0000214 }
215 buffer += readSize;
Steven Moreland2b4f3802021-05-22 01:46:27 +0000216 if (buffer == end) return OK;
Steven Moreland9d11b922021-05-20 01:22:58 +0000217 }
Steven Moreland2b4f3802021-05-22 01:46:27 +0000218 return status;
Steven Moreland9d11b922021-05-20 01:22:58 +0000219}
220
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000221status_t RpcSession::readId() {
222 {
223 std::lock_guard<std::mutex> _l(mMutex);
224 LOG_ALWAYS_FATAL_IF(mForServer != nullptr, "Can only update ID for client.");
225 }
226
Steven Moreland195edb82021-06-08 02:44:39 +0000227 ExclusiveConnection connection;
228 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
229 ConnectionUse::CLIENT, &connection);
230 if (status != OK) return status;
231
Steven Moreland01a6bad2021-06-11 00:59:20 +0000232 mId = RpcAddress::zero();
233 status = state()->getSessionId(connection.get(), sp<RpcSession>::fromExisting(this),
234 &mId.value());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000235 if (status != OK) return status;
236
Steven Moreland01a6bad2021-06-11 00:59:20 +0000237 LOG_RPC_DETAIL("RpcSession %p has id %s", this, mId->toString().c_str());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000238 return OK;
239}
240
Steven Moreland19fc9f72021-06-10 03:57:30 +0000241void RpcSession::WaitForShutdownListener::onSessionLockedAllIncomingThreadsEnded(
Steven Moreland659416d2021-05-11 00:47:50 +0000242 const sp<RpcSession>& session) {
243 (void)session;
244 mShutdown = true;
245}
246
Steven Moreland19fc9f72021-06-10 03:57:30 +0000247void RpcSession::WaitForShutdownListener::onSessionIncomingThreadEnded() {
Steven Moreland659416d2021-05-11 00:47:50 +0000248 mCv.notify_all();
249}
250
251void RpcSession::WaitForShutdownListener::waitForShutdown(std::unique_lock<std::mutex>& lock) {
252 while (!mShutdown) {
253 if (std::cv_status::timeout == mCv.wait_for(lock, std::chrono::seconds(1))) {
254 ALOGE("Waiting for RpcSession to shut down (1s w/o progress).");
255 }
256 }
257}
258
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000259void RpcSession::preJoinThreadOwnership(std::thread thread) {
Steven Morelanda63ff932021-05-12 00:03:15 +0000260 LOG_ALWAYS_FATAL_IF(thread.get_id() != std::this_thread::get_id(), "Must own this thread");
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000261
Steven Morelanda63ff932021-05-12 00:03:15 +0000262 {
263 std::lock_guard<std::mutex> _l(mMutex);
264 mThreads[thread.get_id()] = std::move(thread);
265 }
Steven Moreland5802c2b2021-05-12 20:13:04 +0000266}
Steven Morelanda63ff932021-05-12 00:03:15 +0000267
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000268RpcSession::PreJoinSetupResult RpcSession::preJoinSetup(base::unique_fd fd) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000269 // must be registered to allow arbitrary client code executing commands to
270 // be able to do nested calls (we can't only read from it)
Steven Moreland19fc9f72021-06-10 03:57:30 +0000271 sp<RpcConnection> connection = assignIncomingConnectionToThisThread(std::move(fd));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000272
Steven Moreland5ae62562021-06-10 03:21:42 +0000273 status_t status = mState->readConnectionInit(connection, sp<RpcSession>::fromExisting(this));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000274
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000275 return PreJoinSetupResult{
276 .connection = std::move(connection),
277 .status = status,
278 };
279}
280
Yifan Hong194acf22021-06-29 18:44:56 -0700281namespace {
282// RAII object for attaching / detaching current thread to JVM if Android Runtime exists. If
283// Android Runtime doesn't exist, no-op.
284class JavaThreadAttacher {
285public:
286 JavaThreadAttacher() {
287 // Use dlsym to find androidJavaAttachThread because libandroid_runtime is loaded after
288 // libbinder.
289 auto vm = getJavaVM();
290 if (vm == nullptr) return;
291
292 char threadName[16];
293 if (0 != pthread_getname_np(pthread_self(), threadName, sizeof(threadName))) {
294 constexpr const char* defaultThreadName = "UnknownRpcSessionThread";
295 memcpy(threadName, defaultThreadName,
296 std::min<size_t>(sizeof(threadName), strlen(defaultThreadName) + 1));
297 }
298 LOG_RPC_DETAIL("Attaching current thread %s to JVM", threadName);
299 JavaVMAttachArgs args;
300 args.version = JNI_VERSION_1_2;
301 args.name = threadName;
302 args.group = nullptr;
303 JNIEnv* env;
304
305 LOG_ALWAYS_FATAL_IF(vm->AttachCurrentThread(&env, &args) != JNI_OK,
306 "Cannot attach thread %s to JVM", threadName);
307 mAttached = true;
308 }
309 ~JavaThreadAttacher() {
310 if (!mAttached) return;
311 auto vm = getJavaVM();
312 LOG_ALWAYS_FATAL_IF(vm == nullptr,
313 "Unable to detach thread. No JavaVM, but it was present before!");
314
315 LOG_RPC_DETAIL("Detaching current thread from JVM");
316 if (vm->DetachCurrentThread() != JNI_OK) {
317 mAttached = false;
318 } else {
319 ALOGW("Unable to detach current thread from JVM");
320 }
321 }
322
323private:
324 DISALLOW_COPY_AND_ASSIGN(JavaThreadAttacher);
325 bool mAttached = false;
326
327 static JavaVM* getJavaVM() {
328 static auto fn = reinterpret_cast<decltype(&AndroidRuntimeGetJavaVM)>(
329 dlsym(RTLD_DEFAULT, "AndroidRuntimeGetJavaVM"));
330 if (fn == nullptr) return nullptr;
331 return fn();
332 }
333};
334} // namespace
335
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000336void RpcSession::join(sp<RpcSession>&& session, PreJoinSetupResult&& setupResult) {
337 sp<RpcConnection>& connection = setupResult.connection;
338
339 if (setupResult.status == OK) {
Yifan Hong194acf22021-06-29 18:44:56 -0700340 JavaThreadAttacher javaThreadAttacher;
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000341 while (true) {
Steven Moreland5ae62562021-06-10 03:21:42 +0000342 status_t status = session->state()->getAndExecuteCommand(connection, session,
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000343 RpcState::CommandType::ANY);
344 if (status != OK) {
345 LOG_RPC_DETAIL("Binder connection thread closing w/ status %s",
346 statusToString(status).c_str());
347 break;
348 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000349 }
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000350 } else {
351 ALOGE("Connection failed to init, closing with status %s",
352 statusToString(setupResult.status).c_str());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000353 }
354
Steven Moreland19fc9f72021-06-10 03:57:30 +0000355 LOG_ALWAYS_FATAL_IF(!session->removeIncomingConnection(connection),
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000356 "bad state: connection object guaranteed to be in list");
Steven Morelanda63ff932021-05-12 00:03:15 +0000357
Steven Moreland659416d2021-05-11 00:47:50 +0000358 sp<RpcSession::EventListener> listener;
Steven Morelanda63ff932021-05-12 00:03:15 +0000359 {
Steven Moreland659416d2021-05-11 00:47:50 +0000360 std::lock_guard<std::mutex> _l(session->mMutex);
361 auto it = session->mThreads.find(std::this_thread::get_id());
362 LOG_ALWAYS_FATAL_IF(it == session->mThreads.end());
Steven Morelanda63ff932021-05-12 00:03:15 +0000363 it->second.detach();
Steven Moreland659416d2021-05-11 00:47:50 +0000364 session->mThreads.erase(it);
Steven Morelandee3f4662021-05-22 01:07:33 +0000365
Steven Moreland659416d2021-05-11 00:47:50 +0000366 listener = session->mEventListener.promote();
Steven Morelandee3f4662021-05-22 01:07:33 +0000367 }
368
Steven Moreland659416d2021-05-11 00:47:50 +0000369 session = nullptr;
370
371 if (listener != nullptr) {
Steven Moreland19fc9f72021-06-10 03:57:30 +0000372 listener->onSessionIncomingThreadEnded();
Steven Morelandee78e762021-05-05 21:12:51 +0000373 }
374}
375
Steven Moreland7b8bc4c2021-06-10 22:50:27 +0000376sp<RpcServer> RpcSession::server() {
377 RpcServer* unsafeServer = mForServer.unsafe_get();
378 sp<RpcServer> server = mForServer.promote();
379
380 LOG_ALWAYS_FATAL_IF((unsafeServer == nullptr) != (server == nullptr),
381 "wp<> is to avoid strong cycle only");
382 return server;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000383}
384
385bool RpcSession::setupSocketClient(const RpcSocketAddress& addr) {
386 {
387 std::lock_guard<std::mutex> _l(mMutex);
Steven Moreland19fc9f72021-06-10 03:57:30 +0000388 LOG_ALWAYS_FATAL_IF(mOutgoingConnections.size() != 0,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000389 "Must only setup session once, but already has %zu clients",
Steven Moreland19fc9f72021-06-10 03:57:30 +0000390 mOutgoingConnections.size());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000391 }
392
Steven Moreland01a6bad2021-06-11 00:59:20 +0000393 if (!setupOneSocketConnection(addr, RpcAddress::zero(), false /*reverse*/)) return false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000394
Steven Morelanda5036f02021-06-08 02:26:57 +0000395 // TODO(b/189955605): we should add additional sessions dynamically
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000396 // instead of all at once.
397 // TODO(b/186470974): first risk of blocking
398 size_t numThreadsAvailable;
Steven Moreland1be91352021-05-11 22:12:15 +0000399 if (status_t status = getRemoteMaxThreads(&numThreadsAvailable); status != OK) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000400 ALOGE("Could not get max threads after initial session to %s: %s", addr.toString().c_str(),
401 statusToString(status).c_str());
402 return false;
403 }
404
405 if (status_t status = readId(); status != OK) {
406 ALOGE("Could not get session id after initial session to %s; %s", addr.toString().c_str(),
407 statusToString(status).c_str());
408 return false;
409 }
410
411 // we've already setup one client
412 for (size_t i = 0; i + 1 < numThreadsAvailable; i++) {
Steven Morelanda5036f02021-06-08 02:26:57 +0000413 // TODO(b/189955605): shutdown existing connections?
Steven Moreland659416d2021-05-11 00:47:50 +0000414 if (!setupOneSocketConnection(addr, mId.value(), false /*reverse*/)) return false;
415 }
416
Steven Morelanda5036f02021-06-08 02:26:57 +0000417 // TODO(b/189955605): we should add additional sessions dynamically
Steven Moreland659416d2021-05-11 00:47:50 +0000418 // instead of all at once - the other side should be responsible for setting
419 // up additional connections. We need to create at least one (unless 0 are
420 // requested to be set) in order to allow the other side to reliably make
421 // any requests at all.
422
Steven Moreland103424e2021-06-02 18:16:19 +0000423 for (size_t i = 0; i < mMaxThreads; i++) {
Steven Moreland659416d2021-05-11 00:47:50 +0000424 if (!setupOneSocketConnection(addr, mId.value(), true /*reverse*/)) return false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000425 }
426
427 return true;
428}
429
Steven Moreland01a6bad2021-06-11 00:59:20 +0000430bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, const RpcAddress& id,
431 bool reverse) {
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000432 for (size_t tries = 0; tries < 5; tries++) {
433 if (tries > 0) usleep(10000);
434
435 unique_fd serverFd(
436 TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0)));
437 if (serverFd == -1) {
438 int savedErrno = errno;
439 ALOGE("Could not create socket at %s: %s", addr.toString().c_str(),
440 strerror(savedErrno));
441 return false;
442 }
443
444 if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) {
445 if (errno == ECONNRESET) {
446 ALOGW("Connection reset on %s", addr.toString().c_str());
447 continue;
448 }
449 int savedErrno = errno;
450 ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(),
451 strerror(savedErrno));
452 return false;
453 }
454
Steven Moreland01a6bad2021-06-11 00:59:20 +0000455 RpcConnectionHeader header{.options = 0};
456 memcpy(&header.sessionId, &id.viewRawEmbedded(), sizeof(RpcWireAddress));
457
Steven Moreland659416d2021-05-11 00:47:50 +0000458 if (reverse) header.options |= RPC_CONNECTION_OPTION_REVERSE;
459
460 if (sizeof(header) != TEMP_FAILURE_RETRY(write(serverFd.get(), &header, sizeof(header)))) {
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000461 int savedErrno = errno;
Steven Moreland659416d2021-05-11 00:47:50 +0000462 ALOGE("Could not write connection header to socket at %s: %s", addr.toString().c_str(),
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000463 strerror(savedErrno));
464 return false;
465 }
466
467 LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get());
468
Steven Moreland659416d2021-05-11 00:47:50 +0000469 if (reverse) {
470 std::mutex mutex;
471 std::condition_variable joinCv;
472 std::unique_lock<std::mutex> lock(mutex);
473 std::thread thread;
474 sp<RpcSession> thiz = sp<RpcSession>::fromExisting(this);
475 bool ownershipTransferred = false;
476 thread = std::thread([&]() {
477 std::unique_lock<std::mutex> threadLock(mutex);
478 unique_fd fd = std::move(serverFd);
479 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
480 sp<RpcSession> session = thiz;
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000481 session->preJoinThreadOwnership(std::move(thread));
Steven Moreland659416d2021-05-11 00:47:50 +0000482
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000483 // only continue once we have a response or the connection fails
484 auto setupResult = session->preJoinSetup(std::move(fd));
485
486 ownershipTransferred = true;
Steven Moreland659416d2021-05-11 00:47:50 +0000487 threadLock.unlock();
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000488 joinCv.notify_one();
Steven Moreland659416d2021-05-11 00:47:50 +0000489 // do not use & vars below
490
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000491 RpcSession::join(std::move(session), std::move(setupResult));
Steven Moreland659416d2021-05-11 00:47:50 +0000492 });
493 joinCv.wait(lock, [&] { return ownershipTransferred; });
494 LOG_ALWAYS_FATAL_IF(!ownershipTransferred);
495 return true;
496 } else {
Steven Morelandb86e26b2021-06-12 00:35:58 +0000497 return addOutgoingConnection(std::move(serverFd), true);
Steven Moreland659416d2021-05-11 00:47:50 +0000498 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000499 }
500
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000501 ALOGE("Ran out of retries to connect to %s", addr.toString().c_str());
502 return false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000503}
504
Steven Morelandb86e26b2021-06-12 00:35:58 +0000505bool RpcSession::addOutgoingConnection(unique_fd fd, bool init) {
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000506 sp<RpcConnection> connection = sp<RpcConnection>::make();
507 {
508 std::lock_guard<std::mutex> _l(mMutex);
Steven Morelandee3f4662021-05-22 01:07:33 +0000509
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000510 // first client connection added, but setForServer not called, so
511 // initializaing for a client.
512 if (mShutdownTrigger == nullptr) {
513 mShutdownTrigger = FdTrigger::make();
514 mEventListener = mShutdownListener = sp<WaitForShutdownListener>::make();
515 if (mShutdownTrigger == nullptr) return false;
516 }
517
518 connection->fd = std::move(fd);
519 connection->exclusiveTid = gettid();
Steven Moreland19fc9f72021-06-10 03:57:30 +0000520 mOutgoingConnections.push_back(connection);
Steven Morelandee3f4662021-05-22 01:07:33 +0000521 }
522
Steven Morelandb86e26b2021-06-12 00:35:58 +0000523 status_t status = OK;
524 if (init) {
525 mState->sendConnectionInit(connection, sp<RpcSession>::fromExisting(this));
526 }
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000527
528 {
529 std::lock_guard<std::mutex> _l(mMutex);
530 connection->exclusiveTid = std::nullopt;
531 }
532
533 return status == OK;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000534}
535
Steven Morelanda8b44292021-06-08 01:27:53 +0000536bool RpcSession::setForServer(const wp<RpcServer>& server, const wp<EventListener>& eventListener,
Steven Moreland01a6bad2021-06-11 00:59:20 +0000537 const RpcAddress& sessionId) {
Steven Moreland659416d2021-05-11 00:47:50 +0000538 LOG_ALWAYS_FATAL_IF(mForServer != nullptr);
539 LOG_ALWAYS_FATAL_IF(server == nullptr);
540 LOG_ALWAYS_FATAL_IF(mEventListener != nullptr);
541 LOG_ALWAYS_FATAL_IF(eventListener == nullptr);
Steven Morelandee3f4662021-05-22 01:07:33 +0000542 LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr);
Steven Morelanda8b44292021-06-08 01:27:53 +0000543
544 mShutdownTrigger = FdTrigger::make();
545 if (mShutdownTrigger == nullptr) return false;
Steven Morelandee3f4662021-05-22 01:07:33 +0000546
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000547 mId = sessionId;
548 mForServer = server;
Steven Moreland659416d2021-05-11 00:47:50 +0000549 mEventListener = eventListener;
Steven Morelanda8b44292021-06-08 01:27:53 +0000550 return true;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000551}
552
Steven Moreland19fc9f72021-06-10 03:57:30 +0000553sp<RpcSession::RpcConnection> RpcSession::assignIncomingConnectionToThisThread(unique_fd fd) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000554 std::lock_guard<std::mutex> _l(mMutex);
555 sp<RpcConnection> session = sp<RpcConnection>::make();
556 session->fd = std::move(fd);
557 session->exclusiveTid = gettid();
Steven Moreland19fc9f72021-06-10 03:57:30 +0000558 mIncomingConnections.push_back(session);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000559
560 return session;
561}
562
Steven Moreland19fc9f72021-06-10 03:57:30 +0000563bool RpcSession::removeIncomingConnection(const sp<RpcConnection>& connection) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000564 std::lock_guard<std::mutex> _l(mMutex);
Steven Moreland19fc9f72021-06-10 03:57:30 +0000565 if (auto it = std::find(mIncomingConnections.begin(), mIncomingConnections.end(), connection);
566 it != mIncomingConnections.end()) {
567 mIncomingConnections.erase(it);
568 if (mIncomingConnections.size() == 0) {
Steven Moreland659416d2021-05-11 00:47:50 +0000569 sp<EventListener> listener = mEventListener.promote();
570 if (listener) {
Steven Moreland19fc9f72021-06-10 03:57:30 +0000571 listener->onSessionLockedAllIncomingThreadsEnded(
572 sp<RpcSession>::fromExisting(this));
Steven Morelanda86e8fe2021-05-26 22:52:35 +0000573 }
Steven Morelandee78e762021-05-05 21:12:51 +0000574 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000575 return true;
576 }
577 return false;
578}
579
Steven Moreland195edb82021-06-08 02:44:39 +0000580status_t RpcSession::ExclusiveConnection::find(const sp<RpcSession>& session, ConnectionUse use,
581 ExclusiveConnection* connection) {
582 connection->mSession = session;
583 connection->mConnection = nullptr;
584 connection->mReentrant = false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000585
Steven Moreland195edb82021-06-08 02:44:39 +0000586 pid_t tid = gettid();
587 std::unique_lock<std::mutex> _l(session->mMutex);
588
589 session->mWaitingThreads++;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000590 while (true) {
591 sp<RpcConnection> exclusive;
592 sp<RpcConnection> available;
593
594 // CHECK FOR DEDICATED CLIENT SOCKET
595 //
Steven Moreland85e067b2021-05-26 17:43:53 +0000596 // A server/looper should always use a dedicated connection if available
Steven Moreland19fc9f72021-06-10 03:57:30 +0000597 findConnection(tid, &exclusive, &available, session->mOutgoingConnections,
598 session->mOutgoingConnectionsOffset);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000599
600 // WARNING: this assumes a server cannot request its client to send
Steven Moreland19fc9f72021-06-10 03:57:30 +0000601 // a transaction, as mIncomingConnections is excluded below.
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000602 //
603 // Imagine we have more than one thread in play, and a single thread
604 // sends a synchronous, then an asynchronous command. Imagine the
605 // asynchronous command is sent on the first client connection. Then, if
606 // we naively send a synchronous command to that same connection, the
607 // thread on the far side might be busy processing the asynchronous
608 // command. So, we move to considering the second available thread
609 // for subsequent calls.
610 if (use == ConnectionUse::CLIENT_ASYNC && (exclusive != nullptr || available != nullptr)) {
Steven Moreland19fc9f72021-06-10 03:57:30 +0000611 session->mOutgoingConnectionsOffset = (session->mOutgoingConnectionsOffset + 1) %
612 session->mOutgoingConnections.size();
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000613 }
614
Steven Morelandc7d40132021-06-10 03:42:11 +0000615 // USE SERVING SOCKET (e.g. nested transaction)
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000616 if (use != ConnectionUse::CLIENT_ASYNC) {
Steven Moreland19fc9f72021-06-10 03:57:30 +0000617 sp<RpcConnection> exclusiveIncoming;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000618 // server connections are always assigned to a thread
Steven Moreland19fc9f72021-06-10 03:57:30 +0000619 findConnection(tid, &exclusiveIncoming, nullptr /*available*/,
620 session->mIncomingConnections, 0 /* index hint */);
Steven Morelandc7d40132021-06-10 03:42:11 +0000621
622 // asynchronous calls cannot be nested, we currently allow ref count
623 // calls to be nested (so that you can use this without having extra
624 // threads). Note 'drainCommands' is used so that these ref counts can't
625 // build up.
Steven Moreland19fc9f72021-06-10 03:57:30 +0000626 if (exclusiveIncoming != nullptr) {
627 if (exclusiveIncoming->allowNested) {
Steven Morelandc7d40132021-06-10 03:42:11 +0000628 // guaranteed to be processed as nested command
Steven Moreland19fc9f72021-06-10 03:57:30 +0000629 exclusive = exclusiveIncoming;
Steven Morelandc7d40132021-06-10 03:42:11 +0000630 } else if (use == ConnectionUse::CLIENT_REFCOUNT && available == nullptr) {
631 // prefer available socket, but if we don't have one, don't
632 // wait for one
Steven Moreland19fc9f72021-06-10 03:57:30 +0000633 exclusive = exclusiveIncoming;
Steven Morelandc7d40132021-06-10 03:42:11 +0000634 }
635 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000636 }
637
Steven Moreland85e067b2021-05-26 17:43:53 +0000638 // if our thread is already using a connection, prioritize using that
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000639 if (exclusive != nullptr) {
Steven Moreland195edb82021-06-08 02:44:39 +0000640 connection->mConnection = exclusive;
641 connection->mReentrant = true;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000642 break;
643 } else if (available != nullptr) {
Steven Moreland195edb82021-06-08 02:44:39 +0000644 connection->mConnection = available;
645 connection->mConnection->exclusiveTid = tid;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000646 break;
647 }
648
Steven Moreland19fc9f72021-06-10 03:57:30 +0000649 if (session->mOutgoingConnections.size() == 0) {
Steven Moreland195edb82021-06-08 02:44:39 +0000650 ALOGE("Session has no client connections. This is required for an RPC server to make "
651 "any non-nested (e.g. oneway or on another thread) calls. Use: %d. Server "
652 "connections: %zu",
Steven Moreland19fc9f72021-06-10 03:57:30 +0000653 static_cast<int>(use), session->mIncomingConnections.size());
Steven Moreland195edb82021-06-08 02:44:39 +0000654 return WOULD_BLOCK;
655 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000656
Steven Moreland85e067b2021-05-26 17:43:53 +0000657 LOG_RPC_DETAIL("No available connections (have %zu clients and %zu servers). Waiting...",
Steven Moreland19fc9f72021-06-10 03:57:30 +0000658 session->mOutgoingConnections.size(), session->mIncomingConnections.size());
Steven Moreland195edb82021-06-08 02:44:39 +0000659 session->mAvailableConnectionCv.wait(_l);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000660 }
Steven Moreland195edb82021-06-08 02:44:39 +0000661 session->mWaitingThreads--;
662
663 return OK;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000664}
665
666void RpcSession::ExclusiveConnection::findConnection(pid_t tid, sp<RpcConnection>* exclusive,
667 sp<RpcConnection>* available,
668 std::vector<sp<RpcConnection>>& sockets,
669 size_t socketsIndexHint) {
670 LOG_ALWAYS_FATAL_IF(sockets.size() > 0 && socketsIndexHint >= sockets.size(),
671 "Bad index %zu >= %zu", socketsIndexHint, sockets.size());
672
673 if (*exclusive != nullptr) return; // consistent with break below
674
675 for (size_t i = 0; i < sockets.size(); i++) {
676 sp<RpcConnection>& socket = sockets[(i + socketsIndexHint) % sockets.size()];
677
Steven Moreland85e067b2021-05-26 17:43:53 +0000678 // take first available connection (intuition = caching)
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000679 if (available && *available == nullptr && socket->exclusiveTid == std::nullopt) {
680 *available = socket;
681 continue;
682 }
683
Steven Moreland85e067b2021-05-26 17:43:53 +0000684 // though, prefer to take connection which is already inuse by this thread
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000685 // (nested transactions)
686 if (exclusive && socket->exclusiveTid == tid) {
687 *exclusive = socket;
688 break; // consistent with return above
689 }
690 }
691}
692
693RpcSession::ExclusiveConnection::~ExclusiveConnection() {
Steven Moreland85e067b2021-05-26 17:43:53 +0000694 // reentrant use of a connection means something less deep in the call stack
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000695 // is using this fd, and it retains the right to it. So, we don't give up
696 // exclusive ownership, and no thread is freed.
Steven Moreland195edb82021-06-08 02:44:39 +0000697 if (!mReentrant && mConnection != nullptr) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000698 std::unique_lock<std::mutex> _l(mSession->mMutex);
699 mConnection->exclusiveTid = std::nullopt;
700 if (mSession->mWaitingThreads > 0) {
701 _l.unlock();
702 mSession->mAvailableConnectionCv.notify_one();
703 }
704 }
705}
706
707} // namespace android