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 #include "port_before.h"
237c478bd9Sstevel@tonic-gate #include "fd_setsize.h"
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/types.h>
267c478bd9Sstevel@tonic-gate #include <sys/uio.h>
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <errno.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <isc/eventlib.h>
317c478bd9Sstevel@tonic-gate #include <isc/assertions.h>
327c478bd9Sstevel@tonic-gate #include "eventlib_p.h"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include "port_after.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static int	copyvec(evStream *str, const struct iovec *iov, int iocnt);
377c478bd9Sstevel@tonic-gate static void	consume(evStream *str, size_t bytes);
387c478bd9Sstevel@tonic-gate static void	done(evContext opaqueCtx, evStream *str);
397c478bd9Sstevel@tonic-gate static void	writable(evContext opaqueCtx, void *uap, int fd, int evmask);
407c478bd9Sstevel@tonic-gate static void	readable(evContext opaqueCtx, void *uap, int fd, int evmask);
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate struct iovec
evConsIovec(void * buf,size_t cnt)437c478bd9Sstevel@tonic-gate evConsIovec(void *buf, size_t cnt) {
447c478bd9Sstevel@tonic-gate 	struct iovec ret;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	memset(&ret, 0xf5, sizeof ret);
477c478bd9Sstevel@tonic-gate 	ret.iov_base = buf;
487c478bd9Sstevel@tonic-gate 	ret.iov_len = cnt;
497c478bd9Sstevel@tonic-gate 	return (ret);
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate int
evWrite(evContext opaqueCtx,int fd,const struct iovec * iov,int iocnt,evStreamFunc func,void * uap,evStreamID * id)537c478bd9Sstevel@tonic-gate evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
547c478bd9Sstevel@tonic-gate 	evStreamFunc func, void *uap, evStreamID *id)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	evContext_p *ctx = opaqueCtx.opaque;
577c478bd9Sstevel@tonic-gate 	evStream *new;
587c478bd9Sstevel@tonic-gate 	int save;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	OKNEW(new);
617c478bd9Sstevel@tonic-gate 	new->func = func;
627c478bd9Sstevel@tonic-gate 	new->uap = uap;
637c478bd9Sstevel@tonic-gate 	new->fd = fd;
647c478bd9Sstevel@tonic-gate 	new->flags = 0;
657c478bd9Sstevel@tonic-gate 	if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0)
667c478bd9Sstevel@tonic-gate 		goto free;
677c478bd9Sstevel@tonic-gate 	if (copyvec(new, iov, iocnt) < 0)
687c478bd9Sstevel@tonic-gate 		goto free;
697c478bd9Sstevel@tonic-gate 	new->prevDone = NULL;
707c478bd9Sstevel@tonic-gate 	new->nextDone = NULL;
717c478bd9Sstevel@tonic-gate 	if (ctx->streams != NULL)
727c478bd9Sstevel@tonic-gate 		ctx->streams->prev = new;
737c478bd9Sstevel@tonic-gate 	new->prev = NULL;
747c478bd9Sstevel@tonic-gate 	new->next = ctx->streams;
757c478bd9Sstevel@tonic-gate 	ctx->streams = new;
767c478bd9Sstevel@tonic-gate 	if (id != NULL)
777c478bd9Sstevel@tonic-gate 		id->opaque = new;
787c478bd9Sstevel@tonic-gate 	return (0);
797c478bd9Sstevel@tonic-gate  free:
807c478bd9Sstevel@tonic-gate 	save = errno;
817c478bd9Sstevel@tonic-gate 	FREE(new);
827c478bd9Sstevel@tonic-gate 	errno = save;
837c478bd9Sstevel@tonic-gate 	return (-1);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate int
evRead(evContext opaqueCtx,int fd,const struct iovec * iov,int iocnt,evStreamFunc func,void * uap,evStreamID * id)877c478bd9Sstevel@tonic-gate evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
887c478bd9Sstevel@tonic-gate        evStreamFunc func, void *uap, evStreamID *id)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	evContext_p *ctx = opaqueCtx.opaque;
917c478bd9Sstevel@tonic-gate 	evStream *new;
927c478bd9Sstevel@tonic-gate 	int save;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	OKNEW(new);
957c478bd9Sstevel@tonic-gate 	new->func = func;
967c478bd9Sstevel@tonic-gate 	new->uap = uap;
977c478bd9Sstevel@tonic-gate 	new->fd = fd;
987c478bd9Sstevel@tonic-gate 	new->flags = 0;
997c478bd9Sstevel@tonic-gate 	if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0)
1007c478bd9Sstevel@tonic-gate 		goto free;
1017c478bd9Sstevel@tonic-gate 	if (copyvec(new, iov, iocnt) < 0)
1027c478bd9Sstevel@tonic-gate 		goto free;
1037c478bd9Sstevel@tonic-gate 	new->prevDone = NULL;
1047c478bd9Sstevel@tonic-gate 	new->nextDone = NULL;
1057c478bd9Sstevel@tonic-gate 	if (ctx->streams != NULL)
1067c478bd9Sstevel@tonic-gate 		ctx->streams->prev = new;
1077c478bd9Sstevel@tonic-gate 	new->prev = NULL;
1087c478bd9Sstevel@tonic-gate 	new->next = ctx->streams;
1097c478bd9Sstevel@tonic-gate 	ctx->streams = new;
1107c478bd9Sstevel@tonic-gate 	if (id)
1117c478bd9Sstevel@tonic-gate 		id->opaque = new;
1127c478bd9Sstevel@tonic-gate 	return (0);
1137c478bd9Sstevel@tonic-gate  free:
1147c478bd9Sstevel@tonic-gate 	save = errno;
1157c478bd9Sstevel@tonic-gate 	FREE(new);
1167c478bd9Sstevel@tonic-gate 	errno = save;
1177c478bd9Sstevel@tonic-gate 	return (-1);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate int
evTimeRW(evContext opaqueCtx,evStreamID id,evTimerID timer)1217c478bd9Sstevel@tonic-gate evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ {
1227c478bd9Sstevel@tonic-gate 	evStream *str = id.opaque;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	UNUSED(opaqueCtx);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	str->timer = timer;
1277c478bd9Sstevel@tonic-gate 	str->flags |= EV_STR_TIMEROK;
1287c478bd9Sstevel@tonic-gate 	return (0);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate int
evUntimeRW(evContext opaqueCtx,evStreamID id)1327c478bd9Sstevel@tonic-gate evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ {
1337c478bd9Sstevel@tonic-gate 	evStream *str = id.opaque;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	UNUSED(opaqueCtx);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	str->flags &= ~EV_STR_TIMEROK;
1387c478bd9Sstevel@tonic-gate 	return (0);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate int
evCancelRW(evContext opaqueCtx,evStreamID id)1427c478bd9Sstevel@tonic-gate evCancelRW(evContext opaqueCtx, evStreamID id) {
1437c478bd9Sstevel@tonic-gate 	evContext_p *ctx = opaqueCtx.opaque;
1447c478bd9Sstevel@tonic-gate 	evStream *old = id.opaque;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * The streams list is doubly threaded.  First, there's ctx->streams
1487c478bd9Sstevel@tonic-gate 	 * that's used by evDestroy() to find and cancel all streams.  Second,
1497c478bd9Sstevel@tonic-gate 	 * there's ctx->strDone (head) and ctx->strLast (tail) which thread
1507c478bd9Sstevel@tonic-gate 	 * through the potentially smaller number of "IO completed" streams,
1517c478bd9Sstevel@tonic-gate 	 * used in evGetNext() to avoid scanning the entire list.
1527c478bd9Sstevel@tonic-gate 	 */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/* Unlink from ctx->streams. */
1557c478bd9Sstevel@tonic-gate 	if (old->prev != NULL)
1567c478bd9Sstevel@tonic-gate 		old->prev->next = old->next;
1577c478bd9Sstevel@tonic-gate 	else
1587c478bd9Sstevel@tonic-gate 		ctx->streams = old->next;
1597c478bd9Sstevel@tonic-gate 	if (old->next != NULL)
1607c478bd9Sstevel@tonic-gate 		old->next->prev = old->prev;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/*
1637c478bd9Sstevel@tonic-gate 	 * If 'old' is on the ctx->strDone list, remove it.  Update
1647c478bd9Sstevel@tonic-gate 	 * ctx->strLast if necessary.
1657c478bd9Sstevel@tonic-gate 	 */
1667c478bd9Sstevel@tonic-gate 	if (old->prevDone == NULL && old->nextDone == NULL) {
1677c478bd9Sstevel@tonic-gate 		/*
1687c478bd9Sstevel@tonic-gate 		 * Either 'old' is the only item on the done list, or it's
1697c478bd9Sstevel@tonic-gate 		 * not on the done list.  If the former, then we unlink it
1707c478bd9Sstevel@tonic-gate 		 * from the list.  If the latter, we leave the list alone.
1717c478bd9Sstevel@tonic-gate 		 */
1727c478bd9Sstevel@tonic-gate 		if (ctx->strDone == old) {
1737c478bd9Sstevel@tonic-gate 			ctx->strDone = NULL;
1747c478bd9Sstevel@tonic-gate 			ctx->strLast = NULL;
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 	} else {
1777c478bd9Sstevel@tonic-gate 		if (old->prevDone != NULL)
1787c478bd9Sstevel@tonic-gate 			old->prevDone->nextDone = old->nextDone;
1797c478bd9Sstevel@tonic-gate 		else
1807c478bd9Sstevel@tonic-gate 			ctx->strDone = old->nextDone;
1817c478bd9Sstevel@tonic-gate 		if (old->nextDone != NULL)
1827c478bd9Sstevel@tonic-gate 			old->nextDone->prevDone = old->prevDone;
1837c478bd9Sstevel@tonic-gate 		else
1847c478bd9Sstevel@tonic-gate 			ctx->strLast = old->prevDone;
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/* Deallocate the stream. */
1887c478bd9Sstevel@tonic-gate 	if (old->file.opaque)
1897c478bd9Sstevel@tonic-gate 		evDeselectFD(opaqueCtx, old->file);
1907c478bd9Sstevel@tonic-gate 	memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount);
1917c478bd9Sstevel@tonic-gate 	FREE(old);
1927c478bd9Sstevel@tonic-gate 	return (0);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /* Copy a scatter/gather vector and initialize a stream handler's IO. */
1967c478bd9Sstevel@tonic-gate static int
copyvec(evStream * str,const struct iovec * iov,int iocnt)1977c478bd9Sstevel@tonic-gate copyvec(evStream *str, const struct iovec *iov, int iocnt) {
1987c478bd9Sstevel@tonic-gate 	int i;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt);
2017c478bd9Sstevel@tonic-gate 	if (str->iovOrig == NULL) {
2027c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
2037c478bd9Sstevel@tonic-gate 		return (-1);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	str->ioTotal = 0;
2067c478bd9Sstevel@tonic-gate 	for (i = 0; i < iocnt; i++) {
2077c478bd9Sstevel@tonic-gate 		str->iovOrig[i] = iov[i];
2087c478bd9Sstevel@tonic-gate 		str->ioTotal += iov[i].iov_len;
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	str->iovOrigCount = iocnt;
2117c478bd9Sstevel@tonic-gate 	str->iovCur = str->iovOrig;
2127c478bd9Sstevel@tonic-gate 	str->iovCurCount = str->iovOrigCount;
2137c478bd9Sstevel@tonic-gate 	str->ioDone = 0;
2147c478bd9Sstevel@tonic-gate 	return (0);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /* Pull off or truncate lead iovec(s). */
2187c478bd9Sstevel@tonic-gate static void
consume(evStream * str,size_t bytes)2197c478bd9Sstevel@tonic-gate consume(evStream *str, size_t bytes) {
220*9525b14bSRao Shoaib 	while (bytes > 0U) {
2217c478bd9Sstevel@tonic-gate 		if (bytes < (size_t)str->iovCur->iov_len) {
2227c478bd9Sstevel@tonic-gate 			str->iovCur->iov_len -= bytes;
2237c478bd9Sstevel@tonic-gate 			str->iovCur->iov_base = (void *)
2247c478bd9Sstevel@tonic-gate 				((u_char *)str->iovCur->iov_base + bytes);
2257c478bd9Sstevel@tonic-gate 			str->ioDone += bytes;
2267c478bd9Sstevel@tonic-gate 			bytes = 0;
2277c478bd9Sstevel@tonic-gate 		} else {
2287c478bd9Sstevel@tonic-gate 			bytes -= str->iovCur->iov_len;
2297c478bd9Sstevel@tonic-gate 			str->ioDone += str->iovCur->iov_len;
2307c478bd9Sstevel@tonic-gate 			str->iovCur++;
2317c478bd9Sstevel@tonic-gate 			str->iovCurCount--;
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /* Add a stream to Done list and deselect the FD. */
2377c478bd9Sstevel@tonic-gate static void
done(evContext opaqueCtx,evStream * str)2387c478bd9Sstevel@tonic-gate done(evContext opaqueCtx, evStream *str) {
2397c478bd9Sstevel@tonic-gate 	evContext_p *ctx = opaqueCtx.opaque;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (ctx->strLast != NULL) {
2427c478bd9Sstevel@tonic-gate 		str->prevDone = ctx->strLast;
2437c478bd9Sstevel@tonic-gate 		ctx->strLast->nextDone = str;
2447c478bd9Sstevel@tonic-gate 		ctx->strLast = str;
2457c478bd9Sstevel@tonic-gate 	} else {
2467c478bd9Sstevel@tonic-gate 		INSIST(ctx->strDone == NULL);
2477c478bd9Sstevel@tonic-gate 		ctx->strDone = ctx->strLast = str;
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	evDeselectFD(opaqueCtx, str->file);
2507c478bd9Sstevel@tonic-gate 	str->file.opaque = NULL;
2517c478bd9Sstevel@tonic-gate 	/* evDrop() will call evCancelRW() on us. */
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /* Dribble out some bytes on the stream.  (Called by evDispatch().) */
2557c478bd9Sstevel@tonic-gate static void
writable(evContext opaqueCtx,void * uap,int fd,int evmask)2567c478bd9Sstevel@tonic-gate writable(evContext opaqueCtx, void *uap, int fd, int evmask) {
2577c478bd9Sstevel@tonic-gate 	evStream *str = uap;
2587c478bd9Sstevel@tonic-gate 	int bytes;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	UNUSED(evmask);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	bytes = writev(fd, str->iovCur, str->iovCurCount);
2637c478bd9Sstevel@tonic-gate 	if (bytes > 0) {
2647c478bd9Sstevel@tonic-gate 		if ((str->flags & EV_STR_TIMEROK) != 0)
2657c478bd9Sstevel@tonic-gate 			evTouchIdleTimer(opaqueCtx, str->timer);
2667c478bd9Sstevel@tonic-gate 		consume(str, bytes);
2677c478bd9Sstevel@tonic-gate 	} else {
2687c478bd9Sstevel@tonic-gate 		if (bytes < 0 && errno != EINTR) {
2697c478bd9Sstevel@tonic-gate 			str->ioDone = -1;
2707c478bd9Sstevel@tonic-gate 			str->ioErrno = errno;
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 	if (str->ioDone == -1 || str->ioDone == str->ioTotal)
2747c478bd9Sstevel@tonic-gate 		done(opaqueCtx, str);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /* Scoop up some bytes from the stream.  (Called by evDispatch().) */
2787c478bd9Sstevel@tonic-gate static void
readable(evContext opaqueCtx,void * uap,int fd,int evmask)2797c478bd9Sstevel@tonic-gate readable(evContext opaqueCtx, void *uap, int fd, int evmask) {
2807c478bd9Sstevel@tonic-gate 	evStream *str = uap;
2817c478bd9Sstevel@tonic-gate 	int bytes;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	UNUSED(evmask);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	bytes = readv(fd, str->iovCur, str->iovCurCount);
2867c478bd9Sstevel@tonic-gate 	if (bytes > 0) {
2877c478bd9Sstevel@tonic-gate 		if ((str->flags & EV_STR_TIMEROK) != 0)
2887c478bd9Sstevel@tonic-gate 			evTouchIdleTimer(opaqueCtx, str->timer);
2897c478bd9Sstevel@tonic-gate 		consume(str, bytes);
2907c478bd9Sstevel@tonic-gate 	} else {
2917c478bd9Sstevel@tonic-gate 		if (bytes == 0)
2927c478bd9Sstevel@tonic-gate 			str->ioDone = 0;
2937c478bd9Sstevel@tonic-gate 		else {
2947c478bd9Sstevel@tonic-gate 			if (errno != EINTR) {
2957c478bd9Sstevel@tonic-gate 				str->ioDone = -1;
2967c478bd9Sstevel@tonic-gate 				str->ioErrno = errno;
2977c478bd9Sstevel@tonic-gate 			}
2987c478bd9Sstevel@tonic-gate 		}
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	if (str->ioDone <= 0 || str->ioDone == str->ioTotal)
3017c478bd9Sstevel@tonic-gate 		done(opaqueCtx, str);
3027c478bd9Sstevel@tonic-gate }
303*9525b14bSRao Shoaib 
304*9525b14bSRao Shoaib /*! \file */
305