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