165e96449SHajimu UMEMOTO /* 265e96449SHajimu UMEMOTO * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 365e96449SHajimu UMEMOTO * Copyright (c) 1996-1999 by Internet Software Consortium 465e96449SHajimu UMEMOTO * 565e96449SHajimu UMEMOTO * Permission to use, copy, modify, and distribute this software for any 665e96449SHajimu UMEMOTO * purpose with or without fee is hereby granted, provided that the above 765e96449SHajimu UMEMOTO * copyright notice and this permission notice appear in all copies. 865e96449SHajimu UMEMOTO * 965e96449SHajimu UMEMOTO * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 1065e96449SHajimu UMEMOTO * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1165e96449SHajimu UMEMOTO * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 1265e96449SHajimu UMEMOTO * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1365e96449SHajimu UMEMOTO * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1465e96449SHajimu UMEMOTO * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 1565e96449SHajimu UMEMOTO * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1665e96449SHajimu UMEMOTO */ 1765e96449SHajimu UMEMOTO 1865e96449SHajimu UMEMOTO /* ev_streams.c - implement asynch stream file IO for the eventlib 1965e96449SHajimu UMEMOTO * vix 04mar96 [initial] 2065e96449SHajimu UMEMOTO */ 2165e96449SHajimu UMEMOTO 2265e96449SHajimu UMEMOTO #if !defined(LINT) && !defined(CODECENTER) 23dde4a85dSHajimu UMEMOTO static const char rcsid[] = "$Id: ev_streams.c,v 1.4.18.1 2005/04/27 05:01:06 sra Exp $"; 2465e96449SHajimu UMEMOTO #endif 25ab96eeabSHajimu UMEMOTO #include <sys/cdefs.h> 26ab96eeabSHajimu UMEMOTO __FBSDID("$FreeBSD$"); 2765e96449SHajimu UMEMOTO 2865e96449SHajimu UMEMOTO #include "port_before.h" 29ab96eeabSHajimu UMEMOTO #ifndef _LIBC 3065e96449SHajimu UMEMOTO #include "fd_setsize.h" 31ab96eeabSHajimu UMEMOTO #endif 3265e96449SHajimu UMEMOTO 3365e96449SHajimu UMEMOTO #include <sys/types.h> 3465e96449SHajimu UMEMOTO #include <sys/uio.h> 3565e96449SHajimu UMEMOTO 3665e96449SHajimu UMEMOTO #include <errno.h> 3765e96449SHajimu UMEMOTO 3865e96449SHajimu UMEMOTO #include <isc/eventlib.h> 39ab96eeabSHajimu UMEMOTO #ifndef _LIBC 4065e96449SHajimu UMEMOTO #include <isc/assertions.h> 41ab96eeabSHajimu UMEMOTO #endif 4265e96449SHajimu UMEMOTO #include "eventlib_p.h" 4365e96449SHajimu UMEMOTO 4465e96449SHajimu UMEMOTO #include "port_after.h" 4565e96449SHajimu UMEMOTO 46ab96eeabSHajimu UMEMOTO #ifndef _LIBC 4765e96449SHajimu UMEMOTO static int copyvec(evStream *str, const struct iovec *iov, int iocnt); 4865e96449SHajimu UMEMOTO static void consume(evStream *str, size_t bytes); 4965e96449SHajimu UMEMOTO static void done(evContext opaqueCtx, evStream *str); 5065e96449SHajimu UMEMOTO static void writable(evContext opaqueCtx, void *uap, int fd, int evmask); 5165e96449SHajimu UMEMOTO static void readable(evContext opaqueCtx, void *uap, int fd, int evmask); 52ab96eeabSHajimu UMEMOTO #endif 5365e96449SHajimu UMEMOTO 5465e96449SHajimu UMEMOTO struct iovec 5565e96449SHajimu UMEMOTO evConsIovec(void *buf, size_t cnt) { 5665e96449SHajimu UMEMOTO struct iovec ret; 5765e96449SHajimu UMEMOTO 5865e96449SHajimu UMEMOTO memset(&ret, 0xf5, sizeof ret); 5965e96449SHajimu UMEMOTO ret.iov_base = buf; 6065e96449SHajimu UMEMOTO ret.iov_len = cnt; 6165e96449SHajimu UMEMOTO return (ret); 6265e96449SHajimu UMEMOTO } 6365e96449SHajimu UMEMOTO 64ab96eeabSHajimu UMEMOTO #ifndef _LIBC 6565e96449SHajimu UMEMOTO int 6665e96449SHajimu UMEMOTO evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt, 6765e96449SHajimu UMEMOTO evStreamFunc func, void *uap, evStreamID *id) 6865e96449SHajimu UMEMOTO { 6965e96449SHajimu UMEMOTO evContext_p *ctx = opaqueCtx.opaque; 7065e96449SHajimu UMEMOTO evStream *new; 7165e96449SHajimu UMEMOTO int save; 7265e96449SHajimu UMEMOTO 7365e96449SHajimu UMEMOTO OKNEW(new); 7465e96449SHajimu UMEMOTO new->func = func; 7565e96449SHajimu UMEMOTO new->uap = uap; 7665e96449SHajimu UMEMOTO new->fd = fd; 7765e96449SHajimu UMEMOTO new->flags = 0; 7865e96449SHajimu UMEMOTO if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0) 7965e96449SHajimu UMEMOTO goto free; 8065e96449SHajimu UMEMOTO if (copyvec(new, iov, iocnt) < 0) 8165e96449SHajimu UMEMOTO goto free; 8265e96449SHajimu UMEMOTO new->prevDone = NULL; 8365e96449SHajimu UMEMOTO new->nextDone = NULL; 8465e96449SHajimu UMEMOTO if (ctx->streams != NULL) 8565e96449SHajimu UMEMOTO ctx->streams->prev = new; 8665e96449SHajimu UMEMOTO new->prev = NULL; 8765e96449SHajimu UMEMOTO new->next = ctx->streams; 8865e96449SHajimu UMEMOTO ctx->streams = new; 8965e96449SHajimu UMEMOTO if (id != NULL) 9065e96449SHajimu UMEMOTO id->opaque = new; 9165e96449SHajimu UMEMOTO return (0); 9265e96449SHajimu UMEMOTO free: 9365e96449SHajimu UMEMOTO save = errno; 9465e96449SHajimu UMEMOTO FREE(new); 9565e96449SHajimu UMEMOTO errno = save; 9665e96449SHajimu UMEMOTO return (-1); 9765e96449SHajimu UMEMOTO } 9865e96449SHajimu UMEMOTO 9965e96449SHajimu UMEMOTO int 10065e96449SHajimu UMEMOTO evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt, 10165e96449SHajimu UMEMOTO evStreamFunc func, void *uap, evStreamID *id) 10265e96449SHajimu UMEMOTO { 10365e96449SHajimu UMEMOTO evContext_p *ctx = opaqueCtx.opaque; 10465e96449SHajimu UMEMOTO evStream *new; 10565e96449SHajimu UMEMOTO int save; 10665e96449SHajimu UMEMOTO 10765e96449SHajimu UMEMOTO OKNEW(new); 10865e96449SHajimu UMEMOTO new->func = func; 10965e96449SHajimu UMEMOTO new->uap = uap; 11065e96449SHajimu UMEMOTO new->fd = fd; 11165e96449SHajimu UMEMOTO new->flags = 0; 11265e96449SHajimu UMEMOTO if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0) 11365e96449SHajimu UMEMOTO goto free; 11465e96449SHajimu UMEMOTO if (copyvec(new, iov, iocnt) < 0) 11565e96449SHajimu UMEMOTO goto free; 11665e96449SHajimu UMEMOTO new->prevDone = NULL; 11765e96449SHajimu UMEMOTO new->nextDone = NULL; 11865e96449SHajimu UMEMOTO if (ctx->streams != NULL) 11965e96449SHajimu UMEMOTO ctx->streams->prev = new; 12065e96449SHajimu UMEMOTO new->prev = NULL; 12165e96449SHajimu UMEMOTO new->next = ctx->streams; 12265e96449SHajimu UMEMOTO ctx->streams = new; 12365e96449SHajimu UMEMOTO if (id) 12465e96449SHajimu UMEMOTO id->opaque = new; 12565e96449SHajimu UMEMOTO return (0); 12665e96449SHajimu UMEMOTO free: 12765e96449SHajimu UMEMOTO save = errno; 12865e96449SHajimu UMEMOTO FREE(new); 12965e96449SHajimu UMEMOTO errno = save; 13065e96449SHajimu UMEMOTO return (-1); 13165e96449SHajimu UMEMOTO } 13265e96449SHajimu UMEMOTO 13365e96449SHajimu UMEMOTO int 13465e96449SHajimu UMEMOTO evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ { 13565e96449SHajimu UMEMOTO evStream *str = id.opaque; 13665e96449SHajimu UMEMOTO 13765e96449SHajimu UMEMOTO UNUSED(opaqueCtx); 13865e96449SHajimu UMEMOTO 13965e96449SHajimu UMEMOTO str->timer = timer; 14065e96449SHajimu UMEMOTO str->flags |= EV_STR_TIMEROK; 14165e96449SHajimu UMEMOTO return (0); 14265e96449SHajimu UMEMOTO } 14365e96449SHajimu UMEMOTO 14465e96449SHajimu UMEMOTO int 14565e96449SHajimu UMEMOTO evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ { 14665e96449SHajimu UMEMOTO evStream *str = id.opaque; 14765e96449SHajimu UMEMOTO 14865e96449SHajimu UMEMOTO UNUSED(opaqueCtx); 14965e96449SHajimu UMEMOTO 15065e96449SHajimu UMEMOTO str->flags &= ~EV_STR_TIMEROK; 15165e96449SHajimu UMEMOTO return (0); 15265e96449SHajimu UMEMOTO } 15365e96449SHajimu UMEMOTO 15465e96449SHajimu UMEMOTO int 15565e96449SHajimu UMEMOTO evCancelRW(evContext opaqueCtx, evStreamID id) { 15665e96449SHajimu UMEMOTO evContext_p *ctx = opaqueCtx.opaque; 15765e96449SHajimu UMEMOTO evStream *old = id.opaque; 15865e96449SHajimu UMEMOTO 15965e96449SHajimu UMEMOTO /* 16065e96449SHajimu UMEMOTO * The streams list is doubly threaded. First, there's ctx->streams 16165e96449SHajimu UMEMOTO * that's used by evDestroy() to find and cancel all streams. Second, 16265e96449SHajimu UMEMOTO * there's ctx->strDone (head) and ctx->strLast (tail) which thread 16365e96449SHajimu UMEMOTO * through the potentially smaller number of "IO completed" streams, 16465e96449SHajimu UMEMOTO * used in evGetNext() to avoid scanning the entire list. 16565e96449SHajimu UMEMOTO */ 16665e96449SHajimu UMEMOTO 16765e96449SHajimu UMEMOTO /* Unlink from ctx->streams. */ 16865e96449SHajimu UMEMOTO if (old->prev != NULL) 16965e96449SHajimu UMEMOTO old->prev->next = old->next; 17065e96449SHajimu UMEMOTO else 17165e96449SHajimu UMEMOTO ctx->streams = old->next; 17265e96449SHajimu UMEMOTO if (old->next != NULL) 17365e96449SHajimu UMEMOTO old->next->prev = old->prev; 17465e96449SHajimu UMEMOTO 17565e96449SHajimu UMEMOTO /* 17665e96449SHajimu UMEMOTO * If 'old' is on the ctx->strDone list, remove it. Update 17765e96449SHajimu UMEMOTO * ctx->strLast if necessary. 17865e96449SHajimu UMEMOTO */ 17965e96449SHajimu UMEMOTO if (old->prevDone == NULL && old->nextDone == NULL) { 18065e96449SHajimu UMEMOTO /* 18165e96449SHajimu UMEMOTO * Either 'old' is the only item on the done list, or it's 18265e96449SHajimu UMEMOTO * not on the done list. If the former, then we unlink it 18365e96449SHajimu UMEMOTO * from the list. If the latter, we leave the list alone. 18465e96449SHajimu UMEMOTO */ 18565e96449SHajimu UMEMOTO if (ctx->strDone == old) { 18665e96449SHajimu UMEMOTO ctx->strDone = NULL; 18765e96449SHajimu UMEMOTO ctx->strLast = NULL; 18865e96449SHajimu UMEMOTO } 18965e96449SHajimu UMEMOTO } else { 19065e96449SHajimu UMEMOTO if (old->prevDone != NULL) 19165e96449SHajimu UMEMOTO old->prevDone->nextDone = old->nextDone; 19265e96449SHajimu UMEMOTO else 19365e96449SHajimu UMEMOTO ctx->strDone = old->nextDone; 19465e96449SHajimu UMEMOTO if (old->nextDone != NULL) 19565e96449SHajimu UMEMOTO old->nextDone->prevDone = old->prevDone; 19665e96449SHajimu UMEMOTO else 19765e96449SHajimu UMEMOTO ctx->strLast = old->prevDone; 19865e96449SHajimu UMEMOTO } 19965e96449SHajimu UMEMOTO 20065e96449SHajimu UMEMOTO /* Deallocate the stream. */ 20165e96449SHajimu UMEMOTO if (old->file.opaque) 20265e96449SHajimu UMEMOTO evDeselectFD(opaqueCtx, old->file); 20365e96449SHajimu UMEMOTO memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount); 20465e96449SHajimu UMEMOTO FREE(old); 20565e96449SHajimu UMEMOTO return (0); 20665e96449SHajimu UMEMOTO } 20765e96449SHajimu UMEMOTO 20865e96449SHajimu UMEMOTO /* Copy a scatter/gather vector and initialize a stream handler's IO. */ 20965e96449SHajimu UMEMOTO static int 21065e96449SHajimu UMEMOTO copyvec(evStream *str, const struct iovec *iov, int iocnt) { 21165e96449SHajimu UMEMOTO int i; 21265e96449SHajimu UMEMOTO 21365e96449SHajimu UMEMOTO str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt); 21465e96449SHajimu UMEMOTO if (str->iovOrig == NULL) { 21565e96449SHajimu UMEMOTO errno = ENOMEM; 21665e96449SHajimu UMEMOTO return (-1); 21765e96449SHajimu UMEMOTO } 21865e96449SHajimu UMEMOTO str->ioTotal = 0; 21965e96449SHajimu UMEMOTO for (i = 0; i < iocnt; i++) { 22065e96449SHajimu UMEMOTO str->iovOrig[i] = iov[i]; 22165e96449SHajimu UMEMOTO str->ioTotal += iov[i].iov_len; 22265e96449SHajimu UMEMOTO } 22365e96449SHajimu UMEMOTO str->iovOrigCount = iocnt; 22465e96449SHajimu UMEMOTO str->iovCur = str->iovOrig; 22565e96449SHajimu UMEMOTO str->iovCurCount = str->iovOrigCount; 22665e96449SHajimu UMEMOTO str->ioDone = 0; 22765e96449SHajimu UMEMOTO return (0); 22865e96449SHajimu UMEMOTO } 22965e96449SHajimu UMEMOTO 23065e96449SHajimu UMEMOTO /* Pull off or truncate lead iovec(s). */ 23165e96449SHajimu UMEMOTO static void 23265e96449SHajimu UMEMOTO consume(evStream *str, size_t bytes) { 23365e96449SHajimu UMEMOTO while (bytes > 0U) { 23465e96449SHajimu UMEMOTO if (bytes < (size_t)str->iovCur->iov_len) { 23565e96449SHajimu UMEMOTO str->iovCur->iov_len -= bytes; 23665e96449SHajimu UMEMOTO str->iovCur->iov_base = (void *) 23765e96449SHajimu UMEMOTO ((u_char *)str->iovCur->iov_base + bytes); 23865e96449SHajimu UMEMOTO str->ioDone += bytes; 23965e96449SHajimu UMEMOTO bytes = 0; 24065e96449SHajimu UMEMOTO } else { 24165e96449SHajimu UMEMOTO bytes -= str->iovCur->iov_len; 24265e96449SHajimu UMEMOTO str->ioDone += str->iovCur->iov_len; 24365e96449SHajimu UMEMOTO str->iovCur++; 24465e96449SHajimu UMEMOTO str->iovCurCount--; 24565e96449SHajimu UMEMOTO } 24665e96449SHajimu UMEMOTO } 24765e96449SHajimu UMEMOTO } 24865e96449SHajimu UMEMOTO 24965e96449SHajimu UMEMOTO /* Add a stream to Done list and deselect the FD. */ 25065e96449SHajimu UMEMOTO static void 25165e96449SHajimu UMEMOTO done(evContext opaqueCtx, evStream *str) { 25265e96449SHajimu UMEMOTO evContext_p *ctx = opaqueCtx.opaque; 25365e96449SHajimu UMEMOTO 25465e96449SHajimu UMEMOTO if (ctx->strLast != NULL) { 25565e96449SHajimu UMEMOTO str->prevDone = ctx->strLast; 25665e96449SHajimu UMEMOTO ctx->strLast->nextDone = str; 25765e96449SHajimu UMEMOTO ctx->strLast = str; 25865e96449SHajimu UMEMOTO } else { 25965e96449SHajimu UMEMOTO INSIST(ctx->strDone == NULL); 26065e96449SHajimu UMEMOTO ctx->strDone = ctx->strLast = str; 26165e96449SHajimu UMEMOTO } 26265e96449SHajimu UMEMOTO evDeselectFD(opaqueCtx, str->file); 26365e96449SHajimu UMEMOTO str->file.opaque = NULL; 26465e96449SHajimu UMEMOTO /* evDrop() will call evCancelRW() on us. */ 26565e96449SHajimu UMEMOTO } 26665e96449SHajimu UMEMOTO 26765e96449SHajimu UMEMOTO /* Dribble out some bytes on the stream. (Called by evDispatch().) */ 26865e96449SHajimu UMEMOTO static void 26965e96449SHajimu UMEMOTO writable(evContext opaqueCtx, void *uap, int fd, int evmask) { 27065e96449SHajimu UMEMOTO evStream *str = uap; 27165e96449SHajimu UMEMOTO int bytes; 27265e96449SHajimu UMEMOTO 27365e96449SHajimu UMEMOTO UNUSED(evmask); 27465e96449SHajimu UMEMOTO 27565e96449SHajimu UMEMOTO bytes = writev(fd, str->iovCur, str->iovCurCount); 27665e96449SHajimu UMEMOTO if (bytes > 0) { 27765e96449SHajimu UMEMOTO if ((str->flags & EV_STR_TIMEROK) != 0) 27865e96449SHajimu UMEMOTO evTouchIdleTimer(opaqueCtx, str->timer); 27965e96449SHajimu UMEMOTO consume(str, bytes); 28065e96449SHajimu UMEMOTO } else { 28165e96449SHajimu UMEMOTO if (bytes < 0 && errno != EINTR) { 28265e96449SHajimu UMEMOTO str->ioDone = -1; 28365e96449SHajimu UMEMOTO str->ioErrno = errno; 28465e96449SHajimu UMEMOTO } 28565e96449SHajimu UMEMOTO } 28665e96449SHajimu UMEMOTO if (str->ioDone == -1 || str->ioDone == str->ioTotal) 28765e96449SHajimu UMEMOTO done(opaqueCtx, str); 28865e96449SHajimu UMEMOTO } 28965e96449SHajimu UMEMOTO 29065e96449SHajimu UMEMOTO /* Scoop up some bytes from the stream. (Called by evDispatch().) */ 29165e96449SHajimu UMEMOTO static void 29265e96449SHajimu UMEMOTO readable(evContext opaqueCtx, void *uap, int fd, int evmask) { 29365e96449SHajimu UMEMOTO evStream *str = uap; 29465e96449SHajimu UMEMOTO int bytes; 29565e96449SHajimu UMEMOTO 29665e96449SHajimu UMEMOTO UNUSED(evmask); 29765e96449SHajimu UMEMOTO 29865e96449SHajimu UMEMOTO bytes = readv(fd, str->iovCur, str->iovCurCount); 29965e96449SHajimu UMEMOTO if (bytes > 0) { 30065e96449SHajimu UMEMOTO if ((str->flags & EV_STR_TIMEROK) != 0) 30165e96449SHajimu UMEMOTO evTouchIdleTimer(opaqueCtx, str->timer); 30265e96449SHajimu UMEMOTO consume(str, bytes); 30365e96449SHajimu UMEMOTO } else { 30465e96449SHajimu UMEMOTO if (bytes == 0) 30565e96449SHajimu UMEMOTO str->ioDone = 0; 30665e96449SHajimu UMEMOTO else { 30765e96449SHajimu UMEMOTO if (errno != EINTR) { 30865e96449SHajimu UMEMOTO str->ioDone = -1; 30965e96449SHajimu UMEMOTO str->ioErrno = errno; 31065e96449SHajimu UMEMOTO } 31165e96449SHajimu UMEMOTO } 31265e96449SHajimu UMEMOTO } 31365e96449SHajimu UMEMOTO if (str->ioDone <= 0 || str->ioDone == str->ioTotal) 31465e96449SHajimu UMEMOTO done(opaqueCtx, str); 31565e96449SHajimu UMEMOTO } 316ab96eeabSHajimu UMEMOTO #endif 317dde4a85dSHajimu UMEMOTO 318dde4a85dSHajimu UMEMOTO /*! \file */ 319