blob: 123dfd2c0508440329583f6056d835a2361cd37a [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 Sanedc207542024-11-14 11:49:08 +000033#ifdef LIBBINDER_ADDSERVICE_CACHE
34constexpr bool kUseCacheInAddService = true;
35#else
36constexpr bool kUseCacheInAddService = false;
37#endif
38
Parth Sane56a04712024-04-22 14:21:07 +000039using AidlServiceManager = android::os::IServiceManager;
Devin Moore678984f2024-10-18 22:43:22 +000040using android::os::IAccessor;
41using binder::Status;
Parth Sane56a04712024-04-22 14:21:07 +000042
Parth Saneb6ed0eb2024-06-25 14:38:42 +000043static const char* kStaticCachableList[] = {
Parth Saneac492702024-09-18 15:54:16 +000044 // go/keep-sorted start
45 "accessibility",
46 "account",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000047 "activity",
Parth Saneac492702024-09-18 15:54:16 +000048 "alarm",
Parth Sane0105ea52024-10-11 11:41:16 +000049 "android.frameworks.stats.IStats/default",
Parth Saneac492702024-09-18 15:54:16 +000050 "android.system.keystore2.IKeystoreService/default",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000051 "appops",
52 "audio",
Parth Sane0105ea52024-10-11 11:41:16 +000053 "autofill",
54 "batteryproperties",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000055 "batterystats",
Parth Sane0105ea52024-10-11 11:41:16 +000056 "biometic",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000057 "carrier_config",
58 "connectivity",
Parth Saneac492702024-09-18 15:54:16 +000059 "content",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000060 "content_capture",
61 "device_policy",
62 "display",
63 "dropbox",
64 "econtroller",
Parth Saneac492702024-09-18 15:54:16 +000065 "graphicsstats",
66 "input",
67 "input_method",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000068 "isub",
Parth Saneac492702024-09-18 15:54:16 +000069 "jobscheduler",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000070 "legacy_permission",
71 "location",
Parth Sane0105ea52024-10-11 11:41:16 +000072 "lock_settings",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000073 "media.extractor",
74 "media.metrics",
75 "media.player",
76 "media.resource_manager",
Parth Saneac492702024-09-18 15:54:16 +000077 "media_resource_monitor",
78 "mount",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000079 "netd_listener",
80 "netstats",
81 "network_management",
82 "nfc",
Parth Saneac492702024-09-18 15:54:16 +000083 "notification",
84 "package",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000085 "package_native",
86 "performance_hint",
87 "permission",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000088 "permission_checker",
Parth Saneac492702024-09-18 15:54:16 +000089 "permissionmgr",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000090 "phone",
91 "platform_compat",
92 "power",
Parth Sane0105ea52024-10-11 11:41:16 +000093 "processinfo",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000094 "role",
Parth Sane0105ea52024-10-11 11:41:16 +000095 "sensitive_content_protection_service",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000096 "sensorservice",
97 "statscompanion",
98 "telephony.registry",
99 "thermalservice",
100 "time_detector",
Parth Sane0105ea52024-10-11 11:41:16 +0000101 "tracing.proxy",
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000102 "trust",
103 "uimode",
Parth Saneac492702024-09-18 15:54:16 +0000104 "user",
Parth Sane0105ea52024-10-11 11:41:16 +0000105 "vibrator",
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000106 "virtualdevice",
107 "virtualdevice_native",
108 "webviewupdate",
Parth Saneac492702024-09-18 15:54:16 +0000109 "window",
110 // go/keep-sorted end
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000111};
112
113bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) {
Devin Moore74bc5892024-10-23 17:12:19 +0000114 sp<ProcessState> self = ProcessState::selfOrNull();
115 if (!self || self->getThreadPoolMaxTotalThreadCount() <= 0) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000116 ALOGW("Thread Pool max thread count is 0. Cannot cache binder as linkToDeath cannot be "
117 "implemented. serviceName: %s",
118 serviceName.c_str());
119 return false;
120 }
121 for (const char* name : kStaticCachableList) {
122 if (name == serviceName) {
123 return true;
124 }
125 }
126 return false;
127}
128
Devin Moore678984f2024-10-18 22:43:22 +0000129Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
130 const os::Service& service) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000131 if (!kUseCache) {
Devin Moore678984f2024-10-18 22:43:22 +0000132 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000133 }
Parth Sanedc207542024-11-14 11:49:08 +0000134
135 if (service.getTag() == os::Service::Tag::binder) {
136 return updateCache(serviceName, service.get<os::Service::Tag::binder>());
137 }
138 return Status::ok();
139}
140
141Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
142 const sp<IBinder>& binder) {
Parth Sanea6676ba2024-10-04 14:14:07 +0000143 std::string traceStr;
144 if (atrace_is_tag_enabled(ATRACE_TAG_AIDL)) {
145 traceStr = "BinderCacheWithInvalidation::updateCache : " + serviceName;
146 }
147 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, traceStr.c_str());
Parth Sanedc207542024-11-14 11:49:08 +0000148 if (!binder) {
149 binder::ScopedTrace
150 aidlTrace(ATRACE_TAG_AIDL,
151 "BinderCacheWithInvalidation::updateCache failed: binder_null");
152 } else if (!binder->isBinderAlive()) {
153 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
154 "BinderCacheWithInvalidation::updateCache failed: "
155 "isBinderAlive_false");
156 } else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) {
157 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
158 "BinderCacheWithInvalidation::updateCache successful");
159 return mCacheForGetService->setItem(serviceName, binder);
160 } else {
161 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
162 "BinderCacheWithInvalidation::updateCache failed: "
163 "caching_not_enabled");
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000164 }
Devin Moore678984f2024-10-18 22:43:22 +0000165 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000166}
167
168bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName,
169 os::Service* _out) {
170 if (!kUseCache) {
171 return false;
172 }
173 sp<IBinder> item = mCacheForGetService->getItem(serviceName);
174 // TODO(b/363177618): Enable caching for binders which are always null.
175 if (item != nullptr && item->isBinderAlive()) {
176 *_out = os::Service::make<os::Service::Tag::binder>(item);
177 return true;
178 }
179 return false;
180}
181
Parth Sane56a04712024-04-22 14:21:07 +0000182BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl)
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000183 : mTheRealServiceManager(impl) {
184 mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>();
185}
Parth Sane56a04712024-04-22 14:21:07 +0000186
Devin Moore678984f2024-10-18 22:43:22 +0000187Status BackendUnifiedServiceManager::getService(const ::std::string& name,
188 sp<IBinder>* _aidl_return) {
Alice Wang8578f132024-05-03 09:01:56 +0000189 os::Service service;
Devin Moore678984f2024-10-18 22:43:22 +0000190 Status status = getService2(name, &service);
Alice Wang11da1502024-07-25 12:03:22 +0000191 *_aidl_return = service.get<os::Service::Tag::binder>();
192 return status;
193}
194
Devin Moore678984f2024-10-18 22:43:22 +0000195Status BackendUnifiedServiceManager::getService2(const ::std::string& name, os::Service* _out) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000196 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000197 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000198 }
Alice Wang11da1502024-07-25 12:03:22 +0000199 os::Service service;
Devin Moore678984f2024-10-18 22:43:22 +0000200 Status status = mTheRealServiceManager->getService2(name, &service);
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000201
Devin Moore18f63752024-08-08 21:01:24 +0000202 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000203 status = toBinderService(name, service, _out);
204 if (status.isOk()) {
205 return updateCache(name, service);
206 }
Devin Moore18f63752024-08-08 21:01:24 +0000207 }
Alice Wang8578f132024-05-03 09:01:56 +0000208 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000209}
Alice Wang8578f132024-05-03 09:01:56 +0000210
Devin Moore678984f2024-10-18 22:43:22 +0000211Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000212 os::Service service;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000213 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000214 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000215 }
216
Devin Moore678984f2024-10-18 22:43:22 +0000217 Status status = mTheRealServiceManager->checkService(name, &service);
Devin Moore18f63752024-08-08 21:01:24 +0000218 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000219 status = toBinderService(name, service, _out);
220 if (status.isOk()) {
221 return updateCache(name, service);
222 }
Devin Moore18f63752024-08-08 21:01:24 +0000223 }
Alice Wang8578f132024-05-03 09:01:56 +0000224 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000225}
Alice Wang8578f132024-05-03 09:01:56 +0000226
Devin Moore678984f2024-10-18 22:43:22 +0000227Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
228 const os::Service& in, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000229 switch (in.getTag()) {
230 case os::Service::Tag::binder: {
Devin Moore18f63752024-08-08 21:01:24 +0000231 if (in.get<os::Service::Tag::binder>() == nullptr) {
232 // failed to find a service. Check to see if we have any local
233 // injected Accessors for this service.
234 os::Service accessor;
Devin Moore678984f2024-10-18 22:43:22 +0000235 Status status = getInjectedAccessor(name, &accessor);
Devin Moore18f63752024-08-08 21:01:24 +0000236 if (!status.isOk()) {
237 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
238 return status;
239 }
240 if (accessor.getTag() == os::Service::Tag::accessor &&
241 accessor.get<os::Service::Tag::accessor>() != nullptr) {
242 ALOGI("Found local injected service for %s, will attempt to create connection",
243 name.c_str());
244 // Call this again using the accessor Service to get the real
245 // service's binder into _out
246 return toBinderService(name, accessor, _out);
247 }
248 }
249
Alice Wang8578f132024-05-03 09:01:56 +0000250 *_out = in;
Devin Moore678984f2024-10-18 22:43:22 +0000251 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000252 }
253 case os::Service::Tag::accessor: {
254 sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>();
255 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
256 if (accessor == nullptr) {
257 ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
258 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
Devin Moore678984f2024-10-18 22:43:22 +0000259 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000260 }
261 auto request = [=] {
262 os::ParcelFileDescriptor fd;
Devin Moore678984f2024-10-18 22:43:22 +0000263 Status ret = accessor->addConnection(&fd);
Alice Wang8578f132024-05-03 09:01:56 +0000264 if (ret.isOk()) {
265 return base::unique_fd(fd.release());
266 } else {
267 ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str());
268 return base::unique_fd(-1);
269 }
270 };
271 auto session = RpcSession::make();
Devin Moore18f63752024-08-08 21:01:24 +0000272 status_t status = session->setupPreconnectedClient(base::unique_fd{}, request);
273 if (status != OK) {
274 ALOGE("Failed to set up preconnected binder RPC client: %s",
275 statusToString(status).c_str());
Devin Moore678984f2024-10-18 22:43:22 +0000276 return Status::fromStatusT(status);
Devin Moore18f63752024-08-08 21:01:24 +0000277 }
Alice Wang8578f132024-05-03 09:01:56 +0000278 session->setSessionSpecificRoot(accessorBinder);
279 *_out = os::Service::make<os::Service::Tag::binder>(session->getRootObject());
Devin Moore678984f2024-10-18 22:43:22 +0000280 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000281 }
282 default: {
283 LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag());
284 }
285 }
286}
287
Devin Moore678984f2024-10-18 22:43:22 +0000288Status BackendUnifiedServiceManager::addService(const ::std::string& name,
289 const sp<IBinder>& service, bool allowIsolated,
290 int32_t dumpPriority) {
Parth Sanedc207542024-11-14 11:49:08 +0000291 Status status = mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
292 // mEnableAddServiceCache is true by default.
293 if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) {
294 return updateCache(name, service);
295 }
296 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000297}
Devin Moore678984f2024-10-18 22:43:22 +0000298Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority,
299 ::std::vector<::std::string>* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000300 return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
301}
Devin Moore678984f2024-10-18 22:43:22 +0000302Status BackendUnifiedServiceManager::registerForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000303 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
304 return mTheRealServiceManager->registerForNotifications(name, callback);
305}
Devin Moore678984f2024-10-18 22:43:22 +0000306Status BackendUnifiedServiceManager::unregisterForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000307 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
308 return mTheRealServiceManager->unregisterForNotifications(name, callback);
309}
Devin Moore678984f2024-10-18 22:43:22 +0000310Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name, bool* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000311 return mTheRealServiceManager->isDeclared(name, _aidl_return);
312}
Devin Moore678984f2024-10-18 22:43:22 +0000313Status BackendUnifiedServiceManager::getDeclaredInstances(
Parth Sane56a04712024-04-22 14:21:07 +0000314 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
315 return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
316}
Devin Moore678984f2024-10-18 22:43:22 +0000317Status BackendUnifiedServiceManager::updatableViaApex(
Parth Sane56a04712024-04-22 14:21:07 +0000318 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
319 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
320}
Devin Moore678984f2024-10-18 22:43:22 +0000321Status BackendUnifiedServiceManager::getUpdatableNames(const ::std::string& apexName,
322 ::std::vector<::std::string>* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000323 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
324}
Devin Moore678984f2024-10-18 22:43:22 +0000325Status BackendUnifiedServiceManager::getConnectionInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000326 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
327 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
328}
Devin Moore678984f2024-10-18 22:43:22 +0000329Status BackendUnifiedServiceManager::registerClientCallback(
Parth Sane56a04712024-04-22 14:21:07 +0000330 const ::std::string& name, const sp<IBinder>& service,
331 const sp<os::IClientCallback>& callback) {
332 return mTheRealServiceManager->registerClientCallback(name, service, callback);
333}
Devin Moore678984f2024-10-18 22:43:22 +0000334Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
335 const sp<IBinder>& service) {
Parth Sane56a04712024-04-22 14:21:07 +0000336 return mTheRealServiceManager->tryUnregisterService(name, service);
337}
Devin Moore678984f2024-10-18 22:43:22 +0000338Status BackendUnifiedServiceManager::getServiceDebugInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000339 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
340 return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
341}
342
343[[clang::no_destroy]] static std::once_flag gUSmOnce;
344[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
345
346sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
347 std::call_once(gUSmOnce, []() {
348#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
349 /* wait for service manager */ {
350 using std::literals::chrono_literals::operator""s;
351 using android::base::WaitForProperty;
352 while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
353 ALOGE("Waited for servicemanager.ready for a second, waiting another...");
354 }
355 }
356#endif
357
358 sp<AidlServiceManager> sm = nullptr;
359 while (sm == nullptr) {
360 sm = interface_cast<AidlServiceManager>(
361 ProcessState::self()->getContextObject(nullptr));
362 if (sm == nullptr) {
363 ALOGE("Waiting 1s on context object on %s.",
364 ProcessState::self()->getDriverName().c_str());
365 sleep(1);
366 }
367 }
368
369 gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm);
370 });
371
372 return gUnifiedServiceManager;
373}
374
Devin Moore18f63752024-08-08 21:01:24 +0000375} // namespace android