From 9281e703757e88c0097558ace965bd242ad1e07a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 20 Aug 2019 17:30:15 +0200 Subject: [PATCH] terminal-util: add fallback logic to make_console_stdio() If /dev/console can't be opened, let's use /dev/null instead. Inspired by: #13332 --- src/basic/terminal-util.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index cf6af45fbb4..c732e8021cf 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -578,22 +578,29 @@ int vt_disallocate(const char *name) { int make_console_stdio(void) { int fd, r; - /* Make /dev/console the controlling terminal and stdin/stdout/stderr */ + /* Make /dev/console the controlling terminal and stdin/stdout/stderr, if we can. If we can't use + * /dev/null instead. This is particularly useful if /dev/console is turned off, e.g. if console=null + * is specified on the kernel command line. */ fd = acquire_terminal("/dev/console", ACQUIRE_TERMINAL_FORCE|ACQUIRE_TERMINAL_PERMISSIVE, USEC_INFINITY); - if (fd < 0) - return log_error_errno(fd, "Failed to acquire terminal: %m"); + if (fd < 0) { + log_warning_errno(fd, "Failed to acquire terminal, using /dev/null stdin/stdout/stderr instead: %m"); - r = reset_terminal_fd(fd, true); - if (r < 0) - log_warning_errno(r, "Failed to reset terminal, ignoring: %m"); + r = make_null_stdio(); + if (r < 0) + return log_error_errno(r, "Failed to make /dev/null stdin/stdout/stderr: %m"); - r = rearrange_stdio(fd, fd, fd); /* This invalidates 'fd' both on success and on failure. */ - if (r < 0) - return log_error_errno(r, "Failed to make terminal stdin/stdout/stderr: %m"); + } else { + r = reset_terminal_fd(fd, true); + if (r < 0) + log_warning_errno(r, "Failed to reset terminal, ignoring: %m"); - reset_terminal_feature_caches(); + r = rearrange_stdio(fd, fd, fd); /* This invalidates 'fd' both on success and on failure. */ + if (r < 0) + return log_error_errno(r, "Failed to make terminal stdin/stdout/stderr: %m"); + } + reset_terminal_feature_caches(); return 0; } -- 2.39.2