]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
stdio-bridge: Add --quiet option
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 2 Nov 2025 12:52:15 +0000 (13:52 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 13 Nov 2025 13:45:51 +0000 (14:45 +0100)
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.

man/systemd-stdio-bridge.xml
src/stdio-bridge/stdio-bridge.c

index ae760cc039a29488ce83bfb459aea559e9e7da6f..1b73928725fd6be10441e218392b23f2bef66cf8 100644 (file)
           <xi:include href="version-info.xml" xpointer="v251"/></listitem>
       </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="version" />
     </variablelist>
index 804762ab2e84edea92a320445d637c3f9f96a793..a131e2afe93fec00333847faa568ea159fde037e 100644 (file)
@@ -20,6 +20,7 @@
 static const char *arg_bus_path = DEFAULT_SYSTEM_BUS_ADDRESS;
 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
 static RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
+static bool arg_quiet = false;
 
 static int help(void) {
         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"
                "     --system            Connect to system 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);
 
         return 0;
@@ -50,10 +52,11 @@ static int parse_argv(int argc, char *argv[]) {
                 { "user",            no_argument,       NULL, ARG_USER    },
                 { "system",          no_argument,       NULL, ARG_SYSTEM  },
                 { "machine",         required_argument, NULL, 'M'         },
+                { "quiet",           no_argument,       NULL, 'q'         },
                 {},
         };
 
-        int r, c;
+        int c, r;
 
         assert(argc >= 0);
         assert(argv);
@@ -86,6 +89,10 @@ static int parse_argv(int argc, char *argv[]) {
                                 return r;
                         break;
 
+                case 'q':
+                        arg_quiet = true;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -94,9 +101,9 @@ static int parse_argv(int argc, char *argv[]) {
                 }
 
         if (argc > optind)
-                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
-                                       "%s takes no arguments.",
-                                       program_invocation_short_name);
+                return log_full_errno(arg_quiet ? LOG_DEBUG : LOG_ERR, SYNTHETIC_ERRNO(EINVAL),
+                                      "%s takes no arguments.",
+                                      program_invocation_short_name);
 
         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;
         sd_id128_t server_id;
         bool is_unix;
-        int r, in_fd, out_fd;
+        int in_fd, out_fd, r;
 
         log_setup();
 
@@ -113,6 +120,8 @@ static int run(int argc, char *argv[]) {
         if (r <= 0)
                 return r;
 
+        int priority = arg_quiet ? LOG_DEBUG : LOG_ERR;
+
         r = sd_listen_fds(0);
         if (r == 0) {
                 in_fd = STDIN_FILENO;
@@ -121,7 +130,8 @@ static int run(int argc, char *argv[]) {
                 in_fd = SD_LISTEN_FDS_START;
                 out_fd = SD_LISTEN_FDS_START;
         } 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 =
                 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);
         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)
                 r = bus_set_address_machine(a, arg_runtime_scope, arg_bus_path);
         else
                 r = sd_bus_set_address(a, arg_bus_path);
         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);
         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);
         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);
         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);
         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);
         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);
         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);
         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);
         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);
         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 (;;) {
                 _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 */
                         return 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 (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected"))
                                 return 0;
 
                         r = sd_bus_send(b, m, NULL);
                         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)
@@ -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 */
                         return 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 (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected"))
                                 return 0;
 
                         r = sd_bus_send(a, m, NULL);
                         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)
@@ -217,23 +227,23 @@ static int run(int argc, char *argv[]) {
 
                 fd = sd_bus_get_fd(a);
                 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);
                 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);
                 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);
                 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);
                 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));
 
@@ -245,7 +255,7 @@ static int run(int argc, char *argv[]) {
 
                 r = ppoll_usec(p, ELEMENTSOF(p), t);
                 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");
         }
 }