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