blob: 8f33e2695aecb9edc927e9146fbe94189e95a9d6 [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>
Christopher Fauletbe0faa22017-08-29 15:37:10 +020080#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020081
82#include <types/capture.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020083#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020084#include <types/global.h>
Simon Hormanac821422011-07-15 13:14:09 +090085#include <types/acl.h>
Willy Tarreau3c63fd82011-09-07 18:00:47 +020086#include <types/peers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020087
Willy Tarreau0fc45a72007-06-17 00:36:03 +020088#include <proto/acl.h>
Willy Tarreau3c595ac2015-04-19 09:59:31 +020089#include <proto/applet.h>
Willy Tarreau2e845be2012-10-19 19:49:09 +020090#include <proto/arg.h>
Willy Tarreau3c595ac2015-04-19 09:59:31 +020091#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020092#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020093#include <proto/channel.h>
Willy Tarreauf2943dc2012-10-26 20:10:28 +020094#include <proto/connection.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020095#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020096#include <proto/filters.h>
Willy Tarreau34eb6712011-10-24 18:15:04 +020097#include <proto/hdr_idx.h>
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010098#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020099#include <proto/listener.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100#include <proto/log.h>
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100101#include <proto/pattern.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +0200102#include <proto/protocol.h>
Willy Tarreau80587432006-12-24 17:47:20 +0100103#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104#include <proto/proxy.h>
105#include <proto/queue.h>
106#include <proto/server.h>
Willy Tarreaub1ec8c42015-04-03 13:53:24 +0200107#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +0200108#include <proto/stream.h>
Willy Tarreau29857942009-05-10 09:01:21 +0200109#include <proto/signal.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110#include <proto/task.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +0200111#include <proto/dns.h>
Christopher Fauletff2613e2016-11-09 11:36:17 +0100112#include <proto/vars.h>
Willy Tarreau50bc31d2017-08-16 15:35:19 +0200113#ifdef USE_OPENSSL
Grant Zhang872f9c22017-01-21 01:10:18 +0000114#include <proto/ssl_sock.h>
Willy Tarreau50bc31d2017-08-16 15:35:19 +0200115#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116
Willy Tarreau477ecd82010-01-03 21:12:30 +0100117/* list of config files */
118static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100120int relative_pid = 1; /* process id starting at 1 */
Willy Tarreau387bd4f2017-11-10 19:08:14 +0100121unsigned long pid_bit = 1; /* bit corresponding to the process id */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122
123/* global options */
124struct global global = {
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100125 .hard_stop_after = TICK_ETERNITY,
Willy Tarreau247a13a2012-11-15 17:38:15 +0100126 .nbproc = 1,
Christopher Fauletbe0faa22017-08-29 15:37:10 +0200127 .nbthread = 1,
William Lallemand5f232402012-04-05 18:02:55 +0200128 .req_count = 0,
William Lallemand0f99e342011-10-12 17:50:54 +0200129 .logsrvs = LIST_HEAD_INIT(global.logsrvs),
William Lallemand9d5f5482012-11-07 16:12:57 +0100130 .maxzlibmem = 0,
William Lallemandd85f9172012-11-09 17:05:39 +0100131 .comp_rate_lim = 0,
Emeric Brun850efd52014-01-29 12:24:34 +0100132 .ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED,
Emeric Bruned760922010-10-22 17:59:25 +0200133 .unix_bind = {
134 .ux = {
135 .uid = -1,
136 .gid = -1,
137 .mode = 0,
138 }
139 },
Willy Tarreau27a674e2009-08-17 07:23:33 +0200140 .tune = {
141 .bufsize = BUFSIZE,
Willy Tarreau27097842015-09-28 13:53:23 +0200142 .maxrewrite = -1,
Willy Tarreau43961d52010-10-04 20:39:20 +0200143 .chksize = BUFSIZE,
Willy Tarreaua24adf02014-11-27 01:11:56 +0100144 .reserved_bufs = RESERVED_BUFS,
Willy Tarreauf3045d22015-04-29 16:24:50 +0200145 .pattern_cache = DEFAULT_PAT_LRU_SIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200146#ifdef USE_OPENSSL
Emeric Brun46635772012-11-14 11:32:56 +0100147 .sslcachesize = SSLCACHESIZE,
Emeric Brunfc32aca2012-09-03 12:10:29 +0200148#endif
William Lallemandf3747832012-11-09 12:33:10 +0100149 .comp_maxlevel = 1,
Willy Tarreau7e312732014-02-12 16:35:14 +0100150#ifdef DEFAULT_IDLE_TIMER
151 .idle_timer = DEFAULT_IDLE_TIMER,
152#else
153 .idle_timer = 1000, /* 1 second */
154#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200155 },
Emeric Brun76d88952012-10-05 15:47:31 +0200156#ifdef USE_OPENSSL
157#ifdef DEFAULT_MAXSSLCONN
Willy Tarreau403edff2012-09-06 11:58:37 +0200158 .maxsslconn = DEFAULT_MAXSSLCONN,
159#endif
Emeric Brun76d88952012-10-05 15:47:31 +0200160#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200161 /* others NULL OK */
162};
163
164/*********************************************************************/
165
166int stopping; /* non zero means stopping in progress */
Cyril Bonté203ec5a2017-03-23 22:44:13 +0100167int killed; /* non zero means a hard-stop is triggered */
Willy Tarreauaf7ad002010-08-31 15:39:26 +0200168int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200169
170/* Here we store informations about the pids of the processes we may pause
171 * or kill. We will send them a signal every 10 ms until we can bind to all
172 * our ports. With 200 retries, that's about 2 seconds.
173 */
174#define MAX_START_RETRIES 200
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175static int *oldpids = NULL;
176static int oldpids_sig; /* use USR1 or TERM */
177
Olivier Houchardf73629d2017-04-05 22:33:04 +0200178/* Path to the unix socket we use to retrieve listener sockets from the old process */
179static const char *old_unixsocket;
180
William Lallemand85b0bd92017-06-01 17:38:53 +0200181static char *cur_unixsocket = NULL;
182
William Lallemandcb11fd22017-06-01 17:38:52 +0200183int atexit_flag = 0;
184
Willy Tarreaubb545b42010-08-25 12:58:59 +0200185int nb_oldpids = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200186const int zero = 0;
187const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200188const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189
Willy Tarreau1d21e0a2010-03-12 21:58:54 +0100190char hostname[MAX_HOSTNAME_LEN];
Emeric Brun2b920a12010-09-23 18:30:22 +0200191char localpeer[MAX_HOSTNAME_LEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200192
Willy Tarreau89efaed2013-12-13 15:14:55 +0100193/* used from everywhere just to drain results we don't want to read and which
194 * recent versions of gcc increasingly and annoyingly complain about.
195 */
196int shut_your_big_mouth_gcc_int = 0;
197
William Lallemand73b85e72017-06-01 17:38:51 +0200198int *children = NULL; /* store PIDs of children in master workers mode */
199
200static volatile sig_atomic_t caught_signal = 0;
201static char **next_argv = NULL;
202
William Lallemande20b6a62017-06-01 17:38:55 +0200203int mworker_pipe[2];
204
Willy Tarreau08ceb102011-07-24 22:58:00 +0200205/* list of the temporarily limited listeners because of lack of resource */
206struct list global_listener_queue = LIST_HEAD_INIT(global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +0200207struct task *global_listener_queue_task;
208static struct task *manage_global_listener_queue(struct task *t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200209
Willy Tarreauff055502014-04-28 22:27:06 +0200210/* bitfield of a few warnings to emit just once (WARN_*) */
211unsigned int warned = 0;
212
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100213
214/* These are strings to be reported in the output of "haproxy -vv". They may
215 * either be constants (in which case must_free must be zero) or dynamically
216 * allocated strings to pass to free() on exit, and in this case must_free
217 * must be non-zero.
218 */
219struct list build_opts_list = LIST_HEAD_INIT(build_opts_list);
220struct build_opts_str {
221 struct list list;
222 const char *str;
223 int must_free;
224};
225
Willy Tarreaue6945732016-12-21 19:57:00 +0100226/* These functions are called just after the point where the program exits
227 * after a config validity check, so they are generally suited for resource
228 * allocation and slow initializations that should be skipped during basic
229 * config checks. The functions must return 0 on success, or a combination
230 * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause
231 * and immediate exit, so the function must have emitted any useful error.
232 */
233struct list post_check_list = LIST_HEAD_INIT(post_check_list);
234struct post_check_fct {
235 struct list list;
236 int (*fct)();
237};
238
Willy Tarreau05554e62016-12-21 20:46:26 +0100239/* These functions are called when freeing the global sections at the end
240 * of deinit, after everything is stopped. They don't return anything, and
241 * they work in best effort mode as their sole goal is to make valgrind
242 * mostly happy.
243 */
244struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list);
245struct post_deinit_fct {
246 struct list list;
247 void (*fct)();
248};
249
Christopher Faulet415f6112017-07-25 16:52:58 +0200250/* These functions are called for each thread just after the thread creation
251 * and before running the scheduler. They should be used to do per-thread
252 * initializations. They must return 0 if an error occurred. */
253struct list per_thread_init_list = LIST_HEAD_INIT(per_thread_init_list);
254struct per_thread_init_fct {
255 struct list list;
256 int (*fct)();
257};
258
259/* These functions are called for each thread just after the scheduler loop and
260 * before exiting the thread. They don't return anything and, as for post-deinit
261 * functions, they work in best effort mode as their sole goal is to make
262 * valgrind mostly happy. */
263struct list per_thread_deinit_list = LIST_HEAD_INIT(per_thread_deinit_list);
264struct per_thread_deinit_fct {
265 struct list list;
266 void (*fct)();
267};
268
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269/*********************************************************************/
270/* general purpose functions ***************************************/
271/*********************************************************************/
272
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100273/* used to register some build option strings at boot. Set must_free to
274 * non-zero if the string must be freed upon exit.
275 */
276void hap_register_build_opts(const char *str, int must_free)
277{
278 struct build_opts_str *b;
279
280 b = calloc(1, sizeof(*b));
281 if (!b) {
282 fprintf(stderr, "out of memory\n");
283 exit(1);
284 }
285 b->str = str;
286 b->must_free = must_free;
287 LIST_ADDQ(&build_opts_list, &b->list);
288}
289
Willy Tarreaue6945732016-12-21 19:57:00 +0100290/* used to register some initialization functions to call after the checks. */
291void hap_register_post_check(int (*fct)())
292{
293 struct post_check_fct *b;
294
295 b = calloc(1, sizeof(*b));
296 if (!b) {
297 fprintf(stderr, "out of memory\n");
298 exit(1);
299 }
300 b->fct = fct;
301 LIST_ADDQ(&post_check_list, &b->list);
302}
303
Willy Tarreau05554e62016-12-21 20:46:26 +0100304/* used to register some de-initialization functions to call after everything
305 * has stopped.
306 */
307void hap_register_post_deinit(void (*fct)())
308{
309 struct post_deinit_fct *b;
310
311 b = calloc(1, sizeof(*b));
312 if (!b) {
313 fprintf(stderr, "out of memory\n");
314 exit(1);
315 }
316 b->fct = fct;
317 LIST_ADDQ(&post_deinit_list, &b->list);
318}
319
Christopher Faulet415f6112017-07-25 16:52:58 +0200320/* used to register some initialization functions to call for each thread. */
321void hap_register_per_thread_init(int (*fct)())
322{
323 struct per_thread_init_fct *b;
324
325 b = calloc(1, sizeof(*b));
326 if (!b) {
327 fprintf(stderr, "out of memory\n");
328 exit(1);
329 }
330 b->fct = fct;
331 LIST_ADDQ(&per_thread_init_list, &b->list);
332}
333
334/* used to register some de-initialization functions to call for each thread. */
335void hap_register_per_thread_deinit(void (*fct)())
336{
337 struct per_thread_deinit_fct *b;
338
339 b = calloc(1, sizeof(*b));
340 if (!b) {
341 fprintf(stderr, "out of memory\n");
342 exit(1);
343 }
344 b->fct = fct;
345 LIST_ADDQ(&per_thread_deinit_list, &b->list);
346}
347
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100348static void display_version()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349{
350 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau7b677262017-04-03 09:27:49 +0200351 printf("Copyright 2000-2017 Willy Tarreau <willy@haproxy.org>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352}
353
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100354static void display_build_opts()
Willy Tarreau7b066db2007-12-02 11:28:59 +0100355{
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100356 struct build_opts_str *item;
357
Willy Tarreau7b066db2007-12-02 11:28:59 +0100358 printf("Build options :"
359#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100360 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100361#endif
362#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100363 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100364#endif
365#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100366 "\n CC = " BUILD_CC
367#endif
368#ifdef BUILD_CFLAGS
369 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100370#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100371#ifdef BUILD_OPTIONS
372 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100373#endif
Willy Tarreau27a674e2009-08-17 07:23:33 +0200374 "\n\nDefault settings :"
375 "\n maxconn = %d, bufsize = %d, maxrewrite = %d, maxpollevents = %d"
376 "\n\n",
377 DEFAULT_MAXCONN, BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS);
Willy Tarreaube5b6852009-10-03 18:57:08 +0200378
Willy Tarreaucdb737e2016-12-21 18:43:10 +0100379 list_for_each_entry(item, &build_opts_list, list) {
380 puts(item->str);
381 }
382
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100383 putchar('\n');
384
Willy Tarreaube5b6852009-10-03 18:57:08 +0200385 list_pollers(stdout);
386 putchar('\n');
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100387 list_filters(stdout);
388 putchar('\n');
Willy Tarreau7b066db2007-12-02 11:28:59 +0100389}
390
Willy Tarreaubaaee002006-06-26 02:48:02 +0200391/*
392 * This function prints the command line usage and exits
393 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100394static void usage(char *name)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395{
396 display_version();
397 fprintf(stderr,
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200398 "Usage : %s [-f <cfgfile|cfgdir>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200399 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
Willy Tarreaua088d312015-10-08 11:58:48 +0200400 " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100401 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200402 " -d enters debug mode ; -db only disables background mode.\n"
Willy Tarreau6e064432012-05-08 15:40:42 +0200403 " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau576132e2011-09-10 19:26:56 +0200405 " -D goes daemon ; -C changes to <dir> before loading files.\n"
William Lallemand095ba4c2017-06-01 17:38:50 +0200406 " -W master-worker mode.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200408 " -c check mode : only check config files and exit\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409 " -n sets the maximum total # of connections (%d)\n"
410 " -m limits the usable amount of memory (in MB)\n"
411 " -N sets the default, per-proxy maximum # of connections (%d)\n"
Emeric Brun2b920a12010-09-23 18:30:22 +0200412 " -L set local peer name (default to hostname)\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413 " -p writes pids of all children to this file\n"
414#if defined(ENABLE_EPOLL)
415 " -de disables epoll() usage even when available\n"
416#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200417#if defined(ENABLE_KQUEUE)
418 " -dk disables kqueue() usage even when available\n"
419#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200420#if defined(ENABLE_POLL)
421 " -dp disables poll() usage even when available\n"
422#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +0200423#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100424 " -dS disables splice usage (broken on old kernels)\n"
425#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200426#if defined(USE_GETADDRINFO)
427 " -dG disables getaddrinfo() usage\n"
428#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000429#if defined(SO_REUSEPORT)
430 " -dR disables SO_REUSEPORT usage\n"
431#endif
Willy Tarreau3eed10e2016-11-07 21:03:16 +0100432 " -dr ignores server address resolution failures\n"
Emeric Brun850efd52014-01-29 12:24:34 +0100433 " -dV disables SSL verify on servers side\n"
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +0200434 " -sf/-st [pid ]* finishes/terminates old pids.\n"
Olivier Houchardf73629d2017-04-05 22:33:04 +0200435 " -x <unix_socket> get listening sockets from a unix socket\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200436 "\n",
437 name, DEFAULT_MAXCONN, cfg_maxpconn);
438 exit(1);
439}
440
441
442
443/*********************************************************************/
444/* more specific functions ***************************************/
445/*********************************************************************/
446
William Lallemand73b85e72017-06-01 17:38:51 +0200447/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
448 * pids the signal was correctly delivered to.
449 */
450static int tell_old_pids(int sig)
451{
452 int p;
453 int ret = 0;
454 for (p = 0; p < nb_oldpids; p++)
455 if (kill(oldpids[p], sig) == 0)
456 ret++;
457 return ret;
458}
459
460/* return 1 if a pid is a current child otherwise 0 */
461
462int current_child(int pid)
463{
464 int i;
465
466 for (i = 0; i < global.nbproc; i++) {
467 if (children[i] == pid)
468 return 1;
469 }
470 return 0;
471}
472
473static void mworker_signalhandler(int signum)
474{
475 caught_signal = signum;
476}
477
478static void mworker_register_signals()
479{
480 struct sigaction sa;
481 /* Here we are not using the haproxy async way
482 for signals because it does not exists in
483 the master */
484 memset(&sa, 0, sizeof(struct sigaction));
485 sa.sa_handler = &mworker_signalhandler;
486 sigaction(SIGHUP, &sa, NULL);
487 sigaction(SIGUSR1, &sa, NULL);
488 sigaction(SIGUSR2, &sa, NULL);
489 sigaction(SIGINT, &sa, NULL);
490 sigaction(SIGTERM, &sa, NULL);
491}
492
493static void mworker_block_signals()
494{
495 sigset_t set;
496
497 sigemptyset(&set);
498 sigaddset(&set, SIGUSR1);
499 sigaddset(&set, SIGUSR2);
500 sigaddset(&set, SIGHUP);
501 sigaddset(&set, SIGINT);
502 sigaddset(&set, SIGTERM);
503 sigprocmask(SIG_SETMASK, &set, NULL);
504}
505
506static void mworker_unblock_signals()
507{
508 sigset_t set;
509
510 sigemptyset(&set);
511 sigaddset(&set, SIGUSR1);
512 sigaddset(&set, SIGUSR2);
513 sigaddset(&set, SIGHUP);
514 sigaddset(&set, SIGINT);
515 sigaddset(&set, SIGTERM);
516 sigprocmask(SIG_UNBLOCK, &set, NULL);
517}
518
519static void mworker_unregister_signals()
520{
521 signal(SIGINT, SIG_DFL);
522 signal(SIGTERM, SIG_DFL);
523 signal(SIGHUP, SIG_IGN);
524 signal(SIGUSR1, SIG_IGN);
525 signal(SIGUSR2, SIG_IGN);
526}
527
528/*
529 * Send signal to every known children.
530 */
531
532static void mworker_kill(int sig)
533{
534 int i;
535
536 tell_old_pids(sig);
537 if (children) {
538 for (i = 0; i < global.nbproc; i++)
539 kill(children[i], sig);
540 }
541}
542
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543/*
William Lallemand75ea0a02017-11-15 19:02:58 +0100544 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
545 * fd, and the FD provided by fd@
546 */
547static void mworker_cleanlisteners()
548{
549 struct listener *l, *l_next;
550 struct proxy *curproxy;
551
552 for (curproxy = proxy; curproxy; curproxy = curproxy->next) {
553
554 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
555 /* does not close if the FD is inherited with fd@
556 * from the parent process */
557 if (!(l->options & LI_O_INHERITED)) {
558 close(l->fd);
559 LIST_DEL(&l->by_fe);
560 LIST_DEL(&l->by_bind);
561 free(l->name);
562 free(l->counters);
563 free(l);
564 }
565 }
566 }
567}
568
569
570/*
William Lallemand73b85e72017-06-01 17:38:51 +0200571 * remove a pid forom the olpid array and decrease nb_oldpids
572 * return 1 pid was found otherwise return 0
573 */
574
575int delete_oldpid(int pid)
576{
577 int i;
578
579 for (i = 0; i < nb_oldpids; i++) {
580 if (oldpids[i] == pid) {
581 oldpids[i] = oldpids[nb_oldpids - 1];
582 oldpids[nb_oldpids - 1] = 0;
583 nb_oldpids--;
584 return 1;
585 }
586 }
587 return 0;
588}
589
William Lallemand85b0bd92017-06-01 17:38:53 +0200590
591static void get_cur_unixsocket()
592{
593 /* if -x was used, try to update the stat socket if not available anymore */
594 if (global.stats_fe) {
595 struct bind_conf *bind_conf;
596
597 /* pass through all stats socket */
598 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
599 struct listener *l;
600
601 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
602
603 if (l->addr.ss_family == AF_UNIX &&
604 (bind_conf->level & ACCESS_FD_LISTENERS)) {
605 const struct sockaddr_un *un;
606
607 un = (struct sockaddr_un *)&l->addr;
608 /* priority to old_unixsocket */
609 if (!cur_unixsocket) {
610 cur_unixsocket = strdup(un->sun_path);
611 } else {
612 if (old_unixsocket && !strcmp(un->sun_path, old_unixsocket)) {
613 free(cur_unixsocket);
614 cur_unixsocket = strdup(old_unixsocket);
615 return;
616 }
617 }
618 }
619 }
620 }
621 }
622 if (!cur_unixsocket && old_unixsocket)
623 cur_unixsocket = strdup(old_unixsocket);
624}
625
William Lallemand73b85e72017-06-01 17:38:51 +0200626/*
627 * When called, this function reexec haproxy with -sf followed by current
628 * children PIDs and possibily old children PIDs if they didn't leave yet.
629 */
630static void mworker_reload()
631{
632 int next_argc = 0;
633 int j;
634 char *msg = NULL;
635
636 mworker_block_signals();
637 mworker_unregister_signals();
638 setenv("HAPROXY_MWORKER_REEXEC", "1", 1);
639
640 /* compute length */
641 while (next_argv[next_argc])
642 next_argc++;
643
William Lallemand85b0bd92017-06-01 17:38:53 +0200644 /* 1 for haproxy -sf, 2 for -x /socket */
645 next_argv = realloc(next_argv, (next_argc + 1 + 2 + global.nbproc + nb_oldpids + 1) * sizeof(char *));
William Lallemand73b85e72017-06-01 17:38:51 +0200646 if (next_argv == NULL)
647 goto alloc_error;
648
649
650 /* add -sf <PID>* to argv */
651 if (children || nb_oldpids > 0)
652 next_argv[next_argc++] = "-sf";
653 if (children) {
654 for (j = 0; j < global.nbproc; next_argc++,j++) {
655 next_argv[next_argc] = memprintf(&msg, "%d", children[j]);
656 if (next_argv[next_argc] == NULL)
657 goto alloc_error;
658 msg = NULL;
659 }
660 }
661 /* copy old process PIDs */
662 for (j = 0; j < nb_oldpids; next_argc++,j++) {
663 next_argv[next_argc] = memprintf(&msg, "%d", oldpids[j]);
664 if (next_argv[next_argc] == NULL)
665 goto alloc_error;
666 msg = NULL;
667 }
668 next_argv[next_argc] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200669
William Lallemand2bf6d622017-06-20 11:20:23 +0200670 /* add the -x option with the stat socket */
William Lallemand85b0bd92017-06-01 17:38:53 +0200671 if (cur_unixsocket) {
672
William Lallemand2bf6d622017-06-20 11:20:23 +0200673 next_argv[next_argc++] = "-x";
674 next_argv[next_argc++] = (char *)cur_unixsocket;
675 next_argv[next_argc++] = NULL;
William Lallemand85b0bd92017-06-01 17:38:53 +0200676 }
677
William Lallemand73b85e72017-06-01 17:38:51 +0200678 Warning("Reexecuting Master process\n");
Tim Duesterhus0436ab72017-11-12 17:39:18 +0100679 execvp(next_argv[0], next_argv);
William Lallemand73b85e72017-06-01 17:38:51 +0200680
William Lallemand722d4ca2017-11-15 19:02:55 +0100681 Warning("Failed to reexecute the master process [%d]: %s\n", pid, strerror(errno));
682 return;
683
William Lallemand73b85e72017-06-01 17:38:51 +0200684alloc_error:
William Lallemand722d4ca2017-11-15 19:02:55 +0100685 Warning("Failed to reexecute the master processs [%d]: Cannot allocate memory\n", pid);
William Lallemand73b85e72017-06-01 17:38:51 +0200686 return;
687}
688
689
690/*
691 * Wait for every children to exit
692 */
693
694static void mworker_wait()
695{
696 int exitpid = -1;
697 int status = 0;
698
William Lallemand2f8b31c2017-11-15 19:02:56 +0100699restart_wait:
700
William Lallemand73b85e72017-06-01 17:38:51 +0200701 mworker_register_signals();
702 mworker_unblock_signals();
703
704 while (1) {
705
706 while (((exitpid = wait(&status)) == -1) && errno == EINTR) {
707 int sig = caught_signal;
708 if (sig == SIGUSR2 || sig == SIGHUP) {
709 mworker_reload();
William Lallemand2f8b31c2017-11-15 19:02:56 +0100710 /* should reach there only if it fail */
711 goto restart_wait;
William Lallemand73b85e72017-06-01 17:38:51 +0200712 } else {
713 Warning("Exiting Master process...\n");
714 mworker_kill(sig);
715 mworker_unregister_signals();
716 }
717 caught_signal = 0;
718 }
719
720 if (exitpid == -1 && errno == ECHILD) {
721 Warning("All workers are left. Leaving... (%d)\n", status);
William Lallemandcb11fd22017-06-01 17:38:52 +0200722 atexit_flag = 0;
William Lallemand73b85e72017-06-01 17:38:51 +0200723 exit(status); /* parent must leave using the latest status code known */
724 }
725
726 if (WIFEXITED(status))
727 status = WEXITSTATUS(status);
728 else if (WIFSIGNALED(status))
729 status = 128 + WTERMSIG(status);
730 else if (WIFSTOPPED(status))
731 status = 128 + WSTOPSIG(status);
732 else
733 status = 255;
734
735 if (!children) {
736 Warning("Worker %d left with exit code %d\n", exitpid, status);
737 } else {
738 /* check if exited child was in the current children list */
739 if (current_child(exitpid)) {
740 Alert("Current worker %d left with exit code %d\n", exitpid, status);
William Lallemand69f9b3b2017-06-01 17:38:54 +0200741 if (status != 0 && status != 130 && status != 143
742 && global.tune.options & GTUNE_EXIT_ONFAILURE) {
743 Alert("exit-on-failure: killing every workers with SIGTERM\n");
744 mworker_kill(SIGTERM);
745 }
William Lallemand73b85e72017-06-01 17:38:51 +0200746 } else {
747 Warning("Former worker %d left with exit code %d\n", exitpid, status);
748 delete_oldpid(exitpid);
749 }
750 }
751 }
752}
753
William Lallemandcb11fd22017-06-01 17:38:52 +0200754
755/*
756 * Reexec the process in failure mode, instead of exiting
757 */
758void reexec_on_failure()
759{
760 if (!atexit_flag)
761 return;
762
763 setenv("HAPROXY_MWORKER_WAIT_ONLY", "1", 1);
764
765 Warning("Reexecuting Master process in waitpid mode\n");
766 mworker_reload();
William Lallemandcb11fd22017-06-01 17:38:52 +0200767}
William Lallemand73b85e72017-06-01 17:38:51 +0200768
769
770/*
Willy Tarreaud0807c32010-08-27 18:26:11 +0200771 * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts
772 * a signal zero to all subscribers. This means that it's as easy as
773 * subscribing to signal 0 to get informed about an imminent shutdown.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100775static void sig_soft_stop(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200776{
777 soft_stop();
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200778 signal_unregister_handler(sh);
Christopher Fauletb349e482017-08-29 09:52:38 +0200779 pool_gc2(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200780}
781
782/*
783 * upon SIGTTOU, we pause everything
784 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100785static void sig_pause(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200786{
787 pause_proxies();
Christopher Fauletb349e482017-08-29 09:52:38 +0200788 pool_gc2(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200789}
790
791/*
792 * upon SIGTTIN, let's have a soft stop.
793 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100794static void sig_listen(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200795{
Willy Tarreaube58c382011-07-24 18:28:10 +0200796 resume_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797}
798
799/*
800 * this function dumps every server's state when the process receives SIGHUP.
801 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100802static void sig_dump_state(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803{
804 struct proxy *p = proxy;
805
806 Warning("SIGHUP received, dumping servers states.\n");
807 while (p) {
808 struct server *s = p->srv;
809
810 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
811 while (s) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100812 chunk_printf(&trash,
813 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
814 p->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +0200815 (s->cur_state != SRV_ST_STOPPED) ? "UP" : "DOWN",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100816 s->cur_sess, s->nbpend, s->counters.cum_sess);
817 Warning("%s\n", trash.str);
818 send_log(p, LOG_NOTICE, "%s\n", trash.str);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200819 s = s->next;
820 }
821
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200822 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
823 if (!p->srv) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100824 chunk_printf(&trash,
825 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
826 p->id,
827 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 +0200828 } else if (p->srv_act == 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100829 chunk_printf(&trash,
830 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
831 p->id,
832 (p->srv_bck) ? "is running on backup servers" : "has no server available",
833 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 +0200834 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100835 chunk_printf(&trash,
836 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
837 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
838 p->id, p->srv_act, p->srv_bck,
839 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 +0200840 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100841 Warning("%s\n", trash.str);
842 send_log(p, LOG_NOTICE, "%s\n", trash.str);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843
844 p = p->next;
845 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200846}
847
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100848static void dump(struct sig_handler *sh)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200849{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200850 /* dump memory usage then free everything possible */
851 dump_pools();
Christopher Fauletb349e482017-08-29 09:52:38 +0200852 pool_gc2(NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200853}
854
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200855/* This function check if cfg_cfgfiles containes directories.
856 * If it find one, it add all the files (and only files) it containes
857 * in cfg_cfgfiles in place of the directory (and remove the directory).
858 * It add the files in lexical order.
859 * It add only files with .cfg extension.
860 * It doesn't add files with name starting with '.'
861 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +0100862static void cfgfiles_expand_directories(void)
Maxime de Roucy379d9c72016-05-13 23:52:56 +0200863{
864 struct wordlist *wl, *wlb;
865 char *err = NULL;
866
867 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
868 struct stat file_stat;
869 struct dirent **dir_entries = NULL;
870 int dir_entries_nb;
871 int dir_entries_it;
872
873 if (stat(wl->s, &file_stat)) {
874 Alert("Cannot open configuration file/directory %s : %s\n",
875 wl->s,
876 strerror(errno));
877 exit(1);
878 }
879
880 if (!S_ISDIR(file_stat.st_mode))
881 continue;
882
883 /* from this point wl->s is a directory */
884
885 dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort);
886 if (dir_entries_nb < 0) {
887 Alert("Cannot open configuration directory %s : %s\n",
888 wl->s,
889 strerror(errno));
890 exit(1);
891 }
892
893 /* for each element in the directory wl->s */
894 for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) {
895 struct dirent *dir_entry = dir_entries[dir_entries_it];
896 char *filename = NULL;
897 char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg");
898
899 /* don't add filename that begin with .
900 * only add filename with .cfg extention
901 */
902 if (dir_entry->d_name[0] == '.' ||
903 !(d_name_cfgext && d_name_cfgext[4] == '\0'))
904 goto next_dir_entry;
905
906 if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) {
907 Alert("Cannot load configuration files %s : out of memory.\n",
908 filename);
909 exit(1);
910 }
911
912 if (stat(filename, &file_stat)) {
913 Alert("Cannot open configuration file %s : %s\n",
914 wl->s,
915 strerror(errno));
916 exit(1);
917 }
918
919 /* don't add anything else than regular file in cfg_cfgfiles
920 * this way we avoid loops
921 */
922 if (!S_ISREG(file_stat.st_mode))
923 goto next_dir_entry;
924
925 if (!list_append_word(&wl->list, filename, &err)) {
926 Alert("Cannot load configuration files %s : %s\n",
927 filename,
928 err);
929 exit(1);
930 }
931
932next_dir_entry:
933 free(filename);
934 free(dir_entry);
935 }
936
937 free(dir_entries);
938
939 /* remove the current directory (wl) from cfg_cfgfiles */
940 free(wl->s);
941 LIST_DEL(&wl->list);
942 free(wl);
943 }
944
945 free(err);
946}
947
Olivier Houchardf73629d2017-04-05 22:33:04 +0200948static int get_old_sockets(const char *unixsocket)
949{
950 char *cmsgbuf = NULL, *tmpbuf = NULL;
951 int *tmpfd = NULL;
952 struct sockaddr_un addr;
953 struct cmsghdr *cmsg;
954 struct msghdr msghdr;
955 struct iovec iov;
956 struct xfer_sock_list *xfer_sock = NULL;
Olivier Houchard54740872017-04-06 14:45:14 +0200957 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf73629d2017-04-05 22:33:04 +0200958 int sock = -1;
959 int ret = -1;
960 int ret2 = -1;
961 int fd_nb;
962 int got_fd = 0;
963 int i = 0;
964 size_t maxoff = 0, curoff = 0;
965
966 memset(&msghdr, 0, sizeof(msghdr));
967 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
968 if (!cmsgbuf) {
969 Warning("Failed to allocate memory to send sockets\n");
970 goto out;
971 }
972 sock = socket(PF_UNIX, SOCK_STREAM, 0);
973 if (sock < 0) {
974 Warning("Failed to connect to the old process socket '%s'\n",
975 unixsocket);
976 goto out;
977 }
978 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path));
979 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
980 addr.sun_family = PF_UNIX;
981 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
982 if (ret < 0) {
983 Warning("Failed to connect to the old process socket '%s'\n",
984 unixsocket);
985 goto out;
986 }
Olivier Houchard54740872017-04-06 14:45:14 +0200987 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf73629d2017-04-05 22:33:04 +0200988 iov.iov_base = &fd_nb;
989 iov.iov_len = sizeof(fd_nb);
990 msghdr.msg_iov = &iov;
991 msghdr.msg_iovlen = 1;
992 send(sock, "_getsocks\n", strlen("_getsocks\n"), 0);
993 /* First, get the number of file descriptors to be received */
994 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
995 Warning("Failed to get the number of sockets to be transferred !\n");
996 goto out;
997 }
998 if (fd_nb == 0) {
999 ret = 0;
1000 goto out;
1001 }
Olivier Houchardf143b802017-11-04 15:13:01 +01001002 tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001003 if (tmpbuf == NULL) {
1004 Warning("Failed to allocate memory while receiving sockets\n");
1005 goto out;
1006 }
1007 tmpfd = malloc(fd_nb * sizeof(int));
1008 if (tmpfd == NULL) {
1009 Warning("Failed to allocate memory while receiving sockets\n");
1010 goto out;
1011 }
1012 msghdr.msg_control = cmsgbuf;
1013 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
Olivier Houchardf143b802017-11-04 15:13:01 +01001014 iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int));
Olivier Houchardf73629d2017-04-05 22:33:04 +02001015 do {
1016 int ret3;
1017
1018 iov.iov_base = tmpbuf + curoff;
1019 ret = recvmsg(sock, &msghdr, 0);
1020 if (ret == -1 && errno == EINTR)
1021 continue;
1022 if (ret <= 0)
1023 break;
1024 /* Send an ack to let the sender know we got the sockets
1025 * and it can send some more
1026 */
1027 do {
1028 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
1029 } while (ret3 == -1 && errno == EINTR);
1030 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
1031 cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
1032 if (cmsg->cmsg_level == SOL_SOCKET &&
1033 cmsg->cmsg_type == SCM_RIGHTS) {
1034 size_t totlen = cmsg->cmsg_len -
1035 CMSG_LEN(0);
1036 if (totlen / sizeof(int) + got_fd > fd_nb) {
1037 Warning("Got to many sockets !\n");
1038 goto out;
1039 }
1040 /*
1041 * Be paranoid and use memcpy() to avoid any
1042 * potential alignement issue.
1043 */
1044 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
1045 got_fd += totlen / sizeof(int);
1046 }
1047 }
1048 curoff += ret;
1049 } while (got_fd < fd_nb);
1050
1051 if (got_fd != fd_nb) {
1052 Warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
1053 fd_nb, got_fd);
1054 goto out;
1055 }
1056 maxoff = curoff;
1057 curoff = 0;
1058 for (i = 0; i < got_fd; i++) {
1059 int fd = tmpfd[i];
1060 socklen_t socklen;
1061 int len;
1062
1063 xfer_sock = calloc(1, sizeof(*xfer_sock));
1064 if (!xfer_sock) {
1065 Warning("Failed to allocate memory in get_old_sockets() !\n");
1066 break;
1067 }
1068 xfer_sock->fd = -1;
1069
1070 socklen = sizeof(xfer_sock->addr);
1071 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
1072 Warning("Failed to get socket address\n");
1073 free(xfer_sock);
Olivier Houchardbe7b1ce2017-07-17 17:25:33 +02001074 xfer_sock = NULL;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001075 continue;
1076 }
1077 if (curoff >= maxoff) {
1078 Warning("Inconsistency while transferring sockets\n");
1079 goto out;
1080 }
1081 len = tmpbuf[curoff++];
1082 if (len > 0) {
1083 /* We have a namespace */
1084 if (curoff + len > maxoff) {
1085 Warning("Inconsistency while transferring sockets\n");
1086 goto out;
1087 }
1088 xfer_sock->namespace = malloc(len + 1);
1089 if (!xfer_sock->namespace) {
1090 Warning("Failed to allocate memory while transferring sockets\n");
1091 goto out;
1092 }
1093 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
1094 xfer_sock->namespace[len] = 0;
1095 curoff += len;
1096 }
1097 if (curoff >= maxoff) {
1098 Warning("Inconsistency while transferring sockets\n");
1099 goto out;
1100 }
1101 len = tmpbuf[curoff++];
1102 if (len > 0) {
1103 /* We have an interface */
1104 if (curoff + len > maxoff) {
1105 Warning("Inconsistency while transferring sockets\n");
1106 goto out;
1107 }
1108 xfer_sock->iface = malloc(len + 1);
1109 if (!xfer_sock->iface) {
1110 Warning("Failed to allocate memory while transferring sockets\n");
1111 goto out;
1112 }
1113 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
1114 xfer_sock->namespace[len] = 0;
1115 curoff += len;
1116 }
1117 if (curoff + sizeof(int) > maxoff) {
1118 Warning("Inconsistency while transferring sockets\n");
1119 goto out;
1120 }
1121 memcpy(&xfer_sock->options, &tmpbuf[curoff],
1122 sizeof(xfer_sock->options));
1123 curoff += sizeof(xfer_sock->options);
1124
1125 xfer_sock->fd = fd;
1126 if (xfer_sock_list)
1127 xfer_sock_list->prev = xfer_sock;
1128 xfer_sock->next = xfer_sock_list;
1129 xfer_sock->prev = NULL;
1130 xfer_sock_list = xfer_sock;
1131 xfer_sock = NULL;
1132 }
1133
1134 ret2 = 0;
1135out:
1136 /* If we failed midway make sure to close the remaining
1137 * file descriptors
1138 */
1139 if (tmpfd != NULL && i < got_fd) {
1140 for (; i < got_fd; i++) {
1141 close(tmpfd[i]);
1142 }
1143 }
1144 free(tmpbuf);
1145 free(tmpfd);
1146 free(cmsgbuf);
1147 if (sock != -1)
1148 close(sock);
1149 if (xfer_sock) {
1150 free(xfer_sock->namespace);
1151 free(xfer_sock->iface);
1152 if (xfer_sock->fd != -1)
1153 close(xfer_sock->fd);
1154 free(xfer_sock);
1155 }
1156 return (ret2);
1157}
1158
Willy Tarreaubaaee002006-06-26 02:48:02 +02001159/*
William Lallemand73b85e72017-06-01 17:38:51 +02001160 * copy and cleanup the current argv
1161 * Remove the -sf /-st parameters
1162 * Return an allocated copy of argv
1163 */
1164
1165static char **copy_argv(int argc, char **argv)
1166{
1167 char **newargv;
William Lallemand2bf6d622017-06-20 11:20:23 +02001168 int i = 0, j = 0;
William Lallemand73b85e72017-06-01 17:38:51 +02001169
1170 newargv = calloc(argc + 2, sizeof(char *));
1171 if (newargv == NULL) {
1172 Warning("Cannot allocate memory\n");
1173 return NULL;
1174 }
1175
William Lallemand2bf6d622017-06-20 11:20:23 +02001176 while (i < argc) {
1177 /* -sf or -st or -x */
1178 if ((argv[i][1] == 's' && (argv[i][2] == 'f' || argv[i][2] == 't')) || argv[i][1] == 'x' ) {
1179 /* list of pids to finish ('f') or terminate ('t') or unix socket (-x) */
William Lallemand73b85e72017-06-01 17:38:51 +02001180 i++;
1181 while (i < argc && argv[i][0] != '-') {
1182 i++;
1183 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001184 continue;
William Lallemand73b85e72017-06-01 17:38:51 +02001185 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001186
1187 newargv[j++] = argv[i++];
William Lallemand73b85e72017-06-01 17:38:51 +02001188 }
William Lallemand2bf6d622017-06-20 11:20:23 +02001189
William Lallemand73b85e72017-06-01 17:38:51 +02001190 return newargv;
1191}
1192
1193/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001194 * This function initializes all the necessary variables. It only returns
1195 * if everything is OK. If something fails, it exits.
1196 */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01001197static void init(int argc, char **argv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001198{
Willy Tarreaubaaee002006-06-26 02:48:02 +02001199 int arg_mode = 0; /* MODE_DEBUG, ... */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001200 char *tmp;
1201 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +02001202 int err_code = 0;
Maxime de Roucy0f503922016-05-13 23:52:55 +02001203 char *err_msg = NULL;
Willy Tarreau477ecd82010-01-03 21:12:30 +01001204 struct wordlist *wl;
Kevinm48936af2010-12-22 16:08:21 +00001205 char *progname;
Willy Tarreau576132e2011-09-10 19:26:56 +02001206 char *change_dir = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001207 struct proxy *px;
Willy Tarreaue6945732016-12-21 19:57:00 +01001208 struct post_check_fct *pcf;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001209
Christopher Faulete3a5e352017-10-24 13:53:54 +02001210 global.mode = MODE_STARTING;
William Lallemand73b85e72017-06-01 17:38:51 +02001211 next_argv = copy_argv(argc, argv);
1212
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001213 if (!init_trash_buffers(1)) {
Christopher Faulet748919a2017-07-26 14:59:46 +02001214 Alert("failed to initialize trash buffers.\n");
1215 exit(1);
1216 }
David du Colombier7af46052012-05-16 14:16:48 +02001217
Emeric Brun2b920a12010-09-23 18:30:22 +02001218 /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
1219 * the string in case of truncation, and at least FreeBSD appears not to do
1220 * it.
1221 */
1222 memset(hostname, 0, sizeof(hostname));
1223 gethostname(hostname, sizeof(hostname) - 1);
1224 memset(localpeer, 0, sizeof(localpeer));
1225 memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1);
1226
Willy Tarreaubaaee002006-06-26 02:48:02 +02001227 /*
1228 * Initialize the previously static variables.
1229 */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001230
Willy Tarreau3eba98a2009-01-25 13:56:13 +01001231 totalconn = actconn = maxfd = listeners = stopping = 0;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001232 killed = 0;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001233
Willy Tarreaubaaee002006-06-26 02:48:02 +02001234
1235#ifdef HAPROXY_MEMMAX
Willy Tarreau70060452015-12-14 12:46:07 +01001236 global.rlimit_memmax_all = HAPROXY_MEMMAX;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001237#endif
1238
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001239 tzset();
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001240 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001241 start_date = now;
1242
Willy Tarreau84310e22014-02-14 11:59:04 +01001243 srandom(now_ms - getpid());
1244
Dragan Dosen835b9212016-02-12 13:23:03 +01001245 init_log();
Willy Tarreau29857942009-05-10 09:01:21 +02001246 signal_init();
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001247 if (init_acl() != 0)
1248 exit(1);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02001249 init_task();
Willy Tarreau87b09662015-04-03 00:22:06 +02001250 init_stream();
Willy Tarreaub1ec8c42015-04-03 13:53:24 +02001251 init_session();
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001252 init_connection();
Willy Tarreau8280d642009-09-23 23:37:52 +02001253 /* warning, we init buffers later */
Willy Tarreaue4d7e552007-05-13 20:19:55 +02001254 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +01001255 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001256
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001257 /* Initialise lua. */
1258 hlua_init();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001259
Christopher Fauletff2613e2016-11-09 11:36:17 +01001260 /* Initialize process vars */
1261 vars_init(&global.vars, SCOPE_PROC);
1262
Willy Tarreau43b78992009-01-25 15:42:27 +01001263 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001264#if defined(ENABLE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001265 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001266#endif
1267#if defined(ENABLE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +01001268 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001269#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001270#if defined(ENABLE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +01001271 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001272#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001273#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001274 global.tune.options |= GTUNE_USE_SPLICE;
1275#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001276#if defined(USE_GETADDRINFO)
1277 global.tune.options |= GTUNE_USE_GAI;
1278#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001279#if defined(SO_REUSEPORT)
1280 global.tune.options |= GTUNE_USE_REUSEPORT;
1281#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001282
1283 pid = getpid();
1284 progname = *argv;
1285 while ((tmp = strchr(progname, '/')) != NULL)
1286 progname = tmp + 1;
1287
Kevinm48936af2010-12-22 16:08:21 +00001288 /* the process name is used for the logs only */
Dragan Dosen43885c72015-10-01 13:18:13 +02001289 chunk_initstr(&global.log_tag, strdup(progname));
Kevinm48936af2010-12-22 16:08:21 +00001290
Willy Tarreaubaaee002006-06-26 02:48:02 +02001291 argc--; argv++;
1292 while (argc > 0) {
1293 char *flag;
1294
1295 if (**argv == '-') {
1296 flag = *argv+1;
1297
1298 /* 1 arg */
1299 if (*flag == 'v') {
1300 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +01001301 if (flag[1] == 'v') /* -vv */
1302 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001303 exit(0);
1304 }
1305#if defined(ENABLE_EPOLL)
1306 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +01001307 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001308#endif
1309#if defined(ENABLE_POLL)
1310 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +01001311 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001312#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +02001313#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001314 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +01001315 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001316#endif
Willy Tarreaub55932d2009-08-16 13:20:32 +02001317#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001318 else if (*flag == 'd' && flag[1] == 'S')
1319 global.tune.options &= ~GTUNE_USE_SPLICE;
1320#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +02001321#if defined(USE_GETADDRINFO)
1322 else if (*flag == 'd' && flag[1] == 'G')
1323 global.tune.options &= ~GTUNE_USE_GAI;
1324#endif
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +00001325#if defined(SO_REUSEPORT)
1326 else if (*flag == 'd' && flag[1] == 'R')
1327 global.tune.options &= ~GTUNE_USE_REUSEPORT;
1328#endif
Emeric Brun850efd52014-01-29 12:24:34 +01001329 else if (*flag == 'd' && flag[1] == 'V')
1330 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001331 else if (*flag == 'V')
1332 arg_mode |= MODE_VERBOSE;
1333 else if (*flag == 'd' && flag[1] == 'b')
1334 arg_mode |= MODE_FOREGROUND;
Willy Tarreau6e064432012-05-08 15:40:42 +02001335 else if (*flag == 'd' && flag[1] == 'M')
1336 mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P';
Willy Tarreau3eed10e2016-11-07 21:03:16 +01001337 else if (*flag == 'd' && flag[1] == 'r')
1338 global.tune.options |= GTUNE_RESOLVE_DONTFAIL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001339 else if (*flag == 'd')
1340 arg_mode |= MODE_DEBUG;
1341 else if (*flag == 'c')
1342 arg_mode |= MODE_CHECK;
William Lallemand095ba4c2017-06-01 17:38:50 +02001343 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +02001344 arg_mode |= MODE_DAEMON;
William Lallemand095ba4c2017-06-01 17:38:50 +02001345 else if (*flag == 'W')
1346 arg_mode |= MODE_MWORKER;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001347 else if (*flag == 'q')
1348 arg_mode |= MODE_QUIET;
Olivier Houchardf73629d2017-04-05 22:33:04 +02001349 else if (*flag == 'x') {
William Lallemand45eff442017-06-19 15:57:55 +02001350 if (argc <= 1 || argv[1][0] == '-') {
1351 Alert("Unix socket path expected with the -x flag\n\n");
1352 usage(progname);
Olivier Houchardf73629d2017-04-05 22:33:04 +02001353 }
William Lallemand4fc09692017-06-19 16:37:19 +02001354 if (old_unixsocket)
1355 Warning("-x option already set, overwriting the value\n");
Olivier Houchardf73629d2017-04-05 22:33:04 +02001356 old_unixsocket = argv[1];
William Lallemand4fc09692017-06-19 16:37:19 +02001357
Olivier Houchardf73629d2017-04-05 22:33:04 +02001358 argv++;
1359 argc--;
1360 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001361 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
1362 /* list of pids to finish ('f') or terminate ('t') */
1363
1364 if (flag[1] == 'f')
1365 oldpids_sig = SIGUSR1; /* finish then exit */
1366 else
1367 oldpids_sig = SIGTERM; /* terminate immediately */
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001368 while (argc > 1 && argv[1][0] != '-') {
1369 oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int));
1370 if (!oldpids) {
1371 Alert("Cannot allocate old pid : out of memory.\n");
1372 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001373 }
Willy Tarreauc6ca1aa2015-10-08 11:32:32 +02001374 argc--; argv++;
1375 oldpids[nb_oldpids] = atol(*argv);
1376 if (oldpids[nb_oldpids] <= 0)
1377 usage(progname);
1378 nb_oldpids++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001379 }
1380 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001381 else if (flag[0] == '-' && flag[1] == 0) { /* "--" */
1382 /* now that's a cfgfile list */
1383 argv++; argc--;
1384 while (argc > 0) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02001385 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
1386 Alert("Cannot load configuration file/directory %s : %s\n",
1387 *argv,
1388 err_msg);
Willy Tarreaua088d312015-10-08 11:58:48 +02001389 exit(1);
1390 }
Willy Tarreaua088d312015-10-08 11:58:48 +02001391 argv++; argc--;
1392 }
1393 break;
1394 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001395 else { /* >=2 args */
1396 argv++; argc--;
1397 if (argc == 0)
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001398 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001399
1400 switch (*flag) {
Willy Tarreau576132e2011-09-10 19:26:56 +02001401 case 'C' : change_dir = *argv; break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001402 case 'n' : cfg_maxconn = atol(*argv); break;
Willy Tarreau70060452015-12-14 12:46:07 +01001403 case 'm' : global.rlimit_memmax_all = atol(*argv); break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001404 case 'N' : cfg_maxpconn = atol(*argv); break;
Emeric Brun2b920a12010-09-23 18:30:22 +02001405 case 'L' : strncpy(localpeer, *argv, sizeof(localpeer) - 1); break;
Willy Tarreau5d01a632009-06-22 16:02:30 +02001406 case 'f' :
Maxime de Roucy0f503922016-05-13 23:52:55 +02001407 if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {
1408 Alert("Cannot load configuration file/directory %s : %s\n",
1409 *argv,
1410 err_msg);
Willy Tarreau5d01a632009-06-22 16:02:30 +02001411 exit(1);
1412 }
Willy Tarreau5d01a632009-06-22 16:02:30 +02001413 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001414 case 'p' : cfg_pidfile = *argv; break;
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001415 default: usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001416 }
1417 }
1418 }
1419 else
Willy Tarreau3bafcdc2011-09-10 19:20:23 +02001420 usage(progname);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001421 argv++; argc--;
1422 }
1423
Christopher Faulete3a5e352017-10-24 13:53:54 +02001424 global.mode |= (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
1425 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001426
William Lallemandcb11fd22017-06-01 17:38:52 +02001427 /* Master workers wait mode */
1428 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_WAIT_ONLY") != NULL)) {
1429
1430 unsetenv("HAPROXY_MWORKER_WAIT_ONLY");
1431 mworker_wait();
1432 }
1433
1434 if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {
1435 atexit_flag = 1;
1436 atexit(reexec_on_failure);
1437 }
1438
Willy Tarreau576132e2011-09-10 19:26:56 +02001439 if (change_dir && chdir(change_dir) < 0) {
1440 Alert("Could not change to directory %s : %s\n", change_dir, strerror(errno));
1441 exit(1);
1442 }
1443
Maxime de Roucy379d9c72016-05-13 23:52:56 +02001444 /* handle cfgfiles that are actualy directories */
1445 cfgfiles_expand_directories();
1446
1447 if (LIST_ISEMPTY(&cfg_cfgfiles))
1448 usage(progname);
1449
Willy Tarreaubaaee002006-06-26 02:48:02 +02001450 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001451
1452 init_default_instance();
1453
Willy Tarreau477ecd82010-01-03 21:12:30 +01001454 list_for_each_entry(wl, &cfg_cfgfiles, list) {
Willy Tarreauc4382422009-12-06 13:10:44 +01001455 int ret;
1456
Willy Tarreau477ecd82010-01-03 21:12:30 +01001457 ret = readcfgfile(wl->s);
Willy Tarreauc4382422009-12-06 13:10:44 +01001458 if (ret == -1) {
1459 Alert("Could not open configuration file %s : %s\n",
Willy Tarreau477ecd82010-01-03 21:12:30 +01001460 wl->s, strerror(errno));
Willy Tarreauc4382422009-12-06 13:10:44 +01001461 exit(1);
1462 }
Willy Tarreau25a67fa2009-12-15 21:46:25 +01001463 if (ret & (ERR_ABORT|ERR_FATAL))
Willy Tarreau477ecd82010-01-03 21:12:30 +01001464 Alert("Error(s) found in configuration file : %s\n", wl->s);
Willy Tarreau25a67fa2009-12-15 21:46:25 +01001465 err_code |= ret;
Willy Tarreau058e9072009-07-20 09:30:05 +02001466 if (err_code & ERR_ABORT)
Willy Tarreau5d01a632009-06-22 16:02:30 +02001467 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001468 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001469
Willy Tarreaub83dc3d2017-04-19 11:24:07 +02001470 /* do not try to resolve arguments nor to spot inconsistencies when
1471 * the configuration contains fatal errors caused by files not found
1472 * or failed memory allocations.
1473 */
1474 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1475 Alert("Fatal errors found in configuration.\n");
1476 exit(1);
1477 }
1478
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001479 pattern_finalize_config();
1480
Willy Tarreaubb925012009-07-23 13:36:36 +02001481 err_code |= check_config_validity();
1482 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1483 Alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001484 exit(1);
1485 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001486
Willy Tarreau70060452015-12-14 12:46:07 +01001487 /* recompute the amount of per-process memory depending on nbproc and
1488 * the shared SSL cache size (allowed to exist in all processes).
1489 */
1490 if (global.rlimit_memmax_all) {
1491#if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE)
1492 int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL;
1493
1494 global.rlimit_memmax =
1495 ((((int64_t)global.rlimit_memmax_all * 1048576LL) -
1496 ssl_cache_bytes) / global.nbproc +
1497 ssl_cache_bytes + 1048575LL) / 1048576LL;
1498#else
1499 global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc;
1500#endif
1501 }
1502
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001503#ifdef CONFIG_HAP_NS
1504 err_code |= netns_init();
1505 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1506 Alert("Failed to initialize namespace support.\n");
1507 exit(1);
1508 }
1509#endif
1510
Baptiste Assmann4215d7d2016-11-02 15:33:15 +01001511 /* Apply server states */
1512 apply_server_state();
1513
1514 for (px = proxy; px; px = px->next)
1515 srv_compute_all_admin_states(px);
1516
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001517 /* Apply servers' configured address */
1518 err_code |= srv_init_addr();
1519 if (err_code & (ERR_ABORT|ERR_FATAL)) {
1520 Alert("Failed to initialize server(s) addr.\n");
1521 exit(1);
1522 }
1523
Willy Tarreaubaaee002006-06-26 02:48:02 +02001524 if (global.mode & MODE_CHECK) {
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001525 struct peers *pr;
1526 struct proxy *px;
1527
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001528 for (pr = cfg_peers; pr; pr = pr->next)
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001529 if (pr->peers_fe)
1530 break;
1531
1532 for (px = proxy; px; px = px->next)
Willy Tarreau4348fad2012-09-20 16:48:07 +02001533 if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners))
Willy Tarreau8b15ba12012-02-02 17:48:18 +01001534 break;
1535
1536 if (pr || px) {
1537 /* At least one peer or one listener has been found */
1538 qfprintf(stdout, "Configuration file is valid\n");
1539 exit(0);
1540 }
1541 qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n");
1542 exit(2);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001543 }
1544
Emeric Brunc60def82017-09-27 14:59:38 +02001545 global_listener_queue_task = task_new(MAX_THREADS_MASK);
Willy Tarreaue9b26022011-08-01 20:57:55 +02001546 if (!global_listener_queue_task) {
1547 Alert("Out of memory when initializing global task\n");
1548 exit(1);
1549 }
1550 /* very simple initialization, users will queue the task if needed */
1551 global_listener_queue_task->context = NULL; /* not even a context! */
1552 global_listener_queue_task->process = manage_global_listener_queue;
Willy Tarreaue9b26022011-08-01 20:57:55 +02001553
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001554 /* now we know the buffer size, we can initialize the channels and buffers */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001555 init_buffer();
Willy Tarreau8280d642009-09-23 23:37:52 +02001556
Willy Tarreaue6945732016-12-21 19:57:00 +01001557 list_for_each_entry(pcf, &post_check_list, list) {
1558 err_code |= pcf->fct();
1559 if (err_code & (ERR_ABORT|ERR_FATAL))
1560 exit(1);
1561 }
1562
Willy Tarreaubaaee002006-06-26 02:48:02 +02001563 if (cfg_maxconn > 0)
1564 global.maxconn = cfg_maxconn;
1565
1566 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +02001567 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001568 global.pidfile = strdup(cfg_pidfile);
1569 }
1570
Willy Tarreaud0256482015-01-15 21:45:22 +01001571 /* Now we want to compute the maxconn and possibly maxsslconn values.
1572 * It's a bit tricky. If memmax is not set, maxconn defaults to
1573 * DEFAULT_MAXCONN and maxsslconn defaults to DEFAULT_MAXSSLCONN.
1574 *
1575 * If memmax is set, then it depends on which values are set. If
1576 * maxsslconn is set, we use memmax to determine how many cleartext
1577 * connections may be added, and set maxconn to the sum of the two.
1578 * If maxconn is set and not maxsslconn, maxsslconn is computed from
1579 * the remaining amount of memory between memmax and the cleartext
1580 * connections. If neither are set, then it is considered that all
1581 * connections are SSL-capable, and maxconn is computed based on this,
1582 * then maxsslconn accordingly. We need to know if SSL is used on the
1583 * frontends, backends, or both, because when it's used on both sides,
1584 * we need twice the value for maxsslconn, but we only count the
1585 * handshake once since it is not performed on the two sides at the
1586 * same time (frontend-side is terminated before backend-side begins).
1587 * The SSL stack is supposed to have filled ssl_session_cost and
Willy Tarreau474b96a2015-01-28 19:03:21 +01001588 * ssl_handshake_cost during its initialization. In any case, if
1589 * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for
1590 * maxconn in order to protect the system.
Willy Tarreaud0256482015-01-15 21:45:22 +01001591 */
1592 if (!global.rlimit_memmax) {
1593 if (global.maxconn == 0) {
1594 global.maxconn = DEFAULT_MAXCONN;
1595 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1596 fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn);
1597 }
1598 }
1599#ifdef USE_OPENSSL
1600 else if (!global.maxconn && !global.maxsslconn &&
1601 (global.ssl_used_frontend || global.ssl_used_backend)) {
1602 /* memmax is set, compute everything automatically. Here we want
1603 * to ensure that all SSL connections will be served. We take
1604 * care of the number of sides where SSL is used, and consider
1605 * the worst case : SSL used on both sides and doing a handshake
1606 * simultaneously. Note that we can't have more than maxconn
1607 * handshakes at a time by definition, so for the worst case of
1608 * two SSL conns per connection, we count a single handshake.
1609 */
1610 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1611 int64_t mem = global.rlimit_memmax * 1048576ULL;
1612
1613 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1614 mem -= global.maxzlibmem;
1615 mem = mem * MEM_USABLE_RATIO;
1616
1617 global.maxconn = mem /
Willy Tarreau87b09662015-04-03 00:22:06 +02001618 ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream
Willy Tarreaud0256482015-01-15 21:45:22 +01001619 sides * global.ssl_session_max_cost + // SSL buffers, one per side
1620 global.ssl_handshake_max_cost); // 1 handshake per connection max
1621
1622 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001623#ifdef SYSTEM_MAXCONN
1624 if (global.maxconn > DEFAULT_MAXCONN)
1625 global.maxconn = DEFAULT_MAXCONN;
1626#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001627 global.maxsslconn = sides * global.maxconn;
1628 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1629 fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n",
1630 global.maxconn, global.maxsslconn);
1631 }
1632 else if (!global.maxsslconn &&
1633 (global.ssl_used_frontend || global.ssl_used_backend)) {
1634 /* memmax and maxconn are known, compute maxsslconn automatically.
1635 * maxsslconn being forced, we don't know how many of it will be
1636 * on each side if both sides are being used. The worst case is
1637 * when all connections use only one SSL instance because
1638 * handshakes may be on two sides at the same time.
1639 */
1640 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1641 int64_t mem = global.rlimit_memmax * 1048576ULL;
1642 int64_t sslmem;
1643
1644 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1645 mem -= global.maxzlibmem;
1646 mem = mem * MEM_USABLE_RATIO;
1647
Willy Tarreau87b09662015-04-03 00:22:06 +02001648 sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001649 global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost);
1650 global.maxsslconn = round_2dig(global.maxsslconn);
1651
1652 if (sslmem <= 0 || global.maxsslconn < sides) {
1653 Alert("Cannot compute the automatic maxsslconn because global.maxconn is already too "
1654 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1655 "without SSL is %d, but %d was found and SSL is in use.\n",
1656 global.rlimit_memmax,
Willy Tarreau87b09662015-04-03 00:22:06 +02001657 (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)),
Willy Tarreaud0256482015-01-15 21:45:22 +01001658 global.maxconn);
1659 exit(1);
1660 }
1661
1662 if (global.maxsslconn > sides * global.maxconn)
1663 global.maxsslconn = sides * global.maxconn;
1664
1665 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
1666 fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn);
1667 }
1668#endif
1669 else if (!global.maxconn) {
1670 /* memmax and maxsslconn are known/unused, compute maxconn automatically */
1671 int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend;
1672 int64_t mem = global.rlimit_memmax * 1048576ULL;
1673 int64_t clearmem;
1674
1675 if (global.ssl_used_frontend || global.ssl_used_backend)
1676 mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry
1677
1678 mem -= global.maxzlibmem;
1679 mem = mem * MEM_USABLE_RATIO;
1680
1681 clearmem = mem;
1682 if (sides)
1683 clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn;
1684
Willy Tarreau87b09662015-04-03 00:22:06 +02001685 global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize);
Willy Tarreaud0256482015-01-15 21:45:22 +01001686 global.maxconn = round_2dig(global.maxconn);
Willy Tarreau474b96a2015-01-28 19:03:21 +01001687#ifdef SYSTEM_MAXCONN
1688 if (global.maxconn > DEFAULT_MAXCONN)
1689 global.maxconn = DEFAULT_MAXCONN;
1690#endif /* SYSTEM_MAXCONN */
Willy Tarreaud0256482015-01-15 21:45:22 +01001691
1692 if (clearmem <= 0 || !global.maxconn) {
1693 Alert("Cannot compute the automatic maxconn because global.maxsslconn is already too "
1694 "high for the global.memmax value (%d MB). The absolute maximum possible value "
1695 "is %d, but %d was found.\n",
1696 global.rlimit_memmax,
1697 (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)),
1698 global.maxsslconn);
1699 exit(1);
1700 }
1701
1702 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
1703 if (sides && global.maxsslconn > sides * global.maxconn) {
1704 fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn "
1705 "to be limited to %d. Better reduce global.maxsslconn to get more "
1706 "room for extra connections.\n", global.maxsslconn, global.maxconn);
1707 }
1708 fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn);
1709 }
1710 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001711
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001712 if (!global.maxpipes) {
1713 /* maxpipes not specified. Count how many frontends and backends
1714 * may be using splicing, and bound that to maxconn.
1715 */
1716 struct proxy *cur;
1717 int nbfe = 0, nbbe = 0;
1718
1719 for (cur = proxy; cur; cur = cur->next) {
1720 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
1721 if (cur->cap & PR_CAP_FE)
1722 nbfe += cur->maxconn;
1723 if (cur->cap & PR_CAP_BE)
Willy Tarreauafb48762009-01-25 10:42:05 +01001724 nbbe += cur->fullconn ? cur->fullconn : global.maxconn;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001725 }
1726 }
1727 global.maxpipes = MAX(nbfe, nbbe);
1728 if (global.maxpipes > global.maxconn)
1729 global.maxpipes = global.maxconn;
Willy Tarreau686ac822009-01-25 14:06:58 +01001730 global.maxpipes /= 4;
Willy Tarreau66aa61f2009-01-18 21:44:07 +01001731 }
1732
1733
Willy Tarreauabacc2c2011-09-07 14:26:33 +02001734 global.hardmaxconn = global.maxconn; /* keep this max value */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001735 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
Willy Tarreau3ec79b92009-01-18 20:39:42 +01001736 global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001737
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001738 if (global.stats_fe)
1739 global.maxsock += global.stats_fe->maxconn;
1740
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001741 if (cfg_peers) {
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001742 /* peers also need to bypass global maxconn */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001743 struct peers *p = cfg_peers;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001744
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001745 for (p = cfg_peers; p; p = p->next)
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001746 if (p->peers_fe)
1747 global.maxsock += p->peers_fe->maxconn;
1748 }
1749
Willy Tarreau1db37712007-06-03 17:16:49 +02001750 if (global.tune.maxpollevents <= 0)
1751 global.tune.maxpollevents = MAX_POLL_EVENTS;
1752
Willy Tarreau6f4a82c2009-03-21 20:43:57 +01001753 if (global.tune.recv_enough == 0)
1754 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
1755
Willy Tarreau27097842015-09-28 13:53:23 +02001756 if (global.tune.maxrewrite < 0)
1757 global.tune.maxrewrite = MAXREWRITE;
1758
Willy Tarreau27a674e2009-08-17 07:23:33 +02001759 if (global.tune.maxrewrite >= global.tune.bufsize / 2)
1760 global.tune.maxrewrite = global.tune.bufsize / 2;
1761
Willy Tarreaubaaee002006-06-26 02:48:02 +02001762 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
1763 /* command line debug mode inhibits configuration mode */
William Lallemand095ba4c2017-06-01 17:38:50 +02001764 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001765 global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND));
1766 }
1767
William Lallemand095ba4c2017-06-01 17:38:50 +02001768 if (arg_mode & MODE_DAEMON) {
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001769 /* command line daemon mode inhibits foreground and debug modes mode */
1770 global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND);
William Lallemand095ba4c2017-06-01 17:38:50 +02001771 global.mode |= arg_mode & MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001772 }
Willy Tarreau772f0dd2012-10-26 16:04:28 +02001773
1774 global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001775
William Lallemand095ba4c2017-06-01 17:38:50 +02001776 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
1777 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
1778 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001779 }
1780
William Lallemand095ba4c2017-06-01 17:38:50 +02001781 if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001782 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
William Lallemandcc113822017-11-06 11:00:02 +01001783 Warning("<nbproc> is only meaningful in daemon mode or master-worker mode. Setting limit to 1 process.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001784 global.nbproc = 1;
1785 }
1786
1787 if (global.nbproc < 1)
1788 global.nbproc = 1;
1789
Christopher Fauletbe0faa22017-08-29 15:37:10 +02001790 if (global.nbthread < 1)
1791 global.nbthread = 1;
1792
Christopher Faulet3ef26392017-08-29 16:46:57 +02001793 /* Realloc trash buffers because global.tune.bufsize may have changed */
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001794 if (!init_trash_buffers(0)) {
Christopher Faulet3ef26392017-08-29 16:46:57 +02001795 Alert("failed to initialize trash buffers.\n");
1796 exit(1);
1797 }
1798
Christopher Faulet96d44832017-11-14 22:02:30 +01001799 if (!init_log_buffers()) {
1800 Alert("failed to initialize log buffers.\n");
1801 exit(1);
1802 }
1803
Willy Tarreauef1d1f82007-04-16 00:25:25 +02001804 /*
1805 * Note: we could register external pollers here.
1806 * Built-in pollers have been registered before main().
1807 */
Willy Tarreau4f60f162007-04-08 16:39:58 +02001808
Willy Tarreau43b78992009-01-25 15:42:27 +01001809 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001810 disable_poller("kqueue");
1811
Willy Tarreau43b78992009-01-25 15:42:27 +01001812 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001813 disable_poller("epoll");
1814
Willy Tarreau43b78992009-01-25 15:42:27 +01001815 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001816 disable_poller("poll");
1817
Willy Tarreau43b78992009-01-25 15:42:27 +01001818 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +02001819 disable_poller("select");
1820
1821 /* Note: we could disable any poller by name here */
1822
Christopher Fauletb3f4e142016-03-07 12:46:38 +01001823 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +02001824 list_pollers(stderr);
Christopher Fauletb3f4e142016-03-07 12:46:38 +01001825 fprintf(stderr, "\n");
1826 list_filters(stderr);
1827 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02001828
Willy Tarreau4f60f162007-04-08 16:39:58 +02001829 if (!init_pollers()) {
Willy Tarreau3fa87b12013-03-31 14:41:15 +02001830 Alert("No polling mechanism available.\n"
1831 " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n"
1832 " is too low on this platform to support maxconn and the number of listeners\n"
1833 " and servers. You should rebuild haproxy specifying your system using TARGET=\n"
1834 " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n"
Prach Pongpanichb837e682013-05-14 20:56:28 +02001835 " global maxconn setting to accommodate the system's limitation. For reference,\n"
Willy Tarreau3fa87b12013-03-31 14:41:15 +02001836 " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n"
1837 " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n"
1838 " check build settings using 'haproxy -vv'.\n\n",
1839 FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001840 exit(1);
1841 }
Willy Tarreau2ff76222007-04-09 19:29:56 +02001842 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
1843 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +02001844 }
1845
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001846 if (!global.node)
1847 global.node = strdup(hostname);
1848
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01001849 if (!hlua_post_init())
1850 exit(1);
Thomas Holmes6abded42015-05-12 16:23:58 +01001851
Maxime de Roucy0f503922016-05-13 23:52:55 +02001852 free(err_msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001853}
1854
Simon Horman6fb82592011-07-15 13:14:11 +09001855static void deinit_acl_cond(struct acl_cond *cond)
Simon Hormanac821422011-07-15 13:14:09 +09001856{
Simon Hormanac821422011-07-15 13:14:09 +09001857 struct acl_term_suite *suite, *suiteb;
1858 struct acl_term *term, *termb;
1859
Simon Horman6fb82592011-07-15 13:14:11 +09001860 if (!cond)
1861 return;
1862
1863 list_for_each_entry_safe(suite, suiteb, &cond->suites, list) {
1864 list_for_each_entry_safe(term, termb, &suite->terms, list) {
1865 LIST_DEL(&term->list);
1866 free(term);
Simon Hormanac821422011-07-15 13:14:09 +09001867 }
Simon Horman6fb82592011-07-15 13:14:11 +09001868 LIST_DEL(&suite->list);
1869 free(suite);
1870 }
1871
1872 free(cond);
1873}
1874
1875static void deinit_tcp_rules(struct list *rules)
1876{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001877 struct act_rule *trule, *truleb;
Simon Horman6fb82592011-07-15 13:14:11 +09001878
1879 list_for_each_entry_safe(trule, truleb, rules, list) {
Simon Hormanac821422011-07-15 13:14:09 +09001880 LIST_DEL(&trule->list);
Simon Horman6fb82592011-07-15 13:14:11 +09001881 deinit_acl_cond(trule->cond);
Simon Hormanac821422011-07-15 13:14:09 +09001882 free(trule);
1883 }
1884}
1885
Simon Horman6fb82592011-07-15 13:14:11 +09001886static void deinit_stick_rules(struct list *rules)
1887{
1888 struct sticking_rule *rule, *ruleb;
1889
1890 list_for_each_entry_safe(rule, ruleb, rules, list) {
1891 LIST_DEL(&rule->list);
1892 deinit_acl_cond(rule->cond);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001893 release_sample_expr(rule->expr);
Simon Horman6fb82592011-07-15 13:14:11 +09001894 free(rule);
1895 }
1896}
1897
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001898void deinit(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001899{
Willy Tarreau4d2d0982007-05-14 00:39:29 +02001900 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001901 struct cap_hdr *h,*h_next;
1902 struct server *s,*s_next;
1903 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001904 struct acl_cond *cond, *condb;
1905 struct hdr_exp *exp, *expb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001906 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001907 struct switching_rule *rule, *ruleb;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001908 struct server_rule *srule, *sruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001909 struct redirect_rule *rdr, *rdrb;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001910 struct wordlist *wl, *wlb;
Willy Tarreauf4f04122010-01-28 18:10:50 +01001911 struct cond_wordlist *cwl, *cwlb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001912 struct uri_auth *uap, *ua = NULL;
William Lallemand0f99e342011-10-12 17:50:54 +02001913 struct logsrv *log, *logb;
William Lallemand723b73a2012-02-08 16:37:49 +01001914 struct logformat_node *lf, *lfb;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001915 struct bind_conf *bind_conf, *bind_back;
Willy Tarreaucdb737e2016-12-21 18:43:10 +01001916 struct build_opts_str *bol, *bolb;
Willy Tarreau05554e62016-12-21 20:46:26 +01001917 struct post_deinit_fct *pdf;
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001918 int i;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001919
Willy Tarreau24f4efa2010-08-27 17:56:48 +02001920 deinit_signals();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001921 while (p) {
Willy Tarreau8113a5d2012-10-04 08:01:43 +02001922 free(p->conf.file);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001923 free(p->id);
1924 free(p->check_req);
1925 free(p->cookie_name);
1926 free(p->cookie_domain);
1927 free(p->url_param_name);
1928 free(p->capture_name);
1929 free(p->monitor_uri);
Simon Hormana31c7f72011-07-15 13:14:08 +09001930 free(p->rdp_cookie_name);
Willy Tarreau62a61232013-04-12 18:13:46 +02001931 if (p->conf.logformat_string != default_http_log_format &&
1932 p->conf.logformat_string != default_tcp_log_format &&
1933 p->conf.logformat_string != clf_http_log_format)
1934 free(p->conf.logformat_string);
Willy Tarreau196729e2012-05-31 19:30:26 +02001935
Willy Tarreau62a61232013-04-12 18:13:46 +02001936 free(p->conf.lfs_file);
1937 free(p->conf.uniqueid_format_string);
1938 free(p->conf.uif_file);
Godbachaf1a75d2013-10-02 17:10:11 +08001939 free(p->lbprm.map.srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001940
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001941 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
1942 free(p->conf.logformat_sd_string);
1943 free(p->conf.lfsd_file);
1944
Willy Tarreaua534fea2008-08-03 12:19:50 +02001945 for (i = 0; i < HTTP_ERR_SIZE; i++)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001946 chunk_destroy(&p->errmsg[i]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001947
Willy Tarreauf4f04122010-01-28 18:10:50 +01001948 list_for_each_entry_safe(cwl, cwlb, &p->req_add, list) {
1949 LIST_DEL(&cwl->list);
1950 free(cwl->s);
1951 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001952 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001953
Willy Tarreauf4f04122010-01-28 18:10:50 +01001954 list_for_each_entry_safe(cwl, cwlb, &p->rsp_add, list) {
1955 LIST_DEL(&cwl->list);
1956 free(cwl->s);
1957 free(cwl);
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01001958 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001959
Willy Tarreaub80c2302007-11-30 20:51:32 +01001960 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
1961 LIST_DEL(&cond->list);
1962 prune_acl_cond(cond);
1963 free(cond);
1964 }
1965
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001966 for (exp = p->req_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001967 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02001968 regex_free(exp->preg);
1969 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001970 }
1971
Willy Tarreau98d04852015-05-26 12:18:29 +02001972 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001973 expb = exp;
1974 exp = exp->next;
1975 free(expb);
1976 }
1977
1978 for (exp = p->rsp_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001979 if (exp->preg) {
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02001980 regex_free(exp->preg);
1981 free(exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001982 }
1983
Willy Tarreau98d04852015-05-26 12:18:29 +02001984 free((char *)exp->replace);
Willy Tarreau0fc45a72007-06-17 00:36:03 +02001985 expb = exp;
1986 exp = exp->next;
1987 free(expb);
1988 }
1989
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001990 /* build a list of unique uri_auths */
1991 if (!ua)
1992 ua = p->uri_auth;
1993 else {
1994 /* check if p->uri_auth is unique */
1995 for (uap = ua; uap; uap=uap->next)
1996 if (uap == p->uri_auth)
1997 break;
1998
Willy Tarreauaccc4e12008-06-24 11:14:45 +02001999 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002000 /* add it, if it is */
2001 p->uri_auth->next = ua;
2002 ua = p->uri_auth;
2003 }
2004 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +02002005
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002006 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
2007 LIST_DEL(&acl->list);
2008 prune_acl(acl);
2009 free(acl);
2010 }
2011
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002012 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
2013 LIST_DEL(&srule->list);
2014 prune_acl_cond(srule->cond);
2015 free(srule->cond);
2016 free(srule);
2017 }
2018
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002019 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
2020 LIST_DEL(&rule->list);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002021 if (rule->cond) {
2022 prune_acl_cond(rule->cond);
2023 free(rule->cond);
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +01002024 free(rule->file);
Willy Tarreauf51658d2014-04-23 01:21:56 +02002025 }
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002026 free(rule);
2027 }
2028
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002029 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
2030 LIST_DEL(&rdr->list);
Willy Tarreauf285f542010-01-03 20:03:03 +01002031 if (rdr->cond) {
2032 prune_acl_cond(rdr->cond);
2033 free(rdr->cond);
2034 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002035 free(rdr->rdr_str);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002036 list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) {
2037 LIST_DEL(&lf->list);
2038 free(lf);
2039 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002040 free(rdr);
2041 }
2042
William Lallemand0f99e342011-10-12 17:50:54 +02002043 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
2044 LIST_DEL(&log->list);
2045 free(log);
2046 }
2047
William Lallemand723b73a2012-02-08 16:37:49 +01002048 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
2049 LIST_DEL(&lf->list);
2050 free(lf);
2051 }
2052
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002053 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
2054 LIST_DEL(&lf->list);
2055 free(lf);
2056 }
2057
Simon Hormanac821422011-07-15 13:14:09 +09002058 deinit_tcp_rules(&p->tcp_req.inspect_rules);
2059 deinit_tcp_rules(&p->tcp_req.l4_rules);
2060
Simon Horman6fb82592011-07-15 13:14:11 +09002061 deinit_stick_rules(&p->storersp_rules);
2062 deinit_stick_rules(&p->sticking_rules);
2063
Willy Tarreaubaaee002006-06-26 02:48:02 +02002064 h = p->req_cap;
2065 while (h) {
2066 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002067 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +02002068 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002069 free(h);
2070 h = h_next;
2071 }/* end while(h) */
2072
2073 h = p->rsp_cap;
2074 while (h) {
2075 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002076 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +02002077 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002078 free(h);
2079 h = h_next;
2080 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002081
Willy Tarreaubaaee002006-06-26 02:48:02 +02002082 s = p->srv;
2083 while (s) {
2084 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002085
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002086 if (s->check.task) {
2087 task_delete(s->check.task);
2088 task_free(s->check.task);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002089 }
Simon Hormand60d6912013-11-25 10:46:36 +09002090 if (s->agent.task) {
2091 task_delete(s->agent.task);
2092 task_free(s->agent.task);
2093 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002094
Willy Tarreau2e993902011-10-31 11:53:20 +01002095 if (s->warmup) {
2096 task_delete(s->warmup);
2097 task_free(s->warmup);
2098 }
2099
Willy Tarreaua534fea2008-08-03 12:19:50 +02002100 free(s->id);
2101 free(s->cookie);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002102 free(s->check.bi);
2103 free(s->check.bo);
Simon Hormand60d6912013-11-25 10:46:36 +09002104 free(s->agent.bi);
2105 free(s->agent.bo);
James Brown55f9ff12015-10-21 18:19:05 -07002106 free(s->agent.send_string);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002107 free(s->hostname_dn);
Sárközi, László34c01792014-09-05 10:08:23 +02002108 free((char*)s->conf.file);
Willy Tarreau17d45382016-12-22 21:16:08 +01002109
2110 if (s->use_ssl || s->check.use_ssl) {
2111 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2112 xprt_get(XPRT_SSL)->destroy_srv(s);
2113 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002114 HA_SPIN_DESTROY(&s->lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002115 free(s);
2116 s = s_next;
2117 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002118
Willy Tarreau4348fad2012-09-20 16:48:07 +02002119 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002120 /*
2121 * Zombie proxy, the listener just pretend to be up
2122 * because they still hold an opened fd.
2123 * Close it and give the listener its real state.
2124 */
2125 if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
2126 close(l->fd);
2127 l->state = LI_INIT;
2128 }
Willy Tarreauf6e2cc72010-09-03 10:38:17 +02002129 unbind_listener(l);
2130 delete_listener(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002131 LIST_DEL(&l->by_fe);
2132 LIST_DEL(&l->by_bind);
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002133 free(l->name);
2134 free(l->counters);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002135 free(l);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002136 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002137
Willy Tarreau4348fad2012-09-20 16:48:07 +02002138 /* Release unused SSL configs. */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002139 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01002140 if (bind_conf->xprt->destroy_bind_conf)
2141 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002142 free(bind_conf->file);
2143 free(bind_conf->arg);
2144 LIST_DEL(&bind_conf->by_fe);
2145 free(bind_conf);
2146 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02002147
Christopher Fauletd7c91962015-04-30 11:48:27 +02002148 flt_deinit(p);
2149
Krzysztof Piotr Oledzkiaff01ea2010-02-05 20:31:44 +01002150 free(p->desc);
2151 free(p->fwdfor_hdr_name);
2152
Willy Tarreauff011f22011-01-06 17:51:27 +01002153 free_http_req_rules(&p->http_req_rules);
Sasha Pachev218f0642014-06-16 12:05:59 -06002154 free_http_res_rules(&p->http_res_rules);
Willy Tarreau918ff602011-07-25 16:33:49 +02002155 free(p->task);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002156
Willy Tarreaucf7f3202007-05-13 22:46:04 +02002157 pool_destroy2(p->req_cap_pool);
2158 pool_destroy2(p->rsp_cap_pool);
Simon Hormanb08584a2011-07-15 13:14:10 +09002159 pool_destroy2(p->table.pool);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002160
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002161 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002162 p = p->next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002163 HA_SPIN_DESTROY(&p0->lbprm.lock);
2164 HA_SPIN_DESTROY(&p0->lock);
Willy Tarreau4d2d0982007-05-14 00:39:29 +02002165 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002166 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +02002167
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002168 while (ua) {
2169 uap = ua;
2170 ua = ua->next;
2171
Willy Tarreaua534fea2008-08-03 12:19:50 +02002172 free(uap->uri_prefix);
2173 free(uap->auth_realm);
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002174 free(uap->node);
2175 free(uap->desc);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002176
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002177 userlist_free(uap->userlist);
Willy Tarreauff011f22011-01-06 17:51:27 +01002178 free_http_req_rules(&uap->http_req_rules);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002179
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002180 free(uap);
2181 }
2182
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01002183 userlist_free(userlist);
2184
David Carlier834cb2e2015-09-25 12:02:25 +01002185 cfg_unregister_sections();
2186
Christopher Faulet0132d062017-07-26 15:33:35 +02002187 deinit_log_buffers();
Christopher Faulet748919a2017-07-26 14:59:46 +02002188 deinit_trash_buffers();
David Carlier834cb2e2015-09-25 12:02:25 +01002189
Willy Tarreaudd815982007-10-16 12:25:14 +02002190 protocol_unbind_all();
2191
Willy Tarreau05554e62016-12-21 20:46:26 +01002192 list_for_each_entry(pdf, &post_deinit_list, list)
2193 pdf->fct();
2194
Joe Williamsdf5b38f2010-12-29 17:05:48 +01002195 free(global.log_send_hostname); global.log_send_hostname = NULL;
Dragan Dosen43885c72015-10-01 13:18:13 +02002196 chunk_destroy(&global.log_tag);
Willy Tarreaua534fea2008-08-03 12:19:50 +02002197 free(global.chroot); global.chroot = NULL;
2198 free(global.pidfile); global.pidfile = NULL;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002199 free(global.node); global.node = NULL;
2200 free(global.desc); global.desc = NULL;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002201 free(oldpids); oldpids = NULL;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002202 free(global_listener_queue_task); global_listener_queue_task = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02002203
William Lallemand0f99e342011-10-12 17:50:54 +02002204 list_for_each_entry_safe(log, logb, &global.logsrvs, list) {
2205 LIST_DEL(&log->list);
2206 free(log);
2207 }
Willy Tarreau477ecd82010-01-03 21:12:30 +01002208 list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) {
Maxime de Roucy0f503922016-05-13 23:52:55 +02002209 free(wl->s);
Willy Tarreau477ecd82010-01-03 21:12:30 +01002210 LIST_DEL(&wl->list);
2211 free(wl);
2212 }
2213
Willy Tarreaucdb737e2016-12-21 18:43:10 +01002214 list_for_each_entry_safe(bol, bolb, &build_opts_list, list) {
2215 if (bol->must_free)
2216 free((void *)bol->str);
2217 LIST_DEL(&bol->list);
2218 free(bol);
2219 }
2220
Christopher Fauletff2613e2016-11-09 11:36:17 +01002221 vars_prune(&global.vars, NULL, NULL);
2222
Christopher Fauletad405f12017-08-29 15:30:11 +02002223 deinit_buffer();
2224
Willy Tarreau87b09662015-04-03 00:22:06 +02002225 pool_destroy2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +02002226 pool_destroy2(pool2_session);
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002227 pool_destroy2(pool2_connection);
Olivier Houcharde2b40b92017-09-13 18:30:23 +02002228 pool_destroy2(pool2_connstream);
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002229 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +02002230 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +02002231 pool_destroy2(pool2_capture);
Willy Tarreaue4d7e552007-05-13 20:19:55 +02002232 pool_destroy2(pool2_pendconn);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002233 pool_destroy2(pool2_sig_handlers);
Willy Tarreau34eb6712011-10-24 18:15:04 +02002234 pool_destroy2(pool2_hdr_idx);
Willy Tarreau63986c72015-04-03 22:55:33 +02002235 pool_destroy2(pool2_http_txn);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02002236 deinit_pollers();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002237} /* end deinit() */
2238
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002239void mworker_pipe_handler(int fd)
2240{
2241 char c;
2242
2243 while (read(fd, &c, 1) == -1) {
2244 if (errno == EINTR)
2245 continue;
2246 if (errno == EAGAIN) {
2247 fd_cant_recv(fd);
2248 return;
2249 }
2250 break;
2251 }
2252
2253 deinit();
2254 exit(EXIT_FAILURE);
2255 return;
2256}
2257
2258void mworker_pipe_register(int pipefd[2])
2259{
2260 close(mworker_pipe[1]); /* close the write end of the master pipe in the children */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002261
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002262 fcntl(mworker_pipe[0], F_SETFL, O_NONBLOCK);
2263 fdtab[mworker_pipe[0]].owner = mworker_pipe;
2264 fdtab[mworker_pipe[0]].iocb = mworker_pipe_handler;
Christopher Faulet36716a72017-05-30 11:07:16 +02002265 fd_insert(mworker_pipe[0], MAX_THREADS_MASK);
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002266 fd_want_recv(mworker_pipe[0]);
2267}
Christopher Fauletdc628a32017-10-19 11:59:44 +02002268
2269static void sync_poll_loop()
2270{
2271 if (THREAD_NO_SYNC())
2272 return;
2273
2274 THREAD_ENTER_SYNC();
2275
2276 if (!THREAD_NEED_SYNC())
2277 goto exit;
2278
2279 /* *** { */
2280 /* Put here all sync functions */
2281
Christopher Faulet5d42e092017-10-16 12:00:40 +02002282 servers_update_status(); /* Commit server status changes */
Christopher Fauletdc628a32017-10-19 11:59:44 +02002283
2284 /* *** } */
2285 exit:
2286 THREAD_EXIT_SYNC();
2287}
2288
Willy Tarreau918ff602011-07-25 16:33:49 +02002289/* Runs the polling loop */
Willy Tarreau1b5af7c2016-12-21 18:19:57 +01002290static void run_poll_loop()
Willy Tarreau4f60f162007-04-08 16:39:58 +02002291{
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002292 int next;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002293
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002294 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +02002295 while (1) {
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002296 /* Process a few tasks */
2297 process_runnable_tasks();
2298
Willy Tarreau29857942009-05-10 09:01:21 +02002299 /* check if we caught some signals and process them */
2300 signal_process_queue();
2301
Willy Tarreau58b458d2008-06-29 22:40:23 +02002302 /* Check if we can expire some tasks */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +01002303 next = wake_expired_tasks();
Willy Tarreau4f60f162007-04-08 16:39:58 +02002304
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002305 /* stop when there's nothing left to do */
2306 if (jobs == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +02002307 break;
2308
Willy Tarreau10146c92015-04-13 20:44:19 +02002309 /* expire immediately if events are pending */
Christopher Faulet8a48f672017-11-14 10:38:36 +01002310 if (fd_cache_num || (active_tasks_mask & tid_bit) || signal_queue_len || applets_active_queue)
Willy Tarreau10146c92015-04-13 20:44:19 +02002311 next = now_ms;
2312
Willy Tarreau58b458d2008-06-29 22:40:23 +02002313 /* The poller will ensure it returns around <next> */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002314 cur_poller.poll(&cur_poller, next);
Willy Tarreau033cd9d2014-01-25 19:24:15 +01002315 fd_process_cached_events();
Willy Tarreau3c595ac2015-04-19 09:59:31 +02002316 applet_run_active();
Emeric Brun64cc49c2017-10-03 14:46:45 +02002317
Christopher Fauletdc628a32017-10-19 11:59:44 +02002318
2319 /* Synchronize all polling loops */
2320 sync_poll_loop();
2321
Willy Tarreau4f60f162007-04-08 16:39:58 +02002322 }
2323}
2324
Christopher Faulet1d17c102017-08-29 15:38:48 +02002325static void *run_thread_poll_loop(void *data)
2326{
2327 struct per_thread_init_fct *ptif;
2328 struct per_thread_deinit_fct *ptdf;
2329
2330 tid = *((unsigned int *)data);
2331 tid_bit = (1UL << tid);
2332 tv_update_date(-1,-1);
2333
2334 list_for_each_entry(ptif, &per_thread_init_list, list) {
2335 if (!ptif->fct()) {
2336 Alert("failed to initialize thread %u.\n", tid);
2337 exit(1);
2338 }
2339 }
2340
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002341 if (global.mode & MODE_MWORKER)
2342 mworker_pipe_register(mworker_pipe);
2343
2344 protocol_enable_all();
Christopher Fauletdc628a32017-10-19 11:59:44 +02002345 THREAD_SYNC_ENABLE();
Christopher Faulet1d17c102017-08-29 15:38:48 +02002346 run_poll_loop();
2347
2348 list_for_each_entry(ptdf, &per_thread_deinit_list, list)
2349 ptdf->fct();
2350
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002351#ifdef USE_THREAD
2352 if (tid > 0)
2353 pthread_exit(NULL);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002354#endif
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002355 return NULL;
2356}
Christopher Faulet1d17c102017-08-29 15:38:48 +02002357
Willy Tarreaue9b26022011-08-01 20:57:55 +02002358/* This is the global management task for listeners. It enables listeners waiting
2359 * for global resources when there are enough free resource, or at least once in
2360 * a while. It is designed to be called as a task.
2361 */
2362static struct task *manage_global_listener_queue(struct task *t)
2363{
2364 int next = TICK_ETERNITY;
Willy Tarreaue9b26022011-08-01 20:57:55 +02002365 /* queue is empty, nothing to do */
2366 if (LIST_ISEMPTY(&global_listener_queue))
2367 goto out;
2368
2369 /* If there are still too many concurrent connections, let's wait for
2370 * some of them to go away. We don't need to re-arm the timer because
2371 * each of them will scan the queue anyway.
2372 */
2373 if (unlikely(actconn >= global.maxconn))
2374 goto out;
2375
2376 /* We should periodically try to enable listeners waiting for a global
2377 * resource here, because it is possible, though very unlikely, that
2378 * they have been blocked by a temporary lack of global resource such
2379 * as a file descriptor or memory and that the temporary condition has
2380 * disappeared.
2381 */
Willy Tarreauabacc2c2011-09-07 14:26:33 +02002382 dequeue_all_listeners(&global_listener_queue);
Willy Tarreaue9b26022011-08-01 20:57:55 +02002383
2384 out:
2385 t->expire = next;
2386 task_queue(t);
2387 return t;
2388}
Willy Tarreau4f60f162007-04-08 16:39:58 +02002389
Willy Tarreaubaaee002006-06-26 02:48:02 +02002390int main(int argc, char **argv)
2391{
2392 int err, retry;
2393 struct rlimit limit;
Emeric Bruncf20bf12010-10-22 16:06:11 +02002394 char errmsg[100];
Willy Tarreau269ab312012-09-05 08:02:48 +02002395 int pidfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002396
Emeric Bruncf20bf12010-10-22 16:06:11 +02002397 init(argc, argv);
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002398 signal_register_fct(SIGQUIT, dump, SIGQUIT);
2399 signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
2400 signal_register_fct(SIGHUP, sig_dump_state, SIGHUP);
William Lallemand73b85e72017-06-01 17:38:51 +02002401 signal_register_fct(SIGUSR2, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002402
Willy Tarreaue437c442010-03-17 18:02:46 +01002403 /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL.
2404 * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL
2405 * was defined there, so let's stay on the safe side.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002406 */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002407 signal_register_fct(SIGPIPE, NULL, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002408
Willy Tarreaudc23a922011-02-16 11:10:36 +01002409 /* ulimits */
2410 if (!global.rlimit_nofile)
2411 global.rlimit_nofile = global.maxsock;
2412
2413 if (global.rlimit_nofile) {
2414 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
2415 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
Willy Tarreauef635472016-06-21 11:48:18 +02002416 /* try to set it to the max possible at least */
2417 getrlimit(RLIMIT_NOFILE, &limit);
Willy Tarreau164dd0b2016-06-21 11:51:59 +02002418 limit.rlim_cur = limit.rlim_max;
2419 if (setrlimit(RLIMIT_NOFILE, &limit) != -1)
2420 getrlimit(RLIMIT_NOFILE, &limit);
2421
Willy Tarreauef635472016-06-21 11:48:18 +02002422 Warning("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n", argv[0], global.rlimit_nofile, (int)limit.rlim_cur);
2423 global.rlimit_nofile = limit.rlim_cur;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002424 }
2425 }
2426
2427 if (global.rlimit_memmax) {
2428 limit.rlim_cur = limit.rlim_max =
Willy Tarreau70060452015-12-14 12:46:07 +01002429 global.rlimit_memmax * 1048576ULL;
Willy Tarreaudc23a922011-02-16 11:10:36 +01002430#ifdef RLIMIT_AS
2431 if (setrlimit(RLIMIT_AS, &limit) == -1) {
2432 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2433 argv[0], global.rlimit_memmax);
2434 }
2435#else
2436 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
2437 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
2438 argv[0], global.rlimit_memmax);
2439 }
2440#endif
2441 }
2442
Olivier Houchardf73629d2017-04-05 22:33:04 +02002443 if (old_unixsocket) {
William Lallemand85b0bd92017-06-01 17:38:53 +02002444 if (strcmp("/dev/null", old_unixsocket) != 0) {
2445 if (get_old_sockets(old_unixsocket) != 0) {
2446 Alert("Failed to get the sockets from the old process!\n");
2447 if (!(global.mode & MODE_MWORKER))
2448 exit(1);
2449 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002450 }
2451 }
William Lallemand85b0bd92017-06-01 17:38:53 +02002452 get_cur_unixsocket();
2453
Willy Tarreaubaaee002006-06-26 02:48:02 +02002454 /* We will loop at most 100 times with 10 ms delay each time.
2455 * That's at most 1 second. We only send a signal to old pids
2456 * if we cannot grab at least one port.
2457 */
2458 retry = MAX_START_RETRIES;
2459 err = ERR_NONE;
2460 while (retry >= 0) {
2461 struct timeval w;
2462 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +01002463 /* exit the loop on no error or fatal error */
2464 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002465 break;
Willy Tarreaubb545b42010-08-25 12:58:59 +02002466 if (nb_oldpids == 0 || retry == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002467 break;
2468
2469 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
2470 * listening sockets. So on those platforms, it would be wiser to
2471 * simply send SIGUSR1, which will not be undoable.
2472 */
Willy Tarreaubb545b42010-08-25 12:58:59 +02002473 if (tell_old_pids(SIGTTOU) == 0) {
2474 /* no need to wait if we can't contact old pids */
2475 retry = 0;
2476 continue;
2477 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002478 /* give some time to old processes to stop listening */
2479 w.tv_sec = 0;
2480 w.tv_usec = 10*1000;
2481 select(0, NULL, NULL, NULL, &w);
2482 retry--;
2483 }
2484
2485 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +01002486 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +02002487 if (retry != MAX_START_RETRIES && nb_oldpids) {
2488 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002489 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +02002490 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002491 exit(1);
2492 }
2493
2494 if (listeners == 0) {
Willy Tarreauf2ee0162015-08-09 11:01:51 +02002495 Alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002496 /* Note: we don't have to send anything to the old pids because we
2497 * never stopped them. */
2498 exit(1);
2499 }
2500
Emeric Bruncf20bf12010-10-22 16:06:11 +02002501 err = protocol_bind_all(errmsg, sizeof(errmsg));
2502 if ((err & ~ERR_WARN) != ERR_NONE) {
2503 if ((err & ERR_ALERT) || (err & ERR_WARN))
2504 Alert("[%s.main()] %s.\n", argv[0], errmsg);
2505
Willy Tarreaudd815982007-10-16 12:25:14 +02002506 Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
2507 protocol_unbind_all(); /* cleanup everything we can */
2508 if (nb_oldpids)
2509 tell_old_pids(SIGTTIN);
2510 exit(1);
Emeric Bruncf20bf12010-10-22 16:06:11 +02002511 } else if (err & ERR_WARN) {
2512 Alert("[%s.main()] %s.\n", argv[0], errmsg);
Willy Tarreaudd815982007-10-16 12:25:14 +02002513 }
Olivier Houchardf73629d2017-04-05 22:33:04 +02002514 /* Ok, all listener should now be bound, close any leftover sockets
2515 * the previous process gave us, we don't need them anymore
2516 */
2517 while (xfer_sock_list != NULL) {
2518 struct xfer_sock_list *tmpxfer = xfer_sock_list->next;
2519 close(xfer_sock_list->fd);
2520 free(xfer_sock_list->iface);
2521 free(xfer_sock_list->namespace);
2522 free(xfer_sock_list);
2523 xfer_sock_list = tmpxfer;
2524 }
Willy Tarreaudd815982007-10-16 12:25:14 +02002525
Willy Tarreaubaaee002006-06-26 02:48:02 +02002526 /* prepare pause/play signals */
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002527 signal_register_fct(SIGTTOU, sig_pause, SIGTTOU);
2528 signal_register_fct(SIGTTIN, sig_listen, SIGTTIN);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002529
Willy Tarreaubaaee002006-06-26 02:48:02 +02002530 /* MODE_QUIET can inhibit alerts and warnings below this line */
2531
Willy Tarreaubaaee002006-06-26 02:48:02 +02002532 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
2533 /* detach from the tty */
2534 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002535 }
2536
2537 /* open log & pid files before the chroot */
William Lallemand80293002017-11-06 11:00:03 +01002538 if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && global.pidfile != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002539 unlink(global.pidfile);
2540 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
2541 if (pidfd < 0) {
2542 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
2543 if (nb_oldpids)
2544 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02002545 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002546 exit(1);
2547 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002548 }
2549
Willy Tarreaub38651a2007-03-24 17:24:39 +01002550 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
2551 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002552 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002553 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01002554 exit(1);
2555 }
2556
Willy Tarreau4e30ed72009-02-04 18:02:48 +01002557 /* If the user is not root, we'll still let him try the configuration
2558 * but we inform him that unexpected behaviour may occur.
2559 */
2560 if ((global.last_checks & LSTCHK_NETADM) && getuid())
2561 Warning("[%s.main()] Some options which require full privileges"
2562 " might not work well.\n"
2563 "", argv[0]);
2564
William Lallemand095ba4c2017-06-01 17:38:50 +02002565 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2566
2567 /* chroot if needed */
2568 if (global.chroot != NULL) {
2569 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
2570 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
2571 if (nb_oldpids)
2572 tell_old_pids(SIGTTIN);
2573 protocol_unbind_all();
2574 exit(1);
2575 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002576 }
Willy Tarreauf223cc02007-10-15 18:57:08 +02002577 }
2578
Willy Tarreaubaaee002006-06-26 02:48:02 +02002579 if (nb_oldpids)
Willy Tarreaubb545b42010-08-25 12:58:59 +02002580 nb_oldpids = tell_old_pids(oldpids_sig);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002581
William Lallemand8a361b52017-06-20 11:20:33 +02002582 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) {
2583 nb_oldpids = 0;
2584 free(oldpids);
2585 oldpids = NULL;
2586 }
2587
2588
Willy Tarreaubaaee002006-06-26 02:48:02 +02002589 /* Note that any error at this stage will be fatal because we will not
2590 * be able to restart the old pids.
2591 */
2592
William Lallemand095ba4c2017-06-01 17:38:50 +02002593 if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) {
2594 /* setgid / setuid */
2595 if (global.gid) {
2596 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
2597 Warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2598 " without 'uid'/'user' is generally useless.\n", argv[0]);
2599
2600 if (setgid(global.gid) == -1) {
2601 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
2602 protocol_unbind_all();
2603 exit(1);
2604 }
2605 }
Michael Schererab012dd2013-01-12 18:35:19 +01002606
William Lallemand095ba4c2017-06-01 17:38:50 +02002607 if (global.uid && setuid(global.uid) == -1) {
2608 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Michael Schererab012dd2013-01-12 18:35:19 +01002609 protocol_unbind_all();
2610 exit(1);
2611 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002612 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002613 /* check ulimits */
2614 limit.rlim_cur = limit.rlim_max = 0;
2615 getrlimit(RLIMIT_NOFILE, &limit);
2616 if (limit.rlim_cur < global.maxsock) {
2617 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 +02002618 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002619 }
2620
William Lallemand095ba4c2017-06-01 17:38:50 +02002621 if (global.mode & (MODE_DAEMON | MODE_MWORKER)) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002622 struct proxy *px;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002623 struct peers *curpeers;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002624 int ret = 0;
2625 int proc;
2626
William Lallemand73b85e72017-06-01 17:38:51 +02002627 children = calloc(global.nbproc, sizeof(int));
William Lallemand095ba4c2017-06-01 17:38:50 +02002628 /*
2629 * if daemon + mworker: must fork here to let a master
2630 * process live in background before forking children
2631 */
William Lallemand73b85e72017-06-01 17:38:51 +02002632
2633 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
2634 && (global.mode & MODE_MWORKER)
2635 && (global.mode & MODE_DAEMON)) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002636 ret = fork();
2637 if (ret < 0) {
2638 Alert("[%s.main()] Cannot fork.\n", argv[0]);
2639 protocol_unbind_all();
2640 exit(1); /* there has been an error */
2641 }
2642 /* parent leave to daemonize */
2643 if (ret > 0)
2644 exit(0);
2645 }
William Lallemande20b6a62017-06-01 17:38:55 +02002646
2647 if (global.mode & MODE_MWORKER) {
2648 if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) {
2649 char *msg = NULL;
2650 /* master pipe to ensure the master is still alive */
2651 ret = pipe(mworker_pipe);
2652 if (ret < 0) {
2653 Warning("[%s.main()] Cannot create master pipe.\n", argv[0]);
2654 } else {
2655 memprintf(&msg, "%d", mworker_pipe[0]);
2656 setenv("HAPROXY_MWORKER_PIPE_RD", msg, 1);
2657 memprintf(&msg, "%d", mworker_pipe[1]);
2658 setenv("HAPROXY_MWORKER_PIPE_WR", msg, 1);
2659 free(msg);
2660 }
2661 } else {
2662 mworker_pipe[0] = atol(getenv("HAPROXY_MWORKER_PIPE_RD"));
2663 mworker_pipe[1] = atol(getenv("HAPROXY_MWORKER_PIPE_WR"));
2664 if (mworker_pipe[0] <= 0 || mworker_pipe[1] <= 0) {
2665 Warning("[%s.main()] Cannot get master pipe FDs.\n", argv[0]);
2666 }
2667 }
2668 }
2669
William Lallemanddeed7802017-11-06 11:00:04 +01002670 /* if in master-worker mode, write the PID of the father */
2671 if (global.mode & MODE_MWORKER) {
2672 char pidstr[100];
2673 snprintf(pidstr, sizeof(pidstr), "%d\n", getpid());
2674 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
2675 }
2676
Willy Tarreaubaaee002006-06-26 02:48:02 +02002677 /* the father launches the required number of processes */
2678 for (proc = 0; proc < global.nbproc; proc++) {
2679 ret = fork();
2680 if (ret < 0) {
2681 Alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02002682 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +02002683 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002684 }
2685 else if (ret == 0) /* child breaks here */
2686 break;
Marc-Antoine Perennou992709b2013-02-12 10:53:52 +01002687 children[proc] = ret;
William Lallemand92159b22017-11-06 11:16:12 +01002688 if (pidfd >= 0 && !(global.mode & MODE_MWORKER)) {
Willy Tarreau269ab312012-09-05 08:02:48 +02002689 char pidstr[100];
2690 snprintf(pidstr, sizeof(pidstr), "%d\n", ret);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002691 shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002692 }
Willy Tarreaudcd47712007-11-04 23:35:08 +01002693 relative_pid++; /* each child will get a different one */
Willy Tarreau387bd4f2017-11-10 19:08:14 +01002694 pid_bit <<= 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002695 }
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002696
2697#ifdef USE_CPU_AFFINITY
2698 if (proc < global.nbproc && /* child */
Willy Tarreaue7597492015-04-20 11:36:57 +02002699 proc < LONGBITS && /* only the first 32/64 processes may be pinned */
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002700 global.cpu_map[proc]) /* only do this if the process has a CPU map */
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002701#ifdef __FreeBSD__
Olivier Houchard97148f62017-08-16 17:29:11 +02002702 {
2703 cpuset_t cpuset;
2704 int i;
2705 unsigned long cpu_map = global.cpu_map[proc];
2706
2707 CPU_ZERO(&cpuset);
2708 while ((i = ffsl(cpu_map)) > 0) {
2709 CPU_SET(i - 1, &cpuset);
2710 cpu_map &= ~(1 << (i - 1));
2711 }
2712 ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset);
2713 }
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002714#else
Willy Tarreaufc6c0322012-11-16 16:12:27 +01002715 sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map[proc]);
2716#endif
Pieter Baauwcaa6a1b2015-09-17 21:26:40 +02002717#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002718 /* close the pidfile both in children and father */
Willy Tarreau269ab312012-09-05 08:02:48 +02002719 if (pidfd >= 0) {
2720 //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */
2721 close(pidfd);
2722 }
Willy Tarreaud137dd32010-08-25 12:49:05 +02002723
2724 /* We won't ever use this anymore */
Willy Tarreaud137dd32010-08-25 12:49:05 +02002725 free(global.pidfile); global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002726
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002727 if (proc == global.nbproc) {
William Lallemand095ba4c2017-06-01 17:38:50 +02002728 if (global.mode & MODE_MWORKER) {
William Lallemand75ea0a02017-11-15 19:02:58 +01002729 mworker_cleanlisteners();
2730 deinit_pollers();
William Lallemand73b85e72017-06-01 17:38:51 +02002731 mworker_wait();
William Lallemand1499b9b2017-06-07 15:04:47 +02002732 /* should never get there */
2733 exit(EXIT_FAILURE);
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002734 }
William Lallemandcf4e4962017-06-08 19:05:48 +02002735#if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH)
Grant Zhang872f9c22017-01-21 01:10:18 +00002736 ssl_free_dh();
2737#endif
William Lallemand1499b9b2017-06-07 15:04:47 +02002738 exit(0); /* parent must leave */
Willy Tarreauedaff0a2015-05-01 17:01:08 +02002739 }
2740
William Lallemandcb11fd22017-06-01 17:38:52 +02002741 /* child must never use the atexit function */
2742 atexit_flag = 0;
2743
William Lallemand095ba4c2017-06-01 17:38:50 +02002744 /* Must chroot and setgid/setuid in the children */
2745 /* chroot if needed */
2746 if (global.chroot != NULL) {
2747 if (chroot(global.chroot) == -1 || chdir("/") == -1) {
2748 Alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot);
2749 if (nb_oldpids)
2750 tell_old_pids(SIGTTIN);
2751 protocol_unbind_all();
2752 exit(1);
2753 }
2754 }
2755
2756 free(global.chroot);
2757 global.chroot = NULL;
2758
2759 /* setgid / setuid */
2760 if (global.gid) {
2761 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
2762 Warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
2763 " without 'uid'/'user' is generally useless.\n", argv[0]);
2764
2765 if (setgid(global.gid) == -1) {
2766 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
2767 protocol_unbind_all();
2768 exit(1);
2769 }
2770 }
2771
2772 if (global.uid && setuid(global.uid) == -1) {
2773 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
2774 protocol_unbind_all();
2775 exit(1);
2776 }
2777
William Lallemand7f80eb22017-05-26 18:19:55 +02002778 /* pass through every cli socket, and check if it's bound to
2779 * the current process and if it exposes listeners sockets.
2780 * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork.
2781 * */
2782
2783 if (global.stats_fe) {
2784 struct bind_conf *bind_conf;
2785
2786 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
2787 if (bind_conf->level & ACCESS_FD_LISTENERS) {
2788 if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) {
2789 global.tune.options |= GTUNE_SOCKET_TRANSFER;
2790 break;
2791 }
2792 }
2793 }
2794 }
2795
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002796 /* we might have to unbind some proxies from some processes */
2797 px = proxy;
2798 while (px != NULL) {
2799 if (px->bind_proc && px->state != PR_STSTOPPED) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02002800 if (!(px->bind_proc & (1UL << proc))) {
2801 if (global.tune.options & GTUNE_SOCKET_TRANSFER)
2802 zombify_proxy(px);
2803 else
2804 stop_proxy(px);
2805 }
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01002806 }
2807 px = px->next;
2808 }
2809
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002810 /* we might have to unbind some peers sections from some processes */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002811 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002812 if (!curpeers->peers_fe)
2813 continue;
2814
2815 if (curpeers->peers_fe->bind_proc & (1UL << proc))
2816 continue;
2817
2818 stop_proxy(curpeers->peers_fe);
2819 /* disable this peer section so that it kills itself */
Willy Tarreau47c8c022015-09-28 16:39:25 +02002820 signal_unregister_handler(curpeers->sighandler);
2821 task_delete(curpeers->sync_task);
2822 task_free(curpeers->sync_task);
2823 curpeers->sync_task = NULL;
2824 task_free(curpeers->peers_fe->task);
2825 curpeers->peers_fe->task = NULL;
Willy Tarreauf83d3fe2015-05-01 19:13:41 +02002826 curpeers->peers_fe = NULL;
2827 }
2828
Willy Tarreaubaaee002006-06-26 02:48:02 +02002829 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
2830 * that we can detach from the TTY. We MUST NOT do it in other cases since
2831 * it would have already be done, and 0-2 would have been affected to listening
2832 * sockets
2833 */
Willy Tarreau106cb762008-11-16 07:40:34 +01002834 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002835 /* detach from the tty */
2836 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreau106cb762008-11-16 07:40:34 +01002837 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002838 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
2839 }
2840 pid = getpid(); /* update child's pid */
2841 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02002842 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02002843 }
2844
Christopher Faulete3a5e352017-10-24 13:53:54 +02002845 global.mode &= ~MODE_STARTING;
Willy Tarreau4f60f162007-04-08 16:39:58 +02002846 /*
2847 * That's it : the central polling loop. Run until we stop.
2848 */
Christopher Faulet1d17c102017-08-29 15:38:48 +02002849#ifdef USE_THREAD
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002850 {
Christopher Faulet1d17c102017-08-29 15:38:48 +02002851 unsigned int *tids = calloc(global.nbthread, sizeof(unsigned int));
2852 pthread_t *threads = calloc(global.nbthread, sizeof(pthread_t));
2853 int i;
2854
Christopher Fauletd7bddda2017-10-31 17:30:12 +01002855 THREAD_SYNC_INIT((1UL << global.nbthread) - 1);
2856
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002857 /* Init tids array */
2858 for (i = 0; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02002859 tids[i] = i;
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002860
2861 /* Create nbthread-1 thread. The first thread is the current process */
2862 threads[0] = pthread_self();
2863 for (i = 1; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02002864 pthread_create(&threads[i], NULL, &run_thread_poll_loop, &tids[i]);
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002865
Christopher Faulet62519022017-10-16 15:49:32 +02002866#ifdef USE_CPU_AFFINITY
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002867 /* Now the CPU affinity for all threads */
2868 for (i = 0; i < global.nbthread; i++) {
Christopher Faulet62519022017-10-16 15:49:32 +02002869 if (global.cpu_map[relative_pid-1])
2870 global.thread_map[relative_pid-1][i] &= global.cpu_map[relative_pid-1];
2871
2872 if (i < LONGBITS && /* only the first 32/64 threads may be pinned */
2873 global.thread_map[relative_pid-1][i]) /* only do this if the thread has a THREAD map */
2874 pthread_setaffinity_np(threads[i],
2875 sizeof(unsigned long), (void *)&global.thread_map[relative_pid-1][i]);
Christopher Faulet1d17c102017-08-29 15:38:48 +02002876 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002877#endif /* !USE_CPU_AFFINITY */
2878
2879 /* Finally, start the poll loop for the first thread */
2880 run_thread_poll_loop(&tids[0]);
2881
2882 /* Wait the end of other threads */
2883 for (i = 1; i < global.nbthread; i++)
Christopher Faulet1d17c102017-08-29 15:38:48 +02002884 pthread_join(threads[i], NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002885
Christopher Faulet1d17c102017-08-29 15:38:48 +02002886 free(tids);
2887 free(threads);
2888
Christopher Fauletb79a94c2017-05-30 15:34:30 +02002889#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
2890 show_lock_stats();
2891#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02002892 }
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002893#else /* ! USE_THREAD */
Christopher Fauletd4604ad2017-05-29 10:40:41 +02002894
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002895 run_thread_poll_loop((int []){0});
Christopher Faulet62519022017-10-16 15:49:32 +02002896
Christopher Faulet62519022017-10-16 15:49:32 +02002897#endif
Christopher Faulet1d17c102017-08-29 15:38:48 +02002898
2899 /* Do some cleanup */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002900 deinit();
Christopher Faulet1d17c102017-08-29 15:38:48 +02002901
Willy Tarreaubaaee002006-06-26 02:48:02 +02002902 exit(0);
2903}
2904
2905
2906/*
2907 * Local variables:
2908 * c-indent-level: 8
2909 * c-basic-offset: 8
2910 * End:
2911 */