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.
17 * $Id: ev_streams.c,v 1.5 2005/04/27 04:56:36 sra Exp $
20 /* ev_streams.c - implement asynch stream file IO for the eventlib
21 * vix 04mar96 [initial]
24 #include "port_before.h"
26 #include "fd_setsize.h"
29 #include <sys/types.h>
34 #include "isc/eventlib.h"
36 #include <isc/assertions.h>
38 #include "eventlib_p.h"
40 #include "port_after.h"
43 static int copyvec(evStream
*str
, const struct iovec
*iov
, int iocnt
);
44 static void consume(evStream
*str
, size_t bytes
);
45 static void done(evContext opaqueCtx
, evStream
*str
);
46 static void writable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
);
47 static void readable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
);
51 evConsIovec(void *buf
, size_t cnt
) {
54 memset(&ret
, 0xf5, sizeof ret
);
62 evWrite(evContext opaqueCtx
, int fd
, const struct iovec
*iov
, int iocnt
,
63 evStreamFunc func
, void *uap
, evStreamID
*id
)
65 evContext_p
*ctx
= opaqueCtx
.opaque
;
74 if (evSelectFD(opaqueCtx
, fd
, EV_WRITE
, writable
, new, &new->file
) < 0)
76 if (copyvec(new, iov
, iocnt
) < 0)
80 if (ctx
->streams
!= NULL
)
81 ctx
->streams
->prev
= new;
83 new->next
= ctx
->streams
;
96 evRead(evContext opaqueCtx
, int fd
, const struct iovec
*iov
, int iocnt
,
97 evStreamFunc func
, void *uap
, evStreamID
*id
)
99 evContext_p
*ctx
= opaqueCtx
.opaque
;
108 if (evSelectFD(opaqueCtx
, fd
, EV_READ
, readable
, new, &new->file
) < 0)
110 if (copyvec(new, iov
, iocnt
) < 0)
112 new->prevDone
= NULL
;
113 new->nextDone
= NULL
;
114 if (ctx
->streams
!= NULL
)
115 ctx
->streams
->prev
= new;
117 new->next
= ctx
->streams
;
130 evTimeRW(evContext opaqueCtx
, evStreamID id
, evTimerID timer
) /*ARGSUSED*/ {
131 evStream
*str
= id
.opaque
;
136 str
->flags
|= EV_STR_TIMEROK
;
141 evUntimeRW(evContext opaqueCtx
, evStreamID id
) /*ARGSUSED*/ {
142 evStream
*str
= id
.opaque
;
146 str
->flags
&= ~EV_STR_TIMEROK
;
151 evCancelRW(evContext opaqueCtx
, evStreamID id
) {
152 evContext_p
*ctx
= opaqueCtx
.opaque
;
153 evStream
*old
= id
.opaque
;
156 * The streams list is doubly threaded. First, there's ctx->streams
157 * that's used by evDestroy() to find and cancel all streams. Second,
158 * there's ctx->strDone (head) and ctx->strLast (tail) which thread
159 * through the potentially smaller number of "IO completed" streams,
160 * used in evGetNext() to avoid scanning the entire list.
163 /* Unlink from ctx->streams. */
164 if (old
->prev
!= NULL
)
165 old
->prev
->next
= old
->next
;
167 ctx
->streams
= old
->next
;
168 if (old
->next
!= NULL
)
169 old
->next
->prev
= old
->prev
;
172 * If 'old' is on the ctx->strDone list, remove it. Update
173 * ctx->strLast if necessary.
175 if (old
->prevDone
== NULL
&& old
->nextDone
== NULL
) {
177 * Either 'old' is the only item on the done list, or it's
178 * not on the done list. If the former, then we unlink it
179 * from the list. If the latter, we leave the list alone.
181 if (ctx
->strDone
== old
) {
186 if (old
->prevDone
!= NULL
)
187 old
->prevDone
->nextDone
= old
->nextDone
;
189 ctx
->strDone
= old
->nextDone
;
190 if (old
->nextDone
!= NULL
)
191 old
->nextDone
->prevDone
= old
->prevDone
;
193 ctx
->strLast
= old
->prevDone
;
196 /* Deallocate the stream. */
197 if (old
->file
.opaque
)
198 evDeselectFD(opaqueCtx
, old
->file
);
199 memput(old
->iovOrig
, sizeof (struct iovec
) * old
->iovOrigCount
);
204 /* Copy a scatter/gather vector and initialize a stream handler's IO. */
206 copyvec(evStream
*str
, const struct iovec
*iov
, int iocnt
) {
209 str
->iovOrig
= (struct iovec
*)memget(sizeof(struct iovec
) * iocnt
);
210 if (str
->iovOrig
== NULL
) {
215 for (i
= 0; i
< iocnt
; i
++) {
216 str
->iovOrig
[i
] = iov
[i
];
217 str
->ioTotal
+= iov
[i
].iov_len
;
219 str
->iovOrigCount
= iocnt
;
220 str
->iovCur
= str
->iovOrig
;
221 str
->iovCurCount
= str
->iovOrigCount
;
226 /* Pull off or truncate lead iovec(s). */
228 consume(evStream
*str
, size_t bytes
) {
230 if (bytes
< (size_t)str
->iovCur
->iov_len
) {
231 str
->iovCur
->iov_len
-= bytes
;
232 str
->iovCur
->iov_base
= (void *)
233 ((u_char
*)str
->iovCur
->iov_base
+ bytes
);
234 str
->ioDone
+= bytes
;
237 bytes
-= str
->iovCur
->iov_len
;
238 str
->ioDone
+= str
->iovCur
->iov_len
;
245 /* Add a stream to Done list and deselect the FD. */
247 done(evContext opaqueCtx
, evStream
*str
) {
248 evContext_p
*ctx
= opaqueCtx
.opaque
;
250 if (ctx
->strLast
!= NULL
) {
251 str
->prevDone
= ctx
->strLast
;
252 ctx
->strLast
->nextDone
= str
;
255 INSIST(ctx
->strDone
== NULL
);
256 ctx
->strDone
= ctx
->strLast
= str
;
258 evDeselectFD(opaqueCtx
, str
->file
);
259 str
->file
.opaque
= NULL
;
260 /* evDrop() will call evCancelRW() on us. */
263 /* Dribble out some bytes on the stream. (Called by evDispatch().) */
265 writable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
271 bytes
= writev(fd
, str
->iovCur
, str
->iovCurCount
);
273 if ((str
->flags
& EV_STR_TIMEROK
) != 0)
274 evTouchIdleTimer(opaqueCtx
, str
->timer
);
277 if (bytes
< 0 && errno
!= EINTR
) {
279 str
->ioErrno
= errno
;
282 if (str
->ioDone
== -1 || str
->ioDone
== str
->ioTotal
)
283 done(opaqueCtx
, str
);
286 /* Scoop up some bytes from the stream. (Called by evDispatch().) */
288 readable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
294 bytes
= readv(fd
, str
->iovCur
, str
->iovCurCount
);
296 if ((str
->flags
& EV_STR_TIMEROK
) != 0)
297 evTouchIdleTimer(opaqueCtx
, str
->timer
);
303 if (errno
!= EINTR
) {
305 str
->ioErrno
= errno
;
309 if (str
->ioDone
<= 0 || str
->ioDone
== str
->ioTotal
)
310 done(opaqueCtx
, str
);