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