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