blob: 33768a1d5b0a5eb40ca5215c63d29d3a3e3da5cc [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,
Emeric Bruncf20bf12010-10-22 16:06:11 +020059 .bind = 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
Olivier Houchardf886e342017-04-05 22:24:59 +0200112 if (listener->fd == -1)
Willy Tarreau2d34a712020-08-28 16:49:41 +0200113 listener->fd = sock_find_compatible_fd(listener);
114
Willy Tarreau37159062020-08-27 07:48:42 +0200115 path = ((struct sockaddr_un *)&listener->rx.addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200116
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100117 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
118
Willy Tarreau40aa0702013-03-10 23:51:38 +0100119 /* if the listener already has an fd assigned, then we were offered the
120 * fd by an external process (most likely the parent), and we don't want
121 * to create a new socket. However we still want to set a few flags on
122 * the socket.
123 */
124 fd = listener->fd;
125 ext = (fd >= 0);
126 if (ext)
127 goto fd_ready;
128
Willy Tarreauccfccef2014-05-10 01:49:15 +0200129 if (path[0]) {
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100130 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200131 if (ret < 0 || ret >= sizeof(addr.sun_path)) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200132 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100133 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200134 goto err_return;
135 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200136
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100137 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
138 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200139 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100140 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200141 goto err_return;
142 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200143
Willy Tarreauccfccef2014-05-10 01:49:15 +0200144 /* 2. clean existing orphaned entries */
145 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200146 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200147 msg = "error when trying to unlink previous UNIX socket";
148 goto err_return;
149 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200150
Willy Tarreauccfccef2014-05-10 01:49:15 +0200151 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200152 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200153 msg = "error when trying to unlink previous UNIX socket";
154 goto err_return;
155 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200156
Willy Tarreauccfccef2014-05-10 01:49:15 +0200157 /* 3. backup existing socket */
158 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200159 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200160 msg = "error when trying to preserve previous UNIX socket";
161 goto err_return;
162 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200163
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200164 /* Note: this test is redundant with the snprintf one above and
165 * will never trigger, it's just added as the only way to shut
166 * gcc's painfully dumb warning about possibly truncated output
167 * during strncpy(). Don't move it above or smart gcc will not
168 * see it!
169 */
170 if (strlen(tempname) >= sizeof(addr.sun_path)) {
171 err |= ERR_FATAL | ERR_ALERT;
172 msg = "name too long for UNIX socket (limit usually 97)";
173 goto err_return;
174 }
175
Willy Tarreau719e07c2019-12-11 16:29:10 +0100176 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200177 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200178 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200179 else {
180 /* first char is zero, it's an abstract socket whose address
181 * is defined by all the bytes past this zero.
182 */
183 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
184 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200185 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200186
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100187 fd = socket(PF_UNIX, SOCK_STREAM, 0);
188 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200189 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100190 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200191 goto err_unlink_back;
192 }
193
Willy Tarreau40aa0702013-03-10 23:51:38 +0100194 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100195 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200196 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100197 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200198 goto err_unlink_temp;
199 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100200
201 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200202 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100203 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200204 goto err_unlink_temp;
205 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100206
Willy Tarreau40aa0702013-03-10 23:51:38 +0100207 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200208 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200209 if (errno == EADDRINUSE) {
210 /* the old process might still own it, let's retry */
211 err |= ERR_RETRYABLE | ERR_ALERT;
212 msg = "cannot listen to socket";
213 }
214 else {
215 err |= ERR_FATAL | ERR_ALERT;
216 msg = "cannot bind UNIX socket";
217 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200218 goto err_unlink_temp;
219 }
220
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100221 /* <uid> and <gid> different of -1 will be used to change the socket owner.
222 * If <mode> is not 0, it will be used to restrict access to the socket.
223 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200224 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100225 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200226 if (!ext && path[0] &&
Willy Tarreau6e459d72020-09-03 07:09:09 +0200227 (((listener->bind_conf->settings.ux.uid != -1 || listener->bind_conf->settings.ux.gid != -1) &&
228 (chown(tempname, listener->bind_conf->settings.ux.uid, listener->bind_conf->settings.ux.gid) == -1)) ||
229 (listener->bind_conf->settings.ux.mode != 0 && chmod(tempname, listener->bind_conf->settings.ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200230 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100231 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200232 goto err_unlink_temp;
233 }
234
Willy Tarreau40aa0702013-03-10 23:51:38 +0100235 ready = 0;
236 ready_len = sizeof(ready);
237 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
238 ready = 0;
239
240 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100241 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200242 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100243 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200244 goto err_unlink_temp;
245 }
246
Willy Tarreauccfccef2014-05-10 01:49:15 +0200247 /* Point of no return: we are ready, we'll switch the sockets. We don't
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500248 * fear losing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200249 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200250 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200251 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200252 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100253 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200254 goto err_rename;
255 }
256
Willy Tarreau68986ab2017-06-16 10:34:20 +0200257 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200258 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100259 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200260
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100261 /* the socket is now listening */
262 listener->fd = fd;
263 listener->state = LI_LISTEN;
264
Willy Tarreaua9786b62018-01-25 07:22:13 +0100265 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreaue26993c2020-09-03 07:18:55 +0200266 thread_mask(listener->bind_conf->settings.bind_thread) & all_threads_mask);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100267
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200268 /* for now, all regularly bound UNIX listeners are exportable */
269 if (!(listener->options & LI_O_INHERITED))
270 fdtab[fd].exported = 1;
271
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200272 return err;
273
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100274 err_rename:
275 ret = rename(backname, path);
276 if (ret < 0 && errno == ENOENT)
277 unlink(path);
278 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200279 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100280 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100281 close(fd);
282 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200283 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100284 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100285 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100286 if (msg && errlen) {
287 if (!ext)
288 snprintf(errmsg, errlen, "%s [%s]", msg, path);
289 else
290 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
291 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200292 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100293}
294
Willy Tarreau32282382017-09-15 07:44:44 +0200295/* Add <listener> to the list of unix stream listeners (port is ignored). The
296 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
297 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200298 *
299 * Must be called with proto_lock held.
300 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100301 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200302static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100303{
304 if (listener->state != LI_INIT)
305 return;
306 listener->state = LI_ASSIGNED;
307 listener->proto = &proto_unix;
308 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
309 proto_unix.nb_listeners++;
310}
311
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200312/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
313 * was totally stopped, or > 0 if correctly paused. Nothing is done for
314 * plain unix sockets since currently it's the new process which handles
315 * the renaming. Abstract sockets are completely unbound.
316 */
Willy Tarreau31794892017-09-15 07:59:31 +0200317static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200318{
Willy Tarreau37159062020-08-27 07:48:42 +0200319 if (((struct sockaddr_un *)&l->rx.addr)->sun_path[0])
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200320 return 1;
321
Christopher Faulet510c0d62018-03-16 10:04:47 +0100322 /* Listener's lock already held. Call lockless version of
323 * unbind_listener. */
324 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200325 return 0;
326}
327
Willy Tarreau47f48c42014-05-09 22:57:47 +0200328
329/*
330 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200331 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200332 * and will be selected by the system. conn->target may point either to a valid
333 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
334 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
335 * whether there are data waiting for being sent or not, in order to adjust data
336 * write polling and on some platforms. The <delack> argument is ignored.
337 *
338 * Note that a pending send_proxy message accounts for data.
339 *
340 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200341 * - SF_ERR_NONE if everything's OK
342 * - SF_ERR_SRVTO if there are no more servers
343 * - SF_ERR_SRVCL if the connection was refused by the server
344 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
345 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
346 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100347 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200348 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200349 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200350 * it's invalid and the caller has nothing to do.
351 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200352static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200353{
354 int fd;
355 struct server *srv;
356 struct proxy *be;
357
Willy Tarreau47f48c42014-05-09 22:57:47 +0200358 switch (obj_type(conn->target)) {
359 case OBJ_TYPE_PROXY:
360 be = objt_proxy(conn->target);
361 srv = NULL;
362 break;
363 case OBJ_TYPE_SERVER:
364 srv = objt_server(conn->target);
365 be = srv->proxy;
366 break;
367 default:
368 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200369 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200370 }
371
Willy Tarreau585744b2017-08-24 14:31:19 +0200372 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200373 qfprintf(stderr, "Cannot get a server socket.\n");
374
375 if (errno == ENFILE) {
376 conn->err_code = CO_ER_SYS_FDLIM;
377 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100378 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
379 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200380 }
381 else if (errno == EMFILE) {
382 conn->err_code = CO_ER_PROC_FDLIM;
383 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100384 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
385 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200386 }
387 else if (errno == ENOBUFS || errno == ENOMEM) {
388 conn->err_code = CO_ER_SYS_MEMLIM;
389 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100390 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
391 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200392 }
393 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
394 conn->err_code = CO_ER_NOPROTO;
395 }
396 else
397 conn->err_code = CO_ER_SOCK_ERR;
398
399 /* this is a resource error */
400 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200401 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200402 }
403
404 if (fd >= global.maxsock) {
405 /* do not log anything there, it's a normal condition when this option
406 * is used to serialize connections to a server !
407 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100408 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200409 close(fd);
410 conn->err_code = CO_ER_CONF_FDLIM;
411 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200412 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200413 }
414
415 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
416 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
417 close(fd);
418 conn->err_code = CO_ER_SOCK_ERR;
419 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200420 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200421 }
422
William Lallemandc03eb012018-11-27 12:02:37 +0100423 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
424 ha_alert("Cannot set CLOEXEC on client socket.\n");
425 close(fd);
426 conn->err_code = CO_ER_SOCK_ERR;
427 conn->flags |= CO_FL_ERROR;
428 return SF_ERR_INTERNAL;
429 }
430
Willy Tarreau47f48c42014-05-09 22:57:47 +0200431 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200432 if (conn->send_proxy_ofs)
433 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200434
435 if (global.tune.server_sndbuf)
436 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
437
438 if (global.tune.server_rcvbuf)
439 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
440
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200441 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100442 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200443 conn->flags |= CO_FL_WAIT_L4_CONN;
444 }
Willy Tarreau94841792017-01-25 14:27:38 +0100445 else if (errno == EISCONN) {
446 conn->flags &= ~CO_FL_WAIT_L4_CONN;
447 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200448 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200449 char *msg;
450 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100451 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200452 conn->err_code = CO_ER_FREE_PORTS;
453 }
454 else {
455 msg = "local address already in use";
456 conn->err_code = CO_ER_ADDR_INUSE;
457 }
458
459 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
460 close(fd);
461 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
462 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200463 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200464 }
465 else if (errno == ETIMEDOUT) {
466 close(fd);
467 conn->err_code = CO_ER_SOCK_ERR;
468 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200469 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200470 }
471 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
472 close(fd);
473 conn->err_code = CO_ER_SOCK_ERR;
474 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200475 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200476 }
477 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200478 else {
479 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100480 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200481 */
482 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200483 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200484
485 conn->flags |= CO_FL_ADDR_TO_SET;
486
487 /* Prepare to send a few handshakes related to the on-wire protocol. */
488 if (conn->send_proxy_ofs)
489 conn->flags |= CO_FL_SEND_PROXY;
490
491 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200492 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200493
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100494 if (conn->flags & CO_FL_WAIT_L4_CONN) {
495 fd_want_send(fd);
496 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200497 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100498 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200499
Willy Tarreau47f48c42014-05-09 22:57:47 +0200500 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200501 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200502 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200503 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200504 }
505
Willy Tarreaue7dff022015-04-03 01:14:29 +0200506 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200507}
508
Willy Tarreau92fb9832007-10-16 17:34:28 +0200509/*
510 * Local variables:
511 * c-indent-level: 8
512 * c-basic-offset: 8
513 * End:
514 */