4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996-1999 by Internet Software Consortium
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* ev_streams.c - implement asynch stream file IO for the eventlib
21 * vix 04mar96 [initial]
24 #if !defined(LINT) && !defined(CODECENTER)
25 static const char rcsid
[] = "Id: ev_streams.c,v 1.5 2005/04/27 04:56:36 sra Exp";
28 #include "port_before.h"
29 #include "fd_setsize.h"
31 #include <sys/types.h>
36 #include <isc/eventlib.h>
37 #include <isc/assertions.h>
38 #include "eventlib_p.h"
40 #include "port_after.h"
42 static int copyvec(evStream
*str
, const struct iovec
*iov
, int iocnt
);
43 static void consume(evStream
*str
, size_t bytes
);
44 static void done(evContext opaqueCtx
, evStream
*str
);
45 static void writable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
);
46 static void readable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
);
49 evConsIovec(void *buf
, size_t cnt
) {
52 memset(&ret
, 0xf5, sizeof ret
);
59 evWrite(evContext opaqueCtx
, int fd
, const struct iovec
*iov
, int iocnt
,
60 evStreamFunc func
, void *uap
, evStreamID
*id
)
62 evContext_p
*ctx
= opaqueCtx
.opaque
;
71 if (evSelectFD(opaqueCtx
, fd
, EV_WRITE
, writable
, new, &new->file
) < 0)
73 if (copyvec(new, iov
, iocnt
) < 0)
77 if (ctx
->streams
!= NULL
)
78 ctx
->streams
->prev
= new;
80 new->next
= ctx
->streams
;
93 evRead(evContext opaqueCtx
, int fd
, const struct iovec
*iov
, int iocnt
,
94 evStreamFunc func
, void *uap
, evStreamID
*id
)
96 evContext_p
*ctx
= opaqueCtx
.opaque
;
105 if (evSelectFD(opaqueCtx
, fd
, EV_READ
, readable
, new, &new->file
) < 0)
107 if (copyvec(new, iov
, iocnt
) < 0)
109 new->prevDone
= NULL
;
110 new->nextDone
= NULL
;
111 if (ctx
->streams
!= NULL
)
112 ctx
->streams
->prev
= new;
114 new->next
= ctx
->streams
;
127 evTimeRW(evContext opaqueCtx
, evStreamID id
, evTimerID timer
) /*ARGSUSED*/ {
128 evStream
*str
= id
.opaque
;
133 str
->flags
|= EV_STR_TIMEROK
;
138 evUntimeRW(evContext opaqueCtx
, evStreamID id
) /*ARGSUSED*/ {
139 evStream
*str
= id
.opaque
;
143 str
->flags
&= ~EV_STR_TIMEROK
;
148 evCancelRW(evContext opaqueCtx
, evStreamID id
) {
149 evContext_p
*ctx
= opaqueCtx
.opaque
;
150 evStream
*old
= id
.opaque
;
153 * The streams list is doubly threaded. First, there's ctx->streams
154 * that's used by evDestroy() to find and cancel all streams. Second,
155 * there's ctx->strDone (head) and ctx->strLast (tail) which thread
156 * through the potentially smaller number of "IO completed" streams,
157 * used in evGetNext() to avoid scanning the entire list.
160 /* Unlink from ctx->streams. */
161 if (old
->prev
!= NULL
)
162 old
->prev
->next
= old
->next
;
164 ctx
->streams
= old
->next
;
165 if (old
->next
!= NULL
)
166 old
->next
->prev
= old
->prev
;
169 * If 'old' is on the ctx->strDone list, remove it. Update
170 * ctx->strLast if necessary.
172 if (old
->prevDone
== NULL
&& old
->nextDone
== NULL
) {
174 * Either 'old' is the only item on the done list, or it's
175 * not on the done list. If the former, then we unlink it
176 * from the list. If the latter, we leave the list alone.
178 if (ctx
->strDone
== old
) {
183 if (old
->prevDone
!= NULL
)
184 old
->prevDone
->nextDone
= old
->nextDone
;
186 ctx
->strDone
= old
->nextDone
;
187 if (old
->nextDone
!= NULL
)
188 old
->nextDone
->prevDone
= old
->prevDone
;
190 ctx
->strLast
= old
->prevDone
;
193 /* Deallocate the stream. */
194 if (old
->file
.opaque
)
195 evDeselectFD(opaqueCtx
, old
->file
);
196 memput(old
->iovOrig
, sizeof (struct iovec
) * old
->iovOrigCount
);
201 /* Copy a scatter/gather vector and initialize a stream handler's IO. */
203 copyvec(evStream
*str
, const struct iovec
*iov
, int iocnt
) {
206 str
->iovOrig
= (struct iovec
*)memget(sizeof(struct iovec
) * iocnt
);
207 if (str
->iovOrig
== NULL
) {
212 for (i
= 0; i
< iocnt
; i
++) {
213 str
->iovOrig
[i
] = iov
[i
];
214 str
->ioTotal
+= iov
[i
].iov_len
;
216 str
->iovOrigCount
= iocnt
;
217 str
->iovCur
= str
->iovOrig
;
218 str
->iovCurCount
= str
->iovOrigCount
;
223 /* Pull off or truncate lead iovec(s). */
225 consume(evStream
*str
, size_t bytes
) {
227 if (bytes
< (size_t)str
->iovCur
->iov_len
) {
228 str
->iovCur
->iov_len
-= bytes
;
229 str
->iovCur
->iov_base
= (void *)
230 ((u_char
*)str
->iovCur
->iov_base
+ bytes
);
231 str
->ioDone
+= bytes
;
234 bytes
-= str
->iovCur
->iov_len
;
235 str
->ioDone
+= str
->iovCur
->iov_len
;
242 /* Add a stream to Done list and deselect the FD. */
244 done(evContext opaqueCtx
, evStream
*str
) {
245 evContext_p
*ctx
= opaqueCtx
.opaque
;
247 if (ctx
->strLast
!= NULL
) {
248 str
->prevDone
= ctx
->strLast
;
249 ctx
->strLast
->nextDone
= str
;
252 INSIST(ctx
->strDone
== NULL
);
253 ctx
->strDone
= ctx
->strLast
= str
;
255 evDeselectFD(opaqueCtx
, str
->file
);
256 str
->file
.opaque
= NULL
;
257 /* evDrop() will call evCancelRW() on us. */
260 /* Dribble out some bytes on the stream. (Called by evDispatch().) */
262 writable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
268 bytes
= writev(fd
, str
->iovCur
, str
->iovCurCount
);
270 if ((str
->flags
& EV_STR_TIMEROK
) != 0)
271 evTouchIdleTimer(opaqueCtx
, str
->timer
);
274 if (bytes
< 0 && errno
!= EINTR
) {
276 str
->ioErrno
= errno
;
279 if (str
->ioDone
== -1 || str
->ioDone
== str
->ioTotal
)
280 done(opaqueCtx
, str
);
283 /* Scoop up some bytes from the stream. (Called by evDispatch().) */
285 readable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
291 bytes
= readv(fd
, str
->iovCur
, str
->iovCurCount
);
293 if ((str
->flags
& EV_STR_TIMEROK
) != 0)
294 evTouchIdleTimer(opaqueCtx
, str
->timer
);
300 if (errno
!= EINTR
) {
302 str
->ioErrno
= errno
;
306 if (str
->ioDone
<= 0 || str
->ioDone
== str
->ioTotal
)
307 done(opaqueCtx
, str
);