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