diff options
author | Tambet Ingo <tambet@gmail.com> | 2008-07-31 09:43:00 +0300 |
---|---|---|
committer | Tambet Ingo <tambet@gmail.com> | 2008-07-31 09:43:00 +0300 |
commit | cc31458b18e8c274641cf124f5bf7ec6fe506dfb (patch) | |
tree | e4ec49a3e26cfd0f002faa76e0a6c0d180204177 /src/main.c |
Initial commit.
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 00000000..a18ad3b5 --- /dev/null +++ b/src/main.c @@ -0,0 +1,166 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +#include <syslog.h> +#include <dbus/dbus-glib.h> +#include "mm-manager.h" + +static void +log_handler (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer ignored) +{ + int syslog_priority; + + switch (log_level) { + case G_LOG_LEVEL_ERROR: + syslog_priority = LOG_CRIT; + break; + + case G_LOG_LEVEL_CRITICAL: + syslog_priority = LOG_ERR; + break; + + case G_LOG_LEVEL_WARNING: + syslog_priority = LOG_WARNING; + break; + + case G_LOG_LEVEL_MESSAGE: + syslog_priority = LOG_NOTICE; + break; + + case G_LOG_LEVEL_DEBUG: + syslog_priority = LOG_DEBUG; + break; + + case G_LOG_LEVEL_INFO: + default: + syslog_priority = LOG_INFO; + break; + } + + syslog (syslog_priority, "%s", message); +} + + +static void +logging_setup (void) +{ + openlog (G_LOG_DOMAIN, LOG_CONS, LOG_DAEMON); + g_log_set_handler (G_LOG_DOMAIN, + G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, + log_handler, + NULL); +} + +static void +logging_shutdown (void) +{ + closelog (); +} + +static void +destroy_cb (DBusGProxy *proxy, gpointer user_data) +{ + GMainLoop *loop = (GMainLoop *) user_data; + + g_message ("disconnected from the system bus, exiting."); + g_main_loop_quit (loop); +} + +static gboolean +dbus_init (GMainLoop *loop) +{ + DBusGConnection *connection; + DBusGProxy *proxy; + GError *err = NULL; + int request_name_result; + + connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &err); + if (!connection) { + g_warning ("Could not get the system bus. Make sure " + "the message bus daemon is running! Message: %s", + err->message); + g_error_free (err); + return FALSE; + } + + proxy = dbus_g_proxy_new_for_name (connection, + "org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus"); + + if (!dbus_g_proxy_call (proxy, "RequestName", &err, + G_TYPE_STRING, MM_DBUS_SERVICE, + G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE, + G_TYPE_INVALID, + G_TYPE_UINT, &request_name_result, + G_TYPE_INVALID)) { + g_warning ("Could not acquire the %s service.\n" + " Message: '%s'", MM_DBUS_SERVICE, err->message); + g_error_free (err); + goto err; + } + + if (request_name_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { + g_warning ("Could not acquire the NetworkManagerSystemSettings service " + "as it is already taken. Return: %d", + request_name_result); + goto err; + } + + g_signal_connect (proxy, "destroy", G_CALLBACK (destroy_cb), loop); + + return TRUE; + + err: + dbus_g_connection_unref (connection); + g_object_unref (proxy); + + return FALSE; +} + +int +main (int argc, char *argv[]) +{ + GOptionContext *opt_ctx; + GError *error = NULL; + GMainLoop *loop; + MMManager *manager; + gboolean debug = FALSE; + GOptionEntry entries[] = { + { "debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Output to console rather than syslog", NULL }, + { NULL } + }; + + opt_ctx = g_option_context_new (NULL); + g_option_context_set_summary (opt_ctx, "DBus system service to communicate with modems."); + g_option_context_add_main_entries (opt_ctx, entries, NULL); + + if (!g_option_context_parse (opt_ctx, &argc, &argv, &error)) { + g_warning ("%s\n", error->message); + g_error_free (error); + return 1; + } + + g_option_context_free (opt_ctx); + + g_type_init (); + + if (!debug) + logging_setup (); + + loop = g_main_loop_new (NULL, FALSE); + + if (!dbus_init (loop)) + return -1; + + manager = mm_manager_new (); + + g_main_loop_run (loop); + + g_object_unref (manager); + logging_shutdown (); + + return 0; +} |