summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel.holtmann@intel.com>2009-05-05 19:03:13 -0700
committerMarcel Holtmann <marcel.holtmann@intel.com>2009-05-05 19:03:13 -0700
commit838583f4988ba132cab57d5c3ddbac80072c5284 (patch)
tree167f166fda9a86134c9d7c9969c09e007836143f /src
parent2eca0bc88fb37edd064f05cbfb4fbea56c0bd56d (diff)
downloadofono-838583f4988ba132cab57d5c3ddbac80072c5284.tar.bz2
Add option parsing and mainloop setup
Diffstat (limited to 'src')
-rw-r--r--src/main.c93
1 files changed, 90 insertions, 3 deletions
diff --git a/src/main.c b/src/main.c
index 5eccb2c8..37deecb2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,6 +1,6 @@
/*
*
- * oFono - Open Telephony stack for Linux
+ * oFono - Open Source Telephony
*
* Copyright (C) 2008-2009 Intel Corporation. All rights reserved.
*
@@ -23,16 +23,103 @@
#include <config.h>
#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <signal.h>
+
#include "ofono.h"
-int main(int argc, char *argv[])
+static GMainLoop *event_loop;
+
+static void sig_debug(int sig)
{
- __ofono_log_init();
+ __ofono_toggle_debug();
+}
+
+static void sig_term(int sig)
+{
+ g_main_loop_quit(event_loop);
+}
+
+static gboolean option_detach = TRUE;
+static gboolean option_debug = FALSE;
+
+static GOptionEntry options[] = {
+ { "nodetach", 'n', G_OPTION_FLAG_REVERSE,
+ G_OPTION_ARG_NONE, &option_detach,
+ "Don't run as daemon in background" },
+ { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
+ "Enable debug information output" },
+ { NULL },
+};
+
+int main(int argc, char **argv)
+{
+ GOptionContext *context;
+ GError *err = NULL;
+ struct sigaction sa;
+
+#ifdef NEED_THREADS
+ if (g_thread_supported() == FALSE)
+ g_thread_init(NULL);
+#endif
+
+ context = g_option_context_new(NULL);
+ g_option_context_add_main_entries(context, options, NULL);
+
+ if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
+ if (err != NULL) {
+ g_printerr("%s\n", err->message);
+ g_error_free(err);
+ return 1;
+ }
+
+ g_printerr("An unknown error occurred\n");
+ return 1;
+ }
+
+ g_option_context_free(context);
+
+ if (option_detach == TRUE) {
+ if (daemon(0, 0)) {
+ perror("Can't start daemon");
+ return 1;
+ }
+ }
+
+ event_loop = g_main_loop_new(NULL, FALSE);
+
+#ifdef NEED_THREADS
+ if (dbus_threads_init_default() == FALSE) {
+ fprintf(stderr, "Can't init usage of threads\n");
+ exit(1);
+ }
+#endif
+
+ __ofono_log_init(option_detach, option_debug);
__ofono_plugin_init(NULL, NULL);
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_flags = SA_NOCLDSTOP;
+ sa.sa_handler = sig_term;
+ sigaction(SIGTERM, &sa, NULL);
+ sigaction(SIGINT, &sa, NULL);
+
+ sa.sa_handler = sig_debug;
+ sigaction(SIGUSR2, &sa, NULL);
+
+ sa.sa_handler = SIG_IGN;
+ sigaction(SIGPIPE, &sa, NULL);
+
+ g_main_loop_run(event_loop);
+
__ofono_plugin_cleanup();
+ g_main_loop_unref(event_loop);
+
__ofono_log_cleanup();
return 0;