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 | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 41 | void setActiveServicesCountCallback(const std::function<bool(int)>& |
| 42 | activeServicesCountCallback); |
| 43 | |
| 44 | bool tryUnregister(); |
| 45 | |
| 46 | void reRegister(); |
| 47 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 48 | protected: |
| 49 | Status onClients(const sp<IBinder>& service, bool clients) override; |
| 50 | |
| 51 | private: |
| 52 | /** |
| 53 | * Unregisters all services that we can. If we can't unregister all, re-register other |
| 54 | * services. |
| 55 | */ |
| 56 | void tryShutdown(); |
| 57 | |
| 58 | /** |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 59 | * Try to shutdown the process, unless: |
| 60 | * - 'forcePersist' is 'true', or |
| 61 | * - The active services count callback returns 'true', or |
| 62 | * - Some services have clients. |
| 63 | */ |
| 64 | void maybeTryShutdown(); |
| 65 | |
| 66 | /* |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 67 | * Counter of the number of services that currently have at least one client. |
| 68 | */ |
| 69 | size_t mNumConnectedServices; |
| 70 | |
| 71 | struct Service { |
| 72 | sp<IBinder> service; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 73 | bool allowIsolated; |
| 74 | int dumpFlags; |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 75 | |
| 76 | bool registered = true; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 77 | }; |
| 78 | /** |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 79 | * Map of registered names and services |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 80 | */ |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 81 | std::map<std::string, Service> mRegisteredServices; |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 82 | |
| 83 | bool mForcePersist; |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 84 | |
| 85 | // Callback used to report the number of services with clients |
| 86 | std::function<bool(int)> mActiveServicesCountCallback; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 89 | class ClientCounterCallback { |
| 90 | public: |
| 91 | ClientCounterCallback(); |
| 92 | |
| 93 | bool registerService(const sp<IBinder>& service, const std::string& name, |
| 94 | bool allowIsolated, int dumpFlags); |
| 95 | |
| 96 | /** |
| 97 | * Set a flag to prevent services from automatically shutting down |
| 98 | */ |
| 99 | void forcePersist(bool persist); |
| 100 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 101 | void setActiveServicesCountCallback(const std::function<bool(int)>& |
| 102 | activeServicesCountCallback); |
| 103 | |
| 104 | bool tryUnregister(); |
| 105 | |
| 106 | void reRegister(); |
| 107 | |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 108 | private: |
| 109 | sp<ClientCounterCallbackImpl> mImpl; |
| 110 | }; |
| 111 | |
| 112 | bool ClientCounterCallbackImpl::registerService(const sp<IBinder>& service, const std::string& name, |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 113 | bool allowIsolated, int dumpFlags) { |
Jon Spivack | 718470e | 2020-02-19 19:18:21 -0800 | [diff] [blame] | 114 | auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager())); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 115 | |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 116 | bool reRegister = mRegisteredServices.count(name) > 0; |
| 117 | std::string regStr = (reRegister) ? "Re-registering" : "Registering"; |
| 118 | ALOGI("%s service %s", regStr.c_str(), name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 119 | |
| 120 | if (!manager->addService(name.c_str(), service, allowIsolated, dumpFlags).isOk()) { |
| 121 | ALOGE("Failed to register service %s", name.c_str()); |
| 122 | return false; |
| 123 | } |
| 124 | |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 125 | if (!reRegister) { |
Steven Moreland | 6077425 | 2020-04-10 15:25:01 -0700 | [diff] [blame] | 126 | if (!manager->registerClientCallback(name, service, this).isOk()) { |
| 127 | ALOGE("Failed to add client callback for service %s", name.c_str()); |
| 128 | return false; |
| 129 | } |
| 130 | |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 131 | // Only add this when a service is added for the first time, as it is not removed |
| 132 | mRegisteredServices[name] = {service, allowIsolated, dumpFlags}; |
| 133 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 138 | void ClientCounterCallbackImpl::forcePersist(bool persist) { |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 139 | mForcePersist = persist; |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 140 | if (!mForcePersist && mNumConnectedServices == 0) { |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 141 | // 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^] | 142 | maybeTryShutdown(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | bool ClientCounterCallbackImpl::tryUnregister() { |
| 147 | auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager())); |
| 148 | |
| 149 | for (auto& [name, entry] : mRegisteredServices) { |
| 150 | bool success = manager->tryUnregisterService(name, entry.service).isOk(); |
| 151 | |
| 152 | if (!success) { |
| 153 | ALOGI("Failed to unregister service %s", name.c_str()); |
| 154 | return false; |
| 155 | } |
| 156 | entry.registered = false; |
| 157 | } |
| 158 | |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | void ClientCounterCallbackImpl::reRegister() { |
| 163 | for (auto& [name, entry] : mRegisteredServices) { |
| 164 | // re-register entry if not already registered |
| 165 | if (entry.registered) { |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | if (!registerService(entry.service, name, entry.allowIsolated, |
| 170 | entry.dumpFlags)) { |
| 171 | // Must restart. Otherwise, clients will never be able to get a hold of this service. |
| 172 | LOG_ALWAYS_FATAL("Bad state: could not re-register services"); |
| 173 | } |
| 174 | |
| 175 | entry.registered = true; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void ClientCounterCallbackImpl::maybeTryShutdown() { |
| 180 | if (mForcePersist) { |
| 181 | ALOGI("Shutdown prevented by forcePersist override flag."); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | bool handledInCallback = false; |
| 186 | if (mActiveServicesCountCallback != nullptr) { |
| 187 | handledInCallback = mActiveServicesCountCallback(mNumConnectedServices); |
| 188 | } |
| 189 | |
| 190 | // If there is no callback defined or the callback did not handle this |
| 191 | // client count change event, try to shutdown the process if its services |
| 192 | // have no clients. |
| 193 | if (!handledInCallback && mNumConnectedServices == 0) { |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 194 | tryShutdown(); |
| 195 | } |
| 196 | } |
| 197 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 198 | /** |
| 199 | * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple |
| 200 | * invocations could occur on different threads however. |
| 201 | */ |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 202 | Status ClientCounterCallbackImpl::onClients(const sp<IBinder>& service, bool clients) { |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 203 | if (clients) { |
| 204 | mNumConnectedServices++; |
| 205 | } else { |
| 206 | mNumConnectedServices--; |
| 207 | } |
| 208 | |
| 209 | ALOGI("Process has %zu (of %zu available) client(s) in use after notification %s has clients: %d", |
| 210 | mNumConnectedServices, mRegisteredServices.size(), |
| 211 | String8(service->getInterfaceDescriptor()).string(), clients); |
| 212 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 213 | maybeTryShutdown(); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 214 | return Status::ok(); |
| 215 | } |
| 216 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 217 | void ClientCounterCallbackImpl::tryShutdown() { |
| 218 | 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] | 219 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 220 | if (tryUnregister()) { |
| 221 | ALOGI("Unregistered all clients and exiting"); |
| 222 | exit(EXIT_SUCCESS); |
| 223 | } |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 224 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 225 | reRegister(); |
| 226 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 227 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 228 | void ClientCounterCallbackImpl::setActiveServicesCountCallback(const std::function<bool(int)>& |
| 229 | activeServicesCountCallback) { |
| 230 | mActiveServicesCountCallback = activeServicesCountCallback; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 233 | ClientCounterCallback::ClientCounterCallback() { |
| 234 | mImpl = new ClientCounterCallbackImpl(); |
| 235 | } |
| 236 | |
| 237 | bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name, |
| 238 | bool allowIsolated, int dumpFlags) { |
| 239 | return mImpl->registerService(service, name, allowIsolated, dumpFlags); |
| 240 | } |
| 241 | |
| 242 | void ClientCounterCallback::forcePersist(bool persist) { |
| 243 | mImpl->forcePersist(persist); |
| 244 | } |
| 245 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 246 | void ClientCounterCallback::setActiveServicesCountCallback(const std::function<bool(int)>& |
| 247 | activeServicesCountCallback) { |
| 248 | mImpl->setActiveServicesCountCallback(activeServicesCountCallback); |
| 249 | } |
| 250 | |
| 251 | bool ClientCounterCallback::tryUnregister() { |
| 252 | return mImpl->tryUnregister(); |
| 253 | } |
| 254 | |
| 255 | void ClientCounterCallback::reRegister() { |
| 256 | mImpl->reRegister(); |
| 257 | } |
| 258 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 259 | } // namespace internal |
| 260 | |
| 261 | LazyServiceRegistrar::LazyServiceRegistrar() { |
| 262 | mClientCC = std::make_shared<internal::ClientCounterCallback>(); |
| 263 | } |
| 264 | |
| 265 | LazyServiceRegistrar& LazyServiceRegistrar::getInstance() { |
| 266 | static auto registrarInstance = new LazyServiceRegistrar(); |
| 267 | return *registrarInstance; |
| 268 | } |
| 269 | |
| 270 | status_t LazyServiceRegistrar::registerService(const sp<IBinder>& service, const std::string& name, |
| 271 | bool allowIsolated, int dumpFlags) { |
| 272 | if (!mClientCC->registerService(service, name, allowIsolated, dumpFlags)) { |
| 273 | return UNKNOWN_ERROR; |
| 274 | } |
| 275 | return OK; |
| 276 | } |
| 277 | |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 278 | void LazyServiceRegistrar::forcePersist(bool persist) { |
| 279 | mClientCC->forcePersist(persist); |
| 280 | } |
| 281 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 282 | void LazyServiceRegistrar::setActiveServicesCountCallback(const std::function<bool(int)>& |
| 283 | activeServicesCountCallback) { |
| 284 | mClientCC->setActiveServicesCountCallback(activeServicesCountCallback); |
| 285 | } |
| 286 | |
| 287 | bool LazyServiceRegistrar::tryUnregister() { |
| 288 | return mClientCC->tryUnregister(); |
| 289 | } |
| 290 | |
| 291 | void LazyServiceRegistrar::reRegister() { |
| 292 | mClientCC->reRegister(); |
| 293 | } |
| 294 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 295 | } // namespace hardware |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame^] | 296 | } // namespace android |