blob: 54ab7c865a2ee4ad58393ae4d3a0c40ca83d6659 [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;
707
708 mworker_block_signals();
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100709#if defined(USE_SYSTEMD)
710 if (global.tune.options & GTUNE_USE_SYSTEMD)
711 sd_notify(0, "RELOADING=1");
712#endif
William Lallemand73b85e72017-06-01 17:38:51 +0200713 setenv("HAPROXY_MWORKER_REEXEC", "1", 1);
714
William Lallemandbc193052018-09-11 10:06:26 +0200715 mworker_proc_list_to_env(); /* put the children description in the env */
716
William Lallemand73b85e72017-06-01 17:38:51 +0200717 /* compute length */
718 while (next_argv[next_argc])
719 next_argc++;
720
William Lallemand85b0bd92017-06-01 17:38:53 +0200721 /* 1 for haproxy -sf, 2 for -x /socket */
722 next_argv = realloc(next_argv, (next_argc + 1 + 2 + global.nbproc + nb_oldpids + 1) * sizeof(char *));
William Lallemand73b85e72017-06-01 17:38:51 +0200723 if (next_argv == NULL)
724 goto alloc_error;
725
726
727 /* add -sf <PID>* to argv */
728 if (children || nb_oldpids > 0)
729 next_argv[next_argc++] = "-sf";
730 if (children) {
731 for (j = 0; j < global.nbproc; next_argc++,j++) {
732 next_argv[next_argc] = memprintf(&msg, "%d", children[j]);
733 if (next_argv[next_argc] == NULL)
734 goto alloc_error;
735 msg = NULL;
736 }
737 }
738 /* copy old process PIDs */
739 for (j = 0; j < nb_oldpids; next_argc++,j++) {
740 next_argv[next_argc] = memprintf(&msg, "%d", oldpids[j]);
741 if (next_argv[next_argc] == NULL)
742 goto alloc_error;
743 msg = NULL;
744 }
745 next_argv[next_argc] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200746
William Lallemand2bf6d622017-06-20 11:20:23 +0200747 /* add the -x option with the stat socket */
William Lallemand85b0bd92017-06-01 17:38:53 +0200748 if (cur_unixsocket) {
749
William Lallemand2bf6d622017-06-20 11:20:23 +0200750 next_argv[next_argc++] = "-x";
751 next_argv[next_argc++] = (char *)cur_unixsocket;
752 next_argv[next_argc++] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200753 }
754
Christopher Faulet767a84b2017-11-24 16:50:31 +0100755 ha_warning("Reexecuting Master process\n");
Tim Duesterhus0436ab72017-11-12 17:39:18 +0100756 execvp(next_argv[0], next_argv);
William Lallemand73b85e72017-06-01 17:38:51 +0200757
Christopher Faulet767a84b2017-11-24 16:50:31 +0100758 ha_warning("Failed to reexecute the master process [%d]: %s\n", pid, strerror(errno));
William Lallemand722d4ca2017-11-15 19:02:55 +0100759 return;
760
William Lallemand73b85e72017-06-01 17:38:51 +0200761alloc_error:
Joseph Herlant07a08342018-11-15 10:43:05 -0800762 ha_warning("Failed to reexecute the master process [%d]: Cannot allocate memory\n", pid);
William Lallemand73b85e72017-06-01 17:38:51 +0200763 return;
764}
765
William Lallemand73b85e72017-06-01 17:38:51 +0200766/*
William Lallemandb3f2be32018-09-11 10:06:18 +0200767 * When called, this function reexec haproxy with -sf followed by current
Joseph Herlant03420902018-11-15 10:41:50 -0800768 * children PIDs and possibly old children PIDs if they didn't leave yet.
William Lallemand73b85e72017-06-01 17:38:51 +0200769 */
William Lallemandb3f2be32018-09-11 10:06:18 +0200770static void mworker_catch_sighup(struct sig_handler *sh)
William Lallemand73b85e72017-06-01 17:38:51 +0200771{
William Lallemandb3f2be32018-09-11 10:06:18 +0200772 mworker_reload();
773}
William Lallemand73b85e72017-06-01 17:38:51 +0200774
William Lallemandb3f2be32018-09-11 10:06:18 +0200775static void mworker_catch_sigterm(struct sig_handler *sh)
776{
777 int sig = sh->arg;
William Lallemand2f8b31c2017-11-15 19:02:56 +0100778
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100779#if defined(USE_SYSTEMD)
William Lallemandb3f2be32018-09-11 10:06:18 +0200780 if (global.tune.options & GTUNE_USE_SYSTEMD) {
781 sd_notify(0, "STOPPING=1");
782 }
Tim Duesterhusd6942c82017-11-20 15:58:35 +0100783#endif
William Lallemandb3f2be32018-09-11 10:06:18 +0200784 ha_warning("Exiting Master process...\n");
785 mworker_kill(sig);
786}
William Lallemand73b85e72017-06-01 17:38:51 +0200787
William Lallemandb3f2be32018-09-11 10:06:18 +0200788/*
789 * Wait for every children to exit
790 */
William Lallemand73b85e72017-06-01 17:38:51 +0200791
William Lallemandb3f2be32018-09-11 10:06:18 +0200792static void mworker_catch_sigchld(struct sig_handler *sh)
793{
794 int exitpid = -1;
795 int status = 0;
William Lallemandbc193052018-09-11 10:06:26 +0200796 struct mworker_proc *child, *it;
William Lallemand73b85e72017-06-01 17:38:51 +0200797
William Lallemandb3f2be32018-09-11 10:06:18 +0200798restart_wait:
William Lallemand73b85e72017-06-01 17:38:51 +0200799
William Lallemandb3f2be32018-09-11 10:06:18 +0200800 exitpid = waitpid(-1, &status, WNOHANG);
801 if (exitpid > 0) {
William Lallemand73b85e72017-06-01 17:38:51 +0200802 if (WIFEXITED(status))
803 status = WEXITSTATUS(status);
804 else if (WIFSIGNALED(status))
805 status = 128 + WTERMSIG(status);
806 else if (WIFSTOPPED(status))
807 status = 128 + WSTOPSIG(status);
808 else
809 status = 255;
810
William Lallemandbc193052018-09-11 10:06:26 +0200811 list_for_each_entry_safe(child, it, &proc_list, list) {
812 if (child->pid != exitpid)
813 continue;
814
815 LIST_DEL(&child->list);
816 close(child->ipc_fd[0]);
William Lallemandbc193052018-09-11 10:06:26 +0200817 break;
818 }
819
William Lallemand73b85e72017-06-01 17:38:51 +0200820 if (!children) {
William Lallemand18e52a82018-11-06 17:37:13 +0100821 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 +0200822 } else {
823 /* check if exited child was in the current children list */
824 if (current_child(exitpid)) {
William Lallemand18e52a82018-11-06 17:37:13 +0100825 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 +0200826 if (status != 0 && status != 130 && status != 143
William Lallemand4cfede82017-11-24 22:02:34 +0100827 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100828 ha_alert("exit-on-failure: killing every workers with SIGTERM\n");
William Lallemand91723742018-11-06 17:37:14 +0100829 if (exitcode < 0)
830 exitcode = status;
William Lallemand69f9b3b2017-06-01 17:38:54 +0200831 mworker_kill(SIGTERM);
832 }
William Lallemand73b85e72017-06-01 17:38:51 +0200833 } else {
William Lallemand18e52a82018-11-06 17:37:13 +0100834 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 +0200835 delete_oldpid(exitpid);
836 }
837 }
William Lallemand18e52a82018-11-06 17:37:13 +0100838 free(child);
839
William Lallemandb3f2be32018-09-11 10:06:18 +0200840 /* do it again to check if it was the last worker */
841 goto restart_wait;
William Lallemand73b85e72017-06-01 17:38:51 +0200842 }
William Lallemandb3f2be32018-09-11 10:06:18 +0200843 /* Better rely on the system than on a list of process to check if it was the last one */
844 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand18e52a82018-11-06 17:37:13 +0100845 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : status);
William Lallemandb3f2be32018-09-11 10:06:18 +0200846 atexit_flag = 0;
William Lallemand91723742018-11-06 17:37:14 +0100847 if (exitcode > 0)
848 exit(exitcode);
William Lallemandb3f2be32018-09-11 10:06:18 +0200849 exit(status); /* parent must leave using the latest status code known */
850 }
851
William Lallemand73b85e72017-06-01 17:38:51 +0200852}
853
William Lallemandb3f2be32018-09-11 10:06:18 +0200854static void mworker_loop()
855{
856
857#if defined(USE_SYSTEMD)
858 if (global.tune.options & GTUNE_USE_SYSTEMD)
859 sd_notifyf(0, "READY=1\nMAINPID=%lu", (unsigned long)getpid());
860#endif
861
William Lallemandbc193052018-09-11 10:06:26 +0200862 master = 1;
863
William Lallemand0564d412018-11-20 17:36:53 +0100864 signal_unregister(SIGUSR1);
865 signal_unregister(SIGHUP);
866 signal_unregister(SIGQUIT);
867
William Lallemandb3f2be32018-09-11 10:06:18 +0200868 signal_register_fct(SIGTERM, mworker_catch_sigterm, SIGTERM);
869 signal_register_fct(SIGUSR1, mworker_catch_sigterm, SIGUSR1);
870 signal_register_fct(SIGINT, mworker_catch_sigterm, SIGINT);
871 signal_register_fct(SIGHUP, mworker_catch_sighup, SIGHUP);
872 signal_register_fct(SIGUSR2, mworker_catch_sighup, SIGUSR2);
873 signal_register_fct(SIGCHLD, mworker_catch_sigchld, SIGCHLD);
874
875 mworker_unblock_signals();
876 mworker_cleanlisteners();
877
William Lallemandbc193052018-09-11 10:06:26 +0200878 mworker_catch_sigchld(NULL); /* ensure we clean the children in case
879 some SIGCHLD were lost */
880
William Lallemandb3f2be32018-09-11 10:06:18 +0200881 global.nbthread = 1;
882 relative_pid = 1;
883 pid_bit = 1;
884
885 jobs++; /* this is the "master" job, we want to take care of the
886 signals even if there is no listener so the poll loop don't
887 leave */
888
889 fork_poller();
890 run_thread_poll_loop((int []){0});
891}
William Lallemandcb11fd22017-06-01 17:38:52 +0200892
893/*
894 * Reexec the process in failure mode, instead of exiting
895 */
896void reexec_on_failure()
897{
898 if (!atexit_flag)
899 return;
900
901 setenv("HAPROXY_MWORKER_WAIT_ONLY", "1", 1);
902
Christopher Faulet767a84b2017-11-24 16:50:31 +0100903 ha_warning("Reexecuting Master process in waitpid mode\n");
William Lallemandcb11fd22017-06-01 17:38:52 +0200904 mworker_reload();
William Lallemandcb11fd22017-06-01 17:38:52 +0200905}
William Lallemand73b85e72017-06-01 17:38:51 +0200906
907
908/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200909 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
910 * a signal zero to all subscribers. This means that it's as easy as
911 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200912 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100913static void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200914{
915 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200916 signal_unregister_handler(sh);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100917 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918}
919
920/*
921 * upon SIGTTOU, we pause everything
922 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100923static void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924{
925 pause_proxies();
Willy Tarreaubafbe012017-11-24 17:34:44 +0100926 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200927}
928
929/*
930 * upon SIGTTIN, let's have a soft stop.
931 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100932static void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933{
Willy Tarreaube58c382011-07-24 18:28:10 +0200934 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935}
936
937/*
938 * this function dumps every server's state when the process receives SIGHUP.
939 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100940static void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200941{
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100942 struct proxy *p = proxies_list;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943
Christopher Faulet767a84b2017-11-24 16:50:31 +0100944 ha_warning("SIGHUP received, dumping servers states.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945 while (p) {
946 struct server *s = p->srv;
947
948 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
949 while (s) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100950 chunk_printf(&trash,
951 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
952 p->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +0200953 (s->cur_state != SRV_ST_STOPPED) ? "UP" : "DOWN",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100954 s->cur_sess, s->nbpend, s->counters.cum_sess);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200955 ha_warning("%s\n", trash.area);
956 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200957 s = s->next;
958 }
959
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200960 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
961 if (!p->srv) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100962 chunk_printf(&trash,
963 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
964 p->id,
965 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 +0200966 } else if (p->srv_act == 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100967 chunk_printf(&trash,
968 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
969 p->id,
970 (p->srv_bck) ? "is running on backup servers" : "has no server available",
971 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 +0200972 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100973 chunk_printf(&trash,
974 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
975 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
976 p->id, p->srv_act, p->srv_bck,
977 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 +0200978 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200979 ha_warning("%s\n", trash.area);
980 send_log(p, LOG_NOTICE, "%s\n", trash.area);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200981
982 p = p->next;
983 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200984}
985
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100986static void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200987{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200988 /* dump memory usage then free everything possible */
989 dump_pools();
Willy Tarreaubafbe012017-11-24 17:34:44 +0100990 pool_gc(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200991}
992
William Lallemande1340412017-12-28 16:09:36 +0100993/*
994 * This function dup2 the stdio FDs (0,1,2) with <fd>, then closes <fd>
995 * If <fd> < 0, it opens /dev/null and use it to dup
996 *
997 * In the case of chrooting, you have to open /dev/null before the chroot, and
998 * pass the <fd> to this function
999 */
1000static void stdio_quiet(int fd)
1001{
1002 if (fd < 0)
1003 fd = open("/dev/null", O_RDWR, 0);
1004
1005 if (fd > -1) {
1006 fclose(stdin);
1007 fclose(stdout);
1008 fclose(stderr);
1009
1010 dup2(fd, 0);
1011 dup2(fd, 1);
1012 dup2(fd, 2);
1013 if (fd > 2)
1014 close(fd);
1015 return;
1016 }
1017
1018 ha_alert("Cannot open /dev/null\n");
1019 exit(EXIT_FAILURE);
1020}
1021
1022
Joseph Herlant03420902018-11-15 10:41:50 -08001023/* This function checks if cfg_cfgfiles contains directories.
1024 * If it finds one, it adds all the files (and only files) it contains
1025 * in cfg_cfgfiles in place of the directory (and removes the directory).
1026 * It adds the files in lexical order.
1027 * It adds only files with .cfg extension.
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001028 * It doesn't add files with name starting with '.'
1029 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001030static void cfgfiles_expand_directories(void)
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001031{
1032 struct wordlist *wl, *wlb;
1033 char *err = NULL;
1034
1035 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
1036 struct stat file_stat;
1037 struct dirent **dir_entries = NULL;
1038 int dir_entries_nb;
1039 int dir_entries_it;
1040
1041 if (stat(wl->s, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001042 ha_alert("Cannot open configuration file/directory %s : %s\n",
1043 wl->s,
1044 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001045 exit(1);
1046 }
1047
1048 if (!S_ISDIR(file_stat.st_mode))
1049 continue;
1050
1051 /* from this point wl->s is a directory */
1052
1053 dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort);
1054 if (dir_entries_nb < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001055 ha_alert("Cannot open configuration directory %s : %s\n",
1056 wl->s,
1057 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001058 exit(1);
1059 }
1060
1061 /* for each element in the directory wl->s */
1062 for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) {
1063 struct dirent *dir_entry = dir_entries[dir_entries_it];
1064 char *filename = NULL;
1065 char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg");
1066
1067 /* don't add filename that begin with .
Joseph Herlant03420902018-11-15 10:41:50 -08001068 * only add filename with .cfg extension
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001069 */
1070 if (dir_entry->d_name[0] == '.' ||
1071 !(d_name_cfgext && d_name_cfgext[4] == '\0'))
1072 goto next_dir_entry;
1073
1074 if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001075 ha_alert("Cannot load configuration files %s : out of memory.\n",
1076 filename);
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001077 exit(1);
1078 }
1079
1080 if (stat(filename, &file_stat)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001081 ha_alert("Cannot open configuration file %s : %s\n",
1082 wl->s,
1083 strerror(errno));
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001084 exit(1);
1085 }
1086
1087 /* don't add anything else than regular file in cfg_cfgfiles
1088 * this way we avoid loops
1089 */
1090 if (!S_ISREG(file_stat.st_mode))
1091 goto next_dir_entry;
1092
1093 if (!list_append_word(&wl->list, filename, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001094 ha_alert("Cannot load configuration files %s : %s\n",
1095 filename,
1096 err);
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001097 exit(1);
1098 }
1099
1100next_dir_entry:
1101 free(filename);
1102 free(dir_entry);
1103 }
1104
1105 free(dir_entries);
1106
1107 /* remove the current directory (wl) from cfg_cfgfiles */
1108 free(wl->s);
1109 LIST_DEL(&wl->list);
1110 free(wl);
1111 }
1112
1113 free(err);
1114}
1115
Olivier Houchardf73629d2017-04-05 22:33:04 +02001116static int get_old_sockets(const char *unixsocket)
1117{
1118 char *cmsgbuf = NULL, *tmpbuf = NULL;
1119 int *tmpfd = NULL;
1120 struct sockaddr_un addr;
1121 struct cmsghdr *cmsg;
1122 struct msghdr msghdr;
1123 struct iovec iov;
1124 struct xfer_sock_list *xfer_sock = NULL;
Olivier Houchard54740872017-04-06 14:45:14 +02001125 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf73629d2017-04-05 22:33:04 +02001126 int sock = -1;
1127 int ret = -1;
1128 int ret2 = -1;
1129 int fd_nb;
1130 int got_fd = 0;
1131 int i = 0;
1132 size_t maxoff = 0, curoff = 0;
1133
1134 memset(&msghdr, 0, sizeof(msghdr));
1135 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
1136 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001137 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001138 goto out;
1139 }
1140 sock = socket(PF_UNIX, SOCK_STREAM, 0);
1141 if (sock < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001142 ha_warning("Failed to connect to the old process socket '%s'\n",
1143 unixsocket);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001144 goto out;
1145 }
1146 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path));
1147 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
1148 addr.sun_family = PF_UNIX;
1149 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
1150 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001151 ha_warning("Failed to connect to the old process socket '%s'\n",
1152 unixsocket);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001153 goto out;
1154 }
Olivier Houchard54740872017-04-06 14:45:14 +02001155 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001156 iov.iov_base = &fd_nb;
1157 iov.iov_len = sizeof(fd_nb);
1158 msghdr.msg_iov = &iov;
1159 msghdr.msg_iovlen = 1;
1160 send(sock, "_getsocks\n", strlen("_getsocks\n"), 0);
1161 /* First, get the number of file descriptors to be received */
1162 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001163 ha_warning("Failed to get the number of sockets to be transferred !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001164 goto out;
1165 }
1166 if (fd_nb == 0) {
1167 ret = 0;
1168 goto out;
1169 }
Olivier Houchardf143b802017-11-04 15:13:01 +01001170 tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001171 if (tmpbuf == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001172 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001173 goto out;
1174 }
1175 tmpfd = malloc(fd_nb * sizeof(int));
1176 if (tmpfd == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001177 ha_warning("Failed to allocate memory while receiving sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001178 goto out;
1179 }
1180 msghdr.msg_control = cmsgbuf;
1181 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
Olivier Houchardf143b802017-11-04 15:13:01 +01001182 iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001183 do {
1184 int ret3;
1185
1186 iov.iov_base = tmpbuf + curoff;
1187 ret = recvmsg(sock, &msghdr, 0);
1188 if (ret == -1 && errno == EINTR)
1189 continue;
1190 if (ret <= 0)
1191 break;
1192 /* Send an ack to let the sender know we got the sockets
1193 * and it can send some more
1194 */
1195 do {
1196 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
1197 } while (ret3 == -1 && errno == EINTR);
1198 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
1199 cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
1200 if (cmsg->cmsg_level == SOL_SOCKET &&
1201 cmsg->cmsg_type == SCM_RIGHTS) {
1202 size_t totlen = cmsg->cmsg_len -
1203 CMSG_LEN(0);
1204 if (totlen / sizeof(int) + got_fd > fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001205 ha_warning("Got to many sockets !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001206 goto out;
1207 }
1208 /*
1209 * Be paranoid and use memcpy() to avoid any
1210 * potential alignement issue.
1211 */
1212 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
1213 got_fd += totlen / sizeof(int);
1214 }
1215 }
1216 curoff += ret;
1217 } while (got_fd < fd_nb);
1218
1219 if (got_fd != fd_nb) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001220 ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
1221 fd_nb, got_fd);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001222 goto out;
1223 }
1224 maxoff = curoff;
1225 curoff = 0;
1226 for (i = 0; i < got_fd; i++) {
1227 int fd = tmpfd[i];
1228 socklen_t socklen;
1229 int len;
1230
1231 xfer_sock = calloc(1, sizeof(*xfer_sock));
1232 if (!xfer_sock) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001233 ha_warning("Failed to allocate memory in get_old_sockets() !\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001234 break;
1235 }
1236 xfer_sock->fd = -1;
1237
1238 socklen = sizeof(xfer_sock->addr);
1239 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001240 ha_warning("Failed to get socket address\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001241 free(xfer_sock);
Olivier Houchardbe7b1ce2017-07-17 17:25:33 +02001242 xfer_sock = NULL;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001243 continue;
1244 }
1245 if (curoff >= maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001246 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001247 goto out;
1248 }
1249 len = tmpbuf[curoff++];
1250 if (len > 0) {
1251 /* We have a namespace */
1252 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001253 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001254 goto out;
1255 }
1256 xfer_sock->namespace = malloc(len + 1);
1257 if (!xfer_sock->namespace) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001258 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001259 goto out;
1260 }
1261 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
1262 xfer_sock->namespace[len] = 0;
1263 curoff += len;
1264 }
1265 if (curoff >= 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 len = tmpbuf[curoff++];
1270 if (len > 0) {
1271 /* We have an interface */
1272 if (curoff + len > maxoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001273 ha_warning("Inconsistency while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001274 goto out;
1275 }
1276 xfer_sock->iface = malloc(len + 1);
1277 if (!xfer_sock->iface) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001278 ha_warning("Failed to allocate memory while transferring sockets\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001279 goto out;
1280 }
1281 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
Olivier Houchard33e083c2018-03-15 17:48:49 +01001282 xfer_sock->iface[len] = 0;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001283 curoff += len;
1284 }
1285 if (curoff + sizeof(int) > 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 memcpy(&xfer_sock->options, &tmpbuf[curoff],
1290 sizeof(xfer_sock->options));
1291 curoff += sizeof(xfer_sock->options);
1292
1293 xfer_sock->fd = fd;
1294 if (xfer_sock_list)
1295 xfer_sock_list->prev = xfer_sock;
1296 xfer_sock->next = xfer_sock_list;
1297 xfer_sock->prev = NULL;
1298 xfer_sock_list = xfer_sock;
1299 xfer_sock = NULL;
1300 }
1301
1302 ret2 = 0;
1303out:
1304 /* If we failed midway make sure to close the remaining
1305 * file descriptors
1306 */
1307 if (tmpfd != NULL && i < got_fd) {
1308 for (; i < got_fd; i++) {
1309 close(tmpfd[i]);
1310 }
1311 }
1312 free(tmpbuf);
1313 free(tmpfd);
1314 free(cmsgbuf);
1315 if (sock != -1)
1316 close(sock);
1317 if (xfer_sock) {
1318 free(xfer_sock->namespace);
1319 free(xfer_sock->iface);
1320 if (xfer_sock->fd != -1)
1321 close(xfer_sock->fd);
1322 free(xfer_sock);
1323 }
1324 return (ret2);
1325}
1326
Willy Tarreaubaaee002006-06-26 02:48:02 +02001327/*
William Lallemand73b85e72017-06-01 17:38:51 +02001328 * copy and cleanup the current argv
1329 * Remove the -sf /-st parameters
1330 * Return an allocated copy of argv
1331 */
1332
1333static char **copy_argv(int argc, char **argv)
1334{
1335 char **newargv;
William Lallemand2bf6d622017-06-20 11:20:23 +02001336 int i = 0, j = 0;
William Lallemand73b85e72017-06-01 17:38:51 +02001337
1338 newargv = calloc(argc + 2, sizeof(char *));
1339 if (newargv == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001340 ha_warning("Cannot allocate memory\n");
William Lallemand73b85e72017-06-01 17:38:51 +02001341 return NULL;
1342 }
1343
William Lallemand2bf6d622017-06-20 11:20:23 +02001344 while (i < argc) {
1345 /* -sf or -st or -x */
William Lallemand29f690c2018-01-09 23:12:27 +01001346 if (i > 0 && argv[i][0] == '-' &&
1347 ((argv[i][1] == 's' && (argv[i][2] == 'f' || argv[i][2] == 't')) || argv[i][1] == 'x' )) {
William Lallemand2bf6d622017-06-20 11:20:23 +02001348 /* list of pids to finish ('f') or terminate ('t') or unix socket (-x) */
William Lallemand73b85e72017-06-01 17:38:51 +02001349 i++;
1350 while (i < argc && argv[i][0] != '-') {
1351 i++;
1352 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001353 continue;
William Lallemand73b85e72017-06-01 17:38:51 +02001354 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001355
1356 newargv[j++] = argv[i++];
William Lallemand73b85e72017-06-01 17:38:51 +02001357 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001358
William Lallemand73b85e72017-06-01 17:38:51 +02001359 return newargv;
1360}
1361
1362/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001363 * This function initializes all the necessary variables. It only returns
1364 * if everything is OK. If something fails, it exits.
1365 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001366static void init(int argc, char **argv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001367{
Willy Tarreaubaaee002006-06-26 02:48:02 +02001368 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001369 char *tmp;
1370 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +02001371 int err_code = 0;
Maxime de Roucy0f503922016-05-13 23:52:55 +02001372 char *err_msg = NULL;
Willy Tarreau477ecd82010-01-03 21:12:30 +01001373 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +00001374 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +02001375 char *change_dir = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001376 struct proxy *px;
Willy Tarreaue6945732016-12-21 19:57:00 +01001377 struct post_check_fct *pcf;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001378
Christopher Faulete3a5e352017-10-24 13:53:54 +02001379 global.mode = MODE_STARTING;
William Lallemand73b85e72017-06-01 17:38:51 +02001380 next_argv = copy_argv(argc, argv);
1381
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001382 if (!init_trash_buffers(1)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001383 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet748919a2017-07-26 14:59:46 +02001384 exit(1);
1385 }
David du Colombier7af46052012-05-16 14:16:48 +02001386
Emeric Brun2b920a12010-09-23 18:30:22 +02001387 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
1388 * the string in case of truncation, and at least FreeBSD appears not to do
1389 * it.
1390 */
1391 memset(hostname, 0, sizeof(hostname));
1392 gethostname(hostname, sizeof(hostname) - 1);
1393 memset(localpeer, 0, sizeof(localpeer));
1394 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
William Lallemanddaf4cd22018-04-17 16:46:13 +02001395 setenv("HAPROXY_LOCALPEER", localpeer, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02001396
Willy Tarreaubaaee002006-06-26 02:48:02 +02001397 /*
1398 * Initialize the previously static variables.
1399 */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001400
Willy Tarreau173d9952018-01-26 21:48:23 +01001401 totalconn = actconn = listeners = stopping = 0;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001402 killed = 0;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001403
Willy Tarreaubaaee002006-06-26 02:48:02 +02001404
1405#ifdef HAPROXY_MEMMAX
Willy Tarreau70060452015-12-14 12:46:07 +01001406 global.rlimit_memmax_all = HAPROXY_MEMMAX;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001407#endif
1408
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001409 tzset();
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001410 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001411 start_date = now;
1412
Willy Tarreau84310e22014-02-14 11:59:04 +01001413 srandom(now_ms - getpid());
1414
Dragan Dosen835b9212016-02-12 13:23:03 +01001415 init_log();
Willy Tarreau29857942009-05-10 09:01:21 +02001416 signal_init();
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001417 if (init_acl() != 0)
1418 exit(1);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001419 init_task();
Willy Tarreau87b09662015-04-03 00:22:06 +02001420 init_stream();
Willy Tarreaub1ec8c42015-04-03 13:53:24 +02001421 init_session();
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001422 init_connection();
Willy Tarreau8280d642009-09-23 23:37:52 +02001423 /* warning, we init buffers later */
Willy Tarreaue4d7e552007-05-13 20:19:55 +02001424 init_pendconn();
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02001425 if (!init_http(&err_msg)) {
1426 ha_alert("%s. Aborting.\n", err_msg);
1427 free(err_msg);
1428 abort();
1429 }
Willy Tarreau80587432006-12-24 17:47:20 +01001430 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001431
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001432 /* Initialise lua. */
1433 hlua_init();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001434
Christopher Fauletff2613e2016-11-09 11:36:17 +01001435 /* Initialize process vars */
1436 vars_init(&global.vars, SCOPE_PROC);
1437
Willy Tarreau43b78992009-01-25 15:42:27 +01001438 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001439#if defined(ENABLE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001440 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001441#endif
1442#if defined(ENABLE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001443 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001444#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001445#if defined(ENABLE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +01001446 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001447#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001448#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001449 global.tune.options |= GTUNE_USE_SPLICE;
1450#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001451#if defined(USE_GETADDRINFO)
1452 global.tune.options |= GTUNE_USE_GAI;
1453#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001454#if defined(SO_REUSEPORT)
1455 global.tune.options |= GTUNE_USE_REUSEPORT;
1456#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001457
1458 pid = getpid();
1459 progname = *argv;
1460 while ((tmp = strchr(progname, '/')) != NULL)
1461 progname = tmp + 1;
1462
Kevinm48936af2010-12-22 16:08:21 +00001463 /* the process name is used for the logs only */
Dragan Dosen43885c72015-10-01 13:18:13 +02001464 chunk_initstr(&global.log_tag, strdup(progname));
Kevinm48936af2010-12-22 16:08:21 +00001465
Willy Tarreaubaaee002006-06-26 02:48:02 +02001466 argc--; argv++;
1467 while (argc > 0) {
1468 char *flag;
1469
1470 if (**argv == '-') {
1471 flag = *argv+1;
1472
1473 /* 1 arg */
1474 if (*flag == 'v') {
1475 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +01001476 if (flag[1] == 'v') /* -vv */
1477 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001478 exit(0);
1479 }
1480#if defined(ENABLE_EPOLL)
1481 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +01001482 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001483#endif
1484#if defined(ENABLE_POLL)
1485 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +01001486 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001487#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +02001488#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001489 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +01001490 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001491#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001492#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001493 else if (*flag == 'd' && flag[1] == 'S')
1494 global.tune.options &= ~GTUNE_USE_SPLICE;
1495#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001496#if defined(USE_GETADDRINFO)
1497 else if (*flag == 'd' && flag[1] == 'G')
1498 global.tune.options &= ~GTUNE_USE_GAI;
1499#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001500#if defined(SO_REUSEPORT)
1501 else if (*flag == 'd' && flag[1] == 'R')
1502 global.tune.options &= ~GTUNE_USE_REUSEPORT;
1503#endif
Emeric Brun850efd52014-01-29 12:24:34 +01001504 else if (*flag == 'd' && flag[1] == 'V')
1505 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001506 else if (*flag == 'V')
1507 arg_mode |= MODE_VERBOSE;
1508 else if (*flag == 'd' && flag[1] == 'b')
1509 arg_mode |= MODE_FOREGROUND;
Willy Tarreau6e064432012-05-08 15:40:42 +02001510 else if (*flag == 'd' && flag[1] == 'M')
1511 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreau3eed10e2016-11-07 21:03:16 +01001512 else if (*flag == 'd' && flag[1] == 'r')
1513 global.tune.options |= GTUNE_RESOLVE_DONTFAIL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001514 else if (*flag == 'd')
1515 arg_mode |= MODE_DEBUG;
1516 else if (*flag == 'c')
1517 arg_mode |= MODE_CHECK;
William Lallemand095ba4c2017-06-01 17:38:50 +02001518 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +02001519 arg_mode |= MODE_DAEMON;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001520 else if (*flag == 'W' && flag[1] == 's') {
Lukas Tribusf46bf952017-11-21 12:39:34 +01001521 arg_mode |= MODE_MWORKER | MODE_FOREGROUND;
Tim Duesterhusd6942c82017-11-20 15:58:35 +01001522#if defined(USE_SYSTEMD)
1523 global.tune.options |= GTUNE_USE_SYSTEMD;
1524#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001525 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 +01001526 usage(progname);
1527#endif
1528 }
William Lallemand095ba4c2017-06-01 17:38:50 +02001529 else if (*flag == 'W')
1530 arg_mode |= MODE_MWORKER;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001531 else if (*flag == 'q')
1532 arg_mode |= MODE_QUIET;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001533 else if (*flag == 'x') {
William Lallemand45eff442017-06-19 15:57:55 +02001534 if (argc <= 1 || argv[1][0] == '-') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001535 ha_alert("Unix socket path expected with the -x flag\n\n");
William Lallemand45eff442017-06-19 15:57:55 +02001536 usage(progname);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001537 }
William Lallemand4fc09692017-06-19 16:37:19 +02001538 if (old_unixsocket)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001539 ha_warning("-x option already set, overwriting the value\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001540 old_unixsocket = argv[1];
William Lallemand4fc09692017-06-19 16:37:19 +02001541
Olivier Houchardf73629d2017-04-05 22:33:04 +02001542 argv++;
1543 argc--;
1544 }
William Lallemande7361152018-10-26 14:47:36 +02001545 else if (*flag == 'S') {
1546 struct wordlist *c;
1547
1548 if (argc <= 1 || argv[1][0] == '-') {
1549 ha_alert("Socket and optional bind parameters expected with the -S flag\n");
1550 usage(progname);
1551 }
1552 if ((c = malloc(sizeof(*c))) == NULL || (c->s = strdup(argv[1])) == NULL) {
1553 ha_alert("Cannot allocate memory\n");
1554 exit(EXIT_FAILURE);
1555 }
1556 LIST_ADD(&mworker_cli_conf, &c->list);
1557
1558 argv++;
1559 argc--;
1560 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001561 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1562 /* list of pids to finish ('f') or terminate ('t') */
1563
1564 if (flag[1] == 'f')
1565 oldpids_sig = SIGUSR1; /* finish then exit */
1566 else
1567 oldpids_sig = SIGTERM; /* terminate immediately */
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001568 while (argc > 1 && argv[1][0] != '-') {
Chris Lane236062f2018-02-05 23:15:44 +00001569 char * endptr = NULL;
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001570 oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int));
1571 if (!oldpids) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001572 ha_alert("Cannot allocate old pid : out of memory.\n");
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001573 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001574 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001575 argc--; argv++;
Chris Lane236062f2018-02-05 23:15:44 +00001576 errno = 0;
1577 oldpids[nb_oldpids] = strtol(*argv, &endptr, 10);
1578 if (errno) {
1579 ha_alert("-%2s option: failed to parse {%s}: %s\n",
1580 flag,
1581 *argv, strerror(errno));
1582 exit(1);
1583 } else if (endptr && strlen(endptr)) {
1584 while (isspace(*endptr)) endptr++;
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001585 if (*endptr != 0) {
Chris Lane236062f2018-02-05 23:15:44 +00001586 ha_alert("-%2s option: some bytes unconsumed in PID list {%s}\n",
1587 flag, endptr);
1588 exit(1);
Aurélien Nephtali39b89882018-02-17 20:53:11 +01001589 }
Chris Lane236062f2018-02-05 23:15:44 +00001590 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001591 if (oldpids[nb_oldpids] <= 0)
1592 usage(progname);
1593 nb_oldpids++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001594 }
1595 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001596 else if (flag[0] == '-' && flag[1] == 0) { /* "--" */
1597 /* now that's a cfgfile list */
1598 argv++; argc--;
1599 while (argc > 0) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02001600 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001601 ha_alert("Cannot load configuration file/directory %s : %s\n",
1602 *argv,
1603 err_msg);
Willy Tarreaua088d312015-10-08 11:58:48 +02001604 exit(1);
1605 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001606 argv++; argc--;
1607 }
1608 break;
1609 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001610 else { /* >=2 args */
1611 argv++; argc--;
1612 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001613 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001614
1615 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +02001616 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001617 case 'n' : cfg_maxconn = atol(*argv); break;
Willy Tarreau70060452015-12-14 12:46:07 +01001618 case 'm' : global.rlimit_memmax_all = atol(*argv); break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001619 case 'N' : cfg_maxpconn = atol(*argv); break;
William Lallemanddaf4cd22018-04-17 16:46:13 +02001620 case 'L' :
1621 strncpy(localpeer, *argv, sizeof(localpeer) - 1);
1622 setenv("HAPROXY_LOCALPEER", localpeer, 1);
1623 break;
Willy Tarreau5d01a632009-06-22 16:02:30 +02001624 case 'f' :
Maxime de Roucy0f503922016-05-13 23:52:55 +02001625 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001626 ha_alert("Cannot load configuration file/directory %s : %s\n",
1627 *argv,
1628 err_msg);
Willy Tarreau5d01a632009-06-22 16:02:30 +02001629 exit(1);
1630 }
Willy Tarreau5d01a632009-06-22 16:02:30 +02001631 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001632 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001633 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001634 }
1635 }
1636 }
1637 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001638 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001639 argv++; argc--;
1640 }
1641
Christopher Faulete3a5e352017-10-24 13:53:54 +02001642 global.mode |= (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
1643 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001644
William Lallemand944e6192018-11-21 15:48:31 +01001645 if (getenv("HAPROXY_MWORKER_WAIT_ONLY")) {
William Lallemandcb11fd22017-06-01 17:38:52 +02001646 unsetenv("HAPROXY_MWORKER_WAIT_ONLY");
William Lallemand944e6192018-11-21 15:48:31 +01001647 global.mode |= MODE_MWORKER_WAIT;
1648 global.mode &= ~MODE_MWORKER;
William Lallemandcb11fd22017-06-01 17:38:52 +02001649 }
1650
1651 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {
1652 atexit_flag = 1;
1653 atexit(reexec_on_failure);
1654 }
1655
Willy Tarreau576132e2011-09-10 19:26:56 +02001656 if (change_dir && chdir(change_dir) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001657 ha_alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
Willy Tarreau576132e2011-09-10 19:26:56 +02001658 exit(1);
1659 }
1660
Willy Tarreaubaaee002006-06-26 02:48:02 +02001661 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001662
1663 init_default_instance();
1664
William Lallemand944e6192018-11-21 15:48:31 +01001665 /* in wait mode, we don't try to read the configuration files */
1666 if (!(global.mode & MODE_MWORKER_WAIT)) {
Willy Tarreauc4382422009-12-06 13:10:44 +01001667
William Lallemand944e6192018-11-21 15:48:31 +01001668 /* handle cfgfiles that are actually directories */
1669 cfgfiles_expand_directories();
1670
1671 if (LIST_ISEMPTY(&cfg_cfgfiles))
1672 usage(progname);
1673
1674
1675 list_for_each_entry(wl, &cfg_cfgfiles, list) {
1676 int ret;
1677
1678 ret = readcfgfile(wl->s);
1679 if (ret == -1) {
1680 ha_alert("Could not open configuration file %s : %s\n",
1681 wl->s, strerror(errno));
1682 exit(1);
1683 }
1684 if (ret & (ERR_ABORT|ERR_FATAL))
1685 ha_alert("Error(s) found in configuration file : %s\n", wl->s);
1686 err_code |= ret;
1687 if (err_code & ERR_ABORT)
1688 exit(1);
Willy Tarreauc4382422009-12-06 13:10:44 +01001689 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001690
William Lallemand944e6192018-11-21 15:48:31 +01001691 /* do not try to resolve arguments nor to spot inconsistencies when
1692 * the configuration contains fatal errors caused by files not found
1693 * or failed memory allocations.
1694 */
1695 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1696 ha_alert("Fatal errors found in configuration.\n");
1697 exit(1);
1698 }
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02001699 }
William Lallemandce83b4a2018-10-26 14:47:30 +02001700 if (global.mode & MODE_MWORKER) {
1701 int proc;
William Lallemand16dd1b32018-11-19 18:46:18 +01001702 struct mworker_proc *tmproc;
1703
1704 if (getenv("HAPROXY_MWORKER_REEXEC") == NULL) {
1705
1706 tmproc = malloc(sizeof(*tmproc));
1707 if (!tmproc) {
1708 ha_alert("Cannot allocate process structures.\n");
1709 exit(EXIT_FAILURE);
1710 }
1711 tmproc->type = 'm'; /* master */
1712 tmproc->reloads = 0;
1713 tmproc->relative_pid = 0;
1714 tmproc->pid = pid;
1715 tmproc->timestamp = start_date.tv_sec;
1716 tmproc->ipc_fd[0] = -1;
1717 tmproc->ipc_fd[1] = -1;
1718
1719 proc_self = tmproc;
1720
1721 LIST_ADDQ(&proc_list, &tmproc->list);
1722 }
William Lallemandce83b4a2018-10-26 14:47:30 +02001723
1724 for (proc = 0; proc < global.nbproc; proc++) {
William Lallemandce83b4a2018-10-26 14:47:30 +02001725
1726 tmproc = malloc(sizeof(*tmproc));
1727 if (!tmproc) {
1728 ha_alert("Cannot allocate process structures.\n");
1729 exit(EXIT_FAILURE);
1730 }
1731
William Lallemand16dd1b32018-11-19 18:46:18 +01001732 tmproc->type = 'w'; /* worker */
William Lallemandce83b4a2018-10-26 14:47:30 +02001733 tmproc->pid = -1;
1734 tmproc->reloads = 0;
William Lallemande3683302018-11-19 18:46:17 +01001735 tmproc->timestamp = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02001736 tmproc->relative_pid = 1 + proc;
William Lallemand550db6d2018-11-06 17:37:12 +01001737 tmproc->ipc_fd[0] = -1;
1738 tmproc->ipc_fd[1] = -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02001739
1740 if (mworker_cli_sockpair_new(tmproc, proc) < 0) {
1741 exit(EXIT_FAILURE);
1742 }
1743
1744 LIST_ADDQ(&proc_list, &tmproc->list);
1745 }
William Lallemand944e6192018-11-21 15:48:31 +01001746 }
1747 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
1748 struct wordlist *it, *c;
1749
William Lallemand1b663612018-10-26 14:47:33 +02001750 mworker_env_to_proc_list(); /* get the info of the children in the env */
William Lallemand8a022572018-10-26 14:47:35 +02001751
William Lallemande7361152018-10-26 14:47:36 +02001752
William Lallemand550db6d2018-11-06 17:37:12 +01001753 if (!LIST_ISEMPTY(&mworker_cli_conf)) {
William Lallemande7361152018-10-26 14:47:36 +02001754
William Lallemand550db6d2018-11-06 17:37:12 +01001755 if (mworker_cli_proxy_create() < 0) {
William Lallemande7361152018-10-26 14:47:36 +02001756 ha_alert("Can't create the master's CLI.\n");
1757 exit(EXIT_FAILURE);
1758 }
William Lallemande7361152018-10-26 14:47:36 +02001759
William Lallemand550db6d2018-11-06 17:37:12 +01001760 list_for_each_entry_safe(c, it, &mworker_cli_conf, list) {
1761
1762 if (mworker_cli_proxy_new_listener(c->s) < 0) {
1763 ha_alert("Can't create the master's CLI.\n");
1764 exit(EXIT_FAILURE);
1765 }
1766 LIST_DEL(&c->list);
1767 free(c->s);
1768 free(c);
1769 }
1770 }
William Lallemandce83b4a2018-10-26 14:47:30 +02001771 }
1772
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001773 pattern_finalize_config();
1774
Willy Tarreaubb925012009-07-23 13:36:36 +02001775 err_code |= check_config_validity();
1776 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001777 ha_alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001778 exit(1);
1779 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001780
Willy Tarreau70060452015-12-14 12:46:07 +01001781 /* recompute the amount of per-process memory depending on nbproc and
1782 * the shared SSL cache size (allowed to exist in all processes).
1783 */
1784 if (global.rlimit_memmax_all) {
1785#if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE)
1786 int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL;
1787
1788 global.rlimit_memmax =
1789 ((((int64_t)global.rlimit_memmax_all * 1048576LL) -
1790 ssl_cache_bytes) / global.nbproc +
1791 ssl_cache_bytes + 1048575LL) / 1048576LL;
1792#else
1793 global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc;
1794#endif
1795 }
1796
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001797#ifdef CONFIG_HAP_NS
1798 err_code |= netns_init();
1799 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001800 ha_alert("Failed to initialize namespace support.\n");
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001801 exit(1);
1802 }
1803#endif
1804
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01001805 /* Apply server states */
1806 apply_server_state();
1807
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001808 for (px = proxies_list; px; px = px->next)
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01001809 srv_compute_all_admin_states(px);
1810
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001811 /* Apply servers' configured address */
1812 err_code |= srv_init_addr();
1813 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001814 ha_alert("Failed to initialize server(s) addr.\n");
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001815 exit(1);
1816 }
1817
Willy Tarreaubaaee002006-06-26 02:48:02 +02001818 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001819 struct peers *pr;
1820 struct proxy *px;
1821
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001822 for (pr = cfg_peers; pr; pr = pr->next)
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001823 if (pr->peers_fe)
1824 break;
1825
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001826 for (px = proxies_list; px; px = px->next)
Willy Tarreau4348fad2012-09-20 16:48:07 +02001827 if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners))
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001828 break;
1829
1830 if (pr || px) {
1831 /* At least one peer or one listener has been found */
1832 qfprintf(stdout, "Configuration file is valid\n");
1833 exit(0);
1834 }
1835 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
1836 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001837 }
1838
Emeric Brunc60def82017-09-27 14:59:38 +02001839 global_listener_queue_task = task_new(MAX_THREADS_MASK);
Willy Tarreaue9b26022011-08-01 20:57:55 +02001840 if (!global_listener_queue_task) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001841 ha_alert("Out of memory when initializing global task\n");
Willy Tarreaue9b26022011-08-01 20:57:55 +02001842 exit(1);
1843 }
1844 /* very simple initialization, users will queue the task if needed */
1845 global_listener_queue_task->context = NULL; /* not even a context! */
1846 global_listener_queue_task->process = manage_global_listener_queue;
Willy Tarreaue9b26022011-08-01 20:57:55 +02001847
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001848 /* now we know the buffer size, we can initialize the channels and buffers */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001849 init_buffer();
Willy Tarreau8280d642009-09-23 23:37:52 +02001850
Willy Tarreaue6945732016-12-21 19:57:00 +01001851 list_for_each_entry(pcf, &post_check_list, list) {
1852 err_code |= pcf->fct();
1853 if (err_code & (ERR_ABORT|ERR_FATAL))
1854 exit(1);
1855 }
1856
Willy Tarreaubaaee002006-06-26 02:48:02 +02001857 if (cfg_maxconn > 0)
1858 global.maxconn = cfg_maxconn;
1859
1860 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +02001861 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001862 global.pidfile = strdup(cfg_pidfile);
1863 }
1864
Willy Tarreaud0256482015-01-15 21:45:22 +01001865 /* Now we want to compute the maxconn and possibly maxsslconn values.
1866 * It's a bit tricky. If memmax is not set, maxconn defaults to
1867 * DEFAULT_MAXCONN and maxsslconn defaults to DEFAULT_MAXSSLCONN.
1868 *
1869 * If memmax is set, then it depends on which values are set. If
1870 * maxsslconn is set, we use memmax to determine how many cleartext
1871 * connections may be added, and set maxconn to the sum of the two.
1872 * If maxconn is set and not maxsslconn, maxsslconn is computed from
1873 * the remaining amount of memory between memmax and the cleartext
1874 * connections. If neither are set, then it is considered that all
1875 * connections are SSL-capable, and maxconn is computed based on this,
1876 * then maxsslconn accordingly. We need to know if SSL is used on the
1877 * frontends, backends, or both, because when it's used on both sides,
1878 * we need twice the value for maxsslconn, but we only count the
1879 * handshake once since it is not performed on the two sides at the
1880 * same time (frontend-side is terminated before backend-side begins).
1881 * The SSL stack is supposed to have filled ssl_session_cost and
Willy Tarreau474b96a2015-01-28 19:03:21 +01001882 * ssl_handshake_cost during its initialization. In any case, if
1883 * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for
1884 * maxconn in order to protect the system.
Willy Tarreaud0256482015-01-15 21:45:22 +01001885 */
1886 if (!global.rlimit_memmax) {
1887 if (global.maxconn == 0) {
1888 global.maxconn = DEFAULT_MAXCONN;
1889 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1890 fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn);
1891 }
1892 }
1893#ifdef USE_OPENSSL
1894 else if (!global.maxconn && !global.maxsslconn &&
1895 (global.ssl_used_frontend || global.ssl_used_backend)) {
1896 /* memmax is set, compute everything automatically. Here we want
1897 * to ensure that all SSL connections will be served. We take
1898 * care of the number of sides where SSL is used, and consider
1899 * the worst case : SSL used on both sides and doing a handshake
1900 * simultaneously. Note that we can't have more than maxconn
1901 * handshakes at a time by definition, so for the worst case of
1902 * two SSL conns per connection, we count a single handshake.
1903 */
1904 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1905 int64_t mem = global.rlimit_memmax * 1048576ULL;
1906
1907 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1908 mem -= global.maxzlibmem;
1909 mem = mem * MEM_USABLE_RATIO;
1910
1911 global.maxconn = mem /
Willy Tarreau87b09662015-04-03 00:22:06 +02001912 ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream
Willy Tarreaud0256482015-01-15 21:45:22 +01001913 sides * global.ssl_session_max_cost + // SSL buffers, one per side
1914 global.ssl_handshake_max_cost); // 1 handshake per connection max
1915
1916 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001917#ifdef SYSTEM_MAXCONN
1918 if (global.maxconn > DEFAULT_MAXCONN)
1919 global.maxconn = DEFAULT_MAXCONN;
1920#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001921 global.maxsslconn = sides * global.maxconn;
1922 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1923 fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n",
1924 global.maxconn, global.maxsslconn);
1925 }
1926 else if (!global.maxsslconn &&
1927 (global.ssl_used_frontend || global.ssl_used_backend)) {
1928 /* memmax and maxconn are known, compute maxsslconn automatically.
1929 * maxsslconn being forced, we don't know how many of it will be
1930 * on each side if both sides are being used. The worst case is
1931 * when all connections use only one SSL instance because
1932 * handshakes may be on two sides at the same time.
1933 */
1934 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1935 int64_t mem = global.rlimit_memmax * 1048576ULL;
1936 int64_t sslmem;
1937
1938 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1939 mem -= global.maxzlibmem;
1940 mem = mem * MEM_USABLE_RATIO;
1941
Willy Tarreau87b09662015-04-03 00:22:06 +02001942 sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001943 global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost);
1944 global.maxsslconn = round_2dig(global.maxsslconn);
1945
1946 if (sslmem <= 0 || global.maxsslconn < sides) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001947 ha_alert("Cannot compute the automatic maxsslconn because global.maxconn is already too "
1948 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1949 "without SSL is %d, but %d was found and SSL is in use.\n",
1950 global.rlimit_memmax,
1951 (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)),
1952 global.maxconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01001953 exit(1);
1954 }
1955
1956 if (global.maxsslconn > sides * global.maxconn)
1957 global.maxsslconn = sides * global.maxconn;
1958
1959 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1960 fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn);
1961 }
1962#endif
1963 else if (!global.maxconn) {
1964 /* memmax and maxsslconn are known/unused, compute maxconn automatically */
1965 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1966 int64_t mem = global.rlimit_memmax * 1048576ULL;
1967 int64_t clearmem;
1968
1969 if (global.ssl_used_frontend || global.ssl_used_backend)
1970 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1971
1972 mem -= global.maxzlibmem;
1973 mem = mem * MEM_USABLE_RATIO;
1974
1975 clearmem = mem;
1976 if (sides)
1977 clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn;
1978
Willy Tarreau87b09662015-04-03 00:22:06 +02001979 global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001980 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001981#ifdef SYSTEM_MAXCONN
1982 if (global.maxconn > DEFAULT_MAXCONN)
1983 global.maxconn = DEFAULT_MAXCONN;
1984#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001985
1986 if (clearmem <= 0 || !global.maxconn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001987 ha_alert("Cannot compute the automatic maxconn because global.maxsslconn is already too "
1988 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1989 "is %d, but %d was found.\n",
1990 global.rlimit_memmax,
1991 (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)),
1992 global.maxsslconn);
Willy Tarreaud0256482015-01-15 21:45:22 +01001993 exit(1);
1994 }
1995
1996 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
1997 if (sides && global.maxsslconn > sides * global.maxconn) {
1998 fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn "
1999 "to be limited to %d. Better reduce global.maxsslconn to get more "
2000 "room for extra connections.\n", global.maxsslconn, global.maxconn);
2001 }
2002 fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn);
2003 }
2004 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002005
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002006 if (!global.maxpipes) {
2007 /* maxpipes not specified. Count how many frontends and backends
2008 * may be using splicing, and bound that to maxconn.
2009 */
2010 struct proxy *cur;
2011 int nbfe = 0, nbbe = 0;
2012
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002013 for (cur = proxies_list; cur; cur = cur->next) {
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002014 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
2015 if (cur->cap & PR_CAP_FE)
2016 nbfe += cur->maxconn;
2017 if (cur->cap & PR_CAP_BE)
Willy Tarreauafb48762009-01-25 10:42:05 +01002018 nbbe += cur->fullconn ? cur->fullconn : global.maxconn;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002019 }
2020 }
2021 global.maxpipes = MAX(nbfe, nbbe);
2022 if (global.maxpipes > global.maxconn)
2023 global.maxpipes = global.maxconn;
Willy Tarreau686ac822009-01-25 14:06:58 +01002024 global.maxpipes /= 4;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01002025 }
2026
2027
Willy Tarreauabacc2c2011-09-07 14:26:33 +02002028 global.hardmaxconn = global.maxconn; /* keep this max value */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002029 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
Willy Tarreau3ec79b92009-01-18 20:39:42 +01002030 global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */
Emeric Brunece0c332017-12-06 13:51:49 +01002031 /* compute fd used by async engines */
2032 if (global.ssl_used_async_engines) {
2033 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
2034 global.maxsock += global.maxconn * sides * global.ssl_used_async_engines;
2035 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002036
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002037 if (global.stats_fe)
2038 global.maxsock += global.stats_fe->maxconn;
2039
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002040 if (cfg_peers) {
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002041 /* peers also need to bypass global maxconn */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002042 struct peers *p = cfg_peers;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002043
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002044 for (p = cfg_peers; p; p = p->next)
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002045 if (p->peers_fe)
2046 global.maxsock += p->peers_fe->maxconn;
2047 }
2048
Willy Tarreau1db37712007-06-03 17:16:49 +02002049 if (global.tune.maxpollevents <= 0)
2050 global.tune.maxpollevents = MAX_POLL_EVENTS;
2051
Olivier Houchard1599b802018-05-24 18:59:04 +02002052 if (global.tune.runqueue_depth <= 0)
2053 global.tune.runqueue_depth = RUNQUEUE_DEPTH;
2054
Willy Tarreau6f4a82c2009-03-21 20:43:57 +01002055 if (global.tune.recv_enough == 0)
2056 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
2057
Willy Tarreau27097842015-09-28 13:53:23 +02002058 if (global.tune.maxrewrite < 0)
2059 global.tune.maxrewrite = MAXREWRITE;
2060
Willy Tarreau27a674e2009-08-17 07:23:33 +02002061 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
2062 global.tune.maxrewrite = global.tune.bufsize / 2;
2063
Willy Tarreaubaaee002006-06-26 02:48:02 +02002064 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
2065 /* command line debug mode inhibits configuration mode */
William Lallemand095ba4c2017-06-01 17:38:50 +02002066 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002067 global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND));
2068 }
2069
William Lallemand095ba4c2017-06-01 17:38:50 +02002070 if (arg_mode & MODE_DAEMON) {
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002071 /* command line daemon mode inhibits foreground and debug modes mode */
2072 global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND);
William Lallemand095ba4c2017-06-01 17:38:50 +02002073 global.mode |= arg_mode & MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002074 }
Willy Tarreau772f0dd2012-10-26 16:04:28 +02002075
2076 global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002077
William Lallemand095ba4c2017-06-01 17:38:50 +02002078 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002079 ha_warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
William Lallemand095ba4c2017-06-01 17:38:50 +02002080 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002081 }
2082
William Lallemand095ba4c2017-06-01 17:38:50 +02002083 if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002084 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
Christopher Faulet767a84b2017-11-24 16:50:31 +01002085 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 +02002086 global.nbproc = 1;
2087 }
2088
2089 if (global.nbproc < 1)
2090 global.nbproc = 1;
2091
Christopher Fauletbe0faa22017-08-29 15:37:10 +02002092 if (global.nbthread < 1)
2093 global.nbthread = 1;
2094
Christopher Faulet3ef26392017-08-29 16:46:57 +02002095 /* Realloc trash buffers because global.tune.bufsize may have changed */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002096 if (!init_trash_buffers(0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002097 ha_alert("failed to initialize trash buffers.\n");
Christopher Faulet3ef26392017-08-29 16:46:57 +02002098 exit(1);
2099 }
2100
Christopher Faulet96d44832017-11-14 22:02:30 +01002101 if (!init_log_buffers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002102 ha_alert("failed to initialize log buffers.\n");
Christopher Faulet96d44832017-11-14 22:02:30 +01002103 exit(1);
2104 }
2105
Willy Tarreauef1d1f82007-04-16 00:25:25 +02002106 /*
2107 * Note: we could register external pollers here.
2108 * Built-in pollers have been registered before main().
2109 */
Willy Tarreau4f60f162007-04-08 16:39:58 +02002110
Willy Tarreau43b78992009-01-25 15:42:27 +01002111 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +02002112 disable_poller("kqueue");
2113
Willy Tarreau43b78992009-01-25 15:42:27 +01002114 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002115 disable_poller("epoll");
2116
Willy Tarreau43b78992009-01-25 15:42:27 +01002117 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002118 disable_poller("poll");
2119
Willy Tarreau43b78992009-01-25 15:42:27 +01002120 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +02002121 disable_poller("select");
2122
2123 /* Note: we could disable any poller by name here */
2124
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002125 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +02002126 list_pollers(stderr);
Christopher Fauletb3f4e142016-03-07 12:46:38 +01002127 fprintf(stderr, "\n");
2128 list_filters(stderr);
2129 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002130
Willy Tarreau4f60f162007-04-08 16:39:58 +02002131 if (!init_pollers()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002132 ha_alert("No polling mechanism available.\n"
2133 " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n"
2134 " is too low on this platform to support maxconn and the number of listeners\n"
2135 " and servers. You should rebuild haproxy specifying your system using TARGET=\n"
2136 " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n"
2137 " global maxconn setting to accommodate the system's limitation. For reference,\n"
2138 " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n"
2139 " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n"
2140 " check build settings using 'haproxy -vv'.\n\n",
2141 FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002142 exit(1);
2143 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02002144 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
2145 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002146 }
2147
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002148 if (!global.node)
2149 global.node = strdup(hostname);
2150
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01002151 if (!hlua_post_init())
2152 exit(1);
Thomas Holmes6abded42015-05-12 16:23:58 +01002153
Maxime de Roucy0f503922016-05-13 23:52:55 +02002154 free(err_msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002155}
2156
Simon Horman6fb82592011-07-15 13:14:11 +09002157static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +09002158{
Simon Hormanac821422011-07-15 13:14:09 +09002159 struct acl_term_suite *suite, *suiteb;
2160 struct acl_term *term, *termb;
2161
Simon Horman6fb82592011-07-15 13:14:11 +09002162 if (!cond)
2163 return;
2164
2165 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
2166 list_for_each_entry_safe(term, termb, &suite->terms, list) {
2167 LIST_DEL(&term->list);
2168 free(term);
Simon Hormanac821422011-07-15 13:14:09 +09002169 }
Simon Horman6fb82592011-07-15 13:14:11 +09002170 LIST_DEL(&suite->list);
2171 free(suite);
2172 }
2173
2174 free(cond);
2175}
2176
2177static void deinit_tcp_rules(struct list *rules)
2178{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002179 struct act_rule *trule, *truleb;
Simon Horman6fb82592011-07-15 13:14:11 +09002180
2181 list_for_each_entry_safe(trule, truleb, rules, list) {
Simon Hormanac821422011-07-15 13:14:09 +09002182 LIST_DEL(&trule->list);
Simon Horman6fb82592011-07-15 13:14:11 +09002183 deinit_acl_cond(trule->cond);
Simon Hormanac821422011-07-15 13:14:09 +09002184 free(trule);
2185 }
2186}
2187
Simon Horman6fb82592011-07-15 13:14:11 +09002188static void deinit_stick_rules(struct list *rules)
2189{
2190 struct sticking_rule *rule, *ruleb;
2191
2192 list_for_each_entry_safe(rule, ruleb, rules, list) {
2193 LIST_DEL(&rule->list);
2194 deinit_acl_cond(rule->cond);
Christopher Faulet476e5d02016-10-26 11:34:47 +02002195 release_sample_expr(rule->expr);
Simon Horman6fb82592011-07-15 13:14:11 +09002196 free(rule);
2197 }
2198}
2199
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002200void deinit(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002201{
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002202 struct proxy *p = proxies_list, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002203 struct cap_hdr *h,*h_next;
2204 struct server *s,*s_next;
2205 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002206 struct acl_cond *cond, *condb;
2207 struct hdr_exp *exp, *expb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002208 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002209 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002210 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002211 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002212 struct wordlist *wl, *wlb;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002213 struct cond_wordlist *cwl, *cwlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002214 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +02002215 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +01002216 struct logformat_node *lf, *lfb;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002217 struct bind_conf *bind_conf, *bind_back;
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002218 struct build_opts_str *bol, *bolb;
Willy Tarreau05554e62016-12-21 20:46:26 +01002219 struct post_deinit_fct *pdf;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002220 int i;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002221
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002222 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002223 while (p) {
Willy Tarreau8113a5d2012-10-04 08:01:43 +02002224 free(p->conf.file);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002225 free(p->id);
2226 free(p->check_req);
2227 free(p->cookie_name);
2228 free(p->cookie_domain);
2229 free(p->url_param_name);
2230 free(p->capture_name);
2231 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +09002232 free(p->rdp_cookie_name);
Willy Tarreau62a61232013-04-12 18:13:46 +02002233 if (p->conf.logformat_string != default_http_log_format &&
2234 p->conf.logformat_string != default_tcp_log_format &&
2235 p->conf.logformat_string != clf_http_log_format)
2236 free(p->conf.logformat_string);
Willy Tarreau196729e2012-05-31 19:30:26 +02002237
Willy Tarreau62a61232013-04-12 18:13:46 +02002238 free(p->conf.lfs_file);
2239 free(p->conf.uniqueid_format_string);
2240 free(p->conf.uif_file);
Godbachaf1a75d2013-10-02 17:10:11 +08002241 free(p->lbprm.map.srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002242
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002243 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
2244 free(p->conf.logformat_sd_string);
2245 free(p->conf.lfsd_file);
2246
Willy Tarreaua534fea2008-08-03 12:19:50 +02002247 for (i = 0; i < HTTP_ERR_SIZE; i++)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002248 chunk_destroy(&p->errmsg[i]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002249
Willy Tarreauf4f04122010-01-28 18:10:50 +01002250 list_for_each_entry_safe(cwl, cwlb, &p->req_add, list) {
2251 LIST_DEL(&cwl->list);
2252 free(cwl->s);
2253 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002254 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002255
Willy Tarreauf4f04122010-01-28 18:10:50 +01002256 list_for_each_entry_safe(cwl, cwlb, &p->rsp_add, list) {
2257 LIST_DEL(&cwl->list);
2258 free(cwl->s);
2259 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002260 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002261
Willy Tarreaub80c2302007-11-30 20:51:32 +01002262 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
2263 LIST_DEL(&cond->list);
2264 prune_acl_cond(cond);
2265 free(cond);
2266 }
2267
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002268 for (exp = p->req_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002269 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002270 regex_free(exp->preg);
2271 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002272 }
2273
Willy Tarreau98d04852015-05-26 12:18:29 +02002274 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002275 expb = exp;
2276 exp = exp->next;
2277 free(expb);
2278 }
2279
2280 for (exp = p->rsp_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002281 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002282 regex_free(exp->preg);
2283 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002284 }
2285
Willy Tarreau98d04852015-05-26 12:18:29 +02002286 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002287 expb = exp;
2288 exp = exp->next;
2289 free(expb);
2290 }
2291
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002292 /* build a list of unique uri_auths */
2293 if (!ua)
2294 ua = p->uri_auth;
2295 else {
2296 /* check if p->uri_auth is unique */
2297 for (uap = ua; uap; uap=uap->next)
2298 if (uap == p->uri_auth)
2299 break;
2300
Willy Tarreauaccc4e12008-06-24 11:14:45 +02002301 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002302 /* add it, if it is */
2303 p->uri_auth->next = ua;
2304 ua = p->uri_auth;
2305 }
2306 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002307
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002308 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
2309 LIST_DEL(&acl->list);
2310 prune_acl(acl);
2311 free(acl);
2312 }
2313
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002314 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
2315 LIST_DEL(&srule->list);
2316 prune_acl_cond(srule->cond);
2317 free(srule->cond);
2318 free(srule);
2319 }
2320
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002321 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
2322 LIST_DEL(&rule->list);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002323 if (rule->cond) {
2324 prune_acl_cond(rule->cond);
2325 free(rule->cond);
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +01002326 free(rule->file);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002327 }
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002328 free(rule);
2329 }
2330
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002331 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
2332 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +01002333 if (rdr->cond) {
2334 prune_acl_cond(rdr->cond);
2335 free(rdr->cond);
2336 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002337 free(rdr->rdr_str);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002338 list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) {
2339 LIST_DEL(&lf->list);
2340 free(lf);
2341 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002342 free(rdr);
2343 }
2344
William Lallemand0f99e342011-10-12 17:50:54 +02002345 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
2346 LIST_DEL(&log->list);
2347 free(log);
2348 }
2349
William Lallemand723b73a2012-02-08 16:37:49 +01002350 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
2351 LIST_DEL(&lf->list);
2352 free(lf);
2353 }
2354
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002355 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
2356 LIST_DEL(&lf->list);
2357 free(lf);
2358 }
2359
Simon Hormanac821422011-07-15 13:14:09 +09002360 deinit_tcp_rules(&p->tcp_req.inspect_rules);
2361 deinit_tcp_rules(&p->tcp_req.l4_rules);
2362
Simon Horman6fb82592011-07-15 13:14:11 +09002363 deinit_stick_rules(&p->storersp_rules);
2364 deinit_stick_rules(&p->sticking_rules);
2365
Willy Tarreaubaaee002006-06-26 02:48:02 +02002366 h = p->req_cap;
2367 while (h) {
2368 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002369 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002370 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002371 free(h);
2372 h = h_next;
2373 }/* end while(h) */
2374
2375 h = p->rsp_cap;
2376 while (h) {
2377 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002378 free(h->name);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002379 pool_destroy(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002380 free(h);
2381 h = h_next;
2382 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002383
Willy Tarreaubaaee002006-06-26 02:48:02 +02002384 s = p->srv;
2385 while (s) {
2386 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002387
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002388 if (s->check.task) {
2389 task_delete(s->check.task);
2390 task_free(s->check.task);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002391 }
Simon Hormand60d6912013-11-25 10:46:36 +09002392 if (s->agent.task) {
2393 task_delete(s->agent.task);
2394 task_free(s->agent.task);
2395 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002396
Willy Tarreau2e993902011-10-31 11:53:20 +01002397 if (s->warmup) {
2398 task_delete(s->warmup);
2399 task_free(s->warmup);
2400 }
2401
Willy Tarreaua534fea2008-08-03 12:19:50 +02002402 free(s->id);
2403 free(s->cookie);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002404 free(s->check.bi.area);
2405 free(s->check.bo.area);
2406 free(s->agent.bi.area);
2407 free(s->agent.bo.area);
James Brown55f9ff12015-10-21 18:19:05 -07002408 free(s->agent.send_string);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002409 free(s->hostname_dn);
Sárközi, László34c01792014-09-05 10:08:23 +02002410 free((char*)s->conf.file);
Olivier Houchard7fc3be72018-11-22 18:50:54 +01002411 free(s->idle_conns);
2412 free(s->priv_conns);
2413 free(s->safe_conns);
Willy Tarreau17d45382016-12-22 21:16:08 +01002414
2415 if (s->use_ssl || s->check.use_ssl) {
2416 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2417 xprt_get(XPRT_SSL)->destroy_srv(s);
2418 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002419 HA_SPIN_DESTROY(&s->lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002420 free(s);
2421 s = s_next;
2422 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002423
Willy Tarreau4348fad2012-09-20 16:48:07 +02002424 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002425 /*
2426 * Zombie proxy, the listener just pretend to be up
2427 * because they still hold an opened fd.
2428 * Close it and give the listener its real state.
2429 */
2430 if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
2431 close(l->fd);
2432 l->state = LI_INIT;
2433 }
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02002434 unbind_listener(l);
2435 delete_listener(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002436 LIST_DEL(&l->by_fe);
2437 LIST_DEL(&l->by_bind);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002438 free(l->name);
2439 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002440 free(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002441 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002442
Willy Tarreau4348fad2012-09-20 16:48:07 +02002443 /* Release unused SSL configs. */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002444 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01002445 if (bind_conf->xprt->destroy_bind_conf)
2446 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002447 free(bind_conf->file);
2448 free(bind_conf->arg);
2449 LIST_DEL(&bind_conf->by_fe);
2450 free(bind_conf);
2451 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02002452
Christopher Fauletd7c91962015-04-30 11:48:27 +02002453 flt_deinit(p);
2454
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002455 free(p->desc);
2456 free(p->fwdfor_hdr_name);
2457
Willy Tarreauff011f22011-01-06 17:51:27 +01002458 free_http_req_rules(&p->http_req_rules);
Sasha Pachev218f0642014-06-16 12:05:59 -06002459 free_http_res_rules(&p->http_res_rules);
Willy Tarreau1f89b182017-11-22 16:53:53 +01002460 task_free(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002461
Willy Tarreaubafbe012017-11-24 17:34:44 +01002462 pool_destroy(p->req_cap_pool);
2463 pool_destroy(p->rsp_cap_pool);
2464 pool_destroy(p->table.pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002465
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002466 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002467 p = p->next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002468 HA_SPIN_DESTROY(&p0->lbprm.lock);
2469 HA_SPIN_DESTROY(&p0->lock);
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002470 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002471 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02002472
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002473 while (ua) {
2474 uap = ua;
2475 ua = ua->next;
2476
Willy Tarreaua534fea2008-08-03 12:19:50 +02002477 free(uap->uri_prefix);
2478 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002479 free(uap->node);
2480 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002481
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002482 userlist_free(uap->userlist);
Willy Tarreauff011f22011-01-06 17:51:27 +01002483 free_http_req_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002484
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002485 free(uap);
2486 }
2487
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002488 userlist_free(userlist);
2489
David Carlier834cb2e2015-09-25 12:02:25 +01002490 cfg_unregister_sections();
2491
Christopher Faulet0132d062017-07-26 15:33:35 +02002492 deinit_log_buffers();
Christopher Faulet748919a2017-07-26 14:59:46 +02002493 deinit_trash_buffers();
David Carlier834cb2e2015-09-25 12:02:25 +01002494
Willy Tarreaudd815982007-10-16 12:25:14 +02002495 protocol_unbind_all();
2496
Willy Tarreau05554e62016-12-21 20:46:26 +01002497 list_for_each_entry(pdf, &post_deinit_list, list)
2498 pdf->fct();
2499
Joe Williamsdf5b38f2010-12-29 17:05:48 +01002500 free(global.log_send_hostname); global.log_send_hostname = NULL;
Dragan Dosen43885c72015-10-01 13:18:13 +02002501 chunk_destroy(&global.log_tag);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002502 free(global.chroot); global.chroot = NULL;
2503 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002504 free(global.node); global.node = NULL;
2505 free(global.desc); global.desc = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002506 free(oldpids); oldpids = NULL;
Willy Tarreau1f89b182017-11-22 16:53:53 +01002507 task_free(global_listener_queue_task); global_listener_queue_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002508
William Lallemand0f99e342011-10-12 17:50:54 +02002509 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
2510 LIST_DEL(&log->list);
2511 free(log);
2512 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01002513 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02002514 free(wl->s);
Willy Tarreau477ecd82010-01-03 21:12:30 +01002515 LIST_DEL(&wl->list);
2516 free(wl);
2517 }
2518
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002519 list_for_each_entry_safe(bol, bolb, &build_opts_list, list) {
2520 if (bol->must_free)
2521 free((void *)bol->str);
2522 LIST_DEL(&bol->list);
2523 free(bol);
2524 }
2525
Christopher Fauletff2613e2016-11-09 11:36:17 +01002526 vars_prune(&global.vars, NULL, NULL);
2527
Christopher Fauletad405f12017-08-29 15:30:11 +02002528 deinit_buffer();
2529
Willy Tarreaubafbe012017-11-24 17:34:44 +01002530 pool_destroy(pool_head_stream);
2531 pool_destroy(pool_head_session);
2532 pool_destroy(pool_head_connection);
2533 pool_destroy(pool_head_connstream);
2534 pool_destroy(pool_head_requri);
2535 pool_destroy(pool_head_task);
2536 pool_destroy(pool_head_capture);
2537 pool_destroy(pool_head_pendconn);
2538 pool_destroy(pool_head_sig_handlers);
2539 pool_destroy(pool_head_hdr_idx);
2540 pool_destroy(pool_head_http_txn);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002541 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002542} /* end deinit() */
2543
William Lallemand72160322018-11-06 17:37:16 +01002544
2545
2546/* This is a wrapper for the sockpair FD, It tests if the socket received an
2547 * EOF, if not, it calls listener_accept */
2548void mworker_accept_wrapper(int fd)
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002549{
2550 char c;
William Lallemand72160322018-11-06 17:37:16 +01002551 int ret;
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002552
William Lallemand72160322018-11-06 17:37:16 +01002553 while (1) {
2554 ret = recv(fd, &c, 1, MSG_PEEK);
2555 if (ret == -1) {
2556 if (errno == EINTR)
2557 continue;
2558 if (errno == EAGAIN) {
2559 fd_cant_recv(fd);
2560 return;
2561 }
2562 break;
2563 } else if (ret > 0) {
2564 listener_accept(fd);
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002565 return;
William Lallemand72160322018-11-06 17:37:16 +01002566 } else if (ret == 0) {
2567 /* At this step the master is down before
2568 * this worker perform a 'normal' exit.
2569 * So we want to exit with an error but
2570 * other threads could currently process
2571 * some stuff so we can't perform a clean
2572 * deinit().
2573 */
2574 exit(EXIT_FAILURE);
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002575 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002576 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002577 return;
2578}
2579
William Lallemand72160322018-11-06 17:37:16 +01002580/*
William Lallemand72160322018-11-06 17:37:16 +01002581 * This function register the accept wrapper for the sockpair of the master worker
2582 */
Willy Tarreau1605c7a2018-01-23 19:01:49 +01002583void mworker_pipe_register()
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002584{
William Lallemand72160322018-11-06 17:37:16 +01002585 /* The iocb should be already initialized with listener_accept */
William Lallemanda90cacf2018-11-07 08:38:32 +01002586 if (fdtab[proc_self->ipc_fd[1]].iocb == mworker_accept_wrapper)
2587 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002588
William Lallemandbc193052018-09-11 10:06:26 +02002589 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
Emeric Brunc8c0ed92018-10-11 15:27:07 +02002590 /* In multi-tread, we need only one thread to process
2591 * events on the pipe with master
2592 */
William Lallemande260e0d2018-11-08 12:00:14 +01002593 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 +02002594 fd_want_recv(proc_self->ipc_fd[1]);
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002595}
Christopher Fauletdc628a32017-10-19 11:59:44 +02002596
Willy Tarreau918ff602011-07-25 16:33:49 +02002597/* Runs the polling loop */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01002598static void run_poll_loop()
Willy Tarreau4f60f162007-04-08 16:39:58 +02002599{
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002600 int next, exp;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002601
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002602 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002603 while (1) {
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002604 /* Process a few tasks */
2605 process_runnable_tasks();
2606
William Lallemand1aab50b2018-06-07 09:46:01 +02002607 /* check if we caught some signals and process them in the
2608 first thread */
2609 if (tid == 0)
2610 signal_process_queue();
Willy Tarreau29857942009-05-10 09:01:21 +02002611
Willy Tarreau58b458d2008-06-29 22:40:23 +02002612 /* Check if we can expire some tasks */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002613 next = wake_expired_tasks();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002614
Willy Tarreau85c459d2018-08-02 10:54:31 +02002615 /* stop when there's nothing left to do */
William Lallemanda7199262018-11-16 16:57:20 +01002616 if ((jobs - unstoppable_jobs) == 0)
Willy Tarreau85c459d2018-08-02 10:54:31 +02002617 break;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002618
Willy Tarreau10146c92015-04-13 20:44:19 +02002619 /* expire immediately if events are pending */
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002620 exp = now_ms;
Christopher Faulet32467fe2018-01-15 12:16:34 +01002621 if (fd_cache_mask & tid_bit)
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002622 activity[tid].wake_cache++;
2623 else if (active_tasks_mask & tid_bit)
2624 activity[tid].wake_tasks++;
William Lallemand1aab50b2018-06-07 09:46:01 +02002625 else if (signal_queue_len && tid == 0)
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002626 activity[tid].wake_signal++;
Olivier Houchard79321b92018-07-26 17:55:11 +02002627 else {
2628 HA_ATOMIC_OR(&sleeping_thread_mask, tid_bit);
2629 __ha_barrier_store();
2630 if (active_tasks_mask & tid_bit) {
2631 activity[tid].wake_tasks++;
2632 HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
2633 } else
2634 exp = next;
2635 }
Willy Tarreau10146c92015-04-13 20:44:19 +02002636
Willy Tarreau58b458d2008-06-29 22:40:23 +02002637 /* The poller will ensure it returns around <next> */
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002638 cur_poller.poll(&cur_poller, exp);
Olivier Houchard79321b92018-07-26 17:55:11 +02002639 if (sleeping_thread_mask & tid_bit)
2640 HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
Willy Tarreau033cd9d2014-01-25 19:24:15 +01002641 fd_process_cached_events();
Emeric Brun64cc49c2017-10-03 14:46:45 +02002642
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002643 activity[tid].loops++;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002644 }
2645}
2646
Christopher Faulet1d17c102017-08-29 15:38:48 +02002647static void *run_thread_poll_loop(void *data)
2648{
2649 struct per_thread_init_fct *ptif;
2650 struct per_thread_deinit_fct *ptdf;
Christopher Fauletda18b9d2018-01-25 16:10:16 +01002651 __decl_hathreads(static HA_SPINLOCK_T start_lock);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002652
Willy Tarreau0c026f42018-08-01 19:12:20 +02002653 ha_set_tid(*((unsigned int *)data));
Christopher Faulet1d17c102017-08-29 15:38:48 +02002654 tv_update_date(-1,-1);
2655
2656 list_for_each_entry(ptif, &per_thread_init_list, list) {
2657 if (!ptif->fct()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002658 ha_alert("failed to initialize thread %u.\n", tid);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002659 exit(1);
2660 }
2661 }
2662
William Lallemandbc193052018-09-11 10:06:26 +02002663 if ((global.mode & MODE_MWORKER) && master == 0) {
Willy Tarreau1605c7a2018-01-23 19:01:49 +01002664 HA_SPIN_LOCK(START_LOCK, &start_lock);
2665 mworker_pipe_register();
2666 HA_SPIN_UNLOCK(START_LOCK, &start_lock);
2667 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002668
2669 protocol_enable_all();
Christopher Faulet1d17c102017-08-29 15:38:48 +02002670 run_poll_loop();
2671
2672 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
2673 ptdf->fct();
2674
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002675#ifdef USE_THREAD
William Lallemand091d8272018-06-24 09:37:03 +02002676 HA_ATOMIC_AND(&all_threads_mask, ~tid_bit);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002677 if (tid > 0)
2678 pthread_exit(NULL);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002679#endif
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002680 return NULL;
2681}
Christopher Faulet1d17c102017-08-29 15:38:48 +02002682
Willy Tarreaue9b26022011-08-01 20:57:55 +02002683/* This is the global management task for listeners. It enables listeners waiting
2684 * for global resources when there are enough free resource, or at least once in
2685 * a while. It is designed to be called as a task.
2686 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002687static struct task *manage_global_listener_queue(struct task *t, void *context, unsigned short state)
Willy Tarreaue9b26022011-08-01 20:57:55 +02002688{
2689 int next = TICK_ETERNITY;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002690 /* queue is empty, nothing to do */
2691 if (LIST_ISEMPTY(&global_listener_queue))
2692 goto out;
2693
2694 /* If there are still too many concurrent connections, let's wait for
2695 * some of them to go away. We don't need to re-arm the timer because
2696 * each of them will scan the queue anyway.
2697 */
2698 if (unlikely(actconn >= global.maxconn))
2699 goto out;
2700
2701 /* We should periodically try to enable listeners waiting for a global
2702 * resource here, because it is possible, though very unlikely, that
2703 * they have been blocked by a temporary lack of global resource such
2704 * as a file descriptor or memory and that the temporary condition has
2705 * disappeared.
2706 */
Willy Tarreauabacc2c2011-09-07 14:26:33 +02002707 dequeue_all_listeners(&global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +02002708
2709 out:
2710 t->expire = next;
2711 task_queue(t);
2712 return t;
2713}
Willy Tarreau4f60f162007-04-08 16:39:58 +02002714
Willy Tarreaubaaee002006-06-26 02:48:02 +02002715int main(int argc, char **argv)
2716{
2717 int err, retry;
2718 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02002719 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02002720 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002721
Olivier Houchard5fa300d2018-02-03 15:15:21 +01002722 setvbuf(stdout, NULL, _IONBF, 0);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002723 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002724 signal_register_fct(SIGQUIT, dump, SIGQUIT);
2725 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
2726 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
William Lallemand73b85e72017-06-01 17:38:51 +02002727 signal_register_fct(SIGUSR2, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002728
Willy Tarreaue437c442010-03-17 18:02:46 +01002729 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
2730 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
2731 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002732 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002733 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002734
Willy Tarreaudc23a922011-02-16 11:10:36 +01002735 /* ulimits */
2736 if (!global.rlimit_nofile)
2737 global.rlimit_nofile = global.maxsock;
2738
2739 if (global.rlimit_nofile) {
2740 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
2741 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
Willy Tarreauef635472016-06-21 11:48:18 +02002742 /* try to set it to the max possible at least */
2743 getrlimit(RLIMIT_NOFILE, &limit);
Willy Tarreau164dd0b2016-06-21 11:51:59 +02002744 limit.rlim_cur = limit.rlim_max;
2745 if (setrlimit(RLIMIT_NOFILE, &limit) != -1)
2746 getrlimit(RLIMIT_NOFILE, &limit);
2747
Christopher Faulet767a84b2017-11-24 16:50:31 +01002748 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 +02002749 global.rlimit_nofile = limit.rlim_cur;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002750 }
2751 }
2752
2753 if (global.rlimit_memmax) {
2754 limit.rlim_cur = limit.rlim_max =
Willy Tarreau70060452015-12-14 12:46:07 +01002755 global.rlimit_memmax * 1048576ULL;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002756#ifdef RLIMIT_AS
2757 if (setrlimit(RLIMIT_AS, &limit) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002758 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2759 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01002760 }
2761#else
2762 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002763 ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2764 argv[0], global.rlimit_memmax);
Willy Tarreaudc23a922011-02-16 11:10:36 +01002765 }
2766#endif
2767 }
2768
Olivier Houchardf73629d2017-04-05 22:33:04 +02002769 if (old_unixsocket) {
William Lallemand85b0bd92017-06-01 17:38:53 +02002770 if (strcmp("/dev/null", old_unixsocket) != 0) {
2771 if (get_old_sockets(old_unixsocket) != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002772 ha_alert("Failed to get the sockets from the old process!\n");
William Lallemand85b0bd92017-06-01 17:38:53 +02002773 if (!(global.mode & MODE_MWORKER))
2774 exit(1);
2775 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002776 }
2777 }
William Lallemand85b0bd92017-06-01 17:38:53 +02002778 get_cur_unixsocket();
2779
Willy Tarreaubaaee002006-06-26 02:48:02 +02002780 /* We will loop at most 100 times with 10 ms delay each time.
2781 * That's at most 1 second. We only send a signal to old pids
2782 * if we cannot grab at least one port.
2783 */
2784 retry = MAX_START_RETRIES;
2785 err = ERR_NONE;
2786 while (retry >= 0) {
2787 struct timeval w;
2788 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01002789 /* exit the loop on no error or fatal error */
2790 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002791 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02002792 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002793 break;
2794
2795 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
2796 * listening sockets. So on those platforms, it would be wiser to
2797 * simply send SIGUSR1, which will not be undoable.
2798 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02002799 if (tell_old_pids(SIGTTOU) == 0) {
2800 /* no need to wait if we can't contact old pids */
2801 retry = 0;
2802 continue;
2803 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002804 /* give some time to old processes to stop listening */
2805 w.tv_sec = 0;
2806 w.tv_usec = 10*1000;
2807 select(0, NULL, NULL, NULL, &w);
2808 retry--;
2809 }
2810
2811 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01002812 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02002813 if (retry != MAX_START_RETRIES && nb_oldpids) {
2814 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002815 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02002816 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002817 exit(1);
2818 }
2819
William Lallemand944e6192018-11-21 15:48:31 +01002820 if (!(global.mode & MODE_MWORKER_WAIT) && listeners == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002821 ha_alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002822 /* Note: we don't have to send anything to the old pids because we
2823 * never stopped them. */
2824 exit(1);
2825 }
2826
Emeric Bruncf20bf12010-10-22 16:06:11 +02002827 err = protocol_bind_all(errmsg, sizeof(errmsg));
2828 if ((err & ~ERR_WARN) != ERR_NONE) {
2829 if ((err & ERR_ALERT) || (err & ERR_WARN))
Christopher Faulet767a84b2017-11-24 16:50:31 +01002830 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002831
Christopher Faulet767a84b2017-11-24 16:50:31 +01002832 ha_alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002833 protocol_unbind_all(); /* cleanup everything we can */
2834 if (nb_oldpids)
2835 tell_old_pids(SIGTTIN);
2836 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002837 } else if (err & ERR_WARN) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002838 ha_alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02002839 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002840 /* Ok, all listener should now be bound, close any leftover sockets
2841 * the previous process gave us, we don't need them anymore
2842 */
2843 while (xfer_sock_list != NULL) {
2844 struct xfer_sock_list *tmpxfer = xfer_sock_list->next;
2845 close(xfer_sock_list->fd);
2846 free(xfer_sock_list->iface);
2847 free(xfer_sock_list->namespace);
2848 free(xfer_sock_list);
2849 xfer_sock_list = tmpxfer;
2850 }
Willy Tarreaudd815982007-10-16 12:25:14 +02002851
Willy Tarreaubaaee002006-06-26 02:48:02 +02002852 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002853 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
2854 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002855
Willy Tarreaubaaee002006-06-26 02:48:02 +02002856 /* MODE_QUIET can inhibit alerts and warnings below this line */
2857
PiBa-NL149a81a2017-12-25 21:03:31 +01002858 if (getenv("HAPROXY_MWORKER_REEXEC") != NULL) {
2859 /* either stdin/out/err are already closed or should stay as they are. */
2860 if ((global.mode & MODE_DAEMON)) {
2861 /* daemon mode re-executing, stdin/stdout/stderr are already closed so keep quiet */
2862 global.mode &= ~MODE_VERBOSE;
2863 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
2864 }
2865 } else {
2866 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
2867 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01002868 stdio_quiet(-1);
PiBa-NL149a81a2017-12-25 21:03:31 +01002869 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002870 }
2871
2872 /* open log & pid files before the chroot */
William Lallemand80293002017-11-06 11:00:03 +01002873 if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002874 unlink(global.pidfile);
2875 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
2876 if (pidfd < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002877 ha_alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002878 if (nb_oldpids)
2879 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02002880 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002881 exit(1);
2882 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002883 }
2884
Willy Tarreaub38651a2007-03-24 17:24:39 +01002885 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002886 ha_alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
2887 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002888 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01002889 exit(1);
2890 }
2891
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002892 /* If the user is not root, we'll still let him try the configuration
2893 * but we inform him that unexpected behaviour may occur.
2894 */
2895 if ((global.last_checks & LSTCHK_NETADM) && getuid())
Christopher Faulet767a84b2017-11-24 16:50:31 +01002896 ha_warning("[%s.main()] Some options which require full privileges"
2897 " might not work well.\n"
2898 "", argv[0]);
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002899
William Lallemand095ba4c2017-06-01 17:38:50 +02002900 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2901
2902 /* chroot if needed */
2903 if (global.chroot != NULL) {
2904 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002905 ha_alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02002906 if (nb_oldpids)
2907 tell_old_pids(SIGTTIN);
2908 protocol_unbind_all();
2909 exit(1);
2910 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002911 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002912 }
2913
William Lallemand944e6192018-11-21 15:48:31 +01002914 if (nb_oldpids && !(global.mode & MODE_MWORKER_WAIT))
Willy Tarreaubb545b42010-08-25 12:58:59 +02002915 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002916
William Lallemand8a361b52017-06-20 11:20:33 +02002917 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) {
2918 nb_oldpids = 0;
2919 free(oldpids);
2920 oldpids = NULL;
2921 }
2922
2923
Willy Tarreaubaaee002006-06-26 02:48:02 +02002924 /* Note that any error at this stage will be fatal because we will not
2925 * be able to restart the old pids.
2926 */
2927
William Lallemand095ba4c2017-06-01 17:38:50 +02002928 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2929 /* setgid / setuid */
2930 if (global.gid) {
2931 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +01002932 ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2933 " without 'uid'/'user' is generally useless.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02002934
2935 if (setgid(global.gid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002936 ha_alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
William Lallemand095ba4c2017-06-01 17:38:50 +02002937 protocol_unbind_all();
2938 exit(1);
2939 }
2940 }
Michael Schererab012dd2013-01-12 18:35:19 +01002941
William Lallemand095ba4c2017-06-01 17:38:50 +02002942 if (global.uid && setuid(global.uid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002943 ha_alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Michael Schererab012dd2013-01-12 18:35:19 +01002944 protocol_unbind_all();
2945 exit(1);
2946 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002947 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002948 /* check ulimits */
2949 limit.rlim_cur = limit.rlim_max = 0;
2950 getrlimit(RLIMIT_NOFILE, &limit);
2951 if (limit.rlim_cur < global.maxsock) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002952 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",
2953 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002954 }
2955
William Lallemand944e6192018-11-21 15:48:31 +01002956 if (global.mode & (MODE_DAEMON | MODE_MWORKER | MODE_MWORKER_WAIT)) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002957 struct proxy *px;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002958 struct peers *curpeers;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002959 int ret = 0;
2960 int proc;
William Lallemande1340412017-12-28 16:09:36 +01002961 int devnullfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002962
William Lallemand095ba4c2017-06-01 17:38:50 +02002963 /*
2964 * if daemon + mworker: must fork here to let a master
2965 * process live in background before forking children
2966 */
William Lallemand73b85e72017-06-01 17:38:51 +02002967
2968 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
2969 && (global.mode & MODE_MWORKER)
2970 && (global.mode & MODE_DAEMON)) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002971 ret = fork();
2972 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002973 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02002974 protocol_unbind_all();
2975 exit(1); /* there has been an error */
William Lallemandbfd8eb52018-07-04 15:31:23 +02002976 } else if (ret > 0) { /* parent leave to daemonize */
William Lallemand095ba4c2017-06-01 17:38:50 +02002977 exit(0);
William Lallemandbfd8eb52018-07-04 15:31:23 +02002978 } else /* change the process group ID in the child (master process) */
2979 setsid();
William Lallemand095ba4c2017-06-01 17:38:50 +02002980 }
William Lallemande20b6a62017-06-01 17:38:55 +02002981
William Lallemande20b6a62017-06-01 17:38:55 +02002982
William Lallemanddeed7802017-11-06 11:00:04 +01002983 /* if in master-worker mode, write the PID of the father */
2984 if (global.mode & MODE_MWORKER) {
2985 char pidstr[100];
2986 snprintf(pidstr, sizeof(pidstr), "%d\n", getpid());
Willy Tarreau46ec48b2018-01-23 19:20:19 +01002987 if (pidfd >= 0)
2988 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
William Lallemanddeed7802017-11-06 11:00:04 +01002989 }
2990
Willy Tarreaubaaee002006-06-26 02:48:02 +02002991 /* the father launches the required number of processes */
William Lallemand944e6192018-11-21 15:48:31 +01002992 if (!(global.mode & MODE_MWORKER_WAIT)) {
2993 children = calloc(global.nbproc, sizeof(int));
2994 for (proc = 0; proc < global.nbproc; proc++) {
2995 ret = fork();
2996 if (ret < 0) {
2997 ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
2998 protocol_unbind_all();
2999 exit(1); /* there has been an error */
3000 }
3001 else if (ret == 0) /* child breaks here */
3002 break;
3003 children[proc] = ret;
3004 if (pidfd >= 0 && !(global.mode & MODE_MWORKER)) {
3005 char pidstr[100];
3006 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
3007 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
3008 }
3009 if (global.mode & MODE_MWORKER) {
3010 struct mworker_proc *child;
William Lallemandce83b4a2018-10-26 14:47:30 +02003011
William Lallemand220567e2018-11-21 18:04:53 +01003012 ha_notice("New worker #%d (%d) forked\n", relative_pid, ret);
William Lallemand944e6192018-11-21 15:48:31 +01003013 /* find the right mworker_proc */
3014 list_for_each_entry(child, &proc_list, list) {
3015 if (child->relative_pid == relative_pid &&
3016 child->reloads == 0) {
3017 child->timestamp = now.tv_sec;
3018 child->pid = ret;
3019 break;
3020 }
William Lallemandce83b4a2018-10-26 14:47:30 +02003021 }
3022 }
William Lallemandbc193052018-09-11 10:06:26 +02003023
William Lallemand944e6192018-11-21 15:48:31 +01003024 relative_pid++; /* each child will get a different one */
3025 pid_bit <<= 1;
3026 }
3027 } else {
3028 /* wait mode */
3029 global.nbproc = 1;
3030 proc = 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003031 }
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003032
3033#ifdef USE_CPU_AFFINITY
3034 if (proc < global.nbproc && /* child */
Willy Tarreaue7597492015-04-20 11:36:57 +02003035 proc < LONGBITS && /* only the first 32/64 processes may be pinned */
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003036 global.cpu_map.proc[proc]) /* only do this if the process has a CPU map */
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003037#ifdef __FreeBSD__
Olivier Houchard97148f62017-08-16 17:29:11 +02003038 {
3039 cpuset_t cpuset;
3040 int i;
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003041 unsigned long cpu_map = global.cpu_map.proc[proc];
Olivier Houchard97148f62017-08-16 17:29:11 +02003042
3043 CPU_ZERO(&cpuset);
3044 while ((i = ffsl(cpu_map)) > 0) {
3045 CPU_SET(i - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003046 cpu_map &= ~(1UL << (i - 1));
Olivier Houchard97148f62017-08-16 17:29:11 +02003047 }
3048 ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset);
3049 }
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003050#else
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003051 sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map.proc[proc]);
Willy Tarreaufc6c0322012-11-16 16:12:27 +01003052#endif
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02003053#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003054 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02003055 if (pidfd >= 0) {
3056 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
3057 close(pidfd);
3058 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02003059
3060 /* We won't ever use this anymore */
Willy Tarreaud137dd32010-08-25 12:49:05 +02003061 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003062
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003063 if (proc == global.nbproc) {
William Lallemand944e6192018-11-21 15:48:31 +01003064 if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003065
3066 if ((!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
3067 (global.mode & MODE_DAEMON)) {
3068 /* detach from the tty, this is required to properly daemonize. */
William Lallemande1340412017-12-28 16:09:36 +01003069 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL))
3070 stdio_quiet(-1);
3071
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003072 global.mode &= ~MODE_VERBOSE;
3073 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
PiBa-NLbaf6ea42017-11-28 23:26:08 +01003074 }
3075
William Lallemandb3f2be32018-09-11 10:06:18 +02003076 mworker_loop();
William Lallemand1499b9b2017-06-07 15:04:47 +02003077 /* should never get there */
3078 exit(EXIT_FAILURE);
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003079 }
William Lallemandcf4e4962017-06-08 19:05:48 +02003080#if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH)
Grant Zhang872f9c22017-01-21 01:10:18 +00003081 ssl_free_dh();
3082#endif
William Lallemand1499b9b2017-06-07 15:04:47 +02003083 exit(0); /* parent must leave */
Willy Tarreauedaff0a2015-05-01 17:01:08 +02003084 }
3085
William Lallemandcb11fd22017-06-01 17:38:52 +02003086 /* child must never use the atexit function */
3087 atexit_flag = 0;
3088
William Lallemandbc193052018-09-11 10:06:26 +02003089 /* close useless master sockets */
3090 if (global.mode & MODE_MWORKER) {
3091 struct mworker_proc *child, *it;
3092 master = 0;
3093
William Lallemand309dc9a2018-10-26 14:47:45 +02003094 mworker_cli_proxy_stop();
3095
William Lallemandbc193052018-09-11 10:06:26 +02003096 /* free proc struct of other processes */
3097 list_for_each_entry_safe(child, it, &proc_list, list) {
William Lallemandce83b4a2018-10-26 14:47:30 +02003098 /* close the FD of the master side for all
3099 * workers, we don't need to close the worker
3100 * side of other workers since it's done with
3101 * the bind_proc */
3102 close(child->ipc_fd[0]);
3103 if (child->relative_pid == relative_pid &&
3104 child->reloads == 0) {
3105 /* keep this struct if this is our pid */
3106 proc_self = child;
William Lallemandbc193052018-09-11 10:06:26 +02003107 continue;
William Lallemandce83b4a2018-10-26 14:47:30 +02003108 }
William Lallemandbc193052018-09-11 10:06:26 +02003109 LIST_DEL(&child->list);
3110 free(child);
3111 }
3112 }
Willy Tarreau1605c7a2018-01-23 19:01:49 +01003113
William Lallemande1340412017-12-28 16:09:36 +01003114 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
3115 devnullfd = open("/dev/null", O_RDWR, 0);
3116 if (devnullfd < 0) {
3117 ha_alert("Cannot open /dev/null\n");
3118 exit(EXIT_FAILURE);
3119 }
3120 }
3121
William Lallemand095ba4c2017-06-01 17:38:50 +02003122 /* Must chroot and setgid/setuid in the children */
3123 /* chroot if needed */
3124 if (global.chroot != NULL) {
3125 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003126 ha_alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot);
William Lallemand095ba4c2017-06-01 17:38:50 +02003127 if (nb_oldpids)
3128 tell_old_pids(SIGTTIN);
3129 protocol_unbind_all();
3130 exit(1);
3131 }
3132 }
3133
3134 free(global.chroot);
3135 global.chroot = NULL;
3136
3137 /* setgid / setuid */
3138 if (global.gid) {
3139 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003140 ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
3141 " without 'uid'/'user' is generally useless.\n", argv[0]);
William Lallemand095ba4c2017-06-01 17:38:50 +02003142
3143 if (setgid(global.gid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003144 ha_alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
William Lallemand095ba4c2017-06-01 17:38:50 +02003145 protocol_unbind_all();
3146 exit(1);
3147 }
3148 }
3149
3150 if (global.uid && setuid(global.uid) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003151 ha_alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
William Lallemand095ba4c2017-06-01 17:38:50 +02003152 protocol_unbind_all();
3153 exit(1);
3154 }
3155
William Lallemand7f80eb22017-05-26 18:19:55 +02003156 /* pass through every cli socket, and check if it's bound to
3157 * the current process and if it exposes listeners sockets.
3158 * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork.
3159 * */
3160
3161 if (global.stats_fe) {
3162 struct bind_conf *bind_conf;
3163
3164 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
3165 if (bind_conf->level & ACCESS_FD_LISTENERS) {
3166 if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) {
3167 global.tune.options |= GTUNE_SOCKET_TRANSFER;
3168 break;
3169 }
3170 }
3171 }
3172 }
3173
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003174 /* we might have to unbind some proxies from some processes */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003175 px = proxies_list;
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003176 while (px != NULL) {
3177 if (px->bind_proc && px->state != PR_STSTOPPED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02003178 if (!(px->bind_proc & (1UL << proc))) {
3179 if (global.tune.options & GTUNE_SOCKET_TRANSFER)
3180 zombify_proxy(px);
3181 else
3182 stop_proxy(px);
3183 }
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01003184 }
3185 px = px->next;
3186 }
3187
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003188 /* we might have to unbind some peers sections from some processes */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003189 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003190 if (!curpeers->peers_fe)
3191 continue;
3192
3193 if (curpeers->peers_fe->bind_proc & (1UL << proc))
3194 continue;
3195
3196 stop_proxy(curpeers->peers_fe);
3197 /* disable this peer section so that it kills itself */
Willy Tarreau47c8c022015-09-28 16:39:25 +02003198 signal_unregister_handler(curpeers->sighandler);
3199 task_delete(curpeers->sync_task);
3200 task_free(curpeers->sync_task);
3201 curpeers->sync_task = NULL;
3202 task_free(curpeers->peers_fe->task);
3203 curpeers->peers_fe->task = NULL;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02003204 curpeers->peers_fe = NULL;
3205 }
3206
William Lallemand2e8fad92018-11-13 16:18:23 +01003207 /*
3208 * This is only done in daemon mode because we might want the
3209 * logs on stdout in mworker mode. If we're NOT in QUIET mode,
3210 * we should now close the 3 first FDs to ensure that we can
3211 * detach from the TTY. We MUST NOT do it in other cases since
3212 * it would have already be done, and 0-2 would have been
3213 * affected to listening sockets
Willy Tarreaubaaee002006-06-26 02:48:02 +02003214 */
William Lallemand2e8fad92018-11-13 16:18:23 +01003215 if ((global.mode & MODE_DAEMON) &&
3216 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003217 /* detach from the tty */
William Lallemande1340412017-12-28 16:09:36 +01003218 stdio_quiet(devnullfd);
Willy Tarreau106cb762008-11-16 07:40:34 +01003219 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003220 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
3221 }
3222 pid = getpid(); /* update child's pid */
William Lallemandbfd8eb52018-07-04 15:31:23 +02003223 if (!(global.mode & MODE_MWORKER)) /* in mworker mode we don't want a new pgid for the children */
3224 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02003225 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02003226 }
3227
Christopher Faulete3a5e352017-10-24 13:53:54 +02003228 global.mode &= ~MODE_STARTING;
Willy Tarreau4f60f162007-04-08 16:39:58 +02003229 /*
3230 * That's it : the central polling loop. Run until we stop.
3231 */
Christopher Faulet1d17c102017-08-29 15:38:48 +02003232#ifdef USE_THREAD
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003233 {
Christopher Faulet1d17c102017-08-29 15:38:48 +02003234 unsigned int *tids = calloc(global.nbthread, sizeof(unsigned int));
3235 pthread_t *threads = calloc(global.nbthread, sizeof(pthread_t));
3236 int i;
William Lallemand1aab50b2018-06-07 09:46:01 +02003237 sigset_t blocked_sig, old_sig;
Christopher Faulet1d17c102017-08-29 15:38:48 +02003238
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003239 /* Init tids array */
3240 for (i = 0; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02003241 tids[i] = i;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003242
William Lallemand1aab50b2018-06-07 09:46:01 +02003243 /* ensure the signals will be blocked in every thread */
3244 sigfillset(&blocked_sig);
3245 sigdelset(&blocked_sig, SIGPROF);
3246 sigdelset(&blocked_sig, SIGBUS);
3247 sigdelset(&blocked_sig, SIGFPE);
3248 sigdelset(&blocked_sig, SIGILL);
3249 sigdelset(&blocked_sig, SIGSEGV);
3250 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
3251
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003252 /* Create nbthread-1 thread. The first thread is the current process */
3253 threads[0] = pthread_self();
3254 for (i = 1; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02003255 pthread_create(&threads[i], NULL, &run_thread_poll_loop, &tids[i]);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003256
Christopher Faulet62519022017-10-16 15:49:32 +02003257#ifdef USE_CPU_AFFINITY
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003258 /* Now the CPU affinity for all threads */
3259 for (i = 0; i < global.nbthread; i++) {
Christopher Fauletcb6a9452017-11-22 16:50:41 +01003260 if (global.cpu_map.proc[relative_pid-1])
3261 global.cpu_map.thread[relative_pid-1][i] &= global.cpu_map.proc[relative_pid-1];
Christopher Faulet62519022017-10-16 15:49:32 +02003262
Willy Tarreau421f02e2018-01-20 18:19:22 +01003263 if (i < MAX_THREADS && /* only the first 32/64 threads may be pinned */
Olivier Houchard829aa242017-12-01 18:19:43 +01003264 global.cpu_map.thread[relative_pid-1][i]) {/* only do this if the thread has a THREAD map */
3265#if defined(__FreeBSD__) || defined(__NetBSD__)
3266 cpuset_t cpuset;
3267#else
3268 cpu_set_t cpuset;
3269#endif
3270 int j;
3271 unsigned long cpu_map = global.cpu_map.thread[relative_pid-1][i];
3272
3273 CPU_ZERO(&cpuset);
3274
3275 while ((j = ffsl(cpu_map)) > 0) {
3276 CPU_SET(j - 1, &cpuset);
Cyril Bontéd400ab32018-03-12 21:47:39 +01003277 cpu_map &= ~(1UL << (j - 1));
Olivier Houchard829aa242017-12-01 18:19:43 +01003278 }
Christopher Faulet62519022017-10-16 15:49:32 +02003279 pthread_setaffinity_np(threads[i],
Olivier Houchard829aa242017-12-01 18:19:43 +01003280 sizeof(cpuset), &cpuset);
3281 }
Christopher Faulet1d17c102017-08-29 15:38:48 +02003282 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003283#endif /* !USE_CPU_AFFINITY */
3284
William Lallemand1aab50b2018-06-07 09:46:01 +02003285 /* when multithreading we need to let only the thread 0 handle the signals */
William Lallemandd3801c12018-09-11 10:06:23 +02003286 haproxy_unblock_signals();
William Lallemand1aab50b2018-06-07 09:46:01 +02003287
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003288 /* Finally, start the poll loop for the first thread */
3289 run_thread_poll_loop(&tids[0]);
3290
3291 /* Wait the end of other threads */
3292 for (i = 1; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02003293 pthread_join(threads[i], NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003294
Christopher Faulet1d17c102017-08-29 15:38:48 +02003295 free(tids);
3296 free(threads);
3297
Christopher Fauletb79a94c2017-05-30 15:34:30 +02003298#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
3299 show_lock_stats();
3300#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003301 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003302#else /* ! USE_THREAD */
William Lallemandd3801c12018-09-11 10:06:23 +02003303 haproxy_unblock_signals();
Christopher Fauletcd7879a2017-10-27 13:53:47 +02003304 run_thread_poll_loop((int []){0});
Christopher Faulet62519022017-10-16 15:49:32 +02003305
Christopher Faulet62519022017-10-16 15:49:32 +02003306#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02003307
3308 /* Do some cleanup */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003309 deinit();
Christopher Faulet1d17c102017-08-29 15:38:48 +02003310
Willy Tarreaubaaee002006-06-26 02:48:02 +02003311 exit(0);
3312}
3313
3314
3315/*
3316 * Local variables:
3317 * c-indent-level: 8
3318 * c-basic-offset: 8
3319 * End:
3320 */