blob: 3744547d25f1ae950bcd938a0a0b254590e199c3 [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 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +050010 * Please refer to RFC7230 - RFC7235 information about HTTP protocol, and
11 * RFC6265 for information 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>
Tim Duesterhusdfad6a42020-04-18 16:02:47 +020049#include <sys/utsname.h>
Marc-Antoine Perennou992709b2013-02-12 10:53:52 +010050#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <time.h>
52#include <syslog.h>
Michael Schererab012dd2013-01-12 18:35:19 +010053#include <grp.h>
Willy Tarreaufc6c0322012-11-16 16:12:27 +010054#ifdef USE_CPU_AFFINITY
Willy Tarreaufc6c0322012-11-16 16:12:27 +010055#include <sched.h>
David Carlier42d9e5a2018-11-12 16:22:19 +000056#if defined(__FreeBSD__) || defined(__DragonFly__)
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +020057#include <sys/param.h>
David Carlier42d9e5a2018-11-12 16:22:19 +000058#ifdef __FreeBSD__
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +020059#include <sys/cpuset.h>
David Carlier42d9e5a2018-11-12 16:22:19 +000060#endif
David Carlier6d5c8412017-11-29 11:02:32 +000061#include <pthread_np.h>
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +020062#endif
David Carlier5e4c8e22019-09-13 05:12:58 +010063#ifdef __APPLE__
64#include <mach/mach_types.h>
65#include <mach/thread_act.h>
66#include <mach/thread_policy.h>
67#endif
Willy Tarreaufc6c0322012-11-16 16:12:27 +010068#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Willy Tarreau636848a2019-04-15 19:38:50 +020070#if defined(USE_PRCTL)
71#include <sys/prctl.h>
72#endif
73
Willy Tarreaubaaee002006-06-26 02:48:02 +020074#ifdef DEBUG_FULL
75#include <assert.h>
76#endif
Tim Duesterhusd6942c82017-11-20 15:58:35 +010077#if defined(USE_SYSTEMD)
78#include <systemd/sd-daemon.h>
79#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020080
Willy Tarreau6c3a6812020-03-06 18:57:15 +010081#include <import/sha1.h>
82
Willy Tarreaub2551052020-06-09 09:07:15 +020083#include <haproxy/acl.h>
84#include <haproxy/activity.h>
85#include <haproxy/api.h>
86#include <haproxy/arg.h>
87#include <haproxy/auth.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020088#include <haproxy/base64.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020089#include <haproxy/capture-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020090#include <haproxy/cfgparse.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020091#include <haproxy/chunk.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020092#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020093#include <haproxy/connection.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020094#include <haproxy/dns.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020095#include <haproxy/dynbuf.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020096#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020097#include <haproxy/fd.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020098#include <haproxy/filters.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020099#include <haproxy/global.h>
Willy Tarreau86416052020-06-04 09:20:54 +0200100#include <haproxy/hlua.h>
Willy Tarreauc761f842020-06-04 11:40:28 +0200101#include <haproxy/http_rules.h>
Willy Tarreau853b2972020-05-27 18:01:47 +0200102#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +0200103#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +0200104#include <haproxy/log.h>
Willy Tarreaub5abe5b2020-06-04 14:07:37 +0200105#include <haproxy/mworker.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +0200106#include <haproxy/namespace.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +0200107#include <haproxy/net_helper.h>
Willy Tarreau6019fab2020-05-27 16:26:00 +0200108#include <haproxy/openssl-compat.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +0200109#include <haproxy/pattern.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +0200110#include <haproxy/peers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +0200111#include <haproxy/pool.h>
112#include <haproxy/protocol.h>
Willy Tarreaubf3b06b2020-08-26 10:23:40 +0200113#include <haproxy/proto_tcp.h>
Willy Tarreaua264d962020-06-04 22:29:18 +0200114#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +0200115#include <haproxy/regex.h>
Willy Tarreaub2551052020-06-09 09:07:15 +0200116#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +0200117#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +0200118#include <haproxy/session.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +0200119#include <haproxy/signal.h>
Willy Tarreau25140cc2020-08-28 15:40:33 +0200120#include <haproxy/sock_inet.h>
Willy Tarreau209108d2020-06-04 20:30:20 +0200121#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +0200122#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +0200123#include <haproxy/task.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +0200124#include <haproxy/thread.h>
Willy Tarreaub2551052020-06-09 09:07:15 +0200125#include <haproxy/time.h>
126#include <haproxy/tools.h>
127#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +0200128#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +0200129#include <haproxy/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200130
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131
Willy Tarreau7b5654f2019-03-29 21:30:17 +0100132/* array of init calls for older platforms */
133DECLARE_INIT_STAGES;
134
Willy Tarreau477ecd82010-01-03 21:12:30 +0100135/* list of config files */
136static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200137int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100138int relative_pid = 1; /* process id starting at 1 */
Willy Tarreau387bd4f2017-11-10 19:08:14 +0100139unsigned long pid_bit = 1; /* bit corresponding to the process id */
Willy Tarreaua38a7172019-02-02 17:11:28 +0100140unsigned long all_proc_mask = 1; /* mask of all processes */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200141
Willy Tarreauf8ea00e2020-03-12 17:24:53 +0100142volatile unsigned long sleeping_thread_mask = 0; /* Threads that are about to sleep in poll() */
Willy Tarreau4b3f27b2020-03-12 17:28:01 +0100143volatile unsigned long stopping_thread_mask = 0; /* Threads acknowledged stopping */
Willy Tarreauf8ea00e2020-03-12 17:24:53 +0100144
Willy Tarreaubaaee002006-06-26 02:48:02 +0200145/* global options */
146struct global global = {
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100147 .hard_stop_after = TICK_ETERNITY,
Willy Tarreau247a13a2012-11-15 17:38:15 +0100148 .nbproc = 1,
Willy Tarreau149ab772019-01-26 14:27:06 +0100149 .nbthread = 0,
William Lallemand5f232402012-04-05 18:02:55 +0200150 .req_count = 0,
William Lallemand0f99e342011-10-12 17:50:54 +0200151 .logsrvs = LIST_HEAD_INIT(global.logsrvs),
William Lallemand9d5f5482012-11-07 16:12:57 +0100152 .maxzlibmem = 0,
William Lallemandd85f9172012-11-09 17:05:39 +0100153 .comp_rate_lim = 0,
Emeric Brun850efd52014-01-29 12:24:34 +0100154 .ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED,
Emeric Bruned760922010-10-22 17:59:25 +0200155 .unix_bind = {
156 .ux = {
157 .uid = -1,
158 .gid = -1,
159 .mode = 0,
160 }
161 },
Willy Tarreau27a674e2009-08-17 07:23:33 +0200162 .tune = {
Willy Tarreau7ac908b2019-02-27 12:02:18 +0100163 .options = GTUNE_LISTENER_MQ,
Willy Tarreauc77d3642018-12-12 06:19:42 +0100164 .bufsize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)),
Christopher Faulet546c4692020-01-22 14:31:21 +0100165 .maxrewrite = MAXREWRITE,
Willy Tarreauc77d3642018-12-12 06:19:42 +0100166 .chksize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)),
Willy Tarreaua24adf02014-11-27 01:11:56 +0100167 .reserved_bufs = RESERVED_BUFS,
Willy Tarreauf3045d22015-04-29 16:24:50 +0200168 .pattern_cache = DEFAULT_PAT_LRU_SIZE,
Olivier Houchard88698d92019-04-16 19:07:22 +0200169 .pool_low_ratio = 20,
170 .pool_high_ratio = 25,
Christopher Faulet41ba36f2019-07-19 09:36:45 +0200171 .max_http_hdr = MAX_HTTP_HDR,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200172#ifdef USE_OPENSSL
Emeric Brun46635772012-11-14 11:32:56 +0100173 .sslcachesize = SSLCACHESIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200174#endif
William Lallemandf3747832012-11-09 12:33:10 +0100175 .comp_maxlevel = 1,
Willy Tarreau7e312732014-02-12 16:35:14 +0100176#ifdef DEFAULT_IDLE_TIMER
177 .idle_timer = DEFAULT_IDLE_TIMER,
178#else
179 .idle_timer = 1000, /* 1 second */
180#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200181 },
Emeric Brun76d88952012-10-05 15:47:31 +0200182#ifdef USE_OPENSSL
183#ifdef DEFAULT_MAXSSLCONN
Willy Tarreau403edff2012-09-06 11:58:37 +0200184 .maxsslconn = DEFAULT_MAXSSLCONN,
185#endif
Emeric Brun76d88952012-10-05 15:47:31 +0200186#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187 /* others NULL OK */
188};
189
190/*********************************************************************/
191
192int stopping; /* non zero means stopping in progress */
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100193int killed; /* non zero means a hard-stop is triggered */
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200194int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */
William Lallemanda7199262018-11-16 16:57:20 +0100195int unstoppable_jobs = 0; /* number of active jobs that can't be stopped during a soft stop */
Willy Tarreau199ad242018-11-05 16:31:22 +0100196int active_peers = 0; /* number of active peers (connection attempts and connected) */
Willy Tarreau2d372c22018-11-05 17:12:27 +0100197int connected_peers = 0; /* number of connected peers (verified ones) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200198
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500199/* Here we store information about the pids of the processes we may pause
Willy Tarreaubaaee002006-06-26 02:48:02 +0200200 * or kill. We will send them a signal every 10 ms until we can bind to all
201 * our ports. With 200 retries, that's about 2 seconds.
202 */
203#define MAX_START_RETRIES 200
Willy Tarreaubaaee002006-06-26 02:48:02 +0200204static int *oldpids = NULL;
205static int oldpids_sig; /* use USR1 or TERM */
206
Olivier Houchardf73629d2017-04-05 22:33:04 +0200207/* Path to the unix socket we use to retrieve listener sockets from the old process */
208static const char *old_unixsocket;
209
William Lallemand85b0bd92017-06-01 17:38:53 +0200210static char *cur_unixsocket = NULL;
211
William Lallemandcb11fd22017-06-01 17:38:52 +0200212int atexit_flag = 0;
213
Willy Tarreaubb545b42010-08-25 12:58:59 +0200214int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215const int zero = 0;
216const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200217const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100219char hostname[MAX_HOSTNAME_LEN];
Dragan Dosen4f014152020-06-18 16:56:47 +0200220char *localpeer = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200221
William Lallemand00417412020-06-05 14:08:41 +0200222static char **old_argv = NULL; /* previous argv but cleaned up */
William Lallemand73b85e72017-06-01 17:38:51 +0200223
William Lallemandbc193052018-09-11 10:06:26 +0200224struct list proc_list = LIST_HEAD_INIT(proc_list);
225
226int master = 0; /* 1 if in master, 0 if in child */
Willy Tarreaubf696402019-03-01 10:09:28 +0100227unsigned int rlim_fd_cur_at_boot = 0;
228unsigned int rlim_fd_max_at_boot = 0;
William Lallemandbc193052018-09-11 10:06:26 +0200229
Willy Tarreau6c3a6812020-03-06 18:57:15 +0100230/* per-boot randomness */
231unsigned char boot_seed[20]; /* per-boot random seed (160 bits initially) */
232
William Lallemand16dd1b32018-11-19 18:46:18 +0100233struct mworker_proc *proc_self = NULL;
William Lallemandbc193052018-09-11 10:06:26 +0200234
William Lallemandb3f2be32018-09-11 10:06:18 +0200235static void *run_thread_poll_loop(void *data);
236
Willy Tarreauff055502014-04-28 22:27:06 +0200237/* bitfield of a few warnings to emit just once (WARN_*) */
238unsigned int warned = 0;
239
William Lallemande7361152018-10-26 14:47:36 +0200240/* master CLI configuration (-S flag) */
241struct list mworker_cli_conf = LIST_HEAD_INIT(mworker_cli_conf);
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100242
243/* These are strings to be reported in the output of "haproxy -vv". They may
244 * either be constants (in which case must_free must be zero) or dynamically
245 * allocated strings to pass to free() on exit, and in this case must_free
246 * must be non-zero.
247 */
248struct list build_opts_list = LIST_HEAD_INIT(build_opts_list);
249struct build_opts_str {
250 struct list list;
251 const char *str;
252 int must_free;
253};
254
Willy Tarreaue6945732016-12-21 19:57:00 +0100255/* These functions are called just after the point where the program exits
256 * after a config validity check, so they are generally suited for resource
257 * allocation and slow initializations that should be skipped during basic
258 * config checks. The functions must return 0 on success, or a combination
259 * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause
260 * and immediate exit, so the function must have emitted any useful error.
261 */
262struct list post_check_list = LIST_HEAD_INIT(post_check_list);
263struct post_check_fct {
264 struct list list;
265 int (*fct)();
266};
267
Christopher Fauletc1692962019-08-12 09:51:07 +0200268/* These functions are called for each proxy just after the config validity
269 * check. The functions must return 0 on success, or a combination of ERR_*
270 * flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause and immediate
271 * exit, so the function must have emitted any useful error.
272 */
273struct list post_proxy_check_list = LIST_HEAD_INIT(post_proxy_check_list);
274struct post_proxy_check_fct {
275 struct list list;
276 int (*fct)(struct proxy *);
277};
278
279/* These functions are called for each server 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_server_check_list = LIST_HEAD_INIT(post_server_check_list);
285struct post_server_check_fct {
286 struct list list;
287 int (*fct)(struct server *);
288};
289
Willy Tarreau082b6282019-05-22 14:42:12 +0200290/* These functions are called for each thread just after the thread creation
291 * and before running the init functions. They should be used to do per-thread
292 * (re-)allocations that are needed by subsequent functoins. They must return 0
293 * if an error occurred. */
294struct list per_thread_alloc_list = LIST_HEAD_INIT(per_thread_alloc_list);
295struct per_thread_alloc_fct {
Willy Tarreau05554e62016-12-21 20:46:26 +0100296 struct list list;
Willy Tarreau082b6282019-05-22 14:42:12 +0200297 int (*fct)();
Willy Tarreau05554e62016-12-21 20:46:26 +0100298};
299
Christopher Faulet415f6112017-07-25 16:52:58 +0200300/* These functions are called for each thread just after the thread creation
301 * and before running the scheduler. They should be used to do per-thread
302 * initializations. They must return 0 if an error occurred. */
303struct list per_thread_init_list = LIST_HEAD_INIT(per_thread_init_list);
304struct per_thread_init_fct {
305 struct list list;
306 int (*fct)();
307};
308
Willy Tarreau082b6282019-05-22 14:42:12 +0200309/* These functions are called when freeing the global sections at the end of
310 * deinit, after everything is stopped. They don't return anything. They should
311 * not release shared resources that are possibly used by other deinit
312 * functions, only close/release what is private. Use the per_thread_free_list
313 * to release shared resources.
314 */
315struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list);
316struct post_deinit_fct {
317 struct list list;
318 void (*fct)();
319};
320
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +0200321/* These functions are called when freeing a proxy during the deinit, after
322 * everything isg stopped. They don't return anything. They should not release
323 * the proxy itself or any shared resources that are possibly used by other
324 * deinit functions, only close/release what is private.
325 */
326struct list proxy_deinit_list = LIST_HEAD_INIT(proxy_deinit_list);
327struct proxy_deinit_fct {
328 struct list list;
329 void (*fct)(struct proxy *);
330};
331
332/* These functions are called when freeing a server 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 server_deinit_list = LIST_HEAD_INIT(server_deinit_list);
338struct server_deinit_fct {
339 struct list list;
340 void (*fct)(struct server *);
341};
342
Willy Tarreau082b6282019-05-22 14:42:12 +0200343/* These functions are called when freeing the global sections at the end of
344 * deinit, after the thread deinit functions, to release unneeded memory
345 * allocations. They don't return anything, and they work in best effort mode
346 * as their sole goal is to make valgrind mostly happy.
347 */
348struct list per_thread_free_list = LIST_HEAD_INIT(per_thread_free_list);
349struct per_thread_free_fct {
350 struct list list;
351 int (*fct)();
352};
353
Christopher Faulet415f6112017-07-25 16:52:58 +0200354/* These functions are called for each thread just after the scheduler loop and
355 * before exiting the thread. They don't return anything and, as for post-deinit
356 * functions, they work in best effort mode as their sole goal is to make
357 * valgrind mostly happy. */
358struct list per_thread_deinit_list = LIST_HEAD_INIT(per_thread_deinit_list);
359struct per_thread_deinit_fct {
360 struct list list;
361 void (*fct)();
362};
363
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364/*********************************************************************/
365/* general purpose functions ***************************************/
366/*********************************************************************/
367
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100368/* used to register some build option strings at boot. Set must_free to
369 * non-zero if the string must be freed upon exit.
370 */
371void hap_register_build_opts(const char *str, int must_free)
372{
373 struct build_opts_str *b;
374
375 b = calloc(1, sizeof(*b));
376 if (!b) {
377 fprintf(stderr, "out of memory\n");
378 exit(1);
379 }
380 b->str = str;
381 b->must_free = must_free;
382 LIST_ADDQ(&build_opts_list, &b->list);
383}
384
Willy Tarreaue6945732016-12-21 19:57:00 +0100385/* used to register some initialization functions to call after the checks. */
386void hap_register_post_check(int (*fct)())
387{
388 struct post_check_fct *b;
389
390 b = calloc(1, sizeof(*b));
391 if (!b) {
392 fprintf(stderr, "out of memory\n");
393 exit(1);
394 }
395 b->fct = fct;
396 LIST_ADDQ(&post_check_list, &b->list);
397}
398
Christopher Fauletc1692962019-08-12 09:51:07 +0200399/* used to register some initialization functions to call for each proxy after
400 * the checks.
401 */
402void hap_register_post_proxy_check(int (*fct)(struct proxy *))
403{
404 struct post_proxy_check_fct *b;
405
406 b = calloc(1, sizeof(*b));
407 if (!b) {
408 fprintf(stderr, "out of memory\n");
409 exit(1);
410 }
411 b->fct = fct;
412 LIST_ADDQ(&post_proxy_check_list, &b->list);
413}
414
415/* used to register some initialization functions to call for each server after
416 * the checks.
417 */
418void hap_register_post_server_check(int (*fct)(struct server *))
419{
420 struct post_server_check_fct *b;
421
422 b = calloc(1, sizeof(*b));
423 if (!b) {
424 fprintf(stderr, "out of memory\n");
425 exit(1);
426 }
427 b->fct = fct;
428 LIST_ADDQ(&post_server_check_list, &b->list);
429}
430
Willy Tarreau05554e62016-12-21 20:46:26 +0100431/* used to register some de-initialization functions to call after everything
432 * has stopped.
433 */
434void hap_register_post_deinit(void (*fct)())
435{
436 struct post_deinit_fct *b;
437
438 b = calloc(1, sizeof(*b));
439 if (!b) {
440 fprintf(stderr, "out of memory\n");
441 exit(1);
442 }
443 b->fct = fct;
444 LIST_ADDQ(&post_deinit_list, &b->list);
445}
446
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +0200447/* used to register some per proxy de-initialization functions to call after
448 * everything has stopped.
449 */
450void hap_register_proxy_deinit(void (*fct)(struct proxy *))
451{
452 struct proxy_deinit_fct *b;
453
454 b = calloc(1, sizeof(*b));
455 if (!b) {
456 fprintf(stderr, "out of memory\n");
457 exit(1);
458 }
459 b->fct = fct;
460 LIST_ADDQ(&proxy_deinit_list, &b->list);
461}
462
463
464/* used to register some per server de-initialization functions to call after
465 * everything has stopped.
466 */
467void hap_register_server_deinit(void (*fct)(struct server *))
468{
469 struct server_deinit_fct *b;
470
471 b = calloc(1, sizeof(*b));
472 if (!b) {
473 fprintf(stderr, "out of memory\n");
474 exit(1);
475 }
476 b->fct = fct;
477 LIST_ADDQ(&server_deinit_list, &b->list);
478}
479
Willy Tarreau082b6282019-05-22 14:42:12 +0200480/* used to register some allocation functions to call for each thread. */
481void hap_register_per_thread_alloc(int (*fct)())
482{
483 struct per_thread_alloc_fct *b;
484
485 b = calloc(1, sizeof(*b));
486 if (!b) {
487 fprintf(stderr, "out of memory\n");
488 exit(1);
489 }
490 b->fct = fct;
491 LIST_ADDQ(&per_thread_alloc_list, &b->list);
492}
493
Christopher Faulet415f6112017-07-25 16:52:58 +0200494/* used to register some initialization functions to call for each thread. */
495void hap_register_per_thread_init(int (*fct)())
496{
497 struct per_thread_init_fct *b;
498
499 b = calloc(1, sizeof(*b));
500 if (!b) {
501 fprintf(stderr, "out of memory\n");
502 exit(1);
503 }
504 b->fct = fct;
505 LIST_ADDQ(&per_thread_init_list, &b->list);
506}
507
508/* used to register some de-initialization functions to call for each thread. */
509void hap_register_per_thread_deinit(void (*fct)())
510{
511 struct per_thread_deinit_fct *b;
512
513 b = calloc(1, sizeof(*b));
514 if (!b) {
515 fprintf(stderr, "out of memory\n");
516 exit(1);
517 }
518 b->fct = fct;
519 LIST_ADDQ(&per_thread_deinit_list, &b->list);
520}
521
Willy Tarreau082b6282019-05-22 14:42:12 +0200522/* used to register some free functions to call for each thread. */
523void hap_register_per_thread_free(int (*fct)())
524{
525 struct per_thread_free_fct *b;
526
527 b = calloc(1, sizeof(*b));
528 if (!b) {
529 fprintf(stderr, "out of memory\n");
530 exit(1);
531 }
532 b->fct = fct;
533 LIST_ADDQ(&per_thread_free_list, &b->list);
534}
535
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100536static void display_version()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537{
Tim Duesterhusdfad6a42020-04-18 16:02:47 +0200538 struct utsname utsname;
539
Willy Tarreau08dd2022019-11-21 18:07:30 +0100540 printf("HA-Proxy version %s %s - https://haproxy.org/\n"
541 PRODUCT_STATUS "\n", haproxy_version, haproxy_date);
Willy Tarreau47479eb2019-11-21 18:48:20 +0100542
543 if (strlen(PRODUCT_URL_BUGS) > 0) {
544 char base_version[20];
545 int dots = 0;
546 char *del;
547
548 /* only retrieve the base version without distro-specific extensions */
549 for (del = haproxy_version; *del; del++) {
550 if (*del == '.')
551 dots++;
552 else if (*del < '0' || *del > '9')
553 break;
554 }
555
556 strlcpy2(base_version, haproxy_version, del - haproxy_version + 1);
557 if (dots < 2)
558 printf("Known bugs: https://github.com/haproxy/haproxy/issues?q=is:issue+is:open\n");
559 else
560 printf("Known bugs: " PRODUCT_URL_BUGS "\n", base_version);
561 }
Tim Duesterhusdfad6a42020-04-18 16:02:47 +0200562
563 if (uname(&utsname) == 0) {
564 printf("Running on: %s %s %s %s\n", utsname.sysname, utsname.release, utsname.version, utsname.machine);
565 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200566}
567
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100568static void display_build_opts()
Willy Tarreau7b066db2007-12-02 11:28:59 +0100569{
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100570 struct build_opts_str *item;
571
Willy Tarreau7b066db2007-12-02 11:28:59 +0100572 printf("Build options :"
573#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100574 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100575#endif
576#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100577 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100578#endif
579#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100580 "\n CC = " BUILD_CC
581#endif
582#ifdef BUILD_CFLAGS
583 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100584#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100585#ifdef BUILD_OPTIONS
586 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100587#endif
Willy Tarreau7728ed32019-03-27 13:20:08 +0100588#ifdef BUILD_FEATURES
589 "\n\nFeature list : " BUILD_FEATURES
590#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200591 "\n\nDefault settings :"
Willy Tarreauca783d42019-03-13 10:03:07 +0100592 "\n bufsize = %d, maxrewrite = %d, maxpollevents = %d"
Willy Tarreau27a674e2009-08-17 07:23:33 +0200593 "\n\n",
Willy Tarreauca783d42019-03-13 10:03:07 +0100594 BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS);
Willy Tarreaube5b6852009-10-03 18:57:08 +0200595
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100596 list_for_each_entry(item, &build_opts_list, list) {
597 puts(item->str);
598 }
599
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100600 putchar('\n');
601
Willy Tarreaube5b6852009-10-03 18:57:08 +0200602 list_pollers(stdout);
603 putchar('\n');
Christopher Faulet98d9fe22018-04-10 14:37:32 +0200604 list_mux_proto(stdout);
605 putchar('\n');
Willy Tarreau679bba12019-03-19 08:08:10 +0100606 list_services(stdout);
607 putchar('\n');
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100608 list_filters(stdout);
609 putchar('\n');
Willy Tarreau7b066db2007-12-02 11:28:59 +0100610}
611
Willy Tarreaubaaee002006-06-26 02:48:02 +0200612/*
613 * This function prints the command line usage and exits
614 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100615static void usage(char *name)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200616{
617 display_version();
618 fprintf(stderr,
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200619 "Usage : %s [-f <cfgfile|cfgdir>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200620 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
Willy Tarreaua088d312015-10-08 11:58:48 +0200621 " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100622 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200623 " -d enters debug mode ; -db only disables background mode.\n"
Willy Tarreau6e064432012-05-08 15:40:42 +0200624 " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200625 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200626 " -D goes daemon ; -C changes to <dir> before loading files.\n"
William Lallemand095ba4c2017-06-01 17:38:50 +0200627 " -W master-worker mode.\n"
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100628#if defined(USE_SYSTEMD)
629 " -Ws master-worker mode with systemd notify support.\n"
630#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200631 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200632 " -c check mode : only check config files and exit\n"
Willy Tarreauca783d42019-03-13 10:03:07 +0100633 " -n sets the maximum total # of connections (uses ulimit -n)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634 " -m limits the usable amount of memory (in MB)\n"
635 " -N sets the default, per-proxy maximum # of connections (%d)\n"
Emeric Brun2b920a12010-09-23 18:30:22 +0200636 " -L set local peer name (default to hostname)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637 " -p writes pids of all children to this file\n"
Willy Tarreaue5733232019-05-22 19:24:06 +0200638#if defined(USE_EPOLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200639 " -de disables epoll() usage even when available\n"
640#endif
Willy Tarreaue5733232019-05-22 19:24:06 +0200641#if defined(USE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200642 " -dk disables kqueue() usage even when available\n"
643#endif
Willy Tarreaue5733232019-05-22 19:24:06 +0200644#if defined(USE_EVPORTS)
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +0000645 " -dv disables event ports usage even when available\n"
646#endif
Willy Tarreaue5733232019-05-22 19:24:06 +0200647#if defined(USE_POLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648 " -dp disables poll() usage even when available\n"
649#endif
Willy Tarreaue5733232019-05-22 19:24:06 +0200650#if defined(USE_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100651 " -dS disables splice usage (broken on old kernels)\n"
652#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200653#if defined(USE_GETADDRINFO)
654 " -dG disables getaddrinfo() usage\n"
655#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000656#if defined(SO_REUSEPORT)
657 " -dR disables SO_REUSEPORT usage\n"
658#endif
Willy Tarreau3eed10e2016-11-07 21:03:16 +0100659 " -dr ignores server address resolution failures\n"
Emeric Brun850efd52014-01-29 12:24:34 +0100660 " -dV disables SSL verify on servers side\n"
Willy Tarreau3eb10b82020-04-15 16:42:39 +0200661 " -dW fails if any warning is emitted\n"
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +0200662 " -sf/-st [pid ]* finishes/terminates old pids.\n"
Olivier Houchardf73629d2017-04-05 22:33:04 +0200663 " -x <unix_socket> get listening sockets from a unix socket\n"
William Lallemand63329e32019-06-13 17:03:37 +0200664 " -S <bind>[,<bind options>...] new master CLI\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665 "\n",
Willy Tarreauca783d42019-03-13 10:03:07 +0100666 name, cfg_maxpconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667 exit(1);
668}
669
670
671
672/*********************************************************************/
673/* more specific functions ***************************************/
674/*********************************************************************/
675
William Lallemand73b85e72017-06-01 17:38:51 +0200676/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
677 * pids the signal was correctly delivered to.
678 */
William Lallemande25473c2019-04-01 11:29:56 +0200679int tell_old_pids(int sig)
William Lallemand73b85e72017-06-01 17:38:51 +0200680{
681 int p;
682 int ret = 0;
683 for (p = 0; p < nb_oldpids; p++)
684 if (kill(oldpids[p], sig) == 0)
685 ret++;
686 return ret;
687}
688
William Lallemand75ea0a02017-11-15 19:02:58 +0100689/*
William Lallemand73b85e72017-06-01 17:38:51 +0200690 * remove a pid forom the olpid array and decrease nb_oldpids
691 * return 1 pid was found otherwise return 0
692 */
693
694int delete_oldpid(int pid)
695{
696 int i;
697
698 for (i = 0; i < nb_oldpids; i++) {
699 if (oldpids[i] == pid) {
700 oldpids[i] = oldpids[nb_oldpids - 1];
701 oldpids[nb_oldpids - 1] = 0;
702 nb_oldpids--;
703 return 1;
704 }
705 }
706 return 0;
707}
708
William Lallemand85b0bd92017-06-01 17:38:53 +0200709
710static void get_cur_unixsocket()
711{
712 /* if -x was used, try to update the stat socket if not available anymore */
713 if (global.stats_fe) {
714 struct bind_conf *bind_conf;
715
716 /* pass through all stats socket */
717 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
718 struct listener *l;
719
720 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
721
722 if (l->addr.ss_family == AF_UNIX &&
723 (bind_conf->level & ACCESS_FD_LISTENERS)) {
724 const struct sockaddr_un *un;
725
726 un = (struct sockaddr_un *)&l->addr;
727 /* priority to old_unixsocket */
728 if (!cur_unixsocket) {
729 cur_unixsocket = strdup(un->sun_path);
730 } else {
731 if (old_unixsocket && !strcmp(un->sun_path, old_unixsocket)) {
732 free(cur_unixsocket);
733 cur_unixsocket = strdup(old_unixsocket);
734 return;
735 }
736 }
737 }
738 }
739 }
740 }
741 if (!cur_unixsocket && old_unixsocket)
742 cur_unixsocket = strdup(old_unixsocket);
743}
744
William Lallemand73b85e72017-06-01 17:38:51 +0200745/*
746 * When called, this function reexec haproxy with -sf followed by current
Joseph Herlant03420902018-11-15 10:41:50 -0800747 * children PIDs and possibly old children PIDs if they didn't leave yet.
William Lallemand73b85e72017-06-01 17:38:51 +0200748 */
William Lallemanda57b7e32018-12-14 21:11:31 +0100749void mworker_reload()
William Lallemand73b85e72017-06-01 17:38:51 +0200750{
William Lallemand00417412020-06-05 14:08:41 +0200751 char **next_argv = NULL;
752 int old_argc = 0; /* previous number of argument */
William Lallemand73b85e72017-06-01 17:38:51 +0200753 int next_argc = 0;
William Lallemand00417412020-06-05 14:08:41 +0200754 int i = 0;
William Lallemand73b85e72017-06-01 17:38:51 +0200755 char *msg = NULL;
Willy Tarreau8dca1952019-03-01 10:21:55 +0100756 struct rlimit limit;
William Lallemand7c756a82018-11-26 11:53:40 +0100757 struct per_thread_deinit_fct *ptdf;
William Lallemand73b85e72017-06-01 17:38:51 +0200758
759 mworker_block_signals();
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100760#if defined(USE_SYSTEMD)
761 if (global.tune.options & GTUNE_USE_SYSTEMD)
762 sd_notify(0, "RELOADING=1");
763#endif
William Lallemand73b85e72017-06-01 17:38:51 +0200764 setenv("HAPROXY_MWORKER_REEXEC", "1", 1);
765
William Lallemandbc193052018-09-11 10:06:26 +0200766 mworker_proc_list_to_env(); /* put the children description in the env */
767
William Lallemand7c756a82018-11-26 11:53:40 +0100768 /* during the reload we must ensure that every FDs that can't be
769 * reuse (ie those that are not referenced in the proc_list)
770 * are closed or they will leak. */
771
772 /* close the listeners FD */
773 mworker_cli_proxy_stop();
William Lallemand16866672019-06-24 17:40:48 +0200774
775 if (getenv("HAPROXY_MWORKER_WAIT_ONLY") == NULL) {
776 /* close the poller FD and the thread waker pipe FD */
777 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
778 ptdf->fct();
779 if (fdtab)
780 deinit_pollers();
781 }
Willy Tarreau5db847a2019-05-09 14:13:35 +0200782#if defined(USE_OPENSSL) && (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
William Lallemand5fdb5b32019-10-15 14:04:08 +0200783 /* close random device FDs */
784 RAND_keep_random_devices_open(0);
Rob Allen56996da2019-05-03 09:11:32 +0100785#endif
William Lallemand7c756a82018-11-26 11:53:40 +0100786
Willy Tarreau8dca1952019-03-01 10:21:55 +0100787 /* restore the initial FD limits */
788 limit.rlim_cur = rlim_fd_cur_at_boot;
789 limit.rlim_max = rlim_fd_max_at_boot;
790 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
791 getrlimit(RLIMIT_NOFILE, &limit);
792 ha_warning("Failed to restore initial FD limits (cur=%u max=%u), using cur=%u max=%u\n",
793 rlim_fd_cur_at_boot, rlim_fd_max_at_boot,
794 (unsigned int)limit.rlim_cur, (unsigned int)limit.rlim_max);
795 }
796
William Lallemand73b85e72017-06-01 17:38:51 +0200797 /* compute length */
William Lallemand00417412020-06-05 14:08:41 +0200798 while (old_argv[old_argc])
799 old_argc++;
William Lallemand73b85e72017-06-01 17:38:51 +0200800
William Lallemand85b0bd92017-06-01 17:38:53 +0200801 /* 1 for haproxy -sf, 2 for -x /socket */
William Lallemand00417412020-06-05 14:08:41 +0200802 next_argv = calloc(old_argc + 1 + 2 + mworker_child_nb() + nb_oldpids + 1, sizeof(char *));
William Lallemand73b85e72017-06-01 17:38:51 +0200803 if (next_argv == NULL)
804 goto alloc_error;
805
William Lallemand00417412020-06-05 14:08:41 +0200806 /* copy the program name */
807 next_argv[next_argc++] = old_argv[0];
808
809 /* insert the new options just after argv[0] in case we have a -- */
810
William Lallemand73b85e72017-06-01 17:38:51 +0200811 /* add -sf <PID>* to argv */
William Lallemand3f128872019-04-01 11:29:59 +0200812 if (mworker_child_nb() > 0) {
813 struct mworker_proc *child;
814
William Lallemand73b85e72017-06-01 17:38:51 +0200815 next_argv[next_argc++] = "-sf";
William Lallemand3f128872019-04-01 11:29:59 +0200816
817 list_for_each_entry(child, &proc_list, list) {
William Lallemand677e2f22019-11-19 17:04:18 +0100818 if (!(child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) || child->pid <= -1 )
William Lallemand3f128872019-04-01 11:29:59 +0200819 continue;
William Lallemand00417412020-06-05 14:08:41 +0200820 if ((next_argv[next_argc++] = memprintf(&msg, "%d", child->pid)) == NULL)
William Lallemand73b85e72017-06-01 17:38:51 +0200821 goto alloc_error;
822 msg = NULL;
823 }
824 }
William Lallemand2bf6d622017-06-20 11:20:23 +0200825 /* add the -x option with the stat socket */
William Lallemand85b0bd92017-06-01 17:38:53 +0200826 if (cur_unixsocket) {
William Lallemand2bf6d622017-06-20 11:20:23 +0200827 next_argv[next_argc++] = "-x";
828 next_argv[next_argc++] = (char *)cur_unixsocket;
William Lallemand85b0bd92017-06-01 17:38:53 +0200829 }
830
William Lallemand00417412020-06-05 14:08:41 +0200831 /* copy the previous options */
832 for (i = 1; i < old_argc; i++)
833 next_argv[next_argc++] = old_argv[i];
834
Christopher Faulet767a84b2017-11-24 16:50:31 +0100835 ha_warning("Reexecuting Master process\n");
Willy Tarreaue0d86e22019-08-26 10:37:39 +0200836 signal(SIGPROF, SIG_IGN);
Tim Duesterhus0436ab72017-11-12 17:39:18 +0100837 execvp(next_argv[0], next_argv);
William Lallemand73b85e72017-06-01 17:38:51 +0200838
Christopher Faulet767a84b2017-11-24 16:50:31 +0100839 ha_warning("Failed to reexecute the master process [%d]: %s\n", pid, strerror(errno));
William Lallemand9fc6c972020-06-08 10:01:13 +0200840 free(next_argv);
841 next_argv = NULL;
William Lallemand722d4ca2017-11-15 19:02:55 +0100842 return;
843
William Lallemand73b85e72017-06-01 17:38:51 +0200844alloc_error:
William Lallemand00417412020-06-05 14:08:41 +0200845 free(next_argv);
846 next_argv = NULL;
Joseph Herlant07a08342018-11-15 10:43:05 -0800847 ha_warning("Failed to reexecute the master process [%d]: Cannot allocate memory\n", pid);
William Lallemand73b85e72017-06-01 17:38:51 +0200848 return;
849}
850
William Lallemandb3f2be32018-09-11 10:06:18 +0200851static void mworker_loop()
852{
853
854#if defined(USE_SYSTEMD)
855 if (global.tune.options & GTUNE_USE_SYSTEMD)
856 sd_notifyf(0, "READY=1\nMAINPID=%lu", (unsigned long)getpid());
857#endif
Willy Tarreaud83b6c12019-04-18 11:31:36 +0200858 /* Busy polling makes no sense in the master :-) */
859 global.tune.options &= ~GTUNE_BUSY_POLLING;
William Lallemandb3f2be32018-09-11 10:06:18 +0200860
William Lallemandbc193052018-09-11 10:06:26 +0200861 master = 1;
862
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100863 signal_unregister(SIGTTIN);
864 signal_unregister(SIGTTOU);
William Lallemand0564d412018-11-20 17:36:53 +0100865 signal_unregister(SIGUSR1);
866 signal_unregister(SIGHUP);
867 signal_unregister(SIGQUIT);
868
William Lallemandb3f2be32018-09-11 10:06:18 +0200869 signal_register_fct(SIGTERM, mworker_catch_sigterm, SIGTERM);
870 signal_register_fct(SIGUSR1, mworker_catch_sigterm, SIGUSR1);
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100871 signal_register_fct(SIGTTIN, mworker_broadcast_signal, SIGTTIN);
872 signal_register_fct(SIGTTOU, mworker_broadcast_signal, SIGTTOU);
William Lallemandb3f2be32018-09-11 10:06:18 +0200873 signal_register_fct(SIGINT, mworker_catch_sigterm, SIGINT);
874 signal_register_fct(SIGHUP, mworker_catch_sighup, SIGHUP);
875 signal_register_fct(SIGUSR2, mworker_catch_sighup, SIGUSR2);
876 signal_register_fct(SIGCHLD, mworker_catch_sigchld, SIGCHLD);
877
878 mworker_unblock_signals();
879 mworker_cleanlisteners();
William Lallemand27f3fa52018-12-06 14:05:20 +0100880 mworker_cleantasks();
William Lallemandb3f2be32018-09-11 10:06:18 +0200881
William Lallemandbc193052018-09-11 10:06:26 +0200882 mworker_catch_sigchld(NULL); /* ensure we clean the children in case
883 some SIGCHLD were lost */
884
William Lallemandb3f2be32018-09-11 10:06:18 +0200885 global.nbthread = 1;
886 relative_pid = 1;
887 pid_bit = 1;
Willy Tarreaua38a7172019-02-02 17:11:28 +0100888 all_proc_mask = 1;
William Lallemandb3f2be32018-09-11 10:06:18 +0200889
William Lallemand2672eb92018-12-14 15:52:39 +0100890#ifdef USE_THREAD
891 tid_bit = 1;
892 all_threads_mask = 1;
893#endif
894
William Lallemandb3f2be32018-09-11 10:06:18 +0200895 jobs++; /* this is the "master" job, we want to take care of the
896 signals even if there is no listener so the poll loop don't
897 leave */
898
899 fork_poller();
Willy Tarreaub4f7cc32019-05-03 09:27:30 +0200900 run_thread_poll_loop(0);
William Lallemandb3f2be32018-09-11 10:06:18 +0200901}
William Lallemandcb11fd22017-06-01 17:38:52 +0200902
903/*
904 * Reexec the process in failure mode, instead of exiting
905 */
906void reexec_on_failure()
907{
908 if (!atexit_flag)
909 return;
910
911 setenv("HAPROXY_MWORKER_WAIT_ONLY", "1", 1);
912
Christopher Faulet767a84b2017-11-24 16:50:31 +0100913 ha_warning("Reexecuting Master process in waitpid mode\n");
William Lallemandcb11fd22017-06-01 17:38:52 +0200914 mworker_reload();
William Lallemandcb11fd22017-06-01 17:38:52 +0200915}
William Lallemand73b85e72017-06-01 17:38:51 +0200916
917
918/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200919 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
920 * a signal zero to all subscribers. This means that it's as easy as
921 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200922 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100923static void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924{
925 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200926 signal_unregister_handler(sh);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100927 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200928}
929
930/*
931 * upon SIGTTOU, we pause everything
932 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100933static void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934{
935 pause_proxies();
Willy Tarreaubafbe012017-11-24 17:34:44 +0100936 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937}
938
939/*
940 * upon SIGTTIN, let's have a soft stop.
941 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100942static void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943{
Willy Tarreaube58c382011-07-24 18:28:10 +0200944 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945}
946
947/*
948 * this function dumps every server's state when the process receives SIGHUP.
949 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100950static void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200951{
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100952 struct proxy *p = proxies_list;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200953
Christopher Faulet767a84b2017-11-24 16:50:31 +0100954 ha_warning("SIGHUP received, dumping servers states.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200955 while (p) {
956 struct server *s = p->srv;
957
958 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
959 while (s) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100960 chunk_printf(&trash,
961 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
962 p->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +0200963 (s->cur_state != SRV_ST_STOPPED) ? "UP" : "DOWN",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100964 s->cur_sess, s->nbpend, s->counters.cum_sess);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200965 ha_warning("%s\n", trash.area);
966 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200967 s = s->next;
968 }
969
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200970 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
971 if (!p->srv) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100972 chunk_printf(&trash,
973 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
974 p->id,
975 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 +0200976 } else if (p->srv_act == 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100977 chunk_printf(&trash,
978 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
979 p->id,
980 (p->srv_bck) ? "is running on backup servers" : "has no server available",
981 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 +0200982 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100983 chunk_printf(&trash,
984 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
985 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
986 p->id, p->srv_act, p->srv_bck,
987 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 +0200988 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200989 ha_warning("%s\n", trash.area);
990 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200991
992 p = p->next;
993 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200994}
995
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100996static void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200997{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200998 /* dump memory usage then free everything possible */
999 dump_pools();
Willy Tarreaubafbe012017-11-24 17:34:44 +01001000 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001001}
1002
William Lallemande1340412017-12-28 16:09:36 +01001003/*
1004 * This function dup2 the stdio FDs (0,1,2) with <fd>, then closes <fd>
1005 * If <fd> < 0, it opens /dev/null and use it to dup
1006 *
1007 * In the case of chrooting, you have to open /dev/null before the chroot, and
1008 * pass the <fd> to this function
1009 */
1010static void stdio_quiet(int fd)
1011{
1012 if (fd < 0)
1013 fd = open("/dev/null", O_RDWR, 0);
1014
1015 if (fd > -1) {
1016 fclose(stdin);
1017 fclose(stdout);
1018 fclose(stderr);
1019
1020 dup2(fd, 0);
1021 dup2(fd, 1);
1022 dup2(fd, 2);
1023 if (fd > 2)
1024 close(fd);
1025 return;
1026 }
1027
1028 ha_alert("Cannot open /dev/null\n");
1029 exit(EXIT_FAILURE);
1030}
1031
1032
Joseph Herlant03420902018-11-15 10:41:50 -08001033/* This function checks if cfg_cfgfiles contains directories.
1034 * If it finds one, it adds all the files (and only files) it contains
1035 * in cfg_cfgfiles in place of the directory (and removes the directory).
1036 * It adds the files in lexical order.
1037 * It adds only files with .cfg extension.
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001038 * It doesn't add files with name starting with '.'
1039 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001040static void cfgfiles_expand_directories(void)
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001041{
1042 struct wordlist *wl, *wlb;
1043 char *err = NULL;
1044
1045 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
1046 struct stat file_stat;
1047 struct dirent **dir_entries = NULL;
1048 int dir_entries_nb;
1049 int dir_entries_it;
1050
1051 if (stat(wl->s, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001052 ha_alert("Cannot open configuration file/directory %s : %s\n",
1053 wl->s,
1054 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001055 exit(1);
1056 }
1057
1058 if (!S_ISDIR(file_stat.st_mode))
1059 continue;
1060
1061 /* from this point wl->s is a directory */
1062
1063 dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort);
1064 if (dir_entries_nb < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001065 ha_alert("Cannot open configuration directory %s : %s\n",
1066 wl->s,
1067 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001068 exit(1);
1069 }
1070
1071 /* for each element in the directory wl->s */
1072 for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) {
1073 struct dirent *dir_entry = dir_entries[dir_entries_it];
1074 char *filename = NULL;
1075 char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg");
1076
1077 /* don't add filename that begin with .
Joseph Herlant03420902018-11-15 10:41:50 -08001078 * only add filename with .cfg extension
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001079 */
1080 if (dir_entry->d_name[0] == '.' ||
1081 !(d_name_cfgext && d_name_cfgext[4] == '\0'))
1082 goto next_dir_entry;
1083
1084 if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001085 ha_alert("Cannot load configuration files %s : out of memory.\n",
1086 filename);
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001087 exit(1);
1088 }
1089
1090 if (stat(filename, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001091 ha_alert("Cannot open configuration file %s : %s\n",
1092 wl->s,
1093 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001094 exit(1);
1095 }
1096
1097 /* don't add anything else than regular file in cfg_cfgfiles
1098 * this way we avoid loops
1099 */
1100 if (!S_ISREG(file_stat.st_mode))
1101 goto next_dir_entry;
1102
1103 if (!list_append_word(&wl->list, filename, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001104 ha_alert("Cannot load configuration files %s : %s\n",
1105 filename,
1106 err);
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001107 exit(1);
1108 }
1109
1110next_dir_entry:
1111 free(filename);
1112 free(dir_entry);
1113 }
1114
1115 free(dir_entries);
1116
1117 /* remove the current directory (wl) from cfg_cfgfiles */
1118 free(wl->s);
1119 LIST_DEL(&wl->list);
1120 free(wl);
1121 }
1122
1123 free(err);
1124}
1125
Willy Tarreaufebbce82020-08-28 18:45:01 +02001126/* Retrieves old sockets from worker process running the CLI at address
1127 * <unixsocket>. Fills xfer_sock_list with what is found. Returns 0 on
1128 * success, -1 on failure.
1129 */
Olivier Houchardf73629d2017-04-05 22:33:04 +02001130static int get_old_sockets(const char *unixsocket)
1131{
1132 char *cmsgbuf = NULL, *tmpbuf = NULL;
1133 int *tmpfd = NULL;
1134 struct sockaddr_un addr;
1135 struct cmsghdr *cmsg;
1136 struct msghdr msghdr;
1137 struct iovec iov;
1138 struct xfer_sock_list *xfer_sock = NULL;
Olivier Houchard54740872017-04-06 14:45:14 +02001139 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf73629d2017-04-05 22:33:04 +02001140 int sock = -1;
1141 int ret = -1;
1142 int ret2 = -1;
1143 int fd_nb;
1144 int got_fd = 0;
1145 int i = 0;
1146 size_t maxoff = 0, curoff = 0;
1147
1148 memset(&msghdr, 0, sizeof(msghdr));
1149 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
1150 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001151 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001152 goto out;
1153 }
1154 sock = socket(PF_UNIX, SOCK_STREAM, 0);
1155 if (sock < 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 }
Willy Tarreau719e07c2019-12-11 16:29:10 +01001160 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path) - 1);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001161 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
1162 addr.sun_family = PF_UNIX;
1163 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
1164 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001165 ha_warning("Failed to connect to the old process socket '%s'\n",
1166 unixsocket);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001167 goto out;
1168 }
Olivier Houchard54740872017-04-06 14:45:14 +02001169 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001170 iov.iov_base = &fd_nb;
1171 iov.iov_len = sizeof(fd_nb);
1172 msghdr.msg_iov = &iov;
1173 msghdr.msg_iovlen = 1;
1174 send(sock, "_getsocks\n", strlen("_getsocks\n"), 0);
1175 /* First, get the number of file descriptors to be received */
1176 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001177 ha_warning("Failed to get the number of sockets to be transferred !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001178 goto out;
1179 }
1180 if (fd_nb == 0) {
Willy Tarreaufebbce82020-08-28 18:45:01 +02001181 ret2 = 0;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001182 goto out;
1183 }
Olivier Houchardf143b802017-11-04 15:13:01 +01001184 tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001185 if (tmpbuf == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001186 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001187 goto out;
1188 }
1189 tmpfd = malloc(fd_nb * sizeof(int));
1190 if (tmpfd == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001191 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001192 goto out;
1193 }
1194 msghdr.msg_control = cmsgbuf;
1195 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
Olivier Houchardf143b802017-11-04 15:13:01 +01001196 iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001197 do {
1198 int ret3;
1199
1200 iov.iov_base = tmpbuf + curoff;
1201 ret = recvmsg(sock, &msghdr, 0);
1202 if (ret == -1 && errno == EINTR)
1203 continue;
1204 if (ret <= 0)
1205 break;
1206 /* Send an ack to let the sender know we got the sockets
1207 * and it can send some more
1208 */
1209 do {
1210 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
1211 } while (ret3 == -1 && errno == EINTR);
1212 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
1213 cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
1214 if (cmsg->cmsg_level == SOL_SOCKET &&
1215 cmsg->cmsg_type == SCM_RIGHTS) {
1216 size_t totlen = cmsg->cmsg_len -
1217 CMSG_LEN(0);
1218 if (totlen / sizeof(int) + got_fd > fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001219 ha_warning("Got to many sockets !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001220 goto out;
1221 }
1222 /*
1223 * Be paranoid and use memcpy() to avoid any
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001224 * potential alignment issue.
Olivier Houchardf73629d2017-04-05 22:33:04 +02001225 */
1226 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
1227 got_fd += totlen / sizeof(int);
1228 }
1229 }
1230 curoff += ret;
1231 } while (got_fd < fd_nb);
1232
1233 if (got_fd != fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001234 ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
1235 fd_nb, got_fd);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001236 goto out;
1237 }
1238 maxoff = curoff;
1239 curoff = 0;
1240 for (i = 0; i < got_fd; i++) {
1241 int fd = tmpfd[i];
1242 socklen_t socklen;
1243 int len;
1244
1245 xfer_sock = calloc(1, sizeof(*xfer_sock));
1246 if (!xfer_sock) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001247 ha_warning("Failed to allocate memory in get_old_sockets() !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001248 break;
1249 }
1250 xfer_sock->fd = -1;
1251
1252 socklen = sizeof(xfer_sock->addr);
1253 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001254 ha_warning("Failed to get socket address\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001255 free(xfer_sock);
Olivier Houchardbe7b1ce2017-07-17 17:25:33 +02001256 xfer_sock = NULL;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001257 continue;
1258 }
1259 if (curoff >= maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001260 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001261 goto out;
1262 }
1263 len = tmpbuf[curoff++];
1264 if (len > 0) {
1265 /* We have a namespace */
1266 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001267 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001268 goto out;
1269 }
1270 xfer_sock->namespace = malloc(len + 1);
1271 if (!xfer_sock->namespace) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001272 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001273 goto out;
1274 }
1275 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
1276 xfer_sock->namespace[len] = 0;
1277 curoff += len;
1278 }
1279 if (curoff >= maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001280 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001281 goto out;
1282 }
1283 len = tmpbuf[curoff++];
1284 if (len > 0) {
1285 /* We have an interface */
1286 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001287 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001288 goto out;
1289 }
1290 xfer_sock->iface = malloc(len + 1);
1291 if (!xfer_sock->iface) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001292 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001293 goto out;
1294 }
1295 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
Olivier Houchard33e083c2018-03-15 17:48:49 +01001296 xfer_sock->iface[len] = 0;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001297 curoff += len;
1298 }
1299 if (curoff + sizeof(int) > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001300 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001301 goto out;
1302 }
Willy Tarreaucf1f1932020-08-26 10:30:09 +02001303
1304 /* we used to have 32 bits of listener options here but we don't
1305 * use them anymore.
1306 */
1307 curoff += sizeof(int);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001308
Willy Tarreaubf3b06b2020-08-26 10:23:40 +02001309 /* determine the foreign status directly from the socket itself */
Willy Tarreau25140cc2020-08-28 15:40:33 +02001310 if (sock_inet_is_foreign(fd, xfer_sock->addr.ss_family))
Willy Tarreaubf3b06b2020-08-26 10:23:40 +02001311 xfer_sock->options |= LI_O_FOREIGN;
1312
Willy Tarreaubca5a4e2020-08-26 09:29:21 +02001313 /* keep only the v6only flag depending on what's currently
1314 * active on the socket, and always drop the v4v6 one.
1315 */
Willy Tarreaucf1f1932020-08-26 10:30:09 +02001316#if defined(IPV6_V6ONLY)
Willy Tarreaubca5a4e2020-08-26 09:29:21 +02001317 {
1318 int val = 0;
Willy Tarreaubca5a4e2020-08-26 09:29:21 +02001319 socklen_t len = sizeof(val);
1320
1321 if (xfer_sock->addr.ss_family == AF_INET6 &&
Willy Tarreaucf1f1932020-08-26 10:30:09 +02001322 getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &len) == 0 &&
1323 val > 0)
Willy Tarreaubca5a4e2020-08-26 09:29:21 +02001324 xfer_sock->options |= LI_O_V6ONLY;
Willy Tarreaubca5a4e2020-08-26 09:29:21 +02001325 }
Willy Tarreaucf1f1932020-08-26 10:30:09 +02001326#endif
Willy Tarreaubca5a4e2020-08-26 09:29:21 +02001327
Olivier Houchardf73629d2017-04-05 22:33:04 +02001328 xfer_sock->fd = fd;
1329 if (xfer_sock_list)
1330 xfer_sock_list->prev = xfer_sock;
1331 xfer_sock->next = xfer_sock_list;
1332 xfer_sock->prev = NULL;
1333 xfer_sock_list = xfer_sock;
1334 xfer_sock = NULL;
1335 }
1336
1337 ret2 = 0;
1338out:
1339 /* If we failed midway make sure to close the remaining
1340 * file descriptors
1341 */
1342 if (tmpfd != NULL && i < got_fd) {
1343 for (; i < got_fd; i++) {
1344 close(tmpfd[i]);
1345 }
1346 }
1347 free(tmpbuf);
1348 free(tmpfd);
1349 free(cmsgbuf);
1350 if (sock != -1)
1351 close(sock);
1352 if (xfer_sock) {
1353 free(xfer_sock->namespace);
1354 free(xfer_sock->iface);
1355 if (xfer_sock->fd != -1)
1356 close(xfer_sock->fd);
1357 free(xfer_sock);
1358 }
1359 return (ret2);
1360}
1361
Willy Tarreaubaaee002006-06-26 02:48:02 +02001362/*
William Lallemand73b85e72017-06-01 17:38:51 +02001363 * copy and cleanup the current argv
William Lallemanddf6c5a82020-06-04 17:40:23 +02001364 * Remove the -sf /-st / -x parameters
William Lallemand73b85e72017-06-01 17:38:51 +02001365 * Return an allocated copy of argv
1366 */
1367
1368static char **copy_argv(int argc, char **argv)
1369{
William Lallemanddf6c5a82020-06-04 17:40:23 +02001370 char **newargv, **retargv;
William Lallemand73b85e72017-06-01 17:38:51 +02001371
1372 newargv = calloc(argc + 2, sizeof(char *));
1373 if (newargv == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001374 ha_warning("Cannot allocate memory\n");
William Lallemand73b85e72017-06-01 17:38:51 +02001375 return NULL;
1376 }
William Lallemanddf6c5a82020-06-04 17:40:23 +02001377 retargv = newargv;
William Lallemand73b85e72017-06-01 17:38:51 +02001378
William Lallemanddf6c5a82020-06-04 17:40:23 +02001379 /* first copy argv[0] */
1380 *newargv++ = *argv++;
1381 argc--;
1382
1383 while (argc > 0) {
1384 if (**argv != '-') {
1385 /* non options are copied but will fail in the argument parser */
1386 *newargv++ = *argv++;
1387 argc--;
1388
1389 } else {
1390 char *flag;
1391
1392 flag = *argv + 1;
1393
1394 if (flag[0] == '-' && flag[1] == 0) {
1395 /* "--\0" copy every arguments till the end of argv */
1396 *newargv++ = *argv++;
1397 argc--;
1398
1399 while (argc > 0) {
1400 *newargv++ = *argv++;
1401 argc--;
1402 }
1403 } else {
1404 switch (*flag) {
1405 case 's':
1406 /* -sf / -st and their parameters are ignored */
1407 if (flag[1] == 'f' || flag[1] == 't') {
1408 argc--;
1409 argv++;
1410 /* The list can't contain a negative value since the only
1411 way to know the end of this list is by looking for the
1412 next option or the end of the options */
1413 while (argc > 0 && argv[0][0] != '-') {
1414 argc--;
1415 argv++;
1416 }
1417 }
1418 break;
1419
1420 case 'x':
1421 /* this option and its parameter are ignored */
1422 argc--;
1423 argv++;
1424 if (argc > 0) {
1425 argc--;
1426 argv++;
1427 }
1428 break;
1429
1430 case 'C':
1431 case 'n':
1432 case 'm':
1433 case 'N':
1434 case 'L':
1435 case 'f':
1436 case 'p':
1437 case 'S':
1438 /* these options have only 1 parameter which must be copied and can start with a '-' */
1439 *newargv++ = *argv++;
1440 argc--;
1441 if (argc == 0)
1442 goto error;
1443 *newargv++ = *argv++;
1444 argc--;
1445 break;
1446 default:
1447 /* for other options just copy them without parameters, this is also done
1448 * for options like "--foo", but this will fail in the argument parser.
1449 * */
1450 *newargv++ = *argv++;
1451 argc--;
1452 break;
1453 }
William Lallemand73b85e72017-06-01 17:38:51 +02001454 }
1455 }
William Lallemand73b85e72017-06-01 17:38:51 +02001456 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001457
William Lallemanddf6c5a82020-06-04 17:40:23 +02001458 return retargv;
1459
1460error:
1461 free(retargv);
1462 return NULL;
William Lallemand73b85e72017-06-01 17:38:51 +02001463}
1464
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001465
1466/* Performs basic random seed initialization. The main issue with this is that
1467 * srandom_r() only takes 32 bits and purposely provides a reproducible sequence,
1468 * which means that there will only be 4 billion possible random sequences once
1469 * srandom() is called, regardless of the internal state. Not calling it is
1470 * even worse as we'll always produce the same randoms sequences. What we do
1471 * here is to create an initial sequence from various entropy sources, hash it
1472 * using SHA1 and keep the resulting 160 bits available globally.
1473 *
1474 * We initialize the current process with the first 32 bits before starting the
1475 * polling loop, where all this will be changed to have process specific and
1476 * thread specific sequences.
Willy Tarreau52bf8392020-03-08 00:42:37 +01001477 *
1478 * Before starting threads, it's still possible to call random() as srandom()
1479 * is initialized from this, but after threads and/or processes are started,
1480 * only ha_random() is expected to be used to guarantee distinct sequences.
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001481 */
1482static void ha_random_boot(char *const *argv)
1483{
1484 unsigned char message[256];
1485 unsigned char *m = message;
1486 struct timeval tv;
1487 blk_SHA_CTX ctx;
1488 unsigned long l;
1489 int fd;
1490 int i;
1491
1492 /* start with current time as pseudo-random seed */
1493 gettimeofday(&tv, NULL);
1494 write_u32(m, tv.tv_sec); m += 4;
1495 write_u32(m, tv.tv_usec); m += 4;
1496
1497 /* PID and PPID add some OS-based randomness */
1498 write_u16(m, getpid()); m += 2;
1499 write_u16(m, getppid()); m += 2;
1500
1501 /* take up to 160 bits bytes from /dev/urandom if available (non-blocking) */
1502 fd = open("/dev/urandom", O_RDONLY);
1503 if (fd >= 0) {
1504 i = read(fd, m, 20);
1505 if (i > 0)
1506 m += i;
1507 close(fd);
1508 }
1509
1510 /* take up to 160 bits bytes from openssl (non-blocking) */
1511#ifdef USE_OPENSSL
1512 if (RAND_bytes(m, 20) == 1)
1513 m += 20;
1514#endif
1515
1516 /* take 160 bits from existing random in case it was already initialized */
1517 for (i = 0; i < 5; i++) {
1518 write_u32(m, random());
1519 m += 4;
1520 }
1521
1522 /* stack address (benefit form operating system's ASLR) */
1523 l = (unsigned long)&m;
1524 memcpy(m, &l, sizeof(l)); m += sizeof(l);
1525
1526 /* argv address (benefit form operating system's ASLR) */
1527 l = (unsigned long)&argv;
1528 memcpy(m, &l, sizeof(l)); m += sizeof(l);
1529
1530 /* use tv_usec again after all the operations above */
1531 gettimeofday(&tv, NULL);
1532 write_u32(m, tv.tv_usec); m += 4;
1533
1534 /*
1535 * At this point, ~84-92 bytes have been used
1536 */
1537
1538 /* finish with the hostname */
1539 strncpy((char *)m, hostname, message + sizeof(message) - m);
1540 m += strlen(hostname);
1541
1542 /* total message length */
1543 l = m - message;
1544
1545 memset(&ctx, 0, sizeof(ctx));
1546 blk_SHA1_Init(&ctx);
1547 blk_SHA1_Update(&ctx, message, l);
1548 blk_SHA1_Final(boot_seed, &ctx);
1549
1550 srandom(read_u32(boot_seed));
Willy Tarreau52bf8392020-03-08 00:42:37 +01001551 ha_random_seed(boot_seed, sizeof(boot_seed));
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001552}
1553
Willy Tarreau5a023f02019-03-01 14:19:31 +01001554/* considers splicing proxies' maxconn, computes the ideal global.maxpipes
1555 * setting, and returns it. It may return -1 meaning "unlimited" if some
1556 * unlimited proxies have been found and the global.maxconn value is not yet
1557 * set. It may also return a value greater than maxconn if it's not yet set.
1558 * Note that a value of zero means there is no need for pipes. -1 is never
1559 * returned if global.maxconn is valid.
1560 */
1561static int compute_ideal_maxpipes()
1562{
1563 struct proxy *cur;
1564 int nbfe = 0, nbbe = 0;
1565 int unlimited = 0;
1566 int pipes;
1567 int max;
1568
1569 for (cur = proxies_list; cur; cur = cur->next) {
1570 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
1571 if (cur->cap & PR_CAP_FE) {
1572 max = cur->maxconn;
1573 nbfe += max;
1574 if (!max) {
1575 unlimited = 1;
1576 break;
1577 }
1578 }
1579 if (cur->cap & PR_CAP_BE) {
1580 max = cur->fullconn ? cur->fullconn : global.maxconn;
1581 nbbe += max;
1582 if (!max) {
1583 unlimited = 1;
1584 break;
1585 }
1586 }
1587 }
1588 }
1589
1590 pipes = MAX(nbfe, nbbe);
1591 if (global.maxconn) {
1592 if (pipes > global.maxconn || unlimited)
1593 pipes = global.maxconn;
1594 } else if (unlimited) {
1595 pipes = -1;
1596 }
1597
1598 return pipes >= 4 ? pipes / 4 : pipes;
1599}
1600
Willy Tarreauac350932019-03-01 15:43:14 +01001601/* considers global.maxsocks, global.maxpipes, async engines, SSL frontends and
1602 * rlimits and computes an ideal maxconn. It's meant to be called only when
1603 * maxsock contains the sum of listening FDs, before it is updated based on
Willy Tarreaudf23c0c2019-03-13 10:10:49 +01001604 * maxconn and pipes. If there are not enough FDs left, DEFAULT_MAXCONN (by
1605 * default 100) is returned as it is expected that it will even run on tight
1606 * environments, and will maintain compatibility with previous packages that
1607 * used to rely on this value as the default one. The system will emit a
1608 * warning indicating how many FDs are missing anyway if needed.
Willy Tarreauac350932019-03-01 15:43:14 +01001609 */
1610static int compute_ideal_maxconn()
1611{
1612 int ssl_sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1613 int engine_fds = global.ssl_used_async_engines * ssl_sides;
1614 int pipes = compute_ideal_maxpipes();
Willy Tarreaub1beaa32020-03-06 10:25:31 +01001615 int remain = MAX(rlim_fd_cur_at_boot, rlim_fd_max_at_boot);
Willy Tarreauac350932019-03-01 15:43:14 +01001616 int maxconn;
1617
1618 /* we have to take into account these elements :
1619 * - number of engine_fds, which inflates the number of FD needed per
1620 * connection by this number.
1621 * - number of pipes per connection on average : for the unlimited
1622 * case, this is 0.5 pipe FDs per connection, otherwise it's a
1623 * fixed value of 2*pipes.
1624 * - two FDs per connection
1625 */
1626
1627 /* subtract listeners and checks */
1628 remain -= global.maxsock;
1629
Willy Tarreau3f200852019-03-14 19:13:17 +01001630 /* one epoll_fd/kqueue_fd per thread */
1631 remain -= global.nbthread;
1632
1633 /* one wake-up pipe (2 fd) per thread */
1634 remain -= 2 * global.nbthread;
1635
Willy Tarreauac350932019-03-01 15:43:14 +01001636 /* Fixed pipes values : we only subtract them if they're not larger
1637 * than the remaining FDs because pipes are optional.
1638 */
1639 if (pipes >= 0 && pipes * 2 < remain)
1640 remain -= pipes * 2;
1641
1642 if (pipes < 0) {
1643 /* maxsock = maxconn * 2 + maxconn/4 * 2 + maxconn * engine_fds.
1644 * = maxconn * (2 + 0.5 + engine_fds)
1645 * = maxconn * (4 + 1 + 2*engine_fds) / 2
1646 */
1647 maxconn = 2 * remain / (5 + 2 * engine_fds);
1648 } else {
1649 /* maxsock = maxconn * 2 + maxconn * engine_fds.
1650 * = maxconn * (2 + engine_fds)
1651 */
1652 maxconn = remain / (2 + engine_fds);
1653 }
1654
Willy Tarreaudf23c0c2019-03-13 10:10:49 +01001655 return MAX(maxconn, DEFAULT_MAXCONN);
Willy Tarreauac350932019-03-01 15:43:14 +01001656}
1657
Willy Tarreaua409f302020-03-10 17:08:53 +01001658/* computes the estimated maxsock value for the given maxconn based on the
1659 * possibly set global.maxpipes and existing partial global.maxsock. It may
1660 * temporarily change global.maxconn for the time needed to propagate the
1661 * computations, and will reset it.
1662 */
1663static int compute_ideal_maxsock(int maxconn)
1664{
1665 int maxpipes = global.maxpipes;
1666 int maxsock = global.maxsock;
1667
1668
1669 if (!maxpipes) {
1670 int old_maxconn = global.maxconn;
1671
1672 global.maxconn = maxconn;
1673 maxpipes = compute_ideal_maxpipes();
1674 global.maxconn = old_maxconn;
1675 }
1676
1677 maxsock += maxconn * 2; /* each connection needs two sockets */
1678 maxsock += maxpipes * 2; /* each pipe needs two FDs */
1679 maxsock += global.nbthread; /* one epoll_fd/kqueue_fd per thread */
1680 maxsock += 2 * global.nbthread; /* one wake-up pipe (2 fd) per thread */
1681
1682 /* compute fd used by async engines */
1683 if (global.ssl_used_async_engines) {
1684 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1685
1686 maxsock += maxconn * sides * global.ssl_used_async_engines;
1687 }
1688 return maxsock;
1689}
1690
Willy Tarreau304e17e2020-03-10 17:54:54 +01001691/* Tests if it is possible to set the current process' RLIMIT_NOFILE to
1692 * <maxsock>, then sets it back to the previous value. Returns non-zero if the
1693 * value is accepted, non-zero otherwise. This is used to determine if an
1694 * automatic limit may be applied or not. When it is not, the caller knows that
1695 * the highest we can do is the rlim_max at boot. In case of error, we return
1696 * that the setting is possible, so that we defer the error processing to the
1697 * final stage in charge of enforcing this.
1698 */
1699static int check_if_maxsock_permitted(int maxsock)
1700{
1701 struct rlimit orig_limit, test_limit;
1702 int ret;
1703
1704 if (getrlimit(RLIMIT_NOFILE, &orig_limit) != 0)
1705 return 1;
1706
1707 /* don't go further if we can't even set to what we have */
1708 if (setrlimit(RLIMIT_NOFILE, &orig_limit) != 0)
1709 return 1;
1710
1711 test_limit.rlim_max = MAX(maxsock, orig_limit.rlim_max);
1712 test_limit.rlim_cur = test_limit.rlim_max;
1713 ret = setrlimit(RLIMIT_NOFILE, &test_limit);
1714
1715 if (setrlimit(RLIMIT_NOFILE, &orig_limit) != 0)
1716 return 1;
1717
1718 return ret == 0;
1719}
1720
1721
William Lallemand73b85e72017-06-01 17:38:51 +02001722/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001723 * This function initializes all the necessary variables. It only returns
1724 * if everything is OK. If something fails, it exits.
1725 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001726static void init(int argc, char **argv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001727{
Willy Tarreaubaaee002006-06-26 02:48:02 +02001728 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001729 char *tmp;
1730 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +02001731 int err_code = 0;
Maxime de Roucy0f503922016-05-13 23:52:55 +02001732 char *err_msg = NULL;
Willy Tarreau477ecd82010-01-03 21:12:30 +01001733 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +00001734 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +02001735 char *change_dir = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001736 struct proxy *px;
Willy Tarreaue6945732016-12-21 19:57:00 +01001737 struct post_check_fct *pcf;
Willy Tarreauac350932019-03-01 15:43:14 +01001738 int ideal_maxconn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001739
Christopher Faulete3a5e352017-10-24 13:53:54 +02001740 global.mode = MODE_STARTING;
William Lallemand00417412020-06-05 14:08:41 +02001741 old_argv = copy_argv(argc, argv);
1742 if (!old_argv) {
William Lallemanddf6c5a82020-06-04 17:40:23 +02001743 ha_alert("failed to copy argv.\n");
1744 exit(1);
1745 }
William Lallemand73b85e72017-06-01 17:38:51 +02001746
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001747 if (!init_trash_buffers(1)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001748 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet748919a2017-07-26 14:59:46 +02001749 exit(1);
1750 }
David du Colombier7af46052012-05-16 14:16:48 +02001751
Emeric Brun2b920a12010-09-23 18:30:22 +02001752 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
1753 * the string in case of truncation, and at least FreeBSD appears not to do
1754 * it.
1755 */
1756 memset(hostname, 0, sizeof(hostname));
1757 gethostname(hostname, sizeof(hostname) - 1);
Dragan Dosen4f014152020-06-18 16:56:47 +02001758
1759 if ((localpeer = strdup(hostname)) == NULL) {
1760 ha_alert("Cannot allocate memory for local peer.\n");
1761 exit(EXIT_FAILURE);
1762 }
William Lallemanddaf4cd22018-04-17 16:46:13 +02001763 setenv("HAPROXY_LOCALPEER", localpeer, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02001764
William Lallemand24c928c2020-01-14 17:58:18 +01001765 /* we were in mworker mode, we should restart in mworker mode */
1766 if (getenv("HAPROXY_MWORKER_REEXEC") != NULL)
1767 global.mode |= MODE_MWORKER;
1768
Willy Tarreaubaaee002006-06-26 02:48:02 +02001769 /*
1770 * Initialize the previously static variables.
1771 */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001772
Willy Tarreau173d9952018-01-26 21:48:23 +01001773 totalconn = actconn = listeners = stopping = 0;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001774 killed = 0;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001775
Willy Tarreaubaaee002006-06-26 02:48:02 +02001776
1777#ifdef HAPROXY_MEMMAX
Willy Tarreau70060452015-12-14 12:46:07 +01001778 global.rlimit_memmax_all = HAPROXY_MEMMAX;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001779#endif
1780
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001781 tzset();
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001782 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001783 start_date = now;
1784
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001785 ha_random_boot(argv);
Willy Tarreau84310e22014-02-14 11:59:04 +01001786
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001787 if (init_acl() != 0)
1788 exit(1);
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001789
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001790 /* Initialise lua. */
1791 hlua_init();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001792
Christopher Fauletff2613e2016-11-09 11:36:17 +01001793 /* Initialize process vars */
1794 vars_init(&global.vars, SCOPE_PROC);
1795
Willy Tarreau43b78992009-01-25 15:42:27 +01001796 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaue5733232019-05-22 19:24:06 +02001797#if defined(USE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001798 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001799#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001800#if defined(USE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001801 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001802#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001803#if defined(USE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +01001804 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001805#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001806#if defined(USE_EVPORTS)
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +00001807 global.tune.options |= GTUNE_USE_EVPORTS;
1808#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001809#if defined(USE_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001810 global.tune.options |= GTUNE_USE_SPLICE;
1811#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001812#if defined(USE_GETADDRINFO)
1813 global.tune.options |= GTUNE_USE_GAI;
1814#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001815#if defined(SO_REUSEPORT)
1816 global.tune.options |= GTUNE_USE_REUSEPORT;
1817#endif
Willy Tarreau76cc6992020-07-01 18:49:24 +02001818#ifdef USE_THREAD
1819 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
1820#endif
William Dauchya5194602020-03-28 19:29:58 +01001821 global.tune.options |= GTUNE_STRICT_LIMITS;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001822
1823 pid = getpid();
1824 progname = *argv;
1825 while ((tmp = strchr(progname, '/')) != NULL)
1826 progname = tmp + 1;
1827
Kevinm48936af2010-12-22 16:08:21 +00001828 /* the process name is used for the logs only */
Dragan Dosen43885c72015-10-01 13:18:13 +02001829 chunk_initstr(&global.log_tag, strdup(progname));
Kevinm48936af2010-12-22 16:08:21 +00001830
Willy Tarreaubaaee002006-06-26 02:48:02 +02001831 argc--; argv++;
1832 while (argc > 0) {
1833 char *flag;
1834
1835 if (**argv == '-') {
1836 flag = *argv+1;
1837
1838 /* 1 arg */
1839 if (*flag == 'v') {
1840 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +01001841 if (flag[1] == 'v') /* -vv */
1842 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001843 exit(0);
1844 }
Willy Tarreaue5733232019-05-22 19:24:06 +02001845#if defined(USE_EPOLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001846 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +01001847 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001848#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001849#if defined(USE_POLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001850 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +01001851 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001852#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001853#if defined(USE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001854 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +01001855 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001856#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001857#if defined(USE_EVPORTS)
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +00001858 else if (*flag == 'd' && flag[1] == 'v')
1859 global.tune.options &= ~GTUNE_USE_EVPORTS;
1860#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001861#if defined(USE_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001862 else if (*flag == 'd' && flag[1] == 'S')
1863 global.tune.options &= ~GTUNE_USE_SPLICE;
1864#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001865#if defined(USE_GETADDRINFO)
1866 else if (*flag == 'd' && flag[1] == 'G')
1867 global.tune.options &= ~GTUNE_USE_GAI;
1868#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001869#if defined(SO_REUSEPORT)
1870 else if (*flag == 'd' && flag[1] == 'R')
1871 global.tune.options &= ~GTUNE_USE_REUSEPORT;
1872#endif
Emeric Brun850efd52014-01-29 12:24:34 +01001873 else if (*flag == 'd' && flag[1] == 'V')
1874 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001875 else if (*flag == 'V')
1876 arg_mode |= MODE_VERBOSE;
1877 else if (*flag == 'd' && flag[1] == 'b')
1878 arg_mode |= MODE_FOREGROUND;
Willy Tarreau3eb10b82020-04-15 16:42:39 +02001879 else if (*flag == 'd' && flag[1] == 'W')
1880 arg_mode |= MODE_ZERO_WARNING;
Willy Tarreau6e064432012-05-08 15:40:42 +02001881 else if (*flag == 'd' && flag[1] == 'M')
1882 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreau3eed10e2016-11-07 21:03:16 +01001883 else if (*flag == 'd' && flag[1] == 'r')
1884 global.tune.options |= GTUNE_RESOLVE_DONTFAIL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001885 else if (*flag == 'd')
1886 arg_mode |= MODE_DEBUG;
1887 else if (*flag == 'c')
1888 arg_mode |= MODE_CHECK;
William Lallemand095ba4c2017-06-01 17:38:50 +02001889 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +02001890 arg_mode |= MODE_DAEMON;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001891 else if (*flag == 'W' && flag[1] == 's') {
Lukas Tribusf46bf952017-11-21 12:39:34 +01001892 arg_mode |= MODE_MWORKER | MODE_FOREGROUND;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001893#if defined(USE_SYSTEMD)
1894 global.tune.options |= GTUNE_USE_SYSTEMD;
1895#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001896 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 +01001897 usage(progname);
1898#endif
1899 }
William Lallemand095ba4c2017-06-01 17:38:50 +02001900 else if (*flag == 'W')
1901 arg_mode |= MODE_MWORKER;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001902 else if (*flag == 'q')
1903 arg_mode |= MODE_QUIET;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001904 else if (*flag == 'x') {
William Lallemand4f71d302020-06-04 23:41:29 +02001905 if (argc <= 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001906 ha_alert("Unix socket path expected with the -x flag\n\n");
William Lallemand45eff442017-06-19 15:57:55 +02001907 usage(progname);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001908 }
William Lallemand4fc09692017-06-19 16:37:19 +02001909 if (old_unixsocket)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001910 ha_warning("-x option already set, overwriting the value\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001911 old_unixsocket = argv[1];
William Lallemand4fc09692017-06-19 16:37:19 +02001912
Olivier Houchardf73629d2017-04-05 22:33:04 +02001913 argv++;
1914 argc--;
1915 }
William Lallemande7361152018-10-26 14:47:36 +02001916 else if (*flag == 'S') {
1917 struct wordlist *c;
1918
William Lallemanda6b32492020-06-04 23:49:20 +02001919 if (argc <= 1) {
William Lallemande7361152018-10-26 14:47:36 +02001920 ha_alert("Socket and optional bind parameters expected with the -S flag\n");
1921 usage(progname);
1922 }
1923 if ((c = malloc(sizeof(*c))) == NULL || (c->s = strdup(argv[1])) == NULL) {
1924 ha_alert("Cannot allocate memory\n");
1925 exit(EXIT_FAILURE);
1926 }
1927 LIST_ADD(&mworker_cli_conf, &c->list);
1928
1929 argv++;
1930 argc--;
1931 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001932 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1933 /* list of pids to finish ('f') or terminate ('t') */
1934
1935 if (flag[1] == 'f')
1936 oldpids_sig = SIGUSR1; /* finish then exit */
1937 else
1938 oldpids_sig = SIGTERM; /* terminate immediately */
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001939 while (argc > 1 && argv[1][0] != '-') {
Chris Lane236062f2018-02-05 23:15:44 +00001940 char * endptr = NULL;
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001941 oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int));
1942 if (!oldpids) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001943 ha_alert("Cannot allocate old pid : out of memory.\n");
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001944 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001945 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001946 argc--; argv++;
Chris Lane236062f2018-02-05 23:15:44 +00001947 errno = 0;
1948 oldpids[nb_oldpids] = strtol(*argv, &endptr, 10);
1949 if (errno) {
1950 ha_alert("-%2s option: failed to parse {%s}: %s\n",
1951 flag,
1952 *argv, strerror(errno));
1953 exit(1);
1954 } else if (endptr && strlen(endptr)) {
Willy Tarreau90807112020-02-25 08:16:33 +01001955 while (isspace((unsigned char)*endptr)) endptr++;
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001956 if (*endptr != 0) {
Chris Lane236062f2018-02-05 23:15:44 +00001957 ha_alert("-%2s option: some bytes unconsumed in PID list {%s}\n",
1958 flag, endptr);
1959 exit(1);
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001960 }
Chris Lane236062f2018-02-05 23:15:44 +00001961 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001962 if (oldpids[nb_oldpids] <= 0)
1963 usage(progname);
1964 nb_oldpids++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001965 }
1966 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001967 else if (flag[0] == '-' && flag[1] == 0) { /* "--" */
1968 /* now that's a cfgfile list */
1969 argv++; argc--;
1970 while (argc > 0) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02001971 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001972 ha_alert("Cannot load configuration file/directory %s : %s\n",
1973 *argv,
1974 err_msg);
Willy Tarreaua088d312015-10-08 11:58:48 +02001975 exit(1);
1976 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001977 argv++; argc--;
1978 }
1979 break;
1980 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001981 else { /* >=2 args */
1982 argv++; argc--;
1983 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001984 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001985
1986 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +02001987 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001988 case 'n' : cfg_maxconn = atol(*argv); break;
Willy Tarreau70060452015-12-14 12:46:07 +01001989 case 'm' : global.rlimit_memmax_all = atol(*argv); break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001990 case 'N' : cfg_maxpconn = atol(*argv); break;
William Lallemanddaf4cd22018-04-17 16:46:13 +02001991 case 'L' :
Dragan Dosen4f014152020-06-18 16:56:47 +02001992 free(localpeer);
1993 if ((localpeer = strdup(*argv)) == NULL) {
1994 ha_alert("Cannot allocate memory for local peer.\n");
1995 exit(EXIT_FAILURE);
1996 }
William Lallemanddaf4cd22018-04-17 16:46:13 +02001997 setenv("HAPROXY_LOCALPEER", localpeer, 1);
Dragan Dosen13cd54c2020-06-18 18:24:05 +02001998 global.localpeer_cmdline = 1;
William Lallemanddaf4cd22018-04-17 16:46:13 +02001999 break;
Willy Tarreau5d01a632009-06-22 16:02:30 +02002000 case 'f' :
Maxime de Roucy0f503922016-05-13 23:52:55 +02002001 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002002 ha_alert("Cannot load configuration file/directory %s : %s\n",
2003 *argv,
2004 err_msg);
Willy Tarreau5d01a632009-06-22 16:02:30 +02002005 exit(1);
2006 }
Willy Tarreau5d01a632009-06-22 16:02:30 +02002007 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002008 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02002009 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002010 }
2011 }
2012 }
2013 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02002014 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002015 argv++; argc--;
2016 }
2017
Christopher Faulete3a5e352017-10-24 13:53:54 +02002018 global.mode |= (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
Willy Tarreau3eb10b82020-04-15 16:42:39 +02002019 | MODE_QUIET | MODE_CHECK | MODE_DEBUG | MODE_ZERO_WARNING));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002020
William Lallemand944e6192018-11-21 15:48:31 +01002021 if (getenv("HAPROXY_MWORKER_WAIT_ONLY")) {
William Lallemandcb11fd22017-06-01 17:38:52 +02002022 unsetenv("HAPROXY_MWORKER_WAIT_ONLY");
William Lallemand944e6192018-11-21 15:48:31 +01002023 global.mode |= MODE_MWORKER_WAIT;
2024 global.mode &= ~MODE_MWORKER;
William Lallemandcb11fd22017-06-01 17:38:52 +02002025 }
2026
2027 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {
2028 atexit_flag = 1;
2029 atexit(reexec_on_failure);
2030 }
2031
Willy Tarreau576132e2011-09-10 19:26:56 +02002032 if (change_dir && chdir(change_dir) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002033 ha_alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
Willy Tarreau576132e2011-09-10 19:26:56 +02002034 exit(1);
2035 }
2036
Willy Tarreaubaaee002006-06-26 02:48:02 +02002037 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002038
2039 init_default_instance();
2040
William Lallemand944e6192018-11-21 15:48:31 +01002041 /* in wait mode, we don't try to read the configuration files */
2042 if (!(global.mode & MODE_MWORKER_WAIT)) {
William Lallemand7b302d82019-05-20 11:15:37 +02002043 struct buffer *trash = get_trash_chunk();
Willy Tarreauc4382422009-12-06 13:10:44 +01002044
William Lallemand944e6192018-11-21 15:48:31 +01002045 /* handle cfgfiles that are actually directories */
2046 cfgfiles_expand_directories();
2047
2048 if (LIST_ISEMPTY(&cfg_cfgfiles))
2049 usage(progname);
2050
2051
2052 list_for_each_entry(wl, &cfg_cfgfiles, list) {
2053 int ret;
2054
William Lallemand7b302d82019-05-20 11:15:37 +02002055 if (trash->data)
2056 chunk_appendf(trash, ";");
2057
2058 chunk_appendf(trash, "%s", wl->s);
2059
William Lallemand944e6192018-11-21 15:48:31 +01002060 ret = readcfgfile(wl->s);
2061 if (ret == -1) {
2062 ha_alert("Could not open configuration file %s : %s\n",
2063 wl->s, strerror(errno));
2064 exit(1);
2065 }
2066 if (ret & (ERR_ABORT|ERR_FATAL))
2067 ha_alert("Error(s) found in configuration file : %s\n", wl->s);
2068 err_code |= ret;
2069 if (err_code & ERR_ABORT)
2070 exit(1);
Willy Tarreauc4382422009-12-06 13:10:44 +01002071 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002072
William Lallemand944e6192018-11-21 15:48:31 +01002073 /* do not try to resolve arguments nor to spot inconsistencies when
2074 * the configuration contains fatal errors caused by files not found
2075 * or failed memory allocations.
2076 */
2077 if (err_code & (ERR_ABORT|ERR_FATAL)) {
2078 ha_alert("Fatal errors found in configuration.\n");
2079 exit(1);
2080 }
William Lallemand7b302d82019-05-20 11:15:37 +02002081 if (trash->data)
2082 setenv("HAPROXY_CFGFILES", trash->area, 1);
2083
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02002084 }
William Lallemandce83b4a2018-10-26 14:47:30 +02002085 if (global.mode & MODE_MWORKER) {
2086 int proc;
William Lallemand16dd1b32018-11-19 18:46:18 +01002087 struct mworker_proc *tmproc;
2088
William Lallemand482f9a92019-04-12 16:15:00 +02002089 setenv("HAPROXY_MWORKER", "1", 1);
2090
William Lallemand16dd1b32018-11-19 18:46:18 +01002091 if (getenv("HAPROXY_MWORKER_REEXEC") == NULL) {
2092
William Lallemandf3a86832019-04-01 11:29:58 +02002093 tmproc = calloc(1, sizeof(*tmproc));
William Lallemand16dd1b32018-11-19 18:46:18 +01002094 if (!tmproc) {
2095 ha_alert("Cannot allocate process structures.\n");
2096 exit(EXIT_FAILURE);
2097 }
William Lallemand8f7069a2019-04-12 16:09:23 +02002098 tmproc->options |= PROC_O_TYPE_MASTER; /* master */
William Lallemand16dd1b32018-11-19 18:46:18 +01002099 tmproc->reloads = 0;
2100 tmproc->relative_pid = 0;
2101 tmproc->pid = pid;
2102 tmproc->timestamp = start_date.tv_sec;
2103 tmproc->ipc_fd[0] = -1;
2104 tmproc->ipc_fd[1] = -1;
2105
2106 proc_self = tmproc;
2107
2108 LIST_ADDQ(&proc_list, &tmproc->list);
2109 }
William Lallemandce83b4a2018-10-26 14:47:30 +02002110
2111 for (proc = 0; proc < global.nbproc; proc++) {
William Lallemandce83b4a2018-10-26 14:47:30 +02002112
William Lallemandf3a86832019-04-01 11:29:58 +02002113 tmproc = calloc(1, sizeof(*tmproc));
William Lallemandce83b4a2018-10-26 14:47:30 +02002114 if (!tmproc) {
2115 ha_alert("Cannot allocate process structures.\n");
2116 exit(EXIT_FAILURE);
2117 }
2118
William Lallemand8f7069a2019-04-12 16:09:23 +02002119 tmproc->options |= PROC_O_TYPE_WORKER; /* worker */
William Lallemandce83b4a2018-10-26 14:47:30 +02002120 tmproc->pid = -1;
2121 tmproc->reloads = 0;
William Lallemande3683302018-11-19 18:46:17 +01002122 tmproc->timestamp = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02002123 tmproc->relative_pid = 1 + proc;
William Lallemand550db6d2018-11-06 17:37:12 +01002124 tmproc->ipc_fd[0] = -1;
2125 tmproc->ipc_fd[1] = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02002126
2127 if (mworker_cli_sockpair_new(tmproc, proc) < 0) {
2128 exit(EXIT_FAILURE);
2129 }
2130
2131 LIST_ADDQ(&proc_list, &tmproc->list);
2132 }
William Lallemand944e6192018-11-21 15:48:31 +01002133 }
2134 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
2135 struct wordlist *it, *c;
2136
William Lallemand1b663612018-10-26 14:47:33 +02002137 mworker_env_to_proc_list(); /* get the info of the children in the env */
William Lallemand8a022572018-10-26 14:47:35 +02002138
William Lallemande7361152018-10-26 14:47:36 +02002139
William Lallemand550db6d2018-11-06 17:37:12 +01002140 if (!LIST_ISEMPTY(&mworker_cli_conf)) {
William Lallemande7361152018-10-26 14:47:36 +02002141
William Lallemand550db6d2018-11-06 17:37:12 +01002142 if (mworker_cli_proxy_create() < 0) {
William Lallemande7361152018-10-26 14:47:36 +02002143 ha_alert("Can't create the master's CLI.\n");
2144 exit(EXIT_FAILURE);
2145 }
William Lallemande7361152018-10-26 14:47:36 +02002146
William Lallemand550db6d2018-11-06 17:37:12 +01002147 list_for_each_entry_safe(c, it, &mworker_cli_conf, list) {
2148
2149 if (mworker_cli_proxy_new_listener(c->s) < 0) {
2150 ha_alert("Can't create the master's CLI.\n");
2151 exit(EXIT_FAILURE);
2152 }
2153 LIST_DEL(&c->list);
2154 free(c->s);
2155 free(c);
2156 }
2157 }
William Lallemandce83b4a2018-10-26 14:47:30 +02002158 }
2159
Willy Tarreaubb925012009-07-23 13:36:36 +02002160 err_code |= check_config_validity();
Christopher Fauletc1692962019-08-12 09:51:07 +02002161 for (px = proxies_list; px; px = px->next) {
2162 struct server *srv;
2163 struct post_proxy_check_fct *ppcf;
2164 struct post_server_check_fct *pscf;
2165
2166 list_for_each_entry(pscf, &post_server_check_list, list) {
2167 for (srv = px->srv; srv; srv = srv->next)
2168 err_code |= pscf->fct(srv);
2169 }
2170 list_for_each_entry(ppcf, &post_proxy_check_list, list)
2171 err_code |= ppcf->fct(px);
2172 }
Willy Tarreaubb925012009-07-23 13:36:36 +02002173 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002174 ha_alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002175 exit(1);
2176 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002177
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002178 err_code |= pattern_finalize_config();
2179 if (err_code & (ERR_ABORT|ERR_FATAL)) {
2180 ha_alert("Failed to finalize pattern config.\n");
2181 exit(1);
2182 }
Willy Tarreau0f936722019-04-11 14:47:08 +02002183
Willy Tarreau70060452015-12-14 12:46:07 +01002184 /* recompute the amount of per-process memory depending on nbproc and
2185 * the shared SSL cache size (allowed to exist in all processes).
2186 */
2187 if (global.rlimit_memmax_all) {
2188#if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE)
2189 int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL;
2190
2191 global.rlimit_memmax =
2192 ((((int64_t)global.rlimit_memmax_all * 1048576LL) -
2193 ssl_cache_bytes) / global.nbproc +
2194 ssl_cache_bytes + 1048575LL) / 1048576LL;
2195#else
2196 global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc;
2197#endif
2198 }
2199
Willy Tarreaue5733232019-05-22 19:24:06 +02002200#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002201 err_code |= netns_init();
2202 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002203 ha_alert("Failed to initialize namespace support.\n");
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002204 exit(1);
2205 }
2206#endif
2207
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01002208 /* Apply server states */
2209 apply_server_state();
2210
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002211 for (px = proxies_list; px; px = px->next)
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01002212 srv_compute_all_admin_states(px);
2213
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01002214 /* Apply servers' configured address */
2215 err_code |= srv_init_addr();
2216 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002217 ha_alert("Failed to initialize server(s) addr.\n");
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01002218 exit(1);
2219 }
2220
Willy Tarreau3eb10b82020-04-15 16:42:39 +02002221 if (warned & WARN_ANY && global.mode & MODE_ZERO_WARNING) {
2222 ha_alert("Some warnings were found and 'zero-warning' is set. Aborting.\n");
2223 exit(1);
2224 }
2225
Willy Tarreaubaaee002006-06-26 02:48:02 +02002226 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +01002227 struct peers *pr;
2228 struct proxy *px;
2229
Willy Tarreaubebd2122020-04-15 16:06:11 +02002230 if (warned & WARN_ANY)
2231 qfprintf(stdout, "Warnings were found.\n");
2232
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002233 for (pr = cfg_peers; pr; pr = pr->next)
Willy Tarreau8b15ba12012-02-02 17:48:18 +01002234 if (pr->peers_fe)
2235 break;
2236
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002237 for (px = proxies_list; px; px = px->next)
Willy Tarreau4348fad2012-09-20 16:48:07 +02002238 if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners))
Willy Tarreau8b15ba12012-02-02 17:48:18 +01002239 break;
2240
2241 if (pr || px) {
2242 /* At least one peer or one listener has been found */
2243 qfprintf(stdout, "Configuration file is valid\n");
Tim Duesterhus0a3b43d2020-06-14 00:37:42 +02002244 deinit_and_exit(0);
Willy Tarreau8b15ba12012-02-02 17:48:18 +01002245 }
2246 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
2247 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002248 }
Willy Tarreaue9b26022011-08-01 20:57:55 +02002249
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002250 /* now we know the buffer size, we can initialize the channels and buffers */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002251 init_buffer();
Willy Tarreau8280d642009-09-23 23:37:52 +02002252
Willy Tarreaue6945732016-12-21 19:57:00 +01002253 list_for_each_entry(pcf, &post_check_list, list) {
2254 err_code |= pcf->fct();
2255 if (err_code & (ERR_ABORT|ERR_FATAL))
2256 exit(1);
2257 }
2258
Willy Tarreaubaaee002006-06-26 02:48:02 +02002259 if (cfg_maxconn > 0)
2260 global.maxconn = cfg_maxconn;
2261
Willy Tarreau8d687d82019-03-01 09:39:42 +01002262 if (global.stats_fe)
2263 global.maxsock += global.stats_fe->maxconn;
2264
2265 if (cfg_peers) {
2266 /* peers also need to bypass global maxconn */
2267 struct peers *p = cfg_peers;
2268
2269 for (p = cfg_peers; p; p = p->next)
2270 if (p->peers_fe)
2271 global.maxsock += p->peers_fe->maxconn;
2272 }
2273
Willy Tarreaubaaee002006-06-26 02:48:02 +02002274 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +02002275 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002276 global.pidfile = strdup(cfg_pidfile);
2277 }
2278
Willy Tarreaud0256482015-01-15 21:45:22 +01002279 /* Now we want to compute the maxconn and possibly maxsslconn values.
Willy Tarreauac350932019-03-01 15:43:14 +01002280 * It's a bit tricky. Maxconn defaults to the pre-computed value based
2281 * on rlim_fd_cur and the number of FDs in use due to the configuration,
2282 * and maxsslconn defaults to DEFAULT_MAXSSLCONN. On top of that we can
2283 * enforce a lower limit based on memmax.
Willy Tarreaud0256482015-01-15 21:45:22 +01002284 *
2285 * If memmax is set, then it depends on which values are set. If
2286 * maxsslconn is set, we use memmax to determine how many cleartext
2287 * connections may be added, and set maxconn to the sum of the two.
2288 * If maxconn is set and not maxsslconn, maxsslconn is computed from
2289 * the remaining amount of memory between memmax and the cleartext
2290 * connections. If neither are set, then it is considered that all
2291 * connections are SSL-capable, and maxconn is computed based on this,
2292 * then maxsslconn accordingly. We need to know if SSL is used on the
2293 * frontends, backends, or both, because when it's used on both sides,
2294 * we need twice the value for maxsslconn, but we only count the
2295 * handshake once since it is not performed on the two sides at the
2296 * same time (frontend-side is terminated before backend-side begins).
2297 * The SSL stack is supposed to have filled ssl_session_cost and
Willy Tarreau474b96a2015-01-28 19:03:21 +01002298 * ssl_handshake_cost during its initialization. In any case, if
2299 * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for
2300 * maxconn in order to protect the system.
Willy Tarreaud0256482015-01-15 21:45:22 +01002301 */
Willy Tarreauac350932019-03-01 15:43:14 +01002302 ideal_maxconn = compute_ideal_maxconn();
2303
Willy Tarreaud0256482015-01-15 21:45:22 +01002304 if (!global.rlimit_memmax) {
2305 if (global.maxconn == 0) {
Willy Tarreauac350932019-03-01 15:43:14 +01002306 global.maxconn = ideal_maxconn;
Willy Tarreaud0256482015-01-15 21:45:22 +01002307 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2308 fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn);
2309 }
2310 }
2311#ifdef USE_OPENSSL
2312 else if (!global.maxconn && !global.maxsslconn &&
2313 (global.ssl_used_frontend || global.ssl_used_backend)) {
2314 /* memmax is set, compute everything automatically. Here we want
2315 * to ensure that all SSL connections will be served. We take
2316 * care of the number of sides where SSL is used, and consider
2317 * the worst case : SSL used on both sides and doing a handshake
2318 * simultaneously. Note that we can't have more than maxconn
2319 * handshakes at a time by definition, so for the worst case of
2320 * two SSL conns per connection, we count a single handshake.
2321 */
2322 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2323 int64_t mem = global.rlimit_memmax * 1048576ULL;
Willy Tarreau304e17e2020-03-10 17:54:54 +01002324 int retried = 0;
Willy Tarreaud0256482015-01-15 21:45:22 +01002325
2326 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2327 mem -= global.maxzlibmem;
2328 mem = mem * MEM_USABLE_RATIO;
2329
Willy Tarreau304e17e2020-03-10 17:54:54 +01002330 /* Principle: we test once to set maxconn according to the free
2331 * memory. If it results in values the system rejects, we try a
2332 * second time by respecting rlim_fd_max. If it fails again, we
2333 * go back to the initial value and will let the final code
2334 * dealing with rlimit report the error. That's up to 3 attempts.
2335 */
2336 do {
2337 global.maxconn = mem /
2338 ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream
2339 sides * global.ssl_session_max_cost + // SSL buffers, one per side
2340 global.ssl_handshake_max_cost); // 1 handshake per connection max
Willy Tarreaud0256482015-01-15 21:45:22 +01002341
Willy Tarreau304e17e2020-03-10 17:54:54 +01002342 if (retried == 1)
2343 global.maxconn = MIN(global.maxconn, ideal_maxconn);
2344 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01002345#ifdef SYSTEM_MAXCONN
Willy Tarreau304e17e2020-03-10 17:54:54 +01002346 if (global.maxconn > SYSTEM_MAXCONN)
2347 global.maxconn = SYSTEM_MAXCONN;
Willy Tarreau474b96a2015-01-28 19:03:21 +01002348#endif /* SYSTEM_MAXCONN */
Willy Tarreau304e17e2020-03-10 17:54:54 +01002349 global.maxsslconn = sides * global.maxconn;
2350
2351 if (check_if_maxsock_permitted(compute_ideal_maxsock(global.maxconn)))
2352 break;
2353 } while (retried++ < 2);
2354
Willy Tarreaud0256482015-01-15 21:45:22 +01002355 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2356 fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n",
2357 global.maxconn, global.maxsslconn);
2358 }
2359 else if (!global.maxsslconn &&
2360 (global.ssl_used_frontend || global.ssl_used_backend)) {
2361 /* memmax and maxconn are known, compute maxsslconn automatically.
2362 * maxsslconn being forced, we don't know how many of it will be
2363 * on each side if both sides are being used. The worst case is
2364 * when all connections use only one SSL instance because
2365 * handshakes may be on two sides at the same time.
2366 */
2367 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2368 int64_t mem = global.rlimit_memmax * 1048576ULL;
2369 int64_t sslmem;
2370
2371 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2372 mem -= global.maxzlibmem;
2373 mem = mem * MEM_USABLE_RATIO;
2374
Willy Tarreau87b09662015-04-03 00:22:06 +02002375 sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01002376 global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost);
2377 global.maxsslconn = round_2dig(global.maxsslconn);
2378
2379 if (sslmem <= 0 || global.maxsslconn < sides) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002380 ha_alert("Cannot compute the automatic maxsslconn because global.maxconn is already too "
2381 "high for the global.memmax value (%d MB). The absolute maximum possible value "
2382 "without SSL is %d, but %d was found and SSL is in use.\n",
2383 global.rlimit_memmax,
2384 (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)),
2385 global.maxconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01002386 exit(1);
2387 }
2388
2389 if (global.maxsslconn > sides * global.maxconn)
2390 global.maxsslconn = sides * global.maxconn;
2391
2392 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2393 fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn);
2394 }
2395#endif
2396 else if (!global.maxconn) {
2397 /* memmax and maxsslconn are known/unused, compute maxconn automatically */
2398 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2399 int64_t mem = global.rlimit_memmax * 1048576ULL;
2400 int64_t clearmem;
Willy Tarreau304e17e2020-03-10 17:54:54 +01002401 int retried = 0;
Willy Tarreaud0256482015-01-15 21:45:22 +01002402
2403 if (global.ssl_used_frontend || global.ssl_used_backend)
2404 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2405
2406 mem -= global.maxzlibmem;
2407 mem = mem * MEM_USABLE_RATIO;
2408
2409 clearmem = mem;
2410 if (sides)
2411 clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn;
2412
Willy Tarreau304e17e2020-03-10 17:54:54 +01002413 /* Principle: we test once to set maxconn according to the free
2414 * memory. If it results in values the system rejects, we try a
2415 * second time by respecting rlim_fd_max. If it fails again, we
2416 * go back to the initial value and will let the final code
2417 * dealing with rlimit report the error. That's up to 3 attempts.
2418 */
2419 do {
2420 global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize);
2421 if (retried == 1)
2422 global.maxconn = MIN(global.maxconn, ideal_maxconn);
2423 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01002424#ifdef SYSTEM_MAXCONN
Willy Tarreau304e17e2020-03-10 17:54:54 +01002425 if (global.maxconn > SYSTEM_MAXCONN)
2426 global.maxconn = SYSTEM_MAXCONN;
Willy Tarreau474b96a2015-01-28 19:03:21 +01002427#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01002428
Willy Tarreau304e17e2020-03-10 17:54:54 +01002429 if (clearmem <= 0 || !global.maxconn) {
2430 ha_alert("Cannot compute the automatic maxconn because global.maxsslconn is already too "
2431 "high for the global.memmax value (%d MB). The absolute maximum possible value "
2432 "is %d, but %d was found.\n",
2433 global.rlimit_memmax,
Christopher Faulet767a84b2017-11-24 16:50:31 +01002434 (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)),
Willy Tarreau304e17e2020-03-10 17:54:54 +01002435 global.maxsslconn);
2436 exit(1);
2437 }
2438
2439 if (check_if_maxsock_permitted(compute_ideal_maxsock(global.maxconn)))
2440 break;
2441 } while (retried++ < 2);
Willy Tarreaud0256482015-01-15 21:45:22 +01002442
2443 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
2444 if (sides && global.maxsslconn > sides * global.maxconn) {
2445 fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn "
2446 "to be limited to %d. Better reduce global.maxsslconn to get more "
2447 "room for extra connections.\n", global.maxsslconn, global.maxconn);
2448 }
2449 fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn);
2450 }
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002451 }
2452
Willy Tarreaua409f302020-03-10 17:08:53 +01002453 global.maxsock = compute_ideal_maxsock(global.maxconn);
2454 global.hardmaxconn = global.maxconn;
Willy Tarreaua4818db2020-06-19 16:20:59 +02002455 if (!global.maxpipes)
2456 global.maxpipes = compute_ideal_maxpipes();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002457
Olivier Houchard88698d92019-04-16 19:07:22 +02002458 /* update connection pool thresholds */
2459 global.tune.pool_low_count = ((long long)global.maxsock * global.tune.pool_low_ratio + 99) / 100;
2460 global.tune.pool_high_count = ((long long)global.maxsock * global.tune.pool_high_ratio + 99) / 100;
2461
Willy Tarreauc8d5b952019-02-27 17:25:52 +01002462 proxy_adjust_all_maxconn();
2463
Willy Tarreau1db37712007-06-03 17:16:49 +02002464 if (global.tune.maxpollevents <= 0)
2465 global.tune.maxpollevents = MAX_POLL_EVENTS;
2466
Olivier Houchard1599b802018-05-24 18:59:04 +02002467 if (global.tune.runqueue_depth <= 0)
2468 global.tune.runqueue_depth = RUNQUEUE_DEPTH;
2469
Willy Tarreau6f4a82c2009-03-21 20:43:57 +01002470 if (global.tune.recv_enough == 0)
2471 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
2472
Willy Tarreau27a674e2009-08-17 07:23:33 +02002473 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
2474 global.tune.maxrewrite = global.tune.bufsize / 2;
2475
Willy Tarreaubaaee002006-06-26 02:48:02 +02002476 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
2477 /* command line debug mode inhibits configuration mode */
William Lallemand095ba4c2017-06-01 17:38:50 +02002478 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002479 global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND));
2480 }
2481
William Lallemand095ba4c2017-06-01 17:38:50 +02002482 if (arg_mode & MODE_DAEMON) {
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002483 /* command line daemon mode inhibits foreground and debug modes mode */
2484 global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND);
William Lallemand095ba4c2017-06-01 17:38:50 +02002485 global.mode |= arg_mode & MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002486 }
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002487
2488 global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002489
William Lallemand095ba4c2017-06-01 17:38:50 +02002490 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002491 ha_warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
William Lallemand095ba4c2017-06-01 17:38:50 +02002492 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002493 }
2494
William Lallemand095ba4c2017-06-01 17:38:50 +02002495 if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002496 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
Christopher Faulet767a84b2017-11-24 16:50:31 +01002497 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 +02002498 global.nbproc = 1;
2499 }
2500
2501 if (global.nbproc < 1)
2502 global.nbproc = 1;
2503
Christopher Fauletbe0faa22017-08-29 15:37:10 +02002504 if (global.nbthread < 1)
2505 global.nbthread = 1;
2506
Christopher Faulet3ef26392017-08-29 16:46:57 +02002507 /* Realloc trash buffers because global.tune.bufsize may have changed */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002508 if (!init_trash_buffers(0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002509 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet3ef26392017-08-29 16:46:57 +02002510 exit(1);
2511 }
2512
Christopher Faulet96d44832017-11-14 22:02:30 +01002513 if (!init_log_buffers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002514 ha_alert("failed to initialize log buffers.\n");
Christopher Faulet96d44832017-11-14 22:02:30 +01002515 exit(1);
2516 }
2517
Willy Tarreauef1d1f82007-04-16 00:25:25 +02002518 /*
2519 * Note: we could register external pollers here.
2520 * Built-in pollers have been registered before main().
2521 */
Willy Tarreau4f60f162007-04-08 16:39:58 +02002522
Willy Tarreau43b78992009-01-25 15:42:27 +01002523 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +02002524 disable_poller("kqueue");
2525
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +00002526 if (!(global.tune.options & GTUNE_USE_EVPORTS))
2527 disable_poller("evports");
2528
Willy Tarreau43b78992009-01-25 15:42:27 +01002529 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002530 disable_poller("epoll");
2531
Willy Tarreau43b78992009-01-25 15:42:27 +01002532 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002533 disable_poller("poll");
2534
Willy Tarreau43b78992009-01-25 15:42:27 +01002535 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002536 disable_poller("select");
2537
2538 /* Note: we could disable any poller by name here */
2539
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002540 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +02002541 list_pollers(stderr);
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002542 fprintf(stderr, "\n");
2543 list_filters(stderr);
2544 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002545
Willy Tarreau4f60f162007-04-08 16:39:58 +02002546 if (!init_pollers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002547 ha_alert("No polling mechanism available.\n"
2548 " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n"
2549 " is too low on this platform to support maxconn and the number of listeners\n"
2550 " and servers. You should rebuild haproxy specifying your system using TARGET=\n"
2551 " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n"
2552 " global maxconn setting to accommodate the system's limitation. For reference,\n"
2553 " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n"
2554 " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n"
2555 " check build settings using 'haproxy -vv'.\n\n",
2556 FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002557 exit(1);
2558 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002559 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
2560 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002561 }
2562
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002563 if (!global.node)
2564 global.node = strdup(hostname);
2565
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01002566 if (!hlua_post_init())
2567 exit(1);
Thomas Holmes6abded42015-05-12 16:23:58 +01002568
Maxime de Roucy0f503922016-05-13 23:52:55 +02002569 free(err_msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002570}
2571
Simon Horman6fb82592011-07-15 13:14:11 +09002572static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +09002573{
Simon Hormanac821422011-07-15 13:14:09 +09002574 struct acl_term_suite *suite, *suiteb;
2575 struct acl_term *term, *termb;
2576
Simon Horman6fb82592011-07-15 13:14:11 +09002577 if (!cond)
2578 return;
2579
2580 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
2581 list_for_each_entry_safe(term, termb, &suite->terms, list) {
2582 LIST_DEL(&term->list);
2583 free(term);
Simon Hormanac821422011-07-15 13:14:09 +09002584 }
Simon Horman6fb82592011-07-15 13:14:11 +09002585 LIST_DEL(&suite->list);
2586 free(suite);
2587 }
2588
2589 free(cond);
2590}
2591
Christopher Fauletcb550132019-12-17 11:25:46 +01002592static void deinit_act_rules(struct list *rules)
Simon Horman6fb82592011-07-15 13:14:11 +09002593{
Christopher Fauletcb550132019-12-17 11:25:46 +01002594 struct act_rule *rule, *ruleb;
Simon Horman6fb82592011-07-15 13:14:11 +09002595
Christopher Fauletcb550132019-12-17 11:25:46 +01002596 list_for_each_entry_safe(rule, ruleb, rules, list) {
2597 LIST_DEL(&rule->list);
2598 deinit_acl_cond(rule->cond);
Christopher Faulet58b35642019-12-17 11:48:42 +01002599 if (rule->release_ptr)
2600 rule->release_ptr(rule);
Christopher Fauletcb550132019-12-17 11:25:46 +01002601 free(rule);
Simon Hormanac821422011-07-15 13:14:09 +09002602 }
2603}
2604
Simon Horman6fb82592011-07-15 13:14:11 +09002605static void deinit_stick_rules(struct list *rules)
2606{
2607 struct sticking_rule *rule, *ruleb;
2608
2609 list_for_each_entry_safe(rule, ruleb, rules, list) {
2610 LIST_DEL(&rule->list);
2611 deinit_acl_cond(rule->cond);
Christopher Faulet476e5d02016-10-26 11:34:47 +02002612 release_sample_expr(rule->expr);
Simon Horman6fb82592011-07-15 13:14:11 +09002613 free(rule);
2614 }
2615}
2616
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002617void deinit(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002618{
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002619 struct proxy *p = proxies_list, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002620 struct cap_hdr *h,*h_next;
2621 struct server *s,*s_next;
2622 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002623 struct acl_cond *cond, *condb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002624 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002625 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002626 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002627 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002628 struct wordlist *wl, *wlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002629 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +02002630 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +01002631 struct logformat_node *lf, *lfb;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002632 struct bind_conf *bind_conf, *bind_back;
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002633 struct build_opts_str *bol, *bolb;
Tim Duesterhusfdf904a2020-07-04 11:49:48 +02002634 struct post_deinit_fct *pdf, *pdfb;
Tim Duesterhus17e363f2020-07-04 11:49:47 +02002635 struct proxy_deinit_fct *pxdf, *pxdfb;
Tim Duesterhus0837eb12020-07-04 11:49:49 +02002636 struct server_deinit_fct *srvdf, *srvdfb;
Tim Duesterhus34bef072020-07-04 11:49:50 +02002637 struct post_server_check_fct *pscf, *pscfb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002638
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002639 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002640 while (p) {
Willy Tarreau8113a5d2012-10-04 08:01:43 +02002641 free(p->conf.file);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002642 free(p->id);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002643 free(p->cookie_name);
2644 free(p->cookie_domain);
Christopher Faulet2f533902020-01-21 11:06:48 +01002645 free(p->cookie_attrs);
Willy Tarreau4c03d1c2019-01-14 15:23:54 +01002646 free(p->lbprm.arg_str);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002647 free(p->capture_name);
2648 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +09002649 free(p->rdp_cookie_name);
William Lallemandefc5a9d2020-08-07 14:48:37 +02002650 free(p->invalid_rep);
2651 free(p->invalid_req);
Willy Tarreau62a61232013-04-12 18:13:46 +02002652 if (p->conf.logformat_string != default_http_log_format &&
2653 p->conf.logformat_string != default_tcp_log_format &&
2654 p->conf.logformat_string != clf_http_log_format)
2655 free(p->conf.logformat_string);
Willy Tarreau196729e2012-05-31 19:30:26 +02002656
Willy Tarreau62a61232013-04-12 18:13:46 +02002657 free(p->conf.lfs_file);
2658 free(p->conf.uniqueid_format_string);
Tim Duesterhus18c63592020-07-04 11:49:42 +02002659 istfree(&p->header_unique_id);
Willy Tarreau62a61232013-04-12 18:13:46 +02002660 free(p->conf.uif_file);
Willy Tarreau0cac26c2019-01-14 16:55:42 +01002661 if ((p->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_MAP)
2662 free(p->lbprm.map.srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002663
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002664 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
2665 free(p->conf.logformat_sd_string);
2666 free(p->conf.lfsd_file);
2667
Willy Tarreaub80c2302007-11-30 20:51:32 +01002668 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
2669 LIST_DEL(&cond->list);
2670 prune_acl_cond(cond);
2671 free(cond);
2672 }
2673
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002674 /* build a list of unique uri_auths */
2675 if (!ua)
2676 ua = p->uri_auth;
2677 else {
2678 /* check if p->uri_auth is unique */
2679 for (uap = ua; uap; uap=uap->next)
2680 if (uap == p->uri_auth)
2681 break;
2682
Willy Tarreauaccc4e12008-06-24 11:14:45 +02002683 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002684 /* add it, if it is */
2685 p->uri_auth->next = ua;
2686 ua = p->uri_auth;
2687 }
2688 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002689
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002690 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
2691 LIST_DEL(&acl->list);
2692 prune_acl(acl);
2693 free(acl);
2694 }
2695
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002696 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
2697 LIST_DEL(&srule->list);
2698 prune_acl_cond(srule->cond);
Tim Duesterhus826cf072020-07-04 11:49:44 +02002699 list_for_each_entry_safe(lf, lfb, &srule->expr, list) {
2700 LIST_DEL(&lf->list);
2701 release_sample_expr(lf->expr);
2702 free(lf->arg);
2703 free(lf);
2704 }
Tim Duesterhus6fb74a12020-07-04 11:49:43 +02002705 free(srule->file);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002706 free(srule->cond);
2707 free(srule);
2708 }
2709
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002710 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
2711 LIST_DEL(&rule->list);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002712 if (rule->cond) {
2713 prune_acl_cond(rule->cond);
2714 free(rule->cond);
2715 }
Dragan Dosen2a7c20f2019-04-30 00:38:36 +02002716 free(rule->file);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002717 free(rule);
2718 }
2719
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002720 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
2721 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +01002722 if (rdr->cond) {
2723 prune_acl_cond(rdr->cond);
2724 free(rdr->cond);
2725 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002726 free(rdr->rdr_str);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002727 list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) {
2728 LIST_DEL(&lf->list);
2729 free(lf);
2730 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002731 free(rdr);
2732 }
2733
William Lallemand0f99e342011-10-12 17:50:54 +02002734 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
2735 LIST_DEL(&log->list);
2736 free(log);
2737 }
2738
William Lallemand723b73a2012-02-08 16:37:49 +01002739 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
2740 LIST_DEL(&lf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +02002741 release_sample_expr(lf->expr);
2742 free(lf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +01002743 free(lf);
2744 }
2745
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002746 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
2747 LIST_DEL(&lf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +02002748 release_sample_expr(lf->expr);
2749 free(lf->arg);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002750 free(lf);
2751 }
2752
Tim Duesterhus79765782020-07-04 11:49:40 +02002753 list_for_each_entry_safe(lf, lfb, &p->format_unique_id, list) {
2754 LIST_DEL(&lf->list);
2755 release_sample_expr(lf->expr);
2756 free(lf->arg);
2757 free(lf);
2758 }
2759
Christopher Fauletcb550132019-12-17 11:25:46 +01002760 deinit_act_rules(&p->tcp_req.inspect_rules);
2761 deinit_act_rules(&p->tcp_rep.inspect_rules);
2762 deinit_act_rules(&p->tcp_req.l4_rules);
2763 deinit_act_rules(&p->tcp_req.l5_rules);
2764 deinit_act_rules(&p->http_req_rules);
2765 deinit_act_rules(&p->http_res_rules);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002766 deinit_act_rules(&p->http_after_res_rules);
Simon Hormanac821422011-07-15 13:14:09 +09002767
Simon Horman6fb82592011-07-15 13:14:11 +09002768 deinit_stick_rules(&p->storersp_rules);
2769 deinit_stick_rules(&p->sticking_rules);
2770
Willy Tarreaubaaee002006-06-26 02:48:02 +02002771 h = p->req_cap;
2772 while (h) {
2773 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002774 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002775 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002776 free(h);
2777 h = h_next;
2778 }/* end while(h) */
2779
2780 h = p->rsp_cap;
2781 while (h) {
2782 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002783 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002784 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002785 free(h);
2786 h = h_next;
2787 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002788
Willy Tarreaubaaee002006-06-26 02:48:02 +02002789 s = p->srv;
2790 while (s) {
2791 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002792
Dragan Dosen026ef572019-04-30 00:56:20 +02002793
Willy Tarreauf6562792019-05-07 19:05:35 +02002794 task_destroy(s->warmup);
Willy Tarreau2e993902011-10-31 11:53:20 +01002795
Willy Tarreaua534fea2008-08-03 12:19:50 +02002796 free(s->id);
2797 free(s->cookie);
Tim Duesterhuscb8f13c2020-07-04 11:49:41 +02002798 free(s->hostname);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002799 free(s->hostname_dn);
Sárközi, László34c01792014-09-05 10:08:23 +02002800 free((char*)s->conf.file);
Olivier Houchard7fc3be72018-11-22 18:50:54 +01002801 free(s->idle_conns);
Olivier Houchard7fc3be72018-11-22 18:50:54 +01002802 free(s->safe_conns);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002803 free(s->available_conns);
Olivier Houchardf1314812019-02-18 16:41:17 +01002804 free(s->curr_idle_thr);
Tim Duesterhuscb8f13c2020-07-04 11:49:41 +02002805 free(s->resolvers_id);
Willy Tarreau17d45382016-12-22 21:16:08 +01002806
Christopher Fauletf61f33a2020-03-27 18:55:49 +01002807 if (s->use_ssl == 1 || s->check.use_ssl == 1 || (s->proxy->options & PR_O_TCPCHK_SSL)) {
Willy Tarreau17d45382016-12-22 21:16:08 +01002808 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2809 xprt_get(XPRT_SSL)->destroy_srv(s);
2810 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002811 HA_SPIN_DESTROY(&s->lock);
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +02002812
2813 list_for_each_entry(srvdf, &server_deinit_list, list)
2814 srvdf->fct(s);
2815
Willy Tarreaubaaee002006-06-26 02:48:02 +02002816 free(s);
2817 s = s_next;
2818 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002819
Willy Tarreau4348fad2012-09-20 16:48:07 +02002820 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002821 /*
2822 * Zombie proxy, the listener just pretend to be up
2823 * because they still hold an opened fd.
2824 * Close it and give the listener its real state.
2825 */
2826 if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
2827 close(l->fd);
2828 l->state = LI_INIT;
2829 }
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02002830 unbind_listener(l);
2831 delete_listener(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002832 LIST_DEL(&l->by_fe);
2833 LIST_DEL(&l->by_bind);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002834 free(l->name);
2835 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002836 free(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002837 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002838
Willy Tarreau4348fad2012-09-20 16:48:07 +02002839 /* Release unused SSL configs. */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002840 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01002841 if (bind_conf->xprt->destroy_bind_conf)
2842 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002843 free(bind_conf->file);
2844 free(bind_conf->arg);
2845 LIST_DEL(&bind_conf->by_fe);
2846 free(bind_conf);
2847 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02002848
Christopher Fauletd7c91962015-04-30 11:48:27 +02002849 flt_deinit(p);
2850
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +02002851 list_for_each_entry(pxdf, &proxy_deinit_list, list)
2852 pxdf->fct(p);
2853
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002854 free(p->desc);
2855 free(p->fwdfor_hdr_name);
2856
Olivier Houchard3f795f72019-04-17 22:51:06 +02002857 task_destroy(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002858
Willy Tarreaubafbe012017-11-24 17:34:44 +01002859 pool_destroy(p->req_cap_pool);
2860 pool_destroy(p->rsp_cap_pool);
Dragan Dosen7d61a332019-05-07 14:16:18 +02002861 if (p->table)
2862 pool_destroy(p->table->pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002863
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002864 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002865 p = p->next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002866 HA_SPIN_DESTROY(&p0->lbprm.lock);
2867 HA_SPIN_DESTROY(&p0->lock);
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002868 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002869 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02002870
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002871 while (ua) {
2872 uap = ua;
2873 ua = ua->next;
2874
Willy Tarreaua534fea2008-08-03 12:19:50 +02002875 free(uap->uri_prefix);
2876 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002877 free(uap->node);
2878 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002879
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002880 userlist_free(uap->userlist);
Christopher Fauletcb550132019-12-17 11:25:46 +01002881 deinit_act_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002882
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002883 free(uap);
2884 }
2885
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002886 userlist_free(userlist);
2887
David Carlier834cb2e2015-09-25 12:02:25 +01002888 cfg_unregister_sections();
2889
Christopher Faulet0132d062017-07-26 15:33:35 +02002890 deinit_log_buffers();
David Carlier834cb2e2015-09-25 12:02:25 +01002891
Willy Tarreaudd815982007-10-16 12:25:14 +02002892 protocol_unbind_all();
2893
Willy Tarreau05554e62016-12-21 20:46:26 +01002894 list_for_each_entry(pdf, &post_deinit_list, list)
2895 pdf->fct();
2896
Joe Williamsdf5b38f2010-12-29 17:05:48 +01002897 free(global.log_send_hostname); global.log_send_hostname = NULL;
Dragan Dosen43885c72015-10-01 13:18:13 +02002898 chunk_destroy(&global.log_tag);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002899 free(global.chroot); global.chroot = NULL;
2900 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002901 free(global.node); global.node = NULL;
2902 free(global.desc); global.desc = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002903 free(oldpids); oldpids = NULL;
Dragan Dosen4f014152020-06-18 16:56:47 +02002904 free(localpeer); localpeer = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +02002905 task_destroy(idle_conn_task);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01002906 idle_conn_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002907
William Lallemand0f99e342011-10-12 17:50:54 +02002908 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
2909 LIST_DEL(&log->list);
2910 free(log);
2911 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01002912 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02002913 free(wl->s);
Willy Tarreau477ecd82010-01-03 21:12:30 +01002914 LIST_DEL(&wl->list);
2915 free(wl);
2916 }
2917
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002918 list_for_each_entry_safe(bol, bolb, &build_opts_list, list) {
2919 if (bol->must_free)
2920 free((void *)bol->str);
2921 LIST_DEL(&bol->list);
2922 free(bol);
2923 }
2924
Tim Duesterhus17e363f2020-07-04 11:49:47 +02002925 list_for_each_entry_safe(pxdf, pxdfb, &proxy_deinit_list, list) {
2926 LIST_DEL(&pxdf->list);
2927 free(pxdf);
2928 }
2929
Tim Duesterhusfdf904a2020-07-04 11:49:48 +02002930 list_for_each_entry_safe(pdf, pdfb, &post_deinit_list, list) {
2931 LIST_DEL(&pdf->list);
2932 free(pdf);
2933 }
2934
Tim Duesterhus0837eb12020-07-04 11:49:49 +02002935 list_for_each_entry_safe(srvdf, srvdfb, &server_deinit_list, list) {
2936 LIST_DEL(&srvdf->list);
2937 free(srvdf);
2938 }
2939
Tim Duesterhus34bef072020-07-04 11:49:50 +02002940 list_for_each_entry_safe(pscf, pscfb, &post_server_check_list, list) {
2941 LIST_DEL(&pscf->list);
2942 free(pscf);
2943 }
2944
Christopher Fauletff2613e2016-11-09 11:36:17 +01002945 vars_prune(&global.vars, NULL, NULL);
Willy Tarreau2455ceb2018-11-26 15:57:34 +01002946 pool_destroy_all();
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002947 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002948} /* end deinit() */
2949
Willy Tarreauf3ca5a02020-06-15 18:43:46 +02002950__attribute__((noreturn)) void deinit_and_exit(int status)
Tim Duesterhus26540552020-06-14 00:37:41 +02002951{
2952 deinit();
2953 exit(status);
2954}
William Lallemand72160322018-11-06 17:37:16 +01002955
Willy Tarreau918ff602011-07-25 16:33:49 +02002956/* Runs the polling loop */
Willy Tarreau3ebd55e2020-03-03 14:59:56 +01002957void run_poll_loop()
Willy Tarreau4f60f162007-04-08 16:39:58 +02002958{
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002959 int next, wake;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002960
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002961 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002962 while (1) {
Willy Tarreauc49ba522019-12-11 08:12:23 +01002963 wake_expired_tasks();
2964
William Lallemand1aab50b2018-06-07 09:46:01 +02002965 /* check if we caught some signals and process them in the
2966 first thread */
Willy Tarreaua7ad4ae2020-06-19 12:06:34 +02002967 if (signal_queue_len && tid == 0) {
2968 activity[tid].wake_signal++;
William Lallemand1aab50b2018-06-07 09:46:01 +02002969 signal_process_queue();
Willy Tarreaua7ad4ae2020-06-19 12:06:34 +02002970 }
2971
2972 /* Process a few tasks */
2973 process_runnable_tasks();
Willy Tarreau29857942009-05-10 09:01:21 +02002974
Willy Tarreau7067b3a2019-06-02 11:11:29 +02002975 /* also stop if we failed to cleanly stop all tasks */
2976 if (killed > 1)
2977 break;
2978
Willy Tarreau10146c92015-04-13 20:44:19 +02002979 /* expire immediately if events are pending */
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002980 wake = 1;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002981 if (thread_has_tasks())
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002982 activity[tid].wake_tasks++;
Olivier Houchard79321b92018-07-26 17:55:11 +02002983 else {
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002984 _HA_ATOMIC_OR(&sleeping_thread_mask, tid_bit);
2985 __ha_barrier_atomic_store();
Willy Tarreau95abd5b2020-03-23 09:33:32 +01002986 if (thread_has_tasks()) {
Olivier Houchard79321b92018-07-26 17:55:11 +02002987 activity[tid].wake_tasks++;
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002988 _HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
Olivier Houchard79321b92018-07-26 17:55:11 +02002989 } else
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002990 wake = 0;
Olivier Houchard79321b92018-07-26 17:55:11 +02002991 }
Willy Tarreau10146c92015-04-13 20:44:19 +02002992
Willy Tarreau4f46a352020-03-23 09:27:28 +01002993 if (!wake) {
Willy Tarreaud7a6b2f2020-05-13 13:51:01 +02002994 int i;
2995
2996 if (stopping) {
Willy Tarreaud6455742020-05-13 14:30:25 +02002997 if (_HA_ATOMIC_OR(&stopping_thread_mask, tid_bit) == tid_bit) {
2998 /* notify all threads that stopping was just set */
2999 for (i = 0; i < global.nbthread; i++)
Willy Tarreau369a2ef2020-06-29 19:23:19 +02003000 if (((all_threads_mask & ~stopping_thread_mask) >> i) & 1)
Willy Tarreaud6455742020-05-13 14:30:25 +02003001 wake_thread(i);
3002 }
Willy Tarreaud7a6b2f2020-05-13 13:51:01 +02003003 }
Willy Tarreau4f46a352020-03-23 09:27:28 +01003004
3005 /* stop when there's nothing left to do */
3006 if ((jobs - unstoppable_jobs) == 0 &&
Willy Tarreaud7a6b2f2020-05-13 13:51:01 +02003007 (stopping_thread_mask & all_threads_mask) == all_threads_mask) {
3008 /* wake all threads waiting on jobs==0 */
3009 for (i = 0; i < global.nbthread; i++)
3010 if (((all_threads_mask & ~tid_bit) >> i) & 1)
3011 wake_thread(i);
Willy Tarreau4f46a352020-03-23 09:27:28 +01003012 break;
Willy Tarreaud7a6b2f2020-05-13 13:51:01 +02003013 }
Willy Tarreau4f46a352020-03-23 09:27:28 +01003014 }
3015
Willy Tarreauc49ba522019-12-11 08:12:23 +01003016 /* If we have to sleep, measure how long */
3017 next = wake ? TICK_ETERNITY : next_timer_expiry();
3018
Willy Tarreau58b458d2008-06-29 22:40:23 +02003019 /* The poller will ensure it returns around <next> */
Willy Tarreau2ae84e42019-05-28 16:44:05 +02003020 cur_poller.poll(&cur_poller, next, wake);
Emeric Brun64cc49c2017-10-03 14:46:45 +02003021
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003022 activity[tid].loops++;
Willy Tarreau4f60f162007-04-08 16:39:58 +02003023 }
3024}
3025
Christopher Faulet1d17c102017-08-29 15:38:48 +02003026static void *run_thread_poll_loop(void *data)
3027{
Willy Tarreau082b6282019-05-22 14:42:12 +02003028 struct per_thread_alloc_fct *ptaf;
Christopher Faulet1d17c102017-08-29 15:38:48 +02003029 struct per_thread_init_fct *ptif;
3030 struct per_thread_deinit_fct *ptdf;
Willy Tarreau082b6282019-05-22 14:42:12 +02003031 struct per_thread_free_fct *ptff;
Willy Tarreau34a150c2019-06-11 09:16:41 +02003032 static int init_left = 0;
Willy Tarreauaf613e82020-06-05 08:40:51 +02003033 __decl_thread(static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER);
3034 __decl_thread(static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER);
Christopher Faulet1d17c102017-08-29 15:38:48 +02003035
Willy Tarreaub4f7cc32019-05-03 09:27:30 +02003036 ha_set_tid((unsigned long)data);
Willy Tarreaud022e9c2019-09-24 08:25:15 +02003037 sched = &task_per_thread[tid];
Willy Tarreau91e6df02019-05-03 17:21:18 +02003038
Willy Tarreauf6178242019-05-21 19:46:58 +02003039#if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
Willy Tarreau91e6df02019-05-03 17:21:18 +02003040#ifdef USE_THREAD
Willy Tarreau8323a372019-05-20 18:57:53 +02003041 pthread_getcpuclockid(pthread_self(), &ti->clock_id);
Willy Tarreau624dcbf2019-05-20 20:23:06 +02003042#else
Willy Tarreau8323a372019-05-20 18:57:53 +02003043 ti->clock_id = CLOCK_THREAD_CPUTIME_ID;
Willy Tarreau91e6df02019-05-03 17:21:18 +02003044#endif
Willy Tarreau663fda42019-05-21 15:14:08 +02003045#endif
Willy Tarreau6ec902a2019-06-07 14:41:11 +02003046 /* Now, initialize one thread init at a time. This is better since
3047 * some init code is a bit tricky and may release global resources
3048 * after reallocating them locally. This will also ensure there is
3049 * no race on file descriptors allocation.
3050 */
Willy Tarreau34a150c2019-06-11 09:16:41 +02003051#ifdef USE_THREAD
3052 pthread_mutex_lock(&init_mutex);
3053#endif
3054 /* The first thread must set the number of threads left */
3055 if (!init_left)
3056 init_left = global.nbthread;
3057 init_left--;
Willy Tarreau91e6df02019-05-03 17:21:18 +02003058
Christopher Faulet1d17c102017-08-29 15:38:48 +02003059 tv_update_date(-1,-1);
3060
Willy Tarreau082b6282019-05-22 14:42:12 +02003061 /* per-thread alloc calls performed here are not allowed to snoop on
3062 * other threads, so they are free to initialize at their own rhythm
3063 * as long as they act as if they were alone. None of them may rely
3064 * on resources initialized by the other ones.
3065 */
3066 list_for_each_entry(ptaf, &per_thread_alloc_list, list) {
3067 if (!ptaf->fct()) {
3068 ha_alert("failed to allocate resources for thread %u.\n", tid);
3069 exit(1);
3070 }
3071 }
3072
Willy Tarreau3078e9f2019-05-20 10:50:43 +02003073 /* per-thread init calls performed here are not allowed to snoop on
3074 * other threads, so they are free to initialize at their own rhythm
3075 * as long as they act as if they were alone.
3076 */
Christopher Faulet1d17c102017-08-29 15:38:48 +02003077 list_for_each_entry(ptif, &per_thread_init_list, list) {
3078 if (!ptif->fct()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003079 ha_alert("failed to initialize thread %u.\n", tid);
Christopher Faulet1d17c102017-08-29 15:38:48 +02003080 exit(1);
3081 }
3082 }
3083
Willy Tarreau71092822019-06-10 09:51:04 +02003084 /* enabling protocols will result in fd_insert() calls to be performed,
3085 * we want all threads to have already allocated their local fd tables
Willy Tarreau34a150c2019-06-11 09:16:41 +02003086 * before doing so, thus only the last thread does it.
Willy Tarreau71092822019-06-10 09:51:04 +02003087 */
Willy Tarreau34a150c2019-06-11 09:16:41 +02003088 if (init_left == 0)
Willy Tarreaue4d7c9d2019-06-10 10:14:52 +02003089 protocol_enable_all();
Willy Tarreau6ec902a2019-06-07 14:41:11 +02003090
Willy Tarreau34a150c2019-06-11 09:16:41 +02003091#ifdef USE_THREAD
3092 pthread_cond_broadcast(&init_cond);
3093 pthread_mutex_unlock(&init_mutex);
3094
3095 /* now wait for other threads to finish starting */
3096 pthread_mutex_lock(&init_mutex);
3097 while (init_left)
3098 pthread_cond_wait(&init_cond, &init_mutex);
3099 pthread_mutex_unlock(&init_mutex);
3100#endif
Willy Tarreau3078e9f2019-05-20 10:50:43 +02003101
Willy Tarreaua45a8b52019-12-06 16:31:45 +01003102#if defined(PR_SET_NO_NEW_PRIVS) && defined(USE_PRCTL)
3103 /* Let's refrain from using setuid executables. This way the impact of
3104 * an eventual vulnerability in a library remains limited. It may
3105 * impact external checks but who cares about them anyway ? In the
3106 * worst case it's possible to disable the option. Obviously we do this
3107 * in workers only. We can't hard-fail on this one as it really is
3108 * implementation dependent though we're interested in feedback, hence
3109 * the warning.
3110 */
3111 if (!(global.tune.options & GTUNE_INSECURE_SETUID) && !master) {
3112 static int warn_fail;
3113 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1 && !_HA_ATOMIC_XADD(&warn_fail, 1)) {
3114 ha_warning("Failed to disable setuid, please report to developers with detailed "
3115 "information about your operating system. You can silence this warning "
3116 "by adding 'insecure-setuid-wanted' in the 'global' section.\n");
3117 }
3118 }
3119#endif
3120
Willy Tarreaud96f1122019-12-03 07:07:36 +01003121#if defined(RLIMIT_NPROC)
3122 /* all threads have started, it's now time to prevent any new thread
3123 * or process from starting. Obviously we do this in workers only. We
3124 * can't hard-fail on this one as it really is implementation dependent
3125 * though we're interested in feedback, hence the warning.
3126 */
3127 if (!(global.tune.options & GTUNE_INSECURE_FORK) && !master) {
3128 struct rlimit limit = { .rlim_cur = 0, .rlim_max = 0 };
3129 static int warn_fail;
3130
3131 if (setrlimit(RLIMIT_NPROC, &limit) == -1 && !_HA_ATOMIC_XADD(&warn_fail, 1)) {
3132 ha_warning("Failed to disable forks, please report to developers with detailed "
3133 "information about your operating system. You can silence this warning "
3134 "by adding 'insecure-fork-wanted' in the 'global' section.\n");
3135 }
3136 }
3137#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003138 run_poll_loop();
3139
3140 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
3141 ptdf->fct();
3142
Willy Tarreau082b6282019-05-22 14:42:12 +02003143 list_for_each_entry(ptff, &per_thread_free_list, list)
3144 ptff->fct();
3145
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003146#ifdef USE_THREAD
Olivier Houchardb23a61f2019-03-08 18:51:17 +01003147 _HA_ATOMIC_AND(&all_threads_mask, ~tid_bit);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003148 if (tid > 0)
3149 pthread_exit(NULL);
Christopher Faulet1d17c102017-08-29 15:38:48 +02003150#endif
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003151 return NULL;
3152}
Christopher Faulet1d17c102017-08-29 15:38:48 +02003153
William Dauchyf9af9d72019-11-17 15:47:16 +01003154/* set uid/gid depending on global settings */
3155static void set_identity(const char *program_name)
3156{
3157 if (global.gid) {
3158 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
3159 ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
3160 " without 'uid'/'user' is generally useless.\n", program_name);
3161
3162 if (setgid(global.gid) == -1) {
3163 ha_alert("[%s.main()] Cannot set gid %d.\n", program_name, global.gid);
3164 protocol_unbind_all();
3165 exit(1);
3166 }
3167 }
3168
3169 if (global.uid && setuid(global.uid) == -1) {
3170 ha_alert("[%s.main()] Cannot set uid %d.\n", program_name, global.uid);
3171 protocol_unbind_all();
3172 exit(1);
3173 }
3174}
3175
Willy Tarreaubaaee002006-06-26 02:48:02 +02003176int main(int argc, char **argv)
3177{
3178 int err, retry;
3179 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02003180 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02003181 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003182
Olivier Houchard5fa300d2018-02-03 15:15:21 +01003183 setvbuf(stdout, NULL, _IONBF, 0);
Willy Tarreau5794fb02018-11-25 18:43:29 +01003184
Willy Tarreauff9c9142019-02-07 10:39:36 +01003185 /* this can only safely be done here, though it's optimized away by
3186 * the compiler.
3187 */
3188 if (MAX_PROCS < 1 || MAX_PROCS > LONGBITS) {
3189 ha_alert("MAX_PROCS value must be between 1 and %d inclusive; "
3190 "HAProxy was built with value %d, please fix it and rebuild.\n",
3191 LONGBITS, MAX_PROCS);
3192 exit(1);
3193 }
3194
Willy Tarreaubf696402019-03-01 10:09:28 +01003195 /* take a copy of initial limits before we possibly change them */
3196 getrlimit(RLIMIT_NOFILE, &limit);
3197 rlim_fd_cur_at_boot = limit.rlim_cur;
3198 rlim_fd_max_at_boot = limit.rlim_max;
3199
Willy Tarreau5794fb02018-11-25 18:43:29 +01003200 /* process all initcalls in order of potential dependency */
3201 RUN_INITCALLS(STG_PREPARE);
3202 RUN_INITCALLS(STG_LOCK);
3203 RUN_INITCALLS(STG_ALLOC);
3204 RUN_INITCALLS(STG_POOL);
3205 RUN_INITCALLS(STG_REGISTER);
3206 RUN_INITCALLS(STG_INIT);
3207
Emeric Bruncf20bf12010-10-22 16:06:11 +02003208 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02003209 signal_register_fct(SIGQUIT, dump, SIGQUIT);
3210 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
3211 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
William Lallemand73b85e72017-06-01 17:38:51 +02003212 signal_register_fct(SIGUSR2, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003213
Willy Tarreaue437c442010-03-17 18:02:46 +01003214 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
3215 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
3216 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003217 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02003218 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003219
Willy Tarreaudc23a922011-02-16 11:10:36 +01003220 /* ulimits */
3221 if (!global.rlimit_nofile)
3222 global.rlimit_nofile = global.maxsock;
3223
3224 if (global.rlimit_nofile) {
Willy Tarreaue5cfdac2019-03-01 10:32:05 +01003225 limit.rlim_cur = global.rlimit_nofile;
3226 limit.rlim_max = MAX(rlim_fd_max_at_boot, limit.rlim_cur);
3227
Willy Tarreaudc23a922011-02-16 11:10:36 +01003228 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
Willy Tarreauef635472016-06-21 11:48:18 +02003229 getrlimit(RLIMIT_NOFILE, &limit);
William Dauchy0fec3ab2019-10-27 20:08:11 +01003230 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3231 ha_alert("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n",
3232 argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
3233 if (!(global.mode & MODE_MWORKER))
3234 exit(1);
3235 }
3236 else {
3237 /* try to set it to the max possible at least */
3238 limit.rlim_cur = limit.rlim_max;
3239 if (setrlimit(RLIMIT_NOFILE, &limit) != -1)
3240 getrlimit(RLIMIT_NOFILE, &limit);
Willy Tarreau164dd0b2016-06-21 11:51:59 +02003241
William Dauchya5194602020-03-28 19:29:58 +01003242 ha_warning("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n",
William Dauchy0fec3ab2019-10-27 20:08:11 +01003243 argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
3244 global.rlimit_nofile = limit.rlim_cur;
3245 }
Willy Tarreaudc23a922011-02-16 11:10:36 +01003246 }
3247 }
3248
3249 if (global.rlimit_memmax) {
3250 limit.rlim_cur = limit.rlim_max =
Willy Tarreau70060452015-12-14 12:46:07 +01003251 global.rlimit_memmax * 1048576ULL;
Willy Tarreaudc23a922011-02-16 11:10:36 +01003252#ifdef RLIMIT_AS
3253 if (setrlimit(RLIMIT_AS, &limit) == -1) {
William Dauchy0fec3ab2019-10-27 20:08:11 +01003254 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3255 ha_alert("[%s.main()] Cannot fix MEM limit to %d megs.\n",
3256 argv[0], global.rlimit_memmax);
3257 if (!(global.mode & MODE_MWORKER))
3258 exit(1);
3259 }
3260 else
William Dauchya5194602020-03-28 19:29:58 +01003261 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
William Dauchy0fec3ab2019-10-27 20:08:11 +01003262 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01003263 }
3264#else
3265 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
William Dauchy0fec3ab2019-10-27 20:08:11 +01003266 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3267 ha_alert("[%s.main()] Cannot fix MEM limit to %d megs.\n",
3268 argv[0], global.rlimit_memmax);
3269 if (!(global.mode & MODE_MWORKER))
3270 exit(1);
3271 }
3272 else
William Dauchya5194602020-03-28 19:29:58 +01003273 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
William Dauchy0fec3ab2019-10-27 20:08:11 +01003274 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01003275 }
3276#endif
3277 }
3278
Olivier Houchardf73629d2017-04-05 22:33:04 +02003279 if (old_unixsocket) {
William Lallemand85b0bd92017-06-01 17:38:53 +02003280 if (strcmp("/dev/null", old_unixsocket) != 0) {
3281 if (get_old_sockets(old_unixsocket) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003282 ha_alert("Failed to get the sockets from the old process!\n");
William Lallemand85b0bd92017-06-01 17:38:53 +02003283 if (!(global.mode & MODE_MWORKER))
3284 exit(1);
3285 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02003286 }
3287 }
William Lallemand85b0bd92017-06-01 17:38:53 +02003288 get_cur_unixsocket();
3289
Willy Tarreaubaaee002006-06-26 02:48:02 +02003290 /* We will loop at most 100 times with 10 ms delay each time.
3291 * That's at most 1 second. We only send a signal to old pids
3292 * if we cannot grab at least one port.
3293 */
3294 retry = MAX_START_RETRIES;
3295 err = ERR_NONE;
3296 while (retry >= 0) {
3297 struct timeval w;
3298 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01003299 /* exit the loop on no error or fatal error */
3300 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003301 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02003302 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003303 break;
3304
3305 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
3306 * listening sockets. So on those platforms, it would be wiser to
3307 * simply send SIGUSR1, which will not be undoable.
3308 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02003309 if (tell_old_pids(SIGTTOU) == 0) {
3310 /* no need to wait if we can't contact old pids */
3311 retry = 0;
3312 continue;
3313 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003314 /* give some time to old processes to stop listening */
3315 w.tv_sec = 0;
3316 w.tv_usec = 10*1000;
3317 select(0, NULL, NULL, NULL, &w);
3318 retry--;
3319 }
3320
3321 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01003322 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02003323 if (retry != MAX_START_RETRIES && nb_oldpids) {
3324 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003325 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02003326 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003327 exit(1);
3328 }
3329
William Lallemand944e6192018-11-21 15:48:31 +01003330 if (!(global.mode & MODE_MWORKER_WAIT) && listeners == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003331 ha_alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003332 /* Note: we don't have to send anything to the old pids because we
3333 * never stopped them. */
3334 exit(1);
3335 }
3336
Emeric Bruncf20bf12010-10-22 16:06:11 +02003337 err = protocol_bind_all(errmsg, sizeof(errmsg));
3338 if ((err & ~ERR_WARN) != ERR_NONE) {
3339 if ((err & ERR_ALERT) || (err & ERR_WARN))
Christopher Faulet767a84b2017-11-24 16:50:31 +01003340 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Emeric Bruncf20bf12010-10-22 16:06:11 +02003341
Christopher Faulet767a84b2017-11-24 16:50:31 +01003342 ha_alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02003343 protocol_unbind_all(); /* cleanup everything we can */
3344 if (nb_oldpids)
3345 tell_old_pids(SIGTTIN);
3346 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02003347 } else if (err & ERR_WARN) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003348 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02003349 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02003350 /* Ok, all listener should now be bound, close any leftover sockets
3351 * the previous process gave us, we don't need them anymore
3352 */
3353 while (xfer_sock_list != NULL) {
3354 struct xfer_sock_list *tmpxfer = xfer_sock_list->next;
3355 close(xfer_sock_list->fd);
3356 free(xfer_sock_list->iface);
3357 free(xfer_sock_list->namespace);
3358 free(xfer_sock_list);
3359 xfer_sock_list = tmpxfer;
3360 }
Willy Tarreaudd815982007-10-16 12:25:14 +02003361
Willy Tarreaubaaee002006-06-26 02:48:02 +02003362 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02003363 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
3364 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003365
Willy Tarreaubaaee002006-06-26 02:48:02 +02003366 /* MODE_QUIET can inhibit alerts and warnings below this line */
3367
PiBa-NL149a81a2017-12-25 21:03:31 +01003368 if (getenv("HAPROXY_MWORKER_REEXEC") != NULL) {
3369 /* either stdin/out/err are already closed or should stay as they are. */
3370 if ((global.mode & MODE_DAEMON)) {
3371 /* daemon mode re-executing, stdin/stdout/stderr are already closed so keep quiet */
3372 global.mode &= ~MODE_VERBOSE;
3373 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
3374 }
3375 } else {
3376 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
3377 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01003378 stdio_quiet(-1);
PiBa-NL149a81a2017-12-25 21:03:31 +01003379 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003380 }
3381
3382 /* open log & pid files before the chroot */
William Lallemand80293002017-11-06 11:00:03 +01003383 if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003384 unlink(global.pidfile);
3385 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
3386 if (pidfd < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003387 ha_alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003388 if (nb_oldpids)
3389 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02003390 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02003391 exit(1);
3392 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003393 }
3394
Willy Tarreaub38651a2007-03-24 17:24:39 +01003395 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003396 ha_alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
3397 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02003398 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01003399 exit(1);
3400 }
3401
Jackie Tapia749f74c2020-07-22 18:59:40 -05003402 /* If the user is not root, we'll still let them try the configuration
3403 * but we inform them that unexpected behaviour may occur.
Willy Tarreau4e30ed72009-02-04 18:02:48 +01003404 */
3405 if ((global.last_checks & LSTCHK_NETADM) && getuid())
Christopher Faulet767a84b2017-11-24 16:50:31 +01003406 ha_warning("[%s.main()] Some options which require full privileges"
3407 " might not work well.\n"
3408 "", argv[0]);
Willy Tarreau4e30ed72009-02-04 18:02:48 +01003409
William Lallemand095ba4c2017-06-01 17:38:50 +02003410 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
3411
3412 /* chroot if needed */
3413 if (global.chroot != NULL) {
3414 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003415 ha_alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02003416 if (nb_oldpids)
3417 tell_old_pids(SIGTTIN);
3418 protocol_unbind_all();
3419 exit(1);
3420 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02003421 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02003422 }
3423
William Lallemand944e6192018-11-21 15:48:31 +01003424 if (nb_oldpids && !(global.mode & MODE_MWORKER_WAIT))
Willy Tarreaubb545b42010-08-25 12:58:59 +02003425 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003426
William Lallemand27edc4b2019-05-07 17:49:33 +02003427 /* send a SIGTERM to workers who have a too high reloads number */
3428 if ((global.mode & MODE_MWORKER) && !(global.mode & MODE_MWORKER_WAIT))
3429 mworker_kill_max_reloads(SIGTERM);
3430
William Lallemand8a361b52017-06-20 11:20:33 +02003431 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) {
3432 nb_oldpids = 0;
3433 free(oldpids);
3434 oldpids = NULL;
3435 }
3436
3437
Willy Tarreaubaaee002006-06-26 02:48:02 +02003438 /* Note that any error at this stage will be fatal because we will not
3439 * be able to restart the old pids.
3440 */
3441
William Dauchyf9af9d72019-11-17 15:47:16 +01003442 if ((global.mode & (MODE_MWORKER | MODE_DAEMON)) == 0)
3443 set_identity(argv[0]);
Willy Tarreau636848a2019-04-15 19:38:50 +02003444
Willy Tarreaubaaee002006-06-26 02:48:02 +02003445 /* check ulimits */
3446 limit.rlim_cur = limit.rlim_max = 0;
3447 getrlimit(RLIMIT_NOFILE, &limit);
3448 if (limit.rlim_cur < global.maxsock) {
William Dauchy0fec3ab2019-10-27 20:08:11 +01003449 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3450 ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. "
3451 "Please raise 'ulimit-n' to %d or more to avoid any trouble.\n",
3452 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock,
3453 global.maxsock);
3454 if (!(global.mode & MODE_MWORKER))
3455 exit(1);
3456 }
3457 else
3458 ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. "
William Dauchya5194602020-03-28 19:29:58 +01003459 "Please raise 'ulimit-n' to %d or more to avoid any trouble.\n",
William Dauchy0fec3ab2019-10-27 20:08:11 +01003460 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock,
3461 global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003462 }
3463
William Lallemand944e6192018-11-21 15:48:31 +01003464 if (global.mode & (MODE_DAEMON | MODE_MWORKER | MODE_MWORKER_WAIT)) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003465 struct proxy *px;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003466 struct peers *curpeers;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003467 int ret = 0;
3468 int proc;
William Lallemande1340412017-12-28 16:09:36 +01003469 int devnullfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003470
William Lallemand095ba4c2017-06-01 17:38:50 +02003471 /*
3472 * if daemon + mworker: must fork here to let a master
3473 * process live in background before forking children
3474 */
William Lallemand73b85e72017-06-01 17:38:51 +02003475
3476 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
3477 && (global.mode & MODE_MWORKER)
3478 && (global.mode & MODE_DAEMON)) {
William Lallemand095ba4c2017-06-01 17:38:50 +02003479 ret = fork();
3480 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003481 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02003482 protocol_unbind_all();
3483 exit(1); /* there has been an error */
William Lallemandbfd8eb52018-07-04 15:31:23 +02003484 } else if (ret > 0) { /* parent leave to daemonize */
William Lallemand095ba4c2017-06-01 17:38:50 +02003485 exit(0);
William Lallemandbfd8eb52018-07-04 15:31:23 +02003486 } else /* change the process group ID in the child (master process) */
3487 setsid();
William Lallemand095ba4c2017-06-01 17:38:50 +02003488 }
William Lallemande20b6a62017-06-01 17:38:55 +02003489
William Lallemande20b6a62017-06-01 17:38:55 +02003490
William Lallemanddeed7802017-11-06 11:00:04 +01003491 /* if in master-worker mode, write the PID of the father */
3492 if (global.mode & MODE_MWORKER) {
3493 char pidstr[100];
Willy Tarreau76a80c72019-06-22 07:41:38 +02003494 snprintf(pidstr, sizeof(pidstr), "%d\n", (int)getpid());
Willy Tarreau46ec48b2018-01-23 19:20:19 +01003495 if (pidfd >= 0)
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01003496 DISGUISE(write(pidfd, pidstr, strlen(pidstr)));
William Lallemanddeed7802017-11-06 11:00:04 +01003497 }
3498
Willy Tarreaubaaee002006-06-26 02:48:02 +02003499 /* the father launches the required number of processes */
William Lallemand944e6192018-11-21 15:48:31 +01003500 if (!(global.mode & MODE_MWORKER_WAIT)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +02003501 if (global.mode & MODE_MWORKER)
3502 mworker_ext_launch_all();
William Lallemand944e6192018-11-21 15:48:31 +01003503 for (proc = 0; proc < global.nbproc; proc++) {
3504 ret = fork();
3505 if (ret < 0) {
3506 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
3507 protocol_unbind_all();
3508 exit(1); /* there has been an error */
3509 }
Willy Tarreau52bf8392020-03-08 00:42:37 +01003510 else if (ret == 0) { /* child breaks here */
3511 ha_random_jump96(relative_pid);
William Lallemand944e6192018-11-21 15:48:31 +01003512 break;
Willy Tarreau52bf8392020-03-08 00:42:37 +01003513 }
William Lallemand944e6192018-11-21 15:48:31 +01003514 if (pidfd >= 0 && !(global.mode & MODE_MWORKER)) {
3515 char pidstr[100];
3516 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01003517 DISGUISE(write(pidfd, pidstr, strlen(pidstr)));
William Lallemand944e6192018-11-21 15:48:31 +01003518 }
3519 if (global.mode & MODE_MWORKER) {
3520 struct mworker_proc *child;
William Lallemandce83b4a2018-10-26 14:47:30 +02003521
William Lallemand220567e2018-11-21 18:04:53 +01003522 ha_notice("New worker #%d (%d) forked\n", relative_pid, ret);
William Lallemand944e6192018-11-21 15:48:31 +01003523 /* find the right mworker_proc */
3524 list_for_each_entry(child, &proc_list, list) {
3525 if (child->relative_pid == relative_pid &&
William Lallemand8f7069a2019-04-12 16:09:23 +02003526 child->reloads == 0 && child->options & PROC_O_TYPE_WORKER) {
William Lallemand944e6192018-11-21 15:48:31 +01003527 child->timestamp = now.tv_sec;
3528 child->pid = ret;
William Lallemand1dc69632019-06-12 19:11:33 +02003529 child->version = strdup(haproxy_version);
William Lallemand944e6192018-11-21 15:48:31 +01003530 break;
3531 }
William Lallemandce83b4a2018-10-26 14:47:30 +02003532 }
3533 }
William Lallemandbc193052018-09-11 10:06:26 +02003534
William Lallemand944e6192018-11-21 15:48:31 +01003535 relative_pid++; /* each child will get a different one */
3536 pid_bit <<= 1;
3537 }
3538 } else {
3539 /* wait mode */
3540 global.nbproc = 1;
3541 proc = 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003542 }
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003543
3544#ifdef USE_CPU_AFFINITY
3545 if (proc < global.nbproc && /* child */
Willy Tarreauff9c9142019-02-07 10:39:36 +01003546 proc < MAX_PROCS && /* only the first 32/64 processes may be pinned */
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003547 global.cpu_map.proc[proc]) /* only do this if the process has a CPU map */
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003548#ifdef __FreeBSD__
Olivier Houchard97148f62017-08-16 17:29:11 +02003549 {
3550 cpuset_t cpuset;
3551 int i;
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003552 unsigned long cpu_map = global.cpu_map.proc[proc];
Olivier Houchard97148f62017-08-16 17:29:11 +02003553
3554 CPU_ZERO(&cpuset);
3555 while ((i = ffsl(cpu_map)) > 0) {
3556 CPU_SET(i - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003557 cpu_map &= ~(1UL << (i - 1));
Olivier Houchard97148f62017-08-16 17:29:11 +02003558 }
3559 ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset);
3560 }
David Carlier5e4c8e22019-09-13 05:12:58 +01003561#elif defined(__linux__)
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003562 sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map.proc[proc]);
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003563#endif
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003564#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003565 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02003566 if (pidfd >= 0) {
3567 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
3568 close(pidfd);
3569 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02003570
3571 /* We won't ever use this anymore */
Willy Tarreaud137dd32010-08-25 12:49:05 +02003572 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003573
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003574 if (proc == global.nbproc) {
William Lallemand944e6192018-11-21 15:48:31 +01003575 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003576
3577 if ((!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
3578 (global.mode & MODE_DAEMON)) {
3579 /* detach from the tty, this is required to properly daemonize. */
William Lallemande1340412017-12-28 16:09:36 +01003580 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL))
3581 stdio_quiet(-1);
3582
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003583 global.mode &= ~MODE_VERBOSE;
3584 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003585 }
3586
William Lallemandb3f2be32018-09-11 10:06:18 +02003587 mworker_loop();
William Lallemand1499b9b2017-06-07 15:04:47 +02003588 /* should never get there */
3589 exit(EXIT_FAILURE);
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003590 }
William Lallemandcf4e4962017-06-08 19:05:48 +02003591#if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH)
Grant Zhang872f9c22017-01-21 01:10:18 +00003592 ssl_free_dh();
3593#endif
William Lallemand1499b9b2017-06-07 15:04:47 +02003594 exit(0); /* parent must leave */
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003595 }
3596
William Lallemandcb11fd22017-06-01 17:38:52 +02003597 /* child must never use the atexit function */
3598 atexit_flag = 0;
3599
William Lallemandbc193052018-09-11 10:06:26 +02003600 /* close useless master sockets */
3601 if (global.mode & MODE_MWORKER) {
3602 struct mworker_proc *child, *it;
3603 master = 0;
3604
William Lallemand309dc9a2018-10-26 14:47:45 +02003605 mworker_cli_proxy_stop();
3606
William Lallemandbc193052018-09-11 10:06:26 +02003607 /* free proc struct of other processes */
3608 list_for_each_entry_safe(child, it, &proc_list, list) {
William Lallemandce83b4a2018-10-26 14:47:30 +02003609 /* close the FD of the master side for all
3610 * workers, we don't need to close the worker
3611 * side of other workers since it's done with
3612 * the bind_proc */
Tim Duesterhus742e0f92018-11-25 20:03:39 +01003613 if (child->ipc_fd[0] >= 0)
3614 close(child->ipc_fd[0]);
William Lallemandce83b4a2018-10-26 14:47:30 +02003615 if (child->relative_pid == relative_pid &&
3616 child->reloads == 0) {
3617 /* keep this struct if this is our pid */
3618 proc_self = child;
William Lallemandbc193052018-09-11 10:06:26 +02003619 continue;
William Lallemandce83b4a2018-10-26 14:47:30 +02003620 }
William Lallemandbc193052018-09-11 10:06:26 +02003621 LIST_DEL(&child->list);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +02003622 mworker_free_child(child);
3623 child = NULL;
William Lallemandbc193052018-09-11 10:06:26 +02003624 }
3625 }
Willy Tarreau1605c7a2018-01-23 19:01:49 +01003626
William Lallemande1340412017-12-28 16:09:36 +01003627 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
3628 devnullfd = open("/dev/null", O_RDWR, 0);
3629 if (devnullfd < 0) {
3630 ha_alert("Cannot open /dev/null\n");
3631 exit(EXIT_FAILURE);
3632 }
3633 }
3634
William Lallemand095ba4c2017-06-01 17:38:50 +02003635 /* Must chroot and setgid/setuid in the children */
3636 /* chroot if needed */
3637 if (global.chroot != NULL) {
3638 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003639 ha_alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02003640 if (nb_oldpids)
3641 tell_old_pids(SIGTTIN);
3642 protocol_unbind_all();
3643 exit(1);
3644 }
3645 }
3646
3647 free(global.chroot);
3648 global.chroot = NULL;
William Dauchyf9af9d72019-11-17 15:47:16 +01003649 set_identity(argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02003650
William Lallemand7f80eb22017-05-26 18:19:55 +02003651 /* pass through every cli socket, and check if it's bound to
3652 * the current process and if it exposes listeners sockets.
3653 * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork.
3654 * */
3655
3656 if (global.stats_fe) {
3657 struct bind_conf *bind_conf;
3658
3659 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
3660 if (bind_conf->level & ACCESS_FD_LISTENERS) {
3661 if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) {
3662 global.tune.options |= GTUNE_SOCKET_TRANSFER;
3663 break;
3664 }
3665 }
3666 }
3667 }
3668
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003669 /* we might have to unbind some proxies from some processes */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003670 px = proxies_list;
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003671 while (px != NULL) {
3672 if (px->bind_proc && px->state != PR_STSTOPPED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02003673 if (!(px->bind_proc & (1UL << proc))) {
3674 if (global.tune.options & GTUNE_SOCKET_TRANSFER)
3675 zombify_proxy(px);
3676 else
3677 stop_proxy(px);
3678 }
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003679 }
3680 px = px->next;
3681 }
3682
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003683 /* we might have to unbind some peers sections from some processes */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003684 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003685 if (!curpeers->peers_fe)
3686 continue;
3687
3688 if (curpeers->peers_fe->bind_proc & (1UL << proc))
3689 continue;
3690
3691 stop_proxy(curpeers->peers_fe);
3692 /* disable this peer section so that it kills itself */
Willy Tarreau47c8c022015-09-28 16:39:25 +02003693 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02003694 task_destroy(curpeers->sync_task);
Willy Tarreau47c8c022015-09-28 16:39:25 +02003695 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +02003696 task_destroy(curpeers->peers_fe->task);
Willy Tarreau47c8c022015-09-28 16:39:25 +02003697 curpeers->peers_fe->task = NULL;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003698 curpeers->peers_fe = NULL;
3699 }
3700
William Lallemand2e8fad92018-11-13 16:18:23 +01003701 /*
3702 * This is only done in daemon mode because we might want the
3703 * logs on stdout in mworker mode. If we're NOT in QUIET mode,
3704 * we should now close the 3 first FDs to ensure that we can
3705 * detach from the TTY. We MUST NOT do it in other cases since
3706 * it would have already be done, and 0-2 would have been
3707 * affected to listening sockets
Willy Tarreaubaaee002006-06-26 02:48:02 +02003708 */
William Lallemand2e8fad92018-11-13 16:18:23 +01003709 if ((global.mode & MODE_DAEMON) &&
3710 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003711 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01003712 stdio_quiet(devnullfd);
Willy Tarreau106cb762008-11-16 07:40:34 +01003713 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003714 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
3715 }
3716 pid = getpid(); /* update child's pid */
William Lallemandbfd8eb52018-07-04 15:31:23 +02003717 if (!(global.mode & MODE_MWORKER)) /* in mworker mode we don't want a new pgid for the children */
3718 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02003719 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02003720 }
3721
William Dauchye039f262019-11-17 15:47:15 +01003722 /* try our best to re-enable core dumps depending on system capabilities.
3723 * What is addressed here :
3724 * - remove file size limits
3725 * - remove core size limits
3726 * - mark the process dumpable again if it lost it due to user/group
3727 */
3728 if (global.tune.options & GTUNE_SET_DUMPABLE) {
3729 limit.rlim_cur = limit.rlim_max = RLIM_INFINITY;
3730
3731#if defined(RLIMIT_FSIZE)
3732 if (setrlimit(RLIMIT_FSIZE, &limit) == -1) {
3733 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3734 ha_alert("[%s.main()] Failed to set the raise the maximum "
3735 "file size.\n", argv[0]);
3736 if (!(global.mode & MODE_MWORKER))
3737 exit(1);
3738 }
3739 else
3740 ha_warning("[%s.main()] Failed to set the raise the maximum "
William Dauchya5194602020-03-28 19:29:58 +01003741 "file size.\n", argv[0]);
William Dauchye039f262019-11-17 15:47:15 +01003742 }
3743#endif
3744
3745#if defined(RLIMIT_CORE)
3746 if (setrlimit(RLIMIT_CORE, &limit) == -1) {
3747 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3748 ha_alert("[%s.main()] Failed to set the raise the core "
3749 "dump size.\n", argv[0]);
3750 if (!(global.mode & MODE_MWORKER))
3751 exit(1);
3752 }
3753 else
3754 ha_warning("[%s.main()] Failed to set the raise the core "
William Dauchya5194602020-03-28 19:29:58 +01003755 "dump size.\n", argv[0]);
William Dauchye039f262019-11-17 15:47:15 +01003756 }
3757#endif
3758
3759#if defined(USE_PRCTL)
3760 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1)
3761 ha_warning("[%s.main()] Failed to set the dumpable flag, "
3762 "no core will be dumped.\n", argv[0]);
3763#endif
3764 }
3765
Christopher Faulete3a5e352017-10-24 13:53:54 +02003766 global.mode &= ~MODE_STARTING;
Willy Tarreau4f60f162007-04-08 16:39:58 +02003767 /*
3768 * That's it : the central polling loop. Run until we stop.
3769 */
Christopher Faulet1d17c102017-08-29 15:38:48 +02003770#ifdef USE_THREAD
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003771 {
William Lallemand1aab50b2018-06-07 09:46:01 +02003772 sigset_t blocked_sig, old_sig;
Willy Tarreauc40efc12019-05-03 09:22:44 +02003773 int i;
3774
William Lallemand1aab50b2018-06-07 09:46:01 +02003775 /* ensure the signals will be blocked in every thread */
3776 sigfillset(&blocked_sig);
3777 sigdelset(&blocked_sig, SIGPROF);
3778 sigdelset(&blocked_sig, SIGBUS);
3779 sigdelset(&blocked_sig, SIGFPE);
3780 sigdelset(&blocked_sig, SIGILL);
3781 sigdelset(&blocked_sig, SIGSEGV);
3782 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
3783
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003784 /* Create nbthread-1 thread. The first thread is the current process */
David Carliera92c5ce2019-09-13 05:03:12 +01003785 ha_thread_info[0].pthread = pthread_self();
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003786 for (i = 1; i < global.nbthread; i++)
David Carliera92c5ce2019-09-13 05:03:12 +01003787 pthread_create(&ha_thread_info[i].pthread, NULL, &run_thread_poll_loop, (void *)(long)i);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003788
Christopher Faulet62519022017-10-16 15:49:32 +02003789#ifdef USE_CPU_AFFINITY
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003790 /* Now the CPU affinity for all threads */
Willy Tarreau7764a572019-07-16 15:10:34 +02003791 if (global.cpu_map.proc_t1[relative_pid-1])
3792 global.cpu_map.thread[0] &= global.cpu_map.proc_t1[relative_pid-1];
3793
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003794 for (i = 0; i < global.nbthread; i++) {
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003795 if (global.cpu_map.proc[relative_pid-1])
Willy Tarreau81492c92019-05-03 09:41:23 +02003796 global.cpu_map.thread[i] &= global.cpu_map.proc[relative_pid-1];
Christopher Faulet62519022017-10-16 15:49:32 +02003797
Willy Tarreau421f02e2018-01-20 18:19:22 +01003798 if (i < MAX_THREADS && /* only the first 32/64 threads may be pinned */
Willy Tarreau81492c92019-05-03 09:41:23 +02003799 global.cpu_map.thread[i]) {/* only do this if the thread has a THREAD map */
David Carlier5e4c8e22019-09-13 05:12:58 +01003800#if defined(__APPLE__)
3801 int j;
3802 unsigned long cpu_map = global.cpu_map.thread[i];
3803
3804 while ((j = ffsl(cpu_map)) > 0) {
3805 thread_affinity_policy_data_t cpu_set = { j - 1 };
3806 thread_port_t mthread = pthread_mach_thread_np(ha_thread_info[i].pthread);
3807 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
3808 cpu_map &= ~(1UL << (j - 1));
3809 }
3810#else
Olivier Houchard829aa242017-12-01 18:19:43 +01003811#if defined(__FreeBSD__) || defined(__NetBSD__)
3812 cpuset_t cpuset;
3813#else
3814 cpu_set_t cpuset;
3815#endif
3816 int j;
Willy Tarreau81492c92019-05-03 09:41:23 +02003817 unsigned long cpu_map = global.cpu_map.thread[i];
Olivier Houchard829aa242017-12-01 18:19:43 +01003818
3819 CPU_ZERO(&cpuset);
3820
3821 while ((j = ffsl(cpu_map)) > 0) {
3822 CPU_SET(j - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003823 cpu_map &= ~(1UL << (j - 1));
Olivier Houchard829aa242017-12-01 18:19:43 +01003824 }
David Carliera92c5ce2019-09-13 05:03:12 +01003825 pthread_setaffinity_np(ha_thread_info[i].pthread,
Olivier Houchard829aa242017-12-01 18:19:43 +01003826 sizeof(cpuset), &cpuset);
David Carlier5e4c8e22019-09-13 05:12:58 +01003827#endif
Olivier Houchard829aa242017-12-01 18:19:43 +01003828 }
Christopher Faulet1d17c102017-08-29 15:38:48 +02003829 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003830#endif /* !USE_CPU_AFFINITY */
3831
William Lallemand1aab50b2018-06-07 09:46:01 +02003832 /* when multithreading we need to let only the thread 0 handle the signals */
William Lallemandd3801c12018-09-11 10:06:23 +02003833 haproxy_unblock_signals();
William Lallemand1aab50b2018-06-07 09:46:01 +02003834
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003835 /* Finally, start the poll loop for the first thread */
Willy Tarreaub4f7cc32019-05-03 09:27:30 +02003836 run_thread_poll_loop(0);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003837
3838 /* Wait the end of other threads */
3839 for (i = 1; i < global.nbthread; i++)
David Carliera92c5ce2019-09-13 05:03:12 +01003840 pthread_join(ha_thread_info[i].pthread, NULL);
Christopher Faulet1d17c102017-08-29 15:38:48 +02003841
Christopher Fauletb79a94c2017-05-30 15:34:30 +02003842#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
3843 show_lock_stats();
3844#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003845 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003846#else /* ! USE_THREAD */
William Lallemandd3801c12018-09-11 10:06:23 +02003847 haproxy_unblock_signals();
Willy Tarreaub4f7cc32019-05-03 09:27:30 +02003848 run_thread_poll_loop(0);
Christopher Faulet62519022017-10-16 15:49:32 +02003849#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003850
Tim Duesterhus0a3b43d2020-06-14 00:37:42 +02003851 deinit_and_exit(0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003852}
3853
Willy Tarreaubaaee002006-06-26 02:48:02 +02003854/*
3855 * Local variables:
3856 * c-indent-level: 8
3857 * c-basic-offset: 8
3858 * End:
3859 */