blob: 649881592a713d6d6b65cac9598acbe984e0e27e [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;
272 l->handler = process_stream;
273 l->default_target = global.stats_fe->default_target;
274 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
275 l->nice = -64; /* we want to boost priority for local stats */
276 global.maxsock += l->maxconn;
277 }
278 }
279 else if (!strcmp(args[1], "timeout")) {
280 unsigned timeout;
281 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
282
283 if (res) {
284 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
285 return -1;
286 }
287
288 if (!timeout) {
289 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
290 return -1;
291 }
292 if (!global.stats_fe) {
293 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
294 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
295 return -1;
296 }
297 }
298 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
299 }
300 else if (!strcmp(args[1], "maxconn")) {
301 int maxconn = atol(args[2]);
302
303 if (maxconn <= 0) {
304 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
305 return -1;
306 }
307
308 if (!global.stats_fe) {
309 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
310 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
311 return -1;
312 }
313 }
314 global.stats_fe->maxconn = maxconn;
315 }
316 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
317 int cur_arg = 2;
318 unsigned long set = 0;
319
320 if (!global.stats_fe) {
321 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
322 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
323 return -1;
324 }
325 }
326
327 while (*args[cur_arg]) {
328 unsigned int low, high;
329
330 if (strcmp(args[cur_arg], "all") == 0) {
331 set = 0;
332 break;
333 }
334 else if (strcmp(args[cur_arg], "odd") == 0) {
335 set |= ~0UL/3UL; /* 0x555....555 */
336 }
337 else if (strcmp(args[cur_arg], "even") == 0) {
338 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
339 }
340 else if (isdigit((int)*args[cur_arg])) {
341 char *dash = strchr(args[cur_arg], '-');
342
343 low = high = str2uic(args[cur_arg]);
344 if (dash)
345 high = str2uic(dash + 1);
346
347 if (high < low) {
348 unsigned int swap = low;
349 low = high;
350 high = swap;
351 }
352
353 if (low < 1 || high > LONGBITS) {
354 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
355 args[0], args[1], LONGBITS);
356 return -1;
357 }
358 while (low <= high)
359 set |= 1UL << (low++ - 1);
360 }
361 else {
362 memprintf(err,
363 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
364 args[0], args[1], LONGBITS);
365 return -1;
366 }
367 cur_arg++;
368 }
369 global.stats_fe->bind_proc = set;
370 }
371 else {
372 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
373 return -1;
374 }
375 return 0;
376}
377
Willy Tarreaude57a572016-11-23 17:01:39 +0100378/* Verifies that the CLI at least has a level at least as high as <level>
379 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
380 * failure, an error message is prepared and the appctx's state is adjusted
381 * to print it so that a return 1 is enough to abort any processing.
382 */
383int cli_has_level(struct appctx *appctx, int level)
384{
385 struct stream_interface *si = appctx->owner;
386 struct stream *s = si_strm(si);
387
William Lallemand07a62f72017-05-24 00:57:40 +0200388 if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < level) {
Willy Tarreaude57a572016-11-23 17:01:39 +0100389 appctx->ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100390 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaude57a572016-11-23 17:01:39 +0100391 return 0;
392 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100393 return 1;
394}
395
William Lallemand74c24fb2016-11-21 17:18:36 +0100396
Willy Tarreau41908562016-11-24 16:23:38 +0100397/* Processes the CLI interpreter on the stats socket. This function is called
398 * from the CLI's IO handler running in an appctx context. The function returns 1
399 * if the request was understood, otherwise zero. It is called with appctx->st0
400 * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do
401 * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to
402 * have its own I/O handler called again. Most of the time, parsers will only
403 * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg.
Willy Tarreaueaffde32016-12-16 17:59:25 +0100404 * If a keyword parser is NULL and an I/O handler is declared, the I/O handler
405 * will automatically be used.
William Lallemand74c24fb2016-11-21 17:18:36 +0100406 */
Willy Tarreau41908562016-11-24 16:23:38 +0100407static int cli_parse_request(struct appctx *appctx, char *line)
William Lallemand74c24fb2016-11-21 17:18:36 +0100408{
William Lallemand74c24fb2016-11-21 17:18:36 +0100409 char *args[MAX_STATS_ARGS + 1];
410 struct cli_kw *kw;
411 int arg;
412 int i, j;
413
414 while (isspace((unsigned char)*line))
415 line++;
416
417 arg = 0;
418 args[arg] = line;
419
420 while (*line && arg < MAX_STATS_ARGS) {
421 if (*line == '\\') {
422 line++;
423 if (*line == '\0')
424 break;
425 }
426 else if (isspace((unsigned char)*line)) {
427 *line++ = '\0';
428
429 while (isspace((unsigned char)*line))
430 line++;
431
432 args[++arg] = line;
433 continue;
434 }
435
436 line++;
437 }
438
439 while (++arg <= MAX_STATS_ARGS)
440 args[arg] = line;
441
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100442 /* unescape '\' */
William Lallemand74c24fb2016-11-21 17:18:36 +0100443 arg = 0;
444 while (*args[arg] != '\0') {
445 j = 0;
446 for (i=0; args[arg][i] != '\0'; i++) {
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100447 if (args[arg][i] == '\\') {
448 if (args[arg][i+1] == '\\')
449 i++;
450 else
451 continue;
452 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100453 args[arg][j] = args[arg][i];
454 j++;
455 }
456 args[arg][j] = '\0';
457 arg++;
458 }
459
Willy Tarreau41908562016-11-24 16:23:38 +0100460 appctx->st2 = 0;
Willy Tarreaua2d58722016-12-16 12:37:03 +0100461 memset(&appctx->ctx.cli, 0, sizeof(appctx->ctx.cli));
Willy Tarreau41908562016-11-24 16:23:38 +0100462
463 kw = cli_find_kw(args);
Willy Tarreaueaffde32016-12-16 17:59:25 +0100464 if (!kw)
Willy Tarreau41908562016-11-24 16:23:38 +0100465 return 0;
466
467 appctx->io_handler = kw->io_handler;
Emeric Brund6871f72017-06-29 19:54:13 +0200468 appctx->io_release = kw->io_release;
469 /* kw->parse could set its own io_handler or ip_release handler */
Willy Tarreaueaffde32016-12-16 17:59:25 +0100470 if ((!kw->parse || kw->parse(args, appctx, kw->private) == 0) && appctx->io_handler) {
Willy Tarreau41908562016-11-24 16:23:38 +0100471 appctx->st0 = CLI_ST_CALLBACK;
William Lallemand74c24fb2016-11-21 17:18:36 +0100472 }
Willy Tarreau41908562016-11-24 16:23:38 +0100473 return 1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100474}
475
476/* This I/O handler runs as an applet embedded in a stream interface. It is
477 * used to processes I/O from/to the stats unix socket. The system relies on a
478 * state machine handling requests and various responses. We read a request,
479 * then we process it and send the response, and we possibly display a prompt.
480 * Then we can read again. The state is stored in appctx->st0 and is one of the
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100481 * CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled
William Lallemand74c24fb2016-11-21 17:18:36 +0100482 * or not.
483 */
484static void cli_io_handler(struct appctx *appctx)
485{
486 struct stream_interface *si = appctx->owner;
487 struct channel *req = si_oc(si);
488 struct channel *res = si_ic(si);
489 int reql;
490 int len;
491
492 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
493 goto out;
494
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100495 /* Check if the input buffer is avalaible. */
496 if (res->buf->size == 0) {
497 si_applet_cant_put(si);
498 goto out;
499 }
500
William Lallemand74c24fb2016-11-21 17:18:36 +0100501 while (1) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100502 if (appctx->st0 == CLI_ST_INIT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100503 /* Stats output not initialized yet */
504 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100505 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100506 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100507 else if (appctx->st0 == CLI_ST_END) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100508 /* Let's close for real now. We just close the request
509 * side, the conditions below will complete if needed.
510 */
511 si_shutw(si);
512 break;
513 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100514 else if (appctx->st0 == CLI_ST_GETREQ) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100515 /* ensure we have some output room left in the event we
516 * would want to return some info right after parsing.
517 */
518 if (buffer_almost_full(si_ib(si))) {
519 si_applet_cant_put(si);
520 break;
521 }
522
523 reql = bo_getline(si_oc(si), trash.str, trash.size);
524 if (reql <= 0) { /* closed or EOL not found */
525 if (reql == 0)
526 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100527 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100528 continue;
529 }
530
531 /* seek for a possible unescaped semi-colon. If we find
532 * one, we replace it with an LF and skip only this part.
533 */
534 for (len = 0; len < reql; len++) {
535 if (trash.str[len] == '\\') {
536 len++;
537 continue;
538 }
539 if (trash.str[len] == ';') {
540 trash.str[len] = '\n';
541 reql = len + 1;
542 break;
543 }
544 }
545
546 /* now it is time to check that we have a full line,
547 * remove the trailing \n and possibly \r, then cut the
548 * line.
549 */
550 len = reql - 1;
551 if (trash.str[len] != '\n') {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100552 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100553 continue;
554 }
555
556 if (len && trash.str[len-1] == '\r')
557 len--;
558
559 trash.str[len] = '\0';
560
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100561 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100562 if (len) {
563 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100564 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100565 continue;
566 }
567 else if (strcmp(trash.str, "prompt") == 0)
568 appctx->st1 = !appctx->st1;
569 else if (strcmp(trash.str, "help") == 0 ||
Willy Tarreau41908562016-11-24 16:23:38 +0100570 !cli_parse_request(appctx, trash.str)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100571 cli_gen_usage_msg();
572 if (dynamic_usage_msg)
573 appctx->ctx.cli.msg = dynamic_usage_msg;
574 else
575 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100576 appctx->st0 = CLI_ST_PRINT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100577 }
578 /* NB: stats_sock_parse_request() may have put
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100579 * another CLI_ST_O_* into appctx->st0.
William Lallemand74c24fb2016-11-21 17:18:36 +0100580 */
581 }
582 else if (!appctx->st1) {
583 /* if prompt is disabled, print help on empty lines,
584 * so that the user at least knows how to enable
585 * prompt and find help.
586 */
587 cli_gen_usage_msg();
588 if (dynamic_usage_msg)
589 appctx->ctx.cli.msg = dynamic_usage_msg;
590 else
591 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100592 appctx->st0 = CLI_ST_PRINT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100593 }
594
595 /* re-adjust req buffer */
596 bo_skip(si_oc(si), reql);
597 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
598 }
599 else { /* output functions */
600 switch (appctx->st0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100601 case CLI_ST_PROMPT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100602 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100603 case CLI_ST_PRINT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100604 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100605 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100606 else
607 si_applet_cant_put(si);
608 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100609 case CLI_ST_PRINT_FREE:
William Lallemand74c24fb2016-11-21 17:18:36 +0100610 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
611 free(appctx->ctx.cli.err);
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100612 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100613 }
614 else
615 si_applet_cant_put(si);
616 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100617 case CLI_ST_CALLBACK: /* use custom pointer */
William Lallemand74c24fb2016-11-21 17:18:36 +0100618 if (appctx->io_handler)
619 if (appctx->io_handler(appctx)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100620 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100621 if (appctx->io_release) {
622 appctx->io_release(appctx);
623 appctx->io_release = NULL;
624 }
625 }
626 break;
627 default: /* abnormal state */
628 si->flags |= SI_FL_ERR;
629 break;
630 }
631
632 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100633 if (appctx->st0 == CLI_ST_PROMPT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100634 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100635 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100636 else
637 si_applet_cant_put(si);
638 }
639
640 /* If the output functions are still there, it means they require more room. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100641 if (appctx->st0 >= CLI_ST_OUTPUT)
William Lallemand74c24fb2016-11-21 17:18:36 +0100642 break;
643
644 /* Now we close the output if one of the writers did so,
645 * or if we're not in interactive mode and the request
646 * buffer is empty. This still allows pipelined requests
647 * to be sent in non-interactive mode.
648 */
649 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100650 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100651 continue;
652 }
653
654 /* switch state back to GETREQ to read next requests */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100655 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100656 }
657 }
658
659 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
660 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
661 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
662 /* Other side has closed, let's abort if we have no more processing to do
663 * and nothing more to consume. This is comparable to a broken pipe, so
664 * we forward the close to the request side so that it flows upstream to
665 * the client.
666 */
667 si_shutw(si);
668 }
669
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100670 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < CLI_ST_OUTPUT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100671 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
672 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
673 /* We have no more processing to do, and nothing more to send, and
674 * the client side has closed. So we'll forward this state downstream
675 * on the response buffer.
676 */
677 si_shutr(si);
678 res->flags |= CF_READ_NULL;
679 }
680
681 out:
682 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
683 __FUNCTION__, __LINE__,
684 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
685}
686
William Lallemand74c24fb2016-11-21 17:18:36 +0100687/* This is called when the stream interface is closed. For instance, upon an
688 * external abort, we won't call the i/o handler anymore so we may need to
689 * remove back references to the stream currently being dumped.
690 */
691static void cli_release_handler(struct appctx *appctx)
692{
693 if (appctx->io_release) {
694 appctx->io_release(appctx);
695 appctx->io_release = NULL;
696 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100697 else if (appctx->st0 == CLI_ST_PRINT_FREE) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100698 free(appctx->ctx.cli.err);
699 appctx->ctx.cli.err = NULL;
700 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100701}
702
703/* This function dumps all environmnent variables to the buffer. It returns 0
704 * if the output buffer is full and it needs to be called again, otherwise
Willy Tarreauf6710f82016-12-16 17:45:44 +0100705 * non-zero. Dumps only one entry if st2 == STAT_ST_END. It uses cli.p0 as the
706 * pointer to the current variable.
William Lallemand74c24fb2016-11-21 17:18:36 +0100707 */
Willy Tarreau0a739292016-11-22 20:21:23 +0100708static int cli_io_handler_show_env(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100709{
Willy Tarreau0a739292016-11-22 20:21:23 +0100710 struct stream_interface *si = appctx->owner;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100711 char **var = appctx->ctx.cli.p0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100712
713 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
714 return 1;
715
716 chunk_reset(&trash);
717
718 /* we have two inner loops here, one for the proxy, the other one for
719 * the buffer.
720 */
Willy Tarreauf6710f82016-12-16 17:45:44 +0100721 while (*var) {
722 chunk_printf(&trash, "%s\n", *var);
William Lallemand74c24fb2016-11-21 17:18:36 +0100723
724 if (bi_putchk(si_ic(si), &trash) == -1) {
725 si_applet_cant_put(si);
726 return 0;
727 }
728 if (appctx->st2 == STAT_ST_END)
729 break;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100730 var++;
731 appctx->ctx.cli.p0 = var;
William Lallemand74c24fb2016-11-21 17:18:36 +0100732 }
733
734 /* dump complete */
735 return 1;
736}
737
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200738/* This function dumps all file descriptors states (or the requested one) to
739 * the buffer. It returns 0 if the output buffer is full and it needs to be
740 * called again, otherwise non-zero. Dumps only one entry if st2 == STAT_ST_END.
741 * It uses cli.i0 as the fd number to restart from.
742 */
743static int cli_io_handler_show_fd(struct appctx *appctx)
744{
745 struct stream_interface *si = appctx->owner;
746 int fd = appctx->ctx.cli.i0;
747
748 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
749 return 1;
750
751 chunk_reset(&trash);
752
753 /* we have two inner loops here, one for the proxy, the other one for
754 * the buffer.
755 */
756 while (fd < maxfd) {
757 struct fdtab fdt;
758 struct listener *li;
759 struct server *sv;
760 struct proxy *px;
761 uint32_t conn_flags;
762
763 fdt = fdtab[fd];
764
765 if (fdt.iocb == conn_fd_handler) {
766 conn_flags = ((struct connection *)fdt.owner)->flags;
767 li = objt_listener(((struct connection *)fdt.owner)->target);
768 sv = objt_server(((struct connection *)fdt.owner)->target);
769 px = objt_proxy(((struct connection *)fdt.owner)->target);
770 }
771 else if (fdt.iocb == listener_accept)
772 li = fdt.owner;
773
774 if (!fdt.owner)
775 goto skip; // closed
776
777 chunk_printf(&trash,
778 " %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)",
779 fd,
780 fdt.state,
781 (fdt.state & FD_EV_POLLED_R) ? 'P' : 'p',
782 (fdt.state & FD_EV_READY_R) ? 'R' : 'r',
783 (fdt.state & FD_EV_ACTIVE_R) ? 'A' : 'a',
784 (fdt.state & FD_EV_POLLED_W) ? 'P' : 'p',
785 (fdt.state & FD_EV_READY_W) ? 'R' : 'r',
786 (fdt.state & FD_EV_ACTIVE_W) ? 'A' : 'a',
787 fdt.ev,
788 (fdt.ev & FD_POLL_HUP) ? 'H' : 'h',
789 (fdt.ev & FD_POLL_ERR) ? 'E' : 'e',
790 (fdt.ev & FD_POLL_OUT) ? 'O' : 'o',
791 (fdt.ev & FD_POLL_PRI) ? 'P' : 'p',
792 (fdt.ev & FD_POLL_IN) ? 'I' : 'i',
793 fdt.new ? 'N' : 'n',
794 fdt.updated ? 'U' : 'u',
795 fdt.linger_risk ? 'L' : 'l',
796 fdt.cloned ? 'C' : 'c',
797 fdt.cache,
798 fdt.owner,
799 fdt.iocb,
800 (fdt.iocb == conn_fd_handler) ? "conn_fd_handler" :
801 (fdt.iocb == dgram_fd_handler) ? "dgram_fd_handler" :
802 (fdt.iocb == listener_accept) ? "listener_accept" :
803 "unknown");
804
805 if (fdt.iocb == conn_fd_handler) {
806 chunk_appendf(&trash, " cflg=0x%08x", conn_flags);
807 if (px)
808 chunk_appendf(&trash, " px=%s", px->id);
809 else if (sv)
810 chunk_appendf(&trash, " sv=%s/%s", sv->id, sv->proxy->id);
811 else if (li)
812 chunk_appendf(&trash, " fe=%s", li->bind_conf->frontend->id);
813 }
814 else if (fdt.iocb == listener_accept) {
815 chunk_appendf(&trash, " l.st=%s fe=%s",
816 listener_state_str(li),
817 li->bind_conf->frontend->id);
818 }
819
820 chunk_appendf(&trash, "\n");
821
822 if (bi_putchk(si_ic(si), &trash) == -1) {
823 si_applet_cant_put(si);
824 return 0;
825 }
826 skip:
827 if (appctx->st2 == STAT_ST_END)
828 break;
829
830 fd++;
831 appctx->ctx.cli.i0 = fd;
832 }
833
834 /* dump complete */
835 return 1;
836}
837
William Lallemandeceddf72016-12-15 18:06:44 +0100838/*
Willy Tarreau3af9d832016-12-16 12:58:09 +0100839 * CLI IO handler for `show cli sockets`.
840 * Uses ctx.cli.p0 to store the restart pointer.
William Lallemandeceddf72016-12-15 18:06:44 +0100841 */
842static int cli_io_handler_show_cli_sock(struct appctx *appctx)
843{
844 struct bind_conf *bind_conf;
845 struct stream_interface *si = appctx->owner;
846
847 chunk_reset(&trash);
848
849 switch (appctx->st2) {
850 case STAT_ST_INIT:
851 chunk_printf(&trash, "# socket lvl processes\n");
852 if (bi_putchk(si_ic(si), &trash) == -1) {
853 si_applet_cant_put(si);
854 return 0;
855 }
William Lallemandeceddf72016-12-15 18:06:44 +0100856 appctx->st2 = STAT_ST_LIST;
857
858 case STAT_ST_LIST:
859 if (global.stats_fe) {
860 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
861 struct listener *l;
862
863 /*
Willy Tarreau3af9d832016-12-16 12:58:09 +0100864 * get the latest dumped node in appctx->ctx.cli.p0
William Lallemandeceddf72016-12-15 18:06:44 +0100865 * if the current node is the first of the list
866 */
867
Willy Tarreau3af9d832016-12-16 12:58:09 +0100868 if (appctx->ctx.cli.p0 &&
869 &bind_conf->by_fe == (&global.stats_fe->conf.bind)->n) {
William Lallemandeceddf72016-12-15 18:06:44 +0100870 /* change the current node to the latest dumped and continue the loop */
Willy Tarreau3af9d832016-12-16 12:58:09 +0100871 bind_conf = LIST_ELEM(appctx->ctx.cli.p0, typeof(bind_conf), by_fe);
William Lallemandeceddf72016-12-15 18:06:44 +0100872 continue;
873 }
874
875 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
876
877 char addr[46];
878 char port[6];
879
880 if (l->addr.ss_family == AF_UNIX) {
881 const struct sockaddr_un *un;
882
883 un = (struct sockaddr_un *)&l->addr;
884 chunk_appendf(&trash, "%s ", un->sun_path);
885 } else if (l->addr.ss_family == AF_INET) {
886 addr_to_str(&l->addr, addr, sizeof(addr));
887 port_to_str(&l->addr, port, sizeof(port));
888 chunk_appendf(&trash, "%s:%s ", addr, port);
889 } else if (l->addr.ss_family == AF_INET6) {
890 addr_to_str(&l->addr, addr, sizeof(addr));
891 port_to_str(&l->addr, port, sizeof(port));
892 chunk_appendf(&trash, "[%s]:%s ", addr, port);
893 } else
894 continue;
895
William Lallemand07a62f72017-05-24 00:57:40 +0200896 if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN)
William Lallemandeceddf72016-12-15 18:06:44 +0100897 chunk_appendf(&trash, "admin ");
William Lallemand07a62f72017-05-24 00:57:40 +0200898 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER)
899 chunk_appendf(&trash, "operator ");
900 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_USER)
901 chunk_appendf(&trash, "user ");
William Lallemandeceddf72016-12-15 18:06:44 +0100902 else
903 chunk_appendf(&trash, " ");
904
905 if (bind_conf->bind_proc != 0) {
906 int pos;
Willy Tarreau20c5e522016-12-16 12:50:55 +0100907 for (pos = 0; pos < 8 * sizeof(bind_conf->bind_proc); pos++) {
Willy Tarreau4305ac72016-12-16 12:56:31 +0100908 if (bind_conf->bind_proc & (1UL << pos)) {
William Lallemandeceddf72016-12-15 18:06:44 +0100909 chunk_appendf(&trash, "%d,", pos+1);
910 }
911 }
912 /* replace the latest comma by a newline */
913 trash.str[trash.len-1] = '\n';
914
915 } else {
916 chunk_appendf(&trash, "all\n");
917 }
918
919 if (bi_putchk(si_ic(si), &trash) == -1) {
920 si_applet_cant_put(si);
921 return 0;
922 }
923 }
Willy Tarreau3af9d832016-12-16 12:58:09 +0100924 appctx->ctx.cli.p0 = &bind_conf->by_fe; /* store the latest list node dumped */
William Lallemandeceddf72016-12-15 18:06:44 +0100925 }
926 }
927 default:
928 appctx->st2 = STAT_ST_FIN;
929 return 1;
930 }
931}
932
933
Willy Tarreau0a739292016-11-22 20:21:23 +0100934/* parse a "show env" CLI request. Returns 0 if it needs to continue, 1 if it
Willy Tarreauf6710f82016-12-16 17:45:44 +0100935 * wants to stop here. It puts the variable to be dumped into cli.p0 if a single
936 * variable is requested otherwise puts environ there.
Willy Tarreau0a739292016-11-22 20:21:23 +0100937 */
938static int cli_parse_show_env(char **args, struct appctx *appctx, void *private)
939{
940 extern char **environ;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100941 char **var;
Willy Tarreau0a739292016-11-22 20:21:23 +0100942
943 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
944 return 1;
945
Willy Tarreauf6710f82016-12-16 17:45:44 +0100946 var = environ;
Willy Tarreau0a739292016-11-22 20:21:23 +0100947
948 if (*args[2]) {
949 int len = strlen(args[2]);
950
Willy Tarreauf6710f82016-12-16 17:45:44 +0100951 for (; *var; var++) {
952 if (strncmp(*var, args[2], len) == 0 &&
953 (*var)[len] == '=')
Willy Tarreau0a739292016-11-22 20:21:23 +0100954 break;
955 }
Willy Tarreauf6710f82016-12-16 17:45:44 +0100956 if (!*var) {
Willy Tarreau0a739292016-11-22 20:21:23 +0100957 appctx->ctx.cli.msg = "Variable not found\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100958 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau0a739292016-11-22 20:21:23 +0100959 return 1;
960 }
961 appctx->st2 = STAT_ST_END;
962 }
Willy Tarreauf6710f82016-12-16 17:45:44 +0100963 appctx->ctx.cli.p0 = var;
Willy Tarreau0a739292016-11-22 20:21:23 +0100964 return 0;
965}
966
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200967/* parse a "show fd" CLI request. Returns 0 if it needs to continue, 1 if it
968 * wants to stop here. It puts the FD number into cli.i0 if a specific FD is
969 * requested and sets st2 to STAT_ST_END, otherwise leaves 0 in i0.
970 */
971static int cli_parse_show_fd(char **args, struct appctx *appctx, void *private)
972{
973 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
974 return 1;
975
976 appctx->ctx.cli.i0 = 0;
977
978 if (*args[2]) {
979 appctx->ctx.cli.i0 = atoi(args[2]);
980 appctx->st2 = STAT_ST_END;
981 }
982 return 0;
983}
984
Willy Tarreau599852e2016-11-22 20:33:32 +0100985/* parse a "set timeout" CLI request. It always returns 1. */
986static int cli_parse_set_timeout(char **args, struct appctx *appctx, void *private)
987{
988 struct stream_interface *si = appctx->owner;
989 struct stream *s = si_strm(si);
990
991 if (strcmp(args[2], "cli") == 0) {
992 unsigned timeout;
993 const char *res;
994
995 if (!*args[3]) {
996 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100997 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +0100998 return 1;
999 }
1000
1001 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1002 if (res || timeout < 1) {
1003 appctx->ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001004 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001005 return 1;
1006 }
1007
1008 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
1009 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
1010 return 1;
1011 }
1012 else {
1013 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001014 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001015 return 1;
1016 }
1017}
1018
Willy Tarreau2af99412016-11-23 11:10:59 +01001019/* parse a "set maxconn global" command. It always returns 1. */
1020static int cli_parse_set_maxconn_global(char **args, struct appctx *appctx, void *private)
1021{
1022 int v;
1023
1024 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1025 return 1;
1026
1027 if (!*args[3]) {
1028 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001029 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +01001030 return 1;
1031 }
1032
1033 v = atoi(args[3]);
1034 if (v > global.hardmaxconn) {
1035 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001036 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +01001037 return 1;
1038 }
1039
1040 /* check for unlimited values */
1041 if (v <= 0)
1042 v = global.hardmaxconn;
1043
1044 global.maxconn = v;
1045
1046 /* Dequeues all of the listeners waiting for a resource */
1047 if (!LIST_ISEMPTY(&global_listener_queue))
1048 dequeue_all_listeners(&global_listener_queue);
1049
1050 return 1;
1051}
1052
William Lallemandeceddf72016-12-15 18:06:44 +01001053
1054int cli_parse_default(char **args, struct appctx *appctx, void *private)
1055{
1056 return 0;
1057}
1058
Willy Tarreau45c742b2016-11-24 14:51:17 +01001059/* parse a "set rate-limit" command. It always returns 1. */
1060static int cli_parse_set_ratelimit(char **args, struct appctx *appctx, void *private)
1061{
1062 int v;
1063 int *res;
1064 int mul = 1;
1065
1066 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1067 return 1;
1068
1069 if (strcmp(args[2], "connections") == 0 && strcmp(args[3], "global") == 0)
1070 res = &global.cps_lim;
1071 else if (strcmp(args[2], "sessions") == 0 && strcmp(args[3], "global") == 0)
1072 res = &global.sps_lim;
1073#ifdef USE_OPENSSL
1074 else if (strcmp(args[2], "ssl-sessions") == 0 && strcmp(args[3], "global") == 0)
1075 res = &global.ssl_lim;
1076#endif
1077 else if (strcmp(args[2], "http-compression") == 0 && strcmp(args[3], "global") == 0) {
1078 res = &global.comp_rate_lim;
1079 mul = 1024;
1080 }
1081 else {
1082 appctx->ctx.cli.msg =
1083 "'set rate-limit' only supports :\n"
1084 " - 'connections global' to set the per-process maximum connection rate\n"
1085 " - 'sessions global' to set the per-process maximum session rate\n"
1086#ifdef USE_OPENSSL
1087 " - 'ssl-session global' to set the per-process maximum SSL session rate\n"
1088#endif
1089 " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001090 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001091 return 1;
1092 }
1093
1094 if (!*args[4]) {
1095 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001096 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001097 return 1;
1098 }
1099
1100 v = atoi(args[4]);
1101 if (v < 0) {
1102 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001103 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001104 return 1;
1105 }
1106
1107 *res = v * mul;
1108
1109 /* Dequeues all of the listeners waiting for a resource */
1110 if (!LIST_ISEMPTY(&global_listener_queue))
1111 dequeue_all_listeners(&global_listener_queue);
1112
1113 return 1;
1114}
1115
William Lallemandf6975e92017-05-26 17:42:10 +02001116/* parse the "expose-fd" argument on the bind lines */
1117static int bind_parse_expose_fd(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1118{
1119 if (!*args[cur_arg + 1]) {
1120 memprintf(err, "'%s' : missing fd type", args[cur_arg]);
1121 return ERR_ALERT | ERR_FATAL;
1122 }
1123 if (!strcmp(args[cur_arg+1], "listeners")) {
1124 conf->level |= ACCESS_FD_LISTENERS;
1125 } else {
1126 memprintf(err, "'%s' only supports 'listeners' (got '%s')",
1127 args[cur_arg], args[cur_arg+1]);
1128 return ERR_ALERT | ERR_FATAL;
1129 }
1130
1131 return 0;
1132}
1133
William Lallemand74c24fb2016-11-21 17:18:36 +01001134/* parse the "level" argument on the bind lines */
1135static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1136{
1137 if (!*args[cur_arg + 1]) {
1138 memprintf(err, "'%s' : missing level", args[cur_arg]);
1139 return ERR_ALERT | ERR_FATAL;
1140 }
1141
William Lallemand07a62f72017-05-24 00:57:40 +02001142 if (!strcmp(args[cur_arg+1], "user")) {
1143 conf->level &= ~ACCESS_LVL_MASK;
1144 conf->level |= ACCESS_LVL_USER;
1145 } else if (!strcmp(args[cur_arg+1], "operator")) {
1146 conf->level &= ~ACCESS_LVL_MASK;
1147 conf->level |= ACCESS_LVL_OPER;
1148 } else if (!strcmp(args[cur_arg+1], "admin")) {
1149 conf->level &= ~ACCESS_LVL_MASK;
1150 conf->level |= ACCESS_LVL_ADMIN;
1151 } else {
William Lallemand74c24fb2016-11-21 17:18:36 +01001152 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
1153 args[cur_arg], args[cur_arg+1]);
1154 return ERR_ALERT | ERR_FATAL;
1155 }
1156
1157 return 0;
1158}
1159
Olivier Houchardf886e342017-04-05 22:24:59 +02001160/* Send all the bound sockets, always returns 1 */
1161static int _getsocks(char **args, struct appctx *appctx, void *private)
1162{
1163 char *cmsgbuf = NULL;
1164 unsigned char *tmpbuf = NULL;
1165 struct cmsghdr *cmsg;
1166 struct stream_interface *si = appctx->owner;
William Lallemandf6975e92017-05-26 17:42:10 +02001167 struct stream *s = si_strm(si);
Olivier Houchardf886e342017-04-05 22:24:59 +02001168 struct connection *remote = objt_conn(si_opposite(si)->end);
1169 struct msghdr msghdr;
1170 struct iovec iov;
Olivier Houchard54740872017-04-06 14:45:14 +02001171 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf886e342017-04-05 22:24:59 +02001172 int *tmpfd;
1173 int tot_fd_nb = 0;
1174 struct proxy *px;
1175 int i = 0;
1176 int fd = remote->t.sock.fd;
1177 int curoff = 0;
1178 int old_fcntl;
1179 int ret;
1180
1181 /* Temporary set the FD in blocking mode, that will make our life easier */
1182 old_fcntl = fcntl(fd, F_GETFL);
1183 if (old_fcntl < 0) {
1184 Warning("Couldn't get the flags for the unix socket\n");
1185 goto out;
1186 }
1187 cmsgbuf = malloc(CMSG_SPACE(sizeof(int) * MAX_SEND_FD));
1188 if (!cmsgbuf) {
1189 Warning("Failed to allocate memory to send sockets\n");
1190 goto out;
1191 }
1192 if (fcntl(fd, F_SETFL, old_fcntl &~ O_NONBLOCK) == -1) {
1193 Warning("Cannot make the unix socket blocking\n");
1194 goto out;
1195 }
Olivier Houchard54740872017-04-06 14:45:14 +02001196 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf886e342017-04-05 22:24:59 +02001197 iov.iov_base = &tot_fd_nb;
1198 iov.iov_len = sizeof(tot_fd_nb);
William Lallemandf6975e92017-05-26 17:42:10 +02001199 if (!(strm_li(s)->bind_conf->level & ACCESS_FD_LISTENERS))
Olivier Houchardf886e342017-04-05 22:24:59 +02001200 goto out;
1201 memset(&msghdr, 0, sizeof(msghdr));
1202 /*
1203 * First, calculates the total number of FD, so that we can let
1204 * the caller know how much he should expects.
1205 */
1206 px = proxy;
1207 while (px) {
1208 struct listener *l;
1209
1210 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02001211 /* Only transfer IPv4/IPv6/UNIX sockets */
1212 if (l->state >= LI_ZOMBIE &&
1213 (l->proto->sock_family == AF_INET ||
Olivier Houchardf886e342017-04-05 22:24:59 +02001214 l->proto->sock_family == AF_INET6 ||
Olivier Houchard1fc05162017-04-06 01:05:05 +02001215 l->proto->sock_family == AF_UNIX))
Olivier Houchardf886e342017-04-05 22:24:59 +02001216 tot_fd_nb++;
1217 }
1218 px = px->next;
1219 }
1220 if (tot_fd_nb == 0)
1221 goto out;
1222
1223 /* First send the total number of file descriptors, so that the
1224 * receiving end knows what to expect.
1225 */
1226 msghdr.msg_iov = &iov;
1227 msghdr.msg_iovlen = 1;
1228 ret = sendmsg(fd, &msghdr, 0);
1229 if (ret != sizeof(tot_fd_nb)) {
1230 Warning("Failed to send the number of sockets to send\n");
1231 goto out;
1232 }
1233
1234 /* Now send the fds */
1235 msghdr.msg_control = cmsgbuf;
1236 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * MAX_SEND_FD);
1237 cmsg = CMSG_FIRSTHDR(&msghdr);
1238 cmsg->cmsg_len = CMSG_LEN(MAX_SEND_FD * sizeof(int));
1239 cmsg->cmsg_level = SOL_SOCKET;
1240 cmsg->cmsg_type = SCM_RIGHTS;
1241 tmpfd = (int *)CMSG_DATA(cmsg);
1242
1243 px = proxy;
1244 /* For each socket, e message is sent, containing the following :
1245 * Size of the namespace name (or 0 if none), as an unsigned char.
1246 * The namespace name, if any
1247 * Size of the interface name (or 0 if none), as an unsigned char
1248 * The interface name, if any
1249 * Listener options, as an int.
1250 */
1251 /* We will send sockets MAX_SEND_FD per MAX_SEND_FD, allocate a
1252 * buffer big enough to store the socket informations.
1253 */
1254 tmpbuf = malloc(MAX_SEND_FD * (1 + NAME_MAX + 1 + IFNAMSIZ + sizeof(int)));
1255 if (tmpbuf == NULL) {
1256 Warning("Failed to allocate memory to transfer socket informations\n");
1257 goto out;
1258 }
1259 iov.iov_base = tmpbuf;
1260 while (px) {
1261 struct listener *l;
1262
1263 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1264 int ret;
1265 /* Only transfer IPv4/IPv6 sockets */
Olivier Houchard1fc05162017-04-06 01:05:05 +02001266 if (l->state >= LI_ZOMBIE &&
Olivier Houchardf886e342017-04-05 22:24:59 +02001267 (l->proto->sock_family == AF_INET ||
1268 l->proto->sock_family == AF_INET6 ||
1269 l->proto->sock_family == AF_UNIX)) {
1270 memcpy(&tmpfd[i % MAX_SEND_FD], &l->fd, sizeof(l->fd));
1271 if (!l->netns)
1272 tmpbuf[curoff++] = 0;
1273#ifdef CONFIG_HAP_NS
1274 else {
1275 char *name = l->netns->node.key;
1276 unsigned char len = l->netns->name_len;
1277 tmpbuf[curoff++] = len;
1278 memcpy(tmpbuf + curoff, name, len);
1279 curoff += len;
1280 }
1281#endif
1282 if (l->interface) {
1283 unsigned char len = strlen(l->interface);
1284 tmpbuf[curoff++] = len;
1285 memcpy(tmpbuf + curoff, l->interface, len);
1286 curoff += len;
1287 } else
1288 tmpbuf[curoff++] = 0;
1289 memcpy(tmpbuf + curoff, &l->options,
1290 sizeof(l->options));
1291 curoff += sizeof(l->options);
1292
1293
1294 i++;
1295 } else
1296 continue;
1297 if ((!(i % MAX_SEND_FD))) {
1298 iov.iov_len = curoff;
1299 if (sendmsg(fd, &msghdr, 0) != curoff) {
1300 Warning("Failed to transfer sockets\n");
1301 printf("errno %d\n", errno);
1302 goto out;
1303 }
1304 /* Wait for an ack */
1305 do {
1306 ret = recv(fd, &tot_fd_nb,
1307 sizeof(tot_fd_nb), 0);
1308 } while (ret == -1 && errno == EINTR);
1309 if (ret <= 0) {
1310 Warning("Unexpected error while transferring sockets\n");
1311 goto out;
1312 }
1313 curoff = 0;
1314 }
1315
1316 }
1317 px = px->next;
1318 }
1319 if (i % MAX_SEND_FD) {
1320 iov.iov_len = curoff;
1321 cmsg->cmsg_len = CMSG_LEN((i % MAX_SEND_FD) * sizeof(int));
1322 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * (i % MAX_SEND_FD));
1323 if (sendmsg(fd, &msghdr, 0) != curoff) {
1324 Warning("Failed to transfer sockets\n");
1325 goto out;
1326 }
1327 }
1328
1329out:
1330 if (old_fcntl >= 0 && fcntl(fd, F_SETFL, old_fcntl) == -1) {
1331 Warning("Cannot make the unix socket non-blocking\n");
1332 goto out;
1333 }
1334 appctx->st0 = CLI_ST_END;
1335 free(cmsgbuf);
1336 free(tmpbuf);
1337 return 1;
1338}
1339
1340
1341
William Lallemand74c24fb2016-11-21 17:18:36 +01001342static struct applet cli_applet = {
1343 .obj_type = OBJ_TYPE_APPLET,
1344 .name = "<CLI>", /* used for logging */
1345 .fct = cli_io_handler,
1346 .release = cli_release_handler,
1347};
1348
Willy Tarreau0a739292016-11-22 20:21:23 +01001349/* register cli keywords */
1350static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau2af99412016-11-23 11:10:59 +01001351 { { "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 +01001352 { { "set", "rate-limit", NULL }, "set rate-limit : change a rate limiting value", cli_parse_set_ratelimit, NULL },
Willy Tarreau599852e2016-11-22 20:33:32 +01001353 { { "set", "timeout", NULL }, "set timeout : change a timeout setting", cli_parse_set_timeout, NULL, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001354 { { "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 +01001355 { { "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 +02001356 { { "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 +02001357 { { "_getsocks", NULL }, NULL, _getsocks, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001358 {{},}
1359}};
1360
William Lallemand74c24fb2016-11-21 17:18:36 +01001361static struct cfg_kw_list cfg_kws = {ILH, {
1362 { CFG_GLOBAL, "stats", stats_parse_global },
1363 { 0, NULL, NULL },
1364}};
1365
1366static struct bind_kw_list bind_kws = { "STAT", { }, {
William Lallemandf6975e92017-05-26 17:42:10 +02001367 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
1368 { "expose-fd", bind_parse_expose_fd, 1 }, /* set the unix socket expose fd rights */
William Lallemand74c24fb2016-11-21 17:18:36 +01001369 { NULL, NULL, 0 },
1370}};
1371
1372__attribute__((constructor))
1373static void __dumpstats_module_init(void)
1374{
1375 cfg_register_keywords(&cfg_kws);
Willy Tarreau0a739292016-11-22 20:21:23 +01001376 cli_register_kw(&cli_kws);
William Lallemand74c24fb2016-11-21 17:18:36 +01001377 bind_register_keywords(&bind_kws);
1378}
1379
1380/*
1381 * Local variables:
1382 * c-indent-level: 8
1383 * c-basic-offset: 8
1384 * End:
1385 */