blob: 0d2194a1a59a169bf6ce701eee7b08c861d9c24e [file] [log] [blame]
Willy Tarreaubcc67332020-06-05 15:31:31 +02001/*
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 Tarreaubcc67332020-06-05 15:31:31 +020021#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 Tarreau36979d92020-06-05 17:27:29 +020032#include <haproxy/errors.h>
Willy Tarreaubcc67332020-06-05 15:31:31 +020033#include <haproxy/global.h>
34#include <haproxy/list.h>
Willy Tarreaubcc67332020-06-05 15:31:31 +020035#include <haproxy/proxy.h>
36#include <haproxy/server.h>
37#include <haproxy/signal.h>
Willy Tarreau397ad412021-10-06 09:11:54 +020038#include <haproxy/stream-t.h>
Willy Tarreaubcc67332020-06-05 15:31:31 +020039#include <haproxy/task.h>
40#include <haproxy/thread.h>
Willy Tarreaubcc67332020-06-05 15:31:31 +020041#include <haproxy/tools.h>
42
43
44static struct list pid_list = LIST_HEAD_INIT(pid_list);
Willy Tarreauff882702021-04-10 17:23:00 +020045static struct pool_head *pool_head_pid_list __read_mostly;
Willy Tarreaubcc67332020-06-05 15:31:31 +020046__decl_spinlock(pid_list_lock);
47
48struct 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 Tarreaucef08c22022-04-14 19:51:02 +020058#define EXTCHK_SIZE_ADDR 256 /* max string length for an IPv4/IPv6/UNIX address */
Willy Tarreaubcc67332020-06-05 15:31:31 +020059
60/* external checks environment variables */
61enum {
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 */
77
78 EXTCHK_SIZE
79};
80
81const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = {
82 [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT },
83 [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT },
84 [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT },
85 [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT },
86 [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT },
87 [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT },
88 [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT },
89 [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_ADDR },
90 [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_UINT },
91 [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT },
92 [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG },
93};
94
95void block_sigchld(void)
96{
97 sigset_t set;
98 sigemptyset(&set);
99 sigaddset(&set, SIGCHLD);
100 assert(ha_sigmask(SIG_BLOCK, &set, NULL) == 0);
101}
102
103void unblock_sigchld(void)
104{
105 sigset_t set;
106 sigemptyset(&set);
107 sigaddset(&set, SIGCHLD);
108 assert(ha_sigmask(SIG_UNBLOCK, &set, NULL) == 0);
109}
110
111static struct pid_list *pid_list_add(pid_t pid, struct task *t)
112{
113 struct pid_list *elem;
114 struct check *check = t->context;
115
116 elem = pool_alloc(pool_head_pid_list);
117 if (!elem)
118 return NULL;
119 elem->pid = pid;
120 elem->t = t;
121 elem->exited = 0;
122 check->curpid = elem;
123 LIST_INIT(&elem->list);
124
125 HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200126 LIST_INSERT(&pid_list, &elem->list);
Willy Tarreaubcc67332020-06-05 15:31:31 +0200127 HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
128
129 return elem;
130}
131
132static void pid_list_del(struct pid_list *elem)
133{
134 struct check *check;
135
136 if (!elem)
137 return;
138
139 HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200140 LIST_DELETE(&elem->list);
Willy Tarreaubcc67332020-06-05 15:31:31 +0200141 HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
142
143 if (!elem->exited)
144 kill(elem->pid, SIGTERM);
145
146 check = elem->t->context;
147 check->curpid = NULL;
148 pool_free(pool_head_pid_list, elem);
149}
150
151/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
152static void pid_list_expire(pid_t pid, int status)
153{
154 struct pid_list *elem;
155
156 HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
157 list_for_each_entry(elem, &pid_list, list) {
158 if (elem->pid == pid) {
159 elem->t->expire = now_ms;
160 elem->status = status;
161 elem->exited = 1;
162 task_wakeup(elem->t, TASK_WOKEN_IO);
163 break;
164 }
165 }
166 HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
167}
168
169static void sigchld_handler(struct sig_handler *sh)
170{
171 pid_t pid;
172 int status;
173
174 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
175 pid_list_expire(pid, status);
176}
177
178int init_pid_list(void)
179{
180 if (pool_head_pid_list != NULL)
181 /* Nothing to do */
182 return 0;
183
184 if (!signal_register_fct(SIGCHLD, sigchld_handler, SIGCHLD)) {
185 ha_alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
186 strerror(errno));
187 return 1;
188 }
189
190 pool_head_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
191 if (pool_head_pid_list == NULL) {
192 ha_alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
193 strerror(errno));
194 return 1;
195 }
196
197 return 0;
198}
199
200/* helper macro to set an environment variable and jump to a specific label on failure. */
201#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
202
203/*
204 * helper function to allocate enough memory to store an environment variable.
205 * It will also check that the environment variable is updatable, and silently
206 * fail if not.
207 */
208static int extchk_setenv(struct check *check, int idx, const char *value)
209{
210 int len, ret;
211 char *envname;
212 int vmaxlen;
213
214 if (idx < 0 || idx >= EXTCHK_SIZE) {
215 ha_alert("Illegal environment variable index %d. Aborting.\n", idx);
216 return 1;
217 }
218
219 envname = extcheck_envs[idx].name;
220 vmaxlen = extcheck_envs[idx].vmaxlen;
221
222 /* Check if the environment variable is already set, and silently reject
223 * the update if this one is not updatable. */
224 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
225 return 0;
226
227 /* Instead of sending NOT_USED, sending an empty value is preferable */
228 if (strcmp(value, "NOT_USED") == 0) {
229 value = "";
230 }
231
232 len = strlen(envname) + 1;
233 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
234 len += strlen(value);
235 else
236 len += vmaxlen;
237
238 if (!check->envp[idx])
239 check->envp[idx] = malloc(len + 1);
240
241 if (!check->envp[idx]) {
242 ha_alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
243 return 1;
244 }
245 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
246 if (ret < 0) {
247 ha_alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
248 return 1;
249 }
250 else if (ret > len) {
251 ha_alert("Environment variable '%s' was truncated. Aborting.\n", envname);
252 return 1;
253 }
254 return 0;
255}
256
257int prepare_external_check(struct check *check)
258{
259 struct server *s = check->server;
260 struct proxy *px = s->proxy;
261 struct listener *listener = NULL, *l;
262 int i;
263 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
264 char buf[256];
265
266 list_for_each_entry(l, &px->conf.listeners, by_fe)
267 /* Use the first INET, INET6 or UNIX listener */
Willy Tarreau37159062020-08-27 07:48:42 +0200268 if (l->rx.addr.ss_family == AF_INET ||
269 l->rx.addr.ss_family == AF_INET6 ||
270 l->rx.addr.ss_family == AF_UNIX) {
Willy Tarreaubcc67332020-06-05 15:31:31 +0200271 listener = l;
272 break;
273 }
274
275 check->curpid = NULL;
Tim Duesterhuse52b6e52020-09-12 20:26:43 +0200276 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(*check->envp));
Willy Tarreaubcc67332020-06-05 15:31:31 +0200277 if (!check->envp) {
278 ha_alert("Failed to allocate memory for environment variables. Aborting\n");
279 goto err;
280 }
281
Tim Duesterhuse52b6e52020-09-12 20:26:43 +0200282 check->argv = calloc(6, sizeof(*check->argv));
Willy Tarreaubcc67332020-06-05 15:31:31 +0200283 if (!check->argv) {
284 ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
285 goto err;
286 }
287
288 check->argv[0] = px->check_command;
289
290 if (!listener) {
291 check->argv[1] = strdup("NOT_USED");
292 check->argv[2] = strdup("NOT_USED");
293 }
Willy Tarreau37159062020-08-27 07:48:42 +0200294 else if (listener->rx.addr.ss_family == AF_INET ||
295 listener->rx.addr.ss_family == AF_INET6) {
296 addr_to_str(&listener->rx.addr, buf, sizeof(buf));
Willy Tarreaubcc67332020-06-05 15:31:31 +0200297 check->argv[1] = strdup(buf);
Willy Tarreau37159062020-08-27 07:48:42 +0200298 port_to_str(&listener->rx.addr, buf, sizeof(buf));
Willy Tarreaubcc67332020-06-05 15:31:31 +0200299 check->argv[2] = strdup(buf);
300 }
Willy Tarreau37159062020-08-27 07:48:42 +0200301 else if (listener->rx.addr.ss_family == AF_UNIX) {
Willy Tarreaubcc67332020-06-05 15:31:31 +0200302 const struct sockaddr_un *un;
303
Willy Tarreau37159062020-08-27 07:48:42 +0200304 un = (struct sockaddr_un *)&listener->rx.addr;
Willy Tarreaubcc67332020-06-05 15:31:31 +0200305 check->argv[1] = strdup(un->sun_path);
306 check->argv[2] = strdup("NOT_USED");
307 }
308 else {
309 ha_alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
310 goto err;
311 }
312
Willy Tarreauc7edc982022-04-14 19:49:50 +0200313 /* args 3 and 4 are the address, they're replaced on each check */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200314 check->argv[3] = calloc(EXTCHK_SIZE_ADDR, sizeof(*check->argv[3]));
315 check->argv[4] = calloc(EXTCHK_SIZE_UINT, sizeof(*check->argv[4]));
Willy Tarreaubcc67332020-06-05 15:31:31 +0200316
317 for (i = 0; i < 5; i++) {
318 if (!check->argv[i]) {
319 ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
320 goto err;
321 }
322 }
323
324 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
325 /* Add proxy environment variables */
326 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
327 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
328 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
329 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
330 /* Add server environment variables */
331 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
332 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
333 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
334 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
335 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
336 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
337
338 /* Ensure that we don't leave any hole in check->envp */
339 for (i = 0; i < EXTCHK_SIZE; i++)
340 if (!check->envp[i])
341 EXTCHK_SETENV(check, i, "", err);
342
343 return 1;
344err:
345 if (check->envp) {
346 for (i = 0; i < EXTCHK_SIZE; i++)
347 free(check->envp[i]);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100348 ha_free(&check->envp);
Willy Tarreaubcc67332020-06-05 15:31:31 +0200349 }
350
351 if (check->argv) {
352 for (i = 1; i < 5; i++)
353 free(check->argv[i]);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100354 ha_free(&check->argv);
Willy Tarreaubcc67332020-06-05 15:31:31 +0200355 }
356 return 0;
357}
358
359/*
360 * establish a server health-check that makes use of a process.
361 *
362 * It can return one of :
363 * - SF_ERR_NONE if everything's OK
364 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
365 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
366 *
367 * Blocks and then unblocks SIGCHLD
368 */
369static int connect_proc_chk(struct task *t)
370{
371 char buf[256];
372 struct check *check = t->context;
373 struct server *s = check->server;
374 struct proxy *px = s->proxy;
375 int status;
376 pid_t pid;
377
378 status = SF_ERR_RESOURCE;
379
380 block_sigchld();
381
382 pid = fork();
383 if (pid < 0) {
384 ha_alert("Failed to fork process for external health check%s: %s. Aborting.\n",
385 (global.tune.options & GTUNE_INSECURE_FORK) ?
386 "" : " (likely caused by missing 'insecure-fork-wanted')",
387 strerror(errno));
388 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
389 goto out;
390 }
391 if (pid == 0) {
392 /* Child */
393 extern char **environ;
394 struct rlimit limit;
395 int fd;
396
397 /* close all FDs. Keep stdin/stdout/stderr in verbose mode */
398 fd = (global.mode & (MODE_QUIET|MODE_VERBOSE)) == MODE_QUIET ? 0 : 3;
399
400 my_closefrom(fd);
401
402 /* restore the initial FD limits */
403 limit.rlim_cur = rlim_fd_cur_at_boot;
404 limit.rlim_max = rlim_fd_max_at_boot;
405 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
406 getrlimit(RLIMIT_NOFILE, &limit);
407 ha_warning("External check: failed to restore initial FD limits (cur=%u max=%u), using cur=%u max=%u\n",
408 rlim_fd_cur_at_boot, rlim_fd_max_at_boot,
409 (unsigned int)limit.rlim_cur, (unsigned int)limit.rlim_max);
410 }
411
412 environ = check->envp;
413
414 /* Update some environment variables and command args: curconn, server addr and server port */
Willy Tarreaub3250a22020-10-24 13:07:39 +0200415 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), fail);
Willy Tarreaubcc67332020-06-05 15:31:31 +0200416
Willy Tarreaucef08c22022-04-14 19:51:02 +0200417 if (s->addr.ss_family == AF_UNIX) {
418 const struct sockaddr_un *un = (struct sockaddr_un *)&s->addr;
419 strlcpy2(check->argv[3], un->sun_path, EXTCHK_SIZE_ADDR);
420 memcpy(check->argv[4], "NOT_USED", 9);
421 } else {
422 addr_to_str(&s->addr, check->argv[3], EXTCHK_SIZE_ADDR);
423 *check->argv[4] = 0; // just in case the address family changed
424 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
425 snprintf(check->argv[4], EXTCHK_SIZE_UINT, "%u", s->svc_port);
426 }
Willy Tarreaubcc67332020-06-05 15:31:31 +0200427
Willy Tarreaucef08c22022-04-14 19:51:02 +0200428 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], fail);
Willy Tarreaub3250a22020-10-24 13:07:39 +0200429 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], fail);
Willy Tarreaubcc67332020-06-05 15:31:31 +0200430
431 haproxy_unblock_signals();
432 execvp(px->check_command, check->argv);
433 ha_alert("Failed to exec process for external health check: %s. Aborting.\n",
434 strerror(errno));
Willy Tarreaub3250a22020-10-24 13:07:39 +0200435 fail:
Willy Tarreaubcc67332020-06-05 15:31:31 +0200436 exit(-1);
437 }
438
439 /* Parent */
440 if (check->result == CHK_RES_UNKNOWN) {
441 if (pid_list_add(pid, t) != NULL) {
442 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
443
444 if (px->timeout.check && px->timeout.connect) {
445 int t_con = tick_add(now_ms, px->timeout.connect);
446 t->expire = tick_first(t->expire, t_con);
447 }
448 status = SF_ERR_NONE;
449 goto out;
450 }
451 else {
452 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
453 }
454 kill(pid, SIGTERM); /* process creation error */
455 }
456 else
457 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
458
459out:
460 unblock_sigchld();
461 return status;
462}
463
464/*
465 * manages a server health-check that uses an external process. Returns
466 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
467 *
468 * Please do NOT place any return statement in this function and only leave
469 * via the out_unlock label.
470 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100471struct task *process_chk_proc(struct task *t, void *context, unsigned int state)
Willy Tarreaubcc67332020-06-05 15:31:31 +0200472{
473 struct check *check = context;
474 struct server *s = check->server;
475 int rv;
476 int ret;
477 int expired = tick_is_expired(t->expire, now_ms);
478
479 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
480 if (!(check->state & CHK_ST_INPROGRESS)) {
481 /* no check currently running */
482 if (!expired) /* woke up too early */
483 goto out_unlock;
484
485 /* we don't send any health-checks when the proxy is
486 * stopped, the server should not be checked or the check
487 * is disabled.
488 */
489 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200490 (s->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Willy Tarreaubcc67332020-06-05 15:31:31 +0200491 goto reschedule;
492
493 /* we'll initiate a new check */
494 set_server_check_status(check, HCHK_STATUS_START, NULL);
495
496 check->state |= CHK_ST_INPROGRESS;
497
498 ret = connect_proc_chk(t);
499 if (ret == SF_ERR_NONE) {
500 /* the process was forked, we allow up to min(inter,
501 * timeout.connect) for it to report its status, but
502 * only when timeout.check is set as it may be to short
503 * for a full check otherwise.
504 */
505 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
506
507 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
508 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
509 t->expire = tick_first(t->expire, t_con);
510 }
511 task_set_affinity(t, tid_bit);
512 goto reschedule;
513 }
514
515 /* here, we failed to start the check */
516
517 check->state &= ~CHK_ST_INPROGRESS;
518 check_notify_failure(check);
519
520 /* we allow up to min(inter, timeout.connect) for a connection
521 * to establish but only when timeout.check is set
522 * as it may be to short for a full check otherwise
523 */
524 while (tick_is_expired(t->expire, now_ms)) {
525 int t_con;
526
527 t_con = tick_add(t->expire, s->proxy->timeout.connect);
528 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
529
530 if (s->proxy->timeout.check)
531 t->expire = tick_first(t->expire, t_con);
532 }
533 }
534 else {
535 /* there was a test running.
536 * First, let's check whether there was an uncaught error,
537 * which can happen on connect timeout or error.
538 */
539 if (check->result == CHK_RES_UNKNOWN) {
540 /* good connection is enough for pure TCP check */
541 struct pid_list *elem = check->curpid;
542 int status = HCHK_STATUS_UNKNOWN;
543
544 if (elem->exited) {
545 status = elem->status; /* Save in case the process exits between use below */
546 if (!WIFEXITED(status))
547 check->code = -1;
548 else
549 check->code = WEXITSTATUS(status);
550 if (!WIFEXITED(status) || WEXITSTATUS(status))
551 status = HCHK_STATUS_PROCERR;
552 else
553 status = HCHK_STATUS_PROCOK;
554 } else if (expired) {
555 status = HCHK_STATUS_PROCTOUT;
556 ha_warning("kill %d\n", (int)elem->pid);
557 kill(elem->pid, SIGTERM);
558 }
559 set_server_check_status(check, status, NULL);
560 }
561
562 if (check->result == CHK_RES_FAILED) {
563 /* a failure or timeout detected */
564 check_notify_failure(check);
565 }
566 else if (check->result == CHK_RES_CONDPASS) {
567 /* check is OK but asks for stopping mode */
568 check_notify_stopping(check);
569 }
570 else if (check->result == CHK_RES_PASSED) {
571 /* a success was detected */
572 check_notify_success(check);
573 }
574 task_set_affinity(t, 1);
575 check->state &= ~CHK_ST_INPROGRESS;
576
577 pid_list_del(check->curpid);
578
579 rv = 0;
580 if (global.spread_checks > 0) {
581 rv = srv_getinter(check) * global.spread_checks / 100;
582 rv -= (int) (2 * rv * (ha_random32() / 4294967295.0));
583 }
584 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
585 }
586
587 reschedule:
588 while (tick_is_expired(t->expire, now_ms))
589 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
590
591 out_unlock:
592 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
593 return t;
594}
595
596/* Parses the "external-check" proxy keyword */
597int proxy_parse_extcheck(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100598 const struct proxy *defpx, const char *file, int line,
Willy Tarreaubcc67332020-06-05 15:31:31 +0200599 char **errmsg)
600{
601 int cur_arg, ret = 0;
602
603 cur_arg = 1;
604 if (!*(args[cur_arg])) {
605 memprintf(errmsg, "missing argument after '%s'.\n", args[0]);
606 goto error;
607 }
608
609 if (strcmp(args[cur_arg], "command") == 0) {
610 if (too_many_args(2, args, errmsg, NULL))
611 goto error;
612 if (!*(args[cur_arg+1])) {
613 memprintf(errmsg, "missing argument after '%s'.", args[cur_arg]);
614 goto error;
615 }
616 free(curpx->check_command);
617 curpx->check_command = strdup(args[cur_arg+1]);
618 }
619 else if (strcmp(args[cur_arg], "path") == 0) {
620 if (too_many_args(2, args, errmsg, NULL))
621 goto error;
622 if (!*(args[cur_arg+1])) {
623 memprintf(errmsg, "missing argument after '%s'.", args[cur_arg]);
624 goto error;
625 }
626 free(curpx->check_path);
627 curpx->check_path = strdup(args[cur_arg+1]);
628 }
629 else {
630 memprintf(errmsg, "'%s' only supports 'command' and 'path'. but got '%s'.",
631 args[0], args[1]);
632 goto error;
633 }
634
635 ret = (*errmsg != NULL); /* Handle warning */
636 return ret;
637
638error:
639 return -1;
640}
641
Willy Tarreau220fd702021-02-12 12:07:38 +0100642int proxy_parse_external_check_opt(char **args, int cur_arg, struct proxy *curpx, const struct proxy *defpx,
Willy Tarreaubcc67332020-06-05 15:31:31 +0200643 const char *file, int line)
644{
645 int err_code = 0;
646
647 curpx->options2 &= ~PR_O2_CHK_ANY;
648 curpx->options2 |= PR_O2_EXT_CHK;
649 if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code))
650 goto out;
651
652 out:
653 return err_code;
654}
655
656static struct cfg_kw_list cfg_kws = {ILH, {
657 { CFG_LISTEN, "external-check", proxy_parse_extcheck },
658 { 0, NULL, NULL },
659}};
660
661INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);