93fd501903701e50f82edafa111c730e70001dd9
[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, bool write)
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 (write || (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_set_read_blocked(struct ustream *s)
44 {
45         ustream_fd_set_uloop(s, false);
46 }
47
48 static void ustream_fd_read_pending(struct ustream_fd *sf, bool *more)
49 {
50         struct ustream *s = &sf->stream;
51         int buflen = 0;
52         ssize_t len;
53         char *buf;
54
55         do {
56                 buf = ustream_reserve(s, 1, &buflen);
57                 if (!buf)
58                         break;
59
60                 len = read(sf->fd.fd, buf, buflen);
61                 if (!len) {
62                         sf->fd.eof = true;
63                         return;
64                 }
65
66                 if (len < 0) {
67                         if (errno == EINTR)
68                                 continue;
69
70                         if (errno == EAGAIN)
71                                 return;
72                 }
73
74                 ustream_fill_read(s, len);
75                 *more = true;
76         } while (1);
77 }
78
79 static int ustream_fd_write(struct ustream *s, const char *buf, int buflen, bool more)
80 {
81         struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
82         ssize_t len;
83
84         if (!buflen)
85                 return 0;
86
87 retry:
88         len = write(sf->fd.fd, buf, buflen);
89         if (!len)
90                 goto retry;
91
92         if (len < 0) {
93                 if (errno == EINTR)
94                         goto retry;
95
96                 if (errno == EAGAIN || errno == EWOULDBLOCK)
97                         len = 0;
98         }
99
100         if (len >= 0 && len < buflen)
101                 ustream_fd_set_uloop(s, true);
102
103         return len;
104 }
105
106 static bool __ustream_fd_poll(struct ustream_fd *sf, unsigned int events)
107 {
108         struct ustream *s = &sf->stream;
109         struct uloop_fd *fd = &sf->fd;
110         bool more = false;
111
112         if (events & ULOOP_READ)
113                 ustream_fd_read_pending(sf, &more);
114
115         if (events & ULOOP_WRITE) {
116                 if (!ustream_write_pending(s))
117                         ustream_fd_set_uloop(s, false);
118         }
119
120         if (!s->eof && fd->eof) {
121                 s->eof = true;
122                 ustream_fd_set_uloop(s, false);
123                 ustream_state_change(s);
124         }
125
126         return more;
127 }
128
129 static bool ustream_fd_poll(struct ustream *s)
130 {
131         struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
132
133         return __ustream_fd_poll(sf, ULOOP_READ | ULOOP_WRITE);
134 }
135
136 static void ustream_uloop_cb(struct uloop_fd *fd, unsigned int events)
137 {
138         struct ustream_fd *sf = container_of(fd, struct ustream_fd, fd);
139
140         __ustream_fd_poll(sf, events);
141 }
142
143 static void ustream_fd_free(struct ustream *s)
144 {
145         struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
146
147         uloop_fd_delete(&sf->fd);
148 }
149
150 void ustream_fd_init(struct ustream_fd *sf, int fd)
151 {
152         struct ustream *s = &sf->stream;
153
154         ustream_init_defaults(s);
155
156         sf->fd.fd = fd;
157         sf->fd.cb = ustream_uloop_cb;
158         s->set_read_blocked = ustream_fd_set_read_blocked;
159         s->write = ustream_fd_write;
160         s->free = ustream_fd_free;
161         s->poll = ustream_fd_poll;
162         ustream_fd_set_uloop(s, false);
163 }