| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <poll.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-bus.h" |
| 7 |
#include "sd-daemon.h" |
| 8 |
|
| 9 |
#include "build.h" |
| 10 |
#include "bus-internal.h" |
| 11 |
#include "bus-util.h" |
| 12 |
#include "errno-util.h" |
| 13 |
#include "format-table.h" |
| 14 |
#include "io-util.h" |
| 15 |
#include "log.h" |
| 16 |
#include "main-func.h" |
| 17 |
#include "options.h" |
| 18 |
#include "parse-argument.h" |
| 19 |
#include "time-util.h" |
| 20 |
|
| 21 |
static const char *arg_bus_path = NULL; |
| 22 |
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
| 23 |
static RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM; |
| 24 |
static bool arg_quiet = false; |
| 25 |
|
| 26 |
static int help(void) { |
| 27 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 28 |
int r; |
| 29 |
|
| 30 |
r = option_parser_get_help_table(&options); |
| 31 |
if (r < 0) |
| 32 |
return r; |
| 33 |
|
| 34 |
printf("%s [OPTIONS...]\n" |
| 35 |
"\nForward messages between a pipe or socket and a D-Bus bus.\n\n", |
| 36 |
program_invocation_short_name); |
| 37 |
|
| 38 |
r = table_print_or_warn(options); |
| 39 |
if (r < 0) |
| 40 |
return r; |
| 41 |
|
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
static int parse_argv(int argc, char *argv[]) { |
| 46 |
assert(argc >= 0); |
| 47 |
assert(argv); |
| 48 |
|
| 49 |
OptionParser opts = { argc, argv }; |
| 50 |
int r; |
| 51 |
|
| 52 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 53 |
switch (c) { |
| 54 |
|
| 55 |
OPTION_COMMON_HELP: |
| 56 |
return help(); |
| 57 |
|
| 58 |
OPTION_COMMON_VERSION: |
| 59 |
return version(); |
| 60 |
|
| 61 |
OPTION_LONG("user", NULL, "Connect to user bus"): |
| 62 |
arg_runtime_scope = RUNTIME_SCOPE_USER; |
| 63 |
break; |
| 64 |
|
| 65 |
OPTION_LONG("system", NULL, "Connect to system bus"): |
| 66 |
arg_runtime_scope = RUNTIME_SCOPE_SYSTEM; |
| 67 |
break; |
| 68 |
|
| 69 |
OPTION('p', "bus-path", "PATH", |
| 70 |
"Path to the bus address (default: " DEFAULT_SYSTEM_BUS_ADDRESS ")"): |
| 71 |
arg_bus_path = opts.arg; |
| 72 |
break; |
| 73 |
|
| 74 |
OPTION_COMMON_MACHINE: |
| 75 |
r = parse_machine_argument(opts.arg, &arg_bus_path, &arg_transport); |
| 76 |
if (r < 0) |
| 77 |
return r; |
| 78 |
break; |
| 79 |
|
| 80 |
OPTION('q', "quiet", NULL, "Fail silently instead of logging errors"): |
| 81 |
arg_quiet = true; |
| 82 |
break; |
| 83 |
} |
| 84 |
|
| 85 |
if (option_parser_get_n_args(&opts) > 0) |
| 86 |
return log_full_errno(arg_quiet ? LOG_DEBUG : LOG_ERR, SYNTHETIC_ERRNO(EINVAL), |
| 87 |
"%s takes no arguments.", |
| 88 |
program_invocation_short_name); |
| 89 |
|
| 90 |
return 1; |
| 91 |
} |
| 92 |
|
| 93 |
static int bus_set_address( |
| 94 |
sd_bus *bus, |
| 95 |
BusTransport transport, |
| 96 |
const char *bus_path, |
| 97 |
RuntimeScope runtime_scope) { |
| 98 |
|
| 99 |
assert(bus); |
| 100 |
|
| 101 |
switch (transport) { |
| 102 |
|
| 103 |
case BUS_TRANSPORT_LOCAL: |
| 104 |
|
| 105 |
if (bus_path) |
| 106 |
return sd_bus_set_address(bus, bus_path); |
| 107 |
|
| 108 |
switch (runtime_scope) { |
| 109 |
|
| 110 |
case RUNTIME_SCOPE_USER: |
| 111 |
return bus_set_address_user(bus); |
| 112 |
|
| 113 |
case RUNTIME_SCOPE_SYSTEM: |
| 114 |
return bus_set_address_system(bus); |
| 115 |
|
| 116 |
default: |
| 117 |
assert_not_reached(); |
| 118 |
} |
| 119 |
|
| 120 |
case BUS_TRANSPORT_MACHINE: |
| 121 |
return bus_set_address_machine(bus, runtime_scope, bus_path); |
| 122 |
|
| 123 |
default: |
| 124 |
assert_not_reached(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
static int run(int argc, char *argv[]) { |
| 129 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *a = NULL, *b = NULL; |
| 130 |
sd_id128_t server_id; |
| 131 |
bool is_unix; |
| 132 |
int in_fd, out_fd, r; |
| 133 |
|
| 134 |
log_setup(); |
| 135 |
|
| 136 |
r = parse_argv(argc, argv); |
| 137 |
if (r <= 0) |
| 138 |
return r; |
| 139 |
|
| 140 |
int priority = arg_quiet ? LOG_DEBUG : LOG_ERR; |
| 141 |
|
| 142 |
r = sd_listen_fds(0); |
| 143 |
if (r == 0) { |
| 144 |
in_fd = STDIN_FILENO; |
| 145 |
out_fd = STDOUT_FILENO; |
| 146 |
} else if (r == 1) { |
| 147 |
in_fd = SD_LISTEN_FDS_START; |
| 148 |
out_fd = SD_LISTEN_FDS_START; |
| 149 |
} else |
| 150 |
return log_full_errno(priority, SYNTHETIC_ERRNO(EINVAL), |
| 151 |
"More than one file descriptor was passed."); |
| 152 |
|
| 153 |
is_unix = |
| 154 |
sd_is_socket(in_fd, AF_UNIX, 0, 0) > 0 && |
| 155 |
sd_is_socket(out_fd, AF_UNIX, 0, 0) > 0; |
| 156 |
|
| 157 |
r = sd_bus_new(&a); |
| 158 |
if (r < 0) |
| 159 |
return log_full_errno(priority, r, "Failed to allocate bus: %m"); |
| 160 |
|
| 161 |
r = bus_set_address(a, arg_transport, arg_bus_path, arg_runtime_scope); |
| 162 |
if (r < 0) |
| 163 |
return log_full_errno(priority, r, "Failed to set address to connect to: %m"); |
| 164 |
|
| 165 |
r = sd_bus_negotiate_fds(a, is_unix); |
| 166 |
if (r < 0) |
| 167 |
return log_full_errno(priority, r, "Failed to set FD negotiation: %m"); |
| 168 |
|
| 169 |
r = sd_bus_start(a); |
| 170 |
if (r < 0) |
| 171 |
return bus_log_connect_full(priority, r, arg_transport, arg_runtime_scope); |
| 172 |
|
| 173 |
r = sd_bus_get_bus_id(a, &server_id); |
| 174 |
if (r < 0) |
| 175 |
return log_full_errno(priority, r, "Failed to get server ID: %m"); |
| 176 |
|
| 177 |
r = sd_bus_new(&b); |
| 178 |
if (r < 0) |
| 179 |
return log_full_errno(priority, r, "Failed to allocate bus: %m"); |
| 180 |
|
| 181 |
r = sd_bus_set_fd(b, in_fd, out_fd); |
| 182 |
if (r < 0) |
| 183 |
return log_full_errno(priority, r, "Failed to set fds: %m"); |
| 184 |
|
| 185 |
r = sd_bus_set_server(b, 1, server_id); |
| 186 |
if (r < 0) |
| 187 |
return log_full_errno(priority, r, "Failed to set server mode: %m"); |
| 188 |
|
| 189 |
r = sd_bus_negotiate_fds(b, is_unix); |
| 190 |
if (r < 0) |
| 191 |
return log_full_errno(priority, r, "Failed to set FD negotiation: %m"); |
| 192 |
|
| 193 |
r = sd_bus_set_anonymous(b, true); |
| 194 |
if (r < 0) |
| 195 |
return log_full_errno(priority, r, "Failed to set anonymous authentication: %m"); |
| 196 |
|
| 197 |
r = sd_bus_start(b); |
| 198 |
if (r < 0) |
| 199 |
return log_full_errno(priority, r, "Failed to start bus forwarding server: %m"); |
| 200 |
|
| 201 |
for (;;) { |
| 202 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 203 |
int events_a, events_b, fd; |
| 204 |
usec_t timeout_a, timeout_b, t; |
| 205 |
|
| 206 |
assert_cc(sizeof(usec_t) == sizeof(uint64_t)); |
| 207 |
|
| 208 |
r = sd_bus_process(a, &m); |
| 209 |
if (ERRNO_IS_NEG_DISCONNECT(r)) /* Treat 'connection reset by peer' as clean exit condition */ |
| 210 |
return 0; |
| 211 |
if (r < 0) |
| 212 |
return log_full_errno(priority, r, "Failed to process bus a: %m"); |
| 213 |
if (m) { |
| 214 |
if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) |
| 215 |
return 0; |
| 216 |
|
| 217 |
r = sd_bus_send(b, m, NULL); |
| 218 |
if (r < 0) |
| 219 |
return log_full_errno(priority, r, "Failed to send message: %m"); |
| 220 |
} |
| 221 |
|
| 222 |
if (r > 0) |
| 223 |
continue; |
| 224 |
|
| 225 |
r = sd_bus_process(b, &m); |
| 226 |
if (ERRNO_IS_NEG_DISCONNECT(r)) /* Treat 'connection reset by peer' as clean exit condition */ |
| 227 |
return 0; |
| 228 |
if (r < 0) |
| 229 |
return log_full_errno(priority, r, "Failed to process bus: %m"); |
| 230 |
if (m) { |
| 231 |
if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) |
| 232 |
return 0; |
| 233 |
|
| 234 |
r = sd_bus_send(a, m, NULL); |
| 235 |
if (r < 0) |
| 236 |
return log_full_errno(priority, r, "Failed to send message: %m"); |
| 237 |
} |
| 238 |
|
| 239 |
if (r > 0) |
| 240 |
continue; |
| 241 |
|
| 242 |
fd = sd_bus_get_fd(a); |
| 243 |
if (fd < 0) |
| 244 |
return log_full_errno(priority, fd, "Failed to get fd: %m"); |
| 245 |
|
| 246 |
events_a = sd_bus_get_events(a); |
| 247 |
if (events_a < 0) |
| 248 |
return log_full_errno(priority, events_a, "Failed to get events mask: %m"); |
| 249 |
|
| 250 |
r = sd_bus_get_timeout(a, &timeout_a); |
| 251 |
if (r < 0) |
| 252 |
return log_full_errno(priority, r, "Failed to get timeout: %m"); |
| 253 |
|
| 254 |
events_b = sd_bus_get_events(b); |
| 255 |
if (events_b < 0) |
| 256 |
return log_full_errno(priority, events_b, "Failed to get events mask: %m"); |
| 257 |
|
| 258 |
r = sd_bus_get_timeout(b, &timeout_b); |
| 259 |
if (r < 0) |
| 260 |
return log_full_errno(priority, r, "Failed to get timeout: %m"); |
| 261 |
|
| 262 |
t = usec_sub_unsigned(MIN(timeout_a, timeout_b), now(CLOCK_MONOTONIC)); |
| 263 |
|
| 264 |
struct pollfd p[3] = { |
| 265 |
{ .fd = fd, .events = events_a }, |
| 266 |
{ .fd = in_fd, .events = events_b & POLLIN }, |
| 267 |
{ .fd = out_fd, .events = events_b & POLLOUT }, |
| 268 |
}; |
| 269 |
|
| 270 |
r = ppoll_usec(p, ELEMENTSOF(p), t); |
| 271 |
if (r < 0 && !ERRNO_IS_TRANSIENT(r)) /* don't be bothered by signals, i.e. EINTR */ |
| 272 |
return log_full_errno(priority, r, "ppoll() failed: %m"); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
DEFINE_MAIN_FUNCTION(run); |