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> |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 50 | #include <proto/senddata.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 51 | #include <proto/session.h> |
| 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; |
| 249 | |
| 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; |
| 259 | |
| 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; |
| 269 | fdtab[fd].owner = (struct task *)listener; /* reference the listener instead of a task */ |
| 270 | fdtab[fd].state = FD_STLISTEN; |
| 271 | fdtab[fd].peeraddr = NULL; |
| 272 | fdtab[fd].peerlen = 0; |
| 273 | fdtab[fd].listener = NULL; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 274 | return ERR_NONE; |
| 275 | } |
| 276 | |
| 277 | /* This function closes the UNIX sockets for the specified listener. |
| 278 | * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE. |
| 279 | */ |
| 280 | static int uxst_unbind_listener(struct listener *listener) |
| 281 | { |
| 282 | if (listener->state == LI_READY) |
| 283 | EV_FD_CLR(listener->fd, DIR_RD); |
| 284 | |
| 285 | if (listener->state >= LI_LISTEN) { |
Willy Tarreau | 8eebe5e | 2007-10-28 22:07:08 +0100 | [diff] [blame] | 286 | fd_delete(listener->fd); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 287 | listener->state = LI_ASSIGNED; |
| 288 | destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path); |
| 289 | } |
| 290 | return ERR_NONE; |
| 291 | } |
| 292 | |
| 293 | /* Add a listener to the list of unix stream listeners. The listener's state |
| 294 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 295 | * listeners is updated. This is the function to use to add a new listener. |
| 296 | */ |
| 297 | void uxst_add_listener(struct listener *listener) |
| 298 | { |
| 299 | if (listener->state != LI_INIT) |
| 300 | return; |
| 301 | listener->state = LI_ASSIGNED; |
| 302 | listener->proto = &proto_unix; |
| 303 | LIST_ADDQ(&proto_unix.listeners, &listener->proto_list); |
| 304 | proto_unix.nb_listeners++; |
| 305 | } |
| 306 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 307 | /******************************** |
| 308 | * 3) protocol-oriented functions |
| 309 | ********************************/ |
| 310 | |
| 311 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 312 | /* This function creates all UNIX sockets bound to the protocol entry <proto>. |
| 313 | * It is intended to be used as the protocol's bind_all() function. |
| 314 | * The sockets will be registered but not added to any fd_set, in order not to |
| 315 | * loose them across the fork(). A call to uxst_enable_listeners() is needed |
| 316 | * to complete initialization. |
| 317 | * |
| 318 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 319 | */ |
| 320 | static int uxst_bind_listeners(struct protocol *proto) |
| 321 | { |
| 322 | struct listener *listener; |
| 323 | int err = ERR_NONE; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 324 | |
| 325 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 326 | err |= uxst_bind_listener(listener); |
| 327 | if (err != ERR_NONE) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 328 | continue; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 329 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 330 | return err; |
| 331 | } |
| 332 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 333 | |
| 334 | /* This function stops all listening UNIX sockets bound to the protocol |
| 335 | * <proto>. It does not detaches them from the protocol. |
| 336 | * It always returns ERR_NONE. |
| 337 | */ |
| 338 | static int uxst_unbind_listeners(struct protocol *proto) |
| 339 | { |
| 340 | struct listener *listener; |
| 341 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 342 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 343 | uxst_unbind_listener(listener); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 344 | return ERR_NONE; |
| 345 | } |
| 346 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 347 | |
| 348 | /******************************** |
| 349 | * 4) high-level functions |
| 350 | ********************************/ |
| 351 | |
| 352 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 353 | /* |
| 354 | * This function is called on a read event from a listen socket, corresponding |
| 355 | * to an accept. It tries to accept as many connections as possible. |
| 356 | * It returns 0. Since we use UNIX sockets on the local system for monitoring |
| 357 | * purposes and other related things, we do not need to output as many messages |
| 358 | * as with TCP which can fall under attack. |
| 359 | */ |
| 360 | int uxst_event_accept(int fd) { |
| 361 | struct listener *l = (struct listener *)fdtab[fd].owner; |
| 362 | struct session *s; |
| 363 | struct task *t; |
| 364 | int cfd; |
| 365 | int max_accept; |
| 366 | |
| 367 | if (global.nbproc > 1) |
| 368 | max_accept = 8; /* let other processes catch some connections too */ |
| 369 | else |
| 370 | max_accept = -1; |
| 371 | |
| 372 | while (max_accept--) { |
| 373 | struct sockaddr_storage addr; |
| 374 | socklen_t laddr = sizeof(addr); |
| 375 | |
| 376 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) { |
| 377 | switch (errno) { |
| 378 | case EAGAIN: |
| 379 | case EINTR: |
| 380 | case ECONNABORTED: |
| 381 | return 0; /* nothing more to accept */ |
| 382 | case ENFILE: |
| 383 | /* Process reached system FD limit. Check system tunables. */ |
| 384 | return 0; |
| 385 | case EMFILE: |
| 386 | /* Process reached process FD limit. Check 'ulimit-n'. */ |
| 387 | return 0; |
| 388 | case ENOBUFS: |
| 389 | case ENOMEM: |
| 390 | /* Process reached system memory limit. Check system tunables. */ |
| 391 | return 0; |
| 392 | default: |
| 393 | return 0; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (l->nbconn >= l->maxconn) { |
| 398 | /* too many connections, we shoot this one and return. |
| 399 | * FIXME: it would be better to simply switch the listener's |
| 400 | * state to LI_FULL and disable the FD. We could re-enable |
| 401 | * it upon fd_delete(), but this requires all protocols to |
| 402 | * be switched. |
| 403 | */ |
| 404 | close(cfd); |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | if ((s = pool_alloc2(pool2_session)) == NULL) { |
| 409 | Alert("out of memory in uxst_event_accept().\n"); |
| 410 | close(cfd); |
| 411 | return 0; |
| 412 | } |
| 413 | |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 414 | s->flags = 0; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 415 | s->analysis = 0; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 416 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 417 | if ((t = pool_alloc2(pool2_task)) == NULL) { |
| 418 | Alert("out of memory in uxst_event_accept().\n"); |
| 419 | close(cfd); |
| 420 | pool_free2(pool2_session, s); |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | s->cli_addr = addr; |
| 425 | |
| 426 | /* FIXME: should be checked earlier */ |
| 427 | if (cfd >= global.maxsock) { |
| 428 | Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 429 | close(cfd); |
| 430 | pool_free2(pool2_task, t); |
| 431 | pool_free2(pool2_session, s); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | if (fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) { |
| 436 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
| 437 | close(cfd); |
| 438 | pool_free2(pool2_task, t); |
| 439 | pool_free2(pool2_session, s); |
| 440 | return 0; |
| 441 | } |
| 442 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 443 | task_init(t); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 444 | t->process = l->handler; |
| 445 | t->context = s; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 446 | t->nice = -64; /* we want to boost priority for local stats */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 447 | |
| 448 | s->task = t; |
| 449 | s->fe = NULL; |
| 450 | s->be = NULL; |
| 451 | |
| 452 | s->cli_state = CL_STDATA; |
| 453 | s->srv_state = SV_STIDLE; |
| 454 | s->req = s->rep = NULL; /* will be allocated later */ |
| 455 | |
| 456 | s->cli_fd = cfd; |
| 457 | s->srv_fd = -1; |
| 458 | s->srv = NULL; |
| 459 | s->pend_pos = NULL; |
| 460 | |
| 461 | memset(&s->logs, 0, sizeof(s->logs)); |
| 462 | memset(&s->txn, 0, sizeof(s->txn)); |
| 463 | |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 464 | s->data_state = DATA_ST_INIT; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 465 | s->data_source = DATA_SRC_NONE; |
| 466 | s->uniq_id = totalconn; |
| 467 | |
| 468 | if ((s->req = pool_alloc2(pool2_buffer)) == NULL) { /* no memory */ |
| 469 | close(cfd); /* nothing can be done for this fd without memory */ |
| 470 | pool_free2(pool2_task, t); |
| 471 | pool_free2(pool2_session, s); |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | if ((s->rep = pool_alloc2(pool2_buffer)) == NULL) { /* no memory */ |
| 476 | pool_free2(pool2_buffer, s->req); |
| 477 | close(cfd); /* nothing can be done for this fd without memory */ |
| 478 | pool_free2(pool2_task, t); |
| 479 | pool_free2(pool2_session, s); |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | buffer_init(s->req); |
| 484 | buffer_init(s->rep); |
| 485 | s->req->rlim += BUFSIZE; |
| 486 | s->rep->rlim += BUFSIZE; |
| 487 | |
| 488 | fd_insert(cfd); |
| 489 | fdtab[cfd].owner = t; |
| 490 | fdtab[cfd].listener = l; |
| 491 | fdtab[cfd].state = FD_STREADY; |
| 492 | fdtab[cfd].cb[DIR_RD].f = l->proto->read; |
| 493 | fdtab[cfd].cb[DIR_RD].b = s->req; |
| 494 | fdtab[cfd].cb[DIR_WR].f = l->proto->write; |
| 495 | fdtab[cfd].cb[DIR_WR].b = s->rep; |
| 496 | fdtab[cfd].peeraddr = (struct sockaddr *)&s->cli_addr; |
| 497 | fdtab[cfd].peerlen = sizeof(s->cli_addr); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 498 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 499 | s->req->rex = TICK_ETERNITY; |
| 500 | s->req->wex = TICK_ETERNITY; |
| 501 | s->req->cex = TICK_ETERNITY; |
| 502 | s->rep->rex = TICK_ETERNITY; |
| 503 | s->rep->wex = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 504 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 505 | s->req->wto = TICK_ETERNITY; |
| 506 | s->req->cto = TICK_ETERNITY; |
| 507 | s->req->rto = TICK_ETERNITY; |
| 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 | |
| 512 | if (l->timeout) |
| 513 | s->req->rto = *l->timeout; |
| 514 | |
| 515 | if (l->timeout) |
| 516 | s->rep->wto = *l->timeout; |
| 517 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 518 | t->expire = TICK_ETERNITY; |
| 519 | if (l->timeout && *l->timeout) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 520 | EV_FD_SET(cfd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 521 | s->req->rex = tick_add(now_ms, s->req->rto); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 522 | t->expire = s->req->rex; |
| 523 | } |
| 524 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 525 | task_wakeup(t); |
| 526 | |
| 527 | l->nbconn++; /* warning! right now, it's up to the handler to decrease this */ |
| 528 | if (l->nbconn >= l->maxconn) { |
| 529 | EV_FD_CLR(l->fd, DIR_RD); |
| 530 | l->state = LI_FULL; |
| 531 | } |
| 532 | actconn++; |
| 533 | totalconn++; |
| 534 | |
| 535 | //fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p, cfd=%d, maxfd=%d\n", p, actconn, totalconn, t, cfd, maxfd); |
| 536 | } /* end of while (p->feconn < p->maxconn) */ |
| 537 | //fprintf(stderr,"fct %s:%d\n", __FUNCTION__, __LINE__); |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * manages the client FSM and its socket. It returns 1 if a state has changed |
| 543 | * (and a resync may be needed), otherwise 0. |
| 544 | */ |
| 545 | static int process_uxst_cli(struct session *t) |
| 546 | { |
| 547 | int s = t->srv_state; |
| 548 | int c = t->cli_state; |
| 549 | struct buffer *req = t->req; |
| 550 | struct buffer *rep = t->rep; |
| 551 | //fprintf(stderr,"fct %s:%d\n", __FUNCTION__, __LINE__); |
| 552 | if (c == CL_STDATA) { |
| 553 | /* FIXME: this error handling is partly buggy because we always report |
| 554 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 555 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 556 | * we're waiting for the server to connect. |
| 557 | */ |
| 558 | /* read or write error */ |
| 559 | if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 560 | buffer_shutr_done(req); |
| 561 | buffer_shutw_done(rep); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 562 | fd_delete(t->cli_fd); |
| 563 | t->cli_state = CL_STCLOSE; |
| 564 | if (!(t->flags & SN_ERR_MASK)) |
| 565 | t->flags |= SN_ERR_CLICL; |
| 566 | if (!(t->flags & SN_FINST_MASK)) { |
| 567 | if (t->pend_pos) |
| 568 | t->flags |= SN_FINST_Q; |
| 569 | else if (s == SV_STCONN) |
| 570 | t->flags |= SN_FINST_C; |
| 571 | else |
| 572 | t->flags |= SN_FINST_D; |
| 573 | } |
| 574 | return 1; |
| 575 | } |
| 576 | /* last read, or end of server write */ |
| 577 | else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
| 578 | EV_FD_CLR(t->cli_fd, DIR_RD); |
| 579 | buffer_shutr(req); |
| 580 | t->cli_state = CL_STSHUTR; |
| 581 | return 1; |
| 582 | } |
| 583 | /* last server read and buffer empty */ |
| 584 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
| 585 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 586 | buffer_shutw_done(rep); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 587 | shutdown(t->cli_fd, SHUT_WR); |
| 588 | /* We must ensure that the read part is still alive when switching |
| 589 | * to shutw */ |
| 590 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 591 | req->rex = tick_add_ifset(now_ms, req->rto); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 592 | t->cli_state = CL_STSHUTW; |
| 593 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 594 | return 1; |
| 595 | } |
| 596 | /* read timeout */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 597 | else if (tick_is_expired(req->rex, now_ms)) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 598 | EV_FD_CLR(t->cli_fd, DIR_RD); |
| 599 | buffer_shutr(req); |
| 600 | t->cli_state = CL_STSHUTR; |
| 601 | if (!(t->flags & SN_ERR_MASK)) |
| 602 | t->flags |= SN_ERR_CLITO; |
| 603 | if (!(t->flags & SN_FINST_MASK)) { |
| 604 | if (t->pend_pos) |
| 605 | t->flags |= SN_FINST_Q; |
| 606 | else if (s == SV_STCONN) |
| 607 | t->flags |= SN_FINST_C; |
| 608 | else |
| 609 | t->flags |= SN_FINST_D; |
| 610 | } |
| 611 | return 1; |
| 612 | } |
| 613 | /* write timeout */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 614 | else if (tick_is_expired(rep->wex, now_ms)) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 615 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 616 | buffer_shutw_done(rep); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 617 | shutdown(t->cli_fd, SHUT_WR); |
| 618 | /* We must ensure that the read part is still alive when switching |
| 619 | * to shutw */ |
| 620 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 621 | req->rex = tick_add_ifset(now_ms, req->rto); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 622 | |
| 623 | t->cli_state = CL_STSHUTW; |
| 624 | if (!(t->flags & SN_ERR_MASK)) |
| 625 | t->flags |= SN_ERR_CLITO; |
| 626 | if (!(t->flags & SN_FINST_MASK)) { |
| 627 | if (t->pend_pos) |
| 628 | t->flags |= SN_FINST_Q; |
| 629 | else if (s == SV_STCONN) |
| 630 | t->flags |= SN_FINST_C; |
| 631 | else |
| 632 | t->flags |= SN_FINST_D; |
| 633 | } |
| 634 | return 1; |
| 635 | } |
| 636 | |
| 637 | if (req->l >= req->rlim - req->data) { |
| 638 | /* no room to read more data */ |
| 639 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
| 640 | /* stop reading until we get some space */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 641 | req->rex = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 642 | } |
| 643 | } else { |
| 644 | /* there's still some space in the buffer */ |
| 645 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 646 | if (!req->rto || |
| 647 | (t->srv_state < SV_STDATA && req->wto)) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 648 | /* If the client has no timeout, or if the server not ready yet, and we |
| 649 | * know for sure that it can expire, then it's cleaner to disable the |
| 650 | * timeout on the client side so that too low values cannot make the |
| 651 | * sessions abort too early. |
| 652 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 653 | req->rex = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 654 | else |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 655 | req->rex = tick_add(now_ms, req->rto); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
| 659 | if ((rep->l == 0) || |
| 660 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
| 661 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 662 | /* stop writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 663 | rep->wex = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 664 | } |
| 665 | } else { |
| 666 | /* buffer not empty */ |
| 667 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 668 | /* restart writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 669 | rep->wex = tick_add_ifset(now_ms, rep->wto); |
| 670 | if (rep->wex) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 671 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 672 | * we refresh it. */ |
| 673 | req->rex = rep->wex; |
| 674 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | return 0; /* other cases change nothing */ |
| 678 | } |
| 679 | else if (c == CL_STSHUTR) { |
| 680 | if (rep->flags & BF_WRITE_ERROR) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 681 | buffer_shutw_done(rep); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 682 | fd_delete(t->cli_fd); |
| 683 | t->cli_state = CL_STCLOSE; |
| 684 | if (!(t->flags & SN_ERR_MASK)) |
| 685 | t->flags |= SN_ERR_CLICL; |
| 686 | if (!(t->flags & SN_FINST_MASK)) { |
| 687 | if (t->pend_pos) |
| 688 | t->flags |= SN_FINST_Q; |
| 689 | else if (s == SV_STCONN) |
| 690 | t->flags |= SN_FINST_C; |
| 691 | else |
| 692 | t->flags |= SN_FINST_D; |
| 693 | } |
| 694 | return 1; |
| 695 | } |
| 696 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 697 | buffer_shutw_done(rep); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 698 | fd_delete(t->cli_fd); |
| 699 | t->cli_state = CL_STCLOSE; |
| 700 | return 1; |
| 701 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 702 | else if (tick_is_expired(rep->wex, now_ms)) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 703 | buffer_shutw_done(rep); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 704 | fd_delete(t->cli_fd); |
| 705 | t->cli_state = CL_STCLOSE; |
| 706 | if (!(t->flags & SN_ERR_MASK)) |
| 707 | t->flags |= SN_ERR_CLITO; |
| 708 | if (!(t->flags & SN_FINST_MASK)) { |
| 709 | if (t->pend_pos) |
| 710 | t->flags |= SN_FINST_Q; |
| 711 | else if (s == SV_STCONN) |
| 712 | t->flags |= SN_FINST_C; |
| 713 | else |
| 714 | t->flags |= SN_FINST_D; |
| 715 | } |
| 716 | return 1; |
| 717 | } |
| 718 | |
| 719 | if (rep->l == 0) { |
| 720 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 721 | /* stop writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 722 | rep->wex = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 723 | } |
| 724 | } else { |
| 725 | /* buffer not empty */ |
| 726 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 727 | /* restart writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 728 | rep->wex = tick_add_ifset(now_ms, rep->wto); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | return 0; |
| 732 | } |
| 733 | else if (c == CL_STSHUTW) { |
| 734 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 735 | buffer_shutr_done(req); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 736 | fd_delete(t->cli_fd); |
| 737 | t->cli_state = CL_STCLOSE; |
| 738 | if (!(t->flags & SN_ERR_MASK)) |
| 739 | t->flags |= SN_ERR_CLICL; |
| 740 | if (!(t->flags & SN_FINST_MASK)) { |
| 741 | if (t->pend_pos) |
| 742 | t->flags |= SN_FINST_Q; |
| 743 | else if (s == SV_STCONN) |
| 744 | t->flags |= SN_FINST_C; |
| 745 | else |
| 746 | t->flags |= SN_FINST_D; |
| 747 | } |
| 748 | return 1; |
| 749 | } |
| 750 | else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 751 | buffer_shutr_done(req); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 752 | fd_delete(t->cli_fd); |
| 753 | t->cli_state = CL_STCLOSE; |
| 754 | return 1; |
| 755 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 756 | else if (tick_is_expired(req->rex, now_ms)) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 757 | buffer_shutr_done(req); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 758 | fd_delete(t->cli_fd); |
| 759 | t->cli_state = CL_STCLOSE; |
| 760 | if (!(t->flags & SN_ERR_MASK)) |
| 761 | t->flags |= SN_ERR_CLITO; |
| 762 | if (!(t->flags & SN_FINST_MASK)) { |
| 763 | if (t->pend_pos) |
| 764 | t->flags |= SN_FINST_Q; |
| 765 | else if (s == SV_STCONN) |
| 766 | t->flags |= SN_FINST_C; |
| 767 | else |
| 768 | t->flags |= SN_FINST_D; |
| 769 | } |
| 770 | return 1; |
| 771 | } |
| 772 | else if (req->l >= req->rlim - req->data) { |
| 773 | /* no room to read more data */ |
| 774 | |
| 775 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 776 | * after the timeout by sending more data after it receives a close ? |
| 777 | */ |
| 778 | |
| 779 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
| 780 | /* stop reading until we get some space */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 781 | req->rex = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 782 | } |
| 783 | } else { |
| 784 | /* there's still some space in the buffer */ |
| 785 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 786 | req->rex = tick_add_ifset(now_ms, req->rto); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | return 0; |
| 790 | } |
| 791 | else { /* CL_STCLOSE: nothing to do */ |
| 792 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 793 | int len; |
| 794 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be?t->be->id:"", |
| 795 | (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 796 | write(1, trash, len); |
| 797 | } |
| 798 | return 0; |
| 799 | } |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | #if 0 |
| 804 | /* FIXME! This part has not been completely converted yet, and it may |
| 805 | * still be very specific to TCPv4 ! Also, it relies on some parameters |
| 806 | * such as conn_retries which are not set upon accept(). |
| 807 | */ |
| 808 | /* |
| 809 | * Manages the server FSM and its socket. It returns 1 if a state has changed |
| 810 | * (and a resync may be needed), otherwise 0. |
| 811 | */ |
| 812 | static int process_uxst_srv(struct session *t) |
| 813 | { |
| 814 | int s = t->srv_state; |
| 815 | int c = t->cli_state; |
| 816 | struct buffer *req = t->req; |
| 817 | struct buffer *rep = t->rep; |
| 818 | int conn_err; |
| 819 | |
| 820 | if (s == SV_STIDLE) { |
| 821 | if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 822 | (c == CL_STSHUTR && |
| 823 | (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 824 | tv_eternity(&req->cex); |
| 825 | if (t->pend_pos) |
| 826 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 827 | srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C); |
| 828 | return 1; |
| 829 | } |
| 830 | else { |
| 831 | /* FIXME: reimplement the TARPIT check here */ |
| 832 | |
| 833 | /* Right now, we will need to create a connection to the server. |
| 834 | * We might already have tried, and got a connection pending, in |
| 835 | * which case we will not do anything till it's pending. It's up |
| 836 | * to any other session to release it and wake us up again. |
| 837 | */ |
| 838 | if (t->pend_pos) { |
| 839 | if (!tv_isle(&req->cex, &now)) |
| 840 | return 0; |
| 841 | else { |
| 842 | /* we've been waiting too long here */ |
| 843 | tv_eternity(&req->cex); |
| 844 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 845 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q); |
| 846 | if (t->srv) |
| 847 | t->srv->failed_conns++; |
| 848 | if (t->fe) |
| 849 | t->fe->failed_conns++; |
| 850 | return 1; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | do { |
| 855 | /* first, get a connection */ |
| 856 | if (srv_redispatch_connect(t)) |
| 857 | return t->srv_state != SV_STIDLE; |
| 858 | |
| 859 | /* try to (re-)connect to the server, and fail if we expire the |
| 860 | * number of retries. |
| 861 | */ |
| 862 | if (srv_retryable_connect(t)) { |
| 863 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 864 | return t->srv_state != SV_STIDLE; |
| 865 | } |
| 866 | } while (1); |
| 867 | } |
| 868 | } |
| 869 | else if (s == SV_STCONN) { /* connection in progress */ |
| 870 | if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 871 | (c == CL_STSHUTR && |
| 872 | ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) || |
| 873 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 874 | tv_eternity(&req->cex); |
| 875 | fd_delete(t->srv_fd); |
| 876 | if (t->srv) |
| 877 | t->srv->cur_sess--; |
| 878 | |
| 879 | srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C); |
| 880 | return 1; |
| 881 | } |
| 882 | if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) { |
| 883 | //fprintf(stderr,"1: c=%d, s=%d, now=%d.%06d, exp=%d.%06d\n", c, s, now.tv_sec, now.tv_usec, req->cex.tv_sec, req->cex.tv_usec); |
| 884 | return 0; /* nothing changed */ |
| 885 | } |
| 886 | else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
| 887 | /* timeout, asynchronous connect error or first write error */ |
| 888 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 889 | |
| 890 | fd_delete(t->srv_fd); |
| 891 | if (t->srv) |
| 892 | t->srv->cur_sess--; |
| 893 | |
| 894 | if (!(req->flags & BF_WRITE_STATUS)) |
| 895 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 896 | else |
| 897 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 898 | |
| 899 | /* ensure that we have enough retries left */ |
| 900 | if (srv_count_retry_down(t, conn_err)) |
| 901 | return 1; |
| 902 | |
| 903 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
| 904 | /* We're on our last chance, and the REDISP option was specified. |
| 905 | * We will ignore cookie and force to balance or use the dispatcher. |
| 906 | */ |
| 907 | /* let's try to offer this slot to anybody */ |
| 908 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 909 | process_srv_queue(t->srv); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 910 | |
| 911 | if (t->srv) |
| 912 | t->srv->failed_conns++; |
| 913 | t->be->failed_conns++; |
| 914 | |
| 915 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 916 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
| 917 | |
| 918 | /* first, get a connection */ |
| 919 | if (srv_redispatch_connect(t)) |
| 920 | return t->srv_state != SV_STIDLE; |
| 921 | } |
| 922 | |
| 923 | do { |
| 924 | /* Now we will try to either reconnect to the same server or |
| 925 | * connect to another server. If the connection gets queued |
| 926 | * because all servers are saturated, then we will go back to |
| 927 | * the SV_STIDLE state. |
| 928 | */ |
| 929 | if (srv_retryable_connect(t)) { |
| 930 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 931 | return t->srv_state != SV_STCONN; |
| 932 | } |
| 933 | |
| 934 | /* we need to redispatch the connection to another server */ |
| 935 | if (srv_redispatch_connect(t)) |
| 936 | return t->srv_state != SV_STCONN; |
| 937 | } while (1); |
| 938 | } |
| 939 | else { /* no error or write 0 */ |
| 940 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 941 | |
| 942 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
| 943 | if (req->l == 0) /* nothing to write */ { |
| 944 | EV_FD_CLR(t->srv_fd, DIR_WR); |
| 945 | tv_eternity(&req->wex); |
| 946 | } else /* need the right to write */ { |
| 947 | EV_FD_SET(t->srv_fd, DIR_WR); |
| 948 | if (tv_add_ifset(&req->wex, &now, &req->wto)) { |
| 949 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 950 | * we refresh it. */ |
| 951 | rep->rex = req->wex; |
| 952 | } |
| 953 | else |
| 954 | tv_eternity(&req->wex); |
| 955 | } |
| 956 | |
| 957 | EV_FD_SET(t->srv_fd, DIR_RD); |
| 958 | if (!tv_add_ifset(&rep->rex, &now, &rep->rto)) |
| 959 | tv_eternity(&rep->rex); |
| 960 | |
| 961 | t->srv_state = SV_STDATA; |
| 962 | if (t->srv) |
| 963 | t->srv->cum_sess++; |
| 964 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
| 965 | |
| 966 | /* if the user wants to log as soon as possible, without counting |
| 967 | bytes from the server, then this is the right moment. */ |
| 968 | if (t->fe && t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 969 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 970 | //uxst_sess_log(t); |
| 971 | } |
| 972 | tv_eternity(&req->cex); |
| 973 | return 1; |
| 974 | } |
| 975 | } |
| 976 | else if (s == SV_STDATA) { |
| 977 | /* read or write error */ |
| 978 | if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 979 | buffer_shutr_done(rep); |
| 980 | buffer_shutw_done(req); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 981 | fd_delete(t->srv_fd); |
| 982 | if (t->srv) { |
| 983 | t->srv->cur_sess--; |
| 984 | t->srv->failed_resp++; |
| 985 | } |
| 986 | t->be->failed_resp++; |
| 987 | t->srv_state = SV_STCLOSE; |
| 988 | if (!(t->flags & SN_ERR_MASK)) |
| 989 | t->flags |= SN_ERR_SRVCL; |
| 990 | if (!(t->flags & SN_FINST_MASK)) |
| 991 | t->flags |= SN_FINST_D; |
| 992 | /* We used to have a free connection slot. Since we'll never use it, |
| 993 | * we have to inform the server that it may be used by another session. |
| 994 | */ |
| 995 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 996 | process_srv_queue(t->srv); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 997 | |
| 998 | return 1; |
| 999 | } |
| 1000 | /* last read, or end of client write */ |
| 1001 | else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
| 1002 | EV_FD_CLR(t->srv_fd, DIR_RD); |
| 1003 | buffer_shutr(rep); |
| 1004 | t->srv_state = SV_STSHUTR; |
| 1005 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 1006 | return 1; |
| 1007 | } |
| 1008 | /* end of client read and no more data to send */ |
| 1009 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
| 1010 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1011 | buffer_shutw_done(req); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1012 | shutdown(t->srv_fd, SHUT_WR); |
| 1013 | /* We must ensure that the read part is still alive when switching |
| 1014 | * to shutw */ |
| 1015 | EV_FD_SET(t->srv_fd, DIR_RD); |
| 1016 | tv_add_ifset(&rep->rex, &now, &rep->rto); |
| 1017 | |
| 1018 | t->srv_state = SV_STSHUTW; |
| 1019 | return 1; |
| 1020 | } |
| 1021 | /* read timeout */ |
| 1022 | else if (tv_isle(&rep->rex, &now)) { |
| 1023 | EV_FD_CLR(t->srv_fd, DIR_RD); |
| 1024 | buffer_shutr(rep); |
| 1025 | t->srv_state = SV_STSHUTR; |
| 1026 | if (!(t->flags & SN_ERR_MASK)) |
| 1027 | t->flags |= SN_ERR_SRVTO; |
| 1028 | if (!(t->flags & SN_FINST_MASK)) |
| 1029 | t->flags |= SN_FINST_D; |
| 1030 | return 1; |
| 1031 | } |
| 1032 | /* write timeout */ |
| 1033 | else if (tv_isle(&req->wex, &now)) { |
| 1034 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1035 | buffer_shutw_done(req); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1036 | shutdown(t->srv_fd, SHUT_WR); |
| 1037 | /* We must ensure that the read part is still alive when switching |
| 1038 | * to shutw */ |
| 1039 | EV_FD_SET(t->srv_fd, DIR_RD); |
| 1040 | tv_add_ifset(&rep->rex, &now, &rep->rto); |
| 1041 | t->srv_state = SV_STSHUTW; |
| 1042 | if (!(t->flags & SN_ERR_MASK)) |
| 1043 | t->flags |= SN_ERR_SRVTO; |
| 1044 | if (!(t->flags & SN_FINST_MASK)) |
| 1045 | t->flags |= SN_FINST_D; |
| 1046 | return 1; |
| 1047 | } |
| 1048 | |
| 1049 | /* recompute request time-outs */ |
| 1050 | if (req->l == 0) { |
| 1051 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 1052 | /* stop writing */ |
| 1053 | tv_eternity(&req->wex); |
| 1054 | } |
| 1055 | } |
| 1056 | else { /* buffer not empty, there are still data to be transferred */ |
| 1057 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 1058 | /* restart writing */ |
| 1059 | if (tv_add_ifset(&req->wex, &now, &req->wto)) { |
| 1060 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 1061 | * we refresh it. */ |
| 1062 | rep->rex = req->wex; |
| 1063 | } |
| 1064 | else |
| 1065 | tv_eternity(&req->wex); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | /* recompute response time-outs */ |
| 1070 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 1071 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
| 1072 | tv_eternity(&rep->rex); |
| 1073 | } |
| 1074 | } |
| 1075 | else { |
| 1076 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
| 1077 | if (!tv_add_ifset(&rep->rex, &now, &rep->rto)) |
| 1078 | tv_eternity(&rep->rex); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | return 0; /* other cases change nothing */ |
| 1083 | } |
| 1084 | else if (s == SV_STSHUTR) { |
| 1085 | if (req->flags & BF_WRITE_ERROR) { |
| 1086 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1087 | buffer_shutw_done(req); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1088 | fd_delete(t->srv_fd); |
| 1089 | if (t->srv) { |
| 1090 | t->srv->cur_sess--; |
| 1091 | t->srv->failed_resp++; |
| 1092 | } |
| 1093 | t->be->failed_resp++; |
| 1094 | //close(t->srv_fd); |
| 1095 | t->srv_state = SV_STCLOSE; |
| 1096 | if (!(t->flags & SN_ERR_MASK)) |
| 1097 | t->flags |= SN_ERR_SRVCL; |
| 1098 | if (!(t->flags & SN_FINST_MASK)) |
| 1099 | t->flags |= SN_FINST_D; |
| 1100 | /* We used to have a free connection slot. Since we'll never use it, |
| 1101 | * we have to inform the server that it may be used by another session. |
| 1102 | */ |
| 1103 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1104 | process_srv_queue(t->srv); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1105 | |
| 1106 | return 1; |
| 1107 | } |
| 1108 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
| 1109 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1110 | buffer_shutw_done(req); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1111 | fd_delete(t->srv_fd); |
| 1112 | if (t->srv) |
| 1113 | t->srv->cur_sess--; |
| 1114 | //close(t->srv_fd); |
| 1115 | t->srv_state = SV_STCLOSE; |
| 1116 | /* We used to have a free connection slot. Since we'll never use it, |
| 1117 | * we have to inform the server that it may be used by another session. |
| 1118 | */ |
| 1119 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1120 | process_srv_queue(t->srv); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1121 | |
| 1122 | return 1; |
| 1123 | } |
| 1124 | else if (tv_isle(&req->wex, &now)) { |
| 1125 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1126 | buffer_shutw_done(req); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1127 | fd_delete(t->srv_fd); |
| 1128 | if (t->srv) |
| 1129 | t->srv->cur_sess--; |
| 1130 | //close(t->srv_fd); |
| 1131 | t->srv_state = SV_STCLOSE; |
| 1132 | if (!(t->flags & SN_ERR_MASK)) |
| 1133 | t->flags |= SN_ERR_SRVTO; |
| 1134 | if (!(t->flags & SN_FINST_MASK)) |
| 1135 | t->flags |= SN_FINST_D; |
| 1136 | /* We used to have a free connection slot. Since we'll never use it, |
| 1137 | * we have to inform the server that it may be used by another session. |
| 1138 | */ |
| 1139 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1140 | process_srv_queue(t->srv); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1141 | |
| 1142 | return 1; |
| 1143 | } |
| 1144 | else if (req->l == 0) { |
| 1145 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 1146 | /* stop writing */ |
| 1147 | tv_eternity(&req->wex); |
| 1148 | } |
| 1149 | } |
| 1150 | else { /* buffer not empty */ |
| 1151 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 1152 | /* restart writing */ |
| 1153 | if (!tv_add_ifset(&req->wex, &now, &req->wto)) |
| 1154 | tv_eternity(&req->wex); |
| 1155 | } |
| 1156 | } |
| 1157 | return 0; |
| 1158 | } |
| 1159 | else if (s == SV_STSHUTW) { |
| 1160 | if (rep->flags & BF_READ_ERROR) { |
| 1161 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1162 | buffer_shutr_done(rep); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1163 | fd_delete(t->srv_fd); |
| 1164 | if (t->srv) { |
| 1165 | t->srv->cur_sess--; |
| 1166 | t->srv->failed_resp++; |
| 1167 | } |
| 1168 | t->be->failed_resp++; |
| 1169 | //close(t->srv_fd); |
| 1170 | t->srv_state = SV_STCLOSE; |
| 1171 | if (!(t->flags & SN_ERR_MASK)) |
| 1172 | t->flags |= SN_ERR_SRVCL; |
| 1173 | if (!(t->flags & SN_FINST_MASK)) |
| 1174 | t->flags |= SN_FINST_D; |
| 1175 | /* We used to have a free connection slot. Since we'll never use it, |
| 1176 | * we have to inform the server that it may be used by another session. |
| 1177 | */ |
| 1178 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1179 | process_srv_queue(t->srv); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1180 | |
| 1181 | return 1; |
| 1182 | } |
| 1183 | else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
| 1184 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1185 | buffer_shutr_done(rep); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1186 | fd_delete(t->srv_fd); |
| 1187 | if (t->srv) |
| 1188 | t->srv->cur_sess--; |
| 1189 | //close(t->srv_fd); |
| 1190 | t->srv_state = SV_STCLOSE; |
| 1191 | /* We used to have a free connection slot. Since we'll never use it, |
| 1192 | * we have to inform the server that it may be used by another session. |
| 1193 | */ |
| 1194 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1195 | process_srv_queue(t->srv); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1196 | |
| 1197 | return 1; |
| 1198 | } |
| 1199 | else if (tv_isle(&rep->rex, &now)) { |
| 1200 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1201 | buffer_shutr_done(rep); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1202 | fd_delete(t->srv_fd); |
| 1203 | if (t->srv) |
| 1204 | t->srv->cur_sess--; |
| 1205 | //close(t->srv_fd); |
| 1206 | t->srv_state = SV_STCLOSE; |
| 1207 | if (!(t->flags & SN_ERR_MASK)) |
| 1208 | t->flags |= SN_ERR_SRVTO; |
| 1209 | if (!(t->flags & SN_FINST_MASK)) |
| 1210 | t->flags |= SN_FINST_D; |
| 1211 | /* We used to have a free connection slot. Since we'll never use it, |
| 1212 | * we have to inform the server that it may be used by another session. |
| 1213 | */ |
| 1214 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1215 | process_srv_queue(t->srv); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1216 | |
| 1217 | return 1; |
| 1218 | } |
| 1219 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 1220 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
| 1221 | tv_eternity(&rep->rex); |
| 1222 | } |
| 1223 | } |
| 1224 | else { |
| 1225 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
| 1226 | if (!tv_add_ifset(&rep->rex, &now, &rep->rto)) |
| 1227 | tv_eternity(&rep->rex); |
| 1228 | } |
| 1229 | } |
| 1230 | return 0; |
| 1231 | } |
| 1232 | else { /* SV_STCLOSE : nothing to do */ |
| 1233 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 1234 | int len; |
| 1235 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 1236 | t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 1237 | write(1, trash, len); |
| 1238 | } |
| 1239 | return 0; |
| 1240 | } |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | /* Processes the client and server jobs of a session task, then |
| 1245 | * puts it back to the wait queue in a clean state, or |
| 1246 | * cleans up its resources if it must be deleted. Returns |
| 1247 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 1248 | * infinity. |
| 1249 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1250 | void process_uxst_session(struct task *t, int *next) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1251 | { |
| 1252 | struct session *s = t->context; |
| 1253 | int fsm_resync = 0; |
| 1254 | |
| 1255 | do { |
| 1256 | fsm_resync = 0; |
| 1257 | fsm_resync |= process_uxst_cli(s); |
| 1258 | if (s->srv_state == SV_STIDLE) { |
| 1259 | if (s->cli_state == CL_STCLOSE || s->cli_state == CL_STSHUTW) { |
| 1260 | s->srv_state = SV_STCLOSE; |
| 1261 | fsm_resync |= 1; |
| 1262 | continue; |
| 1263 | } |
| 1264 | if (s->cli_state == CL_STSHUTR || |
| 1265 | (s->req->l >= s->req->rlim - s->req->data)) { |
| 1266 | if (s->req->l == 0) { |
| 1267 | s->srv_state = SV_STCLOSE; |
| 1268 | fsm_resync |= 1; |
| 1269 | continue; |
| 1270 | } |
| 1271 | /* OK we have some remaining data to process */ |
| 1272 | /* Just as an exercice, we copy the req into the resp, |
| 1273 | * and flush the req. |
| 1274 | */ |
| 1275 | memcpy(s->rep->data, s->req->data, sizeof(s->rep->data)); |
| 1276 | s->rep->l = s->req->l; |
| 1277 | s->rep->rlim = s->rep->data + BUFSIZE; |
| 1278 | s->rep->w = s->rep->data; |
| 1279 | s->rep->lr = s->rep->r = s->rep->data + s->rep->l; |
| 1280 | |
| 1281 | s->req->l = 0; |
| 1282 | s->srv_state = SV_STCLOSE; |
| 1283 | |
| 1284 | fsm_resync |= 1; |
| 1285 | continue; |
| 1286 | } |
| 1287 | } |
| 1288 | } while (fsm_resync); |
| 1289 | |
| 1290 | if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 1291 | |
| 1292 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 1293 | session_process_counters(s); |
| 1294 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1295 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 1296 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 1297 | |
| 1298 | t->expire = s->req->rex; |
| 1299 | tv_min(&t->expire, &s->req->rex, &s->req->wex); |
| 1300 | tv_bound(&t->expire, &s->req->cex); |
| 1301 | tv_bound(&t->expire, &s->rep->rex); |
| 1302 | tv_bound(&t->expire, &s->rep->wex); |
| 1303 | |
| 1304 | /* restore t to its place in the task list */ |
| 1305 | task_queue(t); |
| 1306 | |
| 1307 | *next = t->expire; |
| 1308 | return; /* nothing more to do */ |
| 1309 | } |
| 1310 | |
| 1311 | if (s->fe) |
| 1312 | s->fe->feconn--; |
| 1313 | if (s->be && (s->flags & SN_BE_ASSIGNED)) |
| 1314 | s->be->beconn--; |
| 1315 | actconn--; |
| 1316 | |
| 1317 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1318 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1319 | int len; |
| 1320 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
| 1321 | s->uniq_id, s->be->id, |
| 1322 | (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
| 1323 | write(1, trash, len); |
| 1324 | } |
| 1325 | |
| 1326 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 1327 | session_process_counters(s); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1328 | |
| 1329 | /* let's do a final log if we need it */ |
| 1330 | if (s->logs.logwait && |
| 1331 | !(s->flags & SN_MONITOR) && |
| 1332 | (s->req->total || !(s->fe && s->fe->options & PR_O_NULLNOLOG))) { |
| 1333 | //uxst_sess_log(s); |
| 1334 | } |
| 1335 | |
| 1336 | /* the task MUST not be in the run queue anymore */ |
| 1337 | task_delete(t); |
| 1338 | session_free(s); |
| 1339 | task_free(t); |
| 1340 | tv_eternity(next); |
| 1341 | } |
| 1342 | #endif /* not converted */ |
| 1343 | |
| 1344 | |
| 1345 | /* Processes data exchanges on the statistics socket. The client processing |
| 1346 | * is called and the task is put back in the wait queue or it is cleared. |
| 1347 | * In order to ease the transition, we simply simulate the server status |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 1348 | * for now. It only knows states SV_STIDLE, SV_STCONN, SV_STDATA, and |
| 1349 | * SV_STCLOSE. Returns in <next> the task's expiration date. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1350 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1351 | void process_uxst_stats(struct task *t, int *next) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1352 | { |
| 1353 | struct session *s = t->context; |
| 1354 | struct listener *listener; |
| 1355 | int fsm_resync = 0; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 1356 | int last_rep_l; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1357 | |
| 1358 | do { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 1359 | char *args[MAX_UXST_ARGS + 1]; |
| 1360 | char *line, *p; |
| 1361 | int arg; |
| 1362 | |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 1363 | fsm_resync = process_uxst_cli(s); |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 1364 | |
| 1365 | if (s->cli_state == CL_STCLOSE || s->cli_state == CL_STSHUTW) { |
| 1366 | s->srv_state = SV_STCLOSE; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 1367 | break; |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 1368 | } |
| 1369 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 1370 | switch (s->srv_state) { |
| 1371 | case SV_STIDLE: |
| 1372 | /* stats output not initialized yet */ |
| 1373 | memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats)); |
| 1374 | s->data_source = DATA_SRC_STATS; |
| 1375 | s->srv_state = SV_STCONN; |
| 1376 | fsm_resync |= 1; |
| 1377 | break; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 1378 | |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 1379 | case SV_STCONN: /* should be changed to SV_STHEADERS or something more obvious */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 1380 | /* stats initialized, but waiting for the command */ |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 1381 | line = s->req->data; |
| 1382 | p = memchr(line, '\n', s->req->l); |
| 1383 | |
| 1384 | if (!p) |
| 1385 | continue; |
| 1386 | |
| 1387 | *p = '\0'; |
| 1388 | |
| 1389 | while (isspace((unsigned char)*line)) |
| 1390 | line++; |
| 1391 | |
| 1392 | arg = 0; |
| 1393 | args[arg] = line; |
| 1394 | |
| 1395 | while (*line && arg < MAX_UXST_ARGS) { |
| 1396 | if (isspace((unsigned char)*line)) { |
| 1397 | *line++ = '\0'; |
| 1398 | |
| 1399 | while (isspace((unsigned char)*line)) |
| 1400 | line++; |
| 1401 | |
| 1402 | args[++arg] = line; |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 1403 | continue; |
| 1404 | } |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 1405 | |
| 1406 | line++; |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 1407 | } |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 1408 | |
| 1409 | while (++arg <= MAX_UXST_ARGS) |
| 1410 | args[arg] = line; |
| 1411 | |
| 1412 | if (!strcmp(args[0], "show")) { |
| 1413 | if (!strcmp(args[1], "stat")) { |
| 1414 | if (*args[2] && *args[3] && *args[4]) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 1415 | s->data_ctx.stats.flags |= STAT_BOUND; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 1416 | s->data_ctx.stats.iid = atoi(args[2]); |
| 1417 | s->data_ctx.stats.type = atoi(args[3]); |
| 1418 | s->data_ctx.stats.sid = atoi(args[4]); |
| 1419 | } |
| 1420 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 1421 | s->data_ctx.stats.flags |= STAT_SHOW_STAT; |
| 1422 | s->data_ctx.stats.flags |= STAT_FMT_CSV; |
| 1423 | s->srv_state = SV_STDATA; |
| 1424 | fsm_resync |= 1; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1425 | continue; |
| 1426 | } |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 1427 | |
| 1428 | if (!strcmp(args[1], "info")) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 1429 | s->data_ctx.stats.flags |= STAT_SHOW_INFO; |
| 1430 | s->data_ctx.stats.flags |= STAT_FMT_CSV; |
| 1431 | s->srv_state = SV_STDATA; |
| 1432 | fsm_resync |= 1; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 1433 | continue; |
| 1434 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1435 | } |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 1436 | |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 1437 | s->srv_state = SV_STCLOSE; |
| 1438 | fsm_resync |= 1; |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 1439 | continue; |
| 1440 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 1441 | case SV_STDATA: |
| 1442 | /* OK we have to process the request. Since it is possible |
| 1443 | * that we get there with the client output paused, we |
| 1444 | * will simply check that we have really sent some data |
| 1445 | * and wake the client up if needed. |
| 1446 | */ |
| 1447 | last_rep_l = s->rep->l; |
| 1448 | if (stats_dump_raw(s, NULL) != 0) { |
| 1449 | s->srv_state = SV_STCLOSE; |
| 1450 | fsm_resync |= 1; |
| 1451 | } |
| 1452 | if (s->rep->l != last_rep_l) |
| 1453 | fsm_resync |= 1; |
| 1454 | break; |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 1455 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1456 | } while (fsm_resync); |
| 1457 | |
| 1458 | if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) { |
| 1459 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 1460 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 1461 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1462 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 1463 | tick_first(s->rep->rex, s->rep->wex)); |
| 1464 | t->expire = tick_first(t->expire, s->req->cex); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1465 | |
| 1466 | /* restore t to its place in the task list */ |
| 1467 | task_queue(t); |
| 1468 | |
| 1469 | *next = t->expire; |
| 1470 | return; /* nothing more to do */ |
| 1471 | } |
| 1472 | |
| 1473 | actconn--; |
| 1474 | listener = fdtab[s->cli_fd].listener; |
| 1475 | if (listener) { |
| 1476 | listener->nbconn--; |
| 1477 | if (listener->state == LI_FULL && |
| 1478 | listener->nbconn < listener->maxconn) { |
| 1479 | /* we should reactivate the listener */ |
| 1480 | EV_FD_SET(listener->fd, DIR_RD); |
| 1481 | listener->state = LI_READY; |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | /* the task MUST not be in the run queue anymore */ |
| 1486 | task_delete(t); |
| 1487 | session_free(s); |
| 1488 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1489 | *next = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1490 | } |
| 1491 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1492 | __attribute__((constructor)) |
| 1493 | static void __uxst_protocol_init(void) |
| 1494 | { |
| 1495 | protocol_register(&proto_unix); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | |
| 1499 | /* |
| 1500 | * Local variables: |
| 1501 | * c-indent-level: 8 |
| 1502 | * c-basic-offset: 8 |
| 1503 | * End: |
| 1504 | */ |