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