blob: a719063a81d94bb408abb6688f1e1370b9e16b24 [file] [log] [blame]
William Lallemand2fe7dd02018-09-11 16:51:29 +02001/*
2 * Socket Pair protocol layer (sockpair)
3 *
4 * Copyright HAProxy Technologies - William Lallemand <wlallemand@haproxy.com>
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
13#include <ctype.h>
14#include <errno.h>
William Lallemand2fe7dd02018-09-11 16:51:29 +020015#include <pwd.h>
16#include <grp.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <syslog.h>
21#include <time.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/un.h>
27
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020028#include <haproxy/api.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020029#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020030#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/fd.h>
32#include <haproxy/freq_ctr.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020033#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020034#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020035#include <haproxy/listener.h>
Willy Tarreau344b8fc2020-10-15 09:43:31 +020036#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/protocol.h>
Willy Tarreau62292b22020-09-02 17:52:23 +020038#include <haproxy/proto_sockpair.h>
Willy Tarreau686fa3d2020-09-25 19:09:53 +020039#include <haproxy/sock.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020041#include <haproxy/version.h>
William Lallemand2fe7dd02018-09-11 16:51:29 +020042
William Lallemand2fe7dd02018-09-11 16:51:29 +020043
William Lallemand2fe7dd02018-09-11 16:51:29 +020044static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020045static void sockpair_enable_listener(struct listener *listener);
46static void sockpair_disable_listener(struct listener *listener);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020047static int sockpair_connect_server(struct connection *conn, int flags);
Willy Tarreau7d053e42020-10-15 09:19:43 +020048static int sockpair_accepting_conn(const struct receiver *rx);
Willy Tarreau344b8fc2020-10-15 09:43:31 +020049struct connection *sockpair_accept_conn(struct listener *l, int *status);
William Lallemand2fe7dd02018-09-11 16:51:29 +020050
Willy Tarreaub0254cb2020-09-04 08:07:11 +020051struct proto_fam proto_fam_sockpair = {
52 .name = "sockpair",
53 .sock_domain = AF_CUST_SOCKPAIR,
54 .sock_family = AF_UNIX,
55 .sock_addrlen = sizeof(struct sockaddr_un),
56 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),
57 .addrcmp = NULL,
58 .bind = sockpair_bind_receiver,
59 .get_src = NULL,
60 .get_dst = NULL,
61};
62
William Lallemand2fe7dd02018-09-11 16:51:29 +020063/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub9b2fd72020-12-08 14:13:11 +010064struct protocol proto_sockpair = {
Willy Tarreaub366c9a2020-12-08 14:54:20 +010065 .name = "sockpair",
66
67 /* connection layer */
Willy Tarreau91b47262022-05-20 16:36:46 +020068 .xprt_type = PROTO_TYPE_STREAM,
Willy Tarreaub366c9a2020-12-08 14:54:20 +010069 .listen = sockpair_bind_listener,
70 .enable = sockpair_enable_listener,
71 .disable = sockpair_disable_listener,
72 .add = default_add_listener,
73 .unbind = default_unbind_listener,
74 .accept_conn = sockpair_accept_conn,
Willy Tarreaude471c42020-12-08 15:50:56 +010075 .ctrl_init = sock_conn_ctrl_init,
76 .ctrl_close = sock_conn_ctrl_close,
Willy Tarreaub366c9a2020-12-08 14:54:20 +010077 .connect = sockpair_connect_server,
Willy Tarreau427c8462020-12-11 16:19:12 +010078 .drain = sock_drain,
Willy Tarreau472125b2020-12-11 17:02:50 +010079 .check_events = sock_check_events,
80 .ignore_events = sock_ignore_events,
Willy Tarreaub366c9a2020-12-08 14:54:20 +010081
82 /* binding layer */
83 /* Note: suspend/resume not supported */
84
85 /* address family */
86 .fam = &proto_fam_sockpair,
87
88 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +020089 .proto_type = PROTO_TYPE_STREAM,
Willy Tarreaub366c9a2020-12-08 14:54:20 +010090 .sock_type = SOCK_STREAM,
91 .sock_prot = 0,
92 .rx_enable = sock_enable,
93 .rx_disable = sock_disable,
94 .rx_unbind = sock_unbind,
95 .rx_listening = sockpair_accepting_conn,
96 .default_iocb = sock_accept_iocb,
97 .receivers = LIST_HEAD_INIT(proto_sockpair.receivers),
98 .nb_receivers = 0,
William Lallemand2fe7dd02018-09-11 16:51:29 +020099};
100
Willy Tarreau0108d902018-11-25 19:14:37 +0100101INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
102
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200103/* Enable receipt of incoming connections for listener <l>. The receiver must
Willy Tarreaua4380b22020-11-04 13:59:04 +0100104 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200105 */
106static void sockpair_enable_listener(struct listener *l)
107{
Willy Tarreaua4380b22020-11-04 13:59:04 +0100108 fd_want_recv_safe(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200109}
110
111/* Disable receipt of incoming connections for listener <l>. The receiver must
Willy Tarreaua4380b22020-11-04 13:59:04 +0100112 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200113 */
114static void sockpair_disable_listener(struct listener *l)
115{
Willy Tarreaua4380b22020-11-04 13:59:04 +0100116 fd_stop_recv(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200117}
118
Willy Tarreau233ad282020-10-15 21:45:15 +0200119/* Binds receiver <rx>, and assigns rx->iocb and rx->owner as the callback
120 * and context, respectively, with ->bind_thread as the thread mask. Returns an
121 * error code made of ERR_* bits on failure or ERR_NONE on success. On failure,
122 * an error message may be passed into <errmsg>. Note that the binding address
123 * is only an FD to receive the incoming FDs on. Thus by definition there is no
124 * real "bind" operation, this only completes the receiver. Such FDs are not
Willy Tarreau62292b22020-09-02 17:52:23 +0200125 * inherited upon reload.
126 */
Willy Tarreau233ad282020-10-15 21:45:15 +0200127int sockpair_bind_receiver(struct receiver *rx, char **errmsg)
Willy Tarreau62292b22020-09-02 17:52:23 +0200128{
129 int err;
130
131 /* ensure we never return garbage */
132 if (errmsg)
133 *errmsg = 0;
134
135 err = ERR_NONE;
136
137 if (rx->flags & RX_F_BOUND)
138 return ERR_NONE;
139
Willy Tarreau0e1aaf42023-02-27 16:39:32 +0100140 if (rx->flags & RX_F_MUST_DUP) {
141 /* this is a secondary receiver that is an exact copy of a
142 * reference which must already be bound (or has failed).
143 * We'll try to dup() the other one's FD and take it. We
144 * try hard not to reconfigure the socket since it's shared.
145 */
146 BUG_ON(!rx->shard_info);
147 if (!(rx->shard_info->ref->flags & RX_F_BOUND)) {
148 /* it's assumed that the first one has already reported
149 * the error, let's not spam with another one, and do
150 * not set ERR_ALERT.
151 */
152 err |= ERR_RETRYABLE;
153 goto bind_ret_err;
154 }
155 /* taking the other one's FD will result in it being marked
156 * extern and being dup()ed. Let's mark the receiver as
157 * inherited so that it properly bypasses all second-stage
158 * setup and avoids being passed to new processes.
159 */
160 rx->flags |= RX_F_INHERITED;
161 rx->fd = rx->shard_info->ref->fd;
162 }
163
Willy Tarreau62292b22020-09-02 17:52:23 +0200164 if (rx->fd == -1) {
165 err |= ERR_FATAL | ERR_ALERT;
166 memprintf(errmsg, "sockpair may be only used with inherited FDs");
167 goto bind_return;
168 }
169
170 if (rx->fd >= global.maxsock) {
171 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
172 memprintf(errmsg, "not enough free sockets (raise '-n' parameter)");
173 goto bind_close_return;
174 }
175
Willy Tarreau38247432022-04-26 10:24:14 +0200176 if (fd_set_nonblock(rx->fd) == -1) {
Willy Tarreau62292b22020-09-02 17:52:23 +0200177 err |= ERR_FATAL | ERR_ALERT;
178 memprintf(errmsg, "cannot make socket non-blocking");
179 goto bind_close_return;
180 }
181
182 rx->flags |= RX_F_BOUND;
183
Willy Tarreau9464bb12022-07-05 05:16:13 +0200184 fd_insert(rx->fd, rx->owner, rx->iocb, rx->bind_tgroup, rx->bind_thread);
Willy Tarreau62292b22020-09-02 17:52:23 +0200185 return err;
186
187 bind_return:
188 if (errmsg && *errmsg)
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200189 memprintf(errmsg, "%s for [fd %d]", *errmsg, rx->fd);
Willy Tarreau62292b22020-09-02 17:52:23 +0200190
Willy Tarreau0e1aaf42023-02-27 16:39:32 +0100191 bind_ret_err:
Willy Tarreau62292b22020-09-02 17:52:23 +0200192 return err;
193
194 bind_close_return:
195 close(rx->fd);
196 goto bind_return;
197}
198
William Lallemand2fe7dd02018-09-11 16:51:29 +0200199/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
200 * enabled for polling. The return value is composed from ERR_NONE,
201 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
202 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
203 * Note that <errmsg> may be NULL if <errlen> is also zero.
204 */
205static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
206{
William Lallemand2fe7dd02018-09-11 16:51:29 +0200207 int err;
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200208 char *msg = NULL;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200209
210 err = ERR_NONE;
211
212 /* ensure we never return garbage */
213 if (errlen)
214 *errmsg = 0;
215
216 if (listener->state != LI_ASSIGNED)
217 return ERR_NONE; /* already bound */
218
Willy Tarreauad33acf2020-09-02 18:40:02 +0200219 if (!(listener->rx.flags & RX_F_BOUND)) {
220 msg = "receiving socket not bound";
221 goto err_return;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200222 }
Willy Tarreauad33acf2020-09-02 18:40:02 +0200223
Willy Tarreaua37b2442020-09-24 07:23:45 +0200224 listener_set_state(listener, LI_LISTEN);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200225 return err;
226
227 err_return:
228 if (msg && errlen)
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200229 snprintf(errmsg, errlen, "%s [fd %d]", msg, listener->rx.fd);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200230 return err;
231}
232
233/*
234 * Send FD over a unix socket
235 *
236 * <send_fd> is the FD to send
237 * <fd> is the fd of the unix socket to use for the transfer
238 *
239 * The iobuf variable could be use in the future to enhance the protocol.
240 */
241int send_fd_uxst(int fd, int send_fd)
242{
243 char iobuf[2];
244 struct iovec iov;
245 struct msghdr msghdr;
246
247 char cmsgbuf[CMSG_SPACE(sizeof(int))];
248 char buf[CMSG_SPACE(sizeof(int))];
249 struct cmsghdr *cmsg = (void *)buf;
250
251 int *fdptr;
252
253 iov.iov_base = iobuf;
254 iov.iov_len = sizeof(iobuf);
255
256 memset(&msghdr, 0, sizeof(msghdr));
257 msghdr.msg_iov = &iov;
258 msghdr.msg_iovlen = 1;
259
260 /* Now send the fds */
261 msghdr.msg_control = cmsgbuf;
262 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
263
264 cmsg = CMSG_FIRSTHDR(&msghdr);
265 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
266 cmsg->cmsg_level = SOL_SOCKET;
267 cmsg->cmsg_type = SCM_RIGHTS;
268
269 fdptr = (int *)CMSG_DATA(cmsg);
270 memcpy(fdptr, &send_fd, sizeof(send_fd));
271
272 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
William Lallemandf67e8fb2022-07-25 15:51:30 +0200273 return -1;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200274 }
275
276 return 0;
277}
278
279/*
280 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800281 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200282 * socket and establishing a connection, it creates a pair of connected
283 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200284 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200285 *
286 * conn->target may point either to a valid server or to a backend, depending
287 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
288 * <data> parameter is a boolean indicating whether there are data waiting for
289 * being sent or not, in order to adjust data write polling and on some
290 * platforms. The <delack> argument is ignored.
291 *
292 * Note that a pending send_proxy message accounts for data.
293 *
294 * It can return one of :
295 * - SF_ERR_NONE if everything's OK
296 * - SF_ERR_SRVTO if there are no more servers
297 * - SF_ERR_SRVCL if the connection was refused by the server
298 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
299 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
300 * - SF_ERR_INTERNAL for any other purely internal errors
301 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
302 *
303 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
304 * it's invalid and the caller has nothing to do.
305 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200306static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200307{
308 int sv[2], fd, dst_fd = -1;
309
Willy Tarreau158b6cf2022-05-02 17:45:12 +0200310 BUG_ON(!conn->dst);
311
William Lallemand2fe7dd02018-09-11 16:51:29 +0200312 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200313 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200314
William Lallemand2fe7dd02018-09-11 16:51:29 +0200315 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
316 obj_type(conn->target) != OBJ_TYPE_SERVER) {
317 conn->flags |= CO_FL_ERROR;
318 return SF_ERR_INTERNAL;
319 }
320
321 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
322 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
323 conn->flags |= CO_FL_ERROR;
324 return SF_ERR_RESOURCE;
325 }
326
327 fd = conn->handle.fd = sv[1];
328
329 if (fd >= global.maxsock) {
330 /* do not log anything there, it's a normal condition when this option
331 * is used to serialize connections to a server !
332 */
333 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
334 close(sv[0]);
335 close(sv[1]);
336 conn->err_code = CO_ER_CONF_FDLIM;
337 conn->flags |= CO_FL_ERROR;
338 return SF_ERR_PRXCOND; /* it is a configuration limit */
339 }
340
Willy Tarreau38247432022-04-26 10:24:14 +0200341 if (fd_set_nonblock(fd) == -1) {
William Lallemand2fe7dd02018-09-11 16:51:29 +0200342 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
343 close(sv[0]);
344 close(sv[1]);
345 conn->err_code = CO_ER_SOCK_ERR;
346 conn->flags |= CO_FL_ERROR;
347 return SF_ERR_INTERNAL;
348 }
349
Willy Tarreau38247432022-04-26 10:24:14 +0200350 if (master == 1 && fd_set_cloexec(fd) == -1) {
William Lallemandc03eb012018-11-27 12:02:37 +0100351 ha_alert("Cannot set CLOEXEC on client socket.\n");
352 close(sv[0]);
353 close(sv[1]);
354 conn->err_code = CO_ER_SOCK_ERR;
355 conn->flags |= CO_FL_ERROR;
356 return SF_ERR_INTERNAL;
357 }
358
William Lallemand2fe7dd02018-09-11 16:51:29 +0200359 if (global.tune.server_sndbuf)
360 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
361
362 if (global.tune.server_rcvbuf)
363 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
364
365 /* The new socket is sent on the other side, it should be retrieved and
366 * considered as an 'accept' socket on the server side */
367 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
William Lallemand708949d2022-07-25 16:04:38 +0200368 ha_alert("socketpair: Cannot transfer the fd %d over sockpair@%d. Giving up.\n", sv[0], dst_fd);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200369 close(sv[0]);
370 close(sv[1]);
371 conn->err_code = CO_ER_SOCK_ERR;
372 conn->flags |= CO_FL_ERROR;
373 return SF_ERR_INTERNAL;
374 }
375
376 close(sv[0]); /* we don't need this side anymore */
377
378 conn->flags &= ~CO_FL_WAIT_L4_CONN;
379
William Lallemand2fe7dd02018-09-11 16:51:29 +0200380 /* Prepare to send a few handshakes related to the on-wire protocol. */
381 if (conn->send_proxy_ofs)
382 conn->flags |= CO_FL_SEND_PROXY;
383
384 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreaub41a6e92021-04-06 17:49:19 +0200385 HA_ATOMIC_AND(&fdtab[fd].state, ~FD_LINGER_RISK); /* no need to disable lingering */
William Lallemand2fe7dd02018-09-11 16:51:29 +0200386
William Lallemand2fe7dd02018-09-11 16:51:29 +0200387 return SF_ERR_NONE; /* connection is OK */
388}
389
390
391/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800392 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200393 *
394 * Return -1 or a socket fd;
395 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800396 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200397 */
398int recv_fd_uxst(int sock)
399{
400 struct msghdr msghdr;
401 struct iovec iov;
402 char iobuf[2];
403
404 char cmsgbuf[CMSG_SPACE(sizeof(int))];
405 char buf[CMSG_SPACE(sizeof(int))];
406 struct cmsghdr *cmsg = (void *)buf;
407
408
409 int recv_fd = -1;
410 int ret = -1;
411
412 memset(&msghdr, 0, sizeof(msghdr));
413
414 iov.iov_base = iobuf;
415 iov.iov_len = sizeof(iobuf);
416
417 msghdr.msg_iov = &iov;
418 msghdr.msg_iovlen = 1;
419
420 msghdr.msg_control = cmsgbuf;
421 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
422
423 iov.iov_len = sizeof(iobuf);
424 iov.iov_base = iobuf;
425
426 while (1) {
427 ret = recvmsg(sock, &msghdr, 0);
428 if (ret == -1 && errno == EINTR)
429 continue;
430 else
431 break;
432 }
433
434 if (ret == -1)
435 return ret;
436
437 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200438 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200439 cmsg->cmsg_type == SCM_RIGHTS) {
440 size_t totlen = cmsg->cmsg_len -
441 CMSG_LEN(0);
442 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
443 }
444 return recv_fd;
445}
446
Willy Tarreaucc8b6532020-10-13 17:27:34 +0200447/* Tests if the receiver supports accepting connections. Returns positive on
448 * success, 0 if not possible, negative if the socket is non-recoverable. In
449 * practice zero is never returned since we don't support suspending sockets.
450 * The real test consists in verifying we have a connected SOCK_STREAM of
451 * family AF_UNIX.
452 */
Willy Tarreau7d053e42020-10-15 09:19:43 +0200453static int sockpair_accepting_conn(const struct receiver *rx)
Willy Tarreaucc8b6532020-10-13 17:27:34 +0200454{
455 struct sockaddr sa;
456 socklen_t len;
457 int val;
458
459 len = sizeof(val);
460 if (getsockopt(rx->fd, SOL_SOCKET, SO_TYPE, &val, &len) == -1)
461 return -1;
462
463 if (val != SOCK_STREAM)
464 return -1;
465
466 len = sizeof(sa);
467 if (getsockname(rx->fd, &sa, &len) != 0)
468 return -1;
469
470 if (sa.sa_family != AF_UNIX)
471 return -1;
472
473 len = sizeof(val);
474 if (getsockopt(rx->fd, SOL_SOCKET, SO_ACCEPTCONN, &val, &len) == -1)
475 return -1;
476
477 /* Note: cannot be a listening socket, must be established */
478 if (val)
479 return -1;
480
481 return 1;
482}
483
Willy Tarreau344b8fc2020-10-15 09:43:31 +0200484/* Accept an incoming connection from listener <l>, and return it, as well as
485 * a CO_AC_* status code into <status> if not null. Null is returned on error.
486 * <l> must be a valid listener with a valid frontend.
487 */
488struct connection *sockpair_accept_conn(struct listener *l, int *status)
489{
490 struct proxy *p = l->bind_conf->frontend;
491 struct connection *conn = NULL;
492 int ret;
493 int cfd;
494
495 if ((cfd = recv_fd_uxst(l->rx.fd)) != -1)
Willy Tarreau38247432022-04-26 10:24:14 +0200496 fd_set_nonblock(cfd);
Willy Tarreau344b8fc2020-10-15 09:43:31 +0200497
498 if (likely(cfd != -1)) {
499 /* Perfect, the connection was accepted */
500 conn = conn_new(&l->obj_type);
501 if (!conn)
502 goto fail_conn;
503
504 if (!sockaddr_alloc(&conn->src, NULL, 0))
505 goto fail_addr;
506
507 /* just like with UNIX sockets, only the family is filled */
508 conn->src->ss_family = AF_UNIX;
509 conn->handle.fd = cfd;
Willy Tarreau344b8fc2020-10-15 09:43:31 +0200510 ret = CO_AC_DONE;
511 goto done;
512 }
513
514 switch (errno) {
Willy Tarreauacef5e22022-04-25 20:32:15 +0200515#if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN
516 case EWOULDBLOCK:
517#endif
Willy Tarreau344b8fc2020-10-15 09:43:31 +0200518 case EAGAIN:
519 ret = CO_AC_DONE; /* nothing more to accept */
Willy Tarreauf5090652021-04-06 17:23:40 +0200520 if (fdtab[l->rx.fd].state & (FD_POLL_HUP|FD_POLL_ERR)) {
Willy Tarreau344b8fc2020-10-15 09:43:31 +0200521 /* the listening socket might have been disabled in a shared
522 * process and we're a collateral victim. We'll just pause for
523 * a while in case it comes back. In the mean time, we need to
524 * clear this sticky flag.
525 */
Willy Tarreauf5090652021-04-06 17:23:40 +0200526 _HA_ATOMIC_AND(&fdtab[l->rx.fd].state, ~(FD_POLL_HUP|FD_POLL_ERR));
Willy Tarreau344b8fc2020-10-15 09:43:31 +0200527 ret = CO_AC_PAUSE;
528 }
529 fd_cant_recv(l->rx.fd);
530 break;
531
532 case EINVAL:
533 /* might be trying to accept on a shut fd (eg: soft stop) */
534 ret = CO_AC_PAUSE;
535 break;
536
537 case EINTR:
538 case ECONNABORTED:
539 ret = CO_AC_RETRY;
540 break;
541
542 case ENFILE:
543 if (p)
544 send_log(p, LOG_EMERG,
545 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
546 p->id, global.maxsock);
547 ret = CO_AC_PAUSE;
548 break;
549
550 case EMFILE:
551 if (p)
552 send_log(p, LOG_EMERG,
553 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
554 p->id, global.maxsock);
555 ret = CO_AC_PAUSE;
556 break;
557
558 case ENOBUFS:
559 case ENOMEM:
560 if (p)
561 send_log(p, LOG_EMERG,
562 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
563 p->id, global.maxsock);
564 ret = CO_AC_PAUSE;
565 break;
566
567 default:
568 /* unexpected result, let's give up and let other tasks run */
569 ret = CO_AC_YIELD;
570 }
571 done:
572 if (status)
573 *status = ret;
574 return conn;
575
576 fail_addr:
577 conn_free(conn);
578 conn = NULL;
579 fail_conn:
580 ret = CO_AC_PAUSE;
581 goto done;
582}
583
William Lallemand2fe7dd02018-09-11 16:51:29 +0200584/*
585 * Local variables:
586 * c-indent-level: 8
587 * c-basic-offset: 8
588 * End:
589 */