blob: 0bf3cadd35f641eebb44db4eefee10b170dad905 [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
Alice Wang8578f132024-05-03 09:01:56 +000018#include <android/os/IAccessor.h>
19#include <binder/RpcSession.h>
20
Tomasz Wasilczykfe25f122024-06-26 12:45:57 -070021#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
22#include <android-base/properties.h>
23#endif
24
Parth Sane56a04712024-04-22 14:21:07 +000025namespace android {
26
27using AidlServiceManager = android::os::IServiceManager;
Alice Wang8578f132024-05-03 09:01:56 +000028using IAccessor = android::os::IAccessor;
Parth Sane56a04712024-04-22 14:21:07 +000029
30BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl)
31 : mTheRealServiceManager(impl) {}
32
33sp<AidlServiceManager> BackendUnifiedServiceManager::getImpl() {
34 return mTheRealServiceManager;
35}
36binder::Status BackendUnifiedServiceManager::getService(const ::std::string& name,
Alice Wang8578f132024-05-03 09:01:56 +000037 os::Service* _out) {
38 os::Service service;
39 binder::Status status = mTheRealServiceManager->getService(name, &service);
40 toBinderService(service, _out);
41 return status;
Parth Sane56a04712024-04-22 14:21:07 +000042}
Alice Wang8578f132024-05-03 09:01:56 +000043
Parth Sane56a04712024-04-22 14:21:07 +000044binder::Status BackendUnifiedServiceManager::checkService(const ::std::string& name,
Alice Wang8578f132024-05-03 09:01:56 +000045 os::Service* _out) {
46 os::Service service;
47 binder::Status status = mTheRealServiceManager->checkService(name, &service);
48 toBinderService(service, _out);
49 return status;
Parth Sane56a04712024-04-22 14:21:07 +000050}
Alice Wang8578f132024-05-03 09:01:56 +000051
52void BackendUnifiedServiceManager::toBinderService(const os::Service& in, os::Service* _out) {
53 switch (in.getTag()) {
54 case os::Service::Tag::binder: {
55 *_out = in;
56 break;
57 }
58 case os::Service::Tag::accessor: {
59 sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>();
60 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
61 if (accessor == nullptr) {
62 ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
63 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
64 break;
65 }
66 auto request = [=] {
67 os::ParcelFileDescriptor fd;
68 binder::Status ret = accessor->addConnection(&fd);
69 if (ret.isOk()) {
70 return base::unique_fd(fd.release());
71 } else {
72 ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str());
73 return base::unique_fd(-1);
74 }
75 };
76 auto session = RpcSession::make();
77 session->setupPreconnectedClient(base::unique_fd{}, request);
78 session->setSessionSpecificRoot(accessorBinder);
79 *_out = os::Service::make<os::Service::Tag::binder>(session->getRootObject());
80 break;
81 }
82 default: {
83 LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag());
84 }
85 }
86}
87
Parth Sane56a04712024-04-22 14:21:07 +000088binder::Status BackendUnifiedServiceManager::addService(const ::std::string& name,
89 const sp<IBinder>& service,
90 bool allowIsolated, int32_t dumpPriority) {
91 return mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
92}
93binder::Status BackendUnifiedServiceManager::listServices(
94 int32_t dumpPriority, ::std::vector<::std::string>* _aidl_return) {
95 return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
96}
97binder::Status BackendUnifiedServiceManager::registerForNotifications(
98 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
99 return mTheRealServiceManager->registerForNotifications(name, callback);
100}
101binder::Status BackendUnifiedServiceManager::unregisterForNotifications(
102 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
103 return mTheRealServiceManager->unregisterForNotifications(name, callback);
104}
105binder::Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name,
106 bool* _aidl_return) {
107 return mTheRealServiceManager->isDeclared(name, _aidl_return);
108}
109binder::Status BackendUnifiedServiceManager::getDeclaredInstances(
110 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
111 return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
112}
113binder::Status BackendUnifiedServiceManager::updatableViaApex(
114 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
115 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
116}
117binder::Status BackendUnifiedServiceManager::getUpdatableNames(
118 const ::std::string& apexName, ::std::vector<::std::string>* _aidl_return) {
119 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
120}
121binder::Status BackendUnifiedServiceManager::getConnectionInfo(
122 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
123 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
124}
125binder::Status BackendUnifiedServiceManager::registerClientCallback(
126 const ::std::string& name, const sp<IBinder>& service,
127 const sp<os::IClientCallback>& callback) {
128 return mTheRealServiceManager->registerClientCallback(name, service, callback);
129}
130binder::Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
131 const sp<IBinder>& service) {
132 return mTheRealServiceManager->tryUnregisterService(name, service);
133}
134binder::Status BackendUnifiedServiceManager::getServiceDebugInfo(
135 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
136 return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
137}
138
139[[clang::no_destroy]] static std::once_flag gUSmOnce;
140[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
141
142sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
143 std::call_once(gUSmOnce, []() {
144#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
145 /* wait for service manager */ {
146 using std::literals::chrono_literals::operator""s;
147 using android::base::WaitForProperty;
148 while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
149 ALOGE("Waited for servicemanager.ready for a second, waiting another...");
150 }
151 }
152#endif
153
154 sp<AidlServiceManager> sm = nullptr;
155 while (sm == nullptr) {
156 sm = interface_cast<AidlServiceManager>(
157 ProcessState::self()->getContextObject(nullptr));
158 if (sm == nullptr) {
159 ALOGE("Waiting 1s on context object on %s.",
160 ProcessState::self()->getDriverName().c_str());
161 sleep(1);
162 }
163 }
164
165 gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm);
166 });
167
168 return gUnifiedServiceManager;
169}
170
171} // namespace android