HidlLazyUtils.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  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. #include <hidl/HidlLazyUtils.h>
  17. #include <hidl/HidlTransportSupport.h>
  18. #include <android-base/logging.h>
  19. #include <android/hidl/manager/1.2/IClientCallback.h>
  20. #include <android/hidl/manager/1.2/IServiceManager.h>
  21. namespace android {
  22. namespace hardware {
  23. namespace details {
  24. using ::android::hidl::base::V1_0::IBase;
  25. class ClientCounterCallback : public ::android::hidl::manager::V1_2::IClientCallback {
  26. public:
  27. ClientCounterCallback() : mNumConnectedServices(0) {}
  28. bool addRegisteredService(const sp<IBase>& service, const std::string& name);
  29. protected:
  30. Return<void> onClients(const sp<IBase>& service, bool clients) override;
  31. private:
  32. /**
  33. * Registers or re-registers services. Returns whether successful.
  34. */
  35. bool registerService(const sp<IBase>& service, const std::string& name);
  36. /**
  37. * Unregisters all services that we can. If we can't unregister all, re-register other
  38. * services.
  39. */
  40. void tryShutdown();
  41. /**
  42. * Counter of the number of services that currently have at least one client.
  43. */
  44. size_t mNumConnectedServices;
  45. struct Service {
  46. sp<IBase> service;
  47. std::string name;
  48. };
  49. /**
  50. * Number of services that have been registered.
  51. */
  52. std::vector<Service> mRegisteredServices;
  53. };
  54. class LazyServiceRegistrarImpl {
  55. public:
  56. LazyServiceRegistrarImpl() : mClientCallback(new ClientCounterCallback) {}
  57. status_t registerService(const sp<::android::hidl::base::V1_0::IBase>& service,
  58. const std::string& name);
  59. private:
  60. sp<ClientCounterCallback> mClientCallback;
  61. };
  62. bool ClientCounterCallback::addRegisteredService(const sp<IBase>& service,
  63. const std::string& name) {
  64. bool success = registerService(service, name);
  65. if (success) {
  66. mRegisteredServices.push_back({service, name});
  67. }
  68. return success;
  69. }
  70. bool ClientCounterCallback::registerService(const sp<IBase>& service, const std::string& name) {
  71. auto manager = hardware::defaultServiceManager1_2();
  72. const std::string descriptor = getDescriptor(service.get());
  73. LOG(INFO) << "Registering HAL: " << descriptor << " with name: " << name;
  74. status_t res = android::hardware::details::registerAsServiceInternal(service, name);
  75. if (res != android::OK) {
  76. LOG(ERROR) << "Failed to register as service.";
  77. return false;
  78. }
  79. bool ret = manager->registerClientCallback(getDescriptor(service.get()), name, service, this);
  80. if (!ret) {
  81. LOG(ERROR) << "Failed to add client callback.";
  82. return false;
  83. }
  84. return true;
  85. }
  86. /**
  87. * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple
  88. * invocations could occur on different threads however.
  89. */
  90. Return<void> ClientCounterCallback::onClients(const sp<::android::hidl::base::V1_0::IBase>& service,
  91. bool clients) {
  92. if (clients) {
  93. mNumConnectedServices++;
  94. } else {
  95. mNumConnectedServices--;
  96. }
  97. LOG(INFO) << "Process has " << mNumConnectedServices << " (of " << mRegisteredServices.size()
  98. << " available) client(s) in use after notification " << getDescriptor(service.get())
  99. << " has clients: " << clients;
  100. if (mNumConnectedServices == 0) {
  101. tryShutdown();
  102. }
  103. return Status::ok();
  104. }
  105. void ClientCounterCallback::tryShutdown() {
  106. LOG(INFO) << "Trying to exit HAL. No clients in use for any service in process.";
  107. auto manager = hardware::defaultServiceManager1_2();
  108. auto unRegisterIt = mRegisteredServices.begin();
  109. for (; unRegisterIt != mRegisteredServices.end(); ++unRegisterIt) {
  110. auto& entry = (*unRegisterIt);
  111. const std::string descriptor = getDescriptor(entry.service.get());
  112. bool success = manager->tryUnregister(descriptor, entry.name, entry.service);
  113. if (!success) {
  114. LOG(INFO) << "Failed to unregister HAL " << descriptor << "/" << entry.name;
  115. break;
  116. }
  117. }
  118. if (unRegisterIt == mRegisteredServices.end()) {
  119. LOG(INFO) << "Unregistered all clients and exiting";
  120. exit(EXIT_SUCCESS);
  121. }
  122. for (auto reRegisterIt = mRegisteredServices.begin(); reRegisterIt != unRegisterIt;
  123. reRegisterIt++) {
  124. auto& entry = (*reRegisterIt);
  125. // re-register entry
  126. if (!registerService(entry.service, entry.name)) {
  127. // Must restart. Otherwise, clients will never be able to get ahold of this service.
  128. LOG(FATAL) << "Bad state: could not re-register " << getDescriptor(entry.service.get());
  129. }
  130. }
  131. }
  132. status_t LazyServiceRegistrarImpl::registerService(
  133. const sp<::android::hidl::base::V1_0::IBase>& service, const std::string& name) {
  134. if (!mClientCallback->addRegisteredService(service, name)) {
  135. return ::android::UNKNOWN_ERROR;
  136. }
  137. return ::android::OK;
  138. }
  139. } // namespace details
  140. LazyServiceRegistrar::LazyServiceRegistrar() {
  141. mImpl = std::make_shared<details::LazyServiceRegistrarImpl>();
  142. }
  143. status_t LazyServiceRegistrar::registerService(
  144. const sp<::android::hidl::base::V1_0::IBase>& service, const std::string& name) {
  145. return mImpl->registerService(service, name);
  146. }
  147. } // namespace hardware
  148. } // namespace android