prompt-util: add client helper for muting the console

This commit is contained in:
Lennart Poettering
2025-09-15 22:36:17 +02:00
parent ac63a04bd6
commit 40e742be3c
2 changed files with 61 additions and 0 deletions

View File

@@ -2,6 +2,8 @@
#include <unistd.h>
#include <sd-varlink.h>
#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;
}

View File

@@ -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);