[gnome-session/gnome-3-24] system: add api for detecting if this is the last session for a user



commit 003ea5eedfd5b9ad8611b45852f2c0db83817d4d
Author: Ray Strode <rstrode redhat com>
Date:   Tue Jun 20 16:51:00 2017 -0400

    system: add api for detecting if this is the last session for a user
    
    https://bugzilla.gnome.org/show_bug.cgi?id=764029

 gnome-session/gsm-consolekit.c |    7 ++++
 gnome-session/gsm-system.c     |    7 ++++
 gnome-session/gsm-system.h     |    3 ++
 gnome-session/gsm-systemd.c    |   63 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 80 insertions(+), 0 deletions(-)
---
diff --git a/gnome-session/gsm-consolekit.c b/gnome-session/gsm-consolekit.c
index 4de5c1e..3d3891e 100644
--- a/gnome-session/gsm-consolekit.c
+++ b/gnome-session/gsm-consolekit.c
@@ -1039,6 +1039,12 @@ gsm_consolekit_complete_shutdown (GsmSystem *system)
                 gsm_consolekit_attempt_stop (system);
 }
 
+static gboolean
+gsm_consolekit_is_last_session_for_user (GsmSystem *system)
+{
+        return FALSE;
+}
+
 static void
 gsm_consolekit_system_init (GsmSystemInterface *iface)
 {
@@ -1057,6 +1063,7 @@ gsm_consolekit_system_init (GsmSystemInterface *iface)
         iface->remove_inhibitor = gsm_consolekit_remove_inhibitor;
         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;
 }
 
 GsmConsolekit *
diff --git a/gnome-session/gsm-system.c b/gnome-session/gsm-system.c
index c24a955..a78409d 100644
--- a/gnome-session/gsm-system.c
+++ b/gnome-session/gsm-system.c
@@ -97,6 +97,7 @@ gsm_system_null_init_iface (GsmSystemInterface *iface)
         iface->remove_inhibitor  = (void *) do_nothing;
         iface->prepare_shutdown  = (void *) do_nothing;
         iface->complete_shutdown = (void *) do_nothing;
+        iface->is_last_session_for_user = (void *) return_false;
 }
 
 static void
@@ -218,6 +219,12 @@ gsm_system_is_login_session (GsmSystem *system)
         return GSM_SYSTEM_GET_IFACE (system)->is_login_session (system);
 }
 
+gboolean
+gsm_system_is_last_session_for_user (GsmSystem *system)
+{
+        return GSM_SYSTEM_GET_IFACE (system)->is_last_session_for_user (system);
+}
+
 /**
  * gsm_system_is_active:
  *
diff --git a/gnome-session/gsm-system.h b/gnome-session/gsm-system.h
index cc1c06a..3a34b50 100644
--- a/gnome-session/gsm-system.h
+++ b/gnome-session/gsm-system.h
@@ -70,6 +70,7 @@ struct _GsmSystemInterface
         void     (* prepare_shutdown) (GsmSystem   *system,
                                        gboolean     restart);
         void     (* complete_shutdown)(GsmSystem   *system);
+        gboolean (* is_last_session_for_user) (GsmSystem *system);
 };
 
 enum _GsmSystemError {
@@ -106,6 +107,8 @@ void       gsm_system_set_session_idle (GsmSystem *system,
 
 gboolean   gsm_system_is_login_session (GsmSystem *system);
 
+gboolean   gsm_system_is_last_session_for_user (GsmSystem *system);
+
 gboolean   gsm_system_is_active        (GsmSystem *system);
 
 void       gsm_system_add_inhibitor    (GsmSystem        *system,
diff --git a/gnome-session/gsm-systemd.c b/gnome-session/gsm-systemd.c
index ee43bfa..f42767a 100644
--- a/gnome-session/gsm-systemd.c
+++ b/gnome-session/gsm-systemd.c
@@ -889,6 +889,68 @@ gsm_systemd_complete_shutdown (GsmSystem *system)
         drop_delay_inhibitor (systemd);
 }
 
+static gboolean
+gsm_systemd_is_last_session_for_user (GsmSystem *system)
+{
+        char **sessions = NULL;
+        char *session = NULL;
+        gboolean is_last_session;
+        int ret, i;
+
+        ret = sd_pid_get_session (getpid (), &session);
+
+        if (ret != 0) {
+                return FALSE;
+        }
+
+        ret = sd_uid_get_sessions (getuid (), FALSE, &sessions);
+
+        if (ret <= 0) {
+                free (session);
+                return FALSE;
+        }
+
+        is_last_session = TRUE;
+        for (i = 0; sessions[i]; i++) {
+                char *state = NULL;
+                char *type = NULL;
+
+                if (g_strcmp0 (sessions[i], session) == 0)
+                        continue;
+
+                ret = sd_session_get_state (sessions[i], &state);
+
+                if (ret != 0)
+                        continue;
+
+                if (g_strcmp0 (state, "closing") == 0) {
+                        free (state);
+                        continue;
+                }
+                free (state);
+
+                ret = sd_session_get_type (sessions[i], &type);
+
+                if (ret != 0)
+                        continue;
+
+                if (g_strcmp0 (type, "x11") != 0 &&
+                    g_strcmp0 (type, "wayland") != 0) {
+                        free (type);
+                        continue;
+                }
+
+                is_last_session = FALSE;
+        }
+
+        for (i = 0; sessions[i]; i++)
+                free (sessions[i]);
+        free (sessions);
+        free (session);
+
+        return is_last_session;
+}
+
 static void
 gsm_systemd_system_init (GsmSystemInterface *iface)
 {
@@ -907,6 +969,7 @@ gsm_systemd_system_init (GsmSystemInterface *iface)
         iface->remove_inhibitor = gsm_systemd_remove_inhibitor;
         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;
 }
 
 GsmSystemd *


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