Skip to content

Comments

Use DCS sequence to query terminal name and set $TERM automatically#37538

Merged
YHNdnzj merged 5 commits intosystemd:mainfrom
keszybz:query-terminal-name
May 29, 2025
Merged

Use DCS sequence to query terminal name and set $TERM automatically#37538
YHNdnzj merged 5 commits intosystemd:mainfrom
keszybz:query-terminal-name

Conversation

@keszybz
Copy link
Member

@keszybz keszybz commented May 21, 2025

This code seems to work quickly and nicely for a bunch of modern terminals. Setting $TERM automatically removes an common annoyance for users. This code will not work for all terminal emulators, but by adding it in systemd we'll entice maintainers of those terminals to add support for the sequences. For the terminals that don't support the sequence, we get a bit of a slowdown of < 1 ms, which seems hardly noticeable. The user can always set TERM explicitly to avoid this if upgrading to a newer terminal emulator is not possible.

Closes #36994.

@github-actions github-actions bot added util-lib tests please-review PR is ready for (re-)review by a maintainer labels May 21, 2025
@poettering poettering added reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks and removed please-review PR is ready for (re-)review by a maintainer labels May 21, 2025
@keszybz keszybz force-pushed the query-terminal-name branch from ba8f14a to a5bb690 Compare May 27, 2025 17:03
@github-actions github-actions bot added please-review PR is ready for (re-)review by a maintainer and removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks labels May 27, 2025
@keszybz keszybz force-pushed the query-terminal-name branch from a5bb690 to e4fc5cf Compare May 27, 2025 17:05
@YHNdnzj YHNdnzj added pid1 ci-fails/needs-rework 🔥 Please rework this, the CI noticed an issue with the PR labels May 27, 2025
@keszybz keszybz force-pushed the query-terminal-name branch from e4fc5cf to 34e65d9 Compare May 28, 2025 09:38
@github-actions github-actions bot removed the ci-fails/needs-rework 🔥 Please rework this, the CI noticed an issue with the PR label May 28, 2025
@keszybz
Copy link
Member Author

keszybz commented May 28, 2025

mkosi / ci (ubuntu, noble): 95/95 systemd:integration-tests / TEST-24-CRYPTSETUP TIMEOUT 1800.32s (exit status 254 or signal 126 SIGinvalid)

noble/s390x: 1474s autopkgtest [11:02:49]: ERROR: testbed failure: unexpected eof from the testbed

@keszybz keszybz force-pushed the query-terminal-name branch from 0cadab0 to 0e02a5f Compare May 29, 2025 16:15
@github-actions github-actions bot added please-review PR is ready for (re-)review by a maintainer and removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks labels May 29, 2025
@keszybz
Copy link
Member Author

keszybz commented May 29, 2025

Updated with the requested changes, except for a few cases where I replied above.

@keszybz
Copy link
Member Author

keszybz commented May 29, 2025

So what happens on VTE right now? if i spawn a vm with serial console I'll have to wait 333ms now so that the query times out?

Usually, there are plenty of time between systemd first starts and login prompt shows up. To mitigate the effective delay, could we handle the sequence asynchronously or something like that?

Theoretically, it'd be possible. But in practice pretty annoying, since we'd like to start outputting stuff as soon as possible. So if this was asynchronous, we might end up printing some stuff assuming that color is not available, and then switch, or vice versa. It'd also complicate the code quite a bit. A much better solution is to just enable support in terminals and have the reply quickly.

@YHNdnzj
Copy link
Member

YHNdnzj commented May 29, 2025

LGTM otherwise

@YHNdnzj YHNdnzj added good-to-merge/with-minor-suggestions and removed please-review PR is ready for (re-)review by a maintainer labels May 29, 2025
keszybz added 5 commits May 29, 2025 19:20
As requested in systemd#36994,
use DCS + q name ST. This works, but has limited terminal support:
xterm, foot, kitty.
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 systemd#36994.
@keszybz keszybz force-pushed the query-terminal-name branch from 0e02a5f to f256e48 Compare May 29, 2025 17:22
@keszybz keszybz added good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed and removed good-to-merge/with-minor-suggestions labels May 29, 2025
@keszybz
Copy link
Member Author

keszybz commented May 29, 2025

Updated. Phew.

@YHNdnzj YHNdnzj merged commit c51379e into systemd:main May 29, 2025
37 of 42 checks passed
@github-actions github-actions bot removed the good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed label May 29, 2025
YHNdnzj added a commit to YHNdnzj/systemd that referenced this pull request May 29, 2025
Follow-up for 728dbae
and ad6ca4a

This is inspired by systemd#37538, see the discussion in
systemd#37538 (comment).

If the user already specifies $TERM (which is actually
quite common if you look at run0), we'd needlessly invoke
the "fallback" logic and
a) possibly issue a DCS query whose result we end up simply
   discarding in strv_env_merge()
b) set $COLORTERM to "truecolor" unconditionally, whereas
   the explicit $TERM value might intend to disable the color output

To address this, the logic of setting fallback $TERM and friends
has been split out of build_environment(), and we'd call into it
only after all envvars have been collected.
@bam80
Copy link

bam80 commented May 29, 2025

I would like to thank everyone involved here, especially @keszybz. Good job guys!

I'll report to VTE the patch is merged, with the relevant implications.

@keszybz keszybz deleted the query-terminal-name branch May 30, 2025 08:37
keszybz added a commit that referenced this pull request May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

acquire actual $TERM from terminal instead of hardcoded value

7 participants