]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
terminal-util: add fallback logic to make_console_stdio()
authorLennart Poettering <lennart@poettering.net>
Tue, 20 Aug 2019 15:30:15 +0000 (17:30 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 20 Aug 2019 15:30:17 +0000 (17:30 +0200)
If /dev/console can't be opened, let's use /dev/null instead.

Inspired by: #13332

src/basic/terminal-util.c

index cf6af45fbb4ff6670cc579ec3cae59d0246f5ca1..c732e8021cf77779ba58f69693c980b20df92764 100644 (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");
 
-        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;
 }