diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index 688d6808a6..8438a3fb21 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -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); diff --git a/src/core/exec-invoke.c b/src/core/exec-invoke.c index b27358d8dc..f4e090297c 100644 --- a/src/core/exec-invoke.c +++ b/src/core/exec-invoke.c @@ -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); diff --git a/src/core/main.c b/src/core/main.c index 2005d733c6..b1f7cc941b 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -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. */