Skip to content

Commit 3c93111

Browse files
Enable ServiceManager APIs without servicemanager process
In some devices, like Microdroid, there is no servicemanager process. These ServiceManager APIs may still be used with the new Accessor pattern for communication over sockets with binder RPC. This CL adds support for all of the getService APIs whithout relying on the servicemanager process. Future API support will be added separately. Test: atest vm_accessor_test binderLibTest Bug: 358427181 Change-Id: Ibef52415bf1509cdcd6c9cb65dbb8e20ec08d007
1 parent b53ef23 commit 3c93111

3 files changed

Lines changed: 145 additions & 33 deletions

File tree

libs/binder/BackendUnifiedServiceManager.cpp

Lines changed: 92 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ using AidlServiceManager = android::os::IServiceManager;
4747
using android::os::IAccessor;
4848
using binder::Status;
4949

50+
static const char* kUnsupportedOpNoServiceManager =
51+
"Unsupported operation without a kernel binder servicemanager process";
52+
5053
static const char* kStaticCachableList[] = {
5154
// go/keep-sorted start
5255
"accessibility",
@@ -220,7 +223,10 @@ Status BackendUnifiedServiceManager::getService2(const ::std::string& name, os::
220223
return Status::ok();
221224
}
222225
os::Service service;
223-
Status status = mTheRealServiceManager->getService2(name, &service);
226+
Status status = Status::ok();
227+
if (mTheRealServiceManager) {
228+
status = mTheRealServiceManager->getService2(name, &service);
229+
}
224230

225231
if (status.isOk()) {
226232
status = toBinderService(name, service, _out);
@@ -237,7 +243,10 @@ Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os:
237243
return Status::ok();
238244
}
239245

240-
Status status = mTheRealServiceManager->checkService(name, &service);
246+
Status status = Status::ok();
247+
if (mTheRealServiceManager) {
248+
status = mTheRealServiceManager->checkService(name, &service);
249+
}
241250
if (status.isOk()) {
242251
status = toBinderService(name, service, _out);
243252
if (status.isOk()) {
@@ -315,66 +324,128 @@ Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
315324
Status BackendUnifiedServiceManager::addService(const ::std::string& name,
316325
const sp<IBinder>& service, bool allowIsolated,
317326
int32_t dumpPriority) {
318-
Status status = mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
319-
// mEnableAddServiceCache is true by default.
320-
if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) {
321-
return updateCache(name, service,
322-
dumpPriority & android::os::IServiceManager::FLAG_IS_LAZY_SERVICE);
327+
if (mTheRealServiceManager) {
328+
Status status =
329+
mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
330+
// mEnableAddServiceCache is true by default.
331+
if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) {
332+
return updateCache(name, service,
333+
dumpPriority & android::os::IServiceManager::FLAG_IS_LAZY_SERVICE);
334+
}
335+
return status;
323336
}
324-
return status;
337+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
338+
kUnsupportedOpNoServiceManager);
325339
}
326340
Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority,
327341
::std::vector<::std::string>* _aidl_return) {
328-
return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
342+
if (mTheRealServiceManager) {
343+
return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
344+
}
345+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
346+
kUnsupportedOpNoServiceManager);
329347
}
330348
Status BackendUnifiedServiceManager::registerForNotifications(
331349
const ::std::string& name, const sp<os::IServiceCallback>& callback) {
332-
return mTheRealServiceManager->registerForNotifications(name, callback);
350+
if (mTheRealServiceManager) {
351+
return mTheRealServiceManager->registerForNotifications(name, callback);
352+
}
353+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
354+
kUnsupportedOpNoServiceManager);
333355
}
334356
Status BackendUnifiedServiceManager::unregisterForNotifications(
335357
const ::std::string& name, const sp<os::IServiceCallback>& callback) {
336-
return mTheRealServiceManager->unregisterForNotifications(name, callback);
358+
if (mTheRealServiceManager) {
359+
return mTheRealServiceManager->unregisterForNotifications(name, callback);
360+
}
361+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
362+
kUnsupportedOpNoServiceManager);
337363
}
338364
Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name, bool* _aidl_return) {
339-
return mTheRealServiceManager->isDeclared(name, _aidl_return);
365+
if (mTheRealServiceManager) {
366+
return mTheRealServiceManager->isDeclared(name, _aidl_return);
367+
}
368+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
369+
kUnsupportedOpNoServiceManager);
340370
}
341371
Status BackendUnifiedServiceManager::getDeclaredInstances(
342372
const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
343-
return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
373+
if (mTheRealServiceManager) {
374+
return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
375+
}
376+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
377+
kUnsupportedOpNoServiceManager);
344378
}
345379
Status BackendUnifiedServiceManager::updatableViaApex(
346380
const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
347-
return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
381+
if (mTheRealServiceManager) {
382+
return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
383+
}
384+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
385+
kUnsupportedOpNoServiceManager);
348386
}
349387
Status BackendUnifiedServiceManager::getUpdatableNames(const ::std::string& apexName,
350388
::std::vector<::std::string>* _aidl_return) {
351-
return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
389+
if (mTheRealServiceManager) {
390+
return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
391+
}
392+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
393+
kUnsupportedOpNoServiceManager);
352394
}
353395
Status BackendUnifiedServiceManager::getConnectionInfo(
354396
const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
355-
return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
397+
if (mTheRealServiceManager) {
398+
return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
399+
}
400+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
401+
kUnsupportedOpNoServiceManager);
356402
}
357403
Status BackendUnifiedServiceManager::registerClientCallback(
358404
const ::std::string& name, const sp<IBinder>& service,
359405
const sp<os::IClientCallback>& callback) {
360-
return mTheRealServiceManager->registerClientCallback(name, service, callback);
406+
if (mTheRealServiceManager) {
407+
return mTheRealServiceManager->registerClientCallback(name, service, callback);
408+
}
409+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
410+
kUnsupportedOpNoServiceManager);
361411
}
362412
Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
363413
const sp<IBinder>& service) {
364-
return mTheRealServiceManager->tryUnregisterService(name, service);
414+
if (mTheRealServiceManager) {
415+
return mTheRealServiceManager->tryUnregisterService(name, service);
416+
}
417+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
418+
kUnsupportedOpNoServiceManager);
365419
}
366420
Status BackendUnifiedServiceManager::getServiceDebugInfo(
367421
::std::vector<os::ServiceDebugInfo>* _aidl_return) {
368-
return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
422+
if (mTheRealServiceManager) {
423+
return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
424+
}
425+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
426+
kUnsupportedOpNoServiceManager);
369427
}
370428

371429
[[clang::no_destroy]] static std::once_flag gUSmOnce;
372430
[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
373431

432+
static bool hasOutOfProcessServiceManager() {
433+
#ifndef BINDER_WITH_KERNEL_IPC
434+
return false;
435+
#else
436+
#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
437+
return android::base::GetBoolProperty("servicemanager.installed", true);
438+
#else
439+
return true;
440+
#endif
441+
#endif // BINDER_WITH_KERNEL_IPC
442+
}
443+
374444
sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
375445
std::call_once(gUSmOnce, []() {
376446
#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
377-
/* wait for service manager */ {
447+
/* wait for service manager */
448+
if (hasOutOfProcessServiceManager()) {
378449
using std::literals::chrono_literals::operator""s;
379450
using android::base::WaitForProperty;
380451
while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
@@ -384,7 +455,7 @@ sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
384455
#endif
385456

386457
sp<AidlServiceManager> sm = nullptr;
387-
while (sm == nullptr) {
458+
while (hasOutOfProcessServiceManager() && sm == nullptr) {
388459
sm = interface_cast<AidlServiceManager>(
389460
ProcessState::self()->getContextObject(nullptr));
390461
if (sm == nullptr) {

libs/binder/include/binder/IServiceManager.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ class LIBBINDER_EXPORTED IServiceManager : public IInterface {
8080

8181
/**
8282
* Register a service.
83+
*
84+
* Note:
85+
* This status_t return value may be an exception code from an underlying
86+
* Status type that doesn't have a representive error code in
87+
* utils/Errors.h.
88+
* One example of this is a return value of -7
89+
* (Status::Exception::EX_UNSUPPORTED_OPERATION) when the service manager
90+
* process is not installed on the device when addService is called.
8391
*/
8492
// NOLINTNEXTLINE(google-default-arguments)
8593
virtual status_t addService(const String16& name, const sp<IBinder>& service,

libs/binder/tests/binderLibTest.cpp

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <processgroup/processgroup.h>
4545
#include <utils/Flattenable.h>
4646
#include <utils/SystemClock.h>
47+
#include "binder/IServiceManagerUnitTestHelper.h"
4748

4849
#include <linux/sched.h>
4950
#include <sys/epoll.h>
@@ -558,27 +559,22 @@ TEST_F(BinderLibTest, AddManagerToManager) {
558559
EXPECT_EQ(NO_ERROR, sm->addService(String16("binderLibTest-manager"), binder));
559560
}
560561

562+
class LocalRegistrationCallbackImpl : public virtual IServiceManager::LocalRegistrationCallback {
563+
void onServiceRegistration(const String16&, const sp<IBinder>&) override {}
564+
virtual ~LocalRegistrationCallbackImpl() {}
565+
};
566+
561567
TEST_F(BinderLibTest, RegisterForNotificationsFailure) {
562568
auto sm = defaultServiceManager();
563-
using LocalRegistrationCallback = IServiceManager::LocalRegistrationCallback;
564-
class LocalRegistrationCallbackImpl : public virtual LocalRegistrationCallback {
565-
void onServiceRegistration(const String16&, const sp<IBinder>&) override {}
566-
virtual ~LocalRegistrationCallbackImpl() {}
567-
};
568-
sp<LocalRegistrationCallback> cb = sp<LocalRegistrationCallbackImpl>::make();
569+
sp<IServiceManager::LocalRegistrationCallback> cb = sp<LocalRegistrationCallbackImpl>::make();
569570

570571
EXPECT_EQ(BAD_VALUE, sm->registerForNotifications(String16("ValidName"), nullptr));
571572
EXPECT_EQ(UNKNOWN_ERROR, sm->registerForNotifications(String16("InvalidName!$"), cb));
572573
}
573574

574575
TEST_F(BinderLibTest, UnregisterForNotificationsFailure) {
575576
auto sm = defaultServiceManager();
576-
using LocalRegistrationCallback = IServiceManager::LocalRegistrationCallback;
577-
class LocalRegistrationCallbackImpl : public virtual LocalRegistrationCallback {
578-
void onServiceRegistration(const String16&, const sp<IBinder>&) override {}
579-
virtual ~LocalRegistrationCallbackImpl() {}
580-
};
581-
sp<LocalRegistrationCallback> cb = sp<LocalRegistrationCallbackImpl>::make();
577+
sp<IServiceManager::LocalRegistrationCallback> cb = sp<LocalRegistrationCallbackImpl>::make();
582578

583579
EXPECT_EQ(OK, sm->registerForNotifications(String16("ValidName"), cb));
584580

@@ -1667,6 +1663,43 @@ TEST(ServiceNotifications, Unregister) {
16671663
EXPECT_EQ(sm->unregisterForNotifications(String16("RogerRafa"), cb), OK);
16681664
}
16691665

1666+
// Make sure all IServiceManager APIs will function without an AIDL service
1667+
// manager registered on the device.
1668+
TEST(ServiceManagerNoAidlServer, SanityCheck) {
1669+
String16 kServiceName("no_services_exist");
1670+
// This is what clients will see when there is no servicemanager process
1671+
// that registers itself as context object 0.
1672+
// Can't use setDefaultServiceManager() here because these test cases run in
1673+
// the same process and will abort when called twice or before/after
1674+
// defaultServiceManager().
1675+
sp<IServiceManager> sm = getServiceManagerShimFromAidlServiceManagerForTests(nullptr);
1676+
auto status = sm->addService(kServiceName, sp<BBinder>::make());
1677+
// CppBackendShim returns Status::exceptionCode as the status_t
1678+
EXPECT_EQ(status, Status::Exception::EX_UNSUPPORTED_OPERATION) << statusToString(status);
1679+
auto service = sm->checkService(String16("no_services_exist"));
1680+
EXPECT_TRUE(service == nullptr);
1681+
auto list = sm->listServices(android::IServiceManager::DUMP_FLAG_PRIORITY_ALL);
1682+
EXPECT_TRUE(list.isEmpty());
1683+
bool declared = sm->isDeclared(kServiceName);
1684+
EXPECT_FALSE(declared);
1685+
list = sm->getDeclaredInstances(kServiceName);
1686+
EXPECT_TRUE(list.isEmpty());
1687+
auto updatable = sm->updatableViaApex(kServiceName);
1688+
EXPECT_EQ(updatable, std::nullopt);
1689+
list = sm->getUpdatableNames(kServiceName);
1690+
EXPECT_TRUE(list.isEmpty());
1691+
auto conInfo = sm->getConnectionInfo(kServiceName);
1692+
EXPECT_EQ(conInfo, std::nullopt);
1693+
auto cb = sp<LocalRegistrationCallbackImpl>::make();
1694+
status = sm->registerForNotifications(kServiceName, cb);
1695+
EXPECT_EQ(status, UNKNOWN_ERROR) << statusToString(status);
1696+
status = sm->unregisterForNotifications(kServiceName, cb);
1697+
EXPECT_EQ(status, BAD_VALUE) << statusToString(status);
1698+
auto dbgInfos = sm->getServiceDebugInfo();
1699+
EXPECT_TRUE(dbgInfos.empty());
1700+
sm->enableAddServiceCache(true);
1701+
}
1702+
16701703
TEST_F(BinderLibTest, ThreadPoolAvailableThreads) {
16711704
Parcel data, reply;
16721705
sp<IBinder> server = addServer();

0 commit comments

Comments
 (0)