MINOR: startup: set HAPROXY_LOCALPEER only once

Before this patch HAPROXY_LOCALPEER variable could be set in init_early(),
in init_args() and in cfg_parse_global(). In master-worker mode, if localpeer
keyword set in the global section, HAPROXY_LOCALPEER in the worker
environment is set to this keyword's value, but in the master environment it
still keeps the default, a localhost name. This is confusing.

To fix it, let's set HAPROXY_LOCALPEER only once, when a worker or process in a
standalone mode has finished to parse its configuration. And let's set this
variable only for the worker process or for the process in a standalone mode,
because the master doesn't need it.

HAPROXY_LOCALPEER takes the value saved in localpeer global variable, which is
always set by default in init_early() to the local hostname. Then, localpeer
could be reset in init_args (-L option) and in cfg_parse_global() (while
parsing "localpeer" keyword).
This commit is contained in:
Valentine Krasnobaeva 2024-11-19 14:22:08 +01:00 committed by Willy Tarreau
parent 1171a23aec
commit d6ccd1738b
2 changed files with 8 additions and 3 deletions

View File

@ -845,7 +845,6 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
err_code |= ERR_ALERT | ERR_FATAL; err_code |= ERR_ALERT | ERR_FATAL;
goto out; goto out;
} }
setenv("HAPROXY_LOCALPEER", localpeer, 1);
} }
else if (strcmp(args[0], "numa-cpu-mapping") == 0) { else if (strcmp(args[0], "numa-cpu-mapping") == 0) {
global.numa_cpu_mapping = (kwm == KWM_NO) ? 0 : 1; global.numa_cpu_mapping = (kwm == KWM_NO) ? 0 : 1;

View File

@ -1573,7 +1573,7 @@ static void init_early(int argc, char **argv)
/* preset some environment variables */ /* preset some environment variables */
localpeer = strdup(hostname); localpeer = strdup(hostname);
if (!localpeer || setenv("HAPROXY_LOCALPEER", localpeer, 1) < 0) { if (!localpeer) {
ha_alert("Cannot allocate memory for local peer.\n"); ha_alert("Cannot allocate memory for local peer.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -1874,7 +1874,6 @@ static void init_args(int argc, char **argv)
ha_alert("Cannot allocate memory for local peer.\n"); ha_alert("Cannot allocate memory for local peer.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
setenv("HAPROXY_LOCALPEER", localpeer, 1);
global.localpeer_cmdline = 1; global.localpeer_cmdline = 1;
break; break;
case 'f' : case 'f' :
@ -3840,6 +3839,13 @@ int main(int argc, char **argv)
/* all sections have been parsed, we can free the content */ /* all sections have been parsed, we can free the content */
list_for_each_entry_safe(cfg, cfg_tmp, &cfg_cfgfiles, list) list_for_each_entry_safe(cfg, cfg_tmp, &cfg_cfgfiles, list)
ha_free(&cfg->content); ha_free(&cfg->content);
/* localpeer could be redefined via 'localpeer' keyword from the
* global section, in master-worker mode it's parsed only by
* worker, so let set HAPROXY_LOCALPEER explicitly here
*/
if (localpeer != NULL)
setenv("HAPROXY_LOCALPEER", localpeer, 1);
usermsgs_clr(NULL); usermsgs_clr(NULL);
} }