62b301
From 54a8d932442d02f5a7dbf6038c8f557ce41f468b Mon Sep 17 00:00:00 2001
62b301
From: David Tardon <dtardon@redhat.com>
62b301
Date: Fri, 10 Jun 2022 15:07:01 +0200
62b301
Subject: [PATCH] logind-session-dbus: allow to set display name via dbus
62b301
62b301
Currently, the only way to set display name of a graphical session is to
62b301
pass it to CreateSession(). But modern display managers like gdm start
62b301
the display server as part of the user session, which means that the
62b301
display name isn't known yet when the session is being created. Hence,
62b301
let's make it possible to set it later.
62b301
62b301
(cherry picked from commit 4885d7490b23e08d8444e5a68927ce9ce8727e5a)
62b301
62b301
Resolves: #1857969
62b301
---
62b301
 src/login/logind-session-dbus.c       | 26 ++++++++++++++++++++++++++
62b301
 src/login/logind-session.c            | 20 ++++++++++++++++++++
62b301
 src/login/logind-session.h            |  1 +
62b301
 src/login/org.freedesktop.login1.conf |  4 ++++
62b301
 4 files changed, 51 insertions(+)
62b301
62b301
diff --git a/src/login/logind-session-dbus.c b/src/login/logind-session-dbus.c
62b301
index 03585b7f8e..c935959e02 100644
62b301
--- a/src/login/logind-session-dbus.c
62b301
+++ b/src/login/logind-session-dbus.c
62b301
@@ -381,6 +381,31 @@ static int method_release_control(sd_bus_message *message, void *userdata, sd_bu
62b301
         return sd_bus_reply_method_return(message, NULL);
62b301
 }
62b301
 
62b301
+static int method_set_display(sd_bus_message *message, void *userdata, sd_bus_error *error) {
62b301
+        Session *s = userdata;
62b301
+        const char *display;
62b301
+        int r;
62b301
+
62b301
+        assert(message);
62b301
+        assert(s);
62b301
+
62b301
+        r = sd_bus_message_read(message, "s", &display);
62b301
+        if (r < 0)
62b301
+                return r;
62b301
+
62b301
+        if (!session_is_controller(s, sd_bus_message_get_sender(message)))
62b301
+                return sd_bus_error_set(error, BUS_ERROR_NOT_IN_CONTROL, "You must be in control of this session to set display");
62b301
+
62b301
+        if (!SESSION_TYPE_IS_GRAPHICAL(s->type))
62b301
+                return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Setting display is only supported for graphical sessions");
62b301
+
62b301
+        r = session_set_display(s, display);
62b301
+        if (r < 0)
62b301
+                return r;
62b301
+
62b301
+        return sd_bus_reply_method_return(message, NULL);
62b301
+}
62b301
+
62b301
 static int method_take_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
62b301
         Session *s = userdata;
62b301
         uint32_t major, minor;
62b301
@@ -523,6 +548,7 @@ const sd_bus_vtable session_vtable[] = {
62b301
         SD_BUS_METHOD("TakeDevice", "uu", "hb", method_take_device, SD_BUS_VTABLE_UNPRIVILEGED),
62b301
         SD_BUS_METHOD("ReleaseDevice", "uu", NULL, method_release_device, SD_BUS_VTABLE_UNPRIVILEGED),
62b301
         SD_BUS_METHOD("PauseDeviceComplete", "uu", NULL, method_pause_device_complete, SD_BUS_VTABLE_UNPRIVILEGED),
62b301
+        SD_BUS_METHOD("SetDisplay", "s", NULL, method_set_display, SD_BUS_VTABLE_UNPRIVILEGED),
62b301
 
62b301
         SD_BUS_SIGNAL("PauseDevice", "uus", 0),
62b301
         SD_BUS_SIGNAL("ResumeDevice", "uuh", 0),
62b301
diff --git a/src/login/logind-session.c b/src/login/logind-session.c
62b301
index e4c8bb36f6..1143a834a4 100644
62b301
--- a/src/login/logind-session.c
62b301
+++ b/src/login/logind-session.c
62b301
@@ -953,6 +953,26 @@ void session_set_locked_hint(Session *s, bool b) {
62b301
         session_send_changed(s, "LockedHint", NULL);
62b301
 }
62b301
 
62b301
+int session_set_display(Session *s, const char *display) {
62b301
+        int r;
62b301
+
62b301
+        assert(s);
62b301
+        assert(display);
62b301
+
62b301
+        if (streq(s->display, display))
62b301
+                return 0;
62b301
+
62b301
+        r = free_and_strdup(&s->display, display);
62b301
+        if (r < 0)
62b301
+                return r;
62b301
+
62b301
+        session_save(s);
62b301
+
62b301
+        session_send_changed(s, "Display", NULL);
62b301
+
62b301
+        return 1;
62b301
+}
62b301
+
62b301
 static int session_dispatch_fifo(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
62b301
         Session *s = userdata;
62b301
 
62b301
diff --git a/src/login/logind-session.h b/src/login/logind-session.h
62b301
index 7d17d9a25f..9bd0c96a03 100644
62b301
--- a/src/login/logind-session.h
62b301
+++ b/src/login/logind-session.h
62b301
@@ -123,6 +123,7 @@ int session_get_idle_hint(Session *s, dual_timestamp *t);
62b301
 void session_set_idle_hint(Session *s, bool b);
62b301
 int session_get_locked_hint(Session *s);
62b301
 void session_set_locked_hint(Session *s, bool b);
62b301
+int session_set_display(Session *s, const char *display);
62b301
 int session_create_fifo(Session *s);
62b301
 int session_start(Session *s, sd_bus_message *properties, sd_bus_error *error);
62b301
 int session_stop(Session *s, bool force);
62b301
diff --git a/src/login/org.freedesktop.login1.conf b/src/login/org.freedesktop.login1.conf
62b301
index dcde0c22c6..b780ae08cf 100644
62b301
--- a/src/login/org.freedesktop.login1.conf
62b301
+++ b/src/login/org.freedesktop.login1.conf
62b301
@@ -314,6 +314,10 @@
62b301
                        send_interface="org.freedesktop.login1.User"
62b301
                        send_member="Kill"/>
62b301
 
62b301
+                
62b301
+                       send_interface="org.freedesktop.login1.Session"
62b301
+                       send_member="SetDisplay"/>
62b301
+
62b301
                 <allow receive_sender="org.freedesktop.login1"/>
62b301
         </policy>
62b301