diff mbox

dbus: terminate cleanly on messagebus shutdown

Message ID 1368678090-7021-1-git-send-email-daniel@gnoutcheff.name
State Accepted
Commit d7692b8d1c39db55055db39e4c56aa0426271c52
Headers show

Commit Message

Daniel Gnoutcheff May 16, 2013, 4:21 a.m. UTC
By default, dbus_connection_dispatch() will call _exit() if the bus
connection has been closed.  This caused wpa_supplicant to terminate
without properly cleaning up after itself.

To ensure that we terminate cleanly when the messagebus terminates,
override the exit_on_disconnect behavior and install a filter to handle
libdbus's "Disconnected" signal.

Signed-hostap: Daniel Gnoutcheff <daniel@gnoutcheff.name>
---

This is a slightly edited version of the patch I previously submitted as
an attachment to:
  https://w1.fi/bugz/show_bug.cgi?id=474

See also:
  https://bugs.launchpad.net/ubuntu/+source/wpa/+bug/1124789

 wpa_supplicant/dbus/dbus_common.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

Comments

Dan Williams May 16, 2013, 3:08 p.m. UTC | #1
On Thu, 2013-05-16 at 00:21 -0400, Daniel Gnoutcheff wrote:
> By default, dbus_connection_dispatch() will call _exit() if the bus
> connection has been closed.  This caused wpa_supplicant to terminate
> without properly cleaning up after itself.
> 
> To ensure that we terminate cleanly when the messagebus terminates,
> override the exit_on_disconnect behavior and install a filter to handle
> libdbus's "Disconnected" signal.
> 
> Signed-hostap: Daniel Gnoutcheff <daniel@gnoutcheff.name>

Looks good to me.

Dan

> ---
> 
> This is a slightly edited version of the patch I previously submitted as
> an attachment to:
>   https://w1.fi/bugz/show_bug.cgi?id=474
> 
> See also:
>   https://bugs.launchpad.net/ubuntu/+source/wpa/+bug/1124789
> 
>  wpa_supplicant/dbus/dbus_common.c | 23 ++++++++++++++++++++++-
>  1 file changed, 22 insertions(+), 1 deletion(-)
> 
> diff --git a/wpa_supplicant/dbus/dbus_common.c b/wpa_supplicant/dbus/dbus_common.c
> index 5d0e31e..1e2153c 100644
> --- a/wpa_supplicant/dbus/dbus_common.c
> +++ b/wpa_supplicant/dbus/dbus_common.c
> @@ -17,6 +17,7 @@
>  #include "dbus_common_i.h"
>  #include "dbus_new.h"
>  #include "dbus_old.h"
> +#include "../wpa_supplicant_i.h"
>  
> 
>  #ifndef SIGPOLL
> @@ -256,6 +257,20 @@ static int integrate_with_eloop(struct wpas_dbus_priv *priv)
>  	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)
>  {
> @@ -265,7 +280,10 @@ static int wpas_dbus_init_common(struct wpas_dbus_priv *priv)
>  	/* Get a reference to the system bus */
>  	dbus_error_init(&error);
>  	priv->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
> -	if (!priv->con) {
> +	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;
> @@ -304,6 +322,9 @@ static void wpas_dbus_deinit_common(struct wpas_dbus_priv *priv)
>  						    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);
>  	}
>
Jouni Malinen May 16, 2013, 5:49 p.m. UTC | #2
On Thu, May 16, 2013 at 12:21:30AM -0400, Daniel Gnoutcheff wrote:
> By default, dbus_connection_dispatch() will call _exit() if the bus
> connection has been closed.  This caused wpa_supplicant to terminate
> without properly cleaning up after itself.
> 
> To ensure that we terminate cleanly when the messagebus terminates,
> override the exit_on_disconnect behavior and install a filter to handle
> libdbus's "Disconnected" signal.

Thanks, applied.
diff mbox

Patch

diff --git a/wpa_supplicant/dbus/dbus_common.c b/wpa_supplicant/dbus/dbus_common.c
index 5d0e31e..1e2153c 100644
--- a/wpa_supplicant/dbus/dbus_common.c
+++ b/wpa_supplicant/dbus/dbus_common.c
@@ -17,6 +17,7 @@ 
 #include "dbus_common_i.h"
 #include "dbus_new.h"
 #include "dbus_old.h"
+#include "../wpa_supplicant_i.h"
 
 
 #ifndef SIGPOLL
@@ -256,6 +257,20 @@  static int integrate_with_eloop(struct wpas_dbus_priv *priv)
 	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)
 {
@@ -265,7 +280,10 @@  static int wpas_dbus_init_common(struct wpas_dbus_priv *priv)
 	/* Get a reference to the system bus */
 	dbus_error_init(&error);
 	priv->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
-	if (!priv->con) {
+	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;
@@ -304,6 +322,9 @@  static void wpas_dbus_deinit_common(struct wpas_dbus_priv *priv)
 						    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);
 	}