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