2 * SPDX-License-Identifier: ISC
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 $";
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "port_before.h"
32 #include "fd_setsize.h"
35 #include <sys/types.h>
40 #include <isc/eventlib.h>
42 #include <isc/assertions.h>
44 #include "eventlib_p.h"
46 #include "port_after.h"
49 static int copyvec(evStream *str, const struct iovec *iov, int iocnt);
50 static void consume(evStream *str, size_t bytes);
51 static void done(evContext opaqueCtx, evStream *str);
52 static void writable(evContext opaqueCtx, void *uap, int fd, int evmask);
53 static void readable(evContext opaqueCtx, void *uap, int fd, int evmask);
57 evConsIovec(void *buf, size_t cnt) {
60 memset(&ret, 0xf5, sizeof ret);
68 evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
69 evStreamFunc func, void *uap, evStreamID *id)
71 evContext_p *ctx = opaqueCtx.opaque;
80 if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0)
82 if (copyvec(new, iov, iocnt) < 0)
86 if (ctx->streams != NULL)
87 ctx->streams->prev = new;
89 new->next = ctx->streams;
102 evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
103 evStreamFunc func, void *uap, evStreamID *id)
105 evContext_p *ctx = opaqueCtx.opaque;
114 if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0)
116 if (copyvec(new, iov, iocnt) < 0)
118 new->prevDone = NULL;
119 new->nextDone = NULL;
120 if (ctx->streams != NULL)
121 ctx->streams->prev = new;
123 new->next = ctx->streams;
136 evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ {
137 evStream *str = id.opaque;
142 str->flags |= EV_STR_TIMEROK;
147 evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ {
148 evStream *str = id.opaque;
152 str->flags &= ~EV_STR_TIMEROK;
157 evCancelRW(evContext opaqueCtx, evStreamID id) {
158 evContext_p *ctx = opaqueCtx.opaque;
159 evStream *old = id.opaque;
162 * The streams list is doubly threaded. First, there's ctx->streams
163 * that's used by evDestroy() to find and cancel all streams. Second,
164 * there's ctx->strDone (head) and ctx->strLast (tail) which thread
165 * through the potentially smaller number of "IO completed" streams,
166 * used in evGetNext() to avoid scanning the entire list.
169 /* Unlink from ctx->streams. */
170 if (old->prev != NULL)
171 old->prev->next = old->next;
173 ctx->streams = old->next;
174 if (old->next != NULL)
175 old->next->prev = old->prev;
178 * If 'old' is on the ctx->strDone list, remove it. Update
179 * ctx->strLast if necessary.
181 if (old->prevDone == NULL && old->nextDone == NULL) {
183 * Either 'old' is the only item on the done list, or it's
184 * not on the done list. If the former, then we unlink it
185 * from the list. If the latter, we leave the list alone.
187 if (ctx->strDone == old) {
192 if (old->prevDone != NULL)
193 old->prevDone->nextDone = old->nextDone;
195 ctx->strDone = old->nextDone;
196 if (old->nextDone != NULL)
197 old->nextDone->prevDone = old->prevDone;
199 ctx->strLast = old->prevDone;
202 /* Deallocate the stream. */
203 if (old->file.opaque)
204 evDeselectFD(opaqueCtx, old->file);
205 memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount);
210 /* Copy a scatter/gather vector and initialize a stream handler's IO. */
212 copyvec(evStream *str, const struct iovec *iov, int iocnt) {
215 str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt);
216 if (str->iovOrig == NULL) {
221 for (i = 0; i < iocnt; i++) {
222 str->iovOrig[i] = iov[i];
223 str->ioTotal += iov[i].iov_len;
225 str->iovOrigCount = iocnt;
226 str->iovCur = str->iovOrig;
227 str->iovCurCount = str->iovOrigCount;
232 /* Pull off or truncate lead iovec(s). */
234 consume(evStream *str, size_t bytes) {
236 if (bytes < (size_t)str->iovCur->iov_len) {
237 str->iovCur->iov_len -= bytes;
238 str->iovCur->iov_base = (void *)
239 ((u_char *)str->iovCur->iov_base + bytes);
240 str->ioDone += bytes;
243 bytes -= str->iovCur->iov_len;
244 str->ioDone += str->iovCur->iov_len;
251 /* Add a stream to Done list and deselect the FD. */
253 done(evContext opaqueCtx, evStream *str) {
254 evContext_p *ctx = opaqueCtx.opaque;
256 if (ctx->strLast != NULL) {
257 str->prevDone = ctx->strLast;
258 ctx->strLast->nextDone = str;
261 INSIST(ctx->strDone == NULL);
262 ctx->strDone = ctx->strLast = str;
264 evDeselectFD(opaqueCtx, str->file);
265 str->file.opaque = NULL;
266 /* evDrop() will call evCancelRW() on us. */
269 /* Dribble out some bytes on the stream. (Called by evDispatch().) */
271 writable(evContext opaqueCtx, void *uap, int fd, int evmask) {
277 bytes = writev(fd, str->iovCur, str->iovCurCount);
279 if ((str->flags & EV_STR_TIMEROK) != 0)
280 evTouchIdleTimer(opaqueCtx, str->timer);
283 if (bytes < 0 && errno != EINTR) {
285 str->ioErrno = errno;
288 if (str->ioDone == -1 || str->ioDone == str->ioTotal)
289 done(opaqueCtx, str);
292 /* Scoop up some bytes from the stream. (Called by evDispatch().) */
294 readable(evContext opaqueCtx, void *uap, int fd, int evmask) {
300 bytes = readv(fd, str->iovCur, str->iovCurCount);
302 if ((str->flags & EV_STR_TIMEROK) != 0)
303 evTouchIdleTimer(opaqueCtx, str->timer);
309 if (errno != EINTR) {
311 str->ioErrno = errno;
315 if (str->ioDone <= 0 || str->ioDone == str->ioTotal)
316 done(opaqueCtx, str);