MINOR: log: add dup_logsrv() helper function

ease code maintenance by introducing dup_logsrv() helper function to
properly duplicate an existing logsrv struct.
This commit is contained in:
Aurelien DARRAGON 2023-07-05 15:52:19 +02:00 committed by Christopher Faulet
parent 7a12e2d369
commit 969e212c66
4 changed files with 48 additions and 19 deletions

View 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. */

View 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;

View File

@ -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;
}

View 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;