main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include <unistd.h>
  17. #include <sys/capability.h>
  18. #include <csignal>
  19. #include <memory>
  20. #include <android-base/logging.h>
  21. #include <android-base/macros.h>
  22. #include <binder/IPCThreadState.h>
  23. #include <binder/IServiceManager.h>
  24. #include <binder/ProcessState.h>
  25. #include <cutils/properties.h>
  26. #include <hidl/HidlTransportSupport.h>
  27. #include <libminijail.h>
  28. #include <utils/String16.h>
  29. #include <wifi_system/interface_tool.h>
  30. #include "wificond/ipc_constants.h"
  31. #include "wificond/looper_backed_event_loop.h"
  32. #include "wificond/net/netlink_manager.h"
  33. #include "wificond/net/netlink_utils.h"
  34. #include "wificond/scanning/scan_utils.h"
  35. #include "wificond/server.h"
  36. using android::net::wifi::IWificond;
  37. using android::wifi_system::InterfaceTool;
  38. using android::wificond::ipc_constants::kServiceName;
  39. using std::unique_ptr;
  40. namespace {
  41. class ScopedSignalHandler final {
  42. public:
  43. ScopedSignalHandler(android::wificond::LooperBackedEventLoop* event_loop) {
  44. if (s_event_loop_ != nullptr) {
  45. LOG(FATAL) << "Only instantiate one signal handler per process!";
  46. }
  47. s_event_loop_ = event_loop;
  48. std::signal(SIGINT, &ScopedSignalHandler::LeaveLoop);
  49. std::signal(SIGTERM, &ScopedSignalHandler::LeaveLoop);
  50. }
  51. ~ScopedSignalHandler() {
  52. std::signal(SIGINT, SIG_DFL);
  53. std::signal(SIGTERM, SIG_DFL);
  54. s_event_loop_ = nullptr;
  55. }
  56. private:
  57. static android::wificond::LooperBackedEventLoop* s_event_loop_;
  58. static void LeaveLoop(int signal) {
  59. if (s_event_loop_ != nullptr) {
  60. s_event_loop_->TriggerExit();
  61. }
  62. }
  63. DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
  64. };
  65. android::wificond::LooperBackedEventLoop*
  66. ScopedSignalHandler::s_event_loop_ = nullptr;
  67. // Setup our interface to the Binder driver or die trying.
  68. int SetupBinderOrCrash() {
  69. int binder_fd = -1;
  70. android::ProcessState::self()->setThreadPoolMaxThreadCount(0);
  71. android::IPCThreadState::self()->disableBackgroundScheduling(true);
  72. int err = android::IPCThreadState::self()->setupPolling(&binder_fd);
  73. CHECK_EQ(err, 0) << "Error setting up binder polling: " << strerror(-err);
  74. CHECK_GE(binder_fd, 0) << "Invalid binder FD: " << binder_fd;
  75. return binder_fd;
  76. }
  77. // Setup our interface to the hw Binder driver or die trying.
  78. int SetupHwBinderOrCrash() {
  79. android::hardware::configureRpcThreadpool(1, true /* callerWillJoin */);
  80. int binder_fd = android::hardware::setupTransportPolling();
  81. CHECK_GE(binder_fd, 0) << "Invalid hw binder FD: " << binder_fd;
  82. return binder_fd;
  83. }
  84. void RegisterServiceOrCrash(const android::sp<android::IBinder>& service) {
  85. android::sp<android::IServiceManager> sm = android::defaultServiceManager();
  86. CHECK_EQ(sm != NULL, true) << "Could not obtain IServiceManager";
  87. CHECK_EQ(sm->addService(android::String16(kServiceName), service),
  88. android::NO_ERROR);
  89. }
  90. } // namespace
  91. void OnBinderReadReady(int fd) {
  92. android::IPCThreadState::self()->handlePolledCommands();
  93. }
  94. void OnHwBinderReadReady(int fd) {
  95. android::hardware::handleTransportPoll(fd);
  96. }
  97. int main(int argc, char** argv) {
  98. android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
  99. LOG(INFO) << "wificond is starting up...";
  100. unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher(
  101. new android::wificond::LooperBackedEventLoop());
  102. ScopedSignalHandler scoped_signal_handler(event_dispatcher.get());
  103. int binder_fd = SetupBinderOrCrash();
  104. CHECK(event_dispatcher->WatchFileDescriptor(
  105. binder_fd,
  106. android::wificond::EventLoop::kModeInput,
  107. &OnBinderReadReady)) << "Failed to watch binder FD";
  108. int hw_binder_fd = SetupHwBinderOrCrash();
  109. CHECK(event_dispatcher->WatchFileDescriptor(
  110. hw_binder_fd, android::wificond::EventLoop::kModeInput,
  111. &OnHwBinderReadReady)) << "Failed to watch Hw Binder FD";
  112. android::wificond::NetlinkManager netlink_manager(event_dispatcher.get());
  113. if (!netlink_manager.Start()) {
  114. LOG(ERROR) << "Failed to start netlink manager";
  115. }
  116. android::wificond::NetlinkUtils netlink_utils(&netlink_manager);
  117. android::wificond::ScanUtils scan_utils(&netlink_manager);
  118. unique_ptr<android::wificond::Server> server(new android::wificond::Server(
  119. unique_ptr<InterfaceTool>(new InterfaceTool),
  120. &netlink_utils,
  121. &scan_utils));
  122. RegisterServiceOrCrash(server.get());
  123. event_dispatcher->Poll();
  124. LOG(INFO) << "wificond is about to exit";
  125. return 0;
  126. }