REORG: init: encapsulate code that reads cfg files

Haproxy master process should not read its configuration the second time
after performing reexec and passing to MODE_MWORKER_WAIT. So, to make
this part of init() function more readable and to distinguish better the
point, where configs have been read, let's encapsulate it in a separate
function.
This commit is contained in:
Valentine Krasnobaeva 2024-06-26 17:03:04 +02:00 committed by William Lallemand
parent 5e06d45df7
commit ed90ad895c

View File

@ -1167,6 +1167,82 @@ static void cfgfiles_expand_directories(void)
free(err);
}
/* Reads config files. Terminates process with exit(1), if we are luck of RAM,
* 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.
*/
static int read_cfg(char *progname)
{
char *env_cfgfiles = NULL;
int env_err = 0;
struct wordlist *wl;
int err_code = 0;
/* handle cfgfiles that are actually directories */
cfgfiles_expand_directories();
if (LIST_ISEMPTY(&cfg_cfgfiles))
usage(progname);
/* temporary create environment variables with default
* values to ease user configuration. Do not forget to
* unset them after the list_for_each_entry loop.
*/
setenv("HAPROXY_HTTP_LOG_FMT", default_http_log_format, 1);
setenv("HAPROXY_HTTPS_LOG_FMT", default_https_log_format, 1);
setenv("HAPROXY_TCP_LOG_FMT", default_tcp_log_format, 1);
setenv("HAPROXY_BRANCH", PRODUCT_BRANCH, 1);
list_for_each_entry(wl, &cfg_cfgfiles, list) {
int ret;
if (env_err == 0) {
if (!memprintf(&env_cfgfiles, "%s%s%s",
(env_cfgfiles ? env_cfgfiles : ""),
(env_cfgfiles ? ";" : ""), wl->s))
env_err = 1;
}
ret = readcfgfile(wl->s);
if (ret == -1) {
ha_alert("Could not open configuration file %s : %s\n",
wl->s, strerror(errno));
free(env_cfgfiles);
exit(1);
}
if (ret & (ERR_ABORT|ERR_FATAL))
ha_alert("Error(s) found in configuration file : %s\n", wl->s);
err_code |= ret;
if (err_code & ERR_ABORT) {
free(env_cfgfiles);
exit(1);
}
}
/* remove temporary environment variables. */
unsetenv("HAPROXY_BRANCH");
unsetenv("HAPROXY_HTTP_LOG_FMT");
unsetenv("HAPROXY_HTTPS_LOG_FMT");
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.
*/
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);
}
setenv("HAPROXY_CFGFILES", env_cfgfiles, 1);
free(env_cfgfiles);
return err_code;
}
/*
* copy and cleanup the current argv
* Remove the -sf /-st / -x parameters
@ -2076,7 +2152,6 @@ static void init(int argc, char **argv)
{
char *progname = global.log_tag.area;
int err_code = 0;
struct wordlist *wl;
struct proxy *px;
struct post_check_fct *pcf;
struct pre_check_fct *prcf;
@ -2149,72 +2224,9 @@ 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)) {
char *env_cfgfiles = NULL;
int env_err = 0;
if (!(global.mode & MODE_MWORKER_WAIT))
read_cfg(progname);
/* handle cfgfiles that are actually directories */
cfgfiles_expand_directories();
if (LIST_ISEMPTY(&cfg_cfgfiles))
usage(progname);
/* temporary create environment variables with default
* values to ease user configuration. Do not forget to
* unset them after the list_for_each_entry loop.
*/
setenv("HAPROXY_HTTP_LOG_FMT", default_http_log_format, 1);
setenv("HAPROXY_HTTPS_LOG_FMT", default_https_log_format, 1);
setenv("HAPROXY_TCP_LOG_FMT", default_tcp_log_format, 1);
setenv("HAPROXY_BRANCH", PRODUCT_BRANCH, 1);
list_for_each_entry(wl, &cfg_cfgfiles, list) {
int ret;
if (env_err == 0) {
if (!memprintf(&env_cfgfiles, "%s%s%s",
(env_cfgfiles ? env_cfgfiles : ""),
(env_cfgfiles ? ";" : ""), wl->s))
env_err = 1;
}
ret = readcfgfile(wl->s);
if (ret == -1) {
ha_alert("Could not open configuration file %s : %s\n",
wl->s, strerror(errno));
free(env_cfgfiles);
exit(1);
}
if (ret & (ERR_ABORT|ERR_FATAL))
ha_alert("Error(s) found in configuration file : %s\n", wl->s);
err_code |= ret;
if (err_code & ERR_ABORT) {
free(env_cfgfiles);
exit(1);
}
}
/* remove temporary environment variables. */
unsetenv("HAPROXY_BRANCH");
unsetenv("HAPROXY_HTTP_LOG_FMT");
unsetenv("HAPROXY_HTTPS_LOG_FMT");
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.
*/
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);
}
setenv("HAPROXY_CFGFILES", env_cfgfiles, 1);
free(env_cfgfiles);
}
if (global.mode & MODE_MWORKER) {
struct mworker_proc *tmproc;