blob: 42430586b8cdf1a775f99f4dc1f32276f09d85e1 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HA-Proxy : High Availability-enabled HTTP/TCP proxy
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>.
Willy Tarreaubaaee002006-06-26 02:48:02 +02004 *
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 *
Willy Tarreaubaaee002006-06-26 02:48:02 +020026 */
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <unistd.h>
31#include <string.h>
32#include <ctype.h>
33#include <sys/time.h>
34#include <sys/types.h>
35#include <sys/socket.h>
36#include <netinet/tcp.h>
37#include <netinet/in.h>
38#include <arpa/inet.h>
39#include <netdb.h>
40#include <fcntl.h>
41#include <errno.h>
42#include <signal.h>
43#include <stdarg.h>
44#include <sys/resource.h>
45#include <time.h>
46#include <syslog.h>
47
48#ifdef DEBUG_FULL
49#include <assert.h>
50#endif
51
Willy Tarreau2dd0d472006-06-29 17:53:05 +020052#include <common/appsession.h>
53#include <common/base64.h>
54#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020055#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020056#include <common/compat.h>
57#include <common/config.h>
58#include <common/defaults.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010059#include <common/errors.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020060#include <common/memory.h>
61#include <common/mini-clist.h>
62#include <common/regex.h>
63#include <common/standard.h>
64#include <common/time.h>
65#include <common/uri_auth.h>
66#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020067
68#include <types/capture.h>
69#include <types/global.h>
Simon Hormanac821422011-07-15 13:14:09 +090070#include <types/proto_tcp.h>
71#include <types/acl.h>
Willy Tarreau3c63fd82011-09-07 18:00:47 +020072#include <types/peers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020073
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +010074#include <proto/auth.h>
Willy Tarreau0fc45a72007-06-17 00:36:03 +020075#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020076#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020077#include <proto/channel.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020078#include <proto/checks.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020079#include <proto/fd.h>
Willy Tarreau34eb6712011-10-24 18:15:04 +020080#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020081#include <proto/log.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020082#include <proto/protocols.h>
Willy Tarreau80587432006-12-24 17:47:20 +010083#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020084#include <proto/proxy.h>
85#include <proto/queue.h>
86#include <proto/server.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020087#include <proto/session.h>
Willy Tarreau29857942009-05-10 09:01:21 +020088#include <proto/signal.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020089#include <proto/task.h>
90
Willy Tarreaub38651a2007-03-24 17:24:39 +010091#ifdef CONFIG_HAP_CTTPROXY
92#include <proto/cttproxy.h>
93#endif
94
Emeric Brunfc0421f2012-09-07 17:30:07 +020095#ifdef USE_OPENSSL
96#include <proto/ssl_sock.h>
97#endif
98
Willy Tarreaubaaee002006-06-26 02:48:02 +020099/*********************************************************************/
100
101/*********************************************************************/
102
Willy Tarreau477ecd82010-01-03 21:12:30 +0100103/* list of config files */
104static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100106int relative_pid = 1; /* process id starting at 1 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107
108/* global options */
109struct global global = {
William Lallemand5f232402012-04-05 18:02:55 +0200110 .req_count = 0,
William Lallemand0f99e342011-10-12 17:50:54 +0200111 .logsrvs = LIST_HEAD_INIT(global.logsrvs),
Willy Tarreau03f6d672007-10-18 15:15:57 +0200112 .stats_sock = {
Willy Tarreau03f6d672007-10-18 15:15:57 +0200113 .perm = {
114 .ux = {
115 .uid = -1,
116 .gid = -1,
117 .mode = 0,
118 }
119 }
Willy Tarreau27a674e2009-08-17 07:23:33 +0200120 },
Emeric Bruned760922010-10-22 17:59:25 +0200121 .unix_bind = {
122 .ux = {
123 .uid = -1,
124 .gid = -1,
125 .mode = 0,
126 }
127 },
Willy Tarreau27a674e2009-08-17 07:23:33 +0200128 .tune = {
129 .bufsize = BUFSIZE,
130 .maxrewrite = MAXREWRITE,
Willy Tarreau43961d52010-10-04 20:39:20 +0200131 .chksize = BUFSIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200132#ifdef USE_OPENSSL
133 .sslcachesize = 20000,
134#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200135 },
Willy Tarreau403edff2012-09-06 11:58:37 +0200136#if defined (USE_OPENSSL) && defined(DEFAULT_MAXSSLCONN)
137 .maxsslconn = DEFAULT_MAXSSLCONN,
138#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139 /* others NULL OK */
140};
141
142/*********************************************************************/
143
144int stopping; /* non zero means stopping in progress */
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200145int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200146
147/* Here we store informations about the pids of the processes we may pause
148 * or kill. We will send them a signal every 10 ms until we can bind to all
149 * our ports. With 200 retries, that's about 2 seconds.
150 */
151#define MAX_START_RETRIES 200
Willy Tarreaubaaee002006-06-26 02:48:02 +0200152static int *oldpids = NULL;
153static int oldpids_sig; /* use USR1 or TERM */
154
155/* this is used to drain data, and as a temporary buffer for sprintf()... */
David du Colombier7af46052012-05-16 14:16:48 +0200156char *trash = NULL;
157int trashlen = BUFSIZE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200158
Willy Tarreau8096de92010-02-26 11:12:27 +0100159/* this buffer is always the same size as standard buffers and is used for
160 * swapping data inside a buffer.
161 */
162char *swap_buffer = NULL;
163
Willy Tarreaubb545b42010-08-25 12:58:59 +0200164int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165const int zero = 0;
166const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200167const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200168
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100169char hostname[MAX_HOSTNAME_LEN];
Emeric Brun2b920a12010-09-23 18:30:22 +0200170char localpeer[MAX_HOSTNAME_LEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200171
Willy Tarreau08ceb102011-07-24 22:58:00 +0200172/* list of the temporarily limited listeners because of lack of resource */
173struct list global_listener_queue = LIST_HEAD_INIT(global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +0200174struct task *global_listener_queue_task;
175static struct task *manage_global_listener_queue(struct task *t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200176
177/*********************************************************************/
178/* general purpose functions ***************************************/
179/*********************************************************************/
180
181void display_version()
182{
183 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau9eeb57b2012-03-26 06:15:29 +0200184 printf("Copyright 2000-2012 Willy Tarreau <w@1wt.eu>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185}
186
Willy Tarreau7b066db2007-12-02 11:28:59 +0100187void display_build_opts()
188{
189 printf("Build options :"
190#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100191 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100192#endif
193#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100194 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100195#endif
196#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100197 "\n CC = " BUILD_CC
198#endif
199#ifdef BUILD_CFLAGS
200 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100201#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100202#ifdef BUILD_OPTIONS
203 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100204#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200205 "\n\nDefault settings :"
206 "\n maxconn = %d, bufsize = %d, maxrewrite = %d, maxpollevents = %d"
207 "\n\n",
208 DEFAULT_MAXCONN, BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS);
Willy Tarreaube5b6852009-10-03 18:57:08 +0200209
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100210 printf("Encrypted password support via crypt(3): "
211#ifdef CONFIG_HAP_CRYPT
212 "yes"
213#else
214 "no"
215#endif
216 "\n");
217
218 putchar('\n');
219
Willy Tarreaube5b6852009-10-03 18:57:08 +0200220 list_pollers(stdout);
221 putchar('\n');
Willy Tarreau7b066db2007-12-02 11:28:59 +0100222}
223
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224/*
225 * This function prints the command line usage and exits
226 */
227void usage(char *name)
228{
229 display_version();
230 fprintf(stderr,
Willy Tarreau5d01a632009-06-22 16:02:30 +0200231 "Usage : %s [-f <cfgfile>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200232 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200233 " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100234 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200235 " -d enters debug mode ; -db only disables background mode.\n"
Willy Tarreau6e064432012-05-08 15:40:42 +0200236 " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200237 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200238 " -D goes daemon ; -C changes to <dir> before loading files.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200239 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200240 " -c check mode : only check config files and exit\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200241 " -n sets the maximum total # of connections (%d)\n"
242 " -m limits the usable amount of memory (in MB)\n"
243 " -N sets the default, per-proxy maximum # of connections (%d)\n"
Emeric Brun2b920a12010-09-23 18:30:22 +0200244 " -L set local peer name (default to hostname)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245 " -p writes pids of all children to this file\n"
246#if defined(ENABLE_EPOLL)
247 " -de disables epoll() usage even when available\n"
248#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200249#if defined(ENABLE_SEPOLL)
250 " -ds disables speculative epoll() usage even when available\n"
251#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200252#if defined(ENABLE_KQUEUE)
253 " -dk disables kqueue() usage even when available\n"
254#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255#if defined(ENABLE_POLL)
256 " -dp disables poll() usage even when available\n"
257#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200258#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100259 " -dS disables splice usage (broken on old kernels)\n"
260#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200261 " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
262 "\n",
263 name, DEFAULT_MAXCONN, cfg_maxpconn);
264 exit(1);
265}
266
267
268
269/*********************************************************************/
270/* more specific functions ***************************************/
271/*********************************************************************/
272
273/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200274 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
275 * a signal zero to all subscribers. This means that it's as easy as
276 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200277 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200278void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200279{
280 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200281 signal_unregister_handler(sh);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200282 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200283}
284
285/*
286 * upon SIGTTOU, we pause everything
287 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200288void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200289{
290 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200291 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200292}
293
294/*
295 * upon SIGTTIN, let's have a soft stop.
296 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200297void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298{
Willy Tarreaube58c382011-07-24 18:28:10 +0200299 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200300}
301
302/*
303 * this function dumps every server's state when the process receives SIGHUP.
304 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200305void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200306{
307 struct proxy *p = proxy;
308
309 Warning("SIGHUP received, dumping servers states.\n");
310 while (p) {
311 struct server *s = p->srv;
312
313 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
314 while (s) {
David du Colombier7af46052012-05-16 14:16:48 +0200315 snprintf(trash, trashlen,
Willy Tarreau3b88d442009-04-11 20:44:08 +0200316 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200317 p->id, s->id,
318 (s->state & SRV_RUNNING) ? "UP" : "DOWN",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200319 s->cur_sess, s->nbpend, s->counters.cum_sess);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320 Warning("%s\n", trash);
321 send_log(p, LOG_NOTICE, "%s\n", trash);
322 s = s->next;
323 }
324
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200325 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
326 if (!p->srv) {
David du Colombier7af46052012-05-16 14:16:48 +0200327 snprintf(trash, trashlen,
Willy Tarreau3b88d442009-04-11 20:44:08 +0200328 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200329 p->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100330 p->feconn, p->beconn, p->totpend, p->nbpend, p->fe_counters.cum_conn, p->be_counters.cum_conn);
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200331 } else if (p->srv_act == 0) {
David du Colombier7af46052012-05-16 14:16:48 +0200332 snprintf(trash, trashlen,
Willy Tarreau3b88d442009-04-11 20:44:08 +0200333 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334 p->id,
335 (p->srv_bck) ? "is running on backup servers" : "has no server available",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100336 p->feconn, p->beconn, p->totpend, p->nbpend, p->fe_counters.cum_conn, p->be_counters.cum_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200337 } else {
David du Colombier7af46052012-05-16 14:16:48 +0200338 snprintf(trash, trashlen,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
Willy Tarreau3b88d442009-04-11 20:44:08 +0200340 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200341 p->id, p->srv_act, p->srv_bck,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100342 p->feconn, p->beconn, p->totpend, p->nbpend, p->fe_counters.cum_conn, p->be_counters.cum_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200343 }
344 Warning("%s\n", trash);
345 send_log(p, LOG_NOTICE, "%s\n", trash);
346
347 p = p->next;
348 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349}
350
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200351void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200353 /* dump memory usage then free everything possible */
354 dump_pools();
355 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356}
357
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358/*
359 * This function initializes all the necessary variables. It only returns
360 * if everything is OK. If something fails, it exits.
361 */
362void init(int argc, char **argv)
363{
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365 char *tmp;
366 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +0200367 int err_code = 0;
Willy Tarreau477ecd82010-01-03 21:12:30 +0100368 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +0000369 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +0200370 char *change_dir = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200371
David du Colombier7af46052012-05-16 14:16:48 +0200372 trash = malloc(trashlen);
373
Emeric Brun2b920a12010-09-23 18:30:22 +0200374 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
375 * the string in case of truncation, and at least FreeBSD appears not to do
376 * it.
377 */
378 memset(hostname, 0, sizeof(hostname));
379 gethostname(hostname, sizeof(hostname) - 1);
380 memset(localpeer, 0, sizeof(localpeer));
381 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
382
Willy Tarreaubaaee002006-06-26 02:48:02 +0200383 /*
384 * Initialize the previously static variables.
385 */
386
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100387 totalconn = actconn = maxfd = listeners = stopping = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388
389
390#ifdef HAPROXY_MEMMAX
391 global.rlimit_memmax = HAPROXY_MEMMAX;
392#endif
393
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200394 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395 start_date = now;
396
Willy Tarreau29857942009-05-10 09:01:21 +0200397 signal_init();
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200398 init_task();
399 init_session();
Willy Tarreau8280d642009-09-23 23:37:52 +0200400 /* warning, we init buffers later */
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200401 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +0100402 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200403
Willy Tarreau43b78992009-01-25 15:42:27 +0100404 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405#if defined(ENABLE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +0100406 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407#endif
408#if defined(ENABLE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +0100409 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200410#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200411#if defined(ENABLE_SEPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +0100412 global.tune.options |= GTUNE_USE_SEPOLL;
Willy Tarreaude99e992007-04-16 00:53:59 +0200413#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200414#if defined(ENABLE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +0100415 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200416#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200417#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100418 global.tune.options |= GTUNE_USE_SPLICE;
419#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200420
421 pid = getpid();
422 progname = *argv;
423 while ((tmp = strchr(progname, '/')) != NULL)
424 progname = tmp + 1;
425
Kevinm48936af2010-12-22 16:08:21 +0000426 /* the process name is used for the logs only */
427 global.log_tag = strdup(progname);
428
Willy Tarreaubaaee002006-06-26 02:48:02 +0200429 argc--; argv++;
430 while (argc > 0) {
431 char *flag;
432
433 if (**argv == '-') {
434 flag = *argv+1;
435
436 /* 1 arg */
437 if (*flag == 'v') {
438 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +0100439 if (flag[1] == 'v') /* -vv */
440 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200441 exit(0);
442 }
443#if defined(ENABLE_EPOLL)
444 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +0100445 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200446#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200447#if defined(ENABLE_SEPOLL)
448 else if (*flag == 'd' && flag[1] == 's')
Willy Tarreau43b78992009-01-25 15:42:27 +0100449 global.tune.options &= ~GTUNE_USE_SEPOLL;
Willy Tarreaude99e992007-04-16 00:53:59 +0200450#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200451#if defined(ENABLE_POLL)
452 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +0100453 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200454#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200455#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200456 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +0100457 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200458#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200459#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100460 else if (*flag == 'd' && flag[1] == 'S')
461 global.tune.options &= ~GTUNE_USE_SPLICE;
462#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200463 else if (*flag == 'V')
464 arg_mode |= MODE_VERBOSE;
465 else if (*flag == 'd' && flag[1] == 'b')
466 arg_mode |= MODE_FOREGROUND;
Willy Tarreau6e064432012-05-08 15:40:42 +0200467 else if (*flag == 'd' && flag[1] == 'M')
468 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469 else if (*flag == 'd')
470 arg_mode |= MODE_DEBUG;
471 else if (*flag == 'c')
472 arg_mode |= MODE_CHECK;
473 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +0200474 arg_mode |= MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200475 else if (*flag == 'q')
476 arg_mode |= MODE_QUIET;
477 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
478 /* list of pids to finish ('f') or terminate ('t') */
479
480 if (flag[1] == 'f')
481 oldpids_sig = SIGUSR1; /* finish then exit */
482 else
483 oldpids_sig = SIGTERM; /* terminate immediately */
484 argv++; argc--;
485
486 if (argc > 0) {
487 oldpids = calloc(argc, sizeof(int));
488 while (argc > 0) {
489 oldpids[nb_oldpids] = atol(*argv);
490 if (oldpids[nb_oldpids] <= 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +0200491 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200492 argc--; argv++;
493 nb_oldpids++;
494 }
495 }
496 }
497 else { /* >=2 args */
498 argv++; argc--;
499 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +0200500 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200501
502 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +0200503 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200504 case 'n' : cfg_maxconn = atol(*argv); break;
505 case 'm' : global.rlimit_memmax = atol(*argv); break;
506 case 'N' : cfg_maxpconn = atol(*argv); break;
Emeric Brun2b920a12010-09-23 18:30:22 +0200507 case 'L' : strncpy(localpeer, *argv, sizeof(localpeer) - 1); break;
Willy Tarreau5d01a632009-06-22 16:02:30 +0200508 case 'f' :
Willy Tarreau477ecd82010-01-03 21:12:30 +0100509 wl = (struct wordlist *)calloc(1, sizeof(*wl));
510 if (!wl) {
511 Alert("Cannot load configuration file %s : out of memory.\n", *argv);
Willy Tarreau5d01a632009-06-22 16:02:30 +0200512 exit(1);
513 }
Willy Tarreau477ecd82010-01-03 21:12:30 +0100514 wl->s = *argv;
515 LIST_ADDQ(&cfg_cfgfiles, &wl->list);
Willy Tarreau5d01a632009-06-22 16:02:30 +0200516 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200517 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +0200518 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200519 }
520 }
521 }
522 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +0200523 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200524 argv++; argc--;
525 }
526
527 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
528 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
529 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
530
Willy Tarreau477ecd82010-01-03 21:12:30 +0100531 if (LIST_ISEMPTY(&cfg_cfgfiles))
Willy Tarreau3bafcdc2011-09-10 19:20:23 +0200532 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200533
Willy Tarreau576132e2011-09-10 19:26:56 +0200534 if (change_dir && chdir(change_dir) < 0) {
535 Alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
536 exit(1);
537 }
538
Willy Tarreaubaaee002006-06-26 02:48:02 +0200539 have_appsession = 0;
540 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200541
542 init_default_instance();
543
Willy Tarreau477ecd82010-01-03 21:12:30 +0100544 list_for_each_entry(wl, &cfg_cfgfiles, list) {
Willy Tarreauc4382422009-12-06 13:10:44 +0100545 int ret;
546
Willy Tarreau477ecd82010-01-03 21:12:30 +0100547 ret = readcfgfile(wl->s);
Willy Tarreauc4382422009-12-06 13:10:44 +0100548 if (ret == -1) {
549 Alert("Could not open configuration file %s : %s\n",
Willy Tarreau477ecd82010-01-03 21:12:30 +0100550 wl->s, strerror(errno));
Willy Tarreauc4382422009-12-06 13:10:44 +0100551 exit(1);
552 }
Willy Tarreau25a67fa2009-12-15 21:46:25 +0100553 if (ret & (ERR_ABORT|ERR_FATAL))
Willy Tarreau477ecd82010-01-03 21:12:30 +0100554 Alert("Error(s) found in configuration file : %s\n", wl->s);
Willy Tarreau25a67fa2009-12-15 21:46:25 +0100555 err_code |= ret;
Willy Tarreau058e9072009-07-20 09:30:05 +0200556 if (err_code & ERR_ABORT)
Willy Tarreau5d01a632009-06-22 16:02:30 +0200557 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200558 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200559
Willy Tarreaubb925012009-07-23 13:36:36 +0200560 err_code |= check_config_validity();
561 if (err_code & (ERR_ABORT|ERR_FATAL)) {
562 Alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200563 exit(1);
564 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200565
566 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +0100567 struct peers *pr;
568 struct proxy *px;
569
570 for (pr = peers; pr; pr = pr->next)
571 if (pr->peers_fe)
572 break;
573
574 for (px = proxy; px; px = px->next)
575 if (px->state == PR_STNEW && px->listen)
576 break;
577
578 if (pr || px) {
579 /* At least one peer or one listener has been found */
580 qfprintf(stdout, "Configuration file is valid\n");
581 exit(0);
582 }
583 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
584 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200585 }
586
Willy Tarreaue9b26022011-08-01 20:57:55 +0200587 global_listener_queue_task = task_new();
588 if (!global_listener_queue_task) {
589 Alert("Out of memory when initializing global task\n");
590 exit(1);
591 }
592 /* very simple initialization, users will queue the task if needed */
593 global_listener_queue_task->context = NULL; /* not even a context! */
594 global_listener_queue_task->process = manage_global_listener_queue;
595 global_listener_queue_task->expire = TICK_ETERNITY;
596
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200597 /* now we know the buffer size, we can initialize the channels and buffers */
598 init_channel();
Willy Tarreau8280d642009-09-23 23:37:52 +0200599
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200600 if (have_appsession)
601 appsession_init();
602
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200603 if (start_checks() < 0)
604 exit(1);
605
Willy Tarreaubaaee002006-06-26 02:48:02 +0200606 if (cfg_maxconn > 0)
607 global.maxconn = cfg_maxconn;
608
609 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +0200610 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200611 global.pidfile = strdup(cfg_pidfile);
612 }
613
614 if (global.maxconn == 0)
615 global.maxconn = DEFAULT_MAXCONN;
616
Willy Tarreau66aa61f2009-01-18 21:44:07 +0100617 if (!global.maxpipes) {
618 /* maxpipes not specified. Count how many frontends and backends
619 * may be using splicing, and bound that to maxconn.
620 */
621 struct proxy *cur;
622 int nbfe = 0, nbbe = 0;
623
624 for (cur = proxy; cur; cur = cur->next) {
625 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
626 if (cur->cap & PR_CAP_FE)
627 nbfe += cur->maxconn;
628 if (cur->cap & PR_CAP_BE)
Willy Tarreauafb48762009-01-25 10:42:05 +0100629 nbbe += cur->fullconn ? cur->fullconn : global.maxconn;
Willy Tarreau66aa61f2009-01-18 21:44:07 +0100630 }
631 }
632 global.maxpipes = MAX(nbfe, nbbe);
633 if (global.maxpipes > global.maxconn)
634 global.maxpipes = global.maxconn;
Willy Tarreau686ac822009-01-25 14:06:58 +0100635 global.maxpipes /= 4;
Willy Tarreau66aa61f2009-01-18 21:44:07 +0100636 }
637
638
Willy Tarreauabacc2c2011-09-07 14:26:33 +0200639 global.hardmaxconn = global.maxconn; /* keep this max value */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200640 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
Willy Tarreau3ec79b92009-01-18 20:39:42 +0100641 global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200642
Willy Tarreau3c63fd82011-09-07 18:00:47 +0200643 if (global.stats_fe)
644 global.maxsock += global.stats_fe->maxconn;
645
646 if (peers) {
647 /* peers also need to bypass global maxconn */
648 struct peers *p = peers;
649
650 for (p = peers; p; p = p->next)
651 if (p->peers_fe)
652 global.maxsock += p->peers_fe->maxconn;
653 }
654
Willy Tarreau1db37712007-06-03 17:16:49 +0200655 if (global.tune.maxpollevents <= 0)
656 global.tune.maxpollevents = MAX_POLL_EVENTS;
657
Willy Tarreauf49d1df2009-03-01 08:35:41 +0100658 if (global.tune.maxaccept == 0) {
Willy Tarreau4827fd22011-07-22 21:59:59 +0200659 /* Note: we should not try to accept too many connections at once,
660 * because past one point we're significantly reducing the cache
661 * efficiency and the highest session rate significantly drops.
662 * Values between 15 and 35 seem fine on a Core i5 with 4M L3 cache.
663 */
Willy Tarreaua0250ba2008-01-06 11:22:57 +0100664 if (global.nbproc > 1)
665 global.tune.maxaccept = 8; /* leave some conns to other processes */
666 else
Willy Tarreau4827fd22011-07-22 21:59:59 +0200667 global.tune.maxaccept = 32; /* accept more incoming conns at once */
Willy Tarreaua0250ba2008-01-06 11:22:57 +0100668 }
669
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100670 if (global.tune.recv_enough == 0)
671 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
672
Willy Tarreau27a674e2009-08-17 07:23:33 +0200673 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
674 global.tune.maxrewrite = global.tune.bufsize / 2;
675
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
677 /* command line debug mode inhibits configuration mode */
678 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
679 }
680 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
Willy Tarreaufbee7132007-10-18 13:53:22 +0200681 MODE_VERBOSE | MODE_DEBUG ));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682
683 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
684 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
685 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
686 }
687
688 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
689 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
690 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
691 global.nbproc = 1;
692 }
693
694 if (global.nbproc < 1)
695 global.nbproc = 1;
696
Willy Tarreau8096de92010-02-26 11:12:27 +0100697 swap_buffer = (char *)calloc(1, global.tune.bufsize);
698
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200699 fdinfo = (struct fdinfo *)calloc(1,
700 sizeof(struct fdinfo) * (global.maxsock));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200701 fdtab = (struct fdtab *)calloc(1,
702 sizeof(struct fdtab) * (global.maxsock));
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200703 /*
704 * Note: we could register external pollers here.
705 * Built-in pollers have been registered before main().
706 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200707
Willy Tarreau43b78992009-01-25 15:42:27 +0100708 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200709 disable_poller("kqueue");
710
Willy Tarreau43b78992009-01-25 15:42:27 +0100711 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200712 disable_poller("epoll");
713
Willy Tarreau43b78992009-01-25 15:42:27 +0100714 if (!(global.tune.options & GTUNE_USE_SEPOLL))
Willy Tarreaude99e992007-04-16 00:53:59 +0200715 disable_poller("sepoll");
716
Willy Tarreau43b78992009-01-25 15:42:27 +0100717 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200718 disable_poller("poll");
719
Willy Tarreau43b78992009-01-25 15:42:27 +0100720 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200721 disable_poller("select");
722
723 /* Note: we could disable any poller by name here */
724
Willy Tarreau2ff76222007-04-09 19:29:56 +0200725 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
726 list_pollers(stderr);
727
Willy Tarreau4f60f162007-04-08 16:39:58 +0200728 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200729 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200730 exit(1);
731 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200732 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
733 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200734 }
735
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200736 if (!global.node)
737 global.node = strdup(hostname);
738
Willy Tarreaubaaee002006-06-26 02:48:02 +0200739}
740
Simon Horman6fb82592011-07-15 13:14:11 +0900741static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +0900742{
Simon Hormanac821422011-07-15 13:14:09 +0900743 struct acl_term_suite *suite, *suiteb;
744 struct acl_term *term, *termb;
745
Simon Horman6fb82592011-07-15 13:14:11 +0900746 if (!cond)
747 return;
748
749 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
750 list_for_each_entry_safe(term, termb, &suite->terms, list) {
751 LIST_DEL(&term->list);
752 free(term);
Simon Hormanac821422011-07-15 13:14:09 +0900753 }
Simon Horman6fb82592011-07-15 13:14:11 +0900754 LIST_DEL(&suite->list);
755 free(suite);
756 }
757
758 free(cond);
759}
760
761static void deinit_tcp_rules(struct list *rules)
762{
763 struct tcp_rule *trule, *truleb;
764
765 list_for_each_entry_safe(trule, truleb, rules, list) {
Simon Hormanac821422011-07-15 13:14:09 +0900766 LIST_DEL(&trule->list);
Simon Horman6fb82592011-07-15 13:14:11 +0900767 deinit_acl_cond(trule->cond);
Simon Hormanac821422011-07-15 13:14:09 +0900768 free(trule);
769 }
770}
771
Willy Tarreau12785782012-04-27 21:37:17 +0200772static void deinit_sample_arg(struct arg *p)
Simon Horman6fb82592011-07-15 13:14:11 +0900773{
Willy Tarreauf9954102012-04-20 14:03:29 +0200774 struct arg *p_back = p;
775
Simon Horman6fb82592011-07-15 13:14:11 +0900776 if (!p)
777 return;
778
Willy Tarreauf9954102012-04-20 14:03:29 +0200779 while (p->type != ARGT_STOP) {
Willy Tarreau496aa012012-06-01 10:38:29 +0200780 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreauf9954102012-04-20 14:03:29 +0200781 free(p->data.str.str);
782 p->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +0200783 p->unresolved = 0;
Willy Tarreauecfb8e82012-04-20 12:29:52 +0200784 }
Willy Tarreauf9954102012-04-20 14:03:29 +0200785 p++;
Willy Tarreauecfb8e82012-04-20 12:29:52 +0200786 }
Simon Horman6fb82592011-07-15 13:14:11 +0900787
Willy Tarreauf9954102012-04-20 14:03:29 +0200788 free(p_back);
Simon Horman6fb82592011-07-15 13:14:11 +0900789}
790
791static void deinit_stick_rules(struct list *rules)
792{
793 struct sticking_rule *rule, *ruleb;
794
795 list_for_each_entry_safe(rule, ruleb, rules, list) {
796 LIST_DEL(&rule->list);
797 deinit_acl_cond(rule->cond);
798 if (rule->expr) {
Willy Tarreau12785782012-04-27 21:37:17 +0200799 struct sample_conv_expr *conv_expr, *conv_exprb;
Simon Horman6fb82592011-07-15 13:14:11 +0900800 list_for_each_entry_safe(conv_expr, conv_exprb, &rule->expr->conv_exprs, list)
Willy Tarreau12785782012-04-27 21:37:17 +0200801 deinit_sample_arg(conv_expr->arg_p);
802 deinit_sample_arg(rule->expr->arg_p);
Simon Horman6fb82592011-07-15 13:14:11 +0900803 free(rule->expr);
804 }
805 free(rule);
806 }
807}
808
Willy Tarreaubaaee002006-06-26 02:48:02 +0200809void deinit(void)
810{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200811 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812 struct cap_hdr *h,*h_next;
813 struct server *s,*s_next;
814 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200815 struct acl_cond *cond, *condb;
816 struct hdr_exp *exp, *expb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200817 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200818 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +0200819 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +0200820 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +0100821 struct wordlist *wl, *wlb;
Willy Tarreauf4f04122010-01-28 18:10:50 +0100822 struct cond_wordlist *cwl, *cwlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200823 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +0200824 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +0100825 struct logformat_node *lf, *lfb;
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200826 struct ssl_conf *ssl_conf, *ssl_back;
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200827 int i;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200828
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200829 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200830 while (p) {
Willy Tarreaua534fea2008-08-03 12:19:50 +0200831 free(p->id);
832 free(p->check_req);
833 free(p->cookie_name);
834 free(p->cookie_domain);
835 free(p->url_param_name);
836 free(p->capture_name);
837 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +0900838 free(p->rdp_cookie_name);
Willy Tarreau39b06652012-06-01 10:58:06 +0200839 if (p->logformat_string != default_http_log_format &&
840 p->logformat_string != default_tcp_log_format &&
841 p->logformat_string != clf_http_log_format)
Willy Tarreau196729e2012-05-31 19:30:26 +0200842 free(p->logformat_string);
843
844 free(p->uniqueid_format_string);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200845
Willy Tarreaua534fea2008-08-03 12:19:50 +0200846 for (i = 0; i < HTTP_ERR_SIZE; i++)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200847 chunk_destroy(&p->errmsg[i]);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200848
Willy Tarreauf4f04122010-01-28 18:10:50 +0100849 list_for_each_entry_safe(cwl, cwlb, &p->req_add, list) {
850 LIST_DEL(&cwl->list);
851 free(cwl->s);
852 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +0100853 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200854
Willy Tarreauf4f04122010-01-28 18:10:50 +0100855 list_for_each_entry_safe(cwl, cwlb, &p->rsp_add, list) {
856 LIST_DEL(&cwl->list);
857 free(cwl->s);
858 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +0100859 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200860
861 list_for_each_entry_safe(cond, condb, &p->block_cond, list) {
862 LIST_DEL(&cond->list);
863 prune_acl_cond(cond);
864 free(cond);
865 }
866
Willy Tarreaub80c2302007-11-30 20:51:32 +0100867 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
868 LIST_DEL(&cond->list);
869 prune_acl_cond(cond);
870 free(cond);
871 }
872
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200873 for (exp = p->req_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200874 if (exp->preg) {
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200875 regfree((regex_t *)exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200876 free((regex_t *)exp->preg);
877 }
878
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200879 if (exp->replace && exp->action != ACT_SETBE)
880 free((char *)exp->replace);
881 expb = exp;
882 exp = exp->next;
883 free(expb);
884 }
885
886 for (exp = p->rsp_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200887 if (exp->preg) {
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200888 regfree((regex_t *)exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200889 free((regex_t *)exp->preg);
890 }
891
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200892 if (exp->replace && exp->action != ACT_SETBE)
893 free((char *)exp->replace);
894 expb = exp;
895 exp = exp->next;
896 free(expb);
897 }
898
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200899 /* build a list of unique uri_auths */
900 if (!ua)
901 ua = p->uri_auth;
902 else {
903 /* check if p->uri_auth is unique */
904 for (uap = ua; uap; uap=uap->next)
905 if (uap == p->uri_auth)
906 break;
907
Willy Tarreauaccc4e12008-06-24 11:14:45 +0200908 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200909 /* add it, if it is */
910 p->uri_auth->next = ua;
911 ua = p->uri_auth;
912 }
913 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200914
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200915 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
916 LIST_DEL(&acl->list);
917 prune_acl(acl);
918 free(acl);
919 }
920
Willy Tarreau4a5cade2012-04-05 21:09:48 +0200921 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
922 LIST_DEL(&srule->list);
923 prune_acl_cond(srule->cond);
924 free(srule->cond);
925 free(srule);
926 }
927
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200928 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
929 LIST_DEL(&rule->list);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200930 prune_acl_cond(rule->cond);
931 free(rule->cond);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200932 free(rule);
933 }
934
Willy Tarreaub463dfb2008-06-07 23:08:56 +0200935 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
936 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +0100937 if (rdr->cond) {
938 prune_acl_cond(rdr->cond);
939 free(rdr->cond);
940 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +0200941 free(rdr->rdr_str);
942 free(rdr);
943 }
944
William Lallemand0f99e342011-10-12 17:50:54 +0200945 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
946 LIST_DEL(&log->list);
947 free(log);
948 }
949
William Lallemand723b73a2012-02-08 16:37:49 +0100950 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
951 LIST_DEL(&lf->list);
952 free(lf);
953 }
954
Simon Hormanac821422011-07-15 13:14:09 +0900955 deinit_tcp_rules(&p->tcp_req.inspect_rules);
956 deinit_tcp_rules(&p->tcp_req.l4_rules);
957
Simon Horman6fb82592011-07-15 13:14:11 +0900958 deinit_stick_rules(&p->storersp_rules);
959 deinit_stick_rules(&p->sticking_rules);
960
Willy Tarreaua534fea2008-08-03 12:19:50 +0200961 free(p->appsession_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200962
963 h = p->req_cap;
964 while (h) {
965 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +0200966 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200967 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200968 free(h);
969 h = h_next;
970 }/* end while(h) */
971
972 h = p->rsp_cap;
973 while (h) {
974 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +0200975 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200976 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200977 free(h);
978 h = h_next;
979 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200980
Willy Tarreaubaaee002006-06-26 02:48:02 +0200981 s = p->srv;
982 while (s) {
983 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200984
985 if (s->check) {
986 task_delete(s->check);
987 task_free(s->check);
988 }
989
Willy Tarreau2e993902011-10-31 11:53:20 +0100990 if (s->warmup) {
991 task_delete(s->warmup);
992 task_free(s->warmup);
993 }
994
Willy Tarreaua534fea2008-08-03 12:19:50 +0200995 free(s->id);
996 free(s->cookie);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000997 free(s->check_data);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200998 free(s);
999 s = s_next;
1000 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001001
Willy Tarreaubaaee002006-06-26 02:48:02 +02001002 l = p->listen;
1003 while (l) {
1004 l_next = l->next;
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02001005 unbind_listener(l);
1006 delete_listener(l);
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02001007 if (l->ssl_conf) {
1008 l->ssl_conf->ref_cnt--;
1009 l->ssl_conf = NULL;
1010 }
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01001011 free(l->name);
1012 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001013 free(l);
1014 l = l_next;
1015 }/* end while(l) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001016
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02001017 ssl_back = ssl_conf = NULL;
1018#ifdef USE_OPENSSL
1019 /* Release unused SSL configs.
1020 */
1021 list_for_each_entry_safe(ssl_conf, ssl_back, &p->conf.ssl_bind, by_fe) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02001022 ssl_sock_free_all_ctx(ssl_conf);
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02001023 free(ssl_conf->ciphers);
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02001024 free(ssl_conf->file);
1025 free(ssl_conf->arg);
1026 LIST_DEL(&ssl_conf->by_fe);
1027 free(ssl_conf);
1028 }
1029#endif /* USE_OPENSSL */
1030
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01001031 free(p->desc);
1032 free(p->fwdfor_hdr_name);
1033
Willy Tarreauff011f22011-01-06 17:51:27 +01001034 free_http_req_rules(&p->http_req_rules);
Willy Tarreau918ff602011-07-25 16:33:49 +02001035 free(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01001036
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001037 pool_destroy2(p->req_cap_pool);
1038 pool_destroy2(p->rsp_cap_pool);
Simon Hormanb08584a2011-07-15 13:14:10 +09001039 pool_destroy2(p->table.pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001040
Willy Tarreau4d2d0982007-05-14 00:39:29 +02001041 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001042 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +02001043 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001044 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02001045
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001046 while (ua) {
1047 uap = ua;
1048 ua = ua->next;
1049
Willy Tarreaua534fea2008-08-03 12:19:50 +02001050 free(uap->uri_prefix);
1051 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001052 free(uap->node);
1053 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001054
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01001055 userlist_free(uap->userlist);
Willy Tarreauff011f22011-01-06 17:51:27 +01001056 free_http_req_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01001057
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001058 free(uap);
1059 }
1060
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001061 userlist_free(userlist);
1062
Willy Tarreaudd815982007-10-16 12:25:14 +02001063 protocol_unbind_all();
1064
Joe Williamsdf5b38f2010-12-29 17:05:48 +01001065 free(global.log_send_hostname); global.log_send_hostname = NULL;
Kevinm48936af2010-12-22 16:08:21 +00001066 free(global.log_tag); global.log_tag = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001067 free(global.chroot); global.chroot = NULL;
1068 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001069 free(global.node); global.node = NULL;
1070 free(global.desc); global.desc = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001071 free(fdtab); fdtab = NULL;
1072 free(oldpids); oldpids = NULL;
Willy Tarreaue9b26022011-08-01 20:57:55 +02001073 free(global_listener_queue_task); global_listener_queue_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001074
William Lallemand0f99e342011-10-12 17:50:54 +02001075 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
1076 LIST_DEL(&log->list);
1077 free(log);
1078 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01001079 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
1080 LIST_DEL(&wl->list);
1081 free(wl);
1082 }
1083
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001084 pool_destroy2(pool2_session);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001085 pool_destroy2(pool2_channel);
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001086 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001087 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +02001088 pool_destroy2(pool2_capture);
Willy Tarreau63963c62007-05-13 21:29:55 +02001089 pool_destroy2(pool2_appsess);
Willy Tarreaue4d7e552007-05-13 20:19:55 +02001090 pool_destroy2(pool2_pendconn);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02001091 pool_destroy2(pool2_sig_handlers);
Willy Tarreau34eb6712011-10-24 18:15:04 +02001092 pool_destroy2(pool2_hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001093
1094 if (have_appsession) {
Willy Tarreau63963c62007-05-13 21:29:55 +02001095 pool_destroy2(apools.serverid);
1096 pool_destroy2(apools.sessid);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001097 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001098
1099 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100} /* end deinit() */
1101
Willy Tarreaubb545b42010-08-25 12:58:59 +02001102/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
1103 * pids the signal was correctly delivered to.
1104 */
1105static int tell_old_pids(int sig)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001106{
1107 int p;
Willy Tarreaubb545b42010-08-25 12:58:59 +02001108 int ret = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001109 for (p = 0; p < nb_oldpids; p++)
Willy Tarreaubb545b42010-08-25 12:58:59 +02001110 if (kill(oldpids[p], sig) == 0)
1111 ret++;
1112 return ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001113}
1114
Willy Tarreau918ff602011-07-25 16:33:49 +02001115/* Runs the polling loop */
Willy Tarreau4f60f162007-04-08 16:39:58 +02001116void run_poll_loop()
1117{
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001118 int next;
Willy Tarreau4f60f162007-04-08 16:39:58 +02001119
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001120 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001121 while (1) {
Willy Tarreau29857942009-05-10 09:01:21 +02001122 /* check if we caught some signals and process them */
1123 signal_process_queue();
1124
Willy Tarreau58b458d2008-06-29 22:40:23 +02001125 /* Check if we can expire some tasks */
1126 wake_expired_tasks(&next);
1127
1128 /* Process a few tasks */
Willy Tarreaud825eef2007-05-12 22:35:00 +02001129 process_runnable_tasks(&next);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001130
Willy Tarreauaf7ad002010-08-31 15:39:26 +02001131 /* stop when there's nothing left to do */
1132 if (jobs == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +02001133 break;
1134
Willy Tarreau58b458d2008-06-29 22:40:23 +02001135 /* The poller will ensure it returns around <next> */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001136 cur_poller.poll(&cur_poller, next);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001137 }
1138}
1139
Willy Tarreaue9b26022011-08-01 20:57:55 +02001140/* This is the global management task for listeners. It enables listeners waiting
1141 * for global resources when there are enough free resource, or at least once in
1142 * a while. It is designed to be called as a task.
1143 */
1144static struct task *manage_global_listener_queue(struct task *t)
1145{
1146 int next = TICK_ETERNITY;
Willy Tarreaue9b26022011-08-01 20:57:55 +02001147 /* queue is empty, nothing to do */
1148 if (LIST_ISEMPTY(&global_listener_queue))
1149 goto out;
1150
1151 /* If there are still too many concurrent connections, let's wait for
1152 * some of them to go away. We don't need to re-arm the timer because
1153 * each of them will scan the queue anyway.
1154 */
1155 if (unlikely(actconn >= global.maxconn))
1156 goto out;
1157
1158 /* We should periodically try to enable listeners waiting for a global
1159 * resource here, because it is possible, though very unlikely, that
1160 * they have been blocked by a temporary lack of global resource such
1161 * as a file descriptor or memory and that the temporary condition has
1162 * disappeared.
1163 */
Willy Tarreauabacc2c2011-09-07 14:26:33 +02001164 dequeue_all_listeners(&global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +02001165
1166 out:
1167 t->expire = next;
1168 task_queue(t);
1169 return t;
1170}
Willy Tarreau4f60f162007-04-08 16:39:58 +02001171
Willy Tarreaubaaee002006-06-26 02:48:02 +02001172int main(int argc, char **argv)
1173{
1174 int err, retry;
1175 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02001176 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02001177 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001178
Emeric Bruncf20bf12010-10-22 16:06:11 +02001179 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02001180 signal_register_fct(SIGQUIT, dump, SIGQUIT);
1181 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
1182 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001183
Willy Tarreaue437c442010-03-17 18:02:46 +01001184 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
1185 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
1186 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001187 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02001188 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001189
Willy Tarreaudc23a922011-02-16 11:10:36 +01001190 /* ulimits */
1191 if (!global.rlimit_nofile)
1192 global.rlimit_nofile = global.maxsock;
1193
1194 if (global.rlimit_nofile) {
1195 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
1196 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
1197 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
1198 }
1199 }
1200
1201 if (global.rlimit_memmax) {
1202 limit.rlim_cur = limit.rlim_max =
1203 global.rlimit_memmax * 1048576 / global.nbproc;
1204#ifdef RLIMIT_AS
1205 if (setrlimit(RLIMIT_AS, &limit) == -1) {
1206 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
1207 argv[0], global.rlimit_memmax);
1208 }
1209#else
1210 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
1211 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
1212 argv[0], global.rlimit_memmax);
1213 }
1214#endif
1215 }
1216
Willy Tarreaubaaee002006-06-26 02:48:02 +02001217 /* We will loop at most 100 times with 10 ms delay each time.
1218 * That's at most 1 second. We only send a signal to old pids
1219 * if we cannot grab at least one port.
1220 */
1221 retry = MAX_START_RETRIES;
1222 err = ERR_NONE;
1223 while (retry >= 0) {
1224 struct timeval w;
1225 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01001226 /* exit the loop on no error or fatal error */
1227 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001228 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02001229 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001230 break;
1231
1232 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
1233 * listening sockets. So on those platforms, it would be wiser to
1234 * simply send SIGUSR1, which will not be undoable.
1235 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02001236 if (tell_old_pids(SIGTTOU) == 0) {
1237 /* no need to wait if we can't contact old pids */
1238 retry = 0;
1239 continue;
1240 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001241 /* give some time to old processes to stop listening */
1242 w.tv_sec = 0;
1243 w.tv_usec = 10*1000;
1244 select(0, NULL, NULL, NULL, &w);
1245 retry--;
1246 }
1247
1248 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01001249 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02001250 if (retry != MAX_START_RETRIES && nb_oldpids) {
1251 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001252 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02001253 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001254 exit(1);
1255 }
1256
1257 if (listeners == 0) {
1258 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
1259 /* Note: we don't have to send anything to the old pids because we
1260 * never stopped them. */
1261 exit(1);
1262 }
1263
Emeric Bruncf20bf12010-10-22 16:06:11 +02001264 err = protocol_bind_all(errmsg, sizeof(errmsg));
1265 if ((err & ~ERR_WARN) != ERR_NONE) {
1266 if ((err & ERR_ALERT) || (err & ERR_WARN))
1267 Alert("[%s.main()] %s.\n", argv[0], errmsg);
1268
Willy Tarreaudd815982007-10-16 12:25:14 +02001269 Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
1270 protocol_unbind_all(); /* cleanup everything we can */
1271 if (nb_oldpids)
1272 tell_old_pids(SIGTTIN);
1273 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02001274 } else if (err & ERR_WARN) {
1275 Alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02001276 }
1277
Willy Tarreaubaaee002006-06-26 02:48:02 +02001278 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02001279 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
1280 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001281
Willy Tarreaubaaee002006-06-26 02:48:02 +02001282 /* MODE_QUIET can inhibit alerts and warnings below this line */
1283
1284 global.mode &= ~MODE_STARTING;
1285 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
1286 /* detach from the tty */
1287 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001288 }
1289
1290 /* open log & pid files before the chroot */
1291 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001292 unlink(global.pidfile);
1293 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
1294 if (pidfd < 0) {
1295 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
1296 if (nb_oldpids)
1297 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02001298 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001299 exit(1);
1300 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001301 }
1302
Willy Tarreaub38651a2007-03-24 17:24:39 +01001303#ifdef CONFIG_HAP_CTTPROXY
1304 if (global.last_checks & LSTCHK_CTTPROXY) {
1305 int ret;
1306
1307 ret = check_cttproxy_version();
1308 if (ret < 0) {
1309 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
1310 argv[0],
1311 (ret == -1) ? " Incorrect module version.\n"
1312 : " Make sure you have enough permissions and that the module is loaded.\n");
Willy Tarreaudd815982007-10-16 12:25:14 +02001313 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01001314 exit(1);
1315 }
1316 }
1317#endif
1318
1319 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
1320 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
Willy Tarreau4e30ed72009-02-04 18:02:48 +01001321 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02001322 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01001323 exit(1);
1324 }
1325
Willy Tarreau4e30ed72009-02-04 18:02:48 +01001326 /* If the user is not root, we'll still let him try the configuration
1327 * but we inform him that unexpected behaviour may occur.
1328 */
1329 if ((global.last_checks & LSTCHK_NETADM) && getuid())
1330 Warning("[%s.main()] Some options which require full privileges"
1331 " might not work well.\n"
1332 "", argv[0]);
1333
Willy Tarreauf223cc02007-10-15 18:57:08 +02001334 /* chroot if needed */
1335 if (global.chroot != NULL) {
Willy Tarreau21337822012-04-29 14:11:38 +02001336 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Willy Tarreauf223cc02007-10-15 18:57:08 +02001337 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
1338 if (nb_oldpids)
1339 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02001340 protocol_unbind_all();
Willy Tarreauf223cc02007-10-15 18:57:08 +02001341 exit(1);
1342 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02001343 }
1344
Willy Tarreaubaaee002006-06-26 02:48:02 +02001345 if (nb_oldpids)
Willy Tarreaubb545b42010-08-25 12:58:59 +02001346 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001347
1348 /* Note that any error at this stage will be fatal because we will not
1349 * be able to restart the old pids.
1350 */
1351
1352 /* setgid / setuid */
1353 if (global.gid && setgid(global.gid) == -1) {
1354 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +02001355 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001356 exit(1);
1357 }
1358
1359 if (global.uid && setuid(global.uid) == -1) {
1360 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Willy Tarreaudd815982007-10-16 12:25:14 +02001361 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001362 exit(1);
1363 }
1364
1365 /* check ulimits */
1366 limit.rlim_cur = limit.rlim_max = 0;
1367 getrlimit(RLIMIT_NOFILE, &limit);
1368 if (limit.rlim_cur < global.maxsock) {
1369 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 Tarreau1772ece2009-04-03 14:49:12 +02001370 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001371 }
1372
1373 if (global.mode & MODE_DAEMON) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01001374 struct proxy *px;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001375 int ret = 0;
1376 int proc;
1377
1378 /* the father launches the required number of processes */
1379 for (proc = 0; proc < global.nbproc; proc++) {
1380 ret = fork();
1381 if (ret < 0) {
1382 Alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02001383 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +02001384 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001385 }
1386 else if (ret == 0) /* child breaks here */
1387 break;
Willy Tarreau269ab312012-09-05 08:02:48 +02001388 if (pidfd >= 0) {
1389 char pidstr[100];
1390 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
Willy Tarreauaa52bef2012-09-07 22:18:59 +02001391 if (write(pidfd, pidstr, strlen(pidstr)) < 0) /* shut gcc warning */;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001392 }
Willy Tarreaudcd47712007-11-04 23:35:08 +01001393 relative_pid++; /* each child will get a different one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001394 }
1395 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02001396 if (pidfd >= 0) {
1397 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
1398 close(pidfd);
1399 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02001400
1401 /* We won't ever use this anymore */
1402 free(oldpids); oldpids = NULL;
1403 free(global.chroot); global.chroot = NULL;
1404 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001405
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01001406 /* we might have to unbind some proxies from some processes */
1407 px = proxy;
1408 while (px != NULL) {
1409 if (px->bind_proc && px->state != PR_STSTOPPED) {
1410 if (!(px->bind_proc & (1 << proc)))
1411 stop_proxy(px);
1412 }
1413 px = px->next;
1414 }
1415
Willy Tarreaubaaee002006-06-26 02:48:02 +02001416 if (proc == global.nbproc)
1417 exit(0); /* parent must leave */
1418
1419 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
1420 * that we can detach from the TTY. We MUST NOT do it in other cases since
1421 * it would have already be done, and 0-2 would have been affected to listening
1422 * sockets
1423 */
Willy Tarreau106cb762008-11-16 07:40:34 +01001424 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001425 /* detach from the tty */
1426 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreau106cb762008-11-16 07:40:34 +01001427 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001428 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
1429 }
1430 pid = getpid(); /* update child's pid */
1431 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02001432 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001433 }
1434
Willy Tarreaudd815982007-10-16 12:25:14 +02001435 protocol_enable_all();
Willy Tarreau4f60f162007-04-08 16:39:58 +02001436 /*
1437 * That's it : the central polling loop. Run until we stop.
1438 */
1439 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001440
1441 /* Free all Hash Keys and all Hash elements */
1442 appsession_cleanup();
1443 /* Do some cleanup */
1444 deinit();
1445
1446 exit(0);
1447}
1448
1449
1450/*
1451 * Local variables:
1452 * c-indent-level: 8
1453 * c-basic-offset: 8
1454 * End:
1455 */