17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3*9525b14bSRao Shoaib * Copyright (c) 1996-1999 by Internet Software Consortium
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate *
9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate /* ev_streams.c - implement asynch stream file IO for the eventlib
197c478bd9Sstevel@tonic-gate * vix 04mar96 [initial]
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
23*9525b14bSRao Shoaib static const char rcsid[] = "$Id: ev_streams.c,v 1.5 2005/04/27 04:56:36 sra Exp $";
247c478bd9Sstevel@tonic-gate #endif
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include "port_before.h"
277c478bd9Sstevel@tonic-gate #include "fd_setsize.h"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/uio.h>
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include <isc/eventlib.h>
357c478bd9Sstevel@tonic-gate #include <isc/assertions.h>
367c478bd9Sstevel@tonic-gate #include "eventlib_p.h"
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate #include "port_after.h"
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate static int copyvec(evStream *str, const struct iovec *iov, int iocnt);
417c478bd9Sstevel@tonic-gate static void consume(evStream *str, size_t bytes);
427c478bd9Sstevel@tonic-gate static void done(evContext opaqueCtx, evStream *str);
437c478bd9Sstevel@tonic-gate static void writable(evContext opaqueCtx, void *uap, int fd, int evmask);
447c478bd9Sstevel@tonic-gate static void readable(evContext opaqueCtx, void *uap, int fd, int evmask);
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate struct iovec
evConsIovec(void * buf,size_t cnt)477c478bd9Sstevel@tonic-gate evConsIovec(void *buf, size_t cnt) {
487c478bd9Sstevel@tonic-gate struct iovec ret;
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate memset(&ret, 0xf5, sizeof ret);
517c478bd9Sstevel@tonic-gate ret.iov_base = buf;
527c478bd9Sstevel@tonic-gate ret.iov_len = cnt;
537c478bd9Sstevel@tonic-gate return (ret);
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate int
evWrite(evContext opaqueCtx,int fd,const struct iovec * iov,int iocnt,evStreamFunc func,void * uap,evStreamID * id)577c478bd9Sstevel@tonic-gate evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
587c478bd9Sstevel@tonic-gate evStreamFunc func, void *uap, evStreamID *id)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate evContext_p *ctx = opaqueCtx.opaque;
617c478bd9Sstevel@tonic-gate evStream *new;
627c478bd9Sstevel@tonic-gate int save;
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate OKNEW(new);
657c478bd9Sstevel@tonic-gate new->func = func;
667c478bd9Sstevel@tonic-gate new->uap = uap;
677c478bd9Sstevel@tonic-gate new->fd = fd;
687c478bd9Sstevel@tonic-gate new->flags = 0;
697c478bd9Sstevel@tonic-gate if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0)
707c478bd9Sstevel@tonic-gate goto free;
717c478bd9Sstevel@tonic-gate if (copyvec(new, iov, iocnt) < 0)
727c478bd9Sstevel@tonic-gate goto free;
737c478bd9Sstevel@tonic-gate new->prevDone = NULL;
747c478bd9Sstevel@tonic-gate new->nextDone = NULL;
757c478bd9Sstevel@tonic-gate if (ctx->streams != NULL)
767c478bd9Sstevel@tonic-gate ctx->streams->prev = new;
777c478bd9Sstevel@tonic-gate new->prev = NULL;
787c478bd9Sstevel@tonic-gate new->next = ctx->streams;
797c478bd9Sstevel@tonic-gate ctx->streams = new;
807c478bd9Sstevel@tonic-gate if (id != NULL)
817c478bd9Sstevel@tonic-gate id->opaque = new;
827c478bd9Sstevel@tonic-gate return (0);
837c478bd9Sstevel@tonic-gate free:
847c478bd9Sstevel@tonic-gate save = errno;
857c478bd9Sstevel@tonic-gate FREE(new);
867c478bd9Sstevel@tonic-gate errno = save;
877c478bd9Sstevel@tonic-gate return (-1);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate int
evRead(evContext opaqueCtx,int fd,const struct iovec * iov,int iocnt,evStreamFunc func,void * uap,evStreamID * id)917c478bd9Sstevel@tonic-gate evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
927c478bd9Sstevel@tonic-gate evStreamFunc func, void *uap, evStreamID *id)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate evContext_p *ctx = opaqueCtx.opaque;
957c478bd9Sstevel@tonic-gate evStream *new;
967c478bd9Sstevel@tonic-gate int save;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate OKNEW(new);
997c478bd9Sstevel@tonic-gate new->func = func;
1007c478bd9Sstevel@tonic-gate new->uap = uap;
1017c478bd9Sstevel@tonic-gate new->fd = fd;
1027c478bd9Sstevel@tonic-gate new->flags = 0;
1037c478bd9Sstevel@tonic-gate if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0)
1047c478bd9Sstevel@tonic-gate goto free;
1057c478bd9Sstevel@tonic-gate if (copyvec(new, iov, iocnt) < 0)
1067c478bd9Sstevel@tonic-gate goto free;
1077c478bd9Sstevel@tonic-gate new->prevDone = NULL;
1087c478bd9Sstevel@tonic-gate new->nextDone = NULL;
1097c478bd9Sstevel@tonic-gate if (ctx->streams != NULL)
1107c478bd9Sstevel@tonic-gate ctx->streams->prev = new;
1117c478bd9Sstevel@tonic-gate new->prev = NULL;
1127c478bd9Sstevel@tonic-gate new->next = ctx->streams;
1137c478bd9Sstevel@tonic-gate ctx->streams = new;
1147c478bd9Sstevel@tonic-gate if (id)
1157c478bd9Sstevel@tonic-gate id->opaque = new;
1167c478bd9Sstevel@tonic-gate return (0);
1177c478bd9Sstevel@tonic-gate free:
1187c478bd9Sstevel@tonic-gate save = errno;
1197c478bd9Sstevel@tonic-gate FREE(new);
1207c478bd9Sstevel@tonic-gate errno = save;
1217c478bd9Sstevel@tonic-gate return (-1);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate int
evTimeRW(evContext opaqueCtx,evStreamID id,evTimerID timer)1257c478bd9Sstevel@tonic-gate evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ {
1267c478bd9Sstevel@tonic-gate evStream *str = id.opaque;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate UNUSED(opaqueCtx);
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate str->timer = timer;
1317c478bd9Sstevel@tonic-gate str->flags |= EV_STR_TIMEROK;
1327c478bd9Sstevel@tonic-gate return (0);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate int
evUntimeRW(evContext opaqueCtx,evStreamID id)1367c478bd9Sstevel@tonic-gate evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ {
1377c478bd9Sstevel@tonic-gate evStream *str = id.opaque;
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate UNUSED(opaqueCtx);
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate str->flags &= ~EV_STR_TIMEROK;
1427c478bd9Sstevel@tonic-gate return (0);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate int
evCancelRW(evContext opaqueCtx,evStreamID id)1467c478bd9Sstevel@tonic-gate evCancelRW(evContext opaqueCtx, evStreamID id) {
1477c478bd9Sstevel@tonic-gate evContext_p *ctx = opaqueCtx.opaque;
1487c478bd9Sstevel@tonic-gate evStream *old = id.opaque;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate * The streams list is doubly threaded. First, there's ctx->streams
1527c478bd9Sstevel@tonic-gate * that's used by evDestroy() to find and cancel all streams. Second,
1537c478bd9Sstevel@tonic-gate * there's ctx->strDone (head) and ctx->strLast (tail) which thread
1547c478bd9Sstevel@tonic-gate * through the potentially smaller number of "IO completed" streams,
1557c478bd9Sstevel@tonic-gate * used in evGetNext() to avoid scanning the entire list.
1567c478bd9Sstevel@tonic-gate */
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate /* Unlink from ctx->streams. */
1597c478bd9Sstevel@tonic-gate if (old->prev != NULL)
1607c478bd9Sstevel@tonic-gate old->prev->next = old->next;
1617c478bd9Sstevel@tonic-gate else
1627c478bd9Sstevel@tonic-gate ctx->streams = old->next;
1637c478bd9Sstevel@tonic-gate if (old->next != NULL)
1647c478bd9Sstevel@tonic-gate old->next->prev = old->prev;
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate * If 'old' is on the ctx->strDone list, remove it. Update
1687c478bd9Sstevel@tonic-gate * ctx->strLast if necessary.
1697c478bd9Sstevel@tonic-gate */
1707c478bd9Sstevel@tonic-gate if (old->prevDone == NULL && old->nextDone == NULL) {
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate * Either 'old' is the only item on the done list, or it's
1737c478bd9Sstevel@tonic-gate * not on the done list. If the former, then we unlink it
1747c478bd9Sstevel@tonic-gate * from the list. If the latter, we leave the list alone.
1757c478bd9Sstevel@tonic-gate */
1767c478bd9Sstevel@tonic-gate if (ctx->strDone == old) {
1777c478bd9Sstevel@tonic-gate ctx->strDone = NULL;
1787c478bd9Sstevel@tonic-gate ctx->strLast = NULL;
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate } else {
1817c478bd9Sstevel@tonic-gate if (old->prevDone != NULL)
1827c478bd9Sstevel@tonic-gate old->prevDone->nextDone = old->nextDone;
1837c478bd9Sstevel@tonic-gate else
1847c478bd9Sstevel@tonic-gate ctx->strDone = old->nextDone;
1857c478bd9Sstevel@tonic-gate if (old->nextDone != NULL)
1867c478bd9Sstevel@tonic-gate old->nextDone->prevDone = old->prevDone;
1877c478bd9Sstevel@tonic-gate else
1887c478bd9Sstevel@tonic-gate ctx->strLast = old->prevDone;
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate /* Deallocate the stream. */
1927c478bd9Sstevel@tonic-gate if (old->file.opaque)
1937c478bd9Sstevel@tonic-gate evDeselectFD(opaqueCtx, old->file);
1947c478bd9Sstevel@tonic-gate memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount);
1957c478bd9Sstevel@tonic-gate FREE(old);
1967c478bd9Sstevel@tonic-gate return (0);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate /* Copy a scatter/gather vector and initialize a stream handler's IO. */
2007c478bd9Sstevel@tonic-gate static int
copyvec(evStream * str,const struct iovec * iov,int iocnt)2017c478bd9Sstevel@tonic-gate copyvec(evStream *str, const struct iovec *iov, int iocnt) {
2027c478bd9Sstevel@tonic-gate int i;
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt);
2057c478bd9Sstevel@tonic-gate if (str->iovOrig == NULL) {
2067c478bd9Sstevel@tonic-gate errno = ENOMEM;
2077c478bd9Sstevel@tonic-gate return (-1);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate str->ioTotal = 0;
2107c478bd9Sstevel@tonic-gate for (i = 0; i < iocnt; i++) {
2117c478bd9Sstevel@tonic-gate str->iovOrig[i] = iov[i];
2127c478bd9Sstevel@tonic-gate str->ioTotal += iov[i].iov_len;
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate str->iovOrigCount = iocnt;
2157c478bd9Sstevel@tonic-gate str->iovCur = str->iovOrig;
2167c478bd9Sstevel@tonic-gate str->iovCurCount = str->iovOrigCount;
2177c478bd9Sstevel@tonic-gate str->ioDone = 0;
2187c478bd9Sstevel@tonic-gate return (0);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate /* Pull off or truncate lead iovec(s). */
2227c478bd9Sstevel@tonic-gate static void
consume(evStream * str,size_t bytes)2237c478bd9Sstevel@tonic-gate consume(evStream *str, size_t bytes) {
224*9525b14bSRao Shoaib while (bytes > 0U) {
2257c478bd9Sstevel@tonic-gate if (bytes < (size_t)str->iovCur->iov_len) {
2267c478bd9Sstevel@tonic-gate str->iovCur->iov_len -= bytes;
2277c478bd9Sstevel@tonic-gate str->iovCur->iov_base = (void *)
2287c478bd9Sstevel@tonic-gate ((u_char *)str->iovCur->iov_base + bytes);
2297c478bd9Sstevel@tonic-gate str->ioDone += bytes;
2307c478bd9Sstevel@tonic-gate bytes = 0;
2317c478bd9Sstevel@tonic-gate } else {
2327c478bd9Sstevel@tonic-gate bytes -= str->iovCur->iov_len;
2337c478bd9Sstevel@tonic-gate str->ioDone += str->iovCur->iov_len;
2347c478bd9Sstevel@tonic-gate str->iovCur++;
2357c478bd9Sstevel@tonic-gate str->iovCurCount--;
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate /* Add a stream to Done list and deselect the FD. */
2417c478bd9Sstevel@tonic-gate static void
done(evContext opaqueCtx,evStream * str)2427c478bd9Sstevel@tonic-gate done(evContext opaqueCtx, evStream *str) {
2437c478bd9Sstevel@tonic-gate evContext_p *ctx = opaqueCtx.opaque;
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate if (ctx->strLast != NULL) {
2467c478bd9Sstevel@tonic-gate str->prevDone = ctx->strLast;
2477c478bd9Sstevel@tonic-gate ctx->strLast->nextDone = str;
2487c478bd9Sstevel@tonic-gate ctx->strLast = str;
2497c478bd9Sstevel@tonic-gate } else {
2507c478bd9Sstevel@tonic-gate INSIST(ctx->strDone == NULL);
2517c478bd9Sstevel@tonic-gate ctx->strDone = ctx->strLast = str;
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate evDeselectFD(opaqueCtx, str->file);
2547c478bd9Sstevel@tonic-gate str->file.opaque = NULL;
2557c478bd9Sstevel@tonic-gate /* evDrop() will call evCancelRW() on us. */
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate /* Dribble out some bytes on the stream. (Called by evDispatch().) */
2597c478bd9Sstevel@tonic-gate static void
writable(evContext opaqueCtx,void * uap,int fd,int evmask)2607c478bd9Sstevel@tonic-gate writable(evContext opaqueCtx, void *uap, int fd, int evmask) {
2617c478bd9Sstevel@tonic-gate evStream *str = uap;
2627c478bd9Sstevel@tonic-gate int bytes;
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate UNUSED(evmask);
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate bytes = writev(fd, str->iovCur, str->iovCurCount);
2677c478bd9Sstevel@tonic-gate if (bytes > 0) {
2687c478bd9Sstevel@tonic-gate if ((str->flags & EV_STR_TIMEROK) != 0)
2697c478bd9Sstevel@tonic-gate evTouchIdleTimer(opaqueCtx, str->timer);
2707c478bd9Sstevel@tonic-gate consume(str, bytes);
2717c478bd9Sstevel@tonic-gate } else {
2727c478bd9Sstevel@tonic-gate if (bytes < 0 && errno != EINTR) {
2737c478bd9Sstevel@tonic-gate str->ioDone = -1;
2747c478bd9Sstevel@tonic-gate str->ioErrno = errno;
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate if (str->ioDone == -1 || str->ioDone == str->ioTotal)
2787c478bd9Sstevel@tonic-gate done(opaqueCtx, str);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /* Scoop up some bytes from the stream. (Called by evDispatch().) */
2827c478bd9Sstevel@tonic-gate static void
readable(evContext opaqueCtx,void * uap,int fd,int evmask)2837c478bd9Sstevel@tonic-gate readable(evContext opaqueCtx, void *uap, int fd, int evmask) {
2847c478bd9Sstevel@tonic-gate evStream *str = uap;
2857c478bd9Sstevel@tonic-gate int bytes;
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate UNUSED(evmask);
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate bytes = readv(fd, str->iovCur, str->iovCurCount);
2907c478bd9Sstevel@tonic-gate if (bytes > 0) {
2917c478bd9Sstevel@tonic-gate if ((str->flags & EV_STR_TIMEROK) != 0)
2927c478bd9Sstevel@tonic-gate evTouchIdleTimer(opaqueCtx, str->timer);
2937c478bd9Sstevel@tonic-gate consume(str, bytes);
2947c478bd9Sstevel@tonic-gate } else {
2957c478bd9Sstevel@tonic-gate if (bytes == 0)
2967c478bd9Sstevel@tonic-gate str->ioDone = 0;
2977c478bd9Sstevel@tonic-gate else {
2987c478bd9Sstevel@tonic-gate if (errno != EINTR) {
2997c478bd9Sstevel@tonic-gate str->ioDone = -1;
3007c478bd9Sstevel@tonic-gate str->ioErrno = errno;
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate if (str->ioDone <= 0 || str->ioDone == str->ioTotal)
3057c478bd9Sstevel@tonic-gate done(opaqueCtx, str);
3067c478bd9Sstevel@tonic-gate }
307*9525b14bSRao Shoaib
308*9525b14bSRao Shoaib /*! \file */
309