blob: 52b485a6f64b9485a24dce33e905a03476dea39c [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 }
117 if (service.getTag() == os::Service::Tag::binder) {
118 sp<IBinder> binder = service.get<os::Service::Tag::binder>();
119 if (binder && mCacheForGetService->isClientSideCachingEnabled(serviceName) &&
120 binder->isBinderAlive()) {
121 return mCacheForGetService->setItem(serviceName, binder);
122 }
123 }
124 return binder::Status::ok();
125}
126
127bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName,
128 os::Service* _out) {
129 if (!kUseCache) {
130 return false;
131 }
132 sp<IBinder> item = mCacheForGetService->getItem(serviceName);
133 // TODO(b/363177618): Enable caching for binders which are always null.
134 if (item != nullptr && item->isBinderAlive()) {
135 *_out = os::Service::make<os::Service::Tag::binder>(item);
136 return true;
137 }
138 return false;
139}
140
Parth Sane56a04712024-04-22 14:21:07 +0000141BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl)
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000142 : mTheRealServiceManager(impl) {
143 mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>();
144}
Parth Sane56a04712024-04-22 14:21:07 +0000145
146sp<AidlServiceManager> BackendUnifiedServiceManager::getImpl() {
147 return mTheRealServiceManager;
148}
Alice Wang11da1502024-07-25 12:03:22 +0000149
Parth Sane56a04712024-04-22 14:21:07 +0000150binder::Status BackendUnifiedServiceManager::getService(const ::std::string& name,
Alice Wang11da1502024-07-25 12:03:22 +0000151 sp<IBinder>* _aidl_return) {
Alice Wang8578f132024-05-03 09:01:56 +0000152 os::Service service;
Alice Wang11da1502024-07-25 12:03:22 +0000153 binder::Status status = getService2(name, &service);
154 *_aidl_return = service.get<os::Service::Tag::binder>();
155 return status;
156}
157
158binder::Status BackendUnifiedServiceManager::getService2(const ::std::string& name,
159 os::Service* _out) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000160 if (returnIfCached(name, _out)) {
161 return binder::Status::ok();
162 }
Alice Wang11da1502024-07-25 12:03:22 +0000163 os::Service service;
164 binder::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
Parth Sane56a04712024-04-22 14:21:07 +0000175binder::Status BackendUnifiedServiceManager::checkService(const ::std::string& name,
Alice Wang8578f132024-05-03 09:01:56 +0000176 os::Service* _out) {
177 os::Service service;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000178 if (returnIfCached(name, _out)) {
179 return binder::Status::ok();
180 }
181
Alice Wang8578f132024-05-03 09:01:56 +0000182 binder::Status status = mTheRealServiceManager->checkService(name, &service);
Devin Moore18f63752024-08-08 21:01:24 +0000183 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000184 status = toBinderService(name, service, _out);
185 if (status.isOk()) {
186 return updateCache(name, service);
187 }
Devin Moore18f63752024-08-08 21:01:24 +0000188 }
Alice Wang8578f132024-05-03 09:01:56 +0000189 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000190}
Alice Wang8578f132024-05-03 09:01:56 +0000191
Devin Moore18f63752024-08-08 21:01:24 +0000192binder::Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
193 const os::Service& in,
194 os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000195 switch (in.getTag()) {
196 case os::Service::Tag::binder: {
Devin Moore18f63752024-08-08 21:01:24 +0000197 if (in.get<os::Service::Tag::binder>() == nullptr) {
198 // failed to find a service. Check to see if we have any local
199 // injected Accessors for this service.
200 os::Service accessor;
201 binder::Status status = getInjectedAccessor(name, &accessor);
202 if (!status.isOk()) {
203 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
204 return status;
205 }
206 if (accessor.getTag() == os::Service::Tag::accessor &&
207 accessor.get<os::Service::Tag::accessor>() != nullptr) {
208 ALOGI("Found local injected service for %s, will attempt to create connection",
209 name.c_str());
210 // Call this again using the accessor Service to get the real
211 // service's binder into _out
212 return toBinderService(name, accessor, _out);
213 }
214 }
215
Alice Wang8578f132024-05-03 09:01:56 +0000216 *_out = in;
Devin Moore18f63752024-08-08 21:01:24 +0000217 return binder::Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000218 }
219 case os::Service::Tag::accessor: {
220 sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>();
221 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
222 if (accessor == nullptr) {
223 ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
224 *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
Devin Moore18f63752024-08-08 21:01:24 +0000225 return binder::Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000226 }
227 auto request = [=] {
228 os::ParcelFileDescriptor fd;
229 binder::Status ret = accessor->addConnection(&fd);
230 if (ret.isOk()) {
231 return base::unique_fd(fd.release());
232 } else {
233 ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str());
234 return base::unique_fd(-1);
235 }
236 };
237 auto session = RpcSession::make();
Devin Moore18f63752024-08-08 21:01:24 +0000238 status_t status = session->setupPreconnectedClient(base::unique_fd{}, request);
239 if (status != OK) {
240 ALOGE("Failed to set up preconnected binder RPC client: %s",
241 statusToString(status).c_str());
242 return binder::Status::fromStatusT(status);
243 }
Alice Wang8578f132024-05-03 09:01:56 +0000244 session->setSessionSpecificRoot(accessorBinder);
245 *_out = os::Service::make<os::Service::Tag::binder>(session->getRootObject());
Devin Moore18f63752024-08-08 21:01:24 +0000246 return binder::Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000247 }
248 default: {
249 LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag());
250 }
251 }
252}
253
Parth Sane56a04712024-04-22 14:21:07 +0000254binder::Status BackendUnifiedServiceManager::addService(const ::std::string& name,
255 const sp<IBinder>& service,
256 bool allowIsolated, int32_t dumpPriority) {
257 return mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
258}
259binder::Status BackendUnifiedServiceManager::listServices(
260 int32_t dumpPriority, ::std::vector<::std::string>* _aidl_return) {
261 return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
262}
263binder::Status BackendUnifiedServiceManager::registerForNotifications(
264 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
265 return mTheRealServiceManager->registerForNotifications(name, callback);
266}
267binder::Status BackendUnifiedServiceManager::unregisterForNotifications(
268 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
269 return mTheRealServiceManager->unregisterForNotifications(name, callback);
270}
271binder::Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name,
272 bool* _aidl_return) {
273 return mTheRealServiceManager->isDeclared(name, _aidl_return);
274}
275binder::Status BackendUnifiedServiceManager::getDeclaredInstances(
276 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
277 return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
278}
279binder::Status BackendUnifiedServiceManager::updatableViaApex(
280 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
281 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
282}
283binder::Status BackendUnifiedServiceManager::getUpdatableNames(
284 const ::std::string& apexName, ::std::vector<::std::string>* _aidl_return) {
285 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
286}
287binder::Status BackendUnifiedServiceManager::getConnectionInfo(
288 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
289 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
290}
291binder::Status BackendUnifiedServiceManager::registerClientCallback(
292 const ::std::string& name, const sp<IBinder>& service,
293 const sp<os::IClientCallback>& callback) {
294 return mTheRealServiceManager->registerClientCallback(name, service, callback);
295}
296binder::Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
297 const sp<IBinder>& service) {
298 return mTheRealServiceManager->tryUnregisterService(name, service);
299}
300binder::Status BackendUnifiedServiceManager::getServiceDebugInfo(
301 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
302 return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
303}
304
305[[clang::no_destroy]] static std::once_flag gUSmOnce;
306[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
307
308sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
309 std::call_once(gUSmOnce, []() {
310#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
311 /* wait for service manager */ {
312 using std::literals::chrono_literals::operator""s;
313 using android::base::WaitForProperty;
314 while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
315 ALOGE("Waited for servicemanager.ready for a second, waiting another...");
316 }
317 }
318#endif
319
320 sp<AidlServiceManager> sm = nullptr;
321 while (sm == nullptr) {
322 sm = interface_cast<AidlServiceManager>(
323 ProcessState::self()->getContextObject(nullptr));
324 if (sm == nullptr) {
325 ALOGE("Waiting 1s on context object on %s.",
326 ProcessState::self()->getDriverName().c_str());
327 sleep(1);
328 }
329 }
330
331 gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm);
332 });
333
334 return gUnifiedServiceManager;
335}
336
Devin Moore18f63752024-08-08 21:01:24 +0000337} // namespace android