Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * External health-checks functions. |
| 3 | * |
| 4 | * Copyright 2000-2009,2020 Willy Tarreau <w@1wt.eu> |
| 5 | * Copyright 2014 Horms Solutions Ltd, Simon Horman <horms@verge.net.au> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <sys/resource.h> |
| 15 | #include <sys/socket.h> |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/wait.h> |
| 18 | #include <assert.h> |
| 19 | #include <ctype.h> |
| 20 | #include <errno.h> |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 21 | #include <signal.h> |
| 22 | #include <stdarg.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <time.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <haproxy/api.h> |
| 30 | #include <haproxy/cfgparse.h> |
| 31 | #include <haproxy/check.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 32 | #include <haproxy/errors.h> |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 33 | #include <haproxy/global.h> |
| 34 | #include <haproxy/list.h> |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 35 | #include <haproxy/proxy.h> |
| 36 | #include <haproxy/server.h> |
| 37 | #include <haproxy/signal.h> |
Willy Tarreau | 397ad41 | 2021-10-06 09:11:54 +0200 | [diff] [blame] | 38 | #include <haproxy/stream-t.h> |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 39 | #include <haproxy/task.h> |
| 40 | #include <haproxy/thread.h> |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 41 | #include <haproxy/tools.h> |
| 42 | |
| 43 | |
| 44 | static struct list pid_list = LIST_HEAD_INIT(pid_list); |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 45 | static struct pool_head *pool_head_pid_list __read_mostly; |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 46 | __decl_spinlock(pid_list_lock); |
| 47 | |
| 48 | struct extcheck_env { |
| 49 | char *name; /* environment variable name */ |
| 50 | int vmaxlen; /* value maximum length, used to determine the required memory allocation */ |
| 51 | }; |
| 52 | |
| 53 | /* environment variables memory requirement for different types of data */ |
| 54 | #define EXTCHK_SIZE_EVAL_INIT 0 /* size determined during the init phase, |
| 55 | * such environment variables are not updatable. */ |
| 56 | #define EXTCHK_SIZE_ULONG 20 /* max string length for an unsigned long value */ |
| 57 | #define EXTCHK_SIZE_UINT 11 /* max string length for an unsigned int value */ |
Willy Tarreau | cef08c2 | 2022-04-14 19:51:02 +0200 | [diff] [blame] | 58 | #define EXTCHK_SIZE_ADDR 256 /* max string length for an IPv4/IPv6/UNIX address */ |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 59 | |
| 60 | /* external checks environment variables */ |
| 61 | enum { |
| 62 | EXTCHK_PATH = 0, |
| 63 | |
| 64 | /* Proxy specific environment variables */ |
| 65 | EXTCHK_HAPROXY_PROXY_NAME, /* the backend name */ |
| 66 | EXTCHK_HAPROXY_PROXY_ID, /* the backend id */ |
| 67 | EXTCHK_HAPROXY_PROXY_ADDR, /* the first bind address if available (or empty) */ |
| 68 | EXTCHK_HAPROXY_PROXY_PORT, /* the first bind port if available (or empty) */ |
| 69 | |
| 70 | /* Server specific environment variables */ |
| 71 | EXTCHK_HAPROXY_SERVER_NAME, /* the server name */ |
| 72 | EXTCHK_HAPROXY_SERVER_ID, /* the server id */ |
| 73 | EXTCHK_HAPROXY_SERVER_ADDR, /* the server address */ |
| 74 | EXTCHK_HAPROXY_SERVER_PORT, /* the server port if available (or empty) */ |
| 75 | EXTCHK_HAPROXY_SERVER_MAXCONN, /* the server max connections */ |
| 76 | EXTCHK_HAPROXY_SERVER_CURCONN, /* the current number of connections on the server */ |
Willy Tarreau | 973cf90 | 2022-05-13 15:58:35 +0200 | [diff] [blame] | 77 | EXTCHK_HAPROXY_SERVER_SSL, /* "1" if the server supports SSL, otherwise zero */ |
| 78 | EXTCHK_HAPROXY_SERVER_PROTO, /* the server's configured proto, if any */ |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 79 | |
| 80 | EXTCHK_SIZE |
| 81 | }; |
| 82 | |
| 83 | const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = { |
| 84 | [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT }, |
| 85 | [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT }, |
| 86 | [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT }, |
| 87 | [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT }, |
| 88 | [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT }, |
| 89 | [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT }, |
| 90 | [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT }, |
| 91 | [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_ADDR }, |
| 92 | [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_UINT }, |
| 93 | [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT }, |
| 94 | [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG }, |
Willy Tarreau | 973cf90 | 2022-05-13 15:58:35 +0200 | [diff] [blame] | 95 | [EXTCHK_HAPROXY_SERVER_SSL] = { "HAPROXY_SERVER_SSL", EXTCHK_SIZE_UINT }, |
| 96 | [EXTCHK_HAPROXY_SERVER_PROTO] = { "HAPROXY_SERVER_PROTO", EXTCHK_SIZE_EVAL_INIT }, |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | void block_sigchld(void) |
| 100 | { |
| 101 | sigset_t set; |
| 102 | sigemptyset(&set); |
| 103 | sigaddset(&set, SIGCHLD); |
| 104 | assert(ha_sigmask(SIG_BLOCK, &set, NULL) == 0); |
| 105 | } |
| 106 | |
| 107 | void unblock_sigchld(void) |
| 108 | { |
| 109 | sigset_t set; |
| 110 | sigemptyset(&set); |
| 111 | sigaddset(&set, SIGCHLD); |
| 112 | assert(ha_sigmask(SIG_UNBLOCK, &set, NULL) == 0); |
| 113 | } |
| 114 | |
| 115 | static struct pid_list *pid_list_add(pid_t pid, struct task *t) |
| 116 | { |
| 117 | struct pid_list *elem; |
| 118 | struct check *check = t->context; |
| 119 | |
| 120 | elem = pool_alloc(pool_head_pid_list); |
| 121 | if (!elem) |
| 122 | return NULL; |
| 123 | elem->pid = pid; |
| 124 | elem->t = t; |
| 125 | elem->exited = 0; |
| 126 | check->curpid = elem; |
| 127 | LIST_INIT(&elem->list); |
| 128 | |
| 129 | HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 130 | LIST_INSERT(&pid_list, &elem->list); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 131 | HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock); |
| 132 | |
| 133 | return elem; |
| 134 | } |
| 135 | |
| 136 | static void pid_list_del(struct pid_list *elem) |
| 137 | { |
| 138 | struct check *check; |
| 139 | |
| 140 | if (!elem) |
| 141 | return; |
| 142 | |
| 143 | HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 144 | LIST_DELETE(&elem->list); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 145 | HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock); |
| 146 | |
| 147 | if (!elem->exited) |
| 148 | kill(elem->pid, SIGTERM); |
| 149 | |
| 150 | check = elem->t->context; |
| 151 | check->curpid = NULL; |
| 152 | pool_free(pool_head_pid_list, elem); |
| 153 | } |
| 154 | |
| 155 | /* Called from inside SIGCHLD handler, SIGCHLD is blocked */ |
| 156 | static void pid_list_expire(pid_t pid, int status) |
| 157 | { |
| 158 | struct pid_list *elem; |
| 159 | |
| 160 | HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock); |
| 161 | list_for_each_entry(elem, &pid_list, list) { |
| 162 | if (elem->pid == pid) { |
| 163 | elem->t->expire = now_ms; |
| 164 | elem->status = status; |
| 165 | elem->exited = 1; |
| 166 | task_wakeup(elem->t, TASK_WOKEN_IO); |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock); |
| 171 | } |
| 172 | |
| 173 | static void sigchld_handler(struct sig_handler *sh) |
| 174 | { |
| 175 | pid_t pid; |
| 176 | int status; |
| 177 | |
| 178 | while ((pid = waitpid(0, &status, WNOHANG)) > 0) |
| 179 | pid_list_expire(pid, status); |
| 180 | } |
| 181 | |
| 182 | int init_pid_list(void) |
| 183 | { |
| 184 | if (pool_head_pid_list != NULL) |
| 185 | /* Nothing to do */ |
| 186 | return 0; |
| 187 | |
| 188 | if (!signal_register_fct(SIGCHLD, sigchld_handler, SIGCHLD)) { |
| 189 | ha_alert("Failed to set signal handler for external health checks: %s. Aborting.\n", |
| 190 | strerror(errno)); |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | pool_head_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED); |
| 195 | if (pool_head_pid_list == NULL) { |
| 196 | ha_alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n", |
| 197 | strerror(errno)); |
| 198 | return 1; |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | /* helper macro to set an environment variable and jump to a specific label on failure. */ |
| 205 | #define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; } |
| 206 | |
| 207 | /* |
| 208 | * helper function to allocate enough memory to store an environment variable. |
| 209 | * It will also check that the environment variable is updatable, and silently |
| 210 | * fail if not. |
| 211 | */ |
| 212 | static int extchk_setenv(struct check *check, int idx, const char *value) |
| 213 | { |
| 214 | int len, ret; |
| 215 | char *envname; |
| 216 | int vmaxlen; |
| 217 | |
| 218 | if (idx < 0 || idx >= EXTCHK_SIZE) { |
| 219 | ha_alert("Illegal environment variable index %d. Aborting.\n", idx); |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | envname = extcheck_envs[idx].name; |
| 224 | vmaxlen = extcheck_envs[idx].vmaxlen; |
| 225 | |
| 226 | /* Check if the environment variable is already set, and silently reject |
| 227 | * the update if this one is not updatable. */ |
| 228 | if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx])) |
| 229 | return 0; |
| 230 | |
| 231 | /* Instead of sending NOT_USED, sending an empty value is preferable */ |
| 232 | if (strcmp(value, "NOT_USED") == 0) { |
| 233 | value = ""; |
| 234 | } |
| 235 | |
| 236 | len = strlen(envname) + 1; |
| 237 | if (vmaxlen == EXTCHK_SIZE_EVAL_INIT) |
| 238 | len += strlen(value); |
| 239 | else |
| 240 | len += vmaxlen; |
| 241 | |
| 242 | if (!check->envp[idx]) |
| 243 | check->envp[idx] = malloc(len + 1); |
| 244 | |
| 245 | if (!check->envp[idx]) { |
| 246 | ha_alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname); |
| 247 | return 1; |
| 248 | } |
| 249 | ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value); |
| 250 | if (ret < 0) { |
| 251 | ha_alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno)); |
| 252 | return 1; |
| 253 | } |
| 254 | else if (ret > len) { |
| 255 | ha_alert("Environment variable '%s' was truncated. Aborting.\n", envname); |
| 256 | return 1; |
| 257 | } |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | int prepare_external_check(struct check *check) |
| 262 | { |
| 263 | struct server *s = check->server; |
| 264 | struct proxy *px = s->proxy; |
| 265 | struct listener *listener = NULL, *l; |
| 266 | int i; |
| 267 | const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH; |
| 268 | char buf[256]; |
Willy Tarreau | 973cf90 | 2022-05-13 15:58:35 +0200 | [diff] [blame] | 269 | const char *svmode = NULL; |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 270 | |
| 271 | list_for_each_entry(l, &px->conf.listeners, by_fe) |
| 272 | /* Use the first INET, INET6 or UNIX listener */ |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 273 | if (l->rx.addr.ss_family == AF_INET || |
| 274 | l->rx.addr.ss_family == AF_INET6 || |
| 275 | l->rx.addr.ss_family == AF_UNIX) { |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 276 | listener = l; |
| 277 | break; |
| 278 | } |
| 279 | |
| 280 | check->curpid = NULL; |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 281 | check->envp = calloc((EXTCHK_SIZE + 1), sizeof(*check->envp)); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 282 | if (!check->envp) { |
| 283 | ha_alert("Failed to allocate memory for environment variables. Aborting\n"); |
| 284 | goto err; |
| 285 | } |
| 286 | |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 287 | check->argv = calloc(6, sizeof(*check->argv)); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 288 | if (!check->argv) { |
| 289 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 290 | goto err; |
| 291 | } |
| 292 | |
| 293 | check->argv[0] = px->check_command; |
| 294 | |
| 295 | if (!listener) { |
| 296 | check->argv[1] = strdup("NOT_USED"); |
| 297 | check->argv[2] = strdup("NOT_USED"); |
| 298 | } |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 299 | else if (listener->rx.addr.ss_family == AF_INET || |
| 300 | listener->rx.addr.ss_family == AF_INET6) { |
| 301 | addr_to_str(&listener->rx.addr, buf, sizeof(buf)); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 302 | check->argv[1] = strdup(buf); |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 303 | port_to_str(&listener->rx.addr, buf, sizeof(buf)); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 304 | check->argv[2] = strdup(buf); |
| 305 | } |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 306 | else if (listener->rx.addr.ss_family == AF_UNIX) { |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 307 | const struct sockaddr_un *un; |
| 308 | |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 309 | un = (struct sockaddr_un *)&listener->rx.addr; |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 310 | check->argv[1] = strdup(un->sun_path); |
| 311 | check->argv[2] = strdup("NOT_USED"); |
| 312 | } |
| 313 | else { |
| 314 | ha_alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id); |
| 315 | goto err; |
| 316 | } |
| 317 | |
Willy Tarreau | c7edc98 | 2022-04-14 19:49:50 +0200 | [diff] [blame] | 318 | /* args 3 and 4 are the address, they're replaced on each check */ |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 319 | check->argv[3] = calloc(EXTCHK_SIZE_ADDR, sizeof(*check->argv[3])); |
| 320 | check->argv[4] = calloc(EXTCHK_SIZE_UINT, sizeof(*check->argv[4])); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 321 | |
| 322 | for (i = 0; i < 5; i++) { |
| 323 | if (!check->argv[i]) { |
| 324 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 325 | goto err; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | EXTCHK_SETENV(check, EXTCHK_PATH, path, err); |
| 330 | /* Add proxy environment variables */ |
| 331 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err); |
| 332 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err); |
| 333 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err); |
| 334 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err); |
| 335 | /* Add server environment variables */ |
| 336 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err); |
| 337 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err); |
| 338 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err); |
| 339 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err); |
| 340 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err); |
| 341 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err); |
Willy Tarreau | 973cf90 | 2022-05-13 15:58:35 +0200 | [diff] [blame] | 342 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_SSL, s->use_ssl ? "1" : "0", err); |
| 343 | |
| 344 | switch (px->mode) { |
| 345 | case PR_MODE_CLI: svmode = "cli"; break; |
| 346 | case PR_MODE_SYSLOG: svmode = "syslog"; break; |
| 347 | case PR_MODE_PEERS: svmode = "peers"; break; |
| 348 | case PR_MODE_HTTP: svmode = (s->mux_proto) ? s->mux_proto->token.ptr : "h1"; break; |
| 349 | case PR_MODE_TCP: svmode = "tcp"; break; |
| 350 | /* all valid cases must be enumerated above, below is to avoid a warning */ |
| 351 | case PR_MODES: svmode = "?"; break; |
| 352 | } |
| 353 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PROTO, svmode, err); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 354 | |
| 355 | /* Ensure that we don't leave any hole in check->envp */ |
| 356 | for (i = 0; i < EXTCHK_SIZE; i++) |
| 357 | if (!check->envp[i]) |
| 358 | EXTCHK_SETENV(check, i, "", err); |
| 359 | |
| 360 | return 1; |
| 361 | err: |
| 362 | if (check->envp) { |
| 363 | for (i = 0; i < EXTCHK_SIZE; i++) |
| 364 | free(check->envp[i]); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 365 | ha_free(&check->envp); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | if (check->argv) { |
| 369 | for (i = 1; i < 5; i++) |
| 370 | free(check->argv[i]); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 371 | ha_free(&check->argv); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 372 | } |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * establish a server health-check that makes use of a process. |
| 378 | * |
| 379 | * It can return one of : |
| 380 | * - SF_ERR_NONE if everything's OK |
| 381 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 382 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
| 383 | * |
| 384 | * Blocks and then unblocks SIGCHLD |
| 385 | */ |
| 386 | static int connect_proc_chk(struct task *t) |
| 387 | { |
| 388 | char buf[256]; |
| 389 | struct check *check = t->context; |
| 390 | struct server *s = check->server; |
| 391 | struct proxy *px = s->proxy; |
| 392 | int status; |
| 393 | pid_t pid; |
| 394 | |
| 395 | status = SF_ERR_RESOURCE; |
| 396 | |
| 397 | block_sigchld(); |
| 398 | |
| 399 | pid = fork(); |
| 400 | if (pid < 0) { |
| 401 | ha_alert("Failed to fork process for external health check%s: %s. Aborting.\n", |
| 402 | (global.tune.options & GTUNE_INSECURE_FORK) ? |
| 403 | "" : " (likely caused by missing 'insecure-fork-wanted')", |
| 404 | strerror(errno)); |
| 405 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 406 | goto out; |
| 407 | } |
| 408 | if (pid == 0) { |
| 409 | /* Child */ |
| 410 | extern char **environ; |
| 411 | struct rlimit limit; |
| 412 | int fd; |
| 413 | |
| 414 | /* close all FDs. Keep stdin/stdout/stderr in verbose mode */ |
| 415 | fd = (global.mode & (MODE_QUIET|MODE_VERBOSE)) == MODE_QUIET ? 0 : 3; |
| 416 | |
| 417 | my_closefrom(fd); |
| 418 | |
| 419 | /* restore the initial FD limits */ |
| 420 | limit.rlim_cur = rlim_fd_cur_at_boot; |
| 421 | limit.rlim_max = rlim_fd_max_at_boot; |
Willy Tarreau | c06557c | 2022-09-22 16:12:08 +0200 | [diff] [blame] | 422 | if (raise_rlim_nofile(NULL, &limit) != 0) { |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 423 | getrlimit(RLIMIT_NOFILE, &limit); |
| 424 | ha_warning("External check: failed to restore initial FD limits (cur=%u max=%u), using cur=%u max=%u\n", |
| 425 | rlim_fd_cur_at_boot, rlim_fd_max_at_boot, |
| 426 | (unsigned int)limit.rlim_cur, (unsigned int)limit.rlim_max); |
| 427 | } |
| 428 | |
| 429 | environ = check->envp; |
| 430 | |
| 431 | /* Update some environment variables and command args: curconn, server addr and server port */ |
Willy Tarreau | b3250a2 | 2020-10-24 13:07:39 +0200 | [diff] [blame] | 432 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), fail); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 433 | |
Willy Tarreau | cef08c2 | 2022-04-14 19:51:02 +0200 | [diff] [blame] | 434 | if (s->addr.ss_family == AF_UNIX) { |
| 435 | const struct sockaddr_un *un = (struct sockaddr_un *)&s->addr; |
| 436 | strlcpy2(check->argv[3], un->sun_path, EXTCHK_SIZE_ADDR); |
| 437 | memcpy(check->argv[4], "NOT_USED", 9); |
| 438 | } else { |
| 439 | addr_to_str(&s->addr, check->argv[3], EXTCHK_SIZE_ADDR); |
| 440 | *check->argv[4] = 0; // just in case the address family changed |
| 441 | if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6) |
| 442 | snprintf(check->argv[4], EXTCHK_SIZE_UINT, "%u", s->svc_port); |
| 443 | } |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 444 | |
Willy Tarreau | cef08c2 | 2022-04-14 19:51:02 +0200 | [diff] [blame] | 445 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], fail); |
Willy Tarreau | b3250a2 | 2020-10-24 13:07:39 +0200 | [diff] [blame] | 446 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], fail); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 447 | |
| 448 | haproxy_unblock_signals(); |
| 449 | execvp(px->check_command, check->argv); |
| 450 | ha_alert("Failed to exec process for external health check: %s. Aborting.\n", |
| 451 | strerror(errno)); |
Willy Tarreau | b3250a2 | 2020-10-24 13:07:39 +0200 | [diff] [blame] | 452 | fail: |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 453 | exit(-1); |
| 454 | } |
| 455 | |
| 456 | /* Parent */ |
| 457 | if (check->result == CHK_RES_UNKNOWN) { |
| 458 | if (pid_list_add(pid, t) != NULL) { |
| 459 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 460 | |
| 461 | if (px->timeout.check && px->timeout.connect) { |
| 462 | int t_con = tick_add(now_ms, px->timeout.connect); |
| 463 | t->expire = tick_first(t->expire, t_con); |
| 464 | } |
| 465 | status = SF_ERR_NONE; |
| 466 | goto out; |
| 467 | } |
| 468 | else { |
| 469 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 470 | } |
| 471 | kill(pid, SIGTERM); /* process creation error */ |
| 472 | } |
| 473 | else |
| 474 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 475 | |
| 476 | out: |
| 477 | unblock_sigchld(); |
| 478 | return status; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * manages a server health-check that uses an external process. Returns |
| 483 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 484 | * |
| 485 | * Please do NOT place any return statement in this function and only leave |
| 486 | * via the out_unlock label. |
| 487 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 488 | struct task *process_chk_proc(struct task *t, void *context, unsigned int state) |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 489 | { |
| 490 | struct check *check = context; |
| 491 | struct server *s = check->server; |
| 492 | int rv; |
| 493 | int ret; |
| 494 | int expired = tick_is_expired(t->expire, now_ms); |
| 495 | |
| 496 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
| 497 | if (!(check->state & CHK_ST_INPROGRESS)) { |
| 498 | /* no check currently running */ |
| 499 | if (!expired) /* woke up too early */ |
| 500 | goto out_unlock; |
| 501 | |
| 502 | /* we don't send any health-checks when the proxy is |
| 503 | * stopped, the server should not be checked or the check |
| 504 | * is disabled. |
| 505 | */ |
| 506 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 507 | (s->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 508 | goto reschedule; |
| 509 | |
| 510 | /* we'll initiate a new check */ |
| 511 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
| 512 | |
| 513 | check->state |= CHK_ST_INPROGRESS; |
| 514 | |
| 515 | ret = connect_proc_chk(t); |
| 516 | if (ret == SF_ERR_NONE) { |
| 517 | /* the process was forked, we allow up to min(inter, |
| 518 | * timeout.connect) for it to report its status, but |
| 519 | * only when timeout.check is set as it may be to short |
| 520 | * for a full check otherwise. |
| 521 | */ |
| 522 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 523 | |
| 524 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 525 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 526 | t->expire = tick_first(t->expire, t_con); |
| 527 | } |
Willy Tarreau | eed3911 | 2022-06-15 17:20:16 +0200 | [diff] [blame] | 528 | task_set_thread(t, tid); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 529 | goto reschedule; |
| 530 | } |
| 531 | |
| 532 | /* here, we failed to start the check */ |
| 533 | |
| 534 | check->state &= ~CHK_ST_INPROGRESS; |
| 535 | check_notify_failure(check); |
| 536 | |
| 537 | /* we allow up to min(inter, timeout.connect) for a connection |
| 538 | * to establish but only when timeout.check is set |
| 539 | * as it may be to short for a full check otherwise |
| 540 | */ |
| 541 | while (tick_is_expired(t->expire, now_ms)) { |
| 542 | int t_con; |
| 543 | |
| 544 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 545 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 546 | |
| 547 | if (s->proxy->timeout.check) |
| 548 | t->expire = tick_first(t->expire, t_con); |
| 549 | } |
| 550 | } |
| 551 | else { |
| 552 | /* there was a test running. |
| 553 | * First, let's check whether there was an uncaught error, |
| 554 | * which can happen on connect timeout or error. |
| 555 | */ |
| 556 | if (check->result == CHK_RES_UNKNOWN) { |
| 557 | /* good connection is enough for pure TCP check */ |
| 558 | struct pid_list *elem = check->curpid; |
| 559 | int status = HCHK_STATUS_UNKNOWN; |
| 560 | |
| 561 | if (elem->exited) { |
| 562 | status = elem->status; /* Save in case the process exits between use below */ |
| 563 | if (!WIFEXITED(status)) |
| 564 | check->code = -1; |
| 565 | else |
| 566 | check->code = WEXITSTATUS(status); |
| 567 | if (!WIFEXITED(status) || WEXITSTATUS(status)) |
| 568 | status = HCHK_STATUS_PROCERR; |
| 569 | else |
| 570 | status = HCHK_STATUS_PROCOK; |
| 571 | } else if (expired) { |
| 572 | status = HCHK_STATUS_PROCTOUT; |
| 573 | ha_warning("kill %d\n", (int)elem->pid); |
| 574 | kill(elem->pid, SIGTERM); |
| 575 | } |
| 576 | set_server_check_status(check, status, NULL); |
| 577 | } |
| 578 | |
| 579 | if (check->result == CHK_RES_FAILED) { |
| 580 | /* a failure or timeout detected */ |
| 581 | check_notify_failure(check); |
| 582 | } |
| 583 | else if (check->result == CHK_RES_CONDPASS) { |
| 584 | /* check is OK but asks for stopping mode */ |
| 585 | check_notify_stopping(check); |
| 586 | } |
| 587 | else if (check->result == CHK_RES_PASSED) { |
| 588 | /* a success was detected */ |
| 589 | check_notify_success(check); |
| 590 | } |
Willy Tarreau | eed3911 | 2022-06-15 17:20:16 +0200 | [diff] [blame] | 591 | task_set_thread(t, 0); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 592 | check->state &= ~CHK_ST_INPROGRESS; |
| 593 | |
| 594 | pid_list_del(check->curpid); |
| 595 | |
| 596 | rv = 0; |
| 597 | if (global.spread_checks > 0) { |
| 598 | rv = srv_getinter(check) * global.spread_checks / 100; |
Willy Tarreau | a840b4a | 2022-10-12 21:48:17 +0200 | [diff] [blame] | 599 | rv -= (int) (2 * rv * (statistical_prng() / 4294967295.0)); |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 600 | } |
| 601 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); |
| 602 | } |
| 603 | |
| 604 | reschedule: |
| 605 | while (tick_is_expired(t->expire, now_ms)) |
| 606 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 607 | |
| 608 | out_unlock: |
| 609 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
| 610 | return t; |
| 611 | } |
| 612 | |
| 613 | /* Parses the "external-check" proxy keyword */ |
| 614 | int proxy_parse_extcheck(char **args, int section, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 615 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 616 | char **errmsg) |
| 617 | { |
| 618 | int cur_arg, ret = 0; |
| 619 | |
| 620 | cur_arg = 1; |
| 621 | if (!*(args[cur_arg])) { |
| 622 | memprintf(errmsg, "missing argument after '%s'.\n", args[0]); |
| 623 | goto error; |
| 624 | } |
| 625 | |
| 626 | if (strcmp(args[cur_arg], "command") == 0) { |
| 627 | if (too_many_args(2, args, errmsg, NULL)) |
| 628 | goto error; |
| 629 | if (!*(args[cur_arg+1])) { |
| 630 | memprintf(errmsg, "missing argument after '%s'.", args[cur_arg]); |
| 631 | goto error; |
| 632 | } |
| 633 | free(curpx->check_command); |
| 634 | curpx->check_command = strdup(args[cur_arg+1]); |
| 635 | } |
| 636 | else if (strcmp(args[cur_arg], "path") == 0) { |
| 637 | if (too_many_args(2, args, errmsg, NULL)) |
| 638 | goto error; |
| 639 | if (!*(args[cur_arg+1])) { |
| 640 | memprintf(errmsg, "missing argument after '%s'.", args[cur_arg]); |
| 641 | goto error; |
| 642 | } |
| 643 | free(curpx->check_path); |
| 644 | curpx->check_path = strdup(args[cur_arg+1]); |
| 645 | } |
| 646 | else { |
| 647 | memprintf(errmsg, "'%s' only supports 'command' and 'path'. but got '%s'.", |
| 648 | args[0], args[1]); |
| 649 | goto error; |
| 650 | } |
| 651 | |
| 652 | ret = (*errmsg != NULL); /* Handle warning */ |
| 653 | return ret; |
| 654 | |
| 655 | error: |
| 656 | return -1; |
| 657 | } |
| 658 | |
Willy Tarreau | 220fd70 | 2021-02-12 12:07:38 +0100 | [diff] [blame] | 659 | int proxy_parse_external_check_opt(char **args, int cur_arg, struct proxy *curpx, const struct proxy *defpx, |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 660 | const char *file, int line) |
| 661 | { |
| 662 | int err_code = 0; |
| 663 | |
| 664 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 665 | curpx->options2 |= PR_O2_EXT_CHK; |
| 666 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 667 | goto out; |
| 668 | |
| 669 | out: |
| 670 | return err_code; |
| 671 | } |
| 672 | |
| 673 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 674 | { CFG_LISTEN, "external-check", proxy_parse_extcheck }, |
| 675 | { 0, NULL, NULL }, |
| 676 | }}; |
| 677 | |
| 678 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |