blob: b1c8994b05f0824fa060fd2aaac83dd70d8631c3 [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();
Parth Sanedb049232025-03-10 06:58:45 -0800133 // Should not cache if process state could not be found, or if thread pool
134 // max could is not greater than zero.
135 if (!self) {
136 ALOGW("Service retrieved before binder threads started. If they are to be started, "
137 "consider starting binder threads earlier.");
138 return false;
139 } else if (self->getThreadPoolMaxTotalThreadCount() <= 0) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000140 ALOGW("Thread Pool max thread count is 0. Cannot cache binder as linkToDeath cannot be "
141 "implemented. serviceName: %s",
142 serviceName.c_str());
143 return false;
144 }
Parth Saneb109db32024-12-05 19:43:40 +0000145 if (kRemoveStaticList) return true;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000146 for (const char* name : kStaticCachableList) {
147 if (name == serviceName) {
148 return true;
149 }
150 }
151 return false;
152}
153
Devin Moore678984f2024-10-18 22:43:22 +0000154Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
155 const os::Service& service) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000156 if (!kUseCache) {
Devin Moore678984f2024-10-18 22:43:22 +0000157 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000158 }
Parth Sanedc207542024-11-14 11:49:08 +0000159
Parth Sane5e1b7e12024-11-29 10:40:41 +0000160 if (service.getTag() == os::Service::Tag::serviceWithMetadata) {
161 auto serviceWithMetadata = service.get<os::Service::Tag::serviceWithMetadata>();
162 return updateCache(serviceName, serviceWithMetadata.service,
163 serviceWithMetadata.isLazyService);
Parth Sanedc207542024-11-14 11:49:08 +0000164 }
165 return Status::ok();
166}
167
168Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
Parth Sane5e1b7e12024-11-29 10:40:41 +0000169 const sp<IBinder>& binder, bool isServiceLazy) {
Parth Sanea6676ba2024-10-04 14:14:07 +0000170 std::string traceStr;
Parth Sane5e1b7e12024-11-29 10:40:41 +0000171 // Don't cache if service is lazy
172 if (kRemoveStaticList && isServiceLazy) {
173 return Status::ok();
174 }
Parth Sanea6676ba2024-10-04 14:14:07 +0000175 if (atrace_is_tag_enabled(ATRACE_TAG_AIDL)) {
176 traceStr = "BinderCacheWithInvalidation::updateCache : " + serviceName;
177 }
178 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, traceStr.c_str());
Parth Sanedc207542024-11-14 11:49:08 +0000179 if (!binder) {
180 binder::ScopedTrace
181 aidlTrace(ATRACE_TAG_AIDL,
182 "BinderCacheWithInvalidation::updateCache failed: binder_null");
183 } else if (!binder->isBinderAlive()) {
184 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
185 "BinderCacheWithInvalidation::updateCache failed: "
186 "isBinderAlive_false");
Parth Sane5e1b7e12024-11-29 10:40:41 +0000187 }
188 // If we reach here with kRemoveStaticList=true then we know service isn't lazy
Parth Saneb109db32024-12-05 19:43:40 +0000189 else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) {
Parth Sanedc207542024-11-14 11:49:08 +0000190 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
191 "BinderCacheWithInvalidation::updateCache successful");
192 return mCacheForGetService->setItem(serviceName, binder);
193 } else {
194 binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
195 "BinderCacheWithInvalidation::updateCache failed: "
196 "caching_not_enabled");
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000197 }
Devin Moore678984f2024-10-18 22:43:22 +0000198 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000199}
200
201bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName,
202 os::Service* _out) {
203 if (!kUseCache) {
204 return false;
205 }
206 sp<IBinder> item = mCacheForGetService->getItem(serviceName);
207 // TODO(b/363177618): Enable caching for binders which are always null.
208 if (item != nullptr && item->isBinderAlive()) {
Parth Sane5e1b7e12024-11-29 10:40:41 +0000209 *_out = createServiceWithMetadata(item, false);
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000210 return true;
211 }
212 return false;
213}
214
Parth Sane56a04712024-04-22 14:21:07 +0000215BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl)
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000216 : mTheRealServiceManager(impl) {
217 mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>();
218}
Parth Sane56a04712024-04-22 14:21:07 +0000219
Devin Moore678984f2024-10-18 22:43:22 +0000220Status BackendUnifiedServiceManager::getService(const ::std::string& name,
221 sp<IBinder>* _aidl_return) {
Alice Wang8578f132024-05-03 09:01:56 +0000222 os::Service service;
Devin Moore678984f2024-10-18 22:43:22 +0000223 Status status = getService2(name, &service);
Alice Wang0aa07a12025-01-08 08:59:49 +0000224 if (status.isOk()) {
225 *_aidl_return = service.get<os::Service::Tag::serviceWithMetadata>().service;
226 }
Alice Wang11da1502024-07-25 12:03:22 +0000227 return status;
228}
229
Devin Moore678984f2024-10-18 22:43:22 +0000230Status BackendUnifiedServiceManager::getService2(const ::std::string& name, os::Service* _out) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000231 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000232 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000233 }
Alice Wang11da1502024-07-25 12:03:22 +0000234 os::Service service;
Devin Moore3c931112024-10-23 17:45:04 +0000235 Status status = Status::ok();
236 if (mTheRealServiceManager) {
237 status = mTheRealServiceManager->getService2(name, &service);
238 }
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000239
Devin Moore18f63752024-08-08 21:01:24 +0000240 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000241 status = toBinderService(name, service, _out);
242 if (status.isOk()) {
243 return updateCache(name, service);
244 }
Devin Moore18f63752024-08-08 21:01:24 +0000245 }
Alice Wang8578f132024-05-03 09:01:56 +0000246 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000247}
Alice Wang8578f132024-05-03 09:01:56 +0000248
Alice Wang2b613442025-01-02 11:53:05 +0000249Status BackendUnifiedServiceManager::checkService(const ::std::string& name,
250 sp<IBinder>* _aidl_return) {
251 os::Service service;
252 Status status = checkService2(name, &service);
253 if (status.isOk()) {
254 *_aidl_return = service.get<os::Service::Tag::serviceWithMetadata>().service;
255 }
256 return status;
257}
258
259Status BackendUnifiedServiceManager::checkService2(const ::std::string& name, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000260 os::Service service;
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000261 if (returnIfCached(name, _out)) {
Devin Moore678984f2024-10-18 22:43:22 +0000262 return Status::ok();
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000263 }
264
Devin Moore3c931112024-10-23 17:45:04 +0000265 Status status = Status::ok();
266 if (mTheRealServiceManager) {
Alice Wang2b613442025-01-02 11:53:05 +0000267 status = mTheRealServiceManager->checkService2(name, &service);
Devin Moore3c931112024-10-23 17:45:04 +0000268 }
Devin Moore18f63752024-08-08 21:01:24 +0000269 if (status.isOk()) {
Parth Saneb6ed0eb2024-06-25 14:38:42 +0000270 status = toBinderService(name, service, _out);
271 if (status.isOk()) {
272 return updateCache(name, service);
273 }
Devin Moore18f63752024-08-08 21:01:24 +0000274 }
Alice Wang8578f132024-05-03 09:01:56 +0000275 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000276}
Alice Wang8578f132024-05-03 09:01:56 +0000277
Devin Moore678984f2024-10-18 22:43:22 +0000278Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
279 const os::Service& in, os::Service* _out) {
Alice Wang8578f132024-05-03 09:01:56 +0000280 switch (in.getTag()) {
Parth Sane5e1b7e12024-11-29 10:40:41 +0000281 case os::Service::Tag::serviceWithMetadata: {
282 auto serviceWithMetadata = in.get<os::Service::Tag::serviceWithMetadata>();
283 if (serviceWithMetadata.service == nullptr) {
Devin Moore18f63752024-08-08 21:01:24 +0000284 // failed to find a service. Check to see if we have any local
285 // injected Accessors for this service.
286 os::Service accessor;
Devin Moore678984f2024-10-18 22:43:22 +0000287 Status status = getInjectedAccessor(name, &accessor);
Devin Moore18f63752024-08-08 21:01:24 +0000288 if (!status.isOk()) {
Parth Sane5e1b7e12024-11-29 10:40:41 +0000289 *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
290 createServiceWithMetadata(nullptr, false));
Devin Moore18f63752024-08-08 21:01:24 +0000291 return status;
292 }
293 if (accessor.getTag() == os::Service::Tag::accessor &&
294 accessor.get<os::Service::Tag::accessor>() != nullptr) {
295 ALOGI("Found local injected service for %s, will attempt to create connection",
296 name.c_str());
297 // Call this again using the accessor Service to get the real
298 // service's binder into _out
299 return toBinderService(name, accessor, _out);
300 }
301 }
302
Alice Wang8578f132024-05-03 09:01:56 +0000303 *_out = in;
Devin Moore678984f2024-10-18 22:43:22 +0000304 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000305 }
306 case os::Service::Tag::accessor: {
307 sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>();
308 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
309 if (accessor == nullptr) {
310 ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
Parth Sane5e1b7e12024-11-29 10:40:41 +0000311 *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
312 createServiceWithMetadata(nullptr, false));
Devin Moore678984f2024-10-18 22:43:22 +0000313 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000314 }
315 auto request = [=] {
316 os::ParcelFileDescriptor fd;
Devin Moore678984f2024-10-18 22:43:22 +0000317 Status ret = accessor->addConnection(&fd);
Alice Wang8578f132024-05-03 09:01:56 +0000318 if (ret.isOk()) {
319 return base::unique_fd(fd.release());
320 } else {
321 ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str());
322 return base::unique_fd(-1);
323 }
324 };
325 auto session = RpcSession::make();
Devin Moore18f63752024-08-08 21:01:24 +0000326 status_t status = session->setupPreconnectedClient(base::unique_fd{}, request);
327 if (status != OK) {
328 ALOGE("Failed to set up preconnected binder RPC client: %s",
329 statusToString(status).c_str());
Devin Moore678984f2024-10-18 22:43:22 +0000330 return Status::fromStatusT(status);
Devin Moore18f63752024-08-08 21:01:24 +0000331 }
Alice Wang8578f132024-05-03 09:01:56 +0000332 session->setSessionSpecificRoot(accessorBinder);
Parth Sane5e1b7e12024-11-29 10:40:41 +0000333 *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
334 createServiceWithMetadata(session->getRootObject(), false));
Devin Moore678984f2024-10-18 22:43:22 +0000335 return Status::ok();
Alice Wang8578f132024-05-03 09:01:56 +0000336 }
337 default: {
338 LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag());
339 }
340 }
341}
342
Devin Moore678984f2024-10-18 22:43:22 +0000343Status BackendUnifiedServiceManager::addService(const ::std::string& name,
344 const sp<IBinder>& service, bool allowIsolated,
345 int32_t dumpPriority) {
Devin Moore3c931112024-10-23 17:45:04 +0000346 if (mTheRealServiceManager) {
347 Status status =
348 mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
349 // mEnableAddServiceCache is true by default.
350 if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) {
351 return updateCache(name, service,
352 dumpPriority & android::os::IServiceManager::FLAG_IS_LAZY_SERVICE);
353 }
354 return status;
Parth Sanedc207542024-11-14 11:49:08 +0000355 }
Devin Moore3c931112024-10-23 17:45:04 +0000356 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
357 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000358}
Devin Moore678984f2024-10-18 22:43:22 +0000359Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority,
360 ::std::vector<::std::string>* _aidl_return) {
Devin Moorea3867c72024-10-23 19:29:49 +0000361 Status status = Status::ok();
Devin Moore3c931112024-10-23 17:45:04 +0000362 if (mTheRealServiceManager) {
Devin Moorea3867c72024-10-23 19:29:49 +0000363 status = mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
Devin Moore3c931112024-10-23 17:45:04 +0000364 }
Devin Moorea3867c72024-10-23 19:29:49 +0000365 if (!status.isOk()) return status;
366
367 appendInjectedAccessorServices(_aidl_return);
368
369 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000370}
Devin Moore678984f2024-10-18 22:43:22 +0000371Status BackendUnifiedServiceManager::registerForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000372 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
Devin Moore3c931112024-10-23 17:45:04 +0000373 if (mTheRealServiceManager) {
374 return mTheRealServiceManager->registerForNotifications(name, callback);
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::unregisterForNotifications(
Parth Sane56a04712024-04-22 14:21:07 +0000380 const ::std::string& name, const sp<os::IServiceCallback>& callback) {
Devin Moore3c931112024-10-23 17:45:04 +0000381 if (mTheRealServiceManager) {
382 return mTheRealServiceManager->unregisterForNotifications(name, callback);
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::isDeclared(const ::std::string& name, bool* _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->isDeclared(name, _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 if (!*_aidl_return) {
395 forEachInjectedAccessorService([&](const std::string& instance) {
396 if (name == instance) {
397 *_aidl_return = true;
398 }
399 });
400 }
401
402 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000403}
Devin Moore678984f2024-10-18 22:43:22 +0000404Status BackendUnifiedServiceManager::getDeclaredInstances(
Parth Sane56a04712024-04-22 14:21:07 +0000405 const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
Devin Moorea3867c72024-10-23 19:29:49 +0000406 Status status = Status::ok();
Devin Moore3c931112024-10-23 17:45:04 +0000407 if (mTheRealServiceManager) {
Devin Moorea3867c72024-10-23 19:29:49 +0000408 status = mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
Devin Moore3c931112024-10-23 17:45:04 +0000409 }
Devin Moorea3867c72024-10-23 19:29:49 +0000410 if (!status.isOk()) return status;
411
412 forEachInjectedAccessorService([&](const std::string& instance) {
413 // Declared instances have the format
414 // <interface>/instance like foo.bar.ISomething/instance
415 // If it does not have that format, consider the instance to be ""
416 std::string_view name(instance);
417 if (base::ConsumePrefix(&name, iface + "/")) {
418 _aidl_return->emplace_back(name);
419 } else if (iface == instance) {
420 _aidl_return->push_back("");
421 }
422 });
423
424 return status;
Parth Sane56a04712024-04-22 14:21:07 +0000425}
Devin Moore678984f2024-10-18 22:43:22 +0000426Status BackendUnifiedServiceManager::updatableViaApex(
Parth Sane56a04712024-04-22 14:21:07 +0000427 const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000428 if (mTheRealServiceManager) {
429 return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
430 }
431 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
432 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000433}
Devin Moore678984f2024-10-18 22:43:22 +0000434Status BackendUnifiedServiceManager::getUpdatableNames(const ::std::string& apexName,
435 ::std::vector<::std::string>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000436 if (mTheRealServiceManager) {
437 return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
438 }
439 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
440 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000441}
Devin Moore678984f2024-10-18 22:43:22 +0000442Status BackendUnifiedServiceManager::getConnectionInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000443 const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000444 if (mTheRealServiceManager) {
445 return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
446 }
447 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
448 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000449}
Devin Moore678984f2024-10-18 22:43:22 +0000450Status BackendUnifiedServiceManager::registerClientCallback(
Parth Sane56a04712024-04-22 14:21:07 +0000451 const ::std::string& name, const sp<IBinder>& service,
452 const sp<os::IClientCallback>& callback) {
Devin Moore3c931112024-10-23 17:45:04 +0000453 if (mTheRealServiceManager) {
454 return mTheRealServiceManager->registerClientCallback(name, service, callback);
455 }
456 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
457 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000458}
Devin Moore678984f2024-10-18 22:43:22 +0000459Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
460 const sp<IBinder>& service) {
Devin Moore3c931112024-10-23 17:45:04 +0000461 if (mTheRealServiceManager) {
462 return mTheRealServiceManager->tryUnregisterService(name, service);
463 }
464 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
465 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000466}
Devin Moore678984f2024-10-18 22:43:22 +0000467Status BackendUnifiedServiceManager::getServiceDebugInfo(
Parth Sane56a04712024-04-22 14:21:07 +0000468 ::std::vector<os::ServiceDebugInfo>* _aidl_return) {
Devin Moore3c931112024-10-23 17:45:04 +0000469 if (mTheRealServiceManager) {
470 return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
471 }
472 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
473 kUnsupportedOpNoServiceManager);
Parth Sane56a04712024-04-22 14:21:07 +0000474}
475
476[[clang::no_destroy]] static std::once_flag gUSmOnce;
477[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
478
Devin Moore3c931112024-10-23 17:45:04 +0000479static bool hasOutOfProcessServiceManager() {
480#ifndef BINDER_WITH_KERNEL_IPC
481 return false;
482#else
483#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
484 return android::base::GetBoolProperty("servicemanager.installed", true);
485#else
486 return true;
487#endif
488#endif // BINDER_WITH_KERNEL_IPC
489}
490
Parth Sane56a04712024-04-22 14:21:07 +0000491sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
492 std::call_once(gUSmOnce, []() {
493#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
Devin Moore3c931112024-10-23 17:45:04 +0000494 /* wait for service manager */
495 if (hasOutOfProcessServiceManager()) {
Parth Sane56a04712024-04-22 14:21:07 +0000496 using std::literals::chrono_literals::operator""s;
497 using android::base::WaitForProperty;
498 while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
499 ALOGE("Waited for servicemanager.ready for a second, waiting another...");
500 }
501 }
502#endif
503
504 sp<AidlServiceManager> sm = nullptr;
Devin Moore3c931112024-10-23 17:45:04 +0000505 while (hasOutOfProcessServiceManager() && sm == nullptr) {
Parth Sane56a04712024-04-22 14:21:07 +0000506 sm = interface_cast<AidlServiceManager>(
507 ProcessState::self()->getContextObject(nullptr));
508 if (sm == nullptr) {
509 ALOGE("Waiting 1s on context object on %s.",
510 ProcessState::self()->getDriverName().c_str());
511 sleep(1);
512 }
513 }
514
515 gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm);
516 });
517
518 return gUnifiedServiceManager;
519}
520
Devin Moore18f63752024-08-08 21:01:24 +0000521} // namespace android