1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "sd-daemon.h"
13 #include "alloc-util.h"
14 #include "bus-internal.h"
20 #define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
22 static const char *arg_bus_path
= DEFAULT_BUS_PATH
;
23 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
25 static int help(void) {
27 printf("%s [OPTIONS...]\n\n"
28 "STDIO or socket-activatable proxy to a given DBus endpoint.\n\n"
29 " -h --help Show this help\n"
30 " --version Show package version\n"
31 " -p --bus-path=PATH Path to the kernel bus (default: %s)\n"
32 " -M --machine=MACHINE Name of machine to connect to\n",
33 program_invocation_short_name
, DEFAULT_BUS_PATH
);
38 static int parse_argv(int argc
, char *argv
[]) {
45 static const struct option options
[] = {
46 { "help", no_argument
, NULL
, 'h' },
47 { "version", no_argument
, NULL
, ARG_VERSION
},
48 { "bus-path", required_argument
, NULL
, 'p' },
49 { "machine", required_argument
, NULL
, 'M' },
58 while ((c
= getopt_long(argc
, argv
, "hsup:", options
, NULL
)) >= 0) {
73 arg_bus_path
= optarg
;
78 arg_bus_path
= optarg
;
80 arg_transport
= BUS_TRANSPORT_MACHINE
;
84 log_error("Unknown option code %c", c
);
92 int main(int argc
, char *argv
[]) {
93 _cleanup_(sd_bus_unrefp
) sd_bus
*a
= NULL
, *b
= NULL
;
98 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG
);
99 log_parse_environment();
102 r
= parse_argv(argc
, argv
);
106 r
= sd_listen_fds(0);
108 in_fd
= STDIN_FILENO
;
109 out_fd
= STDOUT_FILENO
;
111 in_fd
= SD_LISTEN_FDS_START
;
112 out_fd
= SD_LISTEN_FDS_START
;
114 log_error("Illegal number of file descriptors passed.");
119 sd_is_socket(in_fd
, AF_UNIX
, 0, 0) > 0 &&
120 sd_is_socket(out_fd
, AF_UNIX
, 0, 0) > 0;
124 log_error_errno(r
, "Failed to allocate bus: %m");
128 if (arg_transport
== BUS_TRANSPORT_MACHINE
)
129 r
= bus_set_address_system_machine(a
, arg_bus_path
);
131 r
= sd_bus_set_address(a
, arg_bus_path
);
133 log_error_errno(r
, "Failed to set address to connect to: %m");
137 r
= sd_bus_negotiate_fds(a
, is_unix
);
139 log_error_errno(r
, "Failed to set FD negotiation: %m");
145 log_error_errno(r
, "Failed to start bus client: %m");
149 r
= sd_bus_get_bus_id(a
, &server_id
);
151 log_error_errno(r
, "Failed to get server ID: %m");
157 log_error_errno(r
, "Failed to allocate bus: %m");
161 r
= sd_bus_set_fd(b
, in_fd
, out_fd
);
163 log_error_errno(r
, "Failed to set fds: %m");
167 r
= sd_bus_set_server(b
, 1, server_id
);
169 log_error_errno(r
, "Failed to set server mode: %m");
173 r
= sd_bus_negotiate_fds(b
, is_unix
);
175 log_error_errno(r
, "Failed to set FD negotiation: %m");
179 r
= sd_bus_set_anonymous(b
, true);
181 log_error_errno(r
, "Failed to set anonymous authentication: %m");
187 log_error_errno(r
, "Failed to start bus client: %m");
192 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
193 int events_a
, events_b
, fd
;
194 uint64_t timeout_a
, timeout_b
, t
;
195 struct timespec _ts
, *ts
;
197 r
= sd_bus_process(a
, &m
);
199 log_error_errno(r
, "Failed to process bus a: %m");
204 r
= sd_bus_send(b
, m
, NULL
);
206 log_error_errno(r
, "Failed to send message: %m");
214 r
= sd_bus_process(b
, &m
);
216 /* treat 'connection reset by peer' as clean exit condition */
217 if (r
== -ECONNRESET
)
224 r
= sd_bus_send(a
, m
, NULL
);
226 log_error_errno(r
, "Failed to send message: %m");
234 fd
= sd_bus_get_fd(a
);
237 log_error_errno(r
, "Failed to get fd: %m");
241 events_a
= sd_bus_get_events(a
);
244 log_error_errno(r
, "Failed to get events mask: %m");
248 r
= sd_bus_get_timeout(a
, &timeout_a
);
250 log_error_errno(r
, "Failed to get timeout: %m");
254 events_b
= sd_bus_get_events(b
);
257 log_error_errno(r
, "Failed to get events mask: %m");
261 r
= sd_bus_get_timeout(b
, &timeout_b
);
263 log_error_errno(r
, "Failed to get timeout: %m");
268 if (t
== (uint64_t) -1 || (timeout_b
!= (uint64_t) -1 && timeout_b
< timeout_a
))
271 if (t
== (uint64_t) -1)
276 nw
= now(CLOCK_MONOTONIC
);
282 ts
= timespec_store(&_ts
, t
);
286 struct pollfd p
[3] = {
287 {.fd
= fd
, .events
= events_a
, },
288 {.fd
= STDIN_FILENO
, .events
= events_b
& POLLIN
, },
289 {.fd
= STDOUT_FILENO
, .events
= events_b
& POLLOUT
, }};
291 r
= ppoll(p
, ELEMENTSOF(p
), ts
, NULL
);
294 log_error_errno(errno
, "ppoll() failed: %m");
300 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;