2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/capability.h>
23 #include <android-base/logging.h>
24 #include <android-base/macros.h>
25 #include <binder/IPCThreadState.h>
26 #include <binder/IServiceManager.h>
27 #include <binder/ProcessState.h>
28 #include <cutils/properties.h>
29 #include <hwbinder/IPCThreadState.h>
30 #include <hwbinder/ProcessState.h>
31 #include <libminijail.h>
32 #include <utils/String16.h>
33 #include <wifi_system/interface_tool.h>
35 #include "wificond/ipc_constants.h"
36 #include "wificond/looper_backed_event_loop.h"
37 #include "wificond/net/netlink_manager.h"
38 #include "wificond/net/netlink_utils.h"
39 #include "wificond/scanning/scan_utils.h"
40 #include "wificond/server.h"
42 using android::net::wifi::IWificond;
43 using android::wifi_system::HostapdManager;
44 using android::wifi_system::InterfaceTool;
45 using android::wifi_system::SupplicantManager;
46 using android::wificond::ipc_constants::kServiceName;
47 using std::unique_ptr;
51 class ScopedSignalHandler final {
53 ScopedSignalHandler(android::wificond::LooperBackedEventLoop* event_loop) {
54 if (s_event_loop_ != nullptr) {
55 LOG(FATAL) << "Only instantiate one signal handler per process!";
57 s_event_loop_ = event_loop;
58 std::signal(SIGINT, &ScopedSignalHandler::LeaveLoop);
59 std::signal(SIGTERM, &ScopedSignalHandler::LeaveLoop);
62 ~ScopedSignalHandler() {
63 std::signal(SIGINT, SIG_DFL);
64 std::signal(SIGTERM, SIG_DFL);
65 s_event_loop_ = nullptr;
69 static android::wificond::LooperBackedEventLoop* s_event_loop_;
70 static void LeaveLoop(int signal) {
71 if (s_event_loop_ != nullptr) {
72 s_event_loop_->TriggerExit();
76 DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
79 android::wificond::LooperBackedEventLoop*
80 ScopedSignalHandler::s_event_loop_ = nullptr;
83 // Setup our interface to the Binder driver or die trying.
84 int SetupBinderOrCrash() {
86 android::ProcessState::self()->setThreadPoolMaxThreadCount(0);
87 android::IPCThreadState::self()->disableBackgroundScheduling(true);
88 int err = android::IPCThreadState::self()->setupPolling(&binder_fd);
89 CHECK_EQ(err, 0) << "Error setting up binder polling: " << strerror(-err);
90 CHECK_GE(binder_fd, 0) << "Invalid binder FD: " << binder_fd;
94 // Setup our interface to the hw Binder driver or die trying.
95 int SetupHwBinderOrCrash() {
97 android::hardware::ProcessState::self()->setThreadPoolConfiguration(1, true);
98 int err = android::hardware::IPCThreadState::self()->setupPolling(&binder_fd);
99 CHECK_EQ(err, 0) << "Error setting up hw binder polling: " << strerror(-err);
100 CHECK_GE(binder_fd, 0) << "Invalid hw binder FD: " << binder_fd;
104 void RegisterServiceOrCrash(const android::sp<android::IBinder>& service) {
105 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
106 CHECK_EQ(sm != NULL, true) << "Could not obtain IServiceManager";
108 CHECK_EQ(sm->addService(android::String16(kServiceName), service),
114 void OnBinderReadReady(int fd) {
115 android::IPCThreadState::self()->handlePolledCommands();
118 void OnHwBinderReadReady(int fd) {
119 android::hardware::IPCThreadState::self()->handlePolledCommands();
122 int main(int argc, char** argv) {
123 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
124 LOG(INFO) << "wificond is starting up...";
126 unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher(
127 new android::wificond::LooperBackedEventLoop());
128 ScopedSignalHandler scoped_signal_handler(event_dispatcher.get());
130 int binder_fd = SetupBinderOrCrash();
131 CHECK(event_dispatcher->WatchFileDescriptor(
133 android::wificond::EventLoop::kModeInput,
134 &OnBinderReadReady)) << "Failed to watch binder FD";
136 int hw_binder_fd = SetupHwBinderOrCrash();
137 CHECK(event_dispatcher->WatchFileDescriptor(
138 hw_binder_fd, android::wificond::EventLoop::kModeInput,
139 &OnHwBinderReadReady)) << "Failed to watch Hw Binder FD";
141 android::wificond::NetlinkManager netlink_manager(event_dispatcher.get());
142 if (!netlink_manager.Start()) {
143 LOG(ERROR) << "Failed to start netlink manager";
145 android::wificond::NetlinkUtils netlink_utils(&netlink_manager);
146 android::wificond::ScanUtils scan_utils(&netlink_manager);
148 unique_ptr<android::wificond::Server> server(new android::wificond::Server(
149 unique_ptr<InterfaceTool>(new InterfaceTool),
150 unique_ptr<SupplicantManager>(new SupplicantManager()),
151 unique_ptr<HostapdManager>(new HostapdManager()),
154 server->CleanUpSystemState();
155 RegisterServiceOrCrash(server.get());
157 event_dispatcher->Poll();
158 LOG(INFO) << "wificond is about to exit";