From 7d7e83cb48a66ab25a8d174237c71b9ec58ae545 Mon Sep 17 00:00:00 2001
From: Didier Roche <didrocks@ubuntu.com>
Date: Thu, 27 Nov 2014 10:54:44 +0100
Subject: [PATCH 1/3] Handle display-manager transitions.

Add a generator to ensure /etc/X11/default-display-manager is controlling
which display-manager is started.
---
 debian/changelog                                   |   6 +
 ...-run-initctl-support-to-SysV-compat-tools.patch |   2 +-
 ...tc-X11-default-display-manager-if-present.patch | 188 +++++++++++++++++++++
 ...rc.d-defaults-before-update-rc.d-enable-d.patch |   6 +-
 debian/patches/series                              |   1 +
 5 files changed, 199 insertions(+), 4 deletions(-)
 create mode 100644 debian/patches/Prefer-etc-X11-default-display-manager-if-present.patch

diff --git a/debian/changelog b/debian/changelog
index ebcc067..758bfdf 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,5 +1,6 @@
 systemd (217-1) UNRELEASED; urgency=medium
 
+  [ Martin Pitt ]
   * New upstream release. Drop all cherry-picked patches and port the Debian
     specific ones.
   * Bump util-linux dependency to >= 2.25 as per NEWS.
@@ -12,6 +13,11 @@ systemd (217-1) UNRELEASED; urgency=medium
   * debian/rules: Call dpkg-gensymbols with -c4 to immediately spot
     changed/missing symbols during build.
 
+  [ Didier Roche ]
+  * Handle display-manager transitions:
+    - Add a generator to ensure /etc/X11/default-display-manager is controlling
+      which display-manager is started.
+
  -- Martin Pitt <mpitt@debian.org>  Wed, 19 Nov 2014 08:06:34 +0100
 
 systemd (215-7) UNRELEASED; urgency=medium
diff --git a/debian/patches/Add-run-initctl-support-to-SysV-compat-tools.patch b/debian/patches/Add-run-initctl-support-to-SysV-compat-tools.patch
index 8d58a87..8c61602 100644
--- a/debian/patches/Add-run-initctl-support-to-SysV-compat-tools.patch
+++ b/debian/patches/Add-run-initctl-support-to-SysV-compat-tools.patch
@@ -11,7 +11,7 @@ as PID 1.
  1 file changed, 11 insertions(+), 6 deletions(-)
 
 diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
-index 2933c24..235ffe1 100644
+index cf416e5..0a40cc2 100644
 --- a/src/systemctl/systemctl.c
 +++ b/src/systemctl/systemctl.c
 @@ -6686,19 +6686,24 @@ static int talk_initctl(void) {
diff --git a/debian/patches/Prefer-etc-X11-default-display-manager-if-present.patch b/debian/patches/Prefer-etc-X11-default-display-manager-if-present.patch
new file mode 100644
index 0000000..b4bd19b
--- /dev/null
+++ b/debian/patches/Prefer-etc-X11-default-display-manager-if-present.patch
@@ -0,0 +1,188 @@
+From: Didier Roche <didrocks@ubuntu.com>
+Date: Thu, 27 Nov 2014 10:51:50 +0100
+Subject: Prefer /etc/X11/default-display-manager if present
+
+Add a generator to ensure /etc/X11/default-display-manager is controlling
+which display-manager is started.
+---
+ Makefile.am                                        |  11 ++-
+ src/default-display-manager-generator/Makefile     |  28 ++++++
+ .../default-display-manager-generator.c            | 105 +++++++++++++++++++++
+ 3 files changed, 143 insertions(+), 1 deletion(-)
+ create mode 100644 src/default-display-manager-generator/Makefile
+ create mode 100644 src/default-display-manager-generator/default-display-manager-generator.c
+
+diff --git a/Makefile.am b/Makefile.am
+index ec5b871..b607a4a 100644
+--- a/Makefile.am
++++ b/Makefile.am
+@@ -402,7 +402,8 @@ systemgenerator_PROGRAMS = \
+ 	systemd-getty-generator \
+ 	systemd-fstab-generator \
+ 	systemd-system-update-generator \
+-	systemd-debug-generator
++	systemd-debug-generator \
++	systemd-default-display-manager-generator
+ 
+ dist_bashcompletion_DATA = \
+ 	shell-completion/bash/busctl \
+@@ -2190,6 +2191,14 @@ systemd_delta_LDADD = \
+ 	libsystemd-shared.la
+ 
+ # ------------------------------------------------------------------------------
++systemd_default_display_manager_generator_SOURCES = \
++    src/default-display-manager-generator/default-display-manager-generator.c
++
++systemd_default_display_manager_generator_LDADD = \
++    libsystemd-label.la \
++    libsystemd-shared.la
++
++# ------------------------------------------------------------------------------
+ systemd_insserv_generator_SOURCES = \
+ 	src/insserv-generator/insserv-generator.c
+ 
+diff --git a/src/default-display-manager-generator/Makefile b/src/default-display-manager-generator/Makefile
+new file mode 100644
+index 0000000..b29b937
+--- /dev/null
++++ b/src/default-display-manager-generator/Makefile
+@@ -0,0 +1,28 @@
++#  This file is part of systemd.
++#
++#  Copyright 2014 Canonical
++#
++#  systemd is free software; you can redistribute it and/or modify it
++#  under the terms of the GNU Lesser General Public License as published by
++#  the Free Software Foundation; either version 2.1 of the License, or
++#  (at your option) any later version.
++#
++#  systemd is distributed in the hope that it will be useful, but
++#  WITHOUT ANY WARRANTY; without even the implied warranty of
++#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
++#  Lesser General Public License for more details.
++#
++#  You should have received a copy of the GNU Lesser General Public License
++#  along with systemd; If not, see <http://www.gnu.org/licenses/>.
++
++# This file is a dirty trick to simplify compilation from within
++# emacs. This file is not intended to be distributed. So, don't touch
++# it, even better ignore it!
++
++all:
++	$(MAKE) -C ..
++
++clean:
++	$(MAKE) -C .. clean
++
++.PHONY: all clean
+diff --git a/src/default-display-manager-generator/default-display-manager-generator.c b/src/default-display-manager-generator/default-display-manager-generator.c
+new file mode 100644
+index 0000000..2fe9521
+--- /dev/null
++++ b/src/default-display-manager-generator/default-display-manager-generator.c
+@@ -0,0 +1,105 @@
++/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
++
++/***
++  This file is part of systemd.
++
++  Copyright 2014 Canonical
++
++  systemd is free software; you can redistribute it and/or modify it
++  under the terms of the GNU Lesser General Public License as published by
++  the Free Software Foundation; either version 2.1 of the License, or
++  (at your option) any later version.
++
++  systemd is distributed in the hope that it will be useful, but
++  WITHOUT ANY WARRANTY; without even the implied warranty of
++  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
++  Lesser General Public License for more details.
++
++  You should have received a copy of the GNU Lesser General Public License
++  along with systemd; If not, see <http://www.gnu.org/licenses/>.
++***/
++
++#include <errno.h>
++#include <unistd.h>
++
++#include "fileio.h"
++#include "log.h"
++#include "mkdir.h"
++#include "util.h"
++
++
++/*
++ * Ensure that the started display-manager is matching
++ * /etc/X11/default-display-manager if present, otherwise
++ * let display-manager.service symlinks pick the preferred one
++ * if any.
++ */
++
++static const char *dm_service_unit = SYSTEM_CONFIG_UNIT_PATH "/display-manager.service";
++static const char *default_dm_file = "/etc/X11/default-display-manager";
++static const char *dest = NULL;
++
++static int generate_display_manager_alias(void) {
++
++        _cleanup_free_ char *default_dm_path = NULL, *enabled_dm_unit = NULL;
++        const char *default_dm = NULL, *in_mem_symlink = NULL, *target_unit_path = NULL;
++        int r;
++
++        r = read_full_file(default_dm_file, &default_dm_path, NULL);
++        if (r < 0) {
++                log_debug("No %s file, nothing to generate", default_dm_file);
++                return 0;
++        }
++        default_dm = strstrip(basename(default_dm_path));
++
++        r = readlink_value(dm_service_unit, &enabled_dm_unit);
++        if (r < 0) {
++                log_warning("No default display manager unit service enabled, setup is manual or a sysvinit file");
++                return 0;
++        }
++
++        /* all is fine if the info matches */
++        if (streq(strappenda(default_dm, ".service"), enabled_dm_unit))
++                return 0;
++
++        target_unit_path = strappenda(SYSTEM_DATA_UNIT_PATH, "/", default_dm, ".service");
++
++        /* we only create the alias symlink for non sysvinit services */
++        if (access(target_unit_path, F_OK) < 0 && (errno == ENOENT)) {
++                log_warning("%s doesn't seem to be a system unit, we disable the systemd enabled display manager", target_unit_path);
++                target_unit_path = "/dev/null";
++        } else {
++                log_warning("%s point at %s while the default systemd unit is %s. Reconfiguring %s as default.",
++                            default_dm_file, default_dm, enabled_dm_unit, default_dm);
++        }
++
++        in_mem_symlink = strappenda(dest, "/display-manager.service");
++        mkdir_parents_label(in_mem_symlink, 0755);
++        if (symlink(target_unit_path, in_mem_symlink) < 0) {
++                log_error("Failed to create symlink %s: %m", in_mem_symlink);
++                return -errno;
++        }
++
++        return 0;
++}
++
++int main(int argc, char *argv[]) {
++        int r;
++
++        if (argc != 4) {
++                log_error("This program takes three arguments.");
++                return EXIT_FAILURE;
++        }
++
++        dest = argv[2];
++
++        log_set_target(LOG_TARGET_SAFE);
++        log_parse_environment();
++        log_open();
++
++        umask(0022);
++
++        r = generate_display_manager_alias();
++
++        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
++}
diff --git a/debian/patches/Run-update-rc.d-defaults-before-update-rc.d-enable-d.patch b/debian/patches/Run-update-rc.d-defaults-before-update-rc.d-enable-d.patch
index 8ce745f..a00a8ba 100644
--- a/debian/patches/Run-update-rc.d-defaults-before-update-rc.d-enable-d.patch
+++ b/debian/patches/Run-update-rc.d-defaults-before-update-rc.d-enable-d.patch
@@ -4,11 +4,11 @@ Subject: Run update-rc.d defaults before update-rc.d <enable|disable>
 
 Closes: #722523
 ---
- src/systemctl/systemctl.c | 42 ++++++++++++++++++++++++++++++++++++++++--
- 1 file changed, 40 insertions(+), 2 deletions(-)
+ src/systemctl/systemctl.c | 40 +++++++++++++++++++++++++++++++++++++++-
+ 1 file changed, 39 insertions(+), 1 deletion(-)
 
 diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
-index d54de20..2933c24 100644
+index d54de20..cf416e5 100644
 --- a/src/systemctl/systemctl.c
 +++ b/src/systemctl/systemctl.c
 @@ -5135,8 +5135,46 @@ static int enable_sysv_units(const char *verb, char **args) {
diff --git a/debian/patches/series b/debian/patches/series
index 924ba5b..792c6fa 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -48,3 +48,4 @@ sd-bus-Accept-no-sender-as-the-destination-field.patch
 login-don-t-overmount-run-user-UID-on-upgrades.patch
 udev-re-enable-mount-propagation-for-udevd.patch
 Add-env-variable-for-machine-ID-path.patch
+Prefer-etc-X11-default-display-manager-if-present.patch
-- 
2.1.3

