core: use terminal DCS sequence to set $TERM

query_term_for_tty() is used in two places: in fixup_environment(),
which affects PID1 itself, and in build_environment(), which affects
spawned services. There is obviously some cost to the extra call,
but I think it's worthwhile to do it. When $TERM is set incorrectly,
basic output works OK, but then there are various annoying corner
cases. In particular, we get the support for color (or lack of it)
wrong, and when output is garbled, users are annoyed. Things like
text editors are almost certain to behave incorrectly. Testing in
test-terminal-util indicates that the time required to make a successful
query is on the order of a dozen microseconds, and an unsuccessful
query costs as much as our timeout, i.e. currently 1/3 ms. I think
this is an acceptable tradeoff.

No caching is used, because fixup_environment() is only called once,
and the other place in build_environment(), only affects services
which are connected to a tty, which is only a handful of services,
and often only started in special circumstances.

Fixes https://github.com/systemd/systemd/issues/36994.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2025-05-20 19:02:31 +02:00
parent e3b050a5c2
commit ad6ca4a612
3 changed files with 18 additions and 5 deletions

View File

@@ -143,6 +143,9 @@ assert_cc((TTY_MODE & 0711) == 0600);
void termios_disable_echo(struct termios *termios);
/* The $TERM value we use for terminals other than the Linux console */
#define FALLBACK_TERM "vt220"
int get_default_background_color(double *ret_red, double *ret_green, double *ret_blue);
int terminal_get_size_by_dsr(int input_fd, int output_fd, unsigned *ret_rows, unsigned *ret_columns);
int terminal_fix_size(int input_fd, int output_fd);

View File

@@ -2038,7 +2038,7 @@ static int build_environment(
}
if (exec_context_needs_term(c)) {
_cleanup_free_ char *cmdline = NULL;
_cleanup_free_ char *cmdline = NULL, *dcs_term = NULL;
const char *tty_path, *term = NULL;
tty_path = exec_context_tty_path(c);
@@ -2063,8 +2063,16 @@ static int build_environment(
term = cmdline;
}
if (!term && tty_path) {
/* This handles real virtual terminals (returning "linux") and
* any terminals which support the DCS +q query sequence. */
r = query_term_for_tty(tty_path, &dcs_term);
if (r >= 0)
term = dcs_term;
}
if (!term) {
/* If no precise $TERM is known and we pick a fallback default, then let's also set
/* If $TERM is not known and we pick a fallback default, then let's also set
* $COLORTERM=truecolor. That's because our fallback default is vt220, which is
* generally a safe bet (as it supports PageUp/PageDown unlike vt100, and is quite
* universally available in terminfo/termcap), except for the fact that real DEC
@@ -2083,7 +2091,7 @@ static int build_environment(
our_env[n_env++] = x;
term = default_term_for_tty(tty_path);
term = FALLBACK_TERM;
}
x = strjoin("TERM=", term);

View File

@@ -1594,8 +1594,10 @@ static int fixup_environment(void) {
return r;
}
const char *t = term ?: default_term_for_tty("/dev/console");
if (setenv("TERM", t, /* overwrite= */ true) < 0)
if (!term)
(void) query_term_for_tty("/dev/console", &term);
if (setenv("TERM", term ?: FALLBACK_TERM, /* overwrite= */ true) < 0)
return -errno;
/* The kernels sets HOME=/ for init. Let's undo this. */