blob: cc92753792005a5fb46760fc8c25e8a99ad2ea9b [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>
15#include <fcntl.h>
16#include <pwd.h>
17#include <grp.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <syslog.h>
22#include <time.h>
23
24#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <sys/un.h>
28
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020029#include <haproxy/api.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020030#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/fd.h>
33#include <haproxy/freq_ctr.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020034#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020035#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020036#include <haproxy/listener.h>
Willy Tarreau344b8fc2020-10-15 09:43:31 +020037#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/protocol.h>
Willy Tarreau62292b22020-09-02 17:52:23 +020039#include <haproxy/proto_sockpair.h>
Willy Tarreau686fa3d2020-09-25 19:09:53 +020040#include <haproxy/sock.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020041#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020042#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020043#include <haproxy/version.h>
William Lallemand2fe7dd02018-09-11 16:51:29 +020044
William Lallemand2fe7dd02018-09-11 16:51:29 +020045
46static void sockpair_add_listener(struct listener *listener, int port);
47static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020048static void sockpair_enable_listener(struct listener *listener);
49static void sockpair_disable_listener(struct listener *listener);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020050static int sockpair_connect_server(struct connection *conn, int flags);
Willy Tarreau7d053e42020-10-15 09:19:43 +020051static int sockpair_accepting_conn(const struct receiver *rx);
Willy Tarreau344b8fc2020-10-15 09:43:31 +020052struct connection *sockpair_accept_conn(struct listener *l, int *status);
William Lallemand2fe7dd02018-09-11 16:51:29 +020053
Willy Tarreaub0254cb2020-09-04 08:07:11 +020054struct proto_fam proto_fam_sockpair = {
55 .name = "sockpair",
56 .sock_domain = AF_CUST_SOCKPAIR,
57 .sock_family = AF_UNIX,
58 .sock_addrlen = sizeof(struct sockaddr_un),
59 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),
60 .addrcmp = NULL,
61 .bind = sockpair_bind_receiver,
62 .get_src = NULL,
63 .get_dst = NULL,
64};
65
William Lallemand2fe7dd02018-09-11 16:51:29 +020066/* Note: must not be declared <const> as its list will be overwritten */
67static struct protocol proto_sockpair = {
68 .name = "sockpair",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020069 .fam = &proto_fam_sockpair,
Willy Tarreaua54553f2020-09-16 17:50:45 +020070 .ctrl_type = SOCK_STREAM,
William Lallemand2fe7dd02018-09-11 16:51:29 +020071 .sock_domain = AF_CUST_SOCKPAIR,
72 .sock_type = SOCK_STREAM,
73 .sock_prot = 0,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020074 .add = sockpair_add_listener,
75 .listen = sockpair_bind_listener,
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020076 .enable = sockpair_enable_listener,
77 .disable = sockpair_disable_listener,
Willy Tarreau7b2febd2020-10-09 17:18:29 +020078 .unbind = default_unbind_listener,
Willy Tarreau344b8fc2020-10-15 09:43:31 +020079 .accept_conn = sockpair_accept_conn,
Willy Tarreauf58b8db2020-10-09 16:32:08 +020080 .rx_unbind = sock_unbind,
Willy Tarreau686fa3d2020-09-25 19:09:53 +020081 .rx_enable = sock_enable,
82 .rx_disable = sock_disable,
Willy Tarreau7d053e42020-10-15 09:19:43 +020083 .rx_listening = sockpair_accepting_conn,
Willy Tarreaua74cb382020-10-15 21:29:49 +020084 .default_iocb = &sock_accept_iocb,
William Lallemand2fe7dd02018-09-11 16:51:29 +020085 .connect = &sockpair_connect_server,
Willy Tarreaud7f331c2020-09-25 17:01:43 +020086 .receivers = LIST_HEAD_INIT(proto_sockpair.receivers),
87 .nb_receivers = 0,
William Lallemand2fe7dd02018-09-11 16:51:29 +020088};
89
Willy Tarreau0108d902018-11-25 19:14:37 +010090INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
91
William Lallemand2fe7dd02018-09-11 16:51:29 +020092/* Add <listener> to the list of sockpair listeners (port is ignored). The
93 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
94 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +020095 *
96 * Must be called with proto_lock held.
97 *
William Lallemand2fe7dd02018-09-11 16:51:29 +020098 */
99static void sockpair_add_listener(struct listener *listener, int port)
100{
101 if (listener->state != LI_INIT)
102 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +0200103 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +0200104 listener->rx.proto = &proto_sockpair;
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200105 LIST_ADDQ(&proto_sockpair.receivers, &listener->rx.proto_list);
106 proto_sockpair.nb_receivers++;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200107}
108
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200109/* Enable receipt of incoming connections for listener <l>. The receiver must
Willy Tarreaua4380b22020-11-04 13:59:04 +0100110 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200111 */
112static void sockpair_enable_listener(struct listener *l)
113{
Willy Tarreaua4380b22020-11-04 13:59:04 +0100114 fd_want_recv_safe(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200115}
116
117/* Disable receipt of incoming connections for listener <l>. The receiver must
Willy Tarreaua4380b22020-11-04 13:59:04 +0100118 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200119 */
120static void sockpair_disable_listener(struct listener *l)
121{
Willy Tarreaua4380b22020-11-04 13:59:04 +0100122 fd_stop_recv(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200123}
124
Willy Tarreau233ad282020-10-15 21:45:15 +0200125/* Binds receiver <rx>, and assigns rx->iocb and rx->owner as the callback
126 * and context, respectively, with ->bind_thread as the thread mask. Returns an
127 * error code made of ERR_* bits on failure or ERR_NONE on success. On failure,
128 * an error message may be passed into <errmsg>. Note that the binding address
129 * is only an FD to receive the incoming FDs on. Thus by definition there is no
130 * real "bind" operation, this only completes the receiver. Such FDs are not
Willy Tarreau62292b22020-09-02 17:52:23 +0200131 * inherited upon reload.
132 */
Willy Tarreau233ad282020-10-15 21:45:15 +0200133int sockpair_bind_receiver(struct receiver *rx, char **errmsg)
Willy Tarreau62292b22020-09-02 17:52:23 +0200134{
135 int err;
136
137 /* ensure we never return garbage */
138 if (errmsg)
139 *errmsg = 0;
140
141 err = ERR_NONE;
142
143 if (rx->flags & RX_F_BOUND)
144 return ERR_NONE;
145
146 if (rx->fd == -1) {
147 err |= ERR_FATAL | ERR_ALERT;
148 memprintf(errmsg, "sockpair may be only used with inherited FDs");
149 goto bind_return;
150 }
151
152 if (rx->fd >= global.maxsock) {
153 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
154 memprintf(errmsg, "not enough free sockets (raise '-n' parameter)");
155 goto bind_close_return;
156 }
157
158 if (fcntl(rx->fd, F_SETFL, O_NONBLOCK) == -1) {
159 err |= ERR_FATAL | ERR_ALERT;
160 memprintf(errmsg, "cannot make socket non-blocking");
161 goto bind_close_return;
162 }
163
164 rx->flags |= RX_F_BOUND;
165
Willy Tarreau233ad282020-10-15 21:45:15 +0200166 fd_insert(rx->fd, rx->owner, rx->iocb, thread_mask(rx->settings->bind_thread) & all_threads_mask);
Willy Tarreau62292b22020-09-02 17:52:23 +0200167 return err;
168
169 bind_return:
170 if (errmsg && *errmsg)
171 memprintf(errmsg, "%s [fd %d]", *errmsg, rx->fd);
172
173 return err;
174
175 bind_close_return:
176 close(rx->fd);
177 goto bind_return;
178}
179
William Lallemand2fe7dd02018-09-11 16:51:29 +0200180/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
181 * enabled for polling. The return value is composed from ERR_NONE,
182 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
183 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
184 * Note that <errmsg> may be NULL if <errlen> is also zero.
185 */
186static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
187{
William Lallemand2fe7dd02018-09-11 16:51:29 +0200188 int err;
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200189 char *msg = NULL;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200190
191 err = ERR_NONE;
192
193 /* ensure we never return garbage */
194 if (errlen)
195 *errmsg = 0;
196
197 if (listener->state != LI_ASSIGNED)
198 return ERR_NONE; /* already bound */
199
Willy Tarreauad33acf2020-09-02 18:40:02 +0200200 if (!(listener->rx.flags & RX_F_BOUND)) {
201 msg = "receiving socket not bound";
202 goto err_return;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200203 }
Willy Tarreauad33acf2020-09-02 18:40:02 +0200204
Willy Tarreaua37b2442020-09-24 07:23:45 +0200205 listener_set_state(listener, LI_LISTEN);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200206 return err;
207
208 err_return:
209 if (msg && errlen)
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200210 snprintf(errmsg, errlen, "%s [fd %d]", msg, listener->rx.fd);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200211 return err;
212}
213
214/*
215 * Send FD over a unix socket
216 *
217 * <send_fd> is the FD to send
218 * <fd> is the fd of the unix socket to use for the transfer
219 *
220 * The iobuf variable could be use in the future to enhance the protocol.
221 */
222int send_fd_uxst(int fd, int send_fd)
223{
224 char iobuf[2];
225 struct iovec iov;
226 struct msghdr msghdr;
227
228 char cmsgbuf[CMSG_SPACE(sizeof(int))];
229 char buf[CMSG_SPACE(sizeof(int))];
230 struct cmsghdr *cmsg = (void *)buf;
231
232 int *fdptr;
233
234 iov.iov_base = iobuf;
235 iov.iov_len = sizeof(iobuf);
236
237 memset(&msghdr, 0, sizeof(msghdr));
238 msghdr.msg_iov = &iov;
239 msghdr.msg_iovlen = 1;
240
241 /* Now send the fds */
242 msghdr.msg_control = cmsgbuf;
243 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
244
245 cmsg = CMSG_FIRSTHDR(&msghdr);
246 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
247 cmsg->cmsg_level = SOL_SOCKET;
248 cmsg->cmsg_type = SCM_RIGHTS;
249
250 fdptr = (int *)CMSG_DATA(cmsg);
251 memcpy(fdptr, &send_fd, sizeof(send_fd));
252
253 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
254 ha_warning("Failed to transfer socket\n");
255 return 1;
256 }
257
258 return 0;
259}
260
261/*
262 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800263 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200264 * socket and establishing a connection, it creates a pair of connected
265 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200266 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200267 *
268 * conn->target may point either to a valid server or to a backend, depending
269 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
270 * <data> parameter is a boolean indicating whether there are data waiting for
271 * being sent or not, in order to adjust data write polling and on some
272 * platforms. The <delack> argument is ignored.
273 *
274 * Note that a pending send_proxy message accounts for data.
275 *
276 * It can return one of :
277 * - SF_ERR_NONE if everything's OK
278 * - SF_ERR_SRVTO if there are no more servers
279 * - SF_ERR_SRVCL if the connection was refused by the server
280 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
281 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
282 * - SF_ERR_INTERNAL for any other purely internal errors
283 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
284 *
285 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
286 * it's invalid and the caller has nothing to do.
287 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200288static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200289{
290 int sv[2], fd, dst_fd = -1;
291
292 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200293 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200294
William Lallemand2fe7dd02018-09-11 16:51:29 +0200295 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
296 obj_type(conn->target) != OBJ_TYPE_SERVER) {
297 conn->flags |= CO_FL_ERROR;
298 return SF_ERR_INTERNAL;
299 }
300
301 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
302 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
303 conn->flags |= CO_FL_ERROR;
304 return SF_ERR_RESOURCE;
305 }
306
307 fd = conn->handle.fd = sv[1];
308
309 if (fd >= global.maxsock) {
310 /* do not log anything there, it's a normal condition when this option
311 * is used to serialize connections to a server !
312 */
313 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
314 close(sv[0]);
315 close(sv[1]);
316 conn->err_code = CO_ER_CONF_FDLIM;
317 conn->flags |= CO_FL_ERROR;
318 return SF_ERR_PRXCOND; /* it is a configuration limit */
319 }
320
321 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
322 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
323 close(sv[0]);
324 close(sv[1]);
325 conn->err_code = CO_ER_SOCK_ERR;
326 conn->flags |= CO_FL_ERROR;
327 return SF_ERR_INTERNAL;
328 }
329
William Lallemandc03eb012018-11-27 12:02:37 +0100330 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
331 ha_alert("Cannot set CLOEXEC on client socket.\n");
332 close(sv[0]);
333 close(sv[1]);
334 conn->err_code = CO_ER_SOCK_ERR;
335 conn->flags |= CO_FL_ERROR;
336 return SF_ERR_INTERNAL;
337 }
338
William Lallemand2fe7dd02018-09-11 16:51:29 +0200339 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200340 if (conn->send_proxy_ofs)
341 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200342
343 if (global.tune.server_sndbuf)
344 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
345
346 if (global.tune.server_rcvbuf)
347 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
348
349 /* The new socket is sent on the other side, it should be retrieved and
350 * considered as an 'accept' socket on the server side */
351 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
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
359 close(sv[0]); /* we don't need this side anymore */
360
361 conn->flags &= ~CO_FL_WAIT_L4_CONN;
362
363 conn->flags |= CO_FL_ADDR_TO_SET;
364
365 /* Prepare to send a few handshakes related to the on-wire protocol. */
366 if (conn->send_proxy_ofs)
367 conn->flags |= CO_FL_SEND_PROXY;
368
369 conn_ctrl_init(conn); /* registers the FD */
370 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
371
372 if (conn_xprt_init(conn) < 0) {
373 conn_full_close(conn);
374 conn->flags |= CO_FL_ERROR;
375 return SF_ERR_RESOURCE;
376 }
377
William Lallemand2fe7dd02018-09-11 16:51:29 +0200378 return SF_ERR_NONE; /* connection is OK */
379}
380
381
382/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800383 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200384 *
385 * Return -1 or a socket fd;
386 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800387 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200388 */
389int recv_fd_uxst(int sock)
390{
391 struct msghdr msghdr;
392 struct iovec iov;
393 char iobuf[2];
394
395 char cmsgbuf[CMSG_SPACE(sizeof(int))];
396 char buf[CMSG_SPACE(sizeof(int))];
397 struct cmsghdr *cmsg = (void *)buf;
398
399
400 int recv_fd = -1;
401 int ret = -1;
402
403 memset(&msghdr, 0, sizeof(msghdr));
404
405 iov.iov_base = iobuf;
406 iov.iov_len = sizeof(iobuf);
407
408 msghdr.msg_iov = &iov;
409 msghdr.msg_iovlen = 1;
410
411 msghdr.msg_control = cmsgbuf;
412 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
413
414 iov.iov_len = sizeof(iobuf);
415 iov.iov_base = iobuf;
416
417 while (1) {
418 ret = recvmsg(sock, &msghdr, 0);
419 if (ret == -1 && errno == EINTR)
420 continue;
421 else
422 break;
423 }
424
425 if (ret == -1)
426 return ret;
427
428 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200429 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200430 cmsg->cmsg_type == SCM_RIGHTS) {
431 size_t totlen = cmsg->cmsg_len -
432 CMSG_LEN(0);
433 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
434 }
435 return recv_fd;
436}
437
Willy Tarreaucc8b6532020-10-13 17:27:34 +0200438/* Tests if the receiver supports accepting connections. Returns positive on
439 * success, 0 if not possible, negative if the socket is non-recoverable. In
440 * practice zero is never returned since we don't support suspending sockets.
441 * The real test consists in verifying we have a connected SOCK_STREAM of
442 * family AF_UNIX.
443 */
Willy Tarreau7d053e42020-10-15 09:19:43 +0200444static int sockpair_accepting_conn(const struct receiver *rx)
Willy Tarreaucc8b6532020-10-13 17:27:34 +0200445{
446 struct sockaddr sa;
447 socklen_t len;
448 int val;
449
450 len = sizeof(val);
451 if (getsockopt(rx->fd, SOL_SOCKET, SO_TYPE, &val, &len) == -1)
452 return -1;
453
454 if (val != SOCK_STREAM)
455 return -1;
456
457 len = sizeof(sa);
458 if (getsockname(rx->fd, &sa, &len) != 0)
459 return -1;
460
461 if (sa.sa_family != AF_UNIX)
462 return -1;
463
464 len = sizeof(val);
465 if (getsockopt(rx->fd, SOL_SOCKET, SO_ACCEPTCONN, &val, &len) == -1)
466 return -1;
467
468 /* Note: cannot be a listening socket, must be established */
469 if (val)
470 return -1;
471
472 return 1;
473}
474
Willy Tarreau344b8fc2020-10-15 09:43:31 +0200475/* Accept an incoming connection from listener <l>, and return it, as well as
476 * a CO_AC_* status code into <status> if not null. Null is returned on error.
477 * <l> must be a valid listener with a valid frontend.
478 */
479struct connection *sockpair_accept_conn(struct listener *l, int *status)
480{
481 struct proxy *p = l->bind_conf->frontend;
482 struct connection *conn = NULL;
483 int ret;
484 int cfd;
485
486 if ((cfd = recv_fd_uxst(l->rx.fd)) != -1)
487 fcntl(cfd, F_SETFL, O_NONBLOCK);
488
489 if (likely(cfd != -1)) {
490 /* Perfect, the connection was accepted */
491 conn = conn_new(&l->obj_type);
492 if (!conn)
493 goto fail_conn;
494
495 if (!sockaddr_alloc(&conn->src, NULL, 0))
496 goto fail_addr;
497
498 /* just like with UNIX sockets, only the family is filled */
499 conn->src->ss_family = AF_UNIX;
500 conn->handle.fd = cfd;
501 conn->flags |= CO_FL_ADDR_FROM_SET;
502 ret = CO_AC_DONE;
503 goto done;
504 }
505
506 switch (errno) {
507 case EAGAIN:
508 ret = CO_AC_DONE; /* nothing more to accept */
509 if (fdtab[l->rx.fd].ev & (FD_POLL_HUP|FD_POLL_ERR)) {
510 /* the listening socket might have been disabled in a shared
511 * process and we're a collateral victim. We'll just pause for
512 * a while in case it comes back. In the mean time, we need to
513 * clear this sticky flag.
514 */
515 _HA_ATOMIC_AND(&fdtab[l->rx.fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
516 ret = CO_AC_PAUSE;
517 }
518 fd_cant_recv(l->rx.fd);
519 break;
520
521 case EINVAL:
522 /* might be trying to accept on a shut fd (eg: soft stop) */
523 ret = CO_AC_PAUSE;
524 break;
525
526 case EINTR:
527 case ECONNABORTED:
528 ret = CO_AC_RETRY;
529 break;
530
531 case ENFILE:
532 if (p)
533 send_log(p, LOG_EMERG,
534 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
535 p->id, global.maxsock);
536 ret = CO_AC_PAUSE;
537 break;
538
539 case EMFILE:
540 if (p)
541 send_log(p, LOG_EMERG,
542 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
543 p->id, global.maxsock);
544 ret = CO_AC_PAUSE;
545 break;
546
547 case ENOBUFS:
548 case ENOMEM:
549 if (p)
550 send_log(p, LOG_EMERG,
551 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
552 p->id, global.maxsock);
553 ret = CO_AC_PAUSE;
554 break;
555
556 default:
557 /* unexpected result, let's give up and let other tasks run */
558 ret = CO_AC_YIELD;
559 }
560 done:
561 if (status)
562 *status = ret;
563 return conn;
564
565 fail_addr:
566 conn_free(conn);
567 conn = NULL;
568 fail_conn:
569 ret = CO_AC_PAUSE;
570 goto done;
571}
572
William Lallemand2fe7dd02018-09-11 16:51:29 +0200573/*
574 * Local variables:
575 * c-indent-level: 8
576 * c-basic-offset: 8
577 * End:
578 */