blob: 8a4fbc52ca348c6be0cc8cc4fceea083375cea74 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Olivier Houchardf886e342017-04-05 22:24:59 +020027#include <net/if.h>
28
William Lallemand74c24fb2016-11-21 17:18:36 +010029#include <common/cfgparse.h>
30#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
36#include <common/ticks.h>
37#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
40#include <common/base64.h>
41
42#include <types/applet.h>
William Lallemand9ed62032016-11-21 17:49:11 +010043#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010044#include <types/global.h>
45#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010046#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010047
48#include <proto/backend.h>
49#include <proto/channel.h>
50#include <proto/checks.h>
51#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010052#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010053#include <proto/fd.h>
54#include <proto/freq_ctr.h>
55#include <proto/frontend.h>
56#include <proto/log.h>
57#include <proto/pattern.h>
58#include <proto/pipe.h>
William Lallemandce83b4a2018-10-26 14:47:30 +020059#include <proto/protocol.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010060#include <proto/listener.h>
61#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010062#include <proto/proxy.h>
63#include <proto/sample.h>
64#include <proto/session.h>
65#include <proto/stream.h>
66#include <proto/server.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010067#include <proto/stream_interface.h>
68#include <proto/task.h>
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +020069#include <proto/proto_udp.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010070
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020071#define PAYLOAD_PATTERN "<<"
72
William Lallemand74c24fb2016-11-21 17:18:36 +010073static struct applet cli_applet;
74
75static const char stats_sock_usage_msg[] =
76 "Unknown command. Please enter one of the following commands only :\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010077 " help : this message\n"
78 " prompt : toggle interactive mode with prompt\n"
79 " quit : disconnect\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010080 "";
81
82static const char stats_permission_denied_msg[] =
83 "Permission denied\n"
84 "";
85
86
Christopher Faulet1bc04c72017-10-29 20:14:08 +010087static THREAD_LOCAL char *dynamic_usage_msg = NULL;
William Lallemand74c24fb2016-11-21 17:18:36 +010088
89/* List head of cli keywords */
90static struct cli_kw_list cli_keywords = {
91 .list = LIST_HEAD_INIT(cli_keywords.list)
92};
93
94extern const char *stat_status_codes[];
95
William Lallemand8a022572018-10-26 14:47:35 +020096static struct proxy *mworker_proxy; /* CLI proxy of the master */
97
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020098static char *cli_gen_usage_msg(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +010099{
100 struct cli_kw_list *kw_list;
101 struct cli_kw *kw;
Willy Tarreau83061a82018-07-13 11:56:34 +0200102 struct buffer *tmp = get_trash_chunk();
103 struct buffer out;
William Lallemand74c24fb2016-11-21 17:18:36 +0100104
105 free(dynamic_usage_msg);
106 dynamic_usage_msg = NULL;
107
108 if (LIST_ISEMPTY(&cli_keywords.list))
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200109 goto end;
William Lallemand74c24fb2016-11-21 17:18:36 +0100110
111 chunk_reset(tmp);
112 chunk_strcat(tmp, stats_sock_usage_msg);
113 list_for_each_entry(kw_list, &cli_keywords.list, list) {
114 kw = &kw_list->kw[0];
William Lallemand0154edc2018-05-15 11:50:04 +0200115 while (kw->str_kw[0]) {
116 if (kw->usage)
117 chunk_appendf(tmp, " %s\n", kw->usage);
William Lallemand74c24fb2016-11-21 17:18:36 +0100118 kw++;
119 }
120 }
121 chunk_init(&out, NULL, 0);
122 chunk_dup(&out, tmp);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200123 dynamic_usage_msg = out.area;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200124
125end:
126 if (dynamic_usage_msg) {
127 appctx->ctx.cli.severity = LOG_INFO;
128 appctx->ctx.cli.msg = dynamic_usage_msg;
129 }
130 else {
131 appctx->ctx.cli.severity = LOG_INFO;
132 appctx->ctx.cli.msg = stats_sock_usage_msg;
133 }
134 appctx->st0 = CLI_ST_PRINT;
135
William Lallemand74c24fb2016-11-21 17:18:36 +0100136 return dynamic_usage_msg;
137}
138
139struct cli_kw* cli_find_kw(char **args)
140{
141 struct cli_kw_list *kw_list;
142 struct cli_kw *kw;/* current cli_kw */
143 char **tmp_args;
144 const char **tmp_str_kw;
145 int found = 0;
146
147 if (LIST_ISEMPTY(&cli_keywords.list))
148 return NULL;
149
150 list_for_each_entry(kw_list, &cli_keywords.list, list) {
151 kw = &kw_list->kw[0];
152 while (*kw->str_kw) {
153 tmp_args = args;
154 tmp_str_kw = kw->str_kw;
155 while (*tmp_str_kw) {
156 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
157 found = 1;
158 } else {
159 found = 0;
160 break;
161 }
162 tmp_args++;
163 tmp_str_kw++;
164 }
165 if (found)
166 return (kw);
167 kw++;
168 }
169 }
170 return NULL;
171}
172
173void cli_register_kw(struct cli_kw_list *kw_list)
174{
175 LIST_ADDQ(&cli_keywords.list, &kw_list->list);
176}
177
178
179/* allocate a new stats frontend named <name>, and return it
180 * (or NULL in case of lack of memory).
181 */
182static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
183{
184 struct proxy *fe;
185
186 fe = calloc(1, sizeof(*fe));
187 if (!fe)
188 return NULL;
189
190 init_new_proxy(fe);
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100191 fe->next = proxies_list;
192 proxies_list = fe;
William Lallemand74c24fb2016-11-21 17:18:36 +0100193 fe->last_change = now.tv_sec;
194 fe->id = strdup("GLOBAL");
195 fe->cap = PR_CAP_FE;
196 fe->maxconn = 10; /* default to 10 concurrent connections */
197 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
198 fe->conf.file = strdup(file);
199 fe->conf.line = line;
200 fe->accept = frontend_accept;
201 fe->default_target = &cli_applet.obj_type;
202
203 /* the stats frontend is the only one able to assign ID #0 */
204 fe->conf.id.key = fe->uuid = 0;
205 eb32_insert(&used_proxy_id, &fe->conf.id);
206 return fe;
207}
208
209/* This function parses a "stats" statement in the "global" section. It returns
210 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
211 * error message into the <err> buffer which will be preallocated. The trailing
212 * '\n' must not be written. The function must be called with <args> pointing to
213 * the first word after "stats".
214 */
215static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
216 struct proxy *defpx, const char *file, int line,
217 char **err)
218{
219 struct bind_conf *bind_conf;
220 struct listener *l;
221
222 if (!strcmp(args[1], "socket")) {
223 int cur_arg;
224
225 if (*args[2] == 0) {
226 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
227 return -1;
228 }
229
230 if (!global.stats_fe) {
231 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
232 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
233 return -1;
234 }
235 }
236
Willy Tarreaua261e9b2016-12-22 20:44:00 +0100237 bind_conf = bind_conf_alloc(global.stats_fe, file, line, args[2], xprt_get(XPRT_RAW));
William Lallemand07a62f72017-05-24 00:57:40 +0200238 bind_conf->level &= ~ACCESS_LVL_MASK;
239 bind_conf->level |= ACCESS_LVL_OPER; /* default access level */
William Lallemand74c24fb2016-11-21 17:18:36 +0100240
241 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
242 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
243 file, line, args[0], args[1], err && *err ? *err : "error");
244 return -1;
245 }
246
247 cur_arg = 3;
248 while (*args[cur_arg]) {
249 static int bind_dumped;
250 struct bind_kw *kw;
251
252 kw = bind_find_kw(args[cur_arg]);
253 if (kw) {
254 if (!kw->parse) {
255 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
256 args[0], args[1], args[cur_arg]);
257 return -1;
258 }
259
260 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
261 if (err && *err)
262 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
263 else
264 memprintf(err, "'%s %s' : error encountered while processing '%s'",
265 args[0], args[1], args[cur_arg]);
266 return -1;
267 }
268
269 cur_arg += 1 + kw->skip;
270 continue;
271 }
272
273 if (!bind_dumped) {
274 bind_dump_kws(err);
275 indent_msg(err, 4);
276 bind_dumped = 1;
277 }
278
279 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
280 args[0], args[1], args[cur_arg],
281 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
282 return -1;
283 }
284
285 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
286 l->maxconn = global.stats_fe->maxconn;
287 l->backlog = global.stats_fe->backlog;
288 l->accept = session_accept_fd;
William Lallemand74c24fb2016-11-21 17:18:36 +0100289 l->default_target = global.stats_fe->default_target;
290 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
291 l->nice = -64; /* we want to boost priority for local stats */
292 global.maxsock += l->maxconn;
293 }
294 }
295 else if (!strcmp(args[1], "timeout")) {
296 unsigned timeout;
297 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
298
299 if (res) {
300 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
301 return -1;
302 }
303
304 if (!timeout) {
305 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
306 return -1;
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->timeout.client = MS_TO_TICKS(timeout);
315 }
316 else if (!strcmp(args[1], "maxconn")) {
317 int maxconn = atol(args[2]);
318
319 if (maxconn <= 0) {
320 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
321 return -1;
322 }
323
324 if (!global.stats_fe) {
325 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
326 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
327 return -1;
328 }
329 }
330 global.stats_fe->maxconn = maxconn;
331 }
332 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
333 int cur_arg = 2;
334 unsigned long set = 0;
335
336 if (!global.stats_fe) {
337 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
338 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
339 return -1;
340 }
341 }
342
343 while (*args[cur_arg]) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100344 if (strcmp(args[cur_arg], "all") == 0) {
345 set = 0;
346 break;
347 }
Christopher Faulet26028f62017-11-22 15:01:51 +0100348 if (parse_process_number(args[cur_arg], &set, NULL, err)) {
Christopher Fauletf1f0c5f2017-11-22 12:06:43 +0100349 memprintf(err, "'%s %s' : %s", args[0], args[1], *err);
William Lallemand74c24fb2016-11-21 17:18:36 +0100350 return -1;
351 }
352 cur_arg++;
353 }
354 global.stats_fe->bind_proc = set;
355 }
356 else {
357 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
358 return -1;
359 }
360 return 0;
361}
362
Willy Tarreaude57a572016-11-23 17:01:39 +0100363/* Verifies that the CLI at least has a level at least as high as <level>
364 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
365 * failure, an error message is prepared and the appctx's state is adjusted
366 * to print it so that a return 1 is enough to abort any processing.
367 */
368int cli_has_level(struct appctx *appctx, int level)
369{
370 struct stream_interface *si = appctx->owner;
371 struct stream *s = si_strm(si);
372
William Lallemand07a62f72017-05-24 00:57:40 +0200373 if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < level) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200374 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaude57a572016-11-23 17:01:39 +0100375 appctx->ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100376 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaude57a572016-11-23 17:01:39 +0100377 return 0;
378 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100379 return 1;
380}
381
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200382/* Returns severity_output for the current session if set, or default for the socket */
383static int cli_get_severity_output(struct appctx *appctx)
384{
385 if (appctx->cli_severity_output)
386 return appctx->cli_severity_output;
387 return strm_li(si_strm(appctx->owner))->bind_conf->severity_output;
388}
William Lallemand74c24fb2016-11-21 17:18:36 +0100389
Willy Tarreau41908562016-11-24 16:23:38 +0100390/* Processes the CLI interpreter on the stats socket. This function is called
391 * from the CLI's IO handler running in an appctx context. The function returns 1
392 * if the request was understood, otherwise zero. It is called with appctx->st0
393 * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do
394 * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to
395 * have its own I/O handler called again. Most of the time, parsers will only
396 * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg.
Willy Tarreaueaffde32016-12-16 17:59:25 +0100397 * If a keyword parser is NULL and an I/O handler is declared, the I/O handler
398 * will automatically be used.
William Lallemand74c24fb2016-11-21 17:18:36 +0100399 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200400static int cli_parse_request(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100401{
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200402 char *args[MAX_STATS_ARGS + 1], *p, *end, *payload = NULL;
403 int i = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100404 struct cli_kw *kw;
William Lallemand74c24fb2016-11-21 17:18:36 +0100405
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200406 appctx->st2 = 0;
407 memset(&appctx->ctx.cli, 0, sizeof(appctx->ctx.cli));
William Lallemand74c24fb2016-11-21 17:18:36 +0100408
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200409 p = appctx->chunk->area;
410 end = p + appctx->chunk->data;
William Lallemand74c24fb2016-11-21 17:18:36 +0100411
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200412 /*
413 * Get the payload start if there is one.
414 * For the sake of simplicity, the payload pattern is looked up
415 * everywhere from the start of the input but it can only be found
416 * at the end of the first line if APPCTX_CLI_ST1_PAYLOAD is set.
417 *
418 * The input string was zero terminated so it is safe to use
419 * the str*() functions throughout the parsing
420 */
421 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
422 payload = strstr(p, PAYLOAD_PATTERN);
423 end = payload;
424 /* skip the pattern */
425 payload += strlen(PAYLOAD_PATTERN);
426 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100427
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200428 /*
429 * Get pointers on words.
430 * One extra slot is reserved to store a pointer on a null byte.
431 */
432 while (i < MAX_STATS_ARGS && p < end) {
433 int j, k;
William Lallemand74c24fb2016-11-21 17:18:36 +0100434
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200435 /* skip leading spaces/tabs */
436 p += strspn(p, " \t");
437 if (!*p)
438 break;
William Lallemand74c24fb2016-11-21 17:18:36 +0100439
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200440 args[i] = p;
441 p += strcspn(p, " \t");
442 *p++ = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100443
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200444 /* unescape backslashes (\) */
445 for (j = 0, k = 0; args[i][k]; k++) {
446 if (args[i][k] == '\\') {
447 if (args[i][k + 1] == '\\')
448 k++;
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100449 else
450 continue;
451 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200452 args[i][j] = args[i][k];
William Lallemand74c24fb2016-11-21 17:18:36 +0100453 j++;
454 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200455 args[i][j] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100456
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200457 i++;
458 }
459 /* fill unused slots */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200460 p = appctx->chunk->area + appctx->chunk->data;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200461 for (; i < MAX_STATS_ARGS + 1; i++)
462 args[i] = p;
Willy Tarreau41908562016-11-24 16:23:38 +0100463
464 kw = cli_find_kw(args);
Willy Tarreaueaffde32016-12-16 17:59:25 +0100465 if (!kw)
Willy Tarreau41908562016-11-24 16:23:38 +0100466 return 0;
467
468 appctx->io_handler = kw->io_handler;
Emeric Brund6871f72017-06-29 19:54:13 +0200469 appctx->io_release = kw->io_release;
470 /* kw->parse could set its own io_handler or ip_release handler */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200471 if ((!kw->parse || kw->parse(args, payload, appctx, kw->private) == 0) && appctx->io_handler) {
Willy Tarreau41908562016-11-24 16:23:38 +0100472 appctx->st0 = CLI_ST_CALLBACK;
William Lallemand74c24fb2016-11-21 17:18:36 +0100473 }
Willy Tarreau41908562016-11-24 16:23:38 +0100474 return 1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100475}
476
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200477/* prepends then outputs the argument msg with a syslog-type severity depending on severity_output value */
478static int cli_output_msg(struct channel *chn, const char *msg, int severity, int severity_output)
479{
Willy Tarreau83061a82018-07-13 11:56:34 +0200480 struct buffer *tmp;
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200481
482 if (likely(severity_output == CLI_SEVERITY_NONE))
Willy Tarreau06d80a92017-10-19 14:32:15 +0200483 return ci_putblk(chn, msg, strlen(msg));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200484
485 tmp = get_trash_chunk();
486 chunk_reset(tmp);
487
488 if (severity < 0 || severity > 7) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100489 ha_warning("socket command feedback with invalid severity %d", severity);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200490 chunk_printf(tmp, "[%d]: ", severity);
491 }
492 else {
493 switch (severity_output) {
494 case CLI_SEVERITY_NUMBER:
495 chunk_printf(tmp, "[%d]: ", severity);
496 break;
497 case CLI_SEVERITY_STRING:
498 chunk_printf(tmp, "[%s]: ", log_levels[severity]);
499 break;
500 default:
Christopher Faulet767a84b2017-11-24 16:50:31 +0100501 ha_warning("Unrecognized severity output %d", severity_output);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200502 }
503 }
504 chunk_appendf(tmp, "%s", msg);
505
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200506 return ci_putblk(chn, tmp->area, strlen(tmp->area));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200507}
508
William Lallemand74c24fb2016-11-21 17:18:36 +0100509/* This I/O handler runs as an applet embedded in a stream interface. It is
510 * used to processes I/O from/to the stats unix socket. The system relies on a
511 * state machine handling requests and various responses. We read a request,
512 * then we process it and send the response, and we possibly display a prompt.
513 * Then we can read again. The state is stored in appctx->st0 and is one of the
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100514 * CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled
William Lallemand74c24fb2016-11-21 17:18:36 +0100515 * or not.
516 */
517static void cli_io_handler(struct appctx *appctx)
518{
519 struct stream_interface *si = appctx->owner;
520 struct channel *req = si_oc(si);
521 struct channel *res = si_ic(si);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200522 struct bind_conf *bind_conf = strm_li(si_strm(si))->bind_conf;
William Lallemand74c24fb2016-11-21 17:18:36 +0100523 int reql;
524 int len;
525
526 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
527 goto out;
528
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100529 /* Check if the input buffer is avalaible. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200530 if (res->buf.size == 0) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100531 si_applet_cant_put(si);
532 goto out;
533 }
534
William Lallemand74c24fb2016-11-21 17:18:36 +0100535 while (1) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100536 if (appctx->st0 == CLI_ST_INIT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100537 /* Stats output not initialized yet */
538 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200539 /* reset severity to default at init */
540 appctx->cli_severity_output = bind_conf->severity_output;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100541 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100542 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100543 else if (appctx->st0 == CLI_ST_END) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100544 /* Let's close for real now. We just close the request
545 * side, the conditions below will complete if needed.
546 */
547 si_shutw(si);
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200548 free_trash_chunk(appctx->chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +0100549 break;
550 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100551 else if (appctx->st0 == CLI_ST_GETREQ) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200552 char *str;
553
554 /* use a trash chunk to store received data */
555 if (!appctx->chunk) {
556 appctx->chunk = alloc_trash_chunk();
557 if (!appctx->chunk) {
558 appctx->st0 = CLI_ST_END;
559 continue;
560 }
561 }
562
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200563 str = appctx->chunk->area + appctx->chunk->data;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200564
William Lallemand74c24fb2016-11-21 17:18:36 +0100565 /* ensure we have some output room left in the event we
566 * would want to return some info right after parsing.
567 */
568 if (buffer_almost_full(si_ib(si))) {
569 si_applet_cant_put(si);
570 break;
571 }
572
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200573 /* '- 1' is to ensure a null byte can always be inserted at the end */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200574 reql = co_getline(si_oc(si), str,
575 appctx->chunk->size - appctx->chunk->data - 1);
William Lallemand74c24fb2016-11-21 17:18:36 +0100576 if (reql <= 0) { /* closed or EOL not found */
577 if (reql == 0)
578 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100579 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100580 continue;
581 }
582
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200583 if (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)) {
584 /* seek for a possible unescaped semi-colon. If we find
585 * one, we replace it with an LF and skip only this part.
586 */
587 for (len = 0; len < reql; len++) {
588 if (str[len] == '\\') {
589 len++;
590 continue;
591 }
592 if (str[len] == ';') {
593 str[len] = '\n';
594 reql = len + 1;
595 break;
596 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100597 }
598 }
599
600 /* now it is time to check that we have a full line,
601 * remove the trailing \n and possibly \r, then cut the
602 * line.
603 */
604 len = reql - 1;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200605 if (str[len] != '\n') {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100606 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100607 continue;
608 }
609
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200610 if (len && str[len-1] == '\r')
William Lallemand74c24fb2016-11-21 17:18:36 +0100611 len--;
612
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200613 str[len] = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200614 appctx->chunk->data += len;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200615
616 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200617 appctx->chunk->area[appctx->chunk->data] = '\n';
618 appctx->chunk->area[appctx->chunk->data + 1] = 0;
619 appctx->chunk->data++;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200620 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100621
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100622 appctx->st0 = CLI_ST_PROMPT;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200623
624 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
625 /* empty line */
626 if (!len) {
627 /* remove the last two \n */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200628 appctx->chunk->data -= 2;
629 appctx->chunk->area[appctx->chunk->data] = 0;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200630
631 if (!cli_parse_request(appctx))
632 cli_gen_usage_msg(appctx);
633
634 chunk_reset(appctx->chunk);
635 /* NB: cli_sock_parse_request() may have put
636 * another CLI_ST_O_* into appctx->st0.
637 */
638
639 appctx->st1 &= ~APPCTX_CLI_ST1_PAYLOAD;
William Lallemand74c24fb2016-11-21 17:18:36 +0100640 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100641 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200642 else {
643 /*
644 * Look for the "payload start" pattern at the end of a line
645 * Its location is not remembered here, this is just to switch
646 * to a gathering mode.
William Lallemand74c24fb2016-11-21 17:18:36 +0100647 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200648 if (!strcmp(appctx->chunk->area + appctx->chunk->data - strlen(PAYLOAD_PATTERN), PAYLOAD_PATTERN))
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200649 appctx->st1 |= APPCTX_CLI_ST1_PAYLOAD;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200650 else {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200651 /* no payload, the command is complete: parse the request */
652 if (!cli_parse_request(appctx))
653 cli_gen_usage_msg(appctx);
654
655 chunk_reset(appctx->chunk);
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200656 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100657 }
658
659 /* re-adjust req buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200660 co_skip(si_oc(si), reql);
William Lallemand74c24fb2016-11-21 17:18:36 +0100661 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
662 }
663 else { /* output functions */
664 switch (appctx->st0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100665 case CLI_ST_PROMPT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100666 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100667 case CLI_ST_PRINT:
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200668 if (cli_output_msg(res, appctx->ctx.cli.msg, appctx->ctx.cli.severity,
669 cli_get_severity_output(appctx)) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100670 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100671 else
672 si_applet_cant_put(si);
673 break;
Aurélien Nephtalic511b7c2018-04-16 18:50:19 +0200674 case CLI_ST_PRINT_FREE: {
675 const char *msg = appctx->ctx.cli.err;
676
677 if (!msg)
678 msg = "Out of memory.\n";
679
680 if (cli_output_msg(res, msg, LOG_ERR, cli_get_severity_output(appctx)) != -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100681 free(appctx->ctx.cli.err);
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100682 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100683 }
684 else
685 si_applet_cant_put(si);
686 break;
Aurélien Nephtalic511b7c2018-04-16 18:50:19 +0200687 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100688 case CLI_ST_CALLBACK: /* use custom pointer */
William Lallemand74c24fb2016-11-21 17:18:36 +0100689 if (appctx->io_handler)
690 if (appctx->io_handler(appctx)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100691 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100692 if (appctx->io_release) {
693 appctx->io_release(appctx);
694 appctx->io_release = NULL;
695 }
696 }
697 break;
698 default: /* abnormal state */
699 si->flags |= SI_FL_ERR;
700 break;
701 }
702
703 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100704 if (appctx->st0 == CLI_ST_PROMPT) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200705 const char *prompt = "";
706
707 if (appctx->st1 & APPCTX_CLI_ST1_PROMPT) {
708 /*
709 * when entering a payload with interactive mode, change the prompt
710 * to emphasize that more data can still be sent
711 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200712 if (appctx->chunk->data && appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200713 prompt = "+ ";
714 else
715 prompt = "\n> ";
716 }
717 else {
718 if (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD))
719 prompt = "\n";
720 }
721
722 if (ci_putstr(si_ic(si), prompt) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100723 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100724 else
725 si_applet_cant_put(si);
726 }
727
728 /* If the output functions are still there, it means they require more room. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100729 if (appctx->st0 >= CLI_ST_OUTPUT)
William Lallemand74c24fb2016-11-21 17:18:36 +0100730 break;
731
732 /* Now we close the output if one of the writers did so,
733 * or if we're not in interactive mode and the request
734 * buffer is empty. This still allows pipelined requests
735 * to be sent in non-interactive mode.
736 */
Willy Tarreau851d12c2018-06-19 07:01:36 +0200737 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!(appctx->st1 & APPCTX_CLI_ST1_PROMPT) && !co_data(req))) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100738 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100739 continue;
740 }
741
742 /* switch state back to GETREQ to read next requests */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100743 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100744 }
745 }
746
747 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
748 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
749 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
750 /* Other side has closed, let's abort if we have no more processing to do
751 * and nothing more to consume. This is comparable to a broken pipe, so
752 * we forward the close to the request side so that it flows upstream to
753 * the client.
754 */
755 si_shutw(si);
756 }
757
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100758 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < CLI_ST_OUTPUT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100759 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
760 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
761 /* We have no more processing to do, and nothing more to send, and
762 * the client side has closed. So we'll forward this state downstream
763 * on the response buffer.
764 */
765 si_shutr(si);
766 res->flags |= CF_READ_NULL;
767 }
768
769 out:
Christopher Faulet45073512018-07-20 10:16:29 +0200770 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%lu, rqs=%lu, rh=%lu, rs=%lu\n",
William Lallemand74c24fb2016-11-21 17:18:36 +0100771 __FUNCTION__, __LINE__,
Christopher Faulet45073512018-07-20 10:16:29 +0200772 si->state, req->flags, res->flags, ci_data(req), co_data(req), ci_data(res), co_data(res));
William Lallemand74c24fb2016-11-21 17:18:36 +0100773}
774
William Lallemand74c24fb2016-11-21 17:18:36 +0100775/* This is called when the stream interface is closed. For instance, upon an
776 * external abort, we won't call the i/o handler anymore so we may need to
777 * remove back references to the stream currently being dumped.
778 */
779static void cli_release_handler(struct appctx *appctx)
780{
781 if (appctx->io_release) {
782 appctx->io_release(appctx);
783 appctx->io_release = NULL;
784 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100785 else if (appctx->st0 == CLI_ST_PRINT_FREE) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100786 free(appctx->ctx.cli.err);
787 appctx->ctx.cli.err = NULL;
788 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100789}
790
791/* This function dumps all environmnent variables to the buffer. It returns 0
792 * if the output buffer is full and it needs to be called again, otherwise
Willy Tarreauf6710f82016-12-16 17:45:44 +0100793 * non-zero. Dumps only one entry if st2 == STAT_ST_END. It uses cli.p0 as the
794 * pointer to the current variable.
William Lallemand74c24fb2016-11-21 17:18:36 +0100795 */
Willy Tarreau0a739292016-11-22 20:21:23 +0100796static int cli_io_handler_show_env(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100797{
Willy Tarreau0a739292016-11-22 20:21:23 +0100798 struct stream_interface *si = appctx->owner;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100799 char **var = appctx->ctx.cli.p0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100800
801 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
802 return 1;
803
804 chunk_reset(&trash);
805
806 /* we have two inner loops here, one for the proxy, the other one for
807 * the buffer.
808 */
Willy Tarreauf6710f82016-12-16 17:45:44 +0100809 while (*var) {
810 chunk_printf(&trash, "%s\n", *var);
William Lallemand74c24fb2016-11-21 17:18:36 +0100811
Willy Tarreau06d80a92017-10-19 14:32:15 +0200812 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100813 si_applet_cant_put(si);
814 return 0;
815 }
816 if (appctx->st2 == STAT_ST_END)
817 break;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100818 var++;
819 appctx->ctx.cli.p0 = var;
William Lallemand74c24fb2016-11-21 17:18:36 +0100820 }
821
822 /* dump complete */
823 return 1;
824}
825
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200826/* This function dumps all file descriptors states (or the requested one) to
827 * the buffer. It returns 0 if the output buffer is full and it needs to be
828 * called again, otherwise non-zero. Dumps only one entry if st2 == STAT_ST_END.
829 * It uses cli.i0 as the fd number to restart from.
830 */
831static int cli_io_handler_show_fd(struct appctx *appctx)
832{
833 struct stream_interface *si = appctx->owner;
834 int fd = appctx->ctx.cli.i0;
835
836 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
837 return 1;
838
839 chunk_reset(&trash);
840
841 /* we have two inner loops here, one for the proxy, the other one for
842 * the buffer.
843 */
Aurélien Nephtali498a1152018-03-09 18:51:16 +0100844 while (fd >= 0 && fd < global.maxsock) {
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200845 struct fdtab fdt;
Willy Tarreau286ec682017-08-09 16:35:44 +0200846 struct listener *li = NULL;
847 struct server *sv = NULL;
848 struct proxy *px = NULL;
Willy Tarreaua833cd92018-03-29 13:19:37 +0200849 const struct mux_ops *mux = NULL;
Willy Tarreau35b1b482018-03-28 18:41:30 +0200850 void *ctx = NULL;
Willy Tarreau286ec682017-08-09 16:35:44 +0200851 uint32_t conn_flags = 0;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200852
Willy Tarreaubf9fd652018-08-02 11:05:48 +0200853 thread_isolate();
854
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200855 fdt = fdtab[fd];
856
Willy Tarreaubf9fd652018-08-02 11:05:48 +0200857 if (!fdt.owner) {
858 thread_release();
Willy Tarreau017af242017-10-04 20:24:54 +0200859 goto skip; // closed
Willy Tarreaubf9fd652018-08-02 11:05:48 +0200860 }
Willy Tarreau017af242017-10-04 20:24:54 +0200861
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200862 if (fdt.iocb == conn_fd_handler) {
863 conn_flags = ((struct connection *)fdt.owner)->flags;
Willy Tarreau35b1b482018-03-28 18:41:30 +0200864 mux = ((struct connection *)fdt.owner)->mux;
865 ctx = ((struct connection *)fdt.owner)->mux_ctx;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200866 li = objt_listener(((struct connection *)fdt.owner)->target);
867 sv = objt_server(((struct connection *)fdt.owner)->target);
868 px = objt_proxy(((struct connection *)fdt.owner)->target);
869 }
870 else if (fdt.iocb == listener_accept)
871 li = fdt.owner;
872
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200873 chunk_printf(&trash,
Willy Tarreauc754b342018-03-30 15:00:15 +0200874 " %5d : st=0x%02x(R:%c%c%c W:%c%c%c) ev=0x%02x(%c%c%c%c%c) [%c%c] cnext=%d cprev=%d tmask=0x%lx umask=0x%lx owner=%p iocb=%p(%s)",
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200875 fd,
876 fdt.state,
877 (fdt.state & FD_EV_POLLED_R) ? 'P' : 'p',
878 (fdt.state & FD_EV_READY_R) ? 'R' : 'r',
879 (fdt.state & FD_EV_ACTIVE_R) ? 'A' : 'a',
880 (fdt.state & FD_EV_POLLED_W) ? 'P' : 'p',
881 (fdt.state & FD_EV_READY_W) ? 'R' : 'r',
882 (fdt.state & FD_EV_ACTIVE_W) ? 'A' : 'a',
883 fdt.ev,
884 (fdt.ev & FD_POLL_HUP) ? 'H' : 'h',
885 (fdt.ev & FD_POLL_ERR) ? 'E' : 'e',
886 (fdt.ev & FD_POLL_OUT) ? 'O' : 'o',
887 (fdt.ev & FD_POLL_PRI) ? 'P' : 'p',
888 (fdt.ev & FD_POLL_IN) ? 'I' : 'i',
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200889 fdt.linger_risk ? 'L' : 'l',
890 fdt.cloned ? 'C' : 'c',
Willy Tarreauc754b342018-03-30 15:00:15 +0200891 fdt.cache.next,
892 fdt.cache.prev,
893 fdt.thread_mask, fdt.update_mask,
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200894 fdt.owner,
895 fdt.iocb,
896 (fdt.iocb == conn_fd_handler) ? "conn_fd_handler" :
897 (fdt.iocb == dgram_fd_handler) ? "dgram_fd_handler" :
898 (fdt.iocb == listener_accept) ? "listener_accept" :
Olivier Houchard79321b92018-07-26 17:55:11 +0200899 (fdt.iocb == poller_pipe_io_handler) ? "poller_pipe_io_handler" :
Willy Tarreauc754b342018-03-30 15:00:15 +0200900 "unknown");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200901
902 if (fdt.iocb == conn_fd_handler) {
903 chunk_appendf(&trash, " cflg=0x%08x", conn_flags);
904 if (px)
905 chunk_appendf(&trash, " px=%s", px->id);
906 else if (sv)
907 chunk_appendf(&trash, " sv=%s/%s", sv->id, sv->proxy->id);
908 else if (li)
909 chunk_appendf(&trash, " fe=%s", li->bind_conf->frontend->id);
Willy Tarreau35b1b482018-03-28 18:41:30 +0200910
Willy Tarreaub011d8f2018-03-30 14:41:19 +0200911 if (mux) {
Willy Tarreau35b1b482018-03-28 18:41:30 +0200912 chunk_appendf(&trash, " mux=%s mux_ctx=%p", mux->name, ctx);
Willy Tarreaub011d8f2018-03-30 14:41:19 +0200913 if (mux->show_fd)
914 mux->show_fd(&trash, fdt.owner);
915 }
Willy Tarreau35b1b482018-03-28 18:41:30 +0200916 else
917 chunk_appendf(&trash, " nomux");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200918 }
919 else if (fdt.iocb == listener_accept) {
920 chunk_appendf(&trash, " l.st=%s fe=%s",
921 listener_state_str(li),
922 li->bind_conf->frontend->id);
923 }
924
Willy Tarreaubf9fd652018-08-02 11:05:48 +0200925 thread_release();
926
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200927 chunk_appendf(&trash, "\n");
928
Willy Tarreau06d80a92017-10-19 14:32:15 +0200929 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200930 si_applet_cant_put(si);
931 return 0;
932 }
933 skip:
934 if (appctx->st2 == STAT_ST_END)
935 break;
936
937 fd++;
938 appctx->ctx.cli.i0 = fd;
939 }
940
941 /* dump complete */
942 return 1;
943}
944
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100945/* This function dumps some activity counters used by developers and support to
946 * rule out some hypothesis during bug reports. It returns 0 if the output
947 * buffer is full and it needs to be called again, otherwise non-zero. It dumps
948 * everything at once in the buffer and is not designed to do it in multiple
949 * passes.
950 */
951static int cli_io_handler_show_activity(struct appctx *appctx)
952{
953 struct stream_interface *si = appctx->owner;
954 int thr;
955
956 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
957 return 1;
958
959 chunk_reset(&trash);
960
961 chunk_appendf(&trash, "thread_id: %u", tid);
962 chunk_appendf(&trash, "\ndate_now: %lu.%06lu", (long)now.tv_sec, (long)now.tv_usec);
963 chunk_appendf(&trash, "\nloops:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].loops);
964 chunk_appendf(&trash, "\nwake_cache:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].wake_cache);
965 chunk_appendf(&trash, "\nwake_tasks:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].wake_tasks);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100966 chunk_appendf(&trash, "\nwake_signal:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].wake_signal);
967 chunk_appendf(&trash, "\npoll_exp:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_exp);
968 chunk_appendf(&trash, "\npoll_drop:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_drop);
969 chunk_appendf(&trash, "\npoll_dead:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_dead);
970 chunk_appendf(&trash, "\npoll_skip:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_skip);
971 chunk_appendf(&trash, "\nfd_skip:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].fd_skip);
972 chunk_appendf(&trash, "\nfd_lock:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].fd_lock);
973 chunk_appendf(&trash, "\nfd_del:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].fd_del);
974 chunk_appendf(&trash, "\nconn_dead:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].conn_dead);
975 chunk_appendf(&trash, "\nstream:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].stream);
976 chunk_appendf(&trash, "\nempty_rq:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].empty_rq);
977 chunk_appendf(&trash, "\nlong_rq:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].long_rq);
Willy Tarreaued72d822018-10-17 19:01:24 +0200978 chunk_appendf(&trash, "\ncpust_tot:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].cpust_total/2);
979 chunk_appendf(&trash, "\ncpust_1s:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", read_freq_ctr(&activity[thr].cpust_1s)/2);
980 chunk_appendf(&trash, "\ncpust_15s:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", read_freq_ctr_period(&activity[thr].cpust_15s, 15000)/2);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100981
982 chunk_appendf(&trash, "\n");
983
984 if (ci_putchk(si_ic(si), &trash) == -1) {
985 chunk_reset(&trash);
986 chunk_printf(&trash, "[output too large, cannot dump]\n");
987 si_applet_cant_put(si);
988 }
989
990 /* dump complete */
991 return 1;
992}
993
William Lallemandeceddf72016-12-15 18:06:44 +0100994/*
Willy Tarreau3af9d832016-12-16 12:58:09 +0100995 * CLI IO handler for `show cli sockets`.
996 * Uses ctx.cli.p0 to store the restart pointer.
William Lallemandeceddf72016-12-15 18:06:44 +0100997 */
998static int cli_io_handler_show_cli_sock(struct appctx *appctx)
999{
1000 struct bind_conf *bind_conf;
1001 struct stream_interface *si = appctx->owner;
1002
1003 chunk_reset(&trash);
1004
1005 switch (appctx->st2) {
1006 case STAT_ST_INIT:
1007 chunk_printf(&trash, "# socket lvl processes\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001008 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandeceddf72016-12-15 18:06:44 +01001009 si_applet_cant_put(si);
1010 return 0;
1011 }
William Lallemandeceddf72016-12-15 18:06:44 +01001012 appctx->st2 = STAT_ST_LIST;
1013
1014 case STAT_ST_LIST:
1015 if (global.stats_fe) {
1016 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
1017 struct listener *l;
1018
1019 /*
Willy Tarreau3af9d832016-12-16 12:58:09 +01001020 * get the latest dumped node in appctx->ctx.cli.p0
William Lallemandeceddf72016-12-15 18:06:44 +01001021 * if the current node is the first of the list
1022 */
1023
Willy Tarreau3af9d832016-12-16 12:58:09 +01001024 if (appctx->ctx.cli.p0 &&
1025 &bind_conf->by_fe == (&global.stats_fe->conf.bind)->n) {
William Lallemandeceddf72016-12-15 18:06:44 +01001026 /* change the current node to the latest dumped and continue the loop */
Willy Tarreau3af9d832016-12-16 12:58:09 +01001027 bind_conf = LIST_ELEM(appctx->ctx.cli.p0, typeof(bind_conf), by_fe);
William Lallemandeceddf72016-12-15 18:06:44 +01001028 continue;
1029 }
1030
1031 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
1032
1033 char addr[46];
1034 char port[6];
1035
1036 if (l->addr.ss_family == AF_UNIX) {
1037 const struct sockaddr_un *un;
1038
1039 un = (struct sockaddr_un *)&l->addr;
1040 chunk_appendf(&trash, "%s ", un->sun_path);
1041 } else if (l->addr.ss_family == AF_INET) {
1042 addr_to_str(&l->addr, addr, sizeof(addr));
1043 port_to_str(&l->addr, port, sizeof(port));
1044 chunk_appendf(&trash, "%s:%s ", addr, port);
1045 } else if (l->addr.ss_family == AF_INET6) {
1046 addr_to_str(&l->addr, addr, sizeof(addr));
1047 port_to_str(&l->addr, port, sizeof(port));
1048 chunk_appendf(&trash, "[%s]:%s ", addr, port);
1049 } else
1050 continue;
1051
William Lallemand07a62f72017-05-24 00:57:40 +02001052 if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN)
William Lallemandeceddf72016-12-15 18:06:44 +01001053 chunk_appendf(&trash, "admin ");
William Lallemand07a62f72017-05-24 00:57:40 +02001054 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER)
1055 chunk_appendf(&trash, "operator ");
1056 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_USER)
1057 chunk_appendf(&trash, "user ");
William Lallemandeceddf72016-12-15 18:06:44 +01001058 else
1059 chunk_appendf(&trash, " ");
1060
1061 if (bind_conf->bind_proc != 0) {
1062 int pos;
Willy Tarreau20c5e522016-12-16 12:50:55 +01001063 for (pos = 0; pos < 8 * sizeof(bind_conf->bind_proc); pos++) {
Willy Tarreau4305ac72016-12-16 12:56:31 +01001064 if (bind_conf->bind_proc & (1UL << pos)) {
William Lallemandeceddf72016-12-15 18:06:44 +01001065 chunk_appendf(&trash, "%d,", pos+1);
1066 }
1067 }
1068 /* replace the latest comma by a newline */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001069 trash.area[trash.data-1] = '\n';
William Lallemandeceddf72016-12-15 18:06:44 +01001070
1071 } else {
1072 chunk_appendf(&trash, "all\n");
1073 }
1074
Willy Tarreau06d80a92017-10-19 14:32:15 +02001075 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandeceddf72016-12-15 18:06:44 +01001076 si_applet_cant_put(si);
1077 return 0;
1078 }
1079 }
Willy Tarreau3af9d832016-12-16 12:58:09 +01001080 appctx->ctx.cli.p0 = &bind_conf->by_fe; /* store the latest list node dumped */
William Lallemandeceddf72016-12-15 18:06:44 +01001081 }
1082 }
1083 default:
1084 appctx->st2 = STAT_ST_FIN;
1085 return 1;
1086 }
1087}
1088
1089
Willy Tarreau0a739292016-11-22 20:21:23 +01001090/* parse a "show env" CLI request. Returns 0 if it needs to continue, 1 if it
Willy Tarreauf6710f82016-12-16 17:45:44 +01001091 * wants to stop here. It puts the variable to be dumped into cli.p0 if a single
1092 * variable is requested otherwise puts environ there.
Willy Tarreau0a739292016-11-22 20:21:23 +01001093 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001094static int cli_parse_show_env(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0a739292016-11-22 20:21:23 +01001095{
1096 extern char **environ;
Willy Tarreauf6710f82016-12-16 17:45:44 +01001097 char **var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001098
1099 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1100 return 1;
1101
Willy Tarreauf6710f82016-12-16 17:45:44 +01001102 var = environ;
Willy Tarreau0a739292016-11-22 20:21:23 +01001103
1104 if (*args[2]) {
1105 int len = strlen(args[2]);
1106
Willy Tarreauf6710f82016-12-16 17:45:44 +01001107 for (; *var; var++) {
1108 if (strncmp(*var, args[2], len) == 0 &&
1109 (*var)[len] == '=')
Willy Tarreau0a739292016-11-22 20:21:23 +01001110 break;
1111 }
Willy Tarreauf6710f82016-12-16 17:45:44 +01001112 if (!*var) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001113 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau0a739292016-11-22 20:21:23 +01001114 appctx->ctx.cli.msg = "Variable not found\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001115 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau0a739292016-11-22 20:21:23 +01001116 return 1;
1117 }
1118 appctx->st2 = STAT_ST_END;
1119 }
Willy Tarreauf6710f82016-12-16 17:45:44 +01001120 appctx->ctx.cli.p0 = var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001121 return 0;
1122}
1123
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001124/* parse a "show fd" CLI request. Returns 0 if it needs to continue, 1 if it
1125 * wants to stop here. It puts the FD number into cli.i0 if a specific FD is
1126 * requested and sets st2 to STAT_ST_END, otherwise leaves 0 in i0.
1127 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001128static int cli_parse_show_fd(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001129{
1130 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1131 return 1;
1132
1133 appctx->ctx.cli.i0 = 0;
1134
1135 if (*args[2]) {
1136 appctx->ctx.cli.i0 = atoi(args[2]);
1137 appctx->st2 = STAT_ST_END;
1138 }
1139 return 0;
1140}
1141
Willy Tarreau599852e2016-11-22 20:33:32 +01001142/* parse a "set timeout" CLI request. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001143static int cli_parse_set_timeout(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau599852e2016-11-22 20:33:32 +01001144{
1145 struct stream_interface *si = appctx->owner;
1146 struct stream *s = si_strm(si);
1147
1148 if (strcmp(args[2], "cli") == 0) {
1149 unsigned timeout;
1150 const char *res;
1151
1152 if (!*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001153 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau599852e2016-11-22 20:33:32 +01001154 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001155 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001156 return 1;
1157 }
1158
1159 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1160 if (res || timeout < 1) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001161 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau599852e2016-11-22 20:33:32 +01001162 appctx->ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001163 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001164 return 1;
1165 }
1166
1167 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
1168 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
1169 return 1;
1170 }
1171 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001172 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau599852e2016-11-22 20:33:32 +01001173 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001174 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001175 return 1;
1176 }
1177}
1178
Willy Tarreau2af99412016-11-23 11:10:59 +01001179/* parse a "set maxconn global" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001180static int cli_parse_set_maxconn_global(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2af99412016-11-23 11:10:59 +01001181{
1182 int v;
1183
1184 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1185 return 1;
1186
1187 if (!*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001188 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau2af99412016-11-23 11:10:59 +01001189 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001190 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +01001191 return 1;
1192 }
1193
1194 v = atoi(args[3]);
1195 if (v > global.hardmaxconn) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001196 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau2af99412016-11-23 11:10:59 +01001197 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001198 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +01001199 return 1;
1200 }
1201
1202 /* check for unlimited values */
1203 if (v <= 0)
1204 v = global.hardmaxconn;
1205
1206 global.maxconn = v;
1207
1208 /* Dequeues all of the listeners waiting for a resource */
1209 if (!LIST_ISEMPTY(&global_listener_queue))
1210 dequeue_all_listeners(&global_listener_queue);
1211
1212 return 1;
1213}
1214
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001215static int set_severity_output(int *target, char *argument)
1216{
1217 if (!strcmp(argument, "none")) {
1218 *target = CLI_SEVERITY_NONE;
1219 return 1;
1220 }
1221 else if (!strcmp(argument, "number")) {
1222 *target = CLI_SEVERITY_NUMBER;
1223 return 1;
1224 }
1225 else if (!strcmp(argument, "string")) {
1226 *target = CLI_SEVERITY_STRING;
1227 return 1;
1228 }
1229 return 0;
1230}
1231
1232/* parse a "set severity-output" command. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001233static int cli_parse_set_severity_output(char **args, char *payload, struct appctx *appctx, void *private)
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001234{
1235 if (*args[2] && set_severity_output(&appctx->cli_severity_output, args[2]))
1236 return 0;
1237
1238 appctx->ctx.cli.severity = LOG_ERR;
Aurélien Nephtali6e8a41d2018-03-15 21:48:50 +01001239 appctx->ctx.cli.msg = "one of 'none', 'number', 'string' is a required argument\n";
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001240 appctx->st0 = CLI_ST_PRINT;
1241 return 1;
1242}
William Lallemandeceddf72016-12-15 18:06:44 +01001243
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001244int cli_parse_default(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandeceddf72016-12-15 18:06:44 +01001245{
1246 return 0;
1247}
1248
Willy Tarreau45c742b2016-11-24 14:51:17 +01001249/* parse a "set rate-limit" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001250static int cli_parse_set_ratelimit(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau45c742b2016-11-24 14:51:17 +01001251{
1252 int v;
1253 int *res;
1254 int mul = 1;
1255
1256 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1257 return 1;
1258
1259 if (strcmp(args[2], "connections") == 0 && strcmp(args[3], "global") == 0)
1260 res = &global.cps_lim;
1261 else if (strcmp(args[2], "sessions") == 0 && strcmp(args[3], "global") == 0)
1262 res = &global.sps_lim;
1263#ifdef USE_OPENSSL
1264 else if (strcmp(args[2], "ssl-sessions") == 0 && strcmp(args[3], "global") == 0)
1265 res = &global.ssl_lim;
1266#endif
1267 else if (strcmp(args[2], "http-compression") == 0 && strcmp(args[3], "global") == 0) {
1268 res = &global.comp_rate_lim;
1269 mul = 1024;
1270 }
1271 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001272 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001273 appctx->ctx.cli.msg =
1274 "'set rate-limit' only supports :\n"
1275 " - 'connections global' to set the per-process maximum connection rate\n"
1276 " - 'sessions global' to set the per-process maximum session rate\n"
1277#ifdef USE_OPENSSL
Aurélien Nephtalib53e2082018-03-11 16:55:02 +01001278 " - 'ssl-sessions global' to set the per-process maximum SSL session rate\n"
Willy Tarreau45c742b2016-11-24 14:51:17 +01001279#endif
1280 " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001281 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001282 return 1;
1283 }
1284
1285 if (!*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001286 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001287 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001288 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001289 return 1;
1290 }
1291
1292 v = atoi(args[4]);
1293 if (v < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001294 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001295 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001296 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001297 return 1;
1298 }
1299
1300 *res = v * mul;
1301
1302 /* Dequeues all of the listeners waiting for a resource */
1303 if (!LIST_ISEMPTY(&global_listener_queue))
1304 dequeue_all_listeners(&global_listener_queue);
1305
1306 return 1;
1307}
1308
William Lallemandf6975e92017-05-26 17:42:10 +02001309/* parse the "expose-fd" argument on the bind lines */
1310static int bind_parse_expose_fd(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1311{
1312 if (!*args[cur_arg + 1]) {
1313 memprintf(err, "'%s' : missing fd type", args[cur_arg]);
1314 return ERR_ALERT | ERR_FATAL;
1315 }
1316 if (!strcmp(args[cur_arg+1], "listeners")) {
1317 conf->level |= ACCESS_FD_LISTENERS;
1318 } else {
1319 memprintf(err, "'%s' only supports 'listeners' (got '%s')",
1320 args[cur_arg], args[cur_arg+1]);
1321 return ERR_ALERT | ERR_FATAL;
1322 }
1323
1324 return 0;
1325}
1326
William Lallemand74c24fb2016-11-21 17:18:36 +01001327/* parse the "level" argument on the bind lines */
1328static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1329{
1330 if (!*args[cur_arg + 1]) {
1331 memprintf(err, "'%s' : missing level", args[cur_arg]);
1332 return ERR_ALERT | ERR_FATAL;
1333 }
1334
William Lallemand07a62f72017-05-24 00:57:40 +02001335 if (!strcmp(args[cur_arg+1], "user")) {
1336 conf->level &= ~ACCESS_LVL_MASK;
1337 conf->level |= ACCESS_LVL_USER;
1338 } else if (!strcmp(args[cur_arg+1], "operator")) {
1339 conf->level &= ~ACCESS_LVL_MASK;
1340 conf->level |= ACCESS_LVL_OPER;
1341 } else if (!strcmp(args[cur_arg+1], "admin")) {
1342 conf->level &= ~ACCESS_LVL_MASK;
1343 conf->level |= ACCESS_LVL_ADMIN;
1344 } else {
William Lallemand74c24fb2016-11-21 17:18:36 +01001345 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
1346 args[cur_arg], args[cur_arg+1]);
1347 return ERR_ALERT | ERR_FATAL;
1348 }
1349
1350 return 0;
1351}
1352
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001353static int bind_parse_severity_output(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1354{
1355 if (!*args[cur_arg + 1]) {
1356 memprintf(err, "'%s' : missing severity format", args[cur_arg]);
1357 return ERR_ALERT | ERR_FATAL;
1358 }
1359
1360 if (set_severity_output(&conf->severity_output, args[cur_arg+1]))
1361 return 0;
1362 else {
1363 memprintf(err, "'%s' only supports 'none', 'number', and 'string' (got '%s')",
1364 args[cur_arg], args[cur_arg+1]);
1365 return ERR_ALERT | ERR_FATAL;
1366 }
1367}
1368
Olivier Houchardf886e342017-04-05 22:24:59 +02001369/* Send all the bound sockets, always returns 1 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001370static int _getsocks(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchardf886e342017-04-05 22:24:59 +02001371{
1372 char *cmsgbuf = NULL;
1373 unsigned char *tmpbuf = NULL;
1374 struct cmsghdr *cmsg;
1375 struct stream_interface *si = appctx->owner;
William Lallemandf6975e92017-05-26 17:42:10 +02001376 struct stream *s = si_strm(si);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001377 struct connection *remote = cs_conn(objt_cs(si_opposite(si)->end));
Olivier Houchardf886e342017-04-05 22:24:59 +02001378 struct msghdr msghdr;
1379 struct iovec iov;
Olivier Houchard54740872017-04-06 14:45:14 +02001380 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf886e342017-04-05 22:24:59 +02001381 int *tmpfd;
1382 int tot_fd_nb = 0;
1383 struct proxy *px;
1384 int i = 0;
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001385 int fd = -1;
Olivier Houchardf886e342017-04-05 22:24:59 +02001386 int curoff = 0;
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001387 int old_fcntl = -1;
Olivier Houchardf886e342017-04-05 22:24:59 +02001388 int ret;
1389
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001390 if (!remote) {
1391 ha_warning("Only works on real connections\n");
1392 goto out;
1393 }
1394
1395 fd = remote->handle.fd;
1396
Olivier Houchardf886e342017-04-05 22:24:59 +02001397 /* Temporary set the FD in blocking mode, that will make our life easier */
1398 old_fcntl = fcntl(fd, F_GETFL);
1399 if (old_fcntl < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001400 ha_warning("Couldn't get the flags for the unix socket\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001401 goto out;
1402 }
1403 cmsgbuf = malloc(CMSG_SPACE(sizeof(int) * MAX_SEND_FD));
1404 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001405 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001406 goto out;
1407 }
1408 if (fcntl(fd, F_SETFL, old_fcntl &~ O_NONBLOCK) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001409 ha_warning("Cannot make the unix socket blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001410 goto out;
1411 }
Olivier Houchard54740872017-04-06 14:45:14 +02001412 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf886e342017-04-05 22:24:59 +02001413 iov.iov_base = &tot_fd_nb;
1414 iov.iov_len = sizeof(tot_fd_nb);
William Lallemandf6975e92017-05-26 17:42:10 +02001415 if (!(strm_li(s)->bind_conf->level & ACCESS_FD_LISTENERS))
Olivier Houchardf886e342017-04-05 22:24:59 +02001416 goto out;
1417 memset(&msghdr, 0, sizeof(msghdr));
1418 /*
1419 * First, calculates the total number of FD, so that we can let
1420 * the caller know how much he should expects.
1421 */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001422 px = proxies_list;
Olivier Houchardf886e342017-04-05 22:24:59 +02001423 while (px) {
1424 struct listener *l;
1425
1426 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02001427 /* Only transfer IPv4/IPv6/UNIX sockets */
1428 if (l->state >= LI_ZOMBIE &&
1429 (l->proto->sock_family == AF_INET ||
Olivier Houchardf886e342017-04-05 22:24:59 +02001430 l->proto->sock_family == AF_INET6 ||
Olivier Houchard1fc05162017-04-06 01:05:05 +02001431 l->proto->sock_family == AF_UNIX))
Olivier Houchardf886e342017-04-05 22:24:59 +02001432 tot_fd_nb++;
1433 }
1434 px = px->next;
1435 }
1436 if (tot_fd_nb == 0)
1437 goto out;
1438
1439 /* First send the total number of file descriptors, so that the
1440 * receiving end knows what to expect.
1441 */
1442 msghdr.msg_iov = &iov;
1443 msghdr.msg_iovlen = 1;
1444 ret = sendmsg(fd, &msghdr, 0);
1445 if (ret != sizeof(tot_fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001446 ha_warning("Failed to send the number of sockets to send\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001447 goto out;
1448 }
1449
1450 /* Now send the fds */
1451 msghdr.msg_control = cmsgbuf;
1452 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * MAX_SEND_FD);
1453 cmsg = CMSG_FIRSTHDR(&msghdr);
1454 cmsg->cmsg_len = CMSG_LEN(MAX_SEND_FD * sizeof(int));
1455 cmsg->cmsg_level = SOL_SOCKET;
1456 cmsg->cmsg_type = SCM_RIGHTS;
1457 tmpfd = (int *)CMSG_DATA(cmsg);
1458
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001459 px = proxies_list;
Olivier Houchardf886e342017-04-05 22:24:59 +02001460 /* For each socket, e message is sent, containing the following :
1461 * Size of the namespace name (or 0 if none), as an unsigned char.
1462 * The namespace name, if any
1463 * Size of the interface name (or 0 if none), as an unsigned char
1464 * The interface name, if any
1465 * Listener options, as an int.
1466 */
1467 /* We will send sockets MAX_SEND_FD per MAX_SEND_FD, allocate a
1468 * buffer big enough to store the socket informations.
1469 */
Olivier Houchardf143b802017-11-04 15:13:01 +01001470 tmpbuf = malloc(MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf886e342017-04-05 22:24:59 +02001471 if (tmpbuf == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001472 ha_warning("Failed to allocate memory to transfer socket informations\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001473 goto out;
1474 }
1475 iov.iov_base = tmpbuf;
1476 while (px) {
1477 struct listener *l;
1478
1479 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1480 int ret;
1481 /* Only transfer IPv4/IPv6 sockets */
Olivier Houchard1fc05162017-04-06 01:05:05 +02001482 if (l->state >= LI_ZOMBIE &&
Olivier Houchardf886e342017-04-05 22:24:59 +02001483 (l->proto->sock_family == AF_INET ||
1484 l->proto->sock_family == AF_INET6 ||
1485 l->proto->sock_family == AF_UNIX)) {
1486 memcpy(&tmpfd[i % MAX_SEND_FD], &l->fd, sizeof(l->fd));
1487 if (!l->netns)
1488 tmpbuf[curoff++] = 0;
1489#ifdef CONFIG_HAP_NS
1490 else {
1491 char *name = l->netns->node.key;
1492 unsigned char len = l->netns->name_len;
1493 tmpbuf[curoff++] = len;
1494 memcpy(tmpbuf + curoff, name, len);
1495 curoff += len;
1496 }
1497#endif
1498 if (l->interface) {
1499 unsigned char len = strlen(l->interface);
1500 tmpbuf[curoff++] = len;
1501 memcpy(tmpbuf + curoff, l->interface, len);
1502 curoff += len;
1503 } else
1504 tmpbuf[curoff++] = 0;
1505 memcpy(tmpbuf + curoff, &l->options,
1506 sizeof(l->options));
1507 curoff += sizeof(l->options);
1508
1509
1510 i++;
1511 } else
1512 continue;
1513 if ((!(i % MAX_SEND_FD))) {
1514 iov.iov_len = curoff;
1515 if (sendmsg(fd, &msghdr, 0) != curoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001516 ha_warning("Failed to transfer sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001517 goto out;
1518 }
1519 /* Wait for an ack */
1520 do {
1521 ret = recv(fd, &tot_fd_nb,
1522 sizeof(tot_fd_nb), 0);
1523 } while (ret == -1 && errno == EINTR);
1524 if (ret <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001525 ha_warning("Unexpected error while transferring sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001526 goto out;
1527 }
1528 curoff = 0;
1529 }
1530
1531 }
1532 px = px->next;
1533 }
1534 if (i % MAX_SEND_FD) {
1535 iov.iov_len = curoff;
1536 cmsg->cmsg_len = CMSG_LEN((i % MAX_SEND_FD) * sizeof(int));
1537 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * (i % MAX_SEND_FD));
1538 if (sendmsg(fd, &msghdr, 0) != curoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001539 ha_warning("Failed to transfer sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001540 goto out;
1541 }
1542 }
1543
1544out:
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001545 if (fd >= 0 && old_fcntl >= 0 && fcntl(fd, F_SETFL, old_fcntl) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001546 ha_warning("Cannot make the unix socket non-blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001547 goto out;
1548 }
1549 appctx->st0 = CLI_ST_END;
1550 free(cmsgbuf);
1551 free(tmpbuf);
1552 return 1;
1553}
1554
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001555static int cli_parse_simple(char **args, char *payload, struct appctx *appctx, void *private)
1556{
1557 if (*args[0] == 'h')
1558 /* help */
1559 cli_gen_usage_msg(appctx);
1560 else if (*args[0] == 'p')
1561 /* prompt */
1562 appctx->st1 ^= APPCTX_CLI_ST1_PROMPT;
1563 else if (*args[0] == 'q')
1564 /* quit */
1565 appctx->st0 = CLI_ST_END;
1566
1567 return 1;
1568}
Olivier Houchardf886e342017-04-05 22:24:59 +02001569
William Lallemand8a022572018-10-26 14:47:35 +02001570/*
1571 * The mworker functions are used to initialize the CLI in the master process
1572 */
1573
1574/*
1575 * Create the mworker CLI proxy
1576 */
1577int mworker_cli_proxy_create()
1578{
1579 struct mworker_proc *child;
1580
1581 mworker_proxy = calloc(1, sizeof(*mworker_proxy));
1582 if (!mworker_proxy)
1583 return -1;
1584
1585 init_new_proxy(mworker_proxy);
1586 mworker_proxy->next = proxies_list;
1587 proxies_list = mworker_proxy;
1588 mworker_proxy->id = strdup("MASTER");
1589 mworker_proxy->mode = PR_MODE_TCP;
1590 mworker_proxy->state = PR_STNEW;
1591 mworker_proxy->last_change = now.tv_sec;
1592 mworker_proxy->cap = PR_CAP_LISTEN; /* this is a listen section */
1593 mworker_proxy->maxconn = 10; /* default to 10 concurrent connections */
1594 mworker_proxy->timeout.client = 0; /* no timeout */
1595 mworker_proxy->conf.file = strdup("MASTER");
1596 mworker_proxy->conf.line = 0;
1597 mworker_proxy->accept = frontend_accept;
1598 mworker_proxy-> lbprm.algo = BE_LB_ALGO_NONE;
1599
1600 /* Does not init the default target the CLI applet, but must be done in
1601 * the request parsing code */
1602 mworker_proxy->default_target = NULL;
1603
1604 /* the check_config_validity() will get an ID for the proxy */
1605 mworker_proxy->uuid = -1;
1606
1607 proxy_store_name(mworker_proxy);
1608
1609 /* create all servers using the mworker_proc list */
1610 list_for_each_entry(child, &proc_list, list) {
1611 char *msg = NULL;
1612 struct server *newsrv = NULL;
1613 struct sockaddr_storage *sk;
1614 int port1, port2, port;
1615 struct protocol *proto;
1616 char *errmsg;
1617
1618 newsrv = new_server(mworker_proxy);
1619 if (!newsrv)
1620 return -1;
1621
1622 /* we don't know the new pid yet */
1623 if (child->pid == -1)
1624 memprintf(&msg, "cur-%d", child->relative_pid);
1625 else
1626 memprintf(&msg, "old-%d", child->pid);
1627
1628 newsrv->next = mworker_proxy->srv;
1629 mworker_proxy->srv = newsrv;
1630 newsrv->conf.file = strdup(msg);
1631 newsrv->id = strdup(msg);
1632 newsrv->conf.line = 0;
1633
1634 memprintf(&msg, "sockpair@%d", child->ipc_fd[0]);
1635 if ((sk = str2sa_range(msg, &port, &port1, &port2, &errmsg, NULL, NULL, 0)) == 0)
1636 return -1;
1637
1638 proto = protocol_by_family(sk->ss_family);
1639 if (!proto || !proto->connect) {
1640 return -1;
1641 }
1642
1643 /* no port specified */
1644 newsrv->flags |= SRV_F_MAPPORTS;
1645 newsrv->addr = *sk;
1646 newsrv->iweight = 1;
1647 newsrv->uweight = 1;
1648 mworker_proxy->srv_act++;
1649 srv_lb_commit_status(newsrv);
1650 }
1651 return 0;
1652}
Olivier Houchardf886e342017-04-05 22:24:59 +02001653
William Lallemandce83b4a2018-10-26 14:47:30 +02001654/*
William Lallemande7361152018-10-26 14:47:36 +02001655 * Create a new listener for the master CLI proxy
1656 */
1657int mworker_cli_proxy_new_listener(char *line)
1658{
1659 struct bind_conf *bind_conf;
1660 struct listener *l;
1661 char *err = NULL;
1662 char *args[MAX_LINE_ARGS + 1];
1663 int arg;
1664 int cur_arg;
1665
1666 arg = 0;
1667 args[0] = line;
1668
1669 /* args is a bind configuration with spaces replaced by commas */
1670 while (*line && arg < MAX_LINE_ARGS) {
1671
1672 if (*line == ',') {
1673 *line++ = '\0';
1674 while (*line == ',')
1675 line++;
1676 args[++arg] = line;
1677 }
1678 line++;
1679 }
1680
1681 args[++arg] = "\0";
1682
1683 bind_conf = bind_conf_alloc(mworker_proxy, "master-socket", 0, "", xprt_get(XPRT_RAW));
1684
1685 bind_conf->level &= ~ACCESS_LVL_MASK;
1686 bind_conf->level |= ACCESS_LVL_ADMIN;
1687
1688 if (!str2listener(args[0], mworker_proxy, bind_conf, "master-socket", 0, &err)) {
1689 ha_alert("Cannot create the listener of the master CLI\n");
1690 return -1;
1691 }
1692
1693 cur_arg = 1;
1694
1695 while (*args[cur_arg]) {
1696 static int bind_dumped;
1697 struct bind_kw *kw;
1698
1699 kw = bind_find_kw(args[cur_arg]);
1700 if (kw) {
1701 if (!kw->parse) {
1702 memprintf(&err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
1703 args[0], args[1], args[cur_arg]);
1704 goto err;
1705 }
1706
1707 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, &err) != 0) {
1708 if (err)
1709 memprintf(&err, "'%s %s' : '%s'", args[0], args[1], err);
1710 else
1711 memprintf(&err, "'%s %s' : error encountered while processing '%s'",
1712 args[0], args[1], args[cur_arg]);
1713 goto err;
1714 }
1715
1716 cur_arg += 1 + kw->skip;
1717 continue;
1718 }
1719
1720 if (!bind_dumped) {
1721 bind_dump_kws(&err);
1722 indent_msg(&err, 4);
1723 bind_dumped = 1;
1724 }
1725
1726 memprintf(&err, "'%s %s' : unknown keyword '%s'.%s%s",
1727 args[0], args[1], args[cur_arg],
1728 err ? " Registered keywords :" : "", err ? err : "");
1729 goto err;
1730 }
1731
1732
1733 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
1734 l->maxconn = 10;
1735 l->backlog = 10;
1736 l->accept = session_accept_fd;
1737 l->default_target = mworker_proxy->default_target;
1738 /* don't make the peers subject to global limits and don't close it in the master */
1739 l->options |= (LI_O_UNLIMITED|LI_O_MWORKER); /* we are keeping this FD in the master */
1740 l->nice = -64; /* we want to boost priority for local stats */
1741 global.maxsock += l->maxconn;
1742 }
1743
1744 return 0;
1745
1746err:
1747 ha_alert("%s\n", err);
1748 return -1;
1749
1750}
1751
1752/*
William Lallemandce83b4a2018-10-26 14:47:30 +02001753 * Create a new CLI socket using a socketpair for a worker process
1754 * <mworker_proc> is the process structure, and <proc> is the process number
1755 */
1756int mworker_cli_sockpair_new(struct mworker_proc *mworker_proc, int proc)
1757{
1758 struct bind_conf *bind_conf;
1759 struct listener *l;
1760 char *path = NULL;
1761 char *err = NULL;
1762
1763 /* master pipe to ensure the master is still alive */
1764 if (socketpair(AF_UNIX, SOCK_STREAM, 0, mworker_proc->ipc_fd) < 0) {
1765 ha_alert("Cannot create worker socketpair.\n");
1766 return -1;
1767 }
1768
1769 /* XXX: we might want to use a separate frontend at some point */
1770 if (!global.stats_fe) {
1771 if ((global.stats_fe = alloc_stats_fe("GLOBAL", "master-socket", 0)) == NULL) {
1772 ha_alert("out of memory trying to allocate the stats frontend");
1773 return -1;
1774 }
1775 }
1776
1777 bind_conf = bind_conf_alloc(global.stats_fe, "master-socket", 0, "", xprt_get(XPRT_RAW));
1778 bind_conf->level &= ~ACCESS_LVL_MASK;
1779 bind_conf->level |= ACCESS_LVL_ADMIN; /* TODO: need to lower the rights with a CLI keyword*/
1780
1781 bind_conf->bind_proc = 1UL << proc;
1782 global.stats_fe->bind_proc = 0; /* XXX: we should be careful with that, it can be removed by configuration */
1783
1784 if (!memprintf(&path, "sockpair@%d", mworker_proc->ipc_fd[1])) {
1785 ha_alert("Cannot allocate listener.\n");
1786 return -1;
1787 }
1788
1789 if (!str2listener(path, global.stats_fe, bind_conf, "master-socket", 0, &err)) {
1790 ha_alert("Cannot create a CLI sockpair listener for process #%d\n", proc);
1791 return -1;
1792 }
1793
1794 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
1795 l->maxconn = global.stats_fe->maxconn;
1796 l->backlog = global.stats_fe->backlog;
1797 l->accept = session_accept_fd;
1798 l->default_target = global.stats_fe->default_target;
1799 l->options |= LI_O_UNLIMITED;
1800 /* it's a sockpair but we don't want to keep the fd in the master */
1801 l->options &= ~LI_O_INHERITED;
1802 l->nice = -64; /* we want to boost priority for local stats */
1803 global.maxsock += l->maxconn;
1804 }
1805
1806 return 0;
1807}
1808
William Lallemand74c24fb2016-11-21 17:18:36 +01001809static struct applet cli_applet = {
1810 .obj_type = OBJ_TYPE_APPLET,
1811 .name = "<CLI>", /* used for logging */
1812 .fct = cli_io_handler,
1813 .release = cli_release_handler,
1814};
1815
Willy Tarreau0a739292016-11-22 20:21:23 +01001816/* register cli keywords */
1817static struct cli_kw_list cli_kws = {{ },{
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001818 { { "help", NULL }, NULL, cli_parse_simple, NULL },
1819 { { "prompt", NULL }, NULL, cli_parse_simple, NULL },
1820 { { "quit", NULL }, NULL, cli_parse_simple, NULL },
Willy Tarreau2af99412016-11-23 11:10:59 +01001821 { { "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 +01001822 { { "set", "rate-limit", NULL }, "set rate-limit : change a rate limiting value", cli_parse_set_ratelimit, NULL },
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001823 { { "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 +01001824 { { "set", "timeout", NULL }, "set timeout : change a timeout setting", cli_parse_set_timeout, NULL, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001825 { { "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 +01001826 { { "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 +02001827 { { "show", "fd", NULL }, "show fd [num] : dump list of file descriptors in use", cli_parse_show_fd, cli_io_handler_show_fd, NULL },
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001828 { { "show", "activity", NULL }, "show activity : show per-thread activity stats (for support/developers)", cli_parse_default, cli_io_handler_show_activity, NULL },
Olivier Houchardf886e342017-04-05 22:24:59 +02001829 { { "_getsocks", NULL }, NULL, _getsocks, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001830 {{},}
1831}};
1832
William Lallemand74c24fb2016-11-21 17:18:36 +01001833static struct cfg_kw_list cfg_kws = {ILH, {
1834 { CFG_GLOBAL, "stats", stats_parse_global },
1835 { 0, NULL, NULL },
1836}};
1837
1838static struct bind_kw_list bind_kws = { "STAT", { }, {
William Lallemandf6975e92017-05-26 17:42:10 +02001839 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
1840 { "expose-fd", bind_parse_expose_fd, 1 }, /* set the unix socket expose fd rights */
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001841 { "severity-output", bind_parse_severity_output, 1 }, /* set the severity output format */
William Lallemand74c24fb2016-11-21 17:18:36 +01001842 { NULL, NULL, 0 },
1843}};
1844
1845__attribute__((constructor))
1846static void __dumpstats_module_init(void)
1847{
1848 cfg_register_keywords(&cfg_kws);
Willy Tarreau0a739292016-11-22 20:21:23 +01001849 cli_register_kw(&cli_kws);
William Lallemand74c24fb2016-11-21 17:18:36 +01001850 bind_register_keywords(&bind_kws);
1851}
1852
1853/*
1854 * Local variables:
1855 * c-indent-level: 8
1856 * c-basic-offset: 8
1857 * End:
1858 */