From dbbcb33057e328da039c32de3010727afbbd4101 Mon Sep 17 00:00:00 2001
From: c4pp4
Date: Wed, 2 Dec 2026 21:31:44 +0100
Subject: [PATCH 1/1] Look up the session for a service started by systemd --user

Debian and Ubuntu do not pass XDG_SEAT, XDG_SEAT_PATH and XDG_SESSION_ID
to systemd --user, which starts indicator-session-service. Without them,
the service did not find the active user, so "Show my name" showed no
name on the panel and the user list marked no user as logged in. It had
no LightDM seat either, so the menu had no Guest Session item and
switching to another user did nothing.

Without XDG_SESSION_ID, the service does not run in a session. Then look
up the variables that are not set: the display session of the user and
its seat from logind, as Unity does, and the seat of the user's LightDM
session. The user is looked up by UID, because elogind resolves
user/self only for a caller that runs in a session.

In a session, a variable that is not set is not looked up. A remote
session, for example, would otherwise take the seat of the user's local
session, and switching to another user there would switch that seat.

Signed-off-by: c4pp4
---
 src/backend-dbus/guest.c |   5 +-
 src/backend-dbus/users.c |   5 +-
 src/backend-dbus/utils.c | 199 ++++++++++++++++++++++++++++++++++++++-
 src/backend-dbus/utils.h |  11 +++
 4 files changed, 214 insertions(+), 6 deletions(-)

diff --git a/src/backend-dbus/guest.c b/src/backend-dbus/guest.c
--- a/src/backend-dbus/guest.c
+++ b/src/backend-dbus/guest.c
@@ -20,6 +20,7 @@
 #include <glib.h>
 
 #include "guest.h"
+#include "utils.h"
 
 struct _IndicatorSessionGuestDbusPrivate
 {
@@ -109,8 +110,8 @@ on_login1_manager_session_list_ready (GObject      * o,
     }
   else
     {
-      const gchar * const current_seat_id = g_getenv ("XDG_SEAT");
-      const gchar * const current_session_id = g_getenv ("XDG_SESSION_ID");
+      const gchar * const current_seat_id = indicator_session_util_get_seat_id ();
+      const gchar * const current_session_id = indicator_session_util_get_session_id ();
       gboolean is_logged_in = FALSE;
       gboolean is_active = FALSE;
       const gchar * session_id = NULL;
diff --git a/src/backend-dbus/users.c b/src/backend-dbus/users.c
--- a/src/backend-dbus/users.c
+++ b/src/backend-dbus/users.c
@@ -20,6 +20,7 @@
 #include "dbus-user.h"
 
 #include "users.h"
+#include "utils.h"
 
 struct _IndicatorSessionUsersDbusPrivate
 {
@@ -370,8 +371,8 @@ on_login1_manager_session_list_ready (GObject      * o,
     }
   else
     {
-      const gchar * const current_seat_id = g_getenv ("XDG_SEAT");
-      const gchar * const current_session_id = g_getenv ("XDG_SESSION_ID");
+      const gchar * const current_seat_id = indicator_session_util_get_seat_id ();
+      const gchar * const current_session_id = indicator_session_util_get_session_id ();
       IndicatorSessionUsersDbus * self = INDICATOR_SESSION_USERS_DBUS (gself);
       const gchar * session_id = NULL;
       guint32 uid = 0;
diff --git a/src/backend-dbus/utils.c b/src/backend-dbus/utils.c
--- a/src/backend-dbus/utils.c
+++ b/src/backend-dbus/utils.c
@@ -17,8 +17,203 @@
  * with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <unistd.h>
+
 #include "utils.h"
 
+/***
+**** Our session
+***/
+
+struct display_session
+{
+  gchar * seat_id;
+  gchar * session_id;
+  gchar * dm_seat_path;
+};
+
+/* get a property of a system bus object, or NULL if it is not of this type */
+static GVariant *
+get_property (GDBusConnection    * bus,
+              const gchar        * name,
+              const gchar        * path,
+              const gchar        * interface,
+              const gchar        * property,
+              const GVariantType * type)
+{
+  GVariant * ret;
+  GVariant * value = NULL;
+
+  ret = g_dbus_connection_call_sync (bus, name, path,
+                                     "org.freedesktop.DBus.Properties", "Get",
+                                     g_variant_new ("(ss)", interface, property),
+                                     G_VARIANT_TYPE ("(v)"),
+                                     G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
+  if (ret != NULL)
+    {
+      g_variant_get (ret, "(v)", &value);
+      g_variant_unref (ret);
+
+      if (!g_variant_is_of_type (value, type))
+        g_clear_pointer (&value, g_variant_unref);
+    }
+
+  return value;
+}
+
+/* the display session of the user, which Unity uses as well, and its seat */
+static void
+get_login1_display_session (GDBusConnection * bus, struct display_session * session)
+{
+  GVariant * user;
+  GVariant * display;
+  GVariant * seat;
+  const gchar * user_path;
+  const gchar * id;
+  const gchar * path;
+
+  /* look up the user by our UID: elogind resolves user/self only for
+     a caller that runs in a session */
+  user = g_dbus_connection_call_sync (bus, "org.freedesktop.login1",
+                                      "/org/freedesktop/login1",
+                                      "org.freedesktop.login1.Manager", "GetUser",
+                                      g_variant_new ("(u)", (guint32) getuid ()),
+                                      G_VARIANT_TYPE ("(o)"),
+                                      G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
+  if (user == NULL)
+    return;
+
+  g_variant_get (user, "(&o)", &user_path);
+  display = get_property (bus, "org.freedesktop.login1", user_path,
+                          "org.freedesktop.login1.User", "Display",
+                          G_VARIANT_TYPE ("(so)"));
+  g_variant_unref (user);
+  if (display == NULL)
+    return;
+
+  g_variant_get (display, "(&s&o)", &id, &path);
+  if (*id != '\0')
+    {
+      session->session_id = g_strdup (id);
+
+      seat = get_property (bus, "org.freedesktop.login1", path,
+                           "org.freedesktop.login1.Session", "Seat",
+                           G_VARIANT_TYPE ("(so)"));
+      if (seat != NULL)
+        {
+          g_variant_get (seat, "(&so)", &id, NULL);
+          if (*id != '\0')
+            session->seat_id = g_strdup (id);
+          g_variant_unref (seat);
+        }
+    }
+
+  g_variant_unref (display);
+}
+
+/* the seat of the user's LightDM session */
+static void
+get_lightdm_seat (GDBusConnection * bus, struct display_session * session)
+{
+  GVariant * sessions;
+  GVariant * user_name;
+  GVariant * seat;
+  GVariantIter iter;
+  const gchar * path;
+
+  sessions = get_property (bus, "org.freedesktop.DisplayManager",
+                           "/org/freedesktop/DisplayManager",
+                           "org.freedesktop.DisplayManager", "Sessions",
+                           G_VARIANT_TYPE_OBJECT_PATH_ARRAY);
+  if (sessions == NULL)
+    return;
+
+  g_variant_iter_init (&iter, sessions);
+  while (session->dm_seat_path == NULL
+         && g_variant_iter_next (&iter, "&o", &path))
+    {
+      user_name = get_property (bus, "org.freedesktop.DisplayManager", path,
+                                "org.freedesktop.DisplayManager.Session",
+                                "UserName", G_VARIANT_TYPE_STRING);
+      if (user_name == NULL)
+        continue;
+
+      if (!g_strcmp0 (g_variant_get_string (user_name, NULL), g_get_user_name ()))
+        {
+          seat = get_property (bus, "org.freedesktop.DisplayManager", path,
+                               "org.freedesktop.DisplayManager.Session",
+                               "Seat", G_VARIANT_TYPE_OBJECT_PATH);
+          if (seat != NULL)
+            {
+              session->dm_seat_path = g_variant_dup_string (seat, NULL);
+              g_variant_unref (seat);
+            }
+        }
+
+      g_variant_unref (user_name);
+    }
+
+  g_variant_unref (sessions);
+}
+
+static const struct display_session *
+get_display_session (void)
+{
+  static struct display_session session;
+  static gsize initialized = 0;
+
+  if (g_once_init_enter (&initialized))
+    {
+      GDBusConnection * bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
+
+      if (bus != NULL)
+        {
+          get_login1_display_session (bus, &session);
+          get_lightdm_seat (bus, &session);
+          g_object_unref (bus);
+        }
+
+      g_once_init_leave (&initialized, 1);
+    }
+
+  return &session;
+}
+
+/* Look up a variable that is not set only outside a session, without
+   XDG_SESSION_ID. In a session, it would be the seat of another session
+   of the user, for example of the local one in a remote session, and
+   switching the user would switch that seat. */
+
+const gchar *
+indicator_session_util_get_seat_id (void)
+{
+  const gchar * str = g_getenv ("XDG_SEAT");
+
+  if (str == NULL && g_getenv ("XDG_SESSION_ID") == NULL)
+    str = get_display_session ()->seat_id;
+
+  return str;
+}
+
+const gchar *
+indicator_session_util_get_session_id (void)
+{
+  const gchar * str = g_getenv ("XDG_SESSION_ID");
+
+  return str != NULL ? str : get_display_session ()->session_id;
+}
+
+const gchar *
+indicator_session_util_get_dm_seat_path (void)
+{
+  const gchar * str = g_getenv ("XDG_SEAT_PATH");
+
+  if (str == NULL && g_getenv ("XDG_SESSION_ID") == NULL)
+    str = get_display_session ()->dm_seat_path;
+
+  return str;
+}
+
 /***
 **** indicator_session_util_get_session_proxies()
 ***/
@@ -143,7 +338,7 @@ indicator_session_util_get_session_proxies (
                                     on_login1_manager_ready, data);
 
   /* login1 seat */
-  if ((str = g_getenv ("XDG_SEAT")))
+  if ((str = indicator_session_util_get_seat_id ()))
     {
       char * path;
       data->pending++;
@@ -168,7 +363,7 @@ indicator_session_util_get_session_proxies (
                               on_accounts_proxy_ready, data);
 
   /* DisplayManager seat */
-  if ((str = g_getenv ("XDG_SEAT_PATH")))
+  if ((str = indicator_session_util_get_dm_seat_path ()))
     {
       data->pending++;
       display_manager_seat_proxy_new_for_bus (
diff --git a/src/backend-dbus/utils.h b/src/backend-dbus/utils.h
--- a/src/backend-dbus/utils.h
+++ b/src/backend-dbus/utils.h
@@ -46,4 +46,15 @@ void indicator_session_util_get_session_proxies (
                    GCancellable                                * cancellable,
                    gpointer                                      user_data);
 
+/**
+ * The seat and session IDs of our session and the object path of its
+ * display manager seat, from XDG_SEAT, XDG_SESSION_ID and XDG_SEAT_PATH.
+ * Debian and Ubuntu do not pass these variables to systemd --user, which
+ * starts the service, so without XDG_SESSION_ID, a variable that is not
+ * set is looked up for the display session of the user.
+ */
+const gchar * indicator_session_util_get_seat_id (void);
+const gchar * indicator_session_util_get_session_id (void);
+const gchar * indicator_session_util_get_dm_seat_path (void);
+
 #endif
