Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Ring buffer management |
| 3 | * |
| 4 | * Copyright (C) 2000-2019 Willy Tarreau - w@1wt.eu |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation, version 2.1 |
| 9 | * exclusively. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <stdlib.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 22 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 23 | #include <haproxy/applet.h> |
Willy Tarreau | 8dabda7 | 2020-05-27 17:22:10 +0200 | [diff] [blame] | 24 | #include <haproxy/buf.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 25 | #include <haproxy/cli.h> |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 26 | #include <haproxy/conn_stream.h> |
| 27 | #include <haproxy/cs_utils.h> |
Willy Tarreau | d2ad57c | 2020-06-03 19:43:35 +0200 | [diff] [blame] | 28 | #include <haproxy/ring.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 29 | #include <haproxy/thread.h> |
Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 30 | |
Emeric Brun | e14b98c | 2021-01-12 14:21:00 +0100 | [diff] [blame] | 31 | /* Initialize a pre-allocated ring with the buffer area |
| 32 | * of size */ |
| 33 | void ring_init(struct ring *ring, void *area, size_t size) |
| 34 | { |
| 35 | HA_RWLOCK_INIT(&ring->lock); |
| 36 | LIST_INIT(&ring->waiters); |
| 37 | ring->readers_count = 0; |
| 38 | ring->ofs = 0; |
| 39 | ring->buf = b_make(area, size, 0, 0); |
| 40 | /* write the initial RC byte */ |
| 41 | b_putchr(&ring->buf, 0); |
| 42 | } |
| 43 | |
Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 44 | /* Creates and returns a ring buffer of size <size> bytes. Returns NULL on |
| 45 | * allocation failure. |
| 46 | */ |
| 47 | struct ring *ring_new(size_t size) |
| 48 | { |
| 49 | struct ring *ring = NULL; |
| 50 | void *area = NULL; |
| 51 | |
| 52 | if (size < 2) |
| 53 | goto fail; |
| 54 | |
| 55 | ring = malloc(sizeof(*ring)); |
| 56 | if (!ring) |
| 57 | goto fail; |
| 58 | |
| 59 | area = malloc(size); |
| 60 | if (!area) |
| 61 | goto fail; |
| 62 | |
Emeric Brun | e14b98c | 2021-01-12 14:21:00 +0100 | [diff] [blame] | 63 | ring_init(ring, area, size); |
Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 64 | return ring; |
| 65 | fail: |
| 66 | free(area); |
| 67 | free(ring); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | /* Resizes existing ring <ring> to <size> which must be larger, without losing |
| 72 | * its contents. The new size must be at least as large as the previous one or |
| 73 | * no change will be performed. The pointer to the ring is returned on success, |
| 74 | * or NULL on allocation failure. This will lock the ring for writes. |
| 75 | */ |
| 76 | struct ring *ring_resize(struct ring *ring, size_t size) |
| 77 | { |
| 78 | void *area; |
| 79 | |
| 80 | if (b_size(&ring->buf) >= size) |
| 81 | return ring; |
| 82 | |
| 83 | area = malloc(size); |
| 84 | if (!area) |
| 85 | return NULL; |
| 86 | |
| 87 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
| 88 | |
| 89 | /* recheck the buffer's size, it may have changed during the malloc */ |
| 90 | if (b_size(&ring->buf) < size) { |
| 91 | /* copy old contents */ |
| 92 | b_getblk(&ring->buf, area, ring->buf.data, 0); |
| 93 | area = HA_ATOMIC_XCHG(&ring->buf.area, area); |
| 94 | ring->buf.size = size; |
| 95 | ring->buf.head = 0; |
| 96 | } |
| 97 | |
| 98 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 99 | |
| 100 | free(area); |
| 101 | return ring; |
| 102 | } |
| 103 | |
| 104 | /* destroys and frees ring <ring> */ |
| 105 | void ring_free(struct ring *ring) |
| 106 | { |
| 107 | if (!ring) |
| 108 | return; |
| 109 | free(ring->buf.area); |
| 110 | free(ring); |
| 111 | } |
| 112 | |
Willy Tarreau | be97853 | 2019-08-27 11:44:13 +0200 | [diff] [blame] | 113 | /* Tries to send <npfx> parts from <prefix> followed by <nmsg> parts from <msg> |
| 114 | * to ring <ring>. The message is sent atomically. It may be truncated to |
| 115 | * <maxlen> bytes if <maxlen> is non-null. There is no distinction between the |
| 116 | * two lists, it's just a convenience to help the caller prepend some prefixes |
| 117 | * when necessary. It takes the ring's write lock to make sure no other thread |
| 118 | * will touch the buffer during the update. Returns the number of bytes sent, |
| 119 | * or <=0 on failure. |
| 120 | */ |
| 121 | ssize_t ring_write(struct ring *ring, size_t maxlen, const struct ist pfx[], size_t npfx, const struct ist msg[], size_t nmsg) |
| 122 | { |
| 123 | struct buffer *buf = &ring->buf; |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 124 | struct appctx *appctx; |
Willy Tarreau | be97853 | 2019-08-27 11:44:13 +0200 | [diff] [blame] | 125 | size_t totlen = 0; |
| 126 | size_t lenlen; |
Willy Tarreau | 3036290 | 2019-08-30 15:06:10 +0200 | [diff] [blame] | 127 | uint64_t dellen; |
Willy Tarreau | be97853 | 2019-08-27 11:44:13 +0200 | [diff] [blame] | 128 | int dellenlen; |
| 129 | ssize_t sent = 0; |
| 130 | int i; |
| 131 | |
| 132 | /* we have to find some room to add our message (the buffer is |
| 133 | * never empty and at least contains the previous counter) and |
| 134 | * to update both the buffer contents and heads at the same |
| 135 | * time (it's doable using atomic ops but not worth the |
| 136 | * trouble, let's just lock). For this we first need to know |
| 137 | * the total message's length. We cannot measure it while |
| 138 | * copying due to the varint encoding of the length. |
| 139 | */ |
| 140 | for (i = 0; i < npfx; i++) |
| 141 | totlen += pfx[i].len; |
| 142 | for (i = 0; i < nmsg; i++) |
| 143 | totlen += msg[i].len; |
| 144 | |
| 145 | if (totlen > maxlen) |
| 146 | totlen = maxlen; |
| 147 | |
| 148 | lenlen = varint_bytes(totlen); |
| 149 | |
| 150 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
| 151 | if (lenlen + totlen + 1 + 1 > b_size(buf)) |
| 152 | goto done_buf; |
| 153 | |
| 154 | while (b_room(buf) < lenlen + totlen + 1) { |
| 155 | /* we need to delete the oldest message (from the end), |
| 156 | * and we have to stop if there's a reader stuck there. |
| 157 | * Unless there's corruption in the buffer it's guaranteed |
| 158 | * that we have enough data to find 1 counter byte, a |
| 159 | * varint-encoded length (1 byte min) and the message |
| 160 | * payload (0 bytes min). |
| 161 | */ |
| 162 | if (*b_head(buf)) |
| 163 | goto done_buf; |
| 164 | dellenlen = b_peek_varint(buf, 1, &dellen); |
| 165 | if (!dellenlen) |
| 166 | goto done_buf; |
| 167 | BUG_ON(b_data(buf) < 1 + dellenlen + dellen); |
| 168 | |
| 169 | b_del(buf, 1 + dellenlen + dellen); |
| 170 | ring->ofs += 1 + dellenlen + dellen; |
| 171 | } |
| 172 | |
| 173 | /* OK now we do have room */ |
| 174 | __b_put_varint(buf, totlen); |
| 175 | |
| 176 | totlen = 0; |
| 177 | for (i = 0; i < npfx; i++) { |
| 178 | size_t len = pfx[i].len; |
| 179 | |
| 180 | if (len + totlen > maxlen) |
| 181 | len = maxlen - totlen; |
| 182 | if (len) |
| 183 | __b_putblk(buf, pfx[i].ptr, len); |
| 184 | totlen += len; |
| 185 | } |
| 186 | |
| 187 | for (i = 0; i < nmsg; i++) { |
| 188 | size_t len = msg[i].len; |
| 189 | |
| 190 | if (len + totlen > maxlen) |
| 191 | len = maxlen - totlen; |
| 192 | if (len) |
| 193 | __b_putblk(buf, msg[i].ptr, len); |
| 194 | totlen += len; |
| 195 | } |
| 196 | |
William Dauchy | 477757c | 2020-08-07 22:19:23 +0200 | [diff] [blame] | 197 | *b_tail(buf) = 0; buf->data++; // new read counter |
Willy Tarreau | be97853 | 2019-08-27 11:44:13 +0200 | [diff] [blame] | 198 | sent = lenlen + totlen + 1; |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 199 | |
| 200 | /* notify potential readers */ |
Willy Tarreau | 9597cbd | 2020-05-19 17:07:30 +0200 | [diff] [blame] | 201 | list_for_each_entry(appctx, &ring->waiters, wait_entry) |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 202 | appctx_wakeup(appctx); |
| 203 | |
Willy Tarreau | be97853 | 2019-08-27 11:44:13 +0200 | [diff] [blame] | 204 | done_buf: |
| 205 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 206 | return sent; |
| 207 | } |
Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 208 | |
Willy Tarreau | 928068a | 2020-05-19 19:14:42 +0200 | [diff] [blame] | 209 | /* Tries to attach appctx <appctx> as a new reader on ring <ring>. This is |
| 210 | * meant to be used by low level appctx code such as CLI or ring forwarding. |
| 211 | * For higher level functions, please see the relevant parts in appctx or CLI. |
| 212 | * It returns non-zero on success or zero on failure if too many users are |
| 213 | * already attached. On success, the caller MUST call ring_detach_appctx() |
| 214 | * to detach itself, even if it was never woken up. |
| 215 | */ |
Emeric Brun | dcd58af | 2020-05-28 14:39:30 +0200 | [diff] [blame] | 216 | int ring_attach(struct ring *ring) |
Willy Tarreau | 928068a | 2020-05-19 19:14:42 +0200 | [diff] [blame] | 217 | { |
| 218 | int users = ring->readers_count; |
| 219 | |
| 220 | do { |
| 221 | if (users >= 255) |
| 222 | return 0; |
| 223 | } while (!_HA_ATOMIC_CAS(&ring->readers_count, &users, users + 1)); |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | /* detach an appctx from a ring. The appctx is expected to be waiting at |
| 228 | * offset <ofs>. Nothing is done if <ring> is NULL. |
| 229 | */ |
| 230 | void ring_detach_appctx(struct ring *ring, struct appctx *appctx, size_t ofs) |
| 231 | { |
| 232 | if (!ring) |
| 233 | return; |
| 234 | |
| 235 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
| 236 | if (ofs != ~0) { |
| 237 | /* reader was still attached */ |
| 238 | ofs -= ring->ofs; |
| 239 | BUG_ON(ofs >= b_size(&ring->buf)); |
| 240 | LIST_DEL_INIT(&appctx->wait_entry); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 241 | HA_ATOMIC_DEC(b_peek(&ring->buf, ofs)); |
Willy Tarreau | 928068a | 2020-05-19 19:14:42 +0200 | [diff] [blame] | 242 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 243 | HA_ATOMIC_DEC(&ring->readers_count); |
Willy Tarreau | 928068a | 2020-05-19 19:14:42 +0200 | [diff] [blame] | 244 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 245 | } |
| 246 | |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 247 | /* Tries to attach CLI handler <appctx> as a new reader on ring <ring>. This is |
| 248 | * meant to be used when registering a CLI function to dump a buffer, so it |
| 249 | * returns zero on success, or non-zero on failure with a message in the appctx |
Willy Tarreau | fcf9498 | 2019-11-15 15:07:21 +0100 | [diff] [blame] | 250 | * CLI context. It automatically sets the io_handler and io_release callbacks if |
Willy Tarreau | cba8838 | 2022-05-05 15:18:57 +0200 | [diff] [blame^] | 251 | * they were not set. The <flags> take a combination of RING_WF_*. |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 252 | */ |
Willy Tarreau | cba8838 | 2022-05-05 15:18:57 +0200 | [diff] [blame^] | 253 | int ring_attach_cli(struct ring *ring, struct appctx *appctx, uint flags) |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 254 | { |
Emeric Brun | dcd58af | 2020-05-28 14:39:30 +0200 | [diff] [blame] | 255 | if (!ring_attach(ring)) |
Willy Tarreau | 928068a | 2020-05-19 19:14:42 +0200 | [diff] [blame] | 256 | return cli_err(appctx, |
| 257 | "Sorry, too many watchers (255) on this ring buffer. " |
| 258 | "What could it have so interesting to attract so many watchers ?"); |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 259 | |
Willy Tarreau | fcf9498 | 2019-11-15 15:07:21 +0100 | [diff] [blame] | 260 | if (!appctx->io_handler) |
| 261 | appctx->io_handler = cli_io_handler_show_ring; |
| 262 | if (!appctx->io_release) |
| 263 | appctx->io_release = cli_io_release_show_ring; |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 264 | appctx->ctx.cli.p0 = ring; |
Willy Tarreau | 13696ff | 2019-08-30 10:16:14 +0200 | [diff] [blame] | 265 | appctx->ctx.cli.o0 = ~0; // start from the oldest event |
Willy Tarreau | cba8838 | 2022-05-05 15:18:57 +0200 | [diff] [blame^] | 266 | appctx->ctx.cli.i0 = flags; |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | /* This function dumps all events from the ring whose pointer is in <p0> into |
Willy Tarreau | 13696ff | 2019-08-30 10:16:14 +0200 | [diff] [blame] | 271 | * the appctx's output buffer, and takes from <o0> the seek offset into the |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 272 | * buffer's history (0 for oldest known event). It looks at <i0> for boolean |
| 273 | * options: bit0 means it must wait for new data or any key to be pressed. Bit1 |
| 274 | * means it must seek directly to the end to wait for new contents. It returns |
| 275 | * 0 if the output buffer or events are missing is full and it needs to be |
| 276 | * called again, otherwise non-zero. It is meant to be used with |
| 277 | * cli_release_show_ring() to clean up. |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 278 | */ |
| 279 | int cli_io_handler_show_ring(struct appctx *appctx) |
| 280 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 281 | struct conn_stream *cs = appctx->owner; |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 282 | struct ring *ring = appctx->ctx.cli.p0; |
| 283 | struct buffer *buf = &ring->buf; |
Willy Tarreau | 13696ff | 2019-08-30 10:16:14 +0200 | [diff] [blame] | 284 | size_t ofs = appctx->ctx.cli.o0; |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 285 | uint64_t msg_len; |
| 286 | size_t len, cnt; |
| 287 | int ret; |
| 288 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 289 | if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 290 | return 1; |
| 291 | |
Willy Tarreau | 223dded | 2020-05-19 19:21:45 +0200 | [diff] [blame] | 292 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 9597cbd | 2020-05-19 17:07:30 +0200 | [diff] [blame] | 293 | LIST_DEL_INIT(&appctx->wait_entry); |
Willy Tarreau | 223dded | 2020-05-19 19:21:45 +0200 | [diff] [blame] | 294 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 295 | |
| 296 | HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 297 | |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 298 | /* explanation for the initialization below: it would be better to do |
| 299 | * this in the parsing function but this would occasionally result in |
| 300 | * dropped events because we'd take a reference on the oldest message |
| 301 | * and keep it while being scheduled. Thus instead let's take it the |
| 302 | * first time we enter here so that we have a chance to pass many |
Willy Tarreau | 13696ff | 2019-08-30 10:16:14 +0200 | [diff] [blame] | 303 | * existing messages before grabbing a reference to a location. This |
| 304 | * value cannot be produced after initialization. |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 305 | */ |
Willy Tarreau | 13696ff | 2019-08-30 10:16:14 +0200 | [diff] [blame] | 306 | if (unlikely(ofs == ~0)) { |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 307 | ofs = 0; |
| 308 | |
| 309 | /* going to the end means looking at tail-1 */ |
Willy Tarreau | cba8838 | 2022-05-05 15:18:57 +0200 | [diff] [blame^] | 310 | if (appctx->ctx.cli.i0 & RING_WF_SEEK_NEW) |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 311 | ofs += b_data(buf) - 1; |
| 312 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 313 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 314 | ofs += ring->ofs; |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* we were already there, adjust the offset to be relative to |
| 318 | * the buffer's head and remove us from the counter. |
| 319 | */ |
| 320 | ofs -= ring->ofs; |
| 321 | BUG_ON(ofs >= buf->size); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 322 | HA_ATOMIC_DEC(b_peek(buf, ofs)); |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 323 | |
| 324 | /* in this loop, ofs always points to the counter byte that precedes |
| 325 | * the message so that we can take our reference there if we have to |
| 326 | * stop before the end (ret=0). |
| 327 | */ |
| 328 | ret = 1; |
| 329 | while (ofs + 1 < b_data(buf)) { |
| 330 | cnt = 1; |
| 331 | len = b_peek_varint(buf, ofs + cnt, &msg_len); |
| 332 | if (!len) |
| 333 | break; |
| 334 | cnt += len; |
| 335 | BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf)); |
| 336 | |
| 337 | if (unlikely(msg_len + 1 > b_size(&trash))) { |
| 338 | /* too large a message to ever fit, let's skip it */ |
| 339 | ofs += cnt + msg_len; |
| 340 | continue; |
| 341 | } |
| 342 | |
| 343 | chunk_reset(&trash); |
| 344 | len = b_getblk(buf, trash.area, msg_len, ofs + cnt); |
| 345 | trash.data += len; |
| 346 | trash.area[trash.data++] = '\n'; |
| 347 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 348 | if (ci_putchk(cs_ic(cs), &trash) == -1) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 349 | cs_rx_room_blk(cs); |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 350 | ret = 0; |
| 351 | break; |
| 352 | } |
| 353 | ofs += cnt + msg_len; |
| 354 | } |
| 355 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 356 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 357 | ofs += ring->ofs; |
Willy Tarreau | 13696ff | 2019-08-30 10:16:14 +0200 | [diff] [blame] | 358 | appctx->ctx.cli.o0 = ofs; |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 359 | HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 360 | |
Willy Tarreau | cba8838 | 2022-05-05 15:18:57 +0200 | [diff] [blame^] | 361 | if (ret && (appctx->ctx.cli.i0 & RING_WF_WAIT_MODE)) { |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 362 | /* we've drained everything and are configured to wait for more |
| 363 | * data or an event (keypress, close) |
| 364 | */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 365 | if (!cs_oc(cs)->output && !(cs_oc(cs)->flags & CF_SHUTW)) { |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 366 | /* let's be woken up once new data arrive */ |
Willy Tarreau | 223dded | 2020-05-19 19:21:45 +0200 | [diff] [blame] | 367 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 368 | LIST_APPEND(&ring->waiters, &appctx->wait_entry); |
Willy Tarreau | 223dded | 2020-05-19 19:21:45 +0200 | [diff] [blame] | 369 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 370 | cs_rx_endp_done(cs); |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 371 | ret = 0; |
| 372 | } |
| 373 | /* always drain all the request */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 374 | co_skip(cs_oc(cs), cs_oc(cs)->output); |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 375 | } |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 376 | return ret; |
| 377 | } |
| 378 | |
| 379 | /* must be called after cli_io_handler_show_ring() above */ |
| 380 | void cli_io_release_show_ring(struct appctx *appctx) |
| 381 | { |
| 382 | struct ring *ring = appctx->ctx.cli.p0; |
Willy Tarreau | 13696ff | 2019-08-30 10:16:14 +0200 | [diff] [blame] | 383 | size_t ofs = appctx->ctx.cli.o0; |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 384 | |
Willy Tarreau | 928068a | 2020-05-19 19:14:42 +0200 | [diff] [blame] | 385 | ring_detach_appctx(ring, appctx, ofs); |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | |
Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 389 | /* |
| 390 | * Local variables: |
| 391 | * c-indent-level: 8 |
| 392 | * c-basic-offset: 8 |
| 393 | * End: |
| 394 | */ |