blob: ca8423076603a8c6f3178dbbbd5b966bce27041c [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 Tarreau1e0a8602020-09-02 17:14:29 +020059 .bind = sock_unix_bind_receiver,
Willy Tarreaub3580b12020-09-01 10:26:22 +020060 .listen = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010061 .enable_all = enable_all_listeners,
62 .disable_all = disable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020063 .get_src = sock_get_src,
64 .get_dst = sock_get_dst,
Willy Tarreaufd0e0082014-07-07 21:07:51 +020065 .pause = uxst_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020066 .add = uxst_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020067 .addrcmp = sock_unix_addrcmp,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010068 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
69 .nb_listeners = 0,
70};
71
Willy Tarreau0108d902018-11-25 19:14:37 +010072INITCALL1(STG_REGISTER, protocol_register, &proto_unix);
73
Willy Tarreaudabf2e22007-10-28 21:59:24 +010074/********************************
75 * 1) low-level socket functions
76 ********************************/
77
78
Cyril Bonté1f5848a2010-11-14 17:03:19 +010079/********************************
80 * 2) listener-oriented functions
81 ********************************/
82
Cyril Bonté1f5848a2010-11-14 17:03:19 +010083/* This function creates a UNIX socket associated to the listener. It changes
84 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +010085 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
86 * may return a warning or an error message in <errmsg> if the message is at
87 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
88 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +020089 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +010090static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +020091{
Cyril Bonté1f5848a2010-11-14 17:03:19 +010092 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +020093 char tempname[MAXPATHLEN];
94 char backname[MAXPATHLEN];
95 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +010096 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +010097 const char *path;
Willy Tarreau327ea5a2020-02-11 06:43:37 +010098 int maxpathlen;
Willy Tarreau40aa0702013-03-10 23:51:38 +010099 int ext, ready;
100 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200101 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100102 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200103
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200104 err = ERR_NONE;
105
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100106 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100107 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100108 *errmsg = 0;
109
110 if (listener->state != LI_ASSIGNED)
111 return ERR_NONE; /* already bound */
112
Willy Tarreau0b915012020-09-01 10:47:07 +0200113 if (listener->rx.flags & RX_F_BOUND)
114 goto bound;
115
Willy Tarreau38ba6472020-08-27 08:16:52 +0200116 if (listener->rx.fd == -1)
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200117 listener->rx.fd = sock_find_compatible_fd(&listener->rx);
Willy Tarreau37159062020-08-27 07:48:42 +0200118 path = ((struct sockaddr_un *)&listener->rx.addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200119
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100120 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
121
Willy Tarreau40aa0702013-03-10 23:51:38 +0100122 /* if the listener already has an fd assigned, then we were offered the
123 * fd by an external process (most likely the parent), and we don't want
124 * to create a new socket. However we still want to set a few flags on
125 * the socket.
126 */
Willy Tarreau38ba6472020-08-27 08:16:52 +0200127 fd = listener->rx.fd;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100128 ext = (fd >= 0);
129 if (ext)
130 goto fd_ready;
131
Willy Tarreauccfccef2014-05-10 01:49:15 +0200132 if (path[0]) {
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100133 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200134 if (ret < 0 || ret >= sizeof(addr.sun_path)) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200135 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100136 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200137 goto err_return;
138 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200139
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100140 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
141 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200142 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100143 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200144 goto err_return;
145 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200146
Willy Tarreauccfccef2014-05-10 01:49:15 +0200147 /* 2. clean existing orphaned entries */
148 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200149 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200150 msg = "error when trying to unlink previous UNIX socket";
151 goto err_return;
152 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200153
Willy Tarreauccfccef2014-05-10 01:49:15 +0200154 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200155 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200156 msg = "error when trying to unlink previous UNIX socket";
157 goto err_return;
158 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200159
Willy Tarreauccfccef2014-05-10 01:49:15 +0200160 /* 3. backup existing socket */
161 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200162 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200163 msg = "error when trying to preserve previous UNIX socket";
164 goto err_return;
165 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200166
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200167 /* Note: this test is redundant with the snprintf one above and
168 * will never trigger, it's just added as the only way to shut
169 * gcc's painfully dumb warning about possibly truncated output
170 * during strncpy(). Don't move it above or smart gcc will not
171 * see it!
172 */
173 if (strlen(tempname) >= sizeof(addr.sun_path)) {
174 err |= ERR_FATAL | ERR_ALERT;
175 msg = "name too long for UNIX socket (limit usually 97)";
176 goto err_return;
177 }
178
Willy Tarreau719e07c2019-12-11 16:29:10 +0100179 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200180 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200181 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200182 else {
183 /* first char is zero, it's an abstract socket whose address
184 * is defined by all the bytes past this zero.
185 */
186 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
187 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200188 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200189
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100190 fd = socket(PF_UNIX, SOCK_STREAM, 0);
191 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200192 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100193 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200194 goto err_unlink_back;
195 }
196
Willy Tarreau40aa0702013-03-10 23:51:38 +0100197 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100198 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200199 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100200 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200201 goto err_unlink_temp;
202 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100203
204 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200205 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100206 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200207 goto err_unlink_temp;
208 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100209
Willy Tarreau40aa0702013-03-10 23:51:38 +0100210 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200211 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200212 if (errno == EADDRINUSE) {
213 /* the old process might still own it, let's retry */
214 err |= ERR_RETRYABLE | ERR_ALERT;
215 msg = "cannot listen to socket";
216 }
217 else {
218 err |= ERR_FATAL | ERR_ALERT;
219 msg = "cannot bind UNIX socket";
220 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200221 goto err_unlink_temp;
222 }
223
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100224 /* <uid> and <gid> different of -1 will be used to change the socket owner.
225 * If <mode> is not 0, it will be used to restrict access to the socket.
226 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200227 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100228 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200229 if (!ext && path[0] &&
Willy Tarreau818a92e2020-09-03 07:50:19 +0200230 (((listener->rx.settings->ux.uid != -1 || listener->rx.settings->ux.gid != -1) &&
231 (chown(tempname, listener->rx.settings->ux.uid, listener->rx.settings->ux.gid) == -1)) ||
232 (listener->rx.settings->ux.mode != 0 && chmod(tempname, listener->rx.settings->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200233 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100234 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200235 goto err_unlink_temp;
236 }
Willy Tarreau0b915012020-09-01 10:47:07 +0200237 listener->rx.flags |= RX_F_BOUND;
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200238
Willy Tarreau0b915012020-09-01 10:47:07 +0200239 bound:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100240 ready = 0;
241 ready_len = sizeof(ready);
242 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
243 ready = 0;
244
245 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100246 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200247 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100248 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200249 goto err_unlink_temp;
250 }
251
Willy Tarreauccfccef2014-05-10 01:49:15 +0200252 /* Point of no return: we are ready, we'll switch the sockets. We don't
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500253 * fear losing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200254 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200255 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200256 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200257 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100258 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200259 goto err_rename;
260 }
261
Willy Tarreau68986ab2017-06-16 10:34:20 +0200262 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200263 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100264 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200265
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100266 /* the socket is now listening */
Willy Tarreau38ba6472020-08-27 08:16:52 +0200267 listener->rx.fd = fd;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100268 listener->state = LI_LISTEN;
269
Willy Tarreaub7436612020-08-28 19:51:44 +0200270 fd_insert(fd, listener, listener->rx.proto->accept,
Willy Tarreau818a92e2020-09-03 07:50:19 +0200271 thread_mask(listener->rx.settings->bind_thread) & all_threads_mask);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100272
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200273 /* for now, all regularly bound UNIX listeners are exportable */
Willy Tarreau43046fa2020-09-01 15:41:59 +0200274 if (!(listener->rx.flags & RX_F_INHERITED))
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200275 fdtab[fd].exported = 1;
276
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200277 return err;
278
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100279 err_rename:
280 ret = rename(backname, path);
281 if (ret < 0 && errno == ENOENT)
282 unlink(path);
283 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200284 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100285 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100286 close(fd);
287 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200288 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100289 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100290 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100291 if (msg && errlen) {
292 if (!ext)
293 snprintf(errmsg, errlen, "%s [%s]", msg, path);
294 else
295 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
296 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200297 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100298}
299
Willy Tarreau32282382017-09-15 07:44:44 +0200300/* Add <listener> to the list of unix stream listeners (port is ignored). The
301 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
302 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200303 *
304 * Must be called with proto_lock held.
305 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100306 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200307static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100308{
309 if (listener->state != LI_INIT)
310 return;
311 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200312 listener->rx.proto = &proto_unix;
313 LIST_ADDQ(&proto_unix.listeners, &listener->rx.proto_list);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100314 proto_unix.nb_listeners++;
315}
316
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200317/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
318 * was totally stopped, or > 0 if correctly paused. Nothing is done for
319 * plain unix sockets since currently it's the new process which handles
320 * the renaming. Abstract sockets are completely unbound.
321 */
Willy Tarreau31794892017-09-15 07:59:31 +0200322static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200323{
Willy Tarreau37159062020-08-27 07:48:42 +0200324 if (((struct sockaddr_un *)&l->rx.addr)->sun_path[0])
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200325 return 1;
326
Christopher Faulet510c0d62018-03-16 10:04:47 +0100327 /* Listener's lock already held. Call lockless version of
328 * unbind_listener. */
329 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200330 return 0;
331}
332
Willy Tarreau47f48c42014-05-09 22:57:47 +0200333
334/*
335 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200336 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200337 * and will be selected by the system. conn->target may point either to a valid
338 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
339 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
340 * whether there are data waiting for being sent or not, in order to adjust data
341 * write polling and on some platforms. The <delack> argument is ignored.
342 *
343 * Note that a pending send_proxy message accounts for data.
344 *
345 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200346 * - SF_ERR_NONE if everything's OK
347 * - SF_ERR_SRVTO if there are no more servers
348 * - SF_ERR_SRVCL if the connection was refused by the server
349 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
350 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
351 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100352 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200353 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200354 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200355 * it's invalid and the caller has nothing to do.
356 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200357static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200358{
359 int fd;
360 struct server *srv;
361 struct proxy *be;
362
Willy Tarreau47f48c42014-05-09 22:57:47 +0200363 switch (obj_type(conn->target)) {
364 case OBJ_TYPE_PROXY:
365 be = objt_proxy(conn->target);
366 srv = NULL;
367 break;
368 case OBJ_TYPE_SERVER:
369 srv = objt_server(conn->target);
370 be = srv->proxy;
371 break;
372 default:
373 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200374 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200375 }
376
Willy Tarreau585744b2017-08-24 14:31:19 +0200377 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200378 qfprintf(stderr, "Cannot get a server socket.\n");
379
380 if (errno == ENFILE) {
381 conn->err_code = CO_ER_SYS_FDLIM;
382 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100383 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
384 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200385 }
386 else if (errno == EMFILE) {
387 conn->err_code = CO_ER_PROC_FDLIM;
388 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100389 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
390 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200391 }
392 else if (errno == ENOBUFS || errno == ENOMEM) {
393 conn->err_code = CO_ER_SYS_MEMLIM;
394 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100395 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
396 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200397 }
398 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
399 conn->err_code = CO_ER_NOPROTO;
400 }
401 else
402 conn->err_code = CO_ER_SOCK_ERR;
403
404 /* this is a resource error */
405 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200406 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200407 }
408
409 if (fd >= global.maxsock) {
410 /* do not log anything there, it's a normal condition when this option
411 * is used to serialize connections to a server !
412 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100413 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200414 close(fd);
415 conn->err_code = CO_ER_CONF_FDLIM;
416 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200417 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200418 }
419
420 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
421 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
422 close(fd);
423 conn->err_code = CO_ER_SOCK_ERR;
424 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200425 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200426 }
427
William Lallemandc03eb012018-11-27 12:02:37 +0100428 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
429 ha_alert("Cannot set CLOEXEC on client socket.\n");
430 close(fd);
431 conn->err_code = CO_ER_SOCK_ERR;
432 conn->flags |= CO_FL_ERROR;
433 return SF_ERR_INTERNAL;
434 }
435
Willy Tarreau47f48c42014-05-09 22:57:47 +0200436 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200437 if (conn->send_proxy_ofs)
438 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200439
440 if (global.tune.server_sndbuf)
441 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
442
443 if (global.tune.server_rcvbuf)
444 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
445
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200446 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100447 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200448 conn->flags |= CO_FL_WAIT_L4_CONN;
449 }
Willy Tarreau94841792017-01-25 14:27:38 +0100450 else if (errno == EISCONN) {
451 conn->flags &= ~CO_FL_WAIT_L4_CONN;
452 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200453 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200454 char *msg;
455 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100456 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200457 conn->err_code = CO_ER_FREE_PORTS;
458 }
459 else {
460 msg = "local address already in use";
461 conn->err_code = CO_ER_ADDR_INUSE;
462 }
463
464 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
465 close(fd);
466 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
467 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200468 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200469 }
470 else if (errno == ETIMEDOUT) {
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_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200475 }
476 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
477 close(fd);
478 conn->err_code = CO_ER_SOCK_ERR;
479 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200480 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200481 }
482 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200483 else {
484 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100485 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200486 */
487 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200488 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200489
490 conn->flags |= CO_FL_ADDR_TO_SET;
491
492 /* Prepare to send a few handshakes related to the on-wire protocol. */
493 if (conn->send_proxy_ofs)
494 conn->flags |= CO_FL_SEND_PROXY;
495
496 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200497 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200498
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100499 if (conn->flags & CO_FL_WAIT_L4_CONN) {
500 fd_want_send(fd);
501 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200502 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100503 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200504
Willy Tarreau47f48c42014-05-09 22:57:47 +0200505 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200506 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200507 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200508 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200509 }
510
Willy Tarreaue7dff022015-04-03 01:14:29 +0200511 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200512}
513
Willy Tarreau92fb9832007-10-16 17:34:28 +0200514/*
515 * Local variables:
516 * c-indent-level: 8
517 * c-basic-offset: 8
518 * End:
519 */