Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Generic code for native (BSD-compatible) sockets |
| 3 | * |
| 4 | * Copyright 2000-2020 Willy Tarreau <w@1wt.eu> |
| 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 | |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 13 | #define _GNU_SOURCE |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 14 | #include <ctype.h> |
| 15 | #include <errno.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <time.h> |
| 21 | |
| 22 | #include <sys/param.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 26 | #include <net/if.h> |
| 27 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
Willy Tarreau | 5d9ddc5 | 2021-10-06 19:54:09 +0200 | [diff] [blame] | 29 | #include <haproxy/activity.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 30 | #include <haproxy/connection.h> |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 31 | #include <haproxy/listener.h> |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 32 | #include <haproxy/log.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 33 | #include <haproxy/namespace.h> |
William Lallemand | 2be557f | 2021-11-24 18:45:37 +0100 | [diff] [blame] | 34 | #include <haproxy/proto_sockpair.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 35 | #include <haproxy/sock.h> |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 36 | #include <haproxy/sock_inet.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 37 | #include <haproxy/tools.h> |
| 38 | |
Willy Tarreau | 063d47d | 2020-08-28 16:29:53 +0200 | [diff] [blame] | 39 | /* the list of remaining sockets transferred from an older process */ |
| 40 | struct xfer_sock_list *xfer_sock_list = NULL; |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 42 | |
| 43 | /* Accept an incoming connection from listener <l>, and return it, as well as |
| 44 | * a CO_AC_* status code into <status> if not null. Null is returned on error. |
| 45 | * <l> must be a valid listener with a valid frontend. |
| 46 | */ |
| 47 | struct connection *sock_accept_conn(struct listener *l, int *status) |
| 48 | { |
| 49 | #ifdef USE_ACCEPT4 |
| 50 | static int accept4_broken; |
| 51 | #endif |
| 52 | struct proxy *p = l->bind_conf->frontend; |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 53 | struct connection *conn = NULL; |
| 54 | struct sockaddr_storage *addr = NULL; |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 55 | socklen_t laddr; |
| 56 | int ret; |
| 57 | int cfd; |
| 58 | |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 59 | if (!sockaddr_alloc(&addr, NULL, 0)) |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 60 | goto fail_addr; |
| 61 | |
| 62 | /* accept() will mark all accepted FDs O_NONBLOCK and the ones accepted |
| 63 | * in the master process as FD_CLOEXEC. It's not done for workers |
| 64 | * because 1) workers are not supposed to execute anything so there's |
| 65 | * no reason for uselessly slowing down everything, and 2) that would |
| 66 | * prevent us from implementing fd passing in the future. |
| 67 | */ |
| 68 | #ifdef USE_ACCEPT4 |
| 69 | laddr = sizeof(*conn->src); |
| 70 | |
| 71 | /* only call accept4() if it's known to be safe, otherwise fallback to |
| 72 | * the legacy accept() + fcntl(). |
| 73 | */ |
| 74 | if (unlikely(accept4_broken) || |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 75 | (((cfd = accept4(l->rx.fd, (struct sockaddr*)addr, &laddr, |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 76 | SOCK_NONBLOCK | (master ? SOCK_CLOEXEC : 0))) == -1) && |
| 77 | (errno == ENOSYS || errno == EINVAL || errno == EBADF) && |
Tim Duesterhus | f897fc9 | 2021-11-20 14:39:47 +0100 | [diff] [blame] | 78 | ((accept4_broken = 1)))) |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 79 | #endif |
| 80 | { |
| 81 | laddr = sizeof(*conn->src); |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 82 | if ((cfd = accept(l->rx.fd, (struct sockaddr*)addr, &laddr)) != -1) { |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 83 | fcntl(cfd, F_SETFL, O_NONBLOCK); |
| 84 | if (master) |
| 85 | fcntl(cfd, F_SETFD, FD_CLOEXEC); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (likely(cfd != -1)) { |
| 90 | /* Perfect, the connection was accepted */ |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 91 | conn = conn_new(&l->obj_type); |
| 92 | if (!conn) |
| 93 | goto fail_conn; |
| 94 | |
| 95 | conn->src = addr; |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 96 | conn->handle.fd = cfd; |
| 97 | conn->flags |= CO_FL_ADDR_FROM_SET; |
| 98 | ret = CO_AC_DONE; |
| 99 | goto done; |
| 100 | } |
| 101 | |
| 102 | /* error conditions below */ |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 103 | sockaddr_free(&addr); |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 104 | |
| 105 | switch (errno) { |
| 106 | case EAGAIN: |
| 107 | ret = CO_AC_DONE; /* nothing more to accept */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 108 | if (fdtab[l->rx.fd].state & (FD_POLL_HUP|FD_POLL_ERR)) { |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 109 | /* the listening socket might have been disabled in a shared |
| 110 | * process and we're a collateral victim. We'll just pause for |
| 111 | * a while in case it comes back. In the mean time, we need to |
| 112 | * clear this sticky flag. |
| 113 | */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 114 | _HA_ATOMIC_AND(&fdtab[l->rx.fd].state, ~(FD_POLL_HUP|FD_POLL_ERR)); |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 115 | ret = CO_AC_PAUSE; |
| 116 | } |
| 117 | fd_cant_recv(l->rx.fd); |
| 118 | break; |
| 119 | |
| 120 | case EINVAL: |
| 121 | /* might be trying to accept on a shut fd (eg: soft stop) */ |
| 122 | ret = CO_AC_PAUSE; |
| 123 | break; |
| 124 | |
| 125 | case EINTR: |
| 126 | case ECONNABORTED: |
| 127 | ret = CO_AC_RETRY; |
| 128 | break; |
| 129 | |
| 130 | case ENFILE: |
| 131 | if (p) |
| 132 | send_log(p, LOG_EMERG, |
| 133 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 134 | p->id, global.maxsock); |
| 135 | ret = CO_AC_PAUSE; |
| 136 | break; |
| 137 | |
| 138 | case EMFILE: |
| 139 | if (p) |
| 140 | send_log(p, LOG_EMERG, |
| 141 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 142 | p->id, global.maxsock); |
| 143 | ret = CO_AC_PAUSE; |
| 144 | break; |
| 145 | |
| 146 | case ENOBUFS: |
| 147 | case ENOMEM: |
| 148 | if (p) |
| 149 | send_log(p, LOG_EMERG, |
| 150 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 151 | p->id, global.maxsock); |
| 152 | ret = CO_AC_PAUSE; |
| 153 | break; |
| 154 | |
| 155 | default: |
| 156 | /* unexpected result, let's give up and let other tasks run */ |
| 157 | ret = CO_AC_YIELD; |
| 158 | } |
| 159 | done: |
| 160 | if (status) |
| 161 | *status = ret; |
| 162 | return conn; |
| 163 | |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 164 | fail_conn: |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 165 | sockaddr_free(&addr); |
Remi Tricot-Le Breton | 25dd0ad | 2021-01-14 15:26:24 +0100 | [diff] [blame] | 166 | /* The accept call already succeeded by the time we try to allocate the connection, |
| 167 | * we need to close it in case of failure. */ |
| 168 | close(cfd); |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 169 | fail_addr: |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 170 | ret = CO_AC_PAUSE; |
| 171 | goto done; |
| 172 | } |
| 173 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 174 | /* Create a socket to connect to the server in conn->dst (which MUST be valid), |
| 175 | * using the configured namespace if needed, or the one passed by the proxy |
| 176 | * protocol if required to do so. It ultimately calls socket() or socketat() |
| 177 | * and returns the FD or error code. |
| 178 | */ |
| 179 | int sock_create_server_socket(struct connection *conn) |
| 180 | { |
| 181 | const struct netns_entry *ns = NULL; |
| 182 | |
| 183 | #ifdef USE_NS |
| 184 | if (objt_server(conn->target)) { |
| 185 | if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP) |
| 186 | ns = conn->proxy_netns; |
| 187 | else |
| 188 | ns = __objt_server(conn->target)->netns; |
| 189 | } |
| 190 | #endif |
| 191 | return my_socketat(ns, conn->dst->ss_family, SOCK_STREAM, 0); |
| 192 | } |
| 193 | |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 194 | /* Enables receiving on receiver <rx> once already bound. */ |
Willy Tarreau | e70c797 | 2020-09-25 19:00:01 +0200 | [diff] [blame] | 195 | void sock_enable(struct receiver *rx) |
| 196 | { |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 197 | if (rx->flags & RX_F_BOUND) |
| 198 | fd_want_recv_safe(rx->fd); |
Willy Tarreau | e70c797 | 2020-09-25 19:00:01 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 201 | /* Disables receiving on receiver <rx> once already bound. */ |
Willy Tarreau | e70c797 | 2020-09-25 19:00:01 +0200 | [diff] [blame] | 202 | void sock_disable(struct receiver *rx) |
| 203 | { |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 204 | if (rx->flags & RX_F_BOUND) |
Willy Tarreau | e70c797 | 2020-09-25 19:00:01 +0200 | [diff] [blame] | 205 | fd_stop_recv(rx->fd); |
| 206 | } |
| 207 | |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 208 | /* stops, unbinds and possibly closes the FD associated with receiver rx */ |
| 209 | void sock_unbind(struct receiver *rx) |
| 210 | { |
| 211 | /* There are a number of situations where we prefer to keep the FD and |
| 212 | * not to close it (unless we're stopping, of course): |
| 213 | * - worker process unbinding from a worker's FD with socket transfer enabled => keep |
| 214 | * - master process unbinding from a master's inherited FD => keep |
| 215 | * - master process unbinding from a master's FD => close |
Willy Tarreau | 22ccd5e | 2020-11-03 18:38:05 +0100 | [diff] [blame] | 216 | * - master process unbinding from a worker's inherited FD => keep |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 217 | * - master process unbinding from a worker's FD => close |
| 218 | * - worker process unbinding from a master's FD => close |
| 219 | * - worker process unbinding from a worker's FD => close |
| 220 | */ |
| 221 | if (rx->flags & RX_F_BOUND) |
| 222 | rx->proto->rx_disable(rx); |
| 223 | |
| 224 | if (!stopping && !master && |
| 225 | !(rx->flags & RX_F_MWORKER) && |
| 226 | (global.tune.options & GTUNE_SOCKET_TRANSFER)) |
| 227 | return; |
| 228 | |
| 229 | if (!stopping && master && |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 230 | rx->flags & RX_F_INHERITED) |
| 231 | return; |
| 232 | |
| 233 | rx->flags &= ~RX_F_BOUND; |
| 234 | if (rx->fd != -1) |
| 235 | fd_delete(rx->fd); |
| 236 | rx->fd = -1; |
| 237 | } |
| 238 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 239 | /* |
| 240 | * Retrieves the source address for the socket <fd>, with <dir> indicating |
| 241 | * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of |
| 242 | * success, -1 in case of error. The socket's source address is stored in |
| 243 | * <sa> for <salen> bytes. |
| 244 | */ |
| 245 | int sock_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 246 | { |
| 247 | if (dir) |
| 248 | return getsockname(fd, sa, &salen); |
| 249 | else |
| 250 | return getpeername(fd, sa, &salen); |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Retrieves the original destination address for the socket <fd>, with <dir> |
| 255 | * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in |
| 256 | * case of success, -1 in case of error. The socket's source address is stored |
| 257 | * in <sa> for <salen> bytes. |
| 258 | */ |
| 259 | int sock_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 260 | { |
| 261 | if (dir) |
| 262 | return getpeername(fd, sa, &salen); |
| 263 | else |
| 264 | return getsockname(fd, sa, &salen); |
| 265 | } |
| 266 | |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 267 | /* Try to retrieve exported sockets from worker at CLI <unixsocket>. These |
| 268 | * ones will be placed into the xfer_sock_list for later use by function |
| 269 | * sock_find_compatible_fd(). Returns 0 on success, -1 on failure. |
| 270 | */ |
| 271 | int sock_get_old_sockets(const char *unixsocket) |
| 272 | { |
| 273 | char *cmsgbuf = NULL, *tmpbuf = NULL; |
| 274 | int *tmpfd = NULL; |
| 275 | struct sockaddr_un addr; |
| 276 | struct cmsghdr *cmsg; |
| 277 | struct msghdr msghdr; |
| 278 | struct iovec iov; |
| 279 | struct xfer_sock_list *xfer_sock = NULL; |
| 280 | struct timeval tv = { .tv_sec = 1, .tv_usec = 0 }; |
| 281 | int sock = -1; |
| 282 | int ret = -1; |
| 283 | int ret2 = -1; |
| 284 | int fd_nb; |
| 285 | int got_fd = 0; |
| 286 | int cur_fd = 0; |
| 287 | size_t maxoff = 0, curoff = 0; |
| 288 | |
William Lallemand | 2be557f | 2021-11-24 18:45:37 +0100 | [diff] [blame] | 289 | if (strncmp("sockpair@", unixsocket, strlen("sockpair@")) == 0) { |
| 290 | /* sockpair for master-worker usage */ |
| 291 | int sv[2]; |
| 292 | int dst_fd; |
| 293 | |
| 294 | dst_fd = strtoll(unixsocket + strlen("sockpair@"), NULL, 0); |
| 295 | |
| 296 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { |
| 297 | ha_warning("socketpair(): Cannot create socketpair. Giving up.\n"); |
| 298 | } |
| 299 | |
| 300 | if (send_fd_uxst(dst_fd, sv[0]) == -1) { |
| 301 | ha_alert("socketpair: cannot transfer socket.\n"); |
| 302 | close(sv[0]); |
| 303 | close(sv[1]); |
| 304 | goto out; |
| 305 | } |
| 306 | |
| 307 | close(sv[0]); /* we don't need this side anymore */ |
| 308 | sock = sv[1]; |
| 309 | |
| 310 | } else { |
| 311 | /* Unix socket */ |
| 312 | |
| 313 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 314 | if (sock < 0) { |
| 315 | ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket); |
| 316 | goto out; |
| 317 | } |
| 318 | |
| 319 | strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path) - 1); |
| 320 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
| 321 | addr.sun_family = PF_UNIX; |
| 322 | |
| 323 | ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr)); |
| 324 | if (ret < 0) { |
| 325 | ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket); |
| 326 | goto out; |
| 327 | } |
| 328 | |
| 329 | } |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 330 | memset(&msghdr, 0, sizeof(msghdr)); |
| 331 | cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD); |
| 332 | if (!cmsgbuf) { |
| 333 | ha_warning("Failed to allocate memory to send sockets\n"); |
| 334 | goto out; |
| 335 | } |
| 336 | |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 337 | setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv)); |
| 338 | iov.iov_base = &fd_nb; |
| 339 | iov.iov_len = sizeof(fd_nb); |
| 340 | msghdr.msg_iov = &iov; |
| 341 | msghdr.msg_iovlen = 1; |
| 342 | |
| 343 | if (send(sock, "_getsocks\n", strlen("_getsocks\n"), 0) != strlen("_getsocks\n")) { |
| 344 | ha_warning("Failed to get the number of sockets to be transferred !\n"); |
| 345 | goto out; |
| 346 | } |
| 347 | |
| 348 | /* First, get the number of file descriptors to be received */ |
| 349 | if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) { |
| 350 | ha_warning("Failed to get the number of sockets to be transferred !\n"); |
| 351 | goto out; |
| 352 | } |
| 353 | |
| 354 | if (fd_nb == 0) { |
| 355 | ret2 = 0; |
| 356 | goto out; |
| 357 | } |
| 358 | |
| 359 | tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int))); |
| 360 | if (tmpbuf == NULL) { |
| 361 | ha_warning("Failed to allocate memory while receiving sockets\n"); |
| 362 | goto out; |
| 363 | } |
| 364 | |
| 365 | tmpfd = malloc(fd_nb * sizeof(int)); |
| 366 | if (tmpfd == NULL) { |
| 367 | ha_warning("Failed to allocate memory while receiving sockets\n"); |
| 368 | goto out; |
| 369 | } |
| 370 | |
| 371 | msghdr.msg_control = cmsgbuf; |
| 372 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD; |
| 373 | iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)); |
| 374 | |
| 375 | do { |
| 376 | int ret3; |
| 377 | |
| 378 | iov.iov_base = tmpbuf + curoff; |
| 379 | |
| 380 | ret = recvmsg(sock, &msghdr, 0); |
| 381 | |
| 382 | if (ret == -1 && errno == EINTR) |
| 383 | continue; |
| 384 | |
| 385 | if (ret <= 0) |
| 386 | break; |
| 387 | |
| 388 | /* Send an ack to let the sender know we got the sockets |
| 389 | * and it can send some more |
| 390 | */ |
| 391 | do { |
| 392 | ret3 = send(sock, &got_fd, sizeof(got_fd), 0); |
| 393 | } while (ret3 == -1 && errno == EINTR); |
| 394 | |
| 395 | for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) { |
| 396 | if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { |
| 397 | size_t totlen = cmsg->cmsg_len - CMSG_LEN(0); |
| 398 | |
| 399 | if (totlen / sizeof(int) + got_fd > fd_nb) { |
| 400 | ha_warning("Got to many sockets !\n"); |
| 401 | goto out; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Be paranoid and use memcpy() to avoid any |
| 406 | * potential alignment issue. |
| 407 | */ |
| 408 | memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen); |
| 409 | got_fd += totlen / sizeof(int); |
| 410 | } |
| 411 | } |
| 412 | curoff += ret; |
| 413 | } while (got_fd < fd_nb); |
| 414 | |
| 415 | if (got_fd != fd_nb) { |
| 416 | ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n", |
| 417 | fd_nb, got_fd); |
| 418 | goto out; |
| 419 | } |
| 420 | |
| 421 | maxoff = curoff; |
| 422 | curoff = 0; |
| 423 | |
| 424 | for (cur_fd = 0; cur_fd < got_fd; cur_fd++) { |
| 425 | int fd = tmpfd[cur_fd]; |
| 426 | socklen_t socklen; |
| 427 | int val; |
| 428 | int len; |
| 429 | |
| 430 | xfer_sock = calloc(1, sizeof(*xfer_sock)); |
| 431 | if (!xfer_sock) { |
| 432 | ha_warning("Failed to allocate memory in get_old_sockets() !\n"); |
| 433 | break; |
| 434 | } |
| 435 | xfer_sock->fd = -1; |
| 436 | |
| 437 | socklen = sizeof(xfer_sock->addr); |
| 438 | if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) { |
| 439 | ha_warning("Failed to get socket address\n"); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 440 | ha_free(&xfer_sock); |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 441 | continue; |
| 442 | } |
| 443 | |
| 444 | if (curoff >= maxoff) { |
| 445 | ha_warning("Inconsistency while transferring sockets\n"); |
| 446 | goto out; |
| 447 | } |
| 448 | |
| 449 | len = tmpbuf[curoff++]; |
| 450 | if (len > 0) { |
| 451 | /* We have a namespace */ |
| 452 | if (curoff + len > maxoff) { |
| 453 | ha_warning("Inconsistency while transferring sockets\n"); |
| 454 | goto out; |
| 455 | } |
| 456 | xfer_sock->namespace = malloc(len + 1); |
| 457 | if (!xfer_sock->namespace) { |
| 458 | ha_warning("Failed to allocate memory while transferring sockets\n"); |
| 459 | goto out; |
| 460 | } |
| 461 | memcpy(xfer_sock->namespace, &tmpbuf[curoff], len); |
| 462 | xfer_sock->namespace[len] = 0; |
| 463 | xfer_sock->ns_namelen = len; |
| 464 | curoff += len; |
| 465 | } |
| 466 | |
| 467 | if (curoff >= maxoff) { |
| 468 | ha_warning("Inconsistency while transferring sockets\n"); |
| 469 | goto out; |
| 470 | } |
| 471 | |
| 472 | len = tmpbuf[curoff++]; |
| 473 | if (len > 0) { |
| 474 | /* We have an interface */ |
| 475 | if (curoff + len > maxoff) { |
| 476 | ha_warning("Inconsistency while transferring sockets\n"); |
| 477 | goto out; |
| 478 | } |
| 479 | xfer_sock->iface = malloc(len + 1); |
| 480 | if (!xfer_sock->iface) { |
| 481 | ha_warning("Failed to allocate memory while transferring sockets\n"); |
| 482 | goto out; |
| 483 | } |
| 484 | memcpy(xfer_sock->iface, &tmpbuf[curoff], len); |
| 485 | xfer_sock->iface[len] = 0; |
| 486 | xfer_sock->if_namelen = len; |
| 487 | curoff += len; |
| 488 | } |
| 489 | |
| 490 | if (curoff + sizeof(int) > maxoff) { |
| 491 | ha_warning("Inconsistency while transferring sockets\n"); |
| 492 | goto out; |
| 493 | } |
| 494 | |
| 495 | /* we used to have 32 bits of listener options here but we don't |
| 496 | * use them anymore. |
| 497 | */ |
| 498 | curoff += sizeof(int); |
| 499 | |
| 500 | /* determine the foreign status directly from the socket itself */ |
| 501 | if (sock_inet_is_foreign(fd, xfer_sock->addr.ss_family)) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 502 | xfer_sock->options |= SOCK_XFER_OPT_FOREIGN; |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 503 | |
Willy Tarreau | 9dbb6c4 | 2020-08-28 19:20:23 +0200 | [diff] [blame] | 504 | socklen = sizeof(val); |
| 505 | if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &val, &socklen) == 0 && val == SOCK_DGRAM) |
| 506 | xfer_sock->options |= SOCK_XFER_OPT_DGRAM; |
| 507 | |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 508 | #if defined(IPV6_V6ONLY) |
| 509 | /* keep only the v6only flag depending on what's currently |
| 510 | * active on the socket, and always drop the v4v6 one. |
| 511 | */ |
| 512 | socklen = sizeof(val); |
| 513 | if (xfer_sock->addr.ss_family == AF_INET6 && |
| 514 | getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &socklen) == 0 && val > 0) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 515 | xfer_sock->options |= SOCK_XFER_OPT_V6ONLY; |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 516 | #endif |
| 517 | |
| 518 | xfer_sock->fd = fd; |
| 519 | if (xfer_sock_list) |
| 520 | xfer_sock_list->prev = xfer_sock; |
| 521 | xfer_sock->next = xfer_sock_list; |
| 522 | xfer_sock->prev = NULL; |
| 523 | xfer_sock_list = xfer_sock; |
| 524 | xfer_sock = NULL; |
| 525 | } |
| 526 | |
| 527 | ret2 = 0; |
| 528 | out: |
| 529 | /* If we failed midway make sure to close the remaining |
| 530 | * file descriptors |
| 531 | */ |
| 532 | if (tmpfd != NULL && cur_fd < got_fd) { |
| 533 | for (; cur_fd < got_fd; cur_fd++) { |
| 534 | close(tmpfd[cur_fd]); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | free(tmpbuf); |
| 539 | free(tmpfd); |
| 540 | free(cmsgbuf); |
| 541 | |
| 542 | if (sock != -1) |
| 543 | close(sock); |
| 544 | |
| 545 | if (xfer_sock) { |
| 546 | free(xfer_sock->namespace); |
| 547 | free(xfer_sock->iface); |
| 548 | if (xfer_sock->fd != -1) |
| 549 | close(xfer_sock->fd); |
| 550 | free(xfer_sock); |
| 551 | } |
| 552 | return (ret2); |
| 553 | } |
| 554 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 555 | /* When binding the receivers, check if a socket has been sent to us by the |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 556 | * previous process that we could reuse, instead of creating a new one. Note |
| 557 | * that some address family-specific options are checked on the listener and |
| 558 | * on the socket. Typically for AF_INET and AF_INET6, we check for transparent |
| 559 | * mode, and for AF_INET6 we also check for "v4v6" or "v6only". The reused |
| 560 | * socket is automatically removed from the list so that it's not proposed |
| 561 | * anymore. |
| 562 | */ |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 563 | int sock_find_compatible_fd(const struct receiver *rx) |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 564 | { |
| 565 | struct xfer_sock_list *xfer_sock = xfer_sock_list; |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 566 | int options = 0; |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 567 | int if_namelen = 0; |
| 568 | int ns_namelen = 0; |
| 569 | int ret = -1; |
| 570 | |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 571 | if (!rx->proto->fam->addrcmp) |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 572 | return -1; |
| 573 | |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 574 | if (rx->proto->proto_type == PROTO_TYPE_DGRAM) |
Willy Tarreau | 9dbb6c4 | 2020-08-28 19:20:23 +0200 | [diff] [blame] | 575 | options |= SOCK_XFER_OPT_DGRAM; |
| 576 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 577 | if (rx->settings->options & RX_O_FOREIGN) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 578 | options |= SOCK_XFER_OPT_FOREIGN; |
| 579 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 580 | if (rx->addr.ss_family == AF_INET6) { |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 581 | /* Prepare to match the v6only option against what we really want. Note |
| 582 | * that sadly the two options are not exclusive to each other and that |
| 583 | * v6only is stronger than v4v6. |
| 584 | */ |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 585 | if ((rx->settings->options & RX_O_V6ONLY) || |
| 586 | (sock_inet6_v6only_default && !(rx->settings->options & RX_O_V4V6))) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 587 | options |= SOCK_XFER_OPT_V6ONLY; |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 588 | } |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 589 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 590 | if (rx->settings->interface) |
| 591 | if_namelen = strlen(rx->settings->interface); |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 592 | #ifdef USE_NS |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 593 | if (rx->settings->netns) |
| 594 | ns_namelen = rx->settings->netns->name_len; |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 595 | #endif |
| 596 | |
| 597 | while (xfer_sock) { |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 598 | if ((options == xfer_sock->options) && |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 599 | (if_namelen == xfer_sock->if_namelen) && |
| 600 | (ns_namelen == xfer_sock->ns_namelen) && |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 601 | (!if_namelen || strcmp(rx->settings->interface, xfer_sock->iface) == 0) && |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 602 | #ifdef USE_NS |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 603 | (!ns_namelen || strcmp(rx->settings->netns->node.key, xfer_sock->namespace) == 0) && |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 604 | #endif |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 605 | rx->proto->fam->addrcmp(&xfer_sock->addr, &rx->addr) == 0) |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 606 | break; |
| 607 | xfer_sock = xfer_sock->next; |
| 608 | } |
| 609 | |
| 610 | if (xfer_sock != NULL) { |
| 611 | ret = xfer_sock->fd; |
| 612 | if (xfer_sock == xfer_sock_list) |
| 613 | xfer_sock_list = xfer_sock->next; |
| 614 | if (xfer_sock->prev) |
| 615 | xfer_sock->prev->next = xfer_sock->next; |
| 616 | if (xfer_sock->next) |
| 617 | xfer_sock->next->prev = xfer_sock->prev; |
| 618 | free(xfer_sock->iface); |
| 619 | free(xfer_sock->namespace); |
| 620 | free(xfer_sock); |
| 621 | } |
| 622 | return ret; |
| 623 | } |
| 624 | |
Willy Tarreau | 5ced3e8 | 2020-10-13 17:06:12 +0200 | [diff] [blame] | 625 | /* Tests if the receiver supports accepting connections. Returns positive on |
| 626 | * success, 0 if not possible, negative if the socket is non-recoverable. The |
| 627 | * rationale behind this is that inherited FDs may be broken and that shared |
| 628 | * FDs might have been paused by another process. |
| 629 | */ |
Willy Tarreau | 7d053e4 | 2020-10-15 09:19:43 +0200 | [diff] [blame] | 630 | int sock_accepting_conn(const struct receiver *rx) |
Willy Tarreau | 5ced3e8 | 2020-10-13 17:06:12 +0200 | [diff] [blame] | 631 | { |
| 632 | int opt_val = 0; |
| 633 | socklen_t opt_len = sizeof(opt_val); |
| 634 | |
| 635 | if (getsockopt(rx->fd, SOL_SOCKET, SO_ACCEPTCONN, &opt_val, &opt_len) == -1) |
| 636 | return -1; |
| 637 | |
| 638 | return opt_val; |
| 639 | } |
| 640 | |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 641 | /* This is the FD handler IO callback for stream sockets configured for |
| 642 | * accepting incoming connections. It's a pass-through to listener_accept() |
| 643 | * which will iterate over the listener protocol's accept_conn() function. |
| 644 | * The FD's owner must be a listener. |
| 645 | */ |
| 646 | void sock_accept_iocb(int fd) |
| 647 | { |
| 648 | struct listener *l = fdtab[fd].owner; |
| 649 | |
| 650 | if (!l) |
| 651 | return; |
| 652 | |
Willy Tarreau | b4daeeb | 2020-11-04 14:58:36 +0100 | [diff] [blame] | 653 | BUG_ON(!!master != !!(l->rx.flags & RX_F_MWORKER)); |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 654 | listener_accept(l); |
| 655 | } |
| 656 | |
Willy Tarreau | de471c4 | 2020-12-08 15:50:56 +0100 | [diff] [blame] | 657 | /* This completes the initialization of connection <conn> by inserting its FD |
| 658 | * into the fdtab, associating it with the regular connection handler. It will |
| 659 | * be bound to the current thread only. This call cannot fail. |
| 660 | */ |
| 661 | void sock_conn_ctrl_init(struct connection *conn) |
| 662 | { |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 663 | fd_insert(conn->handle.fd, conn, sock_conn_iocb, tid_bit); |
Willy Tarreau | de471c4 | 2020-12-08 15:50:56 +0100 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* This completes the release of connection <conn> by removing its FD from the |
| 667 | * fdtab and deleting it. The connection must not use the FD anymore past this |
| 668 | * point. The FD may be modified in the connection. |
| 669 | */ |
| 670 | void sock_conn_ctrl_close(struct connection *conn) |
| 671 | { |
| 672 | fd_delete(conn->handle.fd); |
| 673 | conn->handle.fd = DEAD_FD_MAGIC; |
| 674 | } |
| 675 | |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 676 | /* This is the callback which is set when a connection establishment is pending |
| 677 | * and we have nothing to send. It may update the FD polling status to indicate |
| 678 | * !READY. It returns 0 if it fails in a fatal way or needs to poll to go |
| 679 | * further, otherwise it returns non-zero and removes the CO_FL_WAIT_L4_CONN |
| 680 | * flag from the connection's flags. In case of error, it sets CO_FL_ERROR and |
| 681 | * leaves the error code in errno. |
| 682 | */ |
| 683 | int sock_conn_check(struct connection *conn) |
| 684 | { |
| 685 | struct sockaddr_storage *addr; |
| 686 | int fd = conn->handle.fd; |
| 687 | |
| 688 | if (conn->flags & CO_FL_ERROR) |
| 689 | return 0; |
| 690 | |
| 691 | if (!conn_ctrl_ready(conn)) |
| 692 | return 0; |
| 693 | |
| 694 | if (!(conn->flags & CO_FL_WAIT_L4_CONN)) |
| 695 | return 1; /* strange we were called while ready */ |
| 696 | |
Willy Tarreau | 5a9c637 | 2021-07-06 08:29:20 +0200 | [diff] [blame] | 697 | if (!fd_send_ready(fd) && !(fdtab[fd].state & (FD_POLL_ERR|FD_POLL_HUP))) |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 698 | return 0; |
| 699 | |
| 700 | /* Here we have 2 cases : |
| 701 | * - modern pollers, able to report ERR/HUP. If these ones return any |
| 702 | * of these flags then it's likely a failure, otherwise it possibly |
| 703 | * is a success (i.e. there may have been data received just before |
| 704 | * the error was reported). |
| 705 | * - select, which doesn't report these and with which it's always |
| 706 | * necessary either to try connect() again or to check for SO_ERROR. |
| 707 | * In order to simplify everything, we double-check using connect() as |
| 708 | * soon as we meet either of these delicate situations. Note that |
| 709 | * SO_ERROR would clear the error after reporting it! |
| 710 | */ |
| 711 | if (cur_poller.flags & HAP_POLL_F_ERRHUP) { |
| 712 | /* modern poller, able to report ERR/HUP */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 713 | if ((fdtab[fd].state & (FD_POLL_IN|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_IN) |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 714 | goto done; |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 715 | if ((fdtab[fd].state & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_OUT) |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 716 | goto done; |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 717 | if (!(fdtab[fd].state & (FD_POLL_ERR|FD_POLL_HUP))) |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 718 | goto wait; |
| 719 | /* error present, fall through common error check path */ |
| 720 | } |
| 721 | |
| 722 | /* Use connect() to check the state of the socket. This has the double |
| 723 | * advantage of *not* clearing the error (so that health checks can |
| 724 | * still use getsockopt(SO_ERROR)) and giving us the following info : |
| 725 | * - error |
| 726 | * - connecting (EALREADY, EINPROGRESS) |
| 727 | * - connected (EISCONN, 0) |
| 728 | */ |
| 729 | addr = conn->dst; |
| 730 | if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER) |
| 731 | addr = &objt_server(conn->target)->socks4_addr; |
| 732 | |
| 733 | if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) { |
| 734 | if (errno == EALREADY || errno == EINPROGRESS) |
| 735 | goto wait; |
| 736 | |
| 737 | if (errno && errno != EISCONN) |
| 738 | goto out_error; |
| 739 | } |
| 740 | |
| 741 | done: |
| 742 | /* The FD is ready now, we'll mark the connection as complete and |
| 743 | * forward the event to the transport layer which will notify the |
| 744 | * data layer. |
| 745 | */ |
| 746 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 747 | fd_may_send(fd); |
| 748 | fd_cond_recv(fd); |
| 749 | errno = 0; // make health checks happy |
| 750 | return 1; |
| 751 | |
| 752 | out_error: |
| 753 | /* Write error on the file descriptor. Report it to the connection |
| 754 | * and disable polling on this FD. |
| 755 | */ |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 756 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
Willy Tarreau | b41a6e9 | 2021-04-06 17:49:19 +0200 | [diff] [blame] | 757 | HA_ATOMIC_AND(&fdtab[fd].state, ~FD_LINGER_RISK); |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 758 | fd_stop_both(fd); |
| 759 | return 0; |
| 760 | |
| 761 | wait: |
| 762 | fd_cant_send(fd); |
| 763 | fd_want_send(fd); |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | /* I/O callback for fd-based connections. It calls the read/write handlers |
| 768 | * provided by the connection's sock_ops, which must be valid. |
| 769 | */ |
| 770 | void sock_conn_iocb(int fd) |
| 771 | { |
| 772 | struct connection *conn = fdtab[fd].owner; |
| 773 | unsigned int flags; |
| 774 | int need_wake = 0; |
| 775 | |
| 776 | if (unlikely(!conn)) { |
| 777 | activity[tid].conn_dead++; |
| 778 | return; |
| 779 | } |
| 780 | |
| 781 | flags = conn->flags & ~CO_FL_ERROR; /* ensure to call the wake handler upon error */ |
| 782 | |
| 783 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && |
| 784 | ((fd_send_ready(fd) && fd_send_active(fd)) || |
| 785 | (fd_recv_ready(fd) && fd_recv_active(fd)))) { |
| 786 | /* Still waiting for a connection to establish and nothing was |
| 787 | * attempted yet to probe the connection. this will clear the |
| 788 | * CO_FL_WAIT_L4_CONN flag on success. |
| 789 | */ |
| 790 | if (!sock_conn_check(conn)) |
| 791 | goto leave; |
| 792 | need_wake = 1; |
| 793 | } |
| 794 | |
| 795 | if (fd_send_ready(fd) && fd_send_active(fd)) { |
| 796 | /* force reporting of activity by clearing the previous flags : |
| 797 | * we'll have at least ERROR or CONNECTED at the end of an I/O, |
| 798 | * both of which will be detected below. |
| 799 | */ |
| 800 | flags = 0; |
| 801 | if (conn->subs && conn->subs->events & SUB_RETRY_SEND) { |
| 802 | need_wake = 0; // wake will be called after this I/O |
| 803 | tasklet_wakeup(conn->subs->tasklet); |
| 804 | conn->subs->events &= ~SUB_RETRY_SEND; |
| 805 | if (!conn->subs->events) |
| 806 | conn->subs = NULL; |
| 807 | } |
| 808 | fd_stop_send(fd); |
| 809 | } |
| 810 | |
| 811 | /* The data transfer starts here and stops on error and handshakes. Note |
| 812 | * that we must absolutely test conn->xprt at each step in case it suddenly |
| 813 | * changes due to a quick unexpected close(). |
| 814 | */ |
| 815 | if (fd_recv_ready(fd) && fd_recv_active(fd)) { |
| 816 | /* force reporting of activity by clearing the previous flags : |
| 817 | * we'll have at least ERROR or CONNECTED at the end of an I/O, |
| 818 | * both of which will be detected below. |
| 819 | */ |
| 820 | flags = 0; |
| 821 | if (conn->subs && conn->subs->events & SUB_RETRY_RECV) { |
| 822 | need_wake = 0; // wake will be called after this I/O |
| 823 | tasklet_wakeup(conn->subs->tasklet); |
| 824 | conn->subs->events &= ~SUB_RETRY_RECV; |
| 825 | if (!conn->subs->events) |
| 826 | conn->subs = NULL; |
| 827 | } |
| 828 | fd_stop_recv(fd); |
| 829 | } |
| 830 | |
| 831 | leave: |
| 832 | /* we may have to finish to install a mux or to wake it up based on |
| 833 | * what was just done above. It may kill the connection so we have to |
| 834 | * be prpared not to use it anymore. |
| 835 | */ |
| 836 | if (conn_notify_mux(conn, flags, need_wake) < 0) |
| 837 | return; |
| 838 | |
| 839 | /* commit polling changes in case of error. |
| 840 | * WT: it seems that the last case where this could still be relevant |
| 841 | * is if a mux wake function above report a connection error but does |
| 842 | * not stop polling. Shouldn't we enforce this into the mux instead of |
| 843 | * having to deal with this ? |
| 844 | */ |
| 845 | if (unlikely(conn->flags & CO_FL_ERROR)) { |
| 846 | if (conn_ctrl_ready(conn)) |
| 847 | fd_stop_both(fd); |
| 848 | } |
| 849 | } |
| 850 | |
Willy Tarreau | 427c846 | 2020-12-11 16:19:12 +0100 | [diff] [blame] | 851 | /* Drains possibly pending incoming data on the file descriptor attached to the |
| 852 | * connection. This is used to know whether we need to disable lingering on |
| 853 | * close. Returns non-zero if it is safe to close without disabling lingering, |
| 854 | * otherwise zero. |
| 855 | */ |
| 856 | int sock_drain(struct connection *conn) |
| 857 | { |
| 858 | int turns = 2; |
| 859 | int fd = conn->handle.fd; |
| 860 | int len; |
| 861 | |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 862 | if (fdtab[fd].state & (FD_POLL_ERR|FD_POLL_HUP)) |
Willy Tarreau | 427c846 | 2020-12-11 16:19:12 +0100 | [diff] [blame] | 863 | goto shut; |
| 864 | |
Willy Tarreau | 20b622e | 2021-10-21 21:31:42 +0200 | [diff] [blame] | 865 | if (!(conn->flags & CO_FL_WANT_DRAIN) && !fd_recv_ready(fd)) |
Willy Tarreau | 427c846 | 2020-12-11 16:19:12 +0100 | [diff] [blame] | 866 | return 0; |
| 867 | |
| 868 | /* no drain function defined, use the generic one */ |
| 869 | |
| 870 | while (turns) { |
| 871 | #ifdef MSG_TRUNC_CLEARS_INPUT |
| 872 | len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC); |
| 873 | if (len == -1 && errno == EFAULT) |
| 874 | #endif |
| 875 | len = recv(fd, trash.area, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 876 | |
| 877 | if (len == 0) |
| 878 | goto shut; |
| 879 | |
| 880 | if (len < 0) { |
| 881 | if (errno == EAGAIN) { |
| 882 | /* connection not closed yet */ |
| 883 | fd_cant_recv(fd); |
| 884 | break; |
| 885 | } |
| 886 | if (errno == EINTR) /* oops, try again */ |
| 887 | continue; |
| 888 | /* other errors indicate a dead connection, fine. */ |
| 889 | goto shut; |
| 890 | } |
| 891 | /* OK we read some data, let's try again once */ |
| 892 | turns--; |
| 893 | } |
| 894 | |
| 895 | /* some data are still present, give up */ |
| 896 | return 0; |
| 897 | |
| 898 | shut: |
| 899 | /* we're certain the connection was shut down */ |
Willy Tarreau | b41a6e9 | 2021-04-06 17:49:19 +0200 | [diff] [blame] | 900 | HA_ATOMIC_AND(&fdtab[fd].state, ~FD_LINGER_RISK); |
Willy Tarreau | 427c846 | 2020-12-11 16:19:12 +0100 | [diff] [blame] | 901 | return 1; |
| 902 | } |
| 903 | |
Willy Tarreau | 472125b | 2020-12-11 17:02:50 +0100 | [diff] [blame] | 904 | /* Checks the connection's FD for readiness of events <event_type>, which may |
| 905 | * only be a combination of SUB_RETRY_RECV and SUB_RETRY_SEND. Those which are |
| 906 | * ready are returned. The ones that are not ready are enabled. The caller is |
| 907 | * expected to do what is needed to handle ready events and to deal with |
| 908 | * subsequent wakeups caused by the requested events' readiness. |
| 909 | */ |
| 910 | int sock_check_events(struct connection *conn, int event_type) |
| 911 | { |
| 912 | int ret = 0; |
| 913 | |
| 914 | if (event_type & SUB_RETRY_RECV) { |
| 915 | if (fd_recv_ready(conn->handle.fd)) |
| 916 | ret |= SUB_RETRY_RECV; |
| 917 | else |
| 918 | fd_want_recv(conn->handle.fd); |
| 919 | } |
| 920 | |
| 921 | if (event_type & SUB_RETRY_SEND) { |
| 922 | if (fd_send_ready(conn->handle.fd)) |
| 923 | ret |= SUB_RETRY_SEND; |
| 924 | else |
| 925 | fd_want_send(conn->handle.fd); |
| 926 | } |
| 927 | |
| 928 | return ret; |
| 929 | } |
| 930 | |
| 931 | /* Ignore readiness events from connection's FD for events of types <event_type> |
| 932 | * which may only be a combination of SUB_RETRY_RECV and SUB_RETRY_SEND. |
| 933 | */ |
| 934 | void sock_ignore_events(struct connection *conn, int event_type) |
| 935 | { |
| 936 | if (event_type & SUB_RETRY_RECV) |
| 937 | fd_stop_recv(conn->handle.fd); |
| 938 | |
| 939 | if (event_type & SUB_RETRY_SEND) |
| 940 | fd_stop_send(conn->handle.fd); |
| 941 | } |
| 942 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 943 | /* |
| 944 | * Local variables: |
| 945 | * c-indent-level: 8 |
| 946 | * c-basic-offset: 8 |
| 947 | * End: |
| 948 | */ |