]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/socket-proxy/socket-proxyd.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[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"
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"
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
a160567e 79static void context_clear(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
3042bbeb 89static int connection_create_pipes(Connection *c, int buffer[static 2], size_t *sz) {
8569a776 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
f2935c77 351static int resolve_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, Connection *c) {
fb69d709
LP
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);
f2935c77 403 r = resolve_getaddrinfo(c->context->resolve, &c->resolve_query, node, service, &hints, resolve_handler, NULL, c);
fb69d709 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
a160567e
YW
619static int run(int argc, char *argv[]) {
620 _cleanup_(context_clear) Context context = {};
8569a776 621 int r, n, fd;
912b54ad
DS
622
623 log_parse_environment();
624 log_open();
625
8569a776 626 r = parse_argv(argc, argv);
912b54ad 627 if (r <= 0)
a160567e 628 return r;
912b54ad 629
fb69d709 630 r = sd_event_default(&context.event);
a160567e
YW
631 if (r < 0)
632 return log_error_errno(r, "Failed to allocate event loop: %m");
912b54ad 633
fb69d709 634 r = sd_resolve_default(&context.resolve);
a160567e
YW
635 if (r < 0)
636 return log_error_errno(r, "Failed to allocate resolver: %m");
fb69d709
LP
637
638 r = sd_resolve_attach_event(context.resolve, context.event, 0);
a160567e
YW
639 if (r < 0)
640 return log_error_errno(r, "Failed to attach resolver: %m");
fb69d709
LP
641
642 sd_event_set_watchdog(context.event, true);
cde93897 643
a160567e
YW
644 r = sd_listen_fds(1);
645 if (r < 0)
646 return log_error_errno(r, "Failed to receive sockets from parent.");
647 if (r == 0)
648 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Didn't get any sockets passed in.");
649
650 n = r;
8cf030b3
LP
651
652 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
fb69d709 653 r = add_listen_socket(&context, fd);
8569a776 654 if (r < 0)
a160567e 655 return r;
912b54ad
DS
656 }
657
fb69d709 658 r = sd_event_loop(context.event);
a160567e
YW
659 if (r < 0)
660 return log_error_errno(r, "Failed to run event loop: %m");
8569a776 661
a160567e 662 return 0;
912b54ad 663}
a160567e
YW
664
665DEFINE_MAIN_FUNCTION(run);