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