blob: 2c42a7096a353250592c7412762e70a910366d00 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HA-Proxy : High Availability-enabled HTTP/TCP proxy
Willy Tarreau65e94d12018-08-02 18:12:50 +02003 * Copyright 2000-2018 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 Tarreau2dd0d472006-06-29 17:53:05 +020078#include <common/memory.h>
79#include <common/mini-clist.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010080#include <common/namespace.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020081#include <common/regex.h>
82#include <common/standard.h>
83#include <common/time.h>
84#include <common/uri_auth.h>
85#include <common/version.h>
Christopher Fauletbe0faa22017-08-29 15:37:10 +020086#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020087
88#include <types/capture.h>
William Lallemandce83b4a2018-10-26 14:47:30 +020089#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020090#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020091#include <types/global.h>
Simon Hormanac821422011-07-15 13:14:09 +090092#include <types/acl.h>
Willy Tarreau3c63fd82011-09-07 18:00:47 +020093#include <types/peers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020094
Willy Tarreau0fc45a72007-06-17 00:36:03 +020095#include <proto/acl.h>
Willy Tarreau609aad92018-11-22 08:31:09 +010096#include <proto/activity.h>
Willy Tarreau2e845be2012-10-19 19:49:09 +020097#include <proto/arg.h>
Willy Tarreau3c595ac2015-04-19 09:59:31 +020098#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020099#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +0200100#include <proto/channel.h>
William Lallemandce83b4a2018-10-26 14:47:30 +0200101#include <proto/cli.h>
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200102#include <proto/connection.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +0200104#include <proto/filters.h>
Willy Tarreau34eb6712011-10-24 18:15:04 +0200105#include <proto/hdr_idx.h>
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +0100106#include <proto/hlua.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +0200107#include <proto/http_rules.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +0200108#include <proto/listener.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109#include <proto/log.h>
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100110#include <proto/pattern.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +0200111#include <proto/protocol.h>
Willy Tarreau80587432006-12-24 17:47:20 +0100112#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113#include <proto/proxy.h>
114#include <proto/queue.h>
115#include <proto/server.h>
Willy Tarreaub1ec8c42015-04-03 13:53:24 +0200116#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +0200117#include <proto/stream.h>
Willy Tarreau29857942009-05-10 09:01:21 +0200118#include <proto/signal.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119#include <proto/task.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +0200120#include <proto/dns.h>
Christopher Fauletff2613e2016-11-09 11:36:17 +0100121#include <proto/vars.h>
Willy Tarreau50bc31d2017-08-16 15:35:19 +0200122#ifdef USE_OPENSSL
Grant Zhang872f9c22017-01-21 01:10:18 +0000123#include <proto/ssl_sock.h>
Willy Tarreau50bc31d2017-08-16 15:35:19 +0200124#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125
Willy Tarreau477ecd82010-01-03 21:12:30 +0100126/* list of config files */
127static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200128int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100129int relative_pid = 1; /* process id starting at 1 */
Willy Tarreau387bd4f2017-11-10 19:08:14 +0100130unsigned long pid_bit = 1; /* bit corresponding to the process id */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131
Olivier Houchard79321b92018-07-26 17:55:11 +0200132volatile unsigned long sleeping_thread_mask; /* Threads that are about to sleep in poll() */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133/* global options */
134struct global global = {
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100135 .hard_stop_after = TICK_ETERNITY,
Willy Tarreau247a13a2012-11-15 17:38:15 +0100136 .nbproc = 1,
Christopher Fauletbe0faa22017-08-29 15:37:10 +0200137 .nbthread = 1,
William Lallemand5f232402012-04-05 18:02:55 +0200138 .req_count = 0,
William Lallemand0f99e342011-10-12 17:50:54 +0200139 .logsrvs = LIST_HEAD_INIT(global.logsrvs),
William Lallemand9d5f5482012-11-07 16:12:57 +0100140 .maxzlibmem = 0,
William Lallemandd85f9172012-11-09 17:05:39 +0100141 .comp_rate_lim = 0,
Emeric Brun850efd52014-01-29 12:24:34 +0100142 .ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED,
Emeric Bruned760922010-10-22 17:59:25 +0200143 .unix_bind = {
144 .ux = {
145 .uid = -1,
146 .gid = -1,
147 .mode = 0,
148 }
149 },
Willy Tarreau27a674e2009-08-17 07:23:33 +0200150 .tune = {
151 .bufsize = BUFSIZE,
Willy Tarreau27097842015-09-28 13:53:23 +0200152 .maxrewrite = -1,
Willy Tarreau43961d52010-10-04 20:39:20 +0200153 .chksize = BUFSIZE,
Willy Tarreaua24adf02014-11-27 01:11:56 +0100154 .reserved_bufs = RESERVED_BUFS,
Willy Tarreauf3045d22015-04-29 16:24:50 +0200155 .pattern_cache = DEFAULT_PAT_LRU_SIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200156#ifdef USE_OPENSSL
Emeric Brun46635772012-11-14 11:32:56 +0100157 .sslcachesize = SSLCACHESIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200158#endif
William Lallemandf3747832012-11-09 12:33:10 +0100159 .comp_maxlevel = 1,
Willy Tarreau7e312732014-02-12 16:35:14 +0100160#ifdef DEFAULT_IDLE_TIMER
161 .idle_timer = DEFAULT_IDLE_TIMER,
162#else
163 .idle_timer = 1000, /* 1 second */
164#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200165 },
Emeric Brun76d88952012-10-05 15:47:31 +0200166#ifdef USE_OPENSSL
167#ifdef DEFAULT_MAXSSLCONN
Willy Tarreau403edff2012-09-06 11:58:37 +0200168 .maxsslconn = DEFAULT_MAXSSLCONN,
169#endif
Emeric Brun76d88952012-10-05 15:47:31 +0200170#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200171 /* others NULL OK */
172};
173
174/*********************************************************************/
175
176int stopping; /* non zero means stopping in progress */
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100177int killed; /* non zero means a hard-stop is triggered */
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200178int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */
William Lallemanda7199262018-11-16 16:57:20 +0100179int unstoppable_jobs = 0; /* number of active jobs that can't be stopped during a soft stop */
Willy Tarreau199ad242018-11-05 16:31:22 +0100180int active_peers = 0; /* number of active peers (connection attempts and connected) */
Willy Tarreau2d372c22018-11-05 17:12:27 +0100181int connected_peers = 0; /* number of connected peers (verified ones) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182
183/* Here we store informations about the pids of the processes we may pause
184 * or kill. We will send them a signal every 10 ms until we can bind to all
185 * our ports. With 200 retries, that's about 2 seconds.
186 */
187#define MAX_START_RETRIES 200
Willy Tarreaubaaee002006-06-26 02:48:02 +0200188static int *oldpids = NULL;
189static int oldpids_sig; /* use USR1 or TERM */
190
Olivier Houchardf73629d2017-04-05 22:33:04 +0200191/* Path to the unix socket we use to retrieve listener sockets from the old process */
192static const char *old_unixsocket;
193
William Lallemand85b0bd92017-06-01 17:38:53 +0200194static char *cur_unixsocket = NULL;
195
William Lallemandcb11fd22017-06-01 17:38:52 +0200196int atexit_flag = 0;
197
William Lallemand91723742018-11-06 17:37:14 +0100198static int exitcode = -1;
199
Willy Tarreaubb545b42010-08-25 12:58:59 +0200200int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200201const int zero = 0;
202const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200203const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200204
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100205char hostname[MAX_HOSTNAME_LEN];
Emeric Brun2b920a12010-09-23 18:30:22 +0200206char localpeer[MAX_HOSTNAME_LEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200207
Willy Tarreau89efaed2013-12-13 15:14:55 +0100208/* used from everywhere just to drain results we don't want to read and which
209 * recent versions of gcc increasingly and annoyingly complain about.
210 */
211int shut_your_big_mouth_gcc_int = 0;
212
William Lallemand73b85e72017-06-01 17:38:51 +0200213int *children = NULL; /* store PIDs of children in master workers mode */
214
William Lallemand73b85e72017-06-01 17:38:51 +0200215static char **next_argv = NULL;
216
William Lallemandbc193052018-09-11 10:06:26 +0200217struct list proc_list = LIST_HEAD_INIT(proc_list);
218
219int master = 0; /* 1 if in master, 0 if in child */
220
William Lallemand16dd1b32018-11-19 18:46:18 +0100221struct mworker_proc *proc_self = NULL;
William Lallemandbc193052018-09-11 10:06:26 +0200222
Willy Tarreau08ceb102011-07-24 22:58:00 +0200223/* list of the temporarily limited listeners because of lack of resource */
224struct list global_listener_queue = LIST_HEAD_INIT(global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +0200225struct task *global_listener_queue_task;
Olivier Houchard9f6af332018-05-25 14:04:04 +0200226static struct task *manage_global_listener_queue(struct task *t, void *context, unsigned short state);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200227
William Lallemandb3f2be32018-09-11 10:06:18 +0200228static void *run_thread_poll_loop(void *data);
229
Willy Tarreauff055502014-04-28 22:27:06 +0200230/* bitfield of a few warnings to emit just once (WARN_*) */
231unsigned int warned = 0;
232
William Lallemande7361152018-10-26 14:47:36 +0200233/* master CLI configuration (-S flag) */
234struct list mworker_cli_conf = LIST_HEAD_INIT(mworker_cli_conf);
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100235
236/* These are strings to be reported in the output of "haproxy -vv". They may
237 * either be constants (in which case must_free must be zero) or dynamically
238 * allocated strings to pass to free() on exit, and in this case must_free
239 * must be non-zero.
240 */
241struct list build_opts_list = LIST_HEAD_INIT(build_opts_list);
242struct build_opts_str {
243 struct list list;
244 const char *str;
245 int must_free;
246};
247
Willy Tarreaue6945732016-12-21 19:57:00 +0100248/* These functions are called just after the point where the program exits
249 * after a config validity check, so they are generally suited for resource
250 * allocation and slow initializations that should be skipped during basic
251 * config checks. The functions must return 0 on success, or a combination
252 * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause
253 * and immediate exit, so the function must have emitted any useful error.
254 */
255struct list post_check_list = LIST_HEAD_INIT(post_check_list);
256struct post_check_fct {
257 struct list list;
258 int (*fct)();
259};
260
Willy Tarreau05554e62016-12-21 20:46:26 +0100261/* These functions are called when freeing the global sections at the end
262 * of deinit, after everything is stopped. They don't return anything, and
263 * they work in best effort mode as their sole goal is to make valgrind
264 * mostly happy.
265 */
266struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list);
267struct post_deinit_fct {
268 struct list list;
269 void (*fct)();
270};
271
Christopher Faulet415f6112017-07-25 16:52:58 +0200272/* These functions are called for each thread just after the thread creation
273 * and before running the scheduler. They should be used to do per-thread
274 * initializations. They must return 0 if an error occurred. */
275struct list per_thread_init_list = LIST_HEAD_INIT(per_thread_init_list);
276struct per_thread_init_fct {
277 struct list list;
278 int (*fct)();
279};
280
281/* These functions are called for each thread just after the scheduler loop and
282 * before exiting the thread. They don't return anything and, as for post-deinit
283 * functions, they work in best effort mode as their sole goal is to make
284 * valgrind mostly happy. */
285struct list per_thread_deinit_list = LIST_HEAD_INIT(per_thread_deinit_list);
286struct per_thread_deinit_fct {
287 struct list list;
288 void (*fct)();
289};
290
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291/*********************************************************************/
292/* general purpose functions ***************************************/
293/*********************************************************************/
294
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100295/* used to register some build option strings at boot. Set must_free to
296 * non-zero if the string must be freed upon exit.
297 */
298void hap_register_build_opts(const char *str, int must_free)
299{
300 struct build_opts_str *b;
301
302 b = calloc(1, sizeof(*b));
303 if (!b) {
304 fprintf(stderr, "out of memory\n");
305 exit(1);
306 }
307 b->str = str;
308 b->must_free = must_free;
309 LIST_ADDQ(&build_opts_list, &b->list);
310}
311
Willy Tarreaue6945732016-12-21 19:57:00 +0100312/* used to register some initialization functions to call after the checks. */
313void hap_register_post_check(int (*fct)())
314{
315 struct post_check_fct *b;
316
317 b = calloc(1, sizeof(*b));
318 if (!b) {
319 fprintf(stderr, "out of memory\n");
320 exit(1);
321 }
322 b->fct = fct;
323 LIST_ADDQ(&post_check_list, &b->list);
324}
325
Willy Tarreau05554e62016-12-21 20:46:26 +0100326/* used to register some de-initialization functions to call after everything
327 * has stopped.
328 */
329void hap_register_post_deinit(void (*fct)())
330{
331 struct post_deinit_fct *b;
332
333 b = calloc(1, sizeof(*b));
334 if (!b) {
335 fprintf(stderr, "out of memory\n");
336 exit(1);
337 }
338 b->fct = fct;
339 LIST_ADDQ(&post_deinit_list, &b->list);
340}
341
Christopher Faulet415f6112017-07-25 16:52:58 +0200342/* used to register some initialization functions to call for each thread. */
343void hap_register_per_thread_init(int (*fct)())
344{
345 struct per_thread_init_fct *b;
346
347 b = calloc(1, sizeof(*b));
348 if (!b) {
349 fprintf(stderr, "out of memory\n");
350 exit(1);
351 }
352 b->fct = fct;
353 LIST_ADDQ(&per_thread_init_list, &b->list);
354}
355
356/* used to register some de-initialization functions to call for each thread. */
357void hap_register_per_thread_deinit(void (*fct)())
358{
359 struct per_thread_deinit_fct *b;
360
361 b = calloc(1, sizeof(*b));
362 if (!b) {
363 fprintf(stderr, "out of memory\n");
364 exit(1);
365 }
366 b->fct = fct;
367 LIST_ADDQ(&per_thread_deinit_list, &b->list);
368}
369
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100370static void display_version()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200371{
372 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau65e94d12018-08-02 18:12:50 +0200373 printf("Copyright 2000-2018 Willy Tarreau <willy@haproxy.org>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200374}
375
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100376static void display_build_opts()
Willy Tarreau7b066db2007-12-02 11:28:59 +0100377{
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100378 struct build_opts_str *item;
379
Willy Tarreau7b066db2007-12-02 11:28:59 +0100380 printf("Build options :"
381#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100382 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100383#endif
384#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100385 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100386#endif
387#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100388 "\n CC = " BUILD_CC
389#endif
390#ifdef BUILD_CFLAGS
391 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100392#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100393#ifdef BUILD_OPTIONS
394 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100395#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200396 "\n\nDefault settings :"
397 "\n maxconn = %d, bufsize = %d, maxrewrite = %d, maxpollevents = %d"
398 "\n\n",
399 DEFAULT_MAXCONN, BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS);
Willy Tarreaube5b6852009-10-03 18:57:08 +0200400
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100401 list_for_each_entry(item, &build_opts_list, list) {
402 puts(item->str);
403 }
404
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100405 putchar('\n');
406
Willy Tarreaube5b6852009-10-03 18:57:08 +0200407 list_pollers(stdout);
408 putchar('\n');
Christopher Faulet98d9fe22018-04-10 14:37:32 +0200409 list_mux_proto(stdout);
410 putchar('\n');
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100411 list_filters(stdout);
412 putchar('\n');
Willy Tarreau7b066db2007-12-02 11:28:59 +0100413}
414
Willy Tarreaubaaee002006-06-26 02:48:02 +0200415/*
416 * This function prints the command line usage and exits
417 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100418static void usage(char *name)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200419{
420 display_version();
421 fprintf(stderr,
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200422 "Usage : %s [-f <cfgfile|cfgdir>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200423 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
Willy Tarreaua088d312015-10-08 11:58:48 +0200424 " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100425 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200426 " -d enters debug mode ; -db only disables background mode.\n"
Willy Tarreau6e064432012-05-08 15:40:42 +0200427 " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200429 " -D goes daemon ; -C changes to <dir> before loading files.\n"
William Lallemand095ba4c2017-06-01 17:38:50 +0200430 " -W master-worker mode.\n"
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100431#if defined(USE_SYSTEMD)
432 " -Ws master-worker mode with systemd notify support.\n"
433#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200434 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200435 " -c check mode : only check config files and exit\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200436 " -n sets the maximum total # of connections (%d)\n"
437 " -m limits the usable amount of memory (in MB)\n"
438 " -N sets the default, per-proxy maximum # of connections (%d)\n"
Emeric Brun2b920a12010-09-23 18:30:22 +0200439 " -L set local peer name (default to hostname)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440 " -p writes pids of all children to this file\n"
441#if defined(ENABLE_EPOLL)
442 " -de disables epoll() usage even when available\n"
443#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200444#if defined(ENABLE_KQUEUE)
445 " -dk disables kqueue() usage even when available\n"
446#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200447#if defined(ENABLE_POLL)
448 " -dp disables poll() usage even when available\n"
449#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200450#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100451 " -dS disables splice usage (broken on old kernels)\n"
452#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200453#if defined(USE_GETADDRINFO)
454 " -dG disables getaddrinfo() usage\n"
455#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000456#if defined(SO_REUSEPORT)
457 " -dR disables SO_REUSEPORT usage\n"
458#endif
Willy Tarreau3eed10e2016-11-07 21:03:16 +0100459 " -dr ignores server address resolution failures\n"
Emeric Brun850efd52014-01-29 12:24:34 +0100460 " -dV disables SSL verify on servers side\n"
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +0200461 " -sf/-st [pid ]* finishes/terminates old pids.\n"
Olivier Houchardf73629d2017-04-05 22:33:04 +0200462 " -x <unix_socket> get listening sockets from a unix socket\n"
William Lallemande7361152018-10-26 14:47:36 +0200463 " -S <unix_socket>[,<bind options>...] new stats socket for the master\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464 "\n",
465 name, DEFAULT_MAXCONN, cfg_maxpconn);
466 exit(1);
467}
468
469
470
471/*********************************************************************/
472/* more specific functions ***************************************/
473/*********************************************************************/
474
William Lallemand73b85e72017-06-01 17:38:51 +0200475/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
476 * pids the signal was correctly delivered to.
477 */
478static int tell_old_pids(int sig)
479{
480 int p;
481 int ret = 0;
482 for (p = 0; p < nb_oldpids; p++)
483 if (kill(oldpids[p], sig) == 0)
484 ret++;
485 return ret;
486}
487
488/* return 1 if a pid is a current child otherwise 0 */
489
490int current_child(int pid)
491{
492 int i;
493
494 for (i = 0; i < global.nbproc; i++) {
495 if (children[i] == pid)
496 return 1;
497 }
498 return 0;
499}
500
William Lallemand73b85e72017-06-01 17:38:51 +0200501static void mworker_block_signals()
502{
503 sigset_t set;
504
505 sigemptyset(&set);
506 sigaddset(&set, SIGUSR1);
507 sigaddset(&set, SIGUSR2);
508 sigaddset(&set, SIGHUP);
William Lallemandebf304f2018-09-11 10:06:21 +0200509 sigaddset(&set, SIGCHLD);
William Lallemand6e1796e2018-06-07 11:23:40 +0200510 ha_sigmask(SIG_SETMASK, &set, NULL);
William Lallemand73b85e72017-06-01 17:38:51 +0200511}
512
513static void mworker_unblock_signals()
514{
William Lallemandd3801c12018-09-11 10:06:23 +0200515 haproxy_unblock_signals();
William Lallemand73b85e72017-06-01 17:38:51 +0200516}
517
William Lallemand73b85e72017-06-01 17:38:51 +0200518/*
519 * Send signal to every known children.
520 */
521
522static void mworker_kill(int sig)
523{
524 int i;
525
526 tell_old_pids(sig);
527 if (children) {
528 for (i = 0; i < global.nbproc; i++)
529 kill(children[i], sig);
530 }
William Lallemandbc193052018-09-11 10:06:26 +0200531}
532
533/*
534 * serialize the proc list and put it in the environment
535 */
536static void mworker_proc_list_to_env()
537{
538 char *msg = NULL;
539 struct mworker_proc *child;
540
541 list_for_each_entry(child, &proc_list, list) {
William Lallemand16dd1b32018-11-19 18:46:18 +0100542 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 +0200543 }
544 if (msg)
William Lallemand16dd1b32018-11-19 18:46:18 +0100545 setenv("HAPROXY_PROCESSES", msg, 1);
William Lallemandbc193052018-09-11 10:06:26 +0200546}
547
548/*
549 * unserialize the proc list from the environment
550 */
551static void mworker_env_to_proc_list()
552{
553 char *msg, *token = NULL, *s1;
554
William Lallemand16dd1b32018-11-19 18:46:18 +0100555 msg = getenv("HAPROXY_PROCESSES");
William Lallemandbc193052018-09-11 10:06:26 +0200556 if (!msg)
557 return;
558
559 while ((token = strtok_r(msg, "|", &s1))) {
560 struct mworker_proc *child;
561 char *subtoken = NULL;
562 char *s2;
563
564 msg = NULL;
565
566 child = calloc(1, sizeof(*child));
567
568 while ((subtoken = strtok_r(token, ";", &s2))) {
569
570 token = NULL;
571
William Lallemand16dd1b32018-11-19 18:46:18 +0100572 if (strncmp(subtoken, "type=", 5) == 0) {
573 child->type = *(subtoken+5);
574 if (child->type == 'm') /* we are in the master, assign it */
575 proc_self = child;
576 } else if (strncmp(subtoken, "fd=", 3) == 0) {
William Lallemandbc193052018-09-11 10:06:26 +0200577 child->ipc_fd[0] = atoi(subtoken+3);
578 } else if (strncmp(subtoken, "pid=", 4) == 0) {
579 child->pid = atoi(subtoken+4);
580 } else if (strncmp(subtoken, "rpid=", 5) == 0) {
581 child->relative_pid = atoi(subtoken+5);
William Lallemandf1a62862018-10-26 14:47:29 +0200582 } else if (strncmp(subtoken, "reloads=", 8) == 0) {
583 /* we reloaded this process once more */
584 child->reloads = atoi(subtoken+8) + 1;
William Lallemande3683302018-11-19 18:46:17 +0100585 } else if (strncmp(subtoken, "timestamp=", 10) == 0) {
586 child->timestamp = atoi(subtoken+10);
William Lallemandbc193052018-09-11 10:06:26 +0200587 }
588 }
589 if (child->pid)
590 LIST_ADDQ(&proc_list, &child->list);
591 else
592 free(child);
593 }
594
William Lallemand16dd1b32018-11-19 18:46:18 +0100595 unsetenv("HAPROXY_PROCESSES");
William Lallemand73b85e72017-06-01 17:38:51 +0200596}
597
Willy Tarreaubaaee002006-06-26 02:48:02 +0200598/*
William Lallemand75ea0a02017-11-15 19:02:58 +0100599 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
600 * fd, and the FD provided by fd@
601 */
602static void mworker_cleanlisteners()
603{
604 struct listener *l, *l_next;
605 struct proxy *curproxy;
Willy Tarreau473cf5d2017-12-05 11:14:12 +0100606 struct peers *curpeers;
William Lallemand75ea0a02017-11-15 19:02:58 +0100607
William Lallemand3da97692018-09-11 10:06:19 +0200608 /* we might have to unbind some peers sections from some processes */
609 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
610 if (!curpeers->peers_fe)
611 continue;
Willy Tarreau473cf5d2017-12-05 11:14:12 +0100612
William Lallemand3da97692018-09-11 10:06:19 +0200613 stop_proxy(curpeers->peers_fe);
614 /* disable this peer section so that it kills itself */
615 signal_unregister_handler(curpeers->sighandler);
616 task_delete(curpeers->sync_task);
617 task_free(curpeers->sync_task);
618 curpeers->sync_task = NULL;
619 task_free(curpeers->peers_fe->task);
620 curpeers->peers_fe->task = NULL;
621 curpeers->peers_fe = NULL;
622 }
William Lallemand75ea0a02017-11-15 19:02:58 +0100623
William Lallemand91c13b62018-09-11 10:06:20 +0200624 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
William Lallemand75ea0a02017-11-15 19:02:58 +0100625 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
William Lallemande22f11f2018-09-11 10:06:27 +0200626 /* remove the listener, but not those we need in the master... */
William Lallemanddd319a52018-10-12 10:39:54 +0200627 if (!(l->options & LI_O_MWORKER)) {
628 /* unbind the listener but does not close if
629 the FD is inherited with fd@ from the parent
630 process */
631 if (l->options & LI_O_INHERITED)
632 unbind_listener_no_close(l);
633 else
634 unbind_listener(l);
William Lallemande22f11f2018-09-11 10:06:27 +0200635 delete_listener(l);
William Lallemanddd319a52018-10-12 10:39:54 +0200636 }
William Lallemand75ea0a02017-11-15 19:02:58 +0100637 }
638 }
639}
640
641
642/*
William Lallemand73b85e72017-06-01 17:38:51 +0200643 * remove a pid forom the olpid array and decrease nb_oldpids
644 * return 1 pid was found otherwise return 0
645 */
646
647int delete_oldpid(int pid)
648{
649 int i;
650
651 for (i = 0; i < nb_oldpids; i++) {
652 if (oldpids[i] == pid) {
653 oldpids[i] = oldpids[nb_oldpids - 1];
654 oldpids[nb_oldpids - 1] = 0;
655 nb_oldpids--;
656 return 1;
657 }
658 }
659 return 0;
660}
661
William Lallemand85b0bd92017-06-01 17:38:53 +0200662
663static void get_cur_unixsocket()
664{
665 /* if -x was used, try to update the stat socket if not available anymore */
666 if (global.stats_fe) {
667 struct bind_conf *bind_conf;
668
669 /* pass through all stats socket */
670 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
671 struct listener *l;
672
673 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
674
675 if (l->addr.ss_family == AF_UNIX &&
676 (bind_conf->level & ACCESS_FD_LISTENERS)) {
677 const struct sockaddr_un *un;
678
679 un = (struct sockaddr_un *)&l->addr;
680 /* priority to old_unixsocket */
681 if (!cur_unixsocket) {
682 cur_unixsocket = strdup(un->sun_path);
683 } else {
684 if (old_unixsocket && !strcmp(un->sun_path, old_unixsocket)) {
685 free(cur_unixsocket);
686 cur_unixsocket = strdup(old_unixsocket);
687 return;
688 }
689 }
690 }
691 }
692 }
693 }
694 if (!cur_unixsocket && old_unixsocket)
695 cur_unixsocket = strdup(old_unixsocket);
696}
697
William Lallemand73b85e72017-06-01 17:38:51 +0200698/*
699 * When called, this function reexec haproxy with -sf followed by current
Joseph Herlant03420902018-11-15 10:41:50 -0800700 * children PIDs and possibly old children PIDs if they didn't leave yet.
William Lallemand73b85e72017-06-01 17:38:51 +0200701 */
702static void mworker_reload()
703{
704 int next_argc = 0;
705 int j;
706 char *msg = NULL;
William Lallemand7c756a82018-11-26 11:53:40 +0100707 struct per_thread_deinit_fct *ptdf;
William Lallemand73b85e72017-06-01 17:38:51 +0200708
709 mworker_block_signals();
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100710#if defined(USE_SYSTEMD)
711 if (global.tune.options & GTUNE_USE_SYSTEMD)
712 sd_notify(0, "RELOADING=1");
713#endif
William Lallemand73b85e72017-06-01 17:38:51 +0200714 setenv("HAPROXY_MWORKER_REEXEC", "1", 1);
715
William Lallemandbc193052018-09-11 10:06:26 +0200716 mworker_proc_list_to_env(); /* put the children description in the env */
717
William Lallemand7c756a82018-11-26 11:53:40 +0100718 /* during the reload we must ensure that every FDs that can't be
719 * reuse (ie those that are not referenced in the proc_list)
720 * are closed or they will leak. */
721
722 /* close the listeners FD */
723 mworker_cli_proxy_stop();
724 /* close the poller FD and the thread waker pipe FD */
725 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
726 ptdf->fct();
727 if (fdtab)
728 deinit_pollers();
729
William Lallemand73b85e72017-06-01 17:38:51 +0200730 /* compute length */
731 while (next_argv[next_argc])
732 next_argc++;
733
William Lallemand85b0bd92017-06-01 17:38:53 +0200734 /* 1 for haproxy -sf, 2 for -x /socket */
735 next_argv = realloc(next_argv, (next_argc + 1 + 2 + global.nbproc + nb_oldpids + 1) * sizeof(char *));
William Lallemand73b85e72017-06-01 17:38:51 +0200736 if (next_argv == NULL)
737 goto alloc_error;
738
739
740 /* add -sf <PID>* to argv */
741 if (children || nb_oldpids > 0)
742 next_argv[next_argc++] = "-sf";
743 if (children) {
744 for (j = 0; j < global.nbproc; next_argc++,j++) {
745 next_argv[next_argc] = memprintf(&msg, "%d", children[j]);
746 if (next_argv[next_argc] == NULL)
747 goto alloc_error;
748 msg = NULL;
749 }
750 }
751 /* copy old process PIDs */
752 for (j = 0; j < nb_oldpids; next_argc++,j++) {
753 next_argv[next_argc] = memprintf(&msg, "%d", oldpids[j]);
754 if (next_argv[next_argc] == NULL)
755 goto alloc_error;
756 msg = NULL;
757 }
758 next_argv[next_argc] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200759
William Lallemand2bf6d622017-06-20 11:20:23 +0200760 /* add the -x option with the stat socket */
William Lallemand85b0bd92017-06-01 17:38:53 +0200761 if (cur_unixsocket) {
762
William Lallemand2bf6d622017-06-20 11:20:23 +0200763 next_argv[next_argc++] = "-x";
764 next_argv[next_argc++] = (char *)cur_unixsocket;
765 next_argv[next_argc++] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200766 }
767
Christopher Faulet767a84b2017-11-24 16:50:31 +0100768 ha_warning("Reexecuting Master process\n");
Tim Duesterhus0436ab72017-11-12 17:39:18 +0100769 execvp(next_argv[0], next_argv);
William Lallemand73b85e72017-06-01 17:38:51 +0200770
Christopher Faulet767a84b2017-11-24 16:50:31 +0100771 ha_warning("Failed to reexecute the master process [%d]: %s\n", pid, strerror(errno));
William Lallemand722d4ca2017-11-15 19:02:55 +0100772 return;
773
William Lallemand73b85e72017-06-01 17:38:51 +0200774alloc_error:
Joseph Herlant07a08342018-11-15 10:43:05 -0800775 ha_warning("Failed to reexecute the master process [%d]: Cannot allocate memory\n", pid);
William Lallemand73b85e72017-06-01 17:38:51 +0200776 return;
777}
778
William Lallemand73b85e72017-06-01 17:38:51 +0200779/*
William Lallemandb3f2be32018-09-11 10:06:18 +0200780 * When called, this function reexec haproxy with -sf followed by current
Joseph Herlant03420902018-11-15 10:41:50 -0800781 * children PIDs and possibly old children PIDs if they didn't leave yet.
William Lallemand73b85e72017-06-01 17:38:51 +0200782 */
William Lallemandb3f2be32018-09-11 10:06:18 +0200783static void mworker_catch_sighup(struct sig_handler *sh)
William Lallemand73b85e72017-06-01 17:38:51 +0200784{
William Lallemandb3f2be32018-09-11 10:06:18 +0200785 mworker_reload();
786}
William Lallemand73b85e72017-06-01 17:38:51 +0200787
William Lallemandb3f2be32018-09-11 10:06:18 +0200788static void mworker_catch_sigterm(struct sig_handler *sh)
789{
790 int sig = sh->arg;
William Lallemand2f8b31c2017-11-15 19:02:56 +0100791
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100792#if defined(USE_SYSTEMD)
William Lallemandb3f2be32018-09-11 10:06:18 +0200793 if (global.tune.options & GTUNE_USE_SYSTEMD) {
794 sd_notify(0, "STOPPING=1");
795 }
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100796#endif
William Lallemandb3f2be32018-09-11 10:06:18 +0200797 ha_warning("Exiting Master process...\n");
798 mworker_kill(sig);
799}
William Lallemand73b85e72017-06-01 17:38:51 +0200800
William Lallemandb3f2be32018-09-11 10:06:18 +0200801/*
802 * Wait for every children to exit
803 */
William Lallemand73b85e72017-06-01 17:38:51 +0200804
William Lallemandb3f2be32018-09-11 10:06:18 +0200805static void mworker_catch_sigchld(struct sig_handler *sh)
806{
807 int exitpid = -1;
808 int status = 0;
William Lallemandbc193052018-09-11 10:06:26 +0200809 struct mworker_proc *child, *it;
William Lallemand73b85e72017-06-01 17:38:51 +0200810
William Lallemandb3f2be32018-09-11 10:06:18 +0200811restart_wait:
William Lallemand73b85e72017-06-01 17:38:51 +0200812
William Lallemandb3f2be32018-09-11 10:06:18 +0200813 exitpid = waitpid(-1, &status, WNOHANG);
814 if (exitpid > 0) {
William Lallemand73b85e72017-06-01 17:38:51 +0200815 if (WIFEXITED(status))
816 status = WEXITSTATUS(status);
817 else if (WIFSIGNALED(status))
818 status = 128 + WTERMSIG(status);
819 else if (WIFSTOPPED(status))
820 status = 128 + WSTOPSIG(status);
821 else
822 status = 255;
823
William Lallemandbc193052018-09-11 10:06:26 +0200824 list_for_each_entry_safe(child, it, &proc_list, list) {
825 if (child->pid != exitpid)
826 continue;
827
828 LIST_DEL(&child->list);
829 close(child->ipc_fd[0]);
William Lallemandbc193052018-09-11 10:06:26 +0200830 break;
831 }
832
William Lallemand73b85e72017-06-01 17:38:51 +0200833 if (!children) {
William Lallemand18e52a82018-11-06 17:37:13 +0100834 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 +0200835 } else {
836 /* check if exited child was in the current children list */
837 if (current_child(exitpid)) {
William Lallemand18e52a82018-11-06 17:37:13 +0100838 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 +0200839 if (status != 0 && status != 130 && status != 143
William Lallemand4cfede82017-11-24 22:02:34 +0100840 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100841 ha_alert("exit-on-failure: killing every workers with SIGTERM\n");
William Lallemand91723742018-11-06 17:37:14 +0100842 if (exitcode < 0)
843 exitcode = status;
William Lallemand69f9b3b2017-06-01 17:38:54 +0200844 mworker_kill(SIGTERM);
845 }
William Lallemand73b85e72017-06-01 17:38:51 +0200846 } else {
William Lallemand18e52a82018-11-06 17:37:13 +0100847 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 +0200848 delete_oldpid(exitpid);
849 }
850 }
William Lallemand18e52a82018-11-06 17:37:13 +0100851 free(child);
852
William Lallemandb3f2be32018-09-11 10:06:18 +0200853 /* do it again to check if it was the last worker */
854 goto restart_wait;
William Lallemand73b85e72017-06-01 17:38:51 +0200855 }
William Lallemandb3f2be32018-09-11 10:06:18 +0200856 /* Better rely on the system than on a list of process to check if it was the last one */
857 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand18e52a82018-11-06 17:37:13 +0100858 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : status);
William Lallemandb3f2be32018-09-11 10:06:18 +0200859 atexit_flag = 0;
William Lallemand91723742018-11-06 17:37:14 +0100860 if (exitcode > 0)
861 exit(exitcode);
William Lallemandb3f2be32018-09-11 10:06:18 +0200862 exit(status); /* parent must leave using the latest status code known */
863 }
864
William Lallemand73b85e72017-06-01 17:38:51 +0200865}
866
William Lallemandb3f2be32018-09-11 10:06:18 +0200867static void mworker_loop()
868{
869
870#if defined(USE_SYSTEMD)
871 if (global.tune.options & GTUNE_USE_SYSTEMD)
872 sd_notifyf(0, "READY=1\nMAINPID=%lu", (unsigned long)getpid());
873#endif
874
William Lallemandbc193052018-09-11 10:06:26 +0200875 master = 1;
876
William Lallemand0564d412018-11-20 17:36:53 +0100877 signal_unregister(SIGUSR1);
878 signal_unregister(SIGHUP);
879 signal_unregister(SIGQUIT);
880
William Lallemandb3f2be32018-09-11 10:06:18 +0200881 signal_register_fct(SIGTERM, mworker_catch_sigterm, SIGTERM);
882 signal_register_fct(SIGUSR1, mworker_catch_sigterm, SIGUSR1);
883 signal_register_fct(SIGINT, mworker_catch_sigterm, SIGINT);
884 signal_register_fct(SIGHUP, mworker_catch_sighup, SIGHUP);
885 signal_register_fct(SIGUSR2, mworker_catch_sighup, SIGUSR2);
886 signal_register_fct(SIGCHLD, mworker_catch_sigchld, SIGCHLD);
887
888 mworker_unblock_signals();
889 mworker_cleanlisteners();
890
William Lallemandbc193052018-09-11 10:06:26 +0200891 mworker_catch_sigchld(NULL); /* ensure we clean the children in case
892 some SIGCHLD were lost */
893
William Lallemandb3f2be32018-09-11 10:06:18 +0200894 global.nbthread = 1;
895 relative_pid = 1;
896 pid_bit = 1;
897
898 jobs++; /* this is the "master" job, we want to take care of the
899 signals even if there is no listener so the poll loop don't
900 leave */
901
902 fork_poller();
903 run_thread_poll_loop((int []){0});
904}
William Lallemandcb11fd22017-06-01 17:38:52 +0200905
906/*
907 * Reexec the process in failure mode, instead of exiting
908 */
909void reexec_on_failure()
910{
911 if (!atexit_flag)
912 return;
913
914 setenv("HAPROXY_MWORKER_WAIT_ONLY", "1", 1);
915
Christopher Faulet767a84b2017-11-24 16:50:31 +0100916 ha_warning("Reexecuting Master process in waitpid mode\n");
William Lallemandcb11fd22017-06-01 17:38:52 +0200917 mworker_reload();
William Lallemandcb11fd22017-06-01 17:38:52 +0200918}
William Lallemand73b85e72017-06-01 17:38:51 +0200919
920
921/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200922 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
923 * a signal zero to all subscribers. This means that it's as easy as
924 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200925 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100926static void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200927{
928 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200929 signal_unregister_handler(sh);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100930 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931}
932
933/*
934 * upon SIGTTOU, we pause everything
935 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100936static void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937{
938 pause_proxies();
Willy Tarreaubafbe012017-11-24 17:34:44 +0100939 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200940}
941
942/*
943 * upon SIGTTIN, let's have a soft stop.
944 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100945static void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200946{
Willy Tarreaube58c382011-07-24 18:28:10 +0200947 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948}
949
950/*
951 * this function dumps every server's state when the process receives SIGHUP.
952 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100953static void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200954{
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100955 struct proxy *p = proxies_list;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200956
Christopher Faulet767a84b2017-11-24 16:50:31 +0100957 ha_warning("SIGHUP received, dumping servers states.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200958 while (p) {
959 struct server *s = p->srv;
960
961 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
962 while (s) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100963 chunk_printf(&trash,
964 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
965 p->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +0200966 (s->cur_state != SRV_ST_STOPPED) ? "UP" : "DOWN",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100967 s->cur_sess, s->nbpend, s->counters.cum_sess);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200968 ha_warning("%s\n", trash.area);
969 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200970 s = s->next;
971 }
972
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200973 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
974 if (!p->srv) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100975 chunk_printf(&trash,
976 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
977 p->id,
978 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 +0200979 } else if (p->srv_act == 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100980 chunk_printf(&trash,
981 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
982 p->id,
983 (p->srv_bck) ? "is running on backup servers" : "has no server available",
984 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 +0200985 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100986 chunk_printf(&trash,
987 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
988 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
989 p->id, p->srv_act, p->srv_bck,
990 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 +0200991 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200992 ha_warning("%s\n", trash.area);
993 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200994
995 p = p->next;
996 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200997}
998
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100999static void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001000{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001001 /* dump memory usage then free everything possible */
1002 dump_pools();
Willy Tarreaubafbe012017-11-24 17:34:44 +01001003 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001004}
1005
William Lallemande1340412017-12-28 16:09:36 +01001006/*
1007 * This function dup2 the stdio FDs (0,1,2) with <fd>, then closes <fd>
1008 * If <fd> < 0, it opens /dev/null and use it to dup
1009 *
1010 * In the case of chrooting, you have to open /dev/null before the chroot, and
1011 * pass the <fd> to this function
1012 */
1013static void stdio_quiet(int fd)
1014{
1015 if (fd < 0)
1016 fd = open("/dev/null", O_RDWR, 0);
1017
1018 if (fd > -1) {
1019 fclose(stdin);
1020 fclose(stdout);
1021 fclose(stderr);
1022
1023 dup2(fd, 0);
1024 dup2(fd, 1);
1025 dup2(fd, 2);
1026 if (fd > 2)
1027 close(fd);
1028 return;
1029 }
1030
1031 ha_alert("Cannot open /dev/null\n");
1032 exit(EXIT_FAILURE);
1033}
1034
1035
Joseph Herlant03420902018-11-15 10:41:50 -08001036/* This function checks if cfg_cfgfiles contains directories.
1037 * If it finds one, it adds all the files (and only files) it contains
1038 * in cfg_cfgfiles in place of the directory (and removes the directory).
1039 * It adds the files in lexical order.
1040 * It adds only files with .cfg extension.
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001041 * It doesn't add files with name starting with '.'
1042 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001043static void cfgfiles_expand_directories(void)
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001044{
1045 struct wordlist *wl, *wlb;
1046 char *err = NULL;
1047
1048 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
1049 struct stat file_stat;
1050 struct dirent **dir_entries = NULL;
1051 int dir_entries_nb;
1052 int dir_entries_it;
1053
1054 if (stat(wl->s, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001055 ha_alert("Cannot open configuration file/directory %s : %s\n",
1056 wl->s,
1057 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001058 exit(1);
1059 }
1060
1061 if (!S_ISDIR(file_stat.st_mode))
1062 continue;
1063
1064 /* from this point wl->s is a directory */
1065
1066 dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort);
1067 if (dir_entries_nb < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001068 ha_alert("Cannot open configuration directory %s : %s\n",
1069 wl->s,
1070 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001071 exit(1);
1072 }
1073
1074 /* for each element in the directory wl->s */
1075 for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) {
1076 struct dirent *dir_entry = dir_entries[dir_entries_it];
1077 char *filename = NULL;
1078 char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg");
1079
1080 /* don't add filename that begin with .
Joseph Herlant03420902018-11-15 10:41:50 -08001081 * only add filename with .cfg extension
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001082 */
1083 if (dir_entry->d_name[0] == '.' ||
1084 !(d_name_cfgext && d_name_cfgext[4] == '\0'))
1085 goto next_dir_entry;
1086
1087 if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001088 ha_alert("Cannot load configuration files %s : out of memory.\n",
1089 filename);
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001090 exit(1);
1091 }
1092
1093 if (stat(filename, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001094 ha_alert("Cannot open configuration file %s : %s\n",
1095 wl->s,
1096 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001097 exit(1);
1098 }
1099
1100 /* don't add anything else than regular file in cfg_cfgfiles
1101 * this way we avoid loops
1102 */
1103 if (!S_ISREG(file_stat.st_mode))
1104 goto next_dir_entry;
1105
1106 if (!list_append_word(&wl->list, filename, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001107 ha_alert("Cannot load configuration files %s : %s\n",
1108 filename,
1109 err);
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001110 exit(1);
1111 }
1112
1113next_dir_entry:
1114 free(filename);
1115 free(dir_entry);
1116 }
1117
1118 free(dir_entries);
1119
1120 /* remove the current directory (wl) from cfg_cfgfiles */
1121 free(wl->s);
1122 LIST_DEL(&wl->list);
1123 free(wl);
1124 }
1125
1126 free(err);
1127}
1128
Olivier Houchardf73629d2017-04-05 22:33:04 +02001129static int get_old_sockets(const char *unixsocket)
1130{
1131 char *cmsgbuf = NULL, *tmpbuf = NULL;
1132 int *tmpfd = NULL;
1133 struct sockaddr_un addr;
1134 struct cmsghdr *cmsg;
1135 struct msghdr msghdr;
1136 struct iovec iov;
1137 struct xfer_sock_list *xfer_sock = NULL;
Olivier Houchard54740872017-04-06 14:45:14 +02001138 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf73629d2017-04-05 22:33:04 +02001139 int sock = -1;
1140 int ret = -1;
1141 int ret2 = -1;
1142 int fd_nb;
1143 int got_fd = 0;
1144 int i = 0;
1145 size_t maxoff = 0, curoff = 0;
1146
1147 memset(&msghdr, 0, sizeof(msghdr));
1148 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
1149 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001150 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001151 goto out;
1152 }
1153 sock = socket(PF_UNIX, SOCK_STREAM, 0);
1154 if (sock < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001155 ha_warning("Failed to connect to the old process socket '%s'\n",
1156 unixsocket);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001157 goto out;
1158 }
1159 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path));
1160 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
1161 addr.sun_family = PF_UNIX;
1162 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
1163 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001164 ha_warning("Failed to connect to the old process socket '%s'\n",
1165 unixsocket);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001166 goto out;
1167 }
Olivier Houchard54740872017-04-06 14:45:14 +02001168 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001169 iov.iov_base = &fd_nb;
1170 iov.iov_len = sizeof(fd_nb);
1171 msghdr.msg_iov = &iov;
1172 msghdr.msg_iovlen = 1;
1173 send(sock, "_getsocks\n", strlen("_getsocks\n"), 0);
1174 /* First, get the number of file descriptors to be received */
1175 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001176 ha_warning("Failed to get the number of sockets to be transferred !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001177 goto out;
1178 }
1179 if (fd_nb == 0) {
1180 ret = 0;
1181 goto out;
1182 }
Olivier Houchardf143b802017-11-04 15:13:01 +01001183 tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001184 if (tmpbuf == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001185 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001186 goto out;
1187 }
1188 tmpfd = malloc(fd_nb * sizeof(int));
1189 if (tmpfd == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001190 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001191 goto out;
1192 }
1193 msghdr.msg_control = cmsgbuf;
1194 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
Olivier Houchardf143b802017-11-04 15:13:01 +01001195 iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001196 do {
1197 int ret3;
1198
1199 iov.iov_base = tmpbuf + curoff;
1200 ret = recvmsg(sock, &msghdr, 0);
1201 if (ret == -1 && errno == EINTR)
1202 continue;
1203 if (ret <= 0)
1204 break;
1205 /* Send an ack to let the sender know we got the sockets
1206 * and it can send some more
1207 */
1208 do {
1209 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
1210 } while (ret3 == -1 && errno == EINTR);
1211 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
1212 cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
1213 if (cmsg->cmsg_level == SOL_SOCKET &&
1214 cmsg->cmsg_type == SCM_RIGHTS) {
1215 size_t totlen = cmsg->cmsg_len -
1216 CMSG_LEN(0);
1217 if (totlen / sizeof(int) + got_fd > fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001218 ha_warning("Got to many sockets !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001219 goto out;
1220 }
1221 /*
1222 * Be paranoid and use memcpy() to avoid any
1223 * potential alignement issue.
1224 */
1225 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
1226 got_fd += totlen / sizeof(int);
1227 }
1228 }
1229 curoff += ret;
1230 } while (got_fd < fd_nb);
1231
1232 if (got_fd != fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001233 ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
1234 fd_nb, got_fd);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001235 goto out;
1236 }
1237 maxoff = curoff;
1238 curoff = 0;
1239 for (i = 0; i < got_fd; i++) {
1240 int fd = tmpfd[i];
1241 socklen_t socklen;
1242 int len;
1243
1244 xfer_sock = calloc(1, sizeof(*xfer_sock));
1245 if (!xfer_sock) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001246 ha_warning("Failed to allocate memory in get_old_sockets() !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001247 break;
1248 }
1249 xfer_sock->fd = -1;
1250
1251 socklen = sizeof(xfer_sock->addr);
1252 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001253 ha_warning("Failed to get socket address\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001254 free(xfer_sock);
Olivier Houchardbe7b1ce2017-07-17 17:25:33 +02001255 xfer_sock = NULL;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001256 continue;
1257 }
1258 if (curoff >= maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001259 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001260 goto out;
1261 }
1262 len = tmpbuf[curoff++];
1263 if (len > 0) {
1264 /* We have a namespace */
1265 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001266 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001267 goto out;
1268 }
1269 xfer_sock->namespace = malloc(len + 1);
1270 if (!xfer_sock->namespace) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001271 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001272 goto out;
1273 }
1274 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
1275 xfer_sock->namespace[len] = 0;
1276 curoff += len;
1277 }
1278 if (curoff >= maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001279 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001280 goto out;
1281 }
1282 len = tmpbuf[curoff++];
1283 if (len > 0) {
1284 /* We have an interface */
1285 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001286 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001287 goto out;
1288 }
1289 xfer_sock->iface = malloc(len + 1);
1290 if (!xfer_sock->iface) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001291 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001292 goto out;
1293 }
1294 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
Olivier Houchard33e083c2018-03-15 17:48:49 +01001295 xfer_sock->iface[len] = 0;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001296 curoff += len;
1297 }
1298 if (curoff + sizeof(int) > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001299 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001300 goto out;
1301 }
1302 memcpy(&xfer_sock->options, &tmpbuf[curoff],
1303 sizeof(xfer_sock->options));
1304 curoff += sizeof(xfer_sock->options);
1305
1306 xfer_sock->fd = fd;
1307 if (xfer_sock_list)
1308 xfer_sock_list->prev = xfer_sock;
1309 xfer_sock->next = xfer_sock_list;
1310 xfer_sock->prev = NULL;
1311 xfer_sock_list = xfer_sock;
1312 xfer_sock = NULL;
1313 }
1314
1315 ret2 = 0;
1316out:
1317 /* If we failed midway make sure to close the remaining
1318 * file descriptors
1319 */
1320 if (tmpfd != NULL && i < got_fd) {
1321 for (; i < got_fd; i++) {
1322 close(tmpfd[i]);
1323 }
1324 }
1325 free(tmpbuf);
1326 free(tmpfd);
1327 free(cmsgbuf);
1328 if (sock != -1)
1329 close(sock);
1330 if (xfer_sock) {
1331 free(xfer_sock->namespace);
1332 free(xfer_sock->iface);
1333 if (xfer_sock->fd != -1)
1334 close(xfer_sock->fd);
1335 free(xfer_sock);
1336 }
1337 return (ret2);
1338}
1339
Willy Tarreaubaaee002006-06-26 02:48:02 +02001340/*
William Lallemand73b85e72017-06-01 17:38:51 +02001341 * copy and cleanup the current argv
1342 * Remove the -sf /-st parameters
1343 * Return an allocated copy of argv
1344 */
1345
1346static char **copy_argv(int argc, char **argv)
1347{
1348 char **newargv;
William Lallemand2bf6d622017-06-20 11:20:23 +02001349 int i = 0, j = 0;
William Lallemand73b85e72017-06-01 17:38:51 +02001350
1351 newargv = calloc(argc + 2, sizeof(char *));
1352 if (newargv == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001353 ha_warning("Cannot allocate memory\n");
William Lallemand73b85e72017-06-01 17:38:51 +02001354 return NULL;
1355 }
1356
William Lallemand2bf6d622017-06-20 11:20:23 +02001357 while (i < argc) {
1358 /* -sf or -st or -x */
William Lallemand29f690c2018-01-09 23:12:27 +01001359 if (i > 0 && argv[i][0] == '-' &&
1360 ((argv[i][1] == 's' && (argv[i][2] == 'f' || argv[i][2] == 't')) || argv[i][1] == 'x' )) {
William Lallemand2bf6d622017-06-20 11:20:23 +02001361 /* list of pids to finish ('f') or terminate ('t') or unix socket (-x) */
William Lallemand73b85e72017-06-01 17:38:51 +02001362 i++;
1363 while (i < argc && argv[i][0] != '-') {
1364 i++;
1365 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001366 continue;
William Lallemand73b85e72017-06-01 17:38:51 +02001367 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001368
1369 newargv[j++] = argv[i++];
William Lallemand73b85e72017-06-01 17:38:51 +02001370 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001371
William Lallemand73b85e72017-06-01 17:38:51 +02001372 return newargv;
1373}
1374
1375/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001376 * This function initializes all the necessary variables. It only returns
1377 * if everything is OK. If something fails, it exits.
1378 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001379static void init(int argc, char **argv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001380{
Willy Tarreaubaaee002006-06-26 02:48:02 +02001381 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001382 char *tmp;
1383 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +02001384 int err_code = 0;
Maxime de Roucy0f503922016-05-13 23:52:55 +02001385 char *err_msg = NULL;
Willy Tarreau477ecd82010-01-03 21:12:30 +01001386 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +00001387 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +02001388 char *change_dir = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001389 struct proxy *px;
Willy Tarreaue6945732016-12-21 19:57:00 +01001390 struct post_check_fct *pcf;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001391
Christopher Faulete3a5e352017-10-24 13:53:54 +02001392 global.mode = MODE_STARTING;
William Lallemand73b85e72017-06-01 17:38:51 +02001393 next_argv = copy_argv(argc, argv);
1394
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001395 if (!init_trash_buffers(1)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001396 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet748919a2017-07-26 14:59:46 +02001397 exit(1);
1398 }
David du Colombier7af46052012-05-16 14:16:48 +02001399
Emeric Brun2b920a12010-09-23 18:30:22 +02001400 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
1401 * the string in case of truncation, and at least FreeBSD appears not to do
1402 * it.
1403 */
1404 memset(hostname, 0, sizeof(hostname));
1405 gethostname(hostname, sizeof(hostname) - 1);
1406 memset(localpeer, 0, sizeof(localpeer));
1407 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
William Lallemanddaf4cd22018-04-17 16:46:13 +02001408 setenv("HAPROXY_LOCALPEER", localpeer, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02001409
Willy Tarreaubaaee002006-06-26 02:48:02 +02001410 /*
1411 * Initialize the previously static variables.
1412 */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001413
Willy Tarreau173d9952018-01-26 21:48:23 +01001414 totalconn = actconn = listeners = stopping = 0;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001415 killed = 0;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001416
Willy Tarreaubaaee002006-06-26 02:48:02 +02001417
1418#ifdef HAPROXY_MEMMAX
Willy Tarreau70060452015-12-14 12:46:07 +01001419 global.rlimit_memmax_all = HAPROXY_MEMMAX;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001420#endif
1421
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001422 tzset();
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001423 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001424 start_date = now;
1425
Willy Tarreau84310e22014-02-14 11:59:04 +01001426 srandom(now_ms - getpid());
1427
Dragan Dosen835b9212016-02-12 13:23:03 +01001428 init_log();
Willy Tarreau29857942009-05-10 09:01:21 +02001429 signal_init();
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001430 if (init_acl() != 0)
1431 exit(1);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001432 init_task();
Willy Tarreau87b09662015-04-03 00:22:06 +02001433 init_stream();
Willy Tarreaub1ec8c42015-04-03 13:53:24 +02001434 init_session();
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001435 init_connection();
Willy Tarreau8280d642009-09-23 23:37:52 +02001436 /* warning, we init buffers later */
Willy Tarreaue4d7e552007-05-13 20:19:55 +02001437 init_pendconn();
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02001438 if (!init_http(&err_msg)) {
1439 ha_alert("%s. Aborting.\n", err_msg);
1440 free(err_msg);
1441 abort();
1442 }
Willy Tarreau80587432006-12-24 17:47:20 +01001443 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001444
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001445 /* Initialise lua. */
1446 hlua_init();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001447
Christopher Fauletff2613e2016-11-09 11:36:17 +01001448 /* Initialize process vars */
1449 vars_init(&global.vars, SCOPE_PROC);
1450
Willy Tarreau43b78992009-01-25 15:42:27 +01001451 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001452#if defined(ENABLE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001453 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001454#endif
1455#if defined(ENABLE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001456 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001457#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001458#if defined(ENABLE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +01001459 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001460#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001461#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001462 global.tune.options |= GTUNE_USE_SPLICE;
1463#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001464#if defined(USE_GETADDRINFO)
1465 global.tune.options |= GTUNE_USE_GAI;
1466#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001467#if defined(SO_REUSEPORT)
1468 global.tune.options |= GTUNE_USE_REUSEPORT;
1469#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001470
1471 pid = getpid();
1472 progname = *argv;
1473 while ((tmp = strchr(progname, '/')) != NULL)
1474 progname = tmp + 1;
1475
Kevinm48936af2010-12-22 16:08:21 +00001476 /* the process name is used for the logs only */
Dragan Dosen43885c72015-10-01 13:18:13 +02001477 chunk_initstr(&global.log_tag, strdup(progname));
Kevinm48936af2010-12-22 16:08:21 +00001478
Willy Tarreaubaaee002006-06-26 02:48:02 +02001479 argc--; argv++;
1480 while (argc > 0) {
1481 char *flag;
1482
1483 if (**argv == '-') {
1484 flag = *argv+1;
1485
1486 /* 1 arg */
1487 if (*flag == 'v') {
1488 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +01001489 if (flag[1] == 'v') /* -vv */
1490 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001491 exit(0);
1492 }
1493#if defined(ENABLE_EPOLL)
1494 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +01001495 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001496#endif
1497#if defined(ENABLE_POLL)
1498 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +01001499 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001500#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +02001501#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001502 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +01001503 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001504#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001505#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001506 else if (*flag == 'd' && flag[1] == 'S')
1507 global.tune.options &= ~GTUNE_USE_SPLICE;
1508#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001509#if defined(USE_GETADDRINFO)
1510 else if (*flag == 'd' && flag[1] == 'G')
1511 global.tune.options &= ~GTUNE_USE_GAI;
1512#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001513#if defined(SO_REUSEPORT)
1514 else if (*flag == 'd' && flag[1] == 'R')
1515 global.tune.options &= ~GTUNE_USE_REUSEPORT;
1516#endif
Emeric Brun850efd52014-01-29 12:24:34 +01001517 else if (*flag == 'd' && flag[1] == 'V')
1518 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001519 else if (*flag == 'V')
1520 arg_mode |= MODE_VERBOSE;
1521 else if (*flag == 'd' && flag[1] == 'b')
1522 arg_mode |= MODE_FOREGROUND;
Willy Tarreau6e064432012-05-08 15:40:42 +02001523 else if (*flag == 'd' && flag[1] == 'M')
1524 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreau3eed10e2016-11-07 21:03:16 +01001525 else if (*flag == 'd' && flag[1] == 'r')
1526 global.tune.options |= GTUNE_RESOLVE_DONTFAIL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001527 else if (*flag == 'd')
1528 arg_mode |= MODE_DEBUG;
1529 else if (*flag == 'c')
1530 arg_mode |= MODE_CHECK;
William Lallemand095ba4c2017-06-01 17:38:50 +02001531 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +02001532 arg_mode |= MODE_DAEMON;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001533 else if (*flag == 'W' && flag[1] == 's') {
Lukas Tribusf46bf952017-11-21 12:39:34 +01001534 arg_mode |= MODE_MWORKER | MODE_FOREGROUND;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001535#if defined(USE_SYSTEMD)
1536 global.tune.options |= GTUNE_USE_SYSTEMD;
1537#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001538 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 +01001539 usage(progname);
1540#endif
1541 }
William Lallemand095ba4c2017-06-01 17:38:50 +02001542 else if (*flag == 'W')
1543 arg_mode |= MODE_MWORKER;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001544 else if (*flag == 'q')
1545 arg_mode |= MODE_QUIET;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001546 else if (*flag == 'x') {
William Lallemand45eff442017-06-19 15:57:55 +02001547 if (argc <= 1 || argv[1][0] == '-') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001548 ha_alert("Unix socket path expected with the -x flag\n\n");
William Lallemand45eff442017-06-19 15:57:55 +02001549 usage(progname);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001550 }
William Lallemand4fc09692017-06-19 16:37:19 +02001551 if (old_unixsocket)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001552 ha_warning("-x option already set, overwriting the value\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001553 old_unixsocket = argv[1];
William Lallemand4fc09692017-06-19 16:37:19 +02001554
Olivier Houchardf73629d2017-04-05 22:33:04 +02001555 argv++;
1556 argc--;
1557 }
William Lallemande7361152018-10-26 14:47:36 +02001558 else if (*flag == 'S') {
1559 struct wordlist *c;
1560
1561 if (argc <= 1 || argv[1][0] == '-') {
1562 ha_alert("Socket and optional bind parameters expected with the -S flag\n");
1563 usage(progname);
1564 }
1565 if ((c = malloc(sizeof(*c))) == NULL || (c->s = strdup(argv[1])) == NULL) {
1566 ha_alert("Cannot allocate memory\n");
1567 exit(EXIT_FAILURE);
1568 }
1569 LIST_ADD(&mworker_cli_conf, &c->list);
1570
1571 argv++;
1572 argc--;
1573 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001574 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1575 /* list of pids to finish ('f') or terminate ('t') */
1576
1577 if (flag[1] == 'f')
1578 oldpids_sig = SIGUSR1; /* finish then exit */
1579 else
1580 oldpids_sig = SIGTERM; /* terminate immediately */
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001581 while (argc > 1 && argv[1][0] != '-') {
Chris Lane236062f2018-02-05 23:15:44 +00001582 char * endptr = NULL;
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001583 oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int));
1584 if (!oldpids) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001585 ha_alert("Cannot allocate old pid : out of memory.\n");
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001586 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001587 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001588 argc--; argv++;
Chris Lane236062f2018-02-05 23:15:44 +00001589 errno = 0;
1590 oldpids[nb_oldpids] = strtol(*argv, &endptr, 10);
1591 if (errno) {
1592 ha_alert("-%2s option: failed to parse {%s}: %s\n",
1593 flag,
1594 *argv, strerror(errno));
1595 exit(1);
1596 } else if (endptr && strlen(endptr)) {
1597 while (isspace(*endptr)) endptr++;
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001598 if (*endptr != 0) {
Chris Lane236062f2018-02-05 23:15:44 +00001599 ha_alert("-%2s option: some bytes unconsumed in PID list {%s}\n",
1600 flag, endptr);
1601 exit(1);
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001602 }
Chris Lane236062f2018-02-05 23:15:44 +00001603 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001604 if (oldpids[nb_oldpids] <= 0)
1605 usage(progname);
1606 nb_oldpids++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001607 }
1608 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001609 else if (flag[0] == '-' && flag[1] == 0) { /* "--" */
1610 /* now that's a cfgfile list */
1611 argv++; argc--;
1612 while (argc > 0) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02001613 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001614 ha_alert("Cannot load configuration file/directory %s : %s\n",
1615 *argv,
1616 err_msg);
Willy Tarreaua088d312015-10-08 11:58:48 +02001617 exit(1);
1618 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001619 argv++; argc--;
1620 }
1621 break;
1622 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001623 else { /* >=2 args */
1624 argv++; argc--;
1625 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001626 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001627
1628 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +02001629 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001630 case 'n' : cfg_maxconn = atol(*argv); break;
Willy Tarreau70060452015-12-14 12:46:07 +01001631 case 'm' : global.rlimit_memmax_all = atol(*argv); break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001632 case 'N' : cfg_maxpconn = atol(*argv); break;
William Lallemanddaf4cd22018-04-17 16:46:13 +02001633 case 'L' :
1634 strncpy(localpeer, *argv, sizeof(localpeer) - 1);
1635 setenv("HAPROXY_LOCALPEER", localpeer, 1);
1636 break;
Willy Tarreau5d01a632009-06-22 16:02:30 +02001637 case 'f' :
Maxime de Roucy0f503922016-05-13 23:52:55 +02001638 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001639 ha_alert("Cannot load configuration file/directory %s : %s\n",
1640 *argv,
1641 err_msg);
Willy Tarreau5d01a632009-06-22 16:02:30 +02001642 exit(1);
1643 }
Willy Tarreau5d01a632009-06-22 16:02:30 +02001644 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001645 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001646 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001647 }
1648 }
1649 }
1650 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001651 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001652 argv++; argc--;
1653 }
1654
Christopher Faulete3a5e352017-10-24 13:53:54 +02001655 global.mode |= (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
1656 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001657
William Lallemand944e6192018-11-21 15:48:31 +01001658 if (getenv("HAPROXY_MWORKER_WAIT_ONLY")) {
William Lallemandcb11fd22017-06-01 17:38:52 +02001659 unsetenv("HAPROXY_MWORKER_WAIT_ONLY");
William Lallemand944e6192018-11-21 15:48:31 +01001660 global.mode |= MODE_MWORKER_WAIT;
1661 global.mode &= ~MODE_MWORKER;
William Lallemandcb11fd22017-06-01 17:38:52 +02001662 }
1663
1664 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {
1665 atexit_flag = 1;
1666 atexit(reexec_on_failure);
1667 }
1668
Willy Tarreau576132e2011-09-10 19:26:56 +02001669 if (change_dir && chdir(change_dir) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001670 ha_alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
Willy Tarreau576132e2011-09-10 19:26:56 +02001671 exit(1);
1672 }
1673
Willy Tarreaubaaee002006-06-26 02:48:02 +02001674 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001675
1676 init_default_instance();
1677
William Lallemand944e6192018-11-21 15:48:31 +01001678 /* in wait mode, we don't try to read the configuration files */
1679 if (!(global.mode & MODE_MWORKER_WAIT)) {
Willy Tarreauc4382422009-12-06 13:10:44 +01001680
William Lallemand944e6192018-11-21 15:48:31 +01001681 /* handle cfgfiles that are actually directories */
1682 cfgfiles_expand_directories();
1683
1684 if (LIST_ISEMPTY(&cfg_cfgfiles))
1685 usage(progname);
1686
1687
1688 list_for_each_entry(wl, &cfg_cfgfiles, list) {
1689 int ret;
1690
1691 ret = readcfgfile(wl->s);
1692 if (ret == -1) {
1693 ha_alert("Could not open configuration file %s : %s\n",
1694 wl->s, strerror(errno));
1695 exit(1);
1696 }
1697 if (ret & (ERR_ABORT|ERR_FATAL))
1698 ha_alert("Error(s) found in configuration file : %s\n", wl->s);
1699 err_code |= ret;
1700 if (err_code & ERR_ABORT)
1701 exit(1);
Willy Tarreauc4382422009-12-06 13:10:44 +01001702 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001703
William Lallemand944e6192018-11-21 15:48:31 +01001704 /* do not try to resolve arguments nor to spot inconsistencies when
1705 * the configuration contains fatal errors caused by files not found
1706 * or failed memory allocations.
1707 */
1708 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1709 ha_alert("Fatal errors found in configuration.\n");
1710 exit(1);
1711 }
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02001712 }
William Lallemandce83b4a2018-10-26 14:47:30 +02001713 if (global.mode & MODE_MWORKER) {
1714 int proc;
William Lallemand16dd1b32018-11-19 18:46:18 +01001715 struct mworker_proc *tmproc;
1716
1717 if (getenv("HAPROXY_MWORKER_REEXEC") == NULL) {
1718
1719 tmproc = malloc(sizeof(*tmproc));
1720 if (!tmproc) {
1721 ha_alert("Cannot allocate process structures.\n");
1722 exit(EXIT_FAILURE);
1723 }
1724 tmproc->type = 'm'; /* master */
1725 tmproc->reloads = 0;
1726 tmproc->relative_pid = 0;
1727 tmproc->pid = pid;
1728 tmproc->timestamp = start_date.tv_sec;
1729 tmproc->ipc_fd[0] = -1;
1730 tmproc->ipc_fd[1] = -1;
1731
1732 proc_self = tmproc;
1733
1734 LIST_ADDQ(&proc_list, &tmproc->list);
1735 }
William Lallemandce83b4a2018-10-26 14:47:30 +02001736
1737 for (proc = 0; proc < global.nbproc; proc++) {
William Lallemandce83b4a2018-10-26 14:47:30 +02001738
1739 tmproc = malloc(sizeof(*tmproc));
1740 if (!tmproc) {
1741 ha_alert("Cannot allocate process structures.\n");
1742 exit(EXIT_FAILURE);
1743 }
1744
William Lallemand16dd1b32018-11-19 18:46:18 +01001745 tmproc->type = 'w'; /* worker */
William Lallemandce83b4a2018-10-26 14:47:30 +02001746 tmproc->pid = -1;
1747 tmproc->reloads = 0;
William Lallemande3683302018-11-19 18:46:17 +01001748 tmproc->timestamp = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02001749 tmproc->relative_pid = 1 + proc;
William Lallemand550db6d2018-11-06 17:37:12 +01001750 tmproc->ipc_fd[0] = -1;
1751 tmproc->ipc_fd[1] = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02001752
1753 if (mworker_cli_sockpair_new(tmproc, proc) < 0) {
1754 exit(EXIT_FAILURE);
1755 }
1756
1757 LIST_ADDQ(&proc_list, &tmproc->list);
1758 }
William Lallemand944e6192018-11-21 15:48:31 +01001759 }
1760 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
1761 struct wordlist *it, *c;
1762
William Lallemand1b663612018-10-26 14:47:33 +02001763 mworker_env_to_proc_list(); /* get the info of the children in the env */
William Lallemand8a022572018-10-26 14:47:35 +02001764
William Lallemande7361152018-10-26 14:47:36 +02001765
William Lallemand550db6d2018-11-06 17:37:12 +01001766 if (!LIST_ISEMPTY(&mworker_cli_conf)) {
William Lallemande7361152018-10-26 14:47:36 +02001767
William Lallemand550db6d2018-11-06 17:37:12 +01001768 if (mworker_cli_proxy_create() < 0) {
William Lallemande7361152018-10-26 14:47:36 +02001769 ha_alert("Can't create the master's CLI.\n");
1770 exit(EXIT_FAILURE);
1771 }
William Lallemande7361152018-10-26 14:47:36 +02001772
William Lallemand550db6d2018-11-06 17:37:12 +01001773 list_for_each_entry_safe(c, it, &mworker_cli_conf, list) {
1774
1775 if (mworker_cli_proxy_new_listener(c->s) < 0) {
1776 ha_alert("Can't create the master's CLI.\n");
1777 exit(EXIT_FAILURE);
1778 }
1779 LIST_DEL(&c->list);
1780 free(c->s);
1781 free(c);
1782 }
1783 }
William Lallemandce83b4a2018-10-26 14:47:30 +02001784 }
1785
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001786 pattern_finalize_config();
1787
Willy Tarreaubb925012009-07-23 13:36:36 +02001788 err_code |= check_config_validity();
1789 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001790 ha_alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001791 exit(1);
1792 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001793
Willy Tarreau70060452015-12-14 12:46:07 +01001794 /* recompute the amount of per-process memory depending on nbproc and
1795 * the shared SSL cache size (allowed to exist in all processes).
1796 */
1797 if (global.rlimit_memmax_all) {
1798#if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE)
1799 int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL;
1800
1801 global.rlimit_memmax =
1802 ((((int64_t)global.rlimit_memmax_all * 1048576LL) -
1803 ssl_cache_bytes) / global.nbproc +
1804 ssl_cache_bytes + 1048575LL) / 1048576LL;
1805#else
1806 global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc;
1807#endif
1808 }
1809
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001810#ifdef CONFIG_HAP_NS
1811 err_code |= netns_init();
1812 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001813 ha_alert("Failed to initialize namespace support.\n");
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001814 exit(1);
1815 }
1816#endif
1817
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01001818 /* Apply server states */
1819 apply_server_state();
1820
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001821 for (px = proxies_list; px; px = px->next)
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01001822 srv_compute_all_admin_states(px);
1823
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001824 /* Apply servers' configured address */
1825 err_code |= srv_init_addr();
1826 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001827 ha_alert("Failed to initialize server(s) addr.\n");
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001828 exit(1);
1829 }
1830
Willy Tarreaubaaee002006-06-26 02:48:02 +02001831 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001832 struct peers *pr;
1833 struct proxy *px;
1834
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001835 for (pr = cfg_peers; pr; pr = pr->next)
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001836 if (pr->peers_fe)
1837 break;
1838
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001839 for (px = proxies_list; px; px = px->next)
Willy Tarreau4348fad2012-09-20 16:48:07 +02001840 if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners))
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001841 break;
1842
1843 if (pr || px) {
1844 /* At least one peer or one listener has been found */
1845 qfprintf(stdout, "Configuration file is valid\n");
1846 exit(0);
1847 }
1848 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
1849 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001850 }
1851
Emeric Brunc60def82017-09-27 14:59:38 +02001852 global_listener_queue_task = task_new(MAX_THREADS_MASK);
Willy Tarreaue9b26022011-08-01 20:57:55 +02001853 if (!global_listener_queue_task) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001854 ha_alert("Out of memory when initializing global task\n");
Willy Tarreaue9b26022011-08-01 20:57:55 +02001855 exit(1);
1856 }
1857 /* very simple initialization, users will queue the task if needed */
1858 global_listener_queue_task->context = NULL; /* not even a context! */
1859 global_listener_queue_task->process = manage_global_listener_queue;
Willy Tarreaue9b26022011-08-01 20:57:55 +02001860
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001861 /* now we know the buffer size, we can initialize the channels and buffers */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001862 init_buffer();
Willy Tarreau8280d642009-09-23 23:37:52 +02001863
Willy Tarreaue6945732016-12-21 19:57:00 +01001864 list_for_each_entry(pcf, &post_check_list, list) {
1865 err_code |= pcf->fct();
1866 if (err_code & (ERR_ABORT|ERR_FATAL))
1867 exit(1);
1868 }
1869
Willy Tarreaubaaee002006-06-26 02:48:02 +02001870 if (cfg_maxconn > 0)
1871 global.maxconn = cfg_maxconn;
1872
1873 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +02001874 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001875 global.pidfile = strdup(cfg_pidfile);
1876 }
1877
Willy Tarreaud0256482015-01-15 21:45:22 +01001878 /* Now we want to compute the maxconn and possibly maxsslconn values.
1879 * It's a bit tricky. If memmax is not set, maxconn defaults to
1880 * DEFAULT_MAXCONN and maxsslconn defaults to DEFAULT_MAXSSLCONN.
1881 *
1882 * If memmax is set, then it depends on which values are set. If
1883 * maxsslconn is set, we use memmax to determine how many cleartext
1884 * connections may be added, and set maxconn to the sum of the two.
1885 * If maxconn is set and not maxsslconn, maxsslconn is computed from
1886 * the remaining amount of memory between memmax and the cleartext
1887 * connections. If neither are set, then it is considered that all
1888 * connections are SSL-capable, and maxconn is computed based on this,
1889 * then maxsslconn accordingly. We need to know if SSL is used on the
1890 * frontends, backends, or both, because when it's used on both sides,
1891 * we need twice the value for maxsslconn, but we only count the
1892 * handshake once since it is not performed on the two sides at the
1893 * same time (frontend-side is terminated before backend-side begins).
1894 * The SSL stack is supposed to have filled ssl_session_cost and
Willy Tarreau474b96a2015-01-28 19:03:21 +01001895 * ssl_handshake_cost during its initialization. In any case, if
1896 * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for
1897 * maxconn in order to protect the system.
Willy Tarreaud0256482015-01-15 21:45:22 +01001898 */
1899 if (!global.rlimit_memmax) {
1900 if (global.maxconn == 0) {
1901 global.maxconn = DEFAULT_MAXCONN;
1902 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1903 fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn);
1904 }
1905 }
1906#ifdef USE_OPENSSL
1907 else if (!global.maxconn && !global.maxsslconn &&
1908 (global.ssl_used_frontend || global.ssl_used_backend)) {
1909 /* memmax is set, compute everything automatically. Here we want
1910 * to ensure that all SSL connections will be served. We take
1911 * care of the number of sides where SSL is used, and consider
1912 * the worst case : SSL used on both sides and doing a handshake
1913 * simultaneously. Note that we can't have more than maxconn
1914 * handshakes at a time by definition, so for the worst case of
1915 * two SSL conns per connection, we count a single handshake.
1916 */
1917 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1918 int64_t mem = global.rlimit_memmax * 1048576ULL;
1919
1920 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1921 mem -= global.maxzlibmem;
1922 mem = mem * MEM_USABLE_RATIO;
1923
1924 global.maxconn = mem /
Willy Tarreau87b09662015-04-03 00:22:06 +02001925 ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream
Willy Tarreaud0256482015-01-15 21:45:22 +01001926 sides * global.ssl_session_max_cost + // SSL buffers, one per side
1927 global.ssl_handshake_max_cost); // 1 handshake per connection max
1928
1929 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001930#ifdef SYSTEM_MAXCONN
1931 if (global.maxconn > DEFAULT_MAXCONN)
1932 global.maxconn = DEFAULT_MAXCONN;
1933#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001934 global.maxsslconn = sides * global.maxconn;
1935 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1936 fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n",
1937 global.maxconn, global.maxsslconn);
1938 }
1939 else if (!global.maxsslconn &&
1940 (global.ssl_used_frontend || global.ssl_used_backend)) {
1941 /* memmax and maxconn are known, compute maxsslconn automatically.
1942 * maxsslconn being forced, we don't know how many of it will be
1943 * on each side if both sides are being used. The worst case is
1944 * when all connections use only one SSL instance because
1945 * handshakes may be on two sides at the same time.
1946 */
1947 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1948 int64_t mem = global.rlimit_memmax * 1048576ULL;
1949 int64_t sslmem;
1950
1951 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1952 mem -= global.maxzlibmem;
1953 mem = mem * MEM_USABLE_RATIO;
1954
Willy Tarreau87b09662015-04-03 00:22:06 +02001955 sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001956 global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost);
1957 global.maxsslconn = round_2dig(global.maxsslconn);
1958
1959 if (sslmem <= 0 || global.maxsslconn < sides) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001960 ha_alert("Cannot compute the automatic maxsslconn because global.maxconn is already too "
1961 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1962 "without SSL is %d, but %d was found and SSL is in use.\n",
1963 global.rlimit_memmax,
1964 (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)),
1965 global.maxconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01001966 exit(1);
1967 }
1968
1969 if (global.maxsslconn > sides * global.maxconn)
1970 global.maxsslconn = sides * global.maxconn;
1971
1972 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1973 fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn);
1974 }
1975#endif
1976 else if (!global.maxconn) {
1977 /* memmax and maxsslconn are known/unused, compute maxconn automatically */
1978 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1979 int64_t mem = global.rlimit_memmax * 1048576ULL;
1980 int64_t clearmem;
1981
1982 if (global.ssl_used_frontend || global.ssl_used_backend)
1983 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1984
1985 mem -= global.maxzlibmem;
1986 mem = mem * MEM_USABLE_RATIO;
1987
1988 clearmem = mem;
1989 if (sides)
1990 clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn;
1991
Willy Tarreau87b09662015-04-03 00:22:06 +02001992 global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001993 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001994#ifdef SYSTEM_MAXCONN
1995 if (global.maxconn > DEFAULT_MAXCONN)
1996 global.maxconn = DEFAULT_MAXCONN;
1997#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001998
1999 if (clearmem <= 0 || !global.maxconn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002000 ha_alert("Cannot compute the automatic maxconn because global.maxsslconn is already too "
2001 "high for the global.memmax value (%d MB). The absolute maximum possible value "
2002 "is %d, but %d was found.\n",
2003 global.rlimit_memmax,
2004 (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)),
2005 global.maxsslconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01002006 exit(1);
2007 }
2008
2009 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
2010 if (sides && global.maxsslconn > sides * global.maxconn) {
2011 fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn "
2012 "to be limited to %d. Better reduce global.maxsslconn to get more "
2013 "room for extra connections.\n", global.maxsslconn, global.maxconn);
2014 }
2015 fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn);
2016 }
2017 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002018
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002019 if (!global.maxpipes) {
2020 /* maxpipes not specified. Count how many frontends and backends
2021 * may be using splicing, and bound that to maxconn.
2022 */
2023 struct proxy *cur;
2024 int nbfe = 0, nbbe = 0;
2025
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002026 for (cur = proxies_list; cur; cur = cur->next) {
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002027 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
2028 if (cur->cap & PR_CAP_FE)
2029 nbfe += cur->maxconn;
2030 if (cur->cap & PR_CAP_BE)
Willy Tarreauafb48762009-01-25 10:42:05 +01002031 nbbe += cur->fullconn ? cur->fullconn : global.maxconn;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002032 }
2033 }
2034 global.maxpipes = MAX(nbfe, nbbe);
2035 if (global.maxpipes > global.maxconn)
2036 global.maxpipes = global.maxconn;
Willy Tarreau686ac822009-01-25 14:06:58 +01002037 global.maxpipes /= 4;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002038 }
2039
2040
Willy Tarreauabacc2c2011-09-07 14:26:33 +02002041 global.hardmaxconn = global.maxconn; /* keep this max value */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002042 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
Willy Tarreau3ec79b92009-01-18 20:39:42 +01002043 global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */
Emeric Brunece0c332017-12-06 13:51:49 +01002044 /* compute fd used by async engines */
2045 if (global.ssl_used_async_engines) {
2046 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2047 global.maxsock += global.maxconn * sides * global.ssl_used_async_engines;
2048 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002049
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002050 if (global.stats_fe)
2051 global.maxsock += global.stats_fe->maxconn;
2052
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002053 if (cfg_peers) {
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002054 /* peers also need to bypass global maxconn */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002055 struct peers *p = cfg_peers;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002056
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002057 for (p = cfg_peers; p; p = p->next)
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002058 if (p->peers_fe)
2059 global.maxsock += p->peers_fe->maxconn;
2060 }
2061
Willy Tarreau1db37712007-06-03 17:16:49 +02002062 if (global.tune.maxpollevents <= 0)
2063 global.tune.maxpollevents = MAX_POLL_EVENTS;
2064
Olivier Houchard1599b802018-05-24 18:59:04 +02002065 if (global.tune.runqueue_depth <= 0)
2066 global.tune.runqueue_depth = RUNQUEUE_DEPTH;
2067
Willy Tarreau6f4a82c2009-03-21 20:43:57 +01002068 if (global.tune.recv_enough == 0)
2069 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
2070
Willy Tarreau27097842015-09-28 13:53:23 +02002071 if (global.tune.maxrewrite < 0)
2072 global.tune.maxrewrite = MAXREWRITE;
2073
Willy Tarreau27a674e2009-08-17 07:23:33 +02002074 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
2075 global.tune.maxrewrite = global.tune.bufsize / 2;
2076
Willy Tarreaubaaee002006-06-26 02:48:02 +02002077 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
2078 /* command line debug mode inhibits configuration mode */
William Lallemand095ba4c2017-06-01 17:38:50 +02002079 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002080 global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND));
2081 }
2082
William Lallemand095ba4c2017-06-01 17:38:50 +02002083 if (arg_mode & MODE_DAEMON) {
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002084 /* command line daemon mode inhibits foreground and debug modes mode */
2085 global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND);
William Lallemand095ba4c2017-06-01 17:38:50 +02002086 global.mode |= arg_mode & MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002087 }
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002088
2089 global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002090
William Lallemand095ba4c2017-06-01 17:38:50 +02002091 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002092 ha_warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
William Lallemand095ba4c2017-06-01 17:38:50 +02002093 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002094 }
2095
William Lallemand095ba4c2017-06-01 17:38:50 +02002096 if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002097 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
Christopher Faulet767a84b2017-11-24 16:50:31 +01002098 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 +02002099 global.nbproc = 1;
2100 }
2101
2102 if (global.nbproc < 1)
2103 global.nbproc = 1;
2104
Christopher Fauletbe0faa22017-08-29 15:37:10 +02002105 if (global.nbthread < 1)
2106 global.nbthread = 1;
2107
Christopher Faulet3ef26392017-08-29 16:46:57 +02002108 /* Realloc trash buffers because global.tune.bufsize may have changed */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002109 if (!init_trash_buffers(0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002110 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet3ef26392017-08-29 16:46:57 +02002111 exit(1);
2112 }
2113
Christopher Faulet96d44832017-11-14 22:02:30 +01002114 if (!init_log_buffers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002115 ha_alert("failed to initialize log buffers.\n");
Christopher Faulet96d44832017-11-14 22:02:30 +01002116 exit(1);
2117 }
2118
Willy Tarreauef1d1f82007-04-16 00:25:25 +02002119 /*
2120 * Note: we could register external pollers here.
2121 * Built-in pollers have been registered before main().
2122 */
Willy Tarreau4f60f162007-04-08 16:39:58 +02002123
Willy Tarreau43b78992009-01-25 15:42:27 +01002124 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +02002125 disable_poller("kqueue");
2126
Willy Tarreau43b78992009-01-25 15:42:27 +01002127 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002128 disable_poller("epoll");
2129
Willy Tarreau43b78992009-01-25 15:42:27 +01002130 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002131 disable_poller("poll");
2132
Willy Tarreau43b78992009-01-25 15:42:27 +01002133 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002134 disable_poller("select");
2135
2136 /* Note: we could disable any poller by name here */
2137
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002138 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +02002139 list_pollers(stderr);
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002140 fprintf(stderr, "\n");
2141 list_filters(stderr);
2142 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002143
Willy Tarreau4f60f162007-04-08 16:39:58 +02002144 if (!init_pollers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002145 ha_alert("No polling mechanism available.\n"
2146 " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n"
2147 " is too low on this platform to support maxconn and the number of listeners\n"
2148 " and servers. You should rebuild haproxy specifying your system using TARGET=\n"
2149 " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n"
2150 " global maxconn setting to accommodate the system's limitation. For reference,\n"
2151 " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n"
2152 " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n"
2153 " check build settings using 'haproxy -vv'.\n\n",
2154 FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002155 exit(1);
2156 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002157 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
2158 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002159 }
2160
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002161 if (!global.node)
2162 global.node = strdup(hostname);
2163
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01002164 if (!hlua_post_init())
2165 exit(1);
Thomas Holmes6abded42015-05-12 16:23:58 +01002166
Maxime de Roucy0f503922016-05-13 23:52:55 +02002167 free(err_msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002168}
2169
Simon Horman6fb82592011-07-15 13:14:11 +09002170static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +09002171{
Simon Hormanac821422011-07-15 13:14:09 +09002172 struct acl_term_suite *suite, *suiteb;
2173 struct acl_term *term, *termb;
2174
Simon Horman6fb82592011-07-15 13:14:11 +09002175 if (!cond)
2176 return;
2177
2178 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
2179 list_for_each_entry_safe(term, termb, &suite->terms, list) {
2180 LIST_DEL(&term->list);
2181 free(term);
Simon Hormanac821422011-07-15 13:14:09 +09002182 }
Simon Horman6fb82592011-07-15 13:14:11 +09002183 LIST_DEL(&suite->list);
2184 free(suite);
2185 }
2186
2187 free(cond);
2188}
2189
2190static void deinit_tcp_rules(struct list *rules)
2191{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002192 struct act_rule *trule, *truleb;
Simon Horman6fb82592011-07-15 13:14:11 +09002193
2194 list_for_each_entry_safe(trule, truleb, rules, list) {
Simon Hormanac821422011-07-15 13:14:09 +09002195 LIST_DEL(&trule->list);
Simon Horman6fb82592011-07-15 13:14:11 +09002196 deinit_acl_cond(trule->cond);
Simon Hormanac821422011-07-15 13:14:09 +09002197 free(trule);
2198 }
2199}
2200
Simon Horman6fb82592011-07-15 13:14:11 +09002201static void deinit_stick_rules(struct list *rules)
2202{
2203 struct sticking_rule *rule, *ruleb;
2204
2205 list_for_each_entry_safe(rule, ruleb, rules, list) {
2206 LIST_DEL(&rule->list);
2207 deinit_acl_cond(rule->cond);
Christopher Faulet476e5d02016-10-26 11:34:47 +02002208 release_sample_expr(rule->expr);
Simon Horman6fb82592011-07-15 13:14:11 +09002209 free(rule);
2210 }
2211}
2212
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002213void deinit(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002214{
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002215 struct proxy *p = proxies_list, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002216 struct cap_hdr *h,*h_next;
2217 struct server *s,*s_next;
2218 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002219 struct acl_cond *cond, *condb;
2220 struct hdr_exp *exp, *expb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002221 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002222 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002223 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002224 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002225 struct wordlist *wl, *wlb;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002226 struct cond_wordlist *cwl, *cwlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002227 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +02002228 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +01002229 struct logformat_node *lf, *lfb;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002230 struct bind_conf *bind_conf, *bind_back;
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002231 struct build_opts_str *bol, *bolb;
Willy Tarreau05554e62016-12-21 20:46:26 +01002232 struct post_deinit_fct *pdf;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002233 int i;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002234
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002235 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002236 while (p) {
Willy Tarreau8113a5d2012-10-04 08:01:43 +02002237 free(p->conf.file);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002238 free(p->id);
2239 free(p->check_req);
2240 free(p->cookie_name);
2241 free(p->cookie_domain);
2242 free(p->url_param_name);
2243 free(p->capture_name);
2244 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +09002245 free(p->rdp_cookie_name);
Willy Tarreau62a61232013-04-12 18:13:46 +02002246 if (p->conf.logformat_string != default_http_log_format &&
2247 p->conf.logformat_string != default_tcp_log_format &&
2248 p->conf.logformat_string != clf_http_log_format)
2249 free(p->conf.logformat_string);
Willy Tarreau196729e2012-05-31 19:30:26 +02002250
Willy Tarreau62a61232013-04-12 18:13:46 +02002251 free(p->conf.lfs_file);
2252 free(p->conf.uniqueid_format_string);
2253 free(p->conf.uif_file);
Godbachaf1a75d2013-10-02 17:10:11 +08002254 free(p->lbprm.map.srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002255
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002256 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
2257 free(p->conf.logformat_sd_string);
2258 free(p->conf.lfsd_file);
2259
Willy Tarreaua534fea2008-08-03 12:19:50 +02002260 for (i = 0; i < HTTP_ERR_SIZE; i++)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002261 chunk_destroy(&p->errmsg[i]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002262
Willy Tarreauf4f04122010-01-28 18:10:50 +01002263 list_for_each_entry_safe(cwl, cwlb, &p->req_add, list) {
2264 LIST_DEL(&cwl->list);
2265 free(cwl->s);
2266 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002267 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002268
Willy Tarreauf4f04122010-01-28 18:10:50 +01002269 list_for_each_entry_safe(cwl, cwlb, &p->rsp_add, list) {
2270 LIST_DEL(&cwl->list);
2271 free(cwl->s);
2272 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002273 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002274
Willy Tarreaub80c2302007-11-30 20:51:32 +01002275 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
2276 LIST_DEL(&cond->list);
2277 prune_acl_cond(cond);
2278 free(cond);
2279 }
2280
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002281 for (exp = p->req_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002282 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002283 regex_free(exp->preg);
2284 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002285 }
2286
Willy Tarreau98d04852015-05-26 12:18:29 +02002287 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002288 expb = exp;
2289 exp = exp->next;
2290 free(expb);
2291 }
2292
2293 for (exp = p->rsp_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002294 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002295 regex_free(exp->preg);
2296 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002297 }
2298
Willy Tarreau98d04852015-05-26 12:18:29 +02002299 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002300 expb = exp;
2301 exp = exp->next;
2302 free(expb);
2303 }
2304
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002305 /* build a list of unique uri_auths */
2306 if (!ua)
2307 ua = p->uri_auth;
2308 else {
2309 /* check if p->uri_auth is unique */
2310 for (uap = ua; uap; uap=uap->next)
2311 if (uap == p->uri_auth)
2312 break;
2313
Willy Tarreauaccc4e12008-06-24 11:14:45 +02002314 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002315 /* add it, if it is */
2316 p->uri_auth->next = ua;
2317 ua = p->uri_auth;
2318 }
2319 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002320
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002321 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
2322 LIST_DEL(&acl->list);
2323 prune_acl(acl);
2324 free(acl);
2325 }
2326
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002327 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
2328 LIST_DEL(&srule->list);
2329 prune_acl_cond(srule->cond);
2330 free(srule->cond);
2331 free(srule);
2332 }
2333
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002334 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
2335 LIST_DEL(&rule->list);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002336 if (rule->cond) {
2337 prune_acl_cond(rule->cond);
2338 free(rule->cond);
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +01002339 free(rule->file);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002340 }
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002341 free(rule);
2342 }
2343
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002344 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
2345 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +01002346 if (rdr->cond) {
2347 prune_acl_cond(rdr->cond);
2348 free(rdr->cond);
2349 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002350 free(rdr->rdr_str);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002351 list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) {
2352 LIST_DEL(&lf->list);
2353 free(lf);
2354 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002355 free(rdr);
2356 }
2357
William Lallemand0f99e342011-10-12 17:50:54 +02002358 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
2359 LIST_DEL(&log->list);
2360 free(log);
2361 }
2362
William Lallemand723b73a2012-02-08 16:37:49 +01002363 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
2364 LIST_DEL(&lf->list);
2365 free(lf);
2366 }
2367
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002368 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
2369 LIST_DEL(&lf->list);
2370 free(lf);
2371 }
2372
Simon Hormanac821422011-07-15 13:14:09 +09002373 deinit_tcp_rules(&p->tcp_req.inspect_rules);
2374 deinit_tcp_rules(&p->tcp_req.l4_rules);
2375
Simon Horman6fb82592011-07-15 13:14:11 +09002376 deinit_stick_rules(&p->storersp_rules);
2377 deinit_stick_rules(&p->sticking_rules);
2378
Willy Tarreaubaaee002006-06-26 02:48:02 +02002379 h = p->req_cap;
2380 while (h) {
2381 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002382 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002383 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002384 free(h);
2385 h = h_next;
2386 }/* end while(h) */
2387
2388 h = p->rsp_cap;
2389 while (h) {
2390 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002391 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002392 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002393 free(h);
2394 h = h_next;
2395 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002396
Willy Tarreaubaaee002006-06-26 02:48:02 +02002397 s = p->srv;
2398 while (s) {
2399 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002400
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002401 if (s->check.task) {
2402 task_delete(s->check.task);
2403 task_free(s->check.task);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002404 }
Simon Hormand60d6912013-11-25 10:46:36 +09002405 if (s->agent.task) {
2406 task_delete(s->agent.task);
2407 task_free(s->agent.task);
2408 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002409
Willy Tarreau2e993902011-10-31 11:53:20 +01002410 if (s->warmup) {
2411 task_delete(s->warmup);
2412 task_free(s->warmup);
2413 }
2414
Willy Tarreaua534fea2008-08-03 12:19:50 +02002415 free(s->id);
2416 free(s->cookie);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002417 free(s->check.bi.area);
2418 free(s->check.bo.area);
2419 free(s->agent.bi.area);
2420 free(s->agent.bo.area);
James Brown55f9ff12015-10-21 18:19:05 -07002421 free(s->agent.send_string);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002422 free(s->hostname_dn);
Sárközi, László34c01792014-09-05 10:08:23 +02002423 free((char*)s->conf.file);
Olivier Houchard7fc3be72018-11-22 18:50:54 +01002424 free(s->idle_conns);
2425 free(s->priv_conns);
2426 free(s->safe_conns);
Willy Tarreau17d45382016-12-22 21:16:08 +01002427
2428 if (s->use_ssl || s->check.use_ssl) {
2429 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2430 xprt_get(XPRT_SSL)->destroy_srv(s);
2431 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002432 HA_SPIN_DESTROY(&s->lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002433 free(s);
2434 s = s_next;
2435 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002436
Willy Tarreau4348fad2012-09-20 16:48:07 +02002437 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002438 /*
2439 * Zombie proxy, the listener just pretend to be up
2440 * because they still hold an opened fd.
2441 * Close it and give the listener its real state.
2442 */
2443 if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
2444 close(l->fd);
2445 l->state = LI_INIT;
2446 }
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02002447 unbind_listener(l);
2448 delete_listener(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002449 LIST_DEL(&l->by_fe);
2450 LIST_DEL(&l->by_bind);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002451 free(l->name);
2452 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002453 free(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002454 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002455
Willy Tarreau4348fad2012-09-20 16:48:07 +02002456 /* Release unused SSL configs. */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002457 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01002458 if (bind_conf->xprt->destroy_bind_conf)
2459 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002460 free(bind_conf->file);
2461 free(bind_conf->arg);
2462 LIST_DEL(&bind_conf->by_fe);
2463 free(bind_conf);
2464 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02002465
Christopher Fauletd7c91962015-04-30 11:48:27 +02002466 flt_deinit(p);
2467
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002468 free(p->desc);
2469 free(p->fwdfor_hdr_name);
2470
Willy Tarreauff011f22011-01-06 17:51:27 +01002471 free_http_req_rules(&p->http_req_rules);
Sasha Pachev218f0642014-06-16 12:05:59 -06002472 free_http_res_rules(&p->http_res_rules);
Willy Tarreau1f89b182017-11-22 16:53:53 +01002473 task_free(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002474
Willy Tarreaubafbe012017-11-24 17:34:44 +01002475 pool_destroy(p->req_cap_pool);
2476 pool_destroy(p->rsp_cap_pool);
2477 pool_destroy(p->table.pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002478
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002479 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002480 p = p->next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002481 HA_SPIN_DESTROY(&p0->lbprm.lock);
2482 HA_SPIN_DESTROY(&p0->lock);
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002483 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002484 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02002485
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002486 while (ua) {
2487 uap = ua;
2488 ua = ua->next;
2489
Willy Tarreaua534fea2008-08-03 12:19:50 +02002490 free(uap->uri_prefix);
2491 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002492 free(uap->node);
2493 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002494
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002495 userlist_free(uap->userlist);
Willy Tarreauff011f22011-01-06 17:51:27 +01002496 free_http_req_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002497
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002498 free(uap);
2499 }
2500
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002501 userlist_free(userlist);
2502
David Carlier834cb2e2015-09-25 12:02:25 +01002503 cfg_unregister_sections();
2504
Christopher Faulet0132d062017-07-26 15:33:35 +02002505 deinit_log_buffers();
Christopher Faulet748919a2017-07-26 14:59:46 +02002506 deinit_trash_buffers();
David Carlier834cb2e2015-09-25 12:02:25 +01002507
Willy Tarreaudd815982007-10-16 12:25:14 +02002508 protocol_unbind_all();
2509
Willy Tarreau05554e62016-12-21 20:46:26 +01002510 list_for_each_entry(pdf, &post_deinit_list, list)
2511 pdf->fct();
2512
Joe Williamsdf5b38f2010-12-29 17:05:48 +01002513 free(global.log_send_hostname); global.log_send_hostname = NULL;
Dragan Dosen43885c72015-10-01 13:18:13 +02002514 chunk_destroy(&global.log_tag);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002515 free(global.chroot); global.chroot = NULL;
2516 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002517 free(global.node); global.node = NULL;
2518 free(global.desc); global.desc = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002519 free(oldpids); oldpids = NULL;
Willy Tarreau1f89b182017-11-22 16:53:53 +01002520 task_free(global_listener_queue_task); global_listener_queue_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002521
William Lallemand0f99e342011-10-12 17:50:54 +02002522 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
2523 LIST_DEL(&log->list);
2524 free(log);
2525 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01002526 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02002527 free(wl->s);
Willy Tarreau477ecd82010-01-03 21:12:30 +01002528 LIST_DEL(&wl->list);
2529 free(wl);
2530 }
2531
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002532 list_for_each_entry_safe(bol, bolb, &build_opts_list, list) {
2533 if (bol->must_free)
2534 free((void *)bol->str);
2535 LIST_DEL(&bol->list);
2536 free(bol);
2537 }
2538
Christopher Fauletff2613e2016-11-09 11:36:17 +01002539 vars_prune(&global.vars, NULL, NULL);
2540
Christopher Fauletad405f12017-08-29 15:30:11 +02002541 deinit_buffer();
2542
Willy Tarreaubafbe012017-11-24 17:34:44 +01002543 pool_destroy(pool_head_stream);
2544 pool_destroy(pool_head_session);
2545 pool_destroy(pool_head_connection);
2546 pool_destroy(pool_head_connstream);
2547 pool_destroy(pool_head_requri);
2548 pool_destroy(pool_head_task);
2549 pool_destroy(pool_head_capture);
2550 pool_destroy(pool_head_pendconn);
2551 pool_destroy(pool_head_sig_handlers);
2552 pool_destroy(pool_head_hdr_idx);
2553 pool_destroy(pool_head_http_txn);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002554 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002555} /* end deinit() */
2556
William Lallemand72160322018-11-06 17:37:16 +01002557
2558
2559/* This is a wrapper for the sockpair FD, It tests if the socket received an
2560 * EOF, if not, it calls listener_accept */
2561void mworker_accept_wrapper(int fd)
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002562{
2563 char c;
William Lallemand72160322018-11-06 17:37:16 +01002564 int ret;
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002565
William Lallemand72160322018-11-06 17:37:16 +01002566 while (1) {
2567 ret = recv(fd, &c, 1, MSG_PEEK);
2568 if (ret == -1) {
2569 if (errno == EINTR)
2570 continue;
2571 if (errno == EAGAIN) {
2572 fd_cant_recv(fd);
2573 return;
2574 }
2575 break;
2576 } else if (ret > 0) {
2577 listener_accept(fd);
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002578 return;
William Lallemand72160322018-11-06 17:37:16 +01002579 } else if (ret == 0) {
2580 /* At this step the master is down before
2581 * this worker perform a 'normal' exit.
2582 * So we want to exit with an error but
2583 * other threads could currently process
2584 * some stuff so we can't perform a clean
2585 * deinit().
2586 */
2587 exit(EXIT_FAILURE);
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002588 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002589 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002590 return;
2591}
2592
William Lallemand72160322018-11-06 17:37:16 +01002593/*
William Lallemand72160322018-11-06 17:37:16 +01002594 * This function register the accept wrapper for the sockpair of the master worker
2595 */
Willy Tarreau1605c7a2018-01-23 19:01:49 +01002596void mworker_pipe_register()
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002597{
William Lallemand72160322018-11-06 17:37:16 +01002598 /* The iocb should be already initialized with listener_accept */
William Lallemanda90cacf2018-11-07 08:38:32 +01002599 if (fdtab[proc_self->ipc_fd[1]].iocb == mworker_accept_wrapper)
2600 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002601
William Lallemandbc193052018-09-11 10:06:26 +02002602 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
Emeric Brunc8c0ed92018-10-11 15:27:07 +02002603 /* In multi-tread, we need only one thread to process
2604 * events on the pipe with master
2605 */
William Lallemande260e0d2018-11-08 12:00:14 +01002606 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 +02002607 fd_want_recv(proc_self->ipc_fd[1]);
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002608}
Christopher Fauletdc628a32017-10-19 11:59:44 +02002609
Willy Tarreau918ff602011-07-25 16:33:49 +02002610/* Runs the polling loop */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01002611static void run_poll_loop()
Willy Tarreau4f60f162007-04-08 16:39:58 +02002612{
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002613 int next, exp;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002614
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002615 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002616 while (1) {
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002617 /* Process a few tasks */
2618 process_runnable_tasks();
2619
William Lallemand1aab50b2018-06-07 09:46:01 +02002620 /* check if we caught some signals and process them in the
2621 first thread */
2622 if (tid == 0)
2623 signal_process_queue();
Willy Tarreau29857942009-05-10 09:01:21 +02002624
Willy Tarreau58b458d2008-06-29 22:40:23 +02002625 /* Check if we can expire some tasks */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002626 next = wake_expired_tasks();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002627
Willy Tarreau85c459d2018-08-02 10:54:31 +02002628 /* stop when there's nothing left to do */
William Lallemanda7199262018-11-16 16:57:20 +01002629 if ((jobs - unstoppable_jobs) == 0)
Willy Tarreau85c459d2018-08-02 10:54:31 +02002630 break;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002631
Willy Tarreau10146c92015-04-13 20:44:19 +02002632 /* expire immediately if events are pending */
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002633 exp = now_ms;
Christopher Faulet32467fe2018-01-15 12:16:34 +01002634 if (fd_cache_mask & tid_bit)
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002635 activity[tid].wake_cache++;
2636 else if (active_tasks_mask & tid_bit)
2637 activity[tid].wake_tasks++;
William Lallemand1aab50b2018-06-07 09:46:01 +02002638 else if (signal_queue_len && tid == 0)
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002639 activity[tid].wake_signal++;
Olivier Houchard79321b92018-07-26 17:55:11 +02002640 else {
2641 HA_ATOMIC_OR(&sleeping_thread_mask, tid_bit);
2642 __ha_barrier_store();
2643 if (active_tasks_mask & tid_bit) {
2644 activity[tid].wake_tasks++;
2645 HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
2646 } else
2647 exp = next;
2648 }
Willy Tarreau10146c92015-04-13 20:44:19 +02002649
Willy Tarreau58b458d2008-06-29 22:40:23 +02002650 /* The poller will ensure it returns around <next> */
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002651 cur_poller.poll(&cur_poller, exp);
Olivier Houchard79321b92018-07-26 17:55:11 +02002652 if (sleeping_thread_mask & tid_bit)
2653 HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
Willy Tarreau033cd9d2014-01-25 19:24:15 +01002654 fd_process_cached_events();
Emeric Brun64cc49c2017-10-03 14:46:45 +02002655
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002656 activity[tid].loops++;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002657 }
2658}
2659
Christopher Faulet1d17c102017-08-29 15:38:48 +02002660static void *run_thread_poll_loop(void *data)
2661{
2662 struct per_thread_init_fct *ptif;
2663 struct per_thread_deinit_fct *ptdf;
Christopher Fauletda18b9d2018-01-25 16:10:16 +01002664 __decl_hathreads(static HA_SPINLOCK_T start_lock);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002665
Willy Tarreau0c026f42018-08-01 19:12:20 +02002666 ha_set_tid(*((unsigned int *)data));
Christopher Faulet1d17c102017-08-29 15:38:48 +02002667 tv_update_date(-1,-1);
2668
2669 list_for_each_entry(ptif, &per_thread_init_list, list) {
2670 if (!ptif->fct()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002671 ha_alert("failed to initialize thread %u.\n", tid);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002672 exit(1);
2673 }
2674 }
2675
William Lallemandbc193052018-09-11 10:06:26 +02002676 if ((global.mode & MODE_MWORKER) && master == 0) {
Willy Tarreau1605c7a2018-01-23 19:01:49 +01002677 HA_SPIN_LOCK(START_LOCK, &start_lock);
2678 mworker_pipe_register();
2679 HA_SPIN_UNLOCK(START_LOCK, &start_lock);
2680 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002681
2682 protocol_enable_all();
Christopher Faulet1d17c102017-08-29 15:38:48 +02002683 run_poll_loop();
2684
2685 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
2686 ptdf->fct();
2687
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002688#ifdef USE_THREAD
William Lallemand091d8272018-06-24 09:37:03 +02002689 HA_ATOMIC_AND(&all_threads_mask, ~tid_bit);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002690 if (tid > 0)
2691 pthread_exit(NULL);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002692#endif
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002693 return NULL;
2694}
Christopher Faulet1d17c102017-08-29 15:38:48 +02002695
Willy Tarreaue9b26022011-08-01 20:57:55 +02002696/* This is the global management task for listeners. It enables listeners waiting
2697 * for global resources when there are enough free resource, or at least once in
2698 * a while. It is designed to be called as a task.
2699 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002700static struct task *manage_global_listener_queue(struct task *t, void *context, unsigned short state)
Willy Tarreaue9b26022011-08-01 20:57:55 +02002701{
2702 int next = TICK_ETERNITY;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002703 /* queue is empty, nothing to do */
2704 if (LIST_ISEMPTY(&global_listener_queue))
2705 goto out;
2706
2707 /* If there are still too many concurrent connections, let's wait for
2708 * some of them to go away. We don't need to re-arm the timer because
2709 * each of them will scan the queue anyway.
2710 */
2711 if (unlikely(actconn >= global.maxconn))
2712 goto out;
2713
2714 /* We should periodically try to enable listeners waiting for a global
2715 * resource here, because it is possible, though very unlikely, that
2716 * they have been blocked by a temporary lack of global resource such
2717 * as a file descriptor or memory and that the temporary condition has
2718 * disappeared.
2719 */
Willy Tarreauabacc2c2011-09-07 14:26:33 +02002720 dequeue_all_listeners(&global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +02002721
2722 out:
2723 t->expire = next;
2724 task_queue(t);
2725 return t;
2726}
Willy Tarreau4f60f162007-04-08 16:39:58 +02002727
Willy Tarreaubaaee002006-06-26 02:48:02 +02002728int main(int argc, char **argv)
2729{
2730 int err, retry;
2731 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02002732 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02002733 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002734
Olivier Houchard5fa300d2018-02-03 15:15:21 +01002735 setvbuf(stdout, NULL, _IONBF, 0);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002736 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002737 signal_register_fct(SIGQUIT, dump, SIGQUIT);
2738 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
2739 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
William Lallemand73b85e72017-06-01 17:38:51 +02002740 signal_register_fct(SIGUSR2, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002741
Willy Tarreaue437c442010-03-17 18:02:46 +01002742 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
2743 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
2744 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002745 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002746 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002747
Willy Tarreaudc23a922011-02-16 11:10:36 +01002748 /* ulimits */
2749 if (!global.rlimit_nofile)
2750 global.rlimit_nofile = global.maxsock;
2751
2752 if (global.rlimit_nofile) {
2753 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
2754 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
Willy Tarreauef635472016-06-21 11:48:18 +02002755 /* try to set it to the max possible at least */
2756 getrlimit(RLIMIT_NOFILE, &limit);
Willy Tarreau164dd0b2016-06-21 11:51:59 +02002757 limit.rlim_cur = limit.rlim_max;
2758 if (setrlimit(RLIMIT_NOFILE, &limit) != -1)
2759 getrlimit(RLIMIT_NOFILE, &limit);
2760
Christopher Faulet767a84b2017-11-24 16:50:31 +01002761 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 +02002762 global.rlimit_nofile = limit.rlim_cur;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002763 }
2764 }
2765
2766 if (global.rlimit_memmax) {
2767 limit.rlim_cur = limit.rlim_max =
Willy Tarreau70060452015-12-14 12:46:07 +01002768 global.rlimit_memmax * 1048576ULL;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002769#ifdef RLIMIT_AS
2770 if (setrlimit(RLIMIT_AS, &limit) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002771 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2772 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01002773 }
2774#else
2775 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002776 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2777 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01002778 }
2779#endif
2780 }
2781
Olivier Houchardf73629d2017-04-05 22:33:04 +02002782 if (old_unixsocket) {
William Lallemand85b0bd92017-06-01 17:38:53 +02002783 if (strcmp("/dev/null", old_unixsocket) != 0) {
2784 if (get_old_sockets(old_unixsocket) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002785 ha_alert("Failed to get the sockets from the old process!\n");
William Lallemand85b0bd92017-06-01 17:38:53 +02002786 if (!(global.mode & MODE_MWORKER))
2787 exit(1);
2788 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002789 }
2790 }
William Lallemand85b0bd92017-06-01 17:38:53 +02002791 get_cur_unixsocket();
2792
Willy Tarreaubaaee002006-06-26 02:48:02 +02002793 /* We will loop at most 100 times with 10 ms delay each time.
2794 * That's at most 1 second. We only send a signal to old pids
2795 * if we cannot grab at least one port.
2796 */
2797 retry = MAX_START_RETRIES;
2798 err = ERR_NONE;
2799 while (retry >= 0) {
2800 struct timeval w;
2801 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01002802 /* exit the loop on no error or fatal error */
2803 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002804 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02002805 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002806 break;
2807
2808 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
2809 * listening sockets. So on those platforms, it would be wiser to
2810 * simply send SIGUSR1, which will not be undoable.
2811 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02002812 if (tell_old_pids(SIGTTOU) == 0) {
2813 /* no need to wait if we can't contact old pids */
2814 retry = 0;
2815 continue;
2816 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002817 /* give some time to old processes to stop listening */
2818 w.tv_sec = 0;
2819 w.tv_usec = 10*1000;
2820 select(0, NULL, NULL, NULL, &w);
2821 retry--;
2822 }
2823
2824 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01002825 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02002826 if (retry != MAX_START_RETRIES && nb_oldpids) {
2827 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002828 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02002829 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002830 exit(1);
2831 }
2832
William Lallemand944e6192018-11-21 15:48:31 +01002833 if (!(global.mode & MODE_MWORKER_WAIT) && listeners == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002834 ha_alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002835 /* Note: we don't have to send anything to the old pids because we
2836 * never stopped them. */
2837 exit(1);
2838 }
2839
Emeric Bruncf20bf12010-10-22 16:06:11 +02002840 err = protocol_bind_all(errmsg, sizeof(errmsg));
2841 if ((err & ~ERR_WARN) != ERR_NONE) {
2842 if ((err & ERR_ALERT) || (err & ERR_WARN))
Christopher Faulet767a84b2017-11-24 16:50:31 +01002843 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002844
Christopher Faulet767a84b2017-11-24 16:50:31 +01002845 ha_alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002846 protocol_unbind_all(); /* cleanup everything we can */
2847 if (nb_oldpids)
2848 tell_old_pids(SIGTTIN);
2849 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002850 } else if (err & ERR_WARN) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002851 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02002852 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002853 /* Ok, all listener should now be bound, close any leftover sockets
2854 * the previous process gave us, we don't need them anymore
2855 */
2856 while (xfer_sock_list != NULL) {
2857 struct xfer_sock_list *tmpxfer = xfer_sock_list->next;
2858 close(xfer_sock_list->fd);
2859 free(xfer_sock_list->iface);
2860 free(xfer_sock_list->namespace);
2861 free(xfer_sock_list);
2862 xfer_sock_list = tmpxfer;
2863 }
Willy Tarreaudd815982007-10-16 12:25:14 +02002864
Willy Tarreaubaaee002006-06-26 02:48:02 +02002865 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002866 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
2867 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002868
Willy Tarreaubaaee002006-06-26 02:48:02 +02002869 /* MODE_QUIET can inhibit alerts and warnings below this line */
2870
PiBa-NL149a81a2017-12-25 21:03:31 +01002871 if (getenv("HAPROXY_MWORKER_REEXEC") != NULL) {
2872 /* either stdin/out/err are already closed or should stay as they are. */
2873 if ((global.mode & MODE_DAEMON)) {
2874 /* daemon mode re-executing, stdin/stdout/stderr are already closed so keep quiet */
2875 global.mode &= ~MODE_VERBOSE;
2876 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
2877 }
2878 } else {
2879 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
2880 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01002881 stdio_quiet(-1);
PiBa-NL149a81a2017-12-25 21:03:31 +01002882 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002883 }
2884
2885 /* open log & pid files before the chroot */
William Lallemand80293002017-11-06 11:00:03 +01002886 if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002887 unlink(global.pidfile);
2888 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
2889 if (pidfd < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002890 ha_alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002891 if (nb_oldpids)
2892 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02002893 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002894 exit(1);
2895 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002896 }
2897
Willy Tarreaub38651a2007-03-24 17:24:39 +01002898 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002899 ha_alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
2900 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002901 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01002902 exit(1);
2903 }
2904
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002905 /* If the user is not root, we'll still let him try the configuration
2906 * but we inform him that unexpected behaviour may occur.
2907 */
2908 if ((global.last_checks & LSTCHK_NETADM) && getuid())
Christopher Faulet767a84b2017-11-24 16:50:31 +01002909 ha_warning("[%s.main()] Some options which require full privileges"
2910 " might not work well.\n"
2911 "", argv[0]);
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002912
William Lallemand095ba4c2017-06-01 17:38:50 +02002913 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2914
2915 /* chroot if needed */
2916 if (global.chroot != NULL) {
2917 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002918 ha_alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02002919 if (nb_oldpids)
2920 tell_old_pids(SIGTTIN);
2921 protocol_unbind_all();
2922 exit(1);
2923 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002924 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002925 }
2926
William Lallemand944e6192018-11-21 15:48:31 +01002927 if (nb_oldpids && !(global.mode & MODE_MWORKER_WAIT))
Willy Tarreaubb545b42010-08-25 12:58:59 +02002928 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002929
William Lallemand8a361b52017-06-20 11:20:33 +02002930 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) {
2931 nb_oldpids = 0;
2932 free(oldpids);
2933 oldpids = NULL;
2934 }
2935
2936
Willy Tarreaubaaee002006-06-26 02:48:02 +02002937 /* Note that any error at this stage will be fatal because we will not
2938 * be able to restart the old pids.
2939 */
2940
William Lallemand095ba4c2017-06-01 17:38:50 +02002941 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2942 /* setgid / setuid */
2943 if (global.gid) {
2944 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +01002945 ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2946 " without 'uid'/'user' is generally useless.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02002947
2948 if (setgid(global.gid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002949 ha_alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
William Lallemand095ba4c2017-06-01 17:38:50 +02002950 protocol_unbind_all();
2951 exit(1);
2952 }
2953 }
Michael Schererab012dd2013-01-12 18:35:19 +01002954
William Lallemand095ba4c2017-06-01 17:38:50 +02002955 if (global.uid && setuid(global.uid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002956 ha_alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Michael Schererab012dd2013-01-12 18:35:19 +01002957 protocol_unbind_all();
2958 exit(1);
2959 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002960 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002961 /* check ulimits */
2962 limit.rlim_cur = limit.rlim_max = 0;
2963 getrlimit(RLIMIT_NOFILE, &limit);
2964 if (limit.rlim_cur < global.maxsock) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002965 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",
2966 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002967 }
2968
William Lallemand944e6192018-11-21 15:48:31 +01002969 if (global.mode & (MODE_DAEMON | MODE_MWORKER | MODE_MWORKER_WAIT)) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002970 struct proxy *px;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002971 struct peers *curpeers;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002972 int ret = 0;
2973 int proc;
William Lallemande1340412017-12-28 16:09:36 +01002974 int devnullfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002975
William Lallemand095ba4c2017-06-01 17:38:50 +02002976 /*
2977 * if daemon + mworker: must fork here to let a master
2978 * process live in background before forking children
2979 */
William Lallemand73b85e72017-06-01 17:38:51 +02002980
2981 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
2982 && (global.mode & MODE_MWORKER)
2983 && (global.mode & MODE_DAEMON)) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002984 ret = fork();
2985 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002986 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02002987 protocol_unbind_all();
2988 exit(1); /* there has been an error */
William Lallemandbfd8eb52018-07-04 15:31:23 +02002989 } else if (ret > 0) { /* parent leave to daemonize */
William Lallemand095ba4c2017-06-01 17:38:50 +02002990 exit(0);
William Lallemandbfd8eb52018-07-04 15:31:23 +02002991 } else /* change the process group ID in the child (master process) */
2992 setsid();
William Lallemand095ba4c2017-06-01 17:38:50 +02002993 }
William Lallemande20b6a62017-06-01 17:38:55 +02002994
William Lallemande20b6a62017-06-01 17:38:55 +02002995
William Lallemanddeed7802017-11-06 11:00:04 +01002996 /* if in master-worker mode, write the PID of the father */
2997 if (global.mode & MODE_MWORKER) {
2998 char pidstr[100];
2999 snprintf(pidstr, sizeof(pidstr), "%d\n", getpid());
Willy Tarreau46ec48b2018-01-23 19:20:19 +01003000 if (pidfd >= 0)
3001 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
William Lallemanddeed7802017-11-06 11:00:04 +01003002 }
3003
Willy Tarreaubaaee002006-06-26 02:48:02 +02003004 /* the father launches the required number of processes */
William Lallemand944e6192018-11-21 15:48:31 +01003005 if (!(global.mode & MODE_MWORKER_WAIT)) {
3006 children = calloc(global.nbproc, sizeof(int));
3007 for (proc = 0; proc < global.nbproc; proc++) {
3008 ret = fork();
3009 if (ret < 0) {
3010 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
3011 protocol_unbind_all();
3012 exit(1); /* there has been an error */
3013 }
3014 else if (ret == 0) /* child breaks here */
3015 break;
3016 children[proc] = ret;
3017 if (pidfd >= 0 && !(global.mode & MODE_MWORKER)) {
3018 char pidstr[100];
3019 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
3020 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
3021 }
3022 if (global.mode & MODE_MWORKER) {
3023 struct mworker_proc *child;
William Lallemandce83b4a2018-10-26 14:47:30 +02003024
William Lallemand220567e2018-11-21 18:04:53 +01003025 ha_notice("New worker #%d (%d) forked\n", relative_pid, ret);
William Lallemand944e6192018-11-21 15:48:31 +01003026 /* find the right mworker_proc */
3027 list_for_each_entry(child, &proc_list, list) {
3028 if (child->relative_pid == relative_pid &&
3029 child->reloads == 0) {
3030 child->timestamp = now.tv_sec;
3031 child->pid = ret;
3032 break;
3033 }
William Lallemandce83b4a2018-10-26 14:47:30 +02003034 }
3035 }
William Lallemandbc193052018-09-11 10:06:26 +02003036
William Lallemand944e6192018-11-21 15:48:31 +01003037 relative_pid++; /* each child will get a different one */
3038 pid_bit <<= 1;
3039 }
3040 } else {
3041 /* wait mode */
3042 global.nbproc = 1;
3043 proc = 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003044 }
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003045
3046#ifdef USE_CPU_AFFINITY
3047 if (proc < global.nbproc && /* child */
Willy Tarreaue7597492015-04-20 11:36:57 +02003048 proc < LONGBITS && /* only the first 32/64 processes may be pinned */
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003049 global.cpu_map.proc[proc]) /* only do this if the process has a CPU map */
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003050#ifdef __FreeBSD__
Olivier Houchard97148f62017-08-16 17:29:11 +02003051 {
3052 cpuset_t cpuset;
3053 int i;
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003054 unsigned long cpu_map = global.cpu_map.proc[proc];
Olivier Houchard97148f62017-08-16 17:29:11 +02003055
3056 CPU_ZERO(&cpuset);
3057 while ((i = ffsl(cpu_map)) > 0) {
3058 CPU_SET(i - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003059 cpu_map &= ~(1UL << (i - 1));
Olivier Houchard97148f62017-08-16 17:29:11 +02003060 }
3061 ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset);
3062 }
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003063#else
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003064 sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map.proc[proc]);
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003065#endif
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003066#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003067 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02003068 if (pidfd >= 0) {
3069 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
3070 close(pidfd);
3071 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02003072
3073 /* We won't ever use this anymore */
Willy Tarreaud137dd32010-08-25 12:49:05 +02003074 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003075
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003076 if (proc == global.nbproc) {
William Lallemand944e6192018-11-21 15:48:31 +01003077 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003078
3079 if ((!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
3080 (global.mode & MODE_DAEMON)) {
3081 /* detach from the tty, this is required to properly daemonize. */
William Lallemande1340412017-12-28 16:09:36 +01003082 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL))
3083 stdio_quiet(-1);
3084
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003085 global.mode &= ~MODE_VERBOSE;
3086 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003087 }
3088
William Lallemandb3f2be32018-09-11 10:06:18 +02003089 mworker_loop();
William Lallemand1499b9b2017-06-07 15:04:47 +02003090 /* should never get there */
3091 exit(EXIT_FAILURE);
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003092 }
William Lallemandcf4e4962017-06-08 19:05:48 +02003093#if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH)
Grant Zhang872f9c22017-01-21 01:10:18 +00003094 ssl_free_dh();
3095#endif
William Lallemand1499b9b2017-06-07 15:04:47 +02003096 exit(0); /* parent must leave */
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003097 }
3098
William Lallemandcb11fd22017-06-01 17:38:52 +02003099 /* child must never use the atexit function */
3100 atexit_flag = 0;
3101
William Lallemandbc193052018-09-11 10:06:26 +02003102 /* close useless master sockets */
3103 if (global.mode & MODE_MWORKER) {
3104 struct mworker_proc *child, *it;
3105 master = 0;
3106
William Lallemand309dc9a2018-10-26 14:47:45 +02003107 mworker_cli_proxy_stop();
3108
William Lallemandbc193052018-09-11 10:06:26 +02003109 /* free proc struct of other processes */
3110 list_for_each_entry_safe(child, it, &proc_list, list) {
William Lallemandce83b4a2018-10-26 14:47:30 +02003111 /* close the FD of the master side for all
3112 * workers, we don't need to close the worker
3113 * side of other workers since it's done with
3114 * the bind_proc */
Tim Duesterhus742e0f92018-11-25 20:03:39 +01003115 if (child->ipc_fd[0] >= 0)
3116 close(child->ipc_fd[0]);
William Lallemandce83b4a2018-10-26 14:47:30 +02003117 if (child->relative_pid == relative_pid &&
3118 child->reloads == 0) {
3119 /* keep this struct if this is our pid */
3120 proc_self = child;
William Lallemandbc193052018-09-11 10:06:26 +02003121 continue;
William Lallemandce83b4a2018-10-26 14:47:30 +02003122 }
William Lallemandbc193052018-09-11 10:06:26 +02003123 LIST_DEL(&child->list);
3124 free(child);
3125 }
3126 }
Willy Tarreau1605c7a2018-01-23 19:01:49 +01003127
William Lallemande1340412017-12-28 16:09:36 +01003128 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
3129 devnullfd = open("/dev/null", O_RDWR, 0);
3130 if (devnullfd < 0) {
3131 ha_alert("Cannot open /dev/null\n");
3132 exit(EXIT_FAILURE);
3133 }
3134 }
3135
William Lallemand095ba4c2017-06-01 17:38:50 +02003136 /* Must chroot and setgid/setuid in the children */
3137 /* chroot if needed */
3138 if (global.chroot != NULL) {
3139 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003140 ha_alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02003141 if (nb_oldpids)
3142 tell_old_pids(SIGTTIN);
3143 protocol_unbind_all();
3144 exit(1);
3145 }
3146 }
3147
3148 free(global.chroot);
3149 global.chroot = NULL;
3150
3151 /* setgid / setuid */
3152 if (global.gid) {
3153 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003154 ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
3155 " without 'uid'/'user' is generally useless.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02003156
3157 if (setgid(global.gid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003158 ha_alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
William Lallemand095ba4c2017-06-01 17:38:50 +02003159 protocol_unbind_all();
3160 exit(1);
3161 }
3162 }
3163
3164 if (global.uid && setuid(global.uid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003165 ha_alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
William Lallemand095ba4c2017-06-01 17:38:50 +02003166 protocol_unbind_all();
3167 exit(1);
3168 }
3169
William Lallemand7f80eb22017-05-26 18:19:55 +02003170 /* pass through every cli socket, and check if it's bound to
3171 * the current process and if it exposes listeners sockets.
3172 * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork.
3173 * */
3174
3175 if (global.stats_fe) {
3176 struct bind_conf *bind_conf;
3177
3178 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
3179 if (bind_conf->level & ACCESS_FD_LISTENERS) {
3180 if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) {
3181 global.tune.options |= GTUNE_SOCKET_TRANSFER;
3182 break;
3183 }
3184 }
3185 }
3186 }
3187
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003188 /* we might have to unbind some proxies from some processes */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003189 px = proxies_list;
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003190 while (px != NULL) {
3191 if (px->bind_proc && px->state != PR_STSTOPPED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02003192 if (!(px->bind_proc & (1UL << proc))) {
3193 if (global.tune.options & GTUNE_SOCKET_TRANSFER)
3194 zombify_proxy(px);
3195 else
3196 stop_proxy(px);
3197 }
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003198 }
3199 px = px->next;
3200 }
3201
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003202 /* we might have to unbind some peers sections from some processes */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003203 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003204 if (!curpeers->peers_fe)
3205 continue;
3206
3207 if (curpeers->peers_fe->bind_proc & (1UL << proc))
3208 continue;
3209
3210 stop_proxy(curpeers->peers_fe);
3211 /* disable this peer section so that it kills itself */
Willy Tarreau47c8c022015-09-28 16:39:25 +02003212 signal_unregister_handler(curpeers->sighandler);
3213 task_delete(curpeers->sync_task);
3214 task_free(curpeers->sync_task);
3215 curpeers->sync_task = NULL;
3216 task_free(curpeers->peers_fe->task);
3217 curpeers->peers_fe->task = NULL;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003218 curpeers->peers_fe = NULL;
3219 }
3220
William Lallemand2e8fad92018-11-13 16:18:23 +01003221 /*
3222 * This is only done in daemon mode because we might want the
3223 * logs on stdout in mworker mode. If we're NOT in QUIET mode,
3224 * we should now close the 3 first FDs to ensure that we can
3225 * detach from the TTY. We MUST NOT do it in other cases since
3226 * it would have already be done, and 0-2 would have been
3227 * affected to listening sockets
Willy Tarreaubaaee002006-06-26 02:48:02 +02003228 */
William Lallemand2e8fad92018-11-13 16:18:23 +01003229 if ((global.mode & MODE_DAEMON) &&
3230 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003231 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01003232 stdio_quiet(devnullfd);
Willy Tarreau106cb762008-11-16 07:40:34 +01003233 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003234 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
3235 }
3236 pid = getpid(); /* update child's pid */
William Lallemandbfd8eb52018-07-04 15:31:23 +02003237 if (!(global.mode & MODE_MWORKER)) /* in mworker mode we don't want a new pgid for the children */
3238 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02003239 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02003240 }
3241
Christopher Faulete3a5e352017-10-24 13:53:54 +02003242 global.mode &= ~MODE_STARTING;
Willy Tarreau4f60f162007-04-08 16:39:58 +02003243 /*
3244 * That's it : the central polling loop. Run until we stop.
3245 */
Christopher Faulet1d17c102017-08-29 15:38:48 +02003246#ifdef USE_THREAD
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003247 {
Christopher Faulet1d17c102017-08-29 15:38:48 +02003248 unsigned int *tids = calloc(global.nbthread, sizeof(unsigned int));
3249 pthread_t *threads = calloc(global.nbthread, sizeof(pthread_t));
3250 int i;
William Lallemand1aab50b2018-06-07 09:46:01 +02003251 sigset_t blocked_sig, old_sig;
Christopher Faulet1d17c102017-08-29 15:38:48 +02003252
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003253 /* Init tids array */
3254 for (i = 0; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02003255 tids[i] = i;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003256
William Lallemand1aab50b2018-06-07 09:46:01 +02003257 /* ensure the signals will be blocked in every thread */
3258 sigfillset(&blocked_sig);
3259 sigdelset(&blocked_sig, SIGPROF);
3260 sigdelset(&blocked_sig, SIGBUS);
3261 sigdelset(&blocked_sig, SIGFPE);
3262 sigdelset(&blocked_sig, SIGILL);
3263 sigdelset(&blocked_sig, SIGSEGV);
3264 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
3265
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003266 /* Create nbthread-1 thread. The first thread is the current process */
3267 threads[0] = pthread_self();
3268 for (i = 1; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02003269 pthread_create(&threads[i], NULL, &run_thread_poll_loop, &tids[i]);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003270
Christopher Faulet62519022017-10-16 15:49:32 +02003271#ifdef USE_CPU_AFFINITY
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003272 /* Now the CPU affinity for all threads */
3273 for (i = 0; i < global.nbthread; i++) {
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003274 if (global.cpu_map.proc[relative_pid-1])
3275 global.cpu_map.thread[relative_pid-1][i] &= global.cpu_map.proc[relative_pid-1];
Christopher Faulet62519022017-10-16 15:49:32 +02003276
Willy Tarreau421f02e2018-01-20 18:19:22 +01003277 if (i < MAX_THREADS && /* only the first 32/64 threads may be pinned */
Olivier Houchard829aa242017-12-01 18:19:43 +01003278 global.cpu_map.thread[relative_pid-1][i]) {/* only do this if the thread has a THREAD map */
3279#if defined(__FreeBSD__) || defined(__NetBSD__)
3280 cpuset_t cpuset;
3281#else
3282 cpu_set_t cpuset;
3283#endif
3284 int j;
3285 unsigned long cpu_map = global.cpu_map.thread[relative_pid-1][i];
3286
3287 CPU_ZERO(&cpuset);
3288
3289 while ((j = ffsl(cpu_map)) > 0) {
3290 CPU_SET(j - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003291 cpu_map &= ~(1UL << (j - 1));
Olivier Houchard829aa242017-12-01 18:19:43 +01003292 }
Christopher Faulet62519022017-10-16 15:49:32 +02003293 pthread_setaffinity_np(threads[i],
Olivier Houchard829aa242017-12-01 18:19:43 +01003294 sizeof(cpuset), &cpuset);
3295 }
Christopher Faulet1d17c102017-08-29 15:38:48 +02003296 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003297#endif /* !USE_CPU_AFFINITY */
3298
William Lallemand1aab50b2018-06-07 09:46:01 +02003299 /* when multithreading we need to let only the thread 0 handle the signals */
William Lallemandd3801c12018-09-11 10:06:23 +02003300 haproxy_unblock_signals();
William Lallemand1aab50b2018-06-07 09:46:01 +02003301
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003302 /* Finally, start the poll loop for the first thread */
3303 run_thread_poll_loop(&tids[0]);
3304
3305 /* Wait the end of other threads */
3306 for (i = 1; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02003307 pthread_join(threads[i], NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003308
Christopher Faulet1d17c102017-08-29 15:38:48 +02003309 free(tids);
3310 free(threads);
3311
Christopher Fauletb79a94c2017-05-30 15:34:30 +02003312#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
3313 show_lock_stats();
3314#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003315 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003316#else /* ! USE_THREAD */
William Lallemandd3801c12018-09-11 10:06:23 +02003317 haproxy_unblock_signals();
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003318 run_thread_poll_loop((int []){0});
Christopher Faulet62519022017-10-16 15:49:32 +02003319
Christopher Faulet62519022017-10-16 15:49:32 +02003320#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003321
3322 /* Do some cleanup */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003323 deinit();
Christopher Faulet1d17c102017-08-29 15:38:48 +02003324
Willy Tarreaubaaee002006-06-26 02:48:02 +02003325 exit(0);
3326}
3327
3328
3329/*
3330 * Local variables:
3331 * c-indent-level: 8
3332 * c-basic-offset: 8
3333 * End:
3334 */