Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HA-Proxy : High Availability-enabled HTTP/TCP proxy |
Willy Tarreau | b455691 | 2009-07-23 13:45:47 +0200 | [diff] [blame] | 3 | * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [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 | * |
| 10 | * Please refer to RFC2068 or RFC2616 for informations about HTTP protocol, and |
| 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/ |
| 15 | * |
| 16 | * Pending bugs (may be not fixed because never reproduced) : |
| 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 |
| 19 | * the response. Seen on 1.1.8pre4, but never reproduced. May not be related to |
| 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) |
| 22 | * - a proxy with an invalid config will prevent the startup even if disabled. |
| 23 | * |
| 24 | * ChangeLog has moved to the CHANGELOG file. |
| 25 | * |
| 26 | * TODO: |
| 27 | * - handle properly intermediate incomplete server headers. Done ? |
| 28 | * - handle hot-reconfiguration |
| 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. |
| 32 | * - remove MAX_NEWHDR |
| 33 | * - cut this huge file into several ones |
| 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> |
| 55 | #include <syslog.h> |
| 56 | |
| 57 | #ifdef DEBUG_FULL |
| 58 | #include <assert.h> |
| 59 | #endif |
| 60 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 61 | #include <common/appsession.h> |
| 62 | #include <common/base64.h> |
| 63 | #include <common/cfgparse.h> |
| 64 | #include <common/compat.h> |
| 65 | #include <common/config.h> |
| 66 | #include <common/defaults.h> |
Willy Tarreau | d740bab | 2007-10-28 11:14:07 +0100 | [diff] [blame] | 67 | #include <common/errors.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 68 | #include <common/memory.h> |
| 69 | #include <common/mini-clist.h> |
| 70 | #include <common/regex.h> |
| 71 | #include <common/standard.h> |
| 72 | #include <common/time.h> |
| 73 | #include <common/uri_auth.h> |
| 74 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 75 | |
| 76 | #include <types/capture.h> |
| 77 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 78 | |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 79 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | #include <proto/backend.h> |
| 81 | #include <proto/buffers.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 82 | #include <proto/checks.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 83 | #include <proto/client.h> |
| 84 | #include <proto/fd.h> |
| 85 | #include <proto/log.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 86 | #include <proto/protocols.h> |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 87 | #include <proto/proto_http.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 88 | #include <proto/proxy.h> |
| 89 | #include <proto/queue.h> |
| 90 | #include <proto/server.h> |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 91 | #include <proto/session.h> |
Willy Tarreau | 2985794 | 2009-05-10 09:01:21 +0200 | [diff] [blame] | 92 | #include <proto/signal.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | #include <proto/stream_sock.h> |
| 94 | #include <proto/task.h> |
| 95 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 96 | #ifdef CONFIG_HAP_TCPSPLICE |
| 97 | #include <libtcpsplice.h> |
| 98 | #endif |
| 99 | |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 100 | #ifdef CONFIG_HAP_CTTPROXY |
| 101 | #include <proto/cttproxy.h> |
| 102 | #endif |
| 103 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | /*********************************************************************/ |
| 105 | |
| 106 | /*********************************************************************/ |
| 107 | |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 108 | static int cfg_nbcfgfiles; /* number of config files */ |
| 109 | static char *cfg_cfgfile[10]; /* configuration files, stop at NULL */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | char *progname = NULL; /* program name */ |
| 111 | int pid; /* current process id */ |
Willy Tarreau | 2815664 | 2007-11-26 16:13:36 +0100 | [diff] [blame] | 112 | int relative_pid = 1; /* process id starting at 1 */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | |
| 114 | /* global options */ |
| 115 | struct global global = { |
| 116 | logfac1 : -1, |
| 117 | logfac2 : -1, |
| 118 | loglev1 : 7, /* max syslog level : debug */ |
| 119 | loglev2 : 7, |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 120 | .stats_timeout = MS_TO_TICKS(10000), /* stats timeout = 10 seconds */ |
Willy Tarreau | 03f6d67 | 2007-10-18 15:15:57 +0200 | [diff] [blame] | 121 | .stats_sock = { |
| 122 | .timeout = &global.stats_timeout, |
| 123 | .maxconn = 10, /* 10 concurrent stats connections */ |
| 124 | .perm = { |
| 125 | .ux = { |
| 126 | .uid = -1, |
| 127 | .gid = -1, |
| 128 | .mode = 0, |
| 129 | } |
| 130 | } |
| 131 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 132 | /* others NULL OK */ |
| 133 | }; |
| 134 | |
| 135 | /*********************************************************************/ |
| 136 | |
| 137 | int stopping; /* non zero means stopping in progress */ |
| 138 | |
| 139 | /* Here we store informations about the pids of the processes we may pause |
| 140 | * or kill. We will send them a signal every 10 ms until we can bind to all |
| 141 | * our ports. With 200 retries, that's about 2 seconds. |
| 142 | */ |
| 143 | #define MAX_START_RETRIES 200 |
| 144 | static int nb_oldpids = 0; |
| 145 | static int *oldpids = NULL; |
| 146 | static int oldpids_sig; /* use USR1 or TERM */ |
| 147 | |
| 148 | /* this is used to drain data, and as a temporary buffer for sprintf()... */ |
| 149 | char trash[BUFSIZE]; |
| 150 | |
| 151 | const int zero = 0; |
| 152 | const int one = 1; |
Alexandre Cassen | 87ea548 | 2007-10-11 20:48:58 +0200 | [diff] [blame] | 153 | const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 154 | |
Willy Tarreau | 3ad6a76 | 2009-08-16 10:08:02 +0200 | [diff] [blame^] | 155 | char hostname[MAX_HOSTNAME_LEN] = ""; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 156 | |
| 157 | |
| 158 | /*********************************************************************/ |
| 159 | /* general purpose functions ***************************************/ |
| 160 | /*********************************************************************/ |
| 161 | |
| 162 | void display_version() |
| 163 | { |
| 164 | printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n"); |
Willy Tarreau | b455691 | 2009-07-23 13:45:47 +0200 | [diff] [blame] | 165 | printf("Copyright 2000-2009 Willy Tarreau <w@1wt.eu>\n\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 168 | void display_build_opts() |
| 169 | { |
| 170 | printf("Build options :" |
| 171 | #ifdef BUILD_TARGET |
Willy Tarreau | 9f2b730 | 2008-01-02 20:48:34 +0100 | [diff] [blame] | 172 | "\n TARGET = " BUILD_TARGET |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 173 | #endif |
| 174 | #ifdef BUILD_CPU |
Willy Tarreau | 9f2b730 | 2008-01-02 20:48:34 +0100 | [diff] [blame] | 175 | "\n CPU = " BUILD_CPU |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 176 | #endif |
| 177 | #ifdef BUILD_CC |
Willy Tarreau | 9f2b730 | 2008-01-02 20:48:34 +0100 | [diff] [blame] | 178 | "\n CC = " BUILD_CC |
| 179 | #endif |
| 180 | #ifdef BUILD_CFLAGS |
| 181 | "\n CFLAGS = " BUILD_CFLAGS |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 182 | #endif |
Willy Tarreau | 9f2b730 | 2008-01-02 20:48:34 +0100 | [diff] [blame] | 183 | #ifdef BUILD_OPTIONS |
| 184 | "\n OPTIONS = " BUILD_OPTIONS |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 185 | #endif |
| 186 | "\n\n"); |
| 187 | } |
| 188 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 189 | /* |
| 190 | * This function prints the command line usage and exits |
| 191 | */ |
| 192 | void usage(char *name) |
| 193 | { |
| 194 | display_version(); |
| 195 | fprintf(stderr, |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 196 | "Usage : %s [-f <cfgfile>]* [ -vdV" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 197 | "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n" |
| 198 | " [ -p <pidfile> ] [ -m <max megs> ]\n" |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 199 | " -v displays version ; -vv shows known build options.\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 200 | " -d enters debug mode ; -db only disables background mode.\n" |
| 201 | " -V enters verbose mode (disables quiet mode)\n" |
Willy Tarreau | 6bde87b | 2009-05-18 16:29:51 +0200 | [diff] [blame] | 202 | " -D goes daemon\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 203 | " -q quiet mode : don't display messages\n" |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 204 | " -c check mode : only check config files and exit\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 205 | " -n sets the maximum total # of connections (%d)\n" |
| 206 | " -m limits the usable amount of memory (in MB)\n" |
| 207 | " -N sets the default, per-proxy maximum # of connections (%d)\n" |
| 208 | " -p writes pids of all children to this file\n" |
| 209 | #if defined(ENABLE_EPOLL) |
| 210 | " -de disables epoll() usage even when available\n" |
| 211 | #endif |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 212 | #if defined(ENABLE_SEPOLL) |
| 213 | " -ds disables speculative epoll() usage even when available\n" |
| 214 | #endif |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 215 | #if defined(ENABLE_KQUEUE) |
| 216 | " -dk disables kqueue() usage even when available\n" |
| 217 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 218 | #if defined(ENABLE_POLL) |
| 219 | " -dp disables poll() usage even when available\n" |
| 220 | #endif |
Willy Tarreau | 3ab68cf | 2009-01-25 16:03:28 +0100 | [diff] [blame] | 221 | #if defined(CONFIG_HAP_LINUX_SPLICE) || defined(CONFIG_HAP_TCPSPLICE) |
| 222 | " -dS disables splice usage (broken on old kernels)\n" |
| 223 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 224 | " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n" |
| 225 | "\n", |
| 226 | name, DEFAULT_MAXCONN, cfg_maxpconn); |
| 227 | exit(1); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | |
| 232 | /*********************************************************************/ |
| 233 | /* more specific functions ***************************************/ |
| 234 | /*********************************************************************/ |
| 235 | |
| 236 | /* |
| 237 | * upon SIGUSR1, let's have a soft stop. |
| 238 | */ |
| 239 | void sig_soft_stop(int sig) |
| 240 | { |
| 241 | soft_stop(); |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 242 | pool_gc2(); |
Willy Tarreau | 01b3a53 | 2009-05-10 09:59:50 +0200 | [diff] [blame] | 243 | signal_register(sig, SIG_IGN); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* |
| 247 | * upon SIGTTOU, we pause everything |
| 248 | */ |
| 249 | void sig_pause(int sig) |
| 250 | { |
| 251 | pause_proxies(); |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 252 | pool_gc2(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* |
| 256 | * upon SIGTTIN, let's have a soft stop. |
| 257 | */ |
| 258 | void sig_listen(int sig) |
| 259 | { |
| 260 | listen_proxies(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* |
| 264 | * this function dumps every server's state when the process receives SIGHUP. |
| 265 | */ |
| 266 | void sig_dump_state(int sig) |
| 267 | { |
| 268 | struct proxy *p = proxy; |
| 269 | |
| 270 | Warning("SIGHUP received, dumping servers states.\n"); |
| 271 | while (p) { |
| 272 | struct server *s = p->srv; |
| 273 | |
| 274 | send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id); |
| 275 | while (s) { |
| 276 | snprintf(trash, sizeof(trash), |
Willy Tarreau | 3b88d44 | 2009-04-11 20:44:08 +0200 | [diff] [blame] | 277 | "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.", |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 278 | p->id, s->id, |
| 279 | (s->state & SRV_RUNNING) ? "UP" : "DOWN", |
| 280 | s->cur_sess, s->nbpend, s->cum_sess); |
| 281 | Warning("%s\n", trash); |
| 282 | send_log(p, LOG_NOTICE, "%s\n", trash); |
| 283 | s = s->next; |
| 284 | } |
| 285 | |
Willy Tarreau | 5fcc8f1 | 2007-09-17 11:27:09 +0200 | [diff] [blame] | 286 | /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */ |
| 287 | if (!p->srv) { |
| 288 | snprintf(trash, sizeof(trash), |
Willy Tarreau | 3b88d44 | 2009-04-11 20:44:08 +0200 | [diff] [blame] | 289 | "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.", |
Willy Tarreau | 5fcc8f1 | 2007-09-17 11:27:09 +0200 | [diff] [blame] | 290 | p->id, |
| 291 | p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn); |
| 292 | } else if (p->srv_act == 0) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 293 | snprintf(trash, sizeof(trash), |
Willy Tarreau | 3b88d44 | 2009-04-11 20:44:08 +0200 | [diff] [blame] | 294 | "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.", |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 295 | p->id, |
| 296 | (p->srv_bck) ? "is running on backup servers" : "has no server available", |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 297 | p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 298 | } else { |
| 299 | snprintf(trash, sizeof(trash), |
| 300 | "SIGHUP: Proxy %s has %d active servers and %d backup servers available." |
Willy Tarreau | 3b88d44 | 2009-04-11 20:44:08 +0200 | [diff] [blame] | 301 | " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.", |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 302 | p->id, p->srv_act, p->srv_bck, |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 303 | p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 304 | } |
| 305 | Warning("%s\n", trash); |
| 306 | send_log(p, LOG_NOTICE, "%s\n", trash); |
| 307 | |
| 308 | p = p->next; |
| 309 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void dump(int sig) |
| 313 | { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 314 | #if 0 |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 315 | struct task *t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 316 | struct session *s; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 317 | struct rb_node *node; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 318 | |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 319 | for(node = rb_first(&wait_queue[0]); |
| 320 | node != NULL; node = rb_next(node)) { |
| 321 | t = rb_entry(node, struct task, rb_node); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 322 | s = t->context; |
| 323 | qfprintf(stderr,"[dump] wq: task %p, still %ld ms, " |
Willy Tarreau | 7e5067d | 2008-12-07 16:27:56 +0100 | [diff] [blame] | 324 | "cli=%d, srv=%d, req=%d, rep=%d\n", |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 325 | s, tv_ms_remain(&now, &t->expire), |
Willy Tarreau | 7e5067d | 2008-12-07 16:27:56 +0100 | [diff] [blame] | 326 | s->si[0].state, |
| 327 | s->si[1].state, |
| 328 | s->req->l, s->rep?s->rep->l:0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 329 | } |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 330 | #endif |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 331 | /* dump memory usage then free everything possible */ |
| 332 | dump_pools(); |
| 333 | pool_gc2(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | #ifdef DEBUG_MEMORY |
| 337 | static void fast_stop(void) |
| 338 | { |
| 339 | struct proxy *p; |
| 340 | p = proxy; |
| 341 | while (p) { |
| 342 | p->grace = 0; |
| 343 | p = p->next; |
| 344 | } |
| 345 | soft_stop(); |
| 346 | } |
| 347 | |
| 348 | void sig_int(int sig) |
| 349 | { |
| 350 | /* This would normally be a hard stop, |
| 351 | but we want to be sure about deallocation, |
| 352 | and so on, so we do a soft stop with |
| 353 | 0 GRACE time |
| 354 | */ |
| 355 | fast_stop(); |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 356 | pool_gc2(); |
Willy Tarreau | 01b3a53 | 2009-05-10 09:59:50 +0200 | [diff] [blame] | 357 | /* If we are killed twice, we decide to die */ |
| 358 | signal_register(sig, SIG_DFL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void sig_term(int sig) |
| 362 | { |
| 363 | /* This would normally be a hard stop, |
| 364 | but we want to be sure about deallocation, |
| 365 | and so on, so we do a soft stop with |
| 366 | 0 GRACE time |
| 367 | */ |
| 368 | fast_stop(); |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 369 | pool_gc2(); |
Willy Tarreau | 01b3a53 | 2009-05-10 09:59:50 +0200 | [diff] [blame] | 370 | /* If we are killed twice, we decide to die */ |
| 371 | signal_register(sig, SIG_DFL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 372 | } |
| 373 | #endif |
| 374 | |
| 375 | |
| 376 | /* |
| 377 | * This function initializes all the necessary variables. It only returns |
| 378 | * if everything is OK. If something fails, it exits. |
| 379 | */ |
| 380 | void init(int argc, char **argv) |
| 381 | { |
| 382 | int i; |
| 383 | int arg_mode = 0; /* MODE_DEBUG, ... */ |
| 384 | char *old_argv = *argv; |
| 385 | char *tmp; |
| 386 | char *cfg_pidfile = NULL; |
Willy Tarreau | 058e907 | 2009-07-20 09:30:05 +0200 | [diff] [blame] | 387 | int err_code = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 388 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 389 | /* |
| 390 | * Initialize the previously static variables. |
| 391 | */ |
| 392 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 393 | totalconn = actconn = maxfd = listeners = stopping = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 394 | |
| 395 | |
| 396 | #ifdef HAPROXY_MEMMAX |
| 397 | global.rlimit_memmax = HAPROXY_MEMMAX; |
| 398 | #endif |
| 399 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 400 | tv_update_date(-1,-1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 401 | start_date = now; |
| 402 | |
Willy Tarreau | 2985794 | 2009-05-10 09:01:21 +0200 | [diff] [blame] | 403 | signal_init(); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 404 | init_task(); |
| 405 | init_session(); |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 406 | init_buffer(); |
| 407 | init_pendconn(); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 408 | init_proto_http(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 409 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 410 | global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 411 | #if defined(ENABLE_POLL) |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 412 | global.tune.options |= GTUNE_USE_POLL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 413 | #endif |
| 414 | #if defined(ENABLE_EPOLL) |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 415 | global.tune.options |= GTUNE_USE_EPOLL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 416 | #endif |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 417 | #if defined(ENABLE_SEPOLL) |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 418 | global.tune.options |= GTUNE_USE_SEPOLL; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 419 | #endif |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 420 | #if defined(ENABLE_KQUEUE) |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 421 | global.tune.options |= GTUNE_USE_KQUEUE; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 422 | #endif |
Willy Tarreau | 3ab68cf | 2009-01-25 16:03:28 +0100 | [diff] [blame] | 423 | #if defined(CONFIG_HAP_LINUX_SPLICE) || defined(CONFIG_HAP_TCPSPLICE) |
| 424 | global.tune.options |= GTUNE_USE_SPLICE; |
| 425 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 426 | |
| 427 | pid = getpid(); |
| 428 | progname = *argv; |
| 429 | while ((tmp = strchr(progname, '/')) != NULL) |
| 430 | progname = tmp + 1; |
| 431 | |
| 432 | argc--; argv++; |
| 433 | while (argc > 0) { |
| 434 | char *flag; |
| 435 | |
| 436 | if (**argv == '-') { |
| 437 | flag = *argv+1; |
| 438 | |
| 439 | /* 1 arg */ |
| 440 | if (*flag == 'v') { |
| 441 | display_version(); |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 442 | if (flag[1] == 'v') /* -vv */ |
| 443 | display_build_opts(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 444 | exit(0); |
| 445 | } |
| 446 | #if defined(ENABLE_EPOLL) |
| 447 | else if (*flag == 'd' && flag[1] == 'e') |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 448 | global.tune.options &= ~GTUNE_USE_EPOLL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 449 | #endif |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 450 | #if defined(ENABLE_SEPOLL) |
| 451 | else if (*flag == 'd' && flag[1] == 's') |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 452 | global.tune.options &= ~GTUNE_USE_SEPOLL; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 453 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 454 | #if defined(ENABLE_POLL) |
| 455 | else if (*flag == 'd' && flag[1] == 'p') |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 456 | global.tune.options &= ~GTUNE_USE_POLL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 457 | #endif |
Willy Tarreau | 69cad1a | 2007-04-10 22:45:11 +0200 | [diff] [blame] | 458 | #if defined(ENABLE_KQUEUE) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 459 | else if (*flag == 'd' && flag[1] == 'k') |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 460 | global.tune.options &= ~GTUNE_USE_KQUEUE; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 461 | #endif |
Willy Tarreau | 3ab68cf | 2009-01-25 16:03:28 +0100 | [diff] [blame] | 462 | #if defined(CONFIG_HAP_LINUX_SPLICE) || defined(CONFIG_HAP_TCPSPLICE) |
| 463 | else if (*flag == 'd' && flag[1] == 'S') |
| 464 | global.tune.options &= ~GTUNE_USE_SPLICE; |
| 465 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 466 | else if (*flag == 'V') |
| 467 | arg_mode |= MODE_VERBOSE; |
| 468 | else if (*flag == 'd' && flag[1] == 'b') |
| 469 | arg_mode |= MODE_FOREGROUND; |
| 470 | else if (*flag == 'd') |
| 471 | arg_mode |= MODE_DEBUG; |
| 472 | else if (*flag == 'c') |
| 473 | arg_mode |= MODE_CHECK; |
| 474 | else if (*flag == 'D') |
Willy Tarreau | 6bde87b | 2009-05-18 16:29:51 +0200 | [diff] [blame] | 475 | arg_mode |= MODE_DAEMON; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 476 | else if (*flag == 'q') |
| 477 | arg_mode |= MODE_QUIET; |
| 478 | else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) { |
| 479 | /* list of pids to finish ('f') or terminate ('t') */ |
| 480 | |
| 481 | if (flag[1] == 'f') |
| 482 | oldpids_sig = SIGUSR1; /* finish then exit */ |
| 483 | else |
| 484 | oldpids_sig = SIGTERM; /* terminate immediately */ |
| 485 | argv++; argc--; |
| 486 | |
| 487 | if (argc > 0) { |
| 488 | oldpids = calloc(argc, sizeof(int)); |
| 489 | while (argc > 0) { |
| 490 | oldpids[nb_oldpids] = atol(*argv); |
| 491 | if (oldpids[nb_oldpids] <= 0) |
| 492 | usage(old_argv); |
| 493 | argc--; argv++; |
| 494 | nb_oldpids++; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | else { /* >=2 args */ |
| 499 | argv++; argc--; |
| 500 | if (argc == 0) |
| 501 | usage(old_argv); |
| 502 | |
| 503 | switch (*flag) { |
| 504 | case 'n' : cfg_maxconn = atol(*argv); break; |
| 505 | case 'm' : global.rlimit_memmax = atol(*argv); break; |
| 506 | case 'N' : cfg_maxpconn = atol(*argv); break; |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 507 | case 'f' : |
| 508 | if (cfg_nbcfgfiles > MAX_CFG_FILES) { |
| 509 | Alert("Cannot load configuration file %s : too many configuration files (max %d).\n", |
| 510 | *argv, MAX_CFG_FILES); |
| 511 | exit(1); |
| 512 | } |
| 513 | cfg_cfgfile[cfg_nbcfgfiles++] = *argv; |
| 514 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 515 | case 'p' : cfg_pidfile = *argv; break; |
| 516 | default: usage(old_argv); |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | else |
| 521 | usage(old_argv); |
| 522 | argv++; argc--; |
| 523 | } |
| 524 | |
| 525 | global.mode = MODE_STARTING | /* during startup, we want most of the alerts */ |
| 526 | (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE |
| 527 | | MODE_QUIET | MODE_CHECK | MODE_DEBUG)); |
| 528 | |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 529 | if (!cfg_nbcfgfiles) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 530 | usage(old_argv); |
| 531 | |
| 532 | gethostname(hostname, MAX_HOSTNAME_LEN); |
| 533 | |
| 534 | have_appsession = 0; |
| 535 | global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */ |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 536 | |
| 537 | init_default_instance(); |
| 538 | |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 539 | for (i = 0; i < cfg_nbcfgfiles; i++) { |
Willy Tarreau | 058e907 | 2009-07-20 09:30:05 +0200 | [diff] [blame] | 540 | err_code |= readcfgfile(cfg_cfgfile[i]); |
| 541 | if (err_code & (ERR_ABORT|ERR_FATAL)) |
| 542 | Alert("Error(s) found in configuration file : %s\n", cfg_cfgfile[i]); |
| 543 | if (err_code & ERR_ABORT) |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 544 | exit(1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 545 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 546 | |
Willy Tarreau | bb92501 | 2009-07-23 13:36:36 +0200 | [diff] [blame] | 547 | err_code |= check_config_validity(); |
| 548 | if (err_code & (ERR_ABORT|ERR_FATAL)) { |
| 549 | Alert("Fatal errors found in configuration.\n"); |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 550 | exit(1); |
| 551 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 552 | |
| 553 | if (global.mode & MODE_CHECK) { |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 554 | qfprintf(stdout, "Configuration file is valid\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 555 | exit(0); |
| 556 | } |
| 557 | |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 558 | if (have_appsession) |
| 559 | appsession_init(); |
| 560 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 561 | if (start_checks() < 0) |
| 562 | exit(1); |
| 563 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 564 | if (cfg_maxconn > 0) |
| 565 | global.maxconn = cfg_maxconn; |
| 566 | |
| 567 | if (cfg_pidfile) { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 568 | free(global.pidfile); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 569 | global.pidfile = strdup(cfg_pidfile); |
| 570 | } |
| 571 | |
| 572 | if (global.maxconn == 0) |
| 573 | global.maxconn = DEFAULT_MAXCONN; |
| 574 | |
Willy Tarreau | 66aa61f | 2009-01-18 21:44:07 +0100 | [diff] [blame] | 575 | if (!global.maxpipes) { |
| 576 | /* maxpipes not specified. Count how many frontends and backends |
| 577 | * may be using splicing, and bound that to maxconn. |
| 578 | */ |
| 579 | struct proxy *cur; |
| 580 | int nbfe = 0, nbbe = 0; |
| 581 | |
| 582 | for (cur = proxy; cur; cur = cur->next) { |
| 583 | if (cur->options2 & (PR_O2_SPLIC_ANY)) { |
| 584 | if (cur->cap & PR_CAP_FE) |
| 585 | nbfe += cur->maxconn; |
| 586 | if (cur->cap & PR_CAP_BE) |
Willy Tarreau | afb4876 | 2009-01-25 10:42:05 +0100 | [diff] [blame] | 587 | nbbe += cur->fullconn ? cur->fullconn : global.maxconn; |
Willy Tarreau | 66aa61f | 2009-01-18 21:44:07 +0100 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | global.maxpipes = MAX(nbfe, nbbe); |
| 591 | if (global.maxpipes > global.maxconn) |
| 592 | global.maxpipes = global.maxconn; |
Willy Tarreau | 686ac82 | 2009-01-25 14:06:58 +0100 | [diff] [blame] | 593 | global.maxpipes /= 4; |
Willy Tarreau | 66aa61f | 2009-01-18 21:44:07 +0100 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 597 | global.maxsock += global.maxconn * 2; /* each connection needs two sockets */ |
Willy Tarreau | 3ec79b9 | 2009-01-18 20:39:42 +0100 | [diff] [blame] | 598 | global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 599 | |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 600 | if (global.tune.maxpollevents <= 0) |
| 601 | global.tune.maxpollevents = MAX_POLL_EVENTS; |
| 602 | |
Willy Tarreau | f49d1df | 2009-03-01 08:35:41 +0100 | [diff] [blame] | 603 | if (global.tune.maxaccept == 0) { |
Willy Tarreau | a0250ba | 2008-01-06 11:22:57 +0100 | [diff] [blame] | 604 | if (global.nbproc > 1) |
| 605 | global.tune.maxaccept = 8; /* leave some conns to other processes */ |
| 606 | else |
Willy Tarreau | f49d1df | 2009-03-01 08:35:41 +0100 | [diff] [blame] | 607 | global.tune.maxaccept = 100; /* accept many incoming conns at once */ |
Willy Tarreau | a0250ba | 2008-01-06 11:22:57 +0100 | [diff] [blame] | 608 | } |
| 609 | |
Willy Tarreau | 6f4a82c | 2009-03-21 20:43:57 +0100 | [diff] [blame] | 610 | if (global.tune.recv_enough == 0) |
| 611 | global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH; |
| 612 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 613 | if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) { |
| 614 | /* command line debug mode inhibits configuration mode */ |
| 615 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 616 | } |
| 617 | global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET | |
Willy Tarreau | fbee713 | 2007-10-18 13:53:22 +0200 | [diff] [blame] | 618 | MODE_VERBOSE | MODE_DEBUG )); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 619 | |
| 620 | if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) { |
| 621 | Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n"); |
| 622 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 623 | } |
| 624 | |
| 625 | if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) { |
| 626 | if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG))) |
| 627 | Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n"); |
| 628 | global.nbproc = 1; |
| 629 | } |
| 630 | |
| 631 | if (global.nbproc < 1) |
| 632 | global.nbproc = 1; |
| 633 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 634 | fdtab = (struct fdtab *)calloc(1, |
| 635 | sizeof(struct fdtab) * (global.maxsock)); |
| 636 | for (i = 0; i < global.maxsock; i++) { |
| 637 | fdtab[i].state = FD_STCLOSE; |
| 638 | } |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 639 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 640 | /* |
| 641 | * Note: we could register external pollers here. |
| 642 | * Built-in pollers have been registered before main(). |
| 643 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 644 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 645 | if (!(global.tune.options & GTUNE_USE_KQUEUE)) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 646 | disable_poller("kqueue"); |
| 647 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 648 | if (!(global.tune.options & GTUNE_USE_EPOLL)) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 649 | disable_poller("epoll"); |
| 650 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 651 | if (!(global.tune.options & GTUNE_USE_SEPOLL)) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 652 | disable_poller("sepoll"); |
| 653 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 654 | if (!(global.tune.options & GTUNE_USE_POLL)) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 655 | disable_poller("poll"); |
| 656 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 657 | if (!(global.tune.options & GTUNE_USE_SELECT)) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 658 | disable_poller("select"); |
| 659 | |
| 660 | /* Note: we could disable any poller by name here */ |
| 661 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 662 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) |
| 663 | list_pollers(stderr); |
| 664 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 665 | if (!init_pollers()) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 666 | Alert("No polling mechanism available.\n"); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 667 | exit(1); |
| 668 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 669 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) { |
| 670 | printf("Using %s() as the polling mechanism.\n", cur_poller.name); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 671 | } |
| 672 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | void deinit(void) |
| 676 | { |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 677 | struct proxy *p = proxy, *p0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 678 | struct cap_hdr *h,*h_next; |
| 679 | struct server *s,*s_next; |
| 680 | struct listener *l,*l_next; |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 681 | struct acl_cond *cond, *condb; |
| 682 | struct hdr_exp *exp, *expb; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 683 | struct acl *acl, *aclb; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 684 | struct switching_rule *rule, *ruleb; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 685 | struct redirect_rule *rdr, *rdrb; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 686 | struct uri_auth *uap, *ua = NULL; |
| 687 | struct user_auth *user; |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 688 | int i; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 689 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 690 | while (p) { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 691 | free(p->id); |
| 692 | free(p->check_req); |
| 693 | free(p->cookie_name); |
| 694 | free(p->cookie_domain); |
| 695 | free(p->url_param_name); |
| 696 | free(p->capture_name); |
| 697 | free(p->monitor_uri); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 698 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 699 | for (i = 0; i < HTTP_ERR_SIZE; i++) |
| 700 | free(p->errmsg[i].str); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 701 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 702 | for (i = 0; i < p->nb_reqadd; i++) |
| 703 | free(p->req_add[i]); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 704 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 705 | for (i = 0; i < p->nb_rspadd; i++) |
| 706 | free(p->rsp_add[i]); |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 707 | |
| 708 | list_for_each_entry_safe(cond, condb, &p->block_cond, list) { |
| 709 | LIST_DEL(&cond->list); |
| 710 | prune_acl_cond(cond); |
| 711 | free(cond); |
| 712 | } |
| 713 | |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 714 | list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) { |
| 715 | LIST_DEL(&cond->list); |
| 716 | prune_acl_cond(cond); |
| 717 | free(cond); |
| 718 | } |
| 719 | |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 720 | for (exp = p->req_exp; exp != NULL; ) { |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 721 | if (exp->preg) { |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 722 | regfree((regex_t *)exp->preg); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 723 | free((regex_t *)exp->preg); |
| 724 | } |
| 725 | |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 726 | if (exp->replace && exp->action != ACT_SETBE) |
| 727 | free((char *)exp->replace); |
| 728 | expb = exp; |
| 729 | exp = exp->next; |
| 730 | free(expb); |
| 731 | } |
| 732 | |
| 733 | for (exp = p->rsp_exp; exp != NULL; ) { |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 734 | if (exp->preg) { |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 735 | regfree((regex_t *)exp->preg); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 736 | free((regex_t *)exp->preg); |
| 737 | } |
| 738 | |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 739 | if (exp->replace && exp->action != ACT_SETBE) |
| 740 | free((char *)exp->replace); |
| 741 | expb = exp; |
| 742 | exp = exp->next; |
| 743 | free(expb); |
| 744 | } |
| 745 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 746 | /* build a list of unique uri_auths */ |
| 747 | if (!ua) |
| 748 | ua = p->uri_auth; |
| 749 | else { |
| 750 | /* check if p->uri_auth is unique */ |
| 751 | for (uap = ua; uap; uap=uap->next) |
| 752 | if (uap == p->uri_auth) |
| 753 | break; |
| 754 | |
Willy Tarreau | accc4e1 | 2008-06-24 11:14:45 +0200 | [diff] [blame] | 755 | if (!uap && p->uri_auth) { |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 756 | /* add it, if it is */ |
| 757 | p->uri_auth->next = ua; |
| 758 | ua = p->uri_auth; |
| 759 | } |
| 760 | } |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 761 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 762 | list_for_each_entry_safe(acl, aclb, &p->acl, list) { |
| 763 | LIST_DEL(&acl->list); |
| 764 | prune_acl(acl); |
| 765 | free(acl); |
| 766 | } |
| 767 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 768 | list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) { |
| 769 | LIST_DEL(&rule->list); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 770 | prune_acl_cond(rule->cond); |
| 771 | free(rule->cond); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 772 | free(rule); |
| 773 | } |
| 774 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 775 | list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) { |
| 776 | LIST_DEL(&rdr->list); |
| 777 | prune_acl_cond(rdr->cond); |
| 778 | free(rdr->cond); |
| 779 | free(rdr->rdr_str); |
| 780 | free(rdr); |
| 781 | } |
| 782 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 783 | free(p->appsession_name); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 784 | |
| 785 | h = p->req_cap; |
| 786 | while (h) { |
| 787 | h_next = h->next; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 788 | free(h->name); |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 789 | pool_destroy2(h->pool); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 790 | free(h); |
| 791 | h = h_next; |
| 792 | }/* end while(h) */ |
| 793 | |
| 794 | h = p->rsp_cap; |
| 795 | while (h) { |
| 796 | h_next = h->next; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 797 | free(h->name); |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 798 | pool_destroy2(h->pool); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 799 | free(h); |
| 800 | h = h_next; |
| 801 | }/* end while(h) */ |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 802 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 803 | s = p->srv; |
| 804 | while (s) { |
| 805 | s_next = s->next; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 806 | |
| 807 | if (s->check) { |
| 808 | task_delete(s->check); |
| 809 | task_free(s->check); |
| 810 | } |
| 811 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 812 | free(s->id); |
| 813 | free(s->cookie); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 814 | free(s); |
| 815 | s = s_next; |
| 816 | }/* end while(s) */ |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 817 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 818 | l = p->listen; |
| 819 | while (l) { |
| 820 | l_next = l->next; |
| 821 | free(l); |
| 822 | l = l_next; |
| 823 | }/* end while(l) */ |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 824 | |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 825 | pool_destroy2(p->req_cap_pool); |
| 826 | pool_destroy2(p->rsp_cap_pool); |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 827 | pool_destroy2(p->hdr_idx_pool); |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 828 | p0 = p; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 829 | p = p->next; |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 830 | free(p0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 831 | }/* end while(p) */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 832 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 833 | while (ua) { |
| 834 | uap = ua; |
| 835 | ua = ua->next; |
| 836 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 837 | free(uap->uri_prefix); |
| 838 | free(uap->auth_realm); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 839 | |
| 840 | while (uap->users) { |
| 841 | user = uap->users; |
| 842 | uap->users = uap->users->next; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 843 | free(user->user_pwd); |
| 844 | free(user); |
| 845 | } |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 846 | free(uap); |
| 847 | } |
| 848 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 849 | protocol_unbind_all(); |
| 850 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 851 | free(global.chroot); global.chroot = NULL; |
| 852 | free(global.pidfile); global.pidfile = NULL; |
| 853 | free(fdtab); fdtab = NULL; |
| 854 | free(oldpids); oldpids = NULL; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 855 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 856 | pool_destroy2(pool2_session); |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 857 | pool_destroy2(pool2_buffer); |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 858 | pool_destroy2(pool2_requri); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 859 | pool_destroy2(pool2_task); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 860 | pool_destroy2(pool2_capture); |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 861 | pool_destroy2(pool2_appsess); |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 862 | pool_destroy2(pool2_pendconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 863 | |
| 864 | if (have_appsession) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 865 | pool_destroy2(apools.serverid); |
| 866 | pool_destroy2(apools.sessid); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 867 | } |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 868 | |
| 869 | deinit_pollers(); |
| 870 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 871 | } /* end deinit() */ |
| 872 | |
| 873 | /* sends the signal <sig> to all pids found in <oldpids> */ |
| 874 | static void tell_old_pids(int sig) |
| 875 | { |
| 876 | int p; |
| 877 | for (p = 0; p < nb_oldpids; p++) |
| 878 | kill(oldpids[p], sig); |
| 879 | } |
| 880 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 881 | /* |
| 882 | * Runs the polling loop |
| 883 | * |
| 884 | * FIXME: |
| 885 | * - we still use 'listeners' to check whether we want to stop or not. |
| 886 | * |
| 887 | */ |
| 888 | void run_poll_loop() |
| 889 | { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 890 | int next; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 891 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 892 | tv_update_date(0,1); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 893 | while (1) { |
Willy Tarreau | 2985794 | 2009-05-10 09:01:21 +0200 | [diff] [blame] | 894 | /* check if we caught some signals and process them */ |
| 895 | signal_process_queue(); |
| 896 | |
Willy Tarreau | 58b458d | 2008-06-29 22:40:23 +0200 | [diff] [blame] | 897 | /* Check if we can expire some tasks */ |
| 898 | wake_expired_tasks(&next); |
| 899 | |
| 900 | /* Process a few tasks */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 901 | process_runnable_tasks(&next); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 902 | |
Willy Tarreau | 58b458d | 2008-06-29 22:40:23 +0200 | [diff] [blame] | 903 | /* maintain all proxies in a consistent state. This should quickly |
| 904 | * become a task because it becomes expensive when there are huge |
| 905 | * numbers of proxies. */ |
| 906 | maintain_proxies(&next); |
| 907 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 908 | /* stop when there's no connection left and we don't allow them anymore */ |
| 909 | if (!actconn && listeners == 0) |
| 910 | break; |
| 911 | |
Willy Tarreau | 58b458d | 2008-06-29 22:40:23 +0200 | [diff] [blame] | 912 | /* The poller will ensure it returns around <next> */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 913 | cur_poller.poll(&cur_poller, next); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 914 | } |
| 915 | } |
| 916 | |
| 917 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 918 | int main(int argc, char **argv) |
| 919 | { |
| 920 | int err, retry; |
| 921 | struct rlimit limit; |
| 922 | FILE *pidfile = NULL; |
| 923 | init(argc, argv); |
| 924 | |
Willy Tarreau | 01b3a53 | 2009-05-10 09:59:50 +0200 | [diff] [blame] | 925 | signal_register(SIGQUIT, dump); |
| 926 | signal_register(SIGUSR1, sig_soft_stop); |
| 927 | signal_register(SIGHUP, sig_dump_state); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 928 | #ifdef DEBUG_MEMORY |
Willy Tarreau | 01b3a53 | 2009-05-10 09:59:50 +0200 | [diff] [blame] | 929 | signal_register(SIGINT, sig_int); |
| 930 | signal_register(SIGTERM, sig_term); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 931 | #endif |
| 932 | |
| 933 | /* on very high loads, a sigpipe sometimes happen just between the |
| 934 | * getsockopt() which tells "it's OK to write", and the following write :-( |
| 935 | */ |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 936 | #if !defined(MSG_NOSIGNAL) || defined(CONFIG_HAP_LINUX_SPLICE) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 937 | signal(SIGPIPE, SIG_IGN); |
| 938 | #endif |
| 939 | |
| 940 | /* We will loop at most 100 times with 10 ms delay each time. |
| 941 | * That's at most 1 second. We only send a signal to old pids |
| 942 | * if we cannot grab at least one port. |
| 943 | */ |
| 944 | retry = MAX_START_RETRIES; |
| 945 | err = ERR_NONE; |
| 946 | while (retry >= 0) { |
| 947 | struct timeval w; |
| 948 | err = start_proxies(retry == 0 || nb_oldpids == 0); |
Willy Tarreau | e13e925 | 2007-12-20 23:05:50 +0100 | [diff] [blame] | 949 | /* exit the loop on no error or fatal error */ |
| 950 | if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 951 | break; |
| 952 | if (nb_oldpids == 0) |
| 953 | break; |
| 954 | |
| 955 | /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on |
| 956 | * listening sockets. So on those platforms, it would be wiser to |
| 957 | * simply send SIGUSR1, which will not be undoable. |
| 958 | */ |
| 959 | tell_old_pids(SIGTTOU); |
| 960 | /* give some time to old processes to stop listening */ |
| 961 | w.tv_sec = 0; |
| 962 | w.tv_usec = 10*1000; |
| 963 | select(0, NULL, NULL, NULL, &w); |
| 964 | retry--; |
| 965 | } |
| 966 | |
| 967 | /* Note: start_proxies() sends an alert when it fails. */ |
Willy Tarreau | 0a3b9d9 | 2009-02-04 17:05:23 +0100 | [diff] [blame] | 968 | if ((err & ~ERR_WARN) != ERR_NONE) { |
Willy Tarreau | f68da46 | 2009-06-09 14:36:00 +0200 | [diff] [blame] | 969 | if (retry != MAX_START_RETRIES && nb_oldpids) { |
| 970 | protocol_unbind_all(); /* cleanup everything we can */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 971 | tell_old_pids(SIGTTIN); |
Willy Tarreau | f68da46 | 2009-06-09 14:36:00 +0200 | [diff] [blame] | 972 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 973 | exit(1); |
| 974 | } |
| 975 | |
| 976 | if (listeners == 0) { |
| 977 | Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]); |
| 978 | /* Note: we don't have to send anything to the old pids because we |
| 979 | * never stopped them. */ |
| 980 | exit(1); |
| 981 | } |
| 982 | |
Willy Tarreau | 0a3b9d9 | 2009-02-04 17:05:23 +0100 | [diff] [blame] | 983 | if ((protocol_bind_all() & ~ERR_WARN) != ERR_NONE) { |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 984 | Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]); |
| 985 | protocol_unbind_all(); /* cleanup everything we can */ |
| 986 | if (nb_oldpids) |
| 987 | tell_old_pids(SIGTTIN); |
| 988 | exit(1); |
| 989 | } |
| 990 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 991 | /* prepare pause/play signals */ |
Willy Tarreau | 01b3a53 | 2009-05-10 09:59:50 +0200 | [diff] [blame] | 992 | signal_register(SIGTTOU, sig_pause); |
| 993 | signal_register(SIGTTIN, sig_listen); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 994 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 995 | /* MODE_QUIET can inhibit alerts and warnings below this line */ |
| 996 | |
| 997 | global.mode &= ~MODE_STARTING; |
| 998 | if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) { |
| 999 | /* detach from the tty */ |
| 1000 | fclose(stdin); fclose(stdout); fclose(stderr); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | /* open log & pid files before the chroot */ |
| 1004 | if (global.mode & MODE_DAEMON && global.pidfile != NULL) { |
| 1005 | int pidfd; |
| 1006 | unlink(global.pidfile); |
| 1007 | pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 1008 | if (pidfd < 0) { |
| 1009 | Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile); |
| 1010 | if (nb_oldpids) |
| 1011 | tell_old_pids(SIGTTIN); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1012 | protocol_unbind_all(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1013 | exit(1); |
| 1014 | } |
| 1015 | pidfile = fdopen(pidfd, "w"); |
| 1016 | } |
| 1017 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1018 | /* ulimits */ |
| 1019 | if (!global.rlimit_nofile) |
| 1020 | global.rlimit_nofile = global.maxsock; |
| 1021 | |
| 1022 | if (global.rlimit_nofile) { |
| 1023 | limit.rlim_cur = limit.rlim_max = global.rlimit_nofile; |
| 1024 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 1025 | Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | if (global.rlimit_memmax) { |
| 1030 | limit.rlim_cur = limit.rlim_max = |
| 1031 | global.rlimit_memmax * 1048576 / global.nbproc; |
| 1032 | #ifdef RLIMIT_AS |
| 1033 | if (setrlimit(RLIMIT_AS, &limit) == -1) { |
| 1034 | Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n", |
| 1035 | argv[0], global.rlimit_memmax); |
| 1036 | } |
| 1037 | #else |
| 1038 | if (setrlimit(RLIMIT_DATA, &limit) == -1) { |
| 1039 | Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n", |
| 1040 | argv[0], global.rlimit_memmax); |
| 1041 | } |
| 1042 | #endif |
| 1043 | } |
| 1044 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 1045 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | 3ab68cf | 2009-01-25 16:03:28 +0100 | [diff] [blame] | 1046 | if ((global.tune.options & GTUNE_USE_SPLICE) && (global.last_checks & LSTCHK_TCPSPLICE)) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 1047 | if (tcp_splice_start() < 0) { |
| 1048 | Alert("[%s.main()] Cannot enable tcp_splice.\n" |
| 1049 | " Make sure you have enough permissions and that the module is loadable.\n" |
Willy Tarreau | 3ab68cf | 2009-01-25 16:03:28 +0100 | [diff] [blame] | 1050 | " Alternatively, you may disable the 'tcpsplice' options in the configuration\n" |
| 1051 | " or add 'nosplice' in the global section, or start with '-dS'.\n" |
Willy Tarreau | bc69d8b | 2009-07-26 20:40:05 +0200 | [diff] [blame] | 1052 | "", argv[0]); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1053 | protocol_unbind_all(); |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 1054 | exit(1); |
| 1055 | } |
| 1056 | } |
| 1057 | #endif |
| 1058 | |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 1059 | #ifdef CONFIG_HAP_CTTPROXY |
| 1060 | if (global.last_checks & LSTCHK_CTTPROXY) { |
| 1061 | int ret; |
| 1062 | |
| 1063 | ret = check_cttproxy_version(); |
| 1064 | if (ret < 0) { |
| 1065 | Alert("[%s.main()] Cannot enable cttproxy.\n%s", |
| 1066 | argv[0], |
| 1067 | (ret == -1) ? " Incorrect module version.\n" |
| 1068 | : " Make sure you have enough permissions and that the module is loaded.\n"); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1069 | protocol_unbind_all(); |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 1070 | exit(1); |
| 1071 | } |
| 1072 | } |
| 1073 | #endif |
| 1074 | |
| 1075 | if ((global.last_checks & LSTCHK_NETADM) && global.uid) { |
| 1076 | Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n" |
Willy Tarreau | 4e30ed7 | 2009-02-04 18:02:48 +0100 | [diff] [blame] | 1077 | "", argv[0]); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1078 | protocol_unbind_all(); |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 1079 | exit(1); |
| 1080 | } |
| 1081 | |
Willy Tarreau | 4e30ed7 | 2009-02-04 18:02:48 +0100 | [diff] [blame] | 1082 | /* If the user is not root, we'll still let him try the configuration |
| 1083 | * but we inform him that unexpected behaviour may occur. |
| 1084 | */ |
| 1085 | if ((global.last_checks & LSTCHK_NETADM) && getuid()) |
| 1086 | Warning("[%s.main()] Some options which require full privileges" |
| 1087 | " might not work well.\n" |
| 1088 | "", argv[0]); |
| 1089 | |
Willy Tarreau | f223cc0 | 2007-10-15 18:57:08 +0200 | [diff] [blame] | 1090 | /* chroot if needed */ |
| 1091 | if (global.chroot != NULL) { |
| 1092 | if (chroot(global.chroot) == -1) { |
| 1093 | Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot); |
| 1094 | if (nb_oldpids) |
| 1095 | tell_old_pids(SIGTTIN); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1096 | protocol_unbind_all(); |
Willy Tarreau | f223cc0 | 2007-10-15 18:57:08 +0200 | [diff] [blame] | 1097 | exit(1); |
| 1098 | } |
| 1099 | chdir("/"); |
| 1100 | } |
| 1101 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1102 | if (nb_oldpids) |
| 1103 | tell_old_pids(oldpids_sig); |
| 1104 | |
| 1105 | /* Note that any error at this stage will be fatal because we will not |
| 1106 | * be able to restart the old pids. |
| 1107 | */ |
| 1108 | |
| 1109 | /* setgid / setuid */ |
| 1110 | if (global.gid && setgid(global.gid) == -1) { |
| 1111 | Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1112 | protocol_unbind_all(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1113 | exit(1); |
| 1114 | } |
| 1115 | |
| 1116 | if (global.uid && setuid(global.uid) == -1) { |
| 1117 | Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1118 | protocol_unbind_all(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1119 | exit(1); |
| 1120 | } |
| 1121 | |
| 1122 | /* check ulimits */ |
| 1123 | limit.rlim_cur = limit.rlim_max = 0; |
| 1124 | getrlimit(RLIMIT_NOFILE, &limit); |
| 1125 | if (limit.rlim_cur < global.maxsock) { |
| 1126 | 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", |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1127 | argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | if (global.mode & MODE_DAEMON) { |
Willy Tarreau | 0b9c02c | 2009-02-04 22:05:05 +0100 | [diff] [blame] | 1131 | struct proxy *px; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1132 | int ret = 0; |
| 1133 | int proc; |
| 1134 | |
| 1135 | /* the father launches the required number of processes */ |
| 1136 | for (proc = 0; proc < global.nbproc; proc++) { |
| 1137 | ret = fork(); |
| 1138 | if (ret < 0) { |
| 1139 | Alert("[%s.main()] Cannot fork.\n", argv[0]); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1140 | protocol_unbind_all(); |
Willy Tarreau | d680371 | 2007-10-16 07:44:56 +0200 | [diff] [blame] | 1141 | exit(1); /* there has been an error */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1142 | } |
| 1143 | else if (ret == 0) /* child breaks here */ |
| 1144 | break; |
| 1145 | if (pidfile != NULL) { |
| 1146 | fprintf(pidfile, "%d\n", ret); |
| 1147 | fflush(pidfile); |
| 1148 | } |
Willy Tarreau | dcd4771 | 2007-11-04 23:35:08 +0100 | [diff] [blame] | 1149 | relative_pid++; /* each child will get a different one */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1150 | } |
| 1151 | /* close the pidfile both in children and father */ |
| 1152 | if (pidfile != NULL) |
| 1153 | fclose(pidfile); |
| 1154 | free(global.pidfile); |
Krzysztof Oledzki | 56f1e8b | 2007-10-11 18:30:14 +0200 | [diff] [blame] | 1155 | global.pidfile = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1156 | |
Willy Tarreau | 0b9c02c | 2009-02-04 22:05:05 +0100 | [diff] [blame] | 1157 | /* we might have to unbind some proxies from some processes */ |
| 1158 | px = proxy; |
| 1159 | while (px != NULL) { |
| 1160 | if (px->bind_proc && px->state != PR_STSTOPPED) { |
| 1161 | if (!(px->bind_proc & (1 << proc))) |
| 1162 | stop_proxy(px); |
| 1163 | } |
| 1164 | px = px->next; |
| 1165 | } |
| 1166 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1167 | if (proc == global.nbproc) |
| 1168 | exit(0); /* parent must leave */ |
| 1169 | |
| 1170 | /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure |
| 1171 | * that we can detach from the TTY. We MUST NOT do it in other cases since |
| 1172 | * it would have already be done, and 0-2 would have been affected to listening |
| 1173 | * sockets |
| 1174 | */ |
Willy Tarreau | 106cb76 | 2008-11-16 07:40:34 +0100 | [diff] [blame] | 1175 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1176 | /* detach from the tty */ |
| 1177 | fclose(stdin); fclose(stdout); fclose(stderr); |
Willy Tarreau | 106cb76 | 2008-11-16 07:40:34 +0100 | [diff] [blame] | 1178 | global.mode &= ~MODE_VERBOSE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1179 | global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ |
| 1180 | } |
| 1181 | pid = getpid(); /* update child's pid */ |
| 1182 | setsid(); |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 1183 | fork_poller(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1186 | protocol_enable_all(); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1187 | /* |
| 1188 | * That's it : the central polling loop. Run until we stop. |
| 1189 | */ |
| 1190 | run_poll_loop(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1191 | |
| 1192 | /* Free all Hash Keys and all Hash elements */ |
| 1193 | appsession_cleanup(); |
| 1194 | /* Do some cleanup */ |
| 1195 | deinit(); |
| 1196 | |
| 1197 | exit(0); |
| 1198 | } |
| 1199 | |
| 1200 | |
| 1201 | /* |
| 1202 | * Local variables: |
| 1203 | * c-indent-level: 8 |
| 1204 | * c-basic-offset: 8 |
| 1205 | * End: |
| 1206 | */ |