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