]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: init: encapsulate code that reads cfg files
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Wed, 26 Jun 2024 15:03:04 +0000 (17:03 +0200)
committerWilliam Lallemand <wlallemand@haproxy.com>
Thu, 27 Jun 2024 14:09:38 +0000 (16:09 +0200)
Haproxy master process should not read its configuration the second time
after performing reexec and passing to MODE_MWORKER_WAIT. So, to make
this part of init() function more readable and to distinguish better the
point, where configs have been read, let's encapsulate it in a separate
function.

src/haproxy.c

index 5443ed382f0b18d3f80a9f5f3deb7e17bda7fac7..faff4b0f6baf49e09bda07d07751f42001abb8aa 100644 (file)
@@ -1167,6 +1167,82 @@ next_dir_entry:
        free(err);
 }
 
+/* Reads config files. Terminates process with exit(1), if we are luck of RAM,
+ * couldn't open provided file(s) or parser has detected some fatal error.
+ * Otherwise, returns an err_code, which may contain 0 (OK) or ERR_WARN,
+ * ERR_ALERT. It could be used in further initialization stages.
+ */
+static int read_cfg(char *progname)
+{
+       char *env_cfgfiles = NULL;
+       int env_err = 0;
+       struct wordlist *wl;
+       int err_code = 0;
+
+       /* handle cfgfiles that are actually directories */
+       cfgfiles_expand_directories();
+
+       if (LIST_ISEMPTY(&cfg_cfgfiles))
+               usage(progname);
+
+       /* temporary create environment variables with default
+        * values to ease user configuration. Do not forget to
+        * unset them after the list_for_each_entry loop.
+        */
+       setenv("HAPROXY_HTTP_LOG_FMT", default_http_log_format, 1);
+       setenv("HAPROXY_HTTPS_LOG_FMT", default_https_log_format, 1);
+       setenv("HAPROXY_TCP_LOG_FMT", default_tcp_log_format, 1);
+       setenv("HAPROXY_BRANCH", PRODUCT_BRANCH, 1);
+       list_for_each_entry(wl, &cfg_cfgfiles, list) {
+               int ret;
+
+               if (env_err == 0) {
+                       if (!memprintf(&env_cfgfiles, "%s%s%s",
+                                          (env_cfgfiles ? env_cfgfiles : ""),
+                                          (env_cfgfiles ? ";" : ""), wl->s))
+                               env_err = 1;
+               }
+
+               ret = readcfgfile(wl->s);
+               if (ret == -1) {
+                       ha_alert("Could not open configuration file %s : %s\n",
+                                wl->s, strerror(errno));
+                       free(env_cfgfiles);
+                       exit(1);
+               }
+               if (ret & (ERR_ABORT|ERR_FATAL))
+                       ha_alert("Error(s) found in configuration file : %s\n", wl->s);
+               err_code |= ret;
+               if (err_code & ERR_ABORT) {
+                       free(env_cfgfiles);
+                       exit(1);
+               }
+       }
+       /* remove temporary environment variables. */
+       unsetenv("HAPROXY_BRANCH");
+       unsetenv("HAPROXY_HTTP_LOG_FMT");
+       unsetenv("HAPROXY_HTTPS_LOG_FMT");
+       unsetenv("HAPROXY_TCP_LOG_FMT");
+
+       /* do not try to resolve arguments nor to spot inconsistencies when
+        * the configuration contains fatal errors caused by files not found
+        * or failed memory allocations.
+        */
+       if (err_code & (ERR_ABORT|ERR_FATAL)) {
+               ha_alert("Fatal errors found in configuration.\n");
+               free(env_cfgfiles);
+               exit(1);
+       }
+       if (env_err) {
+               ha_alert("Could not allocate memory for HAPROXY_CFGFILES env variable\n");
+               exit(1);
+       }
+       setenv("HAPROXY_CFGFILES", env_cfgfiles, 1);
+       free(env_cfgfiles);
+
+       return err_code;
+}
+
 /*
  * copy and cleanup the current argv
  * Remove the -sf /-st / -x parameters
@@ -2076,7 +2152,6 @@ static void init(int argc, char **argv)
 {
        char *progname = global.log_tag.area;
        int err_code = 0;
-       struct wordlist *wl;
        struct proxy *px;
        struct post_check_fct *pcf;
        struct pre_check_fct *prcf;
@@ -2149,72 +2224,9 @@ static void init(int argc, char **argv)
        usermsgs_clr("config");
 
        /* in wait mode, we don't try to read the configuration files */
-       if (!(global.mode & MODE_MWORKER_WAIT)) {
-               char *env_cfgfiles = NULL;
-               int env_err = 0;
-
-               /* handle cfgfiles that are actually directories */
-               cfgfiles_expand_directories();
-
-               if (LIST_ISEMPTY(&cfg_cfgfiles))
-                       usage(progname);
+       if (!(global.mode & MODE_MWORKER_WAIT))
+               read_cfg(progname);
 
-               /* temporary create environment variables with default
-                * values to ease user configuration. Do not forget to
-                * unset them after the list_for_each_entry loop.
-                */
-               setenv("HAPROXY_HTTP_LOG_FMT", default_http_log_format, 1);
-               setenv("HAPROXY_HTTPS_LOG_FMT", default_https_log_format, 1);
-               setenv("HAPROXY_TCP_LOG_FMT", default_tcp_log_format, 1);
-               setenv("HAPROXY_BRANCH", PRODUCT_BRANCH, 1);
-               list_for_each_entry(wl, &cfg_cfgfiles, list) {
-                       int ret;
-
-                       if (env_err == 0) {
-                               if (!memprintf(&env_cfgfiles, "%s%s%s",
-                                              (env_cfgfiles ? env_cfgfiles : ""),
-                                              (env_cfgfiles ? ";" : ""), wl->s))
-                                       env_err = 1;
-                       }
-
-                       ret = readcfgfile(wl->s);
-                       if (ret == -1) {
-                               ha_alert("Could not open configuration file %s : %s\n",
-                                        wl->s, strerror(errno));
-                               free(env_cfgfiles);
-                               exit(1);
-                       }
-                       if (ret & (ERR_ABORT|ERR_FATAL))
-                               ha_alert("Error(s) found in configuration file : %s\n", wl->s);
-                       err_code |= ret;
-                       if (err_code & ERR_ABORT) {
-                               free(env_cfgfiles);
-                               exit(1);
-                       }
-               }
-               /* remove temporary environment variables. */
-               unsetenv("HAPROXY_BRANCH");
-               unsetenv("HAPROXY_HTTP_LOG_FMT");
-               unsetenv("HAPROXY_HTTPS_LOG_FMT");
-               unsetenv("HAPROXY_TCP_LOG_FMT");
-
-               /* do not try to resolve arguments nor to spot inconsistencies when
-                * the configuration contains fatal errors caused by files not found
-                * or failed memory allocations.
-                */
-               if (err_code & (ERR_ABORT|ERR_FATAL)) {
-                       ha_alert("Fatal errors found in configuration.\n");
-                       free(env_cfgfiles);
-                       exit(1);
-               }
-               if (env_err) {
-                       ha_alert("Could not allocate memory for HAPROXY_CFGFILES env variable\n");
-                       exit(1);
-               }
-               setenv("HAPROXY_CFGFILES", env_cfgfiles, 1);
-               free(env_cfgfiles);
-
-       }
        if (global.mode & MODE_MWORKER) {
                struct mworker_proc *tmproc;