blob: 1f8e8eba4c577d8b80076c7808741819052371ad [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",
Parth Sane0105ea52024-10-11 11:41:16 +000042 "android.frameworks.stats.IStats/default",
Parth Saneac492702024-09-18 15:54:16 +000043 "android.system.keystore2.IKeystoreService/default",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000044 "appops",
45 "audio",
Parth Sane0105ea52024-10-11 11:41:16 +000046 "autofill",
47 "batteryproperties",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000048 "batterystats",
Parth Sane0105ea52024-10-11 11:41:16 +000049 "biometic",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000050 "carrier_config",
51 "connectivity",
Parth Saneac492702024-09-18 15:54:16 +000052 "content",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000053 "content_capture",
54 "device_policy",
55 "display",
56 "dropbox",
57 "econtroller",
Parth Saneac492702024-09-18 15:54:16 +000058 "graphicsstats",
59 "input",
60 "input_method",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000061 "isub",
Parth Saneac492702024-09-18 15:54:16 +000062 "jobscheduler",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000063 "legacy_permission",
64 "location",
Parth Sane0105ea52024-10-11 11:41:16 +000065 "lock_settings",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000066 "media.extractor",
67 "media.metrics",
68 "media.player",
69 "media.resource_manager",
Parth Saneac492702024-09-18 15:54:16 +000070 "media_resource_monitor",
71 "mount",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000072 "netd_listener",
73 "netstats",
74 "network_management",
75 "nfc",
Parth Saneac492702024-09-18 15:54:16 +000076 "notification",
77 "package",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000078 "package_native",
79 "performance_hint",
80 "permission",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000081 "permission_checker",
Parth Saneac492702024-09-18 15:54:16 +000082 "permissionmgr",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000083 "phone",
84 "platform_compat",
85 "power",
Parth Sane0105ea52024-10-11 11:41:16 +000086 "processinfo",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000087 "role",
Parth Sane0105ea52024-10-11 11:41:16 +000088 "sensitive_content_protection_service",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000089 "sensorservice",
90 "statscompanion",
91 "telephony.registry",
92 "thermalservice",
93 "time_detector",
Parth Sane0105ea52024-10-11 11:41:16 +000094 "tracing.proxy",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000095 "trust",
96 "uimode",
Parth Saneac492702024-09-18 15:54:16 +000097 "user",
Parth Sane0105ea52024-10-11 11:41:16 +000098 "vibrator",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000099 "virtualdevice",
100 "virtualdevice_native",
101 "webviewupdate",
Parth Saneac492702024-09-18 15:54:16 +0000102 "window",
103 // go/keep-sorted end
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000104};
105
106bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) {
107 if (ProcessState::self()->getThreadPoolMaxTotalThreadCount() <= 0) {
108 ALOGW("Thread Pool max thread count is 0. Cannot cache binder as linkToDeath cannot be "
109 "implemented. serviceName: %s",
110 serviceName.c_str());
111 return false;
112 }
113 for (const char* name : kStaticCachableList) {
114 if (name == serviceName) {
115 return true;
116 }
117 }
118 return false;
119}
120
121binder::Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
122 const os::Service& service) {
123 if (!kUseCache) {
124 return binder::Status::ok();
125 }
126 if (service.getTag() == os::Service::Tag::binder) {
127 sp<IBinder> binder = service.get<os::Service::Tag::binder>();
128 if (binder && mCacheForGetService->isClientSideCachingEnabled(serviceName) &&
129 binder->isBinderAlive()) {
130 return mCacheForGetService->setItem(serviceName, binder);
131 }
132 }
133 return binder::Status::ok();
134}
135
136bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName,
137 os::Service* _out) {
138 if (!kUseCache) {
139 return false;
140 }
141 sp<IBinder> item = mCacheForGetService->getItem(serviceName);
142 // TODO(b/363177618): Enable caching for binders which are always null.
143 if (item != nullptr && item->isBinderAlive()) {
144 *_out = os::Service::make<os::Service::Tag::binder>(item);
145 return true;
146 }
147 return false;
148}
149
Parth Sane56a04712024-04-22 14:21:07 +0000150BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl)
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000151 : mTheRealServiceManager(impl) {
152 mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>();
153}
Parth Sane56a04712024-04-22 14:21:07 +0000154
155sp<AidlServiceManager> BackendUnifiedServiceManager::getImpl() {
156 return mTheRealServiceManager;
157}
Alice Wang11da1502024-07-25 12:03:22 +0000158
Parth Sane56a04712024-04-22 14:21:07 +0000159binder::Status BackendUnifiedServiceManager::getService(const ::std::string& name,
Alice Wang11da1502024-07-25 12:03:22 +0000160 sp<IBinder>* _aidl_return) {
Alice Wang8578f132024-05-03 09:01:56 +0000161 os::Service service;
Alice Wang11da1502024-07-25 12:03:22 +0000162 binder::Status status = getService2(name, &service);
163 *_aidl_return = service.get<os::Service::Tag::binder>();
164 return status;
165}
166
167binder::Status BackendUnifiedServiceManager::getService2(const ::std::string& name,
168 os::Service* _out) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000169 if (returnIfCached(name, _out)) {
170 return binder::Status::ok();
171 }
Alice Wang11da1502024-07-25 12:03:22 +0000172 os::Service service;
173 binder::Status status = mTheRealServiceManager->getService2(name, &service);
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000174
Devin Moore18f63752024-08-08 21:01:24 +0000175 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000176 status = toBinderService(name, service, _out);
177 if (status.isOk()) {
178 return updateCache(name, service);
179 }
Devin Moore18f63752024-08-08 21:01:24 +0000180 }
Alice Wang8578f132024-05-03 09:01:56 +0000181 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000182}
Alice Wang8578f132024-05-03 09:01:56 +0000183
Parth Sane56a04712024-04-22 14:21:07 +0000184binder::Status BackendUnifiedServiceManager::checkService(const ::std::string& name,
Alice Wang8578f132024-05-03 09:01:56 +0000185 os::Service* _out) {
186 os::Service service;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000187 if (returnIfCached(name, _out)) {
188 return binder::Status::ok();
189 }
190
Alice Wang8578f132024-05-03 09:01:56 +0000191 binder::Status status = mTheRealServiceManager->checkService(name, &service);
Devin Moore18f63752024-08-08 21:01:24 +0000192 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000193 status = toBinderService(name, service, _out);
194 if (status.isOk()) {
195 return updateCache(name, service);
196 }
Devin Moore18f63752024-08-08 21:01:24 +0000197 }
Alice Wang8578f132024-05-03 09:01:56 +0000198 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000199}
Alice Wang8578f132024-05-03 09:01:56 +0000200
Devin Moore18f63752024-08-08 21:01:24 +0000201binder::Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
202 const os::Service& in,
203 os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000204 switch (in.getTag()) {
205 case os::Service::Tag::binder: {
Devin Moore18f63752024-08-08 21:01:24 +0000206 if (in.get<os::Service::Tag::binder>() == nullptr) {
207 // failed to find a service. Check to see if we have any local
208 // injected Accessors for this service.
209 os::Service accessor;
210 binder::Status status = getInjectedAccessor(name, &accessor);
211 if (!status.isOk()) {
212 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
213 return status;
214 }
215 if (accessor.getTag() == os::Service::Tag::accessor &&
216 accessor.get<os::Service::Tag::accessor>() != nullptr) {
217 ALOGI("Found local injected service for %s, will attempt to create connection",
218 name.c_str());
219 // Call this again using the accessor Service to get the real
220 // service's binder into _out
221 return toBinderService(name, accessor, _out);
222 }
223 }
224
Alice Wang8578f132024-05-03 09:01:56 +0000225 *_out = in;
Devin Moore18f63752024-08-08 21:01:24 +0000226 return binder::Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000227 }
228 case os::Service::Tag::accessor: {
229 sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>();
230 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
231 if (accessor == nullptr) {
232 ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
233 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
Devin Moore18f63752024-08-08 21:01:24 +0000234 return binder::Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000235 }
236 auto request = [=] {
237 os::ParcelFileDescriptor fd;
238 binder::Status ret = accessor->addConnection(&fd);
239 if (ret.isOk()) {
240 return base::unique_fd(fd.release());
241 } else {
242 ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str());
243 return base::unique_fd(-1);
244 }
245 };
246 auto session = RpcSession::make();
Devin Moore18f63752024-08-08 21:01:24 +0000247 status_t status = session->setupPreconnectedClient(base::unique_fd{}, request);
248 if (status != OK) {
249 ALOGE("Failed to set up preconnected binder RPC client: %s",
250 statusToString(status).c_str());
251 return binder::Status::fromStatusT(status);
252 }
Alice Wang8578f132024-05-03 09:01:56 +0000253 session->setSessionSpecificRoot(accessorBinder);
254 *_out = os::Service::make<os::Service::Tag::binder>(session->getRootObject());
Devin Moore18f63752024-08-08 21:01:24 +0000255 return binder::Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000256 }
257 default: {
258 LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag());
259 }
260 }
261}
262
Parth Sane56a04712024-04-22 14:21:07 +0000263binder::Status BackendUnifiedServiceManager::addService(const ::std::string& name,
264 const sp<IBinder>& service,
265 bool allowIsolated, int32_t dumpPriority) {
266 return mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
267}
268binder::Status BackendUnifiedServiceManager::listServices(
269 int32_t dumpPriority, ::std::vector<::std::string>* _aidl_return) {
270 return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
271}
272binder::Status BackendUnifiedServiceManager::registerForNotifications(
273 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
274 return mTheRealServiceManager->registerForNotifications(name, callback);
275}
276binder::Status BackendUnifiedServiceManager::unregisterForNotifications(
277 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
278 return mTheRealServiceManager->unregisterForNotifications(name, callback);
279}
280binder::Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name,
281 bool* _aidl_return) {
282 return mTheRealServiceManager->isDeclared(name, _aidl_return);
283}
284binder::Status BackendUnifiedServiceManager::getDeclaredInstances(
285 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
286 return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
287}
288binder::Status BackendUnifiedServiceManager::updatableViaApex(
289 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
290 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
291}
292binder::Status BackendUnifiedServiceManager::getUpdatableNames(
293 const ::std::string& apexName, ::std::vector<::std::string>* _aidl_return) {
294 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
295}
296binder::Status BackendUnifiedServiceManager::getConnectionInfo(
297 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
298 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
299}
300binder::Status BackendUnifiedServiceManager::registerClientCallback(
301 const ::std::string& name, const sp<IBinder>& service,
302 const sp<os::IClientCallback>& callback) {
303 return mTheRealServiceManager->registerClientCallback(name, service, callback);
304}
305binder::Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
306 const sp<IBinder>& service) {
307 return mTheRealServiceManager->tryUnregisterService(name, service);
308}
309binder::Status BackendUnifiedServiceManager::getServiceDebugInfo(
310 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
311 return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
312}
313
314[[clang::no_destroy]] static std::once_flag gUSmOnce;
315[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
316
317sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
318 std::call_once(gUSmOnce, []() {
319#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
320 /* wait for service manager */ {
321 using std::literals::chrono_literals::operator""s;
322 using android::base::WaitForProperty;
323 while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
324 ALOGE("Waited for servicemanager.ready for a second, waiting another...");
325 }
326 }
327#endif
328
329 sp<AidlServiceManager> sm = nullptr;
330 while (sm == nullptr) {
331 sm = interface_cast<AidlServiceManager>(
332 ProcessState::self()->getContextObject(nullptr));
333 if (sm == nullptr) {
334 ALOGE("Waiting 1s on context object on %s.",
335 ProcessState::self()->getDriverName().c_str());
336 sleep(1);
337 }
338 }
339
340 gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm);
341 });
342
343 return gUnifiedServiceManager;
344}
345
Devin Moore18f63752024-08-08 21:01:24 +0000346} // namespace android