blob: 1f40a128314c4b05ae03377f0542a6c1f629e13a [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HA-Proxy : High Availability-enabled HTTP/TCP proxy
Willy Tarreau7b677262017-04-03 09:27:49 +02003 * Copyright 2000-2017 Willy Tarreau <willy@haproxy.org>.
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 *
Lukas Tribus23953682017-04-28 13:24:30 +000010 * Please refer to RFC7230 - RFC7235 informations about HTTP protocol, and
11 * RFC6265 for informations about cookies usage. More generally, the IETF HTTP
Willy Tarreaubaaee002006-06-26 02:48:02 +020012 * 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
David Carlier7ece0962015-12-08 21:43:09 +000028#define _GNU_SOURCE
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <string.h>
33#include <ctype.h>
Maxime de Roucy379d9c72016-05-13 23:52:56 +020034#include <dirent.h>
Maxime de Roucy379d9c72016-05-13 23:52:56 +020035#include <sys/stat.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <sys/time.h>
37#include <sys/types.h>
38#include <sys/socket.h>
39#include <netinet/tcp.h>
40#include <netinet/in.h>
41#include <arpa/inet.h>
Olivier Houchardf73629d2017-04-05 22:33:04 +020042#include <net/if.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <netdb.h>
44#include <fcntl.h>
45#include <errno.h>
46#include <signal.h>
47#include <stdarg.h>
48#include <sys/resource.h>
Marc-Antoine Perennou992709b2013-02-12 10:53:52 +010049#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <time.h>
51#include <syslog.h>
Michael Schererab012dd2013-01-12 18:35:19 +010052#include <grp.h>
Willy Tarreaufc6c0322012-11-16 16:12:27 +010053#ifdef USE_CPU_AFFINITY
Willy Tarreaufc6c0322012-11-16 16:12:27 +010054#include <sched.h>
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +020055#ifdef __FreeBSD__
56#include <sys/param.h>
57#include <sys/cpuset.h>
58#endif
Willy Tarreaufc6c0322012-11-16 16:12:27 +010059#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020060
61#ifdef DEBUG_FULL
62#include <assert.h>
63#endif
64
Willy Tarreau2dd0d472006-06-29 17:53:05 +020065#include <common/base64.h>
66#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020067#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020068#include <common/compat.h>
69#include <common/config.h>
70#include <common/defaults.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010071#include <common/errors.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020072#include <common/memory.h>
73#include <common/mini-clist.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010074#include <common/namespace.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020075#include <common/regex.h>
76#include <common/standard.h>
77#include <common/time.h>
78#include <common/uri_auth.h>
79#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020080
81#include <types/capture.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020082#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020083#include <types/global.h>
Simon Hormanac821422011-07-15 13:14:09 +090084#include <types/acl.h>
Willy Tarreau3c63fd82011-09-07 18:00:47 +020085#include <types/peers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020086
Willy Tarreau0fc45a72007-06-17 00:36:03 +020087#include <proto/acl.h>
Willy Tarreau3c595ac2015-04-19 09:59:31 +020088#include <proto/applet.h>
Willy Tarreau2e845be2012-10-19 19:49:09 +020089#include <proto/arg.h>
Willy Tarreau3c595ac2015-04-19 09:59:31 +020090#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020091#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020092#include <proto/channel.h>
Willy Tarreauf2943dc2012-10-26 20:10:28 +020093#include <proto/connection.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020094#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020095#include <proto/filters.h>
Willy Tarreau34eb6712011-10-24 18:15:04 +020096#include <proto/hdr_idx.h>
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010097#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020098#include <proto/listener.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020099#include <proto/log.h>
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100100#include <proto/pattern.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +0200101#include <proto/protocol.h>
Willy Tarreau80587432006-12-24 17:47:20 +0100102#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103#include <proto/proxy.h>
104#include <proto/queue.h>
105#include <proto/server.h>
Willy Tarreaub1ec8c42015-04-03 13:53:24 +0200106#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +0200107#include <proto/stream.h>
Willy Tarreau29857942009-05-10 09:01:21 +0200108#include <proto/signal.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109#include <proto/task.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +0200110#include <proto/dns.h>
Christopher Fauletff2613e2016-11-09 11:36:17 +0100111#include <proto/vars.h>
Grant Zhang872f9c22017-01-21 01:10:18 +0000112#include <proto/ssl_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113
Willy Tarreau477ecd82010-01-03 21:12:30 +0100114/* list of config files */
115static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100117int relative_pid = 1; /* process id starting at 1 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118
119/* global options */
120struct global global = {
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100121 .hard_stop_after = TICK_ETERNITY,
Willy Tarreau247a13a2012-11-15 17:38:15 +0100122 .nbproc = 1,
William Lallemand5f232402012-04-05 18:02:55 +0200123 .req_count = 0,
William Lallemand0f99e342011-10-12 17:50:54 +0200124 .logsrvs = LIST_HEAD_INIT(global.logsrvs),
William Lallemand9d5f5482012-11-07 16:12:57 +0100125 .maxzlibmem = 0,
William Lallemandd85f9172012-11-09 17:05:39 +0100126 .comp_rate_lim = 0,
Emeric Brun850efd52014-01-29 12:24:34 +0100127 .ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED,
Emeric Bruned760922010-10-22 17:59:25 +0200128 .unix_bind = {
129 .ux = {
130 .uid = -1,
131 .gid = -1,
132 .mode = 0,
133 }
134 },
Willy Tarreau27a674e2009-08-17 07:23:33 +0200135 .tune = {
136 .bufsize = BUFSIZE,
Willy Tarreau27097842015-09-28 13:53:23 +0200137 .maxrewrite = -1,
Willy Tarreau43961d52010-10-04 20:39:20 +0200138 .chksize = BUFSIZE,
Willy Tarreaua24adf02014-11-27 01:11:56 +0100139 .reserved_bufs = RESERVED_BUFS,
Willy Tarreauf3045d22015-04-29 16:24:50 +0200140 .pattern_cache = DEFAULT_PAT_LRU_SIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200141#ifdef USE_OPENSSL
Emeric Brun46635772012-11-14 11:32:56 +0100142 .sslcachesize = SSLCACHESIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200143#endif
William Lallemandf3747832012-11-09 12:33:10 +0100144 .comp_maxlevel = 1,
Willy Tarreau7e312732014-02-12 16:35:14 +0100145#ifdef DEFAULT_IDLE_TIMER
146 .idle_timer = DEFAULT_IDLE_TIMER,
147#else
148 .idle_timer = 1000, /* 1 second */
149#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200150 },
Emeric Brun76d88952012-10-05 15:47:31 +0200151#ifdef USE_OPENSSL
152#ifdef DEFAULT_MAXSSLCONN
Willy Tarreau403edff2012-09-06 11:58:37 +0200153 .maxsslconn = DEFAULT_MAXSSLCONN,
154#endif
Emeric Brun76d88952012-10-05 15:47:31 +0200155#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200156 /* others NULL OK */
157};
158
159/*********************************************************************/
160
161int stopping; /* non zero means stopping in progress */
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100162int killed; /* non zero means a hard-stop is triggered */
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200163int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164
165/* Here we store informations about the pids of the processes we may pause
166 * or kill. We will send them a signal every 10 ms until we can bind to all
167 * our ports. With 200 retries, that's about 2 seconds.
168 */
169#define MAX_START_RETRIES 200
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170static int *oldpids = NULL;
171static int oldpids_sig; /* use USR1 or TERM */
172
Olivier Houchardf73629d2017-04-05 22:33:04 +0200173/* Path to the unix socket we use to retrieve listener sockets from the old process */
174static const char *old_unixsocket;
175
Willy Tarreaubaaee002006-06-26 02:48:02 +0200176/* this is used to drain data, and as a temporary buffer for sprintf()... */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100177struct chunk trash = { };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200178
Willy Tarreau8096de92010-02-26 11:12:27 +0100179/* this buffer is always the same size as standard buffers and is used for
180 * swapping data inside a buffer.
181 */
182char *swap_buffer = NULL;
183
Willy Tarreaubb545b42010-08-25 12:58:59 +0200184int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185const int zero = 0;
186const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200187const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200188
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100189char hostname[MAX_HOSTNAME_LEN];
Emeric Brun2b920a12010-09-23 18:30:22 +0200190char localpeer[MAX_HOSTNAME_LEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191
Willy Tarreau89efaed2013-12-13 15:14:55 +0100192/* used from everywhere just to drain results we don't want to read and which
193 * recent versions of gcc increasingly and annoyingly complain about.
194 */
195int shut_your_big_mouth_gcc_int = 0;
196
William Lallemand73b85e72017-06-01 17:38:51 +0200197int *children = NULL; /* store PIDs of children in master workers mode */
198
199static volatile sig_atomic_t caught_signal = 0;
200static char **next_argv = NULL;
201
Willy Tarreau08ceb102011-07-24 22:58:00 +0200202/* list of the temporarily limited listeners because of lack of resource */
203struct list global_listener_queue = LIST_HEAD_INIT(global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +0200204struct task *global_listener_queue_task;
205static struct task *manage_global_listener_queue(struct task *t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200206
Willy Tarreauff055502014-04-28 22:27:06 +0200207/* bitfield of a few warnings to emit just once (WARN_*) */
208unsigned int warned = 0;
209
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100210
211/* These are strings to be reported in the output of "haproxy -vv". They may
212 * either be constants (in which case must_free must be zero) or dynamically
213 * allocated strings to pass to free() on exit, and in this case must_free
214 * must be non-zero.
215 */
216struct list build_opts_list = LIST_HEAD_INIT(build_opts_list);
217struct build_opts_str {
218 struct list list;
219 const char *str;
220 int must_free;
221};
222
Willy Tarreaue6945732016-12-21 19:57:00 +0100223/* These functions are called just after the point where the program exits
224 * after a config validity check, so they are generally suited for resource
225 * allocation and slow initializations that should be skipped during basic
226 * config checks. The functions must return 0 on success, or a combination
227 * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause
228 * and immediate exit, so the function must have emitted any useful error.
229 */
230struct list post_check_list = LIST_HEAD_INIT(post_check_list);
231struct post_check_fct {
232 struct list list;
233 int (*fct)();
234};
235
Willy Tarreau05554e62016-12-21 20:46:26 +0100236/* These functions are called when freeing the global sections at the end
237 * of deinit, after everything is stopped. They don't return anything, and
238 * they work in best effort mode as their sole goal is to make valgrind
239 * mostly happy.
240 */
241struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list);
242struct post_deinit_fct {
243 struct list list;
244 void (*fct)();
245};
246
Willy Tarreaubaaee002006-06-26 02:48:02 +0200247/*********************************************************************/
248/* general purpose functions ***************************************/
249/*********************************************************************/
250
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100251/* used to register some build option strings at boot. Set must_free to
252 * non-zero if the string must be freed upon exit.
253 */
254void hap_register_build_opts(const char *str, int must_free)
255{
256 struct build_opts_str *b;
257
258 b = calloc(1, sizeof(*b));
259 if (!b) {
260 fprintf(stderr, "out of memory\n");
261 exit(1);
262 }
263 b->str = str;
264 b->must_free = must_free;
265 LIST_ADDQ(&build_opts_list, &b->list);
266}
267
Willy Tarreaue6945732016-12-21 19:57:00 +0100268/* used to register some initialization functions to call after the checks. */
269void hap_register_post_check(int (*fct)())
270{
271 struct post_check_fct *b;
272
273 b = calloc(1, sizeof(*b));
274 if (!b) {
275 fprintf(stderr, "out of memory\n");
276 exit(1);
277 }
278 b->fct = fct;
279 LIST_ADDQ(&post_check_list, &b->list);
280}
281
Willy Tarreau05554e62016-12-21 20:46:26 +0100282/* used to register some de-initialization functions to call after everything
283 * has stopped.
284 */
285void hap_register_post_deinit(void (*fct)())
286{
287 struct post_deinit_fct *b;
288
289 b = calloc(1, sizeof(*b));
290 if (!b) {
291 fprintf(stderr, "out of memory\n");
292 exit(1);
293 }
294 b->fct = fct;
295 LIST_ADDQ(&post_deinit_list, &b->list);
296}
297
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100298static void display_version()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299{
300 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau7b677262017-04-03 09:27:49 +0200301 printf("Copyright 2000-2017 Willy Tarreau <willy@haproxy.org>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200302}
303
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100304static void display_build_opts()
Willy Tarreau7b066db2007-12-02 11:28:59 +0100305{
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100306 struct build_opts_str *item;
307
Willy Tarreau7b066db2007-12-02 11:28:59 +0100308 printf("Build options :"
309#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100310 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100311#endif
312#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100313 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100314#endif
315#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100316 "\n CC = " BUILD_CC
317#endif
318#ifdef BUILD_CFLAGS
319 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100320#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100321#ifdef BUILD_OPTIONS
322 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100323#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200324 "\n\nDefault settings :"
325 "\n maxconn = %d, bufsize = %d, maxrewrite = %d, maxpollevents = %d"
326 "\n\n",
327 DEFAULT_MAXCONN, BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS);
Willy Tarreaube5b6852009-10-03 18:57:08 +0200328
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100329 list_for_each_entry(item, &build_opts_list, list) {
330 puts(item->str);
331 }
332
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100333 putchar('\n');
334
Willy Tarreaube5b6852009-10-03 18:57:08 +0200335 list_pollers(stdout);
336 putchar('\n');
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100337 list_filters(stdout);
338 putchar('\n');
Willy Tarreau7b066db2007-12-02 11:28:59 +0100339}
340
Willy Tarreaubaaee002006-06-26 02:48:02 +0200341/*
342 * This function prints the command line usage and exits
343 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100344static void usage(char *name)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345{
346 display_version();
347 fprintf(stderr,
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200348 "Usage : %s [-f <cfgfile|cfgdir>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
Willy Tarreaua088d312015-10-08 11:58:48 +0200350 " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100351 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352 " -d enters debug mode ; -db only disables background mode.\n"
Willy Tarreau6e064432012-05-08 15:40:42 +0200353 " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200354 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200355 " -D goes daemon ; -C changes to <dir> before loading files.\n"
William Lallemand095ba4c2017-06-01 17:38:50 +0200356 " -W master-worker mode.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200358 " -c check mode : only check config files and exit\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359 " -n sets the maximum total # of connections (%d)\n"
360 " -m limits the usable amount of memory (in MB)\n"
361 " -N sets the default, per-proxy maximum # of connections (%d)\n"
Emeric Brun2b920a12010-09-23 18:30:22 +0200362 " -L set local peer name (default to hostname)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363 " -p writes pids of all children to this file\n"
364#if defined(ENABLE_EPOLL)
365 " -de disables epoll() usage even when available\n"
366#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200367#if defined(ENABLE_KQUEUE)
368 " -dk disables kqueue() usage even when available\n"
369#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370#if defined(ENABLE_POLL)
371 " -dp disables poll() usage even when available\n"
372#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200373#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100374 " -dS disables splice usage (broken on old kernels)\n"
375#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200376#if defined(USE_GETADDRINFO)
377 " -dG disables getaddrinfo() usage\n"
378#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000379#if defined(SO_REUSEPORT)
380 " -dR disables SO_REUSEPORT usage\n"
381#endif
Willy Tarreau3eed10e2016-11-07 21:03:16 +0100382 " -dr ignores server address resolution failures\n"
Emeric Brun850efd52014-01-29 12:24:34 +0100383 " -dV disables SSL verify on servers side\n"
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +0200384 " -sf/-st [pid ]* finishes/terminates old pids.\n"
Olivier Houchardf73629d2017-04-05 22:33:04 +0200385 " -x <unix_socket> get listening sockets from a unix socket\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386 "\n",
387 name, DEFAULT_MAXCONN, cfg_maxpconn);
388 exit(1);
389}
390
391
392
393/*********************************************************************/
394/* more specific functions ***************************************/
395/*********************************************************************/
396
William Lallemand73b85e72017-06-01 17:38:51 +0200397/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
398 * pids the signal was correctly delivered to.
399 */
400static int tell_old_pids(int sig)
401{
402 int p;
403 int ret = 0;
404 for (p = 0; p < nb_oldpids; p++)
405 if (kill(oldpids[p], sig) == 0)
406 ret++;
407 return ret;
408}
409
410/* return 1 if a pid is a current child otherwise 0 */
411
412int current_child(int pid)
413{
414 int i;
415
416 for (i = 0; i < global.nbproc; i++) {
417 if (children[i] == pid)
418 return 1;
419 }
420 return 0;
421}
422
423static void mworker_signalhandler(int signum)
424{
425 caught_signal = signum;
426}
427
428static void mworker_register_signals()
429{
430 struct sigaction sa;
431 /* Here we are not using the haproxy async way
432 for signals because it does not exists in
433 the master */
434 memset(&sa, 0, sizeof(struct sigaction));
435 sa.sa_handler = &mworker_signalhandler;
436 sigaction(SIGHUP, &sa, NULL);
437 sigaction(SIGUSR1, &sa, NULL);
438 sigaction(SIGUSR2, &sa, NULL);
439 sigaction(SIGINT, &sa, NULL);
440 sigaction(SIGTERM, &sa, NULL);
441}
442
443static void mworker_block_signals()
444{
445 sigset_t set;
446
447 sigemptyset(&set);
448 sigaddset(&set, SIGUSR1);
449 sigaddset(&set, SIGUSR2);
450 sigaddset(&set, SIGHUP);
451 sigaddset(&set, SIGINT);
452 sigaddset(&set, SIGTERM);
453 sigprocmask(SIG_SETMASK, &set, NULL);
454}
455
456static void mworker_unblock_signals()
457{
458 sigset_t set;
459
460 sigemptyset(&set);
461 sigaddset(&set, SIGUSR1);
462 sigaddset(&set, SIGUSR2);
463 sigaddset(&set, SIGHUP);
464 sigaddset(&set, SIGINT);
465 sigaddset(&set, SIGTERM);
466 sigprocmask(SIG_UNBLOCK, &set, NULL);
467}
468
469static void mworker_unregister_signals()
470{
471 signal(SIGINT, SIG_DFL);
472 signal(SIGTERM, SIG_DFL);
473 signal(SIGHUP, SIG_IGN);
474 signal(SIGUSR1, SIG_IGN);
475 signal(SIGUSR2, SIG_IGN);
476}
477
478/*
479 * Send signal to every known children.
480 */
481
482static void mworker_kill(int sig)
483{
484 int i;
485
486 tell_old_pids(sig);
487 if (children) {
488 for (i = 0; i < global.nbproc; i++)
489 kill(children[i], sig);
490 }
491}
492
Willy Tarreaubaaee002006-06-26 02:48:02 +0200493/*
William Lallemand73b85e72017-06-01 17:38:51 +0200494 * remove a pid forom the olpid array and decrease nb_oldpids
495 * return 1 pid was found otherwise return 0
496 */
497
498int delete_oldpid(int pid)
499{
500 int i;
501
502 for (i = 0; i < nb_oldpids; i++) {
503 if (oldpids[i] == pid) {
504 oldpids[i] = oldpids[nb_oldpids - 1];
505 oldpids[nb_oldpids - 1] = 0;
506 nb_oldpids--;
507 return 1;
508 }
509 }
510 return 0;
511}
512
513/*
514 * When called, this function reexec haproxy with -sf followed by current
515 * children PIDs and possibily old children PIDs if they didn't leave yet.
516 */
517static void mworker_reload()
518{
519 int next_argc = 0;
520 int j;
521 char *msg = NULL;
522
523 mworker_block_signals();
524 mworker_unregister_signals();
525 setenv("HAPROXY_MWORKER_REEXEC", "1", 1);
526
527 /* compute length */
528 while (next_argv[next_argc])
529 next_argc++;
530
531 /* 1 for haproxy -sf */
532 next_argv = realloc(next_argv, (next_argc + 1 + global.nbproc + nb_oldpids + 1) * sizeof(char *));
533 if (next_argv == NULL)
534 goto alloc_error;
535
536
537 /* add -sf <PID>* to argv */
538 if (children || nb_oldpids > 0)
539 next_argv[next_argc++] = "-sf";
540 if (children) {
541 for (j = 0; j < global.nbproc; next_argc++,j++) {
542 next_argv[next_argc] = memprintf(&msg, "%d", children[j]);
543 if (next_argv[next_argc] == NULL)
544 goto alloc_error;
545 msg = NULL;
546 }
547 }
548 /* copy old process PIDs */
549 for (j = 0; j < nb_oldpids; next_argc++,j++) {
550 next_argv[next_argc] = memprintf(&msg, "%d", oldpids[j]);
551 if (next_argv[next_argc] == NULL)
552 goto alloc_error;
553 msg = NULL;
554 }
555 next_argv[next_argc] = NULL;
556 deinit(); /* we don't want to leak FD there */
557 Warning("Reexecuting Master process\n");
558 execv(next_argv[0], next_argv);
559
560alloc_error:
561 Warning("Cannot allocate memory\n");
562 Warning("Failed to reexecute the master processs [%d]\n", pid);
563 return;
564}
565
566
567/*
568 * Wait for every children to exit
569 */
570
571static void mworker_wait()
572{
573 int exitpid = -1;
574 int status = 0;
575
576 mworker_register_signals();
577 mworker_unblock_signals();
578
579 while (1) {
580
581 while (((exitpid = wait(&status)) == -1) && errno == EINTR) {
582 int sig = caught_signal;
583 if (sig == SIGUSR2 || sig == SIGHUP) {
584 mworker_reload();
585 } else {
586 Warning("Exiting Master process...\n");
587 mworker_kill(sig);
588 mworker_unregister_signals();
589 }
590 caught_signal = 0;
591 }
592
593 if (exitpid == -1 && errno == ECHILD) {
594 Warning("All workers are left. Leaving... (%d)\n", status);
595 exit(status); /* parent must leave using the latest status code known */
596 }
597
598 if (WIFEXITED(status))
599 status = WEXITSTATUS(status);
600 else if (WIFSIGNALED(status))
601 status = 128 + WTERMSIG(status);
602 else if (WIFSTOPPED(status))
603 status = 128 + WSTOPSIG(status);
604 else
605 status = 255;
606
607 if (!children) {
608 Warning("Worker %d left with exit code %d\n", exitpid, status);
609 } else {
610 /* check if exited child was in the current children list */
611 if (current_child(exitpid)) {
612 Alert("Current worker %d left with exit code %d\n", exitpid, status);
613 } else {
614 Warning("Former worker %d left with exit code %d\n", exitpid, status);
615 delete_oldpid(exitpid);
616 }
617 }
618 }
619}
620
621
622
623/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200624 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
625 * a signal zero to all subscribers. This means that it's as easy as
626 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200627 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100628static void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629{
630 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200631 signal_unregister_handler(sh);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200632 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200633}
634
635/*
636 * upon SIGTTOU, we pause everything
637 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100638static void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200639{
640 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200641 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200642}
643
644/*
645 * upon SIGTTIN, let's have a soft stop.
646 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100647static void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648{
Willy Tarreaube58c382011-07-24 18:28:10 +0200649 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650}
651
652/*
653 * this function dumps every server's state when the process receives SIGHUP.
654 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100655static void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200656{
657 struct proxy *p = proxy;
658
659 Warning("SIGHUP received, dumping servers states.\n");
660 while (p) {
661 struct server *s = p->srv;
662
663 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
664 while (s) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100665 chunk_printf(&trash,
666 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
667 p->id, s->id,
Willy Tarreau892337c2014-05-13 23:41:20 +0200668 (s->state != SRV_ST_STOPPED) ? "UP" : "DOWN",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100669 s->cur_sess, s->nbpend, s->counters.cum_sess);
670 Warning("%s\n", trash.str);
671 send_log(p, LOG_NOTICE, "%s\n", trash.str);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200672 s = s->next;
673 }
674
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200675 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
676 if (!p->srv) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100677 chunk_printf(&trash,
678 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
679 p->id,
680 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 +0200681 } else if (p->srv_act == 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100682 chunk_printf(&trash,
683 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
684 p->id,
685 (p->srv_bck) ? "is running on backup servers" : "has no server available",
686 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 +0200687 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100688 chunk_printf(&trash,
689 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
690 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
691 p->id, p->srv_act, p->srv_bck,
692 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 +0200693 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100694 Warning("%s\n", trash.str);
695 send_log(p, LOG_NOTICE, "%s\n", trash.str);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200696
697 p = p->next;
698 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200699}
700
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100701static void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200702{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200703 /* dump memory usage then free everything possible */
704 dump_pools();
705 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706}
707
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200708/* This function check if cfg_cfgfiles containes directories.
709 * If it find one, it add all the files (and only files) it containes
710 * in cfg_cfgfiles in place of the directory (and remove the directory).
711 * It add the files in lexical order.
712 * It add only files with .cfg extension.
713 * It doesn't add files with name starting with '.'
714 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100715static void cfgfiles_expand_directories(void)
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200716{
717 struct wordlist *wl, *wlb;
718 char *err = NULL;
719
720 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
721 struct stat file_stat;
722 struct dirent **dir_entries = NULL;
723 int dir_entries_nb;
724 int dir_entries_it;
725
726 if (stat(wl->s, &file_stat)) {
727 Alert("Cannot open configuration file/directory %s : %s\n",
728 wl->s,
729 strerror(errno));
730 exit(1);
731 }
732
733 if (!S_ISDIR(file_stat.st_mode))
734 continue;
735
736 /* from this point wl->s is a directory */
737
738 dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort);
739 if (dir_entries_nb < 0) {
740 Alert("Cannot open configuration directory %s : %s\n",
741 wl->s,
742 strerror(errno));
743 exit(1);
744 }
745
746 /* for each element in the directory wl->s */
747 for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) {
748 struct dirent *dir_entry = dir_entries[dir_entries_it];
749 char *filename = NULL;
750 char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg");
751
752 /* don't add filename that begin with .
753 * only add filename with .cfg extention
754 */
755 if (dir_entry->d_name[0] == '.' ||
756 !(d_name_cfgext && d_name_cfgext[4] == '\0'))
757 goto next_dir_entry;
758
759 if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) {
760 Alert("Cannot load configuration files %s : out of memory.\n",
761 filename);
762 exit(1);
763 }
764
765 if (stat(filename, &file_stat)) {
766 Alert("Cannot open configuration file %s : %s\n",
767 wl->s,
768 strerror(errno));
769 exit(1);
770 }
771
772 /* don't add anything else than regular file in cfg_cfgfiles
773 * this way we avoid loops
774 */
775 if (!S_ISREG(file_stat.st_mode))
776 goto next_dir_entry;
777
778 if (!list_append_word(&wl->list, filename, &err)) {
779 Alert("Cannot load configuration files %s : %s\n",
780 filename,
781 err);
782 exit(1);
783 }
784
785next_dir_entry:
786 free(filename);
787 free(dir_entry);
788 }
789
790 free(dir_entries);
791
792 /* remove the current directory (wl) from cfg_cfgfiles */
793 free(wl->s);
794 LIST_DEL(&wl->list);
795 free(wl);
796 }
797
798 free(err);
799}
800
Olivier Houchardf73629d2017-04-05 22:33:04 +0200801static int get_old_sockets(const char *unixsocket)
802{
803 char *cmsgbuf = NULL, *tmpbuf = NULL;
804 int *tmpfd = NULL;
805 struct sockaddr_un addr;
806 struct cmsghdr *cmsg;
807 struct msghdr msghdr;
808 struct iovec iov;
809 struct xfer_sock_list *xfer_sock = NULL;
Olivier Houchard54740872017-04-06 14:45:14 +0200810 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf73629d2017-04-05 22:33:04 +0200811 int sock = -1;
812 int ret = -1;
813 int ret2 = -1;
814 int fd_nb;
815 int got_fd = 0;
816 int i = 0;
817 size_t maxoff = 0, curoff = 0;
818
819 memset(&msghdr, 0, sizeof(msghdr));
820 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
821 if (!cmsgbuf) {
822 Warning("Failed to allocate memory to send sockets\n");
823 goto out;
824 }
825 sock = socket(PF_UNIX, SOCK_STREAM, 0);
826 if (sock < 0) {
827 Warning("Failed to connect to the old process socket '%s'\n",
828 unixsocket);
829 goto out;
830 }
831 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path));
832 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
833 addr.sun_family = PF_UNIX;
834 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
835 if (ret < 0) {
836 Warning("Failed to connect to the old process socket '%s'\n",
837 unixsocket);
838 goto out;
839 }
Olivier Houchard54740872017-04-06 14:45:14 +0200840 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf73629d2017-04-05 22:33:04 +0200841 iov.iov_base = &fd_nb;
842 iov.iov_len = sizeof(fd_nb);
843 msghdr.msg_iov = &iov;
844 msghdr.msg_iovlen = 1;
845 send(sock, "_getsocks\n", strlen("_getsocks\n"), 0);
846 /* First, get the number of file descriptors to be received */
847 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
848 Warning("Failed to get the number of sockets to be transferred !\n");
849 goto out;
850 }
851 if (fd_nb == 0) {
852 ret = 0;
853 goto out;
854 }
855 tmpbuf = malloc(fd_nb * (1 + NAME_MAX + 1 + IFNAMSIZ + sizeof(int)));
856 if (tmpbuf == NULL) {
857 Warning("Failed to allocate memory while receiving sockets\n");
858 goto out;
859 }
860 tmpfd = malloc(fd_nb * sizeof(int));
861 if (tmpfd == NULL) {
862 Warning("Failed to allocate memory while receiving sockets\n");
863 goto out;
864 }
865 msghdr.msg_control = cmsgbuf;
866 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
867 iov.iov_len = MAX_SEND_FD * (1 + NAME_MAX + 1 + IFNAMSIZ + sizeof(int));
868 do {
869 int ret3;
870
871 iov.iov_base = tmpbuf + curoff;
872 ret = recvmsg(sock, &msghdr, 0);
873 if (ret == -1 && errno == EINTR)
874 continue;
875 if (ret <= 0)
876 break;
877 /* Send an ack to let the sender know we got the sockets
878 * and it can send some more
879 */
880 do {
881 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
882 } while (ret3 == -1 && errno == EINTR);
883 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
884 cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
885 if (cmsg->cmsg_level == SOL_SOCKET &&
886 cmsg->cmsg_type == SCM_RIGHTS) {
887 size_t totlen = cmsg->cmsg_len -
888 CMSG_LEN(0);
889 if (totlen / sizeof(int) + got_fd > fd_nb) {
890 Warning("Got to many sockets !\n");
891 goto out;
892 }
893 /*
894 * Be paranoid and use memcpy() to avoid any
895 * potential alignement issue.
896 */
897 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
898 got_fd += totlen / sizeof(int);
899 }
900 }
901 curoff += ret;
902 } while (got_fd < fd_nb);
903
904 if (got_fd != fd_nb) {
905 Warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
906 fd_nb, got_fd);
907 goto out;
908 }
909 maxoff = curoff;
910 curoff = 0;
911 for (i = 0; i < got_fd; i++) {
912 int fd = tmpfd[i];
913 socklen_t socklen;
914 int len;
915
916 xfer_sock = calloc(1, sizeof(*xfer_sock));
917 if (!xfer_sock) {
918 Warning("Failed to allocate memory in get_old_sockets() !\n");
919 break;
920 }
921 xfer_sock->fd = -1;
922
923 socklen = sizeof(xfer_sock->addr);
924 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
925 Warning("Failed to get socket address\n");
926 free(xfer_sock);
927 continue;
928 }
929 if (curoff >= maxoff) {
930 Warning("Inconsistency while transferring sockets\n");
931 goto out;
932 }
933 len = tmpbuf[curoff++];
934 if (len > 0) {
935 /* We have a namespace */
936 if (curoff + len > maxoff) {
937 Warning("Inconsistency while transferring sockets\n");
938 goto out;
939 }
940 xfer_sock->namespace = malloc(len + 1);
941 if (!xfer_sock->namespace) {
942 Warning("Failed to allocate memory while transferring sockets\n");
943 goto out;
944 }
945 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
946 xfer_sock->namespace[len] = 0;
947 curoff += len;
948 }
949 if (curoff >= maxoff) {
950 Warning("Inconsistency while transferring sockets\n");
951 goto out;
952 }
953 len = tmpbuf[curoff++];
954 if (len > 0) {
955 /* We have an interface */
956 if (curoff + len > maxoff) {
957 Warning("Inconsistency while transferring sockets\n");
958 goto out;
959 }
960 xfer_sock->iface = malloc(len + 1);
961 if (!xfer_sock->iface) {
962 Warning("Failed to allocate memory while transferring sockets\n");
963 goto out;
964 }
965 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
966 xfer_sock->namespace[len] = 0;
967 curoff += len;
968 }
969 if (curoff + sizeof(int) > maxoff) {
970 Warning("Inconsistency while transferring sockets\n");
971 goto out;
972 }
973 memcpy(&xfer_sock->options, &tmpbuf[curoff],
974 sizeof(xfer_sock->options));
975 curoff += sizeof(xfer_sock->options);
976
977 xfer_sock->fd = fd;
978 if (xfer_sock_list)
979 xfer_sock_list->prev = xfer_sock;
980 xfer_sock->next = xfer_sock_list;
981 xfer_sock->prev = NULL;
982 xfer_sock_list = xfer_sock;
983 xfer_sock = NULL;
984 }
985
986 ret2 = 0;
987out:
988 /* If we failed midway make sure to close the remaining
989 * file descriptors
990 */
991 if (tmpfd != NULL && i < got_fd) {
992 for (; i < got_fd; i++) {
993 close(tmpfd[i]);
994 }
995 }
996 free(tmpbuf);
997 free(tmpfd);
998 free(cmsgbuf);
999 if (sock != -1)
1000 close(sock);
1001 if (xfer_sock) {
1002 free(xfer_sock->namespace);
1003 free(xfer_sock->iface);
1004 if (xfer_sock->fd != -1)
1005 close(xfer_sock->fd);
1006 free(xfer_sock);
1007 }
1008 return (ret2);
1009}
1010
Willy Tarreaubaaee002006-06-26 02:48:02 +02001011/*
William Lallemand73b85e72017-06-01 17:38:51 +02001012 * copy and cleanup the current argv
1013 * Remove the -sf /-st parameters
1014 * Return an allocated copy of argv
1015 */
1016
1017static char **copy_argv(int argc, char **argv)
1018{
1019 char **newargv;
1020 int i, j;
1021
1022 newargv = calloc(argc + 2, sizeof(char *));
1023 if (newargv == NULL) {
1024 Warning("Cannot allocate memory\n");
1025 return NULL;
1026 }
1027
1028 for (i = 0, j = 0; i < argc; i++, j++) {
1029 char *flag = *(argv + i) + 1;
1030
1031 /* -sf or -st */
1032 if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1033 /* list of pids to finish ('f') or terminate ('t') */
1034 i++;
1035 while (i < argc && argv[i][0] != '-') {
1036 i++;
1037 }
1038 }
1039 newargv[j] = argv[i];
1040 }
1041 return newargv;
1042}
1043
1044/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001045 * This function initializes all the necessary variables. It only returns
1046 * if everything is OK. If something fails, it exits.
1047 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001048static void init(int argc, char **argv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001049{
Willy Tarreaubaaee002006-06-26 02:48:02 +02001050 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001051 char *tmp;
1052 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +02001053 int err_code = 0;
Maxime de Roucy0f503922016-05-13 23:52:55 +02001054 char *err_msg = NULL;
Willy Tarreau477ecd82010-01-03 21:12:30 +01001055 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +00001056 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +02001057 char *change_dir = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001058 struct proxy *px;
Willy Tarreaue6945732016-12-21 19:57:00 +01001059 struct post_check_fct *pcf;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060
William Lallemand73b85e72017-06-01 17:38:51 +02001061 next_argv = copy_argv(argc, argv);
1062
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001063 chunk_init(&trash, malloc(global.tune.bufsize), global.tune.bufsize);
Willy Tarreau2819e992013-12-13 14:41:10 +01001064 alloc_trash_buffers(global.tune.bufsize);
David du Colombier7af46052012-05-16 14:16:48 +02001065
Emeric Brun2b920a12010-09-23 18:30:22 +02001066 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
1067 * the string in case of truncation, and at least FreeBSD appears not to do
1068 * it.
1069 */
1070 memset(hostname, 0, sizeof(hostname));
1071 gethostname(hostname, sizeof(hostname) - 1);
1072 memset(localpeer, 0, sizeof(localpeer));
1073 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
1074
Willy Tarreaubaaee002006-06-26 02:48:02 +02001075 /*
1076 * Initialize the previously static variables.
1077 */
1078
Willy Tarreau3eba98a2009-01-25 13:56:13 +01001079 totalconn = actconn = maxfd = listeners = stopping = 0;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001080 killed = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001081
1082
1083#ifdef HAPROXY_MEMMAX
Willy Tarreau70060452015-12-14 12:46:07 +01001084 global.rlimit_memmax_all = HAPROXY_MEMMAX;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001085#endif
1086
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001087 tzset();
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001088 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001089 start_date = now;
1090
Willy Tarreau84310e22014-02-14 11:59:04 +01001091 srandom(now_ms - getpid());
1092
Dragan Dosen835b9212016-02-12 13:23:03 +01001093 init_log();
Willy Tarreau29857942009-05-10 09:01:21 +02001094 signal_init();
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001095 if (init_acl() != 0)
1096 exit(1);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001097 init_task();
Willy Tarreau87b09662015-04-03 00:22:06 +02001098 init_stream();
Willy Tarreaub1ec8c42015-04-03 13:53:24 +02001099 init_session();
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001100 init_connection();
Willy Tarreau8280d642009-09-23 23:37:52 +02001101 /* warning, we init buffers later */
Willy Tarreaue4d7e552007-05-13 20:19:55 +02001102 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +01001103 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001105 /* Initialise lua. */
1106 hlua_init();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001107
Christopher Fauletff2613e2016-11-09 11:36:17 +01001108 /* Initialize process vars */
1109 vars_init(&global.vars, SCOPE_PROC);
1110
Willy Tarreau43b78992009-01-25 15:42:27 +01001111 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001112#if defined(ENABLE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001113 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001114#endif
1115#if defined(ENABLE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001116 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001117#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001118#if defined(ENABLE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +01001119 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001120#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001121#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001122 global.tune.options |= GTUNE_USE_SPLICE;
1123#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001124#if defined(USE_GETADDRINFO)
1125 global.tune.options |= GTUNE_USE_GAI;
1126#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001127#if defined(SO_REUSEPORT)
1128 global.tune.options |= GTUNE_USE_REUSEPORT;
1129#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001130
1131 pid = getpid();
1132 progname = *argv;
1133 while ((tmp = strchr(progname, '/')) != NULL)
1134 progname = tmp + 1;
1135
Kevinm48936af2010-12-22 16:08:21 +00001136 /* the process name is used for the logs only */
Dragan Dosen43885c72015-10-01 13:18:13 +02001137 chunk_initstr(&global.log_tag, strdup(progname));
Kevinm48936af2010-12-22 16:08:21 +00001138
Willy Tarreaubaaee002006-06-26 02:48:02 +02001139 argc--; argv++;
1140 while (argc > 0) {
1141 char *flag;
1142
1143 if (**argv == '-') {
1144 flag = *argv+1;
1145
1146 /* 1 arg */
1147 if (*flag == 'v') {
1148 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +01001149 if (flag[1] == 'v') /* -vv */
1150 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001151 exit(0);
1152 }
1153#if defined(ENABLE_EPOLL)
1154 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +01001155 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001156#endif
1157#if defined(ENABLE_POLL)
1158 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +01001159 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001160#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +02001161#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001162 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +01001163 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001164#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001165#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001166 else if (*flag == 'd' && flag[1] == 'S')
1167 global.tune.options &= ~GTUNE_USE_SPLICE;
1168#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001169#if defined(USE_GETADDRINFO)
1170 else if (*flag == 'd' && flag[1] == 'G')
1171 global.tune.options &= ~GTUNE_USE_GAI;
1172#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001173#if defined(SO_REUSEPORT)
1174 else if (*flag == 'd' && flag[1] == 'R')
1175 global.tune.options &= ~GTUNE_USE_REUSEPORT;
1176#endif
Emeric Brun850efd52014-01-29 12:24:34 +01001177 else if (*flag == 'd' && flag[1] == 'V')
1178 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001179 else if (*flag == 'V')
1180 arg_mode |= MODE_VERBOSE;
1181 else if (*flag == 'd' && flag[1] == 'b')
1182 arg_mode |= MODE_FOREGROUND;
Willy Tarreau6e064432012-05-08 15:40:42 +02001183 else if (*flag == 'd' && flag[1] == 'M')
1184 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreau3eed10e2016-11-07 21:03:16 +01001185 else if (*flag == 'd' && flag[1] == 'r')
1186 global.tune.options |= GTUNE_RESOLVE_DONTFAIL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001187 else if (*flag == 'd')
1188 arg_mode |= MODE_DEBUG;
1189 else if (*flag == 'c')
1190 arg_mode |= MODE_CHECK;
William Lallemand095ba4c2017-06-01 17:38:50 +02001191 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +02001192 arg_mode |= MODE_DAEMON;
William Lallemand095ba4c2017-06-01 17:38:50 +02001193 else if (*flag == 'W')
1194 arg_mode |= MODE_MWORKER;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001195 else if (*flag == 'q')
1196 arg_mode |= MODE_QUIET;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001197 else if (*flag == 'x') {
1198 if (argv[1][0] == '-') {
1199 Alert("Unix socket path expected with the -x flag\n");
1200 exit(1);
1201 }
1202 old_unixsocket = argv[1];
1203 argv++;
1204 argc--;
1205 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001206 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1207 /* list of pids to finish ('f') or terminate ('t') */
1208
1209 if (flag[1] == 'f')
1210 oldpids_sig = SIGUSR1; /* finish then exit */
1211 else
1212 oldpids_sig = SIGTERM; /* terminate immediately */
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001213 while (argc > 1 && argv[1][0] != '-') {
1214 oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int));
1215 if (!oldpids) {
1216 Alert("Cannot allocate old pid : out of memory.\n");
1217 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001218 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001219 argc--; argv++;
1220 oldpids[nb_oldpids] = atol(*argv);
1221 if (oldpids[nb_oldpids] <= 0)
1222 usage(progname);
1223 nb_oldpids++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001224 }
1225 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001226 else if (flag[0] == '-' && flag[1] == 0) { /* "--" */
1227 /* now that's a cfgfile list */
1228 argv++; argc--;
1229 while (argc > 0) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02001230 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
1231 Alert("Cannot load configuration file/directory %s : %s\n",
1232 *argv,
1233 err_msg);
Willy Tarreaua088d312015-10-08 11:58:48 +02001234 exit(1);
1235 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001236 argv++; argc--;
1237 }
1238 break;
1239 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001240 else { /* >=2 args */
1241 argv++; argc--;
1242 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001243 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001244
1245 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +02001246 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001247 case 'n' : cfg_maxconn = atol(*argv); break;
Willy Tarreau70060452015-12-14 12:46:07 +01001248 case 'm' : global.rlimit_memmax_all = atol(*argv); break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001249 case 'N' : cfg_maxpconn = atol(*argv); break;
Emeric Brun2b920a12010-09-23 18:30:22 +02001250 case 'L' : strncpy(localpeer, *argv, sizeof(localpeer) - 1); break;
Willy Tarreau5d01a632009-06-22 16:02:30 +02001251 case 'f' :
Maxime de Roucy0f503922016-05-13 23:52:55 +02001252 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
1253 Alert("Cannot load configuration file/directory %s : %s\n",
1254 *argv,
1255 err_msg);
Willy Tarreau5d01a632009-06-22 16:02:30 +02001256 exit(1);
1257 }
Willy Tarreau5d01a632009-06-22 16:02:30 +02001258 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001259 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001260 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001261 }
1262 }
1263 }
1264 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001265 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001266 argv++; argc--;
1267 }
1268
1269 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
William Lallemand095ba4c2017-06-01 17:38:50 +02001270 (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
Willy Tarreaubaaee002006-06-26 02:48:02 +02001271 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
1272
Willy Tarreau576132e2011-09-10 19:26:56 +02001273 if (change_dir && chdir(change_dir) < 0) {
1274 Alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
1275 exit(1);
1276 }
1277
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001278 /* handle cfgfiles that are actualy directories */
1279 cfgfiles_expand_directories();
1280
1281 if (LIST_ISEMPTY(&cfg_cfgfiles))
1282 usage(progname);
1283
Willy Tarreaubaaee002006-06-26 02:48:02 +02001284 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001285
1286 init_default_instance();
1287
Willy Tarreau477ecd82010-01-03 21:12:30 +01001288 list_for_each_entry(wl, &cfg_cfgfiles, list) {
Willy Tarreauc4382422009-12-06 13:10:44 +01001289 int ret;
1290
Willy Tarreau477ecd82010-01-03 21:12:30 +01001291 ret = readcfgfile(wl->s);
Willy Tarreauc4382422009-12-06 13:10:44 +01001292 if (ret == -1) {
1293 Alert("Could not open configuration file %s : %s\n",
Willy Tarreau477ecd82010-01-03 21:12:30 +01001294 wl->s, strerror(errno));
Willy Tarreauc4382422009-12-06 13:10:44 +01001295 exit(1);
1296 }
Willy Tarreau25a67fa2009-12-15 21:46:25 +01001297 if (ret & (ERR_ABORT|ERR_FATAL))
Willy Tarreau477ecd82010-01-03 21:12:30 +01001298 Alert("Error(s) found in configuration file : %s\n", wl->s);
Willy Tarreau25a67fa2009-12-15 21:46:25 +01001299 err_code |= ret;
Willy Tarreau058e9072009-07-20 09:30:05 +02001300 if (err_code & ERR_ABORT)
Willy Tarreau5d01a632009-06-22 16:02:30 +02001301 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001302 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001303
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02001304 /* do not try to resolve arguments nor to spot inconsistencies when
1305 * the configuration contains fatal errors caused by files not found
1306 * or failed memory allocations.
1307 */
1308 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1309 Alert("Fatal errors found in configuration.\n");
1310 exit(1);
1311 }
1312
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001313 pattern_finalize_config();
1314
Willy Tarreaubb925012009-07-23 13:36:36 +02001315 err_code |= check_config_validity();
1316 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1317 Alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001318 exit(1);
1319 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001320
Willy Tarreau70060452015-12-14 12:46:07 +01001321 /* recompute the amount of per-process memory depending on nbproc and
1322 * the shared SSL cache size (allowed to exist in all processes).
1323 */
1324 if (global.rlimit_memmax_all) {
1325#if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE)
1326 int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL;
1327
1328 global.rlimit_memmax =
1329 ((((int64_t)global.rlimit_memmax_all * 1048576LL) -
1330 ssl_cache_bytes) / global.nbproc +
1331 ssl_cache_bytes + 1048575LL) / 1048576LL;
1332#else
1333 global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc;
1334#endif
1335 }
1336
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001337#ifdef CONFIG_HAP_NS
1338 err_code |= netns_init();
1339 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1340 Alert("Failed to initialize namespace support.\n");
1341 exit(1);
1342 }
1343#endif
1344
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01001345 /* Apply server states */
1346 apply_server_state();
1347
1348 for (px = proxy; px; px = px->next)
1349 srv_compute_all_admin_states(px);
1350
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001351 /* Apply servers' configured address */
1352 err_code |= srv_init_addr();
1353 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1354 Alert("Failed to initialize server(s) addr.\n");
1355 exit(1);
1356 }
1357
Willy Tarreaubaaee002006-06-26 02:48:02 +02001358 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001359 struct peers *pr;
1360 struct proxy *px;
1361
1362 for (pr = peers; pr; pr = pr->next)
1363 if (pr->peers_fe)
1364 break;
1365
1366 for (px = proxy; px; px = px->next)
Willy Tarreau4348fad2012-09-20 16:48:07 +02001367 if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners))
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001368 break;
1369
1370 if (pr || px) {
1371 /* At least one peer or one listener has been found */
1372 qfprintf(stdout, "Configuration file is valid\n");
1373 exit(0);
1374 }
1375 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
1376 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001377 }
1378
Willy Tarreaue9b26022011-08-01 20:57:55 +02001379 global_listener_queue_task = task_new();
1380 if (!global_listener_queue_task) {
1381 Alert("Out of memory when initializing global task\n");
1382 exit(1);
1383 }
1384 /* very simple initialization, users will queue the task if needed */
1385 global_listener_queue_task->context = NULL; /* not even a context! */
1386 global_listener_queue_task->process = manage_global_listener_queue;
1387 global_listener_queue_task->expire = TICK_ETERNITY;
1388
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001389 /* now we know the buffer size, we can initialize the channels and buffers */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001390 init_buffer();
Willy Tarreau8280d642009-09-23 23:37:52 +02001391
Willy Tarreaue6945732016-12-21 19:57:00 +01001392 list_for_each_entry(pcf, &post_check_list, list) {
1393 err_code |= pcf->fct();
1394 if (err_code & (ERR_ABORT|ERR_FATAL))
1395 exit(1);
1396 }
1397
Willy Tarreaubaaee002006-06-26 02:48:02 +02001398 if (cfg_maxconn > 0)
1399 global.maxconn = cfg_maxconn;
1400
1401 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +02001402 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001403 global.pidfile = strdup(cfg_pidfile);
1404 }
1405
Willy Tarreaud0256482015-01-15 21:45:22 +01001406 /* Now we want to compute the maxconn and possibly maxsslconn values.
1407 * It's a bit tricky. If memmax is not set, maxconn defaults to
1408 * DEFAULT_MAXCONN and maxsslconn defaults to DEFAULT_MAXSSLCONN.
1409 *
1410 * If memmax is set, then it depends on which values are set. If
1411 * maxsslconn is set, we use memmax to determine how many cleartext
1412 * connections may be added, and set maxconn to the sum of the two.
1413 * If maxconn is set and not maxsslconn, maxsslconn is computed from
1414 * the remaining amount of memory between memmax and the cleartext
1415 * connections. If neither are set, then it is considered that all
1416 * connections are SSL-capable, and maxconn is computed based on this,
1417 * then maxsslconn accordingly. We need to know if SSL is used on the
1418 * frontends, backends, or both, because when it's used on both sides,
1419 * we need twice the value for maxsslconn, but we only count the
1420 * handshake once since it is not performed on the two sides at the
1421 * same time (frontend-side is terminated before backend-side begins).
1422 * The SSL stack is supposed to have filled ssl_session_cost and
Willy Tarreau474b96a2015-01-28 19:03:21 +01001423 * ssl_handshake_cost during its initialization. In any case, if
1424 * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for
1425 * maxconn in order to protect the system.
Willy Tarreaud0256482015-01-15 21:45:22 +01001426 */
1427 if (!global.rlimit_memmax) {
1428 if (global.maxconn == 0) {
1429 global.maxconn = DEFAULT_MAXCONN;
1430 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1431 fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn);
1432 }
1433 }
1434#ifdef USE_OPENSSL
1435 else if (!global.maxconn && !global.maxsslconn &&
1436 (global.ssl_used_frontend || global.ssl_used_backend)) {
1437 /* memmax is set, compute everything automatically. Here we want
1438 * to ensure that all SSL connections will be served. We take
1439 * care of the number of sides where SSL is used, and consider
1440 * the worst case : SSL used on both sides and doing a handshake
1441 * simultaneously. Note that we can't have more than maxconn
1442 * handshakes at a time by definition, so for the worst case of
1443 * two SSL conns per connection, we count a single handshake.
1444 */
1445 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1446 int64_t mem = global.rlimit_memmax * 1048576ULL;
1447
1448 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1449 mem -= global.maxzlibmem;
1450 mem = mem * MEM_USABLE_RATIO;
1451
1452 global.maxconn = mem /
Willy Tarreau87b09662015-04-03 00:22:06 +02001453 ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream
Willy Tarreaud0256482015-01-15 21:45:22 +01001454 sides * global.ssl_session_max_cost + // SSL buffers, one per side
1455 global.ssl_handshake_max_cost); // 1 handshake per connection max
1456
1457 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001458#ifdef SYSTEM_MAXCONN
1459 if (global.maxconn > DEFAULT_MAXCONN)
1460 global.maxconn = DEFAULT_MAXCONN;
1461#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001462 global.maxsslconn = sides * global.maxconn;
1463 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1464 fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n",
1465 global.maxconn, global.maxsslconn);
1466 }
1467 else if (!global.maxsslconn &&
1468 (global.ssl_used_frontend || global.ssl_used_backend)) {
1469 /* memmax and maxconn are known, compute maxsslconn automatically.
1470 * maxsslconn being forced, we don't know how many of it will be
1471 * on each side if both sides are being used. The worst case is
1472 * when all connections use only one SSL instance because
1473 * handshakes may be on two sides at the same time.
1474 */
1475 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1476 int64_t mem = global.rlimit_memmax * 1048576ULL;
1477 int64_t sslmem;
1478
1479 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1480 mem -= global.maxzlibmem;
1481 mem = mem * MEM_USABLE_RATIO;
1482
Willy Tarreau87b09662015-04-03 00:22:06 +02001483 sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001484 global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost);
1485 global.maxsslconn = round_2dig(global.maxsslconn);
1486
1487 if (sslmem <= 0 || global.maxsslconn < sides) {
1488 Alert("Cannot compute the automatic maxsslconn because global.maxconn is already too "
1489 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1490 "without SSL is %d, but %d was found and SSL is in use.\n",
1491 global.rlimit_memmax,
Willy Tarreau87b09662015-04-03 00:22:06 +02001492 (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)),
Willy Tarreaud0256482015-01-15 21:45:22 +01001493 global.maxconn);
1494 exit(1);
1495 }
1496
1497 if (global.maxsslconn > sides * global.maxconn)
1498 global.maxsslconn = sides * global.maxconn;
1499
1500 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1501 fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn);
1502 }
1503#endif
1504 else if (!global.maxconn) {
1505 /* memmax and maxsslconn are known/unused, compute maxconn automatically */
1506 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1507 int64_t mem = global.rlimit_memmax * 1048576ULL;
1508 int64_t clearmem;
1509
1510 if (global.ssl_used_frontend || global.ssl_used_backend)
1511 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1512
1513 mem -= global.maxzlibmem;
1514 mem = mem * MEM_USABLE_RATIO;
1515
1516 clearmem = mem;
1517 if (sides)
1518 clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn;
1519
Willy Tarreau87b09662015-04-03 00:22:06 +02001520 global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001521 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001522#ifdef SYSTEM_MAXCONN
1523 if (global.maxconn > DEFAULT_MAXCONN)
1524 global.maxconn = DEFAULT_MAXCONN;
1525#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001526
1527 if (clearmem <= 0 || !global.maxconn) {
1528 Alert("Cannot compute the automatic maxconn because global.maxsslconn is already too "
1529 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1530 "is %d, but %d was found.\n",
1531 global.rlimit_memmax,
1532 (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)),
1533 global.maxsslconn);
1534 exit(1);
1535 }
1536
1537 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
1538 if (sides && global.maxsslconn > sides * global.maxconn) {
1539 fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn "
1540 "to be limited to %d. Better reduce global.maxsslconn to get more "
1541 "room for extra connections.\n", global.maxsslconn, global.maxconn);
1542 }
1543 fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn);
1544 }
1545 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001546
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001547 if (!global.maxpipes) {
1548 /* maxpipes not specified. Count how many frontends and backends
1549 * may be using splicing, and bound that to maxconn.
1550 */
1551 struct proxy *cur;
1552 int nbfe = 0, nbbe = 0;
1553
1554 for (cur = proxy; cur; cur = cur->next) {
1555 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
1556 if (cur->cap & PR_CAP_FE)
1557 nbfe += cur->maxconn;
1558 if (cur->cap & PR_CAP_BE)
Willy Tarreauafb48762009-01-25 10:42:05 +01001559 nbbe += cur->fullconn ? cur->fullconn : global.maxconn;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001560 }
1561 }
1562 global.maxpipes = MAX(nbfe, nbbe);
1563 if (global.maxpipes > global.maxconn)
1564 global.maxpipes = global.maxconn;
Willy Tarreau686ac822009-01-25 14:06:58 +01001565 global.maxpipes /= 4;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001566 }
1567
1568
Willy Tarreauabacc2c2011-09-07 14:26:33 +02001569 global.hardmaxconn = global.maxconn; /* keep this max value */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001570 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
Willy Tarreau3ec79b92009-01-18 20:39:42 +01001571 global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001572
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001573 if (global.stats_fe)
1574 global.maxsock += global.stats_fe->maxconn;
1575
1576 if (peers) {
1577 /* peers also need to bypass global maxconn */
1578 struct peers *p = peers;
1579
1580 for (p = peers; p; p = p->next)
1581 if (p->peers_fe)
1582 global.maxsock += p->peers_fe->maxconn;
1583 }
1584
Willy Tarreau1db37712007-06-03 17:16:49 +02001585 if (global.tune.maxpollevents <= 0)
1586 global.tune.maxpollevents = MAX_POLL_EVENTS;
1587
Willy Tarreau6f4a82c2009-03-21 20:43:57 +01001588 if (global.tune.recv_enough == 0)
1589 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
1590
Willy Tarreau27097842015-09-28 13:53:23 +02001591 if (global.tune.maxrewrite < 0)
1592 global.tune.maxrewrite = MAXREWRITE;
1593
Willy Tarreau27a674e2009-08-17 07:23:33 +02001594 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
1595 global.tune.maxrewrite = global.tune.bufsize / 2;
1596
Willy Tarreaubaaee002006-06-26 02:48:02 +02001597 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
1598 /* command line debug mode inhibits configuration mode */
William Lallemand095ba4c2017-06-01 17:38:50 +02001599 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001600 global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND));
1601 }
1602
William Lallemand095ba4c2017-06-01 17:38:50 +02001603 if (arg_mode & MODE_DAEMON) {
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001604 /* command line daemon mode inhibits foreground and debug modes mode */
1605 global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND);
William Lallemand095ba4c2017-06-01 17:38:50 +02001606 global.mode |= arg_mode & MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001607 }
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001608
1609 global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001610
William Lallemand095ba4c2017-06-01 17:38:50 +02001611 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
1612 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
1613 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001614 }
1615
William Lallemand095ba4c2017-06-01 17:38:50 +02001616 if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001617 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
1618 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
1619 global.nbproc = 1;
1620 }
1621
1622 if (global.nbproc < 1)
1623 global.nbproc = 1;
1624
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001625 swap_buffer = calloc(1, global.tune.bufsize);
1626 get_http_auth_buff = calloc(1, global.tune.bufsize);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02001627 static_table_key = calloc(1, sizeof(*static_table_key));
Willy Tarreau8096de92010-02-26 11:12:27 +01001628
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001629 fdinfo = calloc(1, sizeof(struct fdinfo) * (global.maxsock));
1630 fdtab = calloc(1, sizeof(struct fdtab) * (global.maxsock));
Willy Tarreauef1d1f82007-04-16 00:25:25 +02001631 /*
1632 * Note: we could register external pollers here.
1633 * Built-in pollers have been registered before main().
1634 */
Willy Tarreau4f60f162007-04-08 16:39:58 +02001635
Willy Tarreau43b78992009-01-25 15:42:27 +01001636 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001637 disable_poller("kqueue");
1638
Willy Tarreau43b78992009-01-25 15:42:27 +01001639 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001640 disable_poller("epoll");
1641
Willy Tarreau43b78992009-01-25 15:42:27 +01001642 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001643 disable_poller("poll");
1644
Willy Tarreau43b78992009-01-25 15:42:27 +01001645 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001646 disable_poller("select");
1647
1648 /* Note: we could disable any poller by name here */
1649
Christopher Fauletb3f4e142016-03-07 12:46:38 +01001650 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +02001651 list_pollers(stderr);
Christopher Fauletb3f4e142016-03-07 12:46:38 +01001652 fprintf(stderr, "\n");
1653 list_filters(stderr);
1654 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02001655
Willy Tarreau4f60f162007-04-08 16:39:58 +02001656 if (!init_pollers()) {
Willy Tarreau3fa87b12013-03-31 14:41:15 +02001657 Alert("No polling mechanism available.\n"
1658 " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n"
1659 " is too low on this platform to support maxconn and the number of listeners\n"
1660 " and servers. You should rebuild haproxy specifying your system using TARGET=\n"
1661 " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n"
Prach Pongpanichb837e682013-05-14 20:56:28 +02001662 " global maxconn setting to accommodate the system's limitation. For reference,\n"
Willy Tarreau3fa87b12013-03-31 14:41:15 +02001663 " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n"
1664 " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n"
1665 " check build settings using 'haproxy -vv'.\n\n",
1666 FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001667 exit(1);
1668 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02001669 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
1670 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001671 }
1672
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001673 if (!global.node)
1674 global.node = strdup(hostname);
1675
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01001676 if (!hlua_post_init())
1677 exit(1);
Thomas Holmes6abded42015-05-12 16:23:58 +01001678
Baptiste Assmann325137d2015-04-13 23:40:55 +02001679 /* initialize structures for name resolution */
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001680 if (!dns_init_resolvers(0))
Baptiste Assmann325137d2015-04-13 23:40:55 +02001681 exit(1);
Maxime de Roucy0f503922016-05-13 23:52:55 +02001682
1683 free(err_msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001684}
1685
Simon Horman6fb82592011-07-15 13:14:11 +09001686static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +09001687{
Simon Hormanac821422011-07-15 13:14:09 +09001688 struct acl_term_suite *suite, *suiteb;
1689 struct acl_term *term, *termb;
1690
Simon Horman6fb82592011-07-15 13:14:11 +09001691 if (!cond)
1692 return;
1693
1694 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
1695 list_for_each_entry_safe(term, termb, &suite->terms, list) {
1696 LIST_DEL(&term->list);
1697 free(term);
Simon Hormanac821422011-07-15 13:14:09 +09001698 }
Simon Horman6fb82592011-07-15 13:14:11 +09001699 LIST_DEL(&suite->list);
1700 free(suite);
1701 }
1702
1703 free(cond);
1704}
1705
1706static void deinit_tcp_rules(struct list *rules)
1707{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001708 struct act_rule *trule, *truleb;
Simon Horman6fb82592011-07-15 13:14:11 +09001709
1710 list_for_each_entry_safe(trule, truleb, rules, list) {
Simon Hormanac821422011-07-15 13:14:09 +09001711 LIST_DEL(&trule->list);
Simon Horman6fb82592011-07-15 13:14:11 +09001712 deinit_acl_cond(trule->cond);
Simon Hormanac821422011-07-15 13:14:09 +09001713 free(trule);
1714 }
1715}
1716
Simon Horman6fb82592011-07-15 13:14:11 +09001717static void deinit_stick_rules(struct list *rules)
1718{
1719 struct sticking_rule *rule, *ruleb;
1720
1721 list_for_each_entry_safe(rule, ruleb, rules, list) {
1722 LIST_DEL(&rule->list);
1723 deinit_acl_cond(rule->cond);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001724 release_sample_expr(rule->expr);
Simon Horman6fb82592011-07-15 13:14:11 +09001725 free(rule);
1726 }
1727}
1728
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001729void deinit(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001730{
Willy Tarreau4d2d0982007-05-14 00:39:29 +02001731 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001732 struct cap_hdr *h,*h_next;
1733 struct server *s,*s_next;
1734 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001735 struct acl_cond *cond, *condb;
1736 struct hdr_exp *exp, *expb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001737 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001738 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001739 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001740 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001741 struct wordlist *wl, *wlb;
Willy Tarreauf4f04122010-01-28 18:10:50 +01001742 struct cond_wordlist *cwl, *cwlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001743 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +02001744 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +01001745 struct logformat_node *lf, *lfb;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001746 struct bind_conf *bind_conf, *bind_back;
Willy Tarreaucdb737e2016-12-21 18:43:10 +01001747 struct build_opts_str *bol, *bolb;
Willy Tarreau05554e62016-12-21 20:46:26 +01001748 struct post_deinit_fct *pdf;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001749 int i;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001750
Willy Tarreau24f4efa2010-08-27 17:56:48 +02001751 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001752 while (p) {
Willy Tarreau8113a5d2012-10-04 08:01:43 +02001753 free(p->conf.file);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001754 free(p->id);
1755 free(p->check_req);
1756 free(p->cookie_name);
1757 free(p->cookie_domain);
1758 free(p->url_param_name);
1759 free(p->capture_name);
1760 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +09001761 free(p->rdp_cookie_name);
Willy Tarreau62a61232013-04-12 18:13:46 +02001762 if (p->conf.logformat_string != default_http_log_format &&
1763 p->conf.logformat_string != default_tcp_log_format &&
1764 p->conf.logformat_string != clf_http_log_format)
1765 free(p->conf.logformat_string);
Willy Tarreau196729e2012-05-31 19:30:26 +02001766
Willy Tarreau62a61232013-04-12 18:13:46 +02001767 free(p->conf.lfs_file);
1768 free(p->conf.uniqueid_format_string);
1769 free(p->conf.uif_file);
Godbachaf1a75d2013-10-02 17:10:11 +08001770 free(p->lbprm.map.srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001771
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001772 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
1773 free(p->conf.logformat_sd_string);
1774 free(p->conf.lfsd_file);
1775
Willy Tarreaua534fea2008-08-03 12:19:50 +02001776 for (i = 0; i < HTTP_ERR_SIZE; i++)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001777 chunk_destroy(&p->errmsg[i]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001778
Willy Tarreauf4f04122010-01-28 18:10:50 +01001779 list_for_each_entry_safe(cwl, cwlb, &p->req_add, list) {
1780 LIST_DEL(&cwl->list);
1781 free(cwl->s);
1782 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001783 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001784
Willy Tarreauf4f04122010-01-28 18:10:50 +01001785 list_for_each_entry_safe(cwl, cwlb, &p->rsp_add, list) {
1786 LIST_DEL(&cwl->list);
1787 free(cwl->s);
1788 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001789 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001790
Willy Tarreaub80c2302007-11-30 20:51:32 +01001791 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
1792 LIST_DEL(&cond->list);
1793 prune_acl_cond(cond);
1794 free(cond);
1795 }
1796
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001797 for (exp = p->req_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001798 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02001799 regex_free(exp->preg);
1800 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001801 }
1802
Willy Tarreau98d04852015-05-26 12:18:29 +02001803 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001804 expb = exp;
1805 exp = exp->next;
1806 free(expb);
1807 }
1808
1809 for (exp = p->rsp_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001810 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02001811 regex_free(exp->preg);
1812 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001813 }
1814
Willy Tarreau98d04852015-05-26 12:18:29 +02001815 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001816 expb = exp;
1817 exp = exp->next;
1818 free(expb);
1819 }
1820
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001821 /* build a list of unique uri_auths */
1822 if (!ua)
1823 ua = p->uri_auth;
1824 else {
1825 /* check if p->uri_auth is unique */
1826 for (uap = ua; uap; uap=uap->next)
1827 if (uap == p->uri_auth)
1828 break;
1829
Willy Tarreauaccc4e12008-06-24 11:14:45 +02001830 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001831 /* add it, if it is */
1832 p->uri_auth->next = ua;
1833 ua = p->uri_auth;
1834 }
1835 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001836
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001837 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
1838 LIST_DEL(&acl->list);
1839 prune_acl(acl);
1840 free(acl);
1841 }
1842
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001843 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
1844 LIST_DEL(&srule->list);
1845 prune_acl_cond(srule->cond);
1846 free(srule->cond);
1847 free(srule);
1848 }
1849
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001850 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
1851 LIST_DEL(&rule->list);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001852 if (rule->cond) {
1853 prune_acl_cond(rule->cond);
1854 free(rule->cond);
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +01001855 free(rule->file);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001856 }
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001857 free(rule);
1858 }
1859
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001860 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
1861 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +01001862 if (rdr->cond) {
1863 prune_acl_cond(rdr->cond);
1864 free(rdr->cond);
1865 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001866 free(rdr->rdr_str);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01001867 list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) {
1868 LIST_DEL(&lf->list);
1869 free(lf);
1870 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001871 free(rdr);
1872 }
1873
William Lallemand0f99e342011-10-12 17:50:54 +02001874 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
1875 LIST_DEL(&log->list);
1876 free(log);
1877 }
1878
William Lallemand723b73a2012-02-08 16:37:49 +01001879 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
1880 LIST_DEL(&lf->list);
1881 free(lf);
1882 }
1883
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001884 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
1885 LIST_DEL(&lf->list);
1886 free(lf);
1887 }
1888
Simon Hormanac821422011-07-15 13:14:09 +09001889 deinit_tcp_rules(&p->tcp_req.inspect_rules);
1890 deinit_tcp_rules(&p->tcp_req.l4_rules);
1891
Simon Horman6fb82592011-07-15 13:14:11 +09001892 deinit_stick_rules(&p->storersp_rules);
1893 deinit_stick_rules(&p->sticking_rules);
1894
Willy Tarreaubaaee002006-06-26 02:48:02 +02001895 h = p->req_cap;
1896 while (h) {
1897 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001898 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001899 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001900 free(h);
1901 h = h_next;
1902 }/* end while(h) */
1903
1904 h = p->rsp_cap;
1905 while (h) {
1906 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001907 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001908 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001909 free(h);
1910 h = h_next;
1911 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001912
Willy Tarreaubaaee002006-06-26 02:48:02 +02001913 s = p->srv;
1914 while (s) {
1915 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001916
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001917 if (s->check.task) {
1918 task_delete(s->check.task);
1919 task_free(s->check.task);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001920 }
Simon Hormand60d6912013-11-25 10:46:36 +09001921 if (s->agent.task) {
1922 task_delete(s->agent.task);
1923 task_free(s->agent.task);
1924 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001925
Willy Tarreau2e993902011-10-31 11:53:20 +01001926 if (s->warmup) {
1927 task_delete(s->warmup);
1928 task_free(s->warmup);
1929 }
1930
Willy Tarreaua534fea2008-08-03 12:19:50 +02001931 free(s->id);
1932 free(s->cookie);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001933 free(s->check.bi);
1934 free(s->check.bo);
Simon Hormand60d6912013-11-25 10:46:36 +09001935 free(s->agent.bi);
1936 free(s->agent.bo);
James Brown55f9ff12015-10-21 18:19:05 -07001937 free(s->agent.send_string);
Sárközi, László34c01792014-09-05 10:08:23 +02001938 free((char*)s->conf.file);
Willy Tarreau17d45382016-12-22 21:16:08 +01001939
1940 if (s->use_ssl || s->check.use_ssl) {
1941 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
1942 xprt_get(XPRT_SSL)->destroy_srv(s);
1943 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001944 free(s);
1945 s = s_next;
1946 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001947
Willy Tarreau4348fad2012-09-20 16:48:07 +02001948 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02001949 /*
1950 * Zombie proxy, the listener just pretend to be up
1951 * because they still hold an opened fd.
1952 * Close it and give the listener its real state.
1953 */
1954 if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
1955 close(l->fd);
1956 l->state = LI_INIT;
1957 }
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02001958 unbind_listener(l);
1959 delete_listener(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02001960 LIST_DEL(&l->by_fe);
1961 LIST_DEL(&l->by_bind);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01001962 free(l->name);
1963 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001964 free(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02001965 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001966
Willy Tarreau4348fad2012-09-20 16:48:07 +02001967 /* Release unused SSL configs. */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001968 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01001969 if (bind_conf->xprt->destroy_bind_conf)
1970 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001971 free(bind_conf->file);
1972 free(bind_conf->arg);
1973 LIST_DEL(&bind_conf->by_fe);
1974 free(bind_conf);
1975 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02001976
Christopher Fauletd7c91962015-04-30 11:48:27 +02001977 flt_deinit(p);
1978
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01001979 free(p->desc);
1980 free(p->fwdfor_hdr_name);
1981
Willy Tarreauff011f22011-01-06 17:51:27 +01001982 free_http_req_rules(&p->http_req_rules);
Sasha Pachev218f0642014-06-16 12:05:59 -06001983 free_http_res_rules(&p->http_res_rules);
Willy Tarreau918ff602011-07-25 16:33:49 +02001984 free(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01001985
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001986 pool_destroy2(p->req_cap_pool);
1987 pool_destroy2(p->rsp_cap_pool);
Simon Hormanb08584a2011-07-15 13:14:10 +09001988 pool_destroy2(p->table.pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001989
Willy Tarreau4d2d0982007-05-14 00:39:29 +02001990 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001991 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +02001992 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001993 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02001994
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001995 while (ua) {
1996 uap = ua;
1997 ua = ua->next;
1998
Willy Tarreaua534fea2008-08-03 12:19:50 +02001999 free(uap->uri_prefix);
2000 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002001 free(uap->node);
2002 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002003
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002004 userlist_free(uap->userlist);
Willy Tarreauff011f22011-01-06 17:51:27 +01002005 free_http_req_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002006
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002007 free(uap);
2008 }
2009
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002010 userlist_free(userlist);
2011
David Carlier834cb2e2015-09-25 12:02:25 +01002012 cfg_unregister_sections();
2013
2014 free_trash_buffers();
2015 chunk_destroy(&trash);
2016
Willy Tarreaudd815982007-10-16 12:25:14 +02002017 protocol_unbind_all();
2018
Willy Tarreau05554e62016-12-21 20:46:26 +01002019 list_for_each_entry(pdf, &post_deinit_list, list)
2020 pdf->fct();
2021
Joe Williamsdf5b38f2010-12-29 17:05:48 +01002022 free(global.log_send_hostname); global.log_send_hostname = NULL;
Dragan Dosen43885c72015-10-01 13:18:13 +02002023 chunk_destroy(&global.log_tag);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002024 free(global.chroot); global.chroot = NULL;
2025 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002026 free(global.node); global.node = NULL;
2027 free(global.desc); global.desc = NULL;
Godbach4cc1b0d2013-06-26 16:49:51 +08002028 free(fdinfo); fdinfo = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002029 free(fdtab); fdtab = NULL;
2030 free(oldpids); oldpids = NULL;
David Carlier834cb2e2015-09-25 12:02:25 +01002031 free(static_table_key); static_table_key = NULL;
2032 free(get_http_auth_buff); get_http_auth_buff = NULL;
2033 free(swap_buffer); swap_buffer = NULL;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002034 free(global_listener_queue_task); global_listener_queue_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002035
William Lallemand0f99e342011-10-12 17:50:54 +02002036 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
2037 LIST_DEL(&log->list);
2038 free(log);
2039 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01002040 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02002041 free(wl->s);
Willy Tarreau477ecd82010-01-03 21:12:30 +01002042 LIST_DEL(&wl->list);
2043 free(wl);
2044 }
2045
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002046 list_for_each_entry_safe(bol, bolb, &build_opts_list, list) {
2047 if (bol->must_free)
2048 free((void *)bol->str);
2049 LIST_DEL(&bol->list);
2050 free(bol);
2051 }
2052
Christopher Fauletff2613e2016-11-09 11:36:17 +01002053 vars_prune(&global.vars, NULL, NULL);
2054
Willy Tarreau87b09662015-04-03 00:22:06 +02002055 pool_destroy2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +02002056 pool_destroy2(pool2_session);
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002057 pool_destroy2(pool2_connection);
Willy Tarreaub686afd2017-02-08 11:06:11 +01002058 pool_destroy2(pool2_trash);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002059 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002060 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02002061 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +02002062 pool_destroy2(pool2_capture);
Willy Tarreaue4d7e552007-05-13 20:19:55 +02002063 pool_destroy2(pool2_pendconn);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002064 pool_destroy2(pool2_sig_handlers);
Willy Tarreau34eb6712011-10-24 18:15:04 +02002065 pool_destroy2(pool2_hdr_idx);
Willy Tarreau63986c72015-04-03 22:55:33 +02002066 pool_destroy2(pool2_http_txn);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002067 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002068} /* end deinit() */
2069
Willy Tarreaubaaee002006-06-26 02:48:02 +02002070
Willy Tarreau918ff602011-07-25 16:33:49 +02002071/* Runs the polling loop */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01002072static void run_poll_loop()
Willy Tarreau4f60f162007-04-08 16:39:58 +02002073{
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002074 int next;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002075
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002076 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002077 while (1) {
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002078 /* Process a few tasks */
2079 process_runnable_tasks();
2080
Willy Tarreau29857942009-05-10 09:01:21 +02002081 /* check if we caught some signals and process them */
2082 signal_process_queue();
2083
Willy Tarreau58b458d2008-06-29 22:40:23 +02002084 /* Check if we can expire some tasks */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002085 next = wake_expired_tasks();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002086
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002087 /* stop when there's nothing left to do */
2088 if (jobs == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +02002089 break;
2090
Willy Tarreau10146c92015-04-13 20:44:19 +02002091 /* expire immediately if events are pending */
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002092 if (fd_cache_num || tasks_run_queue || signal_queue_len || applets_active_queue)
Willy Tarreau10146c92015-04-13 20:44:19 +02002093 next = now_ms;
2094
Willy Tarreau58b458d2008-06-29 22:40:23 +02002095 /* The poller will ensure it returns around <next> */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002096 cur_poller.poll(&cur_poller, next);
Willy Tarreau033cd9d2014-01-25 19:24:15 +01002097 fd_process_cached_events();
Willy Tarreau3c595ac2015-04-19 09:59:31 +02002098 applet_run_active();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002099 }
2100}
2101
Willy Tarreaue9b26022011-08-01 20:57:55 +02002102/* This is the global management task for listeners. It enables listeners waiting
2103 * for global resources when there are enough free resource, or at least once in
2104 * a while. It is designed to be called as a task.
2105 */
2106static struct task *manage_global_listener_queue(struct task *t)
2107{
2108 int next = TICK_ETERNITY;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002109 /* queue is empty, nothing to do */
2110 if (LIST_ISEMPTY(&global_listener_queue))
2111 goto out;
2112
2113 /* If there are still too many concurrent connections, let's wait for
2114 * some of them to go away. We don't need to re-arm the timer because
2115 * each of them will scan the queue anyway.
2116 */
2117 if (unlikely(actconn >= global.maxconn))
2118 goto out;
2119
2120 /* We should periodically try to enable listeners waiting for a global
2121 * resource here, because it is possible, though very unlikely, that
2122 * they have been blocked by a temporary lack of global resource such
2123 * as a file descriptor or memory and that the temporary condition has
2124 * disappeared.
2125 */
Willy Tarreauabacc2c2011-09-07 14:26:33 +02002126 dequeue_all_listeners(&global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +02002127
2128 out:
2129 t->expire = next;
2130 task_queue(t);
2131 return t;
2132}
Willy Tarreau4f60f162007-04-08 16:39:58 +02002133
Willy Tarreaubaaee002006-06-26 02:48:02 +02002134int main(int argc, char **argv)
2135{
2136 int err, retry;
2137 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02002138 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02002139 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002140
Emeric Bruncf20bf12010-10-22 16:06:11 +02002141 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002142 signal_register_fct(SIGQUIT, dump, SIGQUIT);
2143 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
2144 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
William Lallemand73b85e72017-06-01 17:38:51 +02002145 signal_register_fct(SIGUSR2, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002146
Willy Tarreaue437c442010-03-17 18:02:46 +01002147 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
2148 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
2149 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002150 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002151 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002152
Willy Tarreaudc23a922011-02-16 11:10:36 +01002153 /* ulimits */
2154 if (!global.rlimit_nofile)
2155 global.rlimit_nofile = global.maxsock;
2156
2157 if (global.rlimit_nofile) {
2158 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
2159 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
Willy Tarreauef635472016-06-21 11:48:18 +02002160 /* try to set it to the max possible at least */
2161 getrlimit(RLIMIT_NOFILE, &limit);
Willy Tarreau164dd0b2016-06-21 11:51:59 +02002162 limit.rlim_cur = limit.rlim_max;
2163 if (setrlimit(RLIMIT_NOFILE, &limit) != -1)
2164 getrlimit(RLIMIT_NOFILE, &limit);
2165
Willy Tarreauef635472016-06-21 11:48:18 +02002166 Warning("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n", argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
2167 global.rlimit_nofile = limit.rlim_cur;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002168 }
2169 }
2170
2171 if (global.rlimit_memmax) {
2172 limit.rlim_cur = limit.rlim_max =
Willy Tarreau70060452015-12-14 12:46:07 +01002173 global.rlimit_memmax * 1048576ULL;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002174#ifdef RLIMIT_AS
2175 if (setrlimit(RLIMIT_AS, &limit) == -1) {
2176 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2177 argv[0], global.rlimit_memmax);
2178 }
2179#else
2180 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
2181 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2182 argv[0], global.rlimit_memmax);
2183 }
2184#endif
2185 }
2186
Olivier Houchardf73629d2017-04-05 22:33:04 +02002187 if (old_unixsocket) {
2188 if (get_old_sockets(old_unixsocket) != 0) {
2189 Alert("Failed to get the sockets from the old process!\n");
2190 exit(1);
2191 }
2192 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002193 /* We will loop at most 100 times with 10 ms delay each time.
2194 * That's at most 1 second. We only send a signal to old pids
2195 * if we cannot grab at least one port.
2196 */
2197 retry = MAX_START_RETRIES;
2198 err = ERR_NONE;
2199 while (retry >= 0) {
2200 struct timeval w;
2201 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01002202 /* exit the loop on no error or fatal error */
2203 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002204 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02002205 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002206 break;
2207
2208 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
2209 * listening sockets. So on those platforms, it would be wiser to
2210 * simply send SIGUSR1, which will not be undoable.
2211 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02002212 if (tell_old_pids(SIGTTOU) == 0) {
2213 /* no need to wait if we can't contact old pids */
2214 retry = 0;
2215 continue;
2216 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002217 /* give some time to old processes to stop listening */
2218 w.tv_sec = 0;
2219 w.tv_usec = 10*1000;
2220 select(0, NULL, NULL, NULL, &w);
2221 retry--;
2222 }
2223
2224 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01002225 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02002226 if (retry != MAX_START_RETRIES && nb_oldpids) {
2227 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002228 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02002229 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002230 exit(1);
2231 }
2232
2233 if (listeners == 0) {
Willy Tarreauf2ee0162015-08-09 11:01:51 +02002234 Alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002235 /* Note: we don't have to send anything to the old pids because we
2236 * never stopped them. */
2237 exit(1);
2238 }
2239
Emeric Bruncf20bf12010-10-22 16:06:11 +02002240 err = protocol_bind_all(errmsg, sizeof(errmsg));
2241 if ((err & ~ERR_WARN) != ERR_NONE) {
2242 if ((err & ERR_ALERT) || (err & ERR_WARN))
2243 Alert("[%s.main()] %s.\n", argv[0], errmsg);
2244
Willy Tarreaudd815982007-10-16 12:25:14 +02002245 Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
2246 protocol_unbind_all(); /* cleanup everything we can */
2247 if (nb_oldpids)
2248 tell_old_pids(SIGTTIN);
2249 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002250 } else if (err & ERR_WARN) {
2251 Alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02002252 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002253 /* Ok, all listener should now be bound, close any leftover sockets
2254 * the previous process gave us, we don't need them anymore
2255 */
2256 while (xfer_sock_list != NULL) {
2257 struct xfer_sock_list *tmpxfer = xfer_sock_list->next;
2258 close(xfer_sock_list->fd);
2259 free(xfer_sock_list->iface);
2260 free(xfer_sock_list->namespace);
2261 free(xfer_sock_list);
2262 xfer_sock_list = tmpxfer;
2263 }
Willy Tarreaudd815982007-10-16 12:25:14 +02002264
Willy Tarreaubaaee002006-06-26 02:48:02 +02002265 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002266 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
2267 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002268
Willy Tarreaubaaee002006-06-26 02:48:02 +02002269 /* MODE_QUIET can inhibit alerts and warnings below this line */
2270
2271 global.mode &= ~MODE_STARTING;
2272 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
2273 /* detach from the tty */
2274 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002275 }
2276
2277 /* open log & pid files before the chroot */
William Lallemand095ba4c2017-06-01 17:38:50 +02002278 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002279 unlink(global.pidfile);
2280 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
2281 if (pidfd < 0) {
2282 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
2283 if (nb_oldpids)
2284 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02002285 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002286 exit(1);
2287 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002288 }
2289
Willy Tarreaub38651a2007-03-24 17:24:39 +01002290 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
2291 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002292 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002293 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01002294 exit(1);
2295 }
2296
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002297 /* If the user is not root, we'll still let him try the configuration
2298 * but we inform him that unexpected behaviour may occur.
2299 */
2300 if ((global.last_checks & LSTCHK_NETADM) && getuid())
2301 Warning("[%s.main()] Some options which require full privileges"
2302 " might not work well.\n"
2303 "", argv[0]);
2304
William Lallemand095ba4c2017-06-01 17:38:50 +02002305 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2306
2307 /* chroot if needed */
2308 if (global.chroot != NULL) {
2309 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
2310 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
2311 if (nb_oldpids)
2312 tell_old_pids(SIGTTIN);
2313 protocol_unbind_all();
2314 exit(1);
2315 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002316 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002317 }
2318
Willy Tarreaubaaee002006-06-26 02:48:02 +02002319 if (nb_oldpids)
Willy Tarreaubb545b42010-08-25 12:58:59 +02002320 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002321
2322 /* Note that any error at this stage will be fatal because we will not
2323 * be able to restart the old pids.
2324 */
2325
William Lallemand095ba4c2017-06-01 17:38:50 +02002326 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2327 /* setgid / setuid */
2328 if (global.gid) {
2329 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
2330 Warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2331 " without 'uid'/'user' is generally useless.\n", argv[0]);
2332
2333 if (setgid(global.gid) == -1) {
2334 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
2335 protocol_unbind_all();
2336 exit(1);
2337 }
2338 }
Michael Schererab012dd2013-01-12 18:35:19 +01002339
William Lallemand095ba4c2017-06-01 17:38:50 +02002340 if (global.uid && setuid(global.uid) == -1) {
2341 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Michael Schererab012dd2013-01-12 18:35:19 +01002342 protocol_unbind_all();
2343 exit(1);
2344 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002345 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002346 /* check ulimits */
2347 limit.rlim_cur = limit.rlim_max = 0;
2348 getrlimit(RLIMIT_NOFILE, &limit);
2349 if (limit.rlim_cur < global.maxsock) {
2350 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 +02002351 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002352 }
2353
William Lallemand095ba4c2017-06-01 17:38:50 +02002354 if (global.mode & (MODE_DAEMON | MODE_MWORKER)) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002355 struct proxy *px;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002356 struct peers *curpeers;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002357 int ret = 0;
2358 int proc;
2359
William Lallemand73b85e72017-06-01 17:38:51 +02002360 children = calloc(global.nbproc, sizeof(int));
William Lallemand095ba4c2017-06-01 17:38:50 +02002361 /*
2362 * if daemon + mworker: must fork here to let a master
2363 * process live in background before forking children
2364 */
William Lallemand73b85e72017-06-01 17:38:51 +02002365
2366 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
2367 && (global.mode & MODE_MWORKER)
2368 && (global.mode & MODE_DAEMON)) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002369 ret = fork();
2370 if (ret < 0) {
2371 Alert("[%s.main()] Cannot fork.\n", argv[0]);
2372 protocol_unbind_all();
2373 exit(1); /* there has been an error */
2374 }
2375 /* parent leave to daemonize */
2376 if (ret > 0)
2377 exit(0);
2378 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002379 /* the father launches the required number of processes */
2380 for (proc = 0; proc < global.nbproc; proc++) {
2381 ret = fork();
2382 if (ret < 0) {
2383 Alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002384 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +02002385 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002386 }
2387 else if (ret == 0) /* child breaks here */
2388 break;
Marc-Antoine Perennou992709b2013-02-12 10:53:52 +01002389 children[proc] = ret;
Willy Tarreau269ab312012-09-05 08:02:48 +02002390 if (pidfd >= 0) {
2391 char pidstr[100];
2392 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002393 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002394 }
Willy Tarreaudcd47712007-11-04 23:35:08 +01002395 relative_pid++; /* each child will get a different one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002396 }
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002397
2398#ifdef USE_CPU_AFFINITY
2399 if (proc < global.nbproc && /* child */
Willy Tarreaue7597492015-04-20 11:36:57 +02002400 proc < LONGBITS && /* only the first 32/64 processes may be pinned */
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002401 global.cpu_map[proc]) /* only do this if the process has a CPU map */
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002402#ifdef __FreeBSD__
2403 cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(unsigned long), (void *)&global.cpu_map[proc]);
2404#else
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002405 sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map[proc]);
2406#endif
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002407#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002408 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02002409 if (pidfd >= 0) {
2410 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
2411 close(pidfd);
2412 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02002413
2414 /* We won't ever use this anymore */
Willy Tarreaud137dd32010-08-25 12:49:05 +02002415 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002416
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002417 if (proc == global.nbproc) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002418 if (global.mode & MODE_MWORKER) {
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002419 protocol_unbind_all();
William Lallemand73b85e72017-06-01 17:38:51 +02002420 mworker_wait();
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002421 }
Grant Zhang872f9c22017-01-21 01:10:18 +00002422#ifndef OPENSSL_NO_DH
2423 ssl_free_dh();
2424#endif
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002425 exit(0); /* parent must leave */
2426 }
2427
William Lallemand095ba4c2017-06-01 17:38:50 +02002428 /* Must chroot and setgid/setuid in the children */
2429 /* chroot if needed */
2430 if (global.chroot != NULL) {
2431 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
2432 Alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot);
2433 if (nb_oldpids)
2434 tell_old_pids(SIGTTIN);
2435 protocol_unbind_all();
2436 exit(1);
2437 }
2438 }
2439
2440 free(global.chroot);
2441 global.chroot = NULL;
2442
2443 /* setgid / setuid */
2444 if (global.gid) {
2445 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
2446 Warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2447 " without 'uid'/'user' is generally useless.\n", argv[0]);
2448
2449 if (setgid(global.gid) == -1) {
2450 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
2451 protocol_unbind_all();
2452 exit(1);
2453 }
2454 }
2455
2456 if (global.uid && setuid(global.uid) == -1) {
2457 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
2458 protocol_unbind_all();
2459 exit(1);
2460 }
2461
William Lallemand7f80eb22017-05-26 18:19:55 +02002462 /* pass through every cli socket, and check if it's bound to
2463 * the current process and if it exposes listeners sockets.
2464 * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork.
2465 * */
2466
2467 if (global.stats_fe) {
2468 struct bind_conf *bind_conf;
2469
2470 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
2471 if (bind_conf->level & ACCESS_FD_LISTENERS) {
2472 if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) {
2473 global.tune.options |= GTUNE_SOCKET_TRANSFER;
2474 break;
2475 }
2476 }
2477 }
2478 }
2479
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002480 /* we might have to unbind some proxies from some processes */
2481 px = proxy;
2482 while (px != NULL) {
2483 if (px->bind_proc && px->state != PR_STSTOPPED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002484 if (!(px->bind_proc & (1UL << proc))) {
2485 if (global.tune.options & GTUNE_SOCKET_TRANSFER)
2486 zombify_proxy(px);
2487 else
2488 stop_proxy(px);
2489 }
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002490 }
2491 px = px->next;
2492 }
2493
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002494 /* we might have to unbind some peers sections from some processes */
2495 for (curpeers = peers; curpeers; curpeers = curpeers->next) {
2496 if (!curpeers->peers_fe)
2497 continue;
2498
2499 if (curpeers->peers_fe->bind_proc & (1UL << proc))
2500 continue;
2501
2502 stop_proxy(curpeers->peers_fe);
2503 /* disable this peer section so that it kills itself */
Willy Tarreau47c8c022015-09-28 16:39:25 +02002504 signal_unregister_handler(curpeers->sighandler);
2505 task_delete(curpeers->sync_task);
2506 task_free(curpeers->sync_task);
2507 curpeers->sync_task = NULL;
2508 task_free(curpeers->peers_fe->task);
2509 curpeers->peers_fe->task = NULL;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002510 curpeers->peers_fe = NULL;
2511 }
2512
Willy Tarreaubaaee002006-06-26 02:48:02 +02002513 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
2514 * that we can detach from the TTY. We MUST NOT do it in other cases since
2515 * it would have already be done, and 0-2 would have been affected to listening
2516 * sockets
2517 */
Willy Tarreau106cb762008-11-16 07:40:34 +01002518 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002519 /* detach from the tty */
2520 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreau106cb762008-11-16 07:40:34 +01002521 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002522 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
2523 }
2524 pid = getpid(); /* update child's pid */
2525 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02002526 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002527 }
2528
Baptiste Assmann26c6eb82017-02-02 23:14:51 +01002529 /* initialize structures for name resolution */
2530 if (!dns_init_resolvers(1))
2531 exit(1);
2532
Willy Tarreaudd815982007-10-16 12:25:14 +02002533 protocol_enable_all();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002534 /*
2535 * That's it : the central polling loop. Run until we stop.
2536 */
2537 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002538
Willy Tarreaubaaee002006-06-26 02:48:02 +02002539 /* Do some cleanup */
2540 deinit();
2541
2542 exit(0);
2543}
2544
2545
2546/*
2547 * Local variables:
2548 * c-indent-level: 8
2549 * c-basic-offset: 8
2550 * End:
2551 */