[gnome-session/benzea/40-alpha: 1/4] Separate out the logout and suspend inhibits




commit 796dcfc141587bfc220d4d35038d639acb3ee8eb
Author: Michael Terry <mike mterry name>
Date:   Wed Sep 23 16:58:51 2020 -0400

    Separate out the logout and suspend inhibits
    
    Previously, asking to inhibit suspend ended up inhibiting both
    sleeping and shutting down. And asking to inhibit logout did neither.
    
    With this change, we inhibit only sleeping if suspend is requested
    and start inhibiting shutdown when logout is requested.
    
    This matches the documentation for those Gtk inhibit flags better.
    
    https://gitlab.gnome.org/GNOME/gnome-session/-/issues/69

 gnome-session/gsm-consolekit.c | 62 ++++++++++++++++++----------------------
 gnome-session/gsm-manager.c    |  6 ++--
 gnome-session/gsm-system.c     | 17 +++--------
 gnome-session/gsm-system.h     | 10 ++-----
 gnome-session/gsm-systemd.c    | 64 +++++++++++++++++++-----------------------
 5 files changed, 65 insertions(+), 94 deletions(-)
---
diff --git a/gnome-session/gsm-consolekit.c b/gnome-session/gsm-consolekit.c
index 44ee25f9..2598e803 100644
--- a/gnome-session/gsm-consolekit.c
+++ b/gnome-session/gsm-consolekit.c
@@ -50,7 +50,7 @@ struct _GsmConsolekitPrivate
         char            *session_id;
         gchar           *session_path;
 
-        GSList          *inhibitors;
+        const gchar     *inhibit_locks;
         gint             inhibit_fd;
 
         gboolean         is_active;
@@ -74,7 +74,7 @@ static void
 drop_system_inhibitor (GsmConsolekit *manager)
 {
         if (manager->priv->inhibit_fd != -1) {
-                g_debug ("GsmConsolekit: Dropping system inhibitor");
+                g_debug ("GsmConsolekit: Dropping system inhibitor fd %d", manager->priv->inhibit_fd);
                 close (manager->priv->inhibit_fd);
                 manager->priv->inhibit_fd = -1;
         }
@@ -100,9 +100,6 @@ gsm_consolekit_finalize (GObject *object)
         free (consolekit->priv->session_id);
         g_free (consolekit->priv->session_path);
 
-        if (consolekit->priv->inhibitors != NULL) {
-                g_slist_free_full (consolekit->priv->inhibitors, g_free);
-        }
         drop_system_inhibitor (consolekit);
         drop_delay_inhibitor (consolekit);
 
@@ -728,6 +725,9 @@ inhibit_done (GObject      *source,
         GUnixFDList *fd_list = NULL;
         gint idx;
 
+        /* Drop any previous inhibit before recording the new one */
+        drop_system_inhibitor (manager);
+
         res = g_dbus_proxy_call_with_unix_fd_list_finish (proxy, &fd_list, result, &error);
 
         if (!res) {
@@ -745,27 +745,39 @@ inhibit_done (GObject      *source,
                 g_variant_unref (res);
         }
 
-        if (manager->priv->inhibitors == NULL) {
+        /* Handle a race condition, where locks got unset during dbus call */
+        if (manager->priv->inhibit_locks == NULL) {
                 drop_system_inhibitor (manager);
         }
 }
 
 static void
-gsm_consolekit_add_inhibitor (GsmSystem        *system,
-                           const gchar      *id,
-                           GsmInhibitorFlag  flag)
+gsm_consolekit_set_inhibitors (GsmSystem        *system,
+                               GsmInhibitorFlag  flags)
 {
         GsmConsolekit *manager = GSM_CONSOLEKIT (system);
+        const gchar *locks = NULL;
+        gboolean inhibit_logout;
+        gboolean inhibit_suspend;
 
-        if ((flag & GSM_INHIBITOR_FLAG_SUSPEND) == 0)
-                return;
+        inhibit_logout = (flags & GSM_INHIBITOR_FLAG_LOGOUT) != 0;
+        inhibit_suspend = (flags & GSM_INHIBITOR_FLAG_SUSPEND) != 0;
+
+        if (inhibit_logout && inhibit_suspend) {
+                locks = "sleep:shutdown";
+        } else if (inhibit_logout) {
+                locks = "shutdown";
+        } else if (inhibit_suspend) {
+                locks = "sleep";
+        }
+        manager->priv->inhibit_locks = locks;
 
-        if (manager->priv->inhibitors == NULL) {
-                g_debug ("Adding system inhibitor");
+        if (locks != NULL) {
+                g_debug ("Adding system inhibitor on %s", locks);
                 g_dbus_proxy_call_with_unix_fd_list (manager->priv->ck_proxy,
                                                      "Inhibit",
                                                      g_variant_new ("(ssss)",
-                                                                    "sleep:shutdown",
+                                                                    locks,
                                                                     g_get_user_name (),
                                                                     "user session inhibited",
                                                                     "block"),
@@ -775,24 +787,7 @@ gsm_consolekit_add_inhibitor (GsmSystem        *system,
                                                      NULL,
                                                      inhibit_done,
                                                      manager);
-        }
-        manager->priv->inhibitors = g_slist_prepend (manager->priv->inhibitors, g_strdup (id));
-}
-
-static void
-gsm_consolekit_remove_inhibitor (GsmSystem   *system,
-                              const gchar *id)
-{
-        GsmConsolekit *manager = GSM_CONSOLEKIT (system);
-        GSList *l;
-
-        l = g_slist_find_custom (manager->priv->inhibitors, id, (GCompareFunc)g_strcmp0);
-        if (l == NULL)
-                return;
-
-        g_free (l->data);
-        manager->priv->inhibitors = g_slist_delete_link (manager->priv->inhibitors, l);
-        if (manager->priv->inhibitors == NULL) {
+        } else {
                 drop_system_inhibitor (manager);
         }
 }
@@ -905,8 +900,7 @@ gsm_consolekit_system_init (GsmSystemInterface *iface)
         iface->hibernate = gsm_consolekit_hibernate;
         iface->set_session_idle = gsm_consolekit_set_session_idle;
         iface->is_login_session = gsm_consolekit_is_login_session;
-        iface->add_inhibitor = gsm_consolekit_add_inhibitor;
-        iface->remove_inhibitor = gsm_consolekit_remove_inhibitor;
+        iface->set_inhibitors = gsm_consolekit_set_inhibitors;
         iface->prepare_shutdown = gsm_consolekit_prepare_shutdown;
         iface->complete_shutdown = gsm_consolekit_complete_shutdown;
         iface->is_last_session_for_user = gsm_consolekit_is_last_session_for_user;
diff --git a/gnome-session/gsm-manager.c b/gnome-session/gsm-manager.c
index a15d2176..d38996a1 100644
--- a/gnome-session/gsm-manager.c
+++ b/gnome-session/gsm-manager.c
@@ -2251,6 +2251,8 @@ update_inhibited_actions (GsmManager *manager,
         if (manager->priv->inhibited_actions == new_inhibited_actions)
                 return;
 
+        gsm_system_set_inhibitors (manager->priv->system, new_inhibited_actions);
+
         manager->priv->inhibited_actions = new_inhibited_actions;
         gsm_exported_manager_set_inhibited_actions (manager->priv->skeleton,
                                                     manager->priv->inhibited_actions);
@@ -2274,8 +2276,6 @@ on_store_inhibitor_added (GsmStore   *store,
         g_debug ("GsmManager: Inhibitor added: %s", id);
 
         i = GSM_INHIBITOR (gsm_store_lookup (store, id));
-        gsm_system_add_inhibitor (manager->priv->system, id,
-                                  gsm_inhibitor_peek_flags (i));
 
         new_inhibited_actions = manager->priv->inhibited_actions | gsm_inhibitor_peek_flags (i);
         update_inhibited_actions (manager, new_inhibited_actions);
@@ -2308,8 +2308,6 @@ on_store_inhibitor_removed (GsmStore   *store,
 
         g_debug ("GsmManager: Inhibitor removed: %s", id);
 
-        gsm_system_remove_inhibitor (manager->priv->system, id);
-
         new_inhibited_actions = 0;
         gsm_store_foreach (manager->priv->inhibitors,
                            collect_inhibition_flags,
diff --git a/gnome-session/gsm-system.c b/gnome-session/gsm-system.c
index 4ce20c1d..39b59f9f 100644
--- a/gnome-session/gsm-system.c
+++ b/gnome-session/gsm-system.c
@@ -95,8 +95,7 @@ gsm_system_null_init_iface (GsmSystemInterface *iface)
         iface->hibernate         = (void *) do_nothing;
         iface->set_session_idle  = (void *) do_nothing;
         iface->is_login_session  = (void *) return_true;
-        iface->add_inhibitor     = (void *) do_nothing;
-        iface->remove_inhibitor  = (void *) do_nothing;
+        iface->set_inhibitors    = (void *) do_nothing;
         iface->prepare_shutdown  = (void *) do_nothing;
         iface->complete_shutdown = (void *) do_nothing;
         iface->is_last_session_for_user = (void *) return_false;
@@ -214,18 +213,10 @@ gsm_system_set_session_idle (GsmSystem *system,
 }
 
 void
-gsm_system_add_inhibitor (GsmSystem        *system,
-                          const gchar      *id,
-                          GsmInhibitorFlag  flag)
+gsm_system_set_inhibitors (GsmSystem        *system,
+                           GsmInhibitorFlag  flags)
 {
-        GSM_SYSTEM_GET_IFACE (system)->add_inhibitor (system, id, flag);
-}
-
-void
-gsm_system_remove_inhibitor (GsmSystem   *system,
-                             const gchar *id)
-{
-        GSM_SYSTEM_GET_IFACE (system)->remove_inhibitor (system, id);
+        GSM_SYSTEM_GET_IFACE (system)->set_inhibitors (system, flags);
 }
 
 gboolean
diff --git a/gnome-session/gsm-system.h b/gnome-session/gsm-system.h
index b25ee61a..438434cc 100644
--- a/gnome-session/gsm-system.h
+++ b/gnome-session/gsm-system.h
@@ -65,11 +65,8 @@ struct _GsmSystemInterface
         void     (* set_session_idle) (GsmSystem *system,
                                        gboolean   is_idle);
         gboolean (* is_login_session) (GsmSystem *system);
-        void     (* add_inhibitor)    (GsmSystem        *system,
-                                       const gchar      *id,
+        void     (* set_inhibitors)   (GsmSystem        *system,
                                        GsmInhibitorFlag  flags);
-        void     (* remove_inhibitor) (GsmSystem        *system,
-                                       const gchar      *id);
         void     (* prepare_shutdown) (GsmSystem   *system,
                                        gboolean     restart);
         void     (* complete_shutdown)(GsmSystem   *system);
@@ -119,12 +116,9 @@ gboolean   gsm_system_is_last_session_for_user (GsmSystem *system);
 
 gboolean   gsm_system_is_active        (GsmSystem *system);
 
-void       gsm_system_add_inhibitor    (GsmSystem        *system,
-                                        const gchar      *id,
+void       gsm_system_set_inhibitors   (GsmSystem        *system,
                                         GsmInhibitorFlag  flags);
 
-void       gsm_system_remove_inhibitor (GsmSystem        *system,
-                                        const gchar      *id);
 void       gsm_system_prepare_shutdown  (GsmSystem  *system,
                                          gboolean    restart);
 void       gsm_system_complete_shutdown (GsmSystem  *system);
diff --git a/gnome-session/gsm-systemd.c b/gnome-session/gsm-systemd.c
index 6ac6c9bf..cf30a4bd 100644
--- a/gnome-session/gsm-systemd.c
+++ b/gnome-session/gsm-systemd.c
@@ -55,7 +55,7 @@ struct _GsmSystemdPrivate
         char            *session_id;
         gchar           *session_path;
 
-        GSList          *inhibitors;
+        const gchar     *inhibit_locks;
         gint             inhibit_fd;
 
         gboolean         is_active;
@@ -79,7 +79,7 @@ static void
 drop_system_inhibitor (GsmSystemd *manager)
 {
         if (manager->priv->inhibit_fd != -1) {
-                g_debug ("GsmSystemd: Dropping system inhibitor");
+                g_debug ("GsmSystemd: Dropping system inhibitor fd %d", manager->priv->inhibit_fd);
                 close (manager->priv->inhibit_fd);
                 manager->priv->inhibit_fd = -1;
         }
@@ -108,9 +108,6 @@ gsm_systemd_finalize (GObject *object)
                 g_source_destroy (systemd->priv->sd_source);
         }
 
-        if (systemd->priv->inhibitors != NULL) {
-                g_slist_free_full (systemd->priv->inhibitors, g_free);
-        }
         drop_system_inhibitor (systemd);
         drop_delay_inhibitor (systemd);
 
@@ -892,6 +889,9 @@ inhibit_done (GObject      *source,
         GUnixFDList *fd_list = NULL;
         gint idx;
 
+        /* Drop any previous inhibit before recording the new one */
+        drop_system_inhibitor (manager);
+
         res = g_dbus_proxy_call_with_unix_fd_list_finish (proxy, &fd_list, result, &error);
 
         if (!res) {
@@ -909,27 +909,39 @@ inhibit_done (GObject      *source,
                 g_variant_unref (res);
         }
 
-        if (manager->priv->inhibitors == NULL) {
+        /* Handle a race condition, where locks got unset during dbus call */
+        if (manager->priv->inhibit_locks == NULL) {
                 drop_system_inhibitor (manager);
         }
 }
 
 static void
-gsm_systemd_add_inhibitor (GsmSystem        *system,
-                           const gchar      *id,
-                           GsmInhibitorFlag  flag)
+gsm_systemd_set_inhibitors (GsmSystem        *system,
+                            GsmInhibitorFlag  flags)
 {
         GsmSystemd *manager = GSM_SYSTEMD (system);
+        const gchar *locks = NULL;
+        gboolean inhibit_logout;
+        gboolean inhibit_suspend;
+
+        inhibit_logout = (flags & GSM_INHIBITOR_FLAG_LOGOUT) != 0;
+        inhibit_suspend = (flags & GSM_INHIBITOR_FLAG_SUSPEND) != 0;
+
+        if (inhibit_logout && inhibit_suspend) {
+                locks = "sleep:shutdown";
+        } else if (inhibit_logout) {
+                locks = "shutdown";
+        } else if (inhibit_suspend) {
+                locks = "sleep";
+        }
+        manager->priv->inhibit_locks = locks;
 
-        if ((flag & GSM_INHIBITOR_FLAG_SUSPEND) == 0)
-                return;
-
-        if (manager->priv->inhibitors == NULL) {
-                g_debug ("Adding system inhibitor");
+        if (locks != NULL) {
+                g_debug ("Adding system inhibitor on %s", locks);
                 g_dbus_proxy_call_with_unix_fd_list (manager->priv->sd_proxy,
                                                      "Inhibit",
                                                      g_variant_new ("(ssss)",
-                                                                    "sleep:shutdown",
+                                                                    locks,
                                                                     g_get_user_name (),
                                                                     "user session inhibited",
                                                                     "block"),
@@ -939,24 +951,7 @@ gsm_systemd_add_inhibitor (GsmSystem        *system,
                                                      NULL,
                                                      inhibit_done,
                                                      manager);
-        }
-        manager->priv->inhibitors = g_slist_prepend (manager->priv->inhibitors, g_strdup (id));
-}
-
-static void
-gsm_systemd_remove_inhibitor (GsmSystem   *system,
-                              const gchar *id)
-{
-        GsmSystemd *manager = GSM_SYSTEMD (system);
-        GSList *l;
-
-        l = g_slist_find_custom (manager->priv->inhibitors, id, (GCompareFunc)g_strcmp0);
-        if (l == NULL)
-                return;
-
-        g_free (l->data);
-        manager->priv->inhibitors = g_slist_delete_link (manager->priv->inhibitors, l);
-        if (manager->priv->inhibitors == NULL) {
+        } else {
                 drop_system_inhibitor (manager);
         }
 }
@@ -1126,8 +1121,7 @@ gsm_systemd_system_init (GsmSystemInterface *iface)
         iface->hibernate = gsm_systemd_hibernate;
         iface->set_session_idle = gsm_systemd_set_session_idle;
         iface->is_login_session = gsm_systemd_is_login_session;
-        iface->add_inhibitor = gsm_systemd_add_inhibitor;
-        iface->remove_inhibitor = gsm_systemd_remove_inhibitor;
+        iface->set_inhibitors = gsm_systemd_set_inhibitors;
         iface->prepare_shutdown = gsm_systemd_prepare_shutdown;
         iface->complete_shutdown = gsm_systemd_complete_shutdown;
         iface->is_last_session_for_user = gsm_systemd_is_last_session_for_user;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]