b9a53a
From 613a02b7d67864af1860e9137e2ee101d603463e Mon Sep 17 00:00:00 2001
b9a53a
From: Jan Synacek <jsynacek@redhat.com>
b9a53a
Date: Thu, 25 Apr 2019 12:19:16 +0200
b9a53a
Subject: [PATCH] debug-generator: enable custom systemd.debug_shell tty
b9a53a
b9a53a
(cherry picked from commit 93912e872fb14e9c372e090409e429084a6450f5)
b9a53a
b9a53a
Resolves: #1723722
b9a53a
---
b9a53a
 man/custom-entities.ent.in            |  1 +
b9a53a
 man/systemd-debug-generator.xml       | 13 ++++++---
b9a53a
 meson.build                           |  1 +
b9a53a
 src/debug-generator/debug-generator.c | 41 +++++++++++++++++++++------
b9a53a
 4 files changed, 43 insertions(+), 13 deletions(-)
b9a53a
b9a53a
diff --git a/man/custom-entities.ent.in b/man/custom-entities.ent.in
b9a53a
index e2bd44e5e7..85805777a0 100644
b9a53a
--- a/man/custom-entities.ent.in
b9a53a
+++ b/man/custom-entities.ent.in
b9a53a
@@ -8,3 +8,4 @@
b9a53a
 
b9a53a
 
b9a53a
 
b9a53a
+
b9a53a
diff --git a/man/systemd-debug-generator.xml b/man/systemd-debug-generator.xml
b9a53a
index fa88e8ac01..25d8b1a873 100644
b9a53a
--- a/man/systemd-debug-generator.xml
b9a53a
+++ b/man/systemd-debug-generator.xml
b9a53a
@@ -1,6 +1,10 @@
b9a53a
 
b9a53a
 
b9a53a
-
b9a53a
+
b9a53a
+  "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
b9a53a
+
b9a53a
+%entities;
b9a53a
+]>
b9a53a
 
b9a53a
   SPDX-License-Identifier: LGPL-2.1+
b9a53a
 -->
b9a53a
@@ -57,9 +61,10 @@
b9a53a
     <option>rd.systemd.debug_shell</option> option is
b9a53a
     specified, the debug shell service
b9a53a
     <literal>debug-shell.service</literal> is pulled into the boot
b9a53a
-    transaction. It will spawn a debug shell on tty9 during early
b9a53a
-    system startup. Note that the shell may also be turned on
b9a53a
-    persistently by enabling it with
b9a53a
+    transaction and a debug shell will be spawned during early boot.
b9a53a
+    By default, <filename>&DEBUGTTY;</filename> is used, but a specific tty can also be set,
b9a53a
+    either with or without the <filename>/dev/</filename> prefix.
b9a53a
+    Note that the shell may also be turned on persistently by enabling it with
b9a53a
     <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
b9a53a
     <command>enable</command> command.
b9a53a
     <option>rd.systemd.debug_shell=</option> is honored only by initial
b9a53a
diff --git a/meson.build b/meson.build
b9a53a
index fe82ca4ac2..70811c29cf 100644
b9a53a
--- a/meson.build
b9a53a
+++ b/meson.build
b9a53a
@@ -768,6 +768,7 @@ conf.set_quoted('GETTEXT_PACKAGE', meson.project_name())
b9a53a
 
b9a53a
 substs.set('SUSHELL', get_option('debug-shell'))
b9a53a
 substs.set('DEBUGTTY', get_option('debug-tty'))
b9a53a
+conf.set_quoted('DEBUGTTY', get_option('debug-tty'))
b9a53a
 
b9a53a
 enable_debug_hashmap = false
b9a53a
 enable_debug_mmap_cache = false
b9a53a
diff --git a/src/debug-generator/debug-generator.c b/src/debug-generator/debug-generator.c
b9a53a
index 800d31cebe..ddef385833 100644
b9a53a
--- a/src/debug-generator/debug-generator.c
b9a53a
+++ b/src/debug-generator/debug-generator.c
b9a53a
@@ -1,8 +1,11 @@
b9a53a
 /* SPDX-License-Identifier: LGPL-2.1+ */
b9a53a
 
b9a53a
 #include "alloc-util.h"
b9a53a
+#include "dropin.h"
b9a53a
+#include "generator.h"
b9a53a
 #include "mkdir.h"
b9a53a
 #include "parse-util.h"
b9a53a
+#include "path-util.h"
b9a53a
 #include "proc-cmdline.h"
b9a53a
 #include "special.h"
b9a53a
 #include "string-util.h"
b9a53a
@@ -14,7 +17,7 @@ static char *arg_default_unit = NULL;
b9a53a
 static const char *arg_dest = "/tmp";
b9a53a
 static char **arg_mask = NULL;
b9a53a
 static char **arg_wants = NULL;
b9a53a
-static bool arg_debug_shell = false;
b9a53a
+static char *arg_debug_shell = NULL;
b9a53a
 
b9a53a
 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
b9a53a
         int r;
b9a53a
@@ -50,15 +53,16 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
b9a53a
                         return log_oom();
b9a53a
 
b9a53a
         } else if (proc_cmdline_key_streq(key, "systemd.debug_shell")) {
b9a53a
+                const char *t = NULL;
b9a53a
 
b9a53a
-                if (value) {
b9a53a
-                        r = parse_boolean(value);
b9a53a
-                        if (r < 0)
b9a53a
-                                log_error("Failed to parse systemd.debug_shell= argument '%s', ignoring.", value);
b9a53a
-                        else
b9a53a
-                                arg_debug_shell = r;
b9a53a
-                } else
b9a53a
-                        arg_debug_shell = true;
b9a53a
+                r = value ? parse_boolean(value) : 1;
b9a53a
+                if (r < 0)
b9a53a
+                        t = skip_dev_prefix(value);
b9a53a
+                else if (r > 0)
b9a53a
+                        t = skip_dev_prefix(DEBUGTTY);
b9a53a
+
b9a53a
+                if (free_and_strdup(&arg_debug_shell, t) < 0)
b9a53a
+                        return log_oom();
b9a53a
 
b9a53a
         } else if (streq(key, "systemd.unit")) {
b9a53a
 
b9a53a
@@ -136,6 +140,23 @@ static int generate_wants_symlinks(void) {
b9a53a
         return r;
b9a53a
 }
b9a53a
 
b9a53a
+static void install_debug_shell_dropin(const char *dir) {
b9a53a
+        int r;
b9a53a
+
b9a53a
+        if (streq(arg_debug_shell, skip_dev_prefix(DEBUGTTY)))
b9a53a
+                return;
b9a53a
+
b9a53a
+        r = write_drop_in_format(dir, "debug-shell.service", 50, "tty",
b9a53a
+                        "[Unit]\n"
b9a53a
+                        "Description=Early root shell on /dev/%s FOR DEBUGGING ONLY\n"
b9a53a
+                        "ConditionPathExists=\n"
b9a53a
+                        "[Service]\n"
b9a53a
+                        "TTYPath=/dev/%s",
b9a53a
+                        arg_debug_shell, arg_debug_shell);
b9a53a
+        if (r < 0)
b9a53a
+                log_warning_errno(r, "Failed to write drop-in for debug-shell.service, ignoring: %m");
b9a53a
+}
b9a53a
+
b9a53a
 int main(int argc, char *argv[]) {
b9a53a
         int r, q;
b9a53a
 
b9a53a
@@ -164,6 +185,8 @@ int main(int argc, char *argv[]) {
b9a53a
                         r = log_oom();
b9a53a
                         goto finish;
b9a53a
                 }
b9a53a
+
b9a53a
+                install_debug_shell_dropin(arg_dest);
b9a53a
         }
b9a53a
 
b9a53a
         r = generate_mask_symlinks();