]>
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 /* 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
;
141 d
= XMALLOC(MTYPE_BUFFER_DATA
,
142 offsetof(struct buffer_data
, data
) + b
->size
);
155 /* Write data to buffer. */
156 void buffer_put(struct buffer
*b
, const void *p
, size_t size
)
158 struct buffer_data
*data
= b
->tail
;
161 /* We use even last one byte of data buffer. */
165 /* If there is no data buffer add it. */
166 if (data
== NULL
|| data
->cp
== b
->size
)
167 data
= buffer_add(b
);
169 chunk
= ((size
<= (b
->size
- data
->cp
)) ? size
170 : (b
->size
- data
->cp
));
171 memcpy((data
->data
+ data
->cp
), ptr
, chunk
);
178 /* Insert character into the buffer. */
179 void buffer_putc(struct buffer
*b
, uint8_t c
)
181 buffer_put(b
, &c
, 1);
184 /* Put string to the buffer. */
185 void buffer_putstr(struct buffer
*b
, const char *c
)
187 buffer_put(b
, c
, strlen(c
));
190 /* Expand \n to \r\n */
191 void buffer_put_crlf(struct buffer
*b
, const void *origp
, size_t origsize
)
193 struct buffer_data
*data
= b
->tail
;
194 const char *p
= origp
, *end
= p
+ origsize
, *lf
;
197 lf
= memchr(p
, '\n', end
- p
);
199 /* We use even last one byte of data buffer. */
203 /* If there is no data buffer add it. */
204 if (data
== NULL
|| data
->cp
== b
->size
)
205 data
= buffer_add(b
);
207 size
= (lf
? lf
: end
) - p
;
208 avail
= b
->size
- data
->cp
;
210 chunk
= (size
<= avail
) ? size
: avail
;
211 memcpy(data
->data
+ data
->cp
, p
, chunk
);
216 if (lf
&& size
<= avail
) {
217 /* we just copied up to (including) a '\n' */
218 if (data
->cp
== b
->size
)
219 data
= buffer_add(b
);
220 data
->data
[data
->cp
++] = '\r';
221 if (data
->cp
== b
->size
)
222 data
= buffer_add(b
);
223 data
->data
[data
->cp
++] = '\n';
226 lf
= memchr(p
, '\n', end
- p
);
231 /* Keep flushing data to the fd until the buffer is empty or an error is
232 encountered or the operation would block. */
233 buffer_status_t
buffer_flush_all(struct buffer
*b
, int fd
)
236 struct buffer_data
*head
;
241 head_sp
= (head
= b
->head
)->sp
;
242 /* Flush all data. */
243 while ((ret
= buffer_flush_available(b
, fd
)) == BUFFER_PENDING
) {
244 if ((b
->head
== head
) && (head_sp
== head
->sp
)
246 /* No data was flushed, so kernel buffer must be full.
249 head_sp
= (head
= b
->head
)->sp
;
255 /* Flush enough data to fill a terminal window of the given scene (used only
256 by vty telnet interface). */
257 buffer_status_t
buffer_flush_window(struct buffer
*b
, int fd
, int width
,
258 int height
, int erase_flag
,
265 struct iovec small_iov
[3];
266 char more
[] = " --More-- ";
267 char erase
[] = {0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
268 0x08, 0x08, ' ', ' ', ' ', ' ', ' ', ' ',
269 ' ', ' ', ' ', ' ', 0x08, 0x08, 0x08, 0x08,
270 0x08, 0x08, 0x08, 0x08, 0x08, 0x08};
271 struct buffer_data
*data
;
279 else if (height
>= 2)
284 /* For erase and more data add two to b's buffer_data count.*/
285 if (b
->head
->next
== NULL
) {
286 iov_alloc
= array_size(small_iov
);
289 iov_alloc
= ((height
* (width
+ 2)) / b
->size
) + 10;
290 iov
= XMALLOC(MTYPE_TMP
, iov_alloc
* sizeof(*iov
));
294 /* Previously print out is performed. */
296 iov
[iov_index
].iov_base
= erase
;
297 iov
[iov_index
].iov_len
= sizeof erase
;
302 column
= 1; /* Column position of next character displayed. */
303 for (data
= b
->head
; data
&& (height
> 0); data
= data
->next
) {
307 while ((cp
< data
->cp
) && (height
> 0)) {
308 /* Calculate lines remaining and column position after
311 if (data
->data
[cp
] == '\r')
313 else if ((data
->data
[cp
] == '\n')
314 || (column
== width
)) {
321 iov
[iov_index
].iov_base
= (char *)(data
->data
+ data
->sp
);
322 iov
[iov_index
++].iov_len
= cp
- data
->sp
;
325 if (iov_index
== iov_alloc
)
326 /* This should not ordinarily happen. */
329 if (iov
!= small_iov
) {
330 iov
= XREALLOC(MTYPE_TMP
, iov
,
331 iov_alloc
* sizeof(*iov
));
333 /* This should absolutely never occur. */
336 "%s: corruption detected: iov_small overflowed; "
337 "head %p, tail %p, head->next %p",
338 __func__
, (void *)b
->head
,
339 (void *)b
->tail
, (void *)b
->head
->next
);
340 iov
= XMALLOC(MTYPE_TMP
,
341 iov_alloc
* sizeof(*iov
));
342 memcpy(iov
, small_iov
, sizeof(small_iov
));
347 /* In case of `more' display need. */
348 if (b
->tail
&& (b
->tail
->sp
< b
->tail
->cp
) && !no_more_flag
) {
349 iov
[iov_index
].iov_base
= more
;
350 iov
[iov_index
].iov_len
= sizeof more
;
356 /* IOV_MAX are normally defined in <sys/uio.h> , Posix.1g.
357 example: Solaris2.6 are defined IOV_MAX size at 16. */
359 struct iovec
*c_iov
= iov
;
360 nbytes
= 0; /* Make sure it's initialized. */
362 while (iov_index
> 0) {
366 ((iov_index
> IOV_MAX
) ? IOV_MAX
: iov_index
);
367 if ((nbytes
= writev(fd
, c_iov
, iov_size
)) < 0) {
368 flog_err(EC_LIB_SOCKET
,
369 "%s: writev to fd %d failed: %s",
370 __func__
, fd
, safe_strerror(errno
));
374 /* move pointer io-vector */
376 iov_index
-= iov_size
;
380 if ((nbytes
= writev(fd
, iov
, iov_index
)) < 0)
381 flog_err(EC_LIB_SOCKET
, "%s: writev to fd %d failed: %s",
382 __func__
, fd
, safe_strerror(errno
));
385 /* Free printed buffer data. */
386 while (b
->head
&& (b
->head
->sp
== b
->head
->cp
)) {
387 struct buffer_data
*del
;
388 if (!(b
->head
= (del
= b
->head
)->next
))
390 BUFFER_DATA_FREE(del
);
393 if (iov
!= small_iov
)
394 XFREE(MTYPE_TMP
, iov
);
396 return (nbytes
< 0) ? BUFFER_ERROR
397 : (b
->head
? BUFFER_PENDING
: BUFFER_EMPTY
);
400 /* This function (unlike other buffer_flush* functions above) is designed
401 to work with non-blocking sockets. It does not attempt to write out
402 all of the queued data, just a "big" chunk. It returns 0 if it was
403 able to empty out the buffers completely, 1 if more flushing is
404 required later, or -1 on a fatal write error. */
405 buffer_status_t
buffer_flush_available(struct buffer
*b
, int fd
)
408 /* These are just reasonable values to make sure a significant amount of
409 data is written. There's no need to go crazy and try to write it all
412 #define MAX_CHUNKS ((IOV_MAX >= 16) ? 16 : IOV_MAX)
414 #define MAX_CHUNKS 16
416 #define MAX_FLUSH 131072
418 struct buffer_data
*d
;
420 struct iovec iov
[MAX_CHUNKS
];
427 for (d
= b
->head
; d
&& (iovcnt
< MAX_CHUNKS
) && (nbyte
< MAX_FLUSH
);
428 d
= d
->next
, iovcnt
++) {
429 iov
[iovcnt
].iov_base
= d
->data
+ d
->sp
;
430 nbyte
+= (iov
[iovcnt
].iov_len
= d
->cp
- d
->sp
);
434 /* No data to flush: should we issue a warning message? */
437 /* only place where written should be sign compared */
438 if ((ssize_t
)(written
= writev(fd
, iov
, iovcnt
)) < 0) {
439 if (ERRNO_IO_RETRY(errno
))
440 /* Calling code should try again later. */
441 return BUFFER_PENDING
;
442 flog_err(EC_LIB_SOCKET
, "%s: write error on fd %d: %s",
443 __func__
, fd
, safe_strerror(errno
));
447 /* Free printed buffer data. */
448 while (written
> 0) {
449 if (!(d
= b
->head
)) {
452 "%s: corruption detected: buffer queue empty, but written is %lu",
453 __func__
, (unsigned long)written
);
456 if (written
< d
->cp
- d
->sp
) {
458 return BUFFER_PENDING
;
461 written
-= (d
->cp
- d
->sp
);
462 if (!(b
->head
= d
->next
))
467 return b
->head
? BUFFER_PENDING
: BUFFER_EMPTY
;
473 buffer_status_t
buffer_write(struct buffer
*b
, int fd
, const void *p
,
480 * Should we attempt to drain any previously buffered data?
481 * This could help reduce latency in pushing out the data if
482 * we are stuck in a long-running thread that is preventing
483 * the main select loop from calling the flush thread...
485 if (b
->head
&& (buffer_flush_available(b
, fd
) == BUFFER_ERROR
))
489 /* Buffer is not empty, so do not attempt to write the new data.
492 else if ((nbytes
= write(fd
, p
, size
)) < 0) {
493 if (ERRNO_IO_RETRY(errno
))
496 flog_err(EC_LIB_SOCKET
, "%s: write error on fd %d: %s",
497 __func__
, fd
, safe_strerror(errno
));
501 /* Add any remaining data to the buffer. */
503 size_t written
= nbytes
;
505 buffer_put(b
, ((const char *)p
) + written
,
508 return b
->head
? BUFFER_PENDING
: BUFFER_EMPTY
;