stdio-bridge: Add --quiet option

When we use stdio-bridge via sd-bus to connect to a bus of a different
user, container or host, stdio-bridge should not log at error level but
at debug level as it's invoked by the sd-bus library and sd-bus should
generally not log above debug level.

We can't actually use the --quiet option yet as that would break connecting
to hosts running older versions of systemd but let's already add the option
now in preparation for a brighter future.
This commit is contained in:
Daan De Meyer
2025-11-13 14:45:51 +01:00
committed by Zbigniew Jędrzejewski-Szmek
parent 0d91a204cb
commit 8bb6f4f164
2 changed files with 47 additions and 28 deletions
+9
View File
@@ -69,6 +69,15 @@
<xi:include href="version-info.xml" xpointer="v251"/></listitem> <xi:include href="version-info.xml" xpointer="v251"/></listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>-q</option></term>
<term><option>--quiet</option></term>
<listitem><para>Suppresses error logging on failure.</para>
<xi:include href="version-info.xml" xpointer="v259"/></listitem>
</varlistentry>
<xi:include href="standard-options.xml" xpointer="help" /> <xi:include href="standard-options.xml" xpointer="help" />
<xi:include href="standard-options.xml" xpointer="version" /> <xi:include href="standard-options.xml" xpointer="version" />
</variablelist> </variablelist>
+38 -28
View File
@@ -20,6 +20,7 @@
static const char *arg_bus_path = DEFAULT_SYSTEM_BUS_ADDRESS; static const char *arg_bus_path = DEFAULT_SYSTEM_BUS_ADDRESS;
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
static RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM; static RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
static bool arg_quiet = false;
static int help(void) { static int help(void) {
printf("%s [OPTIONS...]\n\n" printf("%s [OPTIONS...]\n\n"
@@ -29,7 +30,8 @@ static int help(void) {
" -p --bus-path=PATH Path to the bus address (default: %s)\n" " -p --bus-path=PATH Path to the bus address (default: %s)\n"
" --system Connect to system bus\n" " --system Connect to system bus\n"
" --user Connect to user bus\n" " --user Connect to user bus\n"
" -M --machine=CONTAINER Name of local container to connect to\n", " -M --machine=CONTAINER Name of local container to connect to\n"
" -q --quiet Fail silently instead of logging errors\n",
program_invocation_short_name, DEFAULT_SYSTEM_BUS_ADDRESS); program_invocation_short_name, DEFAULT_SYSTEM_BUS_ADDRESS);
return 0; return 0;
@@ -50,10 +52,11 @@ static int parse_argv(int argc, char *argv[]) {
{ "user", no_argument, NULL, ARG_USER }, { "user", no_argument, NULL, ARG_USER },
{ "system", no_argument, NULL, ARG_SYSTEM }, { "system", no_argument, NULL, ARG_SYSTEM },
{ "machine", required_argument, NULL, 'M' }, { "machine", required_argument, NULL, 'M' },
{ "quiet", no_argument, NULL, 'q' },
{}, {},
}; };
int r, c; int c, r;
assert(argc >= 0); assert(argc >= 0);
assert(argv); assert(argv);
@@ -86,6 +89,10 @@ static int parse_argv(int argc, char *argv[]) {
return r; return r;
break; break;
case 'q':
arg_quiet = true;
break;
case '?': case '?':
return -EINVAL; return -EINVAL;
@@ -94,9 +101,9 @@ static int parse_argv(int argc, char *argv[]) {
} }
if (argc > optind) if (argc > optind)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_full_errno(arg_quiet ? LOG_DEBUG : LOG_ERR, SYNTHETIC_ERRNO(EINVAL),
"%s takes no arguments.", "%s takes no arguments.",
program_invocation_short_name); program_invocation_short_name);
return 1; return 1;
} }
@@ -105,7 +112,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *a = NULL, *b = NULL; _cleanup_(sd_bus_flush_close_unrefp) sd_bus *a = NULL, *b = NULL;
sd_id128_t server_id; sd_id128_t server_id;
bool is_unix; bool is_unix;
int r, in_fd, out_fd; int in_fd, out_fd, r;
log_setup(); log_setup();
@@ -113,6 +120,8 @@ static int run(int argc, char *argv[]) {
if (r <= 0) if (r <= 0)
return r; return r;
int priority = arg_quiet ? LOG_DEBUG : LOG_ERR;
r = sd_listen_fds(0); r = sd_listen_fds(0);
if (r == 0) { if (r == 0) {
in_fd = STDIN_FILENO; in_fd = STDIN_FILENO;
@@ -121,7 +130,8 @@ static int run(int argc, char *argv[]) {
in_fd = SD_LISTEN_FDS_START; in_fd = SD_LISTEN_FDS_START;
out_fd = SD_LISTEN_FDS_START; out_fd = SD_LISTEN_FDS_START;
} else } else
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "More than one file descriptor was passed."); return log_full_errno(priority, SYNTHETIC_ERRNO(EINVAL),
"More than one file descriptor was passed.");
is_unix = is_unix =
sd_is_socket(in_fd, AF_UNIX, 0, 0) > 0 && sd_is_socket(in_fd, AF_UNIX, 0, 0) > 0 &&
@@ -129,50 +139,50 @@ static int run(int argc, char *argv[]) {
r = sd_bus_new(&a); r = sd_bus_new(&a);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to allocate bus: %m"); return log_full_errno(priority, r, "Failed to allocate bus: %m");
if (arg_transport == BUS_TRANSPORT_MACHINE) if (arg_transport == BUS_TRANSPORT_MACHINE)
r = bus_set_address_machine(a, arg_runtime_scope, arg_bus_path); r = bus_set_address_machine(a, arg_runtime_scope, arg_bus_path);
else else
r = sd_bus_set_address(a, arg_bus_path); r = sd_bus_set_address(a, arg_bus_path);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to set address to connect to: %m"); return log_full_errno(priority, r, "Failed to set address to connect to: %m");
r = sd_bus_negotiate_fds(a, is_unix); r = sd_bus_negotiate_fds(a, is_unix);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to set FD negotiation: %m"); return log_full_errno(priority, r, "Failed to set FD negotiation: %m");
r = sd_bus_start(a); r = sd_bus_start(a);
if (r < 0) if (r < 0)
return bus_log_connect_error(r, arg_transport, arg_runtime_scope); return bus_log_connect_full(priority, r, arg_transport, arg_runtime_scope);
r = sd_bus_get_bus_id(a, &server_id); r = sd_bus_get_bus_id(a, &server_id);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to get server ID: %m"); return log_full_errno(priority, r, "Failed to get server ID: %m");
r = sd_bus_new(&b); r = sd_bus_new(&b);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to allocate bus: %m"); return log_full_errno(priority, r, "Failed to allocate bus: %m");
r = sd_bus_set_fd(b, in_fd, out_fd); r = sd_bus_set_fd(b, in_fd, out_fd);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to set fds: %m"); return log_full_errno(priority, r, "Failed to set fds: %m");
r = sd_bus_set_server(b, 1, server_id); r = sd_bus_set_server(b, 1, server_id);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to set server mode: %m"); return log_full_errno(priority, r, "Failed to set server mode: %m");
r = sd_bus_negotiate_fds(b, is_unix); r = sd_bus_negotiate_fds(b, is_unix);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to set FD negotiation: %m"); return log_full_errno(priority, r, "Failed to set FD negotiation: %m");
r = sd_bus_set_anonymous(b, true); r = sd_bus_set_anonymous(b, true);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to set anonymous authentication: %m"); return log_full_errno(priority, r, "Failed to set anonymous authentication: %m");
r = sd_bus_start(b); r = sd_bus_start(b);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to start bus forwarding server: %m"); return log_full_errno(priority, r, "Failed to start bus forwarding server: %m");
for (;;) { for (;;) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
@@ -185,14 +195,14 @@ static int run(int argc, char *argv[]) {
if (ERRNO_IS_NEG_DISCONNECT(r)) /* Treat 'connection reset by peer' as clean exit condition */ if (ERRNO_IS_NEG_DISCONNECT(r)) /* Treat 'connection reset by peer' as clean exit condition */
return 0; return 0;
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to process bus a: %m"); return log_full_errno(priority, r, "Failed to process bus a: %m");
if (m) { if (m) {
if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected"))
return 0; return 0;
r = sd_bus_send(b, m, NULL); r = sd_bus_send(b, m, NULL);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to send message: %m"); return log_full_errno(priority, r, "Failed to send message: %m");
} }
if (r > 0) if (r > 0)
@@ -202,14 +212,14 @@ static int run(int argc, char *argv[]) {
if (ERRNO_IS_NEG_DISCONNECT(r)) /* Treat 'connection reset by peer' as clean exit condition */ if (ERRNO_IS_NEG_DISCONNECT(r)) /* Treat 'connection reset by peer' as clean exit condition */
return 0; return 0;
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to process bus: %m"); return log_full_errno(priority, r, "Failed to process bus: %m");
if (m) { if (m) {
if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected"))
return 0; return 0;
r = sd_bus_send(a, m, NULL); r = sd_bus_send(a, m, NULL);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to send message: %m"); return log_full_errno(priority, r, "Failed to send message: %m");
} }
if (r > 0) if (r > 0)
@@ -217,23 +227,23 @@ static int run(int argc, char *argv[]) {
fd = sd_bus_get_fd(a); fd = sd_bus_get_fd(a);
if (fd < 0) if (fd < 0)
return log_error_errno(fd, "Failed to get fd: %m"); return log_full_errno(priority, fd, "Failed to get fd: %m");
events_a = sd_bus_get_events(a); events_a = sd_bus_get_events(a);
if (events_a < 0) if (events_a < 0)
return log_error_errno(events_a, "Failed to get events mask: %m"); return log_full_errno(priority, events_a, "Failed to get events mask: %m");
r = sd_bus_get_timeout(a, &timeout_a); r = sd_bus_get_timeout(a, &timeout_a);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to get timeout: %m"); return log_full_errno(priority, r, "Failed to get timeout: %m");
events_b = sd_bus_get_events(b); events_b = sd_bus_get_events(b);
if (events_b < 0) if (events_b < 0)
return log_error_errno(events_b, "Failed to get events mask: %m"); return log_full_errno(priority, events_b, "Failed to get events mask: %m");
r = sd_bus_get_timeout(b, &timeout_b); r = sd_bus_get_timeout(b, &timeout_b);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to get timeout: %m"); return log_full_errno(priority, r, "Failed to get timeout: %m");
t = usec_sub_unsigned(MIN(timeout_a, timeout_b), now(CLOCK_MONOTONIC)); t = usec_sub_unsigned(MIN(timeout_a, timeout_b), now(CLOCK_MONOTONIC));
@@ -245,7 +255,7 @@ static int run(int argc, char *argv[]) {
r = ppoll_usec(p, ELEMENTSOF(p), t); r = ppoll_usec(p, ELEMENTSOF(p), t);
if (r < 0 && !ERRNO_IS_TRANSIENT(r)) /* don't be bothered by signals, i.e. EINTR */ if (r < 0 && !ERRNO_IS_TRANSIENT(r)) /* don't be bothered by signals, i.e. EINTR */
return log_error_errno(r, "ppoll() failed: %m"); return log_full_errno(priority, r, "ppoll() failed: %m");
} }
} }