1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "sd-daemon.h"
12 #include "alloc-util.h"
14 #include "bus-internal.h"
16 #include "errno-util.h"
19 #include "main-func.h"
22 #define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
24 static const char *arg_bus_path
= DEFAULT_BUS_PATH
;
25 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
26 static RuntimeScope arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
28 static int help(void) {
29 printf("%s [OPTIONS...]\n\n"
30 "Forward messages between a pipe or socket and a D-Bus bus.\n\n"
31 " -h --help Show this help\n"
32 " --version Show package version\n"
33 " -p --bus-path=PATH Path to the bus address (default: %s)\n"
34 " --system Connect to system bus\n"
35 " --user Connect to user bus\n"
36 " -M --machine=CONTAINER Name of local container to connect to\n",
37 program_invocation_short_name
, DEFAULT_BUS_PATH
);
42 static int parse_argv(int argc
, char *argv
[]) {
50 static const struct option options
[] = {
51 { "help", no_argument
, NULL
, 'h' },
52 { "version", no_argument
, NULL
, ARG_VERSION
},
53 { "bus-path", required_argument
, NULL
, 'p' },
54 { "user", no_argument
, NULL
, ARG_USER
},
55 { "system", no_argument
, NULL
, ARG_SYSTEM
},
56 { "machine", required_argument
, NULL
, 'M' },
65 while ((c
= getopt_long(argc
, argv
, "hp:M:", options
, NULL
)) >= 0)
76 arg_runtime_scope
= RUNTIME_SCOPE_USER
;
80 arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
84 arg_bus_path
= optarg
;
88 arg_bus_path
= optarg
;
89 arg_transport
= BUS_TRANSPORT_MACHINE
;
96 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
97 "Unknown option code %c", c
);
103 static int run(int argc
, char *argv
[]) {
104 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*a
= NULL
, *b
= NULL
;
105 sd_id128_t server_id
;
107 int r
, in_fd
, out_fd
;
109 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG
);
110 log_parse_environment();
113 r
= parse_argv(argc
, argv
);
117 r
= sd_listen_fds(0);
119 in_fd
= STDIN_FILENO
;
120 out_fd
= STDOUT_FILENO
;
122 in_fd
= SD_LISTEN_FDS_START
;
123 out_fd
= SD_LISTEN_FDS_START
;
125 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "More than one file descriptor was passed.");
128 sd_is_socket(in_fd
, AF_UNIX
, 0, 0) > 0 &&
129 sd_is_socket(out_fd
, AF_UNIX
, 0, 0) > 0;
133 return log_error_errno(r
, "Failed to allocate bus: %m");
135 if (arg_transport
== BUS_TRANSPORT_MACHINE
)
136 r
= bus_set_address_machine(a
, arg_runtime_scope
, arg_bus_path
);
138 r
= sd_bus_set_address(a
, arg_bus_path
);
140 return log_error_errno(r
, "Failed to set address to connect to: %m");
142 r
= sd_bus_negotiate_fds(a
, is_unix
);
144 return log_error_errno(r
, "Failed to set FD negotiation: %m");
148 return log_error_errno(r
, "Failed to start bus client: %m");
150 r
= sd_bus_get_bus_id(a
, &server_id
);
152 return log_error_errno(r
, "Failed to get server ID: %m");
156 return log_error_errno(r
, "Failed to allocate bus: %m");
158 r
= sd_bus_set_fd(b
, in_fd
, out_fd
);
160 return log_error_errno(r
, "Failed to set fds: %m");
162 r
= sd_bus_set_server(b
, 1, server_id
);
164 return log_error_errno(r
, "Failed to set server mode: %m");
166 r
= sd_bus_negotiate_fds(b
, is_unix
);
168 return log_error_errno(r
, "Failed to set FD negotiation: %m");
170 r
= sd_bus_set_anonymous(b
, true);
172 return log_error_errno(r
, "Failed to set anonymous authentication: %m");
176 return log_error_errno(r
, "Failed to start bus client: %m");
179 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
180 int events_a
, events_b
, fd
;
181 usec_t timeout_a
, timeout_b
, t
;
183 assert_cc(sizeof(usec_t
) == sizeof(uint64_t));
185 r
= sd_bus_process(a
, &m
);
187 return log_error_errno(r
, "Failed to process bus a: %m");
190 r
= sd_bus_send(b
, m
, NULL
);
192 return log_error_errno(r
, "Failed to send message: %m");
198 r
= sd_bus_process(b
, &m
);
200 /* treat 'connection reset by peer' as clean exit condition */
201 if (ERRNO_IS_DISCONNECT(r
))
204 return log_error_errno(r
, "Failed to process bus: %m");
208 r
= sd_bus_send(a
, m
, NULL
);
210 return log_error_errno(r
, "Failed to send message: %m");
216 fd
= sd_bus_get_fd(a
);
218 return log_error_errno(fd
, "Failed to get fd: %m");
220 events_a
= sd_bus_get_events(a
);
222 return log_error_errno(events_a
, "Failed to get events mask: %m");
224 r
= sd_bus_get_timeout(a
, &timeout_a
);
226 return log_error_errno(r
, "Failed to get timeout: %m");
228 events_b
= sd_bus_get_events(b
);
230 return log_error_errno(events_b
, "Failed to get events mask: %m");
232 r
= sd_bus_get_timeout(b
, &timeout_b
);
234 return log_error_errno(r
, "Failed to get timeout: %m");
236 t
= usec_sub_unsigned(MIN(timeout_a
, timeout_b
), now(CLOCK_MONOTONIC
));
238 struct pollfd p
[3] = {
239 { .fd
= fd
, .events
= events_a
},
240 { .fd
= STDIN_FILENO
, .events
= events_b
& POLLIN
},
241 { .fd
= STDOUT_FILENO
, .events
= events_b
& POLLOUT
},
244 r
= ppoll_usec(p
, ELEMENTSOF(p
), t
);
246 if (ERRNO_IS_TRANSIENT(r
)) /* don't be bothered by signals, i.e. EINTR */
248 return log_error_errno(r
, "ppoll() failed: %m");
255 DEFINE_MAIN_FUNCTION(run
);