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