blob: b0d3048c37752d45b023f07510a87a2a56326e6a [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
Tomasz Wasilczykfe25f122024-06-26 12:45:57 -070018#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
19#include <android-base/properties.h>
20#endif
21
Parth Sane56a04712024-04-22 14:21:07 +000022namespace android {
23
24using AidlServiceManager = android::os::IServiceManager;
25
26BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl)
27 : mTheRealServiceManager(impl) {}
28
29sp<AidlServiceManager> BackendUnifiedServiceManager::getImpl() {
30 return mTheRealServiceManager;
31}
32binder::Status BackendUnifiedServiceManager::getService(const ::std::string& name,
33 sp<IBinder>* _aidl_return) {
34 return mTheRealServiceManager->getService(name, _aidl_return);
35}
36binder::Status BackendUnifiedServiceManager::checkService(const ::std::string& name,
37 sp<IBinder>* _aidl_return) {
38 return mTheRealServiceManager->checkService(name, _aidl_return);
39}
40binder::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}
45binder::Status BackendUnifiedServiceManager::listServices(
46 int32_t dumpPriority, ::std::vector<::std::string>* _aidl_return) {
47 return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
48}
49binder::Status BackendUnifiedServiceManager::registerForNotifications(
50 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
51 return mTheRealServiceManager->registerForNotifications(name, callback);
52}
53binder::Status BackendUnifiedServiceManager::unregisterForNotifications(
54 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
55 return mTheRealServiceManager->unregisterForNotifications(name, callback);
56}
57binder::Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name,
58 bool* _aidl_return) {
59 return mTheRealServiceManager->isDeclared(name, _aidl_return);
60}
61binder::Status BackendUnifiedServiceManager::getDeclaredInstances(
62 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
63 return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
64}
65binder::Status BackendUnifiedServiceManager::updatableViaApex(
66 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
67 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
68}
69binder::Status BackendUnifiedServiceManager::getUpdatableNames(
70 const ::std::string& apexName, ::std::vector<::std::string>* _aidl_return) {
71 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
72}
73binder::Status BackendUnifiedServiceManager::getConnectionInfo(
74 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
75 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
76}
77binder::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}
82binder::Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
83 const sp<IBinder>& service) {
84 return mTheRealServiceManager->tryUnregisterService(name, service);
85}
86binder::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
94sp<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