blob: 5d1df83fe20d8f54158413d23bdeb1fa5462d12f [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);
Steven Morelanddd67b942021-07-23 17:15:41 -0700135
Steven Morelandc9d7b532021-06-04 20:57:41 +0000136 LOG_ALWAYS_FATAL_IF(!mThreads.empty(), "Shutdown failed");
137 }
138
139 _l.unlock();
140 mState->clear();
141
Steven Moreland659416d2021-05-11 00:47:50 +0000142 return true;
143}
144
Steven Morelandf5174272021-05-25 00:39:28 +0000145status_t RpcSession::transact(const sp<IBinder>& binder, uint32_t code, const Parcel& data,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000146 Parcel* reply, uint32_t flags) {
Steven Moreland195edb82021-06-08 02:44:39 +0000147 ExclusiveConnection connection;
148 status_t status =
149 ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
150 (flags & IBinder::FLAG_ONEWAY) ? ConnectionUse::CLIENT_ASYNC
151 : ConnectionUse::CLIENT,
152 &connection);
153 if (status != OK) return status;
Steven Moreland5ae62562021-06-10 03:21:42 +0000154 return state()->transact(connection.get(), binder, code, data,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000155 sp<RpcSession>::fromExisting(this), reply, flags);
156}
157
158status_t RpcSession::sendDecStrong(const RpcAddress& address) {
Steven Moreland195edb82021-06-08 02:44:39 +0000159 ExclusiveConnection connection;
160 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
161 ConnectionUse::CLIENT_REFCOUNT, &connection);
162 if (status != OK) return status;
Steven Moreland5ae62562021-06-10 03:21:42 +0000163 return state()->sendDecStrong(connection.get(), sp<RpcSession>::fromExisting(this), address);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000164}
165
Steven Morelande47511f2021-05-20 00:07:41 +0000166std::unique_ptr<RpcSession::FdTrigger> RpcSession::FdTrigger::make() {
167 auto ret = std::make_unique<RpcSession::FdTrigger>();
Steven Morelanda8b44292021-06-08 01:27:53 +0000168 if (!android::base::Pipe(&ret->mRead, &ret->mWrite)) {
169 ALOGE("Could not create pipe %s", strerror(errno));
170 return nullptr;
171 }
Steven Morelande47511f2021-05-20 00:07:41 +0000172 return ret;
173}
174
175void RpcSession::FdTrigger::trigger() {
176 mWrite.reset();
177}
178
Steven Morelanda8b44292021-06-08 01:27:53 +0000179bool RpcSession::FdTrigger::isTriggered() {
180 return mWrite == -1;
181}
182
Steven Moreland798e0d12021-07-14 23:19:25 +0000183status_t RpcSession::FdTrigger::triggerablePoll(base::borrowed_fd fd, int16_t event) {
Steven Moreland4ec3c432021-05-20 00:32:47 +0000184 while (true) {
Steven Moreland798e0d12021-07-14 23:19:25 +0000185 pollfd pfd[]{{.fd = fd.get(),
186 .events = static_cast<int16_t>(event | POLLHUP),
187 .revents = 0},
Steven Moreland4ec3c432021-05-20 00:32:47 +0000188 {.fd = mRead.get(), .events = POLLHUP, .revents = 0}};
189 int ret = TEMP_FAILURE_RETRY(poll(pfd, arraysize(pfd), -1));
190 if (ret < 0) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000191 return -errno;
Steven Moreland4ec3c432021-05-20 00:32:47 +0000192 }
193 if (ret == 0) {
194 continue;
195 }
196 if (pfd[1].revents & POLLHUP) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000197 return -ECANCELED;
Steven Moreland4ec3c432021-05-20 00:32:47 +0000198 }
Steven Moreland798e0d12021-07-14 23:19:25 +0000199 return pfd[0].revents & event ? OK : DEAD_OBJECT;
Steven Moreland4ec3c432021-05-20 00:32:47 +0000200 }
201}
202
Steven Moreland798e0d12021-07-14 23:19:25 +0000203status_t RpcSession::FdTrigger::interruptableWriteFully(base::borrowed_fd fd, const void* data,
204 size_t size) {
205 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data);
206 const uint8_t* end = buffer + size;
207
208 MAYBE_WAIT_IN_FLAKE_MODE;
209
210 status_t status;
211 while ((status = triggerablePoll(fd, POLLOUT)) == OK) {
212 ssize_t writeSize = TEMP_FAILURE_RETRY(send(fd.get(), buffer, end - buffer, MSG_NOSIGNAL));
213 if (writeSize == 0) return DEAD_OBJECT;
214
215 if (writeSize < 0) {
216 return -errno;
217 }
218 buffer += writeSize;
219 if (buffer == end) return OK;
220 }
221 return status;
222}
223
Steven Moreland2b4f3802021-05-22 01:46:27 +0000224status_t RpcSession::FdTrigger::interruptableReadFully(base::borrowed_fd fd, void* data,
225 size_t size) {
Steven Moreland9d11b922021-05-20 01:22:58 +0000226 uint8_t* buffer = reinterpret_cast<uint8_t*>(data);
227 uint8_t* end = buffer + size;
228
Steven Morelandb8176792021-06-22 20:29:21 +0000229 MAYBE_WAIT_IN_FLAKE_MODE;
230
Steven Moreland2b4f3802021-05-22 01:46:27 +0000231 status_t status;
Steven Moreland798e0d12021-07-14 23:19:25 +0000232 while ((status = triggerablePoll(fd, POLLIN)) == OK) {
Steven Moreland9d11b922021-05-20 01:22:58 +0000233 ssize_t readSize = TEMP_FAILURE_RETRY(recv(fd.get(), buffer, end - buffer, MSG_NOSIGNAL));
Steven Moreland2b4f3802021-05-22 01:46:27 +0000234 if (readSize == 0) return DEAD_OBJECT; // EOF
Steven Morelanddfe3be92021-05-22 00:24:29 +0000235
Steven Moreland9d11b922021-05-20 01:22:58 +0000236 if (readSize < 0) {
Steven Moreland2b4f3802021-05-22 01:46:27 +0000237 return -errno;
Steven Moreland9d11b922021-05-20 01:22:58 +0000238 }
239 buffer += readSize;
Steven Moreland2b4f3802021-05-22 01:46:27 +0000240 if (buffer == end) return OK;
Steven Moreland9d11b922021-05-20 01:22:58 +0000241 }
Steven Moreland2b4f3802021-05-22 01:46:27 +0000242 return status;
Steven Moreland9d11b922021-05-20 01:22:58 +0000243}
244
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000245status_t RpcSession::readId() {
246 {
247 std::lock_guard<std::mutex> _l(mMutex);
248 LOG_ALWAYS_FATAL_IF(mForServer != nullptr, "Can only update ID for client.");
249 }
250
Steven Moreland195edb82021-06-08 02:44:39 +0000251 ExclusiveConnection connection;
252 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
253 ConnectionUse::CLIENT, &connection);
254 if (status != OK) return status;
255
Steven Moreland01a6bad2021-06-11 00:59:20 +0000256 mId = RpcAddress::zero();
257 status = state()->getSessionId(connection.get(), sp<RpcSession>::fromExisting(this),
258 &mId.value());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000259 if (status != OK) return status;
260
Steven Moreland01a6bad2021-06-11 00:59:20 +0000261 LOG_RPC_DETAIL("RpcSession %p has id %s", this, mId->toString().c_str());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000262 return OK;
263}
264
Steven Morelanddd67b942021-07-23 17:15:41 -0700265void RpcSession::WaitForShutdownListener::onSessionAllIncomingThreadsEnded(
Steven Moreland659416d2021-05-11 00:47:50 +0000266 const sp<RpcSession>& session) {
267 (void)session;
268 mShutdown = true;
269}
270
Steven Moreland19fc9f72021-06-10 03:57:30 +0000271void RpcSession::WaitForShutdownListener::onSessionIncomingThreadEnded() {
Steven Moreland659416d2021-05-11 00:47:50 +0000272 mCv.notify_all();
273}
274
275void RpcSession::WaitForShutdownListener::waitForShutdown(std::unique_lock<std::mutex>& lock) {
276 while (!mShutdown) {
277 if (std::cv_status::timeout == mCv.wait_for(lock, std::chrono::seconds(1))) {
278 ALOGE("Waiting for RpcSession to shut down (1s w/o progress).");
279 }
280 }
281}
282
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000283void RpcSession::preJoinThreadOwnership(std::thread thread) {
Steven Morelanda63ff932021-05-12 00:03:15 +0000284 LOG_ALWAYS_FATAL_IF(thread.get_id() != std::this_thread::get_id(), "Must own this thread");
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000285
Steven Morelanda63ff932021-05-12 00:03:15 +0000286 {
287 std::lock_guard<std::mutex> _l(mMutex);
288 mThreads[thread.get_id()] = std::move(thread);
289 }
Steven Moreland5802c2b2021-05-12 20:13:04 +0000290}
Steven Morelanda63ff932021-05-12 00:03:15 +0000291
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000292RpcSession::PreJoinSetupResult RpcSession::preJoinSetup(base::unique_fd fd) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000293 // must be registered to allow arbitrary client code executing commands to
294 // be able to do nested calls (we can't only read from it)
Steven Moreland19fc9f72021-06-10 03:57:30 +0000295 sp<RpcConnection> connection = assignIncomingConnectionToThisThread(std::move(fd));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000296
Steven Morelanddd67b942021-07-23 17:15:41 -0700297 status_t status;
298
299 if (connection == nullptr) {
300 status = DEAD_OBJECT;
301 } else {
302 status = mState->readConnectionInit(connection, sp<RpcSession>::fromExisting(this));
303 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000304
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000305 return PreJoinSetupResult{
306 .connection = std::move(connection),
307 .status = status,
308 };
309}
310
Yifan Hong194acf22021-06-29 18:44:56 -0700311namespace {
312// RAII object for attaching / detaching current thread to JVM if Android Runtime exists. If
313// Android Runtime doesn't exist, no-op.
314class JavaThreadAttacher {
315public:
316 JavaThreadAttacher() {
317 // Use dlsym to find androidJavaAttachThread because libandroid_runtime is loaded after
318 // libbinder.
319 auto vm = getJavaVM();
320 if (vm == nullptr) return;
321
322 char threadName[16];
323 if (0 != pthread_getname_np(pthread_self(), threadName, sizeof(threadName))) {
324 constexpr const char* defaultThreadName = "UnknownRpcSessionThread";
325 memcpy(threadName, defaultThreadName,
326 std::min<size_t>(sizeof(threadName), strlen(defaultThreadName) + 1));
327 }
328 LOG_RPC_DETAIL("Attaching current thread %s to JVM", threadName);
329 JavaVMAttachArgs args;
330 args.version = JNI_VERSION_1_2;
331 args.name = threadName;
332 args.group = nullptr;
333 JNIEnv* env;
334
335 LOG_ALWAYS_FATAL_IF(vm->AttachCurrentThread(&env, &args) != JNI_OK,
336 "Cannot attach thread %s to JVM", threadName);
337 mAttached = true;
338 }
339 ~JavaThreadAttacher() {
340 if (!mAttached) return;
341 auto vm = getJavaVM();
342 LOG_ALWAYS_FATAL_IF(vm == nullptr,
343 "Unable to detach thread. No JavaVM, but it was present before!");
344
345 LOG_RPC_DETAIL("Detaching current thread from JVM");
346 if (vm->DetachCurrentThread() != JNI_OK) {
347 mAttached = false;
348 } else {
349 ALOGW("Unable to detach current thread from JVM");
350 }
351 }
352
353private:
354 DISALLOW_COPY_AND_ASSIGN(JavaThreadAttacher);
355 bool mAttached = false;
356
357 static JavaVM* getJavaVM() {
358 static auto fn = reinterpret_cast<decltype(&AndroidRuntimeGetJavaVM)>(
359 dlsym(RTLD_DEFAULT, "AndroidRuntimeGetJavaVM"));
360 if (fn == nullptr) return nullptr;
361 return fn();
362 }
363};
364} // namespace
365
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000366void RpcSession::join(sp<RpcSession>&& session, PreJoinSetupResult&& setupResult) {
367 sp<RpcConnection>& connection = setupResult.connection;
368
369 if (setupResult.status == OK) {
Steven Morelanddd67b942021-07-23 17:15:41 -0700370 LOG_ALWAYS_FATAL_IF(!connection, "must have connection if setup succeeded");
Yifan Hong194acf22021-06-29 18:44:56 -0700371 JavaThreadAttacher javaThreadAttacher;
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000372 while (true) {
Steven Moreland5ae62562021-06-10 03:21:42 +0000373 status_t status = session->state()->getAndExecuteCommand(connection, session,
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000374 RpcState::CommandType::ANY);
375 if (status != OK) {
376 LOG_RPC_DETAIL("Binder connection thread closing w/ status %s",
377 statusToString(status).c_str());
378 break;
379 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000380 }
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000381 } else {
382 ALOGE("Connection failed to init, closing with status %s",
383 statusToString(setupResult.status).c_str());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000384 }
385
Steven Moreland659416d2021-05-11 00:47:50 +0000386 sp<RpcSession::EventListener> listener;
Steven Morelanda63ff932021-05-12 00:03:15 +0000387 {
Steven Moreland659416d2021-05-11 00:47:50 +0000388 std::lock_guard<std::mutex> _l(session->mMutex);
389 auto it = session->mThreads.find(std::this_thread::get_id());
390 LOG_ALWAYS_FATAL_IF(it == session->mThreads.end());
Steven Morelanda63ff932021-05-12 00:03:15 +0000391 it->second.detach();
Steven Moreland659416d2021-05-11 00:47:50 +0000392 session->mThreads.erase(it);
Steven Morelandee3f4662021-05-22 01:07:33 +0000393
Steven Moreland659416d2021-05-11 00:47:50 +0000394 listener = session->mEventListener.promote();
Steven Morelandee3f4662021-05-22 01:07:33 +0000395 }
396
Steven Morelanddd67b942021-07-23 17:15:41 -0700397 // done after all cleanup, since session shutdown progresses via callbacks here
398 if (connection != nullptr) {
399 LOG_ALWAYS_FATAL_IF(!session->removeIncomingConnection(connection),
400 "bad state: connection object guaranteed to be in list");
401 }
402
Steven Moreland659416d2021-05-11 00:47:50 +0000403 session = nullptr;
404
405 if (listener != nullptr) {
Steven Moreland19fc9f72021-06-10 03:57:30 +0000406 listener->onSessionIncomingThreadEnded();
Steven Morelandee78e762021-05-05 21:12:51 +0000407 }
408}
409
Steven Moreland7b8bc4c2021-06-10 22:50:27 +0000410sp<RpcServer> RpcSession::server() {
411 RpcServer* unsafeServer = mForServer.unsafe_get();
412 sp<RpcServer> server = mForServer.promote();
413
414 LOG_ALWAYS_FATAL_IF((unsafeServer == nullptr) != (server == nullptr),
415 "wp<> is to avoid strong cycle only");
416 return server;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000417}
418
419bool RpcSession::setupSocketClient(const RpcSocketAddress& addr) {
420 {
421 std::lock_guard<std::mutex> _l(mMutex);
Steven Moreland19fc9f72021-06-10 03:57:30 +0000422 LOG_ALWAYS_FATAL_IF(mOutgoingConnections.size() != 0,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000423 "Must only setup session once, but already has %zu clients",
Steven Moreland19fc9f72021-06-10 03:57:30 +0000424 mOutgoingConnections.size());
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000425 }
426
Steven Moreland1b304292021-07-15 22:59:34 +0000427 if (!setupOneSocketConnection(addr, RpcAddress::zero(), false /*incoming*/)) return false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000428
Steven Morelanda5036f02021-06-08 02:26:57 +0000429 // TODO(b/189955605): we should add additional sessions dynamically
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000430 // instead of all at once.
431 // TODO(b/186470974): first risk of blocking
432 size_t numThreadsAvailable;
Steven Moreland1be91352021-05-11 22:12:15 +0000433 if (status_t status = getRemoteMaxThreads(&numThreadsAvailable); status != OK) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000434 ALOGE("Could not get max threads after initial session to %s: %s", addr.toString().c_str(),
435 statusToString(status).c_str());
436 return false;
437 }
438
439 if (status_t status = readId(); status != OK) {
440 ALOGE("Could not get session id after initial session to %s; %s", addr.toString().c_str(),
441 statusToString(status).c_str());
442 return false;
443 }
444
445 // we've already setup one client
446 for (size_t i = 0; i + 1 < numThreadsAvailable; i++) {
Steven Morelanda5036f02021-06-08 02:26:57 +0000447 // TODO(b/189955605): shutdown existing connections?
Steven Moreland1b304292021-07-15 22:59:34 +0000448 if (!setupOneSocketConnection(addr, mId.value(), false /*incoming*/)) return false;
Steven Moreland659416d2021-05-11 00:47:50 +0000449 }
450
Steven Morelanda5036f02021-06-08 02:26:57 +0000451 // TODO(b/189955605): we should add additional sessions dynamically
Steven Moreland659416d2021-05-11 00:47:50 +0000452 // instead of all at once - the other side should be responsible for setting
453 // up additional connections. We need to create at least one (unless 0 are
454 // requested to be set) in order to allow the other side to reliably make
455 // any requests at all.
456
Steven Moreland103424e2021-06-02 18:16:19 +0000457 for (size_t i = 0; i < mMaxThreads; i++) {
Steven Moreland1b304292021-07-15 22:59:34 +0000458 if (!setupOneSocketConnection(addr, mId.value(), true /*incoming*/)) return false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000459 }
460
461 return true;
462}
463
Steven Moreland01a6bad2021-06-11 00:59:20 +0000464bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, const RpcAddress& id,
Steven Moreland1b304292021-07-15 22:59:34 +0000465 bool incoming) {
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000466 for (size_t tries = 0; tries < 5; tries++) {
467 if (tries > 0) usleep(10000);
468
469 unique_fd serverFd(
470 TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0)));
471 if (serverFd == -1) {
472 int savedErrno = errno;
473 ALOGE("Could not create socket at %s: %s", addr.toString().c_str(),
474 strerror(savedErrno));
475 return false;
476 }
477
478 if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) {
479 if (errno == ECONNRESET) {
480 ALOGW("Connection reset on %s", addr.toString().c_str());
481 continue;
482 }
483 int savedErrno = errno;
484 ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(),
485 strerror(savedErrno));
486 return false;
487 }
488
Steven Moreland01a6bad2021-06-11 00:59:20 +0000489 RpcConnectionHeader header{.options = 0};
490 memcpy(&header.sessionId, &id.viewRawEmbedded(), sizeof(RpcWireAddress));
491
Steven Moreland1b304292021-07-15 22:59:34 +0000492 if (incoming) header.options |= RPC_CONNECTION_OPTION_INCOMING;
Steven Moreland659416d2021-05-11 00:47:50 +0000493
494 if (sizeof(header) != TEMP_FAILURE_RETRY(write(serverFd.get(), &header, sizeof(header)))) {
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000495 int savedErrno = errno;
Steven Moreland659416d2021-05-11 00:47:50 +0000496 ALOGE("Could not write connection header to socket at %s: %s", addr.toString().c_str(),
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000497 strerror(savedErrno));
498 return false;
499 }
500
501 LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get());
502
Steven Moreland1b304292021-07-15 22:59:34 +0000503 if (incoming) {
Steven Morelandfba6f772021-07-15 22:45:09 +0000504 return addIncomingConnection(std::move(serverFd));
Steven Moreland659416d2021-05-11 00:47:50 +0000505 } else {
Steven Morelandb86e26b2021-06-12 00:35:58 +0000506 return addOutgoingConnection(std::move(serverFd), true);
Steven Moreland659416d2021-05-11 00:47:50 +0000507 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000508 }
509
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000510 ALOGE("Ran out of retries to connect to %s", addr.toString().c_str());
511 return false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000512}
513
Steven Morelandfba6f772021-07-15 22:45:09 +0000514bool RpcSession::addIncomingConnection(unique_fd fd) {
515 std::mutex mutex;
516 std::condition_variable joinCv;
517 std::unique_lock<std::mutex> lock(mutex);
518 std::thread thread;
519 sp<RpcSession> thiz = sp<RpcSession>::fromExisting(this);
520 bool ownershipTransferred = false;
521 thread = std::thread([&]() {
522 std::unique_lock<std::mutex> threadLock(mutex);
523 unique_fd movedFd = std::move(fd);
524 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
525 sp<RpcSession> session = thiz;
526 session->preJoinThreadOwnership(std::move(thread));
527
528 // only continue once we have a response or the connection fails
529 auto setupResult = session->preJoinSetup(std::move(movedFd));
530
531 ownershipTransferred = true;
532 threadLock.unlock();
533 joinCv.notify_one();
534 // do not use & vars below
535
536 RpcSession::join(std::move(session), std::move(setupResult));
537 });
538 joinCv.wait(lock, [&] { return ownershipTransferred; });
539 LOG_ALWAYS_FATAL_IF(!ownershipTransferred);
540 return true;
541}
542
Steven Morelandb86e26b2021-06-12 00:35:58 +0000543bool RpcSession::addOutgoingConnection(unique_fd fd, bool init) {
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000544 sp<RpcConnection> connection = sp<RpcConnection>::make();
545 {
546 std::lock_guard<std::mutex> _l(mMutex);
Steven Morelandee3f4662021-05-22 01:07:33 +0000547
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000548 // first client connection added, but setForServer not called, so
549 // initializaing for a client.
550 if (mShutdownTrigger == nullptr) {
551 mShutdownTrigger = FdTrigger::make();
552 mEventListener = mShutdownListener = sp<WaitForShutdownListener>::make();
553 if (mShutdownTrigger == nullptr) return false;
554 }
555
556 connection->fd = std::move(fd);
557 connection->exclusiveTid = gettid();
Steven Moreland19fc9f72021-06-10 03:57:30 +0000558 mOutgoingConnections.push_back(connection);
Steven Morelandee3f4662021-05-22 01:07:33 +0000559 }
560
Steven Morelandb86e26b2021-06-12 00:35:58 +0000561 status_t status = OK;
562 if (init) {
563 mState->sendConnectionInit(connection, sp<RpcSession>::fromExisting(this));
564 }
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000565
566 {
567 std::lock_guard<std::mutex> _l(mMutex);
568 connection->exclusiveTid = std::nullopt;
569 }
570
571 return status == OK;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000572}
573
Steven Morelanda8b44292021-06-08 01:27:53 +0000574bool RpcSession::setForServer(const wp<RpcServer>& server, const wp<EventListener>& eventListener,
Steven Moreland01a6bad2021-06-11 00:59:20 +0000575 const RpcAddress& sessionId) {
Steven Moreland659416d2021-05-11 00:47:50 +0000576 LOG_ALWAYS_FATAL_IF(mForServer != nullptr);
577 LOG_ALWAYS_FATAL_IF(server == nullptr);
578 LOG_ALWAYS_FATAL_IF(mEventListener != nullptr);
579 LOG_ALWAYS_FATAL_IF(eventListener == nullptr);
Steven Morelandee3f4662021-05-22 01:07:33 +0000580 LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr);
Steven Morelanda8b44292021-06-08 01:27:53 +0000581
582 mShutdownTrigger = FdTrigger::make();
583 if (mShutdownTrigger == nullptr) return false;
Steven Morelandee3f4662021-05-22 01:07:33 +0000584
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000585 mId = sessionId;
586 mForServer = server;
Steven Moreland659416d2021-05-11 00:47:50 +0000587 mEventListener = eventListener;
Steven Morelanda8b44292021-06-08 01:27:53 +0000588 return true;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000589}
590
Steven Moreland19fc9f72021-06-10 03:57:30 +0000591sp<RpcSession::RpcConnection> RpcSession::assignIncomingConnectionToThisThread(unique_fd fd) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000592 std::lock_guard<std::mutex> _l(mMutex);
Steven Morelanddd67b942021-07-23 17:15:41 -0700593
594 // Don't accept any more connections, some have shutdown. Usually this
595 // happens when new connections are still being established as part of a
596 // very short-lived session which shuts down after it already started
597 // accepting new connections.
598 if (mIncomingConnections.size() < mMaxIncomingConnections) {
599 return nullptr;
600 }
601
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000602 sp<RpcConnection> session = sp<RpcConnection>::make();
603 session->fd = std::move(fd);
604 session->exclusiveTid = gettid();
Steven Morelanddd67b942021-07-23 17:15:41 -0700605
Steven Moreland19fc9f72021-06-10 03:57:30 +0000606 mIncomingConnections.push_back(session);
Steven Morelanddd67b942021-07-23 17:15:41 -0700607 mMaxIncomingConnections = mIncomingConnections.size();
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000608
609 return session;
610}
611
Steven Moreland19fc9f72021-06-10 03:57:30 +0000612bool RpcSession::removeIncomingConnection(const sp<RpcConnection>& connection) {
Steven Morelanddd67b942021-07-23 17:15:41 -0700613 std::unique_lock<std::mutex> _l(mMutex);
Steven Moreland19fc9f72021-06-10 03:57:30 +0000614 if (auto it = std::find(mIncomingConnections.begin(), mIncomingConnections.end(), connection);
615 it != mIncomingConnections.end()) {
616 mIncomingConnections.erase(it);
617 if (mIncomingConnections.size() == 0) {
Steven Moreland659416d2021-05-11 00:47:50 +0000618 sp<EventListener> listener = mEventListener.promote();
619 if (listener) {
Steven Morelanddd67b942021-07-23 17:15:41 -0700620 _l.unlock();
621 listener->onSessionAllIncomingThreadsEnded(sp<RpcSession>::fromExisting(this));
Steven Morelanda86e8fe2021-05-26 22:52:35 +0000622 }
Steven Morelandee78e762021-05-05 21:12:51 +0000623 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000624 return true;
625 }
626 return false;
627}
628
Steven Moreland195edb82021-06-08 02:44:39 +0000629status_t RpcSession::ExclusiveConnection::find(const sp<RpcSession>& session, ConnectionUse use,
630 ExclusiveConnection* connection) {
631 connection->mSession = session;
632 connection->mConnection = nullptr;
633 connection->mReentrant = false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000634
Steven Moreland195edb82021-06-08 02:44:39 +0000635 pid_t tid = gettid();
636 std::unique_lock<std::mutex> _l(session->mMutex);
637
638 session->mWaitingThreads++;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000639 while (true) {
640 sp<RpcConnection> exclusive;
641 sp<RpcConnection> available;
642
643 // CHECK FOR DEDICATED CLIENT SOCKET
644 //
Steven Moreland85e067b2021-05-26 17:43:53 +0000645 // A server/looper should always use a dedicated connection if available
Steven Moreland19fc9f72021-06-10 03:57:30 +0000646 findConnection(tid, &exclusive, &available, session->mOutgoingConnections,
647 session->mOutgoingConnectionsOffset);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000648
649 // WARNING: this assumes a server cannot request its client to send
Steven Moreland19fc9f72021-06-10 03:57:30 +0000650 // a transaction, as mIncomingConnections is excluded below.
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000651 //
652 // Imagine we have more than one thread in play, and a single thread
653 // sends a synchronous, then an asynchronous command. Imagine the
654 // asynchronous command is sent on the first client connection. Then, if
655 // we naively send a synchronous command to that same connection, the
656 // thread on the far side might be busy processing the asynchronous
657 // command. So, we move to considering the second available thread
658 // for subsequent calls.
659 if (use == ConnectionUse::CLIENT_ASYNC && (exclusive != nullptr || available != nullptr)) {
Steven Moreland19fc9f72021-06-10 03:57:30 +0000660 session->mOutgoingConnectionsOffset = (session->mOutgoingConnectionsOffset + 1) %
661 session->mOutgoingConnections.size();
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000662 }
663
Steven Morelandc7d40132021-06-10 03:42:11 +0000664 // USE SERVING SOCKET (e.g. nested transaction)
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000665 if (use != ConnectionUse::CLIENT_ASYNC) {
Steven Moreland19fc9f72021-06-10 03:57:30 +0000666 sp<RpcConnection> exclusiveIncoming;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000667 // server connections are always assigned to a thread
Steven Moreland19fc9f72021-06-10 03:57:30 +0000668 findConnection(tid, &exclusiveIncoming, nullptr /*available*/,
669 session->mIncomingConnections, 0 /* index hint */);
Steven Morelandc7d40132021-06-10 03:42:11 +0000670
671 // asynchronous calls cannot be nested, we currently allow ref count
672 // calls to be nested (so that you can use this without having extra
673 // threads). Note 'drainCommands' is used so that these ref counts can't
674 // build up.
Steven Moreland19fc9f72021-06-10 03:57:30 +0000675 if (exclusiveIncoming != nullptr) {
676 if (exclusiveIncoming->allowNested) {
Steven Morelandc7d40132021-06-10 03:42:11 +0000677 // guaranteed to be processed as nested command
Steven Moreland19fc9f72021-06-10 03:57:30 +0000678 exclusive = exclusiveIncoming;
Steven Morelandc7d40132021-06-10 03:42:11 +0000679 } else if (use == ConnectionUse::CLIENT_REFCOUNT && available == nullptr) {
680 // prefer available socket, but if we don't have one, don't
681 // wait for one
Steven Moreland19fc9f72021-06-10 03:57:30 +0000682 exclusive = exclusiveIncoming;
Steven Morelandc7d40132021-06-10 03:42:11 +0000683 }
684 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000685 }
686
Steven Moreland85e067b2021-05-26 17:43:53 +0000687 // if our thread is already using a connection, prioritize using that
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000688 if (exclusive != nullptr) {
Steven Moreland195edb82021-06-08 02:44:39 +0000689 connection->mConnection = exclusive;
690 connection->mReentrant = true;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000691 break;
692 } else if (available != nullptr) {
Steven Moreland195edb82021-06-08 02:44:39 +0000693 connection->mConnection = available;
694 connection->mConnection->exclusiveTid = tid;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000695 break;
696 }
697
Steven Moreland19fc9f72021-06-10 03:57:30 +0000698 if (session->mOutgoingConnections.size() == 0) {
Steven Moreland195edb82021-06-08 02:44:39 +0000699 ALOGE("Session has no client connections. This is required for an RPC server to make "
700 "any non-nested (e.g. oneway or on another thread) calls. Use: %d. Server "
701 "connections: %zu",
Steven Moreland19fc9f72021-06-10 03:57:30 +0000702 static_cast<int>(use), session->mIncomingConnections.size());
Steven Moreland195edb82021-06-08 02:44:39 +0000703 return WOULD_BLOCK;
704 }
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000705
Steven Moreland85e067b2021-05-26 17:43:53 +0000706 LOG_RPC_DETAIL("No available connections (have %zu clients and %zu servers). Waiting...",
Steven Moreland19fc9f72021-06-10 03:57:30 +0000707 session->mOutgoingConnections.size(), session->mIncomingConnections.size());
Steven Moreland195edb82021-06-08 02:44:39 +0000708 session->mAvailableConnectionCv.wait(_l);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000709 }
Steven Moreland195edb82021-06-08 02:44:39 +0000710 session->mWaitingThreads--;
711
712 return OK;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000713}
714
715void RpcSession::ExclusiveConnection::findConnection(pid_t tid, sp<RpcConnection>* exclusive,
716 sp<RpcConnection>* available,
717 std::vector<sp<RpcConnection>>& sockets,
718 size_t socketsIndexHint) {
719 LOG_ALWAYS_FATAL_IF(sockets.size() > 0 && socketsIndexHint >= sockets.size(),
720 "Bad index %zu >= %zu", socketsIndexHint, sockets.size());
721
722 if (*exclusive != nullptr) return; // consistent with break below
723
724 for (size_t i = 0; i < sockets.size(); i++) {
725 sp<RpcConnection>& socket = sockets[(i + socketsIndexHint) % sockets.size()];
726
Steven Moreland85e067b2021-05-26 17:43:53 +0000727 // take first available connection (intuition = caching)
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000728 if (available && *available == nullptr && socket->exclusiveTid == std::nullopt) {
729 *available = socket;
730 continue;
731 }
732
Steven Moreland85e067b2021-05-26 17:43:53 +0000733 // though, prefer to take connection which is already inuse by this thread
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000734 // (nested transactions)
735 if (exclusive && socket->exclusiveTid == tid) {
736 *exclusive = socket;
737 break; // consistent with return above
738 }
739 }
740}
741
742RpcSession::ExclusiveConnection::~ExclusiveConnection() {
Steven Moreland85e067b2021-05-26 17:43:53 +0000743 // reentrant use of a connection means something less deep in the call stack
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000744 // is using this fd, and it retains the right to it. So, we don't give up
745 // exclusive ownership, and no thread is freed.
Steven Moreland195edb82021-06-08 02:44:39 +0000746 if (!mReentrant && mConnection != nullptr) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000747 std::unique_lock<std::mutex> _l(mSession->mMutex);
748 mConnection->exclusiveTid = std::nullopt;
749 if (mSession->mWaitingThreads > 0) {
750 _l.unlock();
751 mSession->mAvailableConnectionCv.notify_one();
752 }
753 }
754}
755
756} // namespace android