From 27a2513013d3b3c6140de47e04b3042c5b4487e5 Mon Sep 17 00:00:00 2001 From: Mantas Mikulėnas Date: Mon, 17 Jun 2024 12:41:56 +0300 Subject: run: add environment variable to prevent the setting of terminal title This goes together with the existing SYSTEMD_TINT_BACKGROUND. Closes https://github.com/systemd/systemd/issues/33301 --- docs/ENVIRONMENT.md | 4 ++++ src/run/run.c | 3 ++- src/shared/ptyfwd.c | 16 ++++++++++++++++ src/shared/ptyfwd.h | 2 ++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/ENVIRONMENT.md b/docs/ENVIRONMENT.md index d44e91b973..dcd296d17c 100644 --- a/docs/ENVIRONMENT.md +++ b/docs/ENVIRONMENT.md @@ -714,6 +714,10 @@ Tools using the Varlink protocol (such as `varlinkctl`) or sd-bus (such as no effect if the background color is explicitly selected via the relevant `--background=` switch of the tool. +* `$SYSTEMD_ADJUST_TERMINAL_TITLE` – Takes a boolean. When false the terminal + window title will not be updated for interactive invocation of the mentioned + tools. + `systemd-hostnamed`, `systemd-importd`, `systemd-localed`, `systemd-machined`, `systemd-portabled`, `systemd-timedated`: diff --git a/src/run/run.c b/src/run/run.c index 5779403b9c..ba7bb2148a 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -1848,7 +1848,8 @@ static int start_transient_service(sd_bus *bus) { if (!isempty(arg_background)) (void) pty_forward_set_background_color(c.forward, arg_background); - set_window_title(c.forward); + if (shall_set_terminal_title()) + set_window_title(c.forward); } path = unit_dbus_path_from_name(service); diff --git a/src/shared/ptyfwd.c b/src/shared/ptyfwd.c index 998ce96b54..842aef9270 100644 --- a/src/shared/ptyfwd.c +++ b/src/shared/ptyfwd.c @@ -17,6 +17,7 @@ #include "sd-event.h" #include "alloc-util.h" +#include "env-util.h" #include "errno-util.h" #include "extract-word.h" #include "fd-util.h" @@ -367,6 +368,21 @@ static int insert_background_fix(PTYForward *f, size_t offset) { return insert_string(f, offset, s); } +bool shall_set_terminal_title(void) { + static int cache = -1; + + if (cache >= 0) + return cache; + + cache = getenv_bool("SYSTEMD_ADJUST_TERMINAL_TITLE"); + if (cache == -ENXIO) + return (cache = true); + if (cache < 0) + log_debug_errno(cache, "Failed to parse $SYSTEMD_ADJUST_TERMINAL_TITLE, leaving terminal title setting enabled: %m"); + + return cache != 0; +} + static int insert_window_title_fix(PTYForward *f, size_t offset) { assert(f); diff --git a/src/shared/ptyfwd.h b/src/shared/ptyfwd.h index 248646d764..b86027e9bc 100644 --- a/src/shared/ptyfwd.h +++ b/src/shared/ptyfwd.h @@ -50,4 +50,6 @@ int pty_forward_set_titlef(PTYForward *f, const char *format, ...) _printf_(2,3) int pty_forward_set_title_prefix(PTYForward *f, const char *prefix); +bool shall_set_terminal_title(void); + DEFINE_TRIVIAL_CLEANUP_FUNC(PTYForward*, pty_forward_free); -- cgit v1.2.3 From 0bdaa998272da778223ca1965a903c258fcbbfcd Mon Sep 17 00:00:00 2001 From: Mantas Mikulėnas Date: Mon, 17 Jun 2024 14:22:51 +0300 Subject: nspawn, vmspawn: honor the new window title switch --- src/nspawn/nspawn.c | 3 ++- src/vmspawn/vmspawn.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index ed428c6a0b..8cfef7bfbc 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -5562,7 +5562,8 @@ static int run_container( } else if (!isempty(arg_background)) (void) pty_forward_set_background_color(forward, arg_background); - set_window_title(forward); + if (shall_set_terminal_title()) + set_window_title(forward); break; default: diff --git a/src/vmspawn/vmspawn.c b/src/vmspawn/vmspawn.c index 73e165fead..2006545500 100644 --- a/src/vmspawn/vmspawn.c +++ b/src/vmspawn/vmspawn.c @@ -2141,7 +2141,8 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) { } else if (!isempty(arg_background)) (void) pty_forward_set_background_color(forward, arg_background); - set_window_title(forward); + if (shall_set_terminal_title()) + set_window_title(forward); } r = sd_event_loop(event); -- cgit v1.2.3 From 86295cb60e96b96bd971fa87708330271193094d Mon Sep 17 00:00:00 2001 From: Mantas Mikulėnas Date: Wed, 19 Jun 2024 09:23:03 +0300 Subject: run: move condition inside set_window_title() --- src/nspawn/nspawn.c | 6 ++++-- src/run/run.c | 7 +++++-- src/vmspawn/vmspawn.c | 6 ++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 8cfef7bfbc..e29986c214 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -4563,6 +4563,9 @@ static void set_window_title(PTYForward *f) { assert(f); + if (!shall_set_terminal_title()) + return; + (void) gethostname_strict(&hn); if (emoji_enabled()) @@ -5562,8 +5565,7 @@ static int run_container( } else if (!isempty(arg_background)) (void) pty_forward_set_background_color(forward, arg_background); - if (shall_set_terminal_title()) - set_window_title(forward); + set_window_title(forward); break; default: diff --git a/src/run/run.c b/src/run/run.c index ba7bb2148a..cb67a45b26 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -1634,8 +1634,12 @@ static int acquire_invocation_id(sd_bus *bus, const char *unit, sd_id128_t *ret) static void set_window_title(PTYForward *f) { _cleanup_free_ char *hn = NULL, *cl = NULL, *dot = NULL; + assert(f); + if (!shall_set_terminal_title()) + return; + if (!arg_host) (void) gethostname_strict(&hn); @@ -1848,8 +1852,7 @@ static int start_transient_service(sd_bus *bus) { if (!isempty(arg_background)) (void) pty_forward_set_background_color(c.forward, arg_background); - if (shall_set_terminal_title()) - set_window_title(c.forward); + set_window_title(c.forward); } path = unit_dbus_path_from_name(service); diff --git a/src/vmspawn/vmspawn.c b/src/vmspawn/vmspawn.c index 2006545500..9dc1514ba7 100644 --- a/src/vmspawn/vmspawn.c +++ b/src/vmspawn/vmspawn.c @@ -1257,6 +1257,9 @@ static void set_window_title(PTYForward *f) { assert(f); + if (!shall_set_terminal_title()) + return; + (void) gethostname_strict(&hn); if (emoji_enabled()) @@ -2141,8 +2144,7 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) { } else if (!isempty(arg_background)) (void) pty_forward_set_background_color(forward, arg_background); - if (shall_set_terminal_title()) - set_window_title(forward); + set_window_title(forward); } r = sd_event_loop(event); -- cgit v1.2.3