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