]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
main: explicitly pick up $COLORTERM + $NO_COLOR from kernel cmdline where we pick...
authorLennart Poettering <lennart@poettering.net>
Mon, 17 Mar 2025 08:45:16 +0000 (09:45 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 17 Mar 2025 09:17:51 +0000 (10:17 +0100)
I think we should work towards always picking up the triplet of $TERM +
$COLORTERM + $NO_COLOR where we so far picked up $TERM only. I think
it's safe to say that at this time, $TERM is not enough anymore to
clearly communicate terminal feature support. Hence, teach PID 1 to pick
$COLORTERM + $NO_COLOR wherever we pick up $TERM.

src/core/main.c

index dc93c42f1751591efee8c4ec49324d3f401357af..bca6c96b4e1157725b16a95f42c55bf062b3ad52 100644 (file)
@@ -1558,8 +1558,6 @@ static int bump_unix_max_dgram_qlen(void) {
 }
 
 static int fixup_environment(void) {
-        _cleanup_free_ char *term = NULL;
-        const char *t;
         int r;
 
         /* Only fix up the environment when we are started as PID 1 */
@@ -1575,19 +1573,29 @@ static int fixup_environment(void) {
          * not have support for color mode for example.
          *
          * However if TERM was configured through the kernel command line then leave it alone. */
+        _cleanup_free_ char *term = NULL;
         r = proc_cmdline_get_key("TERM", 0, &term);
         if (r < 0)
                 return r;
-
-        if (r == 0) {
+        if (r > 0) {
+                /* If we pick up $TERM, then also pick up $COLORTERM, $NO_COLOR */
+                FOREACH_STRING(v, "COLORTERM", "NO_COLOR") {
+                        _cleanup_free_ char *vv = NULL;
+                        r = proc_cmdline_get_key(v, 0, &vv);
+                        if (r < 0)
+                                return r;
+                        if (r > 0 && setenv(v, vv, /* overwrite= */ true) < 0)
+                                return -errno;
+                }
+        } else {
+                /* If no $TERM is set then look for the per-tty variable instead */
                 r = proc_cmdline_get_key("systemd.tty.term.console", 0, &term);
                 if (r < 0)
                         return r;
         }
 
-        t = term ?: default_term_for_tty("/dev/console");
-
-        if (setenv("TERM", t, 1) < 0)
+        const char *t = term ?: default_term_for_tty("/dev/console");
+        if (setenv("TERM", t, /* overwrite= */ true) < 0)
                 return -errno;
 
         /* The kernels sets HOME=/ for init. Let's undo this. */