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 | } |
| 117 | if (service.getTag() == os::Service::Tag::binder) { |
| 118 | sp<IBinder> binder = service.get<os::Service::Tag::binder>(); |
| 119 | if (binder && mCacheForGetService->isClientSideCachingEnabled(serviceName) && |
| 120 | binder->isBinderAlive()) { |
| 121 | return mCacheForGetService->setItem(serviceName, binder); |
| 122 | } |
| 123 | } |
| 124 | return binder::Status::ok(); |
| 125 | } |
| 126 | |
| 127 | bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName, |
| 128 | os::Service* _out) { |
| 129 | if (!kUseCache) { |
| 130 | return false; |
| 131 | } |
| 132 | sp<IBinder> item = mCacheForGetService->getItem(serviceName); |
| 133 | // TODO(b/363177618): Enable caching for binders which are always null. |
| 134 | if (item != nullptr && item->isBinderAlive()) { |
| 135 | *_out = os::Service::make<os::Service::Tag::binder>(item); |
| 136 | return true; |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 141 | BackendUnifiedServiceManager::BackendUnifiedServiceManager(const sp<AidlServiceManager>& impl) |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 142 | : mTheRealServiceManager(impl) { |
| 143 | mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>(); |
| 144 | } |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 145 | |
| 146 | sp<AidlServiceManager> BackendUnifiedServiceManager::getImpl() { |
| 147 | return mTheRealServiceManager; |
| 148 | } |
Alice Wang | 11da150 | 2024-07-25 12:03:22 +0000 | [diff] [blame] | 149 | |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 150 | binder::Status BackendUnifiedServiceManager::getService(const ::std::string& name, |
Alice Wang | 11da150 | 2024-07-25 12:03:22 +0000 | [diff] [blame] | 151 | sp<IBinder>* _aidl_return) { |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 152 | os::Service service; |
Alice Wang | 11da150 | 2024-07-25 12:03:22 +0000 | [diff] [blame] | 153 | binder::Status status = getService2(name, &service); |
| 154 | *_aidl_return = service.get<os::Service::Tag::binder>(); |
| 155 | return status; |
| 156 | } |
| 157 | |
| 158 | binder::Status BackendUnifiedServiceManager::getService2(const ::std::string& name, |
| 159 | os::Service* _out) { |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 160 | if (returnIfCached(name, _out)) { |
| 161 | return binder::Status::ok(); |
| 162 | } |
Alice Wang | 11da150 | 2024-07-25 12:03:22 +0000 | [diff] [blame] | 163 | os::Service service; |
| 164 | binder::Status status = mTheRealServiceManager->getService2(name, &service); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 165 | |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 166 | if (status.isOk()) { |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 167 | status = toBinderService(name, service, _out); |
| 168 | if (status.isOk()) { |
| 169 | return updateCache(name, service); |
| 170 | } |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 171 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 172 | return status; |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 173 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 174 | |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 175 | binder::Status BackendUnifiedServiceManager::checkService(const ::std::string& name, |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 176 | os::Service* _out) { |
| 177 | os::Service service; |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 178 | if (returnIfCached(name, _out)) { |
| 179 | return binder::Status::ok(); |
| 180 | } |
| 181 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 182 | binder::Status status = mTheRealServiceManager->checkService(name, &service); |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 183 | if (status.isOk()) { |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 184 | status = toBinderService(name, service, _out); |
| 185 | if (status.isOk()) { |
| 186 | return updateCache(name, service); |
| 187 | } |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 188 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 189 | return status; |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 190 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 191 | |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 192 | binder::Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name, |
| 193 | const os::Service& in, |
| 194 | os::Service* _out) { |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 195 | switch (in.getTag()) { |
| 196 | case os::Service::Tag::binder: { |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 197 | if (in.get<os::Service::Tag::binder>() == nullptr) { |
| 198 | // failed to find a service. Check to see if we have any local |
| 199 | // injected Accessors for this service. |
| 200 | os::Service accessor; |
| 201 | binder::Status status = getInjectedAccessor(name, &accessor); |
| 202 | if (!status.isOk()) { |
| 203 | *_out = os::Service::make<os::Service::Tag::binder>(nullptr); |
| 204 | return status; |
| 205 | } |
| 206 | if (accessor.getTag() == os::Service::Tag::accessor && |
| 207 | accessor.get<os::Service::Tag::accessor>() != nullptr) { |
| 208 | ALOGI("Found local injected service for %s, will attempt to create connection", |
| 209 | name.c_str()); |
| 210 | // Call this again using the accessor Service to get the real |
| 211 | // service's binder into _out |
| 212 | return toBinderService(name, accessor, _out); |
| 213 | } |
| 214 | } |
| 215 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 216 | *_out = in; |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 217 | return binder::Status::ok(); |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 218 | } |
| 219 | case os::Service::Tag::accessor: { |
| 220 | sp<IBinder> accessorBinder = in.get<os::Service::Tag::accessor>(); |
| 221 | sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder); |
| 222 | if (accessor == nullptr) { |
| 223 | ALOGE("Service#accessor doesn't have accessor. VM is maybe starting..."); |
| 224 | *_out = os::Service::make<os::Service::Tag::binder>(nullptr); |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 225 | return binder::Status::ok(); |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 226 | } |
| 227 | auto request = [=] { |
| 228 | os::ParcelFileDescriptor fd; |
| 229 | binder::Status ret = accessor->addConnection(&fd); |
| 230 | if (ret.isOk()) { |
| 231 | return base::unique_fd(fd.release()); |
| 232 | } else { |
| 233 | ALOGE("Failed to connect to RpcSession: %s", ret.toString8().c_str()); |
| 234 | return base::unique_fd(-1); |
| 235 | } |
| 236 | }; |
| 237 | auto session = RpcSession::make(); |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 238 | status_t status = session->setupPreconnectedClient(base::unique_fd{}, request); |
| 239 | if (status != OK) { |
| 240 | ALOGE("Failed to set up preconnected binder RPC client: %s", |
| 241 | statusToString(status).c_str()); |
| 242 | return binder::Status::fromStatusT(status); |
| 243 | } |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 244 | session->setSessionSpecificRoot(accessorBinder); |
| 245 | *_out = os::Service::make<os::Service::Tag::binder>(session->getRootObject()); |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 246 | return binder::Status::ok(); |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 247 | } |
| 248 | default: { |
| 249 | LOG_ALWAYS_FATAL("Unknown service type: %d", in.getTag()); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Parth Sane | 56a0471 | 2024-04-22 14:21:07 +0000 | [diff] [blame] | 254 | binder::Status BackendUnifiedServiceManager::addService(const ::std::string& name, |
| 255 | const sp<IBinder>& service, |
| 256 | bool allowIsolated, int32_t dumpPriority) { |
| 257 | return mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority); |
| 258 | } |
| 259 | binder::Status BackendUnifiedServiceManager::listServices( |
| 260 | int32_t dumpPriority, ::std::vector<::std::string>* _aidl_return) { |
| 261 | return mTheRealServiceManager->listServices(dumpPriority, _aidl_return); |
| 262 | } |
| 263 | binder::Status BackendUnifiedServiceManager::registerForNotifications( |
| 264 | const ::std::string& name, const sp<os::IServiceCallback>& callback) { |
| 265 | return mTheRealServiceManager->registerForNotifications(name, callback); |
| 266 | } |
| 267 | binder::Status BackendUnifiedServiceManager::unregisterForNotifications( |
| 268 | const ::std::string& name, const sp<os::IServiceCallback>& callback) { |
| 269 | return mTheRealServiceManager->unregisterForNotifications(name, callback); |
| 270 | } |
| 271 | binder::Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name, |
| 272 | bool* _aidl_return) { |
| 273 | return mTheRealServiceManager->isDeclared(name, _aidl_return); |
| 274 | } |
| 275 | binder::Status BackendUnifiedServiceManager::getDeclaredInstances( |
| 276 | const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) { |
| 277 | return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return); |
| 278 | } |
| 279 | binder::Status BackendUnifiedServiceManager::updatableViaApex( |
| 280 | const ::std::string& name, ::std::optional<::std::string>* _aidl_return) { |
| 281 | return mTheRealServiceManager->updatableViaApex(name, _aidl_return); |
| 282 | } |
| 283 | binder::Status BackendUnifiedServiceManager::getUpdatableNames( |
| 284 | const ::std::string& apexName, ::std::vector<::std::string>* _aidl_return) { |
| 285 | return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return); |
| 286 | } |
| 287 | binder::Status BackendUnifiedServiceManager::getConnectionInfo( |
| 288 | const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) { |
| 289 | return mTheRealServiceManager->getConnectionInfo(name, _aidl_return); |
| 290 | } |
| 291 | binder::Status BackendUnifiedServiceManager::registerClientCallback( |
| 292 | const ::std::string& name, const sp<IBinder>& service, |
| 293 | const sp<os::IClientCallback>& callback) { |
| 294 | return mTheRealServiceManager->registerClientCallback(name, service, callback); |
| 295 | } |
| 296 | binder::Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name, |
| 297 | const sp<IBinder>& service) { |
| 298 | return mTheRealServiceManager->tryUnregisterService(name, service); |
| 299 | } |
| 300 | binder::Status BackendUnifiedServiceManager::getServiceDebugInfo( |
| 301 | ::std::vector<os::ServiceDebugInfo>* _aidl_return) { |
| 302 | return mTheRealServiceManager->getServiceDebugInfo(_aidl_return); |
| 303 | } |
| 304 | |
| 305 | [[clang::no_destroy]] static std::once_flag gUSmOnce; |
| 306 | [[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager; |
| 307 | |
| 308 | sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() { |
| 309 | std::call_once(gUSmOnce, []() { |
| 310 | #if defined(__BIONIC__) && !defined(__ANDROID_VNDK__) |
| 311 | /* wait for service manager */ { |
| 312 | using std::literals::chrono_literals::operator""s; |
| 313 | using android::base::WaitForProperty; |
| 314 | while (!WaitForProperty("servicemanager.ready", "true", 1s)) { |
| 315 | ALOGE("Waited for servicemanager.ready for a second, waiting another..."); |
| 316 | } |
| 317 | } |
| 318 | #endif |
| 319 | |
| 320 | sp<AidlServiceManager> sm = nullptr; |
| 321 | while (sm == nullptr) { |
| 322 | sm = interface_cast<AidlServiceManager>( |
| 323 | ProcessState::self()->getContextObject(nullptr)); |
| 324 | if (sm == nullptr) { |
| 325 | ALOGE("Waiting 1s on context object on %s.", |
| 326 | ProcessState::self()->getDriverName().c_str()); |
| 327 | sleep(1); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | gUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(sm); |
| 332 | }); |
| 333 | |
| 334 | return gUnifiedServiceManager; |
| 335 | } |
| 336 | |
Devin Moore | 18f6375 | 2024-08-08 21:01:24 +0000 | [diff] [blame] | 337 | } // namespace android |