]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/exec-invoke: skip query of /dev/console $TERM if we're child of pid1 37647/head
authorMike Yuan <me@yhndnzj.com>
Thu, 29 May 2025 00:16:13 +0000 (02:16 +0200)
committerMike Yuan <me@yhndnzj.com>
Thu, 29 May 2025 19:25:28 +0000 (21:25 +0200)
src/core/exec-invoke.c

index 2e96bf2eb72f463a92410b7a55791910937da307..6b69f5666ab90117ae3ac85116dd8accb0a81716 100644 (file)
@@ -4540,7 +4540,11 @@ static int setup_term_environment(const ExecContext *context, char ***env) {
         if (tty_path) {
                 /* If we are forked off PID 1 and we are supposed to operate on /dev/console, then let's try
                  * to inherit the $TERM set for PID 1. This is useful for containers so that the $TERM the
-                 * container manager passes to PID 1 ends up all the way in the console login shown. */
+                 * container manager passes to PID 1 ends up all the way in the console login shown.
+                 *
+                 * Note that if this doesn't work out we won't bother with querying systemd.tty.term.console
+                 * kernel cmdline option or DCS anymore either, because pid1 also imports $TERM based on those
+                 * and it should have showed up as our $TERM if there were anything. */
                 if (tty_is_console(tty_path) && getppid() == 1) {
                         const char *term = strv_find_prefix(environ, "TERM=");
                         if (term) {
@@ -4560,28 +4564,29 @@ static int setup_term_environment(const ExecContext *context, char ***env) {
 
                                 return 1;
                         }
-                }
 
-                if (in_charset(skip_dev_prefix(tty_path), ALPHANUMERICAL)) {
-                        _cleanup_free_ char *key = NULL, *cmdline = NULL;
+                } else {
+                        if (in_charset(skip_dev_prefix(tty_path), ALPHANUMERICAL)) {
+                                _cleanup_free_ char *key = NULL, *cmdline = NULL;
 
-                        key = strjoin("systemd.tty.term.", skip_dev_prefix(tty_path));
-                        if (!key)
-                                return -ENOMEM;
+                                key = strjoin("systemd.tty.term.", skip_dev_prefix(tty_path));
+                                if (!key)
+                                        return -ENOMEM;
 
-                        r = proc_cmdline_get_key(key, /* flags = */ 0, &cmdline);
-                        if (r > 0)
-                                return strv_env_assign(env, "TERM", cmdline);
-                        if (r < 0)
-                                log_debug_errno(r, "Failed to read '%s' from kernel cmdline, ignoring: %m", key);
-                }
+                                r = proc_cmdline_get_key(key, /* flags = */ 0, &cmdline);
+                                if (r > 0)
+                                        return strv_env_assign(env, "TERM", cmdline);
+                                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);
+                        /* 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 $TERM is not known and we pick a fallback default, then let's also set