From 23f9ff472409d5f1cd0f74c47fa6f86e9df44828 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 5 Jun 2025 14:59:40 +0900 Subject: [PATCH] ptyfwd: introduce pty_forward_set_window_title() helper function --- src/nspawn/nspawn.c | 25 ++---------------------- src/run/run.c | 31 +++--------------------------- src/shared/ptyfwd.c | 44 +++++++++++++++++++++++++++++++++++++++++++ src/shared/ptyfwd.h | 6 ++++++ src/vmspawn/vmspawn.c | 25 ++---------------------- 5 files changed, 57 insertions(+), 74 deletions(-) diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index f404e085a74..f21614aacac 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -4611,28 +4611,6 @@ static int setup_notify_parent(sd_event *event, int fd, pid_t *inner_child_pid, return 0; } -static void set_window_title(PTYForward *f) { - _cleanup_free_ char *hn = NULL, *dot = NULL; - - assert(f); - - if (!shall_set_terminal_title()) - return; - - (void) gethostname_strict(&hn); - - if (emoji_enabled()) - dot = strjoin(glyph(GLYPH_BLUE_CIRCLE), " "); - - if (hn) - (void) pty_forward_set_titlef(f, "%sContainer %s on %s", strempty(dot), arg_machine, hn); - else - (void) pty_forward_set_titlef(f, "%sContainer %s", strempty(dot), arg_machine); - - if (dot) - (void) pty_forward_set_title_prefix(f, dot); -} - static int ptyfwd_hotkey(PTYForward *f, char c, void *userdata) { pid_t pid = PTR_TO_PID(userdata); const char *word; @@ -5643,7 +5621,8 @@ static int run_container( } else if (!isempty(arg_background)) (void) pty_forward_set_background_color(forward, arg_background); - set_window_title(forward); + (void) pty_forward_set_window_title(forward, GLYPH_BLUE_CIRCLE, /* hostname = */ NULL, + STRV_MAKE("Container", arg_machine)); pty_forward_set_hotkey_handler(forward, ptyfwd_hotkey, PID_TO_PTR(*pid)); break; diff --git a/src/run/run.c b/src/run/run.c index 6a74123c96a..5a9476f13f6 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -36,7 +36,6 @@ #include "format-table.h" #include "format-util.h" #include "fs-util.h" -#include "hostname-setup.h" #include "log.h" #include "main-func.h" #include "osc-context.h" @@ -2085,32 +2084,6 @@ static int acquire_invocation_id(sd_bus *bus, const char *unit, sd_id128_t *ret) return r; /* Return true when we get a non-null invocation ID. */ } -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); - - cl = strv_join(arg_cmdline, " "); - if (!cl) - return (void) log_oom(); - - if (emoji_enabled()) - dot = strjoin(glyph(privileged_execution() ? GLYPH_RED_CIRCLE : GLYPH_YELLOW_CIRCLE), " "); - - if (arg_host || hn) - (void) pty_forward_set_titlef(f, "%s%s on %s", strempty(dot), cl, arg_host ?: hn); - else - (void) pty_forward_set_titlef(f, "%s%s", strempty(dot), cl); - - (void) pty_forward_set_title_prefix(f, dot); -} - static int fchown_to_capsule(int fd, const char *capsule) { _cleanup_free_ char *p = NULL; int r; @@ -2187,7 +2160,9 @@ static int run_context_setup_ptyfwd(RunContext *c) { if (!isempty(arg_background)) (void) pty_forward_set_background_color(c->forward, arg_background); - set_window_title(c->forward); + (void) pty_forward_set_window_title(c->forward, + privileged_execution() ? GLYPH_RED_CIRCLE : GLYPH_YELLOW_CIRCLE, + arg_host, arg_cmdline); return 0; } diff --git a/src/shared/ptyfwd.c b/src/shared/ptyfwd.c index 53d3d31131a..3eda8c08343 100644 --- a/src/shared/ptyfwd.c +++ b/src/shared/ptyfwd.c @@ -15,6 +15,8 @@ #include "errno-util.h" #include "extract-word.h" #include "fd-util.h" +#include "glyph-util.h" +#include "hostname-setup.h" #include "io-util.h" #include "log.h" #include "ptyfwd.h" @@ -1254,3 +1256,45 @@ int pty_forward_set_title_prefix(PTYForward *f, const char *title_prefix) { return free_and_strdup(&f->title_prefix, title_prefix); } + +int pty_forward_set_window_title( + PTYForward *f, + Glyph circle, /* e.g. GLYPH_GREEN_CIRCLE */ + const char *hostname, /* Can be NULL, and obtained by gethostname_strict() in that case. */ + char * const *msg) { + + _cleanup_free_ char *hn = NULL, *dot = NULL, *joined = NULL; + int r; + + assert(f); + + if (!shall_set_terminal_title()) + return 0; + + if (!hostname) { + (void) gethostname_strict(&hn); + hostname = hn; + } + + if (circle >= 0 && emoji_enabled()) { + dot = strjoin(glyph(circle), " "); + if (!dot) + return -ENOMEM; + } + + joined = strv_join(msg, " "); + if (!joined) + return -ENOMEM; + + r = pty_forward_set_titlef(f, "%s%s%s%s", strempty(dot), joined, hn ? " on " : "", strempty(hn)); + if (r < 0) + return r; + + if (dot) { + r = pty_forward_set_title_prefix(f, dot); + if (r < 0) + return r; + } + + return 0; +} diff --git a/src/shared/ptyfwd.h b/src/shared/ptyfwd.h index e7b5210cc7a..6f7ec0734c2 100644 --- a/src/shared/ptyfwd.h +++ b/src/shared/ptyfwd.h @@ -49,4 +49,10 @@ int pty_forward_set_title_prefix(PTYForward *f, const char *prefix); bool shall_set_terminal_title(void); +int pty_forward_set_window_title( + PTYForward *f, + Glyph circle, /* e.g. GLYPH_GREEN_CIRCLE */ + const char *hostname, /* Can be NULL, and obtained by gethostname_strict() in that case. */ + char * const *msg); + DEFINE_TRIVIAL_CLEANUP_FUNC(PTYForward*, pty_forward_free); diff --git a/src/vmspawn/vmspawn.c b/src/vmspawn/vmspawn.c index 7e1bd6c1496..0def23f346b 100644 --- a/src/vmspawn/vmspawn.c +++ b/src/vmspawn/vmspawn.c @@ -1446,28 +1446,6 @@ static int merge_initrds(char **ret) { return 0; } -static void set_window_title(PTYForward *f) { - _cleanup_free_ char *hn = NULL, *dot = NULL; - - assert(f); - - if (!shall_set_terminal_title()) - return; - - (void) gethostname_strict(&hn); - - if (emoji_enabled()) - dot = strjoin(glyph(GLYPH_GREEN_CIRCLE), " "); - - if (hn) - (void) pty_forward_set_titlef(f, "%sVirtual Machine %s on %s", strempty(dot), arg_machine, hn); - else - (void) pty_forward_set_titlef(f, "%sVirtual Machine %s", strempty(dot), arg_machine); - - if (dot) - (void) pty_forward_set_title_prefix(f, dot); -} - static int generate_ssh_keypair(const char *key_path, const char *key_type) { _cleanup_free_ char *ssh_keygen = NULL; _cleanup_strv_free_ char **cmdline = NULL; @@ -2449,7 +2427,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); + (void) pty_forward_set_window_title(forward, GLYPH_GREEN_CIRCLE, /* hostname = */ NULL, + STRV_MAKE("Virtual Machine", arg_machine)); } r = sd_event_loop(event); -- 2.47.3