Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * UNIX SOCK_STREAM protocol layer (uxst) |
| 3 | * |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 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> |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 44 | #include <proto/dumpstats.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 45 | #include <proto/fd.h> |
| 46 | #include <proto/log.h> |
| 47 | #include <proto/protocols.h> |
| 48 | #include <proto/proto_uxst.h> |
| 49 | #include <proto/queue.h> |
| 50 | #include <proto/session.h> |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 51 | #include <proto/stream_interface.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 52 | #include <proto/stream_sock.h> |
| 53 | #include <proto/task.h> |
| 54 | |
| 55 | #ifndef MAXPATHLEN |
| 56 | #define MAXPATHLEN 128 |
| 57 | #endif |
| 58 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 59 | static int uxst_bind_listeners(struct protocol *proto); |
| 60 | static int uxst_unbind_listeners(struct protocol *proto); |
| 61 | |
| 62 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 63 | static struct protocol proto_unix = { |
| 64 | .name = "unix_stream", |
| 65 | .sock_domain = PF_UNIX, |
| 66 | .sock_type = SOCK_STREAM, |
| 67 | .sock_prot = 0, |
| 68 | .sock_family = AF_UNIX, |
| 69 | .sock_addrlen = sizeof(struct sockaddr_un), |
| 70 | .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */ |
| 71 | .read = &stream_sock_read, |
| 72 | .write = &stream_sock_write, |
| 73 | .bind_all = uxst_bind_listeners, |
| 74 | .unbind_all = uxst_unbind_listeners, |
| 75 | .enable_all = enable_all_listeners, |
| 76 | .disable_all = disable_all_listeners, |
| 77 | .listeners = LIST_HEAD_INIT(proto_unix.listeners), |
| 78 | .nb_listeners = 0, |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | /******************************** |
| 83 | * 1) low-level socket functions |
| 84 | ********************************/ |
| 85 | |
| 86 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 87 | /* 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] | 88 | * that the path cannot be NULL nor empty. <uid> and <gid> different of -1 will |
| 89 | * be used to change the socket owner. If <mode> is not 0, it will be used to |
| 90 | * restrict access to the socket. While it is known not to be portable on every |
| 91 | * OS, it's still useful where it works. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 92 | * It returns the assigned file descriptor, or -1 in the event of an error. |
| 93 | */ |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 94 | 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] | 95 | { |
| 96 | char tempname[MAXPATHLEN]; |
| 97 | char backname[MAXPATHLEN]; |
| 98 | struct sockaddr_un addr; |
| 99 | |
| 100 | int ret, sock; |
| 101 | |
| 102 | /* 1. create socket names */ |
| 103 | if (!path[0]) { |
| 104 | Alert("Invalid name for a UNIX socket. Aborting.\n"); |
| 105 | goto err_return; |
| 106 | } |
| 107 | |
| 108 | ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid); |
| 109 | if (ret < 0 || ret >= MAXPATHLEN) { |
| 110 | Alert("name too long for UNIX socket. Aborting.\n"); |
| 111 | goto err_return; |
| 112 | } |
| 113 | |
| 114 | ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid); |
| 115 | if (ret < 0 || ret >= MAXPATHLEN) { |
| 116 | Alert("name too long for UNIX socket. Aborting.\n"); |
| 117 | goto err_return; |
| 118 | } |
| 119 | |
| 120 | /* 2. clean existing orphaned entries */ |
| 121 | if (unlink(tempname) < 0 && errno != ENOENT) { |
| 122 | Alert("error when trying to unlink previous UNIX socket. Aborting.\n"); |
| 123 | goto err_return; |
| 124 | } |
| 125 | |
| 126 | if (unlink(backname) < 0 && errno != ENOENT) { |
| 127 | Alert("error when trying to unlink previous UNIX socket. Aborting.\n"); |
| 128 | goto err_return; |
| 129 | } |
| 130 | |
| 131 | /* 3. backup existing socket */ |
| 132 | if (link(path, backname) < 0 && errno != ENOENT) { |
| 133 | Alert("error when trying to preserve previous UNIX socket. Aborting.\n"); |
| 134 | goto err_return; |
| 135 | } |
| 136 | |
| 137 | /* 4. prepare new socket */ |
| 138 | addr.sun_family = AF_UNIX; |
| 139 | strncpy(addr.sun_path, tempname, sizeof(addr.sun_path)); |
| 140 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
| 141 | |
| 142 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 143 | if (sock < 0) { |
| 144 | Alert("cannot create socket for UNIX listener. Aborting.\n"); |
| 145 | goto err_unlink_back; |
| 146 | } |
| 147 | |
| 148 | if (sock >= global.maxsock) { |
| 149 | Alert("socket(): not enough free sockets for UNIX listener. Raise -n argument. Aborting.\n"); |
| 150 | goto err_unlink_temp; |
| 151 | } |
| 152 | |
| 153 | if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) { |
| 154 | Alert("cannot make UNIX socket non-blocking. Aborting.\n"); |
| 155 | goto err_unlink_temp; |
| 156 | } |
| 157 | |
| 158 | if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 159 | /* note that bind() creates the socket <tempname> on the file system */ |
| 160 | Alert("cannot bind socket for UNIX listener. Aborting.\n"); |
| 161 | goto err_unlink_temp; |
| 162 | } |
| 163 | |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 164 | if (((uid != -1 || gid != -1) && (chown(tempname, uid, gid) == -1)) || |
| 165 | (mode != 0 && chmod(tempname, mode) == -1)) { |
| 166 | Alert("cannot change UNIX socket ownership. Aborting.\n"); |
| 167 | goto err_unlink_temp; |
| 168 | } |
| 169 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 170 | if (listen(sock, 0) < 0) { |
| 171 | Alert("cannot listen to socket for UNIX listener. Aborting.\n"); |
| 172 | goto err_unlink_temp; |
| 173 | } |
| 174 | |
| 175 | /* 5. install. |
| 176 | * Point of no return: we are ready, we'll switch the sockets. We don't |
| 177 | * fear loosing the socket <path> because we have a copy of it in |
| 178 | * backname. |
| 179 | */ |
| 180 | if (rename(tempname, path) < 0) { |
| 181 | Alert("cannot switch final and temporary sockets for UNIX listener. Aborting.\n"); |
| 182 | goto err_rename; |
| 183 | } |
| 184 | |
| 185 | /* 6. cleanup */ |
| 186 | unlink(backname); /* no need to keep this one either */ |
| 187 | |
| 188 | return sock; |
| 189 | |
| 190 | err_rename: |
| 191 | ret = rename(backname, path); |
| 192 | if (ret < 0 && errno == ENOENT) |
| 193 | unlink(path); |
| 194 | err_unlink_temp: |
| 195 | unlink(tempname); |
| 196 | close(sock); |
| 197 | err_unlink_back: |
| 198 | unlink(backname); |
| 199 | err_return: |
| 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | /* Tries to destroy the UNIX stream socket <path>. The socket must not be used |
| 204 | * anymore. It practises best effort, and no error is returned. |
| 205 | */ |
| 206 | static void destroy_uxst_socket(const char *path) |
| 207 | { |
| 208 | struct sockaddr_un addr; |
| 209 | int sock, ret; |
| 210 | |
| 211 | /* We might have been chrooted, so we may not be able to access the |
| 212 | * socket. In order to avoid bothering the other end, we connect with a |
| 213 | * wrong protocol, namely SOCK_DGRAM. The return code from connect() |
| 214 | * is enough to know if the socket is still live or not. If it's live |
| 215 | * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not |
| 216 | * ECONNREFUSED. In this case, we do not touch it because it's used |
| 217 | * by some other process. |
| 218 | */ |
| 219 | sock = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 220 | if (sock < 0) |
| 221 | return; |
| 222 | |
| 223 | addr.sun_family = AF_UNIX; |
| 224 | strncpy(addr.sun_path, path, sizeof(addr.sun_path)); |
Willy Tarreau | 10ae548 | 2007-10-18 16:15:52 +0200 | [diff] [blame] | 225 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 226 | ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr)); |
| 227 | if (ret < 0 && errno == ECONNREFUSED) { |
| 228 | /* Connect failed: the socket still exists but is not used |
| 229 | * anymore. Let's remove this socket now. |
| 230 | */ |
| 231 | unlink(path); |
| 232 | } |
| 233 | close(sock); |
| 234 | } |
| 235 | |
| 236 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 237 | /******************************** |
| 238 | * 2) listener-oriented functions |
| 239 | ********************************/ |
| 240 | |
| 241 | |
| 242 | /* This function creates the UNIX socket associated to the listener. It changes |
| 243 | * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling. |
| 244 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 245 | */ |
| 246 | static int uxst_bind_listener(struct listener *listener) |
| 247 | { |
| 248 | int fd; |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 249 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 250 | if (listener->state != LI_ASSIGNED) |
| 251 | return ERR_NONE; /* already bound */ |
| 252 | |
| 253 | fd = create_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path, |
| 254 | listener->perm.ux.uid, |
| 255 | listener->perm.ux.gid, |
| 256 | listener->perm.ux.mode); |
| 257 | if (fd == -1) |
| 258 | return ERR_FATAL; |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 259 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 260 | /* the socket is now listening */ |
| 261 | listener->fd = fd; |
| 262 | listener->state = LI_LISTEN; |
| 263 | |
| 264 | /* the function for the accept() event */ |
| 265 | fd_insert(fd); |
| 266 | fdtab[fd].cb[DIR_RD].f = listener->accept; |
| 267 | fdtab[fd].cb[DIR_WR].f = NULL; /* never called */ |
| 268 | 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] | 269 | fdtab[fd].owner = listener; /* reference the listener instead of a task */ |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 270 | fdtab[fd].state = FD_STLISTEN; |
| 271 | fdtab[fd].peeraddr = NULL; |
| 272 | fdtab[fd].peerlen = 0; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 273 | return ERR_NONE; |
| 274 | } |
| 275 | |
| 276 | /* This function closes the UNIX sockets for the specified listener. |
| 277 | * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE. |
| 278 | */ |
| 279 | static int uxst_unbind_listener(struct listener *listener) |
| 280 | { |
| 281 | if (listener->state == LI_READY) |
| 282 | EV_FD_CLR(listener->fd, DIR_RD); |
| 283 | |
| 284 | if (listener->state >= LI_LISTEN) { |
Willy Tarreau | 8eebe5e | 2007-10-28 22:07:08 +0100 | [diff] [blame] | 285 | fd_delete(listener->fd); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 286 | listener->state = LI_ASSIGNED; |
| 287 | destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path); |
| 288 | } |
| 289 | return ERR_NONE; |
| 290 | } |
| 291 | |
| 292 | /* Add a listener to the list of unix stream listeners. The listener's state |
| 293 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 294 | * listeners is updated. This is the function to use to add a new listener. |
| 295 | */ |
| 296 | void uxst_add_listener(struct listener *listener) |
| 297 | { |
| 298 | if (listener->state != LI_INIT) |
| 299 | return; |
| 300 | listener->state = LI_ASSIGNED; |
| 301 | listener->proto = &proto_unix; |
| 302 | LIST_ADDQ(&proto_unix.listeners, &listener->proto_list); |
| 303 | proto_unix.nb_listeners++; |
| 304 | } |
| 305 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 306 | /******************************** |
| 307 | * 3) protocol-oriented functions |
| 308 | ********************************/ |
| 309 | |
| 310 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 311 | /* This function creates all UNIX sockets bound to the protocol entry <proto>. |
| 312 | * It is intended to be used as the protocol's bind_all() function. |
| 313 | * The sockets will be registered but not added to any fd_set, in order not to |
| 314 | * loose them across the fork(). A call to uxst_enable_listeners() is needed |
| 315 | * to complete initialization. |
| 316 | * |
| 317 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 318 | */ |
| 319 | static int uxst_bind_listeners(struct protocol *proto) |
| 320 | { |
| 321 | struct listener *listener; |
| 322 | int err = ERR_NONE; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 323 | |
| 324 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 325 | err |= uxst_bind_listener(listener); |
| 326 | if (err != ERR_NONE) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 327 | continue; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 328 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 329 | return err; |
| 330 | } |
| 331 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 332 | |
| 333 | /* This function stops all listening UNIX sockets bound to the protocol |
| 334 | * <proto>. It does not detaches them from the protocol. |
| 335 | * It always returns ERR_NONE. |
| 336 | */ |
| 337 | static int uxst_unbind_listeners(struct protocol *proto) |
| 338 | { |
| 339 | struct listener *listener; |
| 340 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 341 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 342 | uxst_unbind_listener(listener); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 343 | return ERR_NONE; |
| 344 | } |
| 345 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 346 | |
| 347 | /******************************** |
| 348 | * 4) high-level functions |
| 349 | ********************************/ |
| 350 | |
| 351 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 352 | /* |
| 353 | * This function is called on a read event from a listen socket, corresponding |
| 354 | * to an accept. It tries to accept as many connections as possible. |
| 355 | * It returns 0. Since we use UNIX sockets on the local system for monitoring |
| 356 | * purposes and other related things, we do not need to output as many messages |
| 357 | * as with TCP which can fall under attack. |
| 358 | */ |
| 359 | int uxst_event_accept(int fd) { |
Willy Tarreau | eabf313 | 2008-08-29 23:36:51 +0200 | [diff] [blame] | 360 | struct listener *l = fdtab[fd].owner; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 361 | struct session *s; |
| 362 | struct task *t; |
| 363 | int cfd; |
| 364 | int max_accept; |
| 365 | |
| 366 | if (global.nbproc > 1) |
| 367 | max_accept = 8; /* let other processes catch some connections too */ |
| 368 | else |
| 369 | max_accept = -1; |
| 370 | |
Willy Tarreau | 2d04559 | 2009-03-28 11:02:18 +0100 | [diff] [blame] | 371 | while (max_accept--) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 372 | struct sockaddr_storage addr; |
| 373 | socklen_t laddr = sizeof(addr); |
| 374 | |
| 375 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) { |
| 376 | switch (errno) { |
| 377 | case EAGAIN: |
| 378 | case EINTR: |
| 379 | case ECONNABORTED: |
| 380 | return 0; /* nothing more to accept */ |
| 381 | case ENFILE: |
| 382 | /* Process reached system FD limit. Check system tunables. */ |
| 383 | return 0; |
| 384 | case EMFILE: |
| 385 | /* Process reached process FD limit. Check 'ulimit-n'. */ |
| 386 | return 0; |
| 387 | case ENOBUFS: |
| 388 | case ENOMEM: |
| 389 | /* Process reached system memory limit. Check system tunables. */ |
| 390 | return 0; |
| 391 | default: |
| 392 | return 0; |
| 393 | } |
| 394 | } |
| 395 | |
Willy Tarreau | 2d04559 | 2009-03-28 11:02:18 +0100 | [diff] [blame] | 396 | if (l->nbconn >= l->maxconn || actconn >= global.maxconn) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 397 | /* too many connections, we shoot this one and return. |
| 398 | * FIXME: it would be better to simply switch the listener's |
| 399 | * state to LI_FULL and disable the FD. We could re-enable |
| 400 | * it upon fd_delete(), but this requires all protocols to |
| 401 | * be switched. |
| 402 | */ |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 403 | goto out_close; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | if ((s = pool_alloc2(pool2_session)) == NULL) { |
| 407 | Alert("out of memory in uxst_event_accept().\n"); |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 408 | goto out_close; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 411 | LIST_ADDQ(&sessions, &s->list); |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 412 | LIST_INIT(&s->back_refs); |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 413 | |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 414 | s->flags = 0; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 415 | s->term_trace = 0; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 416 | |
Willy Tarreau | a461318 | 2009-03-21 18:13:21 +0100 | [diff] [blame] | 417 | if ((t = task_new()) == NULL) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 418 | Alert("out of memory in uxst_event_accept().\n"); |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 419 | goto out_free_session; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | s->cli_addr = addr; |
| 423 | |
| 424 | /* FIXME: should be checked earlier */ |
| 425 | if (cfd >= global.maxsock) { |
| 426 | Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n"); |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 427 | goto out_free_task; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | if (fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) { |
| 431 | 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] | 432 | goto out_free_task; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 435 | t->process = l->handler; |
| 436 | t->context = s; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 437 | t->nice = -64; /* we want to boost priority for local stats */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 438 | |
| 439 | s->task = t; |
Willy Tarreau | b5654f6 | 2008-12-07 16:45:10 +0100 | [diff] [blame] | 440 | s->listener = l; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 441 | s->fe = NULL; |
| 442 | s->be = NULL; |
| 443 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 444 | s->ana_state = 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 445 | s->req = s->rep = NULL; /* will be allocated later */ |
| 446 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 447 | s->si[0].state = s->si[0].prev_state = SI_ST_EST; |
| 448 | s->si[0].err_type = SI_ET_NONE; |
| 449 | s->si[0].err_loc = NULL; |
| 450 | s->si[0].owner = t; |
| 451 | s->si[0].shutr = stream_sock_shutr; |
| 452 | s->si[0].shutw = stream_sock_shutw; |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 453 | s->si[0].chk_rcv = stream_sock_chk_rcv; |
| 454 | s->si[0].chk_snd = stream_sock_chk_snd; |
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; |
| 463 | s->si[1].shutr = stream_sock_shutr; |
| 464 | s->si[1].shutw = stream_sock_shutw; |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 465 | s->si[1].chk_rcv = stream_sock_chk_rcv; |
| 466 | s->si[1].chk_snd = stream_sock_chk_snd; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 467 | s->si[1].exp = TICK_ETERNITY; |
| 468 | s->si[1].fd = -1; /* just to help with debugging */ |
| 469 | s->si[1].flags = SI_FL_NONE; |
| 470 | |
| 471 | s->srv = s->prev_srv = s->srv_conn = NULL; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 472 | s->pend_pos = NULL; |
| 473 | |
| 474 | memset(&s->logs, 0, sizeof(s->logs)); |
| 475 | memset(&s->txn, 0, sizeof(s->txn)); |
| 476 | |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 477 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 478 | |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 479 | s->data_state = DATA_ST_INIT; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 480 | s->data_source = DATA_SRC_NONE; |
| 481 | s->uniq_id = totalconn; |
| 482 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 483 | if ((s->req = pool_alloc2(pool2_buffer)) == NULL) |
| 484 | goto out_free_task; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 485 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 486 | buffer_init(s->req); |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 487 | s->req->prod = &s->si[0]; |
| 488 | s->req->cons = &s->si[1]; |
| 489 | s->si[0].ib = s->si[1].ob = s->req; |
| 490 | s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */ |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 491 | s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */ |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 492 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 493 | s->req->analysers = l->analysers; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 494 | |
| 495 | s->req->wto = TICK_ETERNITY; |
| 496 | s->req->cto = TICK_ETERNITY; |
| 497 | s->req->rto = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 498 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 499 | if ((s->rep = pool_alloc2(pool2_buffer)) == NULL) |
| 500 | goto out_free_req; |
| 501 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 502 | buffer_init(s->rep); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 503 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 504 | s->rep->prod = &s->si[1]; |
| 505 | s->rep->cons = &s->si[0]; |
| 506 | s->si[0].ob = s->si[1].ib = s->rep; |
| 507 | |
| 508 | s->rep->rto = TICK_ETERNITY; |
| 509 | s->rep->cto = TICK_ETERNITY; |
| 510 | s->rep->wto = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 511 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 512 | s->req->rex = TICK_ETERNITY; |
| 513 | s->req->wex = TICK_ETERNITY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 514 | s->req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 515 | s->rep->rex = TICK_ETERNITY; |
| 516 | s->rep->wex = TICK_ETERNITY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 517 | s->rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 518 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 519 | t->expire = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 520 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 521 | if (l->timeout) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 522 | s->req->rto = *l->timeout; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 523 | s->rep->wto = *l->timeout; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 526 | fd_insert(cfd); |
| 527 | fdtab[cfd].owner = &s->si[0]; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 528 | fdtab[cfd].state = FD_STREADY; |
| 529 | fdtab[cfd].cb[DIR_RD].f = l->proto->read; |
| 530 | fdtab[cfd].cb[DIR_RD].b = s->req; |
| 531 | fdtab[cfd].cb[DIR_WR].f = l->proto->write; |
| 532 | fdtab[cfd].cb[DIR_WR].b = s->rep; |
| 533 | fdtab[cfd].peeraddr = (struct sockaddr *)&s->cli_addr; |
| 534 | fdtab[cfd].peerlen = sizeof(s->cli_addr); |
| 535 | |
| 536 | EV_FD_SET(cfd, DIR_RD); |
| 537 | |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 538 | task_wakeup(t, TASK_WOKEN_INIT); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 539 | |
| 540 | l->nbconn++; /* warning! right now, it's up to the handler to decrease this */ |
| 541 | if (l->nbconn >= l->maxconn) { |
| 542 | EV_FD_CLR(l->fd, DIR_RD); |
| 543 | l->state = LI_FULL; |
| 544 | } |
| 545 | actconn++; |
| 546 | totalconn++; |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 547 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 548 | return 0; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 549 | |
| 550 | out_free_req: |
| 551 | pool_free2(pool2_buffer, s->req); |
| 552 | out_free_task: |
Willy Tarreau | a461318 | 2009-03-21 18:13:21 +0100 | [diff] [blame] | 553 | task_free(t); |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 554 | out_free_session: |
| 555 | LIST_DEL(&s->list); |
| 556 | pool_free2(pool2_session, s); |
| 557 | out_close: |
| 558 | close(cfd); |
| 559 | return 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 560 | } |
| 561 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 562 | /* Parses the request line in <cmd> and possibly starts dumping stats on |
| 563 | * s->rep with the hijack bit set. Returns 1 if OK, 0 in case of any error. |
| 564 | * The line is modified after parsing. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 565 | */ |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 566 | int unix_sock_parse_request(struct session *s, char *line) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 567 | { |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 568 | char *args[MAX_UXST_ARGS + 1]; |
| 569 | int arg; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 570 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 571 | while (isspace((unsigned char)*line)) |
| 572 | line++; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 573 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 574 | arg = 0; |
| 575 | args[arg] = line; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 576 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 577 | while (*line && arg < MAX_UXST_ARGS) { |
| 578 | if (isspace((unsigned char)*line)) { |
| 579 | *line++ = '\0'; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 580 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 581 | while (isspace((unsigned char)*line)) |
| 582 | line++; |
| 583 | |
| 584 | args[++arg] = line; |
| 585 | continue; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 586 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 587 | |
| 588 | line++; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 589 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 590 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 591 | while (++arg <= MAX_UXST_ARGS) |
| 592 | args[arg] = line; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 593 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 594 | if (strcmp(args[0], "show") == 0) { |
| 595 | if (strcmp(args[1], "stat") == 0) { |
| 596 | if (*args[2] && *args[3] && *args[4]) { |
| 597 | s->data_ctx.stats.flags |= STAT_BOUND; |
| 598 | s->data_ctx.stats.iid = atoi(args[2]); |
| 599 | s->data_ctx.stats.type = atoi(args[3]); |
| 600 | s->data_ctx.stats.sid = atoi(args[4]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 601 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 602 | |
| 603 | s->data_ctx.stats.flags |= STAT_SHOW_STAT; |
| 604 | s->data_ctx.stats.flags |= STAT_FMT_CSV; |
| 605 | s->ana_state = STATS_ST_REP; |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 606 | buffer_install_hijacker(s, s->rep, stats_dump_raw_to_buffer); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 607 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 608 | else if (strcmp(args[1], "info") == 0) { |
| 609 | s->data_ctx.stats.flags |= STAT_SHOW_INFO; |
| 610 | s->data_ctx.stats.flags |= STAT_FMT_CSV; |
| 611 | s->ana_state = STATS_ST_REP; |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 612 | buffer_install_hijacker(s, s->rep, stats_dump_raw_to_buffer); |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 613 | } |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 614 | else if (strcmp(args[1], "sess") == 0) { |
| 615 | s->ana_state = STATS_ST_REP; |
| 616 | buffer_install_hijacker(s, s->rep, stats_dump_sess_to_buffer); |
| 617 | } |
Willy Tarreau | 74808cb | 2009-03-04 15:53:18 +0100 | [diff] [blame] | 618 | else if (strcmp(args[1], "errors") == 0) { |
| 619 | if (*args[2]) |
| 620 | s->data_ctx.errors.iid = atoi(args[2]); |
| 621 | else |
| 622 | s->data_ctx.errors.iid = -1; |
| 623 | s->data_ctx.errors.px = NULL; |
| 624 | s->ana_state = STATS_ST_REP; |
| 625 | buffer_install_hijacker(s, s->rep, stats_dump_errors_to_buffer); |
| 626 | } |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 627 | else { /* neither "stat" nor "info" nor "sess" */ |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 628 | return 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 629 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 630 | } |
| 631 | else { /* not "show" */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 632 | return 0; |
| 633 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 634 | return 1; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 635 | } |
| 636 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 637 | /* Processes the stats interpreter on the statistics socket. |
| 638 | * In order to ease the transition, we simply simulate the server status |
| 639 | * for now. It only knows states STATS_ST_INIT, STATS_ST_REQ, STATS_ST_REP, and |
| 640 | * STATS_ST_CLOSE. It removes the AN_REQ_UNIX_STATS bit from req->analysers |
| 641 | * once done. It always returns 0. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 642 | */ |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 643 | int uxst_req_analyser_stats(struct session *s, struct buffer *req) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 644 | { |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 645 | char *line, *p; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 646 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 647 | switch (s->ana_state) { |
| 648 | case STATS_ST_INIT: |
| 649 | /* Stats output not initialized yet */ |
| 650 | memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats)); |
| 651 | s->data_source = DATA_SRC_STATS; |
| 652 | s->ana_state = STATS_ST_REQ; |
Willy Tarreau | 38c99bc | 2009-02-22 15:58:45 +0100 | [diff] [blame] | 653 | buffer_write_dis(s->req); |
| 654 | buffer_shutw_now(s->req); |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 655 | /* fall through */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 656 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 657 | case STATS_ST_REQ: |
| 658 | /* Now, stats are initialized, hijack is not set, and |
| 659 | * we are waiting for a complete request line. |
| 660 | */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 661 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 662 | line = s->req->data; |
| 663 | p = memchr(line, '\n', s->req->l); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 664 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 665 | if (p) { |
| 666 | *p = '\0'; |
| 667 | if (!unix_sock_parse_request(s, line)) { |
| 668 | /* invalid request */ |
Willy Tarreau | 38c99bc | 2009-02-22 15:58:45 +0100 | [diff] [blame] | 669 | buffer_shutw_now(s->rep); |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 670 | s->ana_state = 0; |
| 671 | req->analysers = 0; |
| 672 | return 0; |
| 673 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 674 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 675 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 676 | /* processing a valid or incomplete request */ |
| 677 | if ((req->flags & BF_FULL) || /* invalid request */ |
| 678 | (req->flags & BF_READ_ERROR) || /* input error */ |
| 679 | (req->flags & BF_READ_TIMEOUT) || /* read timeout */ |
| 680 | tick_is_expired(req->analyse_exp, now_ms) || /* request timeout */ |
| 681 | (req->flags & BF_SHUTR)) { /* input closed */ |
Willy Tarreau | 38c99bc | 2009-02-22 15:58:45 +0100 | [diff] [blame] | 682 | buffer_shutw_now(s->rep); |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 683 | s->ana_state = 0; |
| 684 | req->analysers = 0; |
| 685 | return 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 686 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 687 | /* don't forward nor abort */ |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 688 | req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */ |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 689 | return 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 690 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 691 | case STATS_ST_REP: |
| 692 | /* do nothing while response is being processed */ |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 693 | return 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 694 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 695 | case STATS_ST_CLOSE: |
| 696 | /* end of dump */ |
| 697 | s->req->analysers &= ~AN_REQ_UNIX_STATS; |
| 698 | s->ana_state = 0; |
| 699 | break; |
| 700 | } |
| 701 | return 0; |
| 702 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 703 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 704 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 705 | /* This function is the unix-stream equivalent of the global process_session(). |
| 706 | * It is currently limited to unix-stream processing on control sockets such as |
| 707 | * stats, and has no server-side. The two functions should be merged into one |
| 708 | * once client and server sides are better delimited. Note that the server-side |
| 709 | * still exists but remains in SI_ST_INI state forever, so that any call is a |
| 710 | * NOP. |
| 711 | */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 712 | struct task *uxst_process_session(struct task *t) |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 713 | { |
| 714 | struct session *s = t->context; |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 715 | int resync; |
| 716 | unsigned int rqf_last, rpf_last; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 717 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 718 | /* 1a: Check for low level timeouts if needed. We just set a flag on |
| 719 | * stream interfaces when their timeouts have expired. |
| 720 | */ |
| 721 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
| 722 | stream_int_check_timeouts(&s->si[0]); |
| 723 | buffer_check_timeouts(s->req); |
| 724 | buffer_check_timeouts(s->rep); |
| 725 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 726 | |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 727 | s->req->flags &= ~BF_READ_NOEXP; |
| 728 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 729 | /* copy req/rep flags so that we can detect shutdowns */ |
| 730 | rqf_last = s->req->flags; |
| 731 | rpf_last = s->rep->flags; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 732 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 733 | /* 1b: check for low-level errors reported at the stream interface. */ |
| 734 | if (unlikely(s->si[0].flags & SI_FL_ERR)) { |
| 735 | if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) { |
| 736 | s->si[0].shutr(&s->si[0]); |
| 737 | s->si[0].shutw(&s->si[0]); |
| 738 | stream_int_report_error(&s->si[0]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 739 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 740 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 741 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 742 | /* check buffer timeouts, and close the corresponding stream interfaces |
| 743 | * for future reads or writes. Note: this will also concern upper layers |
| 744 | * but we do not touch any other flag. We must be careful and correctly |
| 745 | * detect state changes when calling them. |
| 746 | */ |
| 747 | if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 748 | if (s->req->flags & BF_READ_TIMEOUT) |
| 749 | s->req->prod->shutr(s->req->prod); |
| 750 | if (s->req->flags & BF_WRITE_TIMEOUT) |
| 751 | s->req->cons->shutw(s->req->cons); |
| 752 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 753 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 754 | if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 755 | if (s->rep->flags & BF_READ_TIMEOUT) |
| 756 | s->rep->prod->shutr(s->rep->prod); |
| 757 | if (s->rep->flags & BF_WRITE_TIMEOUT) |
| 758 | s->rep->cons->shutw(s->rep->cons); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 759 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 760 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 761 | /* Check for connection closure */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 762 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 763 | resync_stream_interface: |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 764 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 765 | /* nothing special to be done on client side */ |
| 766 | if (unlikely(s->req->prod->state == SI_ST_DIS)) |
| 767 | s->req->prod->state = SI_ST_CLO; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 768 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 769 | /* |
| 770 | * Note: of the transient states (REQ, CER, DIS), only REQ may remain |
| 771 | * at this point. |
| 772 | */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 773 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 774 | resync_request: |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 775 | /**** Process layer 7 below ****/ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 776 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 777 | resync = 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 778 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 779 | /* Analyse request */ |
| 780 | if ((s->req->flags & BF_MASK_ANALYSER) || |
| 781 | (s->req->flags ^ rqf_last) & BF_MASK_STATIC) { |
| 782 | unsigned int flags = s->req->flags; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 783 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 784 | if (s->req->prod->state >= SI_ST_EST) { |
| 785 | /* it's up to the analysers to reset write_ena */ |
| 786 | buffer_write_ena(s->req); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 787 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 788 | /* We will call all analysers for which a bit is set in |
| 789 | * s->req->analysers, following the bit order from LSB |
| 790 | * to MSB. The analysers must remove themselves from |
| 791 | * the list when not needed. This while() loop is in |
| 792 | * fact a cleaner if(). |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 793 | */ |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 794 | while (s->req->analysers) { |
| 795 | if (s->req->analysers & AN_REQ_UNIX_STATS) |
| 796 | if (!uxst_req_analyser_stats(s, s->req)) |
| 797 | break; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 798 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 799 | /* Just make sure that nobody set a wrong flag causing an endless loop */ |
| 800 | s->req->analysers &= AN_REQ_UNIX_STATS; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 801 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 802 | /* we don't want to loop anyway */ |
| 803 | break; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 804 | } |
| 805 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 806 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 807 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | 06bea94 | 2009-03-21 22:09:29 +0100 | [diff] [blame] | 808 | if ((s->req->flags ^ flags) & BF_MASK_STATIC) |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 809 | resync = 1; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 810 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 811 | |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 812 | /* if noone is interested in analysing data, let's forward everything */ |
| 813 | if (!s->req->analysers && !(s->req->flags & BF_HIJACK)) |
| 814 | s->req->send_max = s->req->l; |
| 815 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 816 | /* If noone is interested in analysing data, it's time to forward |
| 817 | * everything. We will wake up from time to time when either send_max |
| 818 | * or to_forward are reached. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 819 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 820 | if (!s->req->analysers && |
| 821 | !(s->req->flags & (BF_HIJACK|BF_SHUTW)) && |
| 822 | (s->req->prod->state >= SI_ST_EST)) { |
| 823 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 824 | * attached to it. If any data are left in, we'll permit them to |
| 825 | * move. |
| 826 | */ |
| 827 | buffer_flush(s->req); |
| 828 | |
| 829 | /* If the producer is still connected, we'll schedule large blocks |
| 830 | * of data to be forwarded from the producer to the consumer (which |
| 831 | * might possibly not be connected yet). |
| 832 | */ |
| 833 | if (!(s->req->flags & BF_SHUTR) && |
| 834 | s->req->to_forward < FORWARD_DEFAULT_SIZE) |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 835 | buffer_forward(s->req, FORWARD_DEFAULT_SIZE); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 836 | } |
| 837 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 838 | /* reflect what the L7 analysers have seen last */ |
| 839 | rqf_last = s->req->flags; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 840 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 841 | /* |
| 842 | * Now forward all shutdown requests between both sides of the buffer |
| 843 | */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 844 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 845 | /* first, let's check if the request buffer needs to shutdown(write) */ |
| 846 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == |
| 847 | (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) |
| 848 | buffer_shutw_now(s->req); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 849 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 850 | /* shutdown(write) pending */ |
| 851 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW)) |
| 852 | s->req->cons->shutw(s->req->cons); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 853 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 854 | /* shutdown(write) done on server side, we must stop the client too */ |
| 855 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW && |
| 856 | !s->req->analysers)) |
| 857 | buffer_shutr_now(s->req); |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 858 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 859 | /* shutdown(read) pending */ |
| 860 | if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 861 | s->req->prod->shutr(s->req->prod); |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 862 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 863 | /* |
| 864 | * Here we want to check if we need to resync or not. |
| 865 | */ |
| 866 | if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) |
| 867 | resync = 1; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 868 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 869 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 870 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 871 | /* according to benchmarks, it makes sense to resync now */ |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 872 | if (s->req->prod->state == SI_ST_DIS) |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 873 | goto resync_stream_interface; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 874 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 875 | if (resync) |
| 876 | goto resync_request; |
| 877 | |
| 878 | resync_response: |
| 879 | resync = 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 880 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 881 | /* Analyse response */ |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 882 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 883 | /* In inject mode, we wake up everytime something has |
| 884 | * happened on the write side of the buffer. |
| 885 | */ |
| 886 | unsigned int flags = s->rep->flags; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 887 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 888 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 889 | !(s->rep->flags & BF_FULL)) { |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 890 | s->rep->hijacker(s, s->rep); |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 891 | } |
| 892 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 893 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | 06bea94 | 2009-03-21 22:09:29 +0100 | [diff] [blame] | 894 | if ((s->rep->flags ^ flags) & BF_MASK_STATIC) |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 895 | resync = 1; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 896 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 897 | else if ((s->rep->flags & BF_MASK_ANALYSER) || |
| 898 | (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) { |
| 899 | unsigned int flags = s->rep->flags; |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 900 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 901 | if (s->rep->prod->state >= SI_ST_EST) { |
| 902 | /* it's up to the analysers to reset write_ena */ |
| 903 | buffer_write_ena(s->rep); |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 904 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 905 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 906 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | 06bea94 | 2009-03-21 22:09:29 +0100 | [diff] [blame] | 907 | if ((s->rep->flags ^ flags) & BF_MASK_STATIC) |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 908 | resync = 1; |
| 909 | } |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 910 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 911 | /* If noone is interested in analysing data, it's time to forward |
| 912 | * everything. We will wake up from time to time when either send_max |
| 913 | * or to_forward are reached. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 914 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 915 | if (!s->rep->analysers && |
| 916 | !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) && |
| 917 | (s->rep->prod->state >= SI_ST_EST)) { |
| 918 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 919 | * attached to it. If any data are left in, we'll permit them to |
| 920 | * move. |
| 921 | */ |
| 922 | buffer_flush(s->rep); |
| 923 | |
| 924 | /* If the producer is still connected, we'll schedule large blocks |
| 925 | * of data to be forwarded from the producer to the consumer (which |
| 926 | * might possibly not be connected yet). |
| 927 | */ |
| 928 | if (!(s->rep->flags & BF_SHUTR) && |
| 929 | s->rep->to_forward < FORWARD_DEFAULT_SIZE) |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 930 | buffer_forward(s->rep, FORWARD_DEFAULT_SIZE); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 931 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 932 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 933 | /* reflect what the L7 analysers have seen last */ |
| 934 | rpf_last = s->rep->flags; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 935 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 936 | /* |
| 937 | * Now forward all shutdown requests between both sides of the buffer |
| 938 | */ |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 939 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 940 | /* |
| 941 | * FIXME: this is probably where we should produce error responses. |
| 942 | */ |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 943 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 944 | /* first, let's check if the request buffer needs to shutdown(write) */ |
| 945 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == |
| 946 | (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) |
| 947 | buffer_shutw_now(s->rep); |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 948 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 949 | /* shutdown(write) pending */ |
| 950 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW)) |
| 951 | s->rep->cons->shutw(s->rep->cons); |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 952 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 953 | /* shutdown(write) done on the client side, we must stop the server too */ |
| 954 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW)) |
| 955 | buffer_shutr_now(s->rep); |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 956 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 957 | /* shutdown(read) pending */ |
| 958 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 959 | s->rep->prod->shutr(s->rep->prod); |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 960 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 961 | /* |
| 962 | * Here we want to check if we need to resync or not. |
| 963 | */ |
| 964 | if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC) |
| 965 | resync = 1; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 966 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 967 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 968 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 969 | if (s->req->prod->state == SI_ST_DIS) |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 970 | goto resync_stream_interface; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 971 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 972 | if (s->req->flags != rqf_last) |
| 973 | goto resync_request; |
| 974 | |
| 975 | if (resync) |
| 976 | goto resync_response; |
| 977 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 978 | if (likely(s->rep->cons->state != SI_ST_CLO)) { |
| 979 | if (s->rep->cons->state == SI_ST_EST) |
| 980 | stream_sock_data_finish(s->rep->cons); |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 981 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 982 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 983 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 984 | s->si[0].prev_state = s->si[0].state; |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 985 | s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 986 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 987 | /* Trick: if a request is being waiting for the server to respond, |
| 988 | * and if we know the server can timeout, we don't want the timeout |
| 989 | * to expire on the client side first, but we're still interested |
| 990 | * in passing data from the client to the server (eg: POST). Thus, |
| 991 | * we can cancel the client's request timeout if the server's |
| 992 | * request timeout is set and the server has not yet sent a response. |
| 993 | */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 994 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 995 | if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 && |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 996 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) { |
| 997 | s->req->flags |= BF_READ_NOEXP; |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 998 | s->req->rex = TICK_ETERNITY; |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 999 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1000 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1001 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 1002 | tick_first(s->rep->rex, s->rep->wex)); |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 1003 | if (s->req->analysers) |
| 1004 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
| 1005 | |
| 1006 | if (s->si[0].exp) |
| 1007 | t->expire = tick_first(t->expire, s->si[0].exp); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1008 | |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1009 | return t; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | actconn--; |
Willy Tarreau | b5654f6 | 2008-12-07 16:45:10 +0100 | [diff] [blame] | 1013 | if (s->listener) { |
| 1014 | s->listener->nbconn--; |
| 1015 | if (s->listener->state == LI_FULL && |
| 1016 | s->listener->nbconn < s->listener->maxconn) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1017 | /* we should reactivate the listener */ |
Willy Tarreau | b5654f6 | 2008-12-07 16:45:10 +0100 | [diff] [blame] | 1018 | EV_FD_SET(s->listener->fd, DIR_RD); |
| 1019 | s->listener->state = LI_READY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | /* the task MUST not be in the run queue anymore */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1024 | session_free(s); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1025 | task_delete(t); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1026 | task_free(t); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1027 | return NULL; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1030 | __attribute__((constructor)) |
| 1031 | static void __uxst_protocol_init(void) |
| 1032 | { |
| 1033 | protocol_register(&proto_unix); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | /* |
| 1038 | * Local variables: |
| 1039 | * c-indent-level: 8 |
| 1040 | * c-basic-offset: 8 |
| 1041 | * End: |
| 1042 | */ |