blob: be7470f5fdbd3bf466ba8d11b821d19ce2f6c8cd [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
Amos Bianchifb918632021-01-20 17:41:02 -080043 void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
Amos Bianchi57341bb2020-12-23 11:11:37 -080044
Steven Morelandbd603f72020-04-06 14:21:55 -070045 protected:
Steven Moreland40b3ab42019-01-25 13:23:20 -080046 Return<void> onClients(const sp<IBase>& service, bool clients) override;
Peter Kalauskas3373cd32018-10-24 15:37:00 -070047
Steven Morelandbd603f72020-04-06 14:21:55 -070048 private:
49 struct Service {
50 sp<IBase> service;
51 std::string name;
52 bool clients = false;
Amos Bianchi57341bb2020-12-23 11:11:37 -080053 // Used to keep track of unregistered services to allow re-registry
54 bool registered = true;
Steven Morelandbd603f72020-04-06 14:21:55 -070055 };
56
57 /**
58 * Looks up service that is guaranteed to be registered (service from
59 * onClients).
60 */
61 Service& assertRegisteredService(const sp<IBase>& service);
62
Peter Kalauskas3373cd32018-10-24 15:37:00 -070063 /**
Steven Moreland40b3ab42019-01-25 13:23:20 -080064 * Registers or re-registers services. Returns whether successful.
65 */
66 bool registerService(const sp<IBase>& service, const std::string& name);
67
68 /**
69 * Unregisters all services that we can. If we can't unregister all, re-register other
70 * services.
71 */
72 void tryShutdown();
73
74 /**
Peter Kalauskas3373cd32018-10-24 15:37:00 -070075 * Number of services that have been registered.
76 */
Steven Moreland40b3ab42019-01-25 13:23:20 -080077 std::vector<Service> mRegisteredServices;
Amos Bianchi57341bb2020-12-23 11:11:37 -080078
79 /**
80 * Callback for reporting the number of services with clients.
81 */
Amos Bianchifb918632021-01-20 17:41:02 -080082 std::function<bool(bool)> mActiveServicesCallback;
83
84 /**
85 * Previous value passed to the active services callback.
86 */
87 std::optional<bool> mPreviousHasClients;
Peter Kalauskas3373cd32018-10-24 15:37:00 -070088};
89
90class LazyServiceRegistrarImpl {
Steven Morelandbd603f72020-04-06 14:21:55 -070091 public:
Peter Kalauskas3373cd32018-10-24 15:37:00 -070092 LazyServiceRegistrarImpl() : mClientCallback(new ClientCounterCallback) {}
93
Peter Kalauskas7ce31552019-01-03 15:15:46 -080094 status_t registerService(const sp<::android::hidl::base::V1_0::IBase>& service,
95 const std::string& name);
Amos Bianchi57341bb2020-12-23 11:11:37 -080096 bool tryUnregister();
97 void reRegister();
Amos Bianchifb918632021-01-20 17:41:02 -080098 void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
Peter Kalauskas3373cd32018-10-24 15:37:00 -070099
Steven Morelandbd603f72020-04-06 14:21:55 -0700100 private:
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700101 sp<ClientCounterCallback> mClientCallback;
102};
103
Steven Moreland40b3ab42019-01-25 13:23:20 -0800104bool ClientCounterCallback::addRegisteredService(const sp<IBase>& service,
105 const std::string& name) {
106 bool success = registerService(service, name);
107
108 if (success) {
109 mRegisteredServices.push_back({service, name});
110 }
111
112 return success;
113}
114
Steven Morelandbd603f72020-04-06 14:21:55 -0700115ClientCounterCallback::Service& ClientCounterCallback::assertRegisteredService(
116 const sp<IBase>& service) {
117 for (Service& registered : mRegisteredServices) {
118 if (registered.service != service) continue;
119 return registered;
120 }
121 LOG(FATAL) << "Got callback on service " << getDescriptor(service.get())
122 << " which we did not register.";
123 __builtin_unreachable();
124}
125
Steven Moreland40b3ab42019-01-25 13:23:20 -0800126bool ClientCounterCallback::registerService(const sp<IBase>& service, const std::string& name) {
127 auto manager = hardware::defaultServiceManager1_2();
128
129 const std::string descriptor = getDescriptor(service.get());
130
131 LOG(INFO) << "Registering HAL: " << descriptor << " with name: " << name;
132
133 status_t res = android::hardware::details::registerAsServiceInternal(service, name);
134 if (res != android::OK) {
135 LOG(ERROR) << "Failed to register as service.";
136 return false;
137 }
138
139 bool ret = manager->registerClientCallback(getDescriptor(service.get()), name, service, this);
140 if (!ret) {
141 LOG(ERROR) << "Failed to add client callback.";
142 return false;
143 }
144
145 return true;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700146}
147
148/**
149 * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple
150 * invocations could occur on different threads however.
151 */
152Return<void> ClientCounterCallback::onClients(const sp<::android::hidl::base::V1_0::IBase>& service,
153 bool clients) {
Steven Morelandbd603f72020-04-06 14:21:55 -0700154 Service& registered = assertRegisteredService(service);
155 if (registered.clients == clients) {
156 LOG(FATAL) << "Process already thought " << getDescriptor(service.get()) << "/"
157 << registered.name << " had clients: " << registered.clients
158 << " but hwservicemanager has notified has clients: " << clients;
159 }
160 registered.clients = clients;
161
162 size_t numWithClients = 0;
163 for (const Service& registered : mRegisteredServices) {
164 if (registered.clients) numWithClients++;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700165 }
Steven Moreland40b3ab42019-01-25 13:23:20 -0800166
Steven Morelandbd603f72020-04-06 14:21:55 -0700167 LOG(INFO) << "Process has " << numWithClients << " (of " << mRegisteredServices.size()
Steven Moreland06d58be2019-02-04 14:20:13 -0800168 << " available) client(s) in use after notification " << getDescriptor(service.get())
Steven Morelandbd603f72020-04-06 14:21:55 -0700169 << "/" << registered.name << " has clients: " << clients;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700170
Amos Bianchi57341bb2020-12-23 11:11:37 -0800171 bool handledInCallback = false;
Amos Bianchifb918632021-01-20 17:41:02 -0800172 if (mActiveServicesCallback != nullptr) {
173 bool hasClients = numWithClients != 0;
174 if (hasClients != mPreviousHasClients) {
175 handledInCallback = mActiveServicesCallback(hasClients);
176 mPreviousHasClients = hasClients;
177 }
Amos Bianchi57341bb2020-12-23 11:11:37 -0800178 }
179
180 // If there is no callback defined or the callback did not handle this
181 // client count change event, try to shutdown the process if its services
182 // have no clients.
183 if (!handledInCallback && numWithClients == 0) {
Steven Moreland40b3ab42019-01-25 13:23:20 -0800184 tryShutdown();
185 }
186
187 return Status::ok();
188}
189
Amos Bianchi57341bb2020-12-23 11:11:37 -0800190bool ClientCounterCallback::tryUnregister() {
Steven Moreland40b3ab42019-01-25 13:23:20 -0800191 auto manager = hardware::defaultServiceManager1_2();
192
Amos Bianchi57341bb2020-12-23 11:11:37 -0800193 for (Service& entry : mRegisteredServices) {
Steven Moreland40b3ab42019-01-25 13:23:20 -0800194 const std::string descriptor = getDescriptor(entry.service.get());
195 bool success = manager->tryUnregister(descriptor, entry.name, entry.service);
196
197 if (!success) {
Steven Moreland06d58be2019-02-04 14:20:13 -0800198 LOG(INFO) << "Failed to unregister HAL " << descriptor << "/" << entry.name;
Amos Bianchi57341bb2020-12-23 11:11:37 -0800199 return false;
Steven Moreland40b3ab42019-01-25 13:23:20 -0800200 }
Amos Bianchi57341bb2020-12-23 11:11:37 -0800201
202 // Mark the entry unregistered, but do not remove it (may still be re-registered)
203 entry.registered = false;
Steven Moreland40b3ab42019-01-25 13:23:20 -0800204 }
205
Amos Bianchi57341bb2020-12-23 11:11:37 -0800206 return true;
207}
Steven Moreland40b3ab42019-01-25 13:23:20 -0800208
Amos Bianchi57341bb2020-12-23 11:11:37 -0800209void ClientCounterCallback::reRegister() {
210 for (Service& entry : mRegisteredServices) {
211 // re-register entry if not already registered
212 if (entry.registered) {
213 continue;
214 }
Steven Moreland40b3ab42019-01-25 13:23:20 -0800215
Steven Moreland40b3ab42019-01-25 13:23:20 -0800216 if (!registerService(entry.service, entry.name)) {
217 // Must restart. Otherwise, clients will never be able to get ahold of this service.
218 LOG(FATAL) << "Bad state: could not re-register " << getDescriptor(entry.service.get());
219 }
Amos Bianchi57341bb2020-12-23 11:11:37 -0800220
221 entry.registered = true;
Steven Moreland40b3ab42019-01-25 13:23:20 -0800222 }
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700223}
224
Amos Bianchi57341bb2020-12-23 11:11:37 -0800225void ClientCounterCallback::tryShutdown() {
226 LOG(INFO) << "Trying to exit HAL. No clients in use for any service in process.";
227
228 if (tryUnregister()) {
229 LOG(INFO) << "Unregistered all clients and exiting";
230 exit(EXIT_SUCCESS);
231 }
232
233 // At this point, we failed to unregister some of the services, leaving the
234 // server in an inconsistent state. Re-register all services that were
235 // unregistered by tryUnregister().
236 reRegister();
237}
238
Amos Bianchifb918632021-01-20 17:41:02 -0800239void ClientCounterCallback::setActiveServicesCallback(
240 const std::function<bool(bool)>& activeServicesCallback) {
241 mActiveServicesCallback = activeServicesCallback;
Amos Bianchi57341bb2020-12-23 11:11:37 -0800242}
243
Peter Kalauskas7ce31552019-01-03 15:15:46 -0800244status_t LazyServiceRegistrarImpl::registerService(
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700245 const sp<::android::hidl::base::V1_0::IBase>& service, const std::string& name) {
Steven Moreland40b3ab42019-01-25 13:23:20 -0800246 if (!mClientCallback->addRegisteredService(service, name)) {
247 return ::android::UNKNOWN_ERROR;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700248 }
Steven Moreland40b3ab42019-01-25 13:23:20 -0800249
250 return ::android::OK;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700251}
252
Amos Bianchi57341bb2020-12-23 11:11:37 -0800253bool LazyServiceRegistrarImpl::tryUnregister() {
254 return mClientCallback->tryUnregister();
255}
256
257void LazyServiceRegistrarImpl::reRegister() {
258 mClientCallback->reRegister();
259}
260
Amos Bianchifb918632021-01-20 17:41:02 -0800261void LazyServiceRegistrarImpl::setActiveServicesCallback(
262 const std::function<bool(bool)>& activeServicesCallback) {
263 mClientCallback->setActiveServicesCallback(activeServicesCallback);
Amos Bianchi57341bb2020-12-23 11:11:37 -0800264}
265
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700266} // namespace details
267
268LazyServiceRegistrar::LazyServiceRegistrar() {
269 mImpl = std::make_shared<details::LazyServiceRegistrarImpl>();
270}
271
Peter Kalauskas1e969222019-08-14 12:13:11 -0700272LazyServiceRegistrar& LazyServiceRegistrar::getInstance() {
273 static auto registrarInstance = new LazyServiceRegistrar();
274 return *registrarInstance;
275}
276
Peter Kalauskas7ce31552019-01-03 15:15:46 -0800277status_t LazyServiceRegistrar::registerService(
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700278 const sp<::android::hidl::base::V1_0::IBase>& service, const std::string& name) {
Peter Kalauskas7ce31552019-01-03 15:15:46 -0800279 return mImpl->registerService(service, name);
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700280}
281
Amos Bianchi57341bb2020-12-23 11:11:37 -0800282bool LazyServiceRegistrar::tryUnregister() {
283 return mImpl->tryUnregister();
284}
285
286void LazyServiceRegistrar::reRegister() {
287 mImpl->reRegister();
288}
289
Amos Bianchifb918632021-01-20 17:41:02 -0800290void LazyServiceRegistrar::setActiveServicesCallback(
291 const std::function<bool(bool)>& activeServicesCallback) {
292 mImpl->setActiveServicesCallback(activeServicesCallback);
Amos Bianchi57341bb2020-12-23 11:11:37 -0800293}
294
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700295} // namespace hardware
296} // namespace android