From 40e742be3ce1e944beb39d160919f49d3e13230a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 15 Sep 2025 22:36:17 +0200 Subject: [PATCH] prompt-util: add client helper for muting the console --- src/shared/prompt-util.c | 59 ++++++++++++++++++++++++++++++++++++++++ src/shared/prompt-util.h | 2 ++ 2 files changed, 61 insertions(+) diff --git a/src/shared/prompt-util.c b/src/shared/prompt-util.c index 927ef0770e..28435b2da1 100644 --- a/src/shared/prompt-util.c +++ b/src/shared/prompt-util.c @@ -2,6 +2,8 @@ #include +#include + #include "alloc-util.h" #include "glyph-util.h" #include "log.h" @@ -330,3 +332,60 @@ void chrome_hide(void) { fflush(stdout); } + +static int vl_on_reply(sd_varlink *link, sd_json_variant *parameters, const char *error_id, sd_varlink_reply_flags_t flags, void *userdata) { + assert(link); + + /* We want to keep the link around (since its lifetime defines the lifetime of the console muting), + * hence let's detach it from the event loop now, and then exit the event loop. */ + + _cleanup_(sd_event_unrefp) sd_event *e = sd_event_ref(ASSERT_PTR(sd_varlink_get_event(link))); + sd_varlink_detach_event(link); + (void) sd_event_exit(e, (error_id || !FLAGS_SET(flags, SD_VARLINK_REPLY_CONTINUES)) ? -EBADR : 0); + + return 0; +} + +int mute_console(sd_varlink **ret_link) { + int r; + + assert(ret_link); + + /* Talks to the MuteConsole service, and asks for output to the console to be muted, as long as the + * connection is retained */ + + _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *link = NULL; + r = sd_varlink_connect_address(&link, "/run/systemd/io.systemd.MuteConsole"); + if (r < 0) + return log_debug_errno(r, "Failed to connect to console muting service: %m"); + + _cleanup_(sd_event_unrefp) sd_event* event = NULL; + r = sd_event_new(&event); + if (r < 0) + return r; + + r = sd_varlink_attach_event(link, event, /* priority= */ 0); + if (r < 0) + return r; + + r = sd_varlink_bind_reply(link, vl_on_reply); + if (r < 0) + return r; + + r = sd_varlink_set_relative_timeout(link, UINT64_MAX); + if (r < 0) + return log_debug_errno(r, "Failed to disable method call time-out: %m"); + + r = sd_varlink_observe(link, "io.systemd.MuteConsole.Mute", /* parameters= */ NULL); + if (r < 0) + return log_debug_errno(r, "Failed to issue Mute() call to io.systemd.MuteConsole: %m"); + + /* Now run the event loop, it will exit on the first reply, which is when we know the console output + * is now muted. */ + r = sd_event_loop(event); + if (r < 0) + return r; + + *ret_link = TAKE_PTR(link); + return 0; +} diff --git a/src/shared/prompt-util.h b/src/shared/prompt-util.h index 06dd2c7f0c..70f8f0215d 100644 --- a/src/shared/prompt-util.h +++ b/src/shared/prompt-util.h @@ -29,3 +29,5 @@ int prompt_loop(const char *text, int chrome_show(const char *top, const char *bottom); void chrome_hide(void); + +int mute_console(sd_varlink **ret_link);