1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "RpcSession"
18
19 #include <binder/RpcSession.h>
20
21 #include <dlfcn.h>
22 #include <inttypes.h>
23 #include <netinet/tcp.h>
24 #include <poll.h>
25 #include <unistd.h>
26
27 #include <string_view>
28
29 #include <binder/BpBinder.h>
30 #include <binder/Functional.h>
31 #include <binder/Parcel.h>
32 #include <binder/RpcServer.h>
33 #include <binder/RpcTransportRaw.h>
34 #include <binder/Stability.h>
35 #include <utils/String8.h>
36
37 #include "BuildFlags.h"
38 #include "FdTrigger.h"
39 #include "OS.h"
40 #include "RpcSocketAddress.h"
41 #include "RpcState.h"
42 #include "RpcTransportUtils.h"
43 #include "RpcWireFormat.h"
44 #include "Utils.h"
45
46 #if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__)
47 #include <jni.h>
48 extern "C" JavaVM* AndroidRuntimeGetJavaVM();
49 #endif
50
51 namespace android {
52
53 using namespace android::binder::impl;
54 using android::binder::borrowed_fd;
55 using android::binder::unique_fd;
56
RpcSession(std::unique_ptr<RpcTransportCtx> ctx)57 RpcSession::RpcSession(std::unique_ptr<RpcTransportCtx> ctx) : mCtx(std::move(ctx)) {
58 LOG_RPC_DETAIL("RpcSession created %p", this);
59
60 mRpcBinderState = std::make_unique<RpcState>();
61 }
~RpcSession()62 RpcSession::~RpcSession() {
63 LOG_RPC_DETAIL("RpcSession destroyed %p", this);
64
65 RpcMutexLockGuard _l(mMutex);
66 LOG_ALWAYS_FATAL_IF(mConnections.mIncoming.size() != 0,
67 "Should not be able to destroy a session with servers in use.");
68 }
69
make()70 sp<RpcSession> RpcSession::make() {
71 // Default is without TLS.
72 return make(binder::os::makeDefaultRpcTransportCtxFactory());
73 }
74
make(std::unique_ptr<RpcTransportCtxFactory> rpcTransportCtxFactory)75 sp<RpcSession> RpcSession::make(std::unique_ptr<RpcTransportCtxFactory> rpcTransportCtxFactory) {
76 auto ctx = rpcTransportCtxFactory->newClientCtx();
77 if (ctx == nullptr) return nullptr;
78 return sp<RpcSession>::make(std::move(ctx));
79 }
80
setMaxIncomingThreads(size_t threads)81 void RpcSession::setMaxIncomingThreads(size_t threads) {
82 RpcMutexLockGuard _l(mMutex);
83 LOG_ALWAYS_FATAL_IF(mStartedSetup,
84 "Must set max incoming threads before setting up connections");
85 mMaxIncomingThreads = threads;
86 }
87
getMaxIncomingThreads()88 size_t RpcSession::getMaxIncomingThreads() {
89 RpcMutexLockGuard _l(mMutex);
90 return mMaxIncomingThreads;
91 }
92
setMaxOutgoingConnections(size_t connections)93 void RpcSession::setMaxOutgoingConnections(size_t connections) {
94 RpcMutexLockGuard _l(mMutex);
95 LOG_ALWAYS_FATAL_IF(mStartedSetup,
96 "Must set max outgoing threads before setting up connections");
97 mMaxOutgoingConnections = connections;
98 }
99
getMaxOutgoingThreads()100 size_t RpcSession::getMaxOutgoingThreads() {
101 RpcMutexLockGuard _l(mMutex);
102 return mMaxOutgoingConnections;
103 }
104
setProtocolVersionInternal(uint32_t version,bool checkStarted)105 bool RpcSession::setProtocolVersionInternal(uint32_t version, bool checkStarted) {
106 if (!RpcState::validateProtocolVersion(version)) {
107 return false;
108 }
109
110 RpcMutexLockGuard _l(mMutex);
111 LOG_ALWAYS_FATAL_IF(checkStarted && mStartedSetup,
112 "Must set protocol version before setting up connections");
113 if (mProtocolVersion && version > *mProtocolVersion) {
114 ALOGE("Cannot upgrade explicitly capped protocol version %u to newer version %u",
115 *mProtocolVersion, version);
116 return false;
117 }
118
119 mProtocolVersion = version;
120 return true;
121 }
122
setProtocolVersion(uint32_t version)123 bool RpcSession::setProtocolVersion(uint32_t version) {
124 return setProtocolVersionInternal(version, true);
125 }
126
getProtocolVersion()127 std::optional<uint32_t> RpcSession::getProtocolVersion() {
128 RpcMutexLockGuard _l(mMutex);
129 return mProtocolVersion;
130 }
131
setFileDescriptorTransportMode(FileDescriptorTransportMode mode)132 void RpcSession::setFileDescriptorTransportMode(FileDescriptorTransportMode mode) {
133 RpcMutexLockGuard _l(mMutex);
134 LOG_ALWAYS_FATAL_IF(mStartedSetup,
135 "Must set file descriptor transport mode before setting up connections");
136 mFileDescriptorTransportMode = mode;
137 }
138
getFileDescriptorTransportMode()139 RpcSession::FileDescriptorTransportMode RpcSession::getFileDescriptorTransportMode() {
140 return mFileDescriptorTransportMode;
141 }
142
setupUnixDomainClient(const char * path)143 status_t RpcSession::setupUnixDomainClient(const char* path) {
144 return setupSocketClient(UnixSocketAddress(path));
145 }
146
setupUnixDomainSocketBootstrapClient(unique_fd bootstrapFd)147 status_t RpcSession::setupUnixDomainSocketBootstrapClient(unique_fd bootstrapFd) {
148 mBootstrapTransport =
149 mCtx->newTransport(RpcTransportFd(std::move(bootstrapFd)), mShutdownTrigger.get());
150 return setupClient([&](const std::vector<uint8_t>& sessionId, bool incoming) {
151 int socks[2];
152 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, socks) < 0) {
153 int savedErrno = errno;
154 ALOGE("Failed socketpair: %s", strerror(savedErrno));
155 return -savedErrno;
156 }
157 unique_fd clientFd(socks[0]), serverFd(socks[1]);
158
159 int zero = 0;
160 iovec iov{&zero, sizeof(zero)};
161 std::vector<std::variant<unique_fd, borrowed_fd>> fds;
162 fds.push_back(std::move(serverFd));
163
164 status_t status = mBootstrapTransport->interruptableWriteFully(mShutdownTrigger.get(), &iov,
165 1, std::nullopt, &fds);
166 if (status != OK) {
167 ALOGE("Failed to send fd over bootstrap transport: %s", strerror(-status));
168 return status;
169 }
170
171 return initAndAddConnection(RpcTransportFd(std::move(clientFd)), sessionId, incoming);
172 });
173 }
174
setupVsockClient(unsigned int cid,unsigned int port)175 status_t RpcSession::setupVsockClient(unsigned int cid, unsigned int port) {
176 return setupSocketClient(VsockSocketAddress(cid, port));
177 }
178
setupInetClient(const char * addr,unsigned int port)179 status_t RpcSession::setupInetClient(const char* addr, unsigned int port) {
180 auto aiStart = InetSocketAddress::getAddrInfo(addr, port);
181 if (aiStart == nullptr) return UNKNOWN_ERROR;
182 for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) {
183 InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, addr, port);
184 if (status_t status = setupSocketClient(socketAddress); status == OK) return OK;
185 }
186 ALOGE("None of the socket address resolved for %s:%u can be added as inet client.", addr, port);
187 return NAME_NOT_FOUND;
188 }
189
setupPreconnectedClient(unique_fd fd,std::function<unique_fd ()> && request)190 status_t RpcSession::setupPreconnectedClient(unique_fd fd, std::function<unique_fd()>&& request) {
191 return setupClient([&](const std::vector<uint8_t>& sessionId, bool incoming) -> status_t {
192 if (!fd.ok()) {
193 fd = request();
194 if (!fd.ok()) return BAD_VALUE;
195 }
196 if (status_t res = binder::os::setNonBlocking(fd); res != OK) return res;
197
198 RpcTransportFd transportFd(std::move(fd));
199 status_t status = initAndAddConnection(std::move(transportFd), sessionId, incoming);
200 fd = unique_fd(); // Explicitly reset after move to avoid analyzer warning.
201 return status;
202 });
203 }
204
addNullDebuggingClient()205 status_t RpcSession::addNullDebuggingClient() {
206 // Note: only works on raw sockets.
207 if (auto status = initShutdownTrigger(); status != OK) return status;
208
209 unique_fd serverFd(TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY | O_CLOEXEC)));
210
211 if (!serverFd.ok()) {
212 int savedErrno = errno;
213 ALOGE("Could not connect to /dev/null: %s", strerror(savedErrno));
214 return -savedErrno;
215 }
216
217 RpcTransportFd transportFd(std::move(serverFd));
218 auto server = mCtx->newTransport(std::move(transportFd), mShutdownTrigger.get());
219 if (server == nullptr) {
220 ALOGE("Unable to set up RpcTransport");
221 return UNKNOWN_ERROR;
222 }
223 return addOutgoingConnection(std::move(server), false);
224 }
225
getRootObject()226 sp<IBinder> RpcSession::getRootObject() {
227 ExclusiveConnection connection;
228 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
229 ConnectionUse::CLIENT, &connection);
230 if (status != OK) return nullptr;
231 return state()->getRootObject(connection.get(), sp<RpcSession>::fromExisting(this));
232 }
233
getRemoteMaxThreads(size_t * maxThreads)234 status_t RpcSession::getRemoteMaxThreads(size_t* maxThreads) {
235 ExclusiveConnection connection;
236 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
237 ConnectionUse::CLIENT, &connection);
238 if (status != OK) return status;
239 return state()->getMaxThreads(connection.get(), sp<RpcSession>::fromExisting(this), maxThreads);
240 }
241
shutdownAndWait(bool wait)242 bool RpcSession::shutdownAndWait(bool wait) {
243 RpcMutexUniqueLock _l(mMutex);
244 LOG_ALWAYS_FATAL_IF(mShutdownTrigger == nullptr, "Shutdown trigger not installed");
245
246 mShutdownTrigger->trigger();
247
248 if (wait) {
249 LOG_ALWAYS_FATAL_IF(mShutdownListener == nullptr, "Shutdown listener not installed");
250 mShutdownListener->waitForShutdown(_l, sp<RpcSession>::fromExisting(this));
251
252 LOG_ALWAYS_FATAL_IF(!mConnections.mThreads.empty(), "Shutdown failed");
253 }
254
255 _l.unlock();
256
257 if (status_t res = state()->sendObituaries(sp<RpcSession>::fromExisting(this)); res != OK) {
258 ALOGE("Failed to send obituaries as the RpcSession is shutting down: %s",
259 statusToString(res).c_str());
260 }
261
262 mRpcBinderState->clear();
263
264 return true;
265 }
266
transact(const sp<IBinder> & binder,uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)267 status_t RpcSession::transact(const sp<IBinder>& binder, uint32_t code, const Parcel& data,
268 Parcel* reply, uint32_t flags) {
269 ExclusiveConnection connection;
270 status_t status =
271 ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
272 (flags & IBinder::FLAG_ONEWAY) ? ConnectionUse::CLIENT_ASYNC
273 : ConnectionUse::CLIENT,
274 &connection);
275 if (status != OK) return status;
276 return state()->transact(connection.get(), binder, code, data,
277 sp<RpcSession>::fromExisting(this), reply, flags);
278 }
279
sendDecStrong(const BpBinder * binder)280 status_t RpcSession::sendDecStrong(const BpBinder* binder) {
281 // target is 0 because this is used to free BpBinder objects
282 return sendDecStrongToTarget(binder->getPrivateAccessor().rpcAddress(), 0 /*target*/);
283 }
284
sendDecStrongToTarget(uint64_t address,size_t target)285 status_t RpcSession::sendDecStrongToTarget(uint64_t address, size_t target) {
286 ExclusiveConnection connection;
287 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
288 ConnectionUse::CLIENT_REFCOUNT, &connection);
289 if (status != OK) return status;
290 return state()->sendDecStrongToTarget(connection.get(), sp<RpcSession>::fromExisting(this),
291 address, target);
292 }
293
readId()294 status_t RpcSession::readId() {
295 {
296 RpcMutexLockGuard _l(mMutex);
297 LOG_ALWAYS_FATAL_IF(mForServer != nullptr, "Can only update ID for client.");
298 }
299
300 ExclusiveConnection connection;
301 status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
302 ConnectionUse::CLIENT, &connection);
303 if (status != OK) return status;
304
305 status = state()->getSessionId(connection.get(), sp<RpcSession>::fromExisting(this), &mId);
306 if (status != OK) return status;
307
308 LOG_RPC_DETAIL("RpcSession %p has id %s", this, HexString(mId.data(), mId.size()).c_str());
309 return OK;
310 }
311
onSessionAllIncomingThreadsEnded(const sp<RpcSession> & session)312 void RpcSession::WaitForShutdownListener::onSessionAllIncomingThreadsEnded(
313 const sp<RpcSession>& session) {
314 (void)session;
315 }
316
onSessionIncomingThreadEnded()317 void RpcSession::WaitForShutdownListener::onSessionIncomingThreadEnded() {
318 mShutdownCount += 1;
319 mCv.notify_all();
320 }
321
waitForShutdown(RpcMutexUniqueLock & lock,const sp<RpcSession> & session)322 void RpcSession::WaitForShutdownListener::waitForShutdown(RpcMutexUniqueLock& lock,
323 const sp<RpcSession>& session) {
324 while (mShutdownCount < session->mConnections.mMaxIncoming) {
325 if (std::cv_status::timeout == mCv.wait_for(lock, std::chrono::seconds(1))) {
326 ALOGE("Waiting for RpcSession to shut down (1s w/o progress): %zu incoming connections "
327 "still %zu/%zu fully shutdown.",
328 session->mConnections.mIncoming.size(), mShutdownCount.load(),
329 session->mConnections.mMaxIncoming);
330 }
331 }
332 }
333
preJoinThreadOwnership(RpcMaybeThread thread)334 void RpcSession::preJoinThreadOwnership(RpcMaybeThread thread) {
335 LOG_ALWAYS_FATAL_IF(thread.get_id() != rpc_this_thread::get_id(), "Must own this thread");
336
337 {
338 RpcMutexLockGuard _l(mMutex);
339 mConnections.mThreads[thread.get_id()] = std::move(thread);
340 }
341 }
342
preJoinSetup(std::unique_ptr<RpcTransport> rpcTransport)343 RpcSession::PreJoinSetupResult RpcSession::preJoinSetup(
344 std::unique_ptr<RpcTransport> rpcTransport) {
345 // must be registered to allow arbitrary client code executing commands to
346 // be able to do nested calls (we can't only read from it)
347 sp<RpcConnection> connection = assignIncomingConnectionToThisThread(std::move(rpcTransport));
348
349 status_t status;
350
351 if (connection == nullptr) {
352 status = DEAD_OBJECT;
353 } else {
354 status =
355 mRpcBinderState->readConnectionInit(connection, sp<RpcSession>::fromExisting(this));
356 }
357
358 return PreJoinSetupResult{
359 .connection = std::move(connection),
360 .status = status,
361 };
362 }
363
364 namespace {
365 #if !defined(__ANDROID__) || defined(__ANDROID_RECOVERY__)
366 class JavaThreadAttacher {};
367 #else
368 // RAII object for attaching / detaching current thread to JVM if Android Runtime exists. If
369 // Android Runtime doesn't exist, no-op.
370 class JavaThreadAttacher {
371 public:
372 JavaThreadAttacher() {
373 // Use dlsym to find androidJavaAttachThread because libandroid_runtime is loaded after
374 // libbinder.
375 auto vm = getJavaVM();
376 if (vm == nullptr) return;
377
378 char threadName[16];
379 if (0 != pthread_getname_np(pthread_self(), threadName, sizeof(threadName))) {
380 constexpr const char* defaultThreadName = "UnknownRpcSessionThread";
381 memcpy(threadName, defaultThreadName,
382 std::min<size_t>(sizeof(threadName), strlen(defaultThreadName) + 1));
383 }
384 LOG_RPC_DETAIL("Attaching current thread %s to JVM", threadName);
385 JavaVMAttachArgs args;
386 args.version = JNI_VERSION_1_2;
387 args.name = threadName;
388 args.group = nullptr;
389 JNIEnv* env;
390
391 LOG_ALWAYS_FATAL_IF(vm->AttachCurrentThread(&env, &args) != JNI_OK,
392 "Cannot attach thread %s to JVM", threadName);
393 mAttached = true;
394 }
395 ~JavaThreadAttacher() {
396 if (!mAttached) return;
397 auto vm = getJavaVM();
398 LOG_ALWAYS_FATAL_IF(vm == nullptr,
399 "Unable to detach thread. No JavaVM, but it was present before!");
400
401 LOG_RPC_DETAIL("Detaching current thread from JVM");
402 int ret = vm->DetachCurrentThread();
403 if (ret == JNI_OK) {
404 mAttached = false;
405 } else {
406 ALOGW("Unable to detach current thread from JVM (%d)", ret);
407 }
408 }
409
410 private:
411 JavaThreadAttacher(const JavaThreadAttacher&) = delete;
412 void operator=(const JavaThreadAttacher&) = delete;
413
414 bool mAttached = false;
415
416 static JavaVM* getJavaVM() {
417 static auto fn = reinterpret_cast<decltype(&AndroidRuntimeGetJavaVM)>(
418 dlsym(RTLD_DEFAULT, "AndroidRuntimeGetJavaVM"));
419 if (fn == nullptr) return nullptr;
420 return fn();
421 }
422 };
423 #endif
424 } // namespace
425
join(sp<RpcSession> && session,PreJoinSetupResult && setupResult)426 void RpcSession::join(sp<RpcSession>&& session, PreJoinSetupResult&& setupResult) {
427 sp<RpcConnection>& connection = setupResult.connection;
428
429 if (setupResult.status == OK) {
430 LOG_ALWAYS_FATAL_IF(!connection, "must have connection if setup succeeded");
431 [[maybe_unused]] JavaThreadAttacher javaThreadAttacher;
432 while (true) {
433 status_t status = session->state()->getAndExecuteCommand(connection, session,
434 RpcState::CommandType::ANY);
435 if (status != OK) {
436 LOG_RPC_DETAIL("Binder connection thread closing w/ status %s",
437 statusToString(status).c_str());
438 break;
439 }
440 }
441 } else {
442 ALOGE("Connection failed to init, closing with status %s",
443 statusToString(setupResult.status).c_str());
444 }
445
446 sp<RpcSession::EventListener> listener;
447 {
448 RpcMutexLockGuard _l(session->mMutex);
449 auto it = session->mConnections.mThreads.find(rpc_this_thread::get_id());
450 LOG_ALWAYS_FATAL_IF(it == session->mConnections.mThreads.end());
451 it->second.detach();
452 session->mConnections.mThreads.erase(it);
453
454 listener = session->mEventListener.promote();
455 }
456
457 // done after all cleanup, since session shutdown progresses via callbacks here
458 if (connection != nullptr) {
459 LOG_ALWAYS_FATAL_IF(!session->removeIncomingConnection(connection),
460 "bad state: connection object guaranteed to be in list");
461 }
462
463 session = nullptr;
464
465 if (listener != nullptr) {
466 listener->onSessionIncomingThreadEnded();
467 }
468 }
469
server()470 sp<RpcServer> RpcSession::server() {
471 RpcServer* unsafeServer = mForServer.unsafe_get();
472 sp<RpcServer> server = mForServer.promote();
473
474 LOG_ALWAYS_FATAL_IF((unsafeServer == nullptr) != (server == nullptr),
475 "wp<> is to avoid strong cycle only");
476 return server;
477 }
478
setupClient(const std::function<status_t (const std::vector<uint8_t> & sessionId,bool incoming)> & connectAndInit)479 status_t RpcSession::setupClient(const std::function<status_t(const std::vector<uint8_t>& sessionId,
480 bool incoming)>& connectAndInit) {
481 {
482 RpcMutexLockGuard _l(mMutex);
483 LOG_ALWAYS_FATAL_IF(mStartedSetup, "Must only setup session once");
484 mStartedSetup = true;
485
486 if constexpr (!kEnableRpcThreads) {
487 LOG_ALWAYS_FATAL_IF(mMaxIncomingThreads > 0,
488 "Incoming threads are not supported on single-threaded libbinder");
489 // mMaxIncomingThreads should not change from here to its use below,
490 // since we set mStartedSetup==true and setMaxIncomingThreads checks
491 // for that
492 }
493 }
494
495 if (auto status = initShutdownTrigger(); status != OK) return status;
496
497 auto oldProtocolVersion = mProtocolVersion;
498 auto cleanup = make_scope_guard([&] {
499 // if any threads are started, shut them down
500 (void)shutdownAndWait(true);
501
502 mShutdownListener = nullptr;
503 mEventListener.clear();
504
505 mId.clear();
506
507 mShutdownTrigger = nullptr;
508 mRpcBinderState = std::make_unique<RpcState>();
509
510 // protocol version may have been downgraded - if we reuse this object
511 // to connect to another server, force that server to request a
512 // downgrade again
513 mProtocolVersion = oldProtocolVersion;
514
515 mConnections = {};
516
517 // clear mStartedSetup so that we can reuse this RpcSession
518 mStartedSetup = false;
519 });
520
521 if (status_t status = connectAndInit({}, false /*incoming*/); status != OK) return status;
522
523 {
524 ExclusiveConnection connection;
525 if (status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this),
526 ConnectionUse::CLIENT, &connection);
527 status != OK)
528 return status;
529
530 uint32_t version;
531 if (status_t status =
532 state()->readNewSessionResponse(connection.get(),
533 sp<RpcSession>::fromExisting(this), &version);
534 status != OK)
535 return status;
536 if (!setProtocolVersionInternal(version, false)) return BAD_VALUE;
537 }
538
539 // TODO(b/189955605): we should add additional sessions dynamically
540 // instead of all at once.
541 size_t numThreadsAvailable;
542 if (status_t status = getRemoteMaxThreads(&numThreadsAvailable); status != OK) {
543 ALOGE("Could not get max threads after initial session setup: %s",
544 statusToString(status).c_str());
545 return status;
546 }
547
548 if (status_t status = readId(); status != OK) {
549 ALOGE("Could not get session id after initial session setup: %s",
550 statusToString(status).c_str());
551 return status;
552 }
553
554 size_t outgoingConnections = std::min(numThreadsAvailable, mMaxOutgoingConnections);
555 ALOGI_IF(outgoingConnections != numThreadsAvailable,
556 "Server hints client to start %zu outgoing threads, but client will only start %zu "
557 "because it is preconfigured to start at most %zu outgoing threads.",
558 numThreadsAvailable, outgoingConnections, mMaxOutgoingConnections);
559
560 // TODO(b/189955605): we should add additional sessions dynamically
561 // instead of all at once - the other side should be responsible for setting
562 // up additional connections. We need to create at least one (unless 0 are
563 // requested to be set) in order to allow the other side to reliably make
564 // any requests at all.
565
566 // we've already setup one client
567 LOG_RPC_DETAIL("RpcSession::setupClient() instantiating %zu outgoing connections (server max: "
568 "%zu) and %zu incoming threads",
569 outgoingConnections, numThreadsAvailable, mMaxIncomingThreads);
570 for (size_t i = 0; i + 1 < outgoingConnections; i++) {
571 if (status_t status = connectAndInit(mId, false /*incoming*/); status != OK) return status;
572 }
573
574 for (size_t i = 0; i < mMaxIncomingThreads; i++) {
575 if (status_t status = connectAndInit(mId, true /*incoming*/); status != OK) return status;
576 }
577
578 cleanup.release();
579
580 return OK;
581 }
582
setupSocketClient(const RpcSocketAddress & addr)583 status_t RpcSession::setupSocketClient(const RpcSocketAddress& addr) {
584 return setupClient([&](const std::vector<uint8_t>& sessionId, bool incoming) {
585 return setupOneSocketConnection(addr, sessionId, incoming);
586 });
587 }
588
setupOneSocketConnection(const RpcSocketAddress & addr,const std::vector<uint8_t> & sessionId,bool incoming)589 status_t RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr,
590 const std::vector<uint8_t>& sessionId,
591 bool incoming) {
592 for (size_t tries = 0; tries < 5; tries++) {
593 if (tries > 0) usleep(10000);
594
595 unique_fd serverFd(TEMP_FAILURE_RETRY(
596 socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)));
597 if (!serverFd.ok()) {
598 int savedErrno = errno;
599 ALOGE("Could not create socket at %s: %s", addr.toString().c_str(),
600 strerror(savedErrno));
601 return -savedErrno;
602 }
603
604 if (addr.addr()->sa_family == AF_INET || addr.addr()->sa_family == AF_INET6) {
605 int noDelay = 1;
606 int result =
607 setsockopt(serverFd.get(), IPPROTO_TCP, TCP_NODELAY, &noDelay, sizeof(noDelay));
608 if (result < 0) {
609 int savedErrno = errno;
610 ALOGE("Could not set TCP_NODELAY on %s: %s", addr.toString().c_str(),
611 strerror(savedErrno));
612 return -savedErrno;
613 }
614 }
615
616 RpcTransportFd transportFd(std::move(serverFd));
617
618 if (0 != TEMP_FAILURE_RETRY(connect(transportFd.fd.get(), addr.addr(), addr.addrSize()))) {
619 int connErrno = errno;
620 if (connErrno == EAGAIN || connErrno == EINPROGRESS) {
621 // For non-blocking sockets, connect() may return EAGAIN (for unix domain socket) or
622 // EINPROGRESS (for others). Call poll() and getsockopt() to get the error.
623 status_t pollStatus = mShutdownTrigger->triggerablePoll(transportFd, POLLOUT);
624 if (pollStatus != OK) {
625 ALOGE("Could not POLLOUT after connect() on non-blocking socket: %s",
626 statusToString(pollStatus).c_str());
627 return pollStatus;
628 }
629 // Set connErrno to the errno that connect() would have set if the fd were blocking.
630 socklen_t connErrnoLen = sizeof(connErrno);
631 int ret = getsockopt(transportFd.fd.get(), SOL_SOCKET, SO_ERROR, &connErrno,
632 &connErrnoLen);
633 if (ret == -1) {
634 int savedErrno = errno;
635 ALOGE("Could not getsockopt() after connect() on non-blocking socket: %s. "
636 "(Original error from connect() is: %s)",
637 strerror(savedErrno), strerror(connErrno));
638 return -savedErrno;
639 }
640 // Retrieved the real connErrno as if connect() was called with a blocking socket
641 // fd. Continue checking connErrno.
642 }
643 if (connErrno == ECONNRESET) {
644 ALOGW("Connection reset on %s", addr.toString().c_str());
645 continue;
646 }
647 // connErrno could be zero if getsockopt determines so. Hence zero-check again.
648 if (connErrno != 0) {
649 ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(),
650 strerror(connErrno));
651 return -connErrno;
652 }
653 }
654 LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(),
655 transportFd.fd.get());
656
657 return initAndAddConnection(std::move(transportFd), sessionId, incoming);
658 }
659
660 ALOGE("Ran out of retries to connect to %s", addr.toString().c_str());
661 return UNKNOWN_ERROR;
662 }
663
initAndAddConnection(RpcTransportFd fd,const std::vector<uint8_t> & sessionId,bool incoming)664 status_t RpcSession::initAndAddConnection(RpcTransportFd fd, const std::vector<uint8_t>& sessionId,
665 bool incoming) {
666 LOG_ALWAYS_FATAL_IF(mShutdownTrigger == nullptr);
667 auto server = mCtx->newTransport(std::move(fd), mShutdownTrigger.get());
668 if (server == nullptr) {
669 ALOGE("%s: Unable to set up RpcTransport", __PRETTY_FUNCTION__);
670 return UNKNOWN_ERROR;
671 }
672
673 LOG_RPC_DETAIL("Socket at client with RpcTransport %p", server.get());
674
675 if (sessionId.size() > std::numeric_limits<uint16_t>::max()) {
676 ALOGE("Session ID too big %zu", sessionId.size());
677 return BAD_VALUE;
678 }
679
680 RpcConnectionHeader header{
681 .version = mProtocolVersion.value_or(RPC_WIRE_PROTOCOL_VERSION),
682 .options = 0,
683 .fileDescriptorTransportMode = static_cast<uint8_t>(mFileDescriptorTransportMode),
684 .sessionIdSize = static_cast<uint16_t>(sessionId.size()),
685 };
686
687 if (incoming) {
688 header.options |= RPC_CONNECTION_OPTION_INCOMING;
689 }
690
691 iovec headerIov{&header, sizeof(header)};
692 auto sendHeaderStatus = server->interruptableWriteFully(mShutdownTrigger.get(), &headerIov, 1,
693 std::nullopt, nullptr);
694 if (sendHeaderStatus != OK) {
695 ALOGE("Could not write connection header to socket: %s",
696 statusToString(sendHeaderStatus).c_str());
697 return sendHeaderStatus;
698 }
699
700 if (sessionId.size() > 0) {
701 iovec sessionIov{const_cast<void*>(static_cast<const void*>(sessionId.data())),
702 sessionId.size()};
703 auto sendSessionIdStatus =
704 server->interruptableWriteFully(mShutdownTrigger.get(), &sessionIov, 1,
705 std::nullopt, nullptr);
706 if (sendSessionIdStatus != OK) {
707 ALOGE("Could not write session ID ('%s') to socket: %s",
708 HexString(sessionId.data(), sessionId.size()).c_str(),
709 statusToString(sendSessionIdStatus).c_str());
710 return sendSessionIdStatus;
711 }
712 }
713
714 LOG_RPC_DETAIL("Socket at client: header sent");
715
716 if (incoming) {
717 return addIncomingConnection(std::move(server));
718 } else {
719 return addOutgoingConnection(std::move(server), true /*init*/);
720 }
721 }
722
addIncomingConnection(std::unique_ptr<RpcTransport> rpcTransport)723 status_t RpcSession::addIncomingConnection(std::unique_ptr<RpcTransport> rpcTransport) {
724 RpcMutex mutex;
725 RpcConditionVariable joinCv;
726 RpcMutexUniqueLock lock(mutex);
727 RpcMaybeThread thread;
728 sp<RpcSession> thiz = sp<RpcSession>::fromExisting(this);
729 bool ownershipTransferred = false;
730 thread = RpcMaybeThread([&]() {
731 RpcMutexUniqueLock threadLock(mutex);
732 std::unique_ptr<RpcTransport> movedRpcTransport = std::move(rpcTransport);
733 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
734 sp<RpcSession> session = thiz;
735 session->preJoinThreadOwnership(std::move(thread));
736
737 // only continue once we have a response or the connection fails
738 auto setupResult = session->preJoinSetup(std::move(movedRpcTransport));
739
740 ownershipTransferred = true;
741 threadLock.unlock();
742 joinCv.notify_one();
743 // do not use & vars below
744
745 RpcSession::join(std::move(session), std::move(setupResult));
746 });
747 rpcJoinIfSingleThreaded(thread);
748 joinCv.wait(lock, [&] { return ownershipTransferred; });
749 LOG_ALWAYS_FATAL_IF(!ownershipTransferred);
750 return OK;
751 }
752
initShutdownTrigger()753 status_t RpcSession::initShutdownTrigger() {
754 // first client connection added, but setForServer not called, so
755 // initializaing for a client.
756 if (mShutdownTrigger == nullptr) {
757 mShutdownTrigger = FdTrigger::make();
758 mEventListener = mShutdownListener = sp<WaitForShutdownListener>::make();
759 if (mShutdownTrigger == nullptr) return INVALID_OPERATION;
760 }
761 return OK;
762 }
763
addOutgoingConnection(std::unique_ptr<RpcTransport> rpcTransport,bool init)764 status_t RpcSession::addOutgoingConnection(std::unique_ptr<RpcTransport> rpcTransport, bool init) {
765 sp<RpcConnection> connection = sp<RpcConnection>::make();
766 {
767 RpcMutexLockGuard _l(mMutex);
768 connection->rpcTransport = std::move(rpcTransport);
769 connection->exclusiveTid = binder::os::GetThreadId();
770 mConnections.mOutgoing.push_back(connection);
771 }
772
773 status_t status = OK;
774 if (init) {
775 status =
776 mRpcBinderState->sendConnectionInit(connection, sp<RpcSession>::fromExisting(this));
777 }
778
779 clearConnectionTid(connection);
780
781 return status;
782 }
783
setForServer(const wp<RpcServer> & server,const wp<EventListener> & eventListener,const std::vector<uint8_t> & sessionId,const sp<IBinder> & sessionSpecificRoot)784 bool RpcSession::setForServer(const wp<RpcServer>& server, const wp<EventListener>& eventListener,
785 const std::vector<uint8_t>& sessionId,
786 const sp<IBinder>& sessionSpecificRoot) {
787 LOG_ALWAYS_FATAL_IF(mForServer != nullptr);
788 LOG_ALWAYS_FATAL_IF(server == nullptr);
789 LOG_ALWAYS_FATAL_IF(mEventListener != nullptr);
790 LOG_ALWAYS_FATAL_IF(eventListener == nullptr);
791 LOG_ALWAYS_FATAL_IF(mShutdownTrigger != nullptr);
792 LOG_ALWAYS_FATAL_IF(mCtx != nullptr);
793
794 mShutdownTrigger = FdTrigger::make();
795 if (mShutdownTrigger == nullptr) return false;
796
797 mId = sessionId;
798 mForServer = server;
799 mEventListener = eventListener;
800 mSessionSpecificRootObject = sessionSpecificRoot;
801 return true;
802 }
803
assignIncomingConnectionToThisThread(std::unique_ptr<RpcTransport> rpcTransport)804 sp<RpcSession::RpcConnection> RpcSession::assignIncomingConnectionToThisThread(
805 std::unique_ptr<RpcTransport> rpcTransport) {
806 RpcMutexLockGuard _l(mMutex);
807
808 if (mConnections.mIncoming.size() >= mMaxIncomingThreads) {
809 ALOGE("Cannot add thread to session with %zu threads (max is set to %zu)",
810 mConnections.mIncoming.size(), mMaxIncomingThreads);
811 return nullptr;
812 }
813
814 // Don't accept any more connections, some have shutdown. Usually this
815 // happens when new connections are still being established as part of a
816 // very short-lived session which shuts down after it already started
817 // accepting new connections.
818 if (mConnections.mIncoming.size() < mConnections.mMaxIncoming) {
819 return nullptr;
820 }
821
822 sp<RpcConnection> session = sp<RpcConnection>::make();
823 session->rpcTransport = std::move(rpcTransport);
824 session->exclusiveTid = binder::os::GetThreadId();
825
826 mConnections.mIncoming.push_back(session);
827 mConnections.mMaxIncoming = mConnections.mIncoming.size();
828
829 return session;
830 }
831
removeIncomingConnection(const sp<RpcConnection> & connection)832 bool RpcSession::removeIncomingConnection(const sp<RpcConnection>& connection) {
833 RpcMutexUniqueLock _l(mMutex);
834 if (auto it =
835 std::find(mConnections.mIncoming.begin(), mConnections.mIncoming.end(), connection);
836 it != mConnections.mIncoming.end()) {
837 mConnections.mIncoming.erase(it);
838 if (mConnections.mIncoming.size() == 0) {
839 sp<EventListener> listener = mEventListener.promote();
840 if (listener) {
841 _l.unlock();
842 listener->onSessionAllIncomingThreadsEnded(sp<RpcSession>::fromExisting(this));
843 }
844 }
845 return true;
846 }
847 return false;
848 }
849
clearConnectionTid(const sp<RpcConnection> & connection)850 void RpcSession::clearConnectionTid(const sp<RpcConnection>& connection) {
851 RpcMutexUniqueLock _l(mMutex);
852 connection->exclusiveTid = std::nullopt;
853 if (mConnections.mWaitingThreads > 0) {
854 _l.unlock();
855 mAvailableConnectionCv.notify_one();
856 }
857 }
858
getCertificate(RpcCertificateFormat format)859 std::vector<uint8_t> RpcSession::getCertificate(RpcCertificateFormat format) {
860 return mCtx->getCertificate(format);
861 }
862
find(const sp<RpcSession> & session,ConnectionUse use,ExclusiveConnection * connection)863 status_t RpcSession::ExclusiveConnection::find(const sp<RpcSession>& session, ConnectionUse use,
864 ExclusiveConnection* connection) {
865 connection->mSession = session;
866 connection->mConnection = nullptr;
867 connection->mReentrant = false;
868
869 uint64_t tid = binder::os::GetThreadId();
870 RpcMutexUniqueLock _l(session->mMutex);
871
872 session->mConnections.mWaitingThreads++;
873 while (true) {
874 sp<RpcConnection> exclusive;
875 sp<RpcConnection> available;
876
877 // CHECK FOR DEDICATED CLIENT SOCKET
878 //
879 // A server/looper should always use a dedicated connection if available
880 findConnection(tid, &exclusive, &available, session->mConnections.mOutgoing,
881 session->mConnections.mOutgoingOffset);
882
883 // WARNING: this assumes a server cannot request its client to send
884 // a transaction, as mIncoming is excluded below.
885 //
886 // Imagine we have more than one thread in play, and a single thread
887 // sends a synchronous, then an asynchronous command. Imagine the
888 // asynchronous command is sent on the first client connection. Then, if
889 // we naively send a synchronous command to that same connection, the
890 // thread on the far side might be busy processing the asynchronous
891 // command. So, we move to considering the second available thread
892 // for subsequent calls.
893 if (use == ConnectionUse::CLIENT_ASYNC && (exclusive != nullptr || available != nullptr)) {
894 session->mConnections.mOutgoingOffset = (session->mConnections.mOutgoingOffset + 1) %
895 session->mConnections.mOutgoing.size();
896 }
897
898 // USE SERVING SOCKET (e.g. nested transaction)
899 if (use != ConnectionUse::CLIENT_ASYNC) {
900 sp<RpcConnection> exclusiveIncoming;
901 // server connections are always assigned to a thread
902 findConnection(tid, &exclusiveIncoming, nullptr /*available*/,
903 session->mConnections.mIncoming, 0 /* index hint */);
904
905 // asynchronous calls cannot be nested, we currently allow ref count
906 // calls to be nested (so that you can use this without having extra
907 // threads). Note 'drainCommands' is used so that these ref counts can't
908 // build up.
909 if (exclusiveIncoming != nullptr) {
910 if (exclusiveIncoming->allowNested) {
911 // guaranteed to be processed as nested command
912 exclusive = exclusiveIncoming;
913 } else if (use == ConnectionUse::CLIENT_REFCOUNT && available == nullptr) {
914 // prefer available socket, but if we don't have one, don't
915 // wait for one
916 exclusive = exclusiveIncoming;
917 }
918 }
919 }
920
921 // if our thread is already using a connection, prioritize using that
922 if (exclusive != nullptr) {
923 connection->mConnection = exclusive;
924 connection->mReentrant = true;
925 break;
926 } else if (available != nullptr) {
927 connection->mConnection = available;
928 connection->mConnection->exclusiveTid = tid;
929 break;
930 }
931
932 if (session->mConnections.mOutgoing.size() == 0) {
933 ALOGE("Session has no outgoing connections. This is required for an RPC server to make "
934 "any non-nested (e.g. oneway or on another thread) calls. Use code request "
935 "reason: %d. Incoming connections: %zu. %s.",
936 static_cast<int>(use), session->mConnections.mIncoming.size(),
937 (session->server()
938 ? "This is a server session, so see RpcSession::setMaxIncomingThreads "
939 "for the corresponding client"
940 : "This is a client session, so see "
941 "RpcSession::setMaxOutgoingConnections "
942 "for this client or RpcServer::setMaxThreads for the corresponding "
943 "server"));
944 return WOULD_BLOCK;
945 }
946
947 LOG_RPC_DETAIL("No available connections (have %zu clients and %zu servers). Waiting...",
948 session->mConnections.mOutgoing.size(),
949 session->mConnections.mIncoming.size());
950 session->mAvailableConnectionCv.wait(_l);
951 }
952 session->mConnections.mWaitingThreads--;
953
954 return OK;
955 }
956
findConnection(uint64_t tid,sp<RpcConnection> * exclusive,sp<RpcConnection> * available,std::vector<sp<RpcConnection>> & sockets,size_t socketsIndexHint)957 void RpcSession::ExclusiveConnection::findConnection(uint64_t tid, sp<RpcConnection>* exclusive,
958 sp<RpcConnection>* available,
959 std::vector<sp<RpcConnection>>& sockets,
960 size_t socketsIndexHint) {
961 LOG_ALWAYS_FATAL_IF(sockets.size() > 0 && socketsIndexHint >= sockets.size(),
962 "Bad index %zu >= %zu", socketsIndexHint, sockets.size());
963
964 if (*exclusive != nullptr) return; // consistent with break below
965
966 for (size_t i = 0; i < sockets.size(); i++) {
967 sp<RpcConnection>& socket = sockets[(i + socketsIndexHint) % sockets.size()];
968
969 // take first available connection (intuition = caching)
970 if (available && *available == nullptr && socket->exclusiveTid == std::nullopt) {
971 *available = socket;
972 continue;
973 }
974
975 // though, prefer to take connection which is already inuse by this thread
976 // (nested transactions)
977 if (exclusive && socket->exclusiveTid == tid) {
978 *exclusive = socket;
979 break; // consistent with return above
980 }
981 }
982 }
983
~ExclusiveConnection()984 RpcSession::ExclusiveConnection::~ExclusiveConnection() {
985 // reentrant use of a connection means something less deep in the call stack
986 // is using this fd, and it retains the right to it. So, we don't give up
987 // exclusive ownership, and no thread is freed.
988 if (!mReentrant && mConnection != nullptr) {
989 mSession->clearConnectionTid(mConnection);
990 }
991 }
992
hasActiveConnection(const std::vector<sp<RpcConnection>> & connections)993 bool RpcSession::hasActiveConnection(const std::vector<sp<RpcConnection>>& connections) {
994 for (const auto& connection : connections) {
995 if (connection->exclusiveTid != std::nullopt && !connection->rpcTransport->isWaiting()) {
996 return true;
997 }
998 }
999 return false;
1000 }
1001
hasActiveRequests()1002 bool RpcSession::hasActiveRequests() {
1003 RpcMutexUniqueLock _l(mMutex);
1004 if (hasActiveConnection(mConnections.mIncoming)) {
1005 return true;
1006 }
1007 if (hasActiveConnection(mConnections.mOutgoing)) {
1008 return true;
1009 }
1010 return mConnections.mWaitingThreads != 0;
1011 }
1012
1013 } // namespace android
1014