dbus: Remove perror() calls
[mech_eap.git] / wpa_supplicant / dbus / dbus_common.c
1 /*
2  * wpa_supplicant D-Bus control interface - common functionality
3  * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
4  * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
5  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * Alternatively, this software may be distributed under the terms of BSD
12  * license.
13  *
14  * See README and COPYING for more details.
15  */
16
17 #include "utils/includes.h"
18 #include <dbus/dbus.h>
19
20 #include "utils/common.h"
21 #include "utils/eloop.h"
22 #include "dbus_common.h"
23 #include "dbus_common_i.h"
24 #include "dbus_new.h"
25 #include "dbus_old.h"
26
27
28 #ifndef SIGPOLL
29 #ifdef SIGIO
30 /*
31  * If we do not have SIGPOLL, try to use SIGIO instead. This is needed for
32  * FreeBSD.
33  */
34 #define SIGPOLL SIGIO
35 #endif
36 #endif
37
38
39 /**
40  * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
41  *     claiming bus name
42  * @eloop_ctx: the DBusConnection to dispatch on
43  * @timeout_ctx: unused
44  *
45  * If clients are quick to notice that service claimed its bus name,
46  * there may have been messages that came in before initialization was
47  * all finished.  Dispatch those here.
48  */
49 static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
50 {
51         DBusConnection *con = eloop_ctx;
52
53         while (dbus_connection_get_dispatch_status(con) ==
54                DBUS_DISPATCH_DATA_REMAINS)
55                 dbus_connection_dispatch(con);
56 }
57
58
59 static void process_watch(struct wpas_dbus_priv *priv,
60                           DBusWatch *watch, eloop_event_type type)
61 {
62         dbus_connection_ref(priv->con);
63
64         priv->should_dispatch = 0;
65
66         if (type == EVENT_TYPE_READ)
67                 dbus_watch_handle(watch, DBUS_WATCH_READABLE);
68         else if (type == EVENT_TYPE_WRITE)
69                 dbus_watch_handle(watch, DBUS_WATCH_WRITABLE);
70         else if (type == EVENT_TYPE_EXCEPTION)
71                 dbus_watch_handle(watch, DBUS_WATCH_ERROR);
72
73         if (priv->should_dispatch) {
74                 while (dbus_connection_get_dispatch_status(priv->con) ==
75                        DBUS_DISPATCH_DATA_REMAINS)
76                         dbus_connection_dispatch(priv->con);
77                 priv->should_dispatch = 0;
78         }
79
80         dbus_connection_unref(priv->con);
81 }
82
83
84 static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
85 {
86         process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
87 }
88
89
90 static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
91 {
92         process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
93 }
94
95
96 static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
97 {
98         process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
99 }
100
101
102 static void connection_setup_add_watch(struct wpas_dbus_priv *priv,
103                                        DBusWatch *watch)
104 {
105         unsigned int flags;
106         int fd;
107
108         if (!dbus_watch_get_enabled(watch))
109                 return;
110
111         flags = dbus_watch_get_flags(watch);
112         fd = dbus_watch_get_unix_fd(watch);
113
114         eloop_register_sock(fd, EVENT_TYPE_EXCEPTION, process_watch_exception,
115                             priv, watch);
116
117         if (flags & DBUS_WATCH_READABLE) {
118                 eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
119                                     priv, watch);
120         }
121         if (flags & DBUS_WATCH_WRITABLE) {
122                 eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
123                                     priv, watch);
124         }
125
126         dbus_watch_set_data(watch, priv, NULL);
127 }
128
129
130 static void connection_setup_remove_watch(struct wpas_dbus_priv *priv,
131                                           DBusWatch *watch)
132 {
133         unsigned int flags;
134         int fd;
135
136         flags = dbus_watch_get_flags(watch);
137         fd = dbus_watch_get_unix_fd(watch);
138
139         eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
140
141         if (flags & DBUS_WATCH_READABLE)
142                 eloop_unregister_sock(fd, EVENT_TYPE_READ);
143         if (flags & DBUS_WATCH_WRITABLE)
144                 eloop_unregister_sock(fd, EVENT_TYPE_WRITE);
145
146         dbus_watch_set_data(watch, NULL, NULL);
147 }
148
149
150 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
151 {
152         connection_setup_add_watch(data, watch);
153         return TRUE;
154 }
155
156
157 static void remove_watch(DBusWatch *watch, void *data)
158 {
159         connection_setup_remove_watch(data, watch);
160 }
161
162
163 static void watch_toggled(DBusWatch *watch, void *data)
164 {
165         if (dbus_watch_get_enabled(watch))
166                 add_watch(watch, data);
167         else
168                 remove_watch(watch, data);
169 }
170
171
172 static void process_timeout(void *eloop_ctx, void *sock_ctx)
173 {
174         DBusTimeout *timeout = sock_ctx;
175
176         dbus_timeout_handle(timeout);
177 }
178
179
180 static void connection_setup_add_timeout(struct wpas_dbus_priv *priv,
181                                          DBusTimeout *timeout)
182 {
183         if (!dbus_timeout_get_enabled(timeout))
184                 return;
185
186         eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
187                                process_timeout, priv, timeout);
188
189         dbus_timeout_set_data(timeout, priv, NULL);
190 }
191
192
193 static void connection_setup_remove_timeout(struct wpas_dbus_priv *priv,
194                                             DBusTimeout *timeout)
195 {
196         eloop_cancel_timeout(process_timeout, priv, timeout);
197         dbus_timeout_set_data(timeout, NULL, NULL);
198 }
199
200
201 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
202 {
203         if (!dbus_timeout_get_enabled(timeout))
204                 return TRUE;
205
206         connection_setup_add_timeout(data, timeout);
207
208         return TRUE;
209 }
210
211
212 static void remove_timeout(DBusTimeout *timeout, void *data)
213 {
214         connection_setup_remove_timeout(data, timeout);
215 }
216
217
218 static void timeout_toggled(DBusTimeout *timeout, void *data)
219 {
220         if (dbus_timeout_get_enabled(timeout))
221                 add_timeout(timeout, data);
222         else
223                 remove_timeout(timeout, data);
224 }
225
226
227 static void process_wakeup_main(int sig, void *signal_ctx)
228 {
229         struct wpas_dbus_priv *priv = signal_ctx;
230
231         if (sig != SIGPOLL || !priv->con)
232                 return;
233
234         if (dbus_connection_get_dispatch_status(priv->con) !=
235             DBUS_DISPATCH_DATA_REMAINS)
236                 return;
237
238         /* Only dispatch once - we do not want to starve other events */
239         dbus_connection_ref(priv->con);
240         dbus_connection_dispatch(priv->con);
241         dbus_connection_unref(priv->con);
242 }
243
244
245 /**
246  * wakeup_main - Attempt to wake our mainloop up
247  * @data: dbus control interface private data
248  *
249  * Try to wake up the main eloop so it will process
250  * dbus events that may have happened.
251  */
252 static void wakeup_main(void *data)
253 {
254         struct wpas_dbus_priv *priv = data;
255
256         /* Use SIGPOLL to break out of the eloop select() */
257         raise(SIGPOLL);
258         priv->should_dispatch = 1;
259 }
260
261
262 /**
263  * connection_setup_wakeup_main - Tell dbus about our wakeup_main function
264  * @priv: dbus control interface private data
265  * Returns: 0 on success, -1 on failure
266  *
267  * Register our wakeup_main handler with dbus
268  */
269 static int connection_setup_wakeup_main(struct wpas_dbus_priv *priv)
270 {
271         if (eloop_register_signal(SIGPOLL, process_wakeup_main, priv))
272                 return -1;
273
274         dbus_connection_set_wakeup_main_function(priv->con, wakeup_main,
275                                                  priv, NULL);
276
277         return 0;
278 }
279
280
281 /**
282  * integrate_with_eloop - Register our mainloop integration with dbus
283  * @connection: connection to the system message bus
284  * @priv: a dbus control interface data structure
285  * Returns: 0 on success, -1 on failure
286  *
287  * We register our mainloop integration functions with dbus here.
288  */
289 static int integrate_with_eloop(struct wpas_dbus_priv *priv)
290 {
291         if (!dbus_connection_set_watch_functions(priv->con, add_watch,
292                                                  remove_watch, watch_toggled,
293                                                  priv, NULL)) {
294                 wpa_printf(MSG_ERROR, "dbus: Not enough memory to set up");
295                 return -1;
296         }
297
298         if (!dbus_connection_set_timeout_functions(priv->con, add_timeout,
299                                                    remove_timeout,
300                                                    timeout_toggled, priv,
301                                                    NULL)) {
302                 wpa_printf(MSG_ERROR, "dbus: Not enough memory to set up");
303                 return -1;
304         }
305
306         if (connection_setup_wakeup_main(priv) < 0) {
307                 wpa_printf(MSG_ERROR, "dbus: Could not setup main wakeup "
308                            "function");
309                 return -1;
310         }
311
312         return 0;
313 }
314
315
316 static int wpas_dbus_init_common(struct wpas_dbus_priv *priv)
317 {
318         DBusError error;
319
320         /* Get a reference to the system bus */
321         dbus_error_init(&error);
322         priv->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
323         dbus_error_free(&error);
324         if (!priv->con) {
325                 wpa_printf(MSG_ERROR, "dbus: Could not acquire the system "
326                            "bus: %s", strerror(errno));
327                 return -1;
328         }
329
330         return 0;
331 }
332
333
334 static int wpas_dbus_init_common_finish(struct wpas_dbus_priv *priv)
335 {
336         /* Tell dbus about our mainloop integration functions */
337         integrate_with_eloop(priv);
338
339         /*
340          * Dispatch initial DBus messages that may have come in since the bus
341          * name was claimed above. Happens when clients are quick to notice the
342          * service.
343          *
344          * FIXME: is there a better solution to this problem?
345          */
346         eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
347                                priv->con, NULL);
348
349         return 0;
350 }
351
352
353 static void wpas_dbus_deinit_common(struct wpas_dbus_priv *priv)
354 {
355         if (priv->con) {
356                 eloop_cancel_timeout(dispatch_initial_dbus_messages,
357                                      priv->con, NULL);
358                 dbus_connection_set_watch_functions(priv->con, NULL, NULL,
359                                                     NULL, NULL, NULL);
360                 dbus_connection_set_timeout_functions(priv->con, NULL, NULL,
361                                                       NULL, NULL, NULL);
362                 dbus_connection_unref(priv->con);
363         }
364
365         os_free(priv);
366 }
367
368
369 struct wpas_dbus_priv * wpas_dbus_init(struct wpa_global *global)
370 {
371         struct wpas_dbus_priv *priv;
372
373         priv = os_zalloc(sizeof(*priv));
374         if (priv == NULL)
375                 return NULL;
376         priv->global = global;
377
378         if (wpas_dbus_init_common(priv) < 0) {
379                 wpas_dbus_deinit(priv);
380                 return NULL;
381         }
382
383 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW
384         if (wpas_dbus_ctrl_iface_init(priv) < 0) {
385                 wpas_dbus_deinit(priv);
386                 return NULL;
387         }
388 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
389
390 #ifdef CONFIG_CTRL_IFACE_DBUS
391         if (wpa_supplicant_dbus_ctrl_iface_init(priv) < 0) {
392                 wpas_dbus_deinit(priv);
393                 return NULL;
394         }
395 #endif /* CONFIG_CTRL_IFACE_DBUS */
396
397         if (wpas_dbus_init_common_finish(priv) < 0) {
398                 wpas_dbus_deinit(priv);
399                 return NULL;
400         }
401
402         return priv;
403 }
404
405
406 void wpas_dbus_deinit(struct wpas_dbus_priv *priv)
407 {
408         if (priv == NULL)
409                 return;
410
411 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW
412         wpas_dbus_ctrl_iface_deinit(priv);
413 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
414
415 #ifdef CONFIG_CTRL_IFACE_DBUS
416         /* TODO: is any deinit needed? */
417 #endif /* CONFIG_CTRL_IFACE_DBUS */
418
419         wpas_dbus_deinit_common(priv);
420 }