Skip to content

core/exec-invoke: rework $TERM fallback logic#37647

Merged
keszybz merged 6 commits intosystemd:mainfrom
YHNdnzj:setup-term
May 30, 2025
Merged

core/exec-invoke: rework $TERM fallback logic#37647
keszybz merged 6 commits intosystemd:mainfrom
YHNdnzj:setup-term

Conversation

@YHNdnzj
Copy link
Member

@YHNdnzj YHNdnzj commented May 28, 2025

Inspired by #37538, see a detailed rationale in #37538 (comment).

@YHNdnzj YHNdnzj added the pid1 label May 28, 2025
@github-actions github-actions bot added util-lib please-review PR is ready for (re-)review by a maintainer labels May 28, 2025
@YHNdnzj YHNdnzj added this to the v258 milestone May 28, 2025
@YHNdnzj YHNdnzj requested a review from keszybz May 28, 2025 19:22
@bam80
Copy link

bam80 commented May 28, 2025

There should be description I guess.

Copy link
Member

@yuwata yuwata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. But let's wait for other reviewers.

n = strlen_ptr(e);

if (n <= 0)
if (n == 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, @poettering prefers <= even for unsigned. I do not have any preference on it. So, feel free to keep it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I find <= for unsigned types very confusing. I always do a double take and recheck that the type is indeed unsigned. Variables don't change type unexpectedly, so this kind of "safe coding" just in case somebody comes and changes the type of the variable we define ourselves seems completely unneeded.

The only place where this would make sense is for externally defined variables in libc and elsewhere where there is some confusion as to the exact type, or the documented type is iffy.

@YHNdnzj
Copy link
Member Author

YHNdnzj commented May 29, 2025

Added one more commit on top, PTAL.

@keszybz
Copy link
Member

keszybz commented May 29, 2025

I think this should wait until #37538 is merged. I don't want to do a massive rebase.

@YHNdnzj
Copy link
Member Author

YHNdnzj commented May 29, 2025

I think this should wait until #37538 is merged. I don't want to do a massive rebase.

I don't see it being "massive" though. This essentially just moves things around wrt your PR...

@bam80
Copy link

bam80 commented May 29, 2025

The main work happens in #37538 though, and it's not finished yet.
If we deem this change is important enough, maybe request it there then?

@YHNdnzj
Copy link
Member Author

YHNdnzj commented May 29, 2025

If you try to literally "rebase" it it can be painful indeed, but from scratch it's only a couple of lines:

diff --git a/src/core/exec-invoke.c b/src/core/exec-invoke.c
index eb8ac14322..14c4d65b04 100644
--- a/src/core/exec-invoke.c
+++ b/src/core/exec-invoke.c
@@ -4578,9 +4578,16 @@ static int setup_term_environment(const ExecContext *context, char ***env) {
                         if (r < 0)
                                 log_debug_errno(r, "Failed to read '%s' from kernel cmdline, ignoring: %m", key);
                 }
+
+                /* This handles real virtual terminals (returning "linux") and any terminals which support
+                 * the DCS +q query sequence. */
+                _cleanup_free_ char *dcs_term = NULL;
+                r = query_term_for_tty(tty_path, &dcs_term);
+                if (r >= 0)
+                        return strv_env_assign(env, "TERM", dcs_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
@@ -4597,7 +4604,7 @@ static int setup_term_environment(const ExecContext *context, char ***env) {
         if (r < 0)
                 return r;
 
-        return strv_env_assign(env, "TERM", default_term_for_tty(tty_path));
+        return strv_env_assign(env, "TERM", FALLBACK_TERM);
 }
 
 int exec_invoke(

@YHNdnzj
Copy link
Member Author

YHNdnzj commented May 29, 2025

The main work happens in #37538 though, and it's not finished yet. If we deem this change is important enough, maybe request it there then?

This PR makes sense on its own as explained in the commit message. It contains follow-ups to the logic we merged in this release cycle and more cleanups. So either way it should go in IMO.

@keszybz
Copy link
Member

keszybz commented May 29, 2025

I agree that this makes sense and should go in… But it increases work for both of us if there's a pull request open that touches the same function. So please wait with this one until the other one is done.

YHNdnzj added 5 commits May 29, 2025 21:01
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.
Copy link
Member

@keszybz keszybz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the followup.

@keszybz keszybz merged commit 2e8d47f into systemd:main May 30, 2025
38 of 43 checks passed
@github-actions github-actions bot removed the please-review PR is ready for (re-)review by a maintainer label May 30, 2025
@YHNdnzj YHNdnzj deleted the setup-term branch May 30, 2025 10:42
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.

4 participants