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"
20 #include "../wpa_supplicant_i.h"
26 * If we do not have SIGPOLL, try to use SIGIO instead. This is needed for
34 static void dispatch_data(DBusConnection *con)
36 while (dbus_connection_get_dispatch_status(con) ==
37 DBUS_DISPATCH_DATA_REMAINS)
38 dbus_connection_dispatch(con);
43 * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
45 * @eloop_ctx: the DBusConnection to dispatch on
46 * @timeout_ctx: unused
48 * If clients are quick to notice that service claimed its bus name,
49 * there may have been messages that came in before initialization was
50 * all finished. Dispatch those here.
52 static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
54 DBusConnection *con = eloop_ctx;
59 static void process_watch(struct wpas_dbus_priv *priv,
60 DBusWatch *watch, eloop_event_type type)
62 dbus_connection_ref(priv->con);
64 priv->should_dispatch = 0;
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);
73 if (priv->should_dispatch) {
74 dispatch_data(priv->con);
75 priv->should_dispatch = 0;
78 dbus_connection_unref(priv->con);
82 static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
84 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
88 static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
90 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
94 static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
96 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
100 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
102 struct wpas_dbus_priv *priv = data;
106 if (!dbus_watch_get_enabled(watch))
109 flags = dbus_watch_get_flags(watch);
110 fd = dbus_watch_get_unix_fd(watch);
112 eloop_register_sock(fd, EVENT_TYPE_EXCEPTION, process_watch_exception,
115 if (flags & DBUS_WATCH_READABLE) {
116 eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
119 if (flags & DBUS_WATCH_WRITABLE) {
120 eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
124 dbus_watch_set_data(watch, priv, NULL);
130 static void remove_watch(DBusWatch *watch, void *data)
135 flags = dbus_watch_get_flags(watch);
136 fd = dbus_watch_get_unix_fd(watch);
138 eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
140 if (flags & DBUS_WATCH_READABLE)
141 eloop_unregister_sock(fd, EVENT_TYPE_READ);
142 if (flags & DBUS_WATCH_WRITABLE)
143 eloop_unregister_sock(fd, EVENT_TYPE_WRITE);
145 dbus_watch_set_data(watch, NULL, NULL);
149 static void watch_toggled(DBusWatch *watch, void *data)
151 if (dbus_watch_get_enabled(watch))
152 add_watch(watch, data);
154 remove_watch(watch, data);
158 static void process_timeout(void *eloop_ctx, void *sock_ctx)
160 DBusTimeout *timeout = sock_ctx;
161 dbus_timeout_handle(timeout);
165 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
167 struct wpas_dbus_priv *priv = data;
169 if (!dbus_timeout_get_enabled(timeout))
172 eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
173 process_timeout, priv, timeout);
175 dbus_timeout_set_data(timeout, priv, NULL);
181 static void remove_timeout(DBusTimeout *timeout, void *data)
183 struct wpas_dbus_priv *priv = data;
185 eloop_cancel_timeout(process_timeout, priv, timeout);
186 dbus_timeout_set_data(timeout, NULL, NULL);
190 static void timeout_toggled(DBusTimeout *timeout, void *data)
192 if (dbus_timeout_get_enabled(timeout))
193 add_timeout(timeout, data);
195 remove_timeout(timeout, data);
199 static void process_wakeup_main(int sig, void *signal_ctx)
201 struct wpas_dbus_priv *priv = signal_ctx;
203 if (sig != SIGPOLL || !priv->con)
206 if (dbus_connection_get_dispatch_status(priv->con) !=
207 DBUS_DISPATCH_DATA_REMAINS)
210 /* Only dispatch once - we do not want to starve other events */
211 dbus_connection_ref(priv->con);
212 dbus_connection_dispatch(priv->con);
213 dbus_connection_unref(priv->con);
218 * wakeup_main - Attempt to wake our mainloop up
219 * @data: dbus control interface private data
221 * Try to wake up the main eloop so it will process
222 * dbus events that may have happened.
224 static void wakeup_main(void *data)
226 struct wpas_dbus_priv *priv = data;
228 /* Use SIGPOLL to break out of the eloop select() */
230 priv->should_dispatch = 1;
235 * integrate_with_eloop - Register our mainloop integration with dbus
236 * @connection: connection to the system message bus
237 * @priv: a dbus control interface data structure
238 * Returns: 0 on success, -1 on failure
240 static int integrate_with_eloop(struct wpas_dbus_priv *priv)
242 if (!dbus_connection_set_watch_functions(priv->con, add_watch,
243 remove_watch, watch_toggled,
245 !dbus_connection_set_timeout_functions(priv->con, add_timeout,
247 timeout_toggled, priv,
249 wpa_printf(MSG_ERROR, "dbus: Failed to set callback functions");
253 if (eloop_register_signal(SIGPOLL, process_wakeup_main, priv))
255 dbus_connection_set_wakeup_main_function(priv->con, wakeup_main,
262 static DBusHandlerResult disconnect_filter(DBusConnection *conn,
263 DBusMessage *message, void *data)
265 struct wpas_dbus_priv *priv = data;
267 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL,
269 wpa_printf(MSG_DEBUG, "dbus: bus disconnected, terminating");
270 dbus_connection_set_exit_on_disconnect(conn, FALSE);
271 wpa_supplicant_terminate_proc(priv->global);
272 return DBUS_HANDLER_RESULT_HANDLED;
274 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
278 static int wpas_dbus_init_common(struct wpas_dbus_priv *priv)
283 /* Get a reference to the system bus */
284 dbus_error_init(&error);
285 priv->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
287 dbus_connection_add_filter(priv->con, disconnect_filter, priv,
290 wpa_printf(MSG_ERROR,
291 "dbus: Could not acquire the system bus: %s - %s",
292 error.name, error.message);
295 dbus_error_free(&error);
301 static int wpas_dbus_init_common_finish(struct wpas_dbus_priv *priv)
303 /* Tell dbus about our mainloop integration functions */
304 integrate_with_eloop(priv);
307 * Dispatch initial DBus messages that may have come in since the bus
308 * name was claimed above. Happens when clients are quick to notice the
311 * FIXME: is there a better solution to this problem?
313 eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
320 static void wpas_dbus_deinit_common(struct wpas_dbus_priv *priv)
323 eloop_cancel_timeout(dispatch_initial_dbus_messages,
325 eloop_cancel_timeout(process_timeout, priv, ELOOP_ALL_CTX);
327 dbus_connection_set_watch_functions(priv->con, NULL, NULL,
329 dbus_connection_set_timeout_functions(priv->con, NULL, NULL,
331 dbus_connection_remove_filter(priv->con, disconnect_filter,
334 dbus_connection_unref(priv->con);
341 struct wpas_dbus_priv * wpas_dbus_init(struct wpa_global *global)
343 struct wpas_dbus_priv *priv;
345 priv = os_zalloc(sizeof(*priv));
348 priv->global = global;
350 if (wpas_dbus_init_common(priv) < 0 ||
351 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW
352 wpas_dbus_ctrl_iface_init(priv) < 0 ||
353 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
354 #ifdef CONFIG_CTRL_IFACE_DBUS
355 wpa_supplicant_dbus_ctrl_iface_init(priv) < 0 ||
356 #endif /* CONFIG_CTRL_IFACE_DBUS */
357 wpas_dbus_init_common_finish(priv) < 0) {
358 wpas_dbus_deinit(priv);
366 void wpas_dbus_deinit(struct wpas_dbus_priv *priv)
371 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW
372 wpas_dbus_ctrl_iface_deinit(priv);
373 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
375 #ifdef CONFIG_CTRL_IFACE_DBUS
376 /* TODO: is any deinit needed? */
377 #endif /* CONFIG_CTRL_IFACE_DBUS */
379 wpas_dbus_deinit_common(priv);