blob: 9b5a28d1f0f7e85d908dac3a639663c931257cbb [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
Willy Tarreau38ba6472020-08-27 08:16:52 +0200112 if (listener->rx.fd == -1)
113 listener->rx.fd = sock_find_compatible_fd(listener);
Willy Tarreau37159062020-08-27 07:48:42 +0200114 path = ((struct sockaddr_un *)&listener->rx.addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200115
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100116 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
117
Willy Tarreau40aa0702013-03-10 23:51:38 +0100118 /* if the listener already has an fd assigned, then we were offered the
119 * fd by an external process (most likely the parent), and we don't want
120 * to create a new socket. However we still want to set a few flags on
121 * the socket.
122 */
Willy Tarreau38ba6472020-08-27 08:16:52 +0200123 fd = listener->rx.fd;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100124 ext = (fd >= 0);
125 if (ext)
126 goto fd_ready;
127
Willy Tarreauccfccef2014-05-10 01:49:15 +0200128 if (path[0]) {
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100129 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200130 if (ret < 0 || ret >= sizeof(addr.sun_path)) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200131 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100132 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200133 goto err_return;
134 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200135
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100136 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
137 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200138 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100139 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200140 goto err_return;
141 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200142
Willy Tarreauccfccef2014-05-10 01:49:15 +0200143 /* 2. clean existing orphaned entries */
144 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200145 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200146 msg = "error when trying to unlink previous UNIX socket";
147 goto err_return;
148 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200149
Willy Tarreauccfccef2014-05-10 01:49:15 +0200150 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200151 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200152 msg = "error when trying to unlink previous UNIX socket";
153 goto err_return;
154 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200155
Willy Tarreauccfccef2014-05-10 01:49:15 +0200156 /* 3. backup existing socket */
157 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200158 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200159 msg = "error when trying to preserve previous UNIX socket";
160 goto err_return;
161 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200162
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200163 /* Note: this test is redundant with the snprintf one above and
164 * will never trigger, it's just added as the only way to shut
165 * gcc's painfully dumb warning about possibly truncated output
166 * during strncpy(). Don't move it above or smart gcc will not
167 * see it!
168 */
169 if (strlen(tempname) >= sizeof(addr.sun_path)) {
170 err |= ERR_FATAL | ERR_ALERT;
171 msg = "name too long for UNIX socket (limit usually 97)";
172 goto err_return;
173 }
174
Willy Tarreau719e07c2019-12-11 16:29:10 +0100175 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200176 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200177 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200178 else {
179 /* first char is zero, it's an abstract socket whose address
180 * is defined by all the bytes past this zero.
181 */
182 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
183 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200184 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200185
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100186 fd = socket(PF_UNIX, SOCK_STREAM, 0);
187 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200188 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100189 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200190 goto err_unlink_back;
191 }
192
Willy Tarreau40aa0702013-03-10 23:51:38 +0100193 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100194 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200195 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100196 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200197 goto err_unlink_temp;
198 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100199
200 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200201 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100202 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200203 goto err_unlink_temp;
204 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100205
Willy Tarreau40aa0702013-03-10 23:51:38 +0100206 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200207 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200208 if (errno == EADDRINUSE) {
209 /* the old process might still own it, let's retry */
210 err |= ERR_RETRYABLE | ERR_ALERT;
211 msg = "cannot listen to socket";
212 }
213 else {
214 err |= ERR_FATAL | ERR_ALERT;
215 msg = "cannot bind UNIX socket";
216 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200217 goto err_unlink_temp;
218 }
219
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100220 /* <uid> and <gid> different of -1 will be used to change the socket owner.
221 * If <mode> is not 0, it will be used to restrict access to the socket.
222 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200223 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100224 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200225 if (!ext && path[0] &&
Willy Tarreau6e459d72020-09-03 07:09:09 +0200226 (((listener->bind_conf->settings.ux.uid != -1 || listener->bind_conf->settings.ux.gid != -1) &&
227 (chown(tempname, listener->bind_conf->settings.ux.uid, listener->bind_conf->settings.ux.gid) == -1)) ||
228 (listener->bind_conf->settings.ux.mode != 0 && chmod(tempname, listener->bind_conf->settings.ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200229 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100230 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200231 goto err_unlink_temp;
232 }
233
Willy Tarreau40aa0702013-03-10 23:51:38 +0100234 ready = 0;
235 ready_len = sizeof(ready);
236 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
237 ready = 0;
238
239 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100240 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200241 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100242 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200243 goto err_unlink_temp;
244 }
245
Willy Tarreauccfccef2014-05-10 01:49:15 +0200246 /* Point of no return: we are ready, we'll switch the sockets. We don't
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500247 * fear losing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200248 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200249 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200250 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200251 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100252 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200253 goto err_rename;
254 }
255
Willy Tarreau68986ab2017-06-16 10:34:20 +0200256 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200257 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100258 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200259
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100260 /* the socket is now listening */
Willy Tarreau38ba6472020-08-27 08:16:52 +0200261 listener->rx.fd = fd;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100262 listener->state = LI_LISTEN;
263
Willy Tarreaua9786b62018-01-25 07:22:13 +0100264 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreaue26993c2020-09-03 07:18:55 +0200265 thread_mask(listener->bind_conf->settings.bind_thread) & all_threads_mask);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100266
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200267 /* for now, all regularly bound UNIX listeners are exportable */
268 if (!(listener->options & LI_O_INHERITED))
269 fdtab[fd].exported = 1;
270
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200271 return err;
272
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100273 err_rename:
274 ret = rename(backname, path);
275 if (ret < 0 && errno == ENOENT)
276 unlink(path);
277 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200278 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100279 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100280 close(fd);
281 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200282 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100283 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100284 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100285 if (msg && errlen) {
286 if (!ext)
287 snprintf(errmsg, errlen, "%s [%s]", msg, path);
288 else
289 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
290 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200291 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100292}
293
Willy Tarreau32282382017-09-15 07:44:44 +0200294/* Add <listener> to the list of unix stream listeners (port is ignored). The
295 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
296 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200297 *
298 * Must be called with proto_lock held.
299 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100300 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200301static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100302{
303 if (listener->state != LI_INIT)
304 return;
305 listener->state = LI_ASSIGNED;
306 listener->proto = &proto_unix;
307 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
308 proto_unix.nb_listeners++;
309}
310
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200311/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
312 * was totally stopped, or > 0 if correctly paused. Nothing is done for
313 * plain unix sockets since currently it's the new process which handles
314 * the renaming. Abstract sockets are completely unbound.
315 */
Willy Tarreau31794892017-09-15 07:59:31 +0200316static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200317{
Willy Tarreau37159062020-08-27 07:48:42 +0200318 if (((struct sockaddr_un *)&l->rx.addr)->sun_path[0])
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200319 return 1;
320
Christopher Faulet510c0d62018-03-16 10:04:47 +0100321 /* Listener's lock already held. Call lockless version of
322 * unbind_listener. */
323 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200324 return 0;
325}
326
Willy Tarreau47f48c42014-05-09 22:57:47 +0200327
328/*
329 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200330 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200331 * and will be selected by the system. conn->target may point either to a valid
332 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
333 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
334 * whether there are data waiting for being sent or not, in order to adjust data
335 * write polling and on some platforms. The <delack> argument is ignored.
336 *
337 * Note that a pending send_proxy message accounts for data.
338 *
339 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200340 * - SF_ERR_NONE if everything's OK
341 * - SF_ERR_SRVTO if there are no more servers
342 * - SF_ERR_SRVCL if the connection was refused by the server
343 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
344 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
345 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100346 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200347 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200348 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200349 * it's invalid and the caller has nothing to do.
350 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200351static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200352{
353 int fd;
354 struct server *srv;
355 struct proxy *be;
356
Willy Tarreau47f48c42014-05-09 22:57:47 +0200357 switch (obj_type(conn->target)) {
358 case OBJ_TYPE_PROXY:
359 be = objt_proxy(conn->target);
360 srv = NULL;
361 break;
362 case OBJ_TYPE_SERVER:
363 srv = objt_server(conn->target);
364 be = srv->proxy;
365 break;
366 default:
367 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200368 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200369 }
370
Willy Tarreau585744b2017-08-24 14:31:19 +0200371 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200372 qfprintf(stderr, "Cannot get a server socket.\n");
373
374 if (errno == ENFILE) {
375 conn->err_code = CO_ER_SYS_FDLIM;
376 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100377 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
378 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200379 }
380 else if (errno == EMFILE) {
381 conn->err_code = CO_ER_PROC_FDLIM;
382 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100383 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
384 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200385 }
386 else if (errno == ENOBUFS || errno == ENOMEM) {
387 conn->err_code = CO_ER_SYS_MEMLIM;
388 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100389 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
390 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200391 }
392 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
393 conn->err_code = CO_ER_NOPROTO;
394 }
395 else
396 conn->err_code = CO_ER_SOCK_ERR;
397
398 /* this is a resource error */
399 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200400 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200401 }
402
403 if (fd >= global.maxsock) {
404 /* do not log anything there, it's a normal condition when this option
405 * is used to serialize connections to a server !
406 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100407 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200408 close(fd);
409 conn->err_code = CO_ER_CONF_FDLIM;
410 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200411 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200412 }
413
414 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
415 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
416 close(fd);
417 conn->err_code = CO_ER_SOCK_ERR;
418 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200419 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200420 }
421
William Lallemandc03eb012018-11-27 12:02:37 +0100422 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
423 ha_alert("Cannot set CLOEXEC on client socket.\n");
424 close(fd);
425 conn->err_code = CO_ER_SOCK_ERR;
426 conn->flags |= CO_FL_ERROR;
427 return SF_ERR_INTERNAL;
428 }
429
Willy Tarreau47f48c42014-05-09 22:57:47 +0200430 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200431 if (conn->send_proxy_ofs)
432 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200433
434 if (global.tune.server_sndbuf)
435 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
436
437 if (global.tune.server_rcvbuf)
438 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
439
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200440 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100441 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200442 conn->flags |= CO_FL_WAIT_L4_CONN;
443 }
Willy Tarreau94841792017-01-25 14:27:38 +0100444 else if (errno == EISCONN) {
445 conn->flags &= ~CO_FL_WAIT_L4_CONN;
446 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200447 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200448 char *msg;
449 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100450 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200451 conn->err_code = CO_ER_FREE_PORTS;
452 }
453 else {
454 msg = "local address already in use";
455 conn->err_code = CO_ER_ADDR_INUSE;
456 }
457
458 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
459 close(fd);
460 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
461 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200462 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200463 }
464 else if (errno == ETIMEDOUT) {
465 close(fd);
466 conn->err_code = CO_ER_SOCK_ERR;
467 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200468 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200469 }
470 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
471 close(fd);
472 conn->err_code = CO_ER_SOCK_ERR;
473 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200474 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200475 }
476 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200477 else {
478 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100479 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200480 */
481 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200482 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200483
484 conn->flags |= CO_FL_ADDR_TO_SET;
485
486 /* Prepare to send a few handshakes related to the on-wire protocol. */
487 if (conn->send_proxy_ofs)
488 conn->flags |= CO_FL_SEND_PROXY;
489
490 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200491 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200492
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100493 if (conn->flags & CO_FL_WAIT_L4_CONN) {
494 fd_want_send(fd);
495 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200496 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100497 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200498
Willy Tarreau47f48c42014-05-09 22:57:47 +0200499 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200500 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200501 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200502 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200503 }
504
Willy Tarreaue7dff022015-04-03 01:14:29 +0200505 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200506}
507
Willy Tarreau92fb9832007-10-16 17:34:28 +0200508/*
509 * Local variables:
510 * c-indent-level: 8
511 * c-basic-offset: 8
512 * End:
513 */