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