OSDN Git Service

Make failure to take ownership of firmware path fatal
[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 <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>
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_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::kDevModePropertyKey;
49 using android::wificond::ipc_constants::kDevModeServiceName;
50 using android::wificond::ipc_constants::kServiceName;
51 using std::unique_ptr;
52
53 namespace {
54
55 class ScopedSignalHandler final {
56  public:
57   ScopedSignalHandler(android::wificond::LooperBackedEventLoop* event_loop) {
58     if (s_event_loop_ != nullptr) {
59       LOG(FATAL) << "Only instantiate one signal handler per process!";
60     }
61     s_event_loop_ = event_loop;
62     std::signal(SIGINT, &ScopedSignalHandler::LeaveLoop);
63     std::signal(SIGTERM, &ScopedSignalHandler::LeaveLoop);
64   }
65
66   ~ScopedSignalHandler() {
67     std::signal(SIGINT, SIG_DFL);
68     std::signal(SIGTERM, SIG_DFL);
69     s_event_loop_ = nullptr;
70   }
71
72  private:
73   static android::wificond::LooperBackedEventLoop* s_event_loop_;
74   static void LeaveLoop(int signal) {
75     if (s_event_loop_ != nullptr) {
76       s_event_loop_->TriggerExit();
77     }
78   }
79
80   DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
81 };
82
83 android::wificond::LooperBackedEventLoop*
84     ScopedSignalHandler::s_event_loop_ = nullptr;
85
86
87 // Setup our interface to the Binder driver or die trying.
88 int SetupBinderOrCrash() {
89   int binder_fd = -1;
90   android::ProcessState::self()->setThreadPoolMaxThreadCount(0);
91   android::IPCThreadState::self()->disableBackgroundScheduling(true);
92   int err = android::IPCThreadState::self()->setupPolling(&binder_fd);
93   CHECK_EQ(err, 0) << "Error setting up binder polling: " << strerror(-err);
94   CHECK_GE(binder_fd, 0) << "Invalid binder FD: " << binder_fd;
95   return binder_fd;
96 }
97
98 void RegisterServiceOrCrash(const android::sp<android::IBinder>& service) {
99   android::sp<android::IServiceManager> sm = android::defaultServiceManager();
100   CHECK_EQ(sm != NULL, true) << "Could not obtain IServiceManager";
101
102   const int8_t dev_mode_on = property_get_bool(kDevModePropertyKey, 0);
103   const char* service_name = (dev_mode_on) ? kDevModeServiceName : kServiceName;
104   CHECK_EQ(sm->addService(android::String16(service_name), service),
105            android::NO_ERROR);
106 }
107
108 void DoPrivilegedSetupOrCrash() {
109   CHECK(DriverTool::TakeOwnershipOfFirmwareReload());
110 }
111
112 void DropPrivilegesOrCrash() {
113   minijail* j = minijail_new();
114   CHECK(minijail_change_user(j, "wifi") == 0);
115   CHECK(minijail_change_group(j, "wifi") == 0);
116   minijail_use_caps(j,
117                     CAP_TO_MASK(CAP_NET_ADMIN) |
118                     CAP_TO_MASK(CAP_NET_RAW));
119   minijail_enter(j);
120   minijail_destroy(j);
121 }
122
123 }  // namespace
124
125 void OnBinderReadReady(int fd) {
126   android::IPCThreadState::self()->handlePolledCommands();
127 }
128
129 int main(int argc, char** argv) {
130   android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
131   LOG(INFO) << "wificond is starting up...";
132
133   DoPrivilegedSetupOrCrash();
134   DropPrivilegesOrCrash();
135
136   unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher(
137       new android::wificond::LooperBackedEventLoop());
138   ScopedSignalHandler scoped_signal_handler(event_dispatcher.get());
139
140   int binder_fd = SetupBinderOrCrash();
141   CHECK(event_dispatcher->WatchFileDescriptor(
142       binder_fd,
143       android::wificond::EventLoop::kModeInput,
144       &OnBinderReadReady)) << "Failed to watch binder FD";
145
146   android::wificond::NetlinkManager netlink_manager(event_dispatcher.get());
147   CHECK(netlink_manager.Start()) << "Failed to start netlink manager";
148   android::wificond::NetlinkUtils netlink_utils(&netlink_manager);
149   android::wificond::ScanUtils scan_utils(&netlink_manager);
150
151   unique_ptr<android::wificond::Server> server(new android::wificond::Server(
152       unique_ptr<HalTool>(new HalTool),
153       unique_ptr<InterfaceTool>(new InterfaceTool),
154       unique_ptr<DriverTool>(new DriverTool),
155       unique_ptr<SupplicantManager>(new SupplicantManager()),
156       unique_ptr<HostapdManager>(new HostapdManager()),
157       &netlink_utils,
158       &scan_utils));
159   server->CleanUpSystemState();
160   RegisterServiceOrCrash(server.get());
161
162   event_dispatcher->Poll();
163   LOG(INFO) << "wificond is about to exit";
164   return 0;
165 }