MEDIUM: startup: make read_cfg() return immediately on ENOMEM

This commit prepares read_cfg() to call load_cfg_in_mem() helper in order to
load configuration files in memory. Before, read_cfg() calls the parser for all
files from cfg_cfgfiles list and cumulates parser's errors and memprintf's
errors in for_each loop. memprintf's errors did not stop this loop and were
accounted just after.

Now, as we plan to load configuration files in memory, we stop the loop, if
memprintf() fails, and we show appropraite error message with ha_alert. Then
process terminates. So not all cumulated syntax-related errors will be shown
before exit in this case and we has to stop, because we run out of memory.

If we can't open the current file or we fail to allocate a memory to store
some configuration line, the previous behaviour is kept, process emits
appropriate alert message and exits.

If parser returns some syntax-related error on the current file, the previous
behaviour is kept as well. We cumulate such errors for all parsed files and we
check them just after the loop. All syntax-related errors for all files is
shown then  as before in ha_alert messages line by line during the startup.
Then process will exit with 1.

As now cfg_cfgfiles list contains many pointers to some memory areas with
configuration files content and this content could be big, it's better to
free the list explicitly, when parsing was finished. So, let's change
read_cfg() to return some integer value to its caller init(), and let's perform
the free  routine at a caller level, as cfg_cfgfiles list was initialized and
initially filled at this level.
This commit is contained in:
Valentine Krasnobaeva 2024-08-05 10:03:52 +02:00 committed by Willy Tarreau
parent 007f7f2f02
commit 2bb34edb0b

View File

@ -1166,15 +1166,14 @@ next_dir_entry:
free(err);
}
/* Reads config files. Terminates process with exit(1), if we are luck of RAM,
/* Reads config files. Returns -1, if we are run out of memory,
* couldn't open provided file(s) or parser has detected some fatal error.
* Otherwise, returns an err_code, which may contain 0 (OK) or ERR_WARN,
* ERR_ALERT. It could be used in further initialization stages.
* ERR_ALERT. It is used in further initialization stages.
*/
static int read_cfg(char *progname)
{
char *env_cfgfiles = NULL;
int env_err = 0;
struct cfgfile *cfg;
int err_code = 0;
@ -1195,27 +1194,23 @@ static int read_cfg(char *progname)
list_for_each_entry(cfg, &cfg_cfgfiles, list) {
int ret;
if (env_err == 0) {
if (!memprintf(&env_cfgfiles, "%s%s%s",
(env_cfgfiles ? env_cfgfiles : ""),
(env_cfgfiles ? ";" : ""), cfg->filename))
env_err = 1;
(env_cfgfiles ? ";" : ""), cfg->filename)) {
/* free what we've already allocated and free cfglist */
ha_alert("Could not allocate memory for HAPROXY_CFGFILES env variable\n");
goto err;
}
ret = readcfgfile(cfg->filename);
if (ret == -1) {
ha_alert("Could not open configuration file %s : %s\n",
cfg->filename, strerror(errno));
free(env_cfgfiles);
exit(1);
}
if (ret == -1)
goto err;
if (ret & (ERR_ABORT|ERR_FATAL))
ha_alert("Error(s) found in configuration file : %s\n", cfg->filename);
err_code |= ret;
if (err_code & ERR_ABORT) {
free(env_cfgfiles);
exit(1);
}
if (err_code & ERR_ABORT)
goto err;
}
/* remove temporary environment variables. */
unsetenv("HAPROXY_BRANCH");
@ -1224,22 +1219,20 @@ static int read_cfg(char *progname)
unsetenv("HAPROXY_TCP_LOG_FMT");
/* do not try to resolve arguments nor to spot inconsistencies when
* the configuration contains fatal errors caused by files not found
* or failed memory allocations.
* the configuration contains fatal errors.
*/
if (err_code & (ERR_ABORT|ERR_FATAL)) {
ha_alert("Fatal errors found in configuration.\n");
free(env_cfgfiles);
exit(1);
}
if (env_err) {
ha_alert("Could not allocate memory for HAPROXY_CFGFILES env variable\n");
exit(1);
goto err;
}
setenv("HAPROXY_CFGFILES", env_cfgfiles, 1);
free(env_cfgfiles);
return err_code;
err:
free(env_cfgfiles);
return -1;
}
/*
@ -1979,9 +1972,11 @@ static void init(int argc, char **argv)
{
char *progname = global.log_tag.area;
int err_code = 0;
int ret = 0;
struct proxy *px;
struct post_check_fct *pcf;
struct pre_check_fct *prcf;
struct cfgfile *cfg, *cfg_tmp;
int ideal_maxconn;
const char *cc, *cflags, *opts;
@ -2051,8 +2046,14 @@ static void init(int argc, char **argv)
usermsgs_clr("config");
/* in wait mode, we don't try to read the configuration files */
if (!(global.mode & MODE_MWORKER_WAIT))
read_cfg(progname);
if (!(global.mode & MODE_MWORKER_WAIT)) {
ret = read_cfg(progname);
/* free memory to store config file content */
list_for_each_entry_safe(cfg, cfg_tmp, &cfg_cfgfiles, list)
ha_free(&cfg->content);
if (ret < 0)
exit(1);
}
if (global.mode & MODE_MWORKER) {
struct mworker_proc *tmproc;
@ -2767,7 +2768,6 @@ void deinit(void)
}
list_for_each_entry_safe(cfg, cfg_tmp, &cfg_cfgfiles, list) {
ha_free(&cfg->content);
ha_free(&cfg->filename);
LIST_DELETE(&cfg->list);
ha_free(&cfg);