blob: 6370d8e59f14bb40d7b1cc357e22650e23729c24 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HA-Proxy : High Availability-enabled HTTP/TCP proxy
Willy Tarreau71f95fa2020-01-22 10:34:58 +01003 * Copyright 2000-2020 Willy Tarreau <willy@haproxy.org>.
Willy Tarreaubaaee002006-06-26 02:48:02 +02004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
Lukas Tribus23953682017-04-28 13:24:30 +000010 * Please refer to RFC7230 - RFC7235 informations about HTTP protocol, and
11 * RFC6265 for informations about cookies usage. More generally, the IETF HTTP
Willy Tarreaubaaee002006-06-26 02:48:02 +020012 * Working Group's web site should be consulted for protocol related changes :
13 *
14 * http://ftp.ics.uci.edu/pub/ietf/http/
15 *
16 * Pending bugs (may be not fixed because never reproduced) :
17 * - solaris only : sometimes, an HTTP proxy with only a dispatch address causes
18 * the proxy to terminate (no core) if the client breaks the connection during
19 * the response. Seen on 1.1.8pre4, but never reproduced. May not be related to
20 * the snprintf() bug since requests were simple (GET / HTTP/1.0), but may be
21 * related to missing setsid() (fixed in 1.1.15)
22 * - a proxy with an invalid config will prevent the startup even if disabled.
23 *
24 * ChangeLog has moved to the CHANGELOG file.
25 *
Willy Tarreaubaaee002006-06-26 02:48:02 +020026 */
27
David Carlier7ece0962015-12-08 21:43:09 +000028#define _GNU_SOURCE
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <string.h>
33#include <ctype.h>
Maxime de Roucy379d9c72016-05-13 23:52:56 +020034#include <dirent.h>
Maxime de Roucy379d9c72016-05-13 23:52:56 +020035#include <sys/stat.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <sys/time.h>
37#include <sys/types.h>
38#include <sys/socket.h>
39#include <netinet/tcp.h>
40#include <netinet/in.h>
41#include <arpa/inet.h>
Olivier Houchardf73629d2017-04-05 22:33:04 +020042#include <net/if.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <netdb.h>
44#include <fcntl.h>
45#include <errno.h>
46#include <signal.h>
47#include <stdarg.h>
48#include <sys/resource.h>
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 Tarreaudcc048a2020-06-04 19:11:43 +020081#include <haproxy/acl.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020082#include <haproxy/auth.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020083#include <haproxy/api.h>
Willy Tarreau6c3a6812020-03-06 18:57:15 +010084#include <import/sha1.h>
85
Willy Tarreau8d366972020-05-27 16:10:29 +020086#include <haproxy/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020087#include <common/cfgparse.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020088#include <haproxy/chunk.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020089#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020090#include <haproxy/connection.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020091#include <haproxy/dns.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020092#include <haproxy/dynbuf.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020093#include <haproxy/errors.h>
Willy Tarreau86416052020-06-04 09:20:54 +020094#include <haproxy/hlua.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020095#include <haproxy/http_rules.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020096#include <haproxy/pool.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020097#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020098#include <haproxy/listener.h>
Willy Tarreaub5abe5b2020-06-04 14:07:37 +020099#include <haproxy/mworker.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +0200100#include <haproxy/namespace.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +0200101#include <haproxy/net_helper.h>
Willy Tarreau6019fab2020-05-27 16:26:00 +0200102#include <haproxy/openssl-compat.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +0200103#include <haproxy/pattern.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +0200104#include <haproxy/peers.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +0200105#include <haproxy/sample.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +0200106#include <haproxy/regex.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +0200107#include <haproxy/session.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +0200108#include <haproxy/signal.h>
Willy Tarreau209108d2020-06-04 20:30:20 +0200109#include <haproxy/ssl_sock.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +0200110#include <haproxy/tools.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +0200111#include <haproxy/time.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +0200112#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +0200113#include <haproxy/version.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +0200114#include <haproxy/task.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +0200115#include <haproxy/thread.h>
Willy Tarreaua1718922020-06-04 16:25:31 +0200116#include <haproxy/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117
Willy Tarreau278161c2020-06-04 11:18:28 +0200118#include <haproxy/capture-t.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +0200119#include <types/filters.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +0200120#include <haproxy/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121
Willy Tarreaua04ded52020-06-02 10:29:48 +0200122#include <haproxy/activity.h>
Willy Tarreauaa74c4e2020-06-04 10:19:23 +0200123#include <haproxy/arg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124#include <proto/backend.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +0200125#include <haproxy/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +0200126#include <proto/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127#include <proto/log.h>
Willy Tarreau2dd7c352020-06-03 15:26:55 +0200128#include <haproxy/protocol.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129#include <proto/proxy.h>
130#include <proto/queue.h>
131#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +0200132#include <proto/stream.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133
Willy Tarreau7b5654f2019-03-29 21:30:17 +0100134/* array of init calls for older platforms */
135DECLARE_INIT_STAGES;
136
Willy Tarreau477ecd82010-01-03 21:12:30 +0100137/* list of config files */
138static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100140int relative_pid = 1; /* process id starting at 1 */
Willy Tarreau387bd4f2017-11-10 19:08:14 +0100141unsigned long pid_bit = 1; /* bit corresponding to the process id */
Willy Tarreaua38a7172019-02-02 17:11:28 +0100142unsigned long all_proc_mask = 1; /* mask of all processes */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143
Willy Tarreauf8ea00e2020-03-12 17:24:53 +0100144volatile unsigned long sleeping_thread_mask = 0; /* Threads that are about to sleep in poll() */
Willy Tarreau4b3f27b2020-03-12 17:28:01 +0100145volatile unsigned long stopping_thread_mask = 0; /* Threads acknowledged stopping */
Willy Tarreauf8ea00e2020-03-12 17:24:53 +0100146
Willy Tarreaubaaee002006-06-26 02:48:02 +0200147/* global options */
148struct global global = {
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100149 .hard_stop_after = TICK_ETERNITY,
Willy Tarreau247a13a2012-11-15 17:38:15 +0100150 .nbproc = 1,
Willy Tarreau149ab772019-01-26 14:27:06 +0100151 .nbthread = 0,
William Lallemand5f232402012-04-05 18:02:55 +0200152 .req_count = 0,
William Lallemand0f99e342011-10-12 17:50:54 +0200153 .logsrvs = LIST_HEAD_INIT(global.logsrvs),
William Lallemand9d5f5482012-11-07 16:12:57 +0100154 .maxzlibmem = 0,
William Lallemandd85f9172012-11-09 17:05:39 +0100155 .comp_rate_lim = 0,
Emeric Brun850efd52014-01-29 12:24:34 +0100156 .ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED,
Emeric Bruned760922010-10-22 17:59:25 +0200157 .unix_bind = {
158 .ux = {
159 .uid = -1,
160 .gid = -1,
161 .mode = 0,
162 }
163 },
Willy Tarreau27a674e2009-08-17 07:23:33 +0200164 .tune = {
Willy Tarreau7ac908b2019-02-27 12:02:18 +0100165 .options = GTUNE_LISTENER_MQ,
Willy Tarreauc77d3642018-12-12 06:19:42 +0100166 .bufsize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)),
Christopher Faulet546c4692020-01-22 14:31:21 +0100167 .maxrewrite = MAXREWRITE,
Willy Tarreauc77d3642018-12-12 06:19:42 +0100168 .chksize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)),
Willy Tarreaua24adf02014-11-27 01:11:56 +0100169 .reserved_bufs = RESERVED_BUFS,
Willy Tarreauf3045d22015-04-29 16:24:50 +0200170 .pattern_cache = DEFAULT_PAT_LRU_SIZE,
Olivier Houchard88698d92019-04-16 19:07:22 +0200171 .pool_low_ratio = 20,
172 .pool_high_ratio = 25,
Christopher Faulet41ba36f2019-07-19 09:36:45 +0200173 .max_http_hdr = MAX_HTTP_HDR,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200174#ifdef USE_OPENSSL
Emeric Brun46635772012-11-14 11:32:56 +0100175 .sslcachesize = SSLCACHESIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200176#endif
William Lallemandf3747832012-11-09 12:33:10 +0100177 .comp_maxlevel = 1,
Willy Tarreau7e312732014-02-12 16:35:14 +0100178#ifdef DEFAULT_IDLE_TIMER
179 .idle_timer = DEFAULT_IDLE_TIMER,
180#else
181 .idle_timer = 1000, /* 1 second */
182#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200183 },
Emeric Brun76d88952012-10-05 15:47:31 +0200184#ifdef USE_OPENSSL
185#ifdef DEFAULT_MAXSSLCONN
Willy Tarreau403edff2012-09-06 11:58:37 +0200186 .maxsslconn = DEFAULT_MAXSSLCONN,
187#endif
Emeric Brun76d88952012-10-05 15:47:31 +0200188#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189 /* others NULL OK */
190};
191
192/*********************************************************************/
193
194int stopping; /* non zero means stopping in progress */
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100195int killed; /* non zero means a hard-stop is triggered */
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200196int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */
William Lallemanda7199262018-11-16 16:57:20 +0100197int unstoppable_jobs = 0; /* number of active jobs that can't be stopped during a soft stop */
Willy Tarreau199ad242018-11-05 16:31:22 +0100198int active_peers = 0; /* number of active peers (connection attempts and connected) */
Willy Tarreau2d372c22018-11-05 17:12:27 +0100199int connected_peers = 0; /* number of connected peers (verified ones) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200200
201/* Here we store informations about the pids of the processes we may pause
202 * or kill. We will send them a signal every 10 ms until we can bind to all
203 * our ports. With 200 retries, that's about 2 seconds.
204 */
205#define MAX_START_RETRIES 200
Willy Tarreaubaaee002006-06-26 02:48:02 +0200206static int *oldpids = NULL;
207static int oldpids_sig; /* use USR1 or TERM */
208
Olivier Houchardf73629d2017-04-05 22:33:04 +0200209/* Path to the unix socket we use to retrieve listener sockets from the old process */
210static const char *old_unixsocket;
211
William Lallemand85b0bd92017-06-01 17:38:53 +0200212static char *cur_unixsocket = NULL;
213
William Lallemandcb11fd22017-06-01 17:38:52 +0200214int atexit_flag = 0;
215
Willy Tarreaubb545b42010-08-25 12:58:59 +0200216int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200217const int zero = 0;
218const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200219const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200220
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100221char hostname[MAX_HOSTNAME_LEN];
Emeric Brun2b920a12010-09-23 18:30:22 +0200222char localpeer[MAX_HOSTNAME_LEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200223
William Lallemand00417412020-06-05 14:08:41 +0200224static char **old_argv = NULL; /* previous argv but cleaned up */
William Lallemand73b85e72017-06-01 17:38:51 +0200225
William Lallemandbc193052018-09-11 10:06:26 +0200226struct list proc_list = LIST_HEAD_INIT(proc_list);
227
228int master = 0; /* 1 if in master, 0 if in child */
Willy Tarreaubf696402019-03-01 10:09:28 +0100229unsigned int rlim_fd_cur_at_boot = 0;
230unsigned int rlim_fd_max_at_boot = 0;
William Lallemandbc193052018-09-11 10:06:26 +0200231
Willy Tarreau6c3a6812020-03-06 18:57:15 +0100232/* per-boot randomness */
233unsigned char boot_seed[20]; /* per-boot random seed (160 bits initially) */
234
William Lallemand16dd1b32018-11-19 18:46:18 +0100235struct mworker_proc *proc_self = NULL;
William Lallemandbc193052018-09-11 10:06:26 +0200236
William Lallemandb3f2be32018-09-11 10:06:18 +0200237static void *run_thread_poll_loop(void *data);
238
Willy Tarreauff055502014-04-28 22:27:06 +0200239/* bitfield of a few warnings to emit just once (WARN_*) */
240unsigned int warned = 0;
241
William Lallemande7361152018-10-26 14:47:36 +0200242/* master CLI configuration (-S flag) */
243struct list mworker_cli_conf = LIST_HEAD_INIT(mworker_cli_conf);
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100244
245/* These are strings to be reported in the output of "haproxy -vv". They may
246 * either be constants (in which case must_free must be zero) or dynamically
247 * allocated strings to pass to free() on exit, and in this case must_free
248 * must be non-zero.
249 */
250struct list build_opts_list = LIST_HEAD_INIT(build_opts_list);
251struct build_opts_str {
252 struct list list;
253 const char *str;
254 int must_free;
255};
256
Willy Tarreaue6945732016-12-21 19:57:00 +0100257/* These functions are called just after the point where the program exits
258 * after a config validity check, so they are generally suited for resource
259 * allocation and slow initializations that should be skipped during basic
260 * config checks. The functions must return 0 on success, or a combination
261 * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause
262 * and immediate exit, so the function must have emitted any useful error.
263 */
264struct list post_check_list = LIST_HEAD_INIT(post_check_list);
265struct post_check_fct {
266 struct list list;
267 int (*fct)();
268};
269
Christopher Fauletc1692962019-08-12 09:51:07 +0200270/* These functions are called for each proxy just after the config validity
271 * check. The functions must return 0 on success, or a combination of ERR_*
272 * flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause and immediate
273 * exit, so the function must have emitted any useful error.
274 */
275struct list post_proxy_check_list = LIST_HEAD_INIT(post_proxy_check_list);
276struct post_proxy_check_fct {
277 struct list list;
278 int (*fct)(struct proxy *);
279};
280
281/* These functions are called for each server just after the config validity
282 * check. The functions must return 0 on success, or a combination of ERR_*
283 * flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause and immediate
284 * exit, so the function must have emitted any useful error.
285 */
286struct list post_server_check_list = LIST_HEAD_INIT(post_server_check_list);
287struct post_server_check_fct {
288 struct list list;
289 int (*fct)(struct server *);
290};
291
Willy Tarreau082b6282019-05-22 14:42:12 +0200292/* These functions are called for each thread just after the thread creation
293 * and before running the init functions. They should be used to do per-thread
294 * (re-)allocations that are needed by subsequent functoins. They must return 0
295 * if an error occurred. */
296struct list per_thread_alloc_list = LIST_HEAD_INIT(per_thread_alloc_list);
297struct per_thread_alloc_fct {
Willy Tarreau05554e62016-12-21 20:46:26 +0100298 struct list list;
Willy Tarreau082b6282019-05-22 14:42:12 +0200299 int (*fct)();
Willy Tarreau05554e62016-12-21 20:46:26 +0100300};
301
Christopher Faulet415f6112017-07-25 16:52:58 +0200302/* These functions are called for each thread just after the thread creation
303 * and before running the scheduler. They should be used to do per-thread
304 * initializations. They must return 0 if an error occurred. */
305struct list per_thread_init_list = LIST_HEAD_INIT(per_thread_init_list);
306struct per_thread_init_fct {
307 struct list list;
308 int (*fct)();
309};
310
Willy Tarreau082b6282019-05-22 14:42:12 +0200311/* These functions are called when freeing the global sections at the end of
312 * deinit, after everything is stopped. They don't return anything. They should
313 * not release shared resources that are possibly used by other deinit
314 * functions, only close/release what is private. Use the per_thread_free_list
315 * to release shared resources.
316 */
317struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list);
318struct post_deinit_fct {
319 struct list list;
320 void (*fct)();
321};
322
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +0200323/* These functions are called when freeing a proxy during the deinit, after
324 * everything isg stopped. They don't return anything. They should not release
325 * the proxy itself or any shared resources that are possibly used by other
326 * deinit functions, only close/release what is private.
327 */
328struct list proxy_deinit_list = LIST_HEAD_INIT(proxy_deinit_list);
329struct proxy_deinit_fct {
330 struct list list;
331 void (*fct)(struct proxy *);
332};
333
334/* These functions are called when freeing a server during the deinit, after
335 * everything isg stopped. They don't return anything. They should not release
336 * the proxy itself or any shared resources that are possibly used by other
337 * deinit functions, only close/release what is private.
338 */
339struct list server_deinit_list = LIST_HEAD_INIT(server_deinit_list);
340struct server_deinit_fct {
341 struct list list;
342 void (*fct)(struct server *);
343};
344
Willy Tarreau082b6282019-05-22 14:42:12 +0200345/* These functions are called when freeing the global sections at the end of
346 * deinit, after the thread deinit functions, to release unneeded memory
347 * allocations. They don't return anything, and they work in best effort mode
348 * as their sole goal is to make valgrind mostly happy.
349 */
350struct list per_thread_free_list = LIST_HEAD_INIT(per_thread_free_list);
351struct per_thread_free_fct {
352 struct list list;
353 int (*fct)();
354};
355
Christopher Faulet415f6112017-07-25 16:52:58 +0200356/* These functions are called for each thread just after the scheduler loop and
357 * before exiting the thread. They don't return anything and, as for post-deinit
358 * functions, they work in best effort mode as their sole goal is to make
359 * valgrind mostly happy. */
360struct list per_thread_deinit_list = LIST_HEAD_INIT(per_thread_deinit_list);
361struct per_thread_deinit_fct {
362 struct list list;
363 void (*fct)();
364};
365
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366/*********************************************************************/
367/* general purpose functions ***************************************/
368/*********************************************************************/
369
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100370/* used to register some build option strings at boot. Set must_free to
371 * non-zero if the string must be freed upon exit.
372 */
373void hap_register_build_opts(const char *str, int must_free)
374{
375 struct build_opts_str *b;
376
377 b = calloc(1, sizeof(*b));
378 if (!b) {
379 fprintf(stderr, "out of memory\n");
380 exit(1);
381 }
382 b->str = str;
383 b->must_free = must_free;
384 LIST_ADDQ(&build_opts_list, &b->list);
385}
386
Willy Tarreaue6945732016-12-21 19:57:00 +0100387/* used to register some initialization functions to call after the checks. */
388void hap_register_post_check(int (*fct)())
389{
390 struct post_check_fct *b;
391
392 b = calloc(1, sizeof(*b));
393 if (!b) {
394 fprintf(stderr, "out of memory\n");
395 exit(1);
396 }
397 b->fct = fct;
398 LIST_ADDQ(&post_check_list, &b->list);
399}
400
Christopher Fauletc1692962019-08-12 09:51:07 +0200401/* used to register some initialization functions to call for each proxy after
402 * the checks.
403 */
404void hap_register_post_proxy_check(int (*fct)(struct proxy *))
405{
406 struct post_proxy_check_fct *b;
407
408 b = calloc(1, sizeof(*b));
409 if (!b) {
410 fprintf(stderr, "out of memory\n");
411 exit(1);
412 }
413 b->fct = fct;
414 LIST_ADDQ(&post_proxy_check_list, &b->list);
415}
416
417/* used to register some initialization functions to call for each server after
418 * the checks.
419 */
420void hap_register_post_server_check(int (*fct)(struct server *))
421{
422 struct post_server_check_fct *b;
423
424 b = calloc(1, sizeof(*b));
425 if (!b) {
426 fprintf(stderr, "out of memory\n");
427 exit(1);
428 }
429 b->fct = fct;
430 LIST_ADDQ(&post_server_check_list, &b->list);
431}
432
Willy Tarreau05554e62016-12-21 20:46:26 +0100433/* used to register some de-initialization functions to call after everything
434 * has stopped.
435 */
436void hap_register_post_deinit(void (*fct)())
437{
438 struct post_deinit_fct *b;
439
440 b = calloc(1, sizeof(*b));
441 if (!b) {
442 fprintf(stderr, "out of memory\n");
443 exit(1);
444 }
445 b->fct = fct;
446 LIST_ADDQ(&post_deinit_list, &b->list);
447}
448
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +0200449/* used to register some per proxy de-initialization functions to call after
450 * everything has stopped.
451 */
452void hap_register_proxy_deinit(void (*fct)(struct proxy *))
453{
454 struct proxy_deinit_fct *b;
455
456 b = calloc(1, sizeof(*b));
457 if (!b) {
458 fprintf(stderr, "out of memory\n");
459 exit(1);
460 }
461 b->fct = fct;
462 LIST_ADDQ(&proxy_deinit_list, &b->list);
463}
464
465
466/* used to register some per server de-initialization functions to call after
467 * everything has stopped.
468 */
469void hap_register_server_deinit(void (*fct)(struct server *))
470{
471 struct server_deinit_fct *b;
472
473 b = calloc(1, sizeof(*b));
474 if (!b) {
475 fprintf(stderr, "out of memory\n");
476 exit(1);
477 }
478 b->fct = fct;
479 LIST_ADDQ(&server_deinit_list, &b->list);
480}
481
Willy Tarreau082b6282019-05-22 14:42:12 +0200482/* used to register some allocation functions to call for each thread. */
483void hap_register_per_thread_alloc(int (*fct)())
484{
485 struct per_thread_alloc_fct *b;
486
487 b = calloc(1, sizeof(*b));
488 if (!b) {
489 fprintf(stderr, "out of memory\n");
490 exit(1);
491 }
492 b->fct = fct;
493 LIST_ADDQ(&per_thread_alloc_list, &b->list);
494}
495
Christopher Faulet415f6112017-07-25 16:52:58 +0200496/* used to register some initialization functions to call for each thread. */
497void hap_register_per_thread_init(int (*fct)())
498{
499 struct per_thread_init_fct *b;
500
501 b = calloc(1, sizeof(*b));
502 if (!b) {
503 fprintf(stderr, "out of memory\n");
504 exit(1);
505 }
506 b->fct = fct;
507 LIST_ADDQ(&per_thread_init_list, &b->list);
508}
509
510/* used to register some de-initialization functions to call for each thread. */
511void hap_register_per_thread_deinit(void (*fct)())
512{
513 struct per_thread_deinit_fct *b;
514
515 b = calloc(1, sizeof(*b));
516 if (!b) {
517 fprintf(stderr, "out of memory\n");
518 exit(1);
519 }
520 b->fct = fct;
521 LIST_ADDQ(&per_thread_deinit_list, &b->list);
522}
523
Willy Tarreau082b6282019-05-22 14:42:12 +0200524/* used to register some free functions to call for each thread. */
525void hap_register_per_thread_free(int (*fct)())
526{
527 struct per_thread_free_fct *b;
528
529 b = calloc(1, sizeof(*b));
530 if (!b) {
531 fprintf(stderr, "out of memory\n");
532 exit(1);
533 }
534 b->fct = fct;
535 LIST_ADDQ(&per_thread_free_list, &b->list);
536}
537
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100538static void display_version()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200539{
Tim Duesterhusdfad6a42020-04-18 16:02:47 +0200540 struct utsname utsname;
541
Willy Tarreau08dd2022019-11-21 18:07:30 +0100542 printf("HA-Proxy version %s %s - https://haproxy.org/\n"
543 PRODUCT_STATUS "\n", haproxy_version, haproxy_date);
Willy Tarreau47479eb2019-11-21 18:48:20 +0100544
545 if (strlen(PRODUCT_URL_BUGS) > 0) {
546 char base_version[20];
547 int dots = 0;
548 char *del;
549
550 /* only retrieve the base version without distro-specific extensions */
551 for (del = haproxy_version; *del; del++) {
552 if (*del == '.')
553 dots++;
554 else if (*del < '0' || *del > '9')
555 break;
556 }
557
558 strlcpy2(base_version, haproxy_version, del - haproxy_version + 1);
559 if (dots < 2)
560 printf("Known bugs: https://github.com/haproxy/haproxy/issues?q=is:issue+is:open\n");
561 else
562 printf("Known bugs: " PRODUCT_URL_BUGS "\n", base_version);
563 }
Tim Duesterhusdfad6a42020-04-18 16:02:47 +0200564
565 if (uname(&utsname) == 0) {
566 printf("Running on: %s %s %s %s\n", utsname.sysname, utsname.release, utsname.version, utsname.machine);
567 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200568}
569
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100570static void display_build_opts()
Willy Tarreau7b066db2007-12-02 11:28:59 +0100571{
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100572 struct build_opts_str *item;
573
Willy Tarreau7b066db2007-12-02 11:28:59 +0100574 printf("Build options :"
575#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100576 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100577#endif
578#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100579 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100580#endif
581#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100582 "\n CC = " BUILD_CC
583#endif
584#ifdef BUILD_CFLAGS
585 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100586#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100587#ifdef BUILD_OPTIONS
588 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100589#endif
Willy Tarreau7728ed32019-03-27 13:20:08 +0100590#ifdef BUILD_FEATURES
591 "\n\nFeature list : " BUILD_FEATURES
592#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200593 "\n\nDefault settings :"
Willy Tarreauca783d42019-03-13 10:03:07 +0100594 "\n bufsize = %d, maxrewrite = %d, maxpollevents = %d"
Willy Tarreau27a674e2009-08-17 07:23:33 +0200595 "\n\n",
Willy Tarreauca783d42019-03-13 10:03:07 +0100596 BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS);
Willy Tarreaube5b6852009-10-03 18:57:08 +0200597
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100598 list_for_each_entry(item, &build_opts_list, list) {
599 puts(item->str);
600 }
601
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100602 putchar('\n');
603
Willy Tarreaube5b6852009-10-03 18:57:08 +0200604 list_pollers(stdout);
605 putchar('\n');
Christopher Faulet98d9fe22018-04-10 14:37:32 +0200606 list_mux_proto(stdout);
607 putchar('\n');
Willy Tarreau679bba12019-03-19 08:08:10 +0100608 list_services(stdout);
609 putchar('\n');
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100610 list_filters(stdout);
611 putchar('\n');
Willy Tarreau7b066db2007-12-02 11:28:59 +0100612}
613
Willy Tarreaubaaee002006-06-26 02:48:02 +0200614/*
615 * This function prints the command line usage and exits
616 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100617static void usage(char *name)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618{
619 display_version();
620 fprintf(stderr,
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200621 "Usage : %s [-f <cfgfile|cfgdir>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200622 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
Willy Tarreaua088d312015-10-08 11:58:48 +0200623 " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100624 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200625 " -d enters debug mode ; -db only disables background mode.\n"
Willy Tarreau6e064432012-05-08 15:40:42 +0200626 " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200627 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200628 " -D goes daemon ; -C changes to <dir> before loading files.\n"
William Lallemand095ba4c2017-06-01 17:38:50 +0200629 " -W master-worker mode.\n"
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100630#if defined(USE_SYSTEMD)
631 " -Ws master-worker mode with systemd notify support.\n"
632#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200633 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200634 " -c check mode : only check config files and exit\n"
Willy Tarreauca783d42019-03-13 10:03:07 +0100635 " -n sets the maximum total # of connections (uses ulimit -n)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200636 " -m limits the usable amount of memory (in MB)\n"
637 " -N sets the default, per-proxy maximum # of connections (%d)\n"
Emeric Brun2b920a12010-09-23 18:30:22 +0200638 " -L set local peer name (default to hostname)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200639 " -p writes pids of all children to this file\n"
Willy Tarreaue5733232019-05-22 19:24:06 +0200640#if defined(USE_EPOLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200641 " -de disables epoll() usage even when available\n"
642#endif
Willy Tarreaue5733232019-05-22 19:24:06 +0200643#if defined(USE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200644 " -dk disables kqueue() usage even when available\n"
645#endif
Willy Tarreaue5733232019-05-22 19:24:06 +0200646#if defined(USE_EVPORTS)
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +0000647 " -dv disables event ports usage even when available\n"
648#endif
Willy Tarreaue5733232019-05-22 19:24:06 +0200649#if defined(USE_POLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650 " -dp disables poll() usage even when available\n"
651#endif
Willy Tarreaue5733232019-05-22 19:24:06 +0200652#if defined(USE_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100653 " -dS disables splice usage (broken on old kernels)\n"
654#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200655#if defined(USE_GETADDRINFO)
656 " -dG disables getaddrinfo() usage\n"
657#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000658#if defined(SO_REUSEPORT)
659 " -dR disables SO_REUSEPORT usage\n"
660#endif
Willy Tarreau3eed10e2016-11-07 21:03:16 +0100661 " -dr ignores server address resolution failures\n"
Emeric Brun850efd52014-01-29 12:24:34 +0100662 " -dV disables SSL verify on servers side\n"
Willy Tarreau3eb10b82020-04-15 16:42:39 +0200663 " -dW fails if any warning is emitted\n"
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +0200664 " -sf/-st [pid ]* finishes/terminates old pids.\n"
Olivier Houchardf73629d2017-04-05 22:33:04 +0200665 " -x <unix_socket> get listening sockets from a unix socket\n"
William Lallemand63329e32019-06-13 17:03:37 +0200666 " -S <bind>[,<bind options>...] new master CLI\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667 "\n",
Willy Tarreauca783d42019-03-13 10:03:07 +0100668 name, cfg_maxpconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669 exit(1);
670}
671
672
673
674/*********************************************************************/
675/* more specific functions ***************************************/
676/*********************************************************************/
677
William Lallemand73b85e72017-06-01 17:38:51 +0200678/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
679 * pids the signal was correctly delivered to.
680 */
William Lallemande25473c2019-04-01 11:29:56 +0200681int tell_old_pids(int sig)
William Lallemand73b85e72017-06-01 17:38:51 +0200682{
683 int p;
684 int ret = 0;
685 for (p = 0; p < nb_oldpids; p++)
686 if (kill(oldpids[p], sig) == 0)
687 ret++;
688 return ret;
689}
690
William Lallemand75ea0a02017-11-15 19:02:58 +0100691/*
William Lallemand73b85e72017-06-01 17:38:51 +0200692 * remove a pid forom the olpid array and decrease nb_oldpids
693 * return 1 pid was found otherwise return 0
694 */
695
696int delete_oldpid(int pid)
697{
698 int i;
699
700 for (i = 0; i < nb_oldpids; i++) {
701 if (oldpids[i] == pid) {
702 oldpids[i] = oldpids[nb_oldpids - 1];
703 oldpids[nb_oldpids - 1] = 0;
704 nb_oldpids--;
705 return 1;
706 }
707 }
708 return 0;
709}
710
William Lallemand85b0bd92017-06-01 17:38:53 +0200711
712static void get_cur_unixsocket()
713{
714 /* if -x was used, try to update the stat socket if not available anymore */
715 if (global.stats_fe) {
716 struct bind_conf *bind_conf;
717
718 /* pass through all stats socket */
719 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
720 struct listener *l;
721
722 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
723
724 if (l->addr.ss_family == AF_UNIX &&
725 (bind_conf->level & ACCESS_FD_LISTENERS)) {
726 const struct sockaddr_un *un;
727
728 un = (struct sockaddr_un *)&l->addr;
729 /* priority to old_unixsocket */
730 if (!cur_unixsocket) {
731 cur_unixsocket = strdup(un->sun_path);
732 } else {
733 if (old_unixsocket && !strcmp(un->sun_path, old_unixsocket)) {
734 free(cur_unixsocket);
735 cur_unixsocket = strdup(old_unixsocket);
736 return;
737 }
738 }
739 }
740 }
741 }
742 }
743 if (!cur_unixsocket && old_unixsocket)
744 cur_unixsocket = strdup(old_unixsocket);
745}
746
William Lallemand73b85e72017-06-01 17:38:51 +0200747/*
748 * When called, this function reexec haproxy with -sf followed by current
Joseph Herlant03420902018-11-15 10:41:50 -0800749 * children PIDs and possibly old children PIDs if they didn't leave yet.
William Lallemand73b85e72017-06-01 17:38:51 +0200750 */
William Lallemanda57b7e32018-12-14 21:11:31 +0100751void mworker_reload()
William Lallemand73b85e72017-06-01 17:38:51 +0200752{
William Lallemand00417412020-06-05 14:08:41 +0200753 char **next_argv = NULL;
754 int old_argc = 0; /* previous number of argument */
William Lallemand73b85e72017-06-01 17:38:51 +0200755 int next_argc = 0;
William Lallemand00417412020-06-05 14:08:41 +0200756 int i = 0;
William Lallemand73b85e72017-06-01 17:38:51 +0200757 char *msg = NULL;
Willy Tarreau8dca1952019-03-01 10:21:55 +0100758 struct rlimit limit;
William Lallemand7c756a82018-11-26 11:53:40 +0100759 struct per_thread_deinit_fct *ptdf;
William Lallemand73b85e72017-06-01 17:38:51 +0200760
761 mworker_block_signals();
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100762#if defined(USE_SYSTEMD)
763 if (global.tune.options & GTUNE_USE_SYSTEMD)
764 sd_notify(0, "RELOADING=1");
765#endif
William Lallemand73b85e72017-06-01 17:38:51 +0200766 setenv("HAPROXY_MWORKER_REEXEC", "1", 1);
767
William Lallemandbc193052018-09-11 10:06:26 +0200768 mworker_proc_list_to_env(); /* put the children description in the env */
769
William Lallemand7c756a82018-11-26 11:53:40 +0100770 /* during the reload we must ensure that every FDs that can't be
771 * reuse (ie those that are not referenced in the proc_list)
772 * are closed or they will leak. */
773
774 /* close the listeners FD */
775 mworker_cli_proxy_stop();
William Lallemand16866672019-06-24 17:40:48 +0200776
777 if (getenv("HAPROXY_MWORKER_WAIT_ONLY") == NULL) {
778 /* close the poller FD and the thread waker pipe FD */
779 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
780 ptdf->fct();
781 if (fdtab)
782 deinit_pollers();
783 }
Willy Tarreau5db847a2019-05-09 14:13:35 +0200784#if defined(USE_OPENSSL) && (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
William Lallemand5fdb5b32019-10-15 14:04:08 +0200785 /* close random device FDs */
786 RAND_keep_random_devices_open(0);
Rob Allen56996da2019-05-03 09:11:32 +0100787#endif
William Lallemand7c756a82018-11-26 11:53:40 +0100788
Willy Tarreau8dca1952019-03-01 10:21:55 +0100789 /* restore the initial FD limits */
790 limit.rlim_cur = rlim_fd_cur_at_boot;
791 limit.rlim_max = rlim_fd_max_at_boot;
792 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
793 getrlimit(RLIMIT_NOFILE, &limit);
794 ha_warning("Failed to restore initial FD limits (cur=%u max=%u), using cur=%u max=%u\n",
795 rlim_fd_cur_at_boot, rlim_fd_max_at_boot,
796 (unsigned int)limit.rlim_cur, (unsigned int)limit.rlim_max);
797 }
798
William Lallemand73b85e72017-06-01 17:38:51 +0200799 /* compute length */
William Lallemand00417412020-06-05 14:08:41 +0200800 while (old_argv[old_argc])
801 old_argc++;
William Lallemand73b85e72017-06-01 17:38:51 +0200802
William Lallemand85b0bd92017-06-01 17:38:53 +0200803 /* 1 for haproxy -sf, 2 for -x /socket */
William Lallemand00417412020-06-05 14:08:41 +0200804 next_argv = calloc(old_argc + 1 + 2 + mworker_child_nb() + nb_oldpids + 1, sizeof(char *));
William Lallemand73b85e72017-06-01 17:38:51 +0200805 if (next_argv == NULL)
806 goto alloc_error;
807
William Lallemand00417412020-06-05 14:08:41 +0200808 /* copy the program name */
809 next_argv[next_argc++] = old_argv[0];
810
811 /* insert the new options just after argv[0] in case we have a -- */
812
William Lallemand73b85e72017-06-01 17:38:51 +0200813 /* add -sf <PID>* to argv */
William Lallemand3f128872019-04-01 11:29:59 +0200814 if (mworker_child_nb() > 0) {
815 struct mworker_proc *child;
816
William Lallemand73b85e72017-06-01 17:38:51 +0200817 next_argv[next_argc++] = "-sf";
William Lallemand3f128872019-04-01 11:29:59 +0200818
819 list_for_each_entry(child, &proc_list, list) {
William Lallemand677e2f22019-11-19 17:04:18 +0100820 if (!(child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) || child->pid <= -1 )
William Lallemand3f128872019-04-01 11:29:59 +0200821 continue;
William Lallemand00417412020-06-05 14:08:41 +0200822 if ((next_argv[next_argc++] = memprintf(&msg, "%d", child->pid)) == NULL)
William Lallemand73b85e72017-06-01 17:38:51 +0200823 goto alloc_error;
824 msg = NULL;
825 }
826 }
William Lallemand2bf6d622017-06-20 11:20:23 +0200827 /* add the -x option with the stat socket */
William Lallemand85b0bd92017-06-01 17:38:53 +0200828 if (cur_unixsocket) {
William Lallemand2bf6d622017-06-20 11:20:23 +0200829 next_argv[next_argc++] = "-x";
830 next_argv[next_argc++] = (char *)cur_unixsocket;
William Lallemand85b0bd92017-06-01 17:38:53 +0200831 }
832
William Lallemand00417412020-06-05 14:08:41 +0200833 /* copy the previous options */
834 for (i = 1; i < old_argc; i++)
835 next_argv[next_argc++] = old_argv[i];
836
Christopher Faulet767a84b2017-11-24 16:50:31 +0100837 ha_warning("Reexecuting Master process\n");
Willy Tarreaue0d86e22019-08-26 10:37:39 +0200838 signal(SIGPROF, SIG_IGN);
Tim Duesterhus0436ab72017-11-12 17:39:18 +0100839 execvp(next_argv[0], next_argv);
William Lallemand73b85e72017-06-01 17:38:51 +0200840
Christopher Faulet767a84b2017-11-24 16:50:31 +0100841 ha_warning("Failed to reexecute the master process [%d]: %s\n", pid, strerror(errno));
William Lallemand9fc6c972020-06-08 10:01:13 +0200842 free(next_argv);
843 next_argv = NULL;
William Lallemand722d4ca2017-11-15 19:02:55 +0100844 return;
845
William Lallemand73b85e72017-06-01 17:38:51 +0200846alloc_error:
William Lallemand00417412020-06-05 14:08:41 +0200847 free(next_argv);
848 next_argv = NULL;
Joseph Herlant07a08342018-11-15 10:43:05 -0800849 ha_warning("Failed to reexecute the master process [%d]: Cannot allocate memory\n", pid);
William Lallemand73b85e72017-06-01 17:38:51 +0200850 return;
851}
852
William Lallemandb3f2be32018-09-11 10:06:18 +0200853static void mworker_loop()
854{
855
856#if defined(USE_SYSTEMD)
857 if (global.tune.options & GTUNE_USE_SYSTEMD)
858 sd_notifyf(0, "READY=1\nMAINPID=%lu", (unsigned long)getpid());
859#endif
Willy Tarreaud83b6c12019-04-18 11:31:36 +0200860 /* Busy polling makes no sense in the master :-) */
861 global.tune.options &= ~GTUNE_BUSY_POLLING;
William Lallemandb3f2be32018-09-11 10:06:18 +0200862
William Lallemandbc193052018-09-11 10:06:26 +0200863 master = 1;
864
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100865 signal_unregister(SIGTTIN);
866 signal_unregister(SIGTTOU);
William Lallemand0564d412018-11-20 17:36:53 +0100867 signal_unregister(SIGUSR1);
868 signal_unregister(SIGHUP);
869 signal_unregister(SIGQUIT);
870
William Lallemandb3f2be32018-09-11 10:06:18 +0200871 signal_register_fct(SIGTERM, mworker_catch_sigterm, SIGTERM);
872 signal_register_fct(SIGUSR1, mworker_catch_sigterm, SIGUSR1);
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100873 signal_register_fct(SIGTTIN, mworker_broadcast_signal, SIGTTIN);
874 signal_register_fct(SIGTTOU, mworker_broadcast_signal, SIGTTOU);
William Lallemandb3f2be32018-09-11 10:06:18 +0200875 signal_register_fct(SIGINT, mworker_catch_sigterm, SIGINT);
876 signal_register_fct(SIGHUP, mworker_catch_sighup, SIGHUP);
877 signal_register_fct(SIGUSR2, mworker_catch_sighup, SIGUSR2);
878 signal_register_fct(SIGCHLD, mworker_catch_sigchld, SIGCHLD);
879
880 mworker_unblock_signals();
881 mworker_cleanlisteners();
William Lallemand27f3fa52018-12-06 14:05:20 +0100882 mworker_cleantasks();
William Lallemandb3f2be32018-09-11 10:06:18 +0200883
William Lallemandbc193052018-09-11 10:06:26 +0200884 mworker_catch_sigchld(NULL); /* ensure we clean the children in case
885 some SIGCHLD were lost */
886
William Lallemandb3f2be32018-09-11 10:06:18 +0200887 global.nbthread = 1;
888 relative_pid = 1;
889 pid_bit = 1;
Willy Tarreaua38a7172019-02-02 17:11:28 +0100890 all_proc_mask = 1;
William Lallemandb3f2be32018-09-11 10:06:18 +0200891
William Lallemand2672eb92018-12-14 15:52:39 +0100892#ifdef USE_THREAD
893 tid_bit = 1;
894 all_threads_mask = 1;
895#endif
896
William Lallemandb3f2be32018-09-11 10:06:18 +0200897 jobs++; /* this is the "master" job, we want to take care of the
898 signals even if there is no listener so the poll loop don't
899 leave */
900
901 fork_poller();
Willy Tarreaub4f7cc32019-05-03 09:27:30 +0200902 run_thread_poll_loop(0);
William Lallemandb3f2be32018-09-11 10:06:18 +0200903}
William Lallemandcb11fd22017-06-01 17:38:52 +0200904
905/*
906 * Reexec the process in failure mode, instead of exiting
907 */
908void reexec_on_failure()
909{
910 if (!atexit_flag)
911 return;
912
913 setenv("HAPROXY_MWORKER_WAIT_ONLY", "1", 1);
914
Christopher Faulet767a84b2017-11-24 16:50:31 +0100915 ha_warning("Reexecuting Master process in waitpid mode\n");
William Lallemandcb11fd22017-06-01 17:38:52 +0200916 mworker_reload();
William Lallemandcb11fd22017-06-01 17:38:52 +0200917}
William Lallemand73b85e72017-06-01 17:38:51 +0200918
919
920/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200921 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
922 * a signal zero to all subscribers. This means that it's as easy as
923 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100925static void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200926{
927 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200928 signal_unregister_handler(sh);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100929 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930}
931
932/*
933 * upon SIGTTOU, we pause everything
934 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100935static void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200936{
937 pause_proxies();
Willy Tarreaubafbe012017-11-24 17:34:44 +0100938 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200939}
940
941/*
942 * upon SIGTTIN, let's have a soft stop.
943 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100944static void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945{
Willy Tarreaube58c382011-07-24 18:28:10 +0200946 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200947}
948
949/*
950 * this function dumps every server's state when the process receives SIGHUP.
951 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100952static void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200953{
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100954 struct proxy *p = proxies_list;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200955
Christopher Faulet767a84b2017-11-24 16:50:31 +0100956 ha_warning("SIGHUP received, dumping servers states.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200957 while (p) {
958 struct server *s = p->srv;
959
960 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
961 while (s) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100962 chunk_printf(&trash,
963 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
964 p->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +0200965 (s->cur_state != SRV_ST_STOPPED) ? "UP" : "DOWN",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100966 s->cur_sess, s->nbpend, s->counters.cum_sess);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200967 ha_warning("%s\n", trash.area);
968 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200969 s = s->next;
970 }
971
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200972 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
973 if (!p->srv) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100974 chunk_printf(&trash,
975 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
976 p->id,
977 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 +0200978 } else if (p->srv_act == 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100979 chunk_printf(&trash,
980 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
981 p->id,
982 (p->srv_bck) ? "is running on backup servers" : "has no server available",
983 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 +0200984 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100985 chunk_printf(&trash,
986 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
987 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
988 p->id, p->srv_act, p->srv_bck,
989 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 +0200990 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200991 ha_warning("%s\n", trash.area);
992 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200993
994 p = p->next;
995 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200996}
997
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100998static void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200999{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001000 /* dump memory usage then free everything possible */
1001 dump_pools();
Willy Tarreaubafbe012017-11-24 17:34:44 +01001002 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001003}
1004
William Lallemande1340412017-12-28 16:09:36 +01001005/*
1006 * This function dup2 the stdio FDs (0,1,2) with <fd>, then closes <fd>
1007 * If <fd> < 0, it opens /dev/null and use it to dup
1008 *
1009 * In the case of chrooting, you have to open /dev/null before the chroot, and
1010 * pass the <fd> to this function
1011 */
1012static void stdio_quiet(int fd)
1013{
1014 if (fd < 0)
1015 fd = open("/dev/null", O_RDWR, 0);
1016
1017 if (fd > -1) {
1018 fclose(stdin);
1019 fclose(stdout);
1020 fclose(stderr);
1021
1022 dup2(fd, 0);
1023 dup2(fd, 1);
1024 dup2(fd, 2);
1025 if (fd > 2)
1026 close(fd);
1027 return;
1028 }
1029
1030 ha_alert("Cannot open /dev/null\n");
1031 exit(EXIT_FAILURE);
1032}
1033
1034
Joseph Herlant03420902018-11-15 10:41:50 -08001035/* This function checks if cfg_cfgfiles contains directories.
1036 * If it finds one, it adds all the files (and only files) it contains
1037 * in cfg_cfgfiles in place of the directory (and removes the directory).
1038 * It adds the files in lexical order.
1039 * It adds only files with .cfg extension.
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001040 * It doesn't add files with name starting with '.'
1041 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001042static void cfgfiles_expand_directories(void)
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001043{
1044 struct wordlist *wl, *wlb;
1045 char *err = NULL;
1046
1047 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
1048 struct stat file_stat;
1049 struct dirent **dir_entries = NULL;
1050 int dir_entries_nb;
1051 int dir_entries_it;
1052
1053 if (stat(wl->s, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001054 ha_alert("Cannot open configuration file/directory %s : %s\n",
1055 wl->s,
1056 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001057 exit(1);
1058 }
1059
1060 if (!S_ISDIR(file_stat.st_mode))
1061 continue;
1062
1063 /* from this point wl->s is a directory */
1064
1065 dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort);
1066 if (dir_entries_nb < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001067 ha_alert("Cannot open configuration directory %s : %s\n",
1068 wl->s,
1069 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001070 exit(1);
1071 }
1072
1073 /* for each element in the directory wl->s */
1074 for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) {
1075 struct dirent *dir_entry = dir_entries[dir_entries_it];
1076 char *filename = NULL;
1077 char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg");
1078
1079 /* don't add filename that begin with .
Joseph Herlant03420902018-11-15 10:41:50 -08001080 * only add filename with .cfg extension
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001081 */
1082 if (dir_entry->d_name[0] == '.' ||
1083 !(d_name_cfgext && d_name_cfgext[4] == '\0'))
1084 goto next_dir_entry;
1085
1086 if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001087 ha_alert("Cannot load configuration files %s : out of memory.\n",
1088 filename);
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001089 exit(1);
1090 }
1091
1092 if (stat(filename, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001093 ha_alert("Cannot open configuration file %s : %s\n",
1094 wl->s,
1095 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001096 exit(1);
1097 }
1098
1099 /* don't add anything else than regular file in cfg_cfgfiles
1100 * this way we avoid loops
1101 */
1102 if (!S_ISREG(file_stat.st_mode))
1103 goto next_dir_entry;
1104
1105 if (!list_append_word(&wl->list, filename, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001106 ha_alert("Cannot load configuration files %s : %s\n",
1107 filename,
1108 err);
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001109 exit(1);
1110 }
1111
1112next_dir_entry:
1113 free(filename);
1114 free(dir_entry);
1115 }
1116
1117 free(dir_entries);
1118
1119 /* remove the current directory (wl) from cfg_cfgfiles */
1120 free(wl->s);
1121 LIST_DEL(&wl->list);
1122 free(wl);
1123 }
1124
1125 free(err);
1126}
1127
Olivier Houchardf73629d2017-04-05 22:33:04 +02001128static int get_old_sockets(const char *unixsocket)
1129{
1130 char *cmsgbuf = NULL, *tmpbuf = NULL;
1131 int *tmpfd = NULL;
1132 struct sockaddr_un addr;
1133 struct cmsghdr *cmsg;
1134 struct msghdr msghdr;
1135 struct iovec iov;
1136 struct xfer_sock_list *xfer_sock = NULL;
Olivier Houchard54740872017-04-06 14:45:14 +02001137 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf73629d2017-04-05 22:33:04 +02001138 int sock = -1;
1139 int ret = -1;
1140 int ret2 = -1;
1141 int fd_nb;
1142 int got_fd = 0;
1143 int i = 0;
1144 size_t maxoff = 0, curoff = 0;
1145
1146 memset(&msghdr, 0, sizeof(msghdr));
1147 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
1148 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001149 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001150 goto out;
1151 }
1152 sock = socket(PF_UNIX, SOCK_STREAM, 0);
1153 if (sock < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001154 ha_warning("Failed to connect to the old process socket '%s'\n",
1155 unixsocket);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001156 goto out;
1157 }
Willy Tarreau719e07c2019-12-11 16:29:10 +01001158 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path) - 1);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001159 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
1160 addr.sun_family = PF_UNIX;
1161 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
1162 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001163 ha_warning("Failed to connect to the old process socket '%s'\n",
1164 unixsocket);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001165 goto out;
1166 }
Olivier Houchard54740872017-04-06 14:45:14 +02001167 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001168 iov.iov_base = &fd_nb;
1169 iov.iov_len = sizeof(fd_nb);
1170 msghdr.msg_iov = &iov;
1171 msghdr.msg_iovlen = 1;
1172 send(sock, "_getsocks\n", strlen("_getsocks\n"), 0);
1173 /* First, get the number of file descriptors to be received */
1174 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001175 ha_warning("Failed to get the number of sockets to be transferred !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001176 goto out;
1177 }
1178 if (fd_nb == 0) {
1179 ret = 0;
1180 goto out;
1181 }
Olivier Houchardf143b802017-11-04 15:13:01 +01001182 tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001183 if (tmpbuf == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001184 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001185 goto out;
1186 }
1187 tmpfd = malloc(fd_nb * sizeof(int));
1188 if (tmpfd == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001189 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001190 goto out;
1191 }
1192 msghdr.msg_control = cmsgbuf;
1193 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
Olivier Houchardf143b802017-11-04 15:13:01 +01001194 iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001195 do {
1196 int ret3;
1197
1198 iov.iov_base = tmpbuf + curoff;
1199 ret = recvmsg(sock, &msghdr, 0);
1200 if (ret == -1 && errno == EINTR)
1201 continue;
1202 if (ret <= 0)
1203 break;
1204 /* Send an ack to let the sender know we got the sockets
1205 * and it can send some more
1206 */
1207 do {
1208 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
1209 } while (ret3 == -1 && errno == EINTR);
1210 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
1211 cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
1212 if (cmsg->cmsg_level == SOL_SOCKET &&
1213 cmsg->cmsg_type == SCM_RIGHTS) {
1214 size_t totlen = cmsg->cmsg_len -
1215 CMSG_LEN(0);
1216 if (totlen / sizeof(int) + got_fd > fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001217 ha_warning("Got to many sockets !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001218 goto out;
1219 }
1220 /*
1221 * Be paranoid and use memcpy() to avoid any
1222 * potential alignement issue.
1223 */
1224 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
1225 got_fd += totlen / sizeof(int);
1226 }
1227 }
1228 curoff += ret;
1229 } while (got_fd < fd_nb);
1230
1231 if (got_fd != fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001232 ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
1233 fd_nb, got_fd);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001234 goto out;
1235 }
1236 maxoff = curoff;
1237 curoff = 0;
1238 for (i = 0; i < got_fd; i++) {
1239 int fd = tmpfd[i];
1240 socklen_t socklen;
1241 int len;
1242
1243 xfer_sock = calloc(1, sizeof(*xfer_sock));
1244 if (!xfer_sock) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001245 ha_warning("Failed to allocate memory in get_old_sockets() !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001246 break;
1247 }
1248 xfer_sock->fd = -1;
1249
1250 socklen = sizeof(xfer_sock->addr);
1251 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001252 ha_warning("Failed to get socket address\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001253 free(xfer_sock);
Olivier Houchardbe7b1ce2017-07-17 17:25:33 +02001254 xfer_sock = NULL;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001255 continue;
1256 }
1257 if (curoff >= maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001258 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001259 goto out;
1260 }
1261 len = tmpbuf[curoff++];
1262 if (len > 0) {
1263 /* We have a namespace */
1264 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001265 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001266 goto out;
1267 }
1268 xfer_sock->namespace = malloc(len + 1);
1269 if (!xfer_sock->namespace) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001270 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001271 goto out;
1272 }
1273 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
1274 xfer_sock->namespace[len] = 0;
1275 curoff += len;
1276 }
1277 if (curoff >= maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001278 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001279 goto out;
1280 }
1281 len = tmpbuf[curoff++];
1282 if (len > 0) {
1283 /* We have an interface */
1284 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001285 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001286 goto out;
1287 }
1288 xfer_sock->iface = malloc(len + 1);
1289 if (!xfer_sock->iface) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001290 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001291 goto out;
1292 }
1293 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
Olivier Houchard33e083c2018-03-15 17:48:49 +01001294 xfer_sock->iface[len] = 0;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001295 curoff += len;
1296 }
1297 if (curoff + sizeof(int) > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001298 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001299 goto out;
1300 }
1301 memcpy(&xfer_sock->options, &tmpbuf[curoff],
1302 sizeof(xfer_sock->options));
1303 curoff += sizeof(xfer_sock->options);
1304
1305 xfer_sock->fd = fd;
1306 if (xfer_sock_list)
1307 xfer_sock_list->prev = xfer_sock;
1308 xfer_sock->next = xfer_sock_list;
1309 xfer_sock->prev = NULL;
1310 xfer_sock_list = xfer_sock;
1311 xfer_sock = NULL;
1312 }
1313
1314 ret2 = 0;
1315out:
1316 /* If we failed midway make sure to close the remaining
1317 * file descriptors
1318 */
1319 if (tmpfd != NULL && i < got_fd) {
1320 for (; i < got_fd; i++) {
1321 close(tmpfd[i]);
1322 }
1323 }
1324 free(tmpbuf);
1325 free(tmpfd);
1326 free(cmsgbuf);
1327 if (sock != -1)
1328 close(sock);
1329 if (xfer_sock) {
1330 free(xfer_sock->namespace);
1331 free(xfer_sock->iface);
1332 if (xfer_sock->fd != -1)
1333 close(xfer_sock->fd);
1334 free(xfer_sock);
1335 }
1336 return (ret2);
1337}
1338
Willy Tarreaubaaee002006-06-26 02:48:02 +02001339/*
William Lallemand73b85e72017-06-01 17:38:51 +02001340 * copy and cleanup the current argv
William Lallemanddf6c5a82020-06-04 17:40:23 +02001341 * Remove the -sf /-st / -x parameters
William Lallemand73b85e72017-06-01 17:38:51 +02001342 * Return an allocated copy of argv
1343 */
1344
1345static char **copy_argv(int argc, char **argv)
1346{
William Lallemanddf6c5a82020-06-04 17:40:23 +02001347 char **newargv, **retargv;
William Lallemand73b85e72017-06-01 17:38:51 +02001348
1349 newargv = calloc(argc + 2, sizeof(char *));
1350 if (newargv == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001351 ha_warning("Cannot allocate memory\n");
William Lallemand73b85e72017-06-01 17:38:51 +02001352 return NULL;
1353 }
William Lallemanddf6c5a82020-06-04 17:40:23 +02001354 retargv = newargv;
William Lallemand73b85e72017-06-01 17:38:51 +02001355
William Lallemanddf6c5a82020-06-04 17:40:23 +02001356 /* first copy argv[0] */
1357 *newargv++ = *argv++;
1358 argc--;
1359
1360 while (argc > 0) {
1361 if (**argv != '-') {
1362 /* non options are copied but will fail in the argument parser */
1363 *newargv++ = *argv++;
1364 argc--;
1365
1366 } else {
1367 char *flag;
1368
1369 flag = *argv + 1;
1370
1371 if (flag[0] == '-' && flag[1] == 0) {
1372 /* "--\0" copy every arguments till the end of argv */
1373 *newargv++ = *argv++;
1374 argc--;
1375
1376 while (argc > 0) {
1377 *newargv++ = *argv++;
1378 argc--;
1379 }
1380 } else {
1381 switch (*flag) {
1382 case 's':
1383 /* -sf / -st and their parameters are ignored */
1384 if (flag[1] == 'f' || flag[1] == 't') {
1385 argc--;
1386 argv++;
1387 /* The list can't contain a negative value since the only
1388 way to know the end of this list is by looking for the
1389 next option or the end of the options */
1390 while (argc > 0 && argv[0][0] != '-') {
1391 argc--;
1392 argv++;
1393 }
1394 }
1395 break;
1396
1397 case 'x':
1398 /* this option and its parameter are ignored */
1399 argc--;
1400 argv++;
1401 if (argc > 0) {
1402 argc--;
1403 argv++;
1404 }
1405 break;
1406
1407 case 'C':
1408 case 'n':
1409 case 'm':
1410 case 'N':
1411 case 'L':
1412 case 'f':
1413 case 'p':
1414 case 'S':
1415 /* these options have only 1 parameter which must be copied and can start with a '-' */
1416 *newargv++ = *argv++;
1417 argc--;
1418 if (argc == 0)
1419 goto error;
1420 *newargv++ = *argv++;
1421 argc--;
1422 break;
1423 default:
1424 /* for other options just copy them without parameters, this is also done
1425 * for options like "--foo", but this will fail in the argument parser.
1426 * */
1427 *newargv++ = *argv++;
1428 argc--;
1429 break;
1430 }
William Lallemand73b85e72017-06-01 17:38:51 +02001431 }
1432 }
William Lallemand73b85e72017-06-01 17:38:51 +02001433 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001434
William Lallemanddf6c5a82020-06-04 17:40:23 +02001435 return retargv;
1436
1437error:
1438 free(retargv);
1439 return NULL;
William Lallemand73b85e72017-06-01 17:38:51 +02001440}
1441
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001442
1443/* Performs basic random seed initialization. The main issue with this is that
1444 * srandom_r() only takes 32 bits and purposely provides a reproducible sequence,
1445 * which means that there will only be 4 billion possible random sequences once
1446 * srandom() is called, regardless of the internal state. Not calling it is
1447 * even worse as we'll always produce the same randoms sequences. What we do
1448 * here is to create an initial sequence from various entropy sources, hash it
1449 * using SHA1 and keep the resulting 160 bits available globally.
1450 *
1451 * We initialize the current process with the first 32 bits before starting the
1452 * polling loop, where all this will be changed to have process specific and
1453 * thread specific sequences.
Willy Tarreau52bf8392020-03-08 00:42:37 +01001454 *
1455 * Before starting threads, it's still possible to call random() as srandom()
1456 * is initialized from this, but after threads and/or processes are started,
1457 * only ha_random() is expected to be used to guarantee distinct sequences.
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001458 */
1459static void ha_random_boot(char *const *argv)
1460{
1461 unsigned char message[256];
1462 unsigned char *m = message;
1463 struct timeval tv;
1464 blk_SHA_CTX ctx;
1465 unsigned long l;
1466 int fd;
1467 int i;
1468
1469 /* start with current time as pseudo-random seed */
1470 gettimeofday(&tv, NULL);
1471 write_u32(m, tv.tv_sec); m += 4;
1472 write_u32(m, tv.tv_usec); m += 4;
1473
1474 /* PID and PPID add some OS-based randomness */
1475 write_u16(m, getpid()); m += 2;
1476 write_u16(m, getppid()); m += 2;
1477
1478 /* take up to 160 bits bytes from /dev/urandom if available (non-blocking) */
1479 fd = open("/dev/urandom", O_RDONLY);
1480 if (fd >= 0) {
1481 i = read(fd, m, 20);
1482 if (i > 0)
1483 m += i;
1484 close(fd);
1485 }
1486
1487 /* take up to 160 bits bytes from openssl (non-blocking) */
1488#ifdef USE_OPENSSL
1489 if (RAND_bytes(m, 20) == 1)
1490 m += 20;
1491#endif
1492
1493 /* take 160 bits from existing random in case it was already initialized */
1494 for (i = 0; i < 5; i++) {
1495 write_u32(m, random());
1496 m += 4;
1497 }
1498
1499 /* stack address (benefit form operating system's ASLR) */
1500 l = (unsigned long)&m;
1501 memcpy(m, &l, sizeof(l)); m += sizeof(l);
1502
1503 /* argv address (benefit form operating system's ASLR) */
1504 l = (unsigned long)&argv;
1505 memcpy(m, &l, sizeof(l)); m += sizeof(l);
1506
1507 /* use tv_usec again after all the operations above */
1508 gettimeofday(&tv, NULL);
1509 write_u32(m, tv.tv_usec); m += 4;
1510
1511 /*
1512 * At this point, ~84-92 bytes have been used
1513 */
1514
1515 /* finish with the hostname */
1516 strncpy((char *)m, hostname, message + sizeof(message) - m);
1517 m += strlen(hostname);
1518
1519 /* total message length */
1520 l = m - message;
1521
1522 memset(&ctx, 0, sizeof(ctx));
1523 blk_SHA1_Init(&ctx);
1524 blk_SHA1_Update(&ctx, message, l);
1525 blk_SHA1_Final(boot_seed, &ctx);
1526
1527 srandom(read_u32(boot_seed));
Willy Tarreau52bf8392020-03-08 00:42:37 +01001528 ha_random_seed(boot_seed, sizeof(boot_seed));
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001529}
1530
Willy Tarreau5a023f02019-03-01 14:19:31 +01001531/* considers splicing proxies' maxconn, computes the ideal global.maxpipes
1532 * setting, and returns it. It may return -1 meaning "unlimited" if some
1533 * unlimited proxies have been found and the global.maxconn value is not yet
1534 * set. It may also return a value greater than maxconn if it's not yet set.
1535 * Note that a value of zero means there is no need for pipes. -1 is never
1536 * returned if global.maxconn is valid.
1537 */
1538static int compute_ideal_maxpipes()
1539{
1540 struct proxy *cur;
1541 int nbfe = 0, nbbe = 0;
1542 int unlimited = 0;
1543 int pipes;
1544 int max;
1545
1546 for (cur = proxies_list; cur; cur = cur->next) {
1547 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
1548 if (cur->cap & PR_CAP_FE) {
1549 max = cur->maxconn;
1550 nbfe += max;
1551 if (!max) {
1552 unlimited = 1;
1553 break;
1554 }
1555 }
1556 if (cur->cap & PR_CAP_BE) {
1557 max = cur->fullconn ? cur->fullconn : global.maxconn;
1558 nbbe += max;
1559 if (!max) {
1560 unlimited = 1;
1561 break;
1562 }
1563 }
1564 }
1565 }
1566
1567 pipes = MAX(nbfe, nbbe);
1568 if (global.maxconn) {
1569 if (pipes > global.maxconn || unlimited)
1570 pipes = global.maxconn;
1571 } else if (unlimited) {
1572 pipes = -1;
1573 }
1574
1575 return pipes >= 4 ? pipes / 4 : pipes;
1576}
1577
Willy Tarreauac350932019-03-01 15:43:14 +01001578/* considers global.maxsocks, global.maxpipes, async engines, SSL frontends and
1579 * rlimits and computes an ideal maxconn. It's meant to be called only when
1580 * maxsock contains the sum of listening FDs, before it is updated based on
Willy Tarreaudf23c0c2019-03-13 10:10:49 +01001581 * maxconn and pipes. If there are not enough FDs left, DEFAULT_MAXCONN (by
1582 * default 100) is returned as it is expected that it will even run on tight
1583 * environments, and will maintain compatibility with previous packages that
1584 * used to rely on this value as the default one. The system will emit a
1585 * warning indicating how many FDs are missing anyway if needed.
Willy Tarreauac350932019-03-01 15:43:14 +01001586 */
1587static int compute_ideal_maxconn()
1588{
1589 int ssl_sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1590 int engine_fds = global.ssl_used_async_engines * ssl_sides;
1591 int pipes = compute_ideal_maxpipes();
Willy Tarreaub1beaa32020-03-06 10:25:31 +01001592 int remain = MAX(rlim_fd_cur_at_boot, rlim_fd_max_at_boot);
Willy Tarreauac350932019-03-01 15:43:14 +01001593 int maxconn;
1594
1595 /* we have to take into account these elements :
1596 * - number of engine_fds, which inflates the number of FD needed per
1597 * connection by this number.
1598 * - number of pipes per connection on average : for the unlimited
1599 * case, this is 0.5 pipe FDs per connection, otherwise it's a
1600 * fixed value of 2*pipes.
1601 * - two FDs per connection
1602 */
1603
1604 /* subtract listeners and checks */
1605 remain -= global.maxsock;
1606
Willy Tarreau3f200852019-03-14 19:13:17 +01001607 /* one epoll_fd/kqueue_fd per thread */
1608 remain -= global.nbthread;
1609
1610 /* one wake-up pipe (2 fd) per thread */
1611 remain -= 2 * global.nbthread;
1612
Willy Tarreauac350932019-03-01 15:43:14 +01001613 /* Fixed pipes values : we only subtract them if they're not larger
1614 * than the remaining FDs because pipes are optional.
1615 */
1616 if (pipes >= 0 && pipes * 2 < remain)
1617 remain -= pipes * 2;
1618
1619 if (pipes < 0) {
1620 /* maxsock = maxconn * 2 + maxconn/4 * 2 + maxconn * engine_fds.
1621 * = maxconn * (2 + 0.5 + engine_fds)
1622 * = maxconn * (4 + 1 + 2*engine_fds) / 2
1623 */
1624 maxconn = 2 * remain / (5 + 2 * engine_fds);
1625 } else {
1626 /* maxsock = maxconn * 2 + maxconn * engine_fds.
1627 * = maxconn * (2 + engine_fds)
1628 */
1629 maxconn = remain / (2 + engine_fds);
1630 }
1631
Willy Tarreaudf23c0c2019-03-13 10:10:49 +01001632 return MAX(maxconn, DEFAULT_MAXCONN);
Willy Tarreauac350932019-03-01 15:43:14 +01001633}
1634
Willy Tarreaua409f302020-03-10 17:08:53 +01001635/* computes the estimated maxsock value for the given maxconn based on the
1636 * possibly set global.maxpipes and existing partial global.maxsock. It may
1637 * temporarily change global.maxconn for the time needed to propagate the
1638 * computations, and will reset it.
1639 */
1640static int compute_ideal_maxsock(int maxconn)
1641{
1642 int maxpipes = global.maxpipes;
1643 int maxsock = global.maxsock;
1644
1645
1646 if (!maxpipes) {
1647 int old_maxconn = global.maxconn;
1648
1649 global.maxconn = maxconn;
1650 maxpipes = compute_ideal_maxpipes();
1651 global.maxconn = old_maxconn;
1652 }
1653
1654 maxsock += maxconn * 2; /* each connection needs two sockets */
1655 maxsock += maxpipes * 2; /* each pipe needs two FDs */
1656 maxsock += global.nbthread; /* one epoll_fd/kqueue_fd per thread */
1657 maxsock += 2 * global.nbthread; /* one wake-up pipe (2 fd) per thread */
1658
1659 /* compute fd used by async engines */
1660 if (global.ssl_used_async_engines) {
1661 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1662
1663 maxsock += maxconn * sides * global.ssl_used_async_engines;
1664 }
1665 return maxsock;
1666}
1667
Willy Tarreau304e17e2020-03-10 17:54:54 +01001668/* Tests if it is possible to set the current process' RLIMIT_NOFILE to
1669 * <maxsock>, then sets it back to the previous value. Returns non-zero if the
1670 * value is accepted, non-zero otherwise. This is used to determine if an
1671 * automatic limit may be applied or not. When it is not, the caller knows that
1672 * the highest we can do is the rlim_max at boot. In case of error, we return
1673 * that the setting is possible, so that we defer the error processing to the
1674 * final stage in charge of enforcing this.
1675 */
1676static int check_if_maxsock_permitted(int maxsock)
1677{
1678 struct rlimit orig_limit, test_limit;
1679 int ret;
1680
1681 if (getrlimit(RLIMIT_NOFILE, &orig_limit) != 0)
1682 return 1;
1683
1684 /* don't go further if we can't even set to what we have */
1685 if (setrlimit(RLIMIT_NOFILE, &orig_limit) != 0)
1686 return 1;
1687
1688 test_limit.rlim_max = MAX(maxsock, orig_limit.rlim_max);
1689 test_limit.rlim_cur = test_limit.rlim_max;
1690 ret = setrlimit(RLIMIT_NOFILE, &test_limit);
1691
1692 if (setrlimit(RLIMIT_NOFILE, &orig_limit) != 0)
1693 return 1;
1694
1695 return ret == 0;
1696}
1697
1698
William Lallemand73b85e72017-06-01 17:38:51 +02001699/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001700 * This function initializes all the necessary variables. It only returns
1701 * if everything is OK. If something fails, it exits.
1702 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001703static void init(int argc, char **argv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001704{
Willy Tarreaubaaee002006-06-26 02:48:02 +02001705 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001706 char *tmp;
1707 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +02001708 int err_code = 0;
Maxime de Roucy0f503922016-05-13 23:52:55 +02001709 char *err_msg = NULL;
Willy Tarreau477ecd82010-01-03 21:12:30 +01001710 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +00001711 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +02001712 char *change_dir = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001713 struct proxy *px;
Willy Tarreaue6945732016-12-21 19:57:00 +01001714 struct post_check_fct *pcf;
Willy Tarreauac350932019-03-01 15:43:14 +01001715 int ideal_maxconn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001716
Christopher Faulete3a5e352017-10-24 13:53:54 +02001717 global.mode = MODE_STARTING;
William Lallemand00417412020-06-05 14:08:41 +02001718 old_argv = copy_argv(argc, argv);
1719 if (!old_argv) {
William Lallemanddf6c5a82020-06-04 17:40:23 +02001720 ha_alert("failed to copy argv.\n");
1721 exit(1);
1722 }
William Lallemand73b85e72017-06-01 17:38:51 +02001723
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001724 if (!init_trash_buffers(1)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001725 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet748919a2017-07-26 14:59:46 +02001726 exit(1);
1727 }
David du Colombier7af46052012-05-16 14:16:48 +02001728
Emeric Brun2b920a12010-09-23 18:30:22 +02001729 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
1730 * the string in case of truncation, and at least FreeBSD appears not to do
1731 * it.
1732 */
1733 memset(hostname, 0, sizeof(hostname));
1734 gethostname(hostname, sizeof(hostname) - 1);
1735 memset(localpeer, 0, sizeof(localpeer));
1736 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
William Lallemanddaf4cd22018-04-17 16:46:13 +02001737 setenv("HAPROXY_LOCALPEER", localpeer, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02001738
William Lallemand24c928c2020-01-14 17:58:18 +01001739 /* we were in mworker mode, we should restart in mworker mode */
1740 if (getenv("HAPROXY_MWORKER_REEXEC") != NULL)
1741 global.mode |= MODE_MWORKER;
1742
Willy Tarreaubaaee002006-06-26 02:48:02 +02001743 /*
1744 * Initialize the previously static variables.
1745 */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001746
Willy Tarreau173d9952018-01-26 21:48:23 +01001747 totalconn = actconn = listeners = stopping = 0;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001748 killed = 0;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001749
Willy Tarreaubaaee002006-06-26 02:48:02 +02001750
1751#ifdef HAPROXY_MEMMAX
Willy Tarreau70060452015-12-14 12:46:07 +01001752 global.rlimit_memmax_all = HAPROXY_MEMMAX;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001753#endif
1754
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001755 tzset();
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001756 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001757 start_date = now;
1758
Willy Tarreau6c3a6812020-03-06 18:57:15 +01001759 ha_random_boot(argv);
Willy Tarreau84310e22014-02-14 11:59:04 +01001760
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001761 if (init_acl() != 0)
1762 exit(1);
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001763
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001764 /* Initialise lua. */
1765 hlua_init();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001766
Christopher Fauletff2613e2016-11-09 11:36:17 +01001767 /* Initialize process vars */
1768 vars_init(&global.vars, SCOPE_PROC);
1769
Willy Tarreau43b78992009-01-25 15:42:27 +01001770 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaue5733232019-05-22 19:24:06 +02001771#if defined(USE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001772 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001773#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001774#if defined(USE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001775 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001776#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001777#if defined(USE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +01001778 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001779#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001780#if defined(USE_EVPORTS)
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +00001781 global.tune.options |= GTUNE_USE_EVPORTS;
1782#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001783#if defined(USE_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001784 global.tune.options |= GTUNE_USE_SPLICE;
1785#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001786#if defined(USE_GETADDRINFO)
1787 global.tune.options |= GTUNE_USE_GAI;
1788#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001789#if defined(SO_REUSEPORT)
1790 global.tune.options |= GTUNE_USE_REUSEPORT;
1791#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001792
1793 pid = getpid();
1794 progname = *argv;
1795 while ((tmp = strchr(progname, '/')) != NULL)
1796 progname = tmp + 1;
1797
Kevinm48936af2010-12-22 16:08:21 +00001798 /* the process name is used for the logs only */
Dragan Dosen43885c72015-10-01 13:18:13 +02001799 chunk_initstr(&global.log_tag, strdup(progname));
Kevinm48936af2010-12-22 16:08:21 +00001800
Willy Tarreaubaaee002006-06-26 02:48:02 +02001801 argc--; argv++;
1802 while (argc > 0) {
1803 char *flag;
1804
1805 if (**argv == '-') {
1806 flag = *argv+1;
1807
1808 /* 1 arg */
1809 if (*flag == 'v') {
1810 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +01001811 if (flag[1] == 'v') /* -vv */
1812 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001813 exit(0);
1814 }
Willy Tarreaue5733232019-05-22 19:24:06 +02001815#if defined(USE_EPOLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001816 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +01001817 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001818#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001819#if defined(USE_POLL)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001820 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +01001821 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001822#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001823#if defined(USE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001824 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +01001825 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001826#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001827#if defined(USE_EVPORTS)
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +00001828 else if (*flag == 'd' && flag[1] == 'v')
1829 global.tune.options &= ~GTUNE_USE_EVPORTS;
1830#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001831#if defined(USE_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001832 else if (*flag == 'd' && flag[1] == 'S')
1833 global.tune.options &= ~GTUNE_USE_SPLICE;
1834#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001835#if defined(USE_GETADDRINFO)
1836 else if (*flag == 'd' && flag[1] == 'G')
1837 global.tune.options &= ~GTUNE_USE_GAI;
1838#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001839#if defined(SO_REUSEPORT)
1840 else if (*flag == 'd' && flag[1] == 'R')
1841 global.tune.options &= ~GTUNE_USE_REUSEPORT;
1842#endif
Emeric Brun850efd52014-01-29 12:24:34 +01001843 else if (*flag == 'd' && flag[1] == 'V')
1844 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001845 else if (*flag == 'V')
1846 arg_mode |= MODE_VERBOSE;
1847 else if (*flag == 'd' && flag[1] == 'b')
1848 arg_mode |= MODE_FOREGROUND;
Willy Tarreau3eb10b82020-04-15 16:42:39 +02001849 else if (*flag == 'd' && flag[1] == 'W')
1850 arg_mode |= MODE_ZERO_WARNING;
Willy Tarreau6e064432012-05-08 15:40:42 +02001851 else if (*flag == 'd' && flag[1] == 'M')
1852 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreau3eed10e2016-11-07 21:03:16 +01001853 else if (*flag == 'd' && flag[1] == 'r')
1854 global.tune.options |= GTUNE_RESOLVE_DONTFAIL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001855 else if (*flag == 'd')
1856 arg_mode |= MODE_DEBUG;
1857 else if (*flag == 'c')
1858 arg_mode |= MODE_CHECK;
William Lallemand095ba4c2017-06-01 17:38:50 +02001859 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +02001860 arg_mode |= MODE_DAEMON;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001861 else if (*flag == 'W' && flag[1] == 's') {
Lukas Tribusf46bf952017-11-21 12:39:34 +01001862 arg_mode |= MODE_MWORKER | MODE_FOREGROUND;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001863#if defined(USE_SYSTEMD)
1864 global.tune.options |= GTUNE_USE_SYSTEMD;
1865#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001866 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 +01001867 usage(progname);
1868#endif
1869 }
William Lallemand095ba4c2017-06-01 17:38:50 +02001870 else if (*flag == 'W')
1871 arg_mode |= MODE_MWORKER;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001872 else if (*flag == 'q')
1873 arg_mode |= MODE_QUIET;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001874 else if (*flag == 'x') {
William Lallemand4f71d302020-06-04 23:41:29 +02001875 if (argc <= 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001876 ha_alert("Unix socket path expected with the -x flag\n\n");
William Lallemand45eff442017-06-19 15:57:55 +02001877 usage(progname);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001878 }
William Lallemand4fc09692017-06-19 16:37:19 +02001879 if (old_unixsocket)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001880 ha_warning("-x option already set, overwriting the value\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001881 old_unixsocket = argv[1];
William Lallemand4fc09692017-06-19 16:37:19 +02001882
Olivier Houchardf73629d2017-04-05 22:33:04 +02001883 argv++;
1884 argc--;
1885 }
William Lallemande7361152018-10-26 14:47:36 +02001886 else if (*flag == 'S') {
1887 struct wordlist *c;
1888
William Lallemanda6b32492020-06-04 23:49:20 +02001889 if (argc <= 1) {
William Lallemande7361152018-10-26 14:47:36 +02001890 ha_alert("Socket and optional bind parameters expected with the -S flag\n");
1891 usage(progname);
1892 }
1893 if ((c = malloc(sizeof(*c))) == NULL || (c->s = strdup(argv[1])) == NULL) {
1894 ha_alert("Cannot allocate memory\n");
1895 exit(EXIT_FAILURE);
1896 }
1897 LIST_ADD(&mworker_cli_conf, &c->list);
1898
1899 argv++;
1900 argc--;
1901 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001902 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1903 /* list of pids to finish ('f') or terminate ('t') */
1904
1905 if (flag[1] == 'f')
1906 oldpids_sig = SIGUSR1; /* finish then exit */
1907 else
1908 oldpids_sig = SIGTERM; /* terminate immediately */
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001909 while (argc > 1 && argv[1][0] != '-') {
Chris Lane236062f2018-02-05 23:15:44 +00001910 char * endptr = NULL;
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001911 oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int));
1912 if (!oldpids) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001913 ha_alert("Cannot allocate old pid : out of memory.\n");
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001914 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001915 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001916 argc--; argv++;
Chris Lane236062f2018-02-05 23:15:44 +00001917 errno = 0;
1918 oldpids[nb_oldpids] = strtol(*argv, &endptr, 10);
1919 if (errno) {
1920 ha_alert("-%2s option: failed to parse {%s}: %s\n",
1921 flag,
1922 *argv, strerror(errno));
1923 exit(1);
1924 } else if (endptr && strlen(endptr)) {
Willy Tarreau90807112020-02-25 08:16:33 +01001925 while (isspace((unsigned char)*endptr)) endptr++;
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001926 if (*endptr != 0) {
Chris Lane236062f2018-02-05 23:15:44 +00001927 ha_alert("-%2s option: some bytes unconsumed in PID list {%s}\n",
1928 flag, endptr);
1929 exit(1);
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001930 }
Chris Lane236062f2018-02-05 23:15:44 +00001931 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001932 if (oldpids[nb_oldpids] <= 0)
1933 usage(progname);
1934 nb_oldpids++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001935 }
1936 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001937 else if (flag[0] == '-' && flag[1] == 0) { /* "--" */
1938 /* now that's a cfgfile list */
1939 argv++; argc--;
1940 while (argc > 0) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02001941 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001942 ha_alert("Cannot load configuration file/directory %s : %s\n",
1943 *argv,
1944 err_msg);
Willy Tarreaua088d312015-10-08 11:58:48 +02001945 exit(1);
1946 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001947 argv++; argc--;
1948 }
1949 break;
1950 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001951 else { /* >=2 args */
1952 argv++; argc--;
1953 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001954 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001955
1956 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +02001957 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001958 case 'n' : cfg_maxconn = atol(*argv); break;
Willy Tarreau70060452015-12-14 12:46:07 +01001959 case 'm' : global.rlimit_memmax_all = atol(*argv); break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001960 case 'N' : cfg_maxpconn = atol(*argv); break;
William Lallemanddaf4cd22018-04-17 16:46:13 +02001961 case 'L' :
1962 strncpy(localpeer, *argv, sizeof(localpeer) - 1);
1963 setenv("HAPROXY_LOCALPEER", localpeer, 1);
1964 break;
Willy Tarreau5d01a632009-06-22 16:02:30 +02001965 case 'f' :
Maxime de Roucy0f503922016-05-13 23:52:55 +02001966 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001967 ha_alert("Cannot load configuration file/directory %s : %s\n",
1968 *argv,
1969 err_msg);
Willy Tarreau5d01a632009-06-22 16:02:30 +02001970 exit(1);
1971 }
Willy Tarreau5d01a632009-06-22 16:02:30 +02001972 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001973 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001974 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001975 }
1976 }
1977 }
1978 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001979 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001980 argv++; argc--;
1981 }
1982
Christopher Faulete3a5e352017-10-24 13:53:54 +02001983 global.mode |= (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
Willy Tarreau3eb10b82020-04-15 16:42:39 +02001984 | MODE_QUIET | MODE_CHECK | MODE_DEBUG | MODE_ZERO_WARNING));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001985
William Lallemand944e6192018-11-21 15:48:31 +01001986 if (getenv("HAPROXY_MWORKER_WAIT_ONLY")) {
William Lallemandcb11fd22017-06-01 17:38:52 +02001987 unsetenv("HAPROXY_MWORKER_WAIT_ONLY");
William Lallemand944e6192018-11-21 15:48:31 +01001988 global.mode |= MODE_MWORKER_WAIT;
1989 global.mode &= ~MODE_MWORKER;
William Lallemandcb11fd22017-06-01 17:38:52 +02001990 }
1991
1992 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {
1993 atexit_flag = 1;
1994 atexit(reexec_on_failure);
1995 }
1996
Willy Tarreau576132e2011-09-10 19:26:56 +02001997 if (change_dir && chdir(change_dir) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001998 ha_alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
Willy Tarreau576132e2011-09-10 19:26:56 +02001999 exit(1);
2000 }
2001
Willy Tarreaubaaee002006-06-26 02:48:02 +02002002 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002003
2004 init_default_instance();
2005
William Lallemand944e6192018-11-21 15:48:31 +01002006 /* in wait mode, we don't try to read the configuration files */
2007 if (!(global.mode & MODE_MWORKER_WAIT)) {
William Lallemand7b302d82019-05-20 11:15:37 +02002008 struct buffer *trash = get_trash_chunk();
Willy Tarreauc4382422009-12-06 13:10:44 +01002009
William Lallemand944e6192018-11-21 15:48:31 +01002010 /* handle cfgfiles that are actually directories */
2011 cfgfiles_expand_directories();
2012
2013 if (LIST_ISEMPTY(&cfg_cfgfiles))
2014 usage(progname);
2015
2016
2017 list_for_each_entry(wl, &cfg_cfgfiles, list) {
2018 int ret;
2019
William Lallemand7b302d82019-05-20 11:15:37 +02002020 if (trash->data)
2021 chunk_appendf(trash, ";");
2022
2023 chunk_appendf(trash, "%s", wl->s);
2024
William Lallemand944e6192018-11-21 15:48:31 +01002025 ret = readcfgfile(wl->s);
2026 if (ret == -1) {
2027 ha_alert("Could not open configuration file %s : %s\n",
2028 wl->s, strerror(errno));
2029 exit(1);
2030 }
2031 if (ret & (ERR_ABORT|ERR_FATAL))
2032 ha_alert("Error(s) found in configuration file : %s\n", wl->s);
2033 err_code |= ret;
2034 if (err_code & ERR_ABORT)
2035 exit(1);
Willy Tarreauc4382422009-12-06 13:10:44 +01002036 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002037
William Lallemand944e6192018-11-21 15:48:31 +01002038 /* do not try to resolve arguments nor to spot inconsistencies when
2039 * the configuration contains fatal errors caused by files not found
2040 * or failed memory allocations.
2041 */
2042 if (err_code & (ERR_ABORT|ERR_FATAL)) {
2043 ha_alert("Fatal errors found in configuration.\n");
2044 exit(1);
2045 }
William Lallemand7b302d82019-05-20 11:15:37 +02002046 if (trash->data)
2047 setenv("HAPROXY_CFGFILES", trash->area, 1);
2048
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02002049 }
William Lallemandce83b4a2018-10-26 14:47:30 +02002050 if (global.mode & MODE_MWORKER) {
2051 int proc;
William Lallemand16dd1b32018-11-19 18:46:18 +01002052 struct mworker_proc *tmproc;
2053
William Lallemand482f9a92019-04-12 16:15:00 +02002054 setenv("HAPROXY_MWORKER", "1", 1);
2055
William Lallemand16dd1b32018-11-19 18:46:18 +01002056 if (getenv("HAPROXY_MWORKER_REEXEC") == NULL) {
2057
William Lallemandf3a86832019-04-01 11:29:58 +02002058 tmproc = calloc(1, sizeof(*tmproc));
William Lallemand16dd1b32018-11-19 18:46:18 +01002059 if (!tmproc) {
2060 ha_alert("Cannot allocate process structures.\n");
2061 exit(EXIT_FAILURE);
2062 }
William Lallemand8f7069a2019-04-12 16:09:23 +02002063 tmproc->options |= PROC_O_TYPE_MASTER; /* master */
William Lallemand16dd1b32018-11-19 18:46:18 +01002064 tmproc->reloads = 0;
2065 tmproc->relative_pid = 0;
2066 tmproc->pid = pid;
2067 tmproc->timestamp = start_date.tv_sec;
2068 tmproc->ipc_fd[0] = -1;
2069 tmproc->ipc_fd[1] = -1;
2070
2071 proc_self = tmproc;
2072
2073 LIST_ADDQ(&proc_list, &tmproc->list);
2074 }
William Lallemandce83b4a2018-10-26 14:47:30 +02002075
2076 for (proc = 0; proc < global.nbproc; proc++) {
William Lallemandce83b4a2018-10-26 14:47:30 +02002077
William Lallemandf3a86832019-04-01 11:29:58 +02002078 tmproc = calloc(1, sizeof(*tmproc));
William Lallemandce83b4a2018-10-26 14:47:30 +02002079 if (!tmproc) {
2080 ha_alert("Cannot allocate process structures.\n");
2081 exit(EXIT_FAILURE);
2082 }
2083
William Lallemand8f7069a2019-04-12 16:09:23 +02002084 tmproc->options |= PROC_O_TYPE_WORKER; /* worker */
William Lallemandce83b4a2018-10-26 14:47:30 +02002085 tmproc->pid = -1;
2086 tmproc->reloads = 0;
William Lallemande3683302018-11-19 18:46:17 +01002087 tmproc->timestamp = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02002088 tmproc->relative_pid = 1 + proc;
William Lallemand550db6d2018-11-06 17:37:12 +01002089 tmproc->ipc_fd[0] = -1;
2090 tmproc->ipc_fd[1] = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02002091
2092 if (mworker_cli_sockpair_new(tmproc, proc) < 0) {
2093 exit(EXIT_FAILURE);
2094 }
2095
2096 LIST_ADDQ(&proc_list, &tmproc->list);
2097 }
William Lallemand944e6192018-11-21 15:48:31 +01002098 }
2099 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
2100 struct wordlist *it, *c;
2101
William Lallemand1b663612018-10-26 14:47:33 +02002102 mworker_env_to_proc_list(); /* get the info of the children in the env */
William Lallemand8a022572018-10-26 14:47:35 +02002103
William Lallemande7361152018-10-26 14:47:36 +02002104
William Lallemand550db6d2018-11-06 17:37:12 +01002105 if (!LIST_ISEMPTY(&mworker_cli_conf)) {
William Lallemande7361152018-10-26 14:47:36 +02002106
William Lallemand550db6d2018-11-06 17:37:12 +01002107 if (mworker_cli_proxy_create() < 0) {
William Lallemande7361152018-10-26 14:47:36 +02002108 ha_alert("Can't create the master's CLI.\n");
2109 exit(EXIT_FAILURE);
2110 }
William Lallemande7361152018-10-26 14:47:36 +02002111
William Lallemand550db6d2018-11-06 17:37:12 +01002112 list_for_each_entry_safe(c, it, &mworker_cli_conf, list) {
2113
2114 if (mworker_cli_proxy_new_listener(c->s) < 0) {
2115 ha_alert("Can't create the master's CLI.\n");
2116 exit(EXIT_FAILURE);
2117 }
2118 LIST_DEL(&c->list);
2119 free(c->s);
2120 free(c);
2121 }
2122 }
William Lallemandce83b4a2018-10-26 14:47:30 +02002123 }
2124
Willy Tarreaubb925012009-07-23 13:36:36 +02002125 err_code |= check_config_validity();
Christopher Fauletc1692962019-08-12 09:51:07 +02002126 for (px = proxies_list; px; px = px->next) {
2127 struct server *srv;
2128 struct post_proxy_check_fct *ppcf;
2129 struct post_server_check_fct *pscf;
2130
2131 list_for_each_entry(pscf, &post_server_check_list, list) {
2132 for (srv = px->srv; srv; srv = srv->next)
2133 err_code |= pscf->fct(srv);
2134 }
2135 list_for_each_entry(ppcf, &post_proxy_check_list, list)
2136 err_code |= ppcf->fct(px);
2137 }
Willy Tarreaubb925012009-07-23 13:36:36 +02002138 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002139 ha_alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002140 exit(1);
2141 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002142
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002143 err_code |= pattern_finalize_config();
2144 if (err_code & (ERR_ABORT|ERR_FATAL)) {
2145 ha_alert("Failed to finalize pattern config.\n");
2146 exit(1);
2147 }
Willy Tarreau0f936722019-04-11 14:47:08 +02002148
Willy Tarreau70060452015-12-14 12:46:07 +01002149 /* recompute the amount of per-process memory depending on nbproc and
2150 * the shared SSL cache size (allowed to exist in all processes).
2151 */
2152 if (global.rlimit_memmax_all) {
2153#if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE)
2154 int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL;
2155
2156 global.rlimit_memmax =
2157 ((((int64_t)global.rlimit_memmax_all * 1048576LL) -
2158 ssl_cache_bytes) / global.nbproc +
2159 ssl_cache_bytes + 1048575LL) / 1048576LL;
2160#else
2161 global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc;
2162#endif
2163 }
2164
Willy Tarreaue5733232019-05-22 19:24:06 +02002165#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002166 err_code |= netns_init();
2167 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002168 ha_alert("Failed to initialize namespace support.\n");
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002169 exit(1);
2170 }
2171#endif
2172
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01002173 /* Apply server states */
2174 apply_server_state();
2175
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002176 for (px = proxies_list; px; px = px->next)
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01002177 srv_compute_all_admin_states(px);
2178
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01002179 /* Apply servers' configured address */
2180 err_code |= srv_init_addr();
2181 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002182 ha_alert("Failed to initialize server(s) addr.\n");
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01002183 exit(1);
2184 }
2185
Willy Tarreau3eb10b82020-04-15 16:42:39 +02002186 if (warned & WARN_ANY && global.mode & MODE_ZERO_WARNING) {
2187 ha_alert("Some warnings were found and 'zero-warning' is set. Aborting.\n");
2188 exit(1);
2189 }
2190
Willy Tarreaubaaee002006-06-26 02:48:02 +02002191 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +01002192 struct peers *pr;
2193 struct proxy *px;
2194
Willy Tarreaubebd2122020-04-15 16:06:11 +02002195 if (warned & WARN_ANY)
2196 qfprintf(stdout, "Warnings were found.\n");
2197
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002198 for (pr = cfg_peers; pr; pr = pr->next)
Willy Tarreau8b15ba12012-02-02 17:48:18 +01002199 if (pr->peers_fe)
2200 break;
2201
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002202 for (px = proxies_list; px; px = px->next)
Willy Tarreau4348fad2012-09-20 16:48:07 +02002203 if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners))
Willy Tarreau8b15ba12012-02-02 17:48:18 +01002204 break;
2205
2206 if (pr || px) {
2207 /* At least one peer or one listener has been found */
2208 qfprintf(stdout, "Configuration file is valid\n");
2209 exit(0);
2210 }
2211 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
2212 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002213 }
Willy Tarreaue9b26022011-08-01 20:57:55 +02002214
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002215 /* now we know the buffer size, we can initialize the channels and buffers */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002216 init_buffer();
Willy Tarreau8280d642009-09-23 23:37:52 +02002217
Willy Tarreaue6945732016-12-21 19:57:00 +01002218 list_for_each_entry(pcf, &post_check_list, list) {
2219 err_code |= pcf->fct();
2220 if (err_code & (ERR_ABORT|ERR_FATAL))
2221 exit(1);
2222 }
2223
Willy Tarreaubaaee002006-06-26 02:48:02 +02002224 if (cfg_maxconn > 0)
2225 global.maxconn = cfg_maxconn;
2226
Willy Tarreau8d687d82019-03-01 09:39:42 +01002227 if (global.stats_fe)
2228 global.maxsock += global.stats_fe->maxconn;
2229
2230 if (cfg_peers) {
2231 /* peers also need to bypass global maxconn */
2232 struct peers *p = cfg_peers;
2233
2234 for (p = cfg_peers; p; p = p->next)
2235 if (p->peers_fe)
2236 global.maxsock += p->peers_fe->maxconn;
2237 }
2238
Willy Tarreaubaaee002006-06-26 02:48:02 +02002239 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +02002240 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002241 global.pidfile = strdup(cfg_pidfile);
2242 }
2243
Willy Tarreaud0256482015-01-15 21:45:22 +01002244 /* Now we want to compute the maxconn and possibly maxsslconn values.
Willy Tarreauac350932019-03-01 15:43:14 +01002245 * It's a bit tricky. Maxconn defaults to the pre-computed value based
2246 * on rlim_fd_cur and the number of FDs in use due to the configuration,
2247 * and maxsslconn defaults to DEFAULT_MAXSSLCONN. On top of that we can
2248 * enforce a lower limit based on memmax.
Willy Tarreaud0256482015-01-15 21:45:22 +01002249 *
2250 * If memmax is set, then it depends on which values are set. If
2251 * maxsslconn is set, we use memmax to determine how many cleartext
2252 * connections may be added, and set maxconn to the sum of the two.
2253 * If maxconn is set and not maxsslconn, maxsslconn is computed from
2254 * the remaining amount of memory between memmax and the cleartext
2255 * connections. If neither are set, then it is considered that all
2256 * connections are SSL-capable, and maxconn is computed based on this,
2257 * then maxsslconn accordingly. We need to know if SSL is used on the
2258 * frontends, backends, or both, because when it's used on both sides,
2259 * we need twice the value for maxsslconn, but we only count the
2260 * handshake once since it is not performed on the two sides at the
2261 * same time (frontend-side is terminated before backend-side begins).
2262 * The SSL stack is supposed to have filled ssl_session_cost and
Willy Tarreau474b96a2015-01-28 19:03:21 +01002263 * ssl_handshake_cost during its initialization. In any case, if
2264 * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for
2265 * maxconn in order to protect the system.
Willy Tarreaud0256482015-01-15 21:45:22 +01002266 */
Willy Tarreauac350932019-03-01 15:43:14 +01002267 ideal_maxconn = compute_ideal_maxconn();
2268
Willy Tarreaud0256482015-01-15 21:45:22 +01002269 if (!global.rlimit_memmax) {
2270 if (global.maxconn == 0) {
Willy Tarreauac350932019-03-01 15:43:14 +01002271 global.maxconn = ideal_maxconn;
Willy Tarreaud0256482015-01-15 21:45:22 +01002272 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2273 fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn);
2274 }
2275 }
2276#ifdef USE_OPENSSL
2277 else if (!global.maxconn && !global.maxsslconn &&
2278 (global.ssl_used_frontend || global.ssl_used_backend)) {
2279 /* memmax is set, compute everything automatically. Here we want
2280 * to ensure that all SSL connections will be served. We take
2281 * care of the number of sides where SSL is used, and consider
2282 * the worst case : SSL used on both sides and doing a handshake
2283 * simultaneously. Note that we can't have more than maxconn
2284 * handshakes at a time by definition, so for the worst case of
2285 * two SSL conns per connection, we count a single handshake.
2286 */
2287 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2288 int64_t mem = global.rlimit_memmax * 1048576ULL;
Willy Tarreau304e17e2020-03-10 17:54:54 +01002289 int retried = 0;
Willy Tarreaud0256482015-01-15 21:45:22 +01002290
2291 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2292 mem -= global.maxzlibmem;
2293 mem = mem * MEM_USABLE_RATIO;
2294
Willy Tarreau304e17e2020-03-10 17:54:54 +01002295 /* Principle: we test once to set maxconn according to the free
2296 * memory. If it results in values the system rejects, we try a
2297 * second time by respecting rlim_fd_max. If it fails again, we
2298 * go back to the initial value and will let the final code
2299 * dealing with rlimit report the error. That's up to 3 attempts.
2300 */
2301 do {
2302 global.maxconn = mem /
2303 ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream
2304 sides * global.ssl_session_max_cost + // SSL buffers, one per side
2305 global.ssl_handshake_max_cost); // 1 handshake per connection max
Willy Tarreaud0256482015-01-15 21:45:22 +01002306
Willy Tarreau304e17e2020-03-10 17:54:54 +01002307 if (retried == 1)
2308 global.maxconn = MIN(global.maxconn, ideal_maxconn);
2309 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01002310#ifdef SYSTEM_MAXCONN
Willy Tarreau304e17e2020-03-10 17:54:54 +01002311 if (global.maxconn > SYSTEM_MAXCONN)
2312 global.maxconn = SYSTEM_MAXCONN;
Willy Tarreau474b96a2015-01-28 19:03:21 +01002313#endif /* SYSTEM_MAXCONN */
Willy Tarreau304e17e2020-03-10 17:54:54 +01002314 global.maxsslconn = sides * global.maxconn;
2315
2316 if (check_if_maxsock_permitted(compute_ideal_maxsock(global.maxconn)))
2317 break;
2318 } while (retried++ < 2);
2319
Willy Tarreaud0256482015-01-15 21:45:22 +01002320 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2321 fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n",
2322 global.maxconn, global.maxsslconn);
2323 }
2324 else if (!global.maxsslconn &&
2325 (global.ssl_used_frontend || global.ssl_used_backend)) {
2326 /* memmax and maxconn are known, compute maxsslconn automatically.
2327 * maxsslconn being forced, we don't know how many of it will be
2328 * on each side if both sides are being used. The worst case is
2329 * when all connections use only one SSL instance because
2330 * handshakes may be on two sides at the same time.
2331 */
2332 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2333 int64_t mem = global.rlimit_memmax * 1048576ULL;
2334 int64_t sslmem;
2335
2336 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2337 mem -= global.maxzlibmem;
2338 mem = mem * MEM_USABLE_RATIO;
2339
Willy Tarreau87b09662015-04-03 00:22:06 +02002340 sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01002341 global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost);
2342 global.maxsslconn = round_2dig(global.maxsslconn);
2343
2344 if (sslmem <= 0 || global.maxsslconn < sides) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002345 ha_alert("Cannot compute the automatic maxsslconn because global.maxconn is already too "
2346 "high for the global.memmax value (%d MB). The absolute maximum possible value "
2347 "without SSL is %d, but %d was found and SSL is in use.\n",
2348 global.rlimit_memmax,
2349 (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)),
2350 global.maxconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01002351 exit(1);
2352 }
2353
2354 if (global.maxsslconn > sides * global.maxconn)
2355 global.maxsslconn = sides * global.maxconn;
2356
2357 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2358 fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn);
2359 }
2360#endif
2361 else if (!global.maxconn) {
2362 /* memmax and maxsslconn are known/unused, compute maxconn automatically */
2363 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2364 int64_t mem = global.rlimit_memmax * 1048576ULL;
2365 int64_t clearmem;
Willy Tarreau304e17e2020-03-10 17:54:54 +01002366 int retried = 0;
Willy Tarreaud0256482015-01-15 21:45:22 +01002367
2368 if (global.ssl_used_frontend || global.ssl_used_backend)
2369 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2370
2371 mem -= global.maxzlibmem;
2372 mem = mem * MEM_USABLE_RATIO;
2373
2374 clearmem = mem;
2375 if (sides)
2376 clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn;
2377
Willy Tarreau304e17e2020-03-10 17:54:54 +01002378 /* Principle: we test once to set maxconn according to the free
2379 * memory. If it results in values the system rejects, we try a
2380 * second time by respecting rlim_fd_max. If it fails again, we
2381 * go back to the initial value and will let the final code
2382 * dealing with rlimit report the error. That's up to 3 attempts.
2383 */
2384 do {
2385 global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize);
2386 if (retried == 1)
2387 global.maxconn = MIN(global.maxconn, ideal_maxconn);
2388 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01002389#ifdef SYSTEM_MAXCONN
Willy Tarreau304e17e2020-03-10 17:54:54 +01002390 if (global.maxconn > SYSTEM_MAXCONN)
2391 global.maxconn = SYSTEM_MAXCONN;
Willy Tarreau474b96a2015-01-28 19:03:21 +01002392#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01002393
Willy Tarreau304e17e2020-03-10 17:54:54 +01002394 if (clearmem <= 0 || !global.maxconn) {
2395 ha_alert("Cannot compute the automatic maxconn because global.maxsslconn is already too "
2396 "high for the global.memmax value (%d MB). The absolute maximum possible value "
2397 "is %d, but %d was found.\n",
2398 global.rlimit_memmax,
Christopher Faulet767a84b2017-11-24 16:50:31 +01002399 (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)),
Willy Tarreau304e17e2020-03-10 17:54:54 +01002400 global.maxsslconn);
2401 exit(1);
2402 }
2403
2404 if (check_if_maxsock_permitted(compute_ideal_maxsock(global.maxconn)))
2405 break;
2406 } while (retried++ < 2);
Willy Tarreaud0256482015-01-15 21:45:22 +01002407
2408 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
2409 if (sides && global.maxsslconn > sides * global.maxconn) {
2410 fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn "
2411 "to be limited to %d. Better reduce global.maxsslconn to get more "
2412 "room for extra connections.\n", global.maxsslconn, global.maxconn);
2413 }
2414 fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn);
2415 }
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002416 }
2417
Willy Tarreaua409f302020-03-10 17:08:53 +01002418 global.maxsock = compute_ideal_maxsock(global.maxconn);
2419 global.hardmaxconn = global.maxconn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002420
Olivier Houchard88698d92019-04-16 19:07:22 +02002421 /* update connection pool thresholds */
2422 global.tune.pool_low_count = ((long long)global.maxsock * global.tune.pool_low_ratio + 99) / 100;
2423 global.tune.pool_high_count = ((long long)global.maxsock * global.tune.pool_high_ratio + 99) / 100;
2424
Willy Tarreauc8d5b952019-02-27 17:25:52 +01002425 proxy_adjust_all_maxconn();
2426
Willy Tarreau1db37712007-06-03 17:16:49 +02002427 if (global.tune.maxpollevents <= 0)
2428 global.tune.maxpollevents = MAX_POLL_EVENTS;
2429
Olivier Houchard1599b802018-05-24 18:59:04 +02002430 if (global.tune.runqueue_depth <= 0)
2431 global.tune.runqueue_depth = RUNQUEUE_DEPTH;
2432
Willy Tarreau6f4a82c2009-03-21 20:43:57 +01002433 if (global.tune.recv_enough == 0)
2434 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
2435
Willy Tarreau27a674e2009-08-17 07:23:33 +02002436 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
2437 global.tune.maxrewrite = global.tune.bufsize / 2;
2438
Willy Tarreaubaaee002006-06-26 02:48:02 +02002439 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
2440 /* command line debug mode inhibits configuration mode */
William Lallemand095ba4c2017-06-01 17:38:50 +02002441 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002442 global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND));
2443 }
2444
William Lallemand095ba4c2017-06-01 17:38:50 +02002445 if (arg_mode & MODE_DAEMON) {
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002446 /* command line daemon mode inhibits foreground and debug modes mode */
2447 global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND);
William Lallemand095ba4c2017-06-01 17:38:50 +02002448 global.mode |= arg_mode & MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002449 }
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002450
2451 global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002452
William Lallemand095ba4c2017-06-01 17:38:50 +02002453 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002454 ha_warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
William Lallemand095ba4c2017-06-01 17:38:50 +02002455 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002456 }
2457
William Lallemand095ba4c2017-06-01 17:38:50 +02002458 if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002459 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
Christopher Faulet767a84b2017-11-24 16:50:31 +01002460 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 +02002461 global.nbproc = 1;
2462 }
2463
2464 if (global.nbproc < 1)
2465 global.nbproc = 1;
2466
Christopher Fauletbe0faa22017-08-29 15:37:10 +02002467 if (global.nbthread < 1)
2468 global.nbthread = 1;
2469
Christopher Faulet3ef26392017-08-29 16:46:57 +02002470 /* Realloc trash buffers because global.tune.bufsize may have changed */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002471 if (!init_trash_buffers(0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002472 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet3ef26392017-08-29 16:46:57 +02002473 exit(1);
2474 }
2475
Christopher Faulet96d44832017-11-14 22:02:30 +01002476 if (!init_log_buffers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002477 ha_alert("failed to initialize log buffers.\n");
Christopher Faulet96d44832017-11-14 22:02:30 +01002478 exit(1);
2479 }
2480
Willy Tarreauef1d1f82007-04-16 00:25:25 +02002481 /*
2482 * Note: we could register external pollers here.
2483 * Built-in pollers have been registered before main().
2484 */
Willy Tarreau4f60f162007-04-08 16:39:58 +02002485
Willy Tarreau43b78992009-01-25 15:42:27 +01002486 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +02002487 disable_poller("kqueue");
2488
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +00002489 if (!(global.tune.options & GTUNE_USE_EVPORTS))
2490 disable_poller("evports");
2491
Willy Tarreau43b78992009-01-25 15:42:27 +01002492 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002493 disable_poller("epoll");
2494
Willy Tarreau43b78992009-01-25 15:42:27 +01002495 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002496 disable_poller("poll");
2497
Willy Tarreau43b78992009-01-25 15:42:27 +01002498 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002499 disable_poller("select");
2500
2501 /* Note: we could disable any poller by name here */
2502
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002503 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +02002504 list_pollers(stderr);
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002505 fprintf(stderr, "\n");
2506 list_filters(stderr);
2507 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002508
Willy Tarreau4f60f162007-04-08 16:39:58 +02002509 if (!init_pollers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002510 ha_alert("No polling mechanism available.\n"
2511 " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n"
2512 " is too low on this platform to support maxconn and the number of listeners\n"
2513 " and servers. You should rebuild haproxy specifying your system using TARGET=\n"
2514 " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n"
2515 " global maxconn setting to accommodate the system's limitation. For reference,\n"
2516 " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n"
2517 " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n"
2518 " check build settings using 'haproxy -vv'.\n\n",
2519 FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002520 exit(1);
2521 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002522 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
2523 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002524 }
2525
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002526 if (!global.node)
2527 global.node = strdup(hostname);
2528
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01002529 if (!hlua_post_init())
2530 exit(1);
Thomas Holmes6abded42015-05-12 16:23:58 +01002531
Maxime de Roucy0f503922016-05-13 23:52:55 +02002532 free(err_msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002533}
2534
Simon Horman6fb82592011-07-15 13:14:11 +09002535static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +09002536{
Simon Hormanac821422011-07-15 13:14:09 +09002537 struct acl_term_suite *suite, *suiteb;
2538 struct acl_term *term, *termb;
2539
Simon Horman6fb82592011-07-15 13:14:11 +09002540 if (!cond)
2541 return;
2542
2543 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
2544 list_for_each_entry_safe(term, termb, &suite->terms, list) {
2545 LIST_DEL(&term->list);
2546 free(term);
Simon Hormanac821422011-07-15 13:14:09 +09002547 }
Simon Horman6fb82592011-07-15 13:14:11 +09002548 LIST_DEL(&suite->list);
2549 free(suite);
2550 }
2551
2552 free(cond);
2553}
2554
Christopher Fauletcb550132019-12-17 11:25:46 +01002555static void deinit_act_rules(struct list *rules)
Simon Horman6fb82592011-07-15 13:14:11 +09002556{
Christopher Fauletcb550132019-12-17 11:25:46 +01002557 struct act_rule *rule, *ruleb;
Simon Horman6fb82592011-07-15 13:14:11 +09002558
Christopher Fauletcb550132019-12-17 11:25:46 +01002559 list_for_each_entry_safe(rule, ruleb, rules, list) {
2560 LIST_DEL(&rule->list);
2561 deinit_acl_cond(rule->cond);
Christopher Faulet58b35642019-12-17 11:48:42 +01002562 if (rule->release_ptr)
2563 rule->release_ptr(rule);
Christopher Fauletcb550132019-12-17 11:25:46 +01002564 free(rule);
Simon Hormanac821422011-07-15 13:14:09 +09002565 }
2566}
2567
Simon Horman6fb82592011-07-15 13:14:11 +09002568static void deinit_stick_rules(struct list *rules)
2569{
2570 struct sticking_rule *rule, *ruleb;
2571
2572 list_for_each_entry_safe(rule, ruleb, rules, list) {
2573 LIST_DEL(&rule->list);
2574 deinit_acl_cond(rule->cond);
Christopher Faulet476e5d02016-10-26 11:34:47 +02002575 release_sample_expr(rule->expr);
Simon Horman6fb82592011-07-15 13:14:11 +09002576 free(rule);
2577 }
2578}
2579
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002580void deinit(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002581{
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002582 struct proxy *p = proxies_list, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002583 struct cap_hdr *h,*h_next;
2584 struct server *s,*s_next;
2585 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002586 struct acl_cond *cond, *condb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002587 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002588 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002589 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002590 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002591 struct wordlist *wl, *wlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002592 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +02002593 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +01002594 struct logformat_node *lf, *lfb;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002595 struct bind_conf *bind_conf, *bind_back;
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002596 struct build_opts_str *bol, *bolb;
Willy Tarreau05554e62016-12-21 20:46:26 +01002597 struct post_deinit_fct *pdf;
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +02002598 struct proxy_deinit_fct *pxdf;
2599 struct server_deinit_fct *srvdf;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002600
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002601 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002602 while (p) {
Willy Tarreau8113a5d2012-10-04 08:01:43 +02002603 free(p->conf.file);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002604 free(p->id);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002605 free(p->cookie_name);
2606 free(p->cookie_domain);
Christopher Faulet2f533902020-01-21 11:06:48 +01002607 free(p->cookie_attrs);
Willy Tarreau4c03d1c2019-01-14 15:23:54 +01002608 free(p->lbprm.arg_str);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002609 free(p->capture_name);
2610 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +09002611 free(p->rdp_cookie_name);
Willy Tarreau62a61232013-04-12 18:13:46 +02002612 if (p->conf.logformat_string != default_http_log_format &&
2613 p->conf.logformat_string != default_tcp_log_format &&
2614 p->conf.logformat_string != clf_http_log_format)
2615 free(p->conf.logformat_string);
Willy Tarreau196729e2012-05-31 19:30:26 +02002616
Willy Tarreau62a61232013-04-12 18:13:46 +02002617 free(p->conf.lfs_file);
2618 free(p->conf.uniqueid_format_string);
2619 free(p->conf.uif_file);
Willy Tarreau0cac26c2019-01-14 16:55:42 +01002620 if ((p->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_MAP)
2621 free(p->lbprm.map.srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002622
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002623 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
2624 free(p->conf.logformat_sd_string);
2625 free(p->conf.lfsd_file);
2626
Willy Tarreaub80c2302007-11-30 20:51:32 +01002627 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
2628 LIST_DEL(&cond->list);
2629 prune_acl_cond(cond);
2630 free(cond);
2631 }
2632
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002633 /* build a list of unique uri_auths */
2634 if (!ua)
2635 ua = p->uri_auth;
2636 else {
2637 /* check if p->uri_auth is unique */
2638 for (uap = ua; uap; uap=uap->next)
2639 if (uap == p->uri_auth)
2640 break;
2641
Willy Tarreauaccc4e12008-06-24 11:14:45 +02002642 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002643 /* add it, if it is */
2644 p->uri_auth->next = ua;
2645 ua = p->uri_auth;
2646 }
2647 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002648
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002649 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
2650 LIST_DEL(&acl->list);
2651 prune_acl(acl);
2652 free(acl);
2653 }
2654
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002655 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
2656 LIST_DEL(&srule->list);
2657 prune_acl_cond(srule->cond);
2658 free(srule->cond);
2659 free(srule);
2660 }
2661
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002662 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
2663 LIST_DEL(&rule->list);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002664 if (rule->cond) {
2665 prune_acl_cond(rule->cond);
2666 free(rule->cond);
2667 }
Dragan Dosen2a7c20f2019-04-30 00:38:36 +02002668 free(rule->file);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002669 free(rule);
2670 }
2671
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002672 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
2673 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +01002674 if (rdr->cond) {
2675 prune_acl_cond(rdr->cond);
2676 free(rdr->cond);
2677 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002678 free(rdr->rdr_str);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002679 list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) {
2680 LIST_DEL(&lf->list);
2681 free(lf);
2682 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002683 free(rdr);
2684 }
2685
William Lallemand0f99e342011-10-12 17:50:54 +02002686 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
2687 LIST_DEL(&log->list);
2688 free(log);
2689 }
2690
William Lallemand723b73a2012-02-08 16:37:49 +01002691 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
2692 LIST_DEL(&lf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +02002693 release_sample_expr(lf->expr);
2694 free(lf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +01002695 free(lf);
2696 }
2697
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002698 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
2699 LIST_DEL(&lf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +02002700 release_sample_expr(lf->expr);
2701 free(lf->arg);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002702 free(lf);
2703 }
2704
Christopher Fauletcb550132019-12-17 11:25:46 +01002705 deinit_act_rules(&p->tcp_req.inspect_rules);
2706 deinit_act_rules(&p->tcp_rep.inspect_rules);
2707 deinit_act_rules(&p->tcp_req.l4_rules);
2708 deinit_act_rules(&p->tcp_req.l5_rules);
2709 deinit_act_rules(&p->http_req_rules);
2710 deinit_act_rules(&p->http_res_rules);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002711 deinit_act_rules(&p->http_after_res_rules);
Simon Hormanac821422011-07-15 13:14:09 +09002712
Simon Horman6fb82592011-07-15 13:14:11 +09002713 deinit_stick_rules(&p->storersp_rules);
2714 deinit_stick_rules(&p->sticking_rules);
2715
Willy Tarreaubaaee002006-06-26 02:48:02 +02002716 h = p->req_cap;
2717 while (h) {
2718 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002719 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002720 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002721 free(h);
2722 h = h_next;
2723 }/* end while(h) */
2724
2725 h = p->rsp_cap;
2726 while (h) {
2727 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002728 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002729 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002730 free(h);
2731 h = h_next;
2732 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002733
Willy Tarreaubaaee002006-06-26 02:48:02 +02002734 s = p->srv;
2735 while (s) {
2736 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002737
Dragan Dosen026ef572019-04-30 00:56:20 +02002738
Willy Tarreauf6562792019-05-07 19:05:35 +02002739 task_destroy(s->warmup);
Willy Tarreau2e993902011-10-31 11:53:20 +01002740
Willy Tarreaua534fea2008-08-03 12:19:50 +02002741 free(s->id);
2742 free(s->cookie);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002743 free(s->hostname_dn);
Sárközi, László34c01792014-09-05 10:08:23 +02002744 free((char*)s->conf.file);
Olivier Houchard7fc3be72018-11-22 18:50:54 +01002745 free(s->idle_conns);
Olivier Houchard7fc3be72018-11-22 18:50:54 +01002746 free(s->safe_conns);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002747 free(s->available_conns);
Olivier Houchardf1314812019-02-18 16:41:17 +01002748 free(s->curr_idle_thr);
Willy Tarreau17d45382016-12-22 21:16:08 +01002749
Christopher Fauletf61f33a2020-03-27 18:55:49 +01002750 if (s->use_ssl == 1 || s->check.use_ssl == 1 || (s->proxy->options & PR_O_TCPCHK_SSL)) {
Willy Tarreau17d45382016-12-22 21:16:08 +01002751 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2752 xprt_get(XPRT_SSL)->destroy_srv(s);
2753 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002754 HA_SPIN_DESTROY(&s->lock);
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +02002755
2756 list_for_each_entry(srvdf, &server_deinit_list, list)
2757 srvdf->fct(s);
2758
Willy Tarreaubaaee002006-06-26 02:48:02 +02002759 free(s);
2760 s = s_next;
2761 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002762
Willy Tarreau4348fad2012-09-20 16:48:07 +02002763 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002764 /*
2765 * Zombie proxy, the listener just pretend to be up
2766 * because they still hold an opened fd.
2767 * Close it and give the listener its real state.
2768 */
2769 if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
2770 close(l->fd);
2771 l->state = LI_INIT;
2772 }
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02002773 unbind_listener(l);
2774 delete_listener(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002775 LIST_DEL(&l->by_fe);
2776 LIST_DEL(&l->by_bind);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002777 free(l->name);
2778 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002779 free(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002780 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002781
Willy Tarreau4348fad2012-09-20 16:48:07 +02002782 /* Release unused SSL configs. */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002783 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01002784 if (bind_conf->xprt->destroy_bind_conf)
2785 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002786 free(bind_conf->file);
2787 free(bind_conf->arg);
2788 LIST_DEL(&bind_conf->by_fe);
2789 free(bind_conf);
2790 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02002791
Christopher Fauletd7c91962015-04-30 11:48:27 +02002792 flt_deinit(p);
2793
Christopher Faulet3ea5cbe2019-07-31 08:44:12 +02002794 list_for_each_entry(pxdf, &proxy_deinit_list, list)
2795 pxdf->fct(p);
2796
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002797 free(p->desc);
2798 free(p->fwdfor_hdr_name);
2799
Olivier Houchard3f795f72019-04-17 22:51:06 +02002800 task_destroy(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002801
Willy Tarreaubafbe012017-11-24 17:34:44 +01002802 pool_destroy(p->req_cap_pool);
2803 pool_destroy(p->rsp_cap_pool);
Dragan Dosen7d61a332019-05-07 14:16:18 +02002804 if (p->table)
2805 pool_destroy(p->table->pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002806
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002807 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002808 p = p->next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002809 HA_SPIN_DESTROY(&p0->lbprm.lock);
2810 HA_SPIN_DESTROY(&p0->lock);
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002811 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002812 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02002813
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002814 while (ua) {
2815 uap = ua;
2816 ua = ua->next;
2817
Willy Tarreaua534fea2008-08-03 12:19:50 +02002818 free(uap->uri_prefix);
2819 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002820 free(uap->node);
2821 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002822
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002823 userlist_free(uap->userlist);
Christopher Fauletcb550132019-12-17 11:25:46 +01002824 deinit_act_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002825
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002826 free(uap);
2827 }
2828
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002829 userlist_free(userlist);
2830
David Carlier834cb2e2015-09-25 12:02:25 +01002831 cfg_unregister_sections();
2832
Christopher Faulet0132d062017-07-26 15:33:35 +02002833 deinit_log_buffers();
David Carlier834cb2e2015-09-25 12:02:25 +01002834
Willy Tarreaudd815982007-10-16 12:25:14 +02002835 protocol_unbind_all();
2836
Willy Tarreau05554e62016-12-21 20:46:26 +01002837 list_for_each_entry(pdf, &post_deinit_list, list)
2838 pdf->fct();
2839
Joe Williamsdf5b38f2010-12-29 17:05:48 +01002840 free(global.log_send_hostname); global.log_send_hostname = NULL;
Dragan Dosen43885c72015-10-01 13:18:13 +02002841 chunk_destroy(&global.log_tag);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002842 free(global.chroot); global.chroot = NULL;
2843 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002844 free(global.node); global.node = NULL;
2845 free(global.desc); global.desc = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002846 free(oldpids); oldpids = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +02002847 task_destroy(idle_conn_task);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01002848 idle_conn_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002849
William Lallemand0f99e342011-10-12 17:50:54 +02002850 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
2851 LIST_DEL(&log->list);
2852 free(log);
2853 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01002854 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02002855 free(wl->s);
Willy Tarreau477ecd82010-01-03 21:12:30 +01002856 LIST_DEL(&wl->list);
2857 free(wl);
2858 }
2859
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002860 list_for_each_entry_safe(bol, bolb, &build_opts_list, list) {
2861 if (bol->must_free)
2862 free((void *)bol->str);
2863 LIST_DEL(&bol->list);
2864 free(bol);
2865 }
2866
Christopher Fauletff2613e2016-11-09 11:36:17 +01002867 vars_prune(&global.vars, NULL, NULL);
Willy Tarreau2455ceb2018-11-26 15:57:34 +01002868 pool_destroy_all();
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002869 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002870} /* end deinit() */
2871
William Lallemand72160322018-11-06 17:37:16 +01002872
Willy Tarreau918ff602011-07-25 16:33:49 +02002873/* Runs the polling loop */
Willy Tarreau3ebd55e2020-03-03 14:59:56 +01002874void run_poll_loop()
Willy Tarreau4f60f162007-04-08 16:39:58 +02002875{
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002876 int next, wake;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002877
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002878 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002879 while (1) {
Willy Tarreauc49ba522019-12-11 08:12:23 +01002880 wake_expired_tasks();
2881
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002882 /* Process a few tasks */
2883 process_runnable_tasks();
2884
William Lallemand1aab50b2018-06-07 09:46:01 +02002885 /* check if we caught some signals and process them in the
2886 first thread */
2887 if (tid == 0)
2888 signal_process_queue();
Willy Tarreau29857942009-05-10 09:01:21 +02002889
Willy Tarreau7067b3a2019-06-02 11:11:29 +02002890 /* also stop if we failed to cleanly stop all tasks */
2891 if (killed > 1)
2892 break;
2893
Willy Tarreau10146c92015-04-13 20:44:19 +02002894 /* expire immediately if events are pending */
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002895 wake = 1;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002896 if (thread_has_tasks())
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002897 activity[tid].wake_tasks++;
William Lallemand1aab50b2018-06-07 09:46:01 +02002898 else if (signal_queue_len && tid == 0)
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002899 activity[tid].wake_signal++;
Olivier Houchard79321b92018-07-26 17:55:11 +02002900 else {
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002901 _HA_ATOMIC_OR(&sleeping_thread_mask, tid_bit);
2902 __ha_barrier_atomic_store();
Willy Tarreau95abd5b2020-03-23 09:33:32 +01002903 if (thread_has_tasks()) {
Olivier Houchard79321b92018-07-26 17:55:11 +02002904 activity[tid].wake_tasks++;
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002905 _HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
Olivier Houchard79321b92018-07-26 17:55:11 +02002906 } else
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002907 wake = 0;
Olivier Houchard79321b92018-07-26 17:55:11 +02002908 }
Willy Tarreau10146c92015-04-13 20:44:19 +02002909
Willy Tarreau4f46a352020-03-23 09:27:28 +01002910 if (!wake) {
Willy Tarreaud7a6b2f2020-05-13 13:51:01 +02002911 int i;
2912
2913 if (stopping) {
Willy Tarreaud6455742020-05-13 14:30:25 +02002914 if (_HA_ATOMIC_OR(&stopping_thread_mask, tid_bit) == tid_bit) {
2915 /* notify all threads that stopping was just set */
2916 for (i = 0; i < global.nbthread; i++)
2917 if ((all_threads_mask >> i) & 1)
2918 wake_thread(i);
2919 }
Willy Tarreaud7a6b2f2020-05-13 13:51:01 +02002920 }
Willy Tarreau4f46a352020-03-23 09:27:28 +01002921
2922 /* stop when there's nothing left to do */
2923 if ((jobs - unstoppable_jobs) == 0 &&
Willy Tarreaud7a6b2f2020-05-13 13:51:01 +02002924 (stopping_thread_mask & all_threads_mask) == all_threads_mask) {
2925 /* wake all threads waiting on jobs==0 */
2926 for (i = 0; i < global.nbthread; i++)
2927 if (((all_threads_mask & ~tid_bit) >> i) & 1)
2928 wake_thread(i);
Willy Tarreau4f46a352020-03-23 09:27:28 +01002929 break;
Willy Tarreaud7a6b2f2020-05-13 13:51:01 +02002930 }
Willy Tarreau4f46a352020-03-23 09:27:28 +01002931 }
2932
Willy Tarreauc49ba522019-12-11 08:12:23 +01002933 /* If we have to sleep, measure how long */
2934 next = wake ? TICK_ETERNITY : next_timer_expiry();
2935
Willy Tarreau58b458d2008-06-29 22:40:23 +02002936 /* The poller will ensure it returns around <next> */
Willy Tarreau2ae84e42019-05-28 16:44:05 +02002937 cur_poller.poll(&cur_poller, next, wake);
Emeric Brun64cc49c2017-10-03 14:46:45 +02002938
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002939 activity[tid].loops++;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002940 }
2941}
2942
Christopher Faulet1d17c102017-08-29 15:38:48 +02002943static void *run_thread_poll_loop(void *data)
2944{
Willy Tarreau082b6282019-05-22 14:42:12 +02002945 struct per_thread_alloc_fct *ptaf;
Christopher Faulet1d17c102017-08-29 15:38:48 +02002946 struct per_thread_init_fct *ptif;
2947 struct per_thread_deinit_fct *ptdf;
Willy Tarreau082b6282019-05-22 14:42:12 +02002948 struct per_thread_free_fct *ptff;
Willy Tarreau34a150c2019-06-11 09:16:41 +02002949 static int init_left = 0;
Willy Tarreauaf613e82020-06-05 08:40:51 +02002950 __decl_thread(static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER);
2951 __decl_thread(static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002952
Willy Tarreaub4f7cc32019-05-03 09:27:30 +02002953 ha_set_tid((unsigned long)data);
Willy Tarreaud022e9c2019-09-24 08:25:15 +02002954 sched = &task_per_thread[tid];
Willy Tarreau91e6df02019-05-03 17:21:18 +02002955
Willy Tarreauf6178242019-05-21 19:46:58 +02002956#if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
Willy Tarreau91e6df02019-05-03 17:21:18 +02002957#ifdef USE_THREAD
Willy Tarreau8323a372019-05-20 18:57:53 +02002958 pthread_getcpuclockid(pthread_self(), &ti->clock_id);
Willy Tarreau624dcbf2019-05-20 20:23:06 +02002959#else
Willy Tarreau8323a372019-05-20 18:57:53 +02002960 ti->clock_id = CLOCK_THREAD_CPUTIME_ID;
Willy Tarreau91e6df02019-05-03 17:21:18 +02002961#endif
Willy Tarreau663fda42019-05-21 15:14:08 +02002962#endif
Willy Tarreau6ec902a2019-06-07 14:41:11 +02002963 /* Now, initialize one thread init at a time. This is better since
2964 * some init code is a bit tricky and may release global resources
2965 * after reallocating them locally. This will also ensure there is
2966 * no race on file descriptors allocation.
2967 */
Willy Tarreau34a150c2019-06-11 09:16:41 +02002968#ifdef USE_THREAD
2969 pthread_mutex_lock(&init_mutex);
2970#endif
2971 /* The first thread must set the number of threads left */
2972 if (!init_left)
2973 init_left = global.nbthread;
2974 init_left--;
Willy Tarreau91e6df02019-05-03 17:21:18 +02002975
Christopher Faulet1d17c102017-08-29 15:38:48 +02002976 tv_update_date(-1,-1);
2977
Willy Tarreau082b6282019-05-22 14:42:12 +02002978 /* per-thread alloc calls performed here are not allowed to snoop on
2979 * other threads, so they are free to initialize at their own rhythm
2980 * as long as they act as if they were alone. None of them may rely
2981 * on resources initialized by the other ones.
2982 */
2983 list_for_each_entry(ptaf, &per_thread_alloc_list, list) {
2984 if (!ptaf->fct()) {
2985 ha_alert("failed to allocate resources for thread %u.\n", tid);
2986 exit(1);
2987 }
2988 }
2989
Willy Tarreau3078e9f2019-05-20 10:50:43 +02002990 /* per-thread init calls performed here are not allowed to snoop on
2991 * other threads, so they are free to initialize at their own rhythm
2992 * as long as they act as if they were alone.
2993 */
Christopher Faulet1d17c102017-08-29 15:38:48 +02002994 list_for_each_entry(ptif, &per_thread_init_list, list) {
2995 if (!ptif->fct()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002996 ha_alert("failed to initialize thread %u.\n", tid);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002997 exit(1);
2998 }
2999 }
3000
Willy Tarreau71092822019-06-10 09:51:04 +02003001 /* enabling protocols will result in fd_insert() calls to be performed,
3002 * we want all threads to have already allocated their local fd tables
Willy Tarreau34a150c2019-06-11 09:16:41 +02003003 * before doing so, thus only the last thread does it.
Willy Tarreau71092822019-06-10 09:51:04 +02003004 */
Willy Tarreau34a150c2019-06-11 09:16:41 +02003005 if (init_left == 0)
Willy Tarreaue4d7c9d2019-06-10 10:14:52 +02003006 protocol_enable_all();
Willy Tarreau6ec902a2019-06-07 14:41:11 +02003007
Willy Tarreau34a150c2019-06-11 09:16:41 +02003008#ifdef USE_THREAD
3009 pthread_cond_broadcast(&init_cond);
3010 pthread_mutex_unlock(&init_mutex);
3011
3012 /* now wait for other threads to finish starting */
3013 pthread_mutex_lock(&init_mutex);
3014 while (init_left)
3015 pthread_cond_wait(&init_cond, &init_mutex);
3016 pthread_mutex_unlock(&init_mutex);
3017#endif
Willy Tarreau3078e9f2019-05-20 10:50:43 +02003018
Willy Tarreaua45a8b52019-12-06 16:31:45 +01003019#if defined(PR_SET_NO_NEW_PRIVS) && defined(USE_PRCTL)
3020 /* Let's refrain from using setuid executables. This way the impact of
3021 * an eventual vulnerability in a library remains limited. It may
3022 * impact external checks but who cares about them anyway ? In the
3023 * worst case it's possible to disable the option. Obviously we do this
3024 * in workers only. We can't hard-fail on this one as it really is
3025 * implementation dependent though we're interested in feedback, hence
3026 * the warning.
3027 */
3028 if (!(global.tune.options & GTUNE_INSECURE_SETUID) && !master) {
3029 static int warn_fail;
3030 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1 && !_HA_ATOMIC_XADD(&warn_fail, 1)) {
3031 ha_warning("Failed to disable setuid, please report to developers with detailed "
3032 "information about your operating system. You can silence this warning "
3033 "by adding 'insecure-setuid-wanted' in the 'global' section.\n");
3034 }
3035 }
3036#endif
3037
Willy Tarreaud96f1122019-12-03 07:07:36 +01003038#if defined(RLIMIT_NPROC)
3039 /* all threads have started, it's now time to prevent any new thread
3040 * or process from starting. Obviously we do this in workers only. We
3041 * can't hard-fail on this one as it really is implementation dependent
3042 * though we're interested in feedback, hence the warning.
3043 */
3044 if (!(global.tune.options & GTUNE_INSECURE_FORK) && !master) {
3045 struct rlimit limit = { .rlim_cur = 0, .rlim_max = 0 };
3046 static int warn_fail;
3047
3048 if (setrlimit(RLIMIT_NPROC, &limit) == -1 && !_HA_ATOMIC_XADD(&warn_fail, 1)) {
3049 ha_warning("Failed to disable forks, please report to developers with detailed "
3050 "information about your operating system. You can silence this warning "
3051 "by adding 'insecure-fork-wanted' in the 'global' section.\n");
3052 }
3053 }
3054#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003055 run_poll_loop();
3056
3057 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
3058 ptdf->fct();
3059
Willy Tarreau082b6282019-05-22 14:42:12 +02003060 list_for_each_entry(ptff, &per_thread_free_list, list)
3061 ptff->fct();
3062
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003063#ifdef USE_THREAD
Olivier Houchardb23a61f2019-03-08 18:51:17 +01003064 _HA_ATOMIC_AND(&all_threads_mask, ~tid_bit);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003065 if (tid > 0)
3066 pthread_exit(NULL);
Christopher Faulet1d17c102017-08-29 15:38:48 +02003067#endif
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003068 return NULL;
3069}
Christopher Faulet1d17c102017-08-29 15:38:48 +02003070
William Dauchyf9af9d72019-11-17 15:47:16 +01003071/* set uid/gid depending on global settings */
3072static void set_identity(const char *program_name)
3073{
3074 if (global.gid) {
3075 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
3076 ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
3077 " without 'uid'/'user' is generally useless.\n", program_name);
3078
3079 if (setgid(global.gid) == -1) {
3080 ha_alert("[%s.main()] Cannot set gid %d.\n", program_name, global.gid);
3081 protocol_unbind_all();
3082 exit(1);
3083 }
3084 }
3085
3086 if (global.uid && setuid(global.uid) == -1) {
3087 ha_alert("[%s.main()] Cannot set uid %d.\n", program_name, global.uid);
3088 protocol_unbind_all();
3089 exit(1);
3090 }
3091}
3092
Willy Tarreaubaaee002006-06-26 02:48:02 +02003093int main(int argc, char **argv)
3094{
3095 int err, retry;
3096 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02003097 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02003098 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003099
Olivier Houchard5fa300d2018-02-03 15:15:21 +01003100 setvbuf(stdout, NULL, _IONBF, 0);
Willy Tarreau5794fb02018-11-25 18:43:29 +01003101
Willy Tarreauff9c9142019-02-07 10:39:36 +01003102 /* this can only safely be done here, though it's optimized away by
3103 * the compiler.
3104 */
3105 if (MAX_PROCS < 1 || MAX_PROCS > LONGBITS) {
3106 ha_alert("MAX_PROCS value must be between 1 and %d inclusive; "
3107 "HAProxy was built with value %d, please fix it and rebuild.\n",
3108 LONGBITS, MAX_PROCS);
3109 exit(1);
3110 }
3111
Willy Tarreaubf696402019-03-01 10:09:28 +01003112 /* take a copy of initial limits before we possibly change them */
3113 getrlimit(RLIMIT_NOFILE, &limit);
3114 rlim_fd_cur_at_boot = limit.rlim_cur;
3115 rlim_fd_max_at_boot = limit.rlim_max;
3116
Willy Tarreau5794fb02018-11-25 18:43:29 +01003117 /* process all initcalls in order of potential dependency */
3118 RUN_INITCALLS(STG_PREPARE);
3119 RUN_INITCALLS(STG_LOCK);
3120 RUN_INITCALLS(STG_ALLOC);
3121 RUN_INITCALLS(STG_POOL);
3122 RUN_INITCALLS(STG_REGISTER);
3123 RUN_INITCALLS(STG_INIT);
3124
Emeric Bruncf20bf12010-10-22 16:06:11 +02003125 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02003126 signal_register_fct(SIGQUIT, dump, SIGQUIT);
3127 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
3128 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
William Lallemand73b85e72017-06-01 17:38:51 +02003129 signal_register_fct(SIGUSR2, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003130
Willy Tarreaue437c442010-03-17 18:02:46 +01003131 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
3132 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
3133 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003134 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02003135 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003136
Willy Tarreaudc23a922011-02-16 11:10:36 +01003137 /* ulimits */
3138 if (!global.rlimit_nofile)
3139 global.rlimit_nofile = global.maxsock;
3140
3141 if (global.rlimit_nofile) {
Willy Tarreaue5cfdac2019-03-01 10:32:05 +01003142 limit.rlim_cur = global.rlimit_nofile;
3143 limit.rlim_max = MAX(rlim_fd_max_at_boot, limit.rlim_cur);
3144
Willy Tarreaudc23a922011-02-16 11:10:36 +01003145 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
Willy Tarreauef635472016-06-21 11:48:18 +02003146 getrlimit(RLIMIT_NOFILE, &limit);
William Dauchy0fec3ab2019-10-27 20:08:11 +01003147 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3148 ha_alert("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n",
3149 argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
3150 if (!(global.mode & MODE_MWORKER))
3151 exit(1);
3152 }
3153 else {
3154 /* try to set it to the max possible at least */
3155 limit.rlim_cur = limit.rlim_max;
3156 if (setrlimit(RLIMIT_NOFILE, &limit) != -1)
3157 getrlimit(RLIMIT_NOFILE, &limit);
Willy Tarreau164dd0b2016-06-21 11:51:59 +02003158
William Dauchy0fec3ab2019-10-27 20:08:11 +01003159 ha_warning("[%s.main()] Cannot raise FD limit to %d, limit is %d. "
3160 "This will fail in >= v2.3\n",
3161 argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
3162 global.rlimit_nofile = limit.rlim_cur;
3163 }
Willy Tarreaudc23a922011-02-16 11:10:36 +01003164 }
3165 }
3166
3167 if (global.rlimit_memmax) {
3168 limit.rlim_cur = limit.rlim_max =
Willy Tarreau70060452015-12-14 12:46:07 +01003169 global.rlimit_memmax * 1048576ULL;
Willy Tarreaudc23a922011-02-16 11:10:36 +01003170#ifdef RLIMIT_AS
3171 if (setrlimit(RLIMIT_AS, &limit) == -1) {
William Dauchy0fec3ab2019-10-27 20:08:11 +01003172 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3173 ha_alert("[%s.main()] Cannot fix MEM limit to %d megs.\n",
3174 argv[0], global.rlimit_memmax);
3175 if (!(global.mode & MODE_MWORKER))
3176 exit(1);
3177 }
3178 else
3179 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs."
3180 "This will fail in >= v2.3\n",
3181 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01003182 }
3183#else
3184 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
William Dauchy0fec3ab2019-10-27 20:08:11 +01003185 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3186 ha_alert("[%s.main()] Cannot fix MEM limit to %d megs.\n",
3187 argv[0], global.rlimit_memmax);
3188 if (!(global.mode & MODE_MWORKER))
3189 exit(1);
3190 }
3191 else
3192 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.",
3193 "This will fail in >= v2.3\n",
3194 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01003195 }
3196#endif
3197 }
3198
Olivier Houchardf73629d2017-04-05 22:33:04 +02003199 if (old_unixsocket) {
William Lallemand85b0bd92017-06-01 17:38:53 +02003200 if (strcmp("/dev/null", old_unixsocket) != 0) {
3201 if (get_old_sockets(old_unixsocket) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003202 ha_alert("Failed to get the sockets from the old process!\n");
William Lallemand85b0bd92017-06-01 17:38:53 +02003203 if (!(global.mode & MODE_MWORKER))
3204 exit(1);
3205 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02003206 }
3207 }
William Lallemand85b0bd92017-06-01 17:38:53 +02003208 get_cur_unixsocket();
3209
Willy Tarreaubaaee002006-06-26 02:48:02 +02003210 /* We will loop at most 100 times with 10 ms delay each time.
3211 * That's at most 1 second. We only send a signal to old pids
3212 * if we cannot grab at least one port.
3213 */
3214 retry = MAX_START_RETRIES;
3215 err = ERR_NONE;
3216 while (retry >= 0) {
3217 struct timeval w;
3218 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01003219 /* exit the loop on no error or fatal error */
3220 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003221 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02003222 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003223 break;
3224
3225 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
3226 * listening sockets. So on those platforms, it would be wiser to
3227 * simply send SIGUSR1, which will not be undoable.
3228 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02003229 if (tell_old_pids(SIGTTOU) == 0) {
3230 /* no need to wait if we can't contact old pids */
3231 retry = 0;
3232 continue;
3233 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003234 /* give some time to old processes to stop listening */
3235 w.tv_sec = 0;
3236 w.tv_usec = 10*1000;
3237 select(0, NULL, NULL, NULL, &w);
3238 retry--;
3239 }
3240
3241 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01003242 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02003243 if (retry != MAX_START_RETRIES && nb_oldpids) {
3244 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003245 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02003246 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003247 exit(1);
3248 }
3249
William Lallemand944e6192018-11-21 15:48:31 +01003250 if (!(global.mode & MODE_MWORKER_WAIT) && listeners == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003251 ha_alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003252 /* Note: we don't have to send anything to the old pids because we
3253 * never stopped them. */
3254 exit(1);
3255 }
3256
Emeric Bruncf20bf12010-10-22 16:06:11 +02003257 err = protocol_bind_all(errmsg, sizeof(errmsg));
3258 if ((err & ~ERR_WARN) != ERR_NONE) {
3259 if ((err & ERR_ALERT) || (err & ERR_WARN))
Christopher Faulet767a84b2017-11-24 16:50:31 +01003260 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Emeric Bruncf20bf12010-10-22 16:06:11 +02003261
Christopher Faulet767a84b2017-11-24 16:50:31 +01003262 ha_alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02003263 protocol_unbind_all(); /* cleanup everything we can */
3264 if (nb_oldpids)
3265 tell_old_pids(SIGTTIN);
3266 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02003267 } else if (err & ERR_WARN) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003268 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02003269 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02003270 /* Ok, all listener should now be bound, close any leftover sockets
3271 * the previous process gave us, we don't need them anymore
3272 */
3273 while (xfer_sock_list != NULL) {
3274 struct xfer_sock_list *tmpxfer = xfer_sock_list->next;
3275 close(xfer_sock_list->fd);
3276 free(xfer_sock_list->iface);
3277 free(xfer_sock_list->namespace);
3278 free(xfer_sock_list);
3279 xfer_sock_list = tmpxfer;
3280 }
Willy Tarreaudd815982007-10-16 12:25:14 +02003281
Willy Tarreaubaaee002006-06-26 02:48:02 +02003282 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02003283 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
3284 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003285
Willy Tarreaubaaee002006-06-26 02:48:02 +02003286 /* MODE_QUIET can inhibit alerts and warnings below this line */
3287
PiBa-NL149a81a2017-12-25 21:03:31 +01003288 if (getenv("HAPROXY_MWORKER_REEXEC") != NULL) {
3289 /* either stdin/out/err are already closed or should stay as they are. */
3290 if ((global.mode & MODE_DAEMON)) {
3291 /* daemon mode re-executing, stdin/stdout/stderr are already closed so keep quiet */
3292 global.mode &= ~MODE_VERBOSE;
3293 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
3294 }
3295 } else {
3296 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
3297 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01003298 stdio_quiet(-1);
PiBa-NL149a81a2017-12-25 21:03:31 +01003299 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003300 }
3301
3302 /* open log & pid files before the chroot */
William Lallemand80293002017-11-06 11:00:03 +01003303 if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003304 unlink(global.pidfile);
3305 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
3306 if (pidfd < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003307 ha_alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003308 if (nb_oldpids)
3309 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02003310 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02003311 exit(1);
3312 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003313 }
3314
Willy Tarreaub38651a2007-03-24 17:24:39 +01003315 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003316 ha_alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
3317 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02003318 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01003319 exit(1);
3320 }
3321
Willy Tarreau4e30ed72009-02-04 18:02:48 +01003322 /* If the user is not root, we'll still let him try the configuration
3323 * but we inform him that unexpected behaviour may occur.
3324 */
3325 if ((global.last_checks & LSTCHK_NETADM) && getuid())
Christopher Faulet767a84b2017-11-24 16:50:31 +01003326 ha_warning("[%s.main()] Some options which require full privileges"
3327 " might not work well.\n"
3328 "", argv[0]);
Willy Tarreau4e30ed72009-02-04 18:02:48 +01003329
William Lallemand095ba4c2017-06-01 17:38:50 +02003330 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
3331
3332 /* chroot if needed */
3333 if (global.chroot != NULL) {
3334 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003335 ha_alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02003336 if (nb_oldpids)
3337 tell_old_pids(SIGTTIN);
3338 protocol_unbind_all();
3339 exit(1);
3340 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02003341 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02003342 }
3343
William Lallemand944e6192018-11-21 15:48:31 +01003344 if (nb_oldpids && !(global.mode & MODE_MWORKER_WAIT))
Willy Tarreaubb545b42010-08-25 12:58:59 +02003345 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003346
William Lallemand27edc4b2019-05-07 17:49:33 +02003347 /* send a SIGTERM to workers who have a too high reloads number */
3348 if ((global.mode & MODE_MWORKER) && !(global.mode & MODE_MWORKER_WAIT))
3349 mworker_kill_max_reloads(SIGTERM);
3350
William Lallemand8a361b52017-06-20 11:20:33 +02003351 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) {
3352 nb_oldpids = 0;
3353 free(oldpids);
3354 oldpids = NULL;
3355 }
3356
3357
Willy Tarreaubaaee002006-06-26 02:48:02 +02003358 /* Note that any error at this stage will be fatal because we will not
3359 * be able to restart the old pids.
3360 */
3361
William Dauchyf9af9d72019-11-17 15:47:16 +01003362 if ((global.mode & (MODE_MWORKER | MODE_DAEMON)) == 0)
3363 set_identity(argv[0]);
Willy Tarreau636848a2019-04-15 19:38:50 +02003364
Willy Tarreaubaaee002006-06-26 02:48:02 +02003365 /* check ulimits */
3366 limit.rlim_cur = limit.rlim_max = 0;
3367 getrlimit(RLIMIT_NOFILE, &limit);
3368 if (limit.rlim_cur < global.maxsock) {
William Dauchy0fec3ab2019-10-27 20:08:11 +01003369 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3370 ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. "
3371 "Please raise 'ulimit-n' to %d or more to avoid any trouble.\n",
3372 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock,
3373 global.maxsock);
3374 if (!(global.mode & MODE_MWORKER))
3375 exit(1);
3376 }
3377 else
3378 ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. "
3379 "Please raise 'ulimit-n' to %d or more to avoid any trouble."
3380 "This will fail in >= v2.3\n",
3381 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock,
3382 global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003383 }
3384
William Lallemand944e6192018-11-21 15:48:31 +01003385 if (global.mode & (MODE_DAEMON | MODE_MWORKER | MODE_MWORKER_WAIT)) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003386 struct proxy *px;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003387 struct peers *curpeers;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003388 int ret = 0;
3389 int proc;
William Lallemande1340412017-12-28 16:09:36 +01003390 int devnullfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003391
William Lallemand095ba4c2017-06-01 17:38:50 +02003392 /*
3393 * if daemon + mworker: must fork here to let a master
3394 * process live in background before forking children
3395 */
William Lallemand73b85e72017-06-01 17:38:51 +02003396
3397 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
3398 && (global.mode & MODE_MWORKER)
3399 && (global.mode & MODE_DAEMON)) {
William Lallemand095ba4c2017-06-01 17:38:50 +02003400 ret = fork();
3401 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003402 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02003403 protocol_unbind_all();
3404 exit(1); /* there has been an error */
William Lallemandbfd8eb52018-07-04 15:31:23 +02003405 } else if (ret > 0) { /* parent leave to daemonize */
William Lallemand095ba4c2017-06-01 17:38:50 +02003406 exit(0);
William Lallemandbfd8eb52018-07-04 15:31:23 +02003407 } else /* change the process group ID in the child (master process) */
3408 setsid();
William Lallemand095ba4c2017-06-01 17:38:50 +02003409 }
William Lallemande20b6a62017-06-01 17:38:55 +02003410
William Lallemande20b6a62017-06-01 17:38:55 +02003411
William Lallemanddeed7802017-11-06 11:00:04 +01003412 /* if in master-worker mode, write the PID of the father */
3413 if (global.mode & MODE_MWORKER) {
3414 char pidstr[100];
Willy Tarreau76a80c72019-06-22 07:41:38 +02003415 snprintf(pidstr, sizeof(pidstr), "%d\n", (int)getpid());
Willy Tarreau46ec48b2018-01-23 19:20:19 +01003416 if (pidfd >= 0)
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01003417 DISGUISE(write(pidfd, pidstr, strlen(pidstr)));
William Lallemanddeed7802017-11-06 11:00:04 +01003418 }
3419
Willy Tarreaubaaee002006-06-26 02:48:02 +02003420 /* the father launches the required number of processes */
William Lallemand944e6192018-11-21 15:48:31 +01003421 if (!(global.mode & MODE_MWORKER_WAIT)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +02003422 if (global.mode & MODE_MWORKER)
3423 mworker_ext_launch_all();
William Lallemand944e6192018-11-21 15:48:31 +01003424 for (proc = 0; proc < global.nbproc; proc++) {
3425 ret = fork();
3426 if (ret < 0) {
3427 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
3428 protocol_unbind_all();
3429 exit(1); /* there has been an error */
3430 }
Willy Tarreau52bf8392020-03-08 00:42:37 +01003431 else if (ret == 0) { /* child breaks here */
3432 ha_random_jump96(relative_pid);
William Lallemand944e6192018-11-21 15:48:31 +01003433 break;
Willy Tarreau52bf8392020-03-08 00:42:37 +01003434 }
William Lallemand944e6192018-11-21 15:48:31 +01003435 if (pidfd >= 0 && !(global.mode & MODE_MWORKER)) {
3436 char pidstr[100];
3437 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01003438 DISGUISE(write(pidfd, pidstr, strlen(pidstr)));
William Lallemand944e6192018-11-21 15:48:31 +01003439 }
3440 if (global.mode & MODE_MWORKER) {
3441 struct mworker_proc *child;
William Lallemandce83b4a2018-10-26 14:47:30 +02003442
William Lallemand220567e2018-11-21 18:04:53 +01003443 ha_notice("New worker #%d (%d) forked\n", relative_pid, ret);
William Lallemand944e6192018-11-21 15:48:31 +01003444 /* find the right mworker_proc */
3445 list_for_each_entry(child, &proc_list, list) {
3446 if (child->relative_pid == relative_pid &&
William Lallemand8f7069a2019-04-12 16:09:23 +02003447 child->reloads == 0 && child->options & PROC_O_TYPE_WORKER) {
William Lallemand944e6192018-11-21 15:48:31 +01003448 child->timestamp = now.tv_sec;
3449 child->pid = ret;
William Lallemand1dc69632019-06-12 19:11:33 +02003450 child->version = strdup(haproxy_version);
William Lallemand944e6192018-11-21 15:48:31 +01003451 break;
3452 }
William Lallemandce83b4a2018-10-26 14:47:30 +02003453 }
3454 }
William Lallemandbc193052018-09-11 10:06:26 +02003455
William Lallemand944e6192018-11-21 15:48:31 +01003456 relative_pid++; /* each child will get a different one */
3457 pid_bit <<= 1;
3458 }
3459 } else {
3460 /* wait mode */
3461 global.nbproc = 1;
3462 proc = 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003463 }
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003464
3465#ifdef USE_CPU_AFFINITY
3466 if (proc < global.nbproc && /* child */
Willy Tarreauff9c9142019-02-07 10:39:36 +01003467 proc < MAX_PROCS && /* only the first 32/64 processes may be pinned */
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003468 global.cpu_map.proc[proc]) /* only do this if the process has a CPU map */
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003469#ifdef __FreeBSD__
Olivier Houchard97148f62017-08-16 17:29:11 +02003470 {
3471 cpuset_t cpuset;
3472 int i;
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003473 unsigned long cpu_map = global.cpu_map.proc[proc];
Olivier Houchard97148f62017-08-16 17:29:11 +02003474
3475 CPU_ZERO(&cpuset);
3476 while ((i = ffsl(cpu_map)) > 0) {
3477 CPU_SET(i - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003478 cpu_map &= ~(1UL << (i - 1));
Olivier Houchard97148f62017-08-16 17:29:11 +02003479 }
3480 ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset);
3481 }
David Carlier5e4c8e22019-09-13 05:12:58 +01003482#elif defined(__linux__)
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003483 sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map.proc[proc]);
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003484#endif
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003485#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003486 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02003487 if (pidfd >= 0) {
3488 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
3489 close(pidfd);
3490 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02003491
3492 /* We won't ever use this anymore */
Willy Tarreaud137dd32010-08-25 12:49:05 +02003493 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003494
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003495 if (proc == global.nbproc) {
William Lallemand944e6192018-11-21 15:48:31 +01003496 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003497
3498 if ((!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
3499 (global.mode & MODE_DAEMON)) {
3500 /* detach from the tty, this is required to properly daemonize. */
William Lallemande1340412017-12-28 16:09:36 +01003501 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL))
3502 stdio_quiet(-1);
3503
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003504 global.mode &= ~MODE_VERBOSE;
3505 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003506 }
3507
William Lallemandb3f2be32018-09-11 10:06:18 +02003508 mworker_loop();
William Lallemand1499b9b2017-06-07 15:04:47 +02003509 /* should never get there */
3510 exit(EXIT_FAILURE);
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003511 }
William Lallemandcf4e4962017-06-08 19:05:48 +02003512#if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH)
Grant Zhang872f9c22017-01-21 01:10:18 +00003513 ssl_free_dh();
3514#endif
William Lallemand1499b9b2017-06-07 15:04:47 +02003515 exit(0); /* parent must leave */
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003516 }
3517
William Lallemandcb11fd22017-06-01 17:38:52 +02003518 /* child must never use the atexit function */
3519 atexit_flag = 0;
3520
William Lallemandbc193052018-09-11 10:06:26 +02003521 /* close useless master sockets */
3522 if (global.mode & MODE_MWORKER) {
3523 struct mworker_proc *child, *it;
3524 master = 0;
3525
William Lallemand309dc9a2018-10-26 14:47:45 +02003526 mworker_cli_proxy_stop();
3527
William Lallemandbc193052018-09-11 10:06:26 +02003528 /* free proc struct of other processes */
3529 list_for_each_entry_safe(child, it, &proc_list, list) {
William Lallemandce83b4a2018-10-26 14:47:30 +02003530 /* close the FD of the master side for all
3531 * workers, we don't need to close the worker
3532 * side of other workers since it's done with
3533 * the bind_proc */
Tim Duesterhus742e0f92018-11-25 20:03:39 +01003534 if (child->ipc_fd[0] >= 0)
3535 close(child->ipc_fd[0]);
William Lallemandce83b4a2018-10-26 14:47:30 +02003536 if (child->relative_pid == relative_pid &&
3537 child->reloads == 0) {
3538 /* keep this struct if this is our pid */
3539 proc_self = child;
William Lallemandbc193052018-09-11 10:06:26 +02003540 continue;
William Lallemandce83b4a2018-10-26 14:47:30 +02003541 }
William Lallemandbc193052018-09-11 10:06:26 +02003542 LIST_DEL(&child->list);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +02003543 mworker_free_child(child);
3544 child = NULL;
William Lallemandbc193052018-09-11 10:06:26 +02003545 }
3546 }
Willy Tarreau1605c7a2018-01-23 19:01:49 +01003547
William Lallemande1340412017-12-28 16:09:36 +01003548 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
3549 devnullfd = open("/dev/null", O_RDWR, 0);
3550 if (devnullfd < 0) {
3551 ha_alert("Cannot open /dev/null\n");
3552 exit(EXIT_FAILURE);
3553 }
3554 }
3555
William Lallemand095ba4c2017-06-01 17:38:50 +02003556 /* Must chroot and setgid/setuid in the children */
3557 /* chroot if needed */
3558 if (global.chroot != NULL) {
3559 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003560 ha_alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02003561 if (nb_oldpids)
3562 tell_old_pids(SIGTTIN);
3563 protocol_unbind_all();
3564 exit(1);
3565 }
3566 }
3567
3568 free(global.chroot);
3569 global.chroot = NULL;
William Dauchyf9af9d72019-11-17 15:47:16 +01003570 set_identity(argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02003571
William Lallemand7f80eb22017-05-26 18:19:55 +02003572 /* pass through every cli socket, and check if it's bound to
3573 * the current process and if it exposes listeners sockets.
3574 * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork.
3575 * */
3576
3577 if (global.stats_fe) {
3578 struct bind_conf *bind_conf;
3579
3580 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
3581 if (bind_conf->level & ACCESS_FD_LISTENERS) {
3582 if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) {
3583 global.tune.options |= GTUNE_SOCKET_TRANSFER;
3584 break;
3585 }
3586 }
3587 }
3588 }
3589
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003590 /* we might have to unbind some proxies from some processes */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003591 px = proxies_list;
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003592 while (px != NULL) {
3593 if (px->bind_proc && px->state != PR_STSTOPPED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02003594 if (!(px->bind_proc & (1UL << proc))) {
3595 if (global.tune.options & GTUNE_SOCKET_TRANSFER)
3596 zombify_proxy(px);
3597 else
3598 stop_proxy(px);
3599 }
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003600 }
3601 px = px->next;
3602 }
3603
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003604 /* we might have to unbind some peers sections from some processes */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003605 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003606 if (!curpeers->peers_fe)
3607 continue;
3608
3609 if (curpeers->peers_fe->bind_proc & (1UL << proc))
3610 continue;
3611
3612 stop_proxy(curpeers->peers_fe);
3613 /* disable this peer section so that it kills itself */
Willy Tarreau47c8c022015-09-28 16:39:25 +02003614 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02003615 task_destroy(curpeers->sync_task);
Willy Tarreau47c8c022015-09-28 16:39:25 +02003616 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +02003617 task_destroy(curpeers->peers_fe->task);
Willy Tarreau47c8c022015-09-28 16:39:25 +02003618 curpeers->peers_fe->task = NULL;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003619 curpeers->peers_fe = NULL;
3620 }
3621
William Lallemand2e8fad92018-11-13 16:18:23 +01003622 /*
3623 * This is only done in daemon mode because we might want the
3624 * logs on stdout in mworker mode. If we're NOT in QUIET mode,
3625 * we should now close the 3 first FDs to ensure that we can
3626 * detach from the TTY. We MUST NOT do it in other cases since
3627 * it would have already be done, and 0-2 would have been
3628 * affected to listening sockets
Willy Tarreaubaaee002006-06-26 02:48:02 +02003629 */
William Lallemand2e8fad92018-11-13 16:18:23 +01003630 if ((global.mode & MODE_DAEMON) &&
3631 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003632 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01003633 stdio_quiet(devnullfd);
Willy Tarreau106cb762008-11-16 07:40:34 +01003634 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003635 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
3636 }
3637 pid = getpid(); /* update child's pid */
William Lallemandbfd8eb52018-07-04 15:31:23 +02003638 if (!(global.mode & MODE_MWORKER)) /* in mworker mode we don't want a new pgid for the children */
3639 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02003640 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02003641 }
3642
William Dauchye039f262019-11-17 15:47:15 +01003643 /* try our best to re-enable core dumps depending on system capabilities.
3644 * What is addressed here :
3645 * - remove file size limits
3646 * - remove core size limits
3647 * - mark the process dumpable again if it lost it due to user/group
3648 */
3649 if (global.tune.options & GTUNE_SET_DUMPABLE) {
3650 limit.rlim_cur = limit.rlim_max = RLIM_INFINITY;
3651
3652#if defined(RLIMIT_FSIZE)
3653 if (setrlimit(RLIMIT_FSIZE, &limit) == -1) {
3654 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3655 ha_alert("[%s.main()] Failed to set the raise the maximum "
3656 "file size.\n", argv[0]);
3657 if (!(global.mode & MODE_MWORKER))
3658 exit(1);
3659 }
3660 else
3661 ha_warning("[%s.main()] Failed to set the raise the maximum "
3662 "file size. This will fail in >= v2.3\n", argv[0]);
3663 }
3664#endif
3665
3666#if defined(RLIMIT_CORE)
3667 if (setrlimit(RLIMIT_CORE, &limit) == -1) {
3668 if (global.tune.options & GTUNE_STRICT_LIMITS) {
3669 ha_alert("[%s.main()] Failed to set the raise the core "
3670 "dump size.\n", argv[0]);
3671 if (!(global.mode & MODE_MWORKER))
3672 exit(1);
3673 }
3674 else
3675 ha_warning("[%s.main()] Failed to set the raise the core "
3676 "dump size. This will fail in >= v2.3\n", argv[0]);
3677 }
3678#endif
3679
3680#if defined(USE_PRCTL)
3681 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1)
3682 ha_warning("[%s.main()] Failed to set the dumpable flag, "
3683 "no core will be dumped.\n", argv[0]);
3684#endif
3685 }
3686
Christopher Faulete3a5e352017-10-24 13:53:54 +02003687 global.mode &= ~MODE_STARTING;
Willy Tarreau4f60f162007-04-08 16:39:58 +02003688 /*
3689 * That's it : the central polling loop. Run until we stop.
3690 */
Christopher Faulet1d17c102017-08-29 15:38:48 +02003691#ifdef USE_THREAD
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003692 {
William Lallemand1aab50b2018-06-07 09:46:01 +02003693 sigset_t blocked_sig, old_sig;
Willy Tarreauc40efc12019-05-03 09:22:44 +02003694 int i;
3695
William Lallemand1aab50b2018-06-07 09:46:01 +02003696 /* ensure the signals will be blocked in every thread */
3697 sigfillset(&blocked_sig);
3698 sigdelset(&blocked_sig, SIGPROF);
3699 sigdelset(&blocked_sig, SIGBUS);
3700 sigdelset(&blocked_sig, SIGFPE);
3701 sigdelset(&blocked_sig, SIGILL);
3702 sigdelset(&blocked_sig, SIGSEGV);
3703 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
3704
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003705 /* Create nbthread-1 thread. The first thread is the current process */
David Carliera92c5ce2019-09-13 05:03:12 +01003706 ha_thread_info[0].pthread = pthread_self();
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003707 for (i = 1; i < global.nbthread; i++)
David Carliera92c5ce2019-09-13 05:03:12 +01003708 pthread_create(&ha_thread_info[i].pthread, NULL, &run_thread_poll_loop, (void *)(long)i);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003709
Christopher Faulet62519022017-10-16 15:49:32 +02003710#ifdef USE_CPU_AFFINITY
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003711 /* Now the CPU affinity for all threads */
Willy Tarreau7764a572019-07-16 15:10:34 +02003712 if (global.cpu_map.proc_t1[relative_pid-1])
3713 global.cpu_map.thread[0] &= global.cpu_map.proc_t1[relative_pid-1];
3714
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003715 for (i = 0; i < global.nbthread; i++) {
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003716 if (global.cpu_map.proc[relative_pid-1])
Willy Tarreau81492c92019-05-03 09:41:23 +02003717 global.cpu_map.thread[i] &= global.cpu_map.proc[relative_pid-1];
Christopher Faulet62519022017-10-16 15:49:32 +02003718
Willy Tarreau421f02e2018-01-20 18:19:22 +01003719 if (i < MAX_THREADS && /* only the first 32/64 threads may be pinned */
Willy Tarreau81492c92019-05-03 09:41:23 +02003720 global.cpu_map.thread[i]) {/* only do this if the thread has a THREAD map */
David Carlier5e4c8e22019-09-13 05:12:58 +01003721#if defined(__APPLE__)
3722 int j;
3723 unsigned long cpu_map = global.cpu_map.thread[i];
3724
3725 while ((j = ffsl(cpu_map)) > 0) {
3726 thread_affinity_policy_data_t cpu_set = { j - 1 };
3727 thread_port_t mthread = pthread_mach_thread_np(ha_thread_info[i].pthread);
3728 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
3729 cpu_map &= ~(1UL << (j - 1));
3730 }
3731#else
Olivier Houchard829aa242017-12-01 18:19:43 +01003732#if defined(__FreeBSD__) || defined(__NetBSD__)
3733 cpuset_t cpuset;
3734#else
3735 cpu_set_t cpuset;
3736#endif
3737 int j;
Willy Tarreau81492c92019-05-03 09:41:23 +02003738 unsigned long cpu_map = global.cpu_map.thread[i];
Olivier Houchard829aa242017-12-01 18:19:43 +01003739
3740 CPU_ZERO(&cpuset);
3741
3742 while ((j = ffsl(cpu_map)) > 0) {
3743 CPU_SET(j - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003744 cpu_map &= ~(1UL << (j - 1));
Olivier Houchard829aa242017-12-01 18:19:43 +01003745 }
David Carliera92c5ce2019-09-13 05:03:12 +01003746 pthread_setaffinity_np(ha_thread_info[i].pthread,
Olivier Houchard829aa242017-12-01 18:19:43 +01003747 sizeof(cpuset), &cpuset);
David Carlier5e4c8e22019-09-13 05:12:58 +01003748#endif
Olivier Houchard829aa242017-12-01 18:19:43 +01003749 }
Christopher Faulet1d17c102017-08-29 15:38:48 +02003750 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003751#endif /* !USE_CPU_AFFINITY */
3752
William Lallemand1aab50b2018-06-07 09:46:01 +02003753 /* when multithreading we need to let only the thread 0 handle the signals */
William Lallemandd3801c12018-09-11 10:06:23 +02003754 haproxy_unblock_signals();
William Lallemand1aab50b2018-06-07 09:46:01 +02003755
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003756 /* Finally, start the poll loop for the first thread */
Willy Tarreaub4f7cc32019-05-03 09:27:30 +02003757 run_thread_poll_loop(0);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003758
3759 /* Wait the end of other threads */
3760 for (i = 1; i < global.nbthread; i++)
David Carliera92c5ce2019-09-13 05:03:12 +01003761 pthread_join(ha_thread_info[i].pthread, NULL);
Christopher Faulet1d17c102017-08-29 15:38:48 +02003762
Christopher Fauletb79a94c2017-05-30 15:34:30 +02003763#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
3764 show_lock_stats();
3765#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003766 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003767#else /* ! USE_THREAD */
William Lallemandd3801c12018-09-11 10:06:23 +02003768 haproxy_unblock_signals();
Willy Tarreaub4f7cc32019-05-03 09:27:30 +02003769 run_thread_poll_loop(0);
Christopher Faulet62519022017-10-16 15:49:32 +02003770#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003771
3772 /* Do some cleanup */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003773 deinit();
Christopher Faulet1d17c102017-08-29 15:38:48 +02003774
Willy Tarreaubaaee002006-06-26 02:48:02 +02003775 exit(0);
3776}
3777
Willy Tarreaubb1b63c2020-04-15 17:00:03 +02003778#if defined(__clang_version__)
3779REGISTER_BUILD_OPTS("Built with clang compiler version " __clang_version__);
3780#elif defined(__VERSION__)
3781REGISTER_BUILD_OPTS("Built with gcc compiler version " __VERSION__);
3782#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003783
3784/*
3785 * Local variables:
3786 * c-indent-level: 8
3787 * c-basic-offset: 8
3788 * End:
3789 */