From cb08bcb9d6192a3e9c7923ae0dc38400c6cf4b02 Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Wed, 27 Aug 2025 14:51:48 +0200 Subject: [PATCH] MINOR: counters: retrieve detailed errmsg upon failure with counters_{fe,be}_shared_prepare() counters_{fe,be}_shared_prepare now take an extra parameter that contains additional hints about the error in case of failure. It must be freed accordingly since it is allocated using memprintf --- include/haproxy/counters.h | 4 ++-- src/counters.c | 16 ++++++++++------ src/proxy.c | 22 +++++++++++++--------- src/server.c | 6 ++++-- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/include/haproxy/counters.h b/include/haproxy/counters.h index 803e8ec41..2484ece68 100644 --- a/include/haproxy/counters.h +++ b/include/haproxy/counters.h @@ -27,8 +27,8 @@ #include #include -int counters_fe_shared_prepare(struct fe_counters_shared *counters, const struct guid_node *guid); -int counters_be_shared_prepare(struct be_counters_shared *counters, const struct guid_node *guid); +int counters_fe_shared_prepare(struct fe_counters_shared *counters, const struct guid_node *guid, char **errmsg); +int counters_be_shared_prepare(struct be_counters_shared *counters, const struct guid_node *guid, char **errmsg); void counters_fe_shared_drop(struct fe_counters_shared *counters); void counters_be_shared_drop(struct be_counters_shared *counters); diff --git a/src/counters.c b/src/counters.c index a7a1a864d..6f480b2a9 100644 --- a/src/counters.c +++ b/src/counters.c @@ -24,6 +24,7 @@ #include #include #include +#include static void _counters_shared_drop(void *counters) { @@ -55,9 +56,11 @@ void counters_be_shared_drop(struct be_counters_shared *counters) /* prepare shared counters pointer for a given object * hint is expected to reflect the actual tg member size (fe/be) * if is not set, then sharing is disabled - * Returns the pointer on success or NULL on failure + * Returns the pointer on success or NULL on failure, in which case + * will contain additional hints about the error and must be freed accordingly */ -static int _counters_shared_prepare(struct counters_shared *shared, const struct guid_node *guid, size_t size) +static int _counters_shared_prepare(struct counters_shared *shared, + const struct guid_node *guid, size_t size, char **errmsg) { int it = 0; @@ -69,6 +72,7 @@ static int _counters_shared_prepare(struct counters_shared *shared, const struct while (it < global.nbtgroups) { shared->tg[it] = calloc(1, size); if (!shared->tg[it]) { + memprintf(errmsg, "memory error, calloc failed"); _counters_shared_drop(shared); return 0; } @@ -83,13 +87,13 @@ static int _counters_shared_prepare(struct counters_shared *shared, const struct } /* prepare shared fe counters pointer for a given object */ -int counters_fe_shared_prepare(struct fe_counters_shared *shared, const struct guid_node *guid) +int counters_fe_shared_prepare(struct fe_counters_shared *shared, const struct guid_node *guid, char **errmsg) { - return _counters_shared_prepare((struct counters_shared *)shared, guid, sizeof(struct fe_counters_shared_tg)); + return _counters_shared_prepare((struct counters_shared *)shared, guid, sizeof(struct fe_counters_shared_tg), errmsg); } /* prepare shared be counters pointer for a given object */ -int counters_be_shared_prepare(struct be_counters_shared *shared, const struct guid_node *guid) +int counters_be_shared_prepare(struct be_counters_shared *shared, const struct guid_node *guid, char **errmsg) { - return _counters_shared_prepare((struct counters_shared *)shared, guid, sizeof(struct be_counters_shared_tg)); + return _counters_shared_prepare((struct counters_shared *)shared, guid, sizeof(struct be_counters_shared_tg), errmsg); } diff --git a/src/proxy.c b/src/proxy.c index 05a91355d..b2de70bde 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -1749,6 +1749,7 @@ struct proxy *alloc_new_proxy(const char *name, unsigned int cap, char **errmsg) static int proxy_postcheck(struct proxy *px) { struct listener *listener; + char *errmsg = NULL; int err_code = ERR_NONE; /* allocate private memory for shared counters: used as a fallback @@ -1757,9 +1758,10 @@ static int proxy_postcheck(struct proxy *px) * proxy postparsing, see proxy_postparse() */ if (px->cap & PR_CAP_FE) { - if (!counters_fe_shared_prepare(&px->fe_counters.shared, &px->guid)) { - ha_alert("out of memory while setting up shared counters for %s %s\n", - proxy_type_str(px), px->id); + if (!counters_fe_shared_prepare(&px->fe_counters.shared, &px->guid, &errmsg)) { + ha_alert("out of memory while setting up shared counters for %s %s : %s\n", + proxy_type_str(px), px->id, errmsg); + ha_free(&errmsg); err_code |= ERR_ALERT | ERR_FATAL; goto out; } @@ -1769,9 +1771,10 @@ static int proxy_postcheck(struct proxy *px) * be_counters may be used even if the proxy lacks the backend * capability */ - if (!counters_be_shared_prepare(&px->be_counters.shared, &px->guid)) { - ha_alert("out of memory while setting up shared counters for %s %s\n", - proxy_type_str(px), px->id); + if (!counters_be_shared_prepare(&px->be_counters.shared, &px->guid, &errmsg)) { + ha_alert("out of memory while setting up shared counters for %s %s : %s\n", + proxy_type_str(px), px->id, errmsg); + ha_free(&errmsg); err_code |= ERR_ALERT | ERR_FATAL; goto out; } @@ -1780,10 +1783,11 @@ static int proxy_postcheck(struct proxy *px) list_for_each_entry(listener, &px->conf.listeners, by_fe) { if (listener->counters) { - if (!counters_fe_shared_prepare(&listener->counters->shared, &listener->guid)) { + if (!counters_fe_shared_prepare(&listener->counters->shared, &listener->guid, &errmsg)) { ha_free(&listener->counters); - ha_alert("out of memory while setting up shared listener counters for %s %s\n", - proxy_type_str(px), px->id); + ha_alert("out of memory while setting up shared listener counters for %s %s : %s\n", + proxy_type_str(px), px->id, errmsg); + ha_free(&errmsg); err_code |= ERR_ALERT | ERR_FATAL; goto out; } diff --git a/src/server.c b/src/server.c index cfdf9cdf2..01ef6dcc0 100644 --- a/src/server.c +++ b/src/server.c @@ -3466,14 +3466,16 @@ static int init_srv_slowstart(struct server *srv); int srv_postinit(struct server *srv) { int err_code = ERR_NONE; + char *errmsg = NULL; err_code |= _srv_check_proxy_mode(srv, 1); if (err_code & ERR_CODE) goto out; - if (!counters_be_shared_prepare(&srv->counters.shared, &srv->guid)) { - ha_alert("memory error while setting up shared counters for %s/%s server\n", srv->proxy->id, srv->id); + if (!counters_be_shared_prepare(&srv->counters.shared, &srv->guid, &errmsg)) { + ha_alert("memory error while setting up shared counters for %s/%s server : %s\n", srv->proxy->id, srv->id, errmsg); + ha_free(&errmsg); err_code |= ERR_ALERT | ERR_FATAL; goto out; }