aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 854166ab12fc35c8f0a6aa9ba1c0dc0952dd3bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

#include <signal.h>
#include <syslog.h>
#include <dbus/dbus-glib.h>
#include "mm-manager.h"
#include "mm-options.h"

static void
mm_signal_handler (int signo)
{
    if (signo == SIGUSR1)
        mm_options_set_debug (!mm_options_debug ());
}

static void
setup_signals (void)
{
    struct sigaction action;
    sigset_t mask;

    sigemptyset (&mask);
    action.sa_handler = mm_signal_handler;
    action.sa_mask = mask;
    action.sa_flags = 0;
    sigaction (SIGUSR1,  &action, NULL);
}

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 " MM_DBUS_SERVICE
                   " 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[])
{
    GMainLoop *loop;
    MMManager *manager;

    mm_options_parse (argc, argv);
    g_type_init ();

    setup_signals ();

    if (!mm_options_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;
}