blob: fe74ff469d9517009cc76f4c2ff3481c93c2e46c [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
William Lallemand2fe7dd02018-09-11 16:51:29 +020046static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020047static void sockpair_enable_listener(struct listener *listener);
48static void sockpair_disable_listener(struct listener *listener);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020049static int sockpair_connect_server(struct connection *conn, int flags);
Willy Tarreau7d053e42020-10-15 09:19:43 +020050static int sockpair_accepting_conn(const struct receiver *rx);
Willy Tarreau344b8fc2020-10-15 09:43:31 +020051struct connection *sockpair_accept_conn(struct listener *l, int *status);
William Lallemand2fe7dd02018-09-11 16:51:29 +020052
Willy Tarreaub0254cb2020-09-04 08:07:11 +020053struct proto_fam proto_fam_sockpair = {
54 .name = "sockpair",
55 .sock_domain = AF_CUST_SOCKPAIR,
56 .sock_family = AF_UNIX,
57 .sock_addrlen = sizeof(struct sockaddr_un),
58 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),
59 .addrcmp = NULL,
60 .bind = sockpair_bind_receiver,
61 .get_src = NULL,
62 .get_dst = NULL,
63};
64
William Lallemand2fe7dd02018-09-11 16:51:29 +020065/* Note: must not be declared <const> as its list will be overwritten */
66static struct protocol proto_sockpair = {
67 .name = "sockpair",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020068 .fam = &proto_fam_sockpair,
Willy Tarreaua54553f2020-09-16 17:50:45 +020069 .ctrl_type = SOCK_STREAM,
William Lallemand2fe7dd02018-09-11 16:51:29 +020070 .sock_domain = AF_CUST_SOCKPAIR,
71 .sock_type = SOCK_STREAM,
72 .sock_prot = 0,
Willy Tarreauefcf1382020-12-04 15:03:36 +010073 .add = default_add_listener,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020074 .listen = sockpair_bind_listener,
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020075 .enable = sockpair_enable_listener,
76 .disable = sockpair_disable_listener,
Willy Tarreau7b2febd2020-10-09 17:18:29 +020077 .unbind = default_unbind_listener,
Willy Tarreau344b8fc2020-10-15 09:43:31 +020078 .accept_conn = sockpair_accept_conn,
Willy Tarreauf58b8db2020-10-09 16:32:08 +020079 .rx_unbind = sock_unbind,
Willy Tarreau686fa3d2020-09-25 19:09:53 +020080 .rx_enable = sock_enable,
81 .rx_disable = sock_disable,
Willy Tarreau7d053e42020-10-15 09:19:43 +020082 .rx_listening = sockpair_accepting_conn,
Willy Tarreaua74cb382020-10-15 21:29:49 +020083 .default_iocb = &sock_accept_iocb,
William Lallemand2fe7dd02018-09-11 16:51:29 +020084 .connect = &sockpair_connect_server,
Willy Tarreaud7f331c2020-09-25 17:01:43 +020085 .receivers = LIST_HEAD_INIT(proto_sockpair.receivers),
86 .nb_receivers = 0,
William Lallemand2fe7dd02018-09-11 16:51:29 +020087};
88
Willy Tarreau0108d902018-11-25 19:14:37 +010089INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
90
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020091/* Enable receipt of incoming connections for listener <l>. The receiver must
Willy Tarreaua4380b22020-11-04 13:59:04 +010092 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020093 */
94static void sockpair_enable_listener(struct listener *l)
95{
Willy Tarreaua4380b22020-11-04 13:59:04 +010096 fd_want_recv_safe(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020097}
98
99/* Disable receipt of incoming connections for listener <l>. The receiver must
Willy Tarreaua4380b22020-11-04 13:59:04 +0100100 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200101 */
102static void sockpair_disable_listener(struct listener *l)
103{
Willy Tarreaua4380b22020-11-04 13:59:04 +0100104 fd_stop_recv(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200105}
106
Willy Tarreau233ad282020-10-15 21:45:15 +0200107/* Binds receiver <rx>, and assigns rx->iocb and rx->owner as the callback
108 * and context, respectively, with ->bind_thread as the thread mask. Returns an
109 * error code made of ERR_* bits on failure or ERR_NONE on success. On failure,
110 * an error message may be passed into <errmsg>. Note that the binding address
111 * is only an FD to receive the incoming FDs on. Thus by definition there is no
112 * real "bind" operation, this only completes the receiver. Such FDs are not
Willy Tarreau62292b22020-09-02 17:52:23 +0200113 * inherited upon reload.
114 */
Willy Tarreau233ad282020-10-15 21:45:15 +0200115int sockpair_bind_receiver(struct receiver *rx, char **errmsg)
Willy Tarreau62292b22020-09-02 17:52:23 +0200116{
117 int err;
118
119 /* ensure we never return garbage */
120 if (errmsg)
121 *errmsg = 0;
122
123 err = ERR_NONE;
124
125 if (rx->flags & RX_F_BOUND)
126 return ERR_NONE;
127
128 if (rx->fd == -1) {
129 err |= ERR_FATAL | ERR_ALERT;
130 memprintf(errmsg, "sockpair may be only used with inherited FDs");
131 goto bind_return;
132 }
133
134 if (rx->fd >= global.maxsock) {
135 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
136 memprintf(errmsg, "not enough free sockets (raise '-n' parameter)");
137 goto bind_close_return;
138 }
139
140 if (fcntl(rx->fd, F_SETFL, O_NONBLOCK) == -1) {
141 err |= ERR_FATAL | ERR_ALERT;
142 memprintf(errmsg, "cannot make socket non-blocking");
143 goto bind_close_return;
144 }
145
146 rx->flags |= RX_F_BOUND;
147
Willy Tarreau233ad282020-10-15 21:45:15 +0200148 fd_insert(rx->fd, rx->owner, rx->iocb, thread_mask(rx->settings->bind_thread) & all_threads_mask);
Willy Tarreau62292b22020-09-02 17:52:23 +0200149 return err;
150
151 bind_return:
152 if (errmsg && *errmsg)
153 memprintf(errmsg, "%s [fd %d]", *errmsg, rx->fd);
154
155 return err;
156
157 bind_close_return:
158 close(rx->fd);
159 goto bind_return;
160}
161
William Lallemand2fe7dd02018-09-11 16:51:29 +0200162/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
163 * enabled for polling. The return value is composed from ERR_NONE,
164 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
165 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
166 * Note that <errmsg> may be NULL if <errlen> is also zero.
167 */
168static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
169{
William Lallemand2fe7dd02018-09-11 16:51:29 +0200170 int err;
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200171 char *msg = NULL;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200172
173 err = ERR_NONE;
174
175 /* ensure we never return garbage */
176 if (errlen)
177 *errmsg = 0;
178
179 if (listener->state != LI_ASSIGNED)
180 return ERR_NONE; /* already bound */
181
Willy Tarreauad33acf2020-09-02 18:40:02 +0200182 if (!(listener->rx.flags & RX_F_BOUND)) {
183 msg = "receiving socket not bound";
184 goto err_return;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200185 }
Willy Tarreauad33acf2020-09-02 18:40:02 +0200186
Willy Tarreaua37b2442020-09-24 07:23:45 +0200187 listener_set_state(listener, LI_LISTEN);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200188 return err;
189
190 err_return:
191 if (msg && errlen)
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200192 snprintf(errmsg, errlen, "%s [fd %d]", msg, listener->rx.fd);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200193 return err;
194}
195
196/*
197 * Send FD over a unix socket
198 *
199 * <send_fd> is the FD to send
200 * <fd> is the fd of the unix socket to use for the transfer
201 *
202 * The iobuf variable could be use in the future to enhance the protocol.
203 */
204int send_fd_uxst(int fd, int send_fd)
205{
206 char iobuf[2];
207 struct iovec iov;
208 struct msghdr msghdr;
209
210 char cmsgbuf[CMSG_SPACE(sizeof(int))];
211 char buf[CMSG_SPACE(sizeof(int))];
212 struct cmsghdr *cmsg = (void *)buf;
213
214 int *fdptr;
215
216 iov.iov_base = iobuf;
217 iov.iov_len = sizeof(iobuf);
218
219 memset(&msghdr, 0, sizeof(msghdr));
220 msghdr.msg_iov = &iov;
221 msghdr.msg_iovlen = 1;
222
223 /* Now send the fds */
224 msghdr.msg_control = cmsgbuf;
225 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
226
227 cmsg = CMSG_FIRSTHDR(&msghdr);
228 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
229 cmsg->cmsg_level = SOL_SOCKET;
230 cmsg->cmsg_type = SCM_RIGHTS;
231
232 fdptr = (int *)CMSG_DATA(cmsg);
233 memcpy(fdptr, &send_fd, sizeof(send_fd));
234
235 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
236 ha_warning("Failed to transfer socket\n");
237 return 1;
238 }
239
240 return 0;
241}
242
243/*
244 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800245 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200246 * socket and establishing a connection, it creates a pair of connected
247 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200248 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200249 *
250 * conn->target may point either to a valid server or to a backend, depending
251 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
252 * <data> parameter is a boolean indicating whether there are data waiting for
253 * being sent or not, in order to adjust data write polling and on some
254 * platforms. The <delack> argument is ignored.
255 *
256 * Note that a pending send_proxy message accounts for data.
257 *
258 * It can return one of :
259 * - SF_ERR_NONE if everything's OK
260 * - SF_ERR_SRVTO if there are no more servers
261 * - SF_ERR_SRVCL if the connection was refused by the server
262 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
263 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
264 * - SF_ERR_INTERNAL for any other purely internal errors
265 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
266 *
267 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
268 * it's invalid and the caller has nothing to do.
269 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200270static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200271{
272 int sv[2], fd, dst_fd = -1;
273
274 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200275 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200276
William Lallemand2fe7dd02018-09-11 16:51:29 +0200277 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
278 obj_type(conn->target) != OBJ_TYPE_SERVER) {
279 conn->flags |= CO_FL_ERROR;
280 return SF_ERR_INTERNAL;
281 }
282
283 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
284 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
285 conn->flags |= CO_FL_ERROR;
286 return SF_ERR_RESOURCE;
287 }
288
289 fd = conn->handle.fd = sv[1];
290
291 if (fd >= global.maxsock) {
292 /* do not log anything there, it's a normal condition when this option
293 * is used to serialize connections to a server !
294 */
295 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
296 close(sv[0]);
297 close(sv[1]);
298 conn->err_code = CO_ER_CONF_FDLIM;
299 conn->flags |= CO_FL_ERROR;
300 return SF_ERR_PRXCOND; /* it is a configuration limit */
301 }
302
303 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
304 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
305 close(sv[0]);
306 close(sv[1]);
307 conn->err_code = CO_ER_SOCK_ERR;
308 conn->flags |= CO_FL_ERROR;
309 return SF_ERR_INTERNAL;
310 }
311
William Lallemandc03eb012018-11-27 12:02:37 +0100312 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
313 ha_alert("Cannot set CLOEXEC on client socket.\n");
314 close(sv[0]);
315 close(sv[1]);
316 conn->err_code = CO_ER_SOCK_ERR;
317 conn->flags |= CO_FL_ERROR;
318 return SF_ERR_INTERNAL;
319 }
320
William Lallemand2fe7dd02018-09-11 16:51:29 +0200321 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200322 if (conn->send_proxy_ofs)
323 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200324
325 if (global.tune.server_sndbuf)
326 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
327
328 if (global.tune.server_rcvbuf)
329 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
330
331 /* The new socket is sent on the other side, it should be retrieved and
332 * considered as an 'accept' socket on the server side */
333 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
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
341 close(sv[0]); /* we don't need this side anymore */
342
343 conn->flags &= ~CO_FL_WAIT_L4_CONN;
344
345 conn->flags |= CO_FL_ADDR_TO_SET;
346
347 /* Prepare to send a few handshakes related to the on-wire protocol. */
348 if (conn->send_proxy_ofs)
349 conn->flags |= CO_FL_SEND_PROXY;
350
351 conn_ctrl_init(conn); /* registers the FD */
352 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
353
354 if (conn_xprt_init(conn) < 0) {
355 conn_full_close(conn);
356 conn->flags |= CO_FL_ERROR;
357 return SF_ERR_RESOURCE;
358 }
359
William Lallemand2fe7dd02018-09-11 16:51:29 +0200360 return SF_ERR_NONE; /* connection is OK */
361}
362
363
364/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800365 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200366 *
367 * Return -1 or a socket fd;
368 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800369 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200370 */
371int recv_fd_uxst(int sock)
372{
373 struct msghdr msghdr;
374 struct iovec iov;
375 char iobuf[2];
376
377 char cmsgbuf[CMSG_SPACE(sizeof(int))];
378 char buf[CMSG_SPACE(sizeof(int))];
379 struct cmsghdr *cmsg = (void *)buf;
380
381
382 int recv_fd = -1;
383 int ret = -1;
384
385 memset(&msghdr, 0, sizeof(msghdr));
386
387 iov.iov_base = iobuf;
388 iov.iov_len = sizeof(iobuf);
389
390 msghdr.msg_iov = &iov;
391 msghdr.msg_iovlen = 1;
392
393 msghdr.msg_control = cmsgbuf;
394 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
395
396 iov.iov_len = sizeof(iobuf);
397 iov.iov_base = iobuf;
398
399 while (1) {
400 ret = recvmsg(sock, &msghdr, 0);
401 if (ret == -1 && errno == EINTR)
402 continue;
403 else
404 break;
405 }
406
407 if (ret == -1)
408 return ret;
409
410 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200411 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200412 cmsg->cmsg_type == SCM_RIGHTS) {
413 size_t totlen = cmsg->cmsg_len -
414 CMSG_LEN(0);
415 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
416 }
417 return recv_fd;
418}
419
Willy Tarreaucc8b6532020-10-13 17:27:34 +0200420/* Tests if the receiver supports accepting connections. Returns positive on
421 * success, 0 if not possible, negative if the socket is non-recoverable. In
422 * practice zero is never returned since we don't support suspending sockets.
423 * The real test consists in verifying we have a connected SOCK_STREAM of
424 * family AF_UNIX.
425 */
Willy Tarreau7d053e42020-10-15 09:19:43 +0200426static int sockpair_accepting_conn(const struct receiver *rx)
Willy Tarreaucc8b6532020-10-13 17:27:34 +0200427{
428 struct sockaddr sa;
429 socklen_t len;
430 int val;
431
432 len = sizeof(val);
433 if (getsockopt(rx->fd, SOL_SOCKET, SO_TYPE, &val, &len) == -1)
434 return -1;
435
436 if (val != SOCK_STREAM)
437 return -1;
438
439 len = sizeof(sa);
440 if (getsockname(rx->fd, &sa, &len) != 0)
441 return -1;
442
443 if (sa.sa_family != AF_UNIX)
444 return -1;
445
446 len = sizeof(val);
447 if (getsockopt(rx->fd, SOL_SOCKET, SO_ACCEPTCONN, &val, &len) == -1)
448 return -1;
449
450 /* Note: cannot be a listening socket, must be established */
451 if (val)
452 return -1;
453
454 return 1;
455}
456
Willy Tarreau344b8fc2020-10-15 09:43:31 +0200457/* Accept an incoming connection from listener <l>, and return it, as well as
458 * a CO_AC_* status code into <status> if not null. Null is returned on error.
459 * <l> must be a valid listener with a valid frontend.
460 */
461struct connection *sockpair_accept_conn(struct listener *l, int *status)
462{
463 struct proxy *p = l->bind_conf->frontend;
464 struct connection *conn = NULL;
465 int ret;
466 int cfd;
467
468 if ((cfd = recv_fd_uxst(l->rx.fd)) != -1)
469 fcntl(cfd, F_SETFL, O_NONBLOCK);
470
471 if (likely(cfd != -1)) {
472 /* Perfect, the connection was accepted */
473 conn = conn_new(&l->obj_type);
474 if (!conn)
475 goto fail_conn;
476
477 if (!sockaddr_alloc(&conn->src, NULL, 0))
478 goto fail_addr;
479
480 /* just like with UNIX sockets, only the family is filled */
481 conn->src->ss_family = AF_UNIX;
482 conn->handle.fd = cfd;
483 conn->flags |= CO_FL_ADDR_FROM_SET;
484 ret = CO_AC_DONE;
485 goto done;
486 }
487
488 switch (errno) {
489 case EAGAIN:
490 ret = CO_AC_DONE; /* nothing more to accept */
491 if (fdtab[l->rx.fd].ev & (FD_POLL_HUP|FD_POLL_ERR)) {
492 /* the listening socket might have been disabled in a shared
493 * process and we're a collateral victim. We'll just pause for
494 * a while in case it comes back. In the mean time, we need to
495 * clear this sticky flag.
496 */
497 _HA_ATOMIC_AND(&fdtab[l->rx.fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
498 ret = CO_AC_PAUSE;
499 }
500 fd_cant_recv(l->rx.fd);
501 break;
502
503 case EINVAL:
504 /* might be trying to accept on a shut fd (eg: soft stop) */
505 ret = CO_AC_PAUSE;
506 break;
507
508 case EINTR:
509 case ECONNABORTED:
510 ret = CO_AC_RETRY;
511 break;
512
513 case ENFILE:
514 if (p)
515 send_log(p, LOG_EMERG,
516 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
517 p->id, global.maxsock);
518 ret = CO_AC_PAUSE;
519 break;
520
521 case EMFILE:
522 if (p)
523 send_log(p, LOG_EMERG,
524 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
525 p->id, global.maxsock);
526 ret = CO_AC_PAUSE;
527 break;
528
529 case ENOBUFS:
530 case ENOMEM:
531 if (p)
532 send_log(p, LOG_EMERG,
533 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
534 p->id, global.maxsock);
535 ret = CO_AC_PAUSE;
536 break;
537
538 default:
539 /* unexpected result, let's give up and let other tasks run */
540 ret = CO_AC_YIELD;
541 }
542 done:
543 if (status)
544 *status = ret;
545 return conn;
546
547 fail_addr:
548 conn_free(conn);
549 conn = NULL;
550 fail_conn:
551 ret = CO_AC_PAUSE;
552 goto done;
553}
554
William Lallemand2fe7dd02018-09-11 16:51:29 +0200555/*
556 * Local variables:
557 * c-indent-level: 8
558 * c-basic-offset: 8
559 * End:
560 */