Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HA-Proxy : High Availability-enabled HTTP/TCP proxy |
Willy Tarreau | 71f95fa | 2020-01-22 10:34:58 +0100 | [diff] [blame] | 3 | * Copyright 2000-2020 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> |
David Carlier | 42d9e5a | 2018-11-12 16:22:19 +0000 | [diff] [blame] | 55 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
Pieter Baauw | caa6a1b | 2015-09-17 21:26:40 +0200 | [diff] [blame] | 56 | #include <sys/param.h> |
David Carlier | 42d9e5a | 2018-11-12 16:22:19 +0000 | [diff] [blame] | 57 | #ifdef __FreeBSD__ |
Pieter Baauw | caa6a1b | 2015-09-17 21:26:40 +0200 | [diff] [blame] | 58 | #include <sys/cpuset.h> |
David Carlier | 42d9e5a | 2018-11-12 16:22:19 +0000 | [diff] [blame] | 59 | #endif |
David Carlier | 6d5c841 | 2017-11-29 11:02:32 +0000 | [diff] [blame] | 60 | #include <pthread_np.h> |
Pieter Baauw | caa6a1b | 2015-09-17 21:26:40 +0200 | [diff] [blame] | 61 | #endif |
David Carlier | 5e4c8e2 | 2019-09-13 05:12:58 +0100 | [diff] [blame] | 62 | #ifdef __APPLE__ |
| 63 | #include <mach/mach_types.h> |
| 64 | #include <mach/thread_act.h> |
| 65 | #include <mach/thread_policy.h> |
| 66 | #endif |
Willy Tarreau | fc6c032 | 2012-11-16 16:12:27 +0100 | [diff] [blame] | 67 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | |
Willy Tarreau | 636848a | 2019-04-15 19:38:50 +0200 | [diff] [blame] | 69 | #if defined(USE_PRCTL) |
| 70 | #include <sys/prctl.h> |
| 71 | #endif |
| 72 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 73 | #ifdef DEBUG_FULL |
| 74 | #include <assert.h> |
| 75 | #endif |
Tim Duesterhus | d6942c8 | 2017-11-20 15:58:35 +0100 | [diff] [blame] | 76 | #if defined(USE_SYSTEMD) |
| 77 | #include <systemd/sd-daemon.h> |
| 78 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 79 | |
Willy Tarreau | 6c3a681 | 2020-03-06 18:57:15 +0100 | [diff] [blame] | 80 | #include <import/sha1.h> |
| 81 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 82 | #include <common/base64.h> |
| 83 | #include <common/cfgparse.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 84 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 85 | #include <common/compat.h> |
| 86 | #include <common/config.h> |
| 87 | #include <common/defaults.h> |
Willy Tarreau | d740bab | 2007-10-28 11:14:07 +0100 | [diff] [blame] | 88 | #include <common/errors.h> |
Willy Tarreau | 5794fb0 | 2018-11-25 18:43:29 +0100 | [diff] [blame] | 89 | #include <common/initcall.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 90 | #include <common/memory.h> |
| 91 | #include <common/mini-clist.h> |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 92 | #include <common/namespace.h> |
Willy Tarreau | 6c3a681 | 2020-03-06 18:57:15 +0100 | [diff] [blame] | 93 | #include <common/net_helper.h> |
Willy Tarreau | c125cef | 2019-05-10 09:58:43 +0200 | [diff] [blame] | 94 | #include <common/openssl-compat.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 95 | #include <common/regex.h> |
| 96 | #include <common/standard.h> |
| 97 | #include <common/time.h> |
| 98 | #include <common/uri_auth.h> |
| 99 | #include <common/version.h> |
Christopher Faulet | be0faa2 | 2017-08-29 15:37:10 +0200 | [diff] [blame] | 100 | #include <common/hathreads.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | |
| 102 | #include <types/capture.h> |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 103 | #include <types/cli.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 104 | #include <types/filters.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | #include <types/global.h> |
Simon Horman | ac82142 | 2011-07-15 13:14:09 +0900 | [diff] [blame] | 106 | #include <types/acl.h> |
Willy Tarreau | 3c63fd8 | 2011-09-07 18:00:47 +0200 | [diff] [blame] | 107 | #include <types/peers.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 108 | |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 109 | #include <proto/acl.h> |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 110 | #include <proto/activity.h> |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 111 | #include <proto/arg.h> |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 112 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 114 | #include <proto/channel.h> |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 115 | #include <proto/cli.h> |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 116 | #include <proto/connection.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 118 | #include <proto/filters.h> |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 119 | #include <proto/hlua.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 120 | #include <proto/http_rules.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 121 | #include <proto/listener.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | #include <proto/log.h> |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 123 | #include <proto/mworker.h> |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 124 | #include <proto/pattern.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 125 | #include <proto/protocol.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 126 | #include <proto/http_ana.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 127 | #include <proto/proxy.h> |
| 128 | #include <proto/queue.h> |
| 129 | #include <proto/server.h> |
Willy Tarreau | b1ec8c4 | 2015-04-03 13:53:24 +0200 | [diff] [blame] | 130 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 131 | #include <proto/stream.h> |
Willy Tarreau | 2985794 | 2009-05-10 09:01:21 +0200 | [diff] [blame] | 132 | #include <proto/signal.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 133 | #include <proto/task.h> |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 134 | #include <proto/dns.h> |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 135 | #include <proto/vars.h> |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 136 | #include <proto/ssl_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 137 | |
Willy Tarreau | 7b5654f | 2019-03-29 21:30:17 +0100 | [diff] [blame] | 138 | /* array of init calls for older platforms */ |
| 139 | DECLARE_INIT_STAGES; |
| 140 | |
Willy Tarreau | 477ecd8 | 2010-01-03 21:12:30 +0100 | [diff] [blame] | 141 | /* list of config files */ |
| 142 | static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 143 | int pid; /* current process id */ |
Willy Tarreau | 2815664 | 2007-11-26 16:13:36 +0100 | [diff] [blame] | 144 | int relative_pid = 1; /* process id starting at 1 */ |
Willy Tarreau | 387bd4f | 2017-11-10 19:08:14 +0100 | [diff] [blame] | 145 | unsigned long pid_bit = 1; /* bit corresponding to the process id */ |
Willy Tarreau | a38a717 | 2019-02-02 17:11:28 +0100 | [diff] [blame] | 146 | unsigned long all_proc_mask = 1; /* mask of all processes */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 147 | |
Willy Tarreau | f8ea00e | 2020-03-12 17:24:53 +0100 | [diff] [blame] | 148 | volatile unsigned long sleeping_thread_mask = 0; /* Threads that are about to sleep in poll() */ |
Willy Tarreau | 4b3f27b | 2020-03-12 17:28:01 +0100 | [diff] [blame] | 149 | volatile unsigned long stopping_thread_mask = 0; /* Threads acknowledged stopping */ |
Willy Tarreau | f8ea00e | 2020-03-12 17:24:53 +0100 | [diff] [blame] | 150 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 151 | /* global options */ |
| 152 | struct global global = { |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 153 | .hard_stop_after = TICK_ETERNITY, |
Willy Tarreau | 247a13a | 2012-11-15 17:38:15 +0100 | [diff] [blame] | 154 | .nbproc = 1, |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 155 | .nbthread = 0, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 156 | .req_count = 0, |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 157 | .logsrvs = LIST_HEAD_INIT(global.logsrvs), |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 158 | .maxzlibmem = 0, |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 159 | .comp_rate_lim = 0, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 160 | .ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED, |
Emeric Brun | ed76092 | 2010-10-22 17:59:25 +0200 | [diff] [blame] | 161 | .unix_bind = { |
| 162 | .ux = { |
| 163 | .uid = -1, |
| 164 | .gid = -1, |
| 165 | .mode = 0, |
| 166 | } |
| 167 | }, |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 168 | .tune = { |
Willy Tarreau | 7ac908b | 2019-02-27 12:02:18 +0100 | [diff] [blame] | 169 | .options = GTUNE_LISTENER_MQ, |
Willy Tarreau | c77d364 | 2018-12-12 06:19:42 +0100 | [diff] [blame] | 170 | .bufsize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)), |
Christopher Faulet | 546c469 | 2020-01-22 14:31:21 +0100 | [diff] [blame] | 171 | .maxrewrite = MAXREWRITE, |
Willy Tarreau | c77d364 | 2018-12-12 06:19:42 +0100 | [diff] [blame] | 172 | .chksize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)), |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 173 | .reserved_bufs = RESERVED_BUFS, |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 174 | .pattern_cache = DEFAULT_PAT_LRU_SIZE, |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 175 | .pool_low_ratio = 20, |
| 176 | .pool_high_ratio = 25, |
Christopher Faulet | 41ba36f | 2019-07-19 09:36:45 +0200 | [diff] [blame] | 177 | .max_http_hdr = MAX_HTTP_HDR, |
Emeric Brun | fc32aca | 2012-09-03 12:10:29 +0200 | [diff] [blame] | 178 | #ifdef USE_OPENSSL |
Emeric Brun | 4663577 | 2012-11-14 11:32:56 +0100 | [diff] [blame] | 179 | .sslcachesize = SSLCACHESIZE, |
Emeric Brun | fc32aca | 2012-09-03 12:10:29 +0200 | [diff] [blame] | 180 | #endif |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 181 | .comp_maxlevel = 1, |
Willy Tarreau | 7e31273 | 2014-02-12 16:35:14 +0100 | [diff] [blame] | 182 | #ifdef DEFAULT_IDLE_TIMER |
| 183 | .idle_timer = DEFAULT_IDLE_TIMER, |
| 184 | #else |
| 185 | .idle_timer = 1000, /* 1 second */ |
| 186 | #endif |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 187 | }, |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 188 | #ifdef USE_OPENSSL |
| 189 | #ifdef DEFAULT_MAXSSLCONN |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 190 | .maxsslconn = DEFAULT_MAXSSLCONN, |
| 191 | #endif |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 192 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 193 | /* others NULL OK */ |
| 194 | }; |
| 195 | |
| 196 | /*********************************************************************/ |
| 197 | |
| 198 | int stopping; /* non zero means stopping in progress */ |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 199 | int killed; /* non zero means a hard-stop is triggered */ |
Willy Tarreau | af7ad00 | 2010-08-31 15:39:26 +0200 | [diff] [blame] | 200 | int jobs = 0; /* number of active jobs (conns, listeners, active tasks, ...) */ |
William Lallemand | a719926 | 2018-11-16 16:57:20 +0100 | [diff] [blame] | 201 | int unstoppable_jobs = 0; /* number of active jobs that can't be stopped during a soft stop */ |
Willy Tarreau | 199ad24 | 2018-11-05 16:31:22 +0100 | [diff] [blame] | 202 | int active_peers = 0; /* number of active peers (connection attempts and connected) */ |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 203 | int connected_peers = 0; /* number of connected peers (verified ones) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 204 | |
| 205 | /* Here we store informations about the pids of the processes we may pause |
| 206 | * or kill. We will send them a signal every 10 ms until we can bind to all |
| 207 | * our ports. With 200 retries, that's about 2 seconds. |
| 208 | */ |
| 209 | #define MAX_START_RETRIES 200 |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 210 | static int *oldpids = NULL; |
| 211 | static int oldpids_sig; /* use USR1 or TERM */ |
| 212 | |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 213 | /* Path to the unix socket we use to retrieve listener sockets from the old process */ |
| 214 | static const char *old_unixsocket; |
| 215 | |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 216 | static char *cur_unixsocket = NULL; |
| 217 | |
William Lallemand | cb11fd2 | 2017-06-01 17:38:52 +0200 | [diff] [blame] | 218 | int atexit_flag = 0; |
| 219 | |
Willy Tarreau | bb545b4 | 2010-08-25 12:58:59 +0200 | [diff] [blame] | 220 | int nb_oldpids = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 221 | const int zero = 0; |
| 222 | const int one = 1; |
Alexandre Cassen | 87ea548 | 2007-10-11 20:48:58 +0200 | [diff] [blame] | 223 | const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 224 | |
Willy Tarreau | 1d21e0a | 2010-03-12 21:58:54 +0100 | [diff] [blame] | 225 | char hostname[MAX_HOSTNAME_LEN]; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 226 | char localpeer[MAX_HOSTNAME_LEN]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 227 | |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 228 | /* used from everywhere just to drain results we don't want to read and which |
| 229 | * recent versions of gcc increasingly and annoyingly complain about. |
| 230 | */ |
| 231 | int shut_your_big_mouth_gcc_int = 0; |
| 232 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 233 | static char **next_argv = NULL; |
| 234 | |
William Lallemand | bc19305 | 2018-09-11 10:06:26 +0200 | [diff] [blame] | 235 | struct list proc_list = LIST_HEAD_INIT(proc_list); |
| 236 | |
| 237 | int master = 0; /* 1 if in master, 0 if in child */ |
Willy Tarreau | bf69640 | 2019-03-01 10:09:28 +0100 | [diff] [blame] | 238 | unsigned int rlim_fd_cur_at_boot = 0; |
| 239 | unsigned int rlim_fd_max_at_boot = 0; |
William Lallemand | bc19305 | 2018-09-11 10:06:26 +0200 | [diff] [blame] | 240 | |
Willy Tarreau | 6c3a681 | 2020-03-06 18:57:15 +0100 | [diff] [blame] | 241 | /* per-boot randomness */ |
| 242 | unsigned char boot_seed[20]; /* per-boot random seed (160 bits initially) */ |
| 243 | |
William Lallemand | 16dd1b3 | 2018-11-19 18:46:18 +0100 | [diff] [blame] | 244 | struct mworker_proc *proc_self = NULL; |
William Lallemand | bc19305 | 2018-09-11 10:06:26 +0200 | [diff] [blame] | 245 | |
William Lallemand | b3f2be3 | 2018-09-11 10:06:18 +0200 | [diff] [blame] | 246 | static void *run_thread_poll_loop(void *data); |
| 247 | |
Willy Tarreau | ff05550 | 2014-04-28 22:27:06 +0200 | [diff] [blame] | 248 | /* bitfield of a few warnings to emit just once (WARN_*) */ |
| 249 | unsigned int warned = 0; |
| 250 | |
William Lallemand | e736115 | 2018-10-26 14:47:36 +0200 | [diff] [blame] | 251 | /* master CLI configuration (-S flag) */ |
| 252 | struct list mworker_cli_conf = LIST_HEAD_INIT(mworker_cli_conf); |
Willy Tarreau | cdb737e | 2016-12-21 18:43:10 +0100 | [diff] [blame] | 253 | |
| 254 | /* These are strings to be reported in the output of "haproxy -vv". They may |
| 255 | * either be constants (in which case must_free must be zero) or dynamically |
| 256 | * allocated strings to pass to free() on exit, and in this case must_free |
| 257 | * must be non-zero. |
| 258 | */ |
| 259 | struct list build_opts_list = LIST_HEAD_INIT(build_opts_list); |
| 260 | struct build_opts_str { |
| 261 | struct list list; |
| 262 | const char *str; |
| 263 | int must_free; |
| 264 | }; |
| 265 | |
Willy Tarreau | e694573 | 2016-12-21 19:57:00 +0100 | [diff] [blame] | 266 | /* These functions are called just after the point where the program exits |
| 267 | * after a config validity check, so they are generally suited for resource |
| 268 | * allocation and slow initializations that should be skipped during basic |
| 269 | * config checks. The functions must return 0 on success, or a combination |
| 270 | * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause |
| 271 | * and immediate exit, so the function must have emitted any useful error. |
| 272 | */ |
| 273 | struct list post_check_list = LIST_HEAD_INIT(post_check_list); |
| 274 | struct post_check_fct { |
| 275 | struct list list; |
| 276 | int (*fct)(); |
| 277 | }; |
| 278 | |
Christopher Faulet | c169296 | 2019-08-12 09:51:07 +0200 | [diff] [blame] | 279 | /* These functions are called for each proxy just after the config validity |
| 280 | * check. The functions must return 0 on success, or a combination of ERR_* |
| 281 | * flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause and immediate |
| 282 | * exit, so the function must have emitted any useful error. |
| 283 | */ |
| 284 | struct list post_proxy_check_list = LIST_HEAD_INIT(post_proxy_check_list); |
| 285 | struct post_proxy_check_fct { |
| 286 | struct list list; |
| 287 | int (*fct)(struct proxy *); |
| 288 | }; |
| 289 | |
| 290 | /* These functions are called for each server just after the config validity |
| 291 | * check. The functions must return 0 on success, or a combination of ERR_* |
| 292 | * flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause and immediate |
| 293 | * exit, so the function must have emitted any useful error. |
| 294 | */ |
| 295 | struct list post_server_check_list = LIST_HEAD_INIT(post_server_check_list); |
| 296 | struct post_server_check_fct { |
| 297 | struct list list; |
| 298 | int (*fct)(struct server *); |
| 299 | }; |
| 300 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 301 | /* These functions are called for each thread just after the thread creation |
| 302 | * and before running the init functions. They should be used to do per-thread |
| 303 | * (re-)allocations that are needed by subsequent functoins. They must return 0 |
| 304 | * if an error occurred. */ |
| 305 | struct list per_thread_alloc_list = LIST_HEAD_INIT(per_thread_alloc_list); |
| 306 | struct per_thread_alloc_fct { |
Willy Tarreau | 05554e6 | 2016-12-21 20:46:26 +0100 | [diff] [blame] | 307 | struct list list; |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 308 | int (*fct)(); |
Willy Tarreau | 05554e6 | 2016-12-21 20:46:26 +0100 | [diff] [blame] | 309 | }; |
| 310 | |
Christopher Faulet | 415f611 | 2017-07-25 16:52:58 +0200 | [diff] [blame] | 311 | /* These functions are called for each thread just after the thread creation |
| 312 | * and before running the scheduler. They should be used to do per-thread |
| 313 | * initializations. They must return 0 if an error occurred. */ |
| 314 | struct list per_thread_init_list = LIST_HEAD_INIT(per_thread_init_list); |
| 315 | struct per_thread_init_fct { |
| 316 | struct list list; |
| 317 | int (*fct)(); |
| 318 | }; |
| 319 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 320 | /* These functions are called when freeing the global sections at the end of |
| 321 | * deinit, after everything is stopped. They don't return anything. They should |
| 322 | * not release shared resources that are possibly used by other deinit |
| 323 | * functions, only close/release what is private. Use the per_thread_free_list |
| 324 | * to release shared resources. |
| 325 | */ |
| 326 | struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list); |
| 327 | struct post_deinit_fct { |
| 328 | struct list list; |
| 329 | void (*fct)(); |
| 330 | }; |
| 331 | |
Christopher Faulet | 3ea5cbe | 2019-07-31 08:44:12 +0200 | [diff] [blame] | 332 | /* These functions are called when freeing a proxy during the deinit, after |
| 333 | * everything isg stopped. They don't return anything. They should not release |
| 334 | * the proxy itself or any shared resources that are possibly used by other |
| 335 | * deinit functions, only close/release what is private. |
| 336 | */ |
| 337 | struct list proxy_deinit_list = LIST_HEAD_INIT(proxy_deinit_list); |
| 338 | struct proxy_deinit_fct { |
| 339 | struct list list; |
| 340 | void (*fct)(struct proxy *); |
| 341 | }; |
| 342 | |
| 343 | /* These functions are called when freeing a server during the deinit, after |
| 344 | * everything isg stopped. They don't return anything. They should not release |
| 345 | * the proxy itself or any shared resources that are possibly used by other |
| 346 | * deinit functions, only close/release what is private. |
| 347 | */ |
| 348 | struct list server_deinit_list = LIST_HEAD_INIT(server_deinit_list); |
| 349 | struct server_deinit_fct { |
| 350 | struct list list; |
| 351 | void (*fct)(struct server *); |
| 352 | }; |
| 353 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 354 | /* These functions are called when freeing the global sections at the end of |
| 355 | * deinit, after the thread deinit functions, to release unneeded memory |
| 356 | * allocations. They don't return anything, and they work in best effort mode |
| 357 | * as their sole goal is to make valgrind mostly happy. |
| 358 | */ |
| 359 | struct list per_thread_free_list = LIST_HEAD_INIT(per_thread_free_list); |
| 360 | struct per_thread_free_fct { |
| 361 | struct list list; |
| 362 | int (*fct)(); |
| 363 | }; |
| 364 | |
Christopher Faulet | 415f611 | 2017-07-25 16:52:58 +0200 | [diff] [blame] | 365 | /* These functions are called for each thread just after the scheduler loop and |
| 366 | * before exiting the thread. They don't return anything and, as for post-deinit |
| 367 | * functions, they work in best effort mode as their sole goal is to make |
| 368 | * valgrind mostly happy. */ |
| 369 | struct list per_thread_deinit_list = LIST_HEAD_INIT(per_thread_deinit_list); |
| 370 | struct per_thread_deinit_fct { |
| 371 | struct list list; |
| 372 | void (*fct)(); |
| 373 | }; |
| 374 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 375 | /*********************************************************************/ |
| 376 | /* general purpose functions ***************************************/ |
| 377 | /*********************************************************************/ |
| 378 | |
Willy Tarreau | cdb737e | 2016-12-21 18:43:10 +0100 | [diff] [blame] | 379 | /* used to register some build option strings at boot. Set must_free to |
| 380 | * non-zero if the string must be freed upon exit. |
| 381 | */ |
| 382 | void hap_register_build_opts(const char *str, int must_free) |
| 383 | { |
| 384 | struct build_opts_str *b; |
| 385 | |
| 386 | b = calloc(1, sizeof(*b)); |
| 387 | if (!b) { |
| 388 | fprintf(stderr, "out of memory\n"); |
| 389 | exit(1); |
| 390 | } |
| 391 | b->str = str; |
| 392 | b->must_free = must_free; |
| 393 | LIST_ADDQ(&build_opts_list, &b->list); |
| 394 | } |
| 395 | |
Willy Tarreau | e694573 | 2016-12-21 19:57:00 +0100 | [diff] [blame] | 396 | /* used to register some initialization functions to call after the checks. */ |
| 397 | void hap_register_post_check(int (*fct)()) |
| 398 | { |
| 399 | struct post_check_fct *b; |
| 400 | |
| 401 | b = calloc(1, sizeof(*b)); |
| 402 | if (!b) { |
| 403 | fprintf(stderr, "out of memory\n"); |
| 404 | exit(1); |
| 405 | } |
| 406 | b->fct = fct; |
| 407 | LIST_ADDQ(&post_check_list, &b->list); |
| 408 | } |
| 409 | |
Christopher Faulet | c169296 | 2019-08-12 09:51:07 +0200 | [diff] [blame] | 410 | /* used to register some initialization functions to call for each proxy after |
| 411 | * the checks. |
| 412 | */ |
| 413 | void hap_register_post_proxy_check(int (*fct)(struct proxy *)) |
| 414 | { |
| 415 | struct post_proxy_check_fct *b; |
| 416 | |
| 417 | b = calloc(1, sizeof(*b)); |
| 418 | if (!b) { |
| 419 | fprintf(stderr, "out of memory\n"); |
| 420 | exit(1); |
| 421 | } |
| 422 | b->fct = fct; |
| 423 | LIST_ADDQ(&post_proxy_check_list, &b->list); |
| 424 | } |
| 425 | |
| 426 | /* used to register some initialization functions to call for each server after |
| 427 | * the checks. |
| 428 | */ |
| 429 | void hap_register_post_server_check(int (*fct)(struct server *)) |
| 430 | { |
| 431 | struct post_server_check_fct *b; |
| 432 | |
| 433 | b = calloc(1, sizeof(*b)); |
| 434 | if (!b) { |
| 435 | fprintf(stderr, "out of memory\n"); |
| 436 | exit(1); |
| 437 | } |
| 438 | b->fct = fct; |
| 439 | LIST_ADDQ(&post_server_check_list, &b->list); |
| 440 | } |
| 441 | |
Willy Tarreau | 05554e6 | 2016-12-21 20:46:26 +0100 | [diff] [blame] | 442 | /* used to register some de-initialization functions to call after everything |
| 443 | * has stopped. |
| 444 | */ |
| 445 | void hap_register_post_deinit(void (*fct)()) |
| 446 | { |
| 447 | struct post_deinit_fct *b; |
| 448 | |
| 449 | b = calloc(1, sizeof(*b)); |
| 450 | if (!b) { |
| 451 | fprintf(stderr, "out of memory\n"); |
| 452 | exit(1); |
| 453 | } |
| 454 | b->fct = fct; |
| 455 | LIST_ADDQ(&post_deinit_list, &b->list); |
| 456 | } |
| 457 | |
Christopher Faulet | 3ea5cbe | 2019-07-31 08:44:12 +0200 | [diff] [blame] | 458 | /* used to register some per proxy de-initialization functions to call after |
| 459 | * everything has stopped. |
| 460 | */ |
| 461 | void hap_register_proxy_deinit(void (*fct)(struct proxy *)) |
| 462 | { |
| 463 | struct proxy_deinit_fct *b; |
| 464 | |
| 465 | b = calloc(1, sizeof(*b)); |
| 466 | if (!b) { |
| 467 | fprintf(stderr, "out of memory\n"); |
| 468 | exit(1); |
| 469 | } |
| 470 | b->fct = fct; |
| 471 | LIST_ADDQ(&proxy_deinit_list, &b->list); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | /* used to register some per server de-initialization functions to call after |
| 476 | * everything has stopped. |
| 477 | */ |
| 478 | void hap_register_server_deinit(void (*fct)(struct server *)) |
| 479 | { |
| 480 | struct server_deinit_fct *b; |
| 481 | |
| 482 | b = calloc(1, sizeof(*b)); |
| 483 | if (!b) { |
| 484 | fprintf(stderr, "out of memory\n"); |
| 485 | exit(1); |
| 486 | } |
| 487 | b->fct = fct; |
| 488 | LIST_ADDQ(&server_deinit_list, &b->list); |
| 489 | } |
| 490 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 491 | /* used to register some allocation functions to call for each thread. */ |
| 492 | void hap_register_per_thread_alloc(int (*fct)()) |
| 493 | { |
| 494 | struct per_thread_alloc_fct *b; |
| 495 | |
| 496 | b = calloc(1, sizeof(*b)); |
| 497 | if (!b) { |
| 498 | fprintf(stderr, "out of memory\n"); |
| 499 | exit(1); |
| 500 | } |
| 501 | b->fct = fct; |
| 502 | LIST_ADDQ(&per_thread_alloc_list, &b->list); |
| 503 | } |
| 504 | |
Christopher Faulet | 415f611 | 2017-07-25 16:52:58 +0200 | [diff] [blame] | 505 | /* used to register some initialization functions to call for each thread. */ |
| 506 | void hap_register_per_thread_init(int (*fct)()) |
| 507 | { |
| 508 | struct per_thread_init_fct *b; |
| 509 | |
| 510 | b = calloc(1, sizeof(*b)); |
| 511 | if (!b) { |
| 512 | fprintf(stderr, "out of memory\n"); |
| 513 | exit(1); |
| 514 | } |
| 515 | b->fct = fct; |
| 516 | LIST_ADDQ(&per_thread_init_list, &b->list); |
| 517 | } |
| 518 | |
| 519 | /* used to register some de-initialization functions to call for each thread. */ |
| 520 | void hap_register_per_thread_deinit(void (*fct)()) |
| 521 | { |
| 522 | struct per_thread_deinit_fct *b; |
| 523 | |
| 524 | b = calloc(1, sizeof(*b)); |
| 525 | if (!b) { |
| 526 | fprintf(stderr, "out of memory\n"); |
| 527 | exit(1); |
| 528 | } |
| 529 | b->fct = fct; |
| 530 | LIST_ADDQ(&per_thread_deinit_list, &b->list); |
| 531 | } |
| 532 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 533 | /* used to register some free functions to call for each thread. */ |
| 534 | void hap_register_per_thread_free(int (*fct)()) |
| 535 | { |
| 536 | struct per_thread_free_fct *b; |
| 537 | |
| 538 | b = calloc(1, sizeof(*b)); |
| 539 | if (!b) { |
| 540 | fprintf(stderr, "out of memory\n"); |
| 541 | exit(1); |
| 542 | } |
| 543 | b->fct = fct; |
| 544 | LIST_ADDQ(&per_thread_free_list, &b->list); |
| 545 | } |
| 546 | |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 547 | static void display_version() |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 548 | { |
Willy Tarreau | 08dd202 | 2019-11-21 18:07:30 +0100 | [diff] [blame] | 549 | printf("HA-Proxy version %s %s - https://haproxy.org/\n" |
| 550 | PRODUCT_STATUS "\n", haproxy_version, haproxy_date); |
Willy Tarreau | 47479eb | 2019-11-21 18:48:20 +0100 | [diff] [blame] | 551 | |
| 552 | if (strlen(PRODUCT_URL_BUGS) > 0) { |
| 553 | char base_version[20]; |
| 554 | int dots = 0; |
| 555 | char *del; |
| 556 | |
| 557 | /* only retrieve the base version without distro-specific extensions */ |
| 558 | for (del = haproxy_version; *del; del++) { |
| 559 | if (*del == '.') |
| 560 | dots++; |
| 561 | else if (*del < '0' || *del > '9') |
| 562 | break; |
| 563 | } |
| 564 | |
| 565 | strlcpy2(base_version, haproxy_version, del - haproxy_version + 1); |
| 566 | if (dots < 2) |
| 567 | printf("Known bugs: https://github.com/haproxy/haproxy/issues?q=is:issue+is:open\n"); |
| 568 | else |
| 569 | printf("Known bugs: " PRODUCT_URL_BUGS "\n", base_version); |
| 570 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 571 | } |
| 572 | |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 573 | static void display_build_opts() |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 574 | { |
Willy Tarreau | cdb737e | 2016-12-21 18:43:10 +0100 | [diff] [blame] | 575 | struct build_opts_str *item; |
| 576 | |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 577 | printf("Build options :" |
| 578 | #ifdef BUILD_TARGET |
Willy Tarreau | 9f2b730 | 2008-01-02 20:48:34 +0100 | [diff] [blame] | 579 | "\n TARGET = " BUILD_TARGET |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 580 | #endif |
| 581 | #ifdef BUILD_CPU |
Willy Tarreau | 9f2b730 | 2008-01-02 20:48:34 +0100 | [diff] [blame] | 582 | "\n CPU = " BUILD_CPU |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 583 | #endif |
| 584 | #ifdef BUILD_CC |
Willy Tarreau | 9f2b730 | 2008-01-02 20:48:34 +0100 | [diff] [blame] | 585 | "\n CC = " BUILD_CC |
| 586 | #endif |
| 587 | #ifdef BUILD_CFLAGS |
| 588 | "\n CFLAGS = " BUILD_CFLAGS |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 589 | #endif |
Willy Tarreau | 9f2b730 | 2008-01-02 20:48:34 +0100 | [diff] [blame] | 590 | #ifdef BUILD_OPTIONS |
| 591 | "\n OPTIONS = " BUILD_OPTIONS |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 592 | #endif |
Willy Tarreau | 7728ed3 | 2019-03-27 13:20:08 +0100 | [diff] [blame] | 593 | #ifdef BUILD_FEATURES |
| 594 | "\n\nFeature list : " BUILD_FEATURES |
| 595 | #endif |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 596 | "\n\nDefault settings :" |
Willy Tarreau | ca783d4 | 2019-03-13 10:03:07 +0100 | [diff] [blame] | 597 | "\n bufsize = %d, maxrewrite = %d, maxpollevents = %d" |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 598 | "\n\n", |
Willy Tarreau | ca783d4 | 2019-03-13 10:03:07 +0100 | [diff] [blame] | 599 | BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS); |
Willy Tarreau | be5b685 | 2009-10-03 18:57:08 +0200 | [diff] [blame] | 600 | |
Willy Tarreau | cdb737e | 2016-12-21 18:43:10 +0100 | [diff] [blame] | 601 | list_for_each_entry(item, &build_opts_list, list) { |
| 602 | puts(item->str); |
| 603 | } |
| 604 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 605 | putchar('\n'); |
| 606 | |
Willy Tarreau | be5b685 | 2009-10-03 18:57:08 +0200 | [diff] [blame] | 607 | list_pollers(stdout); |
| 608 | putchar('\n'); |
Christopher Faulet | 98d9fe2 | 2018-04-10 14:37:32 +0200 | [diff] [blame] | 609 | list_mux_proto(stdout); |
| 610 | putchar('\n'); |
Willy Tarreau | 679bba1 | 2019-03-19 08:08:10 +0100 | [diff] [blame] | 611 | list_services(stdout); |
| 612 | putchar('\n'); |
Christopher Faulet | b3f4e14 | 2016-03-07 12:46:38 +0100 | [diff] [blame] | 613 | list_filters(stdout); |
| 614 | putchar('\n'); |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 615 | } |
| 616 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 617 | /* |
| 618 | * This function prints the command line usage and exits |
| 619 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 620 | static void usage(char *name) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 621 | { |
| 622 | display_version(); |
| 623 | fprintf(stderr, |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 624 | "Usage : %s [-f <cfgfile|cfgdir>]* [ -vdV" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 625 | "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n" |
Willy Tarreau | a088d31 | 2015-10-08 11:58:48 +0200 | [diff] [blame] | 626 | " [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n" |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 627 | " -v displays version ; -vv shows known build options.\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 628 | " -d enters debug mode ; -db only disables background mode.\n" |
Willy Tarreau | 6e06443 | 2012-05-08 15:40:42 +0200 | [diff] [blame] | 629 | " -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 630 | " -V enters verbose mode (disables quiet mode)\n" |
Willy Tarreau | 576132e | 2011-09-10 19:26:56 +0200 | [diff] [blame] | 631 | " -D goes daemon ; -C changes to <dir> before loading files.\n" |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 632 | " -W master-worker mode.\n" |
Tim Duesterhus | d6942c8 | 2017-11-20 15:58:35 +0100 | [diff] [blame] | 633 | #if defined(USE_SYSTEMD) |
| 634 | " -Ws master-worker mode with systemd notify support.\n" |
| 635 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 636 | " -q quiet mode : don't display messages\n" |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 637 | " -c check mode : only check config files and exit\n" |
Willy Tarreau | ca783d4 | 2019-03-13 10:03:07 +0100 | [diff] [blame] | 638 | " -n sets the maximum total # of connections (uses ulimit -n)\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 639 | " -m limits the usable amount of memory (in MB)\n" |
| 640 | " -N sets the default, per-proxy maximum # of connections (%d)\n" |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 641 | " -L set local peer name (default to hostname)\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 642 | " -p writes pids of all children to this file\n" |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 643 | #if defined(USE_EPOLL) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 644 | " -de disables epoll() usage even when available\n" |
| 645 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 646 | #if defined(USE_KQUEUE) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 647 | " -dk disables kqueue() usage even when available\n" |
| 648 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 649 | #if defined(USE_EVPORTS) |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 650 | " -dv disables event ports usage even when available\n" |
| 651 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 652 | #if defined(USE_POLL) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 653 | " -dp disables poll() usage even when available\n" |
| 654 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 655 | #if defined(USE_LINUX_SPLICE) |
Willy Tarreau | 3ab68cf | 2009-01-25 16:03:28 +0100 | [diff] [blame] | 656 | " -dS disables splice usage (broken on old kernels)\n" |
| 657 | #endif |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 658 | #if defined(USE_GETADDRINFO) |
| 659 | " -dG disables getaddrinfo() usage\n" |
| 660 | #endif |
Lukas Tribus | a0bcbdc | 2016-09-12 21:42:20 +0000 | [diff] [blame] | 661 | #if defined(SO_REUSEPORT) |
| 662 | " -dR disables SO_REUSEPORT usage\n" |
| 663 | #endif |
Willy Tarreau | 3eed10e | 2016-11-07 21:03:16 +0100 | [diff] [blame] | 664 | " -dr ignores server address resolution failures\n" |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 665 | " -dV disables SSL verify on servers side\n" |
Willy Tarreau | c6ca1aa | 2015-10-08 11:32:32 +0200 | [diff] [blame] | 666 | " -sf/-st [pid ]* finishes/terminates old pids.\n" |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 667 | " -x <unix_socket> get listening sockets from a unix socket\n" |
William Lallemand | 63329e3 | 2019-06-13 17:03:37 +0200 | [diff] [blame] | 668 | " -S <bind>[,<bind options>...] new master CLI\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 669 | "\n", |
Willy Tarreau | ca783d4 | 2019-03-13 10:03:07 +0100 | [diff] [blame] | 670 | name, cfg_maxpconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 671 | exit(1); |
| 672 | } |
| 673 | |
| 674 | |
| 675 | |
| 676 | /*********************************************************************/ |
| 677 | /* more specific functions ***************************************/ |
| 678 | /*********************************************************************/ |
| 679 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 680 | /* sends the signal <sig> to all pids found in <oldpids>. Returns the number of |
| 681 | * pids the signal was correctly delivered to. |
| 682 | */ |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 683 | int tell_old_pids(int sig) |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 684 | { |
| 685 | int p; |
| 686 | int ret = 0; |
| 687 | for (p = 0; p < nb_oldpids; p++) |
| 688 | if (kill(oldpids[p], sig) == 0) |
| 689 | ret++; |
| 690 | return ret; |
| 691 | } |
| 692 | |
William Lallemand | 75ea0a0 | 2017-11-15 19:02:58 +0100 | [diff] [blame] | 693 | /* |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 694 | * remove a pid forom the olpid array and decrease nb_oldpids |
| 695 | * return 1 pid was found otherwise return 0 |
| 696 | */ |
| 697 | |
| 698 | int delete_oldpid(int pid) |
| 699 | { |
| 700 | int i; |
| 701 | |
| 702 | for (i = 0; i < nb_oldpids; i++) { |
| 703 | if (oldpids[i] == pid) { |
| 704 | oldpids[i] = oldpids[nb_oldpids - 1]; |
| 705 | oldpids[nb_oldpids - 1] = 0; |
| 706 | nb_oldpids--; |
| 707 | return 1; |
| 708 | } |
| 709 | } |
| 710 | return 0; |
| 711 | } |
| 712 | |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 713 | |
| 714 | static void get_cur_unixsocket() |
| 715 | { |
| 716 | /* if -x was used, try to update the stat socket if not available anymore */ |
| 717 | if (global.stats_fe) { |
| 718 | struct bind_conf *bind_conf; |
| 719 | |
| 720 | /* pass through all stats socket */ |
| 721 | list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) { |
| 722 | struct listener *l; |
| 723 | |
| 724 | list_for_each_entry(l, &bind_conf->listeners, by_bind) { |
| 725 | |
| 726 | if (l->addr.ss_family == AF_UNIX && |
| 727 | (bind_conf->level & ACCESS_FD_LISTENERS)) { |
| 728 | const struct sockaddr_un *un; |
| 729 | |
| 730 | un = (struct sockaddr_un *)&l->addr; |
| 731 | /* priority to old_unixsocket */ |
| 732 | if (!cur_unixsocket) { |
| 733 | cur_unixsocket = strdup(un->sun_path); |
| 734 | } else { |
| 735 | if (old_unixsocket && !strcmp(un->sun_path, old_unixsocket)) { |
| 736 | free(cur_unixsocket); |
| 737 | cur_unixsocket = strdup(old_unixsocket); |
| 738 | return; |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | if (!cur_unixsocket && old_unixsocket) |
| 746 | cur_unixsocket = strdup(old_unixsocket); |
| 747 | } |
| 748 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 749 | /* |
| 750 | * When called, this function reexec haproxy with -sf followed by current |
Joseph Herlant | 0342090 | 2018-11-15 10:41:50 -0800 | [diff] [blame] | 751 | * children PIDs and possibly old children PIDs if they didn't leave yet. |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 752 | */ |
William Lallemand | a57b7e3 | 2018-12-14 21:11:31 +0100 | [diff] [blame] | 753 | void mworker_reload() |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 754 | { |
| 755 | int next_argc = 0; |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 756 | char *msg = NULL; |
Willy Tarreau | 8dca195 | 2019-03-01 10:21:55 +0100 | [diff] [blame] | 757 | struct rlimit limit; |
William Lallemand | 7c756a8 | 2018-11-26 11:53:40 +0100 | [diff] [blame] | 758 | struct per_thread_deinit_fct *ptdf; |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 759 | |
| 760 | mworker_block_signals(); |
Tim Duesterhus | d6942c8 | 2017-11-20 15:58:35 +0100 | [diff] [blame] | 761 | #if defined(USE_SYSTEMD) |
| 762 | if (global.tune.options & GTUNE_USE_SYSTEMD) |
| 763 | sd_notify(0, "RELOADING=1"); |
| 764 | #endif |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 765 | setenv("HAPROXY_MWORKER_REEXEC", "1", 1); |
| 766 | |
William Lallemand | bc19305 | 2018-09-11 10:06:26 +0200 | [diff] [blame] | 767 | mworker_proc_list_to_env(); /* put the children description in the env */ |
| 768 | |
William Lallemand | 7c756a8 | 2018-11-26 11:53:40 +0100 | [diff] [blame] | 769 | /* during the reload we must ensure that every FDs that can't be |
| 770 | * reuse (ie those that are not referenced in the proc_list) |
| 771 | * are closed or they will leak. */ |
| 772 | |
| 773 | /* close the listeners FD */ |
| 774 | mworker_cli_proxy_stop(); |
William Lallemand | 1686667 | 2019-06-24 17:40:48 +0200 | [diff] [blame] | 775 | |
| 776 | if (getenv("HAPROXY_MWORKER_WAIT_ONLY") == NULL) { |
| 777 | /* close the poller FD and the thread waker pipe FD */ |
| 778 | list_for_each_entry(ptdf, &per_thread_deinit_list, list) |
| 779 | ptdf->fct(); |
| 780 | if (fdtab) |
| 781 | deinit_pollers(); |
| 782 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 783 | #if defined(USE_OPENSSL) && (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
William Lallemand | 5fdb5b3 | 2019-10-15 14:04:08 +0200 | [diff] [blame] | 784 | /* close random device FDs */ |
| 785 | RAND_keep_random_devices_open(0); |
Rob Allen | 56996da | 2019-05-03 09:11:32 +0100 | [diff] [blame] | 786 | #endif |
William Lallemand | 7c756a8 | 2018-11-26 11:53:40 +0100 | [diff] [blame] | 787 | |
Willy Tarreau | 8dca195 | 2019-03-01 10:21:55 +0100 | [diff] [blame] | 788 | /* restore the initial FD limits */ |
| 789 | limit.rlim_cur = rlim_fd_cur_at_boot; |
| 790 | limit.rlim_max = rlim_fd_max_at_boot; |
| 791 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 792 | getrlimit(RLIMIT_NOFILE, &limit); |
| 793 | ha_warning("Failed to restore initial FD limits (cur=%u max=%u), using cur=%u max=%u\n", |
| 794 | rlim_fd_cur_at_boot, rlim_fd_max_at_boot, |
| 795 | (unsigned int)limit.rlim_cur, (unsigned int)limit.rlim_max); |
| 796 | } |
| 797 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 798 | /* compute length */ |
| 799 | while (next_argv[next_argc]) |
| 800 | next_argc++; |
| 801 | |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 802 | /* 1 for haproxy -sf, 2 for -x /socket */ |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 803 | next_argv = realloc(next_argv, (next_argc + 1 + 2 + mworker_child_nb() + nb_oldpids + 1) * sizeof(char *)); |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 804 | if (next_argv == NULL) |
| 805 | goto alloc_error; |
| 806 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 807 | /* add -sf <PID>* to argv */ |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 808 | if (mworker_child_nb() > 0) { |
| 809 | struct mworker_proc *child; |
| 810 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 811 | next_argv[next_argc++] = "-sf"; |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 812 | |
| 813 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 677e2f2 | 2019-11-19 17:04:18 +0100 | [diff] [blame] | 814 | if (!(child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) || child->pid <= -1 ) |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 815 | continue; |
| 816 | next_argv[next_argc] = memprintf(&msg, "%d", child->pid); |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 817 | if (next_argv[next_argc] == NULL) |
| 818 | goto alloc_error; |
| 819 | msg = NULL; |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 820 | next_argc++; |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 821 | } |
| 822 | } |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 823 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 824 | next_argv[next_argc] = NULL; |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 825 | |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 826 | /* add the -x option with the stat socket */ |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 827 | if (cur_unixsocket) { |
| 828 | |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 829 | next_argv[next_argc++] = "-x"; |
| 830 | next_argv[next_argc++] = (char *)cur_unixsocket; |
| 831 | next_argv[next_argc++] = NULL; |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 832 | } |
| 833 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 834 | ha_warning("Reexecuting Master process\n"); |
Willy Tarreau | e0d86e2 | 2019-08-26 10:37:39 +0200 | [diff] [blame] | 835 | signal(SIGPROF, SIG_IGN); |
Tim Duesterhus | 0436ab7 | 2017-11-12 17:39:18 +0100 | [diff] [blame] | 836 | execvp(next_argv[0], next_argv); |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 837 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 838 | ha_warning("Failed to reexecute the master process [%d]: %s\n", pid, strerror(errno)); |
William Lallemand | 722d4ca | 2017-11-15 19:02:55 +0100 | [diff] [blame] | 839 | return; |
| 840 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 841 | alloc_error: |
Joseph Herlant | 07a0834 | 2018-11-15 10:43:05 -0800 | [diff] [blame] | 842 | ha_warning("Failed to reexecute the master process [%d]: Cannot allocate memory\n", pid); |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 843 | return; |
| 844 | } |
| 845 | |
William Lallemand | b3f2be3 | 2018-09-11 10:06:18 +0200 | [diff] [blame] | 846 | static void mworker_loop() |
| 847 | { |
| 848 | |
| 849 | #if defined(USE_SYSTEMD) |
| 850 | if (global.tune.options & GTUNE_USE_SYSTEMD) |
| 851 | sd_notifyf(0, "READY=1\nMAINPID=%lu", (unsigned long)getpid()); |
| 852 | #endif |
Willy Tarreau | d83b6c1 | 2019-04-18 11:31:36 +0200 | [diff] [blame] | 853 | /* Busy polling makes no sense in the master :-) */ |
| 854 | global.tune.options &= ~GTUNE_BUSY_POLLING; |
William Lallemand | b3f2be3 | 2018-09-11 10:06:18 +0200 | [diff] [blame] | 855 | |
William Lallemand | bc19305 | 2018-09-11 10:06:26 +0200 | [diff] [blame] | 856 | master = 1; |
| 857 | |
Willy Tarreau | d26c9f9 | 2019-12-11 14:24:07 +0100 | [diff] [blame] | 858 | signal_unregister(SIGTTIN); |
| 859 | signal_unregister(SIGTTOU); |
William Lallemand | 0564d41 | 2018-11-20 17:36:53 +0100 | [diff] [blame] | 860 | signal_unregister(SIGUSR1); |
| 861 | signal_unregister(SIGHUP); |
| 862 | signal_unregister(SIGQUIT); |
| 863 | |
William Lallemand | b3f2be3 | 2018-09-11 10:06:18 +0200 | [diff] [blame] | 864 | signal_register_fct(SIGTERM, mworker_catch_sigterm, SIGTERM); |
| 865 | signal_register_fct(SIGUSR1, mworker_catch_sigterm, SIGUSR1); |
Willy Tarreau | d26c9f9 | 2019-12-11 14:24:07 +0100 | [diff] [blame] | 866 | signal_register_fct(SIGTTIN, mworker_broadcast_signal, SIGTTIN); |
| 867 | signal_register_fct(SIGTTOU, mworker_broadcast_signal, SIGTTOU); |
William Lallemand | b3f2be3 | 2018-09-11 10:06:18 +0200 | [diff] [blame] | 868 | signal_register_fct(SIGINT, mworker_catch_sigterm, SIGINT); |
| 869 | signal_register_fct(SIGHUP, mworker_catch_sighup, SIGHUP); |
| 870 | signal_register_fct(SIGUSR2, mworker_catch_sighup, SIGUSR2); |
| 871 | signal_register_fct(SIGCHLD, mworker_catch_sigchld, SIGCHLD); |
| 872 | |
| 873 | mworker_unblock_signals(); |
| 874 | mworker_cleanlisteners(); |
William Lallemand | 27f3fa5 | 2018-12-06 14:05:20 +0100 | [diff] [blame] | 875 | mworker_cleantasks(); |
William Lallemand | b3f2be3 | 2018-09-11 10:06:18 +0200 | [diff] [blame] | 876 | |
William Lallemand | bc19305 | 2018-09-11 10:06:26 +0200 | [diff] [blame] | 877 | mworker_catch_sigchld(NULL); /* ensure we clean the children in case |
| 878 | some SIGCHLD were lost */ |
| 879 | |
William Lallemand | b3f2be3 | 2018-09-11 10:06:18 +0200 | [diff] [blame] | 880 | global.nbthread = 1; |
| 881 | relative_pid = 1; |
| 882 | pid_bit = 1; |
Willy Tarreau | a38a717 | 2019-02-02 17:11:28 +0100 | [diff] [blame] | 883 | all_proc_mask = 1; |
William Lallemand | b3f2be3 | 2018-09-11 10:06:18 +0200 | [diff] [blame] | 884 | |
William Lallemand | 2672eb9 | 2018-12-14 15:52:39 +0100 | [diff] [blame] | 885 | #ifdef USE_THREAD |
| 886 | tid_bit = 1; |
| 887 | all_threads_mask = 1; |
| 888 | #endif |
| 889 | |
William Lallemand | b3f2be3 | 2018-09-11 10:06:18 +0200 | [diff] [blame] | 890 | jobs++; /* this is the "master" job, we want to take care of the |
| 891 | signals even if there is no listener so the poll loop don't |
| 892 | leave */ |
| 893 | |
| 894 | fork_poller(); |
Willy Tarreau | b4f7cc3 | 2019-05-03 09:27:30 +0200 | [diff] [blame] | 895 | run_thread_poll_loop(0); |
William Lallemand | b3f2be3 | 2018-09-11 10:06:18 +0200 | [diff] [blame] | 896 | } |
William Lallemand | cb11fd2 | 2017-06-01 17:38:52 +0200 | [diff] [blame] | 897 | |
| 898 | /* |
| 899 | * Reexec the process in failure mode, instead of exiting |
| 900 | */ |
| 901 | void reexec_on_failure() |
| 902 | { |
| 903 | if (!atexit_flag) |
| 904 | return; |
| 905 | |
| 906 | setenv("HAPROXY_MWORKER_WAIT_ONLY", "1", 1); |
| 907 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 908 | ha_warning("Reexecuting Master process in waitpid mode\n"); |
William Lallemand | cb11fd2 | 2017-06-01 17:38:52 +0200 | [diff] [blame] | 909 | mworker_reload(); |
William Lallemand | cb11fd2 | 2017-06-01 17:38:52 +0200 | [diff] [blame] | 910 | } |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 911 | |
| 912 | |
| 913 | /* |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 914 | * upon SIGUSR1, let's have a soft stop. Note that soft_stop() broadcasts |
| 915 | * a signal zero to all subscribers. This means that it's as easy as |
| 916 | * subscribing to signal 0 to get informed about an imminent shutdown. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 917 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 918 | static void sig_soft_stop(struct sig_handler *sh) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 919 | { |
| 920 | soft_stop(); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 921 | signal_unregister_handler(sh); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 922 | pool_gc(NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | /* |
| 926 | * upon SIGTTOU, we pause everything |
| 927 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 928 | static void sig_pause(struct sig_handler *sh) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 929 | { |
| 930 | pause_proxies(); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 931 | pool_gc(NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /* |
| 935 | * upon SIGTTIN, let's have a soft stop. |
| 936 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 937 | static void sig_listen(struct sig_handler *sh) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 938 | { |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 939 | resume_proxies(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* |
| 943 | * this function dumps every server's state when the process receives SIGHUP. |
| 944 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 945 | static void sig_dump_state(struct sig_handler *sh) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 946 | { |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 947 | struct proxy *p = proxies_list; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 948 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 949 | ha_warning("SIGHUP received, dumping servers states.\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 950 | while (p) { |
| 951 | struct server *s = p->srv; |
| 952 | |
| 953 | send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id); |
| 954 | while (s) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 955 | chunk_printf(&trash, |
| 956 | "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.", |
| 957 | p->id, s->id, |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 958 | (s->cur_state != SRV_ST_STOPPED) ? "UP" : "DOWN", |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 959 | s->cur_sess, s->nbpend, s->counters.cum_sess); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 960 | ha_warning("%s\n", trash.area); |
| 961 | send_log(p, LOG_NOTICE, "%s\n", trash.area); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 962 | s = s->next; |
| 963 | } |
| 964 | |
Willy Tarreau | 5fcc8f1 | 2007-09-17 11:27:09 +0200 | [diff] [blame] | 965 | /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */ |
| 966 | if (!p->srv) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 967 | chunk_printf(&trash, |
| 968 | "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.", |
| 969 | p->id, |
| 970 | 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] | 971 | } else if (p->srv_act == 0) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 972 | chunk_printf(&trash, |
| 973 | "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.", |
| 974 | p->id, |
| 975 | (p->srv_bck) ? "is running on backup servers" : "has no server available", |
| 976 | 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] | 977 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 978 | chunk_printf(&trash, |
| 979 | "SIGHUP: Proxy %s has %d active servers and %d backup servers available." |
| 980 | " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.", |
| 981 | p->id, p->srv_act, p->srv_bck, |
| 982 | 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] | 983 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 984 | ha_warning("%s\n", trash.area); |
| 985 | send_log(p, LOG_NOTICE, "%s\n", trash.area); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 986 | |
| 987 | p = p->next; |
| 988 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 989 | } |
| 990 | |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 991 | static void dump(struct sig_handler *sh) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 992 | { |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 993 | /* dump memory usage then free everything possible */ |
| 994 | dump_pools(); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 995 | pool_gc(NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 996 | } |
| 997 | |
William Lallemand | e134041 | 2017-12-28 16:09:36 +0100 | [diff] [blame] | 998 | /* |
| 999 | * This function dup2 the stdio FDs (0,1,2) with <fd>, then closes <fd> |
| 1000 | * If <fd> < 0, it opens /dev/null and use it to dup |
| 1001 | * |
| 1002 | * In the case of chrooting, you have to open /dev/null before the chroot, and |
| 1003 | * pass the <fd> to this function |
| 1004 | */ |
| 1005 | static void stdio_quiet(int fd) |
| 1006 | { |
| 1007 | if (fd < 0) |
| 1008 | fd = open("/dev/null", O_RDWR, 0); |
| 1009 | |
| 1010 | if (fd > -1) { |
| 1011 | fclose(stdin); |
| 1012 | fclose(stdout); |
| 1013 | fclose(stderr); |
| 1014 | |
| 1015 | dup2(fd, 0); |
| 1016 | dup2(fd, 1); |
| 1017 | dup2(fd, 2); |
| 1018 | if (fd > 2) |
| 1019 | close(fd); |
| 1020 | return; |
| 1021 | } |
| 1022 | |
| 1023 | ha_alert("Cannot open /dev/null\n"); |
| 1024 | exit(EXIT_FAILURE); |
| 1025 | } |
| 1026 | |
| 1027 | |
Joseph Herlant | 0342090 | 2018-11-15 10:41:50 -0800 | [diff] [blame] | 1028 | /* This function checks if cfg_cfgfiles contains directories. |
| 1029 | * If it finds one, it adds all the files (and only files) it contains |
| 1030 | * in cfg_cfgfiles in place of the directory (and removes the directory). |
| 1031 | * It adds the files in lexical order. |
| 1032 | * It adds only files with .cfg extension. |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 1033 | * It doesn't add files with name starting with '.' |
| 1034 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 1035 | static void cfgfiles_expand_directories(void) |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 1036 | { |
| 1037 | struct wordlist *wl, *wlb; |
| 1038 | char *err = NULL; |
| 1039 | |
| 1040 | list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) { |
| 1041 | struct stat file_stat; |
| 1042 | struct dirent **dir_entries = NULL; |
| 1043 | int dir_entries_nb; |
| 1044 | int dir_entries_it; |
| 1045 | |
| 1046 | if (stat(wl->s, &file_stat)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1047 | ha_alert("Cannot open configuration file/directory %s : %s\n", |
| 1048 | wl->s, |
| 1049 | strerror(errno)); |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 1050 | exit(1); |
| 1051 | } |
| 1052 | |
| 1053 | if (!S_ISDIR(file_stat.st_mode)) |
| 1054 | continue; |
| 1055 | |
| 1056 | /* from this point wl->s is a directory */ |
| 1057 | |
| 1058 | dir_entries_nb = scandir(wl->s, &dir_entries, NULL, alphasort); |
| 1059 | if (dir_entries_nb < 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1060 | ha_alert("Cannot open configuration directory %s : %s\n", |
| 1061 | wl->s, |
| 1062 | strerror(errno)); |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 1063 | exit(1); |
| 1064 | } |
| 1065 | |
| 1066 | /* for each element in the directory wl->s */ |
| 1067 | for (dir_entries_it = 0; dir_entries_it < dir_entries_nb; dir_entries_it++) { |
| 1068 | struct dirent *dir_entry = dir_entries[dir_entries_it]; |
| 1069 | char *filename = NULL; |
| 1070 | char *d_name_cfgext = strstr(dir_entry->d_name, ".cfg"); |
| 1071 | |
| 1072 | /* don't add filename that begin with . |
Joseph Herlant | 0342090 | 2018-11-15 10:41:50 -0800 | [diff] [blame] | 1073 | * only add filename with .cfg extension |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 1074 | */ |
| 1075 | if (dir_entry->d_name[0] == '.' || |
| 1076 | !(d_name_cfgext && d_name_cfgext[4] == '\0')) |
| 1077 | goto next_dir_entry; |
| 1078 | |
| 1079 | if (!memprintf(&filename, "%s/%s", wl->s, dir_entry->d_name)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1080 | ha_alert("Cannot load configuration files %s : out of memory.\n", |
| 1081 | filename); |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 1082 | exit(1); |
| 1083 | } |
| 1084 | |
| 1085 | if (stat(filename, &file_stat)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1086 | ha_alert("Cannot open configuration file %s : %s\n", |
| 1087 | wl->s, |
| 1088 | strerror(errno)); |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 1089 | exit(1); |
| 1090 | } |
| 1091 | |
| 1092 | /* don't add anything else than regular file in cfg_cfgfiles |
| 1093 | * this way we avoid loops |
| 1094 | */ |
| 1095 | if (!S_ISREG(file_stat.st_mode)) |
| 1096 | goto next_dir_entry; |
| 1097 | |
| 1098 | if (!list_append_word(&wl->list, filename, &err)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1099 | ha_alert("Cannot load configuration files %s : %s\n", |
| 1100 | filename, |
| 1101 | err); |
Maxime de Roucy | 379d9c7 | 2016-05-13 23:52:56 +0200 | [diff] [blame] | 1102 | exit(1); |
| 1103 | } |
| 1104 | |
| 1105 | next_dir_entry: |
| 1106 | free(filename); |
| 1107 | free(dir_entry); |
| 1108 | } |
| 1109 | |
| 1110 | free(dir_entries); |
| 1111 | |
| 1112 | /* remove the current directory (wl) from cfg_cfgfiles */ |
| 1113 | free(wl->s); |
| 1114 | LIST_DEL(&wl->list); |
| 1115 | free(wl); |
| 1116 | } |
| 1117 | |
| 1118 | free(err); |
| 1119 | } |
| 1120 | |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1121 | static int get_old_sockets(const char *unixsocket) |
| 1122 | { |
| 1123 | char *cmsgbuf = NULL, *tmpbuf = NULL; |
| 1124 | int *tmpfd = NULL; |
| 1125 | struct sockaddr_un addr; |
| 1126 | struct cmsghdr *cmsg; |
| 1127 | struct msghdr msghdr; |
| 1128 | struct iovec iov; |
| 1129 | struct xfer_sock_list *xfer_sock = NULL; |
Olivier Houchard | 5474087 | 2017-04-06 14:45:14 +0200 | [diff] [blame] | 1130 | struct timeval tv = { .tv_sec = 1, .tv_usec = 0 }; |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1131 | int sock = -1; |
| 1132 | int ret = -1; |
| 1133 | int ret2 = -1; |
| 1134 | int fd_nb; |
| 1135 | int got_fd = 0; |
| 1136 | int i = 0; |
| 1137 | size_t maxoff = 0, curoff = 0; |
| 1138 | |
| 1139 | memset(&msghdr, 0, sizeof(msghdr)); |
| 1140 | cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD); |
| 1141 | if (!cmsgbuf) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1142 | ha_warning("Failed to allocate memory to send sockets\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1143 | goto out; |
| 1144 | } |
| 1145 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 1146 | if (sock < 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1147 | ha_warning("Failed to connect to the old process socket '%s'\n", |
| 1148 | unixsocket); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1149 | goto out; |
| 1150 | } |
Willy Tarreau | 719e07c | 2019-12-11 16:29:10 +0100 | [diff] [blame] | 1151 | strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path) - 1); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1152 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
| 1153 | addr.sun_family = PF_UNIX; |
| 1154 | ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr)); |
| 1155 | if (ret < 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1156 | ha_warning("Failed to connect to the old process socket '%s'\n", |
| 1157 | unixsocket); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1158 | goto out; |
| 1159 | } |
Olivier Houchard | 5474087 | 2017-04-06 14:45:14 +0200 | [diff] [blame] | 1160 | setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv)); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1161 | iov.iov_base = &fd_nb; |
| 1162 | iov.iov_len = sizeof(fd_nb); |
| 1163 | msghdr.msg_iov = &iov; |
| 1164 | msghdr.msg_iovlen = 1; |
| 1165 | send(sock, "_getsocks\n", strlen("_getsocks\n"), 0); |
| 1166 | /* First, get the number of file descriptors to be received */ |
| 1167 | if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1168 | ha_warning("Failed to get the number of sockets to be transferred !\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1169 | goto out; |
| 1170 | } |
| 1171 | if (fd_nb == 0) { |
| 1172 | ret = 0; |
| 1173 | goto out; |
| 1174 | } |
Olivier Houchard | f143b80 | 2017-11-04 15:13:01 +0100 | [diff] [blame] | 1175 | tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int))); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1176 | if (tmpbuf == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1177 | ha_warning("Failed to allocate memory while receiving sockets\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1178 | goto out; |
| 1179 | } |
| 1180 | tmpfd = malloc(fd_nb * sizeof(int)); |
| 1181 | if (tmpfd == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1182 | ha_warning("Failed to allocate memory while receiving sockets\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1183 | goto out; |
| 1184 | } |
| 1185 | msghdr.msg_control = cmsgbuf; |
| 1186 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD; |
Olivier Houchard | f143b80 | 2017-11-04 15:13:01 +0100 | [diff] [blame] | 1187 | iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1188 | do { |
| 1189 | int ret3; |
| 1190 | |
| 1191 | iov.iov_base = tmpbuf + curoff; |
| 1192 | ret = recvmsg(sock, &msghdr, 0); |
| 1193 | if (ret == -1 && errno == EINTR) |
| 1194 | continue; |
| 1195 | if (ret <= 0) |
| 1196 | break; |
| 1197 | /* Send an ack to let the sender know we got the sockets |
| 1198 | * and it can send some more |
| 1199 | */ |
| 1200 | do { |
| 1201 | ret3 = send(sock, &got_fd, sizeof(got_fd), 0); |
| 1202 | } while (ret3 == -1 && errno == EINTR); |
| 1203 | for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL; |
| 1204 | cmsg = CMSG_NXTHDR(&msghdr, cmsg)) { |
| 1205 | if (cmsg->cmsg_level == SOL_SOCKET && |
| 1206 | cmsg->cmsg_type == SCM_RIGHTS) { |
| 1207 | size_t totlen = cmsg->cmsg_len - |
| 1208 | CMSG_LEN(0); |
| 1209 | if (totlen / sizeof(int) + got_fd > fd_nb) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1210 | ha_warning("Got to many sockets !\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1211 | goto out; |
| 1212 | } |
| 1213 | /* |
| 1214 | * Be paranoid and use memcpy() to avoid any |
| 1215 | * potential alignement issue. |
| 1216 | */ |
| 1217 | memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen); |
| 1218 | got_fd += totlen / sizeof(int); |
| 1219 | } |
| 1220 | } |
| 1221 | curoff += ret; |
| 1222 | } while (got_fd < fd_nb); |
| 1223 | |
| 1224 | if (got_fd != fd_nb) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1225 | ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n", |
| 1226 | fd_nb, got_fd); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1227 | goto out; |
| 1228 | } |
| 1229 | maxoff = curoff; |
| 1230 | curoff = 0; |
| 1231 | for (i = 0; i < got_fd; i++) { |
| 1232 | int fd = tmpfd[i]; |
| 1233 | socklen_t socklen; |
| 1234 | int len; |
| 1235 | |
| 1236 | xfer_sock = calloc(1, sizeof(*xfer_sock)); |
| 1237 | if (!xfer_sock) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1238 | ha_warning("Failed to allocate memory in get_old_sockets() !\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1239 | break; |
| 1240 | } |
| 1241 | xfer_sock->fd = -1; |
| 1242 | |
| 1243 | socklen = sizeof(xfer_sock->addr); |
| 1244 | if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1245 | ha_warning("Failed to get socket address\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1246 | free(xfer_sock); |
Olivier Houchard | be7b1ce | 2017-07-17 17:25:33 +0200 | [diff] [blame] | 1247 | xfer_sock = NULL; |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1248 | continue; |
| 1249 | } |
| 1250 | if (curoff >= maxoff) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1251 | ha_warning("Inconsistency while transferring sockets\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1252 | goto out; |
| 1253 | } |
| 1254 | len = tmpbuf[curoff++]; |
| 1255 | if (len > 0) { |
| 1256 | /* We have a namespace */ |
| 1257 | if (curoff + len > maxoff) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1258 | ha_warning("Inconsistency while transferring sockets\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1259 | goto out; |
| 1260 | } |
| 1261 | xfer_sock->namespace = malloc(len + 1); |
| 1262 | if (!xfer_sock->namespace) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1263 | ha_warning("Failed to allocate memory while transferring sockets\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1264 | goto out; |
| 1265 | } |
| 1266 | memcpy(xfer_sock->namespace, &tmpbuf[curoff], len); |
| 1267 | xfer_sock->namespace[len] = 0; |
| 1268 | curoff += len; |
| 1269 | } |
| 1270 | if (curoff >= maxoff) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1271 | ha_warning("Inconsistency while transferring sockets\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1272 | goto out; |
| 1273 | } |
| 1274 | len = tmpbuf[curoff++]; |
| 1275 | if (len > 0) { |
| 1276 | /* We have an interface */ |
| 1277 | if (curoff + len > maxoff) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1278 | ha_warning("Inconsistency while transferring sockets\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1279 | goto out; |
| 1280 | } |
| 1281 | xfer_sock->iface = malloc(len + 1); |
| 1282 | if (!xfer_sock->iface) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1283 | ha_warning("Failed to allocate memory while transferring sockets\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1284 | goto out; |
| 1285 | } |
| 1286 | memcpy(xfer_sock->iface, &tmpbuf[curoff], len); |
Olivier Houchard | 33e083c | 2018-03-15 17:48:49 +0100 | [diff] [blame] | 1287 | xfer_sock->iface[len] = 0; |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1288 | curoff += len; |
| 1289 | } |
| 1290 | if (curoff + sizeof(int) > maxoff) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1291 | ha_warning("Inconsistency while transferring sockets\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1292 | goto out; |
| 1293 | } |
| 1294 | memcpy(&xfer_sock->options, &tmpbuf[curoff], |
| 1295 | sizeof(xfer_sock->options)); |
| 1296 | curoff += sizeof(xfer_sock->options); |
| 1297 | |
| 1298 | xfer_sock->fd = fd; |
| 1299 | if (xfer_sock_list) |
| 1300 | xfer_sock_list->prev = xfer_sock; |
| 1301 | xfer_sock->next = xfer_sock_list; |
| 1302 | xfer_sock->prev = NULL; |
| 1303 | xfer_sock_list = xfer_sock; |
| 1304 | xfer_sock = NULL; |
| 1305 | } |
| 1306 | |
| 1307 | ret2 = 0; |
| 1308 | out: |
| 1309 | /* If we failed midway make sure to close the remaining |
| 1310 | * file descriptors |
| 1311 | */ |
| 1312 | if (tmpfd != NULL && i < got_fd) { |
| 1313 | for (; i < got_fd; i++) { |
| 1314 | close(tmpfd[i]); |
| 1315 | } |
| 1316 | } |
| 1317 | free(tmpbuf); |
| 1318 | free(tmpfd); |
| 1319 | free(cmsgbuf); |
| 1320 | if (sock != -1) |
| 1321 | close(sock); |
| 1322 | if (xfer_sock) { |
| 1323 | free(xfer_sock->namespace); |
| 1324 | free(xfer_sock->iface); |
| 1325 | if (xfer_sock->fd != -1) |
| 1326 | close(xfer_sock->fd); |
| 1327 | free(xfer_sock); |
| 1328 | } |
| 1329 | return (ret2); |
| 1330 | } |
| 1331 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1332 | /* |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1333 | * copy and cleanup the current argv |
| 1334 | * Remove the -sf /-st parameters |
| 1335 | * Return an allocated copy of argv |
| 1336 | */ |
| 1337 | |
| 1338 | static char **copy_argv(int argc, char **argv) |
| 1339 | { |
| 1340 | char **newargv; |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 1341 | int i = 0, j = 0; |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1342 | |
| 1343 | newargv = calloc(argc + 2, sizeof(char *)); |
| 1344 | if (newargv == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1345 | ha_warning("Cannot allocate memory\n"); |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1346 | return NULL; |
| 1347 | } |
| 1348 | |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 1349 | while (i < argc) { |
| 1350 | /* -sf or -st or -x */ |
William Lallemand | 29f690c | 2018-01-09 23:12:27 +0100 | [diff] [blame] | 1351 | if (i > 0 && argv[i][0] == '-' && |
| 1352 | ((argv[i][1] == 's' && (argv[i][2] == 'f' || argv[i][2] == 't')) || argv[i][1] == 'x' )) { |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 1353 | /* 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] | 1354 | i++; |
| 1355 | while (i < argc && argv[i][0] != '-') { |
| 1356 | i++; |
| 1357 | } |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 1358 | continue; |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1359 | } |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 1360 | |
| 1361 | newargv[j++] = argv[i++]; |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1362 | } |
William Lallemand | 2bf6d62 | 2017-06-20 11:20:23 +0200 | [diff] [blame] | 1363 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1364 | return newargv; |
| 1365 | } |
| 1366 | |
Willy Tarreau | 6c3a681 | 2020-03-06 18:57:15 +0100 | [diff] [blame] | 1367 | |
| 1368 | /* Performs basic random seed initialization. The main issue with this is that |
| 1369 | * srandom_r() only takes 32 bits and purposely provides a reproducible sequence, |
| 1370 | * which means that there will only be 4 billion possible random sequences once |
| 1371 | * srandom() is called, regardless of the internal state. Not calling it is |
| 1372 | * even worse as we'll always produce the same randoms sequences. What we do |
| 1373 | * here is to create an initial sequence from various entropy sources, hash it |
| 1374 | * using SHA1 and keep the resulting 160 bits available globally. |
| 1375 | * |
| 1376 | * We initialize the current process with the first 32 bits before starting the |
| 1377 | * polling loop, where all this will be changed to have process specific and |
| 1378 | * thread specific sequences. |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 1379 | * |
| 1380 | * Before starting threads, it's still possible to call random() as srandom() |
| 1381 | * is initialized from this, but after threads and/or processes are started, |
| 1382 | * only ha_random() is expected to be used to guarantee distinct sequences. |
Willy Tarreau | 6c3a681 | 2020-03-06 18:57:15 +0100 | [diff] [blame] | 1383 | */ |
| 1384 | static void ha_random_boot(char *const *argv) |
| 1385 | { |
| 1386 | unsigned char message[256]; |
| 1387 | unsigned char *m = message; |
| 1388 | struct timeval tv; |
| 1389 | blk_SHA_CTX ctx; |
| 1390 | unsigned long l; |
| 1391 | int fd; |
| 1392 | int i; |
| 1393 | |
| 1394 | /* start with current time as pseudo-random seed */ |
| 1395 | gettimeofday(&tv, NULL); |
| 1396 | write_u32(m, tv.tv_sec); m += 4; |
| 1397 | write_u32(m, tv.tv_usec); m += 4; |
| 1398 | |
| 1399 | /* PID and PPID add some OS-based randomness */ |
| 1400 | write_u16(m, getpid()); m += 2; |
| 1401 | write_u16(m, getppid()); m += 2; |
| 1402 | |
| 1403 | /* take up to 160 bits bytes from /dev/urandom if available (non-blocking) */ |
| 1404 | fd = open("/dev/urandom", O_RDONLY); |
| 1405 | if (fd >= 0) { |
| 1406 | i = read(fd, m, 20); |
| 1407 | if (i > 0) |
| 1408 | m += i; |
| 1409 | close(fd); |
| 1410 | } |
| 1411 | |
| 1412 | /* take up to 160 bits bytes from openssl (non-blocking) */ |
| 1413 | #ifdef USE_OPENSSL |
| 1414 | if (RAND_bytes(m, 20) == 1) |
| 1415 | m += 20; |
| 1416 | #endif |
| 1417 | |
| 1418 | /* take 160 bits from existing random in case it was already initialized */ |
| 1419 | for (i = 0; i < 5; i++) { |
| 1420 | write_u32(m, random()); |
| 1421 | m += 4; |
| 1422 | } |
| 1423 | |
| 1424 | /* stack address (benefit form operating system's ASLR) */ |
| 1425 | l = (unsigned long)&m; |
| 1426 | memcpy(m, &l, sizeof(l)); m += sizeof(l); |
| 1427 | |
| 1428 | /* argv address (benefit form operating system's ASLR) */ |
| 1429 | l = (unsigned long)&argv; |
| 1430 | memcpy(m, &l, sizeof(l)); m += sizeof(l); |
| 1431 | |
| 1432 | /* use tv_usec again after all the operations above */ |
| 1433 | gettimeofday(&tv, NULL); |
| 1434 | write_u32(m, tv.tv_usec); m += 4; |
| 1435 | |
| 1436 | /* |
| 1437 | * At this point, ~84-92 bytes have been used |
| 1438 | */ |
| 1439 | |
| 1440 | /* finish with the hostname */ |
| 1441 | strncpy((char *)m, hostname, message + sizeof(message) - m); |
| 1442 | m += strlen(hostname); |
| 1443 | |
| 1444 | /* total message length */ |
| 1445 | l = m - message; |
| 1446 | |
| 1447 | memset(&ctx, 0, sizeof(ctx)); |
| 1448 | blk_SHA1_Init(&ctx); |
| 1449 | blk_SHA1_Update(&ctx, message, l); |
| 1450 | blk_SHA1_Final(boot_seed, &ctx); |
| 1451 | |
| 1452 | srandom(read_u32(boot_seed)); |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 1453 | ha_random_seed(boot_seed, sizeof(boot_seed)); |
Willy Tarreau | 6c3a681 | 2020-03-06 18:57:15 +0100 | [diff] [blame] | 1454 | } |
| 1455 | |
Willy Tarreau | 5a023f0 | 2019-03-01 14:19:31 +0100 | [diff] [blame] | 1456 | /* considers splicing proxies' maxconn, computes the ideal global.maxpipes |
| 1457 | * setting, and returns it. It may return -1 meaning "unlimited" if some |
| 1458 | * unlimited proxies have been found and the global.maxconn value is not yet |
| 1459 | * set. It may also return a value greater than maxconn if it's not yet set. |
| 1460 | * Note that a value of zero means there is no need for pipes. -1 is never |
| 1461 | * returned if global.maxconn is valid. |
| 1462 | */ |
| 1463 | static int compute_ideal_maxpipes() |
| 1464 | { |
| 1465 | struct proxy *cur; |
| 1466 | int nbfe = 0, nbbe = 0; |
| 1467 | int unlimited = 0; |
| 1468 | int pipes; |
| 1469 | int max; |
| 1470 | |
| 1471 | for (cur = proxies_list; cur; cur = cur->next) { |
| 1472 | if (cur->options2 & (PR_O2_SPLIC_ANY)) { |
| 1473 | if (cur->cap & PR_CAP_FE) { |
| 1474 | max = cur->maxconn; |
| 1475 | nbfe += max; |
| 1476 | if (!max) { |
| 1477 | unlimited = 1; |
| 1478 | break; |
| 1479 | } |
| 1480 | } |
| 1481 | if (cur->cap & PR_CAP_BE) { |
| 1482 | max = cur->fullconn ? cur->fullconn : global.maxconn; |
| 1483 | nbbe += max; |
| 1484 | if (!max) { |
| 1485 | unlimited = 1; |
| 1486 | break; |
| 1487 | } |
| 1488 | } |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | pipes = MAX(nbfe, nbbe); |
| 1493 | if (global.maxconn) { |
| 1494 | if (pipes > global.maxconn || unlimited) |
| 1495 | pipes = global.maxconn; |
| 1496 | } else if (unlimited) { |
| 1497 | pipes = -1; |
| 1498 | } |
| 1499 | |
| 1500 | return pipes >= 4 ? pipes / 4 : pipes; |
| 1501 | } |
| 1502 | |
Willy Tarreau | ac35093 | 2019-03-01 15:43:14 +0100 | [diff] [blame] | 1503 | /* considers global.maxsocks, global.maxpipes, async engines, SSL frontends and |
| 1504 | * rlimits and computes an ideal maxconn. It's meant to be called only when |
| 1505 | * maxsock contains the sum of listening FDs, before it is updated based on |
Willy Tarreau | df23c0c | 2019-03-13 10:10:49 +0100 | [diff] [blame] | 1506 | * maxconn and pipes. If there are not enough FDs left, DEFAULT_MAXCONN (by |
| 1507 | * default 100) is returned as it is expected that it will even run on tight |
| 1508 | * environments, and will maintain compatibility with previous packages that |
| 1509 | * used to rely on this value as the default one. The system will emit a |
| 1510 | * warning indicating how many FDs are missing anyway if needed. |
Willy Tarreau | ac35093 | 2019-03-01 15:43:14 +0100 | [diff] [blame] | 1511 | */ |
| 1512 | static int compute_ideal_maxconn() |
| 1513 | { |
| 1514 | int ssl_sides = !!global.ssl_used_frontend + !!global.ssl_used_backend; |
| 1515 | int engine_fds = global.ssl_used_async_engines * ssl_sides; |
| 1516 | int pipes = compute_ideal_maxpipes(); |
Willy Tarreau | b1beaa3 | 2020-03-06 10:25:31 +0100 | [diff] [blame] | 1517 | int remain = MAX(rlim_fd_cur_at_boot, rlim_fd_max_at_boot); |
Willy Tarreau | ac35093 | 2019-03-01 15:43:14 +0100 | [diff] [blame] | 1518 | int maxconn; |
| 1519 | |
| 1520 | /* we have to take into account these elements : |
| 1521 | * - number of engine_fds, which inflates the number of FD needed per |
| 1522 | * connection by this number. |
| 1523 | * - number of pipes per connection on average : for the unlimited |
| 1524 | * case, this is 0.5 pipe FDs per connection, otherwise it's a |
| 1525 | * fixed value of 2*pipes. |
| 1526 | * - two FDs per connection |
| 1527 | */ |
| 1528 | |
| 1529 | /* subtract listeners and checks */ |
| 1530 | remain -= global.maxsock; |
| 1531 | |
Willy Tarreau | 3f20085 | 2019-03-14 19:13:17 +0100 | [diff] [blame] | 1532 | /* one epoll_fd/kqueue_fd per thread */ |
| 1533 | remain -= global.nbthread; |
| 1534 | |
| 1535 | /* one wake-up pipe (2 fd) per thread */ |
| 1536 | remain -= 2 * global.nbthread; |
| 1537 | |
Willy Tarreau | ac35093 | 2019-03-01 15:43:14 +0100 | [diff] [blame] | 1538 | /* Fixed pipes values : we only subtract them if they're not larger |
| 1539 | * than the remaining FDs because pipes are optional. |
| 1540 | */ |
| 1541 | if (pipes >= 0 && pipes * 2 < remain) |
| 1542 | remain -= pipes * 2; |
| 1543 | |
| 1544 | if (pipes < 0) { |
| 1545 | /* maxsock = maxconn * 2 + maxconn/4 * 2 + maxconn * engine_fds. |
| 1546 | * = maxconn * (2 + 0.5 + engine_fds) |
| 1547 | * = maxconn * (4 + 1 + 2*engine_fds) / 2 |
| 1548 | */ |
| 1549 | maxconn = 2 * remain / (5 + 2 * engine_fds); |
| 1550 | } else { |
| 1551 | /* maxsock = maxconn * 2 + maxconn * engine_fds. |
| 1552 | * = maxconn * (2 + engine_fds) |
| 1553 | */ |
| 1554 | maxconn = remain / (2 + engine_fds); |
| 1555 | } |
| 1556 | |
Willy Tarreau | df23c0c | 2019-03-13 10:10:49 +0100 | [diff] [blame] | 1557 | return MAX(maxconn, DEFAULT_MAXCONN); |
Willy Tarreau | ac35093 | 2019-03-01 15:43:14 +0100 | [diff] [blame] | 1558 | } |
| 1559 | |
Willy Tarreau | a409f30 | 2020-03-10 17:08:53 +0100 | [diff] [blame] | 1560 | /* computes the estimated maxsock value for the given maxconn based on the |
| 1561 | * possibly set global.maxpipes and existing partial global.maxsock. It may |
| 1562 | * temporarily change global.maxconn for the time needed to propagate the |
| 1563 | * computations, and will reset it. |
| 1564 | */ |
| 1565 | static int compute_ideal_maxsock(int maxconn) |
| 1566 | { |
| 1567 | int maxpipes = global.maxpipes; |
| 1568 | int maxsock = global.maxsock; |
| 1569 | |
| 1570 | |
| 1571 | if (!maxpipes) { |
| 1572 | int old_maxconn = global.maxconn; |
| 1573 | |
| 1574 | global.maxconn = maxconn; |
| 1575 | maxpipes = compute_ideal_maxpipes(); |
| 1576 | global.maxconn = old_maxconn; |
| 1577 | } |
| 1578 | |
| 1579 | maxsock += maxconn * 2; /* each connection needs two sockets */ |
| 1580 | maxsock += maxpipes * 2; /* each pipe needs two FDs */ |
| 1581 | maxsock += global.nbthread; /* one epoll_fd/kqueue_fd per thread */ |
| 1582 | maxsock += 2 * global.nbthread; /* one wake-up pipe (2 fd) per thread */ |
| 1583 | |
| 1584 | /* compute fd used by async engines */ |
| 1585 | if (global.ssl_used_async_engines) { |
| 1586 | int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend; |
| 1587 | |
| 1588 | maxsock += maxconn * sides * global.ssl_used_async_engines; |
| 1589 | } |
| 1590 | return maxsock; |
| 1591 | } |
| 1592 | |
Willy Tarreau | 304e17e | 2020-03-10 17:54:54 +0100 | [diff] [blame] | 1593 | /* Tests if it is possible to set the current process' RLIMIT_NOFILE to |
| 1594 | * <maxsock>, then sets it back to the previous value. Returns non-zero if the |
| 1595 | * value is accepted, non-zero otherwise. This is used to determine if an |
| 1596 | * automatic limit may be applied or not. When it is not, the caller knows that |
| 1597 | * the highest we can do is the rlim_max at boot. In case of error, we return |
| 1598 | * that the setting is possible, so that we defer the error processing to the |
| 1599 | * final stage in charge of enforcing this. |
| 1600 | */ |
| 1601 | static int check_if_maxsock_permitted(int maxsock) |
| 1602 | { |
| 1603 | struct rlimit orig_limit, test_limit; |
| 1604 | int ret; |
| 1605 | |
| 1606 | if (getrlimit(RLIMIT_NOFILE, &orig_limit) != 0) |
| 1607 | return 1; |
| 1608 | |
| 1609 | /* don't go further if we can't even set to what we have */ |
| 1610 | if (setrlimit(RLIMIT_NOFILE, &orig_limit) != 0) |
| 1611 | return 1; |
| 1612 | |
| 1613 | test_limit.rlim_max = MAX(maxsock, orig_limit.rlim_max); |
| 1614 | test_limit.rlim_cur = test_limit.rlim_max; |
| 1615 | ret = setrlimit(RLIMIT_NOFILE, &test_limit); |
| 1616 | |
| 1617 | if (setrlimit(RLIMIT_NOFILE, &orig_limit) != 0) |
| 1618 | return 1; |
| 1619 | |
| 1620 | return ret == 0; |
| 1621 | } |
| 1622 | |
| 1623 | |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1624 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1625 | * This function initializes all the necessary variables. It only returns |
| 1626 | * if everything is OK. If something fails, it exits. |
| 1627 | */ |
Willy Tarreau | 1b5af7c | 2016-12-21 18:19:57 +0100 | [diff] [blame] | 1628 | static void init(int argc, char **argv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1629 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1630 | int arg_mode = 0; /* MODE_DEBUG, ... */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1631 | char *tmp; |
| 1632 | char *cfg_pidfile = NULL; |
Willy Tarreau | 058e907 | 2009-07-20 09:30:05 +0200 | [diff] [blame] | 1633 | int err_code = 0; |
Maxime de Roucy | 0f50392 | 2016-05-13 23:52:55 +0200 | [diff] [blame] | 1634 | char *err_msg = NULL; |
Willy Tarreau | 477ecd8 | 2010-01-03 21:12:30 +0100 | [diff] [blame] | 1635 | struct wordlist *wl; |
Kevinm | 48936af | 2010-12-22 16:08:21 +0000 | [diff] [blame] | 1636 | char *progname; |
Willy Tarreau | 576132e | 2011-09-10 19:26:56 +0200 | [diff] [blame] | 1637 | char *change_dir = NULL; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1638 | struct proxy *px; |
Willy Tarreau | e694573 | 2016-12-21 19:57:00 +0100 | [diff] [blame] | 1639 | struct post_check_fct *pcf; |
Willy Tarreau | ac35093 | 2019-03-01 15:43:14 +0100 | [diff] [blame] | 1640 | int ideal_maxconn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1641 | |
Christopher Faulet | e3a5e35 | 2017-10-24 13:53:54 +0200 | [diff] [blame] | 1642 | global.mode = MODE_STARTING; |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 1643 | next_argv = copy_argv(argc, argv); |
| 1644 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 1645 | if (!init_trash_buffers(1)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1646 | ha_alert("failed to initialize trash buffers.\n"); |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 1647 | exit(1); |
| 1648 | } |
David du Colombier | 7af4605 | 2012-05-16 14:16:48 +0200 | [diff] [blame] | 1649 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1650 | /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate |
| 1651 | * the string in case of truncation, and at least FreeBSD appears not to do |
| 1652 | * it. |
| 1653 | */ |
| 1654 | memset(hostname, 0, sizeof(hostname)); |
| 1655 | gethostname(hostname, sizeof(hostname) - 1); |
| 1656 | memset(localpeer, 0, sizeof(localpeer)); |
| 1657 | memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1); |
William Lallemand | daf4cd2 | 2018-04-17 16:46:13 +0200 | [diff] [blame] | 1658 | setenv("HAPROXY_LOCALPEER", localpeer, 1); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1659 | |
William Lallemand | 24c928c | 2020-01-14 17:58:18 +0100 | [diff] [blame] | 1660 | /* we were in mworker mode, we should restart in mworker mode */ |
| 1661 | if (getenv("HAPROXY_MWORKER_REEXEC") != NULL) |
| 1662 | global.mode |= MODE_MWORKER; |
| 1663 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1664 | /* |
| 1665 | * Initialize the previously static variables. |
| 1666 | */ |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 1667 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 1668 | totalconn = actconn = listeners = stopping = 0; |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 1669 | killed = 0; |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 1670 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1671 | |
| 1672 | #ifdef HAPROXY_MEMMAX |
Willy Tarreau | 7006045 | 2015-12-14 12:46:07 +0100 | [diff] [blame] | 1673 | global.rlimit_memmax_all = HAPROXY_MEMMAX; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1674 | #endif |
| 1675 | |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 1676 | tzset(); |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 1677 | tv_update_date(-1,-1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1678 | start_date = now; |
| 1679 | |
Willy Tarreau | 6c3a681 | 2020-03-06 18:57:15 +0100 | [diff] [blame] | 1680 | ha_random_boot(argv); |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 1681 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 1682 | if (init_acl() != 0) |
| 1683 | exit(1); |
Willy Tarreau | b6b3df3 | 2018-11-26 16:31:20 +0100 | [diff] [blame] | 1684 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 1685 | /* Initialise lua. */ |
| 1686 | hlua_init(); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 1687 | |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 1688 | /* Initialize process vars */ |
| 1689 | vars_init(&global.vars, SCOPE_PROC); |
| 1690 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1691 | global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */ |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1692 | #if defined(USE_POLL) |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1693 | global.tune.options |= GTUNE_USE_POLL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1694 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1695 | #if defined(USE_EPOLL) |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1696 | global.tune.options |= GTUNE_USE_EPOLL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1697 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1698 | #if defined(USE_KQUEUE) |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1699 | global.tune.options |= GTUNE_USE_KQUEUE; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 1700 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1701 | #if defined(USE_EVPORTS) |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 1702 | global.tune.options |= GTUNE_USE_EVPORTS; |
| 1703 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1704 | #if defined(USE_LINUX_SPLICE) |
Willy Tarreau | 3ab68cf | 2009-01-25 16:03:28 +0100 | [diff] [blame] | 1705 | global.tune.options |= GTUNE_USE_SPLICE; |
| 1706 | #endif |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 1707 | #if defined(USE_GETADDRINFO) |
| 1708 | global.tune.options |= GTUNE_USE_GAI; |
| 1709 | #endif |
Lukas Tribus | a0bcbdc | 2016-09-12 21:42:20 +0000 | [diff] [blame] | 1710 | #if defined(SO_REUSEPORT) |
| 1711 | global.tune.options |= GTUNE_USE_REUSEPORT; |
| 1712 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1713 | |
| 1714 | pid = getpid(); |
| 1715 | progname = *argv; |
| 1716 | while ((tmp = strchr(progname, '/')) != NULL) |
| 1717 | progname = tmp + 1; |
| 1718 | |
Kevinm | 48936af | 2010-12-22 16:08:21 +0000 | [diff] [blame] | 1719 | /* the process name is used for the logs only */ |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1720 | chunk_initstr(&global.log_tag, strdup(progname)); |
Kevinm | 48936af | 2010-12-22 16:08:21 +0000 | [diff] [blame] | 1721 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1722 | argc--; argv++; |
| 1723 | while (argc > 0) { |
| 1724 | char *flag; |
| 1725 | |
| 1726 | if (**argv == '-') { |
| 1727 | flag = *argv+1; |
| 1728 | |
| 1729 | /* 1 arg */ |
| 1730 | if (*flag == 'v') { |
| 1731 | display_version(); |
Willy Tarreau | 7b066db | 2007-12-02 11:28:59 +0100 | [diff] [blame] | 1732 | if (flag[1] == 'v') /* -vv */ |
| 1733 | display_build_opts(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1734 | exit(0); |
| 1735 | } |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1736 | #if defined(USE_EPOLL) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1737 | else if (*flag == 'd' && flag[1] == 'e') |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1738 | global.tune.options &= ~GTUNE_USE_EPOLL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1739 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1740 | #if defined(USE_POLL) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1741 | else if (*flag == 'd' && flag[1] == 'p') |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1742 | global.tune.options &= ~GTUNE_USE_POLL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1743 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1744 | #if defined(USE_KQUEUE) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 1745 | else if (*flag == 'd' && flag[1] == 'k') |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 1746 | global.tune.options &= ~GTUNE_USE_KQUEUE; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 1747 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1748 | #if defined(USE_EVPORTS) |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 1749 | else if (*flag == 'd' && flag[1] == 'v') |
| 1750 | global.tune.options &= ~GTUNE_USE_EVPORTS; |
| 1751 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1752 | #if defined(USE_LINUX_SPLICE) |
Willy Tarreau | 3ab68cf | 2009-01-25 16:03:28 +0100 | [diff] [blame] | 1753 | else if (*flag == 'd' && flag[1] == 'S') |
| 1754 | global.tune.options &= ~GTUNE_USE_SPLICE; |
| 1755 | #endif |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 1756 | #if defined(USE_GETADDRINFO) |
| 1757 | else if (*flag == 'd' && flag[1] == 'G') |
| 1758 | global.tune.options &= ~GTUNE_USE_GAI; |
| 1759 | #endif |
Lukas Tribus | a0bcbdc | 2016-09-12 21:42:20 +0000 | [diff] [blame] | 1760 | #if defined(SO_REUSEPORT) |
| 1761 | else if (*flag == 'd' && flag[1] == 'R') |
| 1762 | global.tune.options &= ~GTUNE_USE_REUSEPORT; |
| 1763 | #endif |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1764 | else if (*flag == 'd' && flag[1] == 'V') |
| 1765 | global.ssl_server_verify = SSL_SERVER_VERIFY_NONE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1766 | else if (*flag == 'V') |
| 1767 | arg_mode |= MODE_VERBOSE; |
| 1768 | else if (*flag == 'd' && flag[1] == 'b') |
| 1769 | arg_mode |= MODE_FOREGROUND; |
Willy Tarreau | 6e06443 | 2012-05-08 15:40:42 +0200 | [diff] [blame] | 1770 | else if (*flag == 'd' && flag[1] == 'M') |
| 1771 | mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P'; |
Willy Tarreau | 3eed10e | 2016-11-07 21:03:16 +0100 | [diff] [blame] | 1772 | else if (*flag == 'd' && flag[1] == 'r') |
| 1773 | global.tune.options |= GTUNE_RESOLVE_DONTFAIL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1774 | else if (*flag == 'd') |
| 1775 | arg_mode |= MODE_DEBUG; |
| 1776 | else if (*flag == 'c') |
| 1777 | arg_mode |= MODE_CHECK; |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 1778 | else if (*flag == 'D') |
Willy Tarreau | 6bde87b | 2009-05-18 16:29:51 +0200 | [diff] [blame] | 1779 | arg_mode |= MODE_DAEMON; |
Tim Duesterhus | d6942c8 | 2017-11-20 15:58:35 +0100 | [diff] [blame] | 1780 | else if (*flag == 'W' && flag[1] == 's') { |
Lukas Tribus | f46bf95 | 2017-11-21 12:39:34 +0100 | [diff] [blame] | 1781 | arg_mode |= MODE_MWORKER | MODE_FOREGROUND; |
Tim Duesterhus | d6942c8 | 2017-11-20 15:58:35 +0100 | [diff] [blame] | 1782 | #if defined(USE_SYSTEMD) |
| 1783 | global.tune.options |= GTUNE_USE_SYSTEMD; |
| 1784 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1785 | ha_alert("master-worker mode with systemd support (-Ws) requested, but not compiled. Use master-worker mode (-W) if you are not using Type=notify in your unit file or recompile with USE_SYSTEMD=1.\n\n"); |
Tim Duesterhus | d6942c8 | 2017-11-20 15:58:35 +0100 | [diff] [blame] | 1786 | usage(progname); |
| 1787 | #endif |
| 1788 | } |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 1789 | else if (*flag == 'W') |
| 1790 | arg_mode |= MODE_MWORKER; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1791 | else if (*flag == 'q') |
| 1792 | arg_mode |= MODE_QUIET; |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1793 | else if (*flag == 'x') { |
William Lallemand | 45eff44 | 2017-06-19 15:57:55 +0200 | [diff] [blame] | 1794 | if (argc <= 1 || argv[1][0] == '-') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1795 | ha_alert("Unix socket path expected with the -x flag\n\n"); |
William Lallemand | 45eff44 | 2017-06-19 15:57:55 +0200 | [diff] [blame] | 1796 | usage(progname); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1797 | } |
William Lallemand | 4fc0969 | 2017-06-19 16:37:19 +0200 | [diff] [blame] | 1798 | if (old_unixsocket) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1799 | ha_warning("-x option already set, overwriting the value\n"); |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1800 | old_unixsocket = argv[1]; |
William Lallemand | 4fc0969 | 2017-06-19 16:37:19 +0200 | [diff] [blame] | 1801 | |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 1802 | argv++; |
| 1803 | argc--; |
| 1804 | } |
William Lallemand | e736115 | 2018-10-26 14:47:36 +0200 | [diff] [blame] | 1805 | else if (*flag == 'S') { |
| 1806 | struct wordlist *c; |
| 1807 | |
| 1808 | if (argc <= 1 || argv[1][0] == '-') { |
| 1809 | ha_alert("Socket and optional bind parameters expected with the -S flag\n"); |
| 1810 | usage(progname); |
| 1811 | } |
| 1812 | if ((c = malloc(sizeof(*c))) == NULL || (c->s = strdup(argv[1])) == NULL) { |
| 1813 | ha_alert("Cannot allocate memory\n"); |
| 1814 | exit(EXIT_FAILURE); |
| 1815 | } |
| 1816 | LIST_ADD(&mworker_cli_conf, &c->list); |
| 1817 | |
| 1818 | argv++; |
| 1819 | argc--; |
| 1820 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1821 | else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) { |
| 1822 | /* list of pids to finish ('f') or terminate ('t') */ |
| 1823 | |
| 1824 | if (flag[1] == 'f') |
| 1825 | oldpids_sig = SIGUSR1; /* finish then exit */ |
| 1826 | else |
| 1827 | oldpids_sig = SIGTERM; /* terminate immediately */ |
Willy Tarreau | c6ca1aa | 2015-10-08 11:32:32 +0200 | [diff] [blame] | 1828 | while (argc > 1 && argv[1][0] != '-') { |
Chris Lane | 236062f | 2018-02-05 23:15:44 +0000 | [diff] [blame] | 1829 | char * endptr = NULL; |
Willy Tarreau | c6ca1aa | 2015-10-08 11:32:32 +0200 | [diff] [blame] | 1830 | oldpids = realloc(oldpids, (nb_oldpids + 1) * sizeof(int)); |
| 1831 | if (!oldpids) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1832 | ha_alert("Cannot allocate old pid : out of memory.\n"); |
Willy Tarreau | c6ca1aa | 2015-10-08 11:32:32 +0200 | [diff] [blame] | 1833 | exit(1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1834 | } |
Willy Tarreau | c6ca1aa | 2015-10-08 11:32:32 +0200 | [diff] [blame] | 1835 | argc--; argv++; |
Chris Lane | 236062f | 2018-02-05 23:15:44 +0000 | [diff] [blame] | 1836 | errno = 0; |
| 1837 | oldpids[nb_oldpids] = strtol(*argv, &endptr, 10); |
| 1838 | if (errno) { |
| 1839 | ha_alert("-%2s option: failed to parse {%s}: %s\n", |
| 1840 | flag, |
| 1841 | *argv, strerror(errno)); |
| 1842 | exit(1); |
| 1843 | } else if (endptr && strlen(endptr)) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1844 | while (isspace((unsigned char)*endptr)) endptr++; |
Aurélien Nephtali | 39b8988 | 2018-02-17 20:53:11 +0100 | [diff] [blame] | 1845 | if (*endptr != 0) { |
Chris Lane | 236062f | 2018-02-05 23:15:44 +0000 | [diff] [blame] | 1846 | ha_alert("-%2s option: some bytes unconsumed in PID list {%s}\n", |
| 1847 | flag, endptr); |
| 1848 | exit(1); |
Aurélien Nephtali | 39b8988 | 2018-02-17 20:53:11 +0100 | [diff] [blame] | 1849 | } |
Chris Lane | 236062f | 2018-02-05 23:15:44 +0000 | [diff] [blame] | 1850 | } |
Willy Tarreau | c6ca1aa | 2015-10-08 11:32:32 +0200 | [diff] [blame] | 1851 | if (oldpids[nb_oldpids] <= 0) |
| 1852 | usage(progname); |
| 1853 | nb_oldpids++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1854 | } |
| 1855 | } |
Willy Tarreau | a088d31 | 2015-10-08 11:58:48 +0200 | [diff] [blame] | 1856 | else if (flag[0] == '-' && flag[1] == 0) { /* "--" */ |
| 1857 | /* now that's a cfgfile list */ |
| 1858 | argv++; argc--; |
| 1859 | while (argc > 0) { |
Maxime de Roucy | 0f50392 | 2016-05-13 23:52:55 +0200 | [diff] [blame] | 1860 | if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1861 | ha_alert("Cannot load configuration file/directory %s : %s\n", |
| 1862 | *argv, |
| 1863 | err_msg); |
Willy Tarreau | a088d31 | 2015-10-08 11:58:48 +0200 | [diff] [blame] | 1864 | exit(1); |
| 1865 | } |
Willy Tarreau | a088d31 | 2015-10-08 11:58:48 +0200 | [diff] [blame] | 1866 | argv++; argc--; |
| 1867 | } |
| 1868 | break; |
| 1869 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1870 | else { /* >=2 args */ |
| 1871 | argv++; argc--; |
| 1872 | if (argc == 0) |
Willy Tarreau | 3bafcdc | 2011-09-10 19:20:23 +0200 | [diff] [blame] | 1873 | usage(progname); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1874 | |
| 1875 | switch (*flag) { |
Willy Tarreau | 576132e | 2011-09-10 19:26:56 +0200 | [diff] [blame] | 1876 | case 'C' : change_dir = *argv; break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1877 | case 'n' : cfg_maxconn = atol(*argv); break; |
Willy Tarreau | 7006045 | 2015-12-14 12:46:07 +0100 | [diff] [blame] | 1878 | case 'm' : global.rlimit_memmax_all = atol(*argv); break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1879 | case 'N' : cfg_maxpconn = atol(*argv); break; |
William Lallemand | daf4cd2 | 2018-04-17 16:46:13 +0200 | [diff] [blame] | 1880 | case 'L' : |
| 1881 | strncpy(localpeer, *argv, sizeof(localpeer) - 1); |
| 1882 | setenv("HAPROXY_LOCALPEER", localpeer, 1); |
| 1883 | break; |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 1884 | case 'f' : |
Maxime de Roucy | 0f50392 | 2016-05-13 23:52:55 +0200 | [diff] [blame] | 1885 | if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1886 | ha_alert("Cannot load configuration file/directory %s : %s\n", |
| 1887 | *argv, |
| 1888 | err_msg); |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 1889 | exit(1); |
| 1890 | } |
Willy Tarreau | 5d01a63 | 2009-06-22 16:02:30 +0200 | [diff] [blame] | 1891 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1892 | case 'p' : cfg_pidfile = *argv; break; |
Willy Tarreau | 3bafcdc | 2011-09-10 19:20:23 +0200 | [diff] [blame] | 1893 | default: usage(progname); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1894 | } |
| 1895 | } |
| 1896 | } |
| 1897 | else |
Willy Tarreau | 3bafcdc | 2011-09-10 19:20:23 +0200 | [diff] [blame] | 1898 | usage(progname); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1899 | argv++; argc--; |
| 1900 | } |
| 1901 | |
Christopher Faulet | e3a5e35 | 2017-10-24 13:53:54 +0200 | [diff] [blame] | 1902 | global.mode |= (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE |
| 1903 | | MODE_QUIET | MODE_CHECK | MODE_DEBUG)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1904 | |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 1905 | if (getenv("HAPROXY_MWORKER_WAIT_ONLY")) { |
William Lallemand | cb11fd2 | 2017-06-01 17:38:52 +0200 | [diff] [blame] | 1906 | unsetenv("HAPROXY_MWORKER_WAIT_ONLY"); |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 1907 | global.mode |= MODE_MWORKER_WAIT; |
| 1908 | global.mode &= ~MODE_MWORKER; |
William Lallemand | cb11fd2 | 2017-06-01 17:38:52 +0200 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | if ((global.mode & MODE_MWORKER) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) { |
| 1912 | atexit_flag = 1; |
| 1913 | atexit(reexec_on_failure); |
| 1914 | } |
| 1915 | |
Willy Tarreau | 576132e | 2011-09-10 19:26:56 +0200 | [diff] [blame] | 1916 | if (change_dir && chdir(change_dir) < 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1917 | ha_alert("Could not change to directory %s : %s\n", change_dir, strerror(errno)); |
Willy Tarreau | 576132e | 2011-09-10 19:26:56 +0200 | [diff] [blame] | 1918 | exit(1); |
| 1919 | } |
| 1920 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1921 | global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */ |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 1922 | |
| 1923 | init_default_instance(); |
| 1924 | |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 1925 | /* in wait mode, we don't try to read the configuration files */ |
| 1926 | if (!(global.mode & MODE_MWORKER_WAIT)) { |
William Lallemand | 7b302d8 | 2019-05-20 11:15:37 +0200 | [diff] [blame] | 1927 | struct buffer *trash = get_trash_chunk(); |
Willy Tarreau | c438242 | 2009-12-06 13:10:44 +0100 | [diff] [blame] | 1928 | |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 1929 | /* handle cfgfiles that are actually directories */ |
| 1930 | cfgfiles_expand_directories(); |
| 1931 | |
| 1932 | if (LIST_ISEMPTY(&cfg_cfgfiles)) |
| 1933 | usage(progname); |
| 1934 | |
| 1935 | |
| 1936 | list_for_each_entry(wl, &cfg_cfgfiles, list) { |
| 1937 | int ret; |
| 1938 | |
William Lallemand | 7b302d8 | 2019-05-20 11:15:37 +0200 | [diff] [blame] | 1939 | if (trash->data) |
| 1940 | chunk_appendf(trash, ";"); |
| 1941 | |
| 1942 | chunk_appendf(trash, "%s", wl->s); |
| 1943 | |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 1944 | ret = readcfgfile(wl->s); |
| 1945 | if (ret == -1) { |
| 1946 | ha_alert("Could not open configuration file %s : %s\n", |
| 1947 | wl->s, strerror(errno)); |
| 1948 | exit(1); |
| 1949 | } |
| 1950 | if (ret & (ERR_ABORT|ERR_FATAL)) |
| 1951 | ha_alert("Error(s) found in configuration file : %s\n", wl->s); |
| 1952 | err_code |= ret; |
| 1953 | if (err_code & ERR_ABORT) |
| 1954 | exit(1); |
Willy Tarreau | c438242 | 2009-12-06 13:10:44 +0100 | [diff] [blame] | 1955 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1956 | |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 1957 | /* do not try to resolve arguments nor to spot inconsistencies when |
| 1958 | * the configuration contains fatal errors caused by files not found |
| 1959 | * or failed memory allocations. |
| 1960 | */ |
| 1961 | if (err_code & (ERR_ABORT|ERR_FATAL)) { |
| 1962 | ha_alert("Fatal errors found in configuration.\n"); |
| 1963 | exit(1); |
| 1964 | } |
William Lallemand | 7b302d8 | 2019-05-20 11:15:37 +0200 | [diff] [blame] | 1965 | if (trash->data) |
| 1966 | setenv("HAPROXY_CFGFILES", trash->area, 1); |
| 1967 | |
Willy Tarreau | b83dc3d | 2017-04-19 11:24:07 +0200 | [diff] [blame] | 1968 | } |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 1969 | if (global.mode & MODE_MWORKER) { |
| 1970 | int proc; |
William Lallemand | 16dd1b3 | 2018-11-19 18:46:18 +0100 | [diff] [blame] | 1971 | struct mworker_proc *tmproc; |
| 1972 | |
William Lallemand | 482f9a9 | 2019-04-12 16:15:00 +0200 | [diff] [blame] | 1973 | setenv("HAPROXY_MWORKER", "1", 1); |
| 1974 | |
William Lallemand | 16dd1b3 | 2018-11-19 18:46:18 +0100 | [diff] [blame] | 1975 | if (getenv("HAPROXY_MWORKER_REEXEC") == NULL) { |
| 1976 | |
William Lallemand | f3a8683 | 2019-04-01 11:29:58 +0200 | [diff] [blame] | 1977 | tmproc = calloc(1, sizeof(*tmproc)); |
William Lallemand | 16dd1b3 | 2018-11-19 18:46:18 +0100 | [diff] [blame] | 1978 | if (!tmproc) { |
| 1979 | ha_alert("Cannot allocate process structures.\n"); |
| 1980 | exit(EXIT_FAILURE); |
| 1981 | } |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 1982 | tmproc->options |= PROC_O_TYPE_MASTER; /* master */ |
William Lallemand | 16dd1b3 | 2018-11-19 18:46:18 +0100 | [diff] [blame] | 1983 | tmproc->reloads = 0; |
| 1984 | tmproc->relative_pid = 0; |
| 1985 | tmproc->pid = pid; |
| 1986 | tmproc->timestamp = start_date.tv_sec; |
| 1987 | tmproc->ipc_fd[0] = -1; |
| 1988 | tmproc->ipc_fd[1] = -1; |
| 1989 | |
| 1990 | proc_self = tmproc; |
| 1991 | |
| 1992 | LIST_ADDQ(&proc_list, &tmproc->list); |
| 1993 | } |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 1994 | |
| 1995 | for (proc = 0; proc < global.nbproc; proc++) { |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 1996 | |
William Lallemand | f3a8683 | 2019-04-01 11:29:58 +0200 | [diff] [blame] | 1997 | tmproc = calloc(1, sizeof(*tmproc)); |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 1998 | if (!tmproc) { |
| 1999 | ha_alert("Cannot allocate process structures.\n"); |
| 2000 | exit(EXIT_FAILURE); |
| 2001 | } |
| 2002 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 2003 | tmproc->options |= PROC_O_TYPE_WORKER; /* worker */ |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 2004 | tmproc->pid = -1; |
| 2005 | tmproc->reloads = 0; |
William Lallemand | e368330 | 2018-11-19 18:46:17 +0100 | [diff] [blame] | 2006 | tmproc->timestamp = -1; |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 2007 | tmproc->relative_pid = 1 + proc; |
William Lallemand | 550db6d | 2018-11-06 17:37:12 +0100 | [diff] [blame] | 2008 | tmproc->ipc_fd[0] = -1; |
| 2009 | tmproc->ipc_fd[1] = -1; |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 2010 | |
| 2011 | if (mworker_cli_sockpair_new(tmproc, proc) < 0) { |
| 2012 | exit(EXIT_FAILURE); |
| 2013 | } |
| 2014 | |
| 2015 | LIST_ADDQ(&proc_list, &tmproc->list); |
| 2016 | } |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 2017 | } |
| 2018 | if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) { |
| 2019 | struct wordlist *it, *c; |
| 2020 | |
William Lallemand | 1b66361 | 2018-10-26 14:47:33 +0200 | [diff] [blame] | 2021 | mworker_env_to_proc_list(); /* get the info of the children in the env */ |
William Lallemand | 8a02257 | 2018-10-26 14:47:35 +0200 | [diff] [blame] | 2022 | |
William Lallemand | e736115 | 2018-10-26 14:47:36 +0200 | [diff] [blame] | 2023 | |
William Lallemand | 550db6d | 2018-11-06 17:37:12 +0100 | [diff] [blame] | 2024 | if (!LIST_ISEMPTY(&mworker_cli_conf)) { |
William Lallemand | e736115 | 2018-10-26 14:47:36 +0200 | [diff] [blame] | 2025 | |
William Lallemand | 550db6d | 2018-11-06 17:37:12 +0100 | [diff] [blame] | 2026 | if (mworker_cli_proxy_create() < 0) { |
William Lallemand | e736115 | 2018-10-26 14:47:36 +0200 | [diff] [blame] | 2027 | ha_alert("Can't create the master's CLI.\n"); |
| 2028 | exit(EXIT_FAILURE); |
| 2029 | } |
William Lallemand | e736115 | 2018-10-26 14:47:36 +0200 | [diff] [blame] | 2030 | |
William Lallemand | 550db6d | 2018-11-06 17:37:12 +0100 | [diff] [blame] | 2031 | list_for_each_entry_safe(c, it, &mworker_cli_conf, list) { |
| 2032 | |
| 2033 | if (mworker_cli_proxy_new_listener(c->s) < 0) { |
| 2034 | ha_alert("Can't create the master's CLI.\n"); |
| 2035 | exit(EXIT_FAILURE); |
| 2036 | } |
| 2037 | LIST_DEL(&c->list); |
| 2038 | free(c->s); |
| 2039 | free(c); |
| 2040 | } |
| 2041 | } |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 2042 | } |
| 2043 | |
Willy Tarreau | bb92501 | 2009-07-23 13:36:36 +0200 | [diff] [blame] | 2044 | err_code |= check_config_validity(); |
Christopher Faulet | c169296 | 2019-08-12 09:51:07 +0200 | [diff] [blame] | 2045 | for (px = proxies_list; px; px = px->next) { |
| 2046 | struct server *srv; |
| 2047 | struct post_proxy_check_fct *ppcf; |
| 2048 | struct post_server_check_fct *pscf; |
| 2049 | |
| 2050 | list_for_each_entry(pscf, &post_server_check_list, list) { |
| 2051 | for (srv = px->srv; srv; srv = srv->next) |
| 2052 | err_code |= pscf->fct(srv); |
| 2053 | } |
| 2054 | list_for_each_entry(ppcf, &post_proxy_check_list, list) |
| 2055 | err_code |= ppcf->fct(px); |
| 2056 | } |
Willy Tarreau | bb92501 | 2009-07-23 13:36:36 +0200 | [diff] [blame] | 2057 | if (err_code & (ERR_ABORT|ERR_FATAL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2058 | ha_alert("Fatal errors found in configuration.\n"); |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 2059 | exit(1); |
| 2060 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2061 | |
Carl Henrik Lunde | f91ac19 | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2062 | err_code |= pattern_finalize_config(); |
| 2063 | if (err_code & (ERR_ABORT|ERR_FATAL)) { |
| 2064 | ha_alert("Failed to finalize pattern config.\n"); |
| 2065 | exit(1); |
| 2066 | } |
Willy Tarreau | 0f93672 | 2019-04-11 14:47:08 +0200 | [diff] [blame] | 2067 | |
Willy Tarreau | 7006045 | 2015-12-14 12:46:07 +0100 | [diff] [blame] | 2068 | /* recompute the amount of per-process memory depending on nbproc and |
| 2069 | * the shared SSL cache size (allowed to exist in all processes). |
| 2070 | */ |
| 2071 | if (global.rlimit_memmax_all) { |
| 2072 | #if defined (USE_OPENSSL) && !defined(USE_PRIVATE_CACHE) |
| 2073 | int64_t ssl_cache_bytes = global.tune.sslcachesize * 200LL; |
| 2074 | |
| 2075 | global.rlimit_memmax = |
| 2076 | ((((int64_t)global.rlimit_memmax_all * 1048576LL) - |
| 2077 | ssl_cache_bytes) / global.nbproc + |
| 2078 | ssl_cache_bytes + 1048575LL) / 1048576LL; |
| 2079 | #else |
| 2080 | global.rlimit_memmax = global.rlimit_memmax_all / global.nbproc; |
| 2081 | #endif |
| 2082 | } |
| 2083 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 2084 | #ifdef USE_NS |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 2085 | err_code |= netns_init(); |
| 2086 | if (err_code & (ERR_ABORT|ERR_FATAL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2087 | ha_alert("Failed to initialize namespace support.\n"); |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 2088 | exit(1); |
| 2089 | } |
| 2090 | #endif |
| 2091 | |
Baptiste Assmann | 4215d7d | 2016-11-02 15:33:15 +0100 | [diff] [blame] | 2092 | /* Apply server states */ |
| 2093 | apply_server_state(); |
| 2094 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2095 | for (px = proxies_list; px; px = px->next) |
Baptiste Assmann | 4215d7d | 2016-11-02 15:33:15 +0100 | [diff] [blame] | 2096 | srv_compute_all_admin_states(px); |
| 2097 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 2098 | /* Apply servers' configured address */ |
| 2099 | err_code |= srv_init_addr(); |
| 2100 | if (err_code & (ERR_ABORT|ERR_FATAL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2101 | ha_alert("Failed to initialize server(s) addr.\n"); |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 2102 | exit(1); |
| 2103 | } |
| 2104 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2105 | if (global.mode & MODE_CHECK) { |
Willy Tarreau | 8b15ba1 | 2012-02-02 17:48:18 +0100 | [diff] [blame] | 2106 | struct peers *pr; |
| 2107 | struct proxy *px; |
| 2108 | |
Frédéric Lécaille | ed2b4a6 | 2017-07-13 09:07:09 +0200 | [diff] [blame] | 2109 | for (pr = cfg_peers; pr; pr = pr->next) |
Willy Tarreau | 8b15ba1 | 2012-02-02 17:48:18 +0100 | [diff] [blame] | 2110 | if (pr->peers_fe) |
| 2111 | break; |
| 2112 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2113 | for (px = proxies_list; px; px = px->next) |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2114 | if (px->state == PR_STNEW && !LIST_ISEMPTY(&px->conf.listeners)) |
Willy Tarreau | 8b15ba1 | 2012-02-02 17:48:18 +0100 | [diff] [blame] | 2115 | break; |
| 2116 | |
| 2117 | if (pr || px) { |
| 2118 | /* At least one peer or one listener has been found */ |
| 2119 | qfprintf(stdout, "Configuration file is valid\n"); |
| 2120 | exit(0); |
| 2121 | } |
| 2122 | qfprintf(stdout, "Configuration file has no error but will not start (no listener) => exit(2).\n"); |
| 2123 | exit(2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2124 | } |
Willy Tarreau | e9b2602 | 2011-08-01 20:57:55 +0200 | [diff] [blame] | 2125 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2126 | /* 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] | 2127 | init_buffer(); |
Willy Tarreau | 8280d64 | 2009-09-23 23:37:52 +0200 | [diff] [blame] | 2128 | |
Willy Tarreau | e694573 | 2016-12-21 19:57:00 +0100 | [diff] [blame] | 2129 | list_for_each_entry(pcf, &post_check_list, list) { |
| 2130 | err_code |= pcf->fct(); |
| 2131 | if (err_code & (ERR_ABORT|ERR_FATAL)) |
| 2132 | exit(1); |
| 2133 | } |
| 2134 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2135 | if (cfg_maxconn > 0) |
| 2136 | global.maxconn = cfg_maxconn; |
| 2137 | |
Willy Tarreau | 8d687d8 | 2019-03-01 09:39:42 +0100 | [diff] [blame] | 2138 | if (global.stats_fe) |
| 2139 | global.maxsock += global.stats_fe->maxconn; |
| 2140 | |
| 2141 | if (cfg_peers) { |
| 2142 | /* peers also need to bypass global maxconn */ |
| 2143 | struct peers *p = cfg_peers; |
| 2144 | |
| 2145 | for (p = cfg_peers; p; p = p->next) |
| 2146 | if (p->peers_fe) |
| 2147 | global.maxsock += p->peers_fe->maxconn; |
| 2148 | } |
| 2149 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2150 | if (cfg_pidfile) { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2151 | free(global.pidfile); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2152 | global.pidfile = strdup(cfg_pidfile); |
| 2153 | } |
| 2154 | |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 2155 | /* Now we want to compute the maxconn and possibly maxsslconn values. |
Willy Tarreau | ac35093 | 2019-03-01 15:43:14 +0100 | [diff] [blame] | 2156 | * It's a bit tricky. Maxconn defaults to the pre-computed value based |
| 2157 | * on rlim_fd_cur and the number of FDs in use due to the configuration, |
| 2158 | * and maxsslconn defaults to DEFAULT_MAXSSLCONN. On top of that we can |
| 2159 | * enforce a lower limit based on memmax. |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 2160 | * |
| 2161 | * If memmax is set, then it depends on which values are set. If |
| 2162 | * maxsslconn is set, we use memmax to determine how many cleartext |
| 2163 | * connections may be added, and set maxconn to the sum of the two. |
| 2164 | * If maxconn is set and not maxsslconn, maxsslconn is computed from |
| 2165 | * the remaining amount of memory between memmax and the cleartext |
| 2166 | * connections. If neither are set, then it is considered that all |
| 2167 | * connections are SSL-capable, and maxconn is computed based on this, |
| 2168 | * then maxsslconn accordingly. We need to know if SSL is used on the |
| 2169 | * frontends, backends, or both, because when it's used on both sides, |
| 2170 | * we need twice the value for maxsslconn, but we only count the |
| 2171 | * handshake once since it is not performed on the two sides at the |
| 2172 | * same time (frontend-side is terminated before backend-side begins). |
| 2173 | * The SSL stack is supposed to have filled ssl_session_cost and |
Willy Tarreau | 474b96a | 2015-01-28 19:03:21 +0100 | [diff] [blame] | 2174 | * ssl_handshake_cost during its initialization. In any case, if |
| 2175 | * SYSTEM_MAXCONN is set, we still enforce it as an upper limit for |
| 2176 | * maxconn in order to protect the system. |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 2177 | */ |
Willy Tarreau | ac35093 | 2019-03-01 15:43:14 +0100 | [diff] [blame] | 2178 | ideal_maxconn = compute_ideal_maxconn(); |
| 2179 | |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 2180 | if (!global.rlimit_memmax) { |
| 2181 | if (global.maxconn == 0) { |
Willy Tarreau | ac35093 | 2019-03-01 15:43:14 +0100 | [diff] [blame] | 2182 | global.maxconn = ideal_maxconn; |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 2183 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) |
| 2184 | fprintf(stderr, "Note: setting global.maxconn to %d.\n", global.maxconn); |
| 2185 | } |
| 2186 | } |
| 2187 | #ifdef USE_OPENSSL |
| 2188 | else if (!global.maxconn && !global.maxsslconn && |
| 2189 | (global.ssl_used_frontend || global.ssl_used_backend)) { |
| 2190 | /* memmax is set, compute everything automatically. Here we want |
| 2191 | * to ensure that all SSL connections will be served. We take |
| 2192 | * care of the number of sides where SSL is used, and consider |
| 2193 | * the worst case : SSL used on both sides and doing a handshake |
| 2194 | * simultaneously. Note that we can't have more than maxconn |
| 2195 | * handshakes at a time by definition, so for the worst case of |
| 2196 | * two SSL conns per connection, we count a single handshake. |
| 2197 | */ |
| 2198 | int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend; |
| 2199 | int64_t mem = global.rlimit_memmax * 1048576ULL; |
Willy Tarreau | 304e17e | 2020-03-10 17:54:54 +0100 | [diff] [blame] | 2200 | int retried = 0; |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 2201 | |
| 2202 | mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry |
| 2203 | mem -= global.maxzlibmem; |
| 2204 | mem = mem * MEM_USABLE_RATIO; |
| 2205 | |
Willy Tarreau | 304e17e | 2020-03-10 17:54:54 +0100 | [diff] [blame] | 2206 | /* Principle: we test once to set maxconn according to the free |
| 2207 | * memory. If it results in values the system rejects, we try a |
| 2208 | * second time by respecting rlim_fd_max. If it fails again, we |
| 2209 | * go back to the initial value and will let the final code |
| 2210 | * dealing with rlimit report the error. That's up to 3 attempts. |
| 2211 | */ |
| 2212 | do { |
| 2213 | global.maxconn = mem / |
| 2214 | ((STREAM_MAX_COST + 2 * global.tune.bufsize) + // stream + 2 buffers per stream |
| 2215 | sides * global.ssl_session_max_cost + // SSL buffers, one per side |
| 2216 | global.ssl_handshake_max_cost); // 1 handshake per connection max |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 2217 | |
Willy Tarreau | 304e17e | 2020-03-10 17:54:54 +0100 | [diff] [blame] | 2218 | if (retried == 1) |
| 2219 | global.maxconn = MIN(global.maxconn, ideal_maxconn); |
| 2220 | global.maxconn = round_2dig(global.maxconn); |
Willy Tarreau | 474b96a | 2015-01-28 19:03:21 +0100 | [diff] [blame] | 2221 | #ifdef SYSTEM_MAXCONN |
Willy Tarreau | 304e17e | 2020-03-10 17:54:54 +0100 | [diff] [blame] | 2222 | if (global.maxconn > SYSTEM_MAXCONN) |
| 2223 | global.maxconn = SYSTEM_MAXCONN; |
Willy Tarreau | 474b96a | 2015-01-28 19:03:21 +0100 | [diff] [blame] | 2224 | #endif /* SYSTEM_MAXCONN */ |
Willy Tarreau | 304e17e | 2020-03-10 17:54:54 +0100 | [diff] [blame] | 2225 | global.maxsslconn = sides * global.maxconn; |
| 2226 | |
| 2227 | if (check_if_maxsock_permitted(compute_ideal_maxsock(global.maxconn))) |
| 2228 | break; |
| 2229 | } while (retried++ < 2); |
| 2230 | |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 2231 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) |
| 2232 | fprintf(stderr, "Note: setting global.maxconn to %d and global.maxsslconn to %d.\n", |
| 2233 | global.maxconn, global.maxsslconn); |
| 2234 | } |
| 2235 | else if (!global.maxsslconn && |
| 2236 | (global.ssl_used_frontend || global.ssl_used_backend)) { |
| 2237 | /* memmax and maxconn are known, compute maxsslconn automatically. |
| 2238 | * maxsslconn being forced, we don't know how many of it will be |
| 2239 | * on each side if both sides are being used. The worst case is |
| 2240 | * when all connections use only one SSL instance because |
| 2241 | * handshakes may be on two sides at the same time. |
| 2242 | */ |
| 2243 | int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend; |
| 2244 | int64_t mem = global.rlimit_memmax * 1048576ULL; |
| 2245 | int64_t sslmem; |
| 2246 | |
| 2247 | mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry |
| 2248 | mem -= global.maxzlibmem; |
| 2249 | mem = mem * MEM_USABLE_RATIO; |
| 2250 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2251 | 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] | 2252 | global.maxsslconn = sslmem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost); |
| 2253 | global.maxsslconn = round_2dig(global.maxsslconn); |
| 2254 | |
| 2255 | if (sslmem <= 0 || global.maxsslconn < sides) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2256 | ha_alert("Cannot compute the automatic maxsslconn because global.maxconn is already too " |
| 2257 | "high for the global.memmax value (%d MB). The absolute maximum possible value " |
| 2258 | "without SSL is %d, but %d was found and SSL is in use.\n", |
| 2259 | global.rlimit_memmax, |
| 2260 | (int)(mem / (STREAM_MAX_COST + 2 * global.tune.bufsize)), |
| 2261 | global.maxconn); |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 2262 | exit(1); |
| 2263 | } |
| 2264 | |
| 2265 | if (global.maxsslconn > sides * global.maxconn) |
| 2266 | global.maxsslconn = sides * global.maxconn; |
| 2267 | |
| 2268 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) |
| 2269 | fprintf(stderr, "Note: setting global.maxsslconn to %d\n", global.maxsslconn); |
| 2270 | } |
| 2271 | #endif |
| 2272 | else if (!global.maxconn) { |
| 2273 | /* memmax and maxsslconn are known/unused, compute maxconn automatically */ |
| 2274 | int sides = !!global.ssl_used_frontend + !!global.ssl_used_backend; |
| 2275 | int64_t mem = global.rlimit_memmax * 1048576ULL; |
| 2276 | int64_t clearmem; |
Willy Tarreau | 304e17e | 2020-03-10 17:54:54 +0100 | [diff] [blame] | 2277 | int retried = 0; |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 2278 | |
| 2279 | if (global.ssl_used_frontend || global.ssl_used_backend) |
| 2280 | mem -= global.tune.sslcachesize * 200; // about 200 bytes per SSL cache entry |
| 2281 | |
| 2282 | mem -= global.maxzlibmem; |
| 2283 | mem = mem * MEM_USABLE_RATIO; |
| 2284 | |
| 2285 | clearmem = mem; |
| 2286 | if (sides) |
| 2287 | clearmem -= (global.ssl_session_max_cost + global.ssl_handshake_max_cost) * (int64_t)global.maxsslconn; |
| 2288 | |
Willy Tarreau | 304e17e | 2020-03-10 17:54:54 +0100 | [diff] [blame] | 2289 | /* Principle: we test once to set maxconn according to the free |
| 2290 | * memory. If it results in values the system rejects, we try a |
| 2291 | * second time by respecting rlim_fd_max. If it fails again, we |
| 2292 | * go back to the initial value and will let the final code |
| 2293 | * dealing with rlimit report the error. That's up to 3 attempts. |
| 2294 | */ |
| 2295 | do { |
| 2296 | global.maxconn = clearmem / (STREAM_MAX_COST + 2 * global.tune.bufsize); |
| 2297 | if (retried == 1) |
| 2298 | global.maxconn = MIN(global.maxconn, ideal_maxconn); |
| 2299 | global.maxconn = round_2dig(global.maxconn); |
Willy Tarreau | 474b96a | 2015-01-28 19:03:21 +0100 | [diff] [blame] | 2300 | #ifdef SYSTEM_MAXCONN |
Willy Tarreau | 304e17e | 2020-03-10 17:54:54 +0100 | [diff] [blame] | 2301 | if (global.maxconn > SYSTEM_MAXCONN) |
| 2302 | global.maxconn = SYSTEM_MAXCONN; |
Willy Tarreau | 474b96a | 2015-01-28 19:03:21 +0100 | [diff] [blame] | 2303 | #endif /* SYSTEM_MAXCONN */ |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 2304 | |
Willy Tarreau | 304e17e | 2020-03-10 17:54:54 +0100 | [diff] [blame] | 2305 | if (clearmem <= 0 || !global.maxconn) { |
| 2306 | ha_alert("Cannot compute the automatic maxconn because global.maxsslconn is already too " |
| 2307 | "high for the global.memmax value (%d MB). The absolute maximum possible value " |
| 2308 | "is %d, but %d was found.\n", |
| 2309 | global.rlimit_memmax, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2310 | (int)(mem / (global.ssl_session_max_cost + global.ssl_handshake_max_cost)), |
Willy Tarreau | 304e17e | 2020-03-10 17:54:54 +0100 | [diff] [blame] | 2311 | global.maxsslconn); |
| 2312 | exit(1); |
| 2313 | } |
| 2314 | |
| 2315 | if (check_if_maxsock_permitted(compute_ideal_maxsock(global.maxconn))) |
| 2316 | break; |
| 2317 | } while (retried++ < 2); |
Willy Tarreau | d025648 | 2015-01-15 21:45:22 +0100 | [diff] [blame] | 2318 | |
| 2319 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) { |
| 2320 | if (sides && global.maxsslconn > sides * global.maxconn) { |
| 2321 | fprintf(stderr, "Note: global.maxsslconn is forced to %d which causes global.maxconn " |
| 2322 | "to be limited to %d. Better reduce global.maxsslconn to get more " |
| 2323 | "room for extra connections.\n", global.maxsslconn, global.maxconn); |
| 2324 | } |
| 2325 | fprintf(stderr, "Note: setting global.maxconn to %d\n", global.maxconn); |
| 2326 | } |
Willy Tarreau | 66aa61f | 2009-01-18 21:44:07 +0100 | [diff] [blame] | 2327 | } |
| 2328 | |
Willy Tarreau | a409f30 | 2020-03-10 17:08:53 +0100 | [diff] [blame] | 2329 | global.maxsock = compute_ideal_maxsock(global.maxconn); |
| 2330 | global.hardmaxconn = global.maxconn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2331 | |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 2332 | /* update connection pool thresholds */ |
| 2333 | global.tune.pool_low_count = ((long long)global.maxsock * global.tune.pool_low_ratio + 99) / 100; |
| 2334 | global.tune.pool_high_count = ((long long)global.maxsock * global.tune.pool_high_ratio + 99) / 100; |
| 2335 | |
Willy Tarreau | c8d5b95 | 2019-02-27 17:25:52 +0100 | [diff] [blame] | 2336 | proxy_adjust_all_maxconn(); |
| 2337 | |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 2338 | if (global.tune.maxpollevents <= 0) |
| 2339 | global.tune.maxpollevents = MAX_POLL_EVENTS; |
| 2340 | |
Olivier Houchard | 1599b80 | 2018-05-24 18:59:04 +0200 | [diff] [blame] | 2341 | if (global.tune.runqueue_depth <= 0) |
| 2342 | global.tune.runqueue_depth = RUNQUEUE_DEPTH; |
| 2343 | |
Willy Tarreau | 6f4a82c | 2009-03-21 20:43:57 +0100 | [diff] [blame] | 2344 | if (global.tune.recv_enough == 0) |
| 2345 | global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH; |
| 2346 | |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 2347 | if (global.tune.maxrewrite >= global.tune.bufsize / 2) |
| 2348 | global.tune.maxrewrite = global.tune.bufsize / 2; |
| 2349 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2350 | if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) { |
| 2351 | /* command line debug mode inhibits configuration mode */ |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2352 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
Willy Tarreau | 772f0dd | 2012-10-26 16:04:28 +0200 | [diff] [blame] | 2353 | global.mode |= (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)); |
| 2354 | } |
| 2355 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2356 | if (arg_mode & MODE_DAEMON) { |
Willy Tarreau | 772f0dd | 2012-10-26 16:04:28 +0200 | [diff] [blame] | 2357 | /* command line daemon mode inhibits foreground and debug modes mode */ |
| 2358 | global.mode &= ~(MODE_DEBUG | MODE_FOREGROUND); |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2359 | global.mode |= arg_mode & MODE_DAEMON; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2360 | } |
Willy Tarreau | 772f0dd | 2012-10-26 16:04:28 +0200 | [diff] [blame] | 2361 | |
| 2362 | global.mode |= (arg_mode & (MODE_QUIET | MODE_VERBOSE)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2363 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2364 | if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2365 | ha_warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n"); |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2366 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2367 | } |
| 2368 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 2369 | if ((global.nbproc > 1) && !(global.mode & (MODE_DAEMON | MODE_MWORKER))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2370 | if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG))) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2371 | ha_warning("<nbproc> is only meaningful in daemon mode or master-worker mode. Setting limit to 1 process.\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2372 | global.nbproc = 1; |
| 2373 | } |
| 2374 | |
| 2375 | if (global.nbproc < 1) |
| 2376 | global.nbproc = 1; |
| 2377 | |
Christopher Faulet | be0faa2 | 2017-08-29 15:37:10 +0200 | [diff] [blame] | 2378 | if (global.nbthread < 1) |
| 2379 | global.nbthread = 1; |
| 2380 | |
Christopher Faulet | 3ef2639 | 2017-08-29 16:46:57 +0200 | [diff] [blame] | 2381 | /* Realloc trash buffers because global.tune.bufsize may have changed */ |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2382 | if (!init_trash_buffers(0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2383 | ha_alert("failed to initialize trash buffers.\n"); |
Christopher Faulet | 3ef2639 | 2017-08-29 16:46:57 +0200 | [diff] [blame] | 2384 | exit(1); |
| 2385 | } |
| 2386 | |
Christopher Faulet | 96d4483 | 2017-11-14 22:02:30 +0100 | [diff] [blame] | 2387 | if (!init_log_buffers()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2388 | ha_alert("failed to initialize log buffers.\n"); |
Christopher Faulet | 96d4483 | 2017-11-14 22:02:30 +0100 | [diff] [blame] | 2389 | exit(1); |
| 2390 | } |
| 2391 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 2392 | /* |
| 2393 | * Note: we could register external pollers here. |
| 2394 | * Built-in pollers have been registered before main(). |
| 2395 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2396 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 2397 | if (!(global.tune.options & GTUNE_USE_KQUEUE)) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 2398 | disable_poller("kqueue"); |
| 2399 | |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 2400 | if (!(global.tune.options & GTUNE_USE_EVPORTS)) |
| 2401 | disable_poller("evports"); |
| 2402 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 2403 | if (!(global.tune.options & GTUNE_USE_EPOLL)) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2404 | disable_poller("epoll"); |
| 2405 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 2406 | if (!(global.tune.options & GTUNE_USE_POLL)) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2407 | disable_poller("poll"); |
| 2408 | |
Willy Tarreau | 43b7899 | 2009-01-25 15:42:27 +0100 | [diff] [blame] | 2409 | if (!(global.tune.options & GTUNE_USE_SELECT)) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2410 | disable_poller("select"); |
| 2411 | |
| 2412 | /* Note: we could disable any poller by name here */ |
| 2413 | |
Christopher Faulet | b3f4e14 | 2016-03-07 12:46:38 +0100 | [diff] [blame] | 2414 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 2415 | list_pollers(stderr); |
Christopher Faulet | b3f4e14 | 2016-03-07 12:46:38 +0100 | [diff] [blame] | 2416 | fprintf(stderr, "\n"); |
| 2417 | list_filters(stderr); |
| 2418 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 2419 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2420 | if (!init_pollers()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2421 | ha_alert("No polling mechanism available.\n" |
| 2422 | " It is likely that haproxy was built with TARGET=generic and that FD_SETSIZE\n" |
| 2423 | " is too low on this platform to support maxconn and the number of listeners\n" |
| 2424 | " and servers. You should rebuild haproxy specifying your system using TARGET=\n" |
| 2425 | " in order to support other polling systems (poll, epoll, kqueue) or reduce the\n" |
| 2426 | " global maxconn setting to accommodate the system's limitation. For reference,\n" |
| 2427 | " FD_SETSIZE=%d on this system, global.maxconn=%d resulting in a maximum of\n" |
| 2428 | " %d file descriptors. You should thus reduce global.maxconn by %d. Also,\n" |
| 2429 | " check build settings using 'haproxy -vv'.\n\n", |
| 2430 | FD_SETSIZE, global.maxconn, global.maxsock, (global.maxsock + 1 - FD_SETSIZE) / 2); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2431 | exit(1); |
| 2432 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 2433 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) { |
| 2434 | printf("Using %s() as the polling mechanism.\n", cur_poller.name); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2435 | } |
| 2436 | |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 2437 | if (!global.node) |
| 2438 | global.node = strdup(hostname); |
| 2439 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 2440 | if (!hlua_post_init()) |
| 2441 | exit(1); |
Thomas Holmes | 6abded4 | 2015-05-12 16:23:58 +0100 | [diff] [blame] | 2442 | |
Maxime de Roucy | 0f50392 | 2016-05-13 23:52:55 +0200 | [diff] [blame] | 2443 | free(err_msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2444 | } |
| 2445 | |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 2446 | static void deinit_acl_cond(struct acl_cond *cond) |
Simon Horman | ac82142 | 2011-07-15 13:14:09 +0900 | [diff] [blame] | 2447 | { |
Simon Horman | ac82142 | 2011-07-15 13:14:09 +0900 | [diff] [blame] | 2448 | struct acl_term_suite *suite, *suiteb; |
| 2449 | struct acl_term *term, *termb; |
| 2450 | |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 2451 | if (!cond) |
| 2452 | return; |
| 2453 | |
| 2454 | list_for_each_entry_safe(suite, suiteb, &cond->suites, list) { |
| 2455 | list_for_each_entry_safe(term, termb, &suite->terms, list) { |
| 2456 | LIST_DEL(&term->list); |
| 2457 | free(term); |
Simon Horman | ac82142 | 2011-07-15 13:14:09 +0900 | [diff] [blame] | 2458 | } |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 2459 | LIST_DEL(&suite->list); |
| 2460 | free(suite); |
| 2461 | } |
| 2462 | |
| 2463 | free(cond); |
| 2464 | } |
| 2465 | |
Christopher Faulet | cb55013 | 2019-12-17 11:25:46 +0100 | [diff] [blame] | 2466 | static void deinit_act_rules(struct list *rules) |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 2467 | { |
Christopher Faulet | cb55013 | 2019-12-17 11:25:46 +0100 | [diff] [blame] | 2468 | struct act_rule *rule, *ruleb; |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 2469 | |
Christopher Faulet | cb55013 | 2019-12-17 11:25:46 +0100 | [diff] [blame] | 2470 | list_for_each_entry_safe(rule, ruleb, rules, list) { |
| 2471 | LIST_DEL(&rule->list); |
| 2472 | deinit_acl_cond(rule->cond); |
Christopher Faulet | 58b3564 | 2019-12-17 11:48:42 +0100 | [diff] [blame] | 2473 | if (rule->release_ptr) |
| 2474 | rule->release_ptr(rule); |
Christopher Faulet | cb55013 | 2019-12-17 11:25:46 +0100 | [diff] [blame] | 2475 | free(rule); |
Simon Horman | ac82142 | 2011-07-15 13:14:09 +0900 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 2479 | static void deinit_stick_rules(struct list *rules) |
| 2480 | { |
| 2481 | struct sticking_rule *rule, *ruleb; |
| 2482 | |
| 2483 | list_for_each_entry_safe(rule, ruleb, rules, list) { |
| 2484 | LIST_DEL(&rule->list); |
| 2485 | deinit_acl_cond(rule->cond); |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 2486 | release_sample_expr(rule->expr); |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 2487 | free(rule); |
| 2488 | } |
| 2489 | } |
| 2490 | |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 2491 | void deinit(void) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2492 | { |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2493 | struct proxy *p = proxies_list, *p0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2494 | struct cap_hdr *h,*h_next; |
| 2495 | struct server *s,*s_next; |
| 2496 | struct listener *l,*l_next; |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 2497 | struct acl_cond *cond, *condb; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2498 | struct acl *acl, *aclb; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2499 | struct switching_rule *rule, *ruleb; |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 2500 | struct server_rule *srule, *sruleb; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2501 | struct redirect_rule *rdr, *rdrb; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2502 | struct wordlist *wl, *wlb; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2503 | struct uri_auth *uap, *ua = NULL; |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 2504 | struct logsrv *log, *logb; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 2505 | struct logformat_node *lf, *lfb; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2506 | struct bind_conf *bind_conf, *bind_back; |
Willy Tarreau | cdb737e | 2016-12-21 18:43:10 +0100 | [diff] [blame] | 2507 | struct build_opts_str *bol, *bolb; |
Willy Tarreau | 05554e6 | 2016-12-21 20:46:26 +0100 | [diff] [blame] | 2508 | struct post_deinit_fct *pdf; |
Christopher Faulet | 3ea5cbe | 2019-07-31 08:44:12 +0200 | [diff] [blame] | 2509 | struct proxy_deinit_fct *pxdf; |
| 2510 | struct server_deinit_fct *srvdf; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2511 | |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 2512 | deinit_signals(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2513 | while (p) { |
Willy Tarreau | 8113a5d | 2012-10-04 08:01:43 +0200 | [diff] [blame] | 2514 | free(p->conf.file); |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2515 | free(p->id); |
| 2516 | free(p->check_req); |
| 2517 | free(p->cookie_name); |
| 2518 | free(p->cookie_domain); |
Christopher Faulet | 2f53390 | 2020-01-21 11:06:48 +0100 | [diff] [blame] | 2519 | free(p->cookie_attrs); |
Willy Tarreau | 4c03d1c | 2019-01-14 15:23:54 +0100 | [diff] [blame] | 2520 | free(p->lbprm.arg_str); |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2521 | free(p->capture_name); |
| 2522 | free(p->monitor_uri); |
Simon Horman | a31c7f7 | 2011-07-15 13:14:08 +0900 | [diff] [blame] | 2523 | free(p->rdp_cookie_name); |
Willy Tarreau | 62a6123 | 2013-04-12 18:13:46 +0200 | [diff] [blame] | 2524 | if (p->conf.logformat_string != default_http_log_format && |
| 2525 | p->conf.logformat_string != default_tcp_log_format && |
| 2526 | p->conf.logformat_string != clf_http_log_format) |
| 2527 | free(p->conf.logformat_string); |
Willy Tarreau | 196729e | 2012-05-31 19:30:26 +0200 | [diff] [blame] | 2528 | |
Willy Tarreau | 62a6123 | 2013-04-12 18:13:46 +0200 | [diff] [blame] | 2529 | free(p->conf.lfs_file); |
| 2530 | free(p->conf.uniqueid_format_string); |
| 2531 | free(p->conf.uif_file); |
Willy Tarreau | 0cac26c | 2019-01-14 16:55:42 +0100 | [diff] [blame] | 2532 | if ((p->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_MAP) |
| 2533 | free(p->lbprm.map.srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2534 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2535 | if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format) |
| 2536 | free(p->conf.logformat_sd_string); |
| 2537 | free(p->conf.lfsd_file); |
| 2538 | |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2539 | list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) { |
| 2540 | LIST_DEL(&cond->list); |
| 2541 | prune_acl_cond(cond); |
| 2542 | free(cond); |
| 2543 | } |
| 2544 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2545 | /* build a list of unique uri_auths */ |
| 2546 | if (!ua) |
| 2547 | ua = p->uri_auth; |
| 2548 | else { |
| 2549 | /* check if p->uri_auth is unique */ |
| 2550 | for (uap = ua; uap; uap=uap->next) |
| 2551 | if (uap == p->uri_auth) |
| 2552 | break; |
| 2553 | |
Willy Tarreau | accc4e1 | 2008-06-24 11:14:45 +0200 | [diff] [blame] | 2554 | if (!uap && p->uri_auth) { |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2555 | /* add it, if it is */ |
| 2556 | p->uri_auth->next = ua; |
| 2557 | ua = p->uri_auth; |
| 2558 | } |
| 2559 | } |
Willy Tarreau | 0fc45a7 | 2007-06-17 00:36:03 +0200 | [diff] [blame] | 2560 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2561 | list_for_each_entry_safe(acl, aclb, &p->acl, list) { |
| 2562 | LIST_DEL(&acl->list); |
| 2563 | prune_acl(acl); |
| 2564 | free(acl); |
| 2565 | } |
| 2566 | |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 2567 | list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) { |
| 2568 | LIST_DEL(&srule->list); |
| 2569 | prune_acl_cond(srule->cond); |
| 2570 | free(srule->cond); |
| 2571 | free(srule); |
| 2572 | } |
| 2573 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2574 | list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) { |
| 2575 | LIST_DEL(&rule->list); |
Willy Tarreau | f51658d | 2014-04-23 01:21:56 +0200 | [diff] [blame] | 2576 | if (rule->cond) { |
| 2577 | prune_acl_cond(rule->cond); |
| 2578 | free(rule->cond); |
| 2579 | } |
Dragan Dosen | 2a7c20f | 2019-04-30 00:38:36 +0200 | [diff] [blame] | 2580 | free(rule->file); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2581 | free(rule); |
| 2582 | } |
| 2583 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2584 | list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) { |
| 2585 | LIST_DEL(&rdr->list); |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2586 | if (rdr->cond) { |
| 2587 | prune_acl_cond(rdr->cond); |
| 2588 | free(rdr->cond); |
| 2589 | } |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2590 | free(rdr->rdr_str); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2591 | list_for_each_entry_safe(lf, lfb, &rdr->rdr_fmt, list) { |
| 2592 | LIST_DEL(&lf->list); |
| 2593 | free(lf); |
| 2594 | } |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2595 | free(rdr); |
| 2596 | } |
| 2597 | |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 2598 | list_for_each_entry_safe(log, logb, &p->logsrvs, list) { |
| 2599 | LIST_DEL(&log->list); |
| 2600 | free(log); |
| 2601 | } |
| 2602 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 2603 | list_for_each_entry_safe(lf, lfb, &p->logformat, list) { |
| 2604 | LIST_DEL(&lf->list); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 2605 | release_sample_expr(lf->expr); |
| 2606 | free(lf->arg); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 2607 | free(lf); |
| 2608 | } |
| 2609 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2610 | list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) { |
| 2611 | LIST_DEL(&lf->list); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 2612 | release_sample_expr(lf->expr); |
| 2613 | free(lf->arg); |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2614 | free(lf); |
| 2615 | } |
| 2616 | |
Christopher Faulet | cb55013 | 2019-12-17 11:25:46 +0100 | [diff] [blame] | 2617 | deinit_act_rules(&p->tcp_req.inspect_rules); |
| 2618 | deinit_act_rules(&p->tcp_rep.inspect_rules); |
| 2619 | deinit_act_rules(&p->tcp_req.l4_rules); |
| 2620 | deinit_act_rules(&p->tcp_req.l5_rules); |
| 2621 | deinit_act_rules(&p->http_req_rules); |
| 2622 | deinit_act_rules(&p->http_res_rules); |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2623 | deinit_act_rules(&p->http_after_res_rules); |
Simon Horman | ac82142 | 2011-07-15 13:14:09 +0900 | [diff] [blame] | 2624 | |
Simon Horman | 6fb8259 | 2011-07-15 13:14:11 +0900 | [diff] [blame] | 2625 | deinit_stick_rules(&p->storersp_rules); |
| 2626 | deinit_stick_rules(&p->sticking_rules); |
| 2627 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2628 | h = p->req_cap; |
| 2629 | while (h) { |
| 2630 | h_next = h->next; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2631 | free(h->name); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2632 | pool_destroy(h->pool); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2633 | free(h); |
| 2634 | h = h_next; |
| 2635 | }/* end while(h) */ |
| 2636 | |
| 2637 | h = p->rsp_cap; |
| 2638 | while (h) { |
| 2639 | h_next = h->next; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2640 | free(h->name); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2641 | pool_destroy(h->pool); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2642 | free(h); |
| 2643 | h = h_next; |
| 2644 | }/* end while(h) */ |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2645 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2646 | s = p->srv; |
| 2647 | while (s) { |
| 2648 | s_next = s->next; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2649 | |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 2650 | task_destroy(s->check.task); |
| 2651 | task_destroy(s->agent.task); |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2652 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2653 | if (s->check.wait_list.tasklet) |
| 2654 | tasklet_free(s->check.wait_list.tasklet); |
| 2655 | if (s->agent.wait_list.tasklet) |
| 2656 | tasklet_free(s->agent.wait_list.tasklet); |
Dragan Dosen | 026ef57 | 2019-04-30 00:56:20 +0200 | [diff] [blame] | 2657 | |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 2658 | task_destroy(s->warmup); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 2659 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2660 | free(s->id); |
| 2661 | free(s->cookie); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2662 | free(s->check.bi.area); |
| 2663 | free(s->check.bo.area); |
| 2664 | free(s->agent.bi.area); |
| 2665 | free(s->agent.bo.area); |
James Brown | 55f9ff1 | 2015-10-21 18:19:05 -0700 | [diff] [blame] | 2666 | free(s->agent.send_string); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2667 | free(s->hostname_dn); |
Sárközi, László | 34c0179 | 2014-09-05 10:08:23 +0200 | [diff] [blame] | 2668 | free((char*)s->conf.file); |
Olivier Houchard | 7fc3be7 | 2018-11-22 18:50:54 +0100 | [diff] [blame] | 2669 | free(s->idle_conns); |
Olivier Houchard | 7fc3be7 | 2018-11-22 18:50:54 +0100 | [diff] [blame] | 2670 | free(s->safe_conns); |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 2671 | free(s->idle_orphan_conns); |
Olivier Houchard | f131481 | 2019-02-18 16:41:17 +0100 | [diff] [blame] | 2672 | free(s->curr_idle_thr); |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 2673 | |
| 2674 | if (s->use_ssl || s->check.use_ssl) { |
| 2675 | if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv) |
| 2676 | xprt_get(XPRT_SSL)->destroy_srv(s); |
| 2677 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2678 | HA_SPIN_DESTROY(&s->lock); |
Christopher Faulet | 3ea5cbe | 2019-07-31 08:44:12 +0200 | [diff] [blame] | 2679 | |
| 2680 | list_for_each_entry(srvdf, &server_deinit_list, list) |
| 2681 | srvdf->fct(s); |
| 2682 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2683 | free(s); |
| 2684 | s = s_next; |
| 2685 | }/* end while(s) */ |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2686 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2687 | 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] | 2688 | /* |
| 2689 | * Zombie proxy, the listener just pretend to be up |
| 2690 | * because they still hold an opened fd. |
| 2691 | * Close it and give the listener its real state. |
| 2692 | */ |
| 2693 | if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) { |
| 2694 | close(l->fd); |
| 2695 | l->state = LI_INIT; |
| 2696 | } |
Willy Tarreau | f6e2cc7 | 2010-09-03 10:38:17 +0200 | [diff] [blame] | 2697 | unbind_listener(l); |
| 2698 | delete_listener(l); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2699 | LIST_DEL(&l->by_fe); |
| 2700 | LIST_DEL(&l->by_bind); |
Krzysztof Piotr Oledzki | aff01ea | 2010-02-05 20:31:44 +0100 | [diff] [blame] | 2701 | free(l->name); |
| 2702 | free(l->counters); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2703 | free(l); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2704 | } |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2705 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2706 | /* Release unused SSL configs. */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2707 | 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] | 2708 | if (bind_conf->xprt->destroy_bind_conf) |
| 2709 | bind_conf->xprt->destroy_bind_conf(bind_conf); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2710 | free(bind_conf->file); |
| 2711 | free(bind_conf->arg); |
| 2712 | LIST_DEL(&bind_conf->by_fe); |
| 2713 | free(bind_conf); |
| 2714 | } |
Willy Tarreau | f5ae8f7 | 2012-09-07 16:58:00 +0200 | [diff] [blame] | 2715 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2716 | flt_deinit(p); |
| 2717 | |
Christopher Faulet | 3ea5cbe | 2019-07-31 08:44:12 +0200 | [diff] [blame] | 2718 | list_for_each_entry(pxdf, &proxy_deinit_list, list) |
| 2719 | pxdf->fct(p); |
| 2720 | |
Krzysztof Piotr Oledzki | aff01ea | 2010-02-05 20:31:44 +0100 | [diff] [blame] | 2721 | free(p->desc); |
| 2722 | free(p->fwdfor_hdr_name); |
| 2723 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2724 | task_destroy(p->task); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2725 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2726 | pool_destroy(p->req_cap_pool); |
| 2727 | pool_destroy(p->rsp_cap_pool); |
Dragan Dosen | 7d61a33 | 2019-05-07 14:16:18 +0200 | [diff] [blame] | 2728 | if (p->table) |
| 2729 | pool_destroy(p->table->pool); |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 2730 | |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 2731 | p0 = p; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2732 | p = p->next; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2733 | HA_SPIN_DESTROY(&p0->lbprm.lock); |
| 2734 | HA_SPIN_DESTROY(&p0->lock); |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 2735 | free(p0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2736 | }/* end while(p) */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 2737 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2738 | while (ua) { |
| 2739 | uap = ua; |
| 2740 | ua = ua->next; |
| 2741 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2742 | free(uap->uri_prefix); |
| 2743 | free(uap->auth_realm); |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 2744 | free(uap->node); |
| 2745 | free(uap->desc); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2746 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2747 | userlist_free(uap->userlist); |
Christopher Faulet | cb55013 | 2019-12-17 11:25:46 +0100 | [diff] [blame] | 2748 | deinit_act_rules(&uap->http_req_rules); |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2749 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2750 | free(uap); |
| 2751 | } |
| 2752 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 2753 | userlist_free(userlist); |
| 2754 | |
David Carlier | 834cb2e | 2015-09-25 12:02:25 +0100 | [diff] [blame] | 2755 | cfg_unregister_sections(); |
| 2756 | |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 2757 | deinit_log_buffers(); |
David Carlier | 834cb2e | 2015-09-25 12:02:25 +0100 | [diff] [blame] | 2758 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 2759 | protocol_unbind_all(); |
| 2760 | |
Willy Tarreau | 05554e6 | 2016-12-21 20:46:26 +0100 | [diff] [blame] | 2761 | list_for_each_entry(pdf, &post_deinit_list, list) |
| 2762 | pdf->fct(); |
| 2763 | |
Joe Williams | df5b38f | 2010-12-29 17:05:48 +0100 | [diff] [blame] | 2764 | free(global.log_send_hostname); global.log_send_hostname = NULL; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 2765 | chunk_destroy(&global.log_tag); |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2766 | free(global.chroot); global.chroot = NULL; |
| 2767 | free(global.pidfile); global.pidfile = NULL; |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 2768 | free(global.node); global.node = NULL; |
| 2769 | free(global.desc); global.desc = NULL; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2770 | free(oldpids); oldpids = NULL; |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2771 | task_destroy(idle_conn_task); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 2772 | idle_conn_task = NULL; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 2773 | |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 2774 | list_for_each_entry_safe(log, logb, &global.logsrvs, list) { |
| 2775 | LIST_DEL(&log->list); |
| 2776 | free(log); |
| 2777 | } |
Willy Tarreau | 477ecd8 | 2010-01-03 21:12:30 +0100 | [diff] [blame] | 2778 | list_for_each_entry_safe(wl, wlb, &cfg_cfgfiles, list) { |
Maxime de Roucy | 0f50392 | 2016-05-13 23:52:55 +0200 | [diff] [blame] | 2779 | free(wl->s); |
Willy Tarreau | 477ecd8 | 2010-01-03 21:12:30 +0100 | [diff] [blame] | 2780 | LIST_DEL(&wl->list); |
| 2781 | free(wl); |
| 2782 | } |
| 2783 | |
Willy Tarreau | cdb737e | 2016-12-21 18:43:10 +0100 | [diff] [blame] | 2784 | list_for_each_entry_safe(bol, bolb, &build_opts_list, list) { |
| 2785 | if (bol->must_free) |
| 2786 | free((void *)bol->str); |
| 2787 | LIST_DEL(&bol->list); |
| 2788 | free(bol); |
| 2789 | } |
| 2790 | |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 2791 | vars_prune(&global.vars, NULL, NULL); |
Willy Tarreau | 2455ceb | 2018-11-26 15:57:34 +0100 | [diff] [blame] | 2792 | pool_destroy_all(); |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 2793 | deinit_pollers(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2794 | } /* end deinit() */ |
| 2795 | |
William Lallemand | 7216032 | 2018-11-06 17:37:16 +0100 | [diff] [blame] | 2796 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 2797 | /* Runs the polling loop */ |
Willy Tarreau | 3ebd55e | 2020-03-03 14:59:56 +0100 | [diff] [blame] | 2798 | void run_poll_loop() |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2799 | { |
Willy Tarreau | 2ae84e4 | 2019-05-28 16:44:05 +0200 | [diff] [blame] | 2800 | int next, wake; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2801 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 2802 | tv_update_date(0,1); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2803 | while (1) { |
Willy Tarreau | c49ba52 | 2019-12-11 08:12:23 +0100 | [diff] [blame] | 2804 | wake_expired_tasks(); |
| 2805 | |
Thierry FOURNIER | 9cf7c4b | 2014-12-15 13:26:01 +0100 | [diff] [blame] | 2806 | /* Process a few tasks */ |
| 2807 | process_runnable_tasks(); |
| 2808 | |
William Lallemand | 1aab50b | 2018-06-07 09:46:01 +0200 | [diff] [blame] | 2809 | /* check if we caught some signals and process them in the |
| 2810 | first thread */ |
| 2811 | if (tid == 0) |
| 2812 | signal_process_queue(); |
Willy Tarreau | 2985794 | 2009-05-10 09:01:21 +0200 | [diff] [blame] | 2813 | |
Willy Tarreau | 4b3f27b | 2020-03-12 17:28:01 +0100 | [diff] [blame] | 2814 | if (stopping && tasks_run_queue == 0) |
| 2815 | _HA_ATOMIC_OR(&stopping_thread_mask, tid_bit); |
| 2816 | |
Willy Tarreau | 85c459d | 2018-08-02 10:54:31 +0200 | [diff] [blame] | 2817 | /* stop when there's nothing left to do */ |
Willy Tarreau | 4b3f27b | 2020-03-12 17:28:01 +0100 | [diff] [blame] | 2818 | if ((jobs - unstoppable_jobs) == 0 && tasks_run_queue == 0 && |
| 2819 | (stopping_thread_mask & all_threads_mask) == all_threads_mask) |
Willy Tarreau | 85c459d | 2018-08-02 10:54:31 +0200 | [diff] [blame] | 2820 | break; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2821 | |
Willy Tarreau | 7067b3a | 2019-06-02 11:11:29 +0200 | [diff] [blame] | 2822 | /* also stop if we failed to cleanly stop all tasks */ |
| 2823 | if (killed > 1) |
| 2824 | break; |
| 2825 | |
Willy Tarreau | 10146c9 | 2015-04-13 20:44:19 +0200 | [diff] [blame] | 2826 | /* expire immediately if events are pending */ |
Willy Tarreau | 2ae84e4 | 2019-05-28 16:44:05 +0200 | [diff] [blame] | 2827 | wake = 1; |
Olivier Houchard | 305d5ab | 2019-07-24 18:07:06 +0200 | [diff] [blame] | 2828 | if (thread_has_tasks()) |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 2829 | activity[tid].wake_tasks++; |
William Lallemand | 1aab50b | 2018-06-07 09:46:01 +0200 | [diff] [blame] | 2830 | else if (signal_queue_len && tid == 0) |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 2831 | activity[tid].wake_signal++; |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 2832 | else { |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 2833 | _HA_ATOMIC_OR(&sleeping_thread_mask, tid_bit); |
| 2834 | __ha_barrier_atomic_store(); |
Olivier Houchard | bba1a26 | 2019-09-24 14:55:28 +0200 | [diff] [blame] | 2835 | if ((global_tasks_mask & tid_bit) || thread_has_tasks()) { |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 2836 | activity[tid].wake_tasks++; |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 2837 | _HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit); |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 2838 | } else |
Willy Tarreau | 2ae84e4 | 2019-05-28 16:44:05 +0200 | [diff] [blame] | 2839 | wake = 0; |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 2840 | } |
Willy Tarreau | 10146c9 | 2015-04-13 20:44:19 +0200 | [diff] [blame] | 2841 | |
Willy Tarreau | c49ba52 | 2019-12-11 08:12:23 +0100 | [diff] [blame] | 2842 | /* If we have to sleep, measure how long */ |
| 2843 | next = wake ? TICK_ETERNITY : next_timer_expiry(); |
| 2844 | |
Willy Tarreau | 58b458d | 2008-06-29 22:40:23 +0200 | [diff] [blame] | 2845 | /* The poller will ensure it returns around <next> */ |
Willy Tarreau | 2ae84e4 | 2019-05-28 16:44:05 +0200 | [diff] [blame] | 2846 | cur_poller.poll(&cur_poller, next, wake); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 2847 | |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 2848 | activity[tid].loops++; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 2849 | } |
| 2850 | } |
| 2851 | |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2852 | static void *run_thread_poll_loop(void *data) |
| 2853 | { |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 2854 | struct per_thread_alloc_fct *ptaf; |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2855 | struct per_thread_init_fct *ptif; |
| 2856 | struct per_thread_deinit_fct *ptdf; |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 2857 | struct per_thread_free_fct *ptff; |
Willy Tarreau | 34a150c | 2019-06-11 09:16:41 +0200 | [diff] [blame] | 2858 | static int init_left = 0; |
| 2859 | __decl_hathreads(static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER); |
| 2860 | __decl_hathreads(static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER); |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2861 | |
Willy Tarreau | b4f7cc3 | 2019-05-03 09:27:30 +0200 | [diff] [blame] | 2862 | ha_set_tid((unsigned long)data); |
Willy Tarreau | d022e9c | 2019-09-24 08:25:15 +0200 | [diff] [blame] | 2863 | sched = &task_per_thread[tid]; |
Willy Tarreau | 91e6df0 | 2019-05-03 17:21:18 +0200 | [diff] [blame] | 2864 | |
Willy Tarreau | f617824 | 2019-05-21 19:46:58 +0200 | [diff] [blame] | 2865 | #if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) |
Willy Tarreau | 91e6df0 | 2019-05-03 17:21:18 +0200 | [diff] [blame] | 2866 | #ifdef USE_THREAD |
Willy Tarreau | 8323a37 | 2019-05-20 18:57:53 +0200 | [diff] [blame] | 2867 | pthread_getcpuclockid(pthread_self(), &ti->clock_id); |
Willy Tarreau | 624dcbf | 2019-05-20 20:23:06 +0200 | [diff] [blame] | 2868 | #else |
Willy Tarreau | 8323a37 | 2019-05-20 18:57:53 +0200 | [diff] [blame] | 2869 | ti->clock_id = CLOCK_THREAD_CPUTIME_ID; |
Willy Tarreau | 91e6df0 | 2019-05-03 17:21:18 +0200 | [diff] [blame] | 2870 | #endif |
Willy Tarreau | 663fda4 | 2019-05-21 15:14:08 +0200 | [diff] [blame] | 2871 | #endif |
Willy Tarreau | 6ec902a | 2019-06-07 14:41:11 +0200 | [diff] [blame] | 2872 | /* Now, initialize one thread init at a time. This is better since |
| 2873 | * some init code is a bit tricky and may release global resources |
| 2874 | * after reallocating them locally. This will also ensure there is |
| 2875 | * no race on file descriptors allocation. |
| 2876 | */ |
Willy Tarreau | 34a150c | 2019-06-11 09:16:41 +0200 | [diff] [blame] | 2877 | #ifdef USE_THREAD |
| 2878 | pthread_mutex_lock(&init_mutex); |
| 2879 | #endif |
| 2880 | /* The first thread must set the number of threads left */ |
| 2881 | if (!init_left) |
| 2882 | init_left = global.nbthread; |
| 2883 | init_left--; |
Willy Tarreau | 91e6df0 | 2019-05-03 17:21:18 +0200 | [diff] [blame] | 2884 | |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2885 | tv_update_date(-1,-1); |
| 2886 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 2887 | /* per-thread alloc calls performed here are not allowed to snoop on |
| 2888 | * other threads, so they are free to initialize at their own rhythm |
| 2889 | * as long as they act as if they were alone. None of them may rely |
| 2890 | * on resources initialized by the other ones. |
| 2891 | */ |
| 2892 | list_for_each_entry(ptaf, &per_thread_alloc_list, list) { |
| 2893 | if (!ptaf->fct()) { |
| 2894 | ha_alert("failed to allocate resources for thread %u.\n", tid); |
| 2895 | exit(1); |
| 2896 | } |
| 2897 | } |
| 2898 | |
Willy Tarreau | 3078e9f | 2019-05-20 10:50:43 +0200 | [diff] [blame] | 2899 | /* per-thread init calls performed here are not allowed to snoop on |
| 2900 | * other threads, so they are free to initialize at their own rhythm |
| 2901 | * as long as they act as if they were alone. |
| 2902 | */ |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2903 | list_for_each_entry(ptif, &per_thread_init_list, list) { |
| 2904 | if (!ptif->fct()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2905 | ha_alert("failed to initialize thread %u.\n", tid); |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2906 | exit(1); |
| 2907 | } |
| 2908 | } |
| 2909 | |
Willy Tarreau | 7109282 | 2019-06-10 09:51:04 +0200 | [diff] [blame] | 2910 | /* enabling protocols will result in fd_insert() calls to be performed, |
| 2911 | * we want all threads to have already allocated their local fd tables |
Willy Tarreau | 34a150c | 2019-06-11 09:16:41 +0200 | [diff] [blame] | 2912 | * before doing so, thus only the last thread does it. |
Willy Tarreau | 7109282 | 2019-06-10 09:51:04 +0200 | [diff] [blame] | 2913 | */ |
Willy Tarreau | 34a150c | 2019-06-11 09:16:41 +0200 | [diff] [blame] | 2914 | if (init_left == 0) |
Willy Tarreau | e4d7c9d | 2019-06-10 10:14:52 +0200 | [diff] [blame] | 2915 | protocol_enable_all(); |
Willy Tarreau | 6ec902a | 2019-06-07 14:41:11 +0200 | [diff] [blame] | 2916 | |
Willy Tarreau | 34a150c | 2019-06-11 09:16:41 +0200 | [diff] [blame] | 2917 | #ifdef USE_THREAD |
| 2918 | pthread_cond_broadcast(&init_cond); |
| 2919 | pthread_mutex_unlock(&init_mutex); |
| 2920 | |
| 2921 | /* now wait for other threads to finish starting */ |
| 2922 | pthread_mutex_lock(&init_mutex); |
| 2923 | while (init_left) |
| 2924 | pthread_cond_wait(&init_cond, &init_mutex); |
| 2925 | pthread_mutex_unlock(&init_mutex); |
| 2926 | #endif |
Willy Tarreau | 3078e9f | 2019-05-20 10:50:43 +0200 | [diff] [blame] | 2927 | |
Willy Tarreau | a45a8b5 | 2019-12-06 16:31:45 +0100 | [diff] [blame] | 2928 | #if defined(PR_SET_NO_NEW_PRIVS) && defined(USE_PRCTL) |
| 2929 | /* Let's refrain from using setuid executables. This way the impact of |
| 2930 | * an eventual vulnerability in a library remains limited. It may |
| 2931 | * impact external checks but who cares about them anyway ? In the |
| 2932 | * worst case it's possible to disable the option. Obviously we do this |
| 2933 | * in workers only. We can't hard-fail on this one as it really is |
| 2934 | * implementation dependent though we're interested in feedback, hence |
| 2935 | * the warning. |
| 2936 | */ |
| 2937 | if (!(global.tune.options & GTUNE_INSECURE_SETUID) && !master) { |
| 2938 | static int warn_fail; |
| 2939 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1 && !_HA_ATOMIC_XADD(&warn_fail, 1)) { |
| 2940 | ha_warning("Failed to disable setuid, please report to developers with detailed " |
| 2941 | "information about your operating system. You can silence this warning " |
| 2942 | "by adding 'insecure-setuid-wanted' in the 'global' section.\n"); |
| 2943 | } |
| 2944 | } |
| 2945 | #endif |
| 2946 | |
Willy Tarreau | d96f112 | 2019-12-03 07:07:36 +0100 | [diff] [blame] | 2947 | #if defined(RLIMIT_NPROC) |
| 2948 | /* all threads have started, it's now time to prevent any new thread |
| 2949 | * or process from starting. Obviously we do this in workers only. We |
| 2950 | * can't hard-fail on this one as it really is implementation dependent |
| 2951 | * though we're interested in feedback, hence the warning. |
| 2952 | */ |
| 2953 | if (!(global.tune.options & GTUNE_INSECURE_FORK) && !master) { |
| 2954 | struct rlimit limit = { .rlim_cur = 0, .rlim_max = 0 }; |
| 2955 | static int warn_fail; |
| 2956 | |
| 2957 | if (setrlimit(RLIMIT_NPROC, &limit) == -1 && !_HA_ATOMIC_XADD(&warn_fail, 1)) { |
| 2958 | ha_warning("Failed to disable forks, please report to developers with detailed " |
| 2959 | "information about your operating system. You can silence this warning " |
| 2960 | "by adding 'insecure-fork-wanted' in the 'global' section.\n"); |
| 2961 | } |
| 2962 | } |
| 2963 | #endif |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2964 | run_poll_loop(); |
| 2965 | |
| 2966 | list_for_each_entry(ptdf, &per_thread_deinit_list, list) |
| 2967 | ptdf->fct(); |
| 2968 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 2969 | list_for_each_entry(ptff, &per_thread_free_list, list) |
| 2970 | ptff->fct(); |
| 2971 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2972 | #ifdef USE_THREAD |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 2973 | _HA_ATOMIC_AND(&all_threads_mask, ~tid_bit); |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2974 | if (tid > 0) |
| 2975 | pthread_exit(NULL); |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2976 | #endif |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 2977 | return NULL; |
| 2978 | } |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 2979 | |
William Dauchy | f9af9d7 | 2019-11-17 15:47:16 +0100 | [diff] [blame] | 2980 | /* set uid/gid depending on global settings */ |
| 2981 | static void set_identity(const char *program_name) |
| 2982 | { |
| 2983 | if (global.gid) { |
| 2984 | if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1) |
| 2985 | ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'" |
| 2986 | " without 'uid'/'user' is generally useless.\n", program_name); |
| 2987 | |
| 2988 | if (setgid(global.gid) == -1) { |
| 2989 | ha_alert("[%s.main()] Cannot set gid %d.\n", program_name, global.gid); |
| 2990 | protocol_unbind_all(); |
| 2991 | exit(1); |
| 2992 | } |
| 2993 | } |
| 2994 | |
| 2995 | if (global.uid && setuid(global.uid) == -1) { |
| 2996 | ha_alert("[%s.main()] Cannot set uid %d.\n", program_name, global.uid); |
| 2997 | protocol_unbind_all(); |
| 2998 | exit(1); |
| 2999 | } |
| 3000 | } |
| 3001 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3002 | int main(int argc, char **argv) |
| 3003 | { |
| 3004 | int err, retry; |
| 3005 | struct rlimit limit; |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 3006 | char errmsg[100]; |
Willy Tarreau | 269ab31 | 2012-09-05 08:02:48 +0200 | [diff] [blame] | 3007 | int pidfd = -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3008 | |
Olivier Houchard | 5fa300d | 2018-02-03 15:15:21 +0100 | [diff] [blame] | 3009 | setvbuf(stdout, NULL, _IONBF, 0); |
Willy Tarreau | 5794fb0 | 2018-11-25 18:43:29 +0100 | [diff] [blame] | 3010 | |
Willy Tarreau | ff9c914 | 2019-02-07 10:39:36 +0100 | [diff] [blame] | 3011 | /* this can only safely be done here, though it's optimized away by |
| 3012 | * the compiler. |
| 3013 | */ |
| 3014 | if (MAX_PROCS < 1 || MAX_PROCS > LONGBITS) { |
| 3015 | ha_alert("MAX_PROCS value must be between 1 and %d inclusive; " |
| 3016 | "HAProxy was built with value %d, please fix it and rebuild.\n", |
| 3017 | LONGBITS, MAX_PROCS); |
| 3018 | exit(1); |
| 3019 | } |
| 3020 | |
Willy Tarreau | bf69640 | 2019-03-01 10:09:28 +0100 | [diff] [blame] | 3021 | /* take a copy of initial limits before we possibly change them */ |
| 3022 | getrlimit(RLIMIT_NOFILE, &limit); |
| 3023 | rlim_fd_cur_at_boot = limit.rlim_cur; |
| 3024 | rlim_fd_max_at_boot = limit.rlim_max; |
| 3025 | |
Willy Tarreau | 5794fb0 | 2018-11-25 18:43:29 +0100 | [diff] [blame] | 3026 | /* process all initcalls in order of potential dependency */ |
| 3027 | RUN_INITCALLS(STG_PREPARE); |
| 3028 | RUN_INITCALLS(STG_LOCK); |
| 3029 | RUN_INITCALLS(STG_ALLOC); |
| 3030 | RUN_INITCALLS(STG_POOL); |
| 3031 | RUN_INITCALLS(STG_REGISTER); |
| 3032 | RUN_INITCALLS(STG_INIT); |
| 3033 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 3034 | init(argc, argv); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 3035 | signal_register_fct(SIGQUIT, dump, SIGQUIT); |
| 3036 | signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1); |
| 3037 | signal_register_fct(SIGHUP, sig_dump_state, SIGHUP); |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 3038 | signal_register_fct(SIGUSR2, NULL, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3039 | |
Willy Tarreau | e437c44 | 2010-03-17 18:02:46 +0100 | [diff] [blame] | 3040 | /* Always catch SIGPIPE even on platforms which define MSG_NOSIGNAL. |
| 3041 | * Some recent FreeBSD setups report broken pipes, and MSG_NOSIGNAL |
| 3042 | * was defined there, so let's stay on the safe side. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3043 | */ |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 3044 | signal_register_fct(SIGPIPE, NULL, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3045 | |
Willy Tarreau | dc23a92 | 2011-02-16 11:10:36 +0100 | [diff] [blame] | 3046 | /* ulimits */ |
| 3047 | if (!global.rlimit_nofile) |
| 3048 | global.rlimit_nofile = global.maxsock; |
| 3049 | |
| 3050 | if (global.rlimit_nofile) { |
Willy Tarreau | e5cfdac | 2019-03-01 10:32:05 +0100 | [diff] [blame] | 3051 | limit.rlim_cur = global.rlimit_nofile; |
| 3052 | limit.rlim_max = MAX(rlim_fd_max_at_boot, limit.rlim_cur); |
| 3053 | |
Willy Tarreau | dc23a92 | 2011-02-16 11:10:36 +0100 | [diff] [blame] | 3054 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
Willy Tarreau | ef63547 | 2016-06-21 11:48:18 +0200 | [diff] [blame] | 3055 | getrlimit(RLIMIT_NOFILE, &limit); |
William Dauchy | 0fec3ab | 2019-10-27 20:08:11 +0100 | [diff] [blame] | 3056 | if (global.tune.options & GTUNE_STRICT_LIMITS) { |
| 3057 | ha_alert("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n", |
| 3058 | argv[0], global.rlimit_nofile, (int)limit.rlim_cur); |
| 3059 | if (!(global.mode & MODE_MWORKER)) |
| 3060 | exit(1); |
| 3061 | } |
| 3062 | else { |
| 3063 | /* try to set it to the max possible at least */ |
| 3064 | limit.rlim_cur = limit.rlim_max; |
| 3065 | if (setrlimit(RLIMIT_NOFILE, &limit) != -1) |
| 3066 | getrlimit(RLIMIT_NOFILE, &limit); |
Willy Tarreau | 164dd0b | 2016-06-21 11:51:59 +0200 | [diff] [blame] | 3067 | |
William Dauchy | 0fec3ab | 2019-10-27 20:08:11 +0100 | [diff] [blame] | 3068 | ha_warning("[%s.main()] Cannot raise FD limit to %d, limit is %d. " |
| 3069 | "This will fail in >= v2.3\n", |
| 3070 | argv[0], global.rlimit_nofile, (int)limit.rlim_cur); |
| 3071 | global.rlimit_nofile = limit.rlim_cur; |
| 3072 | } |
Willy Tarreau | dc23a92 | 2011-02-16 11:10:36 +0100 | [diff] [blame] | 3073 | } |
| 3074 | } |
| 3075 | |
| 3076 | if (global.rlimit_memmax) { |
| 3077 | limit.rlim_cur = limit.rlim_max = |
Willy Tarreau | 7006045 | 2015-12-14 12:46:07 +0100 | [diff] [blame] | 3078 | global.rlimit_memmax * 1048576ULL; |
Willy Tarreau | dc23a92 | 2011-02-16 11:10:36 +0100 | [diff] [blame] | 3079 | #ifdef RLIMIT_AS |
| 3080 | if (setrlimit(RLIMIT_AS, &limit) == -1) { |
William Dauchy | 0fec3ab | 2019-10-27 20:08:11 +0100 | [diff] [blame] | 3081 | if (global.tune.options & GTUNE_STRICT_LIMITS) { |
| 3082 | ha_alert("[%s.main()] Cannot fix MEM limit to %d megs.\n", |
| 3083 | argv[0], global.rlimit_memmax); |
| 3084 | if (!(global.mode & MODE_MWORKER)) |
| 3085 | exit(1); |
| 3086 | } |
| 3087 | else |
| 3088 | ha_warning("[%s.main()] Cannot fix MEM limit to %d megs." |
| 3089 | "This will fail in >= v2.3\n", |
| 3090 | argv[0], global.rlimit_memmax); |
Willy Tarreau | dc23a92 | 2011-02-16 11:10:36 +0100 | [diff] [blame] | 3091 | } |
| 3092 | #else |
| 3093 | if (setrlimit(RLIMIT_DATA, &limit) == -1) { |
William Dauchy | 0fec3ab | 2019-10-27 20:08:11 +0100 | [diff] [blame] | 3094 | if (global.tune.options & GTUNE_STRICT_LIMITS) { |
| 3095 | ha_alert("[%s.main()] Cannot fix MEM limit to %d megs.\n", |
| 3096 | argv[0], global.rlimit_memmax); |
| 3097 | if (!(global.mode & MODE_MWORKER)) |
| 3098 | exit(1); |
| 3099 | } |
| 3100 | else |
| 3101 | ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.", |
| 3102 | "This will fail in >= v2.3\n", |
| 3103 | argv[0], global.rlimit_memmax); |
Willy Tarreau | dc23a92 | 2011-02-16 11:10:36 +0100 | [diff] [blame] | 3104 | } |
| 3105 | #endif |
| 3106 | } |
| 3107 | |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 3108 | if (old_unixsocket) { |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 3109 | if (strcmp("/dev/null", old_unixsocket) != 0) { |
| 3110 | if (get_old_sockets(old_unixsocket) != 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3111 | ha_alert("Failed to get the sockets from the old process!\n"); |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 3112 | if (!(global.mode & MODE_MWORKER)) |
| 3113 | exit(1); |
| 3114 | } |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 3115 | } |
| 3116 | } |
William Lallemand | 85b0bd9 | 2017-06-01 17:38:53 +0200 | [diff] [blame] | 3117 | get_cur_unixsocket(); |
| 3118 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3119 | /* We will loop at most 100 times with 10 ms delay each time. |
| 3120 | * That's at most 1 second. We only send a signal to old pids |
| 3121 | * if we cannot grab at least one port. |
| 3122 | */ |
| 3123 | retry = MAX_START_RETRIES; |
| 3124 | err = ERR_NONE; |
| 3125 | while (retry >= 0) { |
| 3126 | struct timeval w; |
| 3127 | err = start_proxies(retry == 0 || nb_oldpids == 0); |
Willy Tarreau | e13e925 | 2007-12-20 23:05:50 +0100 | [diff] [blame] | 3128 | /* exit the loop on no error or fatal error */ |
| 3129 | if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3130 | break; |
Willy Tarreau | bb545b4 | 2010-08-25 12:58:59 +0200 | [diff] [blame] | 3131 | if (nb_oldpids == 0 || retry == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3132 | break; |
| 3133 | |
| 3134 | /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on |
| 3135 | * listening sockets. So on those platforms, it would be wiser to |
| 3136 | * simply send SIGUSR1, which will not be undoable. |
| 3137 | */ |
Willy Tarreau | bb545b4 | 2010-08-25 12:58:59 +0200 | [diff] [blame] | 3138 | if (tell_old_pids(SIGTTOU) == 0) { |
| 3139 | /* no need to wait if we can't contact old pids */ |
| 3140 | retry = 0; |
| 3141 | continue; |
| 3142 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3143 | /* give some time to old processes to stop listening */ |
| 3144 | w.tv_sec = 0; |
| 3145 | w.tv_usec = 10*1000; |
| 3146 | select(0, NULL, NULL, NULL, &w); |
| 3147 | retry--; |
| 3148 | } |
| 3149 | |
| 3150 | /* Note: start_proxies() sends an alert when it fails. */ |
Willy Tarreau | 0a3b9d9 | 2009-02-04 17:05:23 +0100 | [diff] [blame] | 3151 | if ((err & ~ERR_WARN) != ERR_NONE) { |
Willy Tarreau | f68da46 | 2009-06-09 14:36:00 +0200 | [diff] [blame] | 3152 | if (retry != MAX_START_RETRIES && nb_oldpids) { |
| 3153 | protocol_unbind_all(); /* cleanup everything we can */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3154 | tell_old_pids(SIGTTIN); |
Willy Tarreau | f68da46 | 2009-06-09 14:36:00 +0200 | [diff] [blame] | 3155 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3156 | exit(1); |
| 3157 | } |
| 3158 | |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 3159 | if (!(global.mode & MODE_MWORKER_WAIT) && listeners == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3160 | ha_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] | 3161 | /* Note: we don't have to send anything to the old pids because we |
| 3162 | * never stopped them. */ |
| 3163 | exit(1); |
| 3164 | } |
| 3165 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 3166 | err = protocol_bind_all(errmsg, sizeof(errmsg)); |
| 3167 | if ((err & ~ERR_WARN) != ERR_NONE) { |
| 3168 | if ((err & ERR_ALERT) || (err & ERR_WARN)) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3169 | ha_alert("[%s.main()] %s.\n", argv[0], errmsg); |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 3170 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3171 | ha_alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 3172 | protocol_unbind_all(); /* cleanup everything we can */ |
| 3173 | if (nb_oldpids) |
| 3174 | tell_old_pids(SIGTTIN); |
| 3175 | exit(1); |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 3176 | } else if (err & ERR_WARN) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3177 | ha_alert("[%s.main()] %s.\n", argv[0], errmsg); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 3178 | } |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 3179 | /* Ok, all listener should now be bound, close any leftover sockets |
| 3180 | * the previous process gave us, we don't need them anymore |
| 3181 | */ |
| 3182 | while (xfer_sock_list != NULL) { |
| 3183 | struct xfer_sock_list *tmpxfer = xfer_sock_list->next; |
| 3184 | close(xfer_sock_list->fd); |
| 3185 | free(xfer_sock_list->iface); |
| 3186 | free(xfer_sock_list->namespace); |
| 3187 | free(xfer_sock_list); |
| 3188 | xfer_sock_list = tmpxfer; |
| 3189 | } |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 3190 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3191 | /* prepare pause/play signals */ |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 3192 | signal_register_fct(SIGTTOU, sig_pause, SIGTTOU); |
| 3193 | signal_register_fct(SIGTTIN, sig_listen, SIGTTIN); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3194 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3195 | /* MODE_QUIET can inhibit alerts and warnings below this line */ |
| 3196 | |
PiBa-NL | 149a81a | 2017-12-25 21:03:31 +0100 | [diff] [blame] | 3197 | if (getenv("HAPROXY_MWORKER_REEXEC") != NULL) { |
| 3198 | /* either stdin/out/err are already closed or should stay as they are. */ |
| 3199 | if ((global.mode & MODE_DAEMON)) { |
| 3200 | /* daemon mode re-executing, stdin/stdout/stderr are already closed so keep quiet */ |
| 3201 | global.mode &= ~MODE_VERBOSE; |
| 3202 | global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ |
| 3203 | } |
| 3204 | } else { |
| 3205 | if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) { |
| 3206 | /* detach from the tty */ |
William Lallemand | e134041 | 2017-12-28 16:09:36 +0100 | [diff] [blame] | 3207 | stdio_quiet(-1); |
PiBa-NL | 149a81a | 2017-12-25 21:03:31 +0100 | [diff] [blame] | 3208 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | /* open log & pid files before the chroot */ |
William Lallemand | 8029300 | 2017-11-06 11:00:03 +0100 | [diff] [blame] | 3212 | if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && global.pidfile != NULL) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3213 | unlink(global.pidfile); |
| 3214 | pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 3215 | if (pidfd < 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3216 | ha_alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3217 | if (nb_oldpids) |
| 3218 | tell_old_pids(SIGTTIN); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 3219 | protocol_unbind_all(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3220 | exit(1); |
| 3221 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3222 | } |
| 3223 | |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 3224 | if ((global.last_checks & LSTCHK_NETADM) && global.uid) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3225 | ha_alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n" |
| 3226 | "", argv[0]); |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 3227 | protocol_unbind_all(); |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 3228 | exit(1); |
| 3229 | } |
| 3230 | |
Willy Tarreau | 4e30ed7 | 2009-02-04 18:02:48 +0100 | [diff] [blame] | 3231 | /* If the user is not root, we'll still let him try the configuration |
| 3232 | * but we inform him that unexpected behaviour may occur. |
| 3233 | */ |
| 3234 | if ((global.last_checks & LSTCHK_NETADM) && getuid()) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3235 | ha_warning("[%s.main()] Some options which require full privileges" |
| 3236 | " might not work well.\n" |
| 3237 | "", argv[0]); |
Willy Tarreau | 4e30ed7 | 2009-02-04 18:02:48 +0100 | [diff] [blame] | 3238 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 3239 | if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) { |
| 3240 | |
| 3241 | /* chroot if needed */ |
| 3242 | if (global.chroot != NULL) { |
| 3243 | if (chroot(global.chroot) == -1 || chdir("/") == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3244 | ha_alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot); |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 3245 | if (nb_oldpids) |
| 3246 | tell_old_pids(SIGTTIN); |
| 3247 | protocol_unbind_all(); |
| 3248 | exit(1); |
| 3249 | } |
Willy Tarreau | f223cc0 | 2007-10-15 18:57:08 +0200 | [diff] [blame] | 3250 | } |
Willy Tarreau | f223cc0 | 2007-10-15 18:57:08 +0200 | [diff] [blame] | 3251 | } |
| 3252 | |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 3253 | if (nb_oldpids && !(global.mode & MODE_MWORKER_WAIT)) |
Willy Tarreau | bb545b4 | 2010-08-25 12:58:59 +0200 | [diff] [blame] | 3254 | nb_oldpids = tell_old_pids(oldpids_sig); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3255 | |
William Lallemand | 27edc4b | 2019-05-07 17:49:33 +0200 | [diff] [blame] | 3256 | /* send a SIGTERM to workers who have a too high reloads number */ |
| 3257 | if ((global.mode & MODE_MWORKER) && !(global.mode & MODE_MWORKER_WAIT)) |
| 3258 | mworker_kill_max_reloads(SIGTERM); |
| 3259 | |
William Lallemand | 8a361b5 | 2017-06-20 11:20:33 +0200 | [diff] [blame] | 3260 | if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) { |
| 3261 | nb_oldpids = 0; |
| 3262 | free(oldpids); |
| 3263 | oldpids = NULL; |
| 3264 | } |
| 3265 | |
| 3266 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3267 | /* Note that any error at this stage will be fatal because we will not |
| 3268 | * be able to restart the old pids. |
| 3269 | */ |
| 3270 | |
William Dauchy | f9af9d7 | 2019-11-17 15:47:16 +0100 | [diff] [blame] | 3271 | if ((global.mode & (MODE_MWORKER | MODE_DAEMON)) == 0) |
| 3272 | set_identity(argv[0]); |
Willy Tarreau | 636848a | 2019-04-15 19:38:50 +0200 | [diff] [blame] | 3273 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3274 | /* check ulimits */ |
| 3275 | limit.rlim_cur = limit.rlim_max = 0; |
| 3276 | getrlimit(RLIMIT_NOFILE, &limit); |
| 3277 | if (limit.rlim_cur < global.maxsock) { |
William Dauchy | 0fec3ab | 2019-10-27 20:08:11 +0100 | [diff] [blame] | 3278 | if (global.tune.options & GTUNE_STRICT_LIMITS) { |
| 3279 | ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. " |
| 3280 | "Please raise 'ulimit-n' to %d or more to avoid any trouble.\n", |
| 3281 | argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, |
| 3282 | global.maxsock); |
| 3283 | if (!(global.mode & MODE_MWORKER)) |
| 3284 | exit(1); |
| 3285 | } |
| 3286 | else |
| 3287 | ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. " |
| 3288 | "Please raise 'ulimit-n' to %d or more to avoid any trouble." |
| 3289 | "This will fail in >= v2.3\n", |
| 3290 | argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, |
| 3291 | global.maxsock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3292 | } |
| 3293 | |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 3294 | if (global.mode & (MODE_DAEMON | MODE_MWORKER | MODE_MWORKER_WAIT)) { |
Willy Tarreau | 0b9c02c | 2009-02-04 22:05:05 +0100 | [diff] [blame] | 3295 | struct proxy *px; |
Willy Tarreau | f83d3fe | 2015-05-01 19:13:41 +0200 | [diff] [blame] | 3296 | struct peers *curpeers; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3297 | int ret = 0; |
| 3298 | int proc; |
William Lallemand | e134041 | 2017-12-28 16:09:36 +0100 | [diff] [blame] | 3299 | int devnullfd = -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3300 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 3301 | /* |
| 3302 | * if daemon + mworker: must fork here to let a master |
| 3303 | * process live in background before forking children |
| 3304 | */ |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 3305 | |
| 3306 | if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL) |
| 3307 | && (global.mode & MODE_MWORKER) |
| 3308 | && (global.mode & MODE_DAEMON)) { |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 3309 | ret = fork(); |
| 3310 | if (ret < 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3311 | ha_alert("[%s.main()] Cannot fork.\n", argv[0]); |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 3312 | protocol_unbind_all(); |
| 3313 | exit(1); /* there has been an error */ |
William Lallemand | bfd8eb5 | 2018-07-04 15:31:23 +0200 | [diff] [blame] | 3314 | } else if (ret > 0) { /* parent leave to daemonize */ |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 3315 | exit(0); |
William Lallemand | bfd8eb5 | 2018-07-04 15:31:23 +0200 | [diff] [blame] | 3316 | } else /* change the process group ID in the child (master process) */ |
| 3317 | setsid(); |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 3318 | } |
William Lallemand | e20b6a6 | 2017-06-01 17:38:55 +0200 | [diff] [blame] | 3319 | |
William Lallemand | e20b6a6 | 2017-06-01 17:38:55 +0200 | [diff] [blame] | 3320 | |
William Lallemand | deed780 | 2017-11-06 11:00:04 +0100 | [diff] [blame] | 3321 | /* if in master-worker mode, write the PID of the father */ |
| 3322 | if (global.mode & MODE_MWORKER) { |
| 3323 | char pidstr[100]; |
Willy Tarreau | 76a80c7 | 2019-06-22 07:41:38 +0200 | [diff] [blame] | 3324 | snprintf(pidstr, sizeof(pidstr), "%d\n", (int)getpid()); |
Willy Tarreau | 46ec48b | 2018-01-23 19:20:19 +0100 | [diff] [blame] | 3325 | if (pidfd >= 0) |
| 3326 | shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr))); |
William Lallemand | deed780 | 2017-11-06 11:00:04 +0100 | [diff] [blame] | 3327 | } |
| 3328 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3329 | /* the father launches the required number of processes */ |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 3330 | if (!(global.mode & MODE_MWORKER_WAIT)) { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 3331 | if (global.mode & MODE_MWORKER) |
| 3332 | mworker_ext_launch_all(); |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 3333 | for (proc = 0; proc < global.nbproc; proc++) { |
| 3334 | ret = fork(); |
| 3335 | if (ret < 0) { |
| 3336 | ha_alert("[%s.main()] Cannot fork.\n", argv[0]); |
| 3337 | protocol_unbind_all(); |
| 3338 | exit(1); /* there has been an error */ |
| 3339 | } |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 3340 | else if (ret == 0) { /* child breaks here */ |
| 3341 | ha_random_jump96(relative_pid); |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 3342 | break; |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 3343 | } |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 3344 | if (pidfd >= 0 && !(global.mode & MODE_MWORKER)) { |
| 3345 | char pidstr[100]; |
| 3346 | snprintf(pidstr, sizeof(pidstr), "%d\n", ret); |
| 3347 | shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr))); |
| 3348 | } |
| 3349 | if (global.mode & MODE_MWORKER) { |
| 3350 | struct mworker_proc *child; |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 3351 | |
William Lallemand | 220567e | 2018-11-21 18:04:53 +0100 | [diff] [blame] | 3352 | ha_notice("New worker #%d (%d) forked\n", relative_pid, ret); |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 3353 | /* find the right mworker_proc */ |
| 3354 | list_for_each_entry(child, &proc_list, list) { |
| 3355 | if (child->relative_pid == relative_pid && |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 3356 | child->reloads == 0 && child->options & PROC_O_TYPE_WORKER) { |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 3357 | child->timestamp = now.tv_sec; |
| 3358 | child->pid = ret; |
William Lallemand | 1dc6963 | 2019-06-12 19:11:33 +0200 | [diff] [blame] | 3359 | child->version = strdup(haproxy_version); |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 3360 | break; |
| 3361 | } |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 3362 | } |
| 3363 | } |
William Lallemand | bc19305 | 2018-09-11 10:06:26 +0200 | [diff] [blame] | 3364 | |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 3365 | relative_pid++; /* each child will get a different one */ |
| 3366 | pid_bit <<= 1; |
| 3367 | } |
| 3368 | } else { |
| 3369 | /* wait mode */ |
| 3370 | global.nbproc = 1; |
| 3371 | proc = 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3372 | } |
Willy Tarreau | fc6c032 | 2012-11-16 16:12:27 +0100 | [diff] [blame] | 3373 | |
| 3374 | #ifdef USE_CPU_AFFINITY |
| 3375 | if (proc < global.nbproc && /* child */ |
Willy Tarreau | ff9c914 | 2019-02-07 10:39:36 +0100 | [diff] [blame] | 3376 | proc < MAX_PROCS && /* only the first 32/64 processes may be pinned */ |
Christopher Faulet | cb6a945 | 2017-11-22 16:50:41 +0100 | [diff] [blame] | 3377 | global.cpu_map.proc[proc]) /* only do this if the process has a CPU map */ |
Pieter Baauw | caa6a1b | 2015-09-17 21:26:40 +0200 | [diff] [blame] | 3378 | #ifdef __FreeBSD__ |
Olivier Houchard | 97148f6 | 2017-08-16 17:29:11 +0200 | [diff] [blame] | 3379 | { |
| 3380 | cpuset_t cpuset; |
| 3381 | int i; |
Christopher Faulet | cb6a945 | 2017-11-22 16:50:41 +0100 | [diff] [blame] | 3382 | unsigned long cpu_map = global.cpu_map.proc[proc]; |
Olivier Houchard | 97148f6 | 2017-08-16 17:29:11 +0200 | [diff] [blame] | 3383 | |
| 3384 | CPU_ZERO(&cpuset); |
| 3385 | while ((i = ffsl(cpu_map)) > 0) { |
| 3386 | CPU_SET(i - 1, &cpuset); |
Cyril Bonté | d400ab3 | 2018-03-12 21:47:39 +0100 | [diff] [blame] | 3387 | cpu_map &= ~(1UL << (i - 1)); |
Olivier Houchard | 97148f6 | 2017-08-16 17:29:11 +0200 | [diff] [blame] | 3388 | } |
| 3389 | ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset); |
| 3390 | } |
David Carlier | 5e4c8e2 | 2019-09-13 05:12:58 +0100 | [diff] [blame] | 3391 | #elif defined(__linux__) |
Christopher Faulet | cb6a945 | 2017-11-22 16:50:41 +0100 | [diff] [blame] | 3392 | sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map.proc[proc]); |
Willy Tarreau | fc6c032 | 2012-11-16 16:12:27 +0100 | [diff] [blame] | 3393 | #endif |
Pieter Baauw | caa6a1b | 2015-09-17 21:26:40 +0200 | [diff] [blame] | 3394 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3395 | /* close the pidfile both in children and father */ |
Willy Tarreau | 269ab31 | 2012-09-05 08:02:48 +0200 | [diff] [blame] | 3396 | if (pidfd >= 0) { |
| 3397 | //lseek(pidfd, 0, SEEK_SET); /* debug: emulate eglibc bug */ |
| 3398 | close(pidfd); |
| 3399 | } |
Willy Tarreau | d137dd3 | 2010-08-25 12:49:05 +0200 | [diff] [blame] | 3400 | |
| 3401 | /* We won't ever use this anymore */ |
Willy Tarreau | d137dd3 | 2010-08-25 12:49:05 +0200 | [diff] [blame] | 3402 | free(global.pidfile); global.pidfile = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3403 | |
Willy Tarreau | edaff0a | 2015-05-01 17:01:08 +0200 | [diff] [blame] | 3404 | if (proc == global.nbproc) { |
William Lallemand | 944e619 | 2018-11-21 15:48:31 +0100 | [diff] [blame] | 3405 | if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) { |
PiBa-NL | baf6ea4 | 2017-11-28 23:26:08 +0100 | [diff] [blame] | 3406 | |
| 3407 | if ((!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 3408 | (global.mode & MODE_DAEMON)) { |
| 3409 | /* detach from the tty, this is required to properly daemonize. */ |
William Lallemand | e134041 | 2017-12-28 16:09:36 +0100 | [diff] [blame] | 3410 | if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) |
| 3411 | stdio_quiet(-1); |
| 3412 | |
PiBa-NL | baf6ea4 | 2017-11-28 23:26:08 +0100 | [diff] [blame] | 3413 | global.mode &= ~MODE_VERBOSE; |
| 3414 | global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ |
PiBa-NL | baf6ea4 | 2017-11-28 23:26:08 +0100 | [diff] [blame] | 3415 | } |
| 3416 | |
William Lallemand | b3f2be3 | 2018-09-11 10:06:18 +0200 | [diff] [blame] | 3417 | mworker_loop(); |
William Lallemand | 1499b9b | 2017-06-07 15:04:47 +0200 | [diff] [blame] | 3418 | /* should never get there */ |
| 3419 | exit(EXIT_FAILURE); |
Willy Tarreau | edaff0a | 2015-05-01 17:01:08 +0200 | [diff] [blame] | 3420 | } |
William Lallemand | cf4e496 | 2017-06-08 19:05:48 +0200 | [diff] [blame] | 3421 | #if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH) |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 3422 | ssl_free_dh(); |
| 3423 | #endif |
William Lallemand | 1499b9b | 2017-06-07 15:04:47 +0200 | [diff] [blame] | 3424 | exit(0); /* parent must leave */ |
Willy Tarreau | edaff0a | 2015-05-01 17:01:08 +0200 | [diff] [blame] | 3425 | } |
| 3426 | |
William Lallemand | cb11fd2 | 2017-06-01 17:38:52 +0200 | [diff] [blame] | 3427 | /* child must never use the atexit function */ |
| 3428 | atexit_flag = 0; |
| 3429 | |
William Lallemand | bc19305 | 2018-09-11 10:06:26 +0200 | [diff] [blame] | 3430 | /* close useless master sockets */ |
| 3431 | if (global.mode & MODE_MWORKER) { |
| 3432 | struct mworker_proc *child, *it; |
| 3433 | master = 0; |
| 3434 | |
William Lallemand | 309dc9a | 2018-10-26 14:47:45 +0200 | [diff] [blame] | 3435 | mworker_cli_proxy_stop(); |
| 3436 | |
William Lallemand | bc19305 | 2018-09-11 10:06:26 +0200 | [diff] [blame] | 3437 | /* free proc struct of other processes */ |
| 3438 | list_for_each_entry_safe(child, it, &proc_list, list) { |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 3439 | /* close the FD of the master side for all |
| 3440 | * workers, we don't need to close the worker |
| 3441 | * side of other workers since it's done with |
| 3442 | * the bind_proc */ |
Tim Duesterhus | 742e0f9 | 2018-11-25 20:03:39 +0100 | [diff] [blame] | 3443 | if (child->ipc_fd[0] >= 0) |
| 3444 | close(child->ipc_fd[0]); |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 3445 | if (child->relative_pid == relative_pid && |
| 3446 | child->reloads == 0) { |
| 3447 | /* keep this struct if this is our pid */ |
| 3448 | proc_self = child; |
William Lallemand | bc19305 | 2018-09-11 10:06:26 +0200 | [diff] [blame] | 3449 | continue; |
William Lallemand | ce83b4a | 2018-10-26 14:47:30 +0200 | [diff] [blame] | 3450 | } |
William Lallemand | bc19305 | 2018-09-11 10:06:26 +0200 | [diff] [blame] | 3451 | LIST_DEL(&child->list); |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 3452 | mworker_free_child(child); |
| 3453 | child = NULL; |
William Lallemand | bc19305 | 2018-09-11 10:06:26 +0200 | [diff] [blame] | 3454 | } |
| 3455 | } |
Willy Tarreau | 1605c7a | 2018-01-23 19:01:49 +0100 | [diff] [blame] | 3456 | |
William Lallemand | e134041 | 2017-12-28 16:09:36 +0100 | [diff] [blame] | 3457 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 3458 | devnullfd = open("/dev/null", O_RDWR, 0); |
| 3459 | if (devnullfd < 0) { |
| 3460 | ha_alert("Cannot open /dev/null\n"); |
| 3461 | exit(EXIT_FAILURE); |
| 3462 | } |
| 3463 | } |
| 3464 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 3465 | /* Must chroot and setgid/setuid in the children */ |
| 3466 | /* chroot if needed */ |
| 3467 | if (global.chroot != NULL) { |
| 3468 | if (chroot(global.chroot) == -1 || chdir("/") == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3469 | ha_alert("[%s.main()] Cannot chroot1(%s).\n", argv[0], global.chroot); |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 3470 | if (nb_oldpids) |
| 3471 | tell_old_pids(SIGTTIN); |
| 3472 | protocol_unbind_all(); |
| 3473 | exit(1); |
| 3474 | } |
| 3475 | } |
| 3476 | |
| 3477 | free(global.chroot); |
| 3478 | global.chroot = NULL; |
William Dauchy | f9af9d7 | 2019-11-17 15:47:16 +0100 | [diff] [blame] | 3479 | set_identity(argv[0]); |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 3480 | |
William Lallemand | 7f80eb2 | 2017-05-26 18:19:55 +0200 | [diff] [blame] | 3481 | /* pass through every cli socket, and check if it's bound to |
| 3482 | * the current process and if it exposes listeners sockets. |
| 3483 | * Caution: the GTUNE_SOCKET_TRANSFER is now set after the fork. |
| 3484 | * */ |
| 3485 | |
| 3486 | if (global.stats_fe) { |
| 3487 | struct bind_conf *bind_conf; |
| 3488 | |
| 3489 | list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) { |
| 3490 | if (bind_conf->level & ACCESS_FD_LISTENERS) { |
| 3491 | if (!bind_conf->bind_proc || bind_conf->bind_proc & (1UL << proc)) { |
| 3492 | global.tune.options |= GTUNE_SOCKET_TRANSFER; |
| 3493 | break; |
| 3494 | } |
| 3495 | } |
| 3496 | } |
| 3497 | } |
| 3498 | |
Willy Tarreau | 0b9c02c | 2009-02-04 22:05:05 +0100 | [diff] [blame] | 3499 | /* we might have to unbind some proxies from some processes */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 3500 | px = proxies_list; |
Willy Tarreau | 0b9c02c | 2009-02-04 22:05:05 +0100 | [diff] [blame] | 3501 | while (px != NULL) { |
| 3502 | if (px->bind_proc && px->state != PR_STSTOPPED) { |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 3503 | if (!(px->bind_proc & (1UL << proc))) { |
| 3504 | if (global.tune.options & GTUNE_SOCKET_TRANSFER) |
| 3505 | zombify_proxy(px); |
| 3506 | else |
| 3507 | stop_proxy(px); |
| 3508 | } |
Willy Tarreau | 0b9c02c | 2009-02-04 22:05:05 +0100 | [diff] [blame] | 3509 | } |
| 3510 | px = px->next; |
| 3511 | } |
| 3512 | |
Willy Tarreau | f83d3fe | 2015-05-01 19:13:41 +0200 | [diff] [blame] | 3513 | /* 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] | 3514 | for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) { |
Willy Tarreau | f83d3fe | 2015-05-01 19:13:41 +0200 | [diff] [blame] | 3515 | if (!curpeers->peers_fe) |
| 3516 | continue; |
| 3517 | |
| 3518 | if (curpeers->peers_fe->bind_proc & (1UL << proc)) |
| 3519 | continue; |
| 3520 | |
| 3521 | stop_proxy(curpeers->peers_fe); |
| 3522 | /* disable this peer section so that it kills itself */ |
Willy Tarreau | 47c8c02 | 2015-09-28 16:39:25 +0200 | [diff] [blame] | 3523 | signal_unregister_handler(curpeers->sighandler); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3524 | task_destroy(curpeers->sync_task); |
Willy Tarreau | 47c8c02 | 2015-09-28 16:39:25 +0200 | [diff] [blame] | 3525 | curpeers->sync_task = NULL; |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3526 | task_destroy(curpeers->peers_fe->task); |
Willy Tarreau | 47c8c02 | 2015-09-28 16:39:25 +0200 | [diff] [blame] | 3527 | curpeers->peers_fe->task = NULL; |
Willy Tarreau | f83d3fe | 2015-05-01 19:13:41 +0200 | [diff] [blame] | 3528 | curpeers->peers_fe = NULL; |
| 3529 | } |
| 3530 | |
William Lallemand | 2e8fad9 | 2018-11-13 16:18:23 +0100 | [diff] [blame] | 3531 | /* |
| 3532 | * This is only done in daemon mode because we might want the |
| 3533 | * logs on stdout in mworker mode. If we're NOT in QUIET mode, |
| 3534 | * we should now close the 3 first FDs to ensure that we can |
| 3535 | * detach from the TTY. We MUST NOT do it in other cases since |
| 3536 | * it would have already be done, and 0-2 would have been |
| 3537 | * affected to listening sockets |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3538 | */ |
William Lallemand | 2e8fad9 | 2018-11-13 16:18:23 +0100 | [diff] [blame] | 3539 | if ((global.mode & MODE_DAEMON) && |
| 3540 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3541 | /* detach from the tty */ |
William Lallemand | e134041 | 2017-12-28 16:09:36 +0100 | [diff] [blame] | 3542 | stdio_quiet(devnullfd); |
Willy Tarreau | 106cb76 | 2008-11-16 07:40:34 +0100 | [diff] [blame] | 3543 | global.mode &= ~MODE_VERBOSE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3544 | global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ |
| 3545 | } |
| 3546 | pid = getpid(); /* update child's pid */ |
William Lallemand | bfd8eb5 | 2018-07-04 15:31:23 +0200 | [diff] [blame] | 3547 | if (!(global.mode & MODE_MWORKER)) /* in mworker mode we don't want a new pgid for the children */ |
| 3548 | setsid(); |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 3549 | fork_poller(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3550 | } |
| 3551 | |
William Dauchy | e039f26 | 2019-11-17 15:47:15 +0100 | [diff] [blame] | 3552 | /* try our best to re-enable core dumps depending on system capabilities. |
| 3553 | * What is addressed here : |
| 3554 | * - remove file size limits |
| 3555 | * - remove core size limits |
| 3556 | * - mark the process dumpable again if it lost it due to user/group |
| 3557 | */ |
| 3558 | if (global.tune.options & GTUNE_SET_DUMPABLE) { |
| 3559 | limit.rlim_cur = limit.rlim_max = RLIM_INFINITY; |
| 3560 | |
| 3561 | #if defined(RLIMIT_FSIZE) |
| 3562 | if (setrlimit(RLIMIT_FSIZE, &limit) == -1) { |
| 3563 | if (global.tune.options & GTUNE_STRICT_LIMITS) { |
| 3564 | ha_alert("[%s.main()] Failed to set the raise the maximum " |
| 3565 | "file size.\n", argv[0]); |
| 3566 | if (!(global.mode & MODE_MWORKER)) |
| 3567 | exit(1); |
| 3568 | } |
| 3569 | else |
| 3570 | ha_warning("[%s.main()] Failed to set the raise the maximum " |
| 3571 | "file size. This will fail in >= v2.3\n", argv[0]); |
| 3572 | } |
| 3573 | #endif |
| 3574 | |
| 3575 | #if defined(RLIMIT_CORE) |
| 3576 | if (setrlimit(RLIMIT_CORE, &limit) == -1) { |
| 3577 | if (global.tune.options & GTUNE_STRICT_LIMITS) { |
| 3578 | ha_alert("[%s.main()] Failed to set the raise the core " |
| 3579 | "dump size.\n", argv[0]); |
| 3580 | if (!(global.mode & MODE_MWORKER)) |
| 3581 | exit(1); |
| 3582 | } |
| 3583 | else |
| 3584 | ha_warning("[%s.main()] Failed to set the raise the core " |
| 3585 | "dump size. This will fail in >= v2.3\n", argv[0]); |
| 3586 | } |
| 3587 | #endif |
| 3588 | |
| 3589 | #if defined(USE_PRCTL) |
| 3590 | if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) |
| 3591 | ha_warning("[%s.main()] Failed to set the dumpable flag, " |
| 3592 | "no core will be dumped.\n", argv[0]); |
| 3593 | #endif |
| 3594 | } |
| 3595 | |
Christopher Faulet | e3a5e35 | 2017-10-24 13:53:54 +0200 | [diff] [blame] | 3596 | global.mode &= ~MODE_STARTING; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 3597 | /* |
| 3598 | * That's it : the central polling loop. Run until we stop. |
| 3599 | */ |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 3600 | #ifdef USE_THREAD |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 3601 | { |
William Lallemand | 1aab50b | 2018-06-07 09:46:01 +0200 | [diff] [blame] | 3602 | sigset_t blocked_sig, old_sig; |
Willy Tarreau | c40efc1 | 2019-05-03 09:22:44 +0200 | [diff] [blame] | 3603 | int i; |
| 3604 | |
William Lallemand | 1aab50b | 2018-06-07 09:46:01 +0200 | [diff] [blame] | 3605 | /* ensure the signals will be blocked in every thread */ |
| 3606 | sigfillset(&blocked_sig); |
| 3607 | sigdelset(&blocked_sig, SIGPROF); |
| 3608 | sigdelset(&blocked_sig, SIGBUS); |
| 3609 | sigdelset(&blocked_sig, SIGFPE); |
| 3610 | sigdelset(&blocked_sig, SIGILL); |
| 3611 | sigdelset(&blocked_sig, SIGSEGV); |
| 3612 | pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig); |
| 3613 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 3614 | /* Create nbthread-1 thread. The first thread is the current process */ |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 3615 | ha_thread_info[0].pthread = pthread_self(); |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 3616 | for (i = 1; i < global.nbthread; i++) |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 3617 | pthread_create(&ha_thread_info[i].pthread, NULL, &run_thread_poll_loop, (void *)(long)i); |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 3618 | |
Christopher Faulet | 6251902 | 2017-10-16 15:49:32 +0200 | [diff] [blame] | 3619 | #ifdef USE_CPU_AFFINITY |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 3620 | /* Now the CPU affinity for all threads */ |
Willy Tarreau | 7764a57 | 2019-07-16 15:10:34 +0200 | [diff] [blame] | 3621 | if (global.cpu_map.proc_t1[relative_pid-1]) |
| 3622 | global.cpu_map.thread[0] &= global.cpu_map.proc_t1[relative_pid-1]; |
| 3623 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 3624 | for (i = 0; i < global.nbthread; i++) { |
Christopher Faulet | cb6a945 | 2017-11-22 16:50:41 +0100 | [diff] [blame] | 3625 | if (global.cpu_map.proc[relative_pid-1]) |
Willy Tarreau | 81492c9 | 2019-05-03 09:41:23 +0200 | [diff] [blame] | 3626 | global.cpu_map.thread[i] &= global.cpu_map.proc[relative_pid-1]; |
Christopher Faulet | 6251902 | 2017-10-16 15:49:32 +0200 | [diff] [blame] | 3627 | |
Willy Tarreau | 421f02e | 2018-01-20 18:19:22 +0100 | [diff] [blame] | 3628 | if (i < MAX_THREADS && /* only the first 32/64 threads may be pinned */ |
Willy Tarreau | 81492c9 | 2019-05-03 09:41:23 +0200 | [diff] [blame] | 3629 | global.cpu_map.thread[i]) {/* only do this if the thread has a THREAD map */ |
David Carlier | 5e4c8e2 | 2019-09-13 05:12:58 +0100 | [diff] [blame] | 3630 | #if defined(__APPLE__) |
| 3631 | int j; |
| 3632 | unsigned long cpu_map = global.cpu_map.thread[i]; |
| 3633 | |
| 3634 | while ((j = ffsl(cpu_map)) > 0) { |
| 3635 | thread_affinity_policy_data_t cpu_set = { j - 1 }; |
| 3636 | thread_port_t mthread = pthread_mach_thread_np(ha_thread_info[i].pthread); |
| 3637 | thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1); |
| 3638 | cpu_map &= ~(1UL << (j - 1)); |
| 3639 | } |
| 3640 | #else |
Olivier Houchard | 829aa24 | 2017-12-01 18:19:43 +0100 | [diff] [blame] | 3641 | #if defined(__FreeBSD__) || defined(__NetBSD__) |
| 3642 | cpuset_t cpuset; |
| 3643 | #else |
| 3644 | cpu_set_t cpuset; |
| 3645 | #endif |
| 3646 | int j; |
Willy Tarreau | 81492c9 | 2019-05-03 09:41:23 +0200 | [diff] [blame] | 3647 | unsigned long cpu_map = global.cpu_map.thread[i]; |
Olivier Houchard | 829aa24 | 2017-12-01 18:19:43 +0100 | [diff] [blame] | 3648 | |
| 3649 | CPU_ZERO(&cpuset); |
| 3650 | |
| 3651 | while ((j = ffsl(cpu_map)) > 0) { |
| 3652 | CPU_SET(j - 1, &cpuset); |
Cyril Bonté | d400ab3 | 2018-03-12 21:47:39 +0100 | [diff] [blame] | 3653 | cpu_map &= ~(1UL << (j - 1)); |
Olivier Houchard | 829aa24 | 2017-12-01 18:19:43 +0100 | [diff] [blame] | 3654 | } |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 3655 | pthread_setaffinity_np(ha_thread_info[i].pthread, |
Olivier Houchard | 829aa24 | 2017-12-01 18:19:43 +0100 | [diff] [blame] | 3656 | sizeof(cpuset), &cpuset); |
David Carlier | 5e4c8e2 | 2019-09-13 05:12:58 +0100 | [diff] [blame] | 3657 | #endif |
Olivier Houchard | 829aa24 | 2017-12-01 18:19:43 +0100 | [diff] [blame] | 3658 | } |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 3659 | } |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 3660 | #endif /* !USE_CPU_AFFINITY */ |
| 3661 | |
William Lallemand | 1aab50b | 2018-06-07 09:46:01 +0200 | [diff] [blame] | 3662 | /* when multithreading we need to let only the thread 0 handle the signals */ |
William Lallemand | d3801c1 | 2018-09-11 10:06:23 +0200 | [diff] [blame] | 3663 | haproxy_unblock_signals(); |
William Lallemand | 1aab50b | 2018-06-07 09:46:01 +0200 | [diff] [blame] | 3664 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 3665 | /* Finally, start the poll loop for the first thread */ |
Willy Tarreau | b4f7cc3 | 2019-05-03 09:27:30 +0200 | [diff] [blame] | 3666 | run_thread_poll_loop(0); |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 3667 | |
| 3668 | /* Wait the end of other threads */ |
| 3669 | for (i = 1; i < global.nbthread; i++) |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 3670 | pthread_join(ha_thread_info[i].pthread, NULL); |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 3671 | |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 3672 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 3673 | show_lock_stats(); |
| 3674 | #endif |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 3675 | } |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 3676 | #else /* ! USE_THREAD */ |
William Lallemand | d3801c1 | 2018-09-11 10:06:23 +0200 | [diff] [blame] | 3677 | haproxy_unblock_signals(); |
Willy Tarreau | b4f7cc3 | 2019-05-03 09:27:30 +0200 | [diff] [blame] | 3678 | run_thread_poll_loop(0); |
Christopher Faulet | 6251902 | 2017-10-16 15:49:32 +0200 | [diff] [blame] | 3679 | #endif |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 3680 | |
| 3681 | /* Do some cleanup */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3682 | deinit(); |
Christopher Faulet | 1d17c10 | 2017-08-29 15:38:48 +0200 | [diff] [blame] | 3683 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3684 | exit(0); |
| 3685 | } |
| 3686 | |
| 3687 | |
| 3688 | /* |
| 3689 | * Local variables: |
| 3690 | * c-indent-level: 8 |
| 3691 | * c-basic-offset: 8 |
| 3692 | * End: |
| 3693 | */ |