]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: ha-inject: Move several general purpose function from haproxy.c to global.c
authorFrederic Lecaille <flecaille@haproxy.com>
Wed, 10 Dec 2025 15:55:48 +0000 (16:55 +0100)
committerFrederic Lecaille <flecaille@haproxy.com>
Wed, 10 Dec 2025 16:45:28 +0000 (17:45 +0100)
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.

include/haproxy/global.h
src/global.c
src/haproxy.c

index b4a7a406bc29c41c8c2f1d1ebd87ef220f6cc670..56ccf53b0b10a7841417d290570e8776cc78726e 100644 (file)
@@ -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) \
index b952cf56aa0bd7c8fe231d0d88c7dab43c0d5d9b..39c159ab577b6fa69b2f9984df12a9794ab7215c 100644 (file)
@@ -1,7 +1,12 @@
+#include <stdlib.h>
+#include <string.h>
+
 #include <haproxy/global.h>
 #include <haproxy/list.h>
 #include <haproxy/protocol-t.h>
 #include <haproxy/ticks.h>
+#include <haproxy/tools.h>
+#include <haproxy/version.h>
 
 /* 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 <curr> is NULL, or the next one when
+ * <curr> 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);
+       }
+}
index 3d330efdab53450a380fec0bcefc82b83d4fab45..fd7a8f6a22af0fcd018418a2518a187208416b19 100644 (file)
@@ -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 <curr> is NULL, or the next one when
- * <curr> 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);