OSDN Git Service

Merge "Initial commit for nl80211 attribute"
[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
19 #include <csignal>
20 #include <memory>
21
22 #include <android-base/logging.h>
23 #include <android-base/macros.h>
24 #include <binder/IPCThreadState.h>
25 #include <binder/IServiceManager.h>
26 #include <binder/ProcessState.h>
27 #include <cutils/properties.h>
28 #include <utils/String16.h>
29
30 #include "ipc_constants.h"
31 #include "looper_backed_event_loop.h"
32 #include "server.h"
33
34 using android::net::wifi::IWificond;
35 using android::wificond::ipc_constants::kDevModePropertyKey;
36 using android::wificond::ipc_constants::kDevModeServiceName;
37 using android::wificond::ipc_constants::kServiceName;
38
39 namespace {
40
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
52   ~ScopedSignalHandler() {
53     std::signal(SIGINT, SIG_DFL);
54     std::signal(SIGTERM, SIG_DFL);
55     s_event_loop_ = nullptr;
56   }
57
58  private:
59   static android::wificond::LooperBackedEventLoop* s_event_loop_;
60   static void LeaveLoop(int signal) {
61     if (s_event_loop_ != nullptr) {
62       s_event_loop_->TriggerExit();
63     }
64   }
65
66   DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
67 };
68
69 android::wificond::LooperBackedEventLoop*
70     ScopedSignalHandler::s_event_loop_ = nullptr;
71
72
73 // Setup our interface to the Binder driver or die trying.
74 int SetupBinderOrCrash() {
75   int binder_fd = -1;
76   android::ProcessState::self()->setThreadPoolMaxThreadCount(0);
77   android::IPCThreadState::self()->disableBackgroundScheduling(true);
78   int err = android::IPCThreadState::self()->setupPolling(&binder_fd);
79   CHECK_EQ(err, 0) << "Error setting up binder polling: " << strerror(-err);
80   CHECK_GE(binder_fd, 0) << "Invalid binder FD: " << binder_fd;
81   return binder_fd;
82 }
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
88   const int8_t dev_mode_on = property_get_bool(kDevModePropertyKey, 0);
89   const char* service_name = (dev_mode_on) ? kDevModeServiceName : kServiceName;
90   CHECK_EQ(sm->addService(android::String16(service_name), service),
91            android::NO_ERROR);
92 }
93
94 }  // namespace
95
96 void OnBinderReadReady(int fd) {
97   android::IPCThreadState::self()->handlePolledCommands();
98 }
99
100 int main(int argc, char** argv) {
101   android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
102   LOG(INFO) << "wificond is starting up...";
103
104   std::unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher(
105       new android::wificond::LooperBackedEventLoop());
106   ScopedSignalHandler scoped_signal_handler(event_dispatcher.get());
107
108   int binder_fd = SetupBinderOrCrash();
109   CHECK(event_dispatcher->WatchFileDescriptor(
110       binder_fd,
111       android::wificond::EventLoop::kModeInput,
112       &OnBinderReadReady)) << "Failed to watch binder FD";
113
114   android::sp<android::IBinder> server = new android::wificond::Server();
115   RegisterServiceOrCrash(server);
116
117   event_dispatcher->Poll();
118   LOG(INFO) << "wificond is about to exit";
119   return 0;
120 }