blob: 4440f251642b1400800bdbb551150c087ee26869 [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/proxy.h>
62#include <proto/sample.h>
63#include <proto/session.h>
64#include <proto/stream.h>
65#include <proto/server.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066#include <proto/stream_interface.h>
67#include <proto/task.h>
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +020068#include <proto/proto_udp.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010069
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020070#define PAYLOAD_PATTERN "<<"
71
William Lallemand74c24fb2016-11-21 17:18:36 +010072static struct applet cli_applet;
73
74static const char stats_sock_usage_msg[] =
75 "Unknown command. Please enter one of the following commands only :\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010076 " help : this message\n"
77 " prompt : toggle interactive mode with prompt\n"
78 " quit : disconnect\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010079 "";
80
81static const char stats_permission_denied_msg[] =
82 "Permission denied\n"
83 "";
84
85
Christopher Faulet1bc04c72017-10-29 20:14:08 +010086static THREAD_LOCAL char *dynamic_usage_msg = NULL;
William Lallemand74c24fb2016-11-21 17:18:36 +010087
88/* List head of cli keywords */
89static struct cli_kw_list cli_keywords = {
90 .list = LIST_HEAD_INIT(cli_keywords.list)
91};
92
93extern const char *stat_status_codes[];
94
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020095static char *cli_gen_usage_msg(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +010096{
97 struct cli_kw_list *kw_list;
98 struct cli_kw *kw;
99 struct chunk *tmp = get_trash_chunk();
100 struct chunk out;
101
102 free(dynamic_usage_msg);
103 dynamic_usage_msg = NULL;
104
105 if (LIST_ISEMPTY(&cli_keywords.list))
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200106 goto end;
William Lallemand74c24fb2016-11-21 17:18:36 +0100107
108 chunk_reset(tmp);
109 chunk_strcat(tmp, stats_sock_usage_msg);
110 list_for_each_entry(kw_list, &cli_keywords.list, list) {
111 kw = &kw_list->kw[0];
William Lallemand0154edc2018-05-15 11:50:04 +0200112 while (kw->str_kw[0]) {
113 if (kw->usage)
114 chunk_appendf(tmp, " %s\n", kw->usage);
William Lallemand74c24fb2016-11-21 17:18:36 +0100115 kw++;
116 }
117 }
118 chunk_init(&out, NULL, 0);
119 chunk_dup(&out, tmp);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200120 dynamic_usage_msg = out.area;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200121
122end:
123 if (dynamic_usage_msg) {
124 appctx->ctx.cli.severity = LOG_INFO;
125 appctx->ctx.cli.msg = dynamic_usage_msg;
126 }
127 else {
128 appctx->ctx.cli.severity = LOG_INFO;
129 appctx->ctx.cli.msg = stats_sock_usage_msg;
130 }
131 appctx->st0 = CLI_ST_PRINT;
132
William Lallemand74c24fb2016-11-21 17:18:36 +0100133 return dynamic_usage_msg;
134}
135
136struct cli_kw* cli_find_kw(char **args)
137{
138 struct cli_kw_list *kw_list;
139 struct cli_kw *kw;/* current cli_kw */
140 char **tmp_args;
141 const char **tmp_str_kw;
142 int found = 0;
143
144 if (LIST_ISEMPTY(&cli_keywords.list))
145 return NULL;
146
147 list_for_each_entry(kw_list, &cli_keywords.list, list) {
148 kw = &kw_list->kw[0];
149 while (*kw->str_kw) {
150 tmp_args = args;
151 tmp_str_kw = kw->str_kw;
152 while (*tmp_str_kw) {
153 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
154 found = 1;
155 } else {
156 found = 0;
157 break;
158 }
159 tmp_args++;
160 tmp_str_kw++;
161 }
162 if (found)
163 return (kw);
164 kw++;
165 }
166 }
167 return NULL;
168}
169
170void cli_register_kw(struct cli_kw_list *kw_list)
171{
172 LIST_ADDQ(&cli_keywords.list, &kw_list->list);
173}
174
175
176/* allocate a new stats frontend named <name>, and return it
177 * (or NULL in case of lack of memory).
178 */
179static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
180{
181 struct proxy *fe;
182
183 fe = calloc(1, sizeof(*fe));
184 if (!fe)
185 return NULL;
186
187 init_new_proxy(fe);
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100188 fe->next = proxies_list;
189 proxies_list = fe;
William Lallemand74c24fb2016-11-21 17:18:36 +0100190 fe->last_change = now.tv_sec;
191 fe->id = strdup("GLOBAL");
192 fe->cap = PR_CAP_FE;
193 fe->maxconn = 10; /* default to 10 concurrent connections */
194 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
195 fe->conf.file = strdup(file);
196 fe->conf.line = line;
197 fe->accept = frontend_accept;
198 fe->default_target = &cli_applet.obj_type;
199
200 /* the stats frontend is the only one able to assign ID #0 */
201 fe->conf.id.key = fe->uuid = 0;
202 eb32_insert(&used_proxy_id, &fe->conf.id);
203 return fe;
204}
205
206/* This function parses a "stats" statement in the "global" section. It returns
207 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
208 * error message into the <err> buffer which will be preallocated. The trailing
209 * '\n' must not be written. The function must be called with <args> pointing to
210 * the first word after "stats".
211 */
212static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
213 struct proxy *defpx, const char *file, int line,
214 char **err)
215{
216 struct bind_conf *bind_conf;
217 struct listener *l;
218
219 if (!strcmp(args[1], "socket")) {
220 int cur_arg;
221
222 if (*args[2] == 0) {
223 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
224 return -1;
225 }
226
227 if (!global.stats_fe) {
228 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
229 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
230 return -1;
231 }
232 }
233
Willy Tarreaua261e9b2016-12-22 20:44:00 +0100234 bind_conf = bind_conf_alloc(global.stats_fe, file, line, args[2], xprt_get(XPRT_RAW));
William Lallemand07a62f72017-05-24 00:57:40 +0200235 bind_conf->level &= ~ACCESS_LVL_MASK;
236 bind_conf->level |= ACCESS_LVL_OPER; /* default access level */
William Lallemand74c24fb2016-11-21 17:18:36 +0100237
238 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
239 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
240 file, line, args[0], args[1], err && *err ? *err : "error");
241 return -1;
242 }
243
244 cur_arg = 3;
245 while (*args[cur_arg]) {
246 static int bind_dumped;
247 struct bind_kw *kw;
248
249 kw = bind_find_kw(args[cur_arg]);
250 if (kw) {
251 if (!kw->parse) {
252 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
253 args[0], args[1], args[cur_arg]);
254 return -1;
255 }
256
257 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
258 if (err && *err)
259 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
260 else
261 memprintf(err, "'%s %s' : error encountered while processing '%s'",
262 args[0], args[1], args[cur_arg]);
263 return -1;
264 }
265
266 cur_arg += 1 + kw->skip;
267 continue;
268 }
269
270 if (!bind_dumped) {
271 bind_dump_kws(err);
272 indent_msg(err, 4);
273 bind_dumped = 1;
274 }
275
276 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
277 args[0], args[1], args[cur_arg],
278 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
279 return -1;
280 }
281
282 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
283 l->maxconn = global.stats_fe->maxconn;
284 l->backlog = global.stats_fe->backlog;
285 l->accept = session_accept_fd;
William Lallemand74c24fb2016-11-21 17:18:36 +0100286 l->default_target = global.stats_fe->default_target;
287 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
288 l->nice = -64; /* we want to boost priority for local stats */
289 global.maxsock += l->maxconn;
290 }
291 }
292 else if (!strcmp(args[1], "timeout")) {
293 unsigned timeout;
294 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
295
296 if (res) {
297 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
298 return -1;
299 }
300
301 if (!timeout) {
302 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
303 return -1;
304 }
305 if (!global.stats_fe) {
306 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
307 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
308 return -1;
309 }
310 }
311 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
312 }
313 else if (!strcmp(args[1], "maxconn")) {
314 int maxconn = atol(args[2]);
315
316 if (maxconn <= 0) {
317 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
318 return -1;
319 }
320
321 if (!global.stats_fe) {
322 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
323 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
324 return -1;
325 }
326 }
327 global.stats_fe->maxconn = maxconn;
328 }
329 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
330 int cur_arg = 2;
331 unsigned long set = 0;
332
333 if (!global.stats_fe) {
334 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
335 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
336 return -1;
337 }
338 }
339
340 while (*args[cur_arg]) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100341 if (strcmp(args[cur_arg], "all") == 0) {
342 set = 0;
343 break;
344 }
Christopher Faulet26028f62017-11-22 15:01:51 +0100345 if (parse_process_number(args[cur_arg], &set, NULL, err)) {
Christopher Fauletf1f0c5f2017-11-22 12:06:43 +0100346 memprintf(err, "'%s %s' : %s", args[0], args[1], *err);
William Lallemand74c24fb2016-11-21 17:18:36 +0100347 return -1;
348 }
349 cur_arg++;
350 }
351 global.stats_fe->bind_proc = set;
352 }
353 else {
354 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
355 return -1;
356 }
357 return 0;
358}
359
Willy Tarreaude57a572016-11-23 17:01:39 +0100360/* Verifies that the CLI at least has a level at least as high as <level>
361 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
362 * failure, an error message is prepared and the appctx's state is adjusted
363 * to print it so that a return 1 is enough to abort any processing.
364 */
365int cli_has_level(struct appctx *appctx, int level)
366{
367 struct stream_interface *si = appctx->owner;
368 struct stream *s = si_strm(si);
369
William Lallemand07a62f72017-05-24 00:57:40 +0200370 if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < level) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200371 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaude57a572016-11-23 17:01:39 +0100372 appctx->ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100373 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaude57a572016-11-23 17:01:39 +0100374 return 0;
375 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100376 return 1;
377}
378
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200379/* Returns severity_output for the current session if set, or default for the socket */
380static int cli_get_severity_output(struct appctx *appctx)
381{
382 if (appctx->cli_severity_output)
383 return appctx->cli_severity_output;
384 return strm_li(si_strm(appctx->owner))->bind_conf->severity_output;
385}
William Lallemand74c24fb2016-11-21 17:18:36 +0100386
Willy Tarreau41908562016-11-24 16:23:38 +0100387/* Processes the CLI interpreter on the stats socket. This function is called
388 * from the CLI's IO handler running in an appctx context. The function returns 1
389 * if the request was understood, otherwise zero. It is called with appctx->st0
390 * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do
391 * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to
392 * have its own I/O handler called again. Most of the time, parsers will only
393 * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg.
Willy Tarreaueaffde32016-12-16 17:59:25 +0100394 * If a keyword parser is NULL and an I/O handler is declared, the I/O handler
395 * will automatically be used.
William Lallemand74c24fb2016-11-21 17:18:36 +0100396 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200397static int cli_parse_request(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100398{
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200399 char *args[MAX_STATS_ARGS + 1], *p, *end, *payload = NULL;
400 int i = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100401 struct cli_kw *kw;
William Lallemand74c24fb2016-11-21 17:18:36 +0100402
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200403 appctx->st2 = 0;
404 memset(&appctx->ctx.cli, 0, sizeof(appctx->ctx.cli));
William Lallemand74c24fb2016-11-21 17:18:36 +0100405
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200406 p = appctx->chunk->area;
407 end = p + appctx->chunk->data;
William Lallemand74c24fb2016-11-21 17:18:36 +0100408
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200409 /*
410 * Get the payload start if there is one.
411 * For the sake of simplicity, the payload pattern is looked up
412 * everywhere from the start of the input but it can only be found
413 * at the end of the first line if APPCTX_CLI_ST1_PAYLOAD is set.
414 *
415 * The input string was zero terminated so it is safe to use
416 * the str*() functions throughout the parsing
417 */
418 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
419 payload = strstr(p, PAYLOAD_PATTERN);
420 end = payload;
421 /* skip the pattern */
422 payload += strlen(PAYLOAD_PATTERN);
423 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100424
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200425 /*
426 * Get pointers on words.
427 * One extra slot is reserved to store a pointer on a null byte.
428 */
429 while (i < MAX_STATS_ARGS && p < end) {
430 int j, k;
William Lallemand74c24fb2016-11-21 17:18:36 +0100431
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200432 /* skip leading spaces/tabs */
433 p += strspn(p, " \t");
434 if (!*p)
435 break;
William Lallemand74c24fb2016-11-21 17:18:36 +0100436
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200437 args[i] = p;
438 p += strcspn(p, " \t");
439 *p++ = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100440
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200441 /* unescape backslashes (\) */
442 for (j = 0, k = 0; args[i][k]; k++) {
443 if (args[i][k] == '\\') {
444 if (args[i][k + 1] == '\\')
445 k++;
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100446 else
447 continue;
448 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200449 args[i][j] = args[i][k];
William Lallemand74c24fb2016-11-21 17:18:36 +0100450 j++;
451 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200452 args[i][j] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100453
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200454 i++;
455 }
456 /* fill unused slots */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200457 p = appctx->chunk->area + appctx->chunk->data;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200458 for (; i < MAX_STATS_ARGS + 1; i++)
459 args[i] = p;
Willy Tarreau41908562016-11-24 16:23:38 +0100460
461 kw = cli_find_kw(args);
Willy Tarreaueaffde32016-12-16 17:59:25 +0100462 if (!kw)
Willy Tarreau41908562016-11-24 16:23:38 +0100463 return 0;
464
465 appctx->io_handler = kw->io_handler;
Emeric Brund6871f72017-06-29 19:54:13 +0200466 appctx->io_release = kw->io_release;
467 /* kw->parse could set its own io_handler or ip_release handler */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200468 if ((!kw->parse || kw->parse(args, payload, appctx, kw->private) == 0) && appctx->io_handler) {
Willy Tarreau41908562016-11-24 16:23:38 +0100469 appctx->st0 = CLI_ST_CALLBACK;
William Lallemand74c24fb2016-11-21 17:18:36 +0100470 }
Willy Tarreau41908562016-11-24 16:23:38 +0100471 return 1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100472}
473
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200474/* prepends then outputs the argument msg with a syslog-type severity depending on severity_output value */
475static int cli_output_msg(struct channel *chn, const char *msg, int severity, int severity_output)
476{
477 struct chunk *tmp;
478
479 if (likely(severity_output == CLI_SEVERITY_NONE))
Willy Tarreau06d80a92017-10-19 14:32:15 +0200480 return ci_putblk(chn, msg, strlen(msg));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200481
482 tmp = get_trash_chunk();
483 chunk_reset(tmp);
484
485 if (severity < 0 || severity > 7) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100486 ha_warning("socket command feedback with invalid severity %d", severity);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200487 chunk_printf(tmp, "[%d]: ", severity);
488 }
489 else {
490 switch (severity_output) {
491 case CLI_SEVERITY_NUMBER:
492 chunk_printf(tmp, "[%d]: ", severity);
493 break;
494 case CLI_SEVERITY_STRING:
495 chunk_printf(tmp, "[%s]: ", log_levels[severity]);
496 break;
497 default:
Christopher Faulet767a84b2017-11-24 16:50:31 +0100498 ha_warning("Unrecognized severity output %d", severity_output);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200499 }
500 }
501 chunk_appendf(tmp, "%s", msg);
502
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 return ci_putblk(chn, tmp->area, strlen(tmp->area));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200504}
505
William Lallemand74c24fb2016-11-21 17:18:36 +0100506/* This I/O handler runs as an applet embedded in a stream interface. It is
507 * used to processes I/O from/to the stats unix socket. The system relies on a
508 * state machine handling requests and various responses. We read a request,
509 * then we process it and send the response, and we possibly display a prompt.
510 * Then we can read again. The state is stored in appctx->st0 and is one of the
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100511 * CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled
William Lallemand74c24fb2016-11-21 17:18:36 +0100512 * or not.
513 */
514static void cli_io_handler(struct appctx *appctx)
515{
516 struct stream_interface *si = appctx->owner;
517 struct channel *req = si_oc(si);
518 struct channel *res = si_ic(si);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200519 struct bind_conf *bind_conf = strm_li(si_strm(si))->bind_conf;
William Lallemand74c24fb2016-11-21 17:18:36 +0100520 int reql;
521 int len;
522
523 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
524 goto out;
525
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100526 /* Check if the input buffer is avalaible. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200527 if (res->buf.size == 0) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100528 si_applet_cant_put(si);
529 goto out;
530 }
531
William Lallemand74c24fb2016-11-21 17:18:36 +0100532 while (1) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100533 if (appctx->st0 == CLI_ST_INIT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100534 /* Stats output not initialized yet */
535 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200536 /* reset severity to default at init */
537 appctx->cli_severity_output = bind_conf->severity_output;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100538 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100539 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100540 else if (appctx->st0 == CLI_ST_END) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100541 /* Let's close for real now. We just close the request
542 * side, the conditions below will complete if needed.
543 */
544 si_shutw(si);
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200545 free_trash_chunk(appctx->chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +0100546 break;
547 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100548 else if (appctx->st0 == CLI_ST_GETREQ) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200549 char *str;
550
551 /* use a trash chunk to store received data */
552 if (!appctx->chunk) {
553 appctx->chunk = alloc_trash_chunk();
554 if (!appctx->chunk) {
555 appctx->st0 = CLI_ST_END;
556 continue;
557 }
558 }
559
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200560 str = appctx->chunk->area + appctx->chunk->data;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200561
William Lallemand74c24fb2016-11-21 17:18:36 +0100562 /* ensure we have some output room left in the event we
563 * would want to return some info right after parsing.
564 */
565 if (buffer_almost_full(si_ib(si))) {
566 si_applet_cant_put(si);
567 break;
568 }
569
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200570 /* '- 1' is to ensure a null byte can always be inserted at the end */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200571 reql = co_getline(si_oc(si), str,
572 appctx->chunk->size - appctx->chunk->data - 1);
William Lallemand74c24fb2016-11-21 17:18:36 +0100573 if (reql <= 0) { /* closed or EOL not found */
574 if (reql == 0)
575 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100576 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100577 continue;
578 }
579
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200580 if (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)) {
581 /* seek for a possible unescaped semi-colon. If we find
582 * one, we replace it with an LF and skip only this part.
583 */
584 for (len = 0; len < reql; len++) {
585 if (str[len] == '\\') {
586 len++;
587 continue;
588 }
589 if (str[len] == ';') {
590 str[len] = '\n';
591 reql = len + 1;
592 break;
593 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100594 }
595 }
596
597 /* now it is time to check that we have a full line,
598 * remove the trailing \n and possibly \r, then cut the
599 * line.
600 */
601 len = reql - 1;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200602 if (str[len] != '\n') {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100603 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100604 continue;
605 }
606
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200607 if (len && str[len-1] == '\r')
William Lallemand74c24fb2016-11-21 17:18:36 +0100608 len--;
609
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200610 str[len] = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200611 appctx->chunk->data += len;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200612
613 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200614 appctx->chunk->area[appctx->chunk->data] = '\n';
615 appctx->chunk->area[appctx->chunk->data + 1] = 0;
616 appctx->chunk->data++;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200617 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100618
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100619 appctx->st0 = CLI_ST_PROMPT;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200620
621 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
622 /* empty line */
623 if (!len) {
624 /* remove the last two \n */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200625 appctx->chunk->data -= 2;
626 appctx->chunk->area[appctx->chunk->data] = 0;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200627
628 if (!cli_parse_request(appctx))
629 cli_gen_usage_msg(appctx);
630
631 chunk_reset(appctx->chunk);
632 /* NB: cli_sock_parse_request() may have put
633 * another CLI_ST_O_* into appctx->st0.
634 */
635
636 appctx->st1 &= ~APPCTX_CLI_ST1_PAYLOAD;
William Lallemand74c24fb2016-11-21 17:18:36 +0100637 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100638 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200639 else {
640 /*
641 * Look for the "payload start" pattern at the end of a line
642 * Its location is not remembered here, this is just to switch
643 * to a gathering mode.
William Lallemand74c24fb2016-11-21 17:18:36 +0100644 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200645 if (!strcmp(appctx->chunk->area + appctx->chunk->data - strlen(PAYLOAD_PATTERN), PAYLOAD_PATTERN))
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200646 appctx->st1 |= APPCTX_CLI_ST1_PAYLOAD;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200647 else {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200648 /* no payload, the command is complete: parse the request */
649 if (!cli_parse_request(appctx))
650 cli_gen_usage_msg(appctx);
651
652 chunk_reset(appctx->chunk);
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200653 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100654 }
655
656 /* re-adjust req buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200657 co_skip(si_oc(si), reql);
William Lallemand74c24fb2016-11-21 17:18:36 +0100658 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
659 }
660 else { /* output functions */
661 switch (appctx->st0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100662 case CLI_ST_PROMPT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100663 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100664 case CLI_ST_PRINT:
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200665 if (cli_output_msg(res, appctx->ctx.cli.msg, appctx->ctx.cli.severity,
666 cli_get_severity_output(appctx)) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100667 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100668 else
669 si_applet_cant_put(si);
670 break;
Aurélien Nephtalic511b7c2018-04-16 18:50:19 +0200671 case CLI_ST_PRINT_FREE: {
672 const char *msg = appctx->ctx.cli.err;
673
674 if (!msg)
675 msg = "Out of memory.\n";
676
677 if (cli_output_msg(res, msg, LOG_ERR, cli_get_severity_output(appctx)) != -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100678 free(appctx->ctx.cli.err);
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100679 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100680 }
681 else
682 si_applet_cant_put(si);
683 break;
Aurélien Nephtalic511b7c2018-04-16 18:50:19 +0200684 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100685 case CLI_ST_CALLBACK: /* use custom pointer */
William Lallemand74c24fb2016-11-21 17:18:36 +0100686 if (appctx->io_handler)
687 if (appctx->io_handler(appctx)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100688 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100689 if (appctx->io_release) {
690 appctx->io_release(appctx);
691 appctx->io_release = NULL;
692 }
693 }
694 break;
695 default: /* abnormal state */
696 si->flags |= SI_FL_ERR;
697 break;
698 }
699
700 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100701 if (appctx->st0 == CLI_ST_PROMPT) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200702 const char *prompt = "";
703
704 if (appctx->st1 & APPCTX_CLI_ST1_PROMPT) {
705 /*
706 * when entering a payload with interactive mode, change the prompt
707 * to emphasize that more data can still be sent
708 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200709 if (appctx->chunk->data && appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200710 prompt = "+ ";
711 else
712 prompt = "\n> ";
713 }
714 else {
715 if (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD))
716 prompt = "\n";
717 }
718
719 if (ci_putstr(si_ic(si), prompt) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100720 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100721 else
722 si_applet_cant_put(si);
723 }
724
725 /* If the output functions are still there, it means they require more room. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100726 if (appctx->st0 >= CLI_ST_OUTPUT)
William Lallemand74c24fb2016-11-21 17:18:36 +0100727 break;
728
729 /* Now we close the output if one of the writers did so,
730 * or if we're not in interactive mode and the request
731 * buffer is empty. This still allows pipelined requests
732 * to be sent in non-interactive mode.
733 */
Willy Tarreau851d12c2018-06-19 07:01:36 +0200734 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!(appctx->st1 & APPCTX_CLI_ST1_PROMPT) && !co_data(req))) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100735 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100736 continue;
737 }
738
739 /* switch state back to GETREQ to read next requests */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100740 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100741 }
742 }
743
744 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
745 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
746 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
747 /* Other side has closed, let's abort if we have no more processing to do
748 * and nothing more to consume. This is comparable to a broken pipe, so
749 * we forward the close to the request side so that it flows upstream to
750 * the client.
751 */
752 si_shutw(si);
753 }
754
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100755 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < CLI_ST_OUTPUT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100756 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
757 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
758 /* We have no more processing to do, and nothing more to send, and
759 * the client side has closed. So we'll forward this state downstream
760 * on the response buffer.
761 */
762 si_shutr(si);
763 res->flags |= CF_READ_NULL;
764 }
765
766 out:
767 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
768 __FUNCTION__, __LINE__,
769 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
770}
771
William Lallemand74c24fb2016-11-21 17:18:36 +0100772/* This is called when the stream interface is closed. For instance, upon an
773 * external abort, we won't call the i/o handler anymore so we may need to
774 * remove back references to the stream currently being dumped.
775 */
776static void cli_release_handler(struct appctx *appctx)
777{
778 if (appctx->io_release) {
779 appctx->io_release(appctx);
780 appctx->io_release = NULL;
781 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100782 else if (appctx->st0 == CLI_ST_PRINT_FREE) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100783 free(appctx->ctx.cli.err);
784 appctx->ctx.cli.err = NULL;
785 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100786}
787
788/* This function dumps all environmnent variables to the buffer. It returns 0
789 * if the output buffer is full and it needs to be called again, otherwise
Willy Tarreauf6710f82016-12-16 17:45:44 +0100790 * non-zero. Dumps only one entry if st2 == STAT_ST_END. It uses cli.p0 as the
791 * pointer to the current variable.
William Lallemand74c24fb2016-11-21 17:18:36 +0100792 */
Willy Tarreau0a739292016-11-22 20:21:23 +0100793static int cli_io_handler_show_env(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100794{
Willy Tarreau0a739292016-11-22 20:21:23 +0100795 struct stream_interface *si = appctx->owner;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100796 char **var = appctx->ctx.cli.p0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100797
798 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
799 return 1;
800
801 chunk_reset(&trash);
802
803 /* we have two inner loops here, one for the proxy, the other one for
804 * the buffer.
805 */
Willy Tarreauf6710f82016-12-16 17:45:44 +0100806 while (*var) {
807 chunk_printf(&trash, "%s\n", *var);
William Lallemand74c24fb2016-11-21 17:18:36 +0100808
Willy Tarreau06d80a92017-10-19 14:32:15 +0200809 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100810 si_applet_cant_put(si);
811 return 0;
812 }
813 if (appctx->st2 == STAT_ST_END)
814 break;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100815 var++;
816 appctx->ctx.cli.p0 = var;
William Lallemand74c24fb2016-11-21 17:18:36 +0100817 }
818
819 /* dump complete */
820 return 1;
821}
822
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200823/* This function dumps all file descriptors states (or the requested one) to
824 * the buffer. It returns 0 if the output buffer is full and it needs to be
825 * called again, otherwise non-zero. Dumps only one entry if st2 == STAT_ST_END.
826 * It uses cli.i0 as the fd number to restart from.
827 */
828static int cli_io_handler_show_fd(struct appctx *appctx)
829{
830 struct stream_interface *si = appctx->owner;
831 int fd = appctx->ctx.cli.i0;
832
833 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
834 return 1;
835
836 chunk_reset(&trash);
837
838 /* we have two inner loops here, one for the proxy, the other one for
839 * the buffer.
840 */
Aurélien Nephtali498a1152018-03-09 18:51:16 +0100841 while (fd >= 0 && fd < global.maxsock) {
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200842 struct fdtab fdt;
Willy Tarreau286ec682017-08-09 16:35:44 +0200843 struct listener *li = NULL;
844 struct server *sv = NULL;
845 struct proxy *px = NULL;
Willy Tarreaua833cd92018-03-29 13:19:37 +0200846 const struct mux_ops *mux = NULL;
Willy Tarreau35b1b482018-03-28 18:41:30 +0200847 void *ctx = NULL;
Willy Tarreau286ec682017-08-09 16:35:44 +0200848 uint32_t conn_flags = 0;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200849
850 fdt = fdtab[fd];
851
Willy Tarreau017af242017-10-04 20:24:54 +0200852 if (!fdt.owner)
853 goto skip; // closed
854
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200855 if (fdt.iocb == conn_fd_handler) {
856 conn_flags = ((struct connection *)fdt.owner)->flags;
Willy Tarreau35b1b482018-03-28 18:41:30 +0200857 mux = ((struct connection *)fdt.owner)->mux;
858 ctx = ((struct connection *)fdt.owner)->mux_ctx;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200859 li = objt_listener(((struct connection *)fdt.owner)->target);
860 sv = objt_server(((struct connection *)fdt.owner)->target);
861 px = objt_proxy(((struct connection *)fdt.owner)->target);
862 }
863 else if (fdt.iocb == listener_accept)
864 li = fdt.owner;
865
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200866 chunk_printf(&trash,
Willy Tarreauc754b342018-03-30 15:00:15 +0200867 " %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 +0200868 fd,
869 fdt.state,
870 (fdt.state & FD_EV_POLLED_R) ? 'P' : 'p',
871 (fdt.state & FD_EV_READY_R) ? 'R' : 'r',
872 (fdt.state & FD_EV_ACTIVE_R) ? 'A' : 'a',
873 (fdt.state & FD_EV_POLLED_W) ? 'P' : 'p',
874 (fdt.state & FD_EV_READY_W) ? 'R' : 'r',
875 (fdt.state & FD_EV_ACTIVE_W) ? 'A' : 'a',
876 fdt.ev,
877 (fdt.ev & FD_POLL_HUP) ? 'H' : 'h',
878 (fdt.ev & FD_POLL_ERR) ? 'E' : 'e',
879 (fdt.ev & FD_POLL_OUT) ? 'O' : 'o',
880 (fdt.ev & FD_POLL_PRI) ? 'P' : 'p',
881 (fdt.ev & FD_POLL_IN) ? 'I' : 'i',
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200882 fdt.linger_risk ? 'L' : 'l',
883 fdt.cloned ? 'C' : 'c',
Willy Tarreauc754b342018-03-30 15:00:15 +0200884 fdt.cache.next,
885 fdt.cache.prev,
886 fdt.thread_mask, fdt.update_mask,
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200887 fdt.owner,
888 fdt.iocb,
889 (fdt.iocb == conn_fd_handler) ? "conn_fd_handler" :
890 (fdt.iocb == dgram_fd_handler) ? "dgram_fd_handler" :
891 (fdt.iocb == listener_accept) ? "listener_accept" :
Willy Tarreau4037a3f2018-03-28 18:06:47 +0200892 (fdt.iocb == thread_sync_io_handler) ? "thread_sync_io_handler" :
Willy Tarreauc754b342018-03-30 15:00:15 +0200893 "unknown");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200894
895 if (fdt.iocb == conn_fd_handler) {
896 chunk_appendf(&trash, " cflg=0x%08x", conn_flags);
897 if (px)
898 chunk_appendf(&trash, " px=%s", px->id);
899 else if (sv)
900 chunk_appendf(&trash, " sv=%s/%s", sv->id, sv->proxy->id);
901 else if (li)
902 chunk_appendf(&trash, " fe=%s", li->bind_conf->frontend->id);
Willy Tarreau35b1b482018-03-28 18:41:30 +0200903
Willy Tarreaub011d8f2018-03-30 14:41:19 +0200904 if (mux) {
Willy Tarreau35b1b482018-03-28 18:41:30 +0200905 chunk_appendf(&trash, " mux=%s mux_ctx=%p", mux->name, ctx);
Willy Tarreaub011d8f2018-03-30 14:41:19 +0200906 if (mux->show_fd)
907 mux->show_fd(&trash, fdt.owner);
908 }
Willy Tarreau35b1b482018-03-28 18:41:30 +0200909 else
910 chunk_appendf(&trash, " nomux");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200911 }
912 else if (fdt.iocb == listener_accept) {
913 chunk_appendf(&trash, " l.st=%s fe=%s",
914 listener_state_str(li),
915 li->bind_conf->frontend->id);
916 }
917
918 chunk_appendf(&trash, "\n");
919
Willy Tarreau06d80a92017-10-19 14:32:15 +0200920 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200921 si_applet_cant_put(si);
922 return 0;
923 }
924 skip:
925 if (appctx->st2 == STAT_ST_END)
926 break;
927
928 fd++;
929 appctx->ctx.cli.i0 = fd;
930 }
931
932 /* dump complete */
933 return 1;
934}
935
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100936/* This function dumps some activity counters used by developers and support to
937 * rule out some hypothesis during bug reports. It returns 0 if the output
938 * buffer is full and it needs to be called again, otherwise non-zero. It dumps
939 * everything at once in the buffer and is not designed to do it in multiple
940 * passes.
941 */
942static int cli_io_handler_show_activity(struct appctx *appctx)
943{
944 struct stream_interface *si = appctx->owner;
945 int thr;
946
947 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
948 return 1;
949
950 chunk_reset(&trash);
951
952 chunk_appendf(&trash, "thread_id: %u", tid);
953 chunk_appendf(&trash, "\ndate_now: %lu.%06lu", (long)now.tv_sec, (long)now.tv_usec);
954 chunk_appendf(&trash, "\nloops:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].loops);
955 chunk_appendf(&trash, "\nwake_cache:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].wake_cache);
956 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 +0100957 chunk_appendf(&trash, "\nwake_signal:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].wake_signal);
958 chunk_appendf(&trash, "\npoll_exp:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_exp);
959 chunk_appendf(&trash, "\npoll_drop:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_drop);
960 chunk_appendf(&trash, "\npoll_dead:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_dead);
961 chunk_appendf(&trash, "\npoll_skip:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_skip);
962 chunk_appendf(&trash, "\nfd_skip:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].fd_skip);
963 chunk_appendf(&trash, "\nfd_lock:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].fd_lock);
964 chunk_appendf(&trash, "\nfd_del:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].fd_del);
965 chunk_appendf(&trash, "\nconn_dead:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].conn_dead);
966 chunk_appendf(&trash, "\nstream:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].stream);
967 chunk_appendf(&trash, "\nempty_rq:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].empty_rq);
968 chunk_appendf(&trash, "\nlong_rq:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].long_rq);
969
970 chunk_appendf(&trash, "\n");
971
972 if (ci_putchk(si_ic(si), &trash) == -1) {
973 chunk_reset(&trash);
974 chunk_printf(&trash, "[output too large, cannot dump]\n");
975 si_applet_cant_put(si);
976 }
977
978 /* dump complete */
979 return 1;
980}
981
William Lallemandeceddf72016-12-15 18:06:44 +0100982/*
Willy Tarreau3af9d832016-12-16 12:58:09 +0100983 * CLI IO handler for `show cli sockets`.
984 * Uses ctx.cli.p0 to store the restart pointer.
William Lallemandeceddf72016-12-15 18:06:44 +0100985 */
986static int cli_io_handler_show_cli_sock(struct appctx *appctx)
987{
988 struct bind_conf *bind_conf;
989 struct stream_interface *si = appctx->owner;
990
991 chunk_reset(&trash);
992
993 switch (appctx->st2) {
994 case STAT_ST_INIT:
995 chunk_printf(&trash, "# socket lvl processes\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +0200996 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandeceddf72016-12-15 18:06:44 +0100997 si_applet_cant_put(si);
998 return 0;
999 }
William Lallemandeceddf72016-12-15 18:06:44 +01001000 appctx->st2 = STAT_ST_LIST;
1001
1002 case STAT_ST_LIST:
1003 if (global.stats_fe) {
1004 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
1005 struct listener *l;
1006
1007 /*
Willy Tarreau3af9d832016-12-16 12:58:09 +01001008 * get the latest dumped node in appctx->ctx.cli.p0
William Lallemandeceddf72016-12-15 18:06:44 +01001009 * if the current node is the first of the list
1010 */
1011
Willy Tarreau3af9d832016-12-16 12:58:09 +01001012 if (appctx->ctx.cli.p0 &&
1013 &bind_conf->by_fe == (&global.stats_fe->conf.bind)->n) {
William Lallemandeceddf72016-12-15 18:06:44 +01001014 /* change the current node to the latest dumped and continue the loop */
Willy Tarreau3af9d832016-12-16 12:58:09 +01001015 bind_conf = LIST_ELEM(appctx->ctx.cli.p0, typeof(bind_conf), by_fe);
William Lallemandeceddf72016-12-15 18:06:44 +01001016 continue;
1017 }
1018
1019 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
1020
1021 char addr[46];
1022 char port[6];
1023
1024 if (l->addr.ss_family == AF_UNIX) {
1025 const struct sockaddr_un *un;
1026
1027 un = (struct sockaddr_un *)&l->addr;
1028 chunk_appendf(&trash, "%s ", un->sun_path);
1029 } else if (l->addr.ss_family == AF_INET) {
1030 addr_to_str(&l->addr, addr, sizeof(addr));
1031 port_to_str(&l->addr, port, sizeof(port));
1032 chunk_appendf(&trash, "%s:%s ", addr, port);
1033 } else if (l->addr.ss_family == AF_INET6) {
1034 addr_to_str(&l->addr, addr, sizeof(addr));
1035 port_to_str(&l->addr, port, sizeof(port));
1036 chunk_appendf(&trash, "[%s]:%s ", addr, port);
1037 } else
1038 continue;
1039
William Lallemand07a62f72017-05-24 00:57:40 +02001040 if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN)
William Lallemandeceddf72016-12-15 18:06:44 +01001041 chunk_appendf(&trash, "admin ");
William Lallemand07a62f72017-05-24 00:57:40 +02001042 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER)
1043 chunk_appendf(&trash, "operator ");
1044 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_USER)
1045 chunk_appendf(&trash, "user ");
William Lallemandeceddf72016-12-15 18:06:44 +01001046 else
1047 chunk_appendf(&trash, " ");
1048
1049 if (bind_conf->bind_proc != 0) {
1050 int pos;
Willy Tarreau20c5e522016-12-16 12:50:55 +01001051 for (pos = 0; pos < 8 * sizeof(bind_conf->bind_proc); pos++) {
Willy Tarreau4305ac72016-12-16 12:56:31 +01001052 if (bind_conf->bind_proc & (1UL << pos)) {
William Lallemandeceddf72016-12-15 18:06:44 +01001053 chunk_appendf(&trash, "%d,", pos+1);
1054 }
1055 }
1056 /* replace the latest comma by a newline */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001057 trash.area[trash.data-1] = '\n';
William Lallemandeceddf72016-12-15 18:06:44 +01001058
1059 } else {
1060 chunk_appendf(&trash, "all\n");
1061 }
1062
Willy Tarreau06d80a92017-10-19 14:32:15 +02001063 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandeceddf72016-12-15 18:06:44 +01001064 si_applet_cant_put(si);
1065 return 0;
1066 }
1067 }
Willy Tarreau3af9d832016-12-16 12:58:09 +01001068 appctx->ctx.cli.p0 = &bind_conf->by_fe; /* store the latest list node dumped */
William Lallemandeceddf72016-12-15 18:06:44 +01001069 }
1070 }
1071 default:
1072 appctx->st2 = STAT_ST_FIN;
1073 return 1;
1074 }
1075}
1076
1077
Willy Tarreau0a739292016-11-22 20:21:23 +01001078/* parse a "show env" CLI request. Returns 0 if it needs to continue, 1 if it
Willy Tarreauf6710f82016-12-16 17:45:44 +01001079 * wants to stop here. It puts the variable to be dumped into cli.p0 if a single
1080 * variable is requested otherwise puts environ there.
Willy Tarreau0a739292016-11-22 20:21:23 +01001081 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001082static int cli_parse_show_env(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0a739292016-11-22 20:21:23 +01001083{
1084 extern char **environ;
Willy Tarreauf6710f82016-12-16 17:45:44 +01001085 char **var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001086
1087 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1088 return 1;
1089
Willy Tarreauf6710f82016-12-16 17:45:44 +01001090 var = environ;
Willy Tarreau0a739292016-11-22 20:21:23 +01001091
1092 if (*args[2]) {
1093 int len = strlen(args[2]);
1094
Willy Tarreauf6710f82016-12-16 17:45:44 +01001095 for (; *var; var++) {
1096 if (strncmp(*var, args[2], len) == 0 &&
1097 (*var)[len] == '=')
Willy Tarreau0a739292016-11-22 20:21:23 +01001098 break;
1099 }
Willy Tarreauf6710f82016-12-16 17:45:44 +01001100 if (!*var) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001101 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau0a739292016-11-22 20:21:23 +01001102 appctx->ctx.cli.msg = "Variable not found\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001103 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau0a739292016-11-22 20:21:23 +01001104 return 1;
1105 }
1106 appctx->st2 = STAT_ST_END;
1107 }
Willy Tarreauf6710f82016-12-16 17:45:44 +01001108 appctx->ctx.cli.p0 = var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001109 return 0;
1110}
1111
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001112/* parse a "show fd" CLI request. Returns 0 if it needs to continue, 1 if it
1113 * wants to stop here. It puts the FD number into cli.i0 if a specific FD is
1114 * requested and sets st2 to STAT_ST_END, otherwise leaves 0 in i0.
1115 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001116static int cli_parse_show_fd(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001117{
1118 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1119 return 1;
1120
1121 appctx->ctx.cli.i0 = 0;
1122
1123 if (*args[2]) {
1124 appctx->ctx.cli.i0 = atoi(args[2]);
1125 appctx->st2 = STAT_ST_END;
1126 }
1127 return 0;
1128}
1129
Willy Tarreau599852e2016-11-22 20:33:32 +01001130/* parse a "set timeout" CLI request. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001131static int cli_parse_set_timeout(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau599852e2016-11-22 20:33:32 +01001132{
1133 struct stream_interface *si = appctx->owner;
1134 struct stream *s = si_strm(si);
1135
1136 if (strcmp(args[2], "cli") == 0) {
1137 unsigned timeout;
1138 const char *res;
1139
1140 if (!*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001141 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau599852e2016-11-22 20:33:32 +01001142 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001143 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001144 return 1;
1145 }
1146
1147 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1148 if (res || timeout < 1) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001149 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau599852e2016-11-22 20:33:32 +01001150 appctx->ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001151 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001152 return 1;
1153 }
1154
1155 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
1156 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
1157 return 1;
1158 }
1159 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001160 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau599852e2016-11-22 20:33:32 +01001161 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001162 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001163 return 1;
1164 }
1165}
1166
Willy Tarreau2af99412016-11-23 11:10:59 +01001167/* parse a "set maxconn global" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001168static int cli_parse_set_maxconn_global(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2af99412016-11-23 11:10:59 +01001169{
1170 int v;
1171
1172 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1173 return 1;
1174
1175 if (!*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001176 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau2af99412016-11-23 11:10:59 +01001177 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001178 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +01001179 return 1;
1180 }
1181
1182 v = atoi(args[3]);
1183 if (v > global.hardmaxconn) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001184 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau2af99412016-11-23 11:10:59 +01001185 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001186 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +01001187 return 1;
1188 }
1189
1190 /* check for unlimited values */
1191 if (v <= 0)
1192 v = global.hardmaxconn;
1193
1194 global.maxconn = v;
1195
1196 /* Dequeues all of the listeners waiting for a resource */
1197 if (!LIST_ISEMPTY(&global_listener_queue))
1198 dequeue_all_listeners(&global_listener_queue);
1199
1200 return 1;
1201}
1202
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001203static int set_severity_output(int *target, char *argument)
1204{
1205 if (!strcmp(argument, "none")) {
1206 *target = CLI_SEVERITY_NONE;
1207 return 1;
1208 }
1209 else if (!strcmp(argument, "number")) {
1210 *target = CLI_SEVERITY_NUMBER;
1211 return 1;
1212 }
1213 else if (!strcmp(argument, "string")) {
1214 *target = CLI_SEVERITY_STRING;
1215 return 1;
1216 }
1217 return 0;
1218}
1219
1220/* parse a "set severity-output" command. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001221static int cli_parse_set_severity_output(char **args, char *payload, struct appctx *appctx, void *private)
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001222{
1223 if (*args[2] && set_severity_output(&appctx->cli_severity_output, args[2]))
1224 return 0;
1225
1226 appctx->ctx.cli.severity = LOG_ERR;
Aurélien Nephtali6e8a41d2018-03-15 21:48:50 +01001227 appctx->ctx.cli.msg = "one of 'none', 'number', 'string' is a required argument\n";
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001228 appctx->st0 = CLI_ST_PRINT;
1229 return 1;
1230}
William Lallemandeceddf72016-12-15 18:06:44 +01001231
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001232int cli_parse_default(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandeceddf72016-12-15 18:06:44 +01001233{
1234 return 0;
1235}
1236
Willy Tarreau45c742b2016-11-24 14:51:17 +01001237/* parse a "set rate-limit" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001238static int cli_parse_set_ratelimit(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau45c742b2016-11-24 14:51:17 +01001239{
1240 int v;
1241 int *res;
1242 int mul = 1;
1243
1244 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1245 return 1;
1246
1247 if (strcmp(args[2], "connections") == 0 && strcmp(args[3], "global") == 0)
1248 res = &global.cps_lim;
1249 else if (strcmp(args[2], "sessions") == 0 && strcmp(args[3], "global") == 0)
1250 res = &global.sps_lim;
1251#ifdef USE_OPENSSL
1252 else if (strcmp(args[2], "ssl-sessions") == 0 && strcmp(args[3], "global") == 0)
1253 res = &global.ssl_lim;
1254#endif
1255 else if (strcmp(args[2], "http-compression") == 0 && strcmp(args[3], "global") == 0) {
1256 res = &global.comp_rate_lim;
1257 mul = 1024;
1258 }
1259 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001260 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001261 appctx->ctx.cli.msg =
1262 "'set rate-limit' only supports :\n"
1263 " - 'connections global' to set the per-process maximum connection rate\n"
1264 " - 'sessions global' to set the per-process maximum session rate\n"
1265#ifdef USE_OPENSSL
Aurélien Nephtalib53e2082018-03-11 16:55:02 +01001266 " - 'ssl-sessions global' to set the per-process maximum SSL session rate\n"
Willy Tarreau45c742b2016-11-24 14:51:17 +01001267#endif
1268 " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001269 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001270 return 1;
1271 }
1272
1273 if (!*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001274 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001275 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001276 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001277 return 1;
1278 }
1279
1280 v = atoi(args[4]);
1281 if (v < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001282 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001283 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001284 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001285 return 1;
1286 }
1287
1288 *res = v * mul;
1289
1290 /* Dequeues all of the listeners waiting for a resource */
1291 if (!LIST_ISEMPTY(&global_listener_queue))
1292 dequeue_all_listeners(&global_listener_queue);
1293
1294 return 1;
1295}
1296
William Lallemandf6975e92017-05-26 17:42:10 +02001297/* parse the "expose-fd" argument on the bind lines */
1298static int bind_parse_expose_fd(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1299{
1300 if (!*args[cur_arg + 1]) {
1301 memprintf(err, "'%s' : missing fd type", args[cur_arg]);
1302 return ERR_ALERT | ERR_FATAL;
1303 }
1304 if (!strcmp(args[cur_arg+1], "listeners")) {
1305 conf->level |= ACCESS_FD_LISTENERS;
1306 } else {
1307 memprintf(err, "'%s' only supports 'listeners' (got '%s')",
1308 args[cur_arg], args[cur_arg+1]);
1309 return ERR_ALERT | ERR_FATAL;
1310 }
1311
1312 return 0;
1313}
1314
William Lallemand74c24fb2016-11-21 17:18:36 +01001315/* parse the "level" argument on the bind lines */
1316static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1317{
1318 if (!*args[cur_arg + 1]) {
1319 memprintf(err, "'%s' : missing level", args[cur_arg]);
1320 return ERR_ALERT | ERR_FATAL;
1321 }
1322
William Lallemand07a62f72017-05-24 00:57:40 +02001323 if (!strcmp(args[cur_arg+1], "user")) {
1324 conf->level &= ~ACCESS_LVL_MASK;
1325 conf->level |= ACCESS_LVL_USER;
1326 } else if (!strcmp(args[cur_arg+1], "operator")) {
1327 conf->level &= ~ACCESS_LVL_MASK;
1328 conf->level |= ACCESS_LVL_OPER;
1329 } else if (!strcmp(args[cur_arg+1], "admin")) {
1330 conf->level &= ~ACCESS_LVL_MASK;
1331 conf->level |= ACCESS_LVL_ADMIN;
1332 } else {
William Lallemand74c24fb2016-11-21 17:18:36 +01001333 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
1334 args[cur_arg], args[cur_arg+1]);
1335 return ERR_ALERT | ERR_FATAL;
1336 }
1337
1338 return 0;
1339}
1340
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001341static int bind_parse_severity_output(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1342{
1343 if (!*args[cur_arg + 1]) {
1344 memprintf(err, "'%s' : missing severity format", args[cur_arg]);
1345 return ERR_ALERT | ERR_FATAL;
1346 }
1347
1348 if (set_severity_output(&conf->severity_output, args[cur_arg+1]))
1349 return 0;
1350 else {
1351 memprintf(err, "'%s' only supports 'none', 'number', and 'string' (got '%s')",
1352 args[cur_arg], args[cur_arg+1]);
1353 return ERR_ALERT | ERR_FATAL;
1354 }
1355}
1356
Olivier Houchardf886e342017-04-05 22:24:59 +02001357/* Send all the bound sockets, always returns 1 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001358static int _getsocks(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchardf886e342017-04-05 22:24:59 +02001359{
1360 char *cmsgbuf = NULL;
1361 unsigned char *tmpbuf = NULL;
1362 struct cmsghdr *cmsg;
1363 struct stream_interface *si = appctx->owner;
William Lallemandf6975e92017-05-26 17:42:10 +02001364 struct stream *s = si_strm(si);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001365 struct connection *remote = cs_conn(objt_cs(si_opposite(si)->end));
Olivier Houchardf886e342017-04-05 22:24:59 +02001366 struct msghdr msghdr;
1367 struct iovec iov;
Olivier Houchard54740872017-04-06 14:45:14 +02001368 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf886e342017-04-05 22:24:59 +02001369 int *tmpfd;
1370 int tot_fd_nb = 0;
1371 struct proxy *px;
1372 int i = 0;
Willy Tarreau585744b2017-08-24 14:31:19 +02001373 int fd = remote->handle.fd;
Olivier Houchardf886e342017-04-05 22:24:59 +02001374 int curoff = 0;
1375 int old_fcntl;
1376 int ret;
1377
1378 /* Temporary set the FD in blocking mode, that will make our life easier */
1379 old_fcntl = fcntl(fd, F_GETFL);
1380 if (old_fcntl < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001381 ha_warning("Couldn't get the flags for the unix socket\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001382 goto out;
1383 }
1384 cmsgbuf = malloc(CMSG_SPACE(sizeof(int) * MAX_SEND_FD));
1385 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001386 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001387 goto out;
1388 }
1389 if (fcntl(fd, F_SETFL, old_fcntl &~ O_NONBLOCK) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001390 ha_warning("Cannot make the unix socket blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001391 goto out;
1392 }
Olivier Houchard54740872017-04-06 14:45:14 +02001393 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf886e342017-04-05 22:24:59 +02001394 iov.iov_base = &tot_fd_nb;
1395 iov.iov_len = sizeof(tot_fd_nb);
William Lallemandf6975e92017-05-26 17:42:10 +02001396 if (!(strm_li(s)->bind_conf->level & ACCESS_FD_LISTENERS))
Olivier Houchardf886e342017-04-05 22:24:59 +02001397 goto out;
1398 memset(&msghdr, 0, sizeof(msghdr));
1399 /*
1400 * First, calculates the total number of FD, so that we can let
1401 * the caller know how much he should expects.
1402 */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001403 px = proxies_list;
Olivier Houchardf886e342017-04-05 22:24:59 +02001404 while (px) {
1405 struct listener *l;
1406
1407 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02001408 /* Only transfer IPv4/IPv6/UNIX sockets */
1409 if (l->state >= LI_ZOMBIE &&
1410 (l->proto->sock_family == AF_INET ||
Olivier Houchardf886e342017-04-05 22:24:59 +02001411 l->proto->sock_family == AF_INET6 ||
Olivier Houchard1fc05162017-04-06 01:05:05 +02001412 l->proto->sock_family == AF_UNIX))
Olivier Houchardf886e342017-04-05 22:24:59 +02001413 tot_fd_nb++;
1414 }
1415 px = px->next;
1416 }
1417 if (tot_fd_nb == 0)
1418 goto out;
1419
1420 /* First send the total number of file descriptors, so that the
1421 * receiving end knows what to expect.
1422 */
1423 msghdr.msg_iov = &iov;
1424 msghdr.msg_iovlen = 1;
1425 ret = sendmsg(fd, &msghdr, 0);
1426 if (ret != sizeof(tot_fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001427 ha_warning("Failed to send the number of sockets to send\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001428 goto out;
1429 }
1430
1431 /* Now send the fds */
1432 msghdr.msg_control = cmsgbuf;
1433 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * MAX_SEND_FD);
1434 cmsg = CMSG_FIRSTHDR(&msghdr);
1435 cmsg->cmsg_len = CMSG_LEN(MAX_SEND_FD * sizeof(int));
1436 cmsg->cmsg_level = SOL_SOCKET;
1437 cmsg->cmsg_type = SCM_RIGHTS;
1438 tmpfd = (int *)CMSG_DATA(cmsg);
1439
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001440 px = proxies_list;
Olivier Houchardf886e342017-04-05 22:24:59 +02001441 /* For each socket, e message is sent, containing the following :
1442 * Size of the namespace name (or 0 if none), as an unsigned char.
1443 * The namespace name, if any
1444 * Size of the interface name (or 0 if none), as an unsigned char
1445 * The interface name, if any
1446 * Listener options, as an int.
1447 */
1448 /* We will send sockets MAX_SEND_FD per MAX_SEND_FD, allocate a
1449 * buffer big enough to store the socket informations.
1450 */
Olivier Houchardf143b802017-11-04 15:13:01 +01001451 tmpbuf = malloc(MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf886e342017-04-05 22:24:59 +02001452 if (tmpbuf == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001453 ha_warning("Failed to allocate memory to transfer socket informations\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001454 goto out;
1455 }
1456 iov.iov_base = tmpbuf;
1457 while (px) {
1458 struct listener *l;
1459
1460 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1461 int ret;
1462 /* Only transfer IPv4/IPv6 sockets */
Olivier Houchard1fc05162017-04-06 01:05:05 +02001463 if (l->state >= LI_ZOMBIE &&
Olivier Houchardf886e342017-04-05 22:24:59 +02001464 (l->proto->sock_family == AF_INET ||
1465 l->proto->sock_family == AF_INET6 ||
1466 l->proto->sock_family == AF_UNIX)) {
1467 memcpy(&tmpfd[i % MAX_SEND_FD], &l->fd, sizeof(l->fd));
1468 if (!l->netns)
1469 tmpbuf[curoff++] = 0;
1470#ifdef CONFIG_HAP_NS
1471 else {
1472 char *name = l->netns->node.key;
1473 unsigned char len = l->netns->name_len;
1474 tmpbuf[curoff++] = len;
1475 memcpy(tmpbuf + curoff, name, len);
1476 curoff += len;
1477 }
1478#endif
1479 if (l->interface) {
1480 unsigned char len = strlen(l->interface);
1481 tmpbuf[curoff++] = len;
1482 memcpy(tmpbuf + curoff, l->interface, len);
1483 curoff += len;
1484 } else
1485 tmpbuf[curoff++] = 0;
1486 memcpy(tmpbuf + curoff, &l->options,
1487 sizeof(l->options));
1488 curoff += sizeof(l->options);
1489
1490
1491 i++;
1492 } else
1493 continue;
1494 if ((!(i % MAX_SEND_FD))) {
1495 iov.iov_len = curoff;
1496 if (sendmsg(fd, &msghdr, 0) != curoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001497 ha_warning("Failed to transfer sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001498 goto out;
1499 }
1500 /* Wait for an ack */
1501 do {
1502 ret = recv(fd, &tot_fd_nb,
1503 sizeof(tot_fd_nb), 0);
1504 } while (ret == -1 && errno == EINTR);
1505 if (ret <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001506 ha_warning("Unexpected error while transferring sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001507 goto out;
1508 }
1509 curoff = 0;
1510 }
1511
1512 }
1513 px = px->next;
1514 }
1515 if (i % MAX_SEND_FD) {
1516 iov.iov_len = curoff;
1517 cmsg->cmsg_len = CMSG_LEN((i % MAX_SEND_FD) * sizeof(int));
1518 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * (i % MAX_SEND_FD));
1519 if (sendmsg(fd, &msghdr, 0) != curoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001520 ha_warning("Failed to transfer sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001521 goto out;
1522 }
1523 }
1524
1525out:
1526 if (old_fcntl >= 0 && fcntl(fd, F_SETFL, old_fcntl) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001527 ha_warning("Cannot make the unix socket non-blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001528 goto out;
1529 }
1530 appctx->st0 = CLI_ST_END;
1531 free(cmsgbuf);
1532 free(tmpbuf);
1533 return 1;
1534}
1535
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001536static int cli_parse_simple(char **args, char *payload, struct appctx *appctx, void *private)
1537{
1538 if (*args[0] == 'h')
1539 /* help */
1540 cli_gen_usage_msg(appctx);
1541 else if (*args[0] == 'p')
1542 /* prompt */
1543 appctx->st1 ^= APPCTX_CLI_ST1_PROMPT;
1544 else if (*args[0] == 'q')
1545 /* quit */
1546 appctx->st0 = CLI_ST_END;
1547
1548 return 1;
1549}
Olivier Houchardf886e342017-04-05 22:24:59 +02001550
1551
William Lallemand74c24fb2016-11-21 17:18:36 +01001552static struct applet cli_applet = {
1553 .obj_type = OBJ_TYPE_APPLET,
1554 .name = "<CLI>", /* used for logging */
1555 .fct = cli_io_handler,
1556 .release = cli_release_handler,
1557};
1558
Willy Tarreau0a739292016-11-22 20:21:23 +01001559/* register cli keywords */
1560static struct cli_kw_list cli_kws = {{ },{
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001561 { { "help", NULL }, NULL, cli_parse_simple, NULL },
1562 { { "prompt", NULL }, NULL, cli_parse_simple, NULL },
1563 { { "quit", NULL }, NULL, cli_parse_simple, NULL },
Willy Tarreau2af99412016-11-23 11:10:59 +01001564 { { "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 +01001565 { { "set", "rate-limit", NULL }, "set rate-limit : change a rate limiting value", cli_parse_set_ratelimit, NULL },
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001566 { { "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 +01001567 { { "set", "timeout", NULL }, "set timeout : change a timeout setting", cli_parse_set_timeout, NULL, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001568 { { "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 +01001569 { { "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 +02001570 { { "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 +01001571 { { "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 +02001572 { { "_getsocks", NULL }, NULL, _getsocks, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001573 {{},}
1574}};
1575
William Lallemand74c24fb2016-11-21 17:18:36 +01001576static struct cfg_kw_list cfg_kws = {ILH, {
1577 { CFG_GLOBAL, "stats", stats_parse_global },
1578 { 0, NULL, NULL },
1579}};
1580
1581static struct bind_kw_list bind_kws = { "STAT", { }, {
William Lallemandf6975e92017-05-26 17:42:10 +02001582 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
1583 { "expose-fd", bind_parse_expose_fd, 1 }, /* set the unix socket expose fd rights */
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001584 { "severity-output", bind_parse_severity_output, 1 }, /* set the severity output format */
William Lallemand74c24fb2016-11-21 17:18:36 +01001585 { NULL, NULL, 0 },
1586}};
1587
1588__attribute__((constructor))
1589static void __dumpstats_module_init(void)
1590{
1591 cfg_register_keywords(&cfg_kws);
Willy Tarreau0a739292016-11-22 20:21:23 +01001592 cli_register_kw(&cli_kws);
William Lallemand74c24fb2016-11-21 17:18:36 +01001593 bind_register_keywords(&bind_kws);
1594}
1595
1596/*
1597 * Local variables:
1598 * c-indent-level: 8
1599 * c-basic-offset: 8
1600 * End:
1601 */