Use DCS sequence to query terminal name and set $TERM automatically#37538
Use DCS sequence to query terminal name and set $TERM automatically#37538YHNdnzj merged 5 commits intosystemd:mainfrom
Conversation
ba8f14a to
a5bb690
Compare
a5bb690 to
e4fc5cf
Compare
e4fc5cf to
34e65d9
Compare
|
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 |
0cadab0 to
0e02a5f
Compare
|
Updated with the requested changes, except for a few cases where I replied above. |
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. |
|
LGTM otherwise |
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.
0e02a5f to
f256e48
Compare
|
Updated. Phew. |
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.
Inspired by #37538, see a detailed rationale in #37538 (comment).
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.