Skip to content

Commit d6ccd17

Browse files
einval22wtarreau
authored andcommitted
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).
1 parent 1171a23 commit d6ccd17

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/cfgparse-global.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,6 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
845845
err_code |= ERR_ALERT | ERR_FATAL;
846846
goto out;
847847
}
848-
setenv("HAPROXY_LOCALPEER", localpeer, 1);
849848
}
850849
else if (strcmp(args[0], "numa-cpu-mapping") == 0) {
851850
global.numa_cpu_mapping = (kwm == KWM_NO) ? 0 : 1;

src/haproxy.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,7 @@ static void init_early(int argc, char **argv)
15731573

15741574
/* preset some environment variables */
15751575
localpeer = strdup(hostname);
1576-
if (!localpeer || setenv("HAPROXY_LOCALPEER", localpeer, 1) < 0) {
1576+
if (!localpeer) {
15771577
ha_alert("Cannot allocate memory for local peer.\n");
15781578
exit(EXIT_FAILURE);
15791579
}
@@ -1874,7 +1874,6 @@ static void init_args(int argc, char **argv)
18741874
ha_alert("Cannot allocate memory for local peer.\n");
18751875
exit(EXIT_FAILURE);
18761876
}
1877-
setenv("HAPROXY_LOCALPEER", localpeer, 1);
18781877
global.localpeer_cmdline = 1;
18791878
break;
18801879
case 'f' :
@@ -3840,6 +3839,13 @@ int main(int argc, char **argv)
38403839
/* all sections have been parsed, we can free the content */
38413840
list_for_each_entry_safe(cfg, cfg_tmp, &cfg_cfgfiles, list)
38423841
ha_free(&cfg->content);
3842+
3843+
/* localpeer could be redefined via 'localpeer' keyword from the
3844+
* global section, in master-worker mode it's parsed only by
3845+
* worker, so let set HAPROXY_LOCALPEER explicitly here
3846+
*/
3847+
if (localpeer != NULL)
3848+
setenv("HAPROXY_LOCALPEER", localpeer, 1);
38433849
usermsgs_clr(NULL);
38443850
}
38453851

0 commit comments

Comments
 (0)