From c8d02def1178e9df778439845e6599fb1fb30bd5 Mon Sep 17 00:00:00 2001 From: Valentine Krasnobaeva Date: Tue, 4 Mar 2025 11:04:01 +0100 Subject: [PATCH] BUG/MEIDUM: startup: return to initial cwd only after check_config_validity() In check_config_validity() we evaluate some sample fetch expressions (log-format, server rules, etc). These expressions may use external files like maps. If some particular 'default-path' was set in the global section before, it's no longer applied to resolve file pathes in check_config_validity(). parse_cfg() at the end of config parsing switches back to the initial cwd. This fixes the issue #2886. This patch should be backported in all stable versions since 2.4.0, including 2.4.0. (cherry picked from commit e900ef987e4167cf0921e97b09059d757f2c0ea5) Signed-off-by: Willy Tarreau (cherry picked from commit 4d7fe11b70df072007eaa1633061e3581b9618fc) Signed-off-by: Amaury Denoyelle --- include/haproxy/cfgparse.h | 1 + src/cfgparse.c | 7 +------ src/haproxy.c | 4 ++++ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/haproxy/cfgparse.h b/include/haproxy/cfgparse.h index 3a769d5..f1712bc 100644 --- a/include/haproxy/cfgparse.h +++ b/include/haproxy/cfgparse.h @@ -99,6 +99,7 @@ extern char *cursection; extern int non_global_section_parsed; extern struct proxy *curproxy; +extern char initial_cwd[PATH_MAX]; int cfg_parse_global(const char *file, int linenum, char **args, int inv); int cfg_parse_listen(const char *file, int linenum, char **args, int inv); diff --git a/src/cfgparse.c b/src/cfgparse.c index 53b4c7a..edb9747 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -119,7 +119,7 @@ static enum default_path_mode { DEFAULT_PATH_ORIGIN, /* "origin": paths are relative to default_path_origin */ } default_path_mode; -static char initial_cwd[PATH_MAX]; +char initial_cwd[PATH_MAX]; static char current_cwd[PATH_MAX]; /* List head of all known configuration keywords */ @@ -2525,11 +2525,6 @@ next_line: err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT; } - if (*initial_cwd && chdir(initial_cwd) == -1) { - ha_alert("Impossible to get back to initial directory '%s' : %s\n", initial_cwd, strerror(errno)); - err_code |= ERR_ALERT | ERR_FATAL; - } - err: ha_free(&cfg_scope); cursection = NULL; diff --git a/src/haproxy.c b/src/haproxy.c index 04595d1..0ef9e68 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -2357,6 +2357,10 @@ static void init(int argc, char **argv) /* Note: global.nbthread will be initialized as part of this call */ err_code |= check_config_validity(); + if (*initial_cwd && chdir(initial_cwd) == -1) { + ha_alert("Impossible to get back to initial directory '%s' : %s\n", initial_cwd, strerror(errno)); + exit(1); + } /* update the ready date to also account for the check time */ clock_update_date(0, 1); -- 1.7.10.4