blob: f0cb2baae372e28bbc18c7295c623b09eb07dfbd [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HA-Proxy : High Availability-enabled HTTP/TCP proxy
Willy Tarreau6c1b6672019-02-26 16:43:49 +01003 * Copyright 2000-2019 Willy Tarreau <willy@haproxy.org>.
Willy Tarreaubaaee002006-06-26 02:48:02 +02004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
Lukas Tribus23953682017-04-28 13:24:30 +000010 * Please refer to RFC7230 - RFC7235 informations about HTTP protocol, and
11 * RFC6265 for informations about cookies usage. More generally, the IETF HTTP
Willy Tarreaubaaee002006-06-26 02:48:02 +020012 * Working Group's web site should be consulted for protocol related changes :
13 *
14 * http://ftp.ics.uci.edu/pub/ietf/http/
15 *
16 * Pending bugs (may be not fixed because never reproduced) :
17 * - solaris only : sometimes, an HTTP proxy with only a dispatch address causes
18 * the proxy to terminate (no core) if the client breaks the connection during
19 * the response. Seen on 1.1.8pre4, but never reproduced. May not be related to
20 * the snprintf() bug since requests were simple (GET / HTTP/1.0), but may be
21 * related to missing setsid() (fixed in 1.1.15)
22 * - a proxy with an invalid config will prevent the startup even if disabled.
23 *
24 * ChangeLog has moved to the CHANGELOG file.
25 *
Willy Tarreaubaaee002006-06-26 02:48:02 +020026 */
27
David Carlier7ece0962015-12-08 21:43:09 +000028#define _GNU_SOURCE
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <string.h>
33#include <ctype.h>
Maxime de Roucy379d9c72016-05-13 23:52:56 +020034#include <dirent.h>
Maxime de Roucy379d9c72016-05-13 23:52:56 +020035#include <sys/stat.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <sys/time.h>
37#include <sys/types.h>
38#include <sys/socket.h>
39#include <netinet/tcp.h>
40#include <netinet/in.h>
41#include <arpa/inet.h>
Olivier Houchardf73629d2017-04-05 22:33:04 +020042#include <net/if.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <netdb.h>
44#include <fcntl.h>
45#include <errno.h>
46#include <signal.h>
47#include <stdarg.h>
48#include <sys/resource.h>
Marc-Antoine Perennou992709b2013-02-12 10:53:52 +010049#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <time.h>
51#include <syslog.h>
Michael Schererab012dd2013-01-12 18:35:19 +010052#include <grp.h>
Willy Tarreaufc6c0322012-11-16 16:12:27 +010053#ifdef USE_CPU_AFFINITY
Willy Tarreaufc6c0322012-11-16 16:12:27 +010054#include <sched.h>
David Carlier42d9e5a2018-11-12 16:22:19 +000055#if defined(__FreeBSD__) || defined(__DragonFly__)
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +020056#include <sys/param.h>
David Carlier42d9e5a2018-11-12 16:22:19 +000057#ifdef __FreeBSD__
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +020058#include <sys/cpuset.h>
David Carlier42d9e5a2018-11-12 16:22:19 +000059#endif
David Carlier6d5c8412017-11-29 11:02:32 +000060#include <pthread_np.h>
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +020061#endif
Willy Tarreaufc6c0322012-11-16 16:12:27 +010062#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020063
64#ifdef DEBUG_FULL
65#include <assert.h>
66#endif
Tim Duesterhusd6942c82017-11-20 15:58:35 +010067#if defined(USE_SYSTEMD)
68#include <systemd/sd-daemon.h>
69#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020070
Willy Tarreau2dd0d472006-06-29 17:53:05 +020071#include <common/base64.h>
72#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020073#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020074#include <common/compat.h>
75#include <common/config.h>
76#include <common/defaults.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010077#include <common/errors.h>
Willy Tarreau5794fb02018-11-25 18:43:29 +010078#include <common/initcall.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020079#include <common/memory.h>
80#include <common/mini-clist.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010081#include <common/namespace.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020082#include <common/regex.h>
83#include <common/standard.h>
84#include <common/time.h>
85#include <common/uri_auth.h>
86#include <common/version.h>
Christopher Fauletbe0faa22017-08-29 15:37:10 +020087#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020088
89#include <types/capture.h>
William Lallemandce83b4a2018-10-26 14:47:30 +020090#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020091#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020092#include <types/global.h>
Simon Hormanac821422011-07-15 13:14:09 +090093#include <types/acl.h>
Willy Tarreau3c63fd82011-09-07 18:00:47 +020094#include <types/peers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020095
Willy Tarreau0fc45a72007-06-17 00:36:03 +020096#include <proto/acl.h>
Willy Tarreau609aad92018-11-22 08:31:09 +010097#include <proto/activity.h>
Willy Tarreau2e845be2012-10-19 19:49:09 +020098#include <proto/arg.h>
Willy Tarreau3c595ac2015-04-19 09:59:31 +020099#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +0200101#include <proto/channel.h>
William Lallemandce83b4a2018-10-26 14:47:30 +0200102#include <proto/cli.h>
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200103#include <proto/connection.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +0200105#include <proto/filters.h>
Willy Tarreau34eb6712011-10-24 18:15:04 +0200106#include <proto/hdr_idx.h>
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +0100107#include <proto/hlua.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +0200108#include <proto/http_rules.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +0200109#include <proto/listener.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110#include <proto/log.h>
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100111#include <proto/pattern.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +0200112#include <proto/protocol.h>
Willy Tarreau80587432006-12-24 17:47:20 +0100113#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114#include <proto/proxy.h>
115#include <proto/queue.h>
116#include <proto/server.h>
Willy Tarreaub1ec8c42015-04-03 13:53:24 +0200117#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +0200118#include <proto/stream.h>
Willy Tarreau29857942009-05-10 09:01:21 +0200119#include <proto/signal.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120#include <proto/task.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +0200121#include <proto/dns.h>
Christopher Fauletff2613e2016-11-09 11:36:17 +0100122#include <proto/vars.h>
Willy Tarreau50bc31d2017-08-16 15:35:19 +0200123#ifdef USE_OPENSSL
Grant Zhang872f9c22017-01-21 01:10:18 +0000124#include <proto/ssl_sock.h>
Willy Tarreau50bc31d2017-08-16 15:35:19 +0200125#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126
Willy Tarreau7b5654f2019-03-29 21:30:17 +0100127/* array of init calls for older platforms */
128DECLARE_INIT_STAGES;
129
Willy Tarreau477ecd82010-01-03 21:12:30 +0100130/* list of config files */
131static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200132int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100133int relative_pid = 1; /* process id starting at 1 */
Willy Tarreau387bd4f2017-11-10 19:08:14 +0100134unsigned long pid_bit = 1; /* bit corresponding to the process id */
Willy Tarreaua38a7172019-02-02 17:11:28 +0100135unsigned long all_proc_mask = 1; /* mask of all processes */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200136
Olivier Houchard79321b92018-07-26 17:55:11 +0200137volatile unsigned long sleeping_thread_mask; /* Threads that are about to sleep in poll() */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200138/* global options */
139struct global global = {
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100140 .hard_stop_after = TICK_ETERNITY,
Willy Tarreau247a13a2012-11-15 17:38:15 +0100141 .nbproc = 1,
Willy Tarreau149ab772019-01-26 14:27:06 +0100142 .nbthread = 0,
William Lallemand5f232402012-04-05 18:02:55 +0200143 .req_count = 0,
William Lallemand0f99e342011-10-12 17:50:54 +0200144 .logsrvs = LIST_HEAD_INIT(global.logsrvs),
William Lallemand9d5f5482012-11-07 16:12:57 +0100145 .maxzlibmem = 0,
William Lallemandd85f9172012-11-09 17:05:39 +0100146 .comp_rate_lim = 0,
Emeric Brun850efd52014-01-29 12:24:34 +0100147 .ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED,
Emeric Bruned760922010-10-22 17:59:25 +0200148 .unix_bind = {
149 .ux = {
150 .uid = -1,
151 .gid = -1,
152 .mode = 0,
153 }
154 },
Willy Tarreau27a674e2009-08-17 07:23:33 +0200155 .tune = {
Willy Tarreau7ac908b2019-02-27 12:02:18 +0100156 .options = GTUNE_LISTENER_MQ,
Willy Tarreauc77d3642018-12-12 06:19:42 +0100157 .bufsize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)),
Willy Tarreau27097842015-09-28 13:53:23 +0200158 .maxrewrite = -1,
Willy Tarreauc77d3642018-12-12 06:19:42 +0100159 .chksize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)),
Willy Tarreaua24adf02014-11-27 01:11:56 +0100160 .reserved_bufs = RESERVED_BUFS,
Willy Tarreauf3045d22015-04-29 16:24:50 +0200161 .pattern_cache = DEFAULT_PAT_LRU_SIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200162#ifdef USE_OPENSSL
Emeric Brun46635772012-11-14 11:32:56 +0100163 .sslcachesize = SSLCACHESIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200164#endif
William Lallemandf3747832012-11-09 12:33:10 +0100165 .comp_maxlevel = 1,
Willy Tarreau7e312732014-02-12 16:35:14 +0100166#ifdef DEFAULT_IDLE_TIMER
167 .idle_timer = DEFAULT_IDLE_TIMER,
168#else
169 .idle_timer = 1000, /* 1 second */
170#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200171 },
Emeric Brun76d88952012-10-05 15:47:31 +0200172#ifdef USE_OPENSSL
173#ifdef DEFAULT_MAXSSLCONN
Willy Tarreau403edff2012-09-06 11:58:37 +0200174 .maxsslconn = DEFAULT_MAXSSLCONN,
175#endif
Emeric Brun76d88952012-10-05 15:47:31 +0200176#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200177 /* others NULL OK */
178};
179
180/*********************************************************************/
181
182int stopping; /* non zero means stopping in progress */
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100183int killed; /* non zero means a hard-stop is triggered */
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200184int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */
William Lallemanda7199262018-11-16 16:57:20 +0100185int unstoppable_jobs = 0; /* number of active jobs that can't be stopped during a soft stop */
Willy Tarreau199ad242018-11-05 16:31:22 +0100186int active_peers = 0; /* number of active peers (connection attempts and connected) */
Willy Tarreau2d372c22018-11-05 17:12:27 +0100187int connected_peers = 0; /* number of connected peers (verified ones) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200188
189/* Here we store informations about the pids of the processes we may pause
190 * or kill. We will send them a signal every 10 ms until we can bind to all
191 * our ports. With 200 retries, that's about 2 seconds.
192 */
193#define MAX_START_RETRIES 200
Willy Tarreaubaaee002006-06-26 02:48:02 +0200194static int *oldpids = NULL;
195static int oldpids_sig; /* use USR1 or TERM */
196
Olivier Houchardf73629d2017-04-05 22:33:04 +0200197/* Path to the unix socket we use to retrieve listener sockets from the old process */
198static const char *old_unixsocket;
199
William Lallemand85b0bd92017-06-01 17:38:53 +0200200static char *cur_unixsocket = NULL;
201
William Lallemandcb11fd22017-06-01 17:38:52 +0200202int atexit_flag = 0;
203
William Lallemand91723742018-11-06 17:37:14 +0100204static int exitcode = -1;
205
Willy Tarreaubb545b42010-08-25 12:58:59 +0200206int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200207const int zero = 0;
208const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200209const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200210
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100211char hostname[MAX_HOSTNAME_LEN];
Emeric Brun2b920a12010-09-23 18:30:22 +0200212char localpeer[MAX_HOSTNAME_LEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200213
Willy Tarreau89efaed2013-12-13 15:14:55 +0100214/* used from everywhere just to drain results we don't want to read and which
215 * recent versions of gcc increasingly and annoyingly complain about.
216 */
217int shut_your_big_mouth_gcc_int = 0;
218
William Lallemand73b85e72017-06-01 17:38:51 +0200219int *children = NULL; /* store PIDs of children in master workers mode */
220
William Lallemand73b85e72017-06-01 17:38:51 +0200221static char **next_argv = NULL;
222
William Lallemandbc193052018-09-11 10:06:26 +0200223struct list proc_list = LIST_HEAD_INIT(proc_list);
224
225int master = 0; /* 1 if in master, 0 if in child */
Willy Tarreaubf696402019-03-01 10:09:28 +0100226unsigned int rlim_fd_cur_at_boot = 0;
227unsigned int rlim_fd_max_at_boot = 0;
William Lallemandbc193052018-09-11 10:06:26 +0200228
William Lallemand16dd1b32018-11-19 18:46:18 +0100229struct mworker_proc *proc_self = NULL;
William Lallemandbc193052018-09-11 10:06:26 +0200230
Willy Tarreau08ceb102011-07-24 22:58:00 +0200231/* list of the temporarily limited listeners because of lack of resource */
232struct list global_listener_queue = LIST_HEAD_INIT(global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +0200233struct task *global_listener_queue_task;
Olivier Houchard9f6af332018-05-25 14:04:04 +0200234static struct task *manage_global_listener_queue(struct task *t, void *context, unsigned short state);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200235
William Lallemandb3f2be32018-09-11 10:06:18 +0200236static void *run_thread_poll_loop(void *data);
237
Willy Tarreauff055502014-04-28 22:27:06 +0200238/* bitfield of a few warnings to emit just once (WARN_*) */
239unsigned int warned = 0;
240
William Lallemande7361152018-10-26 14:47:36 +0200241/* master CLI configuration (-S flag) */
242struct list mworker_cli_conf = LIST_HEAD_INIT(mworker_cli_conf);
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100243
244/* These are strings to be reported in the output of "haproxy -vv". They may
245 * either be constants (in which case must_free must be zero) or dynamically
246 * allocated strings to pass to free() on exit, and in this case must_free
247 * must be non-zero.
248 */
249struct list build_opts_list = LIST_HEAD_INIT(build_opts_list);
250struct build_opts_str {
251 struct list list;
252 const char *str;
253 int must_free;
254};
255
Willy Tarreaue6945732016-12-21 19:57:00 +0100256/* These functions are called just after the point where the program exits
257 * after a config validity check, so they are generally suited for resource
258 * allocation and slow initializations that should be skipped during basic
259 * config checks. The functions must return 0 on success, or a combination
260 * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause
261 * and immediate exit, so the function must have emitted any useful error.
262 */
263struct list post_check_list = LIST_HEAD_INIT(post_check_list);
264struct post_check_fct {
265 struct list list;
266 int (*fct)();
267};
268
Willy Tarreau05554e62016-12-21 20:46:26 +0100269/* These functions are called when freeing the global sections at the end
270 * of deinit, after everything is stopped. They don't return anything, and
271 * they work in best effort mode as their sole goal is to make valgrind
272 * mostly happy.
273 */
274struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list);
275struct post_deinit_fct {
276 struct list list;
277 void (*fct)();
278};
279
Christopher Faulet415f6112017-07-25 16:52:58 +0200280/* These functions are called for each thread just after the thread creation
281 * and before running the scheduler. They should be used to do per-thread
282 * initializations. They must return 0 if an error occurred. */
283struct list per_thread_init_list = LIST_HEAD_INIT(per_thread_init_list);
284struct per_thread_init_fct {
285 struct list list;
286 int (*fct)();
287};
288
289/* These functions are called for each thread just after the scheduler loop and
290 * before exiting the thread. They don't return anything and, as for post-deinit
291 * functions, they work in best effort mode as their sole goal is to make
292 * valgrind mostly happy. */
293struct list per_thread_deinit_list = LIST_HEAD_INIT(per_thread_deinit_list);
294struct per_thread_deinit_fct {
295 struct list list;
296 void (*fct)();
297};
298
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299/*********************************************************************/
300/* general purpose functions ***************************************/
301/*********************************************************************/
302
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100303/* used to register some build option strings at boot. Set must_free to
304 * non-zero if the string must be freed upon exit.
305 */
306void hap_register_build_opts(const char *str, int must_free)
307{
308 struct build_opts_str *b;
309
310 b = calloc(1, sizeof(*b));
311 if (!b) {
312 fprintf(stderr, "out of memory\n");
313 exit(1);
314 }
315 b->str = str;
316 b->must_free = must_free;
317 LIST_ADDQ(&build_opts_list, &b->list);
318}
319
Willy Tarreaue6945732016-12-21 19:57:00 +0100320/* used to register some initialization functions to call after the checks. */
321void hap_register_post_check(int (*fct)())
322{
323 struct post_check_fct *b;
324
325 b = calloc(1, sizeof(*b));
326 if (!b) {
327 fprintf(stderr, "out of memory\n");
328 exit(1);
329 }
330 b->fct = fct;
331 LIST_ADDQ(&post_check_list, &b->list);
332}
333
Willy Tarreau05554e62016-12-21 20:46:26 +0100334/* used to register some de-initialization functions to call after everything
335 * has stopped.
336 */
337void hap_register_post_deinit(void (*fct)())
338{
339 struct post_deinit_fct *b;
340
341 b = calloc(1, sizeof(*b));
342 if (!b) {
343 fprintf(stderr, "out of memory\n");
344 exit(1);
345 }
346 b->fct = fct;
347 LIST_ADDQ(&post_deinit_list, &b->list);
348}
349
Christopher Faulet415f6112017-07-25 16:52:58 +0200350/* used to register some initialization functions to call for each thread. */
351void hap_register_per_thread_init(int (*fct)())
352{
353 struct per_thread_init_fct *b;
354
355 b = calloc(1, sizeof(*b));
356 if (!b) {
357 fprintf(stderr, "out of memory\n");
358 exit(1);
359 }
360 b->fct = fct;
361 LIST_ADDQ(&per_thread_init_list, &b->list);
362}
363
364/* used to register some de-initialization functions to call for each thread. */
365void hap_register_per_thread_deinit(void (*fct)())
366{
367 struct per_thread_deinit_fct *b;
368
369 b = calloc(1, sizeof(*b));
370 if (!b) {
371 fprintf(stderr, "out of memory\n");
372 exit(1);
373 }
374 b->fct = fct;
375 LIST_ADDQ(&per_thread_deinit_list, &b->list);
376}
377
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100378static void display_version()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200379{
Willy Tarreau909b9d82019-01-04 18:20:32 +0100380 printf("HA-Proxy version %s %s - https://haproxy.org/\n", haproxy_version, haproxy_date);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381}
382
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100383static void display_build_opts()
Willy Tarreau7b066db2007-12-02 11:28:59 +0100384{
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100385 struct build_opts_str *item;
386
Willy Tarreau7b066db2007-12-02 11:28:59 +0100387 printf("Build options :"
388#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100389 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100390#endif
391#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100392 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100393#endif
394#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100395 "\n CC = " BUILD_CC
396#endif
397#ifdef BUILD_CFLAGS
398 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100399#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100400#ifdef BUILD_OPTIONS
401 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100402#endif
Willy Tarreau7728ed32019-03-27 13:20:08 +0100403#ifdef BUILD_FEATURES
404 "\n\nFeature list : " BUILD_FEATURES
405#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200406 "\n\nDefault settings :"
Willy Tarreauca783d42019-03-13 10:03:07 +0100407 "\n bufsize = %d, maxrewrite = %d, maxpollevents = %d"
Willy Tarreau27a674e2009-08-17 07:23:33 +0200408 "\n\n",
Willy Tarreauca783d42019-03-13 10:03:07 +0100409 BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS);
Willy Tarreaube5b6852009-10-03 18:57:08 +0200410
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100411 list_for_each_entry(item, &build_opts_list, list) {
412 puts(item->str);
413 }
414
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100415 putchar('\n');
416
Willy Tarreaube5b6852009-10-03 18:57:08 +0200417 list_pollers(stdout);
418 putchar('\n');
Christopher Faulet98d9fe22018-04-10 14:37:32 +0200419 list_mux_proto(stdout);
420 putchar('\n');
Willy Tarreau679bba12019-03-19 08:08:10 +0100421 list_services(stdout);
422 putchar('\n');
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100423 list_filters(stdout);
424 putchar('\n');
Willy Tarreau7b066db2007-12-02 11:28:59 +0100425}
426
Willy Tarreaubaaee002006-06-26 02:48:02 +0200427/*
428 * This function prints the command line usage and exits
429 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100430static void usage(char *name)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200431{
432 display_version();
433 fprintf(stderr,
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200434 "Usage : %s [-f <cfgfile|cfgdir>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
Willy Tarreaua088d312015-10-08 11:58:48 +0200436 " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100437 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200438 " -d enters debug mode ; -db only disables background mode.\n"
Willy Tarreau6e064432012-05-08 15:40:42 +0200439 " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200441 " -D goes daemon ; -C changes to <dir> before loading files.\n"
William Lallemand095ba4c2017-06-01 17:38:50 +0200442 " -W master-worker mode.\n"
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100443#if defined(USE_SYSTEMD)
444 " -Ws master-worker mode with systemd notify support.\n"
445#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200446 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200447 " -c check mode : only check config files and exit\n"
Willy Tarreauca783d42019-03-13 10:03:07 +0100448 " -n sets the maximum total # of connections (uses ulimit -n)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449 " -m limits the usable amount of memory (in MB)\n"
450 " -N sets the default, per-proxy maximum # of connections (%d)\n"
Emeric Brun2b920a12010-09-23 18:30:22 +0200451 " -L set local peer name (default to hostname)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200452 " -p writes pids of all children to this file\n"
453#if defined(ENABLE_EPOLL)
454 " -de disables epoll() usage even when available\n"
455#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200456#if defined(ENABLE_KQUEUE)
457 " -dk disables kqueue() usage even when available\n"
458#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200459#if defined(ENABLE_POLL)
460 " -dp disables poll() usage even when available\n"
461#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200462#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100463 " -dS disables splice usage (broken on old kernels)\n"
464#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200465#if defined(USE_GETADDRINFO)
466 " -dG disables getaddrinfo() usage\n"
467#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000468#if defined(SO_REUSEPORT)
469 " -dR disables SO_REUSEPORT usage\n"
470#endif
Willy Tarreau3eed10e2016-11-07 21:03:16 +0100471 " -dr ignores server address resolution failures\n"
Emeric Brun850efd52014-01-29 12:24:34 +0100472 " -dV disables SSL verify on servers side\n"
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +0200473 " -sf/-st [pid ]* finishes/terminates old pids.\n"
Olivier Houchardf73629d2017-04-05 22:33:04 +0200474 " -x <unix_socket> get listening sockets from a unix socket\n"
William Lallemande7361152018-10-26 14:47:36 +0200475 " -S <unix_socket>[,<bind options>...] new stats socket for the master\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476 "\n",
Willy Tarreauca783d42019-03-13 10:03:07 +0100477 name, cfg_maxpconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200478 exit(1);
479}
480
481
482
483/*********************************************************************/
484/* more specific functions ***************************************/
485/*********************************************************************/
486
William Lallemand73b85e72017-06-01 17:38:51 +0200487/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
488 * pids the signal was correctly delivered to.
489 */
490static int tell_old_pids(int sig)
491{
492 int p;
493 int ret = 0;
494 for (p = 0; p < nb_oldpids; p++)
495 if (kill(oldpids[p], sig) == 0)
496 ret++;
497 return ret;
498}
499
500/* return 1 if a pid is a current child otherwise 0 */
501
502int current_child(int pid)
503{
504 int i;
505
506 for (i = 0; i < global.nbproc; i++) {
507 if (children[i] == pid)
508 return 1;
509 }
510 return 0;
511}
512
William Lallemand73b85e72017-06-01 17:38:51 +0200513static void mworker_block_signals()
514{
515 sigset_t set;
516
517 sigemptyset(&set);
518 sigaddset(&set, SIGUSR1);
519 sigaddset(&set, SIGUSR2);
520 sigaddset(&set, SIGHUP);
William Lallemandebf304f2018-09-11 10:06:21 +0200521 sigaddset(&set, SIGCHLD);
William Lallemand6e1796e2018-06-07 11:23:40 +0200522 ha_sigmask(SIG_SETMASK, &set, NULL);
William Lallemand73b85e72017-06-01 17:38:51 +0200523}
524
525static void mworker_unblock_signals()
526{
William Lallemandd3801c12018-09-11 10:06:23 +0200527 haproxy_unblock_signals();
William Lallemand73b85e72017-06-01 17:38:51 +0200528}
529
William Lallemand73b85e72017-06-01 17:38:51 +0200530/*
531 * Send signal to every known children.
532 */
533
534static void mworker_kill(int sig)
535{
536 int i;
537
538 tell_old_pids(sig);
539 if (children) {
540 for (i = 0; i < global.nbproc; i++)
541 kill(children[i], sig);
542 }
William Lallemandbc193052018-09-11 10:06:26 +0200543}
544
545/*
546 * serialize the proc list and put it in the environment
547 */
548static void mworker_proc_list_to_env()
549{
550 char *msg = NULL;
551 struct mworker_proc *child;
552
553 list_for_each_entry(child, &proc_list, list) {
William Lallemand6e0d8ae2018-12-14 19:31:21 +0100554 if (child->pid > -1)
555 memprintf(&msg, "%s|type=%c;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d", msg ? msg : "", child->type, child->ipc_fd[0], child->pid, child->relative_pid, child->reloads, child->timestamp);
William Lallemandbc193052018-09-11 10:06:26 +0200556 }
557 if (msg)
William Lallemand16dd1b32018-11-19 18:46:18 +0100558 setenv("HAPROXY_PROCESSES", msg, 1);
William Lallemandbc193052018-09-11 10:06:26 +0200559}
560
561/*
562 * unserialize the proc list from the environment
563 */
564static void mworker_env_to_proc_list()
565{
566 char *msg, *token = NULL, *s1;
567
William Lallemand16dd1b32018-11-19 18:46:18 +0100568 msg = getenv("HAPROXY_PROCESSES");
William Lallemandbc193052018-09-11 10:06:26 +0200569 if (!msg)
570 return;
571
572 while ((token = strtok_r(msg, "|", &s1))) {
573 struct mworker_proc *child;
574 char *subtoken = NULL;
575 char *s2;
576
577 msg = NULL;
578
579 child = calloc(1, sizeof(*child));
580
581 while ((subtoken = strtok_r(token, ";", &s2))) {
582
583 token = NULL;
584
William Lallemand16dd1b32018-11-19 18:46:18 +0100585 if (strncmp(subtoken, "type=", 5) == 0) {
586 child->type = *(subtoken+5);
587 if (child->type == 'm') /* we are in the master, assign it */
588 proc_self = child;
589 } else if (strncmp(subtoken, "fd=", 3) == 0) {
William Lallemandbc193052018-09-11 10:06:26 +0200590 child->ipc_fd[0] = atoi(subtoken+3);
591 } else if (strncmp(subtoken, "pid=", 4) == 0) {
592 child->pid = atoi(subtoken+4);
593 } else if (strncmp(subtoken, "rpid=", 5) == 0) {
594 child->relative_pid = atoi(subtoken+5);
William Lallemandf1a62862018-10-26 14:47:29 +0200595 } else if (strncmp(subtoken, "reloads=", 8) == 0) {
596 /* we reloaded this process once more */
597 child->reloads = atoi(subtoken+8) + 1;
William Lallemande3683302018-11-19 18:46:17 +0100598 } else if (strncmp(subtoken, "timestamp=", 10) == 0) {
599 child->timestamp = atoi(subtoken+10);
William Lallemandbc193052018-09-11 10:06:26 +0200600 }
601 }
602 if (child->pid)
603 LIST_ADDQ(&proc_list, &child->list);
604 else
605 free(child);
606 }
607
William Lallemand16dd1b32018-11-19 18:46:18 +0100608 unsetenv("HAPROXY_PROCESSES");
William Lallemand73b85e72017-06-01 17:38:51 +0200609}
610
Willy Tarreaubaaee002006-06-26 02:48:02 +0200611/*
William Lallemand75ea0a02017-11-15 19:02:58 +0100612 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
613 * fd, and the FD provided by fd@
614 */
615static void mworker_cleanlisteners()
616{
617 struct listener *l, *l_next;
618 struct proxy *curproxy;
Willy Tarreau473cf5d2017-12-05 11:14:12 +0100619 struct peers *curpeers;
William Lallemand75ea0a02017-11-15 19:02:58 +0100620
William Lallemand3da97692018-09-11 10:06:19 +0200621 /* we might have to unbind some peers sections from some processes */
622 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
623 if (!curpeers->peers_fe)
624 continue;
Willy Tarreau473cf5d2017-12-05 11:14:12 +0100625
William Lallemand3da97692018-09-11 10:06:19 +0200626 stop_proxy(curpeers->peers_fe);
627 /* disable this peer section so that it kills itself */
628 signal_unregister_handler(curpeers->sighandler);
629 task_delete(curpeers->sync_task);
630 task_free(curpeers->sync_task);
631 curpeers->sync_task = NULL;
632 task_free(curpeers->peers_fe->task);
633 curpeers->peers_fe->task = NULL;
634 curpeers->peers_fe = NULL;
635 }
William Lallemand75ea0a02017-11-15 19:02:58 +0100636
William Lallemand91c13b62018-09-11 10:06:20 +0200637 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
William Lallemand2fd45fa2018-12-03 20:34:44 +0100638 int listen_in_master = 0;
639
William Lallemand75ea0a02017-11-15 19:02:58 +0100640 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
William Lallemande22f11f2018-09-11 10:06:27 +0200641 /* remove the listener, but not those we need in the master... */
William Lallemanddd319a52018-10-12 10:39:54 +0200642 if (!(l->options & LI_O_MWORKER)) {
643 /* unbind the listener but does not close if
644 the FD is inherited with fd@ from the parent
645 process */
646 if (l->options & LI_O_INHERITED)
647 unbind_listener_no_close(l);
648 else
649 unbind_listener(l);
William Lallemande22f11f2018-09-11 10:06:27 +0200650 delete_listener(l);
William Lallemand2fd45fa2018-12-03 20:34:44 +0100651 } else {
652 listen_in_master = 1;
William Lallemanddd319a52018-10-12 10:39:54 +0200653 }
William Lallemand75ea0a02017-11-15 19:02:58 +0100654 }
William Lallemand2fd45fa2018-12-03 20:34:44 +0100655 /* if the proxy shouldn't be in the master, we stop it */
656 if (!listen_in_master)
657 curproxy->state = PR_STSTOPPED;
William Lallemand75ea0a02017-11-15 19:02:58 +0100658 }
659}
660
661
662/*
William Lallemand73b85e72017-06-01 17:38:51 +0200663 * remove a pid forom the olpid array and decrease nb_oldpids
664 * return 1 pid was found otherwise return 0
665 */
666
667int delete_oldpid(int pid)
668{
669 int i;
670
671 for (i = 0; i < nb_oldpids; i++) {
672 if (oldpids[i] == pid) {
673 oldpids[i] = oldpids[nb_oldpids - 1];
674 oldpids[nb_oldpids - 1] = 0;
675 nb_oldpids--;
676 return 1;
677 }
678 }
679 return 0;
680}
681
William Lallemand85b0bd92017-06-01 17:38:53 +0200682
683static void get_cur_unixsocket()
684{
685 /* if -x was used, try to update the stat socket if not available anymore */
686 if (global.stats_fe) {
687 struct bind_conf *bind_conf;
688
689 /* pass through all stats socket */
690 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
691 struct listener *l;
692
693 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
694
695 if (l->addr.ss_family == AF_UNIX &&
696 (bind_conf->level & ACCESS_FD_LISTENERS)) {
697 const struct sockaddr_un *un;
698
699 un = (struct sockaddr_un *)&l->addr;
700 /* priority to old_unixsocket */
701 if (!cur_unixsocket) {
702 cur_unixsocket = strdup(un->sun_path);
703 } else {
704 if (old_unixsocket && !strcmp(un->sun_path, old_unixsocket)) {
705 free(cur_unixsocket);
706 cur_unixsocket = strdup(old_unixsocket);
707 return;
708 }
709 }
710 }
711 }
712 }
713 }
714 if (!cur_unixsocket && old_unixsocket)
715 cur_unixsocket = strdup(old_unixsocket);
716}
717
William Lallemand73b85e72017-06-01 17:38:51 +0200718/*
719 * When called, this function reexec haproxy with -sf followed by current
Joseph Herlant03420902018-11-15 10:41:50 -0800720 * children PIDs and possibly old children PIDs if they didn't leave yet.
William Lallemand73b85e72017-06-01 17:38:51 +0200721 */
William Lallemanda57b7e32018-12-14 21:11:31 +0100722void mworker_reload()
William Lallemand73b85e72017-06-01 17:38:51 +0200723{
724 int next_argc = 0;
725 int j;
726 char *msg = NULL;
Willy Tarreau8dca1952019-03-01 10:21:55 +0100727 struct rlimit limit;
William Lallemand7c756a82018-11-26 11:53:40 +0100728 struct per_thread_deinit_fct *ptdf;
William Lallemand73b85e72017-06-01 17:38:51 +0200729
730 mworker_block_signals();
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100731#if defined(USE_SYSTEMD)
732 if (global.tune.options & GTUNE_USE_SYSTEMD)
733 sd_notify(0, "RELOADING=1");
734#endif
William Lallemand73b85e72017-06-01 17:38:51 +0200735 setenv("HAPROXY_MWORKER_REEXEC", "1", 1);
736
William Lallemandbc193052018-09-11 10:06:26 +0200737 mworker_proc_list_to_env(); /* put the children description in the env */
738
William Lallemand7c756a82018-11-26 11:53:40 +0100739 /* during the reload we must ensure that every FDs that can't be
740 * reuse (ie those that are not referenced in the proc_list)
741 * are closed or they will leak. */
742
743 /* close the listeners FD */
744 mworker_cli_proxy_stop();
745 /* close the poller FD and the thread waker pipe FD */
746 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
747 ptdf->fct();
748 if (fdtab)
749 deinit_pollers();
750
Willy Tarreau8dca1952019-03-01 10:21:55 +0100751 /* restore the initial FD limits */
752 limit.rlim_cur = rlim_fd_cur_at_boot;
753 limit.rlim_max = rlim_fd_max_at_boot;
754 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
755 getrlimit(RLIMIT_NOFILE, &limit);
756 ha_warning("Failed to restore initial FD limits (cur=%u max=%u), using cur=%u max=%u\n",
757 rlim_fd_cur_at_boot, rlim_fd_max_at_boot,
758 (unsigned int)limit.rlim_cur, (unsigned int)limit.rlim_max);
759 }
760
William Lallemand73b85e72017-06-01 17:38:51 +0200761 /* compute length */
762 while (next_argv[next_argc])
763 next_argc++;
764
William Lallemand85b0bd92017-06-01 17:38:53 +0200765 /* 1 for haproxy -sf, 2 for -x /socket */
766 next_argv = realloc(next_argv, (next_argc + 1 + 2 + global.nbproc + nb_oldpids + 1) * sizeof(char *));
William Lallemand73b85e72017-06-01 17:38:51 +0200767 if (next_argv == NULL)
768 goto alloc_error;
769
770
771 /* add -sf <PID>* to argv */
772 if (children || nb_oldpids > 0)
773 next_argv[next_argc++] = "-sf";
774 if (children) {
775 for (j = 0; j < global.nbproc; next_argc++,j++) {
776 next_argv[next_argc] = memprintf(&msg, "%d", children[j]);
777 if (next_argv[next_argc] == NULL)
778 goto alloc_error;
779 msg = NULL;
780 }
781 }
782 /* copy old process PIDs */
783 for (j = 0; j < nb_oldpids; next_argc++,j++) {
784 next_argv[next_argc] = memprintf(&msg, "%d", oldpids[j]);
785 if (next_argv[next_argc] == NULL)
786 goto alloc_error;
787 msg = NULL;
788 }
789 next_argv[next_argc] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200790
William Lallemand2bf6d622017-06-20 11:20:23 +0200791 /* add the -x option with the stat socket */
William Lallemand85b0bd92017-06-01 17:38:53 +0200792 if (cur_unixsocket) {
793
William Lallemand2bf6d622017-06-20 11:20:23 +0200794 next_argv[next_argc++] = "-x";
795 next_argv[next_argc++] = (char *)cur_unixsocket;
796 next_argv[next_argc++] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200797 }
798
Christopher Faulet767a84b2017-11-24 16:50:31 +0100799 ha_warning("Reexecuting Master process\n");
Tim Duesterhus0436ab72017-11-12 17:39:18 +0100800 execvp(next_argv[0], next_argv);
William Lallemand73b85e72017-06-01 17:38:51 +0200801
Christopher Faulet767a84b2017-11-24 16:50:31 +0100802 ha_warning("Failed to reexecute the master process [%d]: %s\n", pid, strerror(errno));
William Lallemand722d4ca2017-11-15 19:02:55 +0100803 return;
804
William Lallemand73b85e72017-06-01 17:38:51 +0200805alloc_error:
Joseph Herlant07a08342018-11-15 10:43:05 -0800806 ha_warning("Failed to reexecute the master process [%d]: Cannot allocate memory\n", pid);
William Lallemand73b85e72017-06-01 17:38:51 +0200807 return;
808}
809
William Lallemand73b85e72017-06-01 17:38:51 +0200810/*
William Lallemandb3f2be32018-09-11 10:06:18 +0200811 * When called, this function reexec haproxy with -sf followed by current
Joseph Herlant03420902018-11-15 10:41:50 -0800812 * children PIDs and possibly old children PIDs if they didn't leave yet.
William Lallemand73b85e72017-06-01 17:38:51 +0200813 */
William Lallemandb3f2be32018-09-11 10:06:18 +0200814static void mworker_catch_sighup(struct sig_handler *sh)
William Lallemand73b85e72017-06-01 17:38:51 +0200815{
William Lallemandb3f2be32018-09-11 10:06:18 +0200816 mworker_reload();
817}
William Lallemand73b85e72017-06-01 17:38:51 +0200818
William Lallemandb3f2be32018-09-11 10:06:18 +0200819static void mworker_catch_sigterm(struct sig_handler *sh)
820{
821 int sig = sh->arg;
William Lallemand2f8b31c2017-11-15 19:02:56 +0100822
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100823#if defined(USE_SYSTEMD)
William Lallemandb3f2be32018-09-11 10:06:18 +0200824 if (global.tune.options & GTUNE_USE_SYSTEMD) {
825 sd_notify(0, "STOPPING=1");
826 }
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100827#endif
William Lallemandb3f2be32018-09-11 10:06:18 +0200828 ha_warning("Exiting Master process...\n");
829 mworker_kill(sig);
830}
William Lallemand73b85e72017-06-01 17:38:51 +0200831
William Lallemandb3f2be32018-09-11 10:06:18 +0200832/*
833 * Wait for every children to exit
834 */
William Lallemand73b85e72017-06-01 17:38:51 +0200835
William Lallemandb3f2be32018-09-11 10:06:18 +0200836static void mworker_catch_sigchld(struct sig_handler *sh)
837{
838 int exitpid = -1;
839 int status = 0;
William Lallemandbc193052018-09-11 10:06:26 +0200840 struct mworker_proc *child, *it;
William Lallemandf94afeb2019-03-27 14:19:10 +0100841 int childfound;
William Lallemand73b85e72017-06-01 17:38:51 +0200842
William Lallemandb3f2be32018-09-11 10:06:18 +0200843restart_wait:
William Lallemand73b85e72017-06-01 17:38:51 +0200844
William Lallemandf94afeb2019-03-27 14:19:10 +0100845 childfound = 0;
846
William Lallemandb3f2be32018-09-11 10:06:18 +0200847 exitpid = waitpid(-1, &status, WNOHANG);
848 if (exitpid > 0) {
William Lallemand73b85e72017-06-01 17:38:51 +0200849 if (WIFEXITED(status))
850 status = WEXITSTATUS(status);
851 else if (WIFSIGNALED(status))
852 status = 128 + WTERMSIG(status);
853 else if (WIFSTOPPED(status))
854 status = 128 + WSTOPSIG(status);
855 else
856 status = 255;
857
William Lallemandbc193052018-09-11 10:06:26 +0200858 list_for_each_entry_safe(child, it, &proc_list, list) {
859 if (child->pid != exitpid)
860 continue;
861
862 LIST_DEL(&child->list);
863 close(child->ipc_fd[0]);
William Lallemandf94afeb2019-03-27 14:19:10 +0100864 childfound = 1;
William Lallemandbc193052018-09-11 10:06:26 +0200865 break;
866 }
867
William Lallemandf94afeb2019-03-27 14:19:10 +0100868 if (!children || !childfound) {
William Lallemand18e52a82018-11-06 17:37:13 +0100869 ha_warning("Worker %d exited with code %d (%s)\n", exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
William Lallemand73b85e72017-06-01 17:38:51 +0200870 } else {
871 /* check if exited child was in the current children list */
872 if (current_child(exitpid)) {
William Lallemand18e52a82018-11-06 17:37:13 +0100873 ha_alert("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
William Lallemand69f9b3b2017-06-01 17:38:54 +0200874 if (status != 0 && status != 130 && status != 143
William Lallemand4cfede82017-11-24 22:02:34 +0100875 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100876 ha_alert("exit-on-failure: killing every workers with SIGTERM\n");
William Lallemand91723742018-11-06 17:37:14 +0100877 if (exitcode < 0)
878 exitcode = status;
William Lallemand69f9b3b2017-06-01 17:38:54 +0200879 mworker_kill(SIGTERM);
880 }
William Lallemand73b85e72017-06-01 17:38:51 +0200881 } else {
William Lallemand18e52a82018-11-06 17:37:13 +0100882 ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
William Lallemand73b85e72017-06-01 17:38:51 +0200883 delete_oldpid(exitpid);
884 }
William Lallemandf94afeb2019-03-27 14:19:10 +0100885 free(child);
William Lallemand73b85e72017-06-01 17:38:51 +0200886 }
William Lallemand18e52a82018-11-06 17:37:13 +0100887
William Lallemandb3f2be32018-09-11 10:06:18 +0200888 /* do it again to check if it was the last worker */
889 goto restart_wait;
William Lallemand73b85e72017-06-01 17:38:51 +0200890 }
William Lallemandb3f2be32018-09-11 10:06:18 +0200891 /* Better rely on the system than on a list of process to check if it was the last one */
892 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand18e52a82018-11-06 17:37:13 +0100893 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : status);
William Lallemandb3f2be32018-09-11 10:06:18 +0200894 atexit_flag = 0;
William Lallemand91723742018-11-06 17:37:14 +0100895 if (exitcode > 0)
896 exit(exitcode);
William Lallemandb3f2be32018-09-11 10:06:18 +0200897 exit(status); /* parent must leave using the latest status code known */
898 }
899
William Lallemand73b85e72017-06-01 17:38:51 +0200900}
901
William Lallemandb3f2be32018-09-11 10:06:18 +0200902static void mworker_loop()
903{
904
905#if defined(USE_SYSTEMD)
906 if (global.tune.options & GTUNE_USE_SYSTEMD)
907 sd_notifyf(0, "READY=1\nMAINPID=%lu", (unsigned long)getpid());
908#endif
909
William Lallemandbc193052018-09-11 10:06:26 +0200910 master = 1;
911
William Lallemand0564d412018-11-20 17:36:53 +0100912 signal_unregister(SIGUSR1);
913 signal_unregister(SIGHUP);
914 signal_unregister(SIGQUIT);
915
William Lallemandb3f2be32018-09-11 10:06:18 +0200916 signal_register_fct(SIGTERM, mworker_catch_sigterm, SIGTERM);
917 signal_register_fct(SIGUSR1, mworker_catch_sigterm, SIGUSR1);
918 signal_register_fct(SIGINT, mworker_catch_sigterm, SIGINT);
919 signal_register_fct(SIGHUP, mworker_catch_sighup, SIGHUP);
920 signal_register_fct(SIGUSR2, mworker_catch_sighup, SIGUSR2);
921 signal_register_fct(SIGCHLD, mworker_catch_sigchld, SIGCHLD);
922
923 mworker_unblock_signals();
924 mworker_cleanlisteners();
William Lallemand27f3fa52018-12-06 14:05:20 +0100925 mworker_cleantasks();
William Lallemandb3f2be32018-09-11 10:06:18 +0200926
William Lallemandbc193052018-09-11 10:06:26 +0200927 mworker_catch_sigchld(NULL); /* ensure we clean the children in case
928 some SIGCHLD were lost */
929
William Lallemandb3f2be32018-09-11 10:06:18 +0200930 global.nbthread = 1;
931 relative_pid = 1;
932 pid_bit = 1;
Willy Tarreaua38a7172019-02-02 17:11:28 +0100933 all_proc_mask = 1;
William Lallemandb3f2be32018-09-11 10:06:18 +0200934
William Lallemand2672eb92018-12-14 15:52:39 +0100935#ifdef USE_THREAD
936 tid_bit = 1;
937 all_threads_mask = 1;
938#endif
939
William Lallemandb3f2be32018-09-11 10:06:18 +0200940 jobs++; /* this is the "master" job, we want to take care of the
941 signals even if there is no listener so the poll loop don't
942 leave */
943
944 fork_poller();
945 run_thread_poll_loop((int []){0});
946}
William Lallemandcb11fd22017-06-01 17:38:52 +0200947
948/*
949 * Reexec the process in failure mode, instead of exiting
950 */
951void reexec_on_failure()
952{
953 if (!atexit_flag)
954 return;
955
956 setenv("HAPROXY_MWORKER_WAIT_ONLY", "1", 1);
957
Christopher Faulet767a84b2017-11-24 16:50:31 +0100958 ha_warning("Reexecuting Master process in waitpid mode\n");
William Lallemandcb11fd22017-06-01 17:38:52 +0200959 mworker_reload();
William Lallemandcb11fd22017-06-01 17:38:52 +0200960}
William Lallemand73b85e72017-06-01 17:38:51 +0200961
962
963/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200964 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
965 * a signal zero to all subscribers. This means that it's as easy as
966 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200967 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100968static void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200969{
970 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200971 signal_unregister_handler(sh);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100972 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200973}
974
975/*
976 * upon SIGTTOU, we pause everything
977 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100978static void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200979{
980 pause_proxies();
Willy Tarreaubafbe012017-11-24 17:34:44 +0100981 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200982}
983
984/*
985 * upon SIGTTIN, let's have a soft stop.
986 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100987static void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200988{
Willy Tarreaube58c382011-07-24 18:28:10 +0200989 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200990}
991
992/*
993 * this function dumps every server's state when the process receives SIGHUP.
994 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100995static void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200996{
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100997 struct proxy *p = proxies_list;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200998
Christopher Faulet767a84b2017-11-24 16:50:31 +0100999 ha_warning("SIGHUP received, dumping servers states.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001000 while (p) {
1001 struct server *s = p->srv;
1002
1003 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
1004 while (s) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001005 chunk_printf(&trash,
1006 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
1007 p->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +02001008 (s->cur_state != SRV_ST_STOPPED) ? "UP" : "DOWN",
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001009 s->cur_sess, s->nbpend, s->counters.cum_sess);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001010 ha_warning("%s\n", trash.area);
1011 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001012 s = s->next;
1013 }
1014
Willy Tarreau5fcc8f12007-09-17 11:27:09 +02001015 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
1016 if (!p->srv) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001017 chunk_printf(&trash,
1018 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
1019 p->id,
1020 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 +02001021 } else if (p->srv_act == 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001022 chunk_printf(&trash,
1023 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
1024 p->id,
1025 (p->srv_bck) ? "is running on backup servers" : "has no server available",
1026 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 +02001027 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001028 chunk_printf(&trash,
1029 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
1030 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
1031 p->id, p->srv_act, p->srv_bck,
1032 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 +02001033 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001034 ha_warning("%s\n", trash.area);
1035 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001036
1037 p = p->next;
1038 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001039}
1040
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001041static void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001042{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001043 /* dump memory usage then free everything possible */
1044 dump_pools();
Willy Tarreaubafbe012017-11-24 17:34:44 +01001045 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001046}
1047
William Lallemande1340412017-12-28 16:09:36 +01001048/*
1049 * This function dup2 the stdio FDs (0,1,2) with <fd>, then closes <fd>
1050 * If <fd> < 0, it opens /dev/null and use it to dup
1051 *
1052 * In the case of chrooting, you have to open /dev/null before the chroot, and
1053 * pass the <fd> to this function
1054 */
1055static void stdio_quiet(int fd)
1056{
1057 if (fd < 0)
1058 fd = open("/dev/null", O_RDWR, 0);
1059
1060 if (fd > -1) {
1061 fclose(stdin);
1062 fclose(stdout);
1063 fclose(stderr);
1064
1065 dup2(fd, 0);
1066 dup2(fd, 1);
1067 dup2(fd, 2);
1068 if (fd > 2)
1069 close(fd);
1070 return;
1071 }
1072
1073 ha_alert("Cannot open /dev/null\n");
1074 exit(EXIT_FAILURE);
1075}
1076
1077
Joseph Herlant03420902018-11-15 10:41:50 -08001078/* This function checks if cfg_cfgfiles contains directories.
1079 * If it finds one, it adds all the files (and only files) it contains
1080 * in cfg_cfgfiles in place of the directory (and removes the directory).
1081 * It adds the files in lexical order.
1082 * It adds only files with .cfg extension.
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001083 * It doesn't add files with name starting with '.'
1084 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001085static void cfgfiles_expand_directories(void)
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001086{
1087 struct wordlist *wl, *wlb;
1088 char *err = NULL;
1089
1090 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
1091 struct stat file_stat;
1092 struct dirent **dir_entries = NULL;
1093 int dir_entries_nb;
1094 int dir_entries_it;
1095
1096 if (stat(wl->s, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001097 ha_alert("Cannot open configuration file/directory %s : %s\n",
1098 wl->s,
1099 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001100 exit(1);
1101 }
1102
1103 if (!S_ISDIR(file_stat.st_mode))
1104 continue;
1105
1106 /* from this point wl->s is a directory */
1107
1108 dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort);
1109 if (dir_entries_nb < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001110 ha_alert("Cannot open configuration directory %s : %s\n",
1111 wl->s,
1112 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001113 exit(1);
1114 }
1115
1116 /* for each element in the directory wl->s */
1117 for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) {
1118 struct dirent *dir_entry = dir_entries[dir_entries_it];
1119 char *filename = NULL;
1120 char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg");
1121
1122 /* don't add filename that begin with .
Joseph Herlant03420902018-11-15 10:41:50 -08001123 * only add filename with .cfg extension
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001124 */
1125 if (dir_entry->d_name[0] == '.' ||
1126 !(d_name_cfgext && d_name_cfgext[4] == '\0'))
1127 goto next_dir_entry;
1128
1129 if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001130 ha_alert("Cannot load configuration files %s : out of memory.\n",
1131 filename);
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001132 exit(1);
1133 }
1134
1135 if (stat(filename, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001136 ha_alert("Cannot open configuration file %s : %s\n",
1137 wl->s,
1138 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001139 exit(1);
1140 }
1141
1142 /* don't add anything else than regular file in cfg_cfgfiles
1143 * this way we avoid loops
1144 */
1145 if (!S_ISREG(file_stat.st_mode))
1146 goto next_dir_entry;
1147
1148 if (!list_append_word(&wl->list, filename, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001149 ha_alert("Cannot load configuration files %s : %s\n",
1150 filename,
1151 err);
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001152 exit(1);
1153 }
1154
1155next_dir_entry:
1156 free(filename);
1157 free(dir_entry);
1158 }
1159
1160 free(dir_entries);
1161
1162 /* remove the current directory (wl) from cfg_cfgfiles */
1163 free(wl->s);
1164 LIST_DEL(&wl->list);
1165 free(wl);
1166 }
1167
1168 free(err);
1169}
1170
Olivier Houchardf73629d2017-04-05 22:33:04 +02001171static int get_old_sockets(const char *unixsocket)
1172{
1173 char *cmsgbuf = NULL, *tmpbuf = NULL;
1174 int *tmpfd = NULL;
1175 struct sockaddr_un addr;
1176 struct cmsghdr *cmsg;
1177 struct msghdr msghdr;
1178 struct iovec iov;
1179 struct xfer_sock_list *xfer_sock = NULL;
Olivier Houchard54740872017-04-06 14:45:14 +02001180 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf73629d2017-04-05 22:33:04 +02001181 int sock = -1;
1182 int ret = -1;
1183 int ret2 = -1;
1184 int fd_nb;
1185 int got_fd = 0;
1186 int i = 0;
1187 size_t maxoff = 0, curoff = 0;
1188
1189 memset(&msghdr, 0, sizeof(msghdr));
1190 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
1191 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001192 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001193 goto out;
1194 }
1195 sock = socket(PF_UNIX, SOCK_STREAM, 0);
1196 if (sock < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001197 ha_warning("Failed to connect to the old process socket '%s'\n",
1198 unixsocket);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001199 goto out;
1200 }
1201 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path));
1202 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
1203 addr.sun_family = PF_UNIX;
1204 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
1205 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001206 ha_warning("Failed to connect to the old process socket '%s'\n",
1207 unixsocket);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001208 goto out;
1209 }
Olivier Houchard54740872017-04-06 14:45:14 +02001210 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001211 iov.iov_base = &fd_nb;
1212 iov.iov_len = sizeof(fd_nb);
1213 msghdr.msg_iov = &iov;
1214 msghdr.msg_iovlen = 1;
1215 send(sock, "_getsocks\n", strlen("_getsocks\n"), 0);
1216 /* First, get the number of file descriptors to be received */
1217 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001218 ha_warning("Failed to get the number of sockets to be transferred !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001219 goto out;
1220 }
1221 if (fd_nb == 0) {
1222 ret = 0;
1223 goto out;
1224 }
Olivier Houchardf143b802017-11-04 15:13:01 +01001225 tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001226 if (tmpbuf == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001227 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001228 goto out;
1229 }
1230 tmpfd = malloc(fd_nb * sizeof(int));
1231 if (tmpfd == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001232 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001233 goto out;
1234 }
1235 msghdr.msg_control = cmsgbuf;
1236 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
Olivier Houchardf143b802017-11-04 15:13:01 +01001237 iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001238 do {
1239 int ret3;
1240
1241 iov.iov_base = tmpbuf + curoff;
1242 ret = recvmsg(sock, &msghdr, 0);
1243 if (ret == -1 && errno == EINTR)
1244 continue;
1245 if (ret <= 0)
1246 break;
1247 /* Send an ack to let the sender know we got the sockets
1248 * and it can send some more
1249 */
1250 do {
1251 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
1252 } while (ret3 == -1 && errno == EINTR);
1253 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
1254 cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
1255 if (cmsg->cmsg_level == SOL_SOCKET &&
1256 cmsg->cmsg_type == SCM_RIGHTS) {
1257 size_t totlen = cmsg->cmsg_len -
1258 CMSG_LEN(0);
1259 if (totlen / sizeof(int) + got_fd > fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001260 ha_warning("Got to many sockets !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001261 goto out;
1262 }
1263 /*
1264 * Be paranoid and use memcpy() to avoid any
1265 * potential alignement issue.
1266 */
1267 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
1268 got_fd += totlen / sizeof(int);
1269 }
1270 }
1271 curoff += ret;
1272 } while (got_fd < fd_nb);
1273
1274 if (got_fd != fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001275 ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
1276 fd_nb, got_fd);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001277 goto out;
1278 }
1279 maxoff = curoff;
1280 curoff = 0;
1281 for (i = 0; i < got_fd; i++) {
1282 int fd = tmpfd[i];
1283 socklen_t socklen;
1284 int len;
1285
1286 xfer_sock = calloc(1, sizeof(*xfer_sock));
1287 if (!xfer_sock) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001288 ha_warning("Failed to allocate memory in get_old_sockets() !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001289 break;
1290 }
1291 xfer_sock->fd = -1;
1292
1293 socklen = sizeof(xfer_sock->addr);
1294 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001295 ha_warning("Failed to get socket address\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001296 free(xfer_sock);
Olivier Houchardbe7b1ce2017-07-17 17:25:33 +02001297 xfer_sock = NULL;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001298 continue;
1299 }
1300 if (curoff >= maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001301 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001302 goto out;
1303 }
1304 len = tmpbuf[curoff++];
1305 if (len > 0) {
1306 /* We have a namespace */
1307 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001308 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001309 goto out;
1310 }
1311 xfer_sock->namespace = malloc(len + 1);
1312 if (!xfer_sock->namespace) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001313 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001314 goto out;
1315 }
1316 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
1317 xfer_sock->namespace[len] = 0;
1318 curoff += len;
1319 }
1320 if (curoff >= maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001321 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001322 goto out;
1323 }
1324 len = tmpbuf[curoff++];
1325 if (len > 0) {
1326 /* We have an interface */
1327 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001328 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001329 goto out;
1330 }
1331 xfer_sock->iface = malloc(len + 1);
1332 if (!xfer_sock->iface) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001333 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001334 goto out;
1335 }
1336 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
Olivier Houchard33e083c2018-03-15 17:48:49 +01001337 xfer_sock->iface[len] = 0;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001338 curoff += len;
1339 }
1340 if (curoff + sizeof(int) > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001341 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001342 goto out;
1343 }
1344 memcpy(&xfer_sock->options, &tmpbuf[curoff],
1345 sizeof(xfer_sock->options));
1346 curoff += sizeof(xfer_sock->options);
1347
1348 xfer_sock->fd = fd;
1349 if (xfer_sock_list)
1350 xfer_sock_list->prev = xfer_sock;
1351 xfer_sock->next = xfer_sock_list;
1352 xfer_sock->prev = NULL;
1353 xfer_sock_list = xfer_sock;
1354 xfer_sock = NULL;
1355 }
1356
1357 ret2 = 0;
1358out:
1359 /* If we failed midway make sure to close the remaining
1360 * file descriptors
1361 */
1362 if (tmpfd != NULL && i < got_fd) {
1363 for (; i < got_fd; i++) {
1364 close(tmpfd[i]);
1365 }
1366 }
1367 free(tmpbuf);
1368 free(tmpfd);
1369 free(cmsgbuf);
1370 if (sock != -1)
1371 close(sock);
1372 if (xfer_sock) {
1373 free(xfer_sock->namespace);
1374 free(xfer_sock->iface);
1375 if (xfer_sock->fd != -1)
1376 close(xfer_sock->fd);
1377 free(xfer_sock);
1378 }
1379 return (ret2);
1380}
1381
Willy Tarreaubaaee002006-06-26 02:48:02 +02001382/*
William Lallemand73b85e72017-06-01 17:38:51 +02001383 * copy and cleanup the current argv
1384 * Remove the -sf /-st parameters
1385 * Return an allocated copy of argv
1386 */
1387
1388static char **copy_argv(int argc, char **argv)
1389{
1390 char **newargv;
William Lallemand2bf6d622017-06-20 11:20:23 +02001391 int i = 0, j = 0;
William Lallemand73b85e72017-06-01 17:38:51 +02001392
1393 newargv = calloc(argc + 2, sizeof(char *));
1394 if (newargv == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001395 ha_warning("Cannot allocate memory\n");
William Lallemand73b85e72017-06-01 17:38:51 +02001396 return NULL;
1397 }
1398
William Lallemand2bf6d622017-06-20 11:20:23 +02001399 while (i < argc) {
1400 /* -sf or -st or -x */
William Lallemand29f690c2018-01-09 23:12:27 +01001401 if (i > 0 && argv[i][0] == '-' &&
1402 ((argv[i][1] == 's' && (argv[i][2] == 'f' || argv[i][2] == 't')) || argv[i][1] == 'x' )) {
William Lallemand2bf6d622017-06-20 11:20:23 +02001403 /* list of pids to finish ('f') or terminate ('t') or unix socket (-x) */
William Lallemand73b85e72017-06-01 17:38:51 +02001404 i++;
1405 while (i < argc && argv[i][0] != '-') {
1406 i++;
1407 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001408 continue;
William Lallemand73b85e72017-06-01 17:38:51 +02001409 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001410
1411 newargv[j++] = argv[i++];
William Lallemand73b85e72017-06-01 17:38:51 +02001412 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001413
William Lallemand73b85e72017-06-01 17:38:51 +02001414 return newargv;
1415}
1416
Willy Tarreau5a023f02019-03-01 14:19:31 +01001417/* considers splicing proxies' maxconn, computes the ideal global.maxpipes
1418 * setting, and returns it. It may return -1 meaning "unlimited" if some
1419 * unlimited proxies have been found and the global.maxconn value is not yet
1420 * set. It may also return a value greater than maxconn if it's not yet set.
1421 * Note that a value of zero means there is no need for pipes. -1 is never
1422 * returned if global.maxconn is valid.
1423 */
1424static int compute_ideal_maxpipes()
1425{
1426 struct proxy *cur;
1427 int nbfe = 0, nbbe = 0;
1428 int unlimited = 0;
1429 int pipes;
1430 int max;
1431
1432 for (cur = proxies_list; cur; cur = cur->next) {
1433 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
1434 if (cur->cap & PR_CAP_FE) {
1435 max = cur->maxconn;
1436 nbfe += max;
1437 if (!max) {
1438 unlimited = 1;
1439 break;
1440 }
1441 }
1442 if (cur->cap & PR_CAP_BE) {
1443 max = cur->fullconn ? cur->fullconn : global.maxconn;
1444 nbbe += max;
1445 if (!max) {
1446 unlimited = 1;
1447 break;
1448 }
1449 }
1450 }
1451 }
1452
1453 pipes = MAX(nbfe, nbbe);
1454 if (global.maxconn) {
1455 if (pipes > global.maxconn || unlimited)
1456 pipes = global.maxconn;
1457 } else if (unlimited) {
1458 pipes = -1;
1459 }
1460
1461 return pipes >= 4 ? pipes / 4 : pipes;
1462}
1463
Willy Tarreauac350932019-03-01 15:43:14 +01001464/* considers global.maxsocks, global.maxpipes, async engines, SSL frontends and
1465 * rlimits and computes an ideal maxconn. It's meant to be called only when
1466 * maxsock contains the sum of listening FDs, before it is updated based on
Willy Tarreaudf23c0c2019-03-13 10:10:49 +01001467 * maxconn and pipes. If there are not enough FDs left, DEFAULT_MAXCONN (by
1468 * default 100) is returned as it is expected that it will even run on tight
1469 * environments, and will maintain compatibility with previous packages that
1470 * used to rely on this value as the default one. The system will emit a
1471 * warning indicating how many FDs are missing anyway if needed.
Willy Tarreauac350932019-03-01 15:43:14 +01001472 */
1473static int compute_ideal_maxconn()
1474{
1475 int ssl_sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1476 int engine_fds = global.ssl_used_async_engines * ssl_sides;
1477 int pipes = compute_ideal_maxpipes();
1478 int remain = rlim_fd_cur_at_boot;
1479 int maxconn;
1480
1481 /* we have to take into account these elements :
1482 * - number of engine_fds, which inflates the number of FD needed per
1483 * connection by this number.
1484 * - number of pipes per connection on average : for the unlimited
1485 * case, this is 0.5 pipe FDs per connection, otherwise it's a
1486 * fixed value of 2*pipes.
1487 * - two FDs per connection
1488 */
1489
1490 /* subtract listeners and checks */
1491 remain -= global.maxsock;
1492
Willy Tarreau3f200852019-03-14 19:13:17 +01001493 /* one epoll_fd/kqueue_fd per thread */
1494 remain -= global.nbthread;
1495
1496 /* one wake-up pipe (2 fd) per thread */
1497 remain -= 2 * global.nbthread;
1498
Willy Tarreauac350932019-03-01 15:43:14 +01001499 /* Fixed pipes values : we only subtract them if they're not larger
1500 * than the remaining FDs because pipes are optional.
1501 */
1502 if (pipes >= 0 && pipes * 2 < remain)
1503 remain -= pipes * 2;
1504
1505 if (pipes < 0) {
1506 /* maxsock = maxconn * 2 + maxconn/4 * 2 + maxconn * engine_fds.
1507 * = maxconn * (2 + 0.5 + engine_fds)
1508 * = maxconn * (4 + 1 + 2*engine_fds) / 2
1509 */
1510 maxconn = 2 * remain / (5 + 2 * engine_fds);
1511 } else {
1512 /* maxsock = maxconn * 2 + maxconn * engine_fds.
1513 * = maxconn * (2 + engine_fds)
1514 */
1515 maxconn = remain / (2 + engine_fds);
1516 }
1517
Willy Tarreaudf23c0c2019-03-13 10:10:49 +01001518 return MAX(maxconn, DEFAULT_MAXCONN);
Willy Tarreauac350932019-03-01 15:43:14 +01001519}
1520
William Lallemand73b85e72017-06-01 17:38:51 +02001521/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001522 * This function initializes all the necessary variables. It only returns
1523 * if everything is OK. If something fails, it exits.
1524 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001525static void init(int argc, char **argv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001526{
Willy Tarreaubaaee002006-06-26 02:48:02 +02001527 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001528 char *tmp;
1529 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +02001530 int err_code = 0;
Maxime de Roucy0f503922016-05-13 23:52:55 +02001531 char *err_msg = NULL;
Willy Tarreau477ecd82010-01-03 21:12:30 +01001532 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +00001533 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +02001534 char *change_dir = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001535 struct proxy *px;
Willy Tarreaue6945732016-12-21 19:57:00 +01001536 struct post_check_fct *pcf;
Willy Tarreauac350932019-03-01 15:43:14 +01001537 int ideal_maxconn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001538
Christopher Faulete3a5e352017-10-24 13:53:54 +02001539 global.mode = MODE_STARTING;
William Lallemand73b85e72017-06-01 17:38:51 +02001540 next_argv = copy_argv(argc, argv);
1541
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001542 if (!init_trash_buffers(1)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001543 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet748919a2017-07-26 14:59:46 +02001544 exit(1);
1545 }
David du Colombier7af46052012-05-16 14:16:48 +02001546
Emeric Brun2b920a12010-09-23 18:30:22 +02001547 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
1548 * the string in case of truncation, and at least FreeBSD appears not to do
1549 * it.
1550 */
1551 memset(hostname, 0, sizeof(hostname));
1552 gethostname(hostname, sizeof(hostname) - 1);
1553 memset(localpeer, 0, sizeof(localpeer));
1554 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
William Lallemanddaf4cd22018-04-17 16:46:13 +02001555 setenv("HAPROXY_LOCALPEER", localpeer, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02001556
Willy Tarreaubaaee002006-06-26 02:48:02 +02001557 /*
1558 * Initialize the previously static variables.
1559 */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001560
Willy Tarreau173d9952018-01-26 21:48:23 +01001561 totalconn = actconn = listeners = stopping = 0;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001562 killed = 0;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001563
Willy Tarreaubaaee002006-06-26 02:48:02 +02001564
1565#ifdef HAPROXY_MEMMAX
Willy Tarreau70060452015-12-14 12:46:07 +01001566 global.rlimit_memmax_all = HAPROXY_MEMMAX;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001567#endif
1568
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001569 tzset();
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001570 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001571 start_date = now;
1572
Willy Tarreau84310e22014-02-14 11:59:04 +01001573 srandom(now_ms - getpid());
1574
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001575 if (init_acl() != 0)
1576 exit(1);
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001577
Willy Tarreau8280d642009-09-23 23:37:52 +02001578 /* warning, we init buffers later */
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02001579 if (!init_http(&err_msg)) {
1580 ha_alert("%s. Aborting.\n", err_msg);
1581 free(err_msg);
1582 abort();
1583 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001584
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001585 /* Initialise lua. */
1586 hlua_init();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001587
Christopher Fauletff2613e2016-11-09 11:36:17 +01001588 /* Initialize process vars */
1589 vars_init(&global.vars, SCOPE_PROC);
1590
Willy Tarreau43b78992009-01-25 15:42:27 +01001591 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001592#if defined(ENABLE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001593 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001594#endif
1595#if defined(ENABLE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001596 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001597#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001598#if defined(ENABLE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +01001599 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001600#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001601#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001602 global.tune.options |= GTUNE_USE_SPLICE;
1603#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001604#if defined(USE_GETADDRINFO)
1605 global.tune.options |= GTUNE_USE_GAI;
1606#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001607#if defined(SO_REUSEPORT)
1608 global.tune.options |= GTUNE_USE_REUSEPORT;
1609#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001610
1611 pid = getpid();
1612 progname = *argv;
1613 while ((tmp = strchr(progname, '/')) != NULL)
1614 progname = tmp + 1;
1615
Kevinm48936af2010-12-22 16:08:21 +00001616 /* the process name is used for the logs only */
Dragan Dosen43885c72015-10-01 13:18:13 +02001617 chunk_initstr(&global.log_tag, strdup(progname));
Kevinm48936af2010-12-22 16:08:21 +00001618
Willy Tarreaubaaee002006-06-26 02:48:02 +02001619 argc--; argv++;
1620 while (argc > 0) {
1621 char *flag;
1622
1623 if (**argv == '-') {
1624 flag = *argv+1;
1625
1626 /* 1 arg */
1627 if (*flag == 'v') {
1628 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +01001629 if (flag[1] == 'v') /* -vv */
1630 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001631 exit(0);
1632 }
1633#if defined(ENABLE_EPOLL)
1634 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +01001635 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001636#endif
1637#if defined(ENABLE_POLL)
1638 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +01001639 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001640#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +02001641#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001642 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +01001643 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001644#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001645#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001646 else if (*flag == 'd' && flag[1] == 'S')
1647 global.tune.options &= ~GTUNE_USE_SPLICE;
1648#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001649#if defined(USE_GETADDRINFO)
1650 else if (*flag == 'd' && flag[1] == 'G')
1651 global.tune.options &= ~GTUNE_USE_GAI;
1652#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001653#if defined(SO_REUSEPORT)
1654 else if (*flag == 'd' && flag[1] == 'R')
1655 global.tune.options &= ~GTUNE_USE_REUSEPORT;
1656#endif
Emeric Brun850efd52014-01-29 12:24:34 +01001657 else if (*flag == 'd' && flag[1] == 'V')
1658 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001659 else if (*flag == 'V')
1660 arg_mode |= MODE_VERBOSE;
1661 else if (*flag == 'd' && flag[1] == 'b')
1662 arg_mode |= MODE_FOREGROUND;
Willy Tarreau6e064432012-05-08 15:40:42 +02001663 else if (*flag == 'd' && flag[1] == 'M')
1664 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreau3eed10e2016-11-07 21:03:16 +01001665 else if (*flag == 'd' && flag[1] == 'r')
1666 global.tune.options |= GTUNE_RESOLVE_DONTFAIL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001667 else if (*flag == 'd')
1668 arg_mode |= MODE_DEBUG;
1669 else if (*flag == 'c')
1670 arg_mode |= MODE_CHECK;
William Lallemand095ba4c2017-06-01 17:38:50 +02001671 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +02001672 arg_mode |= MODE_DAEMON;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001673 else if (*flag == 'W' && flag[1] == 's') {
Lukas Tribusf46bf952017-11-21 12:39:34 +01001674 arg_mode |= MODE_MWORKER | MODE_FOREGROUND;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001675#if defined(USE_SYSTEMD)
1676 global.tune.options |= GTUNE_USE_SYSTEMD;
1677#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001678 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 +01001679 usage(progname);
1680#endif
1681 }
William Lallemand095ba4c2017-06-01 17:38:50 +02001682 else if (*flag == 'W')
1683 arg_mode |= MODE_MWORKER;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001684 else if (*flag == 'q')
1685 arg_mode |= MODE_QUIET;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001686 else if (*flag == 'x') {
William Lallemand45eff442017-06-19 15:57:55 +02001687 if (argc <= 1 || argv[1][0] == '-') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001688 ha_alert("Unix socket path expected with the -x flag\n\n");
William Lallemand45eff442017-06-19 15:57:55 +02001689 usage(progname);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001690 }
William Lallemand4fc09692017-06-19 16:37:19 +02001691 if (old_unixsocket)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001692 ha_warning("-x option already set, overwriting the value\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001693 old_unixsocket = argv[1];
William Lallemand4fc09692017-06-19 16:37:19 +02001694
Olivier Houchardf73629d2017-04-05 22:33:04 +02001695 argv++;
1696 argc--;
1697 }
William Lallemande7361152018-10-26 14:47:36 +02001698 else if (*flag == 'S') {
1699 struct wordlist *c;
1700
1701 if (argc <= 1 || argv[1][0] == '-') {
1702 ha_alert("Socket and optional bind parameters expected with the -S flag\n");
1703 usage(progname);
1704 }
1705 if ((c = malloc(sizeof(*c))) == NULL || (c->s = strdup(argv[1])) == NULL) {
1706 ha_alert("Cannot allocate memory\n");
1707 exit(EXIT_FAILURE);
1708 }
1709 LIST_ADD(&mworker_cli_conf, &c->list);
1710
1711 argv++;
1712 argc--;
1713 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001714 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1715 /* list of pids to finish ('f') or terminate ('t') */
1716
1717 if (flag[1] == 'f')
1718 oldpids_sig = SIGUSR1; /* finish then exit */
1719 else
1720 oldpids_sig = SIGTERM; /* terminate immediately */
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001721 while (argc > 1 && argv[1][0] != '-') {
Chris Lane236062f2018-02-05 23:15:44 +00001722 char * endptr = NULL;
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001723 oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int));
1724 if (!oldpids) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001725 ha_alert("Cannot allocate old pid : out of memory.\n");
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001726 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001727 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001728 argc--; argv++;
Chris Lane236062f2018-02-05 23:15:44 +00001729 errno = 0;
1730 oldpids[nb_oldpids] = strtol(*argv, &endptr, 10);
1731 if (errno) {
1732 ha_alert("-%2s option: failed to parse {%s}: %s\n",
1733 flag,
1734 *argv, strerror(errno));
1735 exit(1);
1736 } else if (endptr && strlen(endptr)) {
1737 while (isspace(*endptr)) endptr++;
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001738 if (*endptr != 0) {
Chris Lane236062f2018-02-05 23:15:44 +00001739 ha_alert("-%2s option: some bytes unconsumed in PID list {%s}\n",
1740 flag, endptr);
1741 exit(1);
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001742 }
Chris Lane236062f2018-02-05 23:15:44 +00001743 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001744 if (oldpids[nb_oldpids] <= 0)
1745 usage(progname);
1746 nb_oldpids++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001747 }
1748 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001749 else if (flag[0] == '-' && flag[1] == 0) { /* "--" */
1750 /* now that's a cfgfile list */
1751 argv++; argc--;
1752 while (argc > 0) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02001753 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001754 ha_alert("Cannot load configuration file/directory %s : %s\n",
1755 *argv,
1756 err_msg);
Willy Tarreaua088d312015-10-08 11:58:48 +02001757 exit(1);
1758 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001759 argv++; argc--;
1760 }
1761 break;
1762 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001763 else { /* >=2 args */
1764 argv++; argc--;
1765 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001766 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001767
1768 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +02001769 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001770 case 'n' : cfg_maxconn = atol(*argv); break;
Willy Tarreau70060452015-12-14 12:46:07 +01001771 case 'm' : global.rlimit_memmax_all = atol(*argv); break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001772 case 'N' : cfg_maxpconn = atol(*argv); break;
William Lallemanddaf4cd22018-04-17 16:46:13 +02001773 case 'L' :
1774 strncpy(localpeer, *argv, sizeof(localpeer) - 1);
1775 setenv("HAPROXY_LOCALPEER", localpeer, 1);
1776 break;
Willy Tarreau5d01a632009-06-22 16:02:30 +02001777 case 'f' :
Maxime de Roucy0f503922016-05-13 23:52:55 +02001778 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001779 ha_alert("Cannot load configuration file/directory %s : %s\n",
1780 *argv,
1781 err_msg);
Willy Tarreau5d01a632009-06-22 16:02:30 +02001782 exit(1);
1783 }
Willy Tarreau5d01a632009-06-22 16:02:30 +02001784 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001785 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001786 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001787 }
1788 }
1789 }
1790 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001791 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001792 argv++; argc--;
1793 }
1794
Christopher Faulete3a5e352017-10-24 13:53:54 +02001795 global.mode |= (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
1796 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001797
William Lallemand944e6192018-11-21 15:48:31 +01001798 if (getenv("HAPROXY_MWORKER_WAIT_ONLY")) {
William Lallemandcb11fd22017-06-01 17:38:52 +02001799 unsetenv("HAPROXY_MWORKER_WAIT_ONLY");
William Lallemand944e6192018-11-21 15:48:31 +01001800 global.mode |= MODE_MWORKER_WAIT;
1801 global.mode &= ~MODE_MWORKER;
William Lallemandcb11fd22017-06-01 17:38:52 +02001802 }
1803
1804 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {
1805 atexit_flag = 1;
1806 atexit(reexec_on_failure);
1807 }
1808
Willy Tarreau576132e2011-09-10 19:26:56 +02001809 if (change_dir && chdir(change_dir) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001810 ha_alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
Willy Tarreau576132e2011-09-10 19:26:56 +02001811 exit(1);
1812 }
1813
Willy Tarreaubaaee002006-06-26 02:48:02 +02001814 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001815
1816 init_default_instance();
1817
William Lallemand944e6192018-11-21 15:48:31 +01001818 /* in wait mode, we don't try to read the configuration files */
1819 if (!(global.mode & MODE_MWORKER_WAIT)) {
Willy Tarreauc4382422009-12-06 13:10:44 +01001820
William Lallemand944e6192018-11-21 15:48:31 +01001821 /* handle cfgfiles that are actually directories */
1822 cfgfiles_expand_directories();
1823
1824 if (LIST_ISEMPTY(&cfg_cfgfiles))
1825 usage(progname);
1826
1827
1828 list_for_each_entry(wl, &cfg_cfgfiles, list) {
1829 int ret;
1830
1831 ret = readcfgfile(wl->s);
1832 if (ret == -1) {
1833 ha_alert("Could not open configuration file %s : %s\n",
1834 wl->s, strerror(errno));
1835 exit(1);
1836 }
1837 if (ret & (ERR_ABORT|ERR_FATAL))
1838 ha_alert("Error(s) found in configuration file : %s\n", wl->s);
1839 err_code |= ret;
1840 if (err_code & ERR_ABORT)
1841 exit(1);
Willy Tarreauc4382422009-12-06 13:10:44 +01001842 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001843
William Lallemand944e6192018-11-21 15:48:31 +01001844 /* do not try to resolve arguments nor to spot inconsistencies when
1845 * the configuration contains fatal errors caused by files not found
1846 * or failed memory allocations.
1847 */
1848 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1849 ha_alert("Fatal errors found in configuration.\n");
1850 exit(1);
1851 }
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02001852 }
William Lallemandce83b4a2018-10-26 14:47:30 +02001853 if (global.mode & MODE_MWORKER) {
1854 int proc;
William Lallemand16dd1b32018-11-19 18:46:18 +01001855 struct mworker_proc *tmproc;
1856
1857 if (getenv("HAPROXY_MWORKER_REEXEC") == NULL) {
1858
1859 tmproc = malloc(sizeof(*tmproc));
1860 if (!tmproc) {
1861 ha_alert("Cannot allocate process structures.\n");
1862 exit(EXIT_FAILURE);
1863 }
1864 tmproc->type = 'm'; /* master */
1865 tmproc->reloads = 0;
1866 tmproc->relative_pid = 0;
1867 tmproc->pid = pid;
1868 tmproc->timestamp = start_date.tv_sec;
1869 tmproc->ipc_fd[0] = -1;
1870 tmproc->ipc_fd[1] = -1;
1871
1872 proc_self = tmproc;
1873
1874 LIST_ADDQ(&proc_list, &tmproc->list);
1875 }
William Lallemandce83b4a2018-10-26 14:47:30 +02001876
1877 for (proc = 0; proc < global.nbproc; proc++) {
William Lallemandce83b4a2018-10-26 14:47:30 +02001878
1879 tmproc = malloc(sizeof(*tmproc));
1880 if (!tmproc) {
1881 ha_alert("Cannot allocate process structures.\n");
1882 exit(EXIT_FAILURE);
1883 }
1884
William Lallemand16dd1b32018-11-19 18:46:18 +01001885 tmproc->type = 'w'; /* worker */
William Lallemandce83b4a2018-10-26 14:47:30 +02001886 tmproc->pid = -1;
1887 tmproc->reloads = 0;
William Lallemande3683302018-11-19 18:46:17 +01001888 tmproc->timestamp = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02001889 tmproc->relative_pid = 1 + proc;
William Lallemand550db6d2018-11-06 17:37:12 +01001890 tmproc->ipc_fd[0] = -1;
1891 tmproc->ipc_fd[1] = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02001892
1893 if (mworker_cli_sockpair_new(tmproc, proc) < 0) {
1894 exit(EXIT_FAILURE);
1895 }
1896
1897 LIST_ADDQ(&proc_list, &tmproc->list);
1898 }
William Lallemand944e6192018-11-21 15:48:31 +01001899 }
1900 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
1901 struct wordlist *it, *c;
1902
William Lallemand1b663612018-10-26 14:47:33 +02001903 mworker_env_to_proc_list(); /* get the info of the children in the env */
William Lallemand8a022572018-10-26 14:47:35 +02001904
William Lallemande7361152018-10-26 14:47:36 +02001905
William Lallemand550db6d2018-11-06 17:37:12 +01001906 if (!LIST_ISEMPTY(&mworker_cli_conf)) {
William Lallemande7361152018-10-26 14:47:36 +02001907
William Lallemand550db6d2018-11-06 17:37:12 +01001908 if (mworker_cli_proxy_create() < 0) {
William Lallemande7361152018-10-26 14:47:36 +02001909 ha_alert("Can't create the master's CLI.\n");
1910 exit(EXIT_FAILURE);
1911 }
William Lallemande7361152018-10-26 14:47:36 +02001912
William Lallemand550db6d2018-11-06 17:37:12 +01001913 list_for_each_entry_safe(c, it, &mworker_cli_conf, list) {
1914
1915 if (mworker_cli_proxy_new_listener(c->s) < 0) {
1916 ha_alert("Can't create the master's CLI.\n");
1917 exit(EXIT_FAILURE);
1918 }
1919 LIST_DEL(&c->list);
1920 free(c->s);
1921 free(c);
1922 }
1923 }
William Lallemandce83b4a2018-10-26 14:47:30 +02001924 }
1925
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001926 pattern_finalize_config();
1927
Willy Tarreaubb925012009-07-23 13:36:36 +02001928 err_code |= check_config_validity();
1929 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001930 ha_alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001931 exit(1);
1932 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001933
Willy Tarreau70060452015-12-14 12:46:07 +01001934 /* recompute the amount of per-process memory depending on nbproc and
1935 * the shared SSL cache size (allowed to exist in all processes).
1936 */
1937 if (global.rlimit_memmax_all) {
1938#if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE)
1939 int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL;
1940
1941 global.rlimit_memmax =
1942 ((((int64_t)global.rlimit_memmax_all * 1048576LL) -
1943 ssl_cache_bytes) / global.nbproc +
1944 ssl_cache_bytes + 1048575LL) / 1048576LL;
1945#else
1946 global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc;
1947#endif
1948 }
1949
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001950#ifdef CONFIG_HAP_NS
1951 err_code |= netns_init();
1952 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001953 ha_alert("Failed to initialize namespace support.\n");
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001954 exit(1);
1955 }
1956#endif
1957
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01001958 /* Apply server states */
1959 apply_server_state();
1960
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001961 for (px = proxies_list; px; px = px->next)
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01001962 srv_compute_all_admin_states(px);
1963
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001964 /* Apply servers' configured address */
1965 err_code |= srv_init_addr();
1966 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001967 ha_alert("Failed to initialize server(s) addr.\n");
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001968 exit(1);
1969 }
1970
Willy Tarreaubaaee002006-06-26 02:48:02 +02001971 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001972 struct peers *pr;
1973 struct proxy *px;
1974
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001975 for (pr = cfg_peers; pr; pr = pr->next)
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001976 if (pr->peers_fe)
1977 break;
1978
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001979 for (px = proxies_list; px; px = px->next)
Willy Tarreau4348fad2012-09-20 16:48:07 +02001980 if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners))
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001981 break;
1982
1983 if (pr || px) {
1984 /* At least one peer or one listener has been found */
1985 qfprintf(stdout, "Configuration file is valid\n");
1986 exit(0);
1987 }
1988 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
1989 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001990 }
1991
Emeric Brunc60def82017-09-27 14:59:38 +02001992 global_listener_queue_task = task_new(MAX_THREADS_MASK);
Willy Tarreaue9b26022011-08-01 20:57:55 +02001993 if (!global_listener_queue_task) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001994 ha_alert("Out of memory when initializing global task\n");
Willy Tarreaue9b26022011-08-01 20:57:55 +02001995 exit(1);
1996 }
1997 /* very simple initialization, users will queue the task if needed */
1998 global_listener_queue_task->context = NULL; /* not even a context! */
1999 global_listener_queue_task->process = manage_global_listener_queue;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002000
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002001 /* now we know the buffer size, we can initialize the channels and buffers */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002002 init_buffer();
Willy Tarreau8280d642009-09-23 23:37:52 +02002003
Willy Tarreaue6945732016-12-21 19:57:00 +01002004 list_for_each_entry(pcf, &post_check_list, list) {
2005 err_code |= pcf->fct();
2006 if (err_code & (ERR_ABORT|ERR_FATAL))
2007 exit(1);
2008 }
2009
Willy Tarreaubaaee002006-06-26 02:48:02 +02002010 if (cfg_maxconn > 0)
2011 global.maxconn = cfg_maxconn;
2012
Willy Tarreau8d687d82019-03-01 09:39:42 +01002013 if (global.stats_fe)
2014 global.maxsock += global.stats_fe->maxconn;
2015
2016 if (cfg_peers) {
2017 /* peers also need to bypass global maxconn */
2018 struct peers *p = cfg_peers;
2019
2020 for (p = cfg_peers; p; p = p->next)
2021 if (p->peers_fe)
2022 global.maxsock += p->peers_fe->maxconn;
2023 }
2024
Willy Tarreaubaaee002006-06-26 02:48:02 +02002025 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +02002026 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002027 global.pidfile = strdup(cfg_pidfile);
2028 }
2029
Willy Tarreaud0256482015-01-15 21:45:22 +01002030 /* Now we want to compute the maxconn and possibly maxsslconn values.
Willy Tarreauac350932019-03-01 15:43:14 +01002031 * It's a bit tricky. Maxconn defaults to the pre-computed value based
2032 * on rlim_fd_cur and the number of FDs in use due to the configuration,
2033 * and maxsslconn defaults to DEFAULT_MAXSSLCONN. On top of that we can
2034 * enforce a lower limit based on memmax.
Willy Tarreaud0256482015-01-15 21:45:22 +01002035 *
2036 * If memmax is set, then it depends on which values are set. If
2037 * maxsslconn is set, we use memmax to determine how many cleartext
2038 * connections may be added, and set maxconn to the sum of the two.
2039 * If maxconn is set and not maxsslconn, maxsslconn is computed from
2040 * the remaining amount of memory between memmax and the cleartext
2041 * connections. If neither are set, then it is considered that all
2042 * connections are SSL-capable, and maxconn is computed based on this,
2043 * then maxsslconn accordingly. We need to know if SSL is used on the
2044 * frontends, backends, or both, because when it's used on both sides,
2045 * we need twice the value for maxsslconn, but we only count the
2046 * handshake once since it is not performed on the two sides at the
2047 * same time (frontend-side is terminated before backend-side begins).
2048 * The SSL stack is supposed to have filled ssl_session_cost and
Willy Tarreau474b96a2015-01-28 19:03:21 +01002049 * ssl_handshake_cost during its initialization. In any case, if
2050 * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for
2051 * maxconn in order to protect the system.
Willy Tarreaud0256482015-01-15 21:45:22 +01002052 */
Willy Tarreauac350932019-03-01 15:43:14 +01002053 ideal_maxconn = compute_ideal_maxconn();
2054
Willy Tarreaud0256482015-01-15 21:45:22 +01002055 if (!global.rlimit_memmax) {
2056 if (global.maxconn == 0) {
Willy Tarreauac350932019-03-01 15:43:14 +01002057 global.maxconn = ideal_maxconn;
Willy Tarreaud0256482015-01-15 21:45:22 +01002058 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2059 fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn);
2060 }
2061 }
2062#ifdef USE_OPENSSL
2063 else if (!global.maxconn && !global.maxsslconn &&
2064 (global.ssl_used_frontend || global.ssl_used_backend)) {
2065 /* memmax is set, compute everything automatically. Here we want
2066 * to ensure that all SSL connections will be served. We take
2067 * care of the number of sides where SSL is used, and consider
2068 * the worst case : SSL used on both sides and doing a handshake
2069 * simultaneously. Note that we can't have more than maxconn
2070 * handshakes at a time by definition, so for the worst case of
2071 * two SSL conns per connection, we count a single handshake.
2072 */
2073 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2074 int64_t mem = global.rlimit_memmax * 1048576ULL;
2075
2076 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2077 mem -= global.maxzlibmem;
2078 mem = mem * MEM_USABLE_RATIO;
2079
2080 global.maxconn = mem /
Willy Tarreau87b09662015-04-03 00:22:06 +02002081 ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream
Willy Tarreaud0256482015-01-15 21:45:22 +01002082 sides * global.ssl_session_max_cost + // SSL buffers, one per side
2083 global.ssl_handshake_max_cost); // 1 handshake per connection max
2084
Willy Tarreauac350932019-03-01 15:43:14 +01002085 global.maxconn = MIN(global.maxconn, ideal_maxconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01002086 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01002087#ifdef SYSTEM_MAXCONN
Willy Tarreauca783d42019-03-13 10:03:07 +01002088 if (global.maxconn > SYSTEM_MAXCONN)
2089 global.maxconn = SYSTEM_MAXCONN;
Willy Tarreau474b96a2015-01-28 19:03:21 +01002090#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01002091 global.maxsslconn = sides * global.maxconn;
2092 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2093 fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n",
2094 global.maxconn, global.maxsslconn);
2095 }
2096 else if (!global.maxsslconn &&
2097 (global.ssl_used_frontend || global.ssl_used_backend)) {
2098 /* memmax and maxconn are known, compute maxsslconn automatically.
2099 * maxsslconn being forced, we don't know how many of it will be
2100 * on each side if both sides are being used. The worst case is
2101 * when all connections use only one SSL instance because
2102 * handshakes may be on two sides at the same time.
2103 */
2104 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2105 int64_t mem = global.rlimit_memmax * 1048576ULL;
2106 int64_t sslmem;
2107
2108 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2109 mem -= global.maxzlibmem;
2110 mem = mem * MEM_USABLE_RATIO;
2111
Willy Tarreau87b09662015-04-03 00:22:06 +02002112 sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01002113 global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost);
2114 global.maxsslconn = round_2dig(global.maxsslconn);
2115
2116 if (sslmem <= 0 || global.maxsslconn < sides) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002117 ha_alert("Cannot compute the automatic maxsslconn because global.maxconn is already too "
2118 "high for the global.memmax value (%d MB). The absolute maximum possible value "
2119 "without SSL is %d, but %d was found and SSL is in use.\n",
2120 global.rlimit_memmax,
2121 (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)),
2122 global.maxconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01002123 exit(1);
2124 }
2125
2126 if (global.maxsslconn > sides * global.maxconn)
2127 global.maxsslconn = sides * global.maxconn;
2128
2129 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
2130 fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn);
2131 }
2132#endif
2133 else if (!global.maxconn) {
2134 /* memmax and maxsslconn are known/unused, compute maxconn automatically */
2135 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2136 int64_t mem = global.rlimit_memmax * 1048576ULL;
2137 int64_t clearmem;
2138
2139 if (global.ssl_used_frontend || global.ssl_used_backend)
2140 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
2141
2142 mem -= global.maxzlibmem;
2143 mem = mem * MEM_USABLE_RATIO;
2144
2145 clearmem = mem;
2146 if (sides)
2147 clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn;
2148
Willy Tarreau87b09662015-04-03 00:22:06 +02002149 global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreauac350932019-03-01 15:43:14 +01002150 global.maxconn = MIN(global.maxconn, ideal_maxconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01002151 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01002152#ifdef SYSTEM_MAXCONN
Willy Tarreauca783d42019-03-13 10:03:07 +01002153 if (global.maxconn > SYSTEM_MAXCONN)
2154 global.maxconn = SYSTEM_MAXCONN;
Willy Tarreau474b96a2015-01-28 19:03:21 +01002155#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01002156
2157 if (clearmem <= 0 || !global.maxconn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002158 ha_alert("Cannot compute the automatic maxconn because global.maxsslconn is already too "
2159 "high for the global.memmax value (%d MB). The absolute maximum possible value "
2160 "is %d, but %d was found.\n",
2161 global.rlimit_memmax,
2162 (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)),
2163 global.maxsslconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01002164 exit(1);
2165 }
2166
2167 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
2168 if (sides && global.maxsslconn > sides * global.maxconn) {
2169 fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn "
2170 "to be limited to %d. Better reduce global.maxsslconn to get more "
2171 "room for extra connections.\n", global.maxsslconn, global.maxconn);
2172 }
2173 fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn);
2174 }
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002175 }
2176
Willy Tarreau5a023f02019-03-01 14:19:31 +01002177 if (!global.maxpipes)
2178 global.maxpipes = compute_ideal_maxpipes();
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002179
Willy Tarreauabacc2c2011-09-07 14:26:33 +02002180 global.hardmaxconn = global.maxconn; /* keep this max value */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002181 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
Willy Tarreau3ec79b92009-01-18 20:39:42 +01002182 global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */
Willy Tarreau2c58b412019-03-14 19:10:55 +01002183 global.maxsock += global.nbthread; /* one epoll_fd/kqueue_fd per thread */
2184 global.maxsock += 2 * global.nbthread; /* one wake-up pipe (2 fd) per thread */
2185
Emeric Brunece0c332017-12-06 13:51:49 +01002186 /* compute fd used by async engines */
2187 if (global.ssl_used_async_engines) {
2188 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2189 global.maxsock += global.maxconn * sides * global.ssl_used_async_engines;
2190 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002191
Willy Tarreauc8d5b952019-02-27 17:25:52 +01002192 proxy_adjust_all_maxconn();
2193
Willy Tarreau1db37712007-06-03 17:16:49 +02002194 if (global.tune.maxpollevents <= 0)
2195 global.tune.maxpollevents = MAX_POLL_EVENTS;
2196
Olivier Houchard1599b802018-05-24 18:59:04 +02002197 if (global.tune.runqueue_depth <= 0)
2198 global.tune.runqueue_depth = RUNQUEUE_DEPTH;
2199
Willy Tarreau6f4a82c2009-03-21 20:43:57 +01002200 if (global.tune.recv_enough == 0)
2201 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
2202
Willy Tarreau27097842015-09-28 13:53:23 +02002203 if (global.tune.maxrewrite < 0)
2204 global.tune.maxrewrite = MAXREWRITE;
2205
Willy Tarreau27a674e2009-08-17 07:23:33 +02002206 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
2207 global.tune.maxrewrite = global.tune.bufsize / 2;
2208
Willy Tarreaubaaee002006-06-26 02:48:02 +02002209 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
2210 /* command line debug mode inhibits configuration mode */
William Lallemand095ba4c2017-06-01 17:38:50 +02002211 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002212 global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND));
2213 }
2214
William Lallemand095ba4c2017-06-01 17:38:50 +02002215 if (arg_mode & MODE_DAEMON) {
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002216 /* command line daemon mode inhibits foreground and debug modes mode */
2217 global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND);
William Lallemand095ba4c2017-06-01 17:38:50 +02002218 global.mode |= arg_mode & MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002219 }
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002220
2221 global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002222
William Lallemand095ba4c2017-06-01 17:38:50 +02002223 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002224 ha_warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
William Lallemand095ba4c2017-06-01 17:38:50 +02002225 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002226 }
2227
William Lallemand095ba4c2017-06-01 17:38:50 +02002228 if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002229 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
Christopher Faulet767a84b2017-11-24 16:50:31 +01002230 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 +02002231 global.nbproc = 1;
2232 }
2233
2234 if (global.nbproc < 1)
2235 global.nbproc = 1;
2236
Christopher Fauletbe0faa22017-08-29 15:37:10 +02002237 if (global.nbthread < 1)
2238 global.nbthread = 1;
2239
Christopher Faulet3ef26392017-08-29 16:46:57 +02002240 /* Realloc trash buffers because global.tune.bufsize may have changed */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002241 if (!init_trash_buffers(0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002242 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet3ef26392017-08-29 16:46:57 +02002243 exit(1);
2244 }
2245
Christopher Faulet96d44832017-11-14 22:02:30 +01002246 if (!init_log_buffers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002247 ha_alert("failed to initialize log buffers.\n");
Christopher Faulet96d44832017-11-14 22:02:30 +01002248 exit(1);
2249 }
2250
Willy Tarreauef1d1f82007-04-16 00:25:25 +02002251 /*
2252 * Note: we could register external pollers here.
2253 * Built-in pollers have been registered before main().
2254 */
Willy Tarreau4f60f162007-04-08 16:39:58 +02002255
Willy Tarreau43b78992009-01-25 15:42:27 +01002256 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +02002257 disable_poller("kqueue");
2258
Willy Tarreau43b78992009-01-25 15:42:27 +01002259 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002260 disable_poller("epoll");
2261
Willy Tarreau43b78992009-01-25 15:42:27 +01002262 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002263 disable_poller("poll");
2264
Willy Tarreau43b78992009-01-25 15:42:27 +01002265 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002266 disable_poller("select");
2267
2268 /* Note: we could disable any poller by name here */
2269
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002270 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +02002271 list_pollers(stderr);
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002272 fprintf(stderr, "\n");
2273 list_filters(stderr);
2274 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002275
Willy Tarreau4f60f162007-04-08 16:39:58 +02002276 if (!init_pollers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002277 ha_alert("No polling mechanism available.\n"
2278 " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n"
2279 " is too low on this platform to support maxconn and the number of listeners\n"
2280 " and servers. You should rebuild haproxy specifying your system using TARGET=\n"
2281 " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n"
2282 " global maxconn setting to accommodate the system's limitation. For reference,\n"
2283 " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n"
2284 " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n"
2285 " check build settings using 'haproxy -vv'.\n\n",
2286 FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002287 exit(1);
2288 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002289 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
2290 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002291 }
2292
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002293 if (!global.node)
2294 global.node = strdup(hostname);
2295
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01002296 if (!hlua_post_init())
2297 exit(1);
Thomas Holmes6abded42015-05-12 16:23:58 +01002298
Maxime de Roucy0f503922016-05-13 23:52:55 +02002299 free(err_msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002300}
2301
Simon Horman6fb82592011-07-15 13:14:11 +09002302static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +09002303{
Simon Hormanac821422011-07-15 13:14:09 +09002304 struct acl_term_suite *suite, *suiteb;
2305 struct acl_term *term, *termb;
2306
Simon Horman6fb82592011-07-15 13:14:11 +09002307 if (!cond)
2308 return;
2309
2310 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
2311 list_for_each_entry_safe(term, termb, &suite->terms, list) {
2312 LIST_DEL(&term->list);
2313 free(term);
Simon Hormanac821422011-07-15 13:14:09 +09002314 }
Simon Horman6fb82592011-07-15 13:14:11 +09002315 LIST_DEL(&suite->list);
2316 free(suite);
2317 }
2318
2319 free(cond);
2320}
2321
2322static void deinit_tcp_rules(struct list *rules)
2323{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002324 struct act_rule *trule, *truleb;
Simon Horman6fb82592011-07-15 13:14:11 +09002325
2326 list_for_each_entry_safe(trule, truleb, rules, list) {
Simon Hormanac821422011-07-15 13:14:09 +09002327 LIST_DEL(&trule->list);
Simon Horman6fb82592011-07-15 13:14:11 +09002328 deinit_acl_cond(trule->cond);
Simon Hormanac821422011-07-15 13:14:09 +09002329 free(trule);
2330 }
2331}
2332
Simon Horman6fb82592011-07-15 13:14:11 +09002333static void deinit_stick_rules(struct list *rules)
2334{
2335 struct sticking_rule *rule, *ruleb;
2336
2337 list_for_each_entry_safe(rule, ruleb, rules, list) {
2338 LIST_DEL(&rule->list);
2339 deinit_acl_cond(rule->cond);
Christopher Faulet476e5d02016-10-26 11:34:47 +02002340 release_sample_expr(rule->expr);
Simon Horman6fb82592011-07-15 13:14:11 +09002341 free(rule);
2342 }
2343}
2344
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002345void deinit(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002346{
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002347 struct proxy *p = proxies_list, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002348 struct cap_hdr *h,*h_next;
2349 struct server *s,*s_next;
2350 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002351 struct acl_cond *cond, *condb;
2352 struct hdr_exp *exp, *expb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002353 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002354 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002355 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002356 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002357 struct wordlist *wl, *wlb;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002358 struct cond_wordlist *cwl, *cwlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002359 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +02002360 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +01002361 struct logformat_node *lf, *lfb;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002362 struct bind_conf *bind_conf, *bind_back;
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002363 struct build_opts_str *bol, *bolb;
Willy Tarreau05554e62016-12-21 20:46:26 +01002364 struct post_deinit_fct *pdf;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002365 int i;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002366
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002367 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002368 while (p) {
Willy Tarreau8113a5d2012-10-04 08:01:43 +02002369 free(p->conf.file);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002370 free(p->id);
2371 free(p->check_req);
2372 free(p->cookie_name);
2373 free(p->cookie_domain);
Willy Tarreau4c03d1c2019-01-14 15:23:54 +01002374 free(p->lbprm.arg_str);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002375 free(p->capture_name);
2376 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +09002377 free(p->rdp_cookie_name);
Willy Tarreau62a61232013-04-12 18:13:46 +02002378 if (p->conf.logformat_string != default_http_log_format &&
2379 p->conf.logformat_string != default_tcp_log_format &&
2380 p->conf.logformat_string != clf_http_log_format)
2381 free(p->conf.logformat_string);
Willy Tarreau196729e2012-05-31 19:30:26 +02002382
Willy Tarreau62a61232013-04-12 18:13:46 +02002383 free(p->conf.lfs_file);
2384 free(p->conf.uniqueid_format_string);
2385 free(p->conf.uif_file);
Willy Tarreau0cac26c2019-01-14 16:55:42 +01002386 if ((p->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_MAP)
2387 free(p->lbprm.map.srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002388
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002389 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
2390 free(p->conf.logformat_sd_string);
2391 free(p->conf.lfsd_file);
2392
Willy Tarreaua534fea2008-08-03 12:19:50 +02002393 for (i = 0; i < HTTP_ERR_SIZE; i++)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002394 chunk_destroy(&p->errmsg[i]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002395
Willy Tarreauf4f04122010-01-28 18:10:50 +01002396 list_for_each_entry_safe(cwl, cwlb, &p->req_add, list) {
2397 LIST_DEL(&cwl->list);
2398 free(cwl->s);
2399 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002400 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002401
Willy Tarreauf4f04122010-01-28 18:10:50 +01002402 list_for_each_entry_safe(cwl, cwlb, &p->rsp_add, list) {
2403 LIST_DEL(&cwl->list);
2404 free(cwl->s);
2405 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002406 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002407
Willy Tarreaub80c2302007-11-30 20:51:32 +01002408 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
2409 LIST_DEL(&cond->list);
2410 prune_acl_cond(cond);
2411 free(cond);
2412 }
2413
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002414 for (exp = p->req_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002415 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002416 regex_free(exp->preg);
2417 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002418 }
2419
Willy Tarreau98d04852015-05-26 12:18:29 +02002420 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002421 expb = exp;
2422 exp = exp->next;
2423 free(expb);
2424 }
2425
2426 for (exp = p->rsp_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002427 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002428 regex_free(exp->preg);
2429 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002430 }
2431
Willy Tarreau98d04852015-05-26 12:18:29 +02002432 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002433 expb = exp;
2434 exp = exp->next;
2435 free(expb);
2436 }
2437
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002438 /* build a list of unique uri_auths */
2439 if (!ua)
2440 ua = p->uri_auth;
2441 else {
2442 /* check if p->uri_auth is unique */
2443 for (uap = ua; uap; uap=uap->next)
2444 if (uap == p->uri_auth)
2445 break;
2446
Willy Tarreauaccc4e12008-06-24 11:14:45 +02002447 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002448 /* add it, if it is */
2449 p->uri_auth->next = ua;
2450 ua = p->uri_auth;
2451 }
2452 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002453
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002454 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
2455 LIST_DEL(&acl->list);
2456 prune_acl(acl);
2457 free(acl);
2458 }
2459
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002460 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
2461 LIST_DEL(&srule->list);
2462 prune_acl_cond(srule->cond);
2463 free(srule->cond);
2464 free(srule);
2465 }
2466
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002467 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
2468 LIST_DEL(&rule->list);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002469 if (rule->cond) {
2470 prune_acl_cond(rule->cond);
2471 free(rule->cond);
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +01002472 free(rule->file);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002473 }
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002474 free(rule);
2475 }
2476
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002477 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
2478 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +01002479 if (rdr->cond) {
2480 prune_acl_cond(rdr->cond);
2481 free(rdr->cond);
2482 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002483 free(rdr->rdr_str);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002484 list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) {
2485 LIST_DEL(&lf->list);
2486 free(lf);
2487 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002488 free(rdr);
2489 }
2490
William Lallemand0f99e342011-10-12 17:50:54 +02002491 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
2492 LIST_DEL(&log->list);
2493 free(log);
2494 }
2495
William Lallemand723b73a2012-02-08 16:37:49 +01002496 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
2497 LIST_DEL(&lf->list);
2498 free(lf);
2499 }
2500
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002501 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
2502 LIST_DEL(&lf->list);
2503 free(lf);
2504 }
2505
Simon Hormanac821422011-07-15 13:14:09 +09002506 deinit_tcp_rules(&p->tcp_req.inspect_rules);
Kevin Zhu13ebef72019-01-30 16:01:21 +08002507 deinit_tcp_rules(&p->tcp_rep.inspect_rules);
Simon Hormanac821422011-07-15 13:14:09 +09002508 deinit_tcp_rules(&p->tcp_req.l4_rules);
2509
Simon Horman6fb82592011-07-15 13:14:11 +09002510 deinit_stick_rules(&p->storersp_rules);
2511 deinit_stick_rules(&p->sticking_rules);
2512
Willy Tarreaubaaee002006-06-26 02:48:02 +02002513 h = p->req_cap;
2514 while (h) {
2515 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002516 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002517 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002518 free(h);
2519 h = h_next;
2520 }/* end while(h) */
2521
2522 h = p->rsp_cap;
2523 while (h) {
2524 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002525 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002526 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002527 free(h);
2528 h = h_next;
2529 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002530
Willy Tarreaubaaee002006-06-26 02:48:02 +02002531 s = p->srv;
2532 while (s) {
2533 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002534
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002535 if (s->check.task) {
2536 task_delete(s->check.task);
2537 task_free(s->check.task);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002538 }
Simon Hormand60d6912013-11-25 10:46:36 +09002539 if (s->agent.task) {
2540 task_delete(s->agent.task);
2541 task_free(s->agent.task);
2542 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002543
Willy Tarreau2e993902011-10-31 11:53:20 +01002544 if (s->warmup) {
2545 task_delete(s->warmup);
2546 task_free(s->warmup);
2547 }
2548
Willy Tarreaua534fea2008-08-03 12:19:50 +02002549 free(s->id);
2550 free(s->cookie);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002551 free(s->check.bi.area);
2552 free(s->check.bo.area);
2553 free(s->agent.bi.area);
2554 free(s->agent.bo.area);
James Brown55f9ff12015-10-21 18:19:05 -07002555 free(s->agent.send_string);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002556 free(s->hostname_dn);
Sárközi, László34c01792014-09-05 10:08:23 +02002557 free((char*)s->conf.file);
Olivier Houchard7fc3be72018-11-22 18:50:54 +01002558 free(s->idle_conns);
2559 free(s->priv_conns);
2560 free(s->safe_conns);
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002561 free(s->idle_orphan_conns);
Olivier Houchardf1314812019-02-18 16:41:17 +01002562 free(s->curr_idle_thr);
Willy Tarreau17d45382016-12-22 21:16:08 +01002563
2564 if (s->use_ssl || s->check.use_ssl) {
2565 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2566 xprt_get(XPRT_SSL)->destroy_srv(s);
2567 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002568 HA_SPIN_DESTROY(&s->lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002569 free(s);
2570 s = s_next;
2571 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002572
Willy Tarreau4348fad2012-09-20 16:48:07 +02002573 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002574 /*
2575 * Zombie proxy, the listener just pretend to be up
2576 * because they still hold an opened fd.
2577 * Close it and give the listener its real state.
2578 */
2579 if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
2580 close(l->fd);
2581 l->state = LI_INIT;
2582 }
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02002583 unbind_listener(l);
2584 delete_listener(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002585 LIST_DEL(&l->by_fe);
2586 LIST_DEL(&l->by_bind);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002587 free(l->name);
2588 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002589 free(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002590 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002591
Willy Tarreau4348fad2012-09-20 16:48:07 +02002592 /* Release unused SSL configs. */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002593 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01002594 if (bind_conf->xprt->destroy_bind_conf)
2595 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002596 free(bind_conf->file);
2597 free(bind_conf->arg);
2598 LIST_DEL(&bind_conf->by_fe);
2599 free(bind_conf);
2600 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02002601
Christopher Fauletd7c91962015-04-30 11:48:27 +02002602 flt_deinit(p);
2603
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002604 free(p->desc);
2605 free(p->fwdfor_hdr_name);
2606
Willy Tarreauff011f22011-01-06 17:51:27 +01002607 free_http_req_rules(&p->http_req_rules);
Sasha Pachev218f0642014-06-16 12:05:59 -06002608 free_http_res_rules(&p->http_res_rules);
Willy Tarreau1f89b182017-11-22 16:53:53 +01002609 task_free(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002610
Willy Tarreaubafbe012017-11-24 17:34:44 +01002611 pool_destroy(p->req_cap_pool);
2612 pool_destroy(p->rsp_cap_pool);
2613 pool_destroy(p->table.pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002614
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002615 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002616 p = p->next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002617 HA_SPIN_DESTROY(&p0->lbprm.lock);
2618 HA_SPIN_DESTROY(&p0->lock);
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002619 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002620 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02002621
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002622 while (ua) {
2623 uap = ua;
2624 ua = ua->next;
2625
Willy Tarreaua534fea2008-08-03 12:19:50 +02002626 free(uap->uri_prefix);
2627 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002628 free(uap->node);
2629 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002630
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002631 userlist_free(uap->userlist);
Willy Tarreauff011f22011-01-06 17:51:27 +01002632 free_http_req_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002633
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002634 free(uap);
2635 }
2636
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002637 userlist_free(userlist);
2638
David Carlier834cb2e2015-09-25 12:02:25 +01002639 cfg_unregister_sections();
2640
Christopher Faulet0132d062017-07-26 15:33:35 +02002641 deinit_log_buffers();
David Carlier834cb2e2015-09-25 12:02:25 +01002642
Willy Tarreaudd815982007-10-16 12:25:14 +02002643 protocol_unbind_all();
2644
Willy Tarreau05554e62016-12-21 20:46:26 +01002645 list_for_each_entry(pdf, &post_deinit_list, list)
2646 pdf->fct();
2647
Joe Williamsdf5b38f2010-12-29 17:05:48 +01002648 free(global.log_send_hostname); global.log_send_hostname = NULL;
Dragan Dosen43885c72015-10-01 13:18:13 +02002649 chunk_destroy(&global.log_tag);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002650 free(global.chroot); global.chroot = NULL;
2651 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002652 free(global.node); global.node = NULL;
2653 free(global.desc); global.desc = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002654 free(oldpids); oldpids = NULL;
Willy Tarreau1f89b182017-11-22 16:53:53 +01002655 task_free(global_listener_queue_task); global_listener_queue_task = NULL;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01002656 task_free(idle_conn_task);
2657 idle_conn_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002658
William Lallemand0f99e342011-10-12 17:50:54 +02002659 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
2660 LIST_DEL(&log->list);
2661 free(log);
2662 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01002663 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02002664 free(wl->s);
Willy Tarreau477ecd82010-01-03 21:12:30 +01002665 LIST_DEL(&wl->list);
2666 free(wl);
2667 }
2668
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002669 list_for_each_entry_safe(bol, bolb, &build_opts_list, list) {
2670 if (bol->must_free)
2671 free((void *)bol->str);
2672 LIST_DEL(&bol->list);
2673 free(bol);
2674 }
2675
Christopher Fauletff2613e2016-11-09 11:36:17 +01002676 vars_prune(&global.vars, NULL, NULL);
Willy Tarreau2455ceb2018-11-26 15:57:34 +01002677 pool_destroy_all();
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002678 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002679} /* end deinit() */
2680
William Lallemand72160322018-11-06 17:37:16 +01002681
2682
2683/* This is a wrapper for the sockpair FD, It tests if the socket received an
2684 * EOF, if not, it calls listener_accept */
2685void mworker_accept_wrapper(int fd)
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002686{
2687 char c;
William Lallemand72160322018-11-06 17:37:16 +01002688 int ret;
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002689
William Lallemand72160322018-11-06 17:37:16 +01002690 while (1) {
2691 ret = recv(fd, &c, 1, MSG_PEEK);
2692 if (ret == -1) {
2693 if (errno == EINTR)
2694 continue;
2695 if (errno == EAGAIN) {
2696 fd_cant_recv(fd);
2697 return;
2698 }
2699 break;
2700 } else if (ret > 0) {
2701 listener_accept(fd);
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002702 return;
William Lallemand72160322018-11-06 17:37:16 +01002703 } else if (ret == 0) {
2704 /* At this step the master is down before
2705 * this worker perform a 'normal' exit.
2706 * So we want to exit with an error but
2707 * other threads could currently process
2708 * some stuff so we can't perform a clean
2709 * deinit().
2710 */
2711 exit(EXIT_FAILURE);
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002712 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002713 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002714 return;
2715}
2716
William Lallemand72160322018-11-06 17:37:16 +01002717/*
William Lallemand72160322018-11-06 17:37:16 +01002718 * This function register the accept wrapper for the sockpair of the master worker
2719 */
Willy Tarreau1605c7a2018-01-23 19:01:49 +01002720void mworker_pipe_register()
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002721{
William Lallemand72160322018-11-06 17:37:16 +01002722 /* The iocb should be already initialized with listener_accept */
William Lallemanda90cacf2018-11-07 08:38:32 +01002723 if (fdtab[proc_self->ipc_fd[1]].iocb == mworker_accept_wrapper)
2724 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002725
William Lallemandbc193052018-09-11 10:06:26 +02002726 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
Emeric Brunc8c0ed92018-10-11 15:27:07 +02002727 /* In multi-tread, we need only one thread to process
2728 * events on the pipe with master
2729 */
William Lallemande260e0d2018-11-08 12:00:14 +01002730 fd_insert(proc_self->ipc_fd[1], fdtab[proc_self->ipc_fd[1]].owner, mworker_accept_wrapper, 1);
William Lallemandbc193052018-09-11 10:06:26 +02002731 fd_want_recv(proc_self->ipc_fd[1]);
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002732}
Christopher Fauletdc628a32017-10-19 11:59:44 +02002733
Willy Tarreau918ff602011-07-25 16:33:49 +02002734/* Runs the polling loop */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01002735static void run_poll_loop()
Willy Tarreau4f60f162007-04-08 16:39:58 +02002736{
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002737 int next, exp;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002738
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002739 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002740 while (1) {
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002741 /* Process a few tasks */
2742 process_runnable_tasks();
2743
William Lallemand1aab50b2018-06-07 09:46:01 +02002744 /* check if we caught some signals and process them in the
2745 first thread */
2746 if (tid == 0)
2747 signal_process_queue();
Willy Tarreau29857942009-05-10 09:01:21 +02002748
Willy Tarreau58b458d2008-06-29 22:40:23 +02002749 /* Check if we can expire some tasks */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002750 next = wake_expired_tasks();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002751
Willy Tarreau85c459d2018-08-02 10:54:31 +02002752 /* stop when there's nothing left to do */
William Lallemanda7199262018-11-16 16:57:20 +01002753 if ((jobs - unstoppable_jobs) == 0)
Willy Tarreau85c459d2018-08-02 10:54:31 +02002754 break;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002755
Willy Tarreau10146c92015-04-13 20:44:19 +02002756 /* expire immediately if events are pending */
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002757 exp = now_ms;
Christopher Faulet32467fe2018-01-15 12:16:34 +01002758 if (fd_cache_mask & tid_bit)
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002759 activity[tid].wake_cache++;
2760 else if (active_tasks_mask & tid_bit)
2761 activity[tid].wake_tasks++;
William Lallemand1aab50b2018-06-07 09:46:01 +02002762 else if (signal_queue_len && tid == 0)
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002763 activity[tid].wake_signal++;
Olivier Houchard79321b92018-07-26 17:55:11 +02002764 else {
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002765 _HA_ATOMIC_OR(&sleeping_thread_mask, tid_bit);
2766 __ha_barrier_atomic_store();
Olivier Houchard79321b92018-07-26 17:55:11 +02002767 if (active_tasks_mask & tid_bit) {
2768 activity[tid].wake_tasks++;
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002769 _HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
Olivier Houchard79321b92018-07-26 17:55:11 +02002770 } else
2771 exp = next;
2772 }
Willy Tarreau10146c92015-04-13 20:44:19 +02002773
Willy Tarreau58b458d2008-06-29 22:40:23 +02002774 /* The poller will ensure it returns around <next> */
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002775 cur_poller.poll(&cur_poller, exp);
Olivier Houchard79321b92018-07-26 17:55:11 +02002776 if (sleeping_thread_mask & tid_bit)
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002777 _HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
Willy Tarreau033cd9d2014-01-25 19:24:15 +01002778 fd_process_cached_events();
Emeric Brun64cc49c2017-10-03 14:46:45 +02002779
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002780 activity[tid].loops++;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002781 }
2782}
2783
Christopher Faulet1d17c102017-08-29 15:38:48 +02002784static void *run_thread_poll_loop(void *data)
2785{
2786 struct per_thread_init_fct *ptif;
2787 struct per_thread_deinit_fct *ptdf;
Christopher Fauletda18b9d2018-01-25 16:10:16 +01002788 __decl_hathreads(static HA_SPINLOCK_T start_lock);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002789
Willy Tarreau0c026f42018-08-01 19:12:20 +02002790 ha_set_tid(*((unsigned int *)data));
Christopher Faulet1d17c102017-08-29 15:38:48 +02002791 tv_update_date(-1,-1);
2792
2793 list_for_each_entry(ptif, &per_thread_init_list, list) {
2794 if (!ptif->fct()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002795 ha_alert("failed to initialize thread %u.\n", tid);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002796 exit(1);
2797 }
2798 }
2799
William Lallemandbc193052018-09-11 10:06:26 +02002800 if ((global.mode & MODE_MWORKER) && master == 0) {
Willy Tarreau1605c7a2018-01-23 19:01:49 +01002801 HA_SPIN_LOCK(START_LOCK, &start_lock);
2802 mworker_pipe_register();
2803 HA_SPIN_UNLOCK(START_LOCK, &start_lock);
2804 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002805
2806 protocol_enable_all();
Christopher Faulet1d17c102017-08-29 15:38:48 +02002807 run_poll_loop();
2808
2809 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
2810 ptdf->fct();
2811
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002812#ifdef USE_THREAD
Olivier Houchardb23a61f2019-03-08 18:51:17 +01002813 _HA_ATOMIC_AND(&all_threads_mask, ~tid_bit);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002814 if (tid > 0)
2815 pthread_exit(NULL);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002816#endif
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002817 return NULL;
2818}
Christopher Faulet1d17c102017-08-29 15:38:48 +02002819
Willy Tarreaue9b26022011-08-01 20:57:55 +02002820/* This is the global management task for listeners. It enables listeners waiting
2821 * for global resources when there are enough free resource, or at least once in
2822 * a while. It is designed to be called as a task.
2823 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002824static struct task *manage_global_listener_queue(struct task *t, void *context, unsigned short state)
Willy Tarreaue9b26022011-08-01 20:57:55 +02002825{
2826 int next = TICK_ETERNITY;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002827 /* queue is empty, nothing to do */
2828 if (LIST_ISEMPTY(&global_listener_queue))
2829 goto out;
2830
2831 /* If there are still too many concurrent connections, let's wait for
2832 * some of them to go away. We don't need to re-arm the timer because
2833 * each of them will scan the queue anyway.
2834 */
2835 if (unlikely(actconn >= global.maxconn))
2836 goto out;
2837
2838 /* We should periodically try to enable listeners waiting for a global
2839 * resource here, because it is possible, though very unlikely, that
2840 * they have been blocked by a temporary lack of global resource such
2841 * as a file descriptor or memory and that the temporary condition has
2842 * disappeared.
2843 */
Willy Tarreauabacc2c2011-09-07 14:26:33 +02002844 dequeue_all_listeners(&global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +02002845
2846 out:
2847 t->expire = next;
2848 task_queue(t);
2849 return t;
2850}
Willy Tarreau4f60f162007-04-08 16:39:58 +02002851
Willy Tarreaubaaee002006-06-26 02:48:02 +02002852int main(int argc, char **argv)
2853{
2854 int err, retry;
2855 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02002856 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02002857 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002858
Olivier Houchard5fa300d2018-02-03 15:15:21 +01002859 setvbuf(stdout, NULL, _IONBF, 0);
Willy Tarreau5794fb02018-11-25 18:43:29 +01002860
Willy Tarreauff9c9142019-02-07 10:39:36 +01002861 /* this can only safely be done here, though it's optimized away by
2862 * the compiler.
2863 */
2864 if (MAX_PROCS < 1 || MAX_PROCS > LONGBITS) {
2865 ha_alert("MAX_PROCS value must be between 1 and %d inclusive; "
2866 "HAProxy was built with value %d, please fix it and rebuild.\n",
2867 LONGBITS, MAX_PROCS);
2868 exit(1);
2869 }
2870
Willy Tarreaubf696402019-03-01 10:09:28 +01002871 /* take a copy of initial limits before we possibly change them */
2872 getrlimit(RLIMIT_NOFILE, &limit);
2873 rlim_fd_cur_at_boot = limit.rlim_cur;
2874 rlim_fd_max_at_boot = limit.rlim_max;
2875
Willy Tarreau5794fb02018-11-25 18:43:29 +01002876 /* process all initcalls in order of potential dependency */
2877 RUN_INITCALLS(STG_PREPARE);
2878 RUN_INITCALLS(STG_LOCK);
2879 RUN_INITCALLS(STG_ALLOC);
2880 RUN_INITCALLS(STG_POOL);
2881 RUN_INITCALLS(STG_REGISTER);
2882 RUN_INITCALLS(STG_INIT);
2883
Emeric Bruncf20bf12010-10-22 16:06:11 +02002884 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002885 signal_register_fct(SIGQUIT, dump, SIGQUIT);
2886 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
2887 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
William Lallemand73b85e72017-06-01 17:38:51 +02002888 signal_register_fct(SIGUSR2, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002889
Willy Tarreaue437c442010-03-17 18:02:46 +01002890 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
2891 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
2892 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002893 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002894 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002895
Willy Tarreaudc23a922011-02-16 11:10:36 +01002896 /* ulimits */
2897 if (!global.rlimit_nofile)
2898 global.rlimit_nofile = global.maxsock;
2899
2900 if (global.rlimit_nofile) {
Willy Tarreaue5cfdac2019-03-01 10:32:05 +01002901 limit.rlim_cur = global.rlimit_nofile;
2902 limit.rlim_max = MAX(rlim_fd_max_at_boot, limit.rlim_cur);
2903
Willy Tarreaudc23a922011-02-16 11:10:36 +01002904 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
Willy Tarreauef635472016-06-21 11:48:18 +02002905 /* try to set it to the max possible at least */
2906 getrlimit(RLIMIT_NOFILE, &limit);
Willy Tarreau164dd0b2016-06-21 11:51:59 +02002907 limit.rlim_cur = limit.rlim_max;
2908 if (setrlimit(RLIMIT_NOFILE, &limit) != -1)
2909 getrlimit(RLIMIT_NOFILE, &limit);
2910
Christopher Faulet767a84b2017-11-24 16:50:31 +01002911 ha_warning("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n", argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
Willy Tarreauef635472016-06-21 11:48:18 +02002912 global.rlimit_nofile = limit.rlim_cur;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002913 }
2914 }
2915
2916 if (global.rlimit_memmax) {
2917 limit.rlim_cur = limit.rlim_max =
Willy Tarreau70060452015-12-14 12:46:07 +01002918 global.rlimit_memmax * 1048576ULL;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002919#ifdef RLIMIT_AS
2920 if (setrlimit(RLIMIT_AS, &limit) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002921 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2922 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01002923 }
2924#else
2925 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002926 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2927 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01002928 }
2929#endif
2930 }
2931
Olivier Houchardf73629d2017-04-05 22:33:04 +02002932 if (old_unixsocket) {
William Lallemand85b0bd92017-06-01 17:38:53 +02002933 if (strcmp("/dev/null", old_unixsocket) != 0) {
2934 if (get_old_sockets(old_unixsocket) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002935 ha_alert("Failed to get the sockets from the old process!\n");
William Lallemand85b0bd92017-06-01 17:38:53 +02002936 if (!(global.mode & MODE_MWORKER))
2937 exit(1);
2938 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002939 }
2940 }
William Lallemand85b0bd92017-06-01 17:38:53 +02002941 get_cur_unixsocket();
2942
Willy Tarreaubaaee002006-06-26 02:48:02 +02002943 /* We will loop at most 100 times with 10 ms delay each time.
2944 * That's at most 1 second. We only send a signal to old pids
2945 * if we cannot grab at least one port.
2946 */
2947 retry = MAX_START_RETRIES;
2948 err = ERR_NONE;
2949 while (retry >= 0) {
2950 struct timeval w;
2951 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01002952 /* exit the loop on no error or fatal error */
2953 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002954 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02002955 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002956 break;
2957
2958 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
2959 * listening sockets. So on those platforms, it would be wiser to
2960 * simply send SIGUSR1, which will not be undoable.
2961 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02002962 if (tell_old_pids(SIGTTOU) == 0) {
2963 /* no need to wait if we can't contact old pids */
2964 retry = 0;
2965 continue;
2966 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002967 /* give some time to old processes to stop listening */
2968 w.tv_sec = 0;
2969 w.tv_usec = 10*1000;
2970 select(0, NULL, NULL, NULL, &w);
2971 retry--;
2972 }
2973
2974 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01002975 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02002976 if (retry != MAX_START_RETRIES && nb_oldpids) {
2977 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002978 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02002979 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002980 exit(1);
2981 }
2982
William Lallemand944e6192018-11-21 15:48:31 +01002983 if (!(global.mode & MODE_MWORKER_WAIT) && listeners == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002984 ha_alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002985 /* Note: we don't have to send anything to the old pids because we
2986 * never stopped them. */
2987 exit(1);
2988 }
2989
Emeric Bruncf20bf12010-10-22 16:06:11 +02002990 err = protocol_bind_all(errmsg, sizeof(errmsg));
2991 if ((err & ~ERR_WARN) != ERR_NONE) {
2992 if ((err & ERR_ALERT) || (err & ERR_WARN))
Christopher Faulet767a84b2017-11-24 16:50:31 +01002993 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002994
Christopher Faulet767a84b2017-11-24 16:50:31 +01002995 ha_alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002996 protocol_unbind_all(); /* cleanup everything we can */
2997 if (nb_oldpids)
2998 tell_old_pids(SIGTTIN);
2999 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02003000 } else if (err & ERR_WARN) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003001 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02003002 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02003003 /* Ok, all listener should now be bound, close any leftover sockets
3004 * the previous process gave us, we don't need them anymore
3005 */
3006 while (xfer_sock_list != NULL) {
3007 struct xfer_sock_list *tmpxfer = xfer_sock_list->next;
3008 close(xfer_sock_list->fd);
3009 free(xfer_sock_list->iface);
3010 free(xfer_sock_list->namespace);
3011 free(xfer_sock_list);
3012 xfer_sock_list = tmpxfer;
3013 }
Willy Tarreaudd815982007-10-16 12:25:14 +02003014
Willy Tarreaubaaee002006-06-26 02:48:02 +02003015 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02003016 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
3017 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003018
Willy Tarreaubaaee002006-06-26 02:48:02 +02003019 /* MODE_QUIET can inhibit alerts and warnings below this line */
3020
PiBa-NL149a81a2017-12-25 21:03:31 +01003021 if (getenv("HAPROXY_MWORKER_REEXEC") != NULL) {
3022 /* either stdin/out/err are already closed or should stay as they are. */
3023 if ((global.mode & MODE_DAEMON)) {
3024 /* daemon mode re-executing, stdin/stdout/stderr are already closed so keep quiet */
3025 global.mode &= ~MODE_VERBOSE;
3026 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
3027 }
3028 } else {
3029 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
3030 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01003031 stdio_quiet(-1);
PiBa-NL149a81a2017-12-25 21:03:31 +01003032 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003033 }
3034
3035 /* open log & pid files before the chroot */
William Lallemand80293002017-11-06 11:00:03 +01003036 if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003037 unlink(global.pidfile);
3038 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
3039 if (pidfd < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003040 ha_alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003041 if (nb_oldpids)
3042 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02003043 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02003044 exit(1);
3045 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003046 }
3047
Willy Tarreaub38651a2007-03-24 17:24:39 +01003048 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003049 ha_alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
3050 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02003051 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01003052 exit(1);
3053 }
3054
Willy Tarreau4e30ed72009-02-04 18:02:48 +01003055 /* If the user is not root, we'll still let him try the configuration
3056 * but we inform him that unexpected behaviour may occur.
3057 */
3058 if ((global.last_checks & LSTCHK_NETADM) && getuid())
Christopher Faulet767a84b2017-11-24 16:50:31 +01003059 ha_warning("[%s.main()] Some options which require full privileges"
3060 " might not work well.\n"
3061 "", argv[0]);
Willy Tarreau4e30ed72009-02-04 18:02:48 +01003062
William Lallemand095ba4c2017-06-01 17:38:50 +02003063 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
3064
3065 /* chroot if needed */
3066 if (global.chroot != NULL) {
3067 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003068 ha_alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02003069 if (nb_oldpids)
3070 tell_old_pids(SIGTTIN);
3071 protocol_unbind_all();
3072 exit(1);
3073 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02003074 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02003075 }
3076
William Lallemand944e6192018-11-21 15:48:31 +01003077 if (nb_oldpids && !(global.mode & MODE_MWORKER_WAIT))
Willy Tarreaubb545b42010-08-25 12:58:59 +02003078 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003079
William Lallemand8a361b52017-06-20 11:20:33 +02003080 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) {
3081 nb_oldpids = 0;
3082 free(oldpids);
3083 oldpids = NULL;
3084 }
3085
3086
Willy Tarreaubaaee002006-06-26 02:48:02 +02003087 /* Note that any error at this stage will be fatal because we will not
3088 * be able to restart the old pids.
3089 */
3090
William Lallemand095ba4c2017-06-01 17:38:50 +02003091 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
3092 /* setgid / setuid */
3093 if (global.gid) {
3094 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003095 ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
3096 " without 'uid'/'user' is generally useless.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02003097
3098 if (setgid(global.gid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003099 ha_alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
William Lallemand095ba4c2017-06-01 17:38:50 +02003100 protocol_unbind_all();
3101 exit(1);
3102 }
3103 }
Michael Schererab012dd2013-01-12 18:35:19 +01003104
William Lallemand095ba4c2017-06-01 17:38:50 +02003105 if (global.uid && setuid(global.uid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003106 ha_alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Michael Schererab012dd2013-01-12 18:35:19 +01003107 protocol_unbind_all();
3108 exit(1);
3109 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003110 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003111 /* check ulimits */
3112 limit.rlim_cur = limit.rlim_max = 0;
3113 getrlimit(RLIMIT_NOFILE, &limit);
3114 if (limit.rlim_cur < global.maxsock) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003115 ha_warning("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. Please raise 'ulimit-n' to %d or more to avoid any trouble.\n",
3116 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003117 }
3118
William Lallemand944e6192018-11-21 15:48:31 +01003119 if (global.mode & (MODE_DAEMON | MODE_MWORKER | MODE_MWORKER_WAIT)) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003120 struct proxy *px;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003121 struct peers *curpeers;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003122 int ret = 0;
3123 int proc;
William Lallemande1340412017-12-28 16:09:36 +01003124 int devnullfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003125
William Lallemand095ba4c2017-06-01 17:38:50 +02003126 /*
3127 * if daemon + mworker: must fork here to let a master
3128 * process live in background before forking children
3129 */
William Lallemand73b85e72017-06-01 17:38:51 +02003130
3131 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
3132 && (global.mode & MODE_MWORKER)
3133 && (global.mode & MODE_DAEMON)) {
William Lallemand095ba4c2017-06-01 17:38:50 +02003134 ret = fork();
3135 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003136 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02003137 protocol_unbind_all();
3138 exit(1); /* there has been an error */
William Lallemandbfd8eb52018-07-04 15:31:23 +02003139 } else if (ret > 0) { /* parent leave to daemonize */
William Lallemand095ba4c2017-06-01 17:38:50 +02003140 exit(0);
William Lallemandbfd8eb52018-07-04 15:31:23 +02003141 } else /* change the process group ID in the child (master process) */
3142 setsid();
William Lallemand095ba4c2017-06-01 17:38:50 +02003143 }
William Lallemande20b6a62017-06-01 17:38:55 +02003144
William Lallemande20b6a62017-06-01 17:38:55 +02003145
William Lallemanddeed7802017-11-06 11:00:04 +01003146 /* if in master-worker mode, write the PID of the father */
3147 if (global.mode & MODE_MWORKER) {
3148 char pidstr[100];
3149 snprintf(pidstr, sizeof(pidstr), "%d\n", getpid());
Willy Tarreau46ec48b2018-01-23 19:20:19 +01003150 if (pidfd >= 0)
3151 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
William Lallemanddeed7802017-11-06 11:00:04 +01003152 }
3153
Willy Tarreaubaaee002006-06-26 02:48:02 +02003154 /* the father launches the required number of processes */
William Lallemand944e6192018-11-21 15:48:31 +01003155 if (!(global.mode & MODE_MWORKER_WAIT)) {
3156 children = calloc(global.nbproc, sizeof(int));
3157 for (proc = 0; proc < global.nbproc; proc++) {
3158 ret = fork();
3159 if (ret < 0) {
3160 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
3161 protocol_unbind_all();
3162 exit(1); /* there has been an error */
3163 }
3164 else if (ret == 0) /* child breaks here */
3165 break;
3166 children[proc] = ret;
3167 if (pidfd >= 0 && !(global.mode & MODE_MWORKER)) {
3168 char pidstr[100];
3169 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
3170 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
3171 }
3172 if (global.mode & MODE_MWORKER) {
3173 struct mworker_proc *child;
William Lallemandce83b4a2018-10-26 14:47:30 +02003174
William Lallemand220567e2018-11-21 18:04:53 +01003175 ha_notice("New worker #%d (%d) forked\n", relative_pid, ret);
William Lallemand944e6192018-11-21 15:48:31 +01003176 /* find the right mworker_proc */
3177 list_for_each_entry(child, &proc_list, list) {
3178 if (child->relative_pid == relative_pid &&
3179 child->reloads == 0) {
3180 child->timestamp = now.tv_sec;
3181 child->pid = ret;
3182 break;
3183 }
William Lallemandce83b4a2018-10-26 14:47:30 +02003184 }
3185 }
William Lallemandbc193052018-09-11 10:06:26 +02003186
William Lallemand944e6192018-11-21 15:48:31 +01003187 relative_pid++; /* each child will get a different one */
3188 pid_bit <<= 1;
3189 }
3190 } else {
3191 /* wait mode */
3192 global.nbproc = 1;
3193 proc = 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003194 }
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003195
3196#ifdef USE_CPU_AFFINITY
3197 if (proc < global.nbproc && /* child */
Willy Tarreauff9c9142019-02-07 10:39:36 +01003198 proc < MAX_PROCS && /* only the first 32/64 processes may be pinned */
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003199 global.cpu_map.proc[proc]) /* only do this if the process has a CPU map */
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003200#ifdef __FreeBSD__
Olivier Houchard97148f62017-08-16 17:29:11 +02003201 {
3202 cpuset_t cpuset;
3203 int i;
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003204 unsigned long cpu_map = global.cpu_map.proc[proc];
Olivier Houchard97148f62017-08-16 17:29:11 +02003205
3206 CPU_ZERO(&cpuset);
3207 while ((i = ffsl(cpu_map)) > 0) {
3208 CPU_SET(i - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003209 cpu_map &= ~(1UL << (i - 1));
Olivier Houchard97148f62017-08-16 17:29:11 +02003210 }
3211 ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset);
3212 }
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003213#else
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003214 sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map.proc[proc]);
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003215#endif
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003216#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003217 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02003218 if (pidfd >= 0) {
3219 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
3220 close(pidfd);
3221 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02003222
3223 /* We won't ever use this anymore */
Willy Tarreaud137dd32010-08-25 12:49:05 +02003224 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003225
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003226 if (proc == global.nbproc) {
William Lallemand944e6192018-11-21 15:48:31 +01003227 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003228
3229 if ((!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
3230 (global.mode & MODE_DAEMON)) {
3231 /* detach from the tty, this is required to properly daemonize. */
William Lallemande1340412017-12-28 16:09:36 +01003232 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL))
3233 stdio_quiet(-1);
3234
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003235 global.mode &= ~MODE_VERBOSE;
3236 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003237 }
3238
William Lallemandb3f2be32018-09-11 10:06:18 +02003239 mworker_loop();
William Lallemand1499b9b2017-06-07 15:04:47 +02003240 /* should never get there */
3241 exit(EXIT_FAILURE);
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003242 }
William Lallemandcf4e4962017-06-08 19:05:48 +02003243#if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH)
Grant Zhang872f9c22017-01-21 01:10:18 +00003244 ssl_free_dh();
3245#endif
William Lallemand1499b9b2017-06-07 15:04:47 +02003246 exit(0); /* parent must leave */
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003247 }
3248
William Lallemandcb11fd22017-06-01 17:38:52 +02003249 /* child must never use the atexit function */
3250 atexit_flag = 0;
3251
William Lallemandbc193052018-09-11 10:06:26 +02003252 /* close useless master sockets */
3253 if (global.mode & MODE_MWORKER) {
3254 struct mworker_proc *child, *it;
3255 master = 0;
3256
William Lallemand309dc9a2018-10-26 14:47:45 +02003257 mworker_cli_proxy_stop();
3258
William Lallemandbc193052018-09-11 10:06:26 +02003259 /* free proc struct of other processes */
3260 list_for_each_entry_safe(child, it, &proc_list, list) {
William Lallemandce83b4a2018-10-26 14:47:30 +02003261 /* close the FD of the master side for all
3262 * workers, we don't need to close the worker
3263 * side of other workers since it's done with
3264 * the bind_proc */
Tim Duesterhus742e0f92018-11-25 20:03:39 +01003265 if (child->ipc_fd[0] >= 0)
3266 close(child->ipc_fd[0]);
William Lallemandce83b4a2018-10-26 14:47:30 +02003267 if (child->relative_pid == relative_pid &&
3268 child->reloads == 0) {
3269 /* keep this struct if this is our pid */
3270 proc_self = child;
William Lallemandbc193052018-09-11 10:06:26 +02003271 continue;
William Lallemandce83b4a2018-10-26 14:47:30 +02003272 }
William Lallemandbc193052018-09-11 10:06:26 +02003273 LIST_DEL(&child->list);
3274 free(child);
3275 }
3276 }
Willy Tarreau1605c7a2018-01-23 19:01:49 +01003277
William Lallemande1340412017-12-28 16:09:36 +01003278 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
3279 devnullfd = open("/dev/null", O_RDWR, 0);
3280 if (devnullfd < 0) {
3281 ha_alert("Cannot open /dev/null\n");
3282 exit(EXIT_FAILURE);
3283 }
3284 }
3285
William Lallemand095ba4c2017-06-01 17:38:50 +02003286 /* Must chroot and setgid/setuid in the children */
3287 /* chroot if needed */
3288 if (global.chroot != NULL) {
3289 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003290 ha_alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02003291 if (nb_oldpids)
3292 tell_old_pids(SIGTTIN);
3293 protocol_unbind_all();
3294 exit(1);
3295 }
3296 }
3297
3298 free(global.chroot);
3299 global.chroot = NULL;
3300
3301 /* setgid / setuid */
3302 if (global.gid) {
3303 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003304 ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
3305 " without 'uid'/'user' is generally useless.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02003306
3307 if (setgid(global.gid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003308 ha_alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
William Lallemand095ba4c2017-06-01 17:38:50 +02003309 protocol_unbind_all();
3310 exit(1);
3311 }
3312 }
3313
3314 if (global.uid && setuid(global.uid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003315 ha_alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
William Lallemand095ba4c2017-06-01 17:38:50 +02003316 protocol_unbind_all();
3317 exit(1);
3318 }
3319
William Lallemand7f80eb22017-05-26 18:19:55 +02003320 /* pass through every cli socket, and check if it's bound to
3321 * the current process and if it exposes listeners sockets.
3322 * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork.
3323 * */
3324
3325 if (global.stats_fe) {
3326 struct bind_conf *bind_conf;
3327
3328 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
3329 if (bind_conf->level & ACCESS_FD_LISTENERS) {
3330 if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) {
3331 global.tune.options |= GTUNE_SOCKET_TRANSFER;
3332 break;
3333 }
3334 }
3335 }
3336 }
3337
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003338 /* we might have to unbind some proxies from some processes */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003339 px = proxies_list;
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003340 while (px != NULL) {
3341 if (px->bind_proc && px->state != PR_STSTOPPED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02003342 if (!(px->bind_proc & (1UL << proc))) {
3343 if (global.tune.options & GTUNE_SOCKET_TRANSFER)
3344 zombify_proxy(px);
3345 else
3346 stop_proxy(px);
3347 }
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003348 }
3349 px = px->next;
3350 }
3351
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003352 /* we might have to unbind some peers sections from some processes */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003353 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003354 if (!curpeers->peers_fe)
3355 continue;
3356
3357 if (curpeers->peers_fe->bind_proc & (1UL << proc))
3358 continue;
3359
3360 stop_proxy(curpeers->peers_fe);
3361 /* disable this peer section so that it kills itself */
Willy Tarreau47c8c022015-09-28 16:39:25 +02003362 signal_unregister_handler(curpeers->sighandler);
3363 task_delete(curpeers->sync_task);
3364 task_free(curpeers->sync_task);
3365 curpeers->sync_task = NULL;
3366 task_free(curpeers->peers_fe->task);
3367 curpeers->peers_fe->task = NULL;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003368 curpeers->peers_fe = NULL;
3369 }
3370
William Lallemand2e8fad92018-11-13 16:18:23 +01003371 /*
3372 * This is only done in daemon mode because we might want the
3373 * logs on stdout in mworker mode. If we're NOT in QUIET mode,
3374 * we should now close the 3 first FDs to ensure that we can
3375 * detach from the TTY. We MUST NOT do it in other cases since
3376 * it would have already be done, and 0-2 would have been
3377 * affected to listening sockets
Willy Tarreaubaaee002006-06-26 02:48:02 +02003378 */
William Lallemand2e8fad92018-11-13 16:18:23 +01003379 if ((global.mode & MODE_DAEMON) &&
3380 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003381 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01003382 stdio_quiet(devnullfd);
Willy Tarreau106cb762008-11-16 07:40:34 +01003383 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003384 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
3385 }
3386 pid = getpid(); /* update child's pid */
William Lallemandbfd8eb52018-07-04 15:31:23 +02003387 if (!(global.mode & MODE_MWORKER)) /* in mworker mode we don't want a new pgid for the children */
3388 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02003389 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02003390 }
3391
Christopher Faulete3a5e352017-10-24 13:53:54 +02003392 global.mode &= ~MODE_STARTING;
Willy Tarreau4f60f162007-04-08 16:39:58 +02003393 /*
3394 * That's it : the central polling loop. Run until we stop.
3395 */
Christopher Faulet1d17c102017-08-29 15:38:48 +02003396#ifdef USE_THREAD
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003397 {
Christopher Faulet1d17c102017-08-29 15:38:48 +02003398 unsigned int *tids = calloc(global.nbthread, sizeof(unsigned int));
3399 pthread_t *threads = calloc(global.nbthread, sizeof(pthread_t));
3400 int i;
William Lallemand1aab50b2018-06-07 09:46:01 +02003401 sigset_t blocked_sig, old_sig;
Christopher Faulet1d17c102017-08-29 15:38:48 +02003402
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003403 /* Init tids array */
3404 for (i = 0; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02003405 tids[i] = i;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003406
William Lallemand1aab50b2018-06-07 09:46:01 +02003407 /* ensure the signals will be blocked in every thread */
3408 sigfillset(&blocked_sig);
3409 sigdelset(&blocked_sig, SIGPROF);
3410 sigdelset(&blocked_sig, SIGBUS);
3411 sigdelset(&blocked_sig, SIGFPE);
3412 sigdelset(&blocked_sig, SIGILL);
3413 sigdelset(&blocked_sig, SIGSEGV);
3414 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
3415
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003416 /* Create nbthread-1 thread. The first thread is the current process */
3417 threads[0] = pthread_self();
3418 for (i = 1; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02003419 pthread_create(&threads[i], NULL, &run_thread_poll_loop, &tids[i]);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003420
Christopher Faulet62519022017-10-16 15:49:32 +02003421#ifdef USE_CPU_AFFINITY
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003422 /* Now the CPU affinity for all threads */
3423 for (i = 0; i < global.nbthread; i++) {
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003424 if (global.cpu_map.proc[relative_pid-1])
3425 global.cpu_map.thread[relative_pid-1][i] &= global.cpu_map.proc[relative_pid-1];
Christopher Faulet62519022017-10-16 15:49:32 +02003426
Willy Tarreau421f02e2018-01-20 18:19:22 +01003427 if (i < MAX_THREADS && /* only the first 32/64 threads may be pinned */
Olivier Houchard829aa242017-12-01 18:19:43 +01003428 global.cpu_map.thread[relative_pid-1][i]) {/* only do this if the thread has a THREAD map */
3429#if defined(__FreeBSD__) || defined(__NetBSD__)
3430 cpuset_t cpuset;
3431#else
3432 cpu_set_t cpuset;
3433#endif
3434 int j;
3435 unsigned long cpu_map = global.cpu_map.thread[relative_pid-1][i];
3436
3437 CPU_ZERO(&cpuset);
3438
3439 while ((j = ffsl(cpu_map)) > 0) {
3440 CPU_SET(j - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003441 cpu_map &= ~(1UL << (j - 1));
Olivier Houchard829aa242017-12-01 18:19:43 +01003442 }
Christopher Faulet62519022017-10-16 15:49:32 +02003443 pthread_setaffinity_np(threads[i],
Olivier Houchard829aa242017-12-01 18:19:43 +01003444 sizeof(cpuset), &cpuset);
3445 }
Christopher Faulet1d17c102017-08-29 15:38:48 +02003446 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003447#endif /* !USE_CPU_AFFINITY */
3448
William Lallemand1aab50b2018-06-07 09:46:01 +02003449 /* when multithreading we need to let only the thread 0 handle the signals */
William Lallemandd3801c12018-09-11 10:06:23 +02003450 haproxy_unblock_signals();
William Lallemand1aab50b2018-06-07 09:46:01 +02003451
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003452 /* Finally, start the poll loop for the first thread */
3453 run_thread_poll_loop(&tids[0]);
3454
3455 /* Wait the end of other threads */
3456 for (i = 1; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02003457 pthread_join(threads[i], NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003458
Christopher Faulet1d17c102017-08-29 15:38:48 +02003459 free(tids);
3460 free(threads);
3461
Christopher Fauletb79a94c2017-05-30 15:34:30 +02003462#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
3463 show_lock_stats();
3464#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003465 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003466#else /* ! USE_THREAD */
William Lallemandd3801c12018-09-11 10:06:23 +02003467 haproxy_unblock_signals();
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003468 run_thread_poll_loop((int []){0});
Christopher Faulet62519022017-10-16 15:49:32 +02003469
Christopher Faulet62519022017-10-16 15:49:32 +02003470#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003471
3472 /* Do some cleanup */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003473 deinit();
Christopher Faulet1d17c102017-08-29 15:38:48 +02003474
Willy Tarreaubaaee002006-06-26 02:48:02 +02003475 exit(0);
3476}
3477
3478
3479/*
3480 * Local variables:
3481 * c-indent-level: 8
3482 * c-basic-offset: 8
3483 * End:
3484 */