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