blob: 30e850c4ac4719b71adccb3b6bd41248ef5bb470 [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>
Willy Tarreau50bc31d2017-08-16 15:35:19 +0200112#ifdef USE_OPENSSL
Grant Zhang872f9c22017-01-21 01:10:18 +0000113#include <proto/ssl_sock.h>
Willy Tarreau50bc31d2017-08-16 15:35:19 +0200114#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115
Willy Tarreau477ecd82010-01-03 21:12:30 +0100116/* list of config files */
117static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100119int relative_pid = 1; /* process id starting at 1 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120
121/* global options */
122struct global global = {
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100123 .hard_stop_after = TICK_ETERNITY,
Willy Tarreau247a13a2012-11-15 17:38:15 +0100124 .nbproc = 1,
William Lallemand5f232402012-04-05 18:02:55 +0200125 .req_count = 0,
William Lallemand0f99e342011-10-12 17:50:54 +0200126 .logsrvs = LIST_HEAD_INIT(global.logsrvs),
William Lallemand9d5f5482012-11-07 16:12:57 +0100127 .maxzlibmem = 0,
William Lallemandd85f9172012-11-09 17:05:39 +0100128 .comp_rate_lim = 0,
Emeric Brun850efd52014-01-29 12:24:34 +0100129 .ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED,
Emeric Bruned760922010-10-22 17:59:25 +0200130 .unix_bind = {
131 .ux = {
132 .uid = -1,
133 .gid = -1,
134 .mode = 0,
135 }
136 },
Willy Tarreau27a674e2009-08-17 07:23:33 +0200137 .tune = {
138 .bufsize = BUFSIZE,
Willy Tarreau27097842015-09-28 13:53:23 +0200139 .maxrewrite = -1,
Willy Tarreau43961d52010-10-04 20:39:20 +0200140 .chksize = BUFSIZE,
Willy Tarreaua24adf02014-11-27 01:11:56 +0100141 .reserved_bufs = RESERVED_BUFS,
Willy Tarreauf3045d22015-04-29 16:24:50 +0200142 .pattern_cache = DEFAULT_PAT_LRU_SIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200143#ifdef USE_OPENSSL
Emeric Brun46635772012-11-14 11:32:56 +0100144 .sslcachesize = SSLCACHESIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200145#endif
William Lallemandf3747832012-11-09 12:33:10 +0100146 .comp_maxlevel = 1,
Willy Tarreau7e312732014-02-12 16:35:14 +0100147#ifdef DEFAULT_IDLE_TIMER
148 .idle_timer = DEFAULT_IDLE_TIMER,
149#else
150 .idle_timer = 1000, /* 1 second */
151#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200152 },
Emeric Brun76d88952012-10-05 15:47:31 +0200153#ifdef USE_OPENSSL
154#ifdef DEFAULT_MAXSSLCONN
Willy Tarreau403edff2012-09-06 11:58:37 +0200155 .maxsslconn = DEFAULT_MAXSSLCONN,
156#endif
Emeric Brun76d88952012-10-05 15:47:31 +0200157#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200158 /* others NULL OK */
159};
160
161/*********************************************************************/
162
163int stopping; /* non zero means stopping in progress */
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100164int killed; /* non zero means a hard-stop is triggered */
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200165int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166
167/* Here we store informations about the pids of the processes we may pause
168 * or kill. We will send them a signal every 10 ms until we can bind to all
169 * our ports. With 200 retries, that's about 2 seconds.
170 */
171#define MAX_START_RETRIES 200
Willy Tarreaubaaee002006-06-26 02:48:02 +0200172static int *oldpids = NULL;
173static int oldpids_sig; /* use USR1 or TERM */
174
Olivier Houchardf73629d2017-04-05 22:33:04 +0200175/* Path to the unix socket we use to retrieve listener sockets from the old process */
176static const char *old_unixsocket;
177
William Lallemand85b0bd92017-06-01 17:38:53 +0200178static char *cur_unixsocket = NULL;
179
Willy Tarreaubaaee002006-06-26 02:48:02 +0200180/* this is used to drain data, and as a temporary buffer for sprintf()... */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100181struct chunk trash = { };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182
Willy Tarreau8096de92010-02-26 11:12:27 +0100183/* this buffer is always the same size as standard buffers and is used for
184 * swapping data inside a buffer.
185 */
186char *swap_buffer = NULL;
187
William Lallemandcb11fd22017-06-01 17:38:52 +0200188int atexit_flag = 0;
189
Willy Tarreaubb545b42010-08-25 12:58:59 +0200190int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191const int zero = 0;
192const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200193const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200194
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100195char hostname[MAX_HOSTNAME_LEN];
Emeric Brun2b920a12010-09-23 18:30:22 +0200196char localpeer[MAX_HOSTNAME_LEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197
Willy Tarreau89efaed2013-12-13 15:14:55 +0100198/* used from everywhere just to drain results we don't want to read and which
199 * recent versions of gcc increasingly and annoyingly complain about.
200 */
201int shut_your_big_mouth_gcc_int = 0;
202
William Lallemand73b85e72017-06-01 17:38:51 +0200203int *children = NULL; /* store PIDs of children in master workers mode */
204
205static volatile sig_atomic_t caught_signal = 0;
206static char **next_argv = NULL;
207
William Lallemande20b6a62017-06-01 17:38:55 +0200208int mworker_pipe[2];
209
Willy Tarreau08ceb102011-07-24 22:58:00 +0200210/* list of the temporarily limited listeners because of lack of resource */
211struct list global_listener_queue = LIST_HEAD_INIT(global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +0200212struct task *global_listener_queue_task;
213static struct task *manage_global_listener_queue(struct task *t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200214
Willy Tarreauff055502014-04-28 22:27:06 +0200215/* bitfield of a few warnings to emit just once (WARN_*) */
216unsigned int warned = 0;
217
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100218
219/* These are strings to be reported in the output of "haproxy -vv". They may
220 * either be constants (in which case must_free must be zero) or dynamically
221 * allocated strings to pass to free() on exit, and in this case must_free
222 * must be non-zero.
223 */
224struct list build_opts_list = LIST_HEAD_INIT(build_opts_list);
225struct build_opts_str {
226 struct list list;
227 const char *str;
228 int must_free;
229};
230
Willy Tarreaue6945732016-12-21 19:57:00 +0100231/* These functions are called just after the point where the program exits
232 * after a config validity check, so they are generally suited for resource
233 * allocation and slow initializations that should be skipped during basic
234 * config checks. The functions must return 0 on success, or a combination
235 * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause
236 * and immediate exit, so the function must have emitted any useful error.
237 */
238struct list post_check_list = LIST_HEAD_INIT(post_check_list);
239struct post_check_fct {
240 struct list list;
241 int (*fct)();
242};
243
Willy Tarreau05554e62016-12-21 20:46:26 +0100244/* These functions are called when freeing the global sections at the end
245 * of deinit, after everything is stopped. They don't return anything, and
246 * they work in best effort mode as their sole goal is to make valgrind
247 * mostly happy.
248 */
249struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list);
250struct post_deinit_fct {
251 struct list list;
252 void (*fct)();
253};
254
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255/*********************************************************************/
256/* general purpose functions ***************************************/
257/*********************************************************************/
258
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100259/* used to register some build option strings at boot. Set must_free to
260 * non-zero if the string must be freed upon exit.
261 */
262void hap_register_build_opts(const char *str, int must_free)
263{
264 struct build_opts_str *b;
265
266 b = calloc(1, sizeof(*b));
267 if (!b) {
268 fprintf(stderr, "out of memory\n");
269 exit(1);
270 }
271 b->str = str;
272 b->must_free = must_free;
273 LIST_ADDQ(&build_opts_list, &b->list);
274}
275
Willy Tarreaue6945732016-12-21 19:57:00 +0100276/* used to register some initialization functions to call after the checks. */
277void hap_register_post_check(int (*fct)())
278{
279 struct post_check_fct *b;
280
281 b = calloc(1, sizeof(*b));
282 if (!b) {
283 fprintf(stderr, "out of memory\n");
284 exit(1);
285 }
286 b->fct = fct;
287 LIST_ADDQ(&post_check_list, &b->list);
288}
289
Willy Tarreau05554e62016-12-21 20:46:26 +0100290/* used to register some de-initialization functions to call after everything
291 * has stopped.
292 */
293void hap_register_post_deinit(void (*fct)())
294{
295 struct post_deinit_fct *b;
296
297 b = calloc(1, sizeof(*b));
298 if (!b) {
299 fprintf(stderr, "out of memory\n");
300 exit(1);
301 }
302 b->fct = fct;
303 LIST_ADDQ(&post_deinit_list, &b->list);
304}
305
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100306static void display_version()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307{
308 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau7b677262017-04-03 09:27:49 +0200309 printf("Copyright 2000-2017 Willy Tarreau <willy@haproxy.org>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200310}
311
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100312static void display_build_opts()
Willy Tarreau7b066db2007-12-02 11:28:59 +0100313{
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100314 struct build_opts_str *item;
315
Willy Tarreau7b066db2007-12-02 11:28:59 +0100316 printf("Build options :"
317#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100318 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100319#endif
320#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100321 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100322#endif
323#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100324 "\n CC = " BUILD_CC
325#endif
326#ifdef BUILD_CFLAGS
327 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100328#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100329#ifdef BUILD_OPTIONS
330 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100331#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200332 "\n\nDefault settings :"
333 "\n maxconn = %d, bufsize = %d, maxrewrite = %d, maxpollevents = %d"
334 "\n\n",
335 DEFAULT_MAXCONN, BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS);
Willy Tarreaube5b6852009-10-03 18:57:08 +0200336
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100337 list_for_each_entry(item, &build_opts_list, list) {
338 puts(item->str);
339 }
340
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100341 putchar('\n');
342
Willy Tarreaube5b6852009-10-03 18:57:08 +0200343 list_pollers(stdout);
344 putchar('\n');
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100345 list_filters(stdout);
346 putchar('\n');
Willy Tarreau7b066db2007-12-02 11:28:59 +0100347}
348
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349/*
350 * This function prints the command line usage and exits
351 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100352static void usage(char *name)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353{
354 display_version();
355 fprintf(stderr,
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200356 "Usage : %s [-f <cfgfile|cfgdir>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
Willy Tarreaua088d312015-10-08 11:58:48 +0200358 " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100359 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360 " -d enters debug mode ; -db only disables background mode.\n"
Willy Tarreau6e064432012-05-08 15:40:42 +0200361 " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200363 " -D goes daemon ; -C changes to <dir> before loading files.\n"
William Lallemand095ba4c2017-06-01 17:38:50 +0200364 " -W master-worker mode.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200366 " -c check mode : only check config files and exit\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367 " -n sets the maximum total # of connections (%d)\n"
368 " -m limits the usable amount of memory (in MB)\n"
369 " -N sets the default, per-proxy maximum # of connections (%d)\n"
Emeric Brun2b920a12010-09-23 18:30:22 +0200370 " -L set local peer name (default to hostname)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200371 " -p writes pids of all children to this file\n"
372#if defined(ENABLE_EPOLL)
373 " -de disables epoll() usage even when available\n"
374#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200375#if defined(ENABLE_KQUEUE)
376 " -dk disables kqueue() usage even when available\n"
377#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200378#if defined(ENABLE_POLL)
379 " -dp disables poll() usage even when available\n"
380#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200381#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100382 " -dS disables splice usage (broken on old kernels)\n"
383#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200384#if defined(USE_GETADDRINFO)
385 " -dG disables getaddrinfo() usage\n"
386#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000387#if defined(SO_REUSEPORT)
388 " -dR disables SO_REUSEPORT usage\n"
389#endif
Willy Tarreau3eed10e2016-11-07 21:03:16 +0100390 " -dr ignores server address resolution failures\n"
Emeric Brun850efd52014-01-29 12:24:34 +0100391 " -dV disables SSL verify on servers side\n"
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +0200392 " -sf/-st [pid ]* finishes/terminates old pids.\n"
Olivier Houchardf73629d2017-04-05 22:33:04 +0200393 " -x <unix_socket> get listening sockets from a unix socket\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200394 "\n",
395 name, DEFAULT_MAXCONN, cfg_maxpconn);
396 exit(1);
397}
398
399
400
401/*********************************************************************/
402/* more specific functions ***************************************/
403/*********************************************************************/
404
William Lallemand73b85e72017-06-01 17:38:51 +0200405/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
406 * pids the signal was correctly delivered to.
407 */
408static int tell_old_pids(int sig)
409{
410 int p;
411 int ret = 0;
412 for (p = 0; p < nb_oldpids; p++)
413 if (kill(oldpids[p], sig) == 0)
414 ret++;
415 return ret;
416}
417
418/* return 1 if a pid is a current child otherwise 0 */
419
420int current_child(int pid)
421{
422 int i;
423
424 for (i = 0; i < global.nbproc; i++) {
425 if (children[i] == pid)
426 return 1;
427 }
428 return 0;
429}
430
431static void mworker_signalhandler(int signum)
432{
433 caught_signal = signum;
434}
435
436static void mworker_register_signals()
437{
438 struct sigaction sa;
439 /* Here we are not using the haproxy async way
440 for signals because it does not exists in
441 the master */
442 memset(&sa, 0, sizeof(struct sigaction));
443 sa.sa_handler = &mworker_signalhandler;
444 sigaction(SIGHUP, &sa, NULL);
445 sigaction(SIGUSR1, &sa, NULL);
446 sigaction(SIGUSR2, &sa, NULL);
447 sigaction(SIGINT, &sa, NULL);
448 sigaction(SIGTERM, &sa, NULL);
449}
450
451static void mworker_block_signals()
452{
453 sigset_t set;
454
455 sigemptyset(&set);
456 sigaddset(&set, SIGUSR1);
457 sigaddset(&set, SIGUSR2);
458 sigaddset(&set, SIGHUP);
459 sigaddset(&set, SIGINT);
460 sigaddset(&set, SIGTERM);
461 sigprocmask(SIG_SETMASK, &set, NULL);
462}
463
464static void mworker_unblock_signals()
465{
466 sigset_t set;
467
468 sigemptyset(&set);
469 sigaddset(&set, SIGUSR1);
470 sigaddset(&set, SIGUSR2);
471 sigaddset(&set, SIGHUP);
472 sigaddset(&set, SIGINT);
473 sigaddset(&set, SIGTERM);
474 sigprocmask(SIG_UNBLOCK, &set, NULL);
475}
476
477static void mworker_unregister_signals()
478{
479 signal(SIGINT, SIG_DFL);
480 signal(SIGTERM, SIG_DFL);
481 signal(SIGHUP, SIG_IGN);
482 signal(SIGUSR1, SIG_IGN);
483 signal(SIGUSR2, SIG_IGN);
484}
485
486/*
487 * Send signal to every known children.
488 */
489
490static void mworker_kill(int sig)
491{
492 int i;
493
494 tell_old_pids(sig);
495 if (children) {
496 for (i = 0; i < global.nbproc; i++)
497 kill(children[i], sig);
498 }
499}
500
Willy Tarreaubaaee002006-06-26 02:48:02 +0200501/*
William Lallemand73b85e72017-06-01 17:38:51 +0200502 * remove a pid forom the olpid array and decrease nb_oldpids
503 * return 1 pid was found otherwise return 0
504 */
505
506int delete_oldpid(int pid)
507{
508 int i;
509
510 for (i = 0; i < nb_oldpids; i++) {
511 if (oldpids[i] == pid) {
512 oldpids[i] = oldpids[nb_oldpids - 1];
513 oldpids[nb_oldpids - 1] = 0;
514 nb_oldpids--;
515 return 1;
516 }
517 }
518 return 0;
519}
520
William Lallemand85b0bd92017-06-01 17:38:53 +0200521
522static void get_cur_unixsocket()
523{
524 /* if -x was used, try to update the stat socket if not available anymore */
525 if (global.stats_fe) {
526 struct bind_conf *bind_conf;
527
528 /* pass through all stats socket */
529 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
530 struct listener *l;
531
532 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
533
534 if (l->addr.ss_family == AF_UNIX &&
535 (bind_conf->level & ACCESS_FD_LISTENERS)) {
536 const struct sockaddr_un *un;
537
538 un = (struct sockaddr_un *)&l->addr;
539 /* priority to old_unixsocket */
540 if (!cur_unixsocket) {
541 cur_unixsocket = strdup(un->sun_path);
542 } else {
543 if (old_unixsocket && !strcmp(un->sun_path, old_unixsocket)) {
544 free(cur_unixsocket);
545 cur_unixsocket = strdup(old_unixsocket);
546 return;
547 }
548 }
549 }
550 }
551 }
552 }
553 if (!cur_unixsocket && old_unixsocket)
554 cur_unixsocket = strdup(old_unixsocket);
555}
556
William Lallemand73b85e72017-06-01 17:38:51 +0200557/*
558 * When called, this function reexec haproxy with -sf followed by current
559 * children PIDs and possibily old children PIDs if they didn't leave yet.
560 */
561static void mworker_reload()
562{
563 int next_argc = 0;
564 int j;
565 char *msg = NULL;
566
567 mworker_block_signals();
568 mworker_unregister_signals();
569 setenv("HAPROXY_MWORKER_REEXEC", "1", 1);
570
571 /* compute length */
572 while (next_argv[next_argc])
573 next_argc++;
574
William Lallemand85b0bd92017-06-01 17:38:53 +0200575 /* 1 for haproxy -sf, 2 for -x /socket */
576 next_argv = realloc(next_argv, (next_argc + 1 + 2 + global.nbproc + nb_oldpids + 1) * sizeof(char *));
William Lallemand73b85e72017-06-01 17:38:51 +0200577 if (next_argv == NULL)
578 goto alloc_error;
579
580
581 /* add -sf <PID>* to argv */
582 if (children || nb_oldpids > 0)
583 next_argv[next_argc++] = "-sf";
584 if (children) {
585 for (j = 0; j < global.nbproc; next_argc++,j++) {
586 next_argv[next_argc] = memprintf(&msg, "%d", children[j]);
587 if (next_argv[next_argc] == NULL)
588 goto alloc_error;
589 msg = NULL;
590 }
591 }
592 /* copy old process PIDs */
593 for (j = 0; j < nb_oldpids; next_argc++,j++) {
594 next_argv[next_argc] = memprintf(&msg, "%d", oldpids[j]);
595 if (next_argv[next_argc] == NULL)
596 goto alloc_error;
597 msg = NULL;
598 }
599 next_argv[next_argc] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200600
William Lallemand2bf6d622017-06-20 11:20:23 +0200601 /* add the -x option with the stat socket */
William Lallemand85b0bd92017-06-01 17:38:53 +0200602 if (cur_unixsocket) {
603
William Lallemand2bf6d622017-06-20 11:20:23 +0200604 next_argv[next_argc++] = "-x";
605 next_argv[next_argc++] = (char *)cur_unixsocket;
606 next_argv[next_argc++] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200607 }
608
William Lallemand73b85e72017-06-01 17:38:51 +0200609 deinit(); /* we don't want to leak FD there */
610 Warning("Reexecuting Master process\n");
611 execv(next_argv[0], next_argv);
612
613alloc_error:
614 Warning("Cannot allocate memory\n");
615 Warning("Failed to reexecute the master processs [%d]\n", pid);
616 return;
617}
618
619
620/*
621 * Wait for every children to exit
622 */
623
624static void mworker_wait()
625{
626 int exitpid = -1;
627 int status = 0;
628
629 mworker_register_signals();
630 mworker_unblock_signals();
631
632 while (1) {
633
634 while (((exitpid = wait(&status)) == -1) && errno == EINTR) {
635 int sig = caught_signal;
636 if (sig == SIGUSR2 || sig == SIGHUP) {
637 mworker_reload();
638 } else {
639 Warning("Exiting Master process...\n");
640 mworker_kill(sig);
641 mworker_unregister_signals();
642 }
643 caught_signal = 0;
644 }
645
646 if (exitpid == -1 && errno == ECHILD) {
647 Warning("All workers are left. Leaving... (%d)\n", status);
William Lallemandcb11fd22017-06-01 17:38:52 +0200648 atexit_flag = 0;
William Lallemand73b85e72017-06-01 17:38:51 +0200649 exit(status); /* parent must leave using the latest status code known */
650 }
651
652 if (WIFEXITED(status))
653 status = WEXITSTATUS(status);
654 else if (WIFSIGNALED(status))
655 status = 128 + WTERMSIG(status);
656 else if (WIFSTOPPED(status))
657 status = 128 + WSTOPSIG(status);
658 else
659 status = 255;
660
661 if (!children) {
662 Warning("Worker %d left with exit code %d\n", exitpid, status);
663 } else {
664 /* check if exited child was in the current children list */
665 if (current_child(exitpid)) {
666 Alert("Current worker %d left with exit code %d\n", exitpid, status);
William Lallemand69f9b3b2017-06-01 17:38:54 +0200667 if (status != 0 && status != 130 && status != 143
668 && global.tune.options & GTUNE_EXIT_ONFAILURE) {
669 Alert("exit-on-failure: killing every workers with SIGTERM\n");
670 mworker_kill(SIGTERM);
671 }
William Lallemand73b85e72017-06-01 17:38:51 +0200672 } else {
673 Warning("Former worker %d left with exit code %d\n", exitpid, status);
674 delete_oldpid(exitpid);
675 }
676 }
677 }
678}
679
William Lallemandcb11fd22017-06-01 17:38:52 +0200680
681/*
682 * Reexec the process in failure mode, instead of exiting
683 */
684void reexec_on_failure()
685{
686 if (!atexit_flag)
687 return;
688
689 setenv("HAPROXY_MWORKER_WAIT_ONLY", "1", 1);
690
691 Warning("Reexecuting Master process in waitpid mode\n");
692 mworker_reload();
693
694 Warning("Failed to reexecute the master processs\n");
695}
William Lallemand73b85e72017-06-01 17:38:51 +0200696
697
698/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200699 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
700 * a signal zero to all subscribers. This means that it's as easy as
701 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200702 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100703static void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200704{
705 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200706 signal_unregister_handler(sh);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200707 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708}
709
710/*
711 * upon SIGTTOU, we pause everything
712 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100713static void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714{
715 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200716 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717}
718
719/*
720 * upon SIGTTIN, let's have a soft stop.
721 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100722static void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723{
Willy Tarreaube58c382011-07-24 18:28:10 +0200724 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725}
726
727/*
728 * this function dumps every server's state when the process receives SIGHUP.
729 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100730static void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731{
732 struct proxy *p = proxy;
733
734 Warning("SIGHUP received, dumping servers states.\n");
735 while (p) {
736 struct server *s = p->srv;
737
738 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
739 while (s) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100740 chunk_printf(&trash,
741 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
742 p->id, s->id,
Willy Tarreau892337c2014-05-13 23:41:20 +0200743 (s->state != SRV_ST_STOPPED) ? "UP" : "DOWN",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100744 s->cur_sess, s->nbpend, s->counters.cum_sess);
745 Warning("%s\n", trash.str);
746 send_log(p, LOG_NOTICE, "%s\n", trash.str);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747 s = s->next;
748 }
749
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200750 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
751 if (!p->srv) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100752 chunk_printf(&trash,
753 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
754 p->id,
755 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 +0200756 } else if (p->srv_act == 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100757 chunk_printf(&trash,
758 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
759 p->id,
760 (p->srv_bck) ? "is running on backup servers" : "has no server available",
761 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 +0200762 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100763 chunk_printf(&trash,
764 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
765 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
766 p->id, p->srv_act, p->srv_bck,
767 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 +0200768 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100769 Warning("%s\n", trash.str);
770 send_log(p, LOG_NOTICE, "%s\n", trash.str);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200771
772 p = p->next;
773 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774}
775
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100776static void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200778 /* dump memory usage then free everything possible */
779 dump_pools();
780 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200781}
782
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200783/* This function check if cfg_cfgfiles containes directories.
784 * If it find one, it add all the files (and only files) it containes
785 * in cfg_cfgfiles in place of the directory (and remove the directory).
786 * It add the files in lexical order.
787 * It add only files with .cfg extension.
788 * It doesn't add files with name starting with '.'
789 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100790static void cfgfiles_expand_directories(void)
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200791{
792 struct wordlist *wl, *wlb;
793 char *err = NULL;
794
795 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
796 struct stat file_stat;
797 struct dirent **dir_entries = NULL;
798 int dir_entries_nb;
799 int dir_entries_it;
800
801 if (stat(wl->s, &file_stat)) {
802 Alert("Cannot open configuration file/directory %s : %s\n",
803 wl->s,
804 strerror(errno));
805 exit(1);
806 }
807
808 if (!S_ISDIR(file_stat.st_mode))
809 continue;
810
811 /* from this point wl->s is a directory */
812
813 dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort);
814 if (dir_entries_nb < 0) {
815 Alert("Cannot open configuration directory %s : %s\n",
816 wl->s,
817 strerror(errno));
818 exit(1);
819 }
820
821 /* for each element in the directory wl->s */
822 for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) {
823 struct dirent *dir_entry = dir_entries[dir_entries_it];
824 char *filename = NULL;
825 char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg");
826
827 /* don't add filename that begin with .
828 * only add filename with .cfg extention
829 */
830 if (dir_entry->d_name[0] == '.' ||
831 !(d_name_cfgext && d_name_cfgext[4] == '\0'))
832 goto next_dir_entry;
833
834 if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) {
835 Alert("Cannot load configuration files %s : out of memory.\n",
836 filename);
837 exit(1);
838 }
839
840 if (stat(filename, &file_stat)) {
841 Alert("Cannot open configuration file %s : %s\n",
842 wl->s,
843 strerror(errno));
844 exit(1);
845 }
846
847 /* don't add anything else than regular file in cfg_cfgfiles
848 * this way we avoid loops
849 */
850 if (!S_ISREG(file_stat.st_mode))
851 goto next_dir_entry;
852
853 if (!list_append_word(&wl->list, filename, &err)) {
854 Alert("Cannot load configuration files %s : %s\n",
855 filename,
856 err);
857 exit(1);
858 }
859
860next_dir_entry:
861 free(filename);
862 free(dir_entry);
863 }
864
865 free(dir_entries);
866
867 /* remove the current directory (wl) from cfg_cfgfiles */
868 free(wl->s);
869 LIST_DEL(&wl->list);
870 free(wl);
871 }
872
873 free(err);
874}
875
Olivier Houchardf73629d2017-04-05 22:33:04 +0200876static int get_old_sockets(const char *unixsocket)
877{
878 char *cmsgbuf = NULL, *tmpbuf = NULL;
879 int *tmpfd = NULL;
880 struct sockaddr_un addr;
881 struct cmsghdr *cmsg;
882 struct msghdr msghdr;
883 struct iovec iov;
884 struct xfer_sock_list *xfer_sock = NULL;
Olivier Houchard54740872017-04-06 14:45:14 +0200885 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf73629d2017-04-05 22:33:04 +0200886 int sock = -1;
887 int ret = -1;
888 int ret2 = -1;
889 int fd_nb;
890 int got_fd = 0;
891 int i = 0;
892 size_t maxoff = 0, curoff = 0;
893
894 memset(&msghdr, 0, sizeof(msghdr));
895 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
896 if (!cmsgbuf) {
897 Warning("Failed to allocate memory to send sockets\n");
898 goto out;
899 }
900 sock = socket(PF_UNIX, SOCK_STREAM, 0);
901 if (sock < 0) {
902 Warning("Failed to connect to the old process socket '%s'\n",
903 unixsocket);
904 goto out;
905 }
906 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path));
907 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
908 addr.sun_family = PF_UNIX;
909 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
910 if (ret < 0) {
911 Warning("Failed to connect to the old process socket '%s'\n",
912 unixsocket);
913 goto out;
914 }
Olivier Houchard54740872017-04-06 14:45:14 +0200915 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf73629d2017-04-05 22:33:04 +0200916 iov.iov_base = &fd_nb;
917 iov.iov_len = sizeof(fd_nb);
918 msghdr.msg_iov = &iov;
919 msghdr.msg_iovlen = 1;
920 send(sock, "_getsocks\n", strlen("_getsocks\n"), 0);
921 /* First, get the number of file descriptors to be received */
922 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
923 Warning("Failed to get the number of sockets to be transferred !\n");
924 goto out;
925 }
926 if (fd_nb == 0) {
927 ret = 0;
928 goto out;
929 }
930 tmpbuf = malloc(fd_nb * (1 + NAME_MAX + 1 + IFNAMSIZ + sizeof(int)));
931 if (tmpbuf == NULL) {
932 Warning("Failed to allocate memory while receiving sockets\n");
933 goto out;
934 }
935 tmpfd = malloc(fd_nb * sizeof(int));
936 if (tmpfd == NULL) {
937 Warning("Failed to allocate memory while receiving sockets\n");
938 goto out;
939 }
940 msghdr.msg_control = cmsgbuf;
941 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
942 iov.iov_len = MAX_SEND_FD * (1 + NAME_MAX + 1 + IFNAMSIZ + sizeof(int));
943 do {
944 int ret3;
945
946 iov.iov_base = tmpbuf + curoff;
947 ret = recvmsg(sock, &msghdr, 0);
948 if (ret == -1 && errno == EINTR)
949 continue;
950 if (ret <= 0)
951 break;
952 /* Send an ack to let the sender know we got the sockets
953 * and it can send some more
954 */
955 do {
956 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
957 } while (ret3 == -1 && errno == EINTR);
958 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
959 cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
960 if (cmsg->cmsg_level == SOL_SOCKET &&
961 cmsg->cmsg_type == SCM_RIGHTS) {
962 size_t totlen = cmsg->cmsg_len -
963 CMSG_LEN(0);
964 if (totlen / sizeof(int) + got_fd > fd_nb) {
965 Warning("Got to many sockets !\n");
966 goto out;
967 }
968 /*
969 * Be paranoid and use memcpy() to avoid any
970 * potential alignement issue.
971 */
972 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
973 got_fd += totlen / sizeof(int);
974 }
975 }
976 curoff += ret;
977 } while (got_fd < fd_nb);
978
979 if (got_fd != fd_nb) {
980 Warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
981 fd_nb, got_fd);
982 goto out;
983 }
984 maxoff = curoff;
985 curoff = 0;
986 for (i = 0; i < got_fd; i++) {
987 int fd = tmpfd[i];
988 socklen_t socklen;
989 int len;
990
991 xfer_sock = calloc(1, sizeof(*xfer_sock));
992 if (!xfer_sock) {
993 Warning("Failed to allocate memory in get_old_sockets() !\n");
994 break;
995 }
996 xfer_sock->fd = -1;
997
998 socklen = sizeof(xfer_sock->addr);
999 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
1000 Warning("Failed to get socket address\n");
1001 free(xfer_sock);
Olivier Houchardbe7b1ce2017-07-17 17:25:33 +02001002 xfer_sock = NULL;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001003 continue;
1004 }
1005 if (curoff >= maxoff) {
1006 Warning("Inconsistency while transferring sockets\n");
1007 goto out;
1008 }
1009 len = tmpbuf[curoff++];
1010 if (len > 0) {
1011 /* We have a namespace */
1012 if (curoff + len > maxoff) {
1013 Warning("Inconsistency while transferring sockets\n");
1014 goto out;
1015 }
1016 xfer_sock->namespace = malloc(len + 1);
1017 if (!xfer_sock->namespace) {
1018 Warning("Failed to allocate memory while transferring sockets\n");
1019 goto out;
1020 }
1021 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
1022 xfer_sock->namespace[len] = 0;
1023 curoff += len;
1024 }
1025 if (curoff >= maxoff) {
1026 Warning("Inconsistency while transferring sockets\n");
1027 goto out;
1028 }
1029 len = tmpbuf[curoff++];
1030 if (len > 0) {
1031 /* We have an interface */
1032 if (curoff + len > maxoff) {
1033 Warning("Inconsistency while transferring sockets\n");
1034 goto out;
1035 }
1036 xfer_sock->iface = malloc(len + 1);
1037 if (!xfer_sock->iface) {
1038 Warning("Failed to allocate memory while transferring sockets\n");
1039 goto out;
1040 }
1041 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
1042 xfer_sock->namespace[len] = 0;
1043 curoff += len;
1044 }
1045 if (curoff + sizeof(int) > maxoff) {
1046 Warning("Inconsistency while transferring sockets\n");
1047 goto out;
1048 }
1049 memcpy(&xfer_sock->options, &tmpbuf[curoff],
1050 sizeof(xfer_sock->options));
1051 curoff += sizeof(xfer_sock->options);
1052
1053 xfer_sock->fd = fd;
1054 if (xfer_sock_list)
1055 xfer_sock_list->prev = xfer_sock;
1056 xfer_sock->next = xfer_sock_list;
1057 xfer_sock->prev = NULL;
1058 xfer_sock_list = xfer_sock;
1059 xfer_sock = NULL;
1060 }
1061
1062 ret2 = 0;
1063out:
1064 /* If we failed midway make sure to close the remaining
1065 * file descriptors
1066 */
1067 if (tmpfd != NULL && i < got_fd) {
1068 for (; i < got_fd; i++) {
1069 close(tmpfd[i]);
1070 }
1071 }
1072 free(tmpbuf);
1073 free(tmpfd);
1074 free(cmsgbuf);
1075 if (sock != -1)
1076 close(sock);
1077 if (xfer_sock) {
1078 free(xfer_sock->namespace);
1079 free(xfer_sock->iface);
1080 if (xfer_sock->fd != -1)
1081 close(xfer_sock->fd);
1082 free(xfer_sock);
1083 }
1084 return (ret2);
1085}
1086
Willy Tarreaubaaee002006-06-26 02:48:02 +02001087/*
William Lallemand73b85e72017-06-01 17:38:51 +02001088 * copy and cleanup the current argv
1089 * Remove the -sf /-st parameters
1090 * Return an allocated copy of argv
1091 */
1092
1093static char **copy_argv(int argc, char **argv)
1094{
1095 char **newargv;
William Lallemand2bf6d622017-06-20 11:20:23 +02001096 int i = 0, j = 0;
William Lallemand73b85e72017-06-01 17:38:51 +02001097
1098 newargv = calloc(argc + 2, sizeof(char *));
1099 if (newargv == NULL) {
1100 Warning("Cannot allocate memory\n");
1101 return NULL;
1102 }
1103
William Lallemand2bf6d622017-06-20 11:20:23 +02001104 while (i < argc) {
1105 /* -sf or -st or -x */
1106 if ((argv[i][1] == 's' && (argv[i][2] == 'f' || argv[i][2] == 't')) || argv[i][1] == 'x' ) {
1107 /* list of pids to finish ('f') or terminate ('t') or unix socket (-x) */
William Lallemand73b85e72017-06-01 17:38:51 +02001108 i++;
1109 while (i < argc && argv[i][0] != '-') {
1110 i++;
1111 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001112 continue;
William Lallemand73b85e72017-06-01 17:38:51 +02001113 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001114
1115 newargv[j++] = argv[i++];
William Lallemand73b85e72017-06-01 17:38:51 +02001116 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001117
William Lallemand73b85e72017-06-01 17:38:51 +02001118 return newargv;
1119}
1120
1121/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001122 * This function initializes all the necessary variables. It only returns
1123 * if everything is OK. If something fails, it exits.
1124 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001125static void init(int argc, char **argv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001126{
Willy Tarreaubaaee002006-06-26 02:48:02 +02001127 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001128 char *tmp;
1129 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +02001130 int err_code = 0;
Maxime de Roucy0f503922016-05-13 23:52:55 +02001131 char *err_msg = NULL;
Willy Tarreau477ecd82010-01-03 21:12:30 +01001132 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +00001133 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +02001134 char *change_dir = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001135 struct proxy *px;
Willy Tarreaue6945732016-12-21 19:57:00 +01001136 struct post_check_fct *pcf;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001137
William Lallemand73b85e72017-06-01 17:38:51 +02001138 next_argv = copy_argv(argc, argv);
1139
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001140 chunk_init(&trash, malloc(global.tune.bufsize), global.tune.bufsize);
Willy Tarreau2819e992013-12-13 14:41:10 +01001141 alloc_trash_buffers(global.tune.bufsize);
David du Colombier7af46052012-05-16 14:16:48 +02001142
Emeric Brun2b920a12010-09-23 18:30:22 +02001143 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
1144 * the string in case of truncation, and at least FreeBSD appears not to do
1145 * it.
1146 */
1147 memset(hostname, 0, sizeof(hostname));
1148 gethostname(hostname, sizeof(hostname) - 1);
1149 memset(localpeer, 0, sizeof(localpeer));
1150 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
1151
Willy Tarreaubaaee002006-06-26 02:48:02 +02001152 /*
1153 * Initialize the previously static variables.
1154 */
1155
Willy Tarreau3eba98a2009-01-25 13:56:13 +01001156 totalconn = actconn = maxfd = listeners = stopping = 0;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001157 killed = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001158
1159
1160#ifdef HAPROXY_MEMMAX
Willy Tarreau70060452015-12-14 12:46:07 +01001161 global.rlimit_memmax_all = HAPROXY_MEMMAX;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001162#endif
1163
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001164 tzset();
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001165 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001166 start_date = now;
1167
Willy Tarreau84310e22014-02-14 11:59:04 +01001168 srandom(now_ms - getpid());
1169
Dragan Dosen835b9212016-02-12 13:23:03 +01001170 init_log();
Willy Tarreau29857942009-05-10 09:01:21 +02001171 signal_init();
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001172 if (init_acl() != 0)
1173 exit(1);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001174 init_task();
Willy Tarreau87b09662015-04-03 00:22:06 +02001175 init_stream();
Willy Tarreaub1ec8c42015-04-03 13:53:24 +02001176 init_session();
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001177 init_connection();
Willy Tarreau8280d642009-09-23 23:37:52 +02001178 /* warning, we init buffers later */
Willy Tarreaue4d7e552007-05-13 20:19:55 +02001179 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +01001180 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001181
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001182 /* Initialise lua. */
1183 hlua_init();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001184
Christopher Fauletff2613e2016-11-09 11:36:17 +01001185 /* Initialize process vars */
1186 vars_init(&global.vars, SCOPE_PROC);
1187
Willy Tarreau43b78992009-01-25 15:42:27 +01001188 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001189#if defined(ENABLE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001190 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001191#endif
1192#if defined(ENABLE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001193 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001194#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001195#if defined(ENABLE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +01001196 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001197#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001198#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001199 global.tune.options |= GTUNE_USE_SPLICE;
1200#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001201#if defined(USE_GETADDRINFO)
1202 global.tune.options |= GTUNE_USE_GAI;
1203#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001204#if defined(SO_REUSEPORT)
1205 global.tune.options |= GTUNE_USE_REUSEPORT;
1206#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001207
1208 pid = getpid();
1209 progname = *argv;
1210 while ((tmp = strchr(progname, '/')) != NULL)
1211 progname = tmp + 1;
1212
Kevinm48936af2010-12-22 16:08:21 +00001213 /* the process name is used for the logs only */
Dragan Dosen43885c72015-10-01 13:18:13 +02001214 chunk_initstr(&global.log_tag, strdup(progname));
Kevinm48936af2010-12-22 16:08:21 +00001215
Willy Tarreaubaaee002006-06-26 02:48:02 +02001216 argc--; argv++;
1217 while (argc > 0) {
1218 char *flag;
1219
1220 if (**argv == '-') {
1221 flag = *argv+1;
1222
1223 /* 1 arg */
1224 if (*flag == 'v') {
1225 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +01001226 if (flag[1] == 'v') /* -vv */
1227 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001228 exit(0);
1229 }
1230#if defined(ENABLE_EPOLL)
1231 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +01001232 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233#endif
1234#if defined(ENABLE_POLL)
1235 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +01001236 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001237#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +02001238#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001239 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +01001240 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001241#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001242#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001243 else if (*flag == 'd' && flag[1] == 'S')
1244 global.tune.options &= ~GTUNE_USE_SPLICE;
1245#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001246#if defined(USE_GETADDRINFO)
1247 else if (*flag == 'd' && flag[1] == 'G')
1248 global.tune.options &= ~GTUNE_USE_GAI;
1249#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001250#if defined(SO_REUSEPORT)
1251 else if (*flag == 'd' && flag[1] == 'R')
1252 global.tune.options &= ~GTUNE_USE_REUSEPORT;
1253#endif
Emeric Brun850efd52014-01-29 12:24:34 +01001254 else if (*flag == 'd' && flag[1] == 'V')
1255 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001256 else if (*flag == 'V')
1257 arg_mode |= MODE_VERBOSE;
1258 else if (*flag == 'd' && flag[1] == 'b')
1259 arg_mode |= MODE_FOREGROUND;
Willy Tarreau6e064432012-05-08 15:40:42 +02001260 else if (*flag == 'd' && flag[1] == 'M')
1261 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreau3eed10e2016-11-07 21:03:16 +01001262 else if (*flag == 'd' && flag[1] == 'r')
1263 global.tune.options |= GTUNE_RESOLVE_DONTFAIL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001264 else if (*flag == 'd')
1265 arg_mode |= MODE_DEBUG;
1266 else if (*flag == 'c')
1267 arg_mode |= MODE_CHECK;
William Lallemand095ba4c2017-06-01 17:38:50 +02001268 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +02001269 arg_mode |= MODE_DAEMON;
William Lallemand095ba4c2017-06-01 17:38:50 +02001270 else if (*flag == 'W')
1271 arg_mode |= MODE_MWORKER;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001272 else if (*flag == 'q')
1273 arg_mode |= MODE_QUIET;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001274 else if (*flag == 'x') {
William Lallemand45eff442017-06-19 15:57:55 +02001275 if (argc <= 1 || argv[1][0] == '-') {
1276 Alert("Unix socket path expected with the -x flag\n\n");
1277 usage(progname);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001278 }
William Lallemand4fc09692017-06-19 16:37:19 +02001279 if (old_unixsocket)
1280 Warning("-x option already set, overwriting the value\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001281 old_unixsocket = argv[1];
William Lallemand4fc09692017-06-19 16:37:19 +02001282
Olivier Houchardf73629d2017-04-05 22:33:04 +02001283 argv++;
1284 argc--;
1285 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001286 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1287 /* list of pids to finish ('f') or terminate ('t') */
1288
1289 if (flag[1] == 'f')
1290 oldpids_sig = SIGUSR1; /* finish then exit */
1291 else
1292 oldpids_sig = SIGTERM; /* terminate immediately */
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001293 while (argc > 1 && argv[1][0] != '-') {
1294 oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int));
1295 if (!oldpids) {
1296 Alert("Cannot allocate old pid : out of memory.\n");
1297 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001298 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001299 argc--; argv++;
1300 oldpids[nb_oldpids] = atol(*argv);
1301 if (oldpids[nb_oldpids] <= 0)
1302 usage(progname);
1303 nb_oldpids++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001304 }
1305 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001306 else if (flag[0] == '-' && flag[1] == 0) { /* "--" */
1307 /* now that's a cfgfile list */
1308 argv++; argc--;
1309 while (argc > 0) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02001310 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
1311 Alert("Cannot load configuration file/directory %s : %s\n",
1312 *argv,
1313 err_msg);
Willy Tarreaua088d312015-10-08 11:58:48 +02001314 exit(1);
1315 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001316 argv++; argc--;
1317 }
1318 break;
1319 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001320 else { /* >=2 args */
1321 argv++; argc--;
1322 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001323 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001324
1325 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +02001326 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001327 case 'n' : cfg_maxconn = atol(*argv); break;
Willy Tarreau70060452015-12-14 12:46:07 +01001328 case 'm' : global.rlimit_memmax_all = atol(*argv); break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001329 case 'N' : cfg_maxpconn = atol(*argv); break;
Emeric Brun2b920a12010-09-23 18:30:22 +02001330 case 'L' : strncpy(localpeer, *argv, sizeof(localpeer) - 1); break;
Willy Tarreau5d01a632009-06-22 16:02:30 +02001331 case 'f' :
Maxime de Roucy0f503922016-05-13 23:52:55 +02001332 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
1333 Alert("Cannot load configuration file/directory %s : %s\n",
1334 *argv,
1335 err_msg);
Willy Tarreau5d01a632009-06-22 16:02:30 +02001336 exit(1);
1337 }
Willy Tarreau5d01a632009-06-22 16:02:30 +02001338 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001339 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001340 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001341 }
1342 }
1343 }
1344 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001345 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001346 argv++; argc--;
1347 }
1348
1349 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
William Lallemand095ba4c2017-06-01 17:38:50 +02001350 (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
Willy Tarreaubaaee002006-06-26 02:48:02 +02001351 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
1352
William Lallemandcb11fd22017-06-01 17:38:52 +02001353 /* Master workers wait mode */
1354 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_WAIT_ONLY") != NULL)) {
1355
1356 unsetenv("HAPROXY_MWORKER_WAIT_ONLY");
1357 mworker_wait();
1358 }
1359
1360 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {
1361 atexit_flag = 1;
1362 atexit(reexec_on_failure);
1363 }
1364
Willy Tarreau576132e2011-09-10 19:26:56 +02001365 if (change_dir && chdir(change_dir) < 0) {
1366 Alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
1367 exit(1);
1368 }
1369
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001370 /* handle cfgfiles that are actualy directories */
1371 cfgfiles_expand_directories();
1372
1373 if (LIST_ISEMPTY(&cfg_cfgfiles))
1374 usage(progname);
1375
Willy Tarreaubaaee002006-06-26 02:48:02 +02001376 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001377
1378 init_default_instance();
1379
Willy Tarreau477ecd82010-01-03 21:12:30 +01001380 list_for_each_entry(wl, &cfg_cfgfiles, list) {
Willy Tarreauc4382422009-12-06 13:10:44 +01001381 int ret;
1382
Willy Tarreau477ecd82010-01-03 21:12:30 +01001383 ret = readcfgfile(wl->s);
Willy Tarreauc4382422009-12-06 13:10:44 +01001384 if (ret == -1) {
1385 Alert("Could not open configuration file %s : %s\n",
Willy Tarreau477ecd82010-01-03 21:12:30 +01001386 wl->s, strerror(errno));
Willy Tarreauc4382422009-12-06 13:10:44 +01001387 exit(1);
1388 }
Willy Tarreau25a67fa2009-12-15 21:46:25 +01001389 if (ret & (ERR_ABORT|ERR_FATAL))
Willy Tarreau477ecd82010-01-03 21:12:30 +01001390 Alert("Error(s) found in configuration file : %s\n", wl->s);
Willy Tarreau25a67fa2009-12-15 21:46:25 +01001391 err_code |= ret;
Willy Tarreau058e9072009-07-20 09:30:05 +02001392 if (err_code & ERR_ABORT)
Willy Tarreau5d01a632009-06-22 16:02:30 +02001393 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001394 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001395
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02001396 /* do not try to resolve arguments nor to spot inconsistencies when
1397 * the configuration contains fatal errors caused by files not found
1398 * or failed memory allocations.
1399 */
1400 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1401 Alert("Fatal errors found in configuration.\n");
1402 exit(1);
1403 }
1404
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001405 pattern_finalize_config();
1406
Willy Tarreaubb925012009-07-23 13:36:36 +02001407 err_code |= check_config_validity();
1408 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1409 Alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001410 exit(1);
1411 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001412
Willy Tarreau70060452015-12-14 12:46:07 +01001413 /* recompute the amount of per-process memory depending on nbproc and
1414 * the shared SSL cache size (allowed to exist in all processes).
1415 */
1416 if (global.rlimit_memmax_all) {
1417#if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE)
1418 int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL;
1419
1420 global.rlimit_memmax =
1421 ((((int64_t)global.rlimit_memmax_all * 1048576LL) -
1422 ssl_cache_bytes) / global.nbproc +
1423 ssl_cache_bytes + 1048575LL) / 1048576LL;
1424#else
1425 global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc;
1426#endif
1427 }
1428
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001429#ifdef CONFIG_HAP_NS
1430 err_code |= netns_init();
1431 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1432 Alert("Failed to initialize namespace support.\n");
1433 exit(1);
1434 }
1435#endif
1436
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01001437 /* Apply server states */
1438 apply_server_state();
1439
1440 for (px = proxy; px; px = px->next)
1441 srv_compute_all_admin_states(px);
1442
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001443 /* Apply servers' configured address */
1444 err_code |= srv_init_addr();
1445 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1446 Alert("Failed to initialize server(s) addr.\n");
1447 exit(1);
1448 }
1449
Willy Tarreaubaaee002006-06-26 02:48:02 +02001450 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001451 struct peers *pr;
1452 struct proxy *px;
1453
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001454 for (pr = cfg_peers; pr; pr = pr->next)
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001455 if (pr->peers_fe)
1456 break;
1457
1458 for (px = proxy; px; px = px->next)
Willy Tarreau4348fad2012-09-20 16:48:07 +02001459 if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners))
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001460 break;
1461
1462 if (pr || px) {
1463 /* At least one peer or one listener has been found */
1464 qfprintf(stdout, "Configuration file is valid\n");
1465 exit(0);
1466 }
1467 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
1468 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001469 }
1470
Willy Tarreaue9b26022011-08-01 20:57:55 +02001471 global_listener_queue_task = task_new();
1472 if (!global_listener_queue_task) {
1473 Alert("Out of memory when initializing global task\n");
1474 exit(1);
1475 }
1476 /* very simple initialization, users will queue the task if needed */
1477 global_listener_queue_task->context = NULL; /* not even a context! */
1478 global_listener_queue_task->process = manage_global_listener_queue;
Willy Tarreaue9b26022011-08-01 20:57:55 +02001479
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001480 /* now we know the buffer size, we can initialize the channels and buffers */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001481 init_buffer();
Willy Tarreau8280d642009-09-23 23:37:52 +02001482
Willy Tarreaue6945732016-12-21 19:57:00 +01001483 list_for_each_entry(pcf, &post_check_list, list) {
1484 err_code |= pcf->fct();
1485 if (err_code & (ERR_ABORT|ERR_FATAL))
1486 exit(1);
1487 }
1488
Willy Tarreaubaaee002006-06-26 02:48:02 +02001489 if (cfg_maxconn > 0)
1490 global.maxconn = cfg_maxconn;
1491
1492 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +02001493 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001494 global.pidfile = strdup(cfg_pidfile);
1495 }
1496
Willy Tarreaud0256482015-01-15 21:45:22 +01001497 /* Now we want to compute the maxconn and possibly maxsslconn values.
1498 * It's a bit tricky. If memmax is not set, maxconn defaults to
1499 * DEFAULT_MAXCONN and maxsslconn defaults to DEFAULT_MAXSSLCONN.
1500 *
1501 * If memmax is set, then it depends on which values are set. If
1502 * maxsslconn is set, we use memmax to determine how many cleartext
1503 * connections may be added, and set maxconn to the sum of the two.
1504 * If maxconn is set and not maxsslconn, maxsslconn is computed from
1505 * the remaining amount of memory between memmax and the cleartext
1506 * connections. If neither are set, then it is considered that all
1507 * connections are SSL-capable, and maxconn is computed based on this,
1508 * then maxsslconn accordingly. We need to know if SSL is used on the
1509 * frontends, backends, or both, because when it's used on both sides,
1510 * we need twice the value for maxsslconn, but we only count the
1511 * handshake once since it is not performed on the two sides at the
1512 * same time (frontend-side is terminated before backend-side begins).
1513 * The SSL stack is supposed to have filled ssl_session_cost and
Willy Tarreau474b96a2015-01-28 19:03:21 +01001514 * ssl_handshake_cost during its initialization. In any case, if
1515 * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for
1516 * maxconn in order to protect the system.
Willy Tarreaud0256482015-01-15 21:45:22 +01001517 */
1518 if (!global.rlimit_memmax) {
1519 if (global.maxconn == 0) {
1520 global.maxconn = DEFAULT_MAXCONN;
1521 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1522 fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn);
1523 }
1524 }
1525#ifdef USE_OPENSSL
1526 else if (!global.maxconn && !global.maxsslconn &&
1527 (global.ssl_used_frontend || global.ssl_used_backend)) {
1528 /* memmax is set, compute everything automatically. Here we want
1529 * to ensure that all SSL connections will be served. We take
1530 * care of the number of sides where SSL is used, and consider
1531 * the worst case : SSL used on both sides and doing a handshake
1532 * simultaneously. Note that we can't have more than maxconn
1533 * handshakes at a time by definition, so for the worst case of
1534 * two SSL conns per connection, we count a single handshake.
1535 */
1536 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1537 int64_t mem = global.rlimit_memmax * 1048576ULL;
1538
1539 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1540 mem -= global.maxzlibmem;
1541 mem = mem * MEM_USABLE_RATIO;
1542
1543 global.maxconn = mem /
Willy Tarreau87b09662015-04-03 00:22:06 +02001544 ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream
Willy Tarreaud0256482015-01-15 21:45:22 +01001545 sides * global.ssl_session_max_cost + // SSL buffers, one per side
1546 global.ssl_handshake_max_cost); // 1 handshake per connection max
1547
1548 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001549#ifdef SYSTEM_MAXCONN
1550 if (global.maxconn > DEFAULT_MAXCONN)
1551 global.maxconn = DEFAULT_MAXCONN;
1552#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001553 global.maxsslconn = sides * global.maxconn;
1554 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1555 fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n",
1556 global.maxconn, global.maxsslconn);
1557 }
1558 else if (!global.maxsslconn &&
1559 (global.ssl_used_frontend || global.ssl_used_backend)) {
1560 /* memmax and maxconn are known, compute maxsslconn automatically.
1561 * maxsslconn being forced, we don't know how many of it will be
1562 * on each side if both sides are being used. The worst case is
1563 * when all connections use only one SSL instance because
1564 * handshakes may be on two sides at the same time.
1565 */
1566 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1567 int64_t mem = global.rlimit_memmax * 1048576ULL;
1568 int64_t sslmem;
1569
1570 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1571 mem -= global.maxzlibmem;
1572 mem = mem * MEM_USABLE_RATIO;
1573
Willy Tarreau87b09662015-04-03 00:22:06 +02001574 sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001575 global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost);
1576 global.maxsslconn = round_2dig(global.maxsslconn);
1577
1578 if (sslmem <= 0 || global.maxsslconn < sides) {
1579 Alert("Cannot compute the automatic maxsslconn because global.maxconn is already too "
1580 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1581 "without SSL is %d, but %d was found and SSL is in use.\n",
1582 global.rlimit_memmax,
Willy Tarreau87b09662015-04-03 00:22:06 +02001583 (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)),
Willy Tarreaud0256482015-01-15 21:45:22 +01001584 global.maxconn);
1585 exit(1);
1586 }
1587
1588 if (global.maxsslconn > sides * global.maxconn)
1589 global.maxsslconn = sides * global.maxconn;
1590
1591 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1592 fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn);
1593 }
1594#endif
1595 else if (!global.maxconn) {
1596 /* memmax and maxsslconn are known/unused, compute maxconn automatically */
1597 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1598 int64_t mem = global.rlimit_memmax * 1048576ULL;
1599 int64_t clearmem;
1600
1601 if (global.ssl_used_frontend || global.ssl_used_backend)
1602 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1603
1604 mem -= global.maxzlibmem;
1605 mem = mem * MEM_USABLE_RATIO;
1606
1607 clearmem = mem;
1608 if (sides)
1609 clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn;
1610
Willy Tarreau87b09662015-04-03 00:22:06 +02001611 global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001612 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001613#ifdef SYSTEM_MAXCONN
1614 if (global.maxconn > DEFAULT_MAXCONN)
1615 global.maxconn = DEFAULT_MAXCONN;
1616#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001617
1618 if (clearmem <= 0 || !global.maxconn) {
1619 Alert("Cannot compute the automatic maxconn because global.maxsslconn is already too "
1620 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1621 "is %d, but %d was found.\n",
1622 global.rlimit_memmax,
1623 (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)),
1624 global.maxsslconn);
1625 exit(1);
1626 }
1627
1628 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
1629 if (sides && global.maxsslconn > sides * global.maxconn) {
1630 fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn "
1631 "to be limited to %d. Better reduce global.maxsslconn to get more "
1632 "room for extra connections.\n", global.maxsslconn, global.maxconn);
1633 }
1634 fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn);
1635 }
1636 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001637
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001638 if (!global.maxpipes) {
1639 /* maxpipes not specified. Count how many frontends and backends
1640 * may be using splicing, and bound that to maxconn.
1641 */
1642 struct proxy *cur;
1643 int nbfe = 0, nbbe = 0;
1644
1645 for (cur = proxy; cur; cur = cur->next) {
1646 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
1647 if (cur->cap & PR_CAP_FE)
1648 nbfe += cur->maxconn;
1649 if (cur->cap & PR_CAP_BE)
Willy Tarreauafb48762009-01-25 10:42:05 +01001650 nbbe += cur->fullconn ? cur->fullconn : global.maxconn;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001651 }
1652 }
1653 global.maxpipes = MAX(nbfe, nbbe);
1654 if (global.maxpipes > global.maxconn)
1655 global.maxpipes = global.maxconn;
Willy Tarreau686ac822009-01-25 14:06:58 +01001656 global.maxpipes /= 4;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001657 }
1658
1659
Willy Tarreauabacc2c2011-09-07 14:26:33 +02001660 global.hardmaxconn = global.maxconn; /* keep this max value */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001661 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
Willy Tarreau3ec79b92009-01-18 20:39:42 +01001662 global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001663
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001664 if (global.stats_fe)
1665 global.maxsock += global.stats_fe->maxconn;
1666
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001667 if (cfg_peers) {
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001668 /* peers also need to bypass global maxconn */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001669 struct peers *p = cfg_peers;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001670
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001671 for (p = cfg_peers; p; p = p->next)
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001672 if (p->peers_fe)
1673 global.maxsock += p->peers_fe->maxconn;
1674 }
1675
Willy Tarreau1db37712007-06-03 17:16:49 +02001676 if (global.tune.maxpollevents <= 0)
1677 global.tune.maxpollevents = MAX_POLL_EVENTS;
1678
Willy Tarreau6f4a82c2009-03-21 20:43:57 +01001679 if (global.tune.recv_enough == 0)
1680 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
1681
Willy Tarreau27097842015-09-28 13:53:23 +02001682 if (global.tune.maxrewrite < 0)
1683 global.tune.maxrewrite = MAXREWRITE;
1684
Willy Tarreau27a674e2009-08-17 07:23:33 +02001685 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
1686 global.tune.maxrewrite = global.tune.bufsize / 2;
1687
Willy Tarreaubaaee002006-06-26 02:48:02 +02001688 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
1689 /* command line debug mode inhibits configuration mode */
William Lallemand095ba4c2017-06-01 17:38:50 +02001690 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001691 global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND));
1692 }
1693
William Lallemand095ba4c2017-06-01 17:38:50 +02001694 if (arg_mode & MODE_DAEMON) {
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001695 /* command line daemon mode inhibits foreground and debug modes mode */
1696 global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND);
William Lallemand095ba4c2017-06-01 17:38:50 +02001697 global.mode |= arg_mode & MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001698 }
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001699
1700 global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001701
William Lallemand095ba4c2017-06-01 17:38:50 +02001702 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
1703 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
1704 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001705 }
1706
William Lallemand095ba4c2017-06-01 17:38:50 +02001707 if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001708 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
1709 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
1710 global.nbproc = 1;
1711 }
1712
1713 if (global.nbproc < 1)
1714 global.nbproc = 1;
1715
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001716 swap_buffer = calloc(1, global.tune.bufsize);
1717 get_http_auth_buff = calloc(1, global.tune.bufsize);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02001718 static_table_key = calloc(1, sizeof(*static_table_key));
Willy Tarreau8096de92010-02-26 11:12:27 +01001719
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001720 fdinfo = calloc(1, sizeof(struct fdinfo) * (global.maxsock));
1721 fdtab = calloc(1, sizeof(struct fdtab) * (global.maxsock));
Willy Tarreauef1d1f82007-04-16 00:25:25 +02001722 /*
1723 * Note: we could register external pollers here.
1724 * Built-in pollers have been registered before main().
1725 */
Willy Tarreau4f60f162007-04-08 16:39:58 +02001726
Willy Tarreau43b78992009-01-25 15:42:27 +01001727 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001728 disable_poller("kqueue");
1729
Willy Tarreau43b78992009-01-25 15:42:27 +01001730 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001731 disable_poller("epoll");
1732
Willy Tarreau43b78992009-01-25 15:42:27 +01001733 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001734 disable_poller("poll");
1735
Willy Tarreau43b78992009-01-25 15:42:27 +01001736 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001737 disable_poller("select");
1738
1739 /* Note: we could disable any poller by name here */
1740
Christopher Fauletb3f4e142016-03-07 12:46:38 +01001741 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +02001742 list_pollers(stderr);
Christopher Fauletb3f4e142016-03-07 12:46:38 +01001743 fprintf(stderr, "\n");
1744 list_filters(stderr);
1745 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02001746
Willy Tarreau4f60f162007-04-08 16:39:58 +02001747 if (!init_pollers()) {
Willy Tarreau3fa87b12013-03-31 14:41:15 +02001748 Alert("No polling mechanism available.\n"
1749 " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n"
1750 " is too low on this platform to support maxconn and the number of listeners\n"
1751 " and servers. You should rebuild haproxy specifying your system using TARGET=\n"
1752 " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n"
Prach Pongpanichb837e682013-05-14 20:56:28 +02001753 " global maxconn setting to accommodate the system's limitation. For reference,\n"
Willy Tarreau3fa87b12013-03-31 14:41:15 +02001754 " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n"
1755 " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n"
1756 " check build settings using 'haproxy -vv'.\n\n",
1757 FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001758 exit(1);
1759 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02001760 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
1761 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001762 }
1763
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001764 if (!global.node)
1765 global.node = strdup(hostname);
1766
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01001767 if (!hlua_post_init())
1768 exit(1);
Thomas Holmes6abded42015-05-12 16:23:58 +01001769
Baptiste Assmann325137d2015-04-13 23:40:55 +02001770 /* initialize structures for name resolution */
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001771 if (!dns_init_resolvers(0))
Baptiste Assmann325137d2015-04-13 23:40:55 +02001772 exit(1);
Maxime de Roucy0f503922016-05-13 23:52:55 +02001773
1774 free(err_msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001775}
1776
Simon Horman6fb82592011-07-15 13:14:11 +09001777static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +09001778{
Simon Hormanac821422011-07-15 13:14:09 +09001779 struct acl_term_suite *suite, *suiteb;
1780 struct acl_term *term, *termb;
1781
Simon Horman6fb82592011-07-15 13:14:11 +09001782 if (!cond)
1783 return;
1784
1785 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
1786 list_for_each_entry_safe(term, termb, &suite->terms, list) {
1787 LIST_DEL(&term->list);
1788 free(term);
Simon Hormanac821422011-07-15 13:14:09 +09001789 }
Simon Horman6fb82592011-07-15 13:14:11 +09001790 LIST_DEL(&suite->list);
1791 free(suite);
1792 }
1793
1794 free(cond);
1795}
1796
1797static void deinit_tcp_rules(struct list *rules)
1798{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001799 struct act_rule *trule, *truleb;
Simon Horman6fb82592011-07-15 13:14:11 +09001800
1801 list_for_each_entry_safe(trule, truleb, rules, list) {
Simon Hormanac821422011-07-15 13:14:09 +09001802 LIST_DEL(&trule->list);
Simon Horman6fb82592011-07-15 13:14:11 +09001803 deinit_acl_cond(trule->cond);
Simon Hormanac821422011-07-15 13:14:09 +09001804 free(trule);
1805 }
1806}
1807
Simon Horman6fb82592011-07-15 13:14:11 +09001808static void deinit_stick_rules(struct list *rules)
1809{
1810 struct sticking_rule *rule, *ruleb;
1811
1812 list_for_each_entry_safe(rule, ruleb, rules, list) {
1813 LIST_DEL(&rule->list);
1814 deinit_acl_cond(rule->cond);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001815 release_sample_expr(rule->expr);
Simon Horman6fb82592011-07-15 13:14:11 +09001816 free(rule);
1817 }
1818}
1819
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001820void deinit(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001821{
Willy Tarreau4d2d0982007-05-14 00:39:29 +02001822 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001823 struct cap_hdr *h,*h_next;
1824 struct server *s,*s_next;
1825 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001826 struct acl_cond *cond, *condb;
1827 struct hdr_exp *exp, *expb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001828 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001829 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001830 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001831 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001832 struct wordlist *wl, *wlb;
Willy Tarreauf4f04122010-01-28 18:10:50 +01001833 struct cond_wordlist *cwl, *cwlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001834 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +02001835 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +01001836 struct logformat_node *lf, *lfb;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001837 struct bind_conf *bind_conf, *bind_back;
Willy Tarreaucdb737e2016-12-21 18:43:10 +01001838 struct build_opts_str *bol, *bolb;
Willy Tarreau05554e62016-12-21 20:46:26 +01001839 struct post_deinit_fct *pdf;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001840 int i;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001841
Willy Tarreau24f4efa2010-08-27 17:56:48 +02001842 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001843 while (p) {
Willy Tarreau8113a5d2012-10-04 08:01:43 +02001844 free(p->conf.file);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001845 free(p->id);
1846 free(p->check_req);
1847 free(p->cookie_name);
1848 free(p->cookie_domain);
1849 free(p->url_param_name);
1850 free(p->capture_name);
1851 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +09001852 free(p->rdp_cookie_name);
Willy Tarreau62a61232013-04-12 18:13:46 +02001853 if (p->conf.logformat_string != default_http_log_format &&
1854 p->conf.logformat_string != default_tcp_log_format &&
1855 p->conf.logformat_string != clf_http_log_format)
1856 free(p->conf.logformat_string);
Willy Tarreau196729e2012-05-31 19:30:26 +02001857
Willy Tarreau62a61232013-04-12 18:13:46 +02001858 free(p->conf.lfs_file);
1859 free(p->conf.uniqueid_format_string);
1860 free(p->conf.uif_file);
Godbachaf1a75d2013-10-02 17:10:11 +08001861 free(p->lbprm.map.srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001862
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001863 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
1864 free(p->conf.logformat_sd_string);
1865 free(p->conf.lfsd_file);
1866
Willy Tarreaua534fea2008-08-03 12:19:50 +02001867 for (i = 0; i < HTTP_ERR_SIZE; i++)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001868 chunk_destroy(&p->errmsg[i]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001869
Willy Tarreauf4f04122010-01-28 18:10:50 +01001870 list_for_each_entry_safe(cwl, cwlb, &p->req_add, list) {
1871 LIST_DEL(&cwl->list);
1872 free(cwl->s);
1873 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001874 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001875
Willy Tarreauf4f04122010-01-28 18:10:50 +01001876 list_for_each_entry_safe(cwl, cwlb, &p->rsp_add, list) {
1877 LIST_DEL(&cwl->list);
1878 free(cwl->s);
1879 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001880 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001881
Willy Tarreaub80c2302007-11-30 20:51:32 +01001882 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
1883 LIST_DEL(&cond->list);
1884 prune_acl_cond(cond);
1885 free(cond);
1886 }
1887
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001888 for (exp = p->req_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001889 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02001890 regex_free(exp->preg);
1891 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001892 }
1893
Willy Tarreau98d04852015-05-26 12:18:29 +02001894 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001895 expb = exp;
1896 exp = exp->next;
1897 free(expb);
1898 }
1899
1900 for (exp = p->rsp_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001901 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02001902 regex_free(exp->preg);
1903 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001904 }
1905
Willy Tarreau98d04852015-05-26 12:18:29 +02001906 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001907 expb = exp;
1908 exp = exp->next;
1909 free(expb);
1910 }
1911
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001912 /* build a list of unique uri_auths */
1913 if (!ua)
1914 ua = p->uri_auth;
1915 else {
1916 /* check if p->uri_auth is unique */
1917 for (uap = ua; uap; uap=uap->next)
1918 if (uap == p->uri_auth)
1919 break;
1920
Willy Tarreauaccc4e12008-06-24 11:14:45 +02001921 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001922 /* add it, if it is */
1923 p->uri_auth->next = ua;
1924 ua = p->uri_auth;
1925 }
1926 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001927
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001928 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
1929 LIST_DEL(&acl->list);
1930 prune_acl(acl);
1931 free(acl);
1932 }
1933
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001934 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
1935 LIST_DEL(&srule->list);
1936 prune_acl_cond(srule->cond);
1937 free(srule->cond);
1938 free(srule);
1939 }
1940
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001941 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
1942 LIST_DEL(&rule->list);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001943 if (rule->cond) {
1944 prune_acl_cond(rule->cond);
1945 free(rule->cond);
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +01001946 free(rule->file);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001947 }
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001948 free(rule);
1949 }
1950
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001951 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
1952 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +01001953 if (rdr->cond) {
1954 prune_acl_cond(rdr->cond);
1955 free(rdr->cond);
1956 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001957 free(rdr->rdr_str);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01001958 list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) {
1959 LIST_DEL(&lf->list);
1960 free(lf);
1961 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001962 free(rdr);
1963 }
1964
William Lallemand0f99e342011-10-12 17:50:54 +02001965 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
1966 LIST_DEL(&log->list);
1967 free(log);
1968 }
1969
William Lallemand723b73a2012-02-08 16:37:49 +01001970 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
1971 LIST_DEL(&lf->list);
1972 free(lf);
1973 }
1974
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001975 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
1976 LIST_DEL(&lf->list);
1977 free(lf);
1978 }
1979
Simon Hormanac821422011-07-15 13:14:09 +09001980 deinit_tcp_rules(&p->tcp_req.inspect_rules);
1981 deinit_tcp_rules(&p->tcp_req.l4_rules);
1982
Simon Horman6fb82592011-07-15 13:14:11 +09001983 deinit_stick_rules(&p->storersp_rules);
1984 deinit_stick_rules(&p->sticking_rules);
1985
Willy Tarreaubaaee002006-06-26 02:48:02 +02001986 h = p->req_cap;
1987 while (h) {
1988 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001989 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001990 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001991 free(h);
1992 h = h_next;
1993 }/* end while(h) */
1994
1995 h = p->rsp_cap;
1996 while (h) {
1997 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001998 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001999 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002000 free(h);
2001 h = h_next;
2002 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002003
Willy Tarreaubaaee002006-06-26 02:48:02 +02002004 s = p->srv;
2005 while (s) {
2006 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002007
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002008 if (s->check.task) {
2009 task_delete(s->check.task);
2010 task_free(s->check.task);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002011 }
Simon Hormand60d6912013-11-25 10:46:36 +09002012 if (s->agent.task) {
2013 task_delete(s->agent.task);
2014 task_free(s->agent.task);
2015 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002016
Willy Tarreau2e993902011-10-31 11:53:20 +01002017 if (s->warmup) {
2018 task_delete(s->warmup);
2019 task_free(s->warmup);
2020 }
2021
Willy Tarreaua534fea2008-08-03 12:19:50 +02002022 free(s->id);
2023 free(s->cookie);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002024 free(s->check.bi);
2025 free(s->check.bo);
Simon Hormand60d6912013-11-25 10:46:36 +09002026 free(s->agent.bi);
2027 free(s->agent.bo);
James Brown55f9ff12015-10-21 18:19:05 -07002028 free(s->agent.send_string);
Sárközi, László34c01792014-09-05 10:08:23 +02002029 free((char*)s->conf.file);
Willy Tarreau17d45382016-12-22 21:16:08 +01002030
2031 if (s->use_ssl || s->check.use_ssl) {
2032 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2033 xprt_get(XPRT_SSL)->destroy_srv(s);
2034 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002035 free(s);
2036 s = s_next;
2037 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002038
Willy Tarreau4348fad2012-09-20 16:48:07 +02002039 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002040 /*
2041 * Zombie proxy, the listener just pretend to be up
2042 * because they still hold an opened fd.
2043 * Close it and give the listener its real state.
2044 */
2045 if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
2046 close(l->fd);
2047 l->state = LI_INIT;
2048 }
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02002049 unbind_listener(l);
2050 delete_listener(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002051 LIST_DEL(&l->by_fe);
2052 LIST_DEL(&l->by_bind);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002053 free(l->name);
2054 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002055 free(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002056 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002057
Willy Tarreau4348fad2012-09-20 16:48:07 +02002058 /* Release unused SSL configs. */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002059 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01002060 if (bind_conf->xprt->destroy_bind_conf)
2061 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002062 free(bind_conf->file);
2063 free(bind_conf->arg);
2064 LIST_DEL(&bind_conf->by_fe);
2065 free(bind_conf);
2066 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02002067
Christopher Fauletd7c91962015-04-30 11:48:27 +02002068 flt_deinit(p);
2069
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002070 free(p->desc);
2071 free(p->fwdfor_hdr_name);
2072
Willy Tarreauff011f22011-01-06 17:51:27 +01002073 free_http_req_rules(&p->http_req_rules);
Sasha Pachev218f0642014-06-16 12:05:59 -06002074 free_http_res_rules(&p->http_res_rules);
Willy Tarreau918ff602011-07-25 16:33:49 +02002075 free(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002076
Willy Tarreaucf7f3202007-05-13 22:46:04 +02002077 pool_destroy2(p->req_cap_pool);
2078 pool_destroy2(p->rsp_cap_pool);
Simon Hormanb08584a2011-07-15 13:14:10 +09002079 pool_destroy2(p->table.pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002080
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002081 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002082 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002083 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002084 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02002085
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002086 while (ua) {
2087 uap = ua;
2088 ua = ua->next;
2089
Willy Tarreaua534fea2008-08-03 12:19:50 +02002090 free(uap->uri_prefix);
2091 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002092 free(uap->node);
2093 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002094
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002095 userlist_free(uap->userlist);
Willy Tarreauff011f22011-01-06 17:51:27 +01002096 free_http_req_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002097
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002098 free(uap);
2099 }
2100
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002101 userlist_free(userlist);
2102
David Carlier834cb2e2015-09-25 12:02:25 +01002103 cfg_unregister_sections();
2104
2105 free_trash_buffers();
2106 chunk_destroy(&trash);
2107
Willy Tarreaudd815982007-10-16 12:25:14 +02002108 protocol_unbind_all();
2109
Willy Tarreau05554e62016-12-21 20:46:26 +01002110 list_for_each_entry(pdf, &post_deinit_list, list)
2111 pdf->fct();
2112
Joe Williamsdf5b38f2010-12-29 17:05:48 +01002113 free(global.log_send_hostname); global.log_send_hostname = NULL;
Dragan Dosen43885c72015-10-01 13:18:13 +02002114 chunk_destroy(&global.log_tag);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002115 free(global.chroot); global.chroot = NULL;
2116 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002117 free(global.node); global.node = NULL;
2118 free(global.desc); global.desc = NULL;
Godbach4cc1b0d2013-06-26 16:49:51 +08002119 free(fdinfo); fdinfo = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002120 free(fdtab); fdtab = NULL;
2121 free(oldpids); oldpids = NULL;
David Carlier834cb2e2015-09-25 12:02:25 +01002122 free(static_table_key); static_table_key = NULL;
2123 free(get_http_auth_buff); get_http_auth_buff = NULL;
2124 free(swap_buffer); swap_buffer = NULL;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002125 free(global_listener_queue_task); global_listener_queue_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002126
William Lallemand0f99e342011-10-12 17:50:54 +02002127 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
2128 LIST_DEL(&log->list);
2129 free(log);
2130 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01002131 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02002132 free(wl->s);
Willy Tarreau477ecd82010-01-03 21:12:30 +01002133 LIST_DEL(&wl->list);
2134 free(wl);
2135 }
2136
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002137 list_for_each_entry_safe(bol, bolb, &build_opts_list, list) {
2138 if (bol->must_free)
2139 free((void *)bol->str);
2140 LIST_DEL(&bol->list);
2141 free(bol);
2142 }
2143
Christopher Fauletff2613e2016-11-09 11:36:17 +01002144 vars_prune(&global.vars, NULL, NULL);
2145
Willy Tarreau87b09662015-04-03 00:22:06 +02002146 pool_destroy2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +02002147 pool_destroy2(pool2_session);
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002148 pool_destroy2(pool2_connection);
Willy Tarreaub686afd2017-02-08 11:06:11 +01002149 pool_destroy2(pool2_trash);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002150 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002151 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02002152 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +02002153 pool_destroy2(pool2_capture);
Willy Tarreaue4d7e552007-05-13 20:19:55 +02002154 pool_destroy2(pool2_pendconn);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002155 pool_destroy2(pool2_sig_handlers);
Willy Tarreau34eb6712011-10-24 18:15:04 +02002156 pool_destroy2(pool2_hdr_idx);
Willy Tarreau63986c72015-04-03 22:55:33 +02002157 pool_destroy2(pool2_http_txn);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002158 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002159} /* end deinit() */
2160
Willy Tarreaubaaee002006-06-26 02:48:02 +02002161
Willy Tarreau918ff602011-07-25 16:33:49 +02002162/* Runs the polling loop */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01002163static void run_poll_loop()
Willy Tarreau4f60f162007-04-08 16:39:58 +02002164{
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002165 int next;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002166
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002167 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002168 while (1) {
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002169 /* Process a few tasks */
2170 process_runnable_tasks();
2171
Willy Tarreau29857942009-05-10 09:01:21 +02002172 /* check if we caught some signals and process them */
2173 signal_process_queue();
2174
Willy Tarreau58b458d2008-06-29 22:40:23 +02002175 /* Check if we can expire some tasks */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002176 next = wake_expired_tasks();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002177
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002178 /* stop when there's nothing left to do */
2179 if (jobs == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +02002180 break;
2181
Willy Tarreau10146c92015-04-13 20:44:19 +02002182 /* expire immediately if events are pending */
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002183 if (fd_cache_num || tasks_run_queue || signal_queue_len || applets_active_queue)
Willy Tarreau10146c92015-04-13 20:44:19 +02002184 next = now_ms;
2185
Willy Tarreau58b458d2008-06-29 22:40:23 +02002186 /* The poller will ensure it returns around <next> */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002187 cur_poller.poll(&cur_poller, next);
Willy Tarreau033cd9d2014-01-25 19:24:15 +01002188 fd_process_cached_events();
Willy Tarreau3c595ac2015-04-19 09:59:31 +02002189 applet_run_active();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002190 }
2191}
2192
Willy Tarreaue9b26022011-08-01 20:57:55 +02002193/* This is the global management task for listeners. It enables listeners waiting
2194 * for global resources when there are enough free resource, or at least once in
2195 * a while. It is designed to be called as a task.
2196 */
2197static struct task *manage_global_listener_queue(struct task *t)
2198{
2199 int next = TICK_ETERNITY;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002200 /* queue is empty, nothing to do */
2201 if (LIST_ISEMPTY(&global_listener_queue))
2202 goto out;
2203
2204 /* If there are still too many concurrent connections, let's wait for
2205 * some of them to go away. We don't need to re-arm the timer because
2206 * each of them will scan the queue anyway.
2207 */
2208 if (unlikely(actconn >= global.maxconn))
2209 goto out;
2210
2211 /* We should periodically try to enable listeners waiting for a global
2212 * resource here, because it is possible, though very unlikely, that
2213 * they have been blocked by a temporary lack of global resource such
2214 * as a file descriptor or memory and that the temporary condition has
2215 * disappeared.
2216 */
Willy Tarreauabacc2c2011-09-07 14:26:33 +02002217 dequeue_all_listeners(&global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +02002218
2219 out:
2220 t->expire = next;
2221 task_queue(t);
2222 return t;
2223}
Willy Tarreau4f60f162007-04-08 16:39:58 +02002224
William Lallemande20b6a62017-06-01 17:38:55 +02002225void mworker_pipe_handler(int fd)
2226{
2227 char c;
2228
2229 while (read(fd, &c, 1) == -1) {
2230 if (errno == EINTR)
2231 continue;
2232 if (errno == EAGAIN) {
2233 fd_cant_recv(fd);
2234 return;
2235 }
2236 break;
2237 }
2238
2239 deinit();
2240 exit(EXIT_FAILURE);
2241 return;
2242}
2243
2244void mworker_pipe_register(int pipefd[2])
2245{
2246 close(mworker_pipe[1]); /* close the write end of the master pipe in the children */
2247
2248 fcntl(mworker_pipe[0], F_SETFL, O_NONBLOCK);
2249 fdtab[mworker_pipe[0]].owner = mworker_pipe;
2250 fdtab[mworker_pipe[0]].iocb = mworker_pipe_handler;
2251 fd_insert(mworker_pipe[0]);
2252 fd_want_recv(mworker_pipe[0]);
2253 }
2254
2255
Willy Tarreaubaaee002006-06-26 02:48:02 +02002256int main(int argc, char **argv)
2257{
2258 int err, retry;
2259 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02002260 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02002261 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002262
Emeric Bruncf20bf12010-10-22 16:06:11 +02002263 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002264 signal_register_fct(SIGQUIT, dump, SIGQUIT);
2265 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
2266 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
William Lallemand73b85e72017-06-01 17:38:51 +02002267 signal_register_fct(SIGUSR2, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002268
Willy Tarreaue437c442010-03-17 18:02:46 +01002269 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
2270 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
2271 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002272 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002273 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002274
Willy Tarreaudc23a922011-02-16 11:10:36 +01002275 /* ulimits */
2276 if (!global.rlimit_nofile)
2277 global.rlimit_nofile = global.maxsock;
2278
2279 if (global.rlimit_nofile) {
2280 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
2281 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
Willy Tarreauef635472016-06-21 11:48:18 +02002282 /* try to set it to the max possible at least */
2283 getrlimit(RLIMIT_NOFILE, &limit);
Willy Tarreau164dd0b2016-06-21 11:51:59 +02002284 limit.rlim_cur = limit.rlim_max;
2285 if (setrlimit(RLIMIT_NOFILE, &limit) != -1)
2286 getrlimit(RLIMIT_NOFILE, &limit);
2287
Willy Tarreauef635472016-06-21 11:48:18 +02002288 Warning("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n", argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
2289 global.rlimit_nofile = limit.rlim_cur;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002290 }
2291 }
2292
2293 if (global.rlimit_memmax) {
2294 limit.rlim_cur = limit.rlim_max =
Willy Tarreau70060452015-12-14 12:46:07 +01002295 global.rlimit_memmax * 1048576ULL;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002296#ifdef RLIMIT_AS
2297 if (setrlimit(RLIMIT_AS, &limit) == -1) {
2298 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2299 argv[0], global.rlimit_memmax);
2300 }
2301#else
2302 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
2303 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2304 argv[0], global.rlimit_memmax);
2305 }
2306#endif
2307 }
2308
Olivier Houchardf73629d2017-04-05 22:33:04 +02002309 if (old_unixsocket) {
William Lallemand85b0bd92017-06-01 17:38:53 +02002310 if (strcmp("/dev/null", old_unixsocket) != 0) {
2311 if (get_old_sockets(old_unixsocket) != 0) {
2312 Alert("Failed to get the sockets from the old process!\n");
2313 if (!(global.mode & MODE_MWORKER))
2314 exit(1);
2315 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002316 }
2317 }
William Lallemand85b0bd92017-06-01 17:38:53 +02002318 get_cur_unixsocket();
2319
Willy Tarreaubaaee002006-06-26 02:48:02 +02002320 /* We will loop at most 100 times with 10 ms delay each time.
2321 * That's at most 1 second. We only send a signal to old pids
2322 * if we cannot grab at least one port.
2323 */
2324 retry = MAX_START_RETRIES;
2325 err = ERR_NONE;
2326 while (retry >= 0) {
2327 struct timeval w;
2328 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01002329 /* exit the loop on no error or fatal error */
2330 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002331 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02002332 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002333 break;
2334
2335 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
2336 * listening sockets. So on those platforms, it would be wiser to
2337 * simply send SIGUSR1, which will not be undoable.
2338 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02002339 if (tell_old_pids(SIGTTOU) == 0) {
2340 /* no need to wait if we can't contact old pids */
2341 retry = 0;
2342 continue;
2343 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002344 /* give some time to old processes to stop listening */
2345 w.tv_sec = 0;
2346 w.tv_usec = 10*1000;
2347 select(0, NULL, NULL, NULL, &w);
2348 retry--;
2349 }
2350
2351 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01002352 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02002353 if (retry != MAX_START_RETRIES && nb_oldpids) {
2354 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002355 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02002356 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002357 exit(1);
2358 }
2359
2360 if (listeners == 0) {
Willy Tarreauf2ee0162015-08-09 11:01:51 +02002361 Alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002362 /* Note: we don't have to send anything to the old pids because we
2363 * never stopped them. */
2364 exit(1);
2365 }
2366
Emeric Bruncf20bf12010-10-22 16:06:11 +02002367 err = protocol_bind_all(errmsg, sizeof(errmsg));
2368 if ((err & ~ERR_WARN) != ERR_NONE) {
2369 if ((err & ERR_ALERT) || (err & ERR_WARN))
2370 Alert("[%s.main()] %s.\n", argv[0], errmsg);
2371
Willy Tarreaudd815982007-10-16 12:25:14 +02002372 Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
2373 protocol_unbind_all(); /* cleanup everything we can */
2374 if (nb_oldpids)
2375 tell_old_pids(SIGTTIN);
2376 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002377 } else if (err & ERR_WARN) {
2378 Alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02002379 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002380 /* Ok, all listener should now be bound, close any leftover sockets
2381 * the previous process gave us, we don't need them anymore
2382 */
2383 while (xfer_sock_list != NULL) {
2384 struct xfer_sock_list *tmpxfer = xfer_sock_list->next;
2385 close(xfer_sock_list->fd);
2386 free(xfer_sock_list->iface);
2387 free(xfer_sock_list->namespace);
2388 free(xfer_sock_list);
2389 xfer_sock_list = tmpxfer;
2390 }
Willy Tarreaudd815982007-10-16 12:25:14 +02002391
Willy Tarreaubaaee002006-06-26 02:48:02 +02002392 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002393 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
2394 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002395
Willy Tarreaubaaee002006-06-26 02:48:02 +02002396 /* MODE_QUIET can inhibit alerts and warnings below this line */
2397
2398 global.mode &= ~MODE_STARTING;
2399 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
2400 /* detach from the tty */
2401 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002402 }
2403
2404 /* open log & pid files before the chroot */
William Lallemand095ba4c2017-06-01 17:38:50 +02002405 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002406 unlink(global.pidfile);
2407 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
2408 if (pidfd < 0) {
2409 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
2410 if (nb_oldpids)
2411 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02002412 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002413 exit(1);
2414 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002415 }
2416
Willy Tarreaub38651a2007-03-24 17:24:39 +01002417 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
2418 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002419 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002420 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01002421 exit(1);
2422 }
2423
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002424 /* If the user is not root, we'll still let him try the configuration
2425 * but we inform him that unexpected behaviour may occur.
2426 */
2427 if ((global.last_checks & LSTCHK_NETADM) && getuid())
2428 Warning("[%s.main()] Some options which require full privileges"
2429 " might not work well.\n"
2430 "", argv[0]);
2431
William Lallemand095ba4c2017-06-01 17:38:50 +02002432 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2433
2434 /* chroot if needed */
2435 if (global.chroot != NULL) {
2436 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
2437 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
2438 if (nb_oldpids)
2439 tell_old_pids(SIGTTIN);
2440 protocol_unbind_all();
2441 exit(1);
2442 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002443 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002444 }
2445
Willy Tarreaubaaee002006-06-26 02:48:02 +02002446 if (nb_oldpids)
Willy Tarreaubb545b42010-08-25 12:58:59 +02002447 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002448
William Lallemand8a361b52017-06-20 11:20:33 +02002449 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) {
2450 nb_oldpids = 0;
2451 free(oldpids);
2452 oldpids = NULL;
2453 }
2454
2455
Willy Tarreaubaaee002006-06-26 02:48:02 +02002456 /* Note that any error at this stage will be fatal because we will not
2457 * be able to restart the old pids.
2458 */
2459
William Lallemand095ba4c2017-06-01 17:38:50 +02002460 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2461 /* setgid / setuid */
2462 if (global.gid) {
2463 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
2464 Warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2465 " without 'uid'/'user' is generally useless.\n", argv[0]);
2466
2467 if (setgid(global.gid) == -1) {
2468 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
2469 protocol_unbind_all();
2470 exit(1);
2471 }
2472 }
Michael Schererab012dd2013-01-12 18:35:19 +01002473
William Lallemand095ba4c2017-06-01 17:38:50 +02002474 if (global.uid && setuid(global.uid) == -1) {
2475 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Michael Schererab012dd2013-01-12 18:35:19 +01002476 protocol_unbind_all();
2477 exit(1);
2478 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002479 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002480 /* check ulimits */
2481 limit.rlim_cur = limit.rlim_max = 0;
2482 getrlimit(RLIMIT_NOFILE, &limit);
2483 if (limit.rlim_cur < global.maxsock) {
2484 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 +02002485 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002486 }
2487
William Lallemand095ba4c2017-06-01 17:38:50 +02002488 if (global.mode & (MODE_DAEMON | MODE_MWORKER)) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002489 struct proxy *px;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002490 struct peers *curpeers;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002491 int ret = 0;
2492 int proc;
2493
William Lallemand73b85e72017-06-01 17:38:51 +02002494 children = calloc(global.nbproc, sizeof(int));
William Lallemand095ba4c2017-06-01 17:38:50 +02002495 /*
2496 * if daemon + mworker: must fork here to let a master
2497 * process live in background before forking children
2498 */
William Lallemand73b85e72017-06-01 17:38:51 +02002499
2500 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
2501 && (global.mode & MODE_MWORKER)
2502 && (global.mode & MODE_DAEMON)) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002503 ret = fork();
2504 if (ret < 0) {
2505 Alert("[%s.main()] Cannot fork.\n", argv[0]);
2506 protocol_unbind_all();
2507 exit(1); /* there has been an error */
2508 }
2509 /* parent leave to daemonize */
2510 if (ret > 0)
2511 exit(0);
2512 }
William Lallemande20b6a62017-06-01 17:38:55 +02002513
2514 if (global.mode & MODE_MWORKER) {
2515 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) {
2516 char *msg = NULL;
2517 /* master pipe to ensure the master is still alive */
2518 ret = pipe(mworker_pipe);
2519 if (ret < 0) {
2520 Warning("[%s.main()] Cannot create master pipe.\n", argv[0]);
2521 } else {
2522 memprintf(&msg, "%d", mworker_pipe[0]);
2523 setenv("HAPROXY_MWORKER_PIPE_RD", msg, 1);
2524 memprintf(&msg, "%d", mworker_pipe[1]);
2525 setenv("HAPROXY_MWORKER_PIPE_WR", msg, 1);
2526 free(msg);
2527 }
2528 } else {
2529 mworker_pipe[0] = atol(getenv("HAPROXY_MWORKER_PIPE_RD"));
2530 mworker_pipe[1] = atol(getenv("HAPROXY_MWORKER_PIPE_WR"));
2531 if (mworker_pipe[0] <= 0 || mworker_pipe[1] <= 0) {
2532 Warning("[%s.main()] Cannot get master pipe FDs.\n", argv[0]);
2533 }
2534 }
2535 }
2536
Willy Tarreaubaaee002006-06-26 02:48:02 +02002537 /* the father launches the required number of processes */
2538 for (proc = 0; proc < global.nbproc; proc++) {
2539 ret = fork();
2540 if (ret < 0) {
2541 Alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002542 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +02002543 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002544 }
2545 else if (ret == 0) /* child breaks here */
2546 break;
Marc-Antoine Perennou992709b2013-02-12 10:53:52 +01002547 children[proc] = ret;
Willy Tarreau269ab312012-09-05 08:02:48 +02002548 if (pidfd >= 0) {
2549 char pidstr[100];
2550 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002551 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002552 }
Willy Tarreaudcd47712007-11-04 23:35:08 +01002553 relative_pid++; /* each child will get a different one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002554 }
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002555
2556#ifdef USE_CPU_AFFINITY
2557 if (proc < global.nbproc && /* child */
Willy Tarreaue7597492015-04-20 11:36:57 +02002558 proc < LONGBITS && /* only the first 32/64 processes may be pinned */
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002559 global.cpu_map[proc]) /* only do this if the process has a CPU map */
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002560#ifdef __FreeBSD__
Olivier Houchard97148f62017-08-16 17:29:11 +02002561 {
2562 cpuset_t cpuset;
2563 int i;
2564 unsigned long cpu_map = global.cpu_map[proc];
2565
2566 CPU_ZERO(&cpuset);
2567 while ((i = ffsl(cpu_map)) > 0) {
2568 CPU_SET(i - 1, &cpuset);
2569 cpu_map &= ~(1 << (i - 1));
2570 }
2571 ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset);
2572 }
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002573#else
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002574 sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map[proc]);
2575#endif
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002576#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002577 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02002578 if (pidfd >= 0) {
2579 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
2580 close(pidfd);
2581 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02002582
2583 /* We won't ever use this anymore */
Willy Tarreaud137dd32010-08-25 12:49:05 +02002584 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002585
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002586 if (proc == global.nbproc) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002587 if (global.mode & MODE_MWORKER) {
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002588 protocol_unbind_all();
William Lallemand73b85e72017-06-01 17:38:51 +02002589 mworker_wait();
William Lallemand1499b9b2017-06-07 15:04:47 +02002590 /* should never get there */
2591 exit(EXIT_FAILURE);
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002592 }
William Lallemandcf4e4962017-06-08 19:05:48 +02002593#if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH)
Grant Zhang872f9c22017-01-21 01:10:18 +00002594 ssl_free_dh();
2595#endif
William Lallemand1499b9b2017-06-07 15:04:47 +02002596 exit(0); /* parent must leave */
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002597 }
2598
William Lallemandcb11fd22017-06-01 17:38:52 +02002599 /* child must never use the atexit function */
2600 atexit_flag = 0;
2601
William Lallemand095ba4c2017-06-01 17:38:50 +02002602 /* Must chroot and setgid/setuid in the children */
2603 /* chroot if needed */
2604 if (global.chroot != NULL) {
2605 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
2606 Alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot);
2607 if (nb_oldpids)
2608 tell_old_pids(SIGTTIN);
2609 protocol_unbind_all();
2610 exit(1);
2611 }
2612 }
2613
2614 free(global.chroot);
2615 global.chroot = NULL;
2616
2617 /* setgid / setuid */
2618 if (global.gid) {
2619 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
2620 Warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2621 " without 'uid'/'user' is generally useless.\n", argv[0]);
2622
2623 if (setgid(global.gid) == -1) {
2624 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
2625 protocol_unbind_all();
2626 exit(1);
2627 }
2628 }
2629
2630 if (global.uid && setuid(global.uid) == -1) {
2631 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
2632 protocol_unbind_all();
2633 exit(1);
2634 }
2635
William Lallemand7f80eb22017-05-26 18:19:55 +02002636 /* pass through every cli socket, and check if it's bound to
2637 * the current process and if it exposes listeners sockets.
2638 * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork.
2639 * */
2640
2641 if (global.stats_fe) {
2642 struct bind_conf *bind_conf;
2643
2644 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
2645 if (bind_conf->level & ACCESS_FD_LISTENERS) {
2646 if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) {
2647 global.tune.options |= GTUNE_SOCKET_TRANSFER;
2648 break;
2649 }
2650 }
2651 }
2652 }
2653
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002654 /* we might have to unbind some proxies from some processes */
2655 px = proxy;
2656 while (px != NULL) {
2657 if (px->bind_proc && px->state != PR_STSTOPPED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002658 if (!(px->bind_proc & (1UL << proc))) {
2659 if (global.tune.options & GTUNE_SOCKET_TRANSFER)
2660 zombify_proxy(px);
2661 else
2662 stop_proxy(px);
2663 }
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002664 }
2665 px = px->next;
2666 }
2667
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002668 /* we might have to unbind some peers sections from some processes */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002669 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002670 if (!curpeers->peers_fe)
2671 continue;
2672
2673 if (curpeers->peers_fe->bind_proc & (1UL << proc))
2674 continue;
2675
2676 stop_proxy(curpeers->peers_fe);
2677 /* disable this peer section so that it kills itself */
Willy Tarreau47c8c022015-09-28 16:39:25 +02002678 signal_unregister_handler(curpeers->sighandler);
2679 task_delete(curpeers->sync_task);
2680 task_free(curpeers->sync_task);
2681 curpeers->sync_task = NULL;
2682 task_free(curpeers->peers_fe->task);
2683 curpeers->peers_fe->task = NULL;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002684 curpeers->peers_fe = NULL;
2685 }
2686
Willy Tarreaubaaee002006-06-26 02:48:02 +02002687 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
2688 * that we can detach from the TTY. We MUST NOT do it in other cases since
2689 * it would have already be done, and 0-2 would have been affected to listening
2690 * sockets
2691 */
Willy Tarreau106cb762008-11-16 07:40:34 +01002692 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002693 /* detach from the tty */
2694 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreau106cb762008-11-16 07:40:34 +01002695 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002696 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
2697 }
2698 pid = getpid(); /* update child's pid */
2699 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02002700 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002701 }
2702
Baptiste Assmann26c6eb82017-02-02 23:14:51 +01002703 /* initialize structures for name resolution */
2704 if (!dns_init_resolvers(1))
2705 exit(1);
2706
William Lallemande20b6a62017-06-01 17:38:55 +02002707 if (global.mode & MODE_MWORKER)
2708 mworker_pipe_register(mworker_pipe);
2709
Willy Tarreaudd815982007-10-16 12:25:14 +02002710 protocol_enable_all();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002711 /*
2712 * That's it : the central polling loop. Run until we stop.
2713 */
2714 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002715
Willy Tarreaubaaee002006-06-26 02:48:02 +02002716 /* Do some cleanup */
2717 deinit();
2718
2719 exit(0);
2720}
2721
2722
2723/*
2724 * Local variables:
2725 * c-indent-level: 8
2726 * c-basic-offset: 8
2727 * End:
2728 */