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"
27 #include "fd_setsize.h"
29 #include <sys/types.h>
34 #include <isc/eventlib.h>
35 #include <isc/assertions.h>
36 #include "eventlib_p.h"
38 #include "port_after.h"
40 static int copyvec(evStream
*str
, const struct iovec
*iov
, int iocnt
);
41 static void consume(evStream
*str
, size_t bytes
);
42 static void done(evContext opaqueCtx
, evStream
*str
);
43 static void writable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
);
44 static void readable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
);
47 evConsIovec(void *buf
, size_t cnt
) {
50 memset(&ret
, 0xf5, sizeof ret
);
57 evWrite(evContext opaqueCtx
, int fd
, const struct iovec
*iov
, int iocnt
,
58 evStreamFunc func
, void *uap
, evStreamID
*id
)
60 evContext_p
*ctx
= opaqueCtx
.opaque
;
69 if (evSelectFD(opaqueCtx
, fd
, EV_WRITE
, writable
, new, &new->file
) < 0)
71 if (copyvec(new, iov
, iocnt
) < 0)
75 if (ctx
->streams
!= NULL
)
76 ctx
->streams
->prev
= new;
78 new->next
= ctx
->streams
;
91 evRead(evContext opaqueCtx
, int fd
, const struct iovec
*iov
, int iocnt
,
92 evStreamFunc func
, void *uap
, evStreamID
*id
)
94 evContext_p
*ctx
= opaqueCtx
.opaque
;
103 if (evSelectFD(opaqueCtx
, fd
, EV_READ
, readable
, new, &new->file
) < 0)
105 if (copyvec(new, iov
, iocnt
) < 0)
107 new->prevDone
= NULL
;
108 new->nextDone
= NULL
;
109 if (ctx
->streams
!= NULL
)
110 ctx
->streams
->prev
= new;
112 new->next
= ctx
->streams
;
125 evTimeRW(evContext opaqueCtx
, evStreamID id
, evTimerID timer
) /*ARGSUSED*/ {
126 evStream
*str
= id
.opaque
;
131 str
->flags
|= EV_STR_TIMEROK
;
136 evUntimeRW(evContext opaqueCtx
, evStreamID id
) /*ARGSUSED*/ {
137 evStream
*str
= id
.opaque
;
141 str
->flags
&= ~EV_STR_TIMEROK
;
146 evCancelRW(evContext opaqueCtx
, evStreamID id
) {
147 evContext_p
*ctx
= opaqueCtx
.opaque
;
148 evStream
*old
= id
.opaque
;
151 * The streams list is doubly threaded. First, there's ctx->streams
152 * that's used by evDestroy() to find and cancel all streams. Second,
153 * there's ctx->strDone (head) and ctx->strLast (tail) which thread
154 * through the potentially smaller number of "IO completed" streams,
155 * used in evGetNext() to avoid scanning the entire list.
158 /* Unlink from ctx->streams. */
159 if (old
->prev
!= NULL
)
160 old
->prev
->next
= old
->next
;
162 ctx
->streams
= old
->next
;
163 if (old
->next
!= NULL
)
164 old
->next
->prev
= old
->prev
;
167 * If 'old' is on the ctx->strDone list, remove it. Update
168 * ctx->strLast if necessary.
170 if (old
->prevDone
== NULL
&& old
->nextDone
== NULL
) {
172 * Either 'old' is the only item on the done list, or it's
173 * not on the done list. If the former, then we unlink it
174 * from the list. If the latter, we leave the list alone.
176 if (ctx
->strDone
== old
) {
181 if (old
->prevDone
!= NULL
)
182 old
->prevDone
->nextDone
= old
->nextDone
;
184 ctx
->strDone
= old
->nextDone
;
185 if (old
->nextDone
!= NULL
)
186 old
->nextDone
->prevDone
= old
->prevDone
;
188 ctx
->strLast
= old
->prevDone
;
191 /* Deallocate the stream. */
192 if (old
->file
.opaque
)
193 evDeselectFD(opaqueCtx
, old
->file
);
194 memput(old
->iovOrig
, sizeof (struct iovec
) * old
->iovOrigCount
);
199 /* Copy a scatter/gather vector and initialize a stream handler's IO. */
201 copyvec(evStream
*str
, const struct iovec
*iov
, int iocnt
) {
204 str
->iovOrig
= (struct iovec
*)memget(sizeof(struct iovec
) * iocnt
);
205 if (str
->iovOrig
== NULL
) {
210 for (i
= 0; i
< iocnt
; i
++) {
211 str
->iovOrig
[i
] = iov
[i
];
212 str
->ioTotal
+= iov
[i
].iov_len
;
214 str
->iovOrigCount
= iocnt
;
215 str
->iovCur
= str
->iovOrig
;
216 str
->iovCurCount
= str
->iovOrigCount
;
221 /* Pull off or truncate lead iovec(s). */
223 consume(evStream
*str
, size_t bytes
) {
225 if (bytes
< (size_t)str
->iovCur
->iov_len
) {
226 str
->iovCur
->iov_len
-= bytes
;
227 str
->iovCur
->iov_base
= (void *)
228 ((u_char
*)str
->iovCur
->iov_base
+ bytes
);
229 str
->ioDone
+= bytes
;
232 bytes
-= str
->iovCur
->iov_len
;
233 str
->ioDone
+= str
->iovCur
->iov_len
;
240 /* Add a stream to Done list and deselect the FD. */
242 done(evContext opaqueCtx
, evStream
*str
) {
243 evContext_p
*ctx
= opaqueCtx
.opaque
;
245 if (ctx
->strLast
!= NULL
) {
246 str
->prevDone
= ctx
->strLast
;
247 ctx
->strLast
->nextDone
= str
;
250 INSIST(ctx
->strDone
== NULL
);
251 ctx
->strDone
= ctx
->strLast
= str
;
253 evDeselectFD(opaqueCtx
, str
->file
);
254 str
->file
.opaque
= NULL
;
255 /* evDrop() will call evCancelRW() on us. */
258 /* Dribble out some bytes on the stream. (Called by evDispatch().) */
260 writable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
266 bytes
= writev(fd
, str
->iovCur
, str
->iovCurCount
);
268 if ((str
->flags
& EV_STR_TIMEROK
) != 0)
269 evTouchIdleTimer(opaqueCtx
, str
->timer
);
272 if (bytes
< 0 && errno
!= EINTR
) {
274 str
->ioErrno
= errno
;
277 if (str
->ioDone
== -1 || str
->ioDone
== str
->ioTotal
)
278 done(opaqueCtx
, str
);
281 /* Scoop up some bytes from the stream. (Called by evDispatch().) */
283 readable(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
289 bytes
= readv(fd
, str
->iovCur
, str
->iovCurCount
);
291 if ((str
->flags
& EV_STR_TIMEROK
) != 0)
292 evTouchIdleTimer(opaqueCtx
, str
->timer
);
298 if (errno
!= EINTR
) {
300 str
->ioErrno
= errno
;
304 if (str
->ioDone
<= 0 || str
->ioDone
== str
->ioTotal
)
305 done(opaqueCtx
, str
);