Blame SOURCES/0293-debug-generator-enable-custom-systemd.debug_shell-tt.patch

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