blob: 5680798d0dbcd12dd6f636b64f936879b4a51597 [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
Parth Saneb6ed0eb2024-06-25 14:38:42 +000027#ifdef LIBBINDER_CLIENT_CACHE
28constexpr bool kUseCache = true;
29#else
30constexpr bool kUseCache = false;
31#endif
32
Parth Sane56a04712024-04-22 14:21:07 +000033using AidlServiceManager = android::os::IServiceManager;
Alice Wang8578f132024-05-03 09:01:56 +000034using IAccessor = android::os::IAccessor;
Parth Sane56a04712024-04-22 14:21:07 +000035
Parth Saneb6ed0eb2024-06-25 14:38:42 +000036static const char* kStaticCachableList[] = {
37 "activity",
38 "android.hardware.thermal.IThermal/default",
39 "android.hardware.power.IPower/default",
40 "android.frameworks.stats.IStats/default",
41 "android.system.suspend.ISystemSuspend/default",
42 "appops",
43 "audio",
44 "batterystats",
45 "carrier_config",
46 "connectivity",
47 "content_capture",
48 "device_policy",
49 "display",
50 "dropbox",
51 "econtroller",
52 "isub",
53 "legacy_permission",
54 "location",
55 "media.extractor",
56 "media.metrics",
57 "media.player",
58 "media.resource_manager",
59 "netd_listener",
60 "netstats",
61 "network_management",
62 "nfc",
63 "package_native",
64 "performance_hint",
65 "permission",
66 "permissionmgr",
67 "permission_checker",
68 "phone",
69 "platform_compat",
70 "power",
71 "role",
72 "sensorservice",
73 "statscompanion",
74 "telephony.registry",
75 "thermalservice",
76 "time_detector",
77 "trust",
78 "uimode",
79 "virtualdevice",
80 "virtualdevice_native",
81 "webviewupdate",
82};
83
84bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) {
85 if (ProcessState::self()->getThreadPoolMaxTotalThreadCount() <= 0) {
86 ALOGW("Thread Pool max thread count is 0. Cannot cache binder as linkToDeath cannot be "
87 "implemented. serviceName: %s",
88 serviceName.c_str());
89 return false;
90 }
91 for (const char* name : kStaticCachableList) {
92 if (name == serviceName) {
93 return true;
94 }
95 }
96 return false;
97}
98
99binder::Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
100 const os::Service& service) {
101 if (!kUseCache) {
102 return binder::Status::ok();
103 }
104 if (service.getTag() == os::Service::Tag::binder) {
105 sp<IBinder> binder = service.get<os::Service::Tag::binder>();
106 if (binder && mCacheForGetService->isClientSideCachingEnabled(serviceName) &&
107 binder->isBinderAlive()) {
108 return mCacheForGetService->setItem(serviceName, binder);
109 }
110 }
111 return binder::Status::ok();
112}
113
114bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName,
115 os::Service* _out) {
116 if (!kUseCache) {
117 return false;
118 }
119 sp<IBinder> item = mCacheForGetService->getItem(serviceName);
120 // TODO(b/363177618): Enable caching for binders which are always null.
121 if (item != nullptr && item->isBinderAlive()) {
122 *_out = os::Service::make<os::Service::Tag::binder>(item);
123 return true;
124 }
125 return false;
126}
127
Parth Sane56a04712024-04-22 14:21:07 +0000128BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl)
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000129 : mTheRealServiceManager(impl) {
130 mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>();
131}
Parth Sane56a04712024-04-22 14:21:07 +0000132
133sp<AidlServiceManager> BackendUnifiedServiceManager::getImpl() {
134 return mTheRealServiceManager;
135}
Alice Wang11da1502024-07-25 12:03:22 +0000136
Parth Sane56a04712024-04-22 14:21:07 +0000137binder::Status BackendUnifiedServiceManager::getService(const ::std::string& name,
Alice Wang11da1502024-07-25 12:03:22 +0000138 sp<IBinder>* _aidl_return) {
Alice Wang8578f132024-05-03 09:01:56 +0000139 os::Service service;
Alice Wang11da1502024-07-25 12:03:22 +0000140 binder::Status status = getService2(name, &service);
141 *_aidl_return = service.get<os::Service::Tag::binder>();
142 return status;
143}
144
145binder::Status BackendUnifiedServiceManager::getService2(const ::std::string& name,
146 os::Service* _out) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000147 if (returnIfCached(name, _out)) {
148 return binder::Status::ok();
149 }
Alice Wang11da1502024-07-25 12:03:22 +0000150 os::Service service;
151 binder::Status status = mTheRealServiceManager->getService2(name, &service);
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000152
Devin Moore18f63752024-08-08 21:01:24 +0000153 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000154 status = toBinderService(name, service, _out);
155 if (status.isOk()) {
156 return updateCache(name, service);
157 }
Devin Moore18f63752024-08-08 21:01:24 +0000158 }
Alice Wang8578f132024-05-03 09:01:56 +0000159 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000160}
Alice Wang8578f132024-05-03 09:01:56 +0000161
Parth Sane56a04712024-04-22 14:21:07 +0000162binder::Status BackendUnifiedServiceManager::checkService(const ::std::string& name,
Alice Wang8578f132024-05-03 09:01:56 +0000163 os::Service* _out) {
164 os::Service service;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000165 if (returnIfCached(name, _out)) {
166 return binder::Status::ok();
167 }
168
Alice Wang8578f132024-05-03 09:01:56 +0000169 binder::Status status = mTheRealServiceManager->checkService(name, &service);
Devin Moore18f63752024-08-08 21:01:24 +0000170 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000171 status = toBinderService(name, service, _out);
172 if (status.isOk()) {
173 return updateCache(name, service);
174 }
Devin Moore18f63752024-08-08 21:01:24 +0000175 }
Alice Wang8578f132024-05-03 09:01:56 +0000176 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000177}
Alice Wang8578f132024-05-03 09:01:56 +0000178
Devin Moore18f63752024-08-08 21:01:24 +0000179binder::Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
180 const os::Service& in,
181 os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000182 switch (in.getTag()) {
183 case os::Service::Tag::binder: {
Devin Moore18f63752024-08-08 21:01:24 +0000184 if (in.get<os::Service::Tag::binder>() == nullptr) {
185 // failed to find a service. Check to see if we have any local
186 // injected Accessors for this service.
187 os::Service accessor;
188 binder::Status status = getInjectedAccessor(name, &accessor);
189 if (!status.isOk()) {
190 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
191 return status;
192 }
193 if (accessor.getTag() == os::Service::Tag::accessor &&
194 accessor.get<os::Service::Tag::accessor>() != nullptr) {
195 ALOGI("Found local injected service for %s, will attempt to create connection",
196 name.c_str());
197 // Call this again using the accessor Service to get the real
198 // service's binder into _out
199 return toBinderService(name, accessor, _out);
200 }
201 }
202
Alice Wang8578f132024-05-03 09:01:56 +0000203 *_out = in;
Devin Moore18f63752024-08-08 21:01:24 +0000204 return binder::Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000205 }
206 case os::Service::Tag::accessor: {
207 sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>();
208 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
209 if (accessor == nullptr) {
210 ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
211 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
Devin Moore18f63752024-08-08 21:01:24 +0000212 return binder::Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000213 }
214 auto request = [=] {
215 os::ParcelFileDescriptor fd;
216 binder::Status ret = accessor->addConnection(&fd);
217 if (ret.isOk()) {
218 return base::unique_fd(fd.release());
219 } else {
220 ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str());
221 return base::unique_fd(-1);
222 }
223 };
224 auto session = RpcSession::make();
Devin Moore18f63752024-08-08 21:01:24 +0000225 status_t status = session->setupPreconnectedClient(base::unique_fd{}, request);
226 if (status != OK) {
227 ALOGE("Failed to set up preconnected binder RPC client: %s",
228 statusToString(status).c_str());
229 return binder::Status::fromStatusT(status);
230 }
Alice Wang8578f132024-05-03 09:01:56 +0000231 session->setSessionSpecificRoot(accessorBinder);
232 *_out = os::Service::make<os::Service::Tag::binder>(session->getRootObject());
Devin Moore18f63752024-08-08 21:01:24 +0000233 return binder::Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000234 }
235 default: {
236 LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag());
237 }
238 }
239}
240
Parth Sane56a04712024-04-22 14:21:07 +0000241binder::Status BackendUnifiedServiceManager::addService(const ::std::string& name,
242 const sp<IBinder>& service,
243 bool allowIsolated, int32_t dumpPriority) {
244 return mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
245}
246binder::Status BackendUnifiedServiceManager::listServices(
247 int32_t dumpPriority, ::std::vector<::std::string>* _aidl_return) {
248 return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
249}
250binder::Status BackendUnifiedServiceManager::registerForNotifications(
251 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
252 return mTheRealServiceManager->registerForNotifications(name, callback);
253}
254binder::Status BackendUnifiedServiceManager::unregisterForNotifications(
255 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
256 return mTheRealServiceManager->unregisterForNotifications(name, callback);
257}
258binder::Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name,
259 bool* _aidl_return) {
260 return mTheRealServiceManager->isDeclared(name, _aidl_return);
261}
262binder::Status BackendUnifiedServiceManager::getDeclaredInstances(
263 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
264 return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
265}
266binder::Status BackendUnifiedServiceManager::updatableViaApex(
267 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
268 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
269}
270binder::Status BackendUnifiedServiceManager::getUpdatableNames(
271 const ::std::string& apexName, ::std::vector<::std::string>* _aidl_return) {
272 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
273}
274binder::Status BackendUnifiedServiceManager::getConnectionInfo(
275 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
276 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
277}
278binder::Status BackendUnifiedServiceManager::registerClientCallback(
279 const ::std::string& name, const sp<IBinder>& service,
280 const sp<os::IClientCallback>& callback) {
281 return mTheRealServiceManager->registerClientCallback(name, service, callback);
282}
283binder::Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
284 const sp<IBinder>& service) {
285 return mTheRealServiceManager->tryUnregisterService(name, service);
286}
287binder::Status BackendUnifiedServiceManager::getServiceDebugInfo(
288 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
289 return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
290}
291
292[[clang::no_destroy]] static std::once_flag gUSmOnce;
293[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
294
295sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
296 std::call_once(gUSmOnce, []() {
297#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
298 /* wait for service manager */ {
299 using std::literals::chrono_literals::operator""s;
300 using android::base::WaitForProperty;
301 while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
302 ALOGE("Waited for servicemanager.ready for a second, waiting another...");
303 }
304 }
305#endif
306
307 sp<AidlServiceManager> sm = nullptr;
308 while (sm == nullptr) {
309 sm = interface_cast<AidlServiceManager>(
310 ProcessState::self()->getContextObject(nullptr));
311 if (sm == nullptr) {
312 ALOGE("Waiting 1s on context object on %s.",
313 ProcessState::self()->getDriverName().c_str());
314 sleep(1);
315 }
316 }
317
318 gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm);
319 });
320
321 return gUnifiedServiceManager;
322}
323
Devin Moore18f63752024-08-08 21:01:24 +0000324} // namespace android