blob: 8b13d96f48bfac3c841312e3496dc0a344ea5d41 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HA-Proxy : High Availability-enabled HTTP/TCP proxy
Willy Tarreau71f95fa2020-01-22 10:34:58 +01003 * Copyright 2000-2020 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>
David Carlier42d9e5a2018-11-12 16:22:19 +000055#if defined(__FreeBSD__) || defined(__DragonFly__)
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +020056#include <sys/param.h>
David Carlier42d9e5a2018-11-12 16:22:19 +000057#ifdef __FreeBSD__
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +020058#include <sys/cpuset.h>
David Carlier42d9e5a2018-11-12 16:22:19 +000059#endif
David Carlier6d5c8412017-11-29 11:02:32 +000060#include <pthread_np.h>
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +020061#endif
David Carlier5e4c8e22019-09-13 05:12:58 +010062#ifdef __APPLE__
63#include <mach/mach_types.h>
64#include <mach/thread_act.h>
65#include <mach/thread_policy.h>
66#endif
Willy Tarreaufc6c0322012-11-16 16:12:27 +010067#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau636848a2019-04-15 19:38:50 +020069#if defined(USE_PRCTL)
70#include <sys/prctl.h>
71#endif
72
Willy Tarreaubaaee002006-06-26 02:48:02 +020073#ifdef DEBUG_FULL
74#include <assert.h>
75#endif
Tim Duesterhusd6942c82017-11-20 15:58:35 +010076#if defined(USE_SYSTEMD)
77#include <systemd/sd-daemon.h>
78#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020079
Willy Tarreau6c3a6812020-03-06 18:57:15 +010080#include <import/sha1.h>
81
Willy Tarreau2dd0d472006-06-29 17:53:05 +020082#include <common/base64.h>
83#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020084#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020085#include <common/compat.h>
86#include <common/config.h>
87#include <common/defaults.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010088#include <common/errors.h>
Willy Tarreau5794fb02018-11-25 18:43:29 +010089#include <common/initcall.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020090#include <common/memory.h>
91#include <common/mini-clist.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010092#include <common/namespace.h>
Willy Tarreau6c3a6812020-03-06 18:57:15 +010093#include <common/net_helper.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020094#include <common/openssl-compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020095#include <common/regex.h>
96#include <common/standard.h>
97#include <common/time.h>
98#include <common/uri_auth.h>
99#include <common/version.h>
Christopher Fauletbe0faa22017-08-29 15:37:10 +0200100#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101
102#include <types/capture.h>
William Lallemandce83b4a2018-10-26 14:47:30 +0200103#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +0200104#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105#include <types/global.h>
Simon Hormanac821422011-07-15 13:14:09 +0900106#include <types/acl.h>
Willy Tarreau3c63fd82011-09-07 18:00:47 +0200107#include <types/peers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200109#include <proto/acl.h>
Willy Tarreau609aad92018-11-22 08:31:09 +0100110#include <proto/activity.h>
Willy Tarreau2e845be2012-10-19 19:49:09 +0200111#include <proto/arg.h>
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200112#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +0200114#include <proto/channel.h>
William Lallemandce83b4a2018-10-26 14:47:30 +0200115#include <proto/cli.h>
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200116#include <proto/connection.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +0200118#include <proto/filters.h>
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +0100119#include <proto/hlua.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +0200120#include <proto/http_rules.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +0200121#include <proto/listener.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122#include <proto/log.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +0200123#include <proto/mworker.h>
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100124#include <proto/pattern.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +0200125#include <proto/protocol.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200126#include <proto/http_ana.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127#include <proto/proxy.h>
128#include <proto/queue.h>
129#include <proto/server.h>
Willy Tarreaub1ec8c42015-04-03 13:53:24 +0200130#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +0200131#include <proto/stream.h>
Willy Tarreau29857942009-05-10 09:01:21 +0200132#include <proto/signal.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133#include <proto/task.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +0200134#include <proto/dns.h>
Christopher Fauletff2613e2016-11-09 11:36:17 +0100135#include <proto/vars.h>
Grant Zhang872f9c22017-01-21 01:10:18 +0000136#include <proto/ssl_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200137
Willy Tarreau7b5654f2019-03-29 21:30:17 +0100138/* array of init calls for older platforms */
139DECLARE_INIT_STAGES;
140
Willy Tarreau477ecd82010-01-03 21:12:30 +0100141/* list of config files */
142static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100144int relative_pid = 1; /* process id starting at 1 */
Willy Tarreau387bd4f2017-11-10 19:08:14 +0100145unsigned long pid_bit = 1; /* bit corresponding to the process id */
Willy Tarreaua38a7172019-02-02 17:11:28 +0100146unsigned long all_proc_mask = 1; /* mask of all processes */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200147
Olivier Houchard79321b92018-07-26 17:55:11 +0200148volatile unsigned long sleeping_thread_mask; /* Threads that are about to sleep in poll() */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149/* global options */
150struct global global = {
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100151 .hard_stop_after = TICK_ETERNITY,
Willy Tarreau247a13a2012-11-15 17:38:15 +0100152 .nbproc = 1,
Willy Tarreau149ab772019-01-26 14:27:06 +0100153 .nbthread = 0,
William Lallemand5f232402012-04-05 18:02:55 +0200154 .req_count = 0,
William Lallemand0f99e342011-10-12 17:50:54 +0200155 .logsrvs = LIST_HEAD_INIT(global.logsrvs),
William Lallemand9d5f5482012-11-07 16:12:57 +0100156 .maxzlibmem = 0,
William Lallemandd85f9172012-11-09 17:05:39 +0100157 .comp_rate_lim = 0,
Emeric Brun850efd52014-01-29 12:24:34 +0100158 .ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED,
Emeric Bruned760922010-10-22 17:59:25 +0200159 .unix_bind = {
160 .ux = {
161 .uid = -1,
162 .gid = -1,
163 .mode = 0,
164 }
165 },
Willy Tarreau27a674e2009-08-17 07:23:33 +0200166 .tune = {
Willy Tarreau7ac908b2019-02-27 12:02:18 +0100167 .options = GTUNE_LISTENER_MQ,
Willy Tarreauc77d3642018-12-12 06:19:42 +0100168 .bufsize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)),
Christopher Faulet546c4692020-01-22 14:31:21 +0100169 .maxrewrite = MAXREWRITE,
Willy Tarreauc77d3642018-12-12 06:19:42 +0100170 .chksize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)),
Willy Tarreaua24adf02014-11-27 01:11:56 +0100171 .reserved_bufs = RESERVED_BUFS,
Willy Tarreauf3045d22015-04-29 16:24:50 +0200172 .pattern_cache = DEFAULT_PAT_LRU_SIZE,
Olivier Houchard88698d92019-04-16 19:07:22 +0200173 .pool_low_ratio = 20,
174 .pool_high_ratio = 25,
Christopher Faulet41ba36f2019-07-19 09:36:45 +0200175 .max_http_hdr = MAX_HTTP_HDR,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200176#ifdef USE_OPENSSL
Emeric Brun46635772012-11-14 11:32:56 +0100177 .sslcachesize = SSLCACHESIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200178#endif
William Lallemandf3747832012-11-09 12:33:10 +0100179 .comp_maxlevel = 1,
Willy Tarreau7e312732014-02-12 16:35:14 +0100180#ifdef DEFAULT_IDLE_TIMER
181 .idle_timer = DEFAULT_IDLE_TIMER,
182#else
183 .idle_timer = 1000, /* 1 second */
184#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200185 },
Emeric Brun76d88952012-10-05 15:47:31 +0200186#ifdef USE_OPENSSL
187#ifdef DEFAULT_MAXSSLCONN
Willy Tarreau403edff2012-09-06 11:58:37 +0200188 .maxsslconn = DEFAULT_MAXSSLCONN,
189#endif
Emeric Brun76d88952012-10-05 15:47:31 +0200190#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191 /* others NULL OK */
192};
193
194/*********************************************************************/
195
196int stopping; /* non zero means stopping in progress */
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100197int killed; /* non zero means a hard-stop is triggered */
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200198int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */
William Lallemanda7199262018-11-16 16:57:20 +0100199int unstoppable_jobs = 0; /* number of active jobs that can't be stopped during a soft stop */
Willy Tarreau199ad242018-11-05 16:31:22 +0100200int active_peers = 0; /* number of active peers (connection attempts and connected) */
Willy Tarreau2d372c22018-11-05 17:12:27 +0100201int connected_peers = 0; /* number of connected peers (verified ones) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200202
203/* Here we store informations about the pids of the processes we may pause
204 * or kill. We will send them a signal every 10 ms until we can bind to all
205 * our ports. With 200 retries, that's about 2 seconds.
206 */
207#define MAX_START_RETRIES 200
Willy Tarreaubaaee002006-06-26 02:48:02 +0200208static int *oldpids = NULL;
209static int oldpids_sig; /* use USR1 or TERM */
210
Olivier Houchardf73629d2017-04-05 22:33:04 +0200211/* Path to the unix socket we use to retrieve listener sockets from the old process */
212static const char *old_unixsocket;
213
William Lallemand85b0bd92017-06-01 17:38:53 +0200214static char *cur_unixsocket = NULL;
215
William Lallemandcb11fd22017-06-01 17:38:52 +0200216int atexit_flag = 0;
217
Willy Tarreaubb545b42010-08-25 12:58:59 +0200218int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200219const int zero = 0;
220const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200221const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200222
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100223char hostname[MAX_HOSTNAME_LEN];
Emeric Brun2b920a12010-09-23 18:30:22 +0200224char localpeer[MAX_HOSTNAME_LEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200225
Willy Tarreau89efaed2013-12-13 15:14:55 +0100226/* used from everywhere just to drain results we don't want to read and which
227 * recent versions of gcc increasingly and annoyingly complain about.
228 */
229int shut_your_big_mouth_gcc_int = 0;
230
William Lallemand73b85e72017-06-01 17:38:51 +0200231static char **next_argv = NULL;
232
William Lallemandbc193052018-09-11 10:06:26 +0200233struct list proc_list = LIST_HEAD_INIT(proc_list);
234
235int master = 0; /* 1 if in master, 0 if in child */
Willy Tarreaubf696402019-03-01 10:09:28 +0100236unsigned int rlim_fd_cur_at_boot = 0;
237unsigned int rlim_fd_max_at_boot = 0;
William Lallemandbc193052018-09-11 10:06:26 +0200238
Willy Tarreau6c3a6812020-03-06 18:57:15 +0100239/* per-boot randomness */
240unsigned char boot_seed[20]; /* per-boot random seed (160 bits initially) */
Willy Tarreau1c306aa2020-03-06 19:04:55 +0100241THREAD_LOCAL char ha_rand_state[32]; /* opaque 256 bits of random state */
242THREAD_LOCAL struct random_data ha_rand_data; /* opaque internal random_r() date */
Willy Tarreau6c3a6812020-03-06 18:57:15 +0100243
William Lallemand16dd1b32018-11-19 18:46:18 +0100244struct mworker_proc *proc_self = NULL;
William Lallemandbc193052018-09-11 10:06:26 +0200245
William Lallemandb3f2be32018-09-11 10:06:18 +0200246static void *run_thread_poll_loop(void *data);
247
Willy Tarreauff055502014-04-28 22:27:06 +0200248/* bitfield of a few warnings to emit just once (WARN_*) */
249unsigned int warned = 0;
250
William Lallemande7361152018-10-26 14:47:36 +0200251/* master CLI configuration (-S flag) */
252struct list mworker_cli_conf = LIST_HEAD_INIT(mworker_cli_conf);
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100253
254/* These are strings to be reported in the output of "haproxy -vv". They may
255 * either be constants (in which case must_free must be zero) or dynamically
256 * allocated strings to pass to free() on exit, and in this case must_free
257 * must be non-zero.
258 */
259struct list build_opts_list = LIST_HEAD_INIT(build_opts_list);
260struct build_opts_str {
261 struct list list;
262 const char *str;
263 int must_free;
264};
265
Willy Tarreaue6945732016-12-21 19:57:00 +0100266/* These functions are called just after the point where the program exits
267 * after a config validity check, so they are generally suited for resource
268 * allocation and slow initializations that should be skipped during basic
269 * config checks. The functions must return 0 on success, or a combination
270 * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause
271 * and immediate exit, so the function must have emitted any useful error.
272 */
273struct list post_check_list = LIST_HEAD_INIT(post_check_list);
274struct post_check_fct {
275 struct list list;
276 int (*fct)();
277};
278
Christopher Fauletc1692962019-08-12 09:51:07 +0200279/* These functions are called for each proxy just after the config validity
280 * check. The functions must return 0 on success, or a combination of ERR_*
281 * flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause and immediate
282 * exit, so the function must have emitted any useful error.
283 */
284struct list post_proxy_check_list = LIST_HEAD_INIT(post_proxy_check_list);
285struct post_proxy_check_fct {
286 struct list list;
287 int (*fct)(struct proxy *);
288};
289
290/* These functions are called for each server just after the config validity
291 * check. The functions must return 0 on success, or a combination of ERR_*
292 * flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause and immediate
293 * exit, so the function must have emitted any useful error.
294 */
295struct list post_server_check_list = LIST_HEAD_INIT(post_server_check_list);
296struct post_server_check_fct {
297 struct list list;
298 int (*fct)(struct server *);
299};
300
Willy Tarreau082b6282019-05-22 14:42:12 +0200301/* These functions are called for each thread just after the thread creation
302 * and before running the init functions. They should be used to do per-thread
303 * (re-)allocations that are needed by subsequent functoins. They must return 0
304 * if an error occurred. */
305struct list per_thread_alloc_list = LIST_HEAD_INIT(per_thread_alloc_list);
306struct per_thread_alloc_fct {
Willy Tarreau05554e62016-12-21 20:46:26 +0100307 struct list list;
Willy Tarreau082b6282019-05-22 14:42:12 +0200308 int (*fct)();
Willy Tarreau05554e62016-12-21 20:46:26 +0100309};
310
Christopher Faulet415f6112017-07-25 16:52:58 +0200311/* These functions are called for each thread just after the thread creation
312 * and before running the scheduler. They should be used to do per-thread
313 * initializations. They must return 0 if an error occurred. */
314struct list per_thread_init_list = LIST_HEAD_INIT(per_thread_init_list);
315struct per_thread_init_fct {
316 struct list list;
317 int (*fct)();
318};
319
Willy Tarreau082b6282019-05-22 14:42:12 +0200320/* These functions are called when freeing the global sections at the end of
321 * deinit, after everything is stopped. They don't return anything. They should
322 * not release shared resources that are possibly used by other deinit
323 * functions, only close/release what is private. Use the per_thread_free_list
324 * to release shared resources.
325 */
326struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list);
327struct post_deinit_fct {
328 struct list list;
329 void (*fct)();
330};
331
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +0200332/* These functions are called when freeing a proxy during the deinit, after
333 * everything isg stopped. They don't return anything. They should not release
334 * the proxy itself or any shared resources that are possibly used by other
335 * deinit functions, only close/release what is private.
336 */
337struct list proxy_deinit_list = LIST_HEAD_INIT(proxy_deinit_list);
338struct proxy_deinit_fct {
339 struct list list;
340 void (*fct)(struct proxy *);
341};
342
343/* These functions are called when freeing a server during the deinit, after
344 * everything isg stopped. They don't return anything. They should not release
345 * the proxy itself or any shared resources that are possibly used by other
346 * deinit functions, only close/release what is private.
347 */
348struct list server_deinit_list = LIST_HEAD_INIT(server_deinit_list);
349struct server_deinit_fct {
350 struct list list;
351 void (*fct)(struct server *);
352};
353
Willy Tarreau082b6282019-05-22 14:42:12 +0200354/* These functions are called when freeing the global sections at the end of
355 * deinit, after the thread deinit functions, to release unneeded memory
356 * allocations. They don't return anything, and they work in best effort mode
357 * as their sole goal is to make valgrind mostly happy.
358 */
359struct list per_thread_free_list = LIST_HEAD_INIT(per_thread_free_list);
360struct per_thread_free_fct {
361 struct list list;
362 int (*fct)();
363};
364
Christopher Faulet415f6112017-07-25 16:52:58 +0200365/* These functions are called for each thread just after the scheduler loop and
366 * before exiting the thread. They don't return anything and, as for post-deinit
367 * functions, they work in best effort mode as their sole goal is to make
368 * valgrind mostly happy. */
369struct list per_thread_deinit_list = LIST_HEAD_INIT(per_thread_deinit_list);
370struct per_thread_deinit_fct {
371 struct list list;
372 void (*fct)();
373};
374
Willy Tarreaubaaee002006-06-26 02:48:02 +0200375/*********************************************************************/
376/* general purpose functions ***************************************/
377/*********************************************************************/
378
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100379/* used to register some build option strings at boot. Set must_free to
380 * non-zero if the string must be freed upon exit.
381 */
382void hap_register_build_opts(const char *str, int must_free)
383{
384 struct build_opts_str *b;
385
386 b = calloc(1, sizeof(*b));
387 if (!b) {
388 fprintf(stderr, "out of memory\n");
389 exit(1);
390 }
391 b->str = str;
392 b->must_free = must_free;
393 LIST_ADDQ(&build_opts_list, &b->list);
394}
395
Willy Tarreaue6945732016-12-21 19:57:00 +0100396/* used to register some initialization functions to call after the checks. */
397void hap_register_post_check(int (*fct)())
398{
399 struct post_check_fct *b;
400
401 b = calloc(1, sizeof(*b));
402 if (!b) {
403 fprintf(stderr, "out of memory\n");
404 exit(1);
405 }
406 b->fct = fct;
407 LIST_ADDQ(&post_check_list, &b->list);
408}
409
Christopher Fauletc1692962019-08-12 09:51:07 +0200410/* used to register some initialization functions to call for each proxy after
411 * the checks.
412 */
413void hap_register_post_proxy_check(int (*fct)(struct proxy *))
414{
415 struct post_proxy_check_fct *b;
416
417 b = calloc(1, sizeof(*b));
418 if (!b) {
419 fprintf(stderr, "out of memory\n");
420 exit(1);
421 }
422 b->fct = fct;
423 LIST_ADDQ(&post_proxy_check_list, &b->list);
424}
425
426/* used to register some initialization functions to call for each server after
427 * the checks.
428 */
429void hap_register_post_server_check(int (*fct)(struct server *))
430{
431 struct post_server_check_fct *b;
432
433 b = calloc(1, sizeof(*b));
434 if (!b) {
435 fprintf(stderr, "out of memory\n");
436 exit(1);
437 }
438 b->fct = fct;
439 LIST_ADDQ(&post_server_check_list, &b->list);
440}
441
Willy Tarreau05554e62016-12-21 20:46:26 +0100442/* used to register some de-initialization functions to call after everything
443 * has stopped.
444 */
445void hap_register_post_deinit(void (*fct)())
446{
447 struct post_deinit_fct *b;
448
449 b = calloc(1, sizeof(*b));
450 if (!b) {
451 fprintf(stderr, "out of memory\n");
452 exit(1);
453 }
454 b->fct = fct;
455 LIST_ADDQ(&post_deinit_list, &b->list);
456}
457
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +0200458/* used to register some per proxy de-initialization functions to call after
459 * everything has stopped.
460 */
461void hap_register_proxy_deinit(void (*fct)(struct proxy *))
462{
463 struct proxy_deinit_fct *b;
464
465 b = calloc(1, sizeof(*b));
466 if (!b) {
467 fprintf(stderr, "out of memory\n");
468 exit(1);
469 }
470 b->fct = fct;
471 LIST_ADDQ(&proxy_deinit_list, &b->list);
472}
473
474
475/* used to register some per server de-initialization functions to call after
476 * everything has stopped.
477 */
478void hap_register_server_deinit(void (*fct)(struct server *))
479{
480 struct server_deinit_fct *b;
481
482 b = calloc(1, sizeof(*b));
483 if (!b) {
484 fprintf(stderr, "out of memory\n");
485 exit(1);
486 }
487 b->fct = fct;
488 LIST_ADDQ(&server_deinit_list, &b->list);
489}
490
Willy Tarreau082b6282019-05-22 14:42:12 +0200491/* used to register some allocation functions to call for each thread. */
492void hap_register_per_thread_alloc(int (*fct)())
493{
494 struct per_thread_alloc_fct *b;
495
496 b = calloc(1, sizeof(*b));
497 if (!b) {
498 fprintf(stderr, "out of memory\n");
499 exit(1);
500 }
501 b->fct = fct;
502 LIST_ADDQ(&per_thread_alloc_list, &b->list);
503}
504
Christopher Faulet415f6112017-07-25 16:52:58 +0200505/* used to register some initialization functions to call for each thread. */
506void hap_register_per_thread_init(int (*fct)())
507{
508 struct per_thread_init_fct *b;
509
510 b = calloc(1, sizeof(*b));
511 if (!b) {
512 fprintf(stderr, "out of memory\n");
513 exit(1);
514 }
515 b->fct = fct;
516 LIST_ADDQ(&per_thread_init_list, &b->list);
517}
518
519/* used to register some de-initialization functions to call for each thread. */
520void hap_register_per_thread_deinit(void (*fct)())
521{
522 struct per_thread_deinit_fct *b;
523
524 b = calloc(1, sizeof(*b));
525 if (!b) {
526 fprintf(stderr, "out of memory\n");
527 exit(1);
528 }
529 b->fct = fct;
530 LIST_ADDQ(&per_thread_deinit_list, &b->list);
531}
532
Willy Tarreau082b6282019-05-22 14:42:12 +0200533/* used to register some free functions to call for each thread. */
534void hap_register_per_thread_free(int (*fct)())
535{
536 struct per_thread_free_fct *b;
537
538 b = calloc(1, sizeof(*b));
539 if (!b) {
540 fprintf(stderr, "out of memory\n");
541 exit(1);
542 }
543 b->fct = fct;
544 LIST_ADDQ(&per_thread_free_list, &b->list);
545}
546
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100547static void display_version()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200548{
Willy Tarreau08dd2022019-11-21 18:07:30 +0100549 printf("HA-Proxy version %s %s - https://haproxy.org/\n"
550 PRODUCT_STATUS "\n", haproxy_version, haproxy_date);
Willy Tarreau47479eb2019-11-21 18:48:20 +0100551
552 if (strlen(PRODUCT_URL_BUGS) > 0) {
553 char base_version[20];
554 int dots = 0;
555 char *del;
556
557 /* only retrieve the base version without distro-specific extensions */
558 for (del = haproxy_version; *del; del++) {
559 if (*del == '.')
560 dots++;
561 else if (*del < '0' || *del > '9')
562 break;
563 }
564
565 strlcpy2(base_version, haproxy_version, del - haproxy_version + 1);
566 if (dots < 2)
567 printf("Known bugs: https://github.com/haproxy/haproxy/issues?q=is:issue+is:open\n");
568 else
569 printf("Known bugs: " PRODUCT_URL_BUGS "\n", base_version);
570 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571}
572
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100573static void display_build_opts()
Willy Tarreau7b066db2007-12-02 11:28:59 +0100574{
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100575 struct build_opts_str *item;
576
Willy Tarreau7b066db2007-12-02 11:28:59 +0100577 printf("Build options :"
578#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100579 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100580#endif
581#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100582 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100583#endif
584#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100585 "\n CC = " BUILD_CC
586#endif
587#ifdef BUILD_CFLAGS
588 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100589#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100590#ifdef BUILD_OPTIONS
591 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100592#endif
Willy Tarreau7728ed32019-03-27 13:20:08 +0100593#ifdef BUILD_FEATURES
594 "\n\nFeature list : " BUILD_FEATURES
595#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200596 "\n\nDefault settings :"
Willy Tarreauca783d42019-03-13 10:03:07 +0100597 "\n bufsize = %d, maxrewrite = %d, maxpollevents = %d"
Willy Tarreau27a674e2009-08-17 07:23:33 +0200598 "\n\n",
Willy Tarreauca783d42019-03-13 10:03:07 +0100599 BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS);
Willy Tarreaube5b6852009-10-03 18:57:08 +0200600
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100601 list_for_each_entry(item, &build_opts_list, list) {
602 puts(item->str);
603 }
604
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100605 putchar('\n');
606
Willy Tarreaube5b6852009-10-03 18:57:08 +0200607 list_pollers(stdout);
608 putchar('\n');
Christopher Faulet98d9fe22018-04-10 14:37:32 +0200609 list_mux_proto(stdout);
610 putchar('\n');
Willy Tarreau679bba12019-03-19 08:08:10 +0100611 list_services(stdout);
612 putchar('\n');
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100613 list_filters(stdout);
614 putchar('\n');
Willy Tarreau7b066db2007-12-02 11:28:59 +0100615}
616
Willy Tarreaubaaee002006-06-26 02:48:02 +0200617/*
618 * This function prints the command line usage and exits
619 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100620static void usage(char *name)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621{
622 display_version();
623 fprintf(stderr,
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200624 "Usage : %s [-f <cfgfile|cfgdir>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200625 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
Willy Tarreaua088d312015-10-08 11:58:48 +0200626 " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100627 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200628 " -d enters debug mode ; -db only disables background mode.\n"
Willy Tarreau6e064432012-05-08 15:40:42 +0200629 " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200630 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200631 " -D goes daemon ; -C changes to <dir> before loading files.\n"
William Lallemand095ba4c2017-06-01 17:38:50 +0200632 " -W master-worker mode.\n"
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100633#if defined(USE_SYSTEMD)
634 " -Ws master-worker mode with systemd notify support.\n"
635#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200636 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200637 " -c check mode : only check config files and exit\n"
Willy Tarreauca783d42019-03-13 10:03:07 +0100638 " -n sets the maximum total # of connections (uses ulimit -n)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200639 " -m limits the usable amount of memory (in MB)\n"
640 " -N sets the default, per-proxy maximum # of connections (%d)\n"
Emeric Brun2b920a12010-09-23 18:30:22 +0200641 " -L set local peer name (default to hostname)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200642 " -p writes pids of all children to this file\n"
Willy Tarreaue5733232019-05-22 19:24:06 +0200643#if defined(USE_EPOLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200644 " -de disables epoll() usage even when available\n"
645#endif
Willy Tarreaue5733232019-05-22 19:24:06 +0200646#if defined(USE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200647 " -dk disables kqueue() usage even when available\n"
648#endif
Willy Tarreaue5733232019-05-22 19:24:06 +0200649#if defined(USE_EVPORTS)
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +0000650 " -dv disables event ports usage even when available\n"
651#endif
Willy Tarreaue5733232019-05-22 19:24:06 +0200652#if defined(USE_POLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200653 " -dp disables poll() usage even when available\n"
654#endif
Willy Tarreaue5733232019-05-22 19:24:06 +0200655#if defined(USE_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100656 " -dS disables splice usage (broken on old kernels)\n"
657#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200658#if defined(USE_GETADDRINFO)
659 " -dG disables getaddrinfo() usage\n"
660#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000661#if defined(SO_REUSEPORT)
662 " -dR disables SO_REUSEPORT usage\n"
663#endif
Willy Tarreau3eed10e2016-11-07 21:03:16 +0100664 " -dr ignores server address resolution failures\n"
Emeric Brun850efd52014-01-29 12:24:34 +0100665 " -dV disables SSL verify on servers side\n"
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +0200666 " -sf/-st [pid ]* finishes/terminates old pids.\n"
Olivier Houchardf73629d2017-04-05 22:33:04 +0200667 " -x <unix_socket> get listening sockets from a unix socket\n"
William Lallemand63329e32019-06-13 17:03:37 +0200668 " -S <bind>[,<bind options>...] new master CLI\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669 "\n",
Willy Tarreauca783d42019-03-13 10:03:07 +0100670 name, cfg_maxpconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200671 exit(1);
672}
673
674
675
676/*********************************************************************/
677/* more specific functions ***************************************/
678/*********************************************************************/
679
William Lallemand73b85e72017-06-01 17:38:51 +0200680/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
681 * pids the signal was correctly delivered to.
682 */
William Lallemande25473c2019-04-01 11:29:56 +0200683int tell_old_pids(int sig)
William Lallemand73b85e72017-06-01 17:38:51 +0200684{
685 int p;
686 int ret = 0;
687 for (p = 0; p < nb_oldpids; p++)
688 if (kill(oldpids[p], sig) == 0)
689 ret++;
690 return ret;
691}
692
William Lallemand75ea0a02017-11-15 19:02:58 +0100693/*
William Lallemand73b85e72017-06-01 17:38:51 +0200694 * remove a pid forom the olpid array and decrease nb_oldpids
695 * return 1 pid was found otherwise return 0
696 */
697
698int delete_oldpid(int pid)
699{
700 int i;
701
702 for (i = 0; i < nb_oldpids; i++) {
703 if (oldpids[i] == pid) {
704 oldpids[i] = oldpids[nb_oldpids - 1];
705 oldpids[nb_oldpids - 1] = 0;
706 nb_oldpids--;
707 return 1;
708 }
709 }
710 return 0;
711}
712
William Lallemand85b0bd92017-06-01 17:38:53 +0200713
714static void get_cur_unixsocket()
715{
716 /* if -x was used, try to update the stat socket if not available anymore */
717 if (global.stats_fe) {
718 struct bind_conf *bind_conf;
719
720 /* pass through all stats socket */
721 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
722 struct listener *l;
723
724 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
725
726 if (l->addr.ss_family == AF_UNIX &&
727 (bind_conf->level & ACCESS_FD_LISTENERS)) {
728 const struct sockaddr_un *un;
729
730 un = (struct sockaddr_un *)&l->addr;
731 /* priority to old_unixsocket */
732 if (!cur_unixsocket) {
733 cur_unixsocket = strdup(un->sun_path);
734 } else {
735 if (old_unixsocket && !strcmp(un->sun_path, old_unixsocket)) {
736 free(cur_unixsocket);
737 cur_unixsocket = strdup(old_unixsocket);
738 return;
739 }
740 }
741 }
742 }
743 }
744 }
745 if (!cur_unixsocket && old_unixsocket)
746 cur_unixsocket = strdup(old_unixsocket);
747}
748
William Lallemand73b85e72017-06-01 17:38:51 +0200749/*
750 * When called, this function reexec haproxy with -sf followed by current
Joseph Herlant03420902018-11-15 10:41:50 -0800751 * children PIDs and possibly old children PIDs if they didn't leave yet.
William Lallemand73b85e72017-06-01 17:38:51 +0200752 */
William Lallemanda57b7e32018-12-14 21:11:31 +0100753void mworker_reload()
William Lallemand73b85e72017-06-01 17:38:51 +0200754{
755 int next_argc = 0;
William Lallemand73b85e72017-06-01 17:38:51 +0200756 char *msg = NULL;
Willy Tarreau8dca1952019-03-01 10:21:55 +0100757 struct rlimit limit;
William Lallemand7c756a82018-11-26 11:53:40 +0100758 struct per_thread_deinit_fct *ptdf;
William Lallemand73b85e72017-06-01 17:38:51 +0200759
760 mworker_block_signals();
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100761#if defined(USE_SYSTEMD)
762 if (global.tune.options & GTUNE_USE_SYSTEMD)
763 sd_notify(0, "RELOADING=1");
764#endif
William Lallemand73b85e72017-06-01 17:38:51 +0200765 setenv("HAPROXY_MWORKER_REEXEC", "1", 1);
766
William Lallemandbc193052018-09-11 10:06:26 +0200767 mworker_proc_list_to_env(); /* put the children description in the env */
768
William Lallemand7c756a82018-11-26 11:53:40 +0100769 /* during the reload we must ensure that every FDs that can't be
770 * reuse (ie those that are not referenced in the proc_list)
771 * are closed or they will leak. */
772
773 /* close the listeners FD */
774 mworker_cli_proxy_stop();
William Lallemand16866672019-06-24 17:40:48 +0200775
776 if (getenv("HAPROXY_MWORKER_WAIT_ONLY") == NULL) {
777 /* close the poller FD and the thread waker pipe FD */
778 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
779 ptdf->fct();
780 if (fdtab)
781 deinit_pollers();
782 }
Willy Tarreau5db847a2019-05-09 14:13:35 +0200783#if defined(USE_OPENSSL) && (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
William Lallemand5fdb5b32019-10-15 14:04:08 +0200784 /* close random device FDs */
785 RAND_keep_random_devices_open(0);
Rob Allen56996da2019-05-03 09:11:32 +0100786#endif
William Lallemand7c756a82018-11-26 11:53:40 +0100787
Willy Tarreau8dca1952019-03-01 10:21:55 +0100788 /* restore the initial FD limits */
789 limit.rlim_cur = rlim_fd_cur_at_boot;
790 limit.rlim_max = rlim_fd_max_at_boot;
791 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
792 getrlimit(RLIMIT_NOFILE, &limit);
793 ha_warning("Failed to restore initial FD limits (cur=%u max=%u), using cur=%u max=%u\n",
794 rlim_fd_cur_at_boot, rlim_fd_max_at_boot,
795 (unsigned int)limit.rlim_cur, (unsigned int)limit.rlim_max);
796 }
797
William Lallemand73b85e72017-06-01 17:38:51 +0200798 /* compute length */
799 while (next_argv[next_argc])
800 next_argc++;
801
William Lallemand85b0bd92017-06-01 17:38:53 +0200802 /* 1 for haproxy -sf, 2 for -x /socket */
William Lallemand3f128872019-04-01 11:29:59 +0200803 next_argv = realloc(next_argv, (next_argc + 1 + 2 + mworker_child_nb() + nb_oldpids + 1) * sizeof(char *));
William Lallemand73b85e72017-06-01 17:38:51 +0200804 if (next_argv == NULL)
805 goto alloc_error;
806
William Lallemand73b85e72017-06-01 17:38:51 +0200807 /* add -sf <PID>* to argv */
William Lallemand3f128872019-04-01 11:29:59 +0200808 if (mworker_child_nb() > 0) {
809 struct mworker_proc *child;
810
William Lallemand73b85e72017-06-01 17:38:51 +0200811 next_argv[next_argc++] = "-sf";
William Lallemand3f128872019-04-01 11:29:59 +0200812
813 list_for_each_entry(child, &proc_list, list) {
William Lallemand677e2f22019-11-19 17:04:18 +0100814 if (!(child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) || child->pid <= -1 )
William Lallemand3f128872019-04-01 11:29:59 +0200815 continue;
816 next_argv[next_argc] = memprintf(&msg, "%d", child->pid);
William Lallemand73b85e72017-06-01 17:38:51 +0200817 if (next_argv[next_argc] == NULL)
818 goto alloc_error;
819 msg = NULL;
William Lallemand3f128872019-04-01 11:29:59 +0200820 next_argc++;
William Lallemand73b85e72017-06-01 17:38:51 +0200821 }
822 }
William Lallemand3f128872019-04-01 11:29:59 +0200823
William Lallemand73b85e72017-06-01 17:38:51 +0200824 next_argv[next_argc] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200825
William Lallemand2bf6d622017-06-20 11:20:23 +0200826 /* add the -x option with the stat socket */
William Lallemand85b0bd92017-06-01 17:38:53 +0200827 if (cur_unixsocket) {
828
William Lallemand2bf6d622017-06-20 11:20:23 +0200829 next_argv[next_argc++] = "-x";
830 next_argv[next_argc++] = (char *)cur_unixsocket;
831 next_argv[next_argc++] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200832 }
833
Christopher Faulet767a84b2017-11-24 16:50:31 +0100834 ha_warning("Reexecuting Master process\n");
Willy Tarreaue0d86e22019-08-26 10:37:39 +0200835 signal(SIGPROF, SIG_IGN);
Tim Duesterhus0436ab72017-11-12 17:39:18 +0100836 execvp(next_argv[0], next_argv);
William Lallemand73b85e72017-06-01 17:38:51 +0200837
Christopher Faulet767a84b2017-11-24 16:50:31 +0100838 ha_warning("Failed to reexecute the master process [%d]: %s\n", pid, strerror(errno));
William Lallemand722d4ca2017-11-15 19:02:55 +0100839 return;
840
William Lallemand73b85e72017-06-01 17:38:51 +0200841alloc_error:
Joseph Herlant07a08342018-11-15 10:43:05 -0800842 ha_warning("Failed to reexecute the master process [%d]: Cannot allocate memory\n", pid);
William Lallemand73b85e72017-06-01 17:38:51 +0200843 return;
844}
845
William Lallemandb3f2be32018-09-11 10:06:18 +0200846static void mworker_loop()
847{
848
849#if defined(USE_SYSTEMD)
850 if (global.tune.options & GTUNE_USE_SYSTEMD)
851 sd_notifyf(0, "READY=1\nMAINPID=%lu", (unsigned long)getpid());
852#endif
Willy Tarreaud83b6c12019-04-18 11:31:36 +0200853 /* Busy polling makes no sense in the master :-) */
854 global.tune.options &= ~GTUNE_BUSY_POLLING;
William Lallemandb3f2be32018-09-11 10:06:18 +0200855
William Lallemandbc193052018-09-11 10:06:26 +0200856 master = 1;
857
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100858 signal_unregister(SIGTTIN);
859 signal_unregister(SIGTTOU);
William Lallemand0564d412018-11-20 17:36:53 +0100860 signal_unregister(SIGUSR1);
861 signal_unregister(SIGHUP);
862 signal_unregister(SIGQUIT);
863
William Lallemandb3f2be32018-09-11 10:06:18 +0200864 signal_register_fct(SIGTERM, mworker_catch_sigterm, SIGTERM);
865 signal_register_fct(SIGUSR1, mworker_catch_sigterm, SIGUSR1);
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100866 signal_register_fct(SIGTTIN, mworker_broadcast_signal, SIGTTIN);
867 signal_register_fct(SIGTTOU, mworker_broadcast_signal, SIGTTOU);
William Lallemandb3f2be32018-09-11 10:06:18 +0200868 signal_register_fct(SIGINT, mworker_catch_sigterm, SIGINT);
869 signal_register_fct(SIGHUP, mworker_catch_sighup, SIGHUP);
870 signal_register_fct(SIGUSR2, mworker_catch_sighup, SIGUSR2);
871 signal_register_fct(SIGCHLD, mworker_catch_sigchld, SIGCHLD);
872
873 mworker_unblock_signals();
874 mworker_cleanlisteners();
William Lallemand27f3fa52018-12-06 14:05:20 +0100875 mworker_cleantasks();
William Lallemandb3f2be32018-09-11 10:06:18 +0200876
William Lallemandbc193052018-09-11 10:06:26 +0200877 mworker_catch_sigchld(NULL); /* ensure we clean the children in case
878 some SIGCHLD were lost */
879
William Lallemandb3f2be32018-09-11 10:06:18 +0200880 global.nbthread = 1;
881 relative_pid = 1;
882 pid_bit = 1;
Willy Tarreaua38a7172019-02-02 17:11:28 +0100883 all_proc_mask = 1;
William Lallemandb3f2be32018-09-11 10:06:18 +0200884
William Lallemand2672eb92018-12-14 15:52:39 +0100885#ifdef USE_THREAD
886 tid_bit = 1;
887 all_threads_mask = 1;
888#endif
889
William Lallemandb3f2be32018-09-11 10:06:18 +0200890 jobs++; /* this is the "master" job, we want to take care of the
891 signals even if there is no listener so the poll loop don't
892 leave */
893
894 fork_poller();
Willy Tarreaub4f7cc32019-05-03 09:27:30 +0200895 run_thread_poll_loop(0);
William Lallemandb3f2be32018-09-11 10:06:18 +0200896}
William Lallemandcb11fd22017-06-01 17:38:52 +0200897
898/*
899 * Reexec the process in failure mode, instead of exiting
900 */
901void reexec_on_failure()
902{
903 if (!atexit_flag)
904 return;
905
906 setenv("HAPROXY_MWORKER_WAIT_ONLY", "1", 1);
907
Christopher Faulet767a84b2017-11-24 16:50:31 +0100908 ha_warning("Reexecuting Master process in waitpid mode\n");
William Lallemandcb11fd22017-06-01 17:38:52 +0200909 mworker_reload();
William Lallemandcb11fd22017-06-01 17:38:52 +0200910}
William Lallemand73b85e72017-06-01 17:38:51 +0200911
912
913/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200914 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
915 * a signal zero to all subscribers. This means that it's as easy as
916 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200917 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100918static void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200919{
920 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200921 signal_unregister_handler(sh);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100922 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200923}
924
925/*
926 * upon SIGTTOU, we pause everything
927 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100928static void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929{
930 pause_proxies();
Willy Tarreaubafbe012017-11-24 17:34:44 +0100931 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932}
933
934/*
935 * upon SIGTTIN, let's have a soft stop.
936 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100937static void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938{
Willy Tarreaube58c382011-07-24 18:28:10 +0200939 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200940}
941
942/*
943 * this function dumps every server's state when the process receives SIGHUP.
944 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100945static void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200946{
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100947 struct proxy *p = proxies_list;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948
Christopher Faulet767a84b2017-11-24 16:50:31 +0100949 ha_warning("SIGHUP received, dumping servers states.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950 while (p) {
951 struct server *s = p->srv;
952
953 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
954 while (s) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100955 chunk_printf(&trash,
956 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
957 p->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +0200958 (s->cur_state != SRV_ST_STOPPED) ? "UP" : "DOWN",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100959 s->cur_sess, s->nbpend, s->counters.cum_sess);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200960 ha_warning("%s\n", trash.area);
961 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200962 s = s->next;
963 }
964
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200965 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
966 if (!p->srv) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100967 chunk_printf(&trash,
968 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
969 p->id,
970 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 +0200971 } else if (p->srv_act == 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100972 chunk_printf(&trash,
973 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
974 p->id,
975 (p->srv_bck) ? "is running on backup servers" : "has no server available",
976 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 +0200977 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100978 chunk_printf(&trash,
979 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
980 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
981 p->id, p->srv_act, p->srv_bck,
982 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 +0200983 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200984 ha_warning("%s\n", trash.area);
985 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200986
987 p = p->next;
988 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200989}
990
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100991static void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200992{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200993 /* dump memory usage then free everything possible */
994 dump_pools();
Willy Tarreaubafbe012017-11-24 17:34:44 +0100995 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200996}
997
William Lallemande1340412017-12-28 16:09:36 +0100998/*
999 * This function dup2 the stdio FDs (0,1,2) with <fd>, then closes <fd>
1000 * If <fd> < 0, it opens /dev/null and use it to dup
1001 *
1002 * In the case of chrooting, you have to open /dev/null before the chroot, and
1003 * pass the <fd> to this function
1004 */
1005static void stdio_quiet(int fd)
1006{
1007 if (fd < 0)
1008 fd = open("/dev/null", O_RDWR, 0);
1009
1010 if (fd > -1) {
1011 fclose(stdin);
1012 fclose(stdout);
1013 fclose(stderr);
1014
1015 dup2(fd, 0);
1016 dup2(fd, 1);
1017 dup2(fd, 2);
1018 if (fd > 2)
1019 close(fd);
1020 return;
1021 }
1022
1023 ha_alert("Cannot open /dev/null\n");
1024 exit(EXIT_FAILURE);
1025}
1026
1027
Joseph Herlant03420902018-11-15 10:41:50 -08001028/* This function checks if cfg_cfgfiles contains directories.
1029 * If it finds one, it adds all the files (and only files) it contains
1030 * in cfg_cfgfiles in place of the directory (and removes the directory).
1031 * It adds the files in lexical order.
1032 * It adds only files with .cfg extension.
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001033 * It doesn't add files with name starting with '.'
1034 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001035static void cfgfiles_expand_directories(void)
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001036{
1037 struct wordlist *wl, *wlb;
1038 char *err = NULL;
1039
1040 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
1041 struct stat file_stat;
1042 struct dirent **dir_entries = NULL;
1043 int dir_entries_nb;
1044 int dir_entries_it;
1045
1046 if (stat(wl->s, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001047 ha_alert("Cannot open configuration file/directory %s : %s\n",
1048 wl->s,
1049 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001050 exit(1);
1051 }
1052
1053 if (!S_ISDIR(file_stat.st_mode))
1054 continue;
1055
1056 /* from this point wl->s is a directory */
1057
1058 dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort);
1059 if (dir_entries_nb < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001060 ha_alert("Cannot open configuration directory %s : %s\n",
1061 wl->s,
1062 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001063 exit(1);
1064 }
1065
1066 /* for each element in the directory wl->s */
1067 for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) {
1068 struct dirent *dir_entry = dir_entries[dir_entries_it];
1069 char *filename = NULL;
1070 char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg");
1071
1072 /* don't add filename that begin with .
Joseph Herlant03420902018-11-15 10:41:50 -08001073 * only add filename with .cfg extension
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001074 */
1075 if (dir_entry->d_name[0] == '.' ||
1076 !(d_name_cfgext && d_name_cfgext[4] == '\0'))
1077 goto next_dir_entry;
1078
1079 if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001080 ha_alert("Cannot load configuration files %s : out of memory.\n",
1081 filename);
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001082 exit(1);
1083 }
1084
1085 if (stat(filename, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001086 ha_alert("Cannot open configuration file %s : %s\n",
1087 wl->s,
1088 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001089 exit(1);
1090 }
1091
1092 /* don't add anything else than regular file in cfg_cfgfiles
1093 * this way we avoid loops
1094 */
1095 if (!S_ISREG(file_stat.st_mode))
1096 goto next_dir_entry;
1097
1098 if (!list_append_word(&wl->list, filename, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001099 ha_alert("Cannot load configuration files %s : %s\n",
1100 filename,
1101 err);
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001102 exit(1);
1103 }
1104
1105next_dir_entry:
1106 free(filename);
1107 free(dir_entry);
1108 }
1109
1110 free(dir_entries);
1111
1112 /* remove the current directory (wl) from cfg_cfgfiles */
1113 free(wl->s);
1114 LIST_DEL(&wl->list);
1115 free(wl);
1116 }
1117
1118 free(err);
1119}
1120
Olivier Houchardf73629d2017-04-05 22:33:04 +02001121static int get_old_sockets(const char *unixsocket)
1122{
1123 char *cmsgbuf = NULL, *tmpbuf = NULL;
1124 int *tmpfd = NULL;
1125 struct sockaddr_un addr;
1126 struct cmsghdr *cmsg;
1127 struct msghdr msghdr;
1128 struct iovec iov;
1129 struct xfer_sock_list *xfer_sock = NULL;
Olivier Houchard54740872017-04-06 14:45:14 +02001130 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf73629d2017-04-05 22:33:04 +02001131 int sock = -1;
1132 int ret = -1;
1133 int ret2 = -1;
1134 int fd_nb;
1135 int got_fd = 0;
1136 int i = 0;
1137 size_t maxoff = 0, curoff = 0;
1138
1139 memset(&msghdr, 0, sizeof(msghdr));
1140 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
1141 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001142 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001143 goto out;
1144 }
1145 sock = socket(PF_UNIX, SOCK_STREAM, 0);
1146 if (sock < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001147 ha_warning("Failed to connect to the old process socket '%s'\n",
1148 unixsocket);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001149 goto out;
1150 }
Willy Tarreau719e07c2019-12-11 16:29:10 +01001151 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path) - 1);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001152 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
1153 addr.sun_family = PF_UNIX;
1154 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
1155 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001156 ha_warning("Failed to connect to the old process socket '%s'\n",
1157 unixsocket);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001158 goto out;
1159 }
Olivier Houchard54740872017-04-06 14:45:14 +02001160 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001161 iov.iov_base = &fd_nb;
1162 iov.iov_len = sizeof(fd_nb);
1163 msghdr.msg_iov = &iov;
1164 msghdr.msg_iovlen = 1;
1165 send(sock, "_getsocks\n", strlen("_getsocks\n"), 0);
1166 /* First, get the number of file descriptors to be received */
1167 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001168 ha_warning("Failed to get the number of sockets to be transferred !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001169 goto out;
1170 }
1171 if (fd_nb == 0) {
1172 ret = 0;
1173 goto out;
1174 }
Olivier Houchardf143b802017-11-04 15:13:01 +01001175 tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001176 if (tmpbuf == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001177 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001178 goto out;
1179 }
1180 tmpfd = malloc(fd_nb * sizeof(int));
1181 if (tmpfd == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001182 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001183 goto out;
1184 }
1185 msghdr.msg_control = cmsgbuf;
1186 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
Olivier Houchardf143b802017-11-04 15:13:01 +01001187 iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001188 do {
1189 int ret3;
1190
1191 iov.iov_base = tmpbuf + curoff;
1192 ret = recvmsg(sock, &msghdr, 0);
1193 if (ret == -1 && errno == EINTR)
1194 continue;
1195 if (ret <= 0)
1196 break;
1197 /* Send an ack to let the sender know we got the sockets
1198 * and it can send some more
1199 */
1200 do {
1201 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
1202 } while (ret3 == -1 && errno == EINTR);
1203 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
1204 cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
1205 if (cmsg->cmsg_level == SOL_SOCKET &&
1206 cmsg->cmsg_type == SCM_RIGHTS) {
1207 size_t totlen = cmsg->cmsg_len -
1208 CMSG_LEN(0);
1209 if (totlen / sizeof(int) + got_fd > fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001210 ha_warning("Got to many sockets !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001211 goto out;
1212 }
1213 /*
1214 * Be paranoid and use memcpy() to avoid any
1215 * potential alignement issue.
1216 */
1217 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
1218 got_fd += totlen / sizeof(int);
1219 }
1220 }
1221 curoff += ret;
1222 } while (got_fd < fd_nb);
1223
1224 if (got_fd != fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001225 ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
1226 fd_nb, got_fd);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001227 goto out;
1228 }
1229 maxoff = curoff;
1230 curoff = 0;
1231 for (i = 0; i < got_fd; i++) {
1232 int fd = tmpfd[i];
1233 socklen_t socklen;
1234 int len;
1235
1236 xfer_sock = calloc(1, sizeof(*xfer_sock));
1237 if (!xfer_sock) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001238 ha_warning("Failed to allocate memory in get_old_sockets() !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001239 break;
1240 }
1241 xfer_sock->fd = -1;
1242
1243 socklen = sizeof(xfer_sock->addr);
1244 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001245 ha_warning("Failed to get socket address\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001246 free(xfer_sock);
Olivier Houchardbe7b1ce2017-07-17 17:25:33 +02001247 xfer_sock = NULL;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001248 continue;
1249 }
1250 if (curoff >= maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001251 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001252 goto out;
1253 }
1254 len = tmpbuf[curoff++];
1255 if (len > 0) {
1256 /* We have a namespace */
1257 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001258 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001259 goto out;
1260 }
1261 xfer_sock->namespace = malloc(len + 1);
1262 if (!xfer_sock->namespace) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001263 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001264 goto out;
1265 }
1266 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
1267 xfer_sock->namespace[len] = 0;
1268 curoff += len;
1269 }
1270 if (curoff >= maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001271 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001272 goto out;
1273 }
1274 len = tmpbuf[curoff++];
1275 if (len > 0) {
1276 /* We have an interface */
1277 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001278 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001279 goto out;
1280 }
1281 xfer_sock->iface = malloc(len + 1);
1282 if (!xfer_sock->iface) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001283 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001284 goto out;
1285 }
1286 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
Olivier Houchard33e083c2018-03-15 17:48:49 +01001287 xfer_sock->iface[len] = 0;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001288 curoff += len;
1289 }
1290 if (curoff + sizeof(int) > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001291 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001292 goto out;
1293 }
1294 memcpy(&xfer_sock->options, &tmpbuf[curoff],
1295 sizeof(xfer_sock->options));
1296 curoff += sizeof(xfer_sock->options);
1297
1298 xfer_sock->fd = fd;
1299 if (xfer_sock_list)
1300 xfer_sock_list->prev = xfer_sock;
1301 xfer_sock->next = xfer_sock_list;
1302 xfer_sock->prev = NULL;
1303 xfer_sock_list = xfer_sock;
1304 xfer_sock = NULL;
1305 }
1306
1307 ret2 = 0;
1308out:
1309 /* If we failed midway make sure to close the remaining
1310 * file descriptors
1311 */
1312 if (tmpfd != NULL && i < got_fd) {
1313 for (; i < got_fd; i++) {
1314 close(tmpfd[i]);
1315 }
1316 }
1317 free(tmpbuf);
1318 free(tmpfd);
1319 free(cmsgbuf);
1320 if (sock != -1)
1321 close(sock);
1322 if (xfer_sock) {
1323 free(xfer_sock->namespace);
1324 free(xfer_sock->iface);
1325 if (xfer_sock->fd != -1)
1326 close(xfer_sock->fd);
1327 free(xfer_sock);
1328 }
1329 return (ret2);
1330}
1331
Willy Tarreaubaaee002006-06-26 02:48:02 +02001332/*
William Lallemand73b85e72017-06-01 17:38:51 +02001333 * copy and cleanup the current argv
1334 * Remove the -sf /-st parameters
1335 * Return an allocated copy of argv
1336 */
1337
1338static char **copy_argv(int argc, char **argv)
1339{
1340 char **newargv;
William Lallemand2bf6d622017-06-20 11:20:23 +02001341 int i = 0, j = 0;
William Lallemand73b85e72017-06-01 17:38:51 +02001342
1343 newargv = calloc(argc + 2, sizeof(char *));
1344 if (newargv == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001345 ha_warning("Cannot allocate memory\n");
William Lallemand73b85e72017-06-01 17:38:51 +02001346 return NULL;
1347 }
1348
William Lallemand2bf6d622017-06-20 11:20:23 +02001349 while (i < argc) {
1350 /* -sf or -st or -x */
William Lallemand29f690c2018-01-09 23:12:27 +01001351 if (i > 0 && argv[i][0] == '-' &&
1352 ((argv[i][1] == 's' && (argv[i][2] == 'f' || argv[i][2] == 't')) || argv[i][1] == 'x' )) {
William Lallemand2bf6d622017-06-20 11:20:23 +02001353 /* list of pids to finish ('f') or terminate ('t') or unix socket (-x) */
William Lallemand73b85e72017-06-01 17:38:51 +02001354 i++;
1355 while (i < argc && argv[i][0] != '-') {
1356 i++;
1357 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001358 continue;
William Lallemand73b85e72017-06-01 17:38:51 +02001359 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001360
1361 newargv[j++] = argv[i++];
William Lallemand73b85e72017-06-01 17:38:51 +02001362 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001363
William Lallemand73b85e72017-06-01 17:38:51 +02001364 return newargv;
1365}
1366
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001367
Willy Tarreau1c306aa2020-03-06 19:04:55 +01001368/* Initializes the per-thread, per-process random seed for use with random_r().
1369 *
1370 * We cannot pass a global state from one thread to another one because we
1371 * must still call initstate_r() on it to reset the per-thread pointer, and
1372 * this will reinitialize our state. What we do instead is that we use the
1373 * *same* seed for all threads so that they start with the exact same internal
1374 * state, and will loop over random() a different (and large) number of times
1375 * to make sure their internal state is totally different. This results in 4
1376 * billion possible *boot* sequences, and each thread may start with a much
1377 * greater number of sequences as well (we typically add up to 20 bits, giving
1378 * 4 trillon possible initial sequences).
1379 */
1380static void ha_random_init_per_thread()
1381{
1382 unsigned int seed;
1383 unsigned int loops;
1384 uint64_t u64;
1385
1386 /* recreate a distinct initial state for each process/thread */
1387 seed = read_u32(boot_seed);
1388
1389 /* start with a strictly different seed per thread/process */
1390 seed += (relative_pid * MAX_THREADS)+ tid;
1391
1392 memset(&ha_rand_data, 0, sizeof(ha_rand_data));
1393 initstate_r(seed, ha_rand_state, sizeof(ha_rand_state), &ha_rand_data);
1394
1395 /* make sure all pids and tids have a different count, we'll
1396 * loop up to ~1 million times on each thread, with a fairly
1397 * different number for each. This should only take a few ms
1398 * per thread and will provide ~20 extra bits of randomness
1399 * to each thread/process, resulting in ~52 bits per thread per
1400 * boot.
1401 */
1402 loops = read_u32(boot_seed);
1403
1404 u64 = read_u64(boot_seed + 4);
1405 u64 = (u64 << relative_pid) | (u64 >> (63-relative_pid));
1406 loops ^= u64 ^ (u64 >> 32);
1407
1408 u64 = read_u64(boot_seed + 12);
1409 u64 = (u64 << tid) | (u64 >> (63-tid));
1410 loops ^= u64 ^ (u64 >> 32);
1411 loops %= 1048573;
1412
1413 /* burn some randoms to mix the internal state */
1414 while (loops--) {
1415 int32_t drop;
1416
1417 (void)random_r(&ha_rand_data, &drop);
1418 }
1419}
1420
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001421/* Performs basic random seed initialization. The main issue with this is that
1422 * srandom_r() only takes 32 bits and purposely provides a reproducible sequence,
1423 * which means that there will only be 4 billion possible random sequences once
1424 * srandom() is called, regardless of the internal state. Not calling it is
1425 * even worse as we'll always produce the same randoms sequences. What we do
1426 * here is to create an initial sequence from various entropy sources, hash it
1427 * using SHA1 and keep the resulting 160 bits available globally.
1428 *
1429 * We initialize the current process with the first 32 bits before starting the
1430 * polling loop, where all this will be changed to have process specific and
1431 * thread specific sequences.
Willy Tarreau1c306aa2020-03-06 19:04:55 +01001432 *
1433 * Before starting threads, it's still possible to call random() as srandom()
1434 * is initialized from this, but after threads and/or processes are started,
1435 * only ha_random() is expected to be used to guarantee distinct sequences.
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001436 */
1437static void ha_random_boot(char *const *argv)
1438{
1439 unsigned char message[256];
1440 unsigned char *m = message;
1441 struct timeval tv;
1442 blk_SHA_CTX ctx;
1443 unsigned long l;
1444 int fd;
1445 int i;
1446
1447 /* start with current time as pseudo-random seed */
1448 gettimeofday(&tv, NULL);
1449 write_u32(m, tv.tv_sec); m += 4;
1450 write_u32(m, tv.tv_usec); m += 4;
1451
1452 /* PID and PPID add some OS-based randomness */
1453 write_u16(m, getpid()); m += 2;
1454 write_u16(m, getppid()); m += 2;
1455
1456 /* take up to 160 bits bytes from /dev/urandom if available (non-blocking) */
1457 fd = open("/dev/urandom", O_RDONLY);
1458 if (fd >= 0) {
1459 i = read(fd, m, 20);
1460 if (i > 0)
1461 m += i;
1462 close(fd);
1463 }
1464
1465 /* take up to 160 bits bytes from openssl (non-blocking) */
1466#ifdef USE_OPENSSL
1467 if (RAND_bytes(m, 20) == 1)
1468 m += 20;
1469#endif
1470
1471 /* take 160 bits from existing random in case it was already initialized */
1472 for (i = 0; i < 5; i++) {
1473 write_u32(m, random());
1474 m += 4;
1475 }
1476
1477 /* stack address (benefit form operating system's ASLR) */
1478 l = (unsigned long)&m;
1479 memcpy(m, &l, sizeof(l)); m += sizeof(l);
1480
1481 /* argv address (benefit form operating system's ASLR) */
1482 l = (unsigned long)&argv;
1483 memcpy(m, &l, sizeof(l)); m += sizeof(l);
1484
1485 /* use tv_usec again after all the operations above */
1486 gettimeofday(&tv, NULL);
1487 write_u32(m, tv.tv_usec); m += 4;
1488
1489 /*
1490 * At this point, ~84-92 bytes have been used
1491 */
1492
1493 /* finish with the hostname */
1494 strncpy((char *)m, hostname, message + sizeof(message) - m);
1495 m += strlen(hostname);
1496
1497 /* total message length */
1498 l = m - message;
1499
1500 memset(&ctx, 0, sizeof(ctx));
1501 blk_SHA1_Init(&ctx);
1502 blk_SHA1_Update(&ctx, message, l);
1503 blk_SHA1_Final(boot_seed, &ctx);
1504
1505 srandom(read_u32(boot_seed));
Willy Tarreau1c306aa2020-03-06 19:04:55 +01001506 ha_random_init_per_thread();
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001507}
1508
Willy Tarreau5a023f02019-03-01 14:19:31 +01001509/* considers splicing proxies' maxconn, computes the ideal global.maxpipes
1510 * setting, and returns it. It may return -1 meaning "unlimited" if some
1511 * unlimited proxies have been found and the global.maxconn value is not yet
1512 * set. It may also return a value greater than maxconn if it's not yet set.
1513 * Note that a value of zero means there is no need for pipes. -1 is never
1514 * returned if global.maxconn is valid.
1515 */
1516static int compute_ideal_maxpipes()
1517{
1518 struct proxy *cur;
1519 int nbfe = 0, nbbe = 0;
1520 int unlimited = 0;
1521 int pipes;
1522 int max;
1523
1524 for (cur = proxies_list; cur; cur = cur->next) {
1525 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
1526 if (cur->cap & PR_CAP_FE) {
1527 max = cur->maxconn;
1528 nbfe += max;
1529 if (!max) {
1530 unlimited = 1;
1531 break;
1532 }
1533 }
1534 if (cur->cap & PR_CAP_BE) {
1535 max = cur->fullconn ? cur->fullconn : global.maxconn;
1536 nbbe += max;
1537 if (!max) {
1538 unlimited = 1;
1539 break;
1540 }
1541 }
1542 }
1543 }
1544
1545 pipes = MAX(nbfe, nbbe);
1546 if (global.maxconn) {
1547 if (pipes > global.maxconn || unlimited)
1548 pipes = global.maxconn;
1549 } else if (unlimited) {
1550 pipes = -1;
1551 }
1552
1553 return pipes >= 4 ? pipes / 4 : pipes;
1554}
1555
Willy Tarreauac350932019-03-01 15:43:14 +01001556/* considers global.maxsocks, global.maxpipes, async engines, SSL frontends and
1557 * rlimits and computes an ideal maxconn. It's meant to be called only when
1558 * maxsock contains the sum of listening FDs, before it is updated based on
Willy Tarreaudf23c0c2019-03-13 10:10:49 +01001559 * maxconn and pipes. If there are not enough FDs left, DEFAULT_MAXCONN (by
1560 * default 100) is returned as it is expected that it will even run on tight
1561 * environments, and will maintain compatibility with previous packages that
1562 * used to rely on this value as the default one. The system will emit a
1563 * warning indicating how many FDs are missing anyway if needed.
Willy Tarreauac350932019-03-01 15:43:14 +01001564 */
1565static int compute_ideal_maxconn()
1566{
1567 int ssl_sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1568 int engine_fds = global.ssl_used_async_engines * ssl_sides;
1569 int pipes = compute_ideal_maxpipes();
Willy Tarreaub1beaa32020-03-06 10:25:31 +01001570 int remain = MAX(rlim_fd_cur_at_boot, rlim_fd_max_at_boot);
Willy Tarreauac350932019-03-01 15:43:14 +01001571 int maxconn;
1572
1573 /* we have to take into account these elements :
1574 * - number of engine_fds, which inflates the number of FD needed per
1575 * connection by this number.
1576 * - number of pipes per connection on average : for the unlimited
1577 * case, this is 0.5 pipe FDs per connection, otherwise it's a
1578 * fixed value of 2*pipes.
1579 * - two FDs per connection
1580 */
1581
1582 /* subtract listeners and checks */
1583 remain -= global.maxsock;
1584
Willy Tarreau3f200852019-03-14 19:13:17 +01001585 /* one epoll_fd/kqueue_fd per thread */
1586 remain -= global.nbthread;
1587
1588 /* one wake-up pipe (2 fd) per thread */
1589 remain -= 2 * global.nbthread;
1590
Willy Tarreauac350932019-03-01 15:43:14 +01001591 /* Fixed pipes values : we only subtract them if they're not larger
1592 * than the remaining FDs because pipes are optional.
1593 */
1594 if (pipes >= 0 && pipes * 2 < remain)
1595 remain -= pipes * 2;
1596
1597 if (pipes < 0) {
1598 /* maxsock = maxconn * 2 + maxconn/4 * 2 + maxconn * engine_fds.
1599 * = maxconn * (2 + 0.5 + engine_fds)
1600 * = maxconn * (4 + 1 + 2*engine_fds) / 2
1601 */
1602 maxconn = 2 * remain / (5 + 2 * engine_fds);
1603 } else {
1604 /* maxsock = maxconn * 2 + maxconn * engine_fds.
1605 * = maxconn * (2 + engine_fds)
1606 */
1607 maxconn = remain / (2 + engine_fds);
1608 }
1609
Willy Tarreaudf23c0c2019-03-13 10:10:49 +01001610 return MAX(maxconn, DEFAULT_MAXCONN);
Willy Tarreauac350932019-03-01 15:43:14 +01001611}
1612
William Lallemand73b85e72017-06-01 17:38:51 +02001613/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001614 * This function initializes all the necessary variables. It only returns
1615 * if everything is OK. If something fails, it exits.
1616 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001617static void init(int argc, char **argv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001618{
Willy Tarreaubaaee002006-06-26 02:48:02 +02001619 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001620 char *tmp;
1621 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +02001622 int err_code = 0;
Maxime de Roucy0f503922016-05-13 23:52:55 +02001623 char *err_msg = NULL;
Willy Tarreau477ecd82010-01-03 21:12:30 +01001624 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +00001625 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +02001626 char *change_dir = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001627 struct proxy *px;
Willy Tarreaue6945732016-12-21 19:57:00 +01001628 struct post_check_fct *pcf;
Willy Tarreauac350932019-03-01 15:43:14 +01001629 int ideal_maxconn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001630
Christopher Faulete3a5e352017-10-24 13:53:54 +02001631 global.mode = MODE_STARTING;
William Lallemand73b85e72017-06-01 17:38:51 +02001632 next_argv = copy_argv(argc, argv);
1633
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001634 if (!init_trash_buffers(1)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001635 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet748919a2017-07-26 14:59:46 +02001636 exit(1);
1637 }
David du Colombier7af46052012-05-16 14:16:48 +02001638
Emeric Brun2b920a12010-09-23 18:30:22 +02001639 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
1640 * the string in case of truncation, and at least FreeBSD appears not to do
1641 * it.
1642 */
1643 memset(hostname, 0, sizeof(hostname));
1644 gethostname(hostname, sizeof(hostname) - 1);
1645 memset(localpeer, 0, sizeof(localpeer));
1646 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
William Lallemanddaf4cd22018-04-17 16:46:13 +02001647 setenv("HAPROXY_LOCALPEER", localpeer, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02001648
William Lallemand24c928c2020-01-14 17:58:18 +01001649 /* we were in mworker mode, we should restart in mworker mode */
1650 if (getenv("HAPROXY_MWORKER_REEXEC") != NULL)
1651 global.mode |= MODE_MWORKER;
1652
Willy Tarreaubaaee002006-06-26 02:48:02 +02001653 /*
1654 * Initialize the previously static variables.
1655 */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001656
Willy Tarreau173d9952018-01-26 21:48:23 +01001657 totalconn = actconn = listeners = stopping = 0;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001658 killed = 0;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001659
Willy Tarreaubaaee002006-06-26 02:48:02 +02001660
1661#ifdef HAPROXY_MEMMAX
Willy Tarreau70060452015-12-14 12:46:07 +01001662 global.rlimit_memmax_all = HAPROXY_MEMMAX;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001663#endif
1664
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001665 tzset();
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001666 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001667 start_date = now;
1668
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001669 ha_random_boot(argv);
Willy Tarreau84310e22014-02-14 11:59:04 +01001670
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001671 if (init_acl() != 0)
1672 exit(1);
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001673
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001674 /* Initialise lua. */
1675 hlua_init();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001676
Christopher Fauletff2613e2016-11-09 11:36:17 +01001677 /* Initialize process vars */
1678 vars_init(&global.vars, SCOPE_PROC);
1679
Willy Tarreau43b78992009-01-25 15:42:27 +01001680 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaue5733232019-05-22 19:24:06 +02001681#if defined(USE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001682 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001683#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001684#if defined(USE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001685 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001686#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001687#if defined(USE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +01001688 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001689#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001690#if defined(USE_EVPORTS)
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +00001691 global.tune.options |= GTUNE_USE_EVPORTS;
1692#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001693#if defined(USE_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001694 global.tune.options |= GTUNE_USE_SPLICE;
1695#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001696#if defined(USE_GETADDRINFO)
1697 global.tune.options |= GTUNE_USE_GAI;
1698#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001699#if defined(SO_REUSEPORT)
1700 global.tune.options |= GTUNE_USE_REUSEPORT;
1701#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001702
1703 pid = getpid();
1704 progname = *argv;
1705 while ((tmp = strchr(progname, '/')) != NULL)
1706 progname = tmp + 1;
1707
Kevinm48936af2010-12-22 16:08:21 +00001708 /* the process name is used for the logs only */
Dragan Dosen43885c72015-10-01 13:18:13 +02001709 chunk_initstr(&global.log_tag, strdup(progname));
Kevinm48936af2010-12-22 16:08:21 +00001710
Willy Tarreaubaaee002006-06-26 02:48:02 +02001711 argc--; argv++;
1712 while (argc > 0) {
1713 char *flag;
1714
1715 if (**argv == '-') {
1716 flag = *argv+1;
1717
1718 /* 1 arg */
1719 if (*flag == 'v') {
1720 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +01001721 if (flag[1] == 'v') /* -vv */
1722 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001723 exit(0);
1724 }
Willy Tarreaue5733232019-05-22 19:24:06 +02001725#if defined(USE_EPOLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001726 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +01001727 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001728#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001729#if defined(USE_POLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001730 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +01001731 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001732#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001733#if defined(USE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001734 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +01001735 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001736#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001737#if defined(USE_EVPORTS)
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +00001738 else if (*flag == 'd' && flag[1] == 'v')
1739 global.tune.options &= ~GTUNE_USE_EVPORTS;
1740#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001741#if defined(USE_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001742 else if (*flag == 'd' && flag[1] == 'S')
1743 global.tune.options &= ~GTUNE_USE_SPLICE;
1744#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001745#if defined(USE_GETADDRINFO)
1746 else if (*flag == 'd' && flag[1] == 'G')
1747 global.tune.options &= ~GTUNE_USE_GAI;
1748#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001749#if defined(SO_REUSEPORT)
1750 else if (*flag == 'd' && flag[1] == 'R')
1751 global.tune.options &= ~GTUNE_USE_REUSEPORT;
1752#endif
Emeric Brun850efd52014-01-29 12:24:34 +01001753 else if (*flag == 'd' && flag[1] == 'V')
1754 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001755 else if (*flag == 'V')
1756 arg_mode |= MODE_VERBOSE;
1757 else if (*flag == 'd' && flag[1] == 'b')
1758 arg_mode |= MODE_FOREGROUND;
Willy Tarreau6e064432012-05-08 15:40:42 +02001759 else if (*flag == 'd' && flag[1] == 'M')
1760 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreau3eed10e2016-11-07 21:03:16 +01001761 else if (*flag == 'd' && flag[1] == 'r')
1762 global.tune.options |= GTUNE_RESOLVE_DONTFAIL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001763 else if (*flag == 'd')
1764 arg_mode |= MODE_DEBUG;
1765 else if (*flag == 'c')
1766 arg_mode |= MODE_CHECK;
William Lallemand095ba4c2017-06-01 17:38:50 +02001767 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +02001768 arg_mode |= MODE_DAEMON;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001769 else if (*flag == 'W' && flag[1] == 's') {
Lukas Tribusf46bf952017-11-21 12:39:34 +01001770 arg_mode |= MODE_MWORKER | MODE_FOREGROUND;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001771#if defined(USE_SYSTEMD)
1772 global.tune.options |= GTUNE_USE_SYSTEMD;
1773#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001774 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 +01001775 usage(progname);
1776#endif
1777 }
William Lallemand095ba4c2017-06-01 17:38:50 +02001778 else if (*flag == 'W')
1779 arg_mode |= MODE_MWORKER;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001780 else if (*flag == 'q')
1781 arg_mode |= MODE_QUIET;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001782 else if (*flag == 'x') {
William Lallemand45eff442017-06-19 15:57:55 +02001783 if (argc <= 1 || argv[1][0] == '-') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001784 ha_alert("Unix socket path expected with the -x flag\n\n");
William Lallemand45eff442017-06-19 15:57:55 +02001785 usage(progname);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001786 }
William Lallemand4fc09692017-06-19 16:37:19 +02001787 if (old_unixsocket)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001788 ha_warning("-x option already set, overwriting the value\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001789 old_unixsocket = argv[1];
William Lallemand4fc09692017-06-19 16:37:19 +02001790
Olivier Houchardf73629d2017-04-05 22:33:04 +02001791 argv++;
1792 argc--;
1793 }
William Lallemande7361152018-10-26 14:47:36 +02001794 else if (*flag == 'S') {
1795 struct wordlist *c;
1796
1797 if (argc <= 1 || argv[1][0] == '-') {
1798 ha_alert("Socket and optional bind parameters expected with the -S flag\n");
1799 usage(progname);
1800 }
1801 if ((c = malloc(sizeof(*c))) == NULL || (c->s = strdup(argv[1])) == NULL) {
1802 ha_alert("Cannot allocate memory\n");
1803 exit(EXIT_FAILURE);
1804 }
1805 LIST_ADD(&mworker_cli_conf, &c->list);
1806
1807 argv++;
1808 argc--;
1809 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001810 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1811 /* list of pids to finish ('f') or terminate ('t') */
1812
1813 if (flag[1] == 'f')
1814 oldpids_sig = SIGUSR1; /* finish then exit */
1815 else
1816 oldpids_sig = SIGTERM; /* terminate immediately */
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001817 while (argc > 1 && argv[1][0] != '-') {
Chris Lane236062f2018-02-05 23:15:44 +00001818 char * endptr = NULL;
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001819 oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int));
1820 if (!oldpids) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001821 ha_alert("Cannot allocate old pid : out of memory.\n");
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001822 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001823 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001824 argc--; argv++;
Chris Lane236062f2018-02-05 23:15:44 +00001825 errno = 0;
1826 oldpids[nb_oldpids] = strtol(*argv, &endptr, 10);
1827 if (errno) {
1828 ha_alert("-%2s option: failed to parse {%s}: %s\n",
1829 flag,
1830 *argv, strerror(errno));
1831 exit(1);
1832 } else if (endptr && strlen(endptr)) {
Willy Tarreau90807112020-02-25 08:16:33 +01001833 while (isspace((unsigned char)*endptr)) endptr++;
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001834 if (*endptr != 0) {
Chris Lane236062f2018-02-05 23:15:44 +00001835 ha_alert("-%2s option: some bytes unconsumed in PID list {%s}\n",
1836 flag, endptr);
1837 exit(1);
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001838 }
Chris Lane236062f2018-02-05 23:15:44 +00001839 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001840 if (oldpids[nb_oldpids] <= 0)
1841 usage(progname);
1842 nb_oldpids++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001843 }
1844 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001845 else if (flag[0] == '-' && flag[1] == 0) { /* "--" */
1846 /* now that's a cfgfile list */
1847 argv++; argc--;
1848 while (argc > 0) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02001849 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001850 ha_alert("Cannot load configuration file/directory %s : %s\n",
1851 *argv,
1852 err_msg);
Willy Tarreaua088d312015-10-08 11:58:48 +02001853 exit(1);
1854 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001855 argv++; argc--;
1856 }
1857 break;
1858 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001859 else { /* >=2 args */
1860 argv++; argc--;
1861 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001862 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001863
1864 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +02001865 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001866 case 'n' : cfg_maxconn = atol(*argv); break;
Willy Tarreau70060452015-12-14 12:46:07 +01001867 case 'm' : global.rlimit_memmax_all = atol(*argv); break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001868 case 'N' : cfg_maxpconn = atol(*argv); break;
William Lallemanddaf4cd22018-04-17 16:46:13 +02001869 case 'L' :
1870 strncpy(localpeer, *argv, sizeof(localpeer) - 1);
1871 setenv("HAPROXY_LOCALPEER", localpeer, 1);
1872 break;
Willy Tarreau5d01a632009-06-22 16:02:30 +02001873 case 'f' :
Maxime de Roucy0f503922016-05-13 23:52:55 +02001874 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001875 ha_alert("Cannot load configuration file/directory %s : %s\n",
1876 *argv,
1877 err_msg);
Willy Tarreau5d01a632009-06-22 16:02:30 +02001878 exit(1);
1879 }
Willy Tarreau5d01a632009-06-22 16:02:30 +02001880 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001881 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001882 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001883 }
1884 }
1885 }
1886 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001887 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001888 argv++; argc--;
1889 }
1890
Christopher Faulete3a5e352017-10-24 13:53:54 +02001891 global.mode |= (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
1892 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001893
William Lallemand944e6192018-11-21 15:48:31 +01001894 if (getenv("HAPROXY_MWORKER_WAIT_ONLY")) {
William Lallemandcb11fd22017-06-01 17:38:52 +02001895 unsetenv("HAPROXY_MWORKER_WAIT_ONLY");
William Lallemand944e6192018-11-21 15:48:31 +01001896 global.mode |= MODE_MWORKER_WAIT;
1897 global.mode &= ~MODE_MWORKER;
William Lallemandcb11fd22017-06-01 17:38:52 +02001898 }
1899
1900 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {
1901 atexit_flag = 1;
1902 atexit(reexec_on_failure);
1903 }
1904
Willy Tarreau576132e2011-09-10 19:26:56 +02001905 if (change_dir && chdir(change_dir) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001906 ha_alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
Willy Tarreau576132e2011-09-10 19:26:56 +02001907 exit(1);
1908 }
1909
Willy Tarreaubaaee002006-06-26 02:48:02 +02001910 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001911
1912 init_default_instance();
1913
William Lallemand944e6192018-11-21 15:48:31 +01001914 /* in wait mode, we don't try to read the configuration files */
1915 if (!(global.mode & MODE_MWORKER_WAIT)) {
William Lallemand7b302d82019-05-20 11:15:37 +02001916 struct buffer *trash = get_trash_chunk();
Willy Tarreauc4382422009-12-06 13:10:44 +01001917
William Lallemand944e6192018-11-21 15:48:31 +01001918 /* handle cfgfiles that are actually directories */
1919 cfgfiles_expand_directories();
1920
1921 if (LIST_ISEMPTY(&cfg_cfgfiles))
1922 usage(progname);
1923
1924
1925 list_for_each_entry(wl, &cfg_cfgfiles, list) {
1926 int ret;
1927
William Lallemand7b302d82019-05-20 11:15:37 +02001928 if (trash->data)
1929 chunk_appendf(trash, ";");
1930
1931 chunk_appendf(trash, "%s", wl->s);
1932
William Lallemand944e6192018-11-21 15:48:31 +01001933 ret = readcfgfile(wl->s);
1934 if (ret == -1) {
1935 ha_alert("Could not open configuration file %s : %s\n",
1936 wl->s, strerror(errno));
1937 exit(1);
1938 }
1939 if (ret & (ERR_ABORT|ERR_FATAL))
1940 ha_alert("Error(s) found in configuration file : %s\n", wl->s);
1941 err_code |= ret;
1942 if (err_code & ERR_ABORT)
1943 exit(1);
Willy Tarreauc4382422009-12-06 13:10:44 +01001944 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001945
William Lallemand944e6192018-11-21 15:48:31 +01001946 /* do not try to resolve arguments nor to spot inconsistencies when
1947 * the configuration contains fatal errors caused by files not found
1948 * or failed memory allocations.
1949 */
1950 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1951 ha_alert("Fatal errors found in configuration.\n");
1952 exit(1);
1953 }
William Lallemand7b302d82019-05-20 11:15:37 +02001954 if (trash->data)
1955 setenv("HAPROXY_CFGFILES", trash->area, 1);
1956
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02001957 }
William Lallemandce83b4a2018-10-26 14:47:30 +02001958 if (global.mode & MODE_MWORKER) {
1959 int proc;
William Lallemand16dd1b32018-11-19 18:46:18 +01001960 struct mworker_proc *tmproc;
1961
William Lallemand482f9a92019-04-12 16:15:00 +02001962 setenv("HAPROXY_MWORKER", "1", 1);
1963
William Lallemand16dd1b32018-11-19 18:46:18 +01001964 if (getenv("HAPROXY_MWORKER_REEXEC") == NULL) {
1965
William Lallemandf3a86832019-04-01 11:29:58 +02001966 tmproc = calloc(1, sizeof(*tmproc));
William Lallemand16dd1b32018-11-19 18:46:18 +01001967 if (!tmproc) {
1968 ha_alert("Cannot allocate process structures.\n");
1969 exit(EXIT_FAILURE);
1970 }
William Lallemand8f7069a2019-04-12 16:09:23 +02001971 tmproc->options |= PROC_O_TYPE_MASTER; /* master */
William Lallemand16dd1b32018-11-19 18:46:18 +01001972 tmproc->reloads = 0;
1973 tmproc->relative_pid = 0;
1974 tmproc->pid = pid;
1975 tmproc->timestamp = start_date.tv_sec;
1976 tmproc->ipc_fd[0] = -1;
1977 tmproc->ipc_fd[1] = -1;
1978
1979 proc_self = tmproc;
1980
1981 LIST_ADDQ(&proc_list, &tmproc->list);
1982 }
William Lallemandce83b4a2018-10-26 14:47:30 +02001983
1984 for (proc = 0; proc < global.nbproc; proc++) {
William Lallemandce83b4a2018-10-26 14:47:30 +02001985
William Lallemandf3a86832019-04-01 11:29:58 +02001986 tmproc = calloc(1, sizeof(*tmproc));
William Lallemandce83b4a2018-10-26 14:47:30 +02001987 if (!tmproc) {
1988 ha_alert("Cannot allocate process structures.\n");
1989 exit(EXIT_FAILURE);
1990 }
1991
William Lallemand8f7069a2019-04-12 16:09:23 +02001992 tmproc->options |= PROC_O_TYPE_WORKER; /* worker */
William Lallemandce83b4a2018-10-26 14:47:30 +02001993 tmproc->pid = -1;
1994 tmproc->reloads = 0;
William Lallemande3683302018-11-19 18:46:17 +01001995 tmproc->timestamp = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02001996 tmproc->relative_pid = 1 + proc;
William Lallemand550db6d2018-11-06 17:37:12 +01001997 tmproc->ipc_fd[0] = -1;
1998 tmproc->ipc_fd[1] = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02001999
2000 if (mworker_cli_sockpair_new(tmproc, proc) < 0) {
2001 exit(EXIT_FAILURE);
2002 }
2003
2004 LIST_ADDQ(&proc_list, &tmproc->list);
2005 }
William Lallemand944e6192018-11-21 15:48:31 +01002006 }
2007 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
2008 struct wordlist *it, *c;
2009
William Lallemand1b663612018-10-26 14:47:33 +02002010 mworker_env_to_proc_list(); /* get the info of the children in the env */
William Lallemand8a022572018-10-26 14:47:35 +02002011
William Lallemande7361152018-10-26 14:47:36 +02002012
William Lallemand550db6d2018-11-06 17:37:12 +01002013 if (!LIST_ISEMPTY(&mworker_cli_conf)) {
William Lallemande7361152018-10-26 14:47:36 +02002014
William Lallemand550db6d2018-11-06 17:37:12 +01002015 if (mworker_cli_proxy_create() < 0) {
William Lallemande7361152018-10-26 14:47:36 +02002016 ha_alert("Can't create the master's CLI.\n");
2017 exit(EXIT_FAILURE);
2018 }
William Lallemande7361152018-10-26 14:47:36 +02002019
William Lallemand550db6d2018-11-06 17:37:12 +01002020 list_for_each_entry_safe(c, it, &mworker_cli_conf, list) {
2021
2022 if (mworker_cli_proxy_new_listener(c->s) < 0) {
2023 ha_alert("Can't create the master's CLI.\n");
2024 exit(EXIT_FAILURE);
2025 }
2026 LIST_DEL(&c->list);
2027 free(c->s);
2028 free(c);
2029 }
2030 }
William Lallemandce83b4a2018-10-26 14:47:30 +02002031 }
2032
Willy Tarreaubb925012009-07-23 13:36:36 +02002033 err_code |= check_config_validity();
Christopher Fauletc1692962019-08-12 09:51:07 +02002034 for (px = proxies_list; px; px = px->next) {
2035 struct server *srv;
2036 struct post_proxy_check_fct *ppcf;
2037 struct post_server_check_fct *pscf;
2038
2039 list_for_each_entry(pscf, &post_server_check_list, list) {
2040 for (srv = px->srv; srv; srv = srv->next)
2041 err_code |= pscf->fct(srv);
2042 }
2043 list_for_each_entry(ppcf, &post_proxy_check_list, list)
2044 err_code |= ppcf->fct(px);
2045 }
Willy Tarreaubb925012009-07-23 13:36:36 +02002046 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002047 ha_alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002048 exit(1);
2049 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002050
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002051 err_code |= pattern_finalize_config();
2052 if (err_code & (ERR_ABORT|ERR_FATAL)) {
2053 ha_alert("Failed to finalize pattern config.\n");
2054 exit(1);
2055 }
Willy Tarreau0f936722019-04-11 14:47:08 +02002056
Willy Tarreau70060452015-12-14 12:46:07 +01002057 /* recompute the amount of per-process memory depending on nbproc and
2058 * the shared SSL cache size (allowed to exist in all processes).
2059 */
2060 if (global.rlimit_memmax_all) {
2061#if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE)
2062 int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL;
2063
2064 global.rlimit_memmax =
2065 ((((int64_t)global.rlimit_memmax_all * 1048576LL) -
2066 ssl_cache_bytes) / global.nbproc +
2067 ssl_cache_bytes + 1048575LL) / 1048576LL;
2068#else
2069 global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc;
2070#endif
2071 }
2072
Willy Tarreaue5733232019-05-22 19:24:06 +02002073#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002074 err_code |= netns_init();
2075 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002076 ha_alert("Failed to initialize namespace support.\n");
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002077 exit(1);
2078 }
2079#endif
2080
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01002081 /* Apply server states */
2082 apply_server_state();
2083
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002084 for (px = proxies_list; px; px = px->next)
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01002085 srv_compute_all_admin_states(px);
2086
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01002087 /* Apply servers' configured address */
2088 err_code |= srv_init_addr();
2089 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002090 ha_alert("Failed to initialize server(s) addr.\n");
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01002091 exit(1);
2092 }
2093
Willy Tarreaubaaee002006-06-26 02:48:02 +02002094 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +01002095 struct peers *pr;
2096 struct proxy *px;
2097
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002098 for (pr = cfg_peers; pr; pr = pr->next)
Willy Tarreau8b15ba12012-02-02 17:48:18 +01002099 if (pr->peers_fe)
2100 break;
2101
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002102 for (px = proxies_list; px; px = px->next)
Willy Tarreau4348fad2012-09-20 16:48:07 +02002103 if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners))
Willy Tarreau8b15ba12012-02-02 17:48:18 +01002104 break;
2105
2106 if (pr || px) {
2107 /* At least one peer or one listener has been found */
2108 qfprintf(stdout, "Configuration file is valid\n");
2109 exit(0);
2110 }
2111 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
2112 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002113 }
Willy Tarreaue9b26022011-08-01 20:57:55 +02002114
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002115 /* now we know the buffer size, we can initialize the channels and buffers */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002116 init_buffer();
Willy Tarreau8280d642009-09-23 23:37:52 +02002117
Willy Tarreaue6945732016-12-21 19:57:00 +01002118 list_for_each_entry(pcf, &post_check_list, list) {
2119 err_code |= pcf->fct();
2120 if (err_code & (ERR_ABORT|ERR_FATAL))
2121 exit(1);
2122 }
2123
Willy Tarreaubaaee002006-06-26 02:48:02 +02002124 if (cfg_maxconn > 0)
2125 global.maxconn = cfg_maxconn;
2126
Willy Tarreau8d687d82019-03-01 09:39:42 +01002127 if (global.stats_fe)
2128 global.maxsock += global.stats_fe->maxconn;
2129
2130 if (cfg_peers) {
2131 /* peers also need to bypass global maxconn */
2132 struct peers *p = cfg_peers;
2133
2134 for (p = cfg_peers; p; p = p->next)
2135 if (p->peers_fe)
2136 global.maxsock += p->peers_fe->maxconn;
2137 }
2138
Willy Tarreaubaaee002006-06-26 02:48:02 +02002139 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +02002140 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002141 global.pidfile = strdup(cfg_pidfile);
2142 }
2143
Willy Tarreaud0256482015-01-15 21:45:22 +01002144 /* Now we want to compute the maxconn and possibly maxsslconn values.
Willy Tarreauac350932019-03-01 15:43:14 +01002145 * It's a bit tricky. Maxconn defaults to the pre-computed value based
2146 * on rlim_fd_cur and the number of FDs in use due to the configuration,
2147 * and maxsslconn defaults to DEFAULT_MAXSSLCONN. On top of that we can
2148 * enforce a lower limit based on memmax.
Willy Tarreaud0256482015-01-15 21:45:22 +01002149 *
2150 * If memmax is set, then it depends on which values are set. If
2151 * maxsslconn is set, we use memmax to determine how many cleartext
2152 * connections may be added, and set maxconn to the sum of the two.
2153 * If maxconn is set and not maxsslconn, maxsslconn is computed from
2154 * the remaining amount of memory between memmax and the cleartext
2155 * connections. If neither are set, then it is considered that all
2156 * connections are SSL-capable, and maxconn is computed based on this,
2157 * then maxsslconn accordingly. We need to know if SSL is used on the
2158 * frontends, backends, or both, because when it's used on both sides,
2159 * we need twice the value for maxsslconn, but we only count the
2160 * handshake once since it is not performed on the two sides at the
2161 * same time (frontend-side is terminated before backend-side begins).
2162 * The SSL stack is supposed to have filled ssl_session_cost and
Willy Tarreau474b96a2015-01-28 19:03:21 +01002163 * ssl_handshake_cost during its initialization. In any case, if
2164 * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for
2165 * maxconn in order to protect the system.
Willy Tarreaud0256482015-01-15 21:45:22 +01002166 */
Willy Tarreauac350932019-03-01 15:43:14 +01002167 ideal_maxconn = compute_ideal_maxconn();
2168
Willy Tarreaud0256482015-01-15 21:45:22 +01002169 if (!global.rlimit_memmax) {
2170 if (global.maxconn == 0) {
Willy Tarreauac350932019-03-01 15:43:14 +01002171 global.maxconn = ideal_maxconn;
Willy Tarreaud0256482015-01-15 21:45:22 +01002172 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2173 fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn);
2174 }
2175 }
2176#ifdef USE_OPENSSL
2177 else if (!global.maxconn && !global.maxsslconn &&
2178 (global.ssl_used_frontend || global.ssl_used_backend)) {
2179 /* memmax is set, compute everything automatically. Here we want
2180 * to ensure that all SSL connections will be served. We take
2181 * care of the number of sides where SSL is used, and consider
2182 * the worst case : SSL used on both sides and doing a handshake
2183 * simultaneously. Note that we can't have more than maxconn
2184 * handshakes at a time by definition, so for the worst case of
2185 * two SSL conns per connection, we count a single handshake.
2186 */
2187 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2188 int64_t mem = global.rlimit_memmax * 1048576ULL;
2189
2190 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2191 mem -= global.maxzlibmem;
2192 mem = mem * MEM_USABLE_RATIO;
2193
2194 global.maxconn = mem /
Willy Tarreau87b09662015-04-03 00:22:06 +02002195 ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream
Willy Tarreaud0256482015-01-15 21:45:22 +01002196 sides * global.ssl_session_max_cost + // SSL buffers, one per side
2197 global.ssl_handshake_max_cost); // 1 handshake per connection max
2198
Willy Tarreauac350932019-03-01 15:43:14 +01002199 global.maxconn = MIN(global.maxconn, ideal_maxconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01002200 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01002201#ifdef SYSTEM_MAXCONN
Willy Tarreauca783d42019-03-13 10:03:07 +01002202 if (global.maxconn > SYSTEM_MAXCONN)
2203 global.maxconn = SYSTEM_MAXCONN;
Willy Tarreau474b96a2015-01-28 19:03:21 +01002204#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01002205 global.maxsslconn = sides * global.maxconn;
2206 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2207 fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n",
2208 global.maxconn, global.maxsslconn);
2209 }
2210 else if (!global.maxsslconn &&
2211 (global.ssl_used_frontend || global.ssl_used_backend)) {
2212 /* memmax and maxconn are known, compute maxsslconn automatically.
2213 * maxsslconn being forced, we don't know how many of it will be
2214 * on each side if both sides are being used. The worst case is
2215 * when all connections use only one SSL instance because
2216 * handshakes may be on two sides at the same time.
2217 */
2218 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2219 int64_t mem = global.rlimit_memmax * 1048576ULL;
2220 int64_t sslmem;
2221
2222 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2223 mem -= global.maxzlibmem;
2224 mem = mem * MEM_USABLE_RATIO;
2225
Willy Tarreau87b09662015-04-03 00:22:06 +02002226 sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01002227 global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost);
2228 global.maxsslconn = round_2dig(global.maxsslconn);
2229
2230 if (sslmem <= 0 || global.maxsslconn < sides) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002231 ha_alert("Cannot compute the automatic maxsslconn because global.maxconn is already too "
2232 "high for the global.memmax value (%d MB). The absolute maximum possible value "
2233 "without SSL is %d, but %d was found and SSL is in use.\n",
2234 global.rlimit_memmax,
2235 (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)),
2236 global.maxconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01002237 exit(1);
2238 }
2239
2240 if (global.maxsslconn > sides * global.maxconn)
2241 global.maxsslconn = sides * global.maxconn;
2242
2243 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2244 fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn);
2245 }
2246#endif
2247 else if (!global.maxconn) {
2248 /* memmax and maxsslconn are known/unused, compute maxconn automatically */
2249 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2250 int64_t mem = global.rlimit_memmax * 1048576ULL;
2251 int64_t clearmem;
2252
2253 if (global.ssl_used_frontend || global.ssl_used_backend)
2254 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2255
2256 mem -= global.maxzlibmem;
2257 mem = mem * MEM_USABLE_RATIO;
2258
2259 clearmem = mem;
2260 if (sides)
2261 clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn;
2262
Willy Tarreau87b09662015-04-03 00:22:06 +02002263 global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreauac350932019-03-01 15:43:14 +01002264 global.maxconn = MIN(global.maxconn, ideal_maxconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01002265 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01002266#ifdef SYSTEM_MAXCONN
Willy Tarreauca783d42019-03-13 10:03:07 +01002267 if (global.maxconn > SYSTEM_MAXCONN)
2268 global.maxconn = SYSTEM_MAXCONN;
Willy Tarreau474b96a2015-01-28 19:03:21 +01002269#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01002270
2271 if (clearmem <= 0 || !global.maxconn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002272 ha_alert("Cannot compute the automatic maxconn because global.maxsslconn is already too "
2273 "high for the global.memmax value (%d MB). The absolute maximum possible value "
2274 "is %d, but %d was found.\n",
2275 global.rlimit_memmax,
2276 (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)),
2277 global.maxsslconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01002278 exit(1);
2279 }
2280
2281 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
2282 if (sides && global.maxsslconn > sides * global.maxconn) {
2283 fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn "
2284 "to be limited to %d. Better reduce global.maxsslconn to get more "
2285 "room for extra connections.\n", global.maxsslconn, global.maxconn);
2286 }
2287 fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn);
2288 }
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002289 }
2290
Willy Tarreau5a023f02019-03-01 14:19:31 +01002291 if (!global.maxpipes)
2292 global.maxpipes = compute_ideal_maxpipes();
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002293
Willy Tarreauabacc2c2011-09-07 14:26:33 +02002294 global.hardmaxconn = global.maxconn; /* keep this max value */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002295 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
Willy Tarreau3ec79b92009-01-18 20:39:42 +01002296 global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */
Willy Tarreau2c58b412019-03-14 19:10:55 +01002297 global.maxsock += global.nbthread; /* one epoll_fd/kqueue_fd per thread */
2298 global.maxsock += 2 * global.nbthread; /* one wake-up pipe (2 fd) per thread */
2299
Emeric Brunece0c332017-12-06 13:51:49 +01002300 /* compute fd used by async engines */
2301 if (global.ssl_used_async_engines) {
2302 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2303 global.maxsock += global.maxconn * sides * global.ssl_used_async_engines;
2304 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002305
Olivier Houchard88698d92019-04-16 19:07:22 +02002306 /* update connection pool thresholds */
2307 global.tune.pool_low_count = ((long long)global.maxsock * global.tune.pool_low_ratio + 99) / 100;
2308 global.tune.pool_high_count = ((long long)global.maxsock * global.tune.pool_high_ratio + 99) / 100;
2309
Willy Tarreauc8d5b952019-02-27 17:25:52 +01002310 proxy_adjust_all_maxconn();
2311
Willy Tarreau1db37712007-06-03 17:16:49 +02002312 if (global.tune.maxpollevents <= 0)
2313 global.tune.maxpollevents = MAX_POLL_EVENTS;
2314
Olivier Houchard1599b802018-05-24 18:59:04 +02002315 if (global.tune.runqueue_depth <= 0)
2316 global.tune.runqueue_depth = RUNQUEUE_DEPTH;
2317
Willy Tarreau6f4a82c2009-03-21 20:43:57 +01002318 if (global.tune.recv_enough == 0)
2319 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
2320
Willy Tarreau27a674e2009-08-17 07:23:33 +02002321 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
2322 global.tune.maxrewrite = global.tune.bufsize / 2;
2323
Willy Tarreaubaaee002006-06-26 02:48:02 +02002324 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
2325 /* command line debug mode inhibits configuration mode */
William Lallemand095ba4c2017-06-01 17:38:50 +02002326 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002327 global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND));
2328 }
2329
William Lallemand095ba4c2017-06-01 17:38:50 +02002330 if (arg_mode & MODE_DAEMON) {
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002331 /* command line daemon mode inhibits foreground and debug modes mode */
2332 global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND);
William Lallemand095ba4c2017-06-01 17:38:50 +02002333 global.mode |= arg_mode & MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002334 }
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002335
2336 global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002337
William Lallemand095ba4c2017-06-01 17:38:50 +02002338 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002339 ha_warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
William Lallemand095ba4c2017-06-01 17:38:50 +02002340 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002341 }
2342
William Lallemand095ba4c2017-06-01 17:38:50 +02002343 if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002344 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
Christopher Faulet767a84b2017-11-24 16:50:31 +01002345 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 +02002346 global.nbproc = 1;
2347 }
2348
2349 if (global.nbproc < 1)
2350 global.nbproc = 1;
2351
Christopher Fauletbe0faa22017-08-29 15:37:10 +02002352 if (global.nbthread < 1)
2353 global.nbthread = 1;
2354
Christopher Faulet3ef26392017-08-29 16:46:57 +02002355 /* Realloc trash buffers because global.tune.bufsize may have changed */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002356 if (!init_trash_buffers(0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002357 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet3ef26392017-08-29 16:46:57 +02002358 exit(1);
2359 }
2360
Christopher Faulet96d44832017-11-14 22:02:30 +01002361 if (!init_log_buffers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002362 ha_alert("failed to initialize log buffers.\n");
Christopher Faulet96d44832017-11-14 22:02:30 +01002363 exit(1);
2364 }
2365
Willy Tarreauef1d1f82007-04-16 00:25:25 +02002366 /*
2367 * Note: we could register external pollers here.
2368 * Built-in pollers have been registered before main().
2369 */
Willy Tarreau4f60f162007-04-08 16:39:58 +02002370
Willy Tarreau43b78992009-01-25 15:42:27 +01002371 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +02002372 disable_poller("kqueue");
2373
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +00002374 if (!(global.tune.options & GTUNE_USE_EVPORTS))
2375 disable_poller("evports");
2376
Willy Tarreau43b78992009-01-25 15:42:27 +01002377 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002378 disable_poller("epoll");
2379
Willy Tarreau43b78992009-01-25 15:42:27 +01002380 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002381 disable_poller("poll");
2382
Willy Tarreau43b78992009-01-25 15:42:27 +01002383 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002384 disable_poller("select");
2385
2386 /* Note: we could disable any poller by name here */
2387
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002388 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +02002389 list_pollers(stderr);
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002390 fprintf(stderr, "\n");
2391 list_filters(stderr);
2392 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002393
Willy Tarreau4f60f162007-04-08 16:39:58 +02002394 if (!init_pollers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002395 ha_alert("No polling mechanism available.\n"
2396 " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n"
2397 " is too low on this platform to support maxconn and the number of listeners\n"
2398 " and servers. You should rebuild haproxy specifying your system using TARGET=\n"
2399 " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n"
2400 " global maxconn setting to accommodate the system's limitation. For reference,\n"
2401 " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n"
2402 " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n"
2403 " check build settings using 'haproxy -vv'.\n\n",
2404 FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002405 exit(1);
2406 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002407 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
2408 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002409 }
2410
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002411 if (!global.node)
2412 global.node = strdup(hostname);
2413
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01002414 if (!hlua_post_init())
2415 exit(1);
Thomas Holmes6abded42015-05-12 16:23:58 +01002416
Maxime de Roucy0f503922016-05-13 23:52:55 +02002417 free(err_msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002418}
2419
Simon Horman6fb82592011-07-15 13:14:11 +09002420static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +09002421{
Simon Hormanac821422011-07-15 13:14:09 +09002422 struct acl_term_suite *suite, *suiteb;
2423 struct acl_term *term, *termb;
2424
Simon Horman6fb82592011-07-15 13:14:11 +09002425 if (!cond)
2426 return;
2427
2428 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
2429 list_for_each_entry_safe(term, termb, &suite->terms, list) {
2430 LIST_DEL(&term->list);
2431 free(term);
Simon Hormanac821422011-07-15 13:14:09 +09002432 }
Simon Horman6fb82592011-07-15 13:14:11 +09002433 LIST_DEL(&suite->list);
2434 free(suite);
2435 }
2436
2437 free(cond);
2438}
2439
Christopher Fauletcb550132019-12-17 11:25:46 +01002440static void deinit_act_rules(struct list *rules)
Simon Horman6fb82592011-07-15 13:14:11 +09002441{
Christopher Fauletcb550132019-12-17 11:25:46 +01002442 struct act_rule *rule, *ruleb;
Simon Horman6fb82592011-07-15 13:14:11 +09002443
Christopher Fauletcb550132019-12-17 11:25:46 +01002444 list_for_each_entry_safe(rule, ruleb, rules, list) {
2445 LIST_DEL(&rule->list);
2446 deinit_acl_cond(rule->cond);
Christopher Faulet58b35642019-12-17 11:48:42 +01002447 if (rule->release_ptr)
2448 rule->release_ptr(rule);
Christopher Fauletcb550132019-12-17 11:25:46 +01002449 free(rule);
Simon Hormanac821422011-07-15 13:14:09 +09002450 }
2451}
2452
Simon Horman6fb82592011-07-15 13:14:11 +09002453static void deinit_stick_rules(struct list *rules)
2454{
2455 struct sticking_rule *rule, *ruleb;
2456
2457 list_for_each_entry_safe(rule, ruleb, rules, list) {
2458 LIST_DEL(&rule->list);
2459 deinit_acl_cond(rule->cond);
Christopher Faulet476e5d02016-10-26 11:34:47 +02002460 release_sample_expr(rule->expr);
Simon Horman6fb82592011-07-15 13:14:11 +09002461 free(rule);
2462 }
2463}
2464
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002465void deinit(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002466{
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002467 struct proxy *p = proxies_list, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002468 struct cap_hdr *h,*h_next;
2469 struct server *s,*s_next;
2470 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002471 struct acl_cond *cond, *condb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002472 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002473 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002474 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002475 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002476 struct wordlist *wl, *wlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002477 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +02002478 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +01002479 struct logformat_node *lf, *lfb;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002480 struct bind_conf *bind_conf, *bind_back;
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002481 struct build_opts_str *bol, *bolb;
Willy Tarreau05554e62016-12-21 20:46:26 +01002482 struct post_deinit_fct *pdf;
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +02002483 struct proxy_deinit_fct *pxdf;
2484 struct server_deinit_fct *srvdf;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002485
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002486 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002487 while (p) {
Willy Tarreau8113a5d2012-10-04 08:01:43 +02002488 free(p->conf.file);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002489 free(p->id);
2490 free(p->check_req);
2491 free(p->cookie_name);
2492 free(p->cookie_domain);
Christopher Faulet2f533902020-01-21 11:06:48 +01002493 free(p->cookie_attrs);
Willy Tarreau4c03d1c2019-01-14 15:23:54 +01002494 free(p->lbprm.arg_str);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002495 free(p->capture_name);
2496 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +09002497 free(p->rdp_cookie_name);
Willy Tarreau62a61232013-04-12 18:13:46 +02002498 if (p->conf.logformat_string != default_http_log_format &&
2499 p->conf.logformat_string != default_tcp_log_format &&
2500 p->conf.logformat_string != clf_http_log_format)
2501 free(p->conf.logformat_string);
Willy Tarreau196729e2012-05-31 19:30:26 +02002502
Willy Tarreau62a61232013-04-12 18:13:46 +02002503 free(p->conf.lfs_file);
2504 free(p->conf.uniqueid_format_string);
2505 free(p->conf.uif_file);
Willy Tarreau0cac26c2019-01-14 16:55:42 +01002506 if ((p->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_MAP)
2507 free(p->lbprm.map.srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002508
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002509 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
2510 free(p->conf.logformat_sd_string);
2511 free(p->conf.lfsd_file);
2512
Willy Tarreaub80c2302007-11-30 20:51:32 +01002513 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
2514 LIST_DEL(&cond->list);
2515 prune_acl_cond(cond);
2516 free(cond);
2517 }
2518
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002519 /* build a list of unique uri_auths */
2520 if (!ua)
2521 ua = p->uri_auth;
2522 else {
2523 /* check if p->uri_auth is unique */
2524 for (uap = ua; uap; uap=uap->next)
2525 if (uap == p->uri_auth)
2526 break;
2527
Willy Tarreauaccc4e12008-06-24 11:14:45 +02002528 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002529 /* add it, if it is */
2530 p->uri_auth->next = ua;
2531 ua = p->uri_auth;
2532 }
2533 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002534
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002535 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
2536 LIST_DEL(&acl->list);
2537 prune_acl(acl);
2538 free(acl);
2539 }
2540
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002541 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
2542 LIST_DEL(&srule->list);
2543 prune_acl_cond(srule->cond);
2544 free(srule->cond);
2545 free(srule);
2546 }
2547
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002548 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
2549 LIST_DEL(&rule->list);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002550 if (rule->cond) {
2551 prune_acl_cond(rule->cond);
2552 free(rule->cond);
2553 }
Dragan Dosen2a7c20f2019-04-30 00:38:36 +02002554 free(rule->file);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002555 free(rule);
2556 }
2557
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002558 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
2559 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +01002560 if (rdr->cond) {
2561 prune_acl_cond(rdr->cond);
2562 free(rdr->cond);
2563 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002564 free(rdr->rdr_str);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002565 list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) {
2566 LIST_DEL(&lf->list);
2567 free(lf);
2568 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002569 free(rdr);
2570 }
2571
William Lallemand0f99e342011-10-12 17:50:54 +02002572 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
2573 LIST_DEL(&log->list);
2574 free(log);
2575 }
2576
William Lallemand723b73a2012-02-08 16:37:49 +01002577 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
2578 LIST_DEL(&lf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +02002579 release_sample_expr(lf->expr);
2580 free(lf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +01002581 free(lf);
2582 }
2583
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002584 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
2585 LIST_DEL(&lf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +02002586 release_sample_expr(lf->expr);
2587 free(lf->arg);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002588 free(lf);
2589 }
2590
Christopher Fauletcb550132019-12-17 11:25:46 +01002591 deinit_act_rules(&p->tcp_req.inspect_rules);
2592 deinit_act_rules(&p->tcp_rep.inspect_rules);
2593 deinit_act_rules(&p->tcp_req.l4_rules);
2594 deinit_act_rules(&p->tcp_req.l5_rules);
2595 deinit_act_rules(&p->http_req_rules);
2596 deinit_act_rules(&p->http_res_rules);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002597 deinit_act_rules(&p->http_after_res_rules);
Simon Hormanac821422011-07-15 13:14:09 +09002598
Simon Horman6fb82592011-07-15 13:14:11 +09002599 deinit_stick_rules(&p->storersp_rules);
2600 deinit_stick_rules(&p->sticking_rules);
2601
Willy Tarreaubaaee002006-06-26 02:48:02 +02002602 h = p->req_cap;
2603 while (h) {
2604 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002605 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002606 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002607 free(h);
2608 h = h_next;
2609 }/* end while(h) */
2610
2611 h = p->rsp_cap;
2612 while (h) {
2613 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002614 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002615 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002616 free(h);
2617 h = h_next;
2618 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002619
Willy Tarreaubaaee002006-06-26 02:48:02 +02002620 s = p->srv;
2621 while (s) {
2622 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002623
Willy Tarreauf6562792019-05-07 19:05:35 +02002624 task_destroy(s->check.task);
2625 task_destroy(s->agent.task);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002626
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002627 if (s->check.wait_list.tasklet)
2628 tasklet_free(s->check.wait_list.tasklet);
2629 if (s->agent.wait_list.tasklet)
2630 tasklet_free(s->agent.wait_list.tasklet);
Dragan Dosen026ef572019-04-30 00:56:20 +02002631
Willy Tarreauf6562792019-05-07 19:05:35 +02002632 task_destroy(s->warmup);
Willy Tarreau2e993902011-10-31 11:53:20 +01002633
Willy Tarreaua534fea2008-08-03 12:19:50 +02002634 free(s->id);
2635 free(s->cookie);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002636 free(s->check.bi.area);
2637 free(s->check.bo.area);
2638 free(s->agent.bi.area);
2639 free(s->agent.bo.area);
James Brown55f9ff12015-10-21 18:19:05 -07002640 free(s->agent.send_string);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002641 free(s->hostname_dn);
Sárközi, László34c01792014-09-05 10:08:23 +02002642 free((char*)s->conf.file);
Olivier Houchard7fc3be72018-11-22 18:50:54 +01002643 free(s->idle_conns);
2644 free(s->priv_conns);
2645 free(s->safe_conns);
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002646 free(s->idle_orphan_conns);
Olivier Houchardf1314812019-02-18 16:41:17 +01002647 free(s->curr_idle_thr);
Willy Tarreau17d45382016-12-22 21:16:08 +01002648
2649 if (s->use_ssl || s->check.use_ssl) {
2650 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2651 xprt_get(XPRT_SSL)->destroy_srv(s);
2652 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002653 HA_SPIN_DESTROY(&s->lock);
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +02002654
2655 list_for_each_entry(srvdf, &server_deinit_list, list)
2656 srvdf->fct(s);
2657
Willy Tarreaubaaee002006-06-26 02:48:02 +02002658 free(s);
2659 s = s_next;
2660 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002661
Willy Tarreau4348fad2012-09-20 16:48:07 +02002662 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002663 /*
2664 * Zombie proxy, the listener just pretend to be up
2665 * because they still hold an opened fd.
2666 * Close it and give the listener its real state.
2667 */
2668 if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
2669 close(l->fd);
2670 l->state = LI_INIT;
2671 }
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02002672 unbind_listener(l);
2673 delete_listener(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002674 LIST_DEL(&l->by_fe);
2675 LIST_DEL(&l->by_bind);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002676 free(l->name);
2677 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002678 free(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002679 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002680
Willy Tarreau4348fad2012-09-20 16:48:07 +02002681 /* Release unused SSL configs. */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002682 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01002683 if (bind_conf->xprt->destroy_bind_conf)
2684 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002685 free(bind_conf->file);
2686 free(bind_conf->arg);
2687 LIST_DEL(&bind_conf->by_fe);
2688 free(bind_conf);
2689 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02002690
Christopher Fauletd7c91962015-04-30 11:48:27 +02002691 flt_deinit(p);
2692
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +02002693 list_for_each_entry(pxdf, &proxy_deinit_list, list)
2694 pxdf->fct(p);
2695
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002696 free(p->desc);
2697 free(p->fwdfor_hdr_name);
2698
Olivier Houchard3f795f72019-04-17 22:51:06 +02002699 task_destroy(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002700
Willy Tarreaubafbe012017-11-24 17:34:44 +01002701 pool_destroy(p->req_cap_pool);
2702 pool_destroy(p->rsp_cap_pool);
Dragan Dosen7d61a332019-05-07 14:16:18 +02002703 if (p->table)
2704 pool_destroy(p->table->pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002705
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002706 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002707 p = p->next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002708 HA_SPIN_DESTROY(&p0->lbprm.lock);
2709 HA_SPIN_DESTROY(&p0->lock);
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002710 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002711 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02002712
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002713 while (ua) {
2714 uap = ua;
2715 ua = ua->next;
2716
Willy Tarreaua534fea2008-08-03 12:19:50 +02002717 free(uap->uri_prefix);
2718 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002719 free(uap->node);
2720 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002721
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002722 userlist_free(uap->userlist);
Christopher Fauletcb550132019-12-17 11:25:46 +01002723 deinit_act_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002724
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002725 free(uap);
2726 }
2727
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002728 userlist_free(userlist);
2729
David Carlier834cb2e2015-09-25 12:02:25 +01002730 cfg_unregister_sections();
2731
Christopher Faulet0132d062017-07-26 15:33:35 +02002732 deinit_log_buffers();
David Carlier834cb2e2015-09-25 12:02:25 +01002733
Willy Tarreaudd815982007-10-16 12:25:14 +02002734 protocol_unbind_all();
2735
Willy Tarreau05554e62016-12-21 20:46:26 +01002736 list_for_each_entry(pdf, &post_deinit_list, list)
2737 pdf->fct();
2738
Joe Williamsdf5b38f2010-12-29 17:05:48 +01002739 free(global.log_send_hostname); global.log_send_hostname = NULL;
Dragan Dosen43885c72015-10-01 13:18:13 +02002740 chunk_destroy(&global.log_tag);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002741 free(global.chroot); global.chroot = NULL;
2742 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002743 free(global.node); global.node = NULL;
2744 free(global.desc); global.desc = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002745 free(oldpids); oldpids = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +02002746 task_destroy(idle_conn_task);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01002747 idle_conn_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002748
William Lallemand0f99e342011-10-12 17:50:54 +02002749 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
2750 LIST_DEL(&log->list);
2751 free(log);
2752 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01002753 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02002754 free(wl->s);
Willy Tarreau477ecd82010-01-03 21:12:30 +01002755 LIST_DEL(&wl->list);
2756 free(wl);
2757 }
2758
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002759 list_for_each_entry_safe(bol, bolb, &build_opts_list, list) {
2760 if (bol->must_free)
2761 free((void *)bol->str);
2762 LIST_DEL(&bol->list);
2763 free(bol);
2764 }
2765
Christopher Fauletff2613e2016-11-09 11:36:17 +01002766 vars_prune(&global.vars, NULL, NULL);
Willy Tarreau2455ceb2018-11-26 15:57:34 +01002767 pool_destroy_all();
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002768 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002769} /* end deinit() */
2770
William Lallemand72160322018-11-06 17:37:16 +01002771
Willy Tarreau918ff602011-07-25 16:33:49 +02002772/* Runs the polling loop */
Willy Tarreau3ebd55e2020-03-03 14:59:56 +01002773void run_poll_loop()
Willy Tarreau4f60f162007-04-08 16:39:58 +02002774{
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002775 int next, wake;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002776
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002777 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002778 while (1) {
Willy Tarreauc49ba522019-12-11 08:12:23 +01002779 wake_expired_tasks();
2780
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002781 /* Process a few tasks */
2782 process_runnable_tasks();
2783
William Lallemand1aab50b2018-06-07 09:46:01 +02002784 /* check if we caught some signals and process them in the
2785 first thread */
2786 if (tid == 0)
2787 signal_process_queue();
Willy Tarreau29857942009-05-10 09:01:21 +02002788
Willy Tarreau85c459d2018-08-02 10:54:31 +02002789 /* stop when there's nothing left to do */
William Lallemanda7199262018-11-16 16:57:20 +01002790 if ((jobs - unstoppable_jobs) == 0)
Willy Tarreau85c459d2018-08-02 10:54:31 +02002791 break;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002792
Willy Tarreau7067b3a2019-06-02 11:11:29 +02002793 /* also stop if we failed to cleanly stop all tasks */
2794 if (killed > 1)
2795 break;
2796
Willy Tarreau10146c92015-04-13 20:44:19 +02002797 /* expire immediately if events are pending */
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002798 wake = 1;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002799 if (thread_has_tasks())
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002800 activity[tid].wake_tasks++;
William Lallemand1aab50b2018-06-07 09:46:01 +02002801 else if (signal_queue_len && tid == 0)
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002802 activity[tid].wake_signal++;
Olivier Houchard79321b92018-07-26 17:55:11 +02002803 else {
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002804 _HA_ATOMIC_OR(&sleeping_thread_mask, tid_bit);
2805 __ha_barrier_atomic_store();
Olivier Houchardbba1a262019-09-24 14:55:28 +02002806 if ((global_tasks_mask & tid_bit) || thread_has_tasks()) {
Olivier Houchard79321b92018-07-26 17:55:11 +02002807 activity[tid].wake_tasks++;
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002808 _HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
Olivier Houchard79321b92018-07-26 17:55:11 +02002809 } else
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002810 wake = 0;
Olivier Houchard79321b92018-07-26 17:55:11 +02002811 }
Willy Tarreau10146c92015-04-13 20:44:19 +02002812
Willy Tarreauc49ba522019-12-11 08:12:23 +01002813 /* If we have to sleep, measure how long */
2814 next = wake ? TICK_ETERNITY : next_timer_expiry();
2815
Willy Tarreau58b458d2008-06-29 22:40:23 +02002816 /* The poller will ensure it returns around <next> */
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002817 cur_poller.poll(&cur_poller, next, wake);
Emeric Brun64cc49c2017-10-03 14:46:45 +02002818
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002819 activity[tid].loops++;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002820 }
2821}
2822
Christopher Faulet1d17c102017-08-29 15:38:48 +02002823static void *run_thread_poll_loop(void *data)
2824{
Willy Tarreau082b6282019-05-22 14:42:12 +02002825 struct per_thread_alloc_fct *ptaf;
Christopher Faulet1d17c102017-08-29 15:38:48 +02002826 struct per_thread_init_fct *ptif;
2827 struct per_thread_deinit_fct *ptdf;
Willy Tarreau082b6282019-05-22 14:42:12 +02002828 struct per_thread_free_fct *ptff;
Willy Tarreau34a150c2019-06-11 09:16:41 +02002829 static int init_left = 0;
2830 __decl_hathreads(static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER);
2831 __decl_hathreads(static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002832
Willy Tarreaub4f7cc32019-05-03 09:27:30 +02002833 ha_set_tid((unsigned long)data);
Willy Tarreaud022e9c2019-09-24 08:25:15 +02002834 sched = &task_per_thread[tid];
Willy Tarreau91e6df02019-05-03 17:21:18 +02002835
Willy Tarreauf6178242019-05-21 19:46:58 +02002836#if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
Willy Tarreau91e6df02019-05-03 17:21:18 +02002837#ifdef USE_THREAD
Willy Tarreau8323a372019-05-20 18:57:53 +02002838 pthread_getcpuclockid(pthread_self(), &ti->clock_id);
Willy Tarreau624dcbf2019-05-20 20:23:06 +02002839#else
Willy Tarreau8323a372019-05-20 18:57:53 +02002840 ti->clock_id = CLOCK_THREAD_CPUTIME_ID;
Willy Tarreau91e6df02019-05-03 17:21:18 +02002841#endif
Willy Tarreau663fda42019-05-21 15:14:08 +02002842#endif
Willy Tarreau1c306aa2020-03-06 19:04:55 +01002843 /* assign per-process, per-thread randomness */
2844 ha_random_init_per_thread();
2845
Willy Tarreau6ec902a2019-06-07 14:41:11 +02002846 /* Now, initialize one thread init at a time. This is better since
2847 * some init code is a bit tricky and may release global resources
2848 * after reallocating them locally. This will also ensure there is
2849 * no race on file descriptors allocation.
2850 */
Willy Tarreau34a150c2019-06-11 09:16:41 +02002851#ifdef USE_THREAD
2852 pthread_mutex_lock(&init_mutex);
2853#endif
2854 /* The first thread must set the number of threads left */
2855 if (!init_left)
2856 init_left = global.nbthread;
2857 init_left--;
Willy Tarreau91e6df02019-05-03 17:21:18 +02002858
Christopher Faulet1d17c102017-08-29 15:38:48 +02002859 tv_update_date(-1,-1);
2860
Willy Tarreau082b6282019-05-22 14:42:12 +02002861 /* per-thread alloc calls performed here are not allowed to snoop on
2862 * other threads, so they are free to initialize at their own rhythm
2863 * as long as they act as if they were alone. None of them may rely
2864 * on resources initialized by the other ones.
2865 */
2866 list_for_each_entry(ptaf, &per_thread_alloc_list, list) {
2867 if (!ptaf->fct()) {
2868 ha_alert("failed to allocate resources for thread %u.\n", tid);
2869 exit(1);
2870 }
2871 }
2872
Willy Tarreau3078e9f2019-05-20 10:50:43 +02002873 /* per-thread init calls performed here are not allowed to snoop on
2874 * other threads, so they are free to initialize at their own rhythm
2875 * as long as they act as if they were alone.
2876 */
Christopher Faulet1d17c102017-08-29 15:38:48 +02002877 list_for_each_entry(ptif, &per_thread_init_list, list) {
2878 if (!ptif->fct()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002879 ha_alert("failed to initialize thread %u.\n", tid);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002880 exit(1);
2881 }
2882 }
2883
Willy Tarreau71092822019-06-10 09:51:04 +02002884 /* enabling protocols will result in fd_insert() calls to be performed,
2885 * we want all threads to have already allocated their local fd tables
Willy Tarreau34a150c2019-06-11 09:16:41 +02002886 * before doing so, thus only the last thread does it.
Willy Tarreau71092822019-06-10 09:51:04 +02002887 */
Willy Tarreau34a150c2019-06-11 09:16:41 +02002888 if (init_left == 0)
Willy Tarreaue4d7c9d2019-06-10 10:14:52 +02002889 protocol_enable_all();
Willy Tarreau6ec902a2019-06-07 14:41:11 +02002890
Willy Tarreau34a150c2019-06-11 09:16:41 +02002891#ifdef USE_THREAD
2892 pthread_cond_broadcast(&init_cond);
2893 pthread_mutex_unlock(&init_mutex);
2894
2895 /* now wait for other threads to finish starting */
2896 pthread_mutex_lock(&init_mutex);
2897 while (init_left)
2898 pthread_cond_wait(&init_cond, &init_mutex);
2899 pthread_mutex_unlock(&init_mutex);
2900#endif
Willy Tarreau3078e9f2019-05-20 10:50:43 +02002901
Willy Tarreaua45a8b52019-12-06 16:31:45 +01002902#if defined(PR_SET_NO_NEW_PRIVS) && defined(USE_PRCTL)
2903 /* Let's refrain from using setuid executables. This way the impact of
2904 * an eventual vulnerability in a library remains limited. It may
2905 * impact external checks but who cares about them anyway ? In the
2906 * worst case it's possible to disable the option. Obviously we do this
2907 * in workers only. We can't hard-fail on this one as it really is
2908 * implementation dependent though we're interested in feedback, hence
2909 * the warning.
2910 */
2911 if (!(global.tune.options & GTUNE_INSECURE_SETUID) && !master) {
2912 static int warn_fail;
2913 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1 && !_HA_ATOMIC_XADD(&warn_fail, 1)) {
2914 ha_warning("Failed to disable setuid, please report to developers with detailed "
2915 "information about your operating system. You can silence this warning "
2916 "by adding 'insecure-setuid-wanted' in the 'global' section.\n");
2917 }
2918 }
2919#endif
2920
Willy Tarreaud96f1122019-12-03 07:07:36 +01002921#if defined(RLIMIT_NPROC)
2922 /* all threads have started, it's now time to prevent any new thread
2923 * or process from starting. Obviously we do this in workers only. We
2924 * can't hard-fail on this one as it really is implementation dependent
2925 * though we're interested in feedback, hence the warning.
2926 */
2927 if (!(global.tune.options & GTUNE_INSECURE_FORK) && !master) {
2928 struct rlimit limit = { .rlim_cur = 0, .rlim_max = 0 };
2929 static int warn_fail;
2930
2931 if (setrlimit(RLIMIT_NPROC, &limit) == -1 && !_HA_ATOMIC_XADD(&warn_fail, 1)) {
2932 ha_warning("Failed to disable forks, please report to developers with detailed "
2933 "information about your operating system. You can silence this warning "
2934 "by adding 'insecure-fork-wanted' in the 'global' section.\n");
2935 }
2936 }
2937#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02002938 run_poll_loop();
2939
2940 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
2941 ptdf->fct();
2942
Willy Tarreau082b6282019-05-22 14:42:12 +02002943 list_for_each_entry(ptff, &per_thread_free_list, list)
2944 ptff->fct();
2945
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002946#ifdef USE_THREAD
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002947 _HA_ATOMIC_AND(&all_threads_mask, ~tid_bit);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002948 if (tid > 0)
2949 pthread_exit(NULL);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002950#endif
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002951 return NULL;
2952}
Christopher Faulet1d17c102017-08-29 15:38:48 +02002953
William Dauchyf9af9d72019-11-17 15:47:16 +01002954/* set uid/gid depending on global settings */
2955static void set_identity(const char *program_name)
2956{
2957 if (global.gid) {
2958 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
2959 ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2960 " without 'uid'/'user' is generally useless.\n", program_name);
2961
2962 if (setgid(global.gid) == -1) {
2963 ha_alert("[%s.main()] Cannot set gid %d.\n", program_name, global.gid);
2964 protocol_unbind_all();
2965 exit(1);
2966 }
2967 }
2968
2969 if (global.uid && setuid(global.uid) == -1) {
2970 ha_alert("[%s.main()] Cannot set uid %d.\n", program_name, global.uid);
2971 protocol_unbind_all();
2972 exit(1);
2973 }
2974}
2975
Willy Tarreaubaaee002006-06-26 02:48:02 +02002976int main(int argc, char **argv)
2977{
2978 int err, retry;
2979 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02002980 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02002981 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002982
Olivier Houchard5fa300d2018-02-03 15:15:21 +01002983 setvbuf(stdout, NULL, _IONBF, 0);
Willy Tarreau5794fb02018-11-25 18:43:29 +01002984
Willy Tarreauff9c9142019-02-07 10:39:36 +01002985 /* this can only safely be done here, though it's optimized away by
2986 * the compiler.
2987 */
2988 if (MAX_PROCS < 1 || MAX_PROCS > LONGBITS) {
2989 ha_alert("MAX_PROCS value must be between 1 and %d inclusive; "
2990 "HAProxy was built with value %d, please fix it and rebuild.\n",
2991 LONGBITS, MAX_PROCS);
2992 exit(1);
2993 }
2994
Willy Tarreaubf696402019-03-01 10:09:28 +01002995 /* take a copy of initial limits before we possibly change them */
2996 getrlimit(RLIMIT_NOFILE, &limit);
2997 rlim_fd_cur_at_boot = limit.rlim_cur;
2998 rlim_fd_max_at_boot = limit.rlim_max;
2999
Willy Tarreau5794fb02018-11-25 18:43:29 +01003000 /* process all initcalls in order of potential dependency */
3001 RUN_INITCALLS(STG_PREPARE);
3002 RUN_INITCALLS(STG_LOCK);
3003 RUN_INITCALLS(STG_ALLOC);
3004 RUN_INITCALLS(STG_POOL);
3005 RUN_INITCALLS(STG_REGISTER);
3006 RUN_INITCALLS(STG_INIT);
3007
Emeric Bruncf20bf12010-10-22 16:06:11 +02003008 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02003009 signal_register_fct(SIGQUIT, dump, SIGQUIT);
3010 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
3011 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
William Lallemand73b85e72017-06-01 17:38:51 +02003012 signal_register_fct(SIGUSR2, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003013
Willy Tarreaue437c442010-03-17 18:02:46 +01003014 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
3015 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
3016 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003017 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02003018 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003019
Willy Tarreaudc23a922011-02-16 11:10:36 +01003020 /* ulimits */
3021 if (!global.rlimit_nofile)
3022 global.rlimit_nofile = global.maxsock;
3023
3024 if (global.rlimit_nofile) {
Willy Tarreaue5cfdac2019-03-01 10:32:05 +01003025 limit.rlim_cur = global.rlimit_nofile;
3026 limit.rlim_max = MAX(rlim_fd_max_at_boot, limit.rlim_cur);
3027
Willy Tarreaudc23a922011-02-16 11:10:36 +01003028 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
Willy Tarreauef635472016-06-21 11:48:18 +02003029 getrlimit(RLIMIT_NOFILE, &limit);
William Dauchy0fec3ab2019-10-27 20:08:11 +01003030 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3031 ha_alert("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n",
3032 argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
3033 if (!(global.mode & MODE_MWORKER))
3034 exit(1);
3035 }
3036 else {
3037 /* try to set it to the max possible at least */
3038 limit.rlim_cur = limit.rlim_max;
3039 if (setrlimit(RLIMIT_NOFILE, &limit) != -1)
3040 getrlimit(RLIMIT_NOFILE, &limit);
Willy Tarreau164dd0b2016-06-21 11:51:59 +02003041
William Dauchy0fec3ab2019-10-27 20:08:11 +01003042 ha_warning("[%s.main()] Cannot raise FD limit to %d, limit is %d. "
3043 "This will fail in >= v2.3\n",
3044 argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
3045 global.rlimit_nofile = limit.rlim_cur;
3046 }
Willy Tarreaudc23a922011-02-16 11:10:36 +01003047 }
3048 }
3049
3050 if (global.rlimit_memmax) {
3051 limit.rlim_cur = limit.rlim_max =
Willy Tarreau70060452015-12-14 12:46:07 +01003052 global.rlimit_memmax * 1048576ULL;
Willy Tarreaudc23a922011-02-16 11:10:36 +01003053#ifdef RLIMIT_AS
3054 if (setrlimit(RLIMIT_AS, &limit) == -1) {
William Dauchy0fec3ab2019-10-27 20:08:11 +01003055 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3056 ha_alert("[%s.main()] Cannot fix MEM limit to %d megs.\n",
3057 argv[0], global.rlimit_memmax);
3058 if (!(global.mode & MODE_MWORKER))
3059 exit(1);
3060 }
3061 else
3062 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs."
3063 "This will fail in >= v2.3\n",
3064 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01003065 }
3066#else
3067 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
William Dauchy0fec3ab2019-10-27 20:08:11 +01003068 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3069 ha_alert("[%s.main()] Cannot fix MEM limit to %d megs.\n",
3070 argv[0], global.rlimit_memmax);
3071 if (!(global.mode & MODE_MWORKER))
3072 exit(1);
3073 }
3074 else
3075 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.",
3076 "This will fail in >= v2.3\n",
3077 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01003078 }
3079#endif
3080 }
3081
Olivier Houchardf73629d2017-04-05 22:33:04 +02003082 if (old_unixsocket) {
William Lallemand85b0bd92017-06-01 17:38:53 +02003083 if (strcmp("/dev/null", old_unixsocket) != 0) {
3084 if (get_old_sockets(old_unixsocket) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003085 ha_alert("Failed to get the sockets from the old process!\n");
William Lallemand85b0bd92017-06-01 17:38:53 +02003086 if (!(global.mode & MODE_MWORKER))
3087 exit(1);
3088 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02003089 }
3090 }
William Lallemand85b0bd92017-06-01 17:38:53 +02003091 get_cur_unixsocket();
3092
Willy Tarreaubaaee002006-06-26 02:48:02 +02003093 /* We will loop at most 100 times with 10 ms delay each time.
3094 * That's at most 1 second. We only send a signal to old pids
3095 * if we cannot grab at least one port.
3096 */
3097 retry = MAX_START_RETRIES;
3098 err = ERR_NONE;
3099 while (retry >= 0) {
3100 struct timeval w;
3101 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01003102 /* exit the loop on no error or fatal error */
3103 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003104 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02003105 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003106 break;
3107
3108 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
3109 * listening sockets. So on those platforms, it would be wiser to
3110 * simply send SIGUSR1, which will not be undoable.
3111 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02003112 if (tell_old_pids(SIGTTOU) == 0) {
3113 /* no need to wait if we can't contact old pids */
3114 retry = 0;
3115 continue;
3116 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003117 /* give some time to old processes to stop listening */
3118 w.tv_sec = 0;
3119 w.tv_usec = 10*1000;
3120 select(0, NULL, NULL, NULL, &w);
3121 retry--;
3122 }
3123
3124 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01003125 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02003126 if (retry != MAX_START_RETRIES && nb_oldpids) {
3127 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003128 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02003129 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003130 exit(1);
3131 }
3132
William Lallemand944e6192018-11-21 15:48:31 +01003133 if (!(global.mode & MODE_MWORKER_WAIT) && listeners == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003134 ha_alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003135 /* Note: we don't have to send anything to the old pids because we
3136 * never stopped them. */
3137 exit(1);
3138 }
3139
Emeric Bruncf20bf12010-10-22 16:06:11 +02003140 err = protocol_bind_all(errmsg, sizeof(errmsg));
3141 if ((err & ~ERR_WARN) != ERR_NONE) {
3142 if ((err & ERR_ALERT) || (err & ERR_WARN))
Christopher Faulet767a84b2017-11-24 16:50:31 +01003143 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Emeric Bruncf20bf12010-10-22 16:06:11 +02003144
Christopher Faulet767a84b2017-11-24 16:50:31 +01003145 ha_alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02003146 protocol_unbind_all(); /* cleanup everything we can */
3147 if (nb_oldpids)
3148 tell_old_pids(SIGTTIN);
3149 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02003150 } else if (err & ERR_WARN) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003151 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02003152 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02003153 /* Ok, all listener should now be bound, close any leftover sockets
3154 * the previous process gave us, we don't need them anymore
3155 */
3156 while (xfer_sock_list != NULL) {
3157 struct xfer_sock_list *tmpxfer = xfer_sock_list->next;
3158 close(xfer_sock_list->fd);
3159 free(xfer_sock_list->iface);
3160 free(xfer_sock_list->namespace);
3161 free(xfer_sock_list);
3162 xfer_sock_list = tmpxfer;
3163 }
Willy Tarreaudd815982007-10-16 12:25:14 +02003164
Willy Tarreaubaaee002006-06-26 02:48:02 +02003165 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02003166 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
3167 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003168
Willy Tarreaubaaee002006-06-26 02:48:02 +02003169 /* MODE_QUIET can inhibit alerts and warnings below this line */
3170
PiBa-NL149a81a2017-12-25 21:03:31 +01003171 if (getenv("HAPROXY_MWORKER_REEXEC") != NULL) {
3172 /* either stdin/out/err are already closed or should stay as they are. */
3173 if ((global.mode & MODE_DAEMON)) {
3174 /* daemon mode re-executing, stdin/stdout/stderr are already closed so keep quiet */
3175 global.mode &= ~MODE_VERBOSE;
3176 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
3177 }
3178 } else {
3179 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
3180 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01003181 stdio_quiet(-1);
PiBa-NL149a81a2017-12-25 21:03:31 +01003182 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003183 }
3184
3185 /* open log & pid files before the chroot */
William Lallemand80293002017-11-06 11:00:03 +01003186 if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003187 unlink(global.pidfile);
3188 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
3189 if (pidfd < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003190 ha_alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003191 if (nb_oldpids)
3192 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02003193 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02003194 exit(1);
3195 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003196 }
3197
Willy Tarreaub38651a2007-03-24 17:24:39 +01003198 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003199 ha_alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
3200 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02003201 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01003202 exit(1);
3203 }
3204
Willy Tarreau4e30ed72009-02-04 18:02:48 +01003205 /* If the user is not root, we'll still let him try the configuration
3206 * but we inform him that unexpected behaviour may occur.
3207 */
3208 if ((global.last_checks & LSTCHK_NETADM) && getuid())
Christopher Faulet767a84b2017-11-24 16:50:31 +01003209 ha_warning("[%s.main()] Some options which require full privileges"
3210 " might not work well.\n"
3211 "", argv[0]);
Willy Tarreau4e30ed72009-02-04 18:02:48 +01003212
William Lallemand095ba4c2017-06-01 17:38:50 +02003213 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
3214
3215 /* chroot if needed */
3216 if (global.chroot != NULL) {
3217 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003218 ha_alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02003219 if (nb_oldpids)
3220 tell_old_pids(SIGTTIN);
3221 protocol_unbind_all();
3222 exit(1);
3223 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02003224 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02003225 }
3226
William Lallemand944e6192018-11-21 15:48:31 +01003227 if (nb_oldpids && !(global.mode & MODE_MWORKER_WAIT))
Willy Tarreaubb545b42010-08-25 12:58:59 +02003228 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003229
William Lallemand27edc4b2019-05-07 17:49:33 +02003230 /* send a SIGTERM to workers who have a too high reloads number */
3231 if ((global.mode & MODE_MWORKER) && !(global.mode & MODE_MWORKER_WAIT))
3232 mworker_kill_max_reloads(SIGTERM);
3233
William Lallemand8a361b52017-06-20 11:20:33 +02003234 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) {
3235 nb_oldpids = 0;
3236 free(oldpids);
3237 oldpids = NULL;
3238 }
3239
3240
Willy Tarreaubaaee002006-06-26 02:48:02 +02003241 /* Note that any error at this stage will be fatal because we will not
3242 * be able to restart the old pids.
3243 */
3244
William Dauchyf9af9d72019-11-17 15:47:16 +01003245 if ((global.mode & (MODE_MWORKER | MODE_DAEMON)) == 0)
3246 set_identity(argv[0]);
Willy Tarreau636848a2019-04-15 19:38:50 +02003247
Willy Tarreaubaaee002006-06-26 02:48:02 +02003248 /* check ulimits */
3249 limit.rlim_cur = limit.rlim_max = 0;
3250 getrlimit(RLIMIT_NOFILE, &limit);
3251 if (limit.rlim_cur < global.maxsock) {
William Dauchy0fec3ab2019-10-27 20:08:11 +01003252 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3253 ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. "
3254 "Please raise 'ulimit-n' to %d or more to avoid any trouble.\n",
3255 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock,
3256 global.maxsock);
3257 if (!(global.mode & MODE_MWORKER))
3258 exit(1);
3259 }
3260 else
3261 ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. "
3262 "Please raise 'ulimit-n' to %d or more to avoid any trouble."
3263 "This will fail in >= v2.3\n",
3264 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock,
3265 global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003266 }
3267
William Lallemand944e6192018-11-21 15:48:31 +01003268 if (global.mode & (MODE_DAEMON | MODE_MWORKER | MODE_MWORKER_WAIT)) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003269 struct proxy *px;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003270 struct peers *curpeers;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003271 int ret = 0;
3272 int proc;
William Lallemande1340412017-12-28 16:09:36 +01003273 int devnullfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003274
William Lallemand095ba4c2017-06-01 17:38:50 +02003275 /*
3276 * if daemon + mworker: must fork here to let a master
3277 * process live in background before forking children
3278 */
William Lallemand73b85e72017-06-01 17:38:51 +02003279
3280 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
3281 && (global.mode & MODE_MWORKER)
3282 && (global.mode & MODE_DAEMON)) {
William Lallemand095ba4c2017-06-01 17:38:50 +02003283 ret = fork();
3284 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003285 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02003286 protocol_unbind_all();
3287 exit(1); /* there has been an error */
William Lallemandbfd8eb52018-07-04 15:31:23 +02003288 } else if (ret > 0) { /* parent leave to daemonize */
William Lallemand095ba4c2017-06-01 17:38:50 +02003289 exit(0);
William Lallemandbfd8eb52018-07-04 15:31:23 +02003290 } else /* change the process group ID in the child (master process) */
3291 setsid();
William Lallemand095ba4c2017-06-01 17:38:50 +02003292 }
William Lallemande20b6a62017-06-01 17:38:55 +02003293
William Lallemande20b6a62017-06-01 17:38:55 +02003294
William Lallemanddeed7802017-11-06 11:00:04 +01003295 /* if in master-worker mode, write the PID of the father */
3296 if (global.mode & MODE_MWORKER) {
3297 char pidstr[100];
Willy Tarreau76a80c72019-06-22 07:41:38 +02003298 snprintf(pidstr, sizeof(pidstr), "%d\n", (int)getpid());
Willy Tarreau46ec48b2018-01-23 19:20:19 +01003299 if (pidfd >= 0)
3300 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
William Lallemanddeed7802017-11-06 11:00:04 +01003301 }
3302
Willy Tarreaubaaee002006-06-26 02:48:02 +02003303 /* the father launches the required number of processes */
William Lallemand944e6192018-11-21 15:48:31 +01003304 if (!(global.mode & MODE_MWORKER_WAIT)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +02003305 if (global.mode & MODE_MWORKER)
3306 mworker_ext_launch_all();
William Lallemand944e6192018-11-21 15:48:31 +01003307 for (proc = 0; proc < global.nbproc; proc++) {
3308 ret = fork();
3309 if (ret < 0) {
3310 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
3311 protocol_unbind_all();
3312 exit(1); /* there has been an error */
3313 }
3314 else if (ret == 0) /* child breaks here */
3315 break;
William Lallemand944e6192018-11-21 15:48:31 +01003316 if (pidfd >= 0 && !(global.mode & MODE_MWORKER)) {
3317 char pidstr[100];
3318 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
3319 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
3320 }
3321 if (global.mode & MODE_MWORKER) {
3322 struct mworker_proc *child;
William Lallemandce83b4a2018-10-26 14:47:30 +02003323
William Lallemand220567e2018-11-21 18:04:53 +01003324 ha_notice("New worker #%d (%d) forked\n", relative_pid, ret);
William Lallemand944e6192018-11-21 15:48:31 +01003325 /* find the right mworker_proc */
3326 list_for_each_entry(child, &proc_list, list) {
3327 if (child->relative_pid == relative_pid &&
William Lallemand8f7069a2019-04-12 16:09:23 +02003328 child->reloads == 0 && child->options & PROC_O_TYPE_WORKER) {
William Lallemand944e6192018-11-21 15:48:31 +01003329 child->timestamp = now.tv_sec;
3330 child->pid = ret;
William Lallemand1dc69632019-06-12 19:11:33 +02003331 child->version = strdup(haproxy_version);
William Lallemand944e6192018-11-21 15:48:31 +01003332 break;
3333 }
William Lallemandce83b4a2018-10-26 14:47:30 +02003334 }
3335 }
William Lallemandbc193052018-09-11 10:06:26 +02003336
William Lallemand944e6192018-11-21 15:48:31 +01003337 relative_pid++; /* each child will get a different one */
3338 pid_bit <<= 1;
3339 }
3340 } else {
3341 /* wait mode */
3342 global.nbproc = 1;
3343 proc = 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003344 }
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003345
3346#ifdef USE_CPU_AFFINITY
3347 if (proc < global.nbproc && /* child */
Willy Tarreauff9c9142019-02-07 10:39:36 +01003348 proc < MAX_PROCS && /* only the first 32/64 processes may be pinned */
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003349 global.cpu_map.proc[proc]) /* only do this if the process has a CPU map */
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003350#ifdef __FreeBSD__
Olivier Houchard97148f62017-08-16 17:29:11 +02003351 {
3352 cpuset_t cpuset;
3353 int i;
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003354 unsigned long cpu_map = global.cpu_map.proc[proc];
Olivier Houchard97148f62017-08-16 17:29:11 +02003355
3356 CPU_ZERO(&cpuset);
3357 while ((i = ffsl(cpu_map)) > 0) {
3358 CPU_SET(i - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003359 cpu_map &= ~(1UL << (i - 1));
Olivier Houchard97148f62017-08-16 17:29:11 +02003360 }
3361 ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset);
3362 }
David Carlier5e4c8e22019-09-13 05:12:58 +01003363#elif defined(__linux__)
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003364 sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map.proc[proc]);
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003365#endif
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003366#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003367 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02003368 if (pidfd >= 0) {
3369 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
3370 close(pidfd);
3371 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02003372
3373 /* We won't ever use this anymore */
Willy Tarreaud137dd32010-08-25 12:49:05 +02003374 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003375
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003376 if (proc == global.nbproc) {
William Lallemand944e6192018-11-21 15:48:31 +01003377 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003378
3379 if ((!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
3380 (global.mode & MODE_DAEMON)) {
3381 /* detach from the tty, this is required to properly daemonize. */
William Lallemande1340412017-12-28 16:09:36 +01003382 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL))
3383 stdio_quiet(-1);
3384
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003385 global.mode &= ~MODE_VERBOSE;
3386 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003387 }
3388
William Lallemandb3f2be32018-09-11 10:06:18 +02003389 mworker_loop();
William Lallemand1499b9b2017-06-07 15:04:47 +02003390 /* should never get there */
3391 exit(EXIT_FAILURE);
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003392 }
William Lallemandcf4e4962017-06-08 19:05:48 +02003393#if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH)
Grant Zhang872f9c22017-01-21 01:10:18 +00003394 ssl_free_dh();
3395#endif
William Lallemand1499b9b2017-06-07 15:04:47 +02003396 exit(0); /* parent must leave */
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003397 }
3398
William Lallemandcb11fd22017-06-01 17:38:52 +02003399 /* child must never use the atexit function */
3400 atexit_flag = 0;
3401
William Lallemandbc193052018-09-11 10:06:26 +02003402 /* close useless master sockets */
3403 if (global.mode & MODE_MWORKER) {
3404 struct mworker_proc *child, *it;
3405 master = 0;
3406
William Lallemand309dc9a2018-10-26 14:47:45 +02003407 mworker_cli_proxy_stop();
3408
William Lallemandbc193052018-09-11 10:06:26 +02003409 /* free proc struct of other processes */
3410 list_for_each_entry_safe(child, it, &proc_list, list) {
William Lallemandce83b4a2018-10-26 14:47:30 +02003411 /* close the FD of the master side for all
3412 * workers, we don't need to close the worker
3413 * side of other workers since it's done with
3414 * the bind_proc */
Tim Duesterhus742e0f92018-11-25 20:03:39 +01003415 if (child->ipc_fd[0] >= 0)
3416 close(child->ipc_fd[0]);
William Lallemandce83b4a2018-10-26 14:47:30 +02003417 if (child->relative_pid == relative_pid &&
3418 child->reloads == 0) {
3419 /* keep this struct if this is our pid */
3420 proc_self = child;
William Lallemandbc193052018-09-11 10:06:26 +02003421 continue;
William Lallemandce83b4a2018-10-26 14:47:30 +02003422 }
William Lallemandbc193052018-09-11 10:06:26 +02003423 LIST_DEL(&child->list);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +02003424 mworker_free_child(child);
3425 child = NULL;
William Lallemandbc193052018-09-11 10:06:26 +02003426 }
3427 }
Willy Tarreau1605c7a2018-01-23 19:01:49 +01003428
William Lallemande1340412017-12-28 16:09:36 +01003429 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
3430 devnullfd = open("/dev/null", O_RDWR, 0);
3431 if (devnullfd < 0) {
3432 ha_alert("Cannot open /dev/null\n");
3433 exit(EXIT_FAILURE);
3434 }
3435 }
3436
William Lallemand095ba4c2017-06-01 17:38:50 +02003437 /* Must chroot and setgid/setuid in the children */
3438 /* chroot if needed */
3439 if (global.chroot != NULL) {
3440 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003441 ha_alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02003442 if (nb_oldpids)
3443 tell_old_pids(SIGTTIN);
3444 protocol_unbind_all();
3445 exit(1);
3446 }
3447 }
3448
3449 free(global.chroot);
3450 global.chroot = NULL;
William Dauchyf9af9d72019-11-17 15:47:16 +01003451 set_identity(argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02003452
William Lallemand7f80eb22017-05-26 18:19:55 +02003453 /* pass through every cli socket, and check if it's bound to
3454 * the current process and if it exposes listeners sockets.
3455 * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork.
3456 * */
3457
3458 if (global.stats_fe) {
3459 struct bind_conf *bind_conf;
3460
3461 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
3462 if (bind_conf->level & ACCESS_FD_LISTENERS) {
3463 if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) {
3464 global.tune.options |= GTUNE_SOCKET_TRANSFER;
3465 break;
3466 }
3467 }
3468 }
3469 }
3470
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003471 /* we might have to unbind some proxies from some processes */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003472 px = proxies_list;
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003473 while (px != NULL) {
3474 if (px->bind_proc && px->state != PR_STSTOPPED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02003475 if (!(px->bind_proc & (1UL << proc))) {
3476 if (global.tune.options & GTUNE_SOCKET_TRANSFER)
3477 zombify_proxy(px);
3478 else
3479 stop_proxy(px);
3480 }
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003481 }
3482 px = px->next;
3483 }
3484
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003485 /* we might have to unbind some peers sections from some processes */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003486 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003487 if (!curpeers->peers_fe)
3488 continue;
3489
3490 if (curpeers->peers_fe->bind_proc & (1UL << proc))
3491 continue;
3492
3493 stop_proxy(curpeers->peers_fe);
3494 /* disable this peer section so that it kills itself */
Willy Tarreau47c8c022015-09-28 16:39:25 +02003495 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02003496 task_destroy(curpeers->sync_task);
Willy Tarreau47c8c022015-09-28 16:39:25 +02003497 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +02003498 task_destroy(curpeers->peers_fe->task);
Willy Tarreau47c8c022015-09-28 16:39:25 +02003499 curpeers->peers_fe->task = NULL;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003500 curpeers->peers_fe = NULL;
3501 }
3502
William Lallemand2e8fad92018-11-13 16:18:23 +01003503 /*
3504 * This is only done in daemon mode because we might want the
3505 * logs on stdout in mworker mode. If we're NOT in QUIET mode,
3506 * we should now close the 3 first FDs to ensure that we can
3507 * detach from the TTY. We MUST NOT do it in other cases since
3508 * it would have already be done, and 0-2 would have been
3509 * affected to listening sockets
Willy Tarreaubaaee002006-06-26 02:48:02 +02003510 */
William Lallemand2e8fad92018-11-13 16:18:23 +01003511 if ((global.mode & MODE_DAEMON) &&
3512 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003513 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01003514 stdio_quiet(devnullfd);
Willy Tarreau106cb762008-11-16 07:40:34 +01003515 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003516 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
3517 }
3518 pid = getpid(); /* update child's pid */
William Lallemandbfd8eb52018-07-04 15:31:23 +02003519 if (!(global.mode & MODE_MWORKER)) /* in mworker mode we don't want a new pgid for the children */
3520 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02003521 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02003522 }
3523
William Dauchye039f262019-11-17 15:47:15 +01003524 /* try our best to re-enable core dumps depending on system capabilities.
3525 * What is addressed here :
3526 * - remove file size limits
3527 * - remove core size limits
3528 * - mark the process dumpable again if it lost it due to user/group
3529 */
3530 if (global.tune.options & GTUNE_SET_DUMPABLE) {
3531 limit.rlim_cur = limit.rlim_max = RLIM_INFINITY;
3532
3533#if defined(RLIMIT_FSIZE)
3534 if (setrlimit(RLIMIT_FSIZE, &limit) == -1) {
3535 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3536 ha_alert("[%s.main()] Failed to set the raise the maximum "
3537 "file size.\n", argv[0]);
3538 if (!(global.mode & MODE_MWORKER))
3539 exit(1);
3540 }
3541 else
3542 ha_warning("[%s.main()] Failed to set the raise the maximum "
3543 "file size. This will fail in >= v2.3\n", argv[0]);
3544 }
3545#endif
3546
3547#if defined(RLIMIT_CORE)
3548 if (setrlimit(RLIMIT_CORE, &limit) == -1) {
3549 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3550 ha_alert("[%s.main()] Failed to set the raise the core "
3551 "dump size.\n", argv[0]);
3552 if (!(global.mode & MODE_MWORKER))
3553 exit(1);
3554 }
3555 else
3556 ha_warning("[%s.main()] Failed to set the raise the core "
3557 "dump size. This will fail in >= v2.3\n", argv[0]);
3558 }
3559#endif
3560
3561#if defined(USE_PRCTL)
3562 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1)
3563 ha_warning("[%s.main()] Failed to set the dumpable flag, "
3564 "no core will be dumped.\n", argv[0]);
3565#endif
3566 }
3567
Christopher Faulete3a5e352017-10-24 13:53:54 +02003568 global.mode &= ~MODE_STARTING;
Willy Tarreau4f60f162007-04-08 16:39:58 +02003569 /*
3570 * That's it : the central polling loop. Run until we stop.
3571 */
Christopher Faulet1d17c102017-08-29 15:38:48 +02003572#ifdef USE_THREAD
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003573 {
William Lallemand1aab50b2018-06-07 09:46:01 +02003574 sigset_t blocked_sig, old_sig;
Willy Tarreauc40efc12019-05-03 09:22:44 +02003575 int i;
3576
William Lallemand1aab50b2018-06-07 09:46:01 +02003577 /* ensure the signals will be blocked in every thread */
3578 sigfillset(&blocked_sig);
3579 sigdelset(&blocked_sig, SIGPROF);
3580 sigdelset(&blocked_sig, SIGBUS);
3581 sigdelset(&blocked_sig, SIGFPE);
3582 sigdelset(&blocked_sig, SIGILL);
3583 sigdelset(&blocked_sig, SIGSEGV);
3584 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
3585
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003586 /* Create nbthread-1 thread. The first thread is the current process */
David Carliera92c5ce2019-09-13 05:03:12 +01003587 ha_thread_info[0].pthread = pthread_self();
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003588 for (i = 1; i < global.nbthread; i++)
David Carliera92c5ce2019-09-13 05:03:12 +01003589 pthread_create(&ha_thread_info[i].pthread, NULL, &run_thread_poll_loop, (void *)(long)i);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003590
Christopher Faulet62519022017-10-16 15:49:32 +02003591#ifdef USE_CPU_AFFINITY
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003592 /* Now the CPU affinity for all threads */
Willy Tarreau7764a572019-07-16 15:10:34 +02003593 if (global.cpu_map.proc_t1[relative_pid-1])
3594 global.cpu_map.thread[0] &= global.cpu_map.proc_t1[relative_pid-1];
3595
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003596 for (i = 0; i < global.nbthread; i++) {
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003597 if (global.cpu_map.proc[relative_pid-1])
Willy Tarreau81492c92019-05-03 09:41:23 +02003598 global.cpu_map.thread[i] &= global.cpu_map.proc[relative_pid-1];
Christopher Faulet62519022017-10-16 15:49:32 +02003599
Willy Tarreau421f02e2018-01-20 18:19:22 +01003600 if (i < MAX_THREADS && /* only the first 32/64 threads may be pinned */
Willy Tarreau81492c92019-05-03 09:41:23 +02003601 global.cpu_map.thread[i]) {/* only do this if the thread has a THREAD map */
David Carlier5e4c8e22019-09-13 05:12:58 +01003602#if defined(__APPLE__)
3603 int j;
3604 unsigned long cpu_map = global.cpu_map.thread[i];
3605
3606 while ((j = ffsl(cpu_map)) > 0) {
3607 thread_affinity_policy_data_t cpu_set = { j - 1 };
3608 thread_port_t mthread = pthread_mach_thread_np(ha_thread_info[i].pthread);
3609 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
3610 cpu_map &= ~(1UL << (j - 1));
3611 }
3612#else
Olivier Houchard829aa242017-12-01 18:19:43 +01003613#if defined(__FreeBSD__) || defined(__NetBSD__)
3614 cpuset_t cpuset;
3615#else
3616 cpu_set_t cpuset;
3617#endif
3618 int j;
Willy Tarreau81492c92019-05-03 09:41:23 +02003619 unsigned long cpu_map = global.cpu_map.thread[i];
Olivier Houchard829aa242017-12-01 18:19:43 +01003620
3621 CPU_ZERO(&cpuset);
3622
3623 while ((j = ffsl(cpu_map)) > 0) {
3624 CPU_SET(j - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003625 cpu_map &= ~(1UL << (j - 1));
Olivier Houchard829aa242017-12-01 18:19:43 +01003626 }
David Carliera92c5ce2019-09-13 05:03:12 +01003627 pthread_setaffinity_np(ha_thread_info[i].pthread,
Olivier Houchard829aa242017-12-01 18:19:43 +01003628 sizeof(cpuset), &cpuset);
David Carlier5e4c8e22019-09-13 05:12:58 +01003629#endif
Olivier Houchard829aa242017-12-01 18:19:43 +01003630 }
Christopher Faulet1d17c102017-08-29 15:38:48 +02003631 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003632#endif /* !USE_CPU_AFFINITY */
3633
William Lallemand1aab50b2018-06-07 09:46:01 +02003634 /* when multithreading we need to let only the thread 0 handle the signals */
William Lallemandd3801c12018-09-11 10:06:23 +02003635 haproxy_unblock_signals();
William Lallemand1aab50b2018-06-07 09:46:01 +02003636
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003637 /* Finally, start the poll loop for the first thread */
Willy Tarreaub4f7cc32019-05-03 09:27:30 +02003638 run_thread_poll_loop(0);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003639
3640 /* Wait the end of other threads */
3641 for (i = 1; i < global.nbthread; i++)
David Carliera92c5ce2019-09-13 05:03:12 +01003642 pthread_join(ha_thread_info[i].pthread, NULL);
Christopher Faulet1d17c102017-08-29 15:38:48 +02003643
Christopher Fauletb79a94c2017-05-30 15:34:30 +02003644#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
3645 show_lock_stats();
3646#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003647 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003648#else /* ! USE_THREAD */
William Lallemandd3801c12018-09-11 10:06:23 +02003649 haproxy_unblock_signals();
Willy Tarreaub4f7cc32019-05-03 09:27:30 +02003650 run_thread_poll_loop(0);
Christopher Faulet62519022017-10-16 15:49:32 +02003651#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003652
3653 /* Do some cleanup */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003654 deinit();
Christopher Faulet1d17c102017-08-29 15:38:48 +02003655
Willy Tarreaubaaee002006-06-26 02:48:02 +02003656 exit(0);
3657}
3658
3659
3660/*
3661 * Local variables:
3662 * c-indent-level: 8
3663 * c-basic-offset: 8
3664 * End:
3665 */