blob: f0cf99988a5e3a19820457bb5aea51f132378e71 [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);
44static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010045static int uxst_unbind_listeners(struct protocol *proto);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020046static int uxst_connect_server(struct connection *conn, int flags);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020047static void uxst_add_listener(struct listener *listener, int port);
Willy Tarreau31794892017-09-15 07:59:31 +020048static int uxst_pause_listener(struct listener *l);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010049
50/* Note: must not be declared <const> as its list will be overwritten */
51static struct protocol proto_unix = {
52 .name = "unix_stream",
53 .sock_domain = PF_UNIX,
54 .sock_type = SOCK_STREAM,
55 .sock_prot = 0,
56 .sock_family = AF_UNIX,
57 .sock_addrlen = sizeof(struct sockaddr_un),
58 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020059 .accept = &listener_accept,
Willy Tarreau47f48c42014-05-09 22:57:47 +020060 .connect = &uxst_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020061 .bind = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010062 .bind_all = uxst_bind_listeners,
63 .unbind_all = uxst_unbind_listeners,
64 .enable_all = enable_all_listeners,
65 .disable_all = disable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020066 .get_src = sock_get_src,
67 .get_dst = sock_get_dst,
Willy Tarreaufd0e0082014-07-07 21:07:51 +020068 .pause = uxst_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020069 .add = uxst_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020070 .addrcmp = sock_unix_addrcmp,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010071 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
72 .nb_listeners = 0,
73};
74
Willy Tarreau0108d902018-11-25 19:14:37 +010075INITCALL1(STG_REGISTER, protocol_register, &proto_unix);
76
Willy Tarreaudabf2e22007-10-28 21:59:24 +010077/********************************
78 * 1) low-level socket functions
79 ********************************/
80
81
Cyril Bonté1f5848a2010-11-14 17:03:19 +010082/********************************
83 * 2) listener-oriented functions
84 ********************************/
85
86
Olivier Houchardf886e342017-04-05 22:24:59 +020087static int uxst_find_compatible_fd(struct listener *l)
88{
89 struct xfer_sock_list *xfer_sock = xfer_sock_list;
90 int ret = -1;
91
92 while (xfer_sock) {
Olivier Houchardf886e342017-04-05 22:24:59 +020093 /*
94 * The bound socket's path as returned by getsockaddr
95 * will be the temporary name <sockname>.XXXXX.tmp,
96 * so we can't just compare the two names
97 */
Willy Tarreauf1725582020-08-28 15:30:11 +020098 if (!l->proto->addrcmp(&xfer_sock->addr, &l->addr))
Olivier Houchardb4dd15b2018-06-06 18:34:34 +020099 break;
Olivier Houchardf886e342017-04-05 22:24:59 +0200100 xfer_sock = xfer_sock->next;
101 }
102 if (xfer_sock != NULL) {
103 ret = xfer_sock->fd;
104 if (xfer_sock == xfer_sock_list)
105 xfer_sock_list = xfer_sock->next;
106 if (xfer_sock->prev)
107 xfer_sock->prev->next = xfer_sock->next;
108 if (xfer_sock->next)
Olivier Houchardec9516a2018-03-08 18:25:49 +0100109 xfer_sock->next->prev = xfer_sock->prev;
Olivier Houchardf886e342017-04-05 22:24:59 +0200110 free(xfer_sock);
111 }
112 return ret;
113
114}
115
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100116/* This function creates a UNIX socket associated to the listener. It changes
117 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100118 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
119 * may return a warning or an error message in <errmsg> if the message is at
120 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
121 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200122 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100123static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200124{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100125 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200126 char tempname[MAXPATHLEN];
127 char backname[MAXPATHLEN];
128 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100129 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100130 const char *path;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100131 int maxpathlen;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100132 int ext, ready;
133 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200134 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100135 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200136
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200137 err = ERR_NONE;
138
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100139 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100140 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100141 *errmsg = 0;
142
143 if (listener->state != LI_ASSIGNED)
144 return ERR_NONE; /* already bound */
145
Olivier Houchardf886e342017-04-05 22:24:59 +0200146 if (listener->fd == -1)
147 listener->fd = uxst_find_compatible_fd(listener);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100148 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200149
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100150 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
151
Willy Tarreau40aa0702013-03-10 23:51:38 +0100152 /* if the listener already has an fd assigned, then we were offered the
153 * fd by an external process (most likely the parent), and we don't want
154 * to create a new socket. However we still want to set a few flags on
155 * the socket.
156 */
157 fd = listener->fd;
158 ext = (fd >= 0);
159 if (ext)
160 goto fd_ready;
161
Willy Tarreauccfccef2014-05-10 01:49:15 +0200162 if (path[0]) {
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100163 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200164 if (ret < 0 || ret >= sizeof(addr.sun_path)) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200165 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100166 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200167 goto err_return;
168 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200169
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100170 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
171 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200172 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100173 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200174 goto err_return;
175 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200176
Willy Tarreauccfccef2014-05-10 01:49:15 +0200177 /* 2. clean existing orphaned entries */
178 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200179 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200180 msg = "error when trying to unlink previous UNIX socket";
181 goto err_return;
182 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200183
Willy Tarreauccfccef2014-05-10 01:49:15 +0200184 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200185 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200186 msg = "error when trying to unlink previous UNIX socket";
187 goto err_return;
188 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200189
Willy Tarreauccfccef2014-05-10 01:49:15 +0200190 /* 3. backup existing socket */
191 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200192 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200193 msg = "error when trying to preserve previous UNIX socket";
194 goto err_return;
195 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200196
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200197 /* Note: this test is redundant with the snprintf one above and
198 * will never trigger, it's just added as the only way to shut
199 * gcc's painfully dumb warning about possibly truncated output
200 * during strncpy(). Don't move it above or smart gcc will not
201 * see it!
202 */
203 if (strlen(tempname) >= sizeof(addr.sun_path)) {
204 err |= ERR_FATAL | ERR_ALERT;
205 msg = "name too long for UNIX socket (limit usually 97)";
206 goto err_return;
207 }
208
Willy Tarreau719e07c2019-12-11 16:29:10 +0100209 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200210 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200211 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200212 else {
213 /* first char is zero, it's an abstract socket whose address
214 * is defined by all the bytes past this zero.
215 */
216 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
217 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200218 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200219
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100220 fd = socket(PF_UNIX, SOCK_STREAM, 0);
221 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200222 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100223 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200224 goto err_unlink_back;
225 }
226
Willy Tarreau40aa0702013-03-10 23:51:38 +0100227 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100228 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200229 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100230 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200231 goto err_unlink_temp;
232 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100233
234 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200235 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100236 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200237 goto err_unlink_temp;
238 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100239
Willy Tarreau40aa0702013-03-10 23:51:38 +0100240 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200241 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200242 if (errno == EADDRINUSE) {
243 /* the old process might still own it, let's retry */
244 err |= ERR_RETRYABLE | ERR_ALERT;
245 msg = "cannot listen to socket";
246 }
247 else {
248 err |= ERR_FATAL | ERR_ALERT;
249 msg = "cannot bind UNIX socket";
250 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200251 goto err_unlink_temp;
252 }
253
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100254 /* <uid> and <gid> different of -1 will be used to change the socket owner.
255 * If <mode> is not 0, it will be used to restrict access to the socket.
256 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200257 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100258 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200259 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100260 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
261 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
262 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200263 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100264 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200265 goto err_unlink_temp;
266 }
267
Willy Tarreau40aa0702013-03-10 23:51:38 +0100268 ready = 0;
269 ready_len = sizeof(ready);
270 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
271 ready = 0;
272
273 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100274 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200275 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100276 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200277 goto err_unlink_temp;
278 }
279
Willy Tarreauccfccef2014-05-10 01:49:15 +0200280 /* Point of no return: we are ready, we'll switch the sockets. We don't
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500281 * fear losing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200282 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200283 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200284 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200285 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100286 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200287 goto err_rename;
288 }
289
Willy Tarreau68986ab2017-06-16 10:34:20 +0200290 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200291 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100292 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200293
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100294 /* the socket is now listening */
295 listener->fd = fd;
296 listener->state = LI_LISTEN;
297
Willy Tarreaua9786b62018-01-25 07:22:13 +0100298 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +0100299 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100300
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200301 /* for now, all regularly bound UNIX listeners are exportable */
302 if (!(listener->options & LI_O_INHERITED))
303 fdtab[fd].exported = 1;
304
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200305 return err;
306
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100307 err_rename:
308 ret = rename(backname, path);
309 if (ret < 0 && errno == ENOENT)
310 unlink(path);
311 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200312 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100313 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100314 close(fd);
315 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200316 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100317 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100318 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100319 if (msg && errlen) {
320 if (!ext)
321 snprintf(errmsg, errlen, "%s [%s]", msg, path);
322 else
323 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
324 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200325 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100326}
327
328/* This function closes the UNIX sockets for the specified listener.
329 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
330 */
331static int uxst_unbind_listener(struct listener *listener)
332{
Willy Tarreaube58c382011-07-24 18:28:10 +0200333 if (listener->state > LI_ASSIGNED) {
334 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100335 }
336 return ERR_NONE;
337}
338
Willy Tarreau32282382017-09-15 07:44:44 +0200339/* Add <listener> to the list of unix stream listeners (port is ignored). The
340 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
341 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200342 *
343 * Must be called with proto_lock held.
344 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100345 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200346static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100347{
348 if (listener->state != LI_INIT)
349 return;
350 listener->state = LI_ASSIGNED;
351 listener->proto = &proto_unix;
352 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
353 proto_unix.nb_listeners++;
354}
355
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200356/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
357 * was totally stopped, or > 0 if correctly paused. Nothing is done for
358 * plain unix sockets since currently it's the new process which handles
359 * the renaming. Abstract sockets are completely unbound.
360 */
Willy Tarreau31794892017-09-15 07:59:31 +0200361static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200362{
363 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
364 return 1;
365
Christopher Faulet510c0d62018-03-16 10:04:47 +0100366 /* Listener's lock already held. Call lockless version of
367 * unbind_listener. */
368 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200369 return 0;
370}
371
Willy Tarreau47f48c42014-05-09 22:57:47 +0200372
373/*
374 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200375 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200376 * and will be selected by the system. conn->target may point either to a valid
377 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
378 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
379 * whether there are data waiting for being sent or not, in order to adjust data
380 * write polling and on some platforms. The <delack> argument is ignored.
381 *
382 * Note that a pending send_proxy message accounts for data.
383 *
384 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200385 * - SF_ERR_NONE if everything's OK
386 * - SF_ERR_SRVTO if there are no more servers
387 * - SF_ERR_SRVCL if the connection was refused by the server
388 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
389 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
390 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100391 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200392 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200393 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200394 * it's invalid and the caller has nothing to do.
395 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200396static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200397{
398 int fd;
399 struct server *srv;
400 struct proxy *be;
401
Willy Tarreau47f48c42014-05-09 22:57:47 +0200402 switch (obj_type(conn->target)) {
403 case OBJ_TYPE_PROXY:
404 be = objt_proxy(conn->target);
405 srv = NULL;
406 break;
407 case OBJ_TYPE_SERVER:
408 srv = objt_server(conn->target);
409 be = srv->proxy;
410 break;
411 default:
412 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200413 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200414 }
415
Willy Tarreau585744b2017-08-24 14:31:19 +0200416 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200417 qfprintf(stderr, "Cannot get a server socket.\n");
418
419 if (errno == ENFILE) {
420 conn->err_code = CO_ER_SYS_FDLIM;
421 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100422 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
423 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200424 }
425 else if (errno == EMFILE) {
426 conn->err_code = CO_ER_PROC_FDLIM;
427 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100428 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
429 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200430 }
431 else if (errno == ENOBUFS || errno == ENOMEM) {
432 conn->err_code = CO_ER_SYS_MEMLIM;
433 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100434 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
435 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200436 }
437 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
438 conn->err_code = CO_ER_NOPROTO;
439 }
440 else
441 conn->err_code = CO_ER_SOCK_ERR;
442
443 /* this is a resource error */
444 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200445 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200446 }
447
448 if (fd >= global.maxsock) {
449 /* do not log anything there, it's a normal condition when this option
450 * is used to serialize connections to a server !
451 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100452 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200453 close(fd);
454 conn->err_code = CO_ER_CONF_FDLIM;
455 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200456 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200457 }
458
459 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
460 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
461 close(fd);
462 conn->err_code = CO_ER_SOCK_ERR;
463 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200464 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200465 }
466
William Lallemandc03eb012018-11-27 12:02:37 +0100467 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
468 ha_alert("Cannot set CLOEXEC on client socket.\n");
469 close(fd);
470 conn->err_code = CO_ER_SOCK_ERR;
471 conn->flags |= CO_FL_ERROR;
472 return SF_ERR_INTERNAL;
473 }
474
Willy Tarreau47f48c42014-05-09 22:57:47 +0200475 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200476 if (conn->send_proxy_ofs)
477 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200478
479 if (global.tune.server_sndbuf)
480 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
481
482 if (global.tune.server_rcvbuf)
483 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
484
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200485 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100486 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200487 conn->flags |= CO_FL_WAIT_L4_CONN;
488 }
Willy Tarreau94841792017-01-25 14:27:38 +0100489 else if (errno == EISCONN) {
490 conn->flags &= ~CO_FL_WAIT_L4_CONN;
491 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200492 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200493 char *msg;
494 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100495 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200496 conn->err_code = CO_ER_FREE_PORTS;
497 }
498 else {
499 msg = "local address already in use";
500 conn->err_code = CO_ER_ADDR_INUSE;
501 }
502
503 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
504 close(fd);
505 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
506 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 else if (errno == ETIMEDOUT) {
510 close(fd);
511 conn->err_code = CO_ER_SOCK_ERR;
512 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200513 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200514 }
515 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
516 close(fd);
517 conn->err_code = CO_ER_SOCK_ERR;
518 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200519 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200520 }
521 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200522 else {
523 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100524 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200525 */
526 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200527 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200528
529 conn->flags |= CO_FL_ADDR_TO_SET;
530
531 /* Prepare to send a few handshakes related to the on-wire protocol. */
532 if (conn->send_proxy_ofs)
533 conn->flags |= CO_FL_SEND_PROXY;
534
535 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200536 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200537
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100538 if (conn->flags & CO_FL_WAIT_L4_CONN) {
539 fd_want_send(fd);
540 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200541 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100542 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200543
Willy Tarreau47f48c42014-05-09 22:57:47 +0200544 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200545 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200546 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200547 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200548 }
549
Willy Tarreaue7dff022015-04-03 01:14:29 +0200550 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200551}
552
553
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100554/********************************
555 * 3) protocol-oriented functions
556 ********************************/
557
558
Willy Tarreau92fb9832007-10-16 17:34:28 +0200559/* This function creates all UNIX sockets bound to the protocol entry <proto>.
560 * It is intended to be used as the protocol's bind_all() function.
561 * The sockets will be registered but not added to any fd_set, in order not to
562 * loose them across the fork(). A call to uxst_enable_listeners() is needed
563 * to complete initialization.
564 *
Willy Tarreaudaacf362019-07-24 16:45:02 +0200565 * Must be called with proto_lock held.
566 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200567 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
568 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200569static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200570{
571 struct listener *listener;
572 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200573
574 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200575 err |= uxst_bind_listener(listener, errmsg, errlen);
576 if (err & ERR_ABORT)
577 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200578 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200579 return err;
580}
581
Willy Tarreau92fb9832007-10-16 17:34:28 +0200582
583/* This function stops all listening UNIX sockets bound to the protocol
584 * <proto>. It does not detaches them from the protocol.
585 * It always returns ERR_NONE.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200586 *
587 * Must be called with proto_lock held.
588 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200589 */
590static int uxst_unbind_listeners(struct protocol *proto)
591{
592 struct listener *listener;
593
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100594 list_for_each_entry(listener, &proto->listeners, proto_list)
595 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200596 return ERR_NONE;
597}
598
Willy Tarreau92fb9832007-10-16 17:34:28 +0200599/*
600 * Local variables:
601 * c-indent-level: 8
602 * c-basic-offset: 8
603 * End:
604 */