Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(699)

Unified Diff: src/main.cc

Issue 2812018: Implement basic infrastructure for cashew daemon. (Closed) Base URL: ssh://gitrw.chromium.org/cashew.git
Patch Set: Created 10 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/cashew.conf ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/main.cc
diff --git a/src/main.cc b/src/main.cc
new file mode 100644
index 0000000000000000000000000000000000000000..bddddfc1deeecf16521ba9720b5cb7bc80aa7700
--- /dev/null
+++ b/src/main.cc
@@ -0,0 +1,112 @@
+// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <config.h>
+
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <dbus-c++/dbus.h>
+#include <gflags/gflags.h>
+#include <glog/logging.h>
+
+namespace cashew {
+
+typedef int Signal;
+typedef void (*SignalHandler)(Signal signal);
+
+int InstallSignalHandler(const Signal signal,
+ const SignalHandler signal_handler,
+ const sigset_t& mask) {
+ DLOG(INFO) << "installing handler for signal " << signal;
+ struct sigaction action;
+ memset(&action, 0, sizeof(action));
+ action.sa_handler = signal_handler;
+ action.sa_mask = mask;
+ return sigaction(signal, &action, NULL);
+}
+
+static DBus::BusDispatcher *g_dispatcher = NULL;
+
+static void OnSignal(int signal) {
+ DLOG(INFO) << "received signal " << signal;
+ if (g_dispatcher) {
+ g_dispatcher->leave();
+ }
+}
+
+int InstallSignalHandlers() {
+ sigset_t mask;
+ if (sigemptyset(&mask) < 0) {
+ PLOG(ERROR) << "sigemptyset failed";
+ return -1;
+ }
+ if (sigaddset(&mask, SIGINT) < 0) {
+ PLOG(ERROR) << "couldn't add SIGINT to mask";
+ return -1;
+ }
+ if (sigaddset(&mask, SIGTERM) < 0) {
+ PLOG(ERROR) << "couldn't add SIGTERM to mask";
+ return -1;
+ }
+ if (InstallSignalHandler(SIGINT, OnSignal, mask) < 0) {
+ PLOG(ERROR) << "couldn't install SIGINT handler";
+ return -1;
+ }
+ if (InstallSignalHandler(SIGTERM, OnSignal, mask) < 0) {
+ PLOG(ERROR) << "couldn't install SIGTERM handler";
+ return -1;
+ }
+ return 0;
+}
+
+// should not be called while event loop is running
+static void CleanUpForExit() {
+ DLOG(INFO) << "cleaning up";
+ DBus::default_dispatcher = NULL;
+ DBus::BusDispatcher *dispatcher_to_delete = g_dispatcher;
+ g_dispatcher = NULL;
+ delete dispatcher_to_delete;
+ // TODO(vlaviano): uncomment when we get a newer version of glog
+ // google::ShutdownGoogleLogging();
+}
+
+} // namespace cashew
+
+int main(int argc, char *argv[]) {
+ google::SetUsageMessage(argv[0]);
+ google::ParseCommandLineFlags(&argc, &argv, true);
+ google::InitGoogleLogging(argv[0]);
+ google::InstallFailureSignalHandler();
+ LOG(INFO) << PACKAGE_STRING;
+
+ DLOG(INFO) << "creating dbus dispatcher";
+ cashew::g_dispatcher = new(std::nothrow) DBus::BusDispatcher();
+ if (!cashew::g_dispatcher) {
+ LOG(ERROR) << "couldn't create dbus dispatcher";
+ cashew::CleanUpForExit();
+ exit(EXIT_FAILURE);
+ }
+ DBus::default_dispatcher = cashew::g_dispatcher;
+
+ // TODO(vlaviano): policies
+
+ // TODO(vlaviano): back-end carrier apis
+
+ // TODO(vlaviano): front-end dbus api
+
+ if (cashew::InstallSignalHandlers() < 0) {
+ LOG(ERROR) << "couldn't install signal handlers";
+ cashew::CleanUpForExit();
+ exit(EXIT_FAILURE);
+ }
+
+ DLOG(INFO) << "entering event loop";
+ cashew::g_dispatcher->enter();
+ DLOG(INFO) << "exited event loop";
+ cashew::CleanUpForExit();
+ exit(EXIT_SUCCESS);
+}
« no previous file with comments | « src/cashew.conf ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698