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