]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/stdio-bridge/stdio-bridge.c
Remove systemd-firstboot --force entry from TODO
[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 "errno-util.h"
18 #include "log.h"
19 #include "main-func.h"
20 #include "util.h"
21
22 #define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
23
24 static const char *arg_bus_path = DEFAULT_BUS_PATH;
25 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
26
27 static int help(void) {
28
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);
36
37 return 0;
38 }
39
40 static int parse_argv(int argc, char *argv[]) {
41
42 enum {
43 ARG_VERSION = 0x100,
44 ARG_MACHINE,
45 };
46
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' },
52 {},
53 };
54
55 int c;
56
57 assert(argc >= 0);
58 assert(argv);
59
60 while ((c = getopt_long(argc, argv, "hp:M:", options, NULL)) >= 0) {
61
62 switch (c) {
63
64 case 'h':
65 return help();
66
67 case ARG_VERSION:
68 return version();
69
70 case 'p':
71 arg_bus_path = optarg;
72 break;
73
74 case 'M':
75 arg_bus_path = optarg;
76 arg_transport = BUS_TRANSPORT_MACHINE;
77 break;
78
79 case '?':
80 return -EINVAL;
81
82 default:
83 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
84 "Unknown option code %c", c);
85 }
86 }
87
88 return 1;
89 }
90
91 static int run(int argc, char *argv[]) {
92 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *a = NULL, *b = NULL;
93 sd_id128_t server_id;
94 bool is_unix;
95 int r, in_fd, out_fd;
96
97 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
98 log_parse_environment();
99 log_open();
100
101 r = parse_argv(argc, argv);
102 if (r <= 0)
103 return r;
104
105 r = sd_listen_fds(0);
106 if (r == 0) {
107 in_fd = STDIN_FILENO;
108 out_fd = STDOUT_FILENO;
109 } else if (r == 1) {
110 in_fd = SD_LISTEN_FDS_START;
111 out_fd = SD_LISTEN_FDS_START;
112 } else
113 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Illegal number of file descriptors passed.");
114
115 is_unix =
116 sd_is_socket(in_fd, AF_UNIX, 0, 0) > 0 &&
117 sd_is_socket(out_fd, AF_UNIX, 0, 0) > 0;
118
119 r = sd_bus_new(&a);
120 if (r < 0)
121 return log_error_errno(r, "Failed to allocate bus: %m");
122
123 if (arg_transport == BUS_TRANSPORT_MACHINE)
124 r = bus_set_address_system_machine(a, arg_bus_path);
125 else
126 r = sd_bus_set_address(a, arg_bus_path);
127 if (r < 0)
128 return log_error_errno(r, "Failed to set address to connect to: %m");
129
130 r = sd_bus_negotiate_fds(a, is_unix);
131 if (r < 0)
132 return log_error_errno(r, "Failed to set FD negotiation: %m");
133
134 r = sd_bus_start(a);
135 if (r < 0)
136 return log_error_errno(r, "Failed to start bus client: %m");
137
138 r = sd_bus_get_bus_id(a, &server_id);
139 if (r < 0)
140 return log_error_errno(r, "Failed to get server ID: %m");
141
142 r = sd_bus_new(&b);
143 if (r < 0)
144 return log_error_errno(r, "Failed to allocate bus: %m");
145
146 r = sd_bus_set_fd(b, in_fd, out_fd);
147 if (r < 0)
148 return log_error_errno(r, "Failed to set fds: %m");
149
150 r = sd_bus_set_server(b, 1, server_id);
151 if (r < 0)
152 return log_error_errno(r, "Failed to set server mode: %m");
153
154 r = sd_bus_negotiate_fds(b, is_unix);
155 if (r < 0)
156 return log_error_errno(r, "Failed to set FD negotiation: %m");
157
158 r = sd_bus_set_anonymous(b, true);
159 if (r < 0)
160 return log_error_errno(r, "Failed to set anonymous authentication: %m");
161
162 r = sd_bus_start(b);
163 if (r < 0)
164 return log_error_errno(r, "Failed to start bus client: %m");
165
166 for (;;) {
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;
171
172 r = sd_bus_process(a, &m);
173 if (r < 0)
174 return log_error_errno(r, "Failed to process bus a: %m");
175
176 if (m) {
177 r = sd_bus_send(b, m, NULL);
178 if (r < 0)
179 return log_error_errno(r, "Failed to send message: %m");
180 }
181
182 if (r > 0)
183 continue;
184
185 r = sd_bus_process(b, &m);
186 if (r < 0) {
187 /* treat 'connection reset by peer' as clean exit condition */
188 if (ERRNO_IS_DISCONNECT(r))
189 return 0;
190
191 return log_error_errno(r, "Failed to process bus: %m");
192 }
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
248 r = ppoll(p, ELEMENTSOF(p), ts, NULL);
249 }
250 if (r < 0)
251 return log_error_errno(errno, "ppoll() failed: %m");
252 }
253
254 return 0;
255 }
256
257 DEFINE_MAIN_FUNCTION(run);