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>
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
);
43 evConsIovec(void *buf
, size_t cnt
) {
46 memset(&ret
, 0xf5, sizeof ret
);
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
;
65 if (evSelectFD(opaqueCtx
, fd
, EV_WRITE
, writable
, new, &new->file
) < 0)
67 if (copyvec(new, iov
, iocnt
) < 0)
71 if (ctx
->streams
!= NULL
)
72 ctx
->streams
->prev
= new;
74 new->next
= ctx
->streams
;
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
;
99 if (evSelectFD(opaqueCtx
, fd
, EV_READ
, readable
, new, &new->file
) < 0)
101 if (copyvec(new, iov
, iocnt
) < 0)
103 new->prevDone
= NULL
;
104 new->nextDone
= NULL
;
105 if (ctx
->streams
!= NULL
)
106 ctx
->streams
->prev
= new;
108 new->next
= ctx
->streams
;
121 evTimeRW(evContext opaqueCtx
, evStreamID id
, evTimerID timer
) /*ARGSUSED*/ {
122 evStream
*str
= id
.opaque
;
127 str
->flags
|= EV_STR_TIMEROK
;
132 evUntimeRW(evContext opaqueCtx
, evStreamID id
) /*ARGSUSED*/ {
133 evStream
*str
= id
.opaque
;
137 str
->flags
&= ~EV_STR_TIMEROK
;
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
;
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
) {
177 if (old
->prevDone
!= NULL
)
178 old
->prevDone
->nextDone
= old
->nextDone
;
180 ctx
->strDone
= old
->nextDone
;
181 if (old
->nextDone
!= NULL
)
182 old
->nextDone
->prevDone
= old
->prevDone
;
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
);
195 /* Copy a scatter/gather vector and initialize a stream handler's IO. */
197 copyvec(evStream
*str
, const struct iovec
*iov
, int iocnt
) {
200 str
->iovOrig
= (struct iovec
*)memget(sizeof(struct iovec
) * iocnt
);
201 if (str
->iovOrig
== NULL
) {
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
;
217 /* Pull off or truncate lead iovec(s). */
219 consume(evStream
*str
, size_t bytes
) {
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
;
228 bytes
-= str
->iovCur
->iov_len
;
229 str
->ioDone
+= str
->iovCur
->iov_len
;
236 /* Add a stream to Done list and deselect the FD. */
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
;
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().) */
256 writable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
262 bytes
= writev(fd
, str
->iovCur
, str
->iovCurCount
);
264 if ((str
->flags
& EV_STR_TIMEROK
) != 0)
265 evTouchIdleTimer(opaqueCtx
, str
->timer
);
268 if (bytes
< 0 && errno
!= EINTR
) {
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().) */
279 readable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
285 bytes
= readv(fd
, str
->iovCur
, str
->iovCurCount
);
287 if ((str
->flags
& EV_STR_TIMEROK
) != 0)
288 evTouchIdleTimer(opaqueCtx
, str
->timer
);
294 if (errno
!= EINTR
) {
296 str
->ioErrno
= errno
;
300 if (str
->ioDone
<= 0 || str
->ioDone
== str
->ioTotal
)
301 done(opaqueCtx
, str
);