summaryrefslogtreecommitdiffstats
path: root/src/mandi_daemon.c
blob: cf3bfbef301cafec6766c82524bdfec992d0ed85 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

#include <time.h>
#include <unistd.h>

#include <dbus/dbus.h>
#include <errno.h>

#include "plugin.h"

#define MANDI_DAEMON_SERVICE PLUGIN_ROOT_INTF

typedef struct {
    DBusConnection *bus;
    DBusWatch *bus_read_watch;
    int bus_read_fd;
} mandi_daemon_t;

extern plugin_t wpa_supplicant_plugin;
extern plugin_t ifw_plugin;
plugin_t *plugins[] = {
    &wpa_supplicant_plugin,
    &ifw_plugin,
    NULL,
};

static mandi_daemon_t *mandi_daemon_p; /* global variable used in signal handlers only */
static void sigquit(int signum);
static int mandi_daemon_init(mandi_daemon_t *daemon);
static void mandi_daemon_exit(mandi_daemon_t *daemon, int exit_code);
static void mandi_daemon_handle_dbus(mandi_daemon_t *daemon);
static int mandi_daemon_acquire_service(mandi_daemon_t *daemon);
static int mandi_daemon_init_watch(mandi_daemon_t *daemon);
static dbus_bool_t mandi_daemon_add_watch(DBusWatch *watch, void *data);
static void mandi_daemon_toggle_watch(DBusWatch *watch, void *data);
static void mandi_daemon_remove_watch(DBusWatch *watch, void *data);
static DBusConnection *mandi_daemon_get_system_bus();
static int mandi_daemon_init_path(mandi_daemon_t *daemon, plugin_t *plugin);
static void mandi_daemon_object_path_unregister(DBusConnection *connection, void *user_data);
static DBusHandlerResult mandi_daemon_object_path_handle_message(DBusConnection *connection, DBusMessage *message, void *user_data);

int main(int argc, char **argv)
{
    mandi_daemon_t mandi_daemon;

    /* set up signal handlers to exit nicely when needed */
    mandi_daemon_p = &mandi_daemon;
    signal(SIGINT, sigquit);
    signal(SIGTERM, sigquit);
    signal(SIGQUIT, sigquit);

    mandi_daemon_init(&mandi_daemon);

    if (getopt(argc, argv, "d") == 'd') {
        daemon(0, 0);
    }

    printf("Monitoring daemon waiting for events ...\n");

    while (1) {
        int num_fds;
	fd_set read_fds;
        plugin_t **ptr;
        plugin_t *plugin;

        FD_ZERO(&read_fds);
        FD_SET(mandi_daemon.bus_read_fd, &read_fds);
        num_fds = mandi_daemon.bus_read_fd + 1;
        for (ptr = plugins; *ptr; ptr++) {
            plugin = *ptr;
            if (plugin->fd >= 0) {
                FD_SET(plugin->fd, &read_fds);
                if (plugin->fd + 1 > num_fds) {
                    num_fds = plugin->fd  + 1;
                }
            }
        }

        if (select(num_fds, &read_fds, NULL, NULL, NULL) > 0) {
            if (FD_ISSET(mandi_daemon.bus_read_fd, &read_fds)) {
                mandi_daemon_handle_dbus(&mandi_daemon);
            }
            for (ptr = plugins; *ptr; ptr++) {
                plugin = *ptr;
                if (plugin->fd >= 0 && FD_ISSET(plugin->fd, &read_fds)) {
                    plugin->handle_incoming(plugin, mandi_daemon.bus);
                }
            }
        } else {
	    fprintf(stderr, "unhandled error in select(): %s\n", strerror(errno));

       }
    }

    mandi_daemon_exit(&mandi_daemon, EXIT_SUCCESS);
    return 0;
}

static void sigquit(int signum) {
    printf("SIGINT, SIGTERM or SIGQUIT catched, trying to exit nicely\n");
    mandi_daemon_exit(mandi_daemon_p, EXIT_SUCCESS);
}

static int mandi_daemon_init(mandi_daemon_t *daemon) {
    plugin_t **ptr;
    plugin_t *plugin;

    daemon->bus = mandi_daemon_get_system_bus();
    if (!daemon->bus) {
	mandi_daemon_exit(daemon, EXIT_FAILURE);
    }

    if (mandi_daemon_init_watch(daemon) != 0) {
        fprintf(stderr, "unable to init DBus watch\n");
	mandi_daemon_exit(daemon, EXIT_FAILURE);
    }

    if (mandi_daemon_acquire_service(daemon) != 0) {
        fprintf(stderr, "unable to init DBus service\n");
	mandi_daemon_exit(daemon, EXIT_FAILURE);
    }
    
    for (ptr = plugins; *ptr; ptr++) {
        plugin = *ptr;
        if (plugin->init(plugin, daemon->bus) != 0) {
            fprintf(stderr, "unable to init \"%s\" plugin\n", plugin->name);
            mandi_daemon_exit(daemon, EXIT_FAILURE);
        }

        mandi_daemon_init_path(daemon, plugin);
    }

    return 0;
}

static void mandi_daemon_exit(mandi_daemon_t *daemon, int exit_code) {
    plugin_t **ptr;
    plugin_t *plugin;
    for (ptr = plugins; *ptr; ptr++) {
        plugin = *ptr;
        plugin->deinit(plugin, daemon->bus);
    }
    /* unregister dbus */
    exit(exit_code);
}

static void mandi_daemon_handle_dbus(mandi_daemon_t *daemon) {
    dbus_watch_handle(daemon->bus_read_watch, DBUS_WATCH_READABLE);
    dbus_connection_dispatch(daemon->bus);
}

int mandi_daemon_acquire_service(mandi_daemon_t *daemon) {
    DBusError error;
    dbus_error_init(&error);

    if (dbus_bus_request_name(daemon->bus, MANDI_DAEMON_SERVICE, 0, &error) == -1) {
        fprintf(stderr, "dbus_bus_acquire_service(): %s\n", error.message);
        fprintf(stderr, "Make sure a DBus policy allows you to acquire this service.\n");
        dbus_error_free(&error);
        return -1;
    }
    dbus_connection_dispatch(daemon->bus);

    dbus_error_free(&error);
    return 0;
}

/* get fds to be watched */
static int mandi_daemon_init_watch(mandi_daemon_t *daemon) {
    DBusError error;

    dbus_error_init(&error);

    if (dbus_connection_set_watch_functions(daemon->bus,
                                            mandi_daemon_add_watch,
                                            mandi_daemon_toggle_watch,
                                            mandi_daemon_remove_watch,
                                            (void *) daemon,
                                            NULL) == FALSE) {
        fprintf(stderr, "dbus_connection_set_watch_functions(): %s\n", error.message);
        dbus_error_free(&error);
        return -1;
    }
    dbus_connection_dispatch(daemon->bus);

    dbus_error_free(&error);
    return 0;
}

static dbus_bool_t mandi_daemon_add_watch(DBusWatch *watch, void *data) {
    mandi_daemon_t *daemon = (mandi_daemon_t *) data;
    if (dbus_watch_get_flags(watch) & DBUS_WATCH_READABLE) {
        fprintf(stderr, "mandi_daemon_add_watch(): READABLE\n");
        daemon->bus_read_watch = watch;
	daemon->bus_read_fd = dbus_watch_get_fd(daemon->bus_read_watch);
    }
    /* do nothing for WRITABLE watch, we dispatch when needed */
    return TRUE;
}

static void mandi_daemon_toggle_watch(DBusWatch *watch, void *data) {
    fprintf(stderr, "mandi_daemon_toggle_watch()\n");
    /* FIXME : do we need to do something here ? */
}

static void mandi_daemon_remove_watch(DBusWatch *watch, void *data) {
    if (dbus_watch_get_flags(watch) & DBUS_WATCH_READABLE) {
        fprintf(stderr, "mandi_daemon_remove_watch(): READABLE\n");
        /* FIXME : do we need to do something here ? */
    }
}

/* set a connection to the system bus */
static DBusConnection *mandi_daemon_get_system_bus() {
    DBusConnection *bus;
    DBusError error;
    dbus_error_init(&error);

    bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
    if (!bus) {
        fprintf(stderr, "Failed to connect to the D-BUS daemon: %s\n", error.message);
        dbus_error_free(&error);
        return NULL;
    }

    dbus_error_free(&error);
    return bus;
}

static int mandi_daemon_init_path(mandi_daemon_t *daemon, plugin_t *plugin) {
    struct DBusObjectPathVTable object_path_vtable = {
        mandi_daemon_object_path_unregister,
        mandi_daemon_object_path_handle_message,
        NULL, NULL, NULL, NULL
    };

    DBusError error;
    dbus_error_init(&error);

    if (dbus_connection_register_object_path(daemon->bus, plugin->path, &object_path_vtable, plugin) == FALSE) {
        fprintf(stderr, "dbus_connection_register_object_path(): not enough memory\n");
        dbus_error_free(&error);
        return -1;
    }
    dbus_connection_dispatch(daemon->bus);

    dbus_error_free(&error);
    return 0;
}

static void mandi_daemon_object_path_unregister(DBusConnection *connection, void *user_data) {
}

static DBusHandlerResult mandi_daemon_object_path_handle_message(DBusConnection *connection, DBusMessage *message, void *user_data) {
    plugin_t *plugin = (plugin_t *) user_data;

    printf("handling method call '%s' on interface '%s'\n",
           dbus_message_get_member(message),
           dbus_message_get_interface(message));

    return plugin->handle_message(connection, message, plugin);
}