blob: c196d51320558229b84832331284fe68c9f1ddb0 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HA-Proxy : High Availability-enabled HTTP/TCP proxy
Willy Tarreau7b677262017-04-03 09:27:49 +02003 * Copyright 2000-2017 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>
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +020055#ifdef __FreeBSD__
56#include <sys/param.h>
57#include <sys/cpuset.h>
58#endif
Willy Tarreaufc6c0322012-11-16 16:12:27 +010059#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020060
61#ifdef DEBUG_FULL
62#include <assert.h>
63#endif
64
Willy Tarreau2dd0d472006-06-29 17:53:05 +020065#include <common/base64.h>
66#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020067#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020068#include <common/compat.h>
69#include <common/config.h>
70#include <common/defaults.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010071#include <common/errors.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020072#include <common/memory.h>
73#include <common/mini-clist.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010074#include <common/namespace.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020075#include <common/regex.h>
76#include <common/standard.h>
77#include <common/time.h>
78#include <common/uri_auth.h>
79#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020080
81#include <types/capture.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020082#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020083#include <types/global.h>
Simon Hormanac821422011-07-15 13:14:09 +090084#include <types/acl.h>
Willy Tarreau3c63fd82011-09-07 18:00:47 +020085#include <types/peers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020086
Willy Tarreau0fc45a72007-06-17 00:36:03 +020087#include <proto/acl.h>
Willy Tarreau3c595ac2015-04-19 09:59:31 +020088#include <proto/applet.h>
Willy Tarreau2e845be2012-10-19 19:49:09 +020089#include <proto/arg.h>
Willy Tarreau3c595ac2015-04-19 09:59:31 +020090#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020091#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020092#include <proto/channel.h>
Willy Tarreauf2943dc2012-10-26 20:10:28 +020093#include <proto/connection.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020094#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020095#include <proto/filters.h>
Willy Tarreau34eb6712011-10-24 18:15:04 +020096#include <proto/hdr_idx.h>
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010097#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020098#include <proto/listener.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020099#include <proto/log.h>
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100100#include <proto/pattern.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +0200101#include <proto/protocol.h>
Willy Tarreau80587432006-12-24 17:47:20 +0100102#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103#include <proto/proxy.h>
104#include <proto/queue.h>
105#include <proto/server.h>
Willy Tarreaub1ec8c42015-04-03 13:53:24 +0200106#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +0200107#include <proto/stream.h>
Willy Tarreau29857942009-05-10 09:01:21 +0200108#include <proto/signal.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109#include <proto/task.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +0200110#include <proto/dns.h>
Christopher Fauletff2613e2016-11-09 11:36:17 +0100111#include <proto/vars.h>
Grant Zhang872f9c22017-01-21 01:10:18 +0000112#include <proto/ssl_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113
Willy Tarreau477ecd82010-01-03 21:12:30 +0100114/* list of config files */
115static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100117int relative_pid = 1; /* process id starting at 1 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118
119/* global options */
120struct global global = {
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100121 .hard_stop_after = TICK_ETERNITY,
Willy Tarreau247a13a2012-11-15 17:38:15 +0100122 .nbproc = 1,
William Lallemand5f232402012-04-05 18:02:55 +0200123 .req_count = 0,
William Lallemand0f99e342011-10-12 17:50:54 +0200124 .logsrvs = LIST_HEAD_INIT(global.logsrvs),
William Lallemand9d5f5482012-11-07 16:12:57 +0100125 .maxzlibmem = 0,
William Lallemandd85f9172012-11-09 17:05:39 +0100126 .comp_rate_lim = 0,
Emeric Brun850efd52014-01-29 12:24:34 +0100127 .ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED,
Emeric Bruned760922010-10-22 17:59:25 +0200128 .unix_bind = {
129 .ux = {
130 .uid = -1,
131 .gid = -1,
132 .mode = 0,
133 }
134 },
Willy Tarreau27a674e2009-08-17 07:23:33 +0200135 .tune = {
136 .bufsize = BUFSIZE,
Willy Tarreau27097842015-09-28 13:53:23 +0200137 .maxrewrite = -1,
Willy Tarreau43961d52010-10-04 20:39:20 +0200138 .chksize = BUFSIZE,
Willy Tarreaua24adf02014-11-27 01:11:56 +0100139 .reserved_bufs = RESERVED_BUFS,
Willy Tarreauf3045d22015-04-29 16:24:50 +0200140 .pattern_cache = DEFAULT_PAT_LRU_SIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200141#ifdef USE_OPENSSL
Emeric Brun46635772012-11-14 11:32:56 +0100142 .sslcachesize = SSLCACHESIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200143#endif
William Lallemandf3747832012-11-09 12:33:10 +0100144 .comp_maxlevel = 1,
Willy Tarreau7e312732014-02-12 16:35:14 +0100145#ifdef DEFAULT_IDLE_TIMER
146 .idle_timer = DEFAULT_IDLE_TIMER,
147#else
148 .idle_timer = 1000, /* 1 second */
149#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200150 },
Emeric Brun76d88952012-10-05 15:47:31 +0200151#ifdef USE_OPENSSL
152#ifdef DEFAULT_MAXSSLCONN
Willy Tarreau403edff2012-09-06 11:58:37 +0200153 .maxsslconn = DEFAULT_MAXSSLCONN,
154#endif
Emeric Brun76d88952012-10-05 15:47:31 +0200155#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200156 /* others NULL OK */
157};
158
159/*********************************************************************/
160
161int stopping; /* non zero means stopping in progress */
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100162int killed; /* non zero means a hard-stop is triggered */
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200163int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164
165/* Here we store informations about the pids of the processes we may pause
166 * or kill. We will send them a signal every 10 ms until we can bind to all
167 * our ports. With 200 retries, that's about 2 seconds.
168 */
169#define MAX_START_RETRIES 200
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170static int *oldpids = NULL;
171static int oldpids_sig; /* use USR1 or TERM */
172
Olivier Houchardf73629d2017-04-05 22:33:04 +0200173/* Path to the unix socket we use to retrieve listener sockets from the old process */
174static const char *old_unixsocket;
175
Willy Tarreaubaaee002006-06-26 02:48:02 +0200176/* this is used to drain data, and as a temporary buffer for sprintf()... */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100177struct chunk trash = { };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200178
Willy Tarreau8096de92010-02-26 11:12:27 +0100179/* this buffer is always the same size as standard buffers and is used for
180 * swapping data inside a buffer.
181 */
182char *swap_buffer = NULL;
183
William Lallemandcb11fd22017-06-01 17:38:52 +0200184int atexit_flag = 0;
185
Willy Tarreaubb545b42010-08-25 12:58:59 +0200186int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187const int zero = 0;
188const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200189const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200190
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100191char hostname[MAX_HOSTNAME_LEN];
Emeric Brun2b920a12010-09-23 18:30:22 +0200192char localpeer[MAX_HOSTNAME_LEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200193
Willy Tarreau89efaed2013-12-13 15:14:55 +0100194/* used from everywhere just to drain results we don't want to read and which
195 * recent versions of gcc increasingly and annoyingly complain about.
196 */
197int shut_your_big_mouth_gcc_int = 0;
198
William Lallemand73b85e72017-06-01 17:38:51 +0200199int *children = NULL; /* store PIDs of children in master workers mode */
200
201static volatile sig_atomic_t caught_signal = 0;
202static char **next_argv = NULL;
203
Willy Tarreau08ceb102011-07-24 22:58:00 +0200204/* list of the temporarily limited listeners because of lack of resource */
205struct list global_listener_queue = LIST_HEAD_INIT(global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +0200206struct task *global_listener_queue_task;
207static struct task *manage_global_listener_queue(struct task *t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200208
Willy Tarreauff055502014-04-28 22:27:06 +0200209/* bitfield of a few warnings to emit just once (WARN_*) */
210unsigned int warned = 0;
211
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100212
213/* These are strings to be reported in the output of "haproxy -vv". They may
214 * either be constants (in which case must_free must be zero) or dynamically
215 * allocated strings to pass to free() on exit, and in this case must_free
216 * must be non-zero.
217 */
218struct list build_opts_list = LIST_HEAD_INIT(build_opts_list);
219struct build_opts_str {
220 struct list list;
221 const char *str;
222 int must_free;
223};
224
Willy Tarreaue6945732016-12-21 19:57:00 +0100225/* These functions are called just after the point where the program exits
226 * after a config validity check, so they are generally suited for resource
227 * allocation and slow initializations that should be skipped during basic
228 * config checks. The functions must return 0 on success, or a combination
229 * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause
230 * and immediate exit, so the function must have emitted any useful error.
231 */
232struct list post_check_list = LIST_HEAD_INIT(post_check_list);
233struct post_check_fct {
234 struct list list;
235 int (*fct)();
236};
237
Willy Tarreau05554e62016-12-21 20:46:26 +0100238/* These functions are called when freeing the global sections at the end
239 * of deinit, after everything is stopped. They don't return anything, and
240 * they work in best effort mode as their sole goal is to make valgrind
241 * mostly happy.
242 */
243struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list);
244struct post_deinit_fct {
245 struct list list;
246 void (*fct)();
247};
248
Willy Tarreaubaaee002006-06-26 02:48:02 +0200249/*********************************************************************/
250/* general purpose functions ***************************************/
251/*********************************************************************/
252
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100253/* used to register some build option strings at boot. Set must_free to
254 * non-zero if the string must be freed upon exit.
255 */
256void hap_register_build_opts(const char *str, int must_free)
257{
258 struct build_opts_str *b;
259
260 b = calloc(1, sizeof(*b));
261 if (!b) {
262 fprintf(stderr, "out of memory\n");
263 exit(1);
264 }
265 b->str = str;
266 b->must_free = must_free;
267 LIST_ADDQ(&build_opts_list, &b->list);
268}
269
Willy Tarreaue6945732016-12-21 19:57:00 +0100270/* used to register some initialization functions to call after the checks. */
271void hap_register_post_check(int (*fct)())
272{
273 struct post_check_fct *b;
274
275 b = calloc(1, sizeof(*b));
276 if (!b) {
277 fprintf(stderr, "out of memory\n");
278 exit(1);
279 }
280 b->fct = fct;
281 LIST_ADDQ(&post_check_list, &b->list);
282}
283
Willy Tarreau05554e62016-12-21 20:46:26 +0100284/* used to register some de-initialization functions to call after everything
285 * has stopped.
286 */
287void hap_register_post_deinit(void (*fct)())
288{
289 struct post_deinit_fct *b;
290
291 b = calloc(1, sizeof(*b));
292 if (!b) {
293 fprintf(stderr, "out of memory\n");
294 exit(1);
295 }
296 b->fct = fct;
297 LIST_ADDQ(&post_deinit_list, &b->list);
298}
299
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100300static void display_version()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301{
302 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau7b677262017-04-03 09:27:49 +0200303 printf("Copyright 2000-2017 Willy Tarreau <willy@haproxy.org>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200304}
305
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100306static void display_build_opts()
Willy Tarreau7b066db2007-12-02 11:28:59 +0100307{
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100308 struct build_opts_str *item;
309
Willy Tarreau7b066db2007-12-02 11:28:59 +0100310 printf("Build options :"
311#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100312 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100313#endif
314#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100315 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100316#endif
317#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100318 "\n CC = " BUILD_CC
319#endif
320#ifdef BUILD_CFLAGS
321 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100322#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100323#ifdef BUILD_OPTIONS
324 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100325#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200326 "\n\nDefault settings :"
327 "\n maxconn = %d, bufsize = %d, maxrewrite = %d, maxpollevents = %d"
328 "\n\n",
329 DEFAULT_MAXCONN, BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS);
Willy Tarreaube5b6852009-10-03 18:57:08 +0200330
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100331 list_for_each_entry(item, &build_opts_list, list) {
332 puts(item->str);
333 }
334
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100335 putchar('\n');
336
Willy Tarreaube5b6852009-10-03 18:57:08 +0200337 list_pollers(stdout);
338 putchar('\n');
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100339 list_filters(stdout);
340 putchar('\n');
Willy Tarreau7b066db2007-12-02 11:28:59 +0100341}
342
Willy Tarreaubaaee002006-06-26 02:48:02 +0200343/*
344 * This function prints the command line usage and exits
345 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100346static void usage(char *name)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200347{
348 display_version();
349 fprintf(stderr,
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200350 "Usage : %s [-f <cfgfile|cfgdir>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
Willy Tarreaua088d312015-10-08 11:58:48 +0200352 " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100353 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200354 " -d enters debug mode ; -db only disables background mode.\n"
Willy Tarreau6e064432012-05-08 15:40:42 +0200355 " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200357 " -D goes daemon ; -C changes to <dir> before loading files.\n"
William Lallemand095ba4c2017-06-01 17:38:50 +0200358 " -W master-worker mode.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200360 " -c check mode : only check config files and exit\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361 " -n sets the maximum total # of connections (%d)\n"
362 " -m limits the usable amount of memory (in MB)\n"
363 " -N sets the default, per-proxy maximum # of connections (%d)\n"
Emeric Brun2b920a12010-09-23 18:30:22 +0200364 " -L set local peer name (default to hostname)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365 " -p writes pids of all children to this file\n"
366#if defined(ENABLE_EPOLL)
367 " -de disables epoll() usage even when available\n"
368#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200369#if defined(ENABLE_KQUEUE)
370 " -dk disables kqueue() usage even when available\n"
371#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372#if defined(ENABLE_POLL)
373 " -dp disables poll() usage even when available\n"
374#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200375#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100376 " -dS disables splice usage (broken on old kernels)\n"
377#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200378#if defined(USE_GETADDRINFO)
379 " -dG disables getaddrinfo() usage\n"
380#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000381#if defined(SO_REUSEPORT)
382 " -dR disables SO_REUSEPORT usage\n"
383#endif
Willy Tarreau3eed10e2016-11-07 21:03:16 +0100384 " -dr ignores server address resolution failures\n"
Emeric Brun850efd52014-01-29 12:24:34 +0100385 " -dV disables SSL verify on servers side\n"
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +0200386 " -sf/-st [pid ]* finishes/terminates old pids.\n"
Olivier Houchardf73629d2017-04-05 22:33:04 +0200387 " -x <unix_socket> get listening sockets from a unix socket\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388 "\n",
389 name, DEFAULT_MAXCONN, cfg_maxpconn);
390 exit(1);
391}
392
393
394
395/*********************************************************************/
396/* more specific functions ***************************************/
397/*********************************************************************/
398
William Lallemand73b85e72017-06-01 17:38:51 +0200399/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
400 * pids the signal was correctly delivered to.
401 */
402static int tell_old_pids(int sig)
403{
404 int p;
405 int ret = 0;
406 for (p = 0; p < nb_oldpids; p++)
407 if (kill(oldpids[p], sig) == 0)
408 ret++;
409 return ret;
410}
411
412/* return 1 if a pid is a current child otherwise 0 */
413
414int current_child(int pid)
415{
416 int i;
417
418 for (i = 0; i < global.nbproc; i++) {
419 if (children[i] == pid)
420 return 1;
421 }
422 return 0;
423}
424
425static void mworker_signalhandler(int signum)
426{
427 caught_signal = signum;
428}
429
430static void mworker_register_signals()
431{
432 struct sigaction sa;
433 /* Here we are not using the haproxy async way
434 for signals because it does not exists in
435 the master */
436 memset(&sa, 0, sizeof(struct sigaction));
437 sa.sa_handler = &mworker_signalhandler;
438 sigaction(SIGHUP, &sa, NULL);
439 sigaction(SIGUSR1, &sa, NULL);
440 sigaction(SIGUSR2, &sa, NULL);
441 sigaction(SIGINT, &sa, NULL);
442 sigaction(SIGTERM, &sa, NULL);
443}
444
445static void mworker_block_signals()
446{
447 sigset_t set;
448
449 sigemptyset(&set);
450 sigaddset(&set, SIGUSR1);
451 sigaddset(&set, SIGUSR2);
452 sigaddset(&set, SIGHUP);
453 sigaddset(&set, SIGINT);
454 sigaddset(&set, SIGTERM);
455 sigprocmask(SIG_SETMASK, &set, NULL);
456}
457
458static void mworker_unblock_signals()
459{
460 sigset_t set;
461
462 sigemptyset(&set);
463 sigaddset(&set, SIGUSR1);
464 sigaddset(&set, SIGUSR2);
465 sigaddset(&set, SIGHUP);
466 sigaddset(&set, SIGINT);
467 sigaddset(&set, SIGTERM);
468 sigprocmask(SIG_UNBLOCK, &set, NULL);
469}
470
471static void mworker_unregister_signals()
472{
473 signal(SIGINT, SIG_DFL);
474 signal(SIGTERM, SIG_DFL);
475 signal(SIGHUP, SIG_IGN);
476 signal(SIGUSR1, SIG_IGN);
477 signal(SIGUSR2, SIG_IGN);
478}
479
480/*
481 * Send signal to every known children.
482 */
483
484static void mworker_kill(int sig)
485{
486 int i;
487
488 tell_old_pids(sig);
489 if (children) {
490 for (i = 0; i < global.nbproc; i++)
491 kill(children[i], sig);
492 }
493}
494
Willy Tarreaubaaee002006-06-26 02:48:02 +0200495/*
William Lallemand73b85e72017-06-01 17:38:51 +0200496 * remove a pid forom the olpid array and decrease nb_oldpids
497 * return 1 pid was found otherwise return 0
498 */
499
500int delete_oldpid(int pid)
501{
502 int i;
503
504 for (i = 0; i < nb_oldpids; i++) {
505 if (oldpids[i] == pid) {
506 oldpids[i] = oldpids[nb_oldpids - 1];
507 oldpids[nb_oldpids - 1] = 0;
508 nb_oldpids--;
509 return 1;
510 }
511 }
512 return 0;
513}
514
515/*
516 * When called, this function reexec haproxy with -sf followed by current
517 * children PIDs and possibily old children PIDs if they didn't leave yet.
518 */
519static void mworker_reload()
520{
521 int next_argc = 0;
522 int j;
523 char *msg = NULL;
524
525 mworker_block_signals();
526 mworker_unregister_signals();
527 setenv("HAPROXY_MWORKER_REEXEC", "1", 1);
528
529 /* compute length */
530 while (next_argv[next_argc])
531 next_argc++;
532
533 /* 1 for haproxy -sf */
534 next_argv = realloc(next_argv, (next_argc + 1 + global.nbproc + nb_oldpids + 1) * sizeof(char *));
535 if (next_argv == NULL)
536 goto alloc_error;
537
538
539 /* add -sf <PID>* to argv */
540 if (children || nb_oldpids > 0)
541 next_argv[next_argc++] = "-sf";
542 if (children) {
543 for (j = 0; j < global.nbproc; next_argc++,j++) {
544 next_argv[next_argc] = memprintf(&msg, "%d", children[j]);
545 if (next_argv[next_argc] == NULL)
546 goto alloc_error;
547 msg = NULL;
548 }
549 }
550 /* copy old process PIDs */
551 for (j = 0; j < nb_oldpids; next_argc++,j++) {
552 next_argv[next_argc] = memprintf(&msg, "%d", oldpids[j]);
553 if (next_argv[next_argc] == NULL)
554 goto alloc_error;
555 msg = NULL;
556 }
557 next_argv[next_argc] = NULL;
558 deinit(); /* we don't want to leak FD there */
559 Warning("Reexecuting Master process\n");
560 execv(next_argv[0], next_argv);
561
562alloc_error:
563 Warning("Cannot allocate memory\n");
564 Warning("Failed to reexecute the master processs [%d]\n", pid);
565 return;
566}
567
568
569/*
570 * Wait for every children to exit
571 */
572
573static void mworker_wait()
574{
575 int exitpid = -1;
576 int status = 0;
577
578 mworker_register_signals();
579 mworker_unblock_signals();
580
581 while (1) {
582
583 while (((exitpid = wait(&status)) == -1) && errno == EINTR) {
584 int sig = caught_signal;
585 if (sig == SIGUSR2 || sig == SIGHUP) {
586 mworker_reload();
587 } else {
588 Warning("Exiting Master process...\n");
589 mworker_kill(sig);
590 mworker_unregister_signals();
591 }
592 caught_signal = 0;
593 }
594
595 if (exitpid == -1 && errno == ECHILD) {
596 Warning("All workers are left. Leaving... (%d)\n", status);
William Lallemandcb11fd22017-06-01 17:38:52 +0200597 atexit_flag = 0;
William Lallemand73b85e72017-06-01 17:38:51 +0200598 exit(status); /* parent must leave using the latest status code known */
599 }
600
601 if (WIFEXITED(status))
602 status = WEXITSTATUS(status);
603 else if (WIFSIGNALED(status))
604 status = 128 + WTERMSIG(status);
605 else if (WIFSTOPPED(status))
606 status = 128 + WSTOPSIG(status);
607 else
608 status = 255;
609
610 if (!children) {
611 Warning("Worker %d left with exit code %d\n", exitpid, status);
612 } else {
613 /* check if exited child was in the current children list */
614 if (current_child(exitpid)) {
615 Alert("Current worker %d left with exit code %d\n", exitpid, status);
616 } else {
617 Warning("Former worker %d left with exit code %d\n", exitpid, status);
618 delete_oldpid(exitpid);
619 }
620 }
621 }
622}
623
William Lallemandcb11fd22017-06-01 17:38:52 +0200624
625/*
626 * Reexec the process in failure mode, instead of exiting
627 */
628void reexec_on_failure()
629{
630 if (!atexit_flag)
631 return;
632
633 setenv("HAPROXY_MWORKER_WAIT_ONLY", "1", 1);
634
635 Warning("Reexecuting Master process in waitpid mode\n");
636 mworker_reload();
637
638 Warning("Failed to reexecute the master processs\n");
639}
William Lallemand73b85e72017-06-01 17:38:51 +0200640
641
642/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200643 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
644 * a signal zero to all subscribers. This means that it's as easy as
645 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200646 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100647static void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648{
649 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200650 signal_unregister_handler(sh);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200651 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200652}
653
654/*
655 * upon SIGTTOU, we pause everything
656 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100657static void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200658{
659 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200660 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200661}
662
663/*
664 * upon SIGTTIN, let's have a soft stop.
665 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100666static void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667{
Willy Tarreaube58c382011-07-24 18:28:10 +0200668 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669}
670
671/*
672 * this function dumps every server's state when the process receives SIGHUP.
673 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100674static void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675{
676 struct proxy *p = proxy;
677
678 Warning("SIGHUP received, dumping servers states.\n");
679 while (p) {
680 struct server *s = p->srv;
681
682 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
683 while (s) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100684 chunk_printf(&trash,
685 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
686 p->id, s->id,
Willy Tarreau892337c2014-05-13 23:41:20 +0200687 (s->state != SRV_ST_STOPPED) ? "UP" : "DOWN",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100688 s->cur_sess, s->nbpend, s->counters.cum_sess);
689 Warning("%s\n", trash.str);
690 send_log(p, LOG_NOTICE, "%s\n", trash.str);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200691 s = s->next;
692 }
693
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200694 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
695 if (!p->srv) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100696 chunk_printf(&trash,
697 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
698 p->id,
699 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 +0200700 } else if (p->srv_act == 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100701 chunk_printf(&trash,
702 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
703 p->id,
704 (p->srv_bck) ? "is running on backup servers" : "has no server available",
705 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 +0200706 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100707 chunk_printf(&trash,
708 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
709 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
710 p->id, p->srv_act, p->srv_bck,
711 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 +0200712 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100713 Warning("%s\n", trash.str);
714 send_log(p, LOG_NOTICE, "%s\n", trash.str);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200715
716 p = p->next;
717 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200718}
719
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100720static void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200721{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200722 /* dump memory usage then free everything possible */
723 dump_pools();
724 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725}
726
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200727/* This function check if cfg_cfgfiles containes directories.
728 * If it find one, it add all the files (and only files) it containes
729 * in cfg_cfgfiles in place of the directory (and remove the directory).
730 * It add the files in lexical order.
731 * It add only files with .cfg extension.
732 * It doesn't add files with name starting with '.'
733 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100734static void cfgfiles_expand_directories(void)
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200735{
736 struct wordlist *wl, *wlb;
737 char *err = NULL;
738
739 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
740 struct stat file_stat;
741 struct dirent **dir_entries = NULL;
742 int dir_entries_nb;
743 int dir_entries_it;
744
745 if (stat(wl->s, &file_stat)) {
746 Alert("Cannot open configuration file/directory %s : %s\n",
747 wl->s,
748 strerror(errno));
749 exit(1);
750 }
751
752 if (!S_ISDIR(file_stat.st_mode))
753 continue;
754
755 /* from this point wl->s is a directory */
756
757 dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort);
758 if (dir_entries_nb < 0) {
759 Alert("Cannot open configuration directory %s : %s\n",
760 wl->s,
761 strerror(errno));
762 exit(1);
763 }
764
765 /* for each element in the directory wl->s */
766 for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) {
767 struct dirent *dir_entry = dir_entries[dir_entries_it];
768 char *filename = NULL;
769 char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg");
770
771 /* don't add filename that begin with .
772 * only add filename with .cfg extention
773 */
774 if (dir_entry->d_name[0] == '.' ||
775 !(d_name_cfgext && d_name_cfgext[4] == '\0'))
776 goto next_dir_entry;
777
778 if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) {
779 Alert("Cannot load configuration files %s : out of memory.\n",
780 filename);
781 exit(1);
782 }
783
784 if (stat(filename, &file_stat)) {
785 Alert("Cannot open configuration file %s : %s\n",
786 wl->s,
787 strerror(errno));
788 exit(1);
789 }
790
791 /* don't add anything else than regular file in cfg_cfgfiles
792 * this way we avoid loops
793 */
794 if (!S_ISREG(file_stat.st_mode))
795 goto next_dir_entry;
796
797 if (!list_append_word(&wl->list, filename, &err)) {
798 Alert("Cannot load configuration files %s : %s\n",
799 filename,
800 err);
801 exit(1);
802 }
803
804next_dir_entry:
805 free(filename);
806 free(dir_entry);
807 }
808
809 free(dir_entries);
810
811 /* remove the current directory (wl) from cfg_cfgfiles */
812 free(wl->s);
813 LIST_DEL(&wl->list);
814 free(wl);
815 }
816
817 free(err);
818}
819
Olivier Houchardf73629d2017-04-05 22:33:04 +0200820static int get_old_sockets(const char *unixsocket)
821{
822 char *cmsgbuf = NULL, *tmpbuf = NULL;
823 int *tmpfd = NULL;
824 struct sockaddr_un addr;
825 struct cmsghdr *cmsg;
826 struct msghdr msghdr;
827 struct iovec iov;
828 struct xfer_sock_list *xfer_sock = NULL;
Olivier Houchard54740872017-04-06 14:45:14 +0200829 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf73629d2017-04-05 22:33:04 +0200830 int sock = -1;
831 int ret = -1;
832 int ret2 = -1;
833 int fd_nb;
834 int got_fd = 0;
835 int i = 0;
836 size_t maxoff = 0, curoff = 0;
837
838 memset(&msghdr, 0, sizeof(msghdr));
839 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
840 if (!cmsgbuf) {
841 Warning("Failed to allocate memory to send sockets\n");
842 goto out;
843 }
844 sock = socket(PF_UNIX, SOCK_STREAM, 0);
845 if (sock < 0) {
846 Warning("Failed to connect to the old process socket '%s'\n",
847 unixsocket);
848 goto out;
849 }
850 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path));
851 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
852 addr.sun_family = PF_UNIX;
853 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
854 if (ret < 0) {
855 Warning("Failed to connect to the old process socket '%s'\n",
856 unixsocket);
857 goto out;
858 }
Olivier Houchard54740872017-04-06 14:45:14 +0200859 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf73629d2017-04-05 22:33:04 +0200860 iov.iov_base = &fd_nb;
861 iov.iov_len = sizeof(fd_nb);
862 msghdr.msg_iov = &iov;
863 msghdr.msg_iovlen = 1;
864 send(sock, "_getsocks\n", strlen("_getsocks\n"), 0);
865 /* First, get the number of file descriptors to be received */
866 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
867 Warning("Failed to get the number of sockets to be transferred !\n");
868 goto out;
869 }
870 if (fd_nb == 0) {
871 ret = 0;
872 goto out;
873 }
874 tmpbuf = malloc(fd_nb * (1 + NAME_MAX + 1 + IFNAMSIZ + sizeof(int)));
875 if (tmpbuf == NULL) {
876 Warning("Failed to allocate memory while receiving sockets\n");
877 goto out;
878 }
879 tmpfd = malloc(fd_nb * sizeof(int));
880 if (tmpfd == NULL) {
881 Warning("Failed to allocate memory while receiving sockets\n");
882 goto out;
883 }
884 msghdr.msg_control = cmsgbuf;
885 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
886 iov.iov_len = MAX_SEND_FD * (1 + NAME_MAX + 1 + IFNAMSIZ + sizeof(int));
887 do {
888 int ret3;
889
890 iov.iov_base = tmpbuf + curoff;
891 ret = recvmsg(sock, &msghdr, 0);
892 if (ret == -1 && errno == EINTR)
893 continue;
894 if (ret <= 0)
895 break;
896 /* Send an ack to let the sender know we got the sockets
897 * and it can send some more
898 */
899 do {
900 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
901 } while (ret3 == -1 && errno == EINTR);
902 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
903 cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
904 if (cmsg->cmsg_level == SOL_SOCKET &&
905 cmsg->cmsg_type == SCM_RIGHTS) {
906 size_t totlen = cmsg->cmsg_len -
907 CMSG_LEN(0);
908 if (totlen / sizeof(int) + got_fd > fd_nb) {
909 Warning("Got to many sockets !\n");
910 goto out;
911 }
912 /*
913 * Be paranoid and use memcpy() to avoid any
914 * potential alignement issue.
915 */
916 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
917 got_fd += totlen / sizeof(int);
918 }
919 }
920 curoff += ret;
921 } while (got_fd < fd_nb);
922
923 if (got_fd != fd_nb) {
924 Warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
925 fd_nb, got_fd);
926 goto out;
927 }
928 maxoff = curoff;
929 curoff = 0;
930 for (i = 0; i < got_fd; i++) {
931 int fd = tmpfd[i];
932 socklen_t socklen;
933 int len;
934
935 xfer_sock = calloc(1, sizeof(*xfer_sock));
936 if (!xfer_sock) {
937 Warning("Failed to allocate memory in get_old_sockets() !\n");
938 break;
939 }
940 xfer_sock->fd = -1;
941
942 socklen = sizeof(xfer_sock->addr);
943 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
944 Warning("Failed to get socket address\n");
945 free(xfer_sock);
946 continue;
947 }
948 if (curoff >= maxoff) {
949 Warning("Inconsistency while transferring sockets\n");
950 goto out;
951 }
952 len = tmpbuf[curoff++];
953 if (len > 0) {
954 /* We have a namespace */
955 if (curoff + len > maxoff) {
956 Warning("Inconsistency while transferring sockets\n");
957 goto out;
958 }
959 xfer_sock->namespace = malloc(len + 1);
960 if (!xfer_sock->namespace) {
961 Warning("Failed to allocate memory while transferring sockets\n");
962 goto out;
963 }
964 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
965 xfer_sock->namespace[len] = 0;
966 curoff += len;
967 }
968 if (curoff >= maxoff) {
969 Warning("Inconsistency while transferring sockets\n");
970 goto out;
971 }
972 len = tmpbuf[curoff++];
973 if (len > 0) {
974 /* We have an interface */
975 if (curoff + len > maxoff) {
976 Warning("Inconsistency while transferring sockets\n");
977 goto out;
978 }
979 xfer_sock->iface = malloc(len + 1);
980 if (!xfer_sock->iface) {
981 Warning("Failed to allocate memory while transferring sockets\n");
982 goto out;
983 }
984 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
985 xfer_sock->namespace[len] = 0;
986 curoff += len;
987 }
988 if (curoff + sizeof(int) > maxoff) {
989 Warning("Inconsistency while transferring sockets\n");
990 goto out;
991 }
992 memcpy(&xfer_sock->options, &tmpbuf[curoff],
993 sizeof(xfer_sock->options));
994 curoff += sizeof(xfer_sock->options);
995
996 xfer_sock->fd = fd;
997 if (xfer_sock_list)
998 xfer_sock_list->prev = xfer_sock;
999 xfer_sock->next = xfer_sock_list;
1000 xfer_sock->prev = NULL;
1001 xfer_sock_list = xfer_sock;
1002 xfer_sock = NULL;
1003 }
1004
1005 ret2 = 0;
1006out:
1007 /* If we failed midway make sure to close the remaining
1008 * file descriptors
1009 */
1010 if (tmpfd != NULL && i < got_fd) {
1011 for (; i < got_fd; i++) {
1012 close(tmpfd[i]);
1013 }
1014 }
1015 free(tmpbuf);
1016 free(tmpfd);
1017 free(cmsgbuf);
1018 if (sock != -1)
1019 close(sock);
1020 if (xfer_sock) {
1021 free(xfer_sock->namespace);
1022 free(xfer_sock->iface);
1023 if (xfer_sock->fd != -1)
1024 close(xfer_sock->fd);
1025 free(xfer_sock);
1026 }
1027 return (ret2);
1028}
1029
Willy Tarreaubaaee002006-06-26 02:48:02 +02001030/*
William Lallemand73b85e72017-06-01 17:38:51 +02001031 * copy and cleanup the current argv
1032 * Remove the -sf /-st parameters
1033 * Return an allocated copy of argv
1034 */
1035
1036static char **copy_argv(int argc, char **argv)
1037{
1038 char **newargv;
1039 int i, j;
1040
1041 newargv = calloc(argc + 2, sizeof(char *));
1042 if (newargv == NULL) {
1043 Warning("Cannot allocate memory\n");
1044 return NULL;
1045 }
1046
1047 for (i = 0, j = 0; i < argc; i++, j++) {
1048 char *flag = *(argv + i) + 1;
1049
1050 /* -sf or -st */
1051 if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1052 /* list of pids to finish ('f') or terminate ('t') */
1053 i++;
1054 while (i < argc && argv[i][0] != '-') {
1055 i++;
1056 }
1057 }
1058 newargv[j] = argv[i];
1059 }
1060 return newargv;
1061}
1062
1063/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001064 * This function initializes all the necessary variables. It only returns
1065 * if everything is OK. If something fails, it exits.
1066 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001067static void init(int argc, char **argv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001068{
Willy Tarreaubaaee002006-06-26 02:48:02 +02001069 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001070 char *tmp;
1071 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +02001072 int err_code = 0;
Maxime de Roucy0f503922016-05-13 23:52:55 +02001073 char *err_msg = NULL;
Willy Tarreau477ecd82010-01-03 21:12:30 +01001074 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +00001075 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +02001076 char *change_dir = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001077 struct proxy *px;
Willy Tarreaue6945732016-12-21 19:57:00 +01001078 struct post_check_fct *pcf;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001079
William Lallemand73b85e72017-06-01 17:38:51 +02001080 next_argv = copy_argv(argc, argv);
1081
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001082 chunk_init(&trash, malloc(global.tune.bufsize), global.tune.bufsize);
Willy Tarreau2819e992013-12-13 14:41:10 +01001083 alloc_trash_buffers(global.tune.bufsize);
David du Colombier7af46052012-05-16 14:16:48 +02001084
Emeric Brun2b920a12010-09-23 18:30:22 +02001085 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
1086 * the string in case of truncation, and at least FreeBSD appears not to do
1087 * it.
1088 */
1089 memset(hostname, 0, sizeof(hostname));
1090 gethostname(hostname, sizeof(hostname) - 1);
1091 memset(localpeer, 0, sizeof(localpeer));
1092 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
1093
Willy Tarreaubaaee002006-06-26 02:48:02 +02001094 /*
1095 * Initialize the previously static variables.
1096 */
1097
Willy Tarreau3eba98a2009-01-25 13:56:13 +01001098 totalconn = actconn = maxfd = listeners = stopping = 0;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001099 killed = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100
1101
1102#ifdef HAPROXY_MEMMAX
Willy Tarreau70060452015-12-14 12:46:07 +01001103 global.rlimit_memmax_all = HAPROXY_MEMMAX;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104#endif
1105
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001106 tzset();
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001107 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001108 start_date = now;
1109
Willy Tarreau84310e22014-02-14 11:59:04 +01001110 srandom(now_ms - getpid());
1111
Dragan Dosen835b9212016-02-12 13:23:03 +01001112 init_log();
Willy Tarreau29857942009-05-10 09:01:21 +02001113 signal_init();
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001114 if (init_acl() != 0)
1115 exit(1);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001116 init_task();
Willy Tarreau87b09662015-04-03 00:22:06 +02001117 init_stream();
Willy Tarreaub1ec8c42015-04-03 13:53:24 +02001118 init_session();
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001119 init_connection();
Willy Tarreau8280d642009-09-23 23:37:52 +02001120 /* warning, we init buffers later */
Willy Tarreaue4d7e552007-05-13 20:19:55 +02001121 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +01001122 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001123
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001124 /* Initialise lua. */
1125 hlua_init();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001126
Christopher Fauletff2613e2016-11-09 11:36:17 +01001127 /* Initialize process vars */
1128 vars_init(&global.vars, SCOPE_PROC);
1129
Willy Tarreau43b78992009-01-25 15:42:27 +01001130 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001131#if defined(ENABLE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001132 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001133#endif
1134#if defined(ENABLE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001135 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001136#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001137#if defined(ENABLE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +01001138 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001139#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001140#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001141 global.tune.options |= GTUNE_USE_SPLICE;
1142#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001143#if defined(USE_GETADDRINFO)
1144 global.tune.options |= GTUNE_USE_GAI;
1145#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001146#if defined(SO_REUSEPORT)
1147 global.tune.options |= GTUNE_USE_REUSEPORT;
1148#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001149
1150 pid = getpid();
1151 progname = *argv;
1152 while ((tmp = strchr(progname, '/')) != NULL)
1153 progname = tmp + 1;
1154
Kevinm48936af2010-12-22 16:08:21 +00001155 /* the process name is used for the logs only */
Dragan Dosen43885c72015-10-01 13:18:13 +02001156 chunk_initstr(&global.log_tag, strdup(progname));
Kevinm48936af2010-12-22 16:08:21 +00001157
Willy Tarreaubaaee002006-06-26 02:48:02 +02001158 argc--; argv++;
1159 while (argc > 0) {
1160 char *flag;
1161
1162 if (**argv == '-') {
1163 flag = *argv+1;
1164
1165 /* 1 arg */
1166 if (*flag == 'v') {
1167 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +01001168 if (flag[1] == 'v') /* -vv */
1169 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001170 exit(0);
1171 }
1172#if defined(ENABLE_EPOLL)
1173 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +01001174 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001175#endif
1176#if defined(ENABLE_POLL)
1177 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +01001178 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001179#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +02001180#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001181 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +01001182 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001183#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001184#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001185 else if (*flag == 'd' && flag[1] == 'S')
1186 global.tune.options &= ~GTUNE_USE_SPLICE;
1187#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001188#if defined(USE_GETADDRINFO)
1189 else if (*flag == 'd' && flag[1] == 'G')
1190 global.tune.options &= ~GTUNE_USE_GAI;
1191#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001192#if defined(SO_REUSEPORT)
1193 else if (*flag == 'd' && flag[1] == 'R')
1194 global.tune.options &= ~GTUNE_USE_REUSEPORT;
1195#endif
Emeric Brun850efd52014-01-29 12:24:34 +01001196 else if (*flag == 'd' && flag[1] == 'V')
1197 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001198 else if (*flag == 'V')
1199 arg_mode |= MODE_VERBOSE;
1200 else if (*flag == 'd' && flag[1] == 'b')
1201 arg_mode |= MODE_FOREGROUND;
Willy Tarreau6e064432012-05-08 15:40:42 +02001202 else if (*flag == 'd' && flag[1] == 'M')
1203 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreau3eed10e2016-11-07 21:03:16 +01001204 else if (*flag == 'd' && flag[1] == 'r')
1205 global.tune.options |= GTUNE_RESOLVE_DONTFAIL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001206 else if (*flag == 'd')
1207 arg_mode |= MODE_DEBUG;
1208 else if (*flag == 'c')
1209 arg_mode |= MODE_CHECK;
William Lallemand095ba4c2017-06-01 17:38:50 +02001210 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +02001211 arg_mode |= MODE_DAEMON;
William Lallemand095ba4c2017-06-01 17:38:50 +02001212 else if (*flag == 'W')
1213 arg_mode |= MODE_MWORKER;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001214 else if (*flag == 'q')
1215 arg_mode |= MODE_QUIET;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001216 else if (*flag == 'x') {
1217 if (argv[1][0] == '-') {
1218 Alert("Unix socket path expected with the -x flag\n");
1219 exit(1);
1220 }
1221 old_unixsocket = argv[1];
1222 argv++;
1223 argc--;
1224 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001225 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1226 /* list of pids to finish ('f') or terminate ('t') */
1227
1228 if (flag[1] == 'f')
1229 oldpids_sig = SIGUSR1; /* finish then exit */
1230 else
1231 oldpids_sig = SIGTERM; /* terminate immediately */
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001232 while (argc > 1 && argv[1][0] != '-') {
1233 oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int));
1234 if (!oldpids) {
1235 Alert("Cannot allocate old pid : out of memory.\n");
1236 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001237 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001238 argc--; argv++;
1239 oldpids[nb_oldpids] = atol(*argv);
1240 if (oldpids[nb_oldpids] <= 0)
1241 usage(progname);
1242 nb_oldpids++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001243 }
1244 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001245 else if (flag[0] == '-' && flag[1] == 0) { /* "--" */
1246 /* now that's a cfgfile list */
1247 argv++; argc--;
1248 while (argc > 0) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02001249 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
1250 Alert("Cannot load configuration file/directory %s : %s\n",
1251 *argv,
1252 err_msg);
Willy Tarreaua088d312015-10-08 11:58:48 +02001253 exit(1);
1254 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001255 argv++; argc--;
1256 }
1257 break;
1258 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001259 else { /* >=2 args */
1260 argv++; argc--;
1261 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001262 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001263
1264 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +02001265 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001266 case 'n' : cfg_maxconn = atol(*argv); break;
Willy Tarreau70060452015-12-14 12:46:07 +01001267 case 'm' : global.rlimit_memmax_all = atol(*argv); break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001268 case 'N' : cfg_maxpconn = atol(*argv); break;
Emeric Brun2b920a12010-09-23 18:30:22 +02001269 case 'L' : strncpy(localpeer, *argv, sizeof(localpeer) - 1); break;
Willy Tarreau5d01a632009-06-22 16:02:30 +02001270 case 'f' :
Maxime de Roucy0f503922016-05-13 23:52:55 +02001271 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
1272 Alert("Cannot load configuration file/directory %s : %s\n",
1273 *argv,
1274 err_msg);
Willy Tarreau5d01a632009-06-22 16:02:30 +02001275 exit(1);
1276 }
Willy Tarreau5d01a632009-06-22 16:02:30 +02001277 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001278 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001279 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001280 }
1281 }
1282 }
1283 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001284 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001285 argv++; argc--;
1286 }
1287
1288 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
William Lallemand095ba4c2017-06-01 17:38:50 +02001289 (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
Willy Tarreaubaaee002006-06-26 02:48:02 +02001290 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
1291
William Lallemandcb11fd22017-06-01 17:38:52 +02001292 /* Master workers wait mode */
1293 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_WAIT_ONLY") != NULL)) {
1294
1295 unsetenv("HAPROXY_MWORKER_WAIT_ONLY");
1296 mworker_wait();
1297 }
1298
1299 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {
1300 atexit_flag = 1;
1301 atexit(reexec_on_failure);
1302 }
1303
Willy Tarreau576132e2011-09-10 19:26:56 +02001304 if (change_dir && chdir(change_dir) < 0) {
1305 Alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
1306 exit(1);
1307 }
1308
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001309 /* handle cfgfiles that are actualy directories */
1310 cfgfiles_expand_directories();
1311
1312 if (LIST_ISEMPTY(&cfg_cfgfiles))
1313 usage(progname);
1314
Willy Tarreaubaaee002006-06-26 02:48:02 +02001315 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001316
1317 init_default_instance();
1318
Willy Tarreau477ecd82010-01-03 21:12:30 +01001319 list_for_each_entry(wl, &cfg_cfgfiles, list) {
Willy Tarreauc4382422009-12-06 13:10:44 +01001320 int ret;
1321
Willy Tarreau477ecd82010-01-03 21:12:30 +01001322 ret = readcfgfile(wl->s);
Willy Tarreauc4382422009-12-06 13:10:44 +01001323 if (ret == -1) {
1324 Alert("Could not open configuration file %s : %s\n",
Willy Tarreau477ecd82010-01-03 21:12:30 +01001325 wl->s, strerror(errno));
Willy Tarreauc4382422009-12-06 13:10:44 +01001326 exit(1);
1327 }
Willy Tarreau25a67fa2009-12-15 21:46:25 +01001328 if (ret & (ERR_ABORT|ERR_FATAL))
Willy Tarreau477ecd82010-01-03 21:12:30 +01001329 Alert("Error(s) found in configuration file : %s\n", wl->s);
Willy Tarreau25a67fa2009-12-15 21:46:25 +01001330 err_code |= ret;
Willy Tarreau058e9072009-07-20 09:30:05 +02001331 if (err_code & ERR_ABORT)
Willy Tarreau5d01a632009-06-22 16:02:30 +02001332 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001333 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001334
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02001335 /* do not try to resolve arguments nor to spot inconsistencies when
1336 * the configuration contains fatal errors caused by files not found
1337 * or failed memory allocations.
1338 */
1339 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1340 Alert("Fatal errors found in configuration.\n");
1341 exit(1);
1342 }
1343
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001344 pattern_finalize_config();
1345
Willy Tarreaubb925012009-07-23 13:36:36 +02001346 err_code |= check_config_validity();
1347 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1348 Alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001349 exit(1);
1350 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001351
Willy Tarreau70060452015-12-14 12:46:07 +01001352 /* recompute the amount of per-process memory depending on nbproc and
1353 * the shared SSL cache size (allowed to exist in all processes).
1354 */
1355 if (global.rlimit_memmax_all) {
1356#if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE)
1357 int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL;
1358
1359 global.rlimit_memmax =
1360 ((((int64_t)global.rlimit_memmax_all * 1048576LL) -
1361 ssl_cache_bytes) / global.nbproc +
1362 ssl_cache_bytes + 1048575LL) / 1048576LL;
1363#else
1364 global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc;
1365#endif
1366 }
1367
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001368#ifdef CONFIG_HAP_NS
1369 err_code |= netns_init();
1370 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1371 Alert("Failed to initialize namespace support.\n");
1372 exit(1);
1373 }
1374#endif
1375
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01001376 /* Apply server states */
1377 apply_server_state();
1378
1379 for (px = proxy; px; px = px->next)
1380 srv_compute_all_admin_states(px);
1381
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001382 /* Apply servers' configured address */
1383 err_code |= srv_init_addr();
1384 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1385 Alert("Failed to initialize server(s) addr.\n");
1386 exit(1);
1387 }
1388
Willy Tarreaubaaee002006-06-26 02:48:02 +02001389 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001390 struct peers *pr;
1391 struct proxy *px;
1392
1393 for (pr = peers; pr; pr = pr->next)
1394 if (pr->peers_fe)
1395 break;
1396
1397 for (px = proxy; px; px = px->next)
Willy Tarreau4348fad2012-09-20 16:48:07 +02001398 if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners))
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001399 break;
1400
1401 if (pr || px) {
1402 /* At least one peer or one listener has been found */
1403 qfprintf(stdout, "Configuration file is valid\n");
1404 exit(0);
1405 }
1406 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
1407 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001408 }
1409
Willy Tarreaue9b26022011-08-01 20:57:55 +02001410 global_listener_queue_task = task_new();
1411 if (!global_listener_queue_task) {
1412 Alert("Out of memory when initializing global task\n");
1413 exit(1);
1414 }
1415 /* very simple initialization, users will queue the task if needed */
1416 global_listener_queue_task->context = NULL; /* not even a context! */
1417 global_listener_queue_task->process = manage_global_listener_queue;
1418 global_listener_queue_task->expire = TICK_ETERNITY;
1419
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001420 /* now we know the buffer size, we can initialize the channels and buffers */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001421 init_buffer();
Willy Tarreau8280d642009-09-23 23:37:52 +02001422
Willy Tarreaue6945732016-12-21 19:57:00 +01001423 list_for_each_entry(pcf, &post_check_list, list) {
1424 err_code |= pcf->fct();
1425 if (err_code & (ERR_ABORT|ERR_FATAL))
1426 exit(1);
1427 }
1428
Willy Tarreaubaaee002006-06-26 02:48:02 +02001429 if (cfg_maxconn > 0)
1430 global.maxconn = cfg_maxconn;
1431
1432 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +02001433 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001434 global.pidfile = strdup(cfg_pidfile);
1435 }
1436
Willy Tarreaud0256482015-01-15 21:45:22 +01001437 /* Now we want to compute the maxconn and possibly maxsslconn values.
1438 * It's a bit tricky. If memmax is not set, maxconn defaults to
1439 * DEFAULT_MAXCONN and maxsslconn defaults to DEFAULT_MAXSSLCONN.
1440 *
1441 * If memmax is set, then it depends on which values are set. If
1442 * maxsslconn is set, we use memmax to determine how many cleartext
1443 * connections may be added, and set maxconn to the sum of the two.
1444 * If maxconn is set and not maxsslconn, maxsslconn is computed from
1445 * the remaining amount of memory between memmax and the cleartext
1446 * connections. If neither are set, then it is considered that all
1447 * connections are SSL-capable, and maxconn is computed based on this,
1448 * then maxsslconn accordingly. We need to know if SSL is used on the
1449 * frontends, backends, or both, because when it's used on both sides,
1450 * we need twice the value for maxsslconn, but we only count the
1451 * handshake once since it is not performed on the two sides at the
1452 * same time (frontend-side is terminated before backend-side begins).
1453 * The SSL stack is supposed to have filled ssl_session_cost and
Willy Tarreau474b96a2015-01-28 19:03:21 +01001454 * ssl_handshake_cost during its initialization. In any case, if
1455 * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for
1456 * maxconn in order to protect the system.
Willy Tarreaud0256482015-01-15 21:45:22 +01001457 */
1458 if (!global.rlimit_memmax) {
1459 if (global.maxconn == 0) {
1460 global.maxconn = DEFAULT_MAXCONN;
1461 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1462 fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn);
1463 }
1464 }
1465#ifdef USE_OPENSSL
1466 else if (!global.maxconn && !global.maxsslconn &&
1467 (global.ssl_used_frontend || global.ssl_used_backend)) {
1468 /* memmax is set, compute everything automatically. Here we want
1469 * to ensure that all SSL connections will be served. We take
1470 * care of the number of sides where SSL is used, and consider
1471 * the worst case : SSL used on both sides and doing a handshake
1472 * simultaneously. Note that we can't have more than maxconn
1473 * handshakes at a time by definition, so for the worst case of
1474 * two SSL conns per connection, we count a single handshake.
1475 */
1476 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1477 int64_t mem = global.rlimit_memmax * 1048576ULL;
1478
1479 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1480 mem -= global.maxzlibmem;
1481 mem = mem * MEM_USABLE_RATIO;
1482
1483 global.maxconn = mem /
Willy Tarreau87b09662015-04-03 00:22:06 +02001484 ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream
Willy Tarreaud0256482015-01-15 21:45:22 +01001485 sides * global.ssl_session_max_cost + // SSL buffers, one per side
1486 global.ssl_handshake_max_cost); // 1 handshake per connection max
1487
1488 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001489#ifdef SYSTEM_MAXCONN
1490 if (global.maxconn > DEFAULT_MAXCONN)
1491 global.maxconn = DEFAULT_MAXCONN;
1492#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001493 global.maxsslconn = sides * global.maxconn;
1494 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1495 fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n",
1496 global.maxconn, global.maxsslconn);
1497 }
1498 else if (!global.maxsslconn &&
1499 (global.ssl_used_frontend || global.ssl_used_backend)) {
1500 /* memmax and maxconn are known, compute maxsslconn automatically.
1501 * maxsslconn being forced, we don't know how many of it will be
1502 * on each side if both sides are being used. The worst case is
1503 * when all connections use only one SSL instance because
1504 * handshakes may be on two sides at the same time.
1505 */
1506 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1507 int64_t mem = global.rlimit_memmax * 1048576ULL;
1508 int64_t sslmem;
1509
1510 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1511 mem -= global.maxzlibmem;
1512 mem = mem * MEM_USABLE_RATIO;
1513
Willy Tarreau87b09662015-04-03 00:22:06 +02001514 sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001515 global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost);
1516 global.maxsslconn = round_2dig(global.maxsslconn);
1517
1518 if (sslmem <= 0 || global.maxsslconn < sides) {
1519 Alert("Cannot compute the automatic maxsslconn because global.maxconn is already too "
1520 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1521 "without SSL is %d, but %d was found and SSL is in use.\n",
1522 global.rlimit_memmax,
Willy Tarreau87b09662015-04-03 00:22:06 +02001523 (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)),
Willy Tarreaud0256482015-01-15 21:45:22 +01001524 global.maxconn);
1525 exit(1);
1526 }
1527
1528 if (global.maxsslconn > sides * global.maxconn)
1529 global.maxsslconn = sides * global.maxconn;
1530
1531 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1532 fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn);
1533 }
1534#endif
1535 else if (!global.maxconn) {
1536 /* memmax and maxsslconn are known/unused, compute maxconn automatically */
1537 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1538 int64_t mem = global.rlimit_memmax * 1048576ULL;
1539 int64_t clearmem;
1540
1541 if (global.ssl_used_frontend || global.ssl_used_backend)
1542 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1543
1544 mem -= global.maxzlibmem;
1545 mem = mem * MEM_USABLE_RATIO;
1546
1547 clearmem = mem;
1548 if (sides)
1549 clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn;
1550
Willy Tarreau87b09662015-04-03 00:22:06 +02001551 global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001552 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001553#ifdef SYSTEM_MAXCONN
1554 if (global.maxconn > DEFAULT_MAXCONN)
1555 global.maxconn = DEFAULT_MAXCONN;
1556#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001557
1558 if (clearmem <= 0 || !global.maxconn) {
1559 Alert("Cannot compute the automatic maxconn because global.maxsslconn is already too "
1560 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1561 "is %d, but %d was found.\n",
1562 global.rlimit_memmax,
1563 (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)),
1564 global.maxsslconn);
1565 exit(1);
1566 }
1567
1568 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
1569 if (sides && global.maxsslconn > sides * global.maxconn) {
1570 fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn "
1571 "to be limited to %d. Better reduce global.maxsslconn to get more "
1572 "room for extra connections.\n", global.maxsslconn, global.maxconn);
1573 }
1574 fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn);
1575 }
1576 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001577
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001578 if (!global.maxpipes) {
1579 /* maxpipes not specified. Count how many frontends and backends
1580 * may be using splicing, and bound that to maxconn.
1581 */
1582 struct proxy *cur;
1583 int nbfe = 0, nbbe = 0;
1584
1585 for (cur = proxy; cur; cur = cur->next) {
1586 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
1587 if (cur->cap & PR_CAP_FE)
1588 nbfe += cur->maxconn;
1589 if (cur->cap & PR_CAP_BE)
Willy Tarreauafb48762009-01-25 10:42:05 +01001590 nbbe += cur->fullconn ? cur->fullconn : global.maxconn;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001591 }
1592 }
1593 global.maxpipes = MAX(nbfe, nbbe);
1594 if (global.maxpipes > global.maxconn)
1595 global.maxpipes = global.maxconn;
Willy Tarreau686ac822009-01-25 14:06:58 +01001596 global.maxpipes /= 4;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001597 }
1598
1599
Willy Tarreauabacc2c2011-09-07 14:26:33 +02001600 global.hardmaxconn = global.maxconn; /* keep this max value */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001601 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
Willy Tarreau3ec79b92009-01-18 20:39:42 +01001602 global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001603
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001604 if (global.stats_fe)
1605 global.maxsock += global.stats_fe->maxconn;
1606
1607 if (peers) {
1608 /* peers also need to bypass global maxconn */
1609 struct peers *p = peers;
1610
1611 for (p = peers; p; p = p->next)
1612 if (p->peers_fe)
1613 global.maxsock += p->peers_fe->maxconn;
1614 }
1615
Willy Tarreau1db37712007-06-03 17:16:49 +02001616 if (global.tune.maxpollevents <= 0)
1617 global.tune.maxpollevents = MAX_POLL_EVENTS;
1618
Willy Tarreau6f4a82c2009-03-21 20:43:57 +01001619 if (global.tune.recv_enough == 0)
1620 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
1621
Willy Tarreau27097842015-09-28 13:53:23 +02001622 if (global.tune.maxrewrite < 0)
1623 global.tune.maxrewrite = MAXREWRITE;
1624
Willy Tarreau27a674e2009-08-17 07:23:33 +02001625 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
1626 global.tune.maxrewrite = global.tune.bufsize / 2;
1627
Willy Tarreaubaaee002006-06-26 02:48:02 +02001628 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
1629 /* command line debug mode inhibits configuration mode */
William Lallemand095ba4c2017-06-01 17:38:50 +02001630 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001631 global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND));
1632 }
1633
William Lallemand095ba4c2017-06-01 17:38:50 +02001634 if (arg_mode & MODE_DAEMON) {
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001635 /* command line daemon mode inhibits foreground and debug modes mode */
1636 global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND);
William Lallemand095ba4c2017-06-01 17:38:50 +02001637 global.mode |= arg_mode & MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001638 }
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001639
1640 global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001641
William Lallemand095ba4c2017-06-01 17:38:50 +02001642 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
1643 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
1644 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001645 }
1646
William Lallemand095ba4c2017-06-01 17:38:50 +02001647 if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001648 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
1649 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
1650 global.nbproc = 1;
1651 }
1652
1653 if (global.nbproc < 1)
1654 global.nbproc = 1;
1655
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001656 swap_buffer = calloc(1, global.tune.bufsize);
1657 get_http_auth_buff = calloc(1, global.tune.bufsize);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02001658 static_table_key = calloc(1, sizeof(*static_table_key));
Willy Tarreau8096de92010-02-26 11:12:27 +01001659
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001660 fdinfo = calloc(1, sizeof(struct fdinfo) * (global.maxsock));
1661 fdtab = calloc(1, sizeof(struct fdtab) * (global.maxsock));
Willy Tarreauef1d1f82007-04-16 00:25:25 +02001662 /*
1663 * Note: we could register external pollers here.
1664 * Built-in pollers have been registered before main().
1665 */
Willy Tarreau4f60f162007-04-08 16:39:58 +02001666
Willy Tarreau43b78992009-01-25 15:42:27 +01001667 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001668 disable_poller("kqueue");
1669
Willy Tarreau43b78992009-01-25 15:42:27 +01001670 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001671 disable_poller("epoll");
1672
Willy Tarreau43b78992009-01-25 15:42:27 +01001673 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001674 disable_poller("poll");
1675
Willy Tarreau43b78992009-01-25 15:42:27 +01001676 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001677 disable_poller("select");
1678
1679 /* Note: we could disable any poller by name here */
1680
Christopher Fauletb3f4e142016-03-07 12:46:38 +01001681 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +02001682 list_pollers(stderr);
Christopher Fauletb3f4e142016-03-07 12:46:38 +01001683 fprintf(stderr, "\n");
1684 list_filters(stderr);
1685 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02001686
Willy Tarreau4f60f162007-04-08 16:39:58 +02001687 if (!init_pollers()) {
Willy Tarreau3fa87b12013-03-31 14:41:15 +02001688 Alert("No polling mechanism available.\n"
1689 " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n"
1690 " is too low on this platform to support maxconn and the number of listeners\n"
1691 " and servers. You should rebuild haproxy specifying your system using TARGET=\n"
1692 " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n"
Prach Pongpanichb837e682013-05-14 20:56:28 +02001693 " global maxconn setting to accommodate the system's limitation. For reference,\n"
Willy Tarreau3fa87b12013-03-31 14:41:15 +02001694 " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n"
1695 " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n"
1696 " check build settings using 'haproxy -vv'.\n\n",
1697 FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001698 exit(1);
1699 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02001700 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
1701 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001702 }
1703
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001704 if (!global.node)
1705 global.node = strdup(hostname);
1706
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01001707 if (!hlua_post_init())
1708 exit(1);
Thomas Holmes6abded42015-05-12 16:23:58 +01001709
Baptiste Assmann325137d2015-04-13 23:40:55 +02001710 /* initialize structures for name resolution */
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001711 if (!dns_init_resolvers(0))
Baptiste Assmann325137d2015-04-13 23:40:55 +02001712 exit(1);
Maxime de Roucy0f503922016-05-13 23:52:55 +02001713
1714 free(err_msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001715}
1716
Simon Horman6fb82592011-07-15 13:14:11 +09001717static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +09001718{
Simon Hormanac821422011-07-15 13:14:09 +09001719 struct acl_term_suite *suite, *suiteb;
1720 struct acl_term *term, *termb;
1721
Simon Horman6fb82592011-07-15 13:14:11 +09001722 if (!cond)
1723 return;
1724
1725 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
1726 list_for_each_entry_safe(term, termb, &suite->terms, list) {
1727 LIST_DEL(&term->list);
1728 free(term);
Simon Hormanac821422011-07-15 13:14:09 +09001729 }
Simon Horman6fb82592011-07-15 13:14:11 +09001730 LIST_DEL(&suite->list);
1731 free(suite);
1732 }
1733
1734 free(cond);
1735}
1736
1737static void deinit_tcp_rules(struct list *rules)
1738{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001739 struct act_rule *trule, *truleb;
Simon Horman6fb82592011-07-15 13:14:11 +09001740
1741 list_for_each_entry_safe(trule, truleb, rules, list) {
Simon Hormanac821422011-07-15 13:14:09 +09001742 LIST_DEL(&trule->list);
Simon Horman6fb82592011-07-15 13:14:11 +09001743 deinit_acl_cond(trule->cond);
Simon Hormanac821422011-07-15 13:14:09 +09001744 free(trule);
1745 }
1746}
1747
Simon Horman6fb82592011-07-15 13:14:11 +09001748static void deinit_stick_rules(struct list *rules)
1749{
1750 struct sticking_rule *rule, *ruleb;
1751
1752 list_for_each_entry_safe(rule, ruleb, rules, list) {
1753 LIST_DEL(&rule->list);
1754 deinit_acl_cond(rule->cond);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001755 release_sample_expr(rule->expr);
Simon Horman6fb82592011-07-15 13:14:11 +09001756 free(rule);
1757 }
1758}
1759
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001760void deinit(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001761{
Willy Tarreau4d2d0982007-05-14 00:39:29 +02001762 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001763 struct cap_hdr *h,*h_next;
1764 struct server *s,*s_next;
1765 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001766 struct acl_cond *cond, *condb;
1767 struct hdr_exp *exp, *expb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001768 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001769 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001770 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001771 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001772 struct wordlist *wl, *wlb;
Willy Tarreauf4f04122010-01-28 18:10:50 +01001773 struct cond_wordlist *cwl, *cwlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001774 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +02001775 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +01001776 struct logformat_node *lf, *lfb;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001777 struct bind_conf *bind_conf, *bind_back;
Willy Tarreaucdb737e2016-12-21 18:43:10 +01001778 struct build_opts_str *bol, *bolb;
Willy Tarreau05554e62016-12-21 20:46:26 +01001779 struct post_deinit_fct *pdf;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001780 int i;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001781
Willy Tarreau24f4efa2010-08-27 17:56:48 +02001782 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001783 while (p) {
Willy Tarreau8113a5d2012-10-04 08:01:43 +02001784 free(p->conf.file);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001785 free(p->id);
1786 free(p->check_req);
1787 free(p->cookie_name);
1788 free(p->cookie_domain);
1789 free(p->url_param_name);
1790 free(p->capture_name);
1791 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +09001792 free(p->rdp_cookie_name);
Willy Tarreau62a61232013-04-12 18:13:46 +02001793 if (p->conf.logformat_string != default_http_log_format &&
1794 p->conf.logformat_string != default_tcp_log_format &&
1795 p->conf.logformat_string != clf_http_log_format)
1796 free(p->conf.logformat_string);
Willy Tarreau196729e2012-05-31 19:30:26 +02001797
Willy Tarreau62a61232013-04-12 18:13:46 +02001798 free(p->conf.lfs_file);
1799 free(p->conf.uniqueid_format_string);
1800 free(p->conf.uif_file);
Godbachaf1a75d2013-10-02 17:10:11 +08001801 free(p->lbprm.map.srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001802
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001803 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
1804 free(p->conf.logformat_sd_string);
1805 free(p->conf.lfsd_file);
1806
Willy Tarreaua534fea2008-08-03 12:19:50 +02001807 for (i = 0; i < HTTP_ERR_SIZE; i++)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001808 chunk_destroy(&p->errmsg[i]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001809
Willy Tarreauf4f04122010-01-28 18:10:50 +01001810 list_for_each_entry_safe(cwl, cwlb, &p->req_add, list) {
1811 LIST_DEL(&cwl->list);
1812 free(cwl->s);
1813 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001814 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001815
Willy Tarreauf4f04122010-01-28 18:10:50 +01001816 list_for_each_entry_safe(cwl, cwlb, &p->rsp_add, list) {
1817 LIST_DEL(&cwl->list);
1818 free(cwl->s);
1819 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001820 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001821
Willy Tarreaub80c2302007-11-30 20:51:32 +01001822 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
1823 LIST_DEL(&cond->list);
1824 prune_acl_cond(cond);
1825 free(cond);
1826 }
1827
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001828 for (exp = p->req_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001829 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02001830 regex_free(exp->preg);
1831 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001832 }
1833
Willy Tarreau98d04852015-05-26 12:18:29 +02001834 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001835 expb = exp;
1836 exp = exp->next;
1837 free(expb);
1838 }
1839
1840 for (exp = p->rsp_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001841 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02001842 regex_free(exp->preg);
1843 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001844 }
1845
Willy Tarreau98d04852015-05-26 12:18:29 +02001846 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001847 expb = exp;
1848 exp = exp->next;
1849 free(expb);
1850 }
1851
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001852 /* build a list of unique uri_auths */
1853 if (!ua)
1854 ua = p->uri_auth;
1855 else {
1856 /* check if p->uri_auth is unique */
1857 for (uap = ua; uap; uap=uap->next)
1858 if (uap == p->uri_auth)
1859 break;
1860
Willy Tarreauaccc4e12008-06-24 11:14:45 +02001861 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001862 /* add it, if it is */
1863 p->uri_auth->next = ua;
1864 ua = p->uri_auth;
1865 }
1866 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001867
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001868 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
1869 LIST_DEL(&acl->list);
1870 prune_acl(acl);
1871 free(acl);
1872 }
1873
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001874 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
1875 LIST_DEL(&srule->list);
1876 prune_acl_cond(srule->cond);
1877 free(srule->cond);
1878 free(srule);
1879 }
1880
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001881 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
1882 LIST_DEL(&rule->list);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001883 if (rule->cond) {
1884 prune_acl_cond(rule->cond);
1885 free(rule->cond);
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +01001886 free(rule->file);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001887 }
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001888 free(rule);
1889 }
1890
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001891 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
1892 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +01001893 if (rdr->cond) {
1894 prune_acl_cond(rdr->cond);
1895 free(rdr->cond);
1896 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001897 free(rdr->rdr_str);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01001898 list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) {
1899 LIST_DEL(&lf->list);
1900 free(lf);
1901 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001902 free(rdr);
1903 }
1904
William Lallemand0f99e342011-10-12 17:50:54 +02001905 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
1906 LIST_DEL(&log->list);
1907 free(log);
1908 }
1909
William Lallemand723b73a2012-02-08 16:37:49 +01001910 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
1911 LIST_DEL(&lf->list);
1912 free(lf);
1913 }
1914
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001915 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
1916 LIST_DEL(&lf->list);
1917 free(lf);
1918 }
1919
Simon Hormanac821422011-07-15 13:14:09 +09001920 deinit_tcp_rules(&p->tcp_req.inspect_rules);
1921 deinit_tcp_rules(&p->tcp_req.l4_rules);
1922
Simon Horman6fb82592011-07-15 13:14:11 +09001923 deinit_stick_rules(&p->storersp_rules);
1924 deinit_stick_rules(&p->sticking_rules);
1925
Willy Tarreaubaaee002006-06-26 02:48:02 +02001926 h = p->req_cap;
1927 while (h) {
1928 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001929 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001930 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001931 free(h);
1932 h = h_next;
1933 }/* end while(h) */
1934
1935 h = p->rsp_cap;
1936 while (h) {
1937 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001938 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001939 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001940 free(h);
1941 h = h_next;
1942 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001943
Willy Tarreaubaaee002006-06-26 02:48:02 +02001944 s = p->srv;
1945 while (s) {
1946 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001947
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001948 if (s->check.task) {
1949 task_delete(s->check.task);
1950 task_free(s->check.task);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001951 }
Simon Hormand60d6912013-11-25 10:46:36 +09001952 if (s->agent.task) {
1953 task_delete(s->agent.task);
1954 task_free(s->agent.task);
1955 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001956
Willy Tarreau2e993902011-10-31 11:53:20 +01001957 if (s->warmup) {
1958 task_delete(s->warmup);
1959 task_free(s->warmup);
1960 }
1961
Willy Tarreaua534fea2008-08-03 12:19:50 +02001962 free(s->id);
1963 free(s->cookie);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001964 free(s->check.bi);
1965 free(s->check.bo);
Simon Hormand60d6912013-11-25 10:46:36 +09001966 free(s->agent.bi);
1967 free(s->agent.bo);
James Brown55f9ff12015-10-21 18:19:05 -07001968 free(s->agent.send_string);
Sárközi, László34c01792014-09-05 10:08:23 +02001969 free((char*)s->conf.file);
Willy Tarreau17d45382016-12-22 21:16:08 +01001970
1971 if (s->use_ssl || s->check.use_ssl) {
1972 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
1973 xprt_get(XPRT_SSL)->destroy_srv(s);
1974 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001975 free(s);
1976 s = s_next;
1977 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001978
Willy Tarreau4348fad2012-09-20 16:48:07 +02001979 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02001980 /*
1981 * Zombie proxy, the listener just pretend to be up
1982 * because they still hold an opened fd.
1983 * Close it and give the listener its real state.
1984 */
1985 if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
1986 close(l->fd);
1987 l->state = LI_INIT;
1988 }
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02001989 unbind_listener(l);
1990 delete_listener(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02001991 LIST_DEL(&l->by_fe);
1992 LIST_DEL(&l->by_bind);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01001993 free(l->name);
1994 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001995 free(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02001996 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001997
Willy Tarreau4348fad2012-09-20 16:48:07 +02001998 /* Release unused SSL configs. */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001999 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01002000 if (bind_conf->xprt->destroy_bind_conf)
2001 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002002 free(bind_conf->file);
2003 free(bind_conf->arg);
2004 LIST_DEL(&bind_conf->by_fe);
2005 free(bind_conf);
2006 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02002007
Christopher Fauletd7c91962015-04-30 11:48:27 +02002008 flt_deinit(p);
2009
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002010 free(p->desc);
2011 free(p->fwdfor_hdr_name);
2012
Willy Tarreauff011f22011-01-06 17:51:27 +01002013 free_http_req_rules(&p->http_req_rules);
Sasha Pachev218f0642014-06-16 12:05:59 -06002014 free_http_res_rules(&p->http_res_rules);
Willy Tarreau918ff602011-07-25 16:33:49 +02002015 free(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002016
Willy Tarreaucf7f3202007-05-13 22:46:04 +02002017 pool_destroy2(p->req_cap_pool);
2018 pool_destroy2(p->rsp_cap_pool);
Simon Hormanb08584a2011-07-15 13:14:10 +09002019 pool_destroy2(p->table.pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002020
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002021 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002022 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002023 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002024 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02002025
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002026 while (ua) {
2027 uap = ua;
2028 ua = ua->next;
2029
Willy Tarreaua534fea2008-08-03 12:19:50 +02002030 free(uap->uri_prefix);
2031 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002032 free(uap->node);
2033 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002034
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002035 userlist_free(uap->userlist);
Willy Tarreauff011f22011-01-06 17:51:27 +01002036 free_http_req_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002037
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002038 free(uap);
2039 }
2040
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002041 userlist_free(userlist);
2042
David Carlier834cb2e2015-09-25 12:02:25 +01002043 cfg_unregister_sections();
2044
2045 free_trash_buffers();
2046 chunk_destroy(&trash);
2047
Willy Tarreaudd815982007-10-16 12:25:14 +02002048 protocol_unbind_all();
2049
Willy Tarreau05554e62016-12-21 20:46:26 +01002050 list_for_each_entry(pdf, &post_deinit_list, list)
2051 pdf->fct();
2052
Joe Williamsdf5b38f2010-12-29 17:05:48 +01002053 free(global.log_send_hostname); global.log_send_hostname = NULL;
Dragan Dosen43885c72015-10-01 13:18:13 +02002054 chunk_destroy(&global.log_tag);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002055 free(global.chroot); global.chroot = NULL;
2056 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002057 free(global.node); global.node = NULL;
2058 free(global.desc); global.desc = NULL;
Godbach4cc1b0d2013-06-26 16:49:51 +08002059 free(fdinfo); fdinfo = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002060 free(fdtab); fdtab = NULL;
2061 free(oldpids); oldpids = NULL;
David Carlier834cb2e2015-09-25 12:02:25 +01002062 free(static_table_key); static_table_key = NULL;
2063 free(get_http_auth_buff); get_http_auth_buff = NULL;
2064 free(swap_buffer); swap_buffer = NULL;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002065 free(global_listener_queue_task); global_listener_queue_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002066
William Lallemand0f99e342011-10-12 17:50:54 +02002067 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
2068 LIST_DEL(&log->list);
2069 free(log);
2070 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01002071 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02002072 free(wl->s);
Willy Tarreau477ecd82010-01-03 21:12:30 +01002073 LIST_DEL(&wl->list);
2074 free(wl);
2075 }
2076
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002077 list_for_each_entry_safe(bol, bolb, &build_opts_list, list) {
2078 if (bol->must_free)
2079 free((void *)bol->str);
2080 LIST_DEL(&bol->list);
2081 free(bol);
2082 }
2083
Christopher Fauletff2613e2016-11-09 11:36:17 +01002084 vars_prune(&global.vars, NULL, NULL);
2085
Willy Tarreau87b09662015-04-03 00:22:06 +02002086 pool_destroy2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +02002087 pool_destroy2(pool2_session);
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002088 pool_destroy2(pool2_connection);
Willy Tarreaub686afd2017-02-08 11:06:11 +01002089 pool_destroy2(pool2_trash);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002090 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002091 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02002092 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +02002093 pool_destroy2(pool2_capture);
Willy Tarreaue4d7e552007-05-13 20:19:55 +02002094 pool_destroy2(pool2_pendconn);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002095 pool_destroy2(pool2_sig_handlers);
Willy Tarreau34eb6712011-10-24 18:15:04 +02002096 pool_destroy2(pool2_hdr_idx);
Willy Tarreau63986c72015-04-03 22:55:33 +02002097 pool_destroy2(pool2_http_txn);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002098 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002099} /* end deinit() */
2100
Willy Tarreaubaaee002006-06-26 02:48:02 +02002101
Willy Tarreau918ff602011-07-25 16:33:49 +02002102/* Runs the polling loop */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01002103static void run_poll_loop()
Willy Tarreau4f60f162007-04-08 16:39:58 +02002104{
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002105 int next;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002106
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002107 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002108 while (1) {
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002109 /* Process a few tasks */
2110 process_runnable_tasks();
2111
Willy Tarreau29857942009-05-10 09:01:21 +02002112 /* check if we caught some signals and process them */
2113 signal_process_queue();
2114
Willy Tarreau58b458d2008-06-29 22:40:23 +02002115 /* Check if we can expire some tasks */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002116 next = wake_expired_tasks();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002117
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002118 /* stop when there's nothing left to do */
2119 if (jobs == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +02002120 break;
2121
Willy Tarreau10146c92015-04-13 20:44:19 +02002122 /* expire immediately if events are pending */
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002123 if (fd_cache_num || tasks_run_queue || signal_queue_len || applets_active_queue)
Willy Tarreau10146c92015-04-13 20:44:19 +02002124 next = now_ms;
2125
Willy Tarreau58b458d2008-06-29 22:40:23 +02002126 /* The poller will ensure it returns around <next> */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002127 cur_poller.poll(&cur_poller, next);
Willy Tarreau033cd9d2014-01-25 19:24:15 +01002128 fd_process_cached_events();
Willy Tarreau3c595ac2015-04-19 09:59:31 +02002129 applet_run_active();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002130 }
2131}
2132
Willy Tarreaue9b26022011-08-01 20:57:55 +02002133/* This is the global management task for listeners. It enables listeners waiting
2134 * for global resources when there are enough free resource, or at least once in
2135 * a while. It is designed to be called as a task.
2136 */
2137static struct task *manage_global_listener_queue(struct task *t)
2138{
2139 int next = TICK_ETERNITY;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002140 /* queue is empty, nothing to do */
2141 if (LIST_ISEMPTY(&global_listener_queue))
2142 goto out;
2143
2144 /* If there are still too many concurrent connections, let's wait for
2145 * some of them to go away. We don't need to re-arm the timer because
2146 * each of them will scan the queue anyway.
2147 */
2148 if (unlikely(actconn >= global.maxconn))
2149 goto out;
2150
2151 /* We should periodically try to enable listeners waiting for a global
2152 * resource here, because it is possible, though very unlikely, that
2153 * they have been blocked by a temporary lack of global resource such
2154 * as a file descriptor or memory and that the temporary condition has
2155 * disappeared.
2156 */
Willy Tarreauabacc2c2011-09-07 14:26:33 +02002157 dequeue_all_listeners(&global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +02002158
2159 out:
2160 t->expire = next;
2161 task_queue(t);
2162 return t;
2163}
Willy Tarreau4f60f162007-04-08 16:39:58 +02002164
Willy Tarreaubaaee002006-06-26 02:48:02 +02002165int main(int argc, char **argv)
2166{
2167 int err, retry;
2168 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02002169 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02002170 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002171
Emeric Bruncf20bf12010-10-22 16:06:11 +02002172 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002173 signal_register_fct(SIGQUIT, dump, SIGQUIT);
2174 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
2175 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
William Lallemand73b85e72017-06-01 17:38:51 +02002176 signal_register_fct(SIGUSR2, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002177
Willy Tarreaue437c442010-03-17 18:02:46 +01002178 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
2179 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
2180 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002181 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002182 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002183
Willy Tarreaudc23a922011-02-16 11:10:36 +01002184 /* ulimits */
2185 if (!global.rlimit_nofile)
2186 global.rlimit_nofile = global.maxsock;
2187
2188 if (global.rlimit_nofile) {
2189 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
2190 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
Willy Tarreauef635472016-06-21 11:48:18 +02002191 /* try to set it to the max possible at least */
2192 getrlimit(RLIMIT_NOFILE, &limit);
Willy Tarreau164dd0b2016-06-21 11:51:59 +02002193 limit.rlim_cur = limit.rlim_max;
2194 if (setrlimit(RLIMIT_NOFILE, &limit) != -1)
2195 getrlimit(RLIMIT_NOFILE, &limit);
2196
Willy Tarreauef635472016-06-21 11:48:18 +02002197 Warning("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n", argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
2198 global.rlimit_nofile = limit.rlim_cur;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002199 }
2200 }
2201
2202 if (global.rlimit_memmax) {
2203 limit.rlim_cur = limit.rlim_max =
Willy Tarreau70060452015-12-14 12:46:07 +01002204 global.rlimit_memmax * 1048576ULL;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002205#ifdef RLIMIT_AS
2206 if (setrlimit(RLIMIT_AS, &limit) == -1) {
2207 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2208 argv[0], global.rlimit_memmax);
2209 }
2210#else
2211 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
2212 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2213 argv[0], global.rlimit_memmax);
2214 }
2215#endif
2216 }
2217
Olivier Houchardf73629d2017-04-05 22:33:04 +02002218 if (old_unixsocket) {
2219 if (get_old_sockets(old_unixsocket) != 0) {
2220 Alert("Failed to get the sockets from the old process!\n");
2221 exit(1);
2222 }
2223 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002224 /* We will loop at most 100 times with 10 ms delay each time.
2225 * That's at most 1 second. We only send a signal to old pids
2226 * if we cannot grab at least one port.
2227 */
2228 retry = MAX_START_RETRIES;
2229 err = ERR_NONE;
2230 while (retry >= 0) {
2231 struct timeval w;
2232 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01002233 /* exit the loop on no error or fatal error */
2234 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002235 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02002236 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002237 break;
2238
2239 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
2240 * listening sockets. So on those platforms, it would be wiser to
2241 * simply send SIGUSR1, which will not be undoable.
2242 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02002243 if (tell_old_pids(SIGTTOU) == 0) {
2244 /* no need to wait if we can't contact old pids */
2245 retry = 0;
2246 continue;
2247 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002248 /* give some time to old processes to stop listening */
2249 w.tv_sec = 0;
2250 w.tv_usec = 10*1000;
2251 select(0, NULL, NULL, NULL, &w);
2252 retry--;
2253 }
2254
2255 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01002256 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02002257 if (retry != MAX_START_RETRIES && nb_oldpids) {
2258 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002259 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02002260 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002261 exit(1);
2262 }
2263
2264 if (listeners == 0) {
Willy Tarreauf2ee0162015-08-09 11:01:51 +02002265 Alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002266 /* Note: we don't have to send anything to the old pids because we
2267 * never stopped them. */
2268 exit(1);
2269 }
2270
Emeric Bruncf20bf12010-10-22 16:06:11 +02002271 err = protocol_bind_all(errmsg, sizeof(errmsg));
2272 if ((err & ~ERR_WARN) != ERR_NONE) {
2273 if ((err & ERR_ALERT) || (err & ERR_WARN))
2274 Alert("[%s.main()] %s.\n", argv[0], errmsg);
2275
Willy Tarreaudd815982007-10-16 12:25:14 +02002276 Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
2277 protocol_unbind_all(); /* cleanup everything we can */
2278 if (nb_oldpids)
2279 tell_old_pids(SIGTTIN);
2280 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002281 } else if (err & ERR_WARN) {
2282 Alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02002283 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002284 /* Ok, all listener should now be bound, close any leftover sockets
2285 * the previous process gave us, we don't need them anymore
2286 */
2287 while (xfer_sock_list != NULL) {
2288 struct xfer_sock_list *tmpxfer = xfer_sock_list->next;
2289 close(xfer_sock_list->fd);
2290 free(xfer_sock_list->iface);
2291 free(xfer_sock_list->namespace);
2292 free(xfer_sock_list);
2293 xfer_sock_list = tmpxfer;
2294 }
Willy Tarreaudd815982007-10-16 12:25:14 +02002295
Willy Tarreaubaaee002006-06-26 02:48:02 +02002296 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002297 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
2298 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002299
Willy Tarreaubaaee002006-06-26 02:48:02 +02002300 /* MODE_QUIET can inhibit alerts and warnings below this line */
2301
2302 global.mode &= ~MODE_STARTING;
2303 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
2304 /* detach from the tty */
2305 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002306 }
2307
2308 /* open log & pid files before the chroot */
William Lallemand095ba4c2017-06-01 17:38:50 +02002309 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002310 unlink(global.pidfile);
2311 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
2312 if (pidfd < 0) {
2313 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
2314 if (nb_oldpids)
2315 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02002316 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002317 exit(1);
2318 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002319 }
2320
Willy Tarreaub38651a2007-03-24 17:24:39 +01002321 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
2322 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002323 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002324 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01002325 exit(1);
2326 }
2327
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002328 /* If the user is not root, we'll still let him try the configuration
2329 * but we inform him that unexpected behaviour may occur.
2330 */
2331 if ((global.last_checks & LSTCHK_NETADM) && getuid())
2332 Warning("[%s.main()] Some options which require full privileges"
2333 " might not work well.\n"
2334 "", argv[0]);
2335
William Lallemand095ba4c2017-06-01 17:38:50 +02002336 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2337
2338 /* chroot if needed */
2339 if (global.chroot != NULL) {
2340 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
2341 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
2342 if (nb_oldpids)
2343 tell_old_pids(SIGTTIN);
2344 protocol_unbind_all();
2345 exit(1);
2346 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002347 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002348 }
2349
Willy Tarreaubaaee002006-06-26 02:48:02 +02002350 if (nb_oldpids)
Willy Tarreaubb545b42010-08-25 12:58:59 +02002351 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002352
2353 /* Note that any error at this stage will be fatal because we will not
2354 * be able to restart the old pids.
2355 */
2356
William Lallemand095ba4c2017-06-01 17:38:50 +02002357 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2358 /* setgid / setuid */
2359 if (global.gid) {
2360 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
2361 Warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2362 " without 'uid'/'user' is generally useless.\n", argv[0]);
2363
2364 if (setgid(global.gid) == -1) {
2365 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
2366 protocol_unbind_all();
2367 exit(1);
2368 }
2369 }
Michael Schererab012dd2013-01-12 18:35:19 +01002370
William Lallemand095ba4c2017-06-01 17:38:50 +02002371 if (global.uid && setuid(global.uid) == -1) {
2372 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Michael Schererab012dd2013-01-12 18:35:19 +01002373 protocol_unbind_all();
2374 exit(1);
2375 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002376 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002377 /* check ulimits */
2378 limit.rlim_cur = limit.rlim_max = 0;
2379 getrlimit(RLIMIT_NOFILE, &limit);
2380 if (limit.rlim_cur < global.maxsock) {
2381 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",
Willy Tarreau1772ece2009-04-03 14:49:12 +02002382 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002383 }
2384
William Lallemand095ba4c2017-06-01 17:38:50 +02002385 if (global.mode & (MODE_DAEMON | MODE_MWORKER)) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002386 struct proxy *px;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002387 struct peers *curpeers;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002388 int ret = 0;
2389 int proc;
2390
William Lallemand73b85e72017-06-01 17:38:51 +02002391 children = calloc(global.nbproc, sizeof(int));
William Lallemand095ba4c2017-06-01 17:38:50 +02002392 /*
2393 * if daemon + mworker: must fork here to let a master
2394 * process live in background before forking children
2395 */
William Lallemand73b85e72017-06-01 17:38:51 +02002396
2397 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
2398 && (global.mode & MODE_MWORKER)
2399 && (global.mode & MODE_DAEMON)) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002400 ret = fork();
2401 if (ret < 0) {
2402 Alert("[%s.main()] Cannot fork.\n", argv[0]);
2403 protocol_unbind_all();
2404 exit(1); /* there has been an error */
2405 }
2406 /* parent leave to daemonize */
2407 if (ret > 0)
2408 exit(0);
2409 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002410 /* the father launches the required number of processes */
2411 for (proc = 0; proc < global.nbproc; proc++) {
2412 ret = fork();
2413 if (ret < 0) {
2414 Alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002415 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +02002416 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002417 }
2418 else if (ret == 0) /* child breaks here */
2419 break;
Marc-Antoine Perennou992709b2013-02-12 10:53:52 +01002420 children[proc] = ret;
Willy Tarreau269ab312012-09-05 08:02:48 +02002421 if (pidfd >= 0) {
2422 char pidstr[100];
2423 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002424 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002425 }
Willy Tarreaudcd47712007-11-04 23:35:08 +01002426 relative_pid++; /* each child will get a different one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002427 }
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002428
2429#ifdef USE_CPU_AFFINITY
2430 if (proc < global.nbproc && /* child */
Willy Tarreaue7597492015-04-20 11:36:57 +02002431 proc < LONGBITS && /* only the first 32/64 processes may be pinned */
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002432 global.cpu_map[proc]) /* only do this if the process has a CPU map */
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002433#ifdef __FreeBSD__
2434 cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(unsigned long), (void *)&global.cpu_map[proc]);
2435#else
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002436 sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map[proc]);
2437#endif
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002438#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002439 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02002440 if (pidfd >= 0) {
2441 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
2442 close(pidfd);
2443 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02002444
2445 /* We won't ever use this anymore */
Willy Tarreaud137dd32010-08-25 12:49:05 +02002446 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002447
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002448 if (proc == global.nbproc) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002449 if (global.mode & MODE_MWORKER) {
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002450 protocol_unbind_all();
William Lallemand73b85e72017-06-01 17:38:51 +02002451 mworker_wait();
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002452 }
Grant Zhang872f9c22017-01-21 01:10:18 +00002453#ifndef OPENSSL_NO_DH
2454 ssl_free_dh();
2455#endif
William Lallemandcb11fd22017-06-01 17:38:52 +02002456 /* should never get there */
2457 exit(EXIT_FAILURE);
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002458 }
2459
William Lallemandcb11fd22017-06-01 17:38:52 +02002460 /* child must never use the atexit function */
2461 atexit_flag = 0;
2462
William Lallemand095ba4c2017-06-01 17:38:50 +02002463 /* Must chroot and setgid/setuid in the children */
2464 /* chroot if needed */
2465 if (global.chroot != NULL) {
2466 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
2467 Alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot);
2468 if (nb_oldpids)
2469 tell_old_pids(SIGTTIN);
2470 protocol_unbind_all();
2471 exit(1);
2472 }
2473 }
2474
2475 free(global.chroot);
2476 global.chroot = NULL;
2477
2478 /* setgid / setuid */
2479 if (global.gid) {
2480 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
2481 Warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2482 " without 'uid'/'user' is generally useless.\n", argv[0]);
2483
2484 if (setgid(global.gid) == -1) {
2485 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
2486 protocol_unbind_all();
2487 exit(1);
2488 }
2489 }
2490
2491 if (global.uid && setuid(global.uid) == -1) {
2492 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
2493 protocol_unbind_all();
2494 exit(1);
2495 }
2496
William Lallemand7f80eb22017-05-26 18:19:55 +02002497 /* pass through every cli socket, and check if it's bound to
2498 * the current process and if it exposes listeners sockets.
2499 * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork.
2500 * */
2501
2502 if (global.stats_fe) {
2503 struct bind_conf *bind_conf;
2504
2505 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
2506 if (bind_conf->level & ACCESS_FD_LISTENERS) {
2507 if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) {
2508 global.tune.options |= GTUNE_SOCKET_TRANSFER;
2509 break;
2510 }
2511 }
2512 }
2513 }
2514
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002515 /* we might have to unbind some proxies from some processes */
2516 px = proxy;
2517 while (px != NULL) {
2518 if (px->bind_proc && px->state != PR_STSTOPPED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002519 if (!(px->bind_proc & (1UL << proc))) {
2520 if (global.tune.options & GTUNE_SOCKET_TRANSFER)
2521 zombify_proxy(px);
2522 else
2523 stop_proxy(px);
2524 }
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002525 }
2526 px = px->next;
2527 }
2528
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002529 /* we might have to unbind some peers sections from some processes */
2530 for (curpeers = peers; curpeers; curpeers = curpeers->next) {
2531 if (!curpeers->peers_fe)
2532 continue;
2533
2534 if (curpeers->peers_fe->bind_proc & (1UL << proc))
2535 continue;
2536
2537 stop_proxy(curpeers->peers_fe);
2538 /* disable this peer section so that it kills itself */
Willy Tarreau47c8c022015-09-28 16:39:25 +02002539 signal_unregister_handler(curpeers->sighandler);
2540 task_delete(curpeers->sync_task);
2541 task_free(curpeers->sync_task);
2542 curpeers->sync_task = NULL;
2543 task_free(curpeers->peers_fe->task);
2544 curpeers->peers_fe->task = NULL;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002545 curpeers->peers_fe = NULL;
2546 }
2547
Willy Tarreaubaaee002006-06-26 02:48:02 +02002548 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
2549 * that we can detach from the TTY. We MUST NOT do it in other cases since
2550 * it would have already be done, and 0-2 would have been affected to listening
2551 * sockets
2552 */
Willy Tarreau106cb762008-11-16 07:40:34 +01002553 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002554 /* detach from the tty */
2555 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreau106cb762008-11-16 07:40:34 +01002556 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002557 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
2558 }
2559 pid = getpid(); /* update child's pid */
2560 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02002561 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002562 }
2563
Baptiste Assmann26c6eb82017-02-02 23:14:51 +01002564 /* initialize structures for name resolution */
2565 if (!dns_init_resolvers(1))
2566 exit(1);
2567
Willy Tarreaudd815982007-10-16 12:25:14 +02002568 protocol_enable_all();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002569 /*
2570 * That's it : the central polling loop. Run until we stop.
2571 */
2572 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002573
Willy Tarreaubaaee002006-06-26 02:48:02 +02002574 /* Do some cleanup */
2575 deinit();
2576
2577 exit(0);
2578}
2579
2580
2581/*
2582 * Local variables:
2583 * c-indent-level: 8
2584 * c-basic-offset: 8
2585 * End:
2586 */