From: Remi Tricot-Le Breton Date: Thu, 16 Nov 2023 16:38:20 +0000 (+0100) Subject: MINOR: cache: Use dedicated trash for "show cache" cli command X-Git-Tag: v2.9-dev10~55 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a0d7c290ecef26f3b39dc84b8c0c3972207bf913;p=thirdparty%2Fhaproxy.git MINOR: cache: Use dedicated trash for "show cache" cli command After the latest changes in the cache/shared_context mechanism, the cache and shared_context logic were decorrelated and in some unlikely cases we might end up using the "show cache" command while some regular cache processing is occurring (a response being stored in the cache for instance). In such a case, because we used the same 'trash' buffer in those two contexts, we could end up with the contents of a response in the ouput of the "show cache" command. This patch fixes this problem by allocating a dedicated trash for the CLI command. --- diff --git a/src/cache.c b/src/cache.c index 60125c8e7e..36d0412812 100644 --- a/src/cache.c +++ b/src/cache.c @@ -2665,6 +2665,10 @@ static int cli_io_handler_show_cache(struct appctx *appctx) { struct show_cache_ctx *ctx = appctx->svcctx; struct cache* cache = ctx->cache; + struct buffer *buf = alloc_trash_chunk(); + + if (buf == NULL) + return 1; list_for_each_entry_from(cache, &caches, list) { struct eb32_node *node = NULL; @@ -2677,10 +2681,10 @@ static int cli_io_handler_show_cache(struct appctx *appctx) next_key = ctx->next_key; if (!next_key) { - chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav); - if (applet_putchk(appctx, &trash) == -1) { + chunk_printf(buf, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav); + if (applet_putchk(appctx, buf) == -1) { shctx_unlock(shctx); - return 0; + goto yield; } } shctx_unlock(shctx); @@ -2700,10 +2704,10 @@ static int cli_io_handler_show_cache(struct appctx *appctx) next_key = node->key + 1; if (entry->expire > date.tv_sec) { - chunk_printf(&trash, "%p hash:%u vary:0x", entry, read_u32(entry->hash)); + chunk_printf(buf, "%p hash:%u vary:0x", entry, read_u32(entry->hash)); for (i = 0; i < HTTP_CACHE_SEC_KEY_LEN; ++i) - chunk_appendf(&trash, "%02x", (unsigned char)entry->secondary_key[i]); - chunk_appendf(&trash, " size:%u (%u blocks), refcount:%u, expire:%d\n", + chunk_appendf(buf, "%02x", (unsigned char)entry->secondary_key[i]); + chunk_appendf(buf, " size:%u (%u blocks), refcount:%u, expire:%d\n", block_ptr(entry)->len, block_ptr(entry)->block_count, block_ptr(entry)->refcount, entry->expire - (int)date.tv_sec); } @@ -2711,17 +2715,21 @@ static int cli_io_handler_show_cache(struct appctx *appctx) ctx->next_key = next_key; - if (applet_putchk(appctx, &trash) == -1) { + if (applet_putchk(appctx, buf) == -1) { cache_rdunlock(cache); - return 0; + goto yield; } } cache_rdunlock(cache); } + free_trash_chunk(buf); return 1; +yield: + free_trash_chunk(buf); + return 0; }