blob: 2c17c6b8fa89b7dedbec68d3e37cbb6ff18f7674 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Olivier Houchardf886e342017-04-05 22:24:59 +020027#include <net/if.h>
28
William Lallemand74c24fb2016-11-21 17:18:36 +010029#include <common/cfgparse.h>
30#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
36#include <common/ticks.h>
37#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
40#include <common/base64.h>
41
42#include <types/applet.h>
William Lallemand9ed62032016-11-21 17:49:11 +010043#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010044#include <types/global.h>
45#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010046#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010047
48#include <proto/backend.h>
49#include <proto/channel.h>
50#include <proto/checks.h>
51#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010052#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010053#include <proto/fd.h>
54#include <proto/freq_ctr.h>
55#include <proto/frontend.h>
56#include <proto/log.h>
57#include <proto/pattern.h>
58#include <proto/pipe.h>
William Lallemandce83b4a2018-10-26 14:47:30 +020059#include <proto/protocol.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010060#include <proto/listener.h>
61#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010062#include <proto/proxy.h>
63#include <proto/sample.h>
64#include <proto/session.h>
65#include <proto/stream.h>
66#include <proto/server.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010067#include <proto/stream_interface.h>
68#include <proto/task.h>
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +020069#include <proto/proto_udp.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010070
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020071#define PAYLOAD_PATTERN "<<"
72
William Lallemand74c24fb2016-11-21 17:18:36 +010073static struct applet cli_applet;
74
75static const char stats_sock_usage_msg[] =
76 "Unknown command. Please enter one of the following commands only :\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010077 " help : this message\n"
78 " prompt : toggle interactive mode with prompt\n"
79 " quit : disconnect\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010080 "";
81
82static const char stats_permission_denied_msg[] =
83 "Permission denied\n"
84 "";
85
86
Christopher Faulet1bc04c72017-10-29 20:14:08 +010087static THREAD_LOCAL char *dynamic_usage_msg = NULL;
William Lallemand74c24fb2016-11-21 17:18:36 +010088
89/* List head of cli keywords */
90static struct cli_kw_list cli_keywords = {
91 .list = LIST_HEAD_INIT(cli_keywords.list)
92};
93
94extern const char *stat_status_codes[];
95
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020096static char *cli_gen_usage_msg(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +010097{
98 struct cli_kw_list *kw_list;
99 struct cli_kw *kw;
Willy Tarreau83061a82018-07-13 11:56:34 +0200100 struct buffer *tmp = get_trash_chunk();
101 struct buffer out;
William Lallemand74c24fb2016-11-21 17:18:36 +0100102
103 free(dynamic_usage_msg);
104 dynamic_usage_msg = NULL;
105
106 if (LIST_ISEMPTY(&cli_keywords.list))
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200107 goto end;
William Lallemand74c24fb2016-11-21 17:18:36 +0100108
109 chunk_reset(tmp);
110 chunk_strcat(tmp, stats_sock_usage_msg);
111 list_for_each_entry(kw_list, &cli_keywords.list, list) {
112 kw = &kw_list->kw[0];
William Lallemand0154edc2018-05-15 11:50:04 +0200113 while (kw->str_kw[0]) {
114 if (kw->usage)
115 chunk_appendf(tmp, " %s\n", kw->usage);
William Lallemand74c24fb2016-11-21 17:18:36 +0100116 kw++;
117 }
118 }
119 chunk_init(&out, NULL, 0);
120 chunk_dup(&out, tmp);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200121 dynamic_usage_msg = out.area;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200122
123end:
124 if (dynamic_usage_msg) {
125 appctx->ctx.cli.severity = LOG_INFO;
126 appctx->ctx.cli.msg = dynamic_usage_msg;
127 }
128 else {
129 appctx->ctx.cli.severity = LOG_INFO;
130 appctx->ctx.cli.msg = stats_sock_usage_msg;
131 }
132 appctx->st0 = CLI_ST_PRINT;
133
William Lallemand74c24fb2016-11-21 17:18:36 +0100134 return dynamic_usage_msg;
135}
136
137struct cli_kw* cli_find_kw(char **args)
138{
139 struct cli_kw_list *kw_list;
140 struct cli_kw *kw;/* current cli_kw */
141 char **tmp_args;
142 const char **tmp_str_kw;
143 int found = 0;
144
145 if (LIST_ISEMPTY(&cli_keywords.list))
146 return NULL;
147
148 list_for_each_entry(kw_list, &cli_keywords.list, list) {
149 kw = &kw_list->kw[0];
150 while (*kw->str_kw) {
151 tmp_args = args;
152 tmp_str_kw = kw->str_kw;
153 while (*tmp_str_kw) {
154 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
155 found = 1;
156 } else {
157 found = 0;
158 break;
159 }
160 tmp_args++;
161 tmp_str_kw++;
162 }
163 if (found)
164 return (kw);
165 kw++;
166 }
167 }
168 return NULL;
169}
170
171void cli_register_kw(struct cli_kw_list *kw_list)
172{
173 LIST_ADDQ(&cli_keywords.list, &kw_list->list);
174}
175
176
177/* allocate a new stats frontend named <name>, and return it
178 * (or NULL in case of lack of memory).
179 */
180static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
181{
182 struct proxy *fe;
183
184 fe = calloc(1, sizeof(*fe));
185 if (!fe)
186 return NULL;
187
188 init_new_proxy(fe);
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100189 fe->next = proxies_list;
190 proxies_list = fe;
William Lallemand74c24fb2016-11-21 17:18:36 +0100191 fe->last_change = now.tv_sec;
192 fe->id = strdup("GLOBAL");
193 fe->cap = PR_CAP_FE;
194 fe->maxconn = 10; /* default to 10 concurrent connections */
195 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
196 fe->conf.file = strdup(file);
197 fe->conf.line = line;
198 fe->accept = frontend_accept;
199 fe->default_target = &cli_applet.obj_type;
200
201 /* the stats frontend is the only one able to assign ID #0 */
202 fe->conf.id.key = fe->uuid = 0;
203 eb32_insert(&used_proxy_id, &fe->conf.id);
204 return fe;
205}
206
207/* This function parses a "stats" statement in the "global" section. It returns
208 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
209 * error message into the <err> buffer which will be preallocated. The trailing
210 * '\n' must not be written. The function must be called with <args> pointing to
211 * the first word after "stats".
212 */
213static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
214 struct proxy *defpx, const char *file, int line,
215 char **err)
216{
217 struct bind_conf *bind_conf;
218 struct listener *l;
219
220 if (!strcmp(args[1], "socket")) {
221 int cur_arg;
222
223 if (*args[2] == 0) {
224 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
225 return -1;
226 }
227
228 if (!global.stats_fe) {
229 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
230 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
231 return -1;
232 }
233 }
234
Willy Tarreaua261e9b2016-12-22 20:44:00 +0100235 bind_conf = bind_conf_alloc(global.stats_fe, file, line, args[2], xprt_get(XPRT_RAW));
William Lallemand07a62f72017-05-24 00:57:40 +0200236 bind_conf->level &= ~ACCESS_LVL_MASK;
237 bind_conf->level |= ACCESS_LVL_OPER; /* default access level */
William Lallemand74c24fb2016-11-21 17:18:36 +0100238
239 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
240 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
241 file, line, args[0], args[1], err && *err ? *err : "error");
242 return -1;
243 }
244
245 cur_arg = 3;
246 while (*args[cur_arg]) {
247 static int bind_dumped;
248 struct bind_kw *kw;
249
250 kw = bind_find_kw(args[cur_arg]);
251 if (kw) {
252 if (!kw->parse) {
253 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
254 args[0], args[1], args[cur_arg]);
255 return -1;
256 }
257
258 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
259 if (err && *err)
260 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
261 else
262 memprintf(err, "'%s %s' : error encountered while processing '%s'",
263 args[0], args[1], args[cur_arg]);
264 return -1;
265 }
266
267 cur_arg += 1 + kw->skip;
268 continue;
269 }
270
271 if (!bind_dumped) {
272 bind_dump_kws(err);
273 indent_msg(err, 4);
274 bind_dumped = 1;
275 }
276
277 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
278 args[0], args[1], args[cur_arg],
279 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
280 return -1;
281 }
282
283 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
284 l->maxconn = global.stats_fe->maxconn;
285 l->backlog = global.stats_fe->backlog;
286 l->accept = session_accept_fd;
William Lallemand74c24fb2016-11-21 17:18:36 +0100287 l->default_target = global.stats_fe->default_target;
288 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
289 l->nice = -64; /* we want to boost priority for local stats */
290 global.maxsock += l->maxconn;
291 }
292 }
293 else if (!strcmp(args[1], "timeout")) {
294 unsigned timeout;
295 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
296
297 if (res) {
298 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
299 return -1;
300 }
301
302 if (!timeout) {
303 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
304 return -1;
305 }
306 if (!global.stats_fe) {
307 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
308 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
309 return -1;
310 }
311 }
312 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
313 }
314 else if (!strcmp(args[1], "maxconn")) {
315 int maxconn = atol(args[2]);
316
317 if (maxconn <= 0) {
318 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
319 return -1;
320 }
321
322 if (!global.stats_fe) {
323 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
324 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
325 return -1;
326 }
327 }
328 global.stats_fe->maxconn = maxconn;
329 }
330 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
331 int cur_arg = 2;
332 unsigned long set = 0;
333
334 if (!global.stats_fe) {
335 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
336 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
337 return -1;
338 }
339 }
340
341 while (*args[cur_arg]) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100342 if (strcmp(args[cur_arg], "all") == 0) {
343 set = 0;
344 break;
345 }
Christopher Faulet26028f62017-11-22 15:01:51 +0100346 if (parse_process_number(args[cur_arg], &set, NULL, err)) {
Christopher Fauletf1f0c5f2017-11-22 12:06:43 +0100347 memprintf(err, "'%s %s' : %s", args[0], args[1], *err);
William Lallemand74c24fb2016-11-21 17:18:36 +0100348 return -1;
349 }
350 cur_arg++;
351 }
352 global.stats_fe->bind_proc = set;
353 }
354 else {
355 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
356 return -1;
357 }
358 return 0;
359}
360
Willy Tarreaude57a572016-11-23 17:01:39 +0100361/* Verifies that the CLI at least has a level at least as high as <level>
362 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
363 * failure, an error message is prepared and the appctx's state is adjusted
364 * to print it so that a return 1 is enough to abort any processing.
365 */
366int cli_has_level(struct appctx *appctx, int level)
367{
368 struct stream_interface *si = appctx->owner;
369 struct stream *s = si_strm(si);
370
William Lallemand07a62f72017-05-24 00:57:40 +0200371 if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < level) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200372 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaude57a572016-11-23 17:01:39 +0100373 appctx->ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100374 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaude57a572016-11-23 17:01:39 +0100375 return 0;
376 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100377 return 1;
378}
379
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200380/* Returns severity_output for the current session if set, or default for the socket */
381static int cli_get_severity_output(struct appctx *appctx)
382{
383 if (appctx->cli_severity_output)
384 return appctx->cli_severity_output;
385 return strm_li(si_strm(appctx->owner))->bind_conf->severity_output;
386}
William Lallemand74c24fb2016-11-21 17:18:36 +0100387
Willy Tarreau41908562016-11-24 16:23:38 +0100388/* Processes the CLI interpreter on the stats socket. This function is called
389 * from the CLI's IO handler running in an appctx context. The function returns 1
390 * if the request was understood, otherwise zero. It is called with appctx->st0
391 * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do
392 * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to
393 * have its own I/O handler called again. Most of the time, parsers will only
394 * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg.
Willy Tarreaueaffde32016-12-16 17:59:25 +0100395 * If a keyword parser is NULL and an I/O handler is declared, the I/O handler
396 * will automatically be used.
William Lallemand74c24fb2016-11-21 17:18:36 +0100397 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200398static int cli_parse_request(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100399{
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200400 char *args[MAX_STATS_ARGS + 1], *p, *end, *payload = NULL;
401 int i = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100402 struct cli_kw *kw;
William Lallemand74c24fb2016-11-21 17:18:36 +0100403
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200404 appctx->st2 = 0;
405 memset(&appctx->ctx.cli, 0, sizeof(appctx->ctx.cli));
William Lallemand74c24fb2016-11-21 17:18:36 +0100406
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200407 p = appctx->chunk->area;
408 end = p + appctx->chunk->data;
William Lallemand74c24fb2016-11-21 17:18:36 +0100409
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200410 /*
411 * Get the payload start if there is one.
412 * For the sake of simplicity, the payload pattern is looked up
413 * everywhere from the start of the input but it can only be found
414 * at the end of the first line if APPCTX_CLI_ST1_PAYLOAD is set.
415 *
416 * The input string was zero terminated so it is safe to use
417 * the str*() functions throughout the parsing
418 */
419 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
420 payload = strstr(p, PAYLOAD_PATTERN);
421 end = payload;
422 /* skip the pattern */
423 payload += strlen(PAYLOAD_PATTERN);
424 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100425
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200426 /*
427 * Get pointers on words.
428 * One extra slot is reserved to store a pointer on a null byte.
429 */
430 while (i < MAX_STATS_ARGS && p < end) {
431 int j, k;
William Lallemand74c24fb2016-11-21 17:18:36 +0100432
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200433 /* skip leading spaces/tabs */
434 p += strspn(p, " \t");
435 if (!*p)
436 break;
William Lallemand74c24fb2016-11-21 17:18:36 +0100437
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200438 args[i] = p;
439 p += strcspn(p, " \t");
440 *p++ = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100441
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200442 /* unescape backslashes (\) */
443 for (j = 0, k = 0; args[i][k]; k++) {
444 if (args[i][k] == '\\') {
445 if (args[i][k + 1] == '\\')
446 k++;
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100447 else
448 continue;
449 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200450 args[i][j] = args[i][k];
William Lallemand74c24fb2016-11-21 17:18:36 +0100451 j++;
452 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200453 args[i][j] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100454
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200455 i++;
456 }
457 /* fill unused slots */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200458 p = appctx->chunk->area + appctx->chunk->data;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200459 for (; i < MAX_STATS_ARGS + 1; i++)
460 args[i] = p;
Willy Tarreau41908562016-11-24 16:23:38 +0100461
462 kw = cli_find_kw(args);
Willy Tarreaueaffde32016-12-16 17:59:25 +0100463 if (!kw)
Willy Tarreau41908562016-11-24 16:23:38 +0100464 return 0;
465
466 appctx->io_handler = kw->io_handler;
Emeric Brund6871f72017-06-29 19:54:13 +0200467 appctx->io_release = kw->io_release;
468 /* kw->parse could set its own io_handler or ip_release handler */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200469 if ((!kw->parse || kw->parse(args, payload, appctx, kw->private) == 0) && appctx->io_handler) {
Willy Tarreau41908562016-11-24 16:23:38 +0100470 appctx->st0 = CLI_ST_CALLBACK;
William Lallemand74c24fb2016-11-21 17:18:36 +0100471 }
Willy Tarreau41908562016-11-24 16:23:38 +0100472 return 1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100473}
474
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200475/* prepends then outputs the argument msg with a syslog-type severity depending on severity_output value */
476static int cli_output_msg(struct channel *chn, const char *msg, int severity, int severity_output)
477{
Willy Tarreau83061a82018-07-13 11:56:34 +0200478 struct buffer *tmp;
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200479
480 if (likely(severity_output == CLI_SEVERITY_NONE))
Willy Tarreau06d80a92017-10-19 14:32:15 +0200481 return ci_putblk(chn, msg, strlen(msg));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200482
483 tmp = get_trash_chunk();
484 chunk_reset(tmp);
485
486 if (severity < 0 || severity > 7) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100487 ha_warning("socket command feedback with invalid severity %d", severity);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200488 chunk_printf(tmp, "[%d]: ", severity);
489 }
490 else {
491 switch (severity_output) {
492 case CLI_SEVERITY_NUMBER:
493 chunk_printf(tmp, "[%d]: ", severity);
494 break;
495 case CLI_SEVERITY_STRING:
496 chunk_printf(tmp, "[%s]: ", log_levels[severity]);
497 break;
498 default:
Christopher Faulet767a84b2017-11-24 16:50:31 +0100499 ha_warning("Unrecognized severity output %d", severity_output);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200500 }
501 }
502 chunk_appendf(tmp, "%s", msg);
503
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200504 return ci_putblk(chn, tmp->area, strlen(tmp->area));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200505}
506
William Lallemand74c24fb2016-11-21 17:18:36 +0100507/* This I/O handler runs as an applet embedded in a stream interface. It is
508 * used to processes I/O from/to the stats unix socket. The system relies on a
509 * state machine handling requests and various responses. We read a request,
510 * then we process it and send the response, and we possibly display a prompt.
511 * Then we can read again. The state is stored in appctx->st0 and is one of the
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100512 * CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled
William Lallemand74c24fb2016-11-21 17:18:36 +0100513 * or not.
514 */
515static void cli_io_handler(struct appctx *appctx)
516{
517 struct stream_interface *si = appctx->owner;
518 struct channel *req = si_oc(si);
519 struct channel *res = si_ic(si);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200520 struct bind_conf *bind_conf = strm_li(si_strm(si))->bind_conf;
William Lallemand74c24fb2016-11-21 17:18:36 +0100521 int reql;
522 int len;
523
524 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
525 goto out;
526
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100527 /* Check if the input buffer is avalaible. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200528 if (res->buf.size == 0) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100529 si_applet_cant_put(si);
530 goto out;
531 }
532
William Lallemand74c24fb2016-11-21 17:18:36 +0100533 while (1) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100534 if (appctx->st0 == CLI_ST_INIT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100535 /* Stats output not initialized yet */
536 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200537 /* reset severity to default at init */
538 appctx->cli_severity_output = bind_conf->severity_output;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100539 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100540 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100541 else if (appctx->st0 == CLI_ST_END) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100542 /* Let's close for real now. We just close the request
543 * side, the conditions below will complete if needed.
544 */
545 si_shutw(si);
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200546 free_trash_chunk(appctx->chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +0100547 break;
548 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100549 else if (appctx->st0 == CLI_ST_GETREQ) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200550 char *str;
551
552 /* use a trash chunk to store received data */
553 if (!appctx->chunk) {
554 appctx->chunk = alloc_trash_chunk();
555 if (!appctx->chunk) {
556 appctx->st0 = CLI_ST_END;
557 continue;
558 }
559 }
560
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200561 str = appctx->chunk->area + appctx->chunk->data;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200562
William Lallemand74c24fb2016-11-21 17:18:36 +0100563 /* ensure we have some output room left in the event we
564 * would want to return some info right after parsing.
565 */
566 if (buffer_almost_full(si_ib(si))) {
567 si_applet_cant_put(si);
568 break;
569 }
570
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200571 /* '- 1' is to ensure a null byte can always be inserted at the end */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200572 reql = co_getline(si_oc(si), str,
573 appctx->chunk->size - appctx->chunk->data - 1);
William Lallemand74c24fb2016-11-21 17:18:36 +0100574 if (reql <= 0) { /* closed or EOL not found */
575 if (reql == 0)
576 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100577 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100578 continue;
579 }
580
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200581 if (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)) {
582 /* seek for a possible unescaped semi-colon. If we find
583 * one, we replace it with an LF and skip only this part.
584 */
585 for (len = 0; len < reql; len++) {
586 if (str[len] == '\\') {
587 len++;
588 continue;
589 }
590 if (str[len] == ';') {
591 str[len] = '\n';
592 reql = len + 1;
593 break;
594 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100595 }
596 }
597
598 /* now it is time to check that we have a full line,
599 * remove the trailing \n and possibly \r, then cut the
600 * line.
601 */
602 len = reql - 1;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200603 if (str[len] != '\n') {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100604 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100605 continue;
606 }
607
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200608 if (len && str[len-1] == '\r')
William Lallemand74c24fb2016-11-21 17:18:36 +0100609 len--;
610
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200611 str[len] = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200612 appctx->chunk->data += len;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200613
614 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200615 appctx->chunk->area[appctx->chunk->data] = '\n';
616 appctx->chunk->area[appctx->chunk->data + 1] = 0;
617 appctx->chunk->data++;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200618 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100619
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100620 appctx->st0 = CLI_ST_PROMPT;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200621
622 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
623 /* empty line */
624 if (!len) {
625 /* remove the last two \n */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200626 appctx->chunk->data -= 2;
627 appctx->chunk->area[appctx->chunk->data] = 0;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200628
629 if (!cli_parse_request(appctx))
630 cli_gen_usage_msg(appctx);
631
632 chunk_reset(appctx->chunk);
633 /* NB: cli_sock_parse_request() may have put
634 * another CLI_ST_O_* into appctx->st0.
635 */
636
637 appctx->st1 &= ~APPCTX_CLI_ST1_PAYLOAD;
William Lallemand74c24fb2016-11-21 17:18:36 +0100638 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100639 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200640 else {
641 /*
642 * Look for the "payload start" pattern at the end of a line
643 * Its location is not remembered here, this is just to switch
644 * to a gathering mode.
William Lallemand74c24fb2016-11-21 17:18:36 +0100645 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200646 if (!strcmp(appctx->chunk->area + appctx->chunk->data - strlen(PAYLOAD_PATTERN), PAYLOAD_PATTERN))
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200647 appctx->st1 |= APPCTX_CLI_ST1_PAYLOAD;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200648 else {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200649 /* no payload, the command is complete: parse the request */
650 if (!cli_parse_request(appctx))
651 cli_gen_usage_msg(appctx);
652
653 chunk_reset(appctx->chunk);
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200654 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100655 }
656
657 /* re-adjust req buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200658 co_skip(si_oc(si), reql);
William Lallemand74c24fb2016-11-21 17:18:36 +0100659 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
660 }
661 else { /* output functions */
662 switch (appctx->st0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100663 case CLI_ST_PROMPT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100664 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100665 case CLI_ST_PRINT:
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200666 if (cli_output_msg(res, appctx->ctx.cli.msg, appctx->ctx.cli.severity,
667 cli_get_severity_output(appctx)) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100668 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100669 else
670 si_applet_cant_put(si);
671 break;
Aurélien Nephtalic511b7c2018-04-16 18:50:19 +0200672 case CLI_ST_PRINT_FREE: {
673 const char *msg = appctx->ctx.cli.err;
674
675 if (!msg)
676 msg = "Out of memory.\n";
677
678 if (cli_output_msg(res, msg, LOG_ERR, cli_get_severity_output(appctx)) != -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100679 free(appctx->ctx.cli.err);
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100680 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100681 }
682 else
683 si_applet_cant_put(si);
684 break;
Aurélien Nephtalic511b7c2018-04-16 18:50:19 +0200685 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100686 case CLI_ST_CALLBACK: /* use custom pointer */
William Lallemand74c24fb2016-11-21 17:18:36 +0100687 if (appctx->io_handler)
688 if (appctx->io_handler(appctx)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100689 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100690 if (appctx->io_release) {
691 appctx->io_release(appctx);
692 appctx->io_release = NULL;
693 }
694 }
695 break;
696 default: /* abnormal state */
697 si->flags |= SI_FL_ERR;
698 break;
699 }
700
701 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100702 if (appctx->st0 == CLI_ST_PROMPT) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200703 const char *prompt = "";
704
705 if (appctx->st1 & APPCTX_CLI_ST1_PROMPT) {
706 /*
707 * when entering a payload with interactive mode, change the prompt
708 * to emphasize that more data can still be sent
709 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200710 if (appctx->chunk->data && appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200711 prompt = "+ ";
712 else
713 prompt = "\n> ";
714 }
715 else {
716 if (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD))
717 prompt = "\n";
718 }
719
720 if (ci_putstr(si_ic(si), prompt) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100721 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100722 else
723 si_applet_cant_put(si);
724 }
725
726 /* If the output functions are still there, it means they require more room. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100727 if (appctx->st0 >= CLI_ST_OUTPUT)
William Lallemand74c24fb2016-11-21 17:18:36 +0100728 break;
729
730 /* Now we close the output if one of the writers did so,
731 * or if we're not in interactive mode and the request
732 * buffer is empty. This still allows pipelined requests
733 * to be sent in non-interactive mode.
734 */
Willy Tarreau851d12c2018-06-19 07:01:36 +0200735 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!(appctx->st1 & APPCTX_CLI_ST1_PROMPT) && !co_data(req))) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100736 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100737 continue;
738 }
739
740 /* switch state back to GETREQ to read next requests */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100741 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100742 }
743 }
744
745 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
746 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
747 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
748 /* Other side has closed, let's abort if we have no more processing to do
749 * and nothing more to consume. This is comparable to a broken pipe, so
750 * we forward the close to the request side so that it flows upstream to
751 * the client.
752 */
753 si_shutw(si);
754 }
755
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100756 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < CLI_ST_OUTPUT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100757 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
758 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
759 /* We have no more processing to do, and nothing more to send, and
760 * the client side has closed. So we'll forward this state downstream
761 * on the response buffer.
762 */
763 si_shutr(si);
764 res->flags |= CF_READ_NULL;
765 }
766
767 out:
Christopher Faulet45073512018-07-20 10:16:29 +0200768 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%lu, rqs=%lu, rh=%lu, rs=%lu\n",
William Lallemand74c24fb2016-11-21 17:18:36 +0100769 __FUNCTION__, __LINE__,
Christopher Faulet45073512018-07-20 10:16:29 +0200770 si->state, req->flags, res->flags, ci_data(req), co_data(req), ci_data(res), co_data(res));
William Lallemand74c24fb2016-11-21 17:18:36 +0100771}
772
William Lallemand74c24fb2016-11-21 17:18:36 +0100773/* This is called when the stream interface is closed. For instance, upon an
774 * external abort, we won't call the i/o handler anymore so we may need to
775 * remove back references to the stream currently being dumped.
776 */
777static void cli_release_handler(struct appctx *appctx)
778{
779 if (appctx->io_release) {
780 appctx->io_release(appctx);
781 appctx->io_release = NULL;
782 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100783 else if (appctx->st0 == CLI_ST_PRINT_FREE) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100784 free(appctx->ctx.cli.err);
785 appctx->ctx.cli.err = NULL;
786 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100787}
788
789/* This function dumps all environmnent variables to the buffer. It returns 0
790 * if the output buffer is full and it needs to be called again, otherwise
Willy Tarreauf6710f82016-12-16 17:45:44 +0100791 * non-zero. Dumps only one entry if st2 == STAT_ST_END. It uses cli.p0 as the
792 * pointer to the current variable.
William Lallemand74c24fb2016-11-21 17:18:36 +0100793 */
Willy Tarreau0a739292016-11-22 20:21:23 +0100794static int cli_io_handler_show_env(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100795{
Willy Tarreau0a739292016-11-22 20:21:23 +0100796 struct stream_interface *si = appctx->owner;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100797 char **var = appctx->ctx.cli.p0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100798
799 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
800 return 1;
801
802 chunk_reset(&trash);
803
804 /* we have two inner loops here, one for the proxy, the other one for
805 * the buffer.
806 */
Willy Tarreauf6710f82016-12-16 17:45:44 +0100807 while (*var) {
808 chunk_printf(&trash, "%s\n", *var);
William Lallemand74c24fb2016-11-21 17:18:36 +0100809
Willy Tarreau06d80a92017-10-19 14:32:15 +0200810 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100811 si_applet_cant_put(si);
812 return 0;
813 }
814 if (appctx->st2 == STAT_ST_END)
815 break;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100816 var++;
817 appctx->ctx.cli.p0 = var;
William Lallemand74c24fb2016-11-21 17:18:36 +0100818 }
819
820 /* dump complete */
821 return 1;
822}
823
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200824/* This function dumps all file descriptors states (or the requested one) to
825 * the buffer. It returns 0 if the output buffer is full and it needs to be
826 * called again, otherwise non-zero. Dumps only one entry if st2 == STAT_ST_END.
827 * It uses cli.i0 as the fd number to restart from.
828 */
829static int cli_io_handler_show_fd(struct appctx *appctx)
830{
831 struct stream_interface *si = appctx->owner;
832 int fd = appctx->ctx.cli.i0;
833
834 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
835 return 1;
836
837 chunk_reset(&trash);
838
839 /* we have two inner loops here, one for the proxy, the other one for
840 * the buffer.
841 */
Aurélien Nephtali498a1152018-03-09 18:51:16 +0100842 while (fd >= 0 && fd < global.maxsock) {
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200843 struct fdtab fdt;
Willy Tarreau286ec682017-08-09 16:35:44 +0200844 struct listener *li = NULL;
845 struct server *sv = NULL;
846 struct proxy *px = NULL;
Willy Tarreaua833cd92018-03-29 13:19:37 +0200847 const struct mux_ops *mux = NULL;
Willy Tarreau35b1b482018-03-28 18:41:30 +0200848 void *ctx = NULL;
Willy Tarreau286ec682017-08-09 16:35:44 +0200849 uint32_t conn_flags = 0;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200850
Willy Tarreaubf9fd652018-08-02 11:05:48 +0200851 thread_isolate();
852
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200853 fdt = fdtab[fd];
854
Willy Tarreaubf9fd652018-08-02 11:05:48 +0200855 if (!fdt.owner) {
856 thread_release();
Willy Tarreau017af242017-10-04 20:24:54 +0200857 goto skip; // closed
Willy Tarreaubf9fd652018-08-02 11:05:48 +0200858 }
Willy Tarreau017af242017-10-04 20:24:54 +0200859
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200860 if (fdt.iocb == conn_fd_handler) {
861 conn_flags = ((struct connection *)fdt.owner)->flags;
Willy Tarreau35b1b482018-03-28 18:41:30 +0200862 mux = ((struct connection *)fdt.owner)->mux;
863 ctx = ((struct connection *)fdt.owner)->mux_ctx;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200864 li = objt_listener(((struct connection *)fdt.owner)->target);
865 sv = objt_server(((struct connection *)fdt.owner)->target);
866 px = objt_proxy(((struct connection *)fdt.owner)->target);
867 }
868 else if (fdt.iocb == listener_accept)
869 li = fdt.owner;
870
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200871 chunk_printf(&trash,
Willy Tarreauc754b342018-03-30 15:00:15 +0200872 " %5d : st=0x%02x(R:%c%c%c W:%c%c%c) ev=0x%02x(%c%c%c%c%c) [%c%c] cnext=%d cprev=%d tmask=0x%lx umask=0x%lx owner=%p iocb=%p(%s)",
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200873 fd,
874 fdt.state,
875 (fdt.state & FD_EV_POLLED_R) ? 'P' : 'p',
876 (fdt.state & FD_EV_READY_R) ? 'R' : 'r',
877 (fdt.state & FD_EV_ACTIVE_R) ? 'A' : 'a',
878 (fdt.state & FD_EV_POLLED_W) ? 'P' : 'p',
879 (fdt.state & FD_EV_READY_W) ? 'R' : 'r',
880 (fdt.state & FD_EV_ACTIVE_W) ? 'A' : 'a',
881 fdt.ev,
882 (fdt.ev & FD_POLL_HUP) ? 'H' : 'h',
883 (fdt.ev & FD_POLL_ERR) ? 'E' : 'e',
884 (fdt.ev & FD_POLL_OUT) ? 'O' : 'o',
885 (fdt.ev & FD_POLL_PRI) ? 'P' : 'p',
886 (fdt.ev & FD_POLL_IN) ? 'I' : 'i',
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200887 fdt.linger_risk ? 'L' : 'l',
888 fdt.cloned ? 'C' : 'c',
Willy Tarreauc754b342018-03-30 15:00:15 +0200889 fdt.cache.next,
890 fdt.cache.prev,
891 fdt.thread_mask, fdt.update_mask,
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200892 fdt.owner,
893 fdt.iocb,
894 (fdt.iocb == conn_fd_handler) ? "conn_fd_handler" :
895 (fdt.iocb == dgram_fd_handler) ? "dgram_fd_handler" :
896 (fdt.iocb == listener_accept) ? "listener_accept" :
Olivier Houchard79321b92018-07-26 17:55:11 +0200897 (fdt.iocb == poller_pipe_io_handler) ? "poller_pipe_io_handler" :
Willy Tarreauc754b342018-03-30 15:00:15 +0200898 "unknown");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200899
900 if (fdt.iocb == conn_fd_handler) {
901 chunk_appendf(&trash, " cflg=0x%08x", conn_flags);
902 if (px)
903 chunk_appendf(&trash, " px=%s", px->id);
904 else if (sv)
905 chunk_appendf(&trash, " sv=%s/%s", sv->id, sv->proxy->id);
906 else if (li)
907 chunk_appendf(&trash, " fe=%s", li->bind_conf->frontend->id);
Willy Tarreau35b1b482018-03-28 18:41:30 +0200908
Willy Tarreaub011d8f2018-03-30 14:41:19 +0200909 if (mux) {
Willy Tarreau35b1b482018-03-28 18:41:30 +0200910 chunk_appendf(&trash, " mux=%s mux_ctx=%p", mux->name, ctx);
Willy Tarreaub011d8f2018-03-30 14:41:19 +0200911 if (mux->show_fd)
912 mux->show_fd(&trash, fdt.owner);
913 }
Willy Tarreau35b1b482018-03-28 18:41:30 +0200914 else
915 chunk_appendf(&trash, " nomux");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200916 }
917 else if (fdt.iocb == listener_accept) {
918 chunk_appendf(&trash, " l.st=%s fe=%s",
919 listener_state_str(li),
920 li->bind_conf->frontend->id);
921 }
922
Willy Tarreaubf9fd652018-08-02 11:05:48 +0200923 thread_release();
924
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200925 chunk_appendf(&trash, "\n");
926
Willy Tarreau06d80a92017-10-19 14:32:15 +0200927 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200928 si_applet_cant_put(si);
929 return 0;
930 }
931 skip:
932 if (appctx->st2 == STAT_ST_END)
933 break;
934
935 fd++;
936 appctx->ctx.cli.i0 = fd;
937 }
938
939 /* dump complete */
940 return 1;
941}
942
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100943/* This function dumps some activity counters used by developers and support to
944 * rule out some hypothesis during bug reports. It returns 0 if the output
945 * buffer is full and it needs to be called again, otherwise non-zero. It dumps
946 * everything at once in the buffer and is not designed to do it in multiple
947 * passes.
948 */
949static int cli_io_handler_show_activity(struct appctx *appctx)
950{
951 struct stream_interface *si = appctx->owner;
952 int thr;
953
954 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
955 return 1;
956
957 chunk_reset(&trash);
958
959 chunk_appendf(&trash, "thread_id: %u", tid);
960 chunk_appendf(&trash, "\ndate_now: %lu.%06lu", (long)now.tv_sec, (long)now.tv_usec);
961 chunk_appendf(&trash, "\nloops:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].loops);
962 chunk_appendf(&trash, "\nwake_cache:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].wake_cache);
963 chunk_appendf(&trash, "\nwake_tasks:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].wake_tasks);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100964 chunk_appendf(&trash, "\nwake_signal:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].wake_signal);
965 chunk_appendf(&trash, "\npoll_exp:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_exp);
966 chunk_appendf(&trash, "\npoll_drop:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_drop);
967 chunk_appendf(&trash, "\npoll_dead:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_dead);
968 chunk_appendf(&trash, "\npoll_skip:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_skip);
969 chunk_appendf(&trash, "\nfd_skip:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].fd_skip);
970 chunk_appendf(&trash, "\nfd_lock:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].fd_lock);
971 chunk_appendf(&trash, "\nfd_del:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].fd_del);
972 chunk_appendf(&trash, "\nconn_dead:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].conn_dead);
973 chunk_appendf(&trash, "\nstream:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].stream);
974 chunk_appendf(&trash, "\nempty_rq:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].empty_rq);
975 chunk_appendf(&trash, "\nlong_rq:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].long_rq);
Willy Tarreaued72d822018-10-17 19:01:24 +0200976 chunk_appendf(&trash, "\ncpust_tot:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].cpust_total/2);
977 chunk_appendf(&trash, "\ncpust_1s:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", read_freq_ctr(&activity[thr].cpust_1s)/2);
978 chunk_appendf(&trash, "\ncpust_15s:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", read_freq_ctr_period(&activity[thr].cpust_15s, 15000)/2);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100979
980 chunk_appendf(&trash, "\n");
981
982 if (ci_putchk(si_ic(si), &trash) == -1) {
983 chunk_reset(&trash);
984 chunk_printf(&trash, "[output too large, cannot dump]\n");
985 si_applet_cant_put(si);
986 }
987
988 /* dump complete */
989 return 1;
990}
991
William Lallemandeceddf72016-12-15 18:06:44 +0100992/*
Willy Tarreau3af9d832016-12-16 12:58:09 +0100993 * CLI IO handler for `show cli sockets`.
994 * Uses ctx.cli.p0 to store the restart pointer.
William Lallemandeceddf72016-12-15 18:06:44 +0100995 */
996static int cli_io_handler_show_cli_sock(struct appctx *appctx)
997{
998 struct bind_conf *bind_conf;
999 struct stream_interface *si = appctx->owner;
1000
1001 chunk_reset(&trash);
1002
1003 switch (appctx->st2) {
1004 case STAT_ST_INIT:
1005 chunk_printf(&trash, "# socket lvl processes\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001006 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandeceddf72016-12-15 18:06:44 +01001007 si_applet_cant_put(si);
1008 return 0;
1009 }
William Lallemandeceddf72016-12-15 18:06:44 +01001010 appctx->st2 = STAT_ST_LIST;
1011
1012 case STAT_ST_LIST:
1013 if (global.stats_fe) {
1014 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
1015 struct listener *l;
1016
1017 /*
Willy Tarreau3af9d832016-12-16 12:58:09 +01001018 * get the latest dumped node in appctx->ctx.cli.p0
William Lallemandeceddf72016-12-15 18:06:44 +01001019 * if the current node is the first of the list
1020 */
1021
Willy Tarreau3af9d832016-12-16 12:58:09 +01001022 if (appctx->ctx.cli.p0 &&
1023 &bind_conf->by_fe == (&global.stats_fe->conf.bind)->n) {
William Lallemandeceddf72016-12-15 18:06:44 +01001024 /* change the current node to the latest dumped and continue the loop */
Willy Tarreau3af9d832016-12-16 12:58:09 +01001025 bind_conf = LIST_ELEM(appctx->ctx.cli.p0, typeof(bind_conf), by_fe);
William Lallemandeceddf72016-12-15 18:06:44 +01001026 continue;
1027 }
1028
1029 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
1030
1031 char addr[46];
1032 char port[6];
1033
1034 if (l->addr.ss_family == AF_UNIX) {
1035 const struct sockaddr_un *un;
1036
1037 un = (struct sockaddr_un *)&l->addr;
1038 chunk_appendf(&trash, "%s ", un->sun_path);
1039 } else if (l->addr.ss_family == AF_INET) {
1040 addr_to_str(&l->addr, addr, sizeof(addr));
1041 port_to_str(&l->addr, port, sizeof(port));
1042 chunk_appendf(&trash, "%s:%s ", addr, port);
1043 } else if (l->addr.ss_family == AF_INET6) {
1044 addr_to_str(&l->addr, addr, sizeof(addr));
1045 port_to_str(&l->addr, port, sizeof(port));
1046 chunk_appendf(&trash, "[%s]:%s ", addr, port);
1047 } else
1048 continue;
1049
William Lallemand07a62f72017-05-24 00:57:40 +02001050 if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN)
William Lallemandeceddf72016-12-15 18:06:44 +01001051 chunk_appendf(&trash, "admin ");
William Lallemand07a62f72017-05-24 00:57:40 +02001052 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER)
1053 chunk_appendf(&trash, "operator ");
1054 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_USER)
1055 chunk_appendf(&trash, "user ");
William Lallemandeceddf72016-12-15 18:06:44 +01001056 else
1057 chunk_appendf(&trash, " ");
1058
1059 if (bind_conf->bind_proc != 0) {
1060 int pos;
Willy Tarreau20c5e522016-12-16 12:50:55 +01001061 for (pos = 0; pos < 8 * sizeof(bind_conf->bind_proc); pos++) {
Willy Tarreau4305ac72016-12-16 12:56:31 +01001062 if (bind_conf->bind_proc & (1UL << pos)) {
William Lallemandeceddf72016-12-15 18:06:44 +01001063 chunk_appendf(&trash, "%d,", pos+1);
1064 }
1065 }
1066 /* replace the latest comma by a newline */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001067 trash.area[trash.data-1] = '\n';
William Lallemandeceddf72016-12-15 18:06:44 +01001068
1069 } else {
1070 chunk_appendf(&trash, "all\n");
1071 }
1072
Willy Tarreau06d80a92017-10-19 14:32:15 +02001073 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandeceddf72016-12-15 18:06:44 +01001074 si_applet_cant_put(si);
1075 return 0;
1076 }
1077 }
Willy Tarreau3af9d832016-12-16 12:58:09 +01001078 appctx->ctx.cli.p0 = &bind_conf->by_fe; /* store the latest list node dumped */
William Lallemandeceddf72016-12-15 18:06:44 +01001079 }
1080 }
1081 default:
1082 appctx->st2 = STAT_ST_FIN;
1083 return 1;
1084 }
1085}
1086
1087
Willy Tarreau0a739292016-11-22 20:21:23 +01001088/* parse a "show env" CLI request. Returns 0 if it needs to continue, 1 if it
Willy Tarreauf6710f82016-12-16 17:45:44 +01001089 * wants to stop here. It puts the variable to be dumped into cli.p0 if a single
1090 * variable is requested otherwise puts environ there.
Willy Tarreau0a739292016-11-22 20:21:23 +01001091 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001092static int cli_parse_show_env(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0a739292016-11-22 20:21:23 +01001093{
1094 extern char **environ;
Willy Tarreauf6710f82016-12-16 17:45:44 +01001095 char **var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001096
1097 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1098 return 1;
1099
Willy Tarreauf6710f82016-12-16 17:45:44 +01001100 var = environ;
Willy Tarreau0a739292016-11-22 20:21:23 +01001101
1102 if (*args[2]) {
1103 int len = strlen(args[2]);
1104
Willy Tarreauf6710f82016-12-16 17:45:44 +01001105 for (; *var; var++) {
1106 if (strncmp(*var, args[2], len) == 0 &&
1107 (*var)[len] == '=')
Willy Tarreau0a739292016-11-22 20:21:23 +01001108 break;
1109 }
Willy Tarreauf6710f82016-12-16 17:45:44 +01001110 if (!*var) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001111 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau0a739292016-11-22 20:21:23 +01001112 appctx->ctx.cli.msg = "Variable not found\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001113 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau0a739292016-11-22 20:21:23 +01001114 return 1;
1115 }
1116 appctx->st2 = STAT_ST_END;
1117 }
Willy Tarreauf6710f82016-12-16 17:45:44 +01001118 appctx->ctx.cli.p0 = var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001119 return 0;
1120}
1121
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001122/* parse a "show fd" CLI request. Returns 0 if it needs to continue, 1 if it
1123 * wants to stop here. It puts the FD number into cli.i0 if a specific FD is
1124 * requested and sets st2 to STAT_ST_END, otherwise leaves 0 in i0.
1125 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001126static int cli_parse_show_fd(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001127{
1128 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1129 return 1;
1130
1131 appctx->ctx.cli.i0 = 0;
1132
1133 if (*args[2]) {
1134 appctx->ctx.cli.i0 = atoi(args[2]);
1135 appctx->st2 = STAT_ST_END;
1136 }
1137 return 0;
1138}
1139
Willy Tarreau599852e2016-11-22 20:33:32 +01001140/* parse a "set timeout" CLI request. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001141static int cli_parse_set_timeout(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau599852e2016-11-22 20:33:32 +01001142{
1143 struct stream_interface *si = appctx->owner;
1144 struct stream *s = si_strm(si);
1145
1146 if (strcmp(args[2], "cli") == 0) {
1147 unsigned timeout;
1148 const char *res;
1149
1150 if (!*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001151 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau599852e2016-11-22 20:33:32 +01001152 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001153 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001154 return 1;
1155 }
1156
1157 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1158 if (res || timeout < 1) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001159 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau599852e2016-11-22 20:33:32 +01001160 appctx->ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001161 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001162 return 1;
1163 }
1164
1165 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
1166 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
1167 return 1;
1168 }
1169 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001170 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau599852e2016-11-22 20:33:32 +01001171 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001172 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001173 return 1;
1174 }
1175}
1176
Willy Tarreau2af99412016-11-23 11:10:59 +01001177/* parse a "set maxconn global" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001178static int cli_parse_set_maxconn_global(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2af99412016-11-23 11:10:59 +01001179{
1180 int v;
1181
1182 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1183 return 1;
1184
1185 if (!*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001186 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau2af99412016-11-23 11:10:59 +01001187 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001188 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +01001189 return 1;
1190 }
1191
1192 v = atoi(args[3]);
1193 if (v > global.hardmaxconn) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001194 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau2af99412016-11-23 11:10:59 +01001195 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001196 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +01001197 return 1;
1198 }
1199
1200 /* check for unlimited values */
1201 if (v <= 0)
1202 v = global.hardmaxconn;
1203
1204 global.maxconn = v;
1205
1206 /* Dequeues all of the listeners waiting for a resource */
1207 if (!LIST_ISEMPTY(&global_listener_queue))
1208 dequeue_all_listeners(&global_listener_queue);
1209
1210 return 1;
1211}
1212
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001213static int set_severity_output(int *target, char *argument)
1214{
1215 if (!strcmp(argument, "none")) {
1216 *target = CLI_SEVERITY_NONE;
1217 return 1;
1218 }
1219 else if (!strcmp(argument, "number")) {
1220 *target = CLI_SEVERITY_NUMBER;
1221 return 1;
1222 }
1223 else if (!strcmp(argument, "string")) {
1224 *target = CLI_SEVERITY_STRING;
1225 return 1;
1226 }
1227 return 0;
1228}
1229
1230/* parse a "set severity-output" command. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001231static int cli_parse_set_severity_output(char **args, char *payload, struct appctx *appctx, void *private)
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001232{
1233 if (*args[2] && set_severity_output(&appctx->cli_severity_output, args[2]))
1234 return 0;
1235
1236 appctx->ctx.cli.severity = LOG_ERR;
Aurélien Nephtali6e8a41d2018-03-15 21:48:50 +01001237 appctx->ctx.cli.msg = "one of 'none', 'number', 'string' is a required argument\n";
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001238 appctx->st0 = CLI_ST_PRINT;
1239 return 1;
1240}
William Lallemandeceddf72016-12-15 18:06:44 +01001241
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001242int cli_parse_default(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandeceddf72016-12-15 18:06:44 +01001243{
1244 return 0;
1245}
1246
Willy Tarreau45c742b2016-11-24 14:51:17 +01001247/* parse a "set rate-limit" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001248static int cli_parse_set_ratelimit(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau45c742b2016-11-24 14:51:17 +01001249{
1250 int v;
1251 int *res;
1252 int mul = 1;
1253
1254 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1255 return 1;
1256
1257 if (strcmp(args[2], "connections") == 0 && strcmp(args[3], "global") == 0)
1258 res = &global.cps_lim;
1259 else if (strcmp(args[2], "sessions") == 0 && strcmp(args[3], "global") == 0)
1260 res = &global.sps_lim;
1261#ifdef USE_OPENSSL
1262 else if (strcmp(args[2], "ssl-sessions") == 0 && strcmp(args[3], "global") == 0)
1263 res = &global.ssl_lim;
1264#endif
1265 else if (strcmp(args[2], "http-compression") == 0 && strcmp(args[3], "global") == 0) {
1266 res = &global.comp_rate_lim;
1267 mul = 1024;
1268 }
1269 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001270 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001271 appctx->ctx.cli.msg =
1272 "'set rate-limit' only supports :\n"
1273 " - 'connections global' to set the per-process maximum connection rate\n"
1274 " - 'sessions global' to set the per-process maximum session rate\n"
1275#ifdef USE_OPENSSL
Aurélien Nephtalib53e2082018-03-11 16:55:02 +01001276 " - 'ssl-sessions global' to set the per-process maximum SSL session rate\n"
Willy Tarreau45c742b2016-11-24 14:51:17 +01001277#endif
1278 " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001279 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001280 return 1;
1281 }
1282
1283 if (!*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001284 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001285 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001286 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001287 return 1;
1288 }
1289
1290 v = atoi(args[4]);
1291 if (v < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001292 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001293 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001294 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001295 return 1;
1296 }
1297
1298 *res = v * mul;
1299
1300 /* Dequeues all of the listeners waiting for a resource */
1301 if (!LIST_ISEMPTY(&global_listener_queue))
1302 dequeue_all_listeners(&global_listener_queue);
1303
1304 return 1;
1305}
1306
William Lallemandf6975e92017-05-26 17:42:10 +02001307/* parse the "expose-fd" argument on the bind lines */
1308static int bind_parse_expose_fd(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1309{
1310 if (!*args[cur_arg + 1]) {
1311 memprintf(err, "'%s' : missing fd type", args[cur_arg]);
1312 return ERR_ALERT | ERR_FATAL;
1313 }
1314 if (!strcmp(args[cur_arg+1], "listeners")) {
1315 conf->level |= ACCESS_FD_LISTENERS;
1316 } else {
1317 memprintf(err, "'%s' only supports 'listeners' (got '%s')",
1318 args[cur_arg], args[cur_arg+1]);
1319 return ERR_ALERT | ERR_FATAL;
1320 }
1321
1322 return 0;
1323}
1324
William Lallemand74c24fb2016-11-21 17:18:36 +01001325/* parse the "level" argument on the bind lines */
1326static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1327{
1328 if (!*args[cur_arg + 1]) {
1329 memprintf(err, "'%s' : missing level", args[cur_arg]);
1330 return ERR_ALERT | ERR_FATAL;
1331 }
1332
William Lallemand07a62f72017-05-24 00:57:40 +02001333 if (!strcmp(args[cur_arg+1], "user")) {
1334 conf->level &= ~ACCESS_LVL_MASK;
1335 conf->level |= ACCESS_LVL_USER;
1336 } else if (!strcmp(args[cur_arg+1], "operator")) {
1337 conf->level &= ~ACCESS_LVL_MASK;
1338 conf->level |= ACCESS_LVL_OPER;
1339 } else if (!strcmp(args[cur_arg+1], "admin")) {
1340 conf->level &= ~ACCESS_LVL_MASK;
1341 conf->level |= ACCESS_LVL_ADMIN;
1342 } else {
William Lallemand74c24fb2016-11-21 17:18:36 +01001343 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
1344 args[cur_arg], args[cur_arg+1]);
1345 return ERR_ALERT | ERR_FATAL;
1346 }
1347
1348 return 0;
1349}
1350
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001351static int bind_parse_severity_output(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1352{
1353 if (!*args[cur_arg + 1]) {
1354 memprintf(err, "'%s' : missing severity format", args[cur_arg]);
1355 return ERR_ALERT | ERR_FATAL;
1356 }
1357
1358 if (set_severity_output(&conf->severity_output, args[cur_arg+1]))
1359 return 0;
1360 else {
1361 memprintf(err, "'%s' only supports 'none', 'number', and 'string' (got '%s')",
1362 args[cur_arg], args[cur_arg+1]);
1363 return ERR_ALERT | ERR_FATAL;
1364 }
1365}
1366
Olivier Houchardf886e342017-04-05 22:24:59 +02001367/* Send all the bound sockets, always returns 1 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001368static int _getsocks(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchardf886e342017-04-05 22:24:59 +02001369{
1370 char *cmsgbuf = NULL;
1371 unsigned char *tmpbuf = NULL;
1372 struct cmsghdr *cmsg;
1373 struct stream_interface *si = appctx->owner;
William Lallemandf6975e92017-05-26 17:42:10 +02001374 struct stream *s = si_strm(si);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001375 struct connection *remote = cs_conn(objt_cs(si_opposite(si)->end));
Olivier Houchardf886e342017-04-05 22:24:59 +02001376 struct msghdr msghdr;
1377 struct iovec iov;
Olivier Houchard54740872017-04-06 14:45:14 +02001378 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf886e342017-04-05 22:24:59 +02001379 int *tmpfd;
1380 int tot_fd_nb = 0;
1381 struct proxy *px;
1382 int i = 0;
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001383 int fd = -1;
Olivier Houchardf886e342017-04-05 22:24:59 +02001384 int curoff = 0;
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001385 int old_fcntl = -1;
Olivier Houchardf886e342017-04-05 22:24:59 +02001386 int ret;
1387
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001388 if (!remote) {
1389 ha_warning("Only works on real connections\n");
1390 goto out;
1391 }
1392
1393 fd = remote->handle.fd;
1394
Olivier Houchardf886e342017-04-05 22:24:59 +02001395 /* Temporary set the FD in blocking mode, that will make our life easier */
1396 old_fcntl = fcntl(fd, F_GETFL);
1397 if (old_fcntl < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001398 ha_warning("Couldn't get the flags for the unix socket\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001399 goto out;
1400 }
1401 cmsgbuf = malloc(CMSG_SPACE(sizeof(int) * MAX_SEND_FD));
1402 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001403 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001404 goto out;
1405 }
1406 if (fcntl(fd, F_SETFL, old_fcntl &~ O_NONBLOCK) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001407 ha_warning("Cannot make the unix socket blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001408 goto out;
1409 }
Olivier Houchard54740872017-04-06 14:45:14 +02001410 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf886e342017-04-05 22:24:59 +02001411 iov.iov_base = &tot_fd_nb;
1412 iov.iov_len = sizeof(tot_fd_nb);
William Lallemandf6975e92017-05-26 17:42:10 +02001413 if (!(strm_li(s)->bind_conf->level & ACCESS_FD_LISTENERS))
Olivier Houchardf886e342017-04-05 22:24:59 +02001414 goto out;
1415 memset(&msghdr, 0, sizeof(msghdr));
1416 /*
1417 * First, calculates the total number of FD, so that we can let
1418 * the caller know how much he should expects.
1419 */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001420 px = proxies_list;
Olivier Houchardf886e342017-04-05 22:24:59 +02001421 while (px) {
1422 struct listener *l;
1423
1424 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02001425 /* Only transfer IPv4/IPv6/UNIX sockets */
1426 if (l->state >= LI_ZOMBIE &&
1427 (l->proto->sock_family == AF_INET ||
Olivier Houchardf886e342017-04-05 22:24:59 +02001428 l->proto->sock_family == AF_INET6 ||
Olivier Houchard1fc05162017-04-06 01:05:05 +02001429 l->proto->sock_family == AF_UNIX))
Olivier Houchardf886e342017-04-05 22:24:59 +02001430 tot_fd_nb++;
1431 }
1432 px = px->next;
1433 }
1434 if (tot_fd_nb == 0)
1435 goto out;
1436
1437 /* First send the total number of file descriptors, so that the
1438 * receiving end knows what to expect.
1439 */
1440 msghdr.msg_iov = &iov;
1441 msghdr.msg_iovlen = 1;
1442 ret = sendmsg(fd, &msghdr, 0);
1443 if (ret != sizeof(tot_fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001444 ha_warning("Failed to send the number of sockets to send\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001445 goto out;
1446 }
1447
1448 /* Now send the fds */
1449 msghdr.msg_control = cmsgbuf;
1450 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * MAX_SEND_FD);
1451 cmsg = CMSG_FIRSTHDR(&msghdr);
1452 cmsg->cmsg_len = CMSG_LEN(MAX_SEND_FD * sizeof(int));
1453 cmsg->cmsg_level = SOL_SOCKET;
1454 cmsg->cmsg_type = SCM_RIGHTS;
1455 tmpfd = (int *)CMSG_DATA(cmsg);
1456
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001457 px = proxies_list;
Olivier Houchardf886e342017-04-05 22:24:59 +02001458 /* For each socket, e message is sent, containing the following :
1459 * Size of the namespace name (or 0 if none), as an unsigned char.
1460 * The namespace name, if any
1461 * Size of the interface name (or 0 if none), as an unsigned char
1462 * The interface name, if any
1463 * Listener options, as an int.
1464 */
1465 /* We will send sockets MAX_SEND_FD per MAX_SEND_FD, allocate a
1466 * buffer big enough to store the socket informations.
1467 */
Olivier Houchardf143b802017-11-04 15:13:01 +01001468 tmpbuf = malloc(MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf886e342017-04-05 22:24:59 +02001469 if (tmpbuf == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001470 ha_warning("Failed to allocate memory to transfer socket informations\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001471 goto out;
1472 }
1473 iov.iov_base = tmpbuf;
1474 while (px) {
1475 struct listener *l;
1476
1477 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1478 int ret;
1479 /* Only transfer IPv4/IPv6 sockets */
Olivier Houchard1fc05162017-04-06 01:05:05 +02001480 if (l->state >= LI_ZOMBIE &&
Olivier Houchardf886e342017-04-05 22:24:59 +02001481 (l->proto->sock_family == AF_INET ||
1482 l->proto->sock_family == AF_INET6 ||
1483 l->proto->sock_family == AF_UNIX)) {
1484 memcpy(&tmpfd[i % MAX_SEND_FD], &l->fd, sizeof(l->fd));
1485 if (!l->netns)
1486 tmpbuf[curoff++] = 0;
1487#ifdef CONFIG_HAP_NS
1488 else {
1489 char *name = l->netns->node.key;
1490 unsigned char len = l->netns->name_len;
1491 tmpbuf[curoff++] = len;
1492 memcpy(tmpbuf + curoff, name, len);
1493 curoff += len;
1494 }
1495#endif
1496 if (l->interface) {
1497 unsigned char len = strlen(l->interface);
1498 tmpbuf[curoff++] = len;
1499 memcpy(tmpbuf + curoff, l->interface, len);
1500 curoff += len;
1501 } else
1502 tmpbuf[curoff++] = 0;
1503 memcpy(tmpbuf + curoff, &l->options,
1504 sizeof(l->options));
1505 curoff += sizeof(l->options);
1506
1507
1508 i++;
1509 } else
1510 continue;
1511 if ((!(i % MAX_SEND_FD))) {
1512 iov.iov_len = curoff;
1513 if (sendmsg(fd, &msghdr, 0) != curoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001514 ha_warning("Failed to transfer sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001515 goto out;
1516 }
1517 /* Wait for an ack */
1518 do {
1519 ret = recv(fd, &tot_fd_nb,
1520 sizeof(tot_fd_nb), 0);
1521 } while (ret == -1 && errno == EINTR);
1522 if (ret <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001523 ha_warning("Unexpected error while transferring sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001524 goto out;
1525 }
1526 curoff = 0;
1527 }
1528
1529 }
1530 px = px->next;
1531 }
1532 if (i % MAX_SEND_FD) {
1533 iov.iov_len = curoff;
1534 cmsg->cmsg_len = CMSG_LEN((i % MAX_SEND_FD) * sizeof(int));
1535 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * (i % MAX_SEND_FD));
1536 if (sendmsg(fd, &msghdr, 0) != curoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001537 ha_warning("Failed to transfer sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001538 goto out;
1539 }
1540 }
1541
1542out:
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001543 if (fd >= 0 && old_fcntl >= 0 && fcntl(fd, F_SETFL, old_fcntl) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001544 ha_warning("Cannot make the unix socket non-blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001545 goto out;
1546 }
1547 appctx->st0 = CLI_ST_END;
1548 free(cmsgbuf);
1549 free(tmpbuf);
1550 return 1;
1551}
1552
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001553static int cli_parse_simple(char **args, char *payload, struct appctx *appctx, void *private)
1554{
1555 if (*args[0] == 'h')
1556 /* help */
1557 cli_gen_usage_msg(appctx);
1558 else if (*args[0] == 'p')
1559 /* prompt */
1560 appctx->st1 ^= APPCTX_CLI_ST1_PROMPT;
1561 else if (*args[0] == 'q')
1562 /* quit */
1563 appctx->st0 = CLI_ST_END;
1564
1565 return 1;
1566}
Olivier Houchardf886e342017-04-05 22:24:59 +02001567
1568
William Lallemandce83b4a2018-10-26 14:47:30 +02001569/*
1570 * Create a new CLI socket using a socketpair for a worker process
1571 * <mworker_proc> is the process structure, and <proc> is the process number
1572 */
1573int mworker_cli_sockpair_new(struct mworker_proc *mworker_proc, int proc)
1574{
1575 struct bind_conf *bind_conf;
1576 struct listener *l;
1577 char *path = NULL;
1578 char *err = NULL;
1579
1580 /* master pipe to ensure the master is still alive */
1581 if (socketpair(AF_UNIX, SOCK_STREAM, 0, mworker_proc->ipc_fd) < 0) {
1582 ha_alert("Cannot create worker socketpair.\n");
1583 return -1;
1584 }
1585
1586 /* XXX: we might want to use a separate frontend at some point */
1587 if (!global.stats_fe) {
1588 if ((global.stats_fe = alloc_stats_fe("GLOBAL", "master-socket", 0)) == NULL) {
1589 ha_alert("out of memory trying to allocate the stats frontend");
1590 return -1;
1591 }
1592 }
1593
1594 bind_conf = bind_conf_alloc(global.stats_fe, "master-socket", 0, "", xprt_get(XPRT_RAW));
1595 bind_conf->level &= ~ACCESS_LVL_MASK;
1596 bind_conf->level |= ACCESS_LVL_ADMIN; /* TODO: need to lower the rights with a CLI keyword*/
1597
1598 bind_conf->bind_proc = 1UL << proc;
1599 global.stats_fe->bind_proc = 0; /* XXX: we should be careful with that, it can be removed by configuration */
1600
1601 if (!memprintf(&path, "sockpair@%d", mworker_proc->ipc_fd[1])) {
1602 ha_alert("Cannot allocate listener.\n");
1603 return -1;
1604 }
1605
1606 if (!str2listener(path, global.stats_fe, bind_conf, "master-socket", 0, &err)) {
1607 ha_alert("Cannot create a CLI sockpair listener for process #%d\n", proc);
1608 return -1;
1609 }
1610
1611 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
1612 l->maxconn = global.stats_fe->maxconn;
1613 l->backlog = global.stats_fe->backlog;
1614 l->accept = session_accept_fd;
1615 l->default_target = global.stats_fe->default_target;
1616 l->options |= LI_O_UNLIMITED;
1617 /* it's a sockpair but we don't want to keep the fd in the master */
1618 l->options &= ~LI_O_INHERITED;
1619 l->nice = -64; /* we want to boost priority for local stats */
1620 global.maxsock += l->maxconn;
1621 }
1622
1623 return 0;
1624}
1625
William Lallemand74c24fb2016-11-21 17:18:36 +01001626static struct applet cli_applet = {
1627 .obj_type = OBJ_TYPE_APPLET,
1628 .name = "<CLI>", /* used for logging */
1629 .fct = cli_io_handler,
1630 .release = cli_release_handler,
1631};
1632
Willy Tarreau0a739292016-11-22 20:21:23 +01001633/* register cli keywords */
1634static struct cli_kw_list cli_kws = {{ },{
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001635 { { "help", NULL }, NULL, cli_parse_simple, NULL },
1636 { { "prompt", NULL }, NULL, cli_parse_simple, NULL },
1637 { { "quit", NULL }, NULL, cli_parse_simple, NULL },
Willy Tarreau2af99412016-11-23 11:10:59 +01001638 { { "set", "maxconn", "global", NULL }, "set maxconn global : change the per-process maxconn setting", cli_parse_set_maxconn_global, NULL },
Willy Tarreau45c742b2016-11-24 14:51:17 +01001639 { { "set", "rate-limit", NULL }, "set rate-limit : change a rate limiting value", cli_parse_set_ratelimit, NULL },
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001640 { { "set", "severity-output", NULL }, "set severity-output [none|number|string] : set presence of severity level in feedback information", cli_parse_set_severity_output, NULL, NULL },
Willy Tarreau599852e2016-11-22 20:33:32 +01001641 { { "set", "timeout", NULL }, "set timeout : change a timeout setting", cli_parse_set_timeout, NULL, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001642 { { "show", "env", NULL }, "show env [var] : dump environment variables known to the process", cli_parse_show_env, cli_io_handler_show_env, NULL },
William Lallemandeceddf72016-12-15 18:06:44 +01001643 { { "show", "cli", "sockets", NULL }, "show cli sockets : dump list of cli sockets", cli_parse_default, cli_io_handler_show_cli_sock, NULL },
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001644 { { "show", "fd", NULL }, "show fd [num] : dump list of file descriptors in use", cli_parse_show_fd, cli_io_handler_show_fd, NULL },
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001645 { { "show", "activity", NULL }, "show activity : show per-thread activity stats (for support/developers)", cli_parse_default, cli_io_handler_show_activity, NULL },
Olivier Houchardf886e342017-04-05 22:24:59 +02001646 { { "_getsocks", NULL }, NULL, _getsocks, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001647 {{},}
1648}};
1649
William Lallemand74c24fb2016-11-21 17:18:36 +01001650static struct cfg_kw_list cfg_kws = {ILH, {
1651 { CFG_GLOBAL, "stats", stats_parse_global },
1652 { 0, NULL, NULL },
1653}};
1654
1655static struct bind_kw_list bind_kws = { "STAT", { }, {
William Lallemandf6975e92017-05-26 17:42:10 +02001656 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
1657 { "expose-fd", bind_parse_expose_fd, 1 }, /* set the unix socket expose fd rights */
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001658 { "severity-output", bind_parse_severity_output, 1 }, /* set the severity output format */
William Lallemand74c24fb2016-11-21 17:18:36 +01001659 { NULL, NULL, 0 },
1660}};
1661
1662__attribute__((constructor))
1663static void __dumpstats_module_init(void)
1664{
1665 cfg_register_keywords(&cfg_kws);
Willy Tarreau0a739292016-11-22 20:21:23 +01001666 cli_register_kw(&cli_kws);
William Lallemand74c24fb2016-11-21 17:18:36 +01001667 bind_register_keywords(&bind_kws);
1668}
1669
1670/*
1671 * Local variables:
1672 * c-indent-level: 8
1673 * c-basic-offset: 8
1674 * End:
1675 */