blob: 69d42da74f53caf063cae7b74f36b234448fda1a [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HA-Proxy : High Availability-enabled HTTP/TCP proxy
Willy Tarreau65e94d12018-08-02 18:12:50 +02003 * Copyright 2000-2018 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>
David Carlier6d5c8412017-11-29 11:02:32 +000058#include <pthread_np.h>
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +020059#endif
Willy Tarreaufc6c0322012-11-16 16:12:27 +010060#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020061
62#ifdef DEBUG_FULL
63#include <assert.h>
64#endif
Tim Duesterhusd6942c82017-11-20 15:58:35 +010065#if defined(USE_SYSTEMD)
66#include <systemd/sd-daemon.h>
67#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau2dd0d472006-06-29 17:53:05 +020069#include <common/base64.h>
70#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020071#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020072#include <common/compat.h>
73#include <common/config.h>
74#include <common/defaults.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010075#include <common/errors.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020076#include <common/memory.h>
77#include <common/mini-clist.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010078#include <common/namespace.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020079#include <common/regex.h>
80#include <common/standard.h>
81#include <common/time.h>
82#include <common/uri_auth.h>
83#include <common/version.h>
Christopher Fauletbe0faa22017-08-29 15:37:10 +020084#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020085
86#include <types/capture.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020087#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020088#include <types/global.h>
Simon Hormanac821422011-07-15 13:14:09 +090089#include <types/acl.h>
Willy Tarreau3c63fd82011-09-07 18:00:47 +020090#include <types/peers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020091
Willy Tarreau0fc45a72007-06-17 00:36:03 +020092#include <proto/acl.h>
Willy Tarreau2e845be2012-10-19 19:49:09 +020093#include <proto/arg.h>
Willy Tarreau3c595ac2015-04-19 09:59:31 +020094#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020095#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020096#include <proto/channel.h>
Willy Tarreauf2943dc2012-10-26 20:10:28 +020097#include <proto/connection.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020098#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020099#include <proto/filters.h>
Willy Tarreau34eb6712011-10-24 18:15:04 +0200100#include <proto/hdr_idx.h>
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +0100101#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +0200102#include <proto/listener.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103#include <proto/log.h>
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100104#include <proto/pattern.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +0200105#include <proto/protocol.h>
Willy Tarreau80587432006-12-24 17:47:20 +0100106#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107#include <proto/proxy.h>
108#include <proto/queue.h>
109#include <proto/server.h>
Willy Tarreaub1ec8c42015-04-03 13:53:24 +0200110#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +0200111#include <proto/stream.h>
Willy Tarreau29857942009-05-10 09:01:21 +0200112#include <proto/signal.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113#include <proto/task.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +0200114#include <proto/dns.h>
Christopher Fauletff2613e2016-11-09 11:36:17 +0100115#include <proto/vars.h>
Willy Tarreau50bc31d2017-08-16 15:35:19 +0200116#ifdef USE_OPENSSL
Grant Zhang872f9c22017-01-21 01:10:18 +0000117#include <proto/ssl_sock.h>
Willy Tarreau50bc31d2017-08-16 15:35:19 +0200118#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119
Willy Tarreau477ecd82010-01-03 21:12:30 +0100120/* list of config files */
121static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100123int relative_pid = 1; /* process id starting at 1 */
Willy Tarreau387bd4f2017-11-10 19:08:14 +0100124unsigned long pid_bit = 1; /* bit corresponding to the process id */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125
Olivier Houchard79321b92018-07-26 17:55:11 +0200126volatile unsigned long sleeping_thread_mask; /* Threads that are about to sleep in poll() */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127/* global options */
128struct global global = {
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100129 .hard_stop_after = TICK_ETERNITY,
Willy Tarreau247a13a2012-11-15 17:38:15 +0100130 .nbproc = 1,
Christopher Fauletbe0faa22017-08-29 15:37:10 +0200131 .nbthread = 1,
William Lallemand5f232402012-04-05 18:02:55 +0200132 .req_count = 0,
William Lallemand0f99e342011-10-12 17:50:54 +0200133 .logsrvs = LIST_HEAD_INIT(global.logsrvs),
William Lallemand9d5f5482012-11-07 16:12:57 +0100134 .maxzlibmem = 0,
William Lallemandd85f9172012-11-09 17:05:39 +0100135 .comp_rate_lim = 0,
Emeric Brun850efd52014-01-29 12:24:34 +0100136 .ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED,
Emeric Bruned760922010-10-22 17:59:25 +0200137 .unix_bind = {
138 .ux = {
139 .uid = -1,
140 .gid = -1,
141 .mode = 0,
142 }
143 },
Willy Tarreau27a674e2009-08-17 07:23:33 +0200144 .tune = {
145 .bufsize = BUFSIZE,
Willy Tarreau27097842015-09-28 13:53:23 +0200146 .maxrewrite = -1,
Willy Tarreau43961d52010-10-04 20:39:20 +0200147 .chksize = BUFSIZE,
Willy Tarreaua24adf02014-11-27 01:11:56 +0100148 .reserved_bufs = RESERVED_BUFS,
Willy Tarreauf3045d22015-04-29 16:24:50 +0200149 .pattern_cache = DEFAULT_PAT_LRU_SIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200150#ifdef USE_OPENSSL
Emeric Brun46635772012-11-14 11:32:56 +0100151 .sslcachesize = SSLCACHESIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200152#endif
William Lallemandf3747832012-11-09 12:33:10 +0100153 .comp_maxlevel = 1,
Willy Tarreau7e312732014-02-12 16:35:14 +0100154#ifdef DEFAULT_IDLE_TIMER
155 .idle_timer = DEFAULT_IDLE_TIMER,
156#else
157 .idle_timer = 1000, /* 1 second */
158#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200159 },
Emeric Brun76d88952012-10-05 15:47:31 +0200160#ifdef USE_OPENSSL
161#ifdef DEFAULT_MAXSSLCONN
Willy Tarreau403edff2012-09-06 11:58:37 +0200162 .maxsslconn = DEFAULT_MAXSSLCONN,
163#endif
Emeric Brun76d88952012-10-05 15:47:31 +0200164#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165 /* others NULL OK */
166};
167
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100168struct activity activity[MAX_THREADS] __attribute__((aligned(64))) = { };
169
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170/*********************************************************************/
171
172int stopping; /* non zero means stopping in progress */
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100173int killed; /* non zero means a hard-stop is triggered */
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200174int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175
176/* Here we store informations about the pids of the processes we may pause
177 * or kill. We will send them a signal every 10 ms until we can bind to all
178 * our ports. With 200 retries, that's about 2 seconds.
179 */
180#define MAX_START_RETRIES 200
Willy Tarreaubaaee002006-06-26 02:48:02 +0200181static int *oldpids = NULL;
182static int oldpids_sig; /* use USR1 or TERM */
183
Olivier Houchardf73629d2017-04-05 22:33:04 +0200184/* Path to the unix socket we use to retrieve listener sockets from the old process */
185static const char *old_unixsocket;
186
William Lallemand85b0bd92017-06-01 17:38:53 +0200187static char *cur_unixsocket = NULL;
188
William Lallemandcb11fd22017-06-01 17:38:52 +0200189int atexit_flag = 0;
190
Willy Tarreaubb545b42010-08-25 12:58:59 +0200191int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200192const int zero = 0;
193const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200194const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100196char hostname[MAX_HOSTNAME_LEN];
Emeric Brun2b920a12010-09-23 18:30:22 +0200197char localpeer[MAX_HOSTNAME_LEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200198
Willy Tarreau89efaed2013-12-13 15:14:55 +0100199/* used from everywhere just to drain results we don't want to read and which
200 * recent versions of gcc increasingly and annoyingly complain about.
201 */
202int shut_your_big_mouth_gcc_int = 0;
203
William Lallemand73b85e72017-06-01 17:38:51 +0200204int *children = NULL; /* store PIDs of children in master workers mode */
205
206static volatile sig_atomic_t caught_signal = 0;
207static char **next_argv = NULL;
208
William Lallemande20b6a62017-06-01 17:38:55 +0200209int mworker_pipe[2];
210
Willy Tarreau08ceb102011-07-24 22:58:00 +0200211/* list of the temporarily limited listeners because of lack of resource */
212struct list global_listener_queue = LIST_HEAD_INIT(global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +0200213struct task *global_listener_queue_task;
Olivier Houchard9f6af332018-05-25 14:04:04 +0200214static struct task *manage_global_listener_queue(struct task *t, void *context, unsigned short state);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215
William Lallemandb3f2be32018-09-11 10:06:18 +0200216static void *run_thread_poll_loop(void *data);
217
Willy Tarreauff055502014-04-28 22:27:06 +0200218/* bitfield of a few warnings to emit just once (WARN_*) */
219unsigned int warned = 0;
220
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100221
222/* These are strings to be reported in the output of "haproxy -vv". They may
223 * either be constants (in which case must_free must be zero) or dynamically
224 * allocated strings to pass to free() on exit, and in this case must_free
225 * must be non-zero.
226 */
227struct list build_opts_list = LIST_HEAD_INIT(build_opts_list);
228struct build_opts_str {
229 struct list list;
230 const char *str;
231 int must_free;
232};
233
Willy Tarreaue6945732016-12-21 19:57:00 +0100234/* These functions are called just after the point where the program exits
235 * after a config validity check, so they are generally suited for resource
236 * allocation and slow initializations that should be skipped during basic
237 * config checks. The functions must return 0 on success, or a combination
238 * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause
239 * and immediate exit, so the function must have emitted any useful error.
240 */
241struct list post_check_list = LIST_HEAD_INIT(post_check_list);
242struct post_check_fct {
243 struct list list;
244 int (*fct)();
245};
246
Willy Tarreau05554e62016-12-21 20:46:26 +0100247/* These functions are called when freeing the global sections at the end
248 * of deinit, after everything is stopped. They don't return anything, and
249 * they work in best effort mode as their sole goal is to make valgrind
250 * mostly happy.
251 */
252struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list);
253struct post_deinit_fct {
254 struct list list;
255 void (*fct)();
256};
257
Christopher Faulet415f6112017-07-25 16:52:58 +0200258/* These functions are called for each thread just after the thread creation
259 * and before running the scheduler. They should be used to do per-thread
260 * initializations. They must return 0 if an error occurred. */
261struct list per_thread_init_list = LIST_HEAD_INIT(per_thread_init_list);
262struct per_thread_init_fct {
263 struct list list;
264 int (*fct)();
265};
266
267/* These functions are called for each thread just after the scheduler loop and
268 * before exiting the thread. They don't return anything and, as for post-deinit
269 * functions, they work in best effort mode as their sole goal is to make
270 * valgrind mostly happy. */
271struct list per_thread_deinit_list = LIST_HEAD_INIT(per_thread_deinit_list);
272struct per_thread_deinit_fct {
273 struct list list;
274 void (*fct)();
275};
276
Willy Tarreaubaaee002006-06-26 02:48:02 +0200277/*********************************************************************/
278/* general purpose functions ***************************************/
279/*********************************************************************/
280
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100281/* used to register some build option strings at boot. Set must_free to
282 * non-zero if the string must be freed upon exit.
283 */
284void hap_register_build_opts(const char *str, int must_free)
285{
286 struct build_opts_str *b;
287
288 b = calloc(1, sizeof(*b));
289 if (!b) {
290 fprintf(stderr, "out of memory\n");
291 exit(1);
292 }
293 b->str = str;
294 b->must_free = must_free;
295 LIST_ADDQ(&build_opts_list, &b->list);
296}
297
Willy Tarreaue6945732016-12-21 19:57:00 +0100298/* used to register some initialization functions to call after the checks. */
299void hap_register_post_check(int (*fct)())
300{
301 struct post_check_fct *b;
302
303 b = calloc(1, sizeof(*b));
304 if (!b) {
305 fprintf(stderr, "out of memory\n");
306 exit(1);
307 }
308 b->fct = fct;
309 LIST_ADDQ(&post_check_list, &b->list);
310}
311
Willy Tarreau05554e62016-12-21 20:46:26 +0100312/* used to register some de-initialization functions to call after everything
313 * has stopped.
314 */
315void hap_register_post_deinit(void (*fct)())
316{
317 struct post_deinit_fct *b;
318
319 b = calloc(1, sizeof(*b));
320 if (!b) {
321 fprintf(stderr, "out of memory\n");
322 exit(1);
323 }
324 b->fct = fct;
325 LIST_ADDQ(&post_deinit_list, &b->list);
326}
327
Christopher Faulet415f6112017-07-25 16:52:58 +0200328/* used to register some initialization functions to call for each thread. */
329void hap_register_per_thread_init(int (*fct)())
330{
331 struct per_thread_init_fct *b;
332
333 b = calloc(1, sizeof(*b));
334 if (!b) {
335 fprintf(stderr, "out of memory\n");
336 exit(1);
337 }
338 b->fct = fct;
339 LIST_ADDQ(&per_thread_init_list, &b->list);
340}
341
342/* used to register some de-initialization functions to call for each thread. */
343void hap_register_per_thread_deinit(void (*fct)())
344{
345 struct per_thread_deinit_fct *b;
346
347 b = calloc(1, sizeof(*b));
348 if (!b) {
349 fprintf(stderr, "out of memory\n");
350 exit(1);
351 }
352 b->fct = fct;
353 LIST_ADDQ(&per_thread_deinit_list, &b->list);
354}
355
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100356static void display_version()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357{
358 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau65e94d12018-08-02 18:12:50 +0200359 printf("Copyright 2000-2018 Willy Tarreau <willy@haproxy.org>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360}
361
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100362static void display_build_opts()
Willy Tarreau7b066db2007-12-02 11:28:59 +0100363{
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100364 struct build_opts_str *item;
365
Willy Tarreau7b066db2007-12-02 11:28:59 +0100366 printf("Build options :"
367#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100368 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100369#endif
370#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100371 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100372#endif
373#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100374 "\n CC = " BUILD_CC
375#endif
376#ifdef BUILD_CFLAGS
377 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100378#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100379#ifdef BUILD_OPTIONS
380 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100381#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200382 "\n\nDefault settings :"
383 "\n maxconn = %d, bufsize = %d, maxrewrite = %d, maxpollevents = %d"
384 "\n\n",
385 DEFAULT_MAXCONN, BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS);
Willy Tarreaube5b6852009-10-03 18:57:08 +0200386
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100387 list_for_each_entry(item, &build_opts_list, list) {
388 puts(item->str);
389 }
390
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100391 putchar('\n');
392
Willy Tarreaube5b6852009-10-03 18:57:08 +0200393 list_pollers(stdout);
394 putchar('\n');
Christopher Faulet98d9fe22018-04-10 14:37:32 +0200395 list_mux_proto(stdout);
396 putchar('\n');
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100397 list_filters(stdout);
398 putchar('\n');
Willy Tarreau7b066db2007-12-02 11:28:59 +0100399}
400
Willy Tarreaubaaee002006-06-26 02:48:02 +0200401/*
402 * This function prints the command line usage and exits
403 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100404static void usage(char *name)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405{
406 display_version();
407 fprintf(stderr,
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200408 "Usage : %s [-f <cfgfile|cfgdir>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
Willy Tarreaua088d312015-10-08 11:58:48 +0200410 " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100411 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412 " -d enters debug mode ; -db only disables background mode.\n"
Willy Tarreau6e064432012-05-08 15:40:42 +0200413 " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200415 " -D goes daemon ; -C changes to <dir> before loading files.\n"
William Lallemand095ba4c2017-06-01 17:38:50 +0200416 " -W master-worker mode.\n"
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100417#if defined(USE_SYSTEMD)
418 " -Ws master-worker mode with systemd notify support.\n"
419#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200420 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200421 " -c check mode : only check config files and exit\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200422 " -n sets the maximum total # of connections (%d)\n"
423 " -m limits the usable amount of memory (in MB)\n"
424 " -N sets the default, per-proxy maximum # of connections (%d)\n"
Emeric Brun2b920a12010-09-23 18:30:22 +0200425 " -L set local peer name (default to hostname)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200426 " -p writes pids of all children to this file\n"
427#if defined(ENABLE_EPOLL)
428 " -de disables epoll() usage even when available\n"
429#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200430#if defined(ENABLE_KQUEUE)
431 " -dk disables kqueue() usage even when available\n"
432#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433#if defined(ENABLE_POLL)
434 " -dp disables poll() usage even when available\n"
435#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200436#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100437 " -dS disables splice usage (broken on old kernels)\n"
438#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200439#if defined(USE_GETADDRINFO)
440 " -dG disables getaddrinfo() usage\n"
441#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000442#if defined(SO_REUSEPORT)
443 " -dR disables SO_REUSEPORT usage\n"
444#endif
Willy Tarreau3eed10e2016-11-07 21:03:16 +0100445 " -dr ignores server address resolution failures\n"
Emeric Brun850efd52014-01-29 12:24:34 +0100446 " -dV disables SSL verify on servers side\n"
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +0200447 " -sf/-st [pid ]* finishes/terminates old pids.\n"
Olivier Houchardf73629d2017-04-05 22:33:04 +0200448 " -x <unix_socket> get listening sockets from a unix socket\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449 "\n",
450 name, DEFAULT_MAXCONN, cfg_maxpconn);
451 exit(1);
452}
453
454
455
456/*********************************************************************/
457/* more specific functions ***************************************/
458/*********************************************************************/
459
William Lallemand73b85e72017-06-01 17:38:51 +0200460/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
461 * pids the signal was correctly delivered to.
462 */
463static int tell_old_pids(int sig)
464{
465 int p;
466 int ret = 0;
467 for (p = 0; p < nb_oldpids; p++)
468 if (kill(oldpids[p], sig) == 0)
469 ret++;
470 return ret;
471}
472
473/* return 1 if a pid is a current child otherwise 0 */
474
475int current_child(int pid)
476{
477 int i;
478
479 for (i = 0; i < global.nbproc; i++) {
480 if (children[i] == pid)
481 return 1;
482 }
483 return 0;
484}
485
William Lallemand73b85e72017-06-01 17:38:51 +0200486static void mworker_block_signals()
487{
488 sigset_t set;
489
490 sigemptyset(&set);
491 sigaddset(&set, SIGUSR1);
492 sigaddset(&set, SIGUSR2);
493 sigaddset(&set, SIGHUP);
494 sigaddset(&set, SIGINT);
495 sigaddset(&set, SIGTERM);
William Lallemandebf304f2018-09-11 10:06:21 +0200496 sigaddset(&set, SIGCHLD);
William Lallemand6e1796e2018-06-07 11:23:40 +0200497 ha_sigmask(SIG_SETMASK, &set, NULL);
William Lallemand73b85e72017-06-01 17:38:51 +0200498}
499
500static void mworker_unblock_signals()
501{
502 sigset_t set;
503
504 sigemptyset(&set);
505 sigaddset(&set, SIGUSR1);
506 sigaddset(&set, SIGUSR2);
507 sigaddset(&set, SIGHUP);
508 sigaddset(&set, SIGINT);
509 sigaddset(&set, SIGTERM);
William Lallemandebf304f2018-09-11 10:06:21 +0200510 sigaddset(&set, SIGCHLD);
William Lallemand6e1796e2018-06-07 11:23:40 +0200511 ha_sigmask(SIG_UNBLOCK, &set, NULL);
William Lallemand73b85e72017-06-01 17:38:51 +0200512}
513
William Lallemand73b85e72017-06-01 17:38:51 +0200514/*
515 * Send signal to every known children.
516 */
517
518static void mworker_kill(int sig)
519{
520 int i;
521
522 tell_old_pids(sig);
523 if (children) {
524 for (i = 0; i < global.nbproc; i++)
525 kill(children[i], sig);
526 }
527}
528
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529/*
William Lallemand75ea0a02017-11-15 19:02:58 +0100530 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
531 * fd, and the FD provided by fd@
532 */
533static void mworker_cleanlisteners()
534{
535 struct listener *l, *l_next;
536 struct proxy *curproxy;
Willy Tarreau473cf5d2017-12-05 11:14:12 +0100537 struct peers *curpeers;
William Lallemand75ea0a02017-11-15 19:02:58 +0100538
William Lallemand3da97692018-09-11 10:06:19 +0200539 /* we might have to unbind some peers sections from some processes */
540 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
541 if (!curpeers->peers_fe)
542 continue;
Willy Tarreau473cf5d2017-12-05 11:14:12 +0100543
William Lallemand3da97692018-09-11 10:06:19 +0200544 stop_proxy(curpeers->peers_fe);
545 /* disable this peer section so that it kills itself */
546 signal_unregister_handler(curpeers->sighandler);
547 task_delete(curpeers->sync_task);
548 task_free(curpeers->sync_task);
549 curpeers->sync_task = NULL;
550 task_free(curpeers->peers_fe->task);
551 curpeers->peers_fe->task = NULL;
552 curpeers->peers_fe = NULL;
553 }
William Lallemand75ea0a02017-11-15 19:02:58 +0100554
William Lallemand91c13b62018-09-11 10:06:20 +0200555 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
William Lallemand75ea0a02017-11-15 19:02:58 +0100556 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
557 /* does not close if the FD is inherited with fd@
558 * from the parent process */
William Lallemand91c13b62018-09-11 10:06:20 +0200559 if (!(l->options & LI_O_INHERITED))
560 unbind_listener(l);
561 /* remove the listener, but we might want to keep some
562 * for the master in the future... */
563 delete_listener(l);
William Lallemand75ea0a02017-11-15 19:02:58 +0100564 }
565 }
566}
567
568
569/*
William Lallemand73b85e72017-06-01 17:38:51 +0200570 * remove a pid forom the olpid array and decrease nb_oldpids
571 * return 1 pid was found otherwise return 0
572 */
573
574int delete_oldpid(int pid)
575{
576 int i;
577
578 for (i = 0; i < nb_oldpids; i++) {
579 if (oldpids[i] == pid) {
580 oldpids[i] = oldpids[nb_oldpids - 1];
581 oldpids[nb_oldpids - 1] = 0;
582 nb_oldpids--;
583 return 1;
584 }
585 }
586 return 0;
587}
588
William Lallemand85b0bd92017-06-01 17:38:53 +0200589
590static void get_cur_unixsocket()
591{
592 /* if -x was used, try to update the stat socket if not available anymore */
593 if (global.stats_fe) {
594 struct bind_conf *bind_conf;
595
596 /* pass through all stats socket */
597 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
598 struct listener *l;
599
600 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
601
602 if (l->addr.ss_family == AF_UNIX &&
603 (bind_conf->level & ACCESS_FD_LISTENERS)) {
604 const struct sockaddr_un *un;
605
606 un = (struct sockaddr_un *)&l->addr;
607 /* priority to old_unixsocket */
608 if (!cur_unixsocket) {
609 cur_unixsocket = strdup(un->sun_path);
610 } else {
611 if (old_unixsocket && !strcmp(un->sun_path, old_unixsocket)) {
612 free(cur_unixsocket);
613 cur_unixsocket = strdup(old_unixsocket);
614 return;
615 }
616 }
617 }
618 }
619 }
620 }
621 if (!cur_unixsocket && old_unixsocket)
622 cur_unixsocket = strdup(old_unixsocket);
623}
624
William Lallemand73b85e72017-06-01 17:38:51 +0200625/*
626 * When called, this function reexec haproxy with -sf followed by current
627 * children PIDs and possibily old children PIDs if they didn't leave yet.
628 */
629static void mworker_reload()
630{
631 int next_argc = 0;
632 int j;
633 char *msg = NULL;
634
635 mworker_block_signals();
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100636#if defined(USE_SYSTEMD)
637 if (global.tune.options & GTUNE_USE_SYSTEMD)
638 sd_notify(0, "RELOADING=1");
639#endif
William Lallemand73b85e72017-06-01 17:38:51 +0200640 setenv("HAPROXY_MWORKER_REEXEC", "1", 1);
641
642 /* compute length */
643 while (next_argv[next_argc])
644 next_argc++;
645
William Lallemand85b0bd92017-06-01 17:38:53 +0200646 /* 1 for haproxy -sf, 2 for -x /socket */
647 next_argv = realloc(next_argv, (next_argc + 1 + 2 + global.nbproc + nb_oldpids + 1) * sizeof(char *));
William Lallemand73b85e72017-06-01 17:38:51 +0200648 if (next_argv == NULL)
649 goto alloc_error;
650
651
652 /* add -sf <PID>* to argv */
653 if (children || nb_oldpids > 0)
654 next_argv[next_argc++] = "-sf";
655 if (children) {
656 for (j = 0; j < global.nbproc; next_argc++,j++) {
657 next_argv[next_argc] = memprintf(&msg, "%d", children[j]);
658 if (next_argv[next_argc] == NULL)
659 goto alloc_error;
660 msg = NULL;
661 }
662 }
663 /* copy old process PIDs */
664 for (j = 0; j < nb_oldpids; next_argc++,j++) {
665 next_argv[next_argc] = memprintf(&msg, "%d", oldpids[j]);
666 if (next_argv[next_argc] == NULL)
667 goto alloc_error;
668 msg = NULL;
669 }
670 next_argv[next_argc] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200671
William Lallemand2bf6d622017-06-20 11:20:23 +0200672 /* add the -x option with the stat socket */
William Lallemand85b0bd92017-06-01 17:38:53 +0200673 if (cur_unixsocket) {
674
William Lallemand2bf6d622017-06-20 11:20:23 +0200675 next_argv[next_argc++] = "-x";
676 next_argv[next_argc++] = (char *)cur_unixsocket;
677 next_argv[next_argc++] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200678 }
679
William Lallemandb3f2be32018-09-11 10:06:18 +0200680 deinit_pollers(); /* we don't want to leak the poller fd */
681
Christopher Faulet767a84b2017-11-24 16:50:31 +0100682 ha_warning("Reexecuting Master process\n");
Tim Duesterhus0436ab72017-11-12 17:39:18 +0100683 execvp(next_argv[0], next_argv);
William Lallemand73b85e72017-06-01 17:38:51 +0200684
Christopher Faulet767a84b2017-11-24 16:50:31 +0100685 ha_warning("Failed to reexecute the master process [%d]: %s\n", pid, strerror(errno));
William Lallemand722d4ca2017-11-15 19:02:55 +0100686 return;
687
William Lallemand73b85e72017-06-01 17:38:51 +0200688alloc_error:
Christopher Faulet767a84b2017-11-24 16:50:31 +0100689 ha_warning("Failed to reexecute the master processs [%d]: Cannot allocate memory\n", pid);
William Lallemand73b85e72017-06-01 17:38:51 +0200690 return;
691}
692
William Lallemand73b85e72017-06-01 17:38:51 +0200693/*
William Lallemandb3f2be32018-09-11 10:06:18 +0200694 * When called, this function reexec haproxy with -sf followed by current
695 * children PIDs and possibily old children PIDs if they didn't leave yet.
William Lallemand73b85e72017-06-01 17:38:51 +0200696 */
William Lallemandb3f2be32018-09-11 10:06:18 +0200697static void mworker_catch_sighup(struct sig_handler *sh)
William Lallemand73b85e72017-06-01 17:38:51 +0200698{
William Lallemandb3f2be32018-09-11 10:06:18 +0200699 mworker_reload();
700}
William Lallemand73b85e72017-06-01 17:38:51 +0200701
William Lallemandb3f2be32018-09-11 10:06:18 +0200702static void mworker_catch_sigterm(struct sig_handler *sh)
703{
704 int sig = sh->arg;
William Lallemand2f8b31c2017-11-15 19:02:56 +0100705
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100706#if defined(USE_SYSTEMD)
William Lallemandb3f2be32018-09-11 10:06:18 +0200707 if (global.tune.options & GTUNE_USE_SYSTEMD) {
708 sd_notify(0, "STOPPING=1");
709 }
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100710#endif
William Lallemandb3f2be32018-09-11 10:06:18 +0200711 ha_warning("Exiting Master process...\n");
712 mworker_kill(sig);
713}
William Lallemand73b85e72017-06-01 17:38:51 +0200714
William Lallemandb3f2be32018-09-11 10:06:18 +0200715/*
716 * Wait for every children to exit
717 */
William Lallemand73b85e72017-06-01 17:38:51 +0200718
William Lallemandb3f2be32018-09-11 10:06:18 +0200719static void mworker_catch_sigchld(struct sig_handler *sh)
720{
721 int exitpid = -1;
722 int status = 0;
William Lallemand73b85e72017-06-01 17:38:51 +0200723
William Lallemandb3f2be32018-09-11 10:06:18 +0200724restart_wait:
William Lallemand73b85e72017-06-01 17:38:51 +0200725
William Lallemandb3f2be32018-09-11 10:06:18 +0200726 exitpid = waitpid(-1, &status, WNOHANG);
727 if (exitpid > 0) {
William Lallemand73b85e72017-06-01 17:38:51 +0200728 if (WIFEXITED(status))
729 status = WEXITSTATUS(status);
730 else if (WIFSIGNALED(status))
731 status = 128 + WTERMSIG(status);
732 else if (WIFSTOPPED(status))
733 status = 128 + WSTOPSIG(status);
734 else
735 status = 255;
736
737 if (!children) {
Tim Duesterhusd16f4502017-12-05 18:14:13 +0100738 ha_warning("Worker %d exited with code %d\n", exitpid, status);
William Lallemand73b85e72017-06-01 17:38:51 +0200739 } else {
740 /* check if exited child was in the current children list */
741 if (current_child(exitpid)) {
Tim Duesterhusd16f4502017-12-05 18:14:13 +0100742 ha_alert("Current worker %d exited with code %d\n", exitpid, status);
William Lallemand69f9b3b2017-06-01 17:38:54 +0200743 if (status != 0 && status != 130 && status != 143
William Lallemand4cfede82017-11-24 22:02:34 +0100744 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100745 ha_alert("exit-on-failure: killing every workers with SIGTERM\n");
William Lallemand69f9b3b2017-06-01 17:38:54 +0200746 mworker_kill(SIGTERM);
747 }
William Lallemand73b85e72017-06-01 17:38:51 +0200748 } else {
Tim Duesterhusd16f4502017-12-05 18:14:13 +0100749 ha_warning("Former worker %d exited with code %d\n", exitpid, status);
William Lallemand73b85e72017-06-01 17:38:51 +0200750 delete_oldpid(exitpid);
751 }
752 }
William Lallemandb3f2be32018-09-11 10:06:18 +0200753 /* do it again to check if it was the last worker */
754 goto restart_wait;
William Lallemand73b85e72017-06-01 17:38:51 +0200755 }
William Lallemandb3f2be32018-09-11 10:06:18 +0200756 /* Better rely on the system than on a list of process to check if it was the last one */
757 else if (exitpid == -1 && errno == ECHILD) {
758 ha_warning("All workers exited. Exiting... (%d)\n", status);
759 atexit_flag = 0;
760 exit(status); /* parent must leave using the latest status code known */
761 }
762
William Lallemand73b85e72017-06-01 17:38:51 +0200763}
764
William Lallemandb3f2be32018-09-11 10:06:18 +0200765static void mworker_loop()
766{
767
768#if defined(USE_SYSTEMD)
769 if (global.tune.options & GTUNE_USE_SYSTEMD)
770 sd_notifyf(0, "READY=1\nMAINPID=%lu", (unsigned long)getpid());
771#endif
772
773 signal_register_fct(SIGTERM, mworker_catch_sigterm, SIGTERM);
774 signal_register_fct(SIGUSR1, mworker_catch_sigterm, SIGUSR1);
775 signal_register_fct(SIGINT, mworker_catch_sigterm, SIGINT);
776 signal_register_fct(SIGHUP, mworker_catch_sighup, SIGHUP);
777 signal_register_fct(SIGUSR2, mworker_catch_sighup, SIGUSR2);
778 signal_register_fct(SIGCHLD, mworker_catch_sigchld, SIGCHLD);
779
780 mworker_unblock_signals();
781 mworker_cleanlisteners();
782
783 global.nbthread = 1;
784 relative_pid = 1;
785 pid_bit = 1;
786
787 jobs++; /* this is the "master" job, we want to take care of the
788 signals even if there is no listener so the poll loop don't
789 leave */
790
791 fork_poller();
792 run_thread_poll_loop((int []){0});
793}
William Lallemandcb11fd22017-06-01 17:38:52 +0200794
795/*
796 * Reexec the process in failure mode, instead of exiting
797 */
798void reexec_on_failure()
799{
800 if (!atexit_flag)
801 return;
802
803 setenv("HAPROXY_MWORKER_WAIT_ONLY", "1", 1);
804
Christopher Faulet767a84b2017-11-24 16:50:31 +0100805 ha_warning("Reexecuting Master process in waitpid mode\n");
William Lallemandcb11fd22017-06-01 17:38:52 +0200806 mworker_reload();
William Lallemandcb11fd22017-06-01 17:38:52 +0200807}
William Lallemand73b85e72017-06-01 17:38:51 +0200808
809
810/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200811 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
812 * a signal zero to all subscribers. This means that it's as easy as
813 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200814 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100815static void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200816{
817 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200818 signal_unregister_handler(sh);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100819 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200820}
821
822/*
823 * upon SIGTTOU, we pause everything
824 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100825static void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200826{
827 pause_proxies();
Willy Tarreaubafbe012017-11-24 17:34:44 +0100828 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829}
830
831/*
832 * upon SIGTTIN, let's have a soft stop.
833 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100834static void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200835{
Willy Tarreaube58c382011-07-24 18:28:10 +0200836 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200837}
838
839/*
840 * this function dumps every server's state when the process receives SIGHUP.
841 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100842static void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843{
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100844 struct proxy *p = proxies_list;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200845
Christopher Faulet767a84b2017-11-24 16:50:31 +0100846 ha_warning("SIGHUP received, dumping servers states.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847 while (p) {
848 struct server *s = p->srv;
849
850 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
851 while (s) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100852 chunk_printf(&trash,
853 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
854 p->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +0200855 (s->cur_state != SRV_ST_STOPPED) ? "UP" : "DOWN",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100856 s->cur_sess, s->nbpend, s->counters.cum_sess);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200857 ha_warning("%s\n", trash.area);
858 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200859 s = s->next;
860 }
861
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200862 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
863 if (!p->srv) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100864 chunk_printf(&trash,
865 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
866 p->id,
867 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 +0200868 } else if (p->srv_act == 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100869 chunk_printf(&trash,
870 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
871 p->id,
872 (p->srv_bck) ? "is running on backup servers" : "has no server available",
873 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 +0200874 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100875 chunk_printf(&trash,
876 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
877 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
878 p->id, p->srv_act, p->srv_bck,
879 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 +0200880 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200881 ha_warning("%s\n", trash.area);
882 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200883
884 p = p->next;
885 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200886}
887
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100888static void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200889{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200890 /* dump memory usage then free everything possible */
891 dump_pools();
Willy Tarreaubafbe012017-11-24 17:34:44 +0100892 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200893}
894
William Lallemande1340412017-12-28 16:09:36 +0100895/*
896 * This function dup2 the stdio FDs (0,1,2) with <fd>, then closes <fd>
897 * If <fd> < 0, it opens /dev/null and use it to dup
898 *
899 * In the case of chrooting, you have to open /dev/null before the chroot, and
900 * pass the <fd> to this function
901 */
902static void stdio_quiet(int fd)
903{
904 if (fd < 0)
905 fd = open("/dev/null", O_RDWR, 0);
906
907 if (fd > -1) {
908 fclose(stdin);
909 fclose(stdout);
910 fclose(stderr);
911
912 dup2(fd, 0);
913 dup2(fd, 1);
914 dup2(fd, 2);
915 if (fd > 2)
916 close(fd);
917 return;
918 }
919
920 ha_alert("Cannot open /dev/null\n");
921 exit(EXIT_FAILURE);
922}
923
924
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200925/* This function check if cfg_cfgfiles containes directories.
926 * If it find one, it add all the files (and only files) it containes
927 * in cfg_cfgfiles in place of the directory (and remove the directory).
928 * It add the files in lexical order.
929 * It add only files with .cfg extension.
930 * It doesn't add files with name starting with '.'
931 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100932static void cfgfiles_expand_directories(void)
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200933{
934 struct wordlist *wl, *wlb;
935 char *err = NULL;
936
937 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
938 struct stat file_stat;
939 struct dirent **dir_entries = NULL;
940 int dir_entries_nb;
941 int dir_entries_it;
942
943 if (stat(wl->s, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100944 ha_alert("Cannot open configuration file/directory %s : %s\n",
945 wl->s,
946 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200947 exit(1);
948 }
949
950 if (!S_ISDIR(file_stat.st_mode))
951 continue;
952
953 /* from this point wl->s is a directory */
954
955 dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort);
956 if (dir_entries_nb < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100957 ha_alert("Cannot open configuration directory %s : %s\n",
958 wl->s,
959 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200960 exit(1);
961 }
962
963 /* for each element in the directory wl->s */
964 for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) {
965 struct dirent *dir_entry = dir_entries[dir_entries_it];
966 char *filename = NULL;
967 char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg");
968
969 /* don't add filename that begin with .
970 * only add filename with .cfg extention
971 */
972 if (dir_entry->d_name[0] == '.' ||
973 !(d_name_cfgext && d_name_cfgext[4] == '\0'))
974 goto next_dir_entry;
975
976 if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100977 ha_alert("Cannot load configuration files %s : out of memory.\n",
978 filename);
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200979 exit(1);
980 }
981
982 if (stat(filename, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100983 ha_alert("Cannot open configuration file %s : %s\n",
984 wl->s,
985 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200986 exit(1);
987 }
988
989 /* don't add anything else than regular file in cfg_cfgfiles
990 * this way we avoid loops
991 */
992 if (!S_ISREG(file_stat.st_mode))
993 goto next_dir_entry;
994
995 if (!list_append_word(&wl->list, filename, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100996 ha_alert("Cannot load configuration files %s : %s\n",
997 filename,
998 err);
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200999 exit(1);
1000 }
1001
1002next_dir_entry:
1003 free(filename);
1004 free(dir_entry);
1005 }
1006
1007 free(dir_entries);
1008
1009 /* remove the current directory (wl) from cfg_cfgfiles */
1010 free(wl->s);
1011 LIST_DEL(&wl->list);
1012 free(wl);
1013 }
1014
1015 free(err);
1016}
1017
Olivier Houchardf73629d2017-04-05 22:33:04 +02001018static int get_old_sockets(const char *unixsocket)
1019{
1020 char *cmsgbuf = NULL, *tmpbuf = NULL;
1021 int *tmpfd = NULL;
1022 struct sockaddr_un addr;
1023 struct cmsghdr *cmsg;
1024 struct msghdr msghdr;
1025 struct iovec iov;
1026 struct xfer_sock_list *xfer_sock = NULL;
Olivier Houchard54740872017-04-06 14:45:14 +02001027 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf73629d2017-04-05 22:33:04 +02001028 int sock = -1;
1029 int ret = -1;
1030 int ret2 = -1;
1031 int fd_nb;
1032 int got_fd = 0;
1033 int i = 0;
1034 size_t maxoff = 0, curoff = 0;
1035
1036 memset(&msghdr, 0, sizeof(msghdr));
1037 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
1038 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001039 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001040 goto out;
1041 }
1042 sock = socket(PF_UNIX, SOCK_STREAM, 0);
1043 if (sock < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001044 ha_warning("Failed to connect to the old process socket '%s'\n",
1045 unixsocket);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001046 goto out;
1047 }
1048 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path));
1049 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
1050 addr.sun_family = PF_UNIX;
1051 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
1052 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001053 ha_warning("Failed to connect to the old process socket '%s'\n",
1054 unixsocket);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001055 goto out;
1056 }
Olivier Houchard54740872017-04-06 14:45:14 +02001057 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001058 iov.iov_base = &fd_nb;
1059 iov.iov_len = sizeof(fd_nb);
1060 msghdr.msg_iov = &iov;
1061 msghdr.msg_iovlen = 1;
1062 send(sock, "_getsocks\n", strlen("_getsocks\n"), 0);
1063 /* First, get the number of file descriptors to be received */
1064 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001065 ha_warning("Failed to get the number of sockets to be transferred !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001066 goto out;
1067 }
1068 if (fd_nb == 0) {
1069 ret = 0;
1070 goto out;
1071 }
Olivier Houchardf143b802017-11-04 15:13:01 +01001072 tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001073 if (tmpbuf == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001074 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001075 goto out;
1076 }
1077 tmpfd = malloc(fd_nb * sizeof(int));
1078 if (tmpfd == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001079 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001080 goto out;
1081 }
1082 msghdr.msg_control = cmsgbuf;
1083 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
Olivier Houchardf143b802017-11-04 15:13:01 +01001084 iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001085 do {
1086 int ret3;
1087
1088 iov.iov_base = tmpbuf + curoff;
1089 ret = recvmsg(sock, &msghdr, 0);
1090 if (ret == -1 && errno == EINTR)
1091 continue;
1092 if (ret <= 0)
1093 break;
1094 /* Send an ack to let the sender know we got the sockets
1095 * and it can send some more
1096 */
1097 do {
1098 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
1099 } while (ret3 == -1 && errno == EINTR);
1100 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
1101 cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
1102 if (cmsg->cmsg_level == SOL_SOCKET &&
1103 cmsg->cmsg_type == SCM_RIGHTS) {
1104 size_t totlen = cmsg->cmsg_len -
1105 CMSG_LEN(0);
1106 if (totlen / sizeof(int) + got_fd > fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001107 ha_warning("Got to many sockets !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001108 goto out;
1109 }
1110 /*
1111 * Be paranoid and use memcpy() to avoid any
1112 * potential alignement issue.
1113 */
1114 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
1115 got_fd += totlen / sizeof(int);
1116 }
1117 }
1118 curoff += ret;
1119 } while (got_fd < fd_nb);
1120
1121 if (got_fd != fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001122 ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
1123 fd_nb, got_fd);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001124 goto out;
1125 }
1126 maxoff = curoff;
1127 curoff = 0;
1128 for (i = 0; i < got_fd; i++) {
1129 int fd = tmpfd[i];
1130 socklen_t socklen;
1131 int len;
1132
1133 xfer_sock = calloc(1, sizeof(*xfer_sock));
1134 if (!xfer_sock) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001135 ha_warning("Failed to allocate memory in get_old_sockets() !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001136 break;
1137 }
1138 xfer_sock->fd = -1;
1139
1140 socklen = sizeof(xfer_sock->addr);
1141 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001142 ha_warning("Failed to get socket address\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001143 free(xfer_sock);
Olivier Houchardbe7b1ce2017-07-17 17:25:33 +02001144 xfer_sock = NULL;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001145 continue;
1146 }
1147 if (curoff >= maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001148 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001149 goto out;
1150 }
1151 len = tmpbuf[curoff++];
1152 if (len > 0) {
1153 /* We have a namespace */
1154 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001155 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001156 goto out;
1157 }
1158 xfer_sock->namespace = malloc(len + 1);
1159 if (!xfer_sock->namespace) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001160 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001161 goto out;
1162 }
1163 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
1164 xfer_sock->namespace[len] = 0;
1165 curoff += len;
1166 }
1167 if (curoff >= maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001168 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001169 goto out;
1170 }
1171 len = tmpbuf[curoff++];
1172 if (len > 0) {
1173 /* We have an interface */
1174 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001175 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001176 goto out;
1177 }
1178 xfer_sock->iface = malloc(len + 1);
1179 if (!xfer_sock->iface) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001180 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001181 goto out;
1182 }
1183 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
Olivier Houchard33e083c2018-03-15 17:48:49 +01001184 xfer_sock->iface[len] = 0;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001185 curoff += len;
1186 }
1187 if (curoff + sizeof(int) > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001188 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001189 goto out;
1190 }
1191 memcpy(&xfer_sock->options, &tmpbuf[curoff],
1192 sizeof(xfer_sock->options));
1193 curoff += sizeof(xfer_sock->options);
1194
1195 xfer_sock->fd = fd;
1196 if (xfer_sock_list)
1197 xfer_sock_list->prev = xfer_sock;
1198 xfer_sock->next = xfer_sock_list;
1199 xfer_sock->prev = NULL;
1200 xfer_sock_list = xfer_sock;
1201 xfer_sock = NULL;
1202 }
1203
1204 ret2 = 0;
1205out:
1206 /* If we failed midway make sure to close the remaining
1207 * file descriptors
1208 */
1209 if (tmpfd != NULL && i < got_fd) {
1210 for (; i < got_fd; i++) {
1211 close(tmpfd[i]);
1212 }
1213 }
1214 free(tmpbuf);
1215 free(tmpfd);
1216 free(cmsgbuf);
1217 if (sock != -1)
1218 close(sock);
1219 if (xfer_sock) {
1220 free(xfer_sock->namespace);
1221 free(xfer_sock->iface);
1222 if (xfer_sock->fd != -1)
1223 close(xfer_sock->fd);
1224 free(xfer_sock);
1225 }
1226 return (ret2);
1227}
1228
Willy Tarreaubaaee002006-06-26 02:48:02 +02001229/*
William Lallemand73b85e72017-06-01 17:38:51 +02001230 * copy and cleanup the current argv
1231 * Remove the -sf /-st parameters
1232 * Return an allocated copy of argv
1233 */
1234
1235static char **copy_argv(int argc, char **argv)
1236{
1237 char **newargv;
William Lallemand2bf6d622017-06-20 11:20:23 +02001238 int i = 0, j = 0;
William Lallemand73b85e72017-06-01 17:38:51 +02001239
1240 newargv = calloc(argc + 2, sizeof(char *));
1241 if (newargv == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001242 ha_warning("Cannot allocate memory\n");
William Lallemand73b85e72017-06-01 17:38:51 +02001243 return NULL;
1244 }
1245
William Lallemand2bf6d622017-06-20 11:20:23 +02001246 while (i < argc) {
1247 /* -sf or -st or -x */
William Lallemand29f690c2018-01-09 23:12:27 +01001248 if (i > 0 && argv[i][0] == '-' &&
1249 ((argv[i][1] == 's' && (argv[i][2] == 'f' || argv[i][2] == 't')) || argv[i][1] == 'x' )) {
William Lallemand2bf6d622017-06-20 11:20:23 +02001250 /* list of pids to finish ('f') or terminate ('t') or unix socket (-x) */
William Lallemand73b85e72017-06-01 17:38:51 +02001251 i++;
1252 while (i < argc && argv[i][0] != '-') {
1253 i++;
1254 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001255 continue;
William Lallemand73b85e72017-06-01 17:38:51 +02001256 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001257
1258 newargv[j++] = argv[i++];
William Lallemand73b85e72017-06-01 17:38:51 +02001259 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001260
William Lallemand73b85e72017-06-01 17:38:51 +02001261 return newargv;
1262}
1263
1264/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001265 * This function initializes all the necessary variables. It only returns
1266 * if everything is OK. If something fails, it exits.
1267 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001268static void init(int argc, char **argv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001269{
Willy Tarreaubaaee002006-06-26 02:48:02 +02001270 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001271 char *tmp;
1272 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +02001273 int err_code = 0;
Maxime de Roucy0f503922016-05-13 23:52:55 +02001274 char *err_msg = NULL;
Willy Tarreau477ecd82010-01-03 21:12:30 +01001275 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +00001276 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +02001277 char *change_dir = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001278 struct proxy *px;
Willy Tarreaue6945732016-12-21 19:57:00 +01001279 struct post_check_fct *pcf;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001280
Christopher Faulete3a5e352017-10-24 13:53:54 +02001281 global.mode = MODE_STARTING;
William Lallemand73b85e72017-06-01 17:38:51 +02001282 next_argv = copy_argv(argc, argv);
1283
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001284 if (!init_trash_buffers(1)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001285 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet748919a2017-07-26 14:59:46 +02001286 exit(1);
1287 }
David du Colombier7af46052012-05-16 14:16:48 +02001288
Emeric Brun2b920a12010-09-23 18:30:22 +02001289 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
1290 * the string in case of truncation, and at least FreeBSD appears not to do
1291 * it.
1292 */
1293 memset(hostname, 0, sizeof(hostname));
1294 gethostname(hostname, sizeof(hostname) - 1);
1295 memset(localpeer, 0, sizeof(localpeer));
1296 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
William Lallemanddaf4cd22018-04-17 16:46:13 +02001297 setenv("HAPROXY_LOCALPEER", localpeer, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02001298
Willy Tarreaubaaee002006-06-26 02:48:02 +02001299 /*
1300 * Initialize the previously static variables.
1301 */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001302
Willy Tarreau173d9952018-01-26 21:48:23 +01001303 totalconn = actconn = listeners = stopping = 0;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001304 killed = 0;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001305
Willy Tarreaubaaee002006-06-26 02:48:02 +02001306
1307#ifdef HAPROXY_MEMMAX
Willy Tarreau70060452015-12-14 12:46:07 +01001308 global.rlimit_memmax_all = HAPROXY_MEMMAX;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001309#endif
1310
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001311 tzset();
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001312 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001313 start_date = now;
1314
Willy Tarreau84310e22014-02-14 11:59:04 +01001315 srandom(now_ms - getpid());
1316
Dragan Dosen835b9212016-02-12 13:23:03 +01001317 init_log();
Willy Tarreau29857942009-05-10 09:01:21 +02001318 signal_init();
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001319 if (init_acl() != 0)
1320 exit(1);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001321 init_task();
Willy Tarreau87b09662015-04-03 00:22:06 +02001322 init_stream();
Willy Tarreaub1ec8c42015-04-03 13:53:24 +02001323 init_session();
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001324 init_connection();
Willy Tarreau8280d642009-09-23 23:37:52 +02001325 /* warning, we init buffers later */
Willy Tarreaue4d7e552007-05-13 20:19:55 +02001326 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +01001327 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001328
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001329 /* Initialise lua. */
1330 hlua_init();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001331
Christopher Fauletff2613e2016-11-09 11:36:17 +01001332 /* Initialize process vars */
1333 vars_init(&global.vars, SCOPE_PROC);
1334
Willy Tarreau43b78992009-01-25 15:42:27 +01001335 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001336#if defined(ENABLE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001337 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001338#endif
1339#if defined(ENABLE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001340 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001341#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001342#if defined(ENABLE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +01001343 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001344#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001345#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001346 global.tune.options |= GTUNE_USE_SPLICE;
1347#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001348#if defined(USE_GETADDRINFO)
1349 global.tune.options |= GTUNE_USE_GAI;
1350#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001351#if defined(SO_REUSEPORT)
1352 global.tune.options |= GTUNE_USE_REUSEPORT;
1353#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001354
1355 pid = getpid();
1356 progname = *argv;
1357 while ((tmp = strchr(progname, '/')) != NULL)
1358 progname = tmp + 1;
1359
Kevinm48936af2010-12-22 16:08:21 +00001360 /* the process name is used for the logs only */
Dragan Dosen43885c72015-10-01 13:18:13 +02001361 chunk_initstr(&global.log_tag, strdup(progname));
Kevinm48936af2010-12-22 16:08:21 +00001362
Willy Tarreaubaaee002006-06-26 02:48:02 +02001363 argc--; argv++;
1364 while (argc > 0) {
1365 char *flag;
1366
1367 if (**argv == '-') {
1368 flag = *argv+1;
1369
1370 /* 1 arg */
1371 if (*flag == 'v') {
1372 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +01001373 if (flag[1] == 'v') /* -vv */
1374 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001375 exit(0);
1376 }
1377#if defined(ENABLE_EPOLL)
1378 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +01001379 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001380#endif
1381#if defined(ENABLE_POLL)
1382 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +01001383 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001384#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +02001385#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001386 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +01001387 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001388#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001389#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001390 else if (*flag == 'd' && flag[1] == 'S')
1391 global.tune.options &= ~GTUNE_USE_SPLICE;
1392#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001393#if defined(USE_GETADDRINFO)
1394 else if (*flag == 'd' && flag[1] == 'G')
1395 global.tune.options &= ~GTUNE_USE_GAI;
1396#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001397#if defined(SO_REUSEPORT)
1398 else if (*flag == 'd' && flag[1] == 'R')
1399 global.tune.options &= ~GTUNE_USE_REUSEPORT;
1400#endif
Emeric Brun850efd52014-01-29 12:24:34 +01001401 else if (*flag == 'd' && flag[1] == 'V')
1402 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001403 else if (*flag == 'V')
1404 arg_mode |= MODE_VERBOSE;
1405 else if (*flag == 'd' && flag[1] == 'b')
1406 arg_mode |= MODE_FOREGROUND;
Willy Tarreau6e064432012-05-08 15:40:42 +02001407 else if (*flag == 'd' && flag[1] == 'M')
1408 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreau3eed10e2016-11-07 21:03:16 +01001409 else if (*flag == 'd' && flag[1] == 'r')
1410 global.tune.options |= GTUNE_RESOLVE_DONTFAIL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001411 else if (*flag == 'd')
1412 arg_mode |= MODE_DEBUG;
1413 else if (*flag == 'c')
1414 arg_mode |= MODE_CHECK;
William Lallemand095ba4c2017-06-01 17:38:50 +02001415 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +02001416 arg_mode |= MODE_DAEMON;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001417 else if (*flag == 'W' && flag[1] == 's') {
Lukas Tribusf46bf952017-11-21 12:39:34 +01001418 arg_mode |= MODE_MWORKER | MODE_FOREGROUND;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001419#if defined(USE_SYSTEMD)
1420 global.tune.options |= GTUNE_USE_SYSTEMD;
1421#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001422 ha_alert("master-worker mode with systemd support (-Ws) requested, but not compiled. Use master-worker mode (-W) if you are not using Type=notify in your unit file or recompile with USE_SYSTEMD=1.\n\n");
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001423 usage(progname);
1424#endif
1425 }
William Lallemand095ba4c2017-06-01 17:38:50 +02001426 else if (*flag == 'W')
1427 arg_mode |= MODE_MWORKER;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001428 else if (*flag == 'q')
1429 arg_mode |= MODE_QUIET;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001430 else if (*flag == 'x') {
William Lallemand45eff442017-06-19 15:57:55 +02001431 if (argc <= 1 || argv[1][0] == '-') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001432 ha_alert("Unix socket path expected with the -x flag\n\n");
William Lallemand45eff442017-06-19 15:57:55 +02001433 usage(progname);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001434 }
William Lallemand4fc09692017-06-19 16:37:19 +02001435 if (old_unixsocket)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001436 ha_warning("-x option already set, overwriting the value\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001437 old_unixsocket = argv[1];
William Lallemand4fc09692017-06-19 16:37:19 +02001438
Olivier Houchardf73629d2017-04-05 22:33:04 +02001439 argv++;
1440 argc--;
1441 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001442 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1443 /* list of pids to finish ('f') or terminate ('t') */
1444
1445 if (flag[1] == 'f')
1446 oldpids_sig = SIGUSR1; /* finish then exit */
1447 else
1448 oldpids_sig = SIGTERM; /* terminate immediately */
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001449 while (argc > 1 && argv[1][0] != '-') {
Chris Lane236062f2018-02-05 23:15:44 +00001450 char * endptr = NULL;
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001451 oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int));
1452 if (!oldpids) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001453 ha_alert("Cannot allocate old pid : out of memory.\n");
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001454 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001455 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001456 argc--; argv++;
Chris Lane236062f2018-02-05 23:15:44 +00001457 errno = 0;
1458 oldpids[nb_oldpids] = strtol(*argv, &endptr, 10);
1459 if (errno) {
1460 ha_alert("-%2s option: failed to parse {%s}: %s\n",
1461 flag,
1462 *argv, strerror(errno));
1463 exit(1);
1464 } else if (endptr && strlen(endptr)) {
1465 while (isspace(*endptr)) endptr++;
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001466 if (*endptr != 0) {
Chris Lane236062f2018-02-05 23:15:44 +00001467 ha_alert("-%2s option: some bytes unconsumed in PID list {%s}\n",
1468 flag, endptr);
1469 exit(1);
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001470 }
Chris Lane236062f2018-02-05 23:15:44 +00001471 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001472 if (oldpids[nb_oldpids] <= 0)
1473 usage(progname);
1474 nb_oldpids++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001475 }
1476 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001477 else if (flag[0] == '-' && flag[1] == 0) { /* "--" */
1478 /* now that's a cfgfile list */
1479 argv++; argc--;
1480 while (argc > 0) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02001481 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001482 ha_alert("Cannot load configuration file/directory %s : %s\n",
1483 *argv,
1484 err_msg);
Willy Tarreaua088d312015-10-08 11:58:48 +02001485 exit(1);
1486 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001487 argv++; argc--;
1488 }
1489 break;
1490 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001491 else { /* >=2 args */
1492 argv++; argc--;
1493 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001494 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001495
1496 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +02001497 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001498 case 'n' : cfg_maxconn = atol(*argv); break;
Willy Tarreau70060452015-12-14 12:46:07 +01001499 case 'm' : global.rlimit_memmax_all = atol(*argv); break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001500 case 'N' : cfg_maxpconn = atol(*argv); break;
William Lallemanddaf4cd22018-04-17 16:46:13 +02001501 case 'L' :
1502 strncpy(localpeer, *argv, sizeof(localpeer) - 1);
1503 setenv("HAPROXY_LOCALPEER", localpeer, 1);
1504 break;
Willy Tarreau5d01a632009-06-22 16:02:30 +02001505 case 'f' :
Maxime de Roucy0f503922016-05-13 23:52:55 +02001506 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001507 ha_alert("Cannot load configuration file/directory %s : %s\n",
1508 *argv,
1509 err_msg);
Willy Tarreau5d01a632009-06-22 16:02:30 +02001510 exit(1);
1511 }
Willy Tarreau5d01a632009-06-22 16:02:30 +02001512 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001513 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001514 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001515 }
1516 }
1517 }
1518 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001519 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001520 argv++; argc--;
1521 }
1522
Christopher Faulete3a5e352017-10-24 13:53:54 +02001523 global.mode |= (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
1524 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001525
William Lallemandcb11fd22017-06-01 17:38:52 +02001526 /* Master workers wait mode */
1527 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_WAIT_ONLY") != NULL)) {
1528
1529 unsetenv("HAPROXY_MWORKER_WAIT_ONLY");
William Lallemandb3f2be32018-09-11 10:06:18 +02001530 mworker_loop();
William Lallemandcb11fd22017-06-01 17:38:52 +02001531 }
1532
1533 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {
1534 atexit_flag = 1;
1535 atexit(reexec_on_failure);
1536 }
1537
Willy Tarreau576132e2011-09-10 19:26:56 +02001538 if (change_dir && chdir(change_dir) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001539 ha_alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
Willy Tarreau576132e2011-09-10 19:26:56 +02001540 exit(1);
1541 }
1542
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001543 /* handle cfgfiles that are actualy directories */
1544 cfgfiles_expand_directories();
1545
1546 if (LIST_ISEMPTY(&cfg_cfgfiles))
1547 usage(progname);
1548
Willy Tarreaubaaee002006-06-26 02:48:02 +02001549 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001550
1551 init_default_instance();
1552
Willy Tarreau477ecd82010-01-03 21:12:30 +01001553 list_for_each_entry(wl, &cfg_cfgfiles, list) {
Willy Tarreauc4382422009-12-06 13:10:44 +01001554 int ret;
1555
Willy Tarreau477ecd82010-01-03 21:12:30 +01001556 ret = readcfgfile(wl->s);
Willy Tarreauc4382422009-12-06 13:10:44 +01001557 if (ret == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001558 ha_alert("Could not open configuration file %s : %s\n",
1559 wl->s, strerror(errno));
Willy Tarreauc4382422009-12-06 13:10:44 +01001560 exit(1);
1561 }
Willy Tarreau25a67fa2009-12-15 21:46:25 +01001562 if (ret & (ERR_ABORT|ERR_FATAL))
Christopher Faulet767a84b2017-11-24 16:50:31 +01001563 ha_alert("Error(s) found in configuration file : %s\n", wl->s);
Willy Tarreau25a67fa2009-12-15 21:46:25 +01001564 err_code |= ret;
Willy Tarreau058e9072009-07-20 09:30:05 +02001565 if (err_code & ERR_ABORT)
Willy Tarreau5d01a632009-06-22 16:02:30 +02001566 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001567 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001568
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02001569 /* do not try to resolve arguments nor to spot inconsistencies when
1570 * the configuration contains fatal errors caused by files not found
1571 * or failed memory allocations.
1572 */
1573 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001574 ha_alert("Fatal errors found in configuration.\n");
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02001575 exit(1);
1576 }
1577
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001578 pattern_finalize_config();
1579
Willy Tarreaubb925012009-07-23 13:36:36 +02001580 err_code |= check_config_validity();
1581 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001582 ha_alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001583 exit(1);
1584 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001585
Willy Tarreau70060452015-12-14 12:46:07 +01001586 /* recompute the amount of per-process memory depending on nbproc and
1587 * the shared SSL cache size (allowed to exist in all processes).
1588 */
1589 if (global.rlimit_memmax_all) {
1590#if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE)
1591 int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL;
1592
1593 global.rlimit_memmax =
1594 ((((int64_t)global.rlimit_memmax_all * 1048576LL) -
1595 ssl_cache_bytes) / global.nbproc +
1596 ssl_cache_bytes + 1048575LL) / 1048576LL;
1597#else
1598 global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc;
1599#endif
1600 }
1601
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001602#ifdef CONFIG_HAP_NS
1603 err_code |= netns_init();
1604 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001605 ha_alert("Failed to initialize namespace support.\n");
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001606 exit(1);
1607 }
1608#endif
1609
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01001610 /* Apply server states */
1611 apply_server_state();
1612
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001613 for (px = proxies_list; px; px = px->next)
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01001614 srv_compute_all_admin_states(px);
1615
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001616 /* Apply servers' configured address */
1617 err_code |= srv_init_addr();
1618 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001619 ha_alert("Failed to initialize server(s) addr.\n");
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001620 exit(1);
1621 }
1622
Willy Tarreaubaaee002006-06-26 02:48:02 +02001623 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001624 struct peers *pr;
1625 struct proxy *px;
1626
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001627 for (pr = cfg_peers; pr; pr = pr->next)
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001628 if (pr->peers_fe)
1629 break;
1630
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001631 for (px = proxies_list; px; px = px->next)
Willy Tarreau4348fad2012-09-20 16:48:07 +02001632 if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners))
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001633 break;
1634
1635 if (pr || px) {
1636 /* At least one peer or one listener has been found */
1637 qfprintf(stdout, "Configuration file is valid\n");
1638 exit(0);
1639 }
1640 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
1641 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001642 }
1643
Emeric Brunc60def82017-09-27 14:59:38 +02001644 global_listener_queue_task = task_new(MAX_THREADS_MASK);
Willy Tarreaue9b26022011-08-01 20:57:55 +02001645 if (!global_listener_queue_task) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001646 ha_alert("Out of memory when initializing global task\n");
Willy Tarreaue9b26022011-08-01 20:57:55 +02001647 exit(1);
1648 }
1649 /* very simple initialization, users will queue the task if needed */
1650 global_listener_queue_task->context = NULL; /* not even a context! */
1651 global_listener_queue_task->process = manage_global_listener_queue;
Willy Tarreaue9b26022011-08-01 20:57:55 +02001652
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001653 /* now we know the buffer size, we can initialize the channels and buffers */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001654 init_buffer();
Willy Tarreau8280d642009-09-23 23:37:52 +02001655
Willy Tarreaue6945732016-12-21 19:57:00 +01001656 list_for_each_entry(pcf, &post_check_list, list) {
1657 err_code |= pcf->fct();
1658 if (err_code & (ERR_ABORT|ERR_FATAL))
1659 exit(1);
1660 }
1661
Willy Tarreaubaaee002006-06-26 02:48:02 +02001662 if (cfg_maxconn > 0)
1663 global.maxconn = cfg_maxconn;
1664
1665 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +02001666 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001667 global.pidfile = strdup(cfg_pidfile);
1668 }
1669
Willy Tarreaud0256482015-01-15 21:45:22 +01001670 /* Now we want to compute the maxconn and possibly maxsslconn values.
1671 * It's a bit tricky. If memmax is not set, maxconn defaults to
1672 * DEFAULT_MAXCONN and maxsslconn defaults to DEFAULT_MAXSSLCONN.
1673 *
1674 * If memmax is set, then it depends on which values are set. If
1675 * maxsslconn is set, we use memmax to determine how many cleartext
1676 * connections may be added, and set maxconn to the sum of the two.
1677 * If maxconn is set and not maxsslconn, maxsslconn is computed from
1678 * the remaining amount of memory between memmax and the cleartext
1679 * connections. If neither are set, then it is considered that all
1680 * connections are SSL-capable, and maxconn is computed based on this,
1681 * then maxsslconn accordingly. We need to know if SSL is used on the
1682 * frontends, backends, or both, because when it's used on both sides,
1683 * we need twice the value for maxsslconn, but we only count the
1684 * handshake once since it is not performed on the two sides at the
1685 * same time (frontend-side is terminated before backend-side begins).
1686 * The SSL stack is supposed to have filled ssl_session_cost and
Willy Tarreau474b96a2015-01-28 19:03:21 +01001687 * ssl_handshake_cost during its initialization. In any case, if
1688 * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for
1689 * maxconn in order to protect the system.
Willy Tarreaud0256482015-01-15 21:45:22 +01001690 */
1691 if (!global.rlimit_memmax) {
1692 if (global.maxconn == 0) {
1693 global.maxconn = DEFAULT_MAXCONN;
1694 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1695 fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn);
1696 }
1697 }
1698#ifdef USE_OPENSSL
1699 else if (!global.maxconn && !global.maxsslconn &&
1700 (global.ssl_used_frontend || global.ssl_used_backend)) {
1701 /* memmax is set, compute everything automatically. Here we want
1702 * to ensure that all SSL connections will be served. We take
1703 * care of the number of sides where SSL is used, and consider
1704 * the worst case : SSL used on both sides and doing a handshake
1705 * simultaneously. Note that we can't have more than maxconn
1706 * handshakes at a time by definition, so for the worst case of
1707 * two SSL conns per connection, we count a single handshake.
1708 */
1709 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1710 int64_t mem = global.rlimit_memmax * 1048576ULL;
1711
1712 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1713 mem -= global.maxzlibmem;
1714 mem = mem * MEM_USABLE_RATIO;
1715
1716 global.maxconn = mem /
Willy Tarreau87b09662015-04-03 00:22:06 +02001717 ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream
Willy Tarreaud0256482015-01-15 21:45:22 +01001718 sides * global.ssl_session_max_cost + // SSL buffers, one per side
1719 global.ssl_handshake_max_cost); // 1 handshake per connection max
1720
1721 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001722#ifdef SYSTEM_MAXCONN
1723 if (global.maxconn > DEFAULT_MAXCONN)
1724 global.maxconn = DEFAULT_MAXCONN;
1725#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001726 global.maxsslconn = sides * global.maxconn;
1727 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1728 fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n",
1729 global.maxconn, global.maxsslconn);
1730 }
1731 else if (!global.maxsslconn &&
1732 (global.ssl_used_frontend || global.ssl_used_backend)) {
1733 /* memmax and maxconn are known, compute maxsslconn automatically.
1734 * maxsslconn being forced, we don't know how many of it will be
1735 * on each side if both sides are being used. The worst case is
1736 * when all connections use only one SSL instance because
1737 * handshakes may be on two sides at the same time.
1738 */
1739 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1740 int64_t mem = global.rlimit_memmax * 1048576ULL;
1741 int64_t sslmem;
1742
1743 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1744 mem -= global.maxzlibmem;
1745 mem = mem * MEM_USABLE_RATIO;
1746
Willy Tarreau87b09662015-04-03 00:22:06 +02001747 sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001748 global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost);
1749 global.maxsslconn = round_2dig(global.maxsslconn);
1750
1751 if (sslmem <= 0 || global.maxsslconn < sides) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001752 ha_alert("Cannot compute the automatic maxsslconn because global.maxconn is already too "
1753 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1754 "without SSL is %d, but %d was found and SSL is in use.\n",
1755 global.rlimit_memmax,
1756 (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)),
1757 global.maxconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01001758 exit(1);
1759 }
1760
1761 if (global.maxsslconn > sides * global.maxconn)
1762 global.maxsslconn = sides * global.maxconn;
1763
1764 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1765 fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn);
1766 }
1767#endif
1768 else if (!global.maxconn) {
1769 /* memmax and maxsslconn are known/unused, compute maxconn automatically */
1770 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1771 int64_t mem = global.rlimit_memmax * 1048576ULL;
1772 int64_t clearmem;
1773
1774 if (global.ssl_used_frontend || global.ssl_used_backend)
1775 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1776
1777 mem -= global.maxzlibmem;
1778 mem = mem * MEM_USABLE_RATIO;
1779
1780 clearmem = mem;
1781 if (sides)
1782 clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn;
1783
Willy Tarreau87b09662015-04-03 00:22:06 +02001784 global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001785 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001786#ifdef SYSTEM_MAXCONN
1787 if (global.maxconn > DEFAULT_MAXCONN)
1788 global.maxconn = DEFAULT_MAXCONN;
1789#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001790
1791 if (clearmem <= 0 || !global.maxconn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001792 ha_alert("Cannot compute the automatic maxconn because global.maxsslconn is already too "
1793 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1794 "is %d, but %d was found.\n",
1795 global.rlimit_memmax,
1796 (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)),
1797 global.maxsslconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01001798 exit(1);
1799 }
1800
1801 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
1802 if (sides && global.maxsslconn > sides * global.maxconn) {
1803 fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn "
1804 "to be limited to %d. Better reduce global.maxsslconn to get more "
1805 "room for extra connections.\n", global.maxsslconn, global.maxconn);
1806 }
1807 fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn);
1808 }
1809 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001810
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001811 if (!global.maxpipes) {
1812 /* maxpipes not specified. Count how many frontends and backends
1813 * may be using splicing, and bound that to maxconn.
1814 */
1815 struct proxy *cur;
1816 int nbfe = 0, nbbe = 0;
1817
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001818 for (cur = proxies_list; cur; cur = cur->next) {
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001819 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
1820 if (cur->cap & PR_CAP_FE)
1821 nbfe += cur->maxconn;
1822 if (cur->cap & PR_CAP_BE)
Willy Tarreauafb48762009-01-25 10:42:05 +01001823 nbbe += cur->fullconn ? cur->fullconn : global.maxconn;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001824 }
1825 }
1826 global.maxpipes = MAX(nbfe, nbbe);
1827 if (global.maxpipes > global.maxconn)
1828 global.maxpipes = global.maxconn;
Willy Tarreau686ac822009-01-25 14:06:58 +01001829 global.maxpipes /= 4;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001830 }
1831
1832
Willy Tarreauabacc2c2011-09-07 14:26:33 +02001833 global.hardmaxconn = global.maxconn; /* keep this max value */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001834 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
Willy Tarreau3ec79b92009-01-18 20:39:42 +01001835 global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */
Emeric Brunece0c332017-12-06 13:51:49 +01001836 /* compute fd used by async engines */
1837 if (global.ssl_used_async_engines) {
1838 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1839 global.maxsock += global.maxconn * sides * global.ssl_used_async_engines;
1840 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001841
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001842 if (global.stats_fe)
1843 global.maxsock += global.stats_fe->maxconn;
1844
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001845 if (cfg_peers) {
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001846 /* peers also need to bypass global maxconn */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001847 struct peers *p = cfg_peers;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001848
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001849 for (p = cfg_peers; p; p = p->next)
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001850 if (p->peers_fe)
1851 global.maxsock += p->peers_fe->maxconn;
1852 }
1853
Willy Tarreau1db37712007-06-03 17:16:49 +02001854 if (global.tune.maxpollevents <= 0)
1855 global.tune.maxpollevents = MAX_POLL_EVENTS;
1856
Olivier Houchard1599b802018-05-24 18:59:04 +02001857 if (global.tune.runqueue_depth <= 0)
1858 global.tune.runqueue_depth = RUNQUEUE_DEPTH;
1859
Willy Tarreau6f4a82c2009-03-21 20:43:57 +01001860 if (global.tune.recv_enough == 0)
1861 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
1862
Willy Tarreau27097842015-09-28 13:53:23 +02001863 if (global.tune.maxrewrite < 0)
1864 global.tune.maxrewrite = MAXREWRITE;
1865
Willy Tarreau27a674e2009-08-17 07:23:33 +02001866 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
1867 global.tune.maxrewrite = global.tune.bufsize / 2;
1868
Willy Tarreaubaaee002006-06-26 02:48:02 +02001869 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
1870 /* command line debug mode inhibits configuration mode */
William Lallemand095ba4c2017-06-01 17:38:50 +02001871 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001872 global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND));
1873 }
1874
William Lallemand095ba4c2017-06-01 17:38:50 +02001875 if (arg_mode & MODE_DAEMON) {
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001876 /* command line daemon mode inhibits foreground and debug modes mode */
1877 global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND);
William Lallemand095ba4c2017-06-01 17:38:50 +02001878 global.mode |= arg_mode & MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001879 }
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001880
1881 global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001882
William Lallemand095ba4c2017-06-01 17:38:50 +02001883 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001884 ha_warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
William Lallemand095ba4c2017-06-01 17:38:50 +02001885 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001886 }
1887
William Lallemand095ba4c2017-06-01 17:38:50 +02001888 if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001889 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
Christopher Faulet767a84b2017-11-24 16:50:31 +01001890 ha_warning("<nbproc> is only meaningful in daemon mode or master-worker mode. Setting limit to 1 process.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001891 global.nbproc = 1;
1892 }
1893
1894 if (global.nbproc < 1)
1895 global.nbproc = 1;
1896
Christopher Fauletbe0faa22017-08-29 15:37:10 +02001897 if (global.nbthread < 1)
1898 global.nbthread = 1;
1899
Christopher Faulet3ef26392017-08-29 16:46:57 +02001900 /* Realloc trash buffers because global.tune.bufsize may have changed */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001901 if (!init_trash_buffers(0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001902 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet3ef26392017-08-29 16:46:57 +02001903 exit(1);
1904 }
1905
Christopher Faulet96d44832017-11-14 22:02:30 +01001906 if (!init_log_buffers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001907 ha_alert("failed to initialize log buffers.\n");
Christopher Faulet96d44832017-11-14 22:02:30 +01001908 exit(1);
1909 }
1910
Willy Tarreauef1d1f82007-04-16 00:25:25 +02001911 /*
1912 * Note: we could register external pollers here.
1913 * Built-in pollers have been registered before main().
1914 */
Willy Tarreau4f60f162007-04-08 16:39:58 +02001915
Willy Tarreau43b78992009-01-25 15:42:27 +01001916 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001917 disable_poller("kqueue");
1918
Willy Tarreau43b78992009-01-25 15:42:27 +01001919 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001920 disable_poller("epoll");
1921
Willy Tarreau43b78992009-01-25 15:42:27 +01001922 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001923 disable_poller("poll");
1924
Willy Tarreau43b78992009-01-25 15:42:27 +01001925 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001926 disable_poller("select");
1927
1928 /* Note: we could disable any poller by name here */
1929
Christopher Fauletb3f4e142016-03-07 12:46:38 +01001930 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +02001931 list_pollers(stderr);
Christopher Fauletb3f4e142016-03-07 12:46:38 +01001932 fprintf(stderr, "\n");
1933 list_filters(stderr);
1934 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02001935
Willy Tarreau4f60f162007-04-08 16:39:58 +02001936 if (!init_pollers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001937 ha_alert("No polling mechanism available.\n"
1938 " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n"
1939 " is too low on this platform to support maxconn and the number of listeners\n"
1940 " and servers. You should rebuild haproxy specifying your system using TARGET=\n"
1941 " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n"
1942 " global maxconn setting to accommodate the system's limitation. For reference,\n"
1943 " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n"
1944 " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n"
1945 " check build settings using 'haproxy -vv'.\n\n",
1946 FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001947 exit(1);
1948 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02001949 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
1950 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001951 }
1952
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001953 if (!global.node)
1954 global.node = strdup(hostname);
1955
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01001956 if (!hlua_post_init())
1957 exit(1);
Thomas Holmes6abded42015-05-12 16:23:58 +01001958
Maxime de Roucy0f503922016-05-13 23:52:55 +02001959 free(err_msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001960}
1961
Simon Horman6fb82592011-07-15 13:14:11 +09001962static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +09001963{
Simon Hormanac821422011-07-15 13:14:09 +09001964 struct acl_term_suite *suite, *suiteb;
1965 struct acl_term *term, *termb;
1966
Simon Horman6fb82592011-07-15 13:14:11 +09001967 if (!cond)
1968 return;
1969
1970 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
1971 list_for_each_entry_safe(term, termb, &suite->terms, list) {
1972 LIST_DEL(&term->list);
1973 free(term);
Simon Hormanac821422011-07-15 13:14:09 +09001974 }
Simon Horman6fb82592011-07-15 13:14:11 +09001975 LIST_DEL(&suite->list);
1976 free(suite);
1977 }
1978
1979 free(cond);
1980}
1981
1982static void deinit_tcp_rules(struct list *rules)
1983{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001984 struct act_rule *trule, *truleb;
Simon Horman6fb82592011-07-15 13:14:11 +09001985
1986 list_for_each_entry_safe(trule, truleb, rules, list) {
Simon Hormanac821422011-07-15 13:14:09 +09001987 LIST_DEL(&trule->list);
Simon Horman6fb82592011-07-15 13:14:11 +09001988 deinit_acl_cond(trule->cond);
Simon Hormanac821422011-07-15 13:14:09 +09001989 free(trule);
1990 }
1991}
1992
Simon Horman6fb82592011-07-15 13:14:11 +09001993static void deinit_stick_rules(struct list *rules)
1994{
1995 struct sticking_rule *rule, *ruleb;
1996
1997 list_for_each_entry_safe(rule, ruleb, rules, list) {
1998 LIST_DEL(&rule->list);
1999 deinit_acl_cond(rule->cond);
Christopher Faulet476e5d02016-10-26 11:34:47 +02002000 release_sample_expr(rule->expr);
Simon Horman6fb82592011-07-15 13:14:11 +09002001 free(rule);
2002 }
2003}
2004
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002005void deinit(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002006{
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002007 struct proxy *p = proxies_list, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002008 struct cap_hdr *h,*h_next;
2009 struct server *s,*s_next;
2010 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002011 struct acl_cond *cond, *condb;
2012 struct hdr_exp *exp, *expb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002013 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002014 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002015 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002016 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002017 struct wordlist *wl, *wlb;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002018 struct cond_wordlist *cwl, *cwlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002019 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +02002020 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +01002021 struct logformat_node *lf, *lfb;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002022 struct bind_conf *bind_conf, *bind_back;
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002023 struct build_opts_str *bol, *bolb;
Willy Tarreau05554e62016-12-21 20:46:26 +01002024 struct post_deinit_fct *pdf;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002025 int i;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002026
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002027 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002028 while (p) {
Willy Tarreau8113a5d2012-10-04 08:01:43 +02002029 free(p->conf.file);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002030 free(p->id);
2031 free(p->check_req);
2032 free(p->cookie_name);
2033 free(p->cookie_domain);
2034 free(p->url_param_name);
2035 free(p->capture_name);
2036 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +09002037 free(p->rdp_cookie_name);
Willy Tarreau62a61232013-04-12 18:13:46 +02002038 if (p->conf.logformat_string != default_http_log_format &&
2039 p->conf.logformat_string != default_tcp_log_format &&
2040 p->conf.logformat_string != clf_http_log_format)
2041 free(p->conf.logformat_string);
Willy Tarreau196729e2012-05-31 19:30:26 +02002042
Willy Tarreau62a61232013-04-12 18:13:46 +02002043 free(p->conf.lfs_file);
2044 free(p->conf.uniqueid_format_string);
2045 free(p->conf.uif_file);
Godbachaf1a75d2013-10-02 17:10:11 +08002046 free(p->lbprm.map.srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002047
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002048 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
2049 free(p->conf.logformat_sd_string);
2050 free(p->conf.lfsd_file);
2051
Willy Tarreaua534fea2008-08-03 12:19:50 +02002052 for (i = 0; i < HTTP_ERR_SIZE; i++)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002053 chunk_destroy(&p->errmsg[i]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002054
Willy Tarreauf4f04122010-01-28 18:10:50 +01002055 list_for_each_entry_safe(cwl, cwlb, &p->req_add, list) {
2056 LIST_DEL(&cwl->list);
2057 free(cwl->s);
2058 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002059 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002060
Willy Tarreauf4f04122010-01-28 18:10:50 +01002061 list_for_each_entry_safe(cwl, cwlb, &p->rsp_add, list) {
2062 LIST_DEL(&cwl->list);
2063 free(cwl->s);
2064 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002065 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002066
Willy Tarreaub80c2302007-11-30 20:51:32 +01002067 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
2068 LIST_DEL(&cond->list);
2069 prune_acl_cond(cond);
2070 free(cond);
2071 }
2072
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002073 for (exp = p->req_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002074 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002075 regex_free(exp->preg);
2076 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002077 }
2078
Willy Tarreau98d04852015-05-26 12:18:29 +02002079 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002080 expb = exp;
2081 exp = exp->next;
2082 free(expb);
2083 }
2084
2085 for (exp = p->rsp_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002086 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002087 regex_free(exp->preg);
2088 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002089 }
2090
Willy Tarreau98d04852015-05-26 12:18:29 +02002091 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002092 expb = exp;
2093 exp = exp->next;
2094 free(expb);
2095 }
2096
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002097 /* build a list of unique uri_auths */
2098 if (!ua)
2099 ua = p->uri_auth;
2100 else {
2101 /* check if p->uri_auth is unique */
2102 for (uap = ua; uap; uap=uap->next)
2103 if (uap == p->uri_auth)
2104 break;
2105
Willy Tarreauaccc4e12008-06-24 11:14:45 +02002106 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002107 /* add it, if it is */
2108 p->uri_auth->next = ua;
2109 ua = p->uri_auth;
2110 }
2111 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002112
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002113 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
2114 LIST_DEL(&acl->list);
2115 prune_acl(acl);
2116 free(acl);
2117 }
2118
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002119 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
2120 LIST_DEL(&srule->list);
2121 prune_acl_cond(srule->cond);
2122 free(srule->cond);
2123 free(srule);
2124 }
2125
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002126 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
2127 LIST_DEL(&rule->list);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002128 if (rule->cond) {
2129 prune_acl_cond(rule->cond);
2130 free(rule->cond);
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +01002131 free(rule->file);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002132 }
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002133 free(rule);
2134 }
2135
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002136 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
2137 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +01002138 if (rdr->cond) {
2139 prune_acl_cond(rdr->cond);
2140 free(rdr->cond);
2141 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002142 free(rdr->rdr_str);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002143 list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) {
2144 LIST_DEL(&lf->list);
2145 free(lf);
2146 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002147 free(rdr);
2148 }
2149
William Lallemand0f99e342011-10-12 17:50:54 +02002150 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
2151 LIST_DEL(&log->list);
2152 free(log);
2153 }
2154
William Lallemand723b73a2012-02-08 16:37:49 +01002155 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
2156 LIST_DEL(&lf->list);
2157 free(lf);
2158 }
2159
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002160 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
2161 LIST_DEL(&lf->list);
2162 free(lf);
2163 }
2164
Simon Hormanac821422011-07-15 13:14:09 +09002165 deinit_tcp_rules(&p->tcp_req.inspect_rules);
2166 deinit_tcp_rules(&p->tcp_req.l4_rules);
2167
Simon Horman6fb82592011-07-15 13:14:11 +09002168 deinit_stick_rules(&p->storersp_rules);
2169 deinit_stick_rules(&p->sticking_rules);
2170
Willy Tarreaubaaee002006-06-26 02:48:02 +02002171 h = p->req_cap;
2172 while (h) {
2173 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002174 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002175 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002176 free(h);
2177 h = h_next;
2178 }/* end while(h) */
2179
2180 h = p->rsp_cap;
2181 while (h) {
2182 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002183 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002184 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002185 free(h);
2186 h = h_next;
2187 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002188
Willy Tarreaubaaee002006-06-26 02:48:02 +02002189 s = p->srv;
2190 while (s) {
2191 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002192
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002193 if (s->check.task) {
2194 task_delete(s->check.task);
2195 task_free(s->check.task);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002196 }
Simon Hormand60d6912013-11-25 10:46:36 +09002197 if (s->agent.task) {
2198 task_delete(s->agent.task);
2199 task_free(s->agent.task);
2200 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002201
Willy Tarreau2e993902011-10-31 11:53:20 +01002202 if (s->warmup) {
2203 task_delete(s->warmup);
2204 task_free(s->warmup);
2205 }
2206
Willy Tarreaua534fea2008-08-03 12:19:50 +02002207 free(s->id);
2208 free(s->cookie);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002209 free(s->check.bi.area);
2210 free(s->check.bo.area);
2211 free(s->agent.bi.area);
2212 free(s->agent.bo.area);
James Brown55f9ff12015-10-21 18:19:05 -07002213 free(s->agent.send_string);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002214 free(s->hostname_dn);
Sárközi, László34c01792014-09-05 10:08:23 +02002215 free((char*)s->conf.file);
Willy Tarreau17d45382016-12-22 21:16:08 +01002216
2217 if (s->use_ssl || s->check.use_ssl) {
2218 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2219 xprt_get(XPRT_SSL)->destroy_srv(s);
2220 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002221 HA_SPIN_DESTROY(&s->lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002222 free(s);
2223 s = s_next;
2224 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002225
Willy Tarreau4348fad2012-09-20 16:48:07 +02002226 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002227 /*
2228 * Zombie proxy, the listener just pretend to be up
2229 * because they still hold an opened fd.
2230 * Close it and give the listener its real state.
2231 */
2232 if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
2233 close(l->fd);
2234 l->state = LI_INIT;
2235 }
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02002236 unbind_listener(l);
2237 delete_listener(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002238 LIST_DEL(&l->by_fe);
2239 LIST_DEL(&l->by_bind);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002240 free(l->name);
2241 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002242 free(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002243 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002244
Willy Tarreau4348fad2012-09-20 16:48:07 +02002245 /* Release unused SSL configs. */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002246 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01002247 if (bind_conf->xprt->destroy_bind_conf)
2248 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002249 free(bind_conf->file);
2250 free(bind_conf->arg);
2251 LIST_DEL(&bind_conf->by_fe);
2252 free(bind_conf);
2253 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02002254
Christopher Fauletd7c91962015-04-30 11:48:27 +02002255 flt_deinit(p);
2256
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002257 free(p->desc);
2258 free(p->fwdfor_hdr_name);
2259
Willy Tarreauff011f22011-01-06 17:51:27 +01002260 free_http_req_rules(&p->http_req_rules);
Sasha Pachev218f0642014-06-16 12:05:59 -06002261 free_http_res_rules(&p->http_res_rules);
Willy Tarreau1f89b182017-11-22 16:53:53 +01002262 task_free(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002263
Willy Tarreaubafbe012017-11-24 17:34:44 +01002264 pool_destroy(p->req_cap_pool);
2265 pool_destroy(p->rsp_cap_pool);
2266 pool_destroy(p->table.pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002267
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002268 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002269 p = p->next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002270 HA_SPIN_DESTROY(&p0->lbprm.lock);
2271 HA_SPIN_DESTROY(&p0->lock);
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002272 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002273 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02002274
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002275 while (ua) {
2276 uap = ua;
2277 ua = ua->next;
2278
Willy Tarreaua534fea2008-08-03 12:19:50 +02002279 free(uap->uri_prefix);
2280 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002281 free(uap->node);
2282 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002283
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002284 userlist_free(uap->userlist);
Willy Tarreauff011f22011-01-06 17:51:27 +01002285 free_http_req_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002286
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002287 free(uap);
2288 }
2289
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002290 userlist_free(userlist);
2291
David Carlier834cb2e2015-09-25 12:02:25 +01002292 cfg_unregister_sections();
2293
Christopher Faulet0132d062017-07-26 15:33:35 +02002294 deinit_log_buffers();
Christopher Faulet748919a2017-07-26 14:59:46 +02002295 deinit_trash_buffers();
David Carlier834cb2e2015-09-25 12:02:25 +01002296
Willy Tarreaudd815982007-10-16 12:25:14 +02002297 protocol_unbind_all();
2298
Willy Tarreau05554e62016-12-21 20:46:26 +01002299 list_for_each_entry(pdf, &post_deinit_list, list)
2300 pdf->fct();
2301
Joe Williamsdf5b38f2010-12-29 17:05:48 +01002302 free(global.log_send_hostname); global.log_send_hostname = NULL;
Dragan Dosen43885c72015-10-01 13:18:13 +02002303 chunk_destroy(&global.log_tag);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002304 free(global.chroot); global.chroot = NULL;
2305 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002306 free(global.node); global.node = NULL;
2307 free(global.desc); global.desc = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002308 free(oldpids); oldpids = NULL;
Willy Tarreau1f89b182017-11-22 16:53:53 +01002309 task_free(global_listener_queue_task); global_listener_queue_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002310
William Lallemand0f99e342011-10-12 17:50:54 +02002311 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
2312 LIST_DEL(&log->list);
2313 free(log);
2314 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01002315 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02002316 free(wl->s);
Willy Tarreau477ecd82010-01-03 21:12:30 +01002317 LIST_DEL(&wl->list);
2318 free(wl);
2319 }
2320
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002321 list_for_each_entry_safe(bol, bolb, &build_opts_list, list) {
2322 if (bol->must_free)
2323 free((void *)bol->str);
2324 LIST_DEL(&bol->list);
2325 free(bol);
2326 }
2327
Christopher Fauletff2613e2016-11-09 11:36:17 +01002328 vars_prune(&global.vars, NULL, NULL);
2329
Christopher Fauletad405f12017-08-29 15:30:11 +02002330 deinit_buffer();
2331
Willy Tarreaubafbe012017-11-24 17:34:44 +01002332 pool_destroy(pool_head_stream);
2333 pool_destroy(pool_head_session);
2334 pool_destroy(pool_head_connection);
2335 pool_destroy(pool_head_connstream);
2336 pool_destroy(pool_head_requri);
2337 pool_destroy(pool_head_task);
2338 pool_destroy(pool_head_capture);
2339 pool_destroy(pool_head_pendconn);
2340 pool_destroy(pool_head_sig_handlers);
2341 pool_destroy(pool_head_hdr_idx);
2342 pool_destroy(pool_head_http_txn);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002343 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002344} /* end deinit() */
2345
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002346void mworker_pipe_handler(int fd)
2347{
2348 char c;
2349
2350 while (read(fd, &c, 1) == -1) {
2351 if (errno == EINTR)
2352 continue;
2353 if (errno == EAGAIN) {
2354 fd_cant_recv(fd);
2355 return;
2356 }
2357 break;
2358 }
2359
2360 deinit();
2361 exit(EXIT_FAILURE);
2362 return;
2363}
2364
Willy Tarreau1605c7a2018-01-23 19:01:49 +01002365/* should only be called once per process */
2366void mworker_pipe_register()
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002367{
Willy Tarreau1605c7a2018-01-23 19:01:49 +01002368 if (fdtab[mworker_pipe[0]].owner)
2369 /* already initialized */
2370 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002371
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002372 fcntl(mworker_pipe[0], F_SETFL, O_NONBLOCK);
Willy Tarreaua9786b62018-01-25 07:22:13 +01002373 fd_insert(mworker_pipe[0], mworker_pipe, mworker_pipe_handler, MAX_THREADS_MASK);
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002374 fd_want_recv(mworker_pipe[0]);
2375}
Christopher Fauletdc628a32017-10-19 11:59:44 +02002376
Willy Tarreau918ff602011-07-25 16:33:49 +02002377/* Runs the polling loop */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01002378static void run_poll_loop()
Willy Tarreau4f60f162007-04-08 16:39:58 +02002379{
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002380 int next, exp;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002381
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002382 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002383 while (1) {
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002384 /* Process a few tasks */
2385 process_runnable_tasks();
2386
William Lallemand1aab50b2018-06-07 09:46:01 +02002387 /* check if we caught some signals and process them in the
2388 first thread */
2389 if (tid == 0)
2390 signal_process_queue();
Willy Tarreau29857942009-05-10 09:01:21 +02002391
Willy Tarreau58b458d2008-06-29 22:40:23 +02002392 /* Check if we can expire some tasks */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002393 next = wake_expired_tasks();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002394
Willy Tarreau85c459d2018-08-02 10:54:31 +02002395 /* stop when there's nothing left to do */
2396 if (jobs == 0)
2397 break;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002398
Willy Tarreau10146c92015-04-13 20:44:19 +02002399 /* expire immediately if events are pending */
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002400 exp = now_ms;
Christopher Faulet32467fe2018-01-15 12:16:34 +01002401 if (fd_cache_mask & tid_bit)
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002402 activity[tid].wake_cache++;
2403 else if (active_tasks_mask & tid_bit)
2404 activity[tid].wake_tasks++;
William Lallemand1aab50b2018-06-07 09:46:01 +02002405 else if (signal_queue_len && tid == 0)
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002406 activity[tid].wake_signal++;
Olivier Houchard79321b92018-07-26 17:55:11 +02002407 else {
2408 HA_ATOMIC_OR(&sleeping_thread_mask, tid_bit);
2409 __ha_barrier_store();
2410 if (active_tasks_mask & tid_bit) {
2411 activity[tid].wake_tasks++;
2412 HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
2413 } else
2414 exp = next;
2415 }
Willy Tarreau10146c92015-04-13 20:44:19 +02002416
Willy Tarreau58b458d2008-06-29 22:40:23 +02002417 /* The poller will ensure it returns around <next> */
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002418 cur_poller.poll(&cur_poller, exp);
Olivier Houchard79321b92018-07-26 17:55:11 +02002419 if (sleeping_thread_mask & tid_bit)
2420 HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
Willy Tarreau033cd9d2014-01-25 19:24:15 +01002421 fd_process_cached_events();
Emeric Brun64cc49c2017-10-03 14:46:45 +02002422
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002423 activity[tid].loops++;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002424 }
2425}
2426
Christopher Faulet1d17c102017-08-29 15:38:48 +02002427static void *run_thread_poll_loop(void *data)
2428{
2429 struct per_thread_init_fct *ptif;
2430 struct per_thread_deinit_fct *ptdf;
Christopher Fauletda18b9d2018-01-25 16:10:16 +01002431 __decl_hathreads(static HA_SPINLOCK_T start_lock);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002432
Willy Tarreau0c026f42018-08-01 19:12:20 +02002433 ha_set_tid(*((unsigned int *)data));
Christopher Faulet1d17c102017-08-29 15:38:48 +02002434 tv_update_date(-1,-1);
2435
2436 list_for_each_entry(ptif, &per_thread_init_list, list) {
2437 if (!ptif->fct()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002438 ha_alert("failed to initialize thread %u.\n", tid);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002439 exit(1);
2440 }
2441 }
2442
Willy Tarreau1605c7a2018-01-23 19:01:49 +01002443 if (global.mode & MODE_MWORKER) {
2444 HA_SPIN_LOCK(START_LOCK, &start_lock);
2445 mworker_pipe_register();
2446 HA_SPIN_UNLOCK(START_LOCK, &start_lock);
2447 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002448
2449 protocol_enable_all();
Christopher Faulet1d17c102017-08-29 15:38:48 +02002450 run_poll_loop();
2451
2452 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
2453 ptdf->fct();
2454
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002455#ifdef USE_THREAD
William Lallemand091d8272018-06-24 09:37:03 +02002456 HA_ATOMIC_AND(&all_threads_mask, ~tid_bit);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002457 if (tid > 0)
2458 pthread_exit(NULL);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002459#endif
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002460 return NULL;
2461}
Christopher Faulet1d17c102017-08-29 15:38:48 +02002462
Willy Tarreaue9b26022011-08-01 20:57:55 +02002463/* This is the global management task for listeners. It enables listeners waiting
2464 * for global resources when there are enough free resource, or at least once in
2465 * a while. It is designed to be called as a task.
2466 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002467static struct task *manage_global_listener_queue(struct task *t, void *context, unsigned short state)
Willy Tarreaue9b26022011-08-01 20:57:55 +02002468{
2469 int next = TICK_ETERNITY;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002470 /* queue is empty, nothing to do */
2471 if (LIST_ISEMPTY(&global_listener_queue))
2472 goto out;
2473
2474 /* If there are still too many concurrent connections, let's wait for
2475 * some of them to go away. We don't need to re-arm the timer because
2476 * each of them will scan the queue anyway.
2477 */
2478 if (unlikely(actconn >= global.maxconn))
2479 goto out;
2480
2481 /* We should periodically try to enable listeners waiting for a global
2482 * resource here, because it is possible, though very unlikely, that
2483 * they have been blocked by a temporary lack of global resource such
2484 * as a file descriptor or memory and that the temporary condition has
2485 * disappeared.
2486 */
Willy Tarreauabacc2c2011-09-07 14:26:33 +02002487 dequeue_all_listeners(&global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +02002488
2489 out:
2490 t->expire = next;
2491 task_queue(t);
2492 return t;
2493}
Willy Tarreau4f60f162007-04-08 16:39:58 +02002494
Willy Tarreaubaaee002006-06-26 02:48:02 +02002495int main(int argc, char **argv)
2496{
2497 int err, retry;
2498 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02002499 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02002500 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002501
Olivier Houchard5fa300d2018-02-03 15:15:21 +01002502 setvbuf(stdout, NULL, _IONBF, 0);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002503 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002504 signal_register_fct(SIGQUIT, dump, SIGQUIT);
2505 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
2506 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
William Lallemand73b85e72017-06-01 17:38:51 +02002507 signal_register_fct(SIGUSR2, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002508
Willy Tarreaue437c442010-03-17 18:02:46 +01002509 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
2510 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
2511 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002512 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002513 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002514
Willy Tarreaudc23a922011-02-16 11:10:36 +01002515 /* ulimits */
2516 if (!global.rlimit_nofile)
2517 global.rlimit_nofile = global.maxsock;
2518
2519 if (global.rlimit_nofile) {
2520 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
2521 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
Willy Tarreauef635472016-06-21 11:48:18 +02002522 /* try to set it to the max possible at least */
2523 getrlimit(RLIMIT_NOFILE, &limit);
Willy Tarreau164dd0b2016-06-21 11:51:59 +02002524 limit.rlim_cur = limit.rlim_max;
2525 if (setrlimit(RLIMIT_NOFILE, &limit) != -1)
2526 getrlimit(RLIMIT_NOFILE, &limit);
2527
Christopher Faulet767a84b2017-11-24 16:50:31 +01002528 ha_warning("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n", argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
Willy Tarreauef635472016-06-21 11:48:18 +02002529 global.rlimit_nofile = limit.rlim_cur;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002530 }
2531 }
2532
2533 if (global.rlimit_memmax) {
2534 limit.rlim_cur = limit.rlim_max =
Willy Tarreau70060452015-12-14 12:46:07 +01002535 global.rlimit_memmax * 1048576ULL;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002536#ifdef RLIMIT_AS
2537 if (setrlimit(RLIMIT_AS, &limit) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002538 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2539 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01002540 }
2541#else
2542 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002543 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2544 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01002545 }
2546#endif
2547 }
2548
Olivier Houchardf73629d2017-04-05 22:33:04 +02002549 if (old_unixsocket) {
William Lallemand85b0bd92017-06-01 17:38:53 +02002550 if (strcmp("/dev/null", old_unixsocket) != 0) {
2551 if (get_old_sockets(old_unixsocket) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002552 ha_alert("Failed to get the sockets from the old process!\n");
William Lallemand85b0bd92017-06-01 17:38:53 +02002553 if (!(global.mode & MODE_MWORKER))
2554 exit(1);
2555 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002556 }
2557 }
William Lallemand85b0bd92017-06-01 17:38:53 +02002558 get_cur_unixsocket();
2559
Willy Tarreaubaaee002006-06-26 02:48:02 +02002560 /* We will loop at most 100 times with 10 ms delay each time.
2561 * That's at most 1 second. We only send a signal to old pids
2562 * if we cannot grab at least one port.
2563 */
2564 retry = MAX_START_RETRIES;
2565 err = ERR_NONE;
2566 while (retry >= 0) {
2567 struct timeval w;
2568 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01002569 /* exit the loop on no error or fatal error */
2570 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002571 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02002572 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002573 break;
2574
2575 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
2576 * listening sockets. So on those platforms, it would be wiser to
2577 * simply send SIGUSR1, which will not be undoable.
2578 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02002579 if (tell_old_pids(SIGTTOU) == 0) {
2580 /* no need to wait if we can't contact old pids */
2581 retry = 0;
2582 continue;
2583 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002584 /* give some time to old processes to stop listening */
2585 w.tv_sec = 0;
2586 w.tv_usec = 10*1000;
2587 select(0, NULL, NULL, NULL, &w);
2588 retry--;
2589 }
2590
2591 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01002592 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02002593 if (retry != MAX_START_RETRIES && nb_oldpids) {
2594 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002595 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02002596 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002597 exit(1);
2598 }
2599
2600 if (listeners == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002601 ha_alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002602 /* Note: we don't have to send anything to the old pids because we
2603 * never stopped them. */
2604 exit(1);
2605 }
2606
Emeric Bruncf20bf12010-10-22 16:06:11 +02002607 err = protocol_bind_all(errmsg, sizeof(errmsg));
2608 if ((err & ~ERR_WARN) != ERR_NONE) {
2609 if ((err & ERR_ALERT) || (err & ERR_WARN))
Christopher Faulet767a84b2017-11-24 16:50:31 +01002610 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002611
Christopher Faulet767a84b2017-11-24 16:50:31 +01002612 ha_alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002613 protocol_unbind_all(); /* cleanup everything we can */
2614 if (nb_oldpids)
2615 tell_old_pids(SIGTTIN);
2616 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002617 } else if (err & ERR_WARN) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002618 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02002619 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002620 /* Ok, all listener should now be bound, close any leftover sockets
2621 * the previous process gave us, we don't need them anymore
2622 */
2623 while (xfer_sock_list != NULL) {
2624 struct xfer_sock_list *tmpxfer = xfer_sock_list->next;
2625 close(xfer_sock_list->fd);
2626 free(xfer_sock_list->iface);
2627 free(xfer_sock_list->namespace);
2628 free(xfer_sock_list);
2629 xfer_sock_list = tmpxfer;
2630 }
Willy Tarreaudd815982007-10-16 12:25:14 +02002631
Willy Tarreaubaaee002006-06-26 02:48:02 +02002632 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002633 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
2634 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002635
Willy Tarreaubaaee002006-06-26 02:48:02 +02002636 /* MODE_QUIET can inhibit alerts and warnings below this line */
2637
PiBa-NL149a81a2017-12-25 21:03:31 +01002638 if (getenv("HAPROXY_MWORKER_REEXEC") != NULL) {
2639 /* either stdin/out/err are already closed or should stay as they are. */
2640 if ((global.mode & MODE_DAEMON)) {
2641 /* daemon mode re-executing, stdin/stdout/stderr are already closed so keep quiet */
2642 global.mode &= ~MODE_VERBOSE;
2643 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
2644 }
2645 } else {
2646 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
2647 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01002648 stdio_quiet(-1);
PiBa-NL149a81a2017-12-25 21:03:31 +01002649 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002650 }
2651
2652 /* open log & pid files before the chroot */
William Lallemand80293002017-11-06 11:00:03 +01002653 if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002654 unlink(global.pidfile);
2655 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
2656 if (pidfd < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002657 ha_alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002658 if (nb_oldpids)
2659 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02002660 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002661 exit(1);
2662 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002663 }
2664
Willy Tarreaub38651a2007-03-24 17:24:39 +01002665 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002666 ha_alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
2667 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002668 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01002669 exit(1);
2670 }
2671
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002672 /* If the user is not root, we'll still let him try the configuration
2673 * but we inform him that unexpected behaviour may occur.
2674 */
2675 if ((global.last_checks & LSTCHK_NETADM) && getuid())
Christopher Faulet767a84b2017-11-24 16:50:31 +01002676 ha_warning("[%s.main()] Some options which require full privileges"
2677 " might not work well.\n"
2678 "", argv[0]);
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002679
William Lallemand095ba4c2017-06-01 17:38:50 +02002680 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2681
2682 /* chroot if needed */
2683 if (global.chroot != NULL) {
2684 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002685 ha_alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02002686 if (nb_oldpids)
2687 tell_old_pids(SIGTTIN);
2688 protocol_unbind_all();
2689 exit(1);
2690 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002691 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002692 }
2693
Willy Tarreaubaaee002006-06-26 02:48:02 +02002694 if (nb_oldpids)
Willy Tarreaubb545b42010-08-25 12:58:59 +02002695 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002696
William Lallemand8a361b52017-06-20 11:20:33 +02002697 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) {
2698 nb_oldpids = 0;
2699 free(oldpids);
2700 oldpids = NULL;
2701 }
2702
2703
Willy Tarreaubaaee002006-06-26 02:48:02 +02002704 /* Note that any error at this stage will be fatal because we will not
2705 * be able to restart the old pids.
2706 */
2707
William Lallemand095ba4c2017-06-01 17:38:50 +02002708 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2709 /* setgid / setuid */
2710 if (global.gid) {
2711 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +01002712 ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2713 " without 'uid'/'user' is generally useless.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02002714
2715 if (setgid(global.gid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002716 ha_alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
William Lallemand095ba4c2017-06-01 17:38:50 +02002717 protocol_unbind_all();
2718 exit(1);
2719 }
2720 }
Michael Schererab012dd2013-01-12 18:35:19 +01002721
William Lallemand095ba4c2017-06-01 17:38:50 +02002722 if (global.uid && setuid(global.uid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002723 ha_alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Michael Schererab012dd2013-01-12 18:35:19 +01002724 protocol_unbind_all();
2725 exit(1);
2726 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002727 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002728 /* check ulimits */
2729 limit.rlim_cur = limit.rlim_max = 0;
2730 getrlimit(RLIMIT_NOFILE, &limit);
2731 if (limit.rlim_cur < global.maxsock) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002732 ha_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",
2733 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002734 }
2735
William Lallemand095ba4c2017-06-01 17:38:50 +02002736 if (global.mode & (MODE_DAEMON | MODE_MWORKER)) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002737 struct proxy *px;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002738 struct peers *curpeers;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002739 int ret = 0;
2740 int proc;
William Lallemande1340412017-12-28 16:09:36 +01002741 int devnullfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002742
William Lallemand73b85e72017-06-01 17:38:51 +02002743 children = calloc(global.nbproc, sizeof(int));
William Lallemand095ba4c2017-06-01 17:38:50 +02002744 /*
2745 * if daemon + mworker: must fork here to let a master
2746 * process live in background before forking children
2747 */
William Lallemand73b85e72017-06-01 17:38:51 +02002748
2749 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
2750 && (global.mode & MODE_MWORKER)
2751 && (global.mode & MODE_DAEMON)) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002752 ret = fork();
2753 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002754 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02002755 protocol_unbind_all();
2756 exit(1); /* there has been an error */
William Lallemandbfd8eb52018-07-04 15:31:23 +02002757 } else if (ret > 0) { /* parent leave to daemonize */
William Lallemand095ba4c2017-06-01 17:38:50 +02002758 exit(0);
William Lallemandbfd8eb52018-07-04 15:31:23 +02002759 } else /* change the process group ID in the child (master process) */
2760 setsid();
William Lallemand095ba4c2017-06-01 17:38:50 +02002761 }
William Lallemande20b6a62017-06-01 17:38:55 +02002762
2763 if (global.mode & MODE_MWORKER) {
2764 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) {
2765 char *msg = NULL;
2766 /* master pipe to ensure the master is still alive */
2767 ret = pipe(mworker_pipe);
2768 if (ret < 0) {
PiBa-NL4763ffd2017-11-28 23:22:14 +01002769 ha_alert("[%s.main()] Cannot create master pipe.\n", argv[0]);
2770 exit(EXIT_FAILURE);
William Lallemande20b6a62017-06-01 17:38:55 +02002771 } else {
2772 memprintf(&msg, "%d", mworker_pipe[0]);
2773 setenv("HAPROXY_MWORKER_PIPE_RD", msg, 1);
2774 memprintf(&msg, "%d", mworker_pipe[1]);
2775 setenv("HAPROXY_MWORKER_PIPE_WR", msg, 1);
2776 free(msg);
2777 }
2778 } else {
PiBa-NL4763ffd2017-11-28 23:22:14 +01002779 char* rd = getenv("HAPROXY_MWORKER_PIPE_RD");
2780 char* wr = getenv("HAPROXY_MWORKER_PIPE_WR");
2781 if (!rd || !wr) {
2782 ha_alert("[%s.main()] Cannot get master pipe FDs.\n", argv[0]);
2783 atexit_flag = 0;// dont reexecute master process
2784 exit(EXIT_FAILURE);
William Lallemande20b6a62017-06-01 17:38:55 +02002785 }
PiBa-NL4763ffd2017-11-28 23:22:14 +01002786 mworker_pipe[0] = atoi(rd);
2787 mworker_pipe[1] = atoi(wr);
William Lallemande20b6a62017-06-01 17:38:55 +02002788 }
2789 }
2790
William Lallemanddeed7802017-11-06 11:00:04 +01002791 /* if in master-worker mode, write the PID of the father */
2792 if (global.mode & MODE_MWORKER) {
2793 char pidstr[100];
2794 snprintf(pidstr, sizeof(pidstr), "%d\n", getpid());
Willy Tarreau46ec48b2018-01-23 19:20:19 +01002795 if (pidfd >= 0)
2796 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
William Lallemanddeed7802017-11-06 11:00:04 +01002797 }
2798
Willy Tarreaubaaee002006-06-26 02:48:02 +02002799 /* the father launches the required number of processes */
2800 for (proc = 0; proc < global.nbproc; proc++) {
2801 ret = fork();
2802 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002803 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002804 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +02002805 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002806 }
2807 else if (ret == 0) /* child breaks here */
2808 break;
Marc-Antoine Perennou992709b2013-02-12 10:53:52 +01002809 children[proc] = ret;
William Lallemand92159b22017-11-06 11:16:12 +01002810 if (pidfd >= 0 && !(global.mode & MODE_MWORKER)) {
Willy Tarreau269ab312012-09-05 08:02:48 +02002811 char pidstr[100];
2812 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002813 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002814 }
Willy Tarreaudcd47712007-11-04 23:35:08 +01002815 relative_pid++; /* each child will get a different one */
Willy Tarreau387bd4f2017-11-10 19:08:14 +01002816 pid_bit <<= 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002817 }
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002818
2819#ifdef USE_CPU_AFFINITY
2820 if (proc < global.nbproc && /* child */
Willy Tarreaue7597492015-04-20 11:36:57 +02002821 proc < LONGBITS && /* only the first 32/64 processes may be pinned */
Christopher Fauletcb6a9452017-11-22 16:50:41 +01002822 global.cpu_map.proc[proc]) /* only do this if the process has a CPU map */
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002823#ifdef __FreeBSD__
Olivier Houchard97148f62017-08-16 17:29:11 +02002824 {
2825 cpuset_t cpuset;
2826 int i;
Christopher Fauletcb6a9452017-11-22 16:50:41 +01002827 unsigned long cpu_map = global.cpu_map.proc[proc];
Olivier Houchard97148f62017-08-16 17:29:11 +02002828
2829 CPU_ZERO(&cpuset);
2830 while ((i = ffsl(cpu_map)) > 0) {
2831 CPU_SET(i - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01002832 cpu_map &= ~(1UL << (i - 1));
Olivier Houchard97148f62017-08-16 17:29:11 +02002833 }
2834 ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset);
2835 }
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002836#else
Christopher Fauletcb6a9452017-11-22 16:50:41 +01002837 sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map.proc[proc]);
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002838#endif
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002839#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002840 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02002841 if (pidfd >= 0) {
2842 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
2843 close(pidfd);
2844 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02002845
2846 /* We won't ever use this anymore */
Willy Tarreaud137dd32010-08-25 12:49:05 +02002847 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002848
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002849 if (proc == global.nbproc) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002850 if (global.mode & MODE_MWORKER) {
PiBa-NLbaf6ea42017-11-28 23:26:08 +01002851
2852 if ((!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2853 (global.mode & MODE_DAEMON)) {
2854 /* detach from the tty, this is required to properly daemonize. */
William Lallemande1340412017-12-28 16:09:36 +01002855 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL))
2856 stdio_quiet(-1);
2857
PiBa-NLbaf6ea42017-11-28 23:26:08 +01002858 global.mode &= ~MODE_VERBOSE;
2859 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
PiBa-NLbaf6ea42017-11-28 23:26:08 +01002860 }
2861
William Lallemandb3f2be32018-09-11 10:06:18 +02002862 mworker_loop();
William Lallemand1499b9b2017-06-07 15:04:47 +02002863 /* should never get there */
2864 exit(EXIT_FAILURE);
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002865 }
William Lallemandcf4e4962017-06-08 19:05:48 +02002866#if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH)
Grant Zhang872f9c22017-01-21 01:10:18 +00002867 ssl_free_dh();
2868#endif
William Lallemand1499b9b2017-06-07 15:04:47 +02002869 exit(0); /* parent must leave */
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002870 }
2871
William Lallemandcb11fd22017-06-01 17:38:52 +02002872 /* child must never use the atexit function */
2873 atexit_flag = 0;
2874
Willy Tarreau1605c7a2018-01-23 19:01:49 +01002875 /* close the write end of the master pipe in the children */
2876 if (global.mode & MODE_MWORKER)
2877 close(mworker_pipe[1]);
2878
William Lallemande1340412017-12-28 16:09:36 +01002879 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
2880 devnullfd = open("/dev/null", O_RDWR, 0);
2881 if (devnullfd < 0) {
2882 ha_alert("Cannot open /dev/null\n");
2883 exit(EXIT_FAILURE);
2884 }
2885 }
2886
William Lallemand095ba4c2017-06-01 17:38:50 +02002887 /* Must chroot and setgid/setuid in the children */
2888 /* chroot if needed */
2889 if (global.chroot != NULL) {
2890 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002891 ha_alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02002892 if (nb_oldpids)
2893 tell_old_pids(SIGTTIN);
2894 protocol_unbind_all();
2895 exit(1);
2896 }
2897 }
2898
2899 free(global.chroot);
2900 global.chroot = NULL;
2901
2902 /* setgid / setuid */
2903 if (global.gid) {
2904 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +01002905 ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2906 " without 'uid'/'user' is generally useless.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02002907
2908 if (setgid(global.gid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002909 ha_alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
William Lallemand095ba4c2017-06-01 17:38:50 +02002910 protocol_unbind_all();
2911 exit(1);
2912 }
2913 }
2914
2915 if (global.uid && setuid(global.uid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002916 ha_alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
William Lallemand095ba4c2017-06-01 17:38:50 +02002917 protocol_unbind_all();
2918 exit(1);
2919 }
2920
William Lallemand7f80eb22017-05-26 18:19:55 +02002921 /* pass through every cli socket, and check if it's bound to
2922 * the current process and if it exposes listeners sockets.
2923 * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork.
2924 * */
2925
2926 if (global.stats_fe) {
2927 struct bind_conf *bind_conf;
2928
2929 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
2930 if (bind_conf->level & ACCESS_FD_LISTENERS) {
2931 if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) {
2932 global.tune.options |= GTUNE_SOCKET_TRANSFER;
2933 break;
2934 }
2935 }
2936 }
2937 }
2938
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002939 /* we might have to unbind some proxies from some processes */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002940 px = proxies_list;
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002941 while (px != NULL) {
2942 if (px->bind_proc && px->state != PR_STSTOPPED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002943 if (!(px->bind_proc & (1UL << proc))) {
2944 if (global.tune.options & GTUNE_SOCKET_TRANSFER)
2945 zombify_proxy(px);
2946 else
2947 stop_proxy(px);
2948 }
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002949 }
2950 px = px->next;
2951 }
2952
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002953 /* we might have to unbind some peers sections from some processes */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002954 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002955 if (!curpeers->peers_fe)
2956 continue;
2957
2958 if (curpeers->peers_fe->bind_proc & (1UL << proc))
2959 continue;
2960
2961 stop_proxy(curpeers->peers_fe);
2962 /* disable this peer section so that it kills itself */
Willy Tarreau47c8c022015-09-28 16:39:25 +02002963 signal_unregister_handler(curpeers->sighandler);
2964 task_delete(curpeers->sync_task);
2965 task_free(curpeers->sync_task);
2966 curpeers->sync_task = NULL;
2967 task_free(curpeers->peers_fe->task);
2968 curpeers->peers_fe->task = NULL;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002969 curpeers->peers_fe = NULL;
2970 }
2971
Willy Tarreaubaaee002006-06-26 02:48:02 +02002972 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
2973 * that we can detach from the TTY. We MUST NOT do it in other cases since
2974 * it would have already be done, and 0-2 would have been affected to listening
2975 * sockets
2976 */
Willy Tarreau106cb762008-11-16 07:40:34 +01002977 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002978 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01002979 stdio_quiet(devnullfd);
Willy Tarreau106cb762008-11-16 07:40:34 +01002980 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002981 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
2982 }
2983 pid = getpid(); /* update child's pid */
William Lallemandbfd8eb52018-07-04 15:31:23 +02002984 if (!(global.mode & MODE_MWORKER)) /* in mworker mode we don't want a new pgid for the children */
2985 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02002986 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002987 }
2988
Christopher Faulete3a5e352017-10-24 13:53:54 +02002989 global.mode &= ~MODE_STARTING;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002990 /*
2991 * That's it : the central polling loop. Run until we stop.
2992 */
Christopher Faulet1d17c102017-08-29 15:38:48 +02002993#ifdef USE_THREAD
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002994 {
Christopher Faulet1d17c102017-08-29 15:38:48 +02002995 unsigned int *tids = calloc(global.nbthread, sizeof(unsigned int));
2996 pthread_t *threads = calloc(global.nbthread, sizeof(pthread_t));
2997 int i;
William Lallemand1aab50b2018-06-07 09:46:01 +02002998 sigset_t blocked_sig, old_sig;
Christopher Faulet1d17c102017-08-29 15:38:48 +02002999
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003000 /* Init tids array */
3001 for (i = 0; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02003002 tids[i] = i;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003003
William Lallemand1aab50b2018-06-07 09:46:01 +02003004 /* ensure the signals will be blocked in every thread */
3005 sigfillset(&blocked_sig);
3006 sigdelset(&blocked_sig, SIGPROF);
3007 sigdelset(&blocked_sig, SIGBUS);
3008 sigdelset(&blocked_sig, SIGFPE);
3009 sigdelset(&blocked_sig, SIGILL);
3010 sigdelset(&blocked_sig, SIGSEGV);
3011 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
3012
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003013 /* Create nbthread-1 thread. The first thread is the current process */
3014 threads[0] = pthread_self();
3015 for (i = 1; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02003016 pthread_create(&threads[i], NULL, &run_thread_poll_loop, &tids[i]);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003017
Christopher Faulet62519022017-10-16 15:49:32 +02003018#ifdef USE_CPU_AFFINITY
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003019 /* Now the CPU affinity for all threads */
3020 for (i = 0; i < global.nbthread; i++) {
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003021 if (global.cpu_map.proc[relative_pid-1])
3022 global.cpu_map.thread[relative_pid-1][i] &= global.cpu_map.proc[relative_pid-1];
Christopher Faulet62519022017-10-16 15:49:32 +02003023
Willy Tarreau421f02e2018-01-20 18:19:22 +01003024 if (i < MAX_THREADS && /* only the first 32/64 threads may be pinned */
Olivier Houchard829aa242017-12-01 18:19:43 +01003025 global.cpu_map.thread[relative_pid-1][i]) {/* only do this if the thread has a THREAD map */
3026#if defined(__FreeBSD__) || defined(__NetBSD__)
3027 cpuset_t cpuset;
3028#else
3029 cpu_set_t cpuset;
3030#endif
3031 int j;
3032 unsigned long cpu_map = global.cpu_map.thread[relative_pid-1][i];
3033
3034 CPU_ZERO(&cpuset);
3035
3036 while ((j = ffsl(cpu_map)) > 0) {
3037 CPU_SET(j - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003038 cpu_map &= ~(1UL << (j - 1));
Olivier Houchard829aa242017-12-01 18:19:43 +01003039 }
Christopher Faulet62519022017-10-16 15:49:32 +02003040 pthread_setaffinity_np(threads[i],
Olivier Houchard829aa242017-12-01 18:19:43 +01003041 sizeof(cpuset), &cpuset);
3042 }
Christopher Faulet1d17c102017-08-29 15:38:48 +02003043 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003044#endif /* !USE_CPU_AFFINITY */
3045
William Lallemand1aab50b2018-06-07 09:46:01 +02003046 /* when multithreading we need to let only the thread 0 handle the signals */
3047 pthread_sigmask(SIG_SETMASK, &old_sig, NULL);
3048
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003049 /* Finally, start the poll loop for the first thread */
3050 run_thread_poll_loop(&tids[0]);
3051
3052 /* Wait the end of other threads */
3053 for (i = 1; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02003054 pthread_join(threads[i], NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003055
Christopher Faulet1d17c102017-08-29 15:38:48 +02003056 free(tids);
3057 free(threads);
3058
Christopher Fauletb79a94c2017-05-30 15:34:30 +02003059#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
3060 show_lock_stats();
3061#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003062 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003063#else /* ! USE_THREAD */
Christopher Fauletd4604ad2017-05-29 10:40:41 +02003064
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003065 run_thread_poll_loop((int []){0});
Christopher Faulet62519022017-10-16 15:49:32 +02003066
Christopher Faulet62519022017-10-16 15:49:32 +02003067#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003068
3069 /* Do some cleanup */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003070 deinit();
Christopher Faulet1d17c102017-08-29 15:38:48 +02003071
Willy Tarreaubaaee002006-06-26 02:48:02 +02003072 exit(0);
3073}
3074
3075
3076/*
3077 * Local variables:
3078 * c-indent-level: 8
3079 * c-basic-offset: 8
3080 * End:
3081 */