435fb93bb1c12c6e31c78e761f13f91eedec3c4b
[platform/upstream/connman.git] / gdbus / mainloop.c
1 /*
2  *
3  *  D-Bus helper library
4  *
5  *  Copyright (C) 2004-2011  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <glib.h>
29 #include <dbus/dbus.h>
30
31 #include "gdbus.h"
32
33 #define info(fmt...)
34 #define error(fmt...)
35 #define debug(fmt...)
36
37 struct timeout_handler {
38         guint id;
39         DBusTimeout *timeout;
40 };
41
42 struct watch_info {
43         guint id;
44         DBusWatch *watch;
45         DBusConnection *conn;
46 };
47
48 struct disconnect_data {
49         GDBusWatchFunction function;
50         void *user_data;
51 };
52
53 static gboolean disconnected_signal(DBusConnection *conn,
54                                                 DBusMessage *msg, void *data)
55 {
56         struct disconnect_data *dc_data = data;
57
58         error("Got disconnected from the system message bus");
59
60         dc_data->function(conn, dc_data->user_data);
61
62         dbus_connection_unref(conn);
63
64         return TRUE;
65 }
66
67 static gboolean message_dispatch(void *data)
68 {
69         DBusConnection *conn = data;
70
71         /* Dispatch messages */
72         while (dbus_connection_dispatch(conn) == DBUS_DISPATCH_DATA_REMAINS);
73
74         dbus_connection_unref(conn);
75
76         return FALSE;
77 }
78
79 static inline void queue_dispatch(DBusConnection *conn,
80                                                 DBusDispatchStatus status)
81 {
82         if (status == DBUS_DISPATCH_DATA_REMAINS)
83                 g_idle_add(message_dispatch, dbus_connection_ref(conn));
84 }
85
86 static gboolean watch_func(GIOChannel *chan, GIOCondition cond, gpointer data)
87 {
88         struct watch_info *info = data;
89         unsigned int flags = 0;
90         DBusDispatchStatus status;
91
92         if (cond & G_IO_IN)  flags |= DBUS_WATCH_READABLE;
93         if (cond & G_IO_OUT) flags |= DBUS_WATCH_WRITABLE;
94         if (cond & G_IO_HUP) flags |= DBUS_WATCH_HANGUP;
95         if (cond & G_IO_ERR) flags |= DBUS_WATCH_ERROR;
96
97         dbus_watch_handle(info->watch, flags);
98
99         status = dbus_connection_get_dispatch_status(info->conn);
100         queue_dispatch(info->conn, status);
101
102         return TRUE;
103 }
104
105 static void watch_info_free(void *data)
106 {
107         struct watch_info *info = data;
108
109         if (info->id > 0) {
110                 g_source_remove(info->id);
111                 info->id = 0;
112         }
113
114         dbus_connection_unref(info->conn);
115
116         g_free(info);
117 }
118
119 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
120 {
121         DBusConnection *conn = data;
122         GIOCondition cond = G_IO_HUP | G_IO_ERR;
123         GIOChannel *chan;
124         struct watch_info *info;
125         unsigned int flags;
126         int fd;
127
128         if (!dbus_watch_get_enabled(watch))
129                 return TRUE;
130
131         info = g_new0(struct watch_info, 1);
132
133         fd = dbus_watch_get_unix_fd(watch);
134         chan = g_io_channel_unix_new(fd);
135
136         info->watch = watch;
137         info->conn = dbus_connection_ref(conn);
138
139         dbus_watch_set_data(watch, info, watch_info_free);
140
141         flags = dbus_watch_get_flags(watch);
142
143         if (flags & DBUS_WATCH_READABLE) cond |= G_IO_IN;
144         if (flags & DBUS_WATCH_WRITABLE) cond |= G_IO_OUT;
145
146         info->id = g_io_add_watch(chan, cond, watch_func, info);
147
148         g_io_channel_unref(chan);
149
150         return TRUE;
151 }
152
153 static void remove_watch(DBusWatch *watch, void *data)
154 {
155         if (dbus_watch_get_enabled(watch))
156                 return;
157
158         /* will trigger watch_info_free() */
159         dbus_watch_set_data(watch, NULL, NULL);
160 }
161
162 static void watch_toggled(DBusWatch *watch, void *data)
163 {
164         /* Because we just exit on OOM, enable/disable is
165          * no different from add/remove */
166         if (dbus_watch_get_enabled(watch))
167                 add_watch(watch, data);
168         else
169                 remove_watch(watch, data);
170 }
171
172 static gboolean timeout_handler_dispatch(gpointer data)
173 {
174         struct timeout_handler *handler = data;
175
176         handler->id = 0;
177
178         /* if not enabled should not be polled by the main loop */
179         if (!dbus_timeout_get_enabled(handler->timeout))
180                 return FALSE;
181
182         dbus_timeout_handle(handler->timeout);
183
184         return FALSE;
185 }
186
187 static void timeout_handler_free(void *data)
188 {
189         struct timeout_handler *handler = data;
190
191         if (handler->id > 0) {
192                 g_source_remove(handler->id);
193                 handler->id = 0;
194         }
195
196         g_free(handler);
197 }
198
199 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
200 {
201         int interval = dbus_timeout_get_interval(timeout);
202         struct timeout_handler *handler;
203
204         if (!dbus_timeout_get_enabled(timeout))
205                 return TRUE;
206
207         handler = g_new0(struct timeout_handler, 1);
208
209         handler->timeout = timeout;
210
211         dbus_timeout_set_data(timeout, handler, timeout_handler_free);
212
213         handler->id = g_timeout_add(interval, timeout_handler_dispatch,
214                                                                 handler);
215
216         return TRUE;
217 }
218
219 static void remove_timeout(DBusTimeout *timeout, void *data)
220 {
221         /* will trigger timeout_handler_free() */
222         dbus_timeout_set_data(timeout, NULL, NULL);
223 }
224
225 static void timeout_toggled(DBusTimeout *timeout, void *data)
226 {
227         if (dbus_timeout_get_enabled(timeout))
228                 add_timeout(timeout, data);
229         else
230                 remove_timeout(timeout, data);
231 }
232
233 static void dispatch_status(DBusConnection *conn,
234                                         DBusDispatchStatus status, void *data)
235 {
236         if (!dbus_connection_get_is_connected(conn))
237                 return;
238
239         queue_dispatch(conn, status);
240 }
241
242 static inline void setup_dbus_with_main_loop(DBusConnection *conn)
243 {
244         dbus_connection_set_watch_functions(conn, add_watch, remove_watch,
245                                                 watch_toggled, conn, NULL);
246
247         dbus_connection_set_timeout_functions(conn, add_timeout, remove_timeout,
248                                                 timeout_toggled, NULL, NULL);
249
250         dbus_connection_set_dispatch_status_function(conn, dispatch_status,
251                                                                 NULL, NULL);
252 }
253
254 static gboolean setup_bus(DBusConnection *conn, const char *name,
255                                                 DBusError *error)
256 {
257         gboolean result;
258         DBusDispatchStatus status;
259
260         if (name != NULL) {
261                 result = g_dbus_request_name(conn, name, error);
262
263                 if (error != NULL) {
264                         if (dbus_error_is_set(error) == TRUE)
265                                 return FALSE;
266                 }
267
268                 if (result == FALSE)
269                         return FALSE;
270         }
271
272         setup_dbus_with_main_loop(conn);
273
274         status = dbus_connection_get_dispatch_status(conn);
275         queue_dispatch(conn, status);
276
277         return TRUE;
278 }
279
280 DBusConnection *g_dbus_setup_bus(DBusBusType type, const char *name,
281                                                         DBusError *error)
282 {
283         DBusConnection *conn;
284
285         conn = dbus_bus_get(type, error);
286
287         if (error != NULL) {
288                 if (dbus_error_is_set(error) == TRUE)
289                         return NULL;
290         }
291
292         if (conn == NULL)
293                 return NULL;
294
295         if (setup_bus(conn, name, error) == FALSE) {
296                 dbus_connection_unref(conn);
297                 return NULL;
298         }
299
300         return conn;
301 }
302
303 DBusConnection *g_dbus_setup_private(DBusBusType type, const char *name,
304                                                         DBusError *error)
305 {
306         DBusConnection *conn;
307
308         conn = dbus_bus_get_private(type, error);
309
310         if (error != NULL) {
311                 if (dbus_error_is_set(error) == TRUE)
312                         return NULL;
313         }
314
315         if (conn == NULL)
316                 return NULL;
317
318         if (setup_bus(conn, name, error) == FALSE) {
319                 dbus_connection_unref(conn);
320                 return NULL;
321         }
322
323         return conn;
324 }
325
326 gboolean g_dbus_request_name(DBusConnection *connection, const char *name,
327                                                         DBusError *error)
328 {
329         int result;
330
331         result = dbus_bus_request_name(connection, name,
332                                         DBUS_NAME_FLAG_DO_NOT_QUEUE, error);
333
334         if (error != NULL) {
335                 if (dbus_error_is_set(error) == TRUE)
336                         return FALSE;
337         }
338
339         if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
340                 if (error != NULL)
341                         dbus_set_error(error, name, "Name already in use");
342
343                 return FALSE;
344         }
345
346         return TRUE;
347 }
348
349 gboolean g_dbus_set_disconnect_function(DBusConnection *connection,
350                                 GDBusWatchFunction function,
351                                 void *user_data, DBusFreeFunction destroy)
352 {
353         struct disconnect_data *dc_data;
354
355         dc_data = g_new0(struct disconnect_data, 1);
356
357         dc_data->function = function;
358         dc_data->user_data = user_data;
359
360         dbus_connection_set_exit_on_disconnect(connection, FALSE);
361
362         if (g_dbus_add_signal_watch(connection, NULL, NULL,
363                                 DBUS_INTERFACE_LOCAL, "Disconnected",
364                                 disconnected_signal, dc_data, g_free) == 0) {
365                 error("Failed to add watch for D-Bus Disconnected signal");
366                 g_free(dc_data);
367                 return FALSE;
368         }
369
370         return TRUE;
371 }