From c9a82e48bfd2a8af8b8025c012910c9c56b9cc87 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sat, 26 Jan 2019 13:25:14 +0100 Subject: [PATCH] MINOR: cfgparse: make the process/thread parser support a maximum value It was hard-wired to LONGBITS, let's make it configurable depending on the context (threads, processes). --- include/common/cfgparse.h | 2 +- src/cfgparse-global.c | 4 ++-- src/cfgparse-listen.c | 2 +- src/cfgparse.c | 11 ++++++----- src/cli.c | 2 +- src/listener.c | 4 ++-- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/include/common/cfgparse.h b/include/common/cfgparse.h index b46e02b99..1a288166a 100644 --- a/include/common/cfgparse.h +++ b/include/common/cfgparse.h @@ -115,7 +115,7 @@ int too_many_args_idx(int maxarg, int index, char **args, char **msg, int *err_c int too_many_args(int maxarg, char **args, char **msg, int *err_code); int alertif_too_many_args_idx(int maxarg, int index, const char *file, int linenum, char **args, int *err_code); int alertif_too_many_args(int maxarg, const char *file, int linenum, char **args, int *err_code); -int parse_process_number(const char *arg, unsigned long *proc, int *autoinc, char **err); +int parse_process_number(const char *arg, unsigned long *proc, int max, int *autoinc, char **err); unsigned long parse_cpu_set(const char **args, unsigned long *cpu_set, char **err); void free_email_alert(struct proxy *p); diff --git a/src/cfgparse-global.c b/src/cfgparse-global.c index 5a92d9b31..2cdcf7661 100644 --- a/src/cfgparse-global.c +++ b/src/cfgparse-global.c @@ -949,14 +949,14 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm) if ((slash = strchr(args[1], '/')) != NULL) *slash = 0; - if (parse_process_number(args[1], &proc, &autoinc, &errmsg)) { + if (parse_process_number(args[1], &proc, LONGBITS, &autoinc, &errmsg)) { ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); err_code |= ERR_ALERT | ERR_FATAL; goto out; } if (slash) { - if (parse_process_number(slash+1, &thread, NULL, &errmsg)) { + if (parse_process_number(slash+1, &thread, MAX_THREADS, NULL, &errmsg)) { ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); err_code |= ERR_ALERT | ERR_FATAL; goto out; diff --git a/src/cfgparse-listen.c b/src/cfgparse-listen.c index 2a7ed5337..74218b5e6 100644 --- a/src/cfgparse-listen.c +++ b/src/cfgparse-listen.c @@ -922,7 +922,7 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm) set = 0; break; } - if (parse_process_number(args[cur_arg], &set, NULL, &errmsg)) { + if (parse_process_number(args[cur_arg], &set, LONGBITS, NULL, &errmsg)) { ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); err_code |= ERR_ALERT | ERR_FATAL; goto out; diff --git a/src/cfgparse.c b/src/cfgparse.c index 05b6d9e58..50f43cde5 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -345,14 +345,14 @@ int warnif_cond_conflicts(const struct acl_cond *cond, unsigned int where, const } /* Parse a string representing a process number or a set of processes. It must - * be "all", "odd", "even", a number between 1 and or a range with + * be "all", "odd", "even", a number between 1 and or a range with * two such numbers delimited by a dash ('-'). On success, it returns * 0. otherwise it returns 1 with an error message in . * * Note: this function can also be used to parse a thread number or a set of * threads. */ -int parse_process_number(const char *arg, unsigned long *proc, int *autoinc, char **err) +int parse_process_number(const char *arg, unsigned long *proc, int max, int *autoinc, char **err) { if (autoinc) { *autoinc = 0; @@ -379,7 +379,7 @@ int parse_process_number(const char *arg, unsigned long *proc, int *autoinc, cha low = high = str2uic(arg); if ((dash = strchr(arg, '-')) != NULL) - high = ((!*(dash+1)) ? LONGBITS : str2uic(dash + 1)); + high = ((!*(dash+1)) ? max : str2uic(dash + 1)); if (high < low) { unsigned int swap = low; @@ -387,16 +387,17 @@ int parse_process_number(const char *arg, unsigned long *proc, int *autoinc, cha high = swap; } - if (low < 1 || low > LONGBITS || high > LONGBITS) { + if (low < 1 || low > max || high > max) { memprintf(err, "'%s' is not a valid number/range." " It supports numbers from 1 to %d.\n", - arg, LONGBITS); + arg, max); return 1; } for (;low <= high; low++) *proc |= 1UL << (low-1); } + *proc &= ~0UL >> (LONGBITS - max); return 0; } diff --git a/src/cli.c b/src/cli.c index 687796fa5..d1d1e9625 100644 --- a/src/cli.c +++ b/src/cli.c @@ -359,7 +359,7 @@ static int stats_parse_global(char **args, int section_type, struct proxy *curpx set = 0; break; } - if (parse_process_number(args[cur_arg], &set, NULL, err)) { + if (parse_process_number(args[cur_arg], &set, LONGBITS, NULL, err)) { memprintf(err, "'%s %s' : %s", args[0], args[1], *err); return -1; } diff --git a/src/listener.c b/src/listener.c index 2c885e998..9a79e6aba 100644 --- a/src/listener.c +++ b/src/listener.c @@ -961,13 +961,13 @@ static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct if ((slash = strchr(args[cur_arg + 1], '/')) != NULL) *slash = 0; - if (parse_process_number(args[cur_arg + 1], &proc, NULL, err)) { + if (parse_process_number(args[cur_arg + 1], &proc, LONGBITS, NULL, err)) { memprintf(err, "'%s' : %s", args[cur_arg], *err); return ERR_ALERT | ERR_FATAL; } if (slash) { - if (parse_process_number(slash+1, &thread, NULL, err)) { + if (parse_process_number(slash+1, &thread, MAX_THREADS, NULL, err)) { memprintf(err, "'%s' : %s", args[cur_arg], *err); return ERR_ALERT | ERR_FATAL; }