blob: 24ab191dd57aa7193ef425e9769df08e1aa0eb0c [file] [log] [blame]
Willy Tarreau92fb9832007-10-16 17:34:28 +02001/*
2 * UNIX SOCK_STREAM protocol layer (uxst)
3 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreau92fb9832007-10-16 17:34:28 +02005 *
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 Tarreau92fb9832007-10-16 17:34:28 +020022#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020028#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020029#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/fd.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020031#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020032#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020033#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020034#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020035#include <haproxy/protocol.h>
Willy Tarreau18b7df72020-08-28 12:07:22 +020036#include <haproxy/sock.h>
Willy Tarreauf1725582020-08-28 15:30:11 +020037#include <haproxy/sock_unix.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020038#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020040#include <haproxy/version.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020041
Willy Tarreau92fb9832007-10-16 17:34:28 +020042
Emeric Bruncf20bf12010-10-22 16:06:11 +020043static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020044static int uxst_connect_server(struct connection *conn, int flags);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020045static void uxst_add_listener(struct listener *listener, int port);
Willy Tarreau31794892017-09-15 07:59:31 +020046static int uxst_pause_listener(struct listener *l);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010047
48/* Note: must not be declared <const> as its list will be overwritten */
49static struct protocol proto_unix = {
50 .name = "unix_stream",
51 .sock_domain = PF_UNIX,
52 .sock_type = SOCK_STREAM,
53 .sock_prot = 0,
54 .sock_family = AF_UNIX,
55 .sock_addrlen = sizeof(struct sockaddr_un),
56 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020057 .accept = &listener_accept,
Willy Tarreau47f48c42014-05-09 22:57:47 +020058 .connect = &uxst_connect_server,
Willy Tarreaub3580b12020-09-01 10:26:22 +020059 .listen = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010060 .enable_all = enable_all_listeners,
61 .disable_all = disable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020062 .get_src = sock_get_src,
63 .get_dst = sock_get_dst,
Willy Tarreaufd0e0082014-07-07 21:07:51 +020064 .pause = uxst_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020065 .add = uxst_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020066 .addrcmp = sock_unix_addrcmp,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010067 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
68 .nb_listeners = 0,
69};
70
Willy Tarreau0108d902018-11-25 19:14:37 +010071INITCALL1(STG_REGISTER, protocol_register, &proto_unix);
72
Willy Tarreaudabf2e22007-10-28 21:59:24 +010073/********************************
74 * 1) low-level socket functions
75 ********************************/
76
77
Cyril Bonté1f5848a2010-11-14 17:03:19 +010078/********************************
79 * 2) listener-oriented functions
80 ********************************/
81
Cyril Bonté1f5848a2010-11-14 17:03:19 +010082/* This function creates a UNIX socket associated to the listener. It changes
83 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +010084 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
85 * may return a warning or an error message in <errmsg> if the message is at
86 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
87 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +020088 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +010089static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +020090{
Cyril Bonté1f5848a2010-11-14 17:03:19 +010091 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +020092 char tempname[MAXPATHLEN];
93 char backname[MAXPATHLEN];
94 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +010095 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +010096 const char *path;
Willy Tarreau327ea5a2020-02-11 06:43:37 +010097 int maxpathlen;
Willy Tarreau40aa0702013-03-10 23:51:38 +010098 int ext, ready;
99 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200100 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100101 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200102
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200103 err = ERR_NONE;
104
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100105 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100106 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100107 *errmsg = 0;
108
109 if (listener->state != LI_ASSIGNED)
110 return ERR_NONE; /* already bound */
111
Willy Tarreau0b915012020-09-01 10:47:07 +0200112 if (listener->rx.flags & RX_F_BOUND)
113 goto bound;
114
Willy Tarreau38ba6472020-08-27 08:16:52 +0200115 if (listener->rx.fd == -1)
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200116 listener->rx.fd = sock_find_compatible_fd(&listener->rx);
Willy Tarreau37159062020-08-27 07:48:42 +0200117 path = ((struct sockaddr_un *)&listener->rx.addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200118
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100119 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
120
Willy Tarreau40aa0702013-03-10 23:51:38 +0100121 /* if the listener already has an fd assigned, then we were offered the
122 * fd by an external process (most likely the parent), and we don't want
123 * to create a new socket. However we still want to set a few flags on
124 * the socket.
125 */
Willy Tarreau38ba6472020-08-27 08:16:52 +0200126 fd = listener->rx.fd;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100127 ext = (fd >= 0);
128 if (ext)
129 goto fd_ready;
130
Willy Tarreauccfccef2014-05-10 01:49:15 +0200131 if (path[0]) {
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100132 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200133 if (ret < 0 || ret >= sizeof(addr.sun_path)) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200134 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100135 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200136 goto err_return;
137 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200138
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100139 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
140 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200141 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100142 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200143 goto err_return;
144 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200145
Willy Tarreauccfccef2014-05-10 01:49:15 +0200146 /* 2. clean existing orphaned entries */
147 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200148 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200149 msg = "error when trying to unlink previous UNIX socket";
150 goto err_return;
151 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200152
Willy Tarreauccfccef2014-05-10 01:49:15 +0200153 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200154 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200155 msg = "error when trying to unlink previous UNIX socket";
156 goto err_return;
157 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200158
Willy Tarreauccfccef2014-05-10 01:49:15 +0200159 /* 3. backup existing socket */
160 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200161 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200162 msg = "error when trying to preserve previous UNIX socket";
163 goto err_return;
164 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200165
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200166 /* Note: this test is redundant with the snprintf one above and
167 * will never trigger, it's just added as the only way to shut
168 * gcc's painfully dumb warning about possibly truncated output
169 * during strncpy(). Don't move it above or smart gcc will not
170 * see it!
171 */
172 if (strlen(tempname) >= sizeof(addr.sun_path)) {
173 err |= ERR_FATAL | ERR_ALERT;
174 msg = "name too long for UNIX socket (limit usually 97)";
175 goto err_return;
176 }
177
Willy Tarreau719e07c2019-12-11 16:29:10 +0100178 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200179 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200180 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200181 else {
182 /* first char is zero, it's an abstract socket whose address
183 * is defined by all the bytes past this zero.
184 */
185 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
186 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200187 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200188
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100189 fd = socket(PF_UNIX, SOCK_STREAM, 0);
190 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200191 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100192 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200193 goto err_unlink_back;
194 }
195
Willy Tarreau40aa0702013-03-10 23:51:38 +0100196 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100197 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200198 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100199 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200200 goto err_unlink_temp;
201 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100202
203 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200204 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100205 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200206 goto err_unlink_temp;
207 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100208
Willy Tarreau40aa0702013-03-10 23:51:38 +0100209 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200210 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200211 if (errno == EADDRINUSE) {
212 /* the old process might still own it, let's retry */
213 err |= ERR_RETRYABLE | ERR_ALERT;
214 msg = "cannot listen to socket";
215 }
216 else {
217 err |= ERR_FATAL | ERR_ALERT;
218 msg = "cannot bind UNIX socket";
219 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200220 goto err_unlink_temp;
221 }
222
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100223 /* <uid> and <gid> different of -1 will be used to change the socket owner.
224 * If <mode> is not 0, it will be used to restrict access to the socket.
225 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200226 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100227 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200228 if (!ext && path[0] &&
Willy Tarreau818a92e2020-09-03 07:50:19 +0200229 (((listener->rx.settings->ux.uid != -1 || listener->rx.settings->ux.gid != -1) &&
230 (chown(tempname, listener->rx.settings->ux.uid, listener->rx.settings->ux.gid) == -1)) ||
231 (listener->rx.settings->ux.mode != 0 && chmod(tempname, listener->rx.settings->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200232 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100233 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200234 goto err_unlink_temp;
235 }
Willy Tarreau0b915012020-09-01 10:47:07 +0200236 listener->rx.flags |= RX_F_BOUND;
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200237
Willy Tarreau0b915012020-09-01 10:47:07 +0200238 bound:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100239 ready = 0;
240 ready_len = sizeof(ready);
241 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
242 ready = 0;
243
244 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100245 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200246 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100247 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200248 goto err_unlink_temp;
249 }
250
Willy Tarreauccfccef2014-05-10 01:49:15 +0200251 /* Point of no return: we are ready, we'll switch the sockets. We don't
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500252 * fear losing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200253 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200254 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200255 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200256 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100257 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200258 goto err_rename;
259 }
260
Willy Tarreau68986ab2017-06-16 10:34:20 +0200261 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200262 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100263 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200264
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100265 /* the socket is now listening */
Willy Tarreau38ba6472020-08-27 08:16:52 +0200266 listener->rx.fd = fd;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100267 listener->state = LI_LISTEN;
268
Willy Tarreaub7436612020-08-28 19:51:44 +0200269 fd_insert(fd, listener, listener->rx.proto->accept,
Willy Tarreau818a92e2020-09-03 07:50:19 +0200270 thread_mask(listener->rx.settings->bind_thread) & all_threads_mask);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100271
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200272 /* for now, all regularly bound UNIX listeners are exportable */
Willy Tarreau43046fa2020-09-01 15:41:59 +0200273 if (!(listener->rx.flags & RX_F_INHERITED))
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200274 fdtab[fd].exported = 1;
275
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200276 return err;
277
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100278 err_rename:
279 ret = rename(backname, path);
280 if (ret < 0 && errno == ENOENT)
281 unlink(path);
282 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200283 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100284 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100285 close(fd);
286 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200287 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100288 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100289 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100290 if (msg && errlen) {
291 if (!ext)
292 snprintf(errmsg, errlen, "%s [%s]", msg, path);
293 else
294 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
295 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200296 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100297}
298
Willy Tarreau32282382017-09-15 07:44:44 +0200299/* Add <listener> to the list of unix stream listeners (port is ignored). The
300 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
301 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200302 *
303 * Must be called with proto_lock held.
304 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100305 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200306static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100307{
308 if (listener->state != LI_INIT)
309 return;
310 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200311 listener->rx.proto = &proto_unix;
312 LIST_ADDQ(&proto_unix.listeners, &listener->rx.proto_list);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100313 proto_unix.nb_listeners++;
314}
315
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200316/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
317 * was totally stopped, or > 0 if correctly paused. Nothing is done for
318 * plain unix sockets since currently it's the new process which handles
319 * the renaming. Abstract sockets are completely unbound.
320 */
Willy Tarreau31794892017-09-15 07:59:31 +0200321static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200322{
Willy Tarreau37159062020-08-27 07:48:42 +0200323 if (((struct sockaddr_un *)&l->rx.addr)->sun_path[0])
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200324 return 1;
325
Christopher Faulet510c0d62018-03-16 10:04:47 +0100326 /* Listener's lock already held. Call lockless version of
327 * unbind_listener. */
328 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200329 return 0;
330}
331
Willy Tarreau47f48c42014-05-09 22:57:47 +0200332
333/*
334 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200335 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200336 * and will be selected by the system. conn->target may point either to a valid
337 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
338 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
339 * whether there are data waiting for being sent or not, in order to adjust data
340 * write polling and on some platforms. The <delack> argument is ignored.
341 *
342 * Note that a pending send_proxy message accounts for data.
343 *
344 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200345 * - SF_ERR_NONE if everything's OK
346 * - SF_ERR_SRVTO if there are no more servers
347 * - SF_ERR_SRVCL if the connection was refused by the server
348 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
349 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
350 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100351 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200352 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200353 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200354 * it's invalid and the caller has nothing to do.
355 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200356static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200357{
358 int fd;
359 struct server *srv;
360 struct proxy *be;
361
Willy Tarreau47f48c42014-05-09 22:57:47 +0200362 switch (obj_type(conn->target)) {
363 case OBJ_TYPE_PROXY:
364 be = objt_proxy(conn->target);
365 srv = NULL;
366 break;
367 case OBJ_TYPE_SERVER:
368 srv = objt_server(conn->target);
369 be = srv->proxy;
370 break;
371 default:
372 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200373 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200374 }
375
Willy Tarreau585744b2017-08-24 14:31:19 +0200376 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200377 qfprintf(stderr, "Cannot get a server socket.\n");
378
379 if (errno == ENFILE) {
380 conn->err_code = CO_ER_SYS_FDLIM;
381 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100382 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
383 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200384 }
385 else if (errno == EMFILE) {
386 conn->err_code = CO_ER_PROC_FDLIM;
387 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100388 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
389 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200390 }
391 else if (errno == ENOBUFS || errno == ENOMEM) {
392 conn->err_code = CO_ER_SYS_MEMLIM;
393 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100394 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
395 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200396 }
397 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
398 conn->err_code = CO_ER_NOPROTO;
399 }
400 else
401 conn->err_code = CO_ER_SOCK_ERR;
402
403 /* this is a resource error */
404 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200405 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200406 }
407
408 if (fd >= global.maxsock) {
409 /* do not log anything there, it's a normal condition when this option
410 * is used to serialize connections to a server !
411 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100412 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200413 close(fd);
414 conn->err_code = CO_ER_CONF_FDLIM;
415 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200416 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200417 }
418
419 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
420 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
421 close(fd);
422 conn->err_code = CO_ER_SOCK_ERR;
423 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200424 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200425 }
426
William Lallemandc03eb012018-11-27 12:02:37 +0100427 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
428 ha_alert("Cannot set CLOEXEC on client socket.\n");
429 close(fd);
430 conn->err_code = CO_ER_SOCK_ERR;
431 conn->flags |= CO_FL_ERROR;
432 return SF_ERR_INTERNAL;
433 }
434
Willy Tarreau47f48c42014-05-09 22:57:47 +0200435 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200436 if (conn->send_proxy_ofs)
437 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200438
439 if (global.tune.server_sndbuf)
440 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
441
442 if (global.tune.server_rcvbuf)
443 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
444
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200445 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100446 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200447 conn->flags |= CO_FL_WAIT_L4_CONN;
448 }
Willy Tarreau94841792017-01-25 14:27:38 +0100449 else if (errno == EISCONN) {
450 conn->flags &= ~CO_FL_WAIT_L4_CONN;
451 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200452 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200453 char *msg;
454 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100455 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200456 conn->err_code = CO_ER_FREE_PORTS;
457 }
458 else {
459 msg = "local address already in use";
460 conn->err_code = CO_ER_ADDR_INUSE;
461 }
462
463 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
464 close(fd);
465 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
466 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200467 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200468 }
469 else if (errno == ETIMEDOUT) {
470 close(fd);
471 conn->err_code = CO_ER_SOCK_ERR;
472 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200473 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200474 }
475 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
476 close(fd);
477 conn->err_code = CO_ER_SOCK_ERR;
478 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200479 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200480 }
481 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200482 else {
483 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100484 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200485 */
486 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200487 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200488
489 conn->flags |= CO_FL_ADDR_TO_SET;
490
491 /* Prepare to send a few handshakes related to the on-wire protocol. */
492 if (conn->send_proxy_ofs)
493 conn->flags |= CO_FL_SEND_PROXY;
494
495 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200496 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200497
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100498 if (conn->flags & CO_FL_WAIT_L4_CONN) {
499 fd_want_send(fd);
500 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200501 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100502 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200503
Willy Tarreau47f48c42014-05-09 22:57:47 +0200504 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200505 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200506 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200507 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200508 }
509
Willy Tarreaue7dff022015-04-03 01:14:29 +0200510 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200511}
512
Willy Tarreau92fb9832007-10-16 17:34:28 +0200513/*
514 * Local variables:
515 * c-indent-level: 8
516 * c-basic-offset: 8
517 * End:
518 */