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