1515 */
1616#include " BackendUnifiedServiceManager.h"
1717
18+ #include < android-base/strings.h>
1819#include < android/os/IAccessor.h>
1920#include < android/os/IServiceManager.h>
2021#include < binder/RpcSession.h>
@@ -47,6 +48,9 @@ using AidlServiceManager = android::os::IServiceManager;
4748using android::os::IAccessor;
4849using binder::Status;
4950
51+ static const char * kUnsupportedOpNoServiceManager =
52+ " Unsupported operation without a kernel binder servicemanager process" ;
53+
5054static const char * kStaticCachableList [] = {
5155 // go/keep-sorted start
5256 " accessibility" ,
@@ -220,7 +224,10 @@ Status BackendUnifiedServiceManager::getService2(const ::std::string& name, os::
220224 return Status::ok ();
221225 }
222226 os::Service service;
223- Status status = mTheRealServiceManager ->getService2 (name, &service);
227+ Status status = Status::ok ();
228+ if (mTheRealServiceManager ) {
229+ status = mTheRealServiceManager ->getService2 (name, &service);
230+ }
224231
225232 if (status.isOk ()) {
226233 status = toBinderService (name, service, _out);
@@ -237,7 +244,10 @@ Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os:
237244 return Status::ok ();
238245 }
239246
240- Status status = mTheRealServiceManager ->checkService (name, &service);
247+ Status status = Status::ok ();
248+ if (mTheRealServiceManager ) {
249+ status = mTheRealServiceManager ->checkService (name, &service);
250+ }
241251 if (status.isOk ()) {
242252 status = toBinderService (name, service, _out);
243253 if (status.isOk ()) {
@@ -315,66 +325,156 @@ Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
315325Status BackendUnifiedServiceManager::addService (const ::std::string& name,
316326 const sp<IBinder>& service, bool allowIsolated,
317327 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);
328+ if (mTheRealServiceManager ) {
329+ Status status =
330+ mTheRealServiceManager ->addService (name, service, allowIsolated, dumpPriority);
331+ // mEnableAddServiceCache is true by default.
332+ if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk ()) {
333+ return updateCache (name, service,
334+ dumpPriority & android::os::IServiceManager::FLAG_IS_LAZY_SERVICE);
335+ }
336+ return status;
323337 }
324- return status;
338+ return Status::fromExceptionCode (Status::EX_UNSUPPORTED_OPERATION,
339+ kUnsupportedOpNoServiceManager );
325340}
326341Status BackendUnifiedServiceManager::listServices (int32_t dumpPriority,
327342 ::std::vector<::std::string>* _aidl_return) {
328- return mTheRealServiceManager ->listServices (dumpPriority, _aidl_return);
343+ Status status = Status::ok ();
344+ if (mTheRealServiceManager ) {
345+ status = mTheRealServiceManager ->listServices (dumpPriority, _aidl_return);
346+ }
347+ if (!status.isOk ()) return status;
348+
349+ appendInjectedAccessorServices (_aidl_return);
350+
351+ return status;
329352}
330353Status BackendUnifiedServiceManager::registerForNotifications (
331354 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
332- return mTheRealServiceManager ->registerForNotifications (name, callback);
355+ if (mTheRealServiceManager ) {
356+ return mTheRealServiceManager ->registerForNotifications (name, callback);
357+ }
358+ return Status::fromExceptionCode (Status::EX_UNSUPPORTED_OPERATION,
359+ kUnsupportedOpNoServiceManager );
333360}
334361Status BackendUnifiedServiceManager::unregisterForNotifications (
335362 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
336- return mTheRealServiceManager ->unregisterForNotifications (name, callback);
363+ if (mTheRealServiceManager ) {
364+ return mTheRealServiceManager ->unregisterForNotifications (name, callback);
365+ }
366+ return Status::fromExceptionCode (Status::EX_UNSUPPORTED_OPERATION,
367+ kUnsupportedOpNoServiceManager );
337368}
338369Status BackendUnifiedServiceManager::isDeclared (const ::std::string& name, bool * _aidl_return) {
339- return mTheRealServiceManager ->isDeclared (name, _aidl_return);
370+ Status status = Status::ok ();
371+ if (mTheRealServiceManager ) {
372+ status = mTheRealServiceManager ->isDeclared (name, _aidl_return);
373+ }
374+ if (!status.isOk ()) return status;
375+
376+ if (!*_aidl_return) {
377+ forEachInjectedAccessorService ([&](const std::string& instance) {
378+ if (name == instance) {
379+ *_aidl_return = true ;
380+ }
381+ });
382+ }
383+
384+ return status;
340385}
341386Status BackendUnifiedServiceManager::getDeclaredInstances (
342387 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
343- return mTheRealServiceManager ->getDeclaredInstances (iface, _aidl_return);
388+ Status status = Status::ok ();
389+ if (mTheRealServiceManager ) {
390+ status = mTheRealServiceManager ->getDeclaredInstances (iface, _aidl_return);
391+ }
392+ if (!status.isOk ()) return status;
393+
394+ forEachInjectedAccessorService ([&](const std::string& instance) {
395+ // Declared instances have the format
396+ // <interface>/instance like foo.bar.ISomething/instance
397+ // If it does not have that format, consider the instance to be ""
398+ std::string_view name (instance);
399+ if (base::ConsumePrefix (&name, iface + " /" )) {
400+ _aidl_return->emplace_back (name);
401+ } else if (iface == instance) {
402+ _aidl_return->push_back (" " );
403+ }
404+ });
405+
406+ return status;
344407}
345408Status BackendUnifiedServiceManager::updatableViaApex (
346409 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
347- return mTheRealServiceManager ->updatableViaApex (name, _aidl_return);
410+ if (mTheRealServiceManager ) {
411+ return mTheRealServiceManager ->updatableViaApex (name, _aidl_return);
412+ }
413+ return Status::fromExceptionCode (Status::EX_UNSUPPORTED_OPERATION,
414+ kUnsupportedOpNoServiceManager );
348415}
349416Status BackendUnifiedServiceManager::getUpdatableNames (const ::std::string& apexName,
350417 ::std::vector<::std::string>* _aidl_return) {
351- return mTheRealServiceManager ->getUpdatableNames (apexName, _aidl_return);
418+ if (mTheRealServiceManager ) {
419+ return mTheRealServiceManager ->getUpdatableNames (apexName, _aidl_return);
420+ }
421+ return Status::fromExceptionCode (Status::EX_UNSUPPORTED_OPERATION,
422+ kUnsupportedOpNoServiceManager );
352423}
353424Status BackendUnifiedServiceManager::getConnectionInfo (
354425 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
355- return mTheRealServiceManager ->getConnectionInfo (name, _aidl_return);
426+ if (mTheRealServiceManager ) {
427+ return mTheRealServiceManager ->getConnectionInfo (name, _aidl_return);
428+ }
429+ return Status::fromExceptionCode (Status::EX_UNSUPPORTED_OPERATION,
430+ kUnsupportedOpNoServiceManager );
356431}
357432Status BackendUnifiedServiceManager::registerClientCallback (
358433 const ::std::string& name, const sp<IBinder>& service,
359434 const sp<os::IClientCallback>& callback) {
360- return mTheRealServiceManager ->registerClientCallback (name, service, callback);
435+ if (mTheRealServiceManager ) {
436+ return mTheRealServiceManager ->registerClientCallback (name, service, callback);
437+ }
438+ return Status::fromExceptionCode (Status::EX_UNSUPPORTED_OPERATION,
439+ kUnsupportedOpNoServiceManager );
361440}
362441Status BackendUnifiedServiceManager::tryUnregisterService (const ::std::string& name,
363442 const sp<IBinder>& service) {
364- return mTheRealServiceManager ->tryUnregisterService (name, service);
443+ if (mTheRealServiceManager ) {
444+ return mTheRealServiceManager ->tryUnregisterService (name, service);
445+ }
446+ return Status::fromExceptionCode (Status::EX_UNSUPPORTED_OPERATION,
447+ kUnsupportedOpNoServiceManager );
365448}
366449Status BackendUnifiedServiceManager::getServiceDebugInfo (
367450 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
368- return mTheRealServiceManager ->getServiceDebugInfo (_aidl_return);
451+ if (mTheRealServiceManager ) {
452+ return mTheRealServiceManager ->getServiceDebugInfo (_aidl_return);
453+ }
454+ return Status::fromExceptionCode (Status::EX_UNSUPPORTED_OPERATION,
455+ kUnsupportedOpNoServiceManager );
369456}
370457
371458[[clang::no_destroy]] static std::once_flag gUSmOnce ;
372459[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager ;
373460
461+ static bool hasOutOfProcessServiceManager () {
462+ #ifndef BINDER_WITH_KERNEL_IPC
463+ return false ;
464+ #else
465+ #if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
466+ return android::base::GetBoolProperty (" servicemanager.installed" , true );
467+ #else
468+ return true ;
469+ #endif
470+ #endif // BINDER_WITH_KERNEL_IPC
471+ }
472+
374473sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager () {
375474 std::call_once (gUSmOnce , []() {
376475#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
377- /* wait for service manager */ {
476+ /* wait for service manager */
477+ if (hasOutOfProcessServiceManager ()) {
378478 using std::literals::chrono_literals::operator " " s;
379479 using android::base::WaitForProperty;
380480 while (!WaitForProperty (" servicemanager.ready" , " true" , 1s)) {
@@ -384,7 +484,7 @@ sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
384484#endif
385485
386486 sp<AidlServiceManager> sm = nullptr ;
387- while (sm == nullptr ) {
487+ while (hasOutOfProcessServiceManager () && sm == nullptr ) {
388488 sm = interface_cast<AidlServiceManager>(
389489 ProcessState::self ()->getContextObject (nullptr ));
390490 if (sm == nullptr ) {
0 commit comments