1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "sd-daemon.h"
13 #include "alloc-util.h"
15 #include "bus-internal.h"
17 #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
;
27 static int help(void) {
29 printf("%s [OPTIONS...]\n\n"
30 "STDIO or socket-activatable proxy to a given DBus endpoint.\n\n"
31 " -h --help Show this help\n"
32 " --version Show package version\n"
33 " -p --bus-path=PATH Path to the kernel bus (default: %s)\n"
34 " -M --machine=MACHINE Name of machine to connect to\n",
35 program_invocation_short_name
, DEFAULT_BUS_PATH
);
40 static int parse_argv(int argc
, char *argv
[]) {
47 static const struct option options
[] = {
48 { "help", no_argument
, NULL
, 'h' },
49 { "version", no_argument
, NULL
, ARG_VERSION
},
50 { "bus-path", required_argument
, NULL
, 'p' },
51 { "machine", required_argument
, NULL
, 'M' },
60 while ((c
= getopt_long(argc
, argv
, "hp:M:", options
, NULL
)) >= 0) {
71 arg_bus_path
= optarg
;
75 arg_bus_path
= optarg
;
76 arg_transport
= BUS_TRANSPORT_MACHINE
;
83 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
84 "Unknown option code %c", c
);
91 static int run(int argc
, char *argv
[]) {
92 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*a
= NULL
, *b
= NULL
;
97 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG
);
98 log_parse_environment();
101 r
= parse_argv(argc
, argv
);
105 r
= sd_listen_fds(0);
107 in_fd
= STDIN_FILENO
;
108 out_fd
= STDOUT_FILENO
;
110 in_fd
= SD_LISTEN_FDS_START
;
111 out_fd
= SD_LISTEN_FDS_START
;
113 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Illegal number of file descriptors passed.");
116 sd_is_socket(in_fd
, AF_UNIX
, 0, 0) > 0 &&
117 sd_is_socket(out_fd
, AF_UNIX
, 0, 0) > 0;
121 return log_error_errno(r
, "Failed to allocate bus: %m");
123 if (arg_transport
== BUS_TRANSPORT_MACHINE
)
124 r
= bus_set_address_system_machine(a
, arg_bus_path
);
126 r
= sd_bus_set_address(a
, arg_bus_path
);
128 return log_error_errno(r
, "Failed to set address to connect to: %m");
130 r
= sd_bus_negotiate_fds(a
, is_unix
);
132 return log_error_errno(r
, "Failed to set FD negotiation: %m");
136 return log_error_errno(r
, "Failed to start bus client: %m");
138 r
= sd_bus_get_bus_id(a
, &server_id
);
140 return log_error_errno(r
, "Failed to get server ID: %m");
144 return log_error_errno(r
, "Failed to allocate bus: %m");
146 r
= sd_bus_set_fd(b
, in_fd
, out_fd
);
148 return log_error_errno(r
, "Failed to set fds: %m");
150 r
= sd_bus_set_server(b
, 1, server_id
);
152 return log_error_errno(r
, "Failed to set server mode: %m");
154 r
= sd_bus_negotiate_fds(b
, is_unix
);
156 return log_error_errno(r
, "Failed to set FD negotiation: %m");
158 r
= sd_bus_set_anonymous(b
, true);
160 return log_error_errno(r
, "Failed to set anonymous authentication: %m");
164 return log_error_errno(r
, "Failed to start bus client: %m");
167 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
168 int events_a
, events_b
, fd
;
169 uint64_t timeout_a
, timeout_b
, t
;
170 struct timespec _ts
, *ts
;
172 r
= sd_bus_process(a
, &m
);
174 return log_error_errno(r
, "Failed to process bus a: %m");
177 r
= sd_bus_send(b
, m
, NULL
);
179 return log_error_errno(r
, "Failed to send message: %m");
185 r
= sd_bus_process(b
, &m
);
187 /* treat 'connection reset by peer' as clean exit condition */
188 if (ERRNO_IS_DISCONNECT(r
))
191 return log_error_errno(r
, "Failed to process bus: %m");
195 r
= sd_bus_send(a
, m
, NULL
);
197 return log_error_errno(r
, "Failed to send message: %m");
203 fd
= sd_bus_get_fd(a
);
205 return log_error_errno(fd
, "Failed to get fd: %m");
207 events_a
= sd_bus_get_events(a
);
209 return log_error_errno(events_a
, "Failed to get events mask: %m");
211 r
= sd_bus_get_timeout(a
, &timeout_a
);
213 return log_error_errno(r
, "Failed to get timeout: %m");
215 events_b
= sd_bus_get_events(b
);
217 return log_error_errno(events_b
, "Failed to get events mask: %m");
219 r
= sd_bus_get_timeout(b
, &timeout_b
);
221 return log_error_errno(r
, "Failed to get timeout: %m");
224 if (t
== (uint64_t) -1 || (timeout_b
!= (uint64_t) -1 && timeout_b
< timeout_a
))
227 if (t
== (uint64_t) -1)
232 nw
= now(CLOCK_MONOTONIC
);
238 ts
= timespec_store(&_ts
, t
);
241 struct pollfd p
[3] = {
242 { .fd
= fd
, .events
= events_a
},
243 { .fd
= STDIN_FILENO
, .events
= events_b
& POLLIN
},
244 { .fd
= STDOUT_FILENO
, .events
= events_b
& POLLOUT
},
247 r
= ppoll(p
, ELEMENTSOF(p
), ts
, NULL
);
249 return log_error_errno(errno
, "ppoll() failed: %m");
250 if (p
[0].revents
& POLLNVAL
||
251 p
[1].revents
& POLLNVAL
||
252 p
[2].revents
& POLLNVAL
)
253 return log_error_errno(SYNTHETIC_ERRNO(EBADF
), "Invalid file descriptor to poll on?");
259 DEFINE_MAIN_FUNCTION(run
);