Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HA-Proxy : High Availability-enabled HTTP/TCP proxy |
Willy Tarreau | 7b67726 | 2017-04-03 09:27:49 +0200 | [diff] [blame] | 3 | * Copyright 2000-2017 Willy Tarreau <willy@haproxy.org>. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4 | * |
| 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 Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 10 | * Please refer to RFC7230 - RFC7235 informations about HTTP protocol, and |
| 11 | * RFC6265 for informations about cookies usage. More generally, the IETF HTTP |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 12 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | */ |
| 27 | |
David Carlier | 7ece096 | 2015-12-08 21:43:09 +0000 | [diff] [blame] | 28 | #define _GNU_SOURCE |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <unistd.h> |
| 32 | #include <string.h> |
| 33 | #include <ctype.h> |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 34 | #include <dirent.h> |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 35 | #include <sys/stat.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | #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 Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 42 | #include <net/if.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | #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 Perennou | 992709b | 2013-02-12 10:53:52 +0100 | [diff] [blame] | 49 | #include <sys/wait.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <time.h> |
| 51 | #include <syslog.h> |
Michael Scherer | ab012dd | 2013-01-12 18:35:19 +0100 | [diff] [blame] | 52 | #include <grp.h> |
Willy Tarreau | fc6c032 | 2012-11-16 16:12:27 +0100 | [diff] [blame] | 53 | #ifdef USE_CPU_AFFINITY |
Willy Tarreau | fc6c032 | 2012-11-16 16:12:27 +0100 | [diff] [blame] | 54 | #include <sched.h> |
Pieter Baauw | caa6a1b | 2015-09-17 21:26:40 +0200 | [diff] [blame] | 55 | #ifdef __FreeBSD__ |
| 56 | #include <sys/param.h> |
| 57 | #include <sys/cpuset.h> |
| 58 | #endif |
Willy Tarreau | fc6c032 | 2012-11-16 16:12:27 +0100 | [diff] [blame] | 59 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | |
| 61 | #ifdef DEBUG_FULL |
| 62 | #include <assert.h> |
| 63 | #endif |
| 64 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 65 | #include <common/base64.h> |
| 66 | #include <common/cfgparse.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 67 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 68 | #include <common/compat.h> |
| 69 | #include <common/config.h> |
| 70 | #include <common/defaults.h> |
Willy Tarreau | d740bab | 2007-10-28 11:14:07 +0100 | [diff] [blame] | 71 | #include <common/errors.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 72 | #include <common/memory.h> |
| 73 | #include <common/mini-clist.h> |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 74 | #include <common/namespace.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 75 | #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 Faulet | be0faa2 | 2017-08-29 15:37:10 +0200 | [diff] [blame] | 80 | #include <common/hathreads.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 81 | |
| 82 | #include <types/capture.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 83 | #include <types/filters.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | #include <types/global.h> |
Simon Horman | ac82142 | 2011-07-15 13:14:09 +0900 | [diff] [blame] | 85 | #include <types/acl.h> |
Willy Tarreau | 3c63fd8 | 2011-09-07 18:00:47 +0200 | [diff] [blame] | 86 | #include <types/peers.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 87 | |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 88 | #include <proto/acl.h> |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 89 | #include <proto/applet.h> |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 90 | #include <proto/arg.h> |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 91 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 93 | #include <proto/channel.h> |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 94 | #include <proto/connection.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 95 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 96 | #include <proto/filters.h> |
Willy Tarreau | 34eb671 | 2011-10-24 18:15:04 +0200 | [diff] [blame] | 97 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 98 | #include <proto/hlua.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 99 | #include <proto/listener.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 100 | #include <proto/log.h> |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 101 | #include <proto/pattern.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 102 | #include <proto/protocol.h> |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 103 | #include <proto/proto_http.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | #include <proto/proxy.h> |
| 105 | #include <proto/queue.h> |
| 106 | #include <proto/server.h> |
Willy Tarreau | b1ec8c4 | 2015-04-03 13:53:24 +0200 | [diff] [blame] | 107 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 108 | #include <proto/stream.h> |
Willy Tarreau | 2985794 | 2009-05-10 09:01:21 +0200 | [diff] [blame] | 109 | #include <proto/signal.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | #include <proto/task.h> |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 111 | #include <proto/dns.h> |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 112 | #include <proto/vars.h> |
Willy Tarreau | 50bc31d | 2017-08-16 15:35:19 +0200 | [diff] [blame] | 113 | #ifdef USE_OPENSSL |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 114 | #include <proto/ssl_sock.h> |
Willy Tarreau | 50bc31d | 2017-08-16 15:35:19 +0200 | [diff] [blame] | 115 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 116 | |
Willy Tarreau | 477ecd8 | 2010-01-03 21:12:30 +0100 | [diff] [blame] | 117 | /* list of config files */ |
| 118 | static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | int pid; /* current process id */ |
Willy Tarreau | 2815664 | 2007-11-26 16:13:36 +0100 | [diff] [blame] | 120 | int relative_pid = 1; /* process id starting at 1 */ |
Willy Tarreau | 387bd4f | 2017-11-10 19:08:14 +0100 | [diff] [blame] | 121 | unsigned long pid_bit = 1; /* bit corresponding to the process id */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | |
| 123 | /* global options */ |
| 124 | struct global global = { |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 125 | .hard_stop_after = TICK_ETERNITY, |
Willy Tarreau | 247a13a | 2012-11-15 17:38:15 +0100 | [diff] [blame] | 126 | .nbproc = 1, |
Christopher Faulet | be0faa2 | 2017-08-29 15:37:10 +0200 | [diff] [blame] | 127 | .nbthread = 1, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 128 | .req_count = 0, |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 129 | .logsrvs = LIST_HEAD_INIT(global.logsrvs), |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 130 | .maxzlibmem = 0, |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 131 | .comp_rate_lim = 0, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 132 | .ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED, |
Emeric Brun | ed76092 | 2010-10-22 17:59:25 +0200 | [diff] [blame] | 133 | .unix_bind = { |
| 134 | .ux = { |
| 135 | .uid = -1, |
| 136 | .gid = -1, |
| 137 | .mode = 0, |
| 138 | } |
| 139 | }, |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 140 | .tune = { |
| 141 | .bufsize = BUFSIZE, |
Willy Tarreau | 2709784 | 2015-09-28 13:53:23 +0200 | [diff] [blame] | 142 | .maxrewrite = -1, |
Willy Tarreau | 43961d5 | 2010-10-04 20:39:20 +0200 | [diff] [blame] | 143 | .chksize = BUFSIZE, |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 144 | .reserved_bufs = RESERVED_BUFS, |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 145 | .pattern_cache = DEFAULT_PAT_LRU_SIZE, |
Emeric Brun | fc32aca | 2012-09-03 12:10:29 +0200 | [diff] [blame] | 146 | #ifdef USE_OPENSSL |
Emeric Brun | 4663577 | 2012-11-14 11:32:56 +0100 | [diff] [blame] | 147 | .sslcachesize = SSLCACHESIZE, |
Emeric Brun | fc32aca | 2012-09-03 12:10:29 +0200 | [diff] [blame] | 148 | #endif |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 149 | .comp_maxlevel = 1, |
Willy Tarreau | 7e31273 | 2014-02-12 16:35:14 +0100 | [diff] [blame] | 150 | #ifdef DEFAULT_IDLE_TIMER |
| 151 | .idle_timer = DEFAULT_IDLE_TIMER, |
| 152 | #else |
| 153 | .idle_timer = 1000, /* 1 second */ |
| 154 | #endif |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 155 | }, |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 156 | #ifdef USE_OPENSSL |
| 157 | #ifdef DEFAULT_MAXSSLCONN |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 158 | .maxsslconn = DEFAULT_MAXSSLCONN, |
| 159 | #endif |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 160 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 161 | /* others NULL OK */ |
| 162 | }; |
| 163 | |
| 164 | /*********************************************************************/ |
| 165 | |
| 166 | int stopping; /* non zero means stopping in progress */ |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 167 | int killed; /* non zero means a hard-stop is triggered */ |
Willy Tarreau | af7ad00 | 2010-08-31 15:39:26 +0200 | [diff] [blame] | 168 | int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 169 | |
| 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 175 | static int *oldpids = NULL; |
| 176 | static int oldpids_sig; /* use USR1 or TERM */ |
| 177 | |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 178 | /* Path to the unix socket we use to retrieve listener sockets from the old process */ |
| 179 | static const char *old_unixsocket; |
| 180 | |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 181 | static char *cur_unixsocket = NULL; |
| 182 | |
William Lallemand | cb11fd2 | 2017-06-01 17:38:52 +0200 | [diff] [blame] | 183 | int atexit_flag = 0; |
| 184 | |
Willy Tarreau | bb545b4 | 2010-08-25 12:58:59 +0200 | [diff] [blame] | 185 | int nb_oldpids = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 186 | const int zero = 0; |
| 187 | const int one = 1; |
Alexandre Cassen | 87ea548 | 2007-10-11 20:48:58 +0200 | [diff] [blame] | 188 | const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 189 | |
Willy Tarreau | 1d21e0a | 2010-03-12 21:58:54 +0100 | [diff] [blame] | 190 | char hostname[MAX_HOSTNAME_LEN]; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 191 | char localpeer[MAX_HOSTNAME_LEN]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 192 | |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 193 | /* 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 | */ |
| 196 | int shut_your_big_mouth_gcc_int = 0; |
| 197 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 198 | int *children = NULL; /* store PIDs of children in master workers mode */ |
| 199 | |
| 200 | static volatile sig_atomic_t caught_signal = 0; |
| 201 | static char **next_argv = NULL; |
| 202 | |
William Lallemand | e20b6a6 | 2017-06-01 17:38:55 +0200 | [diff] [blame] | 203 | int mworker_pipe[2]; |
| 204 | |
Willy Tarreau | 08ceb10 | 2011-07-24 22:58:00 +0200 | [diff] [blame] | 205 | /* list of the temporarily limited listeners because of lack of resource */ |
| 206 | struct list global_listener_queue = LIST_HEAD_INIT(global_listener_queue); |
Willy Tarreau | e9b2602 | 2011-08-01 20:57:55 +0200 | [diff] [blame] | 207 | struct task *global_listener_queue_task; |
| 208 | static struct task *manage_global_listener_queue(struct task *t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 209 | |
Willy Tarreau | ff05550 | 2014-04-28 22:27:06 +0200 | [diff] [blame] | 210 | /* bitfield of a few warnings to emit just once (WARN_*) */ |
| 211 | unsigned int warned = 0; |
| 212 | |
Willy Tarreau | cdb737e | 2016-12-21 18:43:10 +0100 | [diff] [blame] | 213 | |
| 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 | */ |
| 219 | struct list build_opts_list = LIST_HEAD_INIT(build_opts_list); |
| 220 | struct build_opts_str { |
| 221 | struct list list; |
| 222 | const char *str; |
| 223 | int must_free; |
| 224 | }; |
| 225 | |
Willy Tarreau | e694573 | 2016-12-21 19:57:00 +0100 | [diff] [blame] | 226 | /* 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 | */ |
| 233 | struct list post_check_list = LIST_HEAD_INIT(post_check_list); |
| 234 | struct post_check_fct { |
| 235 | struct list list; |
| 236 | int (*fct)(); |
| 237 | }; |
| 238 | |
Willy Tarreau | 05554e6 | 2016-12-21 20:46:26 +0100 | [diff] [blame] | 239 | /* 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 | */ |
| 244 | struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list); |
| 245 | struct post_deinit_fct { |
| 246 | struct list list; |
| 247 | void (*fct)(); |
| 248 | }; |
| 249 | |
Christopher Faulet | 415f611 | 2017-07-25 16:52:58 +0200 | [diff] [blame] | 250 | /* 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. */ |
| 253 | struct list per_thread_init_list = LIST_HEAD_INIT(per_thread_init_list); |
| 254 | struct 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. */ |
| 263 | struct list per_thread_deinit_list = LIST_HEAD_INIT(per_thread_deinit_list); |
| 264 | struct per_thread_deinit_fct { |
| 265 | struct list list; |
| 266 | void (*fct)(); |
| 267 | }; |
| 268 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 269 | /*********************************************************************/ |
| 270 | /* general purpose functions ***************************************/ |
| 271 | /*********************************************************************/ |
| 272 | |
Willy Tarreau | cdb737e | 2016-12-21 18:43:10 +0100 | [diff] [blame] | 273 | /* 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 | */ |
| 276 | void 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 Tarreau | e694573 | 2016-12-21 19:57:00 +0100 | [diff] [blame] | 290 | /* used to register some initialization functions to call after the checks. */ |
| 291 | void 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 Tarreau | 05554e6 | 2016-12-21 20:46:26 +0100 | [diff] [blame] | 304 | /* used to register some de-initialization functions to call after everything |
| 305 | * has stopped. |
| 306 | */ |
| 307 | void 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 Faulet | 415f611 | 2017-07-25 16:52:58 +0200 | [diff] [blame] | 320 | /* used to register some initialization functions to call for each thread. */ |
| 321 | void 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. */ |
| 335 | void 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 Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 348 | static void display_version() |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 349 | { |
| 350 | printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n"); |
Willy Tarreau | 7b67726 | 2017-04-03 09:27:49 +0200 | [diff] [blame] | 351 | printf("Copyright 2000-2017 Willy Tarreau <willy@haproxy.org>\n\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 354 | static void display_build_opts() |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 355 | { |
Willy Tarreau | cdb737e | 2016-12-21 18:43:10 +0100 | [diff] [blame] | 356 | struct build_opts_str *item; |
| 357 | |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 358 | printf("Build options :" |
| 359 | #ifdef BUILD_TARGET |
Willy Tarreau | 9f2b730 | 2008-01-02 20:48:34 +0100 | [diff] [blame] | 360 | "\n TARGET = " BUILD_TARGET |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 361 | #endif |
| 362 | #ifdef BUILD_CPU |
Willy Tarreau | 9f2b730 | 2008-01-02 20:48:34 +0100 | [diff] [blame] | 363 | "\n CPU = " BUILD_CPU |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 364 | #endif |
| 365 | #ifdef BUILD_CC |
Willy Tarreau | 9f2b730 | 2008-01-02 20:48:34 +0100 | [diff] [blame] | 366 | "\n CC = " BUILD_CC |
| 367 | #endif |
| 368 | #ifdef BUILD_CFLAGS |
| 369 | "\n CFLAGS = " BUILD_CFLAGS |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 370 | #endif |
Willy Tarreau | 9f2b730 | 2008-01-02 20:48:34 +0100 | [diff] [blame] | 371 | #ifdef BUILD_OPTIONS |
| 372 | "\n OPTIONS = " BUILD_OPTIONS |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 373 | #endif |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 374 | "\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 Tarreau | be5b685 | 2009-10-03 18:57:08 +0200 | [diff] [blame] | 378 | |
Willy Tarreau | cdb737e | 2016-12-21 18:43:10 +0100 | [diff] [blame] | 379 | list_for_each_entry(item, &build_opts_list, list) { |
| 380 | puts(item->str); |
| 381 | } |
| 382 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 383 | putchar('\n'); |
| 384 | |
Willy Tarreau | be5b685 | 2009-10-03 18:57:08 +0200 | [diff] [blame] | 385 | list_pollers(stdout); |
| 386 | putchar('\n'); |
Christopher Faulet | b3f4e14 | 2016-03-07 12:46:38 +0100 | [diff] [blame] | 387 | list_filters(stdout); |
| 388 | putchar('\n'); |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 391 | /* |
| 392 | * This function prints the command line usage and exits |
| 393 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 394 | static void usage(char *name) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 395 | { |
| 396 | display_version(); |
| 397 | fprintf(stderr, |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 398 | "Usage : %s [-f <cfgfile|cfgdir>]* [ -vdV" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 399 | "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n" |
Willy Tarreau | a088d31 | 2015-10-08 11:58:48 +0200 | [diff] [blame] | 400 | " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n" |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 401 | " -v displays version ; -vv shows known build options.\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 402 | " -d enters debug mode ; -db only disables background mode.\n" |
Willy Tarreau | 6e06443 | 2012-05-08 15:40:42 +0200 | [diff] [blame] | 403 | " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 404 | " -V enters verbose mode (disables quiet mode)\n" |
Willy Tarreau | 576132e | 2011-09-10 19:26:56 +0200 | [diff] [blame] | 405 | " -D goes daemon ; -C changes to <dir> before loading files.\n" |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 406 | " -W master-worker mode.\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 407 | " -q quiet mode : don't display messages\n" |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 408 | " -c check mode : only check config files and exit\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 409 | " -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 Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 412 | " -L set local peer name (default to hostname)\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 413 | " -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 Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 417 | #if defined(ENABLE_KQUEUE) |
| 418 | " -dk disables kqueue() usage even when available\n" |
| 419 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 420 | #if defined(ENABLE_POLL) |
| 421 | " -dp disables poll() usage even when available\n" |
| 422 | #endif |
Willy Tarreau | b55932d | 2009-08-16 13:20:32 +0200 | [diff] [blame] | 423 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
Willy Tarreau | 3ab68cf | 2009-01-25 16:03:28 +0100 | [diff] [blame] | 424 | " -dS disables splice usage (broken on old kernels)\n" |
| 425 | #endif |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 426 | #if defined(USE_GETADDRINFO) |
| 427 | " -dG disables getaddrinfo() usage\n" |
| 428 | #endif |
Lukas Tribus | a0bcbdc | 2016-09-12 21:42:20 +0000 | [diff] [blame] | 429 | #if defined(SO_REUSEPORT) |
| 430 | " -dR disables SO_REUSEPORT usage\n" |
| 431 | #endif |
Willy Tarreau | 3eed10e | 2016-11-07 21:03:16 +0100 | [diff] [blame] | 432 | " -dr ignores server address resolution failures\n" |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 433 | " -dV disables SSL verify on servers side\n" |
Willy Tarreau | c6ca1aa | 2015-10-08 11:32:32 +0200 | [diff] [blame] | 434 | " -sf/-st [pid ]* finishes/terminates old pids.\n" |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 435 | " -x <unix_socket> get listening sockets from a unix socket\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 436 | "\n", |
| 437 | name, DEFAULT_MAXCONN, cfg_maxpconn); |
| 438 | exit(1); |
| 439 | } |
| 440 | |
| 441 | |
| 442 | |
| 443 | /*********************************************************************/ |
| 444 | /* more specific functions ***************************************/ |
| 445 | /*********************************************************************/ |
| 446 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 447 | /* sends the signal <sig> to all pids found in <oldpids>. Returns the number of |
| 448 | * pids the signal was correctly delivered to. |
| 449 | */ |
| 450 | static 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 | |
| 462 | int 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 | |
| 473 | static void mworker_signalhandler(int signum) |
| 474 | { |
| 475 | caught_signal = signum; |
| 476 | } |
| 477 | |
| 478 | static 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 | |
| 493 | static 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 | |
| 506 | static 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 | |
| 519 | static 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 | |
| 532 | static 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 543 | /* |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 544 | * remove a pid forom the olpid array and decrease nb_oldpids |
| 545 | * return 1 pid was found otherwise return 0 |
| 546 | */ |
| 547 | |
| 548 | int delete_oldpid(int pid) |
| 549 | { |
| 550 | int i; |
| 551 | |
| 552 | for (i = 0; i < nb_oldpids; i++) { |
| 553 | if (oldpids[i] == pid) { |
| 554 | oldpids[i] = oldpids[nb_oldpids - 1]; |
| 555 | oldpids[nb_oldpids - 1] = 0; |
| 556 | nb_oldpids--; |
| 557 | return 1; |
| 558 | } |
| 559 | } |
| 560 | return 0; |
| 561 | } |
| 562 | |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 563 | |
| 564 | static void get_cur_unixsocket() |
| 565 | { |
| 566 | /* if -x was used, try to update the stat socket if not available anymore */ |
| 567 | if (global.stats_fe) { |
| 568 | struct bind_conf *bind_conf; |
| 569 | |
| 570 | /* pass through all stats socket */ |
| 571 | list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) { |
| 572 | struct listener *l; |
| 573 | |
| 574 | list_for_each_entry(l, &bind_conf->listeners, by_bind) { |
| 575 | |
| 576 | if (l->addr.ss_family == AF_UNIX && |
| 577 | (bind_conf->level & ACCESS_FD_LISTENERS)) { |
| 578 | const struct sockaddr_un *un; |
| 579 | |
| 580 | un = (struct sockaddr_un *)&l->addr; |
| 581 | /* priority to old_unixsocket */ |
| 582 | if (!cur_unixsocket) { |
| 583 | cur_unixsocket = strdup(un->sun_path); |
| 584 | } else { |
| 585 | if (old_unixsocket && !strcmp(un->sun_path, old_unixsocket)) { |
| 586 | free(cur_unixsocket); |
| 587 | cur_unixsocket = strdup(old_unixsocket); |
| 588 | return; |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | if (!cur_unixsocket && old_unixsocket) |
| 596 | cur_unixsocket = strdup(old_unixsocket); |
| 597 | } |
| 598 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 599 | /* |
| 600 | * When called, this function reexec haproxy with -sf followed by current |
| 601 | * children PIDs and possibily old children PIDs if they didn't leave yet. |
| 602 | */ |
| 603 | static void mworker_reload() |
| 604 | { |
| 605 | int next_argc = 0; |
| 606 | int j; |
| 607 | char *msg = NULL; |
| 608 | |
| 609 | mworker_block_signals(); |
| 610 | mworker_unregister_signals(); |
| 611 | setenv("HAPROXY_MWORKER_REEXEC", "1", 1); |
| 612 | |
| 613 | /* compute length */ |
| 614 | while (next_argv[next_argc]) |
| 615 | next_argc++; |
| 616 | |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 617 | /* 1 for haproxy -sf, 2 for -x /socket */ |
| 618 | next_argv = realloc(next_argv, (next_argc + 1 + 2 + global.nbproc + nb_oldpids + 1) * sizeof(char *)); |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 619 | if (next_argv == NULL) |
| 620 | goto alloc_error; |
| 621 | |
| 622 | |
| 623 | /* add -sf <PID>* to argv */ |
| 624 | if (children || nb_oldpids > 0) |
| 625 | next_argv[next_argc++] = "-sf"; |
| 626 | if (children) { |
| 627 | for (j = 0; j < global.nbproc; next_argc++,j++) { |
| 628 | next_argv[next_argc] = memprintf(&msg, "%d", children[j]); |
| 629 | if (next_argv[next_argc] == NULL) |
| 630 | goto alloc_error; |
| 631 | msg = NULL; |
| 632 | } |
| 633 | } |
| 634 | /* copy old process PIDs */ |
| 635 | for (j = 0; j < nb_oldpids; next_argc++,j++) { |
| 636 | next_argv[next_argc] = memprintf(&msg, "%d", oldpids[j]); |
| 637 | if (next_argv[next_argc] == NULL) |
| 638 | goto alloc_error; |
| 639 | msg = NULL; |
| 640 | } |
| 641 | next_argv[next_argc] = NULL; |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 642 | |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 643 | /* add the -x option with the stat socket */ |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 644 | if (cur_unixsocket) { |
| 645 | |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 646 | next_argv[next_argc++] = "-x"; |
| 647 | next_argv[next_argc++] = (char *)cur_unixsocket; |
| 648 | next_argv[next_argc++] = NULL; |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 649 | } |
| 650 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 651 | deinit(); /* we don't want to leak FD there */ |
| 652 | Warning("Reexecuting Master process\n"); |
Tim Duesterhus | 0436ab7 | 2017-11-12 17:39:18 +0100 | [diff] [blame] | 653 | execvp(next_argv[0], next_argv); |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 654 | |
| 655 | alloc_error: |
| 656 | Warning("Cannot allocate memory\n"); |
| 657 | Warning("Failed to reexecute the master processs [%d]\n", pid); |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | /* |
| 663 | * Wait for every children to exit |
| 664 | */ |
| 665 | |
| 666 | static void mworker_wait() |
| 667 | { |
| 668 | int exitpid = -1; |
| 669 | int status = 0; |
| 670 | |
| 671 | mworker_register_signals(); |
| 672 | mworker_unblock_signals(); |
| 673 | |
| 674 | while (1) { |
| 675 | |
| 676 | while (((exitpid = wait(&status)) == -1) && errno == EINTR) { |
| 677 | int sig = caught_signal; |
| 678 | if (sig == SIGUSR2 || sig == SIGHUP) { |
| 679 | mworker_reload(); |
| 680 | } else { |
| 681 | Warning("Exiting Master process...\n"); |
| 682 | mworker_kill(sig); |
| 683 | mworker_unregister_signals(); |
| 684 | } |
| 685 | caught_signal = 0; |
| 686 | } |
| 687 | |
| 688 | if (exitpid == -1 && errno == ECHILD) { |
| 689 | Warning("All workers are left. Leaving... (%d)\n", status); |
William Lallemand | cb11fd2 | 2017-06-01 17:38:52 +0200 | [diff] [blame] | 690 | atexit_flag = 0; |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 691 | exit(status); /* parent must leave using the latest status code known */ |
| 692 | } |
| 693 | |
| 694 | if (WIFEXITED(status)) |
| 695 | status = WEXITSTATUS(status); |
| 696 | else if (WIFSIGNALED(status)) |
| 697 | status = 128 + WTERMSIG(status); |
| 698 | else if (WIFSTOPPED(status)) |
| 699 | status = 128 + WSTOPSIG(status); |
| 700 | else |
| 701 | status = 255; |
| 702 | |
| 703 | if (!children) { |
| 704 | Warning("Worker %d left with exit code %d\n", exitpid, status); |
| 705 | } else { |
| 706 | /* check if exited child was in the current children list */ |
| 707 | if (current_child(exitpid)) { |
| 708 | Alert("Current worker %d left with exit code %d\n", exitpid, status); |
William Lallemand | 69f9b3b | 2017-06-01 17:38:54 +0200 | [diff] [blame] | 709 | if (status != 0 && status != 130 && status != 143 |
| 710 | && global.tune.options & GTUNE_EXIT_ONFAILURE) { |
| 711 | Alert("exit-on-failure: killing every workers with SIGTERM\n"); |
| 712 | mworker_kill(SIGTERM); |
| 713 | } |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 714 | } else { |
| 715 | Warning("Former worker %d left with exit code %d\n", exitpid, status); |
| 716 | delete_oldpid(exitpid); |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
William Lallemand | cb11fd2 | 2017-06-01 17:38:52 +0200 | [diff] [blame] | 722 | |
| 723 | /* |
| 724 | * Reexec the process in failure mode, instead of exiting |
| 725 | */ |
| 726 | void reexec_on_failure() |
| 727 | { |
| 728 | if (!atexit_flag) |
| 729 | return; |
| 730 | |
| 731 | setenv("HAPROXY_MWORKER_WAIT_ONLY", "1", 1); |
| 732 | |
| 733 | Warning("Reexecuting Master process in waitpid mode\n"); |
| 734 | mworker_reload(); |
| 735 | |
| 736 | Warning("Failed to reexecute the master processs\n"); |
| 737 | } |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 738 | |
| 739 | |
| 740 | /* |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 741 | * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts |
| 742 | * a signal zero to all subscribers. This means that it's as easy as |
| 743 | * subscribing to signal 0 to get informed about an imminent shutdown. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 744 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 745 | static void sig_soft_stop(struct sig_handler *sh) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 746 | { |
| 747 | soft_stop(); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 748 | signal_unregister_handler(sh); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 749 | pool_gc2(NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* |
| 753 | * upon SIGTTOU, we pause everything |
| 754 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 755 | static void sig_pause(struct sig_handler *sh) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 756 | { |
| 757 | pause_proxies(); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 758 | pool_gc2(NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /* |
| 762 | * upon SIGTTIN, let's have a soft stop. |
| 763 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 764 | static void sig_listen(struct sig_handler *sh) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 765 | { |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 766 | resume_proxies(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /* |
| 770 | * this function dumps every server's state when the process receives SIGHUP. |
| 771 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 772 | static void sig_dump_state(struct sig_handler *sh) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 773 | { |
| 774 | struct proxy *p = proxy; |
| 775 | |
| 776 | Warning("SIGHUP received, dumping servers states.\n"); |
| 777 | while (p) { |
| 778 | struct server *s = p->srv; |
| 779 | |
| 780 | send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id); |
| 781 | while (s) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 782 | chunk_printf(&trash, |
| 783 | "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.", |
| 784 | p->id, s->id, |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 785 | (s->cur_state != SRV_ST_STOPPED) ? "UP" : "DOWN", |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 786 | s->cur_sess, s->nbpend, s->counters.cum_sess); |
| 787 | Warning("%s\n", trash.str); |
| 788 | send_log(p, LOG_NOTICE, "%s\n", trash.str); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 789 | s = s->next; |
| 790 | } |
| 791 | |
Willy Tarreau | 5fcc8f1 | 2007-09-17 11:27:09 +0200 | [diff] [blame] | 792 | /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */ |
| 793 | if (!p->srv) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 794 | chunk_printf(&trash, |
| 795 | "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.", |
| 796 | p->id, |
| 797 | p->feconn, p->beconn, p->totpend, p->nbpend, p->fe_counters.cum_conn, p->be_counters.cum_conn); |
Willy Tarreau | 5fcc8f1 | 2007-09-17 11:27:09 +0200 | [diff] [blame] | 798 | } else if (p->srv_act == 0) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 799 | chunk_printf(&trash, |
| 800 | "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.", |
| 801 | p->id, |
| 802 | (p->srv_bck) ? "is running on backup servers" : "has no server available", |
| 803 | p->feconn, p->beconn, p->totpend, p->nbpend, p->fe_counters.cum_conn, p->be_counters.cum_conn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 804 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 805 | chunk_printf(&trash, |
| 806 | "SIGHUP: Proxy %s has %d active servers and %d backup servers available." |
| 807 | " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.", |
| 808 | p->id, p->srv_act, p->srv_bck, |
| 809 | p->feconn, p->beconn, p->totpend, p->nbpend, p->fe_counters.cum_conn, p->be_counters.cum_conn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 810 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 811 | Warning("%s\n", trash.str); |
| 812 | send_log(p, LOG_NOTICE, "%s\n", trash.str); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 813 | |
| 814 | p = p->next; |
| 815 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 816 | } |
| 817 | |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 818 | static void dump(struct sig_handler *sh) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 819 | { |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 820 | /* dump memory usage then free everything possible */ |
| 821 | dump_pools(); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 822 | pool_gc2(NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 823 | } |
| 824 | |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 825 | /* This function check if cfg_cfgfiles containes directories. |
| 826 | * If it find one, it add all the files (and only files) it containes |
| 827 | * in cfg_cfgfiles in place of the directory (and remove the directory). |
| 828 | * It add the files in lexical order. |
| 829 | * It add only files with .cfg extension. |
| 830 | * It doesn't add files with name starting with '.' |
| 831 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 832 | static void cfgfiles_expand_directories(void) |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 833 | { |
| 834 | struct wordlist *wl, *wlb; |
| 835 | char *err = NULL; |
| 836 | |
| 837 | list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) { |
| 838 | struct stat file_stat; |
| 839 | struct dirent **dir_entries = NULL; |
| 840 | int dir_entries_nb; |
| 841 | int dir_entries_it; |
| 842 | |
| 843 | if (stat(wl->s, &file_stat)) { |
| 844 | Alert("Cannot open configuration file/directory %s : %s\n", |
| 845 | wl->s, |
| 846 | strerror(errno)); |
| 847 | exit(1); |
| 848 | } |
| 849 | |
| 850 | if (!S_ISDIR(file_stat.st_mode)) |
| 851 | continue; |
| 852 | |
| 853 | /* from this point wl->s is a directory */ |
| 854 | |
| 855 | dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort); |
| 856 | if (dir_entries_nb < 0) { |
| 857 | Alert("Cannot open configuration directory %s : %s\n", |
| 858 | wl->s, |
| 859 | strerror(errno)); |
| 860 | exit(1); |
| 861 | } |
| 862 | |
| 863 | /* for each element in the directory wl->s */ |
| 864 | for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) { |
| 865 | struct dirent *dir_entry = dir_entries[dir_entries_it]; |
| 866 | char *filename = NULL; |
| 867 | char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg"); |
| 868 | |
| 869 | /* don't add filename that begin with . |
| 870 | * only add filename with .cfg extention |
| 871 | */ |
| 872 | if (dir_entry->d_name[0] == '.' || |
| 873 | !(d_name_cfgext && d_name_cfgext[4] == '\0')) |
| 874 | goto next_dir_entry; |
| 875 | |
| 876 | if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) { |
| 877 | Alert("Cannot load configuration files %s : out of memory.\n", |
| 878 | filename); |
| 879 | exit(1); |
| 880 | } |
| 881 | |
| 882 | if (stat(filename, &file_stat)) { |
| 883 | Alert("Cannot open configuration file %s : %s\n", |
| 884 | wl->s, |
| 885 | strerror(errno)); |
| 886 | exit(1); |
| 887 | } |
| 888 | |
| 889 | /* don't add anything else than regular file in cfg_cfgfiles |
| 890 | * this way we avoid loops |
| 891 | */ |
| 892 | if (!S_ISREG(file_stat.st_mode)) |
| 893 | goto next_dir_entry; |
| 894 | |
| 895 | if (!list_append_word(&wl->list, filename, &err)) { |
| 896 | Alert("Cannot load configuration files %s : %s\n", |
| 897 | filename, |
| 898 | err); |
| 899 | exit(1); |
| 900 | } |
| 901 | |
| 902 | next_dir_entry: |
| 903 | free(filename); |
| 904 | free(dir_entry); |
| 905 | } |
| 906 | |
| 907 | free(dir_entries); |
| 908 | |
| 909 | /* remove the current directory (wl) from cfg_cfgfiles */ |
| 910 | free(wl->s); |
| 911 | LIST_DEL(&wl->list); |
| 912 | free(wl); |
| 913 | } |
| 914 | |
| 915 | free(err); |
| 916 | } |
| 917 | |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 918 | static int get_old_sockets(const char *unixsocket) |
| 919 | { |
| 920 | char *cmsgbuf = NULL, *tmpbuf = NULL; |
| 921 | int *tmpfd = NULL; |
| 922 | struct sockaddr_un addr; |
| 923 | struct cmsghdr *cmsg; |
| 924 | struct msghdr msghdr; |
| 925 | struct iovec iov; |
| 926 | struct xfer_sock_list *xfer_sock = NULL; |
Olivier Houchard | 5474087 | 2017-04-06 14:45:14 +0200 | [diff] [blame] | 927 | struct timeval tv = { .tv_sec = 1, .tv_usec = 0 }; |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 928 | int sock = -1; |
| 929 | int ret = -1; |
| 930 | int ret2 = -1; |
| 931 | int fd_nb; |
| 932 | int got_fd = 0; |
| 933 | int i = 0; |
| 934 | size_t maxoff = 0, curoff = 0; |
| 935 | |
| 936 | memset(&msghdr, 0, sizeof(msghdr)); |
| 937 | cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD); |
| 938 | if (!cmsgbuf) { |
| 939 | Warning("Failed to allocate memory to send sockets\n"); |
| 940 | goto out; |
| 941 | } |
| 942 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 943 | if (sock < 0) { |
| 944 | Warning("Failed to connect to the old process socket '%s'\n", |
| 945 | unixsocket); |
| 946 | goto out; |
| 947 | } |
| 948 | strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path)); |
| 949 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
| 950 | addr.sun_family = PF_UNIX; |
| 951 | ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr)); |
| 952 | if (ret < 0) { |
| 953 | Warning("Failed to connect to the old process socket '%s'\n", |
| 954 | unixsocket); |
| 955 | goto out; |
| 956 | } |
Olivier Houchard | 5474087 | 2017-04-06 14:45:14 +0200 | [diff] [blame] | 957 | setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv)); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 958 | iov.iov_base = &fd_nb; |
| 959 | iov.iov_len = sizeof(fd_nb); |
| 960 | msghdr.msg_iov = &iov; |
| 961 | msghdr.msg_iovlen = 1; |
| 962 | send(sock, "_getsocks\n", strlen("_getsocks\n"), 0); |
| 963 | /* First, get the number of file descriptors to be received */ |
| 964 | if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) { |
| 965 | Warning("Failed to get the number of sockets to be transferred !\n"); |
| 966 | goto out; |
| 967 | } |
| 968 | if (fd_nb == 0) { |
| 969 | ret = 0; |
| 970 | goto out; |
| 971 | } |
Olivier Houchard | f143b80 | 2017-11-04 15:13:01 +0100 | [diff] [blame] | 972 | tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int))); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 973 | if (tmpbuf == NULL) { |
| 974 | Warning("Failed to allocate memory while receiving sockets\n"); |
| 975 | goto out; |
| 976 | } |
| 977 | tmpfd = malloc(fd_nb * sizeof(int)); |
| 978 | if (tmpfd == NULL) { |
| 979 | Warning("Failed to allocate memory while receiving sockets\n"); |
| 980 | goto out; |
| 981 | } |
| 982 | msghdr.msg_control = cmsgbuf; |
| 983 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD; |
Olivier Houchard | f143b80 | 2017-11-04 15:13:01 +0100 | [diff] [blame] | 984 | iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 985 | do { |
| 986 | int ret3; |
| 987 | |
| 988 | iov.iov_base = tmpbuf + curoff; |
| 989 | ret = recvmsg(sock, &msghdr, 0); |
| 990 | if (ret == -1 && errno == EINTR) |
| 991 | continue; |
| 992 | if (ret <= 0) |
| 993 | break; |
| 994 | /* Send an ack to let the sender know we got the sockets |
| 995 | * and it can send some more |
| 996 | */ |
| 997 | do { |
| 998 | ret3 = send(sock, &got_fd, sizeof(got_fd), 0); |
| 999 | } while (ret3 == -1 && errno == EINTR); |
| 1000 | for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL; |
| 1001 | cmsg = CMSG_NXTHDR(&msghdr, cmsg)) { |
| 1002 | if (cmsg->cmsg_level == SOL_SOCKET && |
| 1003 | cmsg->cmsg_type == SCM_RIGHTS) { |
| 1004 | size_t totlen = cmsg->cmsg_len - |
| 1005 | CMSG_LEN(0); |
| 1006 | if (totlen / sizeof(int) + got_fd > fd_nb) { |
| 1007 | Warning("Got to many sockets !\n"); |
| 1008 | goto out; |
| 1009 | } |
| 1010 | /* |
| 1011 | * Be paranoid and use memcpy() to avoid any |
| 1012 | * potential alignement issue. |
| 1013 | */ |
| 1014 | memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen); |
| 1015 | got_fd += totlen / sizeof(int); |
| 1016 | } |
| 1017 | } |
| 1018 | curoff += ret; |
| 1019 | } while (got_fd < fd_nb); |
| 1020 | |
| 1021 | if (got_fd != fd_nb) { |
| 1022 | Warning("We didn't get the expected number of sockets (expecting %d got %d)\n", |
| 1023 | fd_nb, got_fd); |
| 1024 | goto out; |
| 1025 | } |
| 1026 | maxoff = curoff; |
| 1027 | curoff = 0; |
| 1028 | for (i = 0; i < got_fd; i++) { |
| 1029 | int fd = tmpfd[i]; |
| 1030 | socklen_t socklen; |
| 1031 | int len; |
| 1032 | |
| 1033 | xfer_sock = calloc(1, sizeof(*xfer_sock)); |
| 1034 | if (!xfer_sock) { |
| 1035 | Warning("Failed to allocate memory in get_old_sockets() !\n"); |
| 1036 | break; |
| 1037 | } |
| 1038 | xfer_sock->fd = -1; |
| 1039 | |
| 1040 | socklen = sizeof(xfer_sock->addr); |
| 1041 | if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) { |
| 1042 | Warning("Failed to get socket address\n"); |
| 1043 | free(xfer_sock); |
Olivier Houchard | be7b1ce | 2017-07-17 17:25:33 +0200 | [diff] [blame] | 1044 | xfer_sock = NULL; |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1045 | continue; |
| 1046 | } |
| 1047 | if (curoff >= maxoff) { |
| 1048 | Warning("Inconsistency while transferring sockets\n"); |
| 1049 | goto out; |
| 1050 | } |
| 1051 | len = tmpbuf[curoff++]; |
| 1052 | if (len > 0) { |
| 1053 | /* We have a namespace */ |
| 1054 | if (curoff + len > maxoff) { |
| 1055 | Warning("Inconsistency while transferring sockets\n"); |
| 1056 | goto out; |
| 1057 | } |
| 1058 | xfer_sock->namespace = malloc(len + 1); |
| 1059 | if (!xfer_sock->namespace) { |
| 1060 | Warning("Failed to allocate memory while transferring sockets\n"); |
| 1061 | goto out; |
| 1062 | } |
| 1063 | memcpy(xfer_sock->namespace, &tmpbuf[curoff], len); |
| 1064 | xfer_sock->namespace[len] = 0; |
| 1065 | curoff += len; |
| 1066 | } |
| 1067 | if (curoff >= maxoff) { |
| 1068 | Warning("Inconsistency while transferring sockets\n"); |
| 1069 | goto out; |
| 1070 | } |
| 1071 | len = tmpbuf[curoff++]; |
| 1072 | if (len > 0) { |
| 1073 | /* We have an interface */ |
| 1074 | if (curoff + len > maxoff) { |
| 1075 | Warning("Inconsistency while transferring sockets\n"); |
| 1076 | goto out; |
| 1077 | } |
| 1078 | xfer_sock->iface = malloc(len + 1); |
| 1079 | if (!xfer_sock->iface) { |
| 1080 | Warning("Failed to allocate memory while transferring sockets\n"); |
| 1081 | goto out; |
| 1082 | } |
| 1083 | memcpy(xfer_sock->iface, &tmpbuf[curoff], len); |
| 1084 | xfer_sock->namespace[len] = 0; |
| 1085 | curoff += len; |
| 1086 | } |
| 1087 | if (curoff + sizeof(int) > maxoff) { |
| 1088 | Warning("Inconsistency while transferring sockets\n"); |
| 1089 | goto out; |
| 1090 | } |
| 1091 | memcpy(&xfer_sock->options, &tmpbuf[curoff], |
| 1092 | sizeof(xfer_sock->options)); |
| 1093 | curoff += sizeof(xfer_sock->options); |
| 1094 | |
| 1095 | xfer_sock->fd = fd; |
| 1096 | if (xfer_sock_list) |
| 1097 | xfer_sock_list->prev = xfer_sock; |
| 1098 | xfer_sock->next = xfer_sock_list; |
| 1099 | xfer_sock->prev = NULL; |
| 1100 | xfer_sock_list = xfer_sock; |
| 1101 | xfer_sock = NULL; |
| 1102 | } |
| 1103 | |
| 1104 | ret2 = 0; |
| 1105 | out: |
| 1106 | /* If we failed midway make sure to close the remaining |
| 1107 | * file descriptors |
| 1108 | */ |
| 1109 | if (tmpfd != NULL && i < got_fd) { |
| 1110 | for (; i < got_fd; i++) { |
| 1111 | close(tmpfd[i]); |
| 1112 | } |
| 1113 | } |
| 1114 | free(tmpbuf); |
| 1115 | free(tmpfd); |
| 1116 | free(cmsgbuf); |
| 1117 | if (sock != -1) |
| 1118 | close(sock); |
| 1119 | if (xfer_sock) { |
| 1120 | free(xfer_sock->namespace); |
| 1121 | free(xfer_sock->iface); |
| 1122 | if (xfer_sock->fd != -1) |
| 1123 | close(xfer_sock->fd); |
| 1124 | free(xfer_sock); |
| 1125 | } |
| 1126 | return (ret2); |
| 1127 | } |
| 1128 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1129 | /* |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1130 | * copy and cleanup the current argv |
| 1131 | * Remove the -sf /-st parameters |
| 1132 | * Return an allocated copy of argv |
| 1133 | */ |
| 1134 | |
| 1135 | static char **copy_argv(int argc, char **argv) |
| 1136 | { |
| 1137 | char **newargv; |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 1138 | int i = 0, j = 0; |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1139 | |
| 1140 | newargv = calloc(argc + 2, sizeof(char *)); |
| 1141 | if (newargv == NULL) { |
| 1142 | Warning("Cannot allocate memory\n"); |
| 1143 | return NULL; |
| 1144 | } |
| 1145 | |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 1146 | while (i < argc) { |
| 1147 | /* -sf or -st or -x */ |
| 1148 | if ((argv[i][1] == 's' && (argv[i][2] == 'f' || argv[i][2] == 't')) || argv[i][1] == 'x' ) { |
| 1149 | /* list of pids to finish ('f') or terminate ('t') or unix socket (-x) */ |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1150 | i++; |
| 1151 | while (i < argc && argv[i][0] != '-') { |
| 1152 | i++; |
| 1153 | } |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 1154 | continue; |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1155 | } |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 1156 | |
| 1157 | newargv[j++] = argv[i++]; |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1158 | } |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 1159 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1160 | return newargv; |
| 1161 | } |
| 1162 | |
| 1163 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1164 | * This function initializes all the necessary variables. It only returns |
| 1165 | * if everything is OK. If something fails, it exits. |
| 1166 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 1167 | static void init(int argc, char **argv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1168 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1169 | int arg_mode = 0; /* MODE_DEBUG, ... */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1170 | char *tmp; |
| 1171 | char *cfg_pidfile = NULL; |
Willy Tarreau | 058e907 | 2009-07-20 09:30:05 +0200 | [diff] [blame] | 1172 | int err_code = 0; |
Maxime de Roucy | 0f50392 | 2016-05-13 23:52:55 +0200 | [diff] [blame] | 1173 | char *err_msg = NULL; |
Willy Tarreau | 477ecd8 | 2010-01-03 21:12:30 +0100 | [diff] [blame] | 1174 | struct wordlist *wl; |
Kevinm | 48936af | 2010-12-22 16:08:21 +0000 | [diff] [blame] | 1175 | char *progname; |
Willy Tarreau | 576132e | 2011-09-10 19:26:56 +0200 | [diff] [blame] | 1176 | char *change_dir = NULL; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1177 | struct proxy *px; |
Willy Tarreau | e694573 | 2016-12-21 19:57:00 +0100 | [diff] [blame] | 1178 | struct post_check_fct *pcf; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1179 | |
Christopher Faulet | e3a5e35 | 2017-10-24 13:53:54 +0200 | [diff] [blame] | 1180 | global.mode = MODE_STARTING; |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1181 | next_argv = copy_argv(argc, argv); |
| 1182 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 1183 | if (!init_trash_buffers(1)) { |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 1184 | Alert("failed to initialize trash buffers.\n"); |
| 1185 | exit(1); |
| 1186 | } |
David du Colombier | 7af4605 | 2012-05-16 14:16:48 +0200 | [diff] [blame] | 1187 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1188 | /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate |
| 1189 | * the string in case of truncation, and at least FreeBSD appears not to do |
| 1190 | * it. |
| 1191 | */ |
| 1192 | memset(hostname, 0, sizeof(hostname)); |
| 1193 | gethostname(hostname, sizeof(hostname) - 1); |
| 1194 | memset(localpeer, 0, sizeof(localpeer)); |
| 1195 | memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1); |
| 1196 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1197 | /* |
| 1198 | * Initialize the previously static variables. |
| 1199 | */ |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 1200 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 1201 | totalconn = actconn = maxfd = listeners = stopping = 0; |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 1202 | killed = 0; |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 1203 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1204 | |
| 1205 | #ifdef HAPROXY_MEMMAX |
Willy Tarreau | 7006045 | 2015-12-14 12:46:07 +0100 | [diff] [blame] | 1206 | global.rlimit_memmax_all = HAPROXY_MEMMAX; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1207 | #endif |
| 1208 | |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 1209 | tzset(); |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 1210 | tv_update_date(-1,-1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1211 | start_date = now; |
| 1212 | |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 1213 | srandom(now_ms - getpid()); |
| 1214 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1215 | init_log(); |
Willy Tarreau | 2985794 | 2009-05-10 09:01:21 +0200 | [diff] [blame] | 1216 | signal_init(); |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 1217 | if (init_acl() != 0) |
| 1218 | exit(1); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 1219 | init_task(); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1220 | init_stream(); |
Willy Tarreau | b1ec8c4 | 2015-04-03 13:53:24 +0200 | [diff] [blame] | 1221 | init_session(); |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 1222 | init_connection(); |
Willy Tarreau | 8280d64 | 2009-09-23 23:37:52 +0200 | [diff] [blame] | 1223 | /* warning, we init buffers later */ |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 1224 | init_pendconn(); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1225 | init_proto_http(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1226 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 1227 | /* Initialise lua. */ |
| 1228 | hlua_init(); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 1229 | |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 1230 | /* Initialize process vars */ |
| 1231 | vars_init(&global.vars, SCOPE_PROC); |
| 1232 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1233 | global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1234 | #if defined(ENABLE_POLL) |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1235 | global.tune.options |= GTUNE_USE_POLL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1236 | #endif |
| 1237 | #if defined(ENABLE_EPOLL) |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1238 | global.tune.options |= GTUNE_USE_EPOLL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1239 | #endif |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 1240 | #if defined(ENABLE_KQUEUE) |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1241 | global.tune.options |= GTUNE_USE_KQUEUE; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 1242 | #endif |
Willy Tarreau | b55932d | 2009-08-16 13:20:32 +0200 | [diff] [blame] | 1243 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
Willy Tarreau | 3ab68cf | 2009-01-25 16:03:28 +0100 | [diff] [blame] | 1244 | global.tune.options |= GTUNE_USE_SPLICE; |
| 1245 | #endif |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 1246 | #if defined(USE_GETADDRINFO) |
| 1247 | global.tune.options |= GTUNE_USE_GAI; |
| 1248 | #endif |
Lukas Tribus | a0bcbdc | 2016-09-12 21:42:20 +0000 | [diff] [blame] | 1249 | #if defined(SO_REUSEPORT) |
| 1250 | global.tune.options |= GTUNE_USE_REUSEPORT; |
| 1251 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1252 | |
| 1253 | pid = getpid(); |
| 1254 | progname = *argv; |
| 1255 | while ((tmp = strchr(progname, '/')) != NULL) |
| 1256 | progname = tmp + 1; |
| 1257 | |
Kevinm | 48936af | 2010-12-22 16:08:21 +0000 | [diff] [blame] | 1258 | /* the process name is used for the logs only */ |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1259 | chunk_initstr(&global.log_tag, strdup(progname)); |
Kevinm | 48936af | 2010-12-22 16:08:21 +0000 | [diff] [blame] | 1260 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1261 | argc--; argv++; |
| 1262 | while (argc > 0) { |
| 1263 | char *flag; |
| 1264 | |
| 1265 | if (**argv == '-') { |
| 1266 | flag = *argv+1; |
| 1267 | |
| 1268 | /* 1 arg */ |
| 1269 | if (*flag == 'v') { |
| 1270 | display_version(); |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 1271 | if (flag[1] == 'v') /* -vv */ |
| 1272 | display_build_opts(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1273 | exit(0); |
| 1274 | } |
| 1275 | #if defined(ENABLE_EPOLL) |
| 1276 | else if (*flag == 'd' && flag[1] == 'e') |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1277 | global.tune.options &= ~GTUNE_USE_EPOLL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1278 | #endif |
| 1279 | #if defined(ENABLE_POLL) |
| 1280 | else if (*flag == 'd' && flag[1] == 'p') |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1281 | global.tune.options &= ~GTUNE_USE_POLL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1282 | #endif |
Willy Tarreau | 69cad1a | 2007-04-10 22:45:11 +0200 | [diff] [blame] | 1283 | #if defined(ENABLE_KQUEUE) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 1284 | else if (*flag == 'd' && flag[1] == 'k') |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1285 | global.tune.options &= ~GTUNE_USE_KQUEUE; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 1286 | #endif |
Willy Tarreau | b55932d | 2009-08-16 13:20:32 +0200 | [diff] [blame] | 1287 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
Willy Tarreau | 3ab68cf | 2009-01-25 16:03:28 +0100 | [diff] [blame] | 1288 | else if (*flag == 'd' && flag[1] == 'S') |
| 1289 | global.tune.options &= ~GTUNE_USE_SPLICE; |
| 1290 | #endif |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 1291 | #if defined(USE_GETADDRINFO) |
| 1292 | else if (*flag == 'd' && flag[1] == 'G') |
| 1293 | global.tune.options &= ~GTUNE_USE_GAI; |
| 1294 | #endif |
Lukas Tribus | a0bcbdc | 2016-09-12 21:42:20 +0000 | [diff] [blame] | 1295 | #if defined(SO_REUSEPORT) |
| 1296 | else if (*flag == 'd' && flag[1] == 'R') |
| 1297 | global.tune.options &= ~GTUNE_USE_REUSEPORT; |
| 1298 | #endif |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1299 | else if (*flag == 'd' && flag[1] == 'V') |
| 1300 | global.ssl_server_verify = SSL_SERVER_VERIFY_NONE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1301 | else if (*flag == 'V') |
| 1302 | arg_mode |= MODE_VERBOSE; |
| 1303 | else if (*flag == 'd' && flag[1] == 'b') |
| 1304 | arg_mode |= MODE_FOREGROUND; |
Willy Tarreau | 6e06443 | 2012-05-08 15:40:42 +0200 | [diff] [blame] | 1305 | else if (*flag == 'd' && flag[1] == 'M') |
| 1306 | mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P'; |
Willy Tarreau | 3eed10e | 2016-11-07 21:03:16 +0100 | [diff] [blame] | 1307 | else if (*flag == 'd' && flag[1] == 'r') |
| 1308 | global.tune.options |= GTUNE_RESOLVE_DONTFAIL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1309 | else if (*flag == 'd') |
| 1310 | arg_mode |= MODE_DEBUG; |
| 1311 | else if (*flag == 'c') |
| 1312 | arg_mode |= MODE_CHECK; |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 1313 | else if (*flag == 'D') |
Willy Tarreau | 6bde87b | 2009-05-18 16:29:51 +0200 | [diff] [blame] | 1314 | arg_mode |= MODE_DAEMON; |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 1315 | else if (*flag == 'W') |
| 1316 | arg_mode |= MODE_MWORKER; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1317 | else if (*flag == 'q') |
| 1318 | arg_mode |= MODE_QUIET; |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1319 | else if (*flag == 'x') { |
William Lallemand | 45eff44 | 2017-06-19 15:57:55 +0200 | [diff] [blame] | 1320 | if (argc <= 1 || argv[1][0] == '-') { |
| 1321 | Alert("Unix socket path expected with the -x flag\n\n"); |
| 1322 | usage(progname); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1323 | } |
William Lallemand | 4fc0969 | 2017-06-19 16:37:19 +0200 | [diff] [blame] | 1324 | if (old_unixsocket) |
| 1325 | Warning("-x option already set, overwriting the value\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1326 | old_unixsocket = argv[1]; |
William Lallemand | 4fc0969 | 2017-06-19 16:37:19 +0200 | [diff] [blame] | 1327 | |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1328 | argv++; |
| 1329 | argc--; |
| 1330 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1331 | else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) { |
| 1332 | /* list of pids to finish ('f') or terminate ('t') */ |
| 1333 | |
| 1334 | if (flag[1] == 'f') |
| 1335 | oldpids_sig = SIGUSR1; /* finish then exit */ |
| 1336 | else |
| 1337 | oldpids_sig = SIGTERM; /* terminate immediately */ |
Willy Tarreau | c6ca1aa | 2015-10-08 11:32:32 +0200 | [diff] [blame] | 1338 | while (argc > 1 && argv[1][0] != '-') { |
| 1339 | oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int)); |
| 1340 | if (!oldpids) { |
| 1341 | Alert("Cannot allocate old pid : out of memory.\n"); |
| 1342 | exit(1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1343 | } |
Willy Tarreau | c6ca1aa | 2015-10-08 11:32:32 +0200 | [diff] [blame] | 1344 | argc--; argv++; |
| 1345 | oldpids[nb_oldpids] = atol(*argv); |
| 1346 | if (oldpids[nb_oldpids] <= 0) |
| 1347 | usage(progname); |
| 1348 | nb_oldpids++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1349 | } |
| 1350 | } |
Willy Tarreau | a088d31 | 2015-10-08 11:58:48 +0200 | [diff] [blame] | 1351 | else if (flag[0] == '-' && flag[1] == 0) { /* "--" */ |
| 1352 | /* now that's a cfgfile list */ |
| 1353 | argv++; argc--; |
| 1354 | while (argc > 0) { |
Maxime de Roucy | 0f50392 | 2016-05-13 23:52:55 +0200 | [diff] [blame] | 1355 | if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) { |
| 1356 | Alert("Cannot load configuration file/directory %s : %s\n", |
| 1357 | *argv, |
| 1358 | err_msg); |
Willy Tarreau | a088d31 | 2015-10-08 11:58:48 +0200 | [diff] [blame] | 1359 | exit(1); |
| 1360 | } |
Willy Tarreau | a088d31 | 2015-10-08 11:58:48 +0200 | [diff] [blame] | 1361 | argv++; argc--; |
| 1362 | } |
| 1363 | break; |
| 1364 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1365 | else { /* >=2 args */ |
| 1366 | argv++; argc--; |
| 1367 | if (argc == 0) |
Willy Tarreau | 3bafcdc | 2011-09-10 19:20:23 +0200 | [diff] [blame] | 1368 | usage(progname); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1369 | |
| 1370 | switch (*flag) { |
Willy Tarreau | 576132e | 2011-09-10 19:26:56 +0200 | [diff] [blame] | 1371 | case 'C' : change_dir = *argv; break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1372 | case 'n' : cfg_maxconn = atol(*argv); break; |
Willy Tarreau | 7006045 | 2015-12-14 12:46:07 +0100 | [diff] [blame] | 1373 | case 'm' : global.rlimit_memmax_all = atol(*argv); break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1374 | case 'N' : cfg_maxpconn = atol(*argv); break; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1375 | case 'L' : strncpy(localpeer, *argv, sizeof(localpeer) - 1); break; |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 1376 | case 'f' : |
Maxime de Roucy | 0f50392 | 2016-05-13 23:52:55 +0200 | [diff] [blame] | 1377 | if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) { |
| 1378 | Alert("Cannot load configuration file/directory %s : %s\n", |
| 1379 | *argv, |
| 1380 | err_msg); |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 1381 | exit(1); |
| 1382 | } |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 1383 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1384 | case 'p' : cfg_pidfile = *argv; break; |
Willy Tarreau | 3bafcdc | 2011-09-10 19:20:23 +0200 | [diff] [blame] | 1385 | default: usage(progname); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | else |
Willy Tarreau | 3bafcdc | 2011-09-10 19:20:23 +0200 | [diff] [blame] | 1390 | usage(progname); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1391 | argv++; argc--; |
| 1392 | } |
| 1393 | |
Christopher Faulet | e3a5e35 | 2017-10-24 13:53:54 +0200 | [diff] [blame] | 1394 | global.mode |= (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE |
| 1395 | | MODE_QUIET | MODE_CHECK | MODE_DEBUG)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1396 | |
William Lallemand | cb11fd2 | 2017-06-01 17:38:52 +0200 | [diff] [blame] | 1397 | /* Master workers wait mode */ |
| 1398 | if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_WAIT_ONLY") != NULL)) { |
| 1399 | |
| 1400 | unsetenv("HAPROXY_MWORKER_WAIT_ONLY"); |
| 1401 | mworker_wait(); |
| 1402 | } |
| 1403 | |
| 1404 | if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) { |
| 1405 | atexit_flag = 1; |
| 1406 | atexit(reexec_on_failure); |
| 1407 | } |
| 1408 | |
Willy Tarreau | 576132e | 2011-09-10 19:26:56 +0200 | [diff] [blame] | 1409 | if (change_dir && chdir(change_dir) < 0) { |
| 1410 | Alert("Could not change to directory %s : %s\n", change_dir, strerror(errno)); |
| 1411 | exit(1); |
| 1412 | } |
| 1413 | |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 1414 | /* handle cfgfiles that are actualy directories */ |
| 1415 | cfgfiles_expand_directories(); |
| 1416 | |
| 1417 | if (LIST_ISEMPTY(&cfg_cfgfiles)) |
| 1418 | usage(progname); |
| 1419 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1420 | global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */ |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 1421 | |
| 1422 | init_default_instance(); |
| 1423 | |
Willy Tarreau | 477ecd8 | 2010-01-03 21:12:30 +0100 | [diff] [blame] | 1424 | list_for_each_entry(wl, &cfg_cfgfiles, list) { |
Willy Tarreau | c438242 | 2009-12-06 13:10:44 +0100 | [diff] [blame] | 1425 | int ret; |
| 1426 | |
Willy Tarreau | 477ecd8 | 2010-01-03 21:12:30 +0100 | [diff] [blame] | 1427 | ret = readcfgfile(wl->s); |
Willy Tarreau | c438242 | 2009-12-06 13:10:44 +0100 | [diff] [blame] | 1428 | if (ret == -1) { |
| 1429 | Alert("Could not open configuration file %s : %s\n", |
Willy Tarreau | 477ecd8 | 2010-01-03 21:12:30 +0100 | [diff] [blame] | 1430 | wl->s, strerror(errno)); |
Willy Tarreau | c438242 | 2009-12-06 13:10:44 +0100 | [diff] [blame] | 1431 | exit(1); |
| 1432 | } |
Willy Tarreau | 25a67fa | 2009-12-15 21:46:25 +0100 | [diff] [blame] | 1433 | if (ret & (ERR_ABORT|ERR_FATAL)) |
Willy Tarreau | 477ecd8 | 2010-01-03 21:12:30 +0100 | [diff] [blame] | 1434 | Alert("Error(s) found in configuration file : %s\n", wl->s); |
Willy Tarreau | 25a67fa | 2009-12-15 21:46:25 +0100 | [diff] [blame] | 1435 | err_code |= ret; |
Willy Tarreau | 058e907 | 2009-07-20 09:30:05 +0200 | [diff] [blame] | 1436 | if (err_code & ERR_ABORT) |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 1437 | exit(1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1438 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1439 | |
Willy Tarreau | b83dc3d | 2017-04-19 11:24:07 +0200 | [diff] [blame] | 1440 | /* do not try to resolve arguments nor to spot inconsistencies when |
| 1441 | * the configuration contains fatal errors caused by files not found |
| 1442 | * or failed memory allocations. |
| 1443 | */ |
| 1444 | if (err_code & (ERR_ABORT|ERR_FATAL)) { |
| 1445 | Alert("Fatal errors found in configuration.\n"); |
| 1446 | exit(1); |
| 1447 | } |
| 1448 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1449 | pattern_finalize_config(); |
| 1450 | |
Willy Tarreau | bb92501 | 2009-07-23 13:36:36 +0200 | [diff] [blame] | 1451 | err_code |= check_config_validity(); |
| 1452 | if (err_code & (ERR_ABORT|ERR_FATAL)) { |
| 1453 | Alert("Fatal errors found in configuration.\n"); |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 1454 | exit(1); |
| 1455 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1456 | |
Willy Tarreau | 7006045 | 2015-12-14 12:46:07 +0100 | [diff] [blame] | 1457 | /* recompute the amount of per-process memory depending on nbproc and |
| 1458 | * the shared SSL cache size (allowed to exist in all processes). |
| 1459 | */ |
| 1460 | if (global.rlimit_memmax_all) { |
| 1461 | #if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE) |
| 1462 | int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL; |
| 1463 | |
| 1464 | global.rlimit_memmax = |
| 1465 | ((((int64_t)global.rlimit_memmax_all * 1048576LL) - |
| 1466 | ssl_cache_bytes) / global.nbproc + |
| 1467 | ssl_cache_bytes + 1048575LL) / 1048576LL; |
| 1468 | #else |
| 1469 | global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc; |
| 1470 | #endif |
| 1471 | } |
| 1472 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1473 | #ifdef CONFIG_HAP_NS |
| 1474 | err_code |= netns_init(); |
| 1475 | if (err_code & (ERR_ABORT|ERR_FATAL)) { |
| 1476 | Alert("Failed to initialize namespace support.\n"); |
| 1477 | exit(1); |
| 1478 | } |
| 1479 | #endif |
| 1480 | |
Baptiste Assmann | 4215d7d | 2016-11-02 15:33:15 +0100 | [diff] [blame] | 1481 | /* Apply server states */ |
| 1482 | apply_server_state(); |
| 1483 | |
| 1484 | for (px = proxy; px; px = px->next) |
| 1485 | srv_compute_all_admin_states(px); |
| 1486 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 1487 | /* Apply servers' configured address */ |
| 1488 | err_code |= srv_init_addr(); |
| 1489 | if (err_code & (ERR_ABORT|ERR_FATAL)) { |
| 1490 | Alert("Failed to initialize server(s) addr.\n"); |
| 1491 | exit(1); |
| 1492 | } |
| 1493 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1494 | if (global.mode & MODE_CHECK) { |
Willy Tarreau | 8b15ba1 | 2012-02-02 17:48:18 +0100 | [diff] [blame] | 1495 | struct peers *pr; |
| 1496 | struct proxy *px; |
| 1497 | |
Frédéric Lécaille | ed2b4a6 | 2017-07-13 09:07:09 +0200 | [diff] [blame] | 1498 | for (pr = cfg_peers; pr; pr = pr->next) |
Willy Tarreau | 8b15ba1 | 2012-02-02 17:48:18 +0100 | [diff] [blame] | 1499 | if (pr->peers_fe) |
| 1500 | break; |
| 1501 | |
| 1502 | for (px = proxy; px; px = px->next) |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1503 | if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners)) |
Willy Tarreau | 8b15ba1 | 2012-02-02 17:48:18 +0100 | [diff] [blame] | 1504 | break; |
| 1505 | |
| 1506 | if (pr || px) { |
| 1507 | /* At least one peer or one listener has been found */ |
| 1508 | qfprintf(stdout, "Configuration file is valid\n"); |
| 1509 | exit(0); |
| 1510 | } |
| 1511 | qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n"); |
| 1512 | exit(2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1513 | } |
| 1514 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 1515 | global_listener_queue_task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e9b2602 | 2011-08-01 20:57:55 +0200 | [diff] [blame] | 1516 | if (!global_listener_queue_task) { |
| 1517 | Alert("Out of memory when initializing global task\n"); |
| 1518 | exit(1); |
| 1519 | } |
| 1520 | /* very simple initialization, users will queue the task if needed */ |
| 1521 | global_listener_queue_task->context = NULL; /* not even a context! */ |
| 1522 | global_listener_queue_task->process = manage_global_listener_queue; |
Willy Tarreau | e9b2602 | 2011-08-01 20:57:55 +0200 | [diff] [blame] | 1523 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 1524 | /* now we know the buffer size, we can initialize the channels and buffers */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1525 | init_buffer(); |
Willy Tarreau | 8280d64 | 2009-09-23 23:37:52 +0200 | [diff] [blame] | 1526 | |
Willy Tarreau | e694573 | 2016-12-21 19:57:00 +0100 | [diff] [blame] | 1527 | list_for_each_entry(pcf, &post_check_list, list) { |
| 1528 | err_code |= pcf->fct(); |
| 1529 | if (err_code & (ERR_ABORT|ERR_FATAL)) |
| 1530 | exit(1); |
| 1531 | } |
| 1532 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1533 | if (cfg_maxconn > 0) |
| 1534 | global.maxconn = cfg_maxconn; |
| 1535 | |
| 1536 | if (cfg_pidfile) { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 1537 | free(global.pidfile); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1538 | global.pidfile = strdup(cfg_pidfile); |
| 1539 | } |
| 1540 | |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 1541 | /* Now we want to compute the maxconn and possibly maxsslconn values. |
| 1542 | * It's a bit tricky. If memmax is not set, maxconn defaults to |
| 1543 | * DEFAULT_MAXCONN and maxsslconn defaults to DEFAULT_MAXSSLCONN. |
| 1544 | * |
| 1545 | * If memmax is set, then it depends on which values are set. If |
| 1546 | * maxsslconn is set, we use memmax to determine how many cleartext |
| 1547 | * connections may be added, and set maxconn to the sum of the two. |
| 1548 | * If maxconn is set and not maxsslconn, maxsslconn is computed from |
| 1549 | * the remaining amount of memory between memmax and the cleartext |
| 1550 | * connections. If neither are set, then it is considered that all |
| 1551 | * connections are SSL-capable, and maxconn is computed based on this, |
| 1552 | * then maxsslconn accordingly. We need to know if SSL is used on the |
| 1553 | * frontends, backends, or both, because when it's used on both sides, |
| 1554 | * we need twice the value for maxsslconn, but we only count the |
| 1555 | * handshake once since it is not performed on the two sides at the |
| 1556 | * same time (frontend-side is terminated before backend-side begins). |
| 1557 | * The SSL stack is supposed to have filled ssl_session_cost and |
Willy Tarreau | 474b96a | 2015-01-28 19:03:21 +0100 | [diff] [blame] | 1558 | * ssl_handshake_cost during its initialization. In any case, if |
| 1559 | * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for |
| 1560 | * maxconn in order to protect the system. |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 1561 | */ |
| 1562 | if (!global.rlimit_memmax) { |
| 1563 | if (global.maxconn == 0) { |
| 1564 | global.maxconn = DEFAULT_MAXCONN; |
| 1565 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) |
| 1566 | fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn); |
| 1567 | } |
| 1568 | } |
| 1569 | #ifdef USE_OPENSSL |
| 1570 | else if (!global.maxconn && !global.maxsslconn && |
| 1571 | (global.ssl_used_frontend || global.ssl_used_backend)) { |
| 1572 | /* memmax is set, compute everything automatically. Here we want |
| 1573 | * to ensure that all SSL connections will be served. We take |
| 1574 | * care of the number of sides where SSL is used, and consider |
| 1575 | * the worst case : SSL used on both sides and doing a handshake |
| 1576 | * simultaneously. Note that we can't have more than maxconn |
| 1577 | * handshakes at a time by definition, so for the worst case of |
| 1578 | * two SSL conns per connection, we count a single handshake. |
| 1579 | */ |
| 1580 | int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend; |
| 1581 | int64_t mem = global.rlimit_memmax * 1048576ULL; |
| 1582 | |
| 1583 | mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry |
| 1584 | mem -= global.maxzlibmem; |
| 1585 | mem = mem * MEM_USABLE_RATIO; |
| 1586 | |
| 1587 | global.maxconn = mem / |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1588 | ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 1589 | sides * global.ssl_session_max_cost + // SSL buffers, one per side |
| 1590 | global.ssl_handshake_max_cost); // 1 handshake per connection max |
| 1591 | |
| 1592 | global.maxconn = round_2dig(global.maxconn); |
Willy Tarreau | 474b96a | 2015-01-28 19:03:21 +0100 | [diff] [blame] | 1593 | #ifdef SYSTEM_MAXCONN |
| 1594 | if (global.maxconn > DEFAULT_MAXCONN) |
| 1595 | global.maxconn = DEFAULT_MAXCONN; |
| 1596 | #endif /* SYSTEM_MAXCONN */ |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 1597 | global.maxsslconn = sides * global.maxconn; |
| 1598 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) |
| 1599 | fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n", |
| 1600 | global.maxconn, global.maxsslconn); |
| 1601 | } |
| 1602 | else if (!global.maxsslconn && |
| 1603 | (global.ssl_used_frontend || global.ssl_used_backend)) { |
| 1604 | /* memmax and maxconn are known, compute maxsslconn automatically. |
| 1605 | * maxsslconn being forced, we don't know how many of it will be |
| 1606 | * on each side if both sides are being used. The worst case is |
| 1607 | * when all connections use only one SSL instance because |
| 1608 | * handshakes may be on two sides at the same time. |
| 1609 | */ |
| 1610 | int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend; |
| 1611 | int64_t mem = global.rlimit_memmax * 1048576ULL; |
| 1612 | int64_t sslmem; |
| 1613 | |
| 1614 | mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry |
| 1615 | mem -= global.maxzlibmem; |
| 1616 | mem = mem * MEM_USABLE_RATIO; |
| 1617 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1618 | sslmem = mem - global.maxconn * (int64_t)(STREAM_MAX_COST + 2 * global.tune.bufsize); |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 1619 | global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost); |
| 1620 | global.maxsslconn = round_2dig(global.maxsslconn); |
| 1621 | |
| 1622 | if (sslmem <= 0 || global.maxsslconn < sides) { |
| 1623 | Alert("Cannot compute the automatic maxsslconn because global.maxconn is already too " |
| 1624 | "high for the global.memmax value (%d MB). The absolute maximum possible value " |
| 1625 | "without SSL is %d, but %d was found and SSL is in use.\n", |
| 1626 | global.rlimit_memmax, |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1627 | (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)), |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 1628 | global.maxconn); |
| 1629 | exit(1); |
| 1630 | } |
| 1631 | |
| 1632 | if (global.maxsslconn > sides * global.maxconn) |
| 1633 | global.maxsslconn = sides * global.maxconn; |
| 1634 | |
| 1635 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) |
| 1636 | fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn); |
| 1637 | } |
| 1638 | #endif |
| 1639 | else if (!global.maxconn) { |
| 1640 | /* memmax and maxsslconn are known/unused, compute maxconn automatically */ |
| 1641 | int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend; |
| 1642 | int64_t mem = global.rlimit_memmax * 1048576ULL; |
| 1643 | int64_t clearmem; |
| 1644 | |
| 1645 | if (global.ssl_used_frontend || global.ssl_used_backend) |
| 1646 | mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry |
| 1647 | |
| 1648 | mem -= global.maxzlibmem; |
| 1649 | mem = mem * MEM_USABLE_RATIO; |
| 1650 | |
| 1651 | clearmem = mem; |
| 1652 | if (sides) |
| 1653 | clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn; |
| 1654 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1655 | global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize); |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 1656 | global.maxconn = round_2dig(global.maxconn); |
Willy Tarreau | 474b96a | 2015-01-28 19:03:21 +0100 | [diff] [blame] | 1657 | #ifdef SYSTEM_MAXCONN |
| 1658 | if (global.maxconn > DEFAULT_MAXCONN) |
| 1659 | global.maxconn = DEFAULT_MAXCONN; |
| 1660 | #endif /* SYSTEM_MAXCONN */ |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 1661 | |
| 1662 | if (clearmem <= 0 || !global.maxconn) { |
| 1663 | Alert("Cannot compute the automatic maxconn because global.maxsslconn is already too " |
| 1664 | "high for the global.memmax value (%d MB). The absolute maximum possible value " |
| 1665 | "is %d, but %d was found.\n", |
| 1666 | global.rlimit_memmax, |
| 1667 | (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)), |
| 1668 | global.maxsslconn); |
| 1669 | exit(1); |
| 1670 | } |
| 1671 | |
| 1672 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) { |
| 1673 | if (sides && global.maxsslconn > sides * global.maxconn) { |
| 1674 | fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn " |
| 1675 | "to be limited to %d. Better reduce global.maxsslconn to get more " |
| 1676 | "room for extra connections.\n", global.maxsslconn, global.maxconn); |
| 1677 | } |
| 1678 | fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn); |
| 1679 | } |
| 1680 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1681 | |
Willy Tarreau | 66aa61f | 2009-01-18 21:44:07 +0100 | [diff] [blame] | 1682 | if (!global.maxpipes) { |
| 1683 | /* maxpipes not specified. Count how many frontends and backends |
| 1684 | * may be using splicing, and bound that to maxconn. |
| 1685 | */ |
| 1686 | struct proxy *cur; |
| 1687 | int nbfe = 0, nbbe = 0; |
| 1688 | |
| 1689 | for (cur = proxy; cur; cur = cur->next) { |
| 1690 | if (cur->options2 & (PR_O2_SPLIC_ANY)) { |
| 1691 | if (cur->cap & PR_CAP_FE) |
| 1692 | nbfe += cur->maxconn; |
| 1693 | if (cur->cap & PR_CAP_BE) |
Willy Tarreau | afb4876 | 2009-01-25 10:42:05 +0100 | [diff] [blame] | 1694 | nbbe += cur->fullconn ? cur->fullconn : global.maxconn; |
Willy Tarreau | 66aa61f | 2009-01-18 21:44:07 +0100 | [diff] [blame] | 1695 | } |
| 1696 | } |
| 1697 | global.maxpipes = MAX(nbfe, nbbe); |
| 1698 | if (global.maxpipes > global.maxconn) |
| 1699 | global.maxpipes = global.maxconn; |
Willy Tarreau | 686ac82 | 2009-01-25 14:06:58 +0100 | [diff] [blame] | 1700 | global.maxpipes /= 4; |
Willy Tarreau | 66aa61f | 2009-01-18 21:44:07 +0100 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | |
Willy Tarreau | abacc2c | 2011-09-07 14:26:33 +0200 | [diff] [blame] | 1704 | global.hardmaxconn = global.maxconn; /* keep this max value */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1705 | global.maxsock += global.maxconn * 2; /* each connection needs two sockets */ |
Willy Tarreau | 3ec79b9 | 2009-01-18 20:39:42 +0100 | [diff] [blame] | 1706 | global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1707 | |
Willy Tarreau | 3c63fd8 | 2011-09-07 18:00:47 +0200 | [diff] [blame] | 1708 | if (global.stats_fe) |
| 1709 | global.maxsock += global.stats_fe->maxconn; |
| 1710 | |
Frédéric Lécaille | ed2b4a6 | 2017-07-13 09:07:09 +0200 | [diff] [blame] | 1711 | if (cfg_peers) { |
Willy Tarreau | 3c63fd8 | 2011-09-07 18:00:47 +0200 | [diff] [blame] | 1712 | /* peers also need to bypass global maxconn */ |
Frédéric Lécaille | ed2b4a6 | 2017-07-13 09:07:09 +0200 | [diff] [blame] | 1713 | struct peers *p = cfg_peers; |
Willy Tarreau | 3c63fd8 | 2011-09-07 18:00:47 +0200 | [diff] [blame] | 1714 | |
Frédéric Lécaille | ed2b4a6 | 2017-07-13 09:07:09 +0200 | [diff] [blame] | 1715 | for (p = cfg_peers; p; p = p->next) |
Willy Tarreau | 3c63fd8 | 2011-09-07 18:00:47 +0200 | [diff] [blame] | 1716 | if (p->peers_fe) |
| 1717 | global.maxsock += p->peers_fe->maxconn; |
| 1718 | } |
| 1719 | |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 1720 | if (global.tune.maxpollevents <= 0) |
| 1721 | global.tune.maxpollevents = MAX_POLL_EVENTS; |
| 1722 | |
Willy Tarreau | 6f4a82c | 2009-03-21 20:43:57 +0100 | [diff] [blame] | 1723 | if (global.tune.recv_enough == 0) |
| 1724 | global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH; |
| 1725 | |
Willy Tarreau | 2709784 | 2015-09-28 13:53:23 +0200 | [diff] [blame] | 1726 | if (global.tune.maxrewrite < 0) |
| 1727 | global.tune.maxrewrite = MAXREWRITE; |
| 1728 | |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 1729 | if (global.tune.maxrewrite >= global.tune.bufsize / 2) |
| 1730 | global.tune.maxrewrite = global.tune.bufsize / 2; |
| 1731 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1732 | if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) { |
| 1733 | /* command line debug mode inhibits configuration mode */ |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 1734 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
Willy Tarreau | 772f0dd | 2012-10-26 16:04:28 +0200 | [diff] [blame] | 1735 | global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)); |
| 1736 | } |
| 1737 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 1738 | if (arg_mode & MODE_DAEMON) { |
Willy Tarreau | 772f0dd | 2012-10-26 16:04:28 +0200 | [diff] [blame] | 1739 | /* command line daemon mode inhibits foreground and debug modes mode */ |
| 1740 | global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND); |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 1741 | global.mode |= arg_mode & MODE_DAEMON; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1742 | } |
Willy Tarreau | 772f0dd | 2012-10-26 16:04:28 +0200 | [diff] [blame] | 1743 | |
| 1744 | global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1745 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 1746 | if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) { |
| 1747 | Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n"); |
| 1748 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1749 | } |
| 1750 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 1751 | if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1752 | if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG))) |
William Lallemand | cc11382 | 2017-11-06 11:00:02 +0100 | [diff] [blame] | 1753 | Warning("<nbproc> is only meaningful in daemon mode or master-worker mode. Setting limit to 1 process.\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1754 | global.nbproc = 1; |
| 1755 | } |
| 1756 | |
| 1757 | if (global.nbproc < 1) |
| 1758 | global.nbproc = 1; |
| 1759 | |
Christopher Faulet | be0faa2 | 2017-08-29 15:37:10 +0200 | [diff] [blame] | 1760 | if (global.nbthread < 1) |
| 1761 | global.nbthread = 1; |
| 1762 | |
Christopher Faulet | 3ef2639 | 2017-08-29 16:46:57 +0200 | [diff] [blame] | 1763 | /* Realloc trash buffers because global.tune.bufsize may have changed */ |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 1764 | if (!init_trash_buffers(0)) { |
Christopher Faulet | 3ef2639 | 2017-08-29 16:46:57 +0200 | [diff] [blame] | 1765 | Alert("failed to initialize trash buffers.\n"); |
| 1766 | exit(1); |
| 1767 | } |
| 1768 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 1769 | /* |
| 1770 | * Note: we could register external pollers here. |
| 1771 | * Built-in pollers have been registered before main(). |
| 1772 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1773 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1774 | if (!(global.tune.options & GTUNE_USE_KQUEUE)) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 1775 | disable_poller("kqueue"); |
| 1776 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1777 | if (!(global.tune.options & GTUNE_USE_EPOLL)) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1778 | disable_poller("epoll"); |
| 1779 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1780 | if (!(global.tune.options & GTUNE_USE_POLL)) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1781 | disable_poller("poll"); |
| 1782 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1783 | if (!(global.tune.options & GTUNE_USE_SELECT)) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1784 | disable_poller("select"); |
| 1785 | |
| 1786 | /* Note: we could disable any poller by name here */ |
| 1787 | |
Christopher Faulet | b3f4e14 | 2016-03-07 12:46:38 +0100 | [diff] [blame] | 1788 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 1789 | list_pollers(stderr); |
Christopher Faulet | b3f4e14 | 2016-03-07 12:46:38 +0100 | [diff] [blame] | 1790 | fprintf(stderr, "\n"); |
| 1791 | list_filters(stderr); |
| 1792 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 1793 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1794 | if (!init_pollers()) { |
Willy Tarreau | 3fa87b1 | 2013-03-31 14:41:15 +0200 | [diff] [blame] | 1795 | Alert("No polling mechanism available.\n" |
| 1796 | " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n" |
| 1797 | " is too low on this platform to support maxconn and the number of listeners\n" |
| 1798 | " and servers. You should rebuild haproxy specifying your system using TARGET=\n" |
| 1799 | " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n" |
Prach Pongpanich | b837e68 | 2013-05-14 20:56:28 +0200 | [diff] [blame] | 1800 | " global maxconn setting to accommodate the system's limitation. For reference,\n" |
Willy Tarreau | 3fa87b1 | 2013-03-31 14:41:15 +0200 | [diff] [blame] | 1801 | " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n" |
| 1802 | " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n" |
| 1803 | " check build settings using 'haproxy -vv'.\n\n", |
| 1804 | FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1805 | exit(1); |
| 1806 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 1807 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) { |
| 1808 | printf("Using %s() as the polling mechanism.\n", cur_poller.name); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1809 | } |
| 1810 | |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 1811 | if (!global.node) |
| 1812 | global.node = strdup(hostname); |
| 1813 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 1814 | if (!hlua_post_init()) |
| 1815 | exit(1); |
Thomas Holmes | 6abded4 | 2015-05-12 16:23:58 +0100 | [diff] [blame] | 1816 | |
Maxime de Roucy | 0f50392 | 2016-05-13 23:52:55 +0200 | [diff] [blame] | 1817 | free(err_msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1818 | } |
| 1819 | |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 1820 | static void deinit_acl_cond(struct acl_cond *cond) |
Simon Horman | ac82142 | 2011-07-15 13:14:09 +0900 | [diff] [blame] | 1821 | { |
Simon Horman | ac82142 | 2011-07-15 13:14:09 +0900 | [diff] [blame] | 1822 | struct acl_term_suite *suite, *suiteb; |
| 1823 | struct acl_term *term, *termb; |
| 1824 | |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 1825 | if (!cond) |
| 1826 | return; |
| 1827 | |
| 1828 | list_for_each_entry_safe(suite, suiteb, &cond->suites, list) { |
| 1829 | list_for_each_entry_safe(term, termb, &suite->terms, list) { |
| 1830 | LIST_DEL(&term->list); |
| 1831 | free(term); |
Simon Horman | ac82142 | 2011-07-15 13:14:09 +0900 | [diff] [blame] | 1832 | } |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 1833 | LIST_DEL(&suite->list); |
| 1834 | free(suite); |
| 1835 | } |
| 1836 | |
| 1837 | free(cond); |
| 1838 | } |
| 1839 | |
| 1840 | static void deinit_tcp_rules(struct list *rules) |
| 1841 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 1842 | struct act_rule *trule, *truleb; |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 1843 | |
| 1844 | list_for_each_entry_safe(trule, truleb, rules, list) { |
Simon Horman | ac82142 | 2011-07-15 13:14:09 +0900 | [diff] [blame] | 1845 | LIST_DEL(&trule->list); |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 1846 | deinit_acl_cond(trule->cond); |
Simon Horman | ac82142 | 2011-07-15 13:14:09 +0900 | [diff] [blame] | 1847 | free(trule); |
| 1848 | } |
| 1849 | } |
| 1850 | |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 1851 | static void deinit_stick_rules(struct list *rules) |
| 1852 | { |
| 1853 | struct sticking_rule *rule, *ruleb; |
| 1854 | |
| 1855 | list_for_each_entry_safe(rule, ruleb, rules, list) { |
| 1856 | LIST_DEL(&rule->list); |
| 1857 | deinit_acl_cond(rule->cond); |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 1858 | release_sample_expr(rule->expr); |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 1859 | free(rule); |
| 1860 | } |
| 1861 | } |
| 1862 | |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 1863 | void deinit(void) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1864 | { |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 1865 | struct proxy *p = proxy, *p0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1866 | struct cap_hdr *h,*h_next; |
| 1867 | struct server *s,*s_next; |
| 1868 | struct listener *l,*l_next; |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 1869 | struct acl_cond *cond, *condb; |
| 1870 | struct hdr_exp *exp, *expb; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 1871 | struct acl *acl, *aclb; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 1872 | struct switching_rule *rule, *ruleb; |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 1873 | struct server_rule *srule, *sruleb; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1874 | struct redirect_rule *rdr, *rdrb; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 1875 | struct wordlist *wl, *wlb; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 1876 | struct cond_wordlist *cwl, *cwlb; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 1877 | struct uri_auth *uap, *ua = NULL; |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 1878 | struct logsrv *log, *logb; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 1879 | struct logformat_node *lf, *lfb; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1880 | struct bind_conf *bind_conf, *bind_back; |
Willy Tarreau | cdb737e | 2016-12-21 18:43:10 +0100 | [diff] [blame] | 1881 | struct build_opts_str *bol, *bolb; |
Willy Tarreau | 05554e6 | 2016-12-21 20:46:26 +0100 | [diff] [blame] | 1882 | struct post_deinit_fct *pdf; |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 1883 | int i; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 1884 | |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 1885 | deinit_signals(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1886 | while (p) { |
Willy Tarreau | 8113a5d | 2012-10-04 08:01:43 +0200 | [diff] [blame] | 1887 | free(p->conf.file); |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 1888 | free(p->id); |
| 1889 | free(p->check_req); |
| 1890 | free(p->cookie_name); |
| 1891 | free(p->cookie_domain); |
| 1892 | free(p->url_param_name); |
| 1893 | free(p->capture_name); |
| 1894 | free(p->monitor_uri); |
Simon Horman | a31c7f7 | 2011-07-15 13:14:08 +0900 | [diff] [blame] | 1895 | free(p->rdp_cookie_name); |
Willy Tarreau | 62a6123 | 2013-04-12 18:13:46 +0200 | [diff] [blame] | 1896 | if (p->conf.logformat_string != default_http_log_format && |
| 1897 | p->conf.logformat_string != default_tcp_log_format && |
| 1898 | p->conf.logformat_string != clf_http_log_format) |
| 1899 | free(p->conf.logformat_string); |
Willy Tarreau | 196729e | 2012-05-31 19:30:26 +0200 | [diff] [blame] | 1900 | |
Willy Tarreau | 62a6123 | 2013-04-12 18:13:46 +0200 | [diff] [blame] | 1901 | free(p->conf.lfs_file); |
| 1902 | free(p->conf.uniqueid_format_string); |
| 1903 | free(p->conf.uif_file); |
Godbach | af1a75d | 2013-10-02 17:10:11 +0800 | [diff] [blame] | 1904 | free(p->lbprm.map.srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1905 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1906 | if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format) |
| 1907 | free(p->conf.logformat_sd_string); |
| 1908 | free(p->conf.lfsd_file); |
| 1909 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 1910 | for (i = 0; i < HTTP_ERR_SIZE; i++) |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 1911 | chunk_destroy(&p->errmsg[i]); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1912 | |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 1913 | list_for_each_entry_safe(cwl, cwlb, &p->req_add, list) { |
| 1914 | LIST_DEL(&cwl->list); |
| 1915 | free(cwl->s); |
| 1916 | free(cwl); |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 1917 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1918 | |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 1919 | list_for_each_entry_safe(cwl, cwlb, &p->rsp_add, list) { |
| 1920 | LIST_DEL(&cwl->list); |
| 1921 | free(cwl->s); |
| 1922 | free(cwl); |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 1923 | } |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 1924 | |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1925 | list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) { |
| 1926 | LIST_DEL(&cond->list); |
| 1927 | prune_acl_cond(cond); |
| 1928 | free(cond); |
| 1929 | } |
| 1930 | |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 1931 | for (exp = p->req_exp; exp != NULL; ) { |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 1932 | if (exp->preg) { |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 1933 | regex_free(exp->preg); |
| 1934 | free(exp->preg); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 1935 | } |
| 1936 | |
Willy Tarreau | 98d0485 | 2015-05-26 12:18:29 +0200 | [diff] [blame] | 1937 | free((char *)exp->replace); |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 1938 | expb = exp; |
| 1939 | exp = exp->next; |
| 1940 | free(expb); |
| 1941 | } |
| 1942 | |
| 1943 | for (exp = p->rsp_exp; exp != NULL; ) { |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 1944 | if (exp->preg) { |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 1945 | regex_free(exp->preg); |
| 1946 | free(exp->preg); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 1947 | } |
| 1948 | |
Willy Tarreau | 98d0485 | 2015-05-26 12:18:29 +0200 | [diff] [blame] | 1949 | free((char *)exp->replace); |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 1950 | expb = exp; |
| 1951 | exp = exp->next; |
| 1952 | free(expb); |
| 1953 | } |
| 1954 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 1955 | /* build a list of unique uri_auths */ |
| 1956 | if (!ua) |
| 1957 | ua = p->uri_auth; |
| 1958 | else { |
| 1959 | /* check if p->uri_auth is unique */ |
| 1960 | for (uap = ua; uap; uap=uap->next) |
| 1961 | if (uap == p->uri_auth) |
| 1962 | break; |
| 1963 | |
Willy Tarreau | accc4e1 | 2008-06-24 11:14:45 +0200 | [diff] [blame] | 1964 | if (!uap && p->uri_auth) { |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 1965 | /* add it, if it is */ |
| 1966 | p->uri_auth->next = ua; |
| 1967 | ua = p->uri_auth; |
| 1968 | } |
| 1969 | } |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 1970 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 1971 | list_for_each_entry_safe(acl, aclb, &p->acl, list) { |
| 1972 | LIST_DEL(&acl->list); |
| 1973 | prune_acl(acl); |
| 1974 | free(acl); |
| 1975 | } |
| 1976 | |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 1977 | list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) { |
| 1978 | LIST_DEL(&srule->list); |
| 1979 | prune_acl_cond(srule->cond); |
| 1980 | free(srule->cond); |
| 1981 | free(srule); |
| 1982 | } |
| 1983 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 1984 | list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) { |
| 1985 | LIST_DEL(&rule->list); |
Willy Tarreau | f51658d | 2014-04-23 01:21:56 +0200 | [diff] [blame] | 1986 | if (rule->cond) { |
| 1987 | prune_acl_cond(rule->cond); |
| 1988 | free(rule->cond); |
Thierry FOURNIER / OZON.IO | 4ed1c95 | 2016-11-24 23:57:54 +0100 | [diff] [blame] | 1989 | free(rule->file); |
Willy Tarreau | f51658d | 2014-04-23 01:21:56 +0200 | [diff] [blame] | 1990 | } |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 1991 | free(rule); |
| 1992 | } |
| 1993 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1994 | list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) { |
| 1995 | LIST_DEL(&rdr->list); |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 1996 | if (rdr->cond) { |
| 1997 | prune_acl_cond(rdr->cond); |
| 1998 | free(rdr->cond); |
| 1999 | } |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2000 | free(rdr->rdr_str); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2001 | list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) { |
| 2002 | LIST_DEL(&lf->list); |
| 2003 | free(lf); |
| 2004 | } |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2005 | free(rdr); |
| 2006 | } |
| 2007 | |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 2008 | list_for_each_entry_safe(log, logb, &p->logsrvs, list) { |
| 2009 | LIST_DEL(&log->list); |
| 2010 | free(log); |
| 2011 | } |
| 2012 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 2013 | list_for_each_entry_safe(lf, lfb, &p->logformat, list) { |
| 2014 | LIST_DEL(&lf->list); |
| 2015 | free(lf); |
| 2016 | } |
| 2017 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2018 | list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) { |
| 2019 | LIST_DEL(&lf->list); |
| 2020 | free(lf); |
| 2021 | } |
| 2022 | |
Simon Horman | ac82142 | 2011-07-15 13:14:09 +0900 | [diff] [blame] | 2023 | deinit_tcp_rules(&p->tcp_req.inspect_rules); |
| 2024 | deinit_tcp_rules(&p->tcp_req.l4_rules); |
| 2025 | |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 2026 | deinit_stick_rules(&p->storersp_rules); |
| 2027 | deinit_stick_rules(&p->sticking_rules); |
| 2028 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2029 | h = p->req_cap; |
| 2030 | while (h) { |
| 2031 | h_next = h->next; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2032 | free(h->name); |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 2033 | pool_destroy2(h->pool); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2034 | free(h); |
| 2035 | h = h_next; |
| 2036 | }/* end while(h) */ |
| 2037 | |
| 2038 | h = p->rsp_cap; |
| 2039 | while (h) { |
| 2040 | h_next = h->next; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2041 | free(h->name); |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 2042 | pool_destroy2(h->pool); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2043 | free(h); |
| 2044 | h = h_next; |
| 2045 | }/* end while(h) */ |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2046 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2047 | s = p->srv; |
| 2048 | while (s) { |
| 2049 | s_next = s->next; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2050 | |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 2051 | if (s->check.task) { |
| 2052 | task_delete(s->check.task); |
| 2053 | task_free(s->check.task); |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2054 | } |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2055 | if (s->agent.task) { |
| 2056 | task_delete(s->agent.task); |
| 2057 | task_free(s->agent.task); |
| 2058 | } |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2059 | |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 2060 | if (s->warmup) { |
| 2061 | task_delete(s->warmup); |
| 2062 | task_free(s->warmup); |
| 2063 | } |
| 2064 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2065 | free(s->id); |
| 2066 | free(s->cookie); |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 2067 | free(s->check.bi); |
| 2068 | free(s->check.bo); |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2069 | free(s->agent.bi); |
| 2070 | free(s->agent.bo); |
James Brown | 55f9ff1 | 2015-10-21 18:19:05 -0700 | [diff] [blame] | 2071 | free(s->agent.send_string); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2072 | free(s->hostname_dn); |
Sárközi, László | 34c0179 | 2014-09-05 10:08:23 +0200 | [diff] [blame] | 2073 | free((char*)s->conf.file); |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 2074 | |
| 2075 | if (s->use_ssl || s->check.use_ssl) { |
| 2076 | if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv) |
| 2077 | xprt_get(XPRT_SSL)->destroy_srv(s); |
| 2078 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2079 | HA_SPIN_DESTROY(&s->lock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2080 | free(s); |
| 2081 | s = s_next; |
| 2082 | }/* end while(s) */ |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2083 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2084 | list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) { |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 2085 | /* |
| 2086 | * Zombie proxy, the listener just pretend to be up |
| 2087 | * because they still hold an opened fd. |
| 2088 | * Close it and give the listener its real state. |
| 2089 | */ |
| 2090 | if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) { |
| 2091 | close(l->fd); |
| 2092 | l->state = LI_INIT; |
| 2093 | } |
Willy Tarreau | f6e2cc7 | 2010-09-03 10:38:17 +0200 | [diff] [blame] | 2094 | unbind_listener(l); |
| 2095 | delete_listener(l); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2096 | LIST_DEL(&l->by_fe); |
| 2097 | LIST_DEL(&l->by_bind); |
Krzysztof Piotr Oledzki | aff01ea | 2010-02-05 20:31:44 +0100 | [diff] [blame] | 2098 | free(l->name); |
| 2099 | free(l->counters); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2100 | free(l); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2101 | } |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2102 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2103 | /* Release unused SSL configs. */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2104 | list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 2105 | if (bind_conf->xprt->destroy_bind_conf) |
| 2106 | bind_conf->xprt->destroy_bind_conf(bind_conf); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2107 | free(bind_conf->file); |
| 2108 | free(bind_conf->arg); |
| 2109 | LIST_DEL(&bind_conf->by_fe); |
| 2110 | free(bind_conf); |
| 2111 | } |
Willy Tarreau | f5ae8f7 | 2012-09-07 16:58:00 +0200 | [diff] [blame] | 2112 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2113 | flt_deinit(p); |
| 2114 | |
Krzysztof Piotr Oledzki | aff01ea | 2010-02-05 20:31:44 +0100 | [diff] [blame] | 2115 | free(p->desc); |
| 2116 | free(p->fwdfor_hdr_name); |
| 2117 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2118 | free_http_req_rules(&p->http_req_rules); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2119 | free_http_res_rules(&p->http_res_rules); |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 2120 | free(p->task); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2121 | |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 2122 | pool_destroy2(p->req_cap_pool); |
| 2123 | pool_destroy2(p->rsp_cap_pool); |
Simon Horman | b08584a | 2011-07-15 13:14:10 +0900 | [diff] [blame] | 2124 | pool_destroy2(p->table.pool); |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 2125 | |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 2126 | p0 = p; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2127 | p = p->next; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2128 | HA_SPIN_DESTROY(&p0->lbprm.lock); |
| 2129 | HA_SPIN_DESTROY(&p0->lock); |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 2130 | free(p0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2131 | }/* end while(p) */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 2132 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2133 | while (ua) { |
| 2134 | uap = ua; |
| 2135 | ua = ua->next; |
| 2136 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2137 | free(uap->uri_prefix); |
| 2138 | free(uap->auth_realm); |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 2139 | free(uap->node); |
| 2140 | free(uap->desc); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2141 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2142 | userlist_free(uap->userlist); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2143 | free_http_req_rules(&uap->http_req_rules); |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2144 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2145 | free(uap); |
| 2146 | } |
| 2147 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 2148 | userlist_free(userlist); |
| 2149 | |
David Carlier | 834cb2e | 2015-09-25 12:02:25 +0100 | [diff] [blame] | 2150 | cfg_unregister_sections(); |
| 2151 | |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 2152 | deinit_log_buffers(); |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 2153 | deinit_trash_buffers(); |
David Carlier | 834cb2e | 2015-09-25 12:02:25 +0100 | [diff] [blame] | 2154 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 2155 | protocol_unbind_all(); |
| 2156 | |
Willy Tarreau | 05554e6 | 2016-12-21 20:46:26 +0100 | [diff] [blame] | 2157 | list_for_each_entry(pdf, &post_deinit_list, list) |
| 2158 | pdf->fct(); |
| 2159 | |
Joe Williams | df5b38f | 2010-12-29 17:05:48 +0100 | [diff] [blame] | 2160 | free(global.log_send_hostname); global.log_send_hostname = NULL; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 2161 | chunk_destroy(&global.log_tag); |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2162 | free(global.chroot); global.chroot = NULL; |
| 2163 | free(global.pidfile); global.pidfile = NULL; |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 2164 | free(global.node); global.node = NULL; |
| 2165 | free(global.desc); global.desc = NULL; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2166 | free(oldpids); oldpids = NULL; |
Willy Tarreau | e9b2602 | 2011-08-01 20:57:55 +0200 | [diff] [blame] | 2167 | free(global_listener_queue_task); global_listener_queue_task = NULL; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2168 | |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 2169 | list_for_each_entry_safe(log, logb, &global.logsrvs, list) { |
| 2170 | LIST_DEL(&log->list); |
| 2171 | free(log); |
| 2172 | } |
Willy Tarreau | 477ecd8 | 2010-01-03 21:12:30 +0100 | [diff] [blame] | 2173 | list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) { |
Maxime de Roucy | 0f50392 | 2016-05-13 23:52:55 +0200 | [diff] [blame] | 2174 | free(wl->s); |
Willy Tarreau | 477ecd8 | 2010-01-03 21:12:30 +0100 | [diff] [blame] | 2175 | LIST_DEL(&wl->list); |
| 2176 | free(wl); |
| 2177 | } |
| 2178 | |
Willy Tarreau | cdb737e | 2016-12-21 18:43:10 +0100 | [diff] [blame] | 2179 | list_for_each_entry_safe(bol, bolb, &build_opts_list, list) { |
| 2180 | if (bol->must_free) |
| 2181 | free((void *)bol->str); |
| 2182 | LIST_DEL(&bol->list); |
| 2183 | free(bol); |
| 2184 | } |
| 2185 | |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 2186 | vars_prune(&global.vars, NULL, NULL); |
| 2187 | |
Christopher Faulet | ad405f1 | 2017-08-29 15:30:11 +0200 | [diff] [blame] | 2188 | deinit_buffer(); |
| 2189 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2190 | pool_destroy2(pool2_stream); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2191 | pool_destroy2(pool2_session); |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 2192 | pool_destroy2(pool2_connection); |
Olivier Houchard | e2b40b9 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2193 | pool_destroy2(pool2_connstream); |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 2194 | pool_destroy2(pool2_requri); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 2195 | pool_destroy2(pool2_task); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 2196 | pool_destroy2(pool2_capture); |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 2197 | pool_destroy2(pool2_pendconn); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 2198 | pool_destroy2(pool2_sig_handlers); |
Willy Tarreau | 34eb671 | 2011-10-24 18:15:04 +0200 | [diff] [blame] | 2199 | pool_destroy2(pool2_hdr_idx); |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 2200 | pool_destroy2(pool2_http_txn); |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2201 | deinit_pollers(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2202 | } /* end deinit() */ |
| 2203 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 2204 | void mworker_pipe_handler(int fd) |
| 2205 | { |
| 2206 | char c; |
| 2207 | |
| 2208 | while (read(fd, &c, 1) == -1) { |
| 2209 | if (errno == EINTR) |
| 2210 | continue; |
| 2211 | if (errno == EAGAIN) { |
| 2212 | fd_cant_recv(fd); |
| 2213 | return; |
| 2214 | } |
| 2215 | break; |
| 2216 | } |
| 2217 | |
| 2218 | deinit(); |
| 2219 | exit(EXIT_FAILURE); |
| 2220 | return; |
| 2221 | } |
| 2222 | |
| 2223 | void mworker_pipe_register(int pipefd[2]) |
| 2224 | { |
| 2225 | close(mworker_pipe[1]); /* close the write end of the master pipe in the children */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2226 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 2227 | fcntl(mworker_pipe[0], F_SETFL, O_NONBLOCK); |
| 2228 | fdtab[mworker_pipe[0]].owner = mworker_pipe; |
| 2229 | fdtab[mworker_pipe[0]].iocb = mworker_pipe_handler; |
Christopher Faulet | 36716a7 | 2017-05-30 11:07:16 +0200 | [diff] [blame] | 2230 | fd_insert(mworker_pipe[0], MAX_THREADS_MASK); |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 2231 | fd_want_recv(mworker_pipe[0]); |
| 2232 | } |
Christopher Faulet | dc628a3 | 2017-10-19 11:59:44 +0200 | [diff] [blame] | 2233 | |
| 2234 | static void sync_poll_loop() |
| 2235 | { |
| 2236 | if (THREAD_NO_SYNC()) |
| 2237 | return; |
| 2238 | |
| 2239 | THREAD_ENTER_SYNC(); |
| 2240 | |
| 2241 | if (!THREAD_NEED_SYNC()) |
| 2242 | goto exit; |
| 2243 | |
| 2244 | /* *** { */ |
| 2245 | /* Put here all sync functions */ |
| 2246 | |
Christopher Faulet | 5d42e09 | 2017-10-16 12:00:40 +0200 | [diff] [blame] | 2247 | servers_update_status(); /* Commit server status changes */ |
Christopher Faulet | dc628a3 | 2017-10-19 11:59:44 +0200 | [diff] [blame] | 2248 | |
| 2249 | /* *** } */ |
| 2250 | exit: |
| 2251 | THREAD_EXIT_SYNC(); |
| 2252 | } |
| 2253 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 2254 | /* Runs the polling loop */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 2255 | static void run_poll_loop() |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2256 | { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2257 | int next; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2258 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 2259 | tv_update_date(0,1); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2260 | while (1) { |
Thierry FOURNIER | 9cf7c4b | 2014-12-15 13:26:01 +0100 | [diff] [blame] | 2261 | /* Process a few tasks */ |
| 2262 | process_runnable_tasks(); |
| 2263 | |
Willy Tarreau | 2985794 | 2009-05-10 09:01:21 +0200 | [diff] [blame] | 2264 | /* check if we caught some signals and process them */ |
| 2265 | signal_process_queue(); |
| 2266 | |
Willy Tarreau | 58b458d | 2008-06-29 22:40:23 +0200 | [diff] [blame] | 2267 | /* Check if we can expire some tasks */ |
Thierry FOURNIER | 9cf7c4b | 2014-12-15 13:26:01 +0100 | [diff] [blame] | 2268 | next = wake_expired_tasks(); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2269 | |
Willy Tarreau | af7ad00 | 2010-08-31 15:39:26 +0200 | [diff] [blame] | 2270 | /* stop when there's nothing left to do */ |
| 2271 | if (jobs == 0) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2272 | break; |
| 2273 | |
Willy Tarreau | 10146c9 | 2015-04-13 20:44:19 +0200 | [diff] [blame] | 2274 | /* expire immediately if events are pending */ |
Christopher Faulet | 34c5cc9 | 2016-12-06 09:15:30 +0100 | [diff] [blame] | 2275 | if (fd_cache_num || tasks_run_queue || signal_queue_len || applets_active_queue) |
Willy Tarreau | 10146c9 | 2015-04-13 20:44:19 +0200 | [diff] [blame] | 2276 | next = now_ms; |
| 2277 | |
Willy Tarreau | 58b458d | 2008-06-29 22:40:23 +0200 | [diff] [blame] | 2278 | /* The poller will ensure it returns around <next> */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2279 | cur_poller.poll(&cur_poller, next); |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 2280 | fd_process_cached_events(); |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 2281 | applet_run_active(); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 2282 | |
Christopher Faulet | dc628a3 | 2017-10-19 11:59:44 +0200 | [diff] [blame] | 2283 | |
| 2284 | /* Synchronize all polling loops */ |
| 2285 | sync_poll_loop(); |
| 2286 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2287 | } |
| 2288 | } |
| 2289 | |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2290 | static void *run_thread_poll_loop(void *data) |
| 2291 | { |
| 2292 | struct per_thread_init_fct *ptif; |
| 2293 | struct per_thread_deinit_fct *ptdf; |
| 2294 | |
| 2295 | tid = *((unsigned int *)data); |
| 2296 | tid_bit = (1UL << tid); |
| 2297 | tv_update_date(-1,-1); |
| 2298 | |
| 2299 | list_for_each_entry(ptif, &per_thread_init_list, list) { |
| 2300 | if (!ptif->fct()) { |
| 2301 | Alert("failed to initialize thread %u.\n", tid); |
| 2302 | exit(1); |
| 2303 | } |
| 2304 | } |
| 2305 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 2306 | if (global.mode & MODE_MWORKER) |
| 2307 | mworker_pipe_register(mworker_pipe); |
| 2308 | |
| 2309 | protocol_enable_all(); |
Christopher Faulet | dc628a3 | 2017-10-19 11:59:44 +0200 | [diff] [blame] | 2310 | THREAD_SYNC_ENABLE(); |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2311 | run_poll_loop(); |
| 2312 | |
| 2313 | list_for_each_entry(ptdf, &per_thread_deinit_list, list) |
| 2314 | ptdf->fct(); |
| 2315 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2316 | #ifdef USE_THREAD |
| 2317 | if (tid > 0) |
| 2318 | pthread_exit(NULL); |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2319 | #endif |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2320 | return NULL; |
| 2321 | } |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2322 | |
Willy Tarreau | e9b2602 | 2011-08-01 20:57:55 +0200 | [diff] [blame] | 2323 | /* This is the global management task for listeners. It enables listeners waiting |
| 2324 | * for global resources when there are enough free resource, or at least once in |
| 2325 | * a while. It is designed to be called as a task. |
| 2326 | */ |
| 2327 | static struct task *manage_global_listener_queue(struct task *t) |
| 2328 | { |
| 2329 | int next = TICK_ETERNITY; |
Willy Tarreau | e9b2602 | 2011-08-01 20:57:55 +0200 | [diff] [blame] | 2330 | /* queue is empty, nothing to do */ |
| 2331 | if (LIST_ISEMPTY(&global_listener_queue)) |
| 2332 | goto out; |
| 2333 | |
| 2334 | /* If there are still too many concurrent connections, let's wait for |
| 2335 | * some of them to go away. We don't need to re-arm the timer because |
| 2336 | * each of them will scan the queue anyway. |
| 2337 | */ |
| 2338 | if (unlikely(actconn >= global.maxconn)) |
| 2339 | goto out; |
| 2340 | |
| 2341 | /* We should periodically try to enable listeners waiting for a global |
| 2342 | * resource here, because it is possible, though very unlikely, that |
| 2343 | * they have been blocked by a temporary lack of global resource such |
| 2344 | * as a file descriptor or memory and that the temporary condition has |
| 2345 | * disappeared. |
| 2346 | */ |
Willy Tarreau | abacc2c | 2011-09-07 14:26:33 +0200 | [diff] [blame] | 2347 | dequeue_all_listeners(&global_listener_queue); |
Willy Tarreau | e9b2602 | 2011-08-01 20:57:55 +0200 | [diff] [blame] | 2348 | |
| 2349 | out: |
| 2350 | t->expire = next; |
| 2351 | task_queue(t); |
| 2352 | return t; |
| 2353 | } |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2354 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2355 | int main(int argc, char **argv) |
| 2356 | { |
| 2357 | int err, retry; |
| 2358 | struct rlimit limit; |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 2359 | char errmsg[100]; |
Willy Tarreau | 269ab31 | 2012-09-05 08:02:48 +0200 | [diff] [blame] | 2360 | int pidfd = -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2361 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 2362 | init(argc, argv); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 2363 | signal_register_fct(SIGQUIT, dump, SIGQUIT); |
| 2364 | signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1); |
| 2365 | signal_register_fct(SIGHUP, sig_dump_state, SIGHUP); |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 2366 | signal_register_fct(SIGUSR2, NULL, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2367 | |
Willy Tarreau | e437c44 | 2010-03-17 18:02:46 +0100 | [diff] [blame] | 2368 | /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL. |
| 2369 | * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL |
| 2370 | * was defined there, so let's stay on the safe side. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2371 | */ |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 2372 | signal_register_fct(SIGPIPE, NULL, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2373 | |
Willy Tarreau | dc23a92 | 2011-02-16 11:10:36 +0100 | [diff] [blame] | 2374 | /* ulimits */ |
| 2375 | if (!global.rlimit_nofile) |
| 2376 | global.rlimit_nofile = global.maxsock; |
| 2377 | |
| 2378 | if (global.rlimit_nofile) { |
| 2379 | limit.rlim_cur = limit.rlim_max = global.rlimit_nofile; |
| 2380 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
Willy Tarreau | ef63547 | 2016-06-21 11:48:18 +0200 | [diff] [blame] | 2381 | /* try to set it to the max possible at least */ |
| 2382 | getrlimit(RLIMIT_NOFILE, &limit); |
Willy Tarreau | 164dd0b | 2016-06-21 11:51:59 +0200 | [diff] [blame] | 2383 | limit.rlim_cur = limit.rlim_max; |
| 2384 | if (setrlimit(RLIMIT_NOFILE, &limit) != -1) |
| 2385 | getrlimit(RLIMIT_NOFILE, &limit); |
| 2386 | |
Willy Tarreau | ef63547 | 2016-06-21 11:48:18 +0200 | [diff] [blame] | 2387 | Warning("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n", argv[0], global.rlimit_nofile, (int)limit.rlim_cur); |
| 2388 | global.rlimit_nofile = limit.rlim_cur; |
Willy Tarreau | dc23a92 | 2011-02-16 11:10:36 +0100 | [diff] [blame] | 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | if (global.rlimit_memmax) { |
| 2393 | limit.rlim_cur = limit.rlim_max = |
Willy Tarreau | 7006045 | 2015-12-14 12:46:07 +0100 | [diff] [blame] | 2394 | global.rlimit_memmax * 1048576ULL; |
Willy Tarreau | dc23a92 | 2011-02-16 11:10:36 +0100 | [diff] [blame] | 2395 | #ifdef RLIMIT_AS |
| 2396 | if (setrlimit(RLIMIT_AS, &limit) == -1) { |
| 2397 | Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n", |
| 2398 | argv[0], global.rlimit_memmax); |
| 2399 | } |
| 2400 | #else |
| 2401 | if (setrlimit(RLIMIT_DATA, &limit) == -1) { |
| 2402 | Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n", |
| 2403 | argv[0], global.rlimit_memmax); |
| 2404 | } |
| 2405 | #endif |
| 2406 | } |
| 2407 | |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 2408 | if (old_unixsocket) { |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 2409 | if (strcmp("/dev/null", old_unixsocket) != 0) { |
| 2410 | if (get_old_sockets(old_unixsocket) != 0) { |
| 2411 | Alert("Failed to get the sockets from the old process!\n"); |
| 2412 | if (!(global.mode & MODE_MWORKER)) |
| 2413 | exit(1); |
| 2414 | } |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 2415 | } |
| 2416 | } |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 2417 | get_cur_unixsocket(); |
| 2418 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2419 | /* We will loop at most 100 times with 10 ms delay each time. |
| 2420 | * That's at most 1 second. We only send a signal to old pids |
| 2421 | * if we cannot grab at least one port. |
| 2422 | */ |
| 2423 | retry = MAX_START_RETRIES; |
| 2424 | err = ERR_NONE; |
| 2425 | while (retry >= 0) { |
| 2426 | struct timeval w; |
| 2427 | err = start_proxies(retry == 0 || nb_oldpids == 0); |
Willy Tarreau | e13e925 | 2007-12-20 23:05:50 +0100 | [diff] [blame] | 2428 | /* exit the loop on no error or fatal error */ |
| 2429 | if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2430 | break; |
Willy Tarreau | bb545b4 | 2010-08-25 12:58:59 +0200 | [diff] [blame] | 2431 | if (nb_oldpids == 0 || retry == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2432 | break; |
| 2433 | |
| 2434 | /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on |
| 2435 | * listening sockets. So on those platforms, it would be wiser to |
| 2436 | * simply send SIGUSR1, which will not be undoable. |
| 2437 | */ |
Willy Tarreau | bb545b4 | 2010-08-25 12:58:59 +0200 | [diff] [blame] | 2438 | if (tell_old_pids(SIGTTOU) == 0) { |
| 2439 | /* no need to wait if we can't contact old pids */ |
| 2440 | retry = 0; |
| 2441 | continue; |
| 2442 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2443 | /* give some time to old processes to stop listening */ |
| 2444 | w.tv_sec = 0; |
| 2445 | w.tv_usec = 10*1000; |
| 2446 | select(0, NULL, NULL, NULL, &w); |
| 2447 | retry--; |
| 2448 | } |
| 2449 | |
| 2450 | /* Note: start_proxies() sends an alert when it fails. */ |
Willy Tarreau | 0a3b9d9 | 2009-02-04 17:05:23 +0100 | [diff] [blame] | 2451 | if ((err & ~ERR_WARN) != ERR_NONE) { |
Willy Tarreau | f68da46 | 2009-06-09 14:36:00 +0200 | [diff] [blame] | 2452 | if (retry != MAX_START_RETRIES && nb_oldpids) { |
| 2453 | protocol_unbind_all(); /* cleanup everything we can */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2454 | tell_old_pids(SIGTTIN); |
Willy Tarreau | f68da46 | 2009-06-09 14:36:00 +0200 | [diff] [blame] | 2455 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2456 | exit(1); |
| 2457 | } |
| 2458 | |
| 2459 | if (listeners == 0) { |
Willy Tarreau | f2ee016 | 2015-08-09 11:01:51 +0200 | [diff] [blame] | 2460 | Alert("[%s.main()] No enabled listener found (check for 'bind' directives) ! Exiting.\n", argv[0]); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2461 | /* Note: we don't have to send anything to the old pids because we |
| 2462 | * never stopped them. */ |
| 2463 | exit(1); |
| 2464 | } |
| 2465 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 2466 | err = protocol_bind_all(errmsg, sizeof(errmsg)); |
| 2467 | if ((err & ~ERR_WARN) != ERR_NONE) { |
| 2468 | if ((err & ERR_ALERT) || (err & ERR_WARN)) |
| 2469 | Alert("[%s.main()] %s.\n", argv[0], errmsg); |
| 2470 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 2471 | Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]); |
| 2472 | protocol_unbind_all(); /* cleanup everything we can */ |
| 2473 | if (nb_oldpids) |
| 2474 | tell_old_pids(SIGTTIN); |
| 2475 | exit(1); |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 2476 | } else if (err & ERR_WARN) { |
| 2477 | Alert("[%s.main()] %s.\n", argv[0], errmsg); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 2478 | } |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 2479 | /* Ok, all listener should now be bound, close any leftover sockets |
| 2480 | * the previous process gave us, we don't need them anymore |
| 2481 | */ |
| 2482 | while (xfer_sock_list != NULL) { |
| 2483 | struct xfer_sock_list *tmpxfer = xfer_sock_list->next; |
| 2484 | close(xfer_sock_list->fd); |
| 2485 | free(xfer_sock_list->iface); |
| 2486 | free(xfer_sock_list->namespace); |
| 2487 | free(xfer_sock_list); |
| 2488 | xfer_sock_list = tmpxfer; |
| 2489 | } |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 2490 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2491 | /* prepare pause/play signals */ |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 2492 | signal_register_fct(SIGTTOU, sig_pause, SIGTTOU); |
| 2493 | signal_register_fct(SIGTTIN, sig_listen, SIGTTIN); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2494 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2495 | /* MODE_QUIET can inhibit alerts and warnings below this line */ |
| 2496 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2497 | if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) { |
| 2498 | /* detach from the tty */ |
| 2499 | fclose(stdin); fclose(stdout); fclose(stderr); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | /* open log & pid files before the chroot */ |
William Lallemand | 8029300 | 2017-11-06 11:00:03 +0100 | [diff] [blame] | 2503 | if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && global.pidfile != NULL) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2504 | unlink(global.pidfile); |
| 2505 | pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 2506 | if (pidfd < 0) { |
| 2507 | Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile); |
| 2508 | if (nb_oldpids) |
| 2509 | tell_old_pids(SIGTTIN); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 2510 | protocol_unbind_all(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2511 | exit(1); |
| 2512 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2513 | } |
| 2514 | |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 2515 | if ((global.last_checks & LSTCHK_NETADM) && global.uid) { |
| 2516 | Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n" |
Willy Tarreau | 4e30ed7 | 2009-02-04 18:02:48 +0100 | [diff] [blame] | 2517 | "", argv[0]); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 2518 | protocol_unbind_all(); |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 2519 | exit(1); |
| 2520 | } |
| 2521 | |
Willy Tarreau | 4e30ed7 | 2009-02-04 18:02:48 +0100 | [diff] [blame] | 2522 | /* If the user is not root, we'll still let him try the configuration |
| 2523 | * but we inform him that unexpected behaviour may occur. |
| 2524 | */ |
| 2525 | if ((global.last_checks & LSTCHK_NETADM) && getuid()) |
| 2526 | Warning("[%s.main()] Some options which require full privileges" |
| 2527 | " might not work well.\n" |
| 2528 | "", argv[0]); |
| 2529 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2530 | if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) { |
| 2531 | |
| 2532 | /* chroot if needed */ |
| 2533 | if (global.chroot != NULL) { |
| 2534 | if (chroot(global.chroot) == -1 || chdir("/") == -1) { |
| 2535 | Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot); |
| 2536 | if (nb_oldpids) |
| 2537 | tell_old_pids(SIGTTIN); |
| 2538 | protocol_unbind_all(); |
| 2539 | exit(1); |
| 2540 | } |
Willy Tarreau | f223cc0 | 2007-10-15 18:57:08 +0200 | [diff] [blame] | 2541 | } |
Willy Tarreau | f223cc0 | 2007-10-15 18:57:08 +0200 | [diff] [blame] | 2542 | } |
| 2543 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2544 | if (nb_oldpids) |
Willy Tarreau | bb545b4 | 2010-08-25 12:58:59 +0200 | [diff] [blame] | 2545 | nb_oldpids = tell_old_pids(oldpids_sig); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2546 | |
William Lallemand | 8a361b5 | 2017-06-20 11:20:33 +0200 | [diff] [blame] | 2547 | if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) { |
| 2548 | nb_oldpids = 0; |
| 2549 | free(oldpids); |
| 2550 | oldpids = NULL; |
| 2551 | } |
| 2552 | |
| 2553 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2554 | /* Note that any error at this stage will be fatal because we will not |
| 2555 | * be able to restart the old pids. |
| 2556 | */ |
| 2557 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2558 | if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) { |
| 2559 | /* setgid / setuid */ |
| 2560 | if (global.gid) { |
| 2561 | if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1) |
| 2562 | Warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'" |
| 2563 | " without 'uid'/'user' is generally useless.\n", argv[0]); |
| 2564 | |
| 2565 | if (setgid(global.gid) == -1) { |
| 2566 | Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid); |
| 2567 | protocol_unbind_all(); |
| 2568 | exit(1); |
| 2569 | } |
| 2570 | } |
Michael Scherer | ab012dd | 2013-01-12 18:35:19 +0100 | [diff] [blame] | 2571 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2572 | if (global.uid && setuid(global.uid) == -1) { |
| 2573 | Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid); |
Michael Scherer | ab012dd | 2013-01-12 18:35:19 +0100 | [diff] [blame] | 2574 | protocol_unbind_all(); |
| 2575 | exit(1); |
| 2576 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2577 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2578 | /* check ulimits */ |
| 2579 | limit.rlim_cur = limit.rlim_max = 0; |
| 2580 | getrlimit(RLIMIT_NOFILE, &limit); |
| 2581 | if (limit.rlim_cur < global.maxsock) { |
| 2582 | 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 Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 2583 | argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2584 | } |
| 2585 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2586 | if (global.mode & (MODE_DAEMON | MODE_MWORKER)) { |
Willy Tarreau | 0b9c02c | 2009-02-04 22:05:05 +0100 | [diff] [blame] | 2587 | struct proxy *px; |
Willy Tarreau | f83d3fe | 2015-05-01 19:13:41 +0200 | [diff] [blame] | 2588 | struct peers *curpeers; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2589 | int ret = 0; |
| 2590 | int proc; |
| 2591 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 2592 | children = calloc(global.nbproc, sizeof(int)); |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2593 | /* |
| 2594 | * if daemon + mworker: must fork here to let a master |
| 2595 | * process live in background before forking children |
| 2596 | */ |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 2597 | |
| 2598 | if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL) |
| 2599 | && (global.mode & MODE_MWORKER) |
| 2600 | && (global.mode & MODE_DAEMON)) { |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2601 | ret = fork(); |
| 2602 | if (ret < 0) { |
| 2603 | Alert("[%s.main()] Cannot fork.\n", argv[0]); |
| 2604 | protocol_unbind_all(); |
| 2605 | exit(1); /* there has been an error */ |
| 2606 | } |
| 2607 | /* parent leave to daemonize */ |
| 2608 | if (ret > 0) |
| 2609 | exit(0); |
| 2610 | } |
William Lallemand | e20b6a6 | 2017-06-01 17:38:55 +0200 | [diff] [blame] | 2611 | |
| 2612 | if (global.mode & MODE_MWORKER) { |
| 2613 | if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) { |
| 2614 | char *msg = NULL; |
| 2615 | /* master pipe to ensure the master is still alive */ |
| 2616 | ret = pipe(mworker_pipe); |
| 2617 | if (ret < 0) { |
| 2618 | Warning("[%s.main()] Cannot create master pipe.\n", argv[0]); |
| 2619 | } else { |
| 2620 | memprintf(&msg, "%d", mworker_pipe[0]); |
| 2621 | setenv("HAPROXY_MWORKER_PIPE_RD", msg, 1); |
| 2622 | memprintf(&msg, "%d", mworker_pipe[1]); |
| 2623 | setenv("HAPROXY_MWORKER_PIPE_WR", msg, 1); |
| 2624 | free(msg); |
| 2625 | } |
| 2626 | } else { |
| 2627 | mworker_pipe[0] = atol(getenv("HAPROXY_MWORKER_PIPE_RD")); |
| 2628 | mworker_pipe[1] = atol(getenv("HAPROXY_MWORKER_PIPE_WR")); |
| 2629 | if (mworker_pipe[0] <= 0 || mworker_pipe[1] <= 0) { |
| 2630 | Warning("[%s.main()] Cannot get master pipe FDs.\n", argv[0]); |
| 2631 | } |
| 2632 | } |
| 2633 | } |
| 2634 | |
William Lallemand | deed780 | 2017-11-06 11:00:04 +0100 | [diff] [blame] | 2635 | /* if in master-worker mode, write the PID of the father */ |
| 2636 | if (global.mode & MODE_MWORKER) { |
| 2637 | char pidstr[100]; |
| 2638 | snprintf(pidstr, sizeof(pidstr), "%d\n", getpid()); |
| 2639 | shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr))); |
| 2640 | } |
| 2641 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2642 | /* the father launches the required number of processes */ |
| 2643 | for (proc = 0; proc < global.nbproc; proc++) { |
| 2644 | ret = fork(); |
| 2645 | if (ret < 0) { |
| 2646 | Alert("[%s.main()] Cannot fork.\n", argv[0]); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 2647 | protocol_unbind_all(); |
Willy Tarreau | d680371 | 2007-10-16 07:44:56 +0200 | [diff] [blame] | 2648 | exit(1); /* there has been an error */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2649 | } |
| 2650 | else if (ret == 0) /* child breaks here */ |
| 2651 | break; |
Marc-Antoine Perennou | 992709b | 2013-02-12 10:53:52 +0100 | [diff] [blame] | 2652 | children[proc] = ret; |
William Lallemand | 92159b2 | 2017-11-06 11:16:12 +0100 | [diff] [blame] | 2653 | if (pidfd >= 0 && !(global.mode & MODE_MWORKER)) { |
Willy Tarreau | 269ab31 | 2012-09-05 08:02:48 +0200 | [diff] [blame] | 2654 | char pidstr[100]; |
| 2655 | snprintf(pidstr, sizeof(pidstr), "%d\n", ret); |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 2656 | shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr))); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2657 | } |
Willy Tarreau | dcd4771 | 2007-11-04 23:35:08 +0100 | [diff] [blame] | 2658 | relative_pid++; /* each child will get a different one */ |
Willy Tarreau | 387bd4f | 2017-11-10 19:08:14 +0100 | [diff] [blame] | 2659 | pid_bit <<= 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2660 | } |
Willy Tarreau | fc6c032 | 2012-11-16 16:12:27 +0100 | [diff] [blame] | 2661 | |
| 2662 | #ifdef USE_CPU_AFFINITY |
| 2663 | if (proc < global.nbproc && /* child */ |
Willy Tarreau | e759749 | 2015-04-20 11:36:57 +0200 | [diff] [blame] | 2664 | proc < LONGBITS && /* only the first 32/64 processes may be pinned */ |
Willy Tarreau | fc6c032 | 2012-11-16 16:12:27 +0100 | [diff] [blame] | 2665 | global.cpu_map[proc]) /* only do this if the process has a CPU map */ |
Pieter Baauw | caa6a1b | 2015-09-17 21:26:40 +0200 | [diff] [blame] | 2666 | #ifdef __FreeBSD__ |
Olivier Houchard | 97148f6 | 2017-08-16 17:29:11 +0200 | [diff] [blame] | 2667 | { |
| 2668 | cpuset_t cpuset; |
| 2669 | int i; |
| 2670 | unsigned long cpu_map = global.cpu_map[proc]; |
| 2671 | |
| 2672 | CPU_ZERO(&cpuset); |
| 2673 | while ((i = ffsl(cpu_map)) > 0) { |
| 2674 | CPU_SET(i - 1, &cpuset); |
| 2675 | cpu_map &= ~(1 << (i - 1)); |
| 2676 | } |
| 2677 | ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset); |
| 2678 | } |
Pieter Baauw | caa6a1b | 2015-09-17 21:26:40 +0200 | [diff] [blame] | 2679 | #else |
Willy Tarreau | fc6c032 | 2012-11-16 16:12:27 +0100 | [diff] [blame] | 2680 | sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map[proc]); |
| 2681 | #endif |
Pieter Baauw | caa6a1b | 2015-09-17 21:26:40 +0200 | [diff] [blame] | 2682 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2683 | /* close the pidfile both in children and father */ |
Willy Tarreau | 269ab31 | 2012-09-05 08:02:48 +0200 | [diff] [blame] | 2684 | if (pidfd >= 0) { |
| 2685 | //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */ |
| 2686 | close(pidfd); |
| 2687 | } |
Willy Tarreau | d137dd3 | 2010-08-25 12:49:05 +0200 | [diff] [blame] | 2688 | |
| 2689 | /* We won't ever use this anymore */ |
Willy Tarreau | d137dd3 | 2010-08-25 12:49:05 +0200 | [diff] [blame] | 2690 | free(global.pidfile); global.pidfile = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2691 | |
Willy Tarreau | edaff0a | 2015-05-01 17:01:08 +0200 | [diff] [blame] | 2692 | if (proc == global.nbproc) { |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2693 | if (global.mode & MODE_MWORKER) { |
Willy Tarreau | edaff0a | 2015-05-01 17:01:08 +0200 | [diff] [blame] | 2694 | protocol_unbind_all(); |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 2695 | mworker_wait(); |
William Lallemand | 1499b9b | 2017-06-07 15:04:47 +0200 | [diff] [blame] | 2696 | /* should never get there */ |
| 2697 | exit(EXIT_FAILURE); |
Willy Tarreau | edaff0a | 2015-05-01 17:01:08 +0200 | [diff] [blame] | 2698 | } |
William Lallemand | cf4e496 | 2017-06-08 19:05:48 +0200 | [diff] [blame] | 2699 | #if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH) |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 2700 | ssl_free_dh(); |
| 2701 | #endif |
William Lallemand | 1499b9b | 2017-06-07 15:04:47 +0200 | [diff] [blame] | 2702 | exit(0); /* parent must leave */ |
Willy Tarreau | edaff0a | 2015-05-01 17:01:08 +0200 | [diff] [blame] | 2703 | } |
| 2704 | |
William Lallemand | cb11fd2 | 2017-06-01 17:38:52 +0200 | [diff] [blame] | 2705 | /* child must never use the atexit function */ |
| 2706 | atexit_flag = 0; |
| 2707 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2708 | /* Must chroot and setgid/setuid in the children */ |
| 2709 | /* chroot if needed */ |
| 2710 | if (global.chroot != NULL) { |
| 2711 | if (chroot(global.chroot) == -1 || chdir("/") == -1) { |
| 2712 | Alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot); |
| 2713 | if (nb_oldpids) |
| 2714 | tell_old_pids(SIGTTIN); |
| 2715 | protocol_unbind_all(); |
| 2716 | exit(1); |
| 2717 | } |
| 2718 | } |
| 2719 | |
| 2720 | free(global.chroot); |
| 2721 | global.chroot = NULL; |
| 2722 | |
| 2723 | /* setgid / setuid */ |
| 2724 | if (global.gid) { |
| 2725 | if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1) |
| 2726 | Warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'" |
| 2727 | " without 'uid'/'user' is generally useless.\n", argv[0]); |
| 2728 | |
| 2729 | if (setgid(global.gid) == -1) { |
| 2730 | Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid); |
| 2731 | protocol_unbind_all(); |
| 2732 | exit(1); |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | if (global.uid && setuid(global.uid) == -1) { |
| 2737 | Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid); |
| 2738 | protocol_unbind_all(); |
| 2739 | exit(1); |
| 2740 | } |
| 2741 | |
William Lallemand | 7f80eb2 | 2017-05-26 18:19:55 +0200 | [diff] [blame] | 2742 | /* pass through every cli socket, and check if it's bound to |
| 2743 | * the current process and if it exposes listeners sockets. |
| 2744 | * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork. |
| 2745 | * */ |
| 2746 | |
| 2747 | if (global.stats_fe) { |
| 2748 | struct bind_conf *bind_conf; |
| 2749 | |
| 2750 | list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) { |
| 2751 | if (bind_conf->level & ACCESS_FD_LISTENERS) { |
| 2752 | if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) { |
| 2753 | global.tune.options |= GTUNE_SOCKET_TRANSFER; |
| 2754 | break; |
| 2755 | } |
| 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | |
Willy Tarreau | 0b9c02c | 2009-02-04 22:05:05 +0100 | [diff] [blame] | 2760 | /* we might have to unbind some proxies from some processes */ |
| 2761 | px = proxy; |
| 2762 | while (px != NULL) { |
| 2763 | if (px->bind_proc && px->state != PR_STSTOPPED) { |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 2764 | if (!(px->bind_proc & (1UL << proc))) { |
| 2765 | if (global.tune.options & GTUNE_SOCKET_TRANSFER) |
| 2766 | zombify_proxy(px); |
| 2767 | else |
| 2768 | stop_proxy(px); |
| 2769 | } |
Willy Tarreau | 0b9c02c | 2009-02-04 22:05:05 +0100 | [diff] [blame] | 2770 | } |
| 2771 | px = px->next; |
| 2772 | } |
| 2773 | |
Willy Tarreau | f83d3fe | 2015-05-01 19:13:41 +0200 | [diff] [blame] | 2774 | /* we might have to unbind some peers sections from some processes */ |
Frédéric Lécaille | ed2b4a6 | 2017-07-13 09:07:09 +0200 | [diff] [blame] | 2775 | for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) { |
Willy Tarreau | f83d3fe | 2015-05-01 19:13:41 +0200 | [diff] [blame] | 2776 | if (!curpeers->peers_fe) |
| 2777 | continue; |
| 2778 | |
| 2779 | if (curpeers->peers_fe->bind_proc & (1UL << proc)) |
| 2780 | continue; |
| 2781 | |
| 2782 | stop_proxy(curpeers->peers_fe); |
| 2783 | /* disable this peer section so that it kills itself */ |
Willy Tarreau | 47c8c02 | 2015-09-28 16:39:25 +0200 | [diff] [blame] | 2784 | signal_unregister_handler(curpeers->sighandler); |
| 2785 | task_delete(curpeers->sync_task); |
| 2786 | task_free(curpeers->sync_task); |
| 2787 | curpeers->sync_task = NULL; |
| 2788 | task_free(curpeers->peers_fe->task); |
| 2789 | curpeers->peers_fe->task = NULL; |
Willy Tarreau | f83d3fe | 2015-05-01 19:13:41 +0200 | [diff] [blame] | 2790 | curpeers->peers_fe = NULL; |
| 2791 | } |
| 2792 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2793 | /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure |
| 2794 | * that we can detach from the TTY. We MUST NOT do it in other cases since |
| 2795 | * it would have already be done, and 0-2 would have been affected to listening |
| 2796 | * sockets |
| 2797 | */ |
Willy Tarreau | 106cb76 | 2008-11-16 07:40:34 +0100 | [diff] [blame] | 2798 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2799 | /* detach from the tty */ |
| 2800 | fclose(stdin); fclose(stdout); fclose(stderr); |
Willy Tarreau | 106cb76 | 2008-11-16 07:40:34 +0100 | [diff] [blame] | 2801 | global.mode &= ~MODE_VERBOSE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2802 | global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ |
| 2803 | } |
| 2804 | pid = getpid(); /* update child's pid */ |
| 2805 | setsid(); |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 2806 | fork_poller(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2807 | } |
| 2808 | |
Christopher Faulet | e3a5e35 | 2017-10-24 13:53:54 +0200 | [diff] [blame] | 2809 | global.mode &= ~MODE_STARTING; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2810 | /* |
| 2811 | * That's it : the central polling loop. Run until we stop. |
| 2812 | */ |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2813 | #ifdef USE_THREAD |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2814 | { |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2815 | unsigned int *tids = calloc(global.nbthread, sizeof(unsigned int)); |
| 2816 | pthread_t *threads = calloc(global.nbthread, sizeof(pthread_t)); |
| 2817 | int i; |
| 2818 | |
Christopher Faulet | d7bddda | 2017-10-31 17:30:12 +0100 | [diff] [blame] | 2819 | THREAD_SYNC_INIT((1UL << global.nbthread) - 1); |
| 2820 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2821 | /* Init tids array */ |
| 2822 | for (i = 0; i < global.nbthread; i++) |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2823 | tids[i] = i; |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2824 | |
| 2825 | /* Create nbthread-1 thread. The first thread is the current process */ |
| 2826 | threads[0] = pthread_self(); |
| 2827 | for (i = 1; i < global.nbthread; i++) |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2828 | pthread_create(&threads[i], NULL, &run_thread_poll_loop, &tids[i]); |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2829 | |
Christopher Faulet | 6251902 | 2017-10-16 15:49:32 +0200 | [diff] [blame] | 2830 | #ifdef USE_CPU_AFFINITY |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2831 | /* Now the CPU affinity for all threads */ |
| 2832 | for (i = 0; i < global.nbthread; i++) { |
Christopher Faulet | 6251902 | 2017-10-16 15:49:32 +0200 | [diff] [blame] | 2833 | if (global.cpu_map[relative_pid-1]) |
| 2834 | global.thread_map[relative_pid-1][i] &= global.cpu_map[relative_pid-1]; |
| 2835 | |
| 2836 | if (i < LONGBITS && /* only the first 32/64 threads may be pinned */ |
| 2837 | global.thread_map[relative_pid-1][i]) /* only do this if the thread has a THREAD map */ |
| 2838 | pthread_setaffinity_np(threads[i], |
| 2839 | sizeof(unsigned long), (void *)&global.thread_map[relative_pid-1][i]); |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2840 | } |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2841 | #endif /* !USE_CPU_AFFINITY */ |
| 2842 | |
| 2843 | /* Finally, start the poll loop for the first thread */ |
| 2844 | run_thread_poll_loop(&tids[0]); |
| 2845 | |
| 2846 | /* Wait the end of other threads */ |
| 2847 | for (i = 1; i < global.nbthread; i++) |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2848 | pthread_join(threads[i], NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2849 | |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2850 | free(tids); |
| 2851 | free(threads); |
| 2852 | |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 2853 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 2854 | show_lock_stats(); |
| 2855 | #endif |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2856 | } |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2857 | #else /* ! USE_THREAD */ |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 2858 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2859 | run_thread_poll_loop((int []){0}); |
Christopher Faulet | 6251902 | 2017-10-16 15:49:32 +0200 | [diff] [blame] | 2860 | |
Christopher Faulet | 6251902 | 2017-10-16 15:49:32 +0200 | [diff] [blame] | 2861 | #endif |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2862 | |
| 2863 | /* Do some cleanup */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2864 | deinit(); |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2865 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2866 | exit(0); |
| 2867 | } |
| 2868 | |
| 2869 | |
| 2870 | /* |
| 2871 | * Local variables: |
| 2872 | * c-indent-level: 8 |
| 2873 | * c-basic-offset: 8 |
| 2874 | * End: |
| 2875 | */ |