]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: log: add dup_logsrv() helper function
authorAurelien DARRAGON <adarragon@haproxy.com>
Wed, 5 Jul 2023 13:52:19 +0000 (15:52 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 6 Sep 2023 14:06:39 +0000 (16:06 +0200)
ease code maintenance by introducing dup_logsrv() helper function to
properly duplicate an existing logsrv struct.

include/haproxy/log.h
src/http_client.c
src/log.c
src/proxy.c

index 1715a0c9d282db9bf79612352fb9515f442f4fe3..1a4c79071a00f4a9ceda6e3a84fa8463b1111c5c 100644 (file)
@@ -87,6 +87,7 @@ int add_to_logformat_list(char *start, char *end, int type, struct list *list_fo
  */
 int parse_logformat_string(const char *str, struct proxy *curproxy, struct list *list_format, int options, int cap, char **err);
 
+struct logsrv *dup_logsrv(struct logsrv *def);
 void free_logsrv(struct logsrv *logsrv);
 
 /* Parse "log" keyword and update the linked list. */
index 8346f02f6db3c7b5f7ef419e2f493b475d82a1f5..bebdd7f51e44e58b47b83e3282340e1ea605df90 100644 (file)
@@ -1369,19 +1369,14 @@ static int httpclient_postcheck_proxy(struct proxy *curproxy)
 
        /* copy logs from "global" log list */
        list_for_each_entry(logsrv, &global.logsrvs, list) {
-               struct logsrv *node = malloc(sizeof(*node));
+               struct logsrv *node = dup_logsrv(logsrv);
 
                if (!node) {
                        memprintf(&errmsg, "out of memory.");
                        err_code |= ERR_ALERT | ERR_FATAL;
                        goto err;
                }
-
-               memcpy(node, logsrv, sizeof(*node));
-               LIST_INIT(&node->list);
                LIST_APPEND(&curproxy->logsrvs, &node->list);
-               node->ring_name = logsrv->ring_name ? strdup(logsrv->ring_name) : NULL;
-               node->conf.file = logsrv->conf.file ? strdup(logsrv->conf.file) : NULL;
        }
        if (curproxy->conf.logformat_string) {
                curproxy->conf.args.ctx = ARGC_LOG;
index e8430a88b139dd30cc03f0bbf333ddfad31d391b..97b809e2070873853f003bcf6b54808738e4aca6 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -734,6 +734,43 @@ int smp_log_range_cmp(const void *a, const void *b)
        return 0;
 }
 
+/* tries to duplicate <def> logsrv
+ *
+ * Returns the newly allocated and duplicated logsrv or NULL
+ * in case of error.
+ */
+struct logsrv *dup_logsrv(struct logsrv *def)
+{
+       struct logsrv *cpy = malloc(sizeof(*cpy));
+
+       /* copy everything that can be easily copied */
+       memcpy(cpy, def, sizeof(*cpy));
+
+       /* default values */
+       cpy->ring_name = NULL;
+       cpy->conf.file = NULL;
+       LIST_INIT(&cpy->list);
+       HA_SPIN_INIT(&cpy->lock);
+
+       /* special members */
+       if (def->ring_name) {
+               cpy->ring_name = strdup(def->ring_name);
+               if (!cpy->ring_name)
+                       goto error;
+       }
+       if (def->conf.file) {
+               cpy->conf.file = strdup(def->conf.file);
+               if (!cpy->conf.file)
+                       goto error;
+       }
+       cpy->ref = def;
+       return cpy;
+
+ error:
+       free_logsrv(cpy);
+       return NULL;
+}
+
 /* frees log server <logsrv> after freeing all of its allocated fields. The
  * server must not belong to a list anymore. Logsrv may be NULL, which is
  * silently ignored.
@@ -808,19 +845,21 @@ int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file
                                        goto skip_logsrv;
                        }
 
-                       node = malloc(sizeof(*node));
+                       /* duplicate logsrv from global */
+                       node = dup_logsrv(logsrv);
                        if (!node) {
                                memprintf(err, "out of memory error");
                                goto error;
                        }
-                       memcpy(node, logsrv, sizeof(struct logsrv));
-                       node->ref = logsrv;
-                       LIST_INIT(&node->list);
-                       LIST_APPEND(logsrvs, &node->list);
-                       node->ring_name = logsrv->ring_name ? strdup(logsrv->ring_name) : NULL;
+
+                       /* manually override some values */
+                       ha_free(&node->conf.file);
                        node->conf.file = strdup(file);
                        node->conf.line = linenum;
 
+                       /* add to list */
+                       LIST_APPEND(logsrvs, &node->list);
+
                  skip_logsrv:
                        continue;
                }
index ae8049dbb6b6dbb8d78dad59f6308d1677fcc627..714e70b5ae78998ba35fae6aa9bf8d32f89be7e2 100644 (file)
@@ -1814,19 +1814,13 @@ static int proxy_defproxy_cpy(struct proxy *curproxy, const struct proxy *defpro
 
        /* copy default logsrvs to curproxy */
        list_for_each_entry(tmplogsrv, &defproxy->logsrvs, list) {
-               struct logsrv *node = malloc(sizeof(*node));
+               struct logsrv *node = dup_logsrv(tmplogsrv);
 
                if (!node) {
                        memprintf(errmsg, "proxy '%s': out of memory", curproxy->id);
                        return 1;
                }
-               memcpy(node, tmplogsrv, sizeof(struct logsrv));
-               node->ref = tmplogsrv->ref;
-               LIST_INIT(&node->list);
                LIST_APPEND(&curproxy->logsrvs, &node->list);
-               node->ring_name = tmplogsrv->ring_name ? strdup(tmplogsrv->ring_name) : NULL;
-               node->conf.file = strdup(tmplogsrv->conf.file);
-               node->conf.line = tmplogsrv->conf.line;
        }
 
        curproxy->conf.uniqueid_format_string = defproxy->conf.uniqueid_format_string;