diff --git a/src/activity.c b/src/activity.c index 5ffa6623d..8844e0c5f 100644 --- a/src/activity.c +++ b/src/activity.c @@ -67,12 +67,8 @@ static int cli_parse_set_profiling(char **args, char *payload, struct appctx *ap if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) return 1; - if (strcmp(args[2], "tasks") != 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Expects 'tasks'.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (strcmp(args[2], "tasks") != 0) + return cli_err(appctx, "Expects 'tasks'.\n"); if (strcmp(args[3], "on") == 0) { unsigned int old = profiling; @@ -89,12 +85,9 @@ static int cli_parse_set_profiling(char **args, char *payload, struct appctx *ap while (!_HA_ATOMIC_CAS(&profiling, &old, (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_OFF)) ; } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Expects 'on', 'auto', or 'off'.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + else + return cli_err(appctx, "Expects 'on', 'auto', or 'off'.\n"); + return 1; } diff --git a/src/cli.c b/src/cli.c index 1f6a84576..180567149 100644 --- a/src/cli.c +++ b/src/cli.c @@ -41,7 +41,6 @@ #include #include -#include #include #include #include @@ -50,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -457,9 +457,7 @@ int cli_has_level(struct appctx *appctx, int level) { if ((appctx->cli_level & ACCESS_LVL_MASK) < level) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = stats_permission_denied_msg; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, stats_permission_denied_msg); return 0; } return 1; @@ -1303,12 +1301,9 @@ static int cli_parse_show_env(char **args, char *payload, struct appctx *appctx, (*var)[len] == '=') break; } - if (!*var) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Variable not found\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*var) + return cli_err(appctx, "Variable not found\n"); + appctx->st2 = STAT_ST_END; } appctx->ctx.cli.p0 = var; @@ -1343,31 +1338,19 @@ static int cli_parse_set_timeout(char **args, char *payload, struct appctx *appc unsigned timeout; const char *res; - if (!*args[3]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Expects an integer value.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*args[3]) + return cli_err(appctx, "Expects an integer value.\n"); res = parse_time_err(args[3], &timeout, TIME_UNIT_S); - if (res || timeout < 1) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Invalid timeout value.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (res || timeout < 1) + return cli_err(appctx, "Invalid timeout value.\n"); s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000); task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts return 1; } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + + return cli_err(appctx, "'set timeout' only supports 'cli'.\n"); } /* parse a "set maxconn global" command. It always returns 1. */ @@ -1378,20 +1361,12 @@ static int cli_parse_set_maxconn_global(char **args, char *payload, struct appct if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) return 1; - if (!*args[3]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Expects an integer value.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*args[3]) + return cli_err(appctx, "Expects an integer value.\n"); v = atoi(args[3]); - if (v > global.hardmaxconn) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Value out of range.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (v > global.hardmaxconn) + return cli_err(appctx, "Value out of range.\n"); /* check for unlimited values */ if (v <= 0) @@ -1429,30 +1404,21 @@ static int cli_parse_set_severity_output(char **args, char *payload, struct appc if (*args[2] && set_severity_output(&appctx->cli_severity_output, args[2])) return 0; - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "one of 'none', 'number', 'string' is a required argument\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; + return cli_err(appctx, "one of 'none', 'number', 'string' is a required argument\n"); } /* show the level of the current CLI session */ static int cli_parse_show_lvl(char **args, char *payload, struct appctx *appctx, void *private) { - - appctx->ctx.cli.severity = LOG_INFO; if ((appctx->cli_level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN) - appctx->ctx.cli.msg = "admin\n"; + return cli_msg(appctx, LOG_INFO, "admin\n"); else if ((appctx->cli_level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER) - appctx->ctx.cli.msg = "operator\n"; + return cli_msg(appctx, LOG_INFO, "operator\n"); else if ((appctx->cli_level & ACCESS_LVL_MASK) == ACCESS_LVL_USER) - appctx->ctx.cli.msg = "user\n"; + return cli_msg(appctx, LOG_INFO, "user\n"); else - appctx->ctx.cli.msg = "unknown\n"; - - appctx->st0 = CLI_ST_PRINT; - return 1; - + return cli_msg(appctx, LOG_INFO, "unknown\n"); } /* parse and set the CLI level dynamically */ @@ -1508,33 +1474,22 @@ static int cli_parse_set_ratelimit(char **args, char *payload, struct appctx *ap mul = 1024; } else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = + return cli_err(appctx, "'set rate-limit' only supports :\n" " - 'connections global' to set the per-process maximum connection rate\n" " - 'sessions global' to set the per-process maximum session rate\n" #ifdef USE_OPENSSL " - 'ssl-sessions global' to set the per-process maximum SSL session rate\n" #endif - " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; + " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n"); } - if (!*args[4]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Expects an integer value.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*args[4]) + return cli_err(appctx, "Expects an integer value.\n"); v = atoi(args[4]); - if (v < 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Value out of range.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (v < 0) + return cli_err(appctx, "Value out of range.\n"); *res = v * mul; diff --git a/src/debug.c b/src/debug.c index 0522f88bf..f378cd97f 100644 --- a/src/debug.c +++ b/src/debug.c @@ -189,32 +189,18 @@ static int debug_parse_cli_close(char **args, char *payload, struct appctx *appc if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) return 1; - if (!*args[3]) { - appctx->ctx.cli.msg = "Missing file descriptor number.\n"; - goto reterr; - } + if (!*args[3]) + return cli_err(appctx, "Missing file descriptor number.\n"); fd = atoi(args[3]); - if (fd < 0 || fd >= global.maxsock) { - appctx->ctx.cli.msg = "File descriptor out of range.\n"; - goto reterr; - } + if (fd < 0 || fd >= global.maxsock) + return cli_err(appctx, "File descriptor out of range.\n"); - if (!fdtab[fd].owner) { - appctx->ctx.cli.msg = "File descriptor was already closed.\n"; - goto retinfo; - } + if (!fdtab[fd].owner) + return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n"); fd_delete(fd); return 1; - retinfo: - appctx->ctx.cli.severity = LOG_INFO; - appctx->st0 = CLI_ST_PRINT; - return 1; - reterr: - appctx->ctx.cli.severity = LOG_ERR; - appctx->st0 = CLI_ST_PRINT; - return 1; } /* parse a "debug dev delay" command. It always returns 1. */ @@ -293,12 +279,8 @@ static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appct } f = popen(trash.area, "re"); - if (!f) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Failed to execute command.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!f) + return cli_err(appctx, "Failed to execute command.\n"); chunk_reset(&trash); while (1) { @@ -314,10 +296,7 @@ static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appct fclose(f); trash.area[trash.data] = 0; - appctx->ctx.cli.severity = LOG_INFO; - appctx->ctx.cli.msg = trash.area; - appctx->st0 = CLI_ST_PRINT; - return 1; + return cli_msg(appctx, LOG_INFO, trash.area); } /* parse a "debug dev hex" command. It always returns 1. */ @@ -328,16 +307,12 @@ static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) return 1; - if (!*args[3]) { - appctx->ctx.cli.msg = "Missing memory address to dump from.\n"; - goto reterr; - } + if (!*args[3]) + return cli_err(appctx, "Missing memory address to dump from.\n"); start = strtoul(args[3], NULL, 0); - if (!start) { - appctx->ctx.cli.msg = "Will not dump from NULL address.\n"; - goto reterr; - } + if (!start) + return cli_err(appctx, "Will not dump from NULL address.\n"); /* by default, dump ~128 till next block of 16 */ len = strtoul(args[4], NULL, 0); @@ -347,14 +322,7 @@ static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx chunk_reset(&trash); dump_hex(&trash, " ", (const void *)start, len, 1); trash.area[trash.data] = 0; - appctx->ctx.cli.severity = LOG_INFO; - appctx->ctx.cli.msg = trash.area; - appctx->st0 = CLI_ST_PRINT; - return 1; - reterr: - appctx->ctx.cli.severity = LOG_ERR; - appctx->st0 = CLI_ST_PRINT; - return 1; + return cli_msg(appctx, LOG_INFO, trash.area); } /* parse a "debug dev tkill" command. It always returns 1. */ @@ -369,12 +337,8 @@ static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appc if (*args[3]) thr = atoi(args[3]); - if (thr < 0 || thr > global.nbthread) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Thread number out of range (use 0 for current).\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (thr < 0 || thr > global.nbthread) + return cli_err(appctx, "Thread number out of range (use 0 for current).\n"); if (*args[4]) sig = atoi(args[4]); diff --git a/src/dns.c b/src/dns.c index d59e96ef2..ef840e50c 100644 --- a/src/dns.c +++ b/src/dns.c @@ -2018,12 +2018,8 @@ static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *a break; } } - if (appctx->ctx.cli.p0 == NULL) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Can't find that resolvers section\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (appctx->ctx.cli.p0 == NULL) + return cli_err(appctx, "Can't find that resolvers section\n"); } return 0; } diff --git a/src/map.c b/src/map.c index 1a2190d9a..39a72c952 100644 --- a/src/map.c +++ b/src/map.c @@ -599,31 +599,19 @@ static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, /* No parameter. */ if (!*args[2] || !*args[3]) { - if (appctx->ctx.map.display_flags == PAT_REF_MAP) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Missing map identifier and/or key.\n"; - } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n"; - } - appctx->st0 = CLI_ST_PRINT; - return 1; + if (appctx->ctx.map.display_flags == PAT_REF_MAP) + return cli_err(appctx, "Missing map identifier and/or key.\n"); + else + return cli_err(appctx, "Missing ACL identifier and/or key.\n"); } /* lookup into the maps */ appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); if (!appctx->ctx.map.ref) { - if (appctx->ctx.map.display_flags == PAT_REF_MAP) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown map identifier. Please use # or .\n"; - } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown ACL identifier. Please use # or .\n"; - } - appctx->st0 = CLI_ST_PRINT; - return 1; + if (appctx->ctx.map.display_flags == PAT_REF_MAP) + return cli_err(appctx, "Unknown map identifier. Please use # or .\n"); + else + return cli_err(appctx, "Unknown ACL identifier. Please use # or .\n"); } /* copy input string. The string must be allocated because @@ -633,12 +621,8 @@ static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, appctx->ctx.map.chunk.data = strlen(args[3]); appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.data + 1; appctx->ctx.map.chunk.area = strdup(args[3]); - if (!appctx->ctx.map.chunk.area) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Out of memory error.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!appctx->ctx.map.chunk.area) + return cli_err(appctx, "Out of memory error.\n"); return 0; } @@ -676,16 +660,10 @@ static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); if (!appctx->ctx.map.ref || !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) { - if (appctx->ctx.map.display_flags == PAT_REF_MAP) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown map identifier. Please use # or .\n"; - } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown ACL identifier. Please use # or .\n"; - } - appctx->st0 = CLI_ST_PRINT; - return 1; + if (appctx->ctx.map.display_flags == PAT_REF_MAP) + return cli_err(appctx, "Unknown map identifier. Please use # or .\n"); + else + return cli_err(appctx, "Unknown ACL identifier. Please use # or .\n"); } appctx->io_handler = cli_io_handler_pat_list; appctx->io_release = cli_release_show_map; @@ -704,21 +682,13 @@ static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, appctx->ctx.map.display_flags = PAT_REF_MAP; /* Expect three parameters: map name, key and new value. */ - if (!*args[2] || !*args[3] || !*args[4]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*args[2] || !*args[3] || !*args[4]) + return cli_err(appctx, "'set map' expects three parameters: map identifier, key and value.\n"); /* Lookup the reference in the maps. */ appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); - if (!appctx->ctx.map.ref) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown map identifier. Please use # or .\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!appctx->ctx.map.ref) + return cli_err(appctx, "Unknown map identifier. Please use # or .\n"); /* If the entry identifier start with a '#', it is considered as * pointer id @@ -730,38 +700,23 @@ static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, /* Convert argument to integer value. */ conv = strtoll(&args[3][1], &error, 16); - if (*error != '\0') { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Malformed identifier. Please use # or .\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (*error != '\0') + return cli_err(appctx, "Malformed identifier. Please use # or .\n"); /* Convert and check integer to pointer. */ ref = (struct pat_ref_elt *)(long)conv; - if ((long long int)(long)ref != conv) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Malformed identifier. Please use # or .\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if ((long long int)(long)ref != conv) + return cli_err(appctx, "Malformed identifier. Please use # or .\n"); /* Try to modify the entry. */ err = NULL; HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) { HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); - if (err) { - memprintf(&err, "%s.\n", err); - appctx->ctx.cli.err = err; - appctx->st0 = CLI_ST_PRINT_FREE; - } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Failed to update an entry.\n"; - appctx->st0 = CLI_ST_PRINT; - } - return 1; + if (err) + return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); + else + return cli_err(appctx, "Failed to update an entry.\n"); } HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); } @@ -773,17 +728,10 @@ static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) { HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); - if (err) { - memprintf(&err, "%s.\n", err); - appctx->ctx.cli.err = err; - appctx->st0 = CLI_ST_PRINT_FREE; - } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Failed to update an entry.\n"; - appctx->st0 = CLI_ST_PRINT; - } - return 1; + if (err) + return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); + else + return cli_err(appctx, "Failed to update an entry.\n"); } HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); } @@ -829,35 +777,21 @@ static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, */ if (appctx->ctx.map.display_flags == PAT_REF_MAP) { if ((!payload && (!*args[2] || !*args[3] || !*args[4])) || - (payload && !*args[2])) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'add map' expects three parameters (map identifier, key and value) or one parameter (map identifier) and a payload\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } - } - else { - if (!*args[2] || !*args[3]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + (payload && !*args[2])) + return cli_err(appctx, + "'add map' expects three parameters (map identifier, key and value)" + " or one parameter (map identifier) and a payload\n"); } + else if (!*args[2] || !*args[3]) + return cli_err(appctx, "'add acl' expects two parameters: ACL identifier and pattern.\n"); /* Lookup for the reference. */ appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); if (!appctx->ctx.map.ref) { - if (appctx->ctx.map.display_flags == PAT_REF_MAP) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown map identifier. Please use # or .\n"; - } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown ACL identifier. Please use # or .\n"; - } - appctx->st0 = CLI_ST_PRINT; - return 1; + if (appctx->ctx.map.display_flags == PAT_REF_MAP) + return cli_err(appctx, "Unknown map identifier. Please use # or .\n"); + else + return cli_err(appctx, "Unknown ACL identifier. Please use # or .\n"); } /* The command "add acl" is prohibited if the reference @@ -865,29 +799,19 @@ static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, */ if ((appctx->ctx.map.display_flags & PAT_REF_ACL) && (appctx->ctx.map.ref->flags & PAT_REF_SMP)) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. " - "You must use the command 'add map' to add values.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; + return cli_err(appctx, + "This ACL is shared with a map containing samples. " + "You must use the command 'add map' to add values.\n"); } - /* Add value(s). */ err = NULL; if (!payload) { ret = map_add_key_value(appctx, args[3], args[4], &err); if (!ret) { - if (err) { - memprintf(&err, "%s.\n", err); - appctx->ctx.cli.err = err; - appctx->st0 = CLI_ST_PRINT_FREE; - } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Failed to add an entry.\n"; - appctx->st0 = CLI_ST_PRINT; - } - return 1; + if (err) + return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); + else + return cli_err(appctx, "Failed to add an entry.\n"); } } else { @@ -902,12 +826,9 @@ static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, l = strcspn(key, " \t"); payload += l; - if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP) { - memprintf(&err, "Missing value for key '%s'.\n", key); - appctx->ctx.cli.err = err; - appctx->st0 = CLI_ST_PRINT_FREE; - return 1; - } + if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP) + return cli_dynerr(appctx, memprintf(&err, "Missing value for key '%s'.\n", key)); + key[l] = 0; payload++; @@ -922,17 +843,10 @@ static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, ret = map_add_key_value(appctx, key, value, &err); if (!ret) { - if (err) { - memprintf(&err, "%s.\n", err); - appctx->ctx.cli.err = err; - appctx->st0 = CLI_ST_PRINT_FREE; - } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Failed to add a key.\n"; - appctx->st0 = CLI_ST_PRINT; - } - return 1; + if (err) + return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); + else + return cli_err(appctx, "Failed to add a key.\n"); } } } @@ -953,33 +867,18 @@ static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, appctx->ctx.map.display_flags = PAT_REF_ACL; /* Expect two parameters: map name and key. */ - if (appctx->ctx.map.display_flags == PAT_REF_MAP) { - if (!*args[2] || !*args[3]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } - } - - else { - if (!*args[2] || !*args[3]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*args[2] || !*args[3]) { + if (appctx->ctx.map.display_flags == PAT_REF_MAP) + return cli_err(appctx, "This command expects two parameters: map identifier and key.\n"); + else + return cli_err(appctx, "This command expects two parameters: ACL identifier and key.\n"); } /* Lookup the reference in the maps. */ appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); if (!appctx->ctx.map.ref || - !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown map identifier. Please use # or .\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) + return cli_err(appctx, "Unknown map identifier. Please use # or .\n"); /* If the entry identifier start with a '#', it is considered as * pointer id @@ -991,31 +890,20 @@ static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, /* Convert argument to integer value. */ conv = strtoll(&args[3][1], &error, 16); - if (*error != '\0') { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Malformed identifier. Please use # or .\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (*error != '\0') + return cli_err(appctx, "Malformed identifier. Please use # or .\n"); /* Convert and check integer to pointer. */ ref = (struct pat_ref_elt *)(long)conv; - if ((long long int)(long)ref != conv) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Malformed identifier. Please use # or .\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if ((long long int)(long)ref != conv) + return cli_err(appctx, "Malformed identifier. Please use # or .\n"); /* Try to delete the entry. */ HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) { HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); /* The entry is not found, send message. */ - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Key not found.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; + return cli_err(appctx, "Key not found.\n"); } HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); } @@ -1027,10 +915,7 @@ static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) { HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); /* The entry is not found, send message. */ - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Key not found.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; + return cli_err(appctx, "Key not found.\n"); } HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); } @@ -1052,32 +937,20 @@ static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx /* no parameter */ if (!*args[2]) { - if (appctx->ctx.map.display_flags == PAT_REF_MAP) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Missing map identifier.\n"; - } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Missing ACL identifier.\n"; - } - appctx->st0 = CLI_ST_PRINT; - return 1; + if (appctx->ctx.map.display_flags == PAT_REF_MAP) + return cli_err(appctx, "Missing map identifier.\n"); + else + return cli_err(appctx, "Missing ACL identifier.\n"); } /* lookup into the refs and check the map flag */ appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); if (!appctx->ctx.map.ref || !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) { - if (appctx->ctx.map.display_flags == PAT_REF_MAP) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown map identifier. Please use # or .\n"; - } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown ACL identifier. Please use # or .\n"; - } - appctx->st0 = CLI_ST_PRINT; - return 1; + if (appctx->ctx.map.display_flags == PAT_REF_MAP) + return cli_err(appctx, "Unknown map identifier. Please use # or .\n"); + else + return cli_err(appctx, "Unknown ACL identifier. Please use # or .\n"); } /* Clear all. */ diff --git a/src/peers.c b/src/peers.c index cb6998c58..4695b5ee0 100644 --- a/src/peers.c +++ b/src/peers.c @@ -3015,12 +3015,8 @@ static int cli_parse_show_peers(char **args, char *payload, struct appctx *appct } } - if (!p) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "No such peers\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!p) + return cli_err(appctx, "No such peers\n"); } return 0; diff --git a/src/proxy.c b/src/proxy.c index 9d99ade25..af4809fe9 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -1681,17 +1681,13 @@ struct proxy *cli_find_frontend(struct appctx *appctx, const char *arg) struct proxy *px; if (!*arg) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "A frontend name is expected.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "A frontend name is expected.\n"); return NULL; } px = proxy_fe_by_name(arg); if (!px) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "No such frontend.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "No such frontend.\n"); return NULL; } return px; @@ -1706,17 +1702,13 @@ struct proxy *cli_find_backend(struct appctx *appctx, const char *arg) struct proxy *px; if (!*arg) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "A backend name is expected.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "A backend name is expected.\n"); return NULL; } px = proxy_be_by_name(arg); if (!px) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "No such backend.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "No such backend.\n"); return NULL; } return px; @@ -1736,12 +1728,9 @@ static int cli_parse_show_servers(char **args, char *payload, struct appctx *app /* read server state from local file */ px = proxy_be_by_name(args[3]); - if (!px) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Can't find backend.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!px) + return cli_err(appctx, "Can't find backend.\n"); + appctx->ctx.cli.p0 = px; appctx->ctx.cli.i0 = px->uuid; } @@ -1984,20 +1973,12 @@ static int cli_parse_set_dyncookie_key_backend(char **args, char *payload, struc if (!px) return 1; - if (!*args[4]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "String value expected.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*args[4]) + return cli_err(appctx, "String value expected.\n"); newkey = strdup(args[4]); - if (!newkey) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Failed to allocate memory.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!newkey) + return cli_err(appctx, "Failed to allocate memory.\n"); /* Note: this lock is to make sure this doesn't change while another * thread is in srv_set_dyncookie(). @@ -2033,20 +2014,12 @@ static int cli_parse_set_maxconn_frontend(char **args, char *payload, struct app if (!px) return 1; - if (!*args[4]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Integer value expected.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*args[4]) + return cli_err(appctx, "Integer value expected.\n"); v = atoi(args[4]); - if (v < 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Value out of range.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (v < 0) + return cli_err(appctx, "Value out of range.\n"); /* OK, the value is fine, so we assign it to the proxy and to all of * its listeners. The blocked ones will be dequeued. @@ -2082,12 +2055,8 @@ static int cli_parse_shutdown_frontend(char **args, char *payload, struct appctx if (!px) return 1; - if (px->state == PR_STSTOPPED) { - appctx->ctx.cli.severity = LOG_NOTICE; - appctx->ctx.cli.msg = "Frontend was already shut down.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (px->state == PR_STSTOPPED) + return cli_msg(appctx, LOG_NOTICE, "Frontend was already shut down.\n"); ha_warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n", px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn); @@ -2114,30 +2083,19 @@ static int cli_parse_disable_frontend(char **args, char *payload, struct appctx if (!px) return 1; - if (px->state == PR_STSTOPPED) { - appctx->ctx.cli.severity = LOG_NOTICE; - appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (px->state == PR_STSTOPPED) + return cli_msg(appctx, LOG_NOTICE, "Frontend was previously shut down, cannot disable.\n"); - if (px->state == PR_STPAUSED) { - appctx->ctx.cli.severity = LOG_NOTICE; - appctx->ctx.cli.msg = "Frontend is already disabled.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (px->state == PR_STPAUSED) + return cli_msg(appctx, LOG_NOTICE, "Frontend is already disabled.\n"); HA_SPIN_LOCK(PROXY_LOCK, &px->lock); ret = pause_proxy(px); HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock); - if (!ret) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!ret) + return cli_err(appctx, "Failed to pause frontend, check logs for precise cause.\n"); + return 1; } @@ -2157,30 +2115,18 @@ static int cli_parse_enable_frontend(char **args, char *payload, struct appctx * if (!px) return 1; - if (px->state == PR_STSTOPPED) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (px->state == PR_STSTOPPED) + return cli_err(appctx, "Frontend was previously shut down, cannot enable.\n"); - if (px->state != PR_STPAUSED) { - appctx->ctx.cli.severity = LOG_NOTICE; - appctx->ctx.cli.msg = "Frontend is already enabled.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (px->state != PR_STPAUSED) + return cli_msg(appctx, LOG_NOTICE, "Frontend is already enabled.\n"); HA_SPIN_LOCK(PROXY_LOCK, &px->lock); ret = resume_proxy(px); HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock); - if (!ret) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!ret) + return cli_err(appctx, "Failed to resume frontend, check logs for precise cause (port conflict?).\n"); return 1; } @@ -2201,12 +2147,8 @@ static int cli_parse_show_errors(char **args, char *payload, struct appctx *appc else appctx->ctx.errors.iid = atoi(args[2]); - if (!appctx->ctx.errors.iid) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "No such proxy.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!appctx->ctx.errors.iid) + return cli_err(appctx, "No such proxy.\n"); } else appctx->ctx.errors.iid = -1; // dump all proxies diff --git a/src/server.c b/src/server.c index f95574a8a..10c4cf6de 100644 --- a/src/server.c +++ b/src/server.c @@ -4619,23 +4619,17 @@ struct server *cli_find_server(struct appctx *appctx, char *arg) } if (!*line || !*arg) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Require 'backend/server'.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "Require 'backend/server'.\n"); return NULL; } if (!get_backend_server(arg, line, &px, &sv)) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, px ? "No such server.\n" : "No such backend.\n"); return NULL; } if (px->state == PR_STSTOPPED) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Proxy is disabled.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "Proxy is disabled.\n"); return NULL; } @@ -4660,11 +4654,8 @@ static int cli_parse_set_server(char **args, char *payload, struct appctx *appct if (strcmp(args[3], "weight") == 0) { warning = server_parse_weight_change_request(sv, args[4]); - if (warning) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = warning; - appctx->st0 = CLI_ST_PRINT; - } + if (warning) + cli_err(appctx, warning); } else if (strcmp(args[3], "state") == 0) { if (strcmp(args[4], "ready") == 0) @@ -4673,18 +4664,12 @@ static int cli_parse_set_server(char **args, char *payload, struct appctx *appct srv_adm_set_drain(sv); else if (strcmp(args[4], "maint") == 0) srv_adm_set_maint(sv); - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'set server state' expects 'ready', 'drain' and 'maint'.\n"; - appctx->st0 = CLI_ST_PRINT; - } + else + cli_err(appctx, "'set server state' expects 'ready', 'drain' and 'maint'.\n"); } else if (strcmp(args[3], "health") == 0) { - if (sv->track) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "cannot change health on a tracking server.\n"; - appctx->st0 = CLI_ST_PRINT; - } + if (sv->track) + cli_err(appctx, "cannot change health on a tracking server.\n"); else if (strcmp(args[4], "up") == 0) { sv->check.health = sv->check.rise + sv->check.fall - 1; srv_set_running(sv, "changed from CLI", NULL); @@ -4697,18 +4682,12 @@ static int cli_parse_set_server(char **args, char *payload, struct appctx *appct sv->check.health = 0; srv_set_stopped(sv, "changed from CLI", NULL); } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'set server health' expects 'up', 'stopping', or 'down'.\n"; - appctx->st0 = CLI_ST_PRINT; - } + else + cli_err(appctx, "'set server health' expects 'up', 'stopping', or 'down'.\n"); } else if (strcmp(args[3], "agent") == 0) { - if (!(sv->agent.state & CHK_ST_ENABLED)) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n"; - appctx->st0 = CLI_ST_PRINT; - } + if (!(sv->agent.state & CHK_ST_ENABLED)) + cli_err(appctx, "agent checks are not enabled on this server.\n"); else if (strcmp(args[4], "up") == 0) { sv->agent.health = sv->agent.rise + sv->agent.fall - 1; srv_set_running(sv, "changed from CLI", NULL); @@ -4717,37 +4696,23 @@ static int cli_parse_set_server(char **args, char *payload, struct appctx *appct sv->agent.health = 0; srv_set_stopped(sv, "changed from CLI", NULL); } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'set server agent' expects 'up' or 'down'.\n"; - appctx->st0 = CLI_ST_PRINT; - } + else + cli_err(appctx, "'set server agent' expects 'up' or 'down'.\n"); } else if (strcmp(args[3], "agent-addr") == 0) { - if (!(sv->agent.state & CHK_ST_ENABLED)) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n"; - appctx->st0 = CLI_ST_PRINT; - } else { - if (str2ip(args[4], &sv->agent.addr) == NULL) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "incorrect addr address given for agent.\n"; - appctx->st0 = CLI_ST_PRINT; - } - } + if (!(sv->agent.state & CHK_ST_ENABLED)) + cli_err(appctx, "agent checks are not enabled on this server.\n"); + else if (str2ip(args[4], &sv->agent.addr) == NULL) + cli_err(appctx, "incorrect addr address given for agent.\n"); } else if (strcmp(args[3], "agent-send") == 0) { - if (!(sv->agent.state & CHK_ST_ENABLED)) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n"; - appctx->st0 = CLI_ST_PRINT; - } else { + if (!(sv->agent.state & CHK_ST_ENABLED)) + cli_err(appctx, "agent checks are not enabled on this server.\n"); + else { char *nss = strdup(args[4]); - if (!nss) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "cannot allocate memory for new string.\n"; - appctx->st0 = CLI_ST_PRINT; - } else { + if (!nss) + cli_err(appctx, "cannot allocate memory for new string.\n"); + else { free(sv->agent.send_string); sv->agent.send_string = nss; sv->agent.send_string_len = strlen(args[4]); @@ -4757,36 +4722,26 @@ static int cli_parse_set_server(char **args, char *payload, struct appctx *appct else if (strcmp(args[3], "check-port") == 0) { int i = 0; if (strl2irc(args[4], strlen(args[4]), &i) != 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'set server check-port' expects an integer as argument.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "'set server check-port' expects an integer as argument.\n"); goto out_unlock; } if ((i < 0) || (i > 65535)) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "provided port is not valid.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "provided port is not valid.\n"); goto out_unlock; } /* prevent the update of port to 0 if MAPPORTS are in use */ if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "can't unset 'port' since MAPPORTS is in use.\n"); goto out_unlock; } sv->check.port = i; - appctx->ctx.cli.severity = LOG_NOTICE; - appctx->ctx.cli.msg = "health check port updated.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_msg(appctx, LOG_NOTICE, "health check port updated.\n"); } else if (strcmp(args[3], "addr") == 0) { char *addr = NULL; char *port = NULL; if (strlen(args[4]) == 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "set server / addr requires an address and optionally a port.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "set server / addr requires an address and optionally a port.\n"); goto out_unlock; } else { @@ -4796,31 +4751,23 @@ static int cli_parse_set_server(char **args, char *payload, struct appctx *appct port = args[6]; } warning = update_server_addr_port(sv, addr, port, "stats socket command"); - if (warning) { - appctx->ctx.cli.severity = LOG_WARNING; - appctx->ctx.cli.msg = warning; - appctx->st0 = CLI_ST_PRINT; - } + if (warning) + cli_msg(appctx, LOG_WARNING, warning); srv_clr_admin_flag(sv, SRV_ADMF_RMAINT); } else if (strcmp(args[3], "fqdn") == 0) { if (!*args[4]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "set server / fqdn requires a FQDN.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "set server / fqdn requires a FQDN.\n"); goto out_unlock; } warning = update_server_fqdn(sv, args[4], "stats socket command", 0); - if (warning) { - appctx->ctx.cli.severity = LOG_WARNING; - appctx->ctx.cli.msg = warning; - appctx->st0 = CLI_ST_PRINT; - } + if (warning) + cli_msg(appctx, LOG_WARNING, warning); } else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'set server ' only supports 'agent', 'health', 'state', 'weight', 'addr', 'fqdn' and 'check-port'.\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, + "'set server ' only supports 'agent', 'health', 'state'," + " 'weight', 'addr', 'fqdn' and 'check-port'.\n"); } out_unlock: HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); @@ -4842,19 +4789,11 @@ static int cli_parse_get_weight(char **args, char *payload, struct appctx *appct break; } - if (!*line) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Require 'backend/server'.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*line) + return cli_err(appctx, "Require 'backend/server'.\n"); - if (!get_backend_server(args[2], line, &px, &sv)) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!get_backend_server(args[2], line, &px, &sv)) + return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n"); /* return server's effective weight at the moment */ snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight, @@ -4885,11 +4824,8 @@ static int cli_parse_set_weight(char **args, char *payload, struct appctx *appct HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); warning = server_parse_weight_change_request(sv, args[3]); - if (warning) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = warning; - appctx->st0 = CLI_ST_PRINT; - } + if (warning) + cli_err(appctx, warning); HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); @@ -4915,11 +4851,8 @@ static int cli_parse_set_maxconn_server(char **args, char *payload, struct appct HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); warning = server_parse_maxconn_change_request(sv, args[4]); - if (warning) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = warning; - appctx->st0 = CLI_ST_PRINT; - } + if (warning) + cli_err(appctx, warning); HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); @@ -5004,12 +4937,8 @@ static int cli_parse_enable_agent(char **args, char *payload, struct appctx *app if (!sv) return 1; - if (!(sv->agent.state & CHK_ST_CONFIGURED)) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!(sv->agent.state & CHK_ST_CONFIGURED)) + return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n"); HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); sv->agent.state |= CHK_ST_ENABLED; diff --git a/src/ssl_sock.c b/src/ssl_sock.c index a95ff65b6..06da1d921 100644 --- a/src/ssl_sock.c +++ b/src/ssl_sock.c @@ -9376,12 +9376,8 @@ static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *app appctx->ctx.cli.i0 = 1; } else { appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); - if (!appctx->ctx.cli.p0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'show tls-keys' unable to locate referenced filename\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!appctx->ctx.cli.p0) + return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n"); } appctx->io_handler = cli_io_handler_tlskeys_entries; return 0; @@ -9393,41 +9389,22 @@ static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appc int ret; /* Expect two parameters: the filename and the new new TLS key in encoding */ - if (!*args[3] || !*args[4]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*args[3] || !*args[4]) + return cli_err(appctx, "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n"); ref = tlskeys_ref_lookup_ref(args[3]); - if (!ref) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!ref) + return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n"); ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); - if (ret < 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (ret < 0) + return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n"); trash.data = ret; - if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'set ssl tls-key' received a key of wrong size.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } - appctx->ctx.cli.severity = LOG_INFO; - appctx->ctx.cli.msg = "TLS ticket key updated!\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; + if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) + return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n"); + return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); } #endif @@ -9441,12 +9418,8 @@ static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx payload = args[3]; /* Expect one parameter: the new response in base64 encoding */ - if (!*payload) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*payload) + return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n"); /* remove \r and \n from the payload */ for (i = 0, j = 0; payload[i]; i++) { @@ -9457,36 +9430,20 @@ static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx payload[j] = 0; ret = base64dec(payload, j, trash.area, trash.size); - if (ret < 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (ret < 0) + return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n"); trash.data = ret; if (ssl_sock_update_ocsp_response(&trash, &err)) { - if (err) { - memprintf(&err, "%s.\n", err); - appctx->ctx.cli.err = err; - appctx->st0 = CLI_ST_PRINT_FREE; - } - else { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Failed to update OCSP response.\n"; - appctx->st0 = CLI_ST_PRINT; - } - return 1; + if (err) + return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); + else + return cli_err(appctx, "Failed to update OCSP response.\n"); } - appctx->ctx.cli.severity = LOG_INFO; - appctx->ctx.cli.msg = "OCSP Response updated!\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; + + return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n"); #else - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; + return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n"); #endif } diff --git a/src/stats.c b/src/stats.c index 674416324..01fcb4586 100644 --- a/src/stats.c +++ b/src/stats.c @@ -3783,12 +3783,8 @@ static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx else appctx->ctx.stats.iid = atoi(args[2]); - if (!appctx->ctx.stats.iid) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "No such proxy.\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!appctx->ctx.stats.iid) + return cli_err(appctx, "No such proxy.\n"); appctx->ctx.stats.flags |= STAT_BOUND; appctx->ctx.stats.type = atoi(args[3]); diff --git a/src/stick_table.c b/src/stick_table.c index 746cda33d..fe5af7ead 100644 --- a/src/stick_table.c +++ b/src/stick_table.c @@ -3388,12 +3388,8 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args) void *ptr; struct freq_ctr_period *frqp; - if (!*args[4]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Key value expected\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*args[4]) + return cli_err(appctx, "Key value expected\n"); switch (t->type) { case SMP_T_IPV4: @@ -3412,12 +3408,8 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args) val = strtoul(args[4], &endptr, 10); if ((errno == ERANGE && val == ULONG_MAX) || (errno != 0 && val == 0) || endptr == args[4] || - val > 0xffffffff) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Invalid key\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + val > 0xffffffff) + return cli_err(appctx, "Invalid key\n"); uint32_key = (uint32_t) val; static_table_key.key = &uint32_key; break; @@ -3430,24 +3422,14 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args) default: switch (appctx->ctx.table.action) { case STK_CLI_ACT_SHOW: - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n"; - break; + return cli_err(appctx, "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n"); case STK_CLI_ACT_CLR: - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Removing keys from tables of type other than ip, ipv6, string and integer is not supported\n"; - break; + return cli_err(appctx, "Removing keys from tables of type other than ip, ipv6, string and integer is not supported\n"); case STK_CLI_ACT_SET: - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Inserting keys into tables of type other than ip, ipv6, string and integer is not supported\n"; - break; + return cli_err(appctx, "Inserting keys into tables of type other than ip, ipv6, string and integer is not supported\n"); default: - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown action\n"; - break; + return cli_err(appctx, "Unknown action\n"); } - appctx->st0 = CLI_ST_PRINT; - return 1; } /* check permissions */ @@ -3481,30 +3463,20 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args) if (!stksess_kill(t, ts, 1)) { /* don't delete an entry which is currently referenced */ - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; + return cli_err(appctx, "Entry currently in use, cannot remove\n"); } - break; case STK_CLI_ACT_SET: ts = stktable_get_entry(t, &static_table_key); if (!ts) { /* don't delete an entry which is currently referenced */ - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unable to allocate a new entry\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; + return cli_err(appctx, "Unable to allocate a new entry\n"); } - HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) { if (strncmp(args[cur_arg], "data.", 5) != 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "\"data.\" followed by a value expected\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "\"data.\" followed by a value expected\n"); HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); stktable_touch_local(t, ts, 1); return 1; @@ -3512,27 +3484,21 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args) data_type = stktable_get_data_type(args[cur_arg] + 5); if (data_type < 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown data type\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "Unknown data type\n"); HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); stktable_touch_local(t, ts, 1); return 1; } if (!t->data_ofs[data_type]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Data type not stored in this table\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "Data type not stored in this table\n"); HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); stktable_touch_local(t, ts, 1); return 1; } if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Require a valid integer value to store\n"; - appctx->st0 = CLI_ST_PRINT; + cli_err(appctx, "Require a valid integer value to store\n"); HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); stktable_touch_local(t, ts, 1); return 1; @@ -3572,10 +3538,7 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args) break; default: - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown action\n"; - appctx->st0 = CLI_ST_PRINT; - break; + return cli_err(appctx, "Unknown action\n"); } return 1; } @@ -3585,44 +3548,24 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args) */ static int table_prepare_data_request(struct appctx *appctx, char **args) { - if (appctx->ctx.table.action != STK_CLI_ACT_SHOW && appctx->ctx.table.action != STK_CLI_ACT_CLR) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (appctx->ctx.table.action != STK_CLI_ACT_SHOW && appctx->ctx.table.action != STK_CLI_ACT_CLR) + return cli_err(appctx, "content-based lookup is only supported with the \"show\" and \"clear\" actions\n"); /* condition on stored data value */ appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5); - if (appctx->ctx.table.data_type < 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown data type\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (appctx->ctx.table.data_type < 0) + return cli_err(appctx, "Unknown data type\n"); if (!((struct proxy *)appctx->ctx.table.target)->table || - !((struct proxy *)appctx->ctx.table.target)->table->data_ofs[appctx->ctx.table.data_type]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Data type not stored in this table\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + !((struct proxy *)appctx->ctx.table.target)->table->data_ofs[appctx->ctx.table.data_type]) + return cli_err(appctx, "Data type not stored in this table\n"); appctx->ctx.table.data_op = get_std_op(args[4]); - if (appctx->ctx.table.data_op < 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (appctx->ctx.table.data_op < 0) + return cli_err(appctx, "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n"); - if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Require a valid integer value to compare against\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) + return cli_err(appctx, "Require a valid integer value to compare against\n"); /* OK we're done, all the fields are set */ return 0; @@ -3638,12 +3581,8 @@ static int cli_parse_table_req(char **args, char *payload, struct appctx *appctx if (*args[2]) { appctx->ctx.table.target = stktable_find_by_name(args[2]); - if (!appctx->ctx.table.target) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "No such table\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!appctx->ctx.table.target) + return cli_err(appctx, "No such table\n"); } else { if (appctx->ctx.table.action != STK_CLI_ACT_SHOW) @@ -3663,24 +3602,14 @@ static int cli_parse_table_req(char **args, char *payload, struct appctx *appctx err_args: switch (appctx->ctx.table.action) { case STK_CLI_ACT_SHOW: - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Optional argument only supports \"data.\" and key \n"; - break; + return cli_err(appctx, "Optional argument only supports \"data.\" and key \n"); case STK_CLI_ACT_CLR: - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Required arguments: \"data.\" or
key \n"; - break; + return cli_err(appctx, "Required arguments:
\"data.\" or
key \n"); case STK_CLI_ACT_SET: - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Required arguments:
key [data. ]*\n"; - break; + return cli_err(appctx, "Required arguments:
key [data. ]*\n"); default: - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Unknown action\n"; - break; + return cli_err(appctx, "Unknown action\n"); } - appctx->st0 = CLI_ST_PRINT; - return 1; } /* This function is used to deal with table operations (dump or clear depending diff --git a/src/stream.c b/src/stream.c index 913de6389..095565a81 100644 --- a/src/stream.c +++ b/src/stream.c @@ -3624,12 +3624,8 @@ static int cli_parse_shutdown_session(char **args, char *payload, struct appctx if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) return 1; - if (!*args[2]) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (!*args[2]) + return cli_err(appctx, "Session pointer expected (use 'show sess').\n"); ptr = (void *)strtoul(args[2], NULL, 0); @@ -3640,12 +3636,8 @@ static int cli_parse_shutdown_session(char **args, char *payload, struct appctx } /* do we have the stream ? */ - if (strm != ptr) { - appctx->ctx.cli.severity = LOG_ERR; - appctx->ctx.cli.msg = "No such session (use 'show sess').\n"; - appctx->st0 = CLI_ST_PRINT; - return 1; - } + if (strm != ptr) + return cli_err(appctx, "No such session (use 'show sess').\n"); stream_shutdown(strm, SF_ERR_KILLED); return 1;