willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2 | * HA-Proxy : High Availability-enabled HTTP/TCP proxy |
willy tarreau | 726618c | 2006-01-29 22:42:06 +0100 | [diff] [blame] | 3 | * 2000-2006 - Willy Tarreau - willy AT meta-x DOT org. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version |
| 8 | * 2 of the License, or (at your option) any later version. |
| 9 | * |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 10 | * Please refer to RFC2068 or RFC2616 for informations about HTTP protocol, and |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 11 | * RFC2965 for informations about cookies usage. More generally, the IETF HTTP |
| 12 | * Working Group's web site should be consulted for protocol related changes : |
| 13 | * |
| 14 | * http://ftp.ics.uci.edu/pub/ietf/http/ |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 15 | * |
| 16 | * Pending bugs (may be not fixed because never reproduced) : |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 17 | * - solaris only : sometimes, an HTTP proxy with only a dispatch address causes |
| 18 | * the proxy to terminate (no core) if the client breaks the connection during |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 19 | * the response. Seen on 1.1.8pre4, but never reproduced. May not be related to |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 20 | * the snprintf() bug since requests were simple (GET / HTTP/1.0), but may be |
| 21 | * related to missing setsid() (fixed in 1.1.15) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 22 | * - a proxy with an invalid config will prevent the startup even if disabled. |
| 23 | * |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 24 | * ChangeLog has moved to the CHANGELOG file. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 25 | * |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 26 | * TODO: |
| 27 | * - handle properly intermediate incomplete server headers. Done ? |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 28 | * - handle hot-reconfiguration |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 29 | * - fix client/server state transition when server is in connect or headers state |
| 30 | * and client suddenly disconnects. The server *should* switch to SHUT_WR, but |
| 31 | * still handle HTTP headers. |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 32 | * - remove MAX_NEWHDR |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 33 | * - cut this huge file into several ones |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 34 | * |
| 35 | */ |
| 36 | |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <unistd.h> |
| 40 | #include <string.h> |
| 41 | #include <ctype.h> |
| 42 | #include <sys/time.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/socket.h> |
| 45 | #include <netinet/tcp.h> |
| 46 | #include <netinet/in.h> |
| 47 | #include <arpa/inet.h> |
| 48 | #include <netdb.h> |
| 49 | #include <fcntl.h> |
| 50 | #include <errno.h> |
| 51 | #include <signal.h> |
| 52 | #include <stdarg.h> |
| 53 | #include <sys/resource.h> |
| 54 | #include <time.h> |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 55 | #include <syslog.h> |
willy tarreau | 77bc854 | 2005-12-18 01:31:43 +0100 | [diff] [blame] | 56 | |
| 57 | #ifdef USE_PCRE |
| 58 | #include <pcre.h> |
| 59 | #include <pcreposix.h> |
| 60 | #else |
| 61 | #include <regex.h> |
| 62 | #endif |
| 63 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 64 | #if defined(TPROXY) && defined(NETFILTER) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 65 | #include <linux/netfilter_ipv4.h> |
| 66 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 67 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 68 | #if defined(__dietlibc__) |
| 69 | #include <strings.h> |
| 70 | #endif |
| 71 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 72 | #if defined(ENABLE_POLL) |
| 73 | #include <sys/poll.h> |
| 74 | #endif |
| 75 | |
| 76 | #if defined(ENABLE_EPOLL) |
| 77 | #if !defined(USE_MY_EPOLL) |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 78 | #include <sys/epoll.h> |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 79 | #else |
| 80 | #include "include/epoll.h" |
| 81 | #endif |
| 82 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 83 | |
willy tarreau | 779dc89 | 2006-03-19 19:32:29 +0100 | [diff] [blame] | 84 | #ifdef DEBUG_FULL |
| 85 | #include <assert.h> |
| 86 | #endif |
| 87 | |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 88 | #include <include/base64.h> |
| 89 | #include <include/uri_auth.h> |
willy tarreau | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 90 | #include "include/appsession.h" |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 91 | #include "include/mini-clist.h" |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 92 | |
willy tarreau | bfad574 | 2006-03-23 14:19:11 +0100 | [diff] [blame] | 93 | #ifndef HAPROXY_VERSION |
Willy TARREAU | 4404b7e | 2006-05-14 10:00:09 +0200 | [diff] [blame] | 94 | #define HAPROXY_VERSION "1.2.13.1" |
willy tarreau | bfad574 | 2006-03-23 14:19:11 +0100 | [diff] [blame] | 95 | #endif |
| 96 | |
| 97 | #ifndef HAPROXY_DATE |
Willy TARREAU | 4404b7e | 2006-05-14 10:00:09 +0200 | [diff] [blame] | 98 | #define HAPROXY_DATE "2006/05/14" |
willy tarreau | bfad574 | 2006-03-23 14:19:11 +0100 | [diff] [blame] | 99 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 100 | |
| 101 | /* this is for libc5 for example */ |
| 102 | #ifndef TCP_NODELAY |
| 103 | #define TCP_NODELAY 1 |
| 104 | #endif |
| 105 | |
| 106 | #ifndef SHUT_RD |
| 107 | #define SHUT_RD 0 |
| 108 | #endif |
| 109 | |
| 110 | #ifndef SHUT_WR |
| 111 | #define SHUT_WR 1 |
| 112 | #endif |
| 113 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 114 | /* |
| 115 | * BUFSIZE defines the size of a read and write buffer. It is the maximum |
| 116 | * amount of bytes which can be stored by the proxy for each session. However, |
| 117 | * when reading HTTP headers, the proxy needs some spare space to add or rewrite |
| 118 | * headers if needed. The size of this spare is defined with MAXREWRITE. So it |
| 119 | * is not possible to process headers longer than BUFSIZE-MAXREWRITE bytes. By |
| 120 | * default, BUFSIZE=16384 bytes and MAXREWRITE=BUFSIZE/2, so the maximum length |
| 121 | * of headers accepted is 8192 bytes, which is in line with Apache's limits. |
| 122 | */ |
| 123 | #ifndef BUFSIZE |
| 124 | #define BUFSIZE 16384 |
| 125 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 126 | |
| 127 | // reserved buffer space for header rewriting |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 128 | #ifndef MAXREWRITE |
| 129 | #define MAXREWRITE (BUFSIZE / 2) |
| 130 | #endif |
| 131 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 132 | #define REQURI_LEN 1024 |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 133 | #define CAPTURE_LEN 64 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 134 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 135 | // max # args on a configuration line |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 136 | #define MAX_LINE_ARGS 40 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 137 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 138 | // max # of added headers per request |
| 139 | #define MAX_NEWHDR 10 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 140 | |
| 141 | // max # of matches per regexp |
| 142 | #define MAX_MATCH 10 |
| 143 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 144 | // cookie delimitor in "prefix" mode. This character is inserted between the |
| 145 | // persistence cookie and the original value. The '~' is allowed by RFC2965, |
| 146 | // and should not be too common in server names. |
| 147 | #ifndef COOKIE_DELIM |
| 148 | #define COOKIE_DELIM '~' |
| 149 | #endif |
| 150 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 151 | #define CONN_RETRIES 3 |
| 152 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 153 | #define CHK_CONNTIME 2000 |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 154 | #define DEF_CHKINTR 2000 |
| 155 | #define DEF_FALLTIME 3 |
| 156 | #define DEF_RISETIME 2 |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 157 | #define DEF_CHECK_REQ "OPTIONS / HTTP/1.0\r\n\r\n" |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 158 | |
Willy TARREAU | 13032e7 | 2006-03-12 17:31:45 +0100 | [diff] [blame] | 159 | /* Default connections limit. |
| 160 | * |
| 161 | * A system limit can be enforced at build time in order to avoid using haproxy |
| 162 | * beyond reasonable system limits. For this, just define SYSTEM_MAXCONN to the |
| 163 | * absolute limit accepted by the system. If the configuration specifies a |
| 164 | * higher value, it will be capped to SYSTEM_MAXCONN and a warning will be |
| 165 | * emitted. The only way to override this limit will be to set it via the |
| 166 | * command-line '-n' argument. |
| 167 | */ |
| 168 | #ifndef SYSTEM_MAXCONN |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 169 | #define DEFAULT_MAXCONN 2000 |
Willy TARREAU | 13032e7 | 2006-03-12 17:31:45 +0100 | [diff] [blame] | 170 | #else |
| 171 | #define DEFAULT_MAXCONN SYSTEM_MAXCONN |
| 172 | #endif |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 173 | |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 174 | #ifdef CONFIG_PRODUCT_NAME |
| 175 | #define PRODUCT_NAME CONFIG_PRODUCT_NAME |
| 176 | #else |
| 177 | #define PRODUCT_NAME "HAProxy" |
| 178 | #endif |
| 179 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 180 | /* how many bits are needed to code the size of an int (eg: 32bits -> 5) */ |
| 181 | #define INTBITS 5 |
| 182 | |
| 183 | /* show stats this every millisecond, 0 to disable */ |
| 184 | #ifndef STATTIME |
| 185 | #define STATTIME 2000 |
| 186 | #endif |
| 187 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 188 | /* this reduces the number of calls to select() by choosing appropriate |
| 189 | * sheduler precision in milliseconds. It should be near the minimum |
| 190 | * time that is needed by select() to collect all events. All timeouts |
| 191 | * are rounded up by adding this value prior to pass it to select(). |
| 192 | */ |
| 193 | #define SCHEDULER_RESOLUTION 9 |
| 194 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 195 | #define TIME_ETERNITY -1 |
| 196 | /* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 197 | #define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old))) |
| 198 | #define SETNOW(a) (*a=now) |
| 199 | |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 200 | /****** string-specific macros and functions ******/ |
| 201 | /* if a > max, then bound <a> to <max>. The macro returns the new <a> */ |
| 202 | #define UBOUND(a, max) ({ typeof(a) b = (max); if ((a) > b) (a) = b; (a); }) |
| 203 | |
| 204 | /* if a < min, then bound <a> to <min>. The macro returns the new <a> */ |
| 205 | #define LBOUND(a, min) ({ typeof(a) b = (min); if ((a) < b) (a) = b; (a); }) |
| 206 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 207 | /* returns 1 only if only zero or one bit is set in X, which means that X is a |
| 208 | * power of 2, and 0 otherwise */ |
| 209 | #define POWEROF2(x) (((x) & ((x)-1)) == 0) |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 210 | /* |
| 211 | * copies at most <size-1> chars from <src> to <dst>. Last char is always |
| 212 | * set to 0, unless <size> is 0. The number of chars copied is returned |
| 213 | * (excluding the terminating zero). |
| 214 | * This code has been optimized for size and speed : on x86, it's 45 bytes |
| 215 | * long, uses only registers, and consumes only 4 cycles per char. |
| 216 | */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 217 | int strlcpy2(char *dst, const char *src, int size) { |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 218 | char *orig = dst; |
| 219 | if (size) { |
| 220 | while (--size && (*dst = *src)) { |
| 221 | src++; dst++; |
| 222 | } |
| 223 | *dst = 0; |
| 224 | } |
| 225 | return dst - orig; |
| 226 | } |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 227 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 228 | /* |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 229 | * This function simply returns a statically allocated string containing |
| 230 | * the ascii representation for number 'n' in decimal. |
| 231 | */ |
| 232 | char *ultoa(unsigned long n) { |
| 233 | /* enough to store 2^63=18446744073709551615 */ |
| 234 | static char itoa_str[21]; |
| 235 | char *pos; |
| 236 | |
| 237 | pos = itoa_str + sizeof(itoa_str) - 1; |
| 238 | *pos-- = '\0'; |
| 239 | |
| 240 | do { |
| 241 | *pos-- = '0' + n % 10; |
| 242 | n /= 10; |
| 243 | } while (n && pos >= itoa_str); |
| 244 | return pos + 1; |
| 245 | } |
| 246 | |
| 247 | /* |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 248 | * Returns a pointer to an area of <__len> bytes taken from the pool <pool> or |
| 249 | * dynamically allocated. In the first case, <__pool> is updated to point to |
| 250 | * the next element in the list. |
| 251 | */ |
| 252 | #define pool_alloc_from(__pool, __len) ({ \ |
| 253 | void *__p; \ |
| 254 | if ((__p = (__pool)) == NULL) \ |
| 255 | __p = malloc(((__len) >= sizeof (void *)) ? (__len) : sizeof(void *)); \ |
| 256 | else { \ |
| 257 | __pool = *(void **)(__pool); \ |
| 258 | } \ |
| 259 | __p; \ |
| 260 | }) |
| 261 | |
| 262 | /* |
| 263 | * Puts a memory area back to the corresponding pool. |
| 264 | * Items are chained directly through a pointer that |
| 265 | * is written in the beginning of the memory area, so |
| 266 | * there's no need for any carrier cell. This implies |
| 267 | * that each memory area is at least as big as one |
| 268 | * pointer. |
| 269 | */ |
| 270 | #define pool_free_to(__pool, __ptr) ({ \ |
| 271 | *(void **)(__ptr) = (void *)(__pool); \ |
| 272 | __pool = (void *)(__ptr); \ |
| 273 | }) |
| 274 | |
| 275 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 276 | #define MEM_OPTIM |
| 277 | #ifdef MEM_OPTIM |
| 278 | /* |
| 279 | * Returns a pointer to type <type> taken from the |
| 280 | * pool <pool_type> or dynamically allocated. In the |
| 281 | * first case, <pool_type> is updated to point to the |
| 282 | * next element in the list. |
| 283 | */ |
| 284 | #define pool_alloc(type) ({ \ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 285 | void *__p; \ |
| 286 | if ((__p = pool_##type) == NULL) \ |
| 287 | __p = malloc(sizeof_##type); \ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 288 | else { \ |
| 289 | pool_##type = *(void **)pool_##type; \ |
| 290 | } \ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 291 | __p; \ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 292 | }) |
| 293 | |
| 294 | /* |
| 295 | * Puts a memory area back to the corresponding pool. |
| 296 | * Items are chained directly through a pointer that |
| 297 | * is written in the beginning of the memory area, so |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 298 | * there's no need for any carrier cell. This implies |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 299 | * that each memory area is at least as big as one |
| 300 | * pointer. |
| 301 | */ |
| 302 | #define pool_free(type, ptr) ({ \ |
| 303 | *(void **)ptr = (void *)pool_##type; \ |
| 304 | pool_##type = (void *)ptr; \ |
| 305 | }) |
| 306 | |
| 307 | #else |
| 308 | #define pool_alloc(type) (calloc(1,sizeof_##type)); |
| 309 | #define pool_free(type, ptr) (free(ptr)); |
| 310 | #endif /* MEM_OPTIM */ |
| 311 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 312 | #define sizeof_task sizeof(struct task) |
| 313 | #define sizeof_session sizeof(struct session) |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 314 | #define sizeof_pendconn sizeof(struct pendconn) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 315 | #define sizeof_buffer sizeof(struct buffer) |
| 316 | #define sizeof_fdtab sizeof(struct fdtab) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 317 | #define sizeof_requri REQURI_LEN |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 318 | #define sizeof_capture CAPTURE_LEN |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 319 | #define sizeof_curappsession CAPTURE_LEN /* current_session pool */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 320 | #define sizeof_appsess sizeof(struct appsessions) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 321 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 322 | /* different possible states for the sockets */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 323 | #define FD_STCLOSE 0 |
| 324 | #define FD_STLISTEN 1 |
| 325 | #define FD_STCONN 2 |
| 326 | #define FD_STREADY 3 |
| 327 | #define FD_STERROR 4 |
| 328 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 329 | /* values for task->state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 330 | #define TASK_IDLE 0 |
| 331 | #define TASK_RUNNING 1 |
| 332 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 333 | /* values for proxy->state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 334 | #define PR_STNEW 0 |
| 335 | #define PR_STIDLE 1 |
| 336 | #define PR_STRUN 2 |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 337 | #define PR_STSTOPPED 3 |
| 338 | #define PR_STPAUSED 4 |
willy tarreau | fac1a86 | 2006-05-21 10:20:28 +0200 | [diff] [blame] | 339 | #define PR_STERROR 5 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 340 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 341 | /* values for proxy->mode */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 342 | #define PR_MODE_TCP 0 |
| 343 | #define PR_MODE_HTTP 1 |
| 344 | #define PR_MODE_HEALTH 2 |
| 345 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 346 | /* possible actions for the *poll() loops */ |
| 347 | #define POLL_LOOP_ACTION_INIT 0 |
| 348 | #define POLL_LOOP_ACTION_RUN 1 |
| 349 | #define POLL_LOOP_ACTION_CLEAN 2 |
| 350 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 351 | /* poll mechanisms available */ |
| 352 | #define POLL_USE_SELECT (1<<0) |
| 353 | #define POLL_USE_POLL (1<<1) |
| 354 | #define POLL_USE_EPOLL (1<<2) |
| 355 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 356 | /* bits for proxy->options */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 357 | #define PR_O_REDISP 0x00000001 /* allow reconnection to dispatch in case of errors */ |
| 358 | #define PR_O_TRANSP 0x00000002 /* transparent mode : use original DEST as dispatch */ |
| 359 | #define PR_O_COOK_RW 0x00000004 /* rewrite all direct cookies with the right serverid */ |
| 360 | #define PR_O_COOK_IND 0x00000008 /* keep only indirect cookies */ |
| 361 | #define PR_O_COOK_INS 0x00000010 /* insert cookies when not accessing a server directly */ |
| 362 | #define PR_O_COOK_PFX 0x00000020 /* rewrite all cookies by prefixing the right serverid */ |
| 363 | #define PR_O_COOK_ANY (PR_O_COOK_RW | PR_O_COOK_IND | PR_O_COOK_INS | PR_O_COOK_PFX) |
| 364 | #define PR_O_BALANCE_RR 0x00000040 /* balance in round-robin mode */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 365 | #define PR_O_KEEPALIVE 0x00000080 /* follow keep-alive sessions */ |
| 366 | #define PR_O_FWDFOR 0x00000100 /* insert x-forwarded-for with client address */ |
| 367 | #define PR_O_BIND_SRC 0x00000200 /* bind to a specific source address when connect()ing */ |
| 368 | #define PR_O_NULLNOLOG 0x00000400 /* a connect without request will not be logged */ |
| 369 | #define PR_O_COOK_NOC 0x00000800 /* add a 'Cache-control' header with the cookie */ |
| 370 | #define PR_O_COOK_POST 0x00001000 /* don't insert cookies for requests other than a POST */ |
| 371 | #define PR_O_HTTP_CHK 0x00002000 /* use HTTP 'OPTIONS' method to check server health */ |
| 372 | #define PR_O_PERSIST 0x00004000 /* server persistence stays effective even when server is down */ |
| 373 | #define PR_O_LOGASAP 0x00008000 /* log as soon as possible, without waiting for the session to complete */ |
| 374 | #define PR_O_HTTP_CLOSE 0x00010000 /* force 'connection: close' in both directions */ |
| 375 | #define PR_O_CHK_CACHE 0x00020000 /* require examination of cacheability of the 'set-cookie' field */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 376 | #define PR_O_TCP_CLI_KA 0x00040000 /* enable TCP keep-alive on client-side sessions */ |
| 377 | #define PR_O_TCP_SRV_KA 0x00080000 /* enable TCP keep-alive on server-side sessions */ |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 378 | #define PR_O_USE_ALL_BK 0x00100000 /* load-balance between backup servers */ |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 379 | #define PR_O_FORCE_CLO 0x00200000 /* enforce the connection close immediately after server response */ |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 380 | #define PR_O_BALANCE_SH 0x00400000 /* balance on source IP hash */ |
| 381 | #define PR_O_BALANCE (PR_O_BALANCE_RR | PR_O_BALANCE_SH) |
willy tarreau | 03a92de | 2006-05-21 18:26:53 +0200 | [diff] [blame^] | 382 | #define PR_O_ABRT_CLOSE 0x00800000 /* immediately abort request when client closes */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 383 | |
willy tarreau | a5e8c66 | 2006-04-29 10:43:46 +0200 | [diff] [blame] | 384 | /* various session flags, bits values 0x01 to 0x20 (shift 0) */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 385 | #define SN_DIRECT 0x00000001 /* connection made on the server matching the client cookie */ |
| 386 | #define SN_CLDENY 0x00000002 /* a client header matches a deny regex */ |
| 387 | #define SN_CLALLOW 0x00000004 /* a client header matches an allow regex */ |
| 388 | #define SN_SVDENY 0x00000008 /* a server header matches a deny regex */ |
| 389 | #define SN_SVALLOW 0x00000010 /* a server header matches an allow regex */ |
| 390 | #define SN_POST 0x00000020 /* the request was an HTTP POST */ |
| 391 | |
willy tarreau | a5e8c66 | 2006-04-29 10:43:46 +0200 | [diff] [blame] | 392 | /* session flags dedicated to cookies : bits values 0x40, 0x80 (0-3 shift 6) */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 393 | #define SN_CK_NONE 0x00000000 /* this session had no cookie */ |
| 394 | #define SN_CK_INVALID 0x00000040 /* this session had a cookie which matches no server */ |
| 395 | #define SN_CK_DOWN 0x00000080 /* this session had cookie matching a down server */ |
| 396 | #define SN_CK_VALID 0x000000C0 /* this session had cookie matching a valid server */ |
| 397 | #define SN_CK_MASK 0x000000C0 /* mask to get this session's cookie flags */ |
| 398 | #define SN_CK_SHIFT 6 /* bit shift */ |
| 399 | |
willy tarreau | a5e8c66 | 2006-04-29 10:43:46 +0200 | [diff] [blame] | 400 | /* session termination conditions, bits values 0x100 to 0x700 (0-7 shift 8) */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 401 | #define SN_ERR_NONE 0x00000000 |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 402 | #define SN_ERR_CLITO 0x00000100 /* client time-out */ |
| 403 | #define SN_ERR_CLICL 0x00000200 /* client closed (read/write error) */ |
| 404 | #define SN_ERR_SRVTO 0x00000300 /* server time-out, connect time-out */ |
| 405 | #define SN_ERR_SRVCL 0x00000400 /* server closed (connect/read/write error) */ |
| 406 | #define SN_ERR_PRXCOND 0x00000500 /* the proxy decided to close (deny...) */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 407 | #define SN_ERR_RESOURCE 0x00000600 /* the proxy encountered a lack of a local resources (fd, mem, ...) */ |
| 408 | #define SN_ERR_INTERNAL 0x00000700 /* the proxy encountered an internal error */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 409 | #define SN_ERR_MASK 0x00000700 /* mask to get only session error flags */ |
| 410 | #define SN_ERR_SHIFT 8 /* bit shift */ |
| 411 | |
willy tarreau | a5e8c66 | 2006-04-29 10:43:46 +0200 | [diff] [blame] | 412 | /* session state at termination, bits values 0x1000 to 0x7000 (0-7 shift 12) */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 413 | #define SN_FINST_R 0x00001000 /* session ended during client request */ |
| 414 | #define SN_FINST_C 0x00002000 /* session ended during server connect */ |
| 415 | #define SN_FINST_H 0x00003000 /* session ended during server headers */ |
| 416 | #define SN_FINST_D 0x00004000 /* session ended during data phase */ |
| 417 | #define SN_FINST_L 0x00005000 /* session ended while pushing last data to client */ |
willy tarreau | 078c79a | 2006-05-13 12:23:58 +0200 | [diff] [blame] | 418 | #define SN_FINST_Q 0x00006000 /* session ended while waiting in queue for a server slot */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 419 | #define SN_FINST_MASK 0x00007000 /* mask to get only final session state flags */ |
| 420 | #define SN_FINST_SHIFT 12 /* bit shift */ |
| 421 | |
willy tarreau | a5e8c66 | 2006-04-29 10:43:46 +0200 | [diff] [blame] | 422 | /* cookie information, bits values 0x10000 to 0x80000 (0-8 shift 16) */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 423 | #define SN_SCK_NONE 0x00000000 /* no set-cookie seen for the server cookie */ |
| 424 | #define SN_SCK_DELETED 0x00010000 /* existing set-cookie deleted or changed */ |
| 425 | #define SN_SCK_INSERTED 0x00020000 /* new set-cookie inserted or changed existing one */ |
| 426 | #define SN_SCK_SEEN 0x00040000 /* set-cookie seen for the server cookie */ |
| 427 | #define SN_SCK_MASK 0x00070000 /* mask to get the set-cookie field */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 428 | #define SN_SCK_ANY 0x00080000 /* at least one set-cookie seen (not to be counted) */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 429 | #define SN_SCK_SHIFT 16 /* bit shift */ |
| 430 | |
willy tarreau | a5e8c66 | 2006-04-29 10:43:46 +0200 | [diff] [blame] | 431 | /* cacheability management, bits values 0x100000 to 0x300000 (0-3 shift 20) */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 432 | #define SN_CACHEABLE 0x00100000 /* at least part of the response is cacheable */ |
| 433 | #define SN_CACHE_COOK 0x00200000 /* a cookie in the response is cacheable */ |
| 434 | #define SN_CACHE_SHIFT 20 /* bit shift */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 435 | |
willy tarreau | a5e8c66 | 2006-04-29 10:43:46 +0200 | [diff] [blame] | 436 | /* various other session flags, bits values 0x400000 and above */ |
| 437 | #define SN_MONITOR 0x00400000 /* this session comes from a monitoring system */ |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 438 | #define SN_ASSIGNED 0x00800000 /* no need to assign a server to this session */ |
| 439 | #define SN_ADDR_SET 0x01000000 /* this session's server address has been set */ |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 440 | #define SN_SELF_GEN 0x02000000 /* the proxy generates data for the client (eg: stats) */ |
willy tarreau | a5e8c66 | 2006-04-29 10:43:46 +0200 | [diff] [blame] | 441 | |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 442 | /* various data sources for the responses */ |
| 443 | #define DATA_SRC_NONE 0 |
| 444 | #define DATA_SRC_STATS 1 |
willy tarreau | a5e8c66 | 2006-04-29 10:43:46 +0200 | [diff] [blame] | 445 | |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 446 | /* data transmission states for the responses */ |
| 447 | #define DATA_ST_INIT 0 |
| 448 | #define DATA_ST_DATA 1 |
| 449 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 450 | /* different possible states for the client side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 451 | #define CL_STHEADERS 0 |
| 452 | #define CL_STDATA 1 |
| 453 | #define CL_STSHUTR 2 |
| 454 | #define CL_STSHUTW 3 |
| 455 | #define CL_STCLOSE 4 |
| 456 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 457 | /* different possible states for the server side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 458 | #define SV_STIDLE 0 |
willy tarreau | 9fea194 | 2006-05-12 19:46:40 +0200 | [diff] [blame] | 459 | #define SV_STCONN 1 |
| 460 | #define SV_STHEADERS 2 |
| 461 | #define SV_STDATA 3 |
| 462 | #define SV_STSHUTR 4 |
| 463 | #define SV_STSHUTW 5 |
| 464 | #define SV_STCLOSE 6 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 465 | |
| 466 | /* result of an I/O event */ |
| 467 | #define RES_SILENT 0 /* didn't happen */ |
| 468 | #define RES_DATA 1 /* data were sent or received */ |
| 469 | #define RES_NULL 2 /* result is 0 (read == 0), or connect without need for writing */ |
| 470 | #define RES_ERROR 3 /* result -1 or error on the socket (eg: connect()) */ |
| 471 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 472 | /* modes of operation (global.mode) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 473 | #define MODE_DEBUG 1 |
| 474 | #define MODE_STATS 2 |
| 475 | #define MODE_LOG 4 |
| 476 | #define MODE_DAEMON 8 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 477 | #define MODE_QUIET 16 |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 478 | #define MODE_CHECK 32 |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 479 | #define MODE_VERBOSE 64 |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 480 | #define MODE_STARTING 128 |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 481 | #define MODE_FOREGROUND 256 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 482 | |
| 483 | /* server flags */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 484 | #define SRV_RUNNING 1 /* the server is UP */ |
| 485 | #define SRV_BACKUP 2 /* this server is a backup server */ |
| 486 | #define SRV_MAPPORTS 4 /* this server uses mapped ports */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 487 | #define SRV_BIND_SRC 8 /* this server uses a specific source address */ |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 488 | #define SRV_CHECKED 16 /* this server needs to be checked */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 489 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 490 | /* function which act on servers need to return various errors */ |
| 491 | #define SRV_STATUS_OK 0 /* everything is OK. */ |
| 492 | #define SRV_STATUS_INTERNAL 1 /* other unrecoverable errors. */ |
| 493 | #define SRV_STATUS_NOSRV 2 /* no server is available */ |
| 494 | #define SRV_STATUS_FULL 3 /* the/all server(s) are saturated */ |
| 495 | #define SRV_STATUS_QUEUED 4 /* the/all server(s) are saturated but the connection was queued */ |
| 496 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 497 | /* what to do when a header matches a regex */ |
| 498 | #define ACT_ALLOW 0 /* allow the request */ |
| 499 | #define ACT_REPLACE 1 /* replace the matching header */ |
| 500 | #define ACT_REMOVE 2 /* remove the matching header */ |
| 501 | #define ACT_DENY 3 /* deny the request */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 502 | #define ACT_PASS 4 /* pass this header without allowing or denying the request */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 503 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 504 | /* configuration sections */ |
| 505 | #define CFG_NONE 0 |
| 506 | #define CFG_GLOBAL 1 |
| 507 | #define CFG_LISTEN 2 |
| 508 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 509 | /* fields that need to be logged. They appear as flags in session->logs.logwait */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 510 | #define LW_DATE 1 /* date */ |
| 511 | #define LW_CLIP 2 /* CLient IP */ |
| 512 | #define LW_SVIP 4 /* SerVer IP */ |
| 513 | #define LW_SVID 8 /* server ID */ |
| 514 | #define LW_REQ 16 /* http REQuest */ |
| 515 | #define LW_RESP 32 /* http RESPonse */ |
| 516 | #define LW_PXIP 64 /* proxy IP */ |
| 517 | #define LW_PXID 128 /* proxy ID */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 518 | #define LW_BYTES 256 /* bytes read from server */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 519 | #define LW_COOKIE 512 /* captured cookie */ |
| 520 | #define LW_REQHDR 1024 /* request header(s) */ |
| 521 | #define LW_RSPHDR 2048 /* response header(s) */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 522 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 523 | #define ERR_NONE 0 /* no error */ |
| 524 | #define ERR_RETRYABLE 1 /* retryable error, may be cumulated */ |
| 525 | #define ERR_FATAL 2 /* fatal error, may be cumulated */ |
| 526 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 527 | /*********************************************************************/ |
| 528 | |
| 529 | #define LIST_HEAD(a) ((void *)(&(a))) |
| 530 | |
| 531 | /*********************************************************************/ |
| 532 | |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 533 | /* describes a chunk of string */ |
| 534 | struct chunk { |
| 535 | char *str; /* beginning of the string itself. Might not be 0-terminated */ |
| 536 | int len; /* size of the string from first to last char. <0 = uninit. */ |
| 537 | }; |
| 538 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 539 | struct cap_hdr { |
| 540 | struct cap_hdr *next; |
| 541 | char *name; /* header name, case insensitive */ |
| 542 | int namelen; /* length of the header name, to speed-up lookups */ |
| 543 | int len; /* capture length, not including terminal zero */ |
| 544 | int index; /* index in the output array */ |
| 545 | void *pool; /* pool of pre-allocated memory area of (len+1) bytes */ |
| 546 | }; |
| 547 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 548 | struct hdr_exp { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 549 | struct hdr_exp *next; |
| 550 | regex_t *preg; /* expression to look for */ |
| 551 | int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */ |
| 552 | char *replace; /* expression to set instead */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 553 | }; |
| 554 | |
| 555 | struct buffer { |
| 556 | unsigned int l; /* data length */ |
| 557 | char *r, *w, *h, *lr; /* read ptr, write ptr, last header ptr, last read */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 558 | char *rlim; /* read limit, used for header rewriting */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 559 | unsigned long long total; /* total data read */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 560 | char data[BUFSIZE]; |
| 561 | }; |
| 562 | |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 563 | struct pendconn { |
| 564 | struct list list; /* chaining ... */ |
| 565 | struct session *sess; /* the session waiting for a connection */ |
| 566 | struct server *srv; /* the server we are waiting for */ |
| 567 | }; |
| 568 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 569 | struct server { |
| 570 | struct server *next; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 571 | int state; /* server state (SRV_*) */ |
| 572 | int cklen; /* the len of the cookie, to speed up checks */ |
| 573 | char *cookie; /* the id set in the cookie */ |
| 574 | char *id; /* just for identification */ |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 575 | struct list pendconns; /* pending connections */ |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 576 | int nbpend, nbpend_max; /* number of pending connections */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 577 | struct task *queue_mgt; /* the task associated to the queue processing */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 578 | struct sockaddr_in addr; /* the address to connect to */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 579 | struct sockaddr_in source_addr; /* the address to which we want to bind for connect() */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 580 | short check_port; /* the port to use for the health checks */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 581 | int health; /* 0->rise-1 = bad; rise->rise+fall-1 = good */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 582 | int rise, fall; /* time in iterations */ |
| 583 | int inter; /* time in milliseconds */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 584 | int result; /* 0 = connect OK, -1 = connect KO */ |
| 585 | int curfd; /* file desc used for current test, or -1 if not in test */ |
willy tarreau | e3f023f | 2006-04-08 21:52:24 +0200 | [diff] [blame] | 586 | unsigned char uweight, eweight; /* user-specified weight-1, and effective weight-1 */ |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 587 | unsigned int wscore; /* weight score, used during srv map computation */ |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 588 | int cur_sess, cur_sess_max; /* number of currently active sessions (including syn_sent) */ |
willy tarreau | a647c70 | 2006-04-15 22:45:52 +0200 | [diff] [blame] | 589 | unsigned int cum_sess; /* cumulated number of sessions really sent to this server */ |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 590 | unsigned int maxconn; /* max # of active sessions. 0 = unlimited. */ |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 591 | unsigned failed_checks, down_trans; /* failed checks and up-down transitions */ |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 592 | unsigned failed_conns, failed_resp; /* failed connect() and responses */ |
| 593 | unsigned failed_secu; /* blocked responses because of security concerns */ |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 594 | struct proxy *proxy; /* the proxy this server belongs to */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 595 | }; |
| 596 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 597 | /* The base for all tasks */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 598 | struct task { |
| 599 | struct task *next, *prev; /* chaining ... */ |
| 600 | struct task *rqnext; /* chaining in run queue ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 601 | struct task *wq; /* the wait queue this task is in */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 602 | int state; /* task state : IDLE or RUNNING */ |
| 603 | struct timeval expire; /* next expiration time for this task, use only for fast sorting */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 604 | int (*process)(struct task *t); /* the function which processes the task */ |
| 605 | void *context; /* the task's context */ |
| 606 | }; |
| 607 | |
| 608 | /* WARNING: if new fields are added, they must be initialized in event_accept() */ |
| 609 | struct session { |
| 610 | struct task *task; /* the task associated with this session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 611 | /* application specific below */ |
| 612 | struct timeval crexpire; /* expiration date for a client read */ |
| 613 | struct timeval cwexpire; /* expiration date for a client write */ |
| 614 | struct timeval srexpire; /* expiration date for a server read */ |
| 615 | struct timeval swexpire; /* expiration date for a server write */ |
| 616 | struct timeval cnexpire; /* expiration date for a connect */ |
| 617 | char res_cr, res_cw, res_sr, res_sw;/* results of some events */ |
| 618 | struct proxy *proxy; /* the proxy this socket belongs to */ |
| 619 | int cli_fd; /* the client side fd */ |
| 620 | int srv_fd; /* the server side fd */ |
| 621 | int cli_state; /* state of the client side */ |
| 622 | int srv_state; /* state of the server side */ |
| 623 | int conn_retries; /* number of connect retries left */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 624 | int flags; /* some flags describing the session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 625 | struct buffer *req; /* request buffer */ |
| 626 | struct buffer *rep; /* response buffer */ |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 627 | struct sockaddr_storage cli_addr; /* the client address */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 628 | struct sockaddr_in srv_addr; /* the address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 629 | struct server *srv; /* the server being used */ |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 630 | struct pendconn *pend_pos; /* if not NULL, points to the position in the pending queue */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 631 | char **req_cap; /* array of captured request headers (may be NULL) */ |
| 632 | char **rsp_cap; /* array of captured response headers (may be NULL) */ |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 633 | struct chunk req_line; /* points to first line */ |
| 634 | struct chunk auth_hdr; /* points to 'Authorization:' header */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 635 | struct { |
| 636 | int logwait; /* log fields waiting to be collected : LW_* */ |
| 637 | struct timeval tv_accept; /* date of the accept() (beginning of the session) */ |
| 638 | long t_request; /* delay before the end of the request arrives, -1 if never occurs */ |
willy tarreau | f32f524 | 2006-05-02 22:54:52 +0200 | [diff] [blame] | 639 | long t_queue; /* delay before the session gets out of the connect queue, -1 if never occurs */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 640 | long t_connect; /* delay before the connect() to the server succeeds, -1 if never occurs */ |
| 641 | long t_data; /* delay before the first data byte from the server ... */ |
| 642 | unsigned long t_close; /* total session duration */ |
willy tarreau | 5e69b16 | 2006-05-12 19:49:37 +0200 | [diff] [blame] | 643 | unsigned long srv_queue_size; /* number of sessions waiting for a connect slot on this server at accept() time (in direct assignment) */ |
| 644 | unsigned long prx_queue_size; /* overall number of sessions waiting for a connect slot on this instance at accept() time */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 645 | char *uri; /* first line if log needed, NULL otherwise */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 646 | char *cli_cookie; /* cookie presented by the client, in capture mode */ |
| 647 | char *srv_cookie; /* cookie presented by the server, in capture mode */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 648 | int status; /* HTTP status from the server, negative if from proxy */ |
| 649 | long long bytes; /* number of bytes transferred from the server */ |
| 650 | } logs; |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 651 | short int data_source; /* where to get the data we generate ourselves */ |
| 652 | short int data_state; /* where to get the data we generate ourselves */ |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 653 | union { |
| 654 | struct { |
| 655 | struct proxy *px; |
| 656 | struct server *sv; |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 657 | short px_st, sv_st; /* DATA_ST_INIT or DATA_ST_DATA */ |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 658 | } stats; |
| 659 | } data_ctx; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 660 | unsigned int uniq_id; /* unique ID used for the traces */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 661 | }; |
| 662 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 663 | struct listener { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 664 | int fd; /* the listen socket */ |
| 665 | struct sockaddr_storage addr; /* the address we listen to */ |
| 666 | struct listener *next; /* next address or NULL */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 667 | }; |
willy tarreau | f32f524 | 2006-05-02 22:54:52 +0200 | [diff] [blame] | 668 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 669 | struct proxy { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 670 | struct listener *listen; /* the listen addresses and sockets */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 671 | struct in_addr mon_net, mon_mask; /* don't forward connections from this net (network order) FIXME: should support IPv6 */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 672 | int state; /* proxy state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 673 | struct sockaddr_in dispatch_addr; /* the default address to connect to */ |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 674 | struct server *srv; /* known servers */ |
| 675 | int srv_act, srv_bck; /* # of running servers */ |
| 676 | int tot_wact, tot_wbck; /* total weights of active and backup servers */ |
| 677 | struct server **srv_map; /* the server map used to apply weights */ |
| 678 | int srv_map_sz; /* the size of the effective server map */ |
| 679 | int srv_rr_idx; /* next server to be elected in round robin mode */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 680 | char *cookie_name; /* name of the cookie to look for */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 681 | int cookie_len; /* strlen(cookie_name), computed only once */ |
| 682 | char *appsession_name; /* name of the cookie to look for */ |
| 683 | int appsession_name_len; /* strlen(appsession_name), computed only once */ |
| 684 | int appsession_len; /* length of the appsession cookie value to be used */ |
| 685 | int appsession_timeout; |
| 686 | CHTbl htbl_proxy; /* Per Proxy hashtable */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 687 | char *capture_name; /* beginning of the name of the cookie to capture */ |
| 688 | int capture_namelen; /* length of the cookie name to match */ |
| 689 | int capture_len; /* length of the string to be captured */ |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 690 | struct uri_auth *uri_auth; /* if non-NULL, the (list of) per-URI authentications */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 691 | int clitimeout; /* client I/O timeout (in milliseconds) */ |
| 692 | int srvtimeout; /* server I/O timeout (in milliseconds) */ |
| 693 | int contimeout; /* connect timeout (in milliseconds) */ |
| 694 | char *id; /* proxy id */ |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 695 | struct list pendconns; /* pending connections with no server assigned yet */ |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 696 | int nbpend, nbpend_max; /* number of pending connections with no server assigned yet */ |
willy tarreau | f32f524 | 2006-05-02 22:54:52 +0200 | [diff] [blame] | 697 | int totpend; /* total number of pending connections on this instance (for stats) */ |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 698 | int nbconn, nbconn_max; /* # of active sessions */ |
willy tarreau | 14b4d43 | 2006-04-07 18:23:29 +0200 | [diff] [blame] | 699 | unsigned int cum_conn; /* cumulated number of processed sessions */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 700 | int maxconn; /* max # of active sessions */ |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 701 | unsigned failed_conns, failed_resp; /* failed connect() and responses */ |
| 702 | unsigned failed_secu; /* blocked responses because of security concerns */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 703 | int conn_retries; /* maximum number of connect retries */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 704 | int options; /* PR_O_REDISP, PR_O_TRANSP, ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 705 | int mode; /* mode = PR_MODE_TCP, PR_MODE_HTTP or PR_MODE_HEALTH */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 706 | struct sockaddr_in source_addr; /* the address to which we want to bind for connect() */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 707 | struct proxy *next; |
| 708 | struct sockaddr_in logsrv1, logsrv2; /* 2 syslog servers */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 709 | signed char logfac1, logfac2; /* log facility for both servers. -1 = disabled */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 710 | int loglev1, loglev2; /* log level for each server, 7 by default */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 711 | int to_log; /* things to be logged (LW_*) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 712 | struct timeval stop_time; /* date to stop listening, when stopping != 0 */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 713 | int nb_reqadd, nb_rspadd; |
| 714 | struct hdr_exp *req_exp; /* regular expressions for request headers */ |
| 715 | struct hdr_exp *rsp_exp; /* regular expressions for response headers */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 716 | int nb_req_cap, nb_rsp_cap; /* # of headers to be captured */ |
| 717 | struct cap_hdr *req_cap; /* chained list of request headers to be captured */ |
| 718 | struct cap_hdr *rsp_cap; /* chained list of response headers to be captured */ |
| 719 | void *req_cap_pool, *rsp_cap_pool; /* pools of pre-allocated char ** used to build the sessions */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 720 | char *req_add[MAX_NEWHDR], *rsp_add[MAX_NEWHDR]; /* headers to be added */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 721 | int grace; /* grace time after stop request */ |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 722 | char *check_req; /* HTTP request to use if PR_O_HTTP_CHK is set, else NULL */ |
| 723 | int check_len; /* Length of the HTTP request */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 724 | struct { |
| 725 | char *msg400; /* message for error 400 */ |
| 726 | int len400; /* message length for error 400 */ |
| 727 | char *msg403; /* message for error 403 */ |
| 728 | int len403; /* message length for error 403 */ |
| 729 | char *msg408; /* message for error 408 */ |
| 730 | int len408; /* message length for error 408 */ |
| 731 | char *msg500; /* message for error 500 */ |
| 732 | int len500; /* message length for error 500 */ |
| 733 | char *msg502; /* message for error 502 */ |
| 734 | int len502; /* message length for error 502 */ |
| 735 | char *msg503; /* message for error 503 */ |
| 736 | int len503; /* message length for error 503 */ |
| 737 | char *msg504; /* message for error 504 */ |
| 738 | int len504; /* message length for error 504 */ |
| 739 | } errmsg; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 740 | }; |
| 741 | |
| 742 | /* info about one given fd */ |
| 743 | struct fdtab { |
| 744 | int (*read)(int fd); /* read function */ |
| 745 | int (*write)(int fd); /* write function */ |
| 746 | struct task *owner; /* the session (or proxy) associated with this fd */ |
| 747 | int state; /* the state of this fd */ |
| 748 | }; |
| 749 | |
| 750 | /*********************************************************************/ |
| 751 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 752 | int cfg_maxpconn = DEFAULT_MAXCONN; /* # of simultaneous connections per proxy (-N) */ |
Willy TARREAU | 13032e7 | 2006-03-12 17:31:45 +0100 | [diff] [blame] | 753 | int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 754 | char *cfg_cfgfile = NULL; /* configuration file */ |
| 755 | char *progname = NULL; /* program name */ |
| 756 | int pid; /* current process id */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 757 | |
| 758 | /* global options */ |
| 759 | static struct { |
| 760 | int uid; |
| 761 | int gid; |
| 762 | int nbproc; |
| 763 | int maxconn; |
| 764 | int maxsock; /* max # of sockets */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 765 | int rlimit_nofile; /* default ulimit-n value : 0=unset */ |
willy tarreau | 746e26b | 2006-03-25 11:14:35 +0100 | [diff] [blame] | 766 | int rlimit_memmax; /* default ulimit-d in megs value : 0=unset */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 767 | int mode; |
| 768 | char *chroot; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 769 | char *pidfile; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 770 | int logfac1, logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 771 | int loglev1, loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 772 | struct sockaddr_in logsrv1, logsrv2; |
| 773 | } global = { |
| 774 | logfac1 : -1, |
| 775 | logfac2 : -1, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 776 | loglev1 : 7, /* max syslog level : debug */ |
| 777 | loglev2 : 7, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 778 | /* others NULL OK */ |
| 779 | }; |
| 780 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 781 | /*********************************************************************/ |
| 782 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 783 | fd_set *StaticReadEvent, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 784 | *StaticWriteEvent; |
| 785 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 786 | int cfg_polling_mechanism = 0; /* POLL_USE_{SELECT|POLL|EPOLL} */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 787 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 788 | void **pool_session = NULL, |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 789 | **pool_pendconn = NULL, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 790 | **pool_buffer = NULL, |
| 791 | **pool_fdtab = NULL, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 792 | **pool_requri = NULL, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 793 | **pool_task = NULL, |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 794 | **pool_capture = NULL, |
| 795 | **pool_appsess = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 796 | |
| 797 | struct proxy *proxy = NULL; /* list of all existing proxies */ |
| 798 | struct fdtab *fdtab = NULL; /* array of all the file descriptors */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 799 | struct task *rq = NULL; /* global run queue */ |
willy tarreau | 5e698ef | 2006-05-02 14:51:00 +0200 | [diff] [blame] | 800 | struct task wait_queue[2] = { /* global wait queue */ |
| 801 | { |
| 802 | prev:LIST_HEAD(wait_queue[0]), /* expirable tasks */ |
| 803 | next:LIST_HEAD(wait_queue[0]), |
| 804 | }, |
| 805 | { |
| 806 | prev:LIST_HEAD(wait_queue[1]), /* non-expirable tasks */ |
| 807 | next:LIST_HEAD(wait_queue[1]), |
| 808 | }, |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 809 | }; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 810 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 811 | static int totalconn = 0; /* total # of terminated sessions */ |
| 812 | static int actconn = 0; /* # of active sessions */ |
| 813 | static int maxfd = 0; /* # of the highest fd + 1 */ |
| 814 | static int listeners = 0; /* # of listeners */ |
| 815 | static int stopping = 0; /* non zero means stopping in progress */ |
| 816 | static struct timeval now = {0,0}; /* the current date at any moment */ |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 817 | static struct timeval start_date; /* the process's start date */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 818 | static struct proxy defproxy; /* fake proxy used to assign default values on all instances */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 819 | |
willy tarreau | 53e9970 | 2006-03-25 18:53:50 +0100 | [diff] [blame] | 820 | /* Here we store informations about the pids of the processes we may pause |
| 821 | * or kill. We will send them a signal every 10 ms until we can bind to all |
| 822 | * our ports. With 200 retries, that's about 2 seconds. |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 823 | */ |
willy tarreau | 53e9970 | 2006-03-25 18:53:50 +0100 | [diff] [blame] | 824 | #define MAX_START_RETRIES 200 |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 825 | static int nb_oldpids = 0; |
| 826 | static int *oldpids = NULL; |
| 827 | static int oldpids_sig; /* use USR1 or TERM */ |
| 828 | |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 829 | #if defined(ENABLE_EPOLL) |
| 830 | /* FIXME: this is dirty, but at the moment, there's no other solution to remove |
| 831 | * the old FDs from outside the loop. Perhaps we should export a global 'poll' |
| 832 | * structure with pointers to functions such as init_fd() and close_fd(), plus |
| 833 | * a private structure with several pointers to places such as below. |
| 834 | */ |
| 835 | |
| 836 | static fd_set *PrevReadEvent = NULL, *PrevWriteEvent = NULL; |
| 837 | #endif |
| 838 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 839 | static regmatch_t pmatch[MAX_MATCH]; /* rm_so, rm_eo for regular expressions */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 840 | /* this is used to drain data, and as a temporary buffer for sprintf()... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 841 | static char trash[BUFSIZE]; |
| 842 | |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 843 | const int zero = 0; |
| 844 | const int one = 1; |
| 845 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 846 | /* |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 847 | * Syslog facilities and levels. Conforming to RFC3164. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 848 | */ |
| 849 | |
| 850 | #define MAX_SYSLOG_LEN 1024 |
| 851 | #define NB_LOG_FACILITIES 24 |
| 852 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 853 | "kern", "user", "mail", "daemon", |
| 854 | "auth", "syslog", "lpr", "news", |
| 855 | "uucp", "cron", "auth2", "ftp", |
| 856 | "ntp", "audit", "alert", "cron2", |
| 857 | "local0", "local1", "local2", "local3", |
| 858 | "local4", "local5", "local6", "local7" |
| 859 | }; |
| 860 | |
| 861 | |
| 862 | #define NB_LOG_LEVELS 8 |
| 863 | const char *log_levels[NB_LOG_LEVELS] = { |
| 864 | "emerg", "alert", "crit", "err", |
| 865 | "warning", "notice", "info", "debug" |
| 866 | }; |
| 867 | |
| 868 | #define SYSLOG_PORT 514 |
| 869 | |
| 870 | const char *monthname[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 871 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 872 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 873 | const char sess_term_cond[8] = "-cCsSPRI"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal */ |
willy tarreau | 078c79a | 2006-05-13 12:23:58 +0200 | [diff] [blame] | 874 | const char sess_fin_state[8] = "-RCHDLQ7"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, unknown */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 875 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 876 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 877 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 878 | unknown, Set-cookie Rewritten */ |
| 879 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 880 | #define MAX_HOSTNAME_LEN 32 |
| 881 | static char hostname[MAX_HOSTNAME_LEN] = ""; |
| 882 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 883 | const char *HTTP_302 = |
| 884 | "HTTP/1.0 302 Found\r\n" |
| 885 | "Cache-Control: no-cache\r\n" |
| 886 | "Connection: close\r\n" |
| 887 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 888 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 889 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 890 | const char *HTTP_303 = |
| 891 | "HTTP/1.0 303 See Other\r\n" |
| 892 | "Cache-Control: no-cache\r\n" |
| 893 | "Connection: close\r\n" |
| 894 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 895 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 896 | const char *HTTP_400 = |
| 897 | "HTTP/1.0 400 Bad request\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 898 | "Cache-Control: no-cache\r\n" |
| 899 | "Connection: close\r\n" |
| 900 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 901 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n"; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 902 | |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 903 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 904 | const char *HTTP_401_fmt = |
| 905 | "HTTP/1.0 401 Unauthorized\r\n" |
| 906 | "Cache-Control: no-cache\r\n" |
| 907 | "Connection: close\r\n" |
| 908 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 909 | "\r\n" |
| 910 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 911 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 912 | const char *HTTP_403 = |
| 913 | "HTTP/1.0 403 Forbidden\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 914 | "Cache-Control: no-cache\r\n" |
| 915 | "Connection: close\r\n" |
| 916 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 917 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n"; |
| 918 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 919 | const char *HTTP_408 = |
| 920 | "HTTP/1.0 408 Request Time-out\r\n" |
| 921 | "Cache-Control: no-cache\r\n" |
| 922 | "Connection: close\r\n" |
| 923 | "\r\n" |
| 924 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n"; |
| 925 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 926 | const char *HTTP_500 = |
| 927 | "HTTP/1.0 500 Server Error\r\n" |
| 928 | "Cache-Control: no-cache\r\n" |
| 929 | "Connection: close\r\n" |
| 930 | "\r\n" |
| 931 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n"; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 932 | |
| 933 | const char *HTTP_502 = |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 934 | "HTTP/1.0 502 Bad Gateway\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 935 | "Cache-Control: no-cache\r\n" |
| 936 | "Connection: close\r\n" |
| 937 | "\r\n" |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 938 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n"; |
| 939 | |
| 940 | const char *HTTP_503 = |
| 941 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 942 | "Cache-Control: no-cache\r\n" |
| 943 | "Connection: close\r\n" |
| 944 | "\r\n" |
| 945 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n"; |
| 946 | |
| 947 | const char *HTTP_504 = |
| 948 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 949 | "Cache-Control: no-cache\r\n" |
| 950 | "Connection: close\r\n" |
| 951 | "\r\n" |
| 952 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n"; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 953 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 954 | /*********************************************************************/ |
| 955 | /* statistics ******************************************************/ |
| 956 | /*********************************************************************/ |
| 957 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 958 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 959 | static int stats_tsk_lsrch, stats_tsk_rsrch, |
| 960 | stats_tsk_good, stats_tsk_right, stats_tsk_left, |
| 961 | stats_tsk_new, stats_tsk_nsrch; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 962 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 963 | |
| 964 | |
| 965 | /*********************************************************************/ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 966 | /* debugging *******************************************************/ |
| 967 | /*********************************************************************/ |
| 968 | #ifdef DEBUG_FULL |
| 969 | static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" }; |
willy tarreau | 3504a01 | 2006-05-14 23:20:07 +0200 | [diff] [blame] | 970 | static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" }; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 971 | #endif |
| 972 | |
| 973 | /*********************************************************************/ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 974 | /* function prototypes *********************************************/ |
| 975 | /*********************************************************************/ |
| 976 | |
| 977 | int event_accept(int fd); |
| 978 | int event_cli_read(int fd); |
| 979 | int event_cli_write(int fd); |
| 980 | int event_srv_read(int fd); |
| 981 | int event_srv_write(int fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 982 | int process_session(struct task *t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 983 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 984 | static int appsession_task_init(void); |
| 985 | static int appsession_init(void); |
| 986 | static int appsession_refresh(struct task *t); |
| 987 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 988 | /*********************************************************************/ |
| 989 | /* general purpose functions ***************************************/ |
| 990 | /*********************************************************************/ |
| 991 | |
| 992 | void display_version() { |
| 993 | printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n"); |
willy tarreau | 726618c | 2006-01-29 22:42:06 +0100 | [diff] [blame] | 994 | printf("Copyright 2000-2006 Willy Tarreau <w@w.ods.org>\n\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | /* |
| 998 | * This function prints the command line usage and exits |
| 999 | */ |
| 1000 | void usage(char *name) { |
| 1001 | display_version(); |
| 1002 | fprintf(stderr, |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1003 | "Usage : %s -f <cfgfile> [ -vdV" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1004 | #if STATTIME > 0 |
| 1005 | "sl" |
| 1006 | #endif |
willy tarreau | 746e26b | 2006-03-25 11:14:35 +0100 | [diff] [blame] | 1007 | "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n" |
| 1008 | " [ -p <pidfile> ] [ -m <max megs> ]\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1009 | " -v displays version\n" |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 1010 | " -d enters debug mode ; -db only disables background mode.\n" |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1011 | " -V enters verbose mode (disables quiet mode)\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1012 | #if STATTIME > 0 |
| 1013 | " -s enables statistics output\n" |
| 1014 | " -l enables long statistics format\n" |
| 1015 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1016 | " -D goes daemon ; implies -q\n" |
| 1017 | " -q quiet mode : don't display messages\n" |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 1018 | " -c check mode : only check config file and exit\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1019 | " -n sets the maximum total # of connections (%d)\n" |
willy tarreau | 746e26b | 2006-03-25 11:14:35 +0100 | [diff] [blame] | 1020 | " -m limits the usable amount of memory (in MB)\n" |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 1021 | " -N sets the default, per-proxy maximum # of connections (%d)\n" |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 1022 | " -p writes pids of all children to this file\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 1023 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 1024 | " -de disables epoll() usage even when available\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 1025 | #endif |
| 1026 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 1027 | " -dp disables poll() usage even when available\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 1028 | #endif |
willy tarreau | 53e9970 | 2006-03-25 18:53:50 +0100 | [diff] [blame] | 1029 | " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n" |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 1030 | "\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1031 | name, DEFAULT_MAXCONN, cfg_maxpconn); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1032 | exit(1); |
| 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | /* |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1037 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 1038 | * mode during startup. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1039 | */ |
| 1040 | void Alert(char *fmt, ...) { |
| 1041 | va_list argp; |
| 1042 | struct timeval tv; |
| 1043 | struct tm *tm; |
| 1044 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1045 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1046 | va_start(argp, fmt); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1047 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1048 | gettimeofday(&tv, NULL); |
| 1049 | tm=localtime(&tv.tv_sec); |
| 1050 | fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1051 | tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)getpid()); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1052 | vfprintf(stderr, fmt, argp); |
| 1053 | fflush(stderr); |
| 1054 | va_end(argp); |
| 1055 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | /* |
| 1060 | * Displays the message on stderr with the date and pid. |
| 1061 | */ |
| 1062 | void Warning(char *fmt, ...) { |
| 1063 | va_list argp; |
| 1064 | struct timeval tv; |
| 1065 | struct tm *tm; |
| 1066 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1067 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1068 | va_start(argp, fmt); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1069 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1070 | gettimeofday(&tv, NULL); |
| 1071 | tm=localtime(&tv.tv_sec); |
| 1072 | fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1073 | tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)getpid()); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1074 | vfprintf(stderr, fmt, argp); |
| 1075 | fflush(stderr); |
| 1076 | va_end(argp); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | /* |
| 1081 | * Displays the message on <out> only if quiet mode is not set. |
| 1082 | */ |
| 1083 | void qfprintf(FILE *out, char *fmt, ...) { |
| 1084 | va_list argp; |
| 1085 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1086 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1087 | va_start(argp, fmt); |
| 1088 | vfprintf(out, fmt, argp); |
| 1089 | fflush(out); |
| 1090 | va_end(argp); |
| 1091 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | |
| 1095 | /* |
| 1096 | * converts <str> to a struct sockaddr_in* which is locally allocated. |
| 1097 | * The format is "addr:port", where "addr" can be empty or "*" to indicate |
| 1098 | * INADDR_ANY. |
| 1099 | */ |
| 1100 | struct sockaddr_in *str2sa(char *str) { |
| 1101 | static struct sockaddr_in sa; |
| 1102 | char *c; |
| 1103 | int port; |
| 1104 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1105 | memset(&sa, 0, sizeof(sa)); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1106 | str=strdup(str); |
| 1107 | |
| 1108 | if ((c=strrchr(str,':')) != NULL) { |
| 1109 | *c++=0; |
| 1110 | port=atol(c); |
| 1111 | } |
| 1112 | else |
| 1113 | port=0; |
| 1114 | |
| 1115 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 1116 | sa.sin_addr.s_addr = INADDR_ANY; |
| 1117 | } |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1118 | else if (!inet_pton(AF_INET, str, &sa.sin_addr)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1119 | struct hostent *he; |
| 1120 | |
| 1121 | if ((he = gethostbyname(str)) == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 1122 | Alert("Invalid server name: '%s'\n", str); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1123 | } |
| 1124 | else |
| 1125 | sa.sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 1126 | } |
| 1127 | sa.sin_port=htons(port); |
| 1128 | sa.sin_family=AF_INET; |
| 1129 | |
| 1130 | free(str); |
| 1131 | return &sa; |
| 1132 | } |
| 1133 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1134 | /* |
| 1135 | * converts <str> to a two struct in_addr* which are locally allocated. |
| 1136 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
| 1137 | * is optionnal and either in the dotted or CIDR notation. |
| 1138 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 1139 | */ |
| 1140 | int str2net(char *str, struct in_addr *addr, struct in_addr *mask) { |
| 1141 | char *c; |
| 1142 | unsigned long len; |
| 1143 | |
| 1144 | memset(mask, 0, sizeof(*mask)); |
| 1145 | memset(addr, 0, sizeof(*addr)); |
| 1146 | str=strdup(str); |
| 1147 | |
| 1148 | if ((c = strrchr(str, '/')) != NULL) { |
| 1149 | *c++ = 0; |
| 1150 | /* c points to the mask */ |
| 1151 | if (strchr(c, '.') != NULL) { /* dotted notation */ |
| 1152 | if (!inet_pton(AF_INET, c, mask)) |
| 1153 | return 0; |
| 1154 | } |
| 1155 | else { /* mask length */ |
| 1156 | char *err; |
| 1157 | len = strtol(c, &err, 10); |
| 1158 | if (!*c || (err && *err) || (unsigned)len > 32) |
| 1159 | return 0; |
| 1160 | if (len) |
| 1161 | mask->s_addr = htonl(0xFFFFFFFFUL << (32 - len)); |
| 1162 | else |
| 1163 | mask->s_addr = 0; |
| 1164 | } |
| 1165 | } |
| 1166 | else { |
| 1167 | mask->s_addr = 0xFFFFFFFF; |
| 1168 | } |
| 1169 | if (!inet_pton(AF_INET, str, addr)) { |
| 1170 | struct hostent *he; |
| 1171 | |
| 1172 | if ((he = gethostbyname(str)) == NULL) { |
| 1173 | return 0; |
| 1174 | } |
| 1175 | else |
| 1176 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 1177 | } |
| 1178 | free(str); |
| 1179 | return 1; |
| 1180 | } |
| 1181 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1182 | |
| 1183 | /* |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1184 | * converts <str> to a list of listeners which are dynamically allocated. |
| 1185 | * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where : |
| 1186 | * - <addr> can be empty or "*" to indicate INADDR_ANY ; |
| 1187 | * - <port> is a numerical port from 1 to 65535 ; |
| 1188 | * - <end> indicates to use the range from <port> to <end> instead (inclusive). |
| 1189 | * This can be repeated as many times as necessary, separated by a coma. |
| 1190 | * The <tail> argument is a pointer to a current list which should be appended |
| 1191 | * to the tail of the new list. The pointer to the new list is returned. |
| 1192 | */ |
| 1193 | struct listener *str2listener(char *str, struct listener *tail) { |
| 1194 | struct listener *l; |
| 1195 | char *c, *next, *range, *dupstr; |
| 1196 | int port, end; |
| 1197 | |
| 1198 | next = dupstr = strdup(str); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1199 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1200 | while (next && *next) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1201 | struct sockaddr_storage ss; |
| 1202 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1203 | str = next; |
| 1204 | /* 1) look for the end of the first address */ |
| 1205 | if ((next = strrchr(str, ',')) != NULL) { |
| 1206 | *next++ = 0; |
| 1207 | } |
| 1208 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1209 | /* 2) look for the addr/port delimiter, it's the last colon. */ |
| 1210 | if ((range = strrchr(str, ':')) == NULL) { |
| 1211 | Alert("Missing port number: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1212 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | *range++ = 0; |
| 1216 | |
| 1217 | if (strrchr(str, ':') != NULL) { |
| 1218 | /* IPv6 address contains ':' */ |
| 1219 | memset(&ss, 0, sizeof(ss)); |
| 1220 | ss.ss_family = AF_INET6; |
| 1221 | |
| 1222 | if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in6 *)&ss)->sin6_addr)) { |
| 1223 | Alert("Invalid server address: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1224 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1225 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1226 | } |
| 1227 | else { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1228 | memset(&ss, 0, sizeof(ss)); |
| 1229 | ss.ss_family = AF_INET; |
| 1230 | |
| 1231 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 1232 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; |
| 1233 | } |
| 1234 | else if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in *)&ss)->sin_addr)) { |
| 1235 | struct hostent *he; |
| 1236 | |
| 1237 | if ((he = gethostbyname(str)) == NULL) { |
| 1238 | Alert("Invalid server name: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1239 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1240 | } |
| 1241 | else |
| 1242 | ((struct sockaddr_in *)&ss)->sin_addr = |
| 1243 | *(struct in_addr *) *(he->h_addr_list); |
| 1244 | } |
| 1245 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1246 | |
| 1247 | /* 3) look for the port-end delimiter */ |
| 1248 | if ((c = strchr(range, '-')) != NULL) { |
| 1249 | *c++ = 0; |
| 1250 | end = atol(c); |
| 1251 | } |
| 1252 | else { |
| 1253 | end = atol(range); |
| 1254 | } |
| 1255 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1256 | port = atol(range); |
| 1257 | |
| 1258 | if (port < 1 || port > 65535) { |
| 1259 | Alert("Invalid port '%d' specified for address '%s'.\n", port, str); |
| 1260 | goto fail; |
| 1261 | } |
| 1262 | |
| 1263 | if (end < 1 || end > 65535) { |
| 1264 | Alert("Invalid port '%d' specified for address '%s'.\n", end, str); |
| 1265 | goto fail; |
| 1266 | } |
| 1267 | |
| 1268 | for (; port <= end; port++) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1269 | l = (struct listener *)calloc(1, sizeof(struct listener)); |
| 1270 | l->next = tail; |
| 1271 | tail = l; |
| 1272 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 1273 | l->fd = -1; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1274 | l->addr = ss; |
| 1275 | if (ss.ss_family == AF_INET6) |
| 1276 | ((struct sockaddr_in6 *)(&l->addr))->sin6_port = htons(port); |
| 1277 | else |
| 1278 | ((struct sockaddr_in *)(&l->addr))->sin_port = htons(port); |
| 1279 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1280 | } /* end for(port) */ |
| 1281 | } /* end while(next) */ |
| 1282 | free(dupstr); |
| 1283 | return tail; |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1284 | fail: |
| 1285 | free(dupstr); |
| 1286 | return NULL; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1287 | } |
| 1288 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1289 | |
| 1290 | #define FD_SETS_ARE_BITFIELDS |
| 1291 | #ifdef FD_SETS_ARE_BITFIELDS |
| 1292 | /* |
| 1293 | * This map is used with all the FD_* macros to check whether a particular bit |
| 1294 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 1295 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 1296 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 1297 | * exclusively to the macros. |
| 1298 | */ |
| 1299 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 1300 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 1301 | |
| 1302 | #else |
| 1303 | #error "Check if your OS uses bitfields for fd_sets" |
| 1304 | #endif |
| 1305 | |
| 1306 | /* will try to encode the string <string> replacing all characters tagged in |
| 1307 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 1308 | * prefixed by <escape>, and will store the result between <start> (included |
| 1309 | *) and <stop> (excluded), and will always terminate the string with a '\0' |
| 1310 | * before <stop>. The position of the '\0' is returned if the conversion |
| 1311 | * completes. If bytes are missing between <start> and <stop>, then the |
| 1312 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 1313 | * cannot even be stored so we return <start> without writing the 0. |
| 1314 | * The input string must also be zero-terminated. |
| 1315 | */ |
| 1316 | char hextab[16] = "0123456789ABCDEF"; |
| 1317 | char *encode_string(char *start, char *stop, |
| 1318 | const char escape, const fd_set *map, |
| 1319 | const char *string) |
| 1320 | { |
| 1321 | if (start < stop) { |
| 1322 | stop--; /* reserve one byte for the final '\0' */ |
| 1323 | while (start < stop && *string != 0) { |
| 1324 | if (!FD_ISSET((unsigned char)(*string), map)) |
| 1325 | *start++ = *string; |
| 1326 | else { |
| 1327 | if (start + 3 >= stop) |
| 1328 | break; |
| 1329 | *start++ = escape; |
| 1330 | *start++ = hextab[(*string >> 4) & 15]; |
| 1331 | *start++ = hextab[*string & 15]; |
| 1332 | } |
| 1333 | string++; |
| 1334 | } |
| 1335 | *start = '\0'; |
| 1336 | } |
| 1337 | return start; |
| 1338 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1339 | |
| 1340 | /* |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1341 | * This function sends a syslog message to both log servers of a proxy, |
| 1342 | * or to global log servers if the proxy is NULL. |
| 1343 | * It also tries not to waste too much time computing the message header. |
| 1344 | * It doesn't care about errors nor does it report them. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1345 | */ |
| 1346 | void send_log(struct proxy *p, int level, char *message, ...) { |
| 1347 | static int logfd = -1; /* syslog UDP socket */ |
| 1348 | static long tvsec = -1; /* to force the string to be initialized */ |
| 1349 | struct timeval tv; |
| 1350 | va_list argp; |
| 1351 | static char logmsg[MAX_SYSLOG_LEN]; |
| 1352 | static char *dataptr = NULL; |
| 1353 | int fac_level; |
| 1354 | int hdr_len, data_len; |
| 1355 | struct sockaddr_in *sa[2]; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1356 | int facilities[2], loglevel[2]; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1357 | int nbloggers = 0; |
| 1358 | char *log_ptr; |
| 1359 | |
| 1360 | if (logfd < 0) { |
| 1361 | if ((logfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) |
| 1362 | return; |
| 1363 | } |
| 1364 | |
| 1365 | if (level < 0 || progname == NULL || message == NULL) |
| 1366 | return; |
| 1367 | |
| 1368 | gettimeofday(&tv, NULL); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1369 | if (tv.tv_sec != tvsec || dataptr == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1370 | /* this string is rebuild only once a second */ |
| 1371 | struct tm *tm = localtime(&tv.tv_sec); |
| 1372 | tvsec = tv.tv_sec; |
| 1373 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1374 | hdr_len = snprintf(logmsg, sizeof(logmsg), |
| 1375 | "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ", |
| 1376 | monthname[tm->tm_mon], |
| 1377 | tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 1378 | progname, pid); |
| 1379 | /* WARNING: depending upon implementations, snprintf may return |
| 1380 | * either -1 or the number of bytes that would be needed to store |
| 1381 | * the total message. In both cases, we must adjust it. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1382 | */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1383 | if (hdr_len < 0 || hdr_len > sizeof(logmsg)) |
| 1384 | hdr_len = sizeof(logmsg); |
| 1385 | |
| 1386 | dataptr = logmsg + hdr_len; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | va_start(argp, message); |
| 1390 | data_len = vsnprintf(dataptr, logmsg + sizeof(logmsg) - dataptr, message, argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1391 | if (data_len < 0 || data_len > (logmsg + sizeof(logmsg) - dataptr)) |
| 1392 | data_len = logmsg + sizeof(logmsg) - dataptr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1393 | va_end(argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1394 | dataptr[data_len - 1] = '\n'; /* force a break on ultra-long lines */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1395 | |
| 1396 | if (p == NULL) { |
| 1397 | if (global.logfac1 >= 0) { |
| 1398 | sa[nbloggers] = &global.logsrv1; |
| 1399 | facilities[nbloggers] = global.logfac1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1400 | loglevel[nbloggers] = global.loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1401 | nbloggers++; |
| 1402 | } |
| 1403 | if (global.logfac2 >= 0) { |
| 1404 | sa[nbloggers] = &global.logsrv2; |
| 1405 | facilities[nbloggers] = global.logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1406 | loglevel[nbloggers] = global.loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1407 | nbloggers++; |
| 1408 | } |
| 1409 | } else { |
| 1410 | if (p->logfac1 >= 0) { |
| 1411 | sa[nbloggers] = &p->logsrv1; |
| 1412 | facilities[nbloggers] = p->logfac1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1413 | loglevel[nbloggers] = p->loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1414 | nbloggers++; |
| 1415 | } |
| 1416 | if (p->logfac2 >= 0) { |
| 1417 | sa[nbloggers] = &p->logsrv2; |
| 1418 | facilities[nbloggers] = p->logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1419 | loglevel[nbloggers] = p->loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1420 | nbloggers++; |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | while (nbloggers-- > 0) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1425 | /* we can filter the level of the messages that are sent to each logger */ |
| 1426 | if (level > loglevel[nbloggers]) |
| 1427 | continue; |
| 1428 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1429 | /* For each target, we may have a different facility. |
| 1430 | * We can also have a different log level for each message. |
| 1431 | * This induces variations in the message header length. |
| 1432 | * Since we don't want to recompute it each time, nor copy it every |
| 1433 | * time, we only change the facility in the pre-computed header, |
| 1434 | * and we change the pointer to the header accordingly. |
| 1435 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1436 | fac_level = (facilities[nbloggers] << 3) + level; |
| 1437 | log_ptr = logmsg + 3; /* last digit of the log level */ |
| 1438 | do { |
| 1439 | *log_ptr = '0' + fac_level % 10; |
| 1440 | fac_level /= 10; |
| 1441 | log_ptr--; |
| 1442 | } while (fac_level && log_ptr > logmsg); |
| 1443 | *log_ptr = '<'; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1444 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1445 | /* the total syslog message now starts at logptr, for dataptr+data_len-logptr */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1446 | |
| 1447 | #ifndef MSG_NOSIGNAL |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1448 | sendto(logfd, log_ptr, dataptr + data_len - log_ptr, MSG_DONTWAIT, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1449 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 1450 | #else |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1451 | sendto(logfd, log_ptr, dataptr + data_len - log_ptr, MSG_DONTWAIT | MSG_NOSIGNAL, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1452 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 1453 | #endif |
| 1454 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | |
| 1458 | /* sets <tv> to the current time */ |
| 1459 | static inline struct timeval *tv_now(struct timeval *tv) { |
| 1460 | if (tv) |
| 1461 | gettimeofday(tv, NULL); |
| 1462 | return tv; |
| 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 1467 | */ |
willy tarreau | dab722b | 2006-05-04 19:23:38 +0200 | [diff] [blame] | 1468 | static struct timeval *tv_delayfrom(struct timeval *tv, struct timeval *from, int ms) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1469 | if (!tv || !from) |
| 1470 | return NULL; |
| 1471 | tv->tv_usec = from->tv_usec + (ms%1000)*1000; |
| 1472 | tv->tv_sec = from->tv_sec + (ms/1000); |
| 1473 | while (tv->tv_usec >= 1000000) { |
| 1474 | tv->tv_usec -= 1000000; |
| 1475 | tv->tv_sec++; |
| 1476 | } |
| 1477 | return tv; |
| 1478 | } |
| 1479 | |
| 1480 | /* |
| 1481 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1482 | * Must not be used when either argument is eternity. Use tv_cmp2() for that. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1483 | */ |
| 1484 | static inline int tv_cmp(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1485 | if (tv1->tv_sec < tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1486 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1487 | else if (tv1->tv_sec > tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1488 | return 1; |
| 1489 | else if (tv1->tv_usec < tv2->tv_usec) |
| 1490 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1491 | else if (tv1->tv_usec > tv2->tv_usec) |
| 1492 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1493 | else |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | /* |
| 1498 | * returns the absolute difference, in ms, between tv1 and tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1499 | * Must not be used when either argument is eternity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1500 | */ |
| 1501 | unsigned long tv_delta(struct timeval *tv1, struct timeval *tv2) { |
| 1502 | int cmp; |
| 1503 | unsigned long ret; |
| 1504 | |
| 1505 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1506 | cmp = tv_cmp(tv1, tv2); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1507 | if (!cmp) |
| 1508 | return 0; /* same dates, null diff */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1509 | else if (cmp < 0) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1510 | struct timeval *tmp = tv1; |
| 1511 | tv1 = tv2; |
| 1512 | tv2 = tmp; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1513 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1514 | ret = (tv1->tv_sec - tv2->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1515 | if (tv1->tv_usec > tv2->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1516 | ret += (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1517 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1518 | ret -= (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1519 | return (unsigned long) ret; |
| 1520 | } |
| 1521 | |
| 1522 | /* |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1523 | * returns the difference, in ms, between tv1 and tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1524 | * Must not be used when either argument is eternity. |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1525 | */ |
| 1526 | static inline unsigned long tv_diff(struct timeval *tv1, struct timeval *tv2) { |
| 1527 | unsigned long ret; |
| 1528 | |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 1529 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 1530 | if (tv2->tv_usec > tv1->tv_usec) |
| 1531 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1532 | else |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 1533 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1534 | return (unsigned long) ret; |
| 1535 | } |
| 1536 | |
| 1537 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1538 | * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1539 | * Must not be used when either argument is eternity. Use tv_cmp2_ms() for that. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1540 | */ |
willy tarreau | dab722b | 2006-05-04 19:23:38 +0200 | [diff] [blame] | 1541 | static int tv_cmp_ms(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1542 | if (tv1->tv_sec == tv2->tv_sec) { |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1543 | if (tv2->tv_usec >= tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1544 | return -1; |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1545 | else if (tv1->tv_usec >= tv2->tv_usec + 1000) |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1546 | return 1; |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1547 | else |
| 1548 | return 0; |
| 1549 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1550 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1551 | ((tv2->tv_sec == tv1->tv_sec + 1) && (tv2->tv_usec + 1000000 >= tv1->tv_usec + 1000))) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1552 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1553 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1554 | ((tv1->tv_sec == tv2->tv_sec + 1) && (tv1->tv_usec + 1000000 >= tv2->tv_usec + 1000))) |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1555 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1556 | else |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
| 1560 | /* |
| 1561 | * returns the remaining time between tv1=now and event=tv2 |
| 1562 | * if tv2 is passed, 0 is returned. |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1563 | * Must not be used when either argument is eternity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1564 | */ |
| 1565 | static inline unsigned long tv_remain(struct timeval *tv1, struct timeval *tv2) { |
| 1566 | unsigned long ret; |
| 1567 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1568 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 1569 | return 0; /* event elapsed */ |
| 1570 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1571 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1572 | if (tv2->tv_usec > tv1->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1573 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1574 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1575 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1576 | return (unsigned long) ret; |
| 1577 | } |
| 1578 | |
| 1579 | |
| 1580 | /* |
| 1581 | * zeroes a struct timeval |
| 1582 | */ |
| 1583 | |
| 1584 | static inline struct timeval *tv_eternity(struct timeval *tv) { |
| 1585 | tv->tv_sec = tv->tv_usec = 0; |
| 1586 | return tv; |
| 1587 | } |
| 1588 | |
| 1589 | /* |
| 1590 | * returns 1 if tv is null, else 0 |
| 1591 | */ |
| 1592 | static inline int tv_iseternity(struct timeval *tv) { |
| 1593 | if (tv->tv_sec == 0 && tv->tv_usec == 0) |
| 1594 | return 1; |
| 1595 | else |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1601 | * considering that 0 is the eternity. |
| 1602 | */ |
willy tarreau | dab722b | 2006-05-04 19:23:38 +0200 | [diff] [blame] | 1603 | static int tv_cmp2(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1604 | if (tv_iseternity(tv1)) |
| 1605 | if (tv_iseternity(tv2)) |
| 1606 | return 0; /* same */ |
| 1607 | else |
| 1608 | return 1; /* tv1 later than tv2 */ |
| 1609 | else if (tv_iseternity(tv2)) |
| 1610 | return -1; /* tv2 later than tv1 */ |
| 1611 | |
| 1612 | if (tv1->tv_sec > tv2->tv_sec) |
| 1613 | return 1; |
| 1614 | else if (tv1->tv_sec < tv2->tv_sec) |
| 1615 | return -1; |
| 1616 | else if (tv1->tv_usec > tv2->tv_usec) |
| 1617 | return 1; |
| 1618 | else if (tv1->tv_usec < tv2->tv_usec) |
| 1619 | return -1; |
| 1620 | else |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1626 | * considering that 0 is the eternity. |
| 1627 | */ |
willy tarreau | dab722b | 2006-05-04 19:23:38 +0200 | [diff] [blame] | 1628 | static int tv_cmp2_ms(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1629 | if (tv_iseternity(tv1)) |
| 1630 | if (tv_iseternity(tv2)) |
| 1631 | return 0; /* same */ |
| 1632 | else |
| 1633 | return 1; /* tv1 later than tv2 */ |
| 1634 | else if (tv_iseternity(tv2)) |
| 1635 | return -1; /* tv2 later than tv1 */ |
| 1636 | |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1637 | if (tv1->tv_sec == tv2->tv_sec) { |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1638 | if (tv1->tv_usec >= tv2->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1639 | return 1; |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1640 | else if (tv2->tv_usec >= tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1641 | return -1; |
| 1642 | else |
| 1643 | return 0; |
| 1644 | } |
| 1645 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1646 | ((tv1->tv_sec == tv2->tv_sec + 1) && (tv1->tv_usec + 1000000 >= tv2->tv_usec + 1000))) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1647 | return 1; |
| 1648 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1649 | ((tv2->tv_sec == tv1->tv_sec + 1) && (tv2->tv_usec + 1000000 >= tv1->tv_usec + 1000))) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1650 | return -1; |
| 1651 | else |
| 1652 | return 0; |
| 1653 | } |
| 1654 | |
| 1655 | /* |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1656 | * returns the remaining time between tv1=now and event=tv2 |
| 1657 | * if tv2 is passed, 0 is returned. |
| 1658 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 1659 | */ |
willy tarreau | dab722b | 2006-05-04 19:23:38 +0200 | [diff] [blame] | 1660 | static unsigned long tv_remain2(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1661 | unsigned long ret; |
| 1662 | |
| 1663 | if (tv_iseternity(tv2)) |
| 1664 | return TIME_ETERNITY; |
| 1665 | |
| 1666 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 1667 | return 0; /* event elapsed */ |
| 1668 | |
| 1669 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 1670 | if (tv2->tv_usec > tv1->tv_usec) |
| 1671 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 1672 | else |
| 1673 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 1674 | return (unsigned long) ret; |
| 1675 | } |
| 1676 | |
| 1677 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1678 | * returns the first event between tv1 and tv2 into tvmin. |
| 1679 | * a zero tv is ignored. tvmin is returned. |
| 1680 | */ |
| 1681 | static inline struct timeval *tv_min(struct timeval *tvmin, |
| 1682 | struct timeval *tv1, struct timeval *tv2) { |
| 1683 | |
| 1684 | if (tv_cmp2(tv1, tv2) <= 0) |
| 1685 | *tvmin = *tv1; |
| 1686 | else |
| 1687 | *tvmin = *tv2; |
| 1688 | |
| 1689 | return tvmin; |
| 1690 | } |
| 1691 | |
| 1692 | |
| 1693 | |
| 1694 | /***********************************************************/ |
| 1695 | /* fd management ***************************************/ |
| 1696 | /***********************************************************/ |
| 1697 | |
| 1698 | |
| 1699 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1700 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 1701 | * The file descriptor is also closed. |
| 1702 | */ |
willy tarreau | dab722b | 2006-05-04 19:23:38 +0200 | [diff] [blame] | 1703 | static void fd_delete(int fd) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1704 | FD_CLR(fd, StaticReadEvent); |
| 1705 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 1706 | #if defined(ENABLE_EPOLL) |
| 1707 | if (PrevReadEvent) { |
| 1708 | FD_CLR(fd, PrevReadEvent); |
| 1709 | FD_CLR(fd, PrevWriteEvent); |
| 1710 | } |
| 1711 | #endif |
| 1712 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1713 | close(fd); |
| 1714 | fdtab[fd].state = FD_STCLOSE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1715 | |
| 1716 | while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE)) |
| 1717 | maxfd--; |
| 1718 | } |
| 1719 | |
| 1720 | /* recomputes the maxfd limit from the fd */ |
| 1721 | static inline void fd_insert(int fd) { |
| 1722 | if (fd+1 > maxfd) |
| 1723 | maxfd = fd+1; |
| 1724 | } |
| 1725 | |
| 1726 | /*************************************************************/ |
| 1727 | /* task management ***************************************/ |
| 1728 | /*************************************************************/ |
| 1729 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1730 | /* puts the task <t> in run queue <q>, and returns <t> */ |
| 1731 | static inline struct task *task_wakeup(struct task **q, struct task *t) { |
| 1732 | if (t->state == TASK_RUNNING) |
| 1733 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1734 | else { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1735 | t->rqnext = *q; |
| 1736 | t->state = TASK_RUNNING; |
| 1737 | return *q = t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1741 | /* removes the task <t> from the queue <q> |
| 1742 | * <s> MUST be <q>'s first task. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1743 | * set the run queue to point to the next one, and return it |
| 1744 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1745 | static inline struct task *task_sleep(struct task **q, struct task *t) { |
| 1746 | if (t->state == TASK_RUNNING) { |
| 1747 | *q = t->rqnext; |
| 1748 | t->state = TASK_IDLE; /* tell that s has left the run queue */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1749 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1750 | return *q; /* return next running task */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1754 | * removes the task <t> from its wait queue. It must have already been removed |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1755 | * from the run queue. A pointer to the task itself is returned. |
| 1756 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1757 | static inline struct task *task_delete(struct task *t) { |
| 1758 | t->prev->next = t->next; |
| 1759 | t->next->prev = t->prev; |
| 1760 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1764 | * frees a task. Its context must have been freed since it will be lost. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1765 | */ |
| 1766 | static inline void task_free(struct task *t) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1767 | pool_free(task, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1768 | } |
| 1769 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1770 | /* inserts <task> into its assigned wait queue, where it may already be. In this case, it |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1771 | * may be only moved or left where it was, depending on its timing requirements. |
| 1772 | * <task> is returned. |
| 1773 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1774 | struct task *task_queue(struct task *task) { |
| 1775 | struct task *list = task->wq; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1776 | struct task *start_from; |
| 1777 | |
willy tarreau | 5e698ef | 2006-05-02 14:51:00 +0200 | [diff] [blame] | 1778 | /* This is a very dirty hack to queue non-expirable tasks in another queue |
| 1779 | * in order to avoid pulluting the tail of the standard queue. This will go |
| 1780 | * away with the new O(log(n)) scheduler anyway. |
| 1781 | */ |
| 1782 | if (tv_iseternity(&task->expire)) { |
| 1783 | /* if the task was queued in the standard wait queue, we must dequeue it */ |
| 1784 | if (task->prev) { |
| 1785 | if (task->wq == LIST_HEAD(wait_queue[1])) |
| 1786 | return task; |
| 1787 | else { |
| 1788 | task_delete(task); |
| 1789 | task->prev = NULL; |
| 1790 | } |
| 1791 | } |
| 1792 | list = task->wq = LIST_HEAD(wait_queue[1]); |
| 1793 | } else { |
| 1794 | /* if the task was queued in the eternity queue, we must dequeue it */ |
| 1795 | if (task->prev && (task->wq == LIST_HEAD(wait_queue[1]))) { |
| 1796 | task_delete(task); |
| 1797 | task->prev = NULL; |
| 1798 | list = task->wq = LIST_HEAD(wait_queue[0]); |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | /* next, test if the task was already in a list */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1803 | if (task->prev == NULL) { |
| 1804 | // start_from = list; |
| 1805 | start_from = list->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1806 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1807 | stats_tsk_new++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1808 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1809 | /* insert the unlinked <task> into the list, searching back from the last entry */ |
| 1810 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1811 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1812 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1813 | stats_tsk_nsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1814 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | // while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1818 | // start_from = start_from->next; |
| 1819 | // stats_tsk_nsrch++; |
| 1820 | // } |
| 1821 | } |
| 1822 | else if (task->prev == list || |
| 1823 | tv_cmp2(&task->expire, &task->prev->expire) >= 0) { /* walk right */ |
| 1824 | start_from = task->next; |
| 1825 | if (start_from == list || tv_cmp2(&task->expire, &start_from->expire) <= 0) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1826 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1827 | stats_tsk_good++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1828 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1829 | return task; /* it's already in the right place */ |
| 1830 | } |
| 1831 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1832 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1833 | stats_tsk_right++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1834 | #endif |
| 1835 | |
| 1836 | /* if the task is not at the right place, there's little chance that |
| 1837 | * it has only shifted a bit, and it will nearly always be queued |
| 1838 | * at the end of the list because of constant timeouts |
| 1839 | * (observed in real case). |
| 1840 | */ |
| 1841 | #ifndef WE_REALLY_THINK_THAT_THIS_TASK_MAY_HAVE_SHIFTED |
| 1842 | start_from = list->prev; /* assume we'll queue to the end of the list */ |
| 1843 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1844 | start_from = start_from->prev; |
| 1845 | #if STATTIME > 0 |
| 1846 | stats_tsk_lsrch++; |
| 1847 | #endif |
| 1848 | } |
| 1849 | #else /* WE_REALLY_... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1850 | /* insert the unlinked <task> into the list, searching after position <start_from> */ |
| 1851 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1852 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1853 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1854 | stats_tsk_rsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1855 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1856 | } |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1857 | #endif /* WE_REALLY_... */ |
| 1858 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1859 | /* we need to unlink it now */ |
| 1860 | task_delete(task); |
| 1861 | } |
| 1862 | else { /* walk left. */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1863 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1864 | stats_tsk_left++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1865 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1866 | #ifdef LEFT_TO_TOP /* not very good */ |
| 1867 | start_from = list; |
| 1868 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1869 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1870 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1871 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1872 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1873 | } |
| 1874 | #else |
| 1875 | start_from = task->prev->prev; /* valid because of the previous test above */ |
| 1876 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1877 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1878 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1879 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1880 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1881 | } |
| 1882 | #endif |
| 1883 | /* we need to unlink it now */ |
| 1884 | task_delete(task); |
| 1885 | } |
| 1886 | task->prev = start_from; |
| 1887 | task->next = start_from->next; |
| 1888 | task->next->prev = task; |
| 1889 | start_from->next = task; |
| 1890 | return task; |
| 1891 | } |
| 1892 | |
| 1893 | |
| 1894 | /*********************************************************************/ |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 1895 | /* pending connections queues **************************************/ |
| 1896 | /*********************************************************************/ |
| 1897 | |
| 1898 | /* |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1899 | * Detaches pending connection <p>, decreases the pending count, and frees |
| 1900 | * the pending connection. The connection might have been queued to a specific |
| 1901 | * server as well as to the proxy. The session also gets marked unqueued. |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 1902 | */ |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1903 | static void pendconn_free(struct pendconn *p) { |
| 1904 | LIST_DEL(&p->list); |
| 1905 | p->sess->pend_pos = NULL; |
| 1906 | if (p->srv) |
| 1907 | p->srv->nbpend--; |
| 1908 | else |
| 1909 | p->sess->proxy->nbpend--; |
willy tarreau | f32f524 | 2006-05-02 22:54:52 +0200 | [diff] [blame] | 1910 | p->sess->proxy->totpend--; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1911 | pool_free(pendconn, p); |
| 1912 | } |
| 1913 | |
| 1914 | /* Returns the first pending connection for server <s>, which may be NULL if |
| 1915 | * nothing is pending. |
| 1916 | */ |
| 1917 | static inline struct pendconn *pendconn_from_srv(struct server *s) { |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 1918 | if (!s->nbpend) |
| 1919 | return NULL; |
| 1920 | |
| 1921 | return LIST_ELEM(s->pendconns.n, struct pendconn *, list); |
| 1922 | } |
| 1923 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1924 | /* Returns the first pending connection for proxy <px>, which may be NULL if |
| 1925 | * nothing is pending. |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 1926 | */ |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1927 | static inline struct pendconn *pendconn_from_px(struct proxy *px) { |
| 1928 | if (!px->nbpend) |
| 1929 | return NULL; |
| 1930 | |
| 1931 | return LIST_ELEM(px->pendconns.n, struct pendconn *, list); |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 1932 | } |
| 1933 | |
willy tarreau | bc2eda6 | 2006-05-04 15:16:23 +0200 | [diff] [blame] | 1934 | /* Detaches the next pending connection from either a server or a proxy, and |
| 1935 | * returns its associated session. If no pending connection is found, NULL is |
| 1936 | * returned. Note that neither <srv> nor <px> can be NULL. |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 1937 | */ |
willy tarreau | bc2eda6 | 2006-05-04 15:16:23 +0200 | [diff] [blame] | 1938 | static struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px) { |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 1939 | struct pendconn *p; |
| 1940 | struct session *sess; |
| 1941 | |
willy tarreau | bc2eda6 | 2006-05-04 15:16:23 +0200 | [diff] [blame] | 1942 | p = pendconn_from_srv(srv); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1943 | if (!p) { |
willy tarreau | bc2eda6 | 2006-05-04 15:16:23 +0200 | [diff] [blame] | 1944 | p = pendconn_from_px(px); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1945 | if (!p) |
| 1946 | return NULL; |
willy tarreau | bc2eda6 | 2006-05-04 15:16:23 +0200 | [diff] [blame] | 1947 | p->sess->srv = srv; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1948 | } |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 1949 | sess = p->sess; |
| 1950 | pendconn_free(p); |
| 1951 | return sess; |
| 1952 | } |
| 1953 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1954 | /* Adds the session <sess> to the pending connection list of server <sess>->srv |
| 1955 | * or to the one of <sess>->proxy if srv is NULL. All counters and back pointers |
| 1956 | * are updated accordingly. Returns NULL if no memory is available, otherwise the |
| 1957 | * pendconn itself. |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 1958 | */ |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1959 | static struct pendconn *pendconn_add(struct session *sess) { |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 1960 | struct pendconn *p; |
| 1961 | |
| 1962 | p = pool_alloc(pendconn); |
| 1963 | if (!p) |
| 1964 | return NULL; |
| 1965 | |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 1966 | sess->pend_pos = p; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1967 | p->sess = sess; |
| 1968 | p->srv = sess->srv; |
| 1969 | if (sess->srv) { |
| 1970 | LIST_ADDQ(&sess->srv->pendconns, &p->list); |
willy tarreau | 5e69b16 | 2006-05-12 19:49:37 +0200 | [diff] [blame] | 1971 | sess->logs.srv_queue_size += sess->srv->nbpend; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1972 | sess->srv->nbpend++; |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 1973 | if (sess->srv->nbpend > sess->srv->nbpend_max) |
| 1974 | sess->srv->nbpend_max = sess->srv->nbpend; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1975 | } else { |
| 1976 | LIST_ADDQ(&sess->proxy->pendconns, &p->list); |
willy tarreau | 5e69b16 | 2006-05-12 19:49:37 +0200 | [diff] [blame] | 1977 | sess->logs.prx_queue_size += sess->proxy->nbpend; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1978 | sess->proxy->nbpend++; |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 1979 | if (sess->proxy->nbpend > sess->proxy->nbpend_max) |
| 1980 | sess->proxy->nbpend_max = sess->proxy->nbpend; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 1981 | } |
willy tarreau | f32f524 | 2006-05-02 22:54:52 +0200 | [diff] [blame] | 1982 | sess->proxy->totpend++; |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 1983 | return p; |
| 1984 | } |
| 1985 | |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 1986 | /* returns 0 if nothing has to be done for server <s> regarding queued connections, |
| 1987 | * and non-zero otherwise. Suited for and if/else usage. |
| 1988 | */ |
| 1989 | static inline int may_dequeue_tasks(struct server *s, struct proxy *p) { |
| 1990 | return (s && (s->nbpend || p->nbpend) && |
| 1991 | s->maxconn && s->cur_sess < s->maxconn && s->queue_mgt); |
| 1992 | } |
| 1993 | |
| 1994 | |
| 1995 | |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 1996 | /*********************************************************************/ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1997 | /* more specific functions ***************************************/ |
| 1998 | /*********************************************************************/ |
| 1999 | |
| 2000 | /* some prototypes */ |
| 2001 | static int maintain_proxies(void); |
| 2002 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2003 | /* This either returns the sockname or the original destination address. Code |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2004 | * inspired from Patrick Schaaf's example of nf_getsockname() implementation. |
| 2005 | */ |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2006 | static int get_original_dst(int fd, struct sockaddr_in *sa, socklen_t *salen) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2007 | #if defined(TPROXY) && defined(SO_ORIGINAL_DST) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2008 | return getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, (void *)sa, salen); |
| 2009 | #else |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2010 | #if defined(TPROXY) && defined(USE_GETSOCKNAME) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2011 | return getsockname(fd, (struct sockaddr *)sa, salen); |
| 2012 | #else |
| 2013 | return -1; |
| 2014 | #endif |
| 2015 | #endif |
| 2016 | } |
| 2017 | |
| 2018 | /* |
| 2019 | * frees the context associated to a session. It must have been removed first. |
| 2020 | */ |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2021 | static void session_free(struct session *s) { |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 2022 | if (s->pend_pos) |
| 2023 | pendconn_free(s->pend_pos); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2024 | if (s->req) |
| 2025 | pool_free(buffer, s->req); |
| 2026 | if (s->rep) |
| 2027 | pool_free(buffer, s->rep); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2028 | |
| 2029 | if (s->rsp_cap != NULL) { |
| 2030 | struct cap_hdr *h; |
| 2031 | for (h = s->proxy->rsp_cap; h; h = h->next) { |
| 2032 | if (s->rsp_cap[h->index] != NULL) |
| 2033 | pool_free_to(h->pool, s->rsp_cap[h->index]); |
| 2034 | } |
| 2035 | pool_free_to(s->proxy->rsp_cap_pool, s->rsp_cap); |
| 2036 | } |
| 2037 | if (s->req_cap != NULL) { |
| 2038 | struct cap_hdr *h; |
| 2039 | for (h = s->proxy->req_cap; h; h = h->next) { |
| 2040 | if (s->req_cap[h->index] != NULL) |
| 2041 | pool_free_to(h->pool, s->req_cap[h->index]); |
| 2042 | } |
| 2043 | pool_free_to(s->proxy->req_cap_pool, s->req_cap); |
| 2044 | } |
| 2045 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2046 | if (s->logs.uri) |
| 2047 | pool_free(requri, s->logs.uri); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2048 | if (s->logs.cli_cookie) |
| 2049 | pool_free(capture, s->logs.cli_cookie); |
| 2050 | if (s->logs.srv_cookie) |
| 2051 | pool_free(capture, s->logs.srv_cookie); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2052 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2053 | pool_free(session, s); |
| 2054 | } |
| 2055 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2056 | |
| 2057 | /* |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 2058 | * This function recounts the number of usable active and backup servers for |
| 2059 | * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck. |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2060 | * This function also recomputes the total active and backup weights. |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 2061 | */ |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2062 | static void recount_servers(struct proxy *px) { |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 2063 | struct server *srv; |
| 2064 | |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2065 | px->srv_act = 0; px->srv_bck = px->tot_wact = px->tot_wbck = 0; |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 2066 | for (srv = px->srv; srv != NULL; srv = srv->next) { |
| 2067 | if (srv->state & SRV_RUNNING) { |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2068 | if (srv->state & SRV_BACKUP) { |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 2069 | px->srv_bck++; |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2070 | px->tot_wbck += srv->eweight + 1; |
| 2071 | } else { |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 2072 | px->srv_act++; |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2073 | px->tot_wact += srv->eweight + 1; |
| 2074 | } |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 2075 | } |
| 2076 | } |
| 2077 | } |
| 2078 | |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2079 | /* This function recomputes the server map for proxy px. It |
| 2080 | * relies on px->tot_wact and px->tot_wbck, so it must be |
| 2081 | * called after recount_servers(). It also expects px->srv_map |
| 2082 | * to be initialized to the largest value needed. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2083 | */ |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2084 | static void recalc_server_map(struct proxy *px) { |
| 2085 | int o, tot, flag; |
| 2086 | struct server *cur, *best; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2087 | |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 2088 | if (px->srv_act) { |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2089 | flag = SRV_RUNNING; |
| 2090 | tot = px->tot_wact; |
| 2091 | } else if (px->srv_bck) { |
| 2092 | flag = SRV_RUNNING | SRV_BACKUP; |
| 2093 | if (px->options & PR_O_USE_ALL_BK) |
| 2094 | tot = px->tot_wbck; |
| 2095 | else |
| 2096 | tot = 1; /* the first server is enough */ |
| 2097 | } else { |
| 2098 | px->srv_map_sz = 0; |
| 2099 | return; |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 2100 | } |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 2101 | |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2102 | /* this algorithm gives priority to the first server, which means that |
| 2103 | * it will respect the declaration order for equivalent weights, and |
| 2104 | * that whatever the weights, the first server called will always be |
| 2105 | * the first declard. This is an important asumption for the backup |
| 2106 | * case, where we want the first server only. |
| 2107 | */ |
| 2108 | for (cur = px->srv; cur; cur = cur->next) |
| 2109 | cur->wscore = 0; |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 2110 | |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2111 | for (o = 0; o < tot; o++) { |
| 2112 | int max = 0; |
| 2113 | best = NULL; |
| 2114 | for (cur = px->srv; cur; cur = cur->next) { |
| 2115 | if ((cur->state & (SRV_RUNNING | SRV_BACKUP)) == flag) { |
| 2116 | int v; |
| 2117 | |
| 2118 | /* If we are forced to return only one server, we don't want to |
| 2119 | * go further, because we would return the wrong one due to |
| 2120 | * divide overflow. |
| 2121 | */ |
| 2122 | if (tot == 1) { |
| 2123 | best = cur; |
| 2124 | break; |
| 2125 | } |
| 2126 | |
| 2127 | cur->wscore += cur->eweight + 1; |
| 2128 | v = (cur->wscore + tot) / tot; /* result between 0 and 3 */ |
| 2129 | if (best == NULL || v > max) { |
| 2130 | max = v; |
| 2131 | best = cur; |
| 2132 | } |
| 2133 | } |
| 2134 | } |
| 2135 | px->srv_map[o] = best; |
| 2136 | best->wscore -= tot; |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 2137 | } |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2138 | px->srv_map_sz = tot; |
| 2139 | } |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 2140 | |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2141 | /* |
willy tarreau | 898db9d | 2006-04-12 20:29:08 +0200 | [diff] [blame] | 2142 | * This function tries to find a running server with free connection slots for |
| 2143 | * the proxy <px> following the round-robin method. |
| 2144 | * If any server is found, it will be returned and px->srv_rr_idx will be updated |
| 2145 | * to point to the next server. If no valid server is found, NULL is returned. |
| 2146 | */ |
| 2147 | static inline struct server *get_server_rr_with_conns(struct proxy *px) { |
| 2148 | int newidx; |
| 2149 | struct server *srv; |
| 2150 | |
| 2151 | if (px->srv_map_sz == 0) |
| 2152 | return NULL; |
| 2153 | |
| 2154 | if (px->srv_rr_idx < 0 || px->srv_rr_idx >= px->srv_map_sz) |
| 2155 | px->srv_rr_idx = 0; |
| 2156 | newidx = px->srv_rr_idx; |
| 2157 | |
| 2158 | do { |
| 2159 | srv = px->srv_map[newidx++]; |
| 2160 | if (!srv->maxconn || srv->cur_sess < srv->maxconn) { |
| 2161 | px->srv_rr_idx = newidx; |
| 2162 | return srv; |
| 2163 | } |
| 2164 | if (newidx == px->srv_map_sz) |
| 2165 | newidx = 0; |
| 2166 | } while (newidx != px->srv_rr_idx); |
| 2167 | |
| 2168 | return NULL; |
| 2169 | } |
| 2170 | |
| 2171 | |
| 2172 | /* |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2173 | * This function tries to find a running server for the proxy <px> following |
willy tarreau | 898db9d | 2006-04-12 20:29:08 +0200 | [diff] [blame] | 2174 | * the round-robin method. |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2175 | * If any server is found, it will be returned and px->srv_rr_idx will be updated |
| 2176 | * to point to the next server. If no valid server is found, NULL is returned. |
| 2177 | */ |
| 2178 | static inline struct server *get_server_rr(struct proxy *px) { |
| 2179 | if (px->srv_map_sz == 0) |
| 2180 | return NULL; |
| 2181 | |
| 2182 | if (px->srv_rr_idx < 0 || px->srv_rr_idx >= px->srv_map_sz) |
| 2183 | px->srv_rr_idx = 0; |
| 2184 | return px->srv_map[px->srv_rr_idx++]; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2185 | } |
| 2186 | |
willy tarreau | 62084d4 | 2006-03-24 18:57:41 +0100 | [diff] [blame] | 2187 | |
| 2188 | /* |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 2189 | * This function tries to find a running server for the proxy <px> following |
| 2190 | * the source hash method. Depending on the number of active/backup servers, |
| 2191 | * it will either look for active servers, or for backup servers. |
| 2192 | * If any server is found, it will be returned. If no valid server is found, |
| 2193 | * NULL is returned. |
| 2194 | */ |
| 2195 | static inline struct server *get_server_sh(struct proxy *px, char *addr, int len) { |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2196 | unsigned int h, l; |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 2197 | |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2198 | if (px->srv_map_sz == 0) |
| 2199 | return NULL; |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 2200 | |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2201 | l = h = 0; |
willy tarreau | cd65535 | 2006-04-29 12:11:46 +0200 | [diff] [blame] | 2202 | if (px->srv_act > 1 || (px->srv_act == 0 && px->srv_bck > 1)) { |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2203 | while ((l + sizeof (int)) <= len) { |
| 2204 | h ^= ntohl(*(unsigned int *)(&addr[l])); |
| 2205 | l += sizeof (int); |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 2206 | } |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2207 | h %= px->srv_map_sz; |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 2208 | } |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 2209 | return px->srv_map[h]; |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | |
| 2213 | /* |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2214 | * This function marks the session as 'assigned' in direct or dispatch modes, |
| 2215 | * or tries to assign one in balance mode, according to the algorithm. It does |
| 2216 | * nothing if the session had already been assigned a server. |
| 2217 | * |
| 2218 | * It may return : |
willy tarreau | 000375f | 2006-05-09 23:15:58 +0200 | [diff] [blame] | 2219 | * SRV_STATUS_OK if everything is OK. s->srv will be valid. |
| 2220 | * SRV_STATUS_NOSRV if no server is available. s->srv = NULL. |
| 2221 | * SRV_STATUS_FULL if all servers are saturated. s->srv = NULL. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2222 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 2223 | * |
| 2224 | * Upon successful return, the session flag SN_ASSIGNED to indicate that it does |
| 2225 | * not need to be called anymore. This usually means that s->srv can be trusted |
| 2226 | * in balance and direct modes. This flag is not cleared, so it's to the caller |
| 2227 | * to clear it if required (eg: redispatch). |
| 2228 | * |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2229 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2230 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2231 | int assign_server(struct session *s) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2232 | #ifdef DEBUG_FULL |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2233 | fprintf(stderr,"assign_server : s=%p\n",s); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2234 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2235 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2236 | if (s->pend_pos) |
| 2237 | return SRV_STATUS_INTERNAL; |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 2238 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2239 | if (!(s->flags & SN_ASSIGNED)) { |
| 2240 | if ((s->proxy->options & PR_O_BALANCE) && !(s->flags & SN_DIRECT)) { |
| 2241 | if (!s->proxy->srv_act && !s->proxy->srv_bck) |
| 2242 | return SRV_STATUS_NOSRV; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2243 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2244 | if (s->proxy->options & PR_O_BALANCE_RR) { |
| 2245 | s->srv = get_server_rr_with_conns(s->proxy); |
| 2246 | if (!s->srv) |
| 2247 | return SRV_STATUS_FULL; |
| 2248 | } |
| 2249 | else if (s->proxy->options & PR_O_BALANCE_SH) { |
| 2250 | int len; |
| 2251 | |
| 2252 | if (s->cli_addr.ss_family == AF_INET) |
| 2253 | len = 4; |
| 2254 | else if (s->cli_addr.ss_family == AF_INET6) |
| 2255 | len = 16; |
| 2256 | else /* unknown IP family */ |
| 2257 | return SRV_STATUS_INTERNAL; |
| 2258 | |
| 2259 | s->srv = get_server_sh(s->proxy, |
| 2260 | (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 2261 | len); |
| 2262 | } |
| 2263 | else /* unknown balancing algorithm */ |
| 2264 | return SRV_STATUS_INTERNAL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2265 | } |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2266 | s->flags |= SN_ASSIGNED; |
| 2267 | } |
| 2268 | return SRV_STATUS_OK; |
| 2269 | } |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 2270 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2271 | /* |
| 2272 | * This function assigns a server address to a session, and sets SN_ADDR_SET. |
| 2273 | * The address is taken from the currently assigned server, or from the |
| 2274 | * dispatch or transparent address. |
| 2275 | * |
| 2276 | * It may return : |
| 2277 | * SRV_STATUS_OK if everything is OK. |
| 2278 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 2279 | * |
| 2280 | * Upon successful return, the session flag SN_ADDR_SET is set. This flag is |
| 2281 | * not cleared, so it's to the caller to clear it if required. |
| 2282 | * |
| 2283 | */ |
| 2284 | int assign_server_address(struct session *s) { |
| 2285 | #ifdef DEBUG_FULL |
| 2286 | fprintf(stderr,"assign_server_address : s=%p\n",s); |
| 2287 | #endif |
| 2288 | |
| 2289 | if (s->flags & SN_DIRECT || s->proxy->options & PR_O_BALANCE) { |
| 2290 | /* A server is necessarily known for this session */ |
| 2291 | if (!(s->flags & SN_ASSIGNED)) |
| 2292 | return SRV_STATUS_INTERNAL; |
| 2293 | |
| 2294 | s->srv_addr = s->srv->addr; |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 2295 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2296 | /* if this server remaps proxied ports, we'll use |
| 2297 | * the port the client connected to with an offset. */ |
| 2298 | if (s->srv->state & SRV_MAPPORTS) { |
| 2299 | struct sockaddr_in sockname; |
| 2300 | socklen_t namelen = sizeof(sockname); |
| 2301 | |
| 2302 | if (!(s->proxy->options & PR_O_TRANSP) || |
| 2303 | get_original_dst(s->cli_fd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
| 2304 | getsockname(s->cli_fd, (struct sockaddr *)&sockname, &namelen); |
| 2305 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + ntohs(sockname.sin_port)); |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 2306 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2307 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2308 | else if (*(int *)&s->proxy->dispatch_addr.sin_addr) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2309 | /* connect to the defined dispatch addr */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2310 | s->srv_addr = s->proxy->dispatch_addr; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2311 | } |
| 2312 | else if (s->proxy->options & PR_O_TRANSP) { |
| 2313 | /* in transparent mode, use the original dest addr if no dispatch specified */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2314 | socklen_t salen = sizeof(s->srv_addr); |
| 2315 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2316 | if (get_original_dst(s->cli_fd, &s->srv_addr, &salen) == -1) { |
| 2317 | qfprintf(stderr, "Cannot get original server address.\n"); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2318 | return SRV_STATUS_INTERNAL; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2319 | } |
| 2320 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2321 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2322 | s->flags |= SN_ADDR_SET; |
| 2323 | return SRV_STATUS_OK; |
| 2324 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 2325 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2326 | /* This function assigns a server to session <s> if required, and can add the |
| 2327 | * connection to either the assigned server's queue or to the proxy's queue. |
| 2328 | * |
| 2329 | * Returns : |
| 2330 | * |
| 2331 | * SRV_STATUS_OK if everything is OK. |
willy tarreau | 000375f | 2006-05-09 23:15:58 +0200 | [diff] [blame] | 2332 | * SRV_STATUS_NOSRV if no server is available. s->srv = NULL. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2333 | * SRV_STATUS_QUEUED if the connection has been queued. |
| 2334 | * SRV_STATUS_FULL if the server(s) is/are saturated and the |
| 2335 | * connection could not be queued. |
| 2336 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 2337 | * |
| 2338 | */ |
| 2339 | int assign_server_and_queue(struct session *s) { |
| 2340 | struct pendconn *p; |
| 2341 | int err; |
| 2342 | |
| 2343 | if (s->pend_pos) |
| 2344 | return SRV_STATUS_INTERNAL; |
| 2345 | |
| 2346 | if (s->flags & SN_ASSIGNED) { |
| 2347 | /* a server does not need to be assigned, perhaps because we're in |
| 2348 | * direct mode, or in dispatch or transparent modes where the server |
| 2349 | * is not needed. |
| 2350 | */ |
| 2351 | if (s->srv && |
| 2352 | s->srv->maxconn && s->srv->cur_sess >= s->srv->maxconn) { |
| 2353 | p = pendconn_add(s); |
| 2354 | if (p) |
| 2355 | return SRV_STATUS_QUEUED; |
| 2356 | else |
| 2357 | return SRV_STATUS_FULL; |
| 2358 | } |
| 2359 | return SRV_STATUS_OK; |
| 2360 | } |
| 2361 | |
| 2362 | /* a server needs to be assigned */ |
| 2363 | err = assign_server(s); |
| 2364 | switch (err) { |
| 2365 | case SRV_STATUS_OK: |
| 2366 | /* in balance mode, we might have servers with connection limits */ |
| 2367 | if (s->srv != NULL && |
| 2368 | s->srv->maxconn && s->srv->cur_sess >= s->srv->maxconn) { |
| 2369 | p = pendconn_add(s); |
| 2370 | if (p) |
| 2371 | return SRV_STATUS_QUEUED; |
| 2372 | else |
| 2373 | return SRV_STATUS_FULL; |
| 2374 | } |
| 2375 | return SRV_STATUS_OK; |
| 2376 | |
| 2377 | case SRV_STATUS_FULL: |
| 2378 | /* queue this session into the proxy's queue */ |
| 2379 | p = pendconn_add(s); |
| 2380 | if (p) |
| 2381 | return SRV_STATUS_QUEUED; |
| 2382 | else |
| 2383 | return SRV_STATUS_FULL; |
| 2384 | |
| 2385 | case SRV_STATUS_NOSRV: |
| 2386 | case SRV_STATUS_INTERNAL: |
| 2387 | return err; |
| 2388 | default: |
| 2389 | return SRV_STATUS_INTERNAL; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 2390 | } |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 2391 | } |
| 2392 | |
| 2393 | |
| 2394 | /* |
| 2395 | * This function initiates a connection to the server assigned to this session |
| 2396 | * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. |
| 2397 | * It can return one of : |
| 2398 | * - SN_ERR_NONE if everything's OK |
| 2399 | * - SN_ERR_SRVTO if there are no more servers |
| 2400 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 2401 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 2402 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 2403 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 2404 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 2405 | */ |
| 2406 | int connect_server(struct session *s) { |
| 2407 | int fd, err; |
| 2408 | |
| 2409 | if (!(s->flags & SN_ADDR_SET)) { |
| 2410 | err = assign_server_address(s); |
| 2411 | if (err != SRV_STATUS_OK) |
| 2412 | return SN_ERR_INTERNAL; |
| 2413 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 2414 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2415 | if ((fd = s->srv_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2416 | qfprintf(stderr, "Cannot get a server socket.\n"); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2417 | |
| 2418 | if (errno == ENFILE) |
| 2419 | send_log(s->proxy, LOG_EMERG, |
| 2420 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 2421 | s->proxy->id, maxfd); |
| 2422 | else if (errno == EMFILE) |
| 2423 | send_log(s->proxy, LOG_EMERG, |
| 2424 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 2425 | s->proxy->id, maxfd); |
| 2426 | else if (errno == ENOBUFS || errno == ENOMEM) |
| 2427 | send_log(s->proxy, LOG_EMERG, |
| 2428 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 2429 | s->proxy->id, maxfd); |
| 2430 | /* this is a resource error */ |
| 2431 | return SN_ERR_RESOURCE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2432 | } |
| 2433 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2434 | if (fd >= global.maxsock) { |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2435 | /* do not log anything there, it's a normal condition when this option |
| 2436 | * is used to serialize connections to a server ! |
| 2437 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2438 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 2439 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2440 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2441 | } |
| 2442 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2443 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 2444 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2445 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2446 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2447 | return SN_ERR_INTERNAL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2448 | } |
| 2449 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2450 | if (s->proxy->options & PR_O_TCP_SRV_KA) |
| 2451 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 2452 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 2453 | /* allow specific binding : |
| 2454 | * - server-specific at first |
| 2455 | * - proxy-specific next |
| 2456 | */ |
| 2457 | if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) { |
| 2458 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 2459 | if (bind(fd, (struct sockaddr *)&s->srv->source_addr, sizeof(s->srv->source_addr)) == -1) { |
| 2460 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 2461 | s->proxy->id, s->srv->id); |
| 2462 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2463 | send_log(s->proxy, LOG_EMERG, |
| 2464 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 2465 | s->proxy->id, s->srv->id); |
| 2466 | return SN_ERR_RESOURCE; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 2467 | } |
| 2468 | } |
| 2469 | else if (s->proxy->options & PR_O_BIND_SRC) { |
| 2470 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 2471 | if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) { |
| 2472 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", s->proxy->id); |
| 2473 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2474 | send_log(s->proxy, LOG_EMERG, |
| 2475 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 2476 | s->proxy->id, s->srv->id); |
| 2477 | return SN_ERR_RESOURCE; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 2478 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2479 | } |
| 2480 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2481 | if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) && |
| 2482 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 2483 | |
| 2484 | if (errno == EAGAIN || errno == EADDRINUSE) { |
| 2485 | char *msg; |
| 2486 | if (errno == EAGAIN) /* no free ports left, try again later */ |
| 2487 | msg = "no free ports"; |
| 2488 | else |
| 2489 | msg = "local address already in use"; |
| 2490 | |
| 2491 | qfprintf(stderr,"Cannot connect: %s.\n",msg); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2492 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2493 | send_log(s->proxy, LOG_EMERG, |
| 2494 | "Connect() failed for server %s/%s: %s.\n", |
| 2495 | s->proxy->id, s->srv->id, msg); |
| 2496 | return SN_ERR_RESOURCE; |
| 2497 | } else if (errno == ETIMEDOUT) { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2498 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2499 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2500 | return SN_ERR_SRVTO; |
| 2501 | } else { |
| 2502 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2503 | //qfprintf(stderr,"Connect(): %d", errno); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2504 | close(fd); |
| 2505 | return SN_ERR_SRVCL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2506 | } |
| 2507 | } |
| 2508 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2509 | fdtab[fd].owner = s->task; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2510 | fdtab[fd].read = &event_srv_read; |
| 2511 | fdtab[fd].write = &event_srv_write; |
| 2512 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 2513 | |
| 2514 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 2515 | #if defined(DEBUG_FULL) && defined(ENABLE_EPOLL) |
| 2516 | if (PrevReadEvent) { |
| 2517 | assert(!(FD_ISSET(fd, PrevReadEvent))); |
| 2518 | assert(!(FD_ISSET(fd, PrevWriteEvent))); |
| 2519 | } |
| 2520 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2521 | |
| 2522 | fd_insert(fd); |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 2523 | if (s->srv) { |
willy tarreau | 926a357 | 2006-05-01 15:26:35 +0200 | [diff] [blame] | 2524 | s->srv->cur_sess++; |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 2525 | if (s->srv->cur_sess > s->srv->cur_sess_max) |
| 2526 | s->srv->cur_sess_max = s->srv->cur_sess; |
| 2527 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2528 | |
| 2529 | if (s->proxy->contimeout) |
| 2530 | tv_delayfrom(&s->cnexpire, &now, s->proxy->contimeout); |
| 2531 | else |
| 2532 | tv_eternity(&s->cnexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2533 | return SN_ERR_NONE; /* connection is OK */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | /* |
| 2537 | * this function is called on a read event from a client socket. |
| 2538 | * It returns 0. |
| 2539 | */ |
| 2540 | int event_cli_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2541 | struct task *t = fdtab[fd].owner; |
| 2542 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2543 | struct buffer *b = s->req; |
| 2544 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2545 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2546 | #ifdef DEBUG_FULL |
| 2547 | fprintf(stderr,"event_cli_read : fd=%d, s=%p\n", fd, s); |
| 2548 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2549 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2550 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2551 | #ifdef FILL_BUFFERS |
| 2552 | while (1) |
| 2553 | #else |
| 2554 | do |
| 2555 | #endif |
| 2556 | { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2557 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 2558 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2559 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2560 | } |
| 2561 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2562 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2563 | } |
| 2564 | else { |
| 2565 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2566 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 2567 | * since it means that the rewrite protection has been removed. This |
| 2568 | * implies that the if statement can be removed. |
| 2569 | */ |
| 2570 | if (max > b->rlim - b->data) |
| 2571 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | if (max == 0) { /* not anymore room to store data */ |
| 2575 | FD_CLR(fd, StaticReadEvent); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2576 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2577 | } |
| 2578 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2579 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2580 | { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2581 | int skerr; |
| 2582 | socklen_t lskerr = sizeof(skerr); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2583 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2584 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2585 | if (skerr) |
| 2586 | ret = -1; |
| 2587 | else |
| 2588 | ret = recv(fd, b->r, max, 0); |
| 2589 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2590 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2591 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2592 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2593 | if (ret > 0) { |
| 2594 | b->r += ret; |
| 2595 | b->l += ret; |
| 2596 | s->res_cr = RES_DATA; |
| 2597 | |
| 2598 | if (b->r == b->data + BUFSIZE) { |
| 2599 | b->r = b->data; /* wrap around the buffer */ |
| 2600 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2601 | |
| 2602 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2603 | /* we hope to read more data or to get a close on next round */ |
| 2604 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2605 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2606 | else if (ret == 0) { |
| 2607 | s->res_cr = RES_NULL; |
| 2608 | break; |
| 2609 | } |
| 2610 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 2611 | break; |
| 2612 | } |
| 2613 | else { |
| 2614 | s->res_cr = RES_ERROR; |
| 2615 | fdtab[fd].state = FD_STERROR; |
| 2616 | break; |
| 2617 | } |
| 2618 | } /* while(1) */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2619 | #ifndef FILL_BUFFERS |
| 2620 | while (0); |
| 2621 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2622 | } |
| 2623 | else { |
| 2624 | s->res_cr = RES_ERROR; |
| 2625 | fdtab[fd].state = FD_STERROR; |
| 2626 | } |
| 2627 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2628 | if (s->res_cr != RES_SILENT) { |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2629 | if (s->proxy->clitimeout && FD_ISSET(fd, StaticReadEvent)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2630 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 2631 | else |
| 2632 | tv_eternity(&s->crexpire); |
| 2633 | |
| 2634 | task_wakeup(&rq, t); |
| 2635 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2636 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2637 | return 0; |
| 2638 | } |
| 2639 | |
| 2640 | |
| 2641 | /* |
| 2642 | * this function is called on a read event from a server socket. |
| 2643 | * It returns 0. |
| 2644 | */ |
| 2645 | int event_srv_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2646 | struct task *t = fdtab[fd].owner; |
| 2647 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2648 | struct buffer *b = s->rep; |
| 2649 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2650 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2651 | #ifdef DEBUG_FULL |
| 2652 | fprintf(stderr,"event_srv_read : fd=%d, s=%p\n", fd, s); |
| 2653 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2654 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2655 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2656 | #ifdef FILL_BUFFERS |
| 2657 | while (1) |
| 2658 | #else |
| 2659 | do |
| 2660 | #endif |
| 2661 | { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2662 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 2663 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2664 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2665 | } |
| 2666 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2667 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2668 | } |
| 2669 | else { |
| 2670 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2671 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 2672 | * since it means that the rewrite protection has been removed. This |
| 2673 | * implies that the if statement can be removed. |
| 2674 | */ |
| 2675 | if (max > b->rlim - b->data) |
| 2676 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | if (max == 0) { /* not anymore room to store data */ |
| 2680 | FD_CLR(fd, StaticReadEvent); |
| 2681 | break; |
| 2682 | } |
| 2683 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2684 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2685 | { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2686 | int skerr; |
| 2687 | socklen_t lskerr = sizeof(skerr); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2688 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2689 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2690 | if (skerr) |
| 2691 | ret = -1; |
| 2692 | else |
| 2693 | ret = recv(fd, b->r, max, 0); |
| 2694 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2695 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2696 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2697 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2698 | if (ret > 0) { |
| 2699 | b->r += ret; |
| 2700 | b->l += ret; |
| 2701 | s->res_sr = RES_DATA; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2702 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2703 | if (b->r == b->data + BUFSIZE) { |
| 2704 | b->r = b->data; /* wrap around the buffer */ |
| 2705 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2706 | |
| 2707 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2708 | /* we hope to read more data or to get a close on next round */ |
| 2709 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2710 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2711 | else if (ret == 0) { |
| 2712 | s->res_sr = RES_NULL; |
| 2713 | break; |
| 2714 | } |
| 2715 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 2716 | break; |
| 2717 | } |
| 2718 | else { |
| 2719 | s->res_sr = RES_ERROR; |
| 2720 | fdtab[fd].state = FD_STERROR; |
| 2721 | break; |
| 2722 | } |
| 2723 | } /* while(1) */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2724 | #ifndef FILL_BUFFERS |
| 2725 | while (0); |
| 2726 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2727 | } |
| 2728 | else { |
| 2729 | s->res_sr = RES_ERROR; |
| 2730 | fdtab[fd].state = FD_STERROR; |
| 2731 | } |
| 2732 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2733 | if (s->res_sr != RES_SILENT) { |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2734 | if (s->proxy->srvtimeout && FD_ISSET(fd, StaticReadEvent)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2735 | tv_delayfrom(&s->srexpire, &now, s->proxy->srvtimeout); |
| 2736 | else |
| 2737 | tv_eternity(&s->srexpire); |
| 2738 | |
| 2739 | task_wakeup(&rq, t); |
| 2740 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2741 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2742 | return 0; |
| 2743 | } |
| 2744 | |
| 2745 | /* |
| 2746 | * this function is called on a write event from a client socket. |
| 2747 | * It returns 0. |
| 2748 | */ |
| 2749 | int event_cli_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2750 | struct task *t = fdtab[fd].owner; |
| 2751 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2752 | struct buffer *b = s->rep; |
| 2753 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2754 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2755 | #ifdef DEBUG_FULL |
| 2756 | fprintf(stderr,"event_cli_write : fd=%d, s=%p\n", fd, s); |
| 2757 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2758 | |
| 2759 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 2760 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2761 | // max = BUFSIZE; BUG !!!! |
| 2762 | max = 0; |
| 2763 | } |
| 2764 | else if (b->r > b->w) { |
| 2765 | max = b->r - b->w; |
| 2766 | } |
| 2767 | else |
| 2768 | max = b->data + BUFSIZE - b->w; |
| 2769 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2770 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2771 | if (max == 0) { |
| 2772 | s->res_cw = RES_NULL; |
| 2773 | task_wakeup(&rq, t); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2774 | tv_eternity(&s->cwexpire); |
| 2775 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2776 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2777 | } |
| 2778 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2779 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2780 | { |
| 2781 | int skerr; |
| 2782 | socklen_t lskerr = sizeof(skerr); |
| 2783 | |
| 2784 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2785 | if (skerr) |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2786 | ret = -1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2787 | else |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2788 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2789 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2790 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2791 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2792 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2793 | |
| 2794 | if (ret > 0) { |
| 2795 | b->l -= ret; |
| 2796 | b->w += ret; |
| 2797 | |
| 2798 | s->res_cw = RES_DATA; |
| 2799 | |
| 2800 | if (b->w == b->data + BUFSIZE) { |
| 2801 | b->w = b->data; /* wrap around the buffer */ |
| 2802 | } |
| 2803 | } |
| 2804 | else if (ret == 0) { |
| 2805 | /* nothing written, just make as if we were never called */ |
| 2806 | // s->res_cw = RES_NULL; |
| 2807 | return 0; |
| 2808 | } |
| 2809 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 2810 | return 0; |
| 2811 | else { |
| 2812 | s->res_cw = RES_ERROR; |
| 2813 | fdtab[fd].state = FD_STERROR; |
| 2814 | } |
| 2815 | } |
| 2816 | else { |
| 2817 | s->res_cw = RES_ERROR; |
| 2818 | fdtab[fd].state = FD_STERROR; |
| 2819 | } |
| 2820 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2821 | if (s->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2822 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
willy tarreau | 0889c96 | 2006-04-24 14:36:48 +0200 | [diff] [blame] | 2823 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 2824 | * we refresh it. A solution would be to merge read+write timeouts into a |
| 2825 | * unique one, although that needs some study particularly on full-duplex |
| 2826 | * TCP connections. */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2827 | s->crexpire = s->cwexpire; |
| 2828 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2829 | else |
| 2830 | tv_eternity(&s->cwexpire); |
| 2831 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2832 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2833 | return 0; |
| 2834 | } |
| 2835 | |
| 2836 | |
| 2837 | /* |
| 2838 | * this function is called on a write event from a server socket. |
| 2839 | * It returns 0. |
| 2840 | */ |
| 2841 | int event_srv_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2842 | struct task *t = fdtab[fd].owner; |
| 2843 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2844 | struct buffer *b = s->req; |
| 2845 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2846 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2847 | #ifdef DEBUG_FULL |
| 2848 | fprintf(stderr,"event_srv_write : fd=%d, s=%p\n", fd, s); |
| 2849 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2850 | |
| 2851 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 2852 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2853 | // max = BUFSIZE; BUG !!!! |
| 2854 | max = 0; |
| 2855 | } |
| 2856 | else if (b->r > b->w) { |
| 2857 | max = b->r - b->w; |
| 2858 | } |
| 2859 | else |
| 2860 | max = b->data + BUFSIZE - b->w; |
| 2861 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2862 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2863 | if (max == 0) { |
| 2864 | /* may be we have received a connection acknowledgement in TCP mode without data */ |
willy tarreau | 48b0659 | 2005-12-18 01:37:12 +0100 | [diff] [blame] | 2865 | if (s->srv_state == SV_STCONN) { |
| 2866 | int skerr; |
| 2867 | socklen_t lskerr = sizeof(skerr); |
| 2868 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2869 | if (skerr) { |
| 2870 | s->res_sw = RES_ERROR; |
| 2871 | fdtab[fd].state = FD_STERROR; |
| 2872 | task_wakeup(&rq, t); |
| 2873 | tv_eternity(&s->swexpire); |
| 2874 | FD_CLR(fd, StaticWriteEvent); |
| 2875 | return 0; |
| 2876 | } |
| 2877 | } |
| 2878 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2879 | s->res_sw = RES_NULL; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2880 | task_wakeup(&rq, t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2881 | fdtab[fd].state = FD_STREADY; |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2882 | tv_eternity(&s->swexpire); |
| 2883 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2884 | return 0; |
| 2885 | } |
| 2886 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2887 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2888 | { |
| 2889 | int skerr; |
| 2890 | socklen_t lskerr = sizeof(skerr); |
| 2891 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2892 | if (skerr) |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2893 | ret = -1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2894 | else |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2895 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2896 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2897 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2898 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2899 | #endif |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2900 | fdtab[fd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2901 | if (ret > 0) { |
| 2902 | b->l -= ret; |
| 2903 | b->w += ret; |
| 2904 | |
| 2905 | s->res_sw = RES_DATA; |
| 2906 | |
| 2907 | if (b->w == b->data + BUFSIZE) { |
| 2908 | b->w = b->data; /* wrap around the buffer */ |
| 2909 | } |
| 2910 | } |
| 2911 | else if (ret == 0) { |
| 2912 | /* nothing written, just make as if we were never called */ |
| 2913 | // s->res_sw = RES_NULL; |
| 2914 | return 0; |
| 2915 | } |
| 2916 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 2917 | return 0; |
| 2918 | else { |
| 2919 | s->res_sw = RES_ERROR; |
| 2920 | fdtab[fd].state = FD_STERROR; |
| 2921 | } |
| 2922 | } |
| 2923 | else { |
| 2924 | s->res_sw = RES_ERROR; |
| 2925 | fdtab[fd].state = FD_STERROR; |
| 2926 | } |
| 2927 | |
Willy TARREAU | 1cec83c | 2006-03-01 22:33:49 +0100 | [diff] [blame] | 2928 | /* We don't want to re-arm read/write timeouts if we're trying to connect, |
| 2929 | * otherwise it could loop indefinitely ! |
| 2930 | */ |
| 2931 | if (s->srv_state != SV_STCONN) { |
| 2932 | if (s->proxy->srvtimeout) { |
| 2933 | tv_delayfrom(&s->swexpire, &now, s->proxy->srvtimeout); |
willy tarreau | 0889c96 | 2006-04-24 14:36:48 +0200 | [diff] [blame] | 2934 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2935 | * we refresh it. A solution would be to merge read+write+connect timeouts |
| 2936 | * into a unique one since we don't mind expiring on read or write, and none |
| 2937 | * of them is enabled while waiting for connect(), although that needs some |
| 2938 | * study particularly on full-duplex TCP connections. */ |
Willy TARREAU | 1cec83c | 2006-03-01 22:33:49 +0100 | [diff] [blame] | 2939 | s->srexpire = s->swexpire; |
| 2940 | } |
| 2941 | else |
| 2942 | tv_eternity(&s->swexpire); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2943 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2944 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2945 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2946 | return 0; |
| 2947 | } |
| 2948 | |
| 2949 | |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 2950 | /* returns 1 if the buffer is empty, 0 otherwise */ |
| 2951 | static inline int buffer_isempty(struct buffer *buf) { |
| 2952 | return buf->l == 0; |
| 2953 | } |
| 2954 | |
| 2955 | |
| 2956 | /* returns 1 if the buffer is full, 0 otherwise */ |
| 2957 | static inline int buffer_isfull(struct buffer *buf) { |
| 2958 | return buf->l == BUFSIZE; |
| 2959 | } |
| 2960 | |
| 2961 | |
| 2962 | /* flushes any content from buffer <buf> */ |
| 2963 | void buffer_flush(struct buffer *buf) { |
| 2964 | buf->r = buf->h = buf->lr = buf->w = buf->data; |
| 2965 | buf->l = 0; |
| 2966 | } |
| 2967 | |
| 2968 | |
| 2969 | /* returns the maximum number of bytes writable at once in this buffer */ |
| 2970 | int buffer_max(struct buffer *buf) { |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 2971 | if (buf->l == BUFSIZE) |
| 2972 | return 0; |
| 2973 | else if (buf->r >= buf->w) |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 2974 | return buf->data + BUFSIZE - buf->r; |
| 2975 | else |
| 2976 | return buf->w - buf->r; |
| 2977 | } |
| 2978 | |
| 2979 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2980 | /* |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 2981 | * Tries to realign the given buffer, and returns how many bytes can be written |
| 2982 | * there at once without overwriting anything. |
| 2983 | */ |
| 2984 | int buffer_realign(struct buffer *buf) { |
| 2985 | if (buf->l == 0) { |
| 2986 | /* let's realign the buffer to optimize I/O */ |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 2987 | buf->r = buf->w = buf->h = buf->lr = buf->data; |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 2988 | } |
| 2989 | return buffer_max(buf); |
| 2990 | } |
| 2991 | |
| 2992 | |
| 2993 | /* writes <len> bytes from message <msg> to buffer <buf>. Returns 0 in case of |
| 2994 | * success, or the number of bytes available otherwise. |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 2995 | * FIXME-20060521: handle unaligned data. |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 2996 | */ |
| 2997 | int buffer_write(struct buffer *buf, const char *msg, int len) { |
| 2998 | int max; |
| 2999 | |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3000 | max = buffer_realign(buf); |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3001 | |
| 3002 | if (len > max) |
| 3003 | return max; |
| 3004 | |
| 3005 | memcpy(buf->r, msg, len); |
| 3006 | buf->l += len; |
| 3007 | buf->r += len; |
| 3008 | if (buf->r == buf->data + BUFSIZE) |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3009 | buf->r = buf->data; |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3010 | return 0; |
| 3011 | } |
| 3012 | |
| 3013 | |
| 3014 | /* |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3015 | * returns a message to the client ; the connection is shut down for read, |
| 3016 | * and the request is cleared so that no server connection can be initiated. |
| 3017 | * The client must be in a valid state for this (HEADER, DATA ...). |
| 3018 | * Nothing is performed on the server side. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3019 | * The reply buffer doesn't need to be empty before this. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3020 | */ |
| 3021 | void client_retnclose(struct session *s, int len, const char *msg) { |
| 3022 | FD_CLR(s->cli_fd, StaticReadEvent); |
| 3023 | FD_SET(s->cli_fd, StaticWriteEvent); |
| 3024 | tv_eternity(&s->crexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3025 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3026 | shutdown(s->cli_fd, SHUT_RD); |
| 3027 | s->cli_state = CL_STSHUTR; |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3028 | buffer_flush(s->rep); |
| 3029 | buffer_write(s->rep, msg, len); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3030 | s->req->l = 0; |
| 3031 | } |
| 3032 | |
| 3033 | |
| 3034 | /* |
| 3035 | * returns a message into the rep buffer, and flushes the req buffer. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3036 | * The reply buffer doesn't need to be empty before this. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3037 | */ |
| 3038 | void client_return(struct session *s, int len, const char *msg) { |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3039 | buffer_flush(s->rep); |
| 3040 | buffer_write(s->rep, msg, len); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3041 | s->req->l = 0; |
| 3042 | } |
| 3043 | |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3044 | /* |
| 3045 | * Produces data for the session <s> depending on its source. Expects to be |
| 3046 | * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be |
| 3047 | * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
| 3048 | * session, which it uses to keep on being called when there is free space in |
| 3049 | * the buffer, of simply by letting an empty buffer upon return. It returns 1 |
| 3050 | * if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3051 | */ |
| 3052 | int produce_content(struct session *s) { |
| 3053 | struct buffer *rep = s->rep; |
| 3054 | struct proxy *px; |
| 3055 | struct server *sv; |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3056 | int msglen; |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3057 | |
| 3058 | if (s->data_source == DATA_SRC_NONE) { |
| 3059 | s->flags &= ~SN_SELF_GEN; |
| 3060 | return 1; |
| 3061 | } |
| 3062 | else if (s->data_source == DATA_SRC_STATS) { |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3063 | msglen = 0; |
| 3064 | |
| 3065 | if (s->data_state == DATA_ST_INIT) { /* the function had not been called yet */ |
| 3066 | unsigned int up; |
| 3067 | |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3068 | s->flags |= SN_SELF_GEN; // more data will follow |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3069 | |
| 3070 | /* send the start of the HTTP response */ |
| 3071 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3072 | "HTTP/1.0 200 OK\r\n" |
| 3073 | "Cache-Control: no-cache\r\n" |
| 3074 | "Connection: close\r\n" |
| 3075 | "\r\n\r\n"); |
| 3076 | |
| 3077 | s->logs.status = 200; |
| 3078 | client_retnclose(s, msglen, trash); // send the start of the response. |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3079 | msglen = 0; |
| 3080 | |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3081 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3082 | s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy |
| 3083 | if (!(s->flags & SN_FINST_MASK)) |
| 3084 | s->flags |= SN_FINST_R; |
| 3085 | |
| 3086 | /* WARNING! This must fit in the first buffer !!! */ |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3087 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3088 | "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n" |
| 3089 | "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n" |
| 3090 | "<style type=\"text/css\"><!--\n" |
| 3091 | "body {" |
| 3092 | " font-family: helvetica, arial;" |
| 3093 | " font-size: 12px;" |
| 3094 | " font-weight: normal;" |
| 3095 | " color: black;" |
| 3096 | " background: white;" |
| 3097 | "}\n" |
| 3098 | "td {" |
| 3099 | " font-size: 12px;" |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3100 | " align: center;" |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3101 | "}\n" |
| 3102 | "h1 {" |
| 3103 | " font-size: xx-large;" |
| 3104 | " margin-bottom: 0.5em;" |
| 3105 | "}\n" |
| 3106 | "h2 {" |
| 3107 | " font-family: helvetica, arial;" |
| 3108 | " font-size: x-large;" |
| 3109 | " font-weight: bold;" |
| 3110 | " font-style: italic;" |
| 3111 | " color: #6020a0;" |
| 3112 | " margin-top: 0em;" |
| 3113 | " margin-bottom: 0em;" |
| 3114 | "}\n" |
| 3115 | "h3 {" |
| 3116 | " font-family: helvetica, arial;" |
| 3117 | " font-size: 16px;" |
| 3118 | " font-weight: bold;" |
| 3119 | " color: #b00040;" |
| 3120 | " background: #e8e8d0;" |
| 3121 | " margin-top: 0em;" |
| 3122 | " margin-bottom: 0em;" |
| 3123 | "}\n" |
| 3124 | "li {" |
| 3125 | " margin-top: 0.25em;" |
| 3126 | " margin-right: 2em;" |
| 3127 | "}\n" |
| 3128 | ".hr {" |
| 3129 | " margin-top: 0.25em;" |
| 3130 | " border-color: black;" |
| 3131 | " border-bottom-style: solid;" |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3132 | "}\n" |
| 3133 | "table.tbl { border-collapse: collapse; border-width: 1px; border-style: solid; border-color: gray;}\n" |
| 3134 | "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; border-color: gray; }\n" |
| 3135 | "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray; }\n" |
| 3136 | "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n" |
| 3137 | "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n" |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3138 | "-->" |
| 3139 | "</style></head>"); |
| 3140 | |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3141 | if (buffer_write(rep, trash, msglen) != 0) |
| 3142 | return 0; |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3143 | msglen = 0; |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3144 | |
| 3145 | up = (now.tv_sec - start_date.tv_sec); |
| 3146 | |
| 3147 | /* WARNING! this has to fit the first packet too */ |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3148 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3149 | "<body><h1>" PRODUCT_NAME "</h1>\n" |
| 3150 | "<h2>Statistics Report for pid %d</h2>\n" |
| 3151 | "<hr width=\"100%%\" class=\"hr\">\n" |
willy tarreau | 338be83 | 2006-05-21 08:58:06 +0200 | [diff] [blame] | 3152 | "<h3>> General process information</h3>\n" |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3153 | "<table border=0><tr><td align=\"left\">\n" |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3154 | "<p><b>pid = </b> %d (nbproc = %d)<br>\n" |
| 3155 | "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n" |
willy tarreau | 338be83 | 2006-05-21 08:58:06 +0200 | [diff] [blame] | 3156 | "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n" |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3157 | "<b>maxsock = </b> %d<br>\n" |
| 3158 | "<b>maxconn = </b> %d (current conns = %d)<br>\n" |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3159 | "</td><td width=\"10%%\">\n" |
| 3160 | "</td><td align=\"right\">\n" |
| 3161 | "<table class=\"lgd\">" |
| 3162 | "<tr><td bgcolor=\"#C0FFC0\"> </td><td style=\"border-style: none;\">active UP </td>" |
| 3163 | "<td bgcolor=\"#B0D0FF\"> </td><td style=\"border-style: none;\">backup UP </td></tr>" |
| 3164 | "<tr><td bgcolor=\"#FFFFA0\"></td><td style=\"border-style: none;\">active UP, going down </td>" |
| 3165 | "<td bgcolor=\"#C060FF\"></td><td style=\"border-style: none;\">backup UP, going down </td></tr>" |
| 3166 | "<tr><td bgcolor=\"#FFD020\"></td><td style=\"border-style: none;\">active DOWN, going up </td>" |
| 3167 | "<td bgcolor=\"#FF80FF\"></td><td style=\"border-style: none;\">backup DOWN, going up </td></tr>" |
| 3168 | "<tr><td bgcolor=\"#FF9090\"></td><td style=\"border-style: none;\">active or backup DOWN </td>" |
| 3169 | "<td bgcolor=\"#E0E0E0\"></td><td style=\"border-style: none;\">not checked </td></tr>" |
| 3170 | "</table>\n" |
| 3171 | "</tr></table>\n" |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3172 | "", |
| 3173 | pid, pid, global.nbproc, |
| 3174 | up / 86400, (up % 86400) / 3600, |
| 3175 | (up % 3600) / 60, (up % 60), |
willy tarreau | 338be83 | 2006-05-21 08:58:06 +0200 | [diff] [blame] | 3176 | global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited", |
| 3177 | global.rlimit_memmax ? " MB" : "", |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3178 | global.rlimit_nofile, |
| 3179 | global.maxsock, |
| 3180 | global.maxconn, |
| 3181 | actconn |
| 3182 | ); |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3183 | |
| 3184 | if (buffer_write(rep, trash, msglen) != 0) |
| 3185 | return 0; |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3186 | msglen = 0; |
| 3187 | |
| 3188 | s->data_state = DATA_ST_DATA; |
| 3189 | memset(&s->data_ctx, 0, sizeof(s->data_ctx)); |
| 3190 | |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3191 | px = s->data_ctx.stats.px = proxy; |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3192 | s->data_ctx.stats.px_st = DATA_ST_INIT; |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | while (s->data_ctx.stats.px) { |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3196 | int dispatch_sess, dispatch_cum; |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3197 | int failed_checks, down_trans; |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 3198 | int failed_secu, failed_conns, failed_resp; |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3199 | |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3200 | if (s->data_ctx.stats.px_st == DATA_ST_INIT) { |
| 3201 | /* we are on a new proxy */ |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3202 | px = s->data_ctx.stats.px; |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3203 | |
| 3204 | /* skip the disabled proxies */ |
| 3205 | if (px->state == PR_STSTOPPED) |
| 3206 | goto next_proxy; |
| 3207 | |
| 3208 | if (s->proxy->uri_auth && s->proxy->uri_auth->scope) { |
| 3209 | /* we have a limited scope, we have to check the proxy name */ |
| 3210 | struct stat_scope *scope; |
| 3211 | int len; |
| 3212 | |
| 3213 | len = strlen(px->id); |
| 3214 | scope = s->proxy->uri_auth->scope; |
| 3215 | |
| 3216 | while (scope) { |
| 3217 | /* match exact proxy name */ |
| 3218 | if (scope->px_len == len && !memcmp(px->id, scope->px_id, len)) |
| 3219 | break; |
| 3220 | |
| 3221 | /* match '.' which means 'self' proxy */ |
| 3222 | if (!strcmp(scope->px_id, ".") && px == s->proxy) |
| 3223 | break; |
| 3224 | scope = scope->next; |
| 3225 | } |
| 3226 | |
| 3227 | /* proxy name not found */ |
| 3228 | if (scope == NULL) |
| 3229 | goto next_proxy; |
| 3230 | } |
| 3231 | |
| 3232 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3233 | "<h3>> Proxy instance %s : " |
| 3234 | "%d conns (maxconn=%d), %d queued (%d unassigned), %d total conns</h3>\n" |
| 3235 | "", |
| 3236 | px->id, |
| 3237 | px->nbconn, px->maxconn, px->totpend, px->nbpend, px->cum_conn); |
| 3238 | |
| 3239 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 3240 | "<table cols=\"16\" class=\"tbl\">\n" |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3241 | "<tr align=\"center\" bgcolor=\"#20C0C0\">" |
| 3242 | "<th colspan=5>Server</th>" |
| 3243 | "<th colspan=2>Queue</th>" |
| 3244 | "<th colspan=4>Sessions</th>" |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 3245 | "<th colspan=5>Errors</th></tr>\n" |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3246 | "<tr align=\"center\" bgcolor=\"#20C0C0\">" |
| 3247 | "<th>Name</th><th>Weight</th><th>Status</th><th>Act.</th><th>Bck.</th>" |
| 3248 | "<th>Curr.</th><th>Max.</th>" |
| 3249 | "<th>Curr.</th><th>Max.</th><th>Limit</th><th>Cumul.</th>" |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 3250 | "<th>Conn.</th><th>Resp.</th><th>Sec.</th><th>Check</th><th>Down</th></tr>\n"); |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3251 | |
| 3252 | if (buffer_write(rep, trash, msglen) != 0) |
| 3253 | return 0; |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3254 | msglen = 0; |
| 3255 | |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3256 | s->data_ctx.stats.sv = px->srv; |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3257 | s->data_ctx.stats.px_st = DATA_ST_DATA; |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3258 | } |
| 3259 | |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3260 | px = s->data_ctx.stats.px; |
| 3261 | |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3262 | /* stats.sv has been initialized above */ |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3263 | while (s->data_ctx.stats.sv != NULL) { |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3264 | static char *act_tab_bg[5] = { /*down*/"#FF9090", /*rising*/"#FFD020", /*failing*/"#FFFFA0", /*up*/"#C0FFC0", /*unchecked*/"#E0E0E0" }; |
| 3265 | static char *bck_tab_bg[5] = { /*down*/"#FF9090", /*rising*/"#FF80ff", /*failing*/"#C060FF", /*up*/"#B0D0FF", /*unchecked*/"#E0E0E0" }; |
| 3266 | static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d ↑", "UP %d/%d ↓", "UP", "<i>no check</i>" }; |
| 3267 | int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP */ |
| 3268 | |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3269 | sv = s->data_ctx.stats.sv; |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3270 | |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3271 | /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */ |
| 3272 | if (!(sv->state & SRV_CHECKED)) |
| 3273 | sv_state = 4; |
| 3274 | else if (sv->state & SRV_RUNNING) |
| 3275 | if (sv->health == sv->rise + sv->fall - 1) |
| 3276 | sv_state = 3; /* UP */ |
| 3277 | else |
| 3278 | sv_state = 2; /* going down */ |
| 3279 | else |
| 3280 | if (sv->health) |
| 3281 | sv_state = 1; /* going up */ |
| 3282 | else |
| 3283 | sv_state = 0; /* DOWN */ |
| 3284 | |
| 3285 | /* name, weight */ |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3286 | msglen += snprintf(trash, sizeof(trash), |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3287 | "<tr align=center bgcolor=\"%s\"><td>%s</td><td>%d</td><td>", |
| 3288 | (sv->state & SRV_BACKUP) ? bck_tab_bg[sv_state] : act_tab_bg[sv_state], |
| 3289 | sv->id, sv->uweight+1); |
| 3290 | /* status */ |
| 3291 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, srv_hlt_st[sv_state], |
| 3292 | (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health), |
| 3293 | (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise)); |
| 3294 | |
| 3295 | /* act, bck */ |
| 3296 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3297 | "</td><td>%s</td><td>%s</td>", |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3298 | (sv->state & SRV_BACKUP) ? "-" : "Y", |
| 3299 | (sv->state & SRV_BACKUP) ? "Y" : "-"); |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3300 | |
| 3301 | /* queue : current, max */ |
| 3302 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3303 | "<td align=right>%d</td><td align=right>%d</td>", |
| 3304 | sv->nbpend, sv->nbpend_max); |
| 3305 | |
| 3306 | /* sessions : current, max, limit, cumul */ |
| 3307 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3308 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>", |
| 3309 | sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess); |
| 3310 | |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 3311 | /* errors : connect, response, security */ |
| 3312 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3313 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n", |
| 3314 | sv->failed_conns, sv->failed_resp, sv->failed_secu); |
| 3315 | |
| 3316 | /* check failures : unique, fatal */ |
willy tarreau | 338be83 | 2006-05-21 08:58:06 +0200 | [diff] [blame] | 3317 | if (sv->state & SRV_CHECKED) |
| 3318 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3319 | "<td align=right>%d</td><td align=right>%d</td></tr>\n", |
| 3320 | sv->failed_checks, sv->down_trans); |
| 3321 | else |
| 3322 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3323 | "<td align=right>-</td><td align=right>-</td></tr>\n"); |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3324 | |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3325 | if (buffer_write(rep, trash, msglen) != 0) |
| 3326 | return 0; |
| 3327 | msglen = 0; |
| 3328 | |
| 3329 | s->data_ctx.stats.sv = sv->next; |
| 3330 | } /* while sv */ |
| 3331 | |
| 3332 | /* now we are past the last server, we'll dump information about the dispatcher */ |
| 3333 | |
| 3334 | /* We have to count down from the proxy to the servers to tell how |
| 3335 | * many sessions are on the dispatcher, and how many checks have |
| 3336 | * failed. We cannot count this during the servers dump because it |
| 3337 | * might be interrupted multiple times. |
| 3338 | */ |
| 3339 | dispatch_sess = px->nbconn; |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 3340 | dispatch_cum = px->cum_conn; |
| 3341 | failed_secu = px->failed_secu; |
| 3342 | failed_conns = px->failed_conns; |
| 3343 | failed_resp = px->failed_resp; |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3344 | failed_checks = down_trans = 0; |
| 3345 | |
| 3346 | sv = px->srv; |
| 3347 | while (sv) { |
| 3348 | dispatch_sess -= sv->cur_sess; |
| 3349 | dispatch_cum -= sv->cum_sess; |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 3350 | failed_conns -= sv->failed_conns; |
| 3351 | failed_resp -= sv->failed_resp; |
| 3352 | failed_secu -= sv->failed_secu; |
| 3353 | if (sv->state & SRV_CHECKED) { |
| 3354 | failed_checks += sv->failed_checks; |
| 3355 | down_trans += sv->down_trans; |
| 3356 | } |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3357 | sv = sv->next; |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3358 | } |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3359 | |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3360 | /* name, weight, status, act, bck */ |
| 3361 | msglen += snprintf(trash + msglen, sizeof(trash), |
| 3362 | "<tr align=center bgcolor=\"#e8e8d0\">" |
| 3363 | "<td>Dispatcher</td><td>-</td>" |
| 3364 | "<td>%s</td><td>-</td><td>-</td>", |
| 3365 | px->state == PR_STRUN ? "UP" : "DOWN"); |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3366 | |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3367 | /* queue : current, max */ |
| 3368 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3369 | "<td align=right>%d</td><td align=right>%d</td>", |
| 3370 | px->nbpend, px->nbpend_max); |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3371 | |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3372 | /* sessions : current, max, limit, cumul. */ |
| 3373 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3374 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>", |
| 3375 | dispatch_sess, px->nbconn_max, px->maxconn, dispatch_cum); |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3376 | |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 3377 | /* errors : connect, response, security */ |
| 3378 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3379 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n", |
| 3380 | failed_conns, failed_resp, failed_secu); |
| 3381 | |
| 3382 | /* check failures : unique, fatal */ |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3383 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3384 | "<td align=right>-</td><td align=right>-</td></tr>\n"); |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3385 | |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3386 | |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3387 | /* now the summary for the whole proxy */ |
| 3388 | /* name, weight, status, act, bck */ |
| 3389 | msglen += snprintf(trash + msglen, sizeof(trash), |
| 3390 | "<tr align=center style=\"color: #ffff80; background: #20C0C0;\">" |
| 3391 | "<td><b>Total</b></td><td>-</td>" |
| 3392 | "<td><b>%s</b></td><td><b>%d</b></td><td><b>%d</b></td>", |
| 3393 | (px->state == PR_STRUN && ((px->srv == NULL) || px->srv_act || px->srv_bck)) ? "UP" : "DOWN", |
| 3394 | px->srv_act, px->srv_bck); |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3395 | |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3396 | /* queue : current, max */ |
| 3397 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3398 | "<td align=right><b>%d</b></td><td align=right><b>%d</b></td>", |
| 3399 | px->totpend, px->nbpend_max); |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3400 | |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3401 | /* sessions : current, max, limit, cumul */ |
| 3402 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3403 | "<td align=right><b>%d</b></td><td align=right><b>%d</b></td><td align=right><b>%d</b></td><td align=right><b>%d</b></td>", |
| 3404 | px->nbconn, px->nbconn_max, px->maxconn, px->cum_conn); |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3405 | |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 3406 | /* errors : connect, response, security */ |
| 3407 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3408 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n", |
| 3409 | px->failed_conns, px->failed_resp, px->failed_secu); |
| 3410 | |
| 3411 | /* check failures : unique, fatal */ |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3412 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, |
| 3413 | "<td align=right>%d</td><td align=right>%d</td></tr>\n", |
| 3414 | failed_checks, down_trans); |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3415 | |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 3416 | msglen += snprintf(trash + msglen, sizeof(trash) - msglen, "</table><p>\n"); |
| 3417 | |
| 3418 | if (buffer_write(rep, trash, msglen) != 0) |
| 3419 | return 0; |
| 3420 | msglen = 0; |
| 3421 | |
| 3422 | s->data_ctx.stats.px_st = DATA_ST_INIT; |
| 3423 | next_proxy: |
| 3424 | s->data_ctx.stats.px = px->next; |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3425 | } /* proxy loop */ |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3426 | /* here, we just have reached the sv == NULL and px == NULL */ |
| 3427 | s->flags &= ~SN_SELF_GEN; |
| 3428 | return 1; |
| 3429 | } |
| 3430 | else { |
| 3431 | /* unknown data source */ |
| 3432 | s->logs.status = 500; |
| 3433 | client_retnclose(s, s->proxy->errmsg.len500, s->proxy->errmsg.msg500); |
| 3434 | if (!(s->flags & SN_ERR_MASK)) |
| 3435 | s->flags |= SN_ERR_PRXCOND; |
| 3436 | if (!(s->flags & SN_FINST_MASK)) |
| 3437 | s->flags |= SN_FINST_R; |
| 3438 | s->flags &= SN_SELF_GEN; |
| 3439 | return 1; |
| 3440 | } |
| 3441 | } |
| 3442 | |
| 3443 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3444 | /* |
| 3445 | * send a log for the session when we have enough info about it |
| 3446 | */ |
| 3447 | void sess_log(struct session *s) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3448 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3449 | struct proxy *p = s->proxy; |
| 3450 | int log; |
| 3451 | char *uri; |
| 3452 | char *pxid; |
| 3453 | char *srv; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 3454 | struct tm *tm; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3455 | |
| 3456 | /* This is a first attempt at a better logging system. |
| 3457 | * For now, we rely on send_log() to provide the date, although it obviously |
| 3458 | * is the date of the log and not of the request, and most fields are not |
| 3459 | * computed. |
| 3460 | */ |
| 3461 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3462 | log = p->to_log & ~s->logs.logwait; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3463 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3464 | if (s->cli_addr.ss_family == AF_INET) |
| 3465 | inet_ntop(AF_INET, |
| 3466 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 3467 | pn, sizeof(pn)); |
| 3468 | else |
| 3469 | inet_ntop(AF_INET6, |
| 3470 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 3471 | pn, sizeof(pn)); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3472 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 3473 | uri = (log & LW_REQ) ? s->logs.uri ? s->logs.uri : "<BADREQ>" : ""; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3474 | pxid = p->id; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 3475 | srv = (p->to_log & LW_SVID) ? (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "-"; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3476 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 3477 | tm = localtime(&s->logs.tv_accept.tv_sec); |
| 3478 | if (p->to_log & LW_REQ) { |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 3479 | char tmpline[MAX_SYSLOG_LEN], *h; |
| 3480 | int hdr; |
| 3481 | |
| 3482 | h = tmpline; |
| 3483 | if (p->to_log & LW_REQHDR && (h < tmpline + sizeof(tmpline) - 10)) { |
| 3484 | *(h++) = ' '; |
| 3485 | *(h++) = '{'; |
| 3486 | for (hdr = 0; hdr < p->nb_req_cap; hdr++) { |
| 3487 | if (hdr) |
| 3488 | *(h++) = '|'; |
| 3489 | if (s->req_cap[hdr] != NULL) |
| 3490 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, '#', hdr_encode_map, s->req_cap[hdr]); |
| 3491 | } |
| 3492 | *(h++) = '}'; |
| 3493 | } |
| 3494 | |
| 3495 | if (p->to_log & LW_RSPHDR && (h < tmpline + sizeof(tmpline) - 7)) { |
| 3496 | *(h++) = ' '; |
| 3497 | *(h++) = '{'; |
| 3498 | for (hdr = 0; hdr < p->nb_rsp_cap; hdr++) { |
| 3499 | if (hdr) |
| 3500 | *(h++) = '|'; |
| 3501 | if (s->rsp_cap[hdr] != NULL) |
| 3502 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, '#', hdr_encode_map, s->rsp_cap[hdr]); |
| 3503 | } |
| 3504 | *(h++) = '}'; |
| 3505 | } |
| 3506 | |
| 3507 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 3508 | *(h++) = ' '; |
| 3509 | *(h++) = '"'; |
| 3510 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, '#', url_encode_map, uri); |
| 3511 | *(h++) = '"'; |
| 3512 | } |
| 3513 | *h = '\0'; |
| 3514 | |
willy tarreau | 5e69b16 | 2006-05-12 19:49:37 +0200 | [diff] [blame] | 3515 | send_log(p, LOG_INFO, "%s:%d [%02d/%s/%04d:%02d:%02d:%02d] %s %s %d/%d/%d/%d/%s%d %d %s%lld %s %s %c%c%c%c %d/%d/%d %d/%d%s\n", |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3516 | pn, |
| 3517 | (s->cli_addr.ss_family == AF_INET) ? |
| 3518 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 3519 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3520 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 3521 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 3522 | pxid, srv, |
| 3523 | s->logs.t_request, |
willy tarreau | f32f524 | 2006-05-02 22:54:52 +0200 | [diff] [blame] | 3524 | (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1, |
| 3525 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3526 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3527 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 3528 | s->logs.status, |
| 3529 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.bytes, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3530 | s->logs.cli_cookie ? s->logs.cli_cookie : "-", |
| 3531 | s->logs.srv_cookie ? s->logs.srv_cookie : "-", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3532 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 3533 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 3534 | (p->options & PR_O_COOK_ANY) ? sess_cookie[(s->flags & SN_CK_MASK) >> SN_CK_SHIFT] : '-', |
| 3535 | (p->options & PR_O_COOK_ANY) ? sess_set_cookie[(s->flags & SN_SCK_MASK) >> SN_SCK_SHIFT] : '-', |
willy tarreau | 5e69b16 | 2006-05-12 19:49:37 +0200 | [diff] [blame] | 3536 | s->srv ? s->srv->cur_sess : 0, p->nbconn, actconn, |
| 3537 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3538 | } |
| 3539 | else { |
willy tarreau | 5f15c55 | 2006-05-13 18:37:04 +0200 | [diff] [blame] | 3540 | send_log(p, LOG_INFO, "%s:%d [%02d/%s/%04d:%02d:%02d:%02d] %s %s %d/%d/%s%d %s%lld %c%c %d/%d/%d %d/%d\n", |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3541 | pn, |
| 3542 | (s->cli_addr.ss_family == AF_INET) ? |
| 3543 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 3544 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 3545 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 3546 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3547 | pxid, srv, |
willy tarreau | 5f15c55 | 2006-05-13 18:37:04 +0200 | [diff] [blame] | 3548 | (s->logs.t_queue >= 0) ? s->logs.t_queue : -1, |
| 3549 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3550 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 3551 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.bytes, |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3552 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
willy tarreau | 0fe3965 | 2005-12-18 01:25:24 +0100 | [diff] [blame] | 3553 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
willy tarreau | 5e69b16 | 2006-05-12 19:49:37 +0200 | [diff] [blame] | 3554 | s->srv ? s->srv->cur_sess : 0, p->nbconn, actconn, |
| 3555 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3556 | } |
| 3557 | |
| 3558 | s->logs.logwait = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3559 | } |
| 3560 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3561 | |
| 3562 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3563 | * this function is called on a read event from a listen socket, corresponding |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3564 | * to an accept. It tries to accept as many connections as possible. |
| 3565 | * It returns 0. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3566 | */ |
| 3567 | int event_accept(int fd) { |
| 3568 | struct proxy *p = (struct proxy *)fdtab[fd].owner; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3569 | struct session *s; |
| 3570 | struct task *t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3571 | int cfd; |
willy tarreau | c2becdc | 2006-03-19 19:36:48 +0100 | [diff] [blame] | 3572 | int max_accept; |
| 3573 | |
| 3574 | if (global.nbproc > 1) |
| 3575 | max_accept = 8; /* let other processes catch some connections too */ |
| 3576 | else |
| 3577 | max_accept = -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3578 | |
willy tarreau | c2becdc | 2006-03-19 19:36:48 +0100 | [diff] [blame] | 3579 | while (p->nbconn < p->maxconn && max_accept--) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3580 | struct sockaddr_storage addr; |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 3581 | socklen_t laddr = sizeof(addr); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3582 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3583 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) { |
| 3584 | switch (errno) { |
| 3585 | case EAGAIN: |
| 3586 | case EINTR: |
| 3587 | case ECONNABORTED: |
| 3588 | return 0; /* nothing more to accept */ |
| 3589 | case ENFILE: |
| 3590 | send_log(p, LOG_EMERG, |
| 3591 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 3592 | p->id, maxfd); |
| 3593 | return 0; |
| 3594 | case EMFILE: |
| 3595 | send_log(p, LOG_EMERG, |
| 3596 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 3597 | p->id, maxfd); |
| 3598 | return 0; |
| 3599 | case ENOBUFS: |
| 3600 | case ENOMEM: |
| 3601 | send_log(p, LOG_EMERG, |
| 3602 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 3603 | p->id, maxfd); |
| 3604 | return 0; |
| 3605 | default: |
| 3606 | return 0; |
| 3607 | } |
| 3608 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3609 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3610 | if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */ |
| 3611 | Alert("out of memory in event_accept().\n"); |
| 3612 | FD_CLR(fd, StaticReadEvent); |
| 3613 | p->state = PR_STIDLE; |
| 3614 | close(cfd); |
| 3615 | return 0; |
| 3616 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3617 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3618 | /* if this session comes from a known monitoring system, we want to ignore |
| 3619 | * it as soon as possible, which means closing it immediately for TCP. |
| 3620 | */ |
| 3621 | s->flags = 0; |
| 3622 | if (addr.ss_family == AF_INET && |
| 3623 | p->mon_mask.s_addr && |
| 3624 | (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) { |
| 3625 | if (p->mode == PR_MODE_TCP) { |
| 3626 | close(cfd); |
| 3627 | pool_free(session, s); |
| 3628 | continue; |
| 3629 | } |
| 3630 | s->flags |= SN_MONITOR; |
| 3631 | } |
| 3632 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3633 | if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */ |
| 3634 | Alert("out of memory in event_accept().\n"); |
| 3635 | FD_CLR(fd, StaticReadEvent); |
| 3636 | p->state = PR_STIDLE; |
| 3637 | close(cfd); |
| 3638 | pool_free(session, s); |
| 3639 | return 0; |
| 3640 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3641 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3642 | s->cli_addr = addr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3643 | if (cfd >= global.maxsock) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3644 | Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 3645 | close(cfd); |
| 3646 | pool_free(task, t); |
| 3647 | pool_free(session, s); |
| 3648 | return 0; |
| 3649 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3650 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3651 | if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) || |
| 3652 | (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, |
| 3653 | (char *) &one, sizeof(one)) == -1)) { |
| 3654 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
| 3655 | close(cfd); |
| 3656 | pool_free(task, t); |
| 3657 | pool_free(session, s); |
| 3658 | return 0; |
| 3659 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3660 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3661 | if (p->options & PR_O_TCP_CLI_KA) |
| 3662 | setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 3663 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3664 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
willy tarreau | 5e698ef | 2006-05-02 14:51:00 +0200 | [diff] [blame] | 3665 | t->wq = LIST_HEAD(wait_queue[0]); /* but already has a wait queue assigned */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3666 | t->state = TASK_IDLE; |
| 3667 | t->process = process_session; |
| 3668 | t->context = s; |
| 3669 | |
| 3670 | s->task = t; |
| 3671 | s->proxy = p; |
| 3672 | s->cli_state = (p->mode == PR_MODE_HTTP) ? CL_STHEADERS : CL_STDATA; /* no HTTP headers for non-HTTP proxies */ |
| 3673 | s->srv_state = SV_STIDLE; |
| 3674 | s->req = s->rep = NULL; /* will be allocated later */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 3675 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3676 | s->res_cr = s->res_cw = s->res_sr = s->res_sw = RES_SILENT; |
| 3677 | s->cli_fd = cfd; |
| 3678 | s->srv_fd = -1; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 3679 | s->req_line.len = -1; |
| 3680 | s->auth_hdr.len = -1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3681 | s->srv = NULL; |
Willy TARREAU | 1a71cc1 | 2006-05-14 09:10:03 +0200 | [diff] [blame] | 3682 | s->pend_pos = NULL; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3683 | s->conn_retries = p->conn_retries; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3684 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3685 | if (s->flags & SN_MONITOR) |
| 3686 | s->logs.logwait = 0; |
| 3687 | else |
| 3688 | s->logs.logwait = p->to_log; |
| 3689 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3690 | s->logs.tv_accept = now; |
| 3691 | s->logs.t_request = -1; |
willy tarreau | f32f524 | 2006-05-02 22:54:52 +0200 | [diff] [blame] | 3692 | s->logs.t_queue = -1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3693 | s->logs.t_connect = -1; |
| 3694 | s->logs.t_data = -1; |
| 3695 | s->logs.t_close = 0; |
| 3696 | s->logs.uri = NULL; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3697 | s->logs.cli_cookie = NULL; |
| 3698 | s->logs.srv_cookie = NULL; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3699 | s->logs.status = -1; |
| 3700 | s->logs.bytes = 0; |
willy tarreau | 5e69b16 | 2006-05-12 19:49:37 +0200 | [diff] [blame] | 3701 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3702 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3703 | |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3704 | s->data_source = DATA_SRC_NONE; |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 3705 | |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3706 | s->uniq_id = totalconn; |
willy tarreau | 14b4d43 | 2006-04-07 18:23:29 +0200 | [diff] [blame] | 3707 | p->cum_conn++; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3708 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 3709 | if (p->nb_req_cap > 0) { |
| 3710 | if ((s->req_cap = |
| 3711 | pool_alloc_from(p->req_cap_pool, p->nb_req_cap*sizeof(char *))) |
| 3712 | == NULL) { /* no memory */ |
| 3713 | close(cfd); /* nothing can be done for this fd without memory */ |
| 3714 | pool_free(task, t); |
| 3715 | pool_free(session, s); |
| 3716 | return 0; |
| 3717 | } |
| 3718 | memset(s->req_cap, 0, p->nb_req_cap*sizeof(char *)); |
| 3719 | } |
| 3720 | else |
| 3721 | s->req_cap = NULL; |
| 3722 | |
| 3723 | if (p->nb_rsp_cap > 0) { |
| 3724 | if ((s->rsp_cap = |
| 3725 | pool_alloc_from(p->rsp_cap_pool, p->nb_rsp_cap*sizeof(char *))) |
| 3726 | == NULL) { /* no memory */ |
| 3727 | if (s->req_cap != NULL) |
| 3728 | pool_free_to(p->req_cap_pool, s->req_cap); |
| 3729 | close(cfd); /* nothing can be done for this fd without memory */ |
| 3730 | pool_free(task, t); |
| 3731 | pool_free(session, s); |
| 3732 | return 0; |
| 3733 | } |
| 3734 | memset(s->rsp_cap, 0, p->nb_rsp_cap*sizeof(char *)); |
| 3735 | } |
| 3736 | else |
| 3737 | s->rsp_cap = NULL; |
| 3738 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3739 | if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP) |
| 3740 | && (p->logfac1 >= 0 || p->logfac2 >= 0)) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3741 | struct sockaddr_storage sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3742 | socklen_t namelen = sizeof(sockname); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3743 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3744 | if (addr.ss_family != AF_INET || |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3745 | !(s->proxy->options & PR_O_TRANSP) || |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3746 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3747 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3748 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3749 | if (p->to_log) { |
| 3750 | /* we have the client ip */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3751 | if (s->logs.logwait & LW_CLIP) |
| 3752 | if (!(s->logs.logwait &= ~LW_CLIP)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3753 | sess_log(s); |
| 3754 | } |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3755 | else if (s->cli_addr.ss_family == AF_INET) { |
| 3756 | char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN]; |
| 3757 | if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sockname)->sin_addr, |
| 3758 | sn, sizeof(sn)) && |
| 3759 | inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 3760 | pn, sizeof(pn))) { |
| 3761 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 3762 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port), |
| 3763 | sn, ntohs(((struct sockaddr_in *)&sockname)->sin_port), |
| 3764 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 3765 | } |
| 3766 | } |
| 3767 | else { |
| 3768 | char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN]; |
| 3769 | if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sockname)->sin6_addr, |
| 3770 | sn, sizeof(sn)) && |
| 3771 | inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, |
| 3772 | pn, sizeof(pn))) { |
| 3773 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 3774 | pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 3775 | sn, ntohs(((struct sockaddr_in6 *)&sockname)->sin6_port), |
| 3776 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 3777 | } |
| 3778 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3779 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3780 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 3781 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3782 | struct sockaddr_in sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3783 | socklen_t namelen = sizeof(sockname); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3784 | int len; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3785 | if (addr.ss_family != AF_INET || |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3786 | !(s->proxy->options & PR_O_TRANSP) || |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3787 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3788 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3789 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3790 | if (s->cli_addr.ss_family == AF_INET) { |
| 3791 | char pn[INET_ADDRSTRLEN]; |
| 3792 | inet_ntop(AF_INET, |
| 3793 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 3794 | pn, sizeof(pn)); |
| 3795 | |
| 3796 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 3797 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 3798 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port)); |
| 3799 | } |
| 3800 | else { |
| 3801 | char pn[INET6_ADDRSTRLEN]; |
| 3802 | inet_ntop(AF_INET6, |
| 3803 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 3804 | pn, sizeof(pn)); |
| 3805 | |
| 3806 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 3807 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 3808 | pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port)); |
| 3809 | } |
| 3810 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3811 | write(1, trash, len); |
| 3812 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3813 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3814 | if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 3815 | if (s->rsp_cap != NULL) |
| 3816 | pool_free_to(p->rsp_cap_pool, s->rsp_cap); |
| 3817 | if (s->req_cap != NULL) |
| 3818 | pool_free_to(p->req_cap_pool, s->req_cap); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3819 | close(cfd); /* nothing can be done for this fd without memory */ |
| 3820 | pool_free(task, t); |
| 3821 | pool_free(session, s); |
| 3822 | return 0; |
| 3823 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 3824 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3825 | s->req->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3826 | s->req->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3827 | s->req->h = s->req->r = s->req->lr = s->req->w = s->req->data; /* r and w will be reset further */ |
| 3828 | s->req->rlim = s->req->data + BUFSIZE; |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3829 | if (s->cli_state == CL_STHEADERS) /* reserve some space for header rewriting */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3830 | s->req->rlim -= MAXREWRITE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3831 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3832 | if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 3833 | pool_free(buffer, s->req); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 3834 | if (s->rsp_cap != NULL) |
| 3835 | pool_free_to(p->rsp_cap_pool, s->rsp_cap); |
| 3836 | if (s->req_cap != NULL) |
| 3837 | pool_free_to(p->req_cap_pool, s->req_cap); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3838 | close(cfd); /* nothing can be done for this fd without memory */ |
| 3839 | pool_free(task, t); |
| 3840 | pool_free(session, s); |
| 3841 | return 0; |
| 3842 | } |
| 3843 | s->rep->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3844 | s->rep->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3845 | s->rep->h = s->rep->r = s->rep->lr = s->rep->w = s->rep->rlim = s->rep->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3846 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3847 | fdtab[cfd].read = &event_cli_read; |
| 3848 | fdtab[cfd].write = &event_cli_write; |
| 3849 | fdtab[cfd].owner = t; |
| 3850 | fdtab[cfd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3851 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3852 | if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) || |
| 3853 | (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK))) |
| 3854 | /* Either we got a request from a monitoring system on an HTTP instance, |
| 3855 | * or we're in health check mode with the 'httpchk' option enabled. In |
| 3856 | * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit. |
| 3857 | */ |
| 3858 | client_retnclose(s, 19, "HTTP/1.0 200 OK\r\n\r\n"); /* forge a 200 response */ |
| 3859 | else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */ |
| 3860 | client_retnclose(s, 3, "OK\n"); /* forge an "OK" response */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3861 | } |
| 3862 | else { |
| 3863 | FD_SET(cfd, StaticReadEvent); |
| 3864 | } |
| 3865 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3866 | #if defined(DEBUG_FULL) && defined(ENABLE_EPOLL) |
| 3867 | if (PrevReadEvent) { |
| 3868 | assert(!(FD_ISSET(cfd, PrevReadEvent))); |
| 3869 | assert(!(FD_ISSET(cfd, PrevWriteEvent))); |
| 3870 | } |
| 3871 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3872 | fd_insert(cfd); |
| 3873 | |
| 3874 | tv_eternity(&s->cnexpire); |
| 3875 | tv_eternity(&s->srexpire); |
| 3876 | tv_eternity(&s->swexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3877 | tv_eternity(&s->crexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3878 | tv_eternity(&s->cwexpire); |
| 3879 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3880 | if (s->proxy->clitimeout) { |
| 3881 | if (FD_ISSET(cfd, StaticReadEvent)) |
| 3882 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 3883 | if (FD_ISSET(cfd, StaticWriteEvent)) |
| 3884 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
| 3885 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3886 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3887 | tv_min(&t->expire, &s->crexpire, &s->cwexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3888 | |
| 3889 | task_queue(t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3890 | |
| 3891 | if (p->mode != PR_MODE_HEALTH) |
| 3892 | task_wakeup(&rq, t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3893 | |
| 3894 | p->nbconn++; |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 3895 | if (p->nbconn > p->nbconn_max) |
| 3896 | p->nbconn_max = p->nbconn; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3897 | actconn++; |
| 3898 | totalconn++; |
| 3899 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3900 | // fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p\n", p, actconn, totalconn, t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3901 | } /* end of while (p->nbconn < p->maxconn) */ |
| 3902 | return 0; |
| 3903 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3904 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3905 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3906 | /* |
| 3907 | * This function is used only for server health-checks. It handles |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3908 | * the connection acknowledgement. If the proxy requires HTTP health-checks, |
| 3909 | * it sends the request. In other cases, it returns 1 if the socket is OK, |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3910 | * or -1 if an error occured. |
| 3911 | */ |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3912 | int event_srv_chk_w(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3913 | struct task *t = fdtab[fd].owner; |
| 3914 | struct server *s = t->context; |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 3915 | int skerr; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3916 | socklen_t lskerr = sizeof(skerr); |
| 3917 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3918 | skerr = 1; |
| 3919 | if ((getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) |
| 3920 | || (skerr != 0)) { |
| 3921 | /* in case of TCP only, this tells us if the connection failed */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3922 | s->result = -1; |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3923 | fdtab[fd].state = FD_STERROR; |
| 3924 | FD_CLR(fd, StaticWriteEvent); |
| 3925 | } |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 3926 | else if (s->result != -1) { |
| 3927 | /* we don't want to mark 'UP' a server on which we detected an error earlier */ |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3928 | if (s->proxy->options & PR_O_HTTP_CHK) { |
| 3929 | int ret; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3930 | /* we want to check if this host replies to "OPTIONS / HTTP/1.0" |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3931 | * so we'll send the request, and won't wake the checker up now. |
| 3932 | */ |
| 3933 | #ifndef MSG_NOSIGNAL |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3934 | ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3935 | #else |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3936 | ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3937 | #endif |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 3938 | if (ret == s->proxy->check_len) { |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3939 | FD_SET(fd, StaticReadEvent); /* prepare for reading reply */ |
| 3940 | FD_CLR(fd, StaticWriteEvent); /* nothing more to write */ |
| 3941 | return 0; |
| 3942 | } |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3943 | else { |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3944 | s->result = -1; |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3945 | FD_CLR(fd, StaticWriteEvent); |
| 3946 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3947 | } |
| 3948 | else { |
| 3949 | /* good TCP connection is enough */ |
| 3950 | s->result = 1; |
| 3951 | } |
| 3952 | } |
| 3953 | |
| 3954 | task_wakeup(&rq, t); |
| 3955 | return 0; |
| 3956 | } |
| 3957 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3958 | |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3959 | /* |
| 3960 | * This function is used only for server health-checks. It handles |
| 3961 | * the server's reply to an HTTP request. It returns 1 if the server replies |
| 3962 | * 2xx or 3xx (valid responses), or -1 in other cases. |
| 3963 | */ |
| 3964 | int event_srv_chk_r(int fd) { |
| 3965 | char reply[64]; |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 3966 | int len, result; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3967 | struct task *t = fdtab[fd].owner; |
| 3968 | struct server *s = t->context; |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3969 | int skerr; |
| 3970 | socklen_t lskerr = sizeof(skerr); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3971 | |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 3972 | result = len = -1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3973 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3974 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 3975 | if (!skerr) { |
| 3976 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3977 | len = recv(fd, reply, sizeof(reply), 0); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3978 | #else |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3979 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 3980 | * but the connection was closed on the remote end. Fortunately, recv still |
| 3981 | * works correctly and we don't need to do the getsockopt() on linux. |
| 3982 | */ |
| 3983 | len = recv(fd, reply, sizeof(reply), MSG_NOSIGNAL); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3984 | #endif |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3985 | |
| 3986 | if ((len >= sizeof("HTTP/1.0 000")) && |
| 3987 | !memcmp(reply, "HTTP/1.", 7) && |
| 3988 | (reply[9] == '2' || reply[9] == '3')) /* 2xx or 3xx */ |
| 3989 | result = 1; |
| 3990 | } |
| 3991 | |
| 3992 | if (result == -1) |
| 3993 | fdtab[fd].state = FD_STERROR; |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 3994 | |
| 3995 | if (s->result != -1) |
| 3996 | s->result = result; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3997 | |
| 3998 | FD_CLR(fd, StaticReadEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3999 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4000 | return 0; |
| 4001 | } |
| 4002 | |
| 4003 | |
| 4004 | /* |
| 4005 | * this function writes the string <str> at position <pos> which must be in buffer <b>, |
| 4006 | * and moves <end> just after the end of <str>. |
| 4007 | * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift. |
| 4008 | * the shift value (positive or negative) is returned. |
| 4009 | * If there's no space left, the move is not done. |
| 4010 | * |
| 4011 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4012 | int buffer_replace(struct buffer *b, char *pos, char *end, char *str) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4013 | int delta; |
| 4014 | int len; |
| 4015 | |
| 4016 | len = strlen(str); |
| 4017 | delta = len - (end - pos); |
| 4018 | |
| 4019 | if (delta + b->r >= b->data + BUFSIZE) |
| 4020 | return 0; /* no space left */ |
| 4021 | |
| 4022 | /* first, protect the end of the buffer */ |
| 4023 | memmove(end + delta, end, b->data + b->l - end); |
| 4024 | |
| 4025 | /* now, copy str over pos */ |
| 4026 | memcpy(pos, str,len); |
| 4027 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4028 | /* we only move data after the displaced zone */ |
| 4029 | if (b->r > pos) b->r += delta; |
| 4030 | if (b->w > pos) b->w += delta; |
| 4031 | if (b->h > pos) b->h += delta; |
| 4032 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4033 | b->l += delta; |
| 4034 | |
| 4035 | return delta; |
| 4036 | } |
| 4037 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4038 | /* same except that the string length is given, which allows str to be NULL if |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4039 | * len is 0. |
| 4040 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4041 | int buffer_replace2(struct buffer *b, char *pos, char *end, char *str, int len) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4042 | int delta; |
| 4043 | |
| 4044 | delta = len - (end - pos); |
| 4045 | |
| 4046 | if (delta + b->r >= b->data + BUFSIZE) |
| 4047 | return 0; /* no space left */ |
| 4048 | |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 4049 | if (b->data + b->l < end) |
| 4050 | /* The data has been stolen, we could have crashed. Maybe we should abort() ? */ |
| 4051 | return 0; |
| 4052 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4053 | /* first, protect the end of the buffer */ |
| 4054 | memmove(end + delta, end, b->data + b->l - end); |
| 4055 | |
| 4056 | /* now, copy str over pos */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4057 | if (len) |
| 4058 | memcpy(pos, str, len); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4059 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4060 | /* we only move data after the displaced zone */ |
| 4061 | if (b->r > pos) b->r += delta; |
| 4062 | if (b->w > pos) b->w += delta; |
| 4063 | if (b->h > pos) b->h += delta; |
| 4064 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4065 | b->l += delta; |
| 4066 | |
| 4067 | return delta; |
| 4068 | } |
| 4069 | |
| 4070 | |
| 4071 | int exp_replace(char *dst, char *src, char *str, regmatch_t *matches) { |
| 4072 | char *old_dst = dst; |
| 4073 | |
| 4074 | while (*str) { |
| 4075 | if (*str == '\\') { |
| 4076 | str++; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 4077 | if (isdigit((int)*str)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4078 | int len, num; |
| 4079 | |
| 4080 | num = *str - '0'; |
| 4081 | str++; |
| 4082 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 4083 | if (matches[num].rm_eo > -1 && matches[num].rm_so > -1) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4084 | len = matches[num].rm_eo - matches[num].rm_so; |
| 4085 | memcpy(dst, src + matches[num].rm_so, len); |
| 4086 | dst += len; |
| 4087 | } |
| 4088 | |
| 4089 | } |
| 4090 | else if (*str == 'x') { |
| 4091 | unsigned char hex1, hex2; |
| 4092 | str++; |
| 4093 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 4094 | hex1 = toupper(*str++) - '0'; |
| 4095 | hex2 = toupper(*str++) - '0'; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4096 | |
| 4097 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 4098 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 4099 | *dst++ = (hex1<<4) + hex2; |
| 4100 | } |
| 4101 | else |
| 4102 | *dst++ = *str++; |
| 4103 | } |
| 4104 | else |
| 4105 | *dst++ = *str++; |
| 4106 | } |
| 4107 | *dst = 0; |
| 4108 | return dst - old_dst; |
| 4109 | } |
| 4110 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 4111 | static int ishex(char s) |
| 4112 | { |
| 4113 | return (s >= '0' && s <= '9') || (s >= 'A' && s <= 'F') || (s >= 'a' && s <= 'f'); |
| 4114 | } |
| 4115 | |
| 4116 | /* returns NULL if the replacement string <str> is valid, or the pointer to the first error */ |
| 4117 | char *check_replace_string(char *str) |
| 4118 | { |
| 4119 | char *err = NULL; |
| 4120 | while (*str) { |
| 4121 | if (*str == '\\') { |
| 4122 | err = str; /* in case of a backslash, we return the pointer to it */ |
| 4123 | str++; |
| 4124 | if (!*str) |
| 4125 | return err; |
| 4126 | else if (isdigit((int)*str)) |
| 4127 | err = NULL; |
| 4128 | else if (*str == 'x') { |
| 4129 | str++; |
| 4130 | if (!ishex(*str)) |
| 4131 | return err; |
| 4132 | str++; |
| 4133 | if (!ishex(*str)) |
| 4134 | return err; |
| 4135 | err = NULL; |
| 4136 | } |
| 4137 | else { |
| 4138 | Warning("'\\%c' : deprecated use of a backslash before something not '\\','x' or a digit.\n", *str); |
| 4139 | err = NULL; |
| 4140 | } |
| 4141 | } |
| 4142 | str++; |
| 4143 | } |
| 4144 | return err; |
| 4145 | } |
| 4146 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4147 | /* |
| 4148 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 4149 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 4150 | * 0 else. |
| 4151 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4152 | int process_cli(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4153 | int s = t->srv_state; |
| 4154 | int c = t->cli_state; |
| 4155 | struct buffer *req = t->req; |
| 4156 | struct buffer *rep = t->rep; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4157 | int method_checked = 0; |
| 4158 | appsess *asession_temp = NULL; |
| 4159 | appsess local_asession; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4160 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4161 | #ifdef DEBUG_FULL |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4162 | fprintf(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n", |
| 4163 | cli_stnames[c], srv_stnames[s], |
| 4164 | FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 4165 | t->crexpire.tv_sec, t->crexpire.tv_usec, |
| 4166 | t->cwexpire.tv_sec, t->cwexpire.tv_usec); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4167 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4168 | //fprintf(stderr,"process_cli: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 4169 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 4170 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 4171 | //); |
| 4172 | if (c == CL_STHEADERS) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4173 | /* now parse the partial (or complete) headers */ |
| 4174 | while (req->lr < req->r) { /* this loop only sees one header at each iteration */ |
| 4175 | char *ptr; |
| 4176 | int delete_header; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4177 | char *request_line = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4178 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4179 | ptr = req->lr; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4180 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4181 | /* look for the end of the current header */ |
| 4182 | while (ptr < req->r && *ptr != '\n' && *ptr != '\r') |
| 4183 | ptr++; |
| 4184 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4185 | if (ptr == req->h) { /* empty line, end of headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4186 | int line, len; |
willy tarreau | 43b1512 | 2006-04-10 21:01:39 +0200 | [diff] [blame] | 4187 | |
| 4188 | /* |
| 4189 | * first, let's check that it's not a leading empty line, in |
| 4190 | * which case we'll ignore and remove it (according to RFC2616). |
| 4191 | */ |
| 4192 | if (req->h == req->data) { |
| 4193 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 4194 | if (ptr > req->r - 2) { |
| 4195 | /* this is a partial header, let's wait for more to come */ |
| 4196 | req->lr = ptr; |
| 4197 | break; |
| 4198 | } |
| 4199 | |
| 4200 | /* now we know that *ptr is either \r or \n, |
| 4201 | * and that there are at least 1 char after it. |
| 4202 | */ |
| 4203 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 4204 | req->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 4205 | else |
| 4206 | req->lr = ptr + 2; /* \r\n or \n\r */ |
| 4207 | /* ignore empty leading lines */ |
| 4208 | buffer_replace2(req, req->h, req->lr, NULL, 0); |
| 4209 | req->h = req->lr; |
| 4210 | continue; |
| 4211 | } |
| 4212 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4213 | /* we can only get here after an end of headers */ |
| 4214 | /* we'll have something else to do here : add new headers ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4215 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4216 | if (t->flags & SN_CLDENY) { |
| 4217 | /* no need to go further */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4218 | t->logs.status = 403; |
willy tarreau | 47ee7ad | 2006-05-18 01:25:36 +0200 | [diff] [blame] | 4219 | t->logs.t_request = tv_diff(&t->logs.tv_accept, &now); /* let's log the request time */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4220 | client_retnclose(t, t->proxy->errmsg.len403, t->proxy->errmsg.msg403); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4221 | if (!(t->flags & SN_ERR_MASK)) |
| 4222 | t->flags |= SN_ERR_PRXCOND; |
| 4223 | if (!(t->flags & SN_FINST_MASK)) |
| 4224 | t->flags |= SN_FINST_R; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4225 | return 1; |
| 4226 | } |
| 4227 | |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 4228 | /* Right now, we know that we have processed the entire headers |
| 4229 | * and that unwanted requests have been filtered out. We can do |
| 4230 | * whatever we want. |
| 4231 | */ |
| 4232 | |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 4233 | if (t->proxy->uri_auth != NULL |
| 4234 | && t->req_line.len >= t->proxy->uri_auth->uri_len + 4) { /* +4 for "GET /" */ |
| 4235 | if (!memcmp(t->req_line.str + 4, |
| 4236 | t->proxy->uri_auth->uri_prefix, t->proxy->uri_auth->uri_len) |
| 4237 | && !memcmp(t->req_line.str, "GET ", 4)) { |
| 4238 | struct user_auth *user; |
| 4239 | int authenticated; |
| 4240 | |
| 4241 | /* we are in front of a interceptable URI. Let's check |
| 4242 | * if there's an authentication and if it's valid. |
| 4243 | */ |
| 4244 | user = t->proxy->uri_auth->users; |
| 4245 | if (!user) { |
| 4246 | /* no user auth required, it's OK */ |
| 4247 | authenticated = 1; |
| 4248 | } else { |
| 4249 | authenticated = 0; |
| 4250 | |
| 4251 | /* a user list is defined, we have to check. |
| 4252 | * skip 21 chars for "Authorization: Basic ". |
| 4253 | */ |
| 4254 | if (t->auth_hdr.len < 21 || memcmp(t->auth_hdr.str + 14, " Basic ", 7)) |
| 4255 | user = NULL; |
| 4256 | |
| 4257 | while (user) { |
| 4258 | if ((t->auth_hdr.len == user->user_len + 21) |
| 4259 | && !memcmp(t->auth_hdr.str+21, user->user_pwd, user->user_len)) { |
| 4260 | authenticated = 1; |
| 4261 | break; |
| 4262 | } |
| 4263 | user = user->next; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 4264 | } |
| 4265 | } |
| 4266 | |
| 4267 | if (!authenticated) { |
| 4268 | int msglen; |
| 4269 | |
| 4270 | /* no need to go further */ |
| 4271 | |
| 4272 | msglen = sprintf(trash, HTTP_401_fmt, t->proxy->uri_auth->auth_realm); |
| 4273 | t->logs.status = 401; |
| 4274 | client_retnclose(t, msglen, trash); |
| 4275 | if (!(t->flags & SN_ERR_MASK)) |
| 4276 | t->flags |= SN_ERR_PRXCOND; |
| 4277 | if (!(t->flags & SN_FINST_MASK)) |
| 4278 | t->flags |= SN_FINST_R; |
| 4279 | return 1; |
| 4280 | } |
| 4281 | |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 4282 | t->cli_state = CL_STSHUTR; |
willy tarreau | 950609c | 2006-05-18 01:23:51 +0200 | [diff] [blame] | 4283 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
| 4284 | t->logs.t_request = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 4285 | t->data_source = DATA_SRC_STATS; |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 4286 | t->data_state = DATA_ST_INIT; |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 4287 | produce_content(t); |
| 4288 | return 1; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 4289 | } |
| 4290 | } |
| 4291 | |
| 4292 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4293 | for (line = 0; line < t->proxy->nb_reqadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4294 | len = sprintf(trash, "%s\r\n", t->proxy->req_add[line]); |
| 4295 | buffer_replace2(req, req->h, req->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4296 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4297 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4298 | if (t->proxy->options & PR_O_FWDFOR) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 4299 | if (t->cli_addr.ss_family == AF_INET) { |
| 4300 | unsigned char *pn; |
| 4301 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
| 4302 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d\r\n", |
| 4303 | pn[0], pn[1], pn[2], pn[3]); |
| 4304 | buffer_replace2(req, req->h, req->h, trash, len); |
| 4305 | } |
| 4306 | else if (t->cli_addr.ss_family == AF_INET6) { |
| 4307 | char pn[INET6_ADDRSTRLEN]; |
| 4308 | inet_ntop(AF_INET6, |
| 4309 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 4310 | pn, sizeof(pn)); |
| 4311 | len = sprintf(trash, "X-Forwarded-For: %s\r\n", pn); |
| 4312 | buffer_replace2(req, req->h, req->h, trash, len); |
| 4313 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4314 | } |
| 4315 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4316 | /* add a "connection: close" line if needed */ |
| 4317 | if (t->proxy->options & PR_O_HTTP_CLOSE) |
| 4318 | buffer_replace2(req, req->h, req->h, "Connection: close\r\n", 19); |
| 4319 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4320 | if (!memcmp(req->data, "POST ", 5)) { |
| 4321 | /* this is a POST request, which is not cacheable by default */ |
| 4322 | t->flags |= SN_POST; |
| 4323 | } |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 4324 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4325 | t->cli_state = CL_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4326 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4327 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4328 | t->logs.t_request = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4329 | /* FIXME: we'll set the client in a wait state while we try to |
| 4330 | * connect to the server. Is this really needed ? wouldn't it be |
willy tarreau | 0889c96 | 2006-04-24 14:36:48 +0200 | [diff] [blame] | 4331 | * better to release the maximum of system buffers instead ? |
| 4332 | * The solution is to enable the FD but set its time-out to |
| 4333 | * eternity as long as the server-side does not enable data xfer. |
| 4334 | * CL_STDATA also has to take care of this, which is done below. |
| 4335 | */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4336 | //FD_CLR(t->cli_fd, StaticReadEvent); |
| 4337 | //tv_eternity(&t->crexpire); |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 4338 | |
| 4339 | /* FIXME: if we break here (as up to 1.1.23), having the client |
| 4340 | * shutdown its connection can lead to an abort further. |
| 4341 | * it's better to either return 1 or even jump directly to the |
| 4342 | * data state which will save one schedule. |
| 4343 | */ |
| 4344 | //break; |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 4345 | |
| 4346 | if (!t->proxy->clitimeout || |
| 4347 | (t->srv_state < SV_STDATA && t->proxy->srvtimeout)) |
| 4348 | /* If the client has no timeout, or if the server is not ready yet, |
| 4349 | * and we know for sure that it can expire, then it's cleaner to |
| 4350 | * disable the timeout on the client side so that too low values |
| 4351 | * cannot make the sessions abort too early. |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4352 | * |
| 4353 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 4354 | * when it switches its state, otherwise a client can stay connected |
| 4355 | * indefinitely. This now seems to be OK. |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 4356 | */ |
| 4357 | tv_eternity(&t->crexpire); |
| 4358 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 4359 | goto process_data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4360 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4361 | |
Willy TARREAU | 13032e7 | 2006-03-12 17:31:45 +0100 | [diff] [blame] | 4362 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 4363 | if (ptr > req->r - 2) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4364 | /* this is a partial header, let's wait for more to come */ |
| 4365 | req->lr = ptr; |
| 4366 | break; |
| 4367 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4368 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4369 | /* now we know that *ptr is either \r or \n, |
| 4370 | * and that there are at least 1 char after it. |
| 4371 | */ |
| 4372 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 4373 | req->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 4374 | else |
| 4375 | req->lr = ptr + 2; /* \r\n or \n\r */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4376 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4377 | /* |
| 4378 | * now we know that we have a full header ; we can do whatever |
| 4379 | * we want with these pointers : |
| 4380 | * req->h = beginning of header |
| 4381 | * ptr = end of header (first \r or \n) |
| 4382 | * req->lr = beginning of next line (next rep->h) |
| 4383 | * req->r = end of data (not used at this stage) |
| 4384 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4385 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4386 | if (!method_checked && (t->proxy->appsession_name != NULL) && |
| 4387 | ((memcmp(req->h, "GET ", 4) == 0) || (memcmp(req->h, "POST ", 4) == 0)) && |
| 4388 | ((request_line = memchr(req->h, ';', req->lr - req->h)) != NULL)) { |
| 4389 | |
| 4390 | /* skip ; */ |
| 4391 | request_line++; |
| 4392 | |
| 4393 | /* look if we have a jsessionid */ |
| 4394 | |
| 4395 | if (strncasecmp(request_line, t->proxy->appsession_name, t->proxy->appsession_name_len) == 0) { |
| 4396 | |
| 4397 | /* skip jsessionid= */ |
| 4398 | request_line += t->proxy->appsession_name_len + 1; |
| 4399 | |
| 4400 | /* First try if we allready have an appsession */ |
| 4401 | asession_temp = &local_asession; |
| 4402 | |
| 4403 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
| 4404 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4405 | send_log(t->proxy, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4406 | return 0; |
| 4407 | } |
| 4408 | |
| 4409 | /* Copy the sessionid */ |
| 4410 | memcpy(asession_temp->sessid, request_line, t->proxy->appsession_len); |
| 4411 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 4412 | asession_temp->serverid = NULL; |
| 4413 | |
| 4414 | /* only do insert, if lookup fails */ |
| 4415 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *)&asession_temp)) { |
| 4416 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 4417 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4418 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4419 | return 0; |
| 4420 | } |
| 4421 | asession_temp->sessid = local_asession.sessid; |
| 4422 | asession_temp->serverid = local_asession.serverid; |
| 4423 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4424 | } /* end if (chtbl_lookup()) */ |
| 4425 | else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4426 | /*free wasted memory;*/ |
| 4427 | pool_free_to(apools.sessid, local_asession.sessid); |
| 4428 | } |
| 4429 | |
| 4430 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
| 4431 | asession_temp->request_count++; |
| 4432 | |
| 4433 | #if defined(DEBUG_HASH) |
| 4434 | print_table(&(t->proxy->htbl_proxy)); |
| 4435 | #endif |
| 4436 | |
| 4437 | if (asession_temp->serverid == NULL) { |
| 4438 | Alert("Found Application Session without matching server.\n"); |
| 4439 | } else { |
| 4440 | struct server *srv = t->proxy->srv; |
| 4441 | while (srv) { |
| 4442 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
| 4443 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 4444 | /* we found the server and it's usable */ |
| 4445 | t->flags &= ~SN_CK_MASK; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 4446 | t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4447 | t->srv = srv; |
| 4448 | break; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4449 | } else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4450 | t->flags &= ~SN_CK_MASK; |
| 4451 | t->flags |= SN_CK_DOWN; |
| 4452 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4453 | } /* end if (strcmp()) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4454 | srv = srv->next; |
| 4455 | }/* end while(srv) */ |
| 4456 | }/* end else of if (asession_temp->serverid == NULL) */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4457 | }/* end if (strncasecmp(request_line,t->proxy->appsession_name,apssesion_name_len) == 0) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4458 | else { |
| 4459 | //fprintf(stderr,">>>>>>>>>>>>>>>>>>>>>>NO SESSION\n"); |
| 4460 | } |
willy tarreau | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 4461 | method_checked = 1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4462 | } /* end if (!method_checked ...) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4463 | else{ |
| 4464 | //printf("No Methode-Header with Session-String\n"); |
| 4465 | } |
| 4466 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4467 | if (t->logs.logwait & LW_REQ) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4468 | /* we have a complete HTTP request that we must log */ |
| 4469 | int urilen; |
| 4470 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4471 | if ((t->logs.uri = pool_alloc(requri)) == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4472 | Alert("HTTP logging : out of memory.\n"); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4473 | t->logs.status = 500; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4474 | client_retnclose(t, t->proxy->errmsg.len500, t->proxy->errmsg.msg500); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4475 | if (!(t->flags & SN_ERR_MASK)) |
| 4476 | t->flags |= SN_ERR_PRXCOND; |
| 4477 | if (!(t->flags & SN_FINST_MASK)) |
| 4478 | t->flags |= SN_FINST_R; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4479 | return 1; |
| 4480 | } |
| 4481 | |
| 4482 | urilen = ptr - req->h; |
| 4483 | if (urilen >= REQURI_LEN) |
| 4484 | urilen = REQURI_LEN - 1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4485 | memcpy(t->logs.uri, req->h, urilen); |
| 4486 | t->logs.uri[urilen] = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4487 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4488 | if (!(t->logs.logwait &= ~LW_REQ)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4489 | sess_log(t); |
| 4490 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 4491 | else if (t->logs.logwait & LW_REQHDR) { |
| 4492 | struct cap_hdr *h; |
| 4493 | int len; |
| 4494 | for (h = t->proxy->req_cap; h; h = h->next) { |
| 4495 | if ((h->namelen + 2 <= ptr - req->h) && |
| 4496 | (req->h[h->namelen] == ':') && |
| 4497 | (strncasecmp(req->h, h->name, h->namelen) == 0)) { |
| 4498 | |
| 4499 | if (t->req_cap[h->index] == NULL) |
| 4500 | t->req_cap[h->index] = pool_alloc_from(h->pool, h->len + 1); |
| 4501 | |
| 4502 | len = ptr - (req->h + h->namelen + 2); |
| 4503 | if (len > h->len) |
| 4504 | len = h->len; |
| 4505 | |
| 4506 | memcpy(t->req_cap[h->index], req->h + h->namelen + 2, len); |
| 4507 | t->req_cap[h->index][len]=0; |
| 4508 | } |
| 4509 | } |
| 4510 | |
| 4511 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4512 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4513 | delete_header = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4514 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4515 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4516 | int len, max; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 4517 | len = sprintf(trash, "%08x:%s.clihdr[%04x:%04x]: ", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4518 | max = ptr - req->h; |
| 4519 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4520 | len += strlcpy2(trash + len, req->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4521 | trash[len++] = '\n'; |
| 4522 | write(1, trash, len); |
| 4523 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4524 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4525 | |
| 4526 | /* remove "connection: " if needed */ |
| 4527 | if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE) |
| 4528 | && (strncasecmp(req->h, "Connection: ", 12) == 0)) { |
| 4529 | delete_header = 1; |
| 4530 | } |
| 4531 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4532 | /* try headers regexps */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4533 | if (!delete_header && t->proxy->req_exp != NULL |
| 4534 | && !(t->flags & SN_CLDENY)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4535 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4536 | char term; |
| 4537 | |
| 4538 | term = *ptr; |
| 4539 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4540 | exp = t->proxy->req_exp; |
| 4541 | do { |
| 4542 | if (regexec(exp->preg, req->h, MAX_MATCH, pmatch, 0) == 0) { |
| 4543 | switch (exp->action) { |
| 4544 | case ACT_ALLOW: |
| 4545 | if (!(t->flags & SN_CLDENY)) |
| 4546 | t->flags |= SN_CLALLOW; |
| 4547 | break; |
| 4548 | case ACT_REPLACE: |
| 4549 | if (!(t->flags & SN_CLDENY)) { |
| 4550 | int len = exp_replace(trash, req->h, exp->replace, pmatch); |
| 4551 | ptr += buffer_replace2(req, req->h, ptr, trash, len); |
| 4552 | } |
| 4553 | break; |
| 4554 | case ACT_REMOVE: |
| 4555 | if (!(t->flags & SN_CLDENY)) |
| 4556 | delete_header = 1; |
| 4557 | break; |
| 4558 | case ACT_DENY: |
| 4559 | if (!(t->flags & SN_CLALLOW)) |
| 4560 | t->flags |= SN_CLDENY; |
| 4561 | break; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4562 | case ACT_PASS: /* we simply don't deny this one */ |
| 4563 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4564 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4565 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4566 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4567 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4568 | *ptr = term; /* restore the string terminator */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4569 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4570 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4571 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4572 | * attributes whose name begin with a '$', and associate them with |
| 4573 | * the right cookie, if we want to delete this cookie. |
| 4574 | * So there are 3 cases for each cookie read : |
| 4575 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4576 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4577 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4578 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4579 | * "special" cookie. |
| 4580 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4581 | * remove it. If no application cookie persists in the header, we |
| 4582 | * *MUST* delete it |
| 4583 | */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4584 | if (!delete_header && |
| 4585 | (t->proxy->cookie_name != NULL || t->proxy->capture_name != NULL || t->proxy->appsession_name !=NULL) |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4586 | && !(t->flags & SN_CLDENY) && (ptr >= req->h + 8) |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 4587 | && (strncasecmp(req->h, "Cookie: ", 8) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4588 | char *p1, *p2, *p3, *p4; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4589 | char *del_colon, *del_cookie, *colon; |
| 4590 | int app_cookies; |
| 4591 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4592 | p1 = req->h + 8; /* first char after 'Cookie: ' */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4593 | colon = p1; |
| 4594 | /* del_cookie == NULL => nothing to be deleted */ |
| 4595 | del_colon = del_cookie = NULL; |
| 4596 | app_cookies = 0; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4597 | |
| 4598 | while (p1 < ptr) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4599 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4600 | while (p1 < ptr) { |
| 4601 | if (*p1 == ';' || *p1 == ',') |
| 4602 | colon = p1; |
| 4603 | else if (!isspace((int)*p1)) |
| 4604 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4605 | p1++; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4606 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4607 | |
| 4608 | if (p1 == ptr) |
| 4609 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4610 | |
| 4611 | /* p1 is at the beginning of the cookie name */ |
| 4612 | p2 = p1; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4613 | while (p2 < ptr && *p2 != '=') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4614 | p2++; |
| 4615 | |
| 4616 | if (p2 == ptr) |
| 4617 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4618 | |
| 4619 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4620 | if (p3 == ptr) |
| 4621 | break; |
| 4622 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4623 | p4 = p3; |
| 4624 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';' && *p4 != ',') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4625 | p4++; |
| 4626 | |
| 4627 | /* here, we have the cookie name between p1 and p2, |
| 4628 | * and its value between p3 and p4. |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4629 | * we can process it : |
| 4630 | * |
| 4631 | * Cookie: NAME=VALUE; |
| 4632 | * | || || | |
| 4633 | * | || || +--> p4 |
| 4634 | * | || |+-------> p3 |
| 4635 | * | || +--------> p2 |
| 4636 | * | |+------------> p1 |
| 4637 | * | +-------------> colon |
| 4638 | * +--------------------> req->h |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4639 | */ |
| 4640 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4641 | if (*p1 == '$') { |
| 4642 | /* skip this one */ |
| 4643 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4644 | else { |
| 4645 | /* first, let's see if we want to capture it */ |
| 4646 | if (t->proxy->capture_name != NULL && |
| 4647 | t->logs.cli_cookie == NULL && |
| 4648 | (p4 - p1 >= t->proxy->capture_namelen) && |
| 4649 | memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) { |
| 4650 | int log_len = p4 - p1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4651 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4652 | if ((t->logs.cli_cookie = pool_alloc(capture)) == NULL) { |
| 4653 | Alert("HTTP logging : out of memory.\n"); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4654 | } else { |
| 4655 | if (log_len > t->proxy->capture_len) |
| 4656 | log_len = t->proxy->capture_len; |
| 4657 | memcpy(t->logs.cli_cookie, p1, log_len); |
| 4658 | t->logs.cli_cookie[log_len] = 0; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4659 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4660 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4661 | |
| 4662 | if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) && |
| 4663 | (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
| 4664 | /* Cool... it's the right one */ |
| 4665 | struct server *srv = t->proxy->srv; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4666 | char *delim; |
| 4667 | |
| 4668 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4669 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4670 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4671 | * |
| 4672 | * Cookie: NAME=SRV~VALUE; |
| 4673 | * | || || | | |
| 4674 | * | || || | +--> p4 |
| 4675 | * | || || +--------> delim |
| 4676 | * | || |+-----------> p3 |
| 4677 | * | || +------------> p2 |
| 4678 | * | |+----------------> p1 |
| 4679 | * | +-----------------> colon |
| 4680 | * +------------------------> req->h |
| 4681 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4682 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4683 | if (t->proxy->options & PR_O_COOK_PFX) { |
| 4684 | for (delim = p3; delim < p4; delim++) |
| 4685 | if (*delim == COOKIE_DELIM) |
| 4686 | break; |
| 4687 | } |
| 4688 | else |
| 4689 | delim = p4; |
| 4690 | |
| 4691 | |
| 4692 | /* Here, we'll look for the first running server which supports the cookie. |
| 4693 | * This allows to share a same cookie between several servers, for example |
| 4694 | * to dedicate backup servers to specific servers only. |
willy tarreau | 422bb2e | 2006-05-10 04:27:21 +0200 | [diff] [blame] | 4695 | * However, to prevent clients from sticking to cookie-less backup server |
| 4696 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4697 | * empty cookies and mark them as invalid. |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4698 | */ |
willy tarreau | 422bb2e | 2006-05-10 04:27:21 +0200 | [diff] [blame] | 4699 | if (delim == p3) |
| 4700 | srv = NULL; |
| 4701 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4702 | while (srv) { |
| 4703 | if ((srv->cklen == delim - p3) && !memcmp(p3, srv->cookie, delim - p3)) { |
| 4704 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 4705 | /* we found the server and it's usable */ |
| 4706 | t->flags &= ~SN_CK_MASK; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 4707 | t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4708 | t->srv = srv; |
| 4709 | break; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4710 | } else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4711 | /* we found a server, but it's down */ |
| 4712 | t->flags &= ~SN_CK_MASK; |
| 4713 | t->flags |= SN_CK_DOWN; |
| 4714 | } |
| 4715 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4716 | srv = srv->next; |
| 4717 | } |
| 4718 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4719 | if (!srv && !(t->flags & SN_CK_DOWN)) { |
| 4720 | /* no server matched this cookie */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4721 | t->flags &= ~SN_CK_MASK; |
| 4722 | t->flags |= SN_CK_INVALID; |
| 4723 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4724 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4725 | /* depending on the cookie mode, we may have to either : |
| 4726 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4727 | * the server never sees it ; |
| 4728 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4729 | * application cookie so that it does not get accidentely removed later, |
| 4730 | * if we're in cookie prefix mode |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4731 | */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4732 | if ((t->proxy->options & PR_O_COOK_PFX) && (delim != p4)) { |
| 4733 | buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4734 | p4 -= (delim + 1 - p3); |
| 4735 | ptr -= (delim + 1 - p3); |
| 4736 | del_cookie = del_colon = NULL; |
| 4737 | app_cookies++; /* protect the header from deletion */ |
| 4738 | } |
| 4739 | else if (del_cookie == NULL && |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4740 | (t->proxy->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4741 | del_cookie = p1; |
| 4742 | del_colon = colon; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4743 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4744 | } else { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4745 | /* now we know that we must keep this cookie since it's |
| 4746 | * not ours. But if we wanted to delete our cookie |
| 4747 | * earlier, we cannot remove the complete header, but we |
| 4748 | * can remove the previous block itself. |
| 4749 | */ |
| 4750 | app_cookies++; |
| 4751 | |
| 4752 | if (del_cookie != NULL) { |
| 4753 | buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4754 | p4 -= (p1 - del_cookie); |
| 4755 | ptr -= (p1 - del_cookie); |
| 4756 | del_cookie = del_colon = NULL; |
| 4757 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4758 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4759 | |
| 4760 | if ((t->proxy->appsession_name != NULL) && |
| 4761 | (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) { |
| 4762 | /* first, let's see if the cookie is our appcookie*/ |
| 4763 | |
| 4764 | /* Cool... it's the right one */ |
| 4765 | |
| 4766 | asession_temp = &local_asession; |
| 4767 | |
| 4768 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
| 4769 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4770 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4771 | return 0; |
| 4772 | } |
| 4773 | |
| 4774 | memcpy(asession_temp->sessid, p3, t->proxy->appsession_len); |
| 4775 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 4776 | asession_temp->serverid = NULL; |
| 4777 | |
| 4778 | /* only do insert, if lookup fails */ |
| 4779 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) { |
| 4780 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 4781 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4782 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4783 | return 0; |
| 4784 | } |
| 4785 | |
| 4786 | asession_temp->sessid = local_asession.sessid; |
| 4787 | asession_temp->serverid = local_asession.serverid; |
| 4788 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
| 4789 | } |
| 4790 | else{ |
| 4791 | /* free wasted memory */ |
| 4792 | pool_free_to(apools.sessid, local_asession.sessid); |
| 4793 | } |
| 4794 | |
| 4795 | if (asession_temp->serverid == NULL) { |
| 4796 | Alert("Found Application Session without matching server.\n"); |
| 4797 | } else { |
| 4798 | struct server *srv = t->proxy->srv; |
| 4799 | while (srv) { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4800 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4801 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 4802 | /* we found the server and it's usable */ |
| 4803 | t->flags &= ~SN_CK_MASK; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 4804 | t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4805 | t->srv = srv; |
| 4806 | break; |
| 4807 | } else { |
| 4808 | t->flags &= ~SN_CK_MASK; |
| 4809 | t->flags |= SN_CK_DOWN; |
| 4810 | } |
| 4811 | } |
| 4812 | srv = srv->next; |
| 4813 | }/* end while(srv) */ |
| 4814 | }/* end else if server == NULL */ |
| 4815 | |
| 4816 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4817 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4818 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4819 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4820 | /* we'll have to look for another cookie ... */ |
| 4821 | p1 = p4; |
| 4822 | } /* while (p1 < ptr) */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4823 | |
| 4824 | /* There's no more cookie on this line. |
| 4825 | * We may have marked the last one(s) for deletion. |
| 4826 | * We must do this now in two ways : |
| 4827 | * - if there is no app cookie, we simply delete the header ; |
| 4828 | * - if there are app cookies, we must delete the end of the |
| 4829 | * string properly, including the colon/semi-colon before |
| 4830 | * the cookie name. |
| 4831 | */ |
| 4832 | if (del_cookie != NULL) { |
| 4833 | if (app_cookies) { |
| 4834 | buffer_replace2(req, del_colon, ptr, NULL, 0); |
| 4835 | /* WARNING! <ptr> becomes invalid for now. If some code |
| 4836 | * below needs to rely on it before the end of the global |
| 4837 | * header loop, we need to correct it with this code : |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4838 | */ |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 4839 | ptr = del_colon; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4840 | } |
| 4841 | else |
| 4842 | delete_header = 1; |
| 4843 | } |
| 4844 | } /* end of cookie processing on this header */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4845 | |
| 4846 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4847 | if (delete_header && !(t->flags & SN_CLDENY)) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4848 | buffer_replace2(req, req->h, req->lr, NULL, 0); |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 4849 | /* WARNING: ptr is not valid anymore, since the header may have |
| 4850 | * been deleted or truncated ! */ |
| 4851 | } else { |
| 4852 | /* try to catch the first line as the request */ |
| 4853 | if (t->req_line.len < 0) { |
| 4854 | t->req_line.str = req->h; |
| 4855 | t->req_line.len = ptr - req->h; |
| 4856 | } |
| 4857 | |
| 4858 | /* We might also need the 'Authorization: ' header */ |
| 4859 | if (t->auth_hdr.len < 0 && |
| 4860 | t->proxy->uri_auth != NULL && |
| 4861 | ptr > req->h + 15 && |
| 4862 | !strncasecmp("Authorization: ", req->h, 15)) { |
| 4863 | t->auth_hdr.str = req->h; |
| 4864 | t->auth_hdr.len = ptr - req->h; |
| 4865 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4866 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4867 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4868 | req->h = req->lr; |
| 4869 | } /* while (req->lr < req->r) */ |
| 4870 | |
| 4871 | /* end of header processing (even if incomplete) */ |
| 4872 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4873 | if ((req->l < req->rlim - req->data) && ! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 4874 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 4875 | * full. We cannot loop here since event_cli_read will disable it only if |
| 4876 | * req->l == rlim-data |
| 4877 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4878 | FD_SET(t->cli_fd, StaticReadEvent); |
| 4879 | if (t->proxy->clitimeout) |
| 4880 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 4881 | else |
| 4882 | tv_eternity(&t->crexpire); |
| 4883 | } |
| 4884 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4885 | /* Since we are in header mode, if there's no space left for headers, we |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4886 | * won't be able to free more later, so the session will never terminate. |
| 4887 | */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4888 | if (req->l >= req->rlim - req->data) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4889 | t->logs.status = 400; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4890 | client_retnclose(t, t->proxy->errmsg.len400, t->proxy->errmsg.msg400); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4891 | if (!(t->flags & SN_ERR_MASK)) |
| 4892 | t->flags |= SN_ERR_PRXCOND; |
| 4893 | if (!(t->flags & SN_FINST_MASK)) |
| 4894 | t->flags |= SN_FINST_R; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4895 | return 1; |
| 4896 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4897 | else if (t->res_cr == RES_ERROR || t->res_cr == RES_NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4898 | /* read error, or last read : give up. */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4899 | tv_eternity(&t->crexpire); |
| 4900 | fd_delete(t->cli_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4901 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4902 | if (!(t->flags & SN_ERR_MASK)) |
| 4903 | t->flags |= SN_ERR_CLICL; |
| 4904 | if (!(t->flags & SN_FINST_MASK)) |
| 4905 | t->flags |= SN_FINST_R; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4906 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4907 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4908 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 4909 | |
| 4910 | /* read timeout : give up with an error message. |
| 4911 | */ |
| 4912 | t->logs.status = 408; |
| 4913 | client_retnclose(t, t->proxy->errmsg.len408, t->proxy->errmsg.msg408); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4914 | if (!(t->flags & SN_ERR_MASK)) |
| 4915 | t->flags |= SN_ERR_CLITO; |
| 4916 | if (!(t->flags & SN_FINST_MASK)) |
| 4917 | t->flags |= SN_FINST_R; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4918 | return 1; |
| 4919 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4920 | |
| 4921 | return t->cli_state != CL_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4922 | } |
| 4923 | else if (c == CL_STDATA) { |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 4924 | process_data: |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 4925 | /* FIXME: this error handling is partly buggy because we always report |
| 4926 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 4927 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 4928 | * we're waiting for the server to connect. |
| 4929 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4930 | /* read or write error */ |
| 4931 | if (t->res_cw == RES_ERROR || t->res_cr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4932 | tv_eternity(&t->crexpire); |
| 4933 | tv_eternity(&t->cwexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4934 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4935 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4936 | if (!(t->flags & SN_ERR_MASK)) |
| 4937 | t->flags |= SN_ERR_CLICL; |
willy tarreau | 078c79a | 2006-05-13 12:23:58 +0200 | [diff] [blame] | 4938 | if (!(t->flags & SN_FINST_MASK)) { |
| 4939 | if (t->pend_pos) |
| 4940 | t->flags |= SN_FINST_Q; |
| 4941 | else if (s == SV_STCONN) |
| 4942 | t->flags |= SN_FINST_C; |
| 4943 | else |
| 4944 | t->flags |= SN_FINST_D; |
| 4945 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4946 | return 1; |
| 4947 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4948 | /* last read, or end of server write */ |
| 4949 | else if (t->res_cr == RES_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4950 | FD_CLR(t->cli_fd, StaticReadEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4951 | tv_eternity(&t->crexpire); |
| 4952 | shutdown(t->cli_fd, SHUT_RD); |
| 4953 | t->cli_state = CL_STSHUTR; |
| 4954 | return 1; |
| 4955 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4956 | /* last server read and buffer empty */ |
| 4957 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4958 | FD_CLR(t->cli_fd, StaticWriteEvent); |
| 4959 | tv_eternity(&t->cwexpire); |
| 4960 | shutdown(t->cli_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4961 | /* We must ensure that the read part is still alive when switching |
| 4962 | * to shutw */ |
| 4963 | FD_SET(t->cli_fd, StaticReadEvent); |
| 4964 | if (t->proxy->clitimeout) |
| 4965 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4966 | t->cli_state = CL_STSHUTW; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4967 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4968 | return 1; |
| 4969 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4970 | /* read timeout */ |
| 4971 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 4972 | FD_CLR(t->cli_fd, StaticReadEvent); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4973 | tv_eternity(&t->crexpire); |
| 4974 | shutdown(t->cli_fd, SHUT_RD); |
| 4975 | t->cli_state = CL_STSHUTR; |
| 4976 | if (!(t->flags & SN_ERR_MASK)) |
| 4977 | t->flags |= SN_ERR_CLITO; |
willy tarreau | 078c79a | 2006-05-13 12:23:58 +0200 | [diff] [blame] | 4978 | if (!(t->flags & SN_FINST_MASK)) { |
| 4979 | if (t->pend_pos) |
| 4980 | t->flags |= SN_FINST_Q; |
| 4981 | else if (s == SV_STCONN) |
| 4982 | t->flags |= SN_FINST_C; |
| 4983 | else |
| 4984 | t->flags |= SN_FINST_D; |
| 4985 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4986 | return 1; |
| 4987 | } |
| 4988 | /* write timeout */ |
| 4989 | else if (tv_cmp2_ms(&t->cwexpire, &now) <= 0) { |
| 4990 | FD_CLR(t->cli_fd, StaticWriteEvent); |
| 4991 | tv_eternity(&t->cwexpire); |
| 4992 | shutdown(t->cli_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4993 | /* We must ensure that the read part is still alive when switching |
| 4994 | * to shutw */ |
| 4995 | FD_SET(t->cli_fd, StaticReadEvent); |
| 4996 | if (t->proxy->clitimeout) |
| 4997 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 4998 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4999 | t->cli_state = CL_STSHUTW; |
| 5000 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5001 | t->flags |= SN_ERR_CLITO; |
willy tarreau | 078c79a | 2006-05-13 12:23:58 +0200 | [diff] [blame] | 5002 | if (!(t->flags & SN_FINST_MASK)) { |
| 5003 | if (t->pend_pos) |
| 5004 | t->flags |= SN_FINST_Q; |
| 5005 | else if (s == SV_STCONN) |
| 5006 | t->flags |= SN_FINST_C; |
| 5007 | else |
| 5008 | t->flags |= SN_FINST_D; |
| 5009 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5010 | return 1; |
| 5011 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5012 | |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 5013 | if (req->l >= req->rlim - req->data) { |
| 5014 | /* no room to read more data */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5015 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5016 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5017 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 5018 | tv_eternity(&t->crexpire); |
| 5019 | } |
| 5020 | } |
| 5021 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5022 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5023 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 5024 | FD_SET(t->cli_fd, StaticReadEvent); |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 5025 | if (!t->proxy->clitimeout || |
| 5026 | (t->srv_state < SV_STDATA && t->proxy->srvtimeout)) |
| 5027 | /* If the client has no timeout, or if the server not ready yet, and we |
| 5028 | * know for sure that it can expire, then it's cleaner to disable the |
| 5029 | * timeout on the client side so that too low values cannot make the |
| 5030 | * sessions abort too early. |
| 5031 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5032 | tv_eternity(&t->crexpire); |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 5033 | else |
| 5034 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5035 | } |
| 5036 | } |
| 5037 | |
| 5038 | if ((rep->l == 0) || |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 5039 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5040 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 5041 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 5042 | tv_eternity(&t->cwexpire); |
| 5043 | } |
| 5044 | } |
| 5045 | else { /* buffer not empty */ |
| 5046 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 5047 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5048 | if (t->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5049 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
willy tarreau | 0889c96 | 2006-04-24 14:36:48 +0200 | [diff] [blame] | 5050 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 5051 | * we refresh it. */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5052 | t->crexpire = t->cwexpire; |
| 5053 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5054 | else |
| 5055 | tv_eternity(&t->cwexpire); |
| 5056 | } |
| 5057 | } |
| 5058 | return 0; /* other cases change nothing */ |
| 5059 | } |
| 5060 | else if (c == CL_STSHUTR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5061 | if (t->res_cw == RES_ERROR) { |
| 5062 | tv_eternity(&t->cwexpire); |
| 5063 | fd_delete(t->cli_fd); |
| 5064 | t->cli_state = CL_STCLOSE; |
| 5065 | if (!(t->flags & SN_ERR_MASK)) |
| 5066 | t->flags |= SN_ERR_CLICL; |
willy tarreau | 078c79a | 2006-05-13 12:23:58 +0200 | [diff] [blame] | 5067 | if (!(t->flags & SN_FINST_MASK)) { |
| 5068 | if (t->pend_pos) |
| 5069 | t->flags |= SN_FINST_Q; |
| 5070 | else if (s == SV_STCONN) |
| 5071 | t->flags |= SN_FINST_C; |
| 5072 | else |
| 5073 | t->flags |= SN_FINST_D; |
| 5074 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5075 | return 1; |
| 5076 | } |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 5077 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0) |
| 5078 | && !(t->flags & SN_SELF_GEN)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5079 | tv_eternity(&t->cwexpire); |
| 5080 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5081 | t->cli_state = CL_STCLOSE; |
| 5082 | return 1; |
| 5083 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5084 | else if (tv_cmp2_ms(&t->cwexpire, &now) <= 0) { |
| 5085 | tv_eternity(&t->cwexpire); |
| 5086 | fd_delete(t->cli_fd); |
| 5087 | t->cli_state = CL_STCLOSE; |
| 5088 | if (!(t->flags & SN_ERR_MASK)) |
| 5089 | t->flags |= SN_ERR_CLITO; |
willy tarreau | 078c79a | 2006-05-13 12:23:58 +0200 | [diff] [blame] | 5090 | if (!(t->flags & SN_FINST_MASK)) { |
| 5091 | if (t->pend_pos) |
| 5092 | t->flags |= SN_FINST_Q; |
| 5093 | else if (s == SV_STCONN) |
| 5094 | t->flags |= SN_FINST_C; |
| 5095 | else |
| 5096 | t->flags |= SN_FINST_D; |
| 5097 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5098 | return 1; |
| 5099 | } |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 5100 | |
| 5101 | if (t->flags & SN_SELF_GEN) { |
| 5102 | produce_content(t); |
| 5103 | if (rep->l == 0) { |
| 5104 | tv_eternity(&t->cwexpire); |
| 5105 | fd_delete(t->cli_fd); |
| 5106 | t->cli_state = CL_STCLOSE; |
| 5107 | return 1; |
| 5108 | } |
| 5109 | } |
| 5110 | |
| 5111 | if ((rep->l == 0) |
| 5112 | || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5113 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 5114 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 5115 | tv_eternity(&t->cwexpire); |
| 5116 | } |
| 5117 | } |
| 5118 | else { /* buffer not empty */ |
| 5119 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 5120 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5121 | if (t->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5122 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
willy tarreau | 0889c96 | 2006-04-24 14:36:48 +0200 | [diff] [blame] | 5123 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 5124 | * we refresh it. */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5125 | t->crexpire = t->cwexpire; |
| 5126 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5127 | else |
| 5128 | tv_eternity(&t->cwexpire); |
| 5129 | } |
| 5130 | } |
| 5131 | return 0; |
| 5132 | } |
| 5133 | else if (c == CL_STSHUTW) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5134 | if (t->res_cr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5135 | tv_eternity(&t->crexpire); |
| 5136 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5137 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5138 | if (!(t->flags & SN_ERR_MASK)) |
| 5139 | t->flags |= SN_ERR_CLICL; |
willy tarreau | 078c79a | 2006-05-13 12:23:58 +0200 | [diff] [blame] | 5140 | if (!(t->flags & SN_FINST_MASK)) { |
| 5141 | if (t->pend_pos) |
| 5142 | t->flags |= SN_FINST_Q; |
| 5143 | else if (s == SV_STCONN) |
| 5144 | t->flags |= SN_FINST_C; |
| 5145 | else |
| 5146 | t->flags |= SN_FINST_D; |
| 5147 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5148 | return 1; |
| 5149 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5150 | else if (t->res_cr == RES_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
| 5151 | tv_eternity(&t->crexpire); |
| 5152 | fd_delete(t->cli_fd); |
| 5153 | t->cli_state = CL_STCLOSE; |
| 5154 | return 1; |
| 5155 | } |
| 5156 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 5157 | tv_eternity(&t->crexpire); |
| 5158 | fd_delete(t->cli_fd); |
| 5159 | t->cli_state = CL_STCLOSE; |
| 5160 | if (!(t->flags & SN_ERR_MASK)) |
| 5161 | t->flags |= SN_ERR_CLITO; |
willy tarreau | 078c79a | 2006-05-13 12:23:58 +0200 | [diff] [blame] | 5162 | if (!(t->flags & SN_FINST_MASK)) { |
| 5163 | if (t->pend_pos) |
| 5164 | t->flags |= SN_FINST_Q; |
| 5165 | else if (s == SV_STCONN) |
| 5166 | t->flags |= SN_FINST_C; |
| 5167 | else |
| 5168 | t->flags |= SN_FINST_D; |
| 5169 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5170 | return 1; |
| 5171 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5172 | else if (req->l >= req->rlim - req->data) { |
| 5173 | /* no room to read more data */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 5174 | |
| 5175 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 5176 | * after the timeout by sending more data after it receives a close ? |
| 5177 | */ |
| 5178 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5179 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5180 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5181 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 5182 | tv_eternity(&t->crexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5183 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5184 | } |
| 5185 | } |
| 5186 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5187 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5188 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 5189 | FD_SET(t->cli_fd, StaticReadEvent); |
| 5190 | if (t->proxy->clitimeout) |
| 5191 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 5192 | else |
| 5193 | tv_eternity(&t->crexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5194 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5195 | } |
| 5196 | } |
| 5197 | return 0; |
| 5198 | } |
| 5199 | else { /* CL_STCLOSE: nothing to do */ |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5200 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5201 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 5202 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5203 | write(1, trash, len); |
| 5204 | } |
| 5205 | return 0; |
| 5206 | } |
| 5207 | return 0; |
| 5208 | } |
| 5209 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5210 | /* This function turns the server state into the SV_STCLOSE, and sets |
| 5211 | * indicators accordingly. Note that if <status> is 0, no message is |
| 5212 | * returned. |
| 5213 | */ |
| 5214 | void srv_close_with_err(struct session *t, int err, int finst, int status, int msglen, char *msg) { |
| 5215 | t->srv_state = SV_STCLOSE; |
| 5216 | if (status > 0) { |
| 5217 | t->logs.status = status; |
| 5218 | if (t->proxy->mode == PR_MODE_HTTP) |
| 5219 | client_return(t, msglen, msg); |
| 5220 | } |
| 5221 | if (!(t->flags & SN_ERR_MASK)) |
| 5222 | t->flags |= err; |
| 5223 | if (!(t->flags & SN_FINST_MASK)) |
| 5224 | t->flags |= finst; |
| 5225 | } |
| 5226 | |
| 5227 | /* |
| 5228 | * This function checks the retry count during the connect() job. |
| 5229 | * It updates the session's srv_state and retries, so that the caller knows |
| 5230 | * what it has to do. It uses the last connection error to set the log when |
| 5231 | * it expires. It returns 1 when it has expired, and 0 otherwise. |
| 5232 | */ |
| 5233 | int srv_count_retry_down(struct session *t, int conn_err) { |
| 5234 | /* we are in front of a retryable error */ |
| 5235 | t->conn_retries--; |
| 5236 | if (t->conn_retries < 0) { |
| 5237 | /* if not retryable anymore, let's abort */ |
| 5238 | tv_eternity(&t->cnexpire); |
| 5239 | srv_close_with_err(t, conn_err, SN_FINST_C, |
| 5240 | 503, t->proxy->errmsg.len503, t->proxy->errmsg.msg503); |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 5241 | if (t->srv) |
| 5242 | t->srv->failed_conns++; |
| 5243 | t->proxy->failed_conns++; |
| 5244 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5245 | /* We used to have a free connection slot. Since we'll never use it, |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 5246 | * we have to inform the server that it may be used by another session. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5247 | */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 5248 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 5249 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5250 | return 1; |
| 5251 | } |
| 5252 | return 0; |
| 5253 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5254 | |
| 5255 | /* |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5256 | * This function performs the retryable part of the connect() job. |
| 5257 | * It updates the session's srv_state and retries, so that the caller knows |
| 5258 | * what it has to do. It returns 1 when it breaks out of the loop, or 0 if |
| 5259 | * it needs to redispatch. |
| 5260 | */ |
| 5261 | int srv_retryable_connect(struct session *t) { |
| 5262 | int conn_err; |
| 5263 | |
| 5264 | /* This loop ensures that we stop before the last retry in case of a |
| 5265 | * redispatchable server. |
| 5266 | */ |
| 5267 | do { |
| 5268 | /* initiate a connection to the server */ |
| 5269 | conn_err = connect_server(t); |
| 5270 | switch (conn_err) { |
| 5271 | |
| 5272 | case SN_ERR_NONE: |
| 5273 | //fprintf(stderr,"0: c=%d, s=%d\n", c, s); |
| 5274 | t->srv_state = SV_STCONN; |
| 5275 | return 1; |
| 5276 | |
| 5277 | case SN_ERR_INTERNAL: |
| 5278 | tv_eternity(&t->cnexpire); |
| 5279 | srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C, |
| 5280 | 500, t->proxy->errmsg.len500, t->proxy->errmsg.msg500); |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 5281 | if (t->srv) |
| 5282 | t->srv->failed_conns++; |
| 5283 | t->proxy->failed_conns++; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5284 | /* release other sessions waiting for this server */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 5285 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 5286 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5287 | return 1; |
| 5288 | } |
| 5289 | /* ensure that we have enough retries left */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 5290 | if (srv_count_retry_down(t, conn_err)) { |
| 5291 | /* let's try to offer this slot to anybody */ |
| 5292 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 5293 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5294 | return 1; |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 5295 | } |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5296 | } while (t->srv == NULL || t->conn_retries > 0 || !(t->proxy->options & PR_O_REDISP)); |
| 5297 | |
| 5298 | /* We're on our last chance, and the REDISP option was specified. |
| 5299 | * We will ignore cookie and force to balance or use the dispatcher. |
| 5300 | */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 5301 | /* let's try to offer this slot to anybody */ |
| 5302 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 5303 | task_wakeup(&rq, t->srv->queue_mgt); |
| 5304 | |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 5305 | if (t->srv) |
| 5306 | t->srv->failed_conns++; |
| 5307 | t->proxy->failed_conns++; |
| 5308 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5309 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 5310 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
| 5311 | if ((t->flags & SN_CK_MASK) == SN_CK_VALID) { |
| 5312 | t->flags &= ~SN_CK_MASK; |
| 5313 | t->flags |= SN_CK_DOWN; |
| 5314 | } |
| 5315 | return 0; |
| 5316 | } |
| 5317 | |
| 5318 | /* This function performs the "redispatch" part of a connection attempt. It |
| 5319 | * will assign a server if required, queue the connection if required, and |
| 5320 | * handle errors that might arise at this level. It can change the server |
| 5321 | * state. It will return 1 if it encounters an error, switches the server |
| 5322 | * state, or has to queue a connection. Otherwise, it will return 0 indicating |
| 5323 | * that the connection is ready to use. |
| 5324 | */ |
| 5325 | |
| 5326 | int srv_redispatch_connect(struct session *t) { |
| 5327 | int conn_err; |
| 5328 | |
| 5329 | /* We know that we don't have any connection pending, so we will |
| 5330 | * try to get a new one, and wait in this state if it's queued |
| 5331 | */ |
| 5332 | conn_err = assign_server_and_queue(t); |
| 5333 | switch (conn_err) { |
| 5334 | case SRV_STATUS_OK: |
| 5335 | break; |
| 5336 | |
| 5337 | case SRV_STATUS_NOSRV: |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 5338 | /* note: it is guaranteed that t->srv == NULL here */ |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5339 | tv_eternity(&t->cnexpire); |
| 5340 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_C, |
| 5341 | 503, t->proxy->errmsg.len503, t->proxy->errmsg.msg503); |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 5342 | if (t->srv) |
| 5343 | t->srv->failed_conns++; |
| 5344 | t->proxy->failed_conns++; |
| 5345 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5346 | return 1; |
| 5347 | |
| 5348 | case SRV_STATUS_QUEUED: |
willy tarreau | 45526ed | 2006-05-03 20:11:50 +0200 | [diff] [blame] | 5349 | /* FIXME-20060503 : we should use the queue timeout instead */ |
| 5350 | if (t->proxy->contimeout) |
| 5351 | tv_delayfrom(&t->cnexpire, &now, t->proxy->contimeout); |
| 5352 | else |
| 5353 | tv_eternity(&t->cnexpire); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5354 | t->srv_state = SV_STIDLE; |
| 5355 | /* do nothing else and do not wake any other session up */ |
| 5356 | return 1; |
| 5357 | |
| 5358 | case SRV_STATUS_FULL: |
| 5359 | case SRV_STATUS_INTERNAL: |
| 5360 | default: |
| 5361 | tv_eternity(&t->cnexpire); |
| 5362 | srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C, |
| 5363 | 500, t->proxy->errmsg.len500, t->proxy->errmsg.msg500); |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 5364 | if (t->srv) |
| 5365 | t->srv->failed_conns++; |
| 5366 | t->proxy->failed_conns++; |
| 5367 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5368 | /* release other sessions waiting for this server */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 5369 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 5370 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5371 | return 1; |
| 5372 | } |
| 5373 | /* if we get here, it's because we got SRV_STATUS_OK, which also |
| 5374 | * means that the connection has not been queued. |
| 5375 | */ |
| 5376 | return 0; |
| 5377 | } |
| 5378 | |
| 5379 | |
| 5380 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5381 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 5382 | * (and a resync may be needed), 0 else. |
| 5383 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5384 | int process_srv(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5385 | int s = t->srv_state; |
| 5386 | int c = t->cli_state; |
| 5387 | struct buffer *req = t->req; |
| 5388 | struct buffer *rep = t->rep; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5389 | appsess *asession_temp = NULL; |
| 5390 | appsess local_asession; |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 5391 | int conn_err; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5392 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5393 | #ifdef DEBUG_FULL |
| 5394 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 5395 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5396 | //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 5397 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 5398 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 5399 | //); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5400 | if (s == SV_STIDLE) { |
| 5401 | if (c == CL_STHEADERS) |
| 5402 | return 0; /* stay in idle, waiting for data to reach the client side */ |
willy tarreau | 03a92de | 2006-05-21 18:26:53 +0200 | [diff] [blame^] | 5403 | else if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 5404 | (c == CL_STSHUTR && |
| 5405 | (t->req->l == 0 || t->proxy->options & PR_O_ABRT_CLOSE))) { /* give up */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5406 | tv_eternity(&t->cnexpire); |
willy tarreau | 424e04a | 2006-05-13 16:08:47 +0200 | [diff] [blame] | 5407 | if (t->pend_pos) |
| 5408 | t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 51e9129 | 2006-05-14 23:29:47 +0200 | [diff] [blame] | 5409 | /* note that this must not return any error because it would be able to |
| 5410 | * overwrite the client_retnclose() output. |
| 5411 | */ |
willy tarreau | 078c79a | 2006-05-13 12:23:58 +0200 | [diff] [blame] | 5412 | srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, 0, NULL); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5413 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5414 | return 1; |
| 5415 | } |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5416 | else { |
| 5417 | /* Right now, we will need to create a connection to the server. |
| 5418 | * We might already have tried, and got a connection pending, in |
| 5419 | * which case we will not do anything till it's pending. It's up |
| 5420 | * to any other session to release it and wake us up again. |
| 5421 | */ |
willy tarreau | 45526ed | 2006-05-03 20:11:50 +0200 | [diff] [blame] | 5422 | if (t->pend_pos) { |
| 5423 | if (tv_cmp2_ms(&t->cnexpire, &now) > 0) |
| 5424 | return 0; |
| 5425 | else { |
| 5426 | /* we've been waiting too long here */ |
| 5427 | tv_eternity(&t->cnexpire); |
willy tarreau | 078c79a | 2006-05-13 12:23:58 +0200 | [diff] [blame] | 5428 | t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now); |
| 5429 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
willy tarreau | 45526ed | 2006-05-03 20:11:50 +0200 | [diff] [blame] | 5430 | 503, t->proxy->errmsg.len503, t->proxy->errmsg.msg503); |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 5431 | if (t->srv) |
| 5432 | t->srv->failed_conns++; |
| 5433 | t->proxy->failed_conns++; |
willy tarreau | 45526ed | 2006-05-03 20:11:50 +0200 | [diff] [blame] | 5434 | return 1; |
| 5435 | } |
| 5436 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5437 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5438 | do { |
| 5439 | /* first, get a connection */ |
| 5440 | if (srv_redispatch_connect(t)) |
| 5441 | return t->srv_state != SV_STIDLE; |
| 5442 | |
| 5443 | /* try to (re-)connect to the server, and fail if we expire the |
| 5444 | * number of retries. |
| 5445 | */ |
willy tarreau | f32f524 | 2006-05-02 22:54:52 +0200 | [diff] [blame] | 5446 | if (srv_retryable_connect(t)) { |
| 5447 | t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5448 | return t->srv_state != SV_STIDLE; |
willy tarreau | f32f524 | 2006-05-02 22:54:52 +0200 | [diff] [blame] | 5449 | } |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5450 | |
| 5451 | } while (1); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5452 | } |
| 5453 | } |
| 5454 | else if (s == SV_STCONN) { /* connection in progress */ |
willy tarreau | 03a92de | 2006-05-21 18:26:53 +0200 | [diff] [blame^] | 5455 | if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 5456 | (c == CL_STSHUTR && |
| 5457 | (t->req->l == 0 || t->proxy->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 5458 | tv_eternity(&t->cnexpire); |
| 5459 | fd_delete(t->srv_fd); |
| 5460 | if (t->srv) |
| 5461 | t->srv->cur_sess--; |
| 5462 | |
| 5463 | /* note that this must not return any error because it would be able to |
| 5464 | * overwrite the client_retnclose() output. |
| 5465 | */ |
| 5466 | srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, 0, NULL); |
| 5467 | return 1; |
| 5468 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5469 | if (t->res_sw == RES_SILENT && tv_cmp2_ms(&t->cnexpire, &now) > 0) { |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 5470 | //fprintf(stderr,"1: c=%d, s=%d, now=%d.%06d, exp=%d.%06d\n", c, s, now.tv_sec, now.tv_usec, t->cnexpire.tv_sec, t->cnexpire.tv_usec); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5471 | return 0; /* nothing changed */ |
| 5472 | } |
| 5473 | else if (t->res_sw == RES_SILENT || t->res_sw == RES_ERROR) { |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5474 | /* timeout, asynchronous connect error or first write error */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5475 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5476 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5477 | fd_delete(t->srv_fd); |
willy tarreau | 926a357 | 2006-05-01 15:26:35 +0200 | [diff] [blame] | 5478 | if (t->srv) |
| 5479 | t->srv->cur_sess--; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5480 | |
| 5481 | if (t->res_sw == RES_SILENT) |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 5482 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 5483 | else |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5484 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 5485 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5486 | /* ensure that we have enough retries left */ |
| 5487 | if (srv_count_retry_down(t, conn_err)) |
| 5488 | return 1; |
| 5489 | |
| 5490 | do { |
| 5491 | /* Now we will try to either reconnect to the same server or |
| 5492 | * connect to another server. If the connection gets queued |
| 5493 | * because all servers are saturated, then we will go back to |
| 5494 | * the SV_STIDLE state. |
| 5495 | */ |
willy tarreau | f32f524 | 2006-05-02 22:54:52 +0200 | [diff] [blame] | 5496 | if (srv_retryable_connect(t)) { |
| 5497 | t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5498 | return t->srv_state != SV_STCONN; |
willy tarreau | f32f524 | 2006-05-02 22:54:52 +0200 | [diff] [blame] | 5499 | } |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5500 | |
| 5501 | /* we need to redispatch the connection to another server */ |
| 5502 | if (srv_redispatch_connect(t)) |
| 5503 | return t->srv_state != SV_STCONN; |
| 5504 | } while (1); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5505 | } |
| 5506 | else { /* no error or write 0 */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5507 | t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5508 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5509 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5510 | if (req->l == 0) /* nothing to write */ { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5511 | FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5512 | tv_eternity(&t->swexpire); |
| 5513 | } else /* need the right to write */ { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5514 | FD_SET(t->srv_fd, StaticWriteEvent); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5515 | if (t->proxy->srvtimeout) { |
| 5516 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | 0889c96 | 2006-04-24 14:36:48 +0200 | [diff] [blame] | 5517 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 5518 | * we refresh it. */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5519 | t->srexpire = t->swexpire; |
| 5520 | } |
| 5521 | else |
| 5522 | tv_eternity(&t->swexpire); |
| 5523 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5524 | |
| 5525 | if (t->proxy->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 5526 | FD_SET(t->srv_fd, StaticReadEvent); |
| 5527 | if (t->proxy->srvtimeout) |
| 5528 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 5529 | else |
| 5530 | tv_eternity(&t->srexpire); |
| 5531 | |
| 5532 | t->srv_state = SV_STDATA; |
willy tarreau | 2d505e5 | 2006-05-21 08:32:50 +0200 | [diff] [blame] | 5533 | if (t->srv) |
| 5534 | t->srv->cum_sess++; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5535 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 5536 | |
| 5537 | /* if the user wants to log as soon as possible, without counting |
| 5538 | bytes from the server, then this is the right moment. */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 5539 | if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) { |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 5540 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 5541 | sess_log(t); |
| 5542 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5543 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5544 | else { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5545 | t->srv_state = SV_STHEADERS; |
willy tarreau | 2d505e5 | 2006-05-21 08:32:50 +0200 | [diff] [blame] | 5546 | if (t->srv) |
| 5547 | t->srv->cum_sess++; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5548 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
| 5549 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5550 | tv_eternity(&t->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5551 | return 1; |
| 5552 | } |
| 5553 | } |
| 5554 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5555 | /* now parse the partial (or complete) headers */ |
| 5556 | while (rep->lr < rep->r) { /* this loop only sees one header at each iteration */ |
| 5557 | char *ptr; |
| 5558 | int delete_header; |
| 5559 | |
| 5560 | ptr = rep->lr; |
| 5561 | |
| 5562 | /* look for the end of the current header */ |
| 5563 | while (ptr < rep->r && *ptr != '\n' && *ptr != '\r') |
| 5564 | ptr++; |
| 5565 | |
| 5566 | if (ptr == rep->h) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5567 | int line, len; |
| 5568 | |
| 5569 | /* we can only get here after an end of headers */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 5570 | |
| 5571 | /* first, we'll block if security checks have caught nasty things */ |
| 5572 | if (t->flags & SN_CACHEABLE) { |
| 5573 | if ((t->flags & SN_CACHE_COOK) && |
| 5574 | (t->flags & SN_SCK_ANY) && |
| 5575 | (t->proxy->options & PR_O_CHK_CACHE)) { |
| 5576 | |
| 5577 | /* we're in presence of a cacheable response containing |
| 5578 | * a set-cookie header. We'll block it as requested by |
| 5579 | * the 'checkcache' option, and send an alert. |
| 5580 | */ |
| 5581 | tv_eternity(&t->srexpire); |
| 5582 | tv_eternity(&t->swexpire); |
| 5583 | fd_delete(t->srv_fd); |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 5584 | if (t->srv) { |
willy tarreau | 926a357 | 2006-05-01 15:26:35 +0200 | [diff] [blame] | 5585 | t->srv->cur_sess--; |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 5586 | t->srv->failed_secu++; |
| 5587 | } |
| 5588 | t->proxy->failed_secu++; |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 5589 | t->srv_state = SV_STCLOSE; |
| 5590 | t->logs.status = 502; |
| 5591 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
| 5592 | if (!(t->flags & SN_ERR_MASK)) |
| 5593 | t->flags |= SN_ERR_PRXCOND; |
| 5594 | if (!(t->flags & SN_FINST_MASK)) |
| 5595 | t->flags |= SN_FINST_H; |
| 5596 | |
| 5597 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id); |
| 5598 | send_log(t->proxy, LOG_ALERT, "Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id); |
| 5599 | |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5600 | /* We used to have a free connection slot. Since we'll never use it, |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 5601 | * we have to inform the server that it may be used by another session. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5602 | */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 5603 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 5604 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5605 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 5606 | return 1; |
| 5607 | } |
| 5608 | } |
| 5609 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5610 | /* next, we'll block if an 'rspideny' or 'rspdeny' filter matched */ |
| 5611 | if (t->flags & SN_SVDENY) { |
| 5612 | tv_eternity(&t->srexpire); |
| 5613 | tv_eternity(&t->swexpire); |
| 5614 | fd_delete(t->srv_fd); |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 5615 | if (t->srv) { |
willy tarreau | 926a357 | 2006-05-01 15:26:35 +0200 | [diff] [blame] | 5616 | t->srv->cur_sess--; |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 5617 | t->srv->failed_secu++; |
| 5618 | } |
| 5619 | t->proxy->failed_secu++; |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5620 | t->srv_state = SV_STCLOSE; |
| 5621 | t->logs.status = 502; |
| 5622 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
| 5623 | if (!(t->flags & SN_ERR_MASK)) |
| 5624 | t->flags |= SN_ERR_PRXCOND; |
| 5625 | if (!(t->flags & SN_FINST_MASK)) |
| 5626 | t->flags |= SN_FINST_H; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5627 | /* We used to have a free connection slot. Since we'll never use it, |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 5628 | * we have to inform the server that it may be used by another session. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5629 | */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 5630 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 5631 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 5632 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5633 | return 1; |
| 5634 | } |
| 5635 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5636 | /* we'll have something else to do here : add new headers ... */ |
| 5637 | |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 5638 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_INS) && |
| 5639 | (!(t->proxy->options & PR_O_COOK_POST) || (t->flags & SN_POST))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5640 | /* the server is known, it's not the one the client requested, we have to |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 5641 | * insert a set-cookie here, except if we want to insert only on POST |
willy tarreau | 4f7a101 | 2006-05-09 23:32:26 +0200 | [diff] [blame] | 5642 | * requests and this one isn't. Note that servers which don't have cookies |
| 5643 | * (eg: some backup servers) will return a full cookie removal request. |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5644 | */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5645 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n", |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 5646 | t->proxy->cookie_name, |
willy tarreau | 4f7a101 | 2006-05-09 23:32:26 +0200 | [diff] [blame] | 5647 | t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT"); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5648 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5649 | t->flags |= SN_SCK_INSERTED; |
| 5650 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5651 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5652 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5653 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5654 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5655 | */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 5656 | if (t->proxy->options & PR_O_COOK_NOC) |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5657 | //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n"); |
| 5658 | len += sprintf(trash + len, "Cache-control: private\r\n"); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5659 | |
| 5660 | if (rep->data + rep->l < rep->h) |
| 5661 | /* The data has been stolen, we will crash cleanly instead of corrupting memory */ |
| 5662 | *(int *)0 = 0; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5663 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5664 | } |
| 5665 | |
| 5666 | /* headers to be added */ |
| 5667 | for (line = 0; line < t->proxy->nb_rspadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5668 | len = sprintf(trash, "%s\r\n", t->proxy->rsp_add[line]); |
| 5669 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5670 | } |
| 5671 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 5672 | /* add a "connection: close" line if needed */ |
| 5673 | if (t->proxy->options & PR_O_HTTP_CLOSE) |
| 5674 | buffer_replace2(rep, rep->h, rep->h, "Connection: close\r\n", 19); |
| 5675 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5676 | t->srv_state = SV_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5677 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5678 | t->logs.t_data = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 5679 | |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 5680 | /* client connection already closed or option 'httpclose' required : |
| 5681 | * we close the server's outgoing connection right now. |
| 5682 | */ |
| 5683 | if ((req->l == 0) && |
| 5684 | (c == CL_STSHUTR || c == CL_STCLOSE || t->proxy->options & PR_O_FORCE_CLO)) { |
| 5685 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 5686 | tv_eternity(&t->swexpire); |
| 5687 | |
| 5688 | /* We must ensure that the read part is still alive when switching |
| 5689 | * to shutw */ |
| 5690 | FD_SET(t->srv_fd, StaticReadEvent); |
| 5691 | if (t->proxy->srvtimeout) |
| 5692 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 5693 | |
| 5694 | shutdown(t->srv_fd, SHUT_WR); |
| 5695 | t->srv_state = SV_STSHUTW; |
| 5696 | } |
| 5697 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 5698 | /* if the user wants to log as soon as possible, without counting |
| 5699 | bytes from the server, then this is the right moment. */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 5700 | if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) { |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 5701 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5702 | t->logs.bytes = rep->h - rep->data; |
| 5703 | sess_log(t); |
| 5704 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5705 | break; |
| 5706 | } |
| 5707 | |
| 5708 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 5709 | if (ptr > rep->r - 2) { |
| 5710 | /* this is a partial header, let's wait for more to come */ |
| 5711 | rep->lr = ptr; |
| 5712 | break; |
| 5713 | } |
| 5714 | |
| 5715 | // fprintf(stderr,"h=%p, ptr=%p, lr=%p, r=%p, *h=", rep->h, ptr, rep->lr, rep->r); |
| 5716 | // write(2, rep->h, ptr - rep->h); fprintf(stderr,"\n"); |
| 5717 | |
| 5718 | /* now we know that *ptr is either \r or \n, |
| 5719 | * and that there are at least 1 char after it. |
| 5720 | */ |
| 5721 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 5722 | rep->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 5723 | else |
| 5724 | rep->lr = ptr + 2; /* \r\n or \n\r */ |
| 5725 | |
| 5726 | /* |
| 5727 | * now we know that we have a full header ; we can do whatever |
| 5728 | * we want with these pointers : |
| 5729 | * rep->h = beginning of header |
| 5730 | * ptr = end of header (first \r or \n) |
| 5731 | * rep->lr = beginning of next line (next rep->h) |
| 5732 | * rep->r = end of data (not used at this stage) |
| 5733 | */ |
| 5734 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5735 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5736 | if (t->logs.status == -1) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5737 | t->logs.logwait &= ~LW_RESP; |
| 5738 | t->logs.status = atoi(rep->h + 9); |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5739 | switch (t->logs.status) { |
| 5740 | case 200: |
| 5741 | case 203: |
| 5742 | case 206: |
| 5743 | case 300: |
| 5744 | case 301: |
| 5745 | case 410: |
| 5746 | /* RFC2616 @13.4: |
| 5747 | * "A response received with a status code of |
| 5748 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5749 | * by a cache (...) unless a cache-control |
| 5750 | * directive prohibits caching." |
| 5751 | * |
| 5752 | * RFC2616 @9.5: POST method : |
| 5753 | * "Responses to this method are not cacheable, |
| 5754 | * unless the response includes appropriate |
| 5755 | * Cache-Control or Expires header fields." |
| 5756 | */ |
willy tarreau | 7476ec9 | 2006-05-21 16:24:15 +0200 | [diff] [blame] | 5757 | if (!(t->flags & SN_POST) && (t->proxy->options & PR_O_CHK_CACHE)) |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5758 | t->flags |= SN_CACHEABLE | SN_CACHE_COOK; |
| 5759 | break; |
| 5760 | default: |
| 5761 | break; |
| 5762 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 5763 | } |
| 5764 | else if (t->logs.logwait & LW_RSPHDR) { |
| 5765 | struct cap_hdr *h; |
| 5766 | int len; |
| 5767 | for (h = t->proxy->rsp_cap; h; h = h->next) { |
| 5768 | if ((h->namelen + 2 <= ptr - rep->h) && |
| 5769 | (rep->h[h->namelen] == ':') && |
| 5770 | (strncasecmp(rep->h, h->name, h->namelen) == 0)) { |
| 5771 | |
| 5772 | if (t->rsp_cap[h->index] == NULL) |
| 5773 | t->rsp_cap[h->index] = pool_alloc_from(h->pool, h->len + 1); |
| 5774 | |
| 5775 | len = ptr - (rep->h + h->namelen + 2); |
| 5776 | if (len > h->len) |
| 5777 | len = h->len; |
| 5778 | |
| 5779 | memcpy(t->rsp_cap[h->index], rep->h + h->namelen + 2, len); |
| 5780 | t->rsp_cap[h->index][len]=0; |
| 5781 | } |
| 5782 | } |
| 5783 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5784 | } |
| 5785 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5786 | delete_header = 0; |
| 5787 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5788 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5789 | int len, max; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 5790 | len = sprintf(trash, "%08x:%s.srvhdr[%04x:%04x]: ", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5791 | max = ptr - rep->h; |
| 5792 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5793 | len += strlcpy2(trash + len, rep->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5794 | trash[len++] = '\n'; |
| 5795 | write(1, trash, len); |
| 5796 | } |
| 5797 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 5798 | /* remove "connection: " if needed */ |
| 5799 | if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE) |
| 5800 | && (strncasecmp(rep->h, "Connection: ", 12) == 0)) { |
| 5801 | delete_header = 1; |
| 5802 | } |
| 5803 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5804 | /* try headers regexps */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 5805 | if (!delete_header && t->proxy->rsp_exp != NULL |
| 5806 | && !(t->flags & SN_SVDENY)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 5807 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5808 | char term; |
| 5809 | |
| 5810 | term = *ptr; |
| 5811 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 5812 | exp = t->proxy->rsp_exp; |
| 5813 | do { |
| 5814 | if (regexec(exp->preg, rep->h, MAX_MATCH, pmatch, 0) == 0) { |
| 5815 | switch (exp->action) { |
| 5816 | case ACT_ALLOW: |
| 5817 | if (!(t->flags & SN_SVDENY)) |
| 5818 | t->flags |= SN_SVALLOW; |
| 5819 | break; |
| 5820 | case ACT_REPLACE: |
| 5821 | if (!(t->flags & SN_SVDENY)) { |
| 5822 | int len = exp_replace(trash, rep->h, exp->replace, pmatch); |
| 5823 | ptr += buffer_replace2(rep, rep->h, ptr, trash, len); |
| 5824 | } |
| 5825 | break; |
| 5826 | case ACT_REMOVE: |
| 5827 | if (!(t->flags & SN_SVDENY)) |
| 5828 | delete_header = 1; |
| 5829 | break; |
| 5830 | case ACT_DENY: |
| 5831 | if (!(t->flags & SN_SVALLOW)) |
| 5832 | t->flags |= SN_SVDENY; |
| 5833 | break; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5834 | case ACT_PASS: /* we simply don't deny this one */ |
| 5835 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5836 | } |
| 5837 | break; |
| 5838 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 5839 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5840 | *ptr = term; /* restore the string terminator */ |
| 5841 | } |
| 5842 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 5843 | /* check for cache-control: or pragma: headers */ |
| 5844 | if (!delete_header && (t->flags & SN_CACHEABLE)) { |
| 5845 | if (strncasecmp(rep->h, "Pragma: no-cache", 16) == 0) |
| 5846 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 5847 | else if (strncasecmp(rep->h, "Cache-control: ", 15) == 0) { |
| 5848 | if (strncasecmp(rep->h + 15, "no-cache", 8) == 0) { |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5849 | if (rep->h + 23 == ptr || rep->h[23] == ',') |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 5850 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 5851 | else { |
| 5852 | if (strncasecmp(rep->h + 23, "=\"set-cookie", 12) == 0 |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5853 | && (rep->h[35] == '"' || rep->h[35] == ',')) |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 5854 | t->flags &= ~SN_CACHE_COOK; |
| 5855 | } |
| 5856 | } else if ((strncasecmp(rep->h + 15, "private", 7) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5857 | (rep->h + 22 == ptr || rep->h[22] == ',')) |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 5858 | || (strncasecmp(rep->h + 15, "no-store", 8) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5859 | (rep->h + 23 == ptr || rep->h[23] == ','))) { |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 5860 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 5861 | } else if (strncasecmp(rep->h + 15, "max-age=0", 9) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5862 | (rep->h + 24 == ptr || rep->h[24] == ',')) { |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 5863 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5864 | } else if (strncasecmp(rep->h + 15, "s-maxage=0", 10) == 0 && |
| 5865 | (rep->h + 25 == ptr || rep->h[25] == ',')) { |
| 5866 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 5867 | } else if (strncasecmp(rep->h + 15, "public", 6) == 0 && |
| 5868 | (rep->h + 21 == ptr || rep->h[21] == ',')) { |
| 5869 | t->flags |= SN_CACHEABLE | SN_CACHE_COOK; |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 5870 | } |
| 5871 | } |
| 5872 | } |
| 5873 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5874 | /* check for server cookies */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 5875 | if (!delete_header /*&& (t->proxy->options & PR_O_COOK_ANY)*/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5876 | && (t->proxy->cookie_name != NULL || t->proxy->capture_name != NULL || t->proxy->appsession_name !=NULL) |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 5877 | && (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5878 | char *p1, *p2, *p3, *p4; |
| 5879 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 5880 | t->flags |= SN_SCK_ANY; |
| 5881 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5882 | p1 = rep->h + 12; /* first char after 'Set-Cookie: ' */ |
| 5883 | |
| 5884 | while (p1 < ptr) { /* in fact, we'll break after the first cookie */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 5885 | while (p1 < ptr && (isspace((int)*p1))) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5886 | p1++; |
| 5887 | |
| 5888 | if (p1 == ptr || *p1 == ';') /* end of cookie */ |
| 5889 | break; |
| 5890 | |
| 5891 | /* p1 is at the beginning of the cookie name */ |
| 5892 | p2 = p1; |
| 5893 | |
| 5894 | while (p2 < ptr && *p2 != '=' && *p2 != ';') |
| 5895 | p2++; |
| 5896 | |
| 5897 | if (p2 == ptr || *p2 == ';') /* next cookie */ |
| 5898 | break; |
| 5899 | |
| 5900 | p3 = p2 + 1; /* skips the '=' sign */ |
| 5901 | if (p3 == ptr) |
| 5902 | break; |
| 5903 | |
| 5904 | p4 = p3; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 5905 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5906 | p4++; |
| 5907 | |
| 5908 | /* here, we have the cookie name between p1 and p2, |
| 5909 | * and its value between p3 and p4. |
| 5910 | * we can process it. |
| 5911 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 5912 | |
| 5913 | /* first, let's see if we want to capture it */ |
| 5914 | if (t->proxy->capture_name != NULL && |
| 5915 | t->logs.srv_cookie == NULL && |
| 5916 | (p4 - p1 >= t->proxy->capture_namelen) && |
| 5917 | memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) { |
| 5918 | int log_len = p4 - p1; |
| 5919 | |
| 5920 | if ((t->logs.srv_cookie = pool_alloc(capture)) == NULL) { |
| 5921 | Alert("HTTP logging : out of memory.\n"); |
| 5922 | } |
| 5923 | |
| 5924 | if (log_len > t->proxy->capture_len) |
| 5925 | log_len = t->proxy->capture_len; |
| 5926 | memcpy(t->logs.srv_cookie, p1, log_len); |
| 5927 | t->logs.srv_cookie[log_len] = 0; |
| 5928 | } |
| 5929 | |
| 5930 | if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) && |
| 5931 | (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5932 | /* Cool... it's the right one */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5933 | t->flags |= SN_SCK_SEEN; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5934 | |
| 5935 | /* If the cookie is in insert mode on a known server, we'll delete |
| 5936 | * this occurrence because we'll insert another one later. |
| 5937 | * We'll delete it too if the "indirect" option is set and we're in |
| 5938 | * a direct access. */ |
| 5939 | if (((t->srv) && (t->proxy->options & PR_O_COOK_INS)) || |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 5940 | ((t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_IND))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5941 | /* this header must be deleted */ |
| 5942 | delete_header = 1; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5943 | t->flags |= SN_SCK_DELETED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5944 | } |
| 5945 | else if ((t->srv) && (t->proxy->options & PR_O_COOK_RW)) { |
| 5946 | /* replace bytes p3->p4 with the cookie name associated |
| 5947 | * with this server since we know it. |
| 5948 | */ |
| 5949 | buffer_replace2(rep, p3, p4, t->srv->cookie, t->srv->cklen); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5950 | t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5951 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5952 | else if ((t->srv) && (t->proxy->options & PR_O_COOK_PFX)) { |
| 5953 | /* insert the cookie name associated with this server |
| 5954 | * before existing cookie, and insert a delimitor between them.. |
| 5955 | */ |
| 5956 | buffer_replace2(rep, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5957 | p3[t->srv->cklen] = COOKIE_DELIM; |
| 5958 | t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED; |
| 5959 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5960 | break; |
| 5961 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5962 | |
| 5963 | /* first, let's see if the cookie is our appcookie*/ |
| 5964 | if ((t->proxy->appsession_name != NULL) && |
| 5965 | (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) { |
| 5966 | |
| 5967 | /* Cool... it's the right one */ |
| 5968 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5969 | size_t server_id_len = strlen(t->srv->id) + 1; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5970 | asession_temp = &local_asession; |
| 5971 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5972 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5973 | Alert("Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 5974 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 5975 | } |
| 5976 | memcpy(asession_temp->sessid, p3, t->proxy->appsession_len); |
| 5977 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 5978 | asession_temp->serverid = NULL; |
| 5979 | |
| 5980 | /* only do insert, if lookup fails */ |
| 5981 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) { |
| 5982 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 5983 | Alert("Not enought Memory process_srv():asession:calloc().\n"); |
| 5984 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession:calloc().\n"); |
| 5985 | return 0; |
| 5986 | } |
| 5987 | asession_temp->sessid = local_asession.sessid; |
| 5988 | asession_temp->serverid = local_asession.serverid; |
| 5989 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5990 | }/* end if (chtbl_lookup()) */ |
| 5991 | else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5992 | /* free wasted memory */ |
| 5993 | pool_free_to(apools.sessid, local_asession.sessid); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5994 | } /* end else from if (chtbl_lookup()) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5995 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5996 | if (asession_temp->serverid == NULL) { |
| 5997 | if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5998 | Alert("Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 5999 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 6000 | } |
| 6001 | asession_temp->serverid[0] = '\0'; |
| 6002 | } |
| 6003 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6004 | if (asession_temp->serverid[0] == '\0') |
| 6005 | memcpy(asession_temp->serverid,t->srv->id,server_id_len); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6006 | |
| 6007 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
| 6008 | |
| 6009 | #if defined(DEBUG_HASH) |
| 6010 | print_table(&(t->proxy->htbl_proxy)); |
| 6011 | #endif |
| 6012 | break; |
| 6013 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6014 | else { |
| 6015 | // fprintf(stderr,"Ignoring unknown cookie : "); |
| 6016 | // write(2, p1, p2-p1); |
| 6017 | // fprintf(stderr," = "); |
| 6018 | // write(2, p3, p4-p3); |
| 6019 | // fprintf(stderr,"\n"); |
| 6020 | } |
| 6021 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 6022 | } /* we're now at the end of the cookie value */ |
| 6023 | } /* end of cookie processing */ |
| 6024 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 6025 | /* check for any set-cookie in case we check for cacheability */ |
| 6026 | if (!delete_header && !(t->flags & SN_SCK_ANY) && |
| 6027 | (t->proxy->options & PR_O_CHK_CACHE) && |
| 6028 | (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) { |
| 6029 | t->flags |= SN_SCK_ANY; |
| 6030 | } |
| 6031 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6032 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 6033 | if (delete_header && !(t->flags & SN_SVDENY)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6034 | buffer_replace2(rep, rep->h, rep->lr, "", 0); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 6035 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6036 | rep->h = rep->lr; |
| 6037 | } /* while (rep->lr < rep->r) */ |
| 6038 | |
| 6039 | /* end of header processing (even if incomplete) */ |
| 6040 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 6041 | if ((rep->l < rep->rlim - rep->data) && ! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 6042 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 6043 | * full. We cannot loop here since event_srv_read will disable it only if |
| 6044 | * rep->l == rlim-data |
| 6045 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6046 | FD_SET(t->srv_fd, StaticReadEvent); |
| 6047 | if (t->proxy->srvtimeout) |
| 6048 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 6049 | else |
| 6050 | tv_eternity(&t->srexpire); |
| 6051 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6052 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6053 | /* read error, write error */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6054 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6055 | tv_eternity(&t->srexpire); |
| 6056 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6057 | fd_delete(t->srv_fd); |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 6058 | if (t->srv) { |
willy tarreau | 926a357 | 2006-05-01 15:26:35 +0200 | [diff] [blame] | 6059 | t->srv->cur_sess--; |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 6060 | t->srv->failed_resp++; |
| 6061 | } |
| 6062 | t->proxy->failed_resp++; |
| 6063 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6064 | t->srv_state = SV_STCLOSE; |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 6065 | t->logs.status = 502; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6066 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6067 | if (!(t->flags & SN_ERR_MASK)) |
| 6068 | t->flags |= SN_ERR_SRVCL; |
| 6069 | if (!(t->flags & SN_FINST_MASK)) |
| 6070 | t->flags |= SN_FINST_H; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6071 | /* We used to have a free connection slot. Since we'll never use it, |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6072 | * we have to inform the server that it may be used by another session. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6073 | */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6074 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 6075 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6076 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6077 | return 1; |
| 6078 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6079 | /* end of client write or end of server read. |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 6080 | * since we are in header mode, if there's no space left for headers, we |
| 6081 | * won't be able to free more later, so the session will never terminate. |
| 6082 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6083 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE || rep->l >= rep->rlim - rep->data) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6084 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 6085 | tv_eternity(&t->srexpire); |
| 6086 | shutdown(t->srv_fd, SHUT_RD); |
| 6087 | t->srv_state = SV_STSHUTR; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6088 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6089 | return 1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6090 | } |
| 6091 | /* read timeout : return a 504 to the client. |
| 6092 | */ |
| 6093 | else if (FD_ISSET(t->srv_fd, StaticReadEvent) && tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 6094 | tv_eternity(&t->srexpire); |
| 6095 | tv_eternity(&t->swexpire); |
| 6096 | fd_delete(t->srv_fd); |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 6097 | if (t->srv) { |
willy tarreau | 926a357 | 2006-05-01 15:26:35 +0200 | [diff] [blame] | 6098 | t->srv->cur_sess--; |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 6099 | t->srv->failed_resp++; |
| 6100 | } |
| 6101 | t->proxy->failed_resp++; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6102 | t->srv_state = SV_STCLOSE; |
| 6103 | t->logs.status = 504; |
| 6104 | client_return(t, t->proxy->errmsg.len504, t->proxy->errmsg.msg504); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6105 | if (!(t->flags & SN_ERR_MASK)) |
| 6106 | t->flags |= SN_ERR_SRVTO; |
| 6107 | if (!(t->flags & SN_FINST_MASK)) |
| 6108 | t->flags |= SN_FINST_H; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6109 | /* We used to have a free connection slot. Since we'll never use it, |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6110 | * we have to inform the server that it may be used by another session. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6111 | */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6112 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 6113 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6114 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6115 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6116 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6117 | /* last client read and buffer empty */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 6118 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 6119 | * client shuts read too early, because we may still have |
| 6120 | * some work to do on the headers. |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6121 | * The side-effect is that if the client completely closes its |
| 6122 | * connection during SV_STHEADER, the connection to the server |
| 6123 | * is kept until a response comes back or the timeout is reached. |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 6124 | */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6125 | else if ((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && (req->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6126 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 6127 | tv_eternity(&t->swexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6128 | |
| 6129 | /* We must ensure that the read part is still alive when switching |
| 6130 | * to shutw */ |
| 6131 | FD_SET(t->srv_fd, StaticReadEvent); |
| 6132 | if (t->proxy->srvtimeout) |
| 6133 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 6134 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6135 | shutdown(t->srv_fd, SHUT_WR); |
| 6136 | t->srv_state = SV_STSHUTW; |
| 6137 | return 1; |
| 6138 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6139 | /* write timeout */ |
| 6140 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 6141 | * client shuts read too early, because we may still have |
| 6142 | * some work to do on the headers. |
| 6143 | */ |
| 6144 | else if (FD_ISSET(t->srv_fd, StaticWriteEvent) && tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
| 6145 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 6146 | tv_eternity(&t->swexpire); |
| 6147 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6148 | /* We must ensure that the read part is still alive when switching |
| 6149 | * to shutw */ |
| 6150 | FD_SET(t->srv_fd, StaticReadEvent); |
| 6151 | if (t->proxy->srvtimeout) |
| 6152 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 6153 | |
| 6154 | /* We must ensure that the read part is still alive when switching |
| 6155 | * to shutw */ |
| 6156 | FD_SET(t->srv_fd, StaticReadEvent); |
| 6157 | if (t->proxy->srvtimeout) |
| 6158 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 6159 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6160 | t->srv_state = SV_STSHUTW; |
| 6161 | if (!(t->flags & SN_ERR_MASK)) |
| 6162 | t->flags |= SN_ERR_SRVTO; |
| 6163 | if (!(t->flags & SN_FINST_MASK)) |
| 6164 | t->flags |= SN_FINST_H; |
| 6165 | return 1; |
| 6166 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6167 | |
| 6168 | if (req->l == 0) { |
| 6169 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 6170 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 6171 | tv_eternity(&t->swexpire); |
| 6172 | } |
| 6173 | } |
| 6174 | else { /* client buffer not empty */ |
| 6175 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 6176 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 6177 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6178 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | 0889c96 | 2006-04-24 14:36:48 +0200 | [diff] [blame] | 6179 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 6180 | * we refresh it. */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 6181 | t->srexpire = t->swexpire; |
| 6182 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6183 | else |
| 6184 | tv_eternity(&t->swexpire); |
| 6185 | } |
| 6186 | } |
| 6187 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6188 | /* be nice with the client side which would like to send a complete header |
| 6189 | * FIXME: COMPLETELY BUGGY !!! not all headers may be processed because the client |
| 6190 | * would read all remaining data at once ! The client should not write past rep->lr |
| 6191 | * when the server is in header state. |
| 6192 | */ |
| 6193 | //return header_processed; |
| 6194 | return t->srv_state != SV_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6195 | } |
| 6196 | else if (s == SV_STDATA) { |
| 6197 | /* read or write error */ |
| 6198 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6199 | tv_eternity(&t->srexpire); |
| 6200 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6201 | fd_delete(t->srv_fd); |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 6202 | if (t->srv) { |
willy tarreau | 926a357 | 2006-05-01 15:26:35 +0200 | [diff] [blame] | 6203 | t->srv->cur_sess--; |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 6204 | t->srv->failed_resp++; |
| 6205 | } |
| 6206 | t->proxy->failed_resp++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6207 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6208 | if (!(t->flags & SN_ERR_MASK)) |
| 6209 | t->flags |= SN_ERR_SRVCL; |
| 6210 | if (!(t->flags & SN_FINST_MASK)) |
| 6211 | t->flags |= SN_FINST_D; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6212 | /* We used to have a free connection slot. Since we'll never use it, |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6213 | * we have to inform the server that it may be used by another session. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6214 | */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6215 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 6216 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6217 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6218 | return 1; |
| 6219 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6220 | /* last read, or end of client write */ |
| 6221 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6222 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 6223 | tv_eternity(&t->srexpire); |
| 6224 | shutdown(t->srv_fd, SHUT_RD); |
| 6225 | t->srv_state = SV_STSHUTR; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6226 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6227 | return 1; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6228 | } |
| 6229 | /* end of client read and no more data to send */ |
| 6230 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
| 6231 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 6232 | tv_eternity(&t->swexpire); |
| 6233 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6234 | /* We must ensure that the read part is still alive when switching |
| 6235 | * to shutw */ |
| 6236 | FD_SET(t->srv_fd, StaticReadEvent); |
| 6237 | if (t->proxy->srvtimeout) |
| 6238 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 6239 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6240 | t->srv_state = SV_STSHUTW; |
| 6241 | return 1; |
| 6242 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6243 | /* read timeout */ |
| 6244 | else if (tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 6245 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 6246 | tv_eternity(&t->srexpire); |
| 6247 | shutdown(t->srv_fd, SHUT_RD); |
| 6248 | t->srv_state = SV_STSHUTR; |
| 6249 | if (!(t->flags & SN_ERR_MASK)) |
| 6250 | t->flags |= SN_ERR_SRVTO; |
| 6251 | if (!(t->flags & SN_FINST_MASK)) |
| 6252 | t->flags |= SN_FINST_D; |
| 6253 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6254 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6255 | /* write timeout */ |
| 6256 | else if (tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6257 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 6258 | tv_eternity(&t->swexpire); |
| 6259 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6260 | /* We must ensure that the read part is still alive when switching |
| 6261 | * to shutw */ |
| 6262 | FD_SET(t->srv_fd, StaticReadEvent); |
| 6263 | if (t->proxy->srvtimeout) |
| 6264 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6265 | t->srv_state = SV_STSHUTW; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6266 | if (!(t->flags & SN_ERR_MASK)) |
| 6267 | t->flags |= SN_ERR_SRVTO; |
| 6268 | if (!(t->flags & SN_FINST_MASK)) |
| 6269 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6270 | return 1; |
| 6271 | } |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 6272 | |
| 6273 | /* recompute request time-outs */ |
| 6274 | if (req->l == 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6275 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 6276 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 6277 | tv_eternity(&t->swexpire); |
| 6278 | } |
| 6279 | } |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 6280 | else { /* buffer not empty, there are still data to be transferred */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6281 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 6282 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 6283 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6284 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | 0889c96 | 2006-04-24 14:36:48 +0200 | [diff] [blame] | 6285 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 6286 | * we refresh it. */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 6287 | t->srexpire = t->swexpire; |
| 6288 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6289 | else |
| 6290 | tv_eternity(&t->swexpire); |
| 6291 | } |
| 6292 | } |
| 6293 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 6294 | /* recompute response time-outs */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6295 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 6296 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 6297 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 6298 | tv_eternity(&t->srexpire); |
| 6299 | } |
| 6300 | } |
| 6301 | else { |
| 6302 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 6303 | FD_SET(t->srv_fd, StaticReadEvent); |
| 6304 | if (t->proxy->srvtimeout) |
| 6305 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 6306 | else |
| 6307 | tv_eternity(&t->srexpire); |
| 6308 | } |
| 6309 | } |
| 6310 | |
| 6311 | return 0; /* other cases change nothing */ |
| 6312 | } |
| 6313 | else if (s == SV_STSHUTR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6314 | if (t->res_sw == RES_ERROR) { |
| 6315 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
| 6316 | tv_eternity(&t->swexpire); |
| 6317 | fd_delete(t->srv_fd); |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 6318 | if (t->srv) { |
willy tarreau | 926a357 | 2006-05-01 15:26:35 +0200 | [diff] [blame] | 6319 | t->srv->cur_sess--; |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 6320 | t->srv->failed_resp++; |
| 6321 | } |
| 6322 | t->proxy->failed_resp++; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6323 | //close(t->srv_fd); |
| 6324 | t->srv_state = SV_STCLOSE; |
| 6325 | if (!(t->flags & SN_ERR_MASK)) |
| 6326 | t->flags |= SN_ERR_SRVCL; |
| 6327 | if (!(t->flags & SN_FINST_MASK)) |
| 6328 | t->flags |= SN_FINST_D; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6329 | /* We used to have a free connection slot. Since we'll never use it, |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6330 | * we have to inform the server that it may be used by another session. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6331 | */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6332 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 6333 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6334 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6335 | return 1; |
| 6336 | } |
| 6337 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6338 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6339 | tv_eternity(&t->swexpire); |
| 6340 | fd_delete(t->srv_fd); |
willy tarreau | 926a357 | 2006-05-01 15:26:35 +0200 | [diff] [blame] | 6341 | if (t->srv) |
| 6342 | t->srv->cur_sess--; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6343 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6344 | t->srv_state = SV_STCLOSE; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6345 | /* We used to have a free connection slot. Since we'll never use it, |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6346 | * we have to inform the server that it may be used by another session. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6347 | */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6348 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 6349 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6350 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6351 | return 1; |
| 6352 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6353 | else if (tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
| 6354 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
| 6355 | tv_eternity(&t->swexpire); |
| 6356 | fd_delete(t->srv_fd); |
willy tarreau | 926a357 | 2006-05-01 15:26:35 +0200 | [diff] [blame] | 6357 | if (t->srv) |
| 6358 | t->srv->cur_sess--; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6359 | //close(t->srv_fd); |
| 6360 | t->srv_state = SV_STCLOSE; |
| 6361 | if (!(t->flags & SN_ERR_MASK)) |
| 6362 | t->flags |= SN_ERR_SRVTO; |
| 6363 | if (!(t->flags & SN_FINST_MASK)) |
| 6364 | t->flags |= SN_FINST_D; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6365 | /* We used to have a free connection slot. Since we'll never use it, |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6366 | * we have to inform the server that it may be used by another session. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6367 | */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6368 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 6369 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6370 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6371 | return 1; |
| 6372 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6373 | else if (req->l == 0) { |
| 6374 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 6375 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 6376 | tv_eternity(&t->swexpire); |
| 6377 | } |
| 6378 | } |
| 6379 | else { /* buffer not empty */ |
| 6380 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 6381 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 6382 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6383 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | 0889c96 | 2006-04-24 14:36:48 +0200 | [diff] [blame] | 6384 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 6385 | * we refresh it. */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 6386 | t->srexpire = t->swexpire; |
| 6387 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6388 | else |
| 6389 | tv_eternity(&t->swexpire); |
| 6390 | } |
| 6391 | } |
| 6392 | return 0; |
| 6393 | } |
| 6394 | else if (s == SV_STSHUTW) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6395 | if (t->res_sr == RES_ERROR) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6396 | //FD_CLR(t->srv_fd, StaticReadEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6397 | tv_eternity(&t->srexpire); |
| 6398 | fd_delete(t->srv_fd); |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 6399 | if (t->srv) { |
willy tarreau | 926a357 | 2006-05-01 15:26:35 +0200 | [diff] [blame] | 6400 | t->srv->cur_sess--; |
willy tarreau | e3b3065 | 2006-05-21 16:23:22 +0200 | [diff] [blame] | 6401 | t->srv->failed_resp++; |
| 6402 | } |
| 6403 | t->proxy->failed_resp++; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6404 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6405 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6406 | if (!(t->flags & SN_ERR_MASK)) |
| 6407 | t->flags |= SN_ERR_SRVCL; |
| 6408 | if (!(t->flags & SN_FINST_MASK)) |
| 6409 | t->flags |= SN_FINST_D; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6410 | /* We used to have a free connection slot. Since we'll never use it, |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6411 | * we have to inform the server that it may be used by another session. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6412 | */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6413 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 6414 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6415 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6416 | return 1; |
| 6417 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6418 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
| 6419 | //FD_CLR(t->srv_fd, StaticReadEvent); |
| 6420 | tv_eternity(&t->srexpire); |
| 6421 | fd_delete(t->srv_fd); |
willy tarreau | 926a357 | 2006-05-01 15:26:35 +0200 | [diff] [blame] | 6422 | if (t->srv) |
| 6423 | t->srv->cur_sess--; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6424 | //close(t->srv_fd); |
| 6425 | t->srv_state = SV_STCLOSE; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6426 | /* We used to have a free connection slot. Since we'll never use it, |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6427 | * we have to inform the server that it may be used by another session. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6428 | */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6429 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 6430 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6431 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6432 | return 1; |
| 6433 | } |
| 6434 | else if (tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 6435 | //FD_CLR(t->srv_fd, StaticReadEvent); |
| 6436 | tv_eternity(&t->srexpire); |
| 6437 | fd_delete(t->srv_fd); |
willy tarreau | 926a357 | 2006-05-01 15:26:35 +0200 | [diff] [blame] | 6438 | if (t->srv) |
| 6439 | t->srv->cur_sess--; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6440 | //close(t->srv_fd); |
| 6441 | t->srv_state = SV_STCLOSE; |
| 6442 | if (!(t->flags & SN_ERR_MASK)) |
| 6443 | t->flags |= SN_ERR_SRVTO; |
| 6444 | if (!(t->flags & SN_FINST_MASK)) |
| 6445 | t->flags |= SN_FINST_D; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6446 | /* We used to have a free connection slot. Since we'll never use it, |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6447 | * we have to inform the server that it may be used by another session. |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6448 | */ |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6449 | if (may_dequeue_tasks(t->srv, t->proxy)) |
| 6450 | task_wakeup(&rq, t->srv->queue_mgt); |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 6451 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6452 | return 1; |
| 6453 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6454 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 6455 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 6456 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 6457 | tv_eternity(&t->srexpire); |
| 6458 | } |
| 6459 | } |
| 6460 | else { |
| 6461 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 6462 | FD_SET(t->srv_fd, StaticReadEvent); |
| 6463 | if (t->proxy->srvtimeout) |
| 6464 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 6465 | else |
| 6466 | tv_eternity(&t->srexpire); |
| 6467 | } |
| 6468 | } |
| 6469 | return 0; |
| 6470 | } |
| 6471 | else { /* SV_STCLOSE : nothing to do */ |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 6472 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6473 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 6474 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6475 | write(1, trash, len); |
| 6476 | } |
| 6477 | return 0; |
| 6478 | } |
| 6479 | return 0; |
| 6480 | } |
| 6481 | |
| 6482 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6483 | /* Processes the client and server jobs of a session task, then |
| 6484 | * puts it back to the wait queue in a clean state, or |
| 6485 | * cleans up its resources if it must be deleted. Returns |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6486 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 6487 | * infinity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6488 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6489 | int process_session(struct task *t) { |
| 6490 | struct session *s = t->context; |
| 6491 | int fsm_resync = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6492 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6493 | do { |
| 6494 | fsm_resync = 0; |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 6495 | //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6496 | fsm_resync |= process_cli(s); |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 6497 | //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6498 | fsm_resync |= process_srv(s); |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 6499 | //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6500 | } while (fsm_resync); |
| 6501 | |
| 6502 | if (s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6503 | struct timeval min1, min2; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6504 | s->res_cw = s->res_cr = s->res_sw = s->res_sr = RES_SILENT; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6505 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6506 | tv_min(&min1, &s->crexpire, &s->cwexpire); |
| 6507 | tv_min(&min2, &s->srexpire, &s->swexpire); |
| 6508 | tv_min(&min1, &min1, &s->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6509 | tv_min(&t->expire, &min1, &min2); |
| 6510 | |
| 6511 | /* restore t to its place in the task list */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6512 | task_queue(t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6513 | |
Willy TARREAU | 1cec83c | 2006-03-01 22:33:49 +0100 | [diff] [blame] | 6514 | #ifdef DEBUG_FULL |
| 6515 | /* DEBUG code : this should never ever happen, otherwise it indicates |
| 6516 | * that a task still has something to do and will provoke a quick loop. |
| 6517 | */ |
| 6518 | if (tv_remain2(&now, &t->expire) <= 0) |
| 6519 | exit(100); |
| 6520 | #endif |
| 6521 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6522 | return tv_remain2(&now, &t->expire); /* nothing more to do */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6523 | } |
| 6524 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6525 | s->proxy->nbconn--; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6526 | actconn--; |
| 6527 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 6528 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6529 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 6530 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", s->uniq_id, s->proxy->id, (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6531 | write(1, trash, len); |
| 6532 | } |
| 6533 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 6534 | s->logs.t_close = tv_diff(&s->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6535 | if (s->rep != NULL) |
| 6536 | s->logs.bytes = s->rep->total; |
| 6537 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6538 | /* let's do a final log if we need it */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6539 | if (s->logs.logwait && (!(s->proxy->options & PR_O_NULLNOLOG) || s->req->total)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6540 | sess_log(s); |
| 6541 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6542 | /* the task MUST not be in the run queue anymore */ |
| 6543 | task_delete(t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6544 | session_free(s); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6545 | task_free(t); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6546 | return TIME_ETERNITY; /* rest in peace for eternity */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6547 | } |
| 6548 | |
| 6549 | |
willy tarreau | 2812edc | 2006-05-04 12:09:37 +0200 | [diff] [blame] | 6550 | /* Sets server <s> down, notifies by all available means, recounts the |
| 6551 | * remaining servers on the proxy and transfers queued sessions whenever |
| 6552 | * possible to other servers. |
| 6553 | */ |
| 6554 | void set_server_down(struct server *s) { |
| 6555 | struct pendconn *pc, *pc_bck, *pc_end; |
| 6556 | struct session *sess; |
| 6557 | int xferred; |
| 6558 | |
| 6559 | s->state &= ~SRV_RUNNING; |
| 6560 | |
| 6561 | if (s->health == s->rise) { |
| 6562 | recount_servers(s->proxy); |
| 6563 | recalc_server_map(s->proxy); |
| 6564 | |
| 6565 | /* we might have sessions queued on this server and waiting for |
| 6566 | * a connection. Those which are redispatchable will be queued |
| 6567 | * to another server or to the proxy itself. |
| 6568 | */ |
| 6569 | xferred = 0; |
| 6570 | FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) { |
| 6571 | sess = pc->sess; |
| 6572 | if ((sess->proxy->options & PR_O_REDISP)) { |
| 6573 | /* The REDISP option was specified. We will ignore |
| 6574 | * cookie and force to balance or use the dispatcher. |
| 6575 | */ |
| 6576 | sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 6577 | sess->srv = NULL; /* it's left to the dispatcher to choose a server */ |
| 6578 | if ((sess->flags & SN_CK_MASK) == SN_CK_VALID) { |
| 6579 | sess->flags &= ~SN_CK_MASK; |
| 6580 | sess->flags |= SN_CK_DOWN; |
| 6581 | } |
| 6582 | pendconn_free(pc); |
| 6583 | task_wakeup(&rq, sess->task); |
| 6584 | xferred++; |
| 6585 | } |
| 6586 | } |
| 6587 | |
| 6588 | sprintf(trash, "%sServer %s/%s is DOWN. %d active and %d backup servers left.%s" |
| 6589 | " %d sessions active, %d requeued, %d remaining in queue.\n", |
| 6590 | s->state & SRV_BACKUP ? "Backup " : "", |
| 6591 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 6592 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 6593 | s->cur_sess, xferred, s->nbpend); |
| 6594 | |
willy tarreau | bc2eda6 | 2006-05-04 15:16:23 +0200 | [diff] [blame] | 6595 | Warning("%s", trash); |
| 6596 | send_log(s->proxy, LOG_ALERT, "%s", trash); |
willy tarreau | 2812edc | 2006-05-04 12:09:37 +0200 | [diff] [blame] | 6597 | |
| 6598 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 6599 | Alert("Proxy %s has no server available !\n", s->proxy->id); |
| 6600 | send_log(s->proxy, LOG_EMERG, "Proxy %s has no server available !\n", s->proxy->id); |
| 6601 | } |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 6602 | s->down_trans++; |
willy tarreau | 2812edc | 2006-05-04 12:09:37 +0200 | [diff] [blame] | 6603 | } |
| 6604 | s->health = 0; /* failure */ |
| 6605 | } |
| 6606 | |
| 6607 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6608 | |
| 6609 | /* |
| 6610 | * manages a server health-check. Returns |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6611 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6612 | */ |
| 6613 | int process_chk(struct task *t) { |
| 6614 | struct server *s = t->context; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6615 | struct sockaddr_in sa; |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 6616 | int fd; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6617 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 6618 | //fprintf(stderr, "process_chk: task=%p\n", t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6619 | |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 6620 | new_chk: |
| 6621 | fd = s->curfd; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6622 | if (fd < 0) { /* no check currently running */ |
| 6623 | //fprintf(stderr, "process_chk: 2\n"); |
| 6624 | if (tv_cmp2_ms(&t->expire, &now) > 0) { /* not good time yet */ |
| 6625 | task_queue(t); /* restore t to its place in the task list */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6626 | return tv_remain2(&now, &t->expire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6627 | } |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 6628 | |
| 6629 | /* we don't send any health-checks when the proxy is stopped or when |
| 6630 | * the server should not be checked. |
| 6631 | */ |
| 6632 | if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) { |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 6633 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 6634 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 6635 | task_queue(t); /* restore t to its place in the task list */ |
| 6636 | return tv_remain2(&now, &t->expire); |
| 6637 | } |
| 6638 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6639 | /* we'll initiate a new check */ |
| 6640 | s->result = 0; /* no result yet */ |
| 6641 | if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6642 | if ((fd < global.maxsock) && |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6643 | (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) && |
| 6644 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) { |
| 6645 | //fprintf(stderr, "process_chk: 3\n"); |
| 6646 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6647 | /* we'll connect to the check port on the server */ |
| 6648 | sa = s->addr; |
| 6649 | sa.sin_port = htons(s->check_port); |
| 6650 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6651 | /* allow specific binding : |
| 6652 | * - server-specific at first |
| 6653 | * - proxy-specific next |
| 6654 | */ |
| 6655 | if (s->state & SRV_BIND_SRC) { |
| 6656 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 6657 | if (bind(fd, (struct sockaddr *)&s->source_addr, sizeof(s->source_addr)) == -1) { |
| 6658 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 6659 | s->proxy->id, s->id); |
| 6660 | s->result = -1; |
| 6661 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6662 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6663 | else if (s->proxy->options & PR_O_BIND_SRC) { |
| 6664 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 6665 | if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) { |
| 6666 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", |
| 6667 | s->proxy->id); |
| 6668 | s->result = -1; |
| 6669 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6670 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6671 | |
| 6672 | if (!s->result) { |
| 6673 | if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) { |
| 6674 | /* OK, connection in progress or established */ |
| 6675 | |
| 6676 | //fprintf(stderr, "process_chk: 4\n"); |
| 6677 | |
| 6678 | s->curfd = fd; /* that's how we know a test is in progress ;-) */ |
| 6679 | fdtab[fd].owner = t; |
| 6680 | fdtab[fd].read = &event_srv_chk_r; |
| 6681 | fdtab[fd].write = &event_srv_chk_w; |
| 6682 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 6683 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 6684 | #ifdef DEBUG_FULL |
| 6685 | assert (!FD_ISSET(fd, StaticReadEvent)); |
| 6686 | #endif |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6687 | fd_insert(fd); |
| 6688 | /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */ |
| 6689 | tv_delayfrom(&t->expire, &now, s->inter); |
| 6690 | task_queue(t); /* restore t to its place in the task list */ |
| 6691 | return tv_remain(&now, &t->expire); |
| 6692 | } |
| 6693 | else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) { |
| 6694 | s->result = -1; /* a real error */ |
| 6695 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6696 | } |
| 6697 | } |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 6698 | close(fd); /* socket creation error */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6699 | } |
| 6700 | |
| 6701 | if (!s->result) { /* nothing done */ |
| 6702 | //fprintf(stderr, "process_chk: 6\n"); |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 6703 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 6704 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
| 6705 | goto new_chk; /* may be we should initialize a new check */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6706 | } |
| 6707 | |
| 6708 | /* here, we have seen a failure */ |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 6709 | if (s->health > s->rise) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6710 | s->health--; /* still good */ |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 6711 | s->failed_checks++; |
| 6712 | } |
willy tarreau | 2812edc | 2006-05-04 12:09:37 +0200 | [diff] [blame] | 6713 | else |
| 6714 | set_server_down(s); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6715 | |
| 6716 | //fprintf(stderr, "process_chk: 7\n"); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 6717 | /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */ |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 6718 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 6719 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
| 6720 | goto new_chk; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6721 | } |
| 6722 | else { |
| 6723 | //fprintf(stderr, "process_chk: 8\n"); |
| 6724 | /* there was a test running */ |
| 6725 | if (s->result > 0) { /* good server detected */ |
| 6726 | //fprintf(stderr, "process_chk: 9\n"); |
| 6727 | s->health++; /* was bad, stays for a while */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 6728 | if (s->health >= s->rise) { |
willy tarreau | 06a1205 | 2006-03-30 14:06:51 +0200 | [diff] [blame] | 6729 | s->state |= SRV_RUNNING; |
| 6730 | |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 6731 | if (s->health == s->rise) { |
willy tarreau | bc2eda6 | 2006-05-04 15:16:23 +0200 | [diff] [blame] | 6732 | int xferred; |
| 6733 | |
willy tarreau | 62084d4 | 2006-03-24 18:57:41 +0100 | [diff] [blame] | 6734 | recount_servers(s->proxy); |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 6735 | recalc_server_map(s->proxy); |
willy tarreau | bc2eda6 | 2006-05-04 15:16:23 +0200 | [diff] [blame] | 6736 | |
| 6737 | /* check if we can handle some connections queued at the proxy. We |
| 6738 | * will take as many as we can handle. |
| 6739 | */ |
| 6740 | for (xferred = 0; !s->maxconn || xferred < s->maxconn; xferred++) { |
| 6741 | struct session *sess; |
| 6742 | struct pendconn *p; |
| 6743 | |
| 6744 | p = pendconn_from_px(s->proxy); |
| 6745 | if (!p) |
| 6746 | break; |
| 6747 | p->sess->srv = s; |
| 6748 | sess = p->sess; |
| 6749 | pendconn_free(p); |
| 6750 | task_wakeup(&rq, sess->task); |
| 6751 | } |
| 6752 | |
| 6753 | sprintf(trash, |
| 6754 | "%sServer %s/%s is UP. %d active and %d backup servers online.%s" |
| 6755 | " %d sessions requeued, %d total in queue.\n", |
| 6756 | s->state & SRV_BACKUP ? "Backup " : "", |
| 6757 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 6758 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 6759 | xferred, s->nbpend); |
| 6760 | |
| 6761 | Warning("%s", trash); |
| 6762 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 6763 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 6764 | |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 6765 | s->health = s->rise + s->fall - 1; /* OK now */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6766 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 6767 | s->curfd = -1; /* no check running anymore */ |
| 6768 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6769 | fd_delete(fd); |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 6770 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 6771 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
| 6772 | goto new_chk; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6773 | } |
| 6774 | else if (s->result < 0 || tv_cmp2_ms(&t->expire, &now) <= 0) { |
| 6775 | //fprintf(stderr, "process_chk: 10\n"); |
| 6776 | /* failure or timeout detected */ |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 6777 | if (s->health > s->rise) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6778 | s->health--; /* still good */ |
willy tarreau | cb40651 | 2006-05-18 00:52:35 +0200 | [diff] [blame] | 6779 | s->failed_checks++; |
| 6780 | } |
willy tarreau | 2812edc | 2006-05-04 12:09:37 +0200 | [diff] [blame] | 6781 | else |
| 6782 | set_server_down(s); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6783 | s->curfd = -1; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 6784 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6785 | fd_delete(fd); |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 6786 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 6787 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
| 6788 | goto new_chk; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6789 | } |
| 6790 | /* if result is 0 and there's no timeout, we have to wait again */ |
| 6791 | } |
| 6792 | //fprintf(stderr, "process_chk: 11\n"); |
| 6793 | s->result = 0; |
| 6794 | task_queue(t); /* restore t to its place in the task list */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6795 | return tv_remain2(&now, &t->expire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6796 | } |
| 6797 | |
| 6798 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6799 | |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 6800 | /* |
| 6801 | * Manages a server's connection queue. If woken up, will try to dequeue as |
| 6802 | * many pending sessions as possible, and wake them up. The task has nothing |
| 6803 | * else to do, so it always returns TIME_ETERNITY. |
| 6804 | */ |
| 6805 | int process_srv_queue(struct task *t) { |
| 6806 | struct server *s = (struct server*)t->context; |
| 6807 | struct proxy *p = s->proxy; |
| 6808 | int xferred; |
| 6809 | |
| 6810 | /* First, check if we can handle some connections queued at the proxy. We |
| 6811 | * will take as many as we can handle. |
| 6812 | */ |
| 6813 | for (xferred = 0; s->cur_sess + xferred < s->maxconn; xferred++) { |
| 6814 | struct session *sess; |
| 6815 | |
| 6816 | sess = pendconn_get_next_sess(s, p); |
| 6817 | if (sess == NULL) |
| 6818 | break; |
| 6819 | task_wakeup(&rq, sess->task); |
| 6820 | } |
| 6821 | |
| 6822 | return TIME_ETERNITY; |
| 6823 | } |
| 6824 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6825 | #if STATTIME > 0 |
| 6826 | int stats(void); |
| 6827 | #endif |
| 6828 | |
| 6829 | /* |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6830 | * This does 4 things : |
| 6831 | * - wake up all expired tasks |
| 6832 | * - call all runnable tasks |
| 6833 | * - call maintain_proxies() to enable/disable the listeners |
| 6834 | * - return the delay till next event in ms, -1 = wait indefinitely |
| 6835 | * Note: this part should be rewritten with the O(ln(n)) scheduler. |
| 6836 | * |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6837 | */ |
| 6838 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6839 | int process_runnable_tasks() { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6840 | int next_time; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6841 | int time2; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6842 | struct task *t, *tnext; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6843 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6844 | next_time = TIME_ETERNITY; /* set the timer to wait eternally first */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6845 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6846 | /* look for expired tasks and add them to the run queue. |
| 6847 | */ |
willy tarreau | 5e698ef | 2006-05-02 14:51:00 +0200 | [diff] [blame] | 6848 | tnext = ((struct task *)LIST_HEAD(wait_queue[0]))->next; |
| 6849 | while ((t = tnext) != LIST_HEAD(wait_queue[0])) { /* we haven't looped ? */ |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6850 | tnext = t->next; |
| 6851 | if (t->state & TASK_RUNNING) |
| 6852 | continue; |
| 6853 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6854 | if (tv_iseternity(&t->expire)) |
| 6855 | continue; |
| 6856 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6857 | /* wakeup expired entries. It doesn't matter if they are |
| 6858 | * already running because of a previous event |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6859 | */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6860 | if (tv_cmp_ms(&t->expire, &now) <= 0) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6861 | task_wakeup(&rq, t); |
| 6862 | } |
| 6863 | else { |
| 6864 | /* first non-runnable task. Use its expiration date as an upper bound */ |
| 6865 | int temp_time = tv_remain(&now, &t->expire); |
| 6866 | if (temp_time) |
| 6867 | next_time = temp_time; |
| 6868 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6869 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6870 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6871 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6872 | /* process each task in the run queue now. Each task may be deleted |
willy tarreau | 7feab59 | 2006-04-22 15:13:16 +0200 | [diff] [blame] | 6873 | * since we only use the run queue's head. Note that any task can be |
| 6874 | * woken up by any other task and it will be processed immediately |
| 6875 | * after as it will be queued on the run queue's head. |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6876 | */ |
willy tarreau | 7feab59 | 2006-04-22 15:13:16 +0200 | [diff] [blame] | 6877 | while ((t = rq) != NULL) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6878 | int temp_time; |
willy tarreau | 7feab59 | 2006-04-22 15:13:16 +0200 | [diff] [blame] | 6879 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6880 | task_sleep(&rq, t); |
| 6881 | temp_time = t->process(t); |
| 6882 | next_time = MINTIME(temp_time, next_time); |
| 6883 | } |
| 6884 | |
| 6885 | /* maintain all proxies in a consistent state. This should quickly become a task */ |
| 6886 | time2 = maintain_proxies(); |
| 6887 | return MINTIME(time2, next_time); |
| 6888 | } |
| 6889 | |
| 6890 | |
| 6891 | #if defined(ENABLE_EPOLL) |
| 6892 | |
| 6893 | /* |
| 6894 | * Main epoll() loop. |
| 6895 | */ |
| 6896 | |
| 6897 | /* does 3 actions : |
| 6898 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 6899 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 6900 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 6901 | * |
| 6902 | * returns 0 if initialization failed, !0 otherwise. |
| 6903 | */ |
| 6904 | |
| 6905 | int epoll_loop(int action) { |
| 6906 | int next_time; |
| 6907 | int status; |
| 6908 | int fd; |
| 6909 | |
| 6910 | int fds, count; |
| 6911 | int pr, pw, sr, sw; |
| 6912 | unsigned rn, ro, wn, wo; /* read new, read old, write new, write old */ |
| 6913 | struct epoll_event ev; |
| 6914 | |
| 6915 | /* private data */ |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6916 | static struct epoll_event *epoll_events = NULL; |
| 6917 | static int epoll_fd; |
| 6918 | |
| 6919 | if (action == POLL_LOOP_ACTION_INIT) { |
| 6920 | epoll_fd = epoll_create(global.maxsock + 1); |
| 6921 | if (epoll_fd < 0) |
| 6922 | return 0; |
| 6923 | else { |
| 6924 | epoll_events = (struct epoll_event*) |
| 6925 | calloc(1, sizeof(struct epoll_event) * global.maxsock); |
| 6926 | PrevReadEvent = (fd_set *) |
| 6927 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 6928 | PrevWriteEvent = (fd_set *) |
| 6929 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6930 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6931 | return 1; |
| 6932 | } |
| 6933 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 6934 | if (PrevWriteEvent) free(PrevWriteEvent); |
| 6935 | if (PrevReadEvent) free(PrevReadEvent); |
| 6936 | if (epoll_events) free(epoll_events); |
| 6937 | close(epoll_fd); |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6938 | epoll_fd = 0; |
| 6939 | return 1; |
| 6940 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6941 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6942 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6943 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6944 | tv_now(&now); |
| 6945 | |
| 6946 | while (1) { |
| 6947 | next_time = process_runnable_tasks(); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6948 | |
| 6949 | /* stop when there's no connection left and we don't allow them anymore */ |
| 6950 | if (!actconn && listeners == 0) |
| 6951 | break; |
| 6952 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6953 | #if STATTIME > 0 |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6954 | { |
| 6955 | int time2; |
| 6956 | time2 = stats(); |
| 6957 | next_time = MINTIME(time2, next_time); |
| 6958 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6959 | #endif |
| 6960 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6961 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) { |
| 6962 | |
| 6963 | rn = ((int*)StaticReadEvent)[fds]; ro = ((int*)PrevReadEvent)[fds]; |
| 6964 | wn = ((int*)StaticWriteEvent)[fds]; wo = ((int*)PrevWriteEvent)[fds]; |
| 6965 | |
| 6966 | if ((ro^rn) | (wo^wn)) { |
| 6967 | for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) { |
| 6968 | #define FDSETS_ARE_INT_ALIGNED |
| 6969 | #ifdef FDSETS_ARE_INT_ALIGNED |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6970 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 6971 | #define WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 6972 | #ifdef WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6973 | pr = (ro >> count) & 1; |
| 6974 | pw = (wo >> count) & 1; |
| 6975 | sr = (rn >> count) & 1; |
| 6976 | sw = (wn >> count) & 1; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 6977 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6978 | pr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&ro); |
| 6979 | pw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wo); |
| 6980 | sr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&rn); |
| 6981 | sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 6982 | #endif |
| 6983 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6984 | pr = FD_ISSET(fd, PrevReadEvent); |
| 6985 | pw = FD_ISSET(fd, PrevWriteEvent); |
| 6986 | sr = FD_ISSET(fd, StaticReadEvent); |
| 6987 | sw = FD_ISSET(fd, StaticWriteEvent); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 6988 | #endif |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6989 | if (!((sr^pr) | (sw^pw))) |
| 6990 | continue; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 6991 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 6992 | ev.events = (sr ? EPOLLIN : 0) | (sw ? EPOLLOUT : 0); |
| 6993 | ev.data.fd = fd; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 6994 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6995 | #ifdef EPOLL_CTL_MOD_WORKAROUND |
| 6996 | /* I encountered a rarely reproducible problem with |
| 6997 | * EPOLL_CTL_MOD where a modified FD (systematically |
| 6998 | * the one in epoll_events[0], fd#7) would sometimes |
| 6999 | * be set EPOLL_OUT while asked for a read ! This is |
| 7000 | * with the 2.4 epoll patch. The workaround is to |
| 7001 | * delete then recreate in case of modification. |
| 7002 | * This is in 2.4 up to epoll-lt-0.21 but not in 2.6 |
| 7003 | * nor RHEL kernels. |
| 7004 | */ |
| 7005 | |
| 7006 | if ((pr | pw) && fdtab[fd].state != FD_STCLOSE) |
| 7007 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev); |
| 7008 | |
| 7009 | if ((sr | sw)) |
| 7010 | epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev); |
| 7011 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7012 | if ((pr | pw)) { |
| 7013 | /* the file-descriptor already exists... */ |
| 7014 | if ((sr | sw)) { |
| 7015 | /* ...and it will still exist */ |
| 7016 | if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &ev) < 0) { |
| 7017 | // perror("epoll_ctl(MOD)"); |
| 7018 | // exit(1); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7019 | } |
| 7020 | } else { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7021 | /* ...and it will be removed */ |
| 7022 | if (fdtab[fd].state != FD_STCLOSE && |
| 7023 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev) < 0) { |
| 7024 | // perror("epoll_ctl(DEL)"); |
| 7025 | // exit(1); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7026 | } |
| 7027 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7028 | } else { |
| 7029 | /* the file-descriptor did not exist, let's add it */ |
| 7030 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) { |
| 7031 | // perror("epoll_ctl(ADD)"); |
| 7032 | // exit(1); |
| 7033 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7034 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 7035 | #endif // EPOLL_CTL_MOD_WORKAROUND |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7036 | } |
| 7037 | ((int*)PrevReadEvent)[fds] = rn; |
| 7038 | ((int*)PrevWriteEvent)[fds] = wn; |
| 7039 | } |
| 7040 | } |
| 7041 | |
| 7042 | /* now let's wait for events */ |
| 7043 | status = epoll_wait(epoll_fd, epoll_events, maxfd, next_time); |
| 7044 | tv_now(&now); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7045 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7046 | for (count = 0; count < status; count++) { |
| 7047 | fd = epoll_events[count].data.fd; |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 7048 | |
| 7049 | if (FD_ISSET(fd, StaticReadEvent)) { |
| 7050 | if (fdtab[fd].state == FD_STCLOSE) |
| 7051 | continue; |
| 7052 | if (epoll_events[count].events & ( EPOLLIN | EPOLLERR | EPOLLHUP )) |
| 7053 | fdtab[fd].read(fd); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 7054 | } |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 7055 | |
| 7056 | if (FD_ISSET(fd, StaticWriteEvent)) { |
| 7057 | if (fdtab[fd].state == FD_STCLOSE) |
| 7058 | continue; |
| 7059 | if (epoll_events[count].events & ( EPOLLOUT | EPOLLERR | EPOLLHUP )) |
| 7060 | fdtab[fd].write(fd); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 7061 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7062 | } |
| 7063 | } |
| 7064 | return 1; |
| 7065 | } |
| 7066 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7067 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7068 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7069 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7070 | #if defined(ENABLE_POLL) |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7071 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7072 | /* |
| 7073 | * Main poll() loop. |
| 7074 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7075 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7076 | /* does 3 actions : |
| 7077 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 7078 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 7079 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 7080 | * |
| 7081 | * returns 0 if initialization failed, !0 otherwise. |
| 7082 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7083 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7084 | int poll_loop(int action) { |
| 7085 | int next_time; |
| 7086 | int status; |
| 7087 | int fd, nbfd; |
| 7088 | |
| 7089 | int fds, count; |
| 7090 | int sr, sw; |
| 7091 | unsigned rn, wn; /* read new, write new */ |
| 7092 | |
| 7093 | /* private data */ |
| 7094 | static struct pollfd *poll_events = NULL; |
| 7095 | |
| 7096 | if (action == POLL_LOOP_ACTION_INIT) { |
| 7097 | poll_events = (struct pollfd*) |
| 7098 | calloc(1, sizeof(struct pollfd) * global.maxsock); |
| 7099 | return 1; |
| 7100 | } |
| 7101 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 7102 | if (poll_events) |
| 7103 | free(poll_events); |
| 7104 | return 1; |
| 7105 | } |
| 7106 | |
| 7107 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
| 7108 | |
| 7109 | tv_now(&now); |
| 7110 | |
| 7111 | while (1) { |
| 7112 | next_time = process_runnable_tasks(); |
| 7113 | |
| 7114 | /* stop when there's no connection left and we don't allow them anymore */ |
| 7115 | if (!actconn && listeners == 0) |
| 7116 | break; |
| 7117 | |
| 7118 | #if STATTIME > 0 |
| 7119 | { |
| 7120 | int time2; |
| 7121 | time2 = stats(); |
| 7122 | next_time = MINTIME(time2, next_time); |
| 7123 | } |
| 7124 | #endif |
| 7125 | |
| 7126 | |
| 7127 | nbfd = 0; |
| 7128 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) { |
| 7129 | |
| 7130 | rn = ((int*)StaticReadEvent)[fds]; |
| 7131 | wn = ((int*)StaticWriteEvent)[fds]; |
| 7132 | |
| 7133 | if ((rn|wn)) { |
| 7134 | for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) { |
| 7135 | #define FDSETS_ARE_INT_ALIGNED |
| 7136 | #ifdef FDSETS_ARE_INT_ALIGNED |
| 7137 | |
| 7138 | #define WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 7139 | #ifdef WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 7140 | sr = (rn >> count) & 1; |
| 7141 | sw = (wn >> count) & 1; |
| 7142 | #else |
| 7143 | sr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&rn); |
| 7144 | sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn); |
| 7145 | #endif |
| 7146 | #else |
| 7147 | sr = FD_ISSET(fd, StaticReadEvent); |
| 7148 | sw = FD_ISSET(fd, StaticWriteEvent); |
| 7149 | #endif |
| 7150 | if ((sr|sw)) { |
| 7151 | poll_events[nbfd].fd = fd; |
| 7152 | poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0); |
| 7153 | nbfd++; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7154 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7155 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7156 | } |
| 7157 | } |
| 7158 | |
| 7159 | /* now let's wait for events */ |
| 7160 | status = poll(poll_events, nbfd, next_time); |
| 7161 | tv_now(&now); |
| 7162 | |
| 7163 | for (count = 0; status > 0 && count < nbfd; count++) { |
| 7164 | fd = poll_events[count].fd; |
| 7165 | |
willy tarreau | 606788e | 2006-05-21 16:26:20 +0200 | [diff] [blame] | 7166 | if (!(poll_events[count].revents & ( POLLOUT | POLLIN | POLLERR | POLLHUP ))) |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7167 | continue; |
| 7168 | |
| 7169 | /* ok, we found one active fd */ |
| 7170 | status--; |
| 7171 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 7172 | if (FD_ISSET(fd, StaticReadEvent)) { |
| 7173 | if (fdtab[fd].state == FD_STCLOSE) |
| 7174 | continue; |
| 7175 | if (poll_events[count].revents & ( POLLIN | POLLERR | POLLHUP )) |
| 7176 | fdtab[fd].read(fd); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 7177 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7178 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 7179 | if (FD_ISSET(fd, StaticWriteEvent)) { |
| 7180 | if (fdtab[fd].state == FD_STCLOSE) |
| 7181 | continue; |
| 7182 | if (poll_events[count].revents & ( POLLOUT | POLLERR | POLLHUP )) |
| 7183 | fdtab[fd].write(fd); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 7184 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7185 | } |
| 7186 | } |
| 7187 | return 1; |
| 7188 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7189 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7190 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7191 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7192 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7193 | /* |
| 7194 | * Main select() loop. |
| 7195 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7196 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7197 | /* does 3 actions : |
| 7198 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 7199 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 7200 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 7201 | * |
| 7202 | * returns 0 if initialization failed, !0 otherwise. |
| 7203 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7204 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7205 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7206 | int select_loop(int action) { |
| 7207 | int next_time; |
| 7208 | int status; |
| 7209 | int fd,i; |
| 7210 | struct timeval delta; |
| 7211 | int readnotnull, writenotnull; |
| 7212 | static fd_set *ReadEvent = NULL, *WriteEvent = NULL; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7213 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7214 | if (action == POLL_LOOP_ACTION_INIT) { |
| 7215 | ReadEvent = (fd_set *) |
| 7216 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 7217 | WriteEvent = (fd_set *) |
| 7218 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 7219 | return 1; |
| 7220 | } |
| 7221 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 7222 | if (WriteEvent) free(WriteEvent); |
| 7223 | if (ReadEvent) free(ReadEvent); |
| 7224 | return 1; |
| 7225 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7226 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7227 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7228 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7229 | tv_now(&now); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7230 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7231 | while (1) { |
| 7232 | next_time = process_runnable_tasks(); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7233 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7234 | /* stop when there's no connection left and we don't allow them anymore */ |
| 7235 | if (!actconn && listeners == 0) |
| 7236 | break; |
| 7237 | |
| 7238 | #if STATTIME > 0 |
| 7239 | { |
| 7240 | int time2; |
| 7241 | time2 = stats(); |
| 7242 | next_time = MINTIME(time2, next_time); |
| 7243 | } |
| 7244 | #endif |
| 7245 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7246 | if (next_time > 0) { /* FIXME */ |
| 7247 | /* Convert to timeval */ |
| 7248 | /* to avoid eventual select loops due to timer precision */ |
| 7249 | next_time += SCHEDULER_RESOLUTION; |
| 7250 | delta.tv_sec = next_time / 1000; |
| 7251 | delta.tv_usec = (next_time % 1000) * 1000; |
| 7252 | } |
| 7253 | else if (next_time == 0) { /* allow select to return immediately when needed */ |
| 7254 | delta.tv_sec = delta.tv_usec = 0; |
| 7255 | } |
| 7256 | |
| 7257 | |
| 7258 | /* let's restore fdset state */ |
| 7259 | |
| 7260 | readnotnull = 0; writenotnull = 0; |
| 7261 | for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) { |
| 7262 | readnotnull |= (*(((int*)ReadEvent)+i) = *(((int*)StaticReadEvent)+i)) != 0; |
| 7263 | writenotnull |= (*(((int*)WriteEvent)+i) = *(((int*)StaticWriteEvent)+i)) != 0; |
| 7264 | } |
| 7265 | |
| 7266 | // /* just a verification code, needs to be removed for performance */ |
| 7267 | // for (i=0; i<maxfd; i++) { |
| 7268 | // if (FD_ISSET(i, ReadEvent) != FD_ISSET(i, StaticReadEvent)) |
| 7269 | // abort(); |
| 7270 | // if (FD_ISSET(i, WriteEvent) != FD_ISSET(i, StaticWriteEvent)) |
| 7271 | // abort(); |
| 7272 | // |
| 7273 | // } |
| 7274 | |
| 7275 | status = select(maxfd, |
| 7276 | readnotnull ? ReadEvent : NULL, |
| 7277 | writenotnull ? WriteEvent : NULL, |
| 7278 | NULL, |
| 7279 | (next_time >= 0) ? &delta : NULL); |
| 7280 | |
| 7281 | /* this is an experiment on the separation of the select work */ |
| 7282 | // status = (readnotnull ? select(maxfd, ReadEvent, NULL, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 7283 | // status |= (writenotnull ? select(maxfd, NULL, WriteEvent, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 7284 | |
| 7285 | tv_now(&now); |
| 7286 | |
| 7287 | if (status > 0) { /* must proceed with events */ |
| 7288 | |
| 7289 | int fds; |
| 7290 | char count; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 7291 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7292 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) |
| 7293 | if ((((int *)(ReadEvent))[fds] | ((int *)(WriteEvent))[fds]) != 0) |
| 7294 | for (count = 1<<INTBITS, fd = fds << INTBITS; count && fd < maxfd; count--, fd++) { |
| 7295 | |
| 7296 | /* if we specify read first, the accepts and zero reads will be |
| 7297 | * seen first. Moreover, system buffers will be flushed faster. |
| 7298 | */ |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 7299 | if (FD_ISSET(fd, ReadEvent)) { |
| 7300 | if (fdtab[fd].state == FD_STCLOSE) |
| 7301 | continue; |
| 7302 | fdtab[fd].read(fd); |
| 7303 | } |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 7304 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 7305 | if (FD_ISSET(fd, WriteEvent)) { |
| 7306 | if (fdtab[fd].state == FD_STCLOSE) |
| 7307 | continue; |
| 7308 | fdtab[fd].write(fd); |
| 7309 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7310 | } |
| 7311 | } |
| 7312 | else { |
| 7313 | // fprintf(stderr,"select returned %d, maxfd=%d\n", status, maxfd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7314 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7315 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7316 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7317 | } |
| 7318 | |
| 7319 | |
| 7320 | #if STATTIME > 0 |
| 7321 | /* |
| 7322 | * Display proxy statistics regularly. It is designed to be called from the |
| 7323 | * select_loop(). |
| 7324 | */ |
| 7325 | int stats(void) { |
| 7326 | static int lines; |
| 7327 | static struct timeval nextevt; |
| 7328 | static struct timeval lastevt; |
| 7329 | static struct timeval starttime = {0,0}; |
| 7330 | unsigned long totaltime, deltatime; |
| 7331 | int ret; |
| 7332 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 7333 | if (tv_cmp(&now, &nextevt) > 0) { |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 7334 | deltatime = (tv_diff(&lastevt, &now)?:1); |
| 7335 | totaltime = (tv_diff(&starttime, &now)?:1); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7336 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7337 | if (global.mode & MODE_STATS) { |
| 7338 | if ((lines++ % 16 == 0) && !(global.mode & MODE_LOG)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7339 | qfprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7340 | "\n active total tsknew tskgood tskleft tskrght tsknsch tsklsch tskrsch\n"); |
| 7341 | if (lines>1) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7342 | qfprintf(stderr,"%07d %07d %07d %07d %07d %07d %07d %07d %07d\n", |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7343 | actconn, totalconn, |
| 7344 | stats_tsk_new, stats_tsk_good, |
| 7345 | stats_tsk_left, stats_tsk_right, |
| 7346 | stats_tsk_nsrch, stats_tsk_lsrch, stats_tsk_rsrch); |
| 7347 | } |
| 7348 | } |
| 7349 | |
| 7350 | tv_delayfrom(&nextevt, &now, STATTIME); |
| 7351 | |
| 7352 | lastevt=now; |
| 7353 | } |
| 7354 | ret = tv_remain(&now, &nextevt); |
| 7355 | return ret; |
| 7356 | } |
| 7357 | #endif |
| 7358 | |
| 7359 | |
| 7360 | /* |
| 7361 | * this function enables proxies when there are enough free sessions, |
| 7362 | * or stops them when the table is full. It is designed to be called from the |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7363 | * select_loop(). It returns the time left before next expiration event |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 7364 | * during stop time, TIME_ETERNITY otherwise. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7365 | */ |
| 7366 | static int maintain_proxies(void) { |
| 7367 | struct proxy *p; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7368 | struct listener *l; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7369 | int tleft; /* time left */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7370 | |
| 7371 | p = proxy; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 7372 | tleft = TIME_ETERNITY; /* infinite time */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7373 | |
| 7374 | /* if there are enough free sessions, we'll activate proxies */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7375 | if (actconn < global.maxconn) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7376 | while (p) { |
| 7377 | if (p->nbconn < p->maxconn) { |
| 7378 | if (p->state == PR_STIDLE) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7379 | for (l = p->listen; l != NULL; l = l->next) { |
| 7380 | FD_SET(l->fd, StaticReadEvent); |
| 7381 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7382 | p->state = PR_STRUN; |
| 7383 | } |
| 7384 | } |
| 7385 | else { |
| 7386 | if (p->state == PR_STRUN) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7387 | for (l = p->listen; l != NULL; l = l->next) { |
| 7388 | FD_CLR(l->fd, StaticReadEvent); |
| 7389 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7390 | p->state = PR_STIDLE; |
| 7391 | } |
| 7392 | } |
| 7393 | p = p->next; |
| 7394 | } |
| 7395 | } |
| 7396 | else { /* block all proxies */ |
| 7397 | while (p) { |
| 7398 | if (p->state == PR_STRUN) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7399 | for (l = p->listen; l != NULL; l = l->next) { |
| 7400 | FD_CLR(l->fd, StaticReadEvent); |
| 7401 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7402 | p->state = PR_STIDLE; |
| 7403 | } |
| 7404 | p = p->next; |
| 7405 | } |
| 7406 | } |
| 7407 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7408 | if (stopping) { |
| 7409 | p = proxy; |
| 7410 | while (p) { |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7411 | if (p->state != PR_STSTOPPED) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7412 | int t; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 7413 | t = tv_remain2(&now, &p->stop_time); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7414 | if (t == 0) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 7415 | Warning("Proxy %s stopped.\n", p->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7416 | send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 7417 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7418 | for (l = p->listen; l != NULL; l = l->next) { |
| 7419 | fd_delete(l->fd); |
| 7420 | listeners--; |
| 7421 | } |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7422 | p->state = PR_STSTOPPED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7423 | } |
| 7424 | else { |
| 7425 | tleft = MINTIME(t, tleft); |
| 7426 | } |
| 7427 | } |
| 7428 | p = p->next; |
| 7429 | } |
| 7430 | } |
| 7431 | return tleft; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7432 | } |
| 7433 | |
| 7434 | /* |
| 7435 | * this function disables health-check servers so that the process will quickly be ignored |
willy tarreau | 808b4e6 | 2006-01-20 19:46:44 +0100 | [diff] [blame] | 7436 | * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace |
| 7437 | * time will not be used since it would already not listen anymore to the socket. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7438 | */ |
| 7439 | static void soft_stop(void) { |
| 7440 | struct proxy *p; |
| 7441 | |
| 7442 | stopping = 1; |
| 7443 | p = proxy; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7444 | tv_now(&now); /* else, the old time before select will be used */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7445 | while (p) { |
Willy TARREAU | 2bfdd8e | 2006-03-12 18:03:05 +0100 | [diff] [blame] | 7446 | if (p->state != PR_STSTOPPED) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 7447 | Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7448 | send_log(p, LOG_WARNING, "Stopping proxy %s in %d ms.\n", p->id, p->grace); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7449 | tv_delayfrom(&p->stop_time, &now, p->grace); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 7450 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7451 | p = p->next; |
| 7452 | } |
| 7453 | } |
| 7454 | |
willy tarreau | fac1a86 | 2006-05-21 10:20:28 +0200 | [diff] [blame] | 7455 | /* |
| 7456 | * Linux unbinds the listen socket after a SHUT_RD, and ignores SHUT_WR. |
| 7457 | * Solaris refuses either shutdown(). |
| 7458 | * OpenBSD ignores SHUT_RD but closes upon SHUT_WR and refuses to rebind. |
| 7459 | * So a common validation path involves SHUT_WR && listen && SHUT_RD. |
| 7460 | * If disabling at least one listener returns an error, then the proxy |
| 7461 | * state is set to PR_STERROR because we don't know how to resume from this. |
| 7462 | */ |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7463 | static void pause_proxy(struct proxy *p) { |
| 7464 | struct listener *l; |
| 7465 | for (l = p->listen; l != NULL; l = l->next) { |
willy tarreau | fac1a86 | 2006-05-21 10:20:28 +0200 | [diff] [blame] | 7466 | if (shutdown(l->fd, SHUT_WR) == 0 && listen(l->fd, p->maxconn) == 0 && |
| 7467 | shutdown(l->fd, SHUT_RD) == 0) { |
Willy TARREAU | 007aa46 | 2006-05-14 09:55:23 +0200 | [diff] [blame] | 7468 | FD_CLR(l->fd, StaticReadEvent); |
willy tarreau | fac1a86 | 2006-05-21 10:20:28 +0200 | [diff] [blame] | 7469 | if (p->state != PR_STERROR) |
| 7470 | p->state = PR_STPAUSED; |
Willy TARREAU | 007aa46 | 2006-05-14 09:55:23 +0200 | [diff] [blame] | 7471 | } |
willy tarreau | fac1a86 | 2006-05-21 10:20:28 +0200 | [diff] [blame] | 7472 | else |
| 7473 | p->state = PR_STERROR; |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7474 | } |
| 7475 | } |
| 7476 | |
| 7477 | /* |
| 7478 | * This function temporarily disables listening so that another new instance |
| 7479 | * can start listening. It is designed to be called upon reception of a |
willy tarreau | 808b4e6 | 2006-01-20 19:46:44 +0100 | [diff] [blame] | 7480 | * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7481 | * the proxy, or a SIGTTIN can be sent to listen again. |
| 7482 | */ |
| 7483 | static void pause_proxies(void) { |
Willy TARREAU | 007aa46 | 2006-05-14 09:55:23 +0200 | [diff] [blame] | 7484 | int err; |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7485 | struct proxy *p; |
| 7486 | |
Willy TARREAU | 007aa46 | 2006-05-14 09:55:23 +0200 | [diff] [blame] | 7487 | err = 0; |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7488 | p = proxy; |
| 7489 | tv_now(&now); /* else, the old time before select will be used */ |
| 7490 | while (p) { |
willy tarreau | fac1a86 | 2006-05-21 10:20:28 +0200 | [diff] [blame] | 7491 | if (p->state != PR_STERROR && p->state != PR_STSTOPPED && p->state != PR_STPAUSED) { |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7492 | Warning("Pausing proxy %s.\n", p->id); |
| 7493 | send_log(p, LOG_WARNING, "Pausing proxy %s.\n", p->id); |
| 7494 | pause_proxy(p); |
Willy TARREAU | 007aa46 | 2006-05-14 09:55:23 +0200 | [diff] [blame] | 7495 | if (p->state != PR_STPAUSED) { |
| 7496 | err |= 1; |
| 7497 | Warning("Proxy %s failed to enter pause mode.\n", p->id); |
| 7498 | send_log(p, LOG_WARNING, "Proxy %s failed to enter pause mode.\n", p->id); |
| 7499 | } |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7500 | } |
| 7501 | p = p->next; |
| 7502 | } |
Willy TARREAU | 007aa46 | 2006-05-14 09:55:23 +0200 | [diff] [blame] | 7503 | if (err) { |
| 7504 | Warning("Some proxies refused to pause, performing soft stop now.\n"); |
| 7505 | send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n"); |
| 7506 | soft_stop(); |
| 7507 | } |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7508 | } |
| 7509 | |
| 7510 | |
| 7511 | /* |
| 7512 | * This function reactivates listening. This can be used after a call to |
| 7513 | * sig_pause(), for example when a new instance has failed starting up. |
| 7514 | * It is designed to be called upon reception of a SIGTTIN. |
| 7515 | */ |
| 7516 | static void listen_proxies(void) { |
| 7517 | struct proxy *p; |
| 7518 | struct listener *l; |
| 7519 | |
| 7520 | p = proxy; |
| 7521 | tv_now(&now); /* else, the old time before select will be used */ |
| 7522 | while (p) { |
| 7523 | if (p->state == PR_STPAUSED) { |
| 7524 | Warning("Enabling proxy %s.\n", p->id); |
| 7525 | send_log(p, LOG_WARNING, "Enabling proxy %s.\n", p->id); |
| 7526 | |
| 7527 | for (l = p->listen; l != NULL; l = l->next) { |
| 7528 | if (listen(l->fd, p->maxconn) == 0) { |
| 7529 | if (actconn < global.maxconn && p->nbconn < p->maxconn) { |
| 7530 | FD_SET(l->fd, StaticReadEvent); |
| 7531 | p->state = PR_STRUN; |
| 7532 | } |
| 7533 | else |
| 7534 | p->state = PR_STIDLE; |
| 7535 | } else { |
willy tarreau | cb2e562 | 2006-01-29 21:55:30 +0100 | [diff] [blame] | 7536 | int port; |
| 7537 | |
| 7538 | if (l->addr.ss_family == AF_INET6) |
| 7539 | port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port); |
| 7540 | else |
| 7541 | port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port); |
| 7542 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7543 | Warning("Port %d busy while trying to enable proxy %s.\n", |
willy tarreau | cb2e562 | 2006-01-29 21:55:30 +0100 | [diff] [blame] | 7544 | port, p->id); |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7545 | send_log(p, LOG_WARNING, "Port %d busy while trying to enable proxy %s.\n", |
willy tarreau | cb2e562 | 2006-01-29 21:55:30 +0100 | [diff] [blame] | 7546 | port, p->id); |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7547 | /* Another port might have been enabled. Let's stop everything. */ |
| 7548 | pause_proxy(p); |
| 7549 | break; |
| 7550 | } |
| 7551 | } |
| 7552 | } |
| 7553 | p = p->next; |
| 7554 | } |
| 7555 | } |
| 7556 | |
| 7557 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7558 | /* |
| 7559 | * upon SIGUSR1, let's have a soft stop. |
| 7560 | */ |
| 7561 | void sig_soft_stop(int sig) { |
| 7562 | soft_stop(); |
| 7563 | signal(sig, SIG_IGN); |
| 7564 | } |
| 7565 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7566 | /* |
| 7567 | * upon SIGTTOU, we pause everything |
| 7568 | */ |
| 7569 | void sig_pause(int sig) { |
| 7570 | pause_proxies(); |
| 7571 | signal(sig, sig_pause); |
| 7572 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7573 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7574 | /* |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7575 | * upon SIGTTIN, let's have a soft stop. |
| 7576 | */ |
| 7577 | void sig_listen(int sig) { |
| 7578 | listen_proxies(); |
| 7579 | signal(sig, sig_listen); |
| 7580 | } |
| 7581 | |
| 7582 | /* |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7583 | * this function dumps every server's state when the process receives SIGHUP. |
| 7584 | */ |
| 7585 | void sig_dump_state(int sig) { |
| 7586 | struct proxy *p = proxy; |
| 7587 | |
| 7588 | Warning("SIGHUP received, dumping servers states.\n"); |
| 7589 | while (p) { |
| 7590 | struct server *s = p->srv; |
| 7591 | |
willy tarreau | 4632c21 | 2006-05-02 23:32:51 +0200 | [diff] [blame] | 7592 | send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7593 | while (s) { |
willy tarreau | 4632c21 | 2006-05-02 23:32:51 +0200 | [diff] [blame] | 7594 | snprintf(trash, sizeof(trash), |
| 7595 | "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %d tot.", |
| 7596 | p->id, s->id, |
| 7597 | (s->state & SRV_RUNNING) ? "UP" : "DOWN", |
| 7598 | s->cur_sess, s->nbpend, s->cum_sess); |
willy tarreau | 14b4d43 | 2006-04-07 18:23:29 +0200 | [diff] [blame] | 7599 | Warning("%s\n", trash); |
| 7600 | send_log(p, LOG_NOTICE, "%s\n", trash); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7601 | s = s->next; |
| 7602 | } |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7603 | |
willy tarreau | 62084d4 | 2006-03-24 18:57:41 +0100 | [diff] [blame] | 7604 | if (p->srv_act == 0) { |
willy tarreau | 4632c21 | 2006-05-02 23:32:51 +0200 | [diff] [blame] | 7605 | snprintf(trash, sizeof(trash), |
| 7606 | "SIGHUP: Proxy %s %s ! Conn: %d act, %d pend (%d unass), %d tot.", |
| 7607 | p->id, |
| 7608 | (p->srv_bck) ? "is running on backup servers" : "has no server available", |
| 7609 | p->nbconn, p->totpend, p->nbpend, p->cum_conn); |
willy tarreau | 14b4d43 | 2006-04-07 18:23:29 +0200 | [diff] [blame] | 7610 | } else { |
| 7611 | snprintf(trash, sizeof(trash), |
willy tarreau | 4632c21 | 2006-05-02 23:32:51 +0200 | [diff] [blame] | 7612 | "SIGHUP: Proxy %s has %d active servers and %d backup servers available." |
| 7613 | " Conn: %d act, %d pend (%d unass), %d tot.", |
| 7614 | p->id, p->srv_act, p->srv_bck, |
| 7615 | p->nbconn, p->totpend, p->nbpend, p->cum_conn); |
willy tarreau | 14b4d43 | 2006-04-07 18:23:29 +0200 | [diff] [blame] | 7616 | } |
| 7617 | Warning("%s\n", trash); |
| 7618 | send_log(p, LOG_NOTICE, "%s\n", trash); |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7619 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7620 | p = p->next; |
| 7621 | } |
| 7622 | signal(sig, sig_dump_state); |
| 7623 | } |
| 7624 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7625 | void dump(int sig) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7626 | struct task *t, *tnext; |
| 7627 | struct session *s; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7628 | |
willy tarreau | 5e698ef | 2006-05-02 14:51:00 +0200 | [diff] [blame] | 7629 | tnext = ((struct task *)LIST_HEAD(wait_queue[0]))->next; |
| 7630 | while ((t = tnext) != LIST_HEAD(wait_queue[0])) { /* we haven't looped ? */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7631 | tnext = t->next; |
| 7632 | s = t->context; |
| 7633 | qfprintf(stderr,"[dump] wq: task %p, still %ld ms, " |
| 7634 | "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, " |
| 7635 | "req=%d, rep=%d, clifd=%d\n", |
| 7636 | s, tv_remain(&now, &t->expire), |
| 7637 | s->cli_state, |
| 7638 | s->srv_state, |
| 7639 | FD_ISSET(s->cli_fd, StaticReadEvent), |
| 7640 | FD_ISSET(s->cli_fd, StaticWriteEvent), |
| 7641 | FD_ISSET(s->srv_fd, StaticReadEvent), |
| 7642 | FD_ISSET(s->srv_fd, StaticWriteEvent), |
| 7643 | s->req->l, s->rep?s->rep->l:0, s->cli_fd |
| 7644 | ); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7645 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7646 | } |
| 7647 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 7648 | #ifdef DEBUG_MEMORY |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7649 | static void fast_stop(void) |
| 7650 | { |
| 7651 | struct proxy *p; |
| 7652 | p = proxy; |
| 7653 | while (p) { |
| 7654 | p->grace = 0; |
| 7655 | p = p->next; |
| 7656 | } |
| 7657 | soft_stop(); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7658 | } |
| 7659 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7660 | void sig_int(int sig) { |
| 7661 | /* This would normally be a hard stop, |
| 7662 | but we want to be sure about deallocation, |
| 7663 | and so on, so we do a soft stop with |
| 7664 | 0 GRACE time |
| 7665 | */ |
| 7666 | fast_stop(); |
| 7667 | /* If we are killed twice, we decide to die*/ |
| 7668 | signal(sig, SIG_DFL); |
| 7669 | } |
| 7670 | |
| 7671 | void sig_term(int sig) { |
| 7672 | /* This would normally be a hard stop, |
| 7673 | but we want to be sure about deallocation, |
| 7674 | and so on, so we do a soft stop with |
| 7675 | 0 GRACE time |
| 7676 | */ |
| 7677 | fast_stop(); |
| 7678 | /* If we are killed twice, we decide to die*/ |
| 7679 | signal(sig, SIG_DFL); |
| 7680 | } |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 7681 | #endif |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7682 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7683 | /* returns the pointer to an error in the replacement string, or NULL if OK */ |
| 7684 | char *chain_regex(struct hdr_exp **head, regex_t *preg, int action, char *replace) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7685 | struct hdr_exp *exp; |
| 7686 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7687 | if (replace != NULL) { |
| 7688 | char *err; |
| 7689 | err = check_replace_string(replace); |
| 7690 | if (err) |
| 7691 | return err; |
| 7692 | } |
| 7693 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7694 | while (*head != NULL) |
| 7695 | head = &(*head)->next; |
| 7696 | |
| 7697 | exp = calloc(1, sizeof(struct hdr_exp)); |
| 7698 | |
| 7699 | exp->preg = preg; |
| 7700 | exp->replace = replace; |
| 7701 | exp->action = action; |
| 7702 | *head = exp; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7703 | |
| 7704 | return NULL; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7705 | } |
| 7706 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7707 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7708 | /* |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7709 | * parse a line in a <global> section. Returns 0 if OK, -1 if error. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7710 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7711 | int cfg_parse_global(char *file, int linenum, char **args) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7712 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7713 | if (!strcmp(args[0], "global")) { /* new section */ |
| 7714 | /* no option, nothing special to do */ |
| 7715 | return 0; |
| 7716 | } |
| 7717 | else if (!strcmp(args[0], "daemon")) { |
| 7718 | global.mode |= MODE_DAEMON; |
| 7719 | } |
| 7720 | else if (!strcmp(args[0], "debug")) { |
| 7721 | global.mode |= MODE_DEBUG; |
| 7722 | } |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 7723 | else if (!strcmp(args[0], "noepoll")) { |
| 7724 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
| 7725 | } |
| 7726 | else if (!strcmp(args[0], "nopoll")) { |
| 7727 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
| 7728 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7729 | else if (!strcmp(args[0], "quiet")) { |
| 7730 | global.mode |= MODE_QUIET; |
| 7731 | } |
| 7732 | else if (!strcmp(args[0], "stats")) { |
| 7733 | global.mode |= MODE_STATS; |
| 7734 | } |
| 7735 | else if (!strcmp(args[0], "uid")) { |
| 7736 | if (global.uid != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7737 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7738 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7739 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7740 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7741 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7742 | return -1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7743 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7744 | global.uid = atol(args[1]); |
| 7745 | } |
| 7746 | else if (!strcmp(args[0], "gid")) { |
| 7747 | if (global.gid != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7748 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7749 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7750 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7751 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7752 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7753 | return -1; |
| 7754 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7755 | global.gid = atol(args[1]); |
| 7756 | } |
| 7757 | else if (!strcmp(args[0], "nbproc")) { |
| 7758 | if (global.nbproc != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7759 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7760 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7761 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7762 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7763 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7764 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7765 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7766 | global.nbproc = atol(args[1]); |
| 7767 | } |
| 7768 | else if (!strcmp(args[0], "maxconn")) { |
| 7769 | if (global.maxconn != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7770 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7771 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7772 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7773 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7774 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7775 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7776 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7777 | global.maxconn = atol(args[1]); |
Willy TARREAU | 13032e7 | 2006-03-12 17:31:45 +0100 | [diff] [blame] | 7778 | #ifdef SYSTEM_MAXCONN |
| 7779 | if (global.maxconn > DEFAULT_MAXCONN && cfg_maxconn <= DEFAULT_MAXCONN) { |
| 7780 | Alert("parsing [%s:%d] : maxconn value %d too high for this system.\nLimiting to %d. Please use '-n' to force the value.\n", file, linenum, global.maxconn, DEFAULT_MAXCONN); |
| 7781 | global.maxconn = DEFAULT_MAXCONN; |
| 7782 | } |
| 7783 | #endif /* SYSTEM_MAXCONN */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7784 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 7785 | else if (!strcmp(args[0], "ulimit-n")) { |
| 7786 | if (global.rlimit_nofile != 0) { |
| 7787 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
| 7788 | return 0; |
| 7789 | } |
| 7790 | if (*(args[1]) == 0) { |
| 7791 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
| 7792 | return -1; |
| 7793 | } |
| 7794 | global.rlimit_nofile = atol(args[1]); |
| 7795 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7796 | else if (!strcmp(args[0], "chroot")) { |
| 7797 | if (global.chroot != NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7798 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7799 | return 0; |
| 7800 | } |
| 7801 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7802 | Alert("parsing [%s:%d] : '%s' expects a directory as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7803 | return -1; |
| 7804 | } |
| 7805 | global.chroot = strdup(args[1]); |
| 7806 | } |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 7807 | else if (!strcmp(args[0], "pidfile")) { |
| 7808 | if (global.pidfile != NULL) { |
| 7809 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
| 7810 | return 0; |
| 7811 | } |
| 7812 | if (*(args[1]) == 0) { |
| 7813 | Alert("parsing [%s:%d] : '%s' expects a file name as an argument.\n", file, linenum, args[0]); |
| 7814 | return -1; |
| 7815 | } |
| 7816 | global.pidfile = strdup(args[1]); |
| 7817 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7818 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 7819 | struct sockaddr_in *sa; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7820 | int facility, level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7821 | |
| 7822 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7823 | Alert("parsing [%s:%d] : '%s' expects <address> and <facility> as arguments.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7824 | return -1; |
| 7825 | } |
| 7826 | |
| 7827 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 7828 | if (!strcmp(log_facilities[facility], args[2])) |
| 7829 | break; |
| 7830 | |
| 7831 | if (facility >= NB_LOG_FACILITIES) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7832 | Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7833 | exit(1); |
| 7834 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7835 | |
| 7836 | level = 7; /* max syslog level = debug */ |
| 7837 | if (*(args[3])) { |
| 7838 | while (level >= 0 && strcmp(log_levels[level], args[3])) |
| 7839 | level--; |
| 7840 | if (level < 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7841 | Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7842 | exit(1); |
| 7843 | } |
| 7844 | } |
| 7845 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7846 | sa = str2sa(args[1]); |
| 7847 | if (!sa->sin_port) |
| 7848 | sa->sin_port = htons(SYSLOG_PORT); |
| 7849 | |
| 7850 | if (global.logfac1 == -1) { |
| 7851 | global.logsrv1 = *sa; |
| 7852 | global.logfac1 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7853 | global.loglev1 = level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7854 | } |
| 7855 | else if (global.logfac2 == -1) { |
| 7856 | global.logsrv2 = *sa; |
| 7857 | global.logfac2 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7858 | global.loglev2 = level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7859 | } |
| 7860 | else { |
| 7861 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
| 7862 | return -1; |
| 7863 | } |
| 7864 | |
| 7865 | } |
| 7866 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7867 | Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "global"); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7868 | return -1; |
| 7869 | } |
| 7870 | return 0; |
| 7871 | } |
| 7872 | |
| 7873 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7874 | void init_default_instance() { |
| 7875 | memset(&defproxy, 0, sizeof(defproxy)); |
| 7876 | defproxy.mode = PR_MODE_TCP; |
| 7877 | defproxy.state = PR_STNEW; |
| 7878 | defproxy.maxconn = cfg_maxpconn; |
| 7879 | defproxy.conn_retries = CONN_RETRIES; |
| 7880 | defproxy.logfac1 = defproxy.logfac2 = -1; /* log disabled */ |
| 7881 | } |
| 7882 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7883 | /* |
| 7884 | * parse a line in a <listen> section. Returns 0 if OK, -1 if error. |
| 7885 | */ |
| 7886 | int cfg_parse_listen(char *file, int linenum, char **args) { |
| 7887 | static struct proxy *curproxy = NULL; |
| 7888 | struct server *newsrv = NULL; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7889 | char *err; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7890 | int rc; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7891 | |
| 7892 | if (!strcmp(args[0], "listen")) { /* new proxy */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7893 | if (!*args[1]) { |
| 7894 | Alert("parsing [%s:%d] : '%s' expects an <id> argument and\n" |
| 7895 | " optionnally supports [addr1]:port1[-end1]{,[addr]:port[-end]}...\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7896 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7897 | return -1; |
| 7898 | } |
| 7899 | |
| 7900 | if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7901 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7902 | return -1; |
| 7903 | } |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 7904 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7905 | curproxy->next = proxy; |
| 7906 | proxy = curproxy; |
willy tarreau | dfece23 | 2006-05-02 00:19:57 +0200 | [diff] [blame] | 7907 | LIST_INIT(&curproxy->pendconns); |
| 7908 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7909 | curproxy->id = strdup(args[1]); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 7910 | |
| 7911 | /* parse the listener address if any */ |
| 7912 | if (*args[2]) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7913 | curproxy->listen = str2listener(args[2], curproxy->listen); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 7914 | if (!curproxy->listen) |
| 7915 | return -1; |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 7916 | global.maxsock++; |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 7917 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7918 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7919 | /* set default values */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7920 | curproxy->state = defproxy.state; |
| 7921 | curproxy->maxconn = defproxy.maxconn; |
| 7922 | curproxy->conn_retries = defproxy.conn_retries; |
| 7923 | curproxy->options = defproxy.options; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7924 | |
| 7925 | if (defproxy.check_req) |
| 7926 | curproxy->check_req = strdup(defproxy.check_req); |
| 7927 | curproxy->check_len = defproxy.check_len; |
| 7928 | |
| 7929 | if (defproxy.cookie_name) |
| 7930 | curproxy->cookie_name = strdup(defproxy.cookie_name); |
| 7931 | curproxy->cookie_len = defproxy.cookie_len; |
| 7932 | |
| 7933 | if (defproxy.capture_name) |
| 7934 | curproxy->capture_name = strdup(defproxy.capture_name); |
| 7935 | curproxy->capture_namelen = defproxy.capture_namelen; |
| 7936 | curproxy->capture_len = defproxy.capture_len; |
| 7937 | |
| 7938 | if (defproxy.errmsg.msg400) |
| 7939 | curproxy->errmsg.msg400 = strdup(defproxy.errmsg.msg400); |
| 7940 | curproxy->errmsg.len400 = defproxy.errmsg.len400; |
| 7941 | |
| 7942 | if (defproxy.errmsg.msg403) |
| 7943 | curproxy->errmsg.msg403 = strdup(defproxy.errmsg.msg403); |
| 7944 | curproxy->errmsg.len403 = defproxy.errmsg.len403; |
| 7945 | |
| 7946 | if (defproxy.errmsg.msg408) |
| 7947 | curproxy->errmsg.msg408 = strdup(defproxy.errmsg.msg408); |
| 7948 | curproxy->errmsg.len408 = defproxy.errmsg.len408; |
| 7949 | |
| 7950 | if (defproxy.errmsg.msg500) |
| 7951 | curproxy->errmsg.msg500 = strdup(defproxy.errmsg.msg500); |
| 7952 | curproxy->errmsg.len500 = defproxy.errmsg.len500; |
| 7953 | |
| 7954 | if (defproxy.errmsg.msg502) |
| 7955 | curproxy->errmsg.msg502 = strdup(defproxy.errmsg.msg502); |
| 7956 | curproxy->errmsg.len502 = defproxy.errmsg.len502; |
| 7957 | |
| 7958 | if (defproxy.errmsg.msg503) |
| 7959 | curproxy->errmsg.msg503 = strdup(defproxy.errmsg.msg503); |
| 7960 | curproxy->errmsg.len503 = defproxy.errmsg.len503; |
| 7961 | |
| 7962 | if (defproxy.errmsg.msg504) |
| 7963 | curproxy->errmsg.msg504 = strdup(defproxy.errmsg.msg504); |
| 7964 | curproxy->errmsg.len504 = defproxy.errmsg.len504; |
| 7965 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7966 | curproxy->clitimeout = defproxy.clitimeout; |
| 7967 | curproxy->contimeout = defproxy.contimeout; |
| 7968 | curproxy->srvtimeout = defproxy.srvtimeout; |
| 7969 | curproxy->mode = defproxy.mode; |
| 7970 | curproxy->logfac1 = defproxy.logfac1; |
| 7971 | curproxy->logsrv1 = defproxy.logsrv1; |
| 7972 | curproxy->loglev1 = defproxy.loglev1; |
| 7973 | curproxy->logfac2 = defproxy.logfac2; |
| 7974 | curproxy->logsrv2 = defproxy.logsrv2; |
| 7975 | curproxy->loglev2 = defproxy.loglev2; |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7976 | curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7977 | curproxy->grace = defproxy.grace; |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 7978 | curproxy->uri_auth = defproxy.uri_auth; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7979 | curproxy->source_addr = defproxy.source_addr; |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 7980 | curproxy->mon_net = defproxy.mon_net; |
| 7981 | curproxy->mon_mask = defproxy.mon_mask; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7982 | return 0; |
| 7983 | } |
| 7984 | else if (!strcmp(args[0], "defaults")) { /* use this one to assign default values */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7985 | /* some variables may have already been initialized earlier */ |
| 7986 | if (defproxy.check_req) free(defproxy.check_req); |
| 7987 | if (defproxy.cookie_name) free(defproxy.cookie_name); |
| 7988 | if (defproxy.capture_name) free(defproxy.capture_name); |
| 7989 | if (defproxy.errmsg.msg400) free(defproxy.errmsg.msg400); |
| 7990 | if (defproxy.errmsg.msg403) free(defproxy.errmsg.msg403); |
| 7991 | if (defproxy.errmsg.msg408) free(defproxy.errmsg.msg408); |
| 7992 | if (defproxy.errmsg.msg500) free(defproxy.errmsg.msg500); |
| 7993 | if (defproxy.errmsg.msg502) free(defproxy.errmsg.msg502); |
| 7994 | if (defproxy.errmsg.msg503) free(defproxy.errmsg.msg503); |
| 7995 | if (defproxy.errmsg.msg504) free(defproxy.errmsg.msg504); |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 7996 | /* we cannot free uri_auth because it might already be used */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7997 | init_default_instance(); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7998 | curproxy = &defproxy; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7999 | return 0; |
| 8000 | } |
| 8001 | else if (curproxy == NULL) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8002 | Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8003 | return -1; |
| 8004 | } |
| 8005 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8006 | if (!strcmp(args[0], "bind")) { /* new listen addresses */ |
| 8007 | if (curproxy == &defproxy) { |
| 8008 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8009 | return -1; |
| 8010 | } |
| 8011 | |
| 8012 | if (strchr(args[1], ':') == NULL) { |
| 8013 | Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n", |
| 8014 | file, linenum, args[0]); |
| 8015 | return -1; |
| 8016 | } |
| 8017 | curproxy->listen = str2listener(args[1], curproxy->listen); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8018 | if (!curproxy->listen) |
| 8019 | return -1; |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 8020 | global.maxsock++; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8021 | return 0; |
| 8022 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8023 | else if (!strcmp(args[0], "monitor-net")) { /* set the range of IPs to ignore */ |
| 8024 | if (!*args[1] || !str2net(args[1], &curproxy->mon_net, &curproxy->mon_mask)) { |
| 8025 | Alert("parsing [%s:%d] : '%s' expects address[/mask].\n", |
| 8026 | file, linenum, args[0]); |
| 8027 | return -1; |
| 8028 | } |
| 8029 | /* flush useless bits */ |
| 8030 | curproxy->mon_net.s_addr &= curproxy->mon_mask.s_addr; |
| 8031 | return 0; |
| 8032 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8033 | else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8034 | if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP; |
| 8035 | else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP; |
| 8036 | else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH; |
| 8037 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8038 | Alert("parsing [%s:%d] : unknown proxy mode '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8039 | return -1; |
| 8040 | } |
| 8041 | } |
| 8042 | else if (!strcmp(args[0], "disabled")) { /* disables this proxy */ |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 8043 | curproxy->state = PR_STSTOPPED; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8044 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8045 | else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */ |
| 8046 | curproxy->state = PR_STNEW; |
| 8047 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8048 | else if (!strcmp(args[0], "cookie")) { /* cookie name */ |
| 8049 | int cur_arg; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 8050 | // if (curproxy == &defproxy) { |
| 8051 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8052 | // return -1; |
| 8053 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8054 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8055 | if (curproxy->cookie_name != NULL) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 8056 | // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n", |
| 8057 | // file, linenum); |
| 8058 | // return 0; |
| 8059 | free(curproxy->cookie_name); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8060 | } |
| 8061 | |
| 8062 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8063 | Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n", |
| 8064 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8065 | return -1; |
| 8066 | } |
| 8067 | curproxy->cookie_name = strdup(args[1]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8068 | curproxy->cookie_len = strlen(curproxy->cookie_name); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8069 | |
| 8070 | cur_arg = 2; |
| 8071 | while (*(args[cur_arg])) { |
| 8072 | if (!strcmp(args[cur_arg], "rewrite")) { |
| 8073 | curproxy->options |= PR_O_COOK_RW; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8074 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8075 | else if (!strcmp(args[cur_arg], "indirect")) { |
| 8076 | curproxy->options |= PR_O_COOK_IND; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8077 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8078 | else if (!strcmp(args[cur_arg], "insert")) { |
| 8079 | curproxy->options |= PR_O_COOK_INS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8080 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 8081 | else if (!strcmp(args[cur_arg], "nocache")) { |
| 8082 | curproxy->options |= PR_O_COOK_NOC; |
| 8083 | } |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 8084 | else if (!strcmp(args[cur_arg], "postonly")) { |
| 8085 | curproxy->options |= PR_O_COOK_POST; |
| 8086 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 8087 | else if (!strcmp(args[cur_arg], "prefix")) { |
| 8088 | curproxy->options |= PR_O_COOK_PFX; |
| 8089 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8090 | else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 8091 | Alert("parsing [%s:%d] : '%s' supports 'rewrite', 'insert', 'prefix', 'indirect', 'nocache' and 'postonly' options.\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8092 | file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8093 | return -1; |
| 8094 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8095 | cur_arg++; |
| 8096 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 8097 | if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND))) { |
| 8098 | Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' modes are incompatible.\n", |
| 8099 | file, linenum); |
| 8100 | return -1; |
| 8101 | } |
| 8102 | |
| 8103 | if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_INS|PR_O_COOK_PFX))) { |
| 8104 | Alert("parsing [%s:%d] : cookie 'rewrite', 'insert' and 'prefix' modes are incompatible.\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8105 | file, linenum); |
| 8106 | return -1; |
| 8107 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8108 | }/* end else if (!strcmp(args[0], "cookie")) */ |
| 8109 | else if (!strcmp(args[0], "appsession")) { /* cookie name */ |
| 8110 | // if (curproxy == &defproxy) { |
| 8111 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8112 | // return -1; |
| 8113 | // } |
| 8114 | |
| 8115 | if (curproxy->appsession_name != NULL) { |
| 8116 | // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n", |
| 8117 | // file, linenum); |
| 8118 | // return 0; |
| 8119 | free(curproxy->appsession_name); |
| 8120 | } |
| 8121 | |
| 8122 | if (*(args[5]) == 0) { |
| 8123 | Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n", |
| 8124 | file, linenum, args[0]); |
| 8125 | return -1; |
| 8126 | } |
| 8127 | have_appsession = 1; |
| 8128 | curproxy->appsession_name = strdup(args[1]); |
| 8129 | curproxy->appsession_name_len = strlen(curproxy->appsession_name); |
| 8130 | curproxy->appsession_len = atoi(args[3]); |
| 8131 | curproxy->appsession_timeout = atoi(args[5]); |
| 8132 | rc = chtbl_init(&(curproxy->htbl_proxy), TBLSIZ, hashpjw, match_str, destroy); |
| 8133 | if (rc) { |
| 8134 | Alert("Error Init Appsession Hashtable.\n"); |
| 8135 | return -1; |
| 8136 | } |
| 8137 | } /* Url App Session */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 8138 | else if (!strcmp(args[0], "capture")) { |
| 8139 | if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */ |
| 8140 | // if (curproxy == &defproxy) { |
| 8141 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8142 | // return -1; |
| 8143 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8144 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 8145 | if (curproxy->capture_name != NULL) { |
| 8146 | // Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", |
| 8147 | // file, linenum, args[0]); |
| 8148 | // return 0; |
| 8149 | free(curproxy->capture_name); |
| 8150 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8151 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 8152 | if (*(args[4]) == 0) { |
| 8153 | Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n", |
| 8154 | file, linenum, args[0]); |
| 8155 | return -1; |
| 8156 | } |
| 8157 | curproxy->capture_name = strdup(args[2]); |
| 8158 | curproxy->capture_namelen = strlen(curproxy->capture_name); |
| 8159 | curproxy->capture_len = atol(args[4]); |
| 8160 | if (curproxy->capture_len >= CAPTURE_LEN) { |
| 8161 | Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n", |
| 8162 | file, linenum, CAPTURE_LEN - 1); |
| 8163 | curproxy->capture_len = CAPTURE_LEN - 1; |
| 8164 | } |
| 8165 | curproxy->to_log |= LW_COOKIE; |
| 8166 | } |
| 8167 | else if (!strcmp(args[1], "request") && !strcmp(args[2], "header")) { |
| 8168 | struct cap_hdr *hdr; |
| 8169 | |
| 8170 | if (curproxy == &defproxy) { |
| 8171 | Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]); |
| 8172 | return -1; |
| 8173 | } |
| 8174 | |
| 8175 | if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) { |
| 8176 | Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n", |
| 8177 | file, linenum, args[0], args[1]); |
| 8178 | return -1; |
| 8179 | } |
| 8180 | |
| 8181 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 8182 | hdr->next = curproxy->req_cap; |
| 8183 | hdr->name = strdup(args[3]); |
| 8184 | hdr->namelen = strlen(args[3]); |
| 8185 | hdr->len = atol(args[5]); |
| 8186 | hdr->index = curproxy->nb_req_cap++; |
| 8187 | curproxy->req_cap = hdr; |
| 8188 | curproxy->to_log |= LW_REQHDR; |
| 8189 | } |
| 8190 | else if (!strcmp(args[1], "response") && !strcmp(args[2], "header")) { |
| 8191 | struct cap_hdr *hdr; |
| 8192 | |
| 8193 | if (curproxy == &defproxy) { |
| 8194 | Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]); |
| 8195 | return -1; |
| 8196 | } |
| 8197 | |
| 8198 | if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) { |
| 8199 | Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n", |
| 8200 | file, linenum, args[0], args[1]); |
| 8201 | return -1; |
| 8202 | } |
| 8203 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 8204 | hdr->next = curproxy->rsp_cap; |
| 8205 | hdr->name = strdup(args[3]); |
| 8206 | hdr->namelen = strlen(args[3]); |
| 8207 | hdr->len = atol(args[5]); |
| 8208 | hdr->index = curproxy->nb_rsp_cap++; |
| 8209 | curproxy->rsp_cap = hdr; |
| 8210 | curproxy->to_log |= LW_RSPHDR; |
| 8211 | } |
| 8212 | else { |
| 8213 | Alert("parsing [%s:%d] : '%s' expects 'cookie' or 'request header' or 'response header'.\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8214 | file, linenum, args[0]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8215 | return -1; |
| 8216 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8217 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8218 | else if (!strcmp(args[0], "contimeout")) { /* connect timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8219 | if (curproxy->contimeout != defproxy.contimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8220 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8221 | return 0; |
| 8222 | } |
| 8223 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8224 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 8225 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8226 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8227 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8228 | curproxy->contimeout = atol(args[1]); |
| 8229 | } |
| 8230 | else if (!strcmp(args[0], "clitimeout")) { /* client timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8231 | if (curproxy->clitimeout != defproxy.clitimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8232 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", |
| 8233 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8234 | return 0; |
| 8235 | } |
| 8236 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8237 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 8238 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8239 | return -1; |
| 8240 | } |
| 8241 | curproxy->clitimeout = atol(args[1]); |
| 8242 | } |
| 8243 | else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8244 | if (curproxy->srvtimeout != defproxy.srvtimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8245 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8246 | return 0; |
| 8247 | } |
| 8248 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8249 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 8250 | file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8251 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8252 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8253 | curproxy->srvtimeout = atol(args[1]); |
| 8254 | } |
| 8255 | else if (!strcmp(args[0], "retries")) { /* connection retries */ |
| 8256 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8257 | Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n", |
| 8258 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8259 | return -1; |
| 8260 | } |
| 8261 | curproxy->conn_retries = atol(args[1]); |
| 8262 | } |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 8263 | else if (!strcmp(args[0], "stats")) { |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 8264 | if (curproxy != &defproxy && curproxy->uri_auth == defproxy.uri_auth) |
| 8265 | curproxy->uri_auth = NULL; /* we must detach from the default config */ |
| 8266 | |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 8267 | if (*(args[1]) == 0) { |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 8268 | Alert("parsing [%s:%d] : '%s' expects 'uri', 'realm', 'auth', 'scope' or 'enable'.\n", file, linenum, args[0]); |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 8269 | return -1; |
| 8270 | } else if (!strcmp(args[1], "uri")) { |
| 8271 | if (*(args[2]) == 0) { |
| 8272 | Alert("parsing [%s:%d] : 'uri' needs an URI prefix.\n", file, linenum); |
| 8273 | return -1; |
| 8274 | } else if (!stats_set_uri(&curproxy->uri_auth, args[2])) { |
| 8275 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 8276 | return -1; |
| 8277 | } |
| 8278 | } else if (!strcmp(args[1], "realm")) { |
| 8279 | if (*(args[2]) == 0) { |
| 8280 | Alert("parsing [%s:%d] : 'realm' needs an realm name.\n", file, linenum); |
| 8281 | return -1; |
| 8282 | } else if (!stats_set_realm(&curproxy->uri_auth, args[2])) { |
| 8283 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 8284 | return -1; |
| 8285 | } |
| 8286 | } else if (!strcmp(args[1], "auth")) { |
| 8287 | if (*(args[2]) == 0) { |
| 8288 | Alert("parsing [%s:%d] : 'auth' needs a user:password account.\n", file, linenum); |
| 8289 | return -1; |
| 8290 | } else if (!stats_add_auth(&curproxy->uri_auth, args[2])) { |
| 8291 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 8292 | return -1; |
| 8293 | } |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 8294 | } else if (!strcmp(args[1], "scope")) { |
| 8295 | if (*(args[2]) == 0) { |
| 8296 | Alert("parsing [%s:%d] : 'scope' needs a proxy name.\n", file, linenum); |
| 8297 | return -1; |
| 8298 | } else if (!stats_add_scope(&curproxy->uri_auth, args[2])) { |
| 8299 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 8300 | return -1; |
| 8301 | } |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 8302 | } else if (!strcmp(args[1], "enable")) { |
| 8303 | if (!stats_check_init_uri_auth(&curproxy->uri_auth)) { |
| 8304 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 8305 | return -1; |
| 8306 | } |
| 8307 | } else { |
| 8308 | Alert("parsing [%s:%d] : unknown stats parameter '%s' (expects 'uri', 'realm', 'auth' or 'enable').\n", |
| 8309 | file, linenum, args[0]); |
| 8310 | return -1; |
| 8311 | } |
| 8312 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8313 | else if (!strcmp(args[0], "option")) { |
| 8314 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8315 | Alert("parsing [%s:%d] : '%s' expects an option name.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8316 | return -1; |
| 8317 | } |
| 8318 | if (!strcmp(args[1], "redispatch")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 8319 | /* enable reconnections to dispatch */ |
| 8320 | curproxy->options |= PR_O_REDISP; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 8321 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8322 | else if (!strcmp(args[1], "transparent")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 8323 | /* enable transparent proxy connections */ |
| 8324 | curproxy->options |= PR_O_TRANSP; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8325 | #endif |
| 8326 | else if (!strcmp(args[1], "keepalive")) |
| 8327 | /* enable keep-alive */ |
| 8328 | curproxy->options |= PR_O_KEEPALIVE; |
| 8329 | else if (!strcmp(args[1], "forwardfor")) |
| 8330 | /* insert x-forwarded-for field */ |
| 8331 | curproxy->options |= PR_O_FWDFOR; |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 8332 | else if (!strcmp(args[1], "logasap")) |
| 8333 | /* log as soon as possible, without waiting for the session to complete */ |
| 8334 | curproxy->options |= PR_O_LOGASAP; |
willy tarreau | 03a92de | 2006-05-21 18:26:53 +0200 | [diff] [blame^] | 8335 | else if (!strcmp(args[1], "abortonclose")) |
| 8336 | /* abort connection if client closes during queue or connect() */ |
| 8337 | curproxy->options |= PR_O_ABRT_CLOSE; |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 8338 | else if (!strcmp(args[1], "httpclose")) |
| 8339 | /* force connection: close in both directions in HTTP mode */ |
| 8340 | curproxy->options |= PR_O_HTTP_CLOSE; |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 8341 | else if (!strcmp(args[1], "forceclose")) |
| 8342 | /* force connection: close in both directions in HTTP mode and enforce end of session */ |
| 8343 | curproxy->options |= PR_O_FORCE_CLO | PR_O_HTTP_CLOSE; |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 8344 | else if (!strcmp(args[1], "checkcache")) |
| 8345 | /* require examination of cacheability of the 'set-cookie' field */ |
| 8346 | curproxy->options |= PR_O_CHK_CACHE; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 8347 | else if (!strcmp(args[1], "httplog")) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8348 | /* generate a complete HTTP log */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 8349 | curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_REQ | LW_PXID | LW_RESP | LW_BYTES; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 8350 | else if (!strcmp(args[1], "tcplog")) |
| 8351 | /* generate a detailed TCP log */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 8352 | curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_PXID | LW_BYTES; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 8353 | else if (!strcmp(args[1], "dontlognull")) { |
| 8354 | /* don't log empty requests */ |
| 8355 | curproxy->options |= PR_O_NULLNOLOG; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 8356 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8357 | else if (!strcmp(args[1], "tcpka")) { |
| 8358 | /* enable TCP keep-alives on client and server sessions */ |
| 8359 | curproxy->options |= PR_O_TCP_CLI_KA | PR_O_TCP_SRV_KA; |
| 8360 | } |
| 8361 | else if (!strcmp(args[1], "clitcpka")) { |
| 8362 | /* enable TCP keep-alives on client sessions */ |
| 8363 | curproxy->options |= PR_O_TCP_CLI_KA; |
| 8364 | } |
| 8365 | else if (!strcmp(args[1], "srvtcpka")) { |
| 8366 | /* enable TCP keep-alives on server sessions */ |
| 8367 | curproxy->options |= PR_O_TCP_SRV_KA; |
| 8368 | } |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 8369 | else if (!strcmp(args[1], "allbackups")) { |
| 8370 | /* Use all backup servers simultaneously */ |
| 8371 | curproxy->options |= PR_O_USE_ALL_BK; |
| 8372 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 8373 | else if (!strcmp(args[1], "httpchk")) { |
| 8374 | /* use HTTP request to check servers' health */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 8375 | if (curproxy->check_req != NULL) { |
| 8376 | free(curproxy->check_req); |
| 8377 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 8378 | curproxy->options |= PR_O_HTTP_CHK; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 8379 | if (!*args[2]) { /* no argument */ |
| 8380 | curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */ |
| 8381 | curproxy->check_len = strlen(DEF_CHECK_REQ); |
| 8382 | } else if (!*args[3]) { /* one argument : URI */ |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 8383 | int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n"); |
| 8384 | curproxy->check_req = (char *)malloc(reqlen); |
| 8385 | curproxy->check_len = snprintf(curproxy->check_req, reqlen, |
| 8386 | "OPTIONS %s HTTP/1.0\r\n\r\n", args[2]); /* URI to use */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 8387 | } else { /* more arguments : METHOD URI [HTTP_VER] */ |
| 8388 | int reqlen = strlen(args[2]) + strlen(args[3]) + 3 + strlen("\r\n\r\n"); |
| 8389 | if (*args[4]) |
| 8390 | reqlen += strlen(args[4]); |
| 8391 | else |
| 8392 | reqlen += strlen("HTTP/1.0"); |
| 8393 | |
| 8394 | curproxy->check_req = (char *)malloc(reqlen); |
| 8395 | curproxy->check_len = snprintf(curproxy->check_req, reqlen, |
| 8396 | "%s %s %s\r\n\r\n", args[2], args[3], *args[4]?args[4]:"HTTP/1.0"); |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 8397 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 8398 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8399 | else if (!strcmp(args[1], "persist")) { |
| 8400 | /* persist on using the server specified by the cookie, even when it's down */ |
| 8401 | curproxy->options |= PR_O_PERSIST; |
| 8402 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8403 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8404 | Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8405 | return -1; |
| 8406 | } |
| 8407 | return 0; |
| 8408 | } |
| 8409 | else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) { |
| 8410 | /* enable reconnections to dispatch */ |
| 8411 | curproxy->options |= PR_O_REDISP; |
| 8412 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 8413 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8414 | else if (!strcmp(args[0], "transparent")) { |
| 8415 | /* enable transparent proxy connections */ |
| 8416 | curproxy->options |= PR_O_TRANSP; |
| 8417 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 8418 | #endif |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8419 | else if (!strcmp(args[0], "maxconn")) { /* maxconn */ |
| 8420 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8421 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8422 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8423 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8424 | curproxy->maxconn = atol(args[1]); |
| 8425 | } |
| 8426 | else if (!strcmp(args[0], "grace")) { /* grace time (ms) */ |
| 8427 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8428 | Alert("parsing [%s:%d] : '%s' expects a time in milliseconds.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8429 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8430 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8431 | curproxy->grace = atol(args[1]); |
| 8432 | } |
| 8433 | else if (!strcmp(args[0], "dispatch")) { /* dispatch address */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8434 | if (curproxy == &defproxy) { |
| 8435 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8436 | return -1; |
| 8437 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8438 | if (strchr(args[1], ':') == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8439 | Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8440 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8441 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8442 | curproxy->dispatch_addr = *str2sa(args[1]); |
| 8443 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8444 | else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8445 | if (*(args[1])) { |
| 8446 | if (!strcmp(args[1], "roundrobin")) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 8447 | curproxy->options |= PR_O_BALANCE_RR; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8448 | } |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 8449 | else if (!strcmp(args[1], "source")) { |
| 8450 | curproxy->options |= PR_O_BALANCE_SH; |
| 8451 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8452 | else { |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 8453 | Alert("parsing [%s:%d] : '%s' only supports 'roundrobin' and 'source' options.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8454 | return -1; |
| 8455 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 8456 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8457 | else /* if no option is set, use round-robin by default */ |
| 8458 | curproxy->options |= PR_O_BALANCE_RR; |
| 8459 | } |
| 8460 | else if (!strcmp(args[0], "server")) { /* server address */ |
| 8461 | int cur_arg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8462 | char *rport; |
| 8463 | char *raddr; |
| 8464 | short realport; |
| 8465 | int do_check; |
| 8466 | |
| 8467 | if (curproxy == &defproxy) { |
| 8468 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8469 | return -1; |
| 8470 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 8471 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8472 | if (!*args[2]) { |
| 8473 | Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8474 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8475 | return -1; |
| 8476 | } |
| 8477 | if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) { |
| 8478 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 8479 | return -1; |
| 8480 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 8481 | |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 8482 | /* the servers are linked backwards first */ |
| 8483 | newsrv->next = curproxy->srv; |
| 8484 | curproxy->srv = newsrv; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8485 | newsrv->proxy = curproxy; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8486 | |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 8487 | LIST_INIT(&newsrv->pendconns); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8488 | do_check = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8489 | newsrv->state = SRV_RUNNING; /* early server setup */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8490 | newsrv->id = strdup(args[1]); |
| 8491 | |
| 8492 | /* several ways to check the port component : |
| 8493 | * - IP => port=+0, relative |
| 8494 | * - IP: => port=+0, relative |
| 8495 | * - IP:N => port=N, absolute |
| 8496 | * - IP:+N => port=+N, relative |
| 8497 | * - IP:-N => port=-N, relative |
| 8498 | */ |
| 8499 | raddr = strdup(args[2]); |
| 8500 | rport = strchr(raddr, ':'); |
| 8501 | if (rport) { |
| 8502 | *rport++ = 0; |
| 8503 | realport = atol(rport); |
| 8504 | if (!isdigit((int)*rport)) |
| 8505 | newsrv->state |= SRV_MAPPORTS; |
| 8506 | } else { |
| 8507 | realport = 0; |
| 8508 | newsrv->state |= SRV_MAPPORTS; |
| 8509 | } |
| 8510 | |
| 8511 | newsrv->addr = *str2sa(raddr); |
| 8512 | newsrv->addr.sin_port = htons(realport); |
| 8513 | free(raddr); |
| 8514 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8515 | newsrv->curfd = -1; /* no health-check in progress */ |
| 8516 | newsrv->inter = DEF_CHKINTR; |
| 8517 | newsrv->rise = DEF_RISETIME; |
| 8518 | newsrv->fall = DEF_FALLTIME; |
| 8519 | newsrv->health = newsrv->rise; /* up, but will fall down at first failure */ |
| 8520 | cur_arg = 3; |
| 8521 | while (*args[cur_arg]) { |
| 8522 | if (!strcmp(args[cur_arg], "cookie")) { |
| 8523 | newsrv->cookie = strdup(args[cur_arg + 1]); |
| 8524 | newsrv->cklen = strlen(args[cur_arg + 1]); |
| 8525 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8526 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8527 | else if (!strcmp(args[cur_arg], "rise")) { |
| 8528 | newsrv->rise = atol(args[cur_arg + 1]); |
| 8529 | newsrv->health = newsrv->rise; |
| 8530 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8531 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8532 | else if (!strcmp(args[cur_arg], "fall")) { |
| 8533 | newsrv->fall = atol(args[cur_arg + 1]); |
| 8534 | cur_arg += 2; |
| 8535 | } |
| 8536 | else if (!strcmp(args[cur_arg], "inter")) { |
| 8537 | newsrv->inter = atol(args[cur_arg + 1]); |
| 8538 | cur_arg += 2; |
| 8539 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8540 | else if (!strcmp(args[cur_arg], "port")) { |
| 8541 | newsrv->check_port = atol(args[cur_arg + 1]); |
| 8542 | cur_arg += 2; |
| 8543 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8544 | else if (!strcmp(args[cur_arg], "backup")) { |
| 8545 | newsrv->state |= SRV_BACKUP; |
| 8546 | cur_arg ++; |
| 8547 | } |
willy tarreau | e3f023f | 2006-04-08 21:52:24 +0200 | [diff] [blame] | 8548 | else if (!strcmp(args[cur_arg], "weight")) { |
| 8549 | int w; |
| 8550 | w = atol(args[cur_arg + 1]); |
| 8551 | if (w < 1 || w > 256) { |
| 8552 | Alert("parsing [%s:%d] : weight of server %s is not within 1 and 256 (%d).\n", |
| 8553 | file, linenum, newsrv->id, w); |
| 8554 | return -1; |
| 8555 | } |
| 8556 | newsrv->uweight = w - 1; |
| 8557 | cur_arg += 2; |
| 8558 | } |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 8559 | else if (!strcmp(args[cur_arg], "maxconn")) { |
| 8560 | newsrv->maxconn = atol(args[cur_arg + 1]); |
| 8561 | cur_arg += 2; |
| 8562 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8563 | else if (!strcmp(args[cur_arg], "check")) { |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 8564 | global.maxsock++; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8565 | do_check = 1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8566 | cur_arg += 1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 8567 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 8568 | else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */ |
| 8569 | if (!*args[cur_arg + 1]) { |
| 8570 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n", |
| 8571 | file, linenum, "source"); |
| 8572 | return -1; |
| 8573 | } |
| 8574 | newsrv->state |= SRV_BIND_SRC; |
| 8575 | newsrv->source_addr = *str2sa(args[cur_arg + 1]); |
| 8576 | cur_arg += 2; |
| 8577 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8578 | else { |
willy tarreau | e3f023f | 2006-04-08 21:52:24 +0200 | [diff] [blame] | 8579 | Alert("parsing [%s:%d] : server %s only supports options 'backup', 'cookie', 'check', 'inter', 'rise', 'fall', 'port', 'source', and 'weight'.\n", |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8580 | file, linenum, newsrv->id); |
| 8581 | return -1; |
| 8582 | } |
| 8583 | } |
| 8584 | |
| 8585 | if (do_check) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8586 | if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS)) |
| 8587 | newsrv->check_port = realport; /* by default */ |
| 8588 | if (!newsrv->check_port) { |
| 8589 | Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8590 | file, linenum, newsrv->id); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8591 | return -1; |
| 8592 | } |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 8593 | newsrv->state |= SRV_CHECKED; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8594 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8595 | |
willy tarreau | 62084d4 | 2006-03-24 18:57:41 +0100 | [diff] [blame] | 8596 | if (newsrv->state & SRV_BACKUP) |
| 8597 | curproxy->srv_bck++; |
| 8598 | else |
| 8599 | curproxy->srv_act++; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8600 | } |
| 8601 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 8602 | struct sockaddr_in *sa; |
| 8603 | int facility; |
| 8604 | |
| 8605 | if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) { |
| 8606 | curproxy->logfac1 = global.logfac1; |
| 8607 | curproxy->logsrv1 = global.logsrv1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8608 | curproxy->loglev1 = global.loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8609 | curproxy->logfac2 = global.logfac2; |
| 8610 | curproxy->logsrv2 = global.logsrv2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8611 | curproxy->loglev2 = global.loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8612 | } |
| 8613 | else if (*(args[1]) && *(args[2])) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8614 | int level; |
| 8615 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8616 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 8617 | if (!strcmp(log_facilities[facility], args[2])) |
| 8618 | break; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8619 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8620 | if (facility >= NB_LOG_FACILITIES) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8621 | Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8622 | exit(1); |
| 8623 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8624 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8625 | level = 7; /* max syslog level = debug */ |
| 8626 | if (*(args[3])) { |
| 8627 | while (level >= 0 && strcmp(log_levels[level], args[3])) |
| 8628 | level--; |
| 8629 | if (level < 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8630 | Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8631 | exit(1); |
| 8632 | } |
| 8633 | } |
| 8634 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8635 | sa = str2sa(args[1]); |
| 8636 | if (!sa->sin_port) |
| 8637 | sa->sin_port = htons(SYSLOG_PORT); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8638 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8639 | if (curproxy->logfac1 == -1) { |
| 8640 | curproxy->logsrv1 = *sa; |
| 8641 | curproxy->logfac1 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8642 | curproxy->loglev1 = level; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8643 | } |
| 8644 | else if (curproxy->logfac2 == -1) { |
| 8645 | curproxy->logsrv2 = *sa; |
| 8646 | curproxy->logfac2 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8647 | curproxy->loglev2 = level; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8648 | } |
| 8649 | else { |
| 8650 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8651 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8652 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8653 | } |
| 8654 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8655 | Alert("parsing [%s:%d] : 'log' expects either <address[:port]> and <facility> or 'global' as arguments.\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8656 | file, linenum); |
| 8657 | return -1; |
| 8658 | } |
| 8659 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 8660 | else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8661 | if (!*args[1]) { |
| 8662 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8663 | file, linenum, "source"); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 8664 | return -1; |
| 8665 | } |
| 8666 | |
| 8667 | curproxy->source_addr = *str2sa(args[1]); |
| 8668 | curproxy->options |= PR_O_BIND_SRC; |
| 8669 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8670 | else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */ |
| 8671 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8672 | if (curproxy == &defproxy) { |
| 8673 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8674 | return -1; |
| 8675 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8676 | |
| 8677 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8678 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 8679 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8680 | return -1; |
| 8681 | } |
| 8682 | |
| 8683 | preg = calloc(1, sizeof(regex_t)); |
| 8684 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8685 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8686 | return -1; |
| 8687 | } |
| 8688 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 8689 | err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 8690 | if (err) { |
| 8691 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 8692 | file, linenum, *err); |
| 8693 | return -1; |
| 8694 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8695 | } |
| 8696 | else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */ |
| 8697 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8698 | if (curproxy == &defproxy) { |
| 8699 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8700 | return -1; |
| 8701 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8702 | |
| 8703 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8704 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8705 | return -1; |
| 8706 | } |
| 8707 | |
| 8708 | preg = calloc(1, sizeof(regex_t)); |
| 8709 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8710 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8711 | return -1; |
| 8712 | } |
| 8713 | |
| 8714 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 8715 | } |
| 8716 | else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */ |
| 8717 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8718 | if (curproxy == &defproxy) { |
| 8719 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8720 | return -1; |
| 8721 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8722 | |
| 8723 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8724 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8725 | return -1; |
| 8726 | } |
| 8727 | |
| 8728 | preg = calloc(1, sizeof(regex_t)); |
| 8729 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8730 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8731 | return -1; |
| 8732 | } |
| 8733 | |
| 8734 | chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL); |
| 8735 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8736 | else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */ |
| 8737 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8738 | if (curproxy == &defproxy) { |
| 8739 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8740 | return -1; |
| 8741 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8742 | |
| 8743 | if (*(args[1]) == 0) { |
| 8744 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
| 8745 | return -1; |
| 8746 | } |
| 8747 | |
| 8748 | preg = calloc(1, sizeof(regex_t)); |
| 8749 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 8750 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 8751 | return -1; |
| 8752 | } |
| 8753 | |
| 8754 | chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL); |
| 8755 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8756 | else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */ |
| 8757 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8758 | if (curproxy == &defproxy) { |
| 8759 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8760 | return -1; |
| 8761 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8762 | |
| 8763 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8764 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8765 | return -1; |
| 8766 | } |
| 8767 | |
| 8768 | preg = calloc(1, sizeof(regex_t)); |
| 8769 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8770 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8771 | return -1; |
| 8772 | } |
| 8773 | |
| 8774 | chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL); |
| 8775 | } |
| 8776 | else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */ |
| 8777 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8778 | if (curproxy == &defproxy) { |
| 8779 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8780 | return -1; |
| 8781 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8782 | |
| 8783 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8784 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 8785 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8786 | return -1; |
| 8787 | } |
| 8788 | |
| 8789 | preg = calloc(1, sizeof(regex_t)); |
| 8790 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8791 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8792 | return -1; |
| 8793 | } |
| 8794 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 8795 | err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 8796 | if (err) { |
| 8797 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 8798 | file, linenum, *err); |
| 8799 | return -1; |
| 8800 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8801 | } |
| 8802 | else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */ |
| 8803 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8804 | if (curproxy == &defproxy) { |
| 8805 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8806 | return -1; |
| 8807 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8808 | |
| 8809 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8810 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8811 | return -1; |
| 8812 | } |
| 8813 | |
| 8814 | preg = calloc(1, sizeof(regex_t)); |
| 8815 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8816 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8817 | return -1; |
| 8818 | } |
| 8819 | |
| 8820 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 8821 | } |
| 8822 | else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */ |
| 8823 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8824 | if (curproxy == &defproxy) { |
| 8825 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8826 | return -1; |
| 8827 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8828 | |
| 8829 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8830 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8831 | return -1; |
| 8832 | } |
| 8833 | |
| 8834 | preg = calloc(1, sizeof(regex_t)); |
| 8835 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8836 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8837 | return -1; |
| 8838 | } |
| 8839 | |
| 8840 | chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL); |
| 8841 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8842 | else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */ |
| 8843 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8844 | if (curproxy == &defproxy) { |
| 8845 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8846 | return -1; |
| 8847 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8848 | |
| 8849 | if (*(args[1]) == 0) { |
| 8850 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
| 8851 | return -1; |
| 8852 | } |
| 8853 | |
| 8854 | preg = calloc(1, sizeof(regex_t)); |
| 8855 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 8856 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 8857 | return -1; |
| 8858 | } |
| 8859 | |
| 8860 | chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL); |
| 8861 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8862 | else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */ |
| 8863 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8864 | if (curproxy == &defproxy) { |
| 8865 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8866 | return -1; |
| 8867 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8868 | |
| 8869 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8870 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8871 | return -1; |
| 8872 | } |
| 8873 | |
| 8874 | preg = calloc(1, sizeof(regex_t)); |
| 8875 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8876 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8877 | return -1; |
| 8878 | } |
| 8879 | |
| 8880 | chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL); |
| 8881 | } |
| 8882 | else if (!strcmp(args[0], "reqadd")) { /* add request header */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8883 | if (curproxy == &defproxy) { |
| 8884 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8885 | return -1; |
| 8886 | } |
| 8887 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8888 | if (curproxy->nb_reqadd >= MAX_NEWHDR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8889 | Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8890 | return 0; |
| 8891 | } |
| 8892 | |
| 8893 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8894 | Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8895 | return -1; |
| 8896 | } |
| 8897 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 8898 | curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]); |
| 8899 | } |
| 8900 | else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */ |
| 8901 | regex_t *preg; |
| 8902 | |
| 8903 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 8904 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 8905 | file, linenum, args[0]); |
| 8906 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8907 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 8908 | |
| 8909 | preg = calloc(1, sizeof(regex_t)); |
| 8910 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 8911 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 8912 | return -1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8913 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 8914 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 8915 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 8916 | if (err) { |
| 8917 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 8918 | file, linenum, *err); |
| 8919 | return -1; |
| 8920 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 8921 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8922 | else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */ |
| 8923 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8924 | if (curproxy == &defproxy) { |
| 8925 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8926 | return -1; |
| 8927 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8928 | |
| 8929 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8930 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8931 | return -1; |
| 8932 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 8933 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8934 | preg = calloc(1, sizeof(regex_t)); |
| 8935 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8936 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8937 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8938 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8939 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 8940 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 8941 | if (err) { |
| 8942 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 8943 | file, linenum, *err); |
| 8944 | return -1; |
| 8945 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8946 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 8947 | else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */ |
| 8948 | regex_t *preg; |
| 8949 | if (curproxy == &defproxy) { |
| 8950 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8951 | return -1; |
| 8952 | } |
| 8953 | |
| 8954 | if (*(args[1]) == 0) { |
| 8955 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
| 8956 | return -1; |
| 8957 | } |
| 8958 | |
| 8959 | preg = calloc(1, sizeof(regex_t)); |
| 8960 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 8961 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 8962 | return -1; |
| 8963 | } |
| 8964 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 8965 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2])); |
| 8966 | if (err) { |
| 8967 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 8968 | file, linenum, *err); |
| 8969 | return -1; |
| 8970 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 8971 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8972 | else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8973 | regex_t *preg; |
| 8974 | if (curproxy == &defproxy) { |
| 8975 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 8976 | return -1; |
| 8977 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 8978 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8979 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 8980 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 8981 | file, linenum, args[0]); |
| 8982 | return -1; |
| 8983 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 8984 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8985 | preg = calloc(1, sizeof(regex_t)); |
| 8986 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 8987 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 8988 | return -1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8989 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8990 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 8991 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 8992 | if (err) { |
| 8993 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 8994 | file, linenum, *err); |
| 8995 | return -1; |
| 8996 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8997 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8998 | else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */ |
| 8999 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9000 | if (curproxy == &defproxy) { |
| 9001 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 9002 | return -1; |
| 9003 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9004 | |
| 9005 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 9006 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9007 | return -1; |
| 9008 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9009 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9010 | preg = calloc(1, sizeof(regex_t)); |
| 9011 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 9012 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9013 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 9014 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9015 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 9016 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 9017 | if (err) { |
| 9018 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 9019 | file, linenum, *err); |
| 9020 | return -1; |
| 9021 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9022 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 9023 | else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */ |
| 9024 | regex_t *preg; |
| 9025 | if (curproxy == &defproxy) { |
| 9026 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 9027 | return -1; |
| 9028 | } |
| 9029 | |
| 9030 | if (*(args[1]) == 0) { |
| 9031 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
| 9032 | return -1; |
| 9033 | } |
| 9034 | |
| 9035 | preg = calloc(1, sizeof(regex_t)); |
| 9036 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 9037 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 9038 | return -1; |
| 9039 | } |
| 9040 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 9041 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2])); |
| 9042 | if (err) { |
| 9043 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 9044 | file, linenum, *err); |
| 9045 | return -1; |
| 9046 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 9047 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9048 | else if (!strcmp(args[0], "rspadd")) { /* add response header */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9049 | if (curproxy == &defproxy) { |
| 9050 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 9051 | return -1; |
| 9052 | } |
| 9053 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9054 | if (curproxy->nb_rspadd >= MAX_NEWHDR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 9055 | Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9056 | return 0; |
| 9057 | } |
| 9058 | |
| 9059 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 9060 | Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9061 | return -1; |
| 9062 | } |
| 9063 | |
| 9064 | curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]); |
| 9065 | } |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 9066 | else if (!strcmp(args[0], "errorloc") || |
| 9067 | !strcmp(args[0], "errorloc302") || |
| 9068 | !strcmp(args[0], "errorloc303")) { /* error location */ |
| 9069 | int errnum, errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9070 | char *err; |
| 9071 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 9072 | // if (curproxy == &defproxy) { |
| 9073 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 9074 | // return -1; |
| 9075 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9076 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9077 | if (*(args[2]) == 0) { |
| 9078 | Alert("parsing [%s:%d] : <errorloc> expects <error> and <url> as arguments.\n", file, linenum); |
| 9079 | return -1; |
| 9080 | } |
| 9081 | |
| 9082 | errnum = atol(args[1]); |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 9083 | if (!strcmp(args[0], "errorloc303")) { |
| 9084 | err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5); |
| 9085 | errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]); |
| 9086 | } else { |
| 9087 | err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5); |
| 9088 | errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]); |
| 9089 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9090 | |
| 9091 | if (errnum == 400) { |
| 9092 | if (curproxy->errmsg.msg400) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 9093 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9094 | free(curproxy->errmsg.msg400); |
| 9095 | } |
| 9096 | curproxy->errmsg.msg400 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 9097 | curproxy->errmsg.len400 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9098 | } |
| 9099 | else if (errnum == 403) { |
| 9100 | if (curproxy->errmsg.msg403) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 9101 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9102 | free(curproxy->errmsg.msg403); |
| 9103 | } |
| 9104 | curproxy->errmsg.msg403 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 9105 | curproxy->errmsg.len403 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9106 | } |
| 9107 | else if (errnum == 408) { |
| 9108 | if (curproxy->errmsg.msg408) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 9109 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9110 | free(curproxy->errmsg.msg408); |
| 9111 | } |
| 9112 | curproxy->errmsg.msg408 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 9113 | curproxy->errmsg.len408 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9114 | } |
| 9115 | else if (errnum == 500) { |
| 9116 | if (curproxy->errmsg.msg500) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 9117 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9118 | free(curproxy->errmsg.msg500); |
| 9119 | } |
| 9120 | curproxy->errmsg.msg500 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 9121 | curproxy->errmsg.len500 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9122 | } |
| 9123 | else if (errnum == 502) { |
| 9124 | if (curproxy->errmsg.msg502) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 9125 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9126 | free(curproxy->errmsg.msg502); |
| 9127 | } |
| 9128 | curproxy->errmsg.msg502 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 9129 | curproxy->errmsg.len502 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9130 | } |
| 9131 | else if (errnum == 503) { |
| 9132 | if (curproxy->errmsg.msg503) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 9133 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9134 | free(curproxy->errmsg.msg503); |
| 9135 | } |
| 9136 | curproxy->errmsg.msg503 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 9137 | curproxy->errmsg.len503 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9138 | } |
| 9139 | else if (errnum == 504) { |
| 9140 | if (curproxy->errmsg.msg504) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 9141 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9142 | free(curproxy->errmsg.msg504); |
| 9143 | } |
| 9144 | curproxy->errmsg.msg504 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 9145 | curproxy->errmsg.len504 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9146 | } |
| 9147 | else { |
| 9148 | Warning("parsing [%s:%d] : error %d relocation will be ignored.\n", file, linenum, errnum); |
| 9149 | free(err); |
| 9150 | } |
| 9151 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9152 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 9153 | Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen"); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9154 | return -1; |
| 9155 | } |
| 9156 | return 0; |
| 9157 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 9158 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9159 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9160 | /* |
| 9161 | * This function reads and parses the configuration file given in the argument. |
| 9162 | * returns 0 if OK, -1 if error. |
| 9163 | */ |
| 9164 | int readcfgfile(char *file) { |
| 9165 | char thisline[256]; |
| 9166 | char *line; |
| 9167 | FILE *f; |
| 9168 | int linenum = 0; |
| 9169 | char *end; |
| 9170 | char *args[MAX_LINE_ARGS]; |
| 9171 | int arg; |
| 9172 | int cfgerr = 0; |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 9173 | int nbchk, mininter; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9174 | int confsect = CFG_NONE; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 9175 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9176 | struct proxy *curproxy = NULL; |
| 9177 | struct server *newsrv = NULL; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 9178 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9179 | if ((f=fopen(file,"r")) == NULL) |
| 9180 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 9181 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 9182 | init_default_instance(); |
| 9183 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9184 | while (fgets(line = thisline, sizeof(thisline), f) != NULL) { |
| 9185 | linenum++; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9186 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9187 | end = line + strlen(line); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9188 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9189 | /* skip leading spaces */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 9190 | while (isspace((int)*line)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9191 | line++; |
| 9192 | |
| 9193 | arg = 0; |
| 9194 | args[arg] = line; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9195 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9196 | while (*line && arg < MAX_LINE_ARGS) { |
| 9197 | /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their |
| 9198 | * C equivalent value. Other combinations left unchanged (eg: \1). |
| 9199 | */ |
| 9200 | if (*line == '\\') { |
| 9201 | int skip = 0; |
| 9202 | if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') { |
| 9203 | *line = line[1]; |
| 9204 | skip = 1; |
| 9205 | } |
| 9206 | else if (line[1] == 'r') { |
| 9207 | *line = '\r'; |
| 9208 | skip = 1; |
| 9209 | } |
| 9210 | else if (line[1] == 'n') { |
| 9211 | *line = '\n'; |
| 9212 | skip = 1; |
| 9213 | } |
| 9214 | else if (line[1] == 't') { |
| 9215 | *line = '\t'; |
| 9216 | skip = 1; |
| 9217 | } |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 9218 | else if (line[1] == 'x') { |
| 9219 | if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) { |
| 9220 | unsigned char hex1, hex2; |
| 9221 | hex1 = toupper(line[2]) - '0'; |
| 9222 | hex2 = toupper(line[3]) - '0'; |
| 9223 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 9224 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 9225 | *line = (hex1<<4) + hex2; |
| 9226 | skip = 3; |
| 9227 | } |
| 9228 | else { |
| 9229 | Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]); |
| 9230 | return -1; |
| 9231 | } |
| 9232 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9233 | if (skip) { |
| 9234 | memmove(line + 1, line + 1 + skip, end - (line + skip + 1)); |
| 9235 | end -= skip; |
| 9236 | } |
| 9237 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9238 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 9239 | else if (*line == '#' || *line == '\n' || *line == '\r') { |
| 9240 | /* end of string, end of loop */ |
| 9241 | *line = 0; |
| 9242 | break; |
| 9243 | } |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 9244 | else if (isspace((int)*line)) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9245 | /* a non-escaped space is an argument separator */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 9246 | *line++ = 0; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 9247 | while (isspace((int)*line)) |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 9248 | line++; |
| 9249 | args[++arg] = line; |
| 9250 | } |
| 9251 | else { |
| 9252 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9253 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9254 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9255 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9256 | /* empty line */ |
| 9257 | if (!**args) |
| 9258 | continue; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 9259 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9260 | /* zero out remaining args */ |
| 9261 | while (++arg < MAX_LINE_ARGS) { |
| 9262 | args[arg] = line; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9263 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9264 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9265 | if (!strcmp(args[0], "listen") || !strcmp(args[0], "defaults")) /* new proxy */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9266 | confsect = CFG_LISTEN; |
| 9267 | else if (!strcmp(args[0], "global")) /* global config */ |
| 9268 | confsect = CFG_GLOBAL; |
| 9269 | /* else it's a section keyword */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9270 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9271 | switch (confsect) { |
| 9272 | case CFG_LISTEN: |
| 9273 | if (cfg_parse_listen(file, linenum, args) < 0) |
| 9274 | return -1; |
| 9275 | break; |
| 9276 | case CFG_GLOBAL: |
| 9277 | if (cfg_parse_global(file, linenum, args) < 0) |
| 9278 | return -1; |
| 9279 | break; |
| 9280 | default: |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 9281 | Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9282 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9283 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9284 | |
| 9285 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9286 | } |
| 9287 | fclose(f); |
| 9288 | |
| 9289 | /* |
| 9290 | * Now, check for the integrity of all that we have collected. |
| 9291 | */ |
| 9292 | |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 9293 | /* will be needed further to delay some tasks */ |
| 9294 | tv_now(&now); |
| 9295 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9296 | if ((curproxy = proxy) == NULL) { |
| 9297 | Alert("parsing %s : no <listen> line. Nothing to do !\n", |
| 9298 | file); |
| 9299 | return -1; |
| 9300 | } |
| 9301 | |
| 9302 | while (curproxy != NULL) { |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 9303 | if (curproxy->state == PR_STSTOPPED) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 9304 | curproxy = curproxy->next; |
| 9305 | continue; |
| 9306 | } |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 9307 | |
| 9308 | if (curproxy->listen == NULL) { |
| 9309 | Alert("parsing %s : listener %s has no listen address. Please either specify a valid address on the <listen> line, or use the <bind> keyword.\n", file, curproxy->id); |
| 9310 | cfgerr++; |
| 9311 | } |
| 9312 | else if ((curproxy->mode != PR_MODE_HEALTH) && |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9313 | !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) && |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 9314 | (*(int *)&curproxy->dispatch_addr.sin_addr == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9315 | Alert("parsing %s : listener %s has no dispatch address and is not in transparent or balance mode.\n", |
| 9316 | file, curproxy->id); |
| 9317 | cfgerr++; |
| 9318 | } |
| 9319 | else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) { |
| 9320 | if (curproxy->options & PR_O_TRANSP) { |
| 9321 | Alert("parsing %s : listener %s cannot use both transparent and balance mode.\n", |
| 9322 | file, curproxy->id); |
| 9323 | cfgerr++; |
| 9324 | } |
willy tarreau | 38d7906 | 2006-05-21 14:47:13 +0200 | [diff] [blame] | 9325 | #ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9326 | else if (curproxy->srv == NULL) { |
| 9327 | Alert("parsing %s : listener %s needs at least 1 server in balance mode.\n", |
| 9328 | file, curproxy->id); |
| 9329 | cfgerr++; |
| 9330 | } |
willy tarreau | 38d7906 | 2006-05-21 14:47:13 +0200 | [diff] [blame] | 9331 | #endif |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 9332 | else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9333 | Warning("parsing %s : dispatch address of listener %s will be ignored in balance mode.\n", |
| 9334 | file, curproxy->id); |
| 9335 | } |
| 9336 | } |
| 9337 | else if (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HEALTH) { /* TCP PROXY or HEALTH CHECK */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9338 | if (curproxy->cookie_name != NULL) { |
| 9339 | Warning("parsing %s : cookie will be ignored for listener %s.\n", |
| 9340 | file, curproxy->id); |
| 9341 | } |
| 9342 | if ((newsrv = curproxy->srv) != NULL) { |
| 9343 | Warning("parsing %s : servers will be ignored for listener %s.\n", |
| 9344 | file, curproxy->id); |
| 9345 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 9346 | if (curproxy->rsp_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9347 | Warning("parsing %s : server regular expressions will be ignored for listener %s.\n", |
| 9348 | file, curproxy->id); |
| 9349 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 9350 | if (curproxy->req_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9351 | Warning("parsing %s : client regular expressions will be ignored for listener %s.\n", |
| 9352 | file, curproxy->id); |
| 9353 | } |
| 9354 | } |
| 9355 | else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */ |
| 9356 | if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) { |
| 9357 | Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n", |
| 9358 | file, curproxy->id); |
| 9359 | cfgerr++; |
| 9360 | } |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 9361 | } |
willy tarreau | e3f023f | 2006-04-08 21:52:24 +0200 | [diff] [blame] | 9362 | |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 9363 | /* first, we will invert the servers list order */ |
| 9364 | newsrv = NULL; |
| 9365 | while (curproxy->srv) { |
| 9366 | struct server *next; |
| 9367 | |
| 9368 | next = curproxy->srv->next; |
| 9369 | curproxy->srv->next = newsrv; |
| 9370 | newsrv = curproxy->srv; |
| 9371 | if (!next) |
| 9372 | break; |
| 9373 | curproxy->srv = next; |
| 9374 | } |
| 9375 | |
| 9376 | /* now, newsrv == curproxy->srv */ |
| 9377 | if (newsrv) { |
| 9378 | struct server *srv; |
| 9379 | int pgcd; |
| 9380 | int act, bck; |
willy tarreau | e3f023f | 2006-04-08 21:52:24 +0200 | [diff] [blame] | 9381 | |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 9382 | /* We will factor the weights to reduce the table, |
| 9383 | * using Euclide's largest common divisor algorithm |
| 9384 | */ |
| 9385 | pgcd = newsrv->uweight + 1; |
| 9386 | for (srv = newsrv->next; srv && pgcd > 1; srv = srv->next) { |
| 9387 | int t, w; |
| 9388 | |
| 9389 | w = srv->uweight + 1; |
| 9390 | while (w) { |
| 9391 | t = pgcd % w; |
| 9392 | pgcd = w; |
| 9393 | w = t; |
willy tarreau | e3f023f | 2006-04-08 21:52:24 +0200 | [diff] [blame] | 9394 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9395 | } |
willy tarreau | cc1e2bd | 2006-04-10 20:32:43 +0200 | [diff] [blame] | 9396 | |
| 9397 | act = bck = 0; |
| 9398 | for (srv = newsrv; srv; srv = srv->next) { |
| 9399 | srv->eweight = ((srv->uweight + 1) / pgcd) - 1; |
| 9400 | if (srv->state & SRV_BACKUP) |
| 9401 | bck += srv->eweight + 1; |
| 9402 | else |
| 9403 | act += srv->eweight + 1; |
| 9404 | } |
| 9405 | |
| 9406 | /* this is the largest map we will ever need for this servers list */ |
| 9407 | if (act < bck) |
| 9408 | act = bck; |
| 9409 | |
| 9410 | curproxy->srv_map = (struct server **)calloc(act, sizeof(struct server *)); |
| 9411 | /* recounts servers and their weights */ |
| 9412 | recount_servers(curproxy); |
| 9413 | recalc_server_map(curproxy); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9414 | } |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 9415 | |
| 9416 | if (curproxy->options & PR_O_LOGASAP) |
| 9417 | curproxy->to_log &= ~LW_BYTES; |
| 9418 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 9419 | if (curproxy->errmsg.msg400 == NULL) { |
| 9420 | curproxy->errmsg.msg400 = (char *)HTTP_400; |
| 9421 | curproxy->errmsg.len400 = strlen(HTTP_400); |
| 9422 | } |
| 9423 | if (curproxy->errmsg.msg403 == NULL) { |
| 9424 | curproxy->errmsg.msg403 = (char *)HTTP_403; |
| 9425 | curproxy->errmsg.len403 = strlen(HTTP_403); |
| 9426 | } |
| 9427 | if (curproxy->errmsg.msg408 == NULL) { |
| 9428 | curproxy->errmsg.msg408 = (char *)HTTP_408; |
| 9429 | curproxy->errmsg.len408 = strlen(HTTP_408); |
| 9430 | } |
| 9431 | if (curproxy->errmsg.msg500 == NULL) { |
| 9432 | curproxy->errmsg.msg500 = (char *)HTTP_500; |
| 9433 | curproxy->errmsg.len500 = strlen(HTTP_500); |
| 9434 | } |
| 9435 | if (curproxy->errmsg.msg502 == NULL) { |
| 9436 | curproxy->errmsg.msg502 = (char *)HTTP_502; |
| 9437 | curproxy->errmsg.len502 = strlen(HTTP_502); |
| 9438 | } |
| 9439 | if (curproxy->errmsg.msg503 == NULL) { |
| 9440 | curproxy->errmsg.msg503 = (char *)HTTP_503; |
| 9441 | curproxy->errmsg.len503 = strlen(HTTP_503); |
| 9442 | } |
| 9443 | if (curproxy->errmsg.msg504 == NULL) { |
| 9444 | curproxy->errmsg.msg504 = (char *)HTTP_504; |
| 9445 | curproxy->errmsg.len504 = strlen(HTTP_504); |
| 9446 | } |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 9447 | |
willy tarreau | 59a6cc2 | 2006-05-12 01:29:08 +0200 | [diff] [blame] | 9448 | /* |
| 9449 | * If this server supports a maxconn parameter, it needs a dedicated |
| 9450 | * tasks to fill the emptied slots when a connection leaves. |
| 9451 | */ |
| 9452 | newsrv = curproxy->srv; |
| 9453 | while (newsrv != NULL) { |
| 9454 | if (newsrv->maxconn > 0) { |
| 9455 | struct task *t; |
| 9456 | |
| 9457 | if ((t = pool_alloc(task)) == NULL) { |
| 9458 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 9459 | return -1; |
| 9460 | } |
| 9461 | |
| 9462 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 9463 | t->wq = LIST_HEAD(wait_queue[1]); /* already assigned to the eternity queue */ |
| 9464 | t->state = TASK_IDLE; |
| 9465 | t->process = process_srv_queue; |
| 9466 | t->context = newsrv; |
| 9467 | newsrv->queue_mgt = t; |
| 9468 | |
| 9469 | /* never run it unless specifically woken up */ |
| 9470 | tv_eternity(&t->expire); |
| 9471 | task_queue(t); |
| 9472 | } |
| 9473 | newsrv = newsrv->next; |
| 9474 | } |
| 9475 | |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 9476 | /* now we'll start this proxy's health checks if any */ |
| 9477 | /* 1- count the checkers to run simultaneously */ |
| 9478 | nbchk = 0; |
| 9479 | mininter = 0; |
| 9480 | newsrv = curproxy->srv; |
| 9481 | while (newsrv != NULL) { |
| 9482 | if (newsrv->state & SRV_CHECKED) { |
| 9483 | if (!mininter || mininter > newsrv->inter) |
| 9484 | mininter = newsrv->inter; |
| 9485 | nbchk++; |
| 9486 | } |
| 9487 | newsrv = newsrv->next; |
| 9488 | } |
| 9489 | |
| 9490 | /* 2- start them as far as possible from each others while respecting |
| 9491 | * their own intervals. For this, we will start them after their own |
| 9492 | * interval added to the min interval divided by the number of servers, |
| 9493 | * weighted by the server's position in the list. |
| 9494 | */ |
| 9495 | if (nbchk > 0) { |
| 9496 | struct task *t; |
| 9497 | int srvpos; |
| 9498 | |
| 9499 | newsrv = curproxy->srv; |
| 9500 | srvpos = 0; |
| 9501 | while (newsrv != NULL) { |
| 9502 | /* should this server be checked ? */ |
| 9503 | if (newsrv->state & SRV_CHECKED) { |
| 9504 | if ((t = pool_alloc(task)) == NULL) { |
| 9505 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 9506 | return -1; |
| 9507 | } |
| 9508 | |
| 9509 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
willy tarreau | 5e698ef | 2006-05-02 14:51:00 +0200 | [diff] [blame] | 9510 | t->wq = LIST_HEAD(wait_queue[0]); /* but already has a wait queue assigned */ |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 9511 | t->state = TASK_IDLE; |
| 9512 | t->process = process_chk; |
| 9513 | t->context = newsrv; |
| 9514 | |
| 9515 | /* check this every ms */ |
| 9516 | tv_delayfrom(&t->expire, &now, |
| 9517 | newsrv->inter + mininter * srvpos / nbchk); |
| 9518 | task_queue(t); |
| 9519 | //task_wakeup(&rq, t); |
| 9520 | srvpos++; |
| 9521 | } |
| 9522 | newsrv = newsrv->next; |
| 9523 | } |
| 9524 | } |
| 9525 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9526 | curproxy = curproxy->next; |
| 9527 | } |
| 9528 | if (cfgerr > 0) { |
| 9529 | Alert("Errors found in configuration file, aborting.\n"); |
| 9530 | return -1; |
| 9531 | } |
| 9532 | else |
| 9533 | return 0; |
| 9534 | } |
| 9535 | |
| 9536 | |
| 9537 | /* |
| 9538 | * This function initializes all the necessary variables. It only returns |
| 9539 | * if everything is OK. If something fails, it exits. |
| 9540 | */ |
| 9541 | void init(int argc, char **argv) { |
| 9542 | int i; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9543 | int arg_mode = 0; /* MODE_DEBUG, ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9544 | char *old_argv = *argv; |
| 9545 | char *tmp; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 9546 | char *cfg_pidfile = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9547 | |
| 9548 | if (1<<INTBITS != sizeof(int)*8) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 9549 | fprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9550 | "Error: wrong architecture. Recompile so that sizeof(int)=%d\n", |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 9551 | (int)(sizeof(int)*8)); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9552 | exit(1); |
| 9553 | } |
| 9554 | |
willy tarreau | 746e26b | 2006-03-25 11:14:35 +0100 | [diff] [blame] | 9555 | #ifdef HAPROXY_MEMMAX |
| 9556 | global.rlimit_memmax = HAPROXY_MEMMAX; |
| 9557 | #endif |
| 9558 | |
Willy TARREAU | a9e75f6 | 2006-03-01 22:27:48 +0100 | [diff] [blame] | 9559 | /* initialize the libc's localtime structures once for all so that we |
| 9560 | * won't be missing memory if we want to send alerts under OOM conditions. |
| 9561 | */ |
| 9562 | tv_now(&now); |
| 9563 | localtime(&now.tv_sec); |
willy tarreau | e033126 | 2006-05-15 03:02:46 +0200 | [diff] [blame] | 9564 | start_date = now; |
Willy TARREAU | a9e75f6 | 2006-03-01 22:27:48 +0100 | [diff] [blame] | 9565 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 9566 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 9567 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 9568 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 9569 | * printable characters above. |
| 9570 | */ |
| 9571 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 9572 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 9573 | for (i = 0; i < 32; i++) { |
| 9574 | FD_SET(i, hdr_encode_map); |
| 9575 | FD_SET(i, url_encode_map); |
| 9576 | } |
| 9577 | for (i = 127; i < 256; i++) { |
| 9578 | FD_SET(i, hdr_encode_map); |
| 9579 | FD_SET(i, url_encode_map); |
| 9580 | } |
| 9581 | |
| 9582 | tmp = "\"#{|}"; |
| 9583 | while (*tmp) { |
| 9584 | FD_SET(*tmp, hdr_encode_map); |
| 9585 | tmp++; |
| 9586 | } |
| 9587 | |
| 9588 | tmp = "\"#"; |
| 9589 | while (*tmp) { |
| 9590 | FD_SET(*tmp, url_encode_map); |
| 9591 | tmp++; |
| 9592 | } |
| 9593 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 9594 | cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */ |
| 9595 | #if defined(ENABLE_POLL) |
| 9596 | cfg_polling_mechanism |= POLL_USE_POLL; |
| 9597 | #endif |
| 9598 | #if defined(ENABLE_EPOLL) |
| 9599 | cfg_polling_mechanism |= POLL_USE_EPOLL; |
| 9600 | #endif |
| 9601 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9602 | pid = getpid(); |
| 9603 | progname = *argv; |
| 9604 | while ((tmp = strchr(progname, '/')) != NULL) |
| 9605 | progname = tmp + 1; |
| 9606 | |
| 9607 | argc--; argv++; |
| 9608 | while (argc > 0) { |
| 9609 | char *flag; |
| 9610 | |
| 9611 | if (**argv == '-') { |
| 9612 | flag = *argv+1; |
| 9613 | |
| 9614 | /* 1 arg */ |
| 9615 | if (*flag == 'v') { |
| 9616 | display_version(); |
| 9617 | exit(0); |
| 9618 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 9619 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 9620 | else if (*flag == 'd' && flag[1] == 'e') |
| 9621 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 9622 | #endif |
| 9623 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 9624 | else if (*flag == 'd' && flag[1] == 'p') |
| 9625 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 9626 | #endif |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 9627 | else if (*flag == 'V') |
| 9628 | arg_mode |= MODE_VERBOSE; |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 9629 | else if (*flag == 'd' && flag[1] == 'b') |
| 9630 | arg_mode |= MODE_FOREGROUND; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9631 | else if (*flag == 'd') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9632 | arg_mode |= MODE_DEBUG; |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 9633 | else if (*flag == 'c') |
| 9634 | arg_mode |= MODE_CHECK; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9635 | else if (*flag == 'D') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9636 | arg_mode |= MODE_DAEMON | MODE_QUIET; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9637 | else if (*flag == 'q') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9638 | arg_mode |= MODE_QUIET; |
willy tarreau | 53e9970 | 2006-03-25 18:53:50 +0100 | [diff] [blame] | 9639 | else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) { |
| 9640 | /* list of pids to finish ('f') or terminate ('t') */ |
| 9641 | |
| 9642 | if (flag[1] == 'f') |
| 9643 | oldpids_sig = SIGUSR1; /* finish then exit */ |
| 9644 | else |
| 9645 | oldpids_sig = SIGTERM; /* terminate immediately */ |
| 9646 | argv++; argc--; |
| 9647 | |
| 9648 | if (argc > 0) { |
| 9649 | oldpids = calloc(argc, sizeof(int)); |
| 9650 | while (argc > 0) { |
| 9651 | oldpids[nb_oldpids] = atol(*argv); |
| 9652 | if (oldpids[nb_oldpids] <= 0) |
| 9653 | usage(old_argv); |
| 9654 | argc--; argv++; |
| 9655 | nb_oldpids++; |
| 9656 | } |
| 9657 | } |
| 9658 | } |
willy tarreau | 2c51373 | 2006-04-15 19:25:16 +0200 | [diff] [blame] | 9659 | #if STATTIME > 0 |
| 9660 | else if (*flag == 's') |
| 9661 | arg_mode |= MODE_STATS; |
| 9662 | else if (*flag == 'l') |
| 9663 | arg_mode |= MODE_LOG; |
| 9664 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9665 | else { /* >=2 args */ |
| 9666 | argv++; argc--; |
| 9667 | if (argc == 0) |
| 9668 | usage(old_argv); |
| 9669 | |
| 9670 | switch (*flag) { |
| 9671 | case 'n' : cfg_maxconn = atol(*argv); break; |
willy tarreau | 746e26b | 2006-03-25 11:14:35 +0100 | [diff] [blame] | 9672 | case 'm' : global.rlimit_memmax = atol(*argv); break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9673 | case 'N' : cfg_maxpconn = atol(*argv); break; |
| 9674 | case 'f' : cfg_cfgfile = *argv; break; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 9675 | case 'p' : cfg_pidfile = *argv; break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9676 | default: usage(old_argv); |
| 9677 | } |
| 9678 | } |
| 9679 | } |
| 9680 | else |
| 9681 | usage(old_argv); |
willy tarreau | 53e9970 | 2006-03-25 18:53:50 +0100 | [diff] [blame] | 9682 | argv++; argc--; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9683 | } |
| 9684 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 9685 | global.mode = MODE_STARTING | /* during startup, we want most of the alerts */ |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 9686 | (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE |
| 9687 | | MODE_QUIET | MODE_CHECK | MODE_DEBUG)); |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 9688 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9689 | if (!cfg_cfgfile) |
| 9690 | usage(old_argv); |
| 9691 | |
| 9692 | gethostname(hostname, MAX_HOSTNAME_LEN); |
| 9693 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 9694 | have_appsession = 0; |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 9695 | global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9696 | if (readcfgfile(cfg_cfgfile) < 0) { |
| 9697 | Alert("Error reading configuration file : %s\n", cfg_cfgfile); |
| 9698 | exit(1); |
| 9699 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 9700 | if (have_appsession) |
| 9701 | appsession_init(); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9702 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 9703 | if (global.mode & MODE_CHECK) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 9704 | qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile); |
| 9705 | exit(0); |
| 9706 | } |
| 9707 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9708 | if (cfg_maxconn > 0) |
| 9709 | global.maxconn = cfg_maxconn; |
| 9710 | |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 9711 | if (cfg_pidfile) { |
| 9712 | if (global.pidfile) |
| 9713 | free(global.pidfile); |
| 9714 | global.pidfile = strdup(cfg_pidfile); |
| 9715 | } |
| 9716 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9717 | if (global.maxconn == 0) |
| 9718 | global.maxconn = DEFAULT_MAXCONN; |
| 9719 | |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 9720 | global.maxsock += global.maxconn * 2; /* each connection needs two sockets */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9721 | |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 9722 | if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9723 | /* command line debug mode inhibits configuration mode */ |
| 9724 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 9725 | } |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 9726 | global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET | |
| 9727 | MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG)); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9728 | |
| 9729 | if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) { |
| 9730 | Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n"); |
| 9731 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 9732 | } |
| 9733 | |
| 9734 | if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) { |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 9735 | if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG))) |
| 9736 | Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n"); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9737 | global.nbproc = 1; |
| 9738 | } |
| 9739 | |
| 9740 | if (global.nbproc < 1) |
| 9741 | global.nbproc = 1; |
| 9742 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9743 | StaticReadEvent = (fd_set *)calloc(1, |
| 9744 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9745 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9746 | StaticWriteEvent = (fd_set *)calloc(1, |
| 9747 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9748 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9749 | |
| 9750 | fdtab = (struct fdtab *)calloc(1, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 9751 | sizeof(struct fdtab) * (global.maxsock)); |
| 9752 | for (i = 0; i < global.maxsock; i++) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9753 | fdtab[i].state = FD_STCLOSE; |
| 9754 | } |
| 9755 | } |
| 9756 | |
| 9757 | /* |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9758 | * this function starts all the proxies. Its return value is composed from |
| 9759 | * ERR_NONE, ERR_RETRYABLE and ERR_FATAL. Retryable errors will only be printed |
| 9760 | * if <verbose> is not zero. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9761 | */ |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9762 | int start_proxies(int verbose) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9763 | struct proxy *curproxy; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9764 | struct listener *listener; |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9765 | int err = ERR_NONE; |
| 9766 | int fd, pxerr; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9767 | |
| 9768 | for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) { |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9769 | if (curproxy->state != PR_STNEW) |
| 9770 | continue; /* already initialized */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9771 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9772 | pxerr = 0; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9773 | for (listener = curproxy->listen; listener != NULL; listener = listener->next) { |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9774 | if (listener->fd != -1) |
| 9775 | continue; /* already initialized */ |
| 9776 | |
| 9777 | if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 9778 | if (verbose) |
| 9779 | Alert("cannot create listening socket for proxy %s. Aborting.\n", |
| 9780 | curproxy->id); |
| 9781 | err |= ERR_RETRYABLE; |
| 9782 | pxerr |= 1; |
| 9783 | continue; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9784 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9785 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9786 | if (fd >= global.maxsock) { |
| 9787 | Alert("socket(): not enough free sockets for proxy %s. Raise -n argument. Aborting.\n", |
| 9788 | curproxy->id); |
| 9789 | close(fd); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9790 | err |= ERR_FATAL; |
| 9791 | pxerr |= 1; |
| 9792 | break; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9793 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 9794 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9795 | if ((fcntl(fd, F_SETFL, O_NONBLOCK) == -1) || |
| 9796 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 9797 | (char *) &one, sizeof(one)) == -1)) { |
| 9798 | Alert("cannot make socket non-blocking for proxy %s. Aborting.\n", |
| 9799 | curproxy->id); |
| 9800 | close(fd); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9801 | err |= ERR_FATAL; |
| 9802 | pxerr |= 1; |
| 9803 | break; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9804 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9805 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9806 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) { |
| 9807 | Alert("cannot do so_reuseaddr for proxy %s. Continuing.\n", |
| 9808 | curproxy->id); |
| 9809 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9810 | |
willy tarreau | fac1a86 | 2006-05-21 10:20:28 +0200 | [diff] [blame] | 9811 | #ifdef SO_REUSEPORT |
| 9812 | /* OpenBSD supports this. As it's present in old libc versions of Linux, |
| 9813 | * it might return an error that we will silently ignore. |
| 9814 | */ |
| 9815 | setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one)); |
| 9816 | #endif |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9817 | if (bind(fd, |
| 9818 | (struct sockaddr *)&listener->addr, |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 9819 | listener->addr.ss_family == AF_INET6 ? |
| 9820 | sizeof(struct sockaddr_in6) : |
| 9821 | sizeof(struct sockaddr_in)) == -1) { |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9822 | if (verbose) |
| 9823 | Alert("cannot bind socket for proxy %s. Aborting.\n", |
| 9824 | curproxy->id); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9825 | close(fd); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9826 | err |= ERR_RETRYABLE; |
| 9827 | pxerr |= 1; |
| 9828 | continue; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9829 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9830 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9831 | if (listen(fd, curproxy->maxconn) == -1) { |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9832 | if (verbose) |
| 9833 | Alert("cannot listen to socket for proxy %s. Aborting.\n", |
| 9834 | curproxy->id); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9835 | close(fd); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9836 | err |= ERR_RETRYABLE; |
| 9837 | pxerr |= 1; |
| 9838 | continue; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9839 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9840 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9841 | /* the socket is ready */ |
| 9842 | listener->fd = fd; |
| 9843 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9844 | /* the function for the accept() event */ |
| 9845 | fdtab[fd].read = &event_accept; |
| 9846 | fdtab[fd].write = NULL; /* never called */ |
| 9847 | fdtab[fd].owner = (struct task *)curproxy; /* reference the proxy instead of a task */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 9848 | fdtab[fd].state = FD_STLISTEN; |
| 9849 | FD_SET(fd, StaticReadEvent); |
| 9850 | fd_insert(fd); |
| 9851 | listeners++; |
| 9852 | } |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9853 | |
| 9854 | if (!pxerr) { |
| 9855 | curproxy->state = PR_STRUN; |
| 9856 | send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id); |
| 9857 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9858 | } |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 9859 | |
| 9860 | return err; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 9861 | } |
| 9862 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 9863 | int match_str(const void *key1, const void *key2) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 9864 | |
| 9865 | appsess *temp1,*temp2; |
| 9866 | temp1 = (appsess *)key1; |
| 9867 | temp2 = (appsess *)key2; |
| 9868 | |
| 9869 | //fprintf(stdout,">>>>>>>>>>>>>>temp1->sessid :%s:\n",temp1->sessid); |
| 9870 | //fprintf(stdout,">>>>>>>>>>>>>>temp2->sessid :%s:\n",temp2->sessid); |
| 9871 | |
| 9872 | return (strcmp(temp1->sessid,temp2->sessid) == 0); |
| 9873 | }/* end match_str */ |
| 9874 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 9875 | void destroy(void *data) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 9876 | appsess *temp1; |
| 9877 | |
| 9878 | //printf("destroy called\n"); |
| 9879 | temp1 = (appsess *)data; |
| 9880 | |
| 9881 | if (temp1->sessid) |
| 9882 | pool_free_to(apools.sessid, temp1->sessid); |
| 9883 | |
| 9884 | if (temp1->serverid) |
| 9885 | pool_free_to(apools.serverid, temp1->serverid); |
| 9886 | |
| 9887 | pool_free(appsess, temp1); |
| 9888 | } /* end destroy */ |
| 9889 | |
| 9890 | void appsession_cleanup( void ) |
| 9891 | { |
| 9892 | struct proxy *p = proxy; |
| 9893 | |
| 9894 | while(p) { |
| 9895 | chtbl_destroy(&(p->htbl_proxy)); |
| 9896 | p = p->next; |
| 9897 | } |
| 9898 | }/* end appsession_cleanup() */ |
| 9899 | |
| 9900 | void pool_destroy(void **pool) |
| 9901 | { |
| 9902 | void *temp, *next; |
| 9903 | next = pool; |
| 9904 | while (next) { |
| 9905 | temp = next; |
| 9906 | next = *(void **)temp; |
| 9907 | free(temp); |
| 9908 | } |
| 9909 | }/* end pool_destroy() */ |
| 9910 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 9911 | void deinit(void) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 9912 | struct proxy *p = proxy; |
| 9913 | struct cap_hdr *h,*h_next; |
| 9914 | struct server *s,*s_next; |
| 9915 | struct listener *l,*l_next; |
| 9916 | |
| 9917 | while (p) { |
| 9918 | if (p->id) |
| 9919 | free(p->id); |
| 9920 | |
| 9921 | if (p->check_req) |
| 9922 | free(p->check_req); |
| 9923 | |
| 9924 | if (p->cookie_name) |
| 9925 | free(p->cookie_name); |
| 9926 | |
| 9927 | if (p->capture_name) |
| 9928 | free(p->capture_name); |
| 9929 | |
| 9930 | /* only strup if the user have set in config. |
| 9931 | When should we free it?! |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 9932 | if (p->errmsg.msg400) free(p->errmsg.msg400); |
| 9933 | if (p->errmsg.msg403) free(p->errmsg.msg403); |
| 9934 | if (p->errmsg.msg408) free(p->errmsg.msg408); |
| 9935 | if (p->errmsg.msg500) free(p->errmsg.msg500); |
| 9936 | if (p->errmsg.msg502) free(p->errmsg.msg502); |
| 9937 | if (p->errmsg.msg503) free(p->errmsg.msg503); |
| 9938 | if (p->errmsg.msg504) free(p->errmsg.msg504); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 9939 | */ |
| 9940 | if (p->appsession_name) |
| 9941 | free(p->appsession_name); |
| 9942 | |
| 9943 | h = p->req_cap; |
| 9944 | while (h) { |
| 9945 | h_next = h->next; |
| 9946 | if (h->name) |
| 9947 | free(h->name); |
| 9948 | pool_destroy(h->pool); |
| 9949 | free(h); |
| 9950 | h = h_next; |
| 9951 | }/* end while(h) */ |
| 9952 | |
| 9953 | h = p->rsp_cap; |
| 9954 | while (h) { |
| 9955 | h_next = h->next; |
| 9956 | if (h->name) |
| 9957 | free(h->name); |
| 9958 | |
| 9959 | pool_destroy(h->pool); |
| 9960 | free(h); |
| 9961 | h = h_next; |
| 9962 | }/* end while(h) */ |
| 9963 | |
| 9964 | s = p->srv; |
| 9965 | while (s) { |
| 9966 | s_next = s->next; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 9967 | if (s->id) |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 9968 | free(s->id); |
| 9969 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 9970 | if (s->cookie) |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 9971 | free(s->cookie); |
| 9972 | |
| 9973 | free(s); |
| 9974 | s = s_next; |
| 9975 | }/* end while(s) */ |
| 9976 | |
| 9977 | l = p->listen; |
| 9978 | while (l) { |
| 9979 | l_next = l->next; |
| 9980 | free(l); |
| 9981 | l = l_next; |
| 9982 | }/* end while(l) */ |
| 9983 | |
| 9984 | pool_destroy((void **) p->req_cap_pool); |
| 9985 | pool_destroy((void **) p->rsp_cap_pool); |
| 9986 | p = p->next; |
| 9987 | }/* end while(p) */ |
| 9988 | |
| 9989 | if (global.chroot) free(global.chroot); |
| 9990 | if (global.pidfile) free(global.pidfile); |
| 9991 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 9992 | if (StaticReadEvent) free(StaticReadEvent); |
| 9993 | if (StaticWriteEvent) free(StaticWriteEvent); |
| 9994 | if (fdtab) free(fdtab); |
| 9995 | |
| 9996 | pool_destroy(pool_session); |
| 9997 | pool_destroy(pool_buffer); |
| 9998 | pool_destroy(pool_fdtab); |
| 9999 | pool_destroy(pool_requri); |
| 10000 | pool_destroy(pool_task); |
| 10001 | pool_destroy(pool_capture); |
| 10002 | pool_destroy(pool_appsess); |
| 10003 | |
| 10004 | if (have_appsession) { |
| 10005 | pool_destroy(apools.serverid); |
| 10006 | pool_destroy(apools.sessid); |
| 10007 | } |
| 10008 | } /* end deinit() */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 10009 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 10010 | /* sends the signal <sig> to all pids found in <oldpids> */ |
| 10011 | static void tell_old_pids(int sig) { |
| 10012 | int p; |
| 10013 | for (p = 0; p < nb_oldpids; p++) |
| 10014 | kill(oldpids[p], sig); |
| 10015 | } |
| 10016 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 10017 | int main(int argc, char **argv) { |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 10018 | int err, retry; |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 10019 | struct rlimit limit; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 10020 | FILE *pidfile = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 10021 | init(argc, argv); |
| 10022 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 10023 | signal(SIGQUIT, dump); |
| 10024 | signal(SIGUSR1, sig_soft_stop); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 10025 | signal(SIGHUP, sig_dump_state); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 10026 | #ifdef DEBUG_MEMORY |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 10027 | signal(SIGINT, sig_int); |
| 10028 | signal(SIGTERM, sig_term); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 10029 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 10030 | |
| 10031 | /* on very high loads, a sigpipe sometimes happen just between the |
| 10032 | * getsockopt() which tells "it's OK to write", and the following write :-( |
| 10033 | */ |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 10034 | #ifndef MSG_NOSIGNAL |
| 10035 | signal(SIGPIPE, SIG_IGN); |
| 10036 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 10037 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 10038 | /* We will loop at most 100 times with 10 ms delay each time. |
| 10039 | * That's at most 1 second. We only send a signal to old pids |
| 10040 | * if we cannot grab at least one port. |
| 10041 | */ |
| 10042 | retry = MAX_START_RETRIES; |
| 10043 | err = ERR_NONE; |
| 10044 | while (retry >= 0) { |
| 10045 | struct timeval w; |
| 10046 | err = start_proxies(retry == 0 || nb_oldpids == 0); |
| 10047 | if (err != ERR_RETRYABLE) |
| 10048 | break; |
| 10049 | if (nb_oldpids == 0) |
| 10050 | break; |
| 10051 | |
Willy TARREAU | 007aa46 | 2006-05-14 09:55:23 +0200 | [diff] [blame] | 10052 | /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on |
| 10053 | * listening sockets. So on those platforms, it would be wiser to |
| 10054 | * simply send SIGUSR1, which will not be undoable. |
| 10055 | */ |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 10056 | tell_old_pids(SIGTTOU); |
| 10057 | /* give some time to old processes to stop listening */ |
| 10058 | w.tv_sec = 0; |
| 10059 | w.tv_usec = 10*1000; |
| 10060 | select(0, NULL, NULL, NULL, &w); |
| 10061 | retry--; |
| 10062 | } |
| 10063 | |
| 10064 | /* Note: start_proxies() sends an alert when it fails. */ |
| 10065 | if (err != ERR_NONE) { |
| 10066 | if (retry != MAX_START_RETRIES && nb_oldpids) |
| 10067 | tell_old_pids(SIGTTIN); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 10068 | exit(1); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 10069 | } |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 10070 | |
| 10071 | if (listeners == 0) { |
| 10072 | Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 10073 | /* Note: we don't have to send anything to the old pids because we |
| 10074 | * never stopped them. */ |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 10075 | exit(1); |
| 10076 | } |
| 10077 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 10078 | /* prepare pause/play signals */ |
| 10079 | signal(SIGTTOU, sig_pause); |
| 10080 | signal(SIGTTIN, sig_listen); |
| 10081 | |
Willy TARREAU | e3283d1 | 2006-03-01 22:15:29 +0100 | [diff] [blame] | 10082 | if (global.mode & MODE_DAEMON) { |
| 10083 | global.mode &= ~MODE_VERBOSE; |
| 10084 | global.mode |= MODE_QUIET; |
| 10085 | } |
| 10086 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 10087 | /* MODE_QUIET can inhibit alerts and warnings below this line */ |
| 10088 | |
| 10089 | global.mode &= ~MODE_STARTING; |
Willy TARREAU | e3283d1 | 2006-03-01 22:15:29 +0100 | [diff] [blame] | 10090 | if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) { |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 10091 | /* detach from the tty */ |
| 10092 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 10093 | close(0); close(1); close(2); |
| 10094 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 10095 | |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 10096 | /* open log & pid files before the chroot */ |
| 10097 | if (global.mode & MODE_DAEMON && global.pidfile != NULL) { |
| 10098 | int pidfd; |
| 10099 | unlink(global.pidfile); |
| 10100 | pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 10101 | if (pidfd < 0) { |
| 10102 | Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 10103 | if (nb_oldpids) |
| 10104 | tell_old_pids(SIGTTIN); |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 10105 | exit(1); |
| 10106 | } |
| 10107 | pidfile = fdopen(pidfd, "w"); |
| 10108 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 10109 | |
| 10110 | /* chroot if needed */ |
| 10111 | if (global.chroot != NULL) { |
| 10112 | if (chroot(global.chroot) == -1) { |
| 10113 | Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 10114 | if (nb_oldpids) |
| 10115 | tell_old_pids(SIGTTIN); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 10116 | } |
| 10117 | chdir("/"); |
| 10118 | } |
| 10119 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 10120 | /* ulimits */ |
Willy TARREAU | dd67617 | 2006-03-12 18:01:33 +0100 | [diff] [blame] | 10121 | if (!global.rlimit_nofile) |
| 10122 | global.rlimit_nofile = global.maxsock; |
| 10123 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 10124 | if (global.rlimit_nofile) { |
| 10125 | limit.rlim_cur = limit.rlim_max = global.rlimit_nofile; |
| 10126 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 10127 | Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile); |
| 10128 | } |
willy tarreau | 746e26b | 2006-03-25 11:14:35 +0100 | [diff] [blame] | 10129 | } |
| 10130 | |
| 10131 | if (global.rlimit_memmax) { |
| 10132 | limit.rlim_cur = limit.rlim_max = |
| 10133 | global.rlimit_memmax * 1048576 / global.nbproc; |
| 10134 | #ifdef RLIMIT_AS |
| 10135 | if (setrlimit(RLIMIT_AS, &limit) == -1) { |
| 10136 | Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n", |
| 10137 | argv[0], global.rlimit_memmax); |
| 10138 | } |
| 10139 | #else |
| 10140 | if (setrlimit(RLIMIT_DATA, &limit) == -1) { |
| 10141 | Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n", |
| 10142 | argv[0], global.rlimit_memmax); |
| 10143 | } |
| 10144 | #endif |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 10145 | } |
| 10146 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 10147 | if (nb_oldpids) |
| 10148 | tell_old_pids(oldpids_sig); |
| 10149 | |
| 10150 | /* Note that any error at this stage will be fatal because we will not |
| 10151 | * be able to restart the old pids. |
| 10152 | */ |
| 10153 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 10154 | /* setgid / setuid */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 10155 | if (global.gid && setgid(global.gid) == -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 10156 | Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid); |
| 10157 | exit(1); |
| 10158 | } |
| 10159 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 10160 | if (global.uid && setuid(global.uid) == -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 10161 | Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid); |
| 10162 | exit(1); |
| 10163 | } |
| 10164 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 10165 | /* check ulimits */ |
| 10166 | limit.rlim_cur = limit.rlim_max = 0; |
| 10167 | getrlimit(RLIMIT_NOFILE, &limit); |
| 10168 | if (limit.rlim_cur < global.maxsock) { |
| 10169 | Warning("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. Please raise 'ulimit-n' to %d or more to avoid any trouble.\n", |
| 10170 | argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock); |
| 10171 | } |
| 10172 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 10173 | if (global.mode & MODE_DAEMON) { |
| 10174 | int ret = 0; |
| 10175 | int proc; |
| 10176 | |
| 10177 | /* the father launches the required number of processes */ |
| 10178 | for (proc = 0; proc < global.nbproc; proc++) { |
| 10179 | ret = fork(); |
| 10180 | if (ret < 0) { |
| 10181 | Alert("[%s.main()] Cannot fork.\n", argv[0]); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 10182 | if (nb_oldpids) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 10183 | exit(1); /* there has been an error */ |
| 10184 | } |
| 10185 | else if (ret == 0) /* child breaks here */ |
| 10186 | break; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 10187 | if (pidfile != NULL) { |
| 10188 | fprintf(pidfile, "%d\n", ret); |
| 10189 | fflush(pidfile); |
| 10190 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 10191 | } |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 10192 | /* close the pidfile both in children and father */ |
| 10193 | if (pidfile != NULL) |
| 10194 | fclose(pidfile); |
| 10195 | free(global.pidfile); |
| 10196 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 10197 | if (proc == global.nbproc) |
| 10198 | exit(0); /* parent must leave */ |
| 10199 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 10200 | /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure |
| 10201 | * that we can detach from the TTY. We MUST NOT do it in other cases since |
| 10202 | * it would have already be done, and 0-2 would have been affected to listening |
| 10203 | * sockets |
| 10204 | */ |
| 10205 | if (!(global.mode & MODE_QUIET)) { |
| 10206 | /* detach from the tty */ |
| 10207 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 10208 | close(0); close(1); close(2); /* close all fd's */ |
| 10209 | global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ |
| 10210 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 10211 | pid = getpid(); /* update child's pid */ |
willy tarreau | e867b48 | 2005-12-17 13:28:43 +0100 | [diff] [blame] | 10212 | setsid(); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 10213 | } |
| 10214 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 10215 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 10216 | if (cfg_polling_mechanism & POLL_USE_EPOLL) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 10217 | if (epoll_loop(POLL_LOOP_ACTION_INIT)) { |
| 10218 | epoll_loop(POLL_LOOP_ACTION_RUN); |
| 10219 | epoll_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 10220 | cfg_polling_mechanism &= POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 10221 | } |
| 10222 | else { |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 10223 | Warning("epoll() is not available. Using poll()/select() instead.\n"); |
| 10224 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 10225 | } |
| 10226 | } |
| 10227 | #endif |
| 10228 | |
| 10229 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 10230 | if (cfg_polling_mechanism & POLL_USE_POLL) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 10231 | if (poll_loop(POLL_LOOP_ACTION_INIT)) { |
| 10232 | poll_loop(POLL_LOOP_ACTION_RUN); |
| 10233 | poll_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 10234 | cfg_polling_mechanism &= POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 10235 | } |
| 10236 | else { |
| 10237 | Warning("poll() is not available. Using select() instead.\n"); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 10238 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 10239 | } |
| 10240 | } |
| 10241 | #endif |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 10242 | if (cfg_polling_mechanism & POLL_USE_SELECT) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 10243 | if (select_loop(POLL_LOOP_ACTION_INIT)) { |
| 10244 | select_loop(POLL_LOOP_ACTION_RUN); |
| 10245 | select_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 10246 | cfg_polling_mechanism &= POLL_USE_SELECT; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 10247 | } |
| 10248 | } |
| 10249 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 10250 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 10251 | /* Free all Hash Keys and all Hash elements */ |
| 10252 | appsession_cleanup(); |
| 10253 | /* Do some cleanup */ |
| 10254 | deinit(); |
| 10255 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 10256 | exit(0); |
| 10257 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 10258 | |
| 10259 | #if defined(DEBUG_HASH) |
| 10260 | static void print_table(const CHTbl *htbl) { |
| 10261 | |
| 10262 | ListElmt *element; |
| 10263 | int i; |
| 10264 | appsess *asession; |
| 10265 | |
| 10266 | /***************************************************************************** |
| 10267 | * * |
| 10268 | * Display the chained hash table. * |
| 10269 | * * |
| 10270 | *****************************************************************************/ |
| 10271 | |
| 10272 | fprintf(stdout, "Table size is %d\n", chtbl_size(htbl)); |
| 10273 | |
| 10274 | for (i = 0; i < TBLSIZ; i++) { |
| 10275 | fprintf(stdout, "Bucket[%03d]\n", i); |
| 10276 | |
| 10277 | for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) { |
| 10278 | //fprintf(stdout, "%c", *(char *)list_data(element)); |
| 10279 | asession = (appsess *)list_data(element); |
| 10280 | fprintf(stdout, "ELEM :%s:", asession->sessid); |
| 10281 | fprintf(stdout, " Server :%s: \n", asession->serverid); |
| 10282 | //fprintf(stdout, " Server request_count :%li:\n",asession->request_count); |
| 10283 | } |
| 10284 | |
| 10285 | fprintf(stdout, "\n"); |
| 10286 | } |
| 10287 | return; |
| 10288 | } /* end print_table */ |
| 10289 | #endif |
| 10290 | |
| 10291 | static int appsession_init(void) |
| 10292 | { |
| 10293 | static int initialized = 0; |
| 10294 | int idlen; |
| 10295 | struct server *s; |
| 10296 | struct proxy *p = proxy; |
| 10297 | |
| 10298 | if (!initialized) { |
| 10299 | if (!appsession_task_init()) { |
| 10300 | apools.sessid = NULL; |
| 10301 | apools.serverid = NULL; |
| 10302 | apools.ser_waste = 0; |
| 10303 | apools.ser_use = 0; |
| 10304 | apools.ser_msize = sizeof(void *); |
| 10305 | apools.ses_waste = 0; |
| 10306 | apools.ses_use = 0; |
| 10307 | apools.ses_msize = sizeof(void *); |
| 10308 | while (p) { |
| 10309 | s = p->srv; |
| 10310 | if (apools.ses_msize < p->appsession_len) |
| 10311 | apools.ses_msize = p->appsession_len; |
| 10312 | while (s) { |
| 10313 | idlen = strlen(s->id); |
| 10314 | if (apools.ser_msize < idlen) |
| 10315 | apools.ser_msize = idlen; |
| 10316 | s = s->next; |
| 10317 | } |
| 10318 | p = p->next; |
| 10319 | } |
| 10320 | apools.ser_msize ++; /* we use strings, so reserve space for '\0' */ |
| 10321 | apools.ses_msize ++; |
| 10322 | } |
| 10323 | else { |
| 10324 | fprintf(stderr, "appsession_task_init failed\n"); |
| 10325 | return -1; |
| 10326 | } |
| 10327 | initialized ++; |
| 10328 | } |
| 10329 | return 0; |
| 10330 | } |
| 10331 | |
| 10332 | static int appsession_task_init(void) |
| 10333 | { |
| 10334 | static int initialized = 0; |
| 10335 | struct task *t; |
| 10336 | if (!initialized) { |
| 10337 | if ((t = pool_alloc(task)) == NULL) |
| 10338 | return -1; |
| 10339 | t->next = t->prev = t->rqnext = NULL; |
willy tarreau | 5e698ef | 2006-05-02 14:51:00 +0200 | [diff] [blame] | 10340 | t->wq = LIST_HEAD(wait_queue[0]); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 10341 | t->state = TASK_IDLE; |
| 10342 | t->context = NULL; |
| 10343 | tv_delayfrom(&t->expire, &now, TBLCHKINT); |
| 10344 | task_queue(t); |
| 10345 | t->process = appsession_refresh; |
| 10346 | initialized ++; |
| 10347 | } |
| 10348 | return 0; |
| 10349 | } |
| 10350 | |
| 10351 | static int appsession_refresh(struct task *t) { |
| 10352 | struct proxy *p = proxy; |
| 10353 | CHTbl *htbl; |
| 10354 | ListElmt *element, *last; |
| 10355 | int i; |
| 10356 | appsess *asession; |
| 10357 | void *data; |
| 10358 | |
| 10359 | while (p) { |
| 10360 | if (p->appsession_name != NULL) { |
| 10361 | htbl = &p->htbl_proxy; |
| 10362 | /* if we ever give up the use of TBLSIZ, we need to change this */ |
| 10363 | for (i = 0; i < TBLSIZ; i++) { |
| 10364 | last = NULL; |
| 10365 | for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) { |
| 10366 | asession = (appsess *)list_data(element); |
| 10367 | if (tv_cmp2_ms(&asession->expire, &now) <= 0) { |
| 10368 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 10369 | int len; |
| 10370 | /* |
| 10371 | on Linux NULL pointers are catched by sprintf, on solaris -> segfault |
| 10372 | */ |
| 10373 | len = sprintf(trash, "appsession_refresh: cleaning up expired Session '%s' on Server %s\n", |
| 10374 | asession->sessid, asession->serverid?asession->serverid:"(null)"); |
| 10375 | write(1, trash, len); |
| 10376 | } |
| 10377 | /* delete the expired element from within the hash table */ |
| 10378 | if ((list_rem_next(&htbl->table[i], last, (void **)&data) == 0) |
| 10379 | && (htbl->table[i].destroy != NULL)) { |
| 10380 | htbl->table[i].destroy(data); |
| 10381 | } |
| 10382 | if (last == NULL) {/* patient lost his head, get a new one */ |
| 10383 | element = list_head(&htbl->table[i]); |
| 10384 | if (element == NULL) break; /* no heads left, go to next patient */ |
| 10385 | } |
| 10386 | else |
| 10387 | element = last; |
| 10388 | }/* end if (tv_cmp2_ms(&asession->expire, &now) <= 0) */ |
| 10389 | else |
| 10390 | last = element; |
| 10391 | }/* end for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) */ |
| 10392 | } |
| 10393 | } |
| 10394 | p = p->next; |
| 10395 | } |
| 10396 | tv_delayfrom(&t->expire, &now, TBLCHKINT); /* check expiration every 5 seconds */ |
| 10397 | return TBLCHKINT; |
| 10398 | } /* end appsession_refresh */ |
| 10399 | |
willy tarreau | 18a957c | 2006-04-12 19:26:23 +0200 | [diff] [blame] | 10400 | |
| 10401 | /* |
| 10402 | * Local variables: |
| 10403 | * c-indent-level: 4 |
| 10404 | * c-basic-offset: 4 |
| 10405 | * End: |
| 10406 | */ |