Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * UNIX SOCK_STREAM protocol layer (uxst) |
| 3 | * |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 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 <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
| 20 | #include <time.h> |
| 21 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/un.h> |
| 26 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 27 | #include <haproxy/api.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 28 | #include <haproxy/connection.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 29 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 30 | #include <haproxy/fd.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 31 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 32 | #include <haproxy/list.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 33 | #include <haproxy/listener.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 34 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 35 | #include <haproxy/protocol.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 36 | #include <haproxy/sock.h> |
Willy Tarreau | f172558 | 2020-08-28 15:30:11 +0200 | [diff] [blame] | 37 | #include <haproxy/sock_unix.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 38 | #include <haproxy/time.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 39 | #include <haproxy/tools.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 40 | #include <haproxy/version.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 42 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 43 | static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen); |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 44 | static int uxst_connect_server(struct connection *conn, int flags); |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 45 | static void uxst_add_listener(struct listener *listener, int port); |
Willy Tarreau | 3179489 | 2017-09-15 07:59:31 +0200 | [diff] [blame] | 46 | static int uxst_pause_listener(struct listener *l); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 47 | |
| 48 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 49 | static struct protocol proto_unix = { |
| 50 | .name = "unix_stream", |
Willy Tarreau | b0254cb | 2020-09-04 08:07:11 +0200 | [diff] [blame] | 51 | .fam = &proto_fam_unix, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 52 | .sock_domain = PF_UNIX, |
| 53 | .sock_type = SOCK_STREAM, |
| 54 | .sock_prot = 0, |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 55 | .accept = &listener_accept, |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 56 | .connect = &uxst_connect_server, |
Willy Tarreau | b3580b1 | 2020-09-01 10:26:22 +0200 | [diff] [blame] | 57 | .listen = uxst_bind_listener, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 58 | .enable_all = enable_all_listeners, |
| 59 | .disable_all = disable_all_listeners, |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 60 | .pause = uxst_pause_listener, |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 61 | .add = uxst_add_listener, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 62 | .listeners = LIST_HEAD_INIT(proto_unix.listeners), |
| 63 | .nb_listeners = 0, |
| 64 | }; |
| 65 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 66 | INITCALL1(STG_REGISTER, protocol_register, &proto_unix); |
| 67 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 68 | /******************************** |
| 69 | * 1) low-level socket functions |
| 70 | ********************************/ |
| 71 | |
| 72 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 73 | /******************************** |
| 74 | * 2) listener-oriented functions |
| 75 | ********************************/ |
| 76 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 77 | /* This function creates a UNIX socket associated to the listener. It changes |
| 78 | * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling. |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 79 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It |
| 80 | * may return a warning or an error message in <errmsg> if the message is at |
| 81 | * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if |
| 82 | * <errlen> is also zero. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 83 | */ |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 84 | static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 85 | { |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 86 | int fd, err; |
| 87 | int ready; |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 88 | socklen_t ready_len; |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 89 | char *msg = NULL; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 90 | |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 91 | err = ERR_NONE; |
| 92 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 93 | /* ensure we never return garbage */ |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 94 | if (errlen) |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 95 | *errmsg = 0; |
| 96 | |
| 97 | if (listener->state != LI_ASSIGNED) |
| 98 | return ERR_NONE; /* already bound */ |
Willy Tarreau | 0b91501 | 2020-09-01 10:47:07 +0200 | [diff] [blame] | 99 | |
Willy Tarreau | ad33acf | 2020-09-02 18:40:02 +0200 | [diff] [blame] | 100 | if (!(listener->rx.flags & RX_F_BOUND)) { |
| 101 | msg = "receiving socket not bound"; |
| 102 | goto uxst_return; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 105 | fd = listener->rx.fd; |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 106 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 107 | ready = 0; |
| 108 | ready_len = sizeof(ready); |
| 109 | if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1) |
| 110 | ready = 0; |
| 111 | |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 112 | if (!ready && /* only listen if not already done by external process */ |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 113 | listen(fd, listener_backlog(listener)) < 0) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 114 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 115 | msg = "cannot listen to UNIX socket"; |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 116 | goto uxst_close_return; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 119 | /* the socket is now listening */ |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 120 | listener->state = LI_LISTEN; |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 121 | return err; |
| 122 | |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 123 | uxst_close_return: |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 124 | close(fd); |
Willy Tarreau | ad33acf | 2020-09-02 18:40:02 +0200 | [diff] [blame] | 125 | uxst_return: |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 126 | if (msg && errlen) { |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 127 | const char *path = ((struct sockaddr_un *)&listener->rx.addr)->sun_path; |
| 128 | snprintf(errmsg, errlen, "%s [%s]", msg, path); |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 129 | } |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 130 | return err; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Willy Tarreau | 3228238 | 2017-09-15 07:44:44 +0200 | [diff] [blame] | 133 | /* Add <listener> to the list of unix stream listeners (port is ignored). The |
| 134 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 135 | * The number of listeners for the protocol is updated. |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 136 | * |
| 137 | * Must be called with proto_lock held. |
| 138 | * |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 139 | */ |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 140 | static void uxst_add_listener(struct listener *listener, int port) |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 141 | { |
| 142 | if (listener->state != LI_INIT) |
| 143 | return; |
| 144 | listener->state = LI_ASSIGNED; |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 145 | listener->rx.proto = &proto_unix; |
| 146 | LIST_ADDQ(&proto_unix.listeners, &listener->rx.proto_list); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 147 | proto_unix.nb_listeners++; |
| 148 | } |
| 149 | |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 150 | /* Pause a listener. Returns < 0 in case of failure, 0 if the listener |
| 151 | * was totally stopped, or > 0 if correctly paused. Nothing is done for |
| 152 | * plain unix sockets since currently it's the new process which handles |
| 153 | * the renaming. Abstract sockets are completely unbound. |
| 154 | */ |
Willy Tarreau | 3179489 | 2017-09-15 07:59:31 +0200 | [diff] [blame] | 155 | static int uxst_pause_listener(struct listener *l) |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 156 | { |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 157 | if (((struct sockaddr_un *)&l->rx.addr)->sun_path[0]) |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 158 | return 1; |
| 159 | |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 160 | /* Listener's lock already held. Call lockless version of |
| 161 | * unbind_listener. */ |
| 162 | do_unbind_listener(l, 1); |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * This function initiates a UNIX connection establishment to the target assigned |
Willy Tarreau | ca9f5a9 | 2019-07-17 16:40:37 +0200 | [diff] [blame] | 169 | * to connection <conn> using (si->{target,dst}). The source address is ignored |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 170 | * and will be selected by the system. conn->target may point either to a valid |
| 171 | * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and |
| 172 | * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating |
| 173 | * whether there are data waiting for being sent or not, in order to adjust data |
| 174 | * write polling and on some platforms. The <delack> argument is ignored. |
| 175 | * |
| 176 | * Note that a pending send_proxy message accounts for data. |
| 177 | * |
| 178 | * It can return one of : |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 179 | * - SF_ERR_NONE if everything's OK |
| 180 | * - SF_ERR_SRVTO if there are no more servers |
| 181 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 182 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 183 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 184 | * - SF_ERR_INTERNAL for any other purely internal errors |
Tim Düsterhus | 4896c44 | 2016-11-29 02:15:19 +0100 | [diff] [blame] | 185 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 186 | * |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 187 | * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 188 | * it's invalid and the caller has nothing to do. |
| 189 | */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 190 | static int uxst_connect_server(struct connection *conn, int flags) |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 191 | { |
| 192 | int fd; |
| 193 | struct server *srv; |
| 194 | struct proxy *be; |
| 195 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 196 | switch (obj_type(conn->target)) { |
| 197 | case OBJ_TYPE_PROXY: |
| 198 | be = objt_proxy(conn->target); |
| 199 | srv = NULL; |
| 200 | break; |
| 201 | case OBJ_TYPE_SERVER: |
| 202 | srv = objt_server(conn->target); |
| 203 | be = srv->proxy; |
| 204 | break; |
| 205 | default: |
| 206 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 207 | return SF_ERR_INTERNAL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 210 | if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 211 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 212 | |
| 213 | if (errno == ENFILE) { |
| 214 | conn->err_code = CO_ER_SYS_FDLIM; |
| 215 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 216 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 217 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 218 | } |
| 219 | else if (errno == EMFILE) { |
| 220 | conn->err_code = CO_ER_PROC_FDLIM; |
| 221 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 222 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 223 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 224 | } |
| 225 | else if (errno == ENOBUFS || errno == ENOMEM) { |
| 226 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 227 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 228 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 229 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 230 | } |
| 231 | else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) { |
| 232 | conn->err_code = CO_ER_NOPROTO; |
| 233 | } |
| 234 | else |
| 235 | conn->err_code = CO_ER_SOCK_ERR; |
| 236 | |
| 237 | /* this is a resource error */ |
| 238 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 239 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | if (fd >= global.maxsock) { |
| 243 | /* do not log anything there, it's a normal condition when this option |
| 244 | * is used to serialize connections to a server ! |
| 245 | */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 246 | ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 247 | close(fd); |
| 248 | conn->err_code = CO_ER_CONF_FDLIM; |
| 249 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 250 | return SF_ERR_PRXCOND; /* it is a configuration limit */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
| 254 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 255 | close(fd); |
| 256 | conn->err_code = CO_ER_SOCK_ERR; |
| 257 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 258 | return SF_ERR_INTERNAL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 259 | } |
| 260 | |
William Lallemand | c03eb01 | 2018-11-27 12:02:37 +0100 | [diff] [blame] | 261 | if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) { |
| 262 | ha_alert("Cannot set CLOEXEC on client socket.\n"); |
| 263 | close(fd); |
| 264 | conn->err_code = CO_ER_SOCK_ERR; |
| 265 | conn->flags |= CO_FL_ERROR; |
| 266 | return SF_ERR_INTERNAL; |
| 267 | } |
| 268 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 269 | /* if a send_proxy is there, there are data */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 270 | if (conn->send_proxy_ofs) |
| 271 | flags |= CONNECT_HAS_DATA; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 272 | |
| 273 | if (global.tune.server_sndbuf) |
| 274 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 275 | |
| 276 | if (global.tune.server_rcvbuf) |
| 277 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 278 | |
Willy Tarreau | ca9f5a9 | 2019-07-17 16:40:37 +0200 | [diff] [blame] | 279 | if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) { |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 280 | if (errno == EINPROGRESS || errno == EALREADY) { |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 281 | conn->flags |= CO_FL_WAIT_L4_CONN; |
| 282 | } |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 283 | else if (errno == EISCONN) { |
| 284 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 285 | } |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 286 | else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) { |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 287 | char *msg; |
| 288 | if (errno == EAGAIN || errno == EADDRNOTAVAIL) { |
Lukas Tribus | 9f256d4 | 2016-01-26 20:33:14 +0100 | [diff] [blame] | 289 | msg = "can't connect to destination unix socket, check backlog size on the server"; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 290 | conn->err_code = CO_ER_FREE_PORTS; |
| 291 | } |
| 292 | else { |
| 293 | msg = "local address already in use"; |
| 294 | conn->err_code = CO_ER_ADDR_INUSE; |
| 295 | } |
| 296 | |
| 297 | qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg); |
| 298 | close(fd); |
| 299 | send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg); |
| 300 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 301 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 302 | } |
| 303 | else if (errno == ETIMEDOUT) { |
| 304 | close(fd); |
| 305 | conn->err_code = CO_ER_SOCK_ERR; |
| 306 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 307 | return SF_ERR_SRVTO; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 308 | } |
| 309 | else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 310 | close(fd); |
| 311 | conn->err_code = CO_ER_SOCK_ERR; |
| 312 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 313 | return SF_ERR_SRVCL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 314 | } |
| 315 | } |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 316 | else { |
| 317 | /* connect() already succeeded, which is quite usual for unix |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 318 | * sockets. Let's avoid a second connect() probe to complete it. |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 319 | */ |
| 320 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 321 | } |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 322 | |
| 323 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 324 | |
| 325 | /* Prepare to send a few handshakes related to the on-wire protocol. */ |
| 326 | if (conn->send_proxy_ofs) |
| 327 | conn->flags |= CO_FL_SEND_PROXY; |
| 328 | |
| 329 | conn_ctrl_init(conn); /* registers the FD */ |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 330 | fdtab[fd].linger_risk = 0; /* no need to disable lingering */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 331 | |
Willy Tarreau | 4c69cff | 2020-03-04 16:38:00 +0100 | [diff] [blame] | 332 | if (conn->flags & CO_FL_WAIT_L4_CONN) { |
| 333 | fd_want_send(fd); |
| 334 | fd_cant_send(fd); |
Willy Tarreau | 8dbd1a2 | 2020-07-31 08:59:09 +0200 | [diff] [blame] | 335 | fd_cant_recv(fd); |
Willy Tarreau | 4c69cff | 2020-03-04 16:38:00 +0100 | [diff] [blame] | 336 | } |
Willy Tarreau | ccf3f6d | 2019-09-05 17:05:05 +0200 | [diff] [blame] | 337 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 338 | if (conn_xprt_init(conn) < 0) { |
Willy Tarreau | 8c82901 | 2017-10-05 18:02:11 +0200 | [diff] [blame] | 339 | conn_full_close(conn); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 340 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 341 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 344 | return SF_ERR_NONE; /* connection is OK */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 347 | /* |
| 348 | * Local variables: |
| 349 | * c-indent-level: 8 |
| 350 | * c-basic-offset: 8 |
| 351 | * End: |
| 352 | */ |