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; |
| 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) { |
Willy Tarreau | eabf313 | 2008-08-29 23:36:51 +0200 | [diff] [blame] | 361 | struct listener *l = fdtab[fd].owner; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 404 | goto out_close; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | if ((s = pool_alloc2(pool2_session)) == NULL) { |
| 408 | Alert("out of memory in uxst_event_accept().\n"); |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 409 | goto out_close; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 412 | LIST_ADDQ(&sessions, &s->list); |
| 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 | 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"); |
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 | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 435 | task_init(t); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 436 | t->process = l->handler; |
| 437 | t->context = s; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 438 | t->nice = -64; /* we want to boost priority for local stats */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 439 | |
| 440 | s->task = t; |
| 441 | s->fe = NULL; |
| 442 | s->be = NULL; |
| 443 | |
| 444 | s->cli_state = CL_STDATA; |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 445 | s->ana_state = 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 446 | s->req = s->rep = NULL; /* will be allocated later */ |
| 447 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 448 | s->si[0].state = s->si[0].prev_state = SI_ST_EST; |
| 449 | s->si[0].err_type = SI_ET_NONE; |
| 450 | s->si[0].err_loc = NULL; |
| 451 | s->si[0].owner = t; |
| 452 | s->si[0].shutr = stream_sock_shutr; |
| 453 | s->si[0].shutw = stream_sock_shutw; |
| 454 | s->si[0].fd = cfd; |
| 455 | s->si[0].flags = SI_FL_NONE; |
| 456 | s->si[0].exp = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 457 | s->cli_fd = cfd; |
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; |
| 465 | s->si[1].exp = TICK_ETERNITY; |
| 466 | s->si[1].fd = -1; /* just to help with debugging */ |
| 467 | s->si[1].flags = SI_FL_NONE; |
| 468 | |
| 469 | s->srv = s->prev_srv = s->srv_conn = NULL; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 470 | s->pend_pos = NULL; |
| 471 | |
| 472 | memset(&s->logs, 0, sizeof(s->logs)); |
| 473 | memset(&s->txn, 0, sizeof(s->txn)); |
| 474 | |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 475 | s->data_state = DATA_ST_INIT; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 476 | s->data_source = DATA_SRC_NONE; |
| 477 | s->uniq_id = totalconn; |
| 478 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 479 | if ((s->req = pool_alloc2(pool2_buffer)) == NULL) |
| 480 | goto out_free_task; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 481 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 482 | buffer_init(s->req); |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 483 | s->req->prod = &s->si[0]; |
| 484 | s->req->cons = &s->si[1]; |
| 485 | s->si[0].ib = s->si[1].ob = s->req; |
| 486 | s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */ |
| 487 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 488 | s->req->analysers = l->analysers; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 489 | |
| 490 | s->req->wto = TICK_ETERNITY; |
| 491 | s->req->cto = TICK_ETERNITY; |
| 492 | s->req->rto = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 493 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 494 | if ((s->rep = pool_alloc2(pool2_buffer)) == NULL) |
| 495 | goto out_free_req; |
| 496 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 497 | buffer_init(s->rep); |
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 | s->rep->prod = &s->si[1]; |
| 500 | s->rep->cons = &s->si[0]; |
| 501 | s->si[0].ob = s->si[1].ib = s->rep; |
| 502 | |
| 503 | s->rep->rto = TICK_ETERNITY; |
| 504 | s->rep->cto = TICK_ETERNITY; |
| 505 | s->rep->wto = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 506 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 507 | s->req->rex = TICK_ETERNITY; |
| 508 | s->req->wex = TICK_ETERNITY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 509 | s->req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 510 | s->rep->rex = TICK_ETERNITY; |
| 511 | s->rep->wex = TICK_ETERNITY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 512 | s->rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 513 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 514 | t->expire = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 515 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 516 | if (l->timeout) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 517 | s->req->rto = *l->timeout; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 518 | s->rep->wto = *l->timeout; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 519 | } |
| 520 | |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 521 | fd_insert(cfd); |
| 522 | fdtab[cfd].owner = &s->si[0]; |
| 523 | fdtab[cfd].listener = l; |
| 524 | fdtab[cfd].state = FD_STREADY; |
| 525 | fdtab[cfd].cb[DIR_RD].f = l->proto->read; |
| 526 | fdtab[cfd].cb[DIR_RD].b = s->req; |
| 527 | fdtab[cfd].cb[DIR_WR].f = l->proto->write; |
| 528 | fdtab[cfd].cb[DIR_WR].b = s->rep; |
| 529 | fdtab[cfd].peeraddr = (struct sockaddr *)&s->cli_addr; |
| 530 | fdtab[cfd].peerlen = sizeof(s->cli_addr); |
| 531 | |
| 532 | EV_FD_SET(cfd, DIR_RD); |
| 533 | |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 534 | task_wakeup(t, TASK_WOKEN_INIT); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 535 | |
| 536 | l->nbconn++; /* warning! right now, it's up to the handler to decrease this */ |
| 537 | if (l->nbconn >= l->maxconn) { |
| 538 | EV_FD_CLR(l->fd, DIR_RD); |
| 539 | l->state = LI_FULL; |
| 540 | } |
| 541 | actconn++; |
| 542 | totalconn++; |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 543 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 544 | return 0; |
Willy Tarreau | a11e976 | 2008-12-01 01:44:25 +0100 | [diff] [blame] | 545 | |
| 546 | out_free_req: |
| 547 | pool_free2(pool2_buffer, s->req); |
| 548 | out_free_task: |
| 549 | pool_free2(pool2_task, t); |
| 550 | out_free_session: |
| 551 | LIST_DEL(&s->list); |
| 552 | pool_free2(pool2_session, s); |
| 553 | out_close: |
| 554 | close(cfd); |
| 555 | return 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 556 | } |
| 557 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 558 | /* Parses the request line in <cmd> and possibly starts dumping stats on |
| 559 | * s->rep with the hijack bit set. Returns 1 if OK, 0 in case of any error. |
| 560 | * The line is modified after parsing. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 561 | */ |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 562 | int unix_sock_parse_request(struct session *s, char *line) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 563 | { |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 564 | char *args[MAX_UXST_ARGS + 1]; |
| 565 | int arg; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 566 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 567 | while (isspace((unsigned char)*line)) |
| 568 | line++; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 569 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 570 | arg = 0; |
| 571 | args[arg] = line; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 572 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 573 | while (*line && arg < MAX_UXST_ARGS) { |
| 574 | if (isspace((unsigned char)*line)) { |
| 575 | *line++ = '\0'; |
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 (isspace((unsigned char)*line)) |
| 578 | line++; |
| 579 | |
| 580 | args[++arg] = line; |
| 581 | continue; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 582 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 583 | |
| 584 | line++; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 585 | } |
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 | while (++arg <= MAX_UXST_ARGS) |
| 588 | args[arg] = line; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 589 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 590 | if (strcmp(args[0], "show") == 0) { |
| 591 | if (strcmp(args[1], "stat") == 0) { |
| 592 | if (*args[2] && *args[3] && *args[4]) { |
| 593 | s->data_ctx.stats.flags |= STAT_BOUND; |
| 594 | s->data_ctx.stats.iid = atoi(args[2]); |
| 595 | s->data_ctx.stats.type = atoi(args[3]); |
| 596 | s->data_ctx.stats.sid = atoi(args[4]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 597 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 598 | |
| 599 | s->data_ctx.stats.flags |= STAT_SHOW_STAT; |
| 600 | s->data_ctx.stats.flags |= STAT_FMT_CSV; |
| 601 | s->ana_state = STATS_ST_REP; |
| 602 | buffer_start_hijack(s->rep); |
| 603 | stats_dump_raw_to_buffer(s, s->rep); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 604 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 605 | else if (strcmp(args[1], "info") == 0) { |
| 606 | s->data_ctx.stats.flags |= STAT_SHOW_INFO; |
| 607 | s->data_ctx.stats.flags |= STAT_FMT_CSV; |
| 608 | s->ana_state = STATS_ST_REP; |
| 609 | buffer_start_hijack(s->rep); |
| 610 | stats_dump_raw_to_buffer(s, s->rep); |
| 611 | } |
| 612 | else { /* neither "stat" nor "info" */ |
| 613 | return 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 614 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 615 | } |
| 616 | else { /* not "show" */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 617 | return 0; |
| 618 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 619 | return 1; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 622 | /* Processes the stats interpreter on the statistics socket. |
| 623 | * In order to ease the transition, we simply simulate the server status |
| 624 | * for now. It only knows states STATS_ST_INIT, STATS_ST_REQ, STATS_ST_REP, and |
| 625 | * STATS_ST_CLOSE. It removes the AN_REQ_UNIX_STATS bit from req->analysers |
| 626 | * once done. It always returns 0. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 627 | */ |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 628 | int uxst_req_analyser_stats(struct session *s, struct buffer *req) |
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 | char *line, *p; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 631 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 632 | switch (s->ana_state) { |
| 633 | case STATS_ST_INIT: |
| 634 | /* Stats output not initialized yet */ |
| 635 | memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats)); |
| 636 | s->data_source = DATA_SRC_STATS; |
| 637 | s->ana_state = STATS_ST_REQ; |
| 638 | /* fall through */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 639 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 640 | case STATS_ST_REQ: |
| 641 | /* Now, stats are initialized, hijack is not set, and |
| 642 | * we are waiting for a complete request line. |
| 643 | */ |
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 | line = s->req->data; |
| 646 | p = memchr(line, '\n', s->req->l); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 647 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 648 | if (p) { |
| 649 | *p = '\0'; |
| 650 | if (!unix_sock_parse_request(s, line)) { |
| 651 | /* invalid request */ |
| 652 | buffer_shutw_now(s->req); |
| 653 | s->ana_state = 0; |
| 654 | req->analysers = 0; |
| 655 | return 0; |
| 656 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 657 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 658 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 659 | /* processing a valid or incomplete request */ |
| 660 | if ((req->flags & BF_FULL) || /* invalid request */ |
| 661 | (req->flags & BF_READ_ERROR) || /* input error */ |
| 662 | (req->flags & BF_READ_TIMEOUT) || /* read timeout */ |
| 663 | tick_is_expired(req->analyse_exp, now_ms) || /* request timeout */ |
| 664 | (req->flags & BF_SHUTR)) { /* input closed */ |
| 665 | buffer_shutw_now(s->req); |
| 666 | s->ana_state = 0; |
| 667 | req->analysers = 0; |
| 668 | return 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 669 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 670 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 671 | /* don't forward nor abort */ |
| 672 | buffer_write_dis(req); |
| 673 | return 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 674 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 675 | case STATS_ST_REP: |
| 676 | /* do nothing while response is being processed */ |
| 677 | buffer_write_dis(s->req); |
| 678 | return 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 679 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 680 | case STATS_ST_CLOSE: |
| 681 | /* end of dump */ |
| 682 | s->req->analysers &= ~AN_REQ_UNIX_STATS; |
| 683 | s->ana_state = 0; |
| 684 | break; |
| 685 | } |
| 686 | return 0; |
| 687 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 688 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 689 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 690 | /* This function is the unix-stream equivalent of the global process_session(). |
| 691 | * It is currently limited to unix-stream processing on control sockets such as |
| 692 | * stats, and has no server-side. The two functions should be merged into one |
| 693 | * once client and server sides are better delimited. Note that the server-side |
| 694 | * still exists but remains in SI_ST_INI state forever, so that any call is a |
| 695 | * NOP. |
| 696 | */ |
| 697 | void uxst_process_session(struct task *t, int *next) |
| 698 | { |
| 699 | struct session *s = t->context; |
| 700 | struct listener *listener; |
| 701 | int resync; |
| 702 | unsigned int rqf_last, rpf_last; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 703 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 704 | /* 1a: Check for low level timeouts if needed. We just set a flag on |
| 705 | * stream interfaces when their timeouts have expired. |
| 706 | */ |
| 707 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
| 708 | stream_int_check_timeouts(&s->si[0]); |
| 709 | buffer_check_timeouts(s->req); |
| 710 | buffer_check_timeouts(s->rep); |
| 711 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 712 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 713 | /* copy req/rep flags so that we can detect shutdowns */ |
| 714 | rqf_last = s->req->flags; |
| 715 | rpf_last = s->rep->flags; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 716 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 717 | /* 1b: check for low-level errors reported at the stream interface. */ |
| 718 | if (unlikely(s->si[0].flags & SI_FL_ERR)) { |
| 719 | if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) { |
| 720 | s->si[0].shutr(&s->si[0]); |
| 721 | s->si[0].shutw(&s->si[0]); |
| 722 | stream_int_report_error(&s->si[0]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 723 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 724 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 725 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 726 | /* check buffer timeouts, and close the corresponding stream interfaces |
| 727 | * for future reads or writes. Note: this will also concern upper layers |
| 728 | * but we do not touch any other flag. We must be careful and correctly |
| 729 | * detect state changes when calling them. |
| 730 | */ |
| 731 | if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 732 | if (s->req->flags & BF_READ_TIMEOUT) |
| 733 | s->req->prod->shutr(s->req->prod); |
| 734 | if (s->req->flags & BF_WRITE_TIMEOUT) |
| 735 | s->req->cons->shutw(s->req->cons); |
| 736 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 737 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 738 | if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 739 | if (s->rep->flags & BF_READ_TIMEOUT) |
| 740 | s->rep->prod->shutr(s->rep->prod); |
| 741 | if (s->rep->flags & BF_WRITE_TIMEOUT) |
| 742 | s->rep->cons->shutw(s->rep->cons); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 743 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 744 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 745 | /* Check for connection closure */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 746 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 747 | resync_stream_interface: |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 748 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 749 | /* nothing special to be done on client side */ |
| 750 | if (unlikely(s->req->prod->state == SI_ST_DIS)) |
| 751 | s->req->prod->state = SI_ST_CLO; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 752 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 753 | /* |
| 754 | * Note: of the transient states (REQ, CER, DIS), only REQ may remain |
| 755 | * at this point. |
| 756 | */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 757 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 758 | /**** Process layer 7 below ****/ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 759 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 760 | resync = 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 761 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 762 | /* Analyse request */ |
| 763 | if ((s->req->flags & BF_MASK_ANALYSER) || |
| 764 | (s->req->flags ^ rqf_last) & BF_MASK_STATIC) { |
| 765 | unsigned int flags = s->req->flags; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 766 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 767 | if (s->req->prod->state >= SI_ST_EST) { |
| 768 | /* it's up to the analysers to reset write_ena */ |
| 769 | buffer_write_ena(s->req); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 770 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 771 | /* We will call all analysers for which a bit is set in |
| 772 | * s->req->analysers, following the bit order from LSB |
| 773 | * to MSB. The analysers must remove themselves from |
| 774 | * the list when not needed. This while() loop is in |
| 775 | * fact a cleaner if(). |
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 | while (s->req->analysers) { |
| 778 | if (s->req->analysers & AN_REQ_UNIX_STATS) |
| 779 | if (!uxst_req_analyser_stats(s, s->req)) |
| 780 | break; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 781 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 782 | /* Just make sure that nobody set a wrong flag causing an endless loop */ |
| 783 | s->req->analysers &= AN_REQ_UNIX_STATS; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 784 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 785 | /* we don't want to loop anyway */ |
| 786 | break; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 787 | } |
| 788 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 789 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 790 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 791 | if (s->req->flags != flags) |
| 792 | resync = 1; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 793 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 794 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 795 | /* reflect what the L7 analysers have seen last */ |
| 796 | rqf_last = s->req->flags; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 797 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 798 | /* |
| 799 | * Now forward all shutdown requests between both sides of the buffer |
| 800 | */ |
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 | /* first, let's check if the request buffer needs to shutdown(write) */ |
| 803 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == |
| 804 | (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) |
| 805 | buffer_shutw_now(s->req); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 806 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 807 | /* shutdown(write) pending */ |
| 808 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW)) |
| 809 | s->req->cons->shutw(s->req->cons); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 810 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 811 | /* shutdown(write) done on server side, we must stop the client too */ |
| 812 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW && |
| 813 | !s->req->analysers)) |
| 814 | buffer_shutr_now(s->req); |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 815 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 816 | /* shutdown(read) pending */ |
| 817 | if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 818 | s->req->prod->shutr(s->req->prod); |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 819 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 820 | /* |
| 821 | * Here we want to check if we need to resync or not. |
| 822 | */ |
| 823 | if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) |
| 824 | resync = 1; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 825 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 826 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 827 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 828 | /* according to benchmarks, it makes sense to resync now */ |
| 829 | if (resync) |
| 830 | goto resync_stream_interface; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 831 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 832 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 833 | /* Analyse response */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 834 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 835 | buffer_write_ena(s->rep); |
| 836 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 837 | /* In inject mode, we wake up everytime something has |
| 838 | * happened on the write side of the buffer. |
| 839 | */ |
| 840 | unsigned int flags = s->rep->flags; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 841 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 842 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 843 | !(s->rep->flags & BF_FULL)) { |
| 844 | /* it is the only hijacker right now */ |
| 845 | stats_dump_raw_to_buffer(s, s->rep); |
| 846 | } |
| 847 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 848 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 849 | if (s->rep->flags != flags) |
| 850 | resync = 1; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 851 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 852 | else if ((s->rep->flags & BF_MASK_ANALYSER) || |
| 853 | (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) { |
| 854 | unsigned int flags = s->rep->flags; |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 855 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 856 | if (s->rep->prod->state >= SI_ST_EST) { |
| 857 | /* it's up to the analysers to reset write_ena */ |
| 858 | buffer_write_ena(s->rep); |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 859 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 860 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 861 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 862 | if (s->rep->flags != flags) |
| 863 | resync = 1; |
| 864 | } |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 865 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 866 | /* reflect what the L7 analysers have seen last */ |
| 867 | rpf_last = s->rep->flags; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 868 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 869 | /* |
| 870 | * Now forward all shutdown requests between both sides of the buffer |
| 871 | */ |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 872 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 873 | /* |
| 874 | * FIXME: this is probably where we should produce error responses. |
| 875 | */ |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 876 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 877 | /* first, let's check if the request buffer needs to shutdown(write) */ |
| 878 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == |
| 879 | (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) |
| 880 | buffer_shutw_now(s->rep); |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 881 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 882 | /* shutdown(write) pending */ |
| 883 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW)) |
| 884 | s->rep->cons->shutw(s->rep->cons); |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 885 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 886 | /* shutdown(write) done on the client side, we must stop the server too */ |
| 887 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW)) |
| 888 | buffer_shutr_now(s->rep); |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 889 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 890 | /* shutdown(read) pending */ |
| 891 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 892 | s->rep->prod->shutr(s->rep->prod); |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 893 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 894 | /* |
| 895 | * Here we want to check if we need to resync or not. |
| 896 | */ |
| 897 | if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC) |
| 898 | resync = 1; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 899 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 900 | 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] | 901 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 902 | if (resync) |
| 903 | goto resync_stream_interface; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 904 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 905 | if (likely(s->rep->cons->state != SI_ST_CLO)) { |
| 906 | if (s->rep->cons->state == SI_ST_EST) |
| 907 | stream_sock_data_finish(s->rep->cons); |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 908 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 909 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 910 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 911 | s->si[0].prev_state = s->si[0].state; |
| 912 | s->si[0].flags = SI_FL_NONE; |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 913 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 914 | /* Trick: if a request is being waiting for the server to respond, |
| 915 | * and if we know the server can timeout, we don't want the timeout |
| 916 | * to expire on the client side first, but we're still interested |
| 917 | * in passing data from the client to the server (eg: POST). Thus, |
| 918 | * we can cancel the client's request timeout if the server's |
| 919 | * request timeout is set and the server has not yet sent a response. |
| 920 | */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 921 | |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 922 | if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 && |
| 923 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) |
| 924 | s->req->rex = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 925 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 926 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 927 | tick_first(s->rep->rex, s->rep->wex)); |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame^] | 928 | if (s->req->analysers) |
| 929 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
| 930 | |
| 931 | if (s->si[0].exp) |
| 932 | t->expire = tick_first(t->expire, s->si[0].exp); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 933 | |
| 934 | /* restore t to its place in the task list */ |
| 935 | task_queue(t); |
| 936 | |
| 937 | *next = t->expire; |
| 938 | return; /* nothing more to do */ |
| 939 | } |
| 940 | |
| 941 | actconn--; |
| 942 | listener = fdtab[s->cli_fd].listener; |
| 943 | if (listener) { |
| 944 | listener->nbconn--; |
| 945 | if (listener->state == LI_FULL && |
| 946 | listener->nbconn < listener->maxconn) { |
| 947 | /* we should reactivate the listener */ |
| 948 | EV_FD_SET(listener->fd, DIR_RD); |
| 949 | listener->state = LI_READY; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /* the task MUST not be in the run queue anymore */ |
| 954 | task_delete(t); |
| 955 | session_free(s); |
| 956 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 957 | *next = TICK_ETERNITY; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 958 | } |
| 959 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 960 | __attribute__((constructor)) |
| 961 | static void __uxst_protocol_init(void) |
| 962 | { |
| 963 | protocol_register(&proto_unix); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | |
| 967 | /* |
| 968 | * Local variables: |
| 969 | * c-indent-level: 8 |
| 970 | * c-basic-offset: 8 |
| 971 | * End: |
| 972 | */ |