]>
Commit | Line | Data |
---|---|---|
a032b68d | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
aa27b158 MP |
2 | |
3 | #include <errno.h> | |
4 | #include <getopt.h> | |
aa27b158 MP |
5 | #include <stddef.h> |
6 | #include <string.h> | |
7 | #include <unistd.h> | |
8 | ||
9 | #include "sd-bus.h" | |
10 | #include "sd-daemon.h" | |
11 | ||
6e866b33 | 12 | #include "alloc-util.h" |
aa27b158 MP |
13 | #include "bus-internal.h" |
14 | #include "bus-util.h" | |
bb4f798a | 15 | #include "errno-util.h" |
3a6ce677 | 16 | #include "io-util.h" |
aa27b158 | 17 | #include "log.h" |
6e866b33 | 18 | #include "main-func.h" |
aa27b158 | 19 | #include "util.h" |
8b3d4ff0 | 20 | #include "version.h" |
aa27b158 MP |
21 | |
22 | #define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket" | |
23 | ||
6e866b33 MB |
24 | static const char *arg_bus_path = DEFAULT_BUS_PATH; |
25 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; | |
3a6ce677 | 26 | static bool arg_user = false; |
aa27b158 MP |
27 | |
28 | static int help(void) { | |
29 | ||
30 | printf("%s [OPTIONS...]\n\n" | |
31 | "STDIO or socket-activatable proxy to a given DBus endpoint.\n\n" | |
32 | " -h --help Show this help\n" | |
33 | " --version Show package version\n" | |
3a6ce677 BR |
34 | " -p --bus-path=PATH Path to the bus address (default: %s)\n" |
35 | " --system Connect to system bus\n" | |
36 | " --user Connect to user bus\n" | |
37 | " -M --machine=CONTAINER Name of local container to connect to\n", | |
aa27b158 MP |
38 | program_invocation_short_name, DEFAULT_BUS_PATH); |
39 | ||
40 | return 0; | |
41 | } | |
42 | ||
43 | static int parse_argv(int argc, char *argv[]) { | |
44 | ||
45 | enum { | |
46 | ARG_VERSION = 0x100, | |
6e866b33 | 47 | ARG_MACHINE, |
3a6ce677 BR |
48 | ARG_USER, |
49 | ARG_SYSTEM, | |
aa27b158 MP |
50 | }; |
51 | ||
52 | static const struct option options[] = { | |
52ad194e MB |
53 | { "help", no_argument, NULL, 'h' }, |
54 | { "version", no_argument, NULL, ARG_VERSION }, | |
55 | { "bus-path", required_argument, NULL, 'p' }, | |
3a6ce677 BR |
56 | { "user", no_argument, NULL, ARG_USER }, |
57 | { "system", no_argument, NULL, ARG_SYSTEM }, | |
6e866b33 | 58 | { "machine", required_argument, NULL, 'M' }, |
52ad194e | 59 | {}, |
aa27b158 MP |
60 | }; |
61 | ||
62 | int c; | |
63 | ||
64 | assert(argc >= 0); | |
65 | assert(argv); | |
66 | ||
bb4f798a | 67 | while ((c = getopt_long(argc, argv, "hp:M:", options, NULL)) >= 0) { |
aa27b158 MP |
68 | |
69 | switch (c) { | |
70 | ||
71 | case 'h': | |
bb4f798a | 72 | return help(); |
aa27b158 MP |
73 | |
74 | case ARG_VERSION: | |
75 | return version(); | |
76 | ||
3a6ce677 BR |
77 | case ARG_USER: |
78 | arg_user = true; | |
79 | break; | |
80 | ||
81 | case ARG_SYSTEM: | |
82 | arg_user = false; | |
83 | break; | |
84 | ||
aa27b158 MP |
85 | case 'p': |
86 | arg_bus_path = optarg; | |
87 | break; | |
88 | ||
6e866b33 MB |
89 | case 'M': |
90 | arg_bus_path = optarg; | |
6e866b33 | 91 | arg_transport = BUS_TRANSPORT_MACHINE; |
6e866b33 | 92 | break; |
bb4f798a MB |
93 | |
94 | case '?': | |
95 | return -EINVAL; | |
96 | ||
aa27b158 | 97 | default: |
6e866b33 MB |
98 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
99 | "Unknown option code %c", c); | |
aa27b158 MP |
100 | } |
101 | } | |
102 | ||
103 | return 1; | |
104 | } | |
105 | ||
6e866b33 | 106 | static int run(int argc, char *argv[]) { |
7c20daf6 | 107 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *a = NULL, *b = NULL; |
aa27b158 MP |
108 | sd_id128_t server_id; |
109 | bool is_unix; | |
110 | int r, in_fd, out_fd; | |
111 | ||
112 | log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); | |
113 | log_parse_environment(); | |
114 | log_open(); | |
115 | ||
116 | r = parse_argv(argc, argv); | |
117 | if (r <= 0) | |
6e866b33 | 118 | return r; |
aa27b158 MP |
119 | |
120 | r = sd_listen_fds(0); | |
121 | if (r == 0) { | |
122 | in_fd = STDIN_FILENO; | |
123 | out_fd = STDOUT_FILENO; | |
124 | } else if (r == 1) { | |
125 | in_fd = SD_LISTEN_FDS_START; | |
126 | out_fd = SD_LISTEN_FDS_START; | |
bb4f798a MB |
127 | } else |
128 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Illegal number of file descriptors passed."); | |
aa27b158 MP |
129 | |
130 | is_unix = | |
131 | sd_is_socket(in_fd, AF_UNIX, 0, 0) > 0 && | |
132 | sd_is_socket(out_fd, AF_UNIX, 0, 0) > 0; | |
133 | ||
134 | r = sd_bus_new(&a); | |
6e866b33 MB |
135 | if (r < 0) |
136 | return log_error_errno(r, "Failed to allocate bus: %m"); | |
aa27b158 | 137 | |
6e866b33 | 138 | if (arg_transport == BUS_TRANSPORT_MACHINE) |
3a6ce677 | 139 | r = bus_set_address_machine(a, arg_user, arg_bus_path); |
6e866b33 MB |
140 | else |
141 | r = sd_bus_set_address(a, arg_bus_path); | |
142 | if (r < 0) | |
143 | return log_error_errno(r, "Failed to set address to connect to: %m"); | |
aa27b158 MP |
144 | |
145 | r = sd_bus_negotiate_fds(a, is_unix); | |
6e866b33 MB |
146 | if (r < 0) |
147 | return log_error_errno(r, "Failed to set FD negotiation: %m"); | |
aa27b158 MP |
148 | |
149 | r = sd_bus_start(a); | |
6e866b33 MB |
150 | if (r < 0) |
151 | return log_error_errno(r, "Failed to start bus client: %m"); | |
aa27b158 MP |
152 | |
153 | r = sd_bus_get_bus_id(a, &server_id); | |
6e866b33 MB |
154 | if (r < 0) |
155 | return log_error_errno(r, "Failed to get server ID: %m"); | |
aa27b158 MP |
156 | |
157 | r = sd_bus_new(&b); | |
6e866b33 MB |
158 | if (r < 0) |
159 | return log_error_errno(r, "Failed to allocate bus: %m"); | |
aa27b158 MP |
160 | |
161 | r = sd_bus_set_fd(b, in_fd, out_fd); | |
6e866b33 MB |
162 | if (r < 0) |
163 | return log_error_errno(r, "Failed to set fds: %m"); | |
aa27b158 MP |
164 | |
165 | r = sd_bus_set_server(b, 1, server_id); | |
6e866b33 MB |
166 | if (r < 0) |
167 | return log_error_errno(r, "Failed to set server mode: %m"); | |
aa27b158 MP |
168 | |
169 | r = sd_bus_negotiate_fds(b, is_unix); | |
6e866b33 MB |
170 | if (r < 0) |
171 | return log_error_errno(r, "Failed to set FD negotiation: %m"); | |
aa27b158 MP |
172 | |
173 | r = sd_bus_set_anonymous(b, true); | |
6e866b33 MB |
174 | if (r < 0) |
175 | return log_error_errno(r, "Failed to set anonymous authentication: %m"); | |
aa27b158 MP |
176 | |
177 | r = sd_bus_start(b); | |
6e866b33 MB |
178 | if (r < 0) |
179 | return log_error_errno(r, "Failed to start bus client: %m"); | |
aa27b158 MP |
180 | |
181 | for (;;) { | |
81c58355 | 182 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
aa27b158 | 183 | int events_a, events_b, fd; |
3a6ce677 BR |
184 | usec_t timeout_a, timeout_b, t; |
185 | ||
186 | assert_cc(sizeof(usec_t) == sizeof(uint64_t)); | |
aa27b158 MP |
187 | |
188 | r = sd_bus_process(a, &m); | |
6e866b33 MB |
189 | if (r < 0) |
190 | return log_error_errno(r, "Failed to process bus a: %m"); | |
aa27b158 MP |
191 | |
192 | if (m) { | |
193 | r = sd_bus_send(b, m, NULL); | |
6e866b33 MB |
194 | if (r < 0) |
195 | return log_error_errno(r, "Failed to send message: %m"); | |
aa27b158 MP |
196 | } |
197 | ||
198 | if (r > 0) | |
199 | continue; | |
200 | ||
201 | r = sd_bus_process(b, &m); | |
bb4f798a | 202 | if (r < 0) { |
aa27b158 | 203 | /* treat 'connection reset by peer' as clean exit condition */ |
bb4f798a MB |
204 | if (ERRNO_IS_DISCONNECT(r)) |
205 | return 0; | |
206 | ||
207 | return log_error_errno(r, "Failed to process bus: %m"); | |
208 | } | |
aa27b158 MP |
209 | |
210 | if (m) { | |
211 | r = sd_bus_send(a, m, NULL); | |
6e866b33 MB |
212 | if (r < 0) |
213 | return log_error_errno(r, "Failed to send message: %m"); | |
aa27b158 MP |
214 | } |
215 | ||
216 | if (r > 0) | |
217 | continue; | |
218 | ||
219 | fd = sd_bus_get_fd(a); | |
6e866b33 MB |
220 | if (fd < 0) |
221 | return log_error_errno(fd, "Failed to get fd: %m"); | |
aa27b158 MP |
222 | |
223 | events_a = sd_bus_get_events(a); | |
6e866b33 MB |
224 | if (events_a < 0) |
225 | return log_error_errno(events_a, "Failed to get events mask: %m"); | |
aa27b158 MP |
226 | |
227 | r = sd_bus_get_timeout(a, &timeout_a); | |
6e866b33 MB |
228 | if (r < 0) |
229 | return log_error_errno(r, "Failed to get timeout: %m"); | |
aa27b158 MP |
230 | |
231 | events_b = sd_bus_get_events(b); | |
6e866b33 MB |
232 | if (events_b < 0) |
233 | return log_error_errno(events_b, "Failed to get events mask: %m"); | |
aa27b158 MP |
234 | |
235 | r = sd_bus_get_timeout(b, &timeout_b); | |
6e866b33 MB |
236 | if (r < 0) |
237 | return log_error_errno(r, "Failed to get timeout: %m"); | |
aa27b158 | 238 | |
3a6ce677 | 239 | t = usec_sub_unsigned(MIN(timeout_a, timeout_b), now(CLOCK_MONOTONIC)); |
aa27b158 | 240 | |
a10f5d05 MB |
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 }, | |
245 | }; | |
246 | ||
3a6ce677 | 247 | r = ppoll_usec(p, ELEMENTSOF(p), t); |
6e866b33 | 248 | if (r < 0) |
3a6ce677 | 249 | return log_error_errno(r, "ppoll() failed: %m"); |
aa27b158 MP |
250 | } |
251 | ||
6e866b33 | 252 | return 0; |
aa27b158 | 253 | } |
6e866b33 MB |
254 | |
255 | DEFINE_MAIN_FUNCTION(run); |