blob: 37aaaf388ea4ed8b513e07c8e1967727c82d5675 [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;
Alice Wang8578f132024-05-03 09:01:56 +000034using IAccessor = android::os::IAccessor;
Parth Sane56a04712024-04-22 14:21:07 +000035
Parth Saneb6ed0eb2024-06-25 14:38:42 +000036static const char* kStaticCachableList[] = {
Parth Saneac492702024-09-18 15:54:16 +000037 // go/keep-sorted start
38 "accessibility",
39 "account",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000040 "activity",
Parth Saneac492702024-09-18 15:54:16 +000041 "alarm",
42 "android.system.keystore2.IKeystoreService/default",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000043 "appops",
44 "audio",
45 "batterystats",
46 "carrier_config",
47 "connectivity",
Parth Saneac492702024-09-18 15:54:16 +000048 "content",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000049 "content_capture",
50 "device_policy",
51 "display",
52 "dropbox",
53 "econtroller",
Parth Saneac492702024-09-18 15:54:16 +000054 "graphicsstats",
55 "input",
56 "input_method",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000057 "isub",
Parth Saneac492702024-09-18 15:54:16 +000058 "jobscheduler",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000059 "legacy_permission",
60 "location",
61 "media.extractor",
62 "media.metrics",
63 "media.player",
64 "media.resource_manager",
Parth Saneac492702024-09-18 15:54:16 +000065 "media_resource_monitor",
66 "mount",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000067 "netd_listener",
68 "netstats",
69 "network_management",
70 "nfc",
Parth Saneac492702024-09-18 15:54:16 +000071 "notification",
72 "package",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000073 "package_native",
74 "performance_hint",
75 "permission",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000076 "permission_checker",
Parth Saneac492702024-09-18 15:54:16 +000077 "permissionmgr",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000078 "phone",
79 "platform_compat",
80 "power",
81 "role",
82 "sensorservice",
83 "statscompanion",
84 "telephony.registry",
85 "thermalservice",
86 "time_detector",
87 "trust",
88 "uimode",
Parth Saneac492702024-09-18 15:54:16 +000089 "user",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000090 "virtualdevice",
91 "virtualdevice_native",
92 "webviewupdate",
Parth Saneac492702024-09-18 15:54:16 +000093 "window",
94 // go/keep-sorted end
Parth Saneb6ed0eb2024-06-25 14:38:42 +000095};
96
97bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) {
98 if (ProcessState::self()->getThreadPoolMaxTotalThreadCount() <= 0) {
99 ALOGW("Thread Pool max thread count is 0. Cannot cache binder as linkToDeath cannot be "
100 "implemented. serviceName: %s",
101 serviceName.c_str());
102 return false;
103 }
104 for (const char* name : kStaticCachableList) {
105 if (name == serviceName) {
106 return true;
107 }
108 }
109 return false;
110}
111
112binder::Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
113 const os::Service& service) {
114 if (!kUseCache) {
115 return binder::Status::ok();
116 }
Parth Sanea6676ba2024-10-04 14:14:07 +0000117 std::string traceStr;
118 if (atrace_is_tag_enabled(ATRACE_TAG_AIDL)) {
119 traceStr = "BinderCacheWithInvalidation::updateCache : " + serviceName;
120 }
121 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, traceStr.c_str());
122
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000123 if (service.getTag() == os::Service::Tag::binder) {
124 sp<IBinder> binder = service.get<os::Service::Tag::binder>();
Parth Sanea6676ba2024-10-04 14:14:07 +0000125 if (!binder) {
126 binder::ScopedTrace
127 aidlTrace(ATRACE_TAG_AIDL,
128 "BinderCacheWithInvalidation::updateCache failed: binder_null");
129 } else if (!binder->isBinderAlive()) {
130 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
131 "BinderCacheWithInvalidation::updateCache failed: "
132 "isBinderAlive_false");
133 } else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) {
134 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
135 "BinderCacheWithInvalidation::updateCache successful");
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000136 return mCacheForGetService->setItem(serviceName, binder);
Parth Sanea6676ba2024-10-04 14:14:07 +0000137 } else {
138 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
139 "BinderCacheWithInvalidation::updateCache failed: "
140 "caching_not_enabled");
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000141 }
142 }
143 return binder::Status::ok();
144}
145
146bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName,
147 os::Service* _out) {
148 if (!kUseCache) {
149 return false;
150 }
151 sp<IBinder> item = mCacheForGetService->getItem(serviceName);
152 // TODO(b/363177618): Enable caching for binders which are always null.
153 if (item != nullptr && item->isBinderAlive()) {
154 *_out = os::Service::make<os::Service::Tag::binder>(item);
155 return true;
156 }
157 return false;
158}
159
Parth Sane56a04712024-04-22 14:21:07 +0000160BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl)
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000161 : mTheRealServiceManager(impl) {
162 mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>();
163}
Parth Sane56a04712024-04-22 14:21:07 +0000164
165sp<AidlServiceManager> BackendUnifiedServiceManager::getImpl() {
166 return mTheRealServiceManager;
167}
Alice Wang11da1502024-07-25 12:03:22 +0000168
Parth Sane56a04712024-04-22 14:21:07 +0000169binder::Status BackendUnifiedServiceManager::getService(const ::std::string& name,
Alice Wang11da1502024-07-25 12:03:22 +0000170 sp<IBinder>* _aidl_return) {
Alice Wang8578f132024-05-03 09:01:56 +0000171 os::Service service;
Alice Wang11da1502024-07-25 12:03:22 +0000172 binder::Status status = getService2(name, &service);
173 *_aidl_return = service.get<os::Service::Tag::binder>();
174 return status;
175}
176
177binder::Status BackendUnifiedServiceManager::getService2(const ::std::string& name,
178 os::Service* _out) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000179 if (returnIfCached(name, _out)) {
180 return binder::Status::ok();
181 }
Alice Wang11da1502024-07-25 12:03:22 +0000182 os::Service service;
183 binder::Status status = mTheRealServiceManager->getService2(name, &service);
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000184
Devin Moore18f63752024-08-08 21:01:24 +0000185 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000186 status = toBinderService(name, service, _out);
187 if (status.isOk()) {
188 return updateCache(name, service);
189 }
Devin Moore18f63752024-08-08 21:01:24 +0000190 }
Alice Wang8578f132024-05-03 09:01:56 +0000191 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000192}
Alice Wang8578f132024-05-03 09:01:56 +0000193
Parth Sane56a04712024-04-22 14:21:07 +0000194binder::Status BackendUnifiedServiceManager::checkService(const ::std::string& name,
Alice Wang8578f132024-05-03 09:01:56 +0000195 os::Service* _out) {
196 os::Service service;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000197 if (returnIfCached(name, _out)) {
198 return binder::Status::ok();
199 }
200
Alice Wang8578f132024-05-03 09:01:56 +0000201 binder::Status status = mTheRealServiceManager->checkService(name, &service);
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 Moore18f63752024-08-08 21:01:24 +0000211binder::Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
212 const os::Service& in,
213 os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000214 switch (in.getTag()) {
215 case os::Service::Tag::binder: {
Devin Moore18f63752024-08-08 21:01:24 +0000216 if (in.get<os::Service::Tag::binder>() == nullptr) {
217 // failed to find a service. Check to see if we have any local
218 // injected Accessors for this service.
219 os::Service accessor;
220 binder::Status status = getInjectedAccessor(name, &accessor);
221 if (!status.isOk()) {
222 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
223 return status;
224 }
225 if (accessor.getTag() == os::Service::Tag::accessor &&
226 accessor.get<os::Service::Tag::accessor>() != nullptr) {
227 ALOGI("Found local injected service for %s, will attempt to create connection",
228 name.c_str());
229 // Call this again using the accessor Service to get the real
230 // service's binder into _out
231 return toBinderService(name, accessor, _out);
232 }
233 }
234
Alice Wang8578f132024-05-03 09:01:56 +0000235 *_out = in;
Devin Moore18f63752024-08-08 21:01:24 +0000236 return binder::Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000237 }
238 case os::Service::Tag::accessor: {
239 sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>();
240 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
241 if (accessor == nullptr) {
242 ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
243 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
Devin Moore18f63752024-08-08 21:01:24 +0000244 return binder::Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000245 }
246 auto request = [=] {
247 os::ParcelFileDescriptor fd;
248 binder::Status ret = accessor->addConnection(&fd);
249 if (ret.isOk()) {
250 return base::unique_fd(fd.release());
251 } else {
252 ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str());
253 return base::unique_fd(-1);
254 }
255 };
256 auto session = RpcSession::make();
Devin Moore18f63752024-08-08 21:01:24 +0000257 status_t status = session->setupPreconnectedClient(base::unique_fd{}, request);
258 if (status != OK) {
259 ALOGE("Failed to set up preconnected binder RPC client: %s",
260 statusToString(status).c_str());
261 return binder::Status::fromStatusT(status);
262 }
Alice Wang8578f132024-05-03 09:01:56 +0000263 session->setSessionSpecificRoot(accessorBinder);
264 *_out = os::Service::make<os::Service::Tag::binder>(session->getRootObject());
Devin Moore18f63752024-08-08 21:01:24 +0000265 return binder::Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000266 }
267 default: {
268 LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag());
269 }
270 }
271}
272
Parth Sane56a04712024-04-22 14:21:07 +0000273binder::Status BackendUnifiedServiceManager::addService(const ::std::string& name,
274 const sp<IBinder>& service,
275 bool allowIsolated, int32_t dumpPriority) {
276 return mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
277}
278binder::Status BackendUnifiedServiceManager::listServices(
279 int32_t dumpPriority, ::std::vector<::std::string>* _aidl_return) {
280 return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
281}
282binder::Status BackendUnifiedServiceManager::registerForNotifications(
283 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
284 return mTheRealServiceManager->registerForNotifications(name, callback);
285}
286binder::Status BackendUnifiedServiceManager::unregisterForNotifications(
287 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
288 return mTheRealServiceManager->unregisterForNotifications(name, callback);
289}
290binder::Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name,
291 bool* _aidl_return) {
292 return mTheRealServiceManager->isDeclared(name, _aidl_return);
293}
294binder::Status BackendUnifiedServiceManager::getDeclaredInstances(
295 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
296 return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
297}
298binder::Status BackendUnifiedServiceManager::updatableViaApex(
299 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
300 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
301}
302binder::Status BackendUnifiedServiceManager::getUpdatableNames(
303 const ::std::string& apexName, ::std::vector<::std::string>* _aidl_return) {
304 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
305}
306binder::Status BackendUnifiedServiceManager::getConnectionInfo(
307 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
308 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
309}
310binder::Status BackendUnifiedServiceManager::registerClientCallback(
311 const ::std::string& name, const sp<IBinder>& service,
312 const sp<os::IClientCallback>& callback) {
313 return mTheRealServiceManager->registerClientCallback(name, service, callback);
314}
315binder::Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
316 const sp<IBinder>& service) {
317 return mTheRealServiceManager->tryUnregisterService(name, service);
318}
319binder::Status BackendUnifiedServiceManager::getServiceDebugInfo(
320 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
321 return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
322}
323
324[[clang::no_destroy]] static std::once_flag gUSmOnce;
325[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
326
327sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
328 std::call_once(gUSmOnce, []() {
329#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
330 /* wait for service manager */ {
331 using std::literals::chrono_literals::operator""s;
332 using android::base::WaitForProperty;
333 while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
334 ALOGE("Waited for servicemanager.ready for a second, waiting another...");
335 }
336 }
337#endif
338
339 sp<AidlServiceManager> sm = nullptr;
340 while (sm == nullptr) {
341 sm = interface_cast<AidlServiceManager>(
342 ProcessState::self()->getContextObject(nullptr));
343 if (sm == nullptr) {
344 ALOGE("Waiting 1s on context object on %s.",
345 ProcessState::self()->getDriverName().c_str());
346 sleep(1);
347 }
348 }
349
350 gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm);
351 });
352
353 return gUnifiedServiceManager;
354}
355
Devin Moore18f63752024-08-08 21:01:24 +0000356} // namespace android