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