From: Aurelien DARRAGON Date: Wed, 9 Apr 2025 19:05:35 +0000 (+0200) Subject: MINOR: proxy: add setup_new_proxy() function X-Git-Tag: v3.2-dev10~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e1cec655eeaca8ca8d27b3b2296e2c993e1bca97;p=thirdparty%2Fhaproxy.git MINOR: proxy: add setup_new_proxy() function Split alloc_new_proxy() in two functions: the preparing part is now handled by setup_new_proxy() which can be called individually, while alloc_new_proxy() takes care of allocating a new proxy struct and then calling setup_new_proxy() with the freshly allocated proxy. --- diff --git a/include/haproxy/proxy.h b/include/haproxy/proxy.h index 5abbeb5f3..1666b00ba 100644 --- a/include/haproxy/proxy.h +++ b/include/haproxy/proxy.h @@ -70,6 +70,7 @@ void proxy_destroy_all_unref_defaults(void); void proxy_ref_defaults(struct proxy *px, struct proxy *defpx); void proxy_unref_defaults(struct proxy *px); void proxy_unref_or_destroy_defaults(struct proxy *px); +int setup_new_proxy(struct proxy *px, const char *name, unsigned int cap, char **errmsg); struct proxy *alloc_new_proxy(const char *name, unsigned int cap, char **errmsg); struct proxy *parse_new_proxy(const char *name, unsigned int cap, diff --git a/src/proxy.c b/src/proxy.c index 13be57e40..f1f2a3f03 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -1674,12 +1674,47 @@ void proxy_unref_defaults(struct proxy *px) px->defpx = NULL; } +/* prepares a new proxy of type from the provided + * pointer. + * is assumed to be freshly allocated + * may be NULL: proxy id assigment will be skipped. + * + * Returns a 1 on success or 0 on failure (in which case errmsg must be checked + * then freed). + */ +int setup_new_proxy(struct proxy *px, const char *name, unsigned int cap, char **errmsg) +{ + uint last_change; + + init_new_proxy(px); + + last_change = ns_to_sec(now_ns); + if (cap & PR_CAP_FE) + px->fe_counters.last_change = last_change; + if (cap & PR_CAP_BE) + px->be_counters.last_change = last_change; + + if (name) { + px->id = strdup(name); + if (!px->id) { + memprintf(errmsg, "proxy '%s': out of memory", name); + return 0; + } + } + + px->cap = cap; + + if (name && !(cap & PR_CAP_INT)) + proxy_store_name(px); + + return 1; +} + /* Allocates a new proxy of type . * Returns the proxy instance on success. On error, NULL is returned. */ struct proxy *alloc_new_proxy(const char *name, unsigned int cap, char **errmsg) { - uint last_change; struct proxy *curproxy; if ((curproxy = calloc(1, sizeof(*curproxy))) == NULL) { @@ -1687,19 +1722,8 @@ struct proxy *alloc_new_proxy(const char *name, unsigned int cap, char **errmsg) goto fail; } - init_new_proxy(curproxy); - - last_change = ns_to_sec(now_ns); - if (cap & PR_CAP_FE) - curproxy->fe_counters.last_change = last_change; - if (cap & PR_CAP_BE) - curproxy->be_counters.last_change = last_change; - - curproxy->id = strdup(name); - curproxy->cap = cap; - - if (!(cap & PR_CAP_INT)) - proxy_store_name(curproxy); + if (!setup_new_proxy(curproxy, name, cap, errmsg)) + goto fail; done: return curproxy;