blob: fa45db918f1b09252c77e3dfbebf9a561a8f0f8d [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
27#include <common/cfgparse.h>
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
34#include <common/ticks.h>
35#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38#include <common/base64.h>
39
40#include <types/applet.h>
William Lallemand9ed62032016-11-21 17:49:11 +010041#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010042#include <types/global.h>
43#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010045
46#include <proto/backend.h>
47#include <proto/channel.h>
48#include <proto/checks.h>
49#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010050#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010051#include <proto/fd.h>
52#include <proto/freq_ctr.h>
53#include <proto/frontend.h>
54#include <proto/log.h>
55#include <proto/pattern.h>
56#include <proto/pipe.h>
57#include <proto/listener.h>
58#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010059#include <proto/proto_uxst.h>
60#include <proto/proxy.h>
61#include <proto/sample.h>
62#include <proto/session.h>
63#include <proto/stream.h>
64#include <proto/server.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010065#include <proto/stream_interface.h>
66#include <proto/task.h>
67
William Lallemand74c24fb2016-11-21 17:18:36 +010068static struct applet cli_applet;
69
70static const char stats_sock_usage_msg[] =
71 "Unknown command. Please enter one of the following commands only :\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010072 " help : this message\n"
73 " prompt : toggle interactive mode with prompt\n"
74 " quit : disconnect\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010075 "";
76
77static const char stats_permission_denied_msg[] =
78 "Permission denied\n"
79 "";
80
81
82static char *dynamic_usage_msg = NULL;
83
84/* List head of cli keywords */
85static struct cli_kw_list cli_keywords = {
86 .list = LIST_HEAD_INIT(cli_keywords.list)
87};
88
89extern const char *stat_status_codes[];
90
91char *cli_gen_usage_msg()
92{
93 struct cli_kw_list *kw_list;
94 struct cli_kw *kw;
95 struct chunk *tmp = get_trash_chunk();
96 struct chunk out;
97
98 free(dynamic_usage_msg);
99 dynamic_usage_msg = NULL;
100
101 if (LIST_ISEMPTY(&cli_keywords.list))
102 return NULL;
103
104 chunk_reset(tmp);
105 chunk_strcat(tmp, stats_sock_usage_msg);
106 list_for_each_entry(kw_list, &cli_keywords.list, list) {
107 kw = &kw_list->kw[0];
108 while (kw->usage) {
109 chunk_appendf(tmp, " %s\n", kw->usage);
110 kw++;
111 }
112 }
113 chunk_init(&out, NULL, 0);
114 chunk_dup(&out, tmp);
115 dynamic_usage_msg = out.str;
116 return dynamic_usage_msg;
117}
118
119struct cli_kw* cli_find_kw(char **args)
120{
121 struct cli_kw_list *kw_list;
122 struct cli_kw *kw;/* current cli_kw */
123 char **tmp_args;
124 const char **tmp_str_kw;
125 int found = 0;
126
127 if (LIST_ISEMPTY(&cli_keywords.list))
128 return NULL;
129
130 list_for_each_entry(kw_list, &cli_keywords.list, list) {
131 kw = &kw_list->kw[0];
132 while (*kw->str_kw) {
133 tmp_args = args;
134 tmp_str_kw = kw->str_kw;
135 while (*tmp_str_kw) {
136 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
137 found = 1;
138 } else {
139 found = 0;
140 break;
141 }
142 tmp_args++;
143 tmp_str_kw++;
144 }
145 if (found)
146 return (kw);
147 kw++;
148 }
149 }
150 return NULL;
151}
152
153void cli_register_kw(struct cli_kw_list *kw_list)
154{
155 LIST_ADDQ(&cli_keywords.list, &kw_list->list);
156}
157
158
159/* allocate a new stats frontend named <name>, and return it
160 * (or NULL in case of lack of memory).
161 */
162static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
163{
164 struct proxy *fe;
165
166 fe = calloc(1, sizeof(*fe));
167 if (!fe)
168 return NULL;
169
170 init_new_proxy(fe);
171 fe->next = proxy;
172 proxy = fe;
173 fe->last_change = now.tv_sec;
174 fe->id = strdup("GLOBAL");
175 fe->cap = PR_CAP_FE;
176 fe->maxconn = 10; /* default to 10 concurrent connections */
177 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
178 fe->conf.file = strdup(file);
179 fe->conf.line = line;
180 fe->accept = frontend_accept;
181 fe->default_target = &cli_applet.obj_type;
182
183 /* the stats frontend is the only one able to assign ID #0 */
184 fe->conf.id.key = fe->uuid = 0;
185 eb32_insert(&used_proxy_id, &fe->conf.id);
186 return fe;
187}
188
189/* This function parses a "stats" statement in the "global" section. It returns
190 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
191 * error message into the <err> buffer which will be preallocated. The trailing
192 * '\n' must not be written. The function must be called with <args> pointing to
193 * the first word after "stats".
194 */
195static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
196 struct proxy *defpx, const char *file, int line,
197 char **err)
198{
199 struct bind_conf *bind_conf;
200 struct listener *l;
201
202 if (!strcmp(args[1], "socket")) {
203 int cur_arg;
204
205 if (*args[2] == 0) {
206 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
207 return -1;
208 }
209
210 if (!global.stats_fe) {
211 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
212 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
213 return -1;
214 }
215 }
216
Willy Tarreaua261e9b2016-12-22 20:44:00 +0100217 bind_conf = bind_conf_alloc(global.stats_fe, file, line, args[2], xprt_get(XPRT_RAW));
William Lallemand74c24fb2016-11-21 17:18:36 +0100218 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
219
220 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
221 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
222 file, line, args[0], args[1], err && *err ? *err : "error");
223 return -1;
224 }
225
226 cur_arg = 3;
227 while (*args[cur_arg]) {
228 static int bind_dumped;
229 struct bind_kw *kw;
230
231 kw = bind_find_kw(args[cur_arg]);
232 if (kw) {
233 if (!kw->parse) {
234 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
235 args[0], args[1], args[cur_arg]);
236 return -1;
237 }
238
239 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
240 if (err && *err)
241 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
242 else
243 memprintf(err, "'%s %s' : error encountered while processing '%s'",
244 args[0], args[1], args[cur_arg]);
245 return -1;
246 }
247
248 cur_arg += 1 + kw->skip;
249 continue;
250 }
251
252 if (!bind_dumped) {
253 bind_dump_kws(err);
254 indent_msg(err, 4);
255 bind_dumped = 1;
256 }
257
258 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
259 args[0], args[1], args[cur_arg],
260 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
261 return -1;
262 }
263
264 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
265 l->maxconn = global.stats_fe->maxconn;
266 l->backlog = global.stats_fe->backlog;
267 l->accept = session_accept_fd;
268 l->handler = process_stream;
269 l->default_target = global.stats_fe->default_target;
270 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
271 l->nice = -64; /* we want to boost priority for local stats */
272 global.maxsock += l->maxconn;
273 }
274 }
275 else if (!strcmp(args[1], "timeout")) {
276 unsigned timeout;
277 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
278
279 if (res) {
280 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
281 return -1;
282 }
283
284 if (!timeout) {
285 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
286 return -1;
287 }
288 if (!global.stats_fe) {
289 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
290 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
291 return -1;
292 }
293 }
294 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
295 }
296 else if (!strcmp(args[1], "maxconn")) {
297 int maxconn = atol(args[2]);
298
299 if (maxconn <= 0) {
300 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
301 return -1;
302 }
303
304 if (!global.stats_fe) {
305 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
306 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
307 return -1;
308 }
309 }
310 global.stats_fe->maxconn = maxconn;
311 }
312 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
313 int cur_arg = 2;
314 unsigned long set = 0;
315
316 if (!global.stats_fe) {
317 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
318 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
319 return -1;
320 }
321 }
322
323 while (*args[cur_arg]) {
324 unsigned int low, high;
325
326 if (strcmp(args[cur_arg], "all") == 0) {
327 set = 0;
328 break;
329 }
330 else if (strcmp(args[cur_arg], "odd") == 0) {
331 set |= ~0UL/3UL; /* 0x555....555 */
332 }
333 else if (strcmp(args[cur_arg], "even") == 0) {
334 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
335 }
336 else if (isdigit((int)*args[cur_arg])) {
337 char *dash = strchr(args[cur_arg], '-');
338
339 low = high = str2uic(args[cur_arg]);
340 if (dash)
341 high = str2uic(dash + 1);
342
343 if (high < low) {
344 unsigned int swap = low;
345 low = high;
346 high = swap;
347 }
348
349 if (low < 1 || high > LONGBITS) {
350 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
351 args[0], args[1], LONGBITS);
352 return -1;
353 }
354 while (low <= high)
355 set |= 1UL << (low++ - 1);
356 }
357 else {
358 memprintf(err,
359 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
360 args[0], args[1], LONGBITS);
361 return -1;
362 }
363 cur_arg++;
364 }
365 global.stats_fe->bind_proc = set;
366 }
367 else {
368 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
369 return -1;
370 }
371 return 0;
372}
373
Willy Tarreaude57a572016-11-23 17:01:39 +0100374/* Verifies that the CLI at least has a level at least as high as <level>
375 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
376 * failure, an error message is prepared and the appctx's state is adjusted
377 * to print it so that a return 1 is enough to abort any processing.
378 */
379int cli_has_level(struct appctx *appctx, int level)
380{
381 struct stream_interface *si = appctx->owner;
382 struct stream *s = si_strm(si);
383
384 if (strm_li(s)->bind_conf->level < level) {
385 appctx->ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100386 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaude57a572016-11-23 17:01:39 +0100387 return 0;
388 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100389 return 1;
390}
391
William Lallemand74c24fb2016-11-21 17:18:36 +0100392
Willy Tarreau41908562016-11-24 16:23:38 +0100393/* Processes the CLI interpreter on the stats socket. This function is called
394 * from the CLI's IO handler running in an appctx context. The function returns 1
395 * if the request was understood, otherwise zero. It is called with appctx->st0
396 * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do
397 * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to
398 * have its own I/O handler called again. Most of the time, parsers will only
399 * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg.
Willy Tarreaueaffde32016-12-16 17:59:25 +0100400 * If a keyword parser is NULL and an I/O handler is declared, the I/O handler
401 * will automatically be used.
William Lallemand74c24fb2016-11-21 17:18:36 +0100402 */
Willy Tarreau41908562016-11-24 16:23:38 +0100403static int cli_parse_request(struct appctx *appctx, char *line)
William Lallemand74c24fb2016-11-21 17:18:36 +0100404{
William Lallemand74c24fb2016-11-21 17:18:36 +0100405 char *args[MAX_STATS_ARGS + 1];
406 struct cli_kw *kw;
407 int arg;
408 int i, j;
409
410 while (isspace((unsigned char)*line))
411 line++;
412
413 arg = 0;
414 args[arg] = line;
415
416 while (*line && arg < MAX_STATS_ARGS) {
417 if (*line == '\\') {
418 line++;
419 if (*line == '\0')
420 break;
421 }
422 else if (isspace((unsigned char)*line)) {
423 *line++ = '\0';
424
425 while (isspace((unsigned char)*line))
426 line++;
427
428 args[++arg] = line;
429 continue;
430 }
431
432 line++;
433 }
434
435 while (++arg <= MAX_STATS_ARGS)
436 args[arg] = line;
437
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100438 /* unescape '\' */
William Lallemand74c24fb2016-11-21 17:18:36 +0100439 arg = 0;
440 while (*args[arg] != '\0') {
441 j = 0;
442 for (i=0; args[arg][i] != '\0'; i++) {
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100443 if (args[arg][i] == '\\') {
444 if (args[arg][i+1] == '\\')
445 i++;
446 else
447 continue;
448 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100449 args[arg][j] = args[arg][i];
450 j++;
451 }
452 args[arg][j] = '\0';
453 arg++;
454 }
455
Willy Tarreau41908562016-11-24 16:23:38 +0100456 appctx->st2 = 0;
Willy Tarreaua2d58722016-12-16 12:37:03 +0100457 memset(&appctx->ctx.cli, 0, sizeof(appctx->ctx.cli));
Willy Tarreau41908562016-11-24 16:23:38 +0100458
459 kw = cli_find_kw(args);
Willy Tarreaueaffde32016-12-16 17:59:25 +0100460 if (!kw)
Willy Tarreau41908562016-11-24 16:23:38 +0100461 return 0;
462
463 appctx->io_handler = kw->io_handler;
Willy Tarreaueaffde32016-12-16 17:59:25 +0100464 if ((!kw->parse || kw->parse(args, appctx, kw->private) == 0) && appctx->io_handler) {
Willy Tarreau41908562016-11-24 16:23:38 +0100465 appctx->st0 = CLI_ST_CALLBACK;
466 appctx->io_release = kw->io_release;
William Lallemand74c24fb2016-11-21 17:18:36 +0100467 }
Willy Tarreau41908562016-11-24 16:23:38 +0100468 return 1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100469}
470
471/* This I/O handler runs as an applet embedded in a stream interface. It is
472 * used to processes I/O from/to the stats unix socket. The system relies on a
473 * state machine handling requests and various responses. We read a request,
474 * then we process it and send the response, and we possibly display a prompt.
475 * Then we can read again. The state is stored in appctx->st0 and is one of the
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100476 * CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled
William Lallemand74c24fb2016-11-21 17:18:36 +0100477 * or not.
478 */
479static void cli_io_handler(struct appctx *appctx)
480{
481 struct stream_interface *si = appctx->owner;
482 struct channel *req = si_oc(si);
483 struct channel *res = si_ic(si);
484 int reql;
485 int len;
486
487 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
488 goto out;
489
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100490 /* Check if the input buffer is avalaible. */
491 if (res->buf->size == 0) {
492 si_applet_cant_put(si);
493 goto out;
494 }
495
William Lallemand74c24fb2016-11-21 17:18:36 +0100496 while (1) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100497 if (appctx->st0 == CLI_ST_INIT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100498 /* Stats output not initialized yet */
499 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100500 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100501 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100502 else if (appctx->st0 == CLI_ST_END) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100503 /* Let's close for real now. We just close the request
504 * side, the conditions below will complete if needed.
505 */
506 si_shutw(si);
507 break;
508 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100509 else if (appctx->st0 == CLI_ST_GETREQ) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100510 /* ensure we have some output room left in the event we
511 * would want to return some info right after parsing.
512 */
513 if (buffer_almost_full(si_ib(si))) {
514 si_applet_cant_put(si);
515 break;
516 }
517
518 reql = bo_getline(si_oc(si), trash.str, trash.size);
519 if (reql <= 0) { /* closed or EOL not found */
520 if (reql == 0)
521 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100522 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100523 continue;
524 }
525
526 /* seek for a possible unescaped semi-colon. If we find
527 * one, we replace it with an LF and skip only this part.
528 */
529 for (len = 0; len < reql; len++) {
530 if (trash.str[len] == '\\') {
531 len++;
532 continue;
533 }
534 if (trash.str[len] == ';') {
535 trash.str[len] = '\n';
536 reql = len + 1;
537 break;
538 }
539 }
540
541 /* now it is time to check that we have a full line,
542 * remove the trailing \n and possibly \r, then cut the
543 * line.
544 */
545 len = reql - 1;
546 if (trash.str[len] != '\n') {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100547 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100548 continue;
549 }
550
551 if (len && trash.str[len-1] == '\r')
552 len--;
553
554 trash.str[len] = '\0';
555
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100556 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100557 if (len) {
558 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100559 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100560 continue;
561 }
562 else if (strcmp(trash.str, "prompt") == 0)
563 appctx->st1 = !appctx->st1;
564 else if (strcmp(trash.str, "help") == 0 ||
Willy Tarreau41908562016-11-24 16:23:38 +0100565 !cli_parse_request(appctx, trash.str)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100566 cli_gen_usage_msg();
567 if (dynamic_usage_msg)
568 appctx->ctx.cli.msg = dynamic_usage_msg;
569 else
570 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100571 appctx->st0 = CLI_ST_PRINT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100572 }
573 /* NB: stats_sock_parse_request() may have put
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100574 * another CLI_ST_O_* into appctx->st0.
William Lallemand74c24fb2016-11-21 17:18:36 +0100575 */
576 }
577 else if (!appctx->st1) {
578 /* if prompt is disabled, print help on empty lines,
579 * so that the user at least knows how to enable
580 * prompt and find help.
581 */
582 cli_gen_usage_msg();
583 if (dynamic_usage_msg)
584 appctx->ctx.cli.msg = dynamic_usage_msg;
585 else
586 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100587 appctx->st0 = CLI_ST_PRINT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100588 }
589
590 /* re-adjust req buffer */
591 bo_skip(si_oc(si), reql);
592 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
593 }
594 else { /* output functions */
595 switch (appctx->st0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100596 case CLI_ST_PROMPT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100597 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100598 case CLI_ST_PRINT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100599 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100600 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100601 else
602 si_applet_cant_put(si);
603 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100604 case CLI_ST_PRINT_FREE:
William Lallemand74c24fb2016-11-21 17:18:36 +0100605 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
606 free(appctx->ctx.cli.err);
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100607 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100608 }
609 else
610 si_applet_cant_put(si);
611 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100612 case CLI_ST_CALLBACK: /* use custom pointer */
William Lallemand74c24fb2016-11-21 17:18:36 +0100613 if (appctx->io_handler)
614 if (appctx->io_handler(appctx)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100615 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100616 if (appctx->io_release) {
617 appctx->io_release(appctx);
618 appctx->io_release = NULL;
619 }
620 }
621 break;
622 default: /* abnormal state */
623 si->flags |= SI_FL_ERR;
624 break;
625 }
626
627 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100628 if (appctx->st0 == CLI_ST_PROMPT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100629 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100630 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100631 else
632 si_applet_cant_put(si);
633 }
634
635 /* If the output functions are still there, it means they require more room. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100636 if (appctx->st0 >= CLI_ST_OUTPUT)
William Lallemand74c24fb2016-11-21 17:18:36 +0100637 break;
638
639 /* Now we close the output if one of the writers did so,
640 * or if we're not in interactive mode and the request
641 * buffer is empty. This still allows pipelined requests
642 * to be sent in non-interactive mode.
643 */
644 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100645 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100646 continue;
647 }
648
649 /* switch state back to GETREQ to read next requests */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100650 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100651 }
652 }
653
654 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
655 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
656 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
657 /* Other side has closed, let's abort if we have no more processing to do
658 * and nothing more to consume. This is comparable to a broken pipe, so
659 * we forward the close to the request side so that it flows upstream to
660 * the client.
661 */
662 si_shutw(si);
663 }
664
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100665 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < CLI_ST_OUTPUT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100666 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
667 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
668 /* We have no more processing to do, and nothing more to send, and
669 * the client side has closed. So we'll forward this state downstream
670 * on the response buffer.
671 */
672 si_shutr(si);
673 res->flags |= CF_READ_NULL;
674 }
675
676 out:
677 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
678 __FUNCTION__, __LINE__,
679 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
680}
681
William Lallemand74c24fb2016-11-21 17:18:36 +0100682/* This is called when the stream interface is closed. For instance, upon an
683 * external abort, we won't call the i/o handler anymore so we may need to
684 * remove back references to the stream currently being dumped.
685 */
686static void cli_release_handler(struct appctx *appctx)
687{
688 if (appctx->io_release) {
689 appctx->io_release(appctx);
690 appctx->io_release = NULL;
691 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100692 else if (appctx->st0 == CLI_ST_PRINT_FREE) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100693 free(appctx->ctx.cli.err);
694 appctx->ctx.cli.err = NULL;
695 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100696}
697
698/* This function dumps all environmnent variables to the buffer. It returns 0
699 * if the output buffer is full and it needs to be called again, otherwise
Willy Tarreauf6710f82016-12-16 17:45:44 +0100700 * non-zero. Dumps only one entry if st2 == STAT_ST_END. It uses cli.p0 as the
701 * pointer to the current variable.
William Lallemand74c24fb2016-11-21 17:18:36 +0100702 */
Willy Tarreau0a739292016-11-22 20:21:23 +0100703static int cli_io_handler_show_env(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100704{
Willy Tarreau0a739292016-11-22 20:21:23 +0100705 struct stream_interface *si = appctx->owner;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100706 char **var = appctx->ctx.cli.p0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100707
708 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
709 return 1;
710
711 chunk_reset(&trash);
712
713 /* we have two inner loops here, one for the proxy, the other one for
714 * the buffer.
715 */
Willy Tarreauf6710f82016-12-16 17:45:44 +0100716 while (*var) {
717 chunk_printf(&trash, "%s\n", *var);
William Lallemand74c24fb2016-11-21 17:18:36 +0100718
719 if (bi_putchk(si_ic(si), &trash) == -1) {
720 si_applet_cant_put(si);
721 return 0;
722 }
723 if (appctx->st2 == STAT_ST_END)
724 break;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100725 var++;
726 appctx->ctx.cli.p0 = var;
William Lallemand74c24fb2016-11-21 17:18:36 +0100727 }
728
729 /* dump complete */
730 return 1;
731}
732
William Lallemandeceddf72016-12-15 18:06:44 +0100733/*
Willy Tarreau3af9d832016-12-16 12:58:09 +0100734 * CLI IO handler for `show cli sockets`.
735 * Uses ctx.cli.p0 to store the restart pointer.
William Lallemandeceddf72016-12-15 18:06:44 +0100736 */
737static int cli_io_handler_show_cli_sock(struct appctx *appctx)
738{
739 struct bind_conf *bind_conf;
740 struct stream_interface *si = appctx->owner;
741
742 chunk_reset(&trash);
743
744 switch (appctx->st2) {
745 case STAT_ST_INIT:
746 chunk_printf(&trash, "# socket lvl processes\n");
747 if (bi_putchk(si_ic(si), &trash) == -1) {
748 si_applet_cant_put(si);
749 return 0;
750 }
William Lallemandeceddf72016-12-15 18:06:44 +0100751 appctx->st2 = STAT_ST_LIST;
752
753 case STAT_ST_LIST:
754 if (global.stats_fe) {
755 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
756 struct listener *l;
757
758 /*
Willy Tarreau3af9d832016-12-16 12:58:09 +0100759 * get the latest dumped node in appctx->ctx.cli.p0
William Lallemandeceddf72016-12-15 18:06:44 +0100760 * if the current node is the first of the list
761 */
762
Willy Tarreau3af9d832016-12-16 12:58:09 +0100763 if (appctx->ctx.cli.p0 &&
764 &bind_conf->by_fe == (&global.stats_fe->conf.bind)->n) {
William Lallemandeceddf72016-12-15 18:06:44 +0100765 /* change the current node to the latest dumped and continue the loop */
Willy Tarreau3af9d832016-12-16 12:58:09 +0100766 bind_conf = LIST_ELEM(appctx->ctx.cli.p0, typeof(bind_conf), by_fe);
William Lallemandeceddf72016-12-15 18:06:44 +0100767 continue;
768 }
769
770 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
771
772 char addr[46];
773 char port[6];
774
775 if (l->addr.ss_family == AF_UNIX) {
776 const struct sockaddr_un *un;
777
778 un = (struct sockaddr_un *)&l->addr;
779 chunk_appendf(&trash, "%s ", un->sun_path);
780 } else if (l->addr.ss_family == AF_INET) {
781 addr_to_str(&l->addr, addr, sizeof(addr));
782 port_to_str(&l->addr, port, sizeof(port));
783 chunk_appendf(&trash, "%s:%s ", addr, port);
784 } else if (l->addr.ss_family == AF_INET6) {
785 addr_to_str(&l->addr, addr, sizeof(addr));
786 port_to_str(&l->addr, port, sizeof(port));
787 chunk_appendf(&trash, "[%s]:%s ", addr, port);
788 } else
789 continue;
790
791 if (bind_conf->level == ACCESS_LVL_USER)
792 chunk_appendf(&trash, "user ");
793 else if (bind_conf->level == ACCESS_LVL_OPER)
794 chunk_appendf(&trash, "operator ");
795 else if (bind_conf->level == ACCESS_LVL_ADMIN)
796 chunk_appendf(&trash, "admin ");
797 else
798 chunk_appendf(&trash, " ");
799
800 if (bind_conf->bind_proc != 0) {
801 int pos;
Willy Tarreau20c5e522016-12-16 12:50:55 +0100802 for (pos = 0; pos < 8 * sizeof(bind_conf->bind_proc); pos++) {
Willy Tarreau4305ac72016-12-16 12:56:31 +0100803 if (bind_conf->bind_proc & (1UL << pos)) {
William Lallemandeceddf72016-12-15 18:06:44 +0100804 chunk_appendf(&trash, "%d,", pos+1);
805 }
806 }
807 /* replace the latest comma by a newline */
808 trash.str[trash.len-1] = '\n';
809
810 } else {
811 chunk_appendf(&trash, "all\n");
812 }
813
814 if (bi_putchk(si_ic(si), &trash) == -1) {
815 si_applet_cant_put(si);
816 return 0;
817 }
818 }
Willy Tarreau3af9d832016-12-16 12:58:09 +0100819 appctx->ctx.cli.p0 = &bind_conf->by_fe; /* store the latest list node dumped */
William Lallemandeceddf72016-12-15 18:06:44 +0100820 }
821 }
822 default:
823 appctx->st2 = STAT_ST_FIN;
824 return 1;
825 }
826}
827
828
Willy Tarreau0a739292016-11-22 20:21:23 +0100829/* parse a "show env" CLI request. Returns 0 if it needs to continue, 1 if it
Willy Tarreauf6710f82016-12-16 17:45:44 +0100830 * wants to stop here. It puts the variable to be dumped into cli.p0 if a single
831 * variable is requested otherwise puts environ there.
Willy Tarreau0a739292016-11-22 20:21:23 +0100832 */
833static int cli_parse_show_env(char **args, struct appctx *appctx, void *private)
834{
835 extern char **environ;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100836 char **var;
Willy Tarreau0a739292016-11-22 20:21:23 +0100837
838 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
839 return 1;
840
Willy Tarreauf6710f82016-12-16 17:45:44 +0100841 var = environ;
Willy Tarreau0a739292016-11-22 20:21:23 +0100842
843 if (*args[2]) {
844 int len = strlen(args[2]);
845
Willy Tarreauf6710f82016-12-16 17:45:44 +0100846 for (; *var; var++) {
847 if (strncmp(*var, args[2], len) == 0 &&
848 (*var)[len] == '=')
Willy Tarreau0a739292016-11-22 20:21:23 +0100849 break;
850 }
Willy Tarreauf6710f82016-12-16 17:45:44 +0100851 if (!*var) {
Willy Tarreau0a739292016-11-22 20:21:23 +0100852 appctx->ctx.cli.msg = "Variable not found\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100853 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau0a739292016-11-22 20:21:23 +0100854 return 1;
855 }
856 appctx->st2 = STAT_ST_END;
857 }
Willy Tarreauf6710f82016-12-16 17:45:44 +0100858 appctx->ctx.cli.p0 = var;
Willy Tarreau0a739292016-11-22 20:21:23 +0100859 return 0;
860}
861
Willy Tarreau599852e2016-11-22 20:33:32 +0100862/* parse a "set timeout" CLI request. It always returns 1. */
863static int cli_parse_set_timeout(char **args, struct appctx *appctx, void *private)
864{
865 struct stream_interface *si = appctx->owner;
866 struct stream *s = si_strm(si);
867
868 if (strcmp(args[2], "cli") == 0) {
869 unsigned timeout;
870 const char *res;
871
872 if (!*args[3]) {
873 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100874 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +0100875 return 1;
876 }
877
878 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
879 if (res || timeout < 1) {
880 appctx->ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100881 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +0100882 return 1;
883 }
884
885 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
886 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
887 return 1;
888 }
889 else {
890 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100891 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +0100892 return 1;
893 }
894}
895
Willy Tarreau2af99412016-11-23 11:10:59 +0100896/* parse a "set maxconn global" command. It always returns 1. */
897static int cli_parse_set_maxconn_global(char **args, struct appctx *appctx, void *private)
898{
899 int v;
900
901 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
902 return 1;
903
904 if (!*args[3]) {
905 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100906 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +0100907 return 1;
908 }
909
910 v = atoi(args[3]);
911 if (v > global.hardmaxconn) {
912 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100913 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +0100914 return 1;
915 }
916
917 /* check for unlimited values */
918 if (v <= 0)
919 v = global.hardmaxconn;
920
921 global.maxconn = v;
922
923 /* Dequeues all of the listeners waiting for a resource */
924 if (!LIST_ISEMPTY(&global_listener_queue))
925 dequeue_all_listeners(&global_listener_queue);
926
927 return 1;
928}
929
William Lallemandeceddf72016-12-15 18:06:44 +0100930
931int cli_parse_default(char **args, struct appctx *appctx, void *private)
932{
933 return 0;
934}
935
Willy Tarreau45c742b2016-11-24 14:51:17 +0100936/* parse a "set rate-limit" command. It always returns 1. */
937static int cli_parse_set_ratelimit(char **args, struct appctx *appctx, void *private)
938{
939 int v;
940 int *res;
941 int mul = 1;
942
943 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
944 return 1;
945
946 if (strcmp(args[2], "connections") == 0 && strcmp(args[3], "global") == 0)
947 res = &global.cps_lim;
948 else if (strcmp(args[2], "sessions") == 0 && strcmp(args[3], "global") == 0)
949 res = &global.sps_lim;
950#ifdef USE_OPENSSL
951 else if (strcmp(args[2], "ssl-sessions") == 0 && strcmp(args[3], "global") == 0)
952 res = &global.ssl_lim;
953#endif
954 else if (strcmp(args[2], "http-compression") == 0 && strcmp(args[3], "global") == 0) {
955 res = &global.comp_rate_lim;
956 mul = 1024;
957 }
958 else {
959 appctx->ctx.cli.msg =
960 "'set rate-limit' only supports :\n"
961 " - 'connections global' to set the per-process maximum connection rate\n"
962 " - 'sessions global' to set the per-process maximum session rate\n"
963#ifdef USE_OPENSSL
964 " - 'ssl-session global' to set the per-process maximum SSL session rate\n"
965#endif
966 " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100967 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +0100968 return 1;
969 }
970
971 if (!*args[4]) {
972 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100973 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +0100974 return 1;
975 }
976
977 v = atoi(args[4]);
978 if (v < 0) {
979 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100980 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +0100981 return 1;
982 }
983
984 *res = v * mul;
985
986 /* Dequeues all of the listeners waiting for a resource */
987 if (!LIST_ISEMPTY(&global_listener_queue))
988 dequeue_all_listeners(&global_listener_queue);
989
990 return 1;
991}
992
William Lallemand74c24fb2016-11-21 17:18:36 +0100993/* parse the "level" argument on the bind lines */
994static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
995{
996 if (!*args[cur_arg + 1]) {
997 memprintf(err, "'%s' : missing level", args[cur_arg]);
998 return ERR_ALERT | ERR_FATAL;
999 }
1000
1001 if (!strcmp(args[cur_arg+1], "user"))
1002 conf->level = ACCESS_LVL_USER;
1003 else if (!strcmp(args[cur_arg+1], "operator"))
1004 conf->level = ACCESS_LVL_OPER;
1005 else if (!strcmp(args[cur_arg+1], "admin"))
1006 conf->level = ACCESS_LVL_ADMIN;
1007 else {
1008 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
1009 args[cur_arg], args[cur_arg+1]);
1010 return ERR_ALERT | ERR_FATAL;
1011 }
1012
1013 return 0;
1014}
1015
1016static struct applet cli_applet = {
1017 .obj_type = OBJ_TYPE_APPLET,
1018 .name = "<CLI>", /* used for logging */
1019 .fct = cli_io_handler,
1020 .release = cli_release_handler,
1021};
1022
Willy Tarreau0a739292016-11-22 20:21:23 +01001023/* register cli keywords */
1024static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau2af99412016-11-23 11:10:59 +01001025 { { "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 +01001026 { { "set", "rate-limit", NULL }, "set rate-limit : change a rate limiting value", cli_parse_set_ratelimit, NULL },
Willy Tarreau599852e2016-11-22 20:33:32 +01001027 { { "set", "timeout", NULL }, "set timeout : change a timeout setting", cli_parse_set_timeout, NULL, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001028 { { "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 +01001029 { { "show", "cli", "sockets", NULL }, "show cli sockets : dump list of cli sockets", cli_parse_default, cli_io_handler_show_cli_sock, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001030 {{},}
1031}};
1032
William Lallemand74c24fb2016-11-21 17:18:36 +01001033static struct cfg_kw_list cfg_kws = {ILH, {
1034 { CFG_GLOBAL, "stats", stats_parse_global },
1035 { 0, NULL, NULL },
1036}};
1037
1038static struct bind_kw_list bind_kws = { "STAT", { }, {
1039 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
1040 { NULL, NULL, 0 },
1041}};
1042
1043__attribute__((constructor))
1044static void __dumpstats_module_init(void)
1045{
1046 cfg_register_keywords(&cfg_kws);
Willy Tarreau0a739292016-11-22 20:21:23 +01001047 cli_register_kw(&cli_kws);
William Lallemand74c24fb2016-11-21 17:18:36 +01001048 bind_register_keywords(&bind_kws);
1049}
1050
1051/*
1052 * Local variables:
1053 * c-indent-level: 8
1054 * c-basic-offset: 8
1055 * End:
1056 */