blob: b8b233509a48f33fbfba255ead7ba9ca9991ac2b [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>
Willy Tarreau0108d902018-11-25 19:14:37 +010033#include <common/initcall.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
37#include <common/ticks.h>
38#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
41#include <common/base64.h>
42
43#include <types/applet.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010045#include <types/global.h>
46#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010048
Willy Tarreau609aad92018-11-22 08:31:09 +010049#include <proto/activity.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010050#include <proto/backend.h>
51#include <proto/channel.h>
52#include <proto/checks.h>
53#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010054#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010055#include <proto/fd.h>
56#include <proto/freq_ctr.h>
57#include <proto/frontend.h>
58#include <proto/log.h>
59#include <proto/pattern.h>
60#include <proto/pipe.h>
William Lallemandce83b4a2018-10-26 14:47:30 +020061#include <proto/protocol.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010062#include <proto/listener.h>
63#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010064#include <proto/proxy.h>
65#include <proto/sample.h>
66#include <proto/session.h>
67#include <proto/stream.h>
68#include <proto/server.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010069#include <proto/stream_interface.h>
70#include <proto/task.h>
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +020071#include <proto/proto_udp.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010072
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020073#define PAYLOAD_PATTERN "<<"
74
William Lallemand74c24fb2016-11-21 17:18:36 +010075static struct applet cli_applet;
76
77static const char stats_sock_usage_msg[] =
78 "Unknown command. Please enter one of the following commands only :\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010079 " help : this message\n"
80 " prompt : toggle interactive mode with prompt\n"
81 " quit : disconnect\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010082 "";
83
84static const char stats_permission_denied_msg[] =
85 "Permission denied\n"
86 "";
87
88
Christopher Faulet1bc04c72017-10-29 20:14:08 +010089static THREAD_LOCAL char *dynamic_usage_msg = NULL;
William Lallemand74c24fb2016-11-21 17:18:36 +010090
91/* List head of cli keywords */
92static struct cli_kw_list cli_keywords = {
93 .list = LIST_HEAD_INIT(cli_keywords.list)
94};
95
96extern const char *stat_status_codes[];
97
William Lallemand14721be2018-10-26 14:47:37 +020098extern int master;
99
William Lallemand8a022572018-10-26 14:47:35 +0200100static struct proxy *mworker_proxy; /* CLI proxy of the master */
101
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200102static char *cli_gen_usage_msg(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100103{
104 struct cli_kw_list *kw_list;
105 struct cli_kw *kw;
Willy Tarreau83061a82018-07-13 11:56:34 +0200106 struct buffer *tmp = get_trash_chunk();
107 struct buffer out;
William Lallemand74c24fb2016-11-21 17:18:36 +0100108
109 free(dynamic_usage_msg);
110 dynamic_usage_msg = NULL;
111
112 if (LIST_ISEMPTY(&cli_keywords.list))
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200113 goto end;
William Lallemand74c24fb2016-11-21 17:18:36 +0100114
115 chunk_reset(tmp);
116 chunk_strcat(tmp, stats_sock_usage_msg);
117 list_for_each_entry(kw_list, &cli_keywords.list, list) {
118 kw = &kw_list->kw[0];
William Lallemand0154edc2018-05-15 11:50:04 +0200119 while (kw->str_kw[0]) {
William Lallemand14721be2018-10-26 14:47:37 +0200120
121 /* in a worker or normal process, don't display master only commands */
122 if (master == 0 && (kw->level & ACCESS_MASTER_ONLY))
123 goto next_kw;
124
125 /* in master don't displays if we don't have the master bits */
126 if (master == 1 && !(kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)))
127 goto next_kw;
128
William Lallemand0154edc2018-05-15 11:50:04 +0200129 if (kw->usage)
130 chunk_appendf(tmp, " %s\n", kw->usage);
William Lallemand14721be2018-10-26 14:47:37 +0200131
132next_kw:
133
William Lallemand74c24fb2016-11-21 17:18:36 +0100134 kw++;
135 }
136 }
137 chunk_init(&out, NULL, 0);
138 chunk_dup(&out, tmp);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200139 dynamic_usage_msg = out.area;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200140
141end:
142 if (dynamic_usage_msg) {
143 appctx->ctx.cli.severity = LOG_INFO;
144 appctx->ctx.cli.msg = dynamic_usage_msg;
145 }
146 else {
147 appctx->ctx.cli.severity = LOG_INFO;
148 appctx->ctx.cli.msg = stats_sock_usage_msg;
149 }
150 appctx->st0 = CLI_ST_PRINT;
151
William Lallemand74c24fb2016-11-21 17:18:36 +0100152 return dynamic_usage_msg;
153}
154
155struct cli_kw* cli_find_kw(char **args)
156{
157 struct cli_kw_list *kw_list;
158 struct cli_kw *kw;/* current cli_kw */
159 char **tmp_args;
160 const char **tmp_str_kw;
161 int found = 0;
162
163 if (LIST_ISEMPTY(&cli_keywords.list))
164 return NULL;
165
166 list_for_each_entry(kw_list, &cli_keywords.list, list) {
167 kw = &kw_list->kw[0];
168 while (*kw->str_kw) {
169 tmp_args = args;
170 tmp_str_kw = kw->str_kw;
171 while (*tmp_str_kw) {
172 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
173 found = 1;
174 } else {
175 found = 0;
176 break;
177 }
178 tmp_args++;
179 tmp_str_kw++;
180 }
181 if (found)
182 return (kw);
183 kw++;
184 }
185 }
186 return NULL;
187}
188
189void cli_register_kw(struct cli_kw_list *kw_list)
190{
191 LIST_ADDQ(&cli_keywords.list, &kw_list->list);
192}
193
194
195/* allocate a new stats frontend named <name>, and return it
196 * (or NULL in case of lack of memory).
197 */
198static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
199{
200 struct proxy *fe;
201
202 fe = calloc(1, sizeof(*fe));
203 if (!fe)
204 return NULL;
205
206 init_new_proxy(fe);
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100207 fe->next = proxies_list;
208 proxies_list = fe;
William Lallemand74c24fb2016-11-21 17:18:36 +0100209 fe->last_change = now.tv_sec;
210 fe->id = strdup("GLOBAL");
211 fe->cap = PR_CAP_FE;
212 fe->maxconn = 10; /* default to 10 concurrent connections */
213 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
214 fe->conf.file = strdup(file);
215 fe->conf.line = line;
216 fe->accept = frontend_accept;
217 fe->default_target = &cli_applet.obj_type;
218
219 /* the stats frontend is the only one able to assign ID #0 */
220 fe->conf.id.key = fe->uuid = 0;
221 eb32_insert(&used_proxy_id, &fe->conf.id);
222 return fe;
223}
224
225/* This function parses a "stats" statement in the "global" section. It returns
226 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
227 * error message into the <err> buffer which will be preallocated. The trailing
228 * '\n' must not be written. The function must be called with <args> pointing to
229 * the first word after "stats".
230 */
231static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
232 struct proxy *defpx, const char *file, int line,
233 char **err)
234{
235 struct bind_conf *bind_conf;
236 struct listener *l;
237
238 if (!strcmp(args[1], "socket")) {
239 int cur_arg;
240
241 if (*args[2] == 0) {
242 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
243 return -1;
244 }
245
246 if (!global.stats_fe) {
247 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
248 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
249 return -1;
250 }
251 }
252
Willy Tarreaua261e9b2016-12-22 20:44:00 +0100253 bind_conf = bind_conf_alloc(global.stats_fe, file, line, args[2], xprt_get(XPRT_RAW));
William Lallemand07a62f72017-05-24 00:57:40 +0200254 bind_conf->level &= ~ACCESS_LVL_MASK;
255 bind_conf->level |= ACCESS_LVL_OPER; /* default access level */
William Lallemand74c24fb2016-11-21 17:18:36 +0100256
257 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
258 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
259 file, line, args[0], args[1], err && *err ? *err : "error");
260 return -1;
261 }
262
263 cur_arg = 3;
264 while (*args[cur_arg]) {
265 static int bind_dumped;
266 struct bind_kw *kw;
267
268 kw = bind_find_kw(args[cur_arg]);
269 if (kw) {
270 if (!kw->parse) {
271 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
272 args[0], args[1], args[cur_arg]);
273 return -1;
274 }
275
276 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
277 if (err && *err)
278 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
279 else
280 memprintf(err, "'%s %s' : error encountered while processing '%s'",
281 args[0], args[1], args[cur_arg]);
282 return -1;
283 }
284
285 cur_arg += 1 + kw->skip;
286 continue;
287 }
288
289 if (!bind_dumped) {
290 bind_dump_kws(err);
291 indent_msg(err, 4);
292 bind_dumped = 1;
293 }
294
295 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
296 args[0], args[1], args[cur_arg],
297 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
298 return -1;
299 }
300
301 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
302 l->maxconn = global.stats_fe->maxconn;
303 l->backlog = global.stats_fe->backlog;
304 l->accept = session_accept_fd;
William Lallemand74c24fb2016-11-21 17:18:36 +0100305 l->default_target = global.stats_fe->default_target;
306 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
307 l->nice = -64; /* we want to boost priority for local stats */
308 global.maxsock += l->maxconn;
309 }
310 }
311 else if (!strcmp(args[1], "timeout")) {
312 unsigned timeout;
313 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
314
315 if (res) {
316 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
317 return -1;
318 }
319
320 if (!timeout) {
321 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
322 return -1;
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->timeout.client = MS_TO_TICKS(timeout);
331 }
332 else if (!strcmp(args[1], "maxconn")) {
333 int maxconn = atol(args[2]);
334
335 if (maxconn <= 0) {
336 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
337 return -1;
338 }
339
340 if (!global.stats_fe) {
341 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
342 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
343 return -1;
344 }
345 }
346 global.stats_fe->maxconn = maxconn;
347 }
348 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
349 int cur_arg = 2;
350 unsigned long set = 0;
351
352 if (!global.stats_fe) {
353 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
354 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
355 return -1;
356 }
357 }
358
359 while (*args[cur_arg]) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100360 if (strcmp(args[cur_arg], "all") == 0) {
361 set = 0;
362 break;
363 }
Christopher Faulet26028f62017-11-22 15:01:51 +0100364 if (parse_process_number(args[cur_arg], &set, NULL, err)) {
Christopher Fauletf1f0c5f2017-11-22 12:06:43 +0100365 memprintf(err, "'%s %s' : %s", args[0], args[1], *err);
William Lallemand74c24fb2016-11-21 17:18:36 +0100366 return -1;
367 }
368 cur_arg++;
369 }
370 global.stats_fe->bind_proc = set;
371 }
372 else {
373 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
374 return -1;
375 }
376 return 0;
377}
378
Willy Tarreaude57a572016-11-23 17:01:39 +0100379/* Verifies that the CLI at least has a level at least as high as <level>
380 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
381 * failure, an error message is prepared and the appctx's state is adjusted
382 * to print it so that a return 1 is enough to abort any processing.
383 */
384int cli_has_level(struct appctx *appctx, int level)
385{
386 struct stream_interface *si = appctx->owner;
387 struct stream *s = si_strm(si);
388
William Lallemand07a62f72017-05-24 00:57:40 +0200389 if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < level) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200390 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaude57a572016-11-23 17:01:39 +0100391 appctx->ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100392 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaude57a572016-11-23 17:01:39 +0100393 return 0;
394 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100395 return 1;
396}
397
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200398/* Returns severity_output for the current session if set, or default for the socket */
399static int cli_get_severity_output(struct appctx *appctx)
400{
401 if (appctx->cli_severity_output)
402 return appctx->cli_severity_output;
403 return strm_li(si_strm(appctx->owner))->bind_conf->severity_output;
404}
William Lallemand74c24fb2016-11-21 17:18:36 +0100405
Willy Tarreau41908562016-11-24 16:23:38 +0100406/* Processes the CLI interpreter on the stats socket. This function is called
407 * from the CLI's IO handler running in an appctx context. The function returns 1
408 * if the request was understood, otherwise zero. It is called with appctx->st0
409 * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do
410 * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to
411 * have its own I/O handler called again. Most of the time, parsers will only
412 * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg.
Willy Tarreaueaffde32016-12-16 17:59:25 +0100413 * If a keyword parser is NULL and an I/O handler is declared, the I/O handler
414 * will automatically be used.
William Lallemand74c24fb2016-11-21 17:18:36 +0100415 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200416static int cli_parse_request(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100417{
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200418 char *args[MAX_STATS_ARGS + 1], *p, *end, *payload = NULL;
419 int i = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100420 struct cli_kw *kw;
William Lallemand74c24fb2016-11-21 17:18:36 +0100421
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200422 appctx->st2 = 0;
423 memset(&appctx->ctx.cli, 0, sizeof(appctx->ctx.cli));
William Lallemand74c24fb2016-11-21 17:18:36 +0100424
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200425 p = appctx->chunk->area;
426 end = p + appctx->chunk->data;
William Lallemand74c24fb2016-11-21 17:18:36 +0100427
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200428 /*
429 * Get the payload start if there is one.
430 * For the sake of simplicity, the payload pattern is looked up
431 * everywhere from the start of the input but it can only be found
432 * at the end of the first line if APPCTX_CLI_ST1_PAYLOAD is set.
433 *
434 * The input string was zero terminated so it is safe to use
435 * the str*() functions throughout the parsing
436 */
437 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
438 payload = strstr(p, PAYLOAD_PATTERN);
439 end = payload;
440 /* skip the pattern */
441 payload += strlen(PAYLOAD_PATTERN);
442 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100443
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200444 /*
445 * Get pointers on words.
446 * One extra slot is reserved to store a pointer on a null byte.
447 */
448 while (i < MAX_STATS_ARGS && p < end) {
449 int j, k;
William Lallemand74c24fb2016-11-21 17:18:36 +0100450
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200451 /* skip leading spaces/tabs */
452 p += strspn(p, " \t");
453 if (!*p)
454 break;
William Lallemand74c24fb2016-11-21 17:18:36 +0100455
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200456 args[i] = p;
457 p += strcspn(p, " \t");
458 *p++ = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100459
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200460 /* unescape backslashes (\) */
461 for (j = 0, k = 0; args[i][k]; k++) {
462 if (args[i][k] == '\\') {
463 if (args[i][k + 1] == '\\')
464 k++;
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100465 else
466 continue;
467 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200468 args[i][j] = args[i][k];
William Lallemand74c24fb2016-11-21 17:18:36 +0100469 j++;
470 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200471 args[i][j] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100472
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200473 i++;
474 }
475 /* fill unused slots */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200476 p = appctx->chunk->area + appctx->chunk->data;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200477 for (; i < MAX_STATS_ARGS + 1; i++)
478 args[i] = p;
Willy Tarreau41908562016-11-24 16:23:38 +0100479
480 kw = cli_find_kw(args);
Willy Tarreaueaffde32016-12-16 17:59:25 +0100481 if (!kw)
Willy Tarreau41908562016-11-24 16:23:38 +0100482 return 0;
483
William Lallemand14721be2018-10-26 14:47:37 +0200484 /* in a worker or normal process, don't display master only commands */
485 if (master == 0 && (kw->level & ACCESS_MASTER_ONLY))
486 return 0;
487
488 /* in master don't displays if we don't have the master bits */
489 if (master == 1 && !(kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)))
490 return 0;
491
Willy Tarreau41908562016-11-24 16:23:38 +0100492 appctx->io_handler = kw->io_handler;
Emeric Brund6871f72017-06-29 19:54:13 +0200493 appctx->io_release = kw->io_release;
494 /* kw->parse could set its own io_handler or ip_release handler */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200495 if ((!kw->parse || kw->parse(args, payload, appctx, kw->private) == 0) && appctx->io_handler) {
Willy Tarreau41908562016-11-24 16:23:38 +0100496 appctx->st0 = CLI_ST_CALLBACK;
William Lallemand74c24fb2016-11-21 17:18:36 +0100497 }
Willy Tarreau41908562016-11-24 16:23:38 +0100498 return 1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100499}
500
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200501/* prepends then outputs the argument msg with a syslog-type severity depending on severity_output value */
502static int cli_output_msg(struct channel *chn, const char *msg, int severity, int severity_output)
503{
Willy Tarreau83061a82018-07-13 11:56:34 +0200504 struct buffer *tmp;
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200505
506 if (likely(severity_output == CLI_SEVERITY_NONE))
Willy Tarreau06d80a92017-10-19 14:32:15 +0200507 return ci_putblk(chn, msg, strlen(msg));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200508
509 tmp = get_trash_chunk();
510 chunk_reset(tmp);
511
512 if (severity < 0 || severity > 7) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100513 ha_warning("socket command feedback with invalid severity %d", severity);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200514 chunk_printf(tmp, "[%d]: ", severity);
515 }
516 else {
517 switch (severity_output) {
518 case CLI_SEVERITY_NUMBER:
519 chunk_printf(tmp, "[%d]: ", severity);
520 break;
521 case CLI_SEVERITY_STRING:
522 chunk_printf(tmp, "[%s]: ", log_levels[severity]);
523 break;
524 default:
Christopher Faulet767a84b2017-11-24 16:50:31 +0100525 ha_warning("Unrecognized severity output %d", severity_output);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200526 }
527 }
528 chunk_appendf(tmp, "%s", msg);
529
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200530 return ci_putblk(chn, tmp->area, strlen(tmp->area));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200531}
532
William Lallemand74c24fb2016-11-21 17:18:36 +0100533/* This I/O handler runs as an applet embedded in a stream interface. It is
534 * used to processes I/O from/to the stats unix socket. The system relies on a
535 * state machine handling requests and various responses. We read a request,
536 * then we process it and send the response, and we possibly display a prompt.
537 * Then we can read again. The state is stored in appctx->st0 and is one of the
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100538 * CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled
William Lallemand74c24fb2016-11-21 17:18:36 +0100539 * or not.
540 */
541static void cli_io_handler(struct appctx *appctx)
542{
543 struct stream_interface *si = appctx->owner;
544 struct channel *req = si_oc(si);
545 struct channel *res = si_ic(si);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200546 struct bind_conf *bind_conf = strm_li(si_strm(si))->bind_conf;
William Lallemand74c24fb2016-11-21 17:18:36 +0100547 int reql;
548 int len;
549
550 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
551 goto out;
552
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100553 /* Check if the input buffer is avalaible. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200554 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +0100555 /* buf.size==0 means we failed to get a buffer and were
556 * already subscribed to a wait list to get a buffer.
557 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100558 goto out;
559 }
560
William Lallemand74c24fb2016-11-21 17:18:36 +0100561 while (1) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100562 if (appctx->st0 == CLI_ST_INIT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100563 /* Stats output not initialized yet */
564 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200565 /* reset severity to default at init */
566 appctx->cli_severity_output = bind_conf->severity_output;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100567 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100568 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100569 else if (appctx->st0 == CLI_ST_END) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100570 /* Let's close for real now. We just close the request
571 * side, the conditions below will complete if needed.
572 */
573 si_shutw(si);
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200574 free_trash_chunk(appctx->chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +0100575 break;
576 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100577 else if (appctx->st0 == CLI_ST_GETREQ) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200578 char *str;
579
580 /* use a trash chunk to store received data */
581 if (!appctx->chunk) {
582 appctx->chunk = alloc_trash_chunk();
583 if (!appctx->chunk) {
584 appctx->st0 = CLI_ST_END;
585 continue;
586 }
587 }
588
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200589 str = appctx->chunk->area + appctx->chunk->data;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200590
William Lallemand74c24fb2016-11-21 17:18:36 +0100591 /* ensure we have some output room left in the event we
592 * would want to return some info right after parsing.
593 */
594 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100595 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +0100596 break;
597 }
598
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200599 /* '- 1' is to ensure a null byte can always be inserted at the end */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200600 reql = co_getline(si_oc(si), str,
601 appctx->chunk->size - appctx->chunk->data - 1);
William Lallemand74c24fb2016-11-21 17:18:36 +0100602 if (reql <= 0) { /* closed or EOL not found */
603 if (reql == 0)
604 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100605 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100606 continue;
607 }
608
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200609 if (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)) {
610 /* seek for a possible unescaped semi-colon. If we find
611 * one, we replace it with an LF and skip only this part.
612 */
613 for (len = 0; len < reql; len++) {
614 if (str[len] == '\\') {
615 len++;
616 continue;
617 }
618 if (str[len] == ';') {
619 str[len] = '\n';
620 reql = len + 1;
621 break;
622 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100623 }
624 }
625
626 /* now it is time to check that we have a full line,
627 * remove the trailing \n and possibly \r, then cut the
628 * line.
629 */
630 len = reql - 1;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200631 if (str[len] != '\n') {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100632 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100633 continue;
634 }
635
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200636 if (len && str[len-1] == '\r')
William Lallemand74c24fb2016-11-21 17:18:36 +0100637 len--;
638
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200639 str[len] = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200640 appctx->chunk->data += len;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200641
642 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200643 appctx->chunk->area[appctx->chunk->data] = '\n';
644 appctx->chunk->area[appctx->chunk->data + 1] = 0;
645 appctx->chunk->data++;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200646 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100647
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100648 appctx->st0 = CLI_ST_PROMPT;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200649
650 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
651 /* empty line */
652 if (!len) {
653 /* remove the last two \n */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200654 appctx->chunk->data -= 2;
655 appctx->chunk->area[appctx->chunk->data] = 0;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200656
657 if (!cli_parse_request(appctx))
658 cli_gen_usage_msg(appctx);
659
660 chunk_reset(appctx->chunk);
661 /* NB: cli_sock_parse_request() may have put
662 * another CLI_ST_O_* into appctx->st0.
663 */
664
665 appctx->st1 &= ~APPCTX_CLI_ST1_PAYLOAD;
William Lallemand74c24fb2016-11-21 17:18:36 +0100666 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100667 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200668 else {
669 /*
670 * Look for the "payload start" pattern at the end of a line
671 * Its location is not remembered here, this is just to switch
672 * to a gathering mode.
William Lallemand74c24fb2016-11-21 17:18:36 +0100673 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200674 if (!strcmp(appctx->chunk->area + appctx->chunk->data - strlen(PAYLOAD_PATTERN), PAYLOAD_PATTERN))
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200675 appctx->st1 |= APPCTX_CLI_ST1_PAYLOAD;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200676 else {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200677 /* no payload, the command is complete: parse the request */
678 if (!cli_parse_request(appctx))
679 cli_gen_usage_msg(appctx);
680
681 chunk_reset(appctx->chunk);
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200682 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100683 }
684
685 /* re-adjust req buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200686 co_skip(si_oc(si), reql);
William Lallemand74c24fb2016-11-21 17:18:36 +0100687 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
688 }
689 else { /* output functions */
690 switch (appctx->st0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100691 case CLI_ST_PROMPT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100692 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100693 case CLI_ST_PRINT:
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200694 if (cli_output_msg(res, appctx->ctx.cli.msg, appctx->ctx.cli.severity,
695 cli_get_severity_output(appctx)) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100696 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100697 else
Willy Tarreaudb398432018-11-15 11:08:52 +0100698 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +0100699 break;
Aurélien Nephtalic511b7c2018-04-16 18:50:19 +0200700 case CLI_ST_PRINT_FREE: {
701 const char *msg = appctx->ctx.cli.err;
702
703 if (!msg)
704 msg = "Out of memory.\n";
705
706 if (cli_output_msg(res, msg, LOG_ERR, cli_get_severity_output(appctx)) != -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100707 free(appctx->ctx.cli.err);
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100708 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100709 }
710 else
Willy Tarreaudb398432018-11-15 11:08:52 +0100711 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +0100712 break;
Aurélien Nephtalic511b7c2018-04-16 18:50:19 +0200713 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100714 case CLI_ST_CALLBACK: /* use custom pointer */
William Lallemand74c24fb2016-11-21 17:18:36 +0100715 if (appctx->io_handler)
716 if (appctx->io_handler(appctx)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100717 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100718 if (appctx->io_release) {
719 appctx->io_release(appctx);
720 appctx->io_release = NULL;
721 }
722 }
723 break;
724 default: /* abnormal state */
725 si->flags |= SI_FL_ERR;
726 break;
727 }
728
729 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100730 if (appctx->st0 == CLI_ST_PROMPT) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200731 const char *prompt = "";
732
733 if (appctx->st1 & APPCTX_CLI_ST1_PROMPT) {
734 /*
735 * when entering a payload with interactive mode, change the prompt
736 * to emphasize that more data can still be sent
737 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200738 if (appctx->chunk->data && appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200739 prompt = "+ ";
740 else
741 prompt = "\n> ";
742 }
743 else {
744 if (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD))
745 prompt = "\n";
746 }
747
748 if (ci_putstr(si_ic(si), prompt) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100749 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100750 else
Willy Tarreaudb398432018-11-15 11:08:52 +0100751 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +0100752 }
753
754 /* If the output functions are still there, it means they require more room. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100755 if (appctx->st0 >= CLI_ST_OUTPUT)
William Lallemand74c24fb2016-11-21 17:18:36 +0100756 break;
757
758 /* Now we close the output if one of the writers did so,
759 * or if we're not in interactive mode and the request
760 * buffer is empty. This still allows pipelined requests
761 * to be sent in non-interactive mode.
762 */
Willy Tarreau851d12c2018-06-19 07:01:36 +0200763 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!(appctx->st1 & APPCTX_CLI_ST1_PROMPT) && !co_data(req))) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100764 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100765 continue;
766 }
767
768 /* switch state back to GETREQ to read next requests */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100769 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100770 }
771 }
772
773 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
774 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
775 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
776 /* Other side has closed, let's abort if we have no more processing to do
777 * and nothing more to consume. This is comparable to a broken pipe, so
778 * we forward the close to the request side so that it flows upstream to
779 * the client.
780 */
781 si_shutw(si);
782 }
783
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100784 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < CLI_ST_OUTPUT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100785 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
786 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
787 /* We have no more processing to do, and nothing more to send, and
788 * the client side has closed. So we'll forward this state downstream
789 * on the response buffer.
790 */
791 si_shutr(si);
792 res->flags |= CF_READ_NULL;
793 }
794
795 out:
Christopher Faulet45073512018-07-20 10:16:29 +0200796 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 +0100797 __FUNCTION__, __LINE__,
Christopher Faulet45073512018-07-20 10:16:29 +0200798 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 +0100799}
800
William Lallemand74c24fb2016-11-21 17:18:36 +0100801/* This is called when the stream interface is closed. For instance, upon an
802 * external abort, we won't call the i/o handler anymore so we may need to
803 * remove back references to the stream currently being dumped.
804 */
805static void cli_release_handler(struct appctx *appctx)
806{
807 if (appctx->io_release) {
808 appctx->io_release(appctx);
809 appctx->io_release = NULL;
810 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100811 else if (appctx->st0 == CLI_ST_PRINT_FREE) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100812 free(appctx->ctx.cli.err);
813 appctx->ctx.cli.err = NULL;
814 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100815}
816
817/* This function dumps all environmnent variables to the buffer. It returns 0
818 * if the output buffer is full and it needs to be called again, otherwise
Willy Tarreauf6710f82016-12-16 17:45:44 +0100819 * non-zero. Dumps only one entry if st2 == STAT_ST_END. It uses cli.p0 as the
820 * pointer to the current variable.
William Lallemand74c24fb2016-11-21 17:18:36 +0100821 */
Willy Tarreau0a739292016-11-22 20:21:23 +0100822static int cli_io_handler_show_env(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100823{
Willy Tarreau0a739292016-11-22 20:21:23 +0100824 struct stream_interface *si = appctx->owner;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100825 char **var = appctx->ctx.cli.p0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100826
827 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
828 return 1;
829
830 chunk_reset(&trash);
831
832 /* we have two inner loops here, one for the proxy, the other one for
833 * the buffer.
834 */
Willy Tarreauf6710f82016-12-16 17:45:44 +0100835 while (*var) {
836 chunk_printf(&trash, "%s\n", *var);
William Lallemand74c24fb2016-11-21 17:18:36 +0100837
Willy Tarreau06d80a92017-10-19 14:32:15 +0200838 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100839 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +0100840 return 0;
841 }
842 if (appctx->st2 == STAT_ST_END)
843 break;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100844 var++;
845 appctx->ctx.cli.p0 = var;
William Lallemand74c24fb2016-11-21 17:18:36 +0100846 }
847
848 /* dump complete */
849 return 1;
850}
851
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200852/* This function dumps all file descriptors states (or the requested one) to
853 * the buffer. It returns 0 if the output buffer is full and it needs to be
854 * called again, otherwise non-zero. Dumps only one entry if st2 == STAT_ST_END.
855 * It uses cli.i0 as the fd number to restart from.
856 */
857static int cli_io_handler_show_fd(struct appctx *appctx)
858{
859 struct stream_interface *si = appctx->owner;
860 int fd = appctx->ctx.cli.i0;
861
862 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
863 return 1;
864
865 chunk_reset(&trash);
866
867 /* we have two inner loops here, one for the proxy, the other one for
868 * the buffer.
869 */
Aurélien Nephtali498a1152018-03-09 18:51:16 +0100870 while (fd >= 0 && fd < global.maxsock) {
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200871 struct fdtab fdt;
Willy Tarreau286ec682017-08-09 16:35:44 +0200872 struct listener *li = NULL;
873 struct server *sv = NULL;
874 struct proxy *px = NULL;
Willy Tarreaua833cd92018-03-29 13:19:37 +0200875 const struct mux_ops *mux = NULL;
Willy Tarreau35b1b482018-03-28 18:41:30 +0200876 void *ctx = NULL;
Willy Tarreau286ec682017-08-09 16:35:44 +0200877 uint32_t conn_flags = 0;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200878
Willy Tarreaubf9fd652018-08-02 11:05:48 +0200879 thread_isolate();
880
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200881 fdt = fdtab[fd];
882
Willy Tarreaubf9fd652018-08-02 11:05:48 +0200883 if (!fdt.owner) {
884 thread_release();
Willy Tarreau017af242017-10-04 20:24:54 +0200885 goto skip; // closed
Willy Tarreaubf9fd652018-08-02 11:05:48 +0200886 }
Willy Tarreau017af242017-10-04 20:24:54 +0200887
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200888 if (fdt.iocb == conn_fd_handler) {
889 conn_flags = ((struct connection *)fdt.owner)->flags;
Willy Tarreau35b1b482018-03-28 18:41:30 +0200890 mux = ((struct connection *)fdt.owner)->mux;
891 ctx = ((struct connection *)fdt.owner)->mux_ctx;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200892 li = objt_listener(((struct connection *)fdt.owner)->target);
893 sv = objt_server(((struct connection *)fdt.owner)->target);
894 px = objt_proxy(((struct connection *)fdt.owner)->target);
895 }
896 else if (fdt.iocb == listener_accept)
897 li = fdt.owner;
898
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200899 chunk_printf(&trash,
Willy Tarreauc754b342018-03-30 15:00:15 +0200900 " %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 +0200901 fd,
902 fdt.state,
903 (fdt.state & FD_EV_POLLED_R) ? 'P' : 'p',
904 (fdt.state & FD_EV_READY_R) ? 'R' : 'r',
905 (fdt.state & FD_EV_ACTIVE_R) ? 'A' : 'a',
906 (fdt.state & FD_EV_POLLED_W) ? 'P' : 'p',
907 (fdt.state & FD_EV_READY_W) ? 'R' : 'r',
908 (fdt.state & FD_EV_ACTIVE_W) ? 'A' : 'a',
909 fdt.ev,
910 (fdt.ev & FD_POLL_HUP) ? 'H' : 'h',
911 (fdt.ev & FD_POLL_ERR) ? 'E' : 'e',
912 (fdt.ev & FD_POLL_OUT) ? 'O' : 'o',
913 (fdt.ev & FD_POLL_PRI) ? 'P' : 'p',
914 (fdt.ev & FD_POLL_IN) ? 'I' : 'i',
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200915 fdt.linger_risk ? 'L' : 'l',
916 fdt.cloned ? 'C' : 'c',
Willy Tarreauc754b342018-03-30 15:00:15 +0200917 fdt.cache.next,
918 fdt.cache.prev,
919 fdt.thread_mask, fdt.update_mask,
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200920 fdt.owner,
921 fdt.iocb,
922 (fdt.iocb == conn_fd_handler) ? "conn_fd_handler" :
923 (fdt.iocb == dgram_fd_handler) ? "dgram_fd_handler" :
924 (fdt.iocb == listener_accept) ? "listener_accept" :
Olivier Houchard79321b92018-07-26 17:55:11 +0200925 (fdt.iocb == poller_pipe_io_handler) ? "poller_pipe_io_handler" :
William Lallemanddb6bdfb2018-11-20 17:36:51 +0100926 (fdt.iocb == mworker_accept_wrapper) ? "mworker_accept_wrapper" :
Willy Tarreauc754b342018-03-30 15:00:15 +0200927 "unknown");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200928
929 if (fdt.iocb == conn_fd_handler) {
930 chunk_appendf(&trash, " cflg=0x%08x", conn_flags);
931 if (px)
932 chunk_appendf(&trash, " px=%s", px->id);
933 else if (sv)
934 chunk_appendf(&trash, " sv=%s/%s", sv->id, sv->proxy->id);
935 else if (li)
936 chunk_appendf(&trash, " fe=%s", li->bind_conf->frontend->id);
Willy Tarreau35b1b482018-03-28 18:41:30 +0200937
Willy Tarreaub011d8f2018-03-30 14:41:19 +0200938 if (mux) {
Willy Tarreau35b1b482018-03-28 18:41:30 +0200939 chunk_appendf(&trash, " mux=%s mux_ctx=%p", mux->name, ctx);
Willy Tarreaub011d8f2018-03-30 14:41:19 +0200940 if (mux->show_fd)
941 mux->show_fd(&trash, fdt.owner);
942 }
Willy Tarreau35b1b482018-03-28 18:41:30 +0200943 else
944 chunk_appendf(&trash, " nomux");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200945 }
946 else if (fdt.iocb == listener_accept) {
947 chunk_appendf(&trash, " l.st=%s fe=%s",
948 listener_state_str(li),
949 li->bind_conf->frontend->id);
950 }
951
Willy Tarreaubf9fd652018-08-02 11:05:48 +0200952 thread_release();
953
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200954 chunk_appendf(&trash, "\n");
955
Willy Tarreau06d80a92017-10-19 14:32:15 +0200956 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100957 si_rx_room_blk(si);
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200958 return 0;
959 }
960 skip:
961 if (appctx->st2 == STAT_ST_END)
962 break;
963
964 fd++;
965 appctx->ctx.cli.i0 = fd;
966 }
967
968 /* dump complete */
969 return 1;
970}
971
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100972/* This function dumps some activity counters used by developers and support to
973 * rule out some hypothesis during bug reports. It returns 0 if the output
974 * buffer is full and it needs to be called again, otherwise non-zero. It dumps
975 * everything at once in the buffer and is not designed to do it in multiple
976 * passes.
977 */
978static int cli_io_handler_show_activity(struct appctx *appctx)
979{
980 struct stream_interface *si = appctx->owner;
981 int thr;
982
983 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
984 return 1;
985
986 chunk_reset(&trash);
987
988 chunk_appendf(&trash, "thread_id: %u", tid);
989 chunk_appendf(&trash, "\ndate_now: %lu.%06lu", (long)now.tv_sec, (long)now.tv_usec);
990 chunk_appendf(&trash, "\nloops:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].loops);
991 chunk_appendf(&trash, "\nwake_cache:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].wake_cache);
992 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 +0100993 chunk_appendf(&trash, "\nwake_signal:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].wake_signal);
994 chunk_appendf(&trash, "\npoll_exp:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_exp);
995 chunk_appendf(&trash, "\npoll_drop:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_drop);
996 chunk_appendf(&trash, "\npoll_dead:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_dead);
997 chunk_appendf(&trash, "\npoll_skip:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].poll_skip);
998 chunk_appendf(&trash, "\nfd_skip:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].fd_skip);
999 chunk_appendf(&trash, "\nfd_lock:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].fd_lock);
1000 chunk_appendf(&trash, "\nfd_del:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].fd_del);
1001 chunk_appendf(&trash, "\nconn_dead:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].conn_dead);
1002 chunk_appendf(&trash, "\nstream:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].stream);
1003 chunk_appendf(&trash, "\nempty_rq:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].empty_rq);
1004 chunk_appendf(&trash, "\nlong_rq:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].long_rq);
Willy Tarreau4f93e0c2018-11-22 16:13:17 +01001005 chunk_appendf(&trash, "\ncpust_ms_tot:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", activity[thr].cpust_total/2);
1006 chunk_appendf(&trash, "\ncpust_ms_1s:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", read_freq_ctr(&activity[thr].cpust_1s)/2);
1007 chunk_appendf(&trash, "\ncpust_ms_15s:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", read_freq_ctr_period(&activity[thr].cpust_15s, 15000)/2);
Willy Tarreaubaba82f2018-11-22 08:42:42 +01001008 chunk_appendf(&trash, "\navg_loop_us:"); for (thr = 0; thr < global.nbthread; thr++) chunk_appendf(&trash, " %u", swrate_avg(activity[thr].avg_loop_us, TIME_STATS_SAMPLES));
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001009
1010 chunk_appendf(&trash, "\n");
1011
1012 if (ci_putchk(si_ic(si), &trash) == -1) {
1013 chunk_reset(&trash);
1014 chunk_printf(&trash, "[output too large, cannot dump]\n");
Willy Tarreaudb398432018-11-15 11:08:52 +01001015 si_rx_room_blk(si);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001016 }
1017
1018 /* dump complete */
1019 return 1;
1020}
1021
William Lallemandeceddf72016-12-15 18:06:44 +01001022/*
Willy Tarreau3af9d832016-12-16 12:58:09 +01001023 * CLI IO handler for `show cli sockets`.
1024 * Uses ctx.cli.p0 to store the restart pointer.
William Lallemandeceddf72016-12-15 18:06:44 +01001025 */
1026static int cli_io_handler_show_cli_sock(struct appctx *appctx)
1027{
1028 struct bind_conf *bind_conf;
1029 struct stream_interface *si = appctx->owner;
1030
1031 chunk_reset(&trash);
1032
1033 switch (appctx->st2) {
1034 case STAT_ST_INIT:
1035 chunk_printf(&trash, "# socket lvl processes\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001036 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001037 si_rx_room_blk(si);
William Lallemandeceddf72016-12-15 18:06:44 +01001038 return 0;
1039 }
William Lallemandeceddf72016-12-15 18:06:44 +01001040 appctx->st2 = STAT_ST_LIST;
1041
1042 case STAT_ST_LIST:
1043 if (global.stats_fe) {
1044 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
1045 struct listener *l;
1046
1047 /*
Willy Tarreau3af9d832016-12-16 12:58:09 +01001048 * get the latest dumped node in appctx->ctx.cli.p0
William Lallemandeceddf72016-12-15 18:06:44 +01001049 * if the current node is the first of the list
1050 */
1051
Willy Tarreau3af9d832016-12-16 12:58:09 +01001052 if (appctx->ctx.cli.p0 &&
1053 &bind_conf->by_fe == (&global.stats_fe->conf.bind)->n) {
William Lallemandeceddf72016-12-15 18:06:44 +01001054 /* change the current node to the latest dumped and continue the loop */
Willy Tarreau3af9d832016-12-16 12:58:09 +01001055 bind_conf = LIST_ELEM(appctx->ctx.cli.p0, typeof(bind_conf), by_fe);
William Lallemandeceddf72016-12-15 18:06:44 +01001056 continue;
1057 }
1058
1059 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
1060
1061 char addr[46];
1062 char port[6];
1063
1064 if (l->addr.ss_family == AF_UNIX) {
1065 const struct sockaddr_un *un;
1066
1067 un = (struct sockaddr_un *)&l->addr;
1068 chunk_appendf(&trash, "%s ", un->sun_path);
1069 } else if (l->addr.ss_family == AF_INET) {
1070 addr_to_str(&l->addr, addr, sizeof(addr));
1071 port_to_str(&l->addr, port, sizeof(port));
1072 chunk_appendf(&trash, "%s:%s ", addr, port);
1073 } else if (l->addr.ss_family == AF_INET6) {
1074 addr_to_str(&l->addr, addr, sizeof(addr));
1075 port_to_str(&l->addr, port, sizeof(port));
1076 chunk_appendf(&trash, "[%s]:%s ", addr, port);
William Lallemand26314342018-10-26 14:47:41 +02001077 } else if (l->addr.ss_family == AF_CUST_SOCKPAIR) {
1078 chunk_appendf(&trash, "sockpair@%d ", ((struct sockaddr_in *)&l->addr)->sin_addr.s_addr);
William Lallemandeceddf72016-12-15 18:06:44 +01001079 } else
William Lallemand26314342018-10-26 14:47:41 +02001080 chunk_appendf(&trash, "unknown ");
William Lallemandeceddf72016-12-15 18:06:44 +01001081
William Lallemand07a62f72017-05-24 00:57:40 +02001082 if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN)
William Lallemandeceddf72016-12-15 18:06:44 +01001083 chunk_appendf(&trash, "admin ");
William Lallemand07a62f72017-05-24 00:57:40 +02001084 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER)
1085 chunk_appendf(&trash, "operator ");
1086 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_USER)
1087 chunk_appendf(&trash, "user ");
William Lallemandeceddf72016-12-15 18:06:44 +01001088 else
1089 chunk_appendf(&trash, " ");
1090
1091 if (bind_conf->bind_proc != 0) {
1092 int pos;
Willy Tarreau20c5e522016-12-16 12:50:55 +01001093 for (pos = 0; pos < 8 * sizeof(bind_conf->bind_proc); pos++) {
Willy Tarreau4305ac72016-12-16 12:56:31 +01001094 if (bind_conf->bind_proc & (1UL << pos)) {
William Lallemandeceddf72016-12-15 18:06:44 +01001095 chunk_appendf(&trash, "%d,", pos+1);
1096 }
1097 }
1098 /* replace the latest comma by a newline */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001099 trash.area[trash.data-1] = '\n';
William Lallemandeceddf72016-12-15 18:06:44 +01001100
1101 } else {
1102 chunk_appendf(&trash, "all\n");
1103 }
1104
Willy Tarreau06d80a92017-10-19 14:32:15 +02001105 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001106 si_rx_room_blk(si);
William Lallemandeceddf72016-12-15 18:06:44 +01001107 return 0;
1108 }
1109 }
Willy Tarreau3af9d832016-12-16 12:58:09 +01001110 appctx->ctx.cli.p0 = &bind_conf->by_fe; /* store the latest list node dumped */
William Lallemandeceddf72016-12-15 18:06:44 +01001111 }
1112 }
1113 default:
1114 appctx->st2 = STAT_ST_FIN;
1115 return 1;
1116 }
1117}
1118
1119
Willy Tarreau0a739292016-11-22 20:21:23 +01001120/* parse a "show env" CLI request. Returns 0 if it needs to continue, 1 if it
Willy Tarreauf6710f82016-12-16 17:45:44 +01001121 * wants to stop here. It puts the variable to be dumped into cli.p0 if a single
1122 * variable is requested otherwise puts environ there.
Willy Tarreau0a739292016-11-22 20:21:23 +01001123 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001124static int cli_parse_show_env(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0a739292016-11-22 20:21:23 +01001125{
1126 extern char **environ;
Willy Tarreauf6710f82016-12-16 17:45:44 +01001127 char **var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001128
1129 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1130 return 1;
1131
Willy Tarreauf6710f82016-12-16 17:45:44 +01001132 var = environ;
Willy Tarreau0a739292016-11-22 20:21:23 +01001133
1134 if (*args[2]) {
1135 int len = strlen(args[2]);
1136
Willy Tarreauf6710f82016-12-16 17:45:44 +01001137 for (; *var; var++) {
1138 if (strncmp(*var, args[2], len) == 0 &&
1139 (*var)[len] == '=')
Willy Tarreau0a739292016-11-22 20:21:23 +01001140 break;
1141 }
Willy Tarreauf6710f82016-12-16 17:45:44 +01001142 if (!*var) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001143 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau0a739292016-11-22 20:21:23 +01001144 appctx->ctx.cli.msg = "Variable not found\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001145 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau0a739292016-11-22 20:21:23 +01001146 return 1;
1147 }
1148 appctx->st2 = STAT_ST_END;
1149 }
Willy Tarreauf6710f82016-12-16 17:45:44 +01001150 appctx->ctx.cli.p0 = var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001151 return 0;
1152}
1153
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001154/* parse a "show fd" CLI request. Returns 0 if it needs to continue, 1 if it
1155 * wants to stop here. It puts the FD number into cli.i0 if a specific FD is
1156 * requested and sets st2 to STAT_ST_END, otherwise leaves 0 in i0.
1157 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001158static int cli_parse_show_fd(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001159{
1160 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1161 return 1;
1162
1163 appctx->ctx.cli.i0 = 0;
1164
1165 if (*args[2]) {
1166 appctx->ctx.cli.i0 = atoi(args[2]);
1167 appctx->st2 = STAT_ST_END;
1168 }
1169 return 0;
1170}
1171
Willy Tarreau599852e2016-11-22 20:33:32 +01001172/* parse a "set timeout" CLI request. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001173static int cli_parse_set_timeout(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau599852e2016-11-22 20:33:32 +01001174{
1175 struct stream_interface *si = appctx->owner;
1176 struct stream *s = si_strm(si);
1177
1178 if (strcmp(args[2], "cli") == 0) {
1179 unsigned timeout;
1180 const char *res;
1181
1182 if (!*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001183 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau599852e2016-11-22 20:33:32 +01001184 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001185 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001186 return 1;
1187 }
1188
1189 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1190 if (res || timeout < 1) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001191 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau599852e2016-11-22 20:33:32 +01001192 appctx->ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001193 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001194 return 1;
1195 }
1196
1197 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
1198 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
1199 return 1;
1200 }
1201 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001202 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau599852e2016-11-22 20:33:32 +01001203 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001204 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +01001205 return 1;
1206 }
1207}
1208
Willy Tarreau2af99412016-11-23 11:10:59 +01001209/* parse a "set maxconn global" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001210static int cli_parse_set_maxconn_global(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2af99412016-11-23 11:10:59 +01001211{
1212 int v;
1213
1214 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1215 return 1;
1216
1217 if (!*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001218 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau2af99412016-11-23 11:10:59 +01001219 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001220 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +01001221 return 1;
1222 }
1223
1224 v = atoi(args[3]);
1225 if (v > global.hardmaxconn) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001226 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau2af99412016-11-23 11:10:59 +01001227 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001228 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +01001229 return 1;
1230 }
1231
1232 /* check for unlimited values */
1233 if (v <= 0)
1234 v = global.hardmaxconn;
1235
1236 global.maxconn = v;
1237
1238 /* Dequeues all of the listeners waiting for a resource */
1239 if (!LIST_ISEMPTY(&global_listener_queue))
1240 dequeue_all_listeners(&global_listener_queue);
1241
1242 return 1;
1243}
1244
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001245static int set_severity_output(int *target, char *argument)
1246{
1247 if (!strcmp(argument, "none")) {
1248 *target = CLI_SEVERITY_NONE;
1249 return 1;
1250 }
1251 else if (!strcmp(argument, "number")) {
1252 *target = CLI_SEVERITY_NUMBER;
1253 return 1;
1254 }
1255 else if (!strcmp(argument, "string")) {
1256 *target = CLI_SEVERITY_STRING;
1257 return 1;
1258 }
1259 return 0;
1260}
1261
1262/* parse a "set severity-output" command. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001263static int cli_parse_set_severity_output(char **args, char *payload, struct appctx *appctx, void *private)
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001264{
1265 if (*args[2] && set_severity_output(&appctx->cli_severity_output, args[2]))
1266 return 0;
1267
1268 appctx->ctx.cli.severity = LOG_ERR;
Aurélien Nephtali6e8a41d2018-03-15 21:48:50 +01001269 appctx->ctx.cli.msg = "one of 'none', 'number', 'string' is a required argument\n";
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001270 appctx->st0 = CLI_ST_PRINT;
1271 return 1;
1272}
William Lallemandeceddf72016-12-15 18:06:44 +01001273
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001274int cli_parse_default(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandeceddf72016-12-15 18:06:44 +01001275{
1276 return 0;
1277}
1278
Willy Tarreau45c742b2016-11-24 14:51:17 +01001279/* parse a "set rate-limit" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001280static int cli_parse_set_ratelimit(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau45c742b2016-11-24 14:51:17 +01001281{
1282 int v;
1283 int *res;
1284 int mul = 1;
1285
1286 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1287 return 1;
1288
1289 if (strcmp(args[2], "connections") == 0 && strcmp(args[3], "global") == 0)
1290 res = &global.cps_lim;
1291 else if (strcmp(args[2], "sessions") == 0 && strcmp(args[3], "global") == 0)
1292 res = &global.sps_lim;
1293#ifdef USE_OPENSSL
1294 else if (strcmp(args[2], "ssl-sessions") == 0 && strcmp(args[3], "global") == 0)
1295 res = &global.ssl_lim;
1296#endif
1297 else if (strcmp(args[2], "http-compression") == 0 && strcmp(args[3], "global") == 0) {
1298 res = &global.comp_rate_lim;
1299 mul = 1024;
1300 }
1301 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001302 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001303 appctx->ctx.cli.msg =
1304 "'set rate-limit' only supports :\n"
1305 " - 'connections global' to set the per-process maximum connection rate\n"
1306 " - 'sessions global' to set the per-process maximum session rate\n"
1307#ifdef USE_OPENSSL
Aurélien Nephtalib53e2082018-03-11 16:55:02 +01001308 " - 'ssl-sessions global' to set the per-process maximum SSL session rate\n"
Willy Tarreau45c742b2016-11-24 14:51:17 +01001309#endif
1310 " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001311 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001312 return 1;
1313 }
1314
1315 if (!*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001316 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001317 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001318 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001319 return 1;
1320 }
1321
1322 v = atoi(args[4]);
1323 if (v < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001324 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001325 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001326 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +01001327 return 1;
1328 }
1329
1330 *res = v * mul;
1331
1332 /* Dequeues all of the listeners waiting for a resource */
1333 if (!LIST_ISEMPTY(&global_listener_queue))
1334 dequeue_all_listeners(&global_listener_queue);
1335
1336 return 1;
1337}
1338
William Lallemandf6975e92017-05-26 17:42:10 +02001339/* parse the "expose-fd" argument on the bind lines */
1340static int bind_parse_expose_fd(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1341{
1342 if (!*args[cur_arg + 1]) {
1343 memprintf(err, "'%s' : missing fd type", args[cur_arg]);
1344 return ERR_ALERT | ERR_FATAL;
1345 }
1346 if (!strcmp(args[cur_arg+1], "listeners")) {
1347 conf->level |= ACCESS_FD_LISTENERS;
1348 } else {
1349 memprintf(err, "'%s' only supports 'listeners' (got '%s')",
1350 args[cur_arg], args[cur_arg+1]);
1351 return ERR_ALERT | ERR_FATAL;
1352 }
1353
1354 return 0;
1355}
1356
William Lallemand74c24fb2016-11-21 17:18:36 +01001357/* parse the "level" argument on the bind lines */
1358static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1359{
1360 if (!*args[cur_arg + 1]) {
1361 memprintf(err, "'%s' : missing level", args[cur_arg]);
1362 return ERR_ALERT | ERR_FATAL;
1363 }
1364
William Lallemand07a62f72017-05-24 00:57:40 +02001365 if (!strcmp(args[cur_arg+1], "user")) {
1366 conf->level &= ~ACCESS_LVL_MASK;
1367 conf->level |= ACCESS_LVL_USER;
1368 } else if (!strcmp(args[cur_arg+1], "operator")) {
1369 conf->level &= ~ACCESS_LVL_MASK;
1370 conf->level |= ACCESS_LVL_OPER;
1371 } else if (!strcmp(args[cur_arg+1], "admin")) {
1372 conf->level &= ~ACCESS_LVL_MASK;
1373 conf->level |= ACCESS_LVL_ADMIN;
1374 } else {
William Lallemand74c24fb2016-11-21 17:18:36 +01001375 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
1376 args[cur_arg], args[cur_arg+1]);
1377 return ERR_ALERT | ERR_FATAL;
1378 }
1379
1380 return 0;
1381}
1382
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001383static int bind_parse_severity_output(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1384{
1385 if (!*args[cur_arg + 1]) {
1386 memprintf(err, "'%s' : missing severity format", args[cur_arg]);
1387 return ERR_ALERT | ERR_FATAL;
1388 }
1389
1390 if (set_severity_output(&conf->severity_output, args[cur_arg+1]))
1391 return 0;
1392 else {
1393 memprintf(err, "'%s' only supports 'none', 'number', and 'string' (got '%s')",
1394 args[cur_arg], args[cur_arg+1]);
1395 return ERR_ALERT | ERR_FATAL;
1396 }
1397}
1398
William Lallemandb9f9e3b2018-10-26 14:47:39 +02001399
1400 /* Displays workers and processes */
1401static int cli_io_handler_show_proc(struct appctx *appctx)
1402{
1403 struct stream_interface *si = appctx->owner;
1404 struct mworker_proc *child;
William Lallemande09cdc62018-11-19 18:46:16 +01001405 int old = 0;
William Lallemand16dd1b32018-11-19 18:46:18 +01001406 int up = now.tv_sec - proc_self->timestamp;
William Lallemandb9f9e3b2018-10-26 14:47:39 +02001407
1408 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1409 return 1;
1410
1411 chunk_reset(&trash);
1412
William Lallemande3683302018-11-19 18:46:17 +01001413 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %s\n", "<PID>", "<type>", "<relative PID>", "<reloads>", "<uptime>");
William Lallemand16dd1b32018-11-19 18:46:18 +01001414 chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %dd %02dh%02dm%02ds\n", getpid(), "master", 0, proc_self->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemandb9f9e3b2018-10-26 14:47:39 +02001415
William Lallemande09cdc62018-11-19 18:46:16 +01001416 /* displays current processes */
William Lallemandb9f9e3b2018-10-26 14:47:39 +02001417
William Lallemande09cdc62018-11-19 18:46:16 +01001418 chunk_appendf(&trash, "# workers\n");
William Lallemandb9f9e3b2018-10-26 14:47:39 +02001419 list_for_each_entry(child, &proc_list, list) {
William Lallemand16dd1b32018-11-19 18:46:18 +01001420 up = now.tv_sec - child->timestamp;
1421
1422 if (child->type != 'w')
1423 continue;
William Lallemande09cdc62018-11-19 18:46:16 +01001424
1425 if (child->reloads > 0) {
1426 old++;
1427 continue;
1428 }
William Lallemande3683302018-11-19 18:46:17 +01001429 chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %dd %02dh%02dm%02ds\n", child->pid, "worker", child->relative_pid, child->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemande09cdc62018-11-19 18:46:16 +01001430}
1431
1432 /* displays old processes */
1433
1434 if (old) {
1435 chunk_appendf(&trash, "# old workers\n");
1436 list_for_each_entry(child, &proc_list, list) {
William Lallemand16dd1b32018-11-19 18:46:18 +01001437 up = now.tv_sec - child->timestamp;
1438
1439 if (child->type != 'w')
1440 continue;
1441
William Lallemande09cdc62018-11-19 18:46:16 +01001442 if (child->reloads > 0)
William Lallemande3683302018-11-19 18:46:17 +01001443 chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %dd %02dh%02dm%02ds\n", child->pid, "worker", child->relative_pid, child->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemande09cdc62018-11-19 18:46:16 +01001444 }
William Lallemandb9f9e3b2018-10-26 14:47:39 +02001445 }
1446
1447 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001448 si_rx_room_blk(si);
William Lallemandb9f9e3b2018-10-26 14:47:39 +02001449 return 0;
1450 }
1451
1452 /* dump complete */
1453 return 1;
1454}
1455
Olivier Houchardf886e342017-04-05 22:24:59 +02001456/* Send all the bound sockets, always returns 1 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001457static int _getsocks(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchardf886e342017-04-05 22:24:59 +02001458{
1459 char *cmsgbuf = NULL;
1460 unsigned char *tmpbuf = NULL;
1461 struct cmsghdr *cmsg;
1462 struct stream_interface *si = appctx->owner;
William Lallemandf6975e92017-05-26 17:42:10 +02001463 struct stream *s = si_strm(si);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001464 struct connection *remote = cs_conn(objt_cs(si_opposite(si)->end));
Olivier Houchardf886e342017-04-05 22:24:59 +02001465 struct msghdr msghdr;
1466 struct iovec iov;
Olivier Houchard54740872017-04-06 14:45:14 +02001467 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf886e342017-04-05 22:24:59 +02001468 int *tmpfd;
1469 int tot_fd_nb = 0;
1470 struct proxy *px;
1471 int i = 0;
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001472 int fd = -1;
Olivier Houchardf886e342017-04-05 22:24:59 +02001473 int curoff = 0;
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001474 int old_fcntl = -1;
Olivier Houchardf886e342017-04-05 22:24:59 +02001475 int ret;
1476
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001477 if (!remote) {
1478 ha_warning("Only works on real connections\n");
1479 goto out;
1480 }
1481
1482 fd = remote->handle.fd;
1483
Olivier Houchardf886e342017-04-05 22:24:59 +02001484 /* Temporary set the FD in blocking mode, that will make our life easier */
1485 old_fcntl = fcntl(fd, F_GETFL);
1486 if (old_fcntl < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001487 ha_warning("Couldn't get the flags for the unix socket\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001488 goto out;
1489 }
1490 cmsgbuf = malloc(CMSG_SPACE(sizeof(int) * MAX_SEND_FD));
1491 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001492 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001493 goto out;
1494 }
1495 if (fcntl(fd, F_SETFL, old_fcntl &~ O_NONBLOCK) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001496 ha_warning("Cannot make the unix socket blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001497 goto out;
1498 }
Olivier Houchard54740872017-04-06 14:45:14 +02001499 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf886e342017-04-05 22:24:59 +02001500 iov.iov_base = &tot_fd_nb;
1501 iov.iov_len = sizeof(tot_fd_nb);
William Lallemandf6975e92017-05-26 17:42:10 +02001502 if (!(strm_li(s)->bind_conf->level & ACCESS_FD_LISTENERS))
Olivier Houchardf886e342017-04-05 22:24:59 +02001503 goto out;
1504 memset(&msghdr, 0, sizeof(msghdr));
1505 /*
1506 * First, calculates the total number of FD, so that we can let
1507 * the caller know how much he should expects.
1508 */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001509 px = proxies_list;
Olivier Houchardf886e342017-04-05 22:24:59 +02001510 while (px) {
1511 struct listener *l;
1512
1513 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02001514 /* Only transfer IPv4/IPv6/UNIX sockets */
1515 if (l->state >= LI_ZOMBIE &&
1516 (l->proto->sock_family == AF_INET ||
Olivier Houchardf886e342017-04-05 22:24:59 +02001517 l->proto->sock_family == AF_INET6 ||
Olivier Houchard1fc05162017-04-06 01:05:05 +02001518 l->proto->sock_family == AF_UNIX))
Olivier Houchardf886e342017-04-05 22:24:59 +02001519 tot_fd_nb++;
1520 }
1521 px = px->next;
1522 }
1523 if (tot_fd_nb == 0)
1524 goto out;
1525
1526 /* First send the total number of file descriptors, so that the
1527 * receiving end knows what to expect.
1528 */
1529 msghdr.msg_iov = &iov;
1530 msghdr.msg_iovlen = 1;
1531 ret = sendmsg(fd, &msghdr, 0);
1532 if (ret != sizeof(tot_fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001533 ha_warning("Failed to send the number of sockets to send\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001534 goto out;
1535 }
1536
1537 /* Now send the fds */
1538 msghdr.msg_control = cmsgbuf;
1539 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * MAX_SEND_FD);
1540 cmsg = CMSG_FIRSTHDR(&msghdr);
1541 cmsg->cmsg_len = CMSG_LEN(MAX_SEND_FD * sizeof(int));
1542 cmsg->cmsg_level = SOL_SOCKET;
1543 cmsg->cmsg_type = SCM_RIGHTS;
1544 tmpfd = (int *)CMSG_DATA(cmsg);
1545
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001546 px = proxies_list;
Olivier Houchardf886e342017-04-05 22:24:59 +02001547 /* For each socket, e message is sent, containing the following :
1548 * Size of the namespace name (or 0 if none), as an unsigned char.
1549 * The namespace name, if any
1550 * Size of the interface name (or 0 if none), as an unsigned char
1551 * The interface name, if any
1552 * Listener options, as an int.
1553 */
1554 /* We will send sockets MAX_SEND_FD per MAX_SEND_FD, allocate a
1555 * buffer big enough to store the socket informations.
1556 */
Olivier Houchardf143b802017-11-04 15:13:01 +01001557 tmpbuf = malloc(MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf886e342017-04-05 22:24:59 +02001558 if (tmpbuf == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001559 ha_warning("Failed to allocate memory to transfer socket informations\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001560 goto out;
1561 }
1562 iov.iov_base = tmpbuf;
1563 while (px) {
1564 struct listener *l;
1565
1566 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1567 int ret;
1568 /* Only transfer IPv4/IPv6 sockets */
Olivier Houchard1fc05162017-04-06 01:05:05 +02001569 if (l->state >= LI_ZOMBIE &&
Olivier Houchardf886e342017-04-05 22:24:59 +02001570 (l->proto->sock_family == AF_INET ||
1571 l->proto->sock_family == AF_INET6 ||
1572 l->proto->sock_family == AF_UNIX)) {
1573 memcpy(&tmpfd[i % MAX_SEND_FD], &l->fd, sizeof(l->fd));
1574 if (!l->netns)
1575 tmpbuf[curoff++] = 0;
1576#ifdef CONFIG_HAP_NS
1577 else {
1578 char *name = l->netns->node.key;
1579 unsigned char len = l->netns->name_len;
1580 tmpbuf[curoff++] = len;
1581 memcpy(tmpbuf + curoff, name, len);
1582 curoff += len;
1583 }
1584#endif
1585 if (l->interface) {
1586 unsigned char len = strlen(l->interface);
1587 tmpbuf[curoff++] = len;
1588 memcpy(tmpbuf + curoff, l->interface, len);
1589 curoff += len;
1590 } else
1591 tmpbuf[curoff++] = 0;
1592 memcpy(tmpbuf + curoff, &l->options,
1593 sizeof(l->options));
1594 curoff += sizeof(l->options);
1595
1596
1597 i++;
1598 } else
1599 continue;
1600 if ((!(i % MAX_SEND_FD))) {
1601 iov.iov_len = curoff;
1602 if (sendmsg(fd, &msghdr, 0) != curoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001603 ha_warning("Failed to transfer sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001604 goto out;
1605 }
1606 /* Wait for an ack */
1607 do {
1608 ret = recv(fd, &tot_fd_nb,
1609 sizeof(tot_fd_nb), 0);
1610 } while (ret == -1 && errno == EINTR);
1611 if (ret <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001612 ha_warning("Unexpected error while transferring sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001613 goto out;
1614 }
1615 curoff = 0;
1616 }
1617
1618 }
1619 px = px->next;
1620 }
1621 if (i % MAX_SEND_FD) {
1622 iov.iov_len = curoff;
1623 cmsg->cmsg_len = CMSG_LEN((i % MAX_SEND_FD) * sizeof(int));
1624 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * (i % MAX_SEND_FD));
1625 if (sendmsg(fd, &msghdr, 0) != curoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001626 ha_warning("Failed to transfer sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001627 goto out;
1628 }
1629 }
1630
1631out:
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001632 if (fd >= 0 && old_fcntl >= 0 && fcntl(fd, F_SETFL, old_fcntl) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001633 ha_warning("Cannot make the unix socket non-blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001634 goto out;
1635 }
1636 appctx->st0 = CLI_ST_END;
1637 free(cmsgbuf);
1638 free(tmpbuf);
1639 return 1;
1640}
1641
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001642static int cli_parse_simple(char **args, char *payload, struct appctx *appctx, void *private)
1643{
1644 if (*args[0] == 'h')
1645 /* help */
1646 cli_gen_usage_msg(appctx);
1647 else if (*args[0] == 'p')
1648 /* prompt */
1649 appctx->st1 ^= APPCTX_CLI_ST1_PROMPT;
1650 else if (*args[0] == 'q')
1651 /* quit */
1652 appctx->st0 = CLI_ST_END;
1653
1654 return 1;
1655}
Olivier Houchardf886e342017-04-05 22:24:59 +02001656
William Lallemand2f4ce202018-10-26 14:47:47 +02001657void pcli_write_prompt(struct stream *s)
1658{
1659 struct buffer *msg = get_trash_chunk();
1660 struct channel *oc = si_oc(&s->si[0]);
1661
1662 if (s->pcli_next_pid == 0)
1663 chunk_appendf(msg, "master> ");
1664 else
1665 chunk_appendf(msg, "%d> ", s->pcli_next_pid);
1666 co_inject(oc, msg->area, msg->data);
1667}
1668
William Lallemand291810d2018-10-26 14:47:38 +02001669
William Lallemandcf62f7e2018-10-26 14:47:40 +02001670/* The pcli_* functions are used for the CLI proxy in the master */
1671
William Lallemanddeeaa592018-10-26 14:47:48 +02001672void pcli_reply_and_close(struct stream *s, const char *msg)
1673{
1674 struct buffer *buf = get_trash_chunk();
1675
1676 chunk_initstr(buf, msg);
1677 stream_int_retnclose(&s->si[0], buf);
1678}
1679
William Lallemand291810d2018-10-26 14:47:38 +02001680static enum obj_type *pcli_pid_to_server(int proc_pid)
1681{
1682 struct mworker_proc *child;
1683
1684 list_for_each_entry(child, &proc_list, list) {
1685 if (child->pid == proc_pid){
1686 return &child->srv->obj_type;
1687 }
1688 }
1689 return NULL;
1690}
1691
1692/* Take a CLI prefix in argument (eg: @!1234 @master @1)
1693 * Return:
1694 * 0: master
1695 * > 0: pid of a worker
1696 * < 0: didn't find a worker
1697 */
1698static int pcli_prefix_to_pid(const char *prefix)
1699{
1700 int proc_pid;
1701 struct mworker_proc *child;
1702 char *errtol = NULL;
1703
1704 if (*prefix != '@') /* not a prefix, should not happen */
1705 return -1;
1706
1707 prefix++;
1708 if (!*prefix) /* sent @ alone, return the master */
1709 return 0;
1710
1711 if (strcmp("master", prefix) == 0) {
1712 return 0;
1713 } else if (*prefix == '!') {
1714 prefix++;
1715 if (!*prefix)
1716 return -1;
1717
1718 proc_pid = strtol(prefix, &errtol, 10);
1719 if (*errtol != '\0')
1720 return -1;
1721 list_for_each_entry(child, &proc_list, list) {
William Lallemand16dd1b32018-11-19 18:46:18 +01001722 if (child->type != 'w')
1723 continue;
William Lallemand291810d2018-10-26 14:47:38 +02001724 if (child->pid == proc_pid){
1725 return child->pid;
1726 }
1727 }
1728 } else {
1729 struct mworker_proc *chosen = NULL;
1730 /* this is a relative pid */
1731
1732 proc_pid = strtol(prefix, &errtol, 10);
1733 if (*errtol != '\0')
1734 return -1;
1735
1736 if (proc_pid == 0) /* return the master */
1737 return 0;
1738
1739 /* chose the right process, the current one is the one with the
1740 least number of reloads */
1741 list_for_each_entry(child, &proc_list, list) {
William Lallemand16dd1b32018-11-19 18:46:18 +01001742 if (child->type != 'w')
1743 continue;
William Lallemand291810d2018-10-26 14:47:38 +02001744 if (child->relative_pid == proc_pid){
1745 if (child->reloads == 0)
1746 return child->pid;
1747 else if (chosen == NULL || child->reloads < chosen->reloads)
1748 chosen = child;
1749 }
1750 }
1751 if (chosen)
1752 return chosen->pid;
1753 }
1754 return -1;
1755}
1756
William Lallemandcf62f7e2018-10-26 14:47:40 +02001757/* Parse the CLI request:
1758 *
1759 * - it can rewrite the buffer by trimming the prefix
1760 * - fill dst with the destination server if there is one
1761 *
1762 * Return:
1763 * - the amount of data to forward or
1764 * - -1 if there is no end to the command or
1765 * - 0 everything has been trimmed (only a prefix)
1766 */
1767#define PCLI_REQ_INIT 0
1768#define PCLI_REQ_PFX 1
1769#define PCLI_REQ_TRIM 2
1770#define PCLI_REQ_CMD 3
1771
1772int pcli_parse_request(struct channel *req, int *target_pid)
1773{
1774 char *input = (char *)ci_head(req);
1775 const char *end;
1776 char *ptr, *trim = NULL, *pfx_b = NULL, *cmd_b = NULL;
1777 struct buffer *buf = &req->buf;
1778 int ret = 0;
1779 int state = PCLI_REQ_INIT;
1780
1781 ptr = input;
1782 end = b_stop(buf);
1783
1784 /* The while loop condition is checking the end of the command.
1785 It is needed to iterate for each ptr++ done in the parser */
1786 while (ptr < end && *ptr != '\n' && *ptr != '\r' && *ptr != ';') {
1787 switch (state) {
1788 /* The init state only trims the useless chars */
1789 case PCLI_REQ_INIT:
1790
1791 /* skip every spaces at the start of the command */
1792 if (*ptr == ' ') {
1793 ptr++;
1794 continue;
1795 }
1796 pfx_b = ptr; /* this is the start of the command or of the @ prefix */
1797 state = PCLI_REQ_PFX;
1798
1799 /* the atprefix state looks for a @ prefix. If it finds
1800 it, it will check to which server send the request.
1801 It also ajust the trim pointer */
1802 case PCLI_REQ_PFX:
1803
1804 if (*pfx_b != '@') {
1805 /* there is no prefix */
1806 pfx_b = NULL;
William Lallemand744de5b2018-10-29 17:14:00 +01001807 cmd_b = input; /* if no prefix we don't trim anything */
William Lallemandcf62f7e2018-10-26 14:47:40 +02001808 state = PCLI_REQ_CMD;
1809 continue;
1810 }
1811
1812 if (*ptr != ' ') {
1813 ptr++;
1814 continue;
1815 }
1816 *ptr = '\0'; /* this the end of the prefix */
1817 ptr++;
1818 trim = ptr;
1819 state = PCLI_REQ_TRIM;
1820 break;
1821
1822 /* we really need to trim there because that's the only
1823 way to know if we are going to send a command or if
1824 there is only a prefix */
1825 case PCLI_REQ_TRIM:
1826 if (*ptr == ' ') {
1827 ptr++;
1828 continue;
1829 }
1830 cmd_b = trim = ptr;
1831 state = PCLI_REQ_CMD;
1832
1833 /* just look for the end of the command */
1834 case PCLI_REQ_CMD:
1835 ptr++;
1836 continue;
1837 }
1838 }
1839
1840 /* we didn't find a command separator, not enough data */
1841 if (ptr >= end)
1842 return -1;
1843
1844 if (!pfx_b && !cmd_b) {
1845 /* probably just a \n or a ; */
1846 return 1;
1847 } else if (pfx_b && !cmd_b) {
1848 /* it's only a prefix, we don't want to forward it */
1849 *ptr = '\0';
1850 trim = ptr + 1; /* we want to trim the whole command */
1851 ret = 0;
1852 } else if (cmd_b) {
1853 /* command without a prefix */
1854 *ptr = '\n';
1855 ret = ptr - cmd_b + 1;
1856 }
1857
1858 if (pfx_b)
1859 *target_pid = pcli_prefix_to_pid(pfx_b);
1860
1861 /* trim the useless chars */
1862 if (trim)
1863 b_del(&req->buf, trim - input);
1864
1865 return ret;
1866}
1867
1868int pcli_wait_for_request(struct stream *s, struct channel *req, int an_bit)
1869{
1870 int target_pid;
1871 int to_forward;
1872
1873 target_pid = s->pcli_next_pid;
1874
1875read_again:
1876 /* if the channel is closed for read, we won't receive any more data
1877 from the client, but we don't want to forward this close to the
1878 server */
1879 channel_dont_close(req);
1880
1881 /* We don't know yet to which server we will connect */
1882 channel_dont_connect(req);
1883
1884
1885 /* we are not waiting for a response, there is no more request and we
1886 * receive a close from the client, we can leave */
1887 if (!(ci_data(req)) && req->flags & CF_SHUTR) {
1888 channel_shutw_now(&s->res);
1889 s->req.analysers &= ~AN_REQ_WAIT_CLI;
1890 return 1;
1891 }
1892
1893 req->flags |= CF_READ_DONTWAIT;
1894
1895 /* need more data */
1896 if (!ci_data(req))
1897 return 0;
1898
1899 /* If there is data available for analysis, log the end of the idle time. */
1900 if (c_data(req) && s->logs.t_idle == -1)
1901 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1902
1903 to_forward = pcli_parse_request(req, &target_pid);
1904 if (to_forward > 0) {
1905 /* enough data */
1906
1907 /* we didn't find the process, send an error and close */
1908 if (target_pid < 0) {
1909 pcli_reply_and_close(s, "Can't find the target CLI!\n");
1910 return 0;
1911 }
1912
1913 /* forward only 1 command */
1914 channel_forward(req, to_forward);
1915 /* we send only 1 command per request, and we write close after it */
1916 channel_shutw_now(req);
1917
1918 /* remove the XFER_DATA analysers, which forwards all
1919 * the data, we don't want to forward the next requests
1920 * We need to add CF_FLT_ANALYZE to abort the forward too.
1921 */
1922 req->analysers &= ~(AN_REQ_FLT_XFER_DATA|AN_REQ_WAIT_CLI);
1923 req->analysers |= AN_REQ_FLT_END|CF_FLT_ANALYZE;
1924 s->res.analysers |= AN_RES_WAIT_CLI;
1925
1926 /* we can connect now */
1927 s->target = pcli_pid_to_server(target_pid);
1928 if (!s->target) {
1929 s->target = &cli_applet.obj_type;
1930 }
1931
1932 s->flags |= (SF_DIRECT | SF_ASSIGNED);
1933 channel_auto_connect(req);
1934
1935 } else if (to_forward == 0) {
1936 /* we only received a prefix without command, which
1937 mean that we want to store it for every other
1938 command for this session */
1939 if (target_pid > -1) {
1940 s->pcli_next_pid = target_pid;
William Lallemand2f4ce202018-10-26 14:47:47 +02001941 pcli_write_prompt(s);
William Lallemandcf62f7e2018-10-26 14:47:40 +02001942 } else {
William Lallemandcf62f7e2018-10-26 14:47:40 +02001943 s->pcli_next_pid = 0;
William Lallemanddeeaa592018-10-26 14:47:48 +02001944 pcli_reply_and_close(s, "Can't find the target CLI!\n");
William Lallemandcf62f7e2018-10-26 14:47:40 +02001945 }
1946
1947 /* we trimmed things but we might have other commands to consume */
1948 goto read_again;
1949 } else if (to_forward == -1 && channel_full(req, global.tune.maxrewrite)) {
1950 /* buffer is full and we didn't catch the end of a command */
1951 goto send_help;
1952 }
1953
1954 return 0;
1955
1956send_help:
1957 b_reset(&req->buf);
1958 b_putblk(&req->buf, "help\n", 5);
1959 goto read_again;
1960}
1961
1962int pcli_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
1963{
1964 struct proxy *fe = strm_fe(s);
1965 struct proxy *be = s->be;
1966
William Lallemand6b7cd0a2018-11-06 17:37:11 +01001967 if (rep->flags & CF_READ_ERROR) {
1968 pcli_reply_and_close(s, "Can't connect to the target CLI!\n");
1969 s->res.analysers &= ~AN_RES_WAIT_CLI;
1970 return 0;
1971 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02001972 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
1973 rep->flags |= CF_NEVER_WAIT;
1974
1975 /* don't forward the close */
1976 channel_dont_close(&s->res);
1977 channel_dont_close(&s->req);
1978
1979 /* forward the data */
1980 if (ci_data(rep)) {
1981 c_adv(rep, ci_data(rep));
1982 return 0;
1983 }
1984
1985 if ((rep->flags & (CF_SHUTR|CF_READ_NULL))) {
1986 /* stream cleanup */
1987
William Lallemand2f4ce202018-10-26 14:47:47 +02001988 pcli_write_prompt(s);
1989
William Lallemandcf62f7e2018-10-26 14:47:40 +02001990 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
1991 si_shutr(&s->si[1]);
1992 si_shutw(&s->si[1]);
1993
1994 /*
1995 * starting from there this the same code as
1996 * http_end_txn_clean_session().
1997 *
1998 * It allows to do frontend keepalive while reconnecting to a
1999 * new server for each request.
2000 */
2001
2002 if (s->flags & SF_BE_ASSIGNED) {
2003 HA_ATOMIC_SUB(&be->beconn, 1);
2004 if (unlikely(s->srv_conn))
2005 sess_change_server(s, NULL);
2006 }
2007
2008 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2009 stream_process_counters(s);
2010
2011 /* don't count other requests' data */
2012 s->logs.bytes_in -= ci_data(&s->req);
2013 s->logs.bytes_out -= ci_data(&s->res);
2014
2015 /* we may need to know the position in the queue */
2016 pendconn_free(s);
2017
2018 /* let's do a final log if we need it */
2019 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
2020 !(s->flags & SF_MONITOR) &&
2021 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
2022 s->do_log(s);
2023 }
2024
2025 /* stop tracking content-based counters */
2026 stream_stop_content_counters(s);
2027 stream_update_time_stats(s);
2028
2029 s->logs.accept_date = date; /* user-visible date for logging */
2030 s->logs.tv_accept = now; /* corrected date for internal use */
2031 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
2032 s->logs.t_idle = -1;
2033 tv_zero(&s->logs.tv_request);
2034 s->logs.t_queue = -1;
2035 s->logs.t_connect = -1;
2036 s->logs.t_data = -1;
2037 s->logs.t_close = 0;
2038 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
2039 s->logs.srv_queue_pos = 0; /* we will get this number soon */
2040
2041 s->logs.bytes_in = s->req.total = ci_data(&s->req);
2042 s->logs.bytes_out = s->res.total = ci_data(&s->res);
2043
2044 stream_del_srv_conn(s);
2045 if (objt_server(s->target)) {
2046 if (s->flags & SF_CURR_SESS) {
2047 s->flags &= ~SF_CURR_SESS;
2048 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
2049 }
2050 if (may_dequeue_tasks(objt_server(s->target), be))
2051 process_srv_queue(objt_server(s->target));
2052 }
2053
2054 s->target = NULL;
2055
2056 /* only release our endpoint if we don't intend to reuse the
2057 * connection.
2058 */
2059 if (!si_conn_ready(&s->si[1])) {
2060 si_release_endpoint(&s->si[1]);
2061 s->srv_conn = NULL;
2062 }
2063
2064 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
2065 s->si[1].err_type = SI_ET_NONE;
2066 s->si[1].conn_retries = 0; /* used for logging too */
2067 s->si[1].exp = TICK_ETERNITY;
2068 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */
2069 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
2070 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA);
2071 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
2072 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
2073 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
2074 /* reinitialise the current rule list pointer to NULL. We are sure that
2075 * any rulelist match the NULL pointer.
2076 */
2077 s->current_rule_list = NULL;
2078
2079 s->be = strm_fe(s);
2080 s->logs.logwait = strm_fe(s)->to_log;
2081 s->logs.level = 0;
2082 stream_del_srv_conn(s);
2083 s->target = NULL;
2084 /* re-init store persistence */
2085 s->store_count = 0;
2086 s->uniq_id = global.req_count++;
2087
2088 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
2089
2090 s->req.flags |= CF_WAKE_ONCE; /* need to be called again if there is some command left in the request */
2091
2092 s->req.analysers |= AN_REQ_WAIT_CLI;
2093 s->res.analysers &= ~AN_RES_WAIT_CLI;
2094
2095 /* We must trim any excess data from the response buffer, because we
2096 * may have blocked an invalid response from a server that we don't
2097 * want to accidentely forward once we disable the analysers, nor do
2098 * we want those data to come along with next response. A typical
2099 * example of such data would be from a buggy server responding to
2100 * a HEAD with some data, or sending more than the advertised
2101 * content-length.
2102 */
2103 if (unlikely(ci_data(&s->res)))
2104 b_set_data(&s->res.buf, co_data(&s->res));
2105
2106 /* Now we can realign the response buffer */
2107 c_realign_if_empty(&s->res);
2108
2109 s->req.rto = strm_fe(s)->timeout.client;
2110 s->req.wto = TICK_ETERNITY;
2111
2112 s->res.rto = TICK_ETERNITY;
2113 s->res.wto = strm_fe(s)->timeout.client;
2114
2115 s->req.rex = TICK_ETERNITY;
2116 s->req.wex = TICK_ETERNITY;
2117 s->req.analyse_exp = TICK_ETERNITY;
2118 s->res.rex = TICK_ETERNITY;
2119 s->res.wex = TICK_ETERNITY;
2120 s->res.analyse_exp = TICK_ETERNITY;
2121 s->si[1].hcto = TICK_ETERNITY;
2122
2123 /* we're removing the analysers, we MUST re-enable events detection.
2124 * We don't enable close on the response channel since it's either
2125 * already closed, or in keep-alive with an idle connection handler.
2126 */
2127 channel_auto_read(&s->req);
2128 channel_auto_close(&s->req);
2129 channel_auto_read(&s->res);
2130
2131
2132 return 1;
2133 }
2134 return 0;
2135}
2136
William Lallemand8a022572018-10-26 14:47:35 +02002137/*
2138 * The mworker functions are used to initialize the CLI in the master process
2139 */
2140
William Lallemand309dc9a2018-10-26 14:47:45 +02002141 /*
2142 * Stop the mworker proxy
2143 */
2144void mworker_cli_proxy_stop()
2145{
William Lallemand550db6d2018-11-06 17:37:12 +01002146 if (mworker_proxy)
2147 stop_proxy(mworker_proxy);
William Lallemand309dc9a2018-10-26 14:47:45 +02002148}
2149
William Lallemand8a022572018-10-26 14:47:35 +02002150/*
2151 * Create the mworker CLI proxy
2152 */
2153int mworker_cli_proxy_create()
2154{
2155 struct mworker_proc *child;
William Lallemand744a0892018-11-22 16:46:51 +01002156 char *msg = NULL;
2157 char *errmsg = NULL;
William Lallemand8a022572018-10-26 14:47:35 +02002158
2159 mworker_proxy = calloc(1, sizeof(*mworker_proxy));
2160 if (!mworker_proxy)
2161 return -1;
2162
2163 init_new_proxy(mworker_proxy);
2164 mworker_proxy->next = proxies_list;
2165 proxies_list = mworker_proxy;
2166 mworker_proxy->id = strdup("MASTER");
William Lallemandcf62f7e2018-10-26 14:47:40 +02002167 mworker_proxy->mode = PR_MODE_CLI;
William Lallemand8a022572018-10-26 14:47:35 +02002168 mworker_proxy->state = PR_STNEW;
2169 mworker_proxy->last_change = now.tv_sec;
2170 mworker_proxy->cap = PR_CAP_LISTEN; /* this is a listen section */
2171 mworker_proxy->maxconn = 10; /* default to 10 concurrent connections */
2172 mworker_proxy->timeout.client = 0; /* no timeout */
2173 mworker_proxy->conf.file = strdup("MASTER");
2174 mworker_proxy->conf.line = 0;
2175 mworker_proxy->accept = frontend_accept;
2176 mworker_proxy-> lbprm.algo = BE_LB_ALGO_NONE;
2177
2178 /* Does not init the default target the CLI applet, but must be done in
2179 * the request parsing code */
2180 mworker_proxy->default_target = NULL;
2181
2182 /* the check_config_validity() will get an ID for the proxy */
2183 mworker_proxy->uuid = -1;
2184
2185 proxy_store_name(mworker_proxy);
2186
2187 /* create all servers using the mworker_proc list */
2188 list_for_each_entry(child, &proc_list, list) {
William Lallemand8a022572018-10-26 14:47:35 +02002189 struct server *newsrv = NULL;
2190 struct sockaddr_storage *sk;
2191 int port1, port2, port;
2192 struct protocol *proto;
William Lallemand8a022572018-10-26 14:47:35 +02002193
2194 newsrv = new_server(mworker_proxy);
2195 if (!newsrv)
William Lallemand744a0892018-11-22 16:46:51 +01002196 goto error;
William Lallemand8a022572018-10-26 14:47:35 +02002197
2198 /* we don't know the new pid yet */
2199 if (child->pid == -1)
2200 memprintf(&msg, "cur-%d", child->relative_pid);
2201 else
2202 memprintf(&msg, "old-%d", child->pid);
2203
2204 newsrv->next = mworker_proxy->srv;
2205 mworker_proxy->srv = newsrv;
2206 newsrv->conf.file = strdup(msg);
2207 newsrv->id = strdup(msg);
2208 newsrv->conf.line = 0;
2209
2210 memprintf(&msg, "sockpair@%d", child->ipc_fd[0]);
Tim Duesterhus4cae3b22018-11-22 16:46:50 +01002211 if ((sk = str2sa_range(msg, &port, &port1, &port2, &errmsg, NULL, NULL, 0)) == 0) {
William Lallemand744a0892018-11-22 16:46:51 +01002212 goto error;
Tim Duesterhus4cae3b22018-11-22 16:46:50 +01002213 }
2214 free(msg);
2215 msg = NULL;
William Lallemand8a022572018-10-26 14:47:35 +02002216
2217 proto = protocol_by_family(sk->ss_family);
2218 if (!proto || !proto->connect) {
William Lallemand744a0892018-11-22 16:46:51 +01002219 goto error;
William Lallemand8a022572018-10-26 14:47:35 +02002220 }
2221
2222 /* no port specified */
2223 newsrv->flags |= SRV_F_MAPPORTS;
2224 newsrv->addr = *sk;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002225 /* don't let the server participate to load balancing */
2226 newsrv->iweight = 0;
2227 newsrv->uweight = 0;
William Lallemand8a022572018-10-26 14:47:35 +02002228 srv_lb_commit_status(newsrv);
William Lallemand291810d2018-10-26 14:47:38 +02002229
2230 child->srv = newsrv;
William Lallemand8a022572018-10-26 14:47:35 +02002231 }
2232 return 0;
William Lallemand744a0892018-11-22 16:46:51 +01002233
2234error:
2235 ha_alert("%s\n", errmsg);
2236
2237 list_for_each_entry(child, &proc_list, list) {
2238 free((char *)child->srv->conf.file); /* cast because of const char * */
2239 free(child->srv->id);
2240 free(child->srv);
2241 child->srv = NULL;
2242 }
2243 free(mworker_proxy->id);
2244 free(mworker_proxy->conf.file);
2245 free(mworker_proxy);
2246 mworker_proxy = NULL;
2247 free(errmsg);
2248 free(msg);
2249
2250 return -1;
William Lallemand8a022572018-10-26 14:47:35 +02002251}
Olivier Houchardf886e342017-04-05 22:24:59 +02002252
William Lallemandce83b4a2018-10-26 14:47:30 +02002253/*
William Lallemande7361152018-10-26 14:47:36 +02002254 * Create a new listener for the master CLI proxy
2255 */
2256int mworker_cli_proxy_new_listener(char *line)
2257{
2258 struct bind_conf *bind_conf;
2259 struct listener *l;
2260 char *err = NULL;
2261 char *args[MAX_LINE_ARGS + 1];
2262 int arg;
2263 int cur_arg;
2264
2265 arg = 0;
2266 args[0] = line;
2267
2268 /* args is a bind configuration with spaces replaced by commas */
2269 while (*line && arg < MAX_LINE_ARGS) {
2270
2271 if (*line == ',') {
2272 *line++ = '\0';
2273 while (*line == ',')
2274 line++;
2275 args[++arg] = line;
2276 }
2277 line++;
2278 }
2279
2280 args[++arg] = "\0";
2281
2282 bind_conf = bind_conf_alloc(mworker_proxy, "master-socket", 0, "", xprt_get(XPRT_RAW));
William Lallemand744a0892018-11-22 16:46:51 +01002283 if (!bind_conf)
2284 goto err;
William Lallemande7361152018-10-26 14:47:36 +02002285
2286 bind_conf->level &= ~ACCESS_LVL_MASK;
2287 bind_conf->level |= ACCESS_LVL_ADMIN;
2288
2289 if (!str2listener(args[0], mworker_proxy, bind_conf, "master-socket", 0, &err)) {
2290 ha_alert("Cannot create the listener of the master CLI\n");
William Lallemand744a0892018-11-22 16:46:51 +01002291 goto err;
William Lallemande7361152018-10-26 14:47:36 +02002292 }
2293
2294 cur_arg = 1;
2295
2296 while (*args[cur_arg]) {
2297 static int bind_dumped;
2298 struct bind_kw *kw;
2299
2300 kw = bind_find_kw(args[cur_arg]);
2301 if (kw) {
2302 if (!kw->parse) {
2303 memprintf(&err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
2304 args[0], args[1], args[cur_arg]);
2305 goto err;
2306 }
2307
2308 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, &err) != 0) {
2309 if (err)
2310 memprintf(&err, "'%s %s' : '%s'", args[0], args[1], err);
2311 else
2312 memprintf(&err, "'%s %s' : error encountered while processing '%s'",
2313 args[0], args[1], args[cur_arg]);
2314 goto err;
2315 }
2316
2317 cur_arg += 1 + kw->skip;
2318 continue;
2319 }
2320
2321 if (!bind_dumped) {
2322 bind_dump_kws(&err);
2323 indent_msg(&err, 4);
2324 bind_dumped = 1;
2325 }
2326
2327 memprintf(&err, "'%s %s' : unknown keyword '%s'.%s%s",
2328 args[0], args[1], args[cur_arg],
2329 err ? " Registered keywords :" : "", err ? err : "");
2330 goto err;
2331 }
2332
2333
2334 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
2335 l->maxconn = 10;
2336 l->backlog = 10;
2337 l->accept = session_accept_fd;
2338 l->default_target = mworker_proxy->default_target;
2339 /* don't make the peers subject to global limits and don't close it in the master */
2340 l->options |= (LI_O_UNLIMITED|LI_O_MWORKER); /* we are keeping this FD in the master */
2341 l->nice = -64; /* we want to boost priority for local stats */
2342 global.maxsock += l->maxconn;
2343 }
2344
2345 return 0;
2346
2347err:
2348 ha_alert("%s\n", err);
William Lallemand744a0892018-11-22 16:46:51 +01002349 free(err);
2350 free(bind_conf);
William Lallemande7361152018-10-26 14:47:36 +02002351 return -1;
2352
2353}
2354
2355/*
William Lallemandce83b4a2018-10-26 14:47:30 +02002356 * Create a new CLI socket using a socketpair for a worker process
2357 * <mworker_proc> is the process structure, and <proc> is the process number
2358 */
2359int mworker_cli_sockpair_new(struct mworker_proc *mworker_proc, int proc)
2360{
2361 struct bind_conf *bind_conf;
2362 struct listener *l;
2363 char *path = NULL;
2364 char *err = NULL;
2365
2366 /* master pipe to ensure the master is still alive */
2367 if (socketpair(AF_UNIX, SOCK_STREAM, 0, mworker_proc->ipc_fd) < 0) {
2368 ha_alert("Cannot create worker socketpair.\n");
2369 return -1;
2370 }
2371
2372 /* XXX: we might want to use a separate frontend at some point */
2373 if (!global.stats_fe) {
2374 if ((global.stats_fe = alloc_stats_fe("GLOBAL", "master-socket", 0)) == NULL) {
2375 ha_alert("out of memory trying to allocate the stats frontend");
William Lallemand744a0892018-11-22 16:46:51 +01002376 goto error;
William Lallemandce83b4a2018-10-26 14:47:30 +02002377 }
2378 }
2379
2380 bind_conf = bind_conf_alloc(global.stats_fe, "master-socket", 0, "", xprt_get(XPRT_RAW));
William Lallemand744a0892018-11-22 16:46:51 +01002381 if (!bind_conf)
2382 goto error;
2383
William Lallemandce83b4a2018-10-26 14:47:30 +02002384 bind_conf->level &= ~ACCESS_LVL_MASK;
2385 bind_conf->level |= ACCESS_LVL_ADMIN; /* TODO: need to lower the rights with a CLI keyword*/
2386
2387 bind_conf->bind_proc = 1UL << proc;
2388 global.stats_fe->bind_proc = 0; /* XXX: we should be careful with that, it can be removed by configuration */
2389
2390 if (!memprintf(&path, "sockpair@%d", mworker_proc->ipc_fd[1])) {
2391 ha_alert("Cannot allocate listener.\n");
William Lallemand744a0892018-11-22 16:46:51 +01002392 goto error;
William Lallemandce83b4a2018-10-26 14:47:30 +02002393 }
2394
2395 if (!str2listener(path, global.stats_fe, bind_conf, "master-socket", 0, &err)) {
Tim Duesterhus4cae3b22018-11-22 16:46:50 +01002396 free(path);
William Lallemandce83b4a2018-10-26 14:47:30 +02002397 ha_alert("Cannot create a CLI sockpair listener for process #%d\n", proc);
William Lallemand744a0892018-11-22 16:46:51 +01002398 goto error;
William Lallemandce83b4a2018-10-26 14:47:30 +02002399 }
Tim Duesterhus4cae3b22018-11-22 16:46:50 +01002400 free(path);
2401 path = NULL;
William Lallemandce83b4a2018-10-26 14:47:30 +02002402
2403 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
2404 l->maxconn = global.stats_fe->maxconn;
2405 l->backlog = global.stats_fe->backlog;
2406 l->accept = session_accept_fd;
2407 l->default_target = global.stats_fe->default_target;
William Lallemanda3372292018-11-16 16:57:22 +01002408 l->options |= (LI_O_UNLIMITED | LI_O_NOSTOP);
William Lallemandce83b4a2018-10-26 14:47:30 +02002409 /* it's a sockpair but we don't want to keep the fd in the master */
2410 l->options &= ~LI_O_INHERITED;
2411 l->nice = -64; /* we want to boost priority for local stats */
2412 global.maxsock += l->maxconn;
2413 }
2414
2415 return 0;
William Lallemand744a0892018-11-22 16:46:51 +01002416
2417error:
2418 close(mworker_proc->ipc_fd[0]);
2419 close(mworker_proc->ipc_fd[1]);
2420 free(err);
2421
2422 return -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02002423}
2424
William Lallemand74c24fb2016-11-21 17:18:36 +01002425static struct applet cli_applet = {
2426 .obj_type = OBJ_TYPE_APPLET,
2427 .name = "<CLI>", /* used for logging */
2428 .fct = cli_io_handler,
2429 .release = cli_release_handler,
2430};
2431
Willy Tarreau0a739292016-11-22 20:21:23 +01002432/* register cli keywords */
2433static struct cli_kw_list cli_kws = {{ },{
William Lallemand4e8450b2018-10-26 14:47:43 +02002434 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
2435 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
2436 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002437 { { "help", NULL }, NULL, cli_parse_simple, NULL },
2438 { { "prompt", NULL }, NULL, cli_parse_simple, NULL },
2439 { { "quit", NULL }, NULL, cli_parse_simple, NULL },
Willy Tarreau2af99412016-11-23 11:10:59 +01002440 { { "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 +01002441 { { "set", "rate-limit", NULL }, "set rate-limit : change a rate limiting value", cli_parse_set_ratelimit, NULL },
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02002442 { { "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 +01002443 { { "set", "timeout", NULL }, "set timeout : change a timeout setting", cli_parse_set_timeout, NULL, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01002444 { { "show", "env", NULL }, "show env [var] : dump environment variables known to the process", cli_parse_show_env, cli_io_handler_show_env, NULL },
William Lallemand35851fb2018-10-26 14:47:42 +02002445 { { "show", "cli", "sockets", NULL }, "show cli sockets : dump list of cli sockets", cli_parse_default, cli_io_handler_show_cli_sock, NULL, NULL, ACCESS_MASTER },
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02002446 { { "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 +01002447 { { "show", "activity", NULL }, "show activity : show per-thread activity stats (for support/developers)", cli_parse_default, cli_io_handler_show_activity, NULL },
William Lallemandb9f9e3b2018-10-26 14:47:39 +02002448 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
Olivier Houchardf886e342017-04-05 22:24:59 +02002449 { { "_getsocks", NULL }, NULL, _getsocks, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01002450 {{},}
2451}};
2452
Willy Tarreau0108d902018-11-25 19:14:37 +01002453INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
2454
William Lallemand74c24fb2016-11-21 17:18:36 +01002455static struct cfg_kw_list cfg_kws = {ILH, {
2456 { CFG_GLOBAL, "stats", stats_parse_global },
2457 { 0, NULL, NULL },
2458}};
2459
Willy Tarreau0108d902018-11-25 19:14:37 +01002460INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2461
William Lallemand74c24fb2016-11-21 17:18:36 +01002462static struct bind_kw_list bind_kws = { "STAT", { }, {
William Lallemandf6975e92017-05-26 17:42:10 +02002463 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
2464 { "expose-fd", bind_parse_expose_fd, 1 }, /* set the unix socket expose fd rights */
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02002465 { "severity-output", bind_parse_severity_output, 1 }, /* set the severity output format */
William Lallemand74c24fb2016-11-21 17:18:36 +01002466 { NULL, NULL, 0 },
2467}};
2468
Willy Tarreau0108d902018-11-25 19:14:37 +01002469INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
William Lallemand74c24fb2016-11-21 17:18:36 +01002470
2471/*
2472 * Local variables:
2473 * c-indent-level: 8
2474 * c-basic-offset: 8
2475 * End:
2476 */