blob: 8411fefae3b301ae47e52c9ed9632a8597936f6f [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreau26c25062009-03-08 09:38:41 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
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
Willy Tarreaub8816082008-01-18 12:18:15 +010014#include <assert.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020015#include <ctype.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <errno.h>
17#include <fcntl.h>
Willy Tarreau9b39dc52014-07-08 00:54:10 +020018#include <signal.h>
Simon Horman0ba0e4a2015-01-30 11:23:00 +090019#include <stdarg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020021#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020023#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <unistd.h>
25#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040026#include <sys/types.h>
Simon Horman98637e52014-06-20 12:30:16 +090027#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020029#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <arpa/inet.h>
31
Willy Tarreauc7e42382012-08-24 19:22:53 +020032#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/compat.h>
34#include <common/config.h>
35#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020036#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
Christopher Fauletcfda8472017-10-20 15:40:23 +020038#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039
40#include <types/global.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020041#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010042#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
44#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
William Lallemand9ed62032016-11-21 17:49:11 +010046#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/fd.h>
48#include <proto/log.h>
49#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020050#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010051#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010052#include <proto/proto_tcp.h>
Baptiste Assmann69e273f2013-12-11 00:52:19 +010053#include <proto/protocol.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010054#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/server.h>
Willy Tarreau48d6bf22016-06-21 16:27:34 +020056#include <proto/signal.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010057#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020059#include <proto/log.h>
60#include <proto/dns.h>
61#include <proto/proto_udp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Olivier Houchard9130a962017-10-17 17:33:43 +020063#ifdef USE_OPENSSL
64#include <proto/ssl_sock.h>
65#endif /* USE_OPENSSL */
66
Willy Tarreaubd741542010-03-16 18:46:54 +010067static int httpchk_expect(struct server *s, int done);
Simon Hormane16c1b32015-01-30 11:22:57 +090068static int tcpcheck_get_step_id(struct check *);
Baptiste Assmann22b09d22015-05-01 08:03:04 +020069static char * tcpcheck_get_step_comment(struct check *, int);
Willy Tarreau6bdcab02017-10-04 18:41:00 +020070static int tcpcheck_main(struct check *);
Willy Tarreaubd741542010-03-16 18:46:54 +010071
Christopher Faulet31dff9b2017-10-23 15:45:20 +020072static struct pool_head *pool2_email_alert = NULL;
73static struct pool_head *pool2_tcpcheck_rule = NULL;
74
75
Simon Horman63a4a822012-03-19 07:24:41 +090076static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010077 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
78 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020079 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020080
Willy Tarreau23964182014-05-20 20:56:30 +020081 /* Below we have finished checks */
82 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010083 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010084
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010085 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020086
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010087 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
88 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
89 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020090
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010091 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
92 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
93 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020094
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010095 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
96 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020097
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020098 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020099
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100100 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
101 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
102 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +0900103
104 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
105 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200106 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200107};
108
Cyril Bontéac92a062014-12-27 22:28:38 +0100109const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = {
110 [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT },
111 [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT },
112 [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT },
113 [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT },
114 [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT },
115 [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT },
116 [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT },
117 [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_EVAL_INIT },
118 [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_EVAL_INIT },
119 [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT },
120 [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG },
121};
122
Simon Horman63a4a822012-03-19 07:24:41 +0900123static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100124 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
125
126 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
127 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
128
129 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
130 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
131 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
132 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
133
134 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
135 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
136 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
137};
138
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200139/*
140 * Convert check_status code to description
141 */
142const char *get_check_status_description(short check_status) {
143
144 const char *desc;
145
146 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200147 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200148 else
149 desc = NULL;
150
151 if (desc && *desc)
152 return desc;
153 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200154 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200155}
156
157/*
158 * Convert check_status code to short info
159 */
160const char *get_check_status_info(short check_status) {
161
162 const char *info;
163
164 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200165 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200166 else
167 info = NULL;
168
169 if (info && *info)
170 return info;
171 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200172 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200173}
174
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100175const char *get_analyze_status(short analyze_status) {
176
177 const char *desc;
178
179 if (analyze_status < HANA_STATUS_SIZE)
180 desc = analyze_statuses[analyze_status].desc;
181 else
182 desc = NULL;
183
184 if (desc && *desc)
185 return desc;
186 else
187 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
188}
189
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200190/*
Simon Horman4a741432013-02-23 15:35:38 +0900191 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200192 * an adequate CHK_RES_* value. The new check->health is computed based
193 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200194 *
195 * Show information in logs about failed health check if server is UP
196 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200197 */
Simon Horman4a741432013-02-23 15:35:38 +0900198static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100199{
Simon Horman4a741432013-02-23 15:35:38 +0900200 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200201 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200202 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900203
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200204 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100205 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900206 check->desc[0] = '\0';
207 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200208 return;
209 }
210
Simon Horman4a741432013-02-23 15:35:38 +0900211 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200212 return;
213
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200214 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900215 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
216 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200217 } else
Simon Horman4a741432013-02-23 15:35:38 +0900218 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200219
Simon Horman4a741432013-02-23 15:35:38 +0900220 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200221 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900222 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200223
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100224 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900225 check->duration = -1;
226 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200227 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900228 check->duration = tv_ms_elapsed(&check->start, &now);
229 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200230 }
231
Willy Tarreau23964182014-05-20 20:56:30 +0200232 /* no change is expected if no state change occurred */
233 if (check->result == CHK_RES_NEUTRAL)
234 return;
235
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200236 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200237
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200238 switch (check->result) {
239 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200240 /* Failure to connect to the agent as a secondary check should not
241 * cause the server to be marked down.
242 */
243 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900244 (check->status >= HCHK_STATUS_L57DATA)) &&
Willy Tarreau12634e12014-05-23 11:32:36 +0200245 (check->health >= check->rise)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +0200246 HA_ATOMIC_ADD(&s->counters.failed_checks, 1);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200247 report = 1;
248 check->health--;
249 if (check->health < check->rise)
250 check->health = 0;
251 }
252 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200253
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200254 case CHK_RES_PASSED:
255 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
256 if ((check->health < check->rise + check->fall - 1) &&
257 (check->result == CHK_RES_PASSED || check->health > 0)) {
258 report = 1;
259 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200260
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200261 if (check->health >= check->rise)
262 check->health = check->rise + check->fall - 1; /* OK now */
263 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200264
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200265 /* clear consecutive_errors if observing is enabled */
266 if (s->onerror)
267 s->consecutive_errors = 0;
268 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100269
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200270 default:
271 break;
272 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200273
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200274 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
275 (status != prev_status || report)) {
276 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200277 "%s check for %sserver %s/%s %s%s",
278 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200279 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100280 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100281 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200282 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200283
Emeric Brun5a133512017-10-19 14:42:30 +0200284 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200285
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100286 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200287 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
288 (check->health >= check->rise) ? check->fall : check->rise,
289 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200290
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100291 Warning("%s.\n", trash.str);
292 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman7ea9be02015-04-30 13:10:33 +0900293 send_email_alert(s, LOG_INFO, "%s", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200294 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200295}
296
Willy Tarreau4eec5472014-05-20 22:32:27 +0200297/* Marks the check <check>'s server down if the current check is already failed
298 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200300static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301{
Simon Horman4a741432013-02-23 15:35:38 +0900302 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900303
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200304 /* The agent secondary check should only cause a server to be marked
305 * as down if check->status is HCHK_STATUS_L7STS, which indicates
306 * that the agent returned "fail", "stopped" or "down".
307 * The implication here is that failure to connect to the agent
308 * as a secondary check should not cause the server to be marked
309 * down. */
310 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
311 return;
312
Willy Tarreau4eec5472014-05-20 22:32:27 +0200313 if (check->health > 0)
314 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100315
Willy Tarreau4eec5472014-05-20 22:32:27 +0200316 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200317 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200318}
319
Willy Tarreauaf549582014-05-16 17:37:50 +0200320/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200321 * it isn't in maintenance, it is not tracking a down server and other checks
322 * comply. The rule is simple : by default, a server is up, unless any of the
323 * following conditions is true :
324 * - health check failed (check->health < rise)
325 * - agent check failed (agent->health < rise)
326 * - the server tracks a down server (track && track->state == STOPPED)
327 * Note that if the server has a slowstart, it will switch to STARTING instead
328 * of RUNNING. Also, only the health checks support the nolb mode, so the
329 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200330 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200331static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200332{
Simon Horman4a741432013-02-23 15:35:38 +0900333 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100334
Emeric Brun52a91d32017-08-31 14:41:55 +0200335 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200336 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100337
Emeric Brun52a91d32017-08-31 14:41:55 +0200338 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200339 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100340
Willy Tarreau3e048382014-05-21 10:30:54 +0200341 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
342 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100343
Willy Tarreau3e048382014-05-21 10:30:54 +0200344 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
345 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200346
Emeric Brun52a91d32017-08-31 14:41:55 +0200347 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200348 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100349
Emeric Brun5a133512017-10-19 14:42:30 +0200350 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100351}
352
Willy Tarreaudb58b792014-05-21 13:57:23 +0200353/* Marks the check <check> as valid and tries to set its server into stopping mode
354 * if it was running or starting, and provided it isn't in maintenance and other
355 * checks comply. The conditions for the server to be marked in stopping mode are
356 * the same as for it to be turned up. Also, only the health checks support the
357 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200358 */
Willy Tarreaudb58b792014-05-21 13:57:23 +0200359static void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200360{
Simon Horman4a741432013-02-23 15:35:38 +0900361 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100362
Emeric Brun52a91d32017-08-31 14:41:55 +0200363 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200364 return;
365
Willy Tarreaudb58b792014-05-21 13:57:23 +0200366 if (check->state & CHK_ST_AGENT)
367 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100368
Emeric Brun52a91d32017-08-31 14:41:55 +0200369 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200370 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100371
Willy Tarreaudb58b792014-05-21 13:57:23 +0200372 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
373 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100374
Willy Tarreaudb58b792014-05-21 13:57:23 +0200375 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
376 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100377
Emeric Brun5a133512017-10-19 14:42:30 +0200378 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100379}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200380
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100381/* note: use health_adjust() only, which first checks that the observe mode is
382 * enabled.
383 */
384void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100385{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100386 int failed;
387 int expire;
388
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100389 if (s->observe >= HANA_OBS_SIZE)
390 return;
391
Willy Tarreaubb956662013-01-24 00:37:39 +0100392 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100393 return;
394
395 switch (analyze_statuses[status].lr[s->observe - 1]) {
396 case 1:
397 failed = 1;
398 break;
399
400 case 2:
401 failed = 0;
402 break;
403
404 default:
405 return;
406 }
407
408 if (!failed) {
409 /* good: clear consecutive_errors */
410 s->consecutive_errors = 0;
411 return;
412 }
413
Christopher Faulet29f77e82017-06-08 14:04:45 +0200414 HA_ATOMIC_ADD(&s->consecutive_errors, 1);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100415
416 if (s->consecutive_errors < s->consecutive_errors_limit)
417 return;
418
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100419 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
420 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100421
422 switch (s->onerror) {
423 case HANA_ONERR_FASTINTER:
424 /* force fastinter - nothing to do here as all modes force it */
425 break;
426
427 case HANA_ONERR_SUDDTH:
428 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900429 if (s->check.health > s->check.rise)
430 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100431
432 /* no break - fall through */
433
434 case HANA_ONERR_FAILCHK:
435 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900436 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200437 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100438 break;
439
440 case HANA_ONERR_MARKDWN:
441 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900442 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900443 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200444 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100445 break;
446
447 default:
448 /* write a warning? */
449 break;
450 }
451
452 s->consecutive_errors = 0;
Christopher Faulet29f77e82017-06-08 14:04:45 +0200453 HA_ATOMIC_ADD(&s->counters.failed_hana, 1);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100454
Simon Horman66183002013-02-23 10:16:43 +0900455 if (s->check.fastinter) {
456 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300457 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200458 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300459 /* requeue check task with new expire */
460 task_queue(s->check.task);
461 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100462 }
463}
464
Willy Tarreaua1dab552014-04-14 15:04:54 +0200465static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100466{
467 int sv_state;
468 int ratio;
469 int hlen = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800470 char addr[46];
471 char port[6];
Willy Tarreauef781042010-01-27 11:53:01 +0100472 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
473 "UP %d/%d", "UP",
474 "NOLB %d/%d", "NOLB",
475 "no check" };
476
477 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
478 hlen += 24;
479
Willy Tarreauff5ae352013-12-11 20:36:34 +0100480 if (!(s->check.state & CHK_ST_ENABLED))
481 sv_state = 6;
Emeric Brun52a91d32017-08-31 14:41:55 +0200482 else if (s->cur_state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900483 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100484 sv_state = 3; /* UP */
485 else
486 sv_state = 2; /* going down */
487
Emeric Brun52a91d32017-08-31 14:41:55 +0200488 if (s->cur_state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100489 sv_state += 2;
490 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900491 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100492 sv_state = 1; /* going up */
493 else
494 sv_state = 0; /* DOWN */
495 }
496
Willy Tarreaua1dab552014-04-14 15:04:54 +0200497 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100498 srv_hlt_st[sv_state],
Emeric Brun52a91d32017-08-31 14:41:55 +0200499 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
500 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100501
Joseph Lynch514061c2015-01-15 17:52:59 -0800502 addr_to_str(&s->addr, addr, sizeof(addr));
Willy Tarreau04276f32017-01-06 17:41:29 +0100503 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
504 snprintf(port, sizeof(port), "%u", s->svc_port);
505 else
506 *port = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800507
508 hlen += snprintf(buffer + hlen, size - hlen, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
509 addr, port, s->proxy->id, s->id,
Willy Tarreauef781042010-01-27 11:53:01 +0100510 global.node,
Emeric Brun52a91d32017-08-31 14:41:55 +0200511 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
Willy Tarreauef781042010-01-27 11:53:01 +0100512 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
513 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
514 s->nbpend);
515
Emeric Brun52a91d32017-08-31 14:41:55 +0200516 if ((s->cur_state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100517 now.tv_sec < s->last_change + s->slowstart &&
518 now.tv_sec >= s->last_change) {
519 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200520 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100521 }
522
523 buffer[hlen++] = '\r';
524 buffer[hlen++] = '\n';
525
526 return hlen;
527}
528
Willy Tarreau20a18342013-12-05 00:31:46 +0100529/* Check the connection. If an error has already been reported or the socket is
530 * closed, keep errno intact as it is supposed to contain the valid error code.
531 * If no error is reported, check the socket's error queue using getsockopt().
532 * Warning, this must be done only once when returning from poll, and never
533 * after an I/O error was attempted, otherwise the error queue might contain
534 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
535 * socket. Returns non-zero if an error was reported, zero if everything is
536 * clean (including a properly closed socket).
537 */
538static int retrieve_errno_from_socket(struct connection *conn)
539{
540 int skerr;
541 socklen_t lskerr = sizeof(skerr);
542
543 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
544 return 1;
545
Willy Tarreau3c728722014-01-23 13:50:42 +0100546 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100547 return 0;
548
Willy Tarreau585744b2017-08-24 14:31:19 +0200549 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100550 errno = skerr;
551
552 if (errno == EAGAIN)
553 errno = 0;
554
555 if (!errno) {
556 /* we could not retrieve an error, that does not mean there is
557 * none. Just don't change anything and only report the prior
558 * error if any.
559 */
560 if (conn->flags & CO_FL_ERROR)
561 return 1;
562 else
563 return 0;
564 }
565
566 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
567 return 1;
568}
569
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100570/* Try to collect as much information as possible on the connection status,
571 * and adjust the server status accordingly. It may make use of <errno_bck>
572 * if non-null when the caller is absolutely certain of its validity (eg:
573 * checked just after a syscall). If the caller doesn't have a valid errno,
574 * it can pass zero, and retrieve_errno_from_socket() will be called to try
575 * to extract errno from the socket. If no error is reported, it will consider
576 * the <expired> flag. This is intended to be used when a connection error was
577 * reported in conn->flags or when a timeout was reported in <expired>. The
578 * function takes care of not updating a server status which was already set.
579 * All situations where at least one of <expired> or CO_FL_ERROR are set
580 * produce a status.
581 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200582static void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100583{
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200584 struct connection *conn = check->conn;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100585 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200586 struct chunk *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200587 int step;
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200588 char *comment;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100589
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100590 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100591 return;
592
593 errno = errno_bck;
Willy Tarreau00149122017-10-04 18:05:01 +0200594 if (conn && (!errno || errno == EAGAIN))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100595 retrieve_errno_from_socket(conn);
596
Willy Tarreau00149122017-10-04 18:05:01 +0200597 if (conn && !(conn->flags & CO_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100598 return;
599
600 /* we'll try to build a meaningful error message depending on the
601 * context of the error possibly present in conn->err_code, and the
602 * socket error possibly collected above. This is useful to know the
603 * exact step of the L6 layer (eg: SSL handshake).
604 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200605 chk = get_trash_chunk();
606
607 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormane16c1b32015-01-30 11:22:57 +0900608 step = tcpcheck_get_step_id(check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200609 if (!step)
610 chunk_printf(chk, " at initial connection step of tcp-check");
611 else {
612 chunk_printf(chk, " at step %d of tcp-check", step);
613 /* we were looking for a string */
614 if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) {
615 if (check->last_started_step->port)
616 chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port);
617 else
618 chunk_appendf(chk, " (connect)");
619 }
620 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) {
621 if (check->last_started_step->string)
Baptiste Assmann96a5c9b2015-05-01 08:09:29 +0200622 chunk_appendf(chk, " (expect string '%s')", check->last_started_step->string);
Willy Tarreau213c6782014-10-02 14:51:02 +0200623 else if (check->last_started_step->expect_regex)
624 chunk_appendf(chk, " (expect regex)");
625 }
626 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) {
627 chunk_appendf(chk, " (send)");
628 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200629
630 comment = tcpcheck_get_step_comment(check, step);
631 if (comment)
632 chunk_appendf(chk, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200633 }
634 }
635
Willy Tarreau00149122017-10-04 18:05:01 +0200636 if (conn && conn->err_code) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100637 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200638 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100639 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200640 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100641 err_msg = trash.str;
642 }
643 else {
644 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200645 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100646 err_msg = trash.str;
647 }
648 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200649 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100650 }
651 }
652
Emeric Brun9f0b4582017-10-23 14:39:51 +0200653 SPIN_LOCK(SERVER_LOCK, &check->server->lock);
Willy Tarreau00149122017-10-04 18:05:01 +0200654 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200655 /* NOTE: this is reported after <fall> tries */
656 chunk_printf(chk, "No port available for the TCP connection");
657 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
658 }
659
Willy Tarreau00149122017-10-04 18:05:01 +0200660 if (!conn) {
661 /* connection allocation error before the connection was established */
662 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
663 }
664 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100665 /* L4 not established (yet) */
666 if (conn->flags & CO_FL_ERROR)
667 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
668 else if (expired)
669 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200670
671 /*
672 * might be due to a server IP change.
673 * Let's trigger a DNS resolution if none are currently running.
674 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200675 dns_trigger_resolution(check->server->dns_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200676
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100677 }
678 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
679 /* L6 not established (yet) */
680 if (conn->flags & CO_FL_ERROR)
681 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
682 else if (expired)
683 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
684 }
685 else if (conn->flags & CO_FL_ERROR) {
686 /* I/O error after connection was established and before we could diagnose */
687 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
688 }
689 else if (expired) {
690 /* connection established but expired check */
691 if (check->type == PR_O2_SSL3_CHK)
692 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
693 else /* HTTP, SMTP, ... */
694 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
695 }
Emeric Brun9f0b4582017-10-23 14:39:51 +0200696 SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100697
698 return;
699}
700
Willy Tarreaubaaee002006-06-26 02:48:02 +0200701/*
702 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200703 * the connection acknowledgement. If the proxy requires L7 health-checks,
704 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900705 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200707static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708{
Simon Horman4a741432013-02-23 15:35:38 +0900709 struct check *check = conn->owner;
710 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900711 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200712
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100713 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100714 goto out_wakeup;
715
Willy Tarreau310987a2014-01-22 19:46:33 +0100716 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100717 return;
718
Willy Tarreau20a18342013-12-05 00:31:46 +0100719 if (retrieve_errno_from_socket(conn)) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200720 chk_report_conn_err(check, errno, 0);
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200721 __conn_xprt_stop_both(conn);
Willy Tarreau20a18342013-12-05 00:31:46 +0100722 goto out_wakeup;
723 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100724
Willy Tarreaubbae3f02017-08-30 09:59:52 +0200725 if (conn->flags & CO_FL_SOCK_WR_SH) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100726 /* if the output is closed, we can't do anything */
727 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200728 chk_report_conn_err(check, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100729 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200730 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200731
Willy Tarreau06559ac2013-12-05 01:53:08 +0100732 /* here, we know that the connection is established. That's enough for
733 * a pure TCP check.
734 */
735 if (!check->type)
736 goto out_wakeup;
737
Willy Tarreauc09572f2017-10-04 11:58:22 +0200738 /* wake() will take care of calling tcpcheck_main() */
739 if (check->type == PR_O2_TCPCHK_CHK)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200740 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200741
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100742 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100743 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100744 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200745 chk_report_conn_err(check, errno, 0);
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200746 __conn_xprt_stop_both(conn);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100747 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200748 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100749 if (check->bo->o)
750 return;
751 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200752
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100753 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
754 if (s->proxy->timeout.check) {
755 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
756 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100758 goto out_nowake;
759
Willy Tarreau83749182007-04-15 20:56:27 +0200760 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200761 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200762 out_nowake:
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200763 __conn_xprt_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764}
765
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200767 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200768 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900769 * set_server_check_status() to update check->status, check->duration
770 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200771
772 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
773 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
774 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
775 * response to an SSL HELLO (the principle is that this is enough to
776 * distinguish between an SSL server and a pure TCP relay). All other cases will
777 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
778 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200780static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200781{
Simon Horman4a741432013-02-23 15:35:38 +0900782 struct check *check = conn->owner;
783 struct server *s = check->server;
784 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200785 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100786 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200787 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200788
Emeric Brun9f0b4582017-10-23 14:39:51 +0200789 SPIN_LOCK(SERVER_LOCK, &check->server->lock);
790
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100791 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200792 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200793
Emeric Brun9f0b4582017-10-23 14:39:51 +0200794 if (conn->flags & CO_FL_HANDSHAKE) {
795 SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200796 return;
Emeric Brun9f0b4582017-10-23 14:39:51 +0200797 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200798
Willy Tarreauc09572f2017-10-04 11:58:22 +0200799 /* wake() will take care of calling tcpcheck_main() */
Emeric Brun9f0b4582017-10-23 14:39:51 +0200800 if (check->type == PR_O2_TCPCHK_CHK) {
801 SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200802 return;
Emeric Brun9f0b4582017-10-23 14:39:51 +0200803 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200804
Willy Tarreau83749182007-04-15 20:56:27 +0200805 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
806 * but the connection was closed on the remote end. Fortunately, recv still
807 * works correctly and we don't need to do the getsockopt() on linux.
808 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000809
810 /* Set buffer to point to the end of the data already read, and check
811 * that there is free space remaining. If the buffer is full, proceed
812 * with running the checks without attempting another socket read.
813 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000814
Willy Tarreau03938182010-03-17 21:52:07 +0100815 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000816
Simon Horman4a741432013-02-23 15:35:38 +0900817 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreau54e917c2017-08-30 07:35:35 +0200818 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100819 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900820 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200821 /* Report network errors only if we got no other data. Otherwise
822 * we'll let the upper layers decide whether the response is OK
823 * or not. It is very common that an RST sent by the server is
824 * reported as an error just after the last data chunk.
825 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200826 chk_report_conn_err(check, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100827 goto out_wakeup;
828 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829 }
830
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100831
Willy Tarreau03938182010-03-17 21:52:07 +0100832 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900833 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100834 */
Simon Horman4a741432013-02-23 15:35:38 +0900835 if (check->bi->i < check->bi->size)
836 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100837 else {
Simon Horman4a741432013-02-23 15:35:38 +0900838 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100839 done = 1; /* buffer full, don't wait for more data */
840 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200841
Nick Chalk57b1bf72010-03-16 15:50:46 +0000842 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900843 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200844 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900845 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100846 goto wait_more_data;
847
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100848 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900849 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
850 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
851 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
852 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
853 !isdigit((unsigned char) *(check->bi->data + 11))) {
854 cut_crlf(check->bi->data);
855 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200856
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100857 goto out_wakeup;
858 }
859
Simon Horman4a741432013-02-23 15:35:38 +0900860 check->code = str2uic(check->bi->data + 9);
861 desc = ltrim(check->bi->data + 12, ' ');
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200862
Willy Tarreaubd741542010-03-16 18:46:54 +0100863 if ((s->proxy->options & PR_O_DISABLE404) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200864 (s->next_state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000865 /* 404 may be accepted as "stopping" only if the server was up */
866 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900867 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000868 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100869 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
870 /* Run content verification check... We know we have at least 13 chars */
871 if (!httpchk_expect(s, done))
872 goto wait_more_data;
873 }
874 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900875 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100876 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900877 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100878 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000879 else {
880 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900881 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000882 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200883 break;
884
885 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900886 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100887 goto wait_more_data;
888
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100889 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900890 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
891 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200892 else
Simon Horman4a741432013-02-23 15:35:38 +0900893 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200894 break;
895
896 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900897 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100898 goto wait_more_data;
899
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200900 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900901 if ((check->bi->i < strlen("000\r")) ||
902 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
903 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
904 !isdigit((unsigned char) *(check->bi->data + 2))) {
905 cut_crlf(check->bi->data);
906 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200907 goto out_wakeup;
908 }
909
Simon Horman4a741432013-02-23 15:35:38 +0900910 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200911
Simon Horman4a741432013-02-23 15:35:38 +0900912 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200913 cut_crlf(desc);
914
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100915 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900916 if (*check->bi->data == '2')
917 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200918 else
Simon Horman4a741432013-02-23 15:35:38 +0900919 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200920 break;
921
Simon Hormana2b9dad2013-02-12 10:45:54 +0900922 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100923 int status = HCHK_STATUS_CHECKED;
924 const char *hs = NULL; /* health status */
925 const char *as = NULL; /* admin status */
926 const char *ps = NULL; /* performance status */
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200927 const char *cs = NULL; /* maxconn */
Willy Tarreau81f5d942013-12-09 20:51:51 +0100928 const char *err = NULL; /* first error to report */
929 const char *wrn = NULL; /* first warning to report */
930 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900931
Willy Tarreau81f5d942013-12-09 20:51:51 +0100932 /* We're getting an agent check response. The agent could
933 * have been disabled in the mean time with a long check
934 * still pending. It is important that we ignore the whole
935 * response.
936 */
937 if (!(check->server->agent.state & CHK_ST_ENABLED))
938 break;
939
940 /* The agent supports strings made of a single line ended by the
941 * first CR ('\r') or LF ('\n'). This line is composed of words
942 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
943 * line may optionally contained a description of a state change
944 * after a sharp ('#'), which is only considered if a health state
945 * is announced.
946 *
947 * Words may be composed of :
948 * - a numeric weight suffixed by the percent character ('%').
949 * - a health status among "up", "down", "stopped", and "fail".
950 * - an admin status among "ready", "drain", "maint".
951 *
952 * These words may appear in any order. If multiple words of the
953 * same category appear, the last one wins.
954 */
955
Willy Tarreau9809b782013-12-11 21:40:11 +0100956 p = check->bi->data;
957 while (*p && *p != '\n' && *p != '\r')
958 p++;
959
960 if (!*p) {
961 if (!done)
962 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900963
Willy Tarreau9809b782013-12-11 21:40:11 +0100964 /* at least inform the admin that the agent is mis-behaving */
965 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
966 break;
967 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100968
Willy Tarreau9809b782013-12-11 21:40:11 +0100969 *p = 0;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100970 cmd = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900971
Willy Tarreau81f5d942013-12-09 20:51:51 +0100972 while (*cmd) {
973 /* look for next word */
974 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
975 cmd++;
976 continue;
977 }
Simon Horman671b6f02013-11-25 10:46:39 +0900978
Willy Tarreau81f5d942013-12-09 20:51:51 +0100979 if (*cmd == '#') {
980 /* this is the beginning of a health status description,
981 * skip the sharp and blanks.
982 */
983 cmd++;
984 while (*cmd == '\t' || *cmd == ' ')
985 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +0900986 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900987 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100988
989 /* find the end of the word so that we have a null-terminated
990 * word between <cmd> and <p>.
991 */
992 p = cmd + 1;
993 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
994 p++;
995 if (*p)
996 *p++ = 0;
997
998 /* first, health statuses */
999 if (strcasecmp(cmd, "up") == 0) {
1000 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001001 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +01001002 hs = cmd;
1003 }
1004 else if (strcasecmp(cmd, "down") == 0) {
1005 check->health = 0;
1006 status = HCHK_STATUS_L7STS;
1007 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001008 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001009 else if (strcasecmp(cmd, "stopped") == 0) {
1010 check->health = 0;
1011 status = HCHK_STATUS_L7STS;
1012 hs = cmd;
1013 }
1014 else if (strcasecmp(cmd, "fail") == 0) {
1015 check->health = 0;
1016 status = HCHK_STATUS_L7STS;
1017 hs = cmd;
1018 }
1019 /* admin statuses */
1020 else if (strcasecmp(cmd, "ready") == 0) {
1021 as = cmd;
1022 }
1023 else if (strcasecmp(cmd, "drain") == 0) {
1024 as = cmd;
1025 }
1026 else if (strcasecmp(cmd, "maint") == 0) {
1027 as = cmd;
1028 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001029 /* try to parse a weight here and keep the last one */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001030 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
1031 ps = cmd;
1032 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001033 /* try to parse a maxconn here */
1034 else if (strncasecmp(cmd, "maxconn:", strlen("maxconn:")) == 0) {
1035 cs = cmd;
1036 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001037 else {
1038 /* keep a copy of the first error */
1039 if (!err)
1040 err = cmd;
1041 }
1042 /* skip to next word */
1043 cmd = p;
1044 }
1045 /* here, cmd points either to \0 or to the beginning of a
1046 * description. Skip possible leading spaces.
1047 */
1048 while (*cmd == ' ' || *cmd == '\n')
1049 cmd++;
1050
1051 /* First, update the admin status so that we avoid sending other
1052 * possibly useless warnings and can also update the health if
1053 * present after going back up.
1054 */
1055 if (as) {
1056 if (strcasecmp(as, "drain") == 0)
1057 srv_adm_set_drain(check->server);
1058 else if (strcasecmp(as, "maint") == 0)
1059 srv_adm_set_maint(check->server);
1060 else
1061 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001062 }
1063
Willy Tarreau81f5d942013-12-09 20:51:51 +01001064 /* now change weights */
1065 if (ps) {
1066 const char *msg;
1067
1068 msg = server_parse_weight_change_request(s, ps);
1069 if (!wrn || !*wrn)
1070 wrn = msg;
1071 }
1072
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001073 if (cs) {
1074 const char *msg;
1075
1076 cs += strlen("maxconn:");
1077
1078 msg = server_parse_maxconn_change_request(s, cs);
1079 if (!wrn || !*wrn)
1080 wrn = msg;
1081 }
1082
Willy Tarreau81f5d942013-12-09 20:51:51 +01001083 /* and finally health status */
1084 if (hs) {
1085 /* We'll report some of the warnings and errors we have
1086 * here. Down reports are critical, we leave them untouched.
1087 * Lack of report, or report of 'UP' leaves the room for
1088 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001089 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001090 const char *msg = cmd;
1091 struct chunk *t;
1092
1093 if (!*msg || status == HCHK_STATUS_L7OKD) {
1094 if (err && *err)
1095 msg = err;
1096 else if (wrn && *wrn)
1097 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001098 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001099
1100 t = get_trash_chunk();
1101 chunk_printf(t, "via agent : %s%s%s%s",
1102 hs, *msg ? " (" : "",
1103 msg, *msg ? ")" : "");
1104
1105 set_server_check_status(check, status, t->str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001106 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001107 else if (err && *err) {
1108 /* No status change but we'd like to report something odd.
1109 * Just report the current state and copy the message.
1110 */
1111 chunk_printf(&trash, "agent reports an error : %s", err);
1112 set_server_check_status(check, status/*check->status*/, trash.str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001113
Willy Tarreau81f5d942013-12-09 20:51:51 +01001114 }
1115 else if (wrn && *wrn) {
1116 /* No status change but we'd like to report something odd.
1117 * Just report the current state and copy the message.
1118 */
1119 chunk_printf(&trash, "agent warns : %s", wrn);
1120 set_server_check_status(check, status/*check->status*/, trash.str);
1121 }
1122 else
1123 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001124 break;
1125 }
1126
Willy Tarreau1620ec32011-08-06 17:05:02 +02001127 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001128 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001129 goto wait_more_data;
1130
Simon Horman4a741432013-02-23 15:35:38 +09001131 if (check->bi->data[0] == 'R') {
1132 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001133 }
1134 else {
Simon Horman4a741432013-02-23 15:35:38 +09001135 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1136 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001137 else
1138 desc = "PostgreSQL unknown error";
1139
Simon Horman4a741432013-02-23 15:35:38 +09001140 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001141 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001142 break;
1143
1144 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001145 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001146 goto wait_more_data;
1147
Simon Horman4a741432013-02-23 15:35:38 +09001148 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1149 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001150 }
1151 else {
Simon Horman4a741432013-02-23 15:35:38 +09001152 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001153 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001154 break;
1155
1156 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001157 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001158 goto wait_more_data;
1159
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001160 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001161 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001162 /* We set the MySQL Version in description for information purpose
1163 * FIXME : it can be cool to use MySQL Version for other purpose,
1164 * like mark as down old MySQL server.
1165 */
Simon Horman4a741432013-02-23 15:35:38 +09001166 if (check->bi->i > 51) {
1167 desc = ltrim(check->bi->data + 5, ' ');
1168 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001169 }
1170 else {
1171 if (!done)
1172 goto wait_more_data;
Emeric Brun9f0b4582017-10-23 14:39:51 +02001173
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001174 /* it seems we have a OK packet but without a valid length,
1175 * it must be a protocol error
1176 */
Simon Horman4a741432013-02-23 15:35:38 +09001177 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001178 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001179 }
1180 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001181 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001182 desc = ltrim(check->bi->data + 7, ' ');
1183 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001184 }
1185 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001186 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1187 (((unsigned int) *(check->bi->data + 1)) << 8) +
1188 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001189
Simon Horman4a741432013-02-23 15:35:38 +09001190 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001191 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001192 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001193 /* We have only one MySQL packet and it is a Handshake Initialization packet
1194 * but we need to have a second packet to know if it is alright
1195 */
Simon Horman4a741432013-02-23 15:35:38 +09001196 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001197 goto wait_more_data;
1198 }
1199 else {
1200 /* We have only one packet and it is an Error packet,
1201 * an error message is attached, so we can display it
1202 */
Simon Horman4a741432013-02-23 15:35:38 +09001203 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001204 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001205 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001206 }
Simon Horman4a741432013-02-23 15:35:38 +09001207 } else if (check->bi->i > first_packet_len + 4) {
1208 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1209 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1210 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001211
Simon Horman4a741432013-02-23 15:35:38 +09001212 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001213 /* We have 2 packets and that's good */
1214 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001215 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001216 /* No error packet */
1217 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001218 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001219 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001220 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001221 }
1222 else {
1223 /* An error message is attached in the Error packet
1224 * so we can display it ! :)
1225 */
Simon Horman4a741432013-02-23 15:35:38 +09001226 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001227 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001228 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001229 }
1230 }
1231 }
1232 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001233 if (!done)
1234 goto wait_more_data;
Emeric Brun9f0b4582017-10-23 14:39:51 +02001235
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001236 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001237 * it must be a protocol error
1238 */
Simon Horman4a741432013-02-23 15:35:38 +09001239 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001240 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001241 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001242 }
1243 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001244 break;
1245
1246 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001247 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001248 goto wait_more_data;
1249
1250 /* Check if the server speaks LDAP (ASN.1/BER)
1251 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1252 * http://tools.ietf.org/html/rfc4511
1253 */
1254
1255 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1256 * LDAPMessage: 0x30: SEQUENCE
1257 */
Simon Horman4a741432013-02-23 15:35:38 +09001258 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1259 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001260 }
1261 else {
1262 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001263 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001264
1265 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1266 * messageID: 0x02 0x01 0x01: INTEGER 1
1267 * protocolOp: 0x61: bindResponse
1268 */
1269 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001270 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1271 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001272 goto out_wakeup;
1273 }
1274
1275 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001276 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001277
1278 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1279 * ldapResult: 0x0a 0x01: ENUMERATION
1280 */
1281 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001282 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1283 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001284 goto out_wakeup;
1285 }
1286
1287 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1288 * resultCode
1289 */
Simon Horman4a741432013-02-23 15:35:38 +09001290 check->code = *(check->bi->data + msglen + 9);
1291 if (check->code) {
1292 set_server_check_status(check, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001293 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001294 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001295 }
1296 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001297 break;
1298
Christopher Fauletba7bc162016-11-07 21:07:38 +01001299 case PR_O2_SPOP_CHK: {
1300 unsigned int framesz;
1301 char err[HCHK_DESC_LEN];
1302
1303 if (!done && check->bi->i < 4)
1304 goto wait_more_data;
1305
1306 memcpy(&framesz, check->bi->data, 4);
1307 framesz = ntohl(framesz);
1308
1309 if (!done && check->bi->i < (4+framesz))
1310 goto wait_more_data;
1311
Christopher Faulet8ef75252017-02-20 22:56:03 +01001312 if (!spoe_handle_healthcheck_response(check->bi->data+4, framesz, err, HCHK_DESC_LEN-1))
Christopher Fauletba7bc162016-11-07 21:07:38 +01001313 set_server_check_status(check, HCHK_STATUS_L7OKD, "SPOA server is ok");
1314 else
1315 set_server_check_status(check, HCHK_STATUS_L7STS, err);
1316 break;
1317 }
1318
Willy Tarreau1620ec32011-08-06 17:05:02 +02001319 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001320 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001321 break;
1322 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001323
Emeric Brun9f0b4582017-10-23 14:39:51 +02001324 SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001325 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001326 /* collect possible new errors */
1327 if (conn->flags & CO_FL_ERROR)
Willy Tarreaub5259bf2017-10-04 14:47:29 +02001328 chk_report_conn_err(check, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001329
Nick Chalk57b1bf72010-03-16 15:50:46 +00001330 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001331 *check->bi->data = '\0';
1332 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001333
Steven Davidovitz544d4812017-03-08 11:06:20 -08001334 /* Close the connection... We still attempt to nicely close if,
1335 * for instance, SSL needs to send a "close notify." Later, we perform
1336 * a hard close and reset the connection if some data are pending,
1337 * otherwise we end up with many TIME_WAITs and eat all the source port
1338 * range quickly. To avoid sending RSTs all the time, we first try to
1339 * drain pending data.
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001340 */
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001341 __conn_xprt_stop_both(conn);
1342 conn_xprt_shutw(conn);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001343
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001344 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001345 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001346 conn->flags |= CO_FL_ERROR;
1347
Willy Tarreaufdccded2008-08-29 18:19:04 +02001348 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001349 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001350
1351 wait_more_data:
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001352 __conn_xprt_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001353}
1354
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001355/*
1356 * This function is used only for server health-checks. It handles connection
1357 * status updates including errors. If necessary, it wakes the check task up.
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001358 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1359 * connection (eg: reconnect).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001360 */
1361static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001362{
Simon Horman4a741432013-02-23 15:35:38 +09001363 struct check *check = conn->owner;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001364 int ret = 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001365
Willy Tarreauc09572f2017-10-04 11:58:22 +02001366 /* we may have to make progress on the TCP checks */
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001367 if (check->type == PR_O2_TCPCHK_CHK) {
1368 ret = tcpcheck_main(check);
Willy Tarreau00149122017-10-04 18:05:01 +02001369 conn = check->conn;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001370 }
Willy Tarreauc09572f2017-10-04 11:58:22 +02001371
Willy Tarreau6c560da2012-11-24 11:14:45 +01001372 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001373 /* We may get error reports bypassing the I/O handlers, typically
1374 * the case when sending a pure TCP check which fails, then the I/O
1375 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001376 * main processing task so let's simply wake it up. If we get here,
1377 * we expect errno to still be valid.
1378 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +02001379 chk_report_conn_err(check, errno, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001380
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001381 __conn_xprt_stop_both(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001382 task_wakeup(check->task, TASK_WOKEN_IO);
1383 }
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001384 else if (!(conn->flags & (CO_FL_XPRT_RD_ENA|CO_FL_XPRT_WR_ENA|CO_FL_HANDSHAKE))) {
Willy Tarreau3be293f2014-02-05 18:31:24 +01001385 /* we may get here if only a connection probe was required : we
1386 * don't have any data to send nor anything expected in response,
1387 * so the completion of the connection establishment is enough.
1388 */
1389 task_wakeup(check->task, TASK_WOKEN_IO);
1390 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001391
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001392 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001393 /* We're here because nobody wants to handle the error, so we
1394 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001395 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01001396 conn_sock_drain(conn);
Willy Tarreau402dbc12017-10-05 17:53:13 +02001397 conn_full_close(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001398 ret = -1;
Willy Tarreau2d351b62013-12-05 02:36:25 +01001399 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001400
1401 /* if a connection got replaced, we must absolutely prevent the connection
1402 * handler from touching its fd, and perform the FD polling updates ourselves
1403 */
1404 if (ret < 0)
1405 conn_cond_update_polling(conn);
1406
1407 return ret;
Willy Tarreau20bea422012-07-06 12:00:49 +02001408}
1409
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001410struct data_cb check_conn_cb = {
1411 .recv = event_srv_chk_r,
1412 .send = event_srv_chk_w,
1413 .wake = wake_srv_chk,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01001414 .name = "CHCK",
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001415};
1416
Willy Tarreaubaaee002006-06-26 02:48:02 +02001417/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001418 * updates the server's weight during a warmup stage. Once the final weight is
1419 * reached, the task automatically stops. Note that any server status change
1420 * must have updated s->last_change accordingly.
1421 */
1422static struct task *server_warmup(struct task *t)
1423{
1424 struct server *s = t->context;
1425
1426 /* by default, plan on stopping the task */
1427 t->expire = TICK_ETERNITY;
Emeric Brun52a91d32017-08-31 14:41:55 +02001428 if ((s->next_admin & SRV_ADMF_MAINT) ||
1429 (s->next_state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001430 return t;
1431
Willy Tarreau892337c2014-05-13 23:41:20 +02001432 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001433 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001434
1435 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001436 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001437
1438 /* get back there in 1 second or 1/20th of the slowstart interval,
1439 * whichever is greater, resulting in small 5% steps.
1440 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001441 if (s->next_state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001442 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1443 return t;
1444}
1445
Willy Tarreau894c6422017-10-04 15:58:52 +02001446/* returns the first NON-COMMENT tcp-check rule from list <list> or NULL if
1447 * none was found.
1448 */
1449static struct tcpcheck_rule *get_first_tcpcheck_rule(struct list *list)
1450{
1451 struct tcpcheck_rule *r;
1452
1453 list_for_each_entry(r, list, list) {
1454 if (r->action != TCPCHK_ACT_COMMENT)
1455 return r;
1456 }
1457 return NULL;
1458}
1459
Willy Tarreau2e993902011-10-31 11:53:20 +01001460/*
Simon Horman98637e52014-06-20 12:30:16 +09001461 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001462 *
1463 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001464 * - SF_ERR_NONE if everything's OK and tcpcheck_main() was not called
1465 * - SF_ERR_UP if if everything's OK and tcpcheck_main() was called
1466 * - SF_ERR_SRVTO if there are no more servers
1467 * - SF_ERR_SRVCL if the connection was refused by the server
1468 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1469 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1470 * - SF_ERR_INTERNAL for any other purely internal errors
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001471 * - SF_ERR_CHK_PORT if no port could be found to run a health check on an AF_INET* socket
Tim Düsterhus4896c442016-11-29 02:15:19 +01001472 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001473 * Note that we try to prevent the network stack from sending the ACK during the
1474 * connect() when a pure TCP check is used (without PROXY protocol).
1475 */
Simon Horman98637e52014-06-20 12:30:16 +09001476static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001477{
1478 struct check *check = t->context;
1479 struct server *s = check->server;
1480 struct connection *conn = check->conn;
1481 struct protocol *proto;
Willy Tarreauf411cce2017-10-04 16:21:19 +02001482 struct tcpcheck_rule *tcp_rule = NULL;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001483 int ret;
Willy Tarreauf3d34822014-12-08 12:11:28 +01001484 int quickack;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001485
Willy Tarreau00149122017-10-04 18:05:01 +02001486 /* we cannot have a connection here */
1487 if (conn)
1488 return SF_ERR_INTERNAL;
1489
Simon Hormanb00d17a2014-06-13 16:18:16 +09001490 /* tcpcheck send/expect initialisation */
Willy Tarreauf411cce2017-10-04 16:21:19 +02001491 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001492 check->current_step = NULL;
Willy Tarreauf411cce2017-10-04 16:21:19 +02001493 tcp_rule = get_first_tcpcheck_rule(check->tcpcheck_rules);
1494 }
Simon Hormanb00d17a2014-06-13 16:18:16 +09001495
1496 /* prepare the check buffer.
1497 * This should not be used if check is the secondary agent check
1498 * of a server as s->proxy->check_req will relate to the
1499 * configuration of the primary check. Similarly, tcp-check uses
1500 * its own strings.
1501 */
1502 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1503 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1504
1505 /* we want to check if this host replies to HTTP or SSLv3 requests
1506 * so we'll send the request, and won't wake the checker up now.
1507 */
1508 if ((check->type) == PR_O2_SSL3_CHK) {
1509 /* SSL requires that we put Unix time in the request */
1510 int gmt_time = htonl(date.tv_sec);
1511 memcpy(check->bo->data + 11, &gmt_time, 4);
1512 }
1513 else if ((check->type) == PR_O2_HTTP_CHK) {
1514 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1515 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
Cyril Bonté32602d22015-01-30 00:07:07 +01001516 /* prevent HTTP keep-alive when "http-check expect" is used */
1517 if (s->proxy->options2 & PR_O2_EXP_TYPE)
1518 bo_putstr(check->bo, "Connection: close\r\n");
Simon Hormanb00d17a2014-06-13 16:18:16 +09001519 bo_putstr(check->bo, "\r\n");
1520 *check->bo->p = '\0'; /* to make gdb output easier to read */
1521 }
1522 }
1523
James Brown55f9ff12015-10-21 18:19:05 -07001524 if ((check->type & PR_O2_LB_AGENT_CHK) && check->send_string_len) {
1525 bo_putblk(check->bo, check->send_string, check->send_string_len);
1526 }
1527
Willy Tarreauf411cce2017-10-04 16:21:19 +02001528 /* for tcp-checks, the initial connection setup is handled separately as
1529 * it may be sent to a specific port and not to the server's.
1530 */
1531 if (tcp_rule && tcp_rule->action == TCPCHK_ACT_CONNECT) {
1532 tcpcheck_main(check);
1533 return SF_ERR_UP;
1534 }
1535
Simon Hormanb00d17a2014-06-13 16:18:16 +09001536 /* prepare a new connection */
Willy Tarreau00149122017-10-04 18:05:01 +02001537 conn = check->conn = conn_new();
1538 if (!check->conn)
1539 return SF_ERR_RESOURCE;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001540
Simon Horman41f58762015-01-30 11:22:56 +09001541 if (is_addr(&check->addr)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001542 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09001543 conn->addr.to = check->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001544 }
1545 else {
1546 /* we'll connect to the addr on the server */
1547 conn->addr.to = s->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001548 }
1549
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001550 if ((conn->addr.to.ss_family == AF_INET) || (conn->addr.to.ss_family == AF_INET6)) {
1551 int i = 0;
1552
1553 i = srv_check_healthcheck_port(check);
1554 if (i == 0) {
1555 conn->owner = check;
1556 return SF_ERR_CHK_PORT;
1557 }
1558
1559 set_host_port(&conn->addr.to, i);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001560 }
1561
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001562 proto = protocol_by_family(conn->addr.to.ss_family);
1563
1564 conn_prepare(conn, proto, check->xprt);
1565 conn_attach(conn, check, &check_conn_cb);
1566 conn->target = &s->obj_type;
1567
1568 /* no client address */
1569 clear_addr(&conn->addr.from);
1570
Willy Tarreauf3d34822014-12-08 12:11:28 +01001571 /* only plain tcp-check supports quick ACK */
1572 quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK;
1573
Willy Tarreauf411cce2017-10-04 16:21:19 +02001574 if (tcp_rule && tcp_rule->action == TCPCHK_ACT_EXPECT)
1575 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001576
Willy Tarreaue7dff022015-04-03 01:14:29 +02001577 ret = SF_ERR_INTERNAL;
Olivier Houchardb68fda42017-08-04 18:39:01 +02001578 if (proto && proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001579 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Olivier Houchard9130a962017-10-17 17:33:43 +02001580#ifdef USE_OPENSSL
1581 if (s->check.sni)
1582 ssl_sock_set_servername(conn, s->check.sni);
1583#endif
Willy Tarreauf4949772017-05-06 08:45:28 +02001584 if (s->check.send_proxy && !(check->state & CHK_ST_AGENT)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001585 conn->send_proxy_ofs = 1;
1586 conn->flags |= CO_FL_SEND_PROXY;
1587 }
1588
1589 return ret;
1590}
1591
Simon Horman98637e52014-06-20 12:30:16 +09001592static struct list pid_list = LIST_HEAD_INIT(pid_list);
1593static struct pool_head *pool2_pid_list;
Christopher Fauletcfda8472017-10-20 15:40:23 +02001594#ifdef USE_THREAD
1595HA_SPINLOCK_T pid_list_lock;
1596#endif
Simon Horman98637e52014-06-20 12:30:16 +09001597
1598void block_sigchld(void)
1599{
1600 sigset_t set;
1601 sigemptyset(&set);
1602 sigaddset(&set, SIGCHLD);
Willy Tarreauebc92442016-06-21 17:29:46 +02001603 assert(sigprocmask(SIG_BLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001604}
1605
1606void unblock_sigchld(void)
1607{
1608 sigset_t set;
1609 sigemptyset(&set);
Willy Tarreauebc92442016-06-21 17:29:46 +02001610 sigaddset(&set, SIGCHLD);
1611 assert(sigprocmask(SIG_UNBLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001612}
1613
Simon Horman98637e52014-06-20 12:30:16 +09001614static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1615{
1616 struct pid_list *elem;
1617 struct check *check = t->context;
1618
1619 elem = pool_alloc2(pool2_pid_list);
1620 if (!elem)
1621 return NULL;
1622 elem->pid = pid;
1623 elem->t = t;
1624 elem->exited = 0;
1625 check->curpid = elem;
1626 LIST_INIT(&elem->list);
Christopher Fauletcfda8472017-10-20 15:40:23 +02001627
1628 SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
Simon Horman98637e52014-06-20 12:30:16 +09001629 LIST_ADD(&pid_list, &elem->list);
Christopher Fauletcfda8472017-10-20 15:40:23 +02001630 SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
1631
Simon Horman98637e52014-06-20 12:30:16 +09001632 return elem;
1633}
1634
Simon Horman98637e52014-06-20 12:30:16 +09001635static void pid_list_del(struct pid_list *elem)
1636{
1637 struct check *check;
1638
1639 if (!elem)
1640 return;
1641
Christopher Fauletcfda8472017-10-20 15:40:23 +02001642 SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
Simon Horman98637e52014-06-20 12:30:16 +09001643 LIST_DEL(&elem->list);
Christopher Fauletcfda8472017-10-20 15:40:23 +02001644 SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
1645
Simon Horman98637e52014-06-20 12:30:16 +09001646 if (!elem->exited)
1647 kill(elem->pid, SIGTERM);
1648
1649 check = elem->t->context;
1650 check->curpid = NULL;
1651 pool_free2(pool2_pid_list, elem);
1652}
1653
1654/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1655static void pid_list_expire(pid_t pid, int status)
1656{
1657 struct pid_list *elem;
1658
Christopher Fauletcfda8472017-10-20 15:40:23 +02001659 SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
Simon Horman98637e52014-06-20 12:30:16 +09001660 list_for_each_entry(elem, &pid_list, list) {
1661 if (elem->pid == pid) {
1662 elem->t->expire = now_ms;
1663 elem->status = status;
1664 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001665 task_wakeup(elem->t, TASK_WOKEN_IO);
Christopher Fauletcfda8472017-10-20 15:40:23 +02001666 break;
Simon Horman98637e52014-06-20 12:30:16 +09001667 }
1668 }
Christopher Fauletcfda8472017-10-20 15:40:23 +02001669 SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
Simon Horman98637e52014-06-20 12:30:16 +09001670}
1671
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001672static void sigchld_handler(struct sig_handler *sh)
Simon Horman98637e52014-06-20 12:30:16 +09001673{
1674 pid_t pid;
1675 int status;
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001676
Simon Horman98637e52014-06-20 12:30:16 +09001677 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1678 pid_list_expire(pid, status);
1679}
1680
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001681static int init_pid_list(void)
1682{
Simon Horman98637e52014-06-20 12:30:16 +09001683 if (pool2_pid_list != NULL)
1684 /* Nothing to do */
1685 return 0;
1686
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001687 if (!signal_register_fct(SIGCHLD, sigchld_handler, SIGCHLD)) {
Simon Horman98637e52014-06-20 12:30:16 +09001688 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1689 strerror(errno));
1690 return 1;
1691 }
1692
1693 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1694 if (pool2_pid_list == NULL) {
1695 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1696 strerror(errno));
1697 return 1;
1698 }
1699
Christopher Fauletcfda8472017-10-20 15:40:23 +02001700 SPIN_INIT(&pid_list_lock);
1701
Simon Horman98637e52014-06-20 12:30:16 +09001702 return 0;
1703}
1704
Cyril Bontéac92a062014-12-27 22:28:38 +01001705/* helper macro to set an environment variable and jump to a specific label on failure. */
1706#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001707
1708/*
Cyril Bontéac92a062014-12-27 22:28:38 +01001709 * helper function to allocate enough memory to store an environment variable.
1710 * It will also check that the environment variable is updatable, and silently
1711 * fail if not.
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001712 */
Cyril Bontéac92a062014-12-27 22:28:38 +01001713static int extchk_setenv(struct check *check, int idx, const char *value)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001714{
1715 int len, ret;
Cyril Bontéac92a062014-12-27 22:28:38 +01001716 char *envname;
1717 int vmaxlen;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001718
Cyril Bontéac92a062014-12-27 22:28:38 +01001719 if (idx < 0 || idx >= EXTCHK_SIZE) {
1720 Alert("Illegal environment variable index %d. Aborting.\n", idx);
1721 return 1;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001722 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001723
1724 envname = extcheck_envs[idx].name;
1725 vmaxlen = extcheck_envs[idx].vmaxlen;
1726
1727 /* Check if the environment variable is already set, and silently reject
1728 * the update if this one is not updatable. */
1729 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
1730 return 0;
1731
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001732 /* Instead of sending NOT_USED, sending an empty value is preferable */
1733 if (strcmp(value, "NOT_USED") == 0) {
1734 value = "";
1735 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001736
1737 len = strlen(envname) + 1;
1738 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
1739 len += strlen(value);
1740 else
1741 len += vmaxlen;
1742
1743 if (!check->envp[idx])
1744 check->envp[idx] = malloc(len + 1);
1745
1746 if (!check->envp[idx]) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001747 Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
1748 return 1;
1749 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001750 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001751 if (ret < 0) {
1752 Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
1753 return 1;
1754 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001755 else if (ret > len) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001756 Alert("Environment variable '%s' was truncated. Aborting.\n", envname);
1757 return 1;
1758 }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001759 return 0;
1760}
Simon Horman98637e52014-06-20 12:30:16 +09001761
1762static int prepare_external_check(struct check *check)
1763{
1764 struct server *s = check->server;
1765 struct proxy *px = s->proxy;
1766 struct listener *listener = NULL, *l;
1767 int i;
Simon Horman98637e52014-06-20 12:30:16 +09001768 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001769 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001770
1771 list_for_each_entry(l, &px->conf.listeners, by_fe)
1772 /* Use the first INET, INET6 or UNIX listener */
1773 if (l->addr.ss_family == AF_INET ||
1774 l->addr.ss_family == AF_INET6 ||
1775 l->addr.ss_family == AF_UNIX) {
1776 listener = l;
1777 break;
1778 }
1779
Simon Horman98637e52014-06-20 12:30:16 +09001780 check->curpid = NULL;
Cyril Bontéac92a062014-12-27 22:28:38 +01001781 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *));
1782 if (!check->envp) {
1783 Alert("Failed to allocate memory for environment variables. Aborting\n");
1784 goto err;
1785 }
Simon Horman98637e52014-06-20 12:30:16 +09001786
Cyril Bontéac92a062014-12-27 22:28:38 +01001787 check->argv = calloc(6, sizeof(char *));
1788 if (!check->argv) {
1789 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001790 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001791 }
Simon Horman98637e52014-06-20 12:30:16 +09001792
1793 check->argv[0] = px->check_command;
1794
Cyril Bonté777be862014-12-02 21:21:35 +01001795 if (!listener) {
1796 check->argv[1] = strdup("NOT_USED");
1797 check->argv[2] = strdup("NOT_USED");
1798 }
1799 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001800 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001801 addr_to_str(&listener->addr, buf, sizeof(buf));
1802 check->argv[1] = strdup(buf);
1803 port_to_str(&listener->addr, buf, sizeof(buf));
1804 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001805 }
1806 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001807 const struct sockaddr_un *un;
1808
1809 un = (struct sockaddr_un *)&listener->addr;
1810 check->argv[1] = strdup(un->sun_path);
1811 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001812 }
1813 else {
Cyril Bontéac92a062014-12-27 22:28:38 +01001814 Alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001815 goto err;
1816 }
1817
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001818 addr_to_str(&s->addr, buf, sizeof(buf));
1819 check->argv[3] = strdup(buf);
Willy Tarreau04276f32017-01-06 17:41:29 +01001820
1821 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
1822 snprintf(buf, sizeof(buf), "%u", s->svc_port);
1823 else
1824 *buf = 0;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001825 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001826
Cyril Bontéac92a062014-12-27 22:28:38 +01001827 for (i = 0; i < 5; i++) {
1828 if (!check->argv[i]) {
1829 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001830 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001831 }
1832 }
Simon Horman98637e52014-06-20 12:30:16 +09001833
Cyril Bontéac92a062014-12-27 22:28:38 +01001834 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001835 /* Add proxy environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001836 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
1837 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
1838 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
1839 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001840 /* Add server environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001841 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
1842 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
1843 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
1844 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
1845 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
1846 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
1847
1848 /* Ensure that we don't leave any hole in check->envp */
1849 for (i = 0; i < EXTCHK_SIZE; i++)
1850 if (!check->envp[i])
1851 EXTCHK_SETENV(check, i, "", err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001852
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001853 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001854err:
1855 if (check->envp) {
Cyril Bontéac92a062014-12-27 22:28:38 +01001856 for (i = 0; i < EXTCHK_SIZE; i++)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001857 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001858 free(check->envp);
1859 check->envp = NULL;
1860 }
1861
1862 if (check->argv) {
1863 for (i = 1; i < 5; i++)
1864 free(check->argv[i]);
1865 free(check->argv);
1866 check->argv = NULL;
1867 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001868 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001869}
1870
Simon Hormanb00d17a2014-06-13 16:18:16 +09001871/*
Simon Horman98637e52014-06-20 12:30:16 +09001872 * establish a server health-check that makes use of a process.
1873 *
1874 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001875 * - SF_ERR_NONE if everything's OK
Willy Tarreaue7dff022015-04-03 01:14:29 +02001876 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
Tim Düsterhus4896c442016-11-29 02:15:19 +01001877 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Horman98637e52014-06-20 12:30:16 +09001878 *
1879 * Blocks and then unblocks SIGCHLD
1880 */
1881static int connect_proc_chk(struct task *t)
1882{
Cyril Bontéac92a062014-12-27 22:28:38 +01001883 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001884 struct check *check = t->context;
1885 struct server *s = check->server;
1886 struct proxy *px = s->proxy;
1887 int status;
1888 pid_t pid;
1889
Willy Tarreaue7dff022015-04-03 01:14:29 +02001890 status = SF_ERR_RESOURCE;
Simon Horman98637e52014-06-20 12:30:16 +09001891
1892 block_sigchld();
1893
1894 pid = fork();
1895 if (pid < 0) {
1896 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1897 strerror(errno));
1898 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1899 goto out;
1900 }
1901 if (pid == 0) {
1902 /* Child */
1903 extern char **environ;
Willy Tarreaub7b24782016-06-21 15:32:29 +02001904 int fd;
1905
1906 /* close all FDs. Keep stdin/stdout/stderr in verbose mode */
1907 fd = (global.mode & (MODE_QUIET|MODE_VERBOSE)) == MODE_QUIET ? 0 : 3;
1908
1909 while (fd < global.rlimit_nofile)
1910 close(fd++);
1911
Simon Horman98637e52014-06-20 12:30:16 +09001912 environ = check->envp;
Cyril Bontéac92a062014-12-27 22:28:38 +01001913 extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)));
Simon Horman98637e52014-06-20 12:30:16 +09001914 execvp(px->check_command, check->argv);
1915 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1916 strerror(errno));
1917 exit(-1);
1918 }
1919
1920 /* Parent */
1921 if (check->result == CHK_RES_UNKNOWN) {
1922 if (pid_list_add(pid, t) != NULL) {
1923 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1924
1925 if (px->timeout.check && px->timeout.connect) {
1926 int t_con = tick_add(now_ms, px->timeout.connect);
1927 t->expire = tick_first(t->expire, t_con);
1928 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001929 status = SF_ERR_NONE;
Simon Horman98637e52014-06-20 12:30:16 +09001930 goto out;
1931 }
1932 else {
1933 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1934 }
1935 kill(pid, SIGTERM); /* process creation error */
1936 }
1937 else
1938 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1939
1940out:
1941 unblock_sigchld();
1942 return status;
1943}
1944
1945/*
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001946 * manages a server health-check that uses an external process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001947 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1948 */
Simon Horman98637e52014-06-20 12:30:16 +09001949static struct task *process_chk_proc(struct task *t)
1950{
1951 struct check *check = t->context;
1952 struct server *s = check->server;
Simon Horman98637e52014-06-20 12:30:16 +09001953 int rv;
1954 int ret;
1955 int expired = tick_is_expired(t->expire, now_ms);
1956
1957 if (!(check->state & CHK_ST_INPROGRESS)) {
1958 /* no check currently running */
1959 if (!expired) /* woke up too early */
1960 return t;
1961
1962 /* we don't send any health-checks when the proxy is
1963 * stopped, the server should not be checked or the check
1964 * is disabled.
1965 */
1966 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1967 s->proxy->state == PR_STSTOPPED)
1968 goto reschedule;
1969
1970 /* we'll initiate a new check */
1971 set_server_check_status(check, HCHK_STATUS_START, NULL);
1972
1973 check->state |= CHK_ST_INPROGRESS;
1974
Simon Hormandbf70192015-01-30 11:22:53 +09001975 ret = connect_proc_chk(t);
Willy Tarreaud7c3fbd2017-10-04 15:19:26 +02001976 if (ret == SF_ERR_NONE) {
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001977 /* the process was forked, we allow up to min(inter,
1978 * timeout.connect) for it to report its status, but
1979 * only when timeout.check is set as it may be to short
1980 * for a full check otherwise.
Simon Horman98637e52014-06-20 12:30:16 +09001981 */
1982 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1983
1984 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1985 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1986 t->expire = tick_first(t->expire, t_con);
1987 }
Christopher Faulet88ce5d12017-10-20 15:41:18 +02001988 task_set_affinity(t, tid_bit);
Simon Horman98637e52014-06-20 12:30:16 +09001989 goto reschedule;
Simon Horman98637e52014-06-20 12:30:16 +09001990 }
1991
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001992 /* here, we failed to start the check */
Simon Horman98637e52014-06-20 12:30:16 +09001993
1994 check->state &= ~CHK_ST_INPROGRESS;
1995 check_notify_failure(check);
1996
1997 /* we allow up to min(inter, timeout.connect) for a connection
1998 * to establish but only when timeout.check is set
1999 * as it may be to short for a full check otherwise
2000 */
2001 while (tick_is_expired(t->expire, now_ms)) {
2002 int t_con;
2003
2004 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2005 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2006
2007 if (s->proxy->timeout.check)
2008 t->expire = tick_first(t->expire, t_con);
2009 }
2010 }
2011 else {
2012 /* there was a test running.
2013 * First, let's check whether there was an uncaught error,
2014 * which can happen on connect timeout or error.
2015 */
2016 if (check->result == CHK_RES_UNKNOWN) {
2017 /* good connection is enough for pure TCP check */
2018 struct pid_list *elem = check->curpid;
2019 int status = HCHK_STATUS_UNKNOWN;
2020
2021 if (elem->exited) {
2022 status = elem->status; /* Save in case the process exits between use below */
2023 if (!WIFEXITED(status))
2024 check->code = -1;
2025 else
2026 check->code = WEXITSTATUS(status);
2027 if (!WIFEXITED(status) || WEXITSTATUS(status))
2028 status = HCHK_STATUS_PROCERR;
2029 else
2030 status = HCHK_STATUS_PROCOK;
2031 } else if (expired) {
2032 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02002033 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09002034 kill(elem->pid, SIGTERM);
2035 }
2036 set_server_check_status(check, status, NULL);
2037 }
2038
2039 if (check->result == CHK_RES_FAILED) {
2040 /* a failure or timeout detected */
2041 check_notify_failure(check);
2042 }
2043 else if (check->result == CHK_RES_CONDPASS) {
2044 /* check is OK but asks for stopping mode */
2045 check_notify_stopping(check);
2046 }
2047 else if (check->result == CHK_RES_PASSED) {
2048 /* a success was detected */
2049 check_notify_success(check);
2050 }
Christopher Faulet88ce5d12017-10-20 15:41:18 +02002051 task_set_affinity(t, MAX_THREADS_MASK);
Simon Horman98637e52014-06-20 12:30:16 +09002052 check->state &= ~CHK_ST_INPROGRESS;
2053
2054 pid_list_del(check->curpid);
2055
2056 rv = 0;
2057 if (global.spread_checks > 0) {
2058 rv = srv_getinter(check) * global.spread_checks / 100;
2059 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
2060 }
2061 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
2062 }
2063
2064 reschedule:
2065 while (tick_is_expired(t->expire, now_ms))
2066 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2067 return t;
2068}
2069
2070/*
2071 * manages a server health-check that uses a connection. Returns
2072 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2073 */
2074static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002075{
Simon Horman4a741432013-02-23 15:35:38 +09002076 struct check *check = t->context;
2077 struct server *s = check->server;
2078 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002079 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002080 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002081 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002082
Willy Tarreau2c115e52013-12-11 19:41:16 +01002083 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01002084 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002085 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01002086 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002087
Simon Horman671b6f02013-11-25 10:46:39 +09002088 /* we don't send any health-checks when the proxy is
2089 * stopped, the server should not be checked or the check
2090 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002091 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01002092 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002093 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002094 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002095
2096 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002097 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002098
Willy Tarreau2c115e52013-12-11 19:41:16 +01002099 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09002100 check->bi->p = check->bi->data;
2101 check->bi->i = 0;
2102 check->bo->p = check->bo->data;
2103 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002104
Simon Hormandbf70192015-01-30 11:22:53 +09002105 ret = connect_conn_chk(t);
Willy Tarreau00149122017-10-04 18:05:01 +02002106 conn = check->conn;
2107
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002108 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002109 case SF_ERR_UP:
Simon Hormanb00d17a2014-06-13 16:18:16 +09002110 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002111 case SF_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002112 /* we allow up to min(inter, timeout.connect) for a connection
2113 * to establish but only when timeout.check is set
2114 * as it may be to short for a full check otherwise
2115 */
Simon Horman4a741432013-02-23 15:35:38 +09002116 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002117
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002118 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2119 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2120 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002121 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002122
2123 if (check->type)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002124 conn_xprt_want_recv(conn); /* prepare for reading a possible reply */
Willy Tarreau06559ac2013-12-05 01:53:08 +01002125
Christopher Faulet88ce5d12017-10-20 15:41:18 +02002126 task_set_affinity(t, tid_bit);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002127 goto reschedule;
2128
Willy Tarreaue7dff022015-04-03 01:14:29 +02002129 case SF_ERR_SRVTO: /* ETIMEDOUT */
2130 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau00149122017-10-04 18:05:01 +02002131 if (conn)
2132 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002133 chk_report_conn_err(check, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002134 break;
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002135 /* should share same code than cases below */
2136 case SF_ERR_CHK_PORT:
2137 check->state |= CHK_ST_PORT_MISS;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002138 case SF_ERR_PRXCOND:
2139 case SF_ERR_RESOURCE:
2140 case SF_ERR_INTERNAL:
Willy Tarreau00149122017-10-04 18:05:01 +02002141 if (conn)
2142 conn->flags |= CO_FL_ERROR;
2143 chk_report_conn_err(check, conn ? 0 : ENOMEM, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002144 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002145 }
2146
Willy Tarreau5a78f362012-11-23 12:47:05 +01002147 /* here, we have seen a synchronous error, no fd was allocated */
Olivier Houchard390485a2017-10-24 19:03:30 +02002148 if (conn) {
2149 conn_free(conn);
2150 check->conn = conn = NULL;
2151 }
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002152
Willy Tarreau2c115e52013-12-11 19:41:16 +01002153 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002154 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002155
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002156 /* we allow up to min(inter, timeout.connect) for a connection
2157 * to establish but only when timeout.check is set
2158 * as it may be to short for a full check otherwise
2159 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002160 while (tick_is_expired(t->expire, now_ms)) {
2161 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002162
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002163 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002164 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002165
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002166 if (s->proxy->timeout.check)
2167 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002168 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002169 }
2170 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002171 /* there was a test running.
2172 * First, let's check whether there was an uncaught error,
2173 * which can happen on connect timeout or error.
2174 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002175 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002176 /* good connection is enough for pure TCP check */
2177 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002178 if (check->use_ssl)
2179 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002180 else
Simon Horman4a741432013-02-23 15:35:38 +09002181 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002182 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002183 else if ((conn->flags & CO_FL_ERROR) || expired) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002184 chk_report_conn_err(check, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002185 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002186 else
2187 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002188 }
2189
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002190 /* check complete or aborted */
Willy Tarreau00149122017-10-04 18:05:01 +02002191 if (conn && conn->xprt) {
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002192 /* The check was aborted and the connection was not yet closed.
2193 * This can happen upon timeout, or when an external event such
2194 * as a failed response coupled with "observe layer7" caused the
2195 * server state to be suddenly changed.
2196 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01002197 conn_sock_drain(conn);
Willy Tarreau402dbc12017-10-05 17:53:13 +02002198 conn_full_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002199 }
2200
Willy Tarreau00149122017-10-04 18:05:01 +02002201 if (conn) {
2202 conn_free(conn);
2203 check->conn = conn = NULL;
2204 }
2205
Willy Tarreauaf549582014-05-16 17:37:50 +02002206 if (check->result == CHK_RES_FAILED) {
2207 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002208 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002209 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002210 else if (check->result == CHK_RES_CONDPASS) {
2211 /* check is OK but asks for stopping mode */
2212 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002213 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002214 else if (check->result == CHK_RES_PASSED) {
2215 /* a success was detected */
2216 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002217 }
Christopher Faulet88ce5d12017-10-20 15:41:18 +02002218 task_set_affinity(t, MAX_THREADS_MASK);
Willy Tarreau2c115e52013-12-11 19:41:16 +01002219 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002220
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002221 rv = 0;
2222 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002223 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002224 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002225 }
Simon Horman4a741432013-02-23 15:35:38 +09002226 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002227 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002228
2229 reschedule:
2230 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002231 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002232 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002233 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002234}
2235
Simon Horman98637e52014-06-20 12:30:16 +09002236/*
2237 * manages a server health-check. Returns
2238 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2239 */
2240static struct task *process_chk(struct task *t)
2241{
2242 struct check *check = t->context;
2243
2244 if (check->type == PR_O2_EXT_CHK)
2245 return process_chk_proc(t);
2246 return process_chk_conn(t);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002247
Simon Horman98637e52014-06-20 12:30:16 +09002248}
2249
Simon Horman5c942422013-11-25 10:46:32 +09002250static int start_check_task(struct check *check, int mininter,
2251 int nbcheck, int srvpos)
2252{
2253 struct task *t;
2254 /* task for the check */
Emeric Brunc60def82017-09-27 14:59:38 +02002255 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Simon Horman5c942422013-11-25 10:46:32 +09002256 Alert("Starting [%s:%s] check: out of memory.\n",
2257 check->server->proxy->id, check->server->id);
2258 return 0;
2259 }
2260
2261 check->task = t;
2262 t->process = process_chk;
2263 t->context = check;
2264
Willy Tarreau1746eec2014-04-25 10:46:47 +02002265 if (mininter < srv_getinter(check))
2266 mininter = srv_getinter(check);
2267
2268 if (global.max_spread_checks && mininter > global.max_spread_checks)
2269 mininter = global.max_spread_checks;
2270
Simon Horman5c942422013-11-25 10:46:32 +09002271 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002272 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002273 check->start = now;
2274 task_queue(t);
2275
2276 return 1;
2277}
2278
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002279/*
2280 * Start health-check.
Willy Tarreau865c5142016-12-21 20:04:48 +01002281 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002282 */
Willy Tarreau865c5142016-12-21 20:04:48 +01002283static int start_checks()
2284{
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002285
2286 struct proxy *px;
2287 struct server *s;
2288 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002289 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002290
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002291 /* 1- count the checkers to run simultaneously.
2292 * We also determine the minimum interval among all of those which
2293 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2294 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002295 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002296 * too short an interval for all others.
2297 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002298 for (px = proxy; px; px = px->next) {
2299 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002300 if (s->slowstart) {
Emeric Brunc60def82017-09-27 14:59:38 +02002301 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002302 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002303 return ERR_ALERT | ERR_FATAL;
Willy Tarreaue7b73482013-11-21 11:50:50 +01002304 }
2305 /* We need a warmup task that will be called when the server
2306 * state switches from down to up.
2307 */
2308 s->warmup = t;
2309 t->process = server_warmup;
2310 t->context = s;
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002311 /* server can be in this state only because of */
Emeric Brun52a91d32017-08-31 14:41:55 +02002312 if (s->next_state == SRV_ST_STARTING)
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002313 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, (now.tv_sec - s->last_change)) / 20)));
Willy Tarreaue7b73482013-11-21 11:50:50 +01002314 }
2315
Willy Tarreaud8514a22013-12-11 21:10:14 +01002316 if (s->check.state & CHK_ST_CONFIGURED) {
2317 nbcheck++;
2318 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2319 (!mininter || mininter > srv_getinter(&s->check)))
2320 mininter = srv_getinter(&s->check);
2321 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002322
Willy Tarreaud8514a22013-12-11 21:10:14 +01002323 if (s->agent.state & CHK_ST_CONFIGURED) {
2324 nbcheck++;
2325 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2326 (!mininter || mininter > srv_getinter(&s->agent)))
2327 mininter = srv_getinter(&s->agent);
2328 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002329 }
2330 }
2331
Simon Horman4a741432013-02-23 15:35:38 +09002332 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002333 return 0;
2334
2335 srand((unsigned)time(NULL));
2336
2337 /*
2338 * 2- start them as far as possible from each others. For this, we will
2339 * start them after their interval set to the min interval divided by
2340 * the number of servers, weighted by the server's position in the list.
2341 */
2342 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002343 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2344 if (init_pid_list()) {
2345 Alert("Starting [%s] check: out of memory.\n", px->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002346 return ERR_ALERT | ERR_FATAL;
Simon Horman98637e52014-06-20 12:30:16 +09002347 }
2348 }
2349
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002350 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002351 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002352 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002353 if (s->check.type == PR_O2_EXT_CHK) {
2354 if (!prepare_external_check(&s->check))
Willy Tarreau865c5142016-12-21 20:04:48 +01002355 return ERR_ALERT | ERR_FATAL;
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002356 }
Simon Hormand60d6912013-11-25 10:46:36 +09002357 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
Willy Tarreau865c5142016-12-21 20:04:48 +01002358 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002359 srvpos++;
2360 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002361
Simon Hormand60d6912013-11-25 10:46:36 +09002362 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002363 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002364 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
Willy Tarreau865c5142016-12-21 20:04:48 +01002365 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002366 }
2367 srvpos++;
2368 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002369 }
2370 }
2371 return 0;
2372}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002373
2374/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002375 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002376 * The buffer MUST be terminated by a null byte before calling this function.
2377 * Sets server status appropriately. The caller is responsible for ensuring
2378 * that the buffer contains at least 13 characters. If <done> is zero, we may
2379 * return 0 to indicate that data is required to decide of a match.
2380 */
2381static int httpchk_expect(struct server *s, int done)
2382{
2383 static char status_msg[] = "HTTP status check returned code <000>";
2384 char status_code[] = "000";
2385 char *contentptr;
2386 int crlf;
2387 int ret;
2388
2389 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2390 case PR_O2_EXP_STS:
2391 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002392 memcpy(status_code, s->check.bi->data + 9, 3);
2393 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002394
2395 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2396 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2397 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002398 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002399
2400 /* we necessarily have the response, so there are no partial failures */
2401 if (s->proxy->options2 & PR_O2_EXP_INV)
2402 ret = !ret;
2403
Simon Horman4a741432013-02-23 15:35:38 +09002404 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002405 break;
2406
2407 case PR_O2_EXP_STR:
2408 case PR_O2_EXP_RSTR:
2409 /* very simple response parser: ignore CR and only count consecutive LFs,
2410 * stop with contentptr pointing to first char after the double CRLF or
2411 * to '\0' if crlf < 2.
2412 */
2413 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002414 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002415 if (crlf >= 2)
2416 break;
2417 if (*contentptr == '\r')
2418 continue;
2419 else if (*contentptr == '\n')
2420 crlf++;
2421 else
2422 crlf = 0;
2423 }
2424
2425 /* Check that response contains a body... */
2426 if (crlf < 2) {
2427 if (!done)
2428 return 0;
2429
Simon Horman4a741432013-02-23 15:35:38 +09002430 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002431 "HTTP content check could not find a response body");
2432 return 1;
2433 }
2434
2435 /* Check that response body is not empty... */
2436 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002437 if (!done)
2438 return 0;
2439
Simon Horman4a741432013-02-23 15:35:38 +09002440 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002441 "HTTP content check found empty response body");
2442 return 1;
2443 }
2444
2445 /* Check the response content against the supplied string
2446 * or regex... */
2447 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2448 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2449 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002450 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002451
2452 /* if we don't match, we may need to wait more */
2453 if (!ret && !done)
2454 return 0;
2455
2456 if (ret) {
2457 /* content matched */
2458 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002459 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002460 "HTTP check matched unwanted content");
2461 else
Simon Horman4a741432013-02-23 15:35:38 +09002462 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002463 "HTTP content check matched");
2464 }
2465 else {
2466 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002467 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002468 "HTTP check did not match unwanted content");
2469 else
Simon Horman4a741432013-02-23 15:35:38 +09002470 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002471 "HTTP content check did not match");
2472 }
2473 break;
2474 }
2475 return 1;
2476}
2477
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002478/*
2479 * return the id of a step in a send/expect session
2480 */
Simon Hormane16c1b32015-01-30 11:22:57 +09002481static int tcpcheck_get_step_id(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002482{
2483 struct tcpcheck_rule *cur = NULL, *next = NULL;
2484 int i = 0;
2485
Willy Tarreau213c6782014-10-02 14:51:02 +02002486 /* not even started anything yet => step 0 = initial connect */
Baptiste Assmannf95bc8e2015-04-25 16:03:06 +02002487 if (!check->current_step)
Willy Tarreau213c6782014-10-02 14:51:02 +02002488 return 0;
2489
Simon Hormane16c1b32015-01-30 11:22:57 +09002490 cur = check->last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002491
2492 /* no step => first step */
2493 if (cur == NULL)
2494 return 1;
2495
2496 /* increment i until current step */
Simon Hormane16c1b32015-01-30 11:22:57 +09002497 list_for_each_entry(next, check->tcpcheck_rules, list) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002498 if (next->list.p == &cur->list)
2499 break;
2500 ++i;
2501 }
2502
2503 return i;
2504}
2505
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002506/*
2507 * return the latest known comment before (including) the given stepid
2508 * returns NULL if no comment found
2509 */
2510static char * tcpcheck_get_step_comment(struct check *check, int stepid)
2511{
2512 struct tcpcheck_rule *cur = NULL;
2513 char *ret = NULL;
2514 int i = 0;
2515
2516 /* not even started anything yet, return latest comment found before any action */
2517 if (!check->current_step) {
2518 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2519 if (cur->action == TCPCHK_ACT_COMMENT)
2520 ret = cur->comment;
2521 else
2522 goto return_comment;
2523 }
2524 }
2525
2526 i = 1;
2527 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2528 if (cur->comment)
2529 ret = cur->comment;
2530
2531 if (i >= stepid)
2532 goto return_comment;
2533
2534 ++i;
2535 }
2536
2537 return_comment:
2538 return ret;
2539}
2540
Willy Tarreaube74b882017-10-04 16:22:49 +02002541/* proceed with next steps for the TCP checks <check>. Note that this is called
2542 * both from the connection's wake() callback and from the check scheduling task.
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002543 * It returns 0 on normal cases, or <0 if a close() has happened on an existing
2544 * connection, presenting the risk of an fd replacement.
Willy Tarreaube74b882017-10-04 16:22:49 +02002545 */
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002546static int tcpcheck_main(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002547{
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002548 char *contentptr, *comment;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002549 struct tcpcheck_rule *next;
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002550 int done = 0, ret = 0, step = 0;
Willy Tarreaube74b882017-10-04 16:22:49 +02002551 struct connection *conn = check->conn;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002552 struct server *s = check->server;
2553 struct task *t = check->task;
Simon Hormane16c1b32015-01-30 11:22:57 +09002554 struct list *head = check->tcpcheck_rules;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002555 int retcode = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002556
Willy Tarreauef953952014-10-02 14:30:14 +02002557 /* here, we know that the check is complete or that it failed */
2558 if (check->result != CHK_RES_UNKNOWN)
2559 goto out_end_tcpcheck;
2560
2561 /* We have 4 possibilities here :
2562 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
Willy Tarreau00149122017-10-04 18:05:01 +02002563 * connection attempt was made yet ; conn==NULL;current_step==NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002564 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2565 * does not exist (no rule), so a connection attempt was made
Willy Tarreau00149122017-10-04 18:05:01 +02002566 * before coming here, conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002567 * 3. we're coming back after having started with step 1, so we may
Willy Tarreau00149122017-10-04 18:05:01 +02002568 * be waiting for a connection attempt to complete. conn!=NULL.
2569 * 4. the connection + handshake are complete. conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002570 *
2571 * #2 and #3 are quite similar, we want both the connection and the
2572 * handshake to complete before going any further. Thus we must always
2573 * wait for a connection to complete unless we're before and existing
2574 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002575 */
Willy Tarreau449f9522015-05-13 15:39:48 +02002576
2577 /* find first rule and skip comments */
2578 next = LIST_NEXT(head, struct tcpcheck_rule *, list);
2579 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
2580 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
2581
Willy Tarreau00149122017-10-04 18:05:01 +02002582 if ((check->current_step || &next->list == head) &&
2583 (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002584 /* we allow up to min(inter, timeout.connect) for a connection
2585 * to establish but only when timeout.check is set
2586 * as it may be to short for a full check otherwise
2587 */
2588 while (tick_is_expired(t->expire, now_ms)) {
2589 int t_con;
2590
2591 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2592 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2593
2594 if (s->proxy->timeout.check)
2595 t->expire = tick_first(t->expire, t_con);
2596 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002597 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002598 }
2599
Willy Tarreauef953952014-10-02 14:30:14 +02002600 /* special case: option tcp-check with no rule, a connect is enough */
Willy Tarreau449f9522015-05-13 15:39:48 +02002601 if (&next->list == head) {
Willy Tarreauef953952014-10-02 14:30:14 +02002602 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002603 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002604 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002605
Willy Tarreau213c6782014-10-02 14:51:02 +02002606 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002607 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002608 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002609 check->bo->p = check->bo->data;
2610 check->bo->o = 0;
2611 check->bi->p = check->bi->data;
2612 check->bi->i = 0;
Willy Tarreau449f9522015-05-13 15:39:48 +02002613 check->current_step = next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002614 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2615 if (s->proxy->timeout.check)
2616 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2617 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002618
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002619 /* It's only the rules which will enable send/recv */
Willy Tarreau00149122017-10-04 18:05:01 +02002620 if (conn)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002621 __conn_xprt_stop_both(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002622
Willy Tarreauabca5b62013-12-06 14:19:25 +01002623 while (1) {
Willy Tarreau263013d2015-05-13 11:59:14 +02002624 /* We have to try to flush the output buffer before reading, at
2625 * the end, or if we're about to send a string that does not fit
2626 * in the remaining space. That explains why we break out of the
Willy Tarreau00149122017-10-04 18:05:01 +02002627 * loop after this control. If we have data, conn is valid.
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002628 */
2629 if (check->bo->o &&
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002630 (&check->current_step->list == head ||
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002631 check->current_step->action != TCPCHK_ACT_SEND ||
2632 check->current_step->string_len >= buffer_total_space(check->bo))) {
2633
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002634 __conn_xprt_want_send(conn);
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002635 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002636 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002637 chk_report_conn_err(check, errno, 0);
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002638 __conn_xprt_stop_both(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002639 goto out_end_tcpcheck;
2640 }
Willy Tarreau263013d2015-05-13 11:59:14 +02002641 break;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002642 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002643 }
2644
Willy Tarreau263013d2015-05-13 11:59:14 +02002645 if (&check->current_step->list == head)
Willy Tarreauabca5b62013-12-06 14:19:25 +01002646 break;
2647
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002648 /* have 'next' point to the next rule or NULL if we're on the
2649 * last one, connect() needs this.
2650 */
Willy Tarreau5581c272015-05-13 12:24:53 +02002651 next = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002652
2653 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002654 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002655 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002656
2657 /* NULL if we're on the last rule */
Willy Tarreauf3d34822014-12-08 12:11:28 +01002658 if (&next->list == head)
2659 next = NULL;
2660
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002661 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2662 struct protocol *proto;
2663 struct xprt_ops *xprt;
2664
Willy Tarreau00149122017-10-04 18:05:01 +02002665 /* For a connect action we'll create a new connection.
2666 * We may also have to kill a previous one. But we don't
2667 * want to leave *without* a connection if we came here
2668 * from the connection layer, hence with a connection.
2669 * Thus we'll proceed in the following order :
2670 * 1: close but not release previous connection
2671 * 2: try to get a new connection
2672 * 3: release and replace the old one on success
2673 */
2674 if (check->conn) {
Willy Tarreau402dbc12017-10-05 17:53:13 +02002675 conn_full_close(check->conn);
Willy Tarreau00149122017-10-04 18:05:01 +02002676 retcode = -1; /* do not reuse the fd! */
2677 }
2678
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002679 /* mark the step as started */
2680 check->last_started_step = check->current_step;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002681
2682 /* prepare new connection */
Willy Tarreau00149122017-10-04 18:05:01 +02002683 conn = conn_new();
2684 if (!conn) {
2685 step = tcpcheck_get_step_id(check);
2686 chunk_printf(&trash, "TCPCHK error allocating connection at step %d", step);
2687 comment = tcpcheck_get_step_comment(check, step);
2688 if (comment)
2689 chunk_appendf(&trash, " comment: '%s'", comment);
2690 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2691 check->current_step = NULL;
2692 return retcode;
2693 }
2694
2695 if (check->conn)
2696 conn_free(check->conn);
2697 check->conn = conn;
2698
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002699 conn_attach(conn, check, &check_conn_cb);
2700 conn->target = &s->obj_type;
2701
2702 /* no client address */
2703 clear_addr(&conn->addr.from);
2704
Simon Horman41f58762015-01-30 11:22:56 +09002705 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002706 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002707 conn->addr.to = check->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002708 }
2709 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002710 /* we'll connect to the addr on the server */
2711 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002712 }
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01002713 proto = protocol_by_family(conn->addr.to.ss_family);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002714
2715 /* port */
2716 if (check->current_step->port)
2717 set_host_port(&conn->addr.to, check->current_step->port);
2718 else if (check->port)
2719 set_host_port(&conn->addr.to, check->port);
2720
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002721 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002722 xprt = xprt_get(XPRT_SSL);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002723 }
2724 else {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002725 xprt = xprt_get(XPRT_RAW);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002726 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002727 conn_prepare(conn, proto, xprt);
2728
Willy Tarreaue7dff022015-04-03 01:14:29 +02002729 ret = SF_ERR_INTERNAL;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002730 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002731 ret = proto->connect(conn,
2732 1 /* I/O polling is always needed */,
2733 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002734 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2735 conn->send_proxy_ofs = 1;
2736 conn->flags |= CO_FL_SEND_PROXY;
2737 }
2738
2739 /* It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02002740 * - SF_ERR_NONE if everything's OK
2741 * - SF_ERR_SRVTO if there are no more servers
2742 * - SF_ERR_SRVCL if the connection was refused by the server
2743 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2744 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2745 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01002746 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002747 * Note that we try to prevent the network stack from sending the ACK during the
2748 * connect() when a pure TCP check is used (without PROXY protocol).
2749 */
2750 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002751 case SF_ERR_NONE:
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002752 /* we allow up to min(inter, timeout.connect) for a connection
2753 * to establish but only when timeout.check is set
2754 * as it may be to short for a full check otherwise
2755 */
2756 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2757
2758 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2759 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2760 t->expire = tick_first(t->expire, t_con);
2761 }
2762 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002763 case SF_ERR_SRVTO: /* ETIMEDOUT */
2764 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002765 step = tcpcheck_get_step_id(check);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002766 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002767 step, strerror(errno));
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002768 comment = tcpcheck_get_step_comment(check, step);
2769 if (comment)
2770 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002771 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2772 goto out_end_tcpcheck;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002773 case SF_ERR_PRXCOND:
2774 case SF_ERR_RESOURCE:
2775 case SF_ERR_INTERNAL:
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002776 step = tcpcheck_get_step_id(check);
2777 chunk_printf(&trash, "TCPCHK error establishing connection at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002778 comment = tcpcheck_get_step_comment(check, step);
2779 if (comment)
2780 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002781 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2782 goto out_end_tcpcheck;
2783 }
2784
2785 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002786 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002787
2788 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002789 while (&check->current_step->list != head &&
2790 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002791 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002792
Willy Tarreauf2c87352015-05-13 12:08:21 +02002793 if (&check->current_step->list == head)
2794 break;
2795
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002796 /* don't do anything until the connection is established */
2797 if (!(conn->flags & CO_FL_CONNECTED)) {
2798 /* update expire time, should be done by process_chk */
2799 /* we allow up to min(inter, timeout.connect) for a connection
2800 * to establish but only when timeout.check is set
2801 * as it may be to short for a full check otherwise
2802 */
2803 while (tick_is_expired(t->expire, now_ms)) {
2804 int t_con;
2805
2806 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2807 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2808
2809 if (s->proxy->timeout.check)
2810 t->expire = tick_first(t->expire, t_con);
2811 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002812 return retcode;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002813 }
2814
2815 } /* end 'connect' */
2816 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2817 /* mark the step as started */
2818 check->last_started_step = check->current_step;
2819
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002820 /* reset the read buffer */
2821 if (*check->bi->data != '\0') {
2822 *check->bi->data = '\0';
2823 check->bi->i = 0;
2824 }
2825
Willy Tarreaubbae3f02017-08-30 09:59:52 +02002826 if (conn->flags & CO_FL_SOCK_WR_SH) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002827 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002828 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002829 goto out_end_tcpcheck;
2830 }
2831
Willy Tarreauabca5b62013-12-06 14:19:25 +01002832 if (check->current_step->string_len >= check->bo->size) {
2833 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2834 check->current_step->string_len, check->bo->size,
Simon Hormane16c1b32015-01-30 11:22:57 +09002835 tcpcheck_get_step_id(check));
Willy Tarreauabca5b62013-12-06 14:19:25 +01002836 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2837 goto out_end_tcpcheck;
2838 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002839
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002840 /* do not try to send if there is no space */
2841 if (check->current_step->string_len >= buffer_total_space(check->bo))
2842 continue;
2843
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002844 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2845 *check->bo->p = '\0'; /* to make gdb output easier to read */
2846
Willy Tarreauabca5b62013-12-06 14:19:25 +01002847 /* go to next rule and try to send */
Willy Tarreau5581c272015-05-13 12:24:53 +02002848 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002849
2850 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002851 while (&check->current_step->list != head &&
2852 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002853 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Willy Tarreauf2c87352015-05-13 12:08:21 +02002854
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002855 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002856 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002857 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002858 goto out_end_tcpcheck;
2859
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002860 __conn_xprt_want_recv(conn);
Willy Tarreau310987a2014-01-22 19:46:33 +01002861 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreau54e917c2017-08-30 07:35:35 +02002862 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002863 done = 1;
2864 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2865 /* Report network errors only if we got no other data. Otherwise
2866 * we'll let the upper layers decide whether the response is OK
2867 * or not. It is very common that an RST sent by the server is
2868 * reported as an error just after the last data chunk.
2869 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002870 chk_report_conn_err(check, errno, 0);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002871 goto out_end_tcpcheck;
2872 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002873 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002874 else
Willy Tarreau263013d2015-05-13 11:59:14 +02002875 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002876 }
2877
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002878 /* mark the step as started */
2879 check->last_started_step = check->current_step;
2880
2881
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002882 /* Intermediate or complete response received.
2883 * Terminate string in check->bi->data buffer.
2884 */
2885 if (check->bi->i < check->bi->size) {
2886 check->bi->data[check->bi->i] = '\0';
2887 }
2888 else {
2889 check->bi->data[check->bi->i - 1] = '\0';
2890 done = 1; /* buffer full, don't wait for more data */
2891 }
2892
2893 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002894
2895 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002896 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002897 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002898 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002899
2900 /* empty response */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002901 step = tcpcheck_get_step_id(check);
2902 chunk_printf(&trash, "TCPCHK got an empty response at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002903 comment = tcpcheck_get_step_comment(check, step);
2904 if (comment)
2905 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002906 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2907
2908 goto out_end_tcpcheck;
2909 }
2910
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002911 if (!done && (check->current_step->string != NULL) && (check->bi->i < check->current_step->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002912 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002913
Willy Tarreaua970c282013-12-06 12:47:19 +01002914 tcpcheck_expect:
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002915 if (check->current_step->string != NULL)
2916 ret = my_memmem(contentptr, check->bi->i, check->current_step->string, check->current_step->string_len) != NULL;
2917 else if (check->current_step->expect_regex != NULL)
2918 ret = regex_exec(check->current_step->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002919
2920 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002921 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002922
2923 /* matched */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002924 step = tcpcheck_get_step_id(check);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002925 if (ret) {
2926 /* matched but we did not want to => ERROR */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002927 if (check->current_step->inverse) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002928 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002929 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002930 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002931 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002932 }
2933 else {
2934 /* we were looking for a regex */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002935 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002936 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002937 comment = tcpcheck_get_step_comment(check, step);
2938 if (comment)
2939 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002940 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2941 goto out_end_tcpcheck;
2942 }
2943 /* matched and was supposed to => OK, next step */
2944 else {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002945 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002946 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002947
2948 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002949 while (&check->current_step->list != head &&
2950 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002951 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002952
Willy Tarreauf2c87352015-05-13 12:08:21 +02002953 if (&check->current_step->list == head)
2954 break;
2955
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002956 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002957 goto tcpcheck_expect;
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002958 __conn_xprt_stop_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002959 }
2960 }
2961 else {
2962 /* not matched */
2963 /* not matched and was not supposed to => OK, next step */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002964 if (check->current_step->inverse) {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002965 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002966 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002967
2968 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002969 while (&check->current_step->list != head &&
2970 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002971 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002972
Willy Tarreauf2c87352015-05-13 12:08:21 +02002973 if (&check->current_step->list == head)
2974 break;
2975
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002976 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002977 goto tcpcheck_expect;
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002978 __conn_xprt_stop_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002979 }
2980 /* not matched but was supposed to => ERROR */
2981 else {
2982 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002983 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002984 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002985 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002986 }
2987 else {
2988 /* we were looking for a regex */
2989 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002990 step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002991 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002992 comment = tcpcheck_get_step_comment(check, step);
2993 if (comment)
2994 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002995 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2996 goto out_end_tcpcheck;
2997 }
2998 }
2999 } /* end expect */
3000 } /* end loop over double chained step list */
3001
Willy Tarreau263013d2015-05-13 11:59:14 +02003002 /* We're waiting for some I/O to complete, we've reached the end of the
3003 * rules, or both. Do what we have to do, otherwise we're done.
3004 */
3005 if (&check->current_step->list == head && !check->bo->o) {
3006 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
3007 goto out_end_tcpcheck;
3008 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003009
Willy Tarreau53c5a042015-05-13 11:38:17 +02003010 /* warning, current_step may now point to the head */
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003011 if (check->bo->o)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02003012 __conn_xprt_want_send(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003013
Willy Tarreau53c5a042015-05-13 11:38:17 +02003014 if (&check->current_step->list != head &&
3015 check->current_step->action == TCPCHK_ACT_EXPECT)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02003016 __conn_xprt_want_recv(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02003017 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003018
3019 out_end_tcpcheck:
3020 /* collect possible new errors */
3021 if (conn->flags & CO_FL_ERROR)
Willy Tarreaub5259bf2017-10-04 14:47:29 +02003022 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003023
Baptiste Assmann69e273f2013-12-11 00:52:19 +01003024 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003025 check->current_step = NULL;
3026
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01003027 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003028 conn->flags |= CO_FL_ERROR;
3029
Olivier Houchard1a0545f2017-09-13 18:30:23 +02003030 __conn_xprt_stop_both(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02003031 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003032}
3033
Simon Hormanb1900d52015-01-30 11:22:54 +09003034const char *init_check(struct check *check, int type)
3035{
3036 check->type = type;
3037
3038 /* Allocate buffer for requests... */
3039 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3040 return "out of memory while allocating check buffer";
3041 }
3042 check->bi->size = global.tune.chksize;
3043
3044 /* Allocate buffer for responses... */
3045 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3046 return "out of memory while allocating check buffer";
3047 }
3048 check->bo->size = global.tune.chksize;
3049
Simon Hormanb1900d52015-01-30 11:22:54 +09003050 return NULL;
3051}
3052
Simon Hormanbfb5d332015-01-30 11:22:55 +09003053void free_check(struct check *check)
3054{
3055 free(check->bi);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003056 check->bi = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003057 free(check->bo);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003058 check->bo = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003059 free(check->conn);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003060 check->conn = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003061}
3062
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003063void email_alert_free(struct email_alert *alert)
3064{
3065 struct tcpcheck_rule *rule, *back;
3066
3067 if (!alert)
3068 return;
3069
Christopher Fauletde1a75b2017-10-23 15:38:19 +02003070 list_for_each_entry_safe(rule, back, &alert->tcpcheck_rules, list) {
3071 LIST_DEL(&rule->list);
3072 free(rule->comment);
3073 free(rule->string);
3074 if (rule->expect_regex)
3075 regex_free(rule->expect_regex);
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003076 pool_free2(pool2_tcpcheck_rule, rule);
Christopher Fauletde1a75b2017-10-23 15:38:19 +02003077 }
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003078 pool_free2(pool2_email_alert, alert);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003079}
3080
3081static struct task *process_email_alert(struct task *t)
3082{
Christopher Faulet0108bb32017-10-20 21:34:32 +02003083 struct check *check = t->context;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003084 struct email_alertq *q;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003085 struct email_alert *alert;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003086
3087 q = container_of(check, typeof(*q), check);
3088
Christopher Fauletc2a89a62017-10-23 15:54:24 +02003089 SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003090 while (1) {
3091 if (!(check->state & CHK_ST_ENABLED)) {
3092 if (LIST_ISEMPTY(&q->email_alerts)) {
3093 /* All alerts processed, queue the task */
3094 t->expire = TICK_ETERNITY;
3095 task_queue(t);
Christopher Fauletc2a89a62017-10-23 15:54:24 +02003096 goto end;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003097 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003098
3099 alert = LIST_NEXT(&q->email_alerts, typeof(alert), list);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003100 LIST_DEL(&alert->list);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003101 t->expire = now_ms;
3102 check->server = alert->srv;
3103 check->tcpcheck_rules = &alert->tcpcheck_rules;
3104 check->status = HCHK_STATUS_INI;
3105 check->state |= CHK_ST_ENABLED;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003106 }
3107
Christopher Faulet0108bb32017-10-20 21:34:32 +02003108 process_chk(t);
3109 if (check->state & CHK_ST_INPROGRESS)
3110 break;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003111
3112 alert = container_of(check->tcpcheck_rules, typeof(*alert), tcpcheck_rules);
3113 email_alert_free(alert);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003114 check->tcpcheck_rules = NULL;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003115 check->server = NULL;
3116 check->state &= ~CHK_ST_ENABLED;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003117 }
Christopher Fauletc2a89a62017-10-23 15:54:24 +02003118 end:
3119 SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003120 return t;
3121}
3122
Christopher Faulet0108bb32017-10-20 21:34:32 +02003123/* Initializes mailer alerts for the proxy <p> using <mls> parameters.
3124 *
3125 * The function returns 1 in success case, otherwise, it returns 0 and err is
3126 * filled.
3127 */
3128int init_email_alert(struct mailers *mls, struct proxy *p, char **err)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003129{
Christopher Faulet0108bb32017-10-20 21:34:32 +02003130 struct mailer *mailer;
3131 struct email_alertq *queues;
3132 const char *err_str;
3133 int i = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003134
Christopher Faulet0108bb32017-10-20 21:34:32 +02003135 if ((queues = calloc(mls->count, sizeof(*queues))) == NULL) {
3136 memprintf(err, "out of memory while allocating mailer alerts queues");
3137 goto error;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003138 }
3139
Christopher Faulet0108bb32017-10-20 21:34:32 +02003140 for (mailer = mls->mailer_list; mailer; i++, mailer = mailer->next) {
3141 struct email_alertq *q = &queues[i];
3142 struct check *check = &q->check;
3143 struct task *t;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003144
3145 LIST_INIT(&q->email_alerts);
Christopher Fauletc2a89a62017-10-23 15:54:24 +02003146 SPIN_INIT(&q->lock);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003147 check->inter = mls->timeout.mail;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003148 check->rise = DEF_AGENT_RISETIME;
3149 check->fall = DEF_AGENT_FALLTIME;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003150 if ((err_str = init_check(check, PR_O2_TCPCHK_CHK))) {
3151 memprintf(err, "%s", err_str);
3152 goto error;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003153 }
3154
3155 check->xprt = mailer->xprt;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003156 check->addr = mailer->addr;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003157 if (!get_host_port(&mailer->addr))
3158 /* Default to submission port */
3159 check->port = 587;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003160 //check->server = s;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003161
Emeric Brunc60def82017-09-27 14:59:38 +02003162 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet0108bb32017-10-20 21:34:32 +02003163 memprintf(err, "out of memory while allocating mailer alerts task");
3164 goto error;
3165 }
3166
3167 check->task = t;
3168 t->process = process_email_alert;
3169 t->context = check;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003170
Christopher Faulet0108bb32017-10-20 21:34:32 +02003171 /* check this in one ms */
3172 t->expire = TICK_ETERNITY;
3173 check->start = now;
3174 task_queue(t);
3175 }
3176
3177 mls->users++;
3178 free(p->email_alert.mailers.name);
3179 p->email_alert.mailers.m = mls;
3180 p->email_alert.queues = queues;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003181 return 0;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003182
3183 error:
3184 for (i = 0; i < mls->count; i++) {
3185 struct email_alertq *q = &queues[i];
3186 struct check *check = &q->check;
3187
3188 if (check->task) {
3189 task_delete(check->task);
3190 task_free(check->task);
3191 check->task = NULL;
3192 }
3193 free_check(check);
3194 }
3195 free(queues);
3196 return 1;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003197}
3198
3199
3200static int add_tcpcheck_expect_str(struct list *list, const char *str)
3201{
3202 struct tcpcheck_rule *tcpcheck;
3203
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003204 if ((tcpcheck = pool_alloc2(pool2_tcpcheck_rule)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003205 return 0;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003206 memset(tcpcheck, 0, sizeof(*tcpcheck));
3207 tcpcheck->action = TCPCHK_ACT_EXPECT;
3208 tcpcheck->string = strdup(str);
3209 tcpcheck->expect_regex = NULL;
3210 tcpcheck->comment = NULL;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003211 if (!tcpcheck->string) {
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003212 pool_free2(pool2_tcpcheck_rule, tcpcheck);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003213 return 0;
3214 }
3215
3216 LIST_ADDQ(list, &tcpcheck->list);
3217 return 1;
3218}
3219
3220static int add_tcpcheck_send_strs(struct list *list, const char * const *strs)
3221{
3222 struct tcpcheck_rule *tcpcheck;
Willy Tarreau64345aa2016-08-10 19:29:09 +02003223 const char *in;
3224 char *dst;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003225 int i;
3226
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003227 if ((tcpcheck = pool_alloc2(pool2_tcpcheck_rule)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003228 return 0;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003229 memset(tcpcheck, 0, sizeof(*tcpcheck));
3230 tcpcheck->action = TCPCHK_ACT_SEND;
3231 tcpcheck->expect_regex = NULL;
3232 tcpcheck->comment = NULL;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003233 tcpcheck->string_len = 0;
3234 for (i = 0; strs[i]; i++)
3235 tcpcheck->string_len += strlen(strs[i]);
3236
3237 tcpcheck->string = malloc(tcpcheck->string_len + 1);
3238 if (!tcpcheck->string) {
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003239 pool_free2(pool2_tcpcheck_rule, tcpcheck);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003240 return 0;
3241 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003242
Willy Tarreau64345aa2016-08-10 19:29:09 +02003243 dst = tcpcheck->string;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003244 for (i = 0; strs[i]; i++)
Willy Tarreau64345aa2016-08-10 19:29:09 +02003245 for (in = strs[i]; (*dst = *in++); dst++);
3246 *dst = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003247
3248 LIST_ADDQ(list, &tcpcheck->list);
3249 return 1;
3250}
3251
Christopher Faulet0108bb32017-10-20 21:34:32 +02003252static int enqueue_one_email_alert(struct proxy *p, struct server *s,
3253 struct email_alertq *q, const char *msg)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003254{
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003255 struct email_alert *alert;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003256 struct tcpcheck_rule *tcpcheck;
3257 struct check *check = &q->check;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003258
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003259 if ((alert = pool_alloc2(pool2_email_alert)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003260 goto error;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003261 LIST_INIT(&alert->list);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003262 LIST_INIT(&alert->tcpcheck_rules);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003263 alert->srv = s;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003264
3265 if ((tcpcheck = pool_alloc2(pool2_tcpcheck_rule)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003266 goto error;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003267 memset(tcpcheck, 0, sizeof(*tcpcheck));
3268 tcpcheck->action = TCPCHK_ACT_CONNECT;
3269 tcpcheck->comment = NULL;
3270 tcpcheck->string = NULL;
3271 tcpcheck->expect_regex = NULL;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003272 LIST_ADDQ(&alert->tcpcheck_rules, &tcpcheck->list);
3273
3274 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "220 "))
3275 goto error;
3276
3277 {
3278 const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" };
3279 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3280 goto error;
3281 }
3282
3283 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3284 goto error;
3285
3286 {
3287 const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" };
3288 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3289 goto error;
3290 }
3291
3292 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3293 goto error;
3294
3295 {
3296 const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" };
3297 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3298 goto error;
3299 }
3300
3301 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3302 goto error;
3303
3304 {
3305 const char * const strs[2] = { "DATA\r\n" };
3306 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3307 goto error;
3308 }
3309
3310 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "354 "))
3311 goto error;
3312
3313 {
3314 struct tm tm;
3315 char datestr[48];
3316 const char * const strs[18] = {
Pieter Baauw5e0964e2016-02-13 16:27:35 +01003317 "From: ", p->email_alert.from, "\r\n",
3318 "To: ", p->email_alert.to, "\r\n",
3319 "Date: ", datestr, "\r\n",
3320 "Subject: [HAproxy Alert] ", msg, "\r\n",
3321 "\r\n",
3322 msg, "\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003323 "\r\n",
Pieter Baauwed35c372015-07-22 19:51:54 +02003324 ".\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003325 NULL
3326 };
3327
3328 get_localtime(date.tv_sec, &tm);
3329
3330 if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) {
3331 goto error;
3332 }
3333
3334 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3335 goto error;
3336 }
3337
3338 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3339 goto error;
3340
3341 {
3342 const char * const strs[2] = { "QUIT\r\n" };
3343 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3344 goto error;
3345 }
3346
3347 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "221 "))
3348 goto error;
3349
Christopher Fauletc2a89a62017-10-23 15:54:24 +02003350 SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003351 task_wakeup(check->task, TASK_WOKEN_MSG);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003352 LIST_ADDQ(&q->email_alerts, &alert->list);
Christopher Fauletc2a89a62017-10-23 15:54:24 +02003353 SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003354 return 1;
3355
3356error:
3357 email_alert_free(alert);
3358 return 0;
3359}
3360
Christopher Faulet0108bb32017-10-20 21:34:32 +02003361static void enqueue_email_alert(struct proxy *p, struct server *s, const char *msg)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003362{
3363 int i;
3364 struct mailer *mailer;
3365
3366 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3367 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
Christopher Faulet0108bb32017-10-20 21:34:32 +02003368 if (!enqueue_one_email_alert(p, s, &p->email_alert.queues[i], msg)) {
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003369 Alert("Email alert [%s] could not be enqueued: out of memory\n", p->id);
3370 return;
3371 }
3372 }
3373
3374 return;
3375}
3376
3377/*
3378 * Send email alert if configured.
3379 */
Simon Horman64e34162015-02-06 11:11:57 +09003380void send_email_alert(struct server *s, int level, const char *format, ...)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003381{
3382 va_list argp;
3383 char buf[1024];
3384 int len;
3385 struct proxy *p = s->proxy;
3386
Christopher Faulet0108bb32017-10-20 21:34:32 +02003387 if (!p->email_alert.mailers.m || level > p->email_alert.level || format == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003388 return;
3389
3390 va_start(argp, format);
3391 len = vsnprintf(buf, sizeof(buf), format, argp);
3392 va_end(argp);
3393
Thierry FOURNIER62c8a212017-02-09 12:19:27 +01003394 if (len < 0 || len >= sizeof(buf)) {
Cyril Bontéb65e0332015-12-04 03:07:08 +01003395 Alert("Email alert [%s] could not format message\n", p->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003396 return;
3397 }
3398
Christopher Faulet0108bb32017-10-20 21:34:32 +02003399 enqueue_email_alert(p, s, buf);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003400}
3401
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003402/*
3403 * Return value:
3404 * the port to be used for the health check
3405 * 0 in case no port could be found for the check
3406 */
3407int srv_check_healthcheck_port(struct check *chk)
3408{
3409 int i = 0;
3410 struct server *srv = NULL;
3411
3412 srv = chk->server;
3413
3414 /* If neither a port nor an addr was specified and no check transport
3415 * layer is forced, then the transport layer used by the checks is the
3416 * same as for the production traffic. Otherwise we use raw_sock by
3417 * default, unless one is specified.
3418 */
3419 if (!chk->port && !is_addr(&chk->addr)) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003420 chk->use_ssl |= (srv->use_ssl || (srv->proxy->options & PR_O_TCPCHK_SSL));
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003421 chk->send_proxy |= (srv->pp_opts);
3422 }
3423
3424 /* by default, we use the health check port ocnfigured */
3425 if (chk->port > 0)
3426 return chk->port;
3427
3428 /* try to get the port from check_core.addr if check.port not set */
3429 i = get_host_port(&chk->addr);
3430 if (i > 0)
3431 return i;
3432
3433 /* try to get the port from server address */
3434 /* prevent MAPPORTS from working at this point, since checks could
3435 * not be performed in such case (MAPPORTS impose a relative ports
3436 * based on live traffic)
3437 */
3438 if (srv->flags & SRV_F_MAPPORTS)
3439 return 0;
Willy Tarreau04276f32017-01-06 17:41:29 +01003440
3441 i = srv->svc_port; /* by default */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003442 if (i > 0)
3443 return i;
3444
3445 return 0;
3446}
3447
Willy Tarreau865c5142016-12-21 20:04:48 +01003448__attribute__((constructor))
3449static void __check_init(void)
3450{
3451 hap_register_post_check(start_checks);
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003452
3453 pool2_email_alert = create_pool("email_alert", sizeof(struct email_alert), MEM_F_SHARED);
3454 pool2_tcpcheck_rule = create_pool("tcpcheck_rule", sizeof(struct tcpcheck_rule), MEM_F_SHARED);
Willy Tarreau865c5142016-12-21 20:04:48 +01003455}
3456
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003457
Willy Tarreaubd741542010-03-16 18:46:54 +01003458/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003459 * Local variables:
3460 * c-indent-level: 8
3461 * c-basic-offset: 8
3462 * End:
3463 */