blob: f7b9f059a5abbfe98e4731252ab4618638b3c1e5 [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) {
108 if (ProcessState::self()->getThreadPoolMaxTotalThreadCount() <= 0) {
109 ALOGW("Thread Pool max thread count is 0. Cannot cache binder as linkToDeath cannot be "
110 "implemented. serviceName: %s",
111 serviceName.c_str());
112 return false;
113 }
114 for (const char* name : kStaticCachableList) {
115 if (name == serviceName) {
116 return true;
117 }
118 }
119 return false;
120}
121
Devin Moore678984f2024-10-18 22:43:22 +0000122Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
123 const os::Service& service) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000124 if (!kUseCache) {
Devin Moore678984f2024-10-18 22:43:22 +0000125 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000126 }
Parth Sanea6676ba2024-10-04 14:14:07 +0000127 std::string traceStr;
128 if (atrace_is_tag_enabled(ATRACE_TAG_AIDL)) {
129 traceStr = "BinderCacheWithInvalidation::updateCache : " + serviceName;
130 }
131 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, traceStr.c_str());
132
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000133 if (service.getTag() == os::Service::Tag::binder) {
134 sp<IBinder> binder = service.get<os::Service::Tag::binder>();
Parth Sanea6676ba2024-10-04 14:14:07 +0000135 if (!binder) {
136 binder::ScopedTrace
137 aidlTrace(ATRACE_TAG_AIDL,
138 "BinderCacheWithInvalidation::updateCache failed: binder_null");
139 } else if (!binder->isBinderAlive()) {
140 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
141 "BinderCacheWithInvalidation::updateCache failed: "
142 "isBinderAlive_false");
143 } else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) {
144 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
145 "BinderCacheWithInvalidation::updateCache successful");
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000146 return mCacheForGetService->setItem(serviceName, binder);
Parth Sanea6676ba2024-10-04 14:14:07 +0000147 } else {
148 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
149 "BinderCacheWithInvalidation::updateCache failed: "
150 "caching_not_enabled");
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000151 }
152 }
Devin Moore678984f2024-10-18 22:43:22 +0000153 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000154}
155
156bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName,
157 os::Service* _out) {
158 if (!kUseCache) {
159 return false;
160 }
161 sp<IBinder> item = mCacheForGetService->getItem(serviceName);
162 // TODO(b/363177618): Enable caching for binders which are always null.
163 if (item != nullptr && item->isBinderAlive()) {
164 *_out = os::Service::make<os::Service::Tag::binder>(item);
165 return true;
166 }
167 return false;
168}
169
Parth Sane56a04712024-04-22 14:21:07 +0000170BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl)
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000171 : mTheRealServiceManager(impl) {
172 mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>();
173}
Parth Sane56a04712024-04-22 14:21:07 +0000174
175sp<AidlServiceManager> BackendUnifiedServiceManager::getImpl() {
176 return mTheRealServiceManager;
177}
Alice Wang11da1502024-07-25 12:03:22 +0000178
Devin Moore678984f2024-10-18 22:43:22 +0000179Status BackendUnifiedServiceManager::getService(const ::std::string& name,
180 sp<IBinder>* _aidl_return) {
Alice Wang8578f132024-05-03 09:01:56 +0000181 os::Service service;
Devin Moore678984f2024-10-18 22:43:22 +0000182 Status status = getService2(name, &service);
Alice Wang11da1502024-07-25 12:03:22 +0000183 *_aidl_return = service.get<os::Service::Tag::binder>();
184 return status;
185}
186
Devin Moore678984f2024-10-18 22:43:22 +0000187Status BackendUnifiedServiceManager::getService2(const ::std::string& name, os::Service* _out) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000188 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000189 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000190 }
Alice Wang11da1502024-07-25 12:03:22 +0000191 os::Service service;
Devin Moore678984f2024-10-18 22:43:22 +0000192 Status status = mTheRealServiceManager->getService2(name, &service);
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000193
Devin Moore18f63752024-08-08 21:01:24 +0000194 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000195 status = toBinderService(name, service, _out);
196 if (status.isOk()) {
197 return updateCache(name, service);
198 }
Devin Moore18f63752024-08-08 21:01:24 +0000199 }
Alice Wang8578f132024-05-03 09:01:56 +0000200 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000201}
Alice Wang8578f132024-05-03 09:01:56 +0000202
Devin Moore678984f2024-10-18 22:43:22 +0000203Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000204 os::Service service;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000205 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000206 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000207 }
208
Devin Moore678984f2024-10-18 22:43:22 +0000209 Status status = mTheRealServiceManager->checkService(name, &service);
Devin Moore18f63752024-08-08 21:01:24 +0000210 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000211 status = toBinderService(name, service, _out);
212 if (status.isOk()) {
213 return updateCache(name, service);
214 }
Devin Moore18f63752024-08-08 21:01:24 +0000215 }
Alice Wang8578f132024-05-03 09:01:56 +0000216 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000217}
Alice Wang8578f132024-05-03 09:01:56 +0000218
Devin Moore678984f2024-10-18 22:43:22 +0000219Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
220 const os::Service& in, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000221 switch (in.getTag()) {
222 case os::Service::Tag::binder: {
Devin Moore18f63752024-08-08 21:01:24 +0000223 if (in.get<os::Service::Tag::binder>() == nullptr) {
224 // failed to find a service. Check to see if we have any local
225 // injected Accessors for this service.
226 os::Service accessor;
Devin Moore678984f2024-10-18 22:43:22 +0000227 Status status = getInjectedAccessor(name, &accessor);
Devin Moore18f63752024-08-08 21:01:24 +0000228 if (!status.isOk()) {
229 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
230 return status;
231 }
232 if (accessor.getTag() == os::Service::Tag::accessor &&
233 accessor.get<os::Service::Tag::accessor>() != nullptr) {
234 ALOGI("Found local injected service for %s, will attempt to create connection",
235 name.c_str());
236 // Call this again using the accessor Service to get the real
237 // service's binder into _out
238 return toBinderService(name, accessor, _out);
239 }
240 }
241
Alice Wang8578f132024-05-03 09:01:56 +0000242 *_out = in;
Devin Moore678984f2024-10-18 22:43:22 +0000243 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000244 }
245 case os::Service::Tag::accessor: {
246 sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>();
247 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
248 if (accessor == nullptr) {
249 ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
250 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
Devin Moore678984f2024-10-18 22:43:22 +0000251 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000252 }
253 auto request = [=] {
254 os::ParcelFileDescriptor fd;
Devin Moore678984f2024-10-18 22:43:22 +0000255 Status ret = accessor->addConnection(&fd);
Alice Wang8578f132024-05-03 09:01:56 +0000256 if (ret.isOk()) {
257 return base::unique_fd(fd.release());
258 } else {
259 ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str());
260 return base::unique_fd(-1);
261 }
262 };
263 auto session = RpcSession::make();
Devin Moore18f63752024-08-08 21:01:24 +0000264 status_t status = session->setupPreconnectedClient(base::unique_fd{}, request);
265 if (status != OK) {
266 ALOGE("Failed to set up preconnected binder RPC client: %s",
267 statusToString(status).c_str());
Devin Moore678984f2024-10-18 22:43:22 +0000268 return Status::fromStatusT(status);
Devin Moore18f63752024-08-08 21:01:24 +0000269 }
Alice Wang8578f132024-05-03 09:01:56 +0000270 session->setSessionSpecificRoot(accessorBinder);
271 *_out = os::Service::make<os::Service::Tag::binder>(session->getRootObject());
Devin Moore678984f2024-10-18 22:43:22 +0000272 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000273 }
274 default: {
275 LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag());
276 }
277 }
278}
279
Devin Moore678984f2024-10-18 22:43:22 +0000280Status BackendUnifiedServiceManager::addService(const ::std::string& name,
281 const sp<IBinder>& service, bool allowIsolated,
282 int32_t dumpPriority) {
Parth Sane56a04712024-04-22 14:21:07 +0000283 return mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
284}
Devin Moore678984f2024-10-18 22:43:22 +0000285Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority,
286 ::std::vector<::std::string>* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000287 return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
288}
Devin Moore678984f2024-10-18 22:43:22 +0000289Status BackendUnifiedServiceManager::registerForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000290 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
291 return mTheRealServiceManager->registerForNotifications(name, callback);
292}
Devin Moore678984f2024-10-18 22:43:22 +0000293Status BackendUnifiedServiceManager::unregisterForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000294 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
295 return mTheRealServiceManager->unregisterForNotifications(name, callback);
296}
Devin Moore678984f2024-10-18 22:43:22 +0000297Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name, bool* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000298 return mTheRealServiceManager->isDeclared(name, _aidl_return);
299}
Devin Moore678984f2024-10-18 22:43:22 +0000300Status BackendUnifiedServiceManager::getDeclaredInstances(
Parth Sane56a04712024-04-22 14:21:07 +0000301 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
302 return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
303}
Devin Moore678984f2024-10-18 22:43:22 +0000304Status BackendUnifiedServiceManager::updatableViaApex(
Parth Sane56a04712024-04-22 14:21:07 +0000305 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
306 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
307}
Devin Moore678984f2024-10-18 22:43:22 +0000308Status BackendUnifiedServiceManager::getUpdatableNames(const ::std::string& apexName,
309 ::std::vector<::std::string>* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000310 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
311}
Devin Moore678984f2024-10-18 22:43:22 +0000312Status BackendUnifiedServiceManager::getConnectionInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000313 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
314 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
315}
Devin Moore678984f2024-10-18 22:43:22 +0000316Status BackendUnifiedServiceManager::registerClientCallback(
Parth Sane56a04712024-04-22 14:21:07 +0000317 const ::std::string& name, const sp<IBinder>& service,
318 const sp<os::IClientCallback>& callback) {
319 return mTheRealServiceManager->registerClientCallback(name, service, callback);
320}
Devin Moore678984f2024-10-18 22:43:22 +0000321Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
322 const sp<IBinder>& service) {
Parth Sane56a04712024-04-22 14:21:07 +0000323 return mTheRealServiceManager->tryUnregisterService(name, service);
324}
Devin Moore678984f2024-10-18 22:43:22 +0000325Status BackendUnifiedServiceManager::getServiceDebugInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000326 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
327 return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
328}
329
330[[clang::no_destroy]] static std::once_flag gUSmOnce;
331[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
332
333sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
334 std::call_once(gUSmOnce, []() {
335#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
336 /* wait for service manager */ {
337 using std::literals::chrono_literals::operator""s;
338 using android::base::WaitForProperty;
339 while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
340 ALOGE("Waited for servicemanager.ready for a second, waiting another...");
341 }
342 }
343#endif
344
345 sp<AidlServiceManager> sm = nullptr;
346 while (sm == nullptr) {
347 sm = interface_cast<AidlServiceManager>(
348 ProcessState::self()->getContextObject(nullptr));
349 if (sm == nullptr) {
350 ALOGE("Waiting 1s on context object on %s.",
351 ProcessState::self()->getDriverName().c_str());
352 sleep(1);
353 }
354 }
355
356 gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm);
357 });
358
359 return gUnifiedServiceManager;
360}
361
Devin Moore18f63752024-08-08 21:01:24 +0000362} // namespace android