blob: 9d0a557f4ed843e74977e449d73dea4272a72dfb [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>
Parth Sane5e1b7e12024-11-29 10:40:41 +000019#include <android/os/IServiceManager.h>
Alice Wang8578f132024-05-03 09:01:56 +000020#include <binder/RpcSession.h>
21
Tomasz Wasilczykfe25f122024-06-26 12:45:57 -070022#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
23#include <android-base/properties.h>
24#endif
25
Parth Sane56a04712024-04-22 14:21:07 +000026namespace android {
27
Parth Saneb6ed0eb2024-06-25 14:38:42 +000028#ifdef LIBBINDER_CLIENT_CACHE
29constexpr bool kUseCache = true;
30#else
31constexpr bool kUseCache = false;
32#endif
33
Parth Sanedc207542024-11-14 11:49:08 +000034#ifdef LIBBINDER_ADDSERVICE_CACHE
35constexpr bool kUseCacheInAddService = true;
36#else
37constexpr bool kUseCacheInAddService = false;
38#endif
39
Parth Sane5e1b7e12024-11-29 10:40:41 +000040#ifdef LIBBINDER_REMOVE_CACHE_STATIC_LIST
41constexpr bool kRemoveStaticList = true;
42#else
43constexpr bool kRemoveStaticList = false;
44#endif
45
Parth Sane56a04712024-04-22 14:21:07 +000046using AidlServiceManager = android::os::IServiceManager;
Devin Moore678984f2024-10-18 22:43:22 +000047using android::os::IAccessor;
48using binder::Status;
Parth Sane56a04712024-04-22 14:21:07 +000049
Devin Moore3c931112024-10-23 17:45:04 +000050static const char* kUnsupportedOpNoServiceManager =
51 "Unsupported operation without a kernel binder servicemanager process";
52
Parth Saneb6ed0eb2024-06-25 14:38:42 +000053static const char* kStaticCachableList[] = {
Parth Saneac492702024-09-18 15:54:16 +000054 // go/keep-sorted start
55 "accessibility",
56 "account",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000057 "activity",
Parth Saneac492702024-09-18 15:54:16 +000058 "alarm",
Parth Sane0105ea52024-10-11 11:41:16 +000059 "android.frameworks.stats.IStats/default",
Parth Saneac492702024-09-18 15:54:16 +000060 "android.system.keystore2.IKeystoreService/default",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000061 "appops",
62 "audio",
Parth Sane0105ea52024-10-11 11:41:16 +000063 "autofill",
64 "batteryproperties",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000065 "batterystats",
Parth Sane0105ea52024-10-11 11:41:16 +000066 "biometic",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000067 "carrier_config",
68 "connectivity",
Parth Saneac492702024-09-18 15:54:16 +000069 "content",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000070 "content_capture",
71 "device_policy",
72 "display",
73 "dropbox",
74 "econtroller",
Parth Saneac492702024-09-18 15:54:16 +000075 "graphicsstats",
76 "input",
77 "input_method",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000078 "isub",
Parth Saneac492702024-09-18 15:54:16 +000079 "jobscheduler",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000080 "legacy_permission",
81 "location",
Parth Sane0105ea52024-10-11 11:41:16 +000082 "lock_settings",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000083 "media.extractor",
84 "media.metrics",
85 "media.player",
86 "media.resource_manager",
Parth Saneac492702024-09-18 15:54:16 +000087 "media_resource_monitor",
88 "mount",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000089 "netd_listener",
90 "netstats",
91 "network_management",
92 "nfc",
Parth Saneac492702024-09-18 15:54:16 +000093 "notification",
94 "package",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000095 "package_native",
96 "performance_hint",
97 "permission",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000098 "permission_checker",
Parth Saneac492702024-09-18 15:54:16 +000099 "permissionmgr",
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000100 "phone",
101 "platform_compat",
102 "power",
Parth Sane0105ea52024-10-11 11:41:16 +0000103 "processinfo",
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000104 "role",
Parth Sane0105ea52024-10-11 11:41:16 +0000105 "sensitive_content_protection_service",
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000106 "sensorservice",
107 "statscompanion",
108 "telephony.registry",
109 "thermalservice",
110 "time_detector",
Parth Sane0105ea52024-10-11 11:41:16 +0000111 "tracing.proxy",
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000112 "trust",
113 "uimode",
Parth Saneac492702024-09-18 15:54:16 +0000114 "user",
Parth Sane0105ea52024-10-11 11:41:16 +0000115 "vibrator",
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000116 "virtualdevice",
117 "virtualdevice_native",
118 "webviewupdate",
Parth Saneac492702024-09-18 15:54:16 +0000119 "window",
120 // go/keep-sorted end
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000121};
122
Parth Sane5e1b7e12024-11-29 10:40:41 +0000123os::ServiceWithMetadata createServiceWithMetadata(const sp<IBinder>& service, bool isLazyService) {
124 os::ServiceWithMetadata out = os::ServiceWithMetadata();
125 out.service = service;
126 out.isLazyService = isLazyService;
127 return out;
128}
129
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000130bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) {
Devin Moore74bc5892024-10-23 17:12:19 +0000131 sp<ProcessState> self = ProcessState::selfOrNull();
132 if (!self || self->getThreadPoolMaxTotalThreadCount() <= 0) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000133 ALOGW("Thread Pool max thread count is 0. Cannot cache binder as linkToDeath cannot be "
134 "implemented. serviceName: %s",
135 serviceName.c_str());
136 return false;
137 }
Parth Saneb109db32024-12-05 19:43:40 +0000138 if (kRemoveStaticList) return true;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000139 for (const char* name : kStaticCachableList) {
140 if (name == serviceName) {
141 return true;
142 }
143 }
144 return false;
145}
146
Devin Moore678984f2024-10-18 22:43:22 +0000147Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
148 const os::Service& service) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000149 if (!kUseCache) {
Devin Moore678984f2024-10-18 22:43:22 +0000150 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000151 }
Parth Sanedc207542024-11-14 11:49:08 +0000152
Parth Sane5e1b7e12024-11-29 10:40:41 +0000153 if (service.getTag() == os::Service::Tag::serviceWithMetadata) {
154 auto serviceWithMetadata = service.get<os::Service::Tag::serviceWithMetadata>();
155 return updateCache(serviceName, serviceWithMetadata.service,
156 serviceWithMetadata.isLazyService);
Parth Sanedc207542024-11-14 11:49:08 +0000157 }
158 return Status::ok();
159}
160
161Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
Parth Sane5e1b7e12024-11-29 10:40:41 +0000162 const sp<IBinder>& binder, bool isServiceLazy) {
Parth Sanea6676ba2024-10-04 14:14:07 +0000163 std::string traceStr;
Parth Sane5e1b7e12024-11-29 10:40:41 +0000164 // Don't cache if service is lazy
165 if (kRemoveStaticList && isServiceLazy) {
166 return Status::ok();
167 }
Parth Sanea6676ba2024-10-04 14:14:07 +0000168 if (atrace_is_tag_enabled(ATRACE_TAG_AIDL)) {
169 traceStr = "BinderCacheWithInvalidation::updateCache : " + serviceName;
170 }
171 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, traceStr.c_str());
Parth Sanedc207542024-11-14 11:49:08 +0000172 if (!binder) {
173 binder::ScopedTrace
174 aidlTrace(ATRACE_TAG_AIDL,
175 "BinderCacheWithInvalidation::updateCache failed: binder_null");
176 } else if (!binder->isBinderAlive()) {
177 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
178 "BinderCacheWithInvalidation::updateCache failed: "
179 "isBinderAlive_false");
Parth Sane5e1b7e12024-11-29 10:40:41 +0000180 }
181 // If we reach here with kRemoveStaticList=true then we know service isn't lazy
Parth Saneb109db32024-12-05 19:43:40 +0000182 else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) {
Parth Sanedc207542024-11-14 11:49:08 +0000183 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
184 "BinderCacheWithInvalidation::updateCache successful");
185 return mCacheForGetService->setItem(serviceName, binder);
186 } else {
187 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
188 "BinderCacheWithInvalidation::updateCache failed: "
189 "caching_not_enabled");
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000190 }
Devin Moore678984f2024-10-18 22:43:22 +0000191 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000192}
193
194bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName,
195 os::Service* _out) {
196 if (!kUseCache) {
197 return false;
198 }
199 sp<IBinder> item = mCacheForGetService->getItem(serviceName);
200 // TODO(b/363177618): Enable caching for binders which are always null.
201 if (item != nullptr && item->isBinderAlive()) {
Parth Sane5e1b7e12024-11-29 10:40:41 +0000202 *_out = createServiceWithMetadata(item, false);
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000203 return true;
204 }
205 return false;
206}
207
Parth Sane56a04712024-04-22 14:21:07 +0000208BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl)
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000209 : mTheRealServiceManager(impl) {
210 mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>();
211}
Parth Sane56a04712024-04-22 14:21:07 +0000212
Devin Moore678984f2024-10-18 22:43:22 +0000213Status BackendUnifiedServiceManager::getService(const ::std::string& name,
214 sp<IBinder>* _aidl_return) {
Alice Wang8578f132024-05-03 09:01:56 +0000215 os::Service service;
Devin Moore678984f2024-10-18 22:43:22 +0000216 Status status = getService2(name, &service);
Parth Sane5e1b7e12024-11-29 10:40:41 +0000217 *_aidl_return = service.get<os::Service::Tag::serviceWithMetadata>().service;
Alice Wang11da1502024-07-25 12:03:22 +0000218 return status;
219}
220
Devin Moore678984f2024-10-18 22:43:22 +0000221Status BackendUnifiedServiceManager::getService2(const ::std::string& name, os::Service* _out) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000222 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000223 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000224 }
Alice Wang11da1502024-07-25 12:03:22 +0000225 os::Service service;
Devin Moore3c931112024-10-23 17:45:04 +0000226 Status status = Status::ok();
227 if (mTheRealServiceManager) {
228 status = mTheRealServiceManager->getService2(name, &service);
229 }
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000230
Devin Moore18f63752024-08-08 21:01:24 +0000231 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000232 status = toBinderService(name, service, _out);
233 if (status.isOk()) {
234 return updateCache(name, service);
235 }
Devin Moore18f63752024-08-08 21:01:24 +0000236 }
Alice Wang8578f132024-05-03 09:01:56 +0000237 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000238}
Alice Wang8578f132024-05-03 09:01:56 +0000239
Devin Moore678984f2024-10-18 22:43:22 +0000240Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000241 os::Service service;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000242 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000243 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000244 }
245
Devin Moore3c931112024-10-23 17:45:04 +0000246 Status status = Status::ok();
247 if (mTheRealServiceManager) {
248 status = mTheRealServiceManager->checkService(name, &service);
249 }
Devin Moore18f63752024-08-08 21:01:24 +0000250 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000251 status = toBinderService(name, service, _out);
252 if (status.isOk()) {
253 return updateCache(name, service);
254 }
Devin Moore18f63752024-08-08 21:01:24 +0000255 }
Alice Wang8578f132024-05-03 09:01:56 +0000256 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000257}
Alice Wang8578f132024-05-03 09:01:56 +0000258
Devin Moore678984f2024-10-18 22:43:22 +0000259Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
260 const os::Service& in, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000261 switch (in.getTag()) {
Parth Sane5e1b7e12024-11-29 10:40:41 +0000262 case os::Service::Tag::serviceWithMetadata: {
263 auto serviceWithMetadata = in.get<os::Service::Tag::serviceWithMetadata>();
264 if (serviceWithMetadata.service == nullptr) {
Devin Moore18f63752024-08-08 21:01:24 +0000265 // failed to find a service. Check to see if we have any local
266 // injected Accessors for this service.
267 os::Service accessor;
Devin Moore678984f2024-10-18 22:43:22 +0000268 Status status = getInjectedAccessor(name, &accessor);
Devin Moore18f63752024-08-08 21:01:24 +0000269 if (!status.isOk()) {
Parth Sane5e1b7e12024-11-29 10:40:41 +0000270 *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
271 createServiceWithMetadata(nullptr, false));
Devin Moore18f63752024-08-08 21:01:24 +0000272 return status;
273 }
274 if (accessor.getTag() == os::Service::Tag::accessor &&
275 accessor.get<os::Service::Tag::accessor>() != nullptr) {
276 ALOGI("Found local injected service for %s, will attempt to create connection",
277 name.c_str());
278 // Call this again using the accessor Service to get the real
279 // service's binder into _out
280 return toBinderService(name, accessor, _out);
281 }
282 }
283
Alice Wang8578f132024-05-03 09:01:56 +0000284 *_out = in;
Devin Moore678984f2024-10-18 22:43:22 +0000285 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000286 }
287 case os::Service::Tag::accessor: {
288 sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>();
289 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
290 if (accessor == nullptr) {
291 ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
Parth Sane5e1b7e12024-11-29 10:40:41 +0000292 *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
293 createServiceWithMetadata(nullptr, false));
Devin Moore678984f2024-10-18 22:43:22 +0000294 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000295 }
296 auto request = [=] {
297 os::ParcelFileDescriptor fd;
Devin Moore678984f2024-10-18 22:43:22 +0000298 Status ret = accessor->addConnection(&fd);
Alice Wang8578f132024-05-03 09:01:56 +0000299 if (ret.isOk()) {
300 return base::unique_fd(fd.release());
301 } else {
302 ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str());
303 return base::unique_fd(-1);
304 }
305 };
306 auto session = RpcSession::make();
Devin Moore18f63752024-08-08 21:01:24 +0000307 status_t status = session->setupPreconnectedClient(base::unique_fd{}, request);
308 if (status != OK) {
309 ALOGE("Failed to set up preconnected binder RPC client: %s",
310 statusToString(status).c_str());
Devin Moore678984f2024-10-18 22:43:22 +0000311 return Status::fromStatusT(status);
Devin Moore18f63752024-08-08 21:01:24 +0000312 }
Alice Wang8578f132024-05-03 09:01:56 +0000313 session->setSessionSpecificRoot(accessorBinder);
Parth Sane5e1b7e12024-11-29 10:40:41 +0000314 *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
315 createServiceWithMetadata(session->getRootObject(), false));
Devin Moore678984f2024-10-18 22:43:22 +0000316 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000317 }
318 default: {
319 LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag());
320 }
321 }
322}
323
Devin Moore678984f2024-10-18 22:43:22 +0000324Status BackendUnifiedServiceManager::addService(const ::std::string& name,
325 const sp<IBinder>& service, bool allowIsolated,
326 int32_t dumpPriority) {
Devin Moore3c931112024-10-23 17:45:04 +0000327 if (mTheRealServiceManager) {
328 Status status =
329 mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
330 // mEnableAddServiceCache is true by default.
331 if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) {
332 return updateCache(name, service,
333 dumpPriority & android::os::IServiceManager::FLAG_IS_LAZY_SERVICE);
334 }
335 return status;
Parth Sanedc207542024-11-14 11:49:08 +0000336 }
Devin Moore3c931112024-10-23 17:45:04 +0000337 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
338 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000339}
Devin Moore678984f2024-10-18 22:43:22 +0000340Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority,
341 ::std::vector<::std::string>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000342 if (mTheRealServiceManager) {
343 return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
344 }
345 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
346 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000347}
Devin Moore678984f2024-10-18 22:43:22 +0000348Status BackendUnifiedServiceManager::registerForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000349 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
Devin Moore3c931112024-10-23 17:45:04 +0000350 if (mTheRealServiceManager) {
351 return mTheRealServiceManager->registerForNotifications(name, callback);
352 }
353 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
354 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000355}
Devin Moore678984f2024-10-18 22:43:22 +0000356Status BackendUnifiedServiceManager::unregisterForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000357 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
Devin Moore3c931112024-10-23 17:45:04 +0000358 if (mTheRealServiceManager) {
359 return mTheRealServiceManager->unregisterForNotifications(name, callback);
360 }
361 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
362 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000363}
Devin Moore678984f2024-10-18 22:43:22 +0000364Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name, bool* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000365 if (mTheRealServiceManager) {
366 return mTheRealServiceManager->isDeclared(name, _aidl_return);
367 }
368 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
369 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000370}
Devin Moore678984f2024-10-18 22:43:22 +0000371Status BackendUnifiedServiceManager::getDeclaredInstances(
Parth Sane56a04712024-04-22 14:21:07 +0000372 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000373 if (mTheRealServiceManager) {
374 return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
375 }
376 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
377 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000378}
Devin Moore678984f2024-10-18 22:43:22 +0000379Status BackendUnifiedServiceManager::updatableViaApex(
Parth Sane56a04712024-04-22 14:21:07 +0000380 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000381 if (mTheRealServiceManager) {
382 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
383 }
384 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
385 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000386}
Devin Moore678984f2024-10-18 22:43:22 +0000387Status BackendUnifiedServiceManager::getUpdatableNames(const ::std::string& apexName,
388 ::std::vector<::std::string>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000389 if (mTheRealServiceManager) {
390 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
391 }
392 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
393 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000394}
Devin Moore678984f2024-10-18 22:43:22 +0000395Status BackendUnifiedServiceManager::getConnectionInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000396 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000397 if (mTheRealServiceManager) {
398 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
399 }
400 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
401 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000402}
Devin Moore678984f2024-10-18 22:43:22 +0000403Status BackendUnifiedServiceManager::registerClientCallback(
Parth Sane56a04712024-04-22 14:21:07 +0000404 const ::std::string& name, const sp<IBinder>& service,
405 const sp<os::IClientCallback>& callback) {
Devin Moore3c931112024-10-23 17:45:04 +0000406 if (mTheRealServiceManager) {
407 return mTheRealServiceManager->registerClientCallback(name, service, callback);
408 }
409 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
410 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000411}
Devin Moore678984f2024-10-18 22:43:22 +0000412Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
413 const sp<IBinder>& service) {
Devin Moore3c931112024-10-23 17:45:04 +0000414 if (mTheRealServiceManager) {
415 return mTheRealServiceManager->tryUnregisterService(name, service);
416 }
417 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
418 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000419}
Devin Moore678984f2024-10-18 22:43:22 +0000420Status BackendUnifiedServiceManager::getServiceDebugInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000421 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000422 if (mTheRealServiceManager) {
423 return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
424 }
425 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
426 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000427}
428
429[[clang::no_destroy]] static std::once_flag gUSmOnce;
430[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
431
Devin Moore3c931112024-10-23 17:45:04 +0000432static bool hasOutOfProcessServiceManager() {
433#ifndef BINDER_WITH_KERNEL_IPC
434 return false;
435#else
436#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
437 return android::base::GetBoolProperty("servicemanager.installed", true);
438#else
439 return true;
440#endif
441#endif // BINDER_WITH_KERNEL_IPC
442}
443
Parth Sane56a04712024-04-22 14:21:07 +0000444sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
445 std::call_once(gUSmOnce, []() {
446#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
Devin Moore3c931112024-10-23 17:45:04 +0000447 /* wait for service manager */
448 if (hasOutOfProcessServiceManager()) {
Parth Sane56a04712024-04-22 14:21:07 +0000449 using std::literals::chrono_literals::operator""s;
450 using android::base::WaitForProperty;
451 while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
452 ALOGE("Waited for servicemanager.ready for a second, waiting another...");
453 }
454 }
455#endif
456
457 sp<AidlServiceManager> sm = nullptr;
Devin Moore3c931112024-10-23 17:45:04 +0000458 while (hasOutOfProcessServiceManager() && sm == nullptr) {
Parth Sane56a04712024-04-22 14:21:07 +0000459 sm = interface_cast<AidlServiceManager>(
460 ProcessState::self()->getContextObject(nullptr));
461 if (sm == nullptr) {
462 ALOGE("Waiting 1s on context object on %s.",
463 ProcessState::self()->getDriverName().c_str());
464 sleep(1);
465 }
466 }
467
468 gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm);
469 });
470
471 return gUnifiedServiceManager;
472}
473
Devin Moore18f63752024-08-08 21:01:24 +0000474} // namespace android