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