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 #if !defined(LINT) && !defined(CODECENTER)
23 static const char rcsid
[] = "$Id: ev_streams.c,v 1.5 2005/04/27 04:56:36 sra Exp $";
26 #include "port_before.h"
28 #include "fd_setsize.h"
31 #include <sys/types.h>
36 #include "isc/eventlib.h"
38 #include <isc/assertions.h>
40 #include "eventlib_p.h"
42 #include "port_after.h"
45 static int copyvec(evStream
*str
, const struct iovec
*iov
, int iocnt
);
46 static void consume(evStream
*str
, size_t bytes
);
47 static void done(evContext opaqueCtx
, evStream
*str
);
48 static void writable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
);
49 static void readable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
);
53 evConsIovec(void *buf
, size_t cnt
) {
56 memset(&ret
, 0xf5, sizeof ret
);
64 evWrite(evContext opaqueCtx
, int fd
, const struct iovec
*iov
, int iocnt
,
65 evStreamFunc func
, void *uap
, evStreamID
*id
)
67 evContext_p
*ctx
= opaqueCtx
.opaque
;
76 if (evSelectFD(opaqueCtx
, fd
, EV_WRITE
, writable
, new, &new->file
) < 0)
78 if (copyvec(new, iov
, iocnt
) < 0)
82 if (ctx
->streams
!= NULL
)
83 ctx
->streams
->prev
= new;
85 new->next
= ctx
->streams
;
98 evRead(evContext opaqueCtx
, int fd
, const struct iovec
*iov
, int iocnt
,
99 evStreamFunc func
, void *uap
, evStreamID
*id
)
101 evContext_p
*ctx
= opaqueCtx
.opaque
;
110 if (evSelectFD(opaqueCtx
, fd
, EV_READ
, readable
, new, &new->file
) < 0)
112 if (copyvec(new, iov
, iocnt
) < 0)
114 new->prevDone
= NULL
;
115 new->nextDone
= NULL
;
116 if (ctx
->streams
!= NULL
)
117 ctx
->streams
->prev
= new;
119 new->next
= ctx
->streams
;
132 evTimeRW(evContext opaqueCtx
, evStreamID id
, evTimerID timer
) /*ARGSUSED*/ {
133 evStream
*str
= id
.opaque
;
138 str
->flags
|= EV_STR_TIMEROK
;
143 evUntimeRW(evContext opaqueCtx
, evStreamID id
) /*ARGSUSED*/ {
144 evStream
*str
= id
.opaque
;
148 str
->flags
&= ~EV_STR_TIMEROK
;
153 evCancelRW(evContext opaqueCtx
, evStreamID id
) {
154 evContext_p
*ctx
= opaqueCtx
.opaque
;
155 evStream
*old
= id
.opaque
;
158 * The streams list is doubly threaded. First, there's ctx->streams
159 * that's used by evDestroy() to find and cancel all streams. Second,
160 * there's ctx->strDone (head) and ctx->strLast (tail) which thread
161 * through the potentially smaller number of "IO completed" streams,
162 * used in evGetNext() to avoid scanning the entire list.
165 /* Unlink from ctx->streams. */
166 if (old
->prev
!= NULL
)
167 old
->prev
->next
= old
->next
;
169 ctx
->streams
= old
->next
;
170 if (old
->next
!= NULL
)
171 old
->next
->prev
= old
->prev
;
174 * If 'old' is on the ctx->strDone list, remove it. Update
175 * ctx->strLast if necessary.
177 if (old
->prevDone
== NULL
&& old
->nextDone
== NULL
) {
179 * Either 'old' is the only item on the done list, or it's
180 * not on the done list. If the former, then we unlink it
181 * from the list. If the latter, we leave the list alone.
183 if (ctx
->strDone
== old
) {
188 if (old
->prevDone
!= NULL
)
189 old
->prevDone
->nextDone
= old
->nextDone
;
191 ctx
->strDone
= old
->nextDone
;
192 if (old
->nextDone
!= NULL
)
193 old
->nextDone
->prevDone
= old
->prevDone
;
195 ctx
->strLast
= old
->prevDone
;
198 /* Deallocate the stream. */
199 if (old
->file
.opaque
)
200 evDeselectFD(opaqueCtx
, old
->file
);
201 memput(old
->iovOrig
, sizeof (struct iovec
) * old
->iovOrigCount
);
206 /* Copy a scatter/gather vector and initialize a stream handler's IO. */
208 copyvec(evStream
*str
, const struct iovec
*iov
, int iocnt
) {
211 str
->iovOrig
= (struct iovec
*)memget(sizeof(struct iovec
) * iocnt
);
212 if (str
->iovOrig
== NULL
) {
217 for (i
= 0; i
< iocnt
; i
++) {
218 str
->iovOrig
[i
] = iov
[i
];
219 str
->ioTotal
+= iov
[i
].iov_len
;
221 str
->iovOrigCount
= iocnt
;
222 str
->iovCur
= str
->iovOrig
;
223 str
->iovCurCount
= str
->iovOrigCount
;
228 /* Pull off or truncate lead iovec(s). */
230 consume(evStream
*str
, size_t bytes
) {
232 if (bytes
< (size_t)str
->iovCur
->iov_len
) {
233 str
->iovCur
->iov_len
-= bytes
;
234 str
->iovCur
->iov_base
= (void *)
235 ((u_char
*)str
->iovCur
->iov_base
+ bytes
);
236 str
->ioDone
+= bytes
;
239 bytes
-= str
->iovCur
->iov_len
;
240 str
->ioDone
+= str
->iovCur
->iov_len
;
247 /* Add a stream to Done list and deselect the FD. */
249 done(evContext opaqueCtx
, evStream
*str
) {
250 evContext_p
*ctx
= opaqueCtx
.opaque
;
252 if (ctx
->strLast
!= NULL
) {
253 str
->prevDone
= ctx
->strLast
;
254 ctx
->strLast
->nextDone
= str
;
257 INSIST(ctx
->strDone
== NULL
);
258 ctx
->strDone
= ctx
->strLast
= str
;
260 evDeselectFD(opaqueCtx
, str
->file
);
261 str
->file
.opaque
= NULL
;
262 /* evDrop() will call evCancelRW() on us. */
265 /* Dribble out some bytes on the stream. (Called by evDispatch().) */
267 writable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
273 bytes
= writev(fd
, str
->iovCur
, str
->iovCurCount
);
275 if ((str
->flags
& EV_STR_TIMEROK
) != 0)
276 evTouchIdleTimer(opaqueCtx
, str
->timer
);
279 if (bytes
< 0 && errno
!= EINTR
) {
281 str
->ioErrno
= errno
;
284 if (str
->ioDone
== -1 || str
->ioDone
== str
->ioTotal
)
285 done(opaqueCtx
, str
);
288 /* Scoop up some bytes from the stream. (Called by evDispatch().) */
290 readable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
296 bytes
= readv(fd
, str
->iovCur
, str
->iovCurCount
);
298 if ((str
->flags
& EV_STR_TIMEROK
) != 0)
299 evTouchIdleTimer(opaqueCtx
, str
->timer
);
305 if (errno
!= EINTR
) {
307 str
->ioErrno
= errno
;
311 if (str
->ioDone
<= 0 || str
->ioDone
== str
->ioTotal
)
312 done(opaqueCtx
, str
);