blob: f64c890ab28b510ead526861ff6df1842ab102d6 [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 Tarreau92b4f132020-06-01 11:05:15 +020036#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020038#include <haproxy/version.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020039
Willy Tarreau92fb9832007-10-16 17:34:28 +020040
Emeric Bruncf20bf12010-10-22 16:06:11 +020041static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
42static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010043static int uxst_unbind_listeners(struct protocol *proto);
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);
47static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir);
48static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir);
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 Tarreau59b94792012-05-11 16:16:40 +020066 .get_src = uxst_get_src,
67 .get_dst = uxst_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 Tarreaudabf2e22007-10-28 21:59:24 +010070 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
71 .nb_listeners = 0,
72};
73
Willy Tarreau0108d902018-11-25 19:14:37 +010074INITCALL1(STG_REGISTER, protocol_register, &proto_unix);
75
Willy Tarreaudabf2e22007-10-28 21:59:24 +010076/********************************
77 * 1) low-level socket functions
78 ********************************/
79
Willy Tarreau59b94792012-05-11 16:16:40 +020080/*
81 * Retrieves the source address for the socket <fd>, with <dir> indicating
82 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
83 * success, -1 in case of error. The socket's source address is stored in
84 * <sa> for <salen> bytes.
85 */
Willy Tarreau31794892017-09-15 07:59:31 +020086static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
Willy Tarreau59b94792012-05-11 16:16:40 +020087{
88 if (dir)
89 return getsockname(fd, sa, &salen);
90 else
91 return getpeername(fd, sa, &salen);
92}
93
94
95/*
96 * Retrieves the original destination address for the socket <fd>, with <dir>
97 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
98 * case of success, -1 in case of error. The socket's source address is stored
99 * in <sa> for <salen> bytes.
100 */
Willy Tarreau31794892017-09-15 07:59:31 +0200101static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
Willy Tarreau59b94792012-05-11 16:16:40 +0200102{
103 if (dir)
104 return getpeername(fd, sa, &salen);
105 else
106 return getsockname(fd, sa, &salen);
107}
108
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100109
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100110/********************************
111 * 2) listener-oriented functions
112 ********************************/
113
114
Olivier Houchardf886e342017-04-05 22:24:59 +0200115static int uxst_find_compatible_fd(struct listener *l)
116{
117 struct xfer_sock_list *xfer_sock = xfer_sock_list;
118 int ret = -1;
119
120 while (xfer_sock) {
121 struct sockaddr_un *un1 = (void *)&l->addr;
122 struct sockaddr_un *un2 = (void *)&xfer_sock->addr;
123
124 /*
125 * The bound socket's path as returned by getsockaddr
126 * will be the temporary name <sockname>.XXXXX.tmp,
127 * so we can't just compare the two names
128 */
129 if (xfer_sock->addr.ss_family == AF_UNIX &&
130 strncmp(un1->sun_path, un2->sun_path,
131 strlen(un1->sun_path)) == 0) {
132 char *after_sockname = un2->sun_path +
133 strlen(un1->sun_path);
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500134 /* Make a reasonable effort to check that
Olivier Houchardf886e342017-04-05 22:24:59 +0200135 * it is indeed a haproxy-generated temporary
136 * name, it's not perfect, but probably good enough.
137 */
138 if (after_sockname[0] == '.') {
139 after_sockname++;
140 while (after_sockname[0] >= '0' &&
141 after_sockname[0] <= '9')
142 after_sockname++;
143 if (!strcmp(after_sockname, ".tmp"))
144 break;
Olivier Houchardb4dd15b2018-06-06 18:34:34 +0200145 /* abns sockets sun_path starts with a \0 */
146 } else if (un1->sun_path[0] == 0
147 && un2->sun_path[0] == 0
148 && !memcmp(&un1->sun_path[1], &un2->sun_path[1],
149 sizeof(un1->sun_path) - 1))
150 break;
Olivier Houchardf886e342017-04-05 22:24:59 +0200151 }
152 xfer_sock = xfer_sock->next;
153 }
154 if (xfer_sock != NULL) {
155 ret = xfer_sock->fd;
156 if (xfer_sock == xfer_sock_list)
157 xfer_sock_list = xfer_sock->next;
158 if (xfer_sock->prev)
159 xfer_sock->prev->next = xfer_sock->next;
160 if (xfer_sock->next)
Olivier Houchardec9516a2018-03-08 18:25:49 +0100161 xfer_sock->next->prev = xfer_sock->prev;
Olivier Houchardf886e342017-04-05 22:24:59 +0200162 free(xfer_sock);
163 }
164 return ret;
165
166}
167
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100168/* This function creates a UNIX socket associated to the listener. It changes
169 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100170 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
171 * may return a warning or an error message in <errmsg> if the message is at
172 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
173 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200174 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100175static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200176{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100177 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200178 char tempname[MAXPATHLEN];
179 char backname[MAXPATHLEN];
180 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100181 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100182 const char *path;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100183 int maxpathlen;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100184 int ext, ready;
185 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200186 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100187 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200188
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200189 err = ERR_NONE;
190
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100191 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100192 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100193 *errmsg = 0;
194
195 if (listener->state != LI_ASSIGNED)
196 return ERR_NONE; /* already bound */
197
Olivier Houchardf886e342017-04-05 22:24:59 +0200198 if (listener->fd == -1)
199 listener->fd = uxst_find_compatible_fd(listener);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100200 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200201
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100202 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
203
Willy Tarreau40aa0702013-03-10 23:51:38 +0100204 /* if the listener already has an fd assigned, then we were offered the
205 * fd by an external process (most likely the parent), and we don't want
206 * to create a new socket. However we still want to set a few flags on
207 * the socket.
208 */
209 fd = listener->fd;
210 ext = (fd >= 0);
211 if (ext)
212 goto fd_ready;
213
Willy Tarreauccfccef2014-05-10 01:49:15 +0200214 if (path[0]) {
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100215 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200216 if (ret < 0 || ret >= sizeof(addr.sun_path)) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200217 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100218 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200219 goto err_return;
220 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200221
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100222 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
223 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200224 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100225 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200226 goto err_return;
227 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200228
Willy Tarreauccfccef2014-05-10 01:49:15 +0200229 /* 2. clean existing orphaned entries */
230 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200231 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200232 msg = "error when trying to unlink previous UNIX socket";
233 goto err_return;
234 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200235
Willy Tarreauccfccef2014-05-10 01:49:15 +0200236 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200237 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200238 msg = "error when trying to unlink previous UNIX socket";
239 goto err_return;
240 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200241
Willy Tarreauccfccef2014-05-10 01:49:15 +0200242 /* 3. backup existing socket */
243 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200244 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200245 msg = "error when trying to preserve previous UNIX socket";
246 goto err_return;
247 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200248
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200249 /* Note: this test is redundant with the snprintf one above and
250 * will never trigger, it's just added as the only way to shut
251 * gcc's painfully dumb warning about possibly truncated output
252 * during strncpy(). Don't move it above or smart gcc will not
253 * see it!
254 */
255 if (strlen(tempname) >= sizeof(addr.sun_path)) {
256 err |= ERR_FATAL | ERR_ALERT;
257 msg = "name too long for UNIX socket (limit usually 97)";
258 goto err_return;
259 }
260
Willy Tarreau719e07c2019-12-11 16:29:10 +0100261 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200262 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200263 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200264 else {
265 /* first char is zero, it's an abstract socket whose address
266 * is defined by all the bytes past this zero.
267 */
268 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
269 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200270 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200271
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100272 fd = socket(PF_UNIX, SOCK_STREAM, 0);
273 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200274 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100275 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200276 goto err_unlink_back;
277 }
278
Willy Tarreau40aa0702013-03-10 23:51:38 +0100279 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100280 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200281 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100282 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200283 goto err_unlink_temp;
284 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100285
286 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200287 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100288 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200289 goto err_unlink_temp;
290 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100291
Willy Tarreau40aa0702013-03-10 23:51:38 +0100292 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200293 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200294 if (errno == EADDRINUSE) {
295 /* the old process might still own it, let's retry */
296 err |= ERR_RETRYABLE | ERR_ALERT;
297 msg = "cannot listen to socket";
298 }
299 else {
300 err |= ERR_FATAL | ERR_ALERT;
301 msg = "cannot bind UNIX socket";
302 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200303 goto err_unlink_temp;
304 }
305
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100306 /* <uid> and <gid> different of -1 will be used to change the socket owner.
307 * If <mode> is not 0, it will be used to restrict access to the socket.
308 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200309 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100310 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200311 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100312 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
313 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
314 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200315 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100316 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200317 goto err_unlink_temp;
318 }
319
Willy Tarreau40aa0702013-03-10 23:51:38 +0100320 ready = 0;
321 ready_len = sizeof(ready);
322 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
323 ready = 0;
324
325 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100326 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200327 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100328 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200329 goto err_unlink_temp;
330 }
331
Willy Tarreauccfccef2014-05-10 01:49:15 +0200332 /* Point of no return: we are ready, we'll switch the sockets. We don't
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500333 * fear losing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200334 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200335 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200336 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200337 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100338 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200339 goto err_rename;
340 }
341
Willy Tarreau68986ab2017-06-16 10:34:20 +0200342 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200343 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100344 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200345
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100346 /* the socket is now listening */
347 listener->fd = fd;
348 listener->state = LI_LISTEN;
349
Willy Tarreaua9786b62018-01-25 07:22:13 +0100350 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +0100351 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100352
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200353 /* for now, all regularly bound UNIX listeners are exportable */
354 if (!(listener->options & LI_O_INHERITED))
355 fdtab[fd].exported = 1;
356
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200357 return err;
358
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100359 err_rename:
360 ret = rename(backname, path);
361 if (ret < 0 && errno == ENOENT)
362 unlink(path);
363 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200364 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100365 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100366 close(fd);
367 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200368 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100369 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100370 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100371 if (msg && errlen) {
372 if (!ext)
373 snprintf(errmsg, errlen, "%s [%s]", msg, path);
374 else
375 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
376 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200377 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100378}
379
380/* This function closes the UNIX sockets for the specified listener.
381 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
382 */
383static int uxst_unbind_listener(struct listener *listener)
384{
Willy Tarreaube58c382011-07-24 18:28:10 +0200385 if (listener->state > LI_ASSIGNED) {
386 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100387 }
388 return ERR_NONE;
389}
390
Willy Tarreau32282382017-09-15 07:44:44 +0200391/* Add <listener> to the list of unix stream listeners (port is ignored). The
392 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
393 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200394 *
395 * Must be called with proto_lock held.
396 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100397 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200398static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100399{
400 if (listener->state != LI_INIT)
401 return;
402 listener->state = LI_ASSIGNED;
403 listener->proto = &proto_unix;
404 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
405 proto_unix.nb_listeners++;
406}
407
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200408/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
409 * was totally stopped, or > 0 if correctly paused. Nothing is done for
410 * plain unix sockets since currently it's the new process which handles
411 * the renaming. Abstract sockets are completely unbound.
412 */
Willy Tarreau31794892017-09-15 07:59:31 +0200413static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200414{
415 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
416 return 1;
417
Christopher Faulet510c0d62018-03-16 10:04:47 +0100418 /* Listener's lock already held. Call lockless version of
419 * unbind_listener. */
420 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200421 return 0;
422}
423
Willy Tarreau47f48c42014-05-09 22:57:47 +0200424
425/*
426 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200427 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200428 * and will be selected by the system. conn->target may point either to a valid
429 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
430 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
431 * whether there are data waiting for being sent or not, in order to adjust data
432 * write polling and on some platforms. The <delack> argument is ignored.
433 *
434 * Note that a pending send_proxy message accounts for data.
435 *
436 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200437 * - SF_ERR_NONE if everything's OK
438 * - SF_ERR_SRVTO if there are no more servers
439 * - SF_ERR_SRVCL if the connection was refused by the server
440 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
441 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
442 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100443 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200444 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200445 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200446 * it's invalid and the caller has nothing to do.
447 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200448static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200449{
450 int fd;
451 struct server *srv;
452 struct proxy *be;
453
Willy Tarreau47f48c42014-05-09 22:57:47 +0200454 switch (obj_type(conn->target)) {
455 case OBJ_TYPE_PROXY:
456 be = objt_proxy(conn->target);
457 srv = NULL;
458 break;
459 case OBJ_TYPE_SERVER:
460 srv = objt_server(conn->target);
461 be = srv->proxy;
462 break;
463 default:
464 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200465 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200466 }
467
Willy Tarreau585744b2017-08-24 14:31:19 +0200468 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200469 qfprintf(stderr, "Cannot get a server socket.\n");
470
471 if (errno == ENFILE) {
472 conn->err_code = CO_ER_SYS_FDLIM;
473 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100474 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
475 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200476 }
477 else if (errno == EMFILE) {
478 conn->err_code = CO_ER_PROC_FDLIM;
479 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100480 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
481 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200482 }
483 else if (errno == ENOBUFS || errno == ENOMEM) {
484 conn->err_code = CO_ER_SYS_MEMLIM;
485 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100486 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
487 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200488 }
489 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
490 conn->err_code = CO_ER_NOPROTO;
491 }
492 else
493 conn->err_code = CO_ER_SOCK_ERR;
494
495 /* this is a resource error */
496 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200497 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200498 }
499
500 if (fd >= global.maxsock) {
501 /* do not log anything there, it's a normal condition when this option
502 * is used to serialize connections to a server !
503 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100504 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200505 close(fd);
506 conn->err_code = CO_ER_CONF_FDLIM;
507 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200508 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200509 }
510
511 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
512 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
513 close(fd);
514 conn->err_code = CO_ER_SOCK_ERR;
515 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200516 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200517 }
518
William Lallemandc03eb012018-11-27 12:02:37 +0100519 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
520 ha_alert("Cannot set CLOEXEC on client socket.\n");
521 close(fd);
522 conn->err_code = CO_ER_SOCK_ERR;
523 conn->flags |= CO_FL_ERROR;
524 return SF_ERR_INTERNAL;
525 }
526
Willy Tarreau47f48c42014-05-09 22:57:47 +0200527 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200528 if (conn->send_proxy_ofs)
529 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200530
531 if (global.tune.server_sndbuf)
532 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
533
534 if (global.tune.server_rcvbuf)
535 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
536
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200537 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100538 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200539 conn->flags |= CO_FL_WAIT_L4_CONN;
540 }
Willy Tarreau94841792017-01-25 14:27:38 +0100541 else if (errno == EISCONN) {
542 conn->flags &= ~CO_FL_WAIT_L4_CONN;
543 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200544 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200545 char *msg;
546 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100547 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200548 conn->err_code = CO_ER_FREE_PORTS;
549 }
550 else {
551 msg = "local address already in use";
552 conn->err_code = CO_ER_ADDR_INUSE;
553 }
554
555 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
556 close(fd);
557 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
558 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200559 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200560 }
561 else if (errno == ETIMEDOUT) {
562 close(fd);
563 conn->err_code = CO_ER_SOCK_ERR;
564 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200565 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200566 }
567 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
568 close(fd);
569 conn->err_code = CO_ER_SOCK_ERR;
570 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200571 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200572 }
573 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200574 else {
575 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100576 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200577 */
578 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200579 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200580
581 conn->flags |= CO_FL_ADDR_TO_SET;
582
583 /* Prepare to send a few handshakes related to the on-wire protocol. */
584 if (conn->send_proxy_ofs)
585 conn->flags |= CO_FL_SEND_PROXY;
586
587 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200588 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200589
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100590 if (conn->flags & CO_FL_WAIT_L4_CONN) {
591 fd_want_send(fd);
592 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200593 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100594 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200595
Willy Tarreau47f48c42014-05-09 22:57:47 +0200596 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200597 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200598 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200599 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200600 }
601
Willy Tarreaue7dff022015-04-03 01:14:29 +0200602 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200603}
604
605
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100606/********************************
607 * 3) protocol-oriented functions
608 ********************************/
609
610
Willy Tarreau92fb9832007-10-16 17:34:28 +0200611/* This function creates all UNIX sockets bound to the protocol entry <proto>.
612 * It is intended to be used as the protocol's bind_all() function.
613 * The sockets will be registered but not added to any fd_set, in order not to
614 * loose them across the fork(). A call to uxst_enable_listeners() is needed
615 * to complete initialization.
616 *
Willy Tarreaudaacf362019-07-24 16:45:02 +0200617 * Must be called with proto_lock held.
618 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200619 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
620 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200621static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200622{
623 struct listener *listener;
624 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200625
626 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200627 err |= uxst_bind_listener(listener, errmsg, errlen);
628 if (err & ERR_ABORT)
629 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200630 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200631 return err;
632}
633
Willy Tarreau92fb9832007-10-16 17:34:28 +0200634
635/* This function stops all listening UNIX sockets bound to the protocol
636 * <proto>. It does not detaches them from the protocol.
637 * It always returns ERR_NONE.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200638 *
639 * Must be called with proto_lock held.
640 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200641 */
642static int uxst_unbind_listeners(struct protocol *proto)
643{
644 struct listener *listener;
645
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100646 list_for_each_entry(listener, &proto->listeners, proto_list)
647 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200648 return ERR_NONE;
649}
650
Willy Tarreau92fb9832007-10-16 17:34:28 +0200651/*
652 * Local variables:
653 * c-indent-level: 8
654 * c-basic-offset: 8
655 * End:
656 */