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