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