]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/stdio-bridge/stdio-bridge.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / stdio-bridge / stdio-bridge.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
13 #include "alloc-util.h"
14 #include "build.h"
15 #include "bus-internal.h"
16 #include "bus-util.h"
17 #include "log.h"
18 #include "main-func.h"
19 #include "util.h"
20
21 #define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
22
23 static const char *arg_bus_path = DEFAULT_BUS_PATH;
24 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
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"
32 " -p --bus-path=PATH Path to the kernel bus (default: %s)\n"
33 " -M --machine=MACHINE Name of machine to connect to\n",
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,
43 ARG_MACHINE,
44 };
45
46 static const struct option options[] = {
47 { "help", no_argument, NULL, 'h' },
48 { "version", no_argument, NULL, ARG_VERSION },
49 { "bus-path", required_argument, NULL, 'p' },
50 { "machine", required_argument, NULL, 'M' },
51 {},
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;
75
76 break;
77
78 case 'M':
79 arg_bus_path = optarg;
80
81 arg_transport = BUS_TRANSPORT_MACHINE;
82
83 break;
84 default:
85 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
86 "Unknown option code %c", c);
87 }
88 }
89
90 return 1;
91 }
92
93 static int run(int argc, char *argv[]) {
94 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *a = NULL, *b = NULL;
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)
105 return r;
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 {
115 log_error("Illegal number of file descriptors passed.");
116 return -EINVAL;
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);
124 if (r < 0)
125 return log_error_errno(r, "Failed to allocate bus: %m");
126
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);
131 if (r < 0)
132 return log_error_errno(r, "Failed to set address to connect to: %m");
133
134 r = sd_bus_negotiate_fds(a, is_unix);
135 if (r < 0)
136 return log_error_errno(r, "Failed to set FD negotiation: %m");
137
138 r = sd_bus_start(a);
139 if (r < 0)
140 return log_error_errno(r, "Failed to start bus client: %m");
141
142 r = sd_bus_get_bus_id(a, &server_id);
143 if (r < 0)
144 return log_error_errno(r, "Failed to get server ID: %m");
145
146 r = sd_bus_new(&b);
147 if (r < 0)
148 return log_error_errno(r, "Failed to allocate bus: %m");
149
150 r = sd_bus_set_fd(b, in_fd, out_fd);
151 if (r < 0)
152 return log_error_errno(r, "Failed to set fds: %m");
153
154 r = sd_bus_set_server(b, 1, server_id);
155 if (r < 0)
156 return log_error_errno(r, "Failed to set server mode: %m");
157
158 r = sd_bus_negotiate_fds(b, is_unix);
159 if (r < 0)
160 return log_error_errno(r, "Failed to set FD negotiation: %m");
161
162 r = sd_bus_set_anonymous(b, true);
163 if (r < 0)
164 return log_error_errno(r, "Failed to set anonymous authentication: %m");
165
166 r = sd_bus_start(b);
167 if (r < 0)
168 return log_error_errno(r, "Failed to start bus client: %m");
169
170 for (;;) {
171 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
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);
177 if (r < 0)
178 return log_error_errno(r, "Failed to process bus a: %m");
179
180 if (m) {
181 r = sd_bus_send(b, m, NULL);
182 if (r < 0)
183 return log_error_errno(r, "Failed to send message: %m");
184 }
185
186 if (r > 0)
187 continue;
188
189 r = sd_bus_process(b, &m);
190 if (r < 0)
191 /* treat 'connection reset by peer' as clean exit condition */
192 return r == -ECONNRESET ? 0 : r;
193
194 if (m) {
195 r = sd_bus_send(a, m, NULL);
196 if (r < 0)
197 return log_error_errno(r, "Failed to send message: %m");
198 }
199
200 if (r > 0)
201 continue;
202
203 fd = sd_bus_get_fd(a);
204 if (fd < 0)
205 return log_error_errno(fd, "Failed to get fd: %m");
206
207 events_a = sd_bus_get_events(a);
208 if (events_a < 0)
209 return log_error_errno(events_a, "Failed to get events mask: %m");
210
211 r = sd_bus_get_timeout(a, &timeout_a);
212 if (r < 0)
213 return log_error_errno(r, "Failed to get timeout: %m");
214
215 events_b = sd_bus_get_events(b);
216 if (events_b < 0)
217 return log_error_errno(events_b, "Failed to get events mask: %m");
218
219 r = sd_bus_get_timeout(b, &timeout_b);
220 if (r < 0)
221 return log_error_errno(r, "Failed to get timeout: %m");
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 }
249 if (r < 0)
250 return log_error_errno(errno, "ppoll() failed: %m");
251 }
252
253 return 0;
254 }
255
256 DEFINE_MAIN_FUNCTION(run);