blob: c5f8c74f7d4448a7afb7f67702df61e3787efba2 [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
Steven Moreland3d5b1782021-06-22 18:40:27 +000039 bool tryUnregisterLocked();
Amos Bianchi57341bb2020-12-23 11:11:37 -080040
Steven Moreland3d5b1782021-06-22 18:40:27 +000041 void reRegisterLocked();
Amos Bianchi57341bb2020-12-23 11:11:37 -080042
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 */
Steven Moreland3d5b1782021-06-22 18:40:27 +000061 Service& assertRegisteredServiceLocked(const sp<IBase>& service);
Steven Morelandbd603f72020-04-06 14:21:55 -070062
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 */
Steven Moreland3d5b1782021-06-22 18:40:27 +000066 bool registerServiceLocked(const sp<IBase>& service, const std::string& name);
Steven Moreland40b3ab42019-01-25 13:23:20 -080067
68 /**
69 * Unregisters all services that we can. If we can't unregister all, re-register other
70 * services.
71 */
Steven Moreland3d5b1782021-06-22 18:40:27 +000072 void tryShutdownLocked();
73
74 /**
75 * For below.
76 */
77 std::mutex mMutex;
Steven Moreland40b3ab42019-01-25 13:23:20 -080078
79 /**
Peter Kalauskas3373cd32018-10-24 15:37:00 -070080 * Number of services that have been registered.
81 */
Steven Moreland40b3ab42019-01-25 13:23:20 -080082 std::vector<Service> mRegisteredServices;
Amos Bianchi57341bb2020-12-23 11:11:37 -080083
84 /**
85 * Callback for reporting the number of services with clients.
86 */
Amos Bianchifb918632021-01-20 17:41:02 -080087 std::function<bool(bool)> mActiveServicesCallback;
88
89 /**
90 * Previous value passed to the active services callback.
91 */
92 std::optional<bool> mPreviousHasClients;
Peter Kalauskas3373cd32018-10-24 15:37:00 -070093};
94
95class LazyServiceRegistrarImpl {
Steven Morelandbd603f72020-04-06 14:21:55 -070096 public:
Peter Kalauskas3373cd32018-10-24 15:37:00 -070097 LazyServiceRegistrarImpl() : mClientCallback(new ClientCounterCallback) {}
98
Peter Kalauskas7ce31552019-01-03 15:15:46 -080099 status_t registerService(const sp<::android::hidl::base::V1_0::IBase>& service,
100 const std::string& name);
Amos Bianchi57341bb2020-12-23 11:11:37 -0800101 bool tryUnregister();
102 void reRegister();
Amos Bianchifb918632021-01-20 17:41:02 -0800103 void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700104
Steven Morelandbd603f72020-04-06 14:21:55 -0700105 private:
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700106 sp<ClientCounterCallback> mClientCallback;
107};
108
Steven Moreland40b3ab42019-01-25 13:23:20 -0800109bool ClientCounterCallback::addRegisteredService(const sp<IBase>& service,
110 const std::string& name) {
Steven Moreland3d5b1782021-06-22 18:40:27 +0000111 std::lock_guard<std::mutex> lock(mMutex);
112 bool success = registerServiceLocked(service, name);
Steven Moreland40b3ab42019-01-25 13:23:20 -0800113
114 if (success) {
115 mRegisteredServices.push_back({service, name});
116 }
117
118 return success;
119}
120
Steven Moreland3d5b1782021-06-22 18:40:27 +0000121ClientCounterCallback::Service& ClientCounterCallback::assertRegisteredServiceLocked(
Steven Morelandbd603f72020-04-06 14:21:55 -0700122 const sp<IBase>& service) {
123 for (Service& registered : mRegisteredServices) {
124 if (registered.service != service) continue;
125 return registered;
126 }
127 LOG(FATAL) << "Got callback on service " << getDescriptor(service.get())
128 << " which we did not register.";
129 __builtin_unreachable();
130}
131
Steven Moreland3d5b1782021-06-22 18:40:27 +0000132bool ClientCounterCallback::registerServiceLocked(const sp<IBase>& service,
133 const std::string& name) {
Steven Moreland40b3ab42019-01-25 13:23:20 -0800134 auto manager = hardware::defaultServiceManager1_2();
135
136 const std::string descriptor = getDescriptor(service.get());
137
138 LOG(INFO) << "Registering HAL: " << descriptor << " with name: " << name;
139
140 status_t res = android::hardware::details::registerAsServiceInternal(service, name);
141 if (res != android::OK) {
142 LOG(ERROR) << "Failed to register as service.";
143 return false;
144 }
145
146 bool ret = manager->registerClientCallback(getDescriptor(service.get()), name, service, this);
147 if (!ret) {
148 LOG(ERROR) << "Failed to add client callback.";
149 return false;
150 }
151
152 return true;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700153}
154
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700155Return<void> ClientCounterCallback::onClients(const sp<::android::hidl::base::V1_0::IBase>& service,
156 bool clients) {
Steven Moreland3d5b1782021-06-22 18:40:27 +0000157 std::lock_guard<std::mutex> lock(mMutex);
158 Service& registered = assertRegisteredServiceLocked(service);
Steven Morelandbd603f72020-04-06 14:21:55 -0700159 if (registered.clients == clients) {
160 LOG(FATAL) << "Process already thought " << getDescriptor(service.get()) << "/"
161 << registered.name << " had clients: " << registered.clients
162 << " but hwservicemanager has notified has clients: " << clients;
163 }
164 registered.clients = clients;
165
166 size_t numWithClients = 0;
167 for (const Service& registered : mRegisteredServices) {
168 if (registered.clients) numWithClients++;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700169 }
Steven Moreland40b3ab42019-01-25 13:23:20 -0800170
Steven Morelandbd603f72020-04-06 14:21:55 -0700171 LOG(INFO) << "Process has " << numWithClients << " (of " << mRegisteredServices.size()
Steven Moreland06d58be2019-02-04 14:20:13 -0800172 << " available) client(s) in use after notification " << getDescriptor(service.get())
Steven Morelandbd603f72020-04-06 14:21:55 -0700173 << "/" << registered.name << " has clients: " << clients;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700174
Amos Bianchi57341bb2020-12-23 11:11:37 -0800175 bool handledInCallback = false;
Amos Bianchifb918632021-01-20 17:41:02 -0800176 if (mActiveServicesCallback != nullptr) {
177 bool hasClients = numWithClients != 0;
178 if (hasClients != mPreviousHasClients) {
179 handledInCallback = mActiveServicesCallback(hasClients);
180 mPreviousHasClients = hasClients;
181 }
Amos Bianchi57341bb2020-12-23 11:11:37 -0800182 }
183
184 // If there is no callback defined or the callback did not handle this
185 // client count change event, try to shutdown the process if its services
186 // have no clients.
187 if (!handledInCallback && numWithClients == 0) {
Steven Moreland3d5b1782021-06-22 18:40:27 +0000188 tryShutdownLocked();
Steven Moreland40b3ab42019-01-25 13:23:20 -0800189 }
190
191 return Status::ok();
192}
193
Steven Moreland3d5b1782021-06-22 18:40:27 +0000194bool ClientCounterCallback::tryUnregisterLocked() {
Steven Moreland40b3ab42019-01-25 13:23:20 -0800195 auto manager = hardware::defaultServiceManager1_2();
196
Amos Bianchi57341bb2020-12-23 11:11:37 -0800197 for (Service& entry : mRegisteredServices) {
Steven Moreland40b3ab42019-01-25 13:23:20 -0800198 const std::string descriptor = getDescriptor(entry.service.get());
199 bool success = manager->tryUnregister(descriptor, entry.name, entry.service);
200
201 if (!success) {
Steven Moreland06d58be2019-02-04 14:20:13 -0800202 LOG(INFO) << "Failed to unregister HAL " << descriptor << "/" << entry.name;
Amos Bianchi57341bb2020-12-23 11:11:37 -0800203 return false;
Steven Moreland40b3ab42019-01-25 13:23:20 -0800204 }
Amos Bianchi57341bb2020-12-23 11:11:37 -0800205
206 // Mark the entry unregistered, but do not remove it (may still be re-registered)
207 entry.registered = false;
Steven Moreland40b3ab42019-01-25 13:23:20 -0800208 }
209
Amos Bianchi57341bb2020-12-23 11:11:37 -0800210 return true;
211}
Steven Moreland40b3ab42019-01-25 13:23:20 -0800212
Steven Moreland3d5b1782021-06-22 18:40:27 +0000213void ClientCounterCallback::reRegisterLocked() {
Amos Bianchi57341bb2020-12-23 11:11:37 -0800214 for (Service& entry : mRegisteredServices) {
215 // re-register entry if not already registered
216 if (entry.registered) {
217 continue;
218 }
Steven Moreland40b3ab42019-01-25 13:23:20 -0800219
Steven Moreland3d5b1782021-06-22 18:40:27 +0000220 if (!registerServiceLocked(entry.service, entry.name)) {
Steven Moreland40b3ab42019-01-25 13:23:20 -0800221 // Must restart. Otherwise, clients will never be able to get ahold of this service.
222 LOG(FATAL) << "Bad state: could not re-register " << getDescriptor(entry.service.get());
223 }
Amos Bianchi57341bb2020-12-23 11:11:37 -0800224
225 entry.registered = true;
Steven Moreland40b3ab42019-01-25 13:23:20 -0800226 }
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700227}
228
Steven Moreland3d5b1782021-06-22 18:40:27 +0000229void ClientCounterCallback::tryShutdownLocked() {
Amos Bianchi57341bb2020-12-23 11:11:37 -0800230 LOG(INFO) << "Trying to exit HAL. No clients in use for any service in process.";
231
Steven Moreland3d5b1782021-06-22 18:40:27 +0000232 if (tryUnregisterLocked()) {
Amos Bianchi57341bb2020-12-23 11:11:37 -0800233 LOG(INFO) << "Unregistered all clients and exiting";
234 exit(EXIT_SUCCESS);
235 }
236
237 // At this point, we failed to unregister some of the services, leaving the
238 // server in an inconsistent state. Re-register all services that were
Steven Moreland3d5b1782021-06-22 18:40:27 +0000239 // unregistered by tryUnregisterLocked().
240 reRegisterLocked();
Amos Bianchi57341bb2020-12-23 11:11:37 -0800241}
242
Amos Bianchifb918632021-01-20 17:41:02 -0800243void ClientCounterCallback::setActiveServicesCallback(
244 const std::function<bool(bool)>& activeServicesCallback) {
Steven Moreland3d5b1782021-06-22 18:40:27 +0000245 std::lock_guard<std::mutex> lock(mMutex);
246
Amos Bianchifb918632021-01-20 17:41:02 -0800247 mActiveServicesCallback = activeServicesCallback;
Amos Bianchi57341bb2020-12-23 11:11:37 -0800248}
249
Peter Kalauskas7ce31552019-01-03 15:15:46 -0800250status_t LazyServiceRegistrarImpl::registerService(
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700251 const sp<::android::hidl::base::V1_0::IBase>& service, const std::string& name) {
Steven Moreland40b3ab42019-01-25 13:23:20 -0800252 if (!mClientCallback->addRegisteredService(service, name)) {
253 return ::android::UNKNOWN_ERROR;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700254 }
Steven Moreland40b3ab42019-01-25 13:23:20 -0800255
256 return ::android::OK;
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700257}
258
Amos Bianchi57341bb2020-12-23 11:11:37 -0800259bool LazyServiceRegistrarImpl::tryUnregister() {
Steven Moreland3d5b1782021-06-22 18:40:27 +0000260 // see comments in header, this should only be called from the active
261 // services callback, see also b/191781736
262 return mClientCallback->tryUnregisterLocked();
Amos Bianchi57341bb2020-12-23 11:11:37 -0800263}
264
265void LazyServiceRegistrarImpl::reRegister() {
Steven Moreland3d5b1782021-06-22 18:40:27 +0000266 // see comments in header, this should only be called from the active
267 // services callback, see also b/191781736
268 mClientCallback->reRegisterLocked();
Amos Bianchi57341bb2020-12-23 11:11:37 -0800269}
270
Amos Bianchifb918632021-01-20 17:41:02 -0800271void LazyServiceRegistrarImpl::setActiveServicesCallback(
272 const std::function<bool(bool)>& activeServicesCallback) {
273 mClientCallback->setActiveServicesCallback(activeServicesCallback);
Amos Bianchi57341bb2020-12-23 11:11:37 -0800274}
275
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700276} // namespace details
277
278LazyServiceRegistrar::LazyServiceRegistrar() {
279 mImpl = std::make_shared<details::LazyServiceRegistrarImpl>();
280}
281
Peter Kalauskas1e969222019-08-14 12:13:11 -0700282LazyServiceRegistrar& LazyServiceRegistrar::getInstance() {
283 static auto registrarInstance = new LazyServiceRegistrar();
284 return *registrarInstance;
285}
286
Peter Kalauskas7ce31552019-01-03 15:15:46 -0800287status_t LazyServiceRegistrar::registerService(
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700288 const sp<::android::hidl::base::V1_0::IBase>& service, const std::string& name) {
Peter Kalauskas7ce31552019-01-03 15:15:46 -0800289 return mImpl->registerService(service, name);
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700290}
291
Amos Bianchi57341bb2020-12-23 11:11:37 -0800292bool LazyServiceRegistrar::tryUnregister() {
293 return mImpl->tryUnregister();
294}
295
296void LazyServiceRegistrar::reRegister() {
297 mImpl->reRegister();
298}
299
Amos Bianchifb918632021-01-20 17:41:02 -0800300void LazyServiceRegistrar::setActiveServicesCallback(
301 const std::function<bool(bool)>& activeServicesCallback) {
302 mImpl->setActiveServicesCallback(activeServicesCallback);
Amos Bianchi57341bb2020-12-23 11:11:37 -0800303}
304
Peter Kalauskas3373cd32018-10-24 15:37:00 -0700305} // namespace hardware
306} // namespace android