blob: f70bb744a2bd69dee5158cd87f078b60497dfae2 [file] [log] [blame]
Willy Tarreau18b7df72020-08-28 12:07:22 +02001/*
2 * Generic code for native (BSD-compatible) sockets
3 *
4 * Copyright 2000-2020 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreauf1dc9f22020-10-15 09:21:31 +020013#define _GNU_SOURCE
Willy Tarreau18b7df72020-08-28 12:07:22 +020014#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <time.h>
21
22#include <sys/param.h>
23#include <sys/socket.h>
24#include <sys/types.h>
25
Willy Tarreau42961742020-08-28 18:42:45 +020026#include <net/if.h>
27
Willy Tarreau18b7df72020-08-28 12:07:22 +020028#include <haproxy/api.h>
29#include <haproxy/connection.h>
Willy Tarreau063d47d2020-08-28 16:29:53 +020030#include <haproxy/listener-t.h>
Willy Tarreauf1dc9f22020-10-15 09:21:31 +020031#include <haproxy/log.h>
Willy Tarreau18b7df72020-08-28 12:07:22 +020032#include <haproxy/namespace.h>
33#include <haproxy/sock.h>
Willy Tarreau2d34a712020-08-28 16:49:41 +020034#include <haproxy/sock_inet.h>
Willy Tarreau18b7df72020-08-28 12:07:22 +020035#include <haproxy/tools.h>
36
Willy Tarreau063d47d2020-08-28 16:29:53 +020037/* the list of remaining sockets transferred from an older process */
38struct xfer_sock_list *xfer_sock_list = NULL;
Willy Tarreau18b7df72020-08-28 12:07:22 +020039
Willy Tarreauf1dc9f22020-10-15 09:21:31 +020040
41/* Accept an incoming connection from listener <l>, and return it, as well as
42 * a CO_AC_* status code into <status> if not null. Null is returned on error.
43 * <l> must be a valid listener with a valid frontend.
44 */
45struct connection *sock_accept_conn(struct listener *l, int *status)
46{
47#ifdef USE_ACCEPT4
48 static int accept4_broken;
49#endif
50 struct proxy *p = l->bind_conf->frontend;
51 struct connection *conn;
52 socklen_t laddr;
53 int ret;
54 int cfd;
55
56 conn = conn_new(&l->obj_type);
57 if (!conn)
58 goto fail_conn;
59
60 if (!sockaddr_alloc(&conn->src, NULL, 0))
61 goto fail_addr;
62
63 /* accept() will mark all accepted FDs O_NONBLOCK and the ones accepted
64 * in the master process as FD_CLOEXEC. It's not done for workers
65 * because 1) workers are not supposed to execute anything so there's
66 * no reason for uselessly slowing down everything, and 2) that would
67 * prevent us from implementing fd passing in the future.
68 */
69#ifdef USE_ACCEPT4
70 laddr = sizeof(*conn->src);
71
72 /* only call accept4() if it's known to be safe, otherwise fallback to
73 * the legacy accept() + fcntl().
74 */
75 if (unlikely(accept4_broken) ||
76 (((cfd = accept4(l->rx.fd, (struct sockaddr *)conn->src, &laddr,
77 SOCK_NONBLOCK | (master ? SOCK_CLOEXEC : 0))) == -1) &&
78 (errno == ENOSYS || errno == EINVAL || errno == EBADF) &&
79 (accept4_broken = 1)))
80#endif
81 {
82 laddr = sizeof(*conn->src);
83 if ((cfd = accept(l->rx.fd, (struct sockaddr *)conn->src, &laddr)) != -1) {
84 fcntl(cfd, F_SETFL, O_NONBLOCK);
85 if (master)
86 fcntl(cfd, F_SETFD, FD_CLOEXEC);
87 }
88 }
89
90 if (likely(cfd != -1)) {
91 /* Perfect, the connection was accepted */
92 conn->handle.fd = cfd;
93 conn->flags |= CO_FL_ADDR_FROM_SET;
94 ret = CO_AC_DONE;
95 goto done;
96 }
97
98 /* error conditions below */
99 conn_free(conn);
100 conn = NULL;
101
102 switch (errno) {
103 case EAGAIN:
104 ret = CO_AC_DONE; /* nothing more to accept */
105 if (fdtab[l->rx.fd].ev & (FD_POLL_HUP|FD_POLL_ERR)) {
106 /* the listening socket might have been disabled in a shared
107 * process and we're a collateral victim. We'll just pause for
108 * a while in case it comes back. In the mean time, we need to
109 * clear this sticky flag.
110 */
111 _HA_ATOMIC_AND(&fdtab[l->rx.fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
112 ret = CO_AC_PAUSE;
113 }
114 fd_cant_recv(l->rx.fd);
115 break;
116
117 case EINVAL:
118 /* might be trying to accept on a shut fd (eg: soft stop) */
119 ret = CO_AC_PAUSE;
120 break;
121
122 case EINTR:
123 case ECONNABORTED:
124 ret = CO_AC_RETRY;
125 break;
126
127 case ENFILE:
128 if (p)
129 send_log(p, LOG_EMERG,
130 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
131 p->id, global.maxsock);
132 ret = CO_AC_PAUSE;
133 break;
134
135 case EMFILE:
136 if (p)
137 send_log(p, LOG_EMERG,
138 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
139 p->id, global.maxsock);
140 ret = CO_AC_PAUSE;
141 break;
142
143 case ENOBUFS:
144 case ENOMEM:
145 if (p)
146 send_log(p, LOG_EMERG,
147 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
148 p->id, global.maxsock);
149 ret = CO_AC_PAUSE;
150 break;
151
152 default:
153 /* unexpected result, let's give up and let other tasks run */
154 ret = CO_AC_YIELD;
155 }
156 done:
157 if (status)
158 *status = ret;
159 return conn;
160
161 fail_addr:
162 conn_free(conn);
163 conn = NULL;
164 fail_conn:
165 ret = CO_AC_PAUSE;
166 goto done;
167}
168
Willy Tarreau18b7df72020-08-28 12:07:22 +0200169/* Create a socket to connect to the server in conn->dst (which MUST be valid),
170 * using the configured namespace if needed, or the one passed by the proxy
171 * protocol if required to do so. It ultimately calls socket() or socketat()
172 * and returns the FD or error code.
173 */
174int sock_create_server_socket(struct connection *conn)
175{
176 const struct netns_entry *ns = NULL;
177
178#ifdef USE_NS
179 if (objt_server(conn->target)) {
180 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
181 ns = conn->proxy_netns;
182 else
183 ns = __objt_server(conn->target)->netns;
184 }
185#endif
186 return my_socketat(ns, conn->dst->ss_family, SOCK_STREAM, 0);
187}
188
Willy Tarreaue70c7972020-09-25 19:00:01 +0200189/* Enables receiving on receiver <rx> once already bound. Does nothing in early
190 * boot (needs fd_updt).
191 */
192void sock_enable(struct receiver *rx)
193{
194 if (rx->flags & RX_F_BOUND && fd_updt)
195 fd_want_recv(rx->fd);
196}
197
198/* Disables receiving on receiver <rx> once already bound. Does nothing in early
199 * boot (needs fd_updt).
200 */
201void sock_disable(struct receiver *rx)
202{
203 if (rx->flags & RX_F_BOUND && fd_updt)
204 fd_stop_recv(rx->fd);
205}
206
Willy Tarreauf58b8db2020-10-09 16:32:08 +0200207/* stops, unbinds and possibly closes the FD associated with receiver rx */
208void sock_unbind(struct receiver *rx)
209{
210 /* There are a number of situations where we prefer to keep the FD and
211 * not to close it (unless we're stopping, of course):
212 * - worker process unbinding from a worker's FD with socket transfer enabled => keep
213 * - master process unbinding from a master's inherited FD => keep
214 * - master process unbinding from a master's FD => close
215 * - master process unbinding from a worker's FD => close
216 * - worker process unbinding from a master's FD => close
217 * - worker process unbinding from a worker's FD => close
218 */
219 if (rx->flags & RX_F_BOUND)
220 rx->proto->rx_disable(rx);
221
222 if (!stopping && !master &&
223 !(rx->flags & RX_F_MWORKER) &&
224 (global.tune.options & GTUNE_SOCKET_TRANSFER))
225 return;
226
227 if (!stopping && master &&
228 rx->flags & RX_F_MWORKER &&
229 rx->flags & RX_F_INHERITED)
230 return;
231
232 rx->flags &= ~RX_F_BOUND;
233 if (rx->fd != -1)
234 fd_delete(rx->fd);
235 rx->fd = -1;
236}
237
Willy Tarreau18b7df72020-08-28 12:07:22 +0200238/*
239 * Retrieves the source address for the socket <fd>, with <dir> indicating
240 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
241 * success, -1 in case of error. The socket's source address is stored in
242 * <sa> for <salen> bytes.
243 */
244int sock_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
245{
246 if (dir)
247 return getsockname(fd, sa, &salen);
248 else
249 return getpeername(fd, sa, &salen);
250}
251
252/*
253 * Retrieves the original destination address for the socket <fd>, with <dir>
254 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
255 * case of success, -1 in case of error. The socket's source address is stored
256 * in <sa> for <salen> bytes.
257 */
258int sock_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
259{
260 if (dir)
261 return getpeername(fd, sa, &salen);
262 else
263 return getsockname(fd, sa, &salen);
264}
265
Willy Tarreau42961742020-08-28 18:42:45 +0200266/* Try to retrieve exported sockets from worker at CLI <unixsocket>. These
267 * ones will be placed into the xfer_sock_list for later use by function
268 * sock_find_compatible_fd(). Returns 0 on success, -1 on failure.
269 */
270int sock_get_old_sockets(const char *unixsocket)
271{
272 char *cmsgbuf = NULL, *tmpbuf = NULL;
273 int *tmpfd = NULL;
274 struct sockaddr_un addr;
275 struct cmsghdr *cmsg;
276 struct msghdr msghdr;
277 struct iovec iov;
278 struct xfer_sock_list *xfer_sock = NULL;
279 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
280 int sock = -1;
281 int ret = -1;
282 int ret2 = -1;
283 int fd_nb;
284 int got_fd = 0;
285 int cur_fd = 0;
286 size_t maxoff = 0, curoff = 0;
287
288 memset(&msghdr, 0, sizeof(msghdr));
289 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
290 if (!cmsgbuf) {
291 ha_warning("Failed to allocate memory to send sockets\n");
292 goto out;
293 }
294
295 sock = socket(PF_UNIX, SOCK_STREAM, 0);
296 if (sock < 0) {
297 ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket);
298 goto out;
299 }
300
301 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path) - 1);
302 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
303 addr.sun_family = PF_UNIX;
304
305 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
306 if (ret < 0) {
307 ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket);
308 goto out;
309 }
310
311 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
312 iov.iov_base = &fd_nb;
313 iov.iov_len = sizeof(fd_nb);
314 msghdr.msg_iov = &iov;
315 msghdr.msg_iovlen = 1;
316
317 if (send(sock, "_getsocks\n", strlen("_getsocks\n"), 0) != strlen("_getsocks\n")) {
318 ha_warning("Failed to get the number of sockets to be transferred !\n");
319 goto out;
320 }
321
322 /* First, get the number of file descriptors to be received */
323 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
324 ha_warning("Failed to get the number of sockets to be transferred !\n");
325 goto out;
326 }
327
328 if (fd_nb == 0) {
329 ret2 = 0;
330 goto out;
331 }
332
333 tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
334 if (tmpbuf == NULL) {
335 ha_warning("Failed to allocate memory while receiving sockets\n");
336 goto out;
337 }
338
339 tmpfd = malloc(fd_nb * sizeof(int));
340 if (tmpfd == NULL) {
341 ha_warning("Failed to allocate memory while receiving sockets\n");
342 goto out;
343 }
344
345 msghdr.msg_control = cmsgbuf;
346 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
347 iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int));
348
349 do {
350 int ret3;
351
352 iov.iov_base = tmpbuf + curoff;
353
354 ret = recvmsg(sock, &msghdr, 0);
355
356 if (ret == -1 && errno == EINTR)
357 continue;
358
359 if (ret <= 0)
360 break;
361
362 /* Send an ack to let the sender know we got the sockets
363 * and it can send some more
364 */
365 do {
366 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
367 } while (ret3 == -1 && errno == EINTR);
368
369 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
370 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
371 size_t totlen = cmsg->cmsg_len - CMSG_LEN(0);
372
373 if (totlen / sizeof(int) + got_fd > fd_nb) {
374 ha_warning("Got to many sockets !\n");
375 goto out;
376 }
377
378 /*
379 * Be paranoid and use memcpy() to avoid any
380 * potential alignment issue.
381 */
382 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
383 got_fd += totlen / sizeof(int);
384 }
385 }
386 curoff += ret;
387 } while (got_fd < fd_nb);
388
389 if (got_fd != fd_nb) {
390 ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
391 fd_nb, got_fd);
392 goto out;
393 }
394
395 maxoff = curoff;
396 curoff = 0;
397
398 for (cur_fd = 0; cur_fd < got_fd; cur_fd++) {
399 int fd = tmpfd[cur_fd];
400 socklen_t socklen;
401 int val;
402 int len;
403
404 xfer_sock = calloc(1, sizeof(*xfer_sock));
405 if (!xfer_sock) {
406 ha_warning("Failed to allocate memory in get_old_sockets() !\n");
407 break;
408 }
409 xfer_sock->fd = -1;
410
411 socklen = sizeof(xfer_sock->addr);
412 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
413 ha_warning("Failed to get socket address\n");
414 free(xfer_sock);
415 xfer_sock = NULL;
416 continue;
417 }
418
419 if (curoff >= maxoff) {
420 ha_warning("Inconsistency while transferring sockets\n");
421 goto out;
422 }
423
424 len = tmpbuf[curoff++];
425 if (len > 0) {
426 /* We have a namespace */
427 if (curoff + len > maxoff) {
428 ha_warning("Inconsistency while transferring sockets\n");
429 goto out;
430 }
431 xfer_sock->namespace = malloc(len + 1);
432 if (!xfer_sock->namespace) {
433 ha_warning("Failed to allocate memory while transferring sockets\n");
434 goto out;
435 }
436 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
437 xfer_sock->namespace[len] = 0;
438 xfer_sock->ns_namelen = len;
439 curoff += len;
440 }
441
442 if (curoff >= maxoff) {
443 ha_warning("Inconsistency while transferring sockets\n");
444 goto out;
445 }
446
447 len = tmpbuf[curoff++];
448 if (len > 0) {
449 /* We have an interface */
450 if (curoff + len > maxoff) {
451 ha_warning("Inconsistency while transferring sockets\n");
452 goto out;
453 }
454 xfer_sock->iface = malloc(len + 1);
455 if (!xfer_sock->iface) {
456 ha_warning("Failed to allocate memory while transferring sockets\n");
457 goto out;
458 }
459 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
460 xfer_sock->iface[len] = 0;
461 xfer_sock->if_namelen = len;
462 curoff += len;
463 }
464
465 if (curoff + sizeof(int) > maxoff) {
466 ha_warning("Inconsistency while transferring sockets\n");
467 goto out;
468 }
469
470 /* we used to have 32 bits of listener options here but we don't
471 * use them anymore.
472 */
473 curoff += sizeof(int);
474
475 /* determine the foreign status directly from the socket itself */
476 if (sock_inet_is_foreign(fd, xfer_sock->addr.ss_family))
Willy Tarreaua2c17872020-08-28 19:09:19 +0200477 xfer_sock->options |= SOCK_XFER_OPT_FOREIGN;
Willy Tarreau42961742020-08-28 18:42:45 +0200478
Willy Tarreau9dbb6c42020-08-28 19:20:23 +0200479 socklen = sizeof(val);
480 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &val, &socklen) == 0 && val == SOCK_DGRAM)
481 xfer_sock->options |= SOCK_XFER_OPT_DGRAM;
482
Willy Tarreau42961742020-08-28 18:42:45 +0200483#if defined(IPV6_V6ONLY)
484 /* keep only the v6only flag depending on what's currently
485 * active on the socket, and always drop the v4v6 one.
486 */
487 socklen = sizeof(val);
488 if (xfer_sock->addr.ss_family == AF_INET6 &&
489 getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &socklen) == 0 && val > 0)
Willy Tarreaua2c17872020-08-28 19:09:19 +0200490 xfer_sock->options |= SOCK_XFER_OPT_V6ONLY;
Willy Tarreau42961742020-08-28 18:42:45 +0200491#endif
492
493 xfer_sock->fd = fd;
494 if (xfer_sock_list)
495 xfer_sock_list->prev = xfer_sock;
496 xfer_sock->next = xfer_sock_list;
497 xfer_sock->prev = NULL;
498 xfer_sock_list = xfer_sock;
499 xfer_sock = NULL;
500 }
501
502 ret2 = 0;
503out:
504 /* If we failed midway make sure to close the remaining
505 * file descriptors
506 */
507 if (tmpfd != NULL && cur_fd < got_fd) {
508 for (; cur_fd < got_fd; cur_fd++) {
509 close(tmpfd[cur_fd]);
510 }
511 }
512
513 free(tmpbuf);
514 free(tmpfd);
515 free(cmsgbuf);
516
517 if (sock != -1)
518 close(sock);
519
520 if (xfer_sock) {
521 free(xfer_sock->namespace);
522 free(xfer_sock->iface);
523 if (xfer_sock->fd != -1)
524 close(xfer_sock->fd);
525 free(xfer_sock);
526 }
527 return (ret2);
528}
529
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200530/* When binding the receivers, check if a socket has been sent to us by the
Willy Tarreau2d34a712020-08-28 16:49:41 +0200531 * previous process that we could reuse, instead of creating a new one. Note
532 * that some address family-specific options are checked on the listener and
533 * on the socket. Typically for AF_INET and AF_INET6, we check for transparent
534 * mode, and for AF_INET6 we also check for "v4v6" or "v6only". The reused
535 * socket is automatically removed from the list so that it's not proposed
536 * anymore.
537 */
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200538int sock_find_compatible_fd(const struct receiver *rx)
Willy Tarreau2d34a712020-08-28 16:49:41 +0200539{
540 struct xfer_sock_list *xfer_sock = xfer_sock_list;
Willy Tarreaua2c17872020-08-28 19:09:19 +0200541 int options = 0;
Willy Tarreau2d34a712020-08-28 16:49:41 +0200542 int if_namelen = 0;
543 int ns_namelen = 0;
544 int ret = -1;
545
Willy Tarreauf1f66092020-09-04 08:15:31 +0200546 if (!rx->proto->fam->addrcmp)
Willy Tarreau2d34a712020-08-28 16:49:41 +0200547 return -1;
548
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200549 if (rx->proto->sock_type == SOCK_DGRAM)
Willy Tarreau9dbb6c42020-08-28 19:20:23 +0200550 options |= SOCK_XFER_OPT_DGRAM;
551
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200552 if (rx->settings->options & RX_O_FOREIGN)
Willy Tarreaua2c17872020-08-28 19:09:19 +0200553 options |= SOCK_XFER_OPT_FOREIGN;
554
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200555 if (rx->addr.ss_family == AF_INET6) {
Willy Tarreau2d34a712020-08-28 16:49:41 +0200556 /* Prepare to match the v6only option against what we really want. Note
557 * that sadly the two options are not exclusive to each other and that
558 * v6only is stronger than v4v6.
559 */
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200560 if ((rx->settings->options & RX_O_V6ONLY) ||
561 (sock_inet6_v6only_default && !(rx->settings->options & RX_O_V4V6)))
Willy Tarreaua2c17872020-08-28 19:09:19 +0200562 options |= SOCK_XFER_OPT_V6ONLY;
Willy Tarreau2d34a712020-08-28 16:49:41 +0200563 }
Willy Tarreau2d34a712020-08-28 16:49:41 +0200564
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200565 if (rx->settings->interface)
566 if_namelen = strlen(rx->settings->interface);
Willy Tarreau2d34a712020-08-28 16:49:41 +0200567#ifdef USE_NS
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200568 if (rx->settings->netns)
569 ns_namelen = rx->settings->netns->name_len;
Willy Tarreau2d34a712020-08-28 16:49:41 +0200570#endif
571
572 while (xfer_sock) {
Willy Tarreaua2c17872020-08-28 19:09:19 +0200573 if ((options == xfer_sock->options) &&
Willy Tarreau2d34a712020-08-28 16:49:41 +0200574 (if_namelen == xfer_sock->if_namelen) &&
575 (ns_namelen == xfer_sock->ns_namelen) &&
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200576 (!if_namelen || strcmp(rx->settings->interface, xfer_sock->iface) == 0) &&
Willy Tarreau2d34a712020-08-28 16:49:41 +0200577#ifdef USE_NS
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200578 (!ns_namelen || strcmp(rx->settings->netns->node.key, xfer_sock->namespace) == 0) &&
Willy Tarreau2d34a712020-08-28 16:49:41 +0200579#endif
Willy Tarreauf1f66092020-09-04 08:15:31 +0200580 rx->proto->fam->addrcmp(&xfer_sock->addr, &rx->addr) == 0)
Willy Tarreau2d34a712020-08-28 16:49:41 +0200581 break;
582 xfer_sock = xfer_sock->next;
583 }
584
585 if (xfer_sock != NULL) {
586 ret = xfer_sock->fd;
587 if (xfer_sock == xfer_sock_list)
588 xfer_sock_list = xfer_sock->next;
589 if (xfer_sock->prev)
590 xfer_sock->prev->next = xfer_sock->next;
591 if (xfer_sock->next)
592 xfer_sock->next->prev = xfer_sock->prev;
593 free(xfer_sock->iface);
594 free(xfer_sock->namespace);
595 free(xfer_sock);
596 }
597 return ret;
598}
599
Willy Tarreau5ced3e82020-10-13 17:06:12 +0200600/* Tests if the receiver supports accepting connections. Returns positive on
601 * success, 0 if not possible, negative if the socket is non-recoverable. The
602 * rationale behind this is that inherited FDs may be broken and that shared
603 * FDs might have been paused by another process.
604 */
Willy Tarreau7d053e42020-10-15 09:19:43 +0200605int sock_accepting_conn(const struct receiver *rx)
Willy Tarreau5ced3e82020-10-13 17:06:12 +0200606{
607 int opt_val = 0;
608 socklen_t opt_len = sizeof(opt_val);
609
610 if (getsockopt(rx->fd, SOL_SOCKET, SO_ACCEPTCONN, &opt_val, &opt_len) == -1)
611 return -1;
612
613 return opt_val;
614}
615
Willy Tarreau18b7df72020-08-28 12:07:22 +0200616/*
617 * Local variables:
618 * c-indent-level: 8
619 * c-basic-offset: 8
620 * End:
621 */