From e0cc091dd637af9db805fd56527efa9da48ccbbd Mon Sep 17 00:00:00 2001
From: Benjamin Berg <bberg@redhat.com>
Date: Mon, 16 Dec 2019 12:58:26 +0100
Subject: [PATCH] Add patches for systemd scopes and to fix Xorg fallback

- Add patches to run applications in transient scopes
  https://gitlab.gnome.org/GNOME/gnome-session/merge_requests/36
- Add patches to quit gnome-session quickly if gnome-shell cannot start
  https://gitlab.gnome.org/GNOME/gnome-session/merge_requests/35
---
 ...tical-when-our-SIGTERM-SIGINT-handle.patch | 36 +++++++++
 ...n-to-configure-launched-applications.patch | 49 ++++++++++++
 ...ace-launched-applications-into-a-sys.patch | 77 +++++++++++++++++++
 ...low-quitting-early-on-SIGTERM-SIGINT.patch | 41 ++++++++++
 gnome-session.spec                            | 18 ++++-
 5 files changed, 220 insertions(+), 1 deletion(-)
 create mode 100644 0001-binary-Log-a-critical-when-our-SIGTERM-SIGINT-handle.patch
 create mode 100644 0001-data-Add-drop-in-to-configure-launched-applications.patch
 create mode 100644 0002-autostart-app-Place-launched-applications-into-a-sys.patch
 create mode 100644 0002-binary-Allow-quitting-early-on-SIGTERM-SIGINT.patch

diff --git a/0001-binary-Log-a-critical-when-our-SIGTERM-SIGINT-handle.patch b/0001-binary-Log-a-critical-when-our-SIGTERM-SIGINT-handle.patch
new file mode 100644
index 0000000..d1a27cb
--- /dev/null
+++ b/0001-binary-Log-a-critical-when-our-SIGTERM-SIGINT-handle.patch
@@ -0,0 +1,36 @@
+From 16f3e738f1e831c3af0f2b0718260601298af0ed Mon Sep 17 00:00:00 2001
+From: Iain Lane <iainl@gnome.org>
+Date: Tue, 10 Dec 2019 12:37:31 +0000
+Subject: [PATCH 1/2] binary: Log a critical when our SIGTERM/SIGINT handler
+ fails to log out
+
+---
+ gnome-session/main.c | 7 ++++++-
+ 1 file changed, 6 insertions(+), 1 deletion(-)
+
+diff --git a/gnome-session/main.c b/gnome-session/main.c
+index c9488823..152e60ed 100644
+--- a/gnome-session/main.c
++++ b/gnome-session/main.c
+@@ -103,12 +103,17 @@ on_name_lost (GDBusConnection *connection,
+ static gboolean
+ term_or_int_signal_cb (gpointer data)
+ {
++        g_autoptr(GError) error = NULL;
+         GsmManager *manager = (GsmManager *)data;
+ 
+         /* let the fatal signals interrupt us */
+         g_debug ("Caught SIGINT/SIGTERM, shutting down normally.");
+ 
+-        gsm_manager_logout (manager, GSM_MANAGER_LOGOUT_MODE_FORCE, NULL);
++        gsm_manager_logout (manager, GSM_MANAGER_LOGOUT_MODE_FORCE, &error);
++
++        if (error != NULL) {
++                g_critical ("Failed to log out: %s", error->message);
++        }
+ 
+         return FALSE;
+ }
+-- 
+2.23.0
+
diff --git a/0001-data-Add-drop-in-to-configure-launched-applications.patch b/0001-data-Add-drop-in-to-configure-launched-applications.patch
new file mode 100644
index 0000000..963ebcf
--- /dev/null
+++ b/0001-data-Add-drop-in-to-configure-launched-applications.patch
@@ -0,0 +1,49 @@
+From 228d779b0d8c91c97a6ee7bc8af036f90e618046 Mon Sep 17 00:00:00 2001
+From: Benjamin Berg <bberg@redhat.com>
+Date: Wed, 27 Nov 2019 16:46:49 +0100
+Subject: [PATCH 1/2] data: Add drop-in to configure launched applications
+
+Both gnome-shell and g-s-d will start putting launched applications into
+transient scopes. As these scopes will be prefixed with gnome-launched-,
+we can set defaults by installing an appropriate drop-in. This allows us
+to bind them to graphical-session.target and lower their stop timeout.
+
+See gnome-shell!863 and gnome-settings-daemon!147
+---
+ data/gnome-launched-override.scope.conf | 6 ++++++
+ data/meson.build                        | 6 ++++++
+ 2 files changed, 12 insertions(+)
+ create mode 100644 data/gnome-launched-override.scope.conf
+
+diff --git a/data/gnome-launched-override.scope.conf b/data/gnome-launched-override.scope.conf
+new file mode 100644
+index 00000000..810bf239
+--- /dev/null
++++ b/data/gnome-launched-override.scope.conf
+@@ -0,0 +1,6 @@
++[Unit]
++CollectMode=inactive-or-failed
++PartOf=graphical-session.target
++
++[Scope]
++TimeoutStopSec=5s
+diff --git a/data/meson.build b/data/meson.build
+index 62f32880..3ae6f245 100644
+--- a/data/meson.build
++++ b/data/meson.build
+@@ -133,6 +133,12 @@ if enable_systemd_session
+     systemd_target,
+     install_dir: systemd_userunitdir
+   )
++
++  install_data(
++    'gnome-launched-override.scope.conf',
++    rename: 'override.conf',
++    install_dir : join_paths(systemd_userunitdir, 'gnome-launched-.scope.d')
++  )
+ endif
+ 
+ data = files('hardware-compatibility')
+-- 
+2.23.0
+
diff --git a/0002-autostart-app-Place-launched-applications-into-a-sys.patch b/0002-autostart-app-Place-launched-applications-into-a-sys.patch
new file mode 100644
index 0000000..9ad1ba7
--- /dev/null
+++ b/0002-autostart-app-Place-launched-applications-into-a-sys.patch
@@ -0,0 +1,77 @@
+From a4f9b41e228c943c9997851a3581326ca1435058 Mon Sep 17 00:00:00 2001
+From: Benjamin Berg <bberg@redhat.com>
+Date: Thu, 28 Nov 2019 14:56:30 +0100
+Subject: [PATCH 2/2] autostart-app: Place launched applications into a systemd
+ scope
+
+Try to place launched applications into a systemd scope. This will only
+have an effect on systemd managed sessions, where it will e.g. protect
+gnome-session from OOM actions to a child resulting in a service
+shutdown.
+---
+ gnome-session/gsm-autostart-app.c | 14 ++++++++++++++
+ meson.build                       |  5 ++++-
+ 2 files changed, 18 insertions(+), 1 deletion(-)
+
+diff --git a/gnome-session/gsm-autostart-app.c b/gnome-session/gsm-autostart-app.c
+index 6fd5915a..28b1a88e 100644
+--- a/gnome-session/gsm-autostart-app.c
++++ b/gnome-session/gsm-autostart-app.c
+@@ -28,6 +28,11 @@
+ #include <gio/gio.h>
+ #include <gio/gdesktopappinfo.h>
+ 
++#ifdef HAVE_GNOME_SYSTEMD
++#define GNOME_DESKTOP_USE_UNSTABLE_API
++#include <libgnome-desktop/gnome-systemd.h>
++#endif
++
+ #ifdef HAVE_SYSTEMD
+ #ifdef ENABLE_SYSTEMD_JOURNAL
+ #include <systemd/sd-journal.h>
+@@ -960,6 +965,15 @@ app_launched (GAppLaunchContext *ctx,
+         g_variant_lookup (platform_data, "startup-notification-id", "s", &sn_id);
+         app->priv->pid = pid;
+         app->priv->startup_id = sn_id;
++
++#ifdef HAVE_GNOME_SYSTEMD
++        /* We are not interested in the result. */
++        gnome_start_systemd_scope (app->priv->desktop_id,
++                                   pid,
++                                   NULL,
++                                   NULL,
++                                   NULL, NULL, NULL);
++#endif
+ }
+ 
+ #ifdef ENABLE_SYSTEMD_JOURNAL
+diff --git a/meson.build b/meson.build
+index fc0fa683..1a62113b 100644
+--- a/meson.build
++++ b/meson.build
+@@ -99,11 +99,12 @@ xtrans_dep = dependency('xtrans')
+ ice_dep = dependency('ice')
+ sm_dep = dependency('sm')
+ x11_dep = dependency('x11')
++gnome_desktop_dep = dependency('gnome-desktop-3.0', version: '>= 3.34.2')
+ 
+ session_deps = [
+   gio_dep,
+   glib_dep,
+-  dependency('gnome-desktop-3.0', version: '>= 3.18.0'),
++  gnome_desktop_dep,
+   dependency('json-glib-1.0', version: '>= 0.10')
+ ]
+ 
+@@ -171,6 +172,8 @@ config_h.set('ENABLE_SYSTEMD_SESSION', enable_systemd_session)
+ config_h.set('ENABLE_SYSTEMD_JOURNAL', enable_systemd_journal)
+ config_h.set('HAVE_CONSOLEKIT', enable_consolekit)
+ config_h.set10('USE_SYSTEMD_SESSION', use_systemd_session)
++# Added in gnome-desktop 3.34.2/3.35.2
++config_h.set('HAVE_GNOME_SYSTEMD', cc.has_header('libgnome-desktop/gnome-systemd.h', dependencies: gnome_desktop_dep))
+ 
+ configure_file(
+   output: 'config.h',
+-- 
+2.23.0
+
diff --git a/0002-binary-Allow-quitting-early-on-SIGTERM-SIGINT.patch b/0002-binary-Allow-quitting-early-on-SIGTERM-SIGINT.patch
new file mode 100644
index 0000000..d048d2a
--- /dev/null
+++ b/0002-binary-Allow-quitting-early-on-SIGTERM-SIGINT.patch
@@ -0,0 +1,41 @@
+From 04d1eb2d2fdf8341b684ea6430d51ee1534f2e5d Mon Sep 17 00:00:00 2001
+From: Iain Lane <iainl@gnome.org>
+Date: Tue, 10 Dec 2019 13:01:38 +0000
+Subject: [PATCH 2/2] binary: Allow quitting early on SIGTERM/SIGINT
+
+Now we're started by systemd, we also need to handle being *stopped* by
+systemd too. systemd sends us a SIGTERM to ask us to quit, but we refuse
+to exit on SIGTERM if we're not fully running. That's a problem if the
+SIGTERM is sent early in startup. For example, if gnome-shell has exited
+due to Wayland not being supported, and we're trying to fall back to
+Xorg, the session will not be running at this point, but we want to be
+able to kill everything in order to try again with Xorg.
+
+Fix this by looking at the `GError` returned by `gsm_manager_logout()`,
+and directly quitting if we're not in the running phase yet.
+---
+ gnome-session/main.c | 7 +++++--
+ 1 file changed, 5 insertions(+), 2 deletions(-)
+
+diff --git a/gnome-session/main.c b/gnome-session/main.c
+index 152e60ed..01f56667 100644
+--- a/gnome-session/main.c
++++ b/gnome-session/main.c
+@@ -109,9 +109,12 @@ term_or_int_signal_cb (gpointer data)
+         /* let the fatal signals interrupt us */
+         g_debug ("Caught SIGINT/SIGTERM, shutting down normally.");
+ 
+-        gsm_manager_logout (manager, GSM_MANAGER_LOGOUT_MODE_FORCE, &error);
++        if (!gsm_manager_logout (manager, GSM_MANAGER_LOGOUT_MODE_FORCE, &error)) {
++                if (g_error_matches (error, GSM_MANAGER_ERROR, GSM_MANAGER_ERROR_NOT_IN_RUNNING)) {
++                    gsm_quit ();
++                    return FALSE;
++                }
+ 
+-        if (error != NULL) {
+                 g_critical ("Failed to log out: %s", error->message);
+         }
+ 
+-- 
+2.23.0
+
diff --git a/gnome-session.spec b/gnome-session.spec
index 2cbf061..90eb82e 100644
--- a/gnome-session.spec
+++ b/gnome-session.spec
@@ -9,7 +9,7 @@
 
 Name: gnome-session
 Version: 3.34.2
-Release: 1%{?dist}
+Release: 2%{?dist}
 Summary: GNOME session manager
 
 License: GPLv2+
@@ -26,6 +26,16 @@ Patch4: 0001-check-accelerated-gles-Use-eglGetPlatformDisplay-EXT.patch
 Patch5: 0001-Add-support-for-new-ConfirmedRebootToBootOptions-sig.patch
 Patch6: 0002-Fedora-Set-grub-boot-flags-on-shutdown-reboot.patch
 
+# Not yet merged upstream but used by e.g. gnome-shell 3.34.2
+# https://gitlab.gnome.org/GNOME/gnome-session/merge_requests/36
+Patch10: 0001-data-Add-drop-in-to-configure-launched-applications.patch
+Patch11: 0002-autostart-app-Place-launched-applications-into-a-sys.patch
+
+# Still a few discussions upstream, but this appears sane and reasonably safe
+# https://gitlab.gnome.org/GNOME/gnome-session/merge_requests/35
+Patch20: 0001-binary-Log-a-critical-when-our-SIGTERM-SIGINT-handle.patch
+Patch21: 0002-binary-Allow-quitting-early-on-SIGTERM-SIGINT.patch
+
 BuildRequires: meson
 BuildRequires: gcc
 BuildRequires: pkgconfig(egl)
@@ -129,6 +139,12 @@ Desktop file to add GNOME on wayland to display manager session menu.
 %{_userunitdir}/gnome-session*
 
 %changelog
+* Mon Dec 16 2019 Benjamin Berg <bberg@redhat.com> - 3.34.2-2
+- Add patches to run applications in transient scopes
+  https://gitlab.gnome.org/GNOME/gnome-session/merge_requests/36
+- Add patches to quit gnome-session quickly if gnome-shell cannot start
+  https://gitlab.gnome.org/GNOME/gnome-session/merge_requests/35
+
 * Thu Nov 28 2019 Kalev Lember <klember@redhat.com> - 3.34.2-1
 - Update to 3.34.2