From 15033be1a3edd9e57d18ce65e0ab9c56c115bf9d Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 20 Nov 2018 11:28:33 +0100 Subject: [PATCH] keep-alive: add nm_keep_alive_disarm() to silence notifications once we disconnect The NMKeepAlive instance is useful to find out when we should disconnect. The moment when we start disconnecting, we don't care about it anymore. Add a nm_keep_alive_disarm() function to suppress property change events about the alive state, after that point. Emitting further events from that point on is only confusing. Yes, this means, a NMKeepAlive instance shall not be re-used for multiple purposes. Create a separate keep-alive instace for each target that should be guarded. Also, once disarmed, we can release all resources that the NMKeepAlive instance was holding until now. --- src/nm-active-connection.c | 6 ++++++ src/nm-keep-alive.c | 44 ++++++++++++++++++++++++++++++++++---- src/nm-keep-alive.h | 2 ++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/nm-active-connection.c b/src/nm-active-connection.c index ebe911e477..da478e071e 100644 --- a/src/nm-active-connection.c +++ b/src/nm-active-connection.c @@ -264,6 +264,12 @@ nm_active_connection_set_state (NMActiveConnection *self, state_to_string (new_state), state_to_string (priv->state)); + if (new_state > NM_ACTIVE_CONNECTION_STATE_ACTIVATED) { + /* once we are about to deactivate, we don't need the keep-alive instance + * anymore. Freeze/disarm it. */ + nm_keep_alive_disarm (priv->keep_alive); + } + if ( new_state == NM_ACTIVE_CONNECTION_STATE_ACTIVATED && priv->activation_type == NM_ACTIVATION_TYPE_ASSUME) { /* assuming connections mean to gracefully take over an externally diff --git a/src/nm-keep-alive.c b/src/nm-keep-alive.c index 6949682571..96d817a37c 100644 --- a/src/nm-keep-alive.c +++ b/src/nm-keep-alive.c @@ -42,6 +42,8 @@ typedef struct { guint subscription_id; bool floating:1; + bool disarmed:1; + bool forced:1; bool alive:1; bool dbus_client_confirmed:1; @@ -100,6 +102,11 @@ _notify_alive (NMKeepAlive *self) { NMKeepAlivePrivate *priv = NM_KEEP_ALIVE_GET_PRIVATE (self); + if (priv->disarmed) { + /* once disarmed, the alive state is frozen. */ + return; + } + if (priv->alive == _is_alive (self)) return; priv->alive = !priv->alive; @@ -163,7 +170,8 @@ _set_settings_connection_watch_visible (NMKeepAlive *self, old_connection = g_steal_pointer (&priv->connection); } - if (connection) { + if ( connection + && !priv->disarmed) { priv->connection = g_object_ref (connection); g_signal_connect (priv->connection, NM_SETTINGS_CONNECTION_FLAGS_CHANGED, @@ -300,7 +308,8 @@ nm_keep_alive_set_dbus_client_watch (NMKeepAlive *self, cleanup_dbus_watch (self); - if (client_address) { + if ( client_address + && !priv->disarmed) { _LOGD ("Registering dbus client watch for keep alive"); priv->dbus_client = g_strdup (client_address); @@ -323,6 +332,33 @@ nm_keep_alive_set_dbus_client_watch (NMKeepAlive *self, /*****************************************************************************/ +/** + * nm_keep_alive_disarm: + * @self: the #NMKeepAlive instance + * + * Once the instance is disarmed, it will not change its alive state + * anymore and will not emit anymore property changed signals about + * alive state changed. + * + * As such, it will also free internal resources (since they no longer + * affect the externally visible state). + * + * Once disarmed, the instance is frozen and cannot change anymore. + */ +void +nm_keep_alive_disarm (NMKeepAlive *self) +{ + NMKeepAlivePrivate *priv = NM_KEEP_ALIVE_GET_PRIVATE (self); + + priv->disarmed = TRUE; + + /* release internal data. */ + _set_settings_connection_watch_visible (self, NULL, FALSE); + cleanup_dbus_watch (self); +} + +/*****************************************************************************/ + static void get_property (GObject *object, guint prop_id, @@ -366,8 +402,8 @@ dispose (GObject *object) { NMKeepAlive *self = NM_KEEP_ALIVE (object); - _set_settings_connection_watch_visible (self, NULL, FALSE); - cleanup_dbus_watch (self); + /* disarm also happens to free all resources. */ + nm_keep_alive_disarm (self); } static void diff --git a/src/nm-keep-alive.h b/src/nm-keep-alive.h index b8c26e173d..341563b54c 100644 --- a/src/nm-keep-alive.h +++ b/src/nm-keep-alive.h @@ -42,6 +42,8 @@ gboolean nm_keep_alive_is_alive (NMKeepAlive *self); void nm_keep_alive_sink (NMKeepAlive *self); +void nm_keep_alive_disarm (NMKeepAlive *self); + void nm_keep_alive_set_forced (NMKeepAlive *self, gboolean forced);