blob: ee3d6af742ed3049c10e4300b0e5df894a96baf0 [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
Devin Moorea3867c72024-10-23 19:29:49 +000018#include <android-base/strings.h>
Alice Wang8578f132024-05-03 09:01:56 +000019#include <android/os/IAccessor.h>
Parth Sane5e1b7e12024-11-29 10:40:41 +000020#include <android/os/IServiceManager.h>
Alice Wang8578f132024-05-03 09:01:56 +000021#include <binder/RpcSession.h>
22
Tomasz Wasilczykfe25f122024-06-26 12:45:57 -070023#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
24#include <android-base/properties.h>
25#endif
26
Parth Sane56a04712024-04-22 14:21:07 +000027namespace android {
28
Parth Saneb6ed0eb2024-06-25 14:38:42 +000029#ifdef LIBBINDER_CLIENT_CACHE
30constexpr bool kUseCache = true;
31#else
32constexpr bool kUseCache = false;
33#endif
34
Parth Sanedc207542024-11-14 11:49:08 +000035#ifdef LIBBINDER_ADDSERVICE_CACHE
36constexpr bool kUseCacheInAddService = true;
37#else
38constexpr bool kUseCacheInAddService = false;
39#endif
40
Parth Sane5e1b7e12024-11-29 10:40:41 +000041#ifdef LIBBINDER_REMOVE_CACHE_STATIC_LIST
42constexpr bool kRemoveStaticList = true;
43#else
44constexpr bool kRemoveStaticList = false;
45#endif
46
Parth Sane56a04712024-04-22 14:21:07 +000047using AidlServiceManager = android::os::IServiceManager;
Devin Moore678984f2024-10-18 22:43:22 +000048using android::os::IAccessor;
49using binder::Status;
Parth Sane56a04712024-04-22 14:21:07 +000050
Devin Moore3c931112024-10-23 17:45:04 +000051static const char* kUnsupportedOpNoServiceManager =
52 "Unsupported operation without a kernel binder servicemanager process";
53
Parth Saneb6ed0eb2024-06-25 14:38:42 +000054static const char* kStaticCachableList[] = {
Parth Saneac492702024-09-18 15:54:16 +000055 // go/keep-sorted start
56 "accessibility",
57 "account",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000058 "activity",
Parth Saneac492702024-09-18 15:54:16 +000059 "alarm",
Parth Sane0105ea52024-10-11 11:41:16 +000060 "android.frameworks.stats.IStats/default",
Parth Saneac492702024-09-18 15:54:16 +000061 "android.system.keystore2.IKeystoreService/default",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000062 "appops",
63 "audio",
Parth Sane0105ea52024-10-11 11:41:16 +000064 "autofill",
65 "batteryproperties",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000066 "batterystats",
Parth Sane0105ea52024-10-11 11:41:16 +000067 "biometic",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000068 "carrier_config",
69 "connectivity",
Parth Saneac492702024-09-18 15:54:16 +000070 "content",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000071 "content_capture",
72 "device_policy",
73 "display",
74 "dropbox",
75 "econtroller",
Parth Saneac492702024-09-18 15:54:16 +000076 "graphicsstats",
77 "input",
78 "input_method",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000079 "isub",
Parth Saneac492702024-09-18 15:54:16 +000080 "jobscheduler",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000081 "legacy_permission",
82 "location",
Parth Sane0105ea52024-10-11 11:41:16 +000083 "lock_settings",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000084 "media.extractor",
85 "media.metrics",
86 "media.player",
87 "media.resource_manager",
Parth Saneac492702024-09-18 15:54:16 +000088 "media_resource_monitor",
89 "mount",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000090 "netd_listener",
91 "netstats",
92 "network_management",
93 "nfc",
Parth Saneac492702024-09-18 15:54:16 +000094 "notification",
95 "package",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000096 "package_native",
97 "performance_hint",
98 "permission",
Parth Saneb6ed0eb2024-06-25 14:38:42 +000099 "permission_checker",
Parth Saneac492702024-09-18 15:54:16 +0000100 "permissionmgr",
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000101 "phone",
102 "platform_compat",
103 "power",
Parth Sane0105ea52024-10-11 11:41:16 +0000104 "processinfo",
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000105 "role",
Parth Sane0105ea52024-10-11 11:41:16 +0000106 "sensitive_content_protection_service",
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000107 "sensorservice",
108 "statscompanion",
109 "telephony.registry",
110 "thermalservice",
111 "time_detector",
Parth Sane0105ea52024-10-11 11:41:16 +0000112 "tracing.proxy",
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000113 "trust",
114 "uimode",
Parth Saneac492702024-09-18 15:54:16 +0000115 "user",
Parth Sane0105ea52024-10-11 11:41:16 +0000116 "vibrator",
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000117 "virtualdevice",
118 "virtualdevice_native",
119 "webviewupdate",
Parth Saneac492702024-09-18 15:54:16 +0000120 "window",
121 // go/keep-sorted end
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000122};
123
Parth Sane5e1b7e12024-11-29 10:40:41 +0000124os::ServiceWithMetadata createServiceWithMetadata(const sp<IBinder>& service, bool isLazyService) {
125 os::ServiceWithMetadata out = os::ServiceWithMetadata();
126 out.service = service;
127 out.isLazyService = isLazyService;
128 return out;
129}
130
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000131bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) {
Devin Moore74bc5892024-10-23 17:12:19 +0000132 sp<ProcessState> self = ProcessState::selfOrNull();
133 if (!self || self->getThreadPoolMaxTotalThreadCount() <= 0) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000134 ALOGW("Thread Pool max thread count is 0. Cannot cache binder as linkToDeath cannot be "
135 "implemented. serviceName: %s",
136 serviceName.c_str());
137 return false;
138 }
Parth Saneb109db32024-12-05 19:43:40 +0000139 if (kRemoveStaticList) return true;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000140 for (const char* name : kStaticCachableList) {
141 if (name == serviceName) {
142 return true;
143 }
144 }
145 return false;
146}
147
Devin Moore678984f2024-10-18 22:43:22 +0000148Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
149 const os::Service& service) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000150 if (!kUseCache) {
Devin Moore678984f2024-10-18 22:43:22 +0000151 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000152 }
Parth Sanedc207542024-11-14 11:49:08 +0000153
Parth Sane5e1b7e12024-11-29 10:40:41 +0000154 if (service.getTag() == os::Service::Tag::serviceWithMetadata) {
155 auto serviceWithMetadata = service.get<os::Service::Tag::serviceWithMetadata>();
156 return updateCache(serviceName, serviceWithMetadata.service,
157 serviceWithMetadata.isLazyService);
Parth Sanedc207542024-11-14 11:49:08 +0000158 }
159 return Status::ok();
160}
161
162Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
Parth Sane5e1b7e12024-11-29 10:40:41 +0000163 const sp<IBinder>& binder, bool isServiceLazy) {
Parth Sanea6676ba2024-10-04 14:14:07 +0000164 std::string traceStr;
Parth Sane5e1b7e12024-11-29 10:40:41 +0000165 // Don't cache if service is lazy
166 if (kRemoveStaticList && isServiceLazy) {
167 return Status::ok();
168 }
Parth Sanea6676ba2024-10-04 14:14:07 +0000169 if (atrace_is_tag_enabled(ATRACE_TAG_AIDL)) {
170 traceStr = "BinderCacheWithInvalidation::updateCache : " + serviceName;
171 }
172 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, traceStr.c_str());
Parth Sanedc207542024-11-14 11:49:08 +0000173 if (!binder) {
174 binder::ScopedTrace
175 aidlTrace(ATRACE_TAG_AIDL,
176 "BinderCacheWithInvalidation::updateCache failed: binder_null");
177 } else if (!binder->isBinderAlive()) {
178 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
179 "BinderCacheWithInvalidation::updateCache failed: "
180 "isBinderAlive_false");
Parth Sane5e1b7e12024-11-29 10:40:41 +0000181 }
182 // If we reach here with kRemoveStaticList=true then we know service isn't lazy
Parth Saneb109db32024-12-05 19:43:40 +0000183 else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) {
Parth Sanedc207542024-11-14 11:49:08 +0000184 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
185 "BinderCacheWithInvalidation::updateCache successful");
186 return mCacheForGetService->setItem(serviceName, binder);
187 } else {
188 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
189 "BinderCacheWithInvalidation::updateCache failed: "
190 "caching_not_enabled");
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000191 }
Devin Moore678984f2024-10-18 22:43:22 +0000192 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000193}
194
195bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName,
196 os::Service* _out) {
197 if (!kUseCache) {
198 return false;
199 }
200 sp<IBinder> item = mCacheForGetService->getItem(serviceName);
201 // TODO(b/363177618): Enable caching for binders which are always null.
202 if (item != nullptr && item->isBinderAlive()) {
Parth Sane5e1b7e12024-11-29 10:40:41 +0000203 *_out = createServiceWithMetadata(item, false);
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000204 return true;
205 }
206 return false;
207}
208
Parth Sane56a04712024-04-22 14:21:07 +0000209BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl)
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000210 : mTheRealServiceManager(impl) {
211 mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>();
212}
Parth Sane56a04712024-04-22 14:21:07 +0000213
Devin Moore678984f2024-10-18 22:43:22 +0000214Status BackendUnifiedServiceManager::getService(const ::std::string& name,
215 sp<IBinder>* _aidl_return) {
Alice Wang8578f132024-05-03 09:01:56 +0000216 os::Service service;
Devin Moore678984f2024-10-18 22:43:22 +0000217 Status status = getService2(name, &service);
Parth Sane5e1b7e12024-11-29 10:40:41 +0000218 *_aidl_return = service.get<os::Service::Tag::serviceWithMetadata>().service;
Alice Wang11da1502024-07-25 12:03:22 +0000219 return status;
220}
221
Devin Moore678984f2024-10-18 22:43:22 +0000222Status BackendUnifiedServiceManager::getService2(const ::std::string& name, os::Service* _out) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000223 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000224 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000225 }
Alice Wang11da1502024-07-25 12:03:22 +0000226 os::Service service;
Devin Moore3c931112024-10-23 17:45:04 +0000227 Status status = Status::ok();
228 if (mTheRealServiceManager) {
229 status = mTheRealServiceManager->getService2(name, &service);
230 }
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000231
Devin Moore18f63752024-08-08 21:01:24 +0000232 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000233 status = toBinderService(name, service, _out);
234 if (status.isOk()) {
235 return updateCache(name, service);
236 }
Devin Moore18f63752024-08-08 21:01:24 +0000237 }
Alice Wang8578f132024-05-03 09:01:56 +0000238 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000239}
Alice Wang8578f132024-05-03 09:01:56 +0000240
Devin Moore678984f2024-10-18 22:43:22 +0000241Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000242 os::Service service;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000243 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000244 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000245 }
246
Devin Moore3c931112024-10-23 17:45:04 +0000247 Status status = Status::ok();
248 if (mTheRealServiceManager) {
249 status = mTheRealServiceManager->checkService(name, &service);
250 }
Devin Moore18f63752024-08-08 21:01:24 +0000251 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000252 status = toBinderService(name, service, _out);
253 if (status.isOk()) {
254 return updateCache(name, service);
255 }
Devin Moore18f63752024-08-08 21:01:24 +0000256 }
Alice Wang8578f132024-05-03 09:01:56 +0000257 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000258}
Alice Wang8578f132024-05-03 09:01:56 +0000259
Devin Moore678984f2024-10-18 22:43:22 +0000260Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
261 const os::Service& in, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000262 switch (in.getTag()) {
Parth Sane5e1b7e12024-11-29 10:40:41 +0000263 case os::Service::Tag::serviceWithMetadata: {
264 auto serviceWithMetadata = in.get<os::Service::Tag::serviceWithMetadata>();
265 if (serviceWithMetadata.service == nullptr) {
Devin Moore18f63752024-08-08 21:01:24 +0000266 // failed to find a service. Check to see if we have any local
267 // injected Accessors for this service.
268 os::Service accessor;
Devin Moore678984f2024-10-18 22:43:22 +0000269 Status status = getInjectedAccessor(name, &accessor);
Devin Moore18f63752024-08-08 21:01:24 +0000270 if (!status.isOk()) {
Parth Sane5e1b7e12024-11-29 10:40:41 +0000271 *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
272 createServiceWithMetadata(nullptr, false));
Devin Moore18f63752024-08-08 21:01:24 +0000273 return status;
274 }
275 if (accessor.getTag() == os::Service::Tag::accessor &&
276 accessor.get<os::Service::Tag::accessor>() != nullptr) {
277 ALOGI("Found local injected service for %s, will attempt to create connection",
278 name.c_str());
279 // Call this again using the accessor Service to get the real
280 // service's binder into _out
281 return toBinderService(name, accessor, _out);
282 }
283 }
284
Alice Wang8578f132024-05-03 09:01:56 +0000285 *_out = in;
Devin Moore678984f2024-10-18 22:43:22 +0000286 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000287 }
288 case os::Service::Tag::accessor: {
289 sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>();
290 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
291 if (accessor == nullptr) {
292 ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
Parth Sane5e1b7e12024-11-29 10:40:41 +0000293 *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
294 createServiceWithMetadata(nullptr, false));
Devin Moore678984f2024-10-18 22:43:22 +0000295 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000296 }
297 auto request = [=] {
298 os::ParcelFileDescriptor fd;
Devin Moore678984f2024-10-18 22:43:22 +0000299 Status ret = accessor->addConnection(&fd);
Alice Wang8578f132024-05-03 09:01:56 +0000300 if (ret.isOk()) {
301 return base::unique_fd(fd.release());
302 } else {
303 ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str());
304 return base::unique_fd(-1);
305 }
306 };
307 auto session = RpcSession::make();
Devin Moore18f63752024-08-08 21:01:24 +0000308 status_t status = session->setupPreconnectedClient(base::unique_fd{}, request);
309 if (status != OK) {
310 ALOGE("Failed to set up preconnected binder RPC client: %s",
311 statusToString(status).c_str());
Devin Moore678984f2024-10-18 22:43:22 +0000312 return Status::fromStatusT(status);
Devin Moore18f63752024-08-08 21:01:24 +0000313 }
Alice Wang8578f132024-05-03 09:01:56 +0000314 session->setSessionSpecificRoot(accessorBinder);
Parth Sane5e1b7e12024-11-29 10:40:41 +0000315 *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
316 createServiceWithMetadata(session->getRootObject(), false));
Devin Moore678984f2024-10-18 22:43:22 +0000317 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000318 }
319 default: {
320 LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag());
321 }
322 }
323}
324
Devin Moore678984f2024-10-18 22:43:22 +0000325Status BackendUnifiedServiceManager::addService(const ::std::string& name,
326 const sp<IBinder>& service, bool allowIsolated,
327 int32_t dumpPriority) {
Devin Moore3c931112024-10-23 17:45:04 +0000328 if (mTheRealServiceManager) {
329 Status status =
330 mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
331 // mEnableAddServiceCache is true by default.
332 if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) {
333 return updateCache(name, service,
334 dumpPriority & android::os::IServiceManager::FLAG_IS_LAZY_SERVICE);
335 }
336 return status;
Parth Sanedc207542024-11-14 11:49:08 +0000337 }
Devin Moore3c931112024-10-23 17:45:04 +0000338 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
339 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000340}
Devin Moore678984f2024-10-18 22:43:22 +0000341Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority,
342 ::std::vector<::std::string>* _aidl_return) {
Devin Moorea3867c72024-10-23 19:29:49 +0000343 Status status = Status::ok();
Devin Moore3c931112024-10-23 17:45:04 +0000344 if (mTheRealServiceManager) {
Devin Moorea3867c72024-10-23 19:29:49 +0000345 status = mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
Devin Moore3c931112024-10-23 17:45:04 +0000346 }
Devin Moorea3867c72024-10-23 19:29:49 +0000347 if (!status.isOk()) return status;
348
349 appendInjectedAccessorServices(_aidl_return);
350
351 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000352}
Devin Moore678984f2024-10-18 22:43:22 +0000353Status BackendUnifiedServiceManager::registerForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000354 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
Devin Moore3c931112024-10-23 17:45:04 +0000355 if (mTheRealServiceManager) {
356 return mTheRealServiceManager->registerForNotifications(name, callback);
357 }
358 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
359 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000360}
Devin Moore678984f2024-10-18 22:43:22 +0000361Status BackendUnifiedServiceManager::unregisterForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000362 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
Devin Moore3c931112024-10-23 17:45:04 +0000363 if (mTheRealServiceManager) {
364 return mTheRealServiceManager->unregisterForNotifications(name, callback);
365 }
366 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
367 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000368}
Devin Moore678984f2024-10-18 22:43:22 +0000369Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name, bool* _aidl_return) {
Devin Moorea3867c72024-10-23 19:29:49 +0000370 Status status = Status::ok();
Devin Moore3c931112024-10-23 17:45:04 +0000371 if (mTheRealServiceManager) {
Devin Moorea3867c72024-10-23 19:29:49 +0000372 status = mTheRealServiceManager->isDeclared(name, _aidl_return);
Devin Moore3c931112024-10-23 17:45:04 +0000373 }
Devin Moorea3867c72024-10-23 19:29:49 +0000374 if (!status.isOk()) return status;
375
376 if (!*_aidl_return) {
377 forEachInjectedAccessorService([&](const std::string& instance) {
378 if (name == instance) {
379 *_aidl_return = true;
380 }
381 });
382 }
383
384 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000385}
Devin Moore678984f2024-10-18 22:43:22 +0000386Status BackendUnifiedServiceManager::getDeclaredInstances(
Parth Sane56a04712024-04-22 14:21:07 +0000387 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
Devin Moorea3867c72024-10-23 19:29:49 +0000388 Status status = Status::ok();
Devin Moore3c931112024-10-23 17:45:04 +0000389 if (mTheRealServiceManager) {
Devin Moorea3867c72024-10-23 19:29:49 +0000390 status = mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
Devin Moore3c931112024-10-23 17:45:04 +0000391 }
Devin Moorea3867c72024-10-23 19:29:49 +0000392 if (!status.isOk()) return status;
393
394 forEachInjectedAccessorService([&](const std::string& instance) {
395 // Declared instances have the format
396 // <interface>/instance like foo.bar.ISomething/instance
397 // If it does not have that format, consider the instance to be ""
398 std::string_view name(instance);
399 if (base::ConsumePrefix(&name, iface + "/")) {
400 _aidl_return->emplace_back(name);
401 } else if (iface == instance) {
402 _aidl_return->push_back("");
403 }
404 });
405
406 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000407}
Devin Moore678984f2024-10-18 22:43:22 +0000408Status BackendUnifiedServiceManager::updatableViaApex(
Parth Sane56a04712024-04-22 14:21:07 +0000409 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000410 if (mTheRealServiceManager) {
411 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
412 }
413 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
414 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000415}
Devin Moore678984f2024-10-18 22:43:22 +0000416Status BackendUnifiedServiceManager::getUpdatableNames(const ::std::string& apexName,
417 ::std::vector<::std::string>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000418 if (mTheRealServiceManager) {
419 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
420 }
421 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
422 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000423}
Devin Moore678984f2024-10-18 22:43:22 +0000424Status BackendUnifiedServiceManager::getConnectionInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000425 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000426 if (mTheRealServiceManager) {
427 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
428 }
429 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
430 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000431}
Devin Moore678984f2024-10-18 22:43:22 +0000432Status BackendUnifiedServiceManager::registerClientCallback(
Parth Sane56a04712024-04-22 14:21:07 +0000433 const ::std::string& name, const sp<IBinder>& service,
434 const sp<os::IClientCallback>& callback) {
Devin Moore3c931112024-10-23 17:45:04 +0000435 if (mTheRealServiceManager) {
436 return mTheRealServiceManager->registerClientCallback(name, service, callback);
437 }
438 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
439 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000440}
Devin Moore678984f2024-10-18 22:43:22 +0000441Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
442 const sp<IBinder>& service) {
Devin Moore3c931112024-10-23 17:45:04 +0000443 if (mTheRealServiceManager) {
444 return mTheRealServiceManager->tryUnregisterService(name, service);
445 }
446 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
447 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000448}
Devin Moore678984f2024-10-18 22:43:22 +0000449Status BackendUnifiedServiceManager::getServiceDebugInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000450 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000451 if (mTheRealServiceManager) {
452 return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
453 }
454 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
455 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000456}
457
458[[clang::no_destroy]] static std::once_flag gUSmOnce;
459[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
460
Devin Moore3c931112024-10-23 17:45:04 +0000461static bool hasOutOfProcessServiceManager() {
462#ifndef BINDER_WITH_KERNEL_IPC
463 return false;
464#else
465#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
466 return android::base::GetBoolProperty("servicemanager.installed", true);
467#else
468 return true;
469#endif
470#endif // BINDER_WITH_KERNEL_IPC
471}
472
Parth Sane56a04712024-04-22 14:21:07 +0000473sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
474 std::call_once(gUSmOnce, []() {
475#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
Devin Moore3c931112024-10-23 17:45:04 +0000476 /* wait for service manager */
477 if (hasOutOfProcessServiceManager()) {
Parth Sane56a04712024-04-22 14:21:07 +0000478 using std::literals::chrono_literals::operator""s;
479 using android::base::WaitForProperty;
480 while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
481 ALOGE("Waited for servicemanager.ready for a second, waiting another...");
482 }
483 }
484#endif
485
486 sp<AidlServiceManager> sm = nullptr;
Devin Moore3c931112024-10-23 17:45:04 +0000487 while (hasOutOfProcessServiceManager() && sm == nullptr) {
Parth Sane56a04712024-04-22 14:21:07 +0000488 sm = interface_cast<AidlServiceManager>(
489 ProcessState::self()->getContextObject(nullptr));
490 if (sm == nullptr) {
491 ALOGE("Waiting 1s on context object on %s.",
492 ProcessState::self()->getDriverName().c_str());
493 sleep(1);
494 }
495 }
496
497 gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm);
498 });
499
500 return gUnifiedServiceManager;
501}
502
Devin Moore18f63752024-08-08 21:01:24 +0000503} // namespace android