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