blob: d32eecdc604846710c0be9d8a4e663fa86489f68 [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
Devin Moore678984f2024-10-18 22:43:22 +0000176Status BackendUnifiedServiceManager::getService(const ::std::string& name,
177 sp<IBinder>* _aidl_return) {
Alice Wang8578f132024-05-03 09:01:56 +0000178 os::Service service;
Devin Moore678984f2024-10-18 22:43:22 +0000179 Status status = getService2(name, &service);
Alice Wang11da1502024-07-25 12:03:22 +0000180 *_aidl_return = service.get<os::Service::Tag::binder>();
181 return status;
182}
183
Devin Moore678984f2024-10-18 22:43:22 +0000184Status BackendUnifiedServiceManager::getService2(const ::std::string& name, os::Service* _out) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000185 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000186 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000187 }
Alice Wang11da1502024-07-25 12:03:22 +0000188 os::Service service;
Devin Moore678984f2024-10-18 22:43:22 +0000189 Status status = mTheRealServiceManager->getService2(name, &service);
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000190
Devin Moore18f63752024-08-08 21:01:24 +0000191 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000192 status = toBinderService(name, service, _out);
193 if (status.isOk()) {
194 return updateCache(name, service);
195 }
Devin Moore18f63752024-08-08 21:01:24 +0000196 }
Alice Wang8578f132024-05-03 09:01:56 +0000197 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000198}
Alice Wang8578f132024-05-03 09:01:56 +0000199
Devin Moore678984f2024-10-18 22:43:22 +0000200Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000201 os::Service service;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000202 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000203 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000204 }
205
Devin Moore678984f2024-10-18 22:43:22 +0000206 Status status = mTheRealServiceManager->checkService(name, &service);
Devin Moore18f63752024-08-08 21:01:24 +0000207 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000208 status = toBinderService(name, service, _out);
209 if (status.isOk()) {
210 return updateCache(name, service);
211 }
Devin Moore18f63752024-08-08 21:01:24 +0000212 }
Alice Wang8578f132024-05-03 09:01:56 +0000213 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000214}
Alice Wang8578f132024-05-03 09:01:56 +0000215
Devin Moore678984f2024-10-18 22:43:22 +0000216Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
217 const os::Service& in, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000218 switch (in.getTag()) {
219 case os::Service::Tag::binder: {
Devin Moore18f63752024-08-08 21:01:24 +0000220 if (in.get<os::Service::Tag::binder>() == nullptr) {
221 // failed to find a service. Check to see if we have any local
222 // injected Accessors for this service.
223 os::Service accessor;
Devin Moore678984f2024-10-18 22:43:22 +0000224 Status status = getInjectedAccessor(name, &accessor);
Devin Moore18f63752024-08-08 21:01:24 +0000225 if (!status.isOk()) {
226 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
227 return status;
228 }
229 if (accessor.getTag() == os::Service::Tag::accessor &&
230 accessor.get<os::Service::Tag::accessor>() != nullptr) {
231 ALOGI("Found local injected service for %s, will attempt to create connection",
232 name.c_str());
233 // Call this again using the accessor Service to get the real
234 // service's binder into _out
235 return toBinderService(name, accessor, _out);
236 }
237 }
238
Alice Wang8578f132024-05-03 09:01:56 +0000239 *_out = in;
Devin Moore678984f2024-10-18 22:43:22 +0000240 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000241 }
242 case os::Service::Tag::accessor: {
243 sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>();
244 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
245 if (accessor == nullptr) {
246 ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
247 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
Devin Moore678984f2024-10-18 22:43:22 +0000248 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000249 }
250 auto request = [=] {
251 os::ParcelFileDescriptor fd;
Devin Moore678984f2024-10-18 22:43:22 +0000252 Status ret = accessor->addConnection(&fd);
Alice Wang8578f132024-05-03 09:01:56 +0000253 if (ret.isOk()) {
254 return base::unique_fd(fd.release());
255 } else {
256 ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str());
257 return base::unique_fd(-1);
258 }
259 };
260 auto session = RpcSession::make();
Devin Moore18f63752024-08-08 21:01:24 +0000261 status_t status = session->setupPreconnectedClient(base::unique_fd{}, request);
262 if (status != OK) {
263 ALOGE("Failed to set up preconnected binder RPC client: %s",
264 statusToString(status).c_str());
Devin Moore678984f2024-10-18 22:43:22 +0000265 return Status::fromStatusT(status);
Devin Moore18f63752024-08-08 21:01:24 +0000266 }
Alice Wang8578f132024-05-03 09:01:56 +0000267 session->setSessionSpecificRoot(accessorBinder);
268 *_out = os::Service::make<os::Service::Tag::binder>(session->getRootObject());
Devin Moore678984f2024-10-18 22:43:22 +0000269 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000270 }
271 default: {
272 LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag());
273 }
274 }
275}
276
Devin Moore678984f2024-10-18 22:43:22 +0000277Status BackendUnifiedServiceManager::addService(const ::std::string& name,
278 const sp<IBinder>& service, bool allowIsolated,
279 int32_t dumpPriority) {
Parth Sane56a04712024-04-22 14:21:07 +0000280 return mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
281}
Devin Moore678984f2024-10-18 22:43:22 +0000282Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority,
283 ::std::vector<::std::string>* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000284 return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
285}
Devin Moore678984f2024-10-18 22:43:22 +0000286Status BackendUnifiedServiceManager::registerForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000287 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
288 return mTheRealServiceManager->registerForNotifications(name, callback);
289}
Devin Moore678984f2024-10-18 22:43:22 +0000290Status BackendUnifiedServiceManager::unregisterForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000291 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
292 return mTheRealServiceManager->unregisterForNotifications(name, callback);
293}
Devin Moore678984f2024-10-18 22:43:22 +0000294Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name, bool* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000295 return mTheRealServiceManager->isDeclared(name, _aidl_return);
296}
Devin Moore678984f2024-10-18 22:43:22 +0000297Status BackendUnifiedServiceManager::getDeclaredInstances(
Parth Sane56a04712024-04-22 14:21:07 +0000298 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
299 return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
300}
Devin Moore678984f2024-10-18 22:43:22 +0000301Status BackendUnifiedServiceManager::updatableViaApex(
Parth Sane56a04712024-04-22 14:21:07 +0000302 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
303 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
304}
Devin Moore678984f2024-10-18 22:43:22 +0000305Status BackendUnifiedServiceManager::getUpdatableNames(const ::std::string& apexName,
306 ::std::vector<::std::string>* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000307 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
308}
Devin Moore678984f2024-10-18 22:43:22 +0000309Status BackendUnifiedServiceManager::getConnectionInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000310 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
311 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
312}
Devin Moore678984f2024-10-18 22:43:22 +0000313Status BackendUnifiedServiceManager::registerClientCallback(
Parth Sane56a04712024-04-22 14:21:07 +0000314 const ::std::string& name, const sp<IBinder>& service,
315 const sp<os::IClientCallback>& callback) {
316 return mTheRealServiceManager->registerClientCallback(name, service, callback);
317}
Devin Moore678984f2024-10-18 22:43:22 +0000318Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
319 const sp<IBinder>& service) {
Parth Sane56a04712024-04-22 14:21:07 +0000320 return mTheRealServiceManager->tryUnregisterService(name, service);
321}
Devin Moore678984f2024-10-18 22:43:22 +0000322Status BackendUnifiedServiceManager::getServiceDebugInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000323 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
324 return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
325}
326
327[[clang::no_destroy]] static std::once_flag gUSmOnce;
328[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
329
330sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
331 std::call_once(gUSmOnce, []() {
332#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
333 /* wait for service manager */ {
334 using std::literals::chrono_literals::operator""s;
335 using android::base::WaitForProperty;
336 while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
337 ALOGE("Waited for servicemanager.ready for a second, waiting another...");
338 }
339 }
340#endif
341
342 sp<AidlServiceManager> sm = nullptr;
343 while (sm == nullptr) {
344 sm = interface_cast<AidlServiceManager>(
345 ProcessState::self()->getContextObject(nullptr));
346 if (sm == nullptr) {
347 ALOGE("Waiting 1s on context object on %s.",
348 ProcessState::self()->getDriverName().c_str());
349 sleep(1);
350 }
351 }
352
353 gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm);
354 });
355
356 return gUnifiedServiceManager;
357}
358
Devin Moore18f63752024-08-08 21:01:24 +0000359} // namespace android