Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 1 | /* |
| 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 Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 18 | #include <android/os/IAccessor.h> |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 19 | #include <android/os/IServiceManager.h> |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 20 | #include <binder/RpcSession.h> |
| 21 | |
Tomasz Wasilczyk | fe25f12 | 2024-06-26 12:45:57 -0700 | [diff] [blame] | 22 | #if defined(__BIONIC__) && !defined(__ANDROID_VNDK__) |
| 23 | #include <android-base/properties.h> |
| 24 | #endif |
| 25 | |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 28 | #ifdef LIBBINDER_CLIENT_CACHE |
| 29 | constexpr bool kUseCache = true; |
| 30 | #else |
| 31 | constexpr bool kUseCache = false; |
| 32 | #endif |
| 33 | |
Parth Sane | dc20754 | 2024-11-14 11:49:08 +0000 | [diff] [blame] | 34 | #ifdef LIBBINDER_ADDSERVICE_CACHE |
| 35 | constexpr bool kUseCacheInAddService = true; |
| 36 | #else |
| 37 | constexpr bool kUseCacheInAddService = false; |
| 38 | #endif |
| 39 | |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 40 | #ifdef LIBBINDER_REMOVE_CACHE_STATIC_LIST |
| 41 | constexpr bool kRemoveStaticList = true; |
| 42 | #else |
| 43 | constexpr bool kRemoveStaticList = false; |
| 44 | #endif |
| 45 | |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 46 | using AidlServiceManager = android::os::IServiceManager; |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 47 | using android::os::IAccessor; |
| 48 | using binder::Status; |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 49 | |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 50 | static const char* kStaticCachableList[] = { |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 51 | // go/keep-sorted start |
| 52 | "accessibility", |
| 53 | "account", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 54 | "activity", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 55 | "alarm", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 56 | "android.frameworks.stats.IStats/default", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 57 | "android.system.keystore2.IKeystoreService/default", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 58 | "appops", |
| 59 | "audio", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 60 | "autofill", |
| 61 | "batteryproperties", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 62 | "batterystats", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 63 | "biometic", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 64 | "carrier_config", |
| 65 | "connectivity", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 66 | "content", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 67 | "content_capture", |
| 68 | "device_policy", |
| 69 | "display", |
| 70 | "dropbox", |
| 71 | "econtroller", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 72 | "graphicsstats", |
| 73 | "input", |
| 74 | "input_method", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 75 | "isub", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 76 | "jobscheduler", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 77 | "legacy_permission", |
| 78 | "location", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 79 | "lock_settings", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 80 | "media.extractor", |
| 81 | "media.metrics", |
| 82 | "media.player", |
| 83 | "media.resource_manager", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 84 | "media_resource_monitor", |
| 85 | "mount", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 86 | "netd_listener", |
| 87 | "netstats", |
| 88 | "network_management", |
| 89 | "nfc", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 90 | "notification", |
| 91 | "package", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 92 | "package_native", |
| 93 | "performance_hint", |
| 94 | "permission", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 95 | "permission_checker", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 96 | "permissionmgr", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 97 | "phone", |
| 98 | "platform_compat", |
| 99 | "power", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 100 | "processinfo", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 101 | "role", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 102 | "sensitive_content_protection_service", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 103 | "sensorservice", |
| 104 | "statscompanion", |
| 105 | "telephony.registry", |
| 106 | "thermalservice", |
| 107 | "time_detector", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 108 | "tracing.proxy", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 109 | "trust", |
| 110 | "uimode", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 111 | "user", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 112 | "vibrator", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 113 | "virtualdevice", |
| 114 | "virtualdevice_native", |
| 115 | "webviewupdate", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 116 | "window", |
| 117 | // go/keep-sorted end |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 120 | os::ServiceWithMetadata createServiceWithMetadata(const sp<IBinder>& service, bool isLazyService) { |
| 121 | os::ServiceWithMetadata out = os::ServiceWithMetadata(); |
| 122 | out.service = service; |
| 123 | out.isLazyService = isLazyService; |
| 124 | return out; |
| 125 | } |
| 126 | |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 127 | bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) { |
Devin Moore | 74bc589 | 2024-10-23 17:12:19 +0000 | [diff] [blame] | 128 | sp<ProcessState> self = ProcessState::selfOrNull(); |
| 129 | if (!self || self->getThreadPoolMaxTotalThreadCount() <= 0) { |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 130 | ALOGW("Thread Pool max thread count is 0. Cannot cache binder as linkToDeath cannot be " |
| 131 | "implemented. serviceName: %s", |
| 132 | serviceName.c_str()); |
| 133 | return false; |
| 134 | } |
Parth Sane | b109db3 | 2024-12-05 19:43:40 +0000 | [diff] [blame^] | 135 | if (kRemoveStaticList) return true; |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 136 | for (const char* name : kStaticCachableList) { |
| 137 | if (name == serviceName) { |
| 138 | return true; |
| 139 | } |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 144 | Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName, |
| 145 | const os::Service& service) { |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 146 | if (!kUseCache) { |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 147 | return Status::ok(); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 148 | } |
Parth Sane | dc20754 | 2024-11-14 11:49:08 +0000 | [diff] [blame] | 149 | |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 150 | if (service.getTag() == os::Service::Tag::serviceWithMetadata) { |
| 151 | auto serviceWithMetadata = service.get<os::Service::Tag::serviceWithMetadata>(); |
| 152 | return updateCache(serviceName, serviceWithMetadata.service, |
| 153 | serviceWithMetadata.isLazyService); |
Parth Sane | dc20754 | 2024-11-14 11:49:08 +0000 | [diff] [blame] | 154 | } |
| 155 | return Status::ok(); |
| 156 | } |
| 157 | |
| 158 | Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName, |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 159 | const sp<IBinder>& binder, bool isServiceLazy) { |
Parth Sane | a6676ba | 2024-10-04 14:14:07 +0000 | [diff] [blame] | 160 | std::string traceStr; |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 161 | // Don't cache if service is lazy |
| 162 | if (kRemoveStaticList && isServiceLazy) { |
| 163 | return Status::ok(); |
| 164 | } |
Parth Sane | a6676ba | 2024-10-04 14:14:07 +0000 | [diff] [blame] | 165 | if (atrace_is_tag_enabled(ATRACE_TAG_AIDL)) { |
| 166 | traceStr = "BinderCacheWithInvalidation::updateCache : " + serviceName; |
| 167 | } |
| 168 | binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, traceStr.c_str()); |
Parth Sane | dc20754 | 2024-11-14 11:49:08 +0000 | [diff] [blame] | 169 | if (!binder) { |
| 170 | binder::ScopedTrace |
| 171 | aidlTrace(ATRACE_TAG_AIDL, |
| 172 | "BinderCacheWithInvalidation::updateCache failed: binder_null"); |
| 173 | } else if (!binder->isBinderAlive()) { |
| 174 | binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, |
| 175 | "BinderCacheWithInvalidation::updateCache failed: " |
| 176 | "isBinderAlive_false"); |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 177 | } |
| 178 | // If we reach here with kRemoveStaticList=true then we know service isn't lazy |
Parth Sane | b109db3 | 2024-12-05 19:43:40 +0000 | [diff] [blame^] | 179 | else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) { |
Parth Sane | dc20754 | 2024-11-14 11:49:08 +0000 | [diff] [blame] | 180 | binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, |
| 181 | "BinderCacheWithInvalidation::updateCache successful"); |
| 182 | return mCacheForGetService->setItem(serviceName, binder); |
| 183 | } else { |
| 184 | binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, |
| 185 | "BinderCacheWithInvalidation::updateCache failed: " |
| 186 | "caching_not_enabled"); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 187 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 188 | return Status::ok(); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName, |
| 192 | os::Service* _out) { |
| 193 | if (!kUseCache) { |
| 194 | return false; |
| 195 | } |
| 196 | sp<IBinder> item = mCacheForGetService->getItem(serviceName); |
| 197 | // TODO(b/363177618): Enable caching for binders which are always null. |
| 198 | if (item != nullptr && item->isBinderAlive()) { |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 199 | *_out = createServiceWithMetadata(item, false); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 200 | return true; |
| 201 | } |
| 202 | return false; |
| 203 | } |
| 204 | |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 205 | BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl) |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 206 | : mTheRealServiceManager(impl) { |
| 207 | mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>(); |
| 208 | } |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 209 | |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 210 | Status BackendUnifiedServiceManager::getService(const ::std::string& name, |
| 211 | sp<IBinder>* _aidl_return) { |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 212 | os::Service service; |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 213 | Status status = getService2(name, &service); |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 214 | *_aidl_return = service.get<os::Service::Tag::serviceWithMetadata>().service; |
Alice Wang | 11da150 | 2024-07-25 12:03:22 +0000 | [diff] [blame] | 215 | return status; |
| 216 | } |
| 217 | |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 218 | Status BackendUnifiedServiceManager::getService2(const ::std::string& name, os::Service* _out) { |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 219 | if (returnIfCached(name, _out)) { |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 220 | return Status::ok(); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 221 | } |
Alice Wang | 11da150 | 2024-07-25 12:03:22 +0000 | [diff] [blame] | 222 | os::Service service; |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 223 | Status status = mTheRealServiceManager->getService2(name, &service); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 224 | |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 225 | if (status.isOk()) { |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 226 | status = toBinderService(name, service, _out); |
| 227 | if (status.isOk()) { |
| 228 | return updateCache(name, service); |
| 229 | } |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 230 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 231 | return status; |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 232 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 233 | |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 234 | Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os::Service* _out) { |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 235 | os::Service service; |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 236 | if (returnIfCached(name, _out)) { |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 237 | return Status::ok(); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 240 | Status status = mTheRealServiceManager->checkService(name, &service); |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 241 | if (status.isOk()) { |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 242 | status = toBinderService(name, service, _out); |
| 243 | if (status.isOk()) { |
| 244 | return updateCache(name, service); |
| 245 | } |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 246 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 247 | return status; |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 248 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 249 | |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 250 | Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name, |
| 251 | const os::Service& in, os::Service* _out) { |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 252 | switch (in.getTag()) { |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 253 | case os::Service::Tag::serviceWithMetadata: { |
| 254 | auto serviceWithMetadata = in.get<os::Service::Tag::serviceWithMetadata>(); |
| 255 | if (serviceWithMetadata.service == nullptr) { |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 256 | // failed to find a service. Check to see if we have any local |
| 257 | // injected Accessors for this service. |
| 258 | os::Service accessor; |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 259 | Status status = getInjectedAccessor(name, &accessor); |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 260 | if (!status.isOk()) { |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 261 | *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>( |
| 262 | createServiceWithMetadata(nullptr, false)); |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 263 | return status; |
| 264 | } |
| 265 | if (accessor.getTag() == os::Service::Tag::accessor && |
| 266 | accessor.get<os::Service::Tag::accessor>() != nullptr) { |
| 267 | ALOGI("Found local injected service for %s, will attempt to create connection", |
| 268 | name.c_str()); |
| 269 | // Call this again using the accessor Service to get the real |
| 270 | // service's binder into _out |
| 271 | return toBinderService(name, accessor, _out); |
| 272 | } |
| 273 | } |
| 274 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 275 | *_out = in; |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 276 | return Status::ok(); |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 277 | } |
| 278 | case os::Service::Tag::accessor: { |
| 279 | sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>(); |
| 280 | sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder); |
| 281 | if (accessor == nullptr) { |
| 282 | ALOGE("Service#accessor doesn't have accessor. VM is maybe starting..."); |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 283 | *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>( |
| 284 | createServiceWithMetadata(nullptr, false)); |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 285 | return Status::ok(); |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 286 | } |
| 287 | auto request = [=] { |
| 288 | os::ParcelFileDescriptor fd; |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 289 | Status ret = accessor->addConnection(&fd); |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 290 | if (ret.isOk()) { |
| 291 | return base::unique_fd(fd.release()); |
| 292 | } else { |
| 293 | ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str()); |
| 294 | return base::unique_fd(-1); |
| 295 | } |
| 296 | }; |
| 297 | auto session = RpcSession::make(); |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 298 | status_t status = session->setupPreconnectedClient(base::unique_fd{}, request); |
| 299 | if (status != OK) { |
| 300 | ALOGE("Failed to set up preconnected binder RPC client: %s", |
| 301 | statusToString(status).c_str()); |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 302 | return Status::fromStatusT(status); |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 303 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 304 | session->setSessionSpecificRoot(accessorBinder); |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 305 | *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>( |
| 306 | createServiceWithMetadata(session->getRootObject(), false)); |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 307 | return Status::ok(); |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 308 | } |
| 309 | default: { |
| 310 | LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag()); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 315 | Status BackendUnifiedServiceManager::addService(const ::std::string& name, |
| 316 | const sp<IBinder>& service, bool allowIsolated, |
| 317 | int32_t dumpPriority) { |
Parth Sane | dc20754 | 2024-11-14 11:49:08 +0000 | [diff] [blame] | 318 | Status status = mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority); |
| 319 | // mEnableAddServiceCache is true by default. |
| 320 | if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) { |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 321 | return updateCache(name, service, |
| 322 | dumpPriority & android::os::IServiceManager::FLAG_IS_LAZY_SERVICE); |
Parth Sane | dc20754 | 2024-11-14 11:49:08 +0000 | [diff] [blame] | 323 | } |
| 324 | return status; |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 325 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 326 | Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority, |
| 327 | ::std::vector<::std::string>* _aidl_return) { |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 328 | return mTheRealServiceManager->listServices(dumpPriority, _aidl_return); |
| 329 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 330 | Status BackendUnifiedServiceManager::registerForNotifications( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 331 | const ::std::string& name, const sp<os::IServiceCallback>& callback) { |
| 332 | return mTheRealServiceManager->registerForNotifications(name, callback); |
| 333 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 334 | Status BackendUnifiedServiceManager::unregisterForNotifications( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 335 | const ::std::string& name, const sp<os::IServiceCallback>& callback) { |
| 336 | return mTheRealServiceManager->unregisterForNotifications(name, callback); |
| 337 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 338 | Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name, bool* _aidl_return) { |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 339 | return mTheRealServiceManager->isDeclared(name, _aidl_return); |
| 340 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 341 | Status BackendUnifiedServiceManager::getDeclaredInstances( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 342 | const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) { |
| 343 | return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return); |
| 344 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 345 | Status BackendUnifiedServiceManager::updatableViaApex( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 346 | const ::std::string& name, ::std::optional<::std::string>* _aidl_return) { |
| 347 | return mTheRealServiceManager->updatableViaApex(name, _aidl_return); |
| 348 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 349 | Status BackendUnifiedServiceManager::getUpdatableNames(const ::std::string& apexName, |
| 350 | ::std::vector<::std::string>* _aidl_return) { |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 351 | return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return); |
| 352 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 353 | Status BackendUnifiedServiceManager::getConnectionInfo( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 354 | const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) { |
| 355 | return mTheRealServiceManager->getConnectionInfo(name, _aidl_return); |
| 356 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 357 | Status BackendUnifiedServiceManager::registerClientCallback( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 358 | const ::std::string& name, const sp<IBinder>& service, |
| 359 | const sp<os::IClientCallback>& callback) { |
| 360 | return mTheRealServiceManager->registerClientCallback(name, service, callback); |
| 361 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 362 | Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name, |
| 363 | const sp<IBinder>& service) { |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 364 | return mTheRealServiceManager->tryUnregisterService(name, service); |
| 365 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 366 | Status BackendUnifiedServiceManager::getServiceDebugInfo( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 367 | ::std::vector<os::ServiceDebugInfo>* _aidl_return) { |
| 368 | return mTheRealServiceManager->getServiceDebugInfo(_aidl_return); |
| 369 | } |
| 370 | |
| 371 | [[clang::no_destroy]] static std::once_flag gUSmOnce; |
| 372 | [[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager; |
| 373 | |
| 374 | sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() { |
| 375 | std::call_once(gUSmOnce, []() { |
| 376 | #if defined(__BIONIC__) && !defined(__ANDROID_VNDK__) |
| 377 | /* wait for service manager */ { |
| 378 | using std::literals::chrono_literals::operator""s; |
| 379 | using android::base::WaitForProperty; |
| 380 | while (!WaitForProperty("servicemanager.ready", "true", 1s)) { |
| 381 | ALOGE("Waited for servicemanager.ready for a second, waiting another..."); |
| 382 | } |
| 383 | } |
| 384 | #endif |
| 385 | |
| 386 | sp<AidlServiceManager> sm = nullptr; |
| 387 | while (sm == nullptr) { |
| 388 | sm = interface_cast<AidlServiceManager>( |
| 389 | ProcessState::self()->getContextObject(nullptr)); |
| 390 | if (sm == nullptr) { |
| 391 | ALOGE("Waiting 1s on context object on %s.", |
| 392 | ProcessState::self()->getDriverName().c_str()); |
| 393 | sleep(1); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm); |
| 398 | }); |
| 399 | |
| 400 | return gUnifiedServiceManager; |
| 401 | } |
| 402 | |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 403 | } // namespace android |