blob: 0e1cfc62a25b5b69d41207af8827a9e51eb8b9ad [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 Tarreau92b4f132020-06-01 11:05:15 +020037#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020039#include <haproxy/version.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020040
Willy Tarreau92fb9832007-10-16 17:34:28 +020041
Emeric Bruncf20bf12010-10-22 16:06:11 +020042static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
43static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010044static int uxst_unbind_listeners(struct protocol *proto);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020045static int uxst_connect_server(struct connection *conn, int flags);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020046static void uxst_add_listener(struct listener *listener, int port);
Willy Tarreau31794892017-09-15 07:59:31 +020047static int uxst_pause_listener(struct listener *l);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010048
49/* Note: must not be declared <const> as its list will be overwritten */
50static struct protocol proto_unix = {
51 .name = "unix_stream",
52 .sock_domain = PF_UNIX,
53 .sock_type = SOCK_STREAM,
54 .sock_prot = 0,
55 .sock_family = AF_UNIX,
56 .sock_addrlen = sizeof(struct sockaddr_un),
57 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020058 .accept = &listener_accept,
Willy Tarreau47f48c42014-05-09 22:57:47 +020059 .connect = &uxst_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020060 .bind = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010061 .bind_all = uxst_bind_listeners,
62 .unbind_all = uxst_unbind_listeners,
63 .enable_all = enable_all_listeners,
64 .disable_all = disable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020065 .get_src = sock_get_src,
66 .get_dst = sock_get_dst,
Willy Tarreaufd0e0082014-07-07 21:07:51 +020067 .pause = uxst_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020068 .add = uxst_add_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010069 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
70 .nb_listeners = 0,
71};
72
Willy Tarreau0108d902018-11-25 19:14:37 +010073INITCALL1(STG_REGISTER, protocol_register, &proto_unix);
74
Willy Tarreaudabf2e22007-10-28 21:59:24 +010075/********************************
76 * 1) low-level socket functions
77 ********************************/
78
79
Cyril Bonté1f5848a2010-11-14 17:03:19 +010080/********************************
81 * 2) listener-oriented functions
82 ********************************/
83
84
Olivier Houchardf886e342017-04-05 22:24:59 +020085static int uxst_find_compatible_fd(struct listener *l)
86{
87 struct xfer_sock_list *xfer_sock = xfer_sock_list;
88 int ret = -1;
89
90 while (xfer_sock) {
91 struct sockaddr_un *un1 = (void *)&l->addr;
92 struct sockaddr_un *un2 = (void *)&xfer_sock->addr;
93
94 /*
95 * The bound socket's path as returned by getsockaddr
96 * will be the temporary name <sockname>.XXXXX.tmp,
97 * so we can't just compare the two names
98 */
99 if (xfer_sock->addr.ss_family == AF_UNIX &&
100 strncmp(un1->sun_path, un2->sun_path,
101 strlen(un1->sun_path)) == 0) {
102 char *after_sockname = un2->sun_path +
103 strlen(un1->sun_path);
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500104 /* Make a reasonable effort to check that
Olivier Houchardf886e342017-04-05 22:24:59 +0200105 * it is indeed a haproxy-generated temporary
106 * name, it's not perfect, but probably good enough.
107 */
108 if (after_sockname[0] == '.') {
109 after_sockname++;
110 while (after_sockname[0] >= '0' &&
111 after_sockname[0] <= '9')
112 after_sockname++;
113 if (!strcmp(after_sockname, ".tmp"))
114 break;
Olivier Houchardb4dd15b2018-06-06 18:34:34 +0200115 /* abns sockets sun_path starts with a \0 */
116 } else if (un1->sun_path[0] == 0
117 && un2->sun_path[0] == 0
118 && !memcmp(&un1->sun_path[1], &un2->sun_path[1],
119 sizeof(un1->sun_path) - 1))
120 break;
Olivier Houchardf886e342017-04-05 22:24:59 +0200121 }
122 xfer_sock = xfer_sock->next;
123 }
124 if (xfer_sock != NULL) {
125 ret = xfer_sock->fd;
126 if (xfer_sock == xfer_sock_list)
127 xfer_sock_list = xfer_sock->next;
128 if (xfer_sock->prev)
129 xfer_sock->prev->next = xfer_sock->next;
130 if (xfer_sock->next)
Olivier Houchardec9516a2018-03-08 18:25:49 +0100131 xfer_sock->next->prev = xfer_sock->prev;
Olivier Houchardf886e342017-04-05 22:24:59 +0200132 free(xfer_sock);
133 }
134 return ret;
135
136}
137
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100138/* This function creates a UNIX socket associated to the listener. It changes
139 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100140 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
141 * may return a warning or an error message in <errmsg> if the message is at
142 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
143 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200144 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100145static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200146{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100147 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200148 char tempname[MAXPATHLEN];
149 char backname[MAXPATHLEN];
150 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100151 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100152 const char *path;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100153 int maxpathlen;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100154 int ext, ready;
155 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200156 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100157 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200158
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200159 err = ERR_NONE;
160
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100161 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100162 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100163 *errmsg = 0;
164
165 if (listener->state != LI_ASSIGNED)
166 return ERR_NONE; /* already bound */
167
Olivier Houchardf886e342017-04-05 22:24:59 +0200168 if (listener->fd == -1)
169 listener->fd = uxst_find_compatible_fd(listener);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100170 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200171
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100172 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
173
Willy Tarreau40aa0702013-03-10 23:51:38 +0100174 /* if the listener already has an fd assigned, then we were offered the
175 * fd by an external process (most likely the parent), and we don't want
176 * to create a new socket. However we still want to set a few flags on
177 * the socket.
178 */
179 fd = listener->fd;
180 ext = (fd >= 0);
181 if (ext)
182 goto fd_ready;
183
Willy Tarreauccfccef2014-05-10 01:49:15 +0200184 if (path[0]) {
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100185 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200186 if (ret < 0 || ret >= sizeof(addr.sun_path)) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200187 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100188 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200189 goto err_return;
190 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200191
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100192 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
193 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200194 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100195 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200196 goto err_return;
197 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200198
Willy Tarreauccfccef2014-05-10 01:49:15 +0200199 /* 2. clean existing orphaned entries */
200 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200201 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200202 msg = "error when trying to unlink previous UNIX socket";
203 goto err_return;
204 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200205
Willy Tarreauccfccef2014-05-10 01:49:15 +0200206 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200207 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200208 msg = "error when trying to unlink previous UNIX socket";
209 goto err_return;
210 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200211
Willy Tarreauccfccef2014-05-10 01:49:15 +0200212 /* 3. backup existing socket */
213 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200214 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200215 msg = "error when trying to preserve previous UNIX socket";
216 goto err_return;
217 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200218
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200219 /* Note: this test is redundant with the snprintf one above and
220 * will never trigger, it's just added as the only way to shut
221 * gcc's painfully dumb warning about possibly truncated output
222 * during strncpy(). Don't move it above or smart gcc will not
223 * see it!
224 */
225 if (strlen(tempname) >= sizeof(addr.sun_path)) {
226 err |= ERR_FATAL | ERR_ALERT;
227 msg = "name too long for UNIX socket (limit usually 97)";
228 goto err_return;
229 }
230
Willy Tarreau719e07c2019-12-11 16:29:10 +0100231 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200232 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200233 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200234 else {
235 /* first char is zero, it's an abstract socket whose address
236 * is defined by all the bytes past this zero.
237 */
238 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
239 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200240 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200241
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100242 fd = socket(PF_UNIX, SOCK_STREAM, 0);
243 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200244 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100245 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200246 goto err_unlink_back;
247 }
248
Willy Tarreau40aa0702013-03-10 23:51:38 +0100249 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100250 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200251 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100252 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200253 goto err_unlink_temp;
254 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100255
256 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200257 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100258 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200259 goto err_unlink_temp;
260 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100261
Willy Tarreau40aa0702013-03-10 23:51:38 +0100262 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200263 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200264 if (errno == EADDRINUSE) {
265 /* the old process might still own it, let's retry */
266 err |= ERR_RETRYABLE | ERR_ALERT;
267 msg = "cannot listen to socket";
268 }
269 else {
270 err |= ERR_FATAL | ERR_ALERT;
271 msg = "cannot bind UNIX socket";
272 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200273 goto err_unlink_temp;
274 }
275
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100276 /* <uid> and <gid> different of -1 will be used to change the socket owner.
277 * If <mode> is not 0, it will be used to restrict access to the socket.
278 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200279 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100280 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200281 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100282 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
283 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
284 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200285 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100286 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200287 goto err_unlink_temp;
288 }
289
Willy Tarreau40aa0702013-03-10 23:51:38 +0100290 ready = 0;
291 ready_len = sizeof(ready);
292 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
293 ready = 0;
294
295 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100296 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200297 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100298 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200299 goto err_unlink_temp;
300 }
301
Willy Tarreauccfccef2014-05-10 01:49:15 +0200302 /* Point of no return: we are ready, we'll switch the sockets. We don't
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500303 * fear losing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200304 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200305 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200306 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200307 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100308 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200309 goto err_rename;
310 }
311
Willy Tarreau68986ab2017-06-16 10:34:20 +0200312 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200313 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100314 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200315
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100316 /* the socket is now listening */
317 listener->fd = fd;
318 listener->state = LI_LISTEN;
319
Willy Tarreaua9786b62018-01-25 07:22:13 +0100320 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +0100321 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100322
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200323 /* for now, all regularly bound UNIX listeners are exportable */
324 if (!(listener->options & LI_O_INHERITED))
325 fdtab[fd].exported = 1;
326
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200327 return err;
328
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100329 err_rename:
330 ret = rename(backname, path);
331 if (ret < 0 && errno == ENOENT)
332 unlink(path);
333 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200334 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100335 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100336 close(fd);
337 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200338 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100339 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100340 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100341 if (msg && errlen) {
342 if (!ext)
343 snprintf(errmsg, errlen, "%s [%s]", msg, path);
344 else
345 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
346 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200347 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100348}
349
350/* This function closes the UNIX sockets for the specified listener.
351 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
352 */
353static int uxst_unbind_listener(struct listener *listener)
354{
Willy Tarreaube58c382011-07-24 18:28:10 +0200355 if (listener->state > LI_ASSIGNED) {
356 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100357 }
358 return ERR_NONE;
359}
360
Willy Tarreau32282382017-09-15 07:44:44 +0200361/* Add <listener> to the list of unix stream listeners (port is ignored). The
362 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
363 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200364 *
365 * Must be called with proto_lock held.
366 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100367 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200368static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100369{
370 if (listener->state != LI_INIT)
371 return;
372 listener->state = LI_ASSIGNED;
373 listener->proto = &proto_unix;
374 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
375 proto_unix.nb_listeners++;
376}
377
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200378/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
379 * was totally stopped, or > 0 if correctly paused. Nothing is done for
380 * plain unix sockets since currently it's the new process which handles
381 * the renaming. Abstract sockets are completely unbound.
382 */
Willy Tarreau31794892017-09-15 07:59:31 +0200383static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200384{
385 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
386 return 1;
387
Christopher Faulet510c0d62018-03-16 10:04:47 +0100388 /* Listener's lock already held. Call lockless version of
389 * unbind_listener. */
390 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200391 return 0;
392}
393
Willy Tarreau47f48c42014-05-09 22:57:47 +0200394
395/*
396 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200397 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200398 * and will be selected by the system. conn->target may point either to a valid
399 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
400 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
401 * whether there are data waiting for being sent or not, in order to adjust data
402 * write polling and on some platforms. The <delack> argument is ignored.
403 *
404 * Note that a pending send_proxy message accounts for data.
405 *
406 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200407 * - SF_ERR_NONE if everything's OK
408 * - SF_ERR_SRVTO if there are no more servers
409 * - SF_ERR_SRVCL if the connection was refused by the server
410 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
411 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
412 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100413 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200414 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200415 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200416 * it's invalid and the caller has nothing to do.
417 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200418static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200419{
420 int fd;
421 struct server *srv;
422 struct proxy *be;
423
Willy Tarreau47f48c42014-05-09 22:57:47 +0200424 switch (obj_type(conn->target)) {
425 case OBJ_TYPE_PROXY:
426 be = objt_proxy(conn->target);
427 srv = NULL;
428 break;
429 case OBJ_TYPE_SERVER:
430 srv = objt_server(conn->target);
431 be = srv->proxy;
432 break;
433 default:
434 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200435 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200436 }
437
Willy Tarreau585744b2017-08-24 14:31:19 +0200438 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200439 qfprintf(stderr, "Cannot get a server socket.\n");
440
441 if (errno == ENFILE) {
442 conn->err_code = CO_ER_SYS_FDLIM;
443 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100444 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
445 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200446 }
447 else if (errno == EMFILE) {
448 conn->err_code = CO_ER_PROC_FDLIM;
449 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100450 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
451 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200452 }
453 else if (errno == ENOBUFS || errno == ENOMEM) {
454 conn->err_code = CO_ER_SYS_MEMLIM;
455 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100456 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
457 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200458 }
459 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
460 conn->err_code = CO_ER_NOPROTO;
461 }
462 else
463 conn->err_code = CO_ER_SOCK_ERR;
464
465 /* this is a resource error */
466 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200467 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200468 }
469
470 if (fd >= global.maxsock) {
471 /* do not log anything there, it's a normal condition when this option
472 * is used to serialize connections to a server !
473 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100474 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200475 close(fd);
476 conn->err_code = CO_ER_CONF_FDLIM;
477 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200478 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200479 }
480
481 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
482 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
483 close(fd);
484 conn->err_code = CO_ER_SOCK_ERR;
485 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200486 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200487 }
488
William Lallemandc03eb012018-11-27 12:02:37 +0100489 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
490 ha_alert("Cannot set CLOEXEC on client socket.\n");
491 close(fd);
492 conn->err_code = CO_ER_SOCK_ERR;
493 conn->flags |= CO_FL_ERROR;
494 return SF_ERR_INTERNAL;
495 }
496
Willy Tarreau47f48c42014-05-09 22:57:47 +0200497 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200498 if (conn->send_proxy_ofs)
499 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200500
501 if (global.tune.server_sndbuf)
502 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
503
504 if (global.tune.server_rcvbuf)
505 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
506
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200507 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100508 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200509 conn->flags |= CO_FL_WAIT_L4_CONN;
510 }
Willy Tarreau94841792017-01-25 14:27:38 +0100511 else if (errno == EISCONN) {
512 conn->flags &= ~CO_FL_WAIT_L4_CONN;
513 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200514 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200515 char *msg;
516 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100517 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200518 conn->err_code = CO_ER_FREE_PORTS;
519 }
520 else {
521 msg = "local address already in use";
522 conn->err_code = CO_ER_ADDR_INUSE;
523 }
524
525 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
526 close(fd);
527 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
528 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200529 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200530 }
531 else if (errno == ETIMEDOUT) {
532 close(fd);
533 conn->err_code = CO_ER_SOCK_ERR;
534 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200535 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200536 }
537 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
538 close(fd);
539 conn->err_code = CO_ER_SOCK_ERR;
540 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200541 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200542 }
543 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200544 else {
545 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100546 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200547 */
548 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200549 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200550
551 conn->flags |= CO_FL_ADDR_TO_SET;
552
553 /* Prepare to send a few handshakes related to the on-wire protocol. */
554 if (conn->send_proxy_ofs)
555 conn->flags |= CO_FL_SEND_PROXY;
556
557 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200558 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200559
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100560 if (conn->flags & CO_FL_WAIT_L4_CONN) {
561 fd_want_send(fd);
562 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200563 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100564 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200565
Willy Tarreau47f48c42014-05-09 22:57:47 +0200566 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200567 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200568 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200569 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200570 }
571
Willy Tarreaue7dff022015-04-03 01:14:29 +0200572 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200573}
574
575
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100576/********************************
577 * 3) protocol-oriented functions
578 ********************************/
579
580
Willy Tarreau92fb9832007-10-16 17:34:28 +0200581/* This function creates all UNIX sockets bound to the protocol entry <proto>.
582 * It is intended to be used as the protocol's bind_all() function.
583 * The sockets will be registered but not added to any fd_set, in order not to
584 * loose them across the fork(). A call to uxst_enable_listeners() is needed
585 * to complete initialization.
586 *
Willy Tarreaudaacf362019-07-24 16:45:02 +0200587 * Must be called with proto_lock held.
588 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200589 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
590 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200591static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200592{
593 struct listener *listener;
594 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200595
596 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200597 err |= uxst_bind_listener(listener, errmsg, errlen);
598 if (err & ERR_ABORT)
599 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200600 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200601 return err;
602}
603
Willy Tarreau92fb9832007-10-16 17:34:28 +0200604
605/* This function stops all listening UNIX sockets bound to the protocol
606 * <proto>. It does not detaches them from the protocol.
607 * It always returns ERR_NONE.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200608 *
609 * Must be called with proto_lock held.
610 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200611 */
612static int uxst_unbind_listeners(struct protocol *proto)
613{
614 struct listener *listener;
615
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100616 list_for_each_entry(listener, &proto->listeners, proto_list)
617 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200618 return ERR_NONE;
619}
620
Willy Tarreau92fb9832007-10-16 17:34:28 +0200621/*
622 * Local variables:
623 * c-indent-level: 8
624 * c-basic-offset: 8
625 * End:
626 */