Skip to content

Commit 74bc589

Browse files
Use ProcessState::selfOrNull in ServiceManager APIs
Allow the APIs to be used when ProcessState isn't initialized. This allows the use of these APIs in systems like microdroid where there is no kernel binder driver. Test: atest vm_accessor_test Bug: 358427181 Change-Id: Ie981b0ca4335ddc7a5676d30721c01be1ffdf35e
1 parent dcca938 commit 74bc589

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

libs/binder/BackendUnifiedServiceManager.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ static const char* kStaticCachableList[] = {
105105
};
106106

107107
bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) {
108-
if (ProcessState::self()->getThreadPoolMaxTotalThreadCount() <= 0) {
108+
sp<ProcessState> self = ProcessState::selfOrNull();
109+
if (!self || self->getThreadPoolMaxTotalThreadCount() <= 0) {
109110
ALOGW("Thread Pool max thread count is 0. Cannot cache binder as linkToDeath cannot be "
110111
"implemented. serviceName: %s",
111112
serviceName.c_str());

libs/binder/IServiceManager.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,9 @@ sp<IBinder> CppBackendShim::getService(const String16& name) const {
561561
sp<IBinder> svc = checkService(name);
562562
if (svc != nullptr) return svc;
563563

564+
sp<ProcessState> self = ProcessState::selfOrNull();
564565
const bool isVendorService =
565-
strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder") == 0;
566+
self && strcmp(self->getDriverName().c_str(), "/dev/vndbinder") == 0;
566567
constexpr auto timeout = 5s;
567568
const auto startTime = std::chrono::steady_clock::now();
568569
// Vendor code can't access system properties
@@ -579,7 +580,7 @@ sp<IBinder> CppBackendShim::getService(const String16& name) const {
579580
const useconds_t sleepTime = gSystemBootCompleted ? 1000 : 100;
580581

581582
ALOGI("Waiting for service '%s' on '%s'...", String8(name).c_str(),
582-
ProcessState::self()->getDriverName().c_str());
583+
self ? self->getDriverName().c_str() : "RPC accessors only");
583584

584585
int n = 0;
585586
while (std::chrono::steady_clock::now() - startTime < timeout) {
@@ -661,7 +662,8 @@ sp<IBinder> CppBackendShim::waitForService(const String16& name16) {
661662
if (Status status = realGetService(name, &out); !status.isOk()) {
662663
ALOGW("Failed to getService in waitForService for %s: %s", name.c_str(),
663664
status.toString8().c_str());
664-
if (0 == ProcessState::self()->getThreadPoolMaxTotalThreadCount()) {
665+
sp<ProcessState> self = ProcessState::selfOrNull();
666+
if (self && 0 == self->getThreadPoolMaxTotalThreadCount()) {
665667
ALOGW("Got service, but may be racey because we could not wait efficiently for it. "
666668
"Threadpool has 0 guaranteed threads. "
667669
"Is the threadpool configured properly? "
@@ -695,9 +697,10 @@ sp<IBinder> CppBackendShim::waitForService(const String16& name16) {
695697
if (waiter->mBinder != nullptr) return waiter->mBinder;
696698
}
697699

700+
sp<ProcessState> self = ProcessState::selfOrNull();
698701
ALOGW("Waited one second for %s (is service started? Number of threads started in the "
699702
"threadpool: %zu. Are binder threads started and available?)",
700-
name.c_str(), ProcessState::self()->getThreadPoolMaxTotalThreadCount());
703+
name.c_str(), self ? self->getThreadPoolMaxTotalThreadCount() : 0);
701704

702705
// Handle race condition for lazy services. Here is what can happen:
703706
// - the service dies (not processed by init yet).

0 commit comments

Comments
 (0)