William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1 | /* |
| 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 Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 27 | #include <net/if.h> |
| 28 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 29 | #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 Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 43 | #include <types/cli.h> |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 44 | #include <types/global.h> |
| 45 | #include <types/dns.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 46 | #include <types/stats.h> |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 47 | |
| 48 | #include <proto/backend.h> |
| 49 | #include <proto/channel.h> |
| 50 | #include <proto/checks.h> |
| 51 | #include <proto/compression.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 52 | #include <proto/stats.h> |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 53 | #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 Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 61 | #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 Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 67 | #include <proto/stream_interface.h> |
| 68 | #include <proto/task.h> |
Willy Tarreau | 7a4a0ac | 2017-07-25 19:32:50 +0200 | [diff] [blame] | 69 | #include <proto/proto_udp.h> |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 70 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 71 | static struct applet cli_applet; |
| 72 | |
| 73 | static const char stats_sock_usage_msg[] = |
| 74 | "Unknown command. Please enter one of the following commands only :\n" |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 75 | " help : this message\n" |
| 76 | " prompt : toggle interactive mode with prompt\n" |
| 77 | " quit : disconnect\n" |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 78 | ""; |
| 79 | |
| 80 | static const char stats_permission_denied_msg[] = |
| 81 | "Permission denied\n" |
| 82 | ""; |
| 83 | |
| 84 | |
| 85 | static char *dynamic_usage_msg = NULL; |
| 86 | |
| 87 | /* List head of cli keywords */ |
| 88 | static struct cli_kw_list cli_keywords = { |
| 89 | .list = LIST_HEAD_INIT(cli_keywords.list) |
| 90 | }; |
| 91 | |
| 92 | extern const char *stat_status_codes[]; |
| 93 | |
| 94 | char *cli_gen_usage_msg() |
| 95 | { |
| 96 | struct cli_kw_list *kw_list; |
| 97 | struct cli_kw *kw; |
| 98 | struct chunk *tmp = get_trash_chunk(); |
| 99 | struct chunk out; |
| 100 | |
| 101 | free(dynamic_usage_msg); |
| 102 | dynamic_usage_msg = NULL; |
| 103 | |
| 104 | if (LIST_ISEMPTY(&cli_keywords.list)) |
| 105 | return NULL; |
| 106 | |
| 107 | chunk_reset(tmp); |
| 108 | chunk_strcat(tmp, stats_sock_usage_msg); |
| 109 | list_for_each_entry(kw_list, &cli_keywords.list, list) { |
| 110 | kw = &kw_list->kw[0]; |
| 111 | while (kw->usage) { |
| 112 | chunk_appendf(tmp, " %s\n", kw->usage); |
| 113 | kw++; |
| 114 | } |
| 115 | } |
| 116 | chunk_init(&out, NULL, 0); |
| 117 | chunk_dup(&out, tmp); |
| 118 | dynamic_usage_msg = out.str; |
| 119 | return dynamic_usage_msg; |
| 120 | } |
| 121 | |
| 122 | struct cli_kw* cli_find_kw(char **args) |
| 123 | { |
| 124 | struct cli_kw_list *kw_list; |
| 125 | struct cli_kw *kw;/* current cli_kw */ |
| 126 | char **tmp_args; |
| 127 | const char **tmp_str_kw; |
| 128 | int found = 0; |
| 129 | |
| 130 | if (LIST_ISEMPTY(&cli_keywords.list)) |
| 131 | return NULL; |
| 132 | |
| 133 | list_for_each_entry(kw_list, &cli_keywords.list, list) { |
| 134 | kw = &kw_list->kw[0]; |
| 135 | while (*kw->str_kw) { |
| 136 | tmp_args = args; |
| 137 | tmp_str_kw = kw->str_kw; |
| 138 | while (*tmp_str_kw) { |
| 139 | if (strcmp(*tmp_str_kw, *tmp_args) == 0) { |
| 140 | found = 1; |
| 141 | } else { |
| 142 | found = 0; |
| 143 | break; |
| 144 | } |
| 145 | tmp_args++; |
| 146 | tmp_str_kw++; |
| 147 | } |
| 148 | if (found) |
| 149 | return (kw); |
| 150 | kw++; |
| 151 | } |
| 152 | } |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | void cli_register_kw(struct cli_kw_list *kw_list) |
| 157 | { |
| 158 | LIST_ADDQ(&cli_keywords.list, &kw_list->list); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /* allocate a new stats frontend named <name>, and return it |
| 163 | * (or NULL in case of lack of memory). |
| 164 | */ |
| 165 | static struct proxy *alloc_stats_fe(const char *name, const char *file, int line) |
| 166 | { |
| 167 | struct proxy *fe; |
| 168 | |
| 169 | fe = calloc(1, sizeof(*fe)); |
| 170 | if (!fe) |
| 171 | return NULL; |
| 172 | |
| 173 | init_new_proxy(fe); |
| 174 | fe->next = proxy; |
| 175 | proxy = fe; |
| 176 | fe->last_change = now.tv_sec; |
| 177 | fe->id = strdup("GLOBAL"); |
| 178 | fe->cap = PR_CAP_FE; |
| 179 | fe->maxconn = 10; /* default to 10 concurrent connections */ |
| 180 | fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */ |
| 181 | fe->conf.file = strdup(file); |
| 182 | fe->conf.line = line; |
| 183 | fe->accept = frontend_accept; |
| 184 | fe->default_target = &cli_applet.obj_type; |
| 185 | |
| 186 | /* the stats frontend is the only one able to assign ID #0 */ |
| 187 | fe->conf.id.key = fe->uuid = 0; |
| 188 | eb32_insert(&used_proxy_id, &fe->conf.id); |
| 189 | return fe; |
| 190 | } |
| 191 | |
| 192 | /* This function parses a "stats" statement in the "global" section. It returns |
| 193 | * -1 if there is any error, otherwise zero. If it returns -1, it will write an |
| 194 | * error message into the <err> buffer which will be preallocated. The trailing |
| 195 | * '\n' must not be written. The function must be called with <args> pointing to |
| 196 | * the first word after "stats". |
| 197 | */ |
| 198 | static int stats_parse_global(char **args, int section_type, struct proxy *curpx, |
| 199 | struct proxy *defpx, const char *file, int line, |
| 200 | char **err) |
| 201 | { |
| 202 | struct bind_conf *bind_conf; |
| 203 | struct listener *l; |
| 204 | |
| 205 | if (!strcmp(args[1], "socket")) { |
| 206 | int cur_arg; |
| 207 | |
| 208 | if (*args[2] == 0) { |
| 209 | memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]); |
| 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | if (!global.stats_fe) { |
| 214 | if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) { |
| 215 | memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]); |
| 216 | return -1; |
| 217 | } |
| 218 | } |
| 219 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 220 | bind_conf = bind_conf_alloc(global.stats_fe, file, line, args[2], xprt_get(XPRT_RAW)); |
William Lallemand | 07a62f7 | 2017-05-24 00:57:40 +0200 | [diff] [blame] | 221 | bind_conf->level &= ~ACCESS_LVL_MASK; |
| 222 | bind_conf->level |= ACCESS_LVL_OPER; /* default access level */ |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 223 | |
| 224 | if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) { |
| 225 | memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n", |
| 226 | file, line, args[0], args[1], err && *err ? *err : "error"); |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | cur_arg = 3; |
| 231 | while (*args[cur_arg]) { |
| 232 | static int bind_dumped; |
| 233 | struct bind_kw *kw; |
| 234 | |
| 235 | kw = bind_find_kw(args[cur_arg]); |
| 236 | if (kw) { |
| 237 | if (!kw->parse) { |
| 238 | memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).", |
| 239 | args[0], args[1], args[cur_arg]); |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) { |
| 244 | if (err && *err) |
| 245 | memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err); |
| 246 | else |
| 247 | memprintf(err, "'%s %s' : error encountered while processing '%s'", |
| 248 | args[0], args[1], args[cur_arg]); |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | cur_arg += 1 + kw->skip; |
| 253 | continue; |
| 254 | } |
| 255 | |
| 256 | if (!bind_dumped) { |
| 257 | bind_dump_kws(err); |
| 258 | indent_msg(err, 4); |
| 259 | bind_dumped = 1; |
| 260 | } |
| 261 | |
| 262 | memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s", |
| 263 | args[0], args[1], args[cur_arg], |
| 264 | err && *err ? " Registered keywords :" : "", err && *err ? *err : ""); |
| 265 | return -1; |
| 266 | } |
| 267 | |
| 268 | list_for_each_entry(l, &bind_conf->listeners, by_bind) { |
| 269 | l->maxconn = global.stats_fe->maxconn; |
| 270 | l->backlog = global.stats_fe->backlog; |
| 271 | l->accept = session_accept_fd; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 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 Tarreau | de57a57 | 2016-11-23 17:01:39 +0100 | [diff] [blame] | 377 | /* 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 | */ |
| 382 | int 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 Lallemand | 07a62f7 | 2017-05-24 00:57:40 +0200 | [diff] [blame] | 387 | if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < level) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 388 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | de57a57 | 2016-11-23 17:01:39 +0100 | [diff] [blame] | 389 | appctx->ctx.cli.msg = stats_permission_denied_msg; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 390 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | de57a57 | 2016-11-23 17:01:39 +0100 | [diff] [blame] | 391 | return 0; |
| 392 | } |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 393 | return 1; |
| 394 | } |
| 395 | |
Andjelko Iharos | c4df59e | 2017-07-20 11:59:48 +0200 | [diff] [blame] | 396 | /* Returns severity_output for the current session if set, or default for the socket */ |
| 397 | static int cli_get_severity_output(struct appctx *appctx) |
| 398 | { |
| 399 | if (appctx->cli_severity_output) |
| 400 | return appctx->cli_severity_output; |
| 401 | return strm_li(si_strm(appctx->owner))->bind_conf->severity_output; |
| 402 | } |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 403 | |
Willy Tarreau | 4190856 | 2016-11-24 16:23:38 +0100 | [diff] [blame] | 404 | /* Processes the CLI interpreter on the stats socket. This function is called |
| 405 | * from the CLI's IO handler running in an appctx context. The function returns 1 |
| 406 | * if the request was understood, otherwise zero. It is called with appctx->st0 |
| 407 | * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do |
| 408 | * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to |
| 409 | * have its own I/O handler called again. Most of the time, parsers will only |
| 410 | * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg. |
Willy Tarreau | eaffde3 | 2016-12-16 17:59:25 +0100 | [diff] [blame] | 411 | * If a keyword parser is NULL and an I/O handler is declared, the I/O handler |
| 412 | * will automatically be used. |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 413 | */ |
Willy Tarreau | 4190856 | 2016-11-24 16:23:38 +0100 | [diff] [blame] | 414 | static int cli_parse_request(struct appctx *appctx, char *line) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 415 | { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 416 | char *args[MAX_STATS_ARGS + 1]; |
| 417 | struct cli_kw *kw; |
| 418 | int arg; |
| 419 | int i, j; |
| 420 | |
| 421 | while (isspace((unsigned char)*line)) |
| 422 | line++; |
| 423 | |
| 424 | arg = 0; |
| 425 | args[arg] = line; |
| 426 | |
| 427 | while (*line && arg < MAX_STATS_ARGS) { |
| 428 | if (*line == '\\') { |
| 429 | line++; |
| 430 | if (*line == '\0') |
| 431 | break; |
| 432 | } |
| 433 | else if (isspace((unsigned char)*line)) { |
| 434 | *line++ = '\0'; |
| 435 | |
| 436 | while (isspace((unsigned char)*line)) |
| 437 | line++; |
| 438 | |
| 439 | args[++arg] = line; |
| 440 | continue; |
| 441 | } |
| 442 | |
| 443 | line++; |
| 444 | } |
| 445 | |
| 446 | while (++arg <= MAX_STATS_ARGS) |
| 447 | args[arg] = line; |
| 448 | |
Dragan Dosen | a1c35ab | 2016-11-24 11:33:12 +0100 | [diff] [blame] | 449 | /* unescape '\' */ |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 450 | arg = 0; |
| 451 | while (*args[arg] != '\0') { |
| 452 | j = 0; |
| 453 | for (i=0; args[arg][i] != '\0'; i++) { |
Dragan Dosen | a1c35ab | 2016-11-24 11:33:12 +0100 | [diff] [blame] | 454 | if (args[arg][i] == '\\') { |
| 455 | if (args[arg][i+1] == '\\') |
| 456 | i++; |
| 457 | else |
| 458 | continue; |
| 459 | } |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 460 | args[arg][j] = args[arg][i]; |
| 461 | j++; |
| 462 | } |
| 463 | args[arg][j] = '\0'; |
| 464 | arg++; |
| 465 | } |
| 466 | |
Willy Tarreau | 4190856 | 2016-11-24 16:23:38 +0100 | [diff] [blame] | 467 | appctx->st2 = 0; |
Willy Tarreau | a2d5872 | 2016-12-16 12:37:03 +0100 | [diff] [blame] | 468 | memset(&appctx->ctx.cli, 0, sizeof(appctx->ctx.cli)); |
Willy Tarreau | 4190856 | 2016-11-24 16:23:38 +0100 | [diff] [blame] | 469 | |
| 470 | kw = cli_find_kw(args); |
Willy Tarreau | eaffde3 | 2016-12-16 17:59:25 +0100 | [diff] [blame] | 471 | if (!kw) |
Willy Tarreau | 4190856 | 2016-11-24 16:23:38 +0100 | [diff] [blame] | 472 | return 0; |
| 473 | |
| 474 | appctx->io_handler = kw->io_handler; |
Emeric Brun | d6871f7 | 2017-06-29 19:54:13 +0200 | [diff] [blame] | 475 | appctx->io_release = kw->io_release; |
| 476 | /* kw->parse could set its own io_handler or ip_release handler */ |
Willy Tarreau | eaffde3 | 2016-12-16 17:59:25 +0100 | [diff] [blame] | 477 | if ((!kw->parse || kw->parse(args, appctx, kw->private) == 0) && appctx->io_handler) { |
Willy Tarreau | 4190856 | 2016-11-24 16:23:38 +0100 | [diff] [blame] | 478 | appctx->st0 = CLI_ST_CALLBACK; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 479 | } |
Willy Tarreau | 4190856 | 2016-11-24 16:23:38 +0100 | [diff] [blame] | 480 | return 1; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 481 | } |
| 482 | |
Andjelko Iharos | c4df59e | 2017-07-20 11:59:48 +0200 | [diff] [blame] | 483 | /* prepends then outputs the argument msg with a syslog-type severity depending on severity_output value */ |
| 484 | static int cli_output_msg(struct channel *chn, const char *msg, int severity, int severity_output) |
| 485 | { |
| 486 | struct chunk *tmp; |
| 487 | |
| 488 | if (likely(severity_output == CLI_SEVERITY_NONE)) |
| 489 | return bi_putblk(chn, msg, strlen(msg)); |
| 490 | |
| 491 | tmp = get_trash_chunk(); |
| 492 | chunk_reset(tmp); |
| 493 | |
| 494 | if (severity < 0 || severity > 7) { |
| 495 | Warning("socket command feedback with invalid severity %d", severity); |
| 496 | chunk_printf(tmp, "[%d]: ", severity); |
| 497 | } |
| 498 | else { |
| 499 | switch (severity_output) { |
| 500 | case CLI_SEVERITY_NUMBER: |
| 501 | chunk_printf(tmp, "[%d]: ", severity); |
| 502 | break; |
| 503 | case CLI_SEVERITY_STRING: |
| 504 | chunk_printf(tmp, "[%s]: ", log_levels[severity]); |
| 505 | break; |
| 506 | default: |
| 507 | Warning("Unrecognized severity output %d", severity_output); |
| 508 | } |
| 509 | } |
| 510 | chunk_appendf(tmp, "%s", msg); |
| 511 | |
| 512 | return bi_putblk(chn, tmp->str, strlen(tmp->str)); |
| 513 | } |
| 514 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 515 | /* This I/O handler runs as an applet embedded in a stream interface. It is |
| 516 | * used to processes I/O from/to the stats unix socket. The system relies on a |
| 517 | * state machine handling requests and various responses. We read a request, |
| 518 | * then we process it and send the response, and we possibly display a prompt. |
| 519 | * Then we can read again. The state is stored in appctx->st0 and is one of the |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 520 | * CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 521 | * or not. |
| 522 | */ |
| 523 | static void cli_io_handler(struct appctx *appctx) |
| 524 | { |
| 525 | struct stream_interface *si = appctx->owner; |
| 526 | struct channel *req = si_oc(si); |
| 527 | struct channel *res = si_ic(si); |
Andjelko Iharos | c4df59e | 2017-07-20 11:59:48 +0200 | [diff] [blame] | 528 | struct bind_conf *bind_conf = strm_li(si_strm(si))->bind_conf; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 529 | int reql; |
| 530 | int len; |
| 531 | |
| 532 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 533 | goto out; |
| 534 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 535 | /* Check if the input buffer is avalaible. */ |
| 536 | if (res->buf->size == 0) { |
| 537 | si_applet_cant_put(si); |
| 538 | goto out; |
| 539 | } |
| 540 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 541 | while (1) { |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 542 | if (appctx->st0 == CLI_ST_INIT) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 543 | /* Stats output not initialized yet */ |
| 544 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
Andjelko Iharos | c4df59e | 2017-07-20 11:59:48 +0200 | [diff] [blame] | 545 | /* reset severity to default at init */ |
| 546 | appctx->cli_severity_output = bind_conf->severity_output; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 547 | appctx->st0 = CLI_ST_GETREQ; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 548 | } |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 549 | else if (appctx->st0 == CLI_ST_END) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 550 | /* Let's close for real now. We just close the request |
| 551 | * side, the conditions below will complete if needed. |
| 552 | */ |
| 553 | si_shutw(si); |
| 554 | break; |
| 555 | } |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 556 | else if (appctx->st0 == CLI_ST_GETREQ) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 557 | /* ensure we have some output room left in the event we |
| 558 | * would want to return some info right after parsing. |
| 559 | */ |
| 560 | if (buffer_almost_full(si_ib(si))) { |
| 561 | si_applet_cant_put(si); |
| 562 | break; |
| 563 | } |
| 564 | |
| 565 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
| 566 | if (reql <= 0) { /* closed or EOL not found */ |
| 567 | if (reql == 0) |
| 568 | break; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 569 | appctx->st0 = CLI_ST_END; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 570 | continue; |
| 571 | } |
| 572 | |
| 573 | /* seek for a possible unescaped semi-colon. If we find |
| 574 | * one, we replace it with an LF and skip only this part. |
| 575 | */ |
| 576 | for (len = 0; len < reql; len++) { |
| 577 | if (trash.str[len] == '\\') { |
| 578 | len++; |
| 579 | continue; |
| 580 | } |
| 581 | if (trash.str[len] == ';') { |
| 582 | trash.str[len] = '\n'; |
| 583 | reql = len + 1; |
| 584 | break; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /* now it is time to check that we have a full line, |
| 589 | * remove the trailing \n and possibly \r, then cut the |
| 590 | * line. |
| 591 | */ |
| 592 | len = reql - 1; |
| 593 | if (trash.str[len] != '\n') { |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 594 | appctx->st0 = CLI_ST_END; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 595 | continue; |
| 596 | } |
| 597 | |
| 598 | if (len && trash.str[len-1] == '\r') |
| 599 | len--; |
| 600 | |
| 601 | trash.str[len] = '\0'; |
| 602 | |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 603 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 604 | if (len) { |
| 605 | if (strcmp(trash.str, "quit") == 0) { |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 606 | appctx->st0 = CLI_ST_END; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 607 | continue; |
| 608 | } |
| 609 | else if (strcmp(trash.str, "prompt") == 0) |
| 610 | appctx->st1 = !appctx->st1; |
| 611 | else if (strcmp(trash.str, "help") == 0 || |
Willy Tarreau | 4190856 | 2016-11-24 16:23:38 +0100 | [diff] [blame] | 612 | !cli_parse_request(appctx, trash.str)) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 613 | cli_gen_usage_msg(); |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 614 | if (dynamic_usage_msg) { |
| 615 | appctx->ctx.cli.severity = LOG_INFO; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 616 | appctx->ctx.cli.msg = dynamic_usage_msg; |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 617 | } |
| 618 | else { |
| 619 | appctx->ctx.cli.severity = LOG_INFO; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 620 | appctx->ctx.cli.msg = stats_sock_usage_msg; |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 621 | } |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 622 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 623 | } |
| 624 | /* NB: stats_sock_parse_request() may have put |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 625 | * another CLI_ST_O_* into appctx->st0. |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 626 | */ |
| 627 | } |
| 628 | else if (!appctx->st1) { |
| 629 | /* if prompt is disabled, print help on empty lines, |
| 630 | * so that the user at least knows how to enable |
| 631 | * prompt and find help. |
| 632 | */ |
| 633 | cli_gen_usage_msg(); |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 634 | if (dynamic_usage_msg) { |
| 635 | appctx->ctx.cli.severity = LOG_INFO; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 636 | appctx->ctx.cli.msg = dynamic_usage_msg; |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 637 | } |
| 638 | else { |
| 639 | appctx->ctx.cli.severity = LOG_INFO; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 640 | appctx->ctx.cli.msg = stats_sock_usage_msg; |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 641 | } |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 642 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /* re-adjust req buffer */ |
| 646 | bo_skip(si_oc(si), reql); |
| 647 | req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */ |
| 648 | } |
| 649 | else { /* output functions */ |
| 650 | switch (appctx->st0) { |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 651 | case CLI_ST_PROMPT: |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 652 | break; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 653 | case CLI_ST_PRINT: |
Andjelko Iharos | c4df59e | 2017-07-20 11:59:48 +0200 | [diff] [blame] | 654 | if (cli_output_msg(res, appctx->ctx.cli.msg, appctx->ctx.cli.severity, |
| 655 | cli_get_severity_output(appctx)) != -1) |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 656 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 657 | else |
| 658 | si_applet_cant_put(si); |
| 659 | break; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 660 | case CLI_ST_PRINT_FREE: |
Andjelko Iharos | c4df59e | 2017-07-20 11:59:48 +0200 | [diff] [blame] | 661 | if (cli_output_msg(res, appctx->ctx.cli.err, LOG_ERR, cli_get_severity_output(appctx)) != -1) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 662 | free(appctx->ctx.cli.err); |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 663 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 664 | } |
| 665 | else |
| 666 | si_applet_cant_put(si); |
| 667 | break; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 668 | case CLI_ST_CALLBACK: /* use custom pointer */ |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 669 | if (appctx->io_handler) |
| 670 | if (appctx->io_handler(appctx)) { |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 671 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 672 | if (appctx->io_release) { |
| 673 | appctx->io_release(appctx); |
| 674 | appctx->io_release = NULL; |
| 675 | } |
| 676 | } |
| 677 | break; |
| 678 | default: /* abnormal state */ |
| 679 | si->flags |= SI_FL_ERR; |
| 680 | break; |
| 681 | } |
| 682 | |
| 683 | /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 684 | if (appctx->st0 == CLI_ST_PROMPT) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 685 | if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1) |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 686 | appctx->st0 = CLI_ST_GETREQ; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 687 | else |
| 688 | si_applet_cant_put(si); |
| 689 | } |
| 690 | |
| 691 | /* If the output functions are still there, it means they require more room. */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 692 | if (appctx->st0 >= CLI_ST_OUTPUT) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 693 | break; |
| 694 | |
| 695 | /* Now we close the output if one of the writers did so, |
| 696 | * or if we're not in interactive mode and the request |
| 697 | * buffer is empty. This still allows pipelined requests |
| 698 | * to be sent in non-interactive mode. |
| 699 | */ |
| 700 | if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) { |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 701 | appctx->st0 = CLI_ST_END; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 702 | continue; |
| 703 | } |
| 704 | |
| 705 | /* switch state back to GETREQ to read next requests */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 706 | appctx->st0 = CLI_ST_GETREQ; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) { |
| 711 | DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n", |
| 712 | __FUNCTION__, __LINE__, req->flags, res->flags, si->state); |
| 713 | /* Other side has closed, let's abort if we have no more processing to do |
| 714 | * and nothing more to consume. This is comparable to a broken pipe, so |
| 715 | * we forward the close to the request side so that it flows upstream to |
| 716 | * the client. |
| 717 | */ |
| 718 | si_shutw(si); |
| 719 | } |
| 720 | |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 721 | if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < CLI_ST_OUTPUT)) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 722 | DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n", |
| 723 | __FUNCTION__, __LINE__, req->flags, res->flags, si->state); |
| 724 | /* We have no more processing to do, and nothing more to send, and |
| 725 | * the client side has closed. So we'll forward this state downstream |
| 726 | * on the response buffer. |
| 727 | */ |
| 728 | si_shutr(si); |
| 729 | res->flags |= CF_READ_NULL; |
| 730 | } |
| 731 | |
| 732 | out: |
| 733 | DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n", |
| 734 | __FUNCTION__, __LINE__, |
| 735 | si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o); |
| 736 | } |
| 737 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 738 | /* This is called when the stream interface is closed. For instance, upon an |
| 739 | * external abort, we won't call the i/o handler anymore so we may need to |
| 740 | * remove back references to the stream currently being dumped. |
| 741 | */ |
| 742 | static void cli_release_handler(struct appctx *appctx) |
| 743 | { |
| 744 | if (appctx->io_release) { |
| 745 | appctx->io_release(appctx); |
| 746 | appctx->io_release = NULL; |
| 747 | } |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 748 | else if (appctx->st0 == CLI_ST_PRINT_FREE) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 749 | free(appctx->ctx.cli.err); |
| 750 | appctx->ctx.cli.err = NULL; |
| 751 | } |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /* This function dumps all environmnent variables to the buffer. It returns 0 |
| 755 | * if the output buffer is full and it needs to be called again, otherwise |
Willy Tarreau | f6710f8 | 2016-12-16 17:45:44 +0100 | [diff] [blame] | 756 | * non-zero. Dumps only one entry if st2 == STAT_ST_END. It uses cli.p0 as the |
| 757 | * pointer to the current variable. |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 758 | */ |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 759 | static int cli_io_handler_show_env(struct appctx *appctx) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 760 | { |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 761 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | f6710f8 | 2016-12-16 17:45:44 +0100 | [diff] [blame] | 762 | char **var = appctx->ctx.cli.p0; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 763 | |
| 764 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 765 | return 1; |
| 766 | |
| 767 | chunk_reset(&trash); |
| 768 | |
| 769 | /* we have two inner loops here, one for the proxy, the other one for |
| 770 | * the buffer. |
| 771 | */ |
Willy Tarreau | f6710f8 | 2016-12-16 17:45:44 +0100 | [diff] [blame] | 772 | while (*var) { |
| 773 | chunk_printf(&trash, "%s\n", *var); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 774 | |
| 775 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 776 | si_applet_cant_put(si); |
| 777 | return 0; |
| 778 | } |
| 779 | if (appctx->st2 == STAT_ST_END) |
| 780 | break; |
Willy Tarreau | f6710f8 | 2016-12-16 17:45:44 +0100 | [diff] [blame] | 781 | var++; |
| 782 | appctx->ctx.cli.p0 = var; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /* dump complete */ |
| 786 | return 1; |
| 787 | } |
| 788 | |
Willy Tarreau | 7a4a0ac | 2017-07-25 19:32:50 +0200 | [diff] [blame] | 789 | /* This function dumps all file descriptors states (or the requested one) to |
| 790 | * the buffer. It returns 0 if the output buffer is full and it needs to be |
| 791 | * called again, otherwise non-zero. Dumps only one entry if st2 == STAT_ST_END. |
| 792 | * It uses cli.i0 as the fd number to restart from. |
| 793 | */ |
| 794 | static int cli_io_handler_show_fd(struct appctx *appctx) |
| 795 | { |
| 796 | struct stream_interface *si = appctx->owner; |
| 797 | int fd = appctx->ctx.cli.i0; |
| 798 | |
| 799 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 800 | return 1; |
| 801 | |
| 802 | chunk_reset(&trash); |
| 803 | |
| 804 | /* we have two inner loops here, one for the proxy, the other one for |
| 805 | * the buffer. |
| 806 | */ |
| 807 | while (fd < maxfd) { |
| 808 | struct fdtab fdt; |
Willy Tarreau | 286ec68 | 2017-08-09 16:35:44 +0200 | [diff] [blame] | 809 | struct listener *li = NULL; |
| 810 | struct server *sv = NULL; |
| 811 | struct proxy *px = NULL; |
| 812 | uint32_t conn_flags = 0; |
Willy Tarreau | 7a4a0ac | 2017-07-25 19:32:50 +0200 | [diff] [blame] | 813 | |
| 814 | fdt = fdtab[fd]; |
| 815 | |
| 816 | if (fdt.iocb == conn_fd_handler) { |
| 817 | conn_flags = ((struct connection *)fdt.owner)->flags; |
| 818 | li = objt_listener(((struct connection *)fdt.owner)->target); |
| 819 | sv = objt_server(((struct connection *)fdt.owner)->target); |
| 820 | px = objt_proxy(((struct connection *)fdt.owner)->target); |
| 821 | } |
| 822 | else if (fdt.iocb == listener_accept) |
| 823 | li = fdt.owner; |
| 824 | |
| 825 | if (!fdt.owner) |
| 826 | goto skip; // closed |
| 827 | |
| 828 | chunk_printf(&trash, |
| 829 | " %5d : st=0x%02x(R:%c%c%c W:%c%c%c) ev=0x%02x(%c%c%c%c%c) [%c%c%c%c] cache=%u owner=%p iocb=%p(%s)", |
| 830 | fd, |
| 831 | fdt.state, |
| 832 | (fdt.state & FD_EV_POLLED_R) ? 'P' : 'p', |
| 833 | (fdt.state & FD_EV_READY_R) ? 'R' : 'r', |
| 834 | (fdt.state & FD_EV_ACTIVE_R) ? 'A' : 'a', |
| 835 | (fdt.state & FD_EV_POLLED_W) ? 'P' : 'p', |
| 836 | (fdt.state & FD_EV_READY_W) ? 'R' : 'r', |
| 837 | (fdt.state & FD_EV_ACTIVE_W) ? 'A' : 'a', |
| 838 | fdt.ev, |
| 839 | (fdt.ev & FD_POLL_HUP) ? 'H' : 'h', |
| 840 | (fdt.ev & FD_POLL_ERR) ? 'E' : 'e', |
| 841 | (fdt.ev & FD_POLL_OUT) ? 'O' : 'o', |
| 842 | (fdt.ev & FD_POLL_PRI) ? 'P' : 'p', |
| 843 | (fdt.ev & FD_POLL_IN) ? 'I' : 'i', |
| 844 | fdt.new ? 'N' : 'n', |
| 845 | fdt.updated ? 'U' : 'u', |
| 846 | fdt.linger_risk ? 'L' : 'l', |
| 847 | fdt.cloned ? 'C' : 'c', |
| 848 | fdt.cache, |
| 849 | fdt.owner, |
| 850 | fdt.iocb, |
| 851 | (fdt.iocb == conn_fd_handler) ? "conn_fd_handler" : |
| 852 | (fdt.iocb == dgram_fd_handler) ? "dgram_fd_handler" : |
| 853 | (fdt.iocb == listener_accept) ? "listener_accept" : |
| 854 | "unknown"); |
| 855 | |
| 856 | if (fdt.iocb == conn_fd_handler) { |
| 857 | chunk_appendf(&trash, " cflg=0x%08x", conn_flags); |
| 858 | if (px) |
| 859 | chunk_appendf(&trash, " px=%s", px->id); |
| 860 | else if (sv) |
| 861 | chunk_appendf(&trash, " sv=%s/%s", sv->id, sv->proxy->id); |
| 862 | else if (li) |
| 863 | chunk_appendf(&trash, " fe=%s", li->bind_conf->frontend->id); |
| 864 | } |
| 865 | else if (fdt.iocb == listener_accept) { |
| 866 | chunk_appendf(&trash, " l.st=%s fe=%s", |
| 867 | listener_state_str(li), |
| 868 | li->bind_conf->frontend->id); |
| 869 | } |
| 870 | |
| 871 | chunk_appendf(&trash, "\n"); |
| 872 | |
| 873 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 874 | si_applet_cant_put(si); |
| 875 | return 0; |
| 876 | } |
| 877 | skip: |
| 878 | if (appctx->st2 == STAT_ST_END) |
| 879 | break; |
| 880 | |
| 881 | fd++; |
| 882 | appctx->ctx.cli.i0 = fd; |
| 883 | } |
| 884 | |
| 885 | /* dump complete */ |
| 886 | return 1; |
| 887 | } |
| 888 | |
William Lallemand | eceddf7 | 2016-12-15 18:06:44 +0100 | [diff] [blame] | 889 | /* |
Willy Tarreau | 3af9d83 | 2016-12-16 12:58:09 +0100 | [diff] [blame] | 890 | * CLI IO handler for `show cli sockets`. |
| 891 | * Uses ctx.cli.p0 to store the restart pointer. |
William Lallemand | eceddf7 | 2016-12-15 18:06:44 +0100 | [diff] [blame] | 892 | */ |
| 893 | static int cli_io_handler_show_cli_sock(struct appctx *appctx) |
| 894 | { |
| 895 | struct bind_conf *bind_conf; |
| 896 | struct stream_interface *si = appctx->owner; |
| 897 | |
| 898 | chunk_reset(&trash); |
| 899 | |
| 900 | switch (appctx->st2) { |
| 901 | case STAT_ST_INIT: |
| 902 | chunk_printf(&trash, "# socket lvl processes\n"); |
| 903 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 904 | si_applet_cant_put(si); |
| 905 | return 0; |
| 906 | } |
William Lallemand | eceddf7 | 2016-12-15 18:06:44 +0100 | [diff] [blame] | 907 | appctx->st2 = STAT_ST_LIST; |
| 908 | |
| 909 | case STAT_ST_LIST: |
| 910 | if (global.stats_fe) { |
| 911 | list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) { |
| 912 | struct listener *l; |
| 913 | |
| 914 | /* |
Willy Tarreau | 3af9d83 | 2016-12-16 12:58:09 +0100 | [diff] [blame] | 915 | * get the latest dumped node in appctx->ctx.cli.p0 |
William Lallemand | eceddf7 | 2016-12-15 18:06:44 +0100 | [diff] [blame] | 916 | * if the current node is the first of the list |
| 917 | */ |
| 918 | |
Willy Tarreau | 3af9d83 | 2016-12-16 12:58:09 +0100 | [diff] [blame] | 919 | if (appctx->ctx.cli.p0 && |
| 920 | &bind_conf->by_fe == (&global.stats_fe->conf.bind)->n) { |
William Lallemand | eceddf7 | 2016-12-15 18:06:44 +0100 | [diff] [blame] | 921 | /* change the current node to the latest dumped and continue the loop */ |
Willy Tarreau | 3af9d83 | 2016-12-16 12:58:09 +0100 | [diff] [blame] | 922 | bind_conf = LIST_ELEM(appctx->ctx.cli.p0, typeof(bind_conf), by_fe); |
William Lallemand | eceddf7 | 2016-12-15 18:06:44 +0100 | [diff] [blame] | 923 | continue; |
| 924 | } |
| 925 | |
| 926 | list_for_each_entry(l, &bind_conf->listeners, by_bind) { |
| 927 | |
| 928 | char addr[46]; |
| 929 | char port[6]; |
| 930 | |
| 931 | if (l->addr.ss_family == AF_UNIX) { |
| 932 | const struct sockaddr_un *un; |
| 933 | |
| 934 | un = (struct sockaddr_un *)&l->addr; |
| 935 | chunk_appendf(&trash, "%s ", un->sun_path); |
| 936 | } else if (l->addr.ss_family == AF_INET) { |
| 937 | addr_to_str(&l->addr, addr, sizeof(addr)); |
| 938 | port_to_str(&l->addr, port, sizeof(port)); |
| 939 | chunk_appendf(&trash, "%s:%s ", addr, port); |
| 940 | } else if (l->addr.ss_family == AF_INET6) { |
| 941 | addr_to_str(&l->addr, addr, sizeof(addr)); |
| 942 | port_to_str(&l->addr, port, sizeof(port)); |
| 943 | chunk_appendf(&trash, "[%s]:%s ", addr, port); |
| 944 | } else |
| 945 | continue; |
| 946 | |
William Lallemand | 07a62f7 | 2017-05-24 00:57:40 +0200 | [diff] [blame] | 947 | if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN) |
William Lallemand | eceddf7 | 2016-12-15 18:06:44 +0100 | [diff] [blame] | 948 | chunk_appendf(&trash, "admin "); |
William Lallemand | 07a62f7 | 2017-05-24 00:57:40 +0200 | [diff] [blame] | 949 | else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER) |
| 950 | chunk_appendf(&trash, "operator "); |
| 951 | else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_USER) |
| 952 | chunk_appendf(&trash, "user "); |
William Lallemand | eceddf7 | 2016-12-15 18:06:44 +0100 | [diff] [blame] | 953 | else |
| 954 | chunk_appendf(&trash, " "); |
| 955 | |
| 956 | if (bind_conf->bind_proc != 0) { |
| 957 | int pos; |
Willy Tarreau | 20c5e52 | 2016-12-16 12:50:55 +0100 | [diff] [blame] | 958 | for (pos = 0; pos < 8 * sizeof(bind_conf->bind_proc); pos++) { |
Willy Tarreau | 4305ac7 | 2016-12-16 12:56:31 +0100 | [diff] [blame] | 959 | if (bind_conf->bind_proc & (1UL << pos)) { |
William Lallemand | eceddf7 | 2016-12-15 18:06:44 +0100 | [diff] [blame] | 960 | chunk_appendf(&trash, "%d,", pos+1); |
| 961 | } |
| 962 | } |
| 963 | /* replace the latest comma by a newline */ |
| 964 | trash.str[trash.len-1] = '\n'; |
| 965 | |
| 966 | } else { |
| 967 | chunk_appendf(&trash, "all\n"); |
| 968 | } |
| 969 | |
| 970 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 971 | si_applet_cant_put(si); |
| 972 | return 0; |
| 973 | } |
| 974 | } |
Willy Tarreau | 3af9d83 | 2016-12-16 12:58:09 +0100 | [diff] [blame] | 975 | appctx->ctx.cli.p0 = &bind_conf->by_fe; /* store the latest list node dumped */ |
William Lallemand | eceddf7 | 2016-12-15 18:06:44 +0100 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | default: |
| 979 | appctx->st2 = STAT_ST_FIN; |
| 980 | return 1; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 985 | /* parse a "show env" CLI request. Returns 0 if it needs to continue, 1 if it |
Willy Tarreau | f6710f8 | 2016-12-16 17:45:44 +0100 | [diff] [blame] | 986 | * wants to stop here. It puts the variable to be dumped into cli.p0 if a single |
| 987 | * variable is requested otherwise puts environ there. |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 988 | */ |
| 989 | static int cli_parse_show_env(char **args, struct appctx *appctx, void *private) |
| 990 | { |
| 991 | extern char **environ; |
Willy Tarreau | f6710f8 | 2016-12-16 17:45:44 +0100 | [diff] [blame] | 992 | char **var; |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 993 | |
| 994 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 995 | return 1; |
| 996 | |
Willy Tarreau | f6710f8 | 2016-12-16 17:45:44 +0100 | [diff] [blame] | 997 | var = environ; |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 998 | |
| 999 | if (*args[2]) { |
| 1000 | int len = strlen(args[2]); |
| 1001 | |
Willy Tarreau | f6710f8 | 2016-12-16 17:45:44 +0100 | [diff] [blame] | 1002 | for (; *var; var++) { |
| 1003 | if (strncmp(*var, args[2], len) == 0 && |
| 1004 | (*var)[len] == '=') |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 1005 | break; |
| 1006 | } |
Willy Tarreau | f6710f8 | 2016-12-16 17:45:44 +0100 | [diff] [blame] | 1007 | if (!*var) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1008 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 1009 | appctx->ctx.cli.msg = "Variable not found\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1010 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 1011 | return 1; |
| 1012 | } |
| 1013 | appctx->st2 = STAT_ST_END; |
| 1014 | } |
Willy Tarreau | f6710f8 | 2016-12-16 17:45:44 +0100 | [diff] [blame] | 1015 | appctx->ctx.cli.p0 = var; |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 1016 | return 0; |
| 1017 | } |
| 1018 | |
Willy Tarreau | 7a4a0ac | 2017-07-25 19:32:50 +0200 | [diff] [blame] | 1019 | /* parse a "show fd" CLI request. Returns 0 if it needs to continue, 1 if it |
| 1020 | * wants to stop here. It puts the FD number into cli.i0 if a specific FD is |
| 1021 | * requested and sets st2 to STAT_ST_END, otherwise leaves 0 in i0. |
| 1022 | */ |
| 1023 | static int cli_parse_show_fd(char **args, struct appctx *appctx, void *private) |
| 1024 | { |
| 1025 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 1026 | return 1; |
| 1027 | |
| 1028 | appctx->ctx.cli.i0 = 0; |
| 1029 | |
| 1030 | if (*args[2]) { |
| 1031 | appctx->ctx.cli.i0 = atoi(args[2]); |
| 1032 | appctx->st2 = STAT_ST_END; |
| 1033 | } |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
Willy Tarreau | 599852e | 2016-11-22 20:33:32 +0100 | [diff] [blame] | 1037 | /* parse a "set timeout" CLI request. It always returns 1. */ |
| 1038 | static int cli_parse_set_timeout(char **args, struct appctx *appctx, void *private) |
| 1039 | { |
| 1040 | struct stream_interface *si = appctx->owner; |
| 1041 | struct stream *s = si_strm(si); |
| 1042 | |
| 1043 | if (strcmp(args[2], "cli") == 0) { |
| 1044 | unsigned timeout; |
| 1045 | const char *res; |
| 1046 | |
| 1047 | if (!*args[3]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1048 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 599852e | 2016-11-22 20:33:32 +0100 | [diff] [blame] | 1049 | appctx->ctx.cli.msg = "Expects an integer value.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1050 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 599852e | 2016-11-22 20:33:32 +0100 | [diff] [blame] | 1051 | return 1; |
| 1052 | } |
| 1053 | |
| 1054 | res = parse_time_err(args[3], &timeout, TIME_UNIT_S); |
| 1055 | if (res || timeout < 1) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1056 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 599852e | 2016-11-22 20:33:32 +0100 | [diff] [blame] | 1057 | appctx->ctx.cli.msg = "Invalid timeout value.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1058 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 599852e | 2016-11-22 20:33:32 +0100 | [diff] [blame] | 1059 | return 1; |
| 1060 | } |
| 1061 | |
| 1062 | s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000); |
| 1063 | task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts |
| 1064 | return 1; |
| 1065 | } |
| 1066 | else { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1067 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 599852e | 2016-11-22 20:33:32 +0100 | [diff] [blame] | 1068 | appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1069 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 599852e | 2016-11-22 20:33:32 +0100 | [diff] [blame] | 1070 | return 1; |
| 1071 | } |
| 1072 | } |
| 1073 | |
Willy Tarreau | 2af9941 | 2016-11-23 11:10:59 +0100 | [diff] [blame] | 1074 | /* parse a "set maxconn global" command. It always returns 1. */ |
| 1075 | static int cli_parse_set_maxconn_global(char **args, struct appctx *appctx, void *private) |
| 1076 | { |
| 1077 | int v; |
| 1078 | |
| 1079 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1080 | return 1; |
| 1081 | |
| 1082 | if (!*args[3]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1083 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 2af9941 | 2016-11-23 11:10:59 +0100 | [diff] [blame] | 1084 | appctx->ctx.cli.msg = "Expects an integer value.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1085 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 2af9941 | 2016-11-23 11:10:59 +0100 | [diff] [blame] | 1086 | return 1; |
| 1087 | } |
| 1088 | |
| 1089 | v = atoi(args[3]); |
| 1090 | if (v > global.hardmaxconn) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1091 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 2af9941 | 2016-11-23 11:10:59 +0100 | [diff] [blame] | 1092 | appctx->ctx.cli.msg = "Value out of range.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1093 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 2af9941 | 2016-11-23 11:10:59 +0100 | [diff] [blame] | 1094 | return 1; |
| 1095 | } |
| 1096 | |
| 1097 | /* check for unlimited values */ |
| 1098 | if (v <= 0) |
| 1099 | v = global.hardmaxconn; |
| 1100 | |
| 1101 | global.maxconn = v; |
| 1102 | |
| 1103 | /* Dequeues all of the listeners waiting for a resource */ |
| 1104 | if (!LIST_ISEMPTY(&global_listener_queue)) |
| 1105 | dequeue_all_listeners(&global_listener_queue); |
| 1106 | |
| 1107 | return 1; |
| 1108 | } |
| 1109 | |
Andjelko Iharos | c4df59e | 2017-07-20 11:59:48 +0200 | [diff] [blame] | 1110 | static int set_severity_output(int *target, char *argument) |
| 1111 | { |
| 1112 | if (!strcmp(argument, "none")) { |
| 1113 | *target = CLI_SEVERITY_NONE; |
| 1114 | return 1; |
| 1115 | } |
| 1116 | else if (!strcmp(argument, "number")) { |
| 1117 | *target = CLI_SEVERITY_NUMBER; |
| 1118 | return 1; |
| 1119 | } |
| 1120 | else if (!strcmp(argument, "string")) { |
| 1121 | *target = CLI_SEVERITY_STRING; |
| 1122 | return 1; |
| 1123 | } |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | /* parse a "set severity-output" command. */ |
| 1128 | static int cli_parse_set_severity_output(char **args, struct appctx *appctx, void *private) |
| 1129 | { |
| 1130 | if (*args[2] && set_severity_output(&appctx->cli_severity_output, args[2])) |
| 1131 | return 0; |
| 1132 | |
| 1133 | appctx->ctx.cli.severity = LOG_ERR; |
| 1134 | appctx->ctx.cli.msg = "one of 'none', 'number', 'string' is a required argument"; |
| 1135 | appctx->st0 = CLI_ST_PRINT; |
| 1136 | return 1; |
| 1137 | } |
William Lallemand | eceddf7 | 2016-12-15 18:06:44 +0100 | [diff] [blame] | 1138 | |
| 1139 | int cli_parse_default(char **args, struct appctx *appctx, void *private) |
| 1140 | { |
| 1141 | return 0; |
| 1142 | } |
| 1143 | |
Willy Tarreau | 45c742b | 2016-11-24 14:51:17 +0100 | [diff] [blame] | 1144 | /* parse a "set rate-limit" command. It always returns 1. */ |
| 1145 | static int cli_parse_set_ratelimit(char **args, struct appctx *appctx, void *private) |
| 1146 | { |
| 1147 | int v; |
| 1148 | int *res; |
| 1149 | int mul = 1; |
| 1150 | |
| 1151 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1152 | return 1; |
| 1153 | |
| 1154 | if (strcmp(args[2], "connections") == 0 && strcmp(args[3], "global") == 0) |
| 1155 | res = &global.cps_lim; |
| 1156 | else if (strcmp(args[2], "sessions") == 0 && strcmp(args[3], "global") == 0) |
| 1157 | res = &global.sps_lim; |
| 1158 | #ifdef USE_OPENSSL |
| 1159 | else if (strcmp(args[2], "ssl-sessions") == 0 && strcmp(args[3], "global") == 0) |
| 1160 | res = &global.ssl_lim; |
| 1161 | #endif |
| 1162 | else if (strcmp(args[2], "http-compression") == 0 && strcmp(args[3], "global") == 0) { |
| 1163 | res = &global.comp_rate_lim; |
| 1164 | mul = 1024; |
| 1165 | } |
| 1166 | else { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1167 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 45c742b | 2016-11-24 14:51:17 +0100 | [diff] [blame] | 1168 | appctx->ctx.cli.msg = |
| 1169 | "'set rate-limit' only supports :\n" |
| 1170 | " - 'connections global' to set the per-process maximum connection rate\n" |
| 1171 | " - 'sessions global' to set the per-process maximum session rate\n" |
| 1172 | #ifdef USE_OPENSSL |
| 1173 | " - 'ssl-session global' to set the per-process maximum SSL session rate\n" |
| 1174 | #endif |
| 1175 | " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1176 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 45c742b | 2016-11-24 14:51:17 +0100 | [diff] [blame] | 1177 | return 1; |
| 1178 | } |
| 1179 | |
| 1180 | if (!*args[4]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1181 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 45c742b | 2016-11-24 14:51:17 +0100 | [diff] [blame] | 1182 | appctx->ctx.cli.msg = "Expects an integer value.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1183 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 45c742b | 2016-11-24 14:51:17 +0100 | [diff] [blame] | 1184 | return 1; |
| 1185 | } |
| 1186 | |
| 1187 | v = atoi(args[4]); |
| 1188 | if (v < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1189 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 45c742b | 2016-11-24 14:51:17 +0100 | [diff] [blame] | 1190 | appctx->ctx.cli.msg = "Value out of range.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1191 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 45c742b | 2016-11-24 14:51:17 +0100 | [diff] [blame] | 1192 | return 1; |
| 1193 | } |
| 1194 | |
| 1195 | *res = v * mul; |
| 1196 | |
| 1197 | /* Dequeues all of the listeners waiting for a resource */ |
| 1198 | if (!LIST_ISEMPTY(&global_listener_queue)) |
| 1199 | dequeue_all_listeners(&global_listener_queue); |
| 1200 | |
| 1201 | return 1; |
| 1202 | } |
| 1203 | |
William Lallemand | f6975e9 | 2017-05-26 17:42:10 +0200 | [diff] [blame] | 1204 | /* parse the "expose-fd" argument on the bind lines */ |
| 1205 | static int bind_parse_expose_fd(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1206 | { |
| 1207 | if (!*args[cur_arg + 1]) { |
| 1208 | memprintf(err, "'%s' : missing fd type", args[cur_arg]); |
| 1209 | return ERR_ALERT | ERR_FATAL; |
| 1210 | } |
| 1211 | if (!strcmp(args[cur_arg+1], "listeners")) { |
| 1212 | conf->level |= ACCESS_FD_LISTENERS; |
| 1213 | } else { |
| 1214 | memprintf(err, "'%s' only supports 'listeners' (got '%s')", |
| 1215 | args[cur_arg], args[cur_arg+1]); |
| 1216 | return ERR_ALERT | ERR_FATAL; |
| 1217 | } |
| 1218 | |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1222 | /* parse the "level" argument on the bind lines */ |
| 1223 | static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1224 | { |
| 1225 | if (!*args[cur_arg + 1]) { |
| 1226 | memprintf(err, "'%s' : missing level", args[cur_arg]); |
| 1227 | return ERR_ALERT | ERR_FATAL; |
| 1228 | } |
| 1229 | |
William Lallemand | 07a62f7 | 2017-05-24 00:57:40 +0200 | [diff] [blame] | 1230 | if (!strcmp(args[cur_arg+1], "user")) { |
| 1231 | conf->level &= ~ACCESS_LVL_MASK; |
| 1232 | conf->level |= ACCESS_LVL_USER; |
| 1233 | } else if (!strcmp(args[cur_arg+1], "operator")) { |
| 1234 | conf->level &= ~ACCESS_LVL_MASK; |
| 1235 | conf->level |= ACCESS_LVL_OPER; |
| 1236 | } else if (!strcmp(args[cur_arg+1], "admin")) { |
| 1237 | conf->level &= ~ACCESS_LVL_MASK; |
| 1238 | conf->level |= ACCESS_LVL_ADMIN; |
| 1239 | } else { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1240 | memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')", |
| 1241 | args[cur_arg], args[cur_arg+1]); |
| 1242 | return ERR_ALERT | ERR_FATAL; |
| 1243 | } |
| 1244 | |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
Andjelko Iharos | c4df59e | 2017-07-20 11:59:48 +0200 | [diff] [blame] | 1248 | static int bind_parse_severity_output(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1249 | { |
| 1250 | if (!*args[cur_arg + 1]) { |
| 1251 | memprintf(err, "'%s' : missing severity format", args[cur_arg]); |
| 1252 | return ERR_ALERT | ERR_FATAL; |
| 1253 | } |
| 1254 | |
| 1255 | if (set_severity_output(&conf->severity_output, args[cur_arg+1])) |
| 1256 | return 0; |
| 1257 | else { |
| 1258 | memprintf(err, "'%s' only supports 'none', 'number', and 'string' (got '%s')", |
| 1259 | args[cur_arg], args[cur_arg+1]); |
| 1260 | return ERR_ALERT | ERR_FATAL; |
| 1261 | } |
| 1262 | } |
| 1263 | |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 1264 | /* Send all the bound sockets, always returns 1 */ |
| 1265 | static int _getsocks(char **args, struct appctx *appctx, void *private) |
| 1266 | { |
| 1267 | char *cmsgbuf = NULL; |
| 1268 | unsigned char *tmpbuf = NULL; |
| 1269 | struct cmsghdr *cmsg; |
| 1270 | struct stream_interface *si = appctx->owner; |
William Lallemand | f6975e9 | 2017-05-26 17:42:10 +0200 | [diff] [blame] | 1271 | struct stream *s = si_strm(si); |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 1272 | struct connection *remote = objt_conn(si_opposite(si)->end); |
| 1273 | struct msghdr msghdr; |
| 1274 | struct iovec iov; |
Olivier Houchard | 5474087 | 2017-04-06 14:45:14 +0200 | [diff] [blame] | 1275 | struct timeval tv = { .tv_sec = 1, .tv_usec = 0 }; |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 1276 | int *tmpfd; |
| 1277 | int tot_fd_nb = 0; |
| 1278 | struct proxy *px; |
| 1279 | int i = 0; |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1280 | int fd = remote->handle.fd; |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 1281 | int curoff = 0; |
| 1282 | int old_fcntl; |
| 1283 | int ret; |
| 1284 | |
| 1285 | /* Temporary set the FD in blocking mode, that will make our life easier */ |
| 1286 | old_fcntl = fcntl(fd, F_GETFL); |
| 1287 | if (old_fcntl < 0) { |
| 1288 | Warning("Couldn't get the flags for the unix socket\n"); |
| 1289 | goto out; |
| 1290 | } |
| 1291 | cmsgbuf = malloc(CMSG_SPACE(sizeof(int) * MAX_SEND_FD)); |
| 1292 | if (!cmsgbuf) { |
| 1293 | Warning("Failed to allocate memory to send sockets\n"); |
| 1294 | goto out; |
| 1295 | } |
| 1296 | if (fcntl(fd, F_SETFL, old_fcntl &~ O_NONBLOCK) == -1) { |
| 1297 | Warning("Cannot make the unix socket blocking\n"); |
| 1298 | goto out; |
| 1299 | } |
Olivier Houchard | 5474087 | 2017-04-06 14:45:14 +0200 | [diff] [blame] | 1300 | setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv)); |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 1301 | iov.iov_base = &tot_fd_nb; |
| 1302 | iov.iov_len = sizeof(tot_fd_nb); |
William Lallemand | f6975e9 | 2017-05-26 17:42:10 +0200 | [diff] [blame] | 1303 | if (!(strm_li(s)->bind_conf->level & ACCESS_FD_LISTENERS)) |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 1304 | goto out; |
| 1305 | memset(&msghdr, 0, sizeof(msghdr)); |
| 1306 | /* |
| 1307 | * First, calculates the total number of FD, so that we can let |
| 1308 | * the caller know how much he should expects. |
| 1309 | */ |
| 1310 | px = proxy; |
| 1311 | while (px) { |
| 1312 | struct listener *l; |
| 1313 | |
| 1314 | list_for_each_entry(l, &px->conf.listeners, by_fe) { |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 1315 | /* Only transfer IPv4/IPv6/UNIX sockets */ |
| 1316 | if (l->state >= LI_ZOMBIE && |
| 1317 | (l->proto->sock_family == AF_INET || |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 1318 | l->proto->sock_family == AF_INET6 || |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 1319 | l->proto->sock_family == AF_UNIX)) |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 1320 | tot_fd_nb++; |
| 1321 | } |
| 1322 | px = px->next; |
| 1323 | } |
| 1324 | if (tot_fd_nb == 0) |
| 1325 | goto out; |
| 1326 | |
| 1327 | /* First send the total number of file descriptors, so that the |
| 1328 | * receiving end knows what to expect. |
| 1329 | */ |
| 1330 | msghdr.msg_iov = &iov; |
| 1331 | msghdr.msg_iovlen = 1; |
| 1332 | ret = sendmsg(fd, &msghdr, 0); |
| 1333 | if (ret != sizeof(tot_fd_nb)) { |
| 1334 | Warning("Failed to send the number of sockets to send\n"); |
| 1335 | goto out; |
| 1336 | } |
| 1337 | |
| 1338 | /* Now send the fds */ |
| 1339 | msghdr.msg_control = cmsgbuf; |
| 1340 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * MAX_SEND_FD); |
| 1341 | cmsg = CMSG_FIRSTHDR(&msghdr); |
| 1342 | cmsg->cmsg_len = CMSG_LEN(MAX_SEND_FD * sizeof(int)); |
| 1343 | cmsg->cmsg_level = SOL_SOCKET; |
| 1344 | cmsg->cmsg_type = SCM_RIGHTS; |
| 1345 | tmpfd = (int *)CMSG_DATA(cmsg); |
| 1346 | |
| 1347 | px = proxy; |
| 1348 | /* For each socket, e message is sent, containing the following : |
| 1349 | * Size of the namespace name (or 0 if none), as an unsigned char. |
| 1350 | * The namespace name, if any |
| 1351 | * Size of the interface name (or 0 if none), as an unsigned char |
| 1352 | * The interface name, if any |
| 1353 | * Listener options, as an int. |
| 1354 | */ |
| 1355 | /* We will send sockets MAX_SEND_FD per MAX_SEND_FD, allocate a |
| 1356 | * buffer big enough to store the socket informations. |
| 1357 | */ |
| 1358 | tmpbuf = malloc(MAX_SEND_FD * (1 + NAME_MAX + 1 + IFNAMSIZ + sizeof(int))); |
| 1359 | if (tmpbuf == NULL) { |
| 1360 | Warning("Failed to allocate memory to transfer socket informations\n"); |
| 1361 | goto out; |
| 1362 | } |
| 1363 | iov.iov_base = tmpbuf; |
| 1364 | while (px) { |
| 1365 | struct listener *l; |
| 1366 | |
| 1367 | list_for_each_entry(l, &px->conf.listeners, by_fe) { |
| 1368 | int ret; |
| 1369 | /* Only transfer IPv4/IPv6 sockets */ |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 1370 | if (l->state >= LI_ZOMBIE && |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 1371 | (l->proto->sock_family == AF_INET || |
| 1372 | l->proto->sock_family == AF_INET6 || |
| 1373 | l->proto->sock_family == AF_UNIX)) { |
| 1374 | memcpy(&tmpfd[i % MAX_SEND_FD], &l->fd, sizeof(l->fd)); |
| 1375 | if (!l->netns) |
| 1376 | tmpbuf[curoff++] = 0; |
| 1377 | #ifdef CONFIG_HAP_NS |
| 1378 | else { |
| 1379 | char *name = l->netns->node.key; |
| 1380 | unsigned char len = l->netns->name_len; |
| 1381 | tmpbuf[curoff++] = len; |
| 1382 | memcpy(tmpbuf + curoff, name, len); |
| 1383 | curoff += len; |
| 1384 | } |
| 1385 | #endif |
| 1386 | if (l->interface) { |
| 1387 | unsigned char len = strlen(l->interface); |
| 1388 | tmpbuf[curoff++] = len; |
| 1389 | memcpy(tmpbuf + curoff, l->interface, len); |
| 1390 | curoff += len; |
| 1391 | } else |
| 1392 | tmpbuf[curoff++] = 0; |
| 1393 | memcpy(tmpbuf + curoff, &l->options, |
| 1394 | sizeof(l->options)); |
| 1395 | curoff += sizeof(l->options); |
| 1396 | |
| 1397 | |
| 1398 | i++; |
| 1399 | } else |
| 1400 | continue; |
| 1401 | if ((!(i % MAX_SEND_FD))) { |
| 1402 | iov.iov_len = curoff; |
| 1403 | if (sendmsg(fd, &msghdr, 0) != curoff) { |
| 1404 | Warning("Failed to transfer sockets\n"); |
| 1405 | printf("errno %d\n", errno); |
| 1406 | goto out; |
| 1407 | } |
| 1408 | /* Wait for an ack */ |
| 1409 | do { |
| 1410 | ret = recv(fd, &tot_fd_nb, |
| 1411 | sizeof(tot_fd_nb), 0); |
| 1412 | } while (ret == -1 && errno == EINTR); |
| 1413 | if (ret <= 0) { |
| 1414 | Warning("Unexpected error while transferring sockets\n"); |
| 1415 | goto out; |
| 1416 | } |
| 1417 | curoff = 0; |
| 1418 | } |
| 1419 | |
| 1420 | } |
| 1421 | px = px->next; |
| 1422 | } |
| 1423 | if (i % MAX_SEND_FD) { |
| 1424 | iov.iov_len = curoff; |
| 1425 | cmsg->cmsg_len = CMSG_LEN((i % MAX_SEND_FD) * sizeof(int)); |
| 1426 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * (i % MAX_SEND_FD)); |
| 1427 | if (sendmsg(fd, &msghdr, 0) != curoff) { |
| 1428 | Warning("Failed to transfer sockets\n"); |
| 1429 | goto out; |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | out: |
| 1434 | if (old_fcntl >= 0 && fcntl(fd, F_SETFL, old_fcntl) == -1) { |
| 1435 | Warning("Cannot make the unix socket non-blocking\n"); |
| 1436 | goto out; |
| 1437 | } |
| 1438 | appctx->st0 = CLI_ST_END; |
| 1439 | free(cmsgbuf); |
| 1440 | free(tmpbuf); |
| 1441 | return 1; |
| 1442 | } |
| 1443 | |
| 1444 | |
| 1445 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1446 | static struct applet cli_applet = { |
| 1447 | .obj_type = OBJ_TYPE_APPLET, |
| 1448 | .name = "<CLI>", /* used for logging */ |
| 1449 | .fct = cli_io_handler, |
| 1450 | .release = cli_release_handler, |
| 1451 | }; |
| 1452 | |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 1453 | /* register cli keywords */ |
| 1454 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | 2af9941 | 2016-11-23 11:10:59 +0100 | [diff] [blame] | 1455 | { { "set", "maxconn", "global", NULL }, "set maxconn global : change the per-process maxconn setting", cli_parse_set_maxconn_global, NULL }, |
Willy Tarreau | 45c742b | 2016-11-24 14:51:17 +0100 | [diff] [blame] | 1456 | { { "set", "rate-limit", NULL }, "set rate-limit : change a rate limiting value", cli_parse_set_ratelimit, NULL }, |
Andjelko Iharos | c4df59e | 2017-07-20 11:59:48 +0200 | [diff] [blame] | 1457 | { { "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 Tarreau | 599852e | 2016-11-22 20:33:32 +0100 | [diff] [blame] | 1458 | { { "set", "timeout", NULL }, "set timeout : change a timeout setting", cli_parse_set_timeout, NULL, NULL }, |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 1459 | { { "show", "env", NULL }, "show env [var] : dump environment variables known to the process", cli_parse_show_env, cli_io_handler_show_env, NULL }, |
William Lallemand | eceddf7 | 2016-12-15 18:06:44 +0100 | [diff] [blame] | 1460 | { { "show", "cli", "sockets", NULL }, "show cli sockets : dump list of cli sockets", cli_parse_default, cli_io_handler_show_cli_sock, NULL }, |
Willy Tarreau | 7a4a0ac | 2017-07-25 19:32:50 +0200 | [diff] [blame] | 1461 | { { "show", "fd", NULL }, "show fd [num] : dump list of file descriptors in use", cli_parse_show_fd, cli_io_handler_show_fd, NULL }, |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 1462 | { { "_getsocks", NULL }, NULL, _getsocks, NULL }, |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 1463 | {{},} |
| 1464 | }}; |
| 1465 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1466 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1467 | { CFG_GLOBAL, "stats", stats_parse_global }, |
| 1468 | { 0, NULL, NULL }, |
| 1469 | }}; |
| 1470 | |
| 1471 | static struct bind_kw_list bind_kws = { "STAT", { }, { |
William Lallemand | f6975e9 | 2017-05-26 17:42:10 +0200 | [diff] [blame] | 1472 | { "level", bind_parse_level, 1 }, /* set the unix socket admin level */ |
| 1473 | { "expose-fd", bind_parse_expose_fd, 1 }, /* set the unix socket expose fd rights */ |
Andjelko Iharos | c4df59e | 2017-07-20 11:59:48 +0200 | [diff] [blame] | 1474 | { "severity-output", bind_parse_severity_output, 1 }, /* set the severity output format */ |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1475 | { NULL, NULL, 0 }, |
| 1476 | }}; |
| 1477 | |
| 1478 | __attribute__((constructor)) |
| 1479 | static void __dumpstats_module_init(void) |
| 1480 | { |
| 1481 | cfg_register_keywords(&cfg_kws); |
Willy Tarreau | 0a73929 | 2016-11-22 20:21:23 +0100 | [diff] [blame] | 1482 | cli_register_kw(&cli_kws); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1483 | bind_register_keywords(&bind_kws); |
| 1484 | } |
| 1485 | |
| 1486 | /* |
| 1487 | * Local variables: |
| 1488 | * c-indent-level: 8 |
| 1489 | * c-basic-offset: 8 |
| 1490 | * End: |
| 1491 | */ |