blob: 7eb3bc2a832b3519081a3891735e30de1d360b41 [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
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020029#include <haproxy/api.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010030#include <common/cfgparse.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020031#include <haproxy/dns-t.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020032#include <haproxy/frontend.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020033#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020034#include <haproxy/listener.h>
Willy Tarreaub5abe5b2020-06-04 14:07:37 +020035#include <haproxy/mworker-t.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020036#include <haproxy/tools.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020037#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020038#include <haproxy/time.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010039#include <common/uri_auth.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020040#include <haproxy/version.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020041#include <haproxy/base64.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010042
43#include <types/applet.h>
44#include <types/global.h>
William Lallemand9ed62032016-11-21 17:49:11 +010045#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010046
Willy Tarreaua04ded52020-06-02 10:29:48 +020047#include <haproxy/activity.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010048#include <proto/backend.h>
49#include <proto/channel.h>
50#include <proto/checks.h>
Willy Tarreau9d008692019-08-09 11:21:01 +020051#include <proto/cli.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020052#include <haproxy/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010053#include <proto/stats.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020054#include <haproxy/fd.h>
Willy Tarreau66347942020-06-01 12:18:08 +020055#include <haproxy/freq_ctr.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010056#include <proto/log.h>
57#include <proto/pattern.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020058#include <haproxy/pipe.h>
Willy Tarreau2dd7c352020-06-03 15:26:55 +020059#include <haproxy/protocol.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010060#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010061#include <proto/proxy.h>
62#include <proto/sample.h>
63#include <proto/session.h>
64#include <proto/stream.h>
65#include <proto/server.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066#include <proto/stream_interface.h>
67#include <proto/task.h>
68
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020069#define PAYLOAD_PATTERN "<<"
70
William Lallemand74c24fb2016-11-21 17:18:36 +010071static struct applet cli_applet;
72
73static const char stats_sock_usage_msg[] =
74 "Unknown command. Please enter one of the following commands only :\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010075 " help : this message\n"
76 " prompt : toggle interactive mode with prompt\n"
77 " quit : disconnect\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010078 "";
79
80static const char stats_permission_denied_msg[] =
81 "Permission denied\n"
82 "";
83
84
Christopher Faulet1bc04c72017-10-29 20:14:08 +010085static THREAD_LOCAL char *dynamic_usage_msg = NULL;
William Lallemand74c24fb2016-11-21 17:18:36 +010086
87/* List head of cli keywords */
88static struct cli_kw_list cli_keywords = {
89 .list = LIST_HEAD_INIT(cli_keywords.list)
90};
91
92extern const char *stat_status_codes[];
93
William Lallemand8a022572018-10-26 14:47:35 +020094static struct proxy *mworker_proxy; /* CLI proxy of the master */
95
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020096static char *cli_gen_usage_msg(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +010097{
98 struct cli_kw_list *kw_list;
99 struct cli_kw *kw;
Willy Tarreau83061a82018-07-13 11:56:34 +0200100 struct buffer *tmp = get_trash_chunk();
101 struct buffer out;
William Lallemand74c24fb2016-11-21 17:18:36 +0100102
103 free(dynamic_usage_msg);
104 dynamic_usage_msg = NULL;
105
106 if (LIST_ISEMPTY(&cli_keywords.list))
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200107 goto end;
William Lallemand74c24fb2016-11-21 17:18:36 +0100108
109 chunk_reset(tmp);
110 chunk_strcat(tmp, stats_sock_usage_msg);
111 list_for_each_entry(kw_list, &cli_keywords.list, list) {
112 kw = &kw_list->kw[0];
William Lallemand0154edc2018-05-15 11:50:04 +0200113 while (kw->str_kw[0]) {
William Lallemand14721be2018-10-26 14:47:37 +0200114
115 /* in a worker or normal process, don't display master only commands */
116 if (master == 0 && (kw->level & ACCESS_MASTER_ONLY))
117 goto next_kw;
118
119 /* in master don't displays if we don't have the master bits */
120 if (master == 1 && !(kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)))
121 goto next_kw;
122
Willy Tarreauabb9f9b2019-10-24 17:55:53 +0200123 /* only show expert commands in expert mode */
124 if ((kw->level & ~appctx->cli_level) & ACCESS_EXPERT)
125 goto next_kw;
126
William Lallemand0154edc2018-05-15 11:50:04 +0200127 if (kw->usage)
128 chunk_appendf(tmp, " %s\n", kw->usage);
William Lallemand14721be2018-10-26 14:47:37 +0200129
130next_kw:
131
William Lallemand74c24fb2016-11-21 17:18:36 +0100132 kw++;
133 }
134 }
135 chunk_init(&out, NULL, 0);
136 chunk_dup(&out, tmp);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200137 dynamic_usage_msg = out.area;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200138
139end:
140 if (dynamic_usage_msg) {
141 appctx->ctx.cli.severity = LOG_INFO;
142 appctx->ctx.cli.msg = dynamic_usage_msg;
143 }
144 else {
145 appctx->ctx.cli.severity = LOG_INFO;
146 appctx->ctx.cli.msg = stats_sock_usage_msg;
147 }
148 appctx->st0 = CLI_ST_PRINT;
149
William Lallemand74c24fb2016-11-21 17:18:36 +0100150 return dynamic_usage_msg;
151}
152
153struct cli_kw* cli_find_kw(char **args)
154{
155 struct cli_kw_list *kw_list;
156 struct cli_kw *kw;/* current cli_kw */
157 char **tmp_args;
158 const char **tmp_str_kw;
159 int found = 0;
160
161 if (LIST_ISEMPTY(&cli_keywords.list))
162 return NULL;
163
164 list_for_each_entry(kw_list, &cli_keywords.list, list) {
165 kw = &kw_list->kw[0];
166 while (*kw->str_kw) {
167 tmp_args = args;
168 tmp_str_kw = kw->str_kw;
169 while (*tmp_str_kw) {
170 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
171 found = 1;
172 } else {
173 found = 0;
174 break;
175 }
176 tmp_args++;
177 tmp_str_kw++;
178 }
179 if (found)
180 return (kw);
181 kw++;
182 }
183 }
184 return NULL;
185}
186
187void cli_register_kw(struct cli_kw_list *kw_list)
188{
189 LIST_ADDQ(&cli_keywords.list, &kw_list->list);
190}
191
192
193/* allocate a new stats frontend named <name>, and return it
194 * (or NULL in case of lack of memory).
195 */
196static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
197{
198 struct proxy *fe;
199
200 fe = calloc(1, sizeof(*fe));
201 if (!fe)
202 return NULL;
203
204 init_new_proxy(fe);
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100205 fe->next = proxies_list;
206 proxies_list = fe;
William Lallemand74c24fb2016-11-21 17:18:36 +0100207 fe->last_change = now.tv_sec;
208 fe->id = strdup("GLOBAL");
209 fe->cap = PR_CAP_FE;
210 fe->maxconn = 10; /* default to 10 concurrent connections */
211 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
212 fe->conf.file = strdup(file);
213 fe->conf.line = line;
214 fe->accept = frontend_accept;
215 fe->default_target = &cli_applet.obj_type;
216
217 /* the stats frontend is the only one able to assign ID #0 */
218 fe->conf.id.key = fe->uuid = 0;
219 eb32_insert(&used_proxy_id, &fe->conf.id);
220 return fe;
221}
222
223/* This function parses a "stats" statement in the "global" section. It returns
224 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
225 * error message into the <err> buffer which will be preallocated. The trailing
226 * '\n' must not be written. The function must be called with <args> pointing to
227 * the first word after "stats".
228 */
229static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
230 struct proxy *defpx, const char *file, int line,
231 char **err)
232{
233 struct bind_conf *bind_conf;
234 struct listener *l;
235
236 if (!strcmp(args[1], "socket")) {
237 int cur_arg;
238
239 if (*args[2] == 0) {
240 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
241 return -1;
242 }
243
244 if (!global.stats_fe) {
245 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
246 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
247 return -1;
248 }
249 }
250
Willy Tarreaua261e9b2016-12-22 20:44:00 +0100251 bind_conf = bind_conf_alloc(global.stats_fe, file, line, args[2], xprt_get(XPRT_RAW));
William Lallemand07a62f72017-05-24 00:57:40 +0200252 bind_conf->level &= ~ACCESS_LVL_MASK;
253 bind_conf->level |= ACCESS_LVL_OPER; /* default access level */
William Lallemand74c24fb2016-11-21 17:18:36 +0100254
255 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
256 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
257 file, line, args[0], args[1], err && *err ? *err : "error");
258 return -1;
259 }
260
261 cur_arg = 3;
262 while (*args[cur_arg]) {
263 static int bind_dumped;
264 struct bind_kw *kw;
265
266 kw = bind_find_kw(args[cur_arg]);
267 if (kw) {
268 if (!kw->parse) {
269 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
270 args[0], args[1], args[cur_arg]);
271 return -1;
272 }
273
274 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
275 if (err && *err)
276 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
277 else
278 memprintf(err, "'%s %s' : error encountered while processing '%s'",
279 args[0], args[1], args[cur_arg]);
280 return -1;
281 }
282
283 cur_arg += 1 + kw->skip;
284 continue;
285 }
286
287 if (!bind_dumped) {
288 bind_dump_kws(err);
289 indent_msg(err, 4);
290 bind_dumped = 1;
291 }
292
293 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
294 args[0], args[1], args[cur_arg],
295 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
296 return -1;
297 }
298
299 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100300 l->accept = session_accept_fd;
William Lallemand74c24fb2016-11-21 17:18:36 +0100301 l->default_target = global.stats_fe->default_target;
302 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
303 l->nice = -64; /* we want to boost priority for local stats */
Willy Tarreau18215cb2019-02-27 16:25:28 +0100304 global.maxsock++; /* for the listening socket */
William Lallemand74c24fb2016-11-21 17:18:36 +0100305 }
306 }
307 else if (!strcmp(args[1], "timeout")) {
308 unsigned timeout;
309 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
310
Willy Tarreau9faebe32019-06-07 19:00:37 +0200311 if (res == PARSE_TIME_OVER) {
312 memprintf(err, "timer overflow in argument '%s' to '%s %s' (maximum value is 2147483647 ms or ~24.8 days)",
313 args[2], args[0], args[1]);
314 return -1;
315 }
316 else if (res == PARSE_TIME_UNDER) {
317 memprintf(err, "timer underflow in argument '%s' to '%s %s' (minimum non-null value is 1 ms)",
318 args[2], args[0], args[1]);
319 return -1;
320 }
321 else if (res) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100322 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
323 return -1;
324 }
325
326 if (!timeout) {
327 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
328 return -1;
329 }
330 if (!global.stats_fe) {
331 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
332 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
333 return -1;
334 }
335 }
336 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
337 }
338 else if (!strcmp(args[1], "maxconn")) {
339 int maxconn = atol(args[2]);
340
341 if (maxconn <= 0) {
342 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
343 return -1;
344 }
345
346 if (!global.stats_fe) {
347 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
348 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
349 return -1;
350 }
351 }
352 global.stats_fe->maxconn = maxconn;
353 }
354 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
355 int cur_arg = 2;
356 unsigned long set = 0;
357
358 if (!global.stats_fe) {
359 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
360 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
361 return -1;
362 }
363 }
364
365 while (*args[cur_arg]) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100366 if (strcmp(args[cur_arg], "all") == 0) {
367 set = 0;
368 break;
369 }
Willy Tarreauff9c9142019-02-07 10:39:36 +0100370 if (parse_process_number(args[cur_arg], &set, MAX_PROCS, NULL, err)) {
Christopher Fauletf1f0c5f2017-11-22 12:06:43 +0100371 memprintf(err, "'%s %s' : %s", args[0], args[1], *err);
William Lallemand74c24fb2016-11-21 17:18:36 +0100372 return -1;
373 }
374 cur_arg++;
375 }
376 global.stats_fe->bind_proc = set;
377 }
378 else {
379 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
380 return -1;
381 }
382 return 0;
383}
384
William Lallemand33d29e22019-04-01 11:30:06 +0200385/*
William Lallemand9a37fd02019-04-12 16:09:24 +0200386 * This function exports the bound addresses of a <frontend> in the environment
387 * variable <varname>. Those addresses are separated by semicolons and prefixed
388 * with their type (abns@, unix@, sockpair@ etc)
389 * Return -1 upon error, 0 otherwise
William Lallemand33d29e22019-04-01 11:30:06 +0200390 */
William Lallemand9a37fd02019-04-12 16:09:24 +0200391int listeners_setenv(struct proxy *frontend, const char *varname)
William Lallemand33d29e22019-04-01 11:30:06 +0200392{
393 struct buffer *trash = get_trash_chunk();
394 struct bind_conf *bind_conf;
395
William Lallemand9a37fd02019-04-12 16:09:24 +0200396 if (frontend) {
397 list_for_each_entry(bind_conf, &frontend->conf.bind, by_fe) {
William Lallemand33d29e22019-04-01 11:30:06 +0200398 struct listener *l;
399
400 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
401 char addr[46];
402 char port[6];
403
William Lallemand620072b2019-04-12 16:09:25 +0200404 /* separate listener by semicolons */
405 if (trash->data)
406 chunk_appendf(trash, ";");
407
William Lallemand33d29e22019-04-01 11:30:06 +0200408 if (l->addr.ss_family == AF_UNIX) {
409 const struct sockaddr_un *un;
410
411 un = (struct sockaddr_un *)&l->addr;
412 if (un->sun_path[0] == '\0') {
413 chunk_appendf(trash, "abns@%s", un->sun_path+1);
414 } else {
415 chunk_appendf(trash, "unix@%s", un->sun_path);
416 }
417 } else if (l->addr.ss_family == AF_INET) {
418 addr_to_str(&l->addr, addr, sizeof(addr));
419 port_to_str(&l->addr, port, sizeof(port));
420 chunk_appendf(trash, "ipv4@%s:%s", addr, port);
421 } else if (l->addr.ss_family == AF_INET6) {
422 addr_to_str(&l->addr, addr, sizeof(addr));
423 port_to_str(&l->addr, port, sizeof(port));
424 chunk_appendf(trash, "ipv6@[%s]:%s", addr, port);
425 } else if (l->addr.ss_family == AF_CUST_SOCKPAIR) {
426 chunk_appendf(trash, "sockpair@%d", ((struct sockaddr_in *)&l->addr)->sin_addr.s_addr);
427 }
William Lallemand33d29e22019-04-01 11:30:06 +0200428 }
429 }
430 trash->area[trash->data++] = '\0';
William Lallemand9a37fd02019-04-12 16:09:24 +0200431 if (setenv(varname, trash->area, 1) < 0)
William Lallemand33d29e22019-04-01 11:30:06 +0200432 return -1;
433 }
434
435 return 0;
436}
437
William Lallemand9a37fd02019-04-12 16:09:24 +0200438int cli_socket_setenv()
439{
440 if (listeners_setenv(global.stats_fe, "HAPROXY_CLI") < 0)
441 return -1;
442 if (listeners_setenv(mworker_proxy, "HAPROXY_MASTER_CLI") < 0)
443 return -1;
444
445 return 0;
446}
447
William Lallemand33d29e22019-04-01 11:30:06 +0200448REGISTER_CONFIG_POSTPARSER("cli", cli_socket_setenv);
449
Willy Tarreaude57a572016-11-23 17:01:39 +0100450/* Verifies that the CLI at least has a level at least as high as <level>
451 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
452 * failure, an error message is prepared and the appctx's state is adjusted
453 * to print it so that a return 1 is enough to abort any processing.
454 */
455int cli_has_level(struct appctx *appctx, int level)
456{
Willy Tarreaude57a572016-11-23 17:01:39 +0100457
William Lallemandf630d012018-12-13 09:05:44 +0100458 if ((appctx->cli_level & ACCESS_LVL_MASK) < level) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200459 cli_err(appctx, stats_permission_denied_msg);
Willy Tarreaude57a572016-11-23 17:01:39 +0100460 return 0;
461 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100462 return 1;
463}
464
William Lallemandb7ea1412018-12-13 09:05:47 +0100465/* same as cli_has_level but for the CLI proxy and without error message */
466int pcli_has_level(struct stream *s, int level)
467{
468 if ((s->pcli_flags & ACCESS_LVL_MASK) < level) {
469 return 0;
470 }
471 return 1;
472}
473
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200474/* Returns severity_output for the current session if set, or default for the socket */
475static int cli_get_severity_output(struct appctx *appctx)
476{
477 if (appctx->cli_severity_output)
478 return appctx->cli_severity_output;
479 return strm_li(si_strm(appctx->owner))->bind_conf->severity_output;
480}
William Lallemand74c24fb2016-11-21 17:18:36 +0100481
Willy Tarreau41908562016-11-24 16:23:38 +0100482/* Processes the CLI interpreter on the stats socket. This function is called
483 * from the CLI's IO handler running in an appctx context. The function returns 1
484 * if the request was understood, otherwise zero. It is called with appctx->st0
485 * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do
486 * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to
487 * have its own I/O handler called again. Most of the time, parsers will only
488 * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg.
Willy Tarreaueaffde32016-12-16 17:59:25 +0100489 * If a keyword parser is NULL and an I/O handler is declared, the I/O handler
490 * will automatically be used.
William Lallemand74c24fb2016-11-21 17:18:36 +0100491 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200492static int cli_parse_request(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100493{
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200494 char *args[MAX_STATS_ARGS + 1], *p, *end, *payload = NULL;
495 int i = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100496 struct cli_kw *kw;
William Lallemand74c24fb2016-11-21 17:18:36 +0100497
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200498 appctx->st2 = 0;
499 memset(&appctx->ctx.cli, 0, sizeof(appctx->ctx.cli));
William Lallemand74c24fb2016-11-21 17:18:36 +0100500
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 p = appctx->chunk->area;
502 end = p + appctx->chunk->data;
William Lallemand74c24fb2016-11-21 17:18:36 +0100503
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200504 /*
505 * Get the payload start if there is one.
506 * For the sake of simplicity, the payload pattern is looked up
507 * everywhere from the start of the input but it can only be found
508 * at the end of the first line if APPCTX_CLI_ST1_PAYLOAD is set.
509 *
510 * The input string was zero terminated so it is safe to use
511 * the str*() functions throughout the parsing
512 */
513 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
514 payload = strstr(p, PAYLOAD_PATTERN);
515 end = payload;
516 /* skip the pattern */
517 payload += strlen(PAYLOAD_PATTERN);
518 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100519
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200520 /*
521 * Get pointers on words.
522 * One extra slot is reserved to store a pointer on a null byte.
523 */
524 while (i < MAX_STATS_ARGS && p < end) {
525 int j, k;
William Lallemand74c24fb2016-11-21 17:18:36 +0100526
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200527 /* skip leading spaces/tabs */
528 p += strspn(p, " \t");
529 if (!*p)
530 break;
William Lallemand74c24fb2016-11-21 17:18:36 +0100531
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200532 args[i] = p;
533 p += strcspn(p, " \t");
534 *p++ = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100535
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200536 /* unescape backslashes (\) */
537 for (j = 0, k = 0; args[i][k]; k++) {
538 if (args[i][k] == '\\') {
539 if (args[i][k + 1] == '\\')
540 k++;
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100541 else
542 continue;
543 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200544 args[i][j] = args[i][k];
William Lallemand74c24fb2016-11-21 17:18:36 +0100545 j++;
546 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200547 args[i][j] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100548
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200549 i++;
550 }
551 /* fill unused slots */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200552 p = appctx->chunk->area + appctx->chunk->data;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200553 for (; i < MAX_STATS_ARGS + 1; i++)
554 args[i] = p;
Willy Tarreau41908562016-11-24 16:23:38 +0100555
556 kw = cli_find_kw(args);
Willy Tarreaueaffde32016-12-16 17:59:25 +0100557 if (!kw)
Willy Tarreau41908562016-11-24 16:23:38 +0100558 return 0;
559
William Lallemand14721be2018-10-26 14:47:37 +0200560 /* in a worker or normal process, don't display master only commands */
561 if (master == 0 && (kw->level & ACCESS_MASTER_ONLY))
562 return 0;
563
564 /* in master don't displays if we don't have the master bits */
565 if (master == 1 && !(kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)))
566 return 0;
567
Willy Tarreauabb9f9b2019-10-24 17:55:53 +0200568 /* only accept expert commands in expert mode */
569 if ((kw->level & ~appctx->cli_level) & ACCESS_EXPERT)
570 return 0;
571
Willy Tarreau41908562016-11-24 16:23:38 +0100572 appctx->io_handler = kw->io_handler;
Emeric Brund6871f72017-06-29 19:54:13 +0200573 appctx->io_release = kw->io_release;
William Lallemand90b098c2019-10-25 21:10:14 +0200574
575 if (kw->parse && kw->parse(args, payload, appctx, kw->private) != 0)
576 goto fail;
577
578 /* kw->parse could set its own io_handler or io_release handler */
579 if (!appctx->io_handler)
580 goto fail;
581
582 appctx->st0 = CLI_ST_CALLBACK;
583 return 1;
584fail:
585 appctx->io_handler = NULL;
586 appctx->io_release = NULL;
Willy Tarreau41908562016-11-24 16:23:38 +0100587 return 1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100588}
589
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200590/* prepends then outputs the argument msg with a syslog-type severity depending on severity_output value */
591static int cli_output_msg(struct channel *chn, const char *msg, int severity, int severity_output)
592{
Willy Tarreau83061a82018-07-13 11:56:34 +0200593 struct buffer *tmp;
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200594
595 if (likely(severity_output == CLI_SEVERITY_NONE))
Willy Tarreau06d80a92017-10-19 14:32:15 +0200596 return ci_putblk(chn, msg, strlen(msg));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200597
598 tmp = get_trash_chunk();
599 chunk_reset(tmp);
600
601 if (severity < 0 || severity > 7) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100602 ha_warning("socket command feedback with invalid severity %d", severity);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200603 chunk_printf(tmp, "[%d]: ", severity);
604 }
605 else {
606 switch (severity_output) {
607 case CLI_SEVERITY_NUMBER:
608 chunk_printf(tmp, "[%d]: ", severity);
609 break;
610 case CLI_SEVERITY_STRING:
611 chunk_printf(tmp, "[%s]: ", log_levels[severity]);
612 break;
613 default:
Christopher Faulet767a84b2017-11-24 16:50:31 +0100614 ha_warning("Unrecognized severity output %d", severity_output);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200615 }
616 }
617 chunk_appendf(tmp, "%s", msg);
618
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200619 return ci_putblk(chn, tmp->area, strlen(tmp->area));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200620}
621
William Lallemand74c24fb2016-11-21 17:18:36 +0100622/* This I/O handler runs as an applet embedded in a stream interface. It is
623 * used to processes I/O from/to the stats unix socket. The system relies on a
624 * state machine handling requests and various responses. We read a request,
625 * then we process it and send the response, and we possibly display a prompt.
626 * Then we can read again. The state is stored in appctx->st0 and is one of the
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100627 * CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled
William Lallemand74c24fb2016-11-21 17:18:36 +0100628 * or not.
629 */
630static void cli_io_handler(struct appctx *appctx)
631{
632 struct stream_interface *si = appctx->owner;
633 struct channel *req = si_oc(si);
634 struct channel *res = si_ic(si);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200635 struct bind_conf *bind_conf = strm_li(si_strm(si))->bind_conf;
William Lallemand74c24fb2016-11-21 17:18:36 +0100636 int reql;
637 int len;
638
639 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
640 goto out;
641
Joseph Herlant008b3ce2018-11-25 12:51:45 -0800642 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200643 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +0100644 /* buf.size==0 means we failed to get a buffer and were
645 * already subscribed to a wait list to get a buffer.
646 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100647 goto out;
648 }
649
William Lallemand74c24fb2016-11-21 17:18:36 +0100650 while (1) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100651 if (appctx->st0 == CLI_ST_INIT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100652 /* Stats output not initialized yet */
653 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200654 /* reset severity to default at init */
655 appctx->cli_severity_output = bind_conf->severity_output;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100656 appctx->st0 = CLI_ST_GETREQ;
William Lallemandf630d012018-12-13 09:05:44 +0100657 appctx->cli_level = bind_conf->level;
William Lallemand74c24fb2016-11-21 17:18:36 +0100658 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100659 else if (appctx->st0 == CLI_ST_END) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100660 /* Let's close for real now. We just close the request
661 * side, the conditions below will complete if needed.
662 */
663 si_shutw(si);
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200664 free_trash_chunk(appctx->chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +0100665 break;
666 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100667 else if (appctx->st0 == CLI_ST_GETREQ) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200668 char *str;
669
670 /* use a trash chunk to store received data */
671 if (!appctx->chunk) {
672 appctx->chunk = alloc_trash_chunk();
673 if (!appctx->chunk) {
674 appctx->st0 = CLI_ST_END;
675 continue;
676 }
677 }
678
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200679 str = appctx->chunk->area + appctx->chunk->data;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200680
William Lallemand74c24fb2016-11-21 17:18:36 +0100681 /* ensure we have some output room left in the event we
682 * would want to return some info right after parsing.
683 */
684 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100685 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +0100686 break;
687 }
688
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200689 /* '- 1' is to ensure a null byte can always be inserted at the end */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200690 reql = co_getline(si_oc(si), str,
691 appctx->chunk->size - appctx->chunk->data - 1);
William Lallemand74c24fb2016-11-21 17:18:36 +0100692 if (reql <= 0) { /* closed or EOL not found */
693 if (reql == 0)
694 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100695 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100696 continue;
697 }
698
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200699 if (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)) {
700 /* seek for a possible unescaped semi-colon. If we find
701 * one, we replace it with an LF and skip only this part.
702 */
703 for (len = 0; len < reql; len++) {
704 if (str[len] == '\\') {
705 len++;
706 continue;
707 }
708 if (str[len] == ';') {
709 str[len] = '\n';
710 reql = len + 1;
711 break;
712 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100713 }
714 }
715
716 /* now it is time to check that we have a full line,
717 * remove the trailing \n and possibly \r, then cut the
718 * line.
719 */
720 len = reql - 1;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200721 if (str[len] != '\n') {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100722 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100723 continue;
724 }
725
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200726 if (len && str[len-1] == '\r')
William Lallemand74c24fb2016-11-21 17:18:36 +0100727 len--;
728
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200729 str[len] = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200730 appctx->chunk->data += len;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200731
732 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200733 appctx->chunk->area[appctx->chunk->data] = '\n';
734 appctx->chunk->area[appctx->chunk->data + 1] = 0;
735 appctx->chunk->data++;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200736 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100737
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100738 appctx->st0 = CLI_ST_PROMPT;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200739
740 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
741 /* empty line */
742 if (!len) {
743 /* remove the last two \n */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200744 appctx->chunk->data -= 2;
745 appctx->chunk->area[appctx->chunk->data] = 0;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200746
747 if (!cli_parse_request(appctx))
748 cli_gen_usage_msg(appctx);
749
750 chunk_reset(appctx->chunk);
751 /* NB: cli_sock_parse_request() may have put
752 * another CLI_ST_O_* into appctx->st0.
753 */
754
755 appctx->st1 &= ~APPCTX_CLI_ST1_PAYLOAD;
William Lallemand74c24fb2016-11-21 17:18:36 +0100756 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100757 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200758 else {
759 /*
760 * Look for the "payload start" pattern at the end of a line
761 * Its location is not remembered here, this is just to switch
762 * to a gathering mode.
William Lallemand74c24fb2016-11-21 17:18:36 +0100763 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200764 if (!strcmp(appctx->chunk->area + appctx->chunk->data - strlen(PAYLOAD_PATTERN), PAYLOAD_PATTERN))
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200765 appctx->st1 |= APPCTX_CLI_ST1_PAYLOAD;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200766 else {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200767 /* no payload, the command is complete: parse the request */
768 if (!cli_parse_request(appctx))
769 cli_gen_usage_msg(appctx);
770
771 chunk_reset(appctx->chunk);
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200772 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100773 }
774
775 /* re-adjust req buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200776 co_skip(si_oc(si), reql);
William Lallemand74c24fb2016-11-21 17:18:36 +0100777 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
778 }
779 else { /* output functions */
Willy Tarreaud50c7fe2019-08-09 09:57:36 +0200780 const char *msg;
781 int sev;
782
William Lallemand74c24fb2016-11-21 17:18:36 +0100783 switch (appctx->st0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100784 case CLI_ST_PROMPT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100785 break;
Willy Tarreaud50c7fe2019-08-09 09:57:36 +0200786 case CLI_ST_PRINT: /* print const message in msg */
787 case CLI_ST_PRINT_ERR: /* print const error in msg */
788 case CLI_ST_PRINT_DYN: /* print dyn message in msg, free */
789 case CLI_ST_PRINT_FREE: /* print dyn error in err, free */
790 if (appctx->st0 == CLI_ST_PRINT || appctx->st0 == CLI_ST_PRINT_ERR) {
791 sev = appctx->st0 == CLI_ST_PRINT_ERR ?
792 LOG_ERR : appctx->ctx.cli.severity;
793 msg = appctx->ctx.cli.msg;
794 }
795 else if (appctx->st0 == CLI_ST_PRINT_DYN || appctx->st0 == CLI_ST_PRINT_FREE) {
796 sev = appctx->st0 == CLI_ST_PRINT_FREE ?
797 LOG_ERR : appctx->ctx.cli.severity;
798 msg = appctx->ctx.cli.err;
799 if (!msg) {
800 sev = LOG_ERR;
801 msg = "Out of memory.\n";
802 }
803 }
804 else {
805 sev = LOG_ERR;
806 msg = "Internal error.\n";
807 }
Aurélien Nephtalic511b7c2018-04-16 18:50:19 +0200808
Willy Tarreaud50c7fe2019-08-09 09:57:36 +0200809 if (cli_output_msg(res, msg, sev, cli_get_severity_output(appctx)) != -1) {
810 if (appctx->st0 == CLI_ST_PRINT_FREE ||
811 appctx->st0 == CLI_ST_PRINT_DYN) {
812 free(appctx->ctx.cli.err);
813 appctx->ctx.cli.err = NULL;
814 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100815 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100816 }
817 else
Willy Tarreaudb398432018-11-15 11:08:52 +0100818 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +0100819 break;
Willy Tarreaud50c7fe2019-08-09 09:57:36 +0200820
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100821 case CLI_ST_CALLBACK: /* use custom pointer */
William Lallemand74c24fb2016-11-21 17:18:36 +0100822 if (appctx->io_handler)
823 if (appctx->io_handler(appctx)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100824 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100825 if (appctx->io_release) {
826 appctx->io_release(appctx);
827 appctx->io_release = NULL;
828 }
829 }
830 break;
831 default: /* abnormal state */
832 si->flags |= SI_FL_ERR;
833 break;
834 }
835
836 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100837 if (appctx->st0 == CLI_ST_PROMPT) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200838 const char *prompt = "";
839
840 if (appctx->st1 & APPCTX_CLI_ST1_PROMPT) {
841 /*
842 * when entering a payload with interactive mode, change the prompt
843 * to emphasize that more data can still be sent
844 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200845 if (appctx->chunk->data && appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200846 prompt = "+ ";
847 else
848 prompt = "\n> ";
849 }
850 else {
William Lallemandad032882019-07-01 10:56:15 +0200851 if (!(appctx->st1 & (APPCTX_CLI_ST1_PAYLOAD|APPCTX_CLI_ST1_NOLF)))
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200852 prompt = "\n";
853 }
854
855 if (ci_putstr(si_ic(si), prompt) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100856 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100857 else
Willy Tarreaudb398432018-11-15 11:08:52 +0100858 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +0100859 }
860
861 /* If the output functions are still there, it means they require more room. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100862 if (appctx->st0 >= CLI_ST_OUTPUT)
William Lallemand74c24fb2016-11-21 17:18:36 +0100863 break;
864
865 /* Now we close the output if one of the writers did so,
866 * or if we're not in interactive mode and the request
867 * buffer is empty. This still allows pipelined requests
868 * to be sent in non-interactive mode.
869 */
William Lallemand3de09d52018-12-11 16:10:56 +0100870 if (((res->flags & (CF_SHUTW|CF_SHUTW_NOW))) ||
871 (!(appctx->st1 & APPCTX_CLI_ST1_PROMPT) && !co_data(req) && (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)))) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100872 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100873 continue;
874 }
875
876 /* switch state back to GETREQ to read next requests */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100877 appctx->st0 = CLI_ST_GETREQ;
William Lallemandad032882019-07-01 10:56:15 +0200878 /* reactivate the \n at the end of the response for the next command */
879 appctx->st1 &= ~APPCTX_CLI_ST1_NOLF;
William Lallemand74c24fb2016-11-21 17:18:36 +0100880 }
881 }
882
883 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
884 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
885 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
886 /* Other side has closed, let's abort if we have no more processing to do
887 * and nothing more to consume. This is comparable to a broken pipe, so
888 * we forward the close to the request side so that it flows upstream to
889 * the client.
890 */
891 si_shutw(si);
892 }
893
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100894 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < CLI_ST_OUTPUT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100895 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
896 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
897 /* We have no more processing to do, and nothing more to send, and
898 * the client side has closed. So we'll forward this state downstream
899 * on the response buffer.
900 */
901 si_shutr(si);
902 res->flags |= CF_READ_NULL;
903 }
904
905 out:
Christopher Faulet45073512018-07-20 10:16:29 +0200906 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 +0100907 __FUNCTION__, __LINE__,
Christopher Faulet45073512018-07-20 10:16:29 +0200908 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 +0100909}
910
William Lallemand74c24fb2016-11-21 17:18:36 +0100911/* This is called when the stream interface is closed. For instance, upon an
912 * external abort, we won't call the i/o handler anymore so we may need to
913 * remove back references to the stream currently being dumped.
914 */
915static void cli_release_handler(struct appctx *appctx)
916{
917 if (appctx->io_release) {
918 appctx->io_release(appctx);
919 appctx->io_release = NULL;
920 }
Willy Tarreaud50c7fe2019-08-09 09:57:36 +0200921 else if (appctx->st0 == CLI_ST_PRINT_FREE || appctx->st0 == CLI_ST_PRINT_DYN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100922 free(appctx->ctx.cli.err);
923 appctx->ctx.cli.err = NULL;
924 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100925}
926
927/* This function dumps all environmnent variables to the buffer. It returns 0
928 * if the output buffer is full and it needs to be called again, otherwise
Willy Tarreauf6710f82016-12-16 17:45:44 +0100929 * non-zero. Dumps only one entry if st2 == STAT_ST_END. It uses cli.p0 as the
930 * pointer to the current variable.
William Lallemand74c24fb2016-11-21 17:18:36 +0100931 */
Willy Tarreau0a739292016-11-22 20:21:23 +0100932static int cli_io_handler_show_env(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100933{
Willy Tarreau0a739292016-11-22 20:21:23 +0100934 struct stream_interface *si = appctx->owner;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100935 char **var = appctx->ctx.cli.p0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100936
937 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
938 return 1;
939
940 chunk_reset(&trash);
941
942 /* we have two inner loops here, one for the proxy, the other one for
943 * the buffer.
944 */
Willy Tarreauf6710f82016-12-16 17:45:44 +0100945 while (*var) {
946 chunk_printf(&trash, "%s\n", *var);
William Lallemand74c24fb2016-11-21 17:18:36 +0100947
Willy Tarreau06d80a92017-10-19 14:32:15 +0200948 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100949 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +0100950 return 0;
951 }
952 if (appctx->st2 == STAT_ST_END)
953 break;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100954 var++;
955 appctx->ctx.cli.p0 = var;
William Lallemand74c24fb2016-11-21 17:18:36 +0100956 }
957
958 /* dump complete */
959 return 1;
960}
961
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200962/* This function dumps all file descriptors states (or the requested one) to
963 * the buffer. It returns 0 if the output buffer is full and it needs to be
964 * called again, otherwise non-zero. Dumps only one entry if st2 == STAT_ST_END.
965 * It uses cli.i0 as the fd number to restart from.
966 */
967static int cli_io_handler_show_fd(struct appctx *appctx)
968{
969 struct stream_interface *si = appctx->owner;
970 int fd = appctx->ctx.cli.i0;
Willy Tarreauca1b1572018-12-18 15:45:11 +0100971 int ret = 1;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200972
973 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreauca1b1572018-12-18 15:45:11 +0100974 goto end;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200975
976 chunk_reset(&trash);
977
Willy Tarreauca1b1572018-12-18 15:45:11 +0100978 /* isolate the threads once per round. We're limited to a buffer worth
979 * of output anyway, it cannot last very long.
980 */
981 thread_isolate();
982
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200983 /* we have two inner loops here, one for the proxy, the other one for
984 * the buffer.
985 */
Aurélien Nephtali498a1152018-03-09 18:51:16 +0100986 while (fd >= 0 && fd < global.maxsock) {
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200987 struct fdtab fdt;
Willy Tarreau286ec682017-08-09 16:35:44 +0200988 struct listener *li = NULL;
989 struct server *sv = NULL;
990 struct proxy *px = NULL;
Willy Tarreaua833cd92018-03-29 13:19:37 +0200991 const struct mux_ops *mux = NULL;
Willy Tarreau35b1b482018-03-28 18:41:30 +0200992 void *ctx = NULL;
Willy Tarreau286ec682017-08-09 16:35:44 +0200993 uint32_t conn_flags = 0;
Willy Tarreaue9ca8072018-12-19 18:40:58 +0100994 int is_back = 0;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +0200995
996 fdt = fdtab[fd];
997
Willy Tarreauca1b1572018-12-18 15:45:11 +0100998 if (!fdt.owner)
Willy Tarreau017af242017-10-04 20:24:54 +0200999 goto skip; // closed
1000
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001001 if (fdt.iocb == conn_fd_handler) {
1002 conn_flags = ((struct connection *)fdt.owner)->flags;
Willy Tarreau35b1b482018-03-28 18:41:30 +02001003 mux = ((struct connection *)fdt.owner)->mux;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001004 ctx = ((struct connection *)fdt.owner)->ctx;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001005 li = objt_listener(((struct connection *)fdt.owner)->target);
1006 sv = objt_server(((struct connection *)fdt.owner)->target);
1007 px = objt_proxy(((struct connection *)fdt.owner)->target);
Willy Tarreaue9ca8072018-12-19 18:40:58 +01001008 is_back = conn_is_back((struct connection *)fdt.owner);
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001009 }
1010 else if (fdt.iocb == listener_accept)
1011 li = fdt.owner;
1012
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001013 chunk_printf(&trash,
Willy Tarreaucf12f2e2020-03-03 17:29:58 +01001014 " %5d : st=0x%02x(R:%c%c W:%c%c) ev=0x%02x(%c%c%c%c%c) [%c%c] tmask=0x%lx umask=0x%lx owner=%p iocb=%p(",
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001015 fd,
1016 fdt.state,
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001017 (fdt.state & FD_EV_READY_R) ? 'R' : 'r',
1018 (fdt.state & FD_EV_ACTIVE_R) ? 'A' : 'a',
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001019 (fdt.state & FD_EV_READY_W) ? 'R' : 'r',
1020 (fdt.state & FD_EV_ACTIVE_W) ? 'A' : 'a',
1021 fdt.ev,
1022 (fdt.ev & FD_POLL_HUP) ? 'H' : 'h',
1023 (fdt.ev & FD_POLL_ERR) ? 'E' : 'e',
1024 (fdt.ev & FD_POLL_OUT) ? 'O' : 'o',
1025 (fdt.ev & FD_POLL_PRI) ? 'P' : 'p',
1026 (fdt.ev & FD_POLL_IN) ? 'I' : 'i',
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001027 fdt.linger_risk ? 'L' : 'l',
1028 fdt.cloned ? 'C' : 'c',
Willy Tarreauc754b342018-03-30 15:00:15 +02001029 fdt.thread_mask, fdt.update_mask,
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001030 fdt.owner,
Willy Tarreaucf12f2e2020-03-03 17:29:58 +01001031 fdt.iocb);
1032 resolve_sym_name(&trash, NULL, fdt.iocb);
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001033
1034 if (fdt.iocb == conn_fd_handler) {
Willy Tarreaucf12f2e2020-03-03 17:29:58 +01001035 chunk_appendf(&trash, ") back=%d cflg=0x%08x", is_back, conn_flags);
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001036 if (px)
1037 chunk_appendf(&trash, " px=%s", px->id);
1038 else if (sv)
1039 chunk_appendf(&trash, " sv=%s/%s", sv->id, sv->proxy->id);
1040 else if (li)
1041 chunk_appendf(&trash, " fe=%s", li->bind_conf->frontend->id);
Willy Tarreau35b1b482018-03-28 18:41:30 +02001042
Willy Tarreaub011d8f2018-03-30 14:41:19 +02001043 if (mux) {
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001044 chunk_appendf(&trash, " mux=%s ctx=%p", mux->name, ctx);
Willy Tarreaub011d8f2018-03-30 14:41:19 +02001045 if (mux->show_fd)
1046 mux->show_fd(&trash, fdt.owner);
1047 }
Willy Tarreau35b1b482018-03-28 18:41:30 +02001048 else
1049 chunk_appendf(&trash, " nomux");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001050 }
1051 else if (fdt.iocb == listener_accept) {
Willy Tarreaucf12f2e2020-03-03 17:29:58 +01001052 chunk_appendf(&trash, ") l.st=%s fe=%s",
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001053 listener_state_str(li),
1054 li->bind_conf->frontend->id);
1055 }
1056
Willy Tarreaucf12f2e2020-03-03 17:29:58 +01001057 chunk_appendf(&trash, ")\n");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001058
Willy Tarreau06d80a92017-10-19 14:32:15 +02001059 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001060 si_rx_room_blk(si);
Willy Tarreauca1b1572018-12-18 15:45:11 +01001061 appctx->ctx.cli.i0 = fd;
1062 ret = 0;
1063 break;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001064 }
1065 skip:
1066 if (appctx->st2 == STAT_ST_END)
1067 break;
1068
1069 fd++;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001070 }
1071
Willy Tarreauca1b1572018-12-18 15:45:11 +01001072 end:
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001073 /* dump complete */
Willy Tarreauca1b1572018-12-18 15:45:11 +01001074
1075 thread_release();
1076 return ret;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001077}
1078
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001079/* This function dumps some activity counters used by developers and support to
1080 * rule out some hypothesis during bug reports. It returns 0 if the output
1081 * buffer is full and it needs to be called again, otherwise non-zero. It dumps
1082 * everything at once in the buffer and is not designed to do it in multiple
1083 * passes.
1084 */
1085static int cli_io_handler_show_activity(struct appctx *appctx)
1086{
1087 struct stream_interface *si = appctx->owner;
1088 int thr;
1089
1090 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1091 return 1;
1092
1093 chunk_reset(&trash);
1094
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001095#undef SHOW_TOT
1096#define SHOW_TOT(t, x) \
Willy Tarreaua0211b82019-05-28 15:09:08 +02001097 do { \
1098 unsigned int _v[MAX_THREADS]; \
1099 unsigned int _tot; \
1100 const unsigned int _nbt = global.nbthread; \
1101 for (_tot = t = 0; t < _nbt; t++) \
1102 _tot += _v[t] = (x); \
1103 if (_nbt == 1) { \
1104 chunk_appendf(&trash, " %u\n", _tot); \
1105 break; \
1106 } \
1107 chunk_appendf(&trash, " %u [", _tot); \
1108 for (t = 0; t < _nbt; t++) \
1109 chunk_appendf(&trash, " %u", _v[t]); \
1110 chunk_appendf(&trash, " ]\n"); \
1111 } while (0)
1112
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001113#undef SHOW_AVG
1114#define SHOW_AVG(t, x) \
1115 do { \
1116 unsigned int _v[MAX_THREADS]; \
1117 unsigned int _tot; \
1118 const unsigned int _nbt = global.nbthread; \
1119 for (_tot = t = 0; t < _nbt; t++) \
1120 _tot += _v[t] = (x); \
1121 if (_nbt == 1) { \
1122 chunk_appendf(&trash, " %u\n", _tot); \
1123 break; \
1124 } \
1125 chunk_appendf(&trash, " %u [", (_tot + _nbt/2) / _nbt); \
1126 for (t = 0; t < _nbt; t++) \
1127 chunk_appendf(&trash, " %u", _v[t]); \
1128 chunk_appendf(&trash, " ]\n"); \
1129 } while (0)
1130
Willy Tarreaua0211b82019-05-28 15:09:08 +02001131 chunk_appendf(&trash, "thread_id: %u (%u..%u)\n", tid + 1, 1, global.nbthread);
1132 chunk_appendf(&trash, "date_now: %lu.%06lu\n", (long)now.tv_sec, (long)now.tv_usec);
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001133 chunk_appendf(&trash, "loops:"); SHOW_TOT(thr, activity[thr].loops);
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001134 chunk_appendf(&trash, "wake_tasks:"); SHOW_TOT(thr, activity[thr].wake_tasks);
1135 chunk_appendf(&trash, "wake_signal:"); SHOW_TOT(thr, activity[thr].wake_signal);
1136 chunk_appendf(&trash, "poll_exp:"); SHOW_TOT(thr, activity[thr].poll_exp);
1137 chunk_appendf(&trash, "poll_drop:"); SHOW_TOT(thr, activity[thr].poll_drop);
1138 chunk_appendf(&trash, "poll_dead:"); SHOW_TOT(thr, activity[thr].poll_dead);
1139 chunk_appendf(&trash, "poll_skip:"); SHOW_TOT(thr, activity[thr].poll_skip);
1140 chunk_appendf(&trash, "fd_lock:"); SHOW_TOT(thr, activity[thr].fd_lock);
1141 chunk_appendf(&trash, "conn_dead:"); SHOW_TOT(thr, activity[thr].conn_dead);
1142 chunk_appendf(&trash, "stream:"); SHOW_TOT(thr, activity[thr].stream);
Willy Tarreaua8b2ce02019-05-28 17:04:16 +02001143 chunk_appendf(&trash, "pool_fail:"); SHOW_TOT(thr, activity[thr].pool_fail);
1144 chunk_appendf(&trash, "buf_wait:"); SHOW_TOT(thr, activity[thr].buf_wait);
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001145 chunk_appendf(&trash, "empty_rq:"); SHOW_TOT(thr, activity[thr].empty_rq);
1146 chunk_appendf(&trash, "long_rq:"); SHOW_TOT(thr, activity[thr].long_rq);
1147 chunk_appendf(&trash, "ctxsw:"); SHOW_TOT(thr, activity[thr].ctxsw);
1148 chunk_appendf(&trash, "tasksw:"); SHOW_TOT(thr, activity[thr].tasksw);
1149 chunk_appendf(&trash, "cpust_ms_tot:"); SHOW_TOT(thr, activity[thr].cpust_total / 2);
1150 chunk_appendf(&trash, "cpust_ms_1s:"); SHOW_TOT(thr, read_freq_ctr(&activity[thr].cpust_1s) / 2);
1151 chunk_appendf(&trash, "cpust_ms_15s:"); SHOW_TOT(thr, read_freq_ctr_period(&activity[thr].cpust_15s, 15000) / 2);
1152 chunk_appendf(&trash, "avg_loop_us:"); SHOW_AVG(thr, swrate_avg(activity[thr].avg_loop_us, TIME_STATS_SAMPLES));
1153 chunk_appendf(&trash, "accepted:"); SHOW_TOT(thr, activity[thr].accepted);
1154 chunk_appendf(&trash, "accq_pushed:"); SHOW_TOT(thr, activity[thr].accq_pushed);
1155 chunk_appendf(&trash, "accq_full:"); SHOW_TOT(thr, activity[thr].accq_full);
Willy Tarreaue6182842019-04-15 18:54:10 +02001156#ifdef USE_THREAD
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001157 chunk_appendf(&trash, "accq_ring:"); SHOW_TOT(thr, (accept_queue_rings[thr].tail - accept_queue_rings[thr].head + ACCEPT_QUEUE_SIZE) % ACCEPT_QUEUE_SIZE);
Willy Tarreaue6182842019-04-15 18:54:10 +02001158#endif
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001159
Willy Tarreaud6a78502019-05-27 07:03:38 +02001160#if defined(DEBUG_DEV)
1161 /* keep these ones at the end */
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001162 chunk_appendf(&trash, "ctr0:"); SHOW_TOT(thr, activity[thr].ctr0);
1163 chunk_appendf(&trash, "ctr1:"); SHOW_TOT(thr, activity[thr].ctr1);
1164 chunk_appendf(&trash, "ctr2:"); SHOW_TOT(thr, activity[thr].ctr2);
Willy Tarreaud6a78502019-05-27 07:03:38 +02001165#endif
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001166
1167 if (ci_putchk(si_ic(si), &trash) == -1) {
1168 chunk_reset(&trash);
1169 chunk_printf(&trash, "[output too large, cannot dump]\n");
Willy Tarreaudb398432018-11-15 11:08:52 +01001170 si_rx_room_blk(si);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001171 }
1172
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001173#undef SHOW_AVG
1174#undef SHOW_TOT
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001175 /* dump complete */
1176 return 1;
1177}
1178
William Lallemandeceddf72016-12-15 18:06:44 +01001179/*
Willy Tarreau3af9d832016-12-16 12:58:09 +01001180 * CLI IO handler for `show cli sockets`.
1181 * Uses ctx.cli.p0 to store the restart pointer.
William Lallemandeceddf72016-12-15 18:06:44 +01001182 */
1183static int cli_io_handler_show_cli_sock(struct appctx *appctx)
1184{
1185 struct bind_conf *bind_conf;
1186 struct stream_interface *si = appctx->owner;
1187
1188 chunk_reset(&trash);
1189
1190 switch (appctx->st2) {
1191 case STAT_ST_INIT:
1192 chunk_printf(&trash, "# socket lvl processes\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001193 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001194 si_rx_room_blk(si);
William Lallemandeceddf72016-12-15 18:06:44 +01001195 return 0;
1196 }
William Lallemandeceddf72016-12-15 18:06:44 +01001197 appctx->st2 = STAT_ST_LIST;
1198
1199 case STAT_ST_LIST:
1200 if (global.stats_fe) {
1201 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
1202 struct listener *l;
1203
1204 /*
Willy Tarreau3af9d832016-12-16 12:58:09 +01001205 * get the latest dumped node in appctx->ctx.cli.p0
William Lallemandeceddf72016-12-15 18:06:44 +01001206 * if the current node is the first of the list
1207 */
1208
Willy Tarreau3af9d832016-12-16 12:58:09 +01001209 if (appctx->ctx.cli.p0 &&
1210 &bind_conf->by_fe == (&global.stats_fe->conf.bind)->n) {
William Lallemandeceddf72016-12-15 18:06:44 +01001211 /* change the current node to the latest dumped and continue the loop */
Willy Tarreau3af9d832016-12-16 12:58:09 +01001212 bind_conf = LIST_ELEM(appctx->ctx.cli.p0, typeof(bind_conf), by_fe);
William Lallemandeceddf72016-12-15 18:06:44 +01001213 continue;
1214 }
1215
1216 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
1217
1218 char addr[46];
1219 char port[6];
1220
1221 if (l->addr.ss_family == AF_UNIX) {
1222 const struct sockaddr_un *un;
1223
1224 un = (struct sockaddr_un *)&l->addr;
William Lallemande58915f2019-04-01 11:30:05 +02001225 if (un->sun_path[0] == '\0') {
William Lallemand75812a72019-04-01 11:30:04 +02001226 chunk_appendf(&trash, "abns@%s ", un->sun_path+1);
William Lallemande58915f2019-04-01 11:30:05 +02001227 } else {
1228 chunk_appendf(&trash, "unix@%s ", un->sun_path);
1229 }
William Lallemandeceddf72016-12-15 18:06:44 +01001230 } else if (l->addr.ss_family == AF_INET) {
1231 addr_to_str(&l->addr, addr, sizeof(addr));
1232 port_to_str(&l->addr, port, sizeof(port));
William Lallemande58915f2019-04-01 11:30:05 +02001233 chunk_appendf(&trash, "ipv4@%s:%s ", addr, port);
William Lallemandeceddf72016-12-15 18:06:44 +01001234 } else if (l->addr.ss_family == AF_INET6) {
1235 addr_to_str(&l->addr, addr, sizeof(addr));
1236 port_to_str(&l->addr, port, sizeof(port));
William Lallemande58915f2019-04-01 11:30:05 +02001237 chunk_appendf(&trash, "ipv6@[%s]:%s ", addr, port);
William Lallemand26314342018-10-26 14:47:41 +02001238 } else if (l->addr.ss_family == AF_CUST_SOCKPAIR) {
1239 chunk_appendf(&trash, "sockpair@%d ", ((struct sockaddr_in *)&l->addr)->sin_addr.s_addr);
William Lallemandeceddf72016-12-15 18:06:44 +01001240 } else
William Lallemand26314342018-10-26 14:47:41 +02001241 chunk_appendf(&trash, "unknown ");
William Lallemandeceddf72016-12-15 18:06:44 +01001242
William Lallemand07a62f72017-05-24 00:57:40 +02001243 if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN)
William Lallemandeceddf72016-12-15 18:06:44 +01001244 chunk_appendf(&trash, "admin ");
William Lallemand07a62f72017-05-24 00:57:40 +02001245 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER)
1246 chunk_appendf(&trash, "operator ");
1247 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_USER)
1248 chunk_appendf(&trash, "user ");
William Lallemandeceddf72016-12-15 18:06:44 +01001249 else
1250 chunk_appendf(&trash, " ");
1251
1252 if (bind_conf->bind_proc != 0) {
1253 int pos;
Willy Tarreau20c5e522016-12-16 12:50:55 +01001254 for (pos = 0; pos < 8 * sizeof(bind_conf->bind_proc); pos++) {
Willy Tarreau4305ac72016-12-16 12:56:31 +01001255 if (bind_conf->bind_proc & (1UL << pos)) {
William Lallemandeceddf72016-12-15 18:06:44 +01001256 chunk_appendf(&trash, "%d,", pos+1);
1257 }
1258 }
1259 /* replace the latest comma by a newline */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001260 trash.area[trash.data-1] = '\n';
William Lallemandeceddf72016-12-15 18:06:44 +01001261
1262 } else {
1263 chunk_appendf(&trash, "all\n");
1264 }
1265
Willy Tarreau06d80a92017-10-19 14:32:15 +02001266 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001267 si_rx_room_blk(si);
William Lallemandeceddf72016-12-15 18:06:44 +01001268 return 0;
1269 }
1270 }
Willy Tarreau3af9d832016-12-16 12:58:09 +01001271 appctx->ctx.cli.p0 = &bind_conf->by_fe; /* store the latest list node dumped */
William Lallemandeceddf72016-12-15 18:06:44 +01001272 }
1273 }
1274 default:
1275 appctx->st2 = STAT_ST_FIN;
1276 return 1;
1277 }
1278}
1279
1280
Willy Tarreau0a739292016-11-22 20:21:23 +01001281/* parse a "show env" CLI request. Returns 0 if it needs to continue, 1 if it
Willy Tarreauf6710f82016-12-16 17:45:44 +01001282 * wants to stop here. It puts the variable to be dumped into cli.p0 if a single
1283 * variable is requested otherwise puts environ there.
Willy Tarreau0a739292016-11-22 20:21:23 +01001284 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001285static int cli_parse_show_env(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0a739292016-11-22 20:21:23 +01001286{
1287 extern char **environ;
Willy Tarreauf6710f82016-12-16 17:45:44 +01001288 char **var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001289
1290 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1291 return 1;
1292
Willy Tarreauf6710f82016-12-16 17:45:44 +01001293 var = environ;
Willy Tarreau0a739292016-11-22 20:21:23 +01001294
1295 if (*args[2]) {
1296 int len = strlen(args[2]);
1297
Willy Tarreauf6710f82016-12-16 17:45:44 +01001298 for (; *var; var++) {
1299 if (strncmp(*var, args[2], len) == 0 &&
1300 (*var)[len] == '=')
Willy Tarreau0a739292016-11-22 20:21:23 +01001301 break;
1302 }
Willy Tarreau9d008692019-08-09 11:21:01 +02001303 if (!*var)
1304 return cli_err(appctx, "Variable not found\n");
1305
Willy Tarreau0a739292016-11-22 20:21:23 +01001306 appctx->st2 = STAT_ST_END;
1307 }
Willy Tarreauf6710f82016-12-16 17:45:44 +01001308 appctx->ctx.cli.p0 = var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001309 return 0;
1310}
1311
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001312/* parse a "show fd" CLI request. Returns 0 if it needs to continue, 1 if it
1313 * wants to stop here. It puts the FD number into cli.i0 if a specific FD is
1314 * requested and sets st2 to STAT_ST_END, otherwise leaves 0 in i0.
1315 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001316static int cli_parse_show_fd(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001317{
1318 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1319 return 1;
1320
1321 appctx->ctx.cli.i0 = 0;
1322
1323 if (*args[2]) {
1324 appctx->ctx.cli.i0 = atoi(args[2]);
1325 appctx->st2 = STAT_ST_END;
1326 }
1327 return 0;
1328}
1329
Willy Tarreau599852e2016-11-22 20:33:32 +01001330/* parse a "set timeout" CLI request. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001331static int cli_parse_set_timeout(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau599852e2016-11-22 20:33:32 +01001332{
1333 struct stream_interface *si = appctx->owner;
1334 struct stream *s = si_strm(si);
1335
1336 if (strcmp(args[2], "cli") == 0) {
1337 unsigned timeout;
1338 const char *res;
1339
Willy Tarreau9d008692019-08-09 11:21:01 +02001340 if (!*args[3])
1341 return cli_err(appctx, "Expects an integer value.\n");
Willy Tarreau599852e2016-11-22 20:33:32 +01001342
1343 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
Willy Tarreau9d008692019-08-09 11:21:01 +02001344 if (res || timeout < 1)
1345 return cli_err(appctx, "Invalid timeout value.\n");
Willy Tarreau599852e2016-11-22 20:33:32 +01001346
1347 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
1348 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
1349 return 1;
1350 }
Willy Tarreau9d008692019-08-09 11:21:01 +02001351
1352 return cli_err(appctx, "'set timeout' only supports 'cli'.\n");
Willy Tarreau599852e2016-11-22 20:33:32 +01001353}
1354
Willy Tarreau2af99412016-11-23 11:10:59 +01001355/* parse a "set maxconn global" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001356static int cli_parse_set_maxconn_global(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2af99412016-11-23 11:10:59 +01001357{
1358 int v;
1359
1360 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1361 return 1;
1362
Willy Tarreau9d008692019-08-09 11:21:01 +02001363 if (!*args[3])
1364 return cli_err(appctx, "Expects an integer value.\n");
Willy Tarreau2af99412016-11-23 11:10:59 +01001365
1366 v = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02001367 if (v > global.hardmaxconn)
1368 return cli_err(appctx, "Value out of range.\n");
Willy Tarreau2af99412016-11-23 11:10:59 +01001369
1370 /* check for unlimited values */
1371 if (v <= 0)
1372 v = global.hardmaxconn;
1373
1374 global.maxconn = v;
1375
1376 /* Dequeues all of the listeners waiting for a resource */
Willy Tarreau241797a2019-12-10 14:10:52 +01001377 dequeue_all_listeners();
Willy Tarreau2af99412016-11-23 11:10:59 +01001378
1379 return 1;
1380}
1381
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001382static int set_severity_output(int *target, char *argument)
1383{
1384 if (!strcmp(argument, "none")) {
1385 *target = CLI_SEVERITY_NONE;
1386 return 1;
1387 }
1388 else if (!strcmp(argument, "number")) {
1389 *target = CLI_SEVERITY_NUMBER;
1390 return 1;
1391 }
1392 else if (!strcmp(argument, "string")) {
1393 *target = CLI_SEVERITY_STRING;
1394 return 1;
1395 }
1396 return 0;
1397}
1398
1399/* parse a "set severity-output" command. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001400static int cli_parse_set_severity_output(char **args, char *payload, struct appctx *appctx, void *private)
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001401{
1402 if (*args[2] && set_severity_output(&appctx->cli_severity_output, args[2]))
1403 return 0;
1404
Willy Tarreau9d008692019-08-09 11:21:01 +02001405 return cli_err(appctx, "one of 'none', 'number', 'string' is a required argument\n");
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001406}
William Lallemandeceddf72016-12-15 18:06:44 +01001407
William Lallemand67a234f2018-12-13 09:05:45 +01001408
1409/* show the level of the current CLI session */
1410static int cli_parse_show_lvl(char **args, char *payload, struct appctx *appctx, void *private)
1411{
William Lallemand67a234f2018-12-13 09:05:45 +01001412 if ((appctx->cli_level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN)
Willy Tarreau9d008692019-08-09 11:21:01 +02001413 return cli_msg(appctx, LOG_INFO, "admin\n");
William Lallemand67a234f2018-12-13 09:05:45 +01001414 else if ((appctx->cli_level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER)
Willy Tarreau9d008692019-08-09 11:21:01 +02001415 return cli_msg(appctx, LOG_INFO, "operator\n");
William Lallemand67a234f2018-12-13 09:05:45 +01001416 else if ((appctx->cli_level & ACCESS_LVL_MASK) == ACCESS_LVL_USER)
Willy Tarreau9d008692019-08-09 11:21:01 +02001417 return cli_msg(appctx, LOG_INFO, "user\n");
William Lallemand67a234f2018-12-13 09:05:45 +01001418 else
Willy Tarreau9d008692019-08-09 11:21:01 +02001419 return cli_msg(appctx, LOG_INFO, "unknown\n");
William Lallemand67a234f2018-12-13 09:05:45 +01001420}
1421
1422/* parse and set the CLI level dynamically */
1423static int cli_parse_set_lvl(char **args, char *payload, struct appctx *appctx, void *private)
1424{
William Lallemandad032882019-07-01 10:56:15 +02001425 /* this will ask the applet to not output a \n after the command */
1426 if (!strcmp(args[1], "-"))
1427 appctx->st1 |= APPCTX_CLI_ST1_NOLF;
1428
William Lallemand67a234f2018-12-13 09:05:45 +01001429 if (!strcmp(args[0], "operator")) {
1430 if (!cli_has_level(appctx, ACCESS_LVL_OPER)) {
1431 return 1;
1432 }
1433 appctx->cli_level &= ~ACCESS_LVL_MASK;
1434 appctx->cli_level |= ACCESS_LVL_OPER;
1435
1436 } else if (!strcmp(args[0], "user")) {
1437 if (!cli_has_level(appctx, ACCESS_LVL_USER)) {
1438 return 1;
1439 }
1440 appctx->cli_level &= ~ACCESS_LVL_MASK;
1441 appctx->cli_level |= ACCESS_LVL_USER;
1442 }
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02001443 appctx->cli_level &= ~ACCESS_EXPERT;
1444 return 1;
1445}
1446
1447
1448/* parse and set the CLI expert-mode dynamically */
1449static int cli_parse_expert_mode(char **args, char *payload, struct appctx *appctx, void *private)
1450{
1451 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1452 return 1;
1453
1454 if (!*args[1])
1455 return (appctx->cli_level & ACCESS_EXPERT)
1456 ? cli_msg(appctx, LOG_INFO, "expert-mode is ON\n")
1457 : cli_msg(appctx, LOG_INFO, "expert-mode is OFF\n");
1458
1459 appctx->cli_level &= ~ACCESS_EXPERT;
1460 if (strcmp(args[1], "on") == 0)
1461 appctx->cli_level |= ACCESS_EXPERT;
William Lallemand67a234f2018-12-13 09:05:45 +01001462 return 1;
1463}
1464
William Lallemanda57b7e32018-12-14 21:11:31 +01001465
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001466int cli_parse_default(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandeceddf72016-12-15 18:06:44 +01001467{
1468 return 0;
1469}
1470
Willy Tarreau45c742b2016-11-24 14:51:17 +01001471/* parse a "set rate-limit" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001472static int cli_parse_set_ratelimit(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau45c742b2016-11-24 14:51:17 +01001473{
1474 int v;
1475 int *res;
1476 int mul = 1;
1477
1478 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1479 return 1;
1480
1481 if (strcmp(args[2], "connections") == 0 && strcmp(args[3], "global") == 0)
1482 res = &global.cps_lim;
1483 else if (strcmp(args[2], "sessions") == 0 && strcmp(args[3], "global") == 0)
1484 res = &global.sps_lim;
1485#ifdef USE_OPENSSL
1486 else if (strcmp(args[2], "ssl-sessions") == 0 && strcmp(args[3], "global") == 0)
1487 res = &global.ssl_lim;
1488#endif
1489 else if (strcmp(args[2], "http-compression") == 0 && strcmp(args[3], "global") == 0) {
1490 res = &global.comp_rate_lim;
1491 mul = 1024;
1492 }
1493 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02001494 return cli_err(appctx,
Willy Tarreau45c742b2016-11-24 14:51:17 +01001495 "'set rate-limit' only supports :\n"
1496 " - 'connections global' to set the per-process maximum connection rate\n"
1497 " - 'sessions global' to set the per-process maximum session rate\n"
1498#ifdef USE_OPENSSL
Aurélien Nephtalib53e2082018-03-11 16:55:02 +01001499 " - 'ssl-sessions global' to set the per-process maximum SSL session rate\n"
Willy Tarreau45c742b2016-11-24 14:51:17 +01001500#endif
Willy Tarreau9d008692019-08-09 11:21:01 +02001501 " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n");
Willy Tarreau45c742b2016-11-24 14:51:17 +01001502 }
1503
Willy Tarreau9d008692019-08-09 11:21:01 +02001504 if (!*args[4])
1505 return cli_err(appctx, "Expects an integer value.\n");
Willy Tarreau45c742b2016-11-24 14:51:17 +01001506
1507 v = atoi(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02001508 if (v < 0)
1509 return cli_err(appctx, "Value out of range.\n");
Willy Tarreau45c742b2016-11-24 14:51:17 +01001510
1511 *res = v * mul;
1512
1513 /* Dequeues all of the listeners waiting for a resource */
Willy Tarreau241797a2019-12-10 14:10:52 +01001514 dequeue_all_listeners();
Willy Tarreau45c742b2016-11-24 14:51:17 +01001515
1516 return 1;
1517}
1518
William Lallemandf6975e92017-05-26 17:42:10 +02001519/* parse the "expose-fd" argument on the bind lines */
1520static int bind_parse_expose_fd(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1521{
1522 if (!*args[cur_arg + 1]) {
1523 memprintf(err, "'%s' : missing fd type", args[cur_arg]);
1524 return ERR_ALERT | ERR_FATAL;
1525 }
1526 if (!strcmp(args[cur_arg+1], "listeners")) {
1527 conf->level |= ACCESS_FD_LISTENERS;
1528 } else {
1529 memprintf(err, "'%s' only supports 'listeners' (got '%s')",
1530 args[cur_arg], args[cur_arg+1]);
1531 return ERR_ALERT | ERR_FATAL;
1532 }
1533
1534 return 0;
1535}
1536
William Lallemand74c24fb2016-11-21 17:18:36 +01001537/* parse the "level" argument on the bind lines */
1538static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1539{
1540 if (!*args[cur_arg + 1]) {
1541 memprintf(err, "'%s' : missing level", args[cur_arg]);
1542 return ERR_ALERT | ERR_FATAL;
1543 }
1544
William Lallemand07a62f72017-05-24 00:57:40 +02001545 if (!strcmp(args[cur_arg+1], "user")) {
1546 conf->level &= ~ACCESS_LVL_MASK;
1547 conf->level |= ACCESS_LVL_USER;
1548 } else if (!strcmp(args[cur_arg+1], "operator")) {
1549 conf->level &= ~ACCESS_LVL_MASK;
1550 conf->level |= ACCESS_LVL_OPER;
1551 } else if (!strcmp(args[cur_arg+1], "admin")) {
1552 conf->level &= ~ACCESS_LVL_MASK;
1553 conf->level |= ACCESS_LVL_ADMIN;
1554 } else {
William Lallemand74c24fb2016-11-21 17:18:36 +01001555 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
1556 args[cur_arg], args[cur_arg+1]);
1557 return ERR_ALERT | ERR_FATAL;
1558 }
1559
1560 return 0;
1561}
1562
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001563static int bind_parse_severity_output(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1564{
1565 if (!*args[cur_arg + 1]) {
1566 memprintf(err, "'%s' : missing severity format", args[cur_arg]);
1567 return ERR_ALERT | ERR_FATAL;
1568 }
1569
1570 if (set_severity_output(&conf->severity_output, args[cur_arg+1]))
1571 return 0;
1572 else {
1573 memprintf(err, "'%s' only supports 'none', 'number', and 'string' (got '%s')",
1574 args[cur_arg], args[cur_arg+1]);
1575 return ERR_ALERT | ERR_FATAL;
1576 }
1577}
1578
William Lallemandd308c5e2020-01-16 16:26:41 +01001579/*
1580 * For one proxy, fill the iov and send the msghdr. Also update fd_it and offset.
1581 * Return -1 upon error, otherwise 0.
1582 *
1583 * This function is only meant to deduplicate the code between the peers and
1584 * the proxy list in _getsocks(), not to be used anywhere else.
1585 */
1586inline static int _getsocks_gen_send(struct proxy *px, int sendfd, int *tmpfd, struct iovec *iov,
1587 int tot_fd_nb, int *fd_it, int *offset, struct msghdr *msghdr)
1588{
1589 int i = *fd_it;
1590 int curoff = *offset;
1591 struct listener *l;
1592 unsigned char *tmpbuf = iov->iov_base;
1593
1594 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1595 int ret;
1596 /* Only transfer IPv4/IPv6 sockets */
1597 if (l->state >= LI_ZOMBIE &&
1598 (l->proto->sock_family == AF_INET ||
1599 l->proto->sock_family == AF_INET6 ||
1600 l->proto->sock_family == AF_UNIX)) {
1601 memcpy(&tmpfd[i % MAX_SEND_FD], &l->fd, sizeof(l->fd));
1602 if (!l->netns)
1603 tmpbuf[curoff++] = 0;
1604#ifdef USE_NS
1605 else {
1606 char *name = l->netns->node.key;
1607 unsigned char len = l->netns->name_len;
1608 tmpbuf[curoff++] = len;
1609 memcpy(tmpbuf + curoff, name, len);
1610 curoff += len;
1611 }
1612#endif
1613 if (l->interface) {
1614 unsigned char len = strlen(l->interface);
1615 tmpbuf[curoff++] = len;
1616 memcpy(tmpbuf + curoff, l->interface, len);
1617 curoff += len;
1618 } else
1619 tmpbuf[curoff++] = 0;
1620 memcpy(tmpbuf + curoff, &l->options,
1621 sizeof(l->options));
1622 curoff += sizeof(l->options);
1623
1624 i++;
1625 } else
1626 continue;
1627 /* if it reaches the max number of fd per msghdr */
1628 if ((!(i % MAX_SEND_FD))) {
1629 iov->iov_len = curoff;
1630 if (sendmsg(sendfd, msghdr, 0) != curoff) {
1631 ha_warning("Failed to transfer sockets\n");
1632 return -1;
1633 }
1634 /* Wait for an ack */
1635 do {
1636 ret = recv(sendfd, &tot_fd_nb,
1637 sizeof(tot_fd_nb), 0);
1638 } while (ret == -1 && errno == EINTR);
1639 if (ret <= 0) {
1640 ha_warning("Unexpected error while transferring sockets\n");
1641 return -1;
1642 }
1643 curoff = 0;
1644 }
1645 }
1646
1647 *fd_it = i;
1648 *offset = curoff;
1649
1650 return 0;
1651}
William Lallemand16dd1b32018-11-19 18:46:18 +01001652
William Lallemandb9f9e3b2018-10-26 14:47:39 +02001653
Olivier Houchardf886e342017-04-05 22:24:59 +02001654/* Send all the bound sockets, always returns 1 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001655static int _getsocks(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchardf886e342017-04-05 22:24:59 +02001656{
1657 char *cmsgbuf = NULL;
1658 unsigned char *tmpbuf = NULL;
1659 struct cmsghdr *cmsg;
1660 struct stream_interface *si = appctx->owner;
William Lallemandf6975e92017-05-26 17:42:10 +02001661 struct stream *s = si_strm(si);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001662 struct connection *remote = cs_conn(objt_cs(si_opposite(si)->end));
Olivier Houchardf886e342017-04-05 22:24:59 +02001663 struct msghdr msghdr;
1664 struct iovec iov;
Olivier Houchard54740872017-04-06 14:45:14 +02001665 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf886e342017-04-05 22:24:59 +02001666 int *tmpfd;
1667 int tot_fd_nb = 0;
1668 struct proxy *px;
William Lallemand5fd3b282020-01-16 15:32:08 +01001669 struct peers *prs;
Olivier Houchardf886e342017-04-05 22:24:59 +02001670 int i = 0;
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001671 int fd = -1;
Olivier Houchardf886e342017-04-05 22:24:59 +02001672 int curoff = 0;
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001673 int old_fcntl = -1;
Olivier Houchardf886e342017-04-05 22:24:59 +02001674 int ret;
1675
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001676 if (!remote) {
1677 ha_warning("Only works on real connections\n");
1678 goto out;
1679 }
1680
1681 fd = remote->handle.fd;
1682
Olivier Houchardf886e342017-04-05 22:24:59 +02001683 /* Temporary set the FD in blocking mode, that will make our life easier */
1684 old_fcntl = fcntl(fd, F_GETFL);
1685 if (old_fcntl < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001686 ha_warning("Couldn't get the flags for the unix socket\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001687 goto out;
1688 }
1689 cmsgbuf = malloc(CMSG_SPACE(sizeof(int) * MAX_SEND_FD));
1690 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001691 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001692 goto out;
1693 }
1694 if (fcntl(fd, F_SETFL, old_fcntl &~ O_NONBLOCK) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001695 ha_warning("Cannot make the unix socket blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001696 goto out;
1697 }
Olivier Houchard54740872017-04-06 14:45:14 +02001698 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf886e342017-04-05 22:24:59 +02001699 iov.iov_base = &tot_fd_nb;
1700 iov.iov_len = sizeof(tot_fd_nb);
William Lallemandf6975e92017-05-26 17:42:10 +02001701 if (!(strm_li(s)->bind_conf->level & ACCESS_FD_LISTENERS))
Olivier Houchardf886e342017-04-05 22:24:59 +02001702 goto out;
1703 memset(&msghdr, 0, sizeof(msghdr));
1704 /*
1705 * First, calculates the total number of FD, so that we can let
1706 * the caller know how much he should expects.
1707 */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001708 px = proxies_list;
Olivier Houchardf886e342017-04-05 22:24:59 +02001709 while (px) {
1710 struct listener *l;
1711
1712 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02001713 /* Only transfer IPv4/IPv6/UNIX sockets */
1714 if (l->state >= LI_ZOMBIE &&
1715 (l->proto->sock_family == AF_INET ||
Olivier Houchardf886e342017-04-05 22:24:59 +02001716 l->proto->sock_family == AF_INET6 ||
Olivier Houchard1fc05162017-04-06 01:05:05 +02001717 l->proto->sock_family == AF_UNIX))
Olivier Houchardf886e342017-04-05 22:24:59 +02001718 tot_fd_nb++;
1719 }
1720 px = px->next;
1721 }
William Lallemand5fd3b282020-01-16 15:32:08 +01001722 prs = cfg_peers;
1723 while (prs) {
1724 if (prs->peers_fe) {
1725 struct listener *l;
1726
1727 list_for_each_entry(l, &prs->peers_fe->conf.listeners, by_fe) {
1728 /* Only transfer IPv4/IPv6/UNIX sockets */
1729 if (l->state >= LI_ZOMBIE &&
1730 (l->proto->sock_family == AF_INET ||
1731 l->proto->sock_family == AF_INET6 ||
1732 l->proto->sock_family == AF_UNIX))
1733 tot_fd_nb++;
1734 }
1735 }
1736 prs = prs->next;
1737 }
Olivier Houchardf886e342017-04-05 22:24:59 +02001738 if (tot_fd_nb == 0)
1739 goto out;
1740
1741 /* First send the total number of file descriptors, so that the
1742 * receiving end knows what to expect.
1743 */
1744 msghdr.msg_iov = &iov;
1745 msghdr.msg_iovlen = 1;
1746 ret = sendmsg(fd, &msghdr, 0);
1747 if (ret != sizeof(tot_fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001748 ha_warning("Failed to send the number of sockets to send\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001749 goto out;
1750 }
1751
1752 /* Now send the fds */
1753 msghdr.msg_control = cmsgbuf;
1754 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * MAX_SEND_FD);
1755 cmsg = CMSG_FIRSTHDR(&msghdr);
1756 cmsg->cmsg_len = CMSG_LEN(MAX_SEND_FD * sizeof(int));
1757 cmsg->cmsg_level = SOL_SOCKET;
1758 cmsg->cmsg_type = SCM_RIGHTS;
1759 tmpfd = (int *)CMSG_DATA(cmsg);
1760
Olivier Houchardf886e342017-04-05 22:24:59 +02001761 /* For each socket, e message is sent, containing the following :
1762 * Size of the namespace name (or 0 if none), as an unsigned char.
1763 * The namespace name, if any
1764 * Size of the interface name (or 0 if none), as an unsigned char
1765 * The interface name, if any
1766 * Listener options, as an int.
1767 */
1768 /* We will send sockets MAX_SEND_FD per MAX_SEND_FD, allocate a
Ilya Shipitsind4259502020-04-08 01:07:56 +05001769 * buffer big enough to store the socket information.
Olivier Houchardf886e342017-04-05 22:24:59 +02001770 */
Olivier Houchardf143b802017-11-04 15:13:01 +01001771 tmpbuf = malloc(MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf886e342017-04-05 22:24:59 +02001772 if (tmpbuf == NULL) {
Ilya Shipitsind4259502020-04-08 01:07:56 +05001773 ha_warning("Failed to allocate memory to transfer socket information\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001774 goto out;
1775 }
1776 iov.iov_base = tmpbuf;
William Lallemand5fd3b282020-01-16 15:32:08 +01001777 px = proxies_list;
Olivier Houchardf886e342017-04-05 22:24:59 +02001778 while (px) {
William Lallemandd308c5e2020-01-16 16:26:41 +01001779 if (_getsocks_gen_send(px, fd, tmpfd, &iov,
1780 tot_fd_nb, &i, &curoff, &msghdr) < 0)
1781 goto out;
Olivier Houchardf886e342017-04-05 22:24:59 +02001782 px = px->next;
1783 }
William Lallemand5fd3b282020-01-16 15:32:08 +01001784 /* should be done for peers too */
1785 prs = cfg_peers;
1786 while (prs) {
William Lallemandd308c5e2020-01-16 16:26:41 +01001787 if (prs->peers_fe)
1788 if (_getsocks_gen_send(prs->peers_fe, fd, tmpfd, &iov,
1789 tot_fd_nb, &i, &curoff, &msghdr) < 0)
1790 goto out;
William Lallemand5fd3b282020-01-16 15:32:08 +01001791 prs = prs->next;
1792 }
1793
Olivier Houchardf886e342017-04-05 22:24:59 +02001794 if (i % MAX_SEND_FD) {
1795 iov.iov_len = curoff;
1796 cmsg->cmsg_len = CMSG_LEN((i % MAX_SEND_FD) * sizeof(int));
1797 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * (i % MAX_SEND_FD));
1798 if (sendmsg(fd, &msghdr, 0) != curoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001799 ha_warning("Failed to transfer sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001800 goto out;
1801 }
1802 }
1803
1804out:
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001805 if (fd >= 0 && old_fcntl >= 0 && fcntl(fd, F_SETFL, old_fcntl) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001806 ha_warning("Cannot make the unix socket non-blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001807 goto out;
1808 }
1809 appctx->st0 = CLI_ST_END;
1810 free(cmsgbuf);
1811 free(tmpbuf);
1812 return 1;
1813}
1814
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001815static int cli_parse_simple(char **args, char *payload, struct appctx *appctx, void *private)
1816{
1817 if (*args[0] == 'h')
1818 /* help */
1819 cli_gen_usage_msg(appctx);
1820 else if (*args[0] == 'p')
1821 /* prompt */
1822 appctx->st1 ^= APPCTX_CLI_ST1_PROMPT;
1823 else if (*args[0] == 'q')
1824 /* quit */
1825 appctx->st0 = CLI_ST_END;
1826
1827 return 1;
1828}
Olivier Houchardf886e342017-04-05 22:24:59 +02001829
William Lallemand2f4ce202018-10-26 14:47:47 +02001830void pcli_write_prompt(struct stream *s)
1831{
1832 struct buffer *msg = get_trash_chunk();
1833 struct channel *oc = si_oc(&s->si[0]);
1834
William Lallemanddc12c2e2018-12-13 09:05:46 +01001835 if (!(s->pcli_flags & PCLI_F_PROMPT))
William Lallemand5b80fa22018-12-11 16:10:54 +01001836 return;
1837
William Lallemanddc12c2e2018-12-13 09:05:46 +01001838 if (s->pcli_flags & PCLI_F_PAYLOAD) {
William Lallemandebf61802018-12-11 16:10:57 +01001839 chunk_appendf(msg, "+ ");
1840 } else {
1841 if (s->pcli_next_pid == 0)
Willy Tarreau52880f92018-12-15 13:30:03 +01001842 chunk_appendf(msg, "master%s> ",
1843 (global.mode & MODE_MWORKER_WAIT) ? "[ReloadFailed]" : "");
William Lallemandebf61802018-12-11 16:10:57 +01001844 else
1845 chunk_appendf(msg, "%d> ", s->pcli_next_pid);
1846 }
William Lallemand2f4ce202018-10-26 14:47:47 +02001847 co_inject(oc, msg->area, msg->data);
1848}
1849
William Lallemand291810d2018-10-26 14:47:38 +02001850
William Lallemandcf62f7e2018-10-26 14:47:40 +02001851/* The pcli_* functions are used for the CLI proxy in the master */
1852
William Lallemanddeeaa592018-10-26 14:47:48 +02001853void pcli_reply_and_close(struct stream *s, const char *msg)
1854{
1855 struct buffer *buf = get_trash_chunk();
1856
1857 chunk_initstr(buf, msg);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001858 si_retnclose(&s->si[0], buf);
William Lallemanddeeaa592018-10-26 14:47:48 +02001859}
1860
William Lallemand291810d2018-10-26 14:47:38 +02001861static enum obj_type *pcli_pid_to_server(int proc_pid)
1862{
1863 struct mworker_proc *child;
1864
William Lallemandbddd33a2018-12-11 16:10:53 +01001865 /* return the CLI applet of the master */
1866 if (proc_pid == 0)
1867 return &cli_applet.obj_type;
1868
William Lallemand291810d2018-10-26 14:47:38 +02001869 list_for_each_entry(child, &proc_list, list) {
1870 if (child->pid == proc_pid){
1871 return &child->srv->obj_type;
1872 }
1873 }
1874 return NULL;
1875}
1876
1877/* Take a CLI prefix in argument (eg: @!1234 @master @1)
1878 * Return:
1879 * 0: master
1880 * > 0: pid of a worker
1881 * < 0: didn't find a worker
1882 */
1883static int pcli_prefix_to_pid(const char *prefix)
1884{
1885 int proc_pid;
1886 struct mworker_proc *child;
1887 char *errtol = NULL;
1888
1889 if (*prefix != '@') /* not a prefix, should not happen */
1890 return -1;
1891
1892 prefix++;
1893 if (!*prefix) /* sent @ alone, return the master */
1894 return 0;
1895
1896 if (strcmp("master", prefix) == 0) {
1897 return 0;
1898 } else if (*prefix == '!') {
1899 prefix++;
1900 if (!*prefix)
1901 return -1;
1902
1903 proc_pid = strtol(prefix, &errtol, 10);
1904 if (*errtol != '\0')
1905 return -1;
1906 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +02001907 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand16dd1b32018-11-19 18:46:18 +01001908 continue;
William Lallemand291810d2018-10-26 14:47:38 +02001909 if (child->pid == proc_pid){
1910 return child->pid;
1911 }
1912 }
1913 } else {
1914 struct mworker_proc *chosen = NULL;
1915 /* this is a relative pid */
1916
1917 proc_pid = strtol(prefix, &errtol, 10);
1918 if (*errtol != '\0')
1919 return -1;
1920
1921 if (proc_pid == 0) /* return the master */
1922 return 0;
1923
1924 /* chose the right process, the current one is the one with the
1925 least number of reloads */
1926 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +02001927 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand16dd1b32018-11-19 18:46:18 +01001928 continue;
William Lallemand291810d2018-10-26 14:47:38 +02001929 if (child->relative_pid == proc_pid){
1930 if (child->reloads == 0)
1931 return child->pid;
1932 else if (chosen == NULL || child->reloads < chosen->reloads)
1933 chosen = child;
1934 }
1935 }
1936 if (chosen)
1937 return chosen->pid;
1938 }
1939 return -1;
1940}
1941
William Lallemandbddd33a2018-12-11 16:10:53 +01001942/* Return::
1943 * >= 0 : number of words to escape
1944 * = -1 : error
1945 */
1946
1947int pcli_find_and_exec_kw(struct stream *s, char **args, int argl, char **errmsg, int *next_pid)
1948{
1949 if (argl < 1)
1950 return 0;
1951
1952 /* there is a prefix */
1953 if (args[0][0] == '@') {
1954 int target_pid = pcli_prefix_to_pid(args[0]);
1955
1956 if (target_pid == -1) {
1957 memprintf(errmsg, "Can't find the target PID matching the prefix '%s'\n", args[0]);
1958 return -1;
1959 }
1960
1961 /* if the prefix is alone, define a default target */
1962 if (argl == 1)
1963 s->pcli_next_pid = target_pid;
1964 else
1965 *next_pid = target_pid;
1966 return 1;
William Lallemand5b80fa22018-12-11 16:10:54 +01001967 } else if (!strcmp("prompt", args[0])) {
William Lallemanddc12c2e2018-12-13 09:05:46 +01001968 s->pcli_flags ^= PCLI_F_PROMPT;
William Lallemand5b80fa22018-12-11 16:10:54 +01001969 return argl; /* return the number of elements in the array */
William Lallemand5f610682018-12-11 16:10:55 +01001970
1971 } else if (!strcmp("quit", args[0])) {
1972 channel_shutr_now(&s->req);
1973 channel_shutw_now(&s->res);
1974 return argl; /* return the number of elements in the array */
William Lallemandb7ea1412018-12-13 09:05:47 +01001975 } else if (!strcmp(args[0], "operator")) {
1976 if (!pcli_has_level(s, ACCESS_LVL_OPER)) {
1977 memprintf(errmsg, "Permission denied!\n");
1978 return -1;
1979 }
1980 s->pcli_flags &= ~ACCESS_LVL_MASK;
1981 s->pcli_flags |= ACCESS_LVL_OPER;
1982 return argl;
1983
1984 } else if (!strcmp(args[0], "user")) {
1985 if (!pcli_has_level(s, ACCESS_LVL_USER)) {
1986 memprintf(errmsg, "Permission denied!\n");
1987 return -1;
1988 }
1989 s->pcli_flags &= ~ACCESS_LVL_MASK;
1990 s->pcli_flags |= ACCESS_LVL_USER;
1991 return argl;
William Lallemandbddd33a2018-12-11 16:10:53 +01001992 }
1993
1994 return 0;
1995}
1996
1997/*
1998 * Parse the CLI request:
1999 * - It does basically the same as the cli_io_handler, but as a proxy
2000 * - It can exec a command and strip non forwardable commands
William Lallemandcf62f7e2018-10-26 14:47:40 +02002001 *
2002 * Return:
William Lallemandbddd33a2018-12-11 16:10:53 +01002003 * - the number of characters to forward or
2004 * - 1 if there is an error or not enough data
William Lallemandcf62f7e2018-10-26 14:47:40 +02002005 */
William Lallemandbddd33a2018-12-11 16:10:53 +01002006int pcli_parse_request(struct stream *s, struct channel *req, char **errmsg, int *next_pid)
William Lallemandcf62f7e2018-10-26 14:47:40 +02002007{
William Lallemandbddd33a2018-12-11 16:10:53 +01002008 char *str = (char *)ci_head(req);
2009 char *end = (char *)ci_stop(req);
2010 char *args[MAX_STATS_ARGS + 1]; /* +1 for storing a NULL */
2011 int argl; /* number of args */
2012 char *p;
2013 char *trim = NULL;
William Lallemandebf61802018-12-11 16:10:57 +01002014 char *payload = NULL;
William Lallemandbddd33a2018-12-11 16:10:53 +01002015 int wtrim = 0; /* number of words to trim */
2016 int reql = 0;
William Lallemandb7ea1412018-12-13 09:05:47 +01002017 int ret;
William Lallemandbddd33a2018-12-11 16:10:53 +01002018 int i = 0;
2019
2020 p = str;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002021
William Lallemanddc12c2e2018-12-13 09:05:46 +01002022 if (!(s->pcli_flags & PCLI_F_PAYLOAD)) {
William Lallemandebf61802018-12-11 16:10:57 +01002023
2024 /* Looks for the end of one command */
2025 while (p+reql < end) {
2026 /* handle escaping */
2027 if (p[reql] == '\\') {
2028 reql++;
2029 continue;
2030 }
2031 if (p[reql] == ';' || p[reql] == '\n') {
2032 /* found the end of the command */
2033 p[reql] = '\n';
2034 reql++;
2035 break;
2036 }
William Lallemandbddd33a2018-12-11 16:10:53 +01002037 reql++;
William Lallemandbddd33a2018-12-11 16:10:53 +01002038 }
William Lallemandebf61802018-12-11 16:10:57 +01002039 } else {
2040 while (p+reql < end) {
2041 if (p[reql] == '\n') {
2042 /* found the end of the line */
2043 reql++;
2044 break;
2045 }
William Lallemandbddd33a2018-12-11 16:10:53 +01002046 reql++;
William Lallemandbddd33a2018-12-11 16:10:53 +01002047 }
William Lallemandbddd33a2018-12-11 16:10:53 +01002048 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002049
William Lallemandbddd33a2018-12-11 16:10:53 +01002050 /* set end to first byte after the end of the command */
2051 end = p + reql;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002052
William Lallemandbddd33a2018-12-11 16:10:53 +01002053 /* there is no end to this command, need more to parse ! */
2054 if (*(end-1) != '\n') {
2055 return -1;
2056 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002057
William Lallemand3301f3e2018-12-13 09:05:48 +01002058 if (s->pcli_flags & PCLI_F_PAYLOAD) {
2059 if (reql == 1) /* last line of the payload */
2060 s->pcli_flags &= ~PCLI_F_PAYLOAD;
William Lallemandebf61802018-12-11 16:10:57 +01002061 return reql;
2062 }
2063
William Lallemandbddd33a2018-12-11 16:10:53 +01002064 *(end-1) = '\0';
William Lallemandcf62f7e2018-10-26 14:47:40 +02002065
William Lallemandbddd33a2018-12-11 16:10:53 +01002066 /* splits the command in words */
2067 while (i < MAX_STATS_ARGS && p < end) {
2068 int j, k;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002069
William Lallemandbddd33a2018-12-11 16:10:53 +01002070 /* skip leading spaces/tabs */
2071 p += strspn(p, " \t");
2072 if (!*p)
William Lallemandcf62f7e2018-10-26 14:47:40 +02002073 break;
2074
William Lallemandbddd33a2018-12-11 16:10:53 +01002075 args[i] = p;
2076 p += strcspn(p, " \t");
2077 *p++ = 0;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002078
William Lallemandbddd33a2018-12-11 16:10:53 +01002079 /* unescape backslashes (\) */
2080 for (j = 0, k = 0; args[i][k]; k++) {
2081 if (args[i][k] == '\\') {
2082 if (args[i][k + 1] == '\\')
2083 k++;
2084 else
2085 continue;
2086 }
2087 args[i][j] = args[i][k];
2088 j++;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002089 }
William Lallemandbddd33a2018-12-11 16:10:53 +01002090 args[i][j] = 0;
2091 i++;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002092 }
2093
William Lallemandbddd33a2018-12-11 16:10:53 +01002094 argl = i;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002095
William Lallemandbddd33a2018-12-11 16:10:53 +01002096 for (; i < MAX_STATS_ARGS + 1; i++)
2097 args[i] = NULL;
2098
2099 wtrim = pcli_find_and_exec_kw(s, args, argl, errmsg, next_pid);
2100
2101 /* End of words are ending by \0, we need to replace the \0s by spaces
21021 before forwarding them */
2103 p = str;
William Lallemand3301f3e2018-12-13 09:05:48 +01002104 while (p < end-1) {
William Lallemandbddd33a2018-12-11 16:10:53 +01002105 if (*p == '\0')
2106 *p = ' ';
2107 p++;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002108 }
2109
William Lallemand3301f3e2018-12-13 09:05:48 +01002110 payload = strstr(str, PAYLOAD_PATTERN);
2111 if ((end - 1) == (payload + strlen(PAYLOAD_PATTERN))) {
2112 /* if the payload pattern is at the end */
2113 s->pcli_flags |= PCLI_F_PAYLOAD;
2114 ret = reql;
2115 }
2116
William Lallemandbddd33a2018-12-11 16:10:53 +01002117 *(end-1) = '\n';
William Lallemandcf62f7e2018-10-26 14:47:40 +02002118
William Lallemandbddd33a2018-12-11 16:10:53 +01002119 if (wtrim > 0) {
2120 trim = &args[wtrim][0];
2121 if (trim == NULL) /* if this was the last word in the table */
2122 trim = end;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002123
William Lallemandbddd33a2018-12-11 16:10:53 +01002124 b_del(&req->buf, trim - str);
2125
William Lallemandb7ea1412018-12-13 09:05:47 +01002126 ret = end - trim;
William Lallemandbddd33a2018-12-11 16:10:53 +01002127 } else if (wtrim < 0) {
2128 /* parsing error */
2129 return -1;
William Lallemandb7ea1412018-12-13 09:05:47 +01002130 } else {
2131 /* the whole string */
2132 ret = end - str;
William Lallemandbddd33a2018-12-11 16:10:53 +01002133 }
2134
William Lallemandb7ea1412018-12-13 09:05:47 +01002135 if (ret > 1) {
2136 if (pcli_has_level(s, ACCESS_LVL_ADMIN)) {
2137 goto end;
2138 } else if (pcli_has_level(s, ACCESS_LVL_OPER)) {
William Lallemandad032882019-07-01 10:56:15 +02002139 ci_insert_line2(req, 0, "operator -", strlen("operator -"));
2140 ret += strlen("operator -") + 2;
William Lallemandb7ea1412018-12-13 09:05:47 +01002141 } else if (pcli_has_level(s, ACCESS_LVL_USER)) {
William Lallemandad032882019-07-01 10:56:15 +02002142 ci_insert_line2(req, 0, "user -", strlen("user -"));
2143 ret += strlen("user -") + 2;
William Lallemandb7ea1412018-12-13 09:05:47 +01002144 }
2145 }
2146end:
William Lallemandbddd33a2018-12-11 16:10:53 +01002147
William Lallemandb7ea1412018-12-13 09:05:47 +01002148 return ret;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002149}
2150
2151int pcli_wait_for_request(struct stream *s, struct channel *req, int an_bit)
2152{
William Lallemandbddd33a2018-12-11 16:10:53 +01002153 int next_pid = -1;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002154 int to_forward;
William Lallemandbddd33a2018-12-11 16:10:53 +01002155 char *errmsg = NULL;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002156
William Lallemandb7ea1412018-12-13 09:05:47 +01002157 if ((s->pcli_flags & ACCESS_LVL_MASK) == ACCESS_LVL_NONE)
2158 s->pcli_flags |= strm_li(s)->bind_conf->level & ACCESS_LVL_MASK;
2159
William Lallemandcf62f7e2018-10-26 14:47:40 +02002160read_again:
2161 /* if the channel is closed for read, we won't receive any more data
2162 from the client, but we don't want to forward this close to the
2163 server */
2164 channel_dont_close(req);
2165
2166 /* We don't know yet to which server we will connect */
2167 channel_dont_connect(req);
2168
2169
2170 /* we are not waiting for a response, there is no more request and we
2171 * receive a close from the client, we can leave */
2172 if (!(ci_data(req)) && req->flags & CF_SHUTR) {
2173 channel_shutw_now(&s->res);
2174 s->req.analysers &= ~AN_REQ_WAIT_CLI;
2175 return 1;
2176 }
2177
2178 req->flags |= CF_READ_DONTWAIT;
2179
2180 /* need more data */
2181 if (!ci_data(req))
2182 return 0;
2183
2184 /* If there is data available for analysis, log the end of the idle time. */
2185 if (c_data(req) && s->logs.t_idle == -1)
2186 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2187
William Lallemandbddd33a2018-12-11 16:10:53 +01002188 to_forward = pcli_parse_request(s, req, &errmsg, &next_pid);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002189 if (to_forward > 0) {
William Lallemandbddd33a2018-12-11 16:10:53 +01002190 int target_pid;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002191 /* enough data */
2192
William Lallemandcf62f7e2018-10-26 14:47:40 +02002193 /* forward only 1 command */
2194 channel_forward(req, to_forward);
William Lallemandebf61802018-12-11 16:10:57 +01002195
William Lallemanddc12c2e2018-12-13 09:05:46 +01002196 if (!(s->pcli_flags & PCLI_F_PAYLOAD)) {
William Lallemandebf61802018-12-11 16:10:57 +01002197 /* we send only 1 command per request, and we write close after it */
2198 channel_shutw_now(req);
2199 } else {
2200 pcli_write_prompt(s);
2201 }
2202
2203 s->res.flags |= CF_WAKE_ONCE; /* need to be called again */
William Lallemandcf62f7e2018-10-26 14:47:40 +02002204
2205 /* remove the XFER_DATA analysers, which forwards all
2206 * the data, we don't want to forward the next requests
2207 * We need to add CF_FLT_ANALYZE to abort the forward too.
2208 */
2209 req->analysers &= ~(AN_REQ_FLT_XFER_DATA|AN_REQ_WAIT_CLI);
2210 req->analysers |= AN_REQ_FLT_END|CF_FLT_ANALYZE;
2211 s->res.analysers |= AN_RES_WAIT_CLI;
2212
William Lallemandebf61802018-12-11 16:10:57 +01002213 if (!(s->flags & SF_ASSIGNED)) {
2214 if (next_pid > -1)
2215 target_pid = next_pid;
2216 else
2217 target_pid = s->pcli_next_pid;
2218 /* we can connect now */
2219 s->target = pcli_pid_to_server(target_pid);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002220
William Lallemandebf61802018-12-11 16:10:57 +01002221 s->flags |= (SF_DIRECT | SF_ASSIGNED);
2222 channel_auto_connect(req);
2223 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002224
2225 } else if (to_forward == 0) {
William Lallemandcf62f7e2018-10-26 14:47:40 +02002226 /* we trimmed things but we might have other commands to consume */
William Lallemandbddd33a2018-12-11 16:10:53 +01002227 pcli_write_prompt(s);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002228 goto read_again;
William Lallemandbddd33a2018-12-11 16:10:53 +01002229 } else if (to_forward == -1 && errmsg) {
2230 /* there was an error during the parsing */
2231 pcli_reply_and_close(s, errmsg);
2232 return 0;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002233 } else if (to_forward == -1 && channel_full(req, global.tune.maxrewrite)) {
2234 /* buffer is full and we didn't catch the end of a command */
2235 goto send_help;
2236 }
2237
2238 return 0;
2239
2240send_help:
2241 b_reset(&req->buf);
2242 b_putblk(&req->buf, "help\n", 5);
2243 goto read_again;
2244}
2245
2246int pcli_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
2247{
2248 struct proxy *fe = strm_fe(s);
2249 struct proxy *be = s->be;
2250
William Lallemand6b7cd0a2018-11-06 17:37:11 +01002251 if (rep->flags & CF_READ_ERROR) {
2252 pcli_reply_and_close(s, "Can't connect to the target CLI!\n");
2253 s->res.analysers &= ~AN_RES_WAIT_CLI;
2254 return 0;
2255 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002256 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2257 rep->flags |= CF_NEVER_WAIT;
2258
2259 /* don't forward the close */
2260 channel_dont_close(&s->res);
2261 channel_dont_close(&s->req);
2262
William Lallemanddc12c2e2018-12-13 09:05:46 +01002263 if (s->pcli_flags & PCLI_F_PAYLOAD) {
William Lallemandebf61802018-12-11 16:10:57 +01002264 s->req.analysers |= AN_REQ_WAIT_CLI;
2265 s->res.analysers &= ~AN_RES_WAIT_CLI;
2266 s->req.flags |= CF_WAKE_ONCE; /* need to be called again if there is some command left in the request */
2267 return 0;
2268 }
2269
William Lallemandcf62f7e2018-10-26 14:47:40 +02002270 /* forward the data */
2271 if (ci_data(rep)) {
2272 c_adv(rep, ci_data(rep));
2273 return 0;
2274 }
2275
2276 if ((rep->flags & (CF_SHUTR|CF_READ_NULL))) {
2277 /* stream cleanup */
2278
William Lallemand2f4ce202018-10-26 14:47:47 +02002279 pcli_write_prompt(s);
2280
William Lallemandcf62f7e2018-10-26 14:47:40 +02002281 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
2282 si_shutr(&s->si[1]);
2283 si_shutw(&s->si[1]);
2284
2285 /*
2286 * starting from there this the same code as
2287 * http_end_txn_clean_session().
2288 *
2289 * It allows to do frontend keepalive while reconnecting to a
2290 * new server for each request.
2291 */
2292
2293 if (s->flags & SF_BE_ASSIGNED) {
2294 HA_ATOMIC_SUB(&be->beconn, 1);
2295 if (unlikely(s->srv_conn))
2296 sess_change_server(s, NULL);
2297 }
2298
2299 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2300 stream_process_counters(s);
2301
2302 /* don't count other requests' data */
2303 s->logs.bytes_in -= ci_data(&s->req);
2304 s->logs.bytes_out -= ci_data(&s->res);
2305
2306 /* we may need to know the position in the queue */
2307 pendconn_free(s);
2308
2309 /* let's do a final log if we need it */
2310 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
2311 !(s->flags & SF_MONITOR) &&
2312 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
2313 s->do_log(s);
2314 }
2315
2316 /* stop tracking content-based counters */
2317 stream_stop_content_counters(s);
2318 stream_update_time_stats(s);
2319
2320 s->logs.accept_date = date; /* user-visible date for logging */
2321 s->logs.tv_accept = now; /* corrected date for internal use */
2322 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
2323 s->logs.t_idle = -1;
2324 tv_zero(&s->logs.tv_request);
2325 s->logs.t_queue = -1;
2326 s->logs.t_connect = -1;
2327 s->logs.t_data = -1;
2328 s->logs.t_close = 0;
2329 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
2330 s->logs.srv_queue_pos = 0; /* we will get this number soon */
2331
2332 s->logs.bytes_in = s->req.total = ci_data(&s->req);
2333 s->logs.bytes_out = s->res.total = ci_data(&s->res);
2334
2335 stream_del_srv_conn(s);
2336 if (objt_server(s->target)) {
2337 if (s->flags & SF_CURR_SESS) {
2338 s->flags &= ~SF_CURR_SESS;
Willy Tarreaub54c40a2018-12-02 19:28:41 +01002339 HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002340 }
Willy Tarreaub54c40a2018-12-02 19:28:41 +01002341 if (may_dequeue_tasks(__objt_server(s->target), be))
2342 process_srv_queue(__objt_server(s->target));
William Lallemandcf62f7e2018-10-26 14:47:40 +02002343 }
2344
2345 s->target = NULL;
2346
2347 /* only release our endpoint if we don't intend to reuse the
2348 * connection.
2349 */
2350 if (!si_conn_ready(&s->si[1])) {
2351 si_release_endpoint(&s->si[1]);
2352 s->srv_conn = NULL;
2353 }
2354
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002355 sockaddr_free(&s->target_addr);
2356
William Lallemandcf62f7e2018-10-26 14:47:40 +02002357 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
2358 s->si[1].err_type = SI_ET_NONE;
2359 s->si[1].conn_retries = 0; /* used for logging too */
2360 s->si[1].exp = TICK_ETERNITY;
2361 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */
Christopher Faulet22dc2482019-07-16 14:43:08 +02002362 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WROTE_DATA);
William Lallemand19338012019-06-25 18:00:19 +02002363 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|CF_READ_NULL);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002364 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
2365 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
2366 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
2367 /* reinitialise the current rule list pointer to NULL. We are sure that
2368 * any rulelist match the NULL pointer.
2369 */
2370 s->current_rule_list = NULL;
2371
2372 s->be = strm_fe(s);
2373 s->logs.logwait = strm_fe(s)->to_log;
2374 s->logs.level = 0;
2375 stream_del_srv_conn(s);
2376 s->target = NULL;
2377 /* re-init store persistence */
2378 s->store_count = 0;
2379 s->uniq_id = global.req_count++;
2380
2381 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
2382
2383 s->req.flags |= CF_WAKE_ONCE; /* need to be called again if there is some command left in the request */
2384
2385 s->req.analysers |= AN_REQ_WAIT_CLI;
2386 s->res.analysers &= ~AN_RES_WAIT_CLI;
2387
2388 /* We must trim any excess data from the response buffer, because we
2389 * may have blocked an invalid response from a server that we don't
Ilya Shipitsind4259502020-04-08 01:07:56 +05002390 * want to accidentally forward once we disable the analysers, nor do
William Lallemandcf62f7e2018-10-26 14:47:40 +02002391 * we want those data to come along with next response. A typical
2392 * example of such data would be from a buggy server responding to
2393 * a HEAD with some data, or sending more than the advertised
2394 * content-length.
2395 */
2396 if (unlikely(ci_data(&s->res)))
2397 b_set_data(&s->res.buf, co_data(&s->res));
2398
2399 /* Now we can realign the response buffer */
2400 c_realign_if_empty(&s->res);
2401
2402 s->req.rto = strm_fe(s)->timeout.client;
2403 s->req.wto = TICK_ETERNITY;
2404
2405 s->res.rto = TICK_ETERNITY;
2406 s->res.wto = strm_fe(s)->timeout.client;
2407
2408 s->req.rex = TICK_ETERNITY;
2409 s->req.wex = TICK_ETERNITY;
2410 s->req.analyse_exp = TICK_ETERNITY;
2411 s->res.rex = TICK_ETERNITY;
2412 s->res.wex = TICK_ETERNITY;
2413 s->res.analyse_exp = TICK_ETERNITY;
2414 s->si[1].hcto = TICK_ETERNITY;
2415
2416 /* we're removing the analysers, we MUST re-enable events detection.
2417 * We don't enable close on the response channel since it's either
2418 * already closed, or in keep-alive with an idle connection handler.
2419 */
2420 channel_auto_read(&s->req);
2421 channel_auto_close(&s->req);
2422 channel_auto_read(&s->res);
2423
2424
2425 return 1;
2426 }
2427 return 0;
2428}
2429
William Lallemand8a022572018-10-26 14:47:35 +02002430/*
2431 * The mworker functions are used to initialize the CLI in the master process
2432 */
2433
William Lallemand309dc9a2018-10-26 14:47:45 +02002434 /*
2435 * Stop the mworker proxy
2436 */
2437void mworker_cli_proxy_stop()
2438{
William Lallemand550db6d2018-11-06 17:37:12 +01002439 if (mworker_proxy)
2440 stop_proxy(mworker_proxy);
William Lallemand309dc9a2018-10-26 14:47:45 +02002441}
2442
William Lallemand8a022572018-10-26 14:47:35 +02002443/*
2444 * Create the mworker CLI proxy
2445 */
2446int mworker_cli_proxy_create()
2447{
2448 struct mworker_proc *child;
William Lallemand744a0892018-11-22 16:46:51 +01002449 char *msg = NULL;
2450 char *errmsg = NULL;
William Lallemand8a022572018-10-26 14:47:35 +02002451
2452 mworker_proxy = calloc(1, sizeof(*mworker_proxy));
2453 if (!mworker_proxy)
2454 return -1;
2455
2456 init_new_proxy(mworker_proxy);
2457 mworker_proxy->next = proxies_list;
2458 proxies_list = mworker_proxy;
2459 mworker_proxy->id = strdup("MASTER");
William Lallemandcf62f7e2018-10-26 14:47:40 +02002460 mworker_proxy->mode = PR_MODE_CLI;
William Lallemand8a022572018-10-26 14:47:35 +02002461 mworker_proxy->state = PR_STNEW;
2462 mworker_proxy->last_change = now.tv_sec;
2463 mworker_proxy->cap = PR_CAP_LISTEN; /* this is a listen section */
2464 mworker_proxy->maxconn = 10; /* default to 10 concurrent connections */
2465 mworker_proxy->timeout.client = 0; /* no timeout */
2466 mworker_proxy->conf.file = strdup("MASTER");
2467 mworker_proxy->conf.line = 0;
2468 mworker_proxy->accept = frontend_accept;
2469 mworker_proxy-> lbprm.algo = BE_LB_ALGO_NONE;
2470
2471 /* Does not init the default target the CLI applet, but must be done in
2472 * the request parsing code */
2473 mworker_proxy->default_target = NULL;
2474
2475 /* the check_config_validity() will get an ID for the proxy */
2476 mworker_proxy->uuid = -1;
2477
2478 proxy_store_name(mworker_proxy);
2479
2480 /* create all servers using the mworker_proc list */
2481 list_for_each_entry(child, &proc_list, list) {
William Lallemand8a022572018-10-26 14:47:35 +02002482 struct server *newsrv = NULL;
2483 struct sockaddr_storage *sk;
2484 int port1, port2, port;
2485 struct protocol *proto;
William Lallemand8a022572018-10-26 14:47:35 +02002486
William Lallemanda31b09e2020-01-14 15:25:02 +01002487 /* only the workers support the master CLI */
2488 if (!(child->options & PROC_O_TYPE_WORKER))
2489 continue;
2490
William Lallemand8a022572018-10-26 14:47:35 +02002491 newsrv = new_server(mworker_proxy);
2492 if (!newsrv)
William Lallemand744a0892018-11-22 16:46:51 +01002493 goto error;
William Lallemand8a022572018-10-26 14:47:35 +02002494
2495 /* we don't know the new pid yet */
2496 if (child->pid == -1)
2497 memprintf(&msg, "cur-%d", child->relative_pid);
2498 else
2499 memprintf(&msg, "old-%d", child->pid);
2500
2501 newsrv->next = mworker_proxy->srv;
2502 mworker_proxy->srv = newsrv;
2503 newsrv->conf.file = strdup(msg);
2504 newsrv->id = strdup(msg);
2505 newsrv->conf.line = 0;
2506
2507 memprintf(&msg, "sockpair@%d", child->ipc_fd[0]);
Tim Duesterhus4cae3b22018-11-22 16:46:50 +01002508 if ((sk = str2sa_range(msg, &port, &port1, &port2, &errmsg, NULL, NULL, 0)) == 0) {
William Lallemand744a0892018-11-22 16:46:51 +01002509 goto error;
Tim Duesterhus4cae3b22018-11-22 16:46:50 +01002510 }
2511 free(msg);
2512 msg = NULL;
William Lallemand8a022572018-10-26 14:47:35 +02002513
2514 proto = protocol_by_family(sk->ss_family);
2515 if (!proto || !proto->connect) {
William Lallemand744a0892018-11-22 16:46:51 +01002516 goto error;
William Lallemand8a022572018-10-26 14:47:35 +02002517 }
2518
2519 /* no port specified */
2520 newsrv->flags |= SRV_F_MAPPORTS;
2521 newsrv->addr = *sk;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002522 /* don't let the server participate to load balancing */
2523 newsrv->iweight = 0;
2524 newsrv->uweight = 0;
William Lallemand8a022572018-10-26 14:47:35 +02002525 srv_lb_commit_status(newsrv);
William Lallemand291810d2018-10-26 14:47:38 +02002526
2527 child->srv = newsrv;
William Lallemand8a022572018-10-26 14:47:35 +02002528 }
2529 return 0;
William Lallemand744a0892018-11-22 16:46:51 +01002530
2531error:
2532 ha_alert("%s\n", errmsg);
2533
2534 list_for_each_entry(child, &proc_list, list) {
2535 free((char *)child->srv->conf.file); /* cast because of const char * */
2536 free(child->srv->id);
2537 free(child->srv);
2538 child->srv = NULL;
2539 }
2540 free(mworker_proxy->id);
2541 free(mworker_proxy->conf.file);
2542 free(mworker_proxy);
2543 mworker_proxy = NULL;
2544 free(errmsg);
2545 free(msg);
2546
2547 return -1;
William Lallemand8a022572018-10-26 14:47:35 +02002548}
Olivier Houchardf886e342017-04-05 22:24:59 +02002549
William Lallemandce83b4a2018-10-26 14:47:30 +02002550/*
William Lallemande7361152018-10-26 14:47:36 +02002551 * Create a new listener for the master CLI proxy
2552 */
2553int mworker_cli_proxy_new_listener(char *line)
2554{
2555 struct bind_conf *bind_conf;
2556 struct listener *l;
2557 char *err = NULL;
2558 char *args[MAX_LINE_ARGS + 1];
2559 int arg;
2560 int cur_arg;
2561
William Lallemand2e945c82019-11-25 09:58:37 +01002562 arg = 1;
William Lallemande7361152018-10-26 14:47:36 +02002563 args[0] = line;
2564
2565 /* args is a bind configuration with spaces replaced by commas */
2566 while (*line && arg < MAX_LINE_ARGS) {
2567
2568 if (*line == ',') {
2569 *line++ = '\0';
2570 while (*line == ',')
2571 line++;
William Lallemand2e945c82019-11-25 09:58:37 +01002572 args[arg++] = line;
William Lallemande7361152018-10-26 14:47:36 +02002573 }
2574 line++;
2575 }
2576
William Lallemand2e945c82019-11-25 09:58:37 +01002577 args[arg] = "\0";
William Lallemande7361152018-10-26 14:47:36 +02002578
2579 bind_conf = bind_conf_alloc(mworker_proxy, "master-socket", 0, "", xprt_get(XPRT_RAW));
William Lallemand744a0892018-11-22 16:46:51 +01002580 if (!bind_conf)
2581 goto err;
William Lallemande7361152018-10-26 14:47:36 +02002582
2583 bind_conf->level &= ~ACCESS_LVL_MASK;
2584 bind_conf->level |= ACCESS_LVL_ADMIN;
2585
2586 if (!str2listener(args[0], mworker_proxy, bind_conf, "master-socket", 0, &err)) {
2587 ha_alert("Cannot create the listener of the master CLI\n");
William Lallemand744a0892018-11-22 16:46:51 +01002588 goto err;
William Lallemande7361152018-10-26 14:47:36 +02002589 }
2590
2591 cur_arg = 1;
2592
2593 while (*args[cur_arg]) {
2594 static int bind_dumped;
2595 struct bind_kw *kw;
2596
2597 kw = bind_find_kw(args[cur_arg]);
2598 if (kw) {
2599 if (!kw->parse) {
2600 memprintf(&err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
2601 args[0], args[1], args[cur_arg]);
2602 goto err;
2603 }
2604
2605 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, &err) != 0) {
2606 if (err)
2607 memprintf(&err, "'%s %s' : '%s'", args[0], args[1], err);
2608 else
2609 memprintf(&err, "'%s %s' : error encountered while processing '%s'",
2610 args[0], args[1], args[cur_arg]);
2611 goto err;
2612 }
2613
2614 cur_arg += 1 + kw->skip;
2615 continue;
2616 }
2617
2618 if (!bind_dumped) {
2619 bind_dump_kws(&err);
2620 indent_msg(&err, 4);
2621 bind_dumped = 1;
2622 }
2623
2624 memprintf(&err, "'%s %s' : unknown keyword '%s'.%s%s",
2625 args[0], args[1], args[cur_arg],
2626 err ? " Registered keywords :" : "", err ? err : "");
2627 goto err;
2628 }
2629
2630
2631 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
William Lallemande7361152018-10-26 14:47:36 +02002632 l->accept = session_accept_fd;
2633 l->default_target = mworker_proxy->default_target;
2634 /* don't make the peers subject to global limits and don't close it in the master */
2635 l->options |= (LI_O_UNLIMITED|LI_O_MWORKER); /* we are keeping this FD in the master */
2636 l->nice = -64; /* we want to boost priority for local stats */
Willy Tarreau18215cb2019-02-27 16:25:28 +01002637 global.maxsock++; /* for the listening socket */
William Lallemande7361152018-10-26 14:47:36 +02002638 }
Willy Tarreau18215cb2019-02-27 16:25:28 +01002639 global.maxsock += mworker_proxy->maxconn;
William Lallemande7361152018-10-26 14:47:36 +02002640
2641 return 0;
2642
2643err:
2644 ha_alert("%s\n", err);
William Lallemand744a0892018-11-22 16:46:51 +01002645 free(err);
2646 free(bind_conf);
William Lallemande7361152018-10-26 14:47:36 +02002647 return -1;
2648
2649}
2650
2651/*
William Lallemandce83b4a2018-10-26 14:47:30 +02002652 * Create a new CLI socket using a socketpair for a worker process
2653 * <mworker_proc> is the process structure, and <proc> is the process number
2654 */
2655int mworker_cli_sockpair_new(struct mworker_proc *mworker_proc, int proc)
2656{
2657 struct bind_conf *bind_conf;
2658 struct listener *l;
2659 char *path = NULL;
2660 char *err = NULL;
2661
2662 /* master pipe to ensure the master is still alive */
2663 if (socketpair(AF_UNIX, SOCK_STREAM, 0, mworker_proc->ipc_fd) < 0) {
2664 ha_alert("Cannot create worker socketpair.\n");
2665 return -1;
2666 }
2667
2668 /* XXX: we might want to use a separate frontend at some point */
2669 if (!global.stats_fe) {
2670 if ((global.stats_fe = alloc_stats_fe("GLOBAL", "master-socket", 0)) == NULL) {
2671 ha_alert("out of memory trying to allocate the stats frontend");
William Lallemand744a0892018-11-22 16:46:51 +01002672 goto error;
William Lallemandce83b4a2018-10-26 14:47:30 +02002673 }
2674 }
2675
2676 bind_conf = bind_conf_alloc(global.stats_fe, "master-socket", 0, "", xprt_get(XPRT_RAW));
William Lallemand744a0892018-11-22 16:46:51 +01002677 if (!bind_conf)
2678 goto error;
2679
William Lallemandce83b4a2018-10-26 14:47:30 +02002680 bind_conf->level &= ~ACCESS_LVL_MASK;
2681 bind_conf->level |= ACCESS_LVL_ADMIN; /* TODO: need to lower the rights with a CLI keyword*/
2682
2683 bind_conf->bind_proc = 1UL << proc;
2684 global.stats_fe->bind_proc = 0; /* XXX: we should be careful with that, it can be removed by configuration */
2685
2686 if (!memprintf(&path, "sockpair@%d", mworker_proc->ipc_fd[1])) {
2687 ha_alert("Cannot allocate listener.\n");
William Lallemand744a0892018-11-22 16:46:51 +01002688 goto error;
William Lallemandce83b4a2018-10-26 14:47:30 +02002689 }
2690
2691 if (!str2listener(path, global.stats_fe, bind_conf, "master-socket", 0, &err)) {
Tim Duesterhus4cae3b22018-11-22 16:46:50 +01002692 free(path);
William Lallemandce83b4a2018-10-26 14:47:30 +02002693 ha_alert("Cannot create a CLI sockpair listener for process #%d\n", proc);
William Lallemand744a0892018-11-22 16:46:51 +01002694 goto error;
William Lallemandce83b4a2018-10-26 14:47:30 +02002695 }
Tim Duesterhus4cae3b22018-11-22 16:46:50 +01002696 free(path);
2697 path = NULL;
William Lallemandce83b4a2018-10-26 14:47:30 +02002698
2699 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
William Lallemandce83b4a2018-10-26 14:47:30 +02002700 l->accept = session_accept_fd;
2701 l->default_target = global.stats_fe->default_target;
William Lallemanda3372292018-11-16 16:57:22 +01002702 l->options |= (LI_O_UNLIMITED | LI_O_NOSTOP);
William Lallemandce83b4a2018-10-26 14:47:30 +02002703 /* it's a sockpair but we don't want to keep the fd in the master */
2704 l->options &= ~LI_O_INHERITED;
2705 l->nice = -64; /* we want to boost priority for local stats */
Willy Tarreau18215cb2019-02-27 16:25:28 +01002706 global.maxsock++; /* for the listening socket */
William Lallemandce83b4a2018-10-26 14:47:30 +02002707 }
2708
2709 return 0;
William Lallemand744a0892018-11-22 16:46:51 +01002710
2711error:
2712 close(mworker_proc->ipc_fd[0]);
2713 close(mworker_proc->ipc_fd[1]);
2714 free(err);
2715
2716 return -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02002717}
2718
William Lallemand74c24fb2016-11-21 17:18:36 +01002719static struct applet cli_applet = {
2720 .obj_type = OBJ_TYPE_APPLET,
2721 .name = "<CLI>", /* used for logging */
2722 .fct = cli_io_handler,
2723 .release = cli_release_handler,
2724};
2725
Willy Tarreau0a739292016-11-22 20:21:23 +01002726/* register cli keywords */
2727static struct cli_kw_list cli_kws = {{ },{
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002728 { { "help", NULL }, NULL, cli_parse_simple, NULL },
2729 { { "prompt", NULL }, NULL, cli_parse_simple, NULL },
2730 { { "quit", NULL }, NULL, cli_parse_simple, NULL },
Willy Tarreau2af99412016-11-23 11:10:59 +01002731 { { "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 +01002732 { { "set", "rate-limit", NULL }, "set rate-limit : change a rate limiting value", cli_parse_set_ratelimit, NULL },
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02002733 { { "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 +01002734 { { "set", "timeout", NULL }, "set timeout : change a timeout setting", cli_parse_set_timeout, NULL, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01002735 { { "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 +02002736 { { "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 },
William Lallemand67a234f2018-12-13 09:05:45 +01002737 { { "show", "cli", "level", NULL }, "show cli level : display the level of the current CLI session", cli_parse_show_lvl, NULL, NULL, NULL, ACCESS_MASTER},
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02002738 { { "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 +01002739 { { "show", "activity", NULL }, "show activity : show per-thread activity stats (for support/developers)", cli_parse_default, cli_io_handler_show_activity, NULL },
William Lallemand67a234f2018-12-13 09:05:45 +01002740 { { "operator", NULL }, "operator : lower the level of the current CLI session to operator", cli_parse_set_lvl, NULL, NULL, NULL, ACCESS_MASTER},
2741 { { "user", NULL }, "user : lower the level of the current CLI session to user", cli_parse_set_lvl, NULL, NULL, NULL, ACCESS_MASTER},
Olivier Houchardf886e342017-04-05 22:24:59 +02002742 { { "_getsocks", NULL }, NULL, _getsocks, NULL },
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02002743 { { "expert-mode", NULL }, NULL, cli_parse_expert_mode, NULL }, // not listed
Willy Tarreau0a739292016-11-22 20:21:23 +01002744 {{},}
2745}};
2746
Willy Tarreau0108d902018-11-25 19:14:37 +01002747INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
2748
William Lallemand74c24fb2016-11-21 17:18:36 +01002749static struct cfg_kw_list cfg_kws = {ILH, {
2750 { CFG_GLOBAL, "stats", stats_parse_global },
2751 { 0, NULL, NULL },
2752}};
2753
Willy Tarreau0108d902018-11-25 19:14:37 +01002754INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2755
William Lallemand74c24fb2016-11-21 17:18:36 +01002756static struct bind_kw_list bind_kws = { "STAT", { }, {
William Lallemandf6975e92017-05-26 17:42:10 +02002757 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
2758 { "expose-fd", bind_parse_expose_fd, 1 }, /* set the unix socket expose fd rights */
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02002759 { "severity-output", bind_parse_severity_output, 1 }, /* set the severity output format */
William Lallemand74c24fb2016-11-21 17:18:36 +01002760 { NULL, NULL, 0 },
2761}};
2762
Willy Tarreau0108d902018-11-25 19:14:37 +01002763INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
William Lallemand74c24fb2016-11-21 17:18:36 +01002764
2765/*
2766 * Local variables:
2767 * c-indent-level: 8
2768 * c-basic-offset: 8
2769 * End:
2770 */