blob: 68ea8a6d79f71b1617cc18a618b7bb60aa94acdf [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
Amos Bianchi844e19b2020-12-23 09:57:02 -080017#include "log/log_main.h"
Jon Spivack9f503a42019-10-22 16:49:19 -070018#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
27namespace android {
28namespace binder {
29namespace internal {
30
31using AidlServiceManager = android::os::IServiceManager;
32
Jon Spivackdc6cb052020-10-27 19:29:14 -070033class ClientCounterCallbackImpl : public ::android::os::BnClientCallback {
Jon Spivack9f503a42019-10-22 16:49:19 -070034public:
Jon Spivackdc6cb052020-10-27 19:29:14 -070035 ClientCounterCallbackImpl() : mNumConnectedServices(0), mForcePersist(false) {}
Jon Spivack9f503a42019-10-22 16:49:19 -070036
37 bool registerService(const sp<IBinder>& service, const std::string& name,
38 bool allowIsolated, int dumpFlags);
Jon Spivacke17055a2020-03-06 13:58:01 -080039 void forcePersist(bool persist);
40
Amos Bianchi3f796942021-01-20 16:06:56 -080041 void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
Amos Bianchi844e19b2020-12-23 09:57:02 -080042
43 bool tryUnregister();
44
45 void reRegister();
46
Jon Spivack9f503a42019-10-22 16:49:19 -070047protected:
48 Status onClients(const sp<IBinder>& service, bool clients) override;
49
50private:
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 Bianchi844e19b2020-12-23 09:57:02 -080058 * 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 Spivack9f503a42019-10-22 16:49:19 -070066 * Counter of the number of services that currently have at least one client.
67 */
68 size_t mNumConnectedServices;
69
Amos Bianchi3f796942021-01-20 16:06:56 -080070 // previous value passed to the active services callback
71 std::optional<bool> mPreviousHasClients;
72
Jon Spivack9f503a42019-10-22 16:49:19 -070073 struct Service {
74 sp<IBinder> service;
Jon Spivack9f503a42019-10-22 16:49:19 -070075 bool allowIsolated;
76 int dumpFlags;
Amos Bianchi844e19b2020-12-23 09:57:02 -080077
78 bool registered = true;
Jon Spivack9f503a42019-10-22 16:49:19 -070079 };
80 /**
Jon Spivackd9533c22020-01-27 22:19:22 +000081 * Map of registered names and services
Jon Spivack9f503a42019-10-22 16:49:19 -070082 */
Jon Spivackd9533c22020-01-27 22:19:22 +000083 std::map<std::string, Service> mRegisteredServices;
Jon Spivacke17055a2020-03-06 13:58:01 -080084
85 bool mForcePersist;
Amos Bianchi844e19b2020-12-23 09:57:02 -080086
Amos Bianchi3f796942021-01-20 16:06:56 -080087 // Callback used to report if there are services with clients
88 std::function<bool(bool)> mActiveServicesCallback;
Jon Spivack9f503a42019-10-22 16:49:19 -070089};
90
Jon Spivackdc6cb052020-10-27 19:29:14 -070091class ClientCounterCallback {
92public:
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 Bianchi3f796942021-01-20 16:06:56 -0800103 void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
Amos Bianchi844e19b2020-12-23 09:57:02 -0800104
105 bool tryUnregister();
106
107 void reRegister();
108
Jon Spivackdc6cb052020-10-27 19:29:14 -0700109private:
110 sp<ClientCounterCallbackImpl> mImpl;
111};
112
113bool ClientCounterCallbackImpl::registerService(const sp<IBinder>& service, const std::string& name,
Jon Spivack9f503a42019-10-22 16:49:19 -0700114 bool allowIsolated, int dumpFlags) {
Jon Spivack718470e2020-02-19 19:18:21 -0800115 auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
Jon Spivack9f503a42019-10-22 16:49:19 -0700116
Jon Spivackd9533c22020-01-27 22:19:22 +0000117 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 Spivack9f503a42019-10-22 16:49:19 -0700120
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 Spivackd9533c22020-01-27 22:19:22 +0000126 if (!reRegister) {
Steven Moreland60774252020-04-10 15:25:01 -0700127 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 Spivackd9533c22020-01-27 22:19:22 +0000132 // 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 Spivack9f503a42019-10-22 16:49:19 -0700135
136 return true;
137}
138
Jon Spivackdc6cb052020-10-27 19:29:14 -0700139void ClientCounterCallbackImpl::forcePersist(bool persist) {
Jon Spivacke17055a2020-03-06 13:58:01 -0800140 mForcePersist = persist;
Amos Bianchi3f796942021-01-20 16:06:56 -0800141 if (!mForcePersist) {
Jon Spivacke17055a2020-03-06 13:58:01 -0800142 // Attempt a shutdown in case the number of clients hit 0 while the flag was on
Amos Bianchi844e19b2020-12-23 09:57:02 -0800143 maybeTryShutdown();
144 }
145}
146
147bool 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
163void 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
180void ClientCounterCallbackImpl::maybeTryShutdown() {
181 if (mForcePersist) {
182 ALOGI("Shutdown prevented by forcePersist override flag.");
183 return;
184 }
185
186 bool handledInCallback = false;
Amos Bianchi3f796942021-01-20 16:06:56 -0800187 if (mActiveServicesCallback != nullptr) {
188 bool hasClients = mNumConnectedServices != 0;
189 if (hasClients != mPreviousHasClients) {
190 handledInCallback = mActiveServicesCallback(hasClients);
191 mPreviousHasClients = hasClients;
192 }
Amos Bianchi844e19b2020-12-23 09:57:02 -0800193 }
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 Spivacke17055a2020-03-06 13:58:01 -0800199 tryShutdown();
200 }
201}
202
Jon Spivack9f503a42019-10-22 16:49:19 -0700203/**
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 Spivackdc6cb052020-10-27 19:29:14 -0700207Status ClientCounterCallbackImpl::onClients(const sp<IBinder>& service, bool clients) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700208 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 Bianchi844e19b2020-12-23 09:57:02 -0800218 maybeTryShutdown();
Jon Spivack9f503a42019-10-22 16:49:19 -0700219 return Status::ok();
220}
221
Amos Bianchi844e19b2020-12-23 09:57:02 -0800222 void ClientCounterCallbackImpl::tryShutdown() {
223 ALOGI("Trying to shut down the service. No clients in use for any service in process.");
Jon Spivacke17055a2020-03-06 13:58:01 -0800224
Amos Bianchi844e19b2020-12-23 09:57:02 -0800225 if (tryUnregister()) {
226 ALOGI("Unregistered all clients and exiting");
227 exit(EXIT_SUCCESS);
228 }
Jon Spivacke17055a2020-03-06 13:58:01 -0800229
Amos Bianchi844e19b2020-12-23 09:57:02 -0800230 reRegister();
231}
Jon Spivack9f503a42019-10-22 16:49:19 -0700232
Amos Bianchi3f796942021-01-20 16:06:56 -0800233void ClientCounterCallbackImpl::setActiveServicesCallback(const std::function<bool(bool)>&
234 activeServicesCallback) {
235 mActiveServicesCallback = activeServicesCallback;
Jon Spivack9f503a42019-10-22 16:49:19 -0700236}
237
Jon Spivackdc6cb052020-10-27 19:29:14 -0700238ClientCounterCallback::ClientCounterCallback() {
239 mImpl = new ClientCounterCallbackImpl();
240}
241
242bool 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
247void ClientCounterCallback::forcePersist(bool persist) {
248 mImpl->forcePersist(persist);
249}
250
Amos Bianchi3f796942021-01-20 16:06:56 -0800251void ClientCounterCallback::setActiveServicesCallback(const std::function<bool(bool)>&
252 activeServicesCallback) {
253 mImpl->setActiveServicesCallback(activeServicesCallback);
Amos Bianchi844e19b2020-12-23 09:57:02 -0800254}
255
256bool ClientCounterCallback::tryUnregister() {
257 return mImpl->tryUnregister();
258}
259
260void ClientCounterCallback::reRegister() {
261 mImpl->reRegister();
262}
263
Jon Spivack9f503a42019-10-22 16:49:19 -0700264} // namespace internal
265
266LazyServiceRegistrar::LazyServiceRegistrar() {
267 mClientCC = std::make_shared<internal::ClientCounterCallback>();
268}
269
270LazyServiceRegistrar& LazyServiceRegistrar::getInstance() {
271 static auto registrarInstance = new LazyServiceRegistrar();
272 return *registrarInstance;
273}
274
275status_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 Spivacke17055a2020-03-06 13:58:01 -0800283void LazyServiceRegistrar::forcePersist(bool persist) {
284 mClientCC->forcePersist(persist);
285}
286
Amos Bianchi3f796942021-01-20 16:06:56 -0800287void LazyServiceRegistrar::setActiveServicesCallback(const std::function<bool(bool)>&
288 activeServicesCallback) {
289 mClientCC->setActiveServicesCallback(activeServicesCallback);
Amos Bianchi844e19b2020-12-23 09:57:02 -0800290}
291
292bool LazyServiceRegistrar::tryUnregister() {
293 return mClientCC->tryUnregister();
294}
295
296void LazyServiceRegistrar::reRegister() {
297 mClientCC->reRegister();
298}
299
Jon Spivack9f503a42019-10-22 16:49:19 -0700300} // namespace hardware
Amos Bianchi844e19b2020-12-23 09:57:02 -0800301} // namespace android