ustream: add a pointer to the ustream struct useful for chaining ustreams
[project/libubox.git] / ustream-fd.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 <unistd.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include "ustream.h"
23
24 static void ustream_fd_set_uloop(struct ustream *s)
25 {
26         struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
27         struct ustream_buf *buf;
28         unsigned int flags = ULOOP_EDGE_TRIGGER;
29
30         if (!s->read_blocked && !s->eof)
31                 flags |= ULOOP_READ;
32
33         buf = s->w.head;
34         if (buf && s->w.data_bytes && !s->write_error)
35                 flags |= ULOOP_WRITE;
36
37         uloop_fd_add(&sf->fd, flags);
38
39         if (flags & ULOOP_READ)
40                 sf->fd.cb(&sf->fd, ULOOP_READ);
41 }
42
43 static void ustream_fd_read_pending(struct ustream_fd *sf, bool *update)
44 {
45         struct ustream *s = &sf->stream;
46         int buflen = 0;
47         ssize_t len;
48         char *buf;
49
50         do {
51                 buf = ustream_reserve(s, 1, &buflen);
52                 if (!buf)
53                         break;
54
55                 len = read(sf->fd.fd, buf, buflen);
56                 if (!len) {
57                         sf->fd.eof = true;
58                         return;
59                 }
60
61                 if (len < 0) {
62                         if (errno == EINTR)
63                                 continue;
64
65                         if (errno == EAGAIN)
66                                 return;
67                 }
68
69                 ustream_fill_read(s, len);
70         } while (1);
71 }
72
73 static int ustream_fd_write(struct ustream *s, const char *buf, int buflen, bool more)
74 {
75         struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
76         ssize_t len;
77
78         if (!buflen)
79                 return 0;
80
81 retry:
82         len = write(sf->fd.fd, buf, buflen);
83         if (!len)
84                 goto retry;
85
86         if (len < 0) {
87                 if (errno == EINTR)
88                         goto retry;
89
90                 if (errno == EAGAIN || errno == EWOULDBLOCK)
91                         return 0;
92         }
93
94         return len;
95 }
96
97 static void ustream_uloop_cb(struct uloop_fd *fd, unsigned int events)
98 {
99         struct ustream_fd *sf = container_of(fd, struct ustream_fd, fd);
100         struct ustream *s = &sf->stream;
101         bool update = false;
102
103         if (events & ULOOP_READ)
104                 ustream_fd_read_pending(sf, &update);
105
106         if (events & ULOOP_WRITE) {
107                 if (ustream_write_pending(s))
108                         ustream_fd_set_uloop(s);
109         }
110
111         if (!s->eof && fd->eof) {
112                 s->eof = true;
113                 ustream_fd_set_uloop(s);
114                 ustream_state_change(s);
115         }
116 }
117
118
119 static void ustream_fd_free(struct ustream *s)
120 {
121         struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
122
123         uloop_fd_delete(&sf->fd);
124 }
125
126 void ustream_fd_init(struct ustream_fd *sf, int fd)
127 {
128         struct ustream *s = &sf->stream;
129
130         ustream_init_defaults(s);
131
132         sf->fd.fd = fd;
133         sf->fd.cb = ustream_uloop_cb;
134         s->set_read_blocked = ustream_fd_set_uloop;
135         s->write = ustream_fd_write;
136         s->free = ustream_fd_free;
137         ustream_fd_set_uloop(s);
138 }