xref: /freebsd/lib/libc/isc/ev_streams.c (revision 65e96449c14c00e862c3664803ead4b8aa9ba4b7)
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)
2365e96449SHajimu UMEMOTO static const char rcsid[] = "$Id: ev_streams.c,v 1.2.206.2 2004/03/17 00:29:51 marka Exp $";
2465e96449SHajimu UMEMOTO #endif
2565e96449SHajimu UMEMOTO 
2665e96449SHajimu UMEMOTO #include "port_before.h"
2765e96449SHajimu UMEMOTO #include "fd_setsize.h"
2865e96449SHajimu UMEMOTO 
2965e96449SHajimu UMEMOTO #include <sys/types.h>
3065e96449SHajimu UMEMOTO #include <sys/uio.h>
3165e96449SHajimu UMEMOTO 
3265e96449SHajimu UMEMOTO #include <errno.h>
3365e96449SHajimu UMEMOTO 
3465e96449SHajimu UMEMOTO #include <isc/eventlib.h>
3565e96449SHajimu UMEMOTO #include <isc/assertions.h>
3665e96449SHajimu UMEMOTO #include "eventlib_p.h"
3765e96449SHajimu UMEMOTO 
3865e96449SHajimu UMEMOTO #include "port_after.h"
3965e96449SHajimu UMEMOTO 
4065e96449SHajimu UMEMOTO static int	copyvec(evStream *str, const struct iovec *iov, int iocnt);
4165e96449SHajimu UMEMOTO static void	consume(evStream *str, size_t bytes);
4265e96449SHajimu UMEMOTO static void	done(evContext opaqueCtx, evStream *str);
4365e96449SHajimu UMEMOTO static void	writable(evContext opaqueCtx, void *uap, int fd, int evmask);
4465e96449SHajimu UMEMOTO static void	readable(evContext opaqueCtx, void *uap, int fd, int evmask);
4565e96449SHajimu UMEMOTO 
4665e96449SHajimu UMEMOTO struct iovec
4765e96449SHajimu UMEMOTO evConsIovec(void *buf, size_t cnt) {
4865e96449SHajimu UMEMOTO 	struct iovec ret;
4965e96449SHajimu UMEMOTO 
5065e96449SHajimu UMEMOTO 	memset(&ret, 0xf5, sizeof ret);
5165e96449SHajimu UMEMOTO 	ret.iov_base = buf;
5265e96449SHajimu UMEMOTO 	ret.iov_len = cnt;
5365e96449SHajimu UMEMOTO 	return (ret);
5465e96449SHajimu UMEMOTO }
5565e96449SHajimu UMEMOTO 
5665e96449SHajimu UMEMOTO int
5765e96449SHajimu UMEMOTO evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
5865e96449SHajimu UMEMOTO 	evStreamFunc func, void *uap, evStreamID *id)
5965e96449SHajimu UMEMOTO {
6065e96449SHajimu UMEMOTO 	evContext_p *ctx = opaqueCtx.opaque;
6165e96449SHajimu UMEMOTO 	evStream *new;
6265e96449SHajimu UMEMOTO 	int save;
6365e96449SHajimu UMEMOTO 
6465e96449SHajimu UMEMOTO 	OKNEW(new);
6565e96449SHajimu UMEMOTO 	new->func = func;
6665e96449SHajimu UMEMOTO 	new->uap = uap;
6765e96449SHajimu UMEMOTO 	new->fd = fd;
6865e96449SHajimu UMEMOTO 	new->flags = 0;
6965e96449SHajimu UMEMOTO 	if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0)
7065e96449SHajimu UMEMOTO 		goto free;
7165e96449SHajimu UMEMOTO 	if (copyvec(new, iov, iocnt) < 0)
7265e96449SHajimu UMEMOTO 		goto free;
7365e96449SHajimu UMEMOTO 	new->prevDone = NULL;
7465e96449SHajimu UMEMOTO 	new->nextDone = NULL;
7565e96449SHajimu UMEMOTO 	if (ctx->streams != NULL)
7665e96449SHajimu UMEMOTO 		ctx->streams->prev = new;
7765e96449SHajimu UMEMOTO 	new->prev = NULL;
7865e96449SHajimu UMEMOTO 	new->next = ctx->streams;
7965e96449SHajimu UMEMOTO 	ctx->streams = new;
8065e96449SHajimu UMEMOTO 	if (id != NULL)
8165e96449SHajimu UMEMOTO 		id->opaque = new;
8265e96449SHajimu UMEMOTO 	return (0);
8365e96449SHajimu UMEMOTO  free:
8465e96449SHajimu UMEMOTO 	save = errno;
8565e96449SHajimu UMEMOTO 	FREE(new);
8665e96449SHajimu UMEMOTO 	errno = save;
8765e96449SHajimu UMEMOTO 	return (-1);
8865e96449SHajimu UMEMOTO }
8965e96449SHajimu UMEMOTO 
9065e96449SHajimu UMEMOTO int
9165e96449SHajimu UMEMOTO evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
9265e96449SHajimu UMEMOTO        evStreamFunc func, void *uap, evStreamID *id)
9365e96449SHajimu UMEMOTO {
9465e96449SHajimu UMEMOTO 	evContext_p *ctx = opaqueCtx.opaque;
9565e96449SHajimu UMEMOTO 	evStream *new;
9665e96449SHajimu UMEMOTO 	int save;
9765e96449SHajimu UMEMOTO 
9865e96449SHajimu UMEMOTO 	OKNEW(new);
9965e96449SHajimu UMEMOTO 	new->func = func;
10065e96449SHajimu UMEMOTO 	new->uap = uap;
10165e96449SHajimu UMEMOTO 	new->fd = fd;
10265e96449SHajimu UMEMOTO 	new->flags = 0;
10365e96449SHajimu UMEMOTO 	if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0)
10465e96449SHajimu UMEMOTO 		goto free;
10565e96449SHajimu UMEMOTO 	if (copyvec(new, iov, iocnt) < 0)
10665e96449SHajimu UMEMOTO 		goto free;
10765e96449SHajimu UMEMOTO 	new->prevDone = NULL;
10865e96449SHajimu UMEMOTO 	new->nextDone = NULL;
10965e96449SHajimu UMEMOTO 	if (ctx->streams != NULL)
11065e96449SHajimu UMEMOTO 		ctx->streams->prev = new;
11165e96449SHajimu UMEMOTO 	new->prev = NULL;
11265e96449SHajimu UMEMOTO 	new->next = ctx->streams;
11365e96449SHajimu UMEMOTO 	ctx->streams = new;
11465e96449SHajimu UMEMOTO 	if (id)
11565e96449SHajimu UMEMOTO 		id->opaque = new;
11665e96449SHajimu UMEMOTO 	return (0);
11765e96449SHajimu UMEMOTO  free:
11865e96449SHajimu UMEMOTO 	save = errno;
11965e96449SHajimu UMEMOTO 	FREE(new);
12065e96449SHajimu UMEMOTO 	errno = save;
12165e96449SHajimu UMEMOTO 	return (-1);
12265e96449SHajimu UMEMOTO }
12365e96449SHajimu UMEMOTO 
12465e96449SHajimu UMEMOTO int
12565e96449SHajimu UMEMOTO evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ {
12665e96449SHajimu UMEMOTO 	evStream *str = id.opaque;
12765e96449SHajimu UMEMOTO 
12865e96449SHajimu UMEMOTO 	UNUSED(opaqueCtx);
12965e96449SHajimu UMEMOTO 
13065e96449SHajimu UMEMOTO 	str->timer = timer;
13165e96449SHajimu UMEMOTO 	str->flags |= EV_STR_TIMEROK;
13265e96449SHajimu UMEMOTO 	return (0);
13365e96449SHajimu UMEMOTO }
13465e96449SHajimu UMEMOTO 
13565e96449SHajimu UMEMOTO int
13665e96449SHajimu UMEMOTO evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ {
13765e96449SHajimu UMEMOTO 	evStream *str = id.opaque;
13865e96449SHajimu UMEMOTO 
13965e96449SHajimu UMEMOTO 	UNUSED(opaqueCtx);
14065e96449SHajimu UMEMOTO 
14165e96449SHajimu UMEMOTO 	str->flags &= ~EV_STR_TIMEROK;
14265e96449SHajimu UMEMOTO 	return (0);
14365e96449SHajimu UMEMOTO }
14465e96449SHajimu UMEMOTO 
14565e96449SHajimu UMEMOTO int
14665e96449SHajimu UMEMOTO evCancelRW(evContext opaqueCtx, evStreamID id) {
14765e96449SHajimu UMEMOTO 	evContext_p *ctx = opaqueCtx.opaque;
14865e96449SHajimu UMEMOTO 	evStream *old = id.opaque;
14965e96449SHajimu UMEMOTO 
15065e96449SHajimu UMEMOTO 	/*
15165e96449SHajimu UMEMOTO 	 * The streams list is doubly threaded.  First, there's ctx->streams
15265e96449SHajimu UMEMOTO 	 * that's used by evDestroy() to find and cancel all streams.  Second,
15365e96449SHajimu UMEMOTO 	 * there's ctx->strDone (head) and ctx->strLast (tail) which thread
15465e96449SHajimu UMEMOTO 	 * through the potentially smaller number of "IO completed" streams,
15565e96449SHajimu UMEMOTO 	 * used in evGetNext() to avoid scanning the entire list.
15665e96449SHajimu UMEMOTO 	 */
15765e96449SHajimu UMEMOTO 
15865e96449SHajimu UMEMOTO 	/* Unlink from ctx->streams. */
15965e96449SHajimu UMEMOTO 	if (old->prev != NULL)
16065e96449SHajimu UMEMOTO 		old->prev->next = old->next;
16165e96449SHajimu UMEMOTO 	else
16265e96449SHajimu UMEMOTO 		ctx->streams = old->next;
16365e96449SHajimu UMEMOTO 	if (old->next != NULL)
16465e96449SHajimu UMEMOTO 		old->next->prev = old->prev;
16565e96449SHajimu UMEMOTO 
16665e96449SHajimu UMEMOTO 	/*
16765e96449SHajimu UMEMOTO 	 * If 'old' is on the ctx->strDone list, remove it.  Update
16865e96449SHajimu UMEMOTO 	 * ctx->strLast if necessary.
16965e96449SHajimu UMEMOTO 	 */
17065e96449SHajimu UMEMOTO 	if (old->prevDone == NULL && old->nextDone == NULL) {
17165e96449SHajimu UMEMOTO 		/*
17265e96449SHajimu UMEMOTO 		 * Either 'old' is the only item on the done list, or it's
17365e96449SHajimu UMEMOTO 		 * not on the done list.  If the former, then we unlink it
17465e96449SHajimu UMEMOTO 		 * from the list.  If the latter, we leave the list alone.
17565e96449SHajimu UMEMOTO 		 */
17665e96449SHajimu UMEMOTO 		if (ctx->strDone == old) {
17765e96449SHajimu UMEMOTO 			ctx->strDone = NULL;
17865e96449SHajimu UMEMOTO 			ctx->strLast = NULL;
17965e96449SHajimu UMEMOTO 		}
18065e96449SHajimu UMEMOTO 	} else {
18165e96449SHajimu UMEMOTO 		if (old->prevDone != NULL)
18265e96449SHajimu UMEMOTO 			old->prevDone->nextDone = old->nextDone;
18365e96449SHajimu UMEMOTO 		else
18465e96449SHajimu UMEMOTO 			ctx->strDone = old->nextDone;
18565e96449SHajimu UMEMOTO 		if (old->nextDone != NULL)
18665e96449SHajimu UMEMOTO 			old->nextDone->prevDone = old->prevDone;
18765e96449SHajimu UMEMOTO 		else
18865e96449SHajimu UMEMOTO 			ctx->strLast = old->prevDone;
18965e96449SHajimu UMEMOTO 	}
19065e96449SHajimu UMEMOTO 
19165e96449SHajimu UMEMOTO 	/* Deallocate the stream. */
19265e96449SHajimu UMEMOTO 	if (old->file.opaque)
19365e96449SHajimu UMEMOTO 		evDeselectFD(opaqueCtx, old->file);
19465e96449SHajimu UMEMOTO 	memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount);
19565e96449SHajimu UMEMOTO 	FREE(old);
19665e96449SHajimu UMEMOTO 	return (0);
19765e96449SHajimu UMEMOTO }
19865e96449SHajimu UMEMOTO 
19965e96449SHajimu UMEMOTO /* Copy a scatter/gather vector and initialize a stream handler's IO. */
20065e96449SHajimu UMEMOTO static int
20165e96449SHajimu UMEMOTO copyvec(evStream *str, const struct iovec *iov, int iocnt) {
20265e96449SHajimu UMEMOTO 	int i;
20365e96449SHajimu UMEMOTO 
20465e96449SHajimu UMEMOTO 	str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt);
20565e96449SHajimu UMEMOTO 	if (str->iovOrig == NULL) {
20665e96449SHajimu UMEMOTO 		errno = ENOMEM;
20765e96449SHajimu UMEMOTO 		return (-1);
20865e96449SHajimu UMEMOTO 	}
20965e96449SHajimu UMEMOTO 	str->ioTotal = 0;
21065e96449SHajimu UMEMOTO 	for (i = 0; i < iocnt; i++) {
21165e96449SHajimu UMEMOTO 		str->iovOrig[i] = iov[i];
21265e96449SHajimu UMEMOTO 		str->ioTotal += iov[i].iov_len;
21365e96449SHajimu UMEMOTO 	}
21465e96449SHajimu UMEMOTO 	str->iovOrigCount = iocnt;
21565e96449SHajimu UMEMOTO 	str->iovCur = str->iovOrig;
21665e96449SHajimu UMEMOTO 	str->iovCurCount = str->iovOrigCount;
21765e96449SHajimu UMEMOTO 	str->ioDone = 0;
21865e96449SHajimu UMEMOTO 	return (0);
21965e96449SHajimu UMEMOTO }
22065e96449SHajimu UMEMOTO 
22165e96449SHajimu UMEMOTO /* Pull off or truncate lead iovec(s). */
22265e96449SHajimu UMEMOTO static void
22365e96449SHajimu UMEMOTO consume(evStream *str, size_t bytes) {
22465e96449SHajimu UMEMOTO 	while (bytes > 0U) {
22565e96449SHajimu UMEMOTO 		if (bytes < (size_t)str->iovCur->iov_len) {
22665e96449SHajimu UMEMOTO 			str->iovCur->iov_len -= bytes;
22765e96449SHajimu UMEMOTO 			str->iovCur->iov_base = (void *)
22865e96449SHajimu UMEMOTO 				((u_char *)str->iovCur->iov_base + bytes);
22965e96449SHajimu UMEMOTO 			str->ioDone += bytes;
23065e96449SHajimu UMEMOTO 			bytes = 0;
23165e96449SHajimu UMEMOTO 		} else {
23265e96449SHajimu UMEMOTO 			bytes -= str->iovCur->iov_len;
23365e96449SHajimu UMEMOTO 			str->ioDone += str->iovCur->iov_len;
23465e96449SHajimu UMEMOTO 			str->iovCur++;
23565e96449SHajimu UMEMOTO 			str->iovCurCount--;
23665e96449SHajimu UMEMOTO 		}
23765e96449SHajimu UMEMOTO 	}
23865e96449SHajimu UMEMOTO }
23965e96449SHajimu UMEMOTO 
24065e96449SHajimu UMEMOTO /* Add a stream to Done list and deselect the FD. */
24165e96449SHajimu UMEMOTO static void
24265e96449SHajimu UMEMOTO done(evContext opaqueCtx, evStream *str) {
24365e96449SHajimu UMEMOTO 	evContext_p *ctx = opaqueCtx.opaque;
24465e96449SHajimu UMEMOTO 
24565e96449SHajimu UMEMOTO 	if (ctx->strLast != NULL) {
24665e96449SHajimu UMEMOTO 		str->prevDone = ctx->strLast;
24765e96449SHajimu UMEMOTO 		ctx->strLast->nextDone = str;
24865e96449SHajimu UMEMOTO 		ctx->strLast = str;
24965e96449SHajimu UMEMOTO 	} else {
25065e96449SHajimu UMEMOTO 		INSIST(ctx->strDone == NULL);
25165e96449SHajimu UMEMOTO 		ctx->strDone = ctx->strLast = str;
25265e96449SHajimu UMEMOTO 	}
25365e96449SHajimu UMEMOTO 	evDeselectFD(opaqueCtx, str->file);
25465e96449SHajimu UMEMOTO 	str->file.opaque = NULL;
25565e96449SHajimu UMEMOTO 	/* evDrop() will call evCancelRW() on us. */
25665e96449SHajimu UMEMOTO }
25765e96449SHajimu UMEMOTO 
25865e96449SHajimu UMEMOTO /* Dribble out some bytes on the stream.  (Called by evDispatch().) */
25965e96449SHajimu UMEMOTO static void
26065e96449SHajimu UMEMOTO writable(evContext opaqueCtx, void *uap, int fd, int evmask) {
26165e96449SHajimu UMEMOTO 	evStream *str = uap;
26265e96449SHajimu UMEMOTO 	int bytes;
26365e96449SHajimu UMEMOTO 
26465e96449SHajimu UMEMOTO 	UNUSED(evmask);
26565e96449SHajimu UMEMOTO 
26665e96449SHajimu UMEMOTO 	bytes = writev(fd, str->iovCur, str->iovCurCount);
26765e96449SHajimu UMEMOTO 	if (bytes > 0) {
26865e96449SHajimu UMEMOTO 		if ((str->flags & EV_STR_TIMEROK) != 0)
26965e96449SHajimu UMEMOTO 			evTouchIdleTimer(opaqueCtx, str->timer);
27065e96449SHajimu UMEMOTO 		consume(str, bytes);
27165e96449SHajimu UMEMOTO 	} else {
27265e96449SHajimu UMEMOTO 		if (bytes < 0 && errno != EINTR) {
27365e96449SHajimu UMEMOTO 			str->ioDone = -1;
27465e96449SHajimu UMEMOTO 			str->ioErrno = errno;
27565e96449SHajimu UMEMOTO 		}
27665e96449SHajimu UMEMOTO 	}
27765e96449SHajimu UMEMOTO 	if (str->ioDone == -1 || str->ioDone == str->ioTotal)
27865e96449SHajimu UMEMOTO 		done(opaqueCtx, str);
27965e96449SHajimu UMEMOTO }
28065e96449SHajimu UMEMOTO 
28165e96449SHajimu UMEMOTO /* Scoop up some bytes from the stream.  (Called by evDispatch().) */
28265e96449SHajimu UMEMOTO static void
28365e96449SHajimu UMEMOTO readable(evContext opaqueCtx, void *uap, int fd, int evmask) {
28465e96449SHajimu UMEMOTO 	evStream *str = uap;
28565e96449SHajimu UMEMOTO 	int bytes;
28665e96449SHajimu UMEMOTO 
28765e96449SHajimu UMEMOTO 	UNUSED(evmask);
28865e96449SHajimu UMEMOTO 
28965e96449SHajimu UMEMOTO 	bytes = readv(fd, str->iovCur, str->iovCurCount);
29065e96449SHajimu UMEMOTO 	if (bytes > 0) {
29165e96449SHajimu UMEMOTO 		if ((str->flags & EV_STR_TIMEROK) != 0)
29265e96449SHajimu UMEMOTO 			evTouchIdleTimer(opaqueCtx, str->timer);
29365e96449SHajimu UMEMOTO 		consume(str, bytes);
29465e96449SHajimu UMEMOTO 	} else {
29565e96449SHajimu UMEMOTO 		if (bytes == 0)
29665e96449SHajimu UMEMOTO 			str->ioDone = 0;
29765e96449SHajimu UMEMOTO 		else {
29865e96449SHajimu UMEMOTO 			if (errno != EINTR) {
29965e96449SHajimu UMEMOTO 				str->ioDone = -1;
30065e96449SHajimu UMEMOTO 				str->ioErrno = errno;
30165e96449SHajimu UMEMOTO 			}
30265e96449SHajimu UMEMOTO 		}
30365e96449SHajimu UMEMOTO 	}
30465e96449SHajimu UMEMOTO 	if (str->ioDone <= 0 || str->ioDone == str->ioTotal)
30565e96449SHajimu UMEMOTO 		done(opaqueCtx, str);
30665e96449SHajimu UMEMOTO }
307