8849 libresolv2: remove rcsid
[unleashed.git] / usr / src / lib / libresolv2 / common / isc / ev_streams.c
blob824b4e25c19689bcfc843839f3ca92410075ee50
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-1999 by Internet Software Consortium
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 /* ev_streams.c - implement asynch stream file IO for the eventlib
19 * vix 04mar96 [initial]
22 #include "port_before.h"
23 #include "fd_setsize.h"
25 #include <sys/types.h>
26 #include <sys/uio.h>
28 #include <errno.h>
30 #include <isc/eventlib.h>
31 #include <isc/assertions.h>
32 #include "eventlib_p.h"
34 #include "port_after.h"
36 static int copyvec(evStream *str, const struct iovec *iov, int iocnt);
37 static void consume(evStream *str, size_t bytes);
38 static void done(evContext opaqueCtx, evStream *str);
39 static void writable(evContext opaqueCtx, void *uap, int fd, int evmask);
40 static void readable(evContext opaqueCtx, void *uap, int fd, int evmask);
42 struct iovec
43 evConsIovec(void *buf, size_t cnt) {
44 struct iovec ret;
46 memset(&ret, 0xf5, sizeof ret);
47 ret.iov_base = buf;
48 ret.iov_len = cnt;
49 return (ret);
52 int
53 evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
54 evStreamFunc func, void *uap, evStreamID *id)
56 evContext_p *ctx = opaqueCtx.opaque;
57 evStream *new;
58 int save;
60 OKNEW(new);
61 new->func = func;
62 new->uap = uap;
63 new->fd = fd;
64 new->flags = 0;
65 if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0)
66 goto free;
67 if (copyvec(new, iov, iocnt) < 0)
68 goto free;
69 new->prevDone = NULL;
70 new->nextDone = NULL;
71 if (ctx->streams != NULL)
72 ctx->streams->prev = new;
73 new->prev = NULL;
74 new->next = ctx->streams;
75 ctx->streams = new;
76 if (id != NULL)
77 id->opaque = new;
78 return (0);
79 free:
80 save = errno;
81 FREE(new);
82 errno = save;
83 return (-1);
86 int
87 evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
88 evStreamFunc func, void *uap, evStreamID *id)
90 evContext_p *ctx = opaqueCtx.opaque;
91 evStream *new;
92 int save;
94 OKNEW(new);
95 new->func = func;
96 new->uap = uap;
97 new->fd = fd;
98 new->flags = 0;
99 if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0)
100 goto free;
101 if (copyvec(new, iov, iocnt) < 0)
102 goto free;
103 new->prevDone = NULL;
104 new->nextDone = NULL;
105 if (ctx->streams != NULL)
106 ctx->streams->prev = new;
107 new->prev = NULL;
108 new->next = ctx->streams;
109 ctx->streams = new;
110 if (id)
111 id->opaque = new;
112 return (0);
113 free:
114 save = errno;
115 FREE(new);
116 errno = save;
117 return (-1);
121 evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ {
122 evStream *str = id.opaque;
124 UNUSED(opaqueCtx);
126 str->timer = timer;
127 str->flags |= EV_STR_TIMEROK;
128 return (0);
132 evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ {
133 evStream *str = id.opaque;
135 UNUSED(opaqueCtx);
137 str->flags &= ~EV_STR_TIMEROK;
138 return (0);
142 evCancelRW(evContext opaqueCtx, evStreamID id) {
143 evContext_p *ctx = opaqueCtx.opaque;
144 evStream *old = id.opaque;
147 * The streams list is doubly threaded. First, there's ctx->streams
148 * that's used by evDestroy() to find and cancel all streams. Second,
149 * there's ctx->strDone (head) and ctx->strLast (tail) which thread
150 * through the potentially smaller number of "IO completed" streams,
151 * used in evGetNext() to avoid scanning the entire list.
154 /* Unlink from ctx->streams. */
155 if (old->prev != NULL)
156 old->prev->next = old->next;
157 else
158 ctx->streams = old->next;
159 if (old->next != NULL)
160 old->next->prev = old->prev;
163 * If 'old' is on the ctx->strDone list, remove it. Update
164 * ctx->strLast if necessary.
166 if (old->prevDone == NULL && old->nextDone == NULL) {
168 * Either 'old' is the only item on the done list, or it's
169 * not on the done list. If the former, then we unlink it
170 * from the list. If the latter, we leave the list alone.
172 if (ctx->strDone == old) {
173 ctx->strDone = NULL;
174 ctx->strLast = NULL;
176 } else {
177 if (old->prevDone != NULL)
178 old->prevDone->nextDone = old->nextDone;
179 else
180 ctx->strDone = old->nextDone;
181 if (old->nextDone != NULL)
182 old->nextDone->prevDone = old->prevDone;
183 else
184 ctx->strLast = old->prevDone;
187 /* Deallocate the stream. */
188 if (old->file.opaque)
189 evDeselectFD(opaqueCtx, old->file);
190 memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount);
191 FREE(old);
192 return (0);
195 /* Copy a scatter/gather vector and initialize a stream handler's IO. */
196 static int
197 copyvec(evStream *str, const struct iovec *iov, int iocnt) {
198 int i;
200 str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt);
201 if (str->iovOrig == NULL) {
202 errno = ENOMEM;
203 return (-1);
205 str->ioTotal = 0;
206 for (i = 0; i < iocnt; i++) {
207 str->iovOrig[i] = iov[i];
208 str->ioTotal += iov[i].iov_len;
210 str->iovOrigCount = iocnt;
211 str->iovCur = str->iovOrig;
212 str->iovCurCount = str->iovOrigCount;
213 str->ioDone = 0;
214 return (0);
217 /* Pull off or truncate lead iovec(s). */
218 static void
219 consume(evStream *str, size_t bytes) {
220 while (bytes > 0U) {
221 if (bytes < (size_t)str->iovCur->iov_len) {
222 str->iovCur->iov_len -= bytes;
223 str->iovCur->iov_base = (void *)
224 ((u_char *)str->iovCur->iov_base + bytes);
225 str->ioDone += bytes;
226 bytes = 0;
227 } else {
228 bytes -= str->iovCur->iov_len;
229 str->ioDone += str->iovCur->iov_len;
230 str->iovCur++;
231 str->iovCurCount--;
236 /* Add a stream to Done list and deselect the FD. */
237 static void
238 done(evContext opaqueCtx, evStream *str) {
239 evContext_p *ctx = opaqueCtx.opaque;
241 if (ctx->strLast != NULL) {
242 str->prevDone = ctx->strLast;
243 ctx->strLast->nextDone = str;
244 ctx->strLast = str;
245 } else {
246 INSIST(ctx->strDone == NULL);
247 ctx->strDone = ctx->strLast = str;
249 evDeselectFD(opaqueCtx, str->file);
250 str->file.opaque = NULL;
251 /* evDrop() will call evCancelRW() on us. */
254 /* Dribble out some bytes on the stream. (Called by evDispatch().) */
255 static void
256 writable(evContext opaqueCtx, void *uap, int fd, int evmask) {
257 evStream *str = uap;
258 int bytes;
260 UNUSED(evmask);
262 bytes = writev(fd, str->iovCur, str->iovCurCount);
263 if (bytes > 0) {
264 if ((str->flags & EV_STR_TIMEROK) != 0)
265 evTouchIdleTimer(opaqueCtx, str->timer);
266 consume(str, bytes);
267 } else {
268 if (bytes < 0 && errno != EINTR) {
269 str->ioDone = -1;
270 str->ioErrno = errno;
273 if (str->ioDone == -1 || str->ioDone == str->ioTotal)
274 done(opaqueCtx, str);
277 /* Scoop up some bytes from the stream. (Called by evDispatch().) */
278 static void
279 readable(evContext opaqueCtx, void *uap, int fd, int evmask) {
280 evStream *str = uap;
281 int bytes;
283 UNUSED(evmask);
285 bytes = readv(fd, str->iovCur, str->iovCurCount);
286 if (bytes > 0) {
287 if ((str->flags & EV_STR_TIMEROK) != 0)
288 evTouchIdleTimer(opaqueCtx, str->timer);
289 consume(str, bytes);
290 } else {
291 if (bytes == 0)
292 str->ioDone = 0;
293 else {
294 if (errno != EINTR) {
295 str->ioDone = -1;
296 str->ioErrno = errno;
300 if (str->ioDone <= 0 || str->ioDone == str->ioTotal)
301 done(opaqueCtx, str);
304 /*! \file */