OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <config.h> |
| 6 |
| 7 #include <signal.h> |
| 8 #include <stdio.h> |
| 9 #include <stdlib.h> |
| 10 #include <string.h> |
| 11 |
| 12 #include <dbus-c++/dbus.h> |
| 13 #include <gflags/gflags.h> |
| 14 #include <glog/logging.h> |
| 15 |
| 16 namespace cashew { |
| 17 |
| 18 typedef int Signal; |
| 19 typedef void (*SignalHandler)(Signal signal); |
| 20 |
| 21 int InstallSignalHandler(const Signal signal, |
| 22 const SignalHandler signal_handler, |
| 23 const sigset_t& mask) { |
| 24 DLOG(INFO) << "installing handler for signal " << signal; |
| 25 struct sigaction action; |
| 26 memset(&action, 0, sizeof(action)); |
| 27 action.sa_handler = signal_handler; |
| 28 action.sa_mask = mask; |
| 29 return sigaction(signal, &action, NULL); |
| 30 } |
| 31 |
| 32 static DBus::BusDispatcher *g_dispatcher = NULL; |
| 33 |
| 34 static void OnSignal(int signal) { |
| 35 DLOG(INFO) << "received signal " << signal; |
| 36 if (g_dispatcher) { |
| 37 g_dispatcher->leave(); |
| 38 } |
| 39 } |
| 40 |
| 41 int InstallSignalHandlers() { |
| 42 sigset_t mask; |
| 43 if (sigemptyset(&mask) < 0) { |
| 44 PLOG(ERROR) << "sigemptyset failed"; |
| 45 return -1; |
| 46 } |
| 47 if (sigaddset(&mask, SIGINT) < 0) { |
| 48 PLOG(ERROR) << "couldn't add SIGINT to mask"; |
| 49 return -1; |
| 50 } |
| 51 if (sigaddset(&mask, SIGTERM) < 0) { |
| 52 PLOG(ERROR) << "couldn't add SIGTERM to mask"; |
| 53 return -1; |
| 54 } |
| 55 if (InstallSignalHandler(SIGINT, OnSignal, mask) < 0) { |
| 56 PLOG(ERROR) << "couldn't install SIGINT handler"; |
| 57 return -1; |
| 58 } |
| 59 if (InstallSignalHandler(SIGTERM, OnSignal, mask) < 0) { |
| 60 PLOG(ERROR) << "couldn't install SIGTERM handler"; |
| 61 return -1; |
| 62 } |
| 63 return 0; |
| 64 } |
| 65 |
| 66 // should not be called while event loop is running |
| 67 static void CleanUpForExit() { |
| 68 DLOG(INFO) << "cleaning up"; |
| 69 DBus::default_dispatcher = NULL; |
| 70 DBus::BusDispatcher *dispatcher_to_delete = g_dispatcher; |
| 71 g_dispatcher = NULL; |
| 72 delete dispatcher_to_delete; |
| 73 // TODO(vlaviano): uncomment when we get a newer version of glog |
| 74 // google::ShutdownGoogleLogging(); |
| 75 } |
| 76 |
| 77 } // namespace cashew |
| 78 |
| 79 int main(int argc, char *argv[]) { |
| 80 google::SetUsageMessage(argv[0]); |
| 81 google::ParseCommandLineFlags(&argc, &argv, true); |
| 82 google::InitGoogleLogging(argv[0]); |
| 83 google::InstallFailureSignalHandler(); |
| 84 LOG(INFO) << PACKAGE_STRING; |
| 85 |
| 86 DLOG(INFO) << "creating dbus dispatcher"; |
| 87 cashew::g_dispatcher = new(std::nothrow) DBus::BusDispatcher(); |
| 88 if (!cashew::g_dispatcher) { |
| 89 LOG(ERROR) << "couldn't create dbus dispatcher"; |
| 90 cashew::CleanUpForExit(); |
| 91 exit(EXIT_FAILURE); |
| 92 } |
| 93 DBus::default_dispatcher = cashew::g_dispatcher; |
| 94 |
| 95 // TODO(vlaviano): policies |
| 96 |
| 97 // TODO(vlaviano): back-end carrier apis |
| 98 |
| 99 // TODO(vlaviano): front-end dbus api |
| 100 |
| 101 if (cashew::InstallSignalHandlers() < 0) { |
| 102 LOG(ERROR) << "couldn't install signal handlers"; |
| 103 cashew::CleanUpForExit(); |
| 104 exit(EXIT_FAILURE); |
| 105 } |
| 106 |
| 107 DLOG(INFO) << "entering event loop"; |
| 108 cashew::g_dispatcher->enter(); |
| 109 DLOG(INFO) << "exited event loop"; |
| 110 cashew::CleanUpForExit(); |
| 111 exit(EXIT_SUCCESS); |
| 112 } |
OLD | NEW |