]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
912b54ad | 2 | |
912b54ad | 3 | #include <errno.h> |
3f6fd1ba | 4 | #include <fcntl.h> |
912b54ad | 5 | #include <getopt.h> |
3f6fd1ba | 6 | #include <netdb.h> |
912b54ad DS |
7 | #include <stdio.h> |
8 | #include <stdlib.h> | |
912b54ad DS |
9 | #include <sys/un.h> |
10 | #include <unistd.h> | |
11 | ||
912b54ad DS |
12 | #include "sd-daemon.h" |
13 | #include "sd-event.h" | |
fb69d709 | 14 | #include "sd-resolve.h" |
3f6fd1ba | 15 | |
b5efdb8a | 16 | #include "alloc-util.h" |
d6b4d1c7 | 17 | #include "build.h" |
f60a028a | 18 | #include "errno-util.h" |
3ffd4af2 | 19 | #include "fd-util.h" |
96c374d0 | 20 | #include "log.h" |
a160567e | 21 | #include "main-func.h" |
37ec0fdd | 22 | #include "parse-util.h" |
3f6fd1ba | 23 | #include "path-util.h" |
294bf0c3 | 24 | #include "pretty-print.h" |
f2935c77 | 25 | #include "resolve-private.h" |
3f6fd1ba | 26 | #include "set.h" |
912b54ad | 27 | #include "socket-util.h" |
07630cea | 28 | #include "string-util.h" |
8569a776 LP |
29 | |
30 | #define BUFFER_SIZE (256 * 1024) | |
912b54ad | 31 | |
f60a028a | 32 | static unsigned arg_connections_max = 256; |
fb69d709 | 33 | static const char *arg_remote_host = NULL; |
9e12d5bf | 34 | static usec_t arg_exit_idle_time = USEC_INFINITY; |
fb69d709 | 35 | |
8569a776 | 36 | typedef struct Context { |
fb69d709 LP |
37 | sd_event *event; |
38 | sd_resolve *resolve; | |
9e12d5bf | 39 | sd_event_source *idle_time; |
fb69d709 | 40 | |
8569a776 LP |
41 | Set *listen; |
42 | Set *connections; | |
43 | } Context; | |
912b54ad | 44 | |
8569a776 | 45 | typedef struct Connection { |
e633ea1c LP |
46 | Context *context; |
47 | ||
8569a776 LP |
48 | int server_fd, client_fd; |
49 | int server_to_client_buffer[2]; /* a pipe */ | |
50 | int client_to_server_buffer[2]; /* a pipe */ | |
912b54ad | 51 | |
8569a776 LP |
52 | size_t server_to_client_buffer_full, client_to_server_buffer_full; |
53 | size_t server_to_client_buffer_size, client_to_server_buffer_size; | |
54 | ||
55 | sd_event_source *server_event_source, *client_event_source; | |
912b54ad | 56 | |
fb69d709 LP |
57 | sd_resolve_query *resolve_query; |
58 | } Connection; | |
912b54ad | 59 | |
8569a776 LP |
60 | static void connection_free(Connection *c) { |
61 | assert(c); | |
32d3c809 | 62 | |
e633ea1c LP |
63 | if (c->context) |
64 | set_remove(c->context->connections, c); | |
65 | ||
8569a776 LP |
66 | sd_event_source_unref(c->server_event_source); |
67 | sd_event_source_unref(c->client_event_source); | |
32d3c809 | 68 | |
03e334a1 LP |
69 | safe_close(c->server_fd); |
70 | safe_close(c->client_fd); | |
32d3c809 | 71 | |
3d94f76c LP |
72 | safe_close_pair(c->server_to_client_buffer); |
73 | safe_close_pair(c->client_to_server_buffer); | |
32d3c809 | 74 | |
fb69d709 LP |
75 | sd_resolve_query_unref(c->resolve_query); |
76 | ||
8569a776 LP |
77 | free(c); |
78 | } | |
32d3c809 | 79 | |
9e12d5bf EA |
80 | static int idle_time_cb(sd_event_source *s, uint64_t usec, void *userdata) { |
81 | Context *c = userdata; | |
82 | int r; | |
83 | ||
84 | if (!set_isempty(c->connections)) { | |
85 | log_warning("Idle timer fired even though there are connections, ignoring"); | |
86 | return 0; | |
87 | } | |
88 | ||
89 | r = sd_event_exit(c->event, 0); | |
90 | if (r < 0) { | |
91 | log_warning_errno(r, "Error while stopping event loop, ignoring: %m"); | |
92 | return 0; | |
93 | } | |
94 | return 0; | |
95 | } | |
96 | ||
97 | static int connection_release(Connection *c) { | |
fbd747a4 | 98 | Context *context = ASSERT_PTR(ASSERT_PTR(c)->context); |
39cf0351 | 99 | int r; |
9e12d5bf EA |
100 | |
101 | connection_free(c); | |
102 | ||
103 | if (arg_exit_idle_time < USEC_INFINITY && set_isempty(context->connections)) { | |
9e12d5bf | 104 | if (context->idle_time) { |
39cf0351 | 105 | r = sd_event_source_set_time_relative(context->idle_time, arg_exit_idle_time); |
9e12d5bf EA |
106 | if (r < 0) |
107 | return log_error_errno(r, "Error while setting idle time: %m"); | |
108 | ||
109 | r = sd_event_source_set_enabled(context->idle_time, SD_EVENT_ONESHOT); | |
110 | if (r < 0) | |
111 | return log_error_errno(r, "Error while enabling idle time: %m"); | |
112 | } else { | |
39cf0351 LP |
113 | r = sd_event_add_time_relative( |
114 | context->event, &context->idle_time, CLOCK_MONOTONIC, | |
115 | arg_exit_idle_time, 0, idle_time_cb, context); | |
9e12d5bf EA |
116 | if (r < 0) |
117 | return log_error_errno(r, "Failed to create idle timer: %m"); | |
118 | } | |
119 | } | |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
a160567e | 124 | static void context_clear(Context *context) { |
8569a776 LP |
125 | assert(context); |
126 | ||
224b0e7a ZJS |
127 | set_free_with_destructor(context->listen, sd_event_source_unref); |
128 | set_free_with_destructor(context->connections, connection_free); | |
32d3c809 | 129 | |
fb69d709 LP |
130 | sd_event_unref(context->event); |
131 | sd_resolve_unref(context->resolve); | |
9e12d5bf | 132 | sd_event_source_unref(context->idle_time); |
8569a776 | 133 | } |
912b54ad | 134 | |
3042bbeb | 135 | static int connection_create_pipes(Connection *c, int buffer[static 2], size_t *sz) { |
8569a776 | 136 | int r; |
912b54ad | 137 | |
8569a776 LP |
138 | assert(c); |
139 | assert(buffer); | |
140 | assert(sz); | |
912b54ad | 141 | |
8569a776 LP |
142 | if (buffer[0] >= 0) |
143 | return 0; | |
912b54ad | 144 | |
8569a776 | 145 | r = pipe2(buffer, O_CLOEXEC|O_NONBLOCK); |
4a62c710 MS |
146 | if (r < 0) |
147 | return log_error_errno(errno, "Failed to allocate pipe buffer: %m"); | |
912b54ad | 148 | |
25dbe4f5 | 149 | (void) fcntl(buffer[0], F_SETPIPE_SZ, BUFFER_SIZE); |
8569a776 LP |
150 | |
151 | r = fcntl(buffer[0], F_GETPIPE_SZ); | |
4a62c710 MS |
152 | if (r < 0) |
153 | return log_error_errno(errno, "Failed to get pipe buffer size: %m"); | |
912b54ad | 154 | |
8569a776 LP |
155 | assert(r > 0); |
156 | *sz = r; | |
157 | ||
912b54ad DS |
158 | return 0; |
159 | } | |
160 | ||
8569a776 LP |
161 | static int connection_shovel( |
162 | Connection *c, | |
163 | int *from, int buffer[2], int *to, | |
164 | size_t *full, size_t *sz, | |
165 | sd_event_source **from_source, sd_event_source **to_source) { | |
166 | ||
167 | bool shoveled; | |
168 | ||
169 | assert(c); | |
170 | assert(from); | |
171 | assert(buffer); | |
172 | assert(buffer[0] >= 0); | |
173 | assert(buffer[1] >= 0); | |
174 | assert(to); | |
175 | assert(full); | |
176 | assert(sz); | |
177 | assert(from_source); | |
178 | assert(to_source); | |
179 | ||
180 | do { | |
181 | ssize_t z; | |
182 | ||
183 | shoveled = false; | |
184 | ||
185 | if (*full < *sz && *from >= 0 && *to >= 0) { | |
186 | z = splice(*from, NULL, buffer[1], NULL, *sz - *full, SPLICE_F_MOVE|SPLICE_F_NONBLOCK); | |
187 | if (z > 0) { | |
188 | *full += z; | |
189 | shoveled = true; | |
f60a028a | 190 | } else if (z == 0 || ERRNO_IS_DISCONNECT(errno)) { |
8569a776 | 191 | *from_source = sd_event_source_unref(*from_source); |
03e334a1 | 192 | *from = safe_close(*from); |
8add30a0 | 193 | } else if (!ERRNO_IS_TRANSIENT(errno)) |
4a62c710 | 194 | return log_error_errno(errno, "Failed to splice: %m"); |
912b54ad DS |
195 | } |
196 | ||
8569a776 LP |
197 | if (*full > 0 && *to >= 0) { |
198 | z = splice(buffer[0], NULL, *to, NULL, *full, SPLICE_F_MOVE|SPLICE_F_NONBLOCK); | |
199 | if (z > 0) { | |
200 | *full -= z; | |
201 | shoveled = true; | |
f60a028a | 202 | } else if (z == 0 || ERRNO_IS_DISCONNECT(errno)) { |
8569a776 | 203 | *to_source = sd_event_source_unref(*to_source); |
03e334a1 | 204 | *to = safe_close(*to); |
8add30a0 | 205 | } else if (!ERRNO_IS_TRANSIENT(errno)) |
4a62c710 | 206 | return log_error_errno(errno, "Failed to splice: %m"); |
8569a776 LP |
207 | } |
208 | } while (shoveled); | |
912b54ad | 209 | |
8569a776 | 210 | return 0; |
912b54ad DS |
211 | } |
212 | ||
fb69d709 | 213 | static int connection_enable_event_sources(Connection *c); |
8569a776 LP |
214 | |
215 | static int traffic_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
99534007 | 216 | Connection *c = ASSERT_PTR(userdata); |
912b54ad DS |
217 | int r; |
218 | ||
8569a776 LP |
219 | assert(s); |
220 | assert(fd >= 0); | |
912b54ad | 221 | |
8569a776 LP |
222 | r = connection_shovel(c, |
223 | &c->server_fd, c->server_to_client_buffer, &c->client_fd, | |
224 | &c->server_to_client_buffer_full, &c->server_to_client_buffer_size, | |
225 | &c->server_event_source, &c->client_event_source); | |
226 | if (r < 0) | |
227 | goto quit; | |
912b54ad | 228 | |
8569a776 LP |
229 | r = connection_shovel(c, |
230 | &c->client_fd, c->client_to_server_buffer, &c->server_fd, | |
231 | &c->client_to_server_buffer_full, &c->client_to_server_buffer_size, | |
232 | &c->client_event_source, &c->server_event_source); | |
233 | if (r < 0) | |
234 | goto quit; | |
912b54ad | 235 | |
8569a776 | 236 | /* EOF on both sides? */ |
da850694 | 237 | if (c->server_fd < 0 && c->client_fd < 0) |
8569a776 | 238 | goto quit; |
912b54ad | 239 | |
8569a776 | 240 | /* Server closed, and all data written to client? */ |
da850694 | 241 | if (c->server_fd < 0 && c->server_to_client_buffer_full <= 0) |
8569a776 | 242 | goto quit; |
912b54ad | 243 | |
8569a776 | 244 | /* Client closed, and all data written to server? */ |
da850694 | 245 | if (c->client_fd < 0 && c->client_to_server_buffer_full <= 0) |
8569a776 | 246 | goto quit; |
912b54ad | 247 | |
fb69d709 | 248 | r = connection_enable_event_sources(c); |
8569a776 LP |
249 | if (r < 0) |
250 | goto quit; | |
912b54ad | 251 | |
8569a776 | 252 | return 1; |
912b54ad | 253 | |
8569a776 | 254 | quit: |
9e12d5bf | 255 | connection_release(c); |
8569a776 | 256 | return 0; /* ignore errors, continue serving */ |
912b54ad DS |
257 | } |
258 | ||
fb69d709 | 259 | static int connection_enable_event_sources(Connection *c) { |
8569a776 LP |
260 | uint32_t a = 0, b = 0; |
261 | int r; | |
912b54ad | 262 | |
8569a776 | 263 | assert(c); |
912b54ad | 264 | |
8569a776 LP |
265 | if (c->server_to_client_buffer_full > 0) |
266 | b |= EPOLLOUT; | |
267 | if (c->server_to_client_buffer_full < c->server_to_client_buffer_size) | |
268 | a |= EPOLLIN; | |
912b54ad | 269 | |
8569a776 LP |
270 | if (c->client_to_server_buffer_full > 0) |
271 | a |= EPOLLOUT; | |
272 | if (c->client_to_server_buffer_full < c->client_to_server_buffer_size) | |
273 | b |= EPOLLIN; | |
912b54ad | 274 | |
8569a776 LP |
275 | if (c->server_event_source) |
276 | r = sd_event_source_set_io_events(c->server_event_source, a); | |
277 | else if (c->server_fd >= 0) | |
fb69d709 | 278 | r = sd_event_add_io(c->context->event, &c->server_event_source, c->server_fd, a, traffic_cb, c); |
8569a776 LP |
279 | else |
280 | r = 0; | |
281 | ||
f647962d MS |
282 | if (r < 0) |
283 | return log_error_errno(r, "Failed to set up server event source: %m"); | |
912b54ad | 284 | |
8569a776 LP |
285 | if (c->client_event_source) |
286 | r = sd_event_source_set_io_events(c->client_event_source, b); | |
287 | else if (c->client_fd >= 0) | |
fb69d709 | 288 | r = sd_event_add_io(c->context->event, &c->client_event_source, c->client_fd, b, traffic_cb, c); |
8569a776 LP |
289 | else |
290 | r = 0; | |
912b54ad | 291 | |
f647962d MS |
292 | if (r < 0) |
293 | return log_error_errno(r, "Failed to set up client event source: %m"); | |
912b54ad | 294 | |
8569a776 | 295 | return 0; |
912b54ad DS |
296 | } |
297 | ||
fb69d709 LP |
298 | static int connection_complete(Connection *c) { |
299 | int r; | |
300 | ||
301 | assert(c); | |
302 | ||
303 | r = connection_create_pipes(c, c->server_to_client_buffer, &c->server_to_client_buffer_size); | |
304 | if (r < 0) | |
305 | goto fail; | |
306 | ||
307 | r = connection_create_pipes(c, c->client_to_server_buffer, &c->client_to_server_buffer_size); | |
308 | if (r < 0) | |
309 | goto fail; | |
310 | ||
311 | r = connection_enable_event_sources(c); | |
312 | if (r < 0) | |
313 | goto fail; | |
314 | ||
315 | return 0; | |
316 | ||
317 | fail: | |
9e12d5bf | 318 | connection_release(c); |
fb69d709 LP |
319 | return 0; /* ignore errors, continue serving */ |
320 | } | |
321 | ||
8569a776 | 322 | static int connect_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
99534007 | 323 | Connection *c = ASSERT_PTR(userdata); |
8569a776 LP |
324 | socklen_t solen; |
325 | int error, r; | |
326 | ||
327 | assert(s); | |
328 | assert(fd >= 0); | |
8569a776 LP |
329 | |
330 | solen = sizeof(error); | |
331 | r = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &solen); | |
332 | if (r < 0) { | |
56f64d95 | 333 | log_error_errno(errno, "Failed to issue SO_ERROR: %m"); |
40976028 DS |
334 | goto fail; |
335 | } | |
912b54ad | 336 | |
8569a776 | 337 | if (error != 0) { |
279d3c9c | 338 | log_error_errno(error, "Failed to connect to remote host: %m"); |
912b54ad DS |
339 | goto fail; |
340 | } | |
341 | ||
8569a776 LP |
342 | c->client_event_source = sd_event_source_unref(c->client_event_source); |
343 | ||
fb69d709 | 344 | return connection_complete(c); |
912b54ad | 345 | |
fb69d709 | 346 | fail: |
9e12d5bf | 347 | connection_release(c); |
fb69d709 LP |
348 | return 0; /* ignore errors, continue serving */ |
349 | } | |
912b54ad | 350 | |
fb69d709 LP |
351 | static int connection_start(Connection *c, struct sockaddr *sa, socklen_t salen) { |
352 | int r; | |
353 | ||
354 | assert(c); | |
355 | assert(sa); | |
356 | assert(salen); | |
357 | ||
358 | c->client_fd = socket(sa->sa_family, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0); | |
359 | if (c->client_fd < 0) { | |
56f64d95 | 360 | log_error_errno(errno, "Failed to get remote socket: %m"); |
8569a776 | 361 | goto fail; |
fb69d709 LP |
362 | } |
363 | ||
364 | r = connect(c->client_fd, sa, salen); | |
365 | if (r < 0) { | |
366 | if (errno == EINPROGRESS) { | |
367 | r = sd_event_add_io(c->context->event, &c->client_event_source, c->client_fd, EPOLLOUT, connect_cb, c); | |
368 | if (r < 0) { | |
da927ba9 | 369 | log_error_errno(r, "Failed to add connection socket: %m"); |
fb69d709 LP |
370 | goto fail; |
371 | } | |
372 | ||
373 | r = sd_event_source_set_enabled(c->client_event_source, SD_EVENT_ONESHOT); | |
374 | if (r < 0) { | |
da927ba9 | 375 | log_error_errno(r, "Failed to enable oneshot event source: %m"); |
fb69d709 LP |
376 | goto fail; |
377 | } | |
378 | } else { | |
56f64d95 | 379 | log_error_errno(errno, "Failed to connect to remote host: %m"); |
fb69d709 LP |
380 | goto fail; |
381 | } | |
382 | } else { | |
383 | r = connection_complete(c); | |
384 | if (r < 0) | |
385 | goto fail; | |
386 | } | |
912b54ad | 387 | |
8569a776 | 388 | return 0; |
912b54ad | 389 | |
8569a776 | 390 | fail: |
9e12d5bf | 391 | connection_release(c); |
8569a776 LP |
392 | return 0; /* ignore errors, continue serving */ |
393 | } | |
394 | ||
f2935c77 | 395 | static int resolve_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, Connection *c) { |
fb69d709 LP |
396 | assert(q); |
397 | assert(c); | |
398 | ||
399 | if (ret != 0) { | |
400 | log_error("Failed to resolve host: %s", gai_strerror(ret)); | |
401 | goto fail; | |
402 | } | |
403 | ||
404 | c->resolve_query = sd_resolve_query_unref(c->resolve_query); | |
405 | ||
406 | return connection_start(c, ai->ai_addr, ai->ai_addrlen); | |
407 | ||
408 | fail: | |
9e12d5bf | 409 | connection_release(c); |
fb69d709 LP |
410 | return 0; /* ignore errors, continue serving */ |
411 | } | |
412 | ||
413 | static int resolve_remote(Connection *c) { | |
414 | ||
415 | static const struct addrinfo hints = { | |
416 | .ai_family = AF_UNSPEC, | |
417 | .ai_socktype = SOCK_STREAM, | |
fb69d709 LP |
418 | }; |
419 | ||
fb69d709 | 420 | const char *node, *service; |
fb69d709 LP |
421 | int r; |
422 | ||
15a3e96f | 423 | if (IN_SET(arg_remote_host[0], '/', '@')) { |
f36a9d59 ZJS |
424 | union sockaddr_union sa; |
425 | int sa_len; | |
15a3e96f | 426 | |
f36a9d59 ZJS |
427 | r = sockaddr_un_set_path(&sa.un, arg_remote_host); |
428 | if (r < 0) { | |
429 | log_error_errno(r, "Specified address doesn't fit in an AF_UNIX address, refusing: %m"); | |
15a3e96f LP |
430 | goto fail; |
431 | } | |
f36a9d59 | 432 | sa_len = r; |
fb69d709 | 433 | |
f36a9d59 | 434 | return connection_start(c, &sa.sa, sa_len); |
fb69d709 LP |
435 | } |
436 | ||
437 | service = strrchr(arg_remote_host, ':'); | |
438 | if (service) { | |
2f82562b LP |
439 | node = strndupa_safe(arg_remote_host, |
440 | service - arg_remote_host); | |
313cefa1 | 441 | service++; |
fb69d709 LP |
442 | } else { |
443 | node = arg_remote_host; | |
444 | service = "80"; | |
445 | } | |
446 | ||
447 | log_debug("Looking up address info for %s:%s", node, service); | |
f2935c77 | 448 | r = resolve_getaddrinfo(c->context->resolve, &c->resolve_query, node, service, &hints, resolve_handler, NULL, c); |
fb69d709 | 449 | if (r < 0) { |
da927ba9 | 450 | log_error_errno(r, "Failed to resolve remote host: %m"); |
fb69d709 LP |
451 | goto fail; |
452 | } | |
453 | ||
454 | return 0; | |
455 | ||
456 | fail: | |
9e12d5bf | 457 | connection_release(c); |
fb69d709 LP |
458 | return 0; /* ignore errors, continue serving */ |
459 | } | |
460 | ||
461 | static int add_connection_socket(Context *context, int fd) { | |
8569a776 LP |
462 | Connection *c; |
463 | int r; | |
464 | ||
465 | assert(context); | |
8569a776 LP |
466 | assert(fd >= 0); |
467 | ||
dc3b8afb | 468 | if (set_size(context->connections) > arg_connections_max) { |
8569a776 | 469 | log_warning("Hit connection limit, refusing connection."); |
03e334a1 | 470 | safe_close(fd); |
8569a776 | 471 | return 0; |
912b54ad DS |
472 | } |
473 | ||
9e12d5bf EA |
474 | if (context->idle_time) { |
475 | r = sd_event_source_set_enabled(context->idle_time, SD_EVENT_OFF); | |
476 | if (r < 0) | |
477 | log_warning_errno(r, "Unable to disable idle timer, continuing: %m"); | |
478 | } | |
479 | ||
80ce54ad | 480 | c = new(Connection, 1); |
fb69d709 LP |
481 | if (!c) { |
482 | log_oom(); | |
483 | return 0; | |
484 | } | |
8569a776 | 485 | |
80ce54ad ZJS |
486 | *c = (Connection) { |
487 | .context = context, | |
488 | .server_fd = fd, | |
254d1313 | 489 | .client_fd = -EBADF, |
80ce54ad ZJS |
490 | .server_to_client_buffer = {-1, -1}, |
491 | .client_to_server_buffer = {-1, -1}, | |
492 | }; | |
8569a776 | 493 | |
de7fef4b | 494 | r = set_ensure_put(&context->connections, NULL, c); |
e633ea1c LP |
495 | if (r < 0) { |
496 | free(c); | |
fb69d709 LP |
497 | log_oom(); |
498 | return 0; | |
8569a776 | 499 | } |
912b54ad | 500 | |
fb69d709 | 501 | return resolve_remote(c); |
40976028 DS |
502 | } |
503 | ||
504 | static int accept_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
7b77ed8c | 505 | _cleanup_free_ char *peer = NULL; |
99534007 | 506 | Context *context = ASSERT_PTR(userdata); |
254d1313 | 507 | int nfd = -EBADF, r; |
40976028 | 508 | |
8569a776 LP |
509 | assert(s); |
510 | assert(fd >= 0); | |
40976028 | 511 | assert(revents & EPOLLIN); |
8569a776 LP |
512 | |
513 | nfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); | |
7b77ed8c | 514 | if (nfd < 0) { |
4ff9bc2e | 515 | if (!ERRNO_IS_ACCEPT_AGAIN(errno)) |
56f64d95 | 516 | log_warning_errno(errno, "Failed to accept() socket: %m"); |
7b77ed8c | 517 | } else { |
7ebd758c | 518 | (void) getpeername_pretty(nfd, true, &peer); |
8569a776 | 519 | log_debug("New connection from %s", strna(peer)); |
40976028 | 520 | |
fb69d709 | 521 | r = add_connection_socket(context, nfd); |
f4bd42aa | 522 | if (r < 0) { |
76c59537 LP |
523 | log_warning_errno(r, "Failed to accept connection, ignoring: %m"); |
524 | safe_close(nfd); | |
f4bd42aa | 525 | } |
7b77ed8c | 526 | } |
8569a776 | 527 | |
40976028 | 528 | r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT); |
ccaa30c1 LP |
529 | if (r < 0) |
530 | return log_error_errno(r, "Error while re-enabling listener with ONESHOT: %m"); | |
40976028 | 531 | |
96c374d0 | 532 | return 1; |
912b54ad DS |
533 | } |
534 | ||
fb69d709 | 535 | static int add_listen_socket(Context *context, int fd) { |
8569a776 LP |
536 | sd_event_source *source; |
537 | int r; | |
538 | ||
539 | assert(context); | |
8569a776 | 540 | assert(fd >= 0); |
912b54ad | 541 | |
8569a776 | 542 | r = sd_is_socket(fd, 0, SOCK_STREAM, 1); |
f647962d MS |
543 | if (r < 0) |
544 | return log_error_errno(r, "Failed to determine socket type: %m"); | |
baaa35ad ZJS |
545 | if (r == 0) |
546 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
547 | "Passed in socket is not a stream socket."); | |
912b54ad | 548 | |
8569a776 | 549 | r = fd_nonblock(fd, true); |
f647962d MS |
550 | if (r < 0) |
551 | return log_error_errno(r, "Failed to mark file descriptor non-blocking: %m"); | |
912b54ad | 552 | |
fb69d709 | 553 | r = sd_event_add_io(context->event, &source, fd, EPOLLIN, accept_cb, context); |
f647962d MS |
554 | if (r < 0) |
555 | return log_error_errno(r, "Failed to add event source: %m"); | |
912b54ad | 556 | |
de7fef4b | 557 | r = set_ensure_put(&context->listen, NULL, source); |
96c374d0 | 558 | if (r < 0) { |
8569a776 | 559 | sd_event_source_unref(source); |
ccaa30c1 | 560 | return log_error_errno(r, "Failed to add source to set: %m"); |
40976028 | 561 | } |
ccaa30c1 LP |
562 | |
563 | r = sd_event_source_set_exit_on_failure(source, true); | |
564 | if (r < 0) | |
565 | return log_error_errno(r, "Failed to enable exit-on-failure logic: %m"); | |
40976028 DS |
566 | |
567 | /* Set the watcher to oneshot in case other processes are also | |
568 | * watching to accept(). */ | |
8569a776 | 569 | r = sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); |
f647962d MS |
570 | if (r < 0) |
571 | return log_error_errno(r, "Failed to enable oneshot mode: %m"); | |
912b54ad | 572 | |
8569a776 | 573 | return 0; |
912b54ad DS |
574 | } |
575 | ||
37ec0fdd LP |
576 | static int help(void) { |
577 | _cleanup_free_ char *link = NULL; | |
9e12d5bf | 578 | _cleanup_free_ char *time_link = NULL; |
37ec0fdd LP |
579 | int r; |
580 | ||
581 | r = terminal_urlify_man("systemd-socket-proxyd", "8", &link); | |
9e12d5bf EA |
582 | if (r < 0) |
583 | return log_oom(); | |
584 | r = terminal_urlify_man("systemd.time", "7", &time_link); | |
37ec0fdd LP |
585 | if (r < 0) |
586 | return log_oom(); | |
587 | ||
601185b4 ZJS |
588 | printf("%1$s [HOST:PORT]\n" |
589 | "%1$s [SOCKET]\n\n" | |
8569a776 | 590 | "Bidirectionally proxy local sockets to another (possibly remote) socket.\n\n" |
23d0fff7 | 591 | " -c --connections-max= Set the maximum number of connections to be accepted\n" |
9e12d5bf EA |
592 | " --exit-idle-time= Exit when without a connection for this duration. See\n" |
593 | " the %3$s for time span format\n" | |
8cf030b3 | 594 | " -h --help Show this help\n" |
37ec0fdd | 595 | " --version Show package version\n" |
bc556335 DDM |
596 | "\nSee the %2$s for details.\n", |
597 | program_invocation_short_name, | |
598 | link, | |
599 | time_link); | |
37ec0fdd LP |
600 | |
601 | return 0; | |
912b54ad DS |
602 | } |
603 | ||
8569a776 | 604 | static int parse_argv(int argc, char *argv[]) { |
912b54ad DS |
605 | |
606 | enum { | |
8cf030b3 | 607 | ARG_VERSION = 0x100, |
9e12d5bf | 608 | ARG_EXIT_IDLE, |
8cf030b3 | 609 | ARG_IGNORE_ENV |
912b54ad DS |
610 | }; |
611 | ||
612 | static const struct option options[] = { | |
dc3b8afb | 613 | { "connections-max", required_argument, NULL, 'c' }, |
9e12d5bf | 614 | { "exit-idle-time", required_argument, NULL, ARG_EXIT_IDLE }, |
dc3b8afb DK |
615 | { "help", no_argument, NULL, 'h' }, |
616 | { "version", no_argument, NULL, ARG_VERSION }, | |
eb9da376 | 617 | {} |
912b54ad DS |
618 | }; |
619 | ||
dc3b8afb | 620 | int c, r; |
912b54ad DS |
621 | |
622 | assert(argc >= 0); | |
623 | assert(argv); | |
624 | ||
dc3b8afb | 625 | while ((c = getopt_long(argc, argv, "c:h", options, NULL)) >= 0) |
912b54ad DS |
626 | |
627 | switch (c) { | |
628 | ||
629 | case 'h': | |
37ec0fdd LP |
630 | return help(); |
631 | ||
632 | case ARG_VERSION: | |
633 | return version(); | |
912b54ad | 634 | |
dc3b8afb DK |
635 | case 'c': |
636 | r = safe_atou(optarg, &arg_connections_max); | |
637 | if (r < 0) { | |
638 | log_error("Failed to parse --connections-max= argument: %s", optarg); | |
639 | return r; | |
640 | } | |
641 | ||
baaa35ad ZJS |
642 | if (arg_connections_max < 1) |
643 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
644 | "Connection limit is too low."); | |
dc3b8afb DK |
645 | |
646 | break; | |
647 | ||
9e12d5bf EA |
648 | case ARG_EXIT_IDLE: |
649 | r = parse_sec(optarg, &arg_exit_idle_time); | |
650 | if (r < 0) | |
651 | return log_error_errno(r, "Failed to parse --exit-idle-time= argument: %s", optarg); | |
652 | break; | |
653 | ||
eb9da376 | 654 | case '?': |
912b54ad | 655 | return -EINVAL; |
eb9da376 LP |
656 | |
657 | default: | |
04499a70 | 658 | assert_not_reached(); |
912b54ad | 659 | } |
912b54ad | 660 | |
baaa35ad ZJS |
661 | if (optind >= argc) |
662 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
663 | "Not enough parameters."); | |
912b54ad | 664 | |
baaa35ad ZJS |
665 | if (argc != optind+1) |
666 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
667 | "Too many parameters."); | |
912b54ad | 668 | |
8569a776 | 669 | arg_remote_host = argv[optind]; |
912b54ad DS |
670 | return 1; |
671 | } | |
672 | ||
a160567e YW |
673 | static int run(int argc, char *argv[]) { |
674 | _cleanup_(context_clear) Context context = {}; | |
8569a776 | 675 | int r, n, fd; |
912b54ad DS |
676 | |
677 | log_parse_environment(); | |
678 | log_open(); | |
679 | ||
8569a776 | 680 | r = parse_argv(argc, argv); |
912b54ad | 681 | if (r <= 0) |
a160567e | 682 | return r; |
912b54ad | 683 | |
fb69d709 | 684 | r = sd_event_default(&context.event); |
a160567e YW |
685 | if (r < 0) |
686 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
912b54ad | 687 | |
fb69d709 | 688 | r = sd_resolve_default(&context.resolve); |
a160567e YW |
689 | if (r < 0) |
690 | return log_error_errno(r, "Failed to allocate resolver: %m"); | |
fb69d709 LP |
691 | |
692 | r = sd_resolve_attach_event(context.resolve, context.event, 0); | |
a160567e YW |
693 | if (r < 0) |
694 | return log_error_errno(r, "Failed to attach resolver: %m"); | |
fb69d709 LP |
695 | |
696 | sd_event_set_watchdog(context.event, true); | |
cde93897 | 697 | |
a160567e YW |
698 | r = sd_listen_fds(1); |
699 | if (r < 0) | |
700 | return log_error_errno(r, "Failed to receive sockets from parent."); | |
701 | if (r == 0) | |
702 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Didn't get any sockets passed in."); | |
703 | ||
704 | n = r; | |
8cf030b3 LP |
705 | |
706 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) { | |
fb69d709 | 707 | r = add_listen_socket(&context, fd); |
8569a776 | 708 | if (r < 0) |
a160567e | 709 | return r; |
912b54ad DS |
710 | } |
711 | ||
fb69d709 | 712 | r = sd_event_loop(context.event); |
a160567e YW |
713 | if (r < 0) |
714 | return log_error_errno(r, "Failed to run event loop: %m"); | |
8569a776 | 715 | |
a160567e | 716 | return 0; |
912b54ad | 717 | } |
a160567e YW |
718 | |
719 | DEFINE_MAIN_FUNCTION(run); |