Remove disconnected APs from BSS table if likely out-of-range
[mech_eap.git] / wpa_supplicant / dbus / dbus_common.c
index 77319f3..7ef6cad 100644 (file)
@@ -4,14 +4,8 @@
  * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  */
 
 #include "utils/includes.h"
 #include "dbus_common.h"
 #include "dbus_common_i.h"
 #include "dbus_new.h"
-#include "dbus.h"
+#include "dbus_old.h"
+#include "../wpa_supplicant_i.h"
+
+
+#ifndef SIGPOLL
+#ifdef SIGIO
+/*
+ * If we do not have SIGPOLL, try to use SIGIO instead. This is needed for
+ * FreeBSD.
+ */
+#define SIGPOLL SIGIO
+#endif
+#endif
+
+
+static void dispatch_data(DBusConnection *con)
+{
+       while (dbus_connection_get_dispatch_status(con) ==
+              DBUS_DISPATCH_DATA_REMAINS)
+               dbus_connection_dispatch(con);
+}
 
 
 /**
 static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
 {
        DBusConnection *con = eloop_ctx;
-
-       while (dbus_connection_get_dispatch_status(con) ==
-              DBUS_DISPATCH_DATA_REMAINS)
-               dbus_connection_dispatch(con);
+       dispatch_data(con);
 }
 
 
@@ -60,9 +71,7 @@ static void process_watch(struct wpas_dbus_priv *priv,
                dbus_watch_handle(watch, DBUS_WATCH_ERROR);
 
        if (priv->should_dispatch) {
-               while (dbus_connection_get_dispatch_status(priv->con) ==
-                      DBUS_DISPATCH_DATA_REMAINS)
-                       dbus_connection_dispatch(priv->con);
+               dispatch_data(priv->con);
                priv->should_dispatch = 0;
        }
 
@@ -88,14 +97,14 @@ static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
 }
 
 
-static void connection_setup_add_watch(struct wpas_dbus_priv *priv,
-                                      DBusWatch *watch)
+static dbus_bool_t add_watch(DBusWatch *watch, void *data)
 {
+       struct wpas_dbus_priv *priv = data;
        unsigned int flags;
        int fd;
 
        if (!dbus_watch_get_enabled(watch))
-               return;
+               return TRUE;
 
        flags = dbus_watch_get_flags(watch);
        fd = dbus_watch_get_unix_fd(watch);
@@ -113,11 +122,12 @@ static void connection_setup_add_watch(struct wpas_dbus_priv *priv,
        }
 
        dbus_watch_set_data(watch, priv, NULL);
+
+       return TRUE;
 }
 
 
-static void connection_setup_remove_watch(struct wpas_dbus_priv *priv,
-                                         DBusWatch *watch)
+static void remove_watch(DBusWatch *watch, void *data)
 {
        unsigned int flags;
        int fd;
@@ -136,19 +146,6 @@ static void connection_setup_remove_watch(struct wpas_dbus_priv *priv,
 }
 
 
-static dbus_bool_t add_watch(DBusWatch *watch, void *data)
-{
-       connection_setup_add_watch(data, watch);
-       return TRUE;
-}
-
-
-static void remove_watch(DBusWatch *watch, void *data)
-{
-       connection_setup_remove_watch(data, watch);
-}
-
-
 static void watch_toggled(DBusWatch *watch, void *data)
 {
        if (dbus_watch_get_enabled(watch))
@@ -161,38 +158,21 @@ static void watch_toggled(DBusWatch *watch, void *data)
 static void process_timeout(void *eloop_ctx, void *sock_ctx)
 {
        DBusTimeout *timeout = sock_ctx;
-
        dbus_timeout_handle(timeout);
 }
 
 
-static void connection_setup_add_timeout(struct wpas_dbus_priv *priv,
-                                        DBusTimeout *timeout)
+static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
 {
+       struct wpas_dbus_priv *priv = data;
+
        if (!dbus_timeout_get_enabled(timeout))
-               return;
+               return TRUE;
 
        eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
                               process_timeout, priv, timeout);
 
        dbus_timeout_set_data(timeout, priv, NULL);
-}
-
-
-static void connection_setup_remove_timeout(struct wpas_dbus_priv *priv,
-                                           DBusTimeout *timeout)
-{
-       eloop_cancel_timeout(process_timeout, priv, timeout);
-       dbus_timeout_set_data(timeout, NULL, NULL);
-}
-
-
-static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
-{
-       if (!dbus_timeout_get_enabled(timeout))
-               return TRUE;
-
-       connection_setup_add_timeout(data, timeout);
 
        return TRUE;
 }
@@ -200,7 +180,10 @@ static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
 
 static void remove_timeout(DBusTimeout *timeout, void *data)
 {
-       connection_setup_remove_timeout(data, timeout);
+       struct wpas_dbus_priv *priv = data;
+
+       eloop_cancel_timeout(process_timeout, priv, timeout);
+       dbus_timeout_set_data(timeout, NULL, NULL);
 }
 
 
@@ -249,76 +232,69 @@ static void wakeup_main(void *data)
 
 
 /**
- * connection_setup_wakeup_main - Tell dbus about our wakeup_main function
- * @priv: dbus control interface private data
- * Returns: 0 on success, -1 on failure
- *
- * Register our wakeup_main handler with dbus
- */
-static int connection_setup_wakeup_main(struct wpas_dbus_priv *priv)
-{
-       if (eloop_register_signal(SIGPOLL, process_wakeup_main, priv))
-               return -1;
-
-       dbus_connection_set_wakeup_main_function(priv->con, wakeup_main,
-                                                priv, NULL);
-
-       return 0;
-}
-
-
-/**
  * integrate_with_eloop - Register our mainloop integration with dbus
  * @connection: connection to the system message bus
  * @priv: a dbus control interface data structure
  * Returns: 0 on success, -1 on failure
- *
- * We register our mainloop integration functions with dbus here.
  */
 static int integrate_with_eloop(struct wpas_dbus_priv *priv)
 {
        if (!dbus_connection_set_watch_functions(priv->con, add_watch,
                                                 remove_watch, watch_toggled,
-                                                priv, NULL)) {
-               perror("dbus_connection_set_watch_functions[dbus]");
-               wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
-               return -1;
-       }
-
-       if (!dbus_connection_set_timeout_functions(priv->con, add_timeout,
+                                                priv, NULL) ||
+           !dbus_connection_set_timeout_functions(priv->con, add_timeout,
                                                   remove_timeout,
                                                   timeout_toggled, priv,
                                                   NULL)) {
-               perror("dbus_connection_set_timeout_functions[dbus]");
-               wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
+               wpa_printf(MSG_ERROR, "dbus: Failed to set callback functions");
                return -1;
        }
 
-       if (connection_setup_wakeup_main(priv) < 0) {
-               perror("connection_setup_wakeup_main[dbus]");
-               wpa_printf(MSG_ERROR, "Could not setup main wakeup function.");
+       if (eloop_register_signal(SIGPOLL, process_wakeup_main, priv))
                return -1;
-       }
+       dbus_connection_set_wakeup_main_function(priv->con, wakeup_main,
+                                                priv, NULL);
 
        return 0;
 }
 
 
+static DBusHandlerResult disconnect_filter(DBusConnection *conn,
+                                          DBusMessage *message, void *data)
+{
+       struct wpas_dbus_priv *priv = data;
+
+       if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL,
+                                  "Disconnected")) {
+               wpa_printf(MSG_DEBUG, "dbus: bus disconnected, terminating");
+               dbus_connection_set_exit_on_disconnect(conn, FALSE);
+               wpa_supplicant_terminate_proc(priv->global);
+               return DBUS_HANDLER_RESULT_HANDLED;
+       } else
+               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+
 static int wpas_dbus_init_common(struct wpas_dbus_priv *priv)
 {
        DBusError error;
+       int ret = 0;
 
        /* Get a reference to the system bus */
        dbus_error_init(&error);
        priv->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
-       dbus_error_free(&error);
-       if (!priv->con) {
-               wpa_printf(MSG_ERROR, "dbus: Could not acquire the system "
-                          "bus: %s", strerror(errno));
-               return -1;
+       if (priv->con) {
+               dbus_connection_add_filter(priv->con, disconnect_filter, priv,
+                                          NULL);
+       } else {
+               wpa_printf(MSG_ERROR,
+                          "dbus: Could not acquire the system bus: %s - %s",
+                          error.name, error.message);
+               ret = -1;
        }
+       dbus_error_free(&error);
 
-       return 0;
+       return ret;
 }
 
 
@@ -335,7 +311,7 @@ static int wpas_dbus_init_common_finish(struct wpas_dbus_priv *priv)
         * FIXME: is there a better solution to this problem?
         */
        eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
-                              priv->con, NULL);
+                              priv->con, NULL);
 
        return 0;
 }
@@ -346,10 +322,15 @@ static void wpas_dbus_deinit_common(struct wpas_dbus_priv *priv)
        if (priv->con) {
                eloop_cancel_timeout(dispatch_initial_dbus_messages,
                                     priv->con, NULL);
+               eloop_cancel_timeout(process_timeout, priv, ELOOP_ALL_CTX);
+
                dbus_connection_set_watch_functions(priv->con, NULL, NULL,
                                                    NULL, NULL, NULL);
                dbus_connection_set_timeout_functions(priv->con, NULL, NULL,
                                                      NULL, NULL, NULL);
+               dbus_connection_remove_filter(priv->con, disconnect_filter,
+                                             priv);
+
                dbus_connection_unref(priv->con);
        }
 
@@ -366,26 +347,14 @@ struct wpas_dbus_priv * wpas_dbus_init(struct wpa_global *global)
                return NULL;
        priv->global = global;
 
-       if (wpas_dbus_init_common(priv) < 0) {
-               wpas_dbus_deinit(priv);
-               return NULL;
-       }
-
+       if (wpas_dbus_init_common(priv) < 0 ||
 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW
-       if (wpas_dbus_ctrl_iface_init(priv) < 0) {
-               wpas_dbus_deinit(priv);
-               return NULL;
-       }
+           wpas_dbus_ctrl_iface_init(priv) < 0 ||
 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
-
 #ifdef CONFIG_CTRL_IFACE_DBUS
-       if (wpa_supplicant_dbus_ctrl_iface_init(priv) < 0) {
-               wpas_dbus_deinit(priv);
-               return NULL;
-       }
+           wpa_supplicant_dbus_ctrl_iface_init(priv) < 0 ||
 #endif /* CONFIG_CTRL_IFACE_DBUS */
-
-       if (wpas_dbus_init_common_finish(priv) < 0) {
+           wpas_dbus_init_common_finish(priv) < 0) {
                wpas_dbus_deinit(priv);
                return NULL;
        }