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