blob: 10ddf160206290b959e573210d60a52ce6621917 [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
William Lallemand85b0bd92017-06-01 17:38:53 +0200176static char *cur_unixsocket = NULL;
177
Willy Tarreaubaaee002006-06-26 02:48:02 +0200178/* this is used to drain data, and as a temporary buffer for sprintf()... */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100179struct chunk trash = { };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200180
Willy Tarreau8096de92010-02-26 11:12:27 +0100181/* this buffer is always the same size as standard buffers and is used for
182 * swapping data inside a buffer.
183 */
184char *swap_buffer = NULL;
185
William Lallemandcb11fd22017-06-01 17:38:52 +0200186int atexit_flag = 0;
187
Willy Tarreaubb545b42010-08-25 12:58:59 +0200188int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189const int zero = 0;
190const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200191const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200192
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100193char hostname[MAX_HOSTNAME_LEN];
Emeric Brun2b920a12010-09-23 18:30:22 +0200194char localpeer[MAX_HOSTNAME_LEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195
Willy Tarreau89efaed2013-12-13 15:14:55 +0100196/* used from everywhere just to drain results we don't want to read and which
197 * recent versions of gcc increasingly and annoyingly complain about.
198 */
199int shut_your_big_mouth_gcc_int = 0;
200
William Lallemand73b85e72017-06-01 17:38:51 +0200201int *children = NULL; /* store PIDs of children in master workers mode */
202
203static volatile sig_atomic_t caught_signal = 0;
204static char **next_argv = NULL;
205
Willy Tarreau08ceb102011-07-24 22:58:00 +0200206/* list of the temporarily limited listeners because of lack of resource */
207struct list global_listener_queue = LIST_HEAD_INIT(global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +0200208struct task *global_listener_queue_task;
209static struct task *manage_global_listener_queue(struct task *t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200210
Willy Tarreauff055502014-04-28 22:27:06 +0200211/* bitfield of a few warnings to emit just once (WARN_*) */
212unsigned int warned = 0;
213
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100214
215/* These are strings to be reported in the output of "haproxy -vv". They may
216 * either be constants (in which case must_free must be zero) or dynamically
217 * allocated strings to pass to free() on exit, and in this case must_free
218 * must be non-zero.
219 */
220struct list build_opts_list = LIST_HEAD_INIT(build_opts_list);
221struct build_opts_str {
222 struct list list;
223 const char *str;
224 int must_free;
225};
226
Willy Tarreaue6945732016-12-21 19:57:00 +0100227/* These functions are called just after the point where the program exits
228 * after a config validity check, so they are generally suited for resource
229 * allocation and slow initializations that should be skipped during basic
230 * config checks. The functions must return 0 on success, or a combination
231 * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause
232 * and immediate exit, so the function must have emitted any useful error.
233 */
234struct list post_check_list = LIST_HEAD_INIT(post_check_list);
235struct post_check_fct {
236 struct list list;
237 int (*fct)();
238};
239
Willy Tarreau05554e62016-12-21 20:46:26 +0100240/* These functions are called when freeing the global sections at the end
241 * of deinit, after everything is stopped. They don't return anything, and
242 * they work in best effort mode as their sole goal is to make valgrind
243 * mostly happy.
244 */
245struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list);
246struct post_deinit_fct {
247 struct list list;
248 void (*fct)();
249};
250
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251/*********************************************************************/
252/* general purpose functions ***************************************/
253/*********************************************************************/
254
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100255/* used to register some build option strings at boot. Set must_free to
256 * non-zero if the string must be freed upon exit.
257 */
258void hap_register_build_opts(const char *str, int must_free)
259{
260 struct build_opts_str *b;
261
262 b = calloc(1, sizeof(*b));
263 if (!b) {
264 fprintf(stderr, "out of memory\n");
265 exit(1);
266 }
267 b->str = str;
268 b->must_free = must_free;
269 LIST_ADDQ(&build_opts_list, &b->list);
270}
271
Willy Tarreaue6945732016-12-21 19:57:00 +0100272/* used to register some initialization functions to call after the checks. */
273void hap_register_post_check(int (*fct)())
274{
275 struct post_check_fct *b;
276
277 b = calloc(1, sizeof(*b));
278 if (!b) {
279 fprintf(stderr, "out of memory\n");
280 exit(1);
281 }
282 b->fct = fct;
283 LIST_ADDQ(&post_check_list, &b->list);
284}
285
Willy Tarreau05554e62016-12-21 20:46:26 +0100286/* used to register some de-initialization functions to call after everything
287 * has stopped.
288 */
289void hap_register_post_deinit(void (*fct)())
290{
291 struct post_deinit_fct *b;
292
293 b = calloc(1, sizeof(*b));
294 if (!b) {
295 fprintf(stderr, "out of memory\n");
296 exit(1);
297 }
298 b->fct = fct;
299 LIST_ADDQ(&post_deinit_list, &b->list);
300}
301
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100302static void display_version()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200303{
304 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau7b677262017-04-03 09:27:49 +0200305 printf("Copyright 2000-2017 Willy Tarreau <willy@haproxy.org>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200306}
307
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100308static void display_build_opts()
Willy Tarreau7b066db2007-12-02 11:28:59 +0100309{
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100310 struct build_opts_str *item;
311
Willy Tarreau7b066db2007-12-02 11:28:59 +0100312 printf("Build options :"
313#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100314 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100315#endif
316#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100317 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100318#endif
319#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100320 "\n CC = " BUILD_CC
321#endif
322#ifdef BUILD_CFLAGS
323 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100324#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100325#ifdef BUILD_OPTIONS
326 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100327#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200328 "\n\nDefault settings :"
329 "\n maxconn = %d, bufsize = %d, maxrewrite = %d, maxpollevents = %d"
330 "\n\n",
331 DEFAULT_MAXCONN, BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS);
Willy Tarreaube5b6852009-10-03 18:57:08 +0200332
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100333 list_for_each_entry(item, &build_opts_list, list) {
334 puts(item->str);
335 }
336
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100337 putchar('\n');
338
Willy Tarreaube5b6852009-10-03 18:57:08 +0200339 list_pollers(stdout);
340 putchar('\n');
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100341 list_filters(stdout);
342 putchar('\n');
Willy Tarreau7b066db2007-12-02 11:28:59 +0100343}
344
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345/*
346 * This function prints the command line usage and exits
347 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100348static void usage(char *name)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349{
350 display_version();
351 fprintf(stderr,
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200352 "Usage : %s [-f <cfgfile|cfgdir>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
Willy Tarreaua088d312015-10-08 11:58:48 +0200354 " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100355 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356 " -d enters debug mode ; -db only disables background mode.\n"
Willy Tarreau6e064432012-05-08 15:40:42 +0200357 " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200359 " -D goes daemon ; -C changes to <dir> before loading files.\n"
William Lallemand095ba4c2017-06-01 17:38:50 +0200360 " -W master-worker mode.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200362 " -c check mode : only check config files and exit\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363 " -n sets the maximum total # of connections (%d)\n"
364 " -m limits the usable amount of memory (in MB)\n"
365 " -N sets the default, per-proxy maximum # of connections (%d)\n"
Emeric Brun2b920a12010-09-23 18:30:22 +0200366 " -L set local peer name (default to hostname)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367 " -p writes pids of all children to this file\n"
368#if defined(ENABLE_EPOLL)
369 " -de disables epoll() usage even when available\n"
370#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200371#if defined(ENABLE_KQUEUE)
372 " -dk disables kqueue() usage even when available\n"
373#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200374#if defined(ENABLE_POLL)
375 " -dp disables poll() usage even when available\n"
376#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200377#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100378 " -dS disables splice usage (broken on old kernels)\n"
379#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200380#if defined(USE_GETADDRINFO)
381 " -dG disables getaddrinfo() usage\n"
382#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000383#if defined(SO_REUSEPORT)
384 " -dR disables SO_REUSEPORT usage\n"
385#endif
Willy Tarreau3eed10e2016-11-07 21:03:16 +0100386 " -dr ignores server address resolution failures\n"
Emeric Brun850efd52014-01-29 12:24:34 +0100387 " -dV disables SSL verify on servers side\n"
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +0200388 " -sf/-st [pid ]* finishes/terminates old pids.\n"
Olivier Houchardf73629d2017-04-05 22:33:04 +0200389 " -x <unix_socket> get listening sockets from a unix socket\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200390 "\n",
391 name, DEFAULT_MAXCONN, cfg_maxpconn);
392 exit(1);
393}
394
395
396
397/*********************************************************************/
398/* more specific functions ***************************************/
399/*********************************************************************/
400
William Lallemand73b85e72017-06-01 17:38:51 +0200401/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
402 * pids the signal was correctly delivered to.
403 */
404static int tell_old_pids(int sig)
405{
406 int p;
407 int ret = 0;
408 for (p = 0; p < nb_oldpids; p++)
409 if (kill(oldpids[p], sig) == 0)
410 ret++;
411 return ret;
412}
413
414/* return 1 if a pid is a current child otherwise 0 */
415
416int current_child(int pid)
417{
418 int i;
419
420 for (i = 0; i < global.nbproc; i++) {
421 if (children[i] == pid)
422 return 1;
423 }
424 return 0;
425}
426
427static void mworker_signalhandler(int signum)
428{
429 caught_signal = signum;
430}
431
432static void mworker_register_signals()
433{
434 struct sigaction sa;
435 /* Here we are not using the haproxy async way
436 for signals because it does not exists in
437 the master */
438 memset(&sa, 0, sizeof(struct sigaction));
439 sa.sa_handler = &mworker_signalhandler;
440 sigaction(SIGHUP, &sa, NULL);
441 sigaction(SIGUSR1, &sa, NULL);
442 sigaction(SIGUSR2, &sa, NULL);
443 sigaction(SIGINT, &sa, NULL);
444 sigaction(SIGTERM, &sa, NULL);
445}
446
447static void mworker_block_signals()
448{
449 sigset_t set;
450
451 sigemptyset(&set);
452 sigaddset(&set, SIGUSR1);
453 sigaddset(&set, SIGUSR2);
454 sigaddset(&set, SIGHUP);
455 sigaddset(&set, SIGINT);
456 sigaddset(&set, SIGTERM);
457 sigprocmask(SIG_SETMASK, &set, NULL);
458}
459
460static void mworker_unblock_signals()
461{
462 sigset_t set;
463
464 sigemptyset(&set);
465 sigaddset(&set, SIGUSR1);
466 sigaddset(&set, SIGUSR2);
467 sigaddset(&set, SIGHUP);
468 sigaddset(&set, SIGINT);
469 sigaddset(&set, SIGTERM);
470 sigprocmask(SIG_UNBLOCK, &set, NULL);
471}
472
473static void mworker_unregister_signals()
474{
475 signal(SIGINT, SIG_DFL);
476 signal(SIGTERM, SIG_DFL);
477 signal(SIGHUP, SIG_IGN);
478 signal(SIGUSR1, SIG_IGN);
479 signal(SIGUSR2, SIG_IGN);
480}
481
482/*
483 * Send signal to every known children.
484 */
485
486static void mworker_kill(int sig)
487{
488 int i;
489
490 tell_old_pids(sig);
491 if (children) {
492 for (i = 0; i < global.nbproc; i++)
493 kill(children[i], sig);
494 }
495}
496
Willy Tarreaubaaee002006-06-26 02:48:02 +0200497/*
William Lallemand73b85e72017-06-01 17:38:51 +0200498 * remove a pid forom the olpid array and decrease nb_oldpids
499 * return 1 pid was found otherwise return 0
500 */
501
502int delete_oldpid(int pid)
503{
504 int i;
505
506 for (i = 0; i < nb_oldpids; i++) {
507 if (oldpids[i] == pid) {
508 oldpids[i] = oldpids[nb_oldpids - 1];
509 oldpids[nb_oldpids - 1] = 0;
510 nb_oldpids--;
511 return 1;
512 }
513 }
514 return 0;
515}
516
William Lallemand85b0bd92017-06-01 17:38:53 +0200517
518static void get_cur_unixsocket()
519{
520 /* if -x was used, try to update the stat socket if not available anymore */
521 if (global.stats_fe) {
522 struct bind_conf *bind_conf;
523
524 /* pass through all stats socket */
525 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
526 struct listener *l;
527
528 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
529
530 if (l->addr.ss_family == AF_UNIX &&
531 (bind_conf->level & ACCESS_FD_LISTENERS)) {
532 const struct sockaddr_un *un;
533
534 un = (struct sockaddr_un *)&l->addr;
535 /* priority to old_unixsocket */
536 if (!cur_unixsocket) {
537 cur_unixsocket = strdup(un->sun_path);
538 } else {
539 if (old_unixsocket && !strcmp(un->sun_path, old_unixsocket)) {
540 free(cur_unixsocket);
541 cur_unixsocket = strdup(old_unixsocket);
542 return;
543 }
544 }
545 }
546 }
547 }
548 }
549 if (!cur_unixsocket && old_unixsocket)
550 cur_unixsocket = strdup(old_unixsocket);
551}
552
William Lallemand73b85e72017-06-01 17:38:51 +0200553/*
554 * When called, this function reexec haproxy with -sf followed by current
555 * children PIDs and possibily old children PIDs if they didn't leave yet.
556 */
557static void mworker_reload()
558{
559 int next_argc = 0;
560 int j;
561 char *msg = NULL;
562
563 mworker_block_signals();
564 mworker_unregister_signals();
565 setenv("HAPROXY_MWORKER_REEXEC", "1", 1);
566
567 /* compute length */
568 while (next_argv[next_argc])
569 next_argc++;
570
William Lallemand85b0bd92017-06-01 17:38:53 +0200571 /* 1 for haproxy -sf, 2 for -x /socket */
572 next_argv = realloc(next_argv, (next_argc + 1 + 2 + global.nbproc + nb_oldpids + 1) * sizeof(char *));
William Lallemand73b85e72017-06-01 17:38:51 +0200573 if (next_argv == NULL)
574 goto alloc_error;
575
576
577 /* add -sf <PID>* to argv */
578 if (children || nb_oldpids > 0)
579 next_argv[next_argc++] = "-sf";
580 if (children) {
581 for (j = 0; j < global.nbproc; next_argc++,j++) {
582 next_argv[next_argc] = memprintf(&msg, "%d", children[j]);
583 if (next_argv[next_argc] == NULL)
584 goto alloc_error;
585 msg = NULL;
586 }
587 }
588 /* copy old process PIDs */
589 for (j = 0; j < nb_oldpids; next_argc++,j++) {
590 next_argv[next_argc] = memprintf(&msg, "%d", oldpids[j]);
591 if (next_argv[next_argc] == NULL)
592 goto alloc_error;
593 msg = NULL;
594 }
595 next_argv[next_argc] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200596
597 /* if -x was used, try to update the stat socket if not available anymore */
598 if (cur_unixsocket) {
599
600 if (old_unixsocket) {
601
602 /* look for -x <path> */
603 for (j = 0; next_argv[j]; j++) {
604 if (!strcmp(next_argv[j], "-x"))
605 next_argv[j + 1] = (char *)cur_unixsocket;
606 }
607 } else {
608 /* if -x is not specified but we know the socket, add -x with it */
609 next_argv[next_argc++] = "-x";
610 next_argv[next_argc++] = (char *)cur_unixsocket;
611 next_argv[next_argc++] = NULL;
612
613 }
614 }
615
William Lallemand73b85e72017-06-01 17:38:51 +0200616 deinit(); /* we don't want to leak FD there */
617 Warning("Reexecuting Master process\n");
618 execv(next_argv[0], next_argv);
619
620alloc_error:
621 Warning("Cannot allocate memory\n");
622 Warning("Failed to reexecute the master processs [%d]\n", pid);
623 return;
624}
625
626
627/*
628 * Wait for every children to exit
629 */
630
631static void mworker_wait()
632{
633 int exitpid = -1;
634 int status = 0;
635
636 mworker_register_signals();
637 mworker_unblock_signals();
638
639 while (1) {
640
641 while (((exitpid = wait(&status)) == -1) && errno == EINTR) {
642 int sig = caught_signal;
643 if (sig == SIGUSR2 || sig == SIGHUP) {
644 mworker_reload();
645 } else {
646 Warning("Exiting Master process...\n");
647 mworker_kill(sig);
648 mworker_unregister_signals();
649 }
650 caught_signal = 0;
651 }
652
653 if (exitpid == -1 && errno == ECHILD) {
654 Warning("All workers are left. Leaving... (%d)\n", status);
William Lallemandcb11fd22017-06-01 17:38:52 +0200655 atexit_flag = 0;
William Lallemand73b85e72017-06-01 17:38:51 +0200656 exit(status); /* parent must leave using the latest status code known */
657 }
658
659 if (WIFEXITED(status))
660 status = WEXITSTATUS(status);
661 else if (WIFSIGNALED(status))
662 status = 128 + WTERMSIG(status);
663 else if (WIFSTOPPED(status))
664 status = 128 + WSTOPSIG(status);
665 else
666 status = 255;
667
668 if (!children) {
669 Warning("Worker %d left with exit code %d\n", exitpid, status);
670 } else {
671 /* check if exited child was in the current children list */
672 if (current_child(exitpid)) {
673 Alert("Current worker %d left with exit code %d\n", exitpid, status);
William Lallemand69f9b3b2017-06-01 17:38:54 +0200674 if (status != 0 && status != 130 && status != 143
675 && global.tune.options & GTUNE_EXIT_ONFAILURE) {
676 Alert("exit-on-failure: killing every workers with SIGTERM\n");
677 mworker_kill(SIGTERM);
678 }
William Lallemand73b85e72017-06-01 17:38:51 +0200679 } else {
680 Warning("Former worker %d left with exit code %d\n", exitpid, status);
681 delete_oldpid(exitpid);
682 }
683 }
684 }
685}
686
William Lallemandcb11fd22017-06-01 17:38:52 +0200687
688/*
689 * Reexec the process in failure mode, instead of exiting
690 */
691void reexec_on_failure()
692{
693 if (!atexit_flag)
694 return;
695
696 setenv("HAPROXY_MWORKER_WAIT_ONLY", "1", 1);
697
698 Warning("Reexecuting Master process in waitpid mode\n");
699 mworker_reload();
700
701 Warning("Failed to reexecute the master processs\n");
702}
William Lallemand73b85e72017-06-01 17:38:51 +0200703
704
705/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200706 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
707 * a signal zero to all subscribers. This means that it's as easy as
708 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200709 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100710static void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200711{
712 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200713 signal_unregister_handler(sh);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200714 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200715}
716
717/*
718 * upon SIGTTOU, we pause everything
719 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100720static void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200721{
722 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200723 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200724}
725
726/*
727 * upon SIGTTIN, let's have a soft stop.
728 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100729static void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200730{
Willy Tarreaube58c382011-07-24 18:28:10 +0200731 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732}
733
734/*
735 * this function dumps every server's state when the process receives SIGHUP.
736 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100737static void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738{
739 struct proxy *p = proxy;
740
741 Warning("SIGHUP received, dumping servers states.\n");
742 while (p) {
743 struct server *s = p->srv;
744
745 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
746 while (s) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100747 chunk_printf(&trash,
748 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
749 p->id, s->id,
Willy Tarreau892337c2014-05-13 23:41:20 +0200750 (s->state != SRV_ST_STOPPED) ? "UP" : "DOWN",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100751 s->cur_sess, s->nbpend, s->counters.cum_sess);
752 Warning("%s\n", trash.str);
753 send_log(p, LOG_NOTICE, "%s\n", trash.str);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754 s = s->next;
755 }
756
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200757 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
758 if (!p->srv) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100759 chunk_printf(&trash,
760 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
761 p->id,
762 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 +0200763 } else if (p->srv_act == 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100764 chunk_printf(&trash,
765 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
766 p->id,
767 (p->srv_bck) ? "is running on backup servers" : "has no server available",
768 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 +0200769 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100770 chunk_printf(&trash,
771 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
772 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
773 p->id, p->srv_act, p->srv_bck,
774 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 +0200775 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100776 Warning("%s\n", trash.str);
777 send_log(p, LOG_NOTICE, "%s\n", trash.str);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200778
779 p = p->next;
780 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200781}
782
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100783static void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200784{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200785 /* dump memory usage then free everything possible */
786 dump_pools();
787 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200788}
789
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200790/* This function check if cfg_cfgfiles containes directories.
791 * If it find one, it add all the files (and only files) it containes
792 * in cfg_cfgfiles in place of the directory (and remove the directory).
793 * It add the files in lexical order.
794 * It add only files with .cfg extension.
795 * It doesn't add files with name starting with '.'
796 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100797static void cfgfiles_expand_directories(void)
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200798{
799 struct wordlist *wl, *wlb;
800 char *err = NULL;
801
802 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
803 struct stat file_stat;
804 struct dirent **dir_entries = NULL;
805 int dir_entries_nb;
806 int dir_entries_it;
807
808 if (stat(wl->s, &file_stat)) {
809 Alert("Cannot open configuration file/directory %s : %s\n",
810 wl->s,
811 strerror(errno));
812 exit(1);
813 }
814
815 if (!S_ISDIR(file_stat.st_mode))
816 continue;
817
818 /* from this point wl->s is a directory */
819
820 dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort);
821 if (dir_entries_nb < 0) {
822 Alert("Cannot open configuration directory %s : %s\n",
823 wl->s,
824 strerror(errno));
825 exit(1);
826 }
827
828 /* for each element in the directory wl->s */
829 for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) {
830 struct dirent *dir_entry = dir_entries[dir_entries_it];
831 char *filename = NULL;
832 char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg");
833
834 /* don't add filename that begin with .
835 * only add filename with .cfg extention
836 */
837 if (dir_entry->d_name[0] == '.' ||
838 !(d_name_cfgext && d_name_cfgext[4] == '\0'))
839 goto next_dir_entry;
840
841 if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) {
842 Alert("Cannot load configuration files %s : out of memory.\n",
843 filename);
844 exit(1);
845 }
846
847 if (stat(filename, &file_stat)) {
848 Alert("Cannot open configuration file %s : %s\n",
849 wl->s,
850 strerror(errno));
851 exit(1);
852 }
853
854 /* don't add anything else than regular file in cfg_cfgfiles
855 * this way we avoid loops
856 */
857 if (!S_ISREG(file_stat.st_mode))
858 goto next_dir_entry;
859
860 if (!list_append_word(&wl->list, filename, &err)) {
861 Alert("Cannot load configuration files %s : %s\n",
862 filename,
863 err);
864 exit(1);
865 }
866
867next_dir_entry:
868 free(filename);
869 free(dir_entry);
870 }
871
872 free(dir_entries);
873
874 /* remove the current directory (wl) from cfg_cfgfiles */
875 free(wl->s);
876 LIST_DEL(&wl->list);
877 free(wl);
878 }
879
880 free(err);
881}
882
Olivier Houchardf73629d2017-04-05 22:33:04 +0200883static int get_old_sockets(const char *unixsocket)
884{
885 char *cmsgbuf = NULL, *tmpbuf = NULL;
886 int *tmpfd = NULL;
887 struct sockaddr_un addr;
888 struct cmsghdr *cmsg;
889 struct msghdr msghdr;
890 struct iovec iov;
891 struct xfer_sock_list *xfer_sock = NULL;
Olivier Houchard54740872017-04-06 14:45:14 +0200892 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf73629d2017-04-05 22:33:04 +0200893 int sock = -1;
894 int ret = -1;
895 int ret2 = -1;
896 int fd_nb;
897 int got_fd = 0;
898 int i = 0;
899 size_t maxoff = 0, curoff = 0;
900
901 memset(&msghdr, 0, sizeof(msghdr));
902 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
903 if (!cmsgbuf) {
904 Warning("Failed to allocate memory to send sockets\n");
905 goto out;
906 }
907 sock = socket(PF_UNIX, SOCK_STREAM, 0);
908 if (sock < 0) {
909 Warning("Failed to connect to the old process socket '%s'\n",
910 unixsocket);
911 goto out;
912 }
913 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path));
914 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
915 addr.sun_family = PF_UNIX;
916 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
917 if (ret < 0) {
918 Warning("Failed to connect to the old process socket '%s'\n",
919 unixsocket);
920 goto out;
921 }
Olivier Houchard54740872017-04-06 14:45:14 +0200922 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf73629d2017-04-05 22:33:04 +0200923 iov.iov_base = &fd_nb;
924 iov.iov_len = sizeof(fd_nb);
925 msghdr.msg_iov = &iov;
926 msghdr.msg_iovlen = 1;
927 send(sock, "_getsocks\n", strlen("_getsocks\n"), 0);
928 /* First, get the number of file descriptors to be received */
929 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
930 Warning("Failed to get the number of sockets to be transferred !\n");
931 goto out;
932 }
933 if (fd_nb == 0) {
934 ret = 0;
935 goto out;
936 }
937 tmpbuf = malloc(fd_nb * (1 + NAME_MAX + 1 + IFNAMSIZ + sizeof(int)));
938 if (tmpbuf == NULL) {
939 Warning("Failed to allocate memory while receiving sockets\n");
940 goto out;
941 }
942 tmpfd = malloc(fd_nb * sizeof(int));
943 if (tmpfd == NULL) {
944 Warning("Failed to allocate memory while receiving sockets\n");
945 goto out;
946 }
947 msghdr.msg_control = cmsgbuf;
948 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
949 iov.iov_len = MAX_SEND_FD * (1 + NAME_MAX + 1 + IFNAMSIZ + sizeof(int));
950 do {
951 int ret3;
952
953 iov.iov_base = tmpbuf + curoff;
954 ret = recvmsg(sock, &msghdr, 0);
955 if (ret == -1 && errno == EINTR)
956 continue;
957 if (ret <= 0)
958 break;
959 /* Send an ack to let the sender know we got the sockets
960 * and it can send some more
961 */
962 do {
963 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
964 } while (ret3 == -1 && errno == EINTR);
965 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
966 cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
967 if (cmsg->cmsg_level == SOL_SOCKET &&
968 cmsg->cmsg_type == SCM_RIGHTS) {
969 size_t totlen = cmsg->cmsg_len -
970 CMSG_LEN(0);
971 if (totlen / sizeof(int) + got_fd > fd_nb) {
972 Warning("Got to many sockets !\n");
973 goto out;
974 }
975 /*
976 * Be paranoid and use memcpy() to avoid any
977 * potential alignement issue.
978 */
979 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
980 got_fd += totlen / sizeof(int);
981 }
982 }
983 curoff += ret;
984 } while (got_fd < fd_nb);
985
986 if (got_fd != fd_nb) {
987 Warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
988 fd_nb, got_fd);
989 goto out;
990 }
991 maxoff = curoff;
992 curoff = 0;
993 for (i = 0; i < got_fd; i++) {
994 int fd = tmpfd[i];
995 socklen_t socklen;
996 int len;
997
998 xfer_sock = calloc(1, sizeof(*xfer_sock));
999 if (!xfer_sock) {
1000 Warning("Failed to allocate memory in get_old_sockets() !\n");
1001 break;
1002 }
1003 xfer_sock->fd = -1;
1004
1005 socklen = sizeof(xfer_sock->addr);
1006 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
1007 Warning("Failed to get socket address\n");
1008 free(xfer_sock);
1009 continue;
1010 }
1011 if (curoff >= maxoff) {
1012 Warning("Inconsistency while transferring sockets\n");
1013 goto out;
1014 }
1015 len = tmpbuf[curoff++];
1016 if (len > 0) {
1017 /* We have a namespace */
1018 if (curoff + len > maxoff) {
1019 Warning("Inconsistency while transferring sockets\n");
1020 goto out;
1021 }
1022 xfer_sock->namespace = malloc(len + 1);
1023 if (!xfer_sock->namespace) {
1024 Warning("Failed to allocate memory while transferring sockets\n");
1025 goto out;
1026 }
1027 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
1028 xfer_sock->namespace[len] = 0;
1029 curoff += len;
1030 }
1031 if (curoff >= maxoff) {
1032 Warning("Inconsistency while transferring sockets\n");
1033 goto out;
1034 }
1035 len = tmpbuf[curoff++];
1036 if (len > 0) {
1037 /* We have an interface */
1038 if (curoff + len > maxoff) {
1039 Warning("Inconsistency while transferring sockets\n");
1040 goto out;
1041 }
1042 xfer_sock->iface = malloc(len + 1);
1043 if (!xfer_sock->iface) {
1044 Warning("Failed to allocate memory while transferring sockets\n");
1045 goto out;
1046 }
1047 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
1048 xfer_sock->namespace[len] = 0;
1049 curoff += len;
1050 }
1051 if (curoff + sizeof(int) > maxoff) {
1052 Warning("Inconsistency while transferring sockets\n");
1053 goto out;
1054 }
1055 memcpy(&xfer_sock->options, &tmpbuf[curoff],
1056 sizeof(xfer_sock->options));
1057 curoff += sizeof(xfer_sock->options);
1058
1059 xfer_sock->fd = fd;
1060 if (xfer_sock_list)
1061 xfer_sock_list->prev = xfer_sock;
1062 xfer_sock->next = xfer_sock_list;
1063 xfer_sock->prev = NULL;
1064 xfer_sock_list = xfer_sock;
1065 xfer_sock = NULL;
1066 }
1067
1068 ret2 = 0;
1069out:
1070 /* If we failed midway make sure to close the remaining
1071 * file descriptors
1072 */
1073 if (tmpfd != NULL && i < got_fd) {
1074 for (; i < got_fd; i++) {
1075 close(tmpfd[i]);
1076 }
1077 }
1078 free(tmpbuf);
1079 free(tmpfd);
1080 free(cmsgbuf);
1081 if (sock != -1)
1082 close(sock);
1083 if (xfer_sock) {
1084 free(xfer_sock->namespace);
1085 free(xfer_sock->iface);
1086 if (xfer_sock->fd != -1)
1087 close(xfer_sock->fd);
1088 free(xfer_sock);
1089 }
1090 return (ret2);
1091}
1092
Willy Tarreaubaaee002006-06-26 02:48:02 +02001093/*
William Lallemand73b85e72017-06-01 17:38:51 +02001094 * copy and cleanup the current argv
1095 * Remove the -sf /-st parameters
1096 * Return an allocated copy of argv
1097 */
1098
1099static char **copy_argv(int argc, char **argv)
1100{
1101 char **newargv;
1102 int i, j;
1103
1104 newargv = calloc(argc + 2, sizeof(char *));
1105 if (newargv == NULL) {
1106 Warning("Cannot allocate memory\n");
1107 return NULL;
1108 }
1109
1110 for (i = 0, j = 0; i < argc; i++, j++) {
1111 char *flag = *(argv + i) + 1;
1112
1113 /* -sf or -st */
1114 if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1115 /* list of pids to finish ('f') or terminate ('t') */
1116 i++;
1117 while (i < argc && argv[i][0] != '-') {
1118 i++;
1119 }
1120 }
1121 newargv[j] = argv[i];
1122 }
1123 return newargv;
1124}
1125
1126/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001127 * This function initializes all the necessary variables. It only returns
1128 * if everything is OK. If something fails, it exits.
1129 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001130static void init(int argc, char **argv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001131{
Willy Tarreaubaaee002006-06-26 02:48:02 +02001132 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001133 char *tmp;
1134 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +02001135 int err_code = 0;
Maxime de Roucy0f503922016-05-13 23:52:55 +02001136 char *err_msg = NULL;
Willy Tarreau477ecd82010-01-03 21:12:30 +01001137 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +00001138 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +02001139 char *change_dir = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001140 struct proxy *px;
Willy Tarreaue6945732016-12-21 19:57:00 +01001141 struct post_check_fct *pcf;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001142
William Lallemand73b85e72017-06-01 17:38:51 +02001143 next_argv = copy_argv(argc, argv);
1144
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001145 chunk_init(&trash, malloc(global.tune.bufsize), global.tune.bufsize);
Willy Tarreau2819e992013-12-13 14:41:10 +01001146 alloc_trash_buffers(global.tune.bufsize);
David du Colombier7af46052012-05-16 14:16:48 +02001147
Emeric Brun2b920a12010-09-23 18:30:22 +02001148 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
1149 * the string in case of truncation, and at least FreeBSD appears not to do
1150 * it.
1151 */
1152 memset(hostname, 0, sizeof(hostname));
1153 gethostname(hostname, sizeof(hostname) - 1);
1154 memset(localpeer, 0, sizeof(localpeer));
1155 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
1156
Willy Tarreaubaaee002006-06-26 02:48:02 +02001157 /*
1158 * Initialize the previously static variables.
1159 */
1160
Willy Tarreau3eba98a2009-01-25 13:56:13 +01001161 totalconn = actconn = maxfd = listeners = stopping = 0;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001162 killed = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001163
1164
1165#ifdef HAPROXY_MEMMAX
Willy Tarreau70060452015-12-14 12:46:07 +01001166 global.rlimit_memmax_all = HAPROXY_MEMMAX;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001167#endif
1168
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001169 tzset();
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001170 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001171 start_date = now;
1172
Willy Tarreau84310e22014-02-14 11:59:04 +01001173 srandom(now_ms - getpid());
1174
Dragan Dosen835b9212016-02-12 13:23:03 +01001175 init_log();
Willy Tarreau29857942009-05-10 09:01:21 +02001176 signal_init();
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001177 if (init_acl() != 0)
1178 exit(1);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001179 init_task();
Willy Tarreau87b09662015-04-03 00:22:06 +02001180 init_stream();
Willy Tarreaub1ec8c42015-04-03 13:53:24 +02001181 init_session();
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001182 init_connection();
Willy Tarreau8280d642009-09-23 23:37:52 +02001183 /* warning, we init buffers later */
Willy Tarreaue4d7e552007-05-13 20:19:55 +02001184 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +01001185 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001186
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001187 /* Initialise lua. */
1188 hlua_init();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001189
Christopher Fauletff2613e2016-11-09 11:36:17 +01001190 /* Initialize process vars */
1191 vars_init(&global.vars, SCOPE_PROC);
1192
Willy Tarreau43b78992009-01-25 15:42:27 +01001193 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001194#if defined(ENABLE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001195 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001196#endif
1197#if defined(ENABLE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001198 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001199#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001200#if defined(ENABLE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +01001201 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001202#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001203#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001204 global.tune.options |= GTUNE_USE_SPLICE;
1205#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001206#if defined(USE_GETADDRINFO)
1207 global.tune.options |= GTUNE_USE_GAI;
1208#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001209#if defined(SO_REUSEPORT)
1210 global.tune.options |= GTUNE_USE_REUSEPORT;
1211#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001212
1213 pid = getpid();
1214 progname = *argv;
1215 while ((tmp = strchr(progname, '/')) != NULL)
1216 progname = tmp + 1;
1217
Kevinm48936af2010-12-22 16:08:21 +00001218 /* the process name is used for the logs only */
Dragan Dosen43885c72015-10-01 13:18:13 +02001219 chunk_initstr(&global.log_tag, strdup(progname));
Kevinm48936af2010-12-22 16:08:21 +00001220
Willy Tarreaubaaee002006-06-26 02:48:02 +02001221 argc--; argv++;
1222 while (argc > 0) {
1223 char *flag;
1224
1225 if (**argv == '-') {
1226 flag = *argv+1;
1227
1228 /* 1 arg */
1229 if (*flag == 'v') {
1230 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +01001231 if (flag[1] == 'v') /* -vv */
1232 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233 exit(0);
1234 }
1235#if defined(ENABLE_EPOLL)
1236 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +01001237 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001238#endif
1239#if defined(ENABLE_POLL)
1240 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +01001241 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001242#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +02001243#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001244 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +01001245 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001246#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001247#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001248 else if (*flag == 'd' && flag[1] == 'S')
1249 global.tune.options &= ~GTUNE_USE_SPLICE;
1250#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001251#if defined(USE_GETADDRINFO)
1252 else if (*flag == 'd' && flag[1] == 'G')
1253 global.tune.options &= ~GTUNE_USE_GAI;
1254#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001255#if defined(SO_REUSEPORT)
1256 else if (*flag == 'd' && flag[1] == 'R')
1257 global.tune.options &= ~GTUNE_USE_REUSEPORT;
1258#endif
Emeric Brun850efd52014-01-29 12:24:34 +01001259 else if (*flag == 'd' && flag[1] == 'V')
1260 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001261 else if (*flag == 'V')
1262 arg_mode |= MODE_VERBOSE;
1263 else if (*flag == 'd' && flag[1] == 'b')
1264 arg_mode |= MODE_FOREGROUND;
Willy Tarreau6e064432012-05-08 15:40:42 +02001265 else if (*flag == 'd' && flag[1] == 'M')
1266 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreau3eed10e2016-11-07 21:03:16 +01001267 else if (*flag == 'd' && flag[1] == 'r')
1268 global.tune.options |= GTUNE_RESOLVE_DONTFAIL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001269 else if (*flag == 'd')
1270 arg_mode |= MODE_DEBUG;
1271 else if (*flag == 'c')
1272 arg_mode |= MODE_CHECK;
William Lallemand095ba4c2017-06-01 17:38:50 +02001273 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +02001274 arg_mode |= MODE_DAEMON;
William Lallemand095ba4c2017-06-01 17:38:50 +02001275 else if (*flag == 'W')
1276 arg_mode |= MODE_MWORKER;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001277 else if (*flag == 'q')
1278 arg_mode |= MODE_QUIET;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001279 else if (*flag == 'x') {
1280 if (argv[1][0] == '-') {
1281 Alert("Unix socket path expected with the -x flag\n");
1282 exit(1);
1283 }
1284 old_unixsocket = argv[1];
1285 argv++;
1286 argc--;
1287 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001288 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1289 /* list of pids to finish ('f') or terminate ('t') */
1290
1291 if (flag[1] == 'f')
1292 oldpids_sig = SIGUSR1; /* finish then exit */
1293 else
1294 oldpids_sig = SIGTERM; /* terminate immediately */
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001295 while (argc > 1 && argv[1][0] != '-') {
1296 oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int));
1297 if (!oldpids) {
1298 Alert("Cannot allocate old pid : out of memory.\n");
1299 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001300 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001301 argc--; argv++;
1302 oldpids[nb_oldpids] = atol(*argv);
1303 if (oldpids[nb_oldpids] <= 0)
1304 usage(progname);
1305 nb_oldpids++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001306 }
1307 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001308 else if (flag[0] == '-' && flag[1] == 0) { /* "--" */
1309 /* now that's a cfgfile list */
1310 argv++; argc--;
1311 while (argc > 0) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02001312 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
1313 Alert("Cannot load configuration file/directory %s : %s\n",
1314 *argv,
1315 err_msg);
Willy Tarreaua088d312015-10-08 11:58:48 +02001316 exit(1);
1317 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001318 argv++; argc--;
1319 }
1320 break;
1321 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001322 else { /* >=2 args */
1323 argv++; argc--;
1324 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001325 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001326
1327 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +02001328 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001329 case 'n' : cfg_maxconn = atol(*argv); break;
Willy Tarreau70060452015-12-14 12:46:07 +01001330 case 'm' : global.rlimit_memmax_all = atol(*argv); break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001331 case 'N' : cfg_maxpconn = atol(*argv); break;
Emeric Brun2b920a12010-09-23 18:30:22 +02001332 case 'L' : strncpy(localpeer, *argv, sizeof(localpeer) - 1); break;
Willy Tarreau5d01a632009-06-22 16:02:30 +02001333 case 'f' :
Maxime de Roucy0f503922016-05-13 23:52:55 +02001334 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
1335 Alert("Cannot load configuration file/directory %s : %s\n",
1336 *argv,
1337 err_msg);
Willy Tarreau5d01a632009-06-22 16:02:30 +02001338 exit(1);
1339 }
Willy Tarreau5d01a632009-06-22 16:02:30 +02001340 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001341 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001342 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001343 }
1344 }
1345 }
1346 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001347 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001348 argv++; argc--;
1349 }
1350
1351 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
William Lallemand095ba4c2017-06-01 17:38:50 +02001352 (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
Willy Tarreaubaaee002006-06-26 02:48:02 +02001353 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
1354
William Lallemandcb11fd22017-06-01 17:38:52 +02001355 /* Master workers wait mode */
1356 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_WAIT_ONLY") != NULL)) {
1357
1358 unsetenv("HAPROXY_MWORKER_WAIT_ONLY");
1359 mworker_wait();
1360 }
1361
1362 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {
1363 atexit_flag = 1;
1364 atexit(reexec_on_failure);
1365 }
1366
Willy Tarreau576132e2011-09-10 19:26:56 +02001367 if (change_dir && chdir(change_dir) < 0) {
1368 Alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
1369 exit(1);
1370 }
1371
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001372 /* handle cfgfiles that are actualy directories */
1373 cfgfiles_expand_directories();
1374
1375 if (LIST_ISEMPTY(&cfg_cfgfiles))
1376 usage(progname);
1377
Willy Tarreaubaaee002006-06-26 02:48:02 +02001378 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001379
1380 init_default_instance();
1381
Willy Tarreau477ecd82010-01-03 21:12:30 +01001382 list_for_each_entry(wl, &cfg_cfgfiles, list) {
Willy Tarreauc4382422009-12-06 13:10:44 +01001383 int ret;
1384
Willy Tarreau477ecd82010-01-03 21:12:30 +01001385 ret = readcfgfile(wl->s);
Willy Tarreauc4382422009-12-06 13:10:44 +01001386 if (ret == -1) {
1387 Alert("Could not open configuration file %s : %s\n",
Willy Tarreau477ecd82010-01-03 21:12:30 +01001388 wl->s, strerror(errno));
Willy Tarreauc4382422009-12-06 13:10:44 +01001389 exit(1);
1390 }
Willy Tarreau25a67fa2009-12-15 21:46:25 +01001391 if (ret & (ERR_ABORT|ERR_FATAL))
Willy Tarreau477ecd82010-01-03 21:12:30 +01001392 Alert("Error(s) found in configuration file : %s\n", wl->s);
Willy Tarreau25a67fa2009-12-15 21:46:25 +01001393 err_code |= ret;
Willy Tarreau058e9072009-07-20 09:30:05 +02001394 if (err_code & ERR_ABORT)
Willy Tarreau5d01a632009-06-22 16:02:30 +02001395 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001396 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001397
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02001398 /* do not try to resolve arguments nor to spot inconsistencies when
1399 * the configuration contains fatal errors caused by files not found
1400 * or failed memory allocations.
1401 */
1402 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1403 Alert("Fatal errors found in configuration.\n");
1404 exit(1);
1405 }
1406
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001407 pattern_finalize_config();
1408
Willy Tarreaubb925012009-07-23 13:36:36 +02001409 err_code |= check_config_validity();
1410 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1411 Alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001412 exit(1);
1413 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001414
Willy Tarreau70060452015-12-14 12:46:07 +01001415 /* recompute the amount of per-process memory depending on nbproc and
1416 * the shared SSL cache size (allowed to exist in all processes).
1417 */
1418 if (global.rlimit_memmax_all) {
1419#if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE)
1420 int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL;
1421
1422 global.rlimit_memmax =
1423 ((((int64_t)global.rlimit_memmax_all * 1048576LL) -
1424 ssl_cache_bytes) / global.nbproc +
1425 ssl_cache_bytes + 1048575LL) / 1048576LL;
1426#else
1427 global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc;
1428#endif
1429 }
1430
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001431#ifdef CONFIG_HAP_NS
1432 err_code |= netns_init();
1433 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1434 Alert("Failed to initialize namespace support.\n");
1435 exit(1);
1436 }
1437#endif
1438
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01001439 /* Apply server states */
1440 apply_server_state();
1441
1442 for (px = proxy; px; px = px->next)
1443 srv_compute_all_admin_states(px);
1444
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001445 /* Apply servers' configured address */
1446 err_code |= srv_init_addr();
1447 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1448 Alert("Failed to initialize server(s) addr.\n");
1449 exit(1);
1450 }
1451
Willy Tarreaubaaee002006-06-26 02:48:02 +02001452 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001453 struct peers *pr;
1454 struct proxy *px;
1455
1456 for (pr = peers; pr; pr = pr->next)
1457 if (pr->peers_fe)
1458 break;
1459
1460 for (px = proxy; px; px = px->next)
Willy Tarreau4348fad2012-09-20 16:48:07 +02001461 if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners))
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001462 break;
1463
1464 if (pr || px) {
1465 /* At least one peer or one listener has been found */
1466 qfprintf(stdout, "Configuration file is valid\n");
1467 exit(0);
1468 }
1469 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
1470 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001471 }
1472
Willy Tarreaue9b26022011-08-01 20:57:55 +02001473 global_listener_queue_task = task_new();
1474 if (!global_listener_queue_task) {
1475 Alert("Out of memory when initializing global task\n");
1476 exit(1);
1477 }
1478 /* very simple initialization, users will queue the task if needed */
1479 global_listener_queue_task->context = NULL; /* not even a context! */
1480 global_listener_queue_task->process = manage_global_listener_queue;
1481 global_listener_queue_task->expire = TICK_ETERNITY;
1482
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001483 /* now we know the buffer size, we can initialize the channels and buffers */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001484 init_buffer();
Willy Tarreau8280d642009-09-23 23:37:52 +02001485
Willy Tarreaue6945732016-12-21 19:57:00 +01001486 list_for_each_entry(pcf, &post_check_list, list) {
1487 err_code |= pcf->fct();
1488 if (err_code & (ERR_ABORT|ERR_FATAL))
1489 exit(1);
1490 }
1491
Willy Tarreaubaaee002006-06-26 02:48:02 +02001492 if (cfg_maxconn > 0)
1493 global.maxconn = cfg_maxconn;
1494
1495 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +02001496 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001497 global.pidfile = strdup(cfg_pidfile);
1498 }
1499
Willy Tarreaud0256482015-01-15 21:45:22 +01001500 /* Now we want to compute the maxconn and possibly maxsslconn values.
1501 * It's a bit tricky. If memmax is not set, maxconn defaults to
1502 * DEFAULT_MAXCONN and maxsslconn defaults to DEFAULT_MAXSSLCONN.
1503 *
1504 * If memmax is set, then it depends on which values are set. If
1505 * maxsslconn is set, we use memmax to determine how many cleartext
1506 * connections may be added, and set maxconn to the sum of the two.
1507 * If maxconn is set and not maxsslconn, maxsslconn is computed from
1508 * the remaining amount of memory between memmax and the cleartext
1509 * connections. If neither are set, then it is considered that all
1510 * connections are SSL-capable, and maxconn is computed based on this,
1511 * then maxsslconn accordingly. We need to know if SSL is used on the
1512 * frontends, backends, or both, because when it's used on both sides,
1513 * we need twice the value for maxsslconn, but we only count the
1514 * handshake once since it is not performed on the two sides at the
1515 * same time (frontend-side is terminated before backend-side begins).
1516 * The SSL stack is supposed to have filled ssl_session_cost and
Willy Tarreau474b96a2015-01-28 19:03:21 +01001517 * ssl_handshake_cost during its initialization. In any case, if
1518 * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for
1519 * maxconn in order to protect the system.
Willy Tarreaud0256482015-01-15 21:45:22 +01001520 */
1521 if (!global.rlimit_memmax) {
1522 if (global.maxconn == 0) {
1523 global.maxconn = DEFAULT_MAXCONN;
1524 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1525 fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn);
1526 }
1527 }
1528#ifdef USE_OPENSSL
1529 else if (!global.maxconn && !global.maxsslconn &&
1530 (global.ssl_used_frontend || global.ssl_used_backend)) {
1531 /* memmax is set, compute everything automatically. Here we want
1532 * to ensure that all SSL connections will be served. We take
1533 * care of the number of sides where SSL is used, and consider
1534 * the worst case : SSL used on both sides and doing a handshake
1535 * simultaneously. Note that we can't have more than maxconn
1536 * handshakes at a time by definition, so for the worst case of
1537 * two SSL conns per connection, we count a single handshake.
1538 */
1539 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1540 int64_t mem = global.rlimit_memmax * 1048576ULL;
1541
1542 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1543 mem -= global.maxzlibmem;
1544 mem = mem * MEM_USABLE_RATIO;
1545
1546 global.maxconn = mem /
Willy Tarreau87b09662015-04-03 00:22:06 +02001547 ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream
Willy Tarreaud0256482015-01-15 21:45:22 +01001548 sides * global.ssl_session_max_cost + // SSL buffers, one per side
1549 global.ssl_handshake_max_cost); // 1 handshake per connection max
1550
1551 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001552#ifdef SYSTEM_MAXCONN
1553 if (global.maxconn > DEFAULT_MAXCONN)
1554 global.maxconn = DEFAULT_MAXCONN;
1555#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001556 global.maxsslconn = sides * global.maxconn;
1557 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1558 fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n",
1559 global.maxconn, global.maxsslconn);
1560 }
1561 else if (!global.maxsslconn &&
1562 (global.ssl_used_frontend || global.ssl_used_backend)) {
1563 /* memmax and maxconn are known, compute maxsslconn automatically.
1564 * maxsslconn being forced, we don't know how many of it will be
1565 * on each side if both sides are being used. The worst case is
1566 * when all connections use only one SSL instance because
1567 * handshakes may be on two sides at the same time.
1568 */
1569 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1570 int64_t mem = global.rlimit_memmax * 1048576ULL;
1571 int64_t sslmem;
1572
1573 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1574 mem -= global.maxzlibmem;
1575 mem = mem * MEM_USABLE_RATIO;
1576
Willy Tarreau87b09662015-04-03 00:22:06 +02001577 sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001578 global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost);
1579 global.maxsslconn = round_2dig(global.maxsslconn);
1580
1581 if (sslmem <= 0 || global.maxsslconn < sides) {
1582 Alert("Cannot compute the automatic maxsslconn because global.maxconn is already too "
1583 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1584 "without SSL is %d, but %d was found and SSL is in use.\n",
1585 global.rlimit_memmax,
Willy Tarreau87b09662015-04-03 00:22:06 +02001586 (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)),
Willy Tarreaud0256482015-01-15 21:45:22 +01001587 global.maxconn);
1588 exit(1);
1589 }
1590
1591 if (global.maxsslconn > sides * global.maxconn)
1592 global.maxsslconn = sides * global.maxconn;
1593
1594 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1595 fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn);
1596 }
1597#endif
1598 else if (!global.maxconn) {
1599 /* memmax and maxsslconn are known/unused, compute maxconn automatically */
1600 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1601 int64_t mem = global.rlimit_memmax * 1048576ULL;
1602 int64_t clearmem;
1603
1604 if (global.ssl_used_frontend || global.ssl_used_backend)
1605 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1606
1607 mem -= global.maxzlibmem;
1608 mem = mem * MEM_USABLE_RATIO;
1609
1610 clearmem = mem;
1611 if (sides)
1612 clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn;
1613
Willy Tarreau87b09662015-04-03 00:22:06 +02001614 global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001615 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001616#ifdef SYSTEM_MAXCONN
1617 if (global.maxconn > DEFAULT_MAXCONN)
1618 global.maxconn = DEFAULT_MAXCONN;
1619#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001620
1621 if (clearmem <= 0 || !global.maxconn) {
1622 Alert("Cannot compute the automatic maxconn because global.maxsslconn is already too "
1623 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1624 "is %d, but %d was found.\n",
1625 global.rlimit_memmax,
1626 (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)),
1627 global.maxsslconn);
1628 exit(1);
1629 }
1630
1631 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
1632 if (sides && global.maxsslconn > sides * global.maxconn) {
1633 fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn "
1634 "to be limited to %d. Better reduce global.maxsslconn to get more "
1635 "room for extra connections.\n", global.maxsslconn, global.maxconn);
1636 }
1637 fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn);
1638 }
1639 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001640
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001641 if (!global.maxpipes) {
1642 /* maxpipes not specified. Count how many frontends and backends
1643 * may be using splicing, and bound that to maxconn.
1644 */
1645 struct proxy *cur;
1646 int nbfe = 0, nbbe = 0;
1647
1648 for (cur = proxy; cur; cur = cur->next) {
1649 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
1650 if (cur->cap & PR_CAP_FE)
1651 nbfe += cur->maxconn;
1652 if (cur->cap & PR_CAP_BE)
Willy Tarreauafb48762009-01-25 10:42:05 +01001653 nbbe += cur->fullconn ? cur->fullconn : global.maxconn;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001654 }
1655 }
1656 global.maxpipes = MAX(nbfe, nbbe);
1657 if (global.maxpipes > global.maxconn)
1658 global.maxpipes = global.maxconn;
Willy Tarreau686ac822009-01-25 14:06:58 +01001659 global.maxpipes /= 4;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001660 }
1661
1662
Willy Tarreauabacc2c2011-09-07 14:26:33 +02001663 global.hardmaxconn = global.maxconn; /* keep this max value */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001664 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
Willy Tarreau3ec79b92009-01-18 20:39:42 +01001665 global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001666
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001667 if (global.stats_fe)
1668 global.maxsock += global.stats_fe->maxconn;
1669
1670 if (peers) {
1671 /* peers also need to bypass global maxconn */
1672 struct peers *p = peers;
1673
1674 for (p = peers; p; p = p->next)
1675 if (p->peers_fe)
1676 global.maxsock += p->peers_fe->maxconn;
1677 }
1678
Willy Tarreau1db37712007-06-03 17:16:49 +02001679 if (global.tune.maxpollevents <= 0)
1680 global.tune.maxpollevents = MAX_POLL_EVENTS;
1681
Willy Tarreau6f4a82c2009-03-21 20:43:57 +01001682 if (global.tune.recv_enough == 0)
1683 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
1684
Willy Tarreau27097842015-09-28 13:53:23 +02001685 if (global.tune.maxrewrite < 0)
1686 global.tune.maxrewrite = MAXREWRITE;
1687
Willy Tarreau27a674e2009-08-17 07:23:33 +02001688 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
1689 global.tune.maxrewrite = global.tune.bufsize / 2;
1690
Willy Tarreaubaaee002006-06-26 02:48:02 +02001691 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
1692 /* command line debug mode inhibits configuration mode */
William Lallemand095ba4c2017-06-01 17:38:50 +02001693 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001694 global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND));
1695 }
1696
William Lallemand095ba4c2017-06-01 17:38:50 +02001697 if (arg_mode & MODE_DAEMON) {
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001698 /* command line daemon mode inhibits foreground and debug modes mode */
1699 global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND);
William Lallemand095ba4c2017-06-01 17:38:50 +02001700 global.mode |= arg_mode & MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001701 }
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001702
1703 global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001704
William Lallemand095ba4c2017-06-01 17:38:50 +02001705 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
1706 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
1707 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001708 }
1709
William Lallemand095ba4c2017-06-01 17:38:50 +02001710 if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001711 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
1712 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
1713 global.nbproc = 1;
1714 }
1715
1716 if (global.nbproc < 1)
1717 global.nbproc = 1;
1718
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001719 swap_buffer = calloc(1, global.tune.bufsize);
1720 get_http_auth_buff = calloc(1, global.tune.bufsize);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02001721 static_table_key = calloc(1, sizeof(*static_table_key));
Willy Tarreau8096de92010-02-26 11:12:27 +01001722
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001723 fdinfo = calloc(1, sizeof(struct fdinfo) * (global.maxsock));
1724 fdtab = calloc(1, sizeof(struct fdtab) * (global.maxsock));
Willy Tarreauef1d1f82007-04-16 00:25:25 +02001725 /*
1726 * Note: we could register external pollers here.
1727 * Built-in pollers have been registered before main().
1728 */
Willy Tarreau4f60f162007-04-08 16:39:58 +02001729
Willy Tarreau43b78992009-01-25 15:42:27 +01001730 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001731 disable_poller("kqueue");
1732
Willy Tarreau43b78992009-01-25 15:42:27 +01001733 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001734 disable_poller("epoll");
1735
Willy Tarreau43b78992009-01-25 15:42:27 +01001736 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001737 disable_poller("poll");
1738
Willy Tarreau43b78992009-01-25 15:42:27 +01001739 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001740 disable_poller("select");
1741
1742 /* Note: we could disable any poller by name here */
1743
Christopher Fauletb3f4e142016-03-07 12:46:38 +01001744 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +02001745 list_pollers(stderr);
Christopher Fauletb3f4e142016-03-07 12:46:38 +01001746 fprintf(stderr, "\n");
1747 list_filters(stderr);
1748 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02001749
Willy Tarreau4f60f162007-04-08 16:39:58 +02001750 if (!init_pollers()) {
Willy Tarreau3fa87b12013-03-31 14:41:15 +02001751 Alert("No polling mechanism available.\n"
1752 " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n"
1753 " is too low on this platform to support maxconn and the number of listeners\n"
1754 " and servers. You should rebuild haproxy specifying your system using TARGET=\n"
1755 " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n"
Prach Pongpanichb837e682013-05-14 20:56:28 +02001756 " global maxconn setting to accommodate the system's limitation. For reference,\n"
Willy Tarreau3fa87b12013-03-31 14:41:15 +02001757 " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n"
1758 " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n"
1759 " check build settings using 'haproxy -vv'.\n\n",
1760 FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001761 exit(1);
1762 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02001763 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
1764 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001765 }
1766
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001767 if (!global.node)
1768 global.node = strdup(hostname);
1769
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01001770 if (!hlua_post_init())
1771 exit(1);
Thomas Holmes6abded42015-05-12 16:23:58 +01001772
Baptiste Assmann325137d2015-04-13 23:40:55 +02001773 /* initialize structures for name resolution */
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001774 if (!dns_init_resolvers(0))
Baptiste Assmann325137d2015-04-13 23:40:55 +02001775 exit(1);
Maxime de Roucy0f503922016-05-13 23:52:55 +02001776
1777 free(err_msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001778}
1779
Simon Horman6fb82592011-07-15 13:14:11 +09001780static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +09001781{
Simon Hormanac821422011-07-15 13:14:09 +09001782 struct acl_term_suite *suite, *suiteb;
1783 struct acl_term *term, *termb;
1784
Simon Horman6fb82592011-07-15 13:14:11 +09001785 if (!cond)
1786 return;
1787
1788 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
1789 list_for_each_entry_safe(term, termb, &suite->terms, list) {
1790 LIST_DEL(&term->list);
1791 free(term);
Simon Hormanac821422011-07-15 13:14:09 +09001792 }
Simon Horman6fb82592011-07-15 13:14:11 +09001793 LIST_DEL(&suite->list);
1794 free(suite);
1795 }
1796
1797 free(cond);
1798}
1799
1800static void deinit_tcp_rules(struct list *rules)
1801{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001802 struct act_rule *trule, *truleb;
Simon Horman6fb82592011-07-15 13:14:11 +09001803
1804 list_for_each_entry_safe(trule, truleb, rules, list) {
Simon Hormanac821422011-07-15 13:14:09 +09001805 LIST_DEL(&trule->list);
Simon Horman6fb82592011-07-15 13:14:11 +09001806 deinit_acl_cond(trule->cond);
Simon Hormanac821422011-07-15 13:14:09 +09001807 free(trule);
1808 }
1809}
1810
Simon Horman6fb82592011-07-15 13:14:11 +09001811static void deinit_stick_rules(struct list *rules)
1812{
1813 struct sticking_rule *rule, *ruleb;
1814
1815 list_for_each_entry_safe(rule, ruleb, rules, list) {
1816 LIST_DEL(&rule->list);
1817 deinit_acl_cond(rule->cond);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001818 release_sample_expr(rule->expr);
Simon Horman6fb82592011-07-15 13:14:11 +09001819 free(rule);
1820 }
1821}
1822
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001823void deinit(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001824{
Willy Tarreau4d2d0982007-05-14 00:39:29 +02001825 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001826 struct cap_hdr *h,*h_next;
1827 struct server *s,*s_next;
1828 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001829 struct acl_cond *cond, *condb;
1830 struct hdr_exp *exp, *expb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001831 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001832 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001833 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001834 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001835 struct wordlist *wl, *wlb;
Willy Tarreauf4f04122010-01-28 18:10:50 +01001836 struct cond_wordlist *cwl, *cwlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001837 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +02001838 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +01001839 struct logformat_node *lf, *lfb;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001840 struct bind_conf *bind_conf, *bind_back;
Willy Tarreaucdb737e2016-12-21 18:43:10 +01001841 struct build_opts_str *bol, *bolb;
Willy Tarreau05554e62016-12-21 20:46:26 +01001842 struct post_deinit_fct *pdf;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001843 int i;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001844
Willy Tarreau24f4efa2010-08-27 17:56:48 +02001845 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001846 while (p) {
Willy Tarreau8113a5d2012-10-04 08:01:43 +02001847 free(p->conf.file);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001848 free(p->id);
1849 free(p->check_req);
1850 free(p->cookie_name);
1851 free(p->cookie_domain);
1852 free(p->url_param_name);
1853 free(p->capture_name);
1854 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +09001855 free(p->rdp_cookie_name);
Willy Tarreau62a61232013-04-12 18:13:46 +02001856 if (p->conf.logformat_string != default_http_log_format &&
1857 p->conf.logformat_string != default_tcp_log_format &&
1858 p->conf.logformat_string != clf_http_log_format)
1859 free(p->conf.logformat_string);
Willy Tarreau196729e2012-05-31 19:30:26 +02001860
Willy Tarreau62a61232013-04-12 18:13:46 +02001861 free(p->conf.lfs_file);
1862 free(p->conf.uniqueid_format_string);
1863 free(p->conf.uif_file);
Godbachaf1a75d2013-10-02 17:10:11 +08001864 free(p->lbprm.map.srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001865
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001866 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
1867 free(p->conf.logformat_sd_string);
1868 free(p->conf.lfsd_file);
1869
Willy Tarreaua534fea2008-08-03 12:19:50 +02001870 for (i = 0; i < HTTP_ERR_SIZE; i++)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001871 chunk_destroy(&p->errmsg[i]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001872
Willy Tarreauf4f04122010-01-28 18:10:50 +01001873 list_for_each_entry_safe(cwl, cwlb, &p->req_add, list) {
1874 LIST_DEL(&cwl->list);
1875 free(cwl->s);
1876 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001877 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001878
Willy Tarreauf4f04122010-01-28 18:10:50 +01001879 list_for_each_entry_safe(cwl, cwlb, &p->rsp_add, list) {
1880 LIST_DEL(&cwl->list);
1881 free(cwl->s);
1882 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001883 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001884
Willy Tarreaub80c2302007-11-30 20:51:32 +01001885 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
1886 LIST_DEL(&cond->list);
1887 prune_acl_cond(cond);
1888 free(cond);
1889 }
1890
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001891 for (exp = p->req_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001892 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02001893 regex_free(exp->preg);
1894 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001895 }
1896
Willy Tarreau98d04852015-05-26 12:18:29 +02001897 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001898 expb = exp;
1899 exp = exp->next;
1900 free(expb);
1901 }
1902
1903 for (exp = p->rsp_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001904 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02001905 regex_free(exp->preg);
1906 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001907 }
1908
Willy Tarreau98d04852015-05-26 12:18:29 +02001909 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001910 expb = exp;
1911 exp = exp->next;
1912 free(expb);
1913 }
1914
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001915 /* build a list of unique uri_auths */
1916 if (!ua)
1917 ua = p->uri_auth;
1918 else {
1919 /* check if p->uri_auth is unique */
1920 for (uap = ua; uap; uap=uap->next)
1921 if (uap == p->uri_auth)
1922 break;
1923
Willy Tarreauaccc4e12008-06-24 11:14:45 +02001924 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001925 /* add it, if it is */
1926 p->uri_auth->next = ua;
1927 ua = p->uri_auth;
1928 }
1929 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001930
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001931 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
1932 LIST_DEL(&acl->list);
1933 prune_acl(acl);
1934 free(acl);
1935 }
1936
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001937 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
1938 LIST_DEL(&srule->list);
1939 prune_acl_cond(srule->cond);
1940 free(srule->cond);
1941 free(srule);
1942 }
1943
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001944 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
1945 LIST_DEL(&rule->list);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001946 if (rule->cond) {
1947 prune_acl_cond(rule->cond);
1948 free(rule->cond);
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +01001949 free(rule->file);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001950 }
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001951 free(rule);
1952 }
1953
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001954 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
1955 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +01001956 if (rdr->cond) {
1957 prune_acl_cond(rdr->cond);
1958 free(rdr->cond);
1959 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001960 free(rdr->rdr_str);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01001961 list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) {
1962 LIST_DEL(&lf->list);
1963 free(lf);
1964 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001965 free(rdr);
1966 }
1967
William Lallemand0f99e342011-10-12 17:50:54 +02001968 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
1969 LIST_DEL(&log->list);
1970 free(log);
1971 }
1972
William Lallemand723b73a2012-02-08 16:37:49 +01001973 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
1974 LIST_DEL(&lf->list);
1975 free(lf);
1976 }
1977
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001978 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
1979 LIST_DEL(&lf->list);
1980 free(lf);
1981 }
1982
Simon Hormanac821422011-07-15 13:14:09 +09001983 deinit_tcp_rules(&p->tcp_req.inspect_rules);
1984 deinit_tcp_rules(&p->tcp_req.l4_rules);
1985
Simon Horman6fb82592011-07-15 13:14:11 +09001986 deinit_stick_rules(&p->storersp_rules);
1987 deinit_stick_rules(&p->sticking_rules);
1988
Willy Tarreaubaaee002006-06-26 02:48:02 +02001989 h = p->req_cap;
1990 while (h) {
1991 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001992 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001993 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001994 free(h);
1995 h = h_next;
1996 }/* end while(h) */
1997
1998 h = p->rsp_cap;
1999 while (h) {
2000 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002001 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +02002002 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002003 free(h);
2004 h = h_next;
2005 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002006
Willy Tarreaubaaee002006-06-26 02:48:02 +02002007 s = p->srv;
2008 while (s) {
2009 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002010
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002011 if (s->check.task) {
2012 task_delete(s->check.task);
2013 task_free(s->check.task);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002014 }
Simon Hormand60d6912013-11-25 10:46:36 +09002015 if (s->agent.task) {
2016 task_delete(s->agent.task);
2017 task_free(s->agent.task);
2018 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002019
Willy Tarreau2e993902011-10-31 11:53:20 +01002020 if (s->warmup) {
2021 task_delete(s->warmup);
2022 task_free(s->warmup);
2023 }
2024
Willy Tarreaua534fea2008-08-03 12:19:50 +02002025 free(s->id);
2026 free(s->cookie);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002027 free(s->check.bi);
2028 free(s->check.bo);
Simon Hormand60d6912013-11-25 10:46:36 +09002029 free(s->agent.bi);
2030 free(s->agent.bo);
James Brown55f9ff12015-10-21 18:19:05 -07002031 free(s->agent.send_string);
Sárközi, László34c01792014-09-05 10:08:23 +02002032 free((char*)s->conf.file);
Willy Tarreau17d45382016-12-22 21:16:08 +01002033
2034 if (s->use_ssl || s->check.use_ssl) {
2035 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2036 xprt_get(XPRT_SSL)->destroy_srv(s);
2037 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002038 free(s);
2039 s = s_next;
2040 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002041
Willy Tarreau4348fad2012-09-20 16:48:07 +02002042 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002043 /*
2044 * Zombie proxy, the listener just pretend to be up
2045 * because they still hold an opened fd.
2046 * Close it and give the listener its real state.
2047 */
2048 if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
2049 close(l->fd);
2050 l->state = LI_INIT;
2051 }
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02002052 unbind_listener(l);
2053 delete_listener(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002054 LIST_DEL(&l->by_fe);
2055 LIST_DEL(&l->by_bind);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002056 free(l->name);
2057 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002058 free(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002059 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002060
Willy Tarreau4348fad2012-09-20 16:48:07 +02002061 /* Release unused SSL configs. */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002062 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01002063 if (bind_conf->xprt->destroy_bind_conf)
2064 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002065 free(bind_conf->file);
2066 free(bind_conf->arg);
2067 LIST_DEL(&bind_conf->by_fe);
2068 free(bind_conf);
2069 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02002070
Christopher Fauletd7c91962015-04-30 11:48:27 +02002071 flt_deinit(p);
2072
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002073 free(p->desc);
2074 free(p->fwdfor_hdr_name);
2075
Willy Tarreauff011f22011-01-06 17:51:27 +01002076 free_http_req_rules(&p->http_req_rules);
Sasha Pachev218f0642014-06-16 12:05:59 -06002077 free_http_res_rules(&p->http_res_rules);
Willy Tarreau918ff602011-07-25 16:33:49 +02002078 free(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002079
Willy Tarreaucf7f3202007-05-13 22:46:04 +02002080 pool_destroy2(p->req_cap_pool);
2081 pool_destroy2(p->rsp_cap_pool);
Simon Hormanb08584a2011-07-15 13:14:10 +09002082 pool_destroy2(p->table.pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002083
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002084 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002085 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002086 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002087 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02002088
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002089 while (ua) {
2090 uap = ua;
2091 ua = ua->next;
2092
Willy Tarreaua534fea2008-08-03 12:19:50 +02002093 free(uap->uri_prefix);
2094 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002095 free(uap->node);
2096 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002097
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002098 userlist_free(uap->userlist);
Willy Tarreauff011f22011-01-06 17:51:27 +01002099 free_http_req_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002100
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002101 free(uap);
2102 }
2103
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002104 userlist_free(userlist);
2105
David Carlier834cb2e2015-09-25 12:02:25 +01002106 cfg_unregister_sections();
2107
2108 free_trash_buffers();
2109 chunk_destroy(&trash);
2110
Willy Tarreaudd815982007-10-16 12:25:14 +02002111 protocol_unbind_all();
2112
Willy Tarreau05554e62016-12-21 20:46:26 +01002113 list_for_each_entry(pdf, &post_deinit_list, list)
2114 pdf->fct();
2115
Joe Williamsdf5b38f2010-12-29 17:05:48 +01002116 free(global.log_send_hostname); global.log_send_hostname = NULL;
Dragan Dosen43885c72015-10-01 13:18:13 +02002117 chunk_destroy(&global.log_tag);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002118 free(global.chroot); global.chroot = NULL;
2119 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002120 free(global.node); global.node = NULL;
2121 free(global.desc); global.desc = NULL;
Godbach4cc1b0d2013-06-26 16:49:51 +08002122 free(fdinfo); fdinfo = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002123 free(fdtab); fdtab = NULL;
2124 free(oldpids); oldpids = NULL;
David Carlier834cb2e2015-09-25 12:02:25 +01002125 free(static_table_key); static_table_key = NULL;
2126 free(get_http_auth_buff); get_http_auth_buff = NULL;
2127 free(swap_buffer); swap_buffer = NULL;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002128 free(global_listener_queue_task); global_listener_queue_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002129
William Lallemand0f99e342011-10-12 17:50:54 +02002130 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
2131 LIST_DEL(&log->list);
2132 free(log);
2133 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01002134 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02002135 free(wl->s);
Willy Tarreau477ecd82010-01-03 21:12:30 +01002136 LIST_DEL(&wl->list);
2137 free(wl);
2138 }
2139
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002140 list_for_each_entry_safe(bol, bolb, &build_opts_list, list) {
2141 if (bol->must_free)
2142 free((void *)bol->str);
2143 LIST_DEL(&bol->list);
2144 free(bol);
2145 }
2146
Christopher Fauletff2613e2016-11-09 11:36:17 +01002147 vars_prune(&global.vars, NULL, NULL);
2148
Willy Tarreau87b09662015-04-03 00:22:06 +02002149 pool_destroy2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +02002150 pool_destroy2(pool2_session);
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002151 pool_destroy2(pool2_connection);
Willy Tarreaub686afd2017-02-08 11:06:11 +01002152 pool_destroy2(pool2_trash);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002153 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002154 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02002155 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +02002156 pool_destroy2(pool2_capture);
Willy Tarreaue4d7e552007-05-13 20:19:55 +02002157 pool_destroy2(pool2_pendconn);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002158 pool_destroy2(pool2_sig_handlers);
Willy Tarreau34eb6712011-10-24 18:15:04 +02002159 pool_destroy2(pool2_hdr_idx);
Willy Tarreau63986c72015-04-03 22:55:33 +02002160 pool_destroy2(pool2_http_txn);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002161 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002162} /* end deinit() */
2163
Willy Tarreaubaaee002006-06-26 02:48:02 +02002164
Willy Tarreau918ff602011-07-25 16:33:49 +02002165/* Runs the polling loop */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01002166static void run_poll_loop()
Willy Tarreau4f60f162007-04-08 16:39:58 +02002167{
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002168 int next;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002169
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002170 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002171 while (1) {
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002172 /* Process a few tasks */
2173 process_runnable_tasks();
2174
Willy Tarreau29857942009-05-10 09:01:21 +02002175 /* check if we caught some signals and process them */
2176 signal_process_queue();
2177
Willy Tarreau58b458d2008-06-29 22:40:23 +02002178 /* Check if we can expire some tasks */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002179 next = wake_expired_tasks();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002180
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002181 /* stop when there's nothing left to do */
2182 if (jobs == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +02002183 break;
2184
Willy Tarreau10146c92015-04-13 20:44:19 +02002185 /* expire immediately if events are pending */
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002186 if (fd_cache_num || tasks_run_queue || signal_queue_len || applets_active_queue)
Willy Tarreau10146c92015-04-13 20:44:19 +02002187 next = now_ms;
2188
Willy Tarreau58b458d2008-06-29 22:40:23 +02002189 /* The poller will ensure it returns around <next> */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002190 cur_poller.poll(&cur_poller, next);
Willy Tarreau033cd9d2014-01-25 19:24:15 +01002191 fd_process_cached_events();
Willy Tarreau3c595ac2015-04-19 09:59:31 +02002192 applet_run_active();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002193 }
2194}
2195
Willy Tarreaue9b26022011-08-01 20:57:55 +02002196/* This is the global management task for listeners. It enables listeners waiting
2197 * for global resources when there are enough free resource, or at least once in
2198 * a while. It is designed to be called as a task.
2199 */
2200static struct task *manage_global_listener_queue(struct task *t)
2201{
2202 int next = TICK_ETERNITY;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002203 /* queue is empty, nothing to do */
2204 if (LIST_ISEMPTY(&global_listener_queue))
2205 goto out;
2206
2207 /* If there are still too many concurrent connections, let's wait for
2208 * some of them to go away. We don't need to re-arm the timer because
2209 * each of them will scan the queue anyway.
2210 */
2211 if (unlikely(actconn >= global.maxconn))
2212 goto out;
2213
2214 /* We should periodically try to enable listeners waiting for a global
2215 * resource here, because it is possible, though very unlikely, that
2216 * they have been blocked by a temporary lack of global resource such
2217 * as a file descriptor or memory and that the temporary condition has
2218 * disappeared.
2219 */
Willy Tarreauabacc2c2011-09-07 14:26:33 +02002220 dequeue_all_listeners(&global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +02002221
2222 out:
2223 t->expire = next;
2224 task_queue(t);
2225 return t;
2226}
Willy Tarreau4f60f162007-04-08 16:39:58 +02002227
Willy Tarreaubaaee002006-06-26 02:48:02 +02002228int main(int argc, char **argv)
2229{
2230 int err, retry;
2231 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02002232 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02002233 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002234
Emeric Bruncf20bf12010-10-22 16:06:11 +02002235 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002236 signal_register_fct(SIGQUIT, dump, SIGQUIT);
2237 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
2238 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
William Lallemand73b85e72017-06-01 17:38:51 +02002239 signal_register_fct(SIGUSR2, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002240
Willy Tarreaue437c442010-03-17 18:02:46 +01002241 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
2242 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
2243 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002244 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002245 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002246
Willy Tarreaudc23a922011-02-16 11:10:36 +01002247 /* ulimits */
2248 if (!global.rlimit_nofile)
2249 global.rlimit_nofile = global.maxsock;
2250
2251 if (global.rlimit_nofile) {
2252 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
2253 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
Willy Tarreauef635472016-06-21 11:48:18 +02002254 /* try to set it to the max possible at least */
2255 getrlimit(RLIMIT_NOFILE, &limit);
Willy Tarreau164dd0b2016-06-21 11:51:59 +02002256 limit.rlim_cur = limit.rlim_max;
2257 if (setrlimit(RLIMIT_NOFILE, &limit) != -1)
2258 getrlimit(RLIMIT_NOFILE, &limit);
2259
Willy Tarreauef635472016-06-21 11:48:18 +02002260 Warning("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n", argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
2261 global.rlimit_nofile = limit.rlim_cur;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002262 }
2263 }
2264
2265 if (global.rlimit_memmax) {
2266 limit.rlim_cur = limit.rlim_max =
Willy Tarreau70060452015-12-14 12:46:07 +01002267 global.rlimit_memmax * 1048576ULL;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002268#ifdef RLIMIT_AS
2269 if (setrlimit(RLIMIT_AS, &limit) == -1) {
2270 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2271 argv[0], global.rlimit_memmax);
2272 }
2273#else
2274 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
2275 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2276 argv[0], global.rlimit_memmax);
2277 }
2278#endif
2279 }
2280
Olivier Houchardf73629d2017-04-05 22:33:04 +02002281 if (old_unixsocket) {
William Lallemand85b0bd92017-06-01 17:38:53 +02002282 if (strcmp("/dev/null", old_unixsocket) != 0) {
2283 if (get_old_sockets(old_unixsocket) != 0) {
2284 Alert("Failed to get the sockets from the old process!\n");
2285 if (!(global.mode & MODE_MWORKER))
2286 exit(1);
2287 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002288 }
2289 }
William Lallemand85b0bd92017-06-01 17:38:53 +02002290 get_cur_unixsocket();
2291
Willy Tarreaubaaee002006-06-26 02:48:02 +02002292 /* We will loop at most 100 times with 10 ms delay each time.
2293 * That's at most 1 second. We only send a signal to old pids
2294 * if we cannot grab at least one port.
2295 */
2296 retry = MAX_START_RETRIES;
2297 err = ERR_NONE;
2298 while (retry >= 0) {
2299 struct timeval w;
2300 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01002301 /* exit the loop on no error or fatal error */
2302 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002303 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02002304 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002305 break;
2306
2307 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
2308 * listening sockets. So on those platforms, it would be wiser to
2309 * simply send SIGUSR1, which will not be undoable.
2310 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02002311 if (tell_old_pids(SIGTTOU) == 0) {
2312 /* no need to wait if we can't contact old pids */
2313 retry = 0;
2314 continue;
2315 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002316 /* give some time to old processes to stop listening */
2317 w.tv_sec = 0;
2318 w.tv_usec = 10*1000;
2319 select(0, NULL, NULL, NULL, &w);
2320 retry--;
2321 }
2322
2323 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01002324 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02002325 if (retry != MAX_START_RETRIES && nb_oldpids) {
2326 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002327 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02002328 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002329 exit(1);
2330 }
2331
2332 if (listeners == 0) {
Willy Tarreauf2ee0162015-08-09 11:01:51 +02002333 Alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002334 /* Note: we don't have to send anything to the old pids because we
2335 * never stopped them. */
2336 exit(1);
2337 }
2338
Emeric Bruncf20bf12010-10-22 16:06:11 +02002339 err = protocol_bind_all(errmsg, sizeof(errmsg));
2340 if ((err & ~ERR_WARN) != ERR_NONE) {
2341 if ((err & ERR_ALERT) || (err & ERR_WARN))
2342 Alert("[%s.main()] %s.\n", argv[0], errmsg);
2343
Willy Tarreaudd815982007-10-16 12:25:14 +02002344 Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
2345 protocol_unbind_all(); /* cleanup everything we can */
2346 if (nb_oldpids)
2347 tell_old_pids(SIGTTIN);
2348 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002349 } else if (err & ERR_WARN) {
2350 Alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02002351 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002352 /* Ok, all listener should now be bound, close any leftover sockets
2353 * the previous process gave us, we don't need them anymore
2354 */
2355 while (xfer_sock_list != NULL) {
2356 struct xfer_sock_list *tmpxfer = xfer_sock_list->next;
2357 close(xfer_sock_list->fd);
2358 free(xfer_sock_list->iface);
2359 free(xfer_sock_list->namespace);
2360 free(xfer_sock_list);
2361 xfer_sock_list = tmpxfer;
2362 }
Willy Tarreaudd815982007-10-16 12:25:14 +02002363
Willy Tarreaubaaee002006-06-26 02:48:02 +02002364 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002365 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
2366 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002367
Willy Tarreaubaaee002006-06-26 02:48:02 +02002368 /* MODE_QUIET can inhibit alerts and warnings below this line */
2369
2370 global.mode &= ~MODE_STARTING;
2371 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
2372 /* detach from the tty */
2373 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002374 }
2375
2376 /* open log & pid files before the chroot */
William Lallemand095ba4c2017-06-01 17:38:50 +02002377 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002378 unlink(global.pidfile);
2379 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
2380 if (pidfd < 0) {
2381 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
2382 if (nb_oldpids)
2383 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02002384 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002385 exit(1);
2386 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002387 }
2388
Willy Tarreaub38651a2007-03-24 17:24:39 +01002389 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
2390 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002391 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002392 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01002393 exit(1);
2394 }
2395
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002396 /* If the user is not root, we'll still let him try the configuration
2397 * but we inform him that unexpected behaviour may occur.
2398 */
2399 if ((global.last_checks & LSTCHK_NETADM) && getuid())
2400 Warning("[%s.main()] Some options which require full privileges"
2401 " might not work well.\n"
2402 "", argv[0]);
2403
William Lallemand095ba4c2017-06-01 17:38:50 +02002404 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2405
2406 /* chroot if needed */
2407 if (global.chroot != NULL) {
2408 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
2409 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
2410 if (nb_oldpids)
2411 tell_old_pids(SIGTTIN);
2412 protocol_unbind_all();
2413 exit(1);
2414 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002415 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002416 }
2417
Willy Tarreaubaaee002006-06-26 02:48:02 +02002418 if (nb_oldpids)
Willy Tarreaubb545b42010-08-25 12:58:59 +02002419 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002420
2421 /* Note that any error at this stage will be fatal because we will not
2422 * be able to restart the old pids.
2423 */
2424
William Lallemand095ba4c2017-06-01 17:38:50 +02002425 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2426 /* setgid / setuid */
2427 if (global.gid) {
2428 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
2429 Warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2430 " without 'uid'/'user' is generally useless.\n", argv[0]);
2431
2432 if (setgid(global.gid) == -1) {
2433 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
2434 protocol_unbind_all();
2435 exit(1);
2436 }
2437 }
Michael Schererab012dd2013-01-12 18:35:19 +01002438
William Lallemand095ba4c2017-06-01 17:38:50 +02002439 if (global.uid && setuid(global.uid) == -1) {
2440 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Michael Schererab012dd2013-01-12 18:35:19 +01002441 protocol_unbind_all();
2442 exit(1);
2443 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002444 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002445 /* check ulimits */
2446 limit.rlim_cur = limit.rlim_max = 0;
2447 getrlimit(RLIMIT_NOFILE, &limit);
2448 if (limit.rlim_cur < global.maxsock) {
2449 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 +02002450 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002451 }
2452
William Lallemand095ba4c2017-06-01 17:38:50 +02002453 if (global.mode & (MODE_DAEMON | MODE_MWORKER)) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002454 struct proxy *px;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002455 struct peers *curpeers;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002456 int ret = 0;
2457 int proc;
2458
William Lallemand73b85e72017-06-01 17:38:51 +02002459 children = calloc(global.nbproc, sizeof(int));
William Lallemand095ba4c2017-06-01 17:38:50 +02002460 /*
2461 * if daemon + mworker: must fork here to let a master
2462 * process live in background before forking children
2463 */
William Lallemand73b85e72017-06-01 17:38:51 +02002464
2465 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
2466 && (global.mode & MODE_MWORKER)
2467 && (global.mode & MODE_DAEMON)) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002468 ret = fork();
2469 if (ret < 0) {
2470 Alert("[%s.main()] Cannot fork.\n", argv[0]);
2471 protocol_unbind_all();
2472 exit(1); /* there has been an error */
2473 }
2474 /* parent leave to daemonize */
2475 if (ret > 0)
2476 exit(0);
2477 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002478 /* the father launches the required number of processes */
2479 for (proc = 0; proc < global.nbproc; proc++) {
2480 ret = fork();
2481 if (ret < 0) {
2482 Alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002483 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +02002484 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002485 }
2486 else if (ret == 0) /* child breaks here */
2487 break;
Marc-Antoine Perennou992709b2013-02-12 10:53:52 +01002488 children[proc] = ret;
Willy Tarreau269ab312012-09-05 08:02:48 +02002489 if (pidfd >= 0) {
2490 char pidstr[100];
2491 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002492 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002493 }
Willy Tarreaudcd47712007-11-04 23:35:08 +01002494 relative_pid++; /* each child will get a different one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002495 }
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002496
2497#ifdef USE_CPU_AFFINITY
2498 if (proc < global.nbproc && /* child */
Willy Tarreaue7597492015-04-20 11:36:57 +02002499 proc < LONGBITS && /* only the first 32/64 processes may be pinned */
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002500 global.cpu_map[proc]) /* only do this if the process has a CPU map */
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002501#ifdef __FreeBSD__
2502 cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(unsigned long), (void *)&global.cpu_map[proc]);
2503#else
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002504 sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map[proc]);
2505#endif
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002506#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002507 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02002508 if (pidfd >= 0) {
2509 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
2510 close(pidfd);
2511 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02002512
2513 /* We won't ever use this anymore */
Willy Tarreaud137dd32010-08-25 12:49:05 +02002514 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002515
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002516 if (proc == global.nbproc) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002517 if (global.mode & MODE_MWORKER) {
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002518 protocol_unbind_all();
William Lallemand73b85e72017-06-01 17:38:51 +02002519 mworker_wait();
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002520 }
Grant Zhang872f9c22017-01-21 01:10:18 +00002521#ifndef OPENSSL_NO_DH
2522 ssl_free_dh();
2523#endif
William Lallemandcb11fd22017-06-01 17:38:52 +02002524 /* should never get there */
2525 exit(EXIT_FAILURE);
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002526 }
2527
William Lallemandcb11fd22017-06-01 17:38:52 +02002528 /* child must never use the atexit function */
2529 atexit_flag = 0;
2530
William Lallemand095ba4c2017-06-01 17:38:50 +02002531 /* Must chroot and setgid/setuid in the children */
2532 /* chroot if needed */
2533 if (global.chroot != NULL) {
2534 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
2535 Alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot);
2536 if (nb_oldpids)
2537 tell_old_pids(SIGTTIN);
2538 protocol_unbind_all();
2539 exit(1);
2540 }
2541 }
2542
2543 free(global.chroot);
2544 global.chroot = NULL;
2545
2546 /* setgid / setuid */
2547 if (global.gid) {
2548 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
2549 Warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2550 " without 'uid'/'user' is generally useless.\n", argv[0]);
2551
2552 if (setgid(global.gid) == -1) {
2553 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
2554 protocol_unbind_all();
2555 exit(1);
2556 }
2557 }
2558
2559 if (global.uid && setuid(global.uid) == -1) {
2560 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
2561 protocol_unbind_all();
2562 exit(1);
2563 }
2564
William Lallemand7f80eb22017-05-26 18:19:55 +02002565 /* pass through every cli socket, and check if it's bound to
2566 * the current process and if it exposes listeners sockets.
2567 * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork.
2568 * */
2569
2570 if (global.stats_fe) {
2571 struct bind_conf *bind_conf;
2572
2573 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
2574 if (bind_conf->level & ACCESS_FD_LISTENERS) {
2575 if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) {
2576 global.tune.options |= GTUNE_SOCKET_TRANSFER;
2577 break;
2578 }
2579 }
2580 }
2581 }
2582
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002583 /* we might have to unbind some proxies from some processes */
2584 px = proxy;
2585 while (px != NULL) {
2586 if (px->bind_proc && px->state != PR_STSTOPPED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002587 if (!(px->bind_proc & (1UL << proc))) {
2588 if (global.tune.options & GTUNE_SOCKET_TRANSFER)
2589 zombify_proxy(px);
2590 else
2591 stop_proxy(px);
2592 }
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002593 }
2594 px = px->next;
2595 }
2596
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002597 /* we might have to unbind some peers sections from some processes */
2598 for (curpeers = peers; curpeers; curpeers = curpeers->next) {
2599 if (!curpeers->peers_fe)
2600 continue;
2601
2602 if (curpeers->peers_fe->bind_proc & (1UL << proc))
2603 continue;
2604
2605 stop_proxy(curpeers->peers_fe);
2606 /* disable this peer section so that it kills itself */
Willy Tarreau47c8c022015-09-28 16:39:25 +02002607 signal_unregister_handler(curpeers->sighandler);
2608 task_delete(curpeers->sync_task);
2609 task_free(curpeers->sync_task);
2610 curpeers->sync_task = NULL;
2611 task_free(curpeers->peers_fe->task);
2612 curpeers->peers_fe->task = NULL;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002613 curpeers->peers_fe = NULL;
2614 }
2615
Willy Tarreaubaaee002006-06-26 02:48:02 +02002616 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
2617 * that we can detach from the TTY. We MUST NOT do it in other cases since
2618 * it would have already be done, and 0-2 would have been affected to listening
2619 * sockets
2620 */
Willy Tarreau106cb762008-11-16 07:40:34 +01002621 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002622 /* detach from the tty */
2623 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreau106cb762008-11-16 07:40:34 +01002624 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002625 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
2626 }
2627 pid = getpid(); /* update child's pid */
2628 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02002629 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002630 }
2631
Baptiste Assmann26c6eb82017-02-02 23:14:51 +01002632 /* initialize structures for name resolution */
2633 if (!dns_init_resolvers(1))
2634 exit(1);
2635
Willy Tarreaudd815982007-10-16 12:25:14 +02002636 protocol_enable_all();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002637 /*
2638 * That's it : the central polling loop. Run until we stop.
2639 */
2640 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002641
Willy Tarreaubaaee002006-06-26 02:48:02 +02002642 /* Do some cleanup */
2643 deinit();
2644
2645 exit(0);
2646}
2647
2648
2649/*
2650 * Local variables:
2651 * c-indent-level: 8
2652 * c-basic-offset: 8
2653 * End:
2654 */