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