blob: dfc084a66190349bfd8443e5fb2b9a1bff2b4354 [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;
Devin Moore678984f2024-10-18 22:43:22 +000034using android::os::IAccessor;
35using binder::Status;
Parth Sane56a04712024-04-22 14:21:07 +000036
Parth Saneb6ed0eb2024-06-25 14:38:42 +000037static const char* kStaticCachableList[] = {
Parth Saneac492702024-09-18 15:54:16 +000038 // go/keep-sorted start
39 "accessibility",
40 "account",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000041 "activity",
Parth Saneac492702024-09-18 15:54:16 +000042 "alarm",
Parth Sane0105ea52024-10-11 11:41:16 +000043 "android.frameworks.stats.IStats/default",
Parth Saneac492702024-09-18 15:54:16 +000044 "android.system.keystore2.IKeystoreService/default",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000045 "appops",
46 "audio",
Parth Sane0105ea52024-10-11 11:41:16 +000047 "autofill",
48 "batteryproperties",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000049 "batterystats",
Parth Sane0105ea52024-10-11 11:41:16 +000050 "biometic",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000051 "carrier_config",
52 "connectivity",
Parth Saneac492702024-09-18 15:54:16 +000053 "content",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000054 "content_capture",
55 "device_policy",
56 "display",
57 "dropbox",
58 "econtroller",
Parth Saneac492702024-09-18 15:54:16 +000059 "graphicsstats",
60 "input",
61 "input_method",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000062 "isub",
Parth Saneac492702024-09-18 15:54:16 +000063 "jobscheduler",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000064 "legacy_permission",
65 "location",
Parth Sane0105ea52024-10-11 11:41:16 +000066 "lock_settings",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000067 "media.extractor",
68 "media.metrics",
69 "media.player",
70 "media.resource_manager",
Parth Saneac492702024-09-18 15:54:16 +000071 "media_resource_monitor",
72 "mount",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000073 "netd_listener",
74 "netstats",
75 "network_management",
76 "nfc",
Parth Saneac492702024-09-18 15:54:16 +000077 "notification",
78 "package",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000079 "package_native",
80 "performance_hint",
81 "permission",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000082 "permission_checker",
Parth Saneac492702024-09-18 15:54:16 +000083 "permissionmgr",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000084 "phone",
85 "platform_compat",
86 "power",
Parth Sane0105ea52024-10-11 11:41:16 +000087 "processinfo",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000088 "role",
Parth Sane0105ea52024-10-11 11:41:16 +000089 "sensitive_content_protection_service",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000090 "sensorservice",
91 "statscompanion",
92 "telephony.registry",
93 "thermalservice",
94 "time_detector",
Parth Sane0105ea52024-10-11 11:41:16 +000095 "tracing.proxy",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000096 "trust",
97 "uimode",
Parth Saneac492702024-09-18 15:54:16 +000098 "user",
Parth Sane0105ea52024-10-11 11:41:16 +000099 "vibrator",
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000100 "virtualdevice",
101 "virtualdevice_native",
102 "webviewupdate",
Parth Saneac492702024-09-18 15:54:16 +0000103 "window",
104 // go/keep-sorted end
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000105};
106
107bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) {
Devin Moore74bc5892024-10-23 17:12:19 +0000108 sp<ProcessState> self = ProcessState::selfOrNull();
109 if (!self || self->getThreadPoolMaxTotalThreadCount() <= 0) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000110 ALOGW("Thread Pool max thread count is 0. Cannot cache binder as linkToDeath cannot be "
111 "implemented. serviceName: %s",
112 serviceName.c_str());
113 return false;
114 }
115 for (const char* name : kStaticCachableList) {
116 if (name == serviceName) {
117 return true;
118 }
119 }
120 return false;
121}
122
Devin Moore678984f2024-10-18 22:43:22 +0000123Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
124 const os::Service& service) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000125 if (!kUseCache) {
Devin Moore678984f2024-10-18 22:43:22 +0000126 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000127 }
Parth Sanea6676ba2024-10-04 14:14:07 +0000128 std::string traceStr;
129 if (atrace_is_tag_enabled(ATRACE_TAG_AIDL)) {
130 traceStr = "BinderCacheWithInvalidation::updateCache : " + serviceName;
131 }
132 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, traceStr.c_str());
133
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000134 if (service.getTag() == os::Service::Tag::binder) {
135 sp<IBinder> binder = service.get<os::Service::Tag::binder>();
Parth Sanea6676ba2024-10-04 14:14:07 +0000136 if (!binder) {
137 binder::ScopedTrace
138 aidlTrace(ATRACE_TAG_AIDL,
139 "BinderCacheWithInvalidation::updateCache failed: binder_null");
140 } else if (!binder->isBinderAlive()) {
141 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
142 "BinderCacheWithInvalidation::updateCache failed: "
143 "isBinderAlive_false");
144 } else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) {
145 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
146 "BinderCacheWithInvalidation::updateCache successful");
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000147 return mCacheForGetService->setItem(serviceName, binder);
Parth Sanea6676ba2024-10-04 14:14:07 +0000148 } else {
149 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
150 "BinderCacheWithInvalidation::updateCache failed: "
151 "caching_not_enabled");
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000152 }
153 }
Devin Moore678984f2024-10-18 22:43:22 +0000154 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000155}
156
157bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName,
158 os::Service* _out) {
159 if (!kUseCache) {
160 return false;
161 }
162 sp<IBinder> item = mCacheForGetService->getItem(serviceName);
163 // TODO(b/363177618): Enable caching for binders which are always null.
164 if (item != nullptr && item->isBinderAlive()) {
165 *_out = os::Service::make<os::Service::Tag::binder>(item);
166 return true;
167 }
168 return false;
169}
170
Parth Sane56a04712024-04-22 14:21:07 +0000171BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl)
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000172 : mTheRealServiceManager(impl) {
173 mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>();
174}
Parth Sane56a04712024-04-22 14:21:07 +0000175
176sp<AidlServiceManager> BackendUnifiedServiceManager::getImpl() {
177 return mTheRealServiceManager;
178}
Alice Wang11da1502024-07-25 12:03:22 +0000179
Devin Moore678984f2024-10-18 22:43:22 +0000180Status BackendUnifiedServiceManager::getService(const ::std::string& name,
181 sp<IBinder>* _aidl_return) {
Alice Wang8578f132024-05-03 09:01:56 +0000182 os::Service service;
Devin Moore678984f2024-10-18 22:43:22 +0000183 Status status = getService2(name, &service);
Alice Wang11da1502024-07-25 12:03:22 +0000184 *_aidl_return = service.get<os::Service::Tag::binder>();
185 return status;
186}
187
Devin Moore678984f2024-10-18 22:43:22 +0000188Status BackendUnifiedServiceManager::getService2(const ::std::string& name, os::Service* _out) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000189 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000190 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000191 }
Alice Wang11da1502024-07-25 12:03:22 +0000192 os::Service service;
Devin Moore678984f2024-10-18 22:43:22 +0000193 Status status = mTheRealServiceManager->getService2(name, &service);
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000194
Devin Moore18f63752024-08-08 21:01:24 +0000195 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000196 status = toBinderService(name, service, _out);
197 if (status.isOk()) {
198 return updateCache(name, service);
199 }
Devin Moore18f63752024-08-08 21:01:24 +0000200 }
Alice Wang8578f132024-05-03 09:01:56 +0000201 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000202}
Alice Wang8578f132024-05-03 09:01:56 +0000203
Devin Moore678984f2024-10-18 22:43:22 +0000204Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000205 os::Service service;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000206 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000207 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000208 }
209
Devin Moore678984f2024-10-18 22:43:22 +0000210 Status status = mTheRealServiceManager->checkService(name, &service);
Devin Moore18f63752024-08-08 21:01:24 +0000211 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000212 status = toBinderService(name, service, _out);
213 if (status.isOk()) {
214 return updateCache(name, service);
215 }
Devin Moore18f63752024-08-08 21:01:24 +0000216 }
Alice Wang8578f132024-05-03 09:01:56 +0000217 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000218}
Alice Wang8578f132024-05-03 09:01:56 +0000219
Devin Moore678984f2024-10-18 22:43:22 +0000220Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
221 const os::Service& in, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000222 switch (in.getTag()) {
223 case os::Service::Tag::binder: {
Devin Moore18f63752024-08-08 21:01:24 +0000224 if (in.get<os::Service::Tag::binder>() == nullptr) {
225 // failed to find a service. Check to see if we have any local
226 // injected Accessors for this service.
227 os::Service accessor;
Devin Moore678984f2024-10-18 22:43:22 +0000228 Status status = getInjectedAccessor(name, &accessor);
Devin Moore18f63752024-08-08 21:01:24 +0000229 if (!status.isOk()) {
230 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
231 return status;
232 }
233 if (accessor.getTag() == os::Service::Tag::accessor &&
234 accessor.get<os::Service::Tag::accessor>() != nullptr) {
235 ALOGI("Found local injected service for %s, will attempt to create connection",
236 name.c_str());
237 // Call this again using the accessor Service to get the real
238 // service's binder into _out
239 return toBinderService(name, accessor, _out);
240 }
241 }
242
Alice Wang8578f132024-05-03 09:01:56 +0000243 *_out = in;
Devin Moore678984f2024-10-18 22:43:22 +0000244 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000245 }
246 case os::Service::Tag::accessor: {
247 sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>();
248 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
249 if (accessor == nullptr) {
250 ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
251 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
Devin Moore678984f2024-10-18 22:43:22 +0000252 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000253 }
254 auto request = [=] {
255 os::ParcelFileDescriptor fd;
Devin Moore678984f2024-10-18 22:43:22 +0000256 Status ret = accessor->addConnection(&fd);
Alice Wang8578f132024-05-03 09:01:56 +0000257 if (ret.isOk()) {
258 return base::unique_fd(fd.release());
259 } else {
260 ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str());
261 return base::unique_fd(-1);
262 }
263 };
264 auto session = RpcSession::make();
Devin Moore18f63752024-08-08 21:01:24 +0000265 status_t status = session->setupPreconnectedClient(base::unique_fd{}, request);
266 if (status != OK) {
267 ALOGE("Failed to set up preconnected binder RPC client: %s",
268 statusToString(status).c_str());
Devin Moore678984f2024-10-18 22:43:22 +0000269 return Status::fromStatusT(status);
Devin Moore18f63752024-08-08 21:01:24 +0000270 }
Alice Wang8578f132024-05-03 09:01:56 +0000271 session->setSessionSpecificRoot(accessorBinder);
272 *_out = os::Service::make<os::Service::Tag::binder>(session->getRootObject());
Devin Moore678984f2024-10-18 22:43:22 +0000273 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000274 }
275 default: {
276 LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag());
277 }
278 }
279}
280
Devin Moore678984f2024-10-18 22:43:22 +0000281Status BackendUnifiedServiceManager::addService(const ::std::string& name,
282 const sp<IBinder>& service, bool allowIsolated,
283 int32_t dumpPriority) {
Parth Sane56a04712024-04-22 14:21:07 +0000284 return mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
285}
Devin Moore678984f2024-10-18 22:43:22 +0000286Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority,
287 ::std::vector<::std::string>* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000288 return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
289}
Devin Moore678984f2024-10-18 22:43:22 +0000290Status BackendUnifiedServiceManager::registerForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000291 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
292 return mTheRealServiceManager->registerForNotifications(name, callback);
293}
Devin Moore678984f2024-10-18 22:43:22 +0000294Status BackendUnifiedServiceManager::unregisterForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000295 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
296 return mTheRealServiceManager->unregisterForNotifications(name, callback);
297}
Devin Moore678984f2024-10-18 22:43:22 +0000298Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name, bool* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000299 return mTheRealServiceManager->isDeclared(name, _aidl_return);
300}
Devin Moore678984f2024-10-18 22:43:22 +0000301Status BackendUnifiedServiceManager::getDeclaredInstances(
Parth Sane56a04712024-04-22 14:21:07 +0000302 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
303 return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
304}
Devin Moore678984f2024-10-18 22:43:22 +0000305Status BackendUnifiedServiceManager::updatableViaApex(
Parth Sane56a04712024-04-22 14:21:07 +0000306 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
307 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
308}
Devin Moore678984f2024-10-18 22:43:22 +0000309Status BackendUnifiedServiceManager::getUpdatableNames(const ::std::string& apexName,
310 ::std::vector<::std::string>* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000311 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
312}
Devin Moore678984f2024-10-18 22:43:22 +0000313Status BackendUnifiedServiceManager::getConnectionInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000314 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
315 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
316}
Devin Moore678984f2024-10-18 22:43:22 +0000317Status BackendUnifiedServiceManager::registerClientCallback(
Parth Sane56a04712024-04-22 14:21:07 +0000318 const ::std::string& name, const sp<IBinder>& service,
319 const sp<os::IClientCallback>& callback) {
320 return mTheRealServiceManager->registerClientCallback(name, service, callback);
321}
Devin Moore678984f2024-10-18 22:43:22 +0000322Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
323 const sp<IBinder>& service) {
Parth Sane56a04712024-04-22 14:21:07 +0000324 return mTheRealServiceManager->tryUnregisterService(name, service);
325}
Devin Moore678984f2024-10-18 22:43:22 +0000326Status BackendUnifiedServiceManager::getServiceDebugInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000327 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
328 return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
329}
330
331[[clang::no_destroy]] static std::once_flag gUSmOnce;
332[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
333
334sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
335 std::call_once(gUSmOnce, []() {
336#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
337 /* wait for service manager */ {
338 using std::literals::chrono_literals::operator""s;
339 using android::base::WaitForProperty;
340 while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
341 ALOGE("Waited for servicemanager.ready for a second, waiting another...");
342 }
343 }
344#endif
345
346 sp<AidlServiceManager> sm = nullptr;
347 while (sm == nullptr) {
348 sm = interface_cast<AidlServiceManager>(
349 ProcessState::self()->getContextObject(nullptr));
350 if (sm == nullptr) {
351 ALOGE("Waiting 1s on context object on %s.",
352 ProcessState::self()->getDriverName().c_str());
353 sleep(1);
354 }
355 }
356
357 gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm);
358 });
359
360 return gUnifiedServiceManager;
361}
362
Devin Moore18f63752024-08-08 21:01:24 +0000363} // namespace android