blob: f2c5139b5605ec95ba4f45852bf1c015096a3df4 [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
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
26namespace android {
27namespace binder {
28namespace internal {
29
30using AidlServiceManager = android::os::IServiceManager;
31
Jon Spivackdc6cb052020-10-27 19:29:14 -070032class ClientCounterCallbackImpl : public ::android::os::BnClientCallback {
Jon Spivack9f503a42019-10-22 16:49:19 -070033public:
Jon Spivackdc6cb052020-10-27 19:29:14 -070034 ClientCounterCallbackImpl() : mNumConnectedServices(0), mForcePersist(false) {}
Jon Spivack9f503a42019-10-22 16:49:19 -070035
36 bool registerService(const sp<IBinder>& service, const std::string& name,
37 bool allowIsolated, int dumpFlags);
Jon Spivacke17055a2020-03-06 13:58:01 -080038 void forcePersist(bool persist);
39
Jon Spivack9f503a42019-10-22 16:49:19 -070040protected:
41 Status onClients(const sp<IBinder>& service, bool clients) override;
42
43private:
44 /**
45 * Unregisters all services that we can. If we can't unregister all, re-register other
46 * services.
47 */
48 void tryShutdown();
49
50 /**
51 * Counter of the number of services that currently have at least one client.
52 */
53 size_t mNumConnectedServices;
54
55 struct Service {
56 sp<IBinder> service;
Jon Spivack9f503a42019-10-22 16:49:19 -070057 bool allowIsolated;
58 int dumpFlags;
59 };
60 /**
Jon Spivackd9533c22020-01-27 22:19:22 +000061 * Map of registered names and services
Jon Spivack9f503a42019-10-22 16:49:19 -070062 */
Jon Spivackd9533c22020-01-27 22:19:22 +000063 std::map<std::string, Service> mRegisteredServices;
Jon Spivacke17055a2020-03-06 13:58:01 -080064
65 bool mForcePersist;
Jon Spivack9f503a42019-10-22 16:49:19 -070066};
67
Jon Spivackdc6cb052020-10-27 19:29:14 -070068class ClientCounterCallback {
69public:
70 ClientCounterCallback();
71
72 bool registerService(const sp<IBinder>& service, const std::string& name,
73 bool allowIsolated, int dumpFlags);
74
75 /**
76 * Set a flag to prevent services from automatically shutting down
77 */
78 void forcePersist(bool persist);
79
80private:
81 sp<ClientCounterCallbackImpl> mImpl;
82};
83
84bool ClientCounterCallbackImpl::registerService(const sp<IBinder>& service, const std::string& name,
Jon Spivack9f503a42019-10-22 16:49:19 -070085 bool allowIsolated, int dumpFlags) {
Jon Spivack718470e2020-02-19 19:18:21 -080086 auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
Jon Spivack9f503a42019-10-22 16:49:19 -070087
Jon Spivackd9533c22020-01-27 22:19:22 +000088 bool reRegister = mRegisteredServices.count(name) > 0;
89 std::string regStr = (reRegister) ? "Re-registering" : "Registering";
90 ALOGI("%s service %s", regStr.c_str(), name.c_str());
Jon Spivack9f503a42019-10-22 16:49:19 -070091
92 if (!manager->addService(name.c_str(), service, allowIsolated, dumpFlags).isOk()) {
93 ALOGE("Failed to register service %s", name.c_str());
94 return false;
95 }
96
Jon Spivackd9533c22020-01-27 22:19:22 +000097 if (!reRegister) {
Steven Moreland60774252020-04-10 15:25:01 -070098 if (!manager->registerClientCallback(name, service, this).isOk()) {
99 ALOGE("Failed to add client callback for service %s", name.c_str());
100 return false;
101 }
102
Jon Spivackd9533c22020-01-27 22:19:22 +0000103 // Only add this when a service is added for the first time, as it is not removed
104 mRegisteredServices[name] = {service, allowIsolated, dumpFlags};
105 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700106
107 return true;
108}
109
Jon Spivackdc6cb052020-10-27 19:29:14 -0700110void ClientCounterCallbackImpl::forcePersist(bool persist) {
Jon Spivacke17055a2020-03-06 13:58:01 -0800111 mForcePersist = persist;
112 if(!mForcePersist) {
113 // Attempt a shutdown in case the number of clients hit 0 while the flag was on
114 tryShutdown();
115 }
116}
117
Jon Spivack9f503a42019-10-22 16:49:19 -0700118/**
119 * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple
120 * invocations could occur on different threads however.
121 */
Jon Spivackdc6cb052020-10-27 19:29:14 -0700122Status ClientCounterCallbackImpl::onClients(const sp<IBinder>& service, bool clients) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700123 if (clients) {
124 mNumConnectedServices++;
125 } else {
126 mNumConnectedServices--;
127 }
128
129 ALOGI("Process has %zu (of %zu available) client(s) in use after notification %s has clients: %d",
130 mNumConnectedServices, mRegisteredServices.size(),
131 String8(service->getInterfaceDescriptor()).string(), clients);
132
Jon Spivacke17055a2020-03-06 13:58:01 -0800133 tryShutdown();
Jon Spivack9f503a42019-10-22 16:49:19 -0700134 return Status::ok();
135}
136
Jon Spivackdc6cb052020-10-27 19:29:14 -0700137void ClientCounterCallbackImpl::tryShutdown() {
Jon Spivacke17055a2020-03-06 13:58:01 -0800138 if(mNumConnectedServices > 0) {
139 // Should only shut down if there are no clients
140 return;
141 }
142
143 if(mForcePersist) {
144 ALOGI("Shutdown prevented by forcePersist override flag.");
145 return;
146 }
147
Jon Spivack9f503a42019-10-22 16:49:19 -0700148 ALOGI("Trying to shut down the service. No clients in use for any service in process.");
149
Jon Spivack718470e2020-02-19 19:18:21 -0800150 auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
Jon Spivack9f503a42019-10-22 16:49:19 -0700151
152 auto unRegisterIt = mRegisteredServices.begin();
153 for (; unRegisterIt != mRegisteredServices.end(); ++unRegisterIt) {
154 auto& entry = (*unRegisterIt);
155
Jon Spivackd9533c22020-01-27 22:19:22 +0000156 bool success = manager->tryUnregisterService(entry.first, entry.second.service).isOk();
157
Jon Spivack9f503a42019-10-22 16:49:19 -0700158 if (!success) {
Jon Spivackd9533c22020-01-27 22:19:22 +0000159 ALOGI("Failed to unregister service %s", entry.first.c_str());
Jon Spivack9f503a42019-10-22 16:49:19 -0700160 break;
161 }
162 }
163
164 if (unRegisterIt == mRegisteredServices.end()) {
165 ALOGI("Unregistered all clients and exiting");
166 exit(EXIT_SUCCESS);
167 }
168
169 for (auto reRegisterIt = mRegisteredServices.begin(); reRegisterIt != unRegisterIt;
170 reRegisterIt++) {
171 auto& entry = (*reRegisterIt);
172
173 // re-register entry
Jon Spivackd9533c22020-01-27 22:19:22 +0000174 if (!registerService(entry.second.service, entry.first, entry.second.allowIsolated,
175 entry.second.dumpFlags)) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700176 // Must restart. Otherwise, clients will never be able to get a hold of this service.
177 ALOGE("Bad state: could not re-register services");
178 }
179 }
180}
181
Jon Spivackdc6cb052020-10-27 19:29:14 -0700182ClientCounterCallback::ClientCounterCallback() {
183 mImpl = new ClientCounterCallbackImpl();
184}
185
186bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name,
187 bool allowIsolated, int dumpFlags) {
188 return mImpl->registerService(service, name, allowIsolated, dumpFlags);
189}
190
191void ClientCounterCallback::forcePersist(bool persist) {
192 mImpl->forcePersist(persist);
193}
194
Jon Spivack9f503a42019-10-22 16:49:19 -0700195} // namespace internal
196
197LazyServiceRegistrar::LazyServiceRegistrar() {
198 mClientCC = std::make_shared<internal::ClientCounterCallback>();
199}
200
201LazyServiceRegistrar& LazyServiceRegistrar::getInstance() {
202 static auto registrarInstance = new LazyServiceRegistrar();
203 return *registrarInstance;
204}
205
206status_t LazyServiceRegistrar::registerService(const sp<IBinder>& service, const std::string& name,
207 bool allowIsolated, int dumpFlags) {
208 if (!mClientCC->registerService(service, name, allowIsolated, dumpFlags)) {
209 return UNKNOWN_ERROR;
210 }
211 return OK;
212}
213
Jon Spivacke17055a2020-03-06 13:58:01 -0800214void LazyServiceRegistrar::forcePersist(bool persist) {
215 mClientCC->forcePersist(persist);
216}
217
Jon Spivack9f503a42019-10-22 16:49:19 -0700218} // namespace hardware
219} // namespace android