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 | |
Steven Moreland | 48c73e0 | 2021-05-10 18:31:34 +0000 | [diff] [blame^] | 126 | if (Status status = manager->addService(name.c_str(), service, allowIsolated, dumpFlags); |
| 127 | !status.isOk()) { |
| 128 | ALOGE("Failed to register service %s (%s)", name.c_str(), status.toString8().c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 129 | return false; |
| 130 | } |
| 131 | |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 132 | if (!reRegister) { |
Steven Moreland | 48c73e0 | 2021-05-10 18:31:34 +0000 | [diff] [blame^] | 133 | if (Status status = |
| 134 | manager->registerClientCallback(name, service, |
| 135 | sp<android::os::IClientCallback>::fromExisting( |
| 136 | this)); |
| 137 | !status.isOk()) { |
| 138 | ALOGE("Failed to add client callback for service %s (%s)", name.c_str(), |
| 139 | status.toString8().c_str()); |
Steven Moreland | 6077425 | 2020-04-10 15:25:01 -0700 | [diff] [blame] | 140 | return false; |
| 141 | } |
| 142 | |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 143 | // 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] | 144 | mRegisteredServices[name] = { |
| 145 | .service = service, |
| 146 | .allowIsolated = allowIsolated, |
| 147 | .dumpFlags = dumpFlags |
| 148 | }; |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 149 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 150 | |
| 151 | return true; |
| 152 | } |
| 153 | |
Jon Spivack | 143a10d | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 154 | 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] | 155 | LOG_ALWAYS_FATAL_IF(service == nullptr, "Got onClients callback for null service"); |
| 156 | for (auto it = mRegisteredServices.begin(); it != mRegisteredServices.end(); ++it) { |
| 157 | auto const& [name, registered] = *it; |
| 158 | (void) name; |
| 159 | if (registered.service != service) continue; |
| 160 | return it; |
| 161 | } |
| 162 | LOG_ALWAYS_FATAL("Got callback on service which we did not register: %s", String8(service->getInterfaceDescriptor()).c_str()); |
| 163 | __builtin_unreachable(); |
| 164 | } |
| 165 | |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 166 | void ClientCounterCallbackImpl::forcePersist(bool persist) { |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 167 | mForcePersist = persist; |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 168 | if (!mForcePersist) { |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 169 | // 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] | 170 | maybeTryShutdown(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | bool ClientCounterCallbackImpl::tryUnregister() { |
| 175 | auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager())); |
| 176 | |
| 177 | for (auto& [name, entry] : mRegisteredServices) { |
Steven Moreland | 48c73e0 | 2021-05-10 18:31:34 +0000 | [diff] [blame^] | 178 | Status status = manager->tryUnregisterService(name, entry.service); |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 179 | |
Steven Moreland | 48c73e0 | 2021-05-10 18:31:34 +0000 | [diff] [blame^] | 180 | if (!status.isOk()) { |
| 181 | ALOGI("Failed to unregister service %s (%s)", name.c_str(), status.toString8().c_str()); |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | entry.registered = false; |
| 185 | } |
| 186 | |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | void ClientCounterCallbackImpl::reRegister() { |
| 191 | for (auto& [name, entry] : mRegisteredServices) { |
| 192 | // re-register entry if not already registered |
| 193 | if (entry.registered) { |
| 194 | continue; |
| 195 | } |
| 196 | |
| 197 | if (!registerService(entry.service, name, entry.allowIsolated, |
| 198 | entry.dumpFlags)) { |
| 199 | // Must restart. Otherwise, clients will never be able to get a hold of this service. |
| 200 | LOG_ALWAYS_FATAL("Bad state: could not re-register services"); |
| 201 | } |
| 202 | |
| 203 | entry.registered = true; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void ClientCounterCallbackImpl::maybeTryShutdown() { |
| 208 | if (mForcePersist) { |
| 209 | ALOGI("Shutdown prevented by forcePersist override flag."); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | bool handledInCallback = false; |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 214 | if (mActiveServicesCallback != nullptr) { |
| 215 | bool hasClients = mNumConnectedServices != 0; |
| 216 | if (hasClients != mPreviousHasClients) { |
| 217 | handledInCallback = mActiveServicesCallback(hasClients); |
| 218 | mPreviousHasClients = hasClients; |
| 219 | } |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // If there is no callback defined or the callback did not handle this |
| 223 | // client count change event, try to shutdown the process if its services |
| 224 | // have no clients. |
| 225 | if (!handledInCallback && mNumConnectedServices == 0) { |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 226 | tryShutdown(); |
| 227 | } |
| 228 | } |
| 229 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 230 | /** |
| 231 | * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple |
| 232 | * invocations could occur on different threads however. |
| 233 | */ |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 234 | Status ClientCounterCallbackImpl::onClients(const sp<IBinder>& service, bool clients) { |
Steven Moreland | dd20e7c | 2020-04-10 11:51:14 -0700 | [diff] [blame] | 235 | auto & [name, registered] = *assertRegisteredService(service); |
| 236 | if (registered.clients == clients) { |
| 237 | LOG_ALWAYS_FATAL("Process already thought %s had clients: %d but servicemanager has " |
| 238 | "notified has clients: %d", name.c_str(), registered.clients, clients); |
| 239 | } |
| 240 | registered.clients = clients; |
| 241 | |
| 242 | // update cache count of clients |
| 243 | { |
| 244 | size_t numWithClients = 0; |
| 245 | for (const auto& [name, registered] : mRegisteredServices) { |
| 246 | (void) name; |
| 247 | if (registered.clients) numWithClients++; |
| 248 | } |
| 249 | mNumConnectedServices = numWithClients; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | 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] | 253 | mNumConnectedServices, mRegisteredServices.size(), name.c_str(), clients); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 254 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 255 | maybeTryShutdown(); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 256 | return Status::ok(); |
| 257 | } |
| 258 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 259 | void ClientCounterCallbackImpl::tryShutdown() { |
| 260 | 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] | 261 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 262 | if (tryUnregister()) { |
| 263 | ALOGI("Unregistered all clients and exiting"); |
| 264 | exit(EXIT_SUCCESS); |
| 265 | } |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 266 | |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 267 | reRegister(); |
| 268 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 269 | |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 270 | void ClientCounterCallbackImpl::setActiveServicesCallback(const std::function<bool(bool)>& |
| 271 | activeServicesCallback) { |
| 272 | mActiveServicesCallback = activeServicesCallback; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 275 | ClientCounterCallback::ClientCounterCallback() { |
Jon Spivack | 143a10d | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 276 | mImpl = sp<ClientCounterCallbackImpl>::make(); |
Jon Spivack | dc6cb05 | 2020-10-27 19:29:14 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name, |
| 280 | bool allowIsolated, int dumpFlags) { |
| 281 | return mImpl->registerService(service, name, allowIsolated, dumpFlags); |
| 282 | } |
| 283 | |
| 284 | void ClientCounterCallback::forcePersist(bool persist) { |
| 285 | mImpl->forcePersist(persist); |
| 286 | } |
| 287 | |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 288 | void ClientCounterCallback::setActiveServicesCallback(const std::function<bool(bool)>& |
| 289 | activeServicesCallback) { |
| 290 | mImpl->setActiveServicesCallback(activeServicesCallback); |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | bool ClientCounterCallback::tryUnregister() { |
| 294 | return mImpl->tryUnregister(); |
| 295 | } |
| 296 | |
| 297 | void ClientCounterCallback::reRegister() { |
| 298 | mImpl->reRegister(); |
| 299 | } |
| 300 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 301 | } // namespace internal |
| 302 | |
| 303 | LazyServiceRegistrar::LazyServiceRegistrar() { |
| 304 | mClientCC = std::make_shared<internal::ClientCounterCallback>(); |
| 305 | } |
| 306 | |
| 307 | LazyServiceRegistrar& LazyServiceRegistrar::getInstance() { |
| 308 | static auto registrarInstance = new LazyServiceRegistrar(); |
| 309 | return *registrarInstance; |
| 310 | } |
| 311 | |
| 312 | status_t LazyServiceRegistrar::registerService(const sp<IBinder>& service, const std::string& name, |
| 313 | bool allowIsolated, int dumpFlags) { |
| 314 | if (!mClientCC->registerService(service, name, allowIsolated, dumpFlags)) { |
| 315 | return UNKNOWN_ERROR; |
| 316 | } |
| 317 | return OK; |
| 318 | } |
| 319 | |
Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 320 | void LazyServiceRegistrar::forcePersist(bool persist) { |
| 321 | mClientCC->forcePersist(persist); |
| 322 | } |
| 323 | |
Amos Bianchi | 3f79694 | 2021-01-20 16:06:56 -0800 | [diff] [blame] | 324 | void LazyServiceRegistrar::setActiveServicesCallback(const std::function<bool(bool)>& |
| 325 | activeServicesCallback) { |
| 326 | mClientCC->setActiveServicesCallback(activeServicesCallback); |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | bool LazyServiceRegistrar::tryUnregister() { |
| 330 | return mClientCC->tryUnregister(); |
| 331 | } |
| 332 | |
| 333 | void LazyServiceRegistrar::reRegister() { |
| 334 | mClientCC->reRegister(); |
| 335 | } |
| 336 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 337 | } // namespace hardware |
Amos Bianchi | 844e19b | 2020-12-23 09:57:02 -0800 | [diff] [blame] | 338 | } // namespace android |