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 <libminijail.h>
30 #include <utils/String16.h>
31 #include <wifi_hal/driver_tool.h>
32 #include <wifi_system/hal_tool.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_hal::DriverTool;
44 using android::wifi_system::HalTool;
45 using android::wifi_system::HostapdManager;
46 using android::wifi_system::InterfaceTool;
47 using android::wifi_system::SupplicantManager;
48 using android::wificond::ipc_constants::kServiceName;
49 using std::unique_ptr;
53 class ScopedSignalHandler final {
55 ScopedSignalHandler(android::wificond::LooperBackedEventLoop* event_loop) {
56 if (s_event_loop_ != nullptr) {
57 LOG(FATAL) << "Only instantiate one signal handler per process!";
59 s_event_loop_ = event_loop;
60 std::signal(SIGINT, &ScopedSignalHandler::LeaveLoop);
61 std::signal(SIGTERM, &ScopedSignalHandler::LeaveLoop);
64 ~ScopedSignalHandler() {
65 std::signal(SIGINT, SIG_DFL);
66 std::signal(SIGTERM, SIG_DFL);
67 s_event_loop_ = nullptr;
71 static android::wificond::LooperBackedEventLoop* s_event_loop_;
72 static void LeaveLoop(int signal) {
73 if (s_event_loop_ != nullptr) {
74 s_event_loop_->TriggerExit();
78 DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
81 android::wificond::LooperBackedEventLoop*
82 ScopedSignalHandler::s_event_loop_ = nullptr;
85 // Setup our interface to the Binder driver or die trying.
86 int SetupBinderOrCrash() {
88 android::ProcessState::self()->setThreadPoolMaxThreadCount(0);
89 android::IPCThreadState::self()->disableBackgroundScheduling(true);
90 int err = android::IPCThreadState::self()->setupPolling(&binder_fd);
91 CHECK_EQ(err, 0) << "Error setting up binder polling: " << strerror(-err);
92 CHECK_GE(binder_fd, 0) << "Invalid binder FD: " << binder_fd;
96 void RegisterServiceOrCrash(const android::sp<android::IBinder>& service) {
97 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
98 CHECK_EQ(sm != NULL, true) << "Could not obtain IServiceManager";
100 CHECK_EQ(sm->addService(android::String16(kServiceName), service),
104 void DoPrivilegedSetupOrCrash() {
105 CHECK(DriverTool::TakeOwnershipOfFirmwareReload());
108 void DropPrivilegesOrCrash() {
109 minijail* j = minijail_new();
110 CHECK(minijail_change_user(j, "wifi") == 0);
111 CHECK(minijail_change_group(j, "wifi") == 0);
113 CAP_TO_MASK(CAP_NET_ADMIN) |
114 CAP_TO_MASK(CAP_NET_RAW));
121 void OnBinderReadReady(int fd) {
122 android::IPCThreadState::self()->handlePolledCommands();
125 int main(int argc, char** argv) {
126 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
127 LOG(INFO) << "wificond is starting up...";
129 DoPrivilegedSetupOrCrash();
130 DropPrivilegesOrCrash();
132 unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher(
133 new android::wificond::LooperBackedEventLoop());
134 ScopedSignalHandler scoped_signal_handler(event_dispatcher.get());
136 int binder_fd = SetupBinderOrCrash();
137 CHECK(event_dispatcher->WatchFileDescriptor(
139 android::wificond::EventLoop::kModeInput,
140 &OnBinderReadReady)) << "Failed to watch binder FD";
142 android::wificond::NetlinkManager netlink_manager(event_dispatcher.get());
143 CHECK(netlink_manager.Start()) << "Failed to start netlink manager";
144 android::wificond::NetlinkUtils netlink_utils(&netlink_manager);
145 android::wificond::ScanUtils scan_utils(&netlink_manager);
147 unique_ptr<android::wificond::Server> server(new android::wificond::Server(
148 unique_ptr<HalTool>(new HalTool),
149 unique_ptr<InterfaceTool>(new InterfaceTool),
150 unique_ptr<DriverTool>(new DriverTool),
151 unique_ptr<SupplicantManager>(new SupplicantManager()),
152 unique_ptr<HostapdManager>(new HostapdManager()),
155 server->CleanUpSystemState();
156 RegisterServiceOrCrash(server.get());
158 event_dispatcher->Poll();
159 LOG(INFO) << "wificond is about to exit";