OSDN Git Service

Implement watching file descriptor for wificond event loop
[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
25 #include <looper_backed_event_loop.h>
26
27 namespace {
28
29 class ScopedSignalHandler final {
30  public:
31   ScopedSignalHandler(android::wificond::LooperBackedEventLoop* event_loop) {
32     if (s_event_loop_ != nullptr) {
33       LOG(FATAL) << "Only instantiate one signal handler per process!";
34     }
35     s_event_loop_ = event_loop;
36     std::signal(SIGINT, &ScopedSignalHandler::LeaveLoop);
37     std::signal(SIGTERM, &ScopedSignalHandler::LeaveLoop);
38   }
39
40   ~ScopedSignalHandler() {
41     std::signal(SIGINT, SIG_DFL);
42     std::signal(SIGTERM, SIG_DFL);
43     s_event_loop_ = nullptr;
44   }
45
46  private:
47   static android::wificond::LooperBackedEventLoop* s_event_loop_;
48   static void LeaveLoop(int signal) {
49     if (s_event_loop_ != nullptr) {
50       s_event_loop_->TriggerExit();
51     }
52   }
53
54   DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
55 };
56
57 android::wificond::LooperBackedEventLoop*
58     ScopedSignalHandler::s_event_loop_ = nullptr;
59
60 }  // namespace
61
62 int main(int argc, char** argv) {
63   android::base::InitLogging(argv);
64   LOG(INFO) << "wificond is starting up...";
65   std::unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher_(
66       new android::wificond::LooperBackedEventLoop());
67   ScopedSignalHandler scoped_signal_handler(event_dispatcher_.get());
68   event_dispatcher_->Poll();
69   LOG(INFO) << "Leaving the loop...";
70   return 0;
71 }