From: Lennart Poettering Date: Wed, 10 Jul 2024 16:02:31 +0000 (+0200) Subject: terminal-util: unify code that resets /dev/console in common helper X-Git-Tag: v257-rc1~873^2~32 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2736295ddb78a457796f24805e7b98c3f5304848;p=thirdparty%2Fsystemd.git terminal-util: unify code that resets /dev/console in common helper We have pretty much the same code at two places, let's make it one. --- diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index 92abd52a2ef..54901ccada6 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -582,6 +582,31 @@ int vt_disallocate(const char *name) { return 0; } +void reset_dev_console_fd(int fd, bool switch_to_text) { + int r; + + assert(fd >= 0); + + r = reset_terminal_fd(fd, switch_to_text); + if (r < 0) + log_warning_errno(r, "Failed to reset /dev/console, ignoring: %m"); + + unsigned rows, cols; + r = proc_cmdline_tty_size("/dev/console", &rows, &cols); + if (r < 0) + log_warning_errno(r, "Failed to get /dev/console size, ignoring: %m"); + else if (r > 0) { + r = terminal_set_size_fd(fd, NULL, rows, cols); + if (r < 0) + log_warning_errno(r, "Failed to set configured terminal size on /dev/console, ignoring: %m"); + } else + (void) terminal_fix_size(fd, fd); + + r = terminal_reset_ansi_seq(fd); + if (r < 0) + log_warning_errno(r, "Failed to reset /dev/console using ANSI sequences, ignoring: %m"); +} + int make_console_stdio(void) { int fd, r; @@ -598,25 +623,7 @@ int make_console_stdio(void) { return log_error_errno(r, "Failed to make /dev/null stdin/stdout/stderr: %m"); } else { - unsigned rows, cols; - - r = reset_terminal_fd(fd, /* switch_to_text= */ true); - if (r < 0) - log_warning_errno(r, "Failed to reset terminal, ignoring: %m"); - - r = proc_cmdline_tty_size("/dev/console", &rows, &cols); - if (r < 0) - log_warning_errno(r, "Failed to get terminal size, ignoring: %m"); - else if (r > 0) { - r = terminal_set_size_fd(fd, NULL, rows, cols); - if (r < 0) - log_warning_errno(r, "Failed to set configured terminal size, ignoring: %m"); - } else - (void) terminal_fix_size(fd, fd); - - r = terminal_reset_ansi_seq(fd); - if (r < 0) - log_warning_errno(r, "Failed to reset terminal using ANSI sequences, ignoring: %m"); + reset_dev_console_fd(fd, /* switch_to_text= */ true); r = rearrange_stdio(fd, fd, fd); /* This invalidates 'fd' both on success and on failure. */ if (r < 0) diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index 1c47366eab4..a9d51564510 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -156,6 +156,7 @@ bool tty_is_console(const char *tty) _pure_; int vtnr_from_tty(const char *tty); const char* default_term_for_tty(const char *tty); +void reset_dev_console_fd(int fd, bool switch_to_text); int make_console_stdio(void); int fd_columns(int fd); diff --git a/src/core/main.c b/src/core/main.c index 1b78cd652fc..bc2476ed3a5 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -205,33 +205,18 @@ static int manager_find_user_config_paths(char ***ret_files, char ***ret_dirs) { } static int console_setup(void) { + + if (getpid_cached() != 1) + return 0; + _cleanup_close_ int tty_fd = -EBADF; - unsigned rows, cols; - int r; - tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); + tty_fd = open_terminal("/dev/console", O_RDWR|O_NOCTTY|O_CLOEXEC); if (tty_fd < 0) return log_error_errno(tty_fd, "Failed to open /dev/console: %m"); - /* We don't want to force text mode. plymouth may be showing - * pictures already from initrd. */ - r = reset_terminal_fd(tty_fd, false); - if (r < 0) - return log_error_errno(r, "Failed to reset /dev/console: %m"); - - r = proc_cmdline_tty_size("/dev/console", &rows, &cols); - if (r < 0) - log_warning_errno(r, "Failed to get /dev/console size, ignoring: %m"); - else { - r = terminal_set_size_fd(tty_fd, NULL, rows, cols); - if (r < 0) - log_warning_errno(r, "Failed to set /dev/console size, ignoring: %m"); - } - - r = terminal_reset_ansi_seq(tty_fd); - if (r < 0) - log_warning_errno(r, "Failed to reset /dev/console using ANSI sequences, ignoring: %m"); - + /* We don't want to force text mode. Plymouth may be showing pictures already from initrd. */ + reset_dev_console_fd(tty_fd, /* switch_to_text= */ false); return 0; } @@ -2910,7 +2895,7 @@ static void setup_console_terminal(bool skip_setup) { (void) release_terminal(); /* Reset the console, but only if this is really init and we are freshly booted */ - if (getpid_cached() == 1 && !skip_setup) + if (!skip_setup) (void) console_setup(); }