Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * UNIX SOCK_STREAM protocol layer (uxst) |
| 3 | * |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/un.h> |
| 26 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 27 | #include <haproxy/api.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 28 | #include <haproxy/connection.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 29 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 30 | #include <haproxy/fd.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 31 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 32 | #include <haproxy/list.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 33 | #include <haproxy/listener.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 34 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 35 | #include <haproxy/protocol.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 36 | #include <haproxy/sock.h> |
Willy Tarreau | f172558 | 2020-08-28 15:30:11 +0200 | [diff] [blame] | 37 | #include <haproxy/sock_unix.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 38 | #include <haproxy/time.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 39 | #include <haproxy/tools.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 40 | #include <haproxy/version.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 42 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 43 | static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen); |
| 44 | static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 45 | static int uxst_unbind_listeners(struct protocol *proto); |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 46 | static int uxst_connect_server(struct connection *conn, int flags); |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 47 | static void uxst_add_listener(struct listener *listener, int port); |
Willy Tarreau | 3179489 | 2017-09-15 07:59:31 +0200 | [diff] [blame] | 48 | static int uxst_pause_listener(struct listener *l); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 49 | |
| 50 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 51 | static 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 Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 59 | .accept = &listener_accept, |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 60 | .connect = &uxst_connect_server, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 61 | .bind = uxst_bind_listener, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 62 | .bind_all = uxst_bind_listeners, |
| 63 | .unbind_all = uxst_unbind_listeners, |
| 64 | .enable_all = enable_all_listeners, |
| 65 | .disable_all = disable_all_listeners, |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 66 | .get_src = sock_get_src, |
| 67 | .get_dst = sock_get_dst, |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 68 | .pause = uxst_pause_listener, |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 69 | .add = uxst_add_listener, |
Willy Tarreau | f172558 | 2020-08-28 15:30:11 +0200 | [diff] [blame] | 70 | .addrcmp = sock_unix_addrcmp, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 71 | .listeners = LIST_HEAD_INIT(proto_unix.listeners), |
| 72 | .nb_listeners = 0, |
| 73 | }; |
| 74 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 75 | INITCALL1(STG_REGISTER, protocol_register, &proto_unix); |
| 76 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 77 | /******************************** |
| 78 | * 1) low-level socket functions |
| 79 | ********************************/ |
| 80 | |
| 81 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 82 | /******************************** |
| 83 | * 2) listener-oriented functions |
| 84 | ********************************/ |
| 85 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 86 | /* This function creates a UNIX socket associated to the listener. It changes |
| 87 | * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling. |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 88 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It |
| 89 | * may return a warning or an error message in <errmsg> if the message is at |
| 90 | * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if |
| 91 | * <errlen> is also zero. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 92 | */ |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 93 | static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 94 | { |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 95 | int fd; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 96 | char tempname[MAXPATHLEN]; |
| 97 | char backname[MAXPATHLEN]; |
| 98 | struct sockaddr_un addr; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 99 | const char *msg = NULL; |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 100 | const char *path; |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 101 | int maxpathlen; |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 102 | int ext, ready; |
| 103 | socklen_t ready_len; |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 104 | int err; |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 105 | int ret; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 106 | |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 107 | err = ERR_NONE; |
| 108 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 109 | /* ensure we never return garbage */ |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 110 | if (errlen) |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 111 | *errmsg = 0; |
| 112 | |
| 113 | if (listener->state != LI_ASSIGNED) |
| 114 | return ERR_NONE; /* already bound */ |
| 115 | |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 116 | if (listener->fd == -1) |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame^] | 117 | listener->fd = sock_find_compatible_fd(listener); |
| 118 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 119 | path = ((struct sockaddr_un *)&listener->addr)->sun_path; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 120 | |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 121 | maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path)); |
| 122 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 123 | /* if the listener already has an fd assigned, then we were offered the |
| 124 | * fd by an external process (most likely the parent), and we don't want |
| 125 | * to create a new socket. However we still want to set a few flags on |
| 126 | * the socket. |
| 127 | */ |
| 128 | fd = listener->fd; |
| 129 | ext = (fd >= 0); |
| 130 | if (ext) |
| 131 | goto fd_ready; |
| 132 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 133 | if (path[0]) { |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 134 | ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid); |
Willy Tarreau | f28d5c9 | 2020-06-12 15:58:19 +0200 | [diff] [blame] | 135 | if (ret < 0 || ret >= sizeof(addr.sun_path)) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 136 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 137 | msg = "name too long for UNIX socket (limit usually 97)"; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 138 | goto err_return; |
| 139 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 140 | |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 141 | ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid); |
| 142 | if (ret < 0 || ret >= maxpathlen) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 143 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 144 | msg = "name too long for UNIX socket (limit usually 97)"; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 145 | goto err_return; |
| 146 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 147 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 148 | /* 2. clean existing orphaned entries */ |
| 149 | if (unlink(tempname) < 0 && errno != ENOENT) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 150 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 151 | msg = "error when trying to unlink previous UNIX socket"; |
| 152 | goto err_return; |
| 153 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 154 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 155 | if (unlink(backname) < 0 && errno != ENOENT) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 156 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 157 | msg = "error when trying to unlink previous UNIX socket"; |
| 158 | goto err_return; |
| 159 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 160 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 161 | /* 3. backup existing socket */ |
| 162 | if (link(path, backname) < 0 && errno != ENOENT) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 163 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 164 | msg = "error when trying to preserve previous UNIX socket"; |
| 165 | goto err_return; |
| 166 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 167 | |
Willy Tarreau | f28d5c9 | 2020-06-12 15:58:19 +0200 | [diff] [blame] | 168 | /* Note: this test is redundant with the snprintf one above and |
| 169 | * will never trigger, it's just added as the only way to shut |
| 170 | * gcc's painfully dumb warning about possibly truncated output |
| 171 | * during strncpy(). Don't move it above or smart gcc will not |
| 172 | * see it! |
| 173 | */ |
| 174 | if (strlen(tempname) >= sizeof(addr.sun_path)) { |
| 175 | err |= ERR_FATAL | ERR_ALERT; |
| 176 | msg = "name too long for UNIX socket (limit usually 97)"; |
| 177 | goto err_return; |
| 178 | } |
| 179 | |
Willy Tarreau | 719e07c | 2019-12-11 16:29:10 +0100 | [diff] [blame] | 180 | strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1); |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 181 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 182 | } |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 183 | else { |
| 184 | /* first char is zero, it's an abstract socket whose address |
| 185 | * is defined by all the bytes past this zero. |
| 186 | */ |
| 187 | memcpy(addr.sun_path, path, sizeof(addr.sun_path)); |
| 188 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 189 | addr.sun_family = AF_UNIX; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 190 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 191 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 192 | if (fd < 0) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 193 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 194 | msg = "cannot create UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 195 | goto err_unlink_back; |
| 196 | } |
| 197 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 198 | fd_ready: |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 199 | if (fd >= global.maxsock) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 200 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 201 | msg = "socket(): not enough free sockets, raise -n argument"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 202 | goto err_unlink_temp; |
| 203 | } |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 204 | |
| 205 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 206 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 207 | msg = "cannot make UNIX socket non-blocking"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 208 | goto err_unlink_temp; |
| 209 | } |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 210 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 211 | if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 212 | /* note that bind() creates the socket <tempname> on the file system */ |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 213 | if (errno == EADDRINUSE) { |
| 214 | /* the old process might still own it, let's retry */ |
| 215 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 216 | msg = "cannot listen to socket"; |
| 217 | } |
| 218 | else { |
| 219 | err |= ERR_FATAL | ERR_ALERT; |
| 220 | msg = "cannot bind UNIX socket"; |
| 221 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 222 | goto err_unlink_temp; |
| 223 | } |
| 224 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 225 | /* <uid> and <gid> different of -1 will be used to change the socket owner. |
| 226 | * If <mode> is not 0, it will be used to restrict access to the socket. |
| 227 | * While it is known not to be portable on every OS, it's still useful |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 228 | * where it works. We also don't change permissions on abstract sockets. |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 229 | */ |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 230 | if (!ext && path[0] && |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 231 | (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) && |
| 232 | (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) || |
| 233 | (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 234 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 235 | msg = "cannot change UNIX socket ownership"; |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 236 | goto err_unlink_temp; |
| 237 | } |
| 238 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 239 | ready = 0; |
| 240 | ready_len = sizeof(ready); |
| 241 | if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1) |
| 242 | ready = 0; |
| 243 | |
| 244 | if (!(ext && ready) && /* only listen if not already done by external process */ |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 245 | listen(fd, listener_backlog(listener)) < 0) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 246 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 247 | msg = "cannot listen to UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 248 | goto err_unlink_temp; |
| 249 | } |
| 250 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 251 | /* Point of no return: we are ready, we'll switch the sockets. We don't |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 252 | * fear losing the socket <path> because we have a copy of it in |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 253 | * backname. Abstract sockets are not renamed. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 254 | */ |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 255 | if (!ext && path[0] && rename(tempname, path) < 0) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 256 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 257 | msg = "cannot switch final and temporary UNIX sockets"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 258 | goto err_rename; |
| 259 | } |
| 260 | |
Willy Tarreau | 68986ab | 2017-06-16 10:34:20 +0200 | [diff] [blame] | 261 | /* Cleanup: only unlink if we didn't inherit the fd from the parent */ |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 262 | if (!ext && path[0]) |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 263 | unlink(backname); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 264 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 265 | /* the socket is now listening */ |
| 266 | listener->fd = fd; |
| 267 | listener->state = LI_LISTEN; |
| 268 | |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 269 | fd_insert(fd, listener, listener->proto->accept, |
Willy Tarreau | 0948a78 | 2020-02-12 10:15:34 +0100 | [diff] [blame] | 270 | thread_mask(listener->bind_conf->bind_thread) & all_threads_mask); |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 271 | |
Willy Tarreau | bb1caff | 2020-08-19 10:00:57 +0200 | [diff] [blame] | 272 | /* for now, all regularly bound UNIX listeners are exportable */ |
| 273 | if (!(listener->options & LI_O_INHERITED)) |
| 274 | fdtab[fd].exported = 1; |
| 275 | |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 276 | return err; |
| 277 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 278 | err_rename: |
| 279 | ret = rename(backname, path); |
| 280 | if (ret < 0 && errno == ENOENT) |
| 281 | unlink(path); |
| 282 | err_unlink_temp: |
Jan Seda | 7319b64 | 2014-06-26 20:44:05 +0200 | [diff] [blame] | 283 | if (!ext && path[0]) |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 284 | unlink(tempname); |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 285 | close(fd); |
| 286 | err_unlink_back: |
Jan Seda | 7319b64 | 2014-06-26 20:44:05 +0200 | [diff] [blame] | 287 | if (!ext && path[0]) |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 288 | unlink(backname); |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 289 | err_return: |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 290 | if (msg && errlen) { |
| 291 | if (!ext) |
| 292 | snprintf(errmsg, errlen, "%s [%s]", msg, path); |
| 293 | else |
| 294 | snprintf(errmsg, errlen, "%s [fd %d]", msg, fd); |
| 295 | } |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 296 | return err; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* This function closes the UNIX sockets for the specified listener. |
| 300 | * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE. |
| 301 | */ |
| 302 | static int uxst_unbind_listener(struct listener *listener) |
| 303 | { |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 304 | if (listener->state > LI_ASSIGNED) { |
| 305 | unbind_listener(listener); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 306 | } |
| 307 | return ERR_NONE; |
| 308 | } |
| 309 | |
Willy Tarreau | 3228238 | 2017-09-15 07:44:44 +0200 | [diff] [blame] | 310 | /* Add <listener> to the list of unix stream listeners (port is ignored). The |
| 311 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 312 | * The number of listeners for the protocol is updated. |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 313 | * |
| 314 | * Must be called with proto_lock held. |
| 315 | * |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 316 | */ |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 317 | static void uxst_add_listener(struct listener *listener, int port) |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 318 | { |
| 319 | if (listener->state != LI_INIT) |
| 320 | return; |
| 321 | listener->state = LI_ASSIGNED; |
| 322 | listener->proto = &proto_unix; |
| 323 | LIST_ADDQ(&proto_unix.listeners, &listener->proto_list); |
| 324 | proto_unix.nb_listeners++; |
| 325 | } |
| 326 | |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 327 | /* Pause a listener. Returns < 0 in case of failure, 0 if the listener |
| 328 | * was totally stopped, or > 0 if correctly paused. Nothing is done for |
| 329 | * plain unix sockets since currently it's the new process which handles |
| 330 | * the renaming. Abstract sockets are completely unbound. |
| 331 | */ |
Willy Tarreau | 3179489 | 2017-09-15 07:59:31 +0200 | [diff] [blame] | 332 | static int uxst_pause_listener(struct listener *l) |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 333 | { |
| 334 | if (((struct sockaddr_un *)&l->addr)->sun_path[0]) |
| 335 | return 1; |
| 336 | |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 337 | /* Listener's lock already held. Call lockless version of |
| 338 | * unbind_listener. */ |
| 339 | do_unbind_listener(l, 1); |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 343 | |
| 344 | /* |
| 345 | * This function initiates a UNIX connection establishment to the target assigned |
Willy Tarreau | ca9f5a9 | 2019-07-17 16:40:37 +0200 | [diff] [blame] | 346 | * to connection <conn> using (si->{target,dst}). The source address is ignored |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 347 | * and will be selected by the system. conn->target may point either to a valid |
| 348 | * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and |
| 349 | * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating |
| 350 | * whether there are data waiting for being sent or not, in order to adjust data |
| 351 | * write polling and on some platforms. The <delack> argument is ignored. |
| 352 | * |
| 353 | * Note that a pending send_proxy message accounts for data. |
| 354 | * |
| 355 | * It can return one of : |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 356 | * - SF_ERR_NONE if everything's OK |
| 357 | * - SF_ERR_SRVTO if there are no more servers |
| 358 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 359 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 360 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 361 | * - SF_ERR_INTERNAL for any other purely internal errors |
Tim Düsterhus | 4896c44 | 2016-11-29 02:15:19 +0100 | [diff] [blame] | 362 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 363 | * |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 364 | * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 365 | * it's invalid and the caller has nothing to do. |
| 366 | */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 367 | static int uxst_connect_server(struct connection *conn, int flags) |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 368 | { |
| 369 | int fd; |
| 370 | struct server *srv; |
| 371 | struct proxy *be; |
| 372 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 373 | switch (obj_type(conn->target)) { |
| 374 | case OBJ_TYPE_PROXY: |
| 375 | be = objt_proxy(conn->target); |
| 376 | srv = NULL; |
| 377 | break; |
| 378 | case OBJ_TYPE_SERVER: |
| 379 | srv = objt_server(conn->target); |
| 380 | be = srv->proxy; |
| 381 | break; |
| 382 | default: |
| 383 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 384 | return SF_ERR_INTERNAL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 385 | } |
| 386 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 387 | if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 388 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 389 | |
| 390 | if (errno == ENFILE) { |
| 391 | conn->err_code = CO_ER_SYS_FDLIM; |
| 392 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 393 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 394 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 395 | } |
| 396 | else if (errno == EMFILE) { |
| 397 | conn->err_code = CO_ER_PROC_FDLIM; |
| 398 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 399 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 400 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 401 | } |
| 402 | else if (errno == ENOBUFS || errno == ENOMEM) { |
| 403 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 404 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 405 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 406 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 407 | } |
| 408 | else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) { |
| 409 | conn->err_code = CO_ER_NOPROTO; |
| 410 | } |
| 411 | else |
| 412 | conn->err_code = CO_ER_SOCK_ERR; |
| 413 | |
| 414 | /* this is a resource error */ |
| 415 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 416 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | if (fd >= global.maxsock) { |
| 420 | /* do not log anything there, it's a normal condition when this option |
| 421 | * is used to serialize connections to a server ! |
| 422 | */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 423 | ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 424 | close(fd); |
| 425 | conn->err_code = CO_ER_CONF_FDLIM; |
| 426 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 427 | return SF_ERR_PRXCOND; /* it is a configuration limit */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
| 431 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 432 | close(fd); |
| 433 | conn->err_code = CO_ER_SOCK_ERR; |
| 434 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 435 | return SF_ERR_INTERNAL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 436 | } |
| 437 | |
William Lallemand | c03eb01 | 2018-11-27 12:02:37 +0100 | [diff] [blame] | 438 | if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) { |
| 439 | ha_alert("Cannot set CLOEXEC on client socket.\n"); |
| 440 | close(fd); |
| 441 | conn->err_code = CO_ER_SOCK_ERR; |
| 442 | conn->flags |= CO_FL_ERROR; |
| 443 | return SF_ERR_INTERNAL; |
| 444 | } |
| 445 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 446 | /* if a send_proxy is there, there are data */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 447 | if (conn->send_proxy_ofs) |
| 448 | flags |= CONNECT_HAS_DATA; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 449 | |
| 450 | if (global.tune.server_sndbuf) |
| 451 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 452 | |
| 453 | if (global.tune.server_rcvbuf) |
| 454 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 455 | |
Willy Tarreau | ca9f5a9 | 2019-07-17 16:40:37 +0200 | [diff] [blame] | 456 | if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) { |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 457 | if (errno == EINPROGRESS || errno == EALREADY) { |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 458 | conn->flags |= CO_FL_WAIT_L4_CONN; |
| 459 | } |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 460 | else if (errno == EISCONN) { |
| 461 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 462 | } |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 463 | else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) { |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 464 | char *msg; |
| 465 | if (errno == EAGAIN || errno == EADDRNOTAVAIL) { |
Lukas Tribus | 9f256d4 | 2016-01-26 20:33:14 +0100 | [diff] [blame] | 466 | msg = "can't connect to destination unix socket, check backlog size on the server"; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 467 | conn->err_code = CO_ER_FREE_PORTS; |
| 468 | } |
| 469 | else { |
| 470 | msg = "local address already in use"; |
| 471 | conn->err_code = CO_ER_ADDR_INUSE; |
| 472 | } |
| 473 | |
| 474 | qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg); |
| 475 | close(fd); |
| 476 | send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg); |
| 477 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 478 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 479 | } |
| 480 | else if (errno == ETIMEDOUT) { |
| 481 | close(fd); |
| 482 | conn->err_code = CO_ER_SOCK_ERR; |
| 483 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 484 | return SF_ERR_SRVTO; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 485 | } |
| 486 | else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 487 | close(fd); |
| 488 | conn->err_code = CO_ER_SOCK_ERR; |
| 489 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 490 | return SF_ERR_SRVCL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 491 | } |
| 492 | } |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 493 | else { |
| 494 | /* connect() already succeeded, which is quite usual for unix |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 495 | * sockets. Let's avoid a second connect() probe to complete it. |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 496 | */ |
| 497 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 498 | } |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 499 | |
| 500 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 501 | |
| 502 | /* Prepare to send a few handshakes related to the on-wire protocol. */ |
| 503 | if (conn->send_proxy_ofs) |
| 504 | conn->flags |= CO_FL_SEND_PROXY; |
| 505 | |
| 506 | conn_ctrl_init(conn); /* registers the FD */ |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 507 | fdtab[fd].linger_risk = 0; /* no need to disable lingering */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 508 | |
Willy Tarreau | 4c69cff | 2020-03-04 16:38:00 +0100 | [diff] [blame] | 509 | if (conn->flags & CO_FL_WAIT_L4_CONN) { |
| 510 | fd_want_send(fd); |
| 511 | fd_cant_send(fd); |
Willy Tarreau | 8dbd1a2 | 2020-07-31 08:59:09 +0200 | [diff] [blame] | 512 | fd_cant_recv(fd); |
Willy Tarreau | 4c69cff | 2020-03-04 16:38:00 +0100 | [diff] [blame] | 513 | } |
Willy Tarreau | ccf3f6d | 2019-09-05 17:05:05 +0200 | [diff] [blame] | 514 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 515 | if (conn_xprt_init(conn) < 0) { |
Willy Tarreau | 8c82901 | 2017-10-05 18:02:11 +0200 | [diff] [blame] | 516 | conn_full_close(conn); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 517 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 518 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 519 | } |
| 520 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 521 | return SF_ERR_NONE; /* connection is OK */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 525 | /******************************** |
| 526 | * 3) protocol-oriented functions |
| 527 | ********************************/ |
| 528 | |
| 529 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 530 | /* This function creates all UNIX sockets bound to the protocol entry <proto>. |
| 531 | * It is intended to be used as the protocol's bind_all() function. |
| 532 | * The sockets will be registered but not added to any fd_set, in order not to |
| 533 | * loose them across the fork(). A call to uxst_enable_listeners() is needed |
| 534 | * to complete initialization. |
| 535 | * |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 536 | * Must be called with proto_lock held. |
| 537 | * |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 538 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 539 | */ |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 540 | static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 541 | { |
| 542 | struct listener *listener; |
| 543 | int err = ERR_NONE; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 544 | |
| 545 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 546 | err |= uxst_bind_listener(listener, errmsg, errlen); |
| 547 | if (err & ERR_ABORT) |
| 548 | break; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 549 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 550 | return err; |
| 551 | } |
| 552 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 553 | |
| 554 | /* This function stops all listening UNIX sockets bound to the protocol |
| 555 | * <proto>. It does not detaches them from the protocol. |
| 556 | * It always returns ERR_NONE. |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 557 | * |
| 558 | * Must be called with proto_lock held. |
| 559 | * |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 560 | */ |
| 561 | static int uxst_unbind_listeners(struct protocol *proto) |
| 562 | { |
| 563 | struct listener *listener; |
| 564 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 565 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 566 | uxst_unbind_listener(listener); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 567 | return ERR_NONE; |
| 568 | } |
| 569 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 570 | /* |
| 571 | * Local variables: |
| 572 | * c-indent-level: 8 |
| 573 | * c-basic-offset: 8 |
| 574 | * End: |
| 575 | */ |