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