]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: config: keep up-to-date current file/line/section in the global struct
authorWilly Tarreau <w@1wt.eu>
Thu, 6 May 2021 08:04:45 +0000 (10:04 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 6 May 2021 08:35:03 +0000 (10:35 +0200)
Let's add a few fields to the global struct to store information about
the current file being processed, the current line number and the current
section. This will be used to retrieve them using special variables.

include/haproxy/global-t.h
src/cfgparse.c

index bea97dd7ac79691bfd0f6f3ee43b7c7c36364cd0..8df1349aaf90b35e7a064bdd088fefe70cc4f24d 100644 (file)
@@ -159,6 +159,10 @@ struct global {
        } unix_bind;
        struct proxy *cli_fe;           /* the frontend holding the stats settings */
        int numa_cpu_mapping;
+       int cfg_curr_line;              /* line number currently being parsed */
+       const char *cfg_curr_file;      /* config file currently being parsed or NULL */
+       char *cfg_curr_section;         /* config section name currently being parsed or NULL */
+
        /* The info above is config stuff, it doesn't change during the process' life */
        /* A number of the elements below are updated by all threads in real time and
         * suffer high contention, so we need to put them in their own cache lines, if
index e5585ff609251e68d820001c6f205df4977b553a..168cabb73118fb2ed64964eb17251ec99ff35aa1 100644 (file)
@@ -1685,6 +1685,9 @@ int readcfgfile(const char *file)
        int non_global_section_parsed = 0;
        char *errmsg = NULL;
 
+       global.cfg_curr_line = 0;
+       global.cfg_curr_file = file;
+
        if ((thisline = malloc(sizeof(*thisline) * linesize)) == NULL) {
                ha_alert("Out of memory trying to allocate a buffer for a configuration line.\n");
                err_code = -1;
@@ -1720,6 +1723,7 @@ next_line:
                }
 
                linenum++;
+               global.cfg_curr_line = linenum;
 
                if (fatal >= 50) {
                        ha_alert("parsing [%s:%d]: too many fatal errors (%d), stopping now.\n", file, linenum, fatal);
@@ -2049,6 +2053,8 @@ next_line:
                                cursection = ics->section_name;
                                pcs = cs;
                                cs = ics;
+                               free(global.cfg_curr_section);
+                               global.cfg_curr_section = strdup(*args[1] ? args[1] : args[0]);
 
                                if (global.mode & MODE_DIAG) {
                                        check_section_position(args[0], file, linenum,
@@ -2095,6 +2101,7 @@ next_line:
                err_code |= ERR_ALERT | ERR_FATAL;
        }
 
+       ha_free(&global.cfg_curr_section);
        if (cs && cs->post_section_parser)
                err_code |= cs->post_section_parser();
 
@@ -2113,6 +2120,9 @@ err:
        cursection = NULL;
        free(thisline);
        free(outline);
+       global.cfg_curr_line = 0;
+       global.cfg_curr_file = NULL;
+
        if (f)
                fclose(f);