blob: ce37dd0ca1d4a89da98222c77359a070dc6506e2 [file] [log] [blame]
Peter Kalauskas3373cd32018-10-24 15:37:00 -07001/*
2 * Copyright (C) 2018 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
Peter Kalauskas365a0f92020-06-10 11:43:01 -070017#define LOG_TAG "HidlLazyUtils"
18
Peter Kalauskas3373cd32018-10-24 15:37:00 -070019#include <hidl/HidlLazyUtils.h>
20#include <hidl/HidlTransportSupport.h>
21
22#include <android-base/logging.h>
23
24#include <android/hidl/manager/1.2/IClientCallback.h>
25#include <android/hidl/manager/1.2/IServiceManager.h>
26
27namespace android {
28namespace hardware {
29namespace details {
30
Steven Moreland40b3ab42019-01-25 13:23:20 -080031using ::android::hidl::base::V1_0::IBase;
32
Peter Kalauskas3373cd32018-10-24 15:37:00 -070033class ClientCounterCallback : public ::android::hidl::manager::V1_2::IClientCallback {
Steven Morelandbd603f72020-04-06 14:21:55 -070034 public:
35 ClientCounterCallback() {}
Peter Kalauskas3373cd32018-10-24 15:37:00 -070036
Steven Moreland40b3ab42019-01-25 13:23:20 -080037 bool addRegisteredService(const sp<IBase>& service, const std::string& name);
Peter Kalauskas3373cd32018-10-24 15:37:00 -070038
Amos Bianchi57341bb2020-12-23 11:11:37 -080039 bool tryUnregister();
40
41 void reRegister();
42
43 void setActiveServicesCountCallback(
44 const std::function<bool(int)>& activeServicesCountCallback);
45
Steven Morelandbd603f72020-04-06 14:21:55 -070046 protected:
Steven Moreland40b3ab42019-01-25 13:23:20 -080047 Return<void> onClients(const sp<IBase>& service, bool clients) override;
Peter Kalauskas3373cd32018-10-24 15:37:00 -070048
Steven Morelandbd603f72020-04-06 14:21:55 -070049 private:
50 struct Service {
51 sp<IBase> service;
52 std::string name;
53 bool clients = false;
Amos Bianchi57341bb2020-12-23 11:11:37 -080054 // Used to keep track of unregistered services to allow re-registry
55 bool registered = true;
Steven Morelandbd603f72020-04-06 14:21:55 -070056 };
57
58 /**
59 * Looks up service that is guaranteed to be registered (service from
60 * onClients).
61 */
62 Service& assertRegisteredService(const sp<IBase>& service);
63
Peter Kalauskas3373cd32018-10-24 15:37:00 -070064 /**
Steven Moreland40b3ab42019-01-25 13:23:20 -080065 * Registers or re-registers services. Returns whether successful.
66 */
67 bool registerService(const sp<IBase>& service, const std::string& name);
68
69 /**
70 * Unregisters all services that we can. If we can't unregister all, re-register other
71 * services.
72 */
73 void tryShutdown();
74
75 /**
Peter Kalauskas3373cd32018-10-24 15:37:00 -070076 * Number of services that have been registered.
77 */
Steven Moreland40b3ab42019-01-25 13:23:20 -080078 std::vector<Service> mRegisteredServices;
Amos Bianchi57341bb2020-12-23 11:11:37 -080079
80 /**
81 * Callback for reporting the number of services with clients.
82 */
83 std::function<bool(int)> mActiveServicesCountCallback;
Peter Kalauskas3373cd32018-10-24 15:37:00 -070084};
85
86class LazyServiceRegistrarImpl {
Steven Morelandbd603f72020-04-06 14:21:55 -070087 public:
Peter Kalauskas3373cd32018-10-24 15:37:00 -070088 LazyServiceRegistrarImpl() : mClientCallback(new ClientCounterCallback) {}
89
Peter Kalauskas7ce31552019-01-03 15:15:46 -080090 status_t registerService(const sp<::android::hidl::base::V1_0::IBase>& service,
91 const std::string& name);
Amos Bianchi57341bb2020-12-23 11:11:37 -080092 bool tryUnregister();
93 void reRegister();
94 void setActiveServicesCountCallback(
95 const std::function<bool(int)>& activeServicesCountCallback);
Peter Kalauskas3373cd32018-10-24 15:37:00 -070096
Steven Morelandbd603f72020-04-06 14:21:55 -070097 private:
Peter Kalauskas3373cd32018-10-24 15:37:00 -070098 sp<ClientCounterCallback> mClientCallback;
99};
100
Steven Moreland40b3ab42019-01-25 13:23:20 -0800101bool ClientCounterCallback::addRegisteredService(const sp<IBase>& service,
102 const std::string& name) {
103 bool success = registerService(service, name);
104
105 if (success) {
106 mRegisteredServices.push_back({service, name});
107 }
108
109 return success;
110}
111
Steven Morelandbd603f72020-04-06 14:21:55 -0700112ClientCounterCallback::Service& ClientCounterCallback::assertRegisteredService(
113 const sp<IBase>& service) {
114 for (Service& registered : mRegisteredServices) {
115 if (registered.service != service) continue;
116 return registered;
117 }
118 LOG(FATAL) << "Got callback on service " << getDescriptor(service.get())
119 << " which we did not register.";
120 __builtin_unreachable();
121}
122
Steven Moreland40b3ab42019-01-25 13:23:20 -0800123bool ClientCounterCallback::registerService(const sp<IBase>& service, const std::string& name) {
124 auto manager = hardware::defaultServiceManager1_2();
125
126 const std::string descriptor = getDescriptor(service.get());
127
128 LOG(INFO) << "Registering HAL: " << descriptor << " with name: " << name;
129
130 status_t res = android::hardware::details::registerAsServiceInternal(service, name);
131 if (res != android::OK) {
132 LOG(ERROR) << "Failed to register as service.";
133 return false;
134 }
135
136 bool ret = manager->registerClientCallback(getDescriptor(service.get()), name, service, this);
137 if (!ret) {
138 LOG(ERROR) << "Failed to add client callback.";
139 return false;
140 }
141
142 return true;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700143}
144
145/**
146 * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple
147 * invocations could occur on different threads however.
148 */
149Return<void> ClientCounterCallback::onClients(const sp<::android::hidl::base::V1_0::IBase>& service,
150 bool clients) {
Steven Morelandbd603f72020-04-06 14:21:55 -0700151 Service& registered = assertRegisteredService(service);
152 if (registered.clients == clients) {
153 LOG(FATAL) << "Process already thought " << getDescriptor(service.get()) << "/"
154 << registered.name << " had clients: " << registered.clients
155 << " but hwservicemanager has notified has clients: " << clients;
156 }
157 registered.clients = clients;
158
159 size_t numWithClients = 0;
160 for (const Service& registered : mRegisteredServices) {
161 if (registered.clients) numWithClients++;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700162 }
Steven Moreland40b3ab42019-01-25 13:23:20 -0800163
Steven Morelandbd603f72020-04-06 14:21:55 -0700164 LOG(INFO) << "Process has " << numWithClients << " (of " << mRegisteredServices.size()
Steven Moreland06d58be2019-02-04 14:20:13 -0800165 << " available) client(s) in use after notification " << getDescriptor(service.get())
Steven Morelandbd603f72020-04-06 14:21:55 -0700166 << "/" << registered.name << " has clients: " << clients;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700167
Amos Bianchi57341bb2020-12-23 11:11:37 -0800168 bool handledInCallback = false;
169 if (mActiveServicesCountCallback != nullptr) {
170 handledInCallback = mActiveServicesCountCallback(numWithClients);
171 }
172
173 // If there is no callback defined or the callback did not handle this
174 // client count change event, try to shutdown the process if its services
175 // have no clients.
176 if (!handledInCallback && numWithClients == 0) {
Steven Moreland40b3ab42019-01-25 13:23:20 -0800177 tryShutdown();
178 }
179
180 return Status::ok();
181}
182
Amos Bianchi57341bb2020-12-23 11:11:37 -0800183bool ClientCounterCallback::tryUnregister() {
Steven Moreland40b3ab42019-01-25 13:23:20 -0800184 auto manager = hardware::defaultServiceManager1_2();
185
Amos Bianchi57341bb2020-12-23 11:11:37 -0800186 for (Service& entry : mRegisteredServices) {
Steven Moreland40b3ab42019-01-25 13:23:20 -0800187 const std::string descriptor = getDescriptor(entry.service.get());
188 bool success = manager->tryUnregister(descriptor, entry.name, entry.service);
189
190 if (!success) {
Steven Moreland06d58be2019-02-04 14:20:13 -0800191 LOG(INFO) << "Failed to unregister HAL " << descriptor << "/" << entry.name;
Amos Bianchi57341bb2020-12-23 11:11:37 -0800192 return false;
Steven Moreland40b3ab42019-01-25 13:23:20 -0800193 }
Amos Bianchi57341bb2020-12-23 11:11:37 -0800194
195 // Mark the entry unregistered, but do not remove it (may still be re-registered)
196 entry.registered = false;
Steven Moreland40b3ab42019-01-25 13:23:20 -0800197 }
198
Amos Bianchi57341bb2020-12-23 11:11:37 -0800199 return true;
200}
Steven Moreland40b3ab42019-01-25 13:23:20 -0800201
Amos Bianchi57341bb2020-12-23 11:11:37 -0800202void ClientCounterCallback::reRegister() {
203 for (Service& entry : mRegisteredServices) {
204 // re-register entry if not already registered
205 if (entry.registered) {
206 continue;
207 }
Steven Moreland40b3ab42019-01-25 13:23:20 -0800208
Steven Moreland40b3ab42019-01-25 13:23:20 -0800209 if (!registerService(entry.service, entry.name)) {
210 // Must restart. Otherwise, clients will never be able to get ahold of this service.
211 LOG(FATAL) << "Bad state: could not re-register " << getDescriptor(entry.service.get());
212 }
Amos Bianchi57341bb2020-12-23 11:11:37 -0800213
214 entry.registered = true;
Steven Moreland40b3ab42019-01-25 13:23:20 -0800215 }
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700216}
217
Amos Bianchi57341bb2020-12-23 11:11:37 -0800218void ClientCounterCallback::tryShutdown() {
219 LOG(INFO) << "Trying to exit HAL. No clients in use for any service in process.";
220
221 if (tryUnregister()) {
222 LOG(INFO) << "Unregistered all clients and exiting";
223 exit(EXIT_SUCCESS);
224 }
225
226 // At this point, we failed to unregister some of the services, leaving the
227 // server in an inconsistent state. Re-register all services that were
228 // unregistered by tryUnregister().
229 reRegister();
230}
231
232void ClientCounterCallback::setActiveServicesCountCallback(
233 const std::function<bool(int)>& activeServicesCountCallback) {
234 mActiveServicesCountCallback = activeServicesCountCallback;
235}
236
Peter Kalauskas7ce31552019-01-03 15:15:46 -0800237status_t LazyServiceRegistrarImpl::registerService(
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700238 const sp<::android::hidl::base::V1_0::IBase>& service, const std::string& name) {
Steven Moreland40b3ab42019-01-25 13:23:20 -0800239 if (!mClientCallback->addRegisteredService(service, name)) {
240 return ::android::UNKNOWN_ERROR;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700241 }
Steven Moreland40b3ab42019-01-25 13:23:20 -0800242
243 return ::android::OK;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700244}
245
Amos Bianchi57341bb2020-12-23 11:11:37 -0800246bool LazyServiceRegistrarImpl::tryUnregister() {
247 return mClientCallback->tryUnregister();
248}
249
250void LazyServiceRegistrarImpl::reRegister() {
251 mClientCallback->reRegister();
252}
253
254void LazyServiceRegistrarImpl::setActiveServicesCountCallback(
255 const std::function<bool(int)>& activeServicesCountCallback) {
256 mClientCallback->setActiveServicesCountCallback(activeServicesCountCallback);
257}
258
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700259} // namespace details
260
261LazyServiceRegistrar::LazyServiceRegistrar() {
262 mImpl = std::make_shared<details::LazyServiceRegistrarImpl>();
263}
264
Peter Kalauskas1e969222019-08-14 12:13:11 -0700265LazyServiceRegistrar& LazyServiceRegistrar::getInstance() {
266 static auto registrarInstance = new LazyServiceRegistrar();
267 return *registrarInstance;
268}
269
Peter Kalauskas7ce31552019-01-03 15:15:46 -0800270status_t LazyServiceRegistrar::registerService(
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700271 const sp<::android::hidl::base::V1_0::IBase>& service, const std::string& name) {
Peter Kalauskas7ce31552019-01-03 15:15:46 -0800272 return mImpl->registerService(service, name);
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700273}
274
Amos Bianchi57341bb2020-12-23 11:11:37 -0800275bool LazyServiceRegistrar::tryUnregister() {
276 return mImpl->tryUnregister();
277}
278
279void LazyServiceRegistrar::reRegister() {
280 mImpl->reRegister();
281}
282
283void LazyServiceRegistrar::setActiveServicesCountCallback(
284 const std::function<bool(int)>& activeServicesCountCallback) {
285 mImpl->setActiveServicesCountCallback(activeServicesCountCallback);
286}
287
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700288} // namespace hardware
289} // namespace android