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>
7 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
11 #include "utils/includes.h"
12 #include <dbus/dbus.h>
14 #include "utils/common.h"
15 #include "utils/eloop.h"
16 #include "dbus_common.h"
17 #include "dbus_common_i.h"
25 * If we do not have SIGPOLL, try to use SIGIO instead. This is needed for
33 static void dispatch_data(DBusConnection *con)
35 while (dbus_connection_get_dispatch_status(con) ==
36 DBUS_DISPATCH_DATA_REMAINS)
37 dbus_connection_dispatch(con);
42 * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
44 * @eloop_ctx: the DBusConnection to dispatch on
45 * @timeout_ctx: unused
47 * If clients are quick to notice that service claimed its bus name,
48 * there may have been messages that came in before initialization was
49 * all finished. Dispatch those here.
51 static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
53 DBusConnection *con = eloop_ctx;
58 static void process_watch(struct wpas_dbus_priv *priv,
59 DBusWatch *watch, eloop_event_type type)
61 dbus_connection_ref(priv->con);
63 priv->should_dispatch = 0;
65 if (type == EVENT_TYPE_READ)
66 dbus_watch_handle(watch, DBUS_WATCH_READABLE);
67 else if (type == EVENT_TYPE_WRITE)
68 dbus_watch_handle(watch, DBUS_WATCH_WRITABLE);
69 else if (type == EVENT_TYPE_EXCEPTION)
70 dbus_watch_handle(watch, DBUS_WATCH_ERROR);
72 if (priv->should_dispatch) {
73 dispatch_data(priv->con);
74 priv->should_dispatch = 0;
77 dbus_connection_unref(priv->con);
81 static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
83 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
87 static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
89 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
93 static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
95 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
99 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
101 struct wpas_dbus_priv *priv = data;
105 if (!dbus_watch_get_enabled(watch))
108 flags = dbus_watch_get_flags(watch);
109 fd = dbus_watch_get_unix_fd(watch);
111 eloop_register_sock(fd, EVENT_TYPE_EXCEPTION, process_watch_exception,
114 if (flags & DBUS_WATCH_READABLE) {
115 eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
118 if (flags & DBUS_WATCH_WRITABLE) {
119 eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
123 dbus_watch_set_data(watch, priv, NULL);
129 static void remove_watch(DBusWatch *watch, void *data)
134 flags = dbus_watch_get_flags(watch);
135 fd = dbus_watch_get_unix_fd(watch);
137 eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
139 if (flags & DBUS_WATCH_READABLE)
140 eloop_unregister_sock(fd, EVENT_TYPE_READ);
141 if (flags & DBUS_WATCH_WRITABLE)
142 eloop_unregister_sock(fd, EVENT_TYPE_WRITE);
144 dbus_watch_set_data(watch, NULL, NULL);
148 static void watch_toggled(DBusWatch *watch, void *data)
150 if (dbus_watch_get_enabled(watch))
151 add_watch(watch, data);
153 remove_watch(watch, data);
157 static void process_timeout(void *eloop_ctx, void *sock_ctx)
159 DBusTimeout *timeout = sock_ctx;
160 dbus_timeout_handle(timeout);
164 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
166 struct wpas_dbus_priv *priv = data;
167 if (!dbus_timeout_get_enabled(timeout))
170 eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
171 process_timeout, priv, timeout);
173 dbus_timeout_set_data(timeout, priv, NULL);
179 static void remove_timeout(DBusTimeout *timeout, void *data)
181 struct wpas_dbus_priv *priv = data;
182 eloop_cancel_timeout(process_timeout, priv, timeout);
183 dbus_timeout_set_data(timeout, NULL, NULL);
187 static void timeout_toggled(DBusTimeout *timeout, void *data)
189 if (dbus_timeout_get_enabled(timeout))
190 add_timeout(timeout, data);
192 remove_timeout(timeout, data);
196 static void process_wakeup_main(int sig, void *signal_ctx)
198 struct wpas_dbus_priv *priv = signal_ctx;
200 if (sig != SIGPOLL || !priv->con)
203 if (dbus_connection_get_dispatch_status(priv->con) !=
204 DBUS_DISPATCH_DATA_REMAINS)
207 /* Only dispatch once - we do not want to starve other events */
208 dbus_connection_ref(priv->con);
209 dbus_connection_dispatch(priv->con);
210 dbus_connection_unref(priv->con);
215 * wakeup_main - Attempt to wake our mainloop up
216 * @data: dbus control interface private data
218 * Try to wake up the main eloop so it will process
219 * dbus events that may have happened.
221 static void wakeup_main(void *data)
223 struct wpas_dbus_priv *priv = data;
225 /* Use SIGPOLL to break out of the eloop select() */
227 priv->should_dispatch = 1;
232 * integrate_with_eloop - Register our mainloop integration with dbus
233 * @connection: connection to the system message bus
234 * @priv: a dbus control interface data structure
235 * Returns: 0 on success, -1 on failure
237 static int integrate_with_eloop(struct wpas_dbus_priv *priv)
239 if (!dbus_connection_set_watch_functions(priv->con, add_watch,
240 remove_watch, watch_toggled,
242 !dbus_connection_set_timeout_functions(priv->con, add_timeout,
244 timeout_toggled, priv,
246 wpa_printf(MSG_ERROR, "dbus: Failed to set callback "
251 if (eloop_register_signal(SIGPOLL, process_wakeup_main, priv))
253 dbus_connection_set_wakeup_main_function(priv->con, wakeup_main,
260 static int wpas_dbus_init_common(struct wpas_dbus_priv *priv)
265 /* Get a reference to the system bus */
266 dbus_error_init(&error);
267 priv->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
269 wpa_printf(MSG_ERROR, "dbus: Could not acquire the system "
270 "bus: %s - %s", error.name, error.message);
273 dbus_error_free(&error);
279 static int wpas_dbus_init_common_finish(struct wpas_dbus_priv *priv)
281 /* Tell dbus about our mainloop integration functions */
282 integrate_with_eloop(priv);
285 * Dispatch initial DBus messages that may have come in since the bus
286 * name was claimed above. Happens when clients are quick to notice the
289 * FIXME: is there a better solution to this problem?
291 eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
298 static void wpas_dbus_deinit_common(struct wpas_dbus_priv *priv)
301 eloop_cancel_timeout(dispatch_initial_dbus_messages,
303 dbus_connection_set_watch_functions(priv->con, NULL, NULL,
305 dbus_connection_set_timeout_functions(priv->con, NULL, NULL,
307 dbus_connection_unref(priv->con);
314 struct wpas_dbus_priv * wpas_dbus_init(struct wpa_global *global)
316 struct wpas_dbus_priv *priv;
318 priv = os_zalloc(sizeof(*priv));
321 priv->global = global;
323 if (wpas_dbus_init_common(priv) < 0) {
324 wpas_dbus_deinit(priv);
328 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW
329 if (wpas_dbus_ctrl_iface_init(priv) < 0) {
330 wpas_dbus_deinit(priv);
333 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
335 #ifdef CONFIG_CTRL_IFACE_DBUS
336 if (wpa_supplicant_dbus_ctrl_iface_init(priv) < 0) {
337 wpas_dbus_deinit(priv);
340 #endif /* CONFIG_CTRL_IFACE_DBUS */
342 if (wpas_dbus_init_common_finish(priv) < 0) {
343 wpas_dbus_deinit(priv);
351 void wpas_dbus_deinit(struct wpas_dbus_priv *priv)
356 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW
357 wpas_dbus_ctrl_iface_deinit(priv);
358 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
360 #ifdef CONFIG_CTRL_IFACE_DBUS
361 /* TODO: is any deinit needed? */
362 #endif /* CONFIG_CTRL_IFACE_DBUS */
364 wpas_dbus_deinit_common(priv);