2N/A/*
2N/A * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
2N/A * Copyright (c) 1996-1999 by Internet Software Consortium
2N/A *
2N/A * Permission to use, copy, modify, and distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
2N/A * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2N/A * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
2N/A * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2N/A * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2N/A * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
2N/A * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2N/A */
2N/A
2N/A/* ev_streams.c - implement asynch stream file IO for the eventlib
2N/A * vix 04mar96 [initial]
2N/A */
2N/A
2N/A#if !defined(LINT) && !defined(CODECENTER)
2N/Astatic const char rcsid[] = "$Id: ev_streams.c,v 1.5 2005/04/27 04:56:36 sra Exp $";
2N/A#endif
2N/A
2N/A#include "port_before.h"
2N/A#include "fd_setsize.h"
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/uio.h>
2N/A
2N/A#include <errno.h>
2N/A
2N/A#include <isc/eventlib.h>
2N/A#include <isc/assertions.h>
2N/A#include "eventlib_p.h"
2N/A
2N/A#include "port_after.h"
2N/A
2N/Astatic int copyvec(evStream *str, const struct iovec *iov, int iocnt);
2N/Astatic void consume(evStream *str, size_t bytes);
2N/Astatic void done(evContext opaqueCtx, evStream *str);
2N/Astatic void writable(evContext opaqueCtx, void *uap, int fd, int evmask);
2N/Astatic void readable(evContext opaqueCtx, void *uap, int fd, int evmask);
2N/A
2N/Astruct iovec
2N/AevConsIovec(void *buf, size_t cnt) {
2N/A struct iovec ret;
2N/A
2N/A memset(&ret, 0xf5, sizeof ret);
2N/A ret.iov_base = buf;
2N/A ret.iov_len = cnt;
2N/A return (ret);
2N/A}
2N/A
2N/Aint
2N/AevWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
2N/A evStreamFunc func, void *uap, evStreamID *id)
2N/A{
2N/A evContext_p *ctx = opaqueCtx.opaque;
2N/A evStream *new;
2N/A int save;
2N/A
2N/A OKNEW(new);
2N/A new->func = func;
2N/A new->uap = uap;
2N/A new->fd = fd;
2N/A new->flags = 0;
2N/A if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0)
2N/A goto free;
2N/A if (copyvec(new, iov, iocnt) < 0)
2N/A goto free;
2N/A new->prevDone = NULL;
2N/A new->nextDone = NULL;
2N/A if (ctx->streams != NULL)
2N/A ctx->streams->prev = new;
2N/A new->prev = NULL;
2N/A new->next = ctx->streams;
2N/A ctx->streams = new;
2N/A if (id != NULL)
2N/A id->opaque = new;
2N/A return (0);
2N/A free:
2N/A save = errno;
2N/A FREE(new);
2N/A errno = save;
2N/A return (-1);
2N/A}
2N/A
2N/Aint
2N/AevRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
2N/A evStreamFunc func, void *uap, evStreamID *id)
2N/A{
2N/A evContext_p *ctx = opaqueCtx.opaque;
2N/A evStream *new;
2N/A int save;
2N/A
2N/A OKNEW(new);
2N/A new->func = func;
2N/A new->uap = uap;
2N/A new->fd = fd;
2N/A new->flags = 0;
2N/A if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0)
2N/A goto free;
2N/A if (copyvec(new, iov, iocnt) < 0)
2N/A goto free;
2N/A new->prevDone = NULL;
2N/A new->nextDone = NULL;
2N/A if (ctx->streams != NULL)
2N/A ctx->streams->prev = new;
2N/A new->prev = NULL;
2N/A new->next = ctx->streams;
2N/A ctx->streams = new;
2N/A if (id)
2N/A id->opaque = new;
2N/A return (0);
2N/A free:
2N/A save = errno;
2N/A FREE(new);
2N/A errno = save;
2N/A return (-1);
2N/A}
2N/A
2N/Aint
2N/AevTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ {
2N/A evStream *str = id.opaque;
2N/A
2N/A UNUSED(opaqueCtx);
2N/A
2N/A str->timer = timer;
2N/A str->flags |= EV_STR_TIMEROK;
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/AevUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ {
2N/A evStream *str = id.opaque;
2N/A
2N/A UNUSED(opaqueCtx);
2N/A
2N/A str->flags &= ~EV_STR_TIMEROK;
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/AevCancelRW(evContext opaqueCtx, evStreamID id) {
2N/A evContext_p *ctx = opaqueCtx.opaque;
2N/A evStream *old = id.opaque;
2N/A
2N/A /*
2N/A * The streams list is doubly threaded. First, there's ctx->streams
2N/A * that's used by evDestroy() to find and cancel all streams. Second,
2N/A * there's ctx->strDone (head) and ctx->strLast (tail) which thread
2N/A * through the potentially smaller number of "IO completed" streams,
2N/A * used in evGetNext() to avoid scanning the entire list.
2N/A */
2N/A
2N/A /* Unlink from ctx->streams. */
2N/A if (old->prev != NULL)
2N/A old->prev->next = old->next;
2N/A else
2N/A ctx->streams = old->next;
2N/A if (old->next != NULL)
2N/A old->next->prev = old->prev;
2N/A
2N/A /*
2N/A * If 'old' is on the ctx->strDone list, remove it. Update
2N/A * ctx->strLast if necessary.
2N/A */
2N/A if (old->prevDone == NULL && old->nextDone == NULL) {
2N/A /*
2N/A * Either 'old' is the only item on the done list, or it's
2N/A * not on the done list. If the former, then we unlink it
2N/A * from the list. If the latter, we leave the list alone.
2N/A */
2N/A if (ctx->strDone == old) {
2N/A ctx->strDone = NULL;
2N/A ctx->strLast = NULL;
2N/A }
2N/A } else {
2N/A if (old->prevDone != NULL)
2N/A old->prevDone->nextDone = old->nextDone;
2N/A else
2N/A ctx->strDone = old->nextDone;
2N/A if (old->nextDone != NULL)
2N/A old->nextDone->prevDone = old->prevDone;
2N/A else
2N/A ctx->strLast = old->prevDone;
2N/A }
2N/A
2N/A /* Deallocate the stream. */
2N/A if (old->file.opaque)
2N/A evDeselectFD(opaqueCtx, old->file);
2N/A memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount);
2N/A FREE(old);
2N/A return (0);
2N/A}
2N/A
2N/A/* Copy a scatter/gather vector and initialize a stream handler's IO. */
2N/Astatic int
2N/Acopyvec(evStream *str, const struct iovec *iov, int iocnt) {
2N/A int i;
2N/A
2N/A str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt);
2N/A if (str->iovOrig == NULL) {
2N/A errno = ENOMEM;
2N/A return (-1);
2N/A }
2N/A str->ioTotal = 0;
2N/A for (i = 0; i < iocnt; i++) {
2N/A str->iovOrig[i] = iov[i];
2N/A str->ioTotal += iov[i].iov_len;
2N/A }
2N/A str->iovOrigCount = iocnt;
2N/A str->iovCur = str->iovOrig;
2N/A str->iovCurCount = str->iovOrigCount;
2N/A str->ioDone = 0;
2N/A return (0);
2N/A}
2N/A
2N/A/* Pull off or truncate lead iovec(s). */
2N/Astatic void
2N/Aconsume(evStream *str, size_t bytes) {
2N/A while (bytes > 0U) {
2N/A if (bytes < (size_t)str->iovCur->iov_len) {
2N/A str->iovCur->iov_len -= bytes;
2N/A str->iovCur->iov_base = (void *)
2N/A ((u_char *)str->iovCur->iov_base + bytes);
2N/A str->ioDone += bytes;
2N/A bytes = 0;
2N/A } else {
2N/A bytes -= str->iovCur->iov_len;
2N/A str->ioDone += str->iovCur->iov_len;
2N/A str->iovCur++;
2N/A str->iovCurCount--;
2N/A }
2N/A }
2N/A}
2N/A
2N/A/* Add a stream to Done list and deselect the FD. */
2N/Astatic void
2N/Adone(evContext opaqueCtx, evStream *str) {
2N/A evContext_p *ctx = opaqueCtx.opaque;
2N/A
2N/A if (ctx->strLast != NULL) {
2N/A str->prevDone = ctx->strLast;
2N/A ctx->strLast->nextDone = str;
2N/A ctx->strLast = str;
2N/A } else {
2N/A INSIST(ctx->strDone == NULL);
2N/A ctx->strDone = ctx->strLast = str;
2N/A }
2N/A evDeselectFD(opaqueCtx, str->file);
2N/A str->file.opaque = NULL;
2N/A /* evDrop() will call evCancelRW() on us. */
2N/A}
2N/A
2N/A/* Dribble out some bytes on the stream. (Called by evDispatch().) */
2N/Astatic void
2N/Awritable(evContext opaqueCtx, void *uap, int fd, int evmask) {
2N/A evStream *str = uap;
2N/A int bytes;
2N/A
2N/A UNUSED(evmask);
2N/A
2N/A bytes = writev(fd, str->iovCur, str->iovCurCount);
2N/A if (bytes > 0) {
2N/A if ((str->flags & EV_STR_TIMEROK) != 0)
2N/A evTouchIdleTimer(opaqueCtx, str->timer);
2N/A consume(str, bytes);
2N/A } else {
2N/A if (bytes < 0 && errno != EINTR) {
2N/A str->ioDone = -1;
2N/A str->ioErrno = errno;
2N/A }
2N/A }
2N/A if (str->ioDone == -1 || str->ioDone == str->ioTotal)
2N/A done(opaqueCtx, str);
2N/A}
2N/A
2N/A/* Scoop up some bytes from the stream. (Called by evDispatch().) */
2N/Astatic void
2N/Areadable(evContext opaqueCtx, void *uap, int fd, int evmask) {
2N/A evStream *str = uap;
2N/A int bytes;
2N/A
2N/A UNUSED(evmask);
2N/A
2N/A bytes = readv(fd, str->iovCur, str->iovCurCount);
2N/A if (bytes > 0) {
2N/A if ((str->flags & EV_STR_TIMEROK) != 0)
2N/A evTouchIdleTimer(opaqueCtx, str->timer);
2N/A consume(str, bytes);
2N/A } else {
2N/A if (bytes == 0)
2N/A str->ioDone = 0;
2N/A else {
2N/A if (errno != EINTR) {
2N/A str->ioDone = -1;
2N/A str->ioErrno = errno;
2N/A }
2N/A }
2N/A }
2N/A if (str->ioDone <= 0 || str->ioDone == str->ioTotal)
2N/A done(opaqueCtx, str);
2N/A}
2N/A
2N/A/*! \file */