blob: 0f0af0b1428f5c7092f94e306a3412a4225b5700 [file] [log] [blame]
Jon Spivack9f503a42019-10-22 16:49:19 -07001/*
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
Jon Spivack9f503a42019-10-22 16:49:19 -070019#include <android/os/BnClientCallback.h>
20#include <android/os/IServiceManager.h>
Tomasz Wasilczykb70f4712024-06-21 11:58:54 -070021#include <binder/IPCThreadState.h>
22#include <binder/IServiceManager.h>
23#include <binder/LazyServiceRegistrar.h>
Jon Spivack9f503a42019-10-22 16:49:19 -070024
25namespace android {
26namespace binder {
27namespace internal {
28
29using AidlServiceManager = android::os::IServiceManager;
30
Jon Spivackdc6cb052020-10-27 19:29:14 -070031class ClientCounterCallbackImpl : public ::android::os::BnClientCallback {
Jon Spivack9f503a42019-10-22 16:49:19 -070032public:
Jon Spivackdc6cb052020-10-27 19:29:14 -070033 ClientCounterCallbackImpl() : mNumConnectedServices(0), mForcePersist(false) {}
Jon Spivack9f503a42019-10-22 16:49:19 -070034
35 bool registerService(const sp<IBinder>& service, const std::string& name,
36 bool allowIsolated, int dumpFlags);
Jon Spivacke17055a2020-03-06 13:58:01 -080037 void forcePersist(bool persist);
38
Amos Bianchi3f796942021-01-20 16:06:56 -080039 void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
Amos Bianchi844e19b2020-12-23 09:57:02 -080040
Steven Moreland7a5889c2021-06-22 19:18:28 +000041 bool tryUnregisterLocked();
Amos Bianchi844e19b2020-12-23 09:57:02 -080042
Steven Moreland7a5889c2021-06-22 19:18:28 +000043 void reRegisterLocked();
Amos Bianchi844e19b2020-12-23 09:57:02 -080044
Jon Spivack9f503a42019-10-22 16:49:19 -070045protected:
46 Status onClients(const sp<IBinder>& service, bool clients) override;
47
48private:
Steven Morelanddd20e7c2020-04-10 11:51:14 -070049 struct Service {
50 sp<IBinder> service;
51 bool allowIsolated;
52 int dumpFlags;
53
54 // whether, based on onClients calls, we know we have a client for this
55 // service or not
56 bool clients = false;
Xin Lica3e5792021-02-21 09:43:11 -080057 bool registered = true;
Steven Morelanddd20e7c2020-04-10 11:51:14 -070058 };
59
Steven Moreland7a5889c2021-06-22 19:18:28 +000060 bool registerServiceLocked(const sp<IBinder>& service, const std::string& name,
61 bool allowIsolated, int dumpFlags);
62
Steven Morelanddd20e7c2020-04-10 11:51:14 -070063 /**
64 * Looks up a service guaranteed to be registered (service from onClients).
65 */
66 std::map<std::string, Service>::iterator assertRegisteredService(const sp<IBinder>& service);
67
Jon Spivack9f503a42019-10-22 16:49:19 -070068 /**
69 * Unregisters all services that we can. If we can't unregister all, re-register other
70 * services.
71 */
Steven Moreland7a5889c2021-06-22 19:18:28 +000072 void tryShutdownLocked();
Jon Spivack9f503a42019-10-22 16:49:19 -070073
74 /**
Amos Bianchi844e19b2020-12-23 09:57:02 -080075 * Try to shutdown the process, unless:
76 * - 'forcePersist' is 'true', or
77 * - The active services count callback returns 'true', or
78 * - Some services have clients.
79 */
Steven Moreland7a5889c2021-06-22 19:18:28 +000080 void maybeTryShutdownLocked();
81
82 // for below
83 std::mutex mMutex;
Amos Bianchi844e19b2020-12-23 09:57:02 -080084
Steven Morelanddd20e7c2020-04-10 11:51:14 -070085 // count of services with clients
Jon Spivack9f503a42019-10-22 16:49:19 -070086 size_t mNumConnectedServices;
87
Amos Bianchi3f796942021-01-20 16:06:56 -080088 // previous value passed to the active services callback
89 std::optional<bool> mPreviousHasClients;
90
Steven Morelanddd20e7c2020-04-10 11:51:14 -070091 // map of registered names and services
Jon Spivackd9533c22020-01-27 22:19:22 +000092 std::map<std::string, Service> mRegisteredServices;
Jon Spivacke17055a2020-03-06 13:58:01 -080093
94 bool mForcePersist;
Amos Bianchi844e19b2020-12-23 09:57:02 -080095
Amos Bianchi3f796942021-01-20 16:06:56 -080096 // Callback used to report if there are services with clients
97 std::function<bool(bool)> mActiveServicesCallback;
Jon Spivack9f503a42019-10-22 16:49:19 -070098};
99
Jon Spivackdc6cb052020-10-27 19:29:14 -0700100class ClientCounterCallback {
101public:
102 ClientCounterCallback();
103
104 bool registerService(const sp<IBinder>& service, const std::string& name,
105 bool allowIsolated, int dumpFlags);
106
107 /**
108 * Set a flag to prevent services from automatically shutting down
109 */
110 void forcePersist(bool persist);
111
Amos Bianchi3f796942021-01-20 16:06:56 -0800112 void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
Amos Bianchi844e19b2020-12-23 09:57:02 -0800113
114 bool tryUnregister();
115
116 void reRegister();
117
Jon Spivackdc6cb052020-10-27 19:29:14 -0700118private:
119 sp<ClientCounterCallbackImpl> mImpl;
120};
121
122bool ClientCounterCallbackImpl::registerService(const sp<IBinder>& service, const std::string& name,
Jon Spivack9f503a42019-10-22 16:49:19 -0700123 bool allowIsolated, int dumpFlags) {
Steven Moreland7a5889c2021-06-22 19:18:28 +0000124 std::lock_guard<std::mutex> lock(mMutex);
125 return registerServiceLocked(service, name, allowIsolated, dumpFlags);
126}
127
128bool ClientCounterCallbackImpl::registerServiceLocked(const sp<IBinder>& service,
129 const std::string& name, bool allowIsolated,
130 int dumpFlags) {
Jon Spivack718470e2020-02-19 19:18:21 -0800131 auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
Jon Spivack9f503a42019-10-22 16:49:19 -0700132
Jon Spivackd9533c22020-01-27 22:19:22 +0000133 bool reRegister = mRegisteredServices.count(name) > 0;
134 std::string regStr = (reRegister) ? "Re-registering" : "Registering";
135 ALOGI("%s service %s", regStr.c_str(), name.c_str());
Jon Spivack9f503a42019-10-22 16:49:19 -0700136
Steven Moreland48c73e02021-05-10 18:31:34 +0000137 if (Status status = manager->addService(name.c_str(), service, allowIsolated, dumpFlags);
138 !status.isOk()) {
139 ALOGE("Failed to register service %s (%s)", name.c_str(), status.toString8().c_str());
Jon Spivack9f503a42019-10-22 16:49:19 -0700140 return false;
141 }
142
Jon Spivackd9533c22020-01-27 22:19:22 +0000143 if (!reRegister) {
Steven Moreland48c73e02021-05-10 18:31:34 +0000144 if (Status status =
145 manager->registerClientCallback(name, service,
146 sp<android::os::IClientCallback>::fromExisting(
147 this));
148 !status.isOk()) {
149 ALOGE("Failed to add client callback for service %s (%s)", name.c_str(),
150 status.toString8().c_str());
Steven Moreland60774252020-04-10 15:25:01 -0700151 return false;
152 }
153
Jon Spivackd9533c22020-01-27 22:19:22 +0000154 // Only add this when a service is added for the first time, as it is not removed
Steven Morelanddd20e7c2020-04-10 11:51:14 -0700155 mRegisteredServices[name] = {
156 .service = service,
157 .allowIsolated = allowIsolated,
158 .dumpFlags = dumpFlags
159 };
Jon Spivackd9533c22020-01-27 22:19:22 +0000160 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700161
162 return true;
163}
164
Jon Spivack143a10d2020-10-27 19:29:14 -0700165std::map<std::string, ClientCounterCallbackImpl::Service>::iterator ClientCounterCallbackImpl::assertRegisteredService(const sp<IBinder>& service) {
Steven Morelanddd20e7c2020-04-10 11:51:14 -0700166 LOG_ALWAYS_FATAL_IF(service == nullptr, "Got onClients callback for null service");
167 for (auto it = mRegisteredServices.begin(); it != mRegisteredServices.end(); ++it) {
168 auto const& [name, registered] = *it;
169 (void) name;
170 if (registered.service != service) continue;
171 return it;
172 }
173 LOG_ALWAYS_FATAL("Got callback on service which we did not register: %s", String8(service->getInterfaceDescriptor()).c_str());
174 __builtin_unreachable();
175}
176
Jon Spivackdc6cb052020-10-27 19:29:14 -0700177void ClientCounterCallbackImpl::forcePersist(bool persist) {
Steven Moreland7a5889c2021-06-22 19:18:28 +0000178 std::lock_guard<std::mutex> lock(mMutex);
Jon Spivacke17055a2020-03-06 13:58:01 -0800179 mForcePersist = persist;
Amos Bianchi3f796942021-01-20 16:06:56 -0800180 if (!mForcePersist) {
Jon Spivacke17055a2020-03-06 13:58:01 -0800181 // Attempt a shutdown in case the number of clients hit 0 while the flag was on
Steven Moreland7a5889c2021-06-22 19:18:28 +0000182 maybeTryShutdownLocked();
Amos Bianchi844e19b2020-12-23 09:57:02 -0800183 }
184}
185
Steven Moreland7a5889c2021-06-22 19:18:28 +0000186bool ClientCounterCallbackImpl::tryUnregisterLocked() {
Amos Bianchi844e19b2020-12-23 09:57:02 -0800187 auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
188
189 for (auto& [name, entry] : mRegisteredServices) {
Steven Moreland48c73e02021-05-10 18:31:34 +0000190 Status status = manager->tryUnregisterService(name, entry.service);
Amos Bianchi844e19b2020-12-23 09:57:02 -0800191
Steven Moreland48c73e02021-05-10 18:31:34 +0000192 if (!status.isOk()) {
193 ALOGI("Failed to unregister service %s (%s)", name.c_str(), status.toString8().c_str());
Amos Bianchi844e19b2020-12-23 09:57:02 -0800194 return false;
195 }
196 entry.registered = false;
197 }
198
199 return true;
200}
201
Steven Moreland7a5889c2021-06-22 19:18:28 +0000202void ClientCounterCallbackImpl::reRegisterLocked() {
Amos Bianchi844e19b2020-12-23 09:57:02 -0800203 for (auto& [name, entry] : mRegisteredServices) {
204 // re-register entry if not already registered
205 if (entry.registered) {
206 continue;
207 }
208
Steven Moreland7a5889c2021-06-22 19:18:28 +0000209 if (!registerServiceLocked(entry.service, name, entry.allowIsolated, entry.dumpFlags)) {
Amos Bianchi844e19b2020-12-23 09:57:02 -0800210 // Must restart. Otherwise, clients will never be able to get a hold of this service.
211 LOG_ALWAYS_FATAL("Bad state: could not re-register services");
212 }
213
214 entry.registered = true;
215 }
216}
217
Steven Moreland7a5889c2021-06-22 19:18:28 +0000218void ClientCounterCallbackImpl::maybeTryShutdownLocked() {
Amos Bianchi844e19b2020-12-23 09:57:02 -0800219 if (mForcePersist) {
220 ALOGI("Shutdown prevented by forcePersist override flag.");
221 return;
222 }
223
224 bool handledInCallback = false;
Amos Bianchi3f796942021-01-20 16:06:56 -0800225 if (mActiveServicesCallback != nullptr) {
226 bool hasClients = mNumConnectedServices != 0;
227 if (hasClients != mPreviousHasClients) {
228 handledInCallback = mActiveServicesCallback(hasClients);
229 mPreviousHasClients = hasClients;
230 }
Amos Bianchi844e19b2020-12-23 09:57:02 -0800231 }
232
233 // If there is no callback defined or the callback did not handle this
234 // client count change event, try to shutdown the process if its services
235 // have no clients.
236 if (!handledInCallback && mNumConnectedServices == 0) {
Steven Moreland7a5889c2021-06-22 19:18:28 +0000237 tryShutdownLocked();
Jon Spivacke17055a2020-03-06 13:58:01 -0800238 }
239}
240
Jon Spivackdc6cb052020-10-27 19:29:14 -0700241Status ClientCounterCallbackImpl::onClients(const sp<IBinder>& service, bool clients) {
Steven Moreland7a5889c2021-06-22 19:18:28 +0000242 std::lock_guard<std::mutex> lock(mMutex);
Steven Morelanddd20e7c2020-04-10 11:51:14 -0700243 auto & [name, registered] = *assertRegisteredService(service);
244 if (registered.clients == clients) {
245 LOG_ALWAYS_FATAL("Process already thought %s had clients: %d but servicemanager has "
246 "notified has clients: %d", name.c_str(), registered.clients, clients);
247 }
248 registered.clients = clients;
249
250 // update cache count of clients
251 {
252 size_t numWithClients = 0;
253 for (const auto& [name, registered] : mRegisteredServices) {
254 (void) name;
255 if (registered.clients) numWithClients++;
256 }
257 mNumConnectedServices = numWithClients;
Jon Spivack9f503a42019-10-22 16:49:19 -0700258 }
259
260 ALOGI("Process has %zu (of %zu available) client(s) in use after notification %s has clients: %d",
Steven Morelanddd20e7c2020-04-10 11:51:14 -0700261 mNumConnectedServices, mRegisteredServices.size(), name.c_str(), clients);
Jon Spivack9f503a42019-10-22 16:49:19 -0700262
Steven Moreland7a5889c2021-06-22 19:18:28 +0000263 maybeTryShutdownLocked();
Jon Spivack9f503a42019-10-22 16:49:19 -0700264 return Status::ok();
265}
266
Steven Moreland7a5889c2021-06-22 19:18:28 +0000267void ClientCounterCallbackImpl::tryShutdownLocked() {
268 ALOGI("Trying to shut down the service. No clients in use for any service in process.");
Jon Spivacke17055a2020-03-06 13:58:01 -0800269
Steven Moreland7a5889c2021-06-22 19:18:28 +0000270 if (tryUnregisterLocked()) {
271 ALOGI("Unregistered all clients and exiting");
272 exit(EXIT_SUCCESS);
273 }
Jon Spivacke17055a2020-03-06 13:58:01 -0800274
Steven Moreland7a5889c2021-06-22 19:18:28 +0000275 reRegisterLocked();
Amos Bianchi844e19b2020-12-23 09:57:02 -0800276}
Jon Spivack9f503a42019-10-22 16:49:19 -0700277
Amos Bianchi3f796942021-01-20 16:06:56 -0800278void ClientCounterCallbackImpl::setActiveServicesCallback(const std::function<bool(bool)>&
279 activeServicesCallback) {
Steven Moreland7a5889c2021-06-22 19:18:28 +0000280 std::lock_guard<std::mutex> lock(mMutex);
Amos Bianchi3f796942021-01-20 16:06:56 -0800281 mActiveServicesCallback = activeServicesCallback;
Jon Spivack9f503a42019-10-22 16:49:19 -0700282}
283
Jon Spivackdc6cb052020-10-27 19:29:14 -0700284ClientCounterCallback::ClientCounterCallback() {
Jon Spivack143a10d2020-10-27 19:29:14 -0700285 mImpl = sp<ClientCounterCallbackImpl>::make();
Jon Spivackdc6cb052020-10-27 19:29:14 -0700286}
287
288bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name,
289 bool allowIsolated, int dumpFlags) {
290 return mImpl->registerService(service, name, allowIsolated, dumpFlags);
291}
292
293void ClientCounterCallback::forcePersist(bool persist) {
294 mImpl->forcePersist(persist);
295}
296
Amos Bianchi3f796942021-01-20 16:06:56 -0800297void ClientCounterCallback::setActiveServicesCallback(const std::function<bool(bool)>&
298 activeServicesCallback) {
299 mImpl->setActiveServicesCallback(activeServicesCallback);
Amos Bianchi844e19b2020-12-23 09:57:02 -0800300}
301
302bool ClientCounterCallback::tryUnregister() {
Steven Moreland7a5889c2021-06-22 19:18:28 +0000303 // see comments in header, this should only be called from the active
304 // services callback, see also b/191781736
305 return mImpl->tryUnregisterLocked();
Amos Bianchi844e19b2020-12-23 09:57:02 -0800306}
307
308void ClientCounterCallback::reRegister() {
Steven Moreland7a5889c2021-06-22 19:18:28 +0000309 // see comments in header, this should only be called from the active
310 // services callback, see also b/191781736
311 mImpl->reRegisterLocked();
Amos Bianchi844e19b2020-12-23 09:57:02 -0800312}
313
Jon Spivack9f503a42019-10-22 16:49:19 -0700314} // namespace internal
315
316LazyServiceRegistrar::LazyServiceRegistrar() {
317 mClientCC = std::make_shared<internal::ClientCounterCallback>();
318}
319
320LazyServiceRegistrar& LazyServiceRegistrar::getInstance() {
321 static auto registrarInstance = new LazyServiceRegistrar();
322 return *registrarInstance;
323}
324
Steven Moreland9c864602023-04-13 20:29:33 +0000325LazyServiceRegistrar LazyServiceRegistrar::createExtraTestInstance() {
326 return LazyServiceRegistrar();
327}
328
Jon Spivack9f503a42019-10-22 16:49:19 -0700329status_t LazyServiceRegistrar::registerService(const sp<IBinder>& service, const std::string& name,
330 bool allowIsolated, int dumpFlags) {
331 if (!mClientCC->registerService(service, name, allowIsolated, dumpFlags)) {
332 return UNKNOWN_ERROR;
333 }
334 return OK;
335}
336
Jon Spivacke17055a2020-03-06 13:58:01 -0800337void LazyServiceRegistrar::forcePersist(bool persist) {
338 mClientCC->forcePersist(persist);
339}
340
Amos Bianchi3f796942021-01-20 16:06:56 -0800341void LazyServiceRegistrar::setActiveServicesCallback(const std::function<bool(bool)>&
342 activeServicesCallback) {
343 mClientCC->setActiveServicesCallback(activeServicesCallback);
Amos Bianchi844e19b2020-12-23 09:57:02 -0800344}
345
346bool LazyServiceRegistrar::tryUnregister() {
347 return mClientCC->tryUnregister();
348}
349
350void LazyServiceRegistrar::reRegister() {
351 mClientCC->reRegister();
352}
353
Jon Spivack9f503a42019-10-22 16:49:19 -0700354} // namespace hardware
Amos Bianchi844e19b2020-12-23 09:57:02 -0800355} // namespace android