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 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.
11 * Alternatively, this software may be distributed under the terms of BSD
14 * See README and COPYING for more details.
17 #include "utils/includes.h"
18 #include <dbus/dbus.h>
20 #include "utils/common.h"
21 #include "utils/eloop.h"
22 #include "dbus_common.h"
23 #include "dbus_common_i.h"
31 * If we do not have SIGPOLL, try to use SIGIO instead. This is needed for
40 * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
42 * @eloop_ctx: the DBusConnection to dispatch on
43 * @timeout_ctx: unused
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.
49 static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
51 DBusConnection *con = eloop_ctx;
53 while (dbus_connection_get_dispatch_status(con) ==
54 DBUS_DISPATCH_DATA_REMAINS)
55 dbus_connection_dispatch(con);
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 while (dbus_connection_get_dispatch_status(priv->con) ==
75 DBUS_DISPATCH_DATA_REMAINS)
76 dbus_connection_dispatch(priv->con);
77 priv->should_dispatch = 0;
80 dbus_connection_unref(priv->con);
84 static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
86 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
90 static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
92 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
96 static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
98 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
102 static void connection_setup_add_watch(struct wpas_dbus_priv *priv,
108 if (!dbus_watch_get_enabled(watch))
111 flags = dbus_watch_get_flags(watch);
112 fd = dbus_watch_get_unix_fd(watch);
114 eloop_register_sock(fd, EVENT_TYPE_EXCEPTION, process_watch_exception,
117 if (flags & DBUS_WATCH_READABLE) {
118 eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
121 if (flags & DBUS_WATCH_WRITABLE) {
122 eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
126 dbus_watch_set_data(watch, priv, NULL);
130 static void connection_setup_remove_watch(struct wpas_dbus_priv *priv,
136 flags = dbus_watch_get_flags(watch);
137 fd = dbus_watch_get_unix_fd(watch);
139 eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
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);
146 dbus_watch_set_data(watch, NULL, NULL);
150 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
152 connection_setup_add_watch(data, watch);
157 static void remove_watch(DBusWatch *watch, void *data)
159 connection_setup_remove_watch(data, watch);
163 static void watch_toggled(DBusWatch *watch, void *data)
165 if (dbus_watch_get_enabled(watch))
166 add_watch(watch, data);
168 remove_watch(watch, data);
172 static void process_timeout(void *eloop_ctx, void *sock_ctx)
174 DBusTimeout *timeout = sock_ctx;
176 dbus_timeout_handle(timeout);
180 static void connection_setup_add_timeout(struct wpas_dbus_priv *priv,
181 DBusTimeout *timeout)
183 if (!dbus_timeout_get_enabled(timeout))
186 eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
187 process_timeout, priv, timeout);
189 dbus_timeout_set_data(timeout, priv, NULL);
193 static void connection_setup_remove_timeout(struct wpas_dbus_priv *priv,
194 DBusTimeout *timeout)
196 eloop_cancel_timeout(process_timeout, priv, timeout);
197 dbus_timeout_set_data(timeout, NULL, NULL);
201 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
203 if (!dbus_timeout_get_enabled(timeout))
206 connection_setup_add_timeout(data, timeout);
212 static void remove_timeout(DBusTimeout *timeout, void *data)
214 connection_setup_remove_timeout(data, timeout);
218 static void timeout_toggled(DBusTimeout *timeout, void *data)
220 if (dbus_timeout_get_enabled(timeout))
221 add_timeout(timeout, data);
223 remove_timeout(timeout, data);
227 static void process_wakeup_main(int sig, void *signal_ctx)
229 struct wpas_dbus_priv *priv = signal_ctx;
231 if (sig != SIGPOLL || !priv->con)
234 if (dbus_connection_get_dispatch_status(priv->con) !=
235 DBUS_DISPATCH_DATA_REMAINS)
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);
246 * wakeup_main - Attempt to wake our mainloop up
247 * @data: dbus control interface private data
249 * Try to wake up the main eloop so it will process
250 * dbus events that may have happened.
252 static void wakeup_main(void *data)
254 struct wpas_dbus_priv *priv = data;
256 /* Use SIGPOLL to break out of the eloop select() */
258 priv->should_dispatch = 1;
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
267 * Register our wakeup_main handler with dbus
269 static int connection_setup_wakeup_main(struct wpas_dbus_priv *priv)
271 if (eloop_register_signal(SIGPOLL, process_wakeup_main, priv))
274 dbus_connection_set_wakeup_main_function(priv->con, wakeup_main,
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
287 * We register our mainloop integration functions with dbus here.
289 static int integrate_with_eloop(struct wpas_dbus_priv *priv)
291 if (!dbus_connection_set_watch_functions(priv->con, add_watch,
292 remove_watch, watch_toggled,
294 wpa_printf(MSG_ERROR, "dbus: Not enough memory to set up");
298 if (!dbus_connection_set_timeout_functions(priv->con, add_timeout,
300 timeout_toggled, priv,
302 wpa_printf(MSG_ERROR, "dbus: Not enough memory to set up");
306 if (connection_setup_wakeup_main(priv) < 0) {
307 wpa_printf(MSG_ERROR, "dbus: Could not setup main wakeup "
316 static int wpas_dbus_init_common(struct wpas_dbus_priv *priv)
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);
325 wpa_printf(MSG_ERROR, "dbus: Could not acquire the system "
326 "bus: %s", strerror(errno));
334 static int wpas_dbus_init_common_finish(struct wpas_dbus_priv *priv)
336 /* Tell dbus about our mainloop integration functions */
337 integrate_with_eloop(priv);
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
344 * FIXME: is there a better solution to this problem?
346 eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
353 static void wpas_dbus_deinit_common(struct wpas_dbus_priv *priv)
356 eloop_cancel_timeout(dispatch_initial_dbus_messages,
358 dbus_connection_set_watch_functions(priv->con, NULL, NULL,
360 dbus_connection_set_timeout_functions(priv->con, NULL, NULL,
362 dbus_connection_unref(priv->con);
369 struct wpas_dbus_priv * wpas_dbus_init(struct wpa_global *global)
371 struct wpas_dbus_priv *priv;
373 priv = os_zalloc(sizeof(*priv));
376 priv->global = global;
378 if (wpas_dbus_init_common(priv) < 0) {
379 wpas_dbus_deinit(priv);
383 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW
384 if (wpas_dbus_ctrl_iface_init(priv) < 0) {
385 wpas_dbus_deinit(priv);
388 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
390 #ifdef CONFIG_CTRL_IFACE_DBUS
391 if (wpa_supplicant_dbus_ctrl_iface_init(priv) < 0) {
392 wpas_dbus_deinit(priv);
395 #endif /* CONFIG_CTRL_IFACE_DBUS */
397 if (wpas_dbus_init_common_finish(priv) < 0) {
398 wpas_dbus_deinit(priv);
406 void wpas_dbus_deinit(struct wpas_dbus_priv *priv)
411 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW
412 wpas_dbus_ctrl_iface_deinit(priv);
413 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
415 #ifdef CONFIG_CTRL_IFACE_DBUS
416 /* TODO: is any deinit needed? */
417 #endif /* CONFIG_CTRL_IFACE_DBUS */
419 wpas_dbus_deinit_common(priv);