summaryrefslogtreecommitdiffstats
path: root/src/basic/terminal-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2024-07-17 12:28:53 +0200
committerLennart Poettering <lennart@poettering.net>2024-07-19 11:44:04 +0200
commitdffbe1d152d3ddf5abd747d3792dc1fd18e84775 (patch)
tree9dba676beb2ba32589e0aadc6337cae33382d32b /src/basic/terminal-util.c
parentterminal-util: move lock_dev_console() here (diff)
downloadsystemd-dffbe1d152d3ddf5abd747d3792dc1fd18e84775.tar.xz
systemd-dffbe1d152d3ddf5abd747d3792dc1fd18e84775.zip
terminal-util: teach resolve_dev_console() to deal correctly with /dev/console being a symlink
/dev/console is sometimes a symlink in container managers. Let's handle that correctly, and resolve the symlink, and not consider the data from /sys/ in that case.
Diffstat (limited to 'src/basic/terminal-util.c')
-rw-r--r--src/basic/terminal-util.c26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c
index 6d3a0f8a5c..93f356bde0 100644
--- a/src/basic/terminal-util.c
+++ b/src/basic/terminal-util.c
@@ -22,6 +22,7 @@
#include "alloc-util.h"
#include "ansi-color.h"
+#include "chase.h"
#include "constants.h"
#include "devnum-util.h"
#include "env-util.h"
@@ -698,24 +699,37 @@ int vtnr_from_tty(const char *tty) {
}
int resolve_dev_console(char **ret) {
- _cleanup_free_ char *active = NULL;
- char *tty;
int r;
assert(ret);
- /* Resolve where /dev/console is pointing to, if /sys is actually ours (i.e. not read-only-mounted which is a
- * sign for container setups) */
+ /* Resolve where /dev/console is pointing to. If /dev/console is a symlink (like in container
+ * managers), we'll just resolve the symlink. If it's a real device node, we'll use if
+ * /sys/class/tty/tty0/active, but only if /sys/ is actually ours (i.e. not read-only-mounted which
+ * is a sign for container setups). */
- if (path_is_read_only_fs("/sys") > 0)
+ _cleanup_free_ char *chased = NULL;
+ r = chase("/dev/console", /* root= */ NULL, /* chase_flags= */ 0, &chased, /* ret_fd= */ NULL);
+ if (r < 0)
+ return r;
+ if (!path_equal(chased, "/dev/console")) {
+ *ret = TAKE_PTR(chased);
+ return 0;
+ }
+
+ r = path_is_read_only_fs("/sys");
+ if (r < 0)
+ return r;
+ if (r > 0)
return -ENOMEDIUM;
+ _cleanup_free_ char *active = NULL;
r = read_one_line_file("/sys/class/tty/console/active", &active);
if (r < 0)
return r;
/* If multiple log outputs are configured the last one is what /dev/console points to */
- tty = strrchr(active, ' ');
+ const char *tty = strrchr(active, ' ');
if (tty)
tty++;
else