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
This commit is contained in:
Lennart Poettering 2019-08-20 17:30:15 +02:00
parent e11a5c72ba
commit 9281e70375

View file

@ -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");
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;
}