blob: fef34e97104044e3aa97f2eacae280dabd56b0b2 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Olivier Houchardf886e342017-04-05 22:24:59 +020027#include <net/if.h>
28
William Lallemand74c24fb2016-11-21 17:18:36 +010029#include <common/cfgparse.h>
30#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
36#include <common/ticks.h>
37#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
40#include <common/base64.h>
41
42#include <types/applet.h>
William Lallemand9ed62032016-11-21 17:49:11 +010043#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010044#include <types/global.h>
45#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010046#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010047
48#include <proto/backend.h>
49#include <proto/channel.h>
50#include <proto/checks.h>
51#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010052#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010053#include <proto/fd.h>
54#include <proto/freq_ctr.h>
55#include <proto/frontend.h>
56#include <proto/log.h>
57#include <proto/pattern.h>
58#include <proto/pipe.h>
59#include <proto/listener.h>
60#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010061#include <proto/proto_uxst.h>
62#include <proto/proxy.h>
63#include <proto/sample.h>
64#include <proto/session.h>
65#include <proto/stream.h>
66#include <proto/server.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010067#include <proto/stream_interface.h>
68#include <proto/task.h>
69
William Lallemand74c24fb2016-11-21 17:18:36 +010070static struct applet cli_applet;
71
72static const char stats_sock_usage_msg[] =
73 "Unknown command. Please enter one of the following commands only :\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010074 " help : this message\n"
75 " prompt : toggle interactive mode with prompt\n"
76 " quit : disconnect\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010077 "";
78
79static const char stats_permission_denied_msg[] =
80 "Permission denied\n"
81 "";
82
83
84static char *dynamic_usage_msg = NULL;
85
86/* List head of cli keywords */
87static struct cli_kw_list cli_keywords = {
88 .list = LIST_HEAD_INIT(cli_keywords.list)
89};
90
91extern const char *stat_status_codes[];
92
93char *cli_gen_usage_msg()
94{
95 struct cli_kw_list *kw_list;
96 struct cli_kw *kw;
97 struct chunk *tmp = get_trash_chunk();
98 struct chunk out;
99
100 free(dynamic_usage_msg);
101 dynamic_usage_msg = NULL;
102
103 if (LIST_ISEMPTY(&cli_keywords.list))
104 return NULL;
105
106 chunk_reset(tmp);
107 chunk_strcat(tmp, stats_sock_usage_msg);
108 list_for_each_entry(kw_list, &cli_keywords.list, list) {
109 kw = &kw_list->kw[0];
110 while (kw->usage) {
111 chunk_appendf(tmp, " %s\n", kw->usage);
112 kw++;
113 }
114 }
115 chunk_init(&out, NULL, 0);
116 chunk_dup(&out, tmp);
117 dynamic_usage_msg = out.str;
118 return dynamic_usage_msg;
119}
120
121struct cli_kw* cli_find_kw(char **args)
122{
123 struct cli_kw_list *kw_list;
124 struct cli_kw *kw;/* current cli_kw */
125 char **tmp_args;
126 const char **tmp_str_kw;
127 int found = 0;
128
129 if (LIST_ISEMPTY(&cli_keywords.list))
130 return NULL;
131
132 list_for_each_entry(kw_list, &cli_keywords.list, list) {
133 kw = &kw_list->kw[0];
134 while (*kw->str_kw) {
135 tmp_args = args;
136 tmp_str_kw = kw->str_kw;
137 while (*tmp_str_kw) {
138 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
139 found = 1;
140 } else {
141 found = 0;
142 break;
143 }
144 tmp_args++;
145 tmp_str_kw++;
146 }
147 if (found)
148 return (kw);
149 kw++;
150 }
151 }
152 return NULL;
153}
154
155void cli_register_kw(struct cli_kw_list *kw_list)
156{
157 LIST_ADDQ(&cli_keywords.list, &kw_list->list);
158}
159
160
161/* allocate a new stats frontend named <name>, and return it
162 * (or NULL in case of lack of memory).
163 */
164static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
165{
166 struct proxy *fe;
167
168 fe = calloc(1, sizeof(*fe));
169 if (!fe)
170 return NULL;
171
172 init_new_proxy(fe);
173 fe->next = proxy;
174 proxy = fe;
175 fe->last_change = now.tv_sec;
176 fe->id = strdup("GLOBAL");
177 fe->cap = PR_CAP_FE;
178 fe->maxconn = 10; /* default to 10 concurrent connections */
179 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
180 fe->conf.file = strdup(file);
181 fe->conf.line = line;
182 fe->accept = frontend_accept;
183 fe->default_target = &cli_applet.obj_type;
184
185 /* the stats frontend is the only one able to assign ID #0 */
186 fe->conf.id.key = fe->uuid = 0;
187 eb32_insert(&used_proxy_id, &fe->conf.id);
188 return fe;
189}
190
191/* This function parses a "stats" statement in the "global" section. It returns
192 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
193 * error message into the <err> buffer which will be preallocated. The trailing
194 * '\n' must not be written. The function must be called with <args> pointing to
195 * the first word after "stats".
196 */
197static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
198 struct proxy *defpx, const char *file, int line,
199 char **err)
200{
201 struct bind_conf *bind_conf;
202 struct listener *l;
203
204 if (!strcmp(args[1], "socket")) {
205 int cur_arg;
206
207 if (*args[2] == 0) {
208 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
209 return -1;
210 }
211
212 if (!global.stats_fe) {
213 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
214 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
215 return -1;
216 }
217 }
218
Willy Tarreaua261e9b2016-12-22 20:44:00 +0100219 bind_conf = bind_conf_alloc(global.stats_fe, file, line, args[2], xprt_get(XPRT_RAW));
William Lallemand07a62f72017-05-24 00:57:40 +0200220 bind_conf->level &= ~ACCESS_LVL_MASK;
221 bind_conf->level |= ACCESS_LVL_OPER; /* default access level */
William Lallemand74c24fb2016-11-21 17:18:36 +0100222
223 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
224 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
225 file, line, args[0], args[1], err && *err ? *err : "error");
226 return -1;
227 }
228
229 cur_arg = 3;
230 while (*args[cur_arg]) {
231 static int bind_dumped;
232 struct bind_kw *kw;
233
234 kw = bind_find_kw(args[cur_arg]);
235 if (kw) {
236 if (!kw->parse) {
237 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
238 args[0], args[1], args[cur_arg]);
239 return -1;
240 }
241
242 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
243 if (err && *err)
244 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
245 else
246 memprintf(err, "'%s %s' : error encountered while processing '%s'",
247 args[0], args[1], args[cur_arg]);
248 return -1;
249 }
250
251 cur_arg += 1 + kw->skip;
252 continue;
253 }
254
255 if (!bind_dumped) {
256 bind_dump_kws(err);
257 indent_msg(err, 4);
258 bind_dumped = 1;
259 }
260
261 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
262 args[0], args[1], args[cur_arg],
263 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
264 return -1;
265 }
266
267 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
268 l->maxconn = global.stats_fe->maxconn;
269 l->backlog = global.stats_fe->backlog;
270 l->accept = session_accept_fd;
271 l->handler = process_stream;
272 l->default_target = global.stats_fe->default_target;
273 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
274 l->nice = -64; /* we want to boost priority for local stats */
275 global.maxsock += l->maxconn;
276 }
277 }
278 else if (!strcmp(args[1], "timeout")) {
279 unsigned timeout;
280 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
281
282 if (res) {
283 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
284 return -1;
285 }
286
287 if (!timeout) {
288 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
289 return -1;
290 }
291 if (!global.stats_fe) {
292 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
293 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
294 return -1;
295 }
296 }
297 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
298 }
299 else if (!strcmp(args[1], "maxconn")) {
300 int maxconn = atol(args[2]);
301
302 if (maxconn <= 0) {
303 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
304 return -1;
305 }
306
307 if (!global.stats_fe) {
308 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
309 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
310 return -1;
311 }
312 }
313 global.stats_fe->maxconn = maxconn;
314 }
315 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
316 int cur_arg = 2;
317 unsigned long set = 0;
318
319 if (!global.stats_fe) {
320 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
321 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
322 return -1;
323 }
324 }
325
326 while (*args[cur_arg]) {
327 unsigned int low, high;
328
329 if (strcmp(args[cur_arg], "all") == 0) {
330 set = 0;
331 break;
332 }
333 else if (strcmp(args[cur_arg], "odd") == 0) {
334 set |= ~0UL/3UL; /* 0x555....555 */
335 }
336 else if (strcmp(args[cur_arg], "even") == 0) {
337 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
338 }
339 else if (isdigit((int)*args[cur_arg])) {
340 char *dash = strchr(args[cur_arg], '-');
341
342 low = high = str2uic(args[cur_arg]);
343 if (dash)
344 high = str2uic(dash + 1);
345
346 if (high < low) {
347 unsigned int swap = low;
348 low = high;
349 high = swap;
350 }
351
352 if (low < 1 || high > LONGBITS) {
353 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
354 args[0], args[1], LONGBITS);
355 return -1;
356 }
357 while (low <= high)
358 set |= 1UL << (low++ - 1);
359 }
360 else {
361 memprintf(err,
362 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
363 args[0], args[1], LONGBITS);
364 return -1;
365 }
366 cur_arg++;
367 }
368 global.stats_fe->bind_proc = set;
369 }
370 else {
371 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
372 return -1;
373 }
374 return 0;
375}
376
Willy Tarreaude57a572016-11-23 17:01:39 +0100377/* Verifies that the CLI at least has a level at least as high as <level>
378 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
379 * failure, an error message is prepared and the appctx's state is adjusted
380 * to print it so that a return 1 is enough to abort any processing.
381 */
382int cli_has_level(struct appctx *appctx, int level)
383{
384 struct stream_interface *si = appctx->owner;
385 struct stream *s = si_strm(si);
386
William Lallemand07a62f72017-05-24 00:57:40 +0200387 if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < level) {
Willy Tarreaude57a572016-11-23 17:01:39 +0100388 appctx->ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100389 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaude57a572016-11-23 17:01:39 +0100390 return 0;
391 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100392 return 1;
393}
394
William Lallemand74c24fb2016-11-21 17:18:36 +0100395
Willy Tarreau41908562016-11-24 16:23:38 +0100396/* Processes the CLI interpreter on the stats socket. This function is called
397 * from the CLI's IO handler running in an appctx context. The function returns 1
398 * if the request was understood, otherwise zero. It is called with appctx->st0
399 * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do
400 * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to
401 * have its own I/O handler called again. Most of the time, parsers will only
402 * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg.
Willy Tarreaueaffde32016-12-16 17:59:25 +0100403 * If a keyword parser is NULL and an I/O handler is declared, the I/O handler
404 * will automatically be used.
William Lallemand74c24fb2016-11-21 17:18:36 +0100405 */
Willy Tarreau41908562016-11-24 16:23:38 +0100406static int cli_parse_request(struct appctx *appctx, char *line)
William Lallemand74c24fb2016-11-21 17:18:36 +0100407{
William Lallemand74c24fb2016-11-21 17:18:36 +0100408 char *args[MAX_STATS_ARGS + 1];
409 struct cli_kw *kw;
410 int arg;
411 int i, j;
412
413 while (isspace((unsigned char)*line))
414 line++;
415
416 arg = 0;
417 args[arg] = line;
418
419 while (*line && arg < MAX_STATS_ARGS) {
420 if (*line == '\\') {
421 line++;
422 if (*line == '\0')
423 break;
424 }
425 else if (isspace((unsigned char)*line)) {
426 *line++ = '\0';
427
428 while (isspace((unsigned char)*line))
429 line++;
430
431 args[++arg] = line;
432 continue;
433 }
434
435 line++;
436 }
437
438 while (++arg <= MAX_STATS_ARGS)
439 args[arg] = line;
440
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100441 /* unescape '\' */
William Lallemand74c24fb2016-11-21 17:18:36 +0100442 arg = 0;
443 while (*args[arg] != '\0') {
444 j = 0;
445 for (i=0; args[arg][i] != '\0'; i++) {
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100446 if (args[arg][i] == '\\') {
447 if (args[arg][i+1] == '\\')
448 i++;
449 else
450 continue;
451 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100452 args[arg][j] = args[arg][i];
453 j++;
454 }
455 args[arg][j] = '\0';
456 arg++;
457 }
458
Willy Tarreau41908562016-11-24 16:23:38 +0100459 appctx->st2 = 0;
Willy Tarreaua2d58722016-12-16 12:37:03 +0100460 memset(&appctx->ctx.cli, 0, sizeof(appctx->ctx.cli));
Willy Tarreau41908562016-11-24 16:23:38 +0100461
462 kw = cli_find_kw(args);
Willy Tarreaueaffde32016-12-16 17:59:25 +0100463 if (!kw)
Willy Tarreau41908562016-11-24 16:23:38 +0100464 return 0;
465
466 appctx->io_handler = kw->io_handler;
Emeric Brund6871f72017-06-29 19:54:13 +0200467 appctx->io_release = kw->io_release;
468 /* kw->parse could set its own io_handler or ip_release handler */
Willy Tarreaueaffde32016-12-16 17:59:25 +0100469 if ((!kw->parse || kw->parse(args, appctx, kw->private) == 0) && appctx->io_handler) {
Willy Tarreau41908562016-11-24 16:23:38 +0100470 appctx->st0 = CLI_ST_CALLBACK;
William Lallemand74c24fb2016-11-21 17:18:36 +0100471 }
Willy Tarreau41908562016-11-24 16:23:38 +0100472 return 1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100473}
474
475/* This I/O handler runs as an applet embedded in a stream interface. It is
476 * used to processes I/O from/to the stats unix socket. The system relies on a
477 * state machine handling requests and various responses. We read a request,
478 * then we process it and send the response, and we possibly display a prompt.
479 * Then we can read again. The state is stored in appctx->st0 and is one of the
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100480 * CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled
William Lallemand74c24fb2016-11-21 17:18:36 +0100481 * or not.
482 */
483static void cli_io_handler(struct appctx *appctx)
484{
485 struct stream_interface *si = appctx->owner;
486 struct channel *req = si_oc(si);
487 struct channel *res = si_ic(si);
488 int reql;
489 int len;
490
491 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
492 goto out;
493
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100494 /* Check if the input buffer is avalaible. */
495 if (res->buf->size == 0) {
496 si_applet_cant_put(si);
497 goto out;
498 }
499
William Lallemand74c24fb2016-11-21 17:18:36 +0100500 while (1) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100501 if (appctx->st0 == CLI_ST_INIT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100502 /* Stats output not initialized yet */
503 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100504 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100505 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100506 else if (appctx->st0 == CLI_ST_END) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100507 /* Let's close for real now. We just close the request
508 * side, the conditions below will complete if needed.
509 */
510 si_shutw(si);
511 break;
512 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100513 else if (appctx->st0 == CLI_ST_GETREQ) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100514 /* ensure we have some output room left in the event we
515 * would want to return some info right after parsing.
516 */
517 if (buffer_almost_full(si_ib(si))) {
518 si_applet_cant_put(si);
519 break;
520 }
521
522 reql = bo_getline(si_oc(si), trash.str, trash.size);
523 if (reql <= 0) { /* closed or EOL not found */
524 if (reql == 0)
525 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100526 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100527 continue;
528 }
529
530 /* seek for a possible unescaped semi-colon. If we find
531 * one, we replace it with an LF and skip only this part.
532 */
533 for (len = 0; len < reql; len++) {
534 if (trash.str[len] == '\\') {
535 len++;
536 continue;
537 }
538 if (trash.str[len] == ';') {
539 trash.str[len] = '\n';
540 reql = len + 1;
541 break;
542 }
543 }
544
545 /* now it is time to check that we have a full line,
546 * remove the trailing \n and possibly \r, then cut the
547 * line.
548 */
549 len = reql - 1;
550 if (trash.str[len] != '\n') {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100551 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100552 continue;
553 }
554
555 if (len && trash.str[len-1] == '\r')
556 len--;
557
558 trash.str[len] = '\0';
559
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100560 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100561 if (len) {
562 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100563 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100564 continue;
565 }
566 else if (strcmp(trash.str, "prompt") == 0)
567 appctx->st1 = !appctx->st1;
568 else if (strcmp(trash.str, "help") == 0 ||
Willy Tarreau41908562016-11-24 16:23:38 +0100569 !cli_parse_request(appctx, trash.str)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100570 cli_gen_usage_msg();
571 if (dynamic_usage_msg)
572 appctx->ctx.cli.msg = dynamic_usage_msg;
573 else
574 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100575 appctx->st0 = CLI_ST_PRINT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100576 }
577 /* NB: stats_sock_parse_request() may have put
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100578 * another CLI_ST_O_* into appctx->st0.
William Lallemand74c24fb2016-11-21 17:18:36 +0100579 */
580 }
581 else if (!appctx->st1) {
582 /* if prompt is disabled, print help on empty lines,
583 * so that the user at least knows how to enable
584 * prompt and find help.
585 */
586 cli_gen_usage_msg();
587 if (dynamic_usage_msg)
588 appctx->ctx.cli.msg = dynamic_usage_msg;
589 else
590 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100591 appctx->st0 = CLI_ST_PRINT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100592 }
593
594 /* re-adjust req buffer */
595 bo_skip(si_oc(si), reql);
596 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
597 }
598 else { /* output functions */
599 switch (appctx->st0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100600 case CLI_ST_PROMPT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100601 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100602 case CLI_ST_PRINT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100603 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100604 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100605 else
606 si_applet_cant_put(si);
607 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100608 case CLI_ST_PRINT_FREE:
William Lallemand74c24fb2016-11-21 17:18:36 +0100609 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
610 free(appctx->ctx.cli.err);
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100611 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100612 }
613 else
614 si_applet_cant_put(si);
615 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100616 case CLI_ST_CALLBACK: /* use custom pointer */
William Lallemand74c24fb2016-11-21 17:18:36 +0100617 if (appctx->io_handler)
618 if (appctx->io_handler(appctx)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100619 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100620 if (appctx->io_release) {
621 appctx->io_release(appctx);
622 appctx->io_release = NULL;
623 }
624 }
625 break;
626 default: /* abnormal state */
627 si->flags |= SI_FL_ERR;
628 break;
629 }
630
631 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100632 if (appctx->st0 == CLI_ST_PROMPT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100633 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100634 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100635 else
636 si_applet_cant_put(si);
637 }
638
639 /* If the output functions are still there, it means they require more room. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100640 if (appctx->st0 >= CLI_ST_OUTPUT)
William Lallemand74c24fb2016-11-21 17:18:36 +0100641 break;
642
643 /* Now we close the output if one of the writers did so,
644 * or if we're not in interactive mode and the request
645 * buffer is empty. This still allows pipelined requests
646 * to be sent in non-interactive mode.
647 */
648 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100649 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100650 continue;
651 }
652
653 /* switch state back to GETREQ to read next requests */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100654 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100655 }
656 }
657
658 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
659 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
660 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
661 /* Other side has closed, let's abort if we have no more processing to do
662 * and nothing more to consume. This is comparable to a broken pipe, so
663 * we forward the close to the request side so that it flows upstream to
664 * the client.
665 */
666 si_shutw(si);
667 }
668
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100669 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < CLI_ST_OUTPUT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100670 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
671 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
672 /* We have no more processing to do, and nothing more to send, and
673 * the client side has closed. So we'll forward this state downstream
674 * on the response buffer.
675 */
676 si_shutr(si);
677 res->flags |= CF_READ_NULL;
678 }
679
680 out:
681 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
682 __FUNCTION__, __LINE__,
683 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
684}
685
William Lallemand74c24fb2016-11-21 17:18:36 +0100686/* This is called when the stream interface is closed. For instance, upon an
687 * external abort, we won't call the i/o handler anymore so we may need to
688 * remove back references to the stream currently being dumped.
689 */
690static void cli_release_handler(struct appctx *appctx)
691{
692 if (appctx->io_release) {
693 appctx->io_release(appctx);
694 appctx->io_release = NULL;
695 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100696 else if (appctx->st0 == CLI_ST_PRINT_FREE) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100697 free(appctx->ctx.cli.err);
698 appctx->ctx.cli.err = NULL;
699 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100700}
701
702/* This function dumps all environmnent variables to the buffer. It returns 0
703 * if the output buffer is full and it needs to be called again, otherwise
Willy Tarreauf6710f82016-12-16 17:45:44 +0100704 * non-zero. Dumps only one entry if st2 == STAT_ST_END. It uses cli.p0 as the
705 * pointer to the current variable.
William Lallemand74c24fb2016-11-21 17:18:36 +0100706 */
Willy Tarreau0a739292016-11-22 20:21:23 +0100707static int cli_io_handler_show_env(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100708{
Willy Tarreau0a739292016-11-22 20:21:23 +0100709 struct stream_interface *si = appctx->owner;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100710 char **var = appctx->ctx.cli.p0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100711
712 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
713 return 1;
714
715 chunk_reset(&trash);
716
717 /* we have two inner loops here, one for the proxy, the other one for
718 * the buffer.
719 */
Willy Tarreauf6710f82016-12-16 17:45:44 +0100720 while (*var) {
721 chunk_printf(&trash, "%s\n", *var);
William Lallemand74c24fb2016-11-21 17:18:36 +0100722
723 if (bi_putchk(si_ic(si), &trash) == -1) {
724 si_applet_cant_put(si);
725 return 0;
726 }
727 if (appctx->st2 == STAT_ST_END)
728 break;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100729 var++;
730 appctx->ctx.cli.p0 = var;
William Lallemand74c24fb2016-11-21 17:18:36 +0100731 }
732
733 /* dump complete */
734 return 1;
735}
736
William Lallemandeceddf72016-12-15 18:06:44 +0100737/*
Willy Tarreau3af9d832016-12-16 12:58:09 +0100738 * CLI IO handler for `show cli sockets`.
739 * Uses ctx.cli.p0 to store the restart pointer.
William Lallemandeceddf72016-12-15 18:06:44 +0100740 */
741static int cli_io_handler_show_cli_sock(struct appctx *appctx)
742{
743 struct bind_conf *bind_conf;
744 struct stream_interface *si = appctx->owner;
745
746 chunk_reset(&trash);
747
748 switch (appctx->st2) {
749 case STAT_ST_INIT:
750 chunk_printf(&trash, "# socket lvl processes\n");
751 if (bi_putchk(si_ic(si), &trash) == -1) {
752 si_applet_cant_put(si);
753 return 0;
754 }
William Lallemandeceddf72016-12-15 18:06:44 +0100755 appctx->st2 = STAT_ST_LIST;
756
757 case STAT_ST_LIST:
758 if (global.stats_fe) {
759 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
760 struct listener *l;
761
762 /*
Willy Tarreau3af9d832016-12-16 12:58:09 +0100763 * get the latest dumped node in appctx->ctx.cli.p0
William Lallemandeceddf72016-12-15 18:06:44 +0100764 * if the current node is the first of the list
765 */
766
Willy Tarreau3af9d832016-12-16 12:58:09 +0100767 if (appctx->ctx.cli.p0 &&
768 &bind_conf->by_fe == (&global.stats_fe->conf.bind)->n) {
William Lallemandeceddf72016-12-15 18:06:44 +0100769 /* change the current node to the latest dumped and continue the loop */
Willy Tarreau3af9d832016-12-16 12:58:09 +0100770 bind_conf = LIST_ELEM(appctx->ctx.cli.p0, typeof(bind_conf), by_fe);
William Lallemandeceddf72016-12-15 18:06:44 +0100771 continue;
772 }
773
774 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
775
776 char addr[46];
777 char port[6];
778
779 if (l->addr.ss_family == AF_UNIX) {
780 const struct sockaddr_un *un;
781
782 un = (struct sockaddr_un *)&l->addr;
783 chunk_appendf(&trash, "%s ", un->sun_path);
784 } else if (l->addr.ss_family == AF_INET) {
785 addr_to_str(&l->addr, addr, sizeof(addr));
786 port_to_str(&l->addr, port, sizeof(port));
787 chunk_appendf(&trash, "%s:%s ", addr, port);
788 } else if (l->addr.ss_family == AF_INET6) {
789 addr_to_str(&l->addr, addr, sizeof(addr));
790 port_to_str(&l->addr, port, sizeof(port));
791 chunk_appendf(&trash, "[%s]:%s ", addr, port);
792 } else
793 continue;
794
William Lallemand07a62f72017-05-24 00:57:40 +0200795 if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN)
William Lallemandeceddf72016-12-15 18:06:44 +0100796 chunk_appendf(&trash, "admin ");
William Lallemand07a62f72017-05-24 00:57:40 +0200797 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER)
798 chunk_appendf(&trash, "operator ");
799 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_USER)
800 chunk_appendf(&trash, "user ");
William Lallemandeceddf72016-12-15 18:06:44 +0100801 else
802 chunk_appendf(&trash, " ");
803
804 if (bind_conf->bind_proc != 0) {
805 int pos;
Willy Tarreau20c5e522016-12-16 12:50:55 +0100806 for (pos = 0; pos < 8 * sizeof(bind_conf->bind_proc); pos++) {
Willy Tarreau4305ac72016-12-16 12:56:31 +0100807 if (bind_conf->bind_proc & (1UL << pos)) {
William Lallemandeceddf72016-12-15 18:06:44 +0100808 chunk_appendf(&trash, "%d,", pos+1);
809 }
810 }
811 /* replace the latest comma by a newline */
812 trash.str[trash.len-1] = '\n';
813
814 } else {
815 chunk_appendf(&trash, "all\n");
816 }
817
818 if (bi_putchk(si_ic(si), &trash) == -1) {
819 si_applet_cant_put(si);
820 return 0;
821 }
822 }
Willy Tarreau3af9d832016-12-16 12:58:09 +0100823 appctx->ctx.cli.p0 = &bind_conf->by_fe; /* store the latest list node dumped */
William Lallemandeceddf72016-12-15 18:06:44 +0100824 }
825 }
826 default:
827 appctx->st2 = STAT_ST_FIN;
828 return 1;
829 }
830}
831
832
Willy Tarreau0a739292016-11-22 20:21:23 +0100833/* parse a "show env" CLI request. Returns 0 if it needs to continue, 1 if it
Willy Tarreauf6710f82016-12-16 17:45:44 +0100834 * wants to stop here. It puts the variable to be dumped into cli.p0 if a single
835 * variable is requested otherwise puts environ there.
Willy Tarreau0a739292016-11-22 20:21:23 +0100836 */
837static int cli_parse_show_env(char **args, struct appctx *appctx, void *private)
838{
839 extern char **environ;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100840 char **var;
Willy Tarreau0a739292016-11-22 20:21:23 +0100841
842 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
843 return 1;
844
Willy Tarreauf6710f82016-12-16 17:45:44 +0100845 var = environ;
Willy Tarreau0a739292016-11-22 20:21:23 +0100846
847 if (*args[2]) {
848 int len = strlen(args[2]);
849
Willy Tarreauf6710f82016-12-16 17:45:44 +0100850 for (; *var; var++) {
851 if (strncmp(*var, args[2], len) == 0 &&
852 (*var)[len] == '=')
Willy Tarreau0a739292016-11-22 20:21:23 +0100853 break;
854 }
Willy Tarreauf6710f82016-12-16 17:45:44 +0100855 if (!*var) {
Willy Tarreau0a739292016-11-22 20:21:23 +0100856 appctx->ctx.cli.msg = "Variable not found\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100857 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau0a739292016-11-22 20:21:23 +0100858 return 1;
859 }
860 appctx->st2 = STAT_ST_END;
861 }
Willy Tarreauf6710f82016-12-16 17:45:44 +0100862 appctx->ctx.cli.p0 = var;
Willy Tarreau0a739292016-11-22 20:21:23 +0100863 return 0;
864}
865
Willy Tarreau599852e2016-11-22 20:33:32 +0100866/* parse a "set timeout" CLI request. It always returns 1. */
867static int cli_parse_set_timeout(char **args, struct appctx *appctx, void *private)
868{
869 struct stream_interface *si = appctx->owner;
870 struct stream *s = si_strm(si);
871
872 if (strcmp(args[2], "cli") == 0) {
873 unsigned timeout;
874 const char *res;
875
876 if (!*args[3]) {
877 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100878 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +0100879 return 1;
880 }
881
882 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
883 if (res || timeout < 1) {
884 appctx->ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100885 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +0100886 return 1;
887 }
888
889 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
890 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
891 return 1;
892 }
893 else {
894 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100895 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +0100896 return 1;
897 }
898}
899
Willy Tarreau2af99412016-11-23 11:10:59 +0100900/* parse a "set maxconn global" command. It always returns 1. */
901static int cli_parse_set_maxconn_global(char **args, struct appctx *appctx, void *private)
902{
903 int v;
904
905 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
906 return 1;
907
908 if (!*args[3]) {
909 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100910 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +0100911 return 1;
912 }
913
914 v = atoi(args[3]);
915 if (v > global.hardmaxconn) {
916 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100917 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +0100918 return 1;
919 }
920
921 /* check for unlimited values */
922 if (v <= 0)
923 v = global.hardmaxconn;
924
925 global.maxconn = v;
926
927 /* Dequeues all of the listeners waiting for a resource */
928 if (!LIST_ISEMPTY(&global_listener_queue))
929 dequeue_all_listeners(&global_listener_queue);
930
931 return 1;
932}
933
William Lallemandeceddf72016-12-15 18:06:44 +0100934
935int cli_parse_default(char **args, struct appctx *appctx, void *private)
936{
937 return 0;
938}
939
Willy Tarreau45c742b2016-11-24 14:51:17 +0100940/* parse a "set rate-limit" command. It always returns 1. */
941static int cli_parse_set_ratelimit(char **args, struct appctx *appctx, void *private)
942{
943 int v;
944 int *res;
945 int mul = 1;
946
947 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
948 return 1;
949
950 if (strcmp(args[2], "connections") == 0 && strcmp(args[3], "global") == 0)
951 res = &global.cps_lim;
952 else if (strcmp(args[2], "sessions") == 0 && strcmp(args[3], "global") == 0)
953 res = &global.sps_lim;
954#ifdef USE_OPENSSL
955 else if (strcmp(args[2], "ssl-sessions") == 0 && strcmp(args[3], "global") == 0)
956 res = &global.ssl_lim;
957#endif
958 else if (strcmp(args[2], "http-compression") == 0 && strcmp(args[3], "global") == 0) {
959 res = &global.comp_rate_lim;
960 mul = 1024;
961 }
962 else {
963 appctx->ctx.cli.msg =
964 "'set rate-limit' only supports :\n"
965 " - 'connections global' to set the per-process maximum connection rate\n"
966 " - 'sessions global' to set the per-process maximum session rate\n"
967#ifdef USE_OPENSSL
968 " - 'ssl-session global' to set the per-process maximum SSL session rate\n"
969#endif
970 " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100971 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +0100972 return 1;
973 }
974
975 if (!*args[4]) {
976 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100977 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +0100978 return 1;
979 }
980
981 v = atoi(args[4]);
982 if (v < 0) {
983 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100984 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +0100985 return 1;
986 }
987
988 *res = v * mul;
989
990 /* Dequeues all of the listeners waiting for a resource */
991 if (!LIST_ISEMPTY(&global_listener_queue))
992 dequeue_all_listeners(&global_listener_queue);
993
994 return 1;
995}
996
William Lallemandf6975e92017-05-26 17:42:10 +0200997/* parse the "expose-fd" argument on the bind lines */
998static int bind_parse_expose_fd(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
999{
1000 if (!*args[cur_arg + 1]) {
1001 memprintf(err, "'%s' : missing fd type", args[cur_arg]);
1002 return ERR_ALERT | ERR_FATAL;
1003 }
1004 if (!strcmp(args[cur_arg+1], "listeners")) {
1005 conf->level |= ACCESS_FD_LISTENERS;
1006 } else {
1007 memprintf(err, "'%s' only supports 'listeners' (got '%s')",
1008 args[cur_arg], args[cur_arg+1]);
1009 return ERR_ALERT | ERR_FATAL;
1010 }
1011
1012 return 0;
1013}
1014
William Lallemand74c24fb2016-11-21 17:18:36 +01001015/* parse the "level" argument on the bind lines */
1016static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1017{
1018 if (!*args[cur_arg + 1]) {
1019 memprintf(err, "'%s' : missing level", args[cur_arg]);
1020 return ERR_ALERT | ERR_FATAL;
1021 }
1022
William Lallemand07a62f72017-05-24 00:57:40 +02001023 if (!strcmp(args[cur_arg+1], "user")) {
1024 conf->level &= ~ACCESS_LVL_MASK;
1025 conf->level |= ACCESS_LVL_USER;
1026 } else if (!strcmp(args[cur_arg+1], "operator")) {
1027 conf->level &= ~ACCESS_LVL_MASK;
1028 conf->level |= ACCESS_LVL_OPER;
1029 } else if (!strcmp(args[cur_arg+1], "admin")) {
1030 conf->level &= ~ACCESS_LVL_MASK;
1031 conf->level |= ACCESS_LVL_ADMIN;
1032 } else {
William Lallemand74c24fb2016-11-21 17:18:36 +01001033 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
1034 args[cur_arg], args[cur_arg+1]);
1035 return ERR_ALERT | ERR_FATAL;
1036 }
1037
1038 return 0;
1039}
1040
Olivier Houchardf886e342017-04-05 22:24:59 +02001041/* Send all the bound sockets, always returns 1 */
1042static int _getsocks(char **args, struct appctx *appctx, void *private)
1043{
1044 char *cmsgbuf = NULL;
1045 unsigned char *tmpbuf = NULL;
1046 struct cmsghdr *cmsg;
1047 struct stream_interface *si = appctx->owner;
William Lallemandf6975e92017-05-26 17:42:10 +02001048 struct stream *s = si_strm(si);
Olivier Houchardf886e342017-04-05 22:24:59 +02001049 struct connection *remote = objt_conn(si_opposite(si)->end);
1050 struct msghdr msghdr;
1051 struct iovec iov;
Olivier Houchard54740872017-04-06 14:45:14 +02001052 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Olivier Houchardf886e342017-04-05 22:24:59 +02001053 int *tmpfd;
1054 int tot_fd_nb = 0;
1055 struct proxy *px;
1056 int i = 0;
1057 int fd = remote->t.sock.fd;
1058 int curoff = 0;
1059 int old_fcntl;
1060 int ret;
1061
1062 /* Temporary set the FD in blocking mode, that will make our life easier */
1063 old_fcntl = fcntl(fd, F_GETFL);
1064 if (old_fcntl < 0) {
1065 Warning("Couldn't get the flags for the unix socket\n");
1066 goto out;
1067 }
1068 cmsgbuf = malloc(CMSG_SPACE(sizeof(int) * MAX_SEND_FD));
1069 if (!cmsgbuf) {
1070 Warning("Failed to allocate memory to send sockets\n");
1071 goto out;
1072 }
1073 if (fcntl(fd, F_SETFL, old_fcntl &~ O_NONBLOCK) == -1) {
1074 Warning("Cannot make the unix socket blocking\n");
1075 goto out;
1076 }
Olivier Houchard54740872017-04-06 14:45:14 +02001077 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf886e342017-04-05 22:24:59 +02001078 iov.iov_base = &tot_fd_nb;
1079 iov.iov_len = sizeof(tot_fd_nb);
William Lallemandf6975e92017-05-26 17:42:10 +02001080 if (!(strm_li(s)->bind_conf->level & ACCESS_FD_LISTENERS))
Olivier Houchardf886e342017-04-05 22:24:59 +02001081 goto out;
1082 memset(&msghdr, 0, sizeof(msghdr));
1083 /*
1084 * First, calculates the total number of FD, so that we can let
1085 * the caller know how much he should expects.
1086 */
1087 px = proxy;
1088 while (px) {
1089 struct listener *l;
1090
1091 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02001092 /* Only transfer IPv4/IPv6/UNIX sockets */
1093 if (l->state >= LI_ZOMBIE &&
1094 (l->proto->sock_family == AF_INET ||
Olivier Houchardf886e342017-04-05 22:24:59 +02001095 l->proto->sock_family == AF_INET6 ||
Olivier Houchard1fc05162017-04-06 01:05:05 +02001096 l->proto->sock_family == AF_UNIX))
Olivier Houchardf886e342017-04-05 22:24:59 +02001097 tot_fd_nb++;
1098 }
1099 px = px->next;
1100 }
1101 if (tot_fd_nb == 0)
1102 goto out;
1103
1104 /* First send the total number of file descriptors, so that the
1105 * receiving end knows what to expect.
1106 */
1107 msghdr.msg_iov = &iov;
1108 msghdr.msg_iovlen = 1;
1109 ret = sendmsg(fd, &msghdr, 0);
1110 if (ret != sizeof(tot_fd_nb)) {
1111 Warning("Failed to send the number of sockets to send\n");
1112 goto out;
1113 }
1114
1115 /* Now send the fds */
1116 msghdr.msg_control = cmsgbuf;
1117 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * MAX_SEND_FD);
1118 cmsg = CMSG_FIRSTHDR(&msghdr);
1119 cmsg->cmsg_len = CMSG_LEN(MAX_SEND_FD * sizeof(int));
1120 cmsg->cmsg_level = SOL_SOCKET;
1121 cmsg->cmsg_type = SCM_RIGHTS;
1122 tmpfd = (int *)CMSG_DATA(cmsg);
1123
1124 px = proxy;
1125 /* For each socket, e message is sent, containing the following :
1126 * Size of the namespace name (or 0 if none), as an unsigned char.
1127 * The namespace name, if any
1128 * Size of the interface name (or 0 if none), as an unsigned char
1129 * The interface name, if any
1130 * Listener options, as an int.
1131 */
1132 /* We will send sockets MAX_SEND_FD per MAX_SEND_FD, allocate a
1133 * buffer big enough to store the socket informations.
1134 */
1135 tmpbuf = malloc(MAX_SEND_FD * (1 + NAME_MAX + 1 + IFNAMSIZ + sizeof(int)));
1136 if (tmpbuf == NULL) {
1137 Warning("Failed to allocate memory to transfer socket informations\n");
1138 goto out;
1139 }
1140 iov.iov_base = tmpbuf;
1141 while (px) {
1142 struct listener *l;
1143
1144 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1145 int ret;
1146 /* Only transfer IPv4/IPv6 sockets */
Olivier Houchard1fc05162017-04-06 01:05:05 +02001147 if (l->state >= LI_ZOMBIE &&
Olivier Houchardf886e342017-04-05 22:24:59 +02001148 (l->proto->sock_family == AF_INET ||
1149 l->proto->sock_family == AF_INET6 ||
1150 l->proto->sock_family == AF_UNIX)) {
1151 memcpy(&tmpfd[i % MAX_SEND_FD], &l->fd, sizeof(l->fd));
1152 if (!l->netns)
1153 tmpbuf[curoff++] = 0;
1154#ifdef CONFIG_HAP_NS
1155 else {
1156 char *name = l->netns->node.key;
1157 unsigned char len = l->netns->name_len;
1158 tmpbuf[curoff++] = len;
1159 memcpy(tmpbuf + curoff, name, len);
1160 curoff += len;
1161 }
1162#endif
1163 if (l->interface) {
1164 unsigned char len = strlen(l->interface);
1165 tmpbuf[curoff++] = len;
1166 memcpy(tmpbuf + curoff, l->interface, len);
1167 curoff += len;
1168 } else
1169 tmpbuf[curoff++] = 0;
1170 memcpy(tmpbuf + curoff, &l->options,
1171 sizeof(l->options));
1172 curoff += sizeof(l->options);
1173
1174
1175 i++;
1176 } else
1177 continue;
1178 if ((!(i % MAX_SEND_FD))) {
1179 iov.iov_len = curoff;
1180 if (sendmsg(fd, &msghdr, 0) != curoff) {
1181 Warning("Failed to transfer sockets\n");
1182 printf("errno %d\n", errno);
1183 goto out;
1184 }
1185 /* Wait for an ack */
1186 do {
1187 ret = recv(fd, &tot_fd_nb,
1188 sizeof(tot_fd_nb), 0);
1189 } while (ret == -1 && errno == EINTR);
1190 if (ret <= 0) {
1191 Warning("Unexpected error while transferring sockets\n");
1192 goto out;
1193 }
1194 curoff = 0;
1195 }
1196
1197 }
1198 px = px->next;
1199 }
1200 if (i % MAX_SEND_FD) {
1201 iov.iov_len = curoff;
1202 cmsg->cmsg_len = CMSG_LEN((i % MAX_SEND_FD) * sizeof(int));
1203 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * (i % MAX_SEND_FD));
1204 if (sendmsg(fd, &msghdr, 0) != curoff) {
1205 Warning("Failed to transfer sockets\n");
1206 goto out;
1207 }
1208 }
1209
1210out:
1211 if (old_fcntl >= 0 && fcntl(fd, F_SETFL, old_fcntl) == -1) {
1212 Warning("Cannot make the unix socket non-blocking\n");
1213 goto out;
1214 }
1215 appctx->st0 = CLI_ST_END;
1216 free(cmsgbuf);
1217 free(tmpbuf);
1218 return 1;
1219}
1220
1221
1222
William Lallemand74c24fb2016-11-21 17:18:36 +01001223static struct applet cli_applet = {
1224 .obj_type = OBJ_TYPE_APPLET,
1225 .name = "<CLI>", /* used for logging */
1226 .fct = cli_io_handler,
1227 .release = cli_release_handler,
1228};
1229
Willy Tarreau0a739292016-11-22 20:21:23 +01001230/* register cli keywords */
1231static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau2af99412016-11-23 11:10:59 +01001232 { { "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 +01001233 { { "set", "rate-limit", NULL }, "set rate-limit : change a rate limiting value", cli_parse_set_ratelimit, NULL },
Willy Tarreau599852e2016-11-22 20:33:32 +01001234 { { "set", "timeout", NULL }, "set timeout : change a timeout setting", cli_parse_set_timeout, NULL, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001235 { { "show", "env", NULL }, "show env [var] : dump environment variables known to the process", cli_parse_show_env, cli_io_handler_show_env, NULL },
William Lallemandeceddf72016-12-15 18:06:44 +01001236 { { "show", "cli", "sockets", NULL }, "show cli sockets : dump list of cli sockets", cli_parse_default, cli_io_handler_show_cli_sock, NULL },
Olivier Houchardf886e342017-04-05 22:24:59 +02001237 { { "_getsocks", NULL }, NULL, _getsocks, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001238 {{},}
1239}};
1240
William Lallemand74c24fb2016-11-21 17:18:36 +01001241static struct cfg_kw_list cfg_kws = {ILH, {
1242 { CFG_GLOBAL, "stats", stats_parse_global },
1243 { 0, NULL, NULL },
1244}};
1245
1246static struct bind_kw_list bind_kws = { "STAT", { }, {
William Lallemandf6975e92017-05-26 17:42:10 +02001247 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
1248 { "expose-fd", bind_parse_expose_fd, 1 }, /* set the unix socket expose fd rights */
William Lallemand74c24fb2016-11-21 17:18:36 +01001249 { NULL, NULL, 0 },
1250}};
1251
1252__attribute__((constructor))
1253static void __dumpstats_module_init(void)
1254{
1255 cfg_register_keywords(&cfg_kws);
Willy Tarreau0a739292016-11-22 20:21:23 +01001256 cli_register_kw(&cli_kws);
William Lallemand74c24fb2016-11-21 17:18:36 +01001257 bind_register_keywords(&bind_kws);
1258}
1259
1260/*
1261 * Local variables:
1262 * c-indent-level: 8
1263 * c-basic-offset: 8
1264 * End:
1265 */