]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: startup: set HAPROXY_LOCALPEER only once
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Tue, 19 Nov 2024 13:22:08 +0000 (14:22 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 20 Nov 2024 14:44:10 +0000 (15:44 +0100)
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
src/haproxy.c

index c0056458adb7df519687bbf03f8f207e555270a2..77afe6f6527bb01ab0044b9a1886083fead8d382 100644 (file)
@@ -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;
index 6f371cf2b77664239a98a999321d9209581b6eac..063ec5cb98d9ee292fa7cf2381494dabd67cf95c 100644 (file)
@@ -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);
        }