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