blob: cc1635907b51072785ad65675ef428899fa80544 [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>
55#include <common/compat.h>
56#include <common/config.h>
57#include <common/defaults.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010058#include <common/errors.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020059#include <common/memory.h>
60#include <common/mini-clist.h>
61#include <common/regex.h>
62#include <common/standard.h>
63#include <common/time.h>
64#include <common/uri_auth.h>
65#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066
67#include <types/capture.h>
68#include <types/global.h>
Simon Hormanac821422011-07-15 13:14:09 +090069#include <types/proto_tcp.h>
70#include <types/acl.h>
Willy Tarreau3c63fd82011-09-07 18:00:47 +020071#include <types/peers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020072
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +010073#include <proto/auth.h>
Willy Tarreau0fc45a72007-06-17 00:36:03 +020074#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020075#include <proto/backend.h>
76#include <proto/buffers.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020077#include <proto/checks.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020078#include <proto/fd.h>
Willy Tarreau34eb6712011-10-24 18:15:04 +020079#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020080#include <proto/log.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020081#include <proto/protocols.h>
Willy Tarreau80587432006-12-24 17:47:20 +010082#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020083#include <proto/proxy.h>
84#include <proto/queue.h>
85#include <proto/server.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020086#include <proto/session.h>
Willy Tarreau29857942009-05-10 09:01:21 +020087#include <proto/signal.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020088#include <proto/stream_sock.h>
89#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
Willy Tarreaubaaee002006-06-26 02:48:02 +020095/*********************************************************************/
96
97/*********************************************************************/
98
Willy Tarreau477ecd82010-01-03 21:12:30 +010099/* list of config files */
100static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100102int relative_pid = 1; /* process id starting at 1 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103
104/* global options */
105struct global global = {
106 logfac1 : -1,
107 logfac2 : -1,
108 loglev1 : 7, /* max syslog level : debug */
109 loglev2 : 7,
Willy Tarreau03f6d672007-10-18 15:15:57 +0200110 .stats_sock = {
Willy Tarreau03f6d672007-10-18 15:15:57 +0200111 .perm = {
112 .ux = {
113 .uid = -1,
114 .gid = -1,
115 .mode = 0,
116 }
117 }
Willy Tarreau27a674e2009-08-17 07:23:33 +0200118 },
Emeric Bruned760922010-10-22 17:59:25 +0200119 .unix_bind = {
120 .ux = {
121 .uid = -1,
122 .gid = -1,
123 .mode = 0,
124 }
125 },
Willy Tarreau27a674e2009-08-17 07:23:33 +0200126 .tune = {
127 .bufsize = BUFSIZE,
128 .maxrewrite = MAXREWRITE,
Willy Tarreau43961d52010-10-04 20:39:20 +0200129 .chksize = BUFSIZE,
Willy Tarreau27a674e2009-08-17 07:23:33 +0200130 },
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131 /* others NULL OK */
132};
133
134/*********************************************************************/
135
136int stopping; /* non zero means stopping in progress */
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200137int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200138
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
Willy Tarreaubaaee002006-06-26 02:48:02 +0200144static int *oldpids = NULL;
145static int oldpids_sig; /* use USR1 or TERM */
146
147/* this is used to drain data, and as a temporary buffer for sprintf()... */
148char trash[BUFSIZE];
149
Willy Tarreau8096de92010-02-26 11:12:27 +0100150/* this buffer is always the same size as standard buffers and is used for
151 * swapping data inside a buffer.
152 */
153char *swap_buffer = NULL;
154
Willy Tarreaubb545b42010-08-25 12:58:59 +0200155int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200156const int zero = 0;
157const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200158const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100160char hostname[MAX_HOSTNAME_LEN];
Emeric Brun2b920a12010-09-23 18:30:22 +0200161char localpeer[MAX_HOSTNAME_LEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200162
Willy Tarreau08ceb102011-07-24 22:58:00 +0200163/* list of the temporarily limited listeners because of lack of resource */
164struct list global_listener_queue = LIST_HEAD_INIT(global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +0200165struct task *global_listener_queue_task;
166static struct task *manage_global_listener_queue(struct task *t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200167
168/*********************************************************************/
169/* general purpose functions ***************************************/
170/*********************************************************************/
171
172void display_version()
173{
174 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreauc9ebc442011-08-23 00:23:54 +0200175 printf("Copyright 2000-2011 Willy Tarreau <w@1wt.eu>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200176}
177
Willy Tarreau7b066db2007-12-02 11:28:59 +0100178void display_build_opts()
179{
180 printf("Build options :"
181#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100182 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100183#endif
184#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100185 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100186#endif
187#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100188 "\n CC = " BUILD_CC
189#endif
190#ifdef BUILD_CFLAGS
191 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100192#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100193#ifdef BUILD_OPTIONS
194 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100195#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200196 "\n\nDefault settings :"
197 "\n maxconn = %d, bufsize = %d, maxrewrite = %d, maxpollevents = %d"
198 "\n\n",
199 DEFAULT_MAXCONN, BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS);
Willy Tarreaube5b6852009-10-03 18:57:08 +0200200
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100201 printf("Encrypted password support via crypt(3): "
202#ifdef CONFIG_HAP_CRYPT
203 "yes"
204#else
205 "no"
206#endif
207 "\n");
208
209 putchar('\n');
210
Willy Tarreaube5b6852009-10-03 18:57:08 +0200211 list_pollers(stdout);
212 putchar('\n');
Willy Tarreau7b066db2007-12-02 11:28:59 +0100213}
214
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215/*
216 * This function prints the command line usage and exits
217 */
218void usage(char *name)
219{
220 display_version();
221 fprintf(stderr,
Willy Tarreau5d01a632009-06-22 16:02:30 +0200222 "Usage : %s [-f <cfgfile>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200223 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200224 " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100225 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200226 " -d enters debug mode ; -db only disables background mode.\n"
227 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200228 " -D goes daemon ; -C changes to <dir> before loading files.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200229 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200230 " -c check mode : only check config files and exit\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231 " -n sets the maximum total # of connections (%d)\n"
232 " -m limits the usable amount of memory (in MB)\n"
233 " -N sets the default, per-proxy maximum # of connections (%d)\n"
Emeric Brun2b920a12010-09-23 18:30:22 +0200234 " -L set local peer name (default to hostname)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200235 " -p writes pids of all children to this file\n"
236#if defined(ENABLE_EPOLL)
237 " -de disables epoll() usage even when available\n"
238#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200239#if defined(ENABLE_SEPOLL)
240 " -ds disables speculative epoll() usage even when available\n"
241#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200242#if defined(ENABLE_KQUEUE)
243 " -dk disables kqueue() usage even when available\n"
244#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245#if defined(ENABLE_POLL)
246 " -dp disables poll() usage even when available\n"
247#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200248#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100249 " -dS disables splice usage (broken on old kernels)\n"
250#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251 " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
252 "\n",
253 name, DEFAULT_MAXCONN, cfg_maxpconn);
254 exit(1);
255}
256
257
258
259/*********************************************************************/
260/* more specific functions ***************************************/
261/*********************************************************************/
262
263/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200264 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
265 * a signal zero to all subscribers. This means that it's as easy as
266 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200267 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200268void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269{
270 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200271 signal_unregister_handler(sh);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200272 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273}
274
275/*
276 * upon SIGTTOU, we pause everything
277 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200278void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200279{
280 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200281 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200282}
283
284/*
285 * upon SIGTTIN, let's have a soft stop.
286 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200287void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200288{
Willy Tarreaube58c382011-07-24 18:28:10 +0200289 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200290}
291
292/*
293 * this function dumps every server's state when the process receives SIGHUP.
294 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200295void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200296{
297 struct proxy *p = proxy;
298
299 Warning("SIGHUP received, dumping servers states.\n");
300 while (p) {
301 struct server *s = p->srv;
302
303 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
304 while (s) {
305 snprintf(trash, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +0200306 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307 p->id, s->id,
308 (s->state & SRV_RUNNING) ? "UP" : "DOWN",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200309 s->cur_sess, s->nbpend, s->counters.cum_sess);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200310 Warning("%s\n", trash);
311 send_log(p, LOG_NOTICE, "%s\n", trash);
312 s = s->next;
313 }
314
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200315 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
316 if (!p->srv) {
317 snprintf(trash, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +0200318 "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 +0200319 p->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100320 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 +0200321 } else if (p->srv_act == 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200322 snprintf(trash, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +0200323 "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 +0200324 p->id,
325 (p->srv_bck) ? "is running on backup servers" : "has no server available",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100326 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 +0200327 } else {
328 snprintf(trash, sizeof(trash),
329 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
Willy Tarreau3b88d442009-04-11 20:44:08 +0200330 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200331 p->id, p->srv_act, p->srv_bck,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100332 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 +0200333 }
334 Warning("%s\n", trash);
335 send_log(p, LOG_NOTICE, "%s\n", trash);
336
337 p = p->next;
338 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339}
340
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200341void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200342{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200343 /* dump memory usage then free everything possible */
344 dump_pools();
345 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200346}
347
Willy Tarreaubaaee002006-06-26 02:48:02 +0200348/*
349 * This function initializes all the necessary variables. It only returns
350 * if everything is OK. If something fails, it exits.
351 */
352void init(int argc, char **argv)
353{
354 int i;
355 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356 char *tmp;
357 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +0200358 int err_code = 0;
Willy Tarreau477ecd82010-01-03 21:12:30 +0100359 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +0000360 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +0200361 char *change_dir = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362
Emeric Brun2b920a12010-09-23 18:30:22 +0200363 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
364 * the string in case of truncation, and at least FreeBSD appears not to do
365 * it.
366 */
367 memset(hostname, 0, sizeof(hostname));
368 gethostname(hostname, sizeof(hostname) - 1);
369 memset(localpeer, 0, sizeof(localpeer));
370 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
371
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372 /*
373 * Initialize the previously static variables.
374 */
375
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100376 totalconn = actconn = maxfd = listeners = stopping = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200377
378
379#ifdef HAPROXY_MEMMAX
380 global.rlimit_memmax = HAPROXY_MEMMAX;
381#endif
382
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200383 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200384 start_date = now;
385
Willy Tarreau29857942009-05-10 09:01:21 +0200386 signal_init();
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200387 init_task();
388 init_session();
Willy Tarreau8280d642009-09-23 23:37:52 +0200389 /* warning, we init buffers later */
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200390 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +0100391 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392
Willy Tarreau43b78992009-01-25 15:42:27 +0100393 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200394#if defined(ENABLE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +0100395 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200396#endif
397#if defined(ENABLE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +0100398 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200399#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200400#if defined(ENABLE_SEPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +0100401 global.tune.options |= GTUNE_USE_SEPOLL;
Willy Tarreaude99e992007-04-16 00:53:59 +0200402#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200403#if defined(ENABLE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +0100404 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200405#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200406#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100407 global.tune.options |= GTUNE_USE_SPLICE;
408#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409
410 pid = getpid();
411 progname = *argv;
412 while ((tmp = strchr(progname, '/')) != NULL)
413 progname = tmp + 1;
414
Kevinm48936af2010-12-22 16:08:21 +0000415 /* the process name is used for the logs only */
416 global.log_tag = strdup(progname);
417
Willy Tarreaubaaee002006-06-26 02:48:02 +0200418 argc--; argv++;
419 while (argc > 0) {
420 char *flag;
421
422 if (**argv == '-') {
423 flag = *argv+1;
424
425 /* 1 arg */
426 if (*flag == 'v') {
427 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +0100428 if (flag[1] == 'v') /* -vv */
429 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200430 exit(0);
431 }
432#if defined(ENABLE_EPOLL)
433 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +0100434 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200436#if defined(ENABLE_SEPOLL)
437 else if (*flag == 'd' && flag[1] == 's')
Willy Tarreau43b78992009-01-25 15:42:27 +0100438 global.tune.options &= ~GTUNE_USE_SEPOLL;
Willy Tarreaude99e992007-04-16 00:53:59 +0200439#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440#if defined(ENABLE_POLL)
441 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +0100442 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200443#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200444#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200445 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +0100446 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200447#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200448#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100449 else if (*flag == 'd' && flag[1] == 'S')
450 global.tune.options &= ~GTUNE_USE_SPLICE;
451#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200452 else if (*flag == 'V')
453 arg_mode |= MODE_VERBOSE;
454 else if (*flag == 'd' && flag[1] == 'b')
455 arg_mode |= MODE_FOREGROUND;
456 else if (*flag == 'd')
457 arg_mode |= MODE_DEBUG;
458 else if (*flag == 'c')
459 arg_mode |= MODE_CHECK;
460 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +0200461 arg_mode |= MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200462 else if (*flag == 'q')
463 arg_mode |= MODE_QUIET;
464 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
465 /* list of pids to finish ('f') or terminate ('t') */
466
467 if (flag[1] == 'f')
468 oldpids_sig = SIGUSR1; /* finish then exit */
469 else
470 oldpids_sig = SIGTERM; /* terminate immediately */
471 argv++; argc--;
472
473 if (argc > 0) {
474 oldpids = calloc(argc, sizeof(int));
475 while (argc > 0) {
476 oldpids[nb_oldpids] = atol(*argv);
477 if (oldpids[nb_oldpids] <= 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +0200478 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200479 argc--; argv++;
480 nb_oldpids++;
481 }
482 }
483 }
484 else { /* >=2 args */
485 argv++; argc--;
486 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +0200487 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200488
489 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +0200490 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200491 case 'n' : cfg_maxconn = atol(*argv); break;
492 case 'm' : global.rlimit_memmax = atol(*argv); break;
493 case 'N' : cfg_maxpconn = atol(*argv); break;
Emeric Brun2b920a12010-09-23 18:30:22 +0200494 case 'L' : strncpy(localpeer, *argv, sizeof(localpeer) - 1); break;
Willy Tarreau5d01a632009-06-22 16:02:30 +0200495 case 'f' :
Willy Tarreau477ecd82010-01-03 21:12:30 +0100496 wl = (struct wordlist *)calloc(1, sizeof(*wl));
497 if (!wl) {
498 Alert("Cannot load configuration file %s : out of memory.\n", *argv);
Willy Tarreau5d01a632009-06-22 16:02:30 +0200499 exit(1);
500 }
Willy Tarreau477ecd82010-01-03 21:12:30 +0100501 wl->s = *argv;
502 LIST_ADDQ(&cfg_cfgfiles, &wl->list);
Willy Tarreau5d01a632009-06-22 16:02:30 +0200503 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200504 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +0200505 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200506 }
507 }
508 }
509 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +0200510 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200511 argv++; argc--;
512 }
513
514 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
515 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
516 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
517
Willy Tarreau477ecd82010-01-03 21:12:30 +0100518 if (LIST_ISEMPTY(&cfg_cfgfiles))
Willy Tarreau3bafcdc2011-09-10 19:20:23 +0200519 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200520
Willy Tarreau576132e2011-09-10 19:26:56 +0200521 if (change_dir && chdir(change_dir) < 0) {
522 Alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
523 exit(1);
524 }
525
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526 have_appsession = 0;
527 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200528
529 init_default_instance();
530
Willy Tarreau477ecd82010-01-03 21:12:30 +0100531 list_for_each_entry(wl, &cfg_cfgfiles, list) {
Willy Tarreauc4382422009-12-06 13:10:44 +0100532 int ret;
533
Willy Tarreau477ecd82010-01-03 21:12:30 +0100534 ret = readcfgfile(wl->s);
Willy Tarreauc4382422009-12-06 13:10:44 +0100535 if (ret == -1) {
536 Alert("Could not open configuration file %s : %s\n",
Willy Tarreau477ecd82010-01-03 21:12:30 +0100537 wl->s, strerror(errno));
Willy Tarreauc4382422009-12-06 13:10:44 +0100538 exit(1);
539 }
Willy Tarreau25a67fa2009-12-15 21:46:25 +0100540 if (ret & (ERR_ABORT|ERR_FATAL))
Willy Tarreau477ecd82010-01-03 21:12:30 +0100541 Alert("Error(s) found in configuration file : %s\n", wl->s);
Willy Tarreau25a67fa2009-12-15 21:46:25 +0100542 err_code |= ret;
Willy Tarreau058e9072009-07-20 09:30:05 +0200543 if (err_code & ERR_ABORT)
Willy Tarreau5d01a632009-06-22 16:02:30 +0200544 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200546
Willy Tarreaubb925012009-07-23 13:36:36 +0200547 err_code |= check_config_validity();
548 if (err_code & (ERR_ABORT|ERR_FATAL)) {
549 Alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200550 exit(1);
551 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552
553 if (global.mode & MODE_CHECK) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200554 qfprintf(stdout, "Configuration file is valid\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 exit(0);
556 }
557
Willy Tarreaue9b26022011-08-01 20:57:55 +0200558 global_listener_queue_task = task_new();
559 if (!global_listener_queue_task) {
560 Alert("Out of memory when initializing global task\n");
561 exit(1);
562 }
563 /* very simple initialization, users will queue the task if needed */
564 global_listener_queue_task->context = NULL; /* not even a context! */
565 global_listener_queue_task->process = manage_global_listener_queue;
566 global_listener_queue_task->expire = TICK_ETERNITY;
567
Willy Tarreau8280d642009-09-23 23:37:52 +0200568 /* now we know the buffer size, we can initialize the buffers */
569 init_buffer();
570
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200571 if (have_appsession)
572 appsession_init();
573
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200574 if (start_checks() < 0)
575 exit(1);
576
Willy Tarreaubaaee002006-06-26 02:48:02 +0200577 if (cfg_maxconn > 0)
578 global.maxconn = cfg_maxconn;
579
580 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +0200581 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200582 global.pidfile = strdup(cfg_pidfile);
583 }
584
585 if (global.maxconn == 0)
586 global.maxconn = DEFAULT_MAXCONN;
587
Willy Tarreau66aa61f2009-01-18 21:44:07 +0100588 if (!global.maxpipes) {
589 /* maxpipes not specified. Count how many frontends and backends
590 * may be using splicing, and bound that to maxconn.
591 */
592 struct proxy *cur;
593 int nbfe = 0, nbbe = 0;
594
595 for (cur = proxy; cur; cur = cur->next) {
596 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
597 if (cur->cap & PR_CAP_FE)
598 nbfe += cur->maxconn;
599 if (cur->cap & PR_CAP_BE)
Willy Tarreauafb48762009-01-25 10:42:05 +0100600 nbbe += cur->fullconn ? cur->fullconn : global.maxconn;
Willy Tarreau66aa61f2009-01-18 21:44:07 +0100601 }
602 }
603 global.maxpipes = MAX(nbfe, nbbe);
604 if (global.maxpipes > global.maxconn)
605 global.maxpipes = global.maxconn;
Willy Tarreau686ac822009-01-25 14:06:58 +0100606 global.maxpipes /= 4;
Willy Tarreau66aa61f2009-01-18 21:44:07 +0100607 }
608
609
Willy Tarreauabacc2c2011-09-07 14:26:33 +0200610 global.hardmaxconn = global.maxconn; /* keep this max value */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200611 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
Willy Tarreau3ec79b92009-01-18 20:39:42 +0100612 global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200613
Willy Tarreau3c63fd82011-09-07 18:00:47 +0200614 if (global.stats_fe)
615 global.maxsock += global.stats_fe->maxconn;
616
617 if (peers) {
618 /* peers also need to bypass global maxconn */
619 struct peers *p = peers;
620
621 for (p = peers; p; p = p->next)
622 if (p->peers_fe)
623 global.maxsock += p->peers_fe->maxconn;
624 }
625
Willy Tarreau1db37712007-06-03 17:16:49 +0200626 if (global.tune.maxpollevents <= 0)
627 global.tune.maxpollevents = MAX_POLL_EVENTS;
628
Willy Tarreauf49d1df2009-03-01 08:35:41 +0100629 if (global.tune.maxaccept == 0) {
Willy Tarreau4827fd22011-07-22 21:59:59 +0200630 /* Note: we should not try to accept too many connections at once,
631 * because past one point we're significantly reducing the cache
632 * efficiency and the highest session rate significantly drops.
633 * Values between 15 and 35 seem fine on a Core i5 with 4M L3 cache.
634 */
Willy Tarreaua0250ba2008-01-06 11:22:57 +0100635 if (global.nbproc > 1)
636 global.tune.maxaccept = 8; /* leave some conns to other processes */
637 else
Willy Tarreau4827fd22011-07-22 21:59:59 +0200638 global.tune.maxaccept = 32; /* accept more incoming conns at once */
Willy Tarreaua0250ba2008-01-06 11:22:57 +0100639 }
640
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100641 if (global.tune.recv_enough == 0)
642 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
643
Willy Tarreau27a674e2009-08-17 07:23:33 +0200644 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
645 global.tune.maxrewrite = global.tune.bufsize / 2;
646
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
648 /* command line debug mode inhibits configuration mode */
649 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
650 }
651 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
Willy Tarreaufbee7132007-10-18 13:53:22 +0200652 MODE_VERBOSE | MODE_DEBUG ));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200653
654 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
655 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
656 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
657 }
658
659 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
660 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
661 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
662 global.nbproc = 1;
663 }
664
665 if (global.nbproc < 1)
666 global.nbproc = 1;
667
Willy Tarreau8096de92010-02-26 11:12:27 +0100668 swap_buffer = (char *)calloc(1, global.tune.bufsize);
669
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200670 fdinfo = (struct fdinfo *)calloc(1,
671 sizeof(struct fdinfo) * (global.maxsock));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200672 fdtab = (struct fdtab *)calloc(1,
673 sizeof(struct fdtab) * (global.maxsock));
674 for (i = 0; i < global.maxsock; i++) {
675 fdtab[i].state = FD_STCLOSE;
676 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200677
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200678 /*
679 * Note: we could register external pollers here.
680 * Built-in pollers have been registered before main().
681 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200682
Willy Tarreau43b78992009-01-25 15:42:27 +0100683 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200684 disable_poller("kqueue");
685
Willy Tarreau43b78992009-01-25 15:42:27 +0100686 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200687 disable_poller("epoll");
688
Willy Tarreau43b78992009-01-25 15:42:27 +0100689 if (!(global.tune.options & GTUNE_USE_SEPOLL))
Willy Tarreaude99e992007-04-16 00:53:59 +0200690 disable_poller("sepoll");
691
Willy Tarreau43b78992009-01-25 15:42:27 +0100692 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200693 disable_poller("poll");
694
Willy Tarreau43b78992009-01-25 15:42:27 +0100695 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200696 disable_poller("select");
697
698 /* Note: we could disable any poller by name here */
699
Willy Tarreau2ff76222007-04-09 19:29:56 +0200700 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
701 list_pollers(stderr);
702
Willy Tarreau4f60f162007-04-08 16:39:58 +0200703 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200704 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200705 exit(1);
706 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200707 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
708 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200709 }
710
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200711 if (!global.node)
712 global.node = strdup(hostname);
713
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714}
715
Simon Horman6fb82592011-07-15 13:14:11 +0900716static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +0900717{
Simon Hormanac821422011-07-15 13:14:09 +0900718 struct acl_term_suite *suite, *suiteb;
719 struct acl_term *term, *termb;
720
Simon Horman6fb82592011-07-15 13:14:11 +0900721 if (!cond)
722 return;
723
724 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
725 list_for_each_entry_safe(term, termb, &suite->terms, list) {
726 LIST_DEL(&term->list);
727 free(term);
Simon Hormanac821422011-07-15 13:14:09 +0900728 }
Simon Horman6fb82592011-07-15 13:14:11 +0900729 LIST_DEL(&suite->list);
730 free(suite);
731 }
732
733 free(cond);
734}
735
736static void deinit_tcp_rules(struct list *rules)
737{
738 struct tcp_rule *trule, *truleb;
739
740 list_for_each_entry_safe(trule, truleb, rules, list) {
Simon Hormanac821422011-07-15 13:14:09 +0900741 LIST_DEL(&trule->list);
Simon Horman6fb82592011-07-15 13:14:11 +0900742 deinit_acl_cond(trule->cond);
Simon Hormanac821422011-07-15 13:14:09 +0900743 free(trule);
744 }
745}
746
Simon Horman6fb82592011-07-15 13:14:11 +0900747static void deinit_pattern_arg(struct pattern_arg *p, int i)
748{
749 if (!p)
750 return;
751
752 while (i--)
753 if (p[i].type == PATTERN_ARG_TYPE_STRING)
754 free(p[i].data.str.str);
755
756 free(p);
757}
758
759static void deinit_stick_rules(struct list *rules)
760{
761 struct sticking_rule *rule, *ruleb;
762
763 list_for_each_entry_safe(rule, ruleb, rules, list) {
764 LIST_DEL(&rule->list);
765 deinit_acl_cond(rule->cond);
766 if (rule->expr) {
767 struct pattern_conv_expr *conv_expr, *conv_exprb;
768 list_for_each_entry_safe(conv_expr, conv_exprb, &rule->expr->conv_exprs, list)
769 deinit_pattern_arg(conv_expr->arg_p, conv_expr->arg_i);
770 deinit_pattern_arg(rule->expr->arg_p, rule->expr->arg_i);
771 free(rule->expr);
772 }
773 free(rule);
774 }
775}
776
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777void deinit(void)
778{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200779 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200780 struct cap_hdr *h,*h_next;
781 struct server *s,*s_next;
782 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200783 struct acl_cond *cond, *condb;
784 struct hdr_exp *exp, *expb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200785 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200786 struct switching_rule *rule, *ruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +0200787 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +0100788 struct wordlist *wl, *wlb;
Willy Tarreauf4f04122010-01-28 18:10:50 +0100789 struct cond_wordlist *cwl, *cwlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200790 struct uri_auth *uap, *ua = NULL;
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200791 int i;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200792
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200793 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794 while (p) {
Willy Tarreaua534fea2008-08-03 12:19:50 +0200795 free(p->id);
796 free(p->check_req);
797 free(p->cookie_name);
798 free(p->cookie_domain);
799 free(p->url_param_name);
800 free(p->capture_name);
801 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +0900802 free(p->rdp_cookie_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803
Willy Tarreaua534fea2008-08-03 12:19:50 +0200804 for (i = 0; i < HTTP_ERR_SIZE; i++)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200805 chunk_destroy(&p->errmsg[i]);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200806
Willy Tarreauf4f04122010-01-28 18:10:50 +0100807 list_for_each_entry_safe(cwl, cwlb, &p->req_add, list) {
808 LIST_DEL(&cwl->list);
809 free(cwl->s);
810 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +0100811 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812
Willy Tarreauf4f04122010-01-28 18:10:50 +0100813 list_for_each_entry_safe(cwl, cwlb, &p->rsp_add, list) {
814 LIST_DEL(&cwl->list);
815 free(cwl->s);
816 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +0100817 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200818
819 list_for_each_entry_safe(cond, condb, &p->block_cond, list) {
820 LIST_DEL(&cond->list);
821 prune_acl_cond(cond);
822 free(cond);
823 }
824
Willy Tarreaub80c2302007-11-30 20:51:32 +0100825 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
826 LIST_DEL(&cond->list);
827 prune_acl_cond(cond);
828 free(cond);
829 }
830
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200831 for (exp = p->req_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200832 if (exp->preg) {
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200833 regfree((regex_t *)exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200834 free((regex_t *)exp->preg);
835 }
836
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200837 if (exp->replace && exp->action != ACT_SETBE)
838 free((char *)exp->replace);
839 expb = exp;
840 exp = exp->next;
841 free(expb);
842 }
843
844 for (exp = p->rsp_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200845 if (exp->preg) {
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200846 regfree((regex_t *)exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200847 free((regex_t *)exp->preg);
848 }
849
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200850 if (exp->replace && exp->action != ACT_SETBE)
851 free((char *)exp->replace);
852 expb = exp;
853 exp = exp->next;
854 free(expb);
855 }
856
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200857 /* build a list of unique uri_auths */
858 if (!ua)
859 ua = p->uri_auth;
860 else {
861 /* check if p->uri_auth is unique */
862 for (uap = ua; uap; uap=uap->next)
863 if (uap == p->uri_auth)
864 break;
865
Willy Tarreauaccc4e12008-06-24 11:14:45 +0200866 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200867 /* add it, if it is */
868 p->uri_auth->next = ua;
869 ua = p->uri_auth;
870 }
871 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200872
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200873 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
874 LIST_DEL(&acl->list);
875 prune_acl(acl);
876 free(acl);
877 }
878
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200879 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
880 LIST_DEL(&rule->list);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200881 prune_acl_cond(rule->cond);
882 free(rule->cond);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200883 free(rule);
884 }
885
Willy Tarreaub463dfb2008-06-07 23:08:56 +0200886 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
887 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +0100888 if (rdr->cond) {
889 prune_acl_cond(rdr->cond);
890 free(rdr->cond);
891 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +0200892 free(rdr->rdr_str);
893 free(rdr);
894 }
895
Simon Hormanac821422011-07-15 13:14:09 +0900896 deinit_tcp_rules(&p->tcp_req.inspect_rules);
897 deinit_tcp_rules(&p->tcp_req.l4_rules);
898
Simon Horman6fb82592011-07-15 13:14:11 +0900899 deinit_stick_rules(&p->storersp_rules);
900 deinit_stick_rules(&p->sticking_rules);
901
Willy Tarreaua534fea2008-08-03 12:19:50 +0200902 free(p->appsession_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200903
904 h = p->req_cap;
905 while (h) {
906 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +0200907 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200908 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200909 free(h);
910 h = h_next;
911 }/* end while(h) */
912
913 h = p->rsp_cap;
914 while (h) {
915 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +0200916 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200917 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918 free(h);
919 h = h_next;
920 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200921
Willy Tarreaubaaee002006-06-26 02:48:02 +0200922 s = p->srv;
923 while (s) {
924 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200925
926 if (s->check) {
927 task_delete(s->check);
928 task_free(s->check);
929 }
930
Willy Tarreau2e993902011-10-31 11:53:20 +0100931 if (s->warmup) {
932 task_delete(s->warmup);
933 task_free(s->warmup);
934 }
935
Willy Tarreaua534fea2008-08-03 12:19:50 +0200936 free(s->id);
937 free(s->cookie);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000938 free(s->check_data);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200939 free(s);
940 s = s_next;
941 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200942
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943 l = p->listen;
944 while (l) {
945 l_next = l->next;
Willy Tarreauf6e2cc72010-09-03 10:38:17 +0200946 unbind_listener(l);
947 delete_listener(l);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +0100948 free(l->name);
949 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950 free(l);
951 l = l_next;
952 }/* end while(l) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200953
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +0100954 free(p->desc);
955 free(p->fwdfor_hdr_name);
956
Willy Tarreauff011f22011-01-06 17:51:27 +0100957 free_http_req_rules(&p->http_req_rules);
Willy Tarreau918ff602011-07-25 16:33:49 +0200958 free(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +0100959
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200960 pool_destroy2(p->req_cap_pool);
961 pool_destroy2(p->rsp_cap_pool);
Simon Hormanb08584a2011-07-15 13:14:10 +0900962 pool_destroy2(p->table.pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100963
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200964 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200965 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200966 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200967 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +0200968
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200969 while (ua) {
970 uap = ua;
971 ua = ua->next;
972
Willy Tarreaua534fea2008-08-03 12:19:50 +0200973 free(uap->uri_prefix);
974 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200975 free(uap->node);
976 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200977
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +0100978 userlist_free(uap->userlist);
Willy Tarreauff011f22011-01-06 17:51:27 +0100979 free_http_req_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +0100980
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200981 free(uap);
982 }
983
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100984 userlist_free(userlist);
985
Willy Tarreaudd815982007-10-16 12:25:14 +0200986 protocol_unbind_all();
987
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100988 free(global.log_send_hostname); global.log_send_hostname = NULL;
Kevinm48936af2010-12-22 16:08:21 +0000989 free(global.log_tag); global.log_tag = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +0200990 free(global.chroot); global.chroot = NULL;
991 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200992 free(global.node); global.node = NULL;
993 free(global.desc); global.desc = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +0200994 free(fdtab); fdtab = NULL;
995 free(oldpids); oldpids = NULL;
Willy Tarreaue9b26022011-08-01 20:57:55 +0200996 free(global_listener_queue_task); global_listener_queue_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200997
Willy Tarreau477ecd82010-01-03 21:12:30 +0100998 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
999 LIST_DEL(&wl->list);
1000 free(wl);
1001 }
1002
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001003 pool_destroy2(pool2_session);
Willy Tarreau7341d942007-05-13 19:56:02 +02001004 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001005 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001006 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +02001007 pool_destroy2(pool2_capture);
Willy Tarreau63963c62007-05-13 21:29:55 +02001008 pool_destroy2(pool2_appsess);
Willy Tarreaue4d7e552007-05-13 20:19:55 +02001009 pool_destroy2(pool2_pendconn);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02001010 pool_destroy2(pool2_sig_handlers);
Willy Tarreau34eb6712011-10-24 18:15:04 +02001011 pool_destroy2(pool2_hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001012
1013 if (have_appsession) {
Willy Tarreau63963c62007-05-13 21:29:55 +02001014 pool_destroy2(apools.serverid);
1015 pool_destroy2(apools.sessid);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001016 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001017
1018 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001019} /* end deinit() */
1020
Willy Tarreaubb545b42010-08-25 12:58:59 +02001021/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
1022 * pids the signal was correctly delivered to.
1023 */
1024static int tell_old_pids(int sig)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001025{
1026 int p;
Willy Tarreaubb545b42010-08-25 12:58:59 +02001027 int ret = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001028 for (p = 0; p < nb_oldpids; p++)
Willy Tarreaubb545b42010-08-25 12:58:59 +02001029 if (kill(oldpids[p], sig) == 0)
1030 ret++;
1031 return ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001032}
1033
Willy Tarreau918ff602011-07-25 16:33:49 +02001034/* Runs the polling loop */
Willy Tarreau4f60f162007-04-08 16:39:58 +02001035void run_poll_loop()
1036{
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001037 int next;
Willy Tarreau4f60f162007-04-08 16:39:58 +02001038
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001039 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001040 while (1) {
Willy Tarreau29857942009-05-10 09:01:21 +02001041 /* check if we caught some signals and process them */
1042 signal_process_queue();
1043
Willy Tarreau58b458d2008-06-29 22:40:23 +02001044 /* Check if we can expire some tasks */
1045 wake_expired_tasks(&next);
1046
1047 /* Process a few tasks */
Willy Tarreaud825eef2007-05-12 22:35:00 +02001048 process_runnable_tasks(&next);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001049
Willy Tarreauaf7ad002010-08-31 15:39:26 +02001050 /* stop when there's nothing left to do */
1051 if (jobs == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +02001052 break;
1053
Willy Tarreau58b458d2008-06-29 22:40:23 +02001054 /* The poller will ensure it returns around <next> */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001055 cur_poller.poll(&cur_poller, next);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001056 }
1057}
1058
Willy Tarreaue9b26022011-08-01 20:57:55 +02001059/* This is the global management task for listeners. It enables listeners waiting
1060 * for global resources when there are enough free resource, or at least once in
1061 * a while. It is designed to be called as a task.
1062 */
1063static struct task *manage_global_listener_queue(struct task *t)
1064{
1065 int next = TICK_ETERNITY;
Willy Tarreaue9b26022011-08-01 20:57:55 +02001066 /* queue is empty, nothing to do */
1067 if (LIST_ISEMPTY(&global_listener_queue))
1068 goto out;
1069
1070 /* If there are still too many concurrent connections, let's wait for
1071 * some of them to go away. We don't need to re-arm the timer because
1072 * each of them will scan the queue anyway.
1073 */
1074 if (unlikely(actconn >= global.maxconn))
1075 goto out;
1076
1077 /* We should periodically try to enable listeners waiting for a global
1078 * resource here, because it is possible, though very unlikely, that
1079 * they have been blocked by a temporary lack of global resource such
1080 * as a file descriptor or memory and that the temporary condition has
1081 * disappeared.
1082 */
Willy Tarreauabacc2c2011-09-07 14:26:33 +02001083 dequeue_all_listeners(&global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +02001084
1085 out:
1086 t->expire = next;
1087 task_queue(t);
1088 return t;
1089}
Willy Tarreau4f60f162007-04-08 16:39:58 +02001090
Willy Tarreaubaaee002006-06-26 02:48:02 +02001091int main(int argc, char **argv)
1092{
1093 int err, retry;
1094 struct rlimit limit;
1095 FILE *pidfile = NULL;
Emeric Bruncf20bf12010-10-22 16:06:11 +02001096 char errmsg[100];
Willy Tarreaubaaee002006-06-26 02:48:02 +02001097
Emeric Bruncf20bf12010-10-22 16:06:11 +02001098 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02001099 signal_register_fct(SIGQUIT, dump, SIGQUIT);
1100 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
1101 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001102
Willy Tarreaue437c442010-03-17 18:02:46 +01001103 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
1104 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
1105 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001106 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02001107 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001108
Willy Tarreaudc23a922011-02-16 11:10:36 +01001109 /* ulimits */
1110 if (!global.rlimit_nofile)
1111 global.rlimit_nofile = global.maxsock;
1112
1113 if (global.rlimit_nofile) {
1114 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
1115 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
1116 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
1117 }
1118 }
1119
1120 if (global.rlimit_memmax) {
1121 limit.rlim_cur = limit.rlim_max =
1122 global.rlimit_memmax * 1048576 / global.nbproc;
1123#ifdef RLIMIT_AS
1124 if (setrlimit(RLIMIT_AS, &limit) == -1) {
1125 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
1126 argv[0], global.rlimit_memmax);
1127 }
1128#else
1129 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
1130 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
1131 argv[0], global.rlimit_memmax);
1132 }
1133#endif
1134 }
1135
Willy Tarreaubaaee002006-06-26 02:48:02 +02001136 /* We will loop at most 100 times with 10 ms delay each time.
1137 * That's at most 1 second. We only send a signal to old pids
1138 * if we cannot grab at least one port.
1139 */
1140 retry = MAX_START_RETRIES;
1141 err = ERR_NONE;
1142 while (retry >= 0) {
1143 struct timeval w;
1144 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01001145 /* exit the loop on no error or fatal error */
1146 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001147 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02001148 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001149 break;
1150
1151 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
1152 * listening sockets. So on those platforms, it would be wiser to
1153 * simply send SIGUSR1, which will not be undoable.
1154 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02001155 if (tell_old_pids(SIGTTOU) == 0) {
1156 /* no need to wait if we can't contact old pids */
1157 retry = 0;
1158 continue;
1159 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001160 /* give some time to old processes to stop listening */
1161 w.tv_sec = 0;
1162 w.tv_usec = 10*1000;
1163 select(0, NULL, NULL, NULL, &w);
1164 retry--;
1165 }
1166
1167 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01001168 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02001169 if (retry != MAX_START_RETRIES && nb_oldpids) {
1170 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001171 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02001172 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001173 exit(1);
1174 }
1175
1176 if (listeners == 0) {
1177 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
1178 /* Note: we don't have to send anything to the old pids because we
1179 * never stopped them. */
1180 exit(1);
1181 }
1182
Emeric Bruncf20bf12010-10-22 16:06:11 +02001183 err = protocol_bind_all(errmsg, sizeof(errmsg));
1184 if ((err & ~ERR_WARN) != ERR_NONE) {
1185 if ((err & ERR_ALERT) || (err & ERR_WARN))
1186 Alert("[%s.main()] %s.\n", argv[0], errmsg);
1187
Willy Tarreaudd815982007-10-16 12:25:14 +02001188 Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
1189 protocol_unbind_all(); /* cleanup everything we can */
1190 if (nb_oldpids)
1191 tell_old_pids(SIGTTIN);
1192 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02001193 } else if (err & ERR_WARN) {
1194 Alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02001195 }
1196
Willy Tarreaubaaee002006-06-26 02:48:02 +02001197 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02001198 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
1199 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001200
Willy Tarreaubaaee002006-06-26 02:48:02 +02001201 /* MODE_QUIET can inhibit alerts and warnings below this line */
1202
1203 global.mode &= ~MODE_STARTING;
1204 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
1205 /* detach from the tty */
1206 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001207 }
1208
1209 /* open log & pid files before the chroot */
1210 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
1211 int pidfd;
1212 unlink(global.pidfile);
1213 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
1214 if (pidfd < 0) {
1215 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
1216 if (nb_oldpids)
1217 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02001218 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001219 exit(1);
1220 }
1221 pidfile = fdopen(pidfd, "w");
1222 }
1223
Willy Tarreaub38651a2007-03-24 17:24:39 +01001224#ifdef CONFIG_HAP_CTTPROXY
1225 if (global.last_checks & LSTCHK_CTTPROXY) {
1226 int ret;
1227
1228 ret = check_cttproxy_version();
1229 if (ret < 0) {
1230 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
1231 argv[0],
1232 (ret == -1) ? " Incorrect module version.\n"
1233 : " Make sure you have enough permissions and that the module is loaded.\n");
Willy Tarreaudd815982007-10-16 12:25:14 +02001234 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01001235 exit(1);
1236 }
1237 }
1238#endif
1239
1240 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
1241 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
Willy Tarreau4e30ed72009-02-04 18:02:48 +01001242 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02001243 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01001244 exit(1);
1245 }
1246
Willy Tarreau4e30ed72009-02-04 18:02:48 +01001247 /* If the user is not root, we'll still let him try the configuration
1248 * but we inform him that unexpected behaviour may occur.
1249 */
1250 if ((global.last_checks & LSTCHK_NETADM) && getuid())
1251 Warning("[%s.main()] Some options which require full privileges"
1252 " might not work well.\n"
1253 "", argv[0]);
1254
Willy Tarreauf223cc02007-10-15 18:57:08 +02001255 /* chroot if needed */
1256 if (global.chroot != NULL) {
1257 if (chroot(global.chroot) == -1) {
1258 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
1259 if (nb_oldpids)
1260 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02001261 protocol_unbind_all();
Willy Tarreauf223cc02007-10-15 18:57:08 +02001262 exit(1);
1263 }
1264 chdir("/");
1265 }
1266
Willy Tarreaubaaee002006-06-26 02:48:02 +02001267 if (nb_oldpids)
Willy Tarreaubb545b42010-08-25 12:58:59 +02001268 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001269
1270 /* Note that any error at this stage will be fatal because we will not
1271 * be able to restart the old pids.
1272 */
1273
1274 /* setgid / setuid */
1275 if (global.gid && setgid(global.gid) == -1) {
1276 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +02001277 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001278 exit(1);
1279 }
1280
1281 if (global.uid && setuid(global.uid) == -1) {
1282 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Willy Tarreaudd815982007-10-16 12:25:14 +02001283 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001284 exit(1);
1285 }
1286
1287 /* check ulimits */
1288 limit.rlim_cur = limit.rlim_max = 0;
1289 getrlimit(RLIMIT_NOFILE, &limit);
1290 if (limit.rlim_cur < global.maxsock) {
1291 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 +02001292 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001293 }
1294
1295 if (global.mode & MODE_DAEMON) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01001296 struct proxy *px;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001297 int ret = 0;
1298 int proc;
1299
1300 /* the father launches the required number of processes */
1301 for (proc = 0; proc < global.nbproc; proc++) {
1302 ret = fork();
1303 if (ret < 0) {
1304 Alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02001305 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +02001306 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001307 }
1308 else if (ret == 0) /* child breaks here */
1309 break;
1310 if (pidfile != NULL) {
1311 fprintf(pidfile, "%d\n", ret);
1312 fflush(pidfile);
1313 }
Willy Tarreaudcd47712007-11-04 23:35:08 +01001314 relative_pid++; /* each child will get a different one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001315 }
1316 /* close the pidfile both in children and father */
1317 if (pidfile != NULL)
1318 fclose(pidfile);
Willy Tarreaud137dd32010-08-25 12:49:05 +02001319
1320 /* We won't ever use this anymore */
1321 free(oldpids); oldpids = NULL;
1322 free(global.chroot); global.chroot = NULL;
1323 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001324
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01001325 /* we might have to unbind some proxies from some processes */
1326 px = proxy;
1327 while (px != NULL) {
1328 if (px->bind_proc && px->state != PR_STSTOPPED) {
1329 if (!(px->bind_proc & (1 << proc)))
1330 stop_proxy(px);
1331 }
1332 px = px->next;
1333 }
1334
Willy Tarreaubaaee002006-06-26 02:48:02 +02001335 if (proc == global.nbproc)
1336 exit(0); /* parent must leave */
1337
1338 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
1339 * that we can detach from the TTY. We MUST NOT do it in other cases since
1340 * it would have already be done, and 0-2 would have been affected to listening
1341 * sockets
1342 */
Willy Tarreau106cb762008-11-16 07:40:34 +01001343 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001344 /* detach from the tty */
1345 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreau106cb762008-11-16 07:40:34 +01001346 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001347 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
1348 }
1349 pid = getpid(); /* update child's pid */
1350 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02001351 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001352 }
1353
Willy Tarreaudd815982007-10-16 12:25:14 +02001354 protocol_enable_all();
Willy Tarreau4f60f162007-04-08 16:39:58 +02001355 /*
1356 * That's it : the central polling loop. Run until we stop.
1357 */
1358 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001359
1360 /* Free all Hash Keys and all Hash elements */
1361 appsession_cleanup();
1362 /* Do some cleanup */
1363 deinit();
1364
1365 exit(0);
1366}
1367
1368
1369/*
1370 * Local variables:
1371 * c-indent-level: 8
1372 * c-basic-offset: 8
1373 * End:
1374 */