ofono/src/main.c

190 lines
3.9 KiB
C
Raw Normal View History

2009-04-26 19:12:32 +00:00
/*
*
2009-05-06 02:03:13 +00:00
* oFono - Open Source Telephony
2009-04-26 19:12:32 +00:00
*
* Copyright (C) 2008-2009 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
2009-07-29 15:55:00 +00:00
#include <gdbus.h>
2009-05-06 02:03:13 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
2009-04-26 19:12:32 +00:00
#include "ofono.h"
#define SHUTDOWN_GRACE_SECONDS 10
2009-05-06 02:03:13 +00:00
static GMainLoop *event_loop;
2009-12-03 02:10:18 +00:00
static volatile sig_atomic_t terminated = 0;
void __ofono_exit()
{
g_main_loop_quit(event_loop);
}
static gboolean quit_eventloop(gpointer user_data)
{
__ofono_exit();
return FALSE;
}
2009-05-06 02:03:13 +00:00
static void sig_debug(int sig)
2009-04-26 19:12:32 +00:00
{
2009-05-06 02:03:13 +00:00
__ofono_toggle_debug();
}
2009-12-03 02:10:18 +00:00
static gboolean initiate_shutdown(gpointer user_data)
{
g_timeout_add_seconds(SHUTDOWN_GRACE_SECONDS, quit_eventloop, NULL);
__ofono_modem_shutdown();
return FALSE;
}
2009-05-06 02:03:13 +00:00
static void sig_term(int sig)
{
2009-12-03 02:10:18 +00:00
if (terminated > 0)
return;
2009-12-03 02:10:18 +00:00
terminated = 1;
g_idle_add(initiate_shutdown, NULL);
2009-05-06 02:03:13 +00:00
}
static void system_bus_disconnected(DBusConnection *conn, void *user_data)
{
ofono_error("System bus has disconnected!");
g_main_loop_quit(event_loop);
}
2009-05-06 02:03:13 +00:00
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;
DBusConnection *conn;
DBusError error;
2009-05-06 02:03:13 +00:00
#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);
2009-04-26 19:12:32 +00:00
dbus_error_init(&error);
2009-05-06 04:13:14 +00:00
conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, OFONO_SERVICE, &error);
if (!conn) {
if (dbus_error_is_set(&error) == TRUE) {
ofono_error("Unable to hop onto D-Bus: %s",
error.message);
dbus_error_free(&error);
} else
ofono_error("Unable to hop onto D-Bus");
2009-05-06 04:13:14 +00:00
goto cleanup;
}
g_dbus_set_disconnect_function(conn, system_bus_disconnected,
NULL, NULL);
__ofono_dbus_init(conn);
__ofono_manager_init();
2009-05-06 04:13:14 +00:00
2009-04-26 19:28:48 +00:00
__ofono_plugin_init(NULL, NULL);
2009-05-06 02:03:13 +00:00
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);
2009-04-26 19:28:48 +00:00
__ofono_plugin_cleanup();
2009-05-06 04:13:14 +00:00
__ofono_manager_cleanup();
__ofono_dbus_cleanup();
dbus_connection_unref(conn);
2009-05-06 04:13:14 +00:00
cleanup:
2009-05-06 02:03:13 +00:00
g_main_loop_unref(event_loop);
2009-04-26 19:12:32 +00:00
__ofono_log_cleanup();
return 0;
}