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 | |
Devin Moore | a3867c7 | 2024-10-23 19:29:49 +0000 | [diff] [blame] | 18 | #include <android-base/strings.h> |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 19 | #include <android/os/IAccessor.h> |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 20 | #include <android/os/IServiceManager.h> |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 21 | #include <binder/RpcSession.h> |
| 22 | |
Tomasz Wasilczyk | fe25f12 | 2024-06-26 12:45:57 -0700 | [diff] [blame] | 23 | #if defined(__BIONIC__) && !defined(__ANDROID_VNDK__) |
| 24 | #include <android-base/properties.h> |
| 25 | #endif |
| 26 | |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 27 | namespace android { |
| 28 | |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 29 | #ifdef LIBBINDER_CLIENT_CACHE |
| 30 | constexpr bool kUseCache = true; |
| 31 | #else |
| 32 | constexpr bool kUseCache = false; |
| 33 | #endif |
| 34 | |
Parth Sane | dc20754 | 2024-11-14 11:49:08 +0000 | [diff] [blame] | 35 | #ifdef LIBBINDER_ADDSERVICE_CACHE |
| 36 | constexpr bool kUseCacheInAddService = true; |
| 37 | #else |
| 38 | constexpr bool kUseCacheInAddService = false; |
| 39 | #endif |
| 40 | |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 41 | #ifdef LIBBINDER_REMOVE_CACHE_STATIC_LIST |
| 42 | constexpr bool kRemoveStaticList = true; |
| 43 | #else |
| 44 | constexpr bool kRemoveStaticList = false; |
| 45 | #endif |
| 46 | |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 47 | using AidlServiceManager = android::os::IServiceManager; |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 48 | using android::os::IAccessor; |
| 49 | using binder::Status; |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 50 | |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 51 | static const char* kUnsupportedOpNoServiceManager = |
| 52 | "Unsupported operation without a kernel binder servicemanager process"; |
| 53 | |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 54 | static const char* kStaticCachableList[] = { |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 55 | // go/keep-sorted start |
| 56 | "accessibility", |
| 57 | "account", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 58 | "activity", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 59 | "alarm", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 60 | "android.frameworks.stats.IStats/default", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 61 | "android.system.keystore2.IKeystoreService/default", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 62 | "appops", |
| 63 | "audio", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 64 | "autofill", |
| 65 | "batteryproperties", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 66 | "batterystats", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 67 | "biometic", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 68 | "carrier_config", |
| 69 | "connectivity", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 70 | "content", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 71 | "content_capture", |
| 72 | "device_policy", |
| 73 | "display", |
| 74 | "dropbox", |
| 75 | "econtroller", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 76 | "graphicsstats", |
| 77 | "input", |
| 78 | "input_method", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 79 | "isub", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 80 | "jobscheduler", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 81 | "legacy_permission", |
| 82 | "location", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 83 | "lock_settings", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 84 | "media.extractor", |
| 85 | "media.metrics", |
| 86 | "media.player", |
| 87 | "media.resource_manager", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 88 | "media_resource_monitor", |
| 89 | "mount", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 90 | "netd_listener", |
| 91 | "netstats", |
| 92 | "network_management", |
| 93 | "nfc", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 94 | "notification", |
| 95 | "package", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 96 | "package_native", |
| 97 | "performance_hint", |
| 98 | "permission", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 99 | "permission_checker", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 100 | "permissionmgr", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 101 | "phone", |
| 102 | "platform_compat", |
| 103 | "power", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 104 | "processinfo", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 105 | "role", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 106 | "sensitive_content_protection_service", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 107 | "sensorservice", |
| 108 | "statscompanion", |
| 109 | "telephony.registry", |
| 110 | "thermalservice", |
| 111 | "time_detector", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 112 | "tracing.proxy", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 113 | "trust", |
| 114 | "uimode", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 115 | "user", |
Parth Sane | 0105ea5 | 2024-10-11 11:41:16 +0000 | [diff] [blame] | 116 | "vibrator", |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 117 | "virtualdevice", |
| 118 | "virtualdevice_native", |
| 119 | "webviewupdate", |
Parth Sane | ac49270 | 2024-09-18 15:54:16 +0000 | [diff] [blame] | 120 | "window", |
| 121 | // go/keep-sorted end |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 124 | os::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 Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 131 | bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) { |
Devin Moore | 74bc589 | 2024-10-23 17:12:19 +0000 | [diff] [blame] | 132 | sp<ProcessState> self = ProcessState::selfOrNull(); |
| 133 | if (!self || self->getThreadPoolMaxTotalThreadCount() <= 0) { |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 134 | 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 Sane | b109db3 | 2024-12-05 19:43:40 +0000 | [diff] [blame] | 139 | if (kRemoveStaticList) return true; |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 140 | for (const char* name : kStaticCachableList) { |
| 141 | if (name == serviceName) { |
| 142 | return true; |
| 143 | } |
| 144 | } |
| 145 | return false; |
| 146 | } |
| 147 | |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 148 | Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName, |
| 149 | const os::Service& service) { |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 150 | if (!kUseCache) { |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 151 | return Status::ok(); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 152 | } |
Parth Sane | dc20754 | 2024-11-14 11:49:08 +0000 | [diff] [blame] | 153 | |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 154 | 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 Sane | dc20754 | 2024-11-14 11:49:08 +0000 | [diff] [blame] | 158 | } |
| 159 | return Status::ok(); |
| 160 | } |
| 161 | |
| 162 | Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName, |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 163 | const sp<IBinder>& binder, bool isServiceLazy) { |
Parth Sane | a6676ba | 2024-10-04 14:14:07 +0000 | [diff] [blame] | 164 | std::string traceStr; |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 165 | // Don't cache if service is lazy |
| 166 | if (kRemoveStaticList && isServiceLazy) { |
| 167 | return Status::ok(); |
| 168 | } |
Parth Sane | a6676ba | 2024-10-04 14:14:07 +0000 | [diff] [blame] | 169 | 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 Sane | dc20754 | 2024-11-14 11:49:08 +0000 | [diff] [blame] | 173 | 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 Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 181 | } |
| 182 | // 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] | 183 | else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) { |
Parth Sane | dc20754 | 2024-11-14 11:49:08 +0000 | [diff] [blame] | 184 | 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 Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 191 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 192 | return Status::ok(); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | bool 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 Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 203 | *_out = createServiceWithMetadata(item, false); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 204 | return true; |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 209 | BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl) |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 210 | : mTheRealServiceManager(impl) { |
| 211 | mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>(); |
| 212 | } |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 213 | |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 214 | Status BackendUnifiedServiceManager::getService(const ::std::string& name, |
| 215 | sp<IBinder>* _aidl_return) { |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 216 | os::Service service; |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 217 | Status status = getService2(name, &service); |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 218 | *_aidl_return = service.get<os::Service::Tag::serviceWithMetadata>().service; |
Alice Wang | 11da150 | 2024-07-25 12:03:22 +0000 | [diff] [blame] | 219 | return status; |
| 220 | } |
| 221 | |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 222 | Status BackendUnifiedServiceManager::getService2(const ::std::string& name, os::Service* _out) { |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 223 | if (returnIfCached(name, _out)) { |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 224 | return Status::ok(); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 225 | } |
Alice Wang | 11da150 | 2024-07-25 12:03:22 +0000 | [diff] [blame] | 226 | os::Service service; |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 227 | Status status = Status::ok(); |
| 228 | if (mTheRealServiceManager) { |
| 229 | status = mTheRealServiceManager->getService2(name, &service); |
| 230 | } |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 231 | |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 232 | if (status.isOk()) { |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 233 | status = toBinderService(name, service, _out); |
| 234 | if (status.isOk()) { |
| 235 | return updateCache(name, service); |
| 236 | } |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 237 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 238 | return status; |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 239 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 240 | |
Alice Wang | 2b61344 | 2025-01-02 11:53:05 +0000 | [diff] [blame] | 241 | Status BackendUnifiedServiceManager::checkService(const ::std::string& name, |
| 242 | sp<IBinder>* _aidl_return) { |
| 243 | os::Service service; |
| 244 | Status status = checkService2(name, &service); |
| 245 | if (status.isOk()) { |
| 246 | *_aidl_return = service.get<os::Service::Tag::serviceWithMetadata>().service; |
| 247 | } |
| 248 | return status; |
| 249 | } |
| 250 | |
| 251 | Status BackendUnifiedServiceManager::checkService2(const ::std::string& name, os::Service* _out) { |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 252 | os::Service service; |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 253 | if (returnIfCached(name, _out)) { |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 254 | return Status::ok(); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 257 | Status status = Status::ok(); |
| 258 | if (mTheRealServiceManager) { |
Alice Wang | 2b61344 | 2025-01-02 11:53:05 +0000 | [diff] [blame] | 259 | status = mTheRealServiceManager->checkService2(name, &service); |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 260 | } |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 261 | if (status.isOk()) { |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 262 | status = toBinderService(name, service, _out); |
| 263 | if (status.isOk()) { |
| 264 | return updateCache(name, service); |
| 265 | } |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 266 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 267 | return status; |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 268 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 269 | |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 270 | Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name, |
| 271 | const os::Service& in, os::Service* _out) { |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 272 | switch (in.getTag()) { |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 273 | case os::Service::Tag::serviceWithMetadata: { |
| 274 | auto serviceWithMetadata = in.get<os::Service::Tag::serviceWithMetadata>(); |
| 275 | if (serviceWithMetadata.service == nullptr) { |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 276 | // failed to find a service. Check to see if we have any local |
| 277 | // injected Accessors for this service. |
| 278 | os::Service accessor; |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 279 | Status status = getInjectedAccessor(name, &accessor); |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 280 | if (!status.isOk()) { |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 281 | *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>( |
| 282 | createServiceWithMetadata(nullptr, false)); |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 283 | return status; |
| 284 | } |
| 285 | if (accessor.getTag() == os::Service::Tag::accessor && |
| 286 | accessor.get<os::Service::Tag::accessor>() != nullptr) { |
| 287 | ALOGI("Found local injected service for %s, will attempt to create connection", |
| 288 | name.c_str()); |
| 289 | // Call this again using the accessor Service to get the real |
| 290 | // service's binder into _out |
| 291 | return toBinderService(name, accessor, _out); |
| 292 | } |
| 293 | } |
| 294 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 295 | *_out = in; |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 296 | return Status::ok(); |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 297 | } |
| 298 | case os::Service::Tag::accessor: { |
| 299 | sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>(); |
| 300 | sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder); |
| 301 | if (accessor == nullptr) { |
| 302 | ALOGE("Service#accessor doesn't have accessor. VM is maybe starting..."); |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 303 | *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>( |
| 304 | createServiceWithMetadata(nullptr, false)); |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 305 | return Status::ok(); |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 306 | } |
| 307 | auto request = [=] { |
| 308 | os::ParcelFileDescriptor fd; |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 309 | Status ret = accessor->addConnection(&fd); |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 310 | if (ret.isOk()) { |
| 311 | return base::unique_fd(fd.release()); |
| 312 | } else { |
| 313 | ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str()); |
| 314 | return base::unique_fd(-1); |
| 315 | } |
| 316 | }; |
| 317 | auto session = RpcSession::make(); |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 318 | status_t status = session->setupPreconnectedClient(base::unique_fd{}, request); |
| 319 | if (status != OK) { |
| 320 | ALOGE("Failed to set up preconnected binder RPC client: %s", |
| 321 | statusToString(status).c_str()); |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 322 | return Status::fromStatusT(status); |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 323 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 324 | session->setSessionSpecificRoot(accessorBinder); |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 325 | *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>( |
| 326 | createServiceWithMetadata(session->getRootObject(), false)); |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 327 | return Status::ok(); |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 328 | } |
| 329 | default: { |
| 330 | LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag()); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 335 | Status BackendUnifiedServiceManager::addService(const ::std::string& name, |
| 336 | const sp<IBinder>& service, bool allowIsolated, |
| 337 | int32_t dumpPriority) { |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 338 | if (mTheRealServiceManager) { |
| 339 | Status status = |
| 340 | mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority); |
| 341 | // mEnableAddServiceCache is true by default. |
| 342 | if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) { |
| 343 | return updateCache(name, service, |
| 344 | dumpPriority & android::os::IServiceManager::FLAG_IS_LAZY_SERVICE); |
| 345 | } |
| 346 | return status; |
Parth Sane | dc20754 | 2024-11-14 11:49:08 +0000 | [diff] [blame] | 347 | } |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 348 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION, |
| 349 | kUnsupportedOpNoServiceManager); |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 350 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 351 | Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority, |
| 352 | ::std::vector<::std::string>* _aidl_return) { |
Devin Moore | a3867c7 | 2024-10-23 19:29:49 +0000 | [diff] [blame] | 353 | Status status = Status::ok(); |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 354 | if (mTheRealServiceManager) { |
Devin Moore | a3867c7 | 2024-10-23 19:29:49 +0000 | [diff] [blame] | 355 | status = mTheRealServiceManager->listServices(dumpPriority, _aidl_return); |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 356 | } |
Devin Moore | a3867c7 | 2024-10-23 19:29:49 +0000 | [diff] [blame] | 357 | if (!status.isOk()) return status; |
| 358 | |
| 359 | appendInjectedAccessorServices(_aidl_return); |
| 360 | |
| 361 | return status; |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 362 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 363 | Status BackendUnifiedServiceManager::registerForNotifications( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 364 | const ::std::string& name, const sp<os::IServiceCallback>& callback) { |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 365 | if (mTheRealServiceManager) { |
| 366 | return mTheRealServiceManager->registerForNotifications(name, callback); |
| 367 | } |
| 368 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION, |
| 369 | kUnsupportedOpNoServiceManager); |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 370 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 371 | Status BackendUnifiedServiceManager::unregisterForNotifications( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 372 | const ::std::string& name, const sp<os::IServiceCallback>& callback) { |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 373 | if (mTheRealServiceManager) { |
| 374 | return mTheRealServiceManager->unregisterForNotifications(name, callback); |
| 375 | } |
| 376 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION, |
| 377 | kUnsupportedOpNoServiceManager); |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 378 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 379 | Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name, bool* _aidl_return) { |
Devin Moore | a3867c7 | 2024-10-23 19:29:49 +0000 | [diff] [blame] | 380 | Status status = Status::ok(); |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 381 | if (mTheRealServiceManager) { |
Devin Moore | a3867c7 | 2024-10-23 19:29:49 +0000 | [diff] [blame] | 382 | status = mTheRealServiceManager->isDeclared(name, _aidl_return); |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 383 | } |
Devin Moore | a3867c7 | 2024-10-23 19:29:49 +0000 | [diff] [blame] | 384 | if (!status.isOk()) return status; |
| 385 | |
| 386 | if (!*_aidl_return) { |
| 387 | forEachInjectedAccessorService([&](const std::string& instance) { |
| 388 | if (name == instance) { |
| 389 | *_aidl_return = true; |
| 390 | } |
| 391 | }); |
| 392 | } |
| 393 | |
| 394 | return status; |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 395 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 396 | Status BackendUnifiedServiceManager::getDeclaredInstances( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 397 | const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) { |
Devin Moore | a3867c7 | 2024-10-23 19:29:49 +0000 | [diff] [blame] | 398 | Status status = Status::ok(); |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 399 | if (mTheRealServiceManager) { |
Devin Moore | a3867c7 | 2024-10-23 19:29:49 +0000 | [diff] [blame] | 400 | status = mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return); |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 401 | } |
Devin Moore | a3867c7 | 2024-10-23 19:29:49 +0000 | [diff] [blame] | 402 | if (!status.isOk()) return status; |
| 403 | |
| 404 | forEachInjectedAccessorService([&](const std::string& instance) { |
| 405 | // Declared instances have the format |
| 406 | // <interface>/instance like foo.bar.ISomething/instance |
| 407 | // If it does not have that format, consider the instance to be "" |
| 408 | std::string_view name(instance); |
| 409 | if (base::ConsumePrefix(&name, iface + "/")) { |
| 410 | _aidl_return->emplace_back(name); |
| 411 | } else if (iface == instance) { |
| 412 | _aidl_return->push_back(""); |
| 413 | } |
| 414 | }); |
| 415 | |
| 416 | return status; |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 417 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 418 | Status BackendUnifiedServiceManager::updatableViaApex( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 419 | const ::std::string& name, ::std::optional<::std::string>* _aidl_return) { |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 420 | if (mTheRealServiceManager) { |
| 421 | return mTheRealServiceManager->updatableViaApex(name, _aidl_return); |
| 422 | } |
| 423 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION, |
| 424 | kUnsupportedOpNoServiceManager); |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 425 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 426 | Status BackendUnifiedServiceManager::getUpdatableNames(const ::std::string& apexName, |
| 427 | ::std::vector<::std::string>* _aidl_return) { |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 428 | if (mTheRealServiceManager) { |
| 429 | return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return); |
| 430 | } |
| 431 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION, |
| 432 | kUnsupportedOpNoServiceManager); |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 433 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 434 | Status BackendUnifiedServiceManager::getConnectionInfo( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 435 | const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) { |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 436 | if (mTheRealServiceManager) { |
| 437 | return mTheRealServiceManager->getConnectionInfo(name, _aidl_return); |
| 438 | } |
| 439 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION, |
| 440 | kUnsupportedOpNoServiceManager); |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 441 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 442 | Status BackendUnifiedServiceManager::registerClientCallback( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 443 | const ::std::string& name, const sp<IBinder>& service, |
| 444 | const sp<os::IClientCallback>& callback) { |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 445 | if (mTheRealServiceManager) { |
| 446 | return mTheRealServiceManager->registerClientCallback(name, service, callback); |
| 447 | } |
| 448 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION, |
| 449 | kUnsupportedOpNoServiceManager); |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 450 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 451 | Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name, |
| 452 | const sp<IBinder>& service) { |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 453 | if (mTheRealServiceManager) { |
| 454 | return mTheRealServiceManager->tryUnregisterService(name, service); |
| 455 | } |
| 456 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION, |
| 457 | kUnsupportedOpNoServiceManager); |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 458 | } |
Devin Moore | 678984f | 2024-10-18 22:43:22 +0000 | [diff] [blame] | 459 | Status BackendUnifiedServiceManager::getServiceDebugInfo( |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 460 | ::std::vector<os::ServiceDebugInfo>* _aidl_return) { |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 461 | if (mTheRealServiceManager) { |
| 462 | return mTheRealServiceManager->getServiceDebugInfo(_aidl_return); |
| 463 | } |
| 464 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION, |
| 465 | kUnsupportedOpNoServiceManager); |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | [[clang::no_destroy]] static std::once_flag gUSmOnce; |
| 469 | [[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager; |
| 470 | |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 471 | static bool hasOutOfProcessServiceManager() { |
| 472 | #ifndef BINDER_WITH_KERNEL_IPC |
| 473 | return false; |
| 474 | #else |
| 475 | #if defined(__BIONIC__) && !defined(__ANDROID_VNDK__) |
| 476 | return android::base::GetBoolProperty("servicemanager.installed", true); |
| 477 | #else |
| 478 | return true; |
| 479 | #endif |
| 480 | #endif // BINDER_WITH_KERNEL_IPC |
| 481 | } |
| 482 | |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 483 | sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() { |
| 484 | std::call_once(gUSmOnce, []() { |
| 485 | #if defined(__BIONIC__) && !defined(__ANDROID_VNDK__) |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 486 | /* wait for service manager */ |
| 487 | if (hasOutOfProcessServiceManager()) { |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 488 | using std::literals::chrono_literals::operator""s; |
| 489 | using android::base::WaitForProperty; |
| 490 | while (!WaitForProperty("servicemanager.ready", "true", 1s)) { |
| 491 | ALOGE("Waited for servicemanager.ready for a second, waiting another..."); |
| 492 | } |
| 493 | } |
| 494 | #endif |
| 495 | |
| 496 | sp<AidlServiceManager> sm = nullptr; |
Devin Moore | 3c93111 | 2024-10-23 17:45:04 +0000 | [diff] [blame] | 497 | while (hasOutOfProcessServiceManager() && sm == nullptr) { |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 498 | sm = interface_cast<AidlServiceManager>( |
| 499 | ProcessState::self()->getContextObject(nullptr)); |
| 500 | if (sm == nullptr) { |
| 501 | ALOGE("Waiting 1s on context object on %s.", |
| 502 | ProcessState::self()->getDriverName().c_str()); |
| 503 | sleep(1); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm); |
| 508 | }); |
| 509 | |
| 510 | return gUnifiedServiceManager; |
| 511 | } |
| 512 | |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 513 | } // namespace android |