uloop: fix signal unblocking
[project/libubox.git] / ustream.c
1 /*
2 * ustream - library for stream buffer management
3 *
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24
25 #include "ustream.h"
26
27 static void ustream_init_buf(struct ustream_buf *buf, int len)
28 {
29 if (!len)
30 abort();
31
32 memset(buf, 0, sizeof(*buf));
33 buf->data = buf->tail = buf->head;
34 buf->end = buf->head + len;
35 *buf->head = 0;
36 }
37
38 static void ustream_add_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
39 {
40 l->buffers++;
41 if (!l->tail)
42 l->head = buf;
43 else
44 l->tail->next = buf;
45
46 buf->next = NULL;
47 l->tail = buf;
48 if (!l->data_tail)
49 l->data_tail = l->head;
50 }
51
52 static bool ustream_can_alloc(struct ustream_buf_list *l)
53 {
54 if (l->max_buffers <= 0)
55 return true;
56
57 return (l->buffers < l->max_buffers);
58 }
59
60 static int ustream_alloc_default(struct ustream *s, struct ustream_buf_list *l)
61 {
62 struct ustream_buf *buf;
63
64 if (!ustream_can_alloc(l))
65 return -1;
66
67 buf = malloc(sizeof(*buf) + l->buffer_len + s->string_data);
68 ustream_init_buf(buf, l->buffer_len);
69 ustream_add_buf(l, buf);
70
71 return 0;
72 }
73
74 static void ustream_free_buffers(struct ustream_buf_list *l)
75 {
76 struct ustream_buf *buf = l->head;
77
78 while (buf) {
79 struct ustream_buf *next = buf->next;
80
81 free(buf);
82 buf = next;
83 }
84 l->head = NULL;
85 l->tail = NULL;
86 l->data_tail = NULL;
87 }
88
89 void ustream_free(struct ustream *s)
90 {
91 if (s->free)
92 s->free(s);
93
94 uloop_timeout_cancel(&s->state_change);
95 ustream_free_buffers(&s->r);
96 ustream_free_buffers(&s->w);
97 }
98
99 static void ustream_state_change_cb(struct uloop_timeout *t)
100 {
101 struct ustream *s = container_of(t, struct ustream, state_change);
102
103 if (s->write_error)
104 ustream_free_buffers(&s->w);
105 if (s->notify_state)
106 s->notify_state(s);
107 }
108
109 void ustream_init_defaults(struct ustream *s)
110 {
111 #define DEFAULT_SET(_f, _default) \
112 do { \
113 if (!_f) \
114 _f = _default; \
115 } while(0)
116
117 DEFAULT_SET(s->r.alloc, ustream_alloc_default);
118 DEFAULT_SET(s->w.alloc, ustream_alloc_default);
119
120 DEFAULT_SET(s->r.min_buffers, 1);
121 DEFAULT_SET(s->r.max_buffers, 1);
122 DEFAULT_SET(s->r.buffer_len, 4096);
123
124 DEFAULT_SET(s->w.min_buffers, 2);
125 DEFAULT_SET(s->w.max_buffers, -1);
126 DEFAULT_SET(s->w.buffer_len, 256);
127
128 #undef DEFAULT_SET
129
130 s->state_change.cb = ustream_state_change_cb;
131 s->write_error = false;
132 s->eof = false;
133 s->eof_write_done = false;
134 s->read_blocked = 0;
135
136 s->r.buffers = 0;
137 s->r.data_bytes = 0;
138
139 s->w.buffers = 0;
140 s->w.data_bytes = 0;
141 }
142
143 static bool ustream_should_move(struct ustream_buf_list *l, struct ustream_buf *buf, int len)
144 {
145 int maxlen;
146 int offset;
147
148 /* nothing to squeeze */
149 if (buf->data == buf->head)
150 return false;
151
152 maxlen = buf->end - buf->head;
153 offset = buf->data - buf->head;
154
155 /* less than half is available */
156 if (offset > maxlen / 2)
157 return true;
158
159 /* less than 32 bytes data but takes more than 1/4 space */
160 if (buf->tail - buf->data < 32 && offset > maxlen / 4)
161 return true;
162
163 /* more buf is already in list or can be allocated */
164 if (buf != l->tail || ustream_can_alloc(l))
165 return false;
166
167 /* no need to move if len is available at the tail */
168 return (buf->end - buf->tail < len);
169 }
170
171 static void ustream_free_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
172 {
173 if (buf == l->head)
174 l->head = buf->next;
175
176 if (buf == l->data_tail)
177 l->data_tail = buf->next;
178
179 if (buf == l->tail)
180 l->tail = NULL;
181
182 if (--l->buffers >= l->min_buffers) {
183 free(buf);
184 return;
185 }
186
187 /* recycle */
188 ustream_init_buf(buf, buf->end - buf->head);
189 ustream_add_buf(l, buf);
190 }
191
192 static void __ustream_set_read_blocked(struct ustream *s, unsigned char val)
193 {
194 bool changed = !!s->read_blocked != !!val;
195
196 s->read_blocked = val;
197 if (changed)
198 s->set_read_blocked(s);
199 }
200
201 void ustream_set_read_blocked(struct ustream *s, bool set)
202 {
203 unsigned char val = s->read_blocked & ~READ_BLOCKED_USER;
204
205 if (set)
206 val |= READ_BLOCKED_USER;
207
208 __ustream_set_read_blocked(s, val);
209 }
210
211 void ustream_consume(struct ustream *s, int len)
212 {
213 struct ustream_buf *buf = s->r.head;
214
215 if (!len)
216 return;
217
218 s->r.data_bytes -= len;
219 if (s->r.data_bytes < 0)
220 abort();
221
222 do {
223 struct ustream_buf *next = buf->next;
224 int buf_len = buf->tail - buf->data;
225
226 if (len < buf_len) {
227 buf->data += len;
228 break;
229 }
230
231 len -= buf_len;
232 ustream_free_buf(&s->r, buf);
233 buf = next;
234 } while(len);
235
236 __ustream_set_read_blocked(s, s->read_blocked & ~READ_BLOCKED_FULL);
237 }
238
239 static void ustream_fixup_string(struct ustream *s, struct ustream_buf *buf)
240 {
241 if (!s->string_data)
242 return;
243
244 *buf->tail = 0;
245 }
246
247 static bool ustream_prepare_buf(struct ustream *s, struct ustream_buf_list *l, int len)
248 {
249 struct ustream_buf *buf;
250
251 buf = l->data_tail;
252 if (buf) {
253 if (ustream_should_move(l, buf, len)) {
254 int len = buf->tail - buf->data;
255
256 memmove(buf->head, buf->data, len);
257 buf->data = buf->head;
258 buf->tail = buf->data + len;
259
260 if (l == &s->r)
261 ustream_fixup_string(s, buf);
262 }
263 /* some chunks available at the tail */
264 if (buf->tail != buf->end)
265 return true;
266 /* next buf available */
267 if (buf->next) {
268 l->data_tail = buf->next;
269 return true;
270 }
271 }
272
273 if (!ustream_can_alloc(l))
274 return false;
275
276 if (l->alloc(s, l) < 0)
277 return false;
278
279 l->data_tail = l->tail;
280 return true;
281 }
282
283 char *ustream_reserve(struct ustream *s, int len, int *maxlen)
284 {
285 struct ustream_buf *buf;
286
287 if (!ustream_prepare_buf(s, &s->r, len)) {
288 __ustream_set_read_blocked(s, s->read_blocked | READ_BLOCKED_FULL);
289 *maxlen = 0;
290 return NULL;
291 }
292
293 buf = s->r.data_tail;
294 *maxlen = buf->end - buf->tail;
295 return buf->tail;
296 }
297
298 void ustream_fill_read(struct ustream *s, int len)
299 {
300 struct ustream_buf *buf = s->r.data_tail;
301 int n = len;
302 int maxlen;
303
304 s->r.data_bytes += len;
305 do {
306 if (!buf)
307 abort();
308
309 maxlen = buf->end - buf->tail;
310 if (len < maxlen)
311 maxlen = len;
312
313 len -= maxlen;
314 buf->tail += maxlen;
315 ustream_fixup_string(s, buf);
316
317 s->r.data_tail = buf;
318 buf = buf->next;
319 } while (len);
320
321 if (s->notify_read)
322 s->notify_read(s, n);
323 }
324
325 char *ustream_get_read_buf(struct ustream *s, int *buflen)
326 {
327 char *data = NULL;
328 int len = 0;
329
330 if (s->r.head) {
331 len = s->r.head->tail - s->r.head->data;
332 if (len > 0)
333 data = s->r.head->data;
334 }
335
336 if (buflen)
337 *buflen = len;
338
339 return data;
340 }
341
342 int ustream_read(struct ustream *s, char *buf, int buflen)
343 {
344 char *chunk;
345 int chunk_len;
346 int len = 0;
347
348 do {
349 chunk = ustream_get_read_buf(s, &chunk_len);
350 if (!chunk)
351 break;
352 if (chunk_len > buflen - len)
353 chunk_len = buflen - len;
354 memcpy(buf + len, chunk, chunk_len);
355 ustream_consume(s, chunk_len);
356 len += chunk_len;
357 } while (len < buflen);
358
359 return len;
360 }
361
362 static void ustream_write_error(struct ustream *s)
363 {
364 if (!s->write_error)
365 ustream_state_change(s);
366 s->write_error = true;
367 }
368
369 bool ustream_write_pending(struct ustream *s)
370 {
371 struct ustream_buf *buf = s->w.head;
372 int wr = 0, len;
373
374 if (s->write_error)
375 return false;
376
377 while (buf && s->w.data_bytes) {
378 struct ustream_buf *next = buf->next;
379 int maxlen = buf->tail - buf->data;
380
381 len = s->write(s, buf->data, maxlen, !!buf->next);
382 if (len < 0) {
383 ustream_write_error(s);
384 break;
385 }
386
387 if (len == 0)
388 break;
389
390 wr += len;
391 s->w.data_bytes -= len;
392 if (len < maxlen) {
393 buf->data += len;
394 break;
395 }
396
397 ustream_free_buf(&s->w, buf);
398 buf = next;
399 }
400
401 if (s->notify_write)
402 s->notify_write(s, wr);
403
404 if (s->eof && wr && !s->w.data_bytes)
405 ustream_state_change(s);
406
407 return !s->w.data_bytes;
408 }
409
410 static int ustream_write_buffered(struct ustream *s, const char *data, int len, int wr)
411 {
412 struct ustream_buf_list *l = &s->w;
413 struct ustream_buf *buf;
414 int maxlen;
415
416 while (len) {
417 if (!ustream_prepare_buf(s, &s->w, len))
418 break;
419
420 buf = l->data_tail;
421
422 maxlen = buf->end - buf->tail;
423 if (maxlen > len)
424 maxlen = len;
425
426 memcpy(buf->tail, data, maxlen);
427 buf->tail += maxlen;
428 data += maxlen;
429 len -= maxlen;
430 wr += maxlen;
431 l->data_bytes += maxlen;
432 }
433
434 return wr;
435 }
436
437 int ustream_write(struct ustream *s, const char *data, int len, bool more)
438 {
439 struct ustream_buf_list *l = &s->w;
440 int wr = 0;
441
442 if (s->write_error)
443 return 0;
444
445 if (!l->data_bytes) {
446 wr = s->write(s, data, len, more);
447 if (wr == len)
448 return wr;
449
450 if (wr < 0) {
451 ustream_write_error(s);
452 return wr;
453 }
454
455 data += wr;
456 len -= wr;
457 }
458
459 return ustream_write_buffered(s, data, len, wr);
460 }
461
462 #define MAX_STACK_BUFLEN 256
463
464 int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
465 {
466 struct ustream_buf_list *l = &s->w;
467 char *buf;
468 va_list arg2;
469 int wr, maxlen, buflen;
470
471 if (s->write_error)
472 return 0;
473
474 if (!l->data_bytes) {
475 buf = alloca(MAX_STACK_BUFLEN);
476 va_copy(arg2, arg);
477 maxlen = vsnprintf(buf, MAX_STACK_BUFLEN, format, arg2);
478 va_end(arg2);
479 if (maxlen < MAX_STACK_BUFLEN) {
480 wr = s->write(s, buf, maxlen, false);
481 if (wr < 0) {
482 ustream_write_error(s);
483 return wr;
484 }
485 if (wr == maxlen)
486 return wr;
487
488 buf += wr;
489 maxlen -= wr;
490 return ustream_write_buffered(s, buf, maxlen, wr);
491 } else {
492 buf = malloc(maxlen + 1);
493 wr = vsnprintf(buf, maxlen + 1, format, arg);
494 wr = ustream_write(s, buf, wr, false);
495 free(buf);
496 return wr;
497 }
498 }
499
500 if (!ustream_prepare_buf(s, l, 1))
501 return 0;
502
503 buf = l->data_tail->tail;
504 buflen = l->data_tail->end - buf;
505
506 va_copy(arg2, arg);
507 maxlen = vsnprintf(buf, buflen, format, arg2);
508 va_end(arg2);
509
510 wr = maxlen;
511 if (wr >= buflen)
512 wr = buflen - 1;
513
514 l->data_tail->tail += wr;
515 l->data_bytes += wr;
516 if (maxlen < buflen)
517 return wr;
518
519 buf = malloc(maxlen + 1);
520 maxlen = vsnprintf(buf, maxlen + 1, format, arg);
521 wr = ustream_write_buffered(s, buf + wr, maxlen - wr, wr);
522 free(buf);
523
524 return wr;
525 }
526
527 int ustream_printf(struct ustream *s, const char *format, ...)
528 {
529 va_list arg;
530 int ret;
531
532 if (s->write_error)
533 return 0;
534
535 va_start(arg, format);
536 ret = ustream_vprintf(s, format, arg);
537 va_end(arg);
538
539 return ret;
540 }