From d6ccd1738bae7d56697371fdc09dd3882337e003 Mon Sep 17 00:00:00 2001 From: Valentine Krasnobaeva Date: Tue, 19 Nov 2024 14:22:08 +0100 Subject: [PATCH] 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). --- src/cfgparse-global.c | 1 - src/haproxy.c | 10 ++++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/cfgparse-global.c b/src/cfgparse-global.c index c0056458ad..77afe6f652 100644 --- a/src/cfgparse-global.c +++ b/src/cfgparse-global.c @@ -845,7 +845,6 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm) err_code |= ERR_ALERT | ERR_FATAL; goto out; } - setenv("HAPROXY_LOCALPEER", localpeer, 1); } else if (strcmp(args[0], "numa-cpu-mapping") == 0) { global.numa_cpu_mapping = (kwm == KWM_NO) ? 0 : 1; diff --git a/src/haproxy.c b/src/haproxy.c index 6f371cf2b7..063ec5cb98 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -1573,7 +1573,7 @@ static void init_early(int argc, char **argv) /* preset some environment variables */ localpeer = strdup(hostname); - if (!localpeer || setenv("HAPROXY_LOCALPEER", localpeer, 1) < 0) { + if (!localpeer) { ha_alert("Cannot allocate memory for local peer.\n"); exit(EXIT_FAILURE); } @@ -1874,7 +1874,6 @@ static void init_args(int argc, char **argv) ha_alert("Cannot allocate memory for local peer.\n"); exit(EXIT_FAILURE); } - setenv("HAPROXY_LOCALPEER", localpeer, 1); global.localpeer_cmdline = 1; break; case 'f' : @@ -3840,6 +3839,13 @@ int main(int argc, char **argv) /* all sections have been parsed, we can free the content */ list_for_each_entry_safe(cfg, cfg_tmp, &cfg_cfgfiles, list) 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); }