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