3e88eac8627e4224b00790bfe61a78830e225be4
[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         DBusConnection *conn;
92
93         if (cond & G_IO_IN)  flags |= DBUS_WATCH_READABLE;
94         if (cond & G_IO_OUT) flags |= DBUS_WATCH_WRITABLE;
95         if (cond & G_IO_HUP) flags |= DBUS_WATCH_HANGUP;
96         if (cond & G_IO_ERR) flags |= DBUS_WATCH_ERROR;
97
98         /* Protect connection from being destroyed by dbus_watch_handle */
99         conn = dbus_connection_ref(info->conn);
100
101         dbus_watch_handle(info->watch, flags);
102
103         status = dbus_connection_get_dispatch_status(conn);
104         queue_dispatch(conn, status);
105
106         dbus_connection_unref(conn);
107
108         return TRUE;
109 }
110
111 static void watch_info_free(void *data)
112 {
113         struct watch_info *info = data;
114
115         if (info->id > 0) {
116                 g_source_remove(info->id);
117                 info->id = 0;
118         }
119
120         dbus_connection_unref(info->conn);
121
122         g_free(info);
123 }
124
125 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
126 {
127         DBusConnection *conn = data;
128         GIOCondition cond = G_IO_HUP | G_IO_ERR;
129         GIOChannel *chan;
130         struct watch_info *info;
131         unsigned int flags;
132         int fd;
133
134         if (!dbus_watch_get_enabled(watch))
135                 return TRUE;
136
137         info = g_new0(struct watch_info, 1);
138
139         fd = dbus_watch_get_unix_fd(watch);
140         chan = g_io_channel_unix_new(fd);
141
142         info->watch = watch;
143         info->conn = dbus_connection_ref(conn);
144
145         dbus_watch_set_data(watch, info, watch_info_free);
146
147         flags = dbus_watch_get_flags(watch);
148
149         if (flags & DBUS_WATCH_READABLE) cond |= G_IO_IN;
150         if (flags & DBUS_WATCH_WRITABLE) cond |= G_IO_OUT;
151
152         info->id = g_io_add_watch(chan, cond, watch_func, info);
153
154         g_io_channel_unref(chan);
155
156         return TRUE;
157 }
158
159 static void remove_watch(DBusWatch *watch, void *data)
160 {
161         if (dbus_watch_get_enabled(watch))
162                 return;
163
164         /* will trigger watch_info_free() */
165         dbus_watch_set_data(watch, NULL, NULL);
166 }
167
168 static void watch_toggled(DBusWatch *watch, void *data)
169 {
170         /* Because we just exit on OOM, enable/disable is
171          * no different from add/remove */
172         if (dbus_watch_get_enabled(watch))
173                 add_watch(watch, data);
174         else
175                 remove_watch(watch, data);
176 }
177
178 static gboolean timeout_handler_dispatch(gpointer data)
179 {
180         struct timeout_handler *handler = data;
181
182         handler->id = 0;
183
184         /* if not enabled should not be polled by the main loop */
185         if (!dbus_timeout_get_enabled(handler->timeout))
186                 return FALSE;
187
188         dbus_timeout_handle(handler->timeout);
189
190         return FALSE;
191 }
192
193 static void timeout_handler_free(void *data)
194 {
195         struct timeout_handler *handler = data;
196
197         if (handler->id > 0) {
198                 g_source_remove(handler->id);
199                 handler->id = 0;
200         }
201
202         g_free(handler);
203 }
204
205 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
206 {
207         int interval = dbus_timeout_get_interval(timeout);
208         struct timeout_handler *handler;
209
210         if (!dbus_timeout_get_enabled(timeout))
211                 return TRUE;
212
213         handler = g_new0(struct timeout_handler, 1);
214
215         handler->timeout = timeout;
216
217         dbus_timeout_set_data(timeout, handler, timeout_handler_free);
218
219         handler->id = g_timeout_add(interval, timeout_handler_dispatch,
220                                                                 handler);
221
222         return TRUE;
223 }
224
225 static void remove_timeout(DBusTimeout *timeout, void *data)
226 {
227         /* will trigger timeout_handler_free() */
228         dbus_timeout_set_data(timeout, NULL, NULL);
229 }
230
231 static void timeout_toggled(DBusTimeout *timeout, void *data)
232 {
233         if (dbus_timeout_get_enabled(timeout))
234                 add_timeout(timeout, data);
235         else
236                 remove_timeout(timeout, data);
237 }
238
239 static void dispatch_status(DBusConnection *conn,
240                                         DBusDispatchStatus status, void *data)
241 {
242         if (!dbus_connection_get_is_connected(conn))
243                 return;
244
245         queue_dispatch(conn, status);
246 }
247
248 static inline void setup_dbus_with_main_loop(DBusConnection *conn)
249 {
250         dbus_connection_set_watch_functions(conn, add_watch, remove_watch,
251                                                 watch_toggled, conn, NULL);
252
253         dbus_connection_set_timeout_functions(conn, add_timeout, remove_timeout,
254                                                 timeout_toggled, NULL, NULL);
255
256         dbus_connection_set_dispatch_status_function(conn, dispatch_status,
257                                                                 NULL, NULL);
258 }
259
260 static gboolean setup_bus(DBusConnection *conn, const char *name,
261                                                 DBusError *error)
262 {
263         gboolean result;
264         DBusDispatchStatus status;
265
266         if (name != NULL) {
267                 result = g_dbus_request_name(conn, name, error);
268
269                 if (error != NULL) {
270                         if (dbus_error_is_set(error) == TRUE)
271                                 return FALSE;
272                 }
273
274                 if (result == FALSE)
275                         return FALSE;
276         }
277
278         setup_dbus_with_main_loop(conn);
279
280         status = dbus_connection_get_dispatch_status(conn);
281         queue_dispatch(conn, status);
282
283         return TRUE;
284 }
285
286 DBusConnection *g_dbus_setup_bus(DBusBusType type, const char *name,
287                                                         DBusError *error)
288 {
289         DBusConnection *conn;
290
291         conn = dbus_bus_get(type, error);
292
293         if (error != NULL) {
294                 if (dbus_error_is_set(error) == TRUE)
295                         return NULL;
296         }
297
298         if (conn == NULL)
299                 return NULL;
300
301         if (setup_bus(conn, name, error) == FALSE) {
302                 dbus_connection_unref(conn);
303                 return NULL;
304         }
305
306         return conn;
307 }
308
309 DBusConnection *g_dbus_setup_private(DBusBusType type, const char *name,
310                                                         DBusError *error)
311 {
312         DBusConnection *conn;
313
314         conn = dbus_bus_get_private(type, error);
315
316         if (error != NULL) {
317                 if (dbus_error_is_set(error) == TRUE)
318                         return NULL;
319         }
320
321         if (conn == NULL)
322                 return NULL;
323
324         if (setup_bus(conn, name, error) == FALSE) {
325                 dbus_connection_unref(conn);
326                 return NULL;
327         }
328
329         return conn;
330 }
331
332 gboolean g_dbus_request_name(DBusConnection *connection, const char *name,
333                                                         DBusError *error)
334 {
335         int result;
336
337         result = dbus_bus_request_name(connection, name,
338                                         DBUS_NAME_FLAG_DO_NOT_QUEUE, error);
339
340         if (error != NULL) {
341                 if (dbus_error_is_set(error) == TRUE)
342                         return FALSE;
343         }
344
345         if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
346                 if (error != NULL)
347                         dbus_set_error(error, name, "Name already in use");
348
349                 return FALSE;
350         }
351
352         return TRUE;
353 }
354
355 gboolean g_dbus_set_disconnect_function(DBusConnection *connection,
356                                 GDBusWatchFunction function,
357                                 void *user_data, DBusFreeFunction destroy)
358 {
359         struct disconnect_data *dc_data;
360
361         dc_data = g_new0(struct disconnect_data, 1);
362
363         dc_data->function = function;
364         dc_data->user_data = user_data;
365
366         dbus_connection_set_exit_on_disconnect(connection, FALSE);
367
368         if (g_dbus_add_signal_watch(connection, NULL, NULL,
369                                 DBUS_INTERFACE_LOCAL, "Disconnected",
370                                 disconnected_signal, dc_data, g_free) == 0) {
371                 error("Failed to add watch for D-Bus Disconnected signal");
372                 g_free(dc_data);
373                 return FALSE;
374         }
375
376         return TRUE;
377 }