]>
Commit | Line | Data |
---|---|---|
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 LP |
22 | #include "path-util.h" |
23 | #include "set.h" | |
912b54ad | 24 | #include "socket-util.h" |
07630cea | 25 | #include "string-util.h" |
37ec0fdd | 26 | #include "terminal-util.h" |
912b54ad | 27 | #include "util.h" |
8569a776 LP |
28 | |
29 | #define BUFFER_SIZE (256 * 1024) | |
dc3b8afb | 30 | static unsigned arg_connections_max = 256; |
912b54ad | 31 | |
fb69d709 LP |
32 | static const char *arg_remote_host = NULL; |
33 | ||
8569a776 | 34 | typedef 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 | 42 | typedef 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 |
57 | static 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 | 77 | static 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 |
87 | static 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 |
113 | static 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 | 165 | static int connection_enable_event_sources(Connection *c); |
8569a776 LP |
166 | |
167 | static 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 |
207 | quit: |
208 | connection_free(c); | |
209 | return 0; /* ignore errors, continue serving */ | |
912b54ad DS |
210 | } |
211 | ||
fb69d709 | 212 | static 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 |
251 | static 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 | ||
270 | fail: | |
271 | connection_free(c); | |
272 | return 0; /* ignore errors, continue serving */ | |
273 | } | |
274 | ||
8569a776 LP |
275 | static 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 |
300 | fail: |
301 | connection_free(c); | |
302 | return 0; /* ignore errors, continue serving */ | |
303 | } | |
912b54ad | 304 | |
fb69d709 LP |
305 | static 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 |
344 | fail: |
345 | connection_free(c); | |
346 | return 0; /* ignore errors, continue serving */ | |
347 | } | |
348 | ||
fb69d709 LP |
349 | static 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 | ||
364 | fail: | |
365 | connection_free(c); | |
366 | return 0; /* ignore errors, continue serving */ | |
367 | } | |
368 | ||
369 | static 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 | ||
381 | if (path_is_absolute(arg_remote_host)) { | |
382 | sa.un.sun_family = AF_UNIX; | |
fc2fffe7 LP |
383 | strncpy(sa.un.sun_path, arg_remote_host, sizeof(sa.un.sun_path)); |
384 | return connection_start(c, &sa.sa, SOCKADDR_UN_LEN(sa.un)); | |
fb69d709 LP |
385 | } |
386 | ||
387 | if (arg_remote_host[0] == '@') { | |
388 | sa.un.sun_family = AF_UNIX; | |
389 | sa.un.sun_path[0] = 0; | |
fc2fffe7 LP |
390 | strncpy(sa.un.sun_path+1, arg_remote_host+1, sizeof(sa.un.sun_path)-1); |
391 | return connection_start(c, &sa.sa, SOCKADDR_UN_LEN(sa.un)); | |
fb69d709 LP |
392 | } |
393 | ||
394 | service = strrchr(arg_remote_host, ':'); | |
395 | if (service) { | |
396 | node = strndupa(arg_remote_host, service - arg_remote_host); | |
313cefa1 | 397 | service++; |
fb69d709 LP |
398 | } else { |
399 | node = arg_remote_host; | |
400 | service = "80"; | |
401 | } | |
402 | ||
403 | log_debug("Looking up address info for %s:%s", node, service); | |
404 | r = sd_resolve_getaddrinfo(c->context->resolve, &c->resolve_query, node, service, &hints, resolve_cb, c); | |
405 | if (r < 0) { | |
da927ba9 | 406 | log_error_errno(r, "Failed to resolve remote host: %m"); |
fb69d709 LP |
407 | goto fail; |
408 | } | |
409 | ||
410 | return 0; | |
411 | ||
412 | fail: | |
413 | connection_free(c); | |
414 | return 0; /* ignore errors, continue serving */ | |
415 | } | |
416 | ||
417 | static int add_connection_socket(Context *context, int fd) { | |
8569a776 LP |
418 | Connection *c; |
419 | int r; | |
420 | ||
421 | assert(context); | |
8569a776 LP |
422 | assert(fd >= 0); |
423 | ||
dc3b8afb | 424 | if (set_size(context->connections) > arg_connections_max) { |
8569a776 | 425 | log_warning("Hit connection limit, refusing connection."); |
03e334a1 | 426 | safe_close(fd); |
8569a776 | 427 | return 0; |
912b54ad DS |
428 | } |
429 | ||
d5099efc | 430 | r = set_ensure_allocated(&context->connections, NULL); |
fb69d709 LP |
431 | if (r < 0) { |
432 | log_oom(); | |
433 | return 0; | |
434 | } | |
912b54ad | 435 | |
8569a776 | 436 | c = new0(Connection, 1); |
fb69d709 LP |
437 | if (!c) { |
438 | log_oom(); | |
439 | return 0; | |
440 | } | |
8569a776 | 441 | |
e633ea1c | 442 | c->context = context; |
8569a776 LP |
443 | c->server_fd = fd; |
444 | c->client_fd = -1; | |
445 | c->server_to_client_buffer[0] = c->server_to_client_buffer[1] = -1; | |
446 | c->client_to_server_buffer[0] = c->client_to_server_buffer[1] = -1; | |
447 | ||
e633ea1c LP |
448 | r = set_put(context->connections, c); |
449 | if (r < 0) { | |
450 | free(c); | |
fb69d709 LP |
451 | log_oom(); |
452 | return 0; | |
8569a776 | 453 | } |
912b54ad | 454 | |
fb69d709 | 455 | return resolve_remote(c); |
40976028 DS |
456 | } |
457 | ||
458 | static int accept_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
7b77ed8c | 459 | _cleanup_free_ char *peer = NULL; |
8569a776 LP |
460 | Context *context = userdata; |
461 | int nfd = -1, r; | |
40976028 | 462 | |
8569a776 LP |
463 | assert(s); |
464 | assert(fd >= 0); | |
40976028 | 465 | assert(revents & EPOLLIN); |
8569a776 LP |
466 | assert(context); |
467 | ||
468 | nfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); | |
7b77ed8c LP |
469 | if (nfd < 0) { |
470 | if (errno != -EAGAIN) | |
56f64d95 | 471 | log_warning_errno(errno, "Failed to accept() socket: %m"); |
7b77ed8c | 472 | } else { |
366b7db4 | 473 | getpeername_pretty(nfd, true, &peer); |
8569a776 | 474 | log_debug("New connection from %s", strna(peer)); |
40976028 | 475 | |
fb69d709 | 476 | r = add_connection_socket(context, nfd); |
f4bd42aa | 477 | if (r < 0) { |
da927ba9 | 478 | log_error_errno(r, "Failed to accept connection, ignoring: %m"); |
03e334a1 | 479 | safe_close(fd); |
f4bd42aa | 480 | } |
7b77ed8c | 481 | } |
8569a776 | 482 | |
40976028 DS |
483 | r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT); |
484 | if (r < 0) { | |
da927ba9 | 485 | log_error_errno(r, "Error while re-enabling listener with ONESHOT: %m"); |
fb69d709 | 486 | sd_event_exit(context->event, r); |
40976028 DS |
487 | return r; |
488 | } | |
489 | ||
96c374d0 | 490 | return 1; |
912b54ad DS |
491 | } |
492 | ||
fb69d709 | 493 | static int add_listen_socket(Context *context, int fd) { |
8569a776 LP |
494 | sd_event_source *source; |
495 | int r; | |
496 | ||
497 | assert(context); | |
8569a776 | 498 | assert(fd >= 0); |
912b54ad | 499 | |
d5099efc | 500 | r = set_ensure_allocated(&context->listen, NULL); |
96c374d0 | 501 | if (r < 0) { |
8569a776 LP |
502 | log_oom(); |
503 | return r; | |
504 | } | |
505 | ||
506 | r = sd_is_socket(fd, 0, SOCK_STREAM, 1); | |
f647962d MS |
507 | if (r < 0) |
508 | return log_error_errno(r, "Failed to determine socket type: %m"); | |
8569a776 LP |
509 | if (r == 0) { |
510 | log_error("Passed in socket is not a stream socket."); | |
511 | return -EINVAL; | |
512 | } | |
912b54ad | 513 | |
8569a776 | 514 | r = fd_nonblock(fd, true); |
f647962d MS |
515 | if (r < 0) |
516 | return log_error_errno(r, "Failed to mark file descriptor non-blocking: %m"); | |
912b54ad | 517 | |
fb69d709 | 518 | r = sd_event_add_io(context->event, &source, fd, EPOLLIN, accept_cb, context); |
f647962d MS |
519 | if (r < 0) |
520 | return log_error_errno(r, "Failed to add event source: %m"); | |
912b54ad | 521 | |
8569a776 | 522 | r = set_put(context->listen, source); |
96c374d0 | 523 | if (r < 0) { |
da927ba9 | 524 | log_error_errno(r, "Failed to add source to set: %m"); |
8569a776 | 525 | sd_event_source_unref(source); |
40976028 DS |
526 | return r; |
527 | } | |
528 | ||
529 | /* Set the watcher to oneshot in case other processes are also | |
530 | * watching to accept(). */ | |
8569a776 | 531 | r = sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); |
f647962d MS |
532 | if (r < 0) |
533 | return log_error_errno(r, "Failed to enable oneshot mode: %m"); | |
912b54ad | 534 | |
8569a776 | 535 | return 0; |
912b54ad DS |
536 | } |
537 | ||
37ec0fdd LP |
538 | static int help(void) { |
539 | _cleanup_free_ char *link = NULL; | |
540 | int r; | |
541 | ||
542 | r = terminal_urlify_man("systemd-socket-proxyd", "8", &link); | |
543 | if (r < 0) | |
544 | return log_oom(); | |
545 | ||
601185b4 ZJS |
546 | printf("%1$s [HOST:PORT]\n" |
547 | "%1$s [SOCKET]\n\n" | |
8569a776 | 548 | "Bidirectionally proxy local sockets to another (possibly remote) socket.\n\n" |
23d0fff7 | 549 | " -c --connections-max= Set the maximum number of connections to be accepted\n" |
8cf030b3 | 550 | " -h --help Show this help\n" |
37ec0fdd LP |
551 | " --version Show package version\n" |
552 | "\nSee the %2$s for details.\n" | |
553 | , program_invocation_short_name | |
554 | , link | |
555 | ); | |
556 | ||
557 | return 0; | |
912b54ad DS |
558 | } |
559 | ||
8569a776 | 560 | static int parse_argv(int argc, char *argv[]) { |
912b54ad DS |
561 | |
562 | enum { | |
8cf030b3 LP |
563 | ARG_VERSION = 0x100, |
564 | ARG_IGNORE_ENV | |
912b54ad DS |
565 | }; |
566 | ||
567 | static const struct option options[] = { | |
dc3b8afb DK |
568 | { "connections-max", required_argument, NULL, 'c' }, |
569 | { "help", no_argument, NULL, 'h' }, | |
570 | { "version", no_argument, NULL, ARG_VERSION }, | |
eb9da376 | 571 | {} |
912b54ad DS |
572 | }; |
573 | ||
dc3b8afb | 574 | int c, r; |
912b54ad DS |
575 | |
576 | assert(argc >= 0); | |
577 | assert(argv); | |
578 | ||
dc3b8afb | 579 | while ((c = getopt_long(argc, argv, "c:h", options, NULL)) >= 0) |
912b54ad DS |
580 | |
581 | switch (c) { | |
582 | ||
583 | case 'h': | |
37ec0fdd LP |
584 | return help(); |
585 | ||
586 | case ARG_VERSION: | |
587 | return version(); | |
912b54ad | 588 | |
dc3b8afb DK |
589 | case 'c': |
590 | r = safe_atou(optarg, &arg_connections_max); | |
591 | if (r < 0) { | |
592 | log_error("Failed to parse --connections-max= argument: %s", optarg); | |
593 | return r; | |
594 | } | |
595 | ||
596 | if (arg_connections_max < 1) { | |
597 | log_error("Connection limit is too low."); | |
598 | return -EINVAL; | |
599 | } | |
600 | ||
601 | break; | |
602 | ||
eb9da376 | 603 | case '?': |
912b54ad | 604 | return -EINVAL; |
eb9da376 LP |
605 | |
606 | default: | |
607 | assert_not_reached("Unhandled option"); | |
912b54ad | 608 | } |
912b54ad | 609 | |
8569a776 LP |
610 | if (optind >= argc) { |
611 | log_error("Not enough parameters."); | |
912b54ad DS |
612 | return -EINVAL; |
613 | } | |
614 | ||
8569a776 LP |
615 | if (argc != optind+1) { |
616 | log_error("Too many parameters."); | |
912b54ad DS |
617 | return -EINVAL; |
618 | } | |
619 | ||
8569a776 | 620 | arg_remote_host = argv[optind]; |
912b54ad DS |
621 | return 1; |
622 | } | |
623 | ||
624 | int main(int argc, char *argv[]) { | |
8569a776 LP |
625 | Context context = {}; |
626 | int r, n, fd; | |
912b54ad DS |
627 | |
628 | log_parse_environment(); | |
629 | log_open(); | |
630 | ||
8569a776 | 631 | r = parse_argv(argc, argv); |
912b54ad DS |
632 | if (r <= 0) |
633 | goto finish; | |
634 | ||
fb69d709 | 635 | r = sd_event_default(&context.event); |
8569a776 | 636 | if (r < 0) { |
da927ba9 | 637 | log_error_errno(r, "Failed to allocate event loop: %m"); |
8569a776 LP |
638 | goto finish; |
639 | } | |
912b54ad | 640 | |
fb69d709 LP |
641 | r = sd_resolve_default(&context.resolve); |
642 | if (r < 0) { | |
da927ba9 | 643 | log_error_errno(r, "Failed to allocate resolver: %m"); |
fb69d709 LP |
644 | goto finish; |
645 | } | |
646 | ||
647 | r = sd_resolve_attach_event(context.resolve, context.event, 0); | |
648 | if (r < 0) { | |
da927ba9 | 649 | log_error_errno(r, "Failed to attach resolver: %m"); |
fb69d709 LP |
650 | goto finish; |
651 | } | |
652 | ||
653 | sd_event_set_watchdog(context.event, true); | |
cde93897 | 654 | |
8cf030b3 LP |
655 | n = sd_listen_fds(1); |
656 | if (n < 0) { | |
657 | log_error("Failed to receive sockets from parent."); | |
658 | r = n; | |
659 | goto finish; | |
660 | } else if (n == 0) { | |
661 | log_error("Didn't get any sockets passed in."); | |
662 | r = -EINVAL; | |
663 | goto finish; | |
664 | } | |
665 | ||
666 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) { | |
fb69d709 | 667 | r = add_listen_socket(&context, fd); |
8569a776 | 668 | if (r < 0) |
96c374d0 | 669 | goto finish; |
912b54ad DS |
670 | } |
671 | ||
fb69d709 | 672 | r = sd_event_loop(context.event); |
912b54ad | 673 | if (r < 0) { |
da927ba9 | 674 | log_error_errno(r, "Failed to run event loop: %m"); |
912b54ad DS |
675 | goto finish; |
676 | } | |
677 | ||
912b54ad | 678 | finish: |
8569a776 LP |
679 | context_free(&context); |
680 | ||
912b54ad DS |
681 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
682 | } |