terminal-util: change conditioning in terminal_reset_defensive()

So far we conditioned the logic that issues ansi sequences for resetting
the TTY based on whether something is a pty is not (under the assumption
we need no reset on ptys, since they are shortlived).

This is simply wrong though. The pty that a container getty is invoked
on is generally long-lived: as long as the container is up, and it will
be reused between getty instances/sessions all the time. In such a case
we really should reset properly.

Let's instead make the logic dependent on whether TERM is set to
anything other than "dumb". The previous commit made sure we always set
TERM in a sensible way in systemd-run, hence this
*explicit* logic sounds like a much better choice now, as it mea
This commit is contained in:
Lennart Poettering
2025-02-27 15:17:34 +01:00
parent 9ab703d8e1
commit 5b3eaf9e68
5 changed files with 41 additions and 14 deletions
+5 -8
View File
@@ -1934,21 +1934,18 @@ int terminal_reset_defensive(int fd, TerminalResetFlags flags) {
int r = 0;
assert(fd >= 0);
assert(!FLAGS_SET(flags, TERMINAL_RESET_AVOID_ANSI_SEQ|TERMINAL_RESET_FORCE_ANSI_SEQ));
/* Resets the terminal comprehensively, but defensively. i.e. both resets the tty via ioctl()s and
* via ANSI sequences, but avoids the latter in case we are talking to a pty. That's a safety measure
* because ptys might be connected to shell pipelines where we cannot expect such ansi sequences to
* work. Given that ptys are generally short-lived (and not recycled) this restriction shouldn't hurt
* much.
*
* The specified fd should be open for *writing*! */
/* Resets the terminal comprehensively, i.e. via both ioctl()s and via ANSI sequences, but do so only
* if $TERM is unset or set to "dumb" */
if (!isatty_safe(fd))
return -ENOTTY;
RET_GATHER(r, terminal_reset_ioctl(fd, FLAGS_SET(flags, TERMINAL_RESET_SWITCH_TO_TEXT)));
if (terminal_is_pty_fd(fd) == 0)
if (!FLAGS_SET(flags, TERMINAL_RESET_AVOID_ANSI_SEQ) &&
(FLAGS_SET(flags, TERMINAL_RESET_FORCE_ANSI_SEQ) || !getenv_terminal_is_dumb()))
RET_GATHER(r, terminal_reset_ansi_seq(fd));
return r;
+2
View File
@@ -43,6 +43,8 @@ bool isatty_safe(int fd);
typedef enum TerminalResetFlags {
TERMINAL_RESET_SWITCH_TO_TEXT = 1 << 0,
TERMINAL_RESET_AVOID_ANSI_SEQ = 1 << 1,
TERMINAL_RESET_FORCE_ANSI_SEQ = 1 << 2,
} TerminalResetFlags;
int terminal_reset_defensive(int fd, TerminalResetFlags flags);