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> |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 16 | #include <pwd.h> |
| 17 | #include <grp.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <syslog.h> |
| 22 | #include <time.h> |
| 23 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 24 | #include <sys/socket.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/un.h> |
| 28 | |
| 29 | #include <common/compat.h> |
| 30 | #include <common/config.h> |
| 31 | #include <common/debug.h> |
Willy Tarreau | d740bab | 2007-10-28 11:14:07 +0100 | [diff] [blame] | 32 | #include <common/errors.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 33 | #include <common/mini-clist.h> |
| 34 | #include <common/standard.h> |
| 35 | #include <common/time.h> |
| 36 | #include <common/version.h> |
| 37 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 38 | #include <types/global.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 39 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 40 | #include <proto/connection.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 41 | #include <proto/fd.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 42 | #include <proto/listener.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 43 | #include <proto/log.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 44 | #include <proto/protocol.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 45 | #include <proto/task.h> |
| 46 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 47 | static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen); |
| 48 | static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 49 | static int uxst_unbind_listeners(struct protocol *proto); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 50 | static int uxst_connect_server(struct connection *conn, int data, int delack); |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 51 | static void uxst_add_listener(struct listener *listener, int port); |
Willy Tarreau | 3179489 | 2017-09-15 07:59:31 +0200 | [diff] [blame] | 52 | static int uxst_pause_listener(struct listener *l); |
| 53 | static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir); |
| 54 | static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 55 | |
| 56 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 57 | static struct protocol proto_unix = { |
| 58 | .name = "unix_stream", |
| 59 | .sock_domain = PF_UNIX, |
| 60 | .sock_type = SOCK_STREAM, |
| 61 | .sock_prot = 0, |
| 62 | .sock_family = AF_UNIX, |
| 63 | .sock_addrlen = sizeof(struct sockaddr_un), |
| 64 | .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 65 | .accept = &listener_accept, |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 66 | .connect = &uxst_connect_server, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 67 | .bind = uxst_bind_listener, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 68 | .bind_all = uxst_bind_listeners, |
| 69 | .unbind_all = uxst_unbind_listeners, |
| 70 | .enable_all = enable_all_listeners, |
| 71 | .disable_all = disable_all_listeners, |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 72 | .get_src = uxst_get_src, |
| 73 | .get_dst = uxst_get_dst, |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 74 | .pause = uxst_pause_listener, |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 75 | .add = uxst_add_listener, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 76 | .listeners = LIST_HEAD_INIT(proto_unix.listeners), |
| 77 | .nb_listeners = 0, |
| 78 | }; |
| 79 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 80 | /******************************** |
| 81 | * 1) low-level socket functions |
| 82 | ********************************/ |
| 83 | |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 84 | /* |
| 85 | * Retrieves the source address for the socket <fd>, with <dir> indicating |
| 86 | * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of |
| 87 | * success, -1 in case of error. The socket's source address is stored in |
| 88 | * <sa> for <salen> bytes. |
| 89 | */ |
Willy Tarreau | 3179489 | 2017-09-15 07:59:31 +0200 | [diff] [blame] | 90 | static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 91 | { |
| 92 | if (dir) |
| 93 | return getsockname(fd, sa, &salen); |
| 94 | else |
| 95 | return getpeername(fd, sa, &salen); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /* |
| 100 | * Retrieves the original destination address for the socket <fd>, with <dir> |
| 101 | * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in |
| 102 | * case of success, -1 in case of error. The socket's source address is stored |
| 103 | * in <sa> for <salen> bytes. |
| 104 | */ |
Willy Tarreau | 3179489 | 2017-09-15 07:59:31 +0200 | [diff] [blame] | 105 | static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 106 | { |
| 107 | if (dir) |
| 108 | return getpeername(fd, sa, &salen); |
| 109 | else |
| 110 | return getsockname(fd, sa, &salen); |
| 111 | } |
| 112 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 113 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 114 | /******************************** |
| 115 | * 2) listener-oriented functions |
| 116 | ********************************/ |
| 117 | |
| 118 | |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 119 | static int uxst_find_compatible_fd(struct listener *l) |
| 120 | { |
| 121 | struct xfer_sock_list *xfer_sock = xfer_sock_list; |
| 122 | int ret = -1; |
| 123 | |
| 124 | while (xfer_sock) { |
| 125 | struct sockaddr_un *un1 = (void *)&l->addr; |
| 126 | struct sockaddr_un *un2 = (void *)&xfer_sock->addr; |
| 127 | |
| 128 | /* |
| 129 | * The bound socket's path as returned by getsockaddr |
| 130 | * will be the temporary name <sockname>.XXXXX.tmp, |
| 131 | * so we can't just compare the two names |
| 132 | */ |
| 133 | if (xfer_sock->addr.ss_family == AF_UNIX && |
| 134 | strncmp(un1->sun_path, un2->sun_path, |
| 135 | strlen(un1->sun_path)) == 0) { |
| 136 | char *after_sockname = un2->sun_path + |
| 137 | strlen(un1->sun_path); |
| 138 | /* Make a reasonnable effort to check that |
| 139 | * it is indeed a haproxy-generated temporary |
| 140 | * name, it's not perfect, but probably good enough. |
| 141 | */ |
| 142 | if (after_sockname[0] == '.') { |
| 143 | after_sockname++; |
| 144 | while (after_sockname[0] >= '0' && |
| 145 | after_sockname[0] <= '9') |
| 146 | after_sockname++; |
| 147 | if (!strcmp(after_sockname, ".tmp")) |
| 148 | break; |
Olivier Houchard | b4dd15b | 2018-06-06 18:34:34 +0200 | [diff] [blame] | 149 | /* abns sockets sun_path starts with a \0 */ |
| 150 | } else if (un1->sun_path[0] == 0 |
| 151 | && un2->sun_path[0] == 0 |
| 152 | && !memcmp(&un1->sun_path[1], &un2->sun_path[1], |
| 153 | sizeof(un1->sun_path) - 1)) |
| 154 | break; |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 155 | } |
| 156 | xfer_sock = xfer_sock->next; |
| 157 | } |
| 158 | if (xfer_sock != NULL) { |
| 159 | ret = xfer_sock->fd; |
| 160 | if (xfer_sock == xfer_sock_list) |
| 161 | xfer_sock_list = xfer_sock->next; |
| 162 | if (xfer_sock->prev) |
| 163 | xfer_sock->prev->next = xfer_sock->next; |
| 164 | if (xfer_sock->next) |
Olivier Houchard | ec9516a | 2018-03-08 18:25:49 +0100 | [diff] [blame] | 165 | xfer_sock->next->prev = xfer_sock->prev; |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 166 | free(xfer_sock); |
| 167 | } |
| 168 | return ret; |
| 169 | |
| 170 | } |
| 171 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 172 | /* This function creates a UNIX socket associated to the listener. It changes |
| 173 | * 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] | 174 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It |
| 175 | * may return a warning or an error message in <errmsg> if the message is at |
| 176 | * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if |
| 177 | * <errlen> is also zero. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 178 | */ |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 179 | static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 180 | { |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 181 | int fd; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 182 | char tempname[MAXPATHLEN]; |
| 183 | char backname[MAXPATHLEN]; |
| 184 | struct sockaddr_un addr; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 185 | const char *msg = NULL; |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 186 | const char *path; |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 187 | int ext, ready; |
| 188 | socklen_t ready_len; |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 189 | int err; |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 190 | int ret; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 191 | |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 192 | err = ERR_NONE; |
| 193 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 194 | /* ensure we never return garbage */ |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 195 | if (errlen) |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 196 | *errmsg = 0; |
| 197 | |
| 198 | if (listener->state != LI_ASSIGNED) |
| 199 | return ERR_NONE; /* already bound */ |
| 200 | |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 201 | if (listener->fd == -1) |
| 202 | listener->fd = uxst_find_compatible_fd(listener); |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 203 | path = ((struct sockaddr_un *)&listener->addr)->sun_path; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 204 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 205 | /* if the listener already has an fd assigned, then we were offered the |
| 206 | * fd by an external process (most likely the parent), and we don't want |
| 207 | * to create a new socket. However we still want to set a few flags on |
| 208 | * the socket. |
| 209 | */ |
| 210 | fd = listener->fd; |
| 211 | ext = (fd >= 0); |
| 212 | if (ext) |
| 213 | goto fd_ready; |
| 214 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 215 | if (path[0]) { |
| 216 | ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid); |
| 217 | if (ret < 0 || ret >= MAXPATHLEN) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 218 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 219 | msg = "name too long for UNIX socket"; |
| 220 | goto err_return; |
| 221 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 222 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 223 | ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid); |
| 224 | if (ret < 0 || ret >= MAXPATHLEN) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 225 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 226 | msg = "name too long for UNIX socket"; |
| 227 | goto err_return; |
| 228 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 229 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 230 | /* 2. clean existing orphaned entries */ |
| 231 | if (unlink(tempname) < 0 && errno != ENOENT) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 232 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 233 | msg = "error when trying to unlink previous UNIX socket"; |
| 234 | goto err_return; |
| 235 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 236 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 237 | if (unlink(backname) < 0 && errno != ENOENT) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 238 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 239 | msg = "error when trying to unlink previous UNIX socket"; |
| 240 | goto err_return; |
| 241 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 242 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 243 | /* 3. backup existing socket */ |
| 244 | if (link(path, backname) < 0 && errno != ENOENT) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 245 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 246 | msg = "error when trying to preserve previous UNIX socket"; |
| 247 | goto err_return; |
| 248 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 249 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 250 | strncpy(addr.sun_path, tempname, sizeof(addr.sun_path)); |
| 251 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 252 | } |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 253 | else { |
| 254 | /* first char is zero, it's an abstract socket whose address |
| 255 | * is defined by all the bytes past this zero. |
| 256 | */ |
| 257 | memcpy(addr.sun_path, path, sizeof(addr.sun_path)); |
| 258 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 259 | addr.sun_family = AF_UNIX; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 260 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 261 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 262 | if (fd < 0) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 263 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 264 | msg = "cannot create UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 265 | goto err_unlink_back; |
| 266 | } |
| 267 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 268 | fd_ready: |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 269 | if (fd >= global.maxsock) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 270 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 271 | msg = "socket(): not enough free sockets, raise -n argument"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 272 | goto err_unlink_temp; |
| 273 | } |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 274 | |
| 275 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 276 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 277 | msg = "cannot make UNIX socket non-blocking"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 278 | goto err_unlink_temp; |
| 279 | } |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 280 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 281 | if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 282 | /* note that bind() creates the socket <tempname> on the file system */ |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 283 | if (errno == EADDRINUSE) { |
| 284 | /* the old process might still own it, let's retry */ |
| 285 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 286 | msg = "cannot listen to socket"; |
| 287 | } |
| 288 | else { |
| 289 | err |= ERR_FATAL | ERR_ALERT; |
| 290 | msg = "cannot bind UNIX socket"; |
| 291 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 292 | goto err_unlink_temp; |
| 293 | } |
| 294 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 295 | /* <uid> and <gid> different of -1 will be used to change the socket owner. |
| 296 | * If <mode> is not 0, it will be used to restrict access to the socket. |
| 297 | * 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] | 298 | * where it works. We also don't change permissions on abstract sockets. |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 299 | */ |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 300 | if (!ext && path[0] && |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 301 | (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) && |
| 302 | (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) || |
| 303 | (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] | 304 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 305 | msg = "cannot change UNIX socket ownership"; |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 306 | goto err_unlink_temp; |
| 307 | } |
| 308 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 309 | ready = 0; |
| 310 | ready_len = sizeof(ready); |
| 311 | if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1) |
| 312 | ready = 0; |
| 313 | |
| 314 | if (!(ext && ready) && /* only listen if not already done by external process */ |
| 315 | listen(fd, listener->backlog ? listener->backlog : listener->maxconn) < 0) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 316 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 317 | msg = "cannot listen to UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 318 | goto err_unlink_temp; |
| 319 | } |
| 320 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 321 | /* Point of no return: we are ready, we'll switch the sockets. We don't |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 322 | * fear loosing the socket <path> because we have a copy of it in |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 323 | * backname. Abstract sockets are not renamed. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 324 | */ |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 325 | if (!ext && path[0] && rename(tempname, path) < 0) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 326 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 327 | msg = "cannot switch final and temporary UNIX sockets"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 328 | goto err_rename; |
| 329 | } |
| 330 | |
Willy Tarreau | 68986ab | 2017-06-16 10:34:20 +0200 | [diff] [blame] | 331 | /* 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] | 332 | if (!ext && path[0]) |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 333 | unlink(backname); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 334 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 335 | /* the socket is now listening */ |
| 336 | listener->fd = fd; |
| 337 | listener->state = LI_LISTEN; |
| 338 | |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 339 | fd_insert(fd, listener, listener->proto->accept, |
| 340 | listener->bind_conf->bind_thread[relative_pid-1] ? |
| 341 | listener->bind_conf->bind_thread[relative_pid-1] : MAX_THREADS_MASK); |
| 342 | |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 343 | return err; |
| 344 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 345 | err_rename: |
| 346 | ret = rename(backname, path); |
| 347 | if (ret < 0 && errno == ENOENT) |
| 348 | unlink(path); |
| 349 | err_unlink_temp: |
Jan Seda | 7319b64 | 2014-06-26 20:44:05 +0200 | [diff] [blame] | 350 | if (!ext && path[0]) |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 351 | unlink(tempname); |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 352 | close(fd); |
| 353 | err_unlink_back: |
Jan Seda | 7319b64 | 2014-06-26 20:44:05 +0200 | [diff] [blame] | 354 | if (!ext && path[0]) |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 355 | unlink(backname); |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 356 | err_return: |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 357 | if (msg && errlen) { |
| 358 | if (!ext) |
| 359 | snprintf(errmsg, errlen, "%s [%s]", msg, path); |
| 360 | else |
| 361 | snprintf(errmsg, errlen, "%s [fd %d]", msg, fd); |
| 362 | } |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 363 | return err; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /* This function closes the UNIX sockets for the specified listener. |
| 367 | * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE. |
| 368 | */ |
| 369 | static int uxst_unbind_listener(struct listener *listener) |
| 370 | { |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 371 | if (listener->state > LI_ASSIGNED) { |
| 372 | unbind_listener(listener); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 373 | } |
| 374 | return ERR_NONE; |
| 375 | } |
| 376 | |
Willy Tarreau | 3228238 | 2017-09-15 07:44:44 +0200 | [diff] [blame] | 377 | /* Add <listener> to the list of unix stream listeners (port is ignored). The |
| 378 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 379 | * The number of listeners for the protocol is updated. |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 380 | */ |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 381 | static void uxst_add_listener(struct listener *listener, int port) |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 382 | { |
| 383 | if (listener->state != LI_INIT) |
| 384 | return; |
| 385 | listener->state = LI_ASSIGNED; |
| 386 | listener->proto = &proto_unix; |
| 387 | LIST_ADDQ(&proto_unix.listeners, &listener->proto_list); |
| 388 | proto_unix.nb_listeners++; |
| 389 | } |
| 390 | |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 391 | /* Pause a listener. Returns < 0 in case of failure, 0 if the listener |
| 392 | * was totally stopped, or > 0 if correctly paused. Nothing is done for |
| 393 | * plain unix sockets since currently it's the new process which handles |
| 394 | * the renaming. Abstract sockets are completely unbound. |
| 395 | */ |
Willy Tarreau | 3179489 | 2017-09-15 07:59:31 +0200 | [diff] [blame] | 396 | static int uxst_pause_listener(struct listener *l) |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 397 | { |
| 398 | if (((struct sockaddr_un *)&l->addr)->sun_path[0]) |
| 399 | return 1; |
| 400 | |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 401 | /* Listener's lock already held. Call lockless version of |
| 402 | * unbind_listener. */ |
| 403 | do_unbind_listener(l, 1); |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 407 | |
| 408 | /* |
| 409 | * This function initiates a UNIX connection establishment to the target assigned |
| 410 | * to connection <conn> using (si->{target,addr.to}). The source address is ignored |
| 411 | * and will be selected by the system. conn->target may point either to a valid |
| 412 | * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and |
| 413 | * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating |
| 414 | * whether there are data waiting for being sent or not, in order to adjust data |
| 415 | * write polling and on some platforms. The <delack> argument is ignored. |
| 416 | * |
| 417 | * Note that a pending send_proxy message accounts for data. |
| 418 | * |
| 419 | * It can return one of : |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 420 | * - SF_ERR_NONE if everything's OK |
| 421 | * - SF_ERR_SRVTO if there are no more servers |
| 422 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 423 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 424 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 425 | * - SF_ERR_INTERNAL for any other purely internal errors |
Tim Düsterhus | 4896c44 | 2016-11-29 02:15:19 +0100 | [diff] [blame] | 426 | * 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] | 427 | * |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 428 | * 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] | 429 | * it's invalid and the caller has nothing to do. |
| 430 | */ |
Willy Tarreau | 3179489 | 2017-09-15 07:59:31 +0200 | [diff] [blame] | 431 | static int uxst_connect_server(struct connection *conn, int data, int delack) |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 432 | { |
| 433 | int fd; |
| 434 | struct server *srv; |
| 435 | struct proxy *be; |
| 436 | |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 437 | conn->flags = 0; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 438 | |
| 439 | switch (obj_type(conn->target)) { |
| 440 | case OBJ_TYPE_PROXY: |
| 441 | be = objt_proxy(conn->target); |
| 442 | srv = NULL; |
| 443 | break; |
| 444 | case OBJ_TYPE_SERVER: |
| 445 | srv = objt_server(conn->target); |
| 446 | be = srv->proxy; |
| 447 | break; |
| 448 | default: |
| 449 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 450 | return SF_ERR_INTERNAL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 453 | if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 454 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 455 | |
| 456 | if (errno == ENFILE) { |
| 457 | conn->err_code = CO_ER_SYS_FDLIM; |
| 458 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 459 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 460 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 461 | } |
| 462 | else if (errno == EMFILE) { |
| 463 | conn->err_code = CO_ER_PROC_FDLIM; |
| 464 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 465 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 466 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 467 | } |
| 468 | else if (errno == ENOBUFS || errno == ENOMEM) { |
| 469 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 470 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 471 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 472 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 473 | } |
| 474 | else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) { |
| 475 | conn->err_code = CO_ER_NOPROTO; |
| 476 | } |
| 477 | else |
| 478 | conn->err_code = CO_ER_SOCK_ERR; |
| 479 | |
| 480 | /* this is a resource error */ |
| 481 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 482 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | if (fd >= global.maxsock) { |
| 486 | /* do not log anything there, it's a normal condition when this option |
| 487 | * is used to serialize connections to a server ! |
| 488 | */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 489 | 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] | 490 | close(fd); |
| 491 | conn->err_code = CO_ER_CONF_FDLIM; |
| 492 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 493 | return SF_ERR_PRXCOND; /* it is a configuration limit */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
| 497 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 498 | close(fd); |
| 499 | conn->err_code = CO_ER_SOCK_ERR; |
| 500 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 501 | return SF_ERR_INTERNAL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* if a send_proxy is there, there are data */ |
| 505 | data |= conn->send_proxy_ofs; |
| 506 | |
| 507 | if (global.tune.server_sndbuf) |
| 508 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 509 | |
| 510 | if (global.tune.server_rcvbuf) |
| 511 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 512 | |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 513 | if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) { |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 514 | if (errno == EINPROGRESS || errno == EALREADY) { |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 515 | conn->flags |= CO_FL_WAIT_L4_CONN; |
| 516 | } |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 517 | else if (errno == EISCONN) { |
| 518 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 519 | } |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 520 | else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) { |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 521 | char *msg; |
| 522 | if (errno == EAGAIN || errno == EADDRNOTAVAIL) { |
Lukas Tribus | 9f256d4 | 2016-01-26 20:33:14 +0100 | [diff] [blame] | 523 | 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] | 524 | conn->err_code = CO_ER_FREE_PORTS; |
| 525 | } |
| 526 | else { |
| 527 | msg = "local address already in use"; |
| 528 | conn->err_code = CO_ER_ADDR_INUSE; |
| 529 | } |
| 530 | |
| 531 | qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg); |
| 532 | close(fd); |
| 533 | send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg); |
| 534 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 535 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 536 | } |
| 537 | else if (errno == ETIMEDOUT) { |
| 538 | close(fd); |
| 539 | conn->err_code = CO_ER_SOCK_ERR; |
| 540 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 541 | return SF_ERR_SRVTO; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 542 | } |
| 543 | else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 544 | close(fd); |
| 545 | conn->err_code = CO_ER_SOCK_ERR; |
| 546 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 547 | return SF_ERR_SRVCL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 548 | } |
| 549 | } |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 550 | else { |
| 551 | /* connect() already succeeded, which is quite usual for unix |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 552 | * sockets. Let's avoid a second connect() probe to complete it. |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 553 | */ |
| 554 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 555 | } |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 556 | |
| 557 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 558 | |
| 559 | /* Prepare to send a few handshakes related to the on-wire protocol. */ |
| 560 | if (conn->send_proxy_ofs) |
| 561 | conn->flags |= CO_FL_SEND_PROXY; |
| 562 | |
| 563 | conn_ctrl_init(conn); /* registers the FD */ |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 564 | fdtab[fd].linger_risk = 0; /* no need to disable lingering */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 565 | |
| 566 | if (conn_xprt_init(conn) < 0) { |
Willy Tarreau | 8c82901 | 2017-10-05 18:02:11 +0200 | [diff] [blame] | 567 | conn_full_close(conn); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 568 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 569 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 572 | if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_L4_CONN)) { |
| 573 | conn_sock_want_send(conn); /* for connect status, proxy protocol or SSL */ |
| 574 | } |
| 575 | else { |
| 576 | /* If there's no more handshake, we need to notify the data |
| 577 | * layer when the connection is already OK otherwise we'll have |
| 578 | * no other opportunity to do it later (eg: health checks). |
| 579 | */ |
| 580 | data = 1; |
| 581 | } |
| 582 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 583 | if (data) |
Olivier Houchard | 1a0545f | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 584 | conn_xprt_want_send(conn); /* prepare to send data if any */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 585 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 586 | return SF_ERR_NONE; /* connection is OK */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 590 | /******************************** |
| 591 | * 3) protocol-oriented functions |
| 592 | ********************************/ |
| 593 | |
| 594 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 595 | /* This function creates all UNIX sockets bound to the protocol entry <proto>. |
| 596 | * It is intended to be used as the protocol's bind_all() function. |
| 597 | * The sockets will be registered but not added to any fd_set, in order not to |
| 598 | * loose them across the fork(). A call to uxst_enable_listeners() is needed |
| 599 | * to complete initialization. |
| 600 | * |
| 601 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 602 | */ |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 603 | static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 604 | { |
| 605 | struct listener *listener; |
| 606 | int err = ERR_NONE; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 607 | |
| 608 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 609 | err |= uxst_bind_listener(listener, errmsg, errlen); |
| 610 | if (err & ERR_ABORT) |
| 611 | break; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 612 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 613 | return err; |
| 614 | } |
| 615 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 616 | |
| 617 | /* This function stops all listening UNIX sockets bound to the protocol |
| 618 | * <proto>. It does not detaches them from the protocol. |
| 619 | * It always returns ERR_NONE. |
| 620 | */ |
| 621 | static int uxst_unbind_listeners(struct protocol *proto) |
| 622 | { |
| 623 | struct listener *listener; |
| 624 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 625 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 626 | uxst_unbind_listener(listener); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 627 | return ERR_NONE; |
| 628 | } |
| 629 | |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 630 | /* parse the "mode" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 631 | static int bind_parse_mode(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 632 | { |
Willy Tarreau | a1a247b | 2017-10-04 14:43:44 +0200 | [diff] [blame] | 633 | char *endptr; |
| 634 | |
| 635 | conf->ux.mode = strtol(args[cur_arg + 1], &endptr, 8); |
| 636 | |
| 637 | if (!*args[cur_arg + 1] || *endptr) { |
| 638 | memprintf(err, "'%s' : missing or invalid mode '%s' (octal integer expected)", args[cur_arg], args[cur_arg + 1]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 639 | return ERR_ALERT | ERR_FATAL; |
| 640 | } |
| 641 | |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | /* parse the "gid" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 646 | static int bind_parse_gid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 647 | { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 648 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 649 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 650 | return ERR_ALERT | ERR_FATAL; |
| 651 | } |
| 652 | |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 653 | conf->ux.gid = atol(args[cur_arg + 1]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | /* parse the "group" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 658 | static int bind_parse_group(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 659 | { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 660 | struct group *group; |
| 661 | |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 662 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 663 | memprintf(err, "'%s' : missing group name", args[cur_arg]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 664 | return ERR_ALERT | ERR_FATAL; |
| 665 | } |
| 666 | |
| 667 | group = getgrnam(args[cur_arg + 1]); |
| 668 | if (!group) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 669 | memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 670 | return ERR_ALERT | ERR_FATAL; |
| 671 | } |
| 672 | |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 673 | conf->ux.gid = group->gr_gid; |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /* parse the "uid" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 678 | static int bind_parse_uid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 679 | { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 680 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 681 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 682 | return ERR_ALERT | ERR_FATAL; |
| 683 | } |
| 684 | |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 685 | conf->ux.uid = atol(args[cur_arg + 1]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | /* parse the "user" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 690 | static int bind_parse_user(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 691 | { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 692 | struct passwd *user; |
| 693 | |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 694 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 695 | memprintf(err, "'%s' : missing user name", args[cur_arg]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 696 | return ERR_ALERT | ERR_FATAL; |
| 697 | } |
| 698 | |
| 699 | user = getpwnam(args[cur_arg + 1]); |
| 700 | if (!user) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 701 | memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 702 | return ERR_ALERT | ERR_FATAL; |
| 703 | } |
| 704 | |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 705 | conf->ux.uid = user->pw_uid; |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | /* Note: must not be declared <const> as its list will be overwritten. |
| 710 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 711 | * all code contributors. |
| 712 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 713 | * the config parser can report an appropriate error when a known keyword was |
| 714 | * not enabled. |
| 715 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 716 | static struct bind_kw_list bind_kws = { "UNIX", { }, { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 717 | { "gid", bind_parse_gid, 1 }, /* set the socket's gid */ |
| 718 | { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */ |
| 719 | { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/ |
| 720 | { "uid", bind_parse_uid, 1 }, /* set the socket's uid */ |
| 721 | { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */ |
| 722 | { NULL, NULL, 0 }, |
| 723 | }}; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 724 | |
| 725 | /******************************** |
| 726 | * 4) high-level functions |
| 727 | ********************************/ |
| 728 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 729 | __attribute__((constructor)) |
| 730 | static void __uxst_protocol_init(void) |
| 731 | { |
| 732 | protocol_register(&proto_unix); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 733 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | |
| 737 | /* |
| 738 | * Local variables: |
| 739 | * c-indent-level: 8 |
| 740 | * c-basic-offset: 8 |
| 741 | * End: |
| 742 | */ |