]>
git.proxmox.com Git - mirror_frr.git/blob - lib/buffer.c
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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "lib_errors.h"
32 DEFINE_MTYPE_STATIC(LIB
, BUFFER
, "Buffer")
33 DEFINE_MTYPE_STATIC(LIB
, BUFFER_DATA
, "Buffer data")
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
[]; /* 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) XFREE(MTYPE_BUFFER_DATA, (D))
67 /* Make new buffer. */
68 struct buffer
*buffer_new(size_t size
)
72 b
= XCALLOC(MTYPE_BUFFER
, sizeof(struct buffer
));
77 static size_t default_size
;
79 long pgsz
= sysconf(_SC_PAGESIZE
);
80 default_size
= ((((BUFFER_SIZE_DEFAULT
- 1) / pgsz
) + 1)
83 b
->size
= default_size
;
90 void buffer_free(struct buffer
*b
)
93 XFREE(MTYPE_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
= XMALLOC(MTYPE_TMP
, totlen
+ 1)))
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 /* Clear and free all allocated data. */
118 void buffer_reset(struct buffer
*b
)
120 struct buffer_data
*data
;
121 struct buffer_data
*next
;
123 for (data
= b
->head
; data
; data
= next
) {
125 BUFFER_DATA_FREE(data
);
127 b
->head
= b
->tail
= NULL
;
130 /* Add buffer_data to the end of buffer. */
131 static struct buffer_data
*buffer_add(struct buffer
*b
)
133 struct buffer_data
*d
;
135 d
= XMALLOC(MTYPE_BUFFER_DATA
,
136 offsetof(struct buffer_data
, data
) + b
->size
);
149 /* Write data to buffer. */
150 void buffer_put(struct buffer
*b
, const void *p
, size_t size
)
152 struct buffer_data
*data
= b
->tail
;
155 /* We use even last one byte of data buffer. */
159 /* If there is no data buffer add it. */
160 if (data
== NULL
|| data
->cp
== b
->size
)
161 data
= buffer_add(b
);
163 chunk
= ((size
<= (b
->size
- data
->cp
)) ? size
164 : (b
->size
- data
->cp
));
165 memcpy((data
->data
+ data
->cp
), ptr
, chunk
);
172 /* Insert character into the buffer. */
173 void buffer_putc(struct buffer
*b
, uint8_t c
)
175 buffer_put(b
, &c
, 1);
178 /* Put string to the buffer. */
179 void buffer_putstr(struct buffer
*b
, const char *c
)
181 buffer_put(b
, c
, strlen(c
));
184 /* Expand \n to \r\n */
185 void buffer_put_crlf(struct buffer
*b
, const void *origp
, size_t origsize
)
187 struct buffer_data
*data
= b
->tail
;
188 const char *p
= origp
, *end
= p
+ origsize
, *lf
;
191 lf
= memchr(p
, '\n', end
- p
);
193 /* We use even last one byte of data buffer. */
197 /* If there is no data buffer add it. */
198 if (data
== NULL
|| data
->cp
== b
->size
)
199 data
= buffer_add(b
);
201 size
= (lf
? lf
: end
) - p
;
202 avail
= b
->size
- data
->cp
;
204 chunk
= (size
<= avail
) ? size
: avail
;
205 memcpy(data
->data
+ data
->cp
, p
, chunk
);
210 if (lf
&& size
<= avail
) {
211 /* we just copied up to (including) a '\n' */
212 if (data
->cp
== b
->size
)
213 data
= buffer_add(b
);
214 data
->data
[data
->cp
++] = '\r';
215 if (data
->cp
== b
->size
)
216 data
= buffer_add(b
);
217 data
->data
[data
->cp
++] = '\n';
220 lf
= memchr(p
, '\n', end
- p
);
225 /* Keep flushing data to the fd until the buffer is empty or an error is
226 encountered or the operation would block. */
227 buffer_status_t
buffer_flush_all(struct buffer
*b
, int fd
)
230 struct buffer_data
*head
;
235 head_sp
= (head
= b
->head
)->sp
;
236 /* Flush all data. */
237 while ((ret
= buffer_flush_available(b
, fd
)) == BUFFER_PENDING
) {
238 if ((b
->head
== head
) && (head_sp
== head
->sp
)
240 /* No data was flushed, so kernel buffer must be full.
243 head_sp
= (head
= b
->head
)->sp
;
249 /* Flush enough data to fill a terminal window of the given scene (used only
250 by vty telnet interface). */
251 buffer_status_t
buffer_flush_window(struct buffer
*b
, int fd
, int width
,
252 int height
, int erase_flag
,
259 struct iovec small_iov
[3];
260 char more
[] = " --More-- ";
261 char erase
[] = {0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
262 0x08, 0x08, ' ', ' ', ' ', ' ', ' ', ' ',
263 ' ', ' ', ' ', ' ', 0x08, 0x08, 0x08, 0x08,
264 0x08, 0x08, 0x08, 0x08, 0x08, 0x08};
265 struct buffer_data
*data
;
273 else if (height
>= 2)
278 /* For erase and more data add two to b's buffer_data count.*/
279 if (b
->head
->next
== NULL
) {
280 iov_alloc
= array_size(small_iov
);
283 iov_alloc
= ((height
* (width
+ 2)) / b
->size
) + 10;
284 iov
= XMALLOC(MTYPE_TMP
, iov_alloc
* sizeof(*iov
));
288 /* Previously print out is performed. */
290 iov
[iov_index
].iov_base
= erase
;
291 iov
[iov_index
].iov_len
= sizeof(erase
);
296 column
= 1; /* Column position of next character displayed. */
297 for (data
= b
->head
; data
&& (height
> 0); data
= data
->next
) {
301 while ((cp
< data
->cp
) && (height
> 0)) {
302 /* Calculate lines remaining and column position after
305 if (data
->data
[cp
] == '\r')
307 else if ((data
->data
[cp
] == '\n')
308 || (column
== width
)) {
315 iov
[iov_index
].iov_base
= (char *)(data
->data
+ data
->sp
);
316 iov
[iov_index
++].iov_len
= cp
- data
->sp
;
319 if (iov_index
== iov_alloc
)
320 /* This should not ordinarily happen. */
323 if (iov
!= small_iov
) {
324 iov
= XREALLOC(MTYPE_TMP
, iov
,
325 iov_alloc
* sizeof(*iov
));
327 /* This should absolutely never occur. */
330 "%s: corruption detected: iov_small overflowed; head %p, tail %p, head->next %p",
331 __func__
, (void *)b
->head
,
332 (void *)b
->tail
, (void *)b
->head
->next
);
333 iov
= XMALLOC(MTYPE_TMP
,
334 iov_alloc
* sizeof(*iov
));
335 memcpy(iov
, small_iov
, sizeof(small_iov
));
340 /* In case of `more' display need. */
341 if (b
->tail
&& (b
->tail
->sp
< b
->tail
->cp
) && !no_more_flag
) {
342 iov
[iov_index
].iov_base
= more
;
343 iov
[iov_index
].iov_len
= sizeof(more
);
349 /* IOV_MAX are normally defined in <sys/uio.h> , Posix.1g.
350 example: Solaris2.6 are defined IOV_MAX size at 16. */
352 struct iovec
*c_iov
= iov
;
353 nbytes
= 0; /* Make sure it's initialized. */
355 while (iov_index
> 0) {
359 ((iov_index
> IOV_MAX
) ? IOV_MAX
: iov_index
);
360 if ((nbytes
= writev(fd
, c_iov
, iov_size
)) < 0) {
361 flog_err(EC_LIB_SOCKET
,
362 "%s: writev to fd %d failed: %s",
363 __func__
, fd
, safe_strerror(errno
));
367 /* move pointer io-vector */
369 iov_index
-= iov_size
;
373 if ((nbytes
= writev(fd
, iov
, iov_index
)) < 0)
374 flog_err(EC_LIB_SOCKET
, "%s: writev to fd %d failed: %s",
375 __func__
, fd
, safe_strerror(errno
));
378 /* Free printed buffer data. */
379 while (b
->head
&& (b
->head
->sp
== b
->head
->cp
)) {
380 struct buffer_data
*del
;
381 if (!(b
->head
= (del
= b
->head
)->next
))
383 BUFFER_DATA_FREE(del
);
386 if (iov
!= small_iov
)
387 XFREE(MTYPE_TMP
, iov
);
389 return (nbytes
< 0) ? BUFFER_ERROR
390 : (b
->head
? BUFFER_PENDING
: BUFFER_EMPTY
);
393 /* This function (unlike other buffer_flush* functions above) is designed
394 to work with non-blocking sockets. It does not attempt to write out
395 all of the queued data, just a "big" chunk. It returns 0 if it was
396 able to empty out the buffers completely, 1 if more flushing is
397 required later, or -1 on a fatal write error. */
398 buffer_status_t
buffer_flush_available(struct buffer
*b
, int fd
)
401 /* These are just reasonable values to make sure a significant amount of
402 data is written. There's no need to go crazy and try to write it all
405 #define MAX_CHUNKS ((IOV_MAX >= 16) ? 16 : IOV_MAX)
407 #define MAX_CHUNKS 16
409 #define MAX_FLUSH 131072
411 struct buffer_data
*d
;
413 struct iovec iov
[MAX_CHUNKS
];
420 for (d
= b
->head
; d
&& (iovcnt
< MAX_CHUNKS
) && (nbyte
< MAX_FLUSH
);
421 d
= d
->next
, iovcnt
++) {
422 iov
[iovcnt
].iov_base
= d
->data
+ d
->sp
;
423 nbyte
+= (iov
[iovcnt
].iov_len
= d
->cp
- d
->sp
);
427 /* No data to flush: should we issue a warning message? */
430 /* only place where written should be sign compared */
431 if ((ssize_t
)(written
= writev(fd
, iov
, iovcnt
)) < 0) {
432 if (ERRNO_IO_RETRY(errno
))
433 /* Calling code should try again later. */
434 return BUFFER_PENDING
;
435 flog_err(EC_LIB_SOCKET
, "%s: write error on fd %d: %s",
436 __func__
, fd
, safe_strerror(errno
));
440 /* Free printed buffer data. */
441 while (written
> 0) {
442 if (!(d
= b
->head
)) {
445 "%s: corruption detected: buffer queue empty, but written is %lu",
446 __func__
, (unsigned long)written
);
449 if (written
< d
->cp
- d
->sp
) {
451 return BUFFER_PENDING
;
454 written
-= (d
->cp
- d
->sp
);
455 if (!(b
->head
= d
->next
))
460 return b
->head
? BUFFER_PENDING
: BUFFER_EMPTY
;
466 buffer_status_t
buffer_write(struct buffer
*b
, int fd
, const void *p
,
472 /* Buffer is not empty, so do not attempt to write the new data.
475 else if ((nbytes
= write(fd
, p
, size
)) < 0) {
476 if (ERRNO_IO_RETRY(errno
))
479 flog_err(EC_LIB_SOCKET
, "%s: write error on fd %d: %s",
480 __func__
, fd
, safe_strerror(errno
));
484 /* Add any remaining data to the buffer. */
486 size_t written
= nbytes
;
488 buffer_put(b
, ((const char *)p
) + written
,
491 return b
->head
? BUFFER_PENDING
: BUFFER_EMPTY
;