2 * Buffering of output and input.
3 * Copyright (C) 1998 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
31 #include <osmocore/talloc.h>
32 #include <osmocom/vty/buffer.h>
33 #include <osmocom/vty/vty.h>
38 struct buffer_data *head;
39 struct buffer_data *tail;
41 /* Size of each buffer_data chunk. */
47 struct buffer_data *next;
49 /* Location to add new data. */
52 /* Pointer to data not yet flushed. */
55 /* Actual data stream (variable length). */
56 unsigned char data[0]; /* real dimension is buffer->size */
59 /* It should always be true that: 0 <= sp <= cp <= size */
61 /* Default buffer size (used if none specified). It is rounded up to the
62 next page boundery. */
63 #define BUFFER_SIZE_DEFAULT 4096
65 #define BUFFER_DATA_FREE(D) talloc_free((D))
67 /* Make new buffer. */
68 struct buffer *buffer_new(void *ctx, size_t size)
72 b = talloc_zero(ctx, struct buffer);
77 static size_t default_size;
79 long pgsz = sysconf(_SC_PAGESIZE);
81 ((((BUFFER_SIZE_DEFAULT - 1) / pgsz) + 1) * pgsz);
83 b->size = default_size;
90 void buffer_free(struct buffer *b)
96 /* Make string clone. */
97 char *buffer_getstr(struct buffer *b)
100 struct buffer_data *data;
104 for (data = b->head; data; data = data->next)
105 totlen += data->cp - data->sp;
106 if (!(s = _talloc_zero(tall_vty_ctx, (totlen + 1), "buffer_getstr")))
109 for (data = b->head; data; data = data->next) {
110 memcpy(p, data->data + data->sp, data->cp - data->sp);
111 p += data->cp - data->sp;
117 /* Return 1 if buffer is empty. */
118 int buffer_empty(struct buffer *b)
120 return (b->head == NULL);
123 /* Clear and free all allocated data. */
124 void buffer_reset(struct buffer *b)
126 struct buffer_data *data;
127 struct buffer_data *next;
129 for (data = b->head; data; data = next) {
131 BUFFER_DATA_FREE(data);
133 b->head = b->tail = NULL;
136 /* Add buffer_data to the end of buffer. */
137 static struct buffer_data *buffer_add(struct buffer *b)
139 struct buffer_data *d;
142 offsetof(struct buffer_data, data[b->size]),
158 /* Write data to buffer. */
159 void buffer_put(struct buffer *b, const void *p, size_t size)
161 struct buffer_data *data = b->tail;
164 /* We use even last one byte of data buffer. */
168 /* If there is no data buffer add it. */
169 if (data == NULL || data->cp == b->size)
170 data = buffer_add(b);
174 (b->size - data->cp)) ? size : (b->size - data->cp));
175 memcpy((data->data + data->cp), ptr, chunk);
182 /* Insert character into the buffer. */
183 void buffer_putc(struct buffer *b, u_char c)
185 buffer_put(b, &c, 1);
188 /* Put string to the buffer. */
189 void buffer_putstr(struct buffer *b, const char *c)
191 buffer_put(b, c, strlen(c));
194 /* Keep flushing data to the fd until the buffer is empty or an error is
195 encountered or the operation would block. */
196 buffer_status_t buffer_flush_all(struct buffer *b, int fd)
199 struct buffer_data *head;
204 head_sp = (head = b->head)->sp;
205 /* Flush all data. */
206 while ((ret = buffer_flush_available(b, fd)) == BUFFER_PENDING) {
207 if ((b->head == head) && (head_sp == head->sp)
209 /* No data was flushed, so kernel buffer must be full. */
211 head_sp = (head = b->head)->sp;
218 /* Flush enough data to fill a terminal window of the given scene (used only
219 by vty telnet interface). */
221 buffer_flush_window(struct buffer * b, int fd, int width, int height,
222 int erase_flag, int no_more_flag)
228 struct iovec small_iov[3];
229 char more[] = " --More-- ";
231 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
232 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
233 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08
235 struct buffer_data *data;
243 ("%s called with non-positive window height %d, forcing to 1",
246 } else if (height >= 2)
250 ("%s called with non-positive window width %d, forcing to 1",
255 /* For erase and more data add two to b's buffer_data count. */
256 if (b->head->next == NULL) {
257 iov_alloc = sizeof(small_iov) / sizeof(small_iov[0]);
260 iov_alloc = ((height * (width + 2)) / b->size) + 10;
261 iov = XMALLOC(MTYPE_TMP, iov_alloc * sizeof(*iov));
265 /* Previously print out is performed. */
267 iov[iov_index].iov_base = erase;
268 iov[iov_index].iov_len = sizeof erase;
273 column = 1; /* Column position of next character displayed. */
274 for (data = b->head; data && (height > 0); data = data->next) {
278 while ((cp < data->cp) && (height > 0)) {
279 /* Calculate lines remaining and column position after displaying
281 if (data->data[cp] == '\r')
283 else if ((data->data[cp] == '\n') || (column == width)) {
290 iov[iov_index].iov_base = (char *)(data->data + data->sp);
291 iov[iov_index++].iov_len = cp - data->sp;
294 if (iov_index == iov_alloc)
295 /* This should not ordinarily happen. */
298 if (iov != small_iov) {
299 zlog_warn("%s: growing iov array to %d; "
300 "width %d, height %d, size %lu",
301 __func__, iov_alloc, width, height,
304 XREALLOC(MTYPE_TMP, iov,
305 iov_alloc * sizeof(*iov));
307 /* This should absolutely never occur. */
309 ("%s: corruption detected: iov_small overflowed; "
310 "head %p, tail %p, head->next %p",
311 __func__, b->head, b->tail, b->head->next);
314 iov_alloc * sizeof(*iov));
315 memcpy(iov, small_iov, sizeof(small_iov));
320 /* In case of `more' display need. */
321 if (b->tail && (b->tail->sp < b->tail->cp) && !no_more_flag) {
322 iov[iov_index].iov_base = more;
323 iov[iov_index].iov_len = sizeof more;
327 /* IOV_MAX are normally defined in <sys/uio.h> , Posix.1g.
328 example: Solaris2.6 are defined IOV_MAX size at 16. */
330 struct iovec *c_iov = iov;
331 nbytes = 0; /* Make sure it's initialized. */
333 while (iov_index > 0) {
337 ((iov_index > IOV_MAX) ? IOV_MAX : iov_index);
338 if ((nbytes = writev(fd, c_iov, iov_size)) < 0) {
339 zlog_warn("%s: writev to fd %d failed: %s",
340 __func__, fd, safe_strerror(errno));
344 /* move pointer io-vector */
346 iov_index -= iov_size;
350 if ((nbytes = writev(fd, iov, iov_index)) < 0)
351 zlog_warn("%s: writev to fd %d failed: %s",
352 __func__, fd, safe_strerror(errno));
355 /* Free printed buffer data. */
356 while (b->head && (b->head->sp == b->head->cp)) {
357 struct buffer_data *del;
358 if (!(b->head = (del = b->head)->next))
360 BUFFER_DATA_FREE(del);
363 if (iov != small_iov)
364 XFREE(MTYPE_TMP, iov);
366 return (nbytes < 0) ? BUFFER_ERROR :
367 (b->head ? BUFFER_PENDING : BUFFER_EMPTY);
371 /* This function (unlike other buffer_flush* functions above) is designed
372 to work with non-blocking sockets. It does not attempt to write out
373 all of the queued data, just a "big" chunk. It returns 0 if it was
374 able to empty out the buffers completely, 1 if more flushing is
375 required later, or -1 on a fatal write error. */
376 buffer_status_t buffer_flush_available(struct buffer * b, int fd)
379 /* These are just reasonable values to make sure a significant amount of
380 data is written. There's no need to go crazy and try to write it all
383 #define MAX_CHUNKS ((IOV_MAX >= 16) ? 16 : IOV_MAX)
385 #define MAX_CHUNKS 16
387 #define MAX_FLUSH 131072
389 struct buffer_data *d;
391 struct iovec iov[MAX_CHUNKS];
395 for (d = b->head; d && (iovcnt < MAX_CHUNKS) && (nbyte < MAX_FLUSH);
396 d = d->next, iovcnt++) {
397 iov[iovcnt].iov_base = d->data + d->sp;
398 nbyte += (iov[iovcnt].iov_len = d->cp - d->sp);
402 /* No data to flush: should we issue a warning message? */
405 /* only place where written should be sign compared */
406 if ((ssize_t) (written = writev(fd, iov, iovcnt)) < 0) {
407 if (ERRNO_IO_RETRY(errno))
408 /* Calling code should try again later. */
409 return BUFFER_PENDING;
413 /* Free printed buffer data. */
414 while (written > 0) {
415 struct buffer_data *d;
418 if (written < d->cp - d->sp) {
420 return BUFFER_PENDING;
423 written -= (d->cp - d->sp);
424 if (!(b->head = d->next))
429 return b->head ? BUFFER_PENDING : BUFFER_EMPTY;
436 buffer_write(struct buffer * b, int fd, const void *p, size_t size)
441 /* Should we attempt to drain any previously buffered data? This could help reduce latency in pushing out the data if we are stuck in a long-running thread that is preventing the main select loop from calling the flush thread... */
443 if (b->head && (buffer_flush_available(b, fd) == BUFFER_ERROR))
447 /* Buffer is not empty, so do not attempt to write the new data. */
449 else if ((nbytes = write(fd, p, size)) < 0) {
450 if (ERRNO_IO_RETRY(errno))
455 /* Add any remaining data to the buffer. */
457 size_t written = nbytes;
459 buffer_put(b, ((const char *)p) + written,
462 return b->head ? BUFFER_PENDING : BUFFER_EMPTY;