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