blob: 04de39abe14faa1f5abf982688a3d4a79173c05a [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",
43 "android.system.keystore2.IKeystoreService/default",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000044 "appops",
45 "audio",
46 "batterystats",
47 "carrier_config",
48 "connectivity",
Parth Saneac492702024-09-18 15:54:16 +000049 "content",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000050 "content_capture",
51 "device_policy",
52 "display",
53 "dropbox",
54 "econtroller",
Parth Saneac492702024-09-18 15:54:16 +000055 "graphicsstats",
56 "input",
57 "input_method",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000058 "isub",
Parth Saneac492702024-09-18 15:54:16 +000059 "jobscheduler",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000060 "legacy_permission",
61 "location",
62 "media.extractor",
63 "media.metrics",
64 "media.player",
65 "media.resource_manager",
Parth Saneac492702024-09-18 15:54:16 +000066 "media_resource_monitor",
67 "mount",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000068 "netd_listener",
69 "netstats",
70 "network_management",
71 "nfc",
Parth Saneac492702024-09-18 15:54:16 +000072 "notification",
73 "package",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000074 "package_native",
75 "performance_hint",
76 "permission",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000077 "permission_checker",
Parth Saneac492702024-09-18 15:54:16 +000078 "permissionmgr",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000079 "phone",
80 "platform_compat",
81 "power",
82 "role",
83 "sensorservice",
84 "statscompanion",
85 "telephony.registry",
86 "thermalservice",
87 "time_detector",
88 "trust",
89 "uimode",
Parth Saneac492702024-09-18 15:54:16 +000090 "user",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000091 "virtualdevice",
92 "virtualdevice_native",
93 "webviewupdate",
Parth Saneac492702024-09-18 15:54:16 +000094 "window",
95 // go/keep-sorted end
Parth Saneb6ed0eb2024-06-25 14:38:42 +000096};
97
98bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) {
99 if (ProcessState::self()->getThreadPoolMaxTotalThreadCount() <= 0) {
100 ALOGW("Thread Pool max thread count is 0. Cannot cache binder as linkToDeath cannot be "
101 "implemented. serviceName: %s",
102 serviceName.c_str());
103 return false;
104 }
105 for (const char* name : kStaticCachableList) {
106 if (name == serviceName) {
107 return true;
108 }
109 }
110 return false;
111}
112
Devin Moore678984f2024-10-18 22:43:22 +0000113Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
114 const os::Service& service) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000115 if (!kUseCache) {
Devin Moore678984f2024-10-18 22:43:22 +0000116 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000117 }
118 if (service.getTag() == os::Service::Tag::binder) {
119 sp<IBinder> binder = service.get<os::Service::Tag::binder>();
120 if (binder && mCacheForGetService->isClientSideCachingEnabled(serviceName) &&
121 binder->isBinderAlive()) {
122 return mCacheForGetService->setItem(serviceName, binder);
123 }
124 }
Devin Moore678984f2024-10-18 22:43:22 +0000125 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000126}
127
128bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName,
129 os::Service* _out) {
130 if (!kUseCache) {
131 return false;
132 }
133 sp<IBinder> item = mCacheForGetService->getItem(serviceName);
134 // TODO(b/363177618): Enable caching for binders which are always null.
135 if (item != nullptr && item->isBinderAlive()) {
136 *_out = os::Service::make<os::Service::Tag::binder>(item);
137 return true;
138 }
139 return false;
140}
141
Parth Sane56a04712024-04-22 14:21:07 +0000142BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl)
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000143 : mTheRealServiceManager(impl) {
144 mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>();
145}
Parth Sane56a04712024-04-22 14:21:07 +0000146
147sp<AidlServiceManager> BackendUnifiedServiceManager::getImpl() {
148 return mTheRealServiceManager;
149}
Alice Wang11da1502024-07-25 12:03:22 +0000150
Devin Moore678984f2024-10-18 22:43:22 +0000151Status BackendUnifiedServiceManager::getService(const ::std::string& name,
152 sp<IBinder>* _aidl_return) {
Alice Wang8578f132024-05-03 09:01:56 +0000153 os::Service service;
Devin Moore678984f2024-10-18 22:43:22 +0000154 Status status = getService2(name, &service);
Alice Wang11da1502024-07-25 12:03:22 +0000155 *_aidl_return = service.get<os::Service::Tag::binder>();
156 return status;
157}
158
Devin Moore678984f2024-10-18 22:43:22 +0000159Status BackendUnifiedServiceManager::getService2(const ::std::string& name, os::Service* _out) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000160 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000161 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000162 }
Alice Wang11da1502024-07-25 12:03:22 +0000163 os::Service service;
Devin Moore678984f2024-10-18 22:43:22 +0000164 Status status = mTheRealServiceManager->getService2(name, &service);
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000165
Devin Moore18f63752024-08-08 21:01:24 +0000166 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000167 status = toBinderService(name, service, _out);
168 if (status.isOk()) {
169 return updateCache(name, service);
170 }
Devin Moore18f63752024-08-08 21:01:24 +0000171 }
Alice Wang8578f132024-05-03 09:01:56 +0000172 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000173}
Alice Wang8578f132024-05-03 09:01:56 +0000174
Devin Moore678984f2024-10-18 22:43:22 +0000175Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000176 os::Service service;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000177 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000178 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000179 }
180
Devin Moore678984f2024-10-18 22:43:22 +0000181 Status status = mTheRealServiceManager->checkService(name, &service);
Devin Moore18f63752024-08-08 21:01:24 +0000182 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000183 status = toBinderService(name, service, _out);
184 if (status.isOk()) {
185 return updateCache(name, service);
186 }
Devin Moore18f63752024-08-08 21:01:24 +0000187 }
Alice Wang8578f132024-05-03 09:01:56 +0000188 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000189}
Alice Wang8578f132024-05-03 09:01:56 +0000190
Devin Moore678984f2024-10-18 22:43:22 +0000191Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
192 const os::Service& in, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000193 switch (in.getTag()) {
194 case os::Service::Tag::binder: {
Devin Moore18f63752024-08-08 21:01:24 +0000195 if (in.get<os::Service::Tag::binder>() == nullptr) {
196 // failed to find a service. Check to see if we have any local
197 // injected Accessors for this service.
198 os::Service accessor;
Devin Moore678984f2024-10-18 22:43:22 +0000199 Status status = getInjectedAccessor(name, &accessor);
Devin Moore18f63752024-08-08 21:01:24 +0000200 if (!status.isOk()) {
201 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
202 return status;
203 }
204 if (accessor.getTag() == os::Service::Tag::accessor &&
205 accessor.get<os::Service::Tag::accessor>() != nullptr) {
206 ALOGI("Found local injected service for %s, will attempt to create connection",
207 name.c_str());
208 // Call this again using the accessor Service to get the real
209 // service's binder into _out
210 return toBinderService(name, accessor, _out);
211 }
212 }
213
Alice Wang8578f132024-05-03 09:01:56 +0000214 *_out = in;
Devin Moore678984f2024-10-18 22:43:22 +0000215 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000216 }
217 case os::Service::Tag::accessor: {
218 sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>();
219 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
220 if (accessor == nullptr) {
221 ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
222 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
Devin Moore678984f2024-10-18 22:43:22 +0000223 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000224 }
225 auto request = [=] {
226 os::ParcelFileDescriptor fd;
Devin Moore678984f2024-10-18 22:43:22 +0000227 Status ret = accessor->addConnection(&fd);
Alice Wang8578f132024-05-03 09:01:56 +0000228 if (ret.isOk()) {
229 return base::unique_fd(fd.release());
230 } else {
231 ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str());
232 return base::unique_fd(-1);
233 }
234 };
235 auto session = RpcSession::make();
Devin Moore18f63752024-08-08 21:01:24 +0000236 status_t status = session->setupPreconnectedClient(base::unique_fd{}, request);
237 if (status != OK) {
238 ALOGE("Failed to set up preconnected binder RPC client: %s",
239 statusToString(status).c_str());
Devin Moore678984f2024-10-18 22:43:22 +0000240 return Status::fromStatusT(status);
Devin Moore18f63752024-08-08 21:01:24 +0000241 }
Alice Wang8578f132024-05-03 09:01:56 +0000242 session->setSessionSpecificRoot(accessorBinder);
243 *_out = os::Service::make<os::Service::Tag::binder>(session->getRootObject());
Devin Moore678984f2024-10-18 22:43:22 +0000244 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000245 }
246 default: {
247 LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag());
248 }
249 }
250}
251
Devin Moore678984f2024-10-18 22:43:22 +0000252Status BackendUnifiedServiceManager::addService(const ::std::string& name,
253 const sp<IBinder>& service, bool allowIsolated,
254 int32_t dumpPriority) {
Parth Sane56a04712024-04-22 14:21:07 +0000255 return mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
256}
Devin Moore678984f2024-10-18 22:43:22 +0000257Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority,
258 ::std::vector<::std::string>* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000259 return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
260}
Devin Moore678984f2024-10-18 22:43:22 +0000261Status BackendUnifiedServiceManager::registerForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000262 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
263 return mTheRealServiceManager->registerForNotifications(name, callback);
264}
Devin Moore678984f2024-10-18 22:43:22 +0000265Status BackendUnifiedServiceManager::unregisterForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000266 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
267 return mTheRealServiceManager->unregisterForNotifications(name, callback);
268}
Devin Moore678984f2024-10-18 22:43:22 +0000269Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name, bool* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000270 return mTheRealServiceManager->isDeclared(name, _aidl_return);
271}
Devin Moore678984f2024-10-18 22:43:22 +0000272Status BackendUnifiedServiceManager::getDeclaredInstances(
Parth Sane56a04712024-04-22 14:21:07 +0000273 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
274 return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
275}
Devin Moore678984f2024-10-18 22:43:22 +0000276Status BackendUnifiedServiceManager::updatableViaApex(
Parth Sane56a04712024-04-22 14:21:07 +0000277 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
278 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
279}
Devin Moore678984f2024-10-18 22:43:22 +0000280Status BackendUnifiedServiceManager::getUpdatableNames(const ::std::string& apexName,
281 ::std::vector<::std::string>* _aidl_return) {
Parth Sane56a04712024-04-22 14:21:07 +0000282 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
283}
Devin Moore678984f2024-10-18 22:43:22 +0000284Status BackendUnifiedServiceManager::getConnectionInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000285 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
286 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
287}
Devin Moore678984f2024-10-18 22:43:22 +0000288Status BackendUnifiedServiceManager::registerClientCallback(
Parth Sane56a04712024-04-22 14:21:07 +0000289 const ::std::string& name, const sp<IBinder>& service,
290 const sp<os::IClientCallback>& callback) {
291 return mTheRealServiceManager->registerClientCallback(name, service, callback);
292}
Devin Moore678984f2024-10-18 22:43:22 +0000293Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
294 const sp<IBinder>& service) {
Parth Sane56a04712024-04-22 14:21:07 +0000295 return mTheRealServiceManager->tryUnregisterService(name, service);
296}
Devin Moore678984f2024-10-18 22:43:22 +0000297Status BackendUnifiedServiceManager::getServiceDebugInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000298 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
299 return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
300}
301
302[[clang::no_destroy]] static std::once_flag gUSmOnce;
303[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
304
305sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
306 std::call_once(gUSmOnce, []() {
307#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
308 /* wait for service manager */ {
309 using std::literals::chrono_literals::operator""s;
310 using android::base::WaitForProperty;
311 while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
312 ALOGE("Waited for servicemanager.ready for a second, waiting another...");
313 }
314 }
315#endif
316
317 sp<AidlServiceManager> sm = nullptr;
318 while (sm == nullptr) {
319 sm = interface_cast<AidlServiceManager>(
320 ProcessState::self()->getContextObject(nullptr));
321 if (sm == nullptr) {
322 ALOGE("Waiting 1s on context object on %s.",
323 ProcessState::self()->getDriverName().c_str());
324 sleep(1);
325 }
326 }
327
328 gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm);
329 });
330
331 return gUnifiedServiceManager;
332}
333
Devin Moore18f63752024-08-08 21:01:24 +0000334} // namespace android