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