From a19b51ad9e446948ba60c359641f6c4c14fec1da Mon Sep 17 00:00:00 2001
From: Michal Srb <msrb@suse.com>
Date: Fri, 26 Jan 2018 10:49:18 +0100
Subject: [PATCH] Add runtime option to disable starting X server as user

If the environmental variable GDM_DISABLE_USER_DISPLAY_SERVER is defined, the
X server will be started under root. The same way as if gdm was built with
--disable-user-display-server option.

This allows system to run X server under root if and only-if necessary.
---
Index: gdm-3.34.1/daemon/gdm-local-display-factory.c
===================================================================
--- gdm-3.34.1.orig/daemon/gdm-local-display-factory.c
+++ gdm-3.34.1/daemon/gdm-local-display-factory.c
@@ -231,11 +231,12 @@ gdm_local_display_factory_create_transie
 
         g_debug ("GdmLocalDisplayFactory: Creating transient display");
 
-#ifdef ENABLE_USER_DISPLAY_SERVER
-        display = gdm_local_display_new ();
-        if (gdm_local_display_factory_use_wayland ())
-                g_object_set (G_OBJECT (display), "session-type", "wayland", NULL);
-#else
+        if (getenv ("GDM_DISABLE_USER_DISPLAY_SERVER") == NULL) {
+                display = gdm_local_display_new ();
+                if (gdm_local_display_factory_use_wayland ())
+                        g_object_set (G_OBJECT (display), "session-type", "wayland", NULL);
+        }
+
         if (display == NULL) {
                 guint32 num;
 
@@ -243,7 +244,6 @@ gdm_local_display_factory_create_transie
 
                 display = gdm_legacy_display_new (num);
         }
-#endif
 
         g_object_set (display,
                       "seat-id", "seat0",
@@ -502,7 +502,7 @@ create_display (GdmLocalDisplayFactory *
         g_debug ("GdmLocalDisplayFactory: Adding display on seat %s", seat_id);
 
 #ifdef ENABLE_USER_DISPLAY_SERVER
-        if (g_strcmp0 (seat_id, "seat0") == 0) {
+        if (getenv ("GDM_DISABLE_USER_DISPLAY_SERVER") == NULL && g_strcmp0 (seat_id, "seat0") == 0) {
                 display = gdm_local_display_new ();
                 if (session_type != NULL) {
                         g_object_set (G_OBJECT (display), "session-type", session_type, NULL);
@@ -772,6 +772,10 @@ on_vt_changed (GIOChannel    *source,
         g_debug ("GdmLocalDisplayFactory: VT changed from %u to %u",
                  previous_vt, factory->active_vt);
 
+        if (getenv ("GDM_DISABLE_USER_DISPLAY_SERVER") != NULL) {
+                return G_SOURCE_CONTINUE;
+        }
+
         store = gdm_display_factory_get_display_store (GDM_DISPLAY_FACTORY (factory));
 
         /* if the old VT was running a wayland login screen kill it
@@ -874,6 +878,7 @@ gdm_local_display_factory_start_monitor
                                                                              g_object_unref);
 
 #if defined(ENABLE_WAYLAND_SUPPORT) && defined(ENABLE_USER_DISPLAY_SERVER)
+        if (getenv ("GDM_DISABLE_USER_DISPLAY_SERVER") == NULL ) {
         io_channel = g_io_channel_new_file ("/sys/class/tty/tty0/active", "r", NULL);
 
         if (io_channel != NULL) {
@@ -884,6 +889,7 @@ gdm_local_display_factory_start_monitor
                                         on_vt_changed,
                                         factory);
         }
+        }
 #endif
 }
 
Index: gdm-3.34.1/daemon/gdm-session.c
===================================================================
--- gdm-3.34.1.orig/daemon/gdm-session.c
+++ gdm-3.34.1/daemon/gdm-session.c
@@ -373,7 +373,11 @@ get_system_session_dirs (GdmSession *sel
 #ifdef ENABLE_WAYLAND_SUPPORT
         if (!self->ignore_wayland) {
 #ifdef ENABLE_USER_DISPLAY_SERVER
-                g_array_prepend_val (search_array, wayland_search_dir);
+                if (getenv ("GDM_DISABLE_USER_DISPLAY_SERVER") == NULL) {
+                        g_array_prepend_val (search_array, wayland_search_dir);
+                } else {
+                        g_array_append_val (search_array, wayland_search_dir);
+                }
 
                 for (i = 0; system_data_dirs[i]; i++) {
                         gchar *dir = g_build_filename (system_data_dirs[i], "wayland-sessions", NULL);
@@ -3315,8 +3319,10 @@ gdm_session_get_display_mode (GdmSession
          *   right now.  It will die with an error if logind devices
          *   are paused when handed out.
          */
-        return GDM_SESSION_DISPLAY_MODE_NEW_VT;
-#else
+        if (getenv ("GDM_DISABLE_USER_DISPLAY_SERVER") == NULL) {
+                return GDM_SESSION_DISPLAY_MODE_NEW_VT;
+        }
+#endif
 
 #ifdef ENABLE_WAYLAND_SUPPORT
         /* Wayland sessions are for now assumed to run in a
@@ -3327,7 +3333,6 @@ gdm_session_get_display_mode (GdmSession
         }
 #endif
         return GDM_SESSION_DISPLAY_MODE_REUSE_VT;
-#endif
 }
 
 void
Index: gdm-3.34.1/daemon/gdm-session-worker.c
===================================================================
--- gdm-3.34.1.orig/daemon/gdm-session-worker.c
+++ gdm-3.34.1/daemon/gdm-session-worker.c
@@ -1055,12 +1055,14 @@ gdm_session_worker_uninitialize_pam (Gdm
          * identical to the session_vt. So in that case we never need to
          * do a VT switch. */
 #ifdef ENABLE_USER_DISPLAY_SERVER
+        if (getenv ("GDM_DISABLE_USER_DISPLAY_SERVER") == NULL ) {
         if (g_strcmp0 (worker->priv->display_seat_id, "seat0") == 0) {
                 /* Switch to the login VT if we are not the login screen. */
                 if (worker->priv->session_vt != GDM_INITIAL_VT) {
                         jump_to_vt (worker, GDM_INITIAL_VT);
                 }
         }
+        }
 #endif
 
         worker->priv->session_vt = 0;
Index: gdm-3.34.1/daemon/gdm-manager.c
===================================================================
--- gdm-3.34.1.orig/daemon/gdm-manager.c
+++ gdm-3.34.1/daemon/gdm-manager.c
@@ -1347,6 +1347,8 @@ set_up_automatic_login_session (GdmManag
                       "display-is-initial", FALSE,
                       NULL);
 
+        if (getenv ("GDM_DISABLE_USER_DISPLAY_SERVER") != NULL)
+                manager->priv->did_automatic_login = TRUE;
         g_debug ("GdmManager: Starting automatic login conversation");
         gdm_session_start_conversation (session, "gdm-autologin");
 }
Index: gdm-3.34.1/daemon/gdm-server.c
===================================================================
--- gdm-3.34.1.orig/daemon/gdm-server.c
+++ gdm-3.34.1/daemon/gdm-server.c
@@ -238,9 +238,9 @@ gdm_server_init_command (GdmServer *serv
         /* For systemd, we don't have a log file but instead log to stdout,
            so set it to the xserver's built-in default verbosity */
         if (debug)
-            verbosity = "7 -logfile /dev/null";
+            verbosity = "7";
         else
-            verbosity = "3 -logfile /dev/null";
+            verbosity = "3";
 #endif
 
         if (g_access (SYSTEMD_X_SERVER, X_OK) < 0) {
@@ -752,7 +752,7 @@ gdm_server_start (GdmServer *server)
         GError **error = &local_error;
 
         /* Hardcode the VT for the initial X server, but nothing else */
-        if (server->is_initial) {
+        if (server->is_initial && g_strcmp0 (server->display_name, ":0") == 0) {
                 vtarg = "vt" G_STRINGIFY (GDM_INITIAL_VT);
         }
 
