From: Frederic Lecaille Date: Wed, 10 Dec 2025 15:55:48 +0000 (+0100) Subject: MINOR: ha-inject: Move several general purpose function from haproxy.c to global.c X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=07b440516d1300dbc2240b8a527b75bcb743ad9f;p=thirdparty%2Fhaproxy.git MINOR: ha-inject: Move several general purpose function from haproxy.c to global.c Move several functions defined into haproxy.c but declared into global.h to global.c. Some modules used by ha-inject which does not need haproxy.c refer to such functions. --- diff --git a/include/haproxy/global.h b/include/haproxy/global.h index b4a7a406b..56ccf53b0 100644 --- a/include/haproxy/global.h +++ b/include/haproxy/global.h @@ -90,6 +90,7 @@ struct cfg_keyword; int check_kw_experimental(struct cfg_keyword *kw, const char *file, int linenum, char **errmsg); const char **hap_get_next_build_opt(const char **curr); +void ha_free_build_opts_list(void); /* simplified way to declare static build options in a file */ #define REGISTER_BUILD_OPTS(str) \ diff --git a/src/global.c b/src/global.c index b952cf56a..39c159ab5 100644 --- a/src/global.c +++ b/src/global.c @@ -1,7 +1,12 @@ +#include +#include + #include #include #include #include +#include +#include /* global options */ struct global global = { @@ -58,3 +63,130 @@ struct global global = { }; int stopping; /* non zero means stopping in progress */ + +/* These are strings to be reported in the output of "haproxy -vv". They may + * either be constants (in which case must_free must be zero) or dynamically + * allocated strings to pass to free() on exit, and in this case must_free + * must be non-zero. + */ +struct list build_opts_list = LIST_HEAD_INIT(build_opts_list); +struct build_opts_str { + struct list list; + const char *str; + int must_free; +}; + +/*********************************************************************/ +/* general purpose functions ***************************************/ +/*********************************************************************/ + +/* used to register some build option strings at boot. Set must_free to + * non-zero if the string must be freed upon exit. + */ +void hap_register_build_opts(const char *str, int must_free) +{ + struct build_opts_str *b; + + b = calloc(1, sizeof(*b)); + if (!b) { + fprintf(stderr, "out of memory\n"); + exit(1); + } + b->str = str; + b->must_free = must_free; + LIST_APPEND(&build_opts_list, &b->list); +} + +/* returns the first build option when is NULL, or the next one when + * is passed the last returned value. NULL when there is no more entries + * in the list. Otherwise the returned pointer is &opt->str so the caller can + * print it as *ret. + */ +const char **hap_get_next_build_opt(const char **curr) +{ + struct build_opts_str *head, *start; + + head = container_of(&build_opts_list, struct build_opts_str, list); + + if (curr) + start = container_of(curr, struct build_opts_str, str); + else + start = head; + + start = container_of(start->list.n, struct build_opts_str, list); + + if (start == head) + return NULL; + + return &start->str; +} + +/* used to make a new feature appear in the build_features list at boot time. + * The feature must be in the format "XXX" without the leading "+" which will + * be automatically appended. + */ +void hap_register_feature(const char *name) +{ + static int must_free = 0; + int new_len = strlen(build_features) + 2 + strlen(name); + char *new_features; + char *startp, *endp; + int found = 0; + + new_features = malloc(new_len + 1); + if (!new_features) + return; + + strlcpy2(new_features, build_features, new_len); + + startp = new_features; + + /* look if the string already exists */ + while (startp) { + char *sign = startp; + + /* tokenize for simpler strcmp */ + endp = strchr(startp, ' '); + if (endp) + *endp = '\0'; + + startp++; /* skip sign */ + + if (strcmp(startp, name) == 0) { + *sign = '+'; + found = 1; + } + + /* couldn't find a space, that's the end of the string */ + if (!endp) + break; + + *endp = ' '; + startp = endp + 1; + + if (found) + break; + } + + /* if we didn't find the feature add it to the string */ + if (!found) + snprintf(new_features, new_len + 1, "%s +%s", build_features, name); + + if (must_free) + ha_free(&build_features); + + build_features = new_features; + must_free = 1; +} + +void ha_free_build_opts_list(void) +{ + struct build_opts_str *bol, *bolb; + + list_for_each_entry_safe(bol, bolb, &build_opts_list, list) { + if (bol->must_free) + free((void *)bol->str); + LIST_DELETE(&bol->list); + free(bol); + } +} diff --git a/src/haproxy.c b/src/haproxy.c index 3d330efda..fd7a8f6a2 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -225,123 +225,8 @@ int check_kw_experimental(struct cfg_keyword *kw, const char *file, int linenum, return 0; } -/* These are strings to be reported in the output of "haproxy -vv". They may - * either be constants (in which case must_free must be zero) or dynamically - * allocated strings to pass to free() on exit, and in this case must_free - * must be non-zero. - */ -struct list build_opts_list = LIST_HEAD_INIT(build_opts_list); -struct build_opts_str { - struct list list; - const char *str; - int must_free; -}; - int mode_stress_level = 0; -/*********************************************************************/ -/* general purpose functions ***************************************/ -/*********************************************************************/ - -/* used to register some build option strings at boot. Set must_free to - * non-zero if the string must be freed upon exit. - */ -void hap_register_build_opts(const char *str, int must_free) -{ - struct build_opts_str *b; - - b = calloc(1, sizeof(*b)); - if (!b) { - fprintf(stderr, "out of memory\n"); - exit(1); - } - b->str = str; - b->must_free = must_free; - LIST_APPEND(&build_opts_list, &b->list); -} - -/* returns the first build option when is NULL, or the next one when - * is passed the last returned value. NULL when there is no more entries - * in the list. Otherwise the returned pointer is &opt->str so the caller can - * print it as *ret. - */ -const char **hap_get_next_build_opt(const char **curr) -{ - struct build_opts_str *head, *start; - - head = container_of(&build_opts_list, struct build_opts_str, list); - - if (curr) - start = container_of(curr, struct build_opts_str, str); - else - start = head; - - start = container_of(start->list.n, struct build_opts_str, list); - - if (start == head) - return NULL; - - return &start->str; -} - -/* used to make a new feature appear in the build_features list at boot time. - * The feature must be in the format "XXX" without the leading "+" which will - * be automatically appended. - */ -void hap_register_feature(const char *name) -{ - static int must_free = 0; - int new_len = strlen(build_features) + 2 + strlen(name); - char *new_features; - char *startp, *endp; - int found = 0; - - new_features = malloc(new_len + 1); - if (!new_features) - return; - - strlcpy2(new_features, build_features, new_len); - - startp = new_features; - - /* look if the string already exists */ - while (startp) { - char *sign = startp; - - /* tokenize for simpler strcmp */ - endp = strchr(startp, ' '); - if (endp) - *endp = '\0'; - - startp++; /* skip sign */ - - if (strcmp(startp, name) == 0) { - *sign = '+'; - found = 1; - } - - /* couldn't find a space, that's the end of the string */ - if (!endp) - break; - - *endp = ' '; - startp = endp + 1; - - if (found) - break; - } - - /* if we didn't find the feature add it to the string */ - if (!found) - snprintf(new_features, new_len + 1, "%s +%s", build_features, name); - - if (must_free) - ha_free(&build_features); - - build_features = new_features; - must_free = 1; -} - #ifdef DEBUG_UNIT /* register a function that could be registered in "-U" argument */ void hap_register_unittest(const char *name, int (*fct)(int argc, char **argv)) @@ -2625,7 +2510,6 @@ void deinit(void) struct proxy *p = proxies_list, *p0; struct cfgfile *cfg, *cfg_tmp; struct logger *log, *logb; - struct build_opts_str *bol, *bolb; struct post_deinit_fct *pdf, *pdfb; struct proxy_deinit_fct *pxdf, *pxdfb; struct server_deinit_fct *srvdf, *srvdfb; @@ -2724,12 +2608,7 @@ void deinit(void) ha_free(&cfg); } - list_for_each_entry_safe(bol, bolb, &build_opts_list, list) { - if (bol->must_free) - free((void *)bol->str); - LIST_DELETE(&bol->list); - free(bol); - } + ha_free_build_opts_list(); list_for_each_entry_safe(pxdf, pxdfb, &proxy_deinit_list, list) { LIST_DELETE(&pxdf->list);