]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
nspawn: use table to parse --console=
authorDavid Tardon <dtardon@redhat.com>
Tue, 30 Sep 2025 07:56:09 +0000 (09:56 +0200)
committerDavid Tardon <dtardon@redhat.com>
Thu, 2 Oct 2025 14:18:41 +0000 (16:18 +0200)
src/nspawn/nspawn-settings.c
src/nspawn/nspawn-settings.h
src/nspawn/nspawn.c

index e4f4f5ebf114ce7c1a9b0d122dc81e9229e4f8fa..47c1a30f1f29d4804b06ec5db6a564dff58d239e 100644 (file)
@@ -914,6 +914,15 @@ static const char *const timezone_mode_table[_TIMEZONE_MODE_MAX] = {
 
 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(timezone_mode, TimezoneMode, TIMEZONE_AUTO);
 
+static const char *const console_mode_table[_CONSOLE_MODE_MAX] = {
+        [CONSOLE_INTERACTIVE] = "interactive",
+        [CONSOLE_READ_ONLY]   = "read-only",
+        [CONSOLE_PASSIVE]     = "passive",
+        [CONSOLE_PIPE]        = "pipe",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(console_mode, ConsoleMode);
+
 DEFINE_CONFIG_PARSE_ENUM(config_parse_userns_ownership, user_namespace_ownership, UserNamespaceOwnership);
 
 static const char *const user_namespace_ownership_table[_USER_NAMESPACE_OWNERSHIP_MAX] = {
index 84631e6558cef88429a6b874e42cb55ea3ff0bc6..874e168664fa21fcb1ee5e802b0ad09f96619675 100644 (file)
@@ -282,6 +282,9 @@ ResolvConfMode resolv_conf_mode_from_string(const char *s) _pure_;
 const char* timezone_mode_to_string(TimezoneMode a) _const_;
 TimezoneMode timezone_mode_from_string(const char *s) _pure_;
 
+const char* console_mode_to_string(ConsoleMode m) _const_;
+ConsoleMode console_mode_from_string(const char *s) _pure_;
+
 const char* user_namespace_ownership_to_string(UserNamespaceOwnership a) _const_;
 UserNamespaceOwnership user_namespace_ownership_from_string(const char *s) _pure_;
 
index 6bcb0a06a7144eaecb98085b52b3980da707f80e..c980b1f5631ce0e3be527d59715357883e058672 100644 (file)
@@ -288,35 +288,19 @@ STATIC_DESTRUCTOR_REGISTER(arg_background, freep);
 
 static int handle_arg_console(const char *arg) {
         if (streq(arg, "help")) {
-                puts("autopipe\n"
-                     "interactive\n"
-                     "passive\n"
-                     "pipe\n"
-                     "read-only");
-                return 0;
+                puts("autopipe\n");
+                return DUMP_STRING_TABLE(console_mode, ConsoleMode, _CONSOLE_MODE_MAX);
         }
 
-        if (streq(arg, "interactive"))
-                arg_console_mode = CONSOLE_INTERACTIVE;
-        else if (streq(arg, "read-only"))
-                arg_console_mode = CONSOLE_READ_ONLY;
-        else if (streq(arg, "passive"))
-                arg_console_mode = CONSOLE_PASSIVE;
-        else if (streq(arg, "pipe")) {
-                if (isatty_safe(STDIN_FILENO) && isatty_safe(STDOUT_FILENO))
-                        log_full(arg_quiet ? LOG_DEBUG : LOG_NOTICE,
-                                 "Console mode 'pipe' selected, but standard input/output are connected to an interactive TTY. "
-                                 "Most likely you want to use 'interactive' console mode for proper interactivity and shell job control. "
-                                 "Proceeding anyway.");
-
-                arg_console_mode = CONSOLE_PIPE;
-        } else if (streq(arg, "autopipe")) {
+        if (streq(arg, "autopipe")) {
                 if (isatty_safe(STDIN_FILENO) && isatty_safe(STDOUT_FILENO))
                         arg_console_mode = CONSOLE_INTERACTIVE;
                 else
                         arg_console_mode = CONSOLE_PIPE;
         } else
-                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown console mode: %s", optarg);
+                arg_console_mode = console_mode_from_string(optarg);
+        if (arg_console_mode < 0)
+                return log_error_errno(arg_console_mode, "Unknown console mode: %s", optarg);
 
         arg_settings_mask |= SETTING_CONSOLE_MODE;
         return 1;
@@ -1738,6 +1722,12 @@ static int verify_arguments(void) {
         if (r < 0)
                 return r;
 
+        if (arg_console_mode == CONSOLE_PIPE && isatty_safe(STDIN_FILENO) && isatty_safe(STDOUT_FILENO))
+                log_full(arg_quiet ? LOG_DEBUG : LOG_NOTICE,
+                        "Console mode 'pipe' selected, but standard input/output are connected to an interactive TTY. "
+                        "Most likely you want to use 'interactive' console mode for proper interactivity and shell job control. "
+                        "Proceeding anyway.");
+
         return 0;
 }