| Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2024 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 | #include "BackendUnifiedServiceManager.h" | 
|  | 17 |  | 
| Tomasz Wasilczyk | fe25f12 | 2024-06-26 12:45:57 -0700 | [diff] [blame] | 18 | #if defined(__BIONIC__) && !defined(__ANDROID_VNDK__) | 
|  | 19 | #include <android-base/properties.h> | 
|  | 20 | #endif | 
|  | 21 |  | 
| Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 22 | namespace android { | 
|  | 23 |  | 
|  | 24 | using AidlServiceManager = android::os::IServiceManager; | 
|  | 25 |  | 
|  | 26 | BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl) | 
|  | 27 | : mTheRealServiceManager(impl) {} | 
|  | 28 |  | 
|  | 29 | sp<AidlServiceManager> BackendUnifiedServiceManager::getImpl() { | 
|  | 30 | return mTheRealServiceManager; | 
|  | 31 | } | 
|  | 32 | binder::Status BackendUnifiedServiceManager::getService(const ::std::string& name, | 
|  | 33 | sp<IBinder>* _aidl_return) { | 
|  | 34 | return mTheRealServiceManager->getService(name, _aidl_return); | 
|  | 35 | } | 
|  | 36 | binder::Status BackendUnifiedServiceManager::checkService(const ::std::string& name, | 
|  | 37 | sp<IBinder>* _aidl_return) { | 
|  | 38 | return mTheRealServiceManager->checkService(name, _aidl_return); | 
|  | 39 | } | 
|  | 40 | binder::Status BackendUnifiedServiceManager::addService(const ::std::string& name, | 
|  | 41 | const sp<IBinder>& service, | 
|  | 42 | bool allowIsolated, int32_t dumpPriority) { | 
|  | 43 | return mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority); | 
|  | 44 | } | 
|  | 45 | binder::Status BackendUnifiedServiceManager::listServices( | 
|  | 46 | int32_t dumpPriority, ::std::vector<::std::string>* _aidl_return) { | 
|  | 47 | return mTheRealServiceManager->listServices(dumpPriority, _aidl_return); | 
|  | 48 | } | 
|  | 49 | binder::Status BackendUnifiedServiceManager::registerForNotifications( | 
|  | 50 | const ::std::string& name, const sp<os::IServiceCallback>& callback) { | 
|  | 51 | return mTheRealServiceManager->registerForNotifications(name, callback); | 
|  | 52 | } | 
|  | 53 | binder::Status BackendUnifiedServiceManager::unregisterForNotifications( | 
|  | 54 | const ::std::string& name, const sp<os::IServiceCallback>& callback) { | 
|  | 55 | return mTheRealServiceManager->unregisterForNotifications(name, callback); | 
|  | 56 | } | 
|  | 57 | binder::Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name, | 
|  | 58 | bool* _aidl_return) { | 
|  | 59 | return mTheRealServiceManager->isDeclared(name, _aidl_return); | 
|  | 60 | } | 
|  | 61 | binder::Status BackendUnifiedServiceManager::getDeclaredInstances( | 
|  | 62 | const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) { | 
|  | 63 | return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return); | 
|  | 64 | } | 
|  | 65 | binder::Status BackendUnifiedServiceManager::updatableViaApex( | 
|  | 66 | const ::std::string& name, ::std::optional<::std::string>* _aidl_return) { | 
|  | 67 | return mTheRealServiceManager->updatableViaApex(name, _aidl_return); | 
|  | 68 | } | 
|  | 69 | binder::Status BackendUnifiedServiceManager::getUpdatableNames( | 
|  | 70 | const ::std::string& apexName, ::std::vector<::std::string>* _aidl_return) { | 
|  | 71 | return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return); | 
|  | 72 | } | 
|  | 73 | binder::Status BackendUnifiedServiceManager::getConnectionInfo( | 
|  | 74 | const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) { | 
|  | 75 | return mTheRealServiceManager->getConnectionInfo(name, _aidl_return); | 
|  | 76 | } | 
|  | 77 | binder::Status BackendUnifiedServiceManager::registerClientCallback( | 
|  | 78 | const ::std::string& name, const sp<IBinder>& service, | 
|  | 79 | const sp<os::IClientCallback>& callback) { | 
|  | 80 | return mTheRealServiceManager->registerClientCallback(name, service, callback); | 
|  | 81 | } | 
|  | 82 | binder::Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name, | 
|  | 83 | const sp<IBinder>& service) { | 
|  | 84 | return mTheRealServiceManager->tryUnregisterService(name, service); | 
|  | 85 | } | 
|  | 86 | binder::Status BackendUnifiedServiceManager::getServiceDebugInfo( | 
|  | 87 | ::std::vector<os::ServiceDebugInfo>* _aidl_return) { | 
|  | 88 | return mTheRealServiceManager->getServiceDebugInfo(_aidl_return); | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | [[clang::no_destroy]] static std::once_flag gUSmOnce; | 
|  | 92 | [[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager; | 
|  | 93 |  | 
|  | 94 | sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() { | 
|  | 95 | std::call_once(gUSmOnce, []() { | 
|  | 96 | #if defined(__BIONIC__) && !defined(__ANDROID_VNDK__) | 
|  | 97 | /* wait for service manager */ { | 
|  | 98 | using std::literals::chrono_literals::operator""s; | 
|  | 99 | using android::base::WaitForProperty; | 
|  | 100 | while (!WaitForProperty("servicemanager.ready", "true", 1s)) { | 
|  | 101 | ALOGE("Waited for servicemanager.ready for a second, waiting another..."); | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 | #endif | 
|  | 105 |  | 
|  | 106 | sp<AidlServiceManager> sm = nullptr; | 
|  | 107 | while (sm == nullptr) { | 
|  | 108 | sm = interface_cast<AidlServiceManager>( | 
|  | 109 | ProcessState::self()->getContextObject(nullptr)); | 
|  | 110 | if (sm == nullptr) { | 
|  | 111 | ALOGE("Waiting 1s on context object on %s.", | 
|  | 112 | ProcessState::self()->getDriverName().c_str()); | 
|  | 113 | sleep(1); | 
|  | 114 | } | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm); | 
|  | 118 | }); | 
|  | 119 |  | 
|  | 120 | return gUnifiedServiceManager; | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | } // namespace android |