William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 29 | #include <haproxy/api.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 30 | #include <haproxy/connection.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 31 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 32 | #include <haproxy/fd.h> |
| 33 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 34 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 35 | #include <haproxy/list.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 36 | #include <haproxy/listener.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 37 | #include <haproxy/protocol.h> |
Willy Tarreau | 62292b2 | 2020-09-02 17:52:23 +0200 | [diff] [blame] | 38 | #include <haproxy/proto_sockpair.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 39 | #include <haproxy/time.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 40 | #include <haproxy/tools.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 41 | #include <haproxy/version.h> |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 42 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 43 | |
| 44 | static void sockpair_add_listener(struct listener *listener, int port); |
| 45 | static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen); |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 46 | static int sockpair_connect_server(struct connection *conn, int flags); |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 47 | |
Willy Tarreau | b0254cb | 2020-09-04 08:07:11 +0200 | [diff] [blame] | 48 | struct proto_fam proto_fam_sockpair = { |
| 49 | .name = "sockpair", |
| 50 | .sock_domain = AF_CUST_SOCKPAIR, |
| 51 | .sock_family = AF_UNIX, |
| 52 | .sock_addrlen = sizeof(struct sockaddr_un), |
| 53 | .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path), |
| 54 | .addrcmp = NULL, |
| 55 | .bind = sockpair_bind_receiver, |
| 56 | .get_src = NULL, |
| 57 | .get_dst = NULL, |
| 58 | }; |
| 59 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 60 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 61 | static struct protocol proto_sockpair = { |
| 62 | .name = "sockpair", |
Willy Tarreau | b0254cb | 2020-09-04 08:07:11 +0200 | [diff] [blame] | 63 | .fam = &proto_fam_sockpair, |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 64 | .sock_domain = AF_CUST_SOCKPAIR, |
| 65 | .sock_type = SOCK_STREAM, |
| 66 | .sock_prot = 0, |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 67 | .accept = &listener_accept, |
| 68 | .connect = &sockpair_connect_server, |
Willy Tarreau | b3580b1 | 2020-09-01 10:26:22 +0200 | [diff] [blame] | 69 | .listen = sockpair_bind_listener, |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 70 | .enable_all = enable_all_listeners, |
| 71 | .disable_all = disable_all_listeners, |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 72 | .pause = NULL, |
| 73 | .add = sockpair_add_listener, |
| 74 | .listeners = LIST_HEAD_INIT(proto_sockpair.listeners), |
| 75 | .nb_listeners = 0, |
| 76 | }; |
| 77 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 78 | INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair); |
| 79 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 80 | /* Add <listener> to the list of sockpair listeners (port is ignored). The |
| 81 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 82 | * The number of listeners for the protocol is updated. |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 83 | * |
| 84 | * Must be called with proto_lock held. |
| 85 | * |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 86 | */ |
| 87 | static void sockpair_add_listener(struct listener *listener, int port) |
| 88 | { |
| 89 | if (listener->state != LI_INIT) |
| 90 | return; |
| 91 | listener->state = LI_ASSIGNED; |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 92 | listener->rx.proto = &proto_sockpair; |
| 93 | LIST_ADDQ(&proto_sockpair.listeners, &listener->rx.proto_list); |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 94 | proto_sockpair.nb_listeners++; |
| 95 | } |
| 96 | |
Willy Tarreau | 62292b2 | 2020-09-02 17:52:23 +0200 | [diff] [blame] | 97 | /* Binds receiver <rx>, and assigns <handler> and rx->owner as the callback and |
| 98 | * context, respectively, with <tm> as the thread mask. Returns and error code |
| 99 | * made of ERR_* bits on failure or ERR_NONE on success. On failure, an error |
| 100 | * message may be passed into <errmsg>. Note that the binding address is only |
| 101 | * an FD to receive the incoming FDs on. Thus by definition there is no real |
| 102 | * "bind" operation, this only completes the receiver. Such FDs are not |
| 103 | * inherited upon reload. |
| 104 | */ |
| 105 | int sockpair_bind_receiver(struct receiver *rx, void (*handler)(int fd), char **errmsg) |
| 106 | { |
| 107 | int err; |
| 108 | |
| 109 | /* ensure we never return garbage */ |
| 110 | if (errmsg) |
| 111 | *errmsg = 0; |
| 112 | |
| 113 | err = ERR_NONE; |
| 114 | |
| 115 | if (rx->flags & RX_F_BOUND) |
| 116 | return ERR_NONE; |
| 117 | |
| 118 | if (rx->fd == -1) { |
| 119 | err |= ERR_FATAL | ERR_ALERT; |
| 120 | memprintf(errmsg, "sockpair may be only used with inherited FDs"); |
| 121 | goto bind_return; |
| 122 | } |
| 123 | |
| 124 | if (rx->fd >= global.maxsock) { |
| 125 | err |= ERR_FATAL | ERR_ABORT | ERR_ALERT; |
| 126 | memprintf(errmsg, "not enough free sockets (raise '-n' parameter)"); |
| 127 | goto bind_close_return; |
| 128 | } |
| 129 | |
| 130 | if (fcntl(rx->fd, F_SETFL, O_NONBLOCK) == -1) { |
| 131 | err |= ERR_FATAL | ERR_ALERT; |
| 132 | memprintf(errmsg, "cannot make socket non-blocking"); |
| 133 | goto bind_close_return; |
| 134 | } |
| 135 | |
| 136 | rx->flags |= RX_F_BOUND; |
| 137 | |
| 138 | fd_insert(rx->fd, rx->owner, handler, thread_mask(rx->settings->bind_thread) & all_threads_mask); |
| 139 | return err; |
| 140 | |
| 141 | bind_return: |
| 142 | if (errmsg && *errmsg) |
| 143 | memprintf(errmsg, "%s [fd %d]", *errmsg, rx->fd); |
| 144 | |
| 145 | return err; |
| 146 | |
| 147 | bind_close_return: |
| 148 | close(rx->fd); |
| 149 | goto bind_return; |
| 150 | } |
| 151 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 152 | /* This function changes the state from ASSIGNED to LISTEN. The socket is NOT |
| 153 | * enabled for polling. The return value is composed from ERR_NONE, |
| 154 | * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in |
| 155 | * <errmsg> if the message is at most <errlen> bytes long (including '\0'). |
| 156 | * Note that <errmsg> may be NULL if <errlen> is also zero. |
| 157 | */ |
| 158 | static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 159 | { |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 160 | int err; |
Willy Tarreau | 9eda7a6 | 2020-09-02 18:02:00 +0200 | [diff] [blame] | 161 | char *msg = NULL; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 162 | |
| 163 | err = ERR_NONE; |
| 164 | |
| 165 | /* ensure we never return garbage */ |
| 166 | if (errlen) |
| 167 | *errmsg = 0; |
| 168 | |
| 169 | if (listener->state != LI_ASSIGNED) |
| 170 | return ERR_NONE; /* already bound */ |
| 171 | |
Willy Tarreau | ad33acf | 2020-09-02 18:40:02 +0200 | [diff] [blame] | 172 | if (!(listener->rx.flags & RX_F_BOUND)) { |
| 173 | msg = "receiving socket not bound"; |
| 174 | goto err_return; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 175 | } |
Willy Tarreau | ad33acf | 2020-09-02 18:40:02 +0200 | [diff] [blame] | 176 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 177 | listener->state = LI_LISTEN; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 178 | return err; |
| 179 | |
| 180 | err_return: |
| 181 | if (msg && errlen) |
Willy Tarreau | 9eda7a6 | 2020-09-02 18:02:00 +0200 | [diff] [blame] | 182 | snprintf(errmsg, errlen, "%s [fd %d]", msg, listener->rx.fd); |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 183 | return err; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Send FD over a unix socket |
| 188 | * |
| 189 | * <send_fd> is the FD to send |
| 190 | * <fd> is the fd of the unix socket to use for the transfer |
| 191 | * |
| 192 | * The iobuf variable could be use in the future to enhance the protocol. |
| 193 | */ |
| 194 | int send_fd_uxst(int fd, int send_fd) |
| 195 | { |
| 196 | char iobuf[2]; |
| 197 | struct iovec iov; |
| 198 | struct msghdr msghdr; |
| 199 | |
| 200 | char cmsgbuf[CMSG_SPACE(sizeof(int))]; |
| 201 | char buf[CMSG_SPACE(sizeof(int))]; |
| 202 | struct cmsghdr *cmsg = (void *)buf; |
| 203 | |
| 204 | int *fdptr; |
| 205 | |
| 206 | iov.iov_base = iobuf; |
| 207 | iov.iov_len = sizeof(iobuf); |
| 208 | |
| 209 | memset(&msghdr, 0, sizeof(msghdr)); |
| 210 | msghdr.msg_iov = &iov; |
| 211 | msghdr.msg_iovlen = 1; |
| 212 | |
| 213 | /* Now send the fds */ |
| 214 | msghdr.msg_control = cmsgbuf; |
| 215 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int)); |
| 216 | |
| 217 | cmsg = CMSG_FIRSTHDR(&msghdr); |
| 218 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 219 | cmsg->cmsg_level = SOL_SOCKET; |
| 220 | cmsg->cmsg_type = SCM_RIGHTS; |
| 221 | |
| 222 | fdptr = (int *)CMSG_DATA(cmsg); |
| 223 | memcpy(fdptr, &send_fd, sizeof(send_fd)); |
| 224 | |
| 225 | if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) { |
| 226 | ha_warning("Failed to transfer socket\n"); |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * |
Joseph Herlant | 8bb32ae | 2018-11-25 11:43:27 -0800 | [diff] [blame] | 235 | * This function works like uxst_connect_server but instead of creating a |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 236 | * socket and establishing a connection, it creates a pair of connected |
| 237 | * sockets, and send one of them through the destination FD. The destination FD |
Willy Tarreau | 3f4fa09 | 2019-07-17 16:42:04 +0200 | [diff] [blame] | 238 | * is stored in conn->dst->sin_addr.s_addr during configuration parsing. |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 239 | * |
| 240 | * conn->target may point either to a valid server or to a backend, depending |
| 241 | * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The |
| 242 | * <data> parameter is a boolean indicating whether there are data waiting for |
| 243 | * being sent or not, in order to adjust data write polling and on some |
| 244 | * platforms. The <delack> argument is ignored. |
| 245 | * |
| 246 | * Note that a pending send_proxy message accounts for data. |
| 247 | * |
| 248 | * It can return one of : |
| 249 | * - SF_ERR_NONE if everything's OK |
| 250 | * - SF_ERR_SRVTO if there are no more servers |
| 251 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 252 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 253 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 254 | * - SF_ERR_INTERNAL for any other purely internal errors |
| 255 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
| 256 | * |
| 257 | * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise |
| 258 | * it's invalid and the caller has nothing to do. |
| 259 | */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 260 | static int sockpair_connect_server(struct connection *conn, int flags) |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 261 | { |
| 262 | int sv[2], fd, dst_fd = -1; |
| 263 | |
| 264 | /* the FD is stored in the sockaddr struct */ |
Willy Tarreau | 3f4fa09 | 2019-07-17 16:42:04 +0200 | [diff] [blame] | 265 | dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 266 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 267 | if (obj_type(conn->target) != OBJ_TYPE_PROXY && |
| 268 | obj_type(conn->target) != OBJ_TYPE_SERVER) { |
| 269 | conn->flags |= CO_FL_ERROR; |
| 270 | return SF_ERR_INTERNAL; |
| 271 | } |
| 272 | |
| 273 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { |
| 274 | ha_alert("socketpair(): Cannot create socketpair. Giving up.\n"); |
| 275 | conn->flags |= CO_FL_ERROR; |
| 276 | return SF_ERR_RESOURCE; |
| 277 | } |
| 278 | |
| 279 | fd = conn->handle.fd = sv[1]; |
| 280 | |
| 281 | if (fd >= global.maxsock) { |
| 282 | /* do not log anything there, it's a normal condition when this option |
| 283 | * is used to serialize connections to a server ! |
| 284 | */ |
| 285 | ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 286 | close(sv[0]); |
| 287 | close(sv[1]); |
| 288 | conn->err_code = CO_ER_CONF_FDLIM; |
| 289 | conn->flags |= CO_FL_ERROR; |
| 290 | return SF_ERR_PRXCOND; /* it is a configuration limit */ |
| 291 | } |
| 292 | |
| 293 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
| 294 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 295 | close(sv[0]); |
| 296 | close(sv[1]); |
| 297 | conn->err_code = CO_ER_SOCK_ERR; |
| 298 | conn->flags |= CO_FL_ERROR; |
| 299 | return SF_ERR_INTERNAL; |
| 300 | } |
| 301 | |
William Lallemand | c03eb01 | 2018-11-27 12:02:37 +0100 | [diff] [blame] | 302 | if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) { |
| 303 | ha_alert("Cannot set CLOEXEC on client socket.\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 Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 311 | /* if a send_proxy is there, there are data */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 312 | if (conn->send_proxy_ofs) |
| 313 | flags |= CONNECT_HAS_DATA; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 314 | |
| 315 | if (global.tune.server_sndbuf) |
| 316 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 317 | |
| 318 | if (global.tune.server_rcvbuf) |
| 319 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 320 | |
| 321 | /* The new socket is sent on the other side, it should be retrieved and |
| 322 | * considered as an 'accept' socket on the server side */ |
| 323 | if (send_fd_uxst(dst_fd, sv[0]) == -1) { |
| 324 | close(sv[0]); |
| 325 | close(sv[1]); |
| 326 | conn->err_code = CO_ER_SOCK_ERR; |
| 327 | conn->flags |= CO_FL_ERROR; |
| 328 | return SF_ERR_INTERNAL; |
| 329 | } |
| 330 | |
| 331 | close(sv[0]); /* we don't need this side anymore */ |
| 332 | |
| 333 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 334 | |
| 335 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 336 | |
| 337 | /* Prepare to send a few handshakes related to the on-wire protocol. */ |
| 338 | if (conn->send_proxy_ofs) |
| 339 | conn->flags |= CO_FL_SEND_PROXY; |
| 340 | |
| 341 | conn_ctrl_init(conn); /* registers the FD */ |
| 342 | fdtab[fd].linger_risk = 0; /* no need to disable lingering */ |
| 343 | |
| 344 | if (conn_xprt_init(conn) < 0) { |
| 345 | conn_full_close(conn); |
| 346 | conn->flags |= CO_FL_ERROR; |
| 347 | return SF_ERR_RESOURCE; |
| 348 | } |
| 349 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 350 | return SF_ERR_NONE; /* connection is OK */ |
| 351 | } |
| 352 | |
| 353 | |
| 354 | /* |
Joseph Herlant | 8bb32ae | 2018-11-25 11:43:27 -0800 | [diff] [blame] | 355 | * Receives a file descriptor transferred from a unix socket. |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 356 | * |
| 357 | * Return -1 or a socket fd; |
| 358 | * |
Joseph Herlant | 8bb32ae | 2018-11-25 11:43:27 -0800 | [diff] [blame] | 359 | * The iobuf variable could be used in the future to enhance the protocol. |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 360 | */ |
| 361 | int recv_fd_uxst(int sock) |
| 362 | { |
| 363 | struct msghdr msghdr; |
| 364 | struct iovec iov; |
| 365 | char iobuf[2]; |
| 366 | |
| 367 | char cmsgbuf[CMSG_SPACE(sizeof(int))]; |
| 368 | char buf[CMSG_SPACE(sizeof(int))]; |
| 369 | struct cmsghdr *cmsg = (void *)buf; |
| 370 | |
| 371 | |
| 372 | int recv_fd = -1; |
| 373 | int ret = -1; |
| 374 | |
| 375 | memset(&msghdr, 0, sizeof(msghdr)); |
| 376 | |
| 377 | iov.iov_base = iobuf; |
| 378 | iov.iov_len = sizeof(iobuf); |
| 379 | |
| 380 | msghdr.msg_iov = &iov; |
| 381 | msghdr.msg_iovlen = 1; |
| 382 | |
| 383 | msghdr.msg_control = cmsgbuf; |
| 384 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int)); |
| 385 | |
| 386 | iov.iov_len = sizeof(iobuf); |
| 387 | iov.iov_base = iobuf; |
| 388 | |
| 389 | while (1) { |
| 390 | ret = recvmsg(sock, &msghdr, 0); |
| 391 | if (ret == -1 && errno == EINTR) |
| 392 | continue; |
| 393 | else |
| 394 | break; |
| 395 | } |
| 396 | |
| 397 | if (ret == -1) |
| 398 | return ret; |
| 399 | |
| 400 | cmsg = CMSG_FIRSTHDR(&msghdr); |
Willy Tarreau | 7d7ab43 | 2018-09-20 11:39:39 +0200 | [diff] [blame] | 401 | if (cmsg && cmsg->cmsg_level == SOL_SOCKET && |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 402 | cmsg->cmsg_type == SCM_RIGHTS) { |
| 403 | size_t totlen = cmsg->cmsg_len - |
| 404 | CMSG_LEN(0); |
| 405 | memcpy(&recv_fd, CMSG_DATA(cmsg), totlen); |
| 406 | } |
| 407 | return recv_fd; |
| 408 | } |
| 409 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 410 | /* |
| 411 | * Local variables: |
| 412 | * c-indent-level: 8 |
| 413 | * c-basic-offset: 8 |
| 414 | * End: |
| 415 | */ |