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 "port_before.h"
29 #include "fd_setsize.h"
32 #include <sys/types.h>
37 #include <isc/eventlib.h>
39 #include <isc/assertions.h>
41 #include "eventlib_p.h"
43 #include "port_after.h"
46 static int copyvec(evStream *str, const struct iovec *iov, int iocnt);
47 static void consume(evStream *str, size_t bytes);
48 static void done(evContext opaqueCtx, evStream *str);
49 static void writable(evContext opaqueCtx, void *uap, int fd, int evmask);
50 static void readable(evContext opaqueCtx, void *uap, int fd, int evmask);
54 evConsIovec(void *buf, size_t cnt) {
57 memset(&ret, 0xf5, sizeof ret);
65 evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
66 evStreamFunc func, void *uap, evStreamID *id)
68 evContext_p *ctx = opaqueCtx.opaque;
77 if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0)
79 if (copyvec(new, iov, iocnt) < 0)
83 if (ctx->streams != NULL)
84 ctx->streams->prev = new;
86 new->next = ctx->streams;
99 evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
100 evStreamFunc func, void *uap, evStreamID *id)
102 evContext_p *ctx = opaqueCtx.opaque;
111 if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0)
113 if (copyvec(new, iov, iocnt) < 0)
115 new->prevDone = NULL;
116 new->nextDone = NULL;
117 if (ctx->streams != NULL)
118 ctx->streams->prev = new;
120 new->next = ctx->streams;
133 evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ {
134 evStream *str = id.opaque;
139 str->flags |= EV_STR_TIMEROK;
144 evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ {
145 evStream *str = id.opaque;
149 str->flags &= ~EV_STR_TIMEROK;
154 evCancelRW(evContext opaqueCtx, evStreamID id) {
155 evContext_p *ctx = opaqueCtx.opaque;
156 evStream *old = id.opaque;
159 * The streams list is doubly threaded. First, there's ctx->streams
160 * that's used by evDestroy() to find and cancel all streams. Second,
161 * there's ctx->strDone (head) and ctx->strLast (tail) which thread
162 * through the potentially smaller number of "IO completed" streams,
163 * used in evGetNext() to avoid scanning the entire list.
166 /* Unlink from ctx->streams. */
167 if (old->prev != NULL)
168 old->prev->next = old->next;
170 ctx->streams = old->next;
171 if (old->next != NULL)
172 old->next->prev = old->prev;
175 * If 'old' is on the ctx->strDone list, remove it. Update
176 * ctx->strLast if necessary.
178 if (old->prevDone == NULL && old->nextDone == NULL) {
180 * Either 'old' is the only item on the done list, or it's
181 * not on the done list. If the former, then we unlink it
182 * from the list. If the latter, we leave the list alone.
184 if (ctx->strDone == old) {
189 if (old->prevDone != NULL)
190 old->prevDone->nextDone = old->nextDone;
192 ctx->strDone = old->nextDone;
193 if (old->nextDone != NULL)
194 old->nextDone->prevDone = old->prevDone;
196 ctx->strLast = old->prevDone;
199 /* Deallocate the stream. */
200 if (old->file.opaque)
201 evDeselectFD(opaqueCtx, old->file);
202 memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount);
207 /* Copy a scatter/gather vector and initialize a stream handler's IO. */
209 copyvec(evStream *str, const struct iovec *iov, int iocnt) {
212 str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt);
213 if (str->iovOrig == NULL) {
218 for (i = 0; i < iocnt; i++) {
219 str->iovOrig[i] = iov[i];
220 str->ioTotal += iov[i].iov_len;
222 str->iovOrigCount = iocnt;
223 str->iovCur = str->iovOrig;
224 str->iovCurCount = str->iovOrigCount;
229 /* Pull off or truncate lead iovec(s). */
231 consume(evStream *str, size_t bytes) {
233 if (bytes < (size_t)str->iovCur->iov_len) {
234 str->iovCur->iov_len -= bytes;
235 str->iovCur->iov_base = (void *)
236 ((u_char *)str->iovCur->iov_base + bytes);
237 str->ioDone += bytes;
240 bytes -= str->iovCur->iov_len;
241 str->ioDone += str->iovCur->iov_len;
248 /* Add a stream to Done list and deselect the FD. */
250 done(evContext opaqueCtx, evStream *str) {
251 evContext_p *ctx = opaqueCtx.opaque;
253 if (ctx->strLast != NULL) {
254 str->prevDone = ctx->strLast;
255 ctx->strLast->nextDone = str;
258 INSIST(ctx->strDone == NULL);
259 ctx->strDone = ctx->strLast = str;
261 evDeselectFD(opaqueCtx, str->file);
262 str->file.opaque = NULL;
263 /* evDrop() will call evCancelRW() on us. */
266 /* Dribble out some bytes on the stream. (Called by evDispatch().) */
268 writable(evContext opaqueCtx, void *uap, int fd, int evmask) {
274 bytes = writev(fd, str->iovCur, str->iovCurCount);
276 if ((str->flags & EV_STR_TIMEROK) != 0)
277 evTouchIdleTimer(opaqueCtx, str->timer);
280 if (bytes < 0 && errno != EINTR) {
282 str->ioErrno = errno;
285 if (str->ioDone == -1 || str->ioDone == str->ioTotal)
286 done(opaqueCtx, str);
289 /* Scoop up some bytes from the stream. (Called by evDispatch().) */
291 readable(evContext opaqueCtx, void *uap, int fd, int evmask) {
297 bytes = readv(fd, str->iovCur, str->iovCurCount);
299 if ((str->flags & EV_STR_TIMEROK) != 0)
300 evTouchIdleTimer(opaqueCtx, str->timer);
306 if (errno != EINTR) {
308 str->ioErrno = errno;
312 if (str->ioDone <= 0 || str->ioDone == str->ioTotal)
313 done(opaqueCtx, str);