Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * UNIX SOCK_STREAM protocol layer (uxst) |
| 3 | * |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
| 20 | #include <time.h> |
| 21 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/un.h> |
| 26 | |
| 27 | #include <common/compat.h> |
| 28 | #include <common/config.h> |
| 29 | #include <common/debug.h> |
Willy Tarreau | d740bab | 2007-10-28 11:14:07 +0100 | [diff] [blame] | 30 | #include <common/errors.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 31 | #include <common/mini-clist.h> |
| 32 | #include <common/standard.h> |
| 33 | #include <common/time.h> |
| 34 | #include <common/version.h> |
| 35 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 36 | #include <types/global.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 38 | #include <proto/fd.h> |
| 39 | #include <proto/log.h> |
| 40 | #include <proto/protocols.h> |
| 41 | #include <proto/proto_uxst.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 42 | #include <proto/stream_sock.h> |
| 43 | #include <proto/task.h> |
| 44 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 45 | static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen); |
| 46 | static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 47 | static int uxst_unbind_listeners(struct protocol *proto); |
| 48 | |
| 49 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 50 | static struct protocol proto_unix = { |
| 51 | .name = "unix_stream", |
| 52 | .sock_domain = PF_UNIX, |
| 53 | .sock_type = SOCK_STREAM, |
| 54 | .sock_prot = 0, |
| 55 | .sock_family = AF_UNIX, |
| 56 | .sock_addrlen = sizeof(struct sockaddr_un), |
| 57 | .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */ |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 58 | .accept = &stream_sock_accept, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 59 | .read = &stream_sock_read, |
| 60 | .write = &stream_sock_write, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 61 | .bind = uxst_bind_listener, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 62 | .bind_all = uxst_bind_listeners, |
| 63 | .unbind_all = uxst_unbind_listeners, |
| 64 | .enable_all = enable_all_listeners, |
| 65 | .disable_all = disable_all_listeners, |
| 66 | .listeners = LIST_HEAD_INIT(proto_unix.listeners), |
| 67 | .nb_listeners = 0, |
| 68 | }; |
| 69 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 70 | /******************************** |
| 71 | * 1) low-level socket functions |
| 72 | ********************************/ |
| 73 | |
| 74 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 75 | /* Tries to destroy the UNIX stream socket <path>. The socket must not be used |
| 76 | * anymore. It practises best effort, and no error is returned. |
| 77 | */ |
| 78 | static void destroy_uxst_socket(const char *path) |
| 79 | { |
| 80 | struct sockaddr_un addr; |
| 81 | int sock, ret; |
| 82 | |
| 83 | /* We might have been chrooted, so we may not be able to access the |
| 84 | * socket. In order to avoid bothering the other end, we connect with a |
| 85 | * wrong protocol, namely SOCK_DGRAM. The return code from connect() |
| 86 | * is enough to know if the socket is still live or not. If it's live |
| 87 | * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not |
| 88 | * ECONNREFUSED. In this case, we do not touch it because it's used |
| 89 | * by some other process. |
| 90 | */ |
| 91 | sock = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 92 | if (sock < 0) |
| 93 | return; |
| 94 | |
| 95 | addr.sun_family = AF_UNIX; |
| 96 | strncpy(addr.sun_path, path, sizeof(addr.sun_path)); |
| 97 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
| 98 | ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr)); |
| 99 | if (ret < 0 && errno == ECONNREFUSED) { |
| 100 | /* Connect failed: the socket still exists but is not used |
| 101 | * anymore. Let's remove this socket now. |
| 102 | */ |
| 103 | unlink(path); |
| 104 | } |
| 105 | close(sock); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /******************************** |
| 110 | * 2) listener-oriented functions |
| 111 | ********************************/ |
| 112 | |
| 113 | |
| 114 | /* This function creates a UNIX socket associated to the listener. It changes |
| 115 | * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling. |
| 116 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 117 | */ |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 118 | static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 119 | { |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 120 | int fd; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 121 | char tempname[MAXPATHLEN]; |
| 122 | char backname[MAXPATHLEN]; |
| 123 | struct sockaddr_un addr; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 124 | const char *msg = NULL; |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 125 | const char *path; |
| 126 | |
| 127 | int ret; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 128 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 129 | /* ensure we never return garbage */ |
| 130 | if (errmsg && errlen) |
| 131 | *errmsg = 0; |
| 132 | |
| 133 | if (listener->state != LI_ASSIGNED) |
| 134 | return ERR_NONE; /* already bound */ |
| 135 | |
| 136 | path = ((struct sockaddr_un *)&listener->addr)->sun_path; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 137 | |
| 138 | /* 1. create socket names */ |
| 139 | if (!path[0]) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 140 | msg = "Invalid empty name for a UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 141 | goto err_return; |
| 142 | } |
| 143 | |
| 144 | ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid); |
| 145 | if (ret < 0 || ret >= MAXPATHLEN) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 146 | msg = "name too long for UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 147 | goto err_return; |
| 148 | } |
| 149 | |
| 150 | ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid); |
| 151 | if (ret < 0 || ret >= MAXPATHLEN) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 152 | msg = "name too long for UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 153 | goto err_return; |
| 154 | } |
| 155 | |
| 156 | /* 2. clean existing orphaned entries */ |
| 157 | if (unlink(tempname) < 0 && errno != ENOENT) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 158 | msg = "error when trying to unlink previous UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 159 | goto err_return; |
| 160 | } |
| 161 | |
| 162 | if (unlink(backname) < 0 && errno != ENOENT) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 163 | msg = "error when trying to unlink previous UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 164 | goto err_return; |
| 165 | } |
| 166 | |
| 167 | /* 3. backup existing socket */ |
| 168 | if (link(path, backname) < 0 && errno != ENOENT) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 169 | msg = "error when trying to preserve previous UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 170 | goto err_return; |
| 171 | } |
| 172 | |
| 173 | /* 4. prepare new socket */ |
| 174 | addr.sun_family = AF_UNIX; |
| 175 | strncpy(addr.sun_path, tempname, sizeof(addr.sun_path)); |
| 176 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
| 177 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 178 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 179 | if (fd < 0) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 180 | msg = "cannot create UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 181 | goto err_unlink_back; |
| 182 | } |
| 183 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 184 | if (fd >= global.maxsock) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 185 | msg = "socket(): not enough free sockets, raise -n argument"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 186 | goto err_unlink_temp; |
| 187 | } |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 188 | |
| 189 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 190 | msg = "cannot make UNIX socket non-blocking"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 191 | goto err_unlink_temp; |
| 192 | } |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 193 | |
| 194 | if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 195 | /* note that bind() creates the socket <tempname> on the file system */ |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 196 | msg = "cannot bind UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 197 | goto err_unlink_temp; |
| 198 | } |
| 199 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 200 | /* <uid> and <gid> different of -1 will be used to change the socket owner. |
| 201 | * If <mode> is not 0, it will be used to restrict access to the socket. |
| 202 | * While it is known not to be portable on every OS, it's still useful |
| 203 | * where it works. |
| 204 | */ |
| 205 | if (((listener->perm.ux.uid != -1 || listener->perm.ux.gid != -1) && |
| 206 | (chown(tempname, listener->perm.ux.uid, listener->perm.ux.gid) == -1)) || |
| 207 | (listener->perm.ux.mode != 0 && chmod(tempname, listener->perm.ux.mode) == -1)) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 208 | msg = "cannot change UNIX socket ownership"; |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 209 | goto err_unlink_temp; |
| 210 | } |
| 211 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 212 | if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) < 0) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 213 | msg = "cannot listen to UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 214 | goto err_unlink_temp; |
| 215 | } |
| 216 | |
| 217 | /* 5. install. |
| 218 | * Point of no return: we are ready, we'll switch the sockets. We don't |
| 219 | * fear loosing the socket <path> because we have a copy of it in |
| 220 | * backname. |
| 221 | */ |
| 222 | if (rename(tempname, path) < 0) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 223 | msg = "cannot switch final and temporary UNIX sockets"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 224 | goto err_rename; |
| 225 | } |
| 226 | |
| 227 | /* 6. cleanup */ |
| 228 | unlink(backname); /* no need to keep this one either */ |
| 229 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 230 | /* the socket is now listening */ |
| 231 | listener->fd = fd; |
| 232 | listener->state = LI_LISTEN; |
| 233 | |
| 234 | /* the function for the accept() event */ |
| 235 | fd_insert(fd); |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 236 | fdtab[fd].cb[DIR_RD].f = listener->proto->accept; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 237 | fdtab[fd].cb[DIR_WR].f = NULL; /* never called */ |
| 238 | fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL; |
Willy Tarreau | eabf313 | 2008-08-29 23:36:51 +0200 | [diff] [blame] | 239 | fdtab[fd].owner = listener; /* reference the listener instead of a task */ |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 240 | fdtab[fd].state = FD_STLISTEN; |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 241 | fdinfo[fd].peeraddr = NULL; |
| 242 | fdinfo[fd].peerlen = 0; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 243 | return ERR_NONE; |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 244 | err_rename: |
| 245 | ret = rename(backname, path); |
| 246 | if (ret < 0 && errno == ENOENT) |
| 247 | unlink(path); |
| 248 | err_unlink_temp: |
| 249 | unlink(tempname); |
| 250 | close(fd); |
| 251 | err_unlink_back: |
| 252 | unlink(backname); |
| 253 | err_return: |
| 254 | if (msg && errlen) |
| 255 | snprintf(errmsg, errlen, "%s [%s]", msg, path); |
| 256 | return ERR_FATAL | ERR_ALERT; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* This function closes the UNIX sockets for the specified listener. |
| 260 | * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE. |
| 261 | */ |
| 262 | static int uxst_unbind_listener(struct listener *listener) |
| 263 | { |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 264 | if (listener->state > LI_ASSIGNED) { |
| 265 | unbind_listener(listener); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 266 | destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path); |
| 267 | } |
| 268 | return ERR_NONE; |
| 269 | } |
| 270 | |
| 271 | /* Add a listener to the list of unix stream listeners. The listener's state |
| 272 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 273 | * listeners is updated. This is the function to use to add a new listener. |
| 274 | */ |
| 275 | void uxst_add_listener(struct listener *listener) |
| 276 | { |
| 277 | if (listener->state != LI_INIT) |
| 278 | return; |
| 279 | listener->state = LI_ASSIGNED; |
| 280 | listener->proto = &proto_unix; |
| 281 | LIST_ADDQ(&proto_unix.listeners, &listener->proto_list); |
| 282 | proto_unix.nb_listeners++; |
| 283 | } |
| 284 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 285 | /******************************** |
| 286 | * 3) protocol-oriented functions |
| 287 | ********************************/ |
| 288 | |
| 289 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 290 | /* This function creates all UNIX sockets bound to the protocol entry <proto>. |
| 291 | * It is intended to be used as the protocol's bind_all() function. |
| 292 | * The sockets will be registered but not added to any fd_set, in order not to |
| 293 | * loose them across the fork(). A call to uxst_enable_listeners() is needed |
| 294 | * to complete initialization. |
| 295 | * |
| 296 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 297 | */ |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 298 | static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 299 | { |
| 300 | struct listener *listener; |
| 301 | int err = ERR_NONE; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 302 | |
| 303 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 304 | err |= uxst_bind_listener(listener, errmsg, errlen); |
| 305 | if (err & ERR_ABORT) |
| 306 | break; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 307 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 308 | return err; |
| 309 | } |
| 310 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 311 | |
| 312 | /* This function stops all listening UNIX sockets bound to the protocol |
| 313 | * <proto>. It does not detaches them from the protocol. |
| 314 | * It always returns ERR_NONE. |
| 315 | */ |
| 316 | static int uxst_unbind_listeners(struct protocol *proto) |
| 317 | { |
| 318 | struct listener *listener; |
| 319 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 320 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 321 | uxst_unbind_listener(listener); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 322 | return ERR_NONE; |
| 323 | } |
| 324 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 325 | |
| 326 | /******************************** |
| 327 | * 4) high-level functions |
| 328 | ********************************/ |
| 329 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 330 | __attribute__((constructor)) |
| 331 | static void __uxst_protocol_init(void) |
| 332 | { |
| 333 | protocol_register(&proto_unix); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | |
| 337 | /* |
| 338 | * Local variables: |
| 339 | * c-indent-level: 8 |
| 340 | * c-basic-offset: 8 |
| 341 | * End: |
| 342 | */ |