Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 17 | #include "log/log_main.h" |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 18 | #define LOG_TAG "AidlLazyServiceRegistrar" |
| 19 | |
| 20 | #include <binder/LazyServiceRegistrar.h> |
| 21 | #include <binder/IPCThreadState.h> |
| 22 | #include <binder/IServiceManager.h> |
| 23 | #include <android/os/BnClientCallback.h> |
| 24 | #include <android/os/IServiceManager.h> |
| 25 | #include <utils/Log.h> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace binder { |
| 29 | namespace internal { |
| 30 | |
| 31 | using AidlServiceManager = android::os::IServiceManager; |
| 32 | |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 33 | class ClientCounterCallbackImpl : public ::android::os::BnClientCallback { |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 34 | public: |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 35 | ClientCounterCallbackImpl() : mNumConnectedServices(0), mForcePersist(false) {} |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 36 | |
| 37 | bool registerService(const sp<IBinder>& service, const std::string& name, |
| 38 | bool allowIsolated, int dumpFlags); |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 39 | void forcePersist(bool persist); |
| 40 | |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 41 | void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback); |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 42 | |
| 43 | bool tryUnregister(); |
| 44 | |
| 45 | void reRegister(); |
| 46 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 47 | protected: |
| 48 | Status onClients(const sp<IBinder>& service, bool clients) override; |
| 49 | |
| 50 | private: |
Steven Moreland | dd20e7c | 2020-04-10 11:51:14 -0700 | [diff] [blame] | 51 | struct Service { |
| 52 | sp<IBinder> service; |
| 53 | bool allowIsolated; |
| 54 | int dumpFlags; |
| 55 | |
| 56 | // whether, based on onClients calls, we know we have a client for this |
| 57 | // service or not |
| 58 | bool clients = false; |
Xin Li | ca3e579 | 2021-02-21 09:43:11 -0800 | [diff] [blame^] | 59 | bool registered = true; |
Steven Moreland | dd20e7c | 2020-04-10 11:51:14 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * Looks up a service guaranteed to be registered (service from onClients). |
| 64 | */ |
| 65 | std::map<std::string, Service>::iterator assertRegisteredService(const sp<IBinder>& service); |
| 66 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Unregisters all services that we can. If we can't unregister all, re-register other |
| 69 | * services. |
| 70 | */ |
| 71 | void tryShutdown(); |
| 72 | |
| 73 | /** |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 74 | * Try to shutdown the process, unless: |
| 75 | * - 'forcePersist' is 'true', or |
| 76 | * - The active services count callback returns 'true', or |
| 77 | * - Some services have clients. |
| 78 | */ |
| 79 | void maybeTryShutdown(); |
| 80 | |
Steven Moreland | dd20e7c | 2020-04-10 11:51:14 -0700 | [diff] [blame] | 81 | // count of services with clients |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 82 | size_t mNumConnectedServices; |
| 83 | |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 84 | // previous value passed to the active services callback |
| 85 | std::optional<bool> mPreviousHasClients; |
| 86 | |
Steven Moreland | dd20e7c | 2020-04-10 11:51:14 -0700 | [diff] [blame] | 87 | // map of registered names and services |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 88 | std::map<std::string, Service> mRegisteredServices; |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 89 | |
| 90 | bool mForcePersist; |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 91 | |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 92 | // Callback used to report if there are services with clients |
| 93 | std::function<bool(bool)> mActiveServicesCallback; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 96 | class ClientCounterCallback { |
| 97 | public: |
| 98 | ClientCounterCallback(); |
| 99 | |
| 100 | bool registerService(const sp<IBinder>& service, const std::string& name, |
| 101 | bool allowIsolated, int dumpFlags); |
| 102 | |
| 103 | /** |
| 104 | * Set a flag to prevent services from automatically shutting down |
| 105 | */ |
| 106 | void forcePersist(bool persist); |
| 107 | |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 108 | void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback); |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 109 | |
| 110 | bool tryUnregister(); |
| 111 | |
| 112 | void reRegister(); |
| 113 | |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 114 | private: |
| 115 | sp<ClientCounterCallbackImpl> mImpl; |
| 116 | }; |
| 117 | |
| 118 | bool ClientCounterCallbackImpl::registerService(const sp<IBinder>& service, const std::string& name, |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 119 | bool allowIsolated, int dumpFlags) { |
Jon Spivack | 718470e | 2020-02-19 19:18:21 -0800 | [diff] [blame] | 120 | auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager())); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 121 | |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 122 | bool reRegister = mRegisteredServices.count(name) > 0; |
| 123 | std::string regStr = (reRegister) ? "Re-registering" : "Registering"; |
| 124 | ALOGI("%s service %s", regStr.c_str(), name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 125 | |
| 126 | if (!manager->addService(name.c_str(), service, allowIsolated, dumpFlags).isOk()) { |
| 127 | ALOGE("Failed to register service %s", name.c_str()); |
| 128 | return false; |
| 129 | } |
| 130 | |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 131 | if (!reRegister) { |
Jon Spivack | 143a10d | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 132 | if(!manager->registerClientCallback(name, service, this).isOk()) { |
Steven Moreland | 6077425 | 2020-04-10 15:25:01 -0700 | [diff] [blame] | 133 | ALOGE("Failed to add client callback for service %s", name.c_str()); |
| 134 | return false; |
| 135 | } |
| 136 | |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 137 | // Only add this when a service is added for the first time, as it is not removed |
Steven Moreland | dd20e7c | 2020-04-10 11:51:14 -0700 | [diff] [blame] | 138 | mRegisteredServices[name] = { |
| 139 | .service = service, |
| 140 | .allowIsolated = allowIsolated, |
| 141 | .dumpFlags = dumpFlags |
| 142 | }; |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 143 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
Jon Spivack | 143a10d | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 148 | std::map<std::string, ClientCounterCallbackImpl::Service>::iterator ClientCounterCallbackImpl::assertRegisteredService(const sp<IBinder>& service) { |
Steven Moreland | dd20e7c | 2020-04-10 11:51:14 -0700 | [diff] [blame] | 149 | LOG_ALWAYS_FATAL_IF(service == nullptr, "Got onClients callback for null service"); |
| 150 | for (auto it = mRegisteredServices.begin(); it != mRegisteredServices.end(); ++it) { |
| 151 | auto const& [name, registered] = *it; |
| 152 | (void) name; |
| 153 | if (registered.service != service) continue; |
| 154 | return it; |
| 155 | } |
| 156 | LOG_ALWAYS_FATAL("Got callback on service which we did not register: %s", String8(service->getInterfaceDescriptor()).c_str()); |
| 157 | __builtin_unreachable(); |
| 158 | } |
| 159 | |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 160 | void ClientCounterCallbackImpl::forcePersist(bool persist) { |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 161 | mForcePersist = persist; |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 162 | if (!mForcePersist) { |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 163 | // Attempt a shutdown in case the number of clients hit 0 while the flag was on |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 164 | maybeTryShutdown(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | bool ClientCounterCallbackImpl::tryUnregister() { |
| 169 | auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager())); |
| 170 | |
| 171 | for (auto& [name, entry] : mRegisteredServices) { |
| 172 | bool success = manager->tryUnregisterService(name, entry.service).isOk(); |
| 173 | |
| 174 | if (!success) { |
| 175 | ALOGI("Failed to unregister service %s", name.c_str()); |
| 176 | return false; |
| 177 | } |
| 178 | entry.registered = false; |
| 179 | } |
| 180 | |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | void ClientCounterCallbackImpl::reRegister() { |
| 185 | for (auto& [name, entry] : mRegisteredServices) { |
| 186 | // re-register entry if not already registered |
| 187 | if (entry.registered) { |
| 188 | continue; |
| 189 | } |
| 190 | |
| 191 | if (!registerService(entry.service, name, entry.allowIsolated, |
| 192 | entry.dumpFlags)) { |
| 193 | // Must restart. Otherwise, clients will never be able to get a hold of this service. |
| 194 | LOG_ALWAYS_FATAL("Bad state: could not re-register services"); |
| 195 | } |
| 196 | |
| 197 | entry.registered = true; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void ClientCounterCallbackImpl::maybeTryShutdown() { |
| 202 | if (mForcePersist) { |
| 203 | ALOGI("Shutdown prevented by forcePersist override flag."); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | bool handledInCallback = false; |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 208 | if (mActiveServicesCallback != nullptr) { |
| 209 | bool hasClients = mNumConnectedServices != 0; |
| 210 | if (hasClients != mPreviousHasClients) { |
| 211 | handledInCallback = mActiveServicesCallback(hasClients); |
| 212 | mPreviousHasClients = hasClients; |
| 213 | } |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // If there is no callback defined or the callback did not handle this |
| 217 | // client count change event, try to shutdown the process if its services |
| 218 | // have no clients. |
| 219 | if (!handledInCallback && mNumConnectedServices == 0) { |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 220 | tryShutdown(); |
| 221 | } |
| 222 | } |
| 223 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 224 | /** |
| 225 | * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple |
| 226 | * invocations could occur on different threads however. |
| 227 | */ |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 228 | Status ClientCounterCallbackImpl::onClients(const sp<IBinder>& service, bool clients) { |
Steven Moreland | dd20e7c | 2020-04-10 11:51:14 -0700 | [diff] [blame] | 229 | auto & [name, registered] = *assertRegisteredService(service); |
| 230 | if (registered.clients == clients) { |
| 231 | LOG_ALWAYS_FATAL("Process already thought %s had clients: %d but servicemanager has " |
| 232 | "notified has clients: %d", name.c_str(), registered.clients, clients); |
| 233 | } |
| 234 | registered.clients = clients; |
| 235 | |
| 236 | // update cache count of clients |
| 237 | { |
| 238 | size_t numWithClients = 0; |
| 239 | for (const auto& [name, registered] : mRegisteredServices) { |
| 240 | (void) name; |
| 241 | if (registered.clients) numWithClients++; |
| 242 | } |
| 243 | mNumConnectedServices = numWithClients; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | ALOGI("Process has %zu (of %zu available) client(s) in use after notification %s has clients: %d", |
Steven Moreland | dd20e7c | 2020-04-10 11:51:14 -0700 | [diff] [blame] | 247 | mNumConnectedServices, mRegisteredServices.size(), name.c_str(), clients); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 248 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 249 | maybeTryShutdown(); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 250 | return Status::ok(); |
| 251 | } |
| 252 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 253 | void ClientCounterCallbackImpl::tryShutdown() { |
| 254 | ALOGI("Trying to shut down the service. No clients in use for any service in process."); |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 255 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 256 | if (tryUnregister()) { |
| 257 | ALOGI("Unregistered all clients and exiting"); |
| 258 | exit(EXIT_SUCCESS); |
| 259 | } |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 260 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 261 | reRegister(); |
| 262 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 263 | |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 264 | void ClientCounterCallbackImpl::setActiveServicesCallback(const std::function<bool(bool)>& |
| 265 | activeServicesCallback) { |
| 266 | mActiveServicesCallback = activeServicesCallback; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 269 | ClientCounterCallback::ClientCounterCallback() { |
Jon Spivack | 143a10d | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 270 | mImpl = sp<ClientCounterCallbackImpl>::make(); |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name, |
| 274 | bool allowIsolated, int dumpFlags) { |
| 275 | return mImpl->registerService(service, name, allowIsolated, dumpFlags); |
| 276 | } |
| 277 | |
| 278 | void ClientCounterCallback::forcePersist(bool persist) { |
| 279 | mImpl->forcePersist(persist); |
| 280 | } |
| 281 | |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 282 | void ClientCounterCallback::setActiveServicesCallback(const std::function<bool(bool)>& |
| 283 | activeServicesCallback) { |
| 284 | mImpl->setActiveServicesCallback(activeServicesCallback); |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | bool ClientCounterCallback::tryUnregister() { |
| 288 | return mImpl->tryUnregister(); |
| 289 | } |
| 290 | |
| 291 | void ClientCounterCallback::reRegister() { |
| 292 | mImpl->reRegister(); |
| 293 | } |
| 294 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 295 | } // namespace internal |
| 296 | |
| 297 | LazyServiceRegistrar::LazyServiceRegistrar() { |
| 298 | mClientCC = std::make_shared<internal::ClientCounterCallback>(); |
| 299 | } |
| 300 | |
| 301 | LazyServiceRegistrar& LazyServiceRegistrar::getInstance() { |
| 302 | static auto registrarInstance = new LazyServiceRegistrar(); |
| 303 | return *registrarInstance; |
| 304 | } |
| 305 | |
| 306 | status_t LazyServiceRegistrar::registerService(const sp<IBinder>& service, const std::string& name, |
| 307 | bool allowIsolated, int dumpFlags) { |
| 308 | if (!mClientCC->registerService(service, name, allowIsolated, dumpFlags)) { |
| 309 | return UNKNOWN_ERROR; |
| 310 | } |
| 311 | return OK; |
| 312 | } |
| 313 | |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 314 | void LazyServiceRegistrar::forcePersist(bool persist) { |
| 315 | mClientCC->forcePersist(persist); |
| 316 | } |
| 317 | |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 318 | void LazyServiceRegistrar::setActiveServicesCallback(const std::function<bool(bool)>& |
| 319 | activeServicesCallback) { |
| 320 | mClientCC->setActiveServicesCallback(activeServicesCallback); |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | bool LazyServiceRegistrar::tryUnregister() { |
| 324 | return mClientCC->tryUnregister(); |
| 325 | } |
| 326 | |
| 327 | void LazyServiceRegistrar::reRegister() { |
| 328 | mClientCC->reRegister(); |
| 329 | } |
| 330 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 331 | } // namespace hardware |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 332 | } // namespace android |