OSDN Git Service

Wificond: Remove mannual memory management in OffloadScanManager
[android-x86/system-connectivity-wificond.git] / main.cpp
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
17 #include <unistd.h>
18 #include <sys/capability.h>
19
20 #include <csignal>
21 #include <memory>
22
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>
34
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"
41
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;
48
49 namespace {
50
51 class ScopedSignalHandler final {
52  public:
53   ScopedSignalHandler(android::wificond::LooperBackedEventLoop* event_loop) {
54     if (s_event_loop_ != nullptr) {
55       LOG(FATAL) << "Only instantiate one signal handler per process!";
56     }
57     s_event_loop_ = event_loop;
58     std::signal(SIGINT, &ScopedSignalHandler::LeaveLoop);
59     std::signal(SIGTERM, &ScopedSignalHandler::LeaveLoop);
60   }
61
62   ~ScopedSignalHandler() {
63     std::signal(SIGINT, SIG_DFL);
64     std::signal(SIGTERM, SIG_DFL);
65     s_event_loop_ = nullptr;
66   }
67
68  private:
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();
73     }
74   }
75
76   DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
77 };
78
79 android::wificond::LooperBackedEventLoop*
80     ScopedSignalHandler::s_event_loop_ = nullptr;
81
82
83 // Setup our interface to the Binder driver or die trying.
84 int SetupBinderOrCrash() {
85   int binder_fd = -1;
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;
91   return binder_fd;
92 }
93
94 // Setup our interface to the hw Binder driver or die trying.
95 int SetupHwBinderOrCrash() {
96   int binder_fd = -1;
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;
101   return binder_fd;
102 }
103
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";
107
108   CHECK_EQ(sm->addService(android::String16(kServiceName), service),
109            android::NO_ERROR);
110 }
111
112 }  // namespace
113
114 void OnBinderReadReady(int fd) {
115   android::IPCThreadState::self()->handlePolledCommands();
116 }
117
118 void OnHwBinderReadReady(int fd) {
119   android::hardware::IPCThreadState::self()->handlePolledCommands();
120 }
121
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...";
125
126   unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher(
127       new android::wificond::LooperBackedEventLoop());
128   ScopedSignalHandler scoped_signal_handler(event_dispatcher.get());
129
130   int binder_fd = SetupBinderOrCrash();
131   CHECK(event_dispatcher->WatchFileDescriptor(
132       binder_fd,
133       android::wificond::EventLoop::kModeInput,
134       &OnBinderReadReady)) << "Failed to watch binder FD";
135
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";
140
141   android::wificond::NetlinkManager netlink_manager(event_dispatcher.get());
142   if (!netlink_manager.Start()) {
143     LOG(ERROR) << "Failed to start netlink manager";
144   }
145   android::wificond::NetlinkUtils netlink_utils(&netlink_manager);
146   android::wificond::ScanUtils scan_utils(&netlink_manager);
147
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()),
152       &netlink_utils,
153       &scan_utils));
154   server->CleanUpSystemState();
155   RegisterServiceOrCrash(server.get());
156
157   event_dispatcher->Poll();
158   LOG(INFO) << "wificond is about to exit";
159   return 0;
160 }