REORG: init: encapsulate CHECK_CONDITION logic in a func

As MODE_CHECK_CONDITION logic terminates the process anyway, no matter if
the test for the provided condition was successfull or not, let's
encapsulate it in a separate function. This makes the code of init() more
readable.
This commit is contained in:
Valentine Krasnobaeva 2024-06-26 18:39:45 +02:00 committed by William Lallemand
parent 10de58fbfb
commit 6f613faa71

View File

@ -1539,6 +1539,71 @@ static int check_if_maxsock_permitted(int maxsock)
return ret == 0; return ret == 0;
} }
/* Evaluates a condition provided within a conditional block of the
* configuration. Makes process to exit with 0, if the condition is true, with
* 1, if the condition is false or with 2, if parse_line encounters an error.
*/
static void do_check_condition(char *progname)
{
int result;
uint32_t err;
const char *errptr;
char *errmsg = NULL;
char *args[MAX_LINE_ARGS+1];
int arg = sizeof(args) / sizeof(*args);
size_t outlen;
char *w;
if (!check_condition)
usage(progname);
outlen = strlen(check_condition) + 1;
err = parse_line(check_condition, check_condition, &outlen, args, &arg,
PARSE_OPT_ENV | PARSE_OPT_WORD_EXPAND | PARSE_OPT_DQUOTE | PARSE_OPT_SQUOTE | PARSE_OPT_BKSLASH,
&errptr);
if (err & PARSE_ERR_QUOTE) {
ha_alert("Syntax Error in condition: Unmatched quote.\n");
exit(2);
}
if (err & PARSE_ERR_HEX) {
ha_alert("Syntax Error in condition: Truncated or invalid hexadecimal sequence.\n");
exit(2);
}
if (err & (PARSE_ERR_TOOLARGE|PARSE_ERR_OVERLAP)) {
ha_alert("Error in condition: Line too long.\n");
exit(2);
}
if (err & PARSE_ERR_TOOMANY) {
ha_alert("Error in condition: Too many words.\n");
exit(2);
}
if (err) {
ha_alert("Unhandled error in condition, please report this to the developers.\n");
exit(2);
}
/* remerge all words into a single expression */
for (w = *args; (w += strlen(w)) < check_condition + outlen - 1; *w = ' ')
;
result = cfg_eval_condition(args, &errmsg, &errptr);
if (result < 0) {
if (errmsg)
ha_alert("Failed to evaluate condition: %s\n", errmsg);
exit(2);
}
exit(result ? 0 : 1);
}
/* This performs th every basic early initialization at the end of the PREPARE /* This performs th every basic early initialization at the end of the PREPARE
* init stage. It may only assume that list heads are initialized, but not that * init stage. It may only assume that list heads are initialized, but not that
* anything else is correct. It will initialize a number of variables that * anything else is correct. It will initialize a number of variables that
@ -2063,66 +2128,9 @@ static void init(int argc, char **argv)
global.mode &= ~MODE_MWORKER; global.mode &= ~MODE_MWORKER;
} }
if (global.mode & MODE_CHECK_CONDITION) { /* Do check_condition, if we started with -cc, and exit. */
int result; if (global.mode & MODE_CHECK_CONDITION)
do_check_condition(progname);
uint32_t err;
const char *errptr;
char *errmsg = NULL;
char *args[MAX_LINE_ARGS+1];
int arg = sizeof(args) / sizeof(*args);
size_t outlen;
char *w;
if (!check_condition)
usage(progname);
outlen = strlen(check_condition) + 1;
err = parse_line(check_condition, check_condition, &outlen, args, &arg,
PARSE_OPT_ENV | PARSE_OPT_WORD_EXPAND | PARSE_OPT_DQUOTE | PARSE_OPT_SQUOTE | PARSE_OPT_BKSLASH,
&errptr);
if (err & PARSE_ERR_QUOTE) {
ha_alert("Syntax Error in condition: Unmatched quote.\n");
exit(2);
}
if (err & PARSE_ERR_HEX) {
ha_alert("Syntax Error in condition: Truncated or invalid hexadecimal sequence.\n");
exit(2);
}
if (err & (PARSE_ERR_TOOLARGE|PARSE_ERR_OVERLAP)) {
ha_alert("Error in condition: Line too long.\n");
exit(2);
}
if (err & PARSE_ERR_TOOMANY) {
ha_alert("Error in condition: Too many words.\n");
exit(2);
}
if (err) {
ha_alert("Unhandled error in condition, please report this to the developers.\n");
exit(2);
}
/* remerge all words into a single expression */
for (w = *args; (w += strlen(w)) < check_condition + outlen - 1; *w = ' ')
;
result = cfg_eval_condition(args, &errmsg, &errptr);
if (result < 0) {
if (errmsg)
ha_alert("Failed to evaluate condition: %s\n", errmsg);
exit(2);
}
exit(result ? 0 : 1);
}
/* set the atexit functions when not doing configuration check */ /* set the atexit functions when not doing configuration check */
if (!(global.mode & MODE_CHECK) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) { if (!(global.mode & MODE_CHECK) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {