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