blob: 266bade8af475ec4551842e531fc256555039139 [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>
59#include <proto/listener.h>
60#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010061#include <proto/proto_uxst.h>
62#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
William Lallemand74c24fb2016-11-21 17:18:36 +010071static struct applet cli_applet;
72
73static const char stats_sock_usage_msg[] =
74 "Unknown command. Please enter one of the following commands only :\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010075 " help : this message\n"
76 " prompt : toggle interactive mode with prompt\n"
77 " quit : disconnect\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010078 "";
79
80static const char stats_permission_denied_msg[] =
81 "Permission denied\n"
82 "";
83
84
85static char *dynamic_usage_msg = NULL;
86
87/* List head of cli keywords */
88static struct cli_kw_list cli_keywords = {
89 .list = LIST_HEAD_INIT(cli_keywords.list)
90};
91
92extern const char *stat_status_codes[];
93
94char *cli_gen_usage_msg()
95{
96 struct cli_kw_list *kw_list;
97 struct cli_kw *kw;
98 struct chunk *tmp = get_trash_chunk();
99 struct chunk out;
100
101 free(dynamic_usage_msg);
102 dynamic_usage_msg = NULL;
103
104 if (LIST_ISEMPTY(&cli_keywords.list))
105 return NULL;
106
107 chunk_reset(tmp);
108 chunk_strcat(tmp, stats_sock_usage_msg);
109 list_for_each_entry(kw_list, &cli_keywords.list, list) {
110 kw = &kw_list->kw[0];
111 while (kw->usage) {
112 chunk_appendf(tmp, " %s\n", kw->usage);
113 kw++;
114 }
115 }
116 chunk_init(&out, NULL, 0);
117 chunk_dup(&out, tmp);
118 dynamic_usage_msg = out.str;
119 return dynamic_usage_msg;
120}
121
122struct cli_kw* cli_find_kw(char **args)
123{
124 struct cli_kw_list *kw_list;
125 struct cli_kw *kw;/* current cli_kw */
126 char **tmp_args;
127 const char **tmp_str_kw;
128 int found = 0;
129
130 if (LIST_ISEMPTY(&cli_keywords.list))
131 return NULL;
132
133 list_for_each_entry(kw_list, &cli_keywords.list, list) {
134 kw = &kw_list->kw[0];
135 while (*kw->str_kw) {
136 tmp_args = args;
137 tmp_str_kw = kw->str_kw;
138 while (*tmp_str_kw) {
139 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
140 found = 1;
141 } else {
142 found = 0;
143 break;
144 }
145 tmp_args++;
146 tmp_str_kw++;
147 }
148 if (found)
149 return (kw);
150 kw++;
151 }
152 }
153 return NULL;
154}
155
156void cli_register_kw(struct cli_kw_list *kw_list)
157{
158 LIST_ADDQ(&cli_keywords.list, &kw_list->list);
159}
160
161
162/* allocate a new stats frontend named <name>, and return it
163 * (or NULL in case of lack of memory).
164 */
165static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
166{
167 struct proxy *fe;
168
169 fe = calloc(1, sizeof(*fe));
170 if (!fe)
171 return NULL;
172
173 init_new_proxy(fe);
174 fe->next = proxy;
175 proxy = fe;
176 fe->last_change = now.tv_sec;
177 fe->id = strdup("GLOBAL");
178 fe->cap = PR_CAP_FE;
179 fe->maxconn = 10; /* default to 10 concurrent connections */
180 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
181 fe->conf.file = strdup(file);
182 fe->conf.line = line;
183 fe->accept = frontend_accept;
184 fe->default_target = &cli_applet.obj_type;
185
186 /* the stats frontend is the only one able to assign ID #0 */
187 fe->conf.id.key = fe->uuid = 0;
188 eb32_insert(&used_proxy_id, &fe->conf.id);
189 return fe;
190}
191
192/* This function parses a "stats" statement in the "global" section. It returns
193 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
194 * error message into the <err> buffer which will be preallocated. The trailing
195 * '\n' must not be written. The function must be called with <args> pointing to
196 * the first word after "stats".
197 */
198static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
199 struct proxy *defpx, const char *file, int line,
200 char **err)
201{
202 struct bind_conf *bind_conf;
203 struct listener *l;
204
205 if (!strcmp(args[1], "socket")) {
206 int cur_arg;
207
208 if (*args[2] == 0) {
209 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
210 return -1;
211 }
212
213 if (!global.stats_fe) {
214 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
215 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
216 return -1;
217 }
218 }
219
Willy Tarreaua261e9b2016-12-22 20:44:00 +0100220 bind_conf = bind_conf_alloc(global.stats_fe, file, line, args[2], xprt_get(XPRT_RAW));
William Lallemand07a62f72017-05-24 00:57:40 +0200221 bind_conf->level &= ~ACCESS_LVL_MASK;
222 bind_conf->level |= ACCESS_LVL_OPER; /* default access level */
William Lallemand74c24fb2016-11-21 17:18:36 +0100223
224 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
225 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
226 file, line, args[0], args[1], err && *err ? *err : "error");
227 return -1;
228 }
229
230 cur_arg = 3;
231 while (*args[cur_arg]) {
232 static int bind_dumped;
233 struct bind_kw *kw;
234
235 kw = bind_find_kw(args[cur_arg]);
236 if (kw) {
237 if (!kw->parse) {
238 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
239 args[0], args[1], args[cur_arg]);
240 return -1;
241 }
242
243 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
244 if (err && *err)
245 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
246 else
247 memprintf(err, "'%s %s' : error encountered while processing '%s'",
248 args[0], args[1], args[cur_arg]);
249 return -1;
250 }
251
252 cur_arg += 1 + kw->skip;
253 continue;
254 }
255
256 if (!bind_dumped) {
257 bind_dump_kws(err);
258 indent_msg(err, 4);
259 bind_dumped = 1;
260 }
261
262 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
263 args[0], args[1], args[cur_arg],
264 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
265 return -1;
266 }
267
268 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
269 l->maxconn = global.stats_fe->maxconn;
270 l->backlog = global.stats_fe->backlog;
271 l->accept = session_accept_fd;
William Lallemand74c24fb2016-11-21 17:18:36 +0100272 l->default_target = global.stats_fe->default_target;
273 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
274 l->nice = -64; /* we want to boost priority for local stats */
275 global.maxsock += l->maxconn;
276 }
277 }
278 else if (!strcmp(args[1], "timeout")) {
279 unsigned timeout;
280 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
281
282 if (res) {
283 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
284 return -1;
285 }
286
287 if (!timeout) {
288 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
289 return -1;
290 }
291 if (!global.stats_fe) {
292 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
293 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
294 return -1;
295 }
296 }
297 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
298 }
299 else if (!strcmp(args[1], "maxconn")) {
300 int maxconn = atol(args[2]);
301
302 if (maxconn <= 0) {
303 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
304 return -1;
305 }
306
307 if (!global.stats_fe) {
308 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
309 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
310 return -1;
311 }
312 }
313 global.stats_fe->maxconn = maxconn;
314 }
315 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
316 int cur_arg = 2;
317 unsigned long set = 0;
318
319 if (!global.stats_fe) {
320 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
321 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
322 return -1;
323 }
324 }
325
326 while (*args[cur_arg]) {
327 unsigned int low, high;
328
329 if (strcmp(args[cur_arg], "all") == 0) {
330 set = 0;
331 break;
332 }
333 else if (strcmp(args[cur_arg], "odd") == 0) {
334 set |= ~0UL/3UL; /* 0x555....555 */
335 }
336 else if (strcmp(args[cur_arg], "even") == 0) {
337 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
338 }
339 else if (isdigit((int)*args[cur_arg])) {
340 char *dash = strchr(args[cur_arg], '-');
341
342 low = high = str2uic(args[cur_arg]);
343 if (dash)
344 high = str2uic(dash + 1);
345
346 if (high < low) {
347 unsigned int swap = low;
348 low = high;
349 high = swap;
350 }
351
352 if (low < 1 || high > LONGBITS) {
353 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
354 args[0], args[1], LONGBITS);
355 return -1;
356 }
357 while (low <= high)
358 set |= 1UL << (low++ - 1);
359 }
360 else {
361 memprintf(err,
362 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
363 args[0], args[1], LONGBITS);
364 return -1;
365 }
366 cur_arg++;
367 }
368 global.stats_fe->bind_proc = set;
369 }
370 else {
371 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
372 return -1;
373 }
374 return 0;
375}
376
Willy Tarreaude57a572016-11-23 17:01:39 +0100377/* Verifies that the CLI at least has a level at least as high as <level>
378 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
379 * failure, an error message is prepared and the appctx's state is adjusted
380 * to print it so that a return 1 is enough to abort any processing.
381 */
382int cli_has_level(struct appctx *appctx, int level)
383{
384 struct stream_interface *si = appctx->owner;
385 struct stream *s = si_strm(si);
386
William Lallemand07a62f72017-05-24 00:57:40 +0200387 if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < level) {
Willy Tarreaude57a572016-11-23 17:01:39 +0100388 appctx->ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100389 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaude57a572016-11-23 17:01:39 +0100390 return 0;
391 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100392 return 1;
393}
394
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200395/* Returns severity_output for the current session if set, or default for the socket */
396static int cli_get_severity_output(struct appctx *appctx)
397{
398 if (appctx->cli_severity_output)
399 return appctx->cli_severity_output;
400 return strm_li(si_strm(appctx->owner))->bind_conf->severity_output;
401}
William Lallemand74c24fb2016-11-21 17:18:36 +0100402
Willy Tarreau41908562016-11-24 16:23:38 +0100403/* Processes the CLI interpreter on the stats socket. This function is called
404 * from the CLI's IO handler running in an appctx context. The function returns 1
405 * if the request was understood, otherwise zero. It is called with appctx->st0
406 * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do
407 * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to
408 * have its own I/O handler called again. Most of the time, parsers will only
409 * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg.
Willy Tarreaueaffde32016-12-16 17:59:25 +0100410 * If a keyword parser is NULL and an I/O handler is declared, the I/O handler
411 * will automatically be used.
William Lallemand74c24fb2016-11-21 17:18:36 +0100412 */
Willy Tarreau41908562016-11-24 16:23:38 +0100413static int cli_parse_request(struct appctx *appctx, char *line)
William Lallemand74c24fb2016-11-21 17:18:36 +0100414{
William Lallemand74c24fb2016-11-21 17:18:36 +0100415 char *args[MAX_STATS_ARGS + 1];
416 struct cli_kw *kw;
417 int arg;
418 int i, j;
419
420 while (isspace((unsigned char)*line))
421 line++;
422
423 arg = 0;
424 args[arg] = line;
425
426 while (*line && arg < MAX_STATS_ARGS) {
427 if (*line == '\\') {
428 line++;
429 if (*line == '\0')
430 break;
431 }
432 else if (isspace((unsigned char)*line)) {
433 *line++ = '\0';
434
435 while (isspace((unsigned char)*line))
436 line++;
437
438 args[++arg] = line;
439 continue;
440 }
441
442 line++;
443 }
444
445 while (++arg <= MAX_STATS_ARGS)
446 args[arg] = line;
447
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100448 /* unescape '\' */
William Lallemand74c24fb2016-11-21 17:18:36 +0100449 arg = 0;
450 while (*args[arg] != '\0') {
451 j = 0;
452 for (i=0; args[arg][i] != '\0'; i++) {
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100453 if (args[arg][i] == '\\') {
454 if (args[arg][i+1] == '\\')
455 i++;
456 else
457 continue;
458 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100459 args[arg][j] = args[arg][i];
460 j++;
461 }
462 args[arg][j] = '\0';
463 arg++;
464 }
465
Willy Tarreau41908562016-11-24 16:23:38 +0100466 appctx->st2 = 0;
Willy Tarreaua2d58722016-12-16 12:37:03 +0100467 memset(&appctx->ctx.cli, 0, sizeof(appctx->ctx.cli));
Willy Tarreau41908562016-11-24 16:23:38 +0100468
469 kw = cli_find_kw(args);
Willy Tarreaueaffde32016-12-16 17:59:25 +0100470 if (!kw)
Willy Tarreau41908562016-11-24 16:23:38 +0100471 return 0;
472
473 appctx->io_handler = kw->io_handler;
Emeric Brund6871f72017-06-29 19:54:13 +0200474 appctx->io_release = kw->io_release;
475 /* kw->parse could set its own io_handler or ip_release handler */
Willy Tarreaueaffde32016-12-16 17:59:25 +0100476 if ((!kw->parse || kw->parse(args, appctx, kw->private) == 0) && appctx->io_handler) {
Willy Tarreau41908562016-11-24 16:23:38 +0100477 appctx->st0 = CLI_ST_CALLBACK;
William Lallemand74c24fb2016-11-21 17:18:36 +0100478 }
Willy Tarreau41908562016-11-24 16:23:38 +0100479 return 1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100480}
481
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200482/* prepends then outputs the argument msg with a syslog-type severity depending on severity_output value */
483static int cli_output_msg(struct channel *chn, const char *msg, int severity, int severity_output)
484{
485 struct chunk *tmp;
486
487 if (likely(severity_output == CLI_SEVERITY_NONE))
488 return bi_putblk(chn, msg, strlen(msg));
489
490 tmp = get_trash_chunk();
491 chunk_reset(tmp);
492
493 if (severity < 0 || severity > 7) {
494 Warning("socket command feedback with invalid severity %d", severity);
495 chunk_printf(tmp, "[%d]: ", severity);
496 }
497 else {
498 switch (severity_output) {
499 case CLI_SEVERITY_NUMBER:
500 chunk_printf(tmp, "[%d]: ", severity);
501 break;
502 case CLI_SEVERITY_STRING:
503 chunk_printf(tmp, "[%s]: ", log_levels[severity]);
504 break;
505 default:
506 Warning("Unrecognized severity output %d", severity_output);
507 }
508 }
509 chunk_appendf(tmp, "%s", msg);
510
511 return bi_putblk(chn, tmp->str, strlen(tmp->str));
512}
513
William Lallemand74c24fb2016-11-21 17:18:36 +0100514/* This I/O handler runs as an applet embedded in a stream interface. It is
515 * used to processes I/O from/to the stats unix socket. The system relies on a
516 * state machine handling requests and various responses. We read a request,
517 * then we process it and send the response, and we possibly display a prompt.
518 * Then we can read again. The state is stored in appctx->st0 and is one of the
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100519 * CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled
William Lallemand74c24fb2016-11-21 17:18:36 +0100520 * or not.
521 */
522static void cli_io_handler(struct appctx *appctx)
523{
524 struct stream_interface *si = appctx->owner;
525 struct channel *req = si_oc(si);
526 struct channel *res = si_ic(si);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200527 struct bind_conf *bind_conf = strm_li(si_strm(si))->bind_conf;
William Lallemand74c24fb2016-11-21 17:18:36 +0100528 int reql;
529 int len;
530
531 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
532 goto out;
533
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100534 /* Check if the input buffer is avalaible. */
535 if (res->buf->size == 0) {
536 si_applet_cant_put(si);
537 goto out;
538 }
539
William Lallemand74c24fb2016-11-21 17:18:36 +0100540 while (1) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100541 if (appctx->st0 == CLI_ST_INIT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100542 /* Stats output not initialized yet */
543 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200544 /* reset severity to default at init */
545 appctx->cli_severity_output = bind_conf->severity_output;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100546 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100547 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100548 else if (appctx->st0 == CLI_ST_END) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100549 /* Let's close for real now. We just close the request
550 * side, the conditions below will complete if needed.
551 */
552 si_shutw(si);
553 break;
554 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100555 else if (appctx->st0 == CLI_ST_GETREQ) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100556 /* ensure we have some output room left in the event we
557 * would want to return some info right after parsing.
558 */
559 if (buffer_almost_full(si_ib(si))) {
560 si_applet_cant_put(si);
561 break;
562 }
563
564 reql = bo_getline(si_oc(si), trash.str, trash.size);
565 if (reql <= 0) { /* closed or EOL not found */
566 if (reql == 0)
567 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100568 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100569 continue;
570 }
571
572 /* seek for a possible unescaped semi-colon. If we find
573 * one, we replace it with an LF and skip only this part.
574 */
575 for (len = 0; len < reql; len++) {
576 if (trash.str[len] == '\\') {
577 len++;
578 continue;
579 }
580 if (trash.str[len] == ';') {
581 trash.str[len] = '\n';
582 reql = len + 1;
583 break;
584 }
585 }
586
587 /* now it is time to check that we have a full line,
588 * remove the trailing \n and possibly \r, then cut the
589 * line.
590 */
591 len = reql - 1;
592 if (trash.str[len] != '\n') {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100593 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100594 continue;
595 }
596
597 if (len && trash.str[len-1] == '\r')
598 len--;
599
600 trash.str[len] = '\0';
601
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100602 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100603 if (len) {
604 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100605 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100606 continue;
607 }
608 else if (strcmp(trash.str, "prompt") == 0)
609 appctx->st1 = !appctx->st1;
610 else if (strcmp(trash.str, "help") == 0 ||
Willy Tarreau41908562016-11-24 16:23:38 +0100611 !cli_parse_request(appctx, trash.str)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100612 cli_gen_usage_msg();
613 if (dynamic_usage_msg)
614 appctx->ctx.cli.msg = dynamic_usage_msg;
615 else
616 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100617 appctx->st0 = CLI_ST_PRINT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100618 }
619 /* NB: stats_sock_parse_request() may have put
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100620 * another CLI_ST_O_* into appctx->st0.
William Lallemand74c24fb2016-11-21 17:18:36 +0100621 */
622 }
623 else if (!appctx->st1) {
624 /* if prompt is disabled, print help on empty lines,
625 * so that the user at least knows how to enable
626 * prompt and find help.
627 */
628 cli_gen_usage_msg();
629 if (dynamic_usage_msg)
630 appctx->ctx.cli.msg = dynamic_usage_msg;
631 else
632 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100633 appctx->st0 = CLI_ST_PRINT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100634 }
635
636 /* re-adjust req buffer */
637 bo_skip(si_oc(si), reql);
638 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
639 }
640 else { /* output functions */
641 switch (appctx->st0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100642 case CLI_ST_PROMPT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100643 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100644 case CLI_ST_PRINT:
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200645 if (cli_output_msg(res, appctx->ctx.cli.msg, appctx->ctx.cli.severity,
646 cli_get_severity_output(appctx)) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100647 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100648 else
649 si_applet_cant_put(si);
650 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100651 case CLI_ST_PRINT_FREE:
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200652 if (cli_output_msg(res, appctx->ctx.cli.err, LOG_ERR, cli_get_severity_output(appctx)) != -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100653 free(appctx->ctx.cli.err);
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100654 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100655 }
656 else
657 si_applet_cant_put(si);
658 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100659 case CLI_ST_CALLBACK: /* use custom pointer */
William Lallemand74c24fb2016-11-21 17:18:36 +0100660 if (appctx->io_handler)
661 if (appctx->io_handler(appctx)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100662 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100663 if (appctx->io_release) {
664 appctx->io_release(appctx);
665 appctx->io_release = NULL;
666 }
667 }
668 break;
669 default: /* abnormal state */
670 si->flags |= SI_FL_ERR;
671 break;
672 }
673
674 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100675 if (appctx->st0 == CLI_ST_PROMPT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100676 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100677 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100678 else
679 si_applet_cant_put(si);
680 }
681
682 /* If the output functions are still there, it means they require more room. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100683 if (appctx->st0 >= CLI_ST_OUTPUT)
William Lallemand74c24fb2016-11-21 17:18:36 +0100684 break;
685
686 /* Now we close the output if one of the writers did so,
687 * or if we're not in interactive mode and the request
688 * buffer is empty. This still allows pipelined requests
689 * to be sent in non-interactive mode.
690 */
691 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100692 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100693 continue;
694 }
695
696 /* switch state back to GETREQ to read next requests */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100697 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100698 }
699 }
700
701 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
702 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
703 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
704 /* Other side has closed, let's abort if we have no more processing to do
705 * and nothing more to consume. This is comparable to a broken pipe, so
706 * we forward the close to the request side so that it flows upstream to
707 * the client.
708 */
709 si_shutw(si);
710 }
711
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100712 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < CLI_ST_OUTPUT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100713 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
714 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
715 /* We have no more processing to do, and nothing more to send, and
716 * the client side has closed. So we'll forward this state downstream
717 * on the response buffer.
718 */
719 si_shutr(si);
720 res->flags |= CF_READ_NULL;
721 }
722
723 out:
724 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
725 __FUNCTION__, __LINE__,
726 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
727}
728
William Lallemand74c24fb2016-11-21 17:18:36 +0100729/* This is called when the stream interface is closed. For instance, upon an
730 * external abort, we won't call the i/o handler anymore so we may need to
731 * remove back references to the stream currently being dumped.
732 */
733static void cli_release_handler(struct appctx *appctx)
734{
735 if (appctx->io_release) {
736 appctx->io_release(appctx);
737 appctx->io_release = NULL;
738 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100739 else if (appctx->st0 == CLI_ST_PRINT_FREE) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100740 free(appctx->ctx.cli.err);
741 appctx->ctx.cli.err = NULL;
742 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100743}
744
745/* This function dumps all environmnent variables to the buffer. It returns 0
746 * if the output buffer is full and it needs to be called again, otherwise
Willy Tarreauf6710f82016-12-16 17:45:44 +0100747 * non-zero. Dumps only one entry if st2 == STAT_ST_END. It uses cli.p0 as the
748 * pointer to the current variable.
William Lallemand74c24fb2016-11-21 17:18:36 +0100749 */
Willy Tarreau0a739292016-11-22 20:21:23 +0100750static int cli_io_handler_show_env(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100751{
Willy Tarreau0a739292016-11-22 20:21:23 +0100752 struct stream_interface *si = appctx->owner;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100753 char **var = appctx->ctx.cli.p0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100754
755 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
756 return 1;
757
758 chunk_reset(&trash);
759
760 /* we have two inner loops here, one for the proxy, the other one for
761 * the buffer.
762 */
Willy Tarreauf6710f82016-12-16 17:45:44 +0100763 while (*var) {
764 chunk_printf(&trash, "%s\n", *var);
William Lallemand74c24fb2016-11-21 17:18:36 +0100765
766 if (bi_putchk(si_ic(si), &trash) == -1) {
767 si_applet_cant_put(si);
768 return 0;
769 }
770 if (appctx->st2 == STAT_ST_END)
771 break;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100772 var++;
773 appctx->ctx.cli.p0 = var;
William Lallemand74c24fb2016-11-21 17:18:36 +0100774 }
775
776 /* dump complete */
777 return 1;
778}
779
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200780/* This function dumps all file descriptors states (or the requested one) to
781 * the buffer. It returns 0 if the output buffer is full and it needs to be
782 * called again, otherwise non-zero. Dumps only one entry if st2 == STAT_ST_END.
783 * It uses cli.i0 as the fd number to restart from.
784 */
785static int cli_io_handler_show_fd(struct appctx *appctx)
786{
787 struct stream_interface *si = appctx->owner;
788 int fd = appctx->ctx.cli.i0;
789
790 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
791 return 1;
792
793 chunk_reset(&trash);
794
795 /* we have two inner loops here, one for the proxy, the other one for
796 * the buffer.
797 */
798 while (fd < maxfd) {
799 struct fdtab fdt;
Willy Tarreau286ec682017-08-09 16:35:44 +0200800 struct listener *li = NULL;
801 struct server *sv = NULL;
802 struct proxy *px = NULL;
803 uint32_t conn_flags = 0;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200804
805 fdt = fdtab[fd];
806
807 if (fdt.iocb == conn_fd_handler) {
808 conn_flags = ((struct connection *)fdt.owner)->flags;
809 li = objt_listener(((struct connection *)fdt.owner)->target);
810 sv = objt_server(((struct connection *)fdt.owner)->target);
811 px = objt_proxy(((struct connection *)fdt.owner)->target);
812 }
813 else if (fdt.iocb == listener_accept)
814 li = fdt.owner;
815
816 if (!fdt.owner)
817 goto skip; // closed
818
819 chunk_printf(&trash,
820 " %5d : st=0x%02x(R:%c%c%c W:%c%c%c) ev=0x%02x(%c%c%c%c%c) [%c%c%c%c] cache=%u owner=%p iocb=%p(%s)",
821 fd,
822 fdt.state,
823 (fdt.state & FD_EV_POLLED_R) ? 'P' : 'p',
824 (fdt.state & FD_EV_READY_R) ? 'R' : 'r',
825 (fdt.state & FD_EV_ACTIVE_R) ? 'A' : 'a',
826 (fdt.state & FD_EV_POLLED_W) ? 'P' : 'p',
827 (fdt.state & FD_EV_READY_W) ? 'R' : 'r',
828 (fdt.state & FD_EV_ACTIVE_W) ? 'A' : 'a',
829 fdt.ev,
830 (fdt.ev & FD_POLL_HUP) ? 'H' : 'h',
831 (fdt.ev & FD_POLL_ERR) ? 'E' : 'e',
832 (fdt.ev & FD_POLL_OUT) ? 'O' : 'o',
833 (fdt.ev & FD_POLL_PRI) ? 'P' : 'p',
834 (fdt.ev & FD_POLL_IN) ? 'I' : 'i',
835 fdt.new ? 'N' : 'n',
836 fdt.updated ? 'U' : 'u',
837 fdt.linger_risk ? 'L' : 'l',
838 fdt.cloned ? 'C' : 'c',
839 fdt.cache,
840 fdt.owner,
841 fdt.iocb,
842 (fdt.iocb == conn_fd_handler) ? "conn_fd_handler" :
843 (fdt.iocb == dgram_fd_handler) ? "dgram_fd_handler" :
844 (fdt.iocb == listener_accept) ? "listener_accept" :
845 "unknown");
846
847 if (fdt.iocb == conn_fd_handler) {
848 chunk_appendf(&trash, " cflg=0x%08x", conn_flags);
849 if (px)
850 chunk_appendf(&trash, " px=%s", px->id);
851 else if (sv)
852 chunk_appendf(&trash, " sv=%s/%s", sv->id, sv->proxy->id);
853 else if (li)
854 chunk_appendf(&trash, " fe=%s", li->bind_conf->frontend->id);
855 }
856 else if (fdt.iocb == listener_accept) {
857 chunk_appendf(&trash, " l.st=%s fe=%s",
858 listener_state_str(li),
859 li->bind_conf->frontend->id);
860 }
861
862 chunk_appendf(&trash, "\n");
863
864 if (bi_putchk(si_ic(si), &trash) == -1) {
865 si_applet_cant_put(si);
866 return 0;
867 }
868 skip:
869 if (appctx->st2 == STAT_ST_END)
870 break;
871
872 fd++;
873 appctx->ctx.cli.i0 = fd;
874 }
875
876 /* dump complete */
877 return 1;
878}
879
William Lallemandeceddf72016-12-15 18:06:44 +0100880/*
Willy Tarreau3af9d832016-12-16 12:58:09 +0100881 * CLI IO handler for `show cli sockets`.
882 * Uses ctx.cli.p0 to store the restart pointer.
William Lallemandeceddf72016-12-15 18:06:44 +0100883 */
884static int cli_io_handler_show_cli_sock(struct appctx *appctx)
885{
886 struct bind_conf *bind_conf;
887 struct stream_interface *si = appctx->owner;
888
889 chunk_reset(&trash);
890
891 switch (appctx->st2) {
892 case STAT_ST_INIT:
893 chunk_printf(&trash, "# socket lvl processes\n");
894 if (bi_putchk(si_ic(si), &trash) == -1) {
895 si_applet_cant_put(si);
896 return 0;
897 }
William Lallemandeceddf72016-12-15 18:06:44 +0100898 appctx->st2 = STAT_ST_LIST;
899
900 case STAT_ST_LIST:
901 if (global.stats_fe) {
902 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
903 struct listener *l;
904
905 /*
Willy Tarreau3af9d832016-12-16 12:58:09 +0100906 * get the latest dumped node in appctx->ctx.cli.p0
William Lallemandeceddf72016-12-15 18:06:44 +0100907 * if the current node is the first of the list
908 */
909
Willy Tarreau3af9d832016-12-16 12:58:09 +0100910 if (appctx->ctx.cli.p0 &&
911 &bind_conf->by_fe == (&global.stats_fe->conf.bind)->n) {
William Lallemandeceddf72016-12-15 18:06:44 +0100912 /* change the current node to the latest dumped and continue the loop */
Willy Tarreau3af9d832016-12-16 12:58:09 +0100913 bind_conf = LIST_ELEM(appctx->ctx.cli.p0, typeof(bind_conf), by_fe);
William Lallemandeceddf72016-12-15 18:06:44 +0100914 continue;
915 }
916
917 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
918
919 char addr[46];
920 char port[6];
921
922 if (l->addr.ss_family == AF_UNIX) {
923 const struct sockaddr_un *un;
924
925 un = (struct sockaddr_un *)&l->addr;
926 chunk_appendf(&trash, "%s ", un->sun_path);
927 } else if (l->addr.ss_family == AF_INET) {
928 addr_to_str(&l->addr, addr, sizeof(addr));
929 port_to_str(&l->addr, port, sizeof(port));
930 chunk_appendf(&trash, "%s:%s ", addr, port);
931 } else if (l->addr.ss_family == AF_INET6) {
932 addr_to_str(&l->addr, addr, sizeof(addr));
933 port_to_str(&l->addr, port, sizeof(port));
934 chunk_appendf(&trash, "[%s]:%s ", addr, port);
935 } else
936 continue;
937
William Lallemand07a62f72017-05-24 00:57:40 +0200938 if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN)
William Lallemandeceddf72016-12-15 18:06:44 +0100939 chunk_appendf(&trash, "admin ");
William Lallemand07a62f72017-05-24 00:57:40 +0200940 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER)
941 chunk_appendf(&trash, "operator ");
942 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_USER)
943 chunk_appendf(&trash, "user ");
William Lallemandeceddf72016-12-15 18:06:44 +0100944 else
945 chunk_appendf(&trash, " ");
946
947 if (bind_conf->bind_proc != 0) {
948 int pos;
Willy Tarreau20c5e522016-12-16 12:50:55 +0100949 for (pos = 0; pos < 8 * sizeof(bind_conf->bind_proc); pos++) {
Willy Tarreau4305ac72016-12-16 12:56:31 +0100950 if (bind_conf->bind_proc & (1UL << pos)) {
William Lallemandeceddf72016-12-15 18:06:44 +0100951 chunk_appendf(&trash, "%d,", pos+1);
952 }
953 }
954 /* replace the latest comma by a newline */
955 trash.str[trash.len-1] = '\n';
956
957 } else {
958 chunk_appendf(&trash, "all\n");
959 }
960
961 if (bi_putchk(si_ic(si), &trash) == -1) {
962 si_applet_cant_put(si);
963 return 0;
964 }
965 }
Willy Tarreau3af9d832016-12-16 12:58:09 +0100966 appctx->ctx.cli.p0 = &bind_conf->by_fe; /* store the latest list node dumped */
William Lallemandeceddf72016-12-15 18:06:44 +0100967 }
968 }
969 default:
970 appctx->st2 = STAT_ST_FIN;
971 return 1;
972 }
973}
974
975
Willy Tarreau0a739292016-11-22 20:21:23 +0100976/* parse a "show env" CLI request. Returns 0 if it needs to continue, 1 if it
Willy Tarreauf6710f82016-12-16 17:45:44 +0100977 * wants to stop here. It puts the variable to be dumped into cli.p0 if a single
978 * variable is requested otherwise puts environ there.
Willy Tarreau0a739292016-11-22 20:21:23 +0100979 */
980static int cli_parse_show_env(char **args, struct appctx *appctx, void *private)
981{
982 extern char **environ;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100983 char **var;
Willy Tarreau0a739292016-11-22 20:21:23 +0100984
985 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
986 return 1;
987
Willy Tarreauf6710f82016-12-16 17:45:44 +0100988 var = environ;
Willy Tarreau0a739292016-11-22 20:21:23 +0100989
990 if (*args[2]) {
991 int len = strlen(args[2]);
992
Willy Tarreauf6710f82016-12-16 17:45:44 +0100993 for (; *var; var++) {
994 if (strncmp(*var, args[2], len) == 0 &&
995 (*var)[len] == '=')
Willy Tarreau0a739292016-11-22 20:21:23 +0100996 break;
997 }
Willy Tarreauf6710f82016-12-16 17:45:44 +0100998 if (!*var) {
Willy Tarreau0a739292016-11-22 20:21:23 +0100999 appctx->ctx.cli.msg = "Variable not found\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001000 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau0a739292016-11-22 20:21:23 +01001001 return 1;
1002 }
1003 appctx->st2 = STAT_ST_END;
1004 }
Willy Tarreauf6710f82016-12-16 17:45:44 +01001005 appctx->ctx.cli.p0 = var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001006 return 0;
1007}
1008
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001009/* parse a "show fd" CLI request. Returns 0 if it needs to continue, 1 if it
1010 * wants to stop here. It puts the FD number into cli.i0 if a specific FD is
1011 * requested and sets st2 to STAT_ST_END, otherwise leaves 0 in i0.
1012 */
1013static int cli_parse_show_fd(char **args, struct appctx *appctx, void *private)
1014{
1015 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1016 return 1;
1017
1018 appctx->ctx.cli.i0 = 0;
1019
1020 if (*args[2]) {
1021 appctx->ctx.cli.i0 = atoi(args[2]);
1022 appctx->st2 = STAT_ST_END;
1023 }
1024 return 0;
1025}
1026
Willy Tarreau599852e2016-11-22 20:33:32 +01001027/* parse a "set timeout" CLI request. It always returns 1. */
1028static int cli_parse_set_timeout(char **args, struct appctx *appctx, void *private)
1029{
1030 struct stream_interface *si = appctx->owner;
1031 struct stream *s = si_strm(si);
1032
1033 if (strcmp(args[2], "cli") == 0) {
1034 unsigned timeout;
1035 const char *res;
1036
1037 if (!*args[3]) {
1038 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001039 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001040 return 1;
1041 }
1042
1043 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1044 if (res || timeout < 1) {
1045 appctx->ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001046 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001047 return 1;
1048 }
1049
1050 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
1051 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
1052 return 1;
1053 }
1054 else {
1055 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001056 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001057 return 1;
1058 }
1059}
1060
Willy Tarreau2af99412016-11-23 11:10:59 +01001061/* parse a "set maxconn global" command. It always returns 1. */
1062static int cli_parse_set_maxconn_global(char **args, struct appctx *appctx, void *private)
1063{
1064 int v;
1065
1066 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1067 return 1;
1068
1069 if (!*args[3]) {
1070 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001071 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +01001072 return 1;
1073 }
1074
1075 v = atoi(args[3]);
1076 if (v > global.hardmaxconn) {
1077 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001078 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +01001079 return 1;
1080 }
1081
1082 /* check for unlimited values */
1083 if (v <= 0)
1084 v = global.hardmaxconn;
1085
1086 global.maxconn = v;
1087
1088 /* Dequeues all of the listeners waiting for a resource */
1089 if (!LIST_ISEMPTY(&global_listener_queue))
1090 dequeue_all_listeners(&global_listener_queue);
1091
1092 return 1;
1093}
1094
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001095static int set_severity_output(int *target, char *argument)
1096{
1097 if (!strcmp(argument, "none")) {
1098 *target = CLI_SEVERITY_NONE;
1099 return 1;
1100 }
1101 else if (!strcmp(argument, "number")) {
1102 *target = CLI_SEVERITY_NUMBER;
1103 return 1;
1104 }
1105 else if (!strcmp(argument, "string")) {
1106 *target = CLI_SEVERITY_STRING;
1107 return 1;
1108 }
1109 return 0;
1110}
1111
1112/* parse a "set severity-output" command. */
1113static int cli_parse_set_severity_output(char **args, struct appctx *appctx, void *private)
1114{
1115 if (*args[2] && set_severity_output(&appctx->cli_severity_output, args[2]))
1116 return 0;
1117
1118 appctx->ctx.cli.severity = LOG_ERR;
1119 appctx->ctx.cli.msg = "one of 'none', 'number', 'string' is a required argument";
1120 appctx->st0 = CLI_ST_PRINT;
1121 return 1;
1122}
William Lallemandeceddf72016-12-15 18:06:44 +01001123
1124int cli_parse_default(char **args, struct appctx *appctx, void *private)
1125{
1126 return 0;
1127}
1128
Willy Tarreau45c742b2016-11-24 14:51:17 +01001129/* parse a "set rate-limit" command. It always returns 1. */
1130static int cli_parse_set_ratelimit(char **args, struct appctx *appctx, void *private)
1131{
1132 int v;
1133 int *res;
1134 int mul = 1;
1135
1136 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1137 return 1;
1138
1139 if (strcmp(args[2], "connections") == 0 && strcmp(args[3], "global") == 0)
1140 res = &global.cps_lim;
1141 else if (strcmp(args[2], "sessions") == 0 && strcmp(args[3], "global") == 0)
1142 res = &global.sps_lim;
1143#ifdef USE_OPENSSL
1144 else if (strcmp(args[2], "ssl-sessions") == 0 && strcmp(args[3], "global") == 0)
1145 res = &global.ssl_lim;
1146#endif
1147 else if (strcmp(args[2], "http-compression") == 0 && strcmp(args[3], "global") == 0) {
1148 res = &global.comp_rate_lim;
1149 mul = 1024;
1150 }
1151 else {
1152 appctx->ctx.cli.msg =
1153 "'set rate-limit' only supports :\n"
1154 " - 'connections global' to set the per-process maximum connection rate\n"
1155 " - 'sessions global' to set the per-process maximum session rate\n"
1156#ifdef USE_OPENSSL
1157 " - 'ssl-session global' to set the per-process maximum SSL session rate\n"
1158#endif
1159 " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001160 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001161 return 1;
1162 }
1163
1164 if (!*args[4]) {
1165 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001166 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001167 return 1;
1168 }
1169
1170 v = atoi(args[4]);
1171 if (v < 0) {
1172 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001173 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001174 return 1;
1175 }
1176
1177 *res = v * mul;
1178
1179 /* Dequeues all of the listeners waiting for a resource */
1180 if (!LIST_ISEMPTY(&global_listener_queue))
1181 dequeue_all_listeners(&global_listener_queue);
1182
1183 return 1;
1184}
1185
William Lallemandf6975e92017-05-26 17:42:10 +02001186/* parse the "expose-fd" argument on the bind lines */
1187static int bind_parse_expose_fd(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1188{
1189 if (!*args[cur_arg + 1]) {
1190 memprintf(err, "'%s' : missing fd type", args[cur_arg]);
1191 return ERR_ALERT | ERR_FATAL;
1192 }
1193 if (!strcmp(args[cur_arg+1], "listeners")) {
1194 conf->level |= ACCESS_FD_LISTENERS;
1195 } else {
1196 memprintf(err, "'%s' only supports 'listeners' (got '%s')",
1197 args[cur_arg], args[cur_arg+1]);
1198 return ERR_ALERT | ERR_FATAL;
1199 }
1200
1201 return 0;
1202}
1203
William Lallemand74c24fb2016-11-21 17:18:36 +01001204/* parse the "level" argument on the bind lines */
1205static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1206{
1207 if (!*args[cur_arg + 1]) {
1208 memprintf(err, "'%s' : missing level", args[cur_arg]);
1209 return ERR_ALERT | ERR_FATAL;
1210 }
1211
William Lallemand07a62f72017-05-24 00:57:40 +02001212 if (!strcmp(args[cur_arg+1], "user")) {
1213 conf->level &= ~ACCESS_LVL_MASK;
1214 conf->level |= ACCESS_LVL_USER;
1215 } else if (!strcmp(args[cur_arg+1], "operator")) {
1216 conf->level &= ~ACCESS_LVL_MASK;
1217 conf->level |= ACCESS_LVL_OPER;
1218 } else if (!strcmp(args[cur_arg+1], "admin")) {
1219 conf->level &= ~ACCESS_LVL_MASK;
1220 conf->level |= ACCESS_LVL_ADMIN;
1221 } else {
William Lallemand74c24fb2016-11-21 17:18:36 +01001222 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
1223 args[cur_arg], args[cur_arg+1]);
1224 return ERR_ALERT | ERR_FATAL;
1225 }
1226
1227 return 0;
1228}
1229
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001230static int bind_parse_severity_output(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1231{
1232 if (!*args[cur_arg + 1]) {
1233 memprintf(err, "'%s' : missing severity format", args[cur_arg]);
1234 return ERR_ALERT | ERR_FATAL;
1235 }
1236
1237 if (set_severity_output(&conf->severity_output, args[cur_arg+1]))
1238 return 0;
1239 else {
1240 memprintf(err, "'%s' only supports 'none', 'number', and 'string' (got '%s')",
1241 args[cur_arg], args[cur_arg+1]);
1242 return ERR_ALERT | ERR_FATAL;
1243 }
1244}
1245
Olivier Houchardf886e342017-04-05 22:24:59 +02001246/* Send all the bound sockets, always returns 1 */
1247static int _getsocks(char **args, struct appctx *appctx, void *private)
1248{
1249 char *cmsgbuf = NULL;
1250 unsigned char *tmpbuf = NULL;
1251 struct cmsghdr *cmsg;
1252 struct stream_interface *si = appctx->owner;
William Lallemandf6975e92017-05-26 17:42:10 +02001253 struct stream *s = si_strm(si);
Olivier Houchardf886e342017-04-05 22:24:59 +02001254 struct connection *remote = objt_conn(si_opposite(si)->end);
1255 struct msghdr msghdr;
1256 struct iovec iov;
Olivier Houchard54740872017-04-06 14:45:14 +02001257 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf886e342017-04-05 22:24:59 +02001258 int *tmpfd;
1259 int tot_fd_nb = 0;
1260 struct proxy *px;
1261 int i = 0;
Willy Tarreau585744b2017-08-24 14:31:19 +02001262 int fd = remote->handle.fd;
Olivier Houchardf886e342017-04-05 22:24:59 +02001263 int curoff = 0;
1264 int old_fcntl;
1265 int ret;
1266
1267 /* Temporary set the FD in blocking mode, that will make our life easier */
1268 old_fcntl = fcntl(fd, F_GETFL);
1269 if (old_fcntl < 0) {
1270 Warning("Couldn't get the flags for the unix socket\n");
1271 goto out;
1272 }
1273 cmsgbuf = malloc(CMSG_SPACE(sizeof(int) * MAX_SEND_FD));
1274 if (!cmsgbuf) {
1275 Warning("Failed to allocate memory to send sockets\n");
1276 goto out;
1277 }
1278 if (fcntl(fd, F_SETFL, old_fcntl &~ O_NONBLOCK) == -1) {
1279 Warning("Cannot make the unix socket blocking\n");
1280 goto out;
1281 }
Olivier Houchard54740872017-04-06 14:45:14 +02001282 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf886e342017-04-05 22:24:59 +02001283 iov.iov_base = &tot_fd_nb;
1284 iov.iov_len = sizeof(tot_fd_nb);
William Lallemandf6975e92017-05-26 17:42:10 +02001285 if (!(strm_li(s)->bind_conf->level & ACCESS_FD_LISTENERS))
Olivier Houchardf886e342017-04-05 22:24:59 +02001286 goto out;
1287 memset(&msghdr, 0, sizeof(msghdr));
1288 /*
1289 * First, calculates the total number of FD, so that we can let
1290 * the caller know how much he should expects.
1291 */
1292 px = proxy;
1293 while (px) {
1294 struct listener *l;
1295
1296 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02001297 /* Only transfer IPv4/IPv6/UNIX sockets */
1298 if (l->state >= LI_ZOMBIE &&
1299 (l->proto->sock_family == AF_INET ||
Olivier Houchardf886e342017-04-05 22:24:59 +02001300 l->proto->sock_family == AF_INET6 ||
Olivier Houchard1fc05162017-04-06 01:05:05 +02001301 l->proto->sock_family == AF_UNIX))
Olivier Houchardf886e342017-04-05 22:24:59 +02001302 tot_fd_nb++;
1303 }
1304 px = px->next;
1305 }
1306 if (tot_fd_nb == 0)
1307 goto out;
1308
1309 /* First send the total number of file descriptors, so that the
1310 * receiving end knows what to expect.
1311 */
1312 msghdr.msg_iov = &iov;
1313 msghdr.msg_iovlen = 1;
1314 ret = sendmsg(fd, &msghdr, 0);
1315 if (ret != sizeof(tot_fd_nb)) {
1316 Warning("Failed to send the number of sockets to send\n");
1317 goto out;
1318 }
1319
1320 /* Now send the fds */
1321 msghdr.msg_control = cmsgbuf;
1322 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * MAX_SEND_FD);
1323 cmsg = CMSG_FIRSTHDR(&msghdr);
1324 cmsg->cmsg_len = CMSG_LEN(MAX_SEND_FD * sizeof(int));
1325 cmsg->cmsg_level = SOL_SOCKET;
1326 cmsg->cmsg_type = SCM_RIGHTS;
1327 tmpfd = (int *)CMSG_DATA(cmsg);
1328
1329 px = proxy;
1330 /* For each socket, e message is sent, containing the following :
1331 * Size of the namespace name (or 0 if none), as an unsigned char.
1332 * The namespace name, if any
1333 * Size of the interface name (or 0 if none), as an unsigned char
1334 * The interface name, if any
1335 * Listener options, as an int.
1336 */
1337 /* We will send sockets MAX_SEND_FD per MAX_SEND_FD, allocate a
1338 * buffer big enough to store the socket informations.
1339 */
1340 tmpbuf = malloc(MAX_SEND_FD * (1 + NAME_MAX + 1 + IFNAMSIZ + sizeof(int)));
1341 if (tmpbuf == NULL) {
1342 Warning("Failed to allocate memory to transfer socket informations\n");
1343 goto out;
1344 }
1345 iov.iov_base = tmpbuf;
1346 while (px) {
1347 struct listener *l;
1348
1349 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1350 int ret;
1351 /* Only transfer IPv4/IPv6 sockets */
Olivier Houchard1fc05162017-04-06 01:05:05 +02001352 if (l->state >= LI_ZOMBIE &&
Olivier Houchardf886e342017-04-05 22:24:59 +02001353 (l->proto->sock_family == AF_INET ||
1354 l->proto->sock_family == AF_INET6 ||
1355 l->proto->sock_family == AF_UNIX)) {
1356 memcpy(&tmpfd[i % MAX_SEND_FD], &l->fd, sizeof(l->fd));
1357 if (!l->netns)
1358 tmpbuf[curoff++] = 0;
1359#ifdef CONFIG_HAP_NS
1360 else {
1361 char *name = l->netns->node.key;
1362 unsigned char len = l->netns->name_len;
1363 tmpbuf[curoff++] = len;
1364 memcpy(tmpbuf + curoff, name, len);
1365 curoff += len;
1366 }
1367#endif
1368 if (l->interface) {
1369 unsigned char len = strlen(l->interface);
1370 tmpbuf[curoff++] = len;
1371 memcpy(tmpbuf + curoff, l->interface, len);
1372 curoff += len;
1373 } else
1374 tmpbuf[curoff++] = 0;
1375 memcpy(tmpbuf + curoff, &l->options,
1376 sizeof(l->options));
1377 curoff += sizeof(l->options);
1378
1379
1380 i++;
1381 } else
1382 continue;
1383 if ((!(i % MAX_SEND_FD))) {
1384 iov.iov_len = curoff;
1385 if (sendmsg(fd, &msghdr, 0) != curoff) {
1386 Warning("Failed to transfer sockets\n");
1387 printf("errno %d\n", errno);
1388 goto out;
1389 }
1390 /* Wait for an ack */
1391 do {
1392 ret = recv(fd, &tot_fd_nb,
1393 sizeof(tot_fd_nb), 0);
1394 } while (ret == -1 && errno == EINTR);
1395 if (ret <= 0) {
1396 Warning("Unexpected error while transferring sockets\n");
1397 goto out;
1398 }
1399 curoff = 0;
1400 }
1401
1402 }
1403 px = px->next;
1404 }
1405 if (i % MAX_SEND_FD) {
1406 iov.iov_len = curoff;
1407 cmsg->cmsg_len = CMSG_LEN((i % MAX_SEND_FD) * sizeof(int));
1408 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * (i % MAX_SEND_FD));
1409 if (sendmsg(fd, &msghdr, 0) != curoff) {
1410 Warning("Failed to transfer sockets\n");
1411 goto out;
1412 }
1413 }
1414
1415out:
1416 if (old_fcntl >= 0 && fcntl(fd, F_SETFL, old_fcntl) == -1) {
1417 Warning("Cannot make the unix socket non-blocking\n");
1418 goto out;
1419 }
1420 appctx->st0 = CLI_ST_END;
1421 free(cmsgbuf);
1422 free(tmpbuf);
1423 return 1;
1424}
1425
1426
1427
William Lallemand74c24fb2016-11-21 17:18:36 +01001428static struct applet cli_applet = {
1429 .obj_type = OBJ_TYPE_APPLET,
1430 .name = "<CLI>", /* used for logging */
1431 .fct = cli_io_handler,
1432 .release = cli_release_handler,
1433};
1434
Willy Tarreau0a739292016-11-22 20:21:23 +01001435/* register cli keywords */
1436static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau2af99412016-11-23 11:10:59 +01001437 { { "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 +01001438 { { "set", "rate-limit", NULL }, "set rate-limit : change a rate limiting value", cli_parse_set_ratelimit, NULL },
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001439 { { "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 +01001440 { { "set", "timeout", NULL }, "set timeout : change a timeout setting", cli_parse_set_timeout, NULL, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001441 { { "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 +01001442 { { "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 +02001443 { { "show", "fd", NULL }, "show fd [num] : dump list of file descriptors in use", cli_parse_show_fd, cli_io_handler_show_fd, NULL },
Olivier Houchardf886e342017-04-05 22:24:59 +02001444 { { "_getsocks", NULL }, NULL, _getsocks, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001445 {{},}
1446}};
1447
William Lallemand74c24fb2016-11-21 17:18:36 +01001448static struct cfg_kw_list cfg_kws = {ILH, {
1449 { CFG_GLOBAL, "stats", stats_parse_global },
1450 { 0, NULL, NULL },
1451}};
1452
1453static struct bind_kw_list bind_kws = { "STAT", { }, {
William Lallemandf6975e92017-05-26 17:42:10 +02001454 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
1455 { "expose-fd", bind_parse_expose_fd, 1 }, /* set the unix socket expose fd rights */
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001456 { "severity-output", bind_parse_severity_output, 1 }, /* set the severity output format */
William Lallemand74c24fb2016-11-21 17:18:36 +01001457 { NULL, NULL, 0 },
1458}};
1459
1460__attribute__((constructor))
1461static void __dumpstats_module_init(void)
1462{
1463 cfg_register_keywords(&cfg_kws);
Willy Tarreau0a739292016-11-22 20:21:23 +01001464 cli_register_kw(&cli_kws);
William Lallemand74c24fb2016-11-21 17:18:36 +01001465 bind_register_keywords(&bind_kws);
1466}
1467
1468/*
1469 * Local variables:
1470 * c-indent-level: 8
1471 * c-basic-offset: 8
1472 * End:
1473 */