service.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2016 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. #define LOG_TAG "hwservicemanager"
  17. #include <utils/Log.h>
  18. #include <inttypes.h>
  19. #include <unistd.h>
  20. #include <sys/timerfd.h>
  21. #include <android/hidl/token/1.0/ITokenManager.h>
  22. #include <cutils/properties.h>
  23. #include <hidl/HidlBinderSupport.h>
  24. #include <hidl/HidlTransportSupport.h>
  25. #include <hidl/Status.h>
  26. #include <hwbinder/IPCThreadState.h>
  27. #include <hwbinder/ProcessState.h>
  28. #include <utils/Errors.h>
  29. #include <utils/Looper.h>
  30. #include <utils/StrongPointer.h>
  31. #include "ServiceManager.h"
  32. #include "TokenManager.h"
  33. // libutils:
  34. using android::sp;
  35. using android::Looper;
  36. using android::LooperCallback;
  37. // libhwbinder:
  38. using android::hardware::BHwBinder;
  39. using android::hardware::IBinder;
  40. using android::hardware::IPCThreadState;
  41. using android::hardware::ProcessState;
  42. // libhidl
  43. using android::hardware::handleTransportPoll;
  44. using android::hardware::setRequestingSid;
  45. using android::hardware::HidlReturnRestriction;
  46. using android::hardware::setProcessHidlReturnRestriction;
  47. using android::hardware::setupTransportPolling;
  48. using android::hardware::toBinder;
  49. // implementations
  50. using android::hidl::manager::implementation::ServiceManager;
  51. using android::hidl::manager::V1_0::IServiceManager;
  52. using android::hidl::token::V1_0::implementation::TokenManager;
  53. static std::string serviceName = "default";
  54. class HwBinderCallback : public LooperCallback {
  55. public:
  56. static sp<HwBinderCallback> setupTo(const sp<Looper>& looper) {
  57. sp<HwBinderCallback> cb = new HwBinderCallback;
  58. int fdHwBinder = setupTransportPolling();
  59. LOG_ALWAYS_FATAL_IF(fdHwBinder < 0, "Failed to setupTransportPolling: %d", fdHwBinder);
  60. // Flush after setupPolling(), to make sure the binder driver
  61. // knows about this thread handling commands.
  62. IPCThreadState::self()->flushCommands();
  63. int ret = looper->addFd(fdHwBinder,
  64. Looper::POLL_CALLBACK,
  65. Looper::EVENT_INPUT,
  66. cb,
  67. nullptr /*data*/);
  68. LOG_ALWAYS_FATAL_IF(ret != 1, "Failed to add binder FD to Looper");
  69. return cb;
  70. }
  71. int handleEvent(int fd, int /*events*/, void* /*data*/) override {
  72. handleTransportPoll(fd);
  73. return 1; // Continue receiving callbacks.
  74. }
  75. };
  76. // LooperCallback for IClientCallback
  77. class ClientCallbackCallback : public LooperCallback {
  78. public:
  79. static sp<ClientCallbackCallback> setupTo(const sp<Looper>& looper, const sp<ServiceManager>& manager) {
  80. sp<ClientCallbackCallback> cb = new ClientCallbackCallback(manager);
  81. int fdTimer = timerfd_create(CLOCK_MONOTONIC, 0 /*flags*/);
  82. LOG_ALWAYS_FATAL_IF(fdTimer < 0, "Failed to timerfd_create: fd: %d err: %d", fdTimer, errno);
  83. itimerspec timespec {
  84. .it_interval = {
  85. .tv_sec = 5,
  86. .tv_nsec = 0,
  87. },
  88. .it_value = {
  89. .tv_sec = 5,
  90. .tv_nsec = 0,
  91. },
  92. };
  93. int timeRes = timerfd_settime(fdTimer, 0 /*flags*/, &timespec, nullptr);
  94. LOG_ALWAYS_FATAL_IF(timeRes < 0, "Failed to timerfd_settime: res: %d err: %d", timeRes, errno);
  95. int addRes = looper->addFd(fdTimer,
  96. Looper::POLL_CALLBACK,
  97. Looper::EVENT_INPUT,
  98. cb,
  99. nullptr);
  100. LOG_ALWAYS_FATAL_IF(addRes != 1, "Failed to add client callback FD to Looper");
  101. return cb;
  102. }
  103. int handleEvent(int fd, int /*events*/, void* /*data*/) override {
  104. uint64_t expirations;
  105. int ret = read(fd, &expirations, sizeof(expirations));
  106. if (ret != sizeof(expirations)) {
  107. ALOGE("Read failed to callback FD: ret: %d err: %d", ret, errno);
  108. }
  109. mManager->handleClientCallbacks();
  110. return 1; // Continue receiving callbacks.
  111. }
  112. private:
  113. ClientCallbackCallback(const sp<ServiceManager>& manager) : mManager(manager) {}
  114. sp<ServiceManager> mManager;
  115. };
  116. int main() {
  117. // If hwservicemanager crashes, the system may be unstable and hard to debug. This is both why
  118. // we log this and why we care about this at all.
  119. setProcessHidlReturnRestriction(HidlReturnRestriction::ERROR_IF_UNCHECKED);
  120. sp<ServiceManager> manager = new ServiceManager();
  121. setRequestingSid(manager, true);
  122. if (!manager->add(serviceName, manager).withDefault(false)) {
  123. ALOGE("Failed to register hwservicemanager with itself.");
  124. }
  125. sp<TokenManager> tokenManager = new TokenManager();
  126. if (!manager->add(serviceName, tokenManager).withDefault(false)) {
  127. ALOGE("Failed to register ITokenManager with hwservicemanager.");
  128. }
  129. // Tell IPCThreadState we're the service manager
  130. sp<IBinder> binder = toBinder<IServiceManager>(manager);
  131. sp<BHwBinder> service = static_cast<BHwBinder*>(binder.get()); // local binder object
  132. IPCThreadState::self()->setTheContextObject(service);
  133. // Then tell the kernel
  134. ProcessState::self()->becomeContextManager(nullptr, nullptr);
  135. int rc = property_set("hwservicemanager.ready", "true");
  136. if (rc) {
  137. ALOGE("Failed to set \"hwservicemanager.ready\" (error %d). "\
  138. "HAL services will not start!\n", rc);
  139. }
  140. sp<Looper> looper = Looper::prepare(0 /* opts */);
  141. (void)HwBinderCallback::setupTo(looper);
  142. (void)ClientCallbackCallback::setupTo(looper, manager);
  143. ALOGI("hwservicemanager is ready now.");
  144. while (true) {
  145. looper->pollAll(-1 /* timeoutMillis */);
  146. }
  147. return 0;
  148. }