Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * UNIX SOCK_STREAM protocol layer (uxst) |
| 3 | * |
Willy Tarreau | 2c9f5b1 | 2009-08-16 19:12:36 +0200 | [diff] [blame] | 4 | * Copyright 2000-2009 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/memory.h> |
| 33 | #include <common/mini-clist.h> |
| 34 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 35 | #include <common/ticks.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 36 | #include <common/time.h> |
| 37 | #include <common/version.h> |
| 38 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 39 | #include <types/global.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 40 | |
| 41 | #include <proto/acl.h> |
| 42 | #include <proto/backend.h> |
| 43 | #include <proto/buffers.h> |
| 44 | #include <proto/fd.h> |
| 45 | #include <proto/log.h> |
| 46 | #include <proto/protocols.h> |
| 47 | #include <proto/proto_uxst.h> |
| 48 | #include <proto/queue.h> |
| 49 | #include <proto/session.h> |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 50 | #include <proto/stream_interface.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 51 | #include <proto/stream_sock.h> |
| 52 | #include <proto/task.h> |
| 53 | |
| 54 | #ifndef MAXPATHLEN |
| 55 | #define MAXPATHLEN 128 |
| 56 | #endif |
| 57 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 58 | static int uxst_bind_listeners(struct protocol *proto); |
| 59 | static int uxst_unbind_listeners(struct protocol *proto); |
| 60 | |
| 61 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 62 | static struct protocol proto_unix = { |
| 63 | .name = "unix_stream", |
| 64 | .sock_domain = PF_UNIX, |
| 65 | .sock_type = SOCK_STREAM, |
| 66 | .sock_prot = 0, |
| 67 | .sock_family = AF_UNIX, |
| 68 | .sock_addrlen = sizeof(struct sockaddr_un), |
| 69 | .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */ |
| 70 | .read = &stream_sock_read, |
| 71 | .write = &stream_sock_write, |
| 72 | .bind_all = uxst_bind_listeners, |
| 73 | .unbind_all = uxst_unbind_listeners, |
| 74 | .enable_all = enable_all_listeners, |
| 75 | .disable_all = disable_all_listeners, |
| 76 | .listeners = LIST_HEAD_INIT(proto_unix.listeners), |
| 77 | .nb_listeners = 0, |
| 78 | }; |
| 79 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 80 | /******************************** |
| 81 | * 1) low-level socket functions |
| 82 | ********************************/ |
| 83 | |
| 84 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 85 | /* 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] | 86 | * that the path cannot be NULL nor empty. <uid> and <gid> different of -1 will |
| 87 | * be used to change the socket owner. If <mode> is not 0, it will be used to |
| 88 | * restrict access to the socket. While it is known not to be portable on every |
| 89 | * OS, it's still useful where it works. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 90 | * It returns the assigned file descriptor, or -1 in the event of an error. |
| 91 | */ |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 92 | 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] | 93 | { |
| 94 | char tempname[MAXPATHLEN]; |
| 95 | char backname[MAXPATHLEN]; |
| 96 | struct sockaddr_un addr; |
| 97 | |
| 98 | int ret, sock; |
| 99 | |
| 100 | /* 1. create socket names */ |
| 101 | if (!path[0]) { |
| 102 | Alert("Invalid name for a UNIX socket. Aborting.\n"); |
| 103 | goto err_return; |
| 104 | } |
| 105 | |
| 106 | ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid); |
| 107 | if (ret < 0 || ret >= MAXPATHLEN) { |
| 108 | Alert("name too long for UNIX socket. Aborting.\n"); |
| 109 | goto err_return; |
| 110 | } |
| 111 | |
| 112 | ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid); |
| 113 | if (ret < 0 || ret >= MAXPATHLEN) { |
| 114 | Alert("name too long for UNIX socket. Aborting.\n"); |
| 115 | goto err_return; |
| 116 | } |
| 117 | |
| 118 | /* 2. clean existing orphaned entries */ |
| 119 | if (unlink(tempname) < 0 && errno != ENOENT) { |
| 120 | Alert("error when trying to unlink previous UNIX socket. Aborting.\n"); |
| 121 | goto err_return; |
| 122 | } |
| 123 | |
| 124 | if (unlink(backname) < 0 && errno != ENOENT) { |
| 125 | Alert("error when trying to unlink previous UNIX socket. Aborting.\n"); |
| 126 | goto err_return; |
| 127 | } |
| 128 | |
| 129 | /* 3. backup existing socket */ |
| 130 | if (link(path, backname) < 0 && errno != ENOENT) { |
| 131 | Alert("error when trying to preserve previous UNIX socket. Aborting.\n"); |
| 132 | goto err_return; |
| 133 | } |
| 134 | |
| 135 | /* 4. prepare new socket */ |
| 136 | addr.sun_family = AF_UNIX; |
| 137 | strncpy(addr.sun_path, tempname, sizeof(addr.sun_path)); |
| 138 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
| 139 | |
| 140 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 141 | if (sock < 0) { |
| 142 | Alert("cannot create socket for UNIX listener. Aborting.\n"); |
| 143 | goto err_unlink_back; |
| 144 | } |
| 145 | |
| 146 | if (sock >= global.maxsock) { |
| 147 | Alert("socket(): not enough free sockets for UNIX listener. Raise -n argument. Aborting.\n"); |
| 148 | goto err_unlink_temp; |
| 149 | } |
| 150 | |
| 151 | if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) { |
| 152 | Alert("cannot make UNIX socket non-blocking. Aborting.\n"); |
| 153 | goto err_unlink_temp; |
| 154 | } |
| 155 | |
| 156 | if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 157 | /* note that bind() creates the socket <tempname> on the file system */ |
| 158 | Alert("cannot bind socket for UNIX listener. Aborting.\n"); |
| 159 | goto err_unlink_temp; |
| 160 | } |
| 161 | |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 162 | if (((uid != -1 || gid != -1) && (chown(tempname, uid, gid) == -1)) || |
| 163 | (mode != 0 && chmod(tempname, mode) == -1)) { |
| 164 | Alert("cannot change UNIX socket ownership. Aborting.\n"); |
| 165 | goto err_unlink_temp; |
| 166 | } |
| 167 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 168 | if (listen(sock, 0) < 0) { |
| 169 | Alert("cannot listen to socket for UNIX listener. Aborting.\n"); |
| 170 | goto err_unlink_temp; |
| 171 | } |
| 172 | |
| 173 | /* 5. install. |
| 174 | * Point of no return: we are ready, we'll switch the sockets. We don't |
| 175 | * fear loosing the socket <path> because we have a copy of it in |
| 176 | * backname. |
| 177 | */ |
| 178 | if (rename(tempname, path) < 0) { |
| 179 | Alert("cannot switch final and temporary sockets for UNIX listener. Aborting.\n"); |
| 180 | goto err_rename; |
| 181 | } |
| 182 | |
| 183 | /* 6. cleanup */ |
| 184 | unlink(backname); /* no need to keep this one either */ |
| 185 | |
| 186 | return sock; |
| 187 | |
| 188 | err_rename: |
| 189 | ret = rename(backname, path); |
| 190 | if (ret < 0 && errno == ENOENT) |
| 191 | unlink(path); |
| 192 | err_unlink_temp: |
| 193 | unlink(tempname); |
| 194 | close(sock); |
| 195 | err_unlink_back: |
| 196 | unlink(backname); |
| 197 | err_return: |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | /* Tries to destroy the UNIX stream socket <path>. The socket must not be used |
| 202 | * anymore. It practises best effort, and no error is returned. |
| 203 | */ |
| 204 | static void destroy_uxst_socket(const char *path) |
| 205 | { |
| 206 | struct sockaddr_un addr; |
| 207 | int sock, ret; |
| 208 | |
| 209 | /* We might have been chrooted, so we may not be able to access the |
| 210 | * socket. In order to avoid bothering the other end, we connect with a |
| 211 | * wrong protocol, namely SOCK_DGRAM. The return code from connect() |
| 212 | * is enough to know if the socket is still live or not. If it's live |
| 213 | * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not |
| 214 | * ECONNREFUSED. In this case, we do not touch it because it's used |
| 215 | * by some other process. |
| 216 | */ |
| 217 | sock = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 218 | if (sock < 0) |
| 219 | return; |
| 220 | |
| 221 | addr.sun_family = AF_UNIX; |
| 222 | strncpy(addr.sun_path, path, sizeof(addr.sun_path)); |
Willy Tarreau | 10ae548 | 2007-10-18 16:15:52 +0200 | [diff] [blame] | 223 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 224 | ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr)); |
| 225 | if (ret < 0 && errno == ECONNREFUSED) { |
| 226 | /* Connect failed: the socket still exists but is not used |
| 227 | * anymore. Let's remove this socket now. |
| 228 | */ |
| 229 | unlink(path); |
| 230 | } |
| 231 | close(sock); |
| 232 | } |
| 233 | |
| 234 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 235 | /******************************** |
| 236 | * 2) listener-oriented functions |
| 237 | ********************************/ |
| 238 | |
| 239 | |
| 240 | /* This function creates the UNIX socket associated to the listener. It changes |
| 241 | * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling. |
| 242 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 243 | */ |
| 244 | static int uxst_bind_listener(struct listener *listener) |
| 245 | { |
| 246 | int fd; |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 247 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 248 | if (listener->state != LI_ASSIGNED) |
| 249 | return ERR_NONE; /* already bound */ |
| 250 | |
| 251 | fd = create_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path, |
| 252 | listener->perm.ux.uid, |
| 253 | listener->perm.ux.gid, |
| 254 | listener->perm.ux.mode); |
| 255 | if (fd == -1) |
| 256 | return ERR_FATAL; |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 257 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 258 | /* the socket is now listening */ |
| 259 | listener->fd = fd; |
| 260 | listener->state = LI_LISTEN; |
| 261 | |
| 262 | /* the function for the accept() event */ |
| 263 | fd_insert(fd); |
| 264 | fdtab[fd].cb[DIR_RD].f = listener->accept; |
| 265 | fdtab[fd].cb[DIR_WR].f = NULL; /* never called */ |
| 266 | 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] | 267 | fdtab[fd].owner = listener; /* reference the listener instead of a task */ |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 268 | fdtab[fd].state = FD_STLISTEN; |
| 269 | fdtab[fd].peeraddr = NULL; |
| 270 | fdtab[fd].peerlen = 0; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 271 | return ERR_NONE; |
| 272 | } |
| 273 | |
| 274 | /* This function closes the UNIX sockets for the specified listener. |
| 275 | * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE. |
| 276 | */ |
| 277 | static int uxst_unbind_listener(struct listener *listener) |
| 278 | { |
| 279 | if (listener->state == LI_READY) |
| 280 | EV_FD_CLR(listener->fd, DIR_RD); |
| 281 | |
| 282 | if (listener->state >= LI_LISTEN) { |
Willy Tarreau | 8eebe5e | 2007-10-28 22:07:08 +0100 | [diff] [blame] | 283 | fd_delete(listener->fd); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 284 | listener->state = LI_ASSIGNED; |
| 285 | destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path); |
| 286 | } |
| 287 | return ERR_NONE; |
| 288 | } |
| 289 | |
| 290 | /* Add a listener to the list of unix stream listeners. The listener's state |
| 291 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 292 | * listeners is updated. This is the function to use to add a new listener. |
| 293 | */ |
| 294 | void uxst_add_listener(struct listener *listener) |
| 295 | { |
| 296 | if (listener->state != LI_INIT) |
| 297 | return; |
| 298 | listener->state = LI_ASSIGNED; |
| 299 | listener->proto = &proto_unix; |
| 300 | LIST_ADDQ(&proto_unix.listeners, &listener->proto_list); |
| 301 | proto_unix.nb_listeners++; |
| 302 | } |
| 303 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 304 | /******************************** |
| 305 | * 3) protocol-oriented functions |
| 306 | ********************************/ |
| 307 | |
| 308 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 309 | /* This function creates all UNIX sockets bound to the protocol entry <proto>. |
| 310 | * It is intended to be used as the protocol's bind_all() function. |
| 311 | * The sockets will be registered but not added to any fd_set, in order not to |
| 312 | * loose them across the fork(). A call to uxst_enable_listeners() is needed |
| 313 | * to complete initialization. |
| 314 | * |
| 315 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 316 | */ |
| 317 | static int uxst_bind_listeners(struct protocol *proto) |
| 318 | { |
| 319 | struct listener *listener; |
| 320 | int err = ERR_NONE; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 321 | |
| 322 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 323 | err |= uxst_bind_listener(listener); |
| 324 | if (err != ERR_NONE) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 325 | continue; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 326 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 327 | return err; |
| 328 | } |
| 329 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 330 | |
| 331 | /* This function stops all listening UNIX sockets bound to the protocol |
| 332 | * <proto>. It does not detaches them from the protocol. |
| 333 | * It always returns ERR_NONE. |
| 334 | */ |
| 335 | static int uxst_unbind_listeners(struct protocol *proto) |
| 336 | { |
| 337 | struct listener *listener; |
| 338 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 339 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 340 | uxst_unbind_listener(listener); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 341 | return ERR_NONE; |
| 342 | } |
| 343 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 344 | |
| 345 | /******************************** |
| 346 | * 4) high-level functions |
| 347 | ********************************/ |
| 348 | |
| 349 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 350 | /* |
| 351 | * This function is called on a read event from a listen socket, corresponding |
| 352 | * to an accept. It tries to accept as many connections as possible. |
| 353 | * It returns 0. Since we use UNIX sockets on the local system for monitoring |
| 354 | * purposes and other related things, we do not need to output as many messages |
| 355 | * as with TCP which can fall under attack. |
| 356 | */ |
| 357 | int uxst_event_accept(int fd) { |
Willy Tarreau | eabf313 | 2008-08-29 23:36:51 +0200 | [diff] [blame] | 358 | struct listener *l = fdtab[fd].owner; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 359 | struct session *s; |
| 360 | struct task *t; |
| 361 | int cfd; |
| 362 | int max_accept; |
| 363 | |
| 364 | if (global.nbproc > 1) |
| 365 | max_accept = 8; /* let other processes catch some connections too */ |
| 366 | else |
| 367 | max_accept = -1; |
| 368 | |
Willy Tarreau | 2d04559 | 2009-03-28 11:02:18 +0100 | [diff] [blame] | 369 | while (max_accept--) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 370 | struct sockaddr_storage addr; |
| 371 | socklen_t laddr = sizeof(addr); |
| 372 | |
| 373 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) { |
| 374 | switch (errno) { |
| 375 | case EAGAIN: |
| 376 | case EINTR: |
| 377 | case ECONNABORTED: |
| 378 | return 0; /* nothing more to accept */ |
| 379 | case ENFILE: |
| 380 | /* Process reached system FD limit. Check system tunables. */ |
| 381 | return 0; |
| 382 | case EMFILE: |
| 383 | /* Process reached process FD limit. Check 'ulimit-n'. */ |
| 384 | return 0; |
| 385 | case ENOBUFS: |
| 386 | case ENOMEM: |
| 387 | /* Process reached system memory limit. Check system tunables. */ |
| 388 | return 0; |
| 389 | default: |
| 390 | return 0; |
| 391 | } |
| 392 | } |
| 393 | |
Willy Tarreau | 2d04559 | 2009-03-28 11:02:18 +0100 | [diff] [blame] | 394 | if (l->nbconn >= l->maxconn || actconn >= global.maxconn) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 395 | /* too many connections, we shoot this one and return. |
| 396 | * FIXME: it would be better to simply switch the listener's |
| 397 | * state to LI_FULL and disable the FD. We could re-enable |
| 398 | * it upon fd_delete(), but this requires all protocols to |
| 399 | * be switched. |
| 400 | */ |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 401 | goto out_close; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | if ((s = pool_alloc2(pool2_session)) == NULL) { |
| 405 | Alert("out of memory in uxst_event_accept().\n"); |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 406 | goto out_close; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 409 | LIST_ADDQ(&sessions, &s->list); |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 410 | LIST_INIT(&s->back_refs); |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 411 | |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 412 | s->flags = 0; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 413 | s->term_trace = 0; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 414 | |
Willy Tarreau | a461318 | 2009-03-21 18:13:21 +0100 | [diff] [blame] | 415 | if ((t = task_new()) == NULL) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 416 | Alert("out of memory in uxst_event_accept().\n"); |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 417 | goto out_free_session; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | s->cli_addr = addr; |
| 421 | |
| 422 | /* FIXME: should be checked earlier */ |
| 423 | if (cfd >= global.maxsock) { |
| 424 | Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n"); |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 425 | goto out_free_task; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | if (fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) { |
| 429 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 430 | goto out_free_task; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 433 | t->process = l->handler; |
| 434 | t->context = s; |
Willy Tarreau | 2c9f5b1 | 2009-08-16 19:12:36 +0200 | [diff] [blame] | 435 | t->nice = l->nice; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 436 | |
| 437 | s->task = t; |
Willy Tarreau | b5654f6 | 2008-12-07 16:45:10 +0100 | [diff] [blame] | 438 | s->listener = l; |
Willy Tarreau | 89a6313 | 2009-08-16 17:41:45 +0200 | [diff] [blame] | 439 | s->fe = s->be = l->private; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 440 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 441 | s->ana_state = 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 442 | s->req = s->rep = NULL; /* will be allocated later */ |
| 443 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 444 | s->si[0].state = s->si[0].prev_state = SI_ST_EST; |
| 445 | s->si[0].err_type = SI_ET_NONE; |
| 446 | s->si[0].err_loc = NULL; |
| 447 | s->si[0].owner = t; |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 448 | s->si[0].update = stream_sock_data_finish; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 449 | s->si[0].shutr = stream_sock_shutr; |
| 450 | s->si[0].shutw = stream_sock_shutw; |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 451 | s->si[0].chk_rcv = stream_sock_chk_rcv; |
| 452 | s->si[0].chk_snd = stream_sock_chk_snd; |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 453 | s->si[0].connect = NULL; |
Willy Tarreau | b029f8c | 2009-09-05 20:57:35 +0200 | [diff] [blame^] | 454 | s->si[0].iohandler = NULL; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 455 | s->si[0].fd = cfd; |
| 456 | s->si[0].flags = SI_FL_NONE; |
| 457 | s->si[0].exp = TICK_ETERNITY; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 458 | |
| 459 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 460 | s->si[1].err_type = SI_ET_NONE; |
| 461 | s->si[1].err_loc = NULL; |
| 462 | s->si[1].owner = t; |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 463 | s->si[1].update = stream_sock_data_finish; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 464 | s->si[1].shutr = stream_sock_shutr; |
| 465 | s->si[1].shutw = stream_sock_shutw; |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 466 | s->si[1].chk_rcv = stream_sock_chk_rcv; |
| 467 | s->si[1].chk_snd = stream_sock_chk_snd; |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 468 | s->si[1].connect = NULL; |
Willy Tarreau | b029f8c | 2009-09-05 20:57:35 +0200 | [diff] [blame^] | 469 | s->si[1].iohandler = NULL; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 470 | s->si[1].exp = TICK_ETERNITY; |
| 471 | s->si[1].fd = -1; /* just to help with debugging */ |
| 472 | s->si[1].flags = SI_FL_NONE; |
| 473 | |
| 474 | s->srv = s->prev_srv = s->srv_conn = NULL; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 475 | s->pend_pos = NULL; |
| 476 | |
| 477 | memset(&s->logs, 0, sizeof(s->logs)); |
| 478 | memset(&s->txn, 0, sizeof(s->txn)); |
| 479 | |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 480 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 481 | |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 482 | s->data_state = DATA_ST_INIT; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 483 | s->data_source = DATA_SRC_NONE; |
| 484 | s->uniq_id = totalconn; |
| 485 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 486 | if ((s->req = pool_alloc2(pool2_buffer)) == NULL) |
| 487 | goto out_free_task; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 488 | |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 489 | s->req->size = global.tune.bufsize; |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 490 | buffer_init(s->req); |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 491 | s->req->prod = &s->si[0]; |
| 492 | s->req->cons = &s->si[1]; |
| 493 | s->si[0].ib = s->si[1].ob = s->req; |
| 494 | s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */ |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 495 | s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */ |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 496 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 497 | s->req->analysers = l->analysers; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 498 | |
| 499 | s->req->wto = TICK_ETERNITY; |
| 500 | s->req->cto = TICK_ETERNITY; |
| 501 | s->req->rto = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 502 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 503 | if ((s->rep = pool_alloc2(pool2_buffer)) == NULL) |
| 504 | goto out_free_req; |
| 505 | |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 506 | s->rep->size = global.tune.bufsize; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 507 | buffer_init(s->rep); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 508 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 509 | s->rep->prod = &s->si[1]; |
| 510 | s->rep->cons = &s->si[0]; |
| 511 | s->si[0].ob = s->si[1].ib = s->rep; |
| 512 | |
| 513 | s->rep->rto = TICK_ETERNITY; |
| 514 | s->rep->cto = TICK_ETERNITY; |
| 515 | s->rep->wto = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 516 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 517 | s->req->rex = TICK_ETERNITY; |
| 518 | s->req->wex = TICK_ETERNITY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 519 | s->req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 520 | s->rep->rex = TICK_ETERNITY; |
| 521 | s->rep->wex = TICK_ETERNITY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 522 | s->rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 523 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 524 | t->expire = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 525 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 526 | if (l->timeout) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 527 | s->req->rto = *l->timeout; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 528 | s->rep->wto = *l->timeout; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 531 | fd_insert(cfd); |
| 532 | fdtab[cfd].owner = &s->si[0]; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 533 | fdtab[cfd].state = FD_STREADY; |
| 534 | fdtab[cfd].cb[DIR_RD].f = l->proto->read; |
| 535 | fdtab[cfd].cb[DIR_RD].b = s->req; |
| 536 | fdtab[cfd].cb[DIR_WR].f = l->proto->write; |
| 537 | fdtab[cfd].cb[DIR_WR].b = s->rep; |
| 538 | fdtab[cfd].peeraddr = (struct sockaddr *)&s->cli_addr; |
| 539 | fdtab[cfd].peerlen = sizeof(s->cli_addr); |
| 540 | |
| 541 | EV_FD_SET(cfd, DIR_RD); |
| 542 | |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 543 | task_wakeup(t, TASK_WOKEN_INIT); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 544 | |
| 545 | l->nbconn++; /* warning! right now, it's up to the handler to decrease this */ |
| 546 | if (l->nbconn >= l->maxconn) { |
| 547 | EV_FD_CLR(l->fd, DIR_RD); |
| 548 | l->state = LI_FULL; |
| 549 | } |
| 550 | actconn++; |
| 551 | totalconn++; |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 552 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 553 | return 0; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 554 | |
| 555 | out_free_req: |
| 556 | pool_free2(pool2_buffer, s->req); |
| 557 | out_free_task: |
Willy Tarreau | a461318 | 2009-03-21 18:13:21 +0100 | [diff] [blame] | 558 | task_free(t); |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 559 | out_free_session: |
| 560 | LIST_DEL(&s->list); |
| 561 | pool_free2(pool2_session, s); |
| 562 | out_close: |
| 563 | close(cfd); |
| 564 | return 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 565 | } |
| 566 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 567 | __attribute__((constructor)) |
| 568 | static void __uxst_protocol_init(void) |
| 569 | { |
| 570 | protocol_register(&proto_unix); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | |
| 574 | /* |
| 575 | * Local variables: |
| 576 | * c-indent-level: 8 |
| 577 | * c-basic-offset: 8 |
| 578 | * End: |
| 579 | */ |