paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Buffering of output and input. |
| 3 | * Copyright (C) 1998 Kunihiro Ishiguro |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
| 25 | #include "memory.h" |
| 26 | #include "buffer.h" |
paul | 2265d20 | 2004-11-08 15:43:21 +0000 | [diff] [blame] | 27 | #include "log.h" |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 28 | #include "network.h" |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 29 | #include <stddef.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 30 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 32 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 33 | /* Buffer master. */ |
| 34 | struct buffer |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | { |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 36 | /* Data list. */ |
| 37 | struct buffer_data *head; |
| 38 | struct buffer_data *tail; |
| 39 | |
| 40 | /* Size of each buffer_data chunk. */ |
| 41 | size_t size; |
| 42 | }; |
| 43 | |
| 44 | /* Data container. */ |
| 45 | struct buffer_data |
| 46 | { |
| 47 | struct buffer_data *next; |
| 48 | |
| 49 | /* Location to add new data. */ |
| 50 | size_t cp; |
| 51 | |
| 52 | /* Pointer to data not yet flushed. */ |
| 53 | size_t sp; |
| 54 | |
| 55 | /* Actual data stream (variable length). */ |
| 56 | unsigned char data[0]; /* real dimension is buffer->size */ |
| 57 | }; |
| 58 | |
| 59 | /* It should always be true that: 0 <= sp <= cp <= size */ |
| 60 | |
| 61 | /* Default buffer size (used if none specified). It is rounded up to the |
| 62 | next page boundery. */ |
| 63 | #define BUFFER_SIZE_DEFAULT 4096 |
| 64 | |
| 65 | |
| 66 | #define BUFFER_DATA_FREE(D) XFREE(MTYPE_BUFFER_DATA, (D)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 67 | |
| 68 | /* Make new buffer. */ |
| 69 | struct buffer * |
| 70 | buffer_new (size_t size) |
| 71 | { |
| 72 | struct buffer *b; |
| 73 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 74 | b = XCALLOC (MTYPE_BUFFER, sizeof (struct buffer)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 75 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 76 | if (size) |
| 77 | b->size = size; |
| 78 | else |
| 79 | { |
| 80 | static size_t default_size; |
| 81 | if (!default_size) |
| 82 | { |
| 83 | long pgsz = sysconf(_SC_PAGESIZE); |
| 84 | default_size = ((((BUFFER_SIZE_DEFAULT-1)/pgsz)+1)*pgsz); |
| 85 | } |
| 86 | b->size = default_size; |
| 87 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 88 | |
| 89 | return b; |
| 90 | } |
| 91 | |
| 92 | /* Free buffer. */ |
| 93 | void |
| 94 | buffer_free (struct buffer *b) |
| 95 | { |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 96 | buffer_reset(b); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 97 | XFREE (MTYPE_BUFFER, b); |
| 98 | } |
| 99 | |
| 100 | /* Make string clone. */ |
| 101 | char * |
| 102 | buffer_getstr (struct buffer *b) |
| 103 | { |
ajs | afb8b60 | 2005-01-28 20:41:07 +0000 | [diff] [blame] | 104 | size_t totlen = 0; |
| 105 | struct buffer_data *data; |
| 106 | char *s; |
| 107 | char *p; |
| 108 | |
| 109 | for (data = b->head; data; data = data->next) |
| 110 | totlen += data->cp - data->sp; |
ajs | 3b8b185 | 2005-01-29 18:19:13 +0000 | [diff] [blame] | 111 | if (!(s = XMALLOC(MTYPE_TMP, totlen+1))) |
ajs | afb8b60 | 2005-01-28 20:41:07 +0000 | [diff] [blame] | 112 | return NULL; |
| 113 | p = s; |
| 114 | for (data = b->head; data; data = data->next) |
| 115 | { |
| 116 | memcpy(p, data->data + data->sp, data->cp - data->sp); |
| 117 | p += data->cp - data->sp; |
| 118 | } |
| 119 | *p = '\0'; |
| 120 | return s; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* Return 1 if buffer is empty. */ |
| 124 | int |
| 125 | buffer_empty (struct buffer *b) |
| 126 | { |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 127 | return (b->head == NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* Clear and free all allocated data. */ |
| 131 | void |
| 132 | buffer_reset (struct buffer *b) |
| 133 | { |
| 134 | struct buffer_data *data; |
| 135 | struct buffer_data *next; |
| 136 | |
| 137 | for (data = b->head; data; data = next) |
| 138 | { |
| 139 | next = data->next; |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 140 | BUFFER_DATA_FREE(data); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 141 | } |
| 142 | b->head = b->tail = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* Add buffer_data to the end of buffer. */ |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 146 | static struct buffer_data * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 147 | buffer_add (struct buffer *b) |
| 148 | { |
| 149 | struct buffer_data *d; |
| 150 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 151 | d = XMALLOC(MTYPE_BUFFER_DATA, offsetof(struct buffer_data, data[b->size])); |
| 152 | d->cp = d->sp = 0; |
| 153 | d->next = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 154 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 155 | if (b->tail) |
| 156 | b->tail->next = d; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 157 | else |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 158 | b->head = d; |
| 159 | b->tail = d; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 160 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 161 | return d; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* Write data to buffer. */ |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 165 | void |
| 166 | buffer_put(struct buffer *b, const void *p, size_t size) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 167 | { |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 168 | struct buffer_data *data = b->tail; |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 169 | const char *ptr = p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 170 | |
| 171 | /* We use even last one byte of data buffer. */ |
| 172 | while (size) |
| 173 | { |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 174 | size_t chunk; |
| 175 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 176 | /* If there is no data buffer add it. */ |
| 177 | if (data == NULL || data->cp == b->size) |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 178 | data = buffer_add (b); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 179 | |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 180 | chunk = ((size <= (b->size - data->cp)) ? size : (b->size - data->cp)); |
| 181 | memcpy ((data->data + data->cp), ptr, chunk); |
| 182 | size -= chunk; |
| 183 | ptr += chunk; |
| 184 | data->cp += chunk; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 185 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* Insert character into the buffer. */ |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 189 | void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 190 | buffer_putc (struct buffer *b, u_char c) |
| 191 | { |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 192 | buffer_put(b, &c, 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* Put string to the buffer. */ |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 196 | void |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 197 | buffer_putstr (struct buffer *b, const char *c) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 198 | { |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 199 | buffer_put(b, c, strlen(c)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 200 | } |
| 201 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 202 | /* Keep flushing data to the fd until the buffer is empty or an error is |
| 203 | encountered or the operation would block. */ |
| 204 | buffer_status_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 205 | buffer_flush_all (struct buffer *b, int fd) |
| 206 | { |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 207 | buffer_status_t ret; |
| 208 | struct buffer_data *head; |
| 209 | size_t head_sp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 210 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 211 | if (!b->head) |
| 212 | return BUFFER_EMPTY; |
| 213 | head_sp = (head = b->head)->sp; |
| 214 | /* Flush all data. */ |
| 215 | while ((ret = buffer_flush_available(b, fd)) == BUFFER_PENDING) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 216 | { |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 217 | if ((b->head == head) && (head_sp == head->sp) && (errno != EINTR)) |
| 218 | /* No data was flushed, so kernel buffer must be full. */ |
| 219 | return ret; |
| 220 | head_sp = (head = b->head)->sp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 221 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 222 | |
| 223 | return ret; |
| 224 | } |
| 225 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 226 | /* Flush enough data to fill a terminal window of the given scene (used only |
| 227 | by vty telnet interface). */ |
| 228 | buffer_status_t |
| 229 | buffer_flush_window (struct buffer *b, int fd, int width, int height, |
| 230 | int erase_flag, int no_more_flag) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | { |
| 232 | int nbytes; |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 233 | int iov_alloc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 234 | int iov_index; |
| 235 | struct iovec *iov; |
| 236 | struct iovec small_iov[3]; |
| 237 | char more[] = " --More-- "; |
| 238 | char erase[] = { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, |
| 239 | ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', |
| 240 | 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08}; |
| 241 | struct buffer_data *data; |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 242 | int column; |
| 243 | |
| 244 | if (!b->head) |
| 245 | return BUFFER_EMPTY; |
| 246 | |
| 247 | if (height < 1) |
| 248 | { |
| 249 | zlog_warn("%s called with non-positive window height %d, forcing to 1", |
| 250 | __func__, height); |
| 251 | height = 1; |
| 252 | } |
| 253 | else if (height >= 2) |
| 254 | height--; |
| 255 | if (width < 1) |
| 256 | { |
| 257 | zlog_warn("%s called with non-positive window width %d, forcing to 1", |
| 258 | __func__, width); |
| 259 | width = 1; |
| 260 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 261 | |
| 262 | /* For erase and more data add two to b's buffer_data count.*/ |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 263 | if (b->head->next == NULL) |
| 264 | { |
| 265 | iov_alloc = sizeof(small_iov)/sizeof(small_iov[0]); |
| 266 | iov = small_iov; |
| 267 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 268 | else |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 269 | { |
| 270 | iov_alloc = ((height*(width+2))/b->size)+10; |
| 271 | iov = XMALLOC(MTYPE_TMP, iov_alloc*sizeof(*iov)); |
| 272 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 273 | iov_index = 0; |
| 274 | |
| 275 | /* Previously print out is performed. */ |
| 276 | if (erase_flag) |
| 277 | { |
| 278 | iov[iov_index].iov_base = erase; |
| 279 | iov[iov_index].iov_len = sizeof erase; |
| 280 | iov_index++; |
| 281 | } |
| 282 | |
| 283 | /* Output data. */ |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 284 | column = 1; /* Column position of next character displayed. */ |
| 285 | for (data = b->head; data && (height > 0); data = data->next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 286 | { |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 287 | size_t cp; |
| 288 | |
| 289 | cp = data->sp; |
| 290 | while ((cp < data->cp) && (height > 0)) |
| 291 | { |
| 292 | /* Calculate lines remaining and column position after displaying |
| 293 | this character. */ |
| 294 | if (data->data[cp] == '\r') |
| 295 | column = 1; |
| 296 | else if ((data->data[cp] == '\n') || (column == width)) |
| 297 | { |
| 298 | column = 1; |
| 299 | height--; |
| 300 | } |
| 301 | else |
| 302 | column++; |
| 303 | cp++; |
| 304 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 305 | iov[iov_index].iov_base = (char *)(data->data + data->sp); |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 306 | iov[iov_index++].iov_len = cp-data->sp; |
| 307 | data->sp = cp; |
| 308 | |
| 309 | if (iov_index == iov_alloc) |
| 310 | /* This should not ordinarily happen. */ |
| 311 | { |
| 312 | iov_alloc *= 2; |
| 313 | if (iov != small_iov) |
| 314 | { |
| 315 | zlog_warn("%s: growing iov array to %d; " |
| 316 | "width %d, height %d, size %lu", |
| 317 | __func__, iov_alloc, width, height, (u_long)b->size); |
| 318 | iov = XREALLOC(MTYPE_TMP, iov, iov_alloc*sizeof(*iov)); |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | /* This should absolutely never occur. */ |
| 323 | zlog_err("%s: corruption detected: iov_small overflowed; " |
| 324 | "head %p, tail %p, head->next %p", |
| 325 | __func__, b->head, b->tail, b->head->next); |
| 326 | iov = XMALLOC(MTYPE_TMP, iov_alloc*sizeof(*iov)); |
| 327 | memcpy(iov, small_iov, sizeof(small_iov)); |
| 328 | } |
| 329 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* In case of `more' display need. */ |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 333 | if (b->tail && (b->tail->sp < b->tail->cp) && !no_more_flag) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 334 | { |
| 335 | iov[iov_index].iov_base = more; |
| 336 | iov[iov_index].iov_len = sizeof more; |
| 337 | iov_index++; |
| 338 | } |
| 339 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 340 | |
| 341 | #ifdef IOV_MAX |
| 342 | /* IOV_MAX are normally defined in <sys/uio.h> , Posix.1g. |
| 343 | example: Solaris2.6 are defined IOV_MAX size at 16. */ |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 344 | { |
| 345 | struct iovec *c_iov = iov; |
| 346 | nbytes = 0; /* Make sure it's initialized. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 347 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 348 | while (iov_index > 0) |
| 349 | { |
| 350 | int iov_size; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 351 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 352 | iov_size = ((iov_index > IOV_MAX) ? IOV_MAX : iov_index); |
| 353 | if ((nbytes = writev(fd, c_iov, iov_size)) < 0) |
| 354 | { |
| 355 | zlog_warn("%s: writev to fd %d failed: %s", |
| 356 | __func__, fd, safe_strerror(errno)); |
| 357 | break; |
| 358 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 359 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 360 | /* move pointer io-vector */ |
| 361 | c_iov += iov_size; |
| 362 | iov_index -= iov_size; |
| 363 | } |
| 364 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 365 | #else /* IOV_MAX */ |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 366 | if ((nbytes = writev (fd, iov, iov_index)) < 0) |
| 367 | zlog_warn("%s: writev to fd %d failed: %s", |
| 368 | __func__, fd, safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 369 | #endif /* IOV_MAX */ |
| 370 | |
| 371 | /* Free printed buffer data. */ |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 372 | while (b->head && (b->head->sp == b->head->cp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 373 | { |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 374 | struct buffer_data *del; |
| 375 | if (!(b->head = (del = b->head)->next)) |
| 376 | b->tail = NULL; |
| 377 | BUFFER_DATA_FREE(del); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | if (iov != small_iov) |
| 381 | XFREE (MTYPE_TMP, iov); |
| 382 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 383 | return (nbytes < 0) ? BUFFER_ERROR : |
| 384 | (b->head ? BUFFER_PENDING : BUFFER_EMPTY); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 385 | } |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 386 | |
| 387 | /* This function (unlike other buffer_flush* functions above) is designed |
| 388 | to work with non-blocking sockets. It does not attempt to write out |
| 389 | all of the queued data, just a "big" chunk. It returns 0 if it was |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 390 | able to empty out the buffers completely, 1 if more flushing is |
| 391 | required later, or -1 on a fatal write error. */ |
| 392 | buffer_status_t |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 393 | buffer_flush_available(struct buffer *b, int fd) |
| 394 | { |
| 395 | |
| 396 | /* These are just reasonable values to make sure a significant amount of |
| 397 | data is written. There's no need to go crazy and try to write it all |
| 398 | in one shot. */ |
| 399 | #ifdef IOV_MAX |
| 400 | #define MAX_CHUNKS ((IOV_MAX >= 16) ? 16 : IOV_MAX) |
| 401 | #else |
| 402 | #define MAX_CHUNKS 16 |
| 403 | #endif |
| 404 | #define MAX_FLUSH 131072 |
| 405 | |
| 406 | struct buffer_data *d; |
paul | 2265d20 | 2004-11-08 15:43:21 +0000 | [diff] [blame] | 407 | size_t written; |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 408 | struct iovec iov[MAX_CHUNKS]; |
paul | 2265d20 | 2004-11-08 15:43:21 +0000 | [diff] [blame] | 409 | size_t iovcnt = 0; |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 410 | size_t nbyte = 0; |
| 411 | |
| 412 | for (d = b->head; d && (iovcnt < MAX_CHUNKS) && (nbyte < MAX_FLUSH); |
| 413 | d = d->next, iovcnt++) |
| 414 | { |
| 415 | iov[iovcnt].iov_base = d->data+d->sp; |
| 416 | nbyte += (iov[iovcnt].iov_len = d->cp-d->sp); |
| 417 | } |
| 418 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 419 | if (!nbyte) |
| 420 | /* No data to flush: should we issue a warning message? */ |
| 421 | return BUFFER_EMPTY; |
| 422 | |
paul | 2265d20 | 2004-11-08 15:43:21 +0000 | [diff] [blame] | 423 | /* only place where written should be sign compared */ |
| 424 | if ((ssize_t)(written = writev(fd,iov,iovcnt)) < 0) |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 425 | { |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 426 | if (ERRNO_IO_RETRY(errno)) |
| 427 | /* Calling code should try again later. */ |
| 428 | return BUFFER_PENDING; |
| 429 | zlog_warn("%s: write error on fd %d: %s", |
| 430 | __func__, fd, safe_strerror(errno)); |
| 431 | return BUFFER_ERROR; |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* Free printed buffer data. */ |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 435 | while (written > 0) |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 436 | { |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 437 | struct buffer_data *d; |
| 438 | if (!(d = b->head)) |
| 439 | { |
| 440 | zlog_err("%s: corruption detected: buffer queue empty, " |
| 441 | "but written is %lu", __func__, (u_long)written); |
| 442 | break; |
| 443 | } |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 444 | if (written < d->cp-d->sp) |
| 445 | { |
| 446 | d->sp += written; |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 447 | return BUFFER_PENDING; |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | written -= (d->cp-d->sp); |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 451 | if (!(b->head = d->next)) |
| 452 | b->tail = NULL; |
| 453 | BUFFER_DATA_FREE(d); |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 454 | } |
| 455 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 456 | return b->head ? BUFFER_PENDING : BUFFER_EMPTY; |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 457 | |
| 458 | #undef MAX_CHUNKS |
| 459 | #undef MAX_FLUSH |
| 460 | } |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 461 | |
| 462 | buffer_status_t |
| 463 | buffer_write(struct buffer *b, int fd, const void *p, size_t size) |
| 464 | { |
| 465 | ssize_t nbytes; |
| 466 | |
| 467 | /* Attempt to drain the previously buffered data? */ |
| 468 | if (b->head && (buffer_flush_available(b, fd) == BUFFER_ERROR)) |
| 469 | return BUFFER_ERROR; |
| 470 | if (b->head) |
| 471 | /* Buffer still not empty. */ |
| 472 | nbytes = 0; |
| 473 | else if ((nbytes = write(fd, p, size)) < 0) |
| 474 | { |
| 475 | if (ERRNO_IO_RETRY(errno)) |
| 476 | nbytes = 0; |
| 477 | else |
| 478 | { |
| 479 | zlog_warn("%s: write error on fd %d: %s", |
| 480 | __func__, fd, safe_strerror(errno)); |
| 481 | return BUFFER_ERROR; |
| 482 | } |
| 483 | } |
| 484 | /* Add any remaining data to the buffer. */ |
| 485 | { |
| 486 | size_t written = nbytes; |
| 487 | if (written < size) |
| 488 | buffer_put(b, ((const char *)p)+written, size-written); |
| 489 | } |
| 490 | return b->head ? BUFFER_PENDING : BUFFER_EMPTY; |
| 491 | } |