blob: 8e174601982c82b0415ddc6a8ce664879ac610ff [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
110 * still be valid. Does nothing in early boot (needs fd_updt).
111 */
112static void sockpair_enable_listener(struct listener *l)
113{
114 if (fd_updt)
115 fd_want_recv(l->rx.fd);
116}
117
118/* Disable receipt of incoming connections for listener <l>. The receiver must
119 * still be valid. Does nothing in early boot (needs fd_updt).
120 */
121static void sockpair_disable_listener(struct listener *l)
122{
123 if (fd_updt)
124 fd_stop_recv(l->rx.fd);
125}
126
Willy Tarreau62292b22020-09-02 17:52:23 +0200127/* Binds receiver <rx>, and assigns <handler> and rx->owner as the callback and
128 * context, respectively, with <tm> as the thread mask. Returns and error code
129 * made of ERR_* bits on failure or ERR_NONE on success. On failure, an error
130 * message may be passed into <errmsg>. Note that the binding address is only
131 * an FD to receive the incoming FDs on. Thus by definition there is no real
132 * "bind" operation, this only completes the receiver. Such FDs are not
133 * inherited upon reload.
134 */
135int sockpair_bind_receiver(struct receiver *rx, void (*handler)(int fd), char **errmsg)
136{
137 int err;
138
139 /* ensure we never return garbage */
140 if (errmsg)
141 *errmsg = 0;
142
143 err = ERR_NONE;
144
145 if (rx->flags & RX_F_BOUND)
146 return ERR_NONE;
147
148 if (rx->fd == -1) {
149 err |= ERR_FATAL | ERR_ALERT;
150 memprintf(errmsg, "sockpair may be only used with inherited FDs");
151 goto bind_return;
152 }
153
154 if (rx->fd >= global.maxsock) {
155 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
156 memprintf(errmsg, "not enough free sockets (raise '-n' parameter)");
157 goto bind_close_return;
158 }
159
160 if (fcntl(rx->fd, F_SETFL, O_NONBLOCK) == -1) {
161 err |= ERR_FATAL | ERR_ALERT;
162 memprintf(errmsg, "cannot make socket non-blocking");
163 goto bind_close_return;
164 }
165
166 rx->flags |= RX_F_BOUND;
167
168 fd_insert(rx->fd, rx->owner, handler, thread_mask(rx->settings->bind_thread) & all_threads_mask);
169 return err;
170
171 bind_return:
172 if (errmsg && *errmsg)
173 memprintf(errmsg, "%s [fd %d]", *errmsg, rx->fd);
174
175 return err;
176
177 bind_close_return:
178 close(rx->fd);
179 goto bind_return;
180}
181
William Lallemand2fe7dd02018-09-11 16:51:29 +0200182/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
183 * enabled for polling. The return value is composed from ERR_NONE,
184 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
185 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
186 * Note that <errmsg> may be NULL if <errlen> is also zero.
187 */
188static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
189{
William Lallemand2fe7dd02018-09-11 16:51:29 +0200190 int err;
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200191 char *msg = NULL;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200192
193 err = ERR_NONE;
194
195 /* ensure we never return garbage */
196 if (errlen)
197 *errmsg = 0;
198
199 if (listener->state != LI_ASSIGNED)
200 return ERR_NONE; /* already bound */
201
Willy Tarreauad33acf2020-09-02 18:40:02 +0200202 if (!(listener->rx.flags & RX_F_BOUND)) {
203 msg = "receiving socket not bound";
204 goto err_return;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200205 }
Willy Tarreauad33acf2020-09-02 18:40:02 +0200206
Willy Tarreaua37b2442020-09-24 07:23:45 +0200207 listener_set_state(listener, LI_LISTEN);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200208 return err;
209
210 err_return:
211 if (msg && errlen)
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200212 snprintf(errmsg, errlen, "%s [fd %d]", msg, listener->rx.fd);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200213 return err;
214}
215
216/*
217 * Send FD over a unix socket
218 *
219 * <send_fd> is the FD to send
220 * <fd> is the fd of the unix socket to use for the transfer
221 *
222 * The iobuf variable could be use in the future to enhance the protocol.
223 */
224int send_fd_uxst(int fd, int send_fd)
225{
226 char iobuf[2];
227 struct iovec iov;
228 struct msghdr msghdr;
229
230 char cmsgbuf[CMSG_SPACE(sizeof(int))];
231 char buf[CMSG_SPACE(sizeof(int))];
232 struct cmsghdr *cmsg = (void *)buf;
233
234 int *fdptr;
235
236 iov.iov_base = iobuf;
237 iov.iov_len = sizeof(iobuf);
238
239 memset(&msghdr, 0, sizeof(msghdr));
240 msghdr.msg_iov = &iov;
241 msghdr.msg_iovlen = 1;
242
243 /* Now send the fds */
244 msghdr.msg_control = cmsgbuf;
245 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
246
247 cmsg = CMSG_FIRSTHDR(&msghdr);
248 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
249 cmsg->cmsg_level = SOL_SOCKET;
250 cmsg->cmsg_type = SCM_RIGHTS;
251
252 fdptr = (int *)CMSG_DATA(cmsg);
253 memcpy(fdptr, &send_fd, sizeof(send_fd));
254
255 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
256 ha_warning("Failed to transfer socket\n");
257 return 1;
258 }
259
260 return 0;
261}
262
263/*
264 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800265 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200266 * socket and establishing a connection, it creates a pair of connected
267 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200268 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200269 *
270 * conn->target may point either to a valid server or to a backend, depending
271 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
272 * <data> parameter is a boolean indicating whether there are data waiting for
273 * being sent or not, in order to adjust data write polling and on some
274 * platforms. The <delack> argument is ignored.
275 *
276 * Note that a pending send_proxy message accounts for data.
277 *
278 * It can return one of :
279 * - SF_ERR_NONE if everything's OK
280 * - SF_ERR_SRVTO if there are no more servers
281 * - SF_ERR_SRVCL if the connection was refused by the server
282 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
283 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
284 * - SF_ERR_INTERNAL for any other purely internal errors
285 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
286 *
287 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
288 * it's invalid and the caller has nothing to do.
289 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200290static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200291{
292 int sv[2], fd, dst_fd = -1;
293
294 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200295 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200296
William Lallemand2fe7dd02018-09-11 16:51:29 +0200297 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
298 obj_type(conn->target) != OBJ_TYPE_SERVER) {
299 conn->flags |= CO_FL_ERROR;
300 return SF_ERR_INTERNAL;
301 }
302
303 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
304 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
305 conn->flags |= CO_FL_ERROR;
306 return SF_ERR_RESOURCE;
307 }
308
309 fd = conn->handle.fd = sv[1];
310
311 if (fd >= global.maxsock) {
312 /* do not log anything there, it's a normal condition when this option
313 * is used to serialize connections to a server !
314 */
315 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
316 close(sv[0]);
317 close(sv[1]);
318 conn->err_code = CO_ER_CONF_FDLIM;
319 conn->flags |= CO_FL_ERROR;
320 return SF_ERR_PRXCOND; /* it is a configuration limit */
321 }
322
323 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
324 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
325 close(sv[0]);
326 close(sv[1]);
327 conn->err_code = CO_ER_SOCK_ERR;
328 conn->flags |= CO_FL_ERROR;
329 return SF_ERR_INTERNAL;
330 }
331
William Lallemandc03eb012018-11-27 12:02:37 +0100332 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
333 ha_alert("Cannot set CLOEXEC on client socket.\n");
334 close(sv[0]);
335 close(sv[1]);
336 conn->err_code = CO_ER_SOCK_ERR;
337 conn->flags |= CO_FL_ERROR;
338 return SF_ERR_INTERNAL;
339 }
340
William Lallemand2fe7dd02018-09-11 16:51:29 +0200341 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200342 if (conn->send_proxy_ofs)
343 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200344
345 if (global.tune.server_sndbuf)
346 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
347
348 if (global.tune.server_rcvbuf)
349 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
350
351 /* The new socket is sent on the other side, it should be retrieved and
352 * considered as an 'accept' socket on the server side */
353 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
354 close(sv[0]);
355 close(sv[1]);
356 conn->err_code = CO_ER_SOCK_ERR;
357 conn->flags |= CO_FL_ERROR;
358 return SF_ERR_INTERNAL;
359 }
360
361 close(sv[0]); /* we don't need this side anymore */
362
363 conn->flags &= ~CO_FL_WAIT_L4_CONN;
364
365 conn->flags |= CO_FL_ADDR_TO_SET;
366
367 /* Prepare to send a few handshakes related to the on-wire protocol. */
368 if (conn->send_proxy_ofs)
369 conn->flags |= CO_FL_SEND_PROXY;
370
371 conn_ctrl_init(conn); /* registers the FD */
372 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
373
374 if (conn_xprt_init(conn) < 0) {
375 conn_full_close(conn);
376 conn->flags |= CO_FL_ERROR;
377 return SF_ERR_RESOURCE;
378 }
379
William Lallemand2fe7dd02018-09-11 16:51:29 +0200380 return SF_ERR_NONE; /* connection is OK */
381}
382
383
384/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800385 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200386 *
387 * Return -1 or a socket fd;
388 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800389 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200390 */
391int recv_fd_uxst(int sock)
392{
393 struct msghdr msghdr;
394 struct iovec iov;
395 char iobuf[2];
396
397 char cmsgbuf[CMSG_SPACE(sizeof(int))];
398 char buf[CMSG_SPACE(sizeof(int))];
399 struct cmsghdr *cmsg = (void *)buf;
400
401
402 int recv_fd = -1;
403 int ret = -1;
404
405 memset(&msghdr, 0, sizeof(msghdr));
406
407 iov.iov_base = iobuf;
408 iov.iov_len = sizeof(iobuf);
409
410 msghdr.msg_iov = &iov;
411 msghdr.msg_iovlen = 1;
412
413 msghdr.msg_control = cmsgbuf;
414 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
415
416 iov.iov_len = sizeof(iobuf);
417 iov.iov_base = iobuf;
418
419 while (1) {
420 ret = recvmsg(sock, &msghdr, 0);
421 if (ret == -1 && errno == EINTR)
422 continue;
423 else
424 break;
425 }
426
427 if (ret == -1)
428 return ret;
429
430 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200431 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200432 cmsg->cmsg_type == SCM_RIGHTS) {
433 size_t totlen = cmsg->cmsg_len -
434 CMSG_LEN(0);
435 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
436 }
437 return recv_fd;
438}
439
Willy Tarreaucc8b6532020-10-13 17:27:34 +0200440/* Tests if the receiver supports accepting connections. Returns positive on
441 * success, 0 if not possible, negative if the socket is non-recoverable. In
442 * practice zero is never returned since we don't support suspending sockets.
443 * The real test consists in verifying we have a connected SOCK_STREAM of
444 * family AF_UNIX.
445 */
Willy Tarreau7d053e42020-10-15 09:19:43 +0200446static int sockpair_accepting_conn(const struct receiver *rx)
Willy Tarreaucc8b6532020-10-13 17:27:34 +0200447{
448 struct sockaddr sa;
449 socklen_t len;
450 int val;
451
452 len = sizeof(val);
453 if (getsockopt(rx->fd, SOL_SOCKET, SO_TYPE, &val, &len) == -1)
454 return -1;
455
456 if (val != SOCK_STREAM)
457 return -1;
458
459 len = sizeof(sa);
460 if (getsockname(rx->fd, &sa, &len) != 0)
461 return -1;
462
463 if (sa.sa_family != AF_UNIX)
464 return -1;
465
466 len = sizeof(val);
467 if (getsockopt(rx->fd, SOL_SOCKET, SO_ACCEPTCONN, &val, &len) == -1)
468 return -1;
469
470 /* Note: cannot be a listening socket, must be established */
471 if (val)
472 return -1;
473
474 return 1;
475}
476
Willy Tarreau344b8fc2020-10-15 09:43:31 +0200477/* Accept an incoming connection from listener <l>, and return it, as well as
478 * a CO_AC_* status code into <status> if not null. Null is returned on error.
479 * <l> must be a valid listener with a valid frontend.
480 */
481struct connection *sockpair_accept_conn(struct listener *l, int *status)
482{
483 struct proxy *p = l->bind_conf->frontend;
484 struct connection *conn = NULL;
485 int ret;
486 int cfd;
487
488 if ((cfd = recv_fd_uxst(l->rx.fd)) != -1)
489 fcntl(cfd, F_SETFL, O_NONBLOCK);
490
491 if (likely(cfd != -1)) {
492 /* Perfect, the connection was accepted */
493 conn = conn_new(&l->obj_type);
494 if (!conn)
495 goto fail_conn;
496
497 if (!sockaddr_alloc(&conn->src, NULL, 0))
498 goto fail_addr;
499
500 /* just like with UNIX sockets, only the family is filled */
501 conn->src->ss_family = AF_UNIX;
502 conn->handle.fd = cfd;
503 conn->flags |= CO_FL_ADDR_FROM_SET;
504 ret = CO_AC_DONE;
505 goto done;
506 }
507
508 switch (errno) {
509 case EAGAIN:
510 ret = CO_AC_DONE; /* nothing more to accept */
511 if (fdtab[l->rx.fd].ev & (FD_POLL_HUP|FD_POLL_ERR)) {
512 /* the listening socket might have been disabled in a shared
513 * process and we're a collateral victim. We'll just pause for
514 * a while in case it comes back. In the mean time, we need to
515 * clear this sticky flag.
516 */
517 _HA_ATOMIC_AND(&fdtab[l->rx.fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
518 ret = CO_AC_PAUSE;
519 }
520 fd_cant_recv(l->rx.fd);
521 break;
522
523 case EINVAL:
524 /* might be trying to accept on a shut fd (eg: soft stop) */
525 ret = CO_AC_PAUSE;
526 break;
527
528 case EINTR:
529 case ECONNABORTED:
530 ret = CO_AC_RETRY;
531 break;
532
533 case ENFILE:
534 if (p)
535 send_log(p, LOG_EMERG,
536 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
537 p->id, global.maxsock);
538 ret = CO_AC_PAUSE;
539 break;
540
541 case EMFILE:
542 if (p)
543 send_log(p, LOG_EMERG,
544 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
545 p->id, global.maxsock);
546 ret = CO_AC_PAUSE;
547 break;
548
549 case ENOBUFS:
550 case ENOMEM:
551 if (p)
552 send_log(p, LOG_EMERG,
553 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
554 p->id, global.maxsock);
555 ret = CO_AC_PAUSE;
556 break;
557
558 default:
559 /* unexpected result, let's give up and let other tasks run */
560 ret = CO_AC_YIELD;
561 }
562 done:
563 if (status)
564 *status = ret;
565 return conn;
566
567 fail_addr:
568 conn_free(conn);
569 conn = NULL;
570 fail_conn:
571 ret = CO_AC_PAUSE;
572 goto done;
573}
574
William Lallemand2fe7dd02018-09-11 16:51:29 +0200575/*
576 * Local variables:
577 * c-indent-level: 8
578 * c-basic-offset: 8
579 * End:
580 */