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