[gnome-session/wip/laney/systemd-user] gsm-systemd: Find user's graphical session, not the current pid's session



commit 25cada4225a60ee0741ae2bc29a2ead19a4ca385
Author: Iain Lane <iain orangesquash org uk>
Date:   Tue Dec 12 09:51:29 2017 +0000

    gsm-systemd: Find user's graphical session, not the current pid's session
    
    If we're started by systemd --user, we won't be in the XDG session of
    the user. The session will still exist, and we want to monitor when it
    closes so that we know when to die ourselves.

 gnome-session/gsm-systemd.c |   42 ++++++++++++++++++++++++++++++++++++++----
 1 files changed, 38 insertions(+), 4 deletions(-)
---
diff --git a/gnome-session/gsm-systemd.c b/gnome-session/gsm-systemd.c
index f42767a..fd77744 100644
--- a/gnome-session/gsm-systemd.c
+++ b/gnome-session/gsm-systemd.c
@@ -268,6 +268,40 @@ static void sd_proxy_signal_cb (GDBusProxy  *proxy,
                                 GVariant    *parameters,
                                 gpointer     user_data);
 
+static gboolean
+gsm_systemd_find_session (char **session_id)
+{
+        const gchar * const graphical_session_types[] = { "wayland", "x11", "mir", NULL };
+        g_autofree gchar *local_session_id = NULL;
+        g_autofree gchar *type = NULL;
+        int saved_errno;
+
+        g_return_val_if_fail (session_id != NULL, FALSE);
+
+        if ((saved_errno = sd_uid_get_display (getuid (), &local_session_id)) < 0) {
+                g_critical ("Couldn't get display: %s (%s)", g_strerror (-saved_errno), local_session_id);
+                return FALSE;
+        }
+
+        if ((saved_errno = sd_session_get_type (local_session_id, &type)) < 0) {
+                g_critical ("Couldn't get type for session '%s': %s",
+                            local_session_id,
+                            g_strerror (-saved_errno));
+                return FALSE;
+        }
+
+        if (!g_strv_contains (graphical_session_types, type)) {
+                g_critical ("Session '%s' is not a graphical session (type: '%s')",
+                            local_session_id,
+                            type);
+        }
+
+        *session_id = g_strdup (local_session_id);
+
+        return TRUE;
+
+}
+
 static void
 gsm_systemd_init (GsmSystemd *manager)
 {
@@ -304,7 +338,7 @@ gsm_systemd_init (GsmSystemd *manager)
         g_signal_connect (manager->priv->sd_proxy, "g-signal",
                           G_CALLBACK (sd_proxy_signal_cb), manager);
 
-        sd_pid_get_session (getpid (), &manager->priv->session_id);
+        gsm_systemd_find_session (&manager->priv->session_id);
 
         if (manager->priv->session_id == NULL) {
                 g_warning ("Could not get session id for session. Check that logind is "
@@ -312,6 +346,8 @@ gsm_systemd_init (GsmSystemd *manager)
                 return;
         }
 
+        g_debug ("Found session ID: %s", manager->priv->session_id);
+
         res = g_dbus_proxy_call_sync (manager->priv->sd_proxy,
                                       "GetSession",
                                       g_variant_new ("(s)", manager->priv->session_id),
@@ -897,9 +933,7 @@ gsm_systemd_is_last_session_for_user (GsmSystem *system)
         gboolean is_last_session;
         int ret, i;
 
-        ret = sd_pid_get_session (getpid (), &session);
-
-        if (ret != 0) {
+        if (!gsm_systemd_find_session (&session)) {
                 return FALSE;
         }
 


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