blob: df3b9a6c75c946f693644df09a227058ebad4dcf [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
Willy Tarreauf8ea00e2020-03-12 17:24:53 +0100148volatile unsigned long sleeping_thread_mask = 0; /* Threads that are about to sleep in poll() */
Willy Tarreau4b3f27b2020-03-12 17:28:01 +0100149volatile unsigned long stopping_thread_mask = 0; /* Threads acknowledged stopping */
Willy Tarreauf8ea00e2020-03-12 17:24:53 +0100150
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151/* global options */
152struct global global = {
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100153 .hard_stop_after = TICK_ETERNITY,
Willy Tarreau247a13a2012-11-15 17:38:15 +0100154 .nbproc = 1,
Willy Tarreau149ab772019-01-26 14:27:06 +0100155 .nbthread = 0,
William Lallemand5f232402012-04-05 18:02:55 +0200156 .req_count = 0,
William Lallemand0f99e342011-10-12 17:50:54 +0200157 .logsrvs = LIST_HEAD_INIT(global.logsrvs),
William Lallemand9d5f5482012-11-07 16:12:57 +0100158 .maxzlibmem = 0,
William Lallemandd85f9172012-11-09 17:05:39 +0100159 .comp_rate_lim = 0,
Emeric Brun850efd52014-01-29 12:24:34 +0100160 .ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED,
Emeric Bruned760922010-10-22 17:59:25 +0200161 .unix_bind = {
162 .ux = {
163 .uid = -1,
164 .gid = -1,
165 .mode = 0,
166 }
167 },
Willy Tarreau27a674e2009-08-17 07:23:33 +0200168 .tune = {
Willy Tarreau7ac908b2019-02-27 12:02:18 +0100169 .options = GTUNE_LISTENER_MQ,
Willy Tarreauc77d3642018-12-12 06:19:42 +0100170 .bufsize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)),
Christopher Faulet546c4692020-01-22 14:31:21 +0100171 .maxrewrite = MAXREWRITE,
Willy Tarreauc77d3642018-12-12 06:19:42 +0100172 .chksize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)),
Willy Tarreaua24adf02014-11-27 01:11:56 +0100173 .reserved_bufs = RESERVED_BUFS,
Willy Tarreauf3045d22015-04-29 16:24:50 +0200174 .pattern_cache = DEFAULT_PAT_LRU_SIZE,
Olivier Houchard88698d92019-04-16 19:07:22 +0200175 .pool_low_ratio = 20,
176 .pool_high_ratio = 25,
Christopher Faulet41ba36f2019-07-19 09:36:45 +0200177 .max_http_hdr = MAX_HTTP_HDR,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200178#ifdef USE_OPENSSL
Emeric Brun46635772012-11-14 11:32:56 +0100179 .sslcachesize = SSLCACHESIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200180#endif
William Lallemandf3747832012-11-09 12:33:10 +0100181 .comp_maxlevel = 1,
Willy Tarreau7e312732014-02-12 16:35:14 +0100182#ifdef DEFAULT_IDLE_TIMER
183 .idle_timer = DEFAULT_IDLE_TIMER,
184#else
185 .idle_timer = 1000, /* 1 second */
186#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200187 },
Emeric Brun76d88952012-10-05 15:47:31 +0200188#ifdef USE_OPENSSL
189#ifdef DEFAULT_MAXSSLCONN
Willy Tarreau403edff2012-09-06 11:58:37 +0200190 .maxsslconn = DEFAULT_MAXSSLCONN,
191#endif
Emeric Brun76d88952012-10-05 15:47:31 +0200192#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200193 /* others NULL OK */
194};
195
196/*********************************************************************/
197
198int stopping; /* non zero means stopping in progress */
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100199int killed; /* non zero means a hard-stop is triggered */
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200200int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */
William Lallemanda7199262018-11-16 16:57:20 +0100201int unstoppable_jobs = 0; /* number of active jobs that can't be stopped during a soft stop */
Willy Tarreau199ad242018-11-05 16:31:22 +0100202int active_peers = 0; /* number of active peers (connection attempts and connected) */
Willy Tarreau2d372c22018-11-05 17:12:27 +0100203int connected_peers = 0; /* number of connected peers (verified ones) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200204
205/* Here we store informations about the pids of the processes we may pause
206 * or kill. We will send them a signal every 10 ms until we can bind to all
207 * our ports. With 200 retries, that's about 2 seconds.
208 */
209#define MAX_START_RETRIES 200
Willy Tarreaubaaee002006-06-26 02:48:02 +0200210static int *oldpids = NULL;
211static int oldpids_sig; /* use USR1 or TERM */
212
Olivier Houchardf73629d2017-04-05 22:33:04 +0200213/* Path to the unix socket we use to retrieve listener sockets from the old process */
214static const char *old_unixsocket;
215
William Lallemand85b0bd92017-06-01 17:38:53 +0200216static char *cur_unixsocket = NULL;
217
William Lallemandcb11fd22017-06-01 17:38:52 +0200218int atexit_flag = 0;
219
Willy Tarreaubb545b42010-08-25 12:58:59 +0200220int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200221const int zero = 0;
222const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200223const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100225char hostname[MAX_HOSTNAME_LEN];
Emeric Brun2b920a12010-09-23 18:30:22 +0200226char localpeer[MAX_HOSTNAME_LEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200227
Willy Tarreau89efaed2013-12-13 15:14:55 +0100228/* used from everywhere just to drain results we don't want to read and which
229 * recent versions of gcc increasingly and annoyingly complain about.
230 */
231int shut_your_big_mouth_gcc_int = 0;
232
William Lallemand73b85e72017-06-01 17:38:51 +0200233static char **next_argv = NULL;
234
William Lallemandbc193052018-09-11 10:06:26 +0200235struct list proc_list = LIST_HEAD_INIT(proc_list);
236
237int master = 0; /* 1 if in master, 0 if in child */
Willy Tarreaubf696402019-03-01 10:09:28 +0100238unsigned int rlim_fd_cur_at_boot = 0;
239unsigned int rlim_fd_max_at_boot = 0;
William Lallemandbc193052018-09-11 10:06:26 +0200240
Willy Tarreau6c3a6812020-03-06 18:57:15 +0100241/* per-boot randomness */
242unsigned char boot_seed[20]; /* per-boot random seed (160 bits initially) */
243
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
1368/* Performs basic random seed initialization. The main issue with this is that
1369 * srandom_r() only takes 32 bits and purposely provides a reproducible sequence,
1370 * which means that there will only be 4 billion possible random sequences once
1371 * srandom() is called, regardless of the internal state. Not calling it is
1372 * even worse as we'll always produce the same randoms sequences. What we do
1373 * here is to create an initial sequence from various entropy sources, hash it
1374 * using SHA1 and keep the resulting 160 bits available globally.
1375 *
1376 * We initialize the current process with the first 32 bits before starting the
1377 * polling loop, where all this will be changed to have process specific and
1378 * thread specific sequences.
Willy Tarreau52bf8392020-03-08 00:42:37 +01001379 *
1380 * Before starting threads, it's still possible to call random() as srandom()
1381 * is initialized from this, but after threads and/or processes are started,
1382 * only ha_random() is expected to be used to guarantee distinct sequences.
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001383 */
1384static void ha_random_boot(char *const *argv)
1385{
1386 unsigned char message[256];
1387 unsigned char *m = message;
1388 struct timeval tv;
1389 blk_SHA_CTX ctx;
1390 unsigned long l;
1391 int fd;
1392 int i;
1393
1394 /* start with current time as pseudo-random seed */
1395 gettimeofday(&tv, NULL);
1396 write_u32(m, tv.tv_sec); m += 4;
1397 write_u32(m, tv.tv_usec); m += 4;
1398
1399 /* PID and PPID add some OS-based randomness */
1400 write_u16(m, getpid()); m += 2;
1401 write_u16(m, getppid()); m += 2;
1402
1403 /* take up to 160 bits bytes from /dev/urandom if available (non-blocking) */
1404 fd = open("/dev/urandom", O_RDONLY);
1405 if (fd >= 0) {
1406 i = read(fd, m, 20);
1407 if (i > 0)
1408 m += i;
1409 close(fd);
1410 }
1411
1412 /* take up to 160 bits bytes from openssl (non-blocking) */
1413#ifdef USE_OPENSSL
1414 if (RAND_bytes(m, 20) == 1)
1415 m += 20;
1416#endif
1417
1418 /* take 160 bits from existing random in case it was already initialized */
1419 for (i = 0; i < 5; i++) {
1420 write_u32(m, random());
1421 m += 4;
1422 }
1423
1424 /* stack address (benefit form operating system's ASLR) */
1425 l = (unsigned long)&m;
1426 memcpy(m, &l, sizeof(l)); m += sizeof(l);
1427
1428 /* argv address (benefit form operating system's ASLR) */
1429 l = (unsigned long)&argv;
1430 memcpy(m, &l, sizeof(l)); m += sizeof(l);
1431
1432 /* use tv_usec again after all the operations above */
1433 gettimeofday(&tv, NULL);
1434 write_u32(m, tv.tv_usec); m += 4;
1435
1436 /*
1437 * At this point, ~84-92 bytes have been used
1438 */
1439
1440 /* finish with the hostname */
1441 strncpy((char *)m, hostname, message + sizeof(message) - m);
1442 m += strlen(hostname);
1443
1444 /* total message length */
1445 l = m - message;
1446
1447 memset(&ctx, 0, sizeof(ctx));
1448 blk_SHA1_Init(&ctx);
1449 blk_SHA1_Update(&ctx, message, l);
1450 blk_SHA1_Final(boot_seed, &ctx);
1451
1452 srandom(read_u32(boot_seed));
Willy Tarreau52bf8392020-03-08 00:42:37 +01001453 ha_random_seed(boot_seed, sizeof(boot_seed));
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001454}
1455
Willy Tarreau5a023f02019-03-01 14:19:31 +01001456/* considers splicing proxies' maxconn, computes the ideal global.maxpipes
1457 * setting, and returns it. It may return -1 meaning "unlimited" if some
1458 * unlimited proxies have been found and the global.maxconn value is not yet
1459 * set. It may also return a value greater than maxconn if it's not yet set.
1460 * Note that a value of zero means there is no need for pipes. -1 is never
1461 * returned if global.maxconn is valid.
1462 */
1463static int compute_ideal_maxpipes()
1464{
1465 struct proxy *cur;
1466 int nbfe = 0, nbbe = 0;
1467 int unlimited = 0;
1468 int pipes;
1469 int max;
1470
1471 for (cur = proxies_list; cur; cur = cur->next) {
1472 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
1473 if (cur->cap & PR_CAP_FE) {
1474 max = cur->maxconn;
1475 nbfe += max;
1476 if (!max) {
1477 unlimited = 1;
1478 break;
1479 }
1480 }
1481 if (cur->cap & PR_CAP_BE) {
1482 max = cur->fullconn ? cur->fullconn : global.maxconn;
1483 nbbe += max;
1484 if (!max) {
1485 unlimited = 1;
1486 break;
1487 }
1488 }
1489 }
1490 }
1491
1492 pipes = MAX(nbfe, nbbe);
1493 if (global.maxconn) {
1494 if (pipes > global.maxconn || unlimited)
1495 pipes = global.maxconn;
1496 } else if (unlimited) {
1497 pipes = -1;
1498 }
1499
1500 return pipes >= 4 ? pipes / 4 : pipes;
1501}
1502
Willy Tarreauac350932019-03-01 15:43:14 +01001503/* considers global.maxsocks, global.maxpipes, async engines, SSL frontends and
1504 * rlimits and computes an ideal maxconn. It's meant to be called only when
1505 * maxsock contains the sum of listening FDs, before it is updated based on
Willy Tarreaudf23c0c2019-03-13 10:10:49 +01001506 * maxconn and pipes. If there are not enough FDs left, DEFAULT_MAXCONN (by
1507 * default 100) is returned as it is expected that it will even run on tight
1508 * environments, and will maintain compatibility with previous packages that
1509 * used to rely on this value as the default one. The system will emit a
1510 * warning indicating how many FDs are missing anyway if needed.
Willy Tarreauac350932019-03-01 15:43:14 +01001511 */
1512static int compute_ideal_maxconn()
1513{
1514 int ssl_sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1515 int engine_fds = global.ssl_used_async_engines * ssl_sides;
1516 int pipes = compute_ideal_maxpipes();
Willy Tarreaub1beaa32020-03-06 10:25:31 +01001517 int remain = MAX(rlim_fd_cur_at_boot, rlim_fd_max_at_boot);
Willy Tarreauac350932019-03-01 15:43:14 +01001518 int maxconn;
1519
1520 /* we have to take into account these elements :
1521 * - number of engine_fds, which inflates the number of FD needed per
1522 * connection by this number.
1523 * - number of pipes per connection on average : for the unlimited
1524 * case, this is 0.5 pipe FDs per connection, otherwise it's a
1525 * fixed value of 2*pipes.
1526 * - two FDs per connection
1527 */
1528
1529 /* subtract listeners and checks */
1530 remain -= global.maxsock;
1531
Willy Tarreau3f200852019-03-14 19:13:17 +01001532 /* one epoll_fd/kqueue_fd per thread */
1533 remain -= global.nbthread;
1534
1535 /* one wake-up pipe (2 fd) per thread */
1536 remain -= 2 * global.nbthread;
1537
Willy Tarreauac350932019-03-01 15:43:14 +01001538 /* Fixed pipes values : we only subtract them if they're not larger
1539 * than the remaining FDs because pipes are optional.
1540 */
1541 if (pipes >= 0 && pipes * 2 < remain)
1542 remain -= pipes * 2;
1543
1544 if (pipes < 0) {
1545 /* maxsock = maxconn * 2 + maxconn/4 * 2 + maxconn * engine_fds.
1546 * = maxconn * (2 + 0.5 + engine_fds)
1547 * = maxconn * (4 + 1 + 2*engine_fds) / 2
1548 */
1549 maxconn = 2 * remain / (5 + 2 * engine_fds);
1550 } else {
1551 /* maxsock = maxconn * 2 + maxconn * engine_fds.
1552 * = maxconn * (2 + engine_fds)
1553 */
1554 maxconn = remain / (2 + engine_fds);
1555 }
1556
Willy Tarreaudf23c0c2019-03-13 10:10:49 +01001557 return MAX(maxconn, DEFAULT_MAXCONN);
Willy Tarreauac350932019-03-01 15:43:14 +01001558}
1559
Willy Tarreaua409f302020-03-10 17:08:53 +01001560/* computes the estimated maxsock value for the given maxconn based on the
1561 * possibly set global.maxpipes and existing partial global.maxsock. It may
1562 * temporarily change global.maxconn for the time needed to propagate the
1563 * computations, and will reset it.
1564 */
1565static int compute_ideal_maxsock(int maxconn)
1566{
1567 int maxpipes = global.maxpipes;
1568 int maxsock = global.maxsock;
1569
1570
1571 if (!maxpipes) {
1572 int old_maxconn = global.maxconn;
1573
1574 global.maxconn = maxconn;
1575 maxpipes = compute_ideal_maxpipes();
1576 global.maxconn = old_maxconn;
1577 }
1578
1579 maxsock += maxconn * 2; /* each connection needs two sockets */
1580 maxsock += maxpipes * 2; /* each pipe needs two FDs */
1581 maxsock += global.nbthread; /* one epoll_fd/kqueue_fd per thread */
1582 maxsock += 2 * global.nbthread; /* one wake-up pipe (2 fd) per thread */
1583
1584 /* compute fd used by async engines */
1585 if (global.ssl_used_async_engines) {
1586 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1587
1588 maxsock += maxconn * sides * global.ssl_used_async_engines;
1589 }
1590 return maxsock;
1591}
1592
Willy Tarreau304e17e2020-03-10 17:54:54 +01001593/* Tests if it is possible to set the current process' RLIMIT_NOFILE to
1594 * <maxsock>, then sets it back to the previous value. Returns non-zero if the
1595 * value is accepted, non-zero otherwise. This is used to determine if an
1596 * automatic limit may be applied or not. When it is not, the caller knows that
1597 * the highest we can do is the rlim_max at boot. In case of error, we return
1598 * that the setting is possible, so that we defer the error processing to the
1599 * final stage in charge of enforcing this.
1600 */
1601static int check_if_maxsock_permitted(int maxsock)
1602{
1603 struct rlimit orig_limit, test_limit;
1604 int ret;
1605
1606 if (getrlimit(RLIMIT_NOFILE, &orig_limit) != 0)
1607 return 1;
1608
1609 /* don't go further if we can't even set to what we have */
1610 if (setrlimit(RLIMIT_NOFILE, &orig_limit) != 0)
1611 return 1;
1612
1613 test_limit.rlim_max = MAX(maxsock, orig_limit.rlim_max);
1614 test_limit.rlim_cur = test_limit.rlim_max;
1615 ret = setrlimit(RLIMIT_NOFILE, &test_limit);
1616
1617 if (setrlimit(RLIMIT_NOFILE, &orig_limit) != 0)
1618 return 1;
1619
1620 return ret == 0;
1621}
1622
1623
William Lallemand73b85e72017-06-01 17:38:51 +02001624/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001625 * This function initializes all the necessary variables. It only returns
1626 * if everything is OK. If something fails, it exits.
1627 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001628static void init(int argc, char **argv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001629{
Willy Tarreaubaaee002006-06-26 02:48:02 +02001630 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001631 char *tmp;
1632 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +02001633 int err_code = 0;
Maxime de Roucy0f503922016-05-13 23:52:55 +02001634 char *err_msg = NULL;
Willy Tarreau477ecd82010-01-03 21:12:30 +01001635 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +00001636 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +02001637 char *change_dir = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001638 struct proxy *px;
Willy Tarreaue6945732016-12-21 19:57:00 +01001639 struct post_check_fct *pcf;
Willy Tarreauac350932019-03-01 15:43:14 +01001640 int ideal_maxconn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001641
Christopher Faulete3a5e352017-10-24 13:53:54 +02001642 global.mode = MODE_STARTING;
William Lallemand73b85e72017-06-01 17:38:51 +02001643 next_argv = copy_argv(argc, argv);
1644
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001645 if (!init_trash_buffers(1)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001646 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet748919a2017-07-26 14:59:46 +02001647 exit(1);
1648 }
David du Colombier7af46052012-05-16 14:16:48 +02001649
Emeric Brun2b920a12010-09-23 18:30:22 +02001650 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
1651 * the string in case of truncation, and at least FreeBSD appears not to do
1652 * it.
1653 */
1654 memset(hostname, 0, sizeof(hostname));
1655 gethostname(hostname, sizeof(hostname) - 1);
1656 memset(localpeer, 0, sizeof(localpeer));
1657 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
William Lallemanddaf4cd22018-04-17 16:46:13 +02001658 setenv("HAPROXY_LOCALPEER", localpeer, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02001659
William Lallemand24c928c2020-01-14 17:58:18 +01001660 /* we were in mworker mode, we should restart in mworker mode */
1661 if (getenv("HAPROXY_MWORKER_REEXEC") != NULL)
1662 global.mode |= MODE_MWORKER;
1663
Willy Tarreaubaaee002006-06-26 02:48:02 +02001664 /*
1665 * Initialize the previously static variables.
1666 */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001667
Willy Tarreau173d9952018-01-26 21:48:23 +01001668 totalconn = actconn = listeners = stopping = 0;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001669 killed = 0;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001670
Willy Tarreaubaaee002006-06-26 02:48:02 +02001671
1672#ifdef HAPROXY_MEMMAX
Willy Tarreau70060452015-12-14 12:46:07 +01001673 global.rlimit_memmax_all = HAPROXY_MEMMAX;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001674#endif
1675
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001676 tzset();
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001677 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001678 start_date = now;
1679
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001680 ha_random_boot(argv);
Willy Tarreau84310e22014-02-14 11:59:04 +01001681
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001682 if (init_acl() != 0)
1683 exit(1);
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001684
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001685 /* Initialise lua. */
1686 hlua_init();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001687
Christopher Fauletff2613e2016-11-09 11:36:17 +01001688 /* Initialize process vars */
1689 vars_init(&global.vars, SCOPE_PROC);
1690
Willy Tarreau43b78992009-01-25 15:42:27 +01001691 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaue5733232019-05-22 19:24:06 +02001692#if defined(USE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001693 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001694#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001695#if defined(USE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001696 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001697#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001698#if defined(USE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +01001699 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001700#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001701#if defined(USE_EVPORTS)
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +00001702 global.tune.options |= GTUNE_USE_EVPORTS;
1703#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001704#if defined(USE_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001705 global.tune.options |= GTUNE_USE_SPLICE;
1706#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001707#if defined(USE_GETADDRINFO)
1708 global.tune.options |= GTUNE_USE_GAI;
1709#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001710#if defined(SO_REUSEPORT)
1711 global.tune.options |= GTUNE_USE_REUSEPORT;
1712#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001713
1714 pid = getpid();
1715 progname = *argv;
1716 while ((tmp = strchr(progname, '/')) != NULL)
1717 progname = tmp + 1;
1718
Kevinm48936af2010-12-22 16:08:21 +00001719 /* the process name is used for the logs only */
Dragan Dosen43885c72015-10-01 13:18:13 +02001720 chunk_initstr(&global.log_tag, strdup(progname));
Kevinm48936af2010-12-22 16:08:21 +00001721
Willy Tarreaubaaee002006-06-26 02:48:02 +02001722 argc--; argv++;
1723 while (argc > 0) {
1724 char *flag;
1725
1726 if (**argv == '-') {
1727 flag = *argv+1;
1728
1729 /* 1 arg */
1730 if (*flag == 'v') {
1731 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +01001732 if (flag[1] == 'v') /* -vv */
1733 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001734 exit(0);
1735 }
Willy Tarreaue5733232019-05-22 19:24:06 +02001736#if defined(USE_EPOLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001737 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +01001738 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001739#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001740#if defined(USE_POLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001741 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +01001742 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001743#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001744#if defined(USE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001745 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +01001746 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001747#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001748#if defined(USE_EVPORTS)
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +00001749 else if (*flag == 'd' && flag[1] == 'v')
1750 global.tune.options &= ~GTUNE_USE_EVPORTS;
1751#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001752#if defined(USE_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001753 else if (*flag == 'd' && flag[1] == 'S')
1754 global.tune.options &= ~GTUNE_USE_SPLICE;
1755#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001756#if defined(USE_GETADDRINFO)
1757 else if (*flag == 'd' && flag[1] == 'G')
1758 global.tune.options &= ~GTUNE_USE_GAI;
1759#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001760#if defined(SO_REUSEPORT)
1761 else if (*flag == 'd' && flag[1] == 'R')
1762 global.tune.options &= ~GTUNE_USE_REUSEPORT;
1763#endif
Emeric Brun850efd52014-01-29 12:24:34 +01001764 else if (*flag == 'd' && flag[1] == 'V')
1765 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001766 else if (*flag == 'V')
1767 arg_mode |= MODE_VERBOSE;
1768 else if (*flag == 'd' && flag[1] == 'b')
1769 arg_mode |= MODE_FOREGROUND;
Willy Tarreau6e064432012-05-08 15:40:42 +02001770 else if (*flag == 'd' && flag[1] == 'M')
1771 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreau3eed10e2016-11-07 21:03:16 +01001772 else if (*flag == 'd' && flag[1] == 'r')
1773 global.tune.options |= GTUNE_RESOLVE_DONTFAIL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001774 else if (*flag == 'd')
1775 arg_mode |= MODE_DEBUG;
1776 else if (*flag == 'c')
1777 arg_mode |= MODE_CHECK;
William Lallemand095ba4c2017-06-01 17:38:50 +02001778 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +02001779 arg_mode |= MODE_DAEMON;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001780 else if (*flag == 'W' && flag[1] == 's') {
Lukas Tribusf46bf952017-11-21 12:39:34 +01001781 arg_mode |= MODE_MWORKER | MODE_FOREGROUND;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001782#if defined(USE_SYSTEMD)
1783 global.tune.options |= GTUNE_USE_SYSTEMD;
1784#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001785 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 +01001786 usage(progname);
1787#endif
1788 }
William Lallemand095ba4c2017-06-01 17:38:50 +02001789 else if (*flag == 'W')
1790 arg_mode |= MODE_MWORKER;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001791 else if (*flag == 'q')
1792 arg_mode |= MODE_QUIET;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001793 else if (*flag == 'x') {
William Lallemand45eff442017-06-19 15:57:55 +02001794 if (argc <= 1 || argv[1][0] == '-') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001795 ha_alert("Unix socket path expected with the -x flag\n\n");
William Lallemand45eff442017-06-19 15:57:55 +02001796 usage(progname);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001797 }
William Lallemand4fc09692017-06-19 16:37:19 +02001798 if (old_unixsocket)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001799 ha_warning("-x option already set, overwriting the value\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001800 old_unixsocket = argv[1];
William Lallemand4fc09692017-06-19 16:37:19 +02001801
Olivier Houchardf73629d2017-04-05 22:33:04 +02001802 argv++;
1803 argc--;
1804 }
William Lallemande7361152018-10-26 14:47:36 +02001805 else if (*flag == 'S') {
1806 struct wordlist *c;
1807
1808 if (argc <= 1 || argv[1][0] == '-') {
1809 ha_alert("Socket and optional bind parameters expected with the -S flag\n");
1810 usage(progname);
1811 }
1812 if ((c = malloc(sizeof(*c))) == NULL || (c->s = strdup(argv[1])) == NULL) {
1813 ha_alert("Cannot allocate memory\n");
1814 exit(EXIT_FAILURE);
1815 }
1816 LIST_ADD(&mworker_cli_conf, &c->list);
1817
1818 argv++;
1819 argc--;
1820 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001821 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1822 /* list of pids to finish ('f') or terminate ('t') */
1823
1824 if (flag[1] == 'f')
1825 oldpids_sig = SIGUSR1; /* finish then exit */
1826 else
1827 oldpids_sig = SIGTERM; /* terminate immediately */
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001828 while (argc > 1 && argv[1][0] != '-') {
Chris Lane236062f2018-02-05 23:15:44 +00001829 char * endptr = NULL;
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001830 oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int));
1831 if (!oldpids) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001832 ha_alert("Cannot allocate old pid : out of memory.\n");
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001833 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001834 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001835 argc--; argv++;
Chris Lane236062f2018-02-05 23:15:44 +00001836 errno = 0;
1837 oldpids[nb_oldpids] = strtol(*argv, &endptr, 10);
1838 if (errno) {
1839 ha_alert("-%2s option: failed to parse {%s}: %s\n",
1840 flag,
1841 *argv, strerror(errno));
1842 exit(1);
1843 } else if (endptr && strlen(endptr)) {
Willy Tarreau90807112020-02-25 08:16:33 +01001844 while (isspace((unsigned char)*endptr)) endptr++;
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001845 if (*endptr != 0) {
Chris Lane236062f2018-02-05 23:15:44 +00001846 ha_alert("-%2s option: some bytes unconsumed in PID list {%s}\n",
1847 flag, endptr);
1848 exit(1);
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001849 }
Chris Lane236062f2018-02-05 23:15:44 +00001850 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001851 if (oldpids[nb_oldpids] <= 0)
1852 usage(progname);
1853 nb_oldpids++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001854 }
1855 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001856 else if (flag[0] == '-' && flag[1] == 0) { /* "--" */
1857 /* now that's a cfgfile list */
1858 argv++; argc--;
1859 while (argc > 0) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02001860 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001861 ha_alert("Cannot load configuration file/directory %s : %s\n",
1862 *argv,
1863 err_msg);
Willy Tarreaua088d312015-10-08 11:58:48 +02001864 exit(1);
1865 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001866 argv++; argc--;
1867 }
1868 break;
1869 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001870 else { /* >=2 args */
1871 argv++; argc--;
1872 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001873 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001874
1875 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +02001876 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001877 case 'n' : cfg_maxconn = atol(*argv); break;
Willy Tarreau70060452015-12-14 12:46:07 +01001878 case 'm' : global.rlimit_memmax_all = atol(*argv); break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001879 case 'N' : cfg_maxpconn = atol(*argv); break;
William Lallemanddaf4cd22018-04-17 16:46:13 +02001880 case 'L' :
1881 strncpy(localpeer, *argv, sizeof(localpeer) - 1);
1882 setenv("HAPROXY_LOCALPEER", localpeer, 1);
1883 break;
Willy Tarreau5d01a632009-06-22 16:02:30 +02001884 case 'f' :
Maxime de Roucy0f503922016-05-13 23:52:55 +02001885 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001886 ha_alert("Cannot load configuration file/directory %s : %s\n",
1887 *argv,
1888 err_msg);
Willy Tarreau5d01a632009-06-22 16:02:30 +02001889 exit(1);
1890 }
Willy Tarreau5d01a632009-06-22 16:02:30 +02001891 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001892 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001893 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001894 }
1895 }
1896 }
1897 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001898 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001899 argv++; argc--;
1900 }
1901
Christopher Faulete3a5e352017-10-24 13:53:54 +02001902 global.mode |= (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
1903 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001904
William Lallemand944e6192018-11-21 15:48:31 +01001905 if (getenv("HAPROXY_MWORKER_WAIT_ONLY")) {
William Lallemandcb11fd22017-06-01 17:38:52 +02001906 unsetenv("HAPROXY_MWORKER_WAIT_ONLY");
William Lallemand944e6192018-11-21 15:48:31 +01001907 global.mode |= MODE_MWORKER_WAIT;
1908 global.mode &= ~MODE_MWORKER;
William Lallemandcb11fd22017-06-01 17:38:52 +02001909 }
1910
1911 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {
1912 atexit_flag = 1;
1913 atexit(reexec_on_failure);
1914 }
1915
Willy Tarreau576132e2011-09-10 19:26:56 +02001916 if (change_dir && chdir(change_dir) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001917 ha_alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
Willy Tarreau576132e2011-09-10 19:26:56 +02001918 exit(1);
1919 }
1920
Willy Tarreaubaaee002006-06-26 02:48:02 +02001921 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001922
1923 init_default_instance();
1924
William Lallemand944e6192018-11-21 15:48:31 +01001925 /* in wait mode, we don't try to read the configuration files */
1926 if (!(global.mode & MODE_MWORKER_WAIT)) {
William Lallemand7b302d82019-05-20 11:15:37 +02001927 struct buffer *trash = get_trash_chunk();
Willy Tarreauc4382422009-12-06 13:10:44 +01001928
William Lallemand944e6192018-11-21 15:48:31 +01001929 /* handle cfgfiles that are actually directories */
1930 cfgfiles_expand_directories();
1931
1932 if (LIST_ISEMPTY(&cfg_cfgfiles))
1933 usage(progname);
1934
1935
1936 list_for_each_entry(wl, &cfg_cfgfiles, list) {
1937 int ret;
1938
William Lallemand7b302d82019-05-20 11:15:37 +02001939 if (trash->data)
1940 chunk_appendf(trash, ";");
1941
1942 chunk_appendf(trash, "%s", wl->s);
1943
William Lallemand944e6192018-11-21 15:48:31 +01001944 ret = readcfgfile(wl->s);
1945 if (ret == -1) {
1946 ha_alert("Could not open configuration file %s : %s\n",
1947 wl->s, strerror(errno));
1948 exit(1);
1949 }
1950 if (ret & (ERR_ABORT|ERR_FATAL))
1951 ha_alert("Error(s) found in configuration file : %s\n", wl->s);
1952 err_code |= ret;
1953 if (err_code & ERR_ABORT)
1954 exit(1);
Willy Tarreauc4382422009-12-06 13:10:44 +01001955 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001956
William Lallemand944e6192018-11-21 15:48:31 +01001957 /* do not try to resolve arguments nor to spot inconsistencies when
1958 * the configuration contains fatal errors caused by files not found
1959 * or failed memory allocations.
1960 */
1961 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1962 ha_alert("Fatal errors found in configuration.\n");
1963 exit(1);
1964 }
William Lallemand7b302d82019-05-20 11:15:37 +02001965 if (trash->data)
1966 setenv("HAPROXY_CFGFILES", trash->area, 1);
1967
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02001968 }
William Lallemandce83b4a2018-10-26 14:47:30 +02001969 if (global.mode & MODE_MWORKER) {
1970 int proc;
William Lallemand16dd1b32018-11-19 18:46:18 +01001971 struct mworker_proc *tmproc;
1972
William Lallemand482f9a92019-04-12 16:15:00 +02001973 setenv("HAPROXY_MWORKER", "1", 1);
1974
William Lallemand16dd1b32018-11-19 18:46:18 +01001975 if (getenv("HAPROXY_MWORKER_REEXEC") == NULL) {
1976
William Lallemandf3a86832019-04-01 11:29:58 +02001977 tmproc = calloc(1, sizeof(*tmproc));
William Lallemand16dd1b32018-11-19 18:46:18 +01001978 if (!tmproc) {
1979 ha_alert("Cannot allocate process structures.\n");
1980 exit(EXIT_FAILURE);
1981 }
William Lallemand8f7069a2019-04-12 16:09:23 +02001982 tmproc->options |= PROC_O_TYPE_MASTER; /* master */
William Lallemand16dd1b32018-11-19 18:46:18 +01001983 tmproc->reloads = 0;
1984 tmproc->relative_pid = 0;
1985 tmproc->pid = pid;
1986 tmproc->timestamp = start_date.tv_sec;
1987 tmproc->ipc_fd[0] = -1;
1988 tmproc->ipc_fd[1] = -1;
1989
1990 proc_self = tmproc;
1991
1992 LIST_ADDQ(&proc_list, &tmproc->list);
1993 }
William Lallemandce83b4a2018-10-26 14:47:30 +02001994
1995 for (proc = 0; proc < global.nbproc; proc++) {
William Lallemandce83b4a2018-10-26 14:47:30 +02001996
William Lallemandf3a86832019-04-01 11:29:58 +02001997 tmproc = calloc(1, sizeof(*tmproc));
William Lallemandce83b4a2018-10-26 14:47:30 +02001998 if (!tmproc) {
1999 ha_alert("Cannot allocate process structures.\n");
2000 exit(EXIT_FAILURE);
2001 }
2002
William Lallemand8f7069a2019-04-12 16:09:23 +02002003 tmproc->options |= PROC_O_TYPE_WORKER; /* worker */
William Lallemandce83b4a2018-10-26 14:47:30 +02002004 tmproc->pid = -1;
2005 tmproc->reloads = 0;
William Lallemande3683302018-11-19 18:46:17 +01002006 tmproc->timestamp = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02002007 tmproc->relative_pid = 1 + proc;
William Lallemand550db6d2018-11-06 17:37:12 +01002008 tmproc->ipc_fd[0] = -1;
2009 tmproc->ipc_fd[1] = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02002010
2011 if (mworker_cli_sockpair_new(tmproc, proc) < 0) {
2012 exit(EXIT_FAILURE);
2013 }
2014
2015 LIST_ADDQ(&proc_list, &tmproc->list);
2016 }
William Lallemand944e6192018-11-21 15:48:31 +01002017 }
2018 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
2019 struct wordlist *it, *c;
2020
William Lallemand1b663612018-10-26 14:47:33 +02002021 mworker_env_to_proc_list(); /* get the info of the children in the env */
William Lallemand8a022572018-10-26 14:47:35 +02002022
William Lallemande7361152018-10-26 14:47:36 +02002023
William Lallemand550db6d2018-11-06 17:37:12 +01002024 if (!LIST_ISEMPTY(&mworker_cli_conf)) {
William Lallemande7361152018-10-26 14:47:36 +02002025
William Lallemand550db6d2018-11-06 17:37:12 +01002026 if (mworker_cli_proxy_create() < 0) {
William Lallemande7361152018-10-26 14:47:36 +02002027 ha_alert("Can't create the master's CLI.\n");
2028 exit(EXIT_FAILURE);
2029 }
William Lallemande7361152018-10-26 14:47:36 +02002030
William Lallemand550db6d2018-11-06 17:37:12 +01002031 list_for_each_entry_safe(c, it, &mworker_cli_conf, list) {
2032
2033 if (mworker_cli_proxy_new_listener(c->s) < 0) {
2034 ha_alert("Can't create the master's CLI.\n");
2035 exit(EXIT_FAILURE);
2036 }
2037 LIST_DEL(&c->list);
2038 free(c->s);
2039 free(c);
2040 }
2041 }
William Lallemandce83b4a2018-10-26 14:47:30 +02002042 }
2043
Willy Tarreaubb925012009-07-23 13:36:36 +02002044 err_code |= check_config_validity();
Christopher Fauletc1692962019-08-12 09:51:07 +02002045 for (px = proxies_list; px; px = px->next) {
2046 struct server *srv;
2047 struct post_proxy_check_fct *ppcf;
2048 struct post_server_check_fct *pscf;
2049
2050 list_for_each_entry(pscf, &post_server_check_list, list) {
2051 for (srv = px->srv; srv; srv = srv->next)
2052 err_code |= pscf->fct(srv);
2053 }
2054 list_for_each_entry(ppcf, &post_proxy_check_list, list)
2055 err_code |= ppcf->fct(px);
2056 }
Willy Tarreaubb925012009-07-23 13:36:36 +02002057 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002058 ha_alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002059 exit(1);
2060 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002061
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002062 err_code |= pattern_finalize_config();
2063 if (err_code & (ERR_ABORT|ERR_FATAL)) {
2064 ha_alert("Failed to finalize pattern config.\n");
2065 exit(1);
2066 }
Willy Tarreau0f936722019-04-11 14:47:08 +02002067
Willy Tarreau70060452015-12-14 12:46:07 +01002068 /* recompute the amount of per-process memory depending on nbproc and
2069 * the shared SSL cache size (allowed to exist in all processes).
2070 */
2071 if (global.rlimit_memmax_all) {
2072#if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE)
2073 int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL;
2074
2075 global.rlimit_memmax =
2076 ((((int64_t)global.rlimit_memmax_all * 1048576LL) -
2077 ssl_cache_bytes) / global.nbproc +
2078 ssl_cache_bytes + 1048575LL) / 1048576LL;
2079#else
2080 global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc;
2081#endif
2082 }
2083
Willy Tarreaue5733232019-05-22 19:24:06 +02002084#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002085 err_code |= netns_init();
2086 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002087 ha_alert("Failed to initialize namespace support.\n");
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002088 exit(1);
2089 }
2090#endif
2091
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01002092 /* Apply server states */
2093 apply_server_state();
2094
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002095 for (px = proxies_list; px; px = px->next)
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01002096 srv_compute_all_admin_states(px);
2097
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01002098 /* Apply servers' configured address */
2099 err_code |= srv_init_addr();
2100 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002101 ha_alert("Failed to initialize server(s) addr.\n");
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01002102 exit(1);
2103 }
2104
Willy Tarreaubaaee002006-06-26 02:48:02 +02002105 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +01002106 struct peers *pr;
2107 struct proxy *px;
2108
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002109 for (pr = cfg_peers; pr; pr = pr->next)
Willy Tarreau8b15ba12012-02-02 17:48:18 +01002110 if (pr->peers_fe)
2111 break;
2112
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002113 for (px = proxies_list; px; px = px->next)
Willy Tarreau4348fad2012-09-20 16:48:07 +02002114 if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners))
Willy Tarreau8b15ba12012-02-02 17:48:18 +01002115 break;
2116
2117 if (pr || px) {
2118 /* At least one peer or one listener has been found */
2119 qfprintf(stdout, "Configuration file is valid\n");
2120 exit(0);
2121 }
2122 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
2123 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002124 }
Willy Tarreaue9b26022011-08-01 20:57:55 +02002125
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002126 /* now we know the buffer size, we can initialize the channels and buffers */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002127 init_buffer();
Willy Tarreau8280d642009-09-23 23:37:52 +02002128
Willy Tarreaue6945732016-12-21 19:57:00 +01002129 list_for_each_entry(pcf, &post_check_list, list) {
2130 err_code |= pcf->fct();
2131 if (err_code & (ERR_ABORT|ERR_FATAL))
2132 exit(1);
2133 }
2134
Willy Tarreaubaaee002006-06-26 02:48:02 +02002135 if (cfg_maxconn > 0)
2136 global.maxconn = cfg_maxconn;
2137
Willy Tarreau8d687d82019-03-01 09:39:42 +01002138 if (global.stats_fe)
2139 global.maxsock += global.stats_fe->maxconn;
2140
2141 if (cfg_peers) {
2142 /* peers also need to bypass global maxconn */
2143 struct peers *p = cfg_peers;
2144
2145 for (p = cfg_peers; p; p = p->next)
2146 if (p->peers_fe)
2147 global.maxsock += p->peers_fe->maxconn;
2148 }
2149
Willy Tarreaubaaee002006-06-26 02:48:02 +02002150 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +02002151 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002152 global.pidfile = strdup(cfg_pidfile);
2153 }
2154
Willy Tarreaud0256482015-01-15 21:45:22 +01002155 /* Now we want to compute the maxconn and possibly maxsslconn values.
Willy Tarreauac350932019-03-01 15:43:14 +01002156 * It's a bit tricky. Maxconn defaults to the pre-computed value based
2157 * on rlim_fd_cur and the number of FDs in use due to the configuration,
2158 * and maxsslconn defaults to DEFAULT_MAXSSLCONN. On top of that we can
2159 * enforce a lower limit based on memmax.
Willy Tarreaud0256482015-01-15 21:45:22 +01002160 *
2161 * If memmax is set, then it depends on which values are set. If
2162 * maxsslconn is set, we use memmax to determine how many cleartext
2163 * connections may be added, and set maxconn to the sum of the two.
2164 * If maxconn is set and not maxsslconn, maxsslconn is computed from
2165 * the remaining amount of memory between memmax and the cleartext
2166 * connections. If neither are set, then it is considered that all
2167 * connections are SSL-capable, and maxconn is computed based on this,
2168 * then maxsslconn accordingly. We need to know if SSL is used on the
2169 * frontends, backends, or both, because when it's used on both sides,
2170 * we need twice the value for maxsslconn, but we only count the
2171 * handshake once since it is not performed on the two sides at the
2172 * same time (frontend-side is terminated before backend-side begins).
2173 * The SSL stack is supposed to have filled ssl_session_cost and
Willy Tarreau474b96a2015-01-28 19:03:21 +01002174 * ssl_handshake_cost during its initialization. In any case, if
2175 * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for
2176 * maxconn in order to protect the system.
Willy Tarreaud0256482015-01-15 21:45:22 +01002177 */
Willy Tarreauac350932019-03-01 15:43:14 +01002178 ideal_maxconn = compute_ideal_maxconn();
2179
Willy Tarreaud0256482015-01-15 21:45:22 +01002180 if (!global.rlimit_memmax) {
2181 if (global.maxconn == 0) {
Willy Tarreauac350932019-03-01 15:43:14 +01002182 global.maxconn = ideal_maxconn;
Willy Tarreaud0256482015-01-15 21:45:22 +01002183 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2184 fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn);
2185 }
2186 }
2187#ifdef USE_OPENSSL
2188 else if (!global.maxconn && !global.maxsslconn &&
2189 (global.ssl_used_frontend || global.ssl_used_backend)) {
2190 /* memmax is set, compute everything automatically. Here we want
2191 * to ensure that all SSL connections will be served. We take
2192 * care of the number of sides where SSL is used, and consider
2193 * the worst case : SSL used on both sides and doing a handshake
2194 * simultaneously. Note that we can't have more than maxconn
2195 * handshakes at a time by definition, so for the worst case of
2196 * two SSL conns per connection, we count a single handshake.
2197 */
2198 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2199 int64_t mem = global.rlimit_memmax * 1048576ULL;
Willy Tarreau304e17e2020-03-10 17:54:54 +01002200 int retried = 0;
Willy Tarreaud0256482015-01-15 21:45:22 +01002201
2202 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2203 mem -= global.maxzlibmem;
2204 mem = mem * MEM_USABLE_RATIO;
2205
Willy Tarreau304e17e2020-03-10 17:54:54 +01002206 /* Principle: we test once to set maxconn according to the free
2207 * memory. If it results in values the system rejects, we try a
2208 * second time by respecting rlim_fd_max. If it fails again, we
2209 * go back to the initial value and will let the final code
2210 * dealing with rlimit report the error. That's up to 3 attempts.
2211 */
2212 do {
2213 global.maxconn = mem /
2214 ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream
2215 sides * global.ssl_session_max_cost + // SSL buffers, one per side
2216 global.ssl_handshake_max_cost); // 1 handshake per connection max
Willy Tarreaud0256482015-01-15 21:45:22 +01002217
Willy Tarreau304e17e2020-03-10 17:54:54 +01002218 if (retried == 1)
2219 global.maxconn = MIN(global.maxconn, ideal_maxconn);
2220 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01002221#ifdef SYSTEM_MAXCONN
Willy Tarreau304e17e2020-03-10 17:54:54 +01002222 if (global.maxconn > SYSTEM_MAXCONN)
2223 global.maxconn = SYSTEM_MAXCONN;
Willy Tarreau474b96a2015-01-28 19:03:21 +01002224#endif /* SYSTEM_MAXCONN */
Willy Tarreau304e17e2020-03-10 17:54:54 +01002225 global.maxsslconn = sides * global.maxconn;
2226
2227 if (check_if_maxsock_permitted(compute_ideal_maxsock(global.maxconn)))
2228 break;
2229 } while (retried++ < 2);
2230
Willy Tarreaud0256482015-01-15 21:45:22 +01002231 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2232 fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n",
2233 global.maxconn, global.maxsslconn);
2234 }
2235 else if (!global.maxsslconn &&
2236 (global.ssl_used_frontend || global.ssl_used_backend)) {
2237 /* memmax and maxconn are known, compute maxsslconn automatically.
2238 * maxsslconn being forced, we don't know how many of it will be
2239 * on each side if both sides are being used. The worst case is
2240 * when all connections use only one SSL instance because
2241 * handshakes may be on two sides at the same time.
2242 */
2243 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2244 int64_t mem = global.rlimit_memmax * 1048576ULL;
2245 int64_t sslmem;
2246
2247 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2248 mem -= global.maxzlibmem;
2249 mem = mem * MEM_USABLE_RATIO;
2250
Willy Tarreau87b09662015-04-03 00:22:06 +02002251 sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01002252 global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost);
2253 global.maxsslconn = round_2dig(global.maxsslconn);
2254
2255 if (sslmem <= 0 || global.maxsslconn < sides) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002256 ha_alert("Cannot compute the automatic maxsslconn because global.maxconn is already too "
2257 "high for the global.memmax value (%d MB). The absolute maximum possible value "
2258 "without SSL is %d, but %d was found and SSL is in use.\n",
2259 global.rlimit_memmax,
2260 (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)),
2261 global.maxconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01002262 exit(1);
2263 }
2264
2265 if (global.maxsslconn > sides * global.maxconn)
2266 global.maxsslconn = sides * global.maxconn;
2267
2268 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2269 fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn);
2270 }
2271#endif
2272 else if (!global.maxconn) {
2273 /* memmax and maxsslconn are known/unused, compute maxconn automatically */
2274 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2275 int64_t mem = global.rlimit_memmax * 1048576ULL;
2276 int64_t clearmem;
Willy Tarreau304e17e2020-03-10 17:54:54 +01002277 int retried = 0;
Willy Tarreaud0256482015-01-15 21:45:22 +01002278
2279 if (global.ssl_used_frontend || global.ssl_used_backend)
2280 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2281
2282 mem -= global.maxzlibmem;
2283 mem = mem * MEM_USABLE_RATIO;
2284
2285 clearmem = mem;
2286 if (sides)
2287 clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn;
2288
Willy Tarreau304e17e2020-03-10 17:54:54 +01002289 /* Principle: we test once to set maxconn according to the free
2290 * memory. If it results in values the system rejects, we try a
2291 * second time by respecting rlim_fd_max. If it fails again, we
2292 * go back to the initial value and will let the final code
2293 * dealing with rlimit report the error. That's up to 3 attempts.
2294 */
2295 do {
2296 global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize);
2297 if (retried == 1)
2298 global.maxconn = MIN(global.maxconn, ideal_maxconn);
2299 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01002300#ifdef SYSTEM_MAXCONN
Willy Tarreau304e17e2020-03-10 17:54:54 +01002301 if (global.maxconn > SYSTEM_MAXCONN)
2302 global.maxconn = SYSTEM_MAXCONN;
Willy Tarreau474b96a2015-01-28 19:03:21 +01002303#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01002304
Willy Tarreau304e17e2020-03-10 17:54:54 +01002305 if (clearmem <= 0 || !global.maxconn) {
2306 ha_alert("Cannot compute the automatic maxconn because global.maxsslconn is already too "
2307 "high for the global.memmax value (%d MB). The absolute maximum possible value "
2308 "is %d, but %d was found.\n",
2309 global.rlimit_memmax,
Christopher Faulet767a84b2017-11-24 16:50:31 +01002310 (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)),
Willy Tarreau304e17e2020-03-10 17:54:54 +01002311 global.maxsslconn);
2312 exit(1);
2313 }
2314
2315 if (check_if_maxsock_permitted(compute_ideal_maxsock(global.maxconn)))
2316 break;
2317 } while (retried++ < 2);
Willy Tarreaud0256482015-01-15 21:45:22 +01002318
2319 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
2320 if (sides && global.maxsslconn > sides * global.maxconn) {
2321 fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn "
2322 "to be limited to %d. Better reduce global.maxsslconn to get more "
2323 "room for extra connections.\n", global.maxsslconn, global.maxconn);
2324 }
2325 fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn);
2326 }
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002327 }
2328
Willy Tarreaua409f302020-03-10 17:08:53 +01002329 global.maxsock = compute_ideal_maxsock(global.maxconn);
2330 global.hardmaxconn = global.maxconn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002331
Olivier Houchard88698d92019-04-16 19:07:22 +02002332 /* update connection pool thresholds */
2333 global.tune.pool_low_count = ((long long)global.maxsock * global.tune.pool_low_ratio + 99) / 100;
2334 global.tune.pool_high_count = ((long long)global.maxsock * global.tune.pool_high_ratio + 99) / 100;
2335
Willy Tarreauc8d5b952019-02-27 17:25:52 +01002336 proxy_adjust_all_maxconn();
2337
Willy Tarreau1db37712007-06-03 17:16:49 +02002338 if (global.tune.maxpollevents <= 0)
2339 global.tune.maxpollevents = MAX_POLL_EVENTS;
2340
Olivier Houchard1599b802018-05-24 18:59:04 +02002341 if (global.tune.runqueue_depth <= 0)
2342 global.tune.runqueue_depth = RUNQUEUE_DEPTH;
2343
Willy Tarreau6f4a82c2009-03-21 20:43:57 +01002344 if (global.tune.recv_enough == 0)
2345 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
2346
Willy Tarreau27a674e2009-08-17 07:23:33 +02002347 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
2348 global.tune.maxrewrite = global.tune.bufsize / 2;
2349
Willy Tarreaubaaee002006-06-26 02:48:02 +02002350 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
2351 /* command line debug mode inhibits configuration mode */
William Lallemand095ba4c2017-06-01 17:38:50 +02002352 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002353 global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND));
2354 }
2355
William Lallemand095ba4c2017-06-01 17:38:50 +02002356 if (arg_mode & MODE_DAEMON) {
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002357 /* command line daemon mode inhibits foreground and debug modes mode */
2358 global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND);
William Lallemand095ba4c2017-06-01 17:38:50 +02002359 global.mode |= arg_mode & MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002360 }
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002361
2362 global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002363
William Lallemand095ba4c2017-06-01 17:38:50 +02002364 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002365 ha_warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
William Lallemand095ba4c2017-06-01 17:38:50 +02002366 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002367 }
2368
William Lallemand095ba4c2017-06-01 17:38:50 +02002369 if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002370 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
Christopher Faulet767a84b2017-11-24 16:50:31 +01002371 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 +02002372 global.nbproc = 1;
2373 }
2374
2375 if (global.nbproc < 1)
2376 global.nbproc = 1;
2377
Christopher Fauletbe0faa22017-08-29 15:37:10 +02002378 if (global.nbthread < 1)
2379 global.nbthread = 1;
2380
Christopher Faulet3ef26392017-08-29 16:46:57 +02002381 /* Realloc trash buffers because global.tune.bufsize may have changed */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002382 if (!init_trash_buffers(0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002383 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet3ef26392017-08-29 16:46:57 +02002384 exit(1);
2385 }
2386
Christopher Faulet96d44832017-11-14 22:02:30 +01002387 if (!init_log_buffers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002388 ha_alert("failed to initialize log buffers.\n");
Christopher Faulet96d44832017-11-14 22:02:30 +01002389 exit(1);
2390 }
2391
Willy Tarreauef1d1f82007-04-16 00:25:25 +02002392 /*
2393 * Note: we could register external pollers here.
2394 * Built-in pollers have been registered before main().
2395 */
Willy Tarreau4f60f162007-04-08 16:39:58 +02002396
Willy Tarreau43b78992009-01-25 15:42:27 +01002397 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +02002398 disable_poller("kqueue");
2399
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +00002400 if (!(global.tune.options & GTUNE_USE_EVPORTS))
2401 disable_poller("evports");
2402
Willy Tarreau43b78992009-01-25 15:42:27 +01002403 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002404 disable_poller("epoll");
2405
Willy Tarreau43b78992009-01-25 15:42:27 +01002406 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002407 disable_poller("poll");
2408
Willy Tarreau43b78992009-01-25 15:42:27 +01002409 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002410 disable_poller("select");
2411
2412 /* Note: we could disable any poller by name here */
2413
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002414 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +02002415 list_pollers(stderr);
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002416 fprintf(stderr, "\n");
2417 list_filters(stderr);
2418 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002419
Willy Tarreau4f60f162007-04-08 16:39:58 +02002420 if (!init_pollers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002421 ha_alert("No polling mechanism available.\n"
2422 " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n"
2423 " is too low on this platform to support maxconn and the number of listeners\n"
2424 " and servers. You should rebuild haproxy specifying your system using TARGET=\n"
2425 " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n"
2426 " global maxconn setting to accommodate the system's limitation. For reference,\n"
2427 " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n"
2428 " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n"
2429 " check build settings using 'haproxy -vv'.\n\n",
2430 FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002431 exit(1);
2432 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002433 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
2434 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002435 }
2436
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002437 if (!global.node)
2438 global.node = strdup(hostname);
2439
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01002440 if (!hlua_post_init())
2441 exit(1);
Thomas Holmes6abded42015-05-12 16:23:58 +01002442
Maxime de Roucy0f503922016-05-13 23:52:55 +02002443 free(err_msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002444}
2445
Simon Horman6fb82592011-07-15 13:14:11 +09002446static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +09002447{
Simon Hormanac821422011-07-15 13:14:09 +09002448 struct acl_term_suite *suite, *suiteb;
2449 struct acl_term *term, *termb;
2450
Simon Horman6fb82592011-07-15 13:14:11 +09002451 if (!cond)
2452 return;
2453
2454 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
2455 list_for_each_entry_safe(term, termb, &suite->terms, list) {
2456 LIST_DEL(&term->list);
2457 free(term);
Simon Hormanac821422011-07-15 13:14:09 +09002458 }
Simon Horman6fb82592011-07-15 13:14:11 +09002459 LIST_DEL(&suite->list);
2460 free(suite);
2461 }
2462
2463 free(cond);
2464}
2465
Christopher Fauletcb550132019-12-17 11:25:46 +01002466static void deinit_act_rules(struct list *rules)
Simon Horman6fb82592011-07-15 13:14:11 +09002467{
Christopher Fauletcb550132019-12-17 11:25:46 +01002468 struct act_rule *rule, *ruleb;
Simon Horman6fb82592011-07-15 13:14:11 +09002469
Christopher Fauletcb550132019-12-17 11:25:46 +01002470 list_for_each_entry_safe(rule, ruleb, rules, list) {
2471 LIST_DEL(&rule->list);
2472 deinit_acl_cond(rule->cond);
Christopher Faulet58b35642019-12-17 11:48:42 +01002473 if (rule->release_ptr)
2474 rule->release_ptr(rule);
Christopher Fauletcb550132019-12-17 11:25:46 +01002475 free(rule);
Simon Hormanac821422011-07-15 13:14:09 +09002476 }
2477}
2478
Simon Horman6fb82592011-07-15 13:14:11 +09002479static void deinit_stick_rules(struct list *rules)
2480{
2481 struct sticking_rule *rule, *ruleb;
2482
2483 list_for_each_entry_safe(rule, ruleb, rules, list) {
2484 LIST_DEL(&rule->list);
2485 deinit_acl_cond(rule->cond);
Christopher Faulet476e5d02016-10-26 11:34:47 +02002486 release_sample_expr(rule->expr);
Simon Horman6fb82592011-07-15 13:14:11 +09002487 free(rule);
2488 }
2489}
2490
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002491void deinit(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002492{
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002493 struct proxy *p = proxies_list, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002494 struct cap_hdr *h,*h_next;
2495 struct server *s,*s_next;
2496 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002497 struct acl_cond *cond, *condb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002498 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002499 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002500 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002501 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002502 struct wordlist *wl, *wlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002503 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +02002504 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +01002505 struct logformat_node *lf, *lfb;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002506 struct bind_conf *bind_conf, *bind_back;
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002507 struct build_opts_str *bol, *bolb;
Willy Tarreau05554e62016-12-21 20:46:26 +01002508 struct post_deinit_fct *pdf;
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +02002509 struct proxy_deinit_fct *pxdf;
2510 struct server_deinit_fct *srvdf;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002511
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002512 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002513 while (p) {
Willy Tarreau8113a5d2012-10-04 08:01:43 +02002514 free(p->conf.file);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002515 free(p->id);
2516 free(p->check_req);
2517 free(p->cookie_name);
2518 free(p->cookie_domain);
Christopher Faulet2f533902020-01-21 11:06:48 +01002519 free(p->cookie_attrs);
Willy Tarreau4c03d1c2019-01-14 15:23:54 +01002520 free(p->lbprm.arg_str);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002521 free(p->capture_name);
2522 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +09002523 free(p->rdp_cookie_name);
Willy Tarreau62a61232013-04-12 18:13:46 +02002524 if (p->conf.logformat_string != default_http_log_format &&
2525 p->conf.logformat_string != default_tcp_log_format &&
2526 p->conf.logformat_string != clf_http_log_format)
2527 free(p->conf.logformat_string);
Willy Tarreau196729e2012-05-31 19:30:26 +02002528
Willy Tarreau62a61232013-04-12 18:13:46 +02002529 free(p->conf.lfs_file);
2530 free(p->conf.uniqueid_format_string);
2531 free(p->conf.uif_file);
Willy Tarreau0cac26c2019-01-14 16:55:42 +01002532 if ((p->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_MAP)
2533 free(p->lbprm.map.srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002534
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002535 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
2536 free(p->conf.logformat_sd_string);
2537 free(p->conf.lfsd_file);
2538
Willy Tarreaub80c2302007-11-30 20:51:32 +01002539 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
2540 LIST_DEL(&cond->list);
2541 prune_acl_cond(cond);
2542 free(cond);
2543 }
2544
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002545 /* build a list of unique uri_auths */
2546 if (!ua)
2547 ua = p->uri_auth;
2548 else {
2549 /* check if p->uri_auth is unique */
2550 for (uap = ua; uap; uap=uap->next)
2551 if (uap == p->uri_auth)
2552 break;
2553
Willy Tarreauaccc4e12008-06-24 11:14:45 +02002554 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002555 /* add it, if it is */
2556 p->uri_auth->next = ua;
2557 ua = p->uri_auth;
2558 }
2559 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002560
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002561 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
2562 LIST_DEL(&acl->list);
2563 prune_acl(acl);
2564 free(acl);
2565 }
2566
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002567 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
2568 LIST_DEL(&srule->list);
2569 prune_acl_cond(srule->cond);
2570 free(srule->cond);
2571 free(srule);
2572 }
2573
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002574 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
2575 LIST_DEL(&rule->list);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002576 if (rule->cond) {
2577 prune_acl_cond(rule->cond);
2578 free(rule->cond);
2579 }
Dragan Dosen2a7c20f2019-04-30 00:38:36 +02002580 free(rule->file);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002581 free(rule);
2582 }
2583
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002584 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
2585 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +01002586 if (rdr->cond) {
2587 prune_acl_cond(rdr->cond);
2588 free(rdr->cond);
2589 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002590 free(rdr->rdr_str);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002591 list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) {
2592 LIST_DEL(&lf->list);
2593 free(lf);
2594 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002595 free(rdr);
2596 }
2597
William Lallemand0f99e342011-10-12 17:50:54 +02002598 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
2599 LIST_DEL(&log->list);
2600 free(log);
2601 }
2602
William Lallemand723b73a2012-02-08 16:37:49 +01002603 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
2604 LIST_DEL(&lf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +02002605 release_sample_expr(lf->expr);
2606 free(lf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +01002607 free(lf);
2608 }
2609
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002610 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
2611 LIST_DEL(&lf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +02002612 release_sample_expr(lf->expr);
2613 free(lf->arg);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002614 free(lf);
2615 }
2616
Christopher Fauletcb550132019-12-17 11:25:46 +01002617 deinit_act_rules(&p->tcp_req.inspect_rules);
2618 deinit_act_rules(&p->tcp_rep.inspect_rules);
2619 deinit_act_rules(&p->tcp_req.l4_rules);
2620 deinit_act_rules(&p->tcp_req.l5_rules);
2621 deinit_act_rules(&p->http_req_rules);
2622 deinit_act_rules(&p->http_res_rules);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002623 deinit_act_rules(&p->http_after_res_rules);
Simon Hormanac821422011-07-15 13:14:09 +09002624
Simon Horman6fb82592011-07-15 13:14:11 +09002625 deinit_stick_rules(&p->storersp_rules);
2626 deinit_stick_rules(&p->sticking_rules);
2627
Willy Tarreaubaaee002006-06-26 02:48:02 +02002628 h = p->req_cap;
2629 while (h) {
2630 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002631 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002632 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002633 free(h);
2634 h = h_next;
2635 }/* end while(h) */
2636
2637 h = p->rsp_cap;
2638 while (h) {
2639 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002640 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002641 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002642 free(h);
2643 h = h_next;
2644 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002645
Willy Tarreaubaaee002006-06-26 02:48:02 +02002646 s = p->srv;
2647 while (s) {
2648 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002649
Willy Tarreauf6562792019-05-07 19:05:35 +02002650 task_destroy(s->check.task);
2651 task_destroy(s->agent.task);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002652
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002653 if (s->check.wait_list.tasklet)
2654 tasklet_free(s->check.wait_list.tasklet);
2655 if (s->agent.wait_list.tasklet)
2656 tasklet_free(s->agent.wait_list.tasklet);
Dragan Dosen026ef572019-04-30 00:56:20 +02002657
Willy Tarreauf6562792019-05-07 19:05:35 +02002658 task_destroy(s->warmup);
Willy Tarreau2e993902011-10-31 11:53:20 +01002659
Willy Tarreaua534fea2008-08-03 12:19:50 +02002660 free(s->id);
2661 free(s->cookie);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002662 free(s->check.bi.area);
2663 free(s->check.bo.area);
2664 free(s->agent.bi.area);
2665 free(s->agent.bo.area);
James Brown55f9ff12015-10-21 18:19:05 -07002666 free(s->agent.send_string);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002667 free(s->hostname_dn);
Sárközi, László34c01792014-09-05 10:08:23 +02002668 free((char*)s->conf.file);
Olivier Houchard7fc3be72018-11-22 18:50:54 +01002669 free(s->idle_conns);
Olivier Houchard7fc3be72018-11-22 18:50:54 +01002670 free(s->safe_conns);
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002671 free(s->idle_orphan_conns);
Olivier Houchardf1314812019-02-18 16:41:17 +01002672 free(s->curr_idle_thr);
Willy Tarreau17d45382016-12-22 21:16:08 +01002673
2674 if (s->use_ssl || s->check.use_ssl) {
2675 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2676 xprt_get(XPRT_SSL)->destroy_srv(s);
2677 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002678 HA_SPIN_DESTROY(&s->lock);
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +02002679
2680 list_for_each_entry(srvdf, &server_deinit_list, list)
2681 srvdf->fct(s);
2682
Willy Tarreaubaaee002006-06-26 02:48:02 +02002683 free(s);
2684 s = s_next;
2685 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002686
Willy Tarreau4348fad2012-09-20 16:48:07 +02002687 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002688 /*
2689 * Zombie proxy, the listener just pretend to be up
2690 * because they still hold an opened fd.
2691 * Close it and give the listener its real state.
2692 */
2693 if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
2694 close(l->fd);
2695 l->state = LI_INIT;
2696 }
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02002697 unbind_listener(l);
2698 delete_listener(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002699 LIST_DEL(&l->by_fe);
2700 LIST_DEL(&l->by_bind);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002701 free(l->name);
2702 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002703 free(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002704 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002705
Willy Tarreau4348fad2012-09-20 16:48:07 +02002706 /* Release unused SSL configs. */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002707 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01002708 if (bind_conf->xprt->destroy_bind_conf)
2709 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002710 free(bind_conf->file);
2711 free(bind_conf->arg);
2712 LIST_DEL(&bind_conf->by_fe);
2713 free(bind_conf);
2714 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02002715
Christopher Fauletd7c91962015-04-30 11:48:27 +02002716 flt_deinit(p);
2717
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +02002718 list_for_each_entry(pxdf, &proxy_deinit_list, list)
2719 pxdf->fct(p);
2720
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002721 free(p->desc);
2722 free(p->fwdfor_hdr_name);
2723
Olivier Houchard3f795f72019-04-17 22:51:06 +02002724 task_destroy(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002725
Willy Tarreaubafbe012017-11-24 17:34:44 +01002726 pool_destroy(p->req_cap_pool);
2727 pool_destroy(p->rsp_cap_pool);
Dragan Dosen7d61a332019-05-07 14:16:18 +02002728 if (p->table)
2729 pool_destroy(p->table->pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002730
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002731 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002732 p = p->next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002733 HA_SPIN_DESTROY(&p0->lbprm.lock);
2734 HA_SPIN_DESTROY(&p0->lock);
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002735 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002736 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02002737
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002738 while (ua) {
2739 uap = ua;
2740 ua = ua->next;
2741
Willy Tarreaua534fea2008-08-03 12:19:50 +02002742 free(uap->uri_prefix);
2743 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002744 free(uap->node);
2745 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002746
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002747 userlist_free(uap->userlist);
Christopher Fauletcb550132019-12-17 11:25:46 +01002748 deinit_act_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002749
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002750 free(uap);
2751 }
2752
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002753 userlist_free(userlist);
2754
David Carlier834cb2e2015-09-25 12:02:25 +01002755 cfg_unregister_sections();
2756
Christopher Faulet0132d062017-07-26 15:33:35 +02002757 deinit_log_buffers();
David Carlier834cb2e2015-09-25 12:02:25 +01002758
Willy Tarreaudd815982007-10-16 12:25:14 +02002759 protocol_unbind_all();
2760
Willy Tarreau05554e62016-12-21 20:46:26 +01002761 list_for_each_entry(pdf, &post_deinit_list, list)
2762 pdf->fct();
2763
Joe Williamsdf5b38f2010-12-29 17:05:48 +01002764 free(global.log_send_hostname); global.log_send_hostname = NULL;
Dragan Dosen43885c72015-10-01 13:18:13 +02002765 chunk_destroy(&global.log_tag);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002766 free(global.chroot); global.chroot = NULL;
2767 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002768 free(global.node); global.node = NULL;
2769 free(global.desc); global.desc = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002770 free(oldpids); oldpids = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +02002771 task_destroy(idle_conn_task);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01002772 idle_conn_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002773
William Lallemand0f99e342011-10-12 17:50:54 +02002774 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
2775 LIST_DEL(&log->list);
2776 free(log);
2777 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01002778 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02002779 free(wl->s);
Willy Tarreau477ecd82010-01-03 21:12:30 +01002780 LIST_DEL(&wl->list);
2781 free(wl);
2782 }
2783
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002784 list_for_each_entry_safe(bol, bolb, &build_opts_list, list) {
2785 if (bol->must_free)
2786 free((void *)bol->str);
2787 LIST_DEL(&bol->list);
2788 free(bol);
2789 }
2790
Christopher Fauletff2613e2016-11-09 11:36:17 +01002791 vars_prune(&global.vars, NULL, NULL);
Willy Tarreau2455ceb2018-11-26 15:57:34 +01002792 pool_destroy_all();
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002793 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002794} /* end deinit() */
2795
William Lallemand72160322018-11-06 17:37:16 +01002796
Willy Tarreau918ff602011-07-25 16:33:49 +02002797/* Runs the polling loop */
Willy Tarreau3ebd55e2020-03-03 14:59:56 +01002798void run_poll_loop()
Willy Tarreau4f60f162007-04-08 16:39:58 +02002799{
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002800 int next, wake;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002801
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002802 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002803 while (1) {
Willy Tarreauc49ba522019-12-11 08:12:23 +01002804 wake_expired_tasks();
2805
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002806 /* Process a few tasks */
2807 process_runnable_tasks();
2808
William Lallemand1aab50b2018-06-07 09:46:01 +02002809 /* check if we caught some signals and process them in the
2810 first thread */
2811 if (tid == 0)
2812 signal_process_queue();
Willy Tarreau29857942009-05-10 09:01:21 +02002813
Willy Tarreau4b3f27b2020-03-12 17:28:01 +01002814 if (stopping && tasks_run_queue == 0)
2815 _HA_ATOMIC_OR(&stopping_thread_mask, tid_bit);
2816
Willy Tarreau85c459d2018-08-02 10:54:31 +02002817 /* stop when there's nothing left to do */
Willy Tarreau4b3f27b2020-03-12 17:28:01 +01002818 if ((jobs - unstoppable_jobs) == 0 && tasks_run_queue == 0 &&
2819 (stopping_thread_mask & all_threads_mask) == all_threads_mask)
Willy Tarreau85c459d2018-08-02 10:54:31 +02002820 break;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002821
Willy Tarreau7067b3a2019-06-02 11:11:29 +02002822 /* also stop if we failed to cleanly stop all tasks */
2823 if (killed > 1)
2824 break;
2825
Willy Tarreau10146c92015-04-13 20:44:19 +02002826 /* expire immediately if events are pending */
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002827 wake = 1;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002828 if (thread_has_tasks())
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002829 activity[tid].wake_tasks++;
William Lallemand1aab50b2018-06-07 09:46:01 +02002830 else if (signal_queue_len && tid == 0)
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002831 activity[tid].wake_signal++;
Olivier Houchard79321b92018-07-26 17:55:11 +02002832 else {
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002833 _HA_ATOMIC_OR(&sleeping_thread_mask, tid_bit);
2834 __ha_barrier_atomic_store();
Olivier Houchardbba1a262019-09-24 14:55:28 +02002835 if ((global_tasks_mask & tid_bit) || thread_has_tasks()) {
Olivier Houchard79321b92018-07-26 17:55:11 +02002836 activity[tid].wake_tasks++;
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002837 _HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
Olivier Houchard79321b92018-07-26 17:55:11 +02002838 } else
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002839 wake = 0;
Olivier Houchard79321b92018-07-26 17:55:11 +02002840 }
Willy Tarreau10146c92015-04-13 20:44:19 +02002841
Willy Tarreauc49ba522019-12-11 08:12:23 +01002842 /* If we have to sleep, measure how long */
2843 next = wake ? TICK_ETERNITY : next_timer_expiry();
2844
Willy Tarreau58b458d2008-06-29 22:40:23 +02002845 /* The poller will ensure it returns around <next> */
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002846 cur_poller.poll(&cur_poller, next, wake);
Emeric Brun64cc49c2017-10-03 14:46:45 +02002847
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002848 activity[tid].loops++;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002849 }
2850}
2851
Christopher Faulet1d17c102017-08-29 15:38:48 +02002852static void *run_thread_poll_loop(void *data)
2853{
Willy Tarreau082b6282019-05-22 14:42:12 +02002854 struct per_thread_alloc_fct *ptaf;
Christopher Faulet1d17c102017-08-29 15:38:48 +02002855 struct per_thread_init_fct *ptif;
2856 struct per_thread_deinit_fct *ptdf;
Willy Tarreau082b6282019-05-22 14:42:12 +02002857 struct per_thread_free_fct *ptff;
Willy Tarreau34a150c2019-06-11 09:16:41 +02002858 static int init_left = 0;
2859 __decl_hathreads(static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER);
2860 __decl_hathreads(static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002861
Willy Tarreaub4f7cc32019-05-03 09:27:30 +02002862 ha_set_tid((unsigned long)data);
Willy Tarreaud022e9c2019-09-24 08:25:15 +02002863 sched = &task_per_thread[tid];
Willy Tarreau91e6df02019-05-03 17:21:18 +02002864
Willy Tarreauf6178242019-05-21 19:46:58 +02002865#if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
Willy Tarreau91e6df02019-05-03 17:21:18 +02002866#ifdef USE_THREAD
Willy Tarreau8323a372019-05-20 18:57:53 +02002867 pthread_getcpuclockid(pthread_self(), &ti->clock_id);
Willy Tarreau624dcbf2019-05-20 20:23:06 +02002868#else
Willy Tarreau8323a372019-05-20 18:57:53 +02002869 ti->clock_id = CLOCK_THREAD_CPUTIME_ID;
Willy Tarreau91e6df02019-05-03 17:21:18 +02002870#endif
Willy Tarreau663fda42019-05-21 15:14:08 +02002871#endif
Willy Tarreau6ec902a2019-06-07 14:41:11 +02002872 /* Now, initialize one thread init at a time. This is better since
2873 * some init code is a bit tricky and may release global resources
2874 * after reallocating them locally. This will also ensure there is
2875 * no race on file descriptors allocation.
2876 */
Willy Tarreau34a150c2019-06-11 09:16:41 +02002877#ifdef USE_THREAD
2878 pthread_mutex_lock(&init_mutex);
2879#endif
2880 /* The first thread must set the number of threads left */
2881 if (!init_left)
2882 init_left = global.nbthread;
2883 init_left--;
Willy Tarreau91e6df02019-05-03 17:21:18 +02002884
Christopher Faulet1d17c102017-08-29 15:38:48 +02002885 tv_update_date(-1,-1);
2886
Willy Tarreau082b6282019-05-22 14:42:12 +02002887 /* per-thread alloc calls performed here are not allowed to snoop on
2888 * other threads, so they are free to initialize at their own rhythm
2889 * as long as they act as if they were alone. None of them may rely
2890 * on resources initialized by the other ones.
2891 */
2892 list_for_each_entry(ptaf, &per_thread_alloc_list, list) {
2893 if (!ptaf->fct()) {
2894 ha_alert("failed to allocate resources for thread %u.\n", tid);
2895 exit(1);
2896 }
2897 }
2898
Willy Tarreau3078e9f2019-05-20 10:50:43 +02002899 /* per-thread init calls performed here are not allowed to snoop on
2900 * other threads, so they are free to initialize at their own rhythm
2901 * as long as they act as if they were alone.
2902 */
Christopher Faulet1d17c102017-08-29 15:38:48 +02002903 list_for_each_entry(ptif, &per_thread_init_list, list) {
2904 if (!ptif->fct()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002905 ha_alert("failed to initialize thread %u.\n", tid);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002906 exit(1);
2907 }
2908 }
2909
Willy Tarreau71092822019-06-10 09:51:04 +02002910 /* enabling protocols will result in fd_insert() calls to be performed,
2911 * we want all threads to have already allocated their local fd tables
Willy Tarreau34a150c2019-06-11 09:16:41 +02002912 * before doing so, thus only the last thread does it.
Willy Tarreau71092822019-06-10 09:51:04 +02002913 */
Willy Tarreau34a150c2019-06-11 09:16:41 +02002914 if (init_left == 0)
Willy Tarreaue4d7c9d2019-06-10 10:14:52 +02002915 protocol_enable_all();
Willy Tarreau6ec902a2019-06-07 14:41:11 +02002916
Willy Tarreau34a150c2019-06-11 09:16:41 +02002917#ifdef USE_THREAD
2918 pthread_cond_broadcast(&init_cond);
2919 pthread_mutex_unlock(&init_mutex);
2920
2921 /* now wait for other threads to finish starting */
2922 pthread_mutex_lock(&init_mutex);
2923 while (init_left)
2924 pthread_cond_wait(&init_cond, &init_mutex);
2925 pthread_mutex_unlock(&init_mutex);
2926#endif
Willy Tarreau3078e9f2019-05-20 10:50:43 +02002927
Willy Tarreaua45a8b52019-12-06 16:31:45 +01002928#if defined(PR_SET_NO_NEW_PRIVS) && defined(USE_PRCTL)
2929 /* Let's refrain from using setuid executables. This way the impact of
2930 * an eventual vulnerability in a library remains limited. It may
2931 * impact external checks but who cares about them anyway ? In the
2932 * worst case it's possible to disable the option. Obviously we do this
2933 * in workers only. We can't hard-fail on this one as it really is
2934 * implementation dependent though we're interested in feedback, hence
2935 * the warning.
2936 */
2937 if (!(global.tune.options & GTUNE_INSECURE_SETUID) && !master) {
2938 static int warn_fail;
2939 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1 && !_HA_ATOMIC_XADD(&warn_fail, 1)) {
2940 ha_warning("Failed to disable setuid, please report to developers with detailed "
2941 "information about your operating system. You can silence this warning "
2942 "by adding 'insecure-setuid-wanted' in the 'global' section.\n");
2943 }
2944 }
2945#endif
2946
Willy Tarreaud96f1122019-12-03 07:07:36 +01002947#if defined(RLIMIT_NPROC)
2948 /* all threads have started, it's now time to prevent any new thread
2949 * or process from starting. Obviously we do this in workers only. We
2950 * can't hard-fail on this one as it really is implementation dependent
2951 * though we're interested in feedback, hence the warning.
2952 */
2953 if (!(global.tune.options & GTUNE_INSECURE_FORK) && !master) {
2954 struct rlimit limit = { .rlim_cur = 0, .rlim_max = 0 };
2955 static int warn_fail;
2956
2957 if (setrlimit(RLIMIT_NPROC, &limit) == -1 && !_HA_ATOMIC_XADD(&warn_fail, 1)) {
2958 ha_warning("Failed to disable forks, please report to developers with detailed "
2959 "information about your operating system. You can silence this warning "
2960 "by adding 'insecure-fork-wanted' in the 'global' section.\n");
2961 }
2962 }
2963#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02002964 run_poll_loop();
2965
2966 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
2967 ptdf->fct();
2968
Willy Tarreau082b6282019-05-22 14:42:12 +02002969 list_for_each_entry(ptff, &per_thread_free_list, list)
2970 ptff->fct();
2971
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002972#ifdef USE_THREAD
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002973 _HA_ATOMIC_AND(&all_threads_mask, ~tid_bit);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002974 if (tid > 0)
2975 pthread_exit(NULL);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002976#endif
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002977 return NULL;
2978}
Christopher Faulet1d17c102017-08-29 15:38:48 +02002979
William Dauchyf9af9d72019-11-17 15:47:16 +01002980/* set uid/gid depending on global settings */
2981static void set_identity(const char *program_name)
2982{
2983 if (global.gid) {
2984 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
2985 ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2986 " without 'uid'/'user' is generally useless.\n", program_name);
2987
2988 if (setgid(global.gid) == -1) {
2989 ha_alert("[%s.main()] Cannot set gid %d.\n", program_name, global.gid);
2990 protocol_unbind_all();
2991 exit(1);
2992 }
2993 }
2994
2995 if (global.uid && setuid(global.uid) == -1) {
2996 ha_alert("[%s.main()] Cannot set uid %d.\n", program_name, global.uid);
2997 protocol_unbind_all();
2998 exit(1);
2999 }
3000}
3001
Willy Tarreaubaaee002006-06-26 02:48:02 +02003002int main(int argc, char **argv)
3003{
3004 int err, retry;
3005 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02003006 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02003007 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003008
Olivier Houchard5fa300d2018-02-03 15:15:21 +01003009 setvbuf(stdout, NULL, _IONBF, 0);
Willy Tarreau5794fb02018-11-25 18:43:29 +01003010
Willy Tarreauff9c9142019-02-07 10:39:36 +01003011 /* this can only safely be done here, though it's optimized away by
3012 * the compiler.
3013 */
3014 if (MAX_PROCS < 1 || MAX_PROCS > LONGBITS) {
3015 ha_alert("MAX_PROCS value must be between 1 and %d inclusive; "
3016 "HAProxy was built with value %d, please fix it and rebuild.\n",
3017 LONGBITS, MAX_PROCS);
3018 exit(1);
3019 }
3020
Willy Tarreaubf696402019-03-01 10:09:28 +01003021 /* take a copy of initial limits before we possibly change them */
3022 getrlimit(RLIMIT_NOFILE, &limit);
3023 rlim_fd_cur_at_boot = limit.rlim_cur;
3024 rlim_fd_max_at_boot = limit.rlim_max;
3025
Willy Tarreau5794fb02018-11-25 18:43:29 +01003026 /* process all initcalls in order of potential dependency */
3027 RUN_INITCALLS(STG_PREPARE);
3028 RUN_INITCALLS(STG_LOCK);
3029 RUN_INITCALLS(STG_ALLOC);
3030 RUN_INITCALLS(STG_POOL);
3031 RUN_INITCALLS(STG_REGISTER);
3032 RUN_INITCALLS(STG_INIT);
3033
Emeric Bruncf20bf12010-10-22 16:06:11 +02003034 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02003035 signal_register_fct(SIGQUIT, dump, SIGQUIT);
3036 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
3037 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
William Lallemand73b85e72017-06-01 17:38:51 +02003038 signal_register_fct(SIGUSR2, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003039
Willy Tarreaue437c442010-03-17 18:02:46 +01003040 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
3041 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
3042 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003043 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02003044 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003045
Willy Tarreaudc23a922011-02-16 11:10:36 +01003046 /* ulimits */
3047 if (!global.rlimit_nofile)
3048 global.rlimit_nofile = global.maxsock;
3049
3050 if (global.rlimit_nofile) {
Willy Tarreaue5cfdac2019-03-01 10:32:05 +01003051 limit.rlim_cur = global.rlimit_nofile;
3052 limit.rlim_max = MAX(rlim_fd_max_at_boot, limit.rlim_cur);
3053
Willy Tarreaudc23a922011-02-16 11:10:36 +01003054 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
Willy Tarreauef635472016-06-21 11:48:18 +02003055 getrlimit(RLIMIT_NOFILE, &limit);
William Dauchy0fec3ab2019-10-27 20:08:11 +01003056 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3057 ha_alert("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n",
3058 argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
3059 if (!(global.mode & MODE_MWORKER))
3060 exit(1);
3061 }
3062 else {
3063 /* try to set it to the max possible at least */
3064 limit.rlim_cur = limit.rlim_max;
3065 if (setrlimit(RLIMIT_NOFILE, &limit) != -1)
3066 getrlimit(RLIMIT_NOFILE, &limit);
Willy Tarreau164dd0b2016-06-21 11:51:59 +02003067
William Dauchy0fec3ab2019-10-27 20:08:11 +01003068 ha_warning("[%s.main()] Cannot raise FD limit to %d, limit is %d. "
3069 "This will fail in >= v2.3\n",
3070 argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
3071 global.rlimit_nofile = limit.rlim_cur;
3072 }
Willy Tarreaudc23a922011-02-16 11:10:36 +01003073 }
3074 }
3075
3076 if (global.rlimit_memmax) {
3077 limit.rlim_cur = limit.rlim_max =
Willy Tarreau70060452015-12-14 12:46:07 +01003078 global.rlimit_memmax * 1048576ULL;
Willy Tarreaudc23a922011-02-16 11:10:36 +01003079#ifdef RLIMIT_AS
3080 if (setrlimit(RLIMIT_AS, &limit) == -1) {
William Dauchy0fec3ab2019-10-27 20:08:11 +01003081 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3082 ha_alert("[%s.main()] Cannot fix MEM limit to %d megs.\n",
3083 argv[0], global.rlimit_memmax);
3084 if (!(global.mode & MODE_MWORKER))
3085 exit(1);
3086 }
3087 else
3088 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs."
3089 "This will fail in >= v2.3\n",
3090 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01003091 }
3092#else
3093 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
William Dauchy0fec3ab2019-10-27 20:08:11 +01003094 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3095 ha_alert("[%s.main()] Cannot fix MEM limit to %d megs.\n",
3096 argv[0], global.rlimit_memmax);
3097 if (!(global.mode & MODE_MWORKER))
3098 exit(1);
3099 }
3100 else
3101 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.",
3102 "This will fail in >= v2.3\n",
3103 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01003104 }
3105#endif
3106 }
3107
Olivier Houchardf73629d2017-04-05 22:33:04 +02003108 if (old_unixsocket) {
William Lallemand85b0bd92017-06-01 17:38:53 +02003109 if (strcmp("/dev/null", old_unixsocket) != 0) {
3110 if (get_old_sockets(old_unixsocket) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003111 ha_alert("Failed to get the sockets from the old process!\n");
William Lallemand85b0bd92017-06-01 17:38:53 +02003112 if (!(global.mode & MODE_MWORKER))
3113 exit(1);
3114 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02003115 }
3116 }
William Lallemand85b0bd92017-06-01 17:38:53 +02003117 get_cur_unixsocket();
3118
Willy Tarreaubaaee002006-06-26 02:48:02 +02003119 /* We will loop at most 100 times with 10 ms delay each time.
3120 * That's at most 1 second. We only send a signal to old pids
3121 * if we cannot grab at least one port.
3122 */
3123 retry = MAX_START_RETRIES;
3124 err = ERR_NONE;
3125 while (retry >= 0) {
3126 struct timeval w;
3127 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01003128 /* exit the loop on no error or fatal error */
3129 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003130 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02003131 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003132 break;
3133
3134 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
3135 * listening sockets. So on those platforms, it would be wiser to
3136 * simply send SIGUSR1, which will not be undoable.
3137 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02003138 if (tell_old_pids(SIGTTOU) == 0) {
3139 /* no need to wait if we can't contact old pids */
3140 retry = 0;
3141 continue;
3142 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003143 /* give some time to old processes to stop listening */
3144 w.tv_sec = 0;
3145 w.tv_usec = 10*1000;
3146 select(0, NULL, NULL, NULL, &w);
3147 retry--;
3148 }
3149
3150 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01003151 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02003152 if (retry != MAX_START_RETRIES && nb_oldpids) {
3153 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003154 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02003155 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003156 exit(1);
3157 }
3158
William Lallemand944e6192018-11-21 15:48:31 +01003159 if (!(global.mode & MODE_MWORKER_WAIT) && listeners == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003160 ha_alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003161 /* Note: we don't have to send anything to the old pids because we
3162 * never stopped them. */
3163 exit(1);
3164 }
3165
Emeric Bruncf20bf12010-10-22 16:06:11 +02003166 err = protocol_bind_all(errmsg, sizeof(errmsg));
3167 if ((err & ~ERR_WARN) != ERR_NONE) {
3168 if ((err & ERR_ALERT) || (err & ERR_WARN))
Christopher Faulet767a84b2017-11-24 16:50:31 +01003169 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Emeric Bruncf20bf12010-10-22 16:06:11 +02003170
Christopher Faulet767a84b2017-11-24 16:50:31 +01003171 ha_alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02003172 protocol_unbind_all(); /* cleanup everything we can */
3173 if (nb_oldpids)
3174 tell_old_pids(SIGTTIN);
3175 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02003176 } else if (err & ERR_WARN) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003177 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02003178 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02003179 /* Ok, all listener should now be bound, close any leftover sockets
3180 * the previous process gave us, we don't need them anymore
3181 */
3182 while (xfer_sock_list != NULL) {
3183 struct xfer_sock_list *tmpxfer = xfer_sock_list->next;
3184 close(xfer_sock_list->fd);
3185 free(xfer_sock_list->iface);
3186 free(xfer_sock_list->namespace);
3187 free(xfer_sock_list);
3188 xfer_sock_list = tmpxfer;
3189 }
Willy Tarreaudd815982007-10-16 12:25:14 +02003190
Willy Tarreaubaaee002006-06-26 02:48:02 +02003191 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02003192 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
3193 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003194
Willy Tarreaubaaee002006-06-26 02:48:02 +02003195 /* MODE_QUIET can inhibit alerts and warnings below this line */
3196
PiBa-NL149a81a2017-12-25 21:03:31 +01003197 if (getenv("HAPROXY_MWORKER_REEXEC") != NULL) {
3198 /* either stdin/out/err are already closed or should stay as they are. */
3199 if ((global.mode & MODE_DAEMON)) {
3200 /* daemon mode re-executing, stdin/stdout/stderr are already closed so keep quiet */
3201 global.mode &= ~MODE_VERBOSE;
3202 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
3203 }
3204 } else {
3205 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
3206 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01003207 stdio_quiet(-1);
PiBa-NL149a81a2017-12-25 21:03:31 +01003208 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003209 }
3210
3211 /* open log & pid files before the chroot */
William Lallemand80293002017-11-06 11:00:03 +01003212 if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003213 unlink(global.pidfile);
3214 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
3215 if (pidfd < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003216 ha_alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003217 if (nb_oldpids)
3218 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02003219 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02003220 exit(1);
3221 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003222 }
3223
Willy Tarreaub38651a2007-03-24 17:24:39 +01003224 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003225 ha_alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
3226 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02003227 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01003228 exit(1);
3229 }
3230
Willy Tarreau4e30ed72009-02-04 18:02:48 +01003231 /* If the user is not root, we'll still let him try the configuration
3232 * but we inform him that unexpected behaviour may occur.
3233 */
3234 if ((global.last_checks & LSTCHK_NETADM) && getuid())
Christopher Faulet767a84b2017-11-24 16:50:31 +01003235 ha_warning("[%s.main()] Some options which require full privileges"
3236 " might not work well.\n"
3237 "", argv[0]);
Willy Tarreau4e30ed72009-02-04 18:02:48 +01003238
William Lallemand095ba4c2017-06-01 17:38:50 +02003239 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
3240
3241 /* chroot if needed */
3242 if (global.chroot != NULL) {
3243 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003244 ha_alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02003245 if (nb_oldpids)
3246 tell_old_pids(SIGTTIN);
3247 protocol_unbind_all();
3248 exit(1);
3249 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02003250 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02003251 }
3252
William Lallemand944e6192018-11-21 15:48:31 +01003253 if (nb_oldpids && !(global.mode & MODE_MWORKER_WAIT))
Willy Tarreaubb545b42010-08-25 12:58:59 +02003254 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003255
William Lallemand27edc4b2019-05-07 17:49:33 +02003256 /* send a SIGTERM to workers who have a too high reloads number */
3257 if ((global.mode & MODE_MWORKER) && !(global.mode & MODE_MWORKER_WAIT))
3258 mworker_kill_max_reloads(SIGTERM);
3259
William Lallemand8a361b52017-06-20 11:20:33 +02003260 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) {
3261 nb_oldpids = 0;
3262 free(oldpids);
3263 oldpids = NULL;
3264 }
3265
3266
Willy Tarreaubaaee002006-06-26 02:48:02 +02003267 /* Note that any error at this stage will be fatal because we will not
3268 * be able to restart the old pids.
3269 */
3270
William Dauchyf9af9d72019-11-17 15:47:16 +01003271 if ((global.mode & (MODE_MWORKER | MODE_DAEMON)) == 0)
3272 set_identity(argv[0]);
Willy Tarreau636848a2019-04-15 19:38:50 +02003273
Willy Tarreaubaaee002006-06-26 02:48:02 +02003274 /* check ulimits */
3275 limit.rlim_cur = limit.rlim_max = 0;
3276 getrlimit(RLIMIT_NOFILE, &limit);
3277 if (limit.rlim_cur < global.maxsock) {
William Dauchy0fec3ab2019-10-27 20:08:11 +01003278 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3279 ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. "
3280 "Please raise 'ulimit-n' to %d or more to avoid any trouble.\n",
3281 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock,
3282 global.maxsock);
3283 if (!(global.mode & MODE_MWORKER))
3284 exit(1);
3285 }
3286 else
3287 ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. "
3288 "Please raise 'ulimit-n' to %d or more to avoid any trouble."
3289 "This will fail in >= v2.3\n",
3290 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock,
3291 global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003292 }
3293
William Lallemand944e6192018-11-21 15:48:31 +01003294 if (global.mode & (MODE_DAEMON | MODE_MWORKER | MODE_MWORKER_WAIT)) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003295 struct proxy *px;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003296 struct peers *curpeers;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003297 int ret = 0;
3298 int proc;
William Lallemande1340412017-12-28 16:09:36 +01003299 int devnullfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003300
William Lallemand095ba4c2017-06-01 17:38:50 +02003301 /*
3302 * if daemon + mworker: must fork here to let a master
3303 * process live in background before forking children
3304 */
William Lallemand73b85e72017-06-01 17:38:51 +02003305
3306 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
3307 && (global.mode & MODE_MWORKER)
3308 && (global.mode & MODE_DAEMON)) {
William Lallemand095ba4c2017-06-01 17:38:50 +02003309 ret = fork();
3310 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003311 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02003312 protocol_unbind_all();
3313 exit(1); /* there has been an error */
William Lallemandbfd8eb52018-07-04 15:31:23 +02003314 } else if (ret > 0) { /* parent leave to daemonize */
William Lallemand095ba4c2017-06-01 17:38:50 +02003315 exit(0);
William Lallemandbfd8eb52018-07-04 15:31:23 +02003316 } else /* change the process group ID in the child (master process) */
3317 setsid();
William Lallemand095ba4c2017-06-01 17:38:50 +02003318 }
William Lallemande20b6a62017-06-01 17:38:55 +02003319
William Lallemande20b6a62017-06-01 17:38:55 +02003320
William Lallemanddeed7802017-11-06 11:00:04 +01003321 /* if in master-worker mode, write the PID of the father */
3322 if (global.mode & MODE_MWORKER) {
3323 char pidstr[100];
Willy Tarreau76a80c72019-06-22 07:41:38 +02003324 snprintf(pidstr, sizeof(pidstr), "%d\n", (int)getpid());
Willy Tarreau46ec48b2018-01-23 19:20:19 +01003325 if (pidfd >= 0)
3326 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
William Lallemanddeed7802017-11-06 11:00:04 +01003327 }
3328
Willy Tarreaubaaee002006-06-26 02:48:02 +02003329 /* the father launches the required number of processes */
William Lallemand944e6192018-11-21 15:48:31 +01003330 if (!(global.mode & MODE_MWORKER_WAIT)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +02003331 if (global.mode & MODE_MWORKER)
3332 mworker_ext_launch_all();
William Lallemand944e6192018-11-21 15:48:31 +01003333 for (proc = 0; proc < global.nbproc; proc++) {
3334 ret = fork();
3335 if (ret < 0) {
3336 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
3337 protocol_unbind_all();
3338 exit(1); /* there has been an error */
3339 }
Willy Tarreau52bf8392020-03-08 00:42:37 +01003340 else if (ret == 0) { /* child breaks here */
3341 ha_random_jump96(relative_pid);
William Lallemand944e6192018-11-21 15:48:31 +01003342 break;
Willy Tarreau52bf8392020-03-08 00:42:37 +01003343 }
William Lallemand944e6192018-11-21 15:48:31 +01003344 if (pidfd >= 0 && !(global.mode & MODE_MWORKER)) {
3345 char pidstr[100];
3346 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
3347 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
3348 }
3349 if (global.mode & MODE_MWORKER) {
3350 struct mworker_proc *child;
William Lallemandce83b4a2018-10-26 14:47:30 +02003351
William Lallemand220567e2018-11-21 18:04:53 +01003352 ha_notice("New worker #%d (%d) forked\n", relative_pid, ret);
William Lallemand944e6192018-11-21 15:48:31 +01003353 /* find the right mworker_proc */
3354 list_for_each_entry(child, &proc_list, list) {
3355 if (child->relative_pid == relative_pid &&
William Lallemand8f7069a2019-04-12 16:09:23 +02003356 child->reloads == 0 && child->options & PROC_O_TYPE_WORKER) {
William Lallemand944e6192018-11-21 15:48:31 +01003357 child->timestamp = now.tv_sec;
3358 child->pid = ret;
William Lallemand1dc69632019-06-12 19:11:33 +02003359 child->version = strdup(haproxy_version);
William Lallemand944e6192018-11-21 15:48:31 +01003360 break;
3361 }
William Lallemandce83b4a2018-10-26 14:47:30 +02003362 }
3363 }
William Lallemandbc193052018-09-11 10:06:26 +02003364
William Lallemand944e6192018-11-21 15:48:31 +01003365 relative_pid++; /* each child will get a different one */
3366 pid_bit <<= 1;
3367 }
3368 } else {
3369 /* wait mode */
3370 global.nbproc = 1;
3371 proc = 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003372 }
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003373
3374#ifdef USE_CPU_AFFINITY
3375 if (proc < global.nbproc && /* child */
Willy Tarreauff9c9142019-02-07 10:39:36 +01003376 proc < MAX_PROCS && /* only the first 32/64 processes may be pinned */
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003377 global.cpu_map.proc[proc]) /* only do this if the process has a CPU map */
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003378#ifdef __FreeBSD__
Olivier Houchard97148f62017-08-16 17:29:11 +02003379 {
3380 cpuset_t cpuset;
3381 int i;
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003382 unsigned long cpu_map = global.cpu_map.proc[proc];
Olivier Houchard97148f62017-08-16 17:29:11 +02003383
3384 CPU_ZERO(&cpuset);
3385 while ((i = ffsl(cpu_map)) > 0) {
3386 CPU_SET(i - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003387 cpu_map &= ~(1UL << (i - 1));
Olivier Houchard97148f62017-08-16 17:29:11 +02003388 }
3389 ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset);
3390 }
David Carlier5e4c8e22019-09-13 05:12:58 +01003391#elif defined(__linux__)
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003392 sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map.proc[proc]);
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003393#endif
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003394#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003395 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02003396 if (pidfd >= 0) {
3397 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
3398 close(pidfd);
3399 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02003400
3401 /* We won't ever use this anymore */
Willy Tarreaud137dd32010-08-25 12:49:05 +02003402 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003403
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003404 if (proc == global.nbproc) {
William Lallemand944e6192018-11-21 15:48:31 +01003405 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003406
3407 if ((!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
3408 (global.mode & MODE_DAEMON)) {
3409 /* detach from the tty, this is required to properly daemonize. */
William Lallemande1340412017-12-28 16:09:36 +01003410 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL))
3411 stdio_quiet(-1);
3412
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003413 global.mode &= ~MODE_VERBOSE;
3414 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003415 }
3416
William Lallemandb3f2be32018-09-11 10:06:18 +02003417 mworker_loop();
William Lallemand1499b9b2017-06-07 15:04:47 +02003418 /* should never get there */
3419 exit(EXIT_FAILURE);
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003420 }
William Lallemandcf4e4962017-06-08 19:05:48 +02003421#if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH)
Grant Zhang872f9c22017-01-21 01:10:18 +00003422 ssl_free_dh();
3423#endif
William Lallemand1499b9b2017-06-07 15:04:47 +02003424 exit(0); /* parent must leave */
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003425 }
3426
William Lallemandcb11fd22017-06-01 17:38:52 +02003427 /* child must never use the atexit function */
3428 atexit_flag = 0;
3429
William Lallemandbc193052018-09-11 10:06:26 +02003430 /* close useless master sockets */
3431 if (global.mode & MODE_MWORKER) {
3432 struct mworker_proc *child, *it;
3433 master = 0;
3434
William Lallemand309dc9a2018-10-26 14:47:45 +02003435 mworker_cli_proxy_stop();
3436
William Lallemandbc193052018-09-11 10:06:26 +02003437 /* free proc struct of other processes */
3438 list_for_each_entry_safe(child, it, &proc_list, list) {
William Lallemandce83b4a2018-10-26 14:47:30 +02003439 /* close the FD of the master side for all
3440 * workers, we don't need to close the worker
3441 * side of other workers since it's done with
3442 * the bind_proc */
Tim Duesterhus742e0f92018-11-25 20:03:39 +01003443 if (child->ipc_fd[0] >= 0)
3444 close(child->ipc_fd[0]);
William Lallemandce83b4a2018-10-26 14:47:30 +02003445 if (child->relative_pid == relative_pid &&
3446 child->reloads == 0) {
3447 /* keep this struct if this is our pid */
3448 proc_self = child;
William Lallemandbc193052018-09-11 10:06:26 +02003449 continue;
William Lallemandce83b4a2018-10-26 14:47:30 +02003450 }
William Lallemandbc193052018-09-11 10:06:26 +02003451 LIST_DEL(&child->list);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +02003452 mworker_free_child(child);
3453 child = NULL;
William Lallemandbc193052018-09-11 10:06:26 +02003454 }
3455 }
Willy Tarreau1605c7a2018-01-23 19:01:49 +01003456
William Lallemande1340412017-12-28 16:09:36 +01003457 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
3458 devnullfd = open("/dev/null", O_RDWR, 0);
3459 if (devnullfd < 0) {
3460 ha_alert("Cannot open /dev/null\n");
3461 exit(EXIT_FAILURE);
3462 }
3463 }
3464
William Lallemand095ba4c2017-06-01 17:38:50 +02003465 /* Must chroot and setgid/setuid in the children */
3466 /* chroot if needed */
3467 if (global.chroot != NULL) {
3468 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003469 ha_alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02003470 if (nb_oldpids)
3471 tell_old_pids(SIGTTIN);
3472 protocol_unbind_all();
3473 exit(1);
3474 }
3475 }
3476
3477 free(global.chroot);
3478 global.chroot = NULL;
William Dauchyf9af9d72019-11-17 15:47:16 +01003479 set_identity(argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02003480
William Lallemand7f80eb22017-05-26 18:19:55 +02003481 /* pass through every cli socket, and check if it's bound to
3482 * the current process and if it exposes listeners sockets.
3483 * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork.
3484 * */
3485
3486 if (global.stats_fe) {
3487 struct bind_conf *bind_conf;
3488
3489 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
3490 if (bind_conf->level & ACCESS_FD_LISTENERS) {
3491 if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) {
3492 global.tune.options |= GTUNE_SOCKET_TRANSFER;
3493 break;
3494 }
3495 }
3496 }
3497 }
3498
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003499 /* we might have to unbind some proxies from some processes */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003500 px = proxies_list;
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003501 while (px != NULL) {
3502 if (px->bind_proc && px->state != PR_STSTOPPED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02003503 if (!(px->bind_proc & (1UL << proc))) {
3504 if (global.tune.options & GTUNE_SOCKET_TRANSFER)
3505 zombify_proxy(px);
3506 else
3507 stop_proxy(px);
3508 }
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003509 }
3510 px = px->next;
3511 }
3512
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003513 /* we might have to unbind some peers sections from some processes */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003514 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003515 if (!curpeers->peers_fe)
3516 continue;
3517
3518 if (curpeers->peers_fe->bind_proc & (1UL << proc))
3519 continue;
3520
3521 stop_proxy(curpeers->peers_fe);
3522 /* disable this peer section so that it kills itself */
Willy Tarreau47c8c022015-09-28 16:39:25 +02003523 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02003524 task_destroy(curpeers->sync_task);
Willy Tarreau47c8c022015-09-28 16:39:25 +02003525 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +02003526 task_destroy(curpeers->peers_fe->task);
Willy Tarreau47c8c022015-09-28 16:39:25 +02003527 curpeers->peers_fe->task = NULL;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003528 curpeers->peers_fe = NULL;
3529 }
3530
William Lallemand2e8fad92018-11-13 16:18:23 +01003531 /*
3532 * This is only done in daemon mode because we might want the
3533 * logs on stdout in mworker mode. If we're NOT in QUIET mode,
3534 * we should now close the 3 first FDs to ensure that we can
3535 * detach from the TTY. We MUST NOT do it in other cases since
3536 * it would have already be done, and 0-2 would have been
3537 * affected to listening sockets
Willy Tarreaubaaee002006-06-26 02:48:02 +02003538 */
William Lallemand2e8fad92018-11-13 16:18:23 +01003539 if ((global.mode & MODE_DAEMON) &&
3540 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003541 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01003542 stdio_quiet(devnullfd);
Willy Tarreau106cb762008-11-16 07:40:34 +01003543 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003544 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
3545 }
3546 pid = getpid(); /* update child's pid */
William Lallemandbfd8eb52018-07-04 15:31:23 +02003547 if (!(global.mode & MODE_MWORKER)) /* in mworker mode we don't want a new pgid for the children */
3548 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02003549 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02003550 }
3551
William Dauchye039f262019-11-17 15:47:15 +01003552 /* try our best to re-enable core dumps depending on system capabilities.
3553 * What is addressed here :
3554 * - remove file size limits
3555 * - remove core size limits
3556 * - mark the process dumpable again if it lost it due to user/group
3557 */
3558 if (global.tune.options & GTUNE_SET_DUMPABLE) {
3559 limit.rlim_cur = limit.rlim_max = RLIM_INFINITY;
3560
3561#if defined(RLIMIT_FSIZE)
3562 if (setrlimit(RLIMIT_FSIZE, &limit) == -1) {
3563 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3564 ha_alert("[%s.main()] Failed to set the raise the maximum "
3565 "file size.\n", argv[0]);
3566 if (!(global.mode & MODE_MWORKER))
3567 exit(1);
3568 }
3569 else
3570 ha_warning("[%s.main()] Failed to set the raise the maximum "
3571 "file size. This will fail in >= v2.3\n", argv[0]);
3572 }
3573#endif
3574
3575#if defined(RLIMIT_CORE)
3576 if (setrlimit(RLIMIT_CORE, &limit) == -1) {
3577 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3578 ha_alert("[%s.main()] Failed to set the raise the core "
3579 "dump size.\n", argv[0]);
3580 if (!(global.mode & MODE_MWORKER))
3581 exit(1);
3582 }
3583 else
3584 ha_warning("[%s.main()] Failed to set the raise the core "
3585 "dump size. This will fail in >= v2.3\n", argv[0]);
3586 }
3587#endif
3588
3589#if defined(USE_PRCTL)
3590 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1)
3591 ha_warning("[%s.main()] Failed to set the dumpable flag, "
3592 "no core will be dumped.\n", argv[0]);
3593#endif
3594 }
3595
Christopher Faulete3a5e352017-10-24 13:53:54 +02003596 global.mode &= ~MODE_STARTING;
Willy Tarreau4f60f162007-04-08 16:39:58 +02003597 /*
3598 * That's it : the central polling loop. Run until we stop.
3599 */
Christopher Faulet1d17c102017-08-29 15:38:48 +02003600#ifdef USE_THREAD
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003601 {
William Lallemand1aab50b2018-06-07 09:46:01 +02003602 sigset_t blocked_sig, old_sig;
Willy Tarreauc40efc12019-05-03 09:22:44 +02003603 int i;
3604
William Lallemand1aab50b2018-06-07 09:46:01 +02003605 /* ensure the signals will be blocked in every thread */
3606 sigfillset(&blocked_sig);
3607 sigdelset(&blocked_sig, SIGPROF);
3608 sigdelset(&blocked_sig, SIGBUS);
3609 sigdelset(&blocked_sig, SIGFPE);
3610 sigdelset(&blocked_sig, SIGILL);
3611 sigdelset(&blocked_sig, SIGSEGV);
3612 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
3613
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003614 /* Create nbthread-1 thread. The first thread is the current process */
David Carliera92c5ce2019-09-13 05:03:12 +01003615 ha_thread_info[0].pthread = pthread_self();
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003616 for (i = 1; i < global.nbthread; i++)
David Carliera92c5ce2019-09-13 05:03:12 +01003617 pthread_create(&ha_thread_info[i].pthread, NULL, &run_thread_poll_loop, (void *)(long)i);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003618
Christopher Faulet62519022017-10-16 15:49:32 +02003619#ifdef USE_CPU_AFFINITY
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003620 /* Now the CPU affinity for all threads */
Willy Tarreau7764a572019-07-16 15:10:34 +02003621 if (global.cpu_map.proc_t1[relative_pid-1])
3622 global.cpu_map.thread[0] &= global.cpu_map.proc_t1[relative_pid-1];
3623
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003624 for (i = 0; i < global.nbthread; i++) {
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003625 if (global.cpu_map.proc[relative_pid-1])
Willy Tarreau81492c92019-05-03 09:41:23 +02003626 global.cpu_map.thread[i] &= global.cpu_map.proc[relative_pid-1];
Christopher Faulet62519022017-10-16 15:49:32 +02003627
Willy Tarreau421f02e2018-01-20 18:19:22 +01003628 if (i < MAX_THREADS && /* only the first 32/64 threads may be pinned */
Willy Tarreau81492c92019-05-03 09:41:23 +02003629 global.cpu_map.thread[i]) {/* only do this if the thread has a THREAD map */
David Carlier5e4c8e22019-09-13 05:12:58 +01003630#if defined(__APPLE__)
3631 int j;
3632 unsigned long cpu_map = global.cpu_map.thread[i];
3633
3634 while ((j = ffsl(cpu_map)) > 0) {
3635 thread_affinity_policy_data_t cpu_set = { j - 1 };
3636 thread_port_t mthread = pthread_mach_thread_np(ha_thread_info[i].pthread);
3637 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
3638 cpu_map &= ~(1UL << (j - 1));
3639 }
3640#else
Olivier Houchard829aa242017-12-01 18:19:43 +01003641#if defined(__FreeBSD__) || defined(__NetBSD__)
3642 cpuset_t cpuset;
3643#else
3644 cpu_set_t cpuset;
3645#endif
3646 int j;
Willy Tarreau81492c92019-05-03 09:41:23 +02003647 unsigned long cpu_map = global.cpu_map.thread[i];
Olivier Houchard829aa242017-12-01 18:19:43 +01003648
3649 CPU_ZERO(&cpuset);
3650
3651 while ((j = ffsl(cpu_map)) > 0) {
3652 CPU_SET(j - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003653 cpu_map &= ~(1UL << (j - 1));
Olivier Houchard829aa242017-12-01 18:19:43 +01003654 }
David Carliera92c5ce2019-09-13 05:03:12 +01003655 pthread_setaffinity_np(ha_thread_info[i].pthread,
Olivier Houchard829aa242017-12-01 18:19:43 +01003656 sizeof(cpuset), &cpuset);
David Carlier5e4c8e22019-09-13 05:12:58 +01003657#endif
Olivier Houchard829aa242017-12-01 18:19:43 +01003658 }
Christopher Faulet1d17c102017-08-29 15:38:48 +02003659 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003660#endif /* !USE_CPU_AFFINITY */
3661
William Lallemand1aab50b2018-06-07 09:46:01 +02003662 /* when multithreading we need to let only the thread 0 handle the signals */
William Lallemandd3801c12018-09-11 10:06:23 +02003663 haproxy_unblock_signals();
William Lallemand1aab50b2018-06-07 09:46:01 +02003664
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003665 /* Finally, start the poll loop for the first thread */
Willy Tarreaub4f7cc32019-05-03 09:27:30 +02003666 run_thread_poll_loop(0);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003667
3668 /* Wait the end of other threads */
3669 for (i = 1; i < global.nbthread; i++)
David Carliera92c5ce2019-09-13 05:03:12 +01003670 pthread_join(ha_thread_info[i].pthread, NULL);
Christopher Faulet1d17c102017-08-29 15:38:48 +02003671
Christopher Fauletb79a94c2017-05-30 15:34:30 +02003672#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
3673 show_lock_stats();
3674#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003675 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003676#else /* ! USE_THREAD */
William Lallemandd3801c12018-09-11 10:06:23 +02003677 haproxy_unblock_signals();
Willy Tarreaub4f7cc32019-05-03 09:27:30 +02003678 run_thread_poll_loop(0);
Christopher Faulet62519022017-10-16 15:49:32 +02003679#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003680
3681 /* Do some cleanup */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003682 deinit();
Christopher Faulet1d17c102017-08-29 15:38:48 +02003683
Willy Tarreaubaaee002006-06-26 02:48:02 +02003684 exit(0);
3685}
3686
3687
3688/*
3689 * Local variables:
3690 * c-indent-level: 8
3691 * c-basic-offset: 8
3692 * End:
3693 */