Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Proxy variables and functions. |
| 3 | * |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <fcntl.h> |
| 14 | #include <unistd.h> |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 15 | #include <string.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <sys/stat.h> |
| 19 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 20 | #include <common/defaults.h> |
| 21 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 22 | #include <common/config.h> |
Willy Tarreau | d740bab | 2007-10-28 11:14:07 +0100 | [diff] [blame] | 23 | #include <common/errors.h> |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 24 | #include <common/memory.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 25 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | |
| 27 | #include <types/global.h> |
| 28 | #include <types/polling.h> |
| 29 | |
| 30 | #include <proto/client.h> |
Alexandre Cassen | 87ea548 | 2007-10-11 20:48:58 +0200 | [diff] [blame] | 31 | #include <proto/backend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | #include <proto/fd.h> |
| 33 | #include <proto/log.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 34 | #include <proto/protocols.h> |
| 35 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | #include <proto/proxy.h> |
| 37 | |
| 38 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 39 | int listeners; /* # of proxy listeners, set by cfgparse, unset by maintain_proxies */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | struct proxy *proxy = NULL; /* list of all existing proxies */ |
Willy Tarreau | dcd4771 | 2007-11-04 23:35:08 +0100 | [diff] [blame] | 41 | int next_pxid = 1; /* UUID assigned to next new proxy, 0 reserved */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 43 | /* |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 44 | * This function returns a string containing a name describing capabilities to |
| 45 | * report comprehensible error messages. Specifically, it will return the words |
| 46 | * "frontend", "backend", "ruleset" when appropriate, or "proxy" for all other |
| 47 | * cases including the proxies declared in "listen" mode. |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 48 | */ |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 49 | const char *proxy_cap_str(int cap) |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 50 | { |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 51 | if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) { |
| 52 | if (cap & PR_CAP_FE) |
| 53 | return "frontend"; |
| 54 | else if (cap & PR_CAP_BE) |
| 55 | return "backend"; |
| 56 | else if (cap & PR_CAP_RS) |
| 57 | return "ruleset"; |
| 58 | } |
| 59 | return "proxy"; |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 62 | /* |
| 63 | * This function returns a string containing the mode of the proxy in a format |
| 64 | * suitable for error messages. |
| 65 | */ |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 66 | const char *proxy_mode_str(int mode) { |
| 67 | |
| 68 | if (mode == PR_MODE_TCP) |
| 69 | return "tcp"; |
| 70 | else if (mode == PR_MODE_HTTP) |
| 71 | return "http"; |
| 72 | else if (mode == PR_MODE_HEALTH) |
| 73 | return "health"; |
| 74 | else |
| 75 | return "unknown"; |
| 76 | } |
| 77 | |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 78 | /* This function parses a "timeout" statement in a proxy section. It returns |
| 79 | * -1 if there is any error, 1 for a warning, otherwise zero. If it does not |
| 80 | * return zero, it may write an error message into the <err> buffer, for at |
| 81 | * most <errlen> bytes, trailing zero included. The trailing '\n' must not |
| 82 | * be written. The function must be called with <args> pointing to the first |
| 83 | * word after "timeout", with <proxy> pointing to the proxy being parsed, and |
| 84 | * <defpx> to the default proxy or NULL. As a special case for compatibility |
| 85 | * with older configs, it also accepts "{cli|srv|con}timeout" in args[0]. |
| 86 | */ |
| 87 | int proxy_parse_timeout(const char **args, struct proxy *proxy, |
| 88 | struct proxy *defpx, char *err, int errlen) |
| 89 | { |
| 90 | unsigned timeout; |
| 91 | int retval, cap; |
| 92 | const char *res, *name; |
| 93 | struct timeval *tv = NULL; |
| 94 | struct timeval *td = NULL; |
| 95 | |
| 96 | retval = 0; |
| 97 | name = args[0]; |
| 98 | if (!strcmp(args[0], "client") || !strcmp(args[0], "clitimeout")) { |
| 99 | name = "client"; |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 100 | tv = &proxy->timeout.client; |
| 101 | td = &defpx->timeout.client; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 102 | cap = PR_CAP_FE; |
| 103 | } else if (!strcmp(args[0], "tarpit")) { |
| 104 | tv = &proxy->timeout.tarpit; |
| 105 | td = &defpx->timeout.tarpit; |
Willy Tarreau | 51c9bde | 2008-01-06 13:40:03 +0100 | [diff] [blame] | 106 | cap = PR_CAP_FE | PR_CAP_BE; |
Willy Tarreau | 036fae0 | 2008-01-06 13:24:40 +0100 | [diff] [blame] | 107 | } else if (!strcmp(args[0], "http-request")) { |
| 108 | tv = &proxy->timeout.httpreq; |
| 109 | td = &defpx->timeout.httpreq; |
| 110 | cap = PR_CAP_FE; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 111 | } else if (!strcmp(args[0], "server") || !strcmp(args[0], "srvtimeout")) { |
| 112 | name = "server"; |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 113 | tv = &proxy->timeout.server; |
| 114 | td = &defpx->timeout.server; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 115 | cap = PR_CAP_BE; |
| 116 | } else if (!strcmp(args[0], "connect") || !strcmp(args[0], "contimeout")) { |
| 117 | name = "connect"; |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 118 | tv = &proxy->timeout.connect; |
| 119 | td = &defpx->timeout.connect; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 120 | cap = PR_CAP_BE; |
| 121 | } else if (!strcmp(args[0], "appsession")) { |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 122 | tv = &proxy->timeout.appsession; |
| 123 | td = &defpx->timeout.appsession; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 124 | cap = PR_CAP_BE; |
| 125 | } else if (!strcmp(args[0], "queue")) { |
| 126 | tv = &proxy->timeout.queue; |
| 127 | td = &defpx->timeout.queue; |
| 128 | cap = PR_CAP_BE; |
| 129 | } else { |
Willy Tarreau | 036fae0 | 2008-01-06 13:24:40 +0100 | [diff] [blame] | 130 | snprintf(err, errlen, |
| 131 | "timeout '%s': must be 'client', 'server', 'connect', " |
| 132 | "'appsession', 'queue', 'http-request' or 'tarpit'", |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 133 | args[0]); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | if (*args[1] == 0) { |
| 138 | snprintf(err, errlen, "%s timeout expects an integer value (in milliseconds)", name); |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | res = parse_time_err(args[1], &timeout, TIME_UNIT_MS); |
| 143 | if (res) { |
| 144 | snprintf(err, errlen, "unexpected character '%c' in %s timeout", *err, name); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | if (!(proxy->cap & cap)) { |
| 149 | snprintf(err, errlen, "%s timeout will be ignored because %s '%s' has no %s capability", |
| 150 | name, proxy_type_str(proxy), proxy->id, |
| 151 | (cap & PR_CAP_BE) ? "backend" : "frontend"); |
| 152 | retval = 1; |
| 153 | } |
| 154 | else if (defpx && !__tv_iseq(tv, td)) { |
| 155 | snprintf(err, errlen, "overwriting %s timeout which was already specified", name); |
| 156 | retval = 1; |
| 157 | } |
| 158 | |
| 159 | if (timeout) |
| 160 | __tv_from_ms(tv, timeout); |
| 161 | else |
| 162 | tv_eternity(tv); |
| 163 | |
| 164 | return retval; |
| 165 | } |
| 166 | |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 167 | /* |
| 168 | * This function finds a proxy with matching name, mode and with satisfying |
| 169 | * capabilities. It also checks if there are more matching proxies with |
| 170 | * requested name as this often leads into unexpected situations. |
| 171 | */ |
| 172 | |
| 173 | struct proxy *findproxy(const char *name, int mode, int cap) { |
| 174 | |
| 175 | struct proxy *curproxy, *target=NULL; |
| 176 | |
| 177 | for (curproxy = proxy; curproxy; curproxy = curproxy->next) { |
| 178 | if ((curproxy->cap & cap)!=cap || strcmp(curproxy->id, name)) |
| 179 | continue; |
| 180 | |
| 181 | if (curproxy->mode != mode) { |
| 182 | Alert("Unable to use proxy '%s' with wrong mode, required: %s, has: %s.\n", |
| 183 | name, proxy_mode_str(mode), proxy_mode_str(curproxy->mode)); |
| 184 | Alert("You may want to use 'mode %s'.\n", proxy_mode_str(mode)); |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | if (!target) { |
| 189 | target = curproxy; |
| 190 | continue; |
| 191 | } |
| 192 | |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 193 | Alert("Refusing to use duplicated proxy '%s' with overlapping capabilities: %s/%s!\n", |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 194 | name, proxy_type_str(curproxy), proxy_type_str(target)); |
| 195 | |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | return target; |
| 200 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 201 | |
| 202 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 203 | * This function creates all proxy sockets. It should be done very early, |
| 204 | * typically before privileges are dropped. The sockets will be registered |
| 205 | * but not added to any fd_set, in order not to loose them across the fork(). |
| 206 | * The proxies also start in IDLE state, meaning that it will be |
| 207 | * maintain_proxies that will finally complete their loading. |
| 208 | * |
| 209 | * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 210 | * Retryable errors will only be printed if <verbose> is not zero. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 211 | */ |
| 212 | int start_proxies(int verbose) |
| 213 | { |
| 214 | struct proxy *curproxy; |
| 215 | struct listener *listener; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 216 | int lerr, err = ERR_NONE; |
| 217 | int pxerr; |
| 218 | char msg[100]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 219 | |
| 220 | for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) { |
| 221 | if (curproxy->state != PR_STNEW) |
| 222 | continue; /* already initialized */ |
| 223 | |
| 224 | pxerr = 0; |
| 225 | for (listener = curproxy->listen; listener != NULL; listener = listener->next) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 226 | if (listener->state != LI_ASSIGNED) |
| 227 | continue; /* already started */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 228 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 229 | lerr = tcp_bind_listener(listener, msg, sizeof(msg)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 230 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 231 | /* errors are reported if <verbose> is set or if they are fatal */ |
| 232 | if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) { |
| 233 | if (lerr & ERR_ALERT) |
| 234 | Alert("Starting %s %s: %s\n", |
| 235 | proxy_type_str(curproxy), curproxy->id, msg); |
| 236 | else if (lerr & ERR_WARN) |
| 237 | Warning("Starting %s %s: %s\n", |
| 238 | proxy_type_str(curproxy), curproxy->id, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 241 | err |= lerr; |
| 242 | if (lerr & (ERR_ABORT | ERR_FATAL)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 243 | pxerr |= 1; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 244 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 245 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 246 | else if (lerr & ERR_CODE) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 247 | pxerr |= 1; |
| 248 | continue; |
| 249 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | if (!pxerr) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 253 | curproxy->state = PR_STIDLE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 254 | send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id); |
| 255 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 256 | |
| 257 | if (err & ERR_ABORT) |
| 258 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return err; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | /* |
| 266 | * this function enables proxies when there are enough free sessions, |
| 267 | * or stops them when the table is full. It is designed to be called from the |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 268 | * select_loop(). It returns the date of next expiration event during stop |
| 269 | * time, ETERNITY otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 270 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 271 | void maintain_proxies(struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 272 | { |
| 273 | struct proxy *p; |
| 274 | struct listener *l; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 275 | |
| 276 | p = proxy; |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 277 | tv_eternity(next); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 278 | |
| 279 | /* if there are enough free sessions, we'll activate proxies */ |
| 280 | if (actconn < global.maxconn) { |
| 281 | while (p) { |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 282 | if (p->feconn < p->maxconn) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 283 | if (p->state == PR_STIDLE) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 284 | for (l = p->listen; l != NULL; l = l->next) |
| 285 | enable_listener(l); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 286 | p->state = PR_STRUN; |
| 287 | } |
| 288 | } |
| 289 | else { |
| 290 | if (p->state == PR_STRUN) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 291 | for (l = p->listen; l != NULL; l = l->next) |
| 292 | disable_listener(l); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 293 | p->state = PR_STIDLE; |
| 294 | } |
| 295 | } |
| 296 | p = p->next; |
| 297 | } |
| 298 | } |
| 299 | else { /* block all proxies */ |
| 300 | while (p) { |
| 301 | if (p->state == PR_STRUN) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 302 | for (l = p->listen; l != NULL; l = l->next) |
| 303 | disable_listener(l); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 304 | p->state = PR_STIDLE; |
| 305 | } |
| 306 | p = p->next; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (stopping) { |
| 311 | p = proxy; |
| 312 | while (p) { |
| 313 | if (p->state != PR_STSTOPPED) { |
| 314 | int t; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 315 | t = tv_ms_remain2(&now, &p->stop_time); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 316 | if (t == 0) { |
| 317 | Warning("Proxy %s stopped.\n", p->id); |
| 318 | send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id); |
| 319 | |
| 320 | for (l = p->listen; l != NULL; l = l->next) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 321 | unbind_listener(l); |
| 322 | if (l->state >= LI_ASSIGNED) { |
| 323 | delete_listener(l); |
| 324 | listeners--; |
| 325 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 326 | } |
| 327 | p->state = PR_STSTOPPED; |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 328 | /* try to free more memory */ |
| 329 | pool_gc2(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 330 | } |
| 331 | else { |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 332 | tv_bound(next, &p->stop_time); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | p = p->next; |
| 336 | } |
| 337 | } |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 338 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | |
| 342 | /* |
| 343 | * this function disables health-check servers so that the process will quickly be ignored |
| 344 | * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace |
| 345 | * time will not be used since it would already not listen anymore to the socket. |
| 346 | */ |
| 347 | void soft_stop(void) |
| 348 | { |
| 349 | struct proxy *p; |
| 350 | |
| 351 | stopping = 1; |
| 352 | p = proxy; |
| 353 | tv_now(&now); /* else, the old time before select will be used */ |
| 354 | while (p) { |
| 355 | if (p->state != PR_STSTOPPED) { |
| 356 | Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace); |
| 357 | send_log(p, LOG_WARNING, "Stopping proxy %s in %d ms.\n", p->id, p->grace); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 358 | tv_ms_add(&p->stop_time, &now, p->grace); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 359 | } |
| 360 | p = p->next; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | |
| 365 | /* |
| 366 | * Linux unbinds the listen socket after a SHUT_RD, and ignores SHUT_WR. |
| 367 | * Solaris refuses either shutdown(). |
| 368 | * OpenBSD ignores SHUT_RD but closes upon SHUT_WR and refuses to rebind. |
| 369 | * So a common validation path involves SHUT_WR && listen && SHUT_RD. |
| 370 | * If disabling at least one listener returns an error, then the proxy |
| 371 | * state is set to PR_STERROR because we don't know how to resume from this. |
| 372 | */ |
| 373 | void pause_proxy(struct proxy *p) |
| 374 | { |
| 375 | struct listener *l; |
| 376 | for (l = p->listen; l != NULL; l = l->next) { |
| 377 | if (shutdown(l->fd, SHUT_WR) == 0 && |
Willy Tarreau | c73ce2b | 2008-01-06 10:55:10 +0100 | [diff] [blame] | 378 | listen(l->fd, p->backlog ? p->backlog : p->maxconn) == 0 && |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 379 | shutdown(l->fd, SHUT_RD) == 0) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 380 | EV_FD_CLR(l->fd, DIR_RD); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 381 | if (p->state != PR_STERROR) |
| 382 | p->state = PR_STPAUSED; |
| 383 | } |
| 384 | else |
| 385 | p->state = PR_STERROR; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * This function temporarily disables listening so that another new instance |
| 391 | * can start listening. It is designed to be called upon reception of a |
| 392 | * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop |
| 393 | * the proxy, or a SIGTTIN can be sent to listen again. |
| 394 | */ |
| 395 | void pause_proxies(void) |
| 396 | { |
| 397 | int err; |
| 398 | struct proxy *p; |
| 399 | |
| 400 | err = 0; |
| 401 | p = proxy; |
| 402 | tv_now(&now); /* else, the old time before select will be used */ |
| 403 | while (p) { |
| 404 | if (p->state != PR_STERROR && |
| 405 | p->state != PR_STSTOPPED && |
| 406 | p->state != PR_STPAUSED) { |
| 407 | Warning("Pausing proxy %s.\n", p->id); |
| 408 | send_log(p, LOG_WARNING, "Pausing proxy %s.\n", p->id); |
| 409 | pause_proxy(p); |
| 410 | if (p->state != PR_STPAUSED) { |
| 411 | err |= 1; |
| 412 | Warning("Proxy %s failed to enter pause mode.\n", p->id); |
| 413 | send_log(p, LOG_WARNING, "Proxy %s failed to enter pause mode.\n", p->id); |
| 414 | } |
| 415 | } |
| 416 | p = p->next; |
| 417 | } |
| 418 | if (err) { |
| 419 | Warning("Some proxies refused to pause, performing soft stop now.\n"); |
| 420 | send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n"); |
| 421 | soft_stop(); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | |
| 426 | /* |
| 427 | * This function reactivates listening. This can be used after a call to |
| 428 | * sig_pause(), for example when a new instance has failed starting up. |
| 429 | * It is designed to be called upon reception of a SIGTTIN. |
| 430 | */ |
| 431 | void listen_proxies(void) |
| 432 | { |
| 433 | struct proxy *p; |
| 434 | struct listener *l; |
| 435 | |
| 436 | p = proxy; |
| 437 | tv_now(&now); /* else, the old time before select will be used */ |
| 438 | while (p) { |
| 439 | if (p->state == PR_STPAUSED) { |
| 440 | Warning("Enabling proxy %s.\n", p->id); |
| 441 | send_log(p, LOG_WARNING, "Enabling proxy %s.\n", p->id); |
| 442 | |
| 443 | for (l = p->listen; l != NULL; l = l->next) { |
Willy Tarreau | c73ce2b | 2008-01-06 10:55:10 +0100 | [diff] [blame] | 444 | if (listen(l->fd, p->backlog ? p->backlog : p->maxconn) == 0) { |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 445 | if (actconn < global.maxconn && p->feconn < p->maxconn) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 446 | EV_FD_SET(l->fd, DIR_RD); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 447 | p->state = PR_STRUN; |
| 448 | } |
| 449 | else |
| 450 | p->state = PR_STIDLE; |
| 451 | } else { |
| 452 | int port; |
| 453 | |
| 454 | if (l->addr.ss_family == AF_INET6) |
| 455 | port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port); |
| 456 | else |
| 457 | port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port); |
| 458 | |
| 459 | Warning("Port %d busy while trying to enable proxy %s.\n", |
| 460 | port, p->id); |
| 461 | send_log(p, LOG_WARNING, "Port %d busy while trying to enable proxy %s.\n", |
| 462 | port, p->id); |
| 463 | /* Another port might have been enabled. Let's stop everything. */ |
| 464 | pause_proxy(p); |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | p = p->next; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | |
| 474 | /* |
| 475 | * Local variables: |
| 476 | * c-indent-level: 8 |
| 477 | * c-basic-offset: 8 |
| 478 | * End: |
| 479 | */ |