xref: /dflybsd-src/lib/libc/isc/ev_streams.c (revision fbfb85d2836918c8381a60d528f004e7f0bbbe31)
1ee65b806SJan Lentfer /*
2ee65b806SJan Lentfer  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3ee65b806SJan Lentfer  * Copyright (c) 1996-1999 by Internet Software Consortium
4ee65b806SJan Lentfer  *
5ee65b806SJan Lentfer  * Permission to use, copy, modify, and distribute this software for any
6ee65b806SJan Lentfer  * purpose with or without fee is hereby granted, provided that the above
7ee65b806SJan Lentfer  * copyright notice and this permission notice appear in all copies.
8ee65b806SJan Lentfer  *
9ee65b806SJan Lentfer  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10ee65b806SJan Lentfer  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11ee65b806SJan Lentfer  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12ee65b806SJan Lentfer  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13ee65b806SJan Lentfer  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14ee65b806SJan Lentfer  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15ee65b806SJan Lentfer  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*fbfb85d2SSascha Wildner  *
17*fbfb85d2SSascha Wildner  * $Id: ev_streams.c,v 1.5 2005/04/27 04:56:36 sra Exp $
18ee65b806SJan Lentfer  */
19ee65b806SJan Lentfer 
20ee65b806SJan Lentfer /* ev_streams.c - implement asynch stream file IO for the eventlib
21ee65b806SJan Lentfer  * vix 04mar96 [initial]
22ee65b806SJan Lentfer  */
23ee65b806SJan Lentfer 
24ee65b806SJan Lentfer #include "port_before.h"
25ee65b806SJan Lentfer #ifndef _LIBC
26ee65b806SJan Lentfer #include "fd_setsize.h"
27ee65b806SJan Lentfer #endif
28ee65b806SJan Lentfer 
29ee65b806SJan Lentfer #include <sys/types.h>
30ee65b806SJan Lentfer #include <sys/uio.h>
31ee65b806SJan Lentfer 
32ee65b806SJan Lentfer #include <errno.h>
33ee65b806SJan Lentfer 
34ee65b806SJan Lentfer #include "isc/eventlib.h"
35ee65b806SJan Lentfer #ifndef _LIBC
36ee65b806SJan Lentfer #include <isc/assertions.h>
37ee65b806SJan Lentfer #endif
38ee65b806SJan Lentfer #include "eventlib_p.h"
39ee65b806SJan Lentfer 
40ee65b806SJan Lentfer #include "port_after.h"
41ee65b806SJan Lentfer 
42ee65b806SJan Lentfer #ifndef _LIBC
43ee65b806SJan Lentfer static int	copyvec(evStream *str, const struct iovec *iov, int iocnt);
44ee65b806SJan Lentfer static void	consume(evStream *str, size_t bytes);
45ee65b806SJan Lentfer static void	done(evContext opaqueCtx, evStream *str);
46ee65b806SJan Lentfer static void	writable(evContext opaqueCtx, void *uap, int fd, int evmask);
47ee65b806SJan Lentfer static void	readable(evContext opaqueCtx, void *uap, int fd, int evmask);
48ee65b806SJan Lentfer #endif
49ee65b806SJan Lentfer 
50ee65b806SJan Lentfer struct iovec
evConsIovec(void * buf,size_t cnt)51ee65b806SJan Lentfer evConsIovec(void *buf, size_t cnt) {
52ee65b806SJan Lentfer 	struct iovec ret;
53ee65b806SJan Lentfer 
54ee65b806SJan Lentfer 	memset(&ret, 0xf5, sizeof ret);
55ee65b806SJan Lentfer 	ret.iov_base = buf;
56ee65b806SJan Lentfer 	ret.iov_len = cnt;
57ee65b806SJan Lentfer 	return (ret);
58ee65b806SJan Lentfer }
59ee65b806SJan Lentfer 
60ee65b806SJan Lentfer #ifndef _LIBC
61ee65b806SJan Lentfer int
evWrite(evContext opaqueCtx,int fd,const struct iovec * iov,int iocnt,evStreamFunc func,void * uap,evStreamID * id)62ee65b806SJan Lentfer evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
63ee65b806SJan Lentfer 	evStreamFunc func, void *uap, evStreamID *id)
64ee65b806SJan Lentfer {
65ee65b806SJan Lentfer 	evContext_p *ctx = opaqueCtx.opaque;
66ee65b806SJan Lentfer 	evStream *new;
67ee65b806SJan Lentfer 	int save;
68ee65b806SJan Lentfer 
69ee65b806SJan Lentfer 	OKNEW(new);
70ee65b806SJan Lentfer 	new->func = func;
71ee65b806SJan Lentfer 	new->uap = uap;
72ee65b806SJan Lentfer 	new->fd = fd;
73ee65b806SJan Lentfer 	new->flags = 0;
74ee65b806SJan Lentfer 	if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0)
75ee65b806SJan Lentfer 		goto free;
76ee65b806SJan Lentfer 	if (copyvec(new, iov, iocnt) < 0)
77ee65b806SJan Lentfer 		goto free;
78ee65b806SJan Lentfer 	new->prevDone = NULL;
79ee65b806SJan Lentfer 	new->nextDone = NULL;
80ee65b806SJan Lentfer 	if (ctx->streams != NULL)
81ee65b806SJan Lentfer 		ctx->streams->prev = new;
82ee65b806SJan Lentfer 	new->prev = NULL;
83ee65b806SJan Lentfer 	new->next = ctx->streams;
84ee65b806SJan Lentfer 	ctx->streams = new;
85ee65b806SJan Lentfer 	if (id != NULL)
86ee65b806SJan Lentfer 		id->opaque = new;
87ee65b806SJan Lentfer 	return (0);
88ee65b806SJan Lentfer  free:
89ee65b806SJan Lentfer 	save = errno;
90ee65b806SJan Lentfer 	FREE(new);
91ee65b806SJan Lentfer 	errno = save;
92ee65b806SJan Lentfer 	return (-1);
93ee65b806SJan Lentfer }
94ee65b806SJan Lentfer 
95ee65b806SJan Lentfer int
evRead(evContext opaqueCtx,int fd,const struct iovec * iov,int iocnt,evStreamFunc func,void * uap,evStreamID * id)96ee65b806SJan Lentfer evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
97ee65b806SJan Lentfer        evStreamFunc func, void *uap, evStreamID *id)
98ee65b806SJan Lentfer {
99ee65b806SJan Lentfer 	evContext_p *ctx = opaqueCtx.opaque;
100ee65b806SJan Lentfer 	evStream *new;
101ee65b806SJan Lentfer 	int save;
102ee65b806SJan Lentfer 
103ee65b806SJan Lentfer 	OKNEW(new);
104ee65b806SJan Lentfer 	new->func = func;
105ee65b806SJan Lentfer 	new->uap = uap;
106ee65b806SJan Lentfer 	new->fd = fd;
107ee65b806SJan Lentfer 	new->flags = 0;
108ee65b806SJan Lentfer 	if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0)
109ee65b806SJan Lentfer 		goto free;
110ee65b806SJan Lentfer 	if (copyvec(new, iov, iocnt) < 0)
111ee65b806SJan Lentfer 		goto free;
112ee65b806SJan Lentfer 	new->prevDone = NULL;
113ee65b806SJan Lentfer 	new->nextDone = NULL;
114ee65b806SJan Lentfer 	if (ctx->streams != NULL)
115ee65b806SJan Lentfer 		ctx->streams->prev = new;
116ee65b806SJan Lentfer 	new->prev = NULL;
117ee65b806SJan Lentfer 	new->next = ctx->streams;
118ee65b806SJan Lentfer 	ctx->streams = new;
119ee65b806SJan Lentfer 	if (id)
120ee65b806SJan Lentfer 		id->opaque = new;
121ee65b806SJan Lentfer 	return (0);
122ee65b806SJan Lentfer  free:
123ee65b806SJan Lentfer 	save = errno;
124ee65b806SJan Lentfer 	FREE(new);
125ee65b806SJan Lentfer 	errno = save;
126ee65b806SJan Lentfer 	return (-1);
127ee65b806SJan Lentfer }
128ee65b806SJan Lentfer 
129ee65b806SJan Lentfer int
evTimeRW(evContext opaqueCtx,evStreamID id,evTimerID timer)130ee65b806SJan Lentfer evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ {
131ee65b806SJan Lentfer 	evStream *str = id.opaque;
132ee65b806SJan Lentfer 
133ee65b806SJan Lentfer 	UNUSED(opaqueCtx);
134ee65b806SJan Lentfer 
135ee65b806SJan Lentfer 	str->timer = timer;
136ee65b806SJan Lentfer 	str->flags |= EV_STR_TIMEROK;
137ee65b806SJan Lentfer 	return (0);
138ee65b806SJan Lentfer }
139ee65b806SJan Lentfer 
140ee65b806SJan Lentfer int
evUntimeRW(evContext opaqueCtx,evStreamID id)141ee65b806SJan Lentfer evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ {
142ee65b806SJan Lentfer 	evStream *str = id.opaque;
143ee65b806SJan Lentfer 
144ee65b806SJan Lentfer 	UNUSED(opaqueCtx);
145ee65b806SJan Lentfer 
146ee65b806SJan Lentfer 	str->flags &= ~EV_STR_TIMEROK;
147ee65b806SJan Lentfer 	return (0);
148ee65b806SJan Lentfer }
149ee65b806SJan Lentfer 
150ee65b806SJan Lentfer int
evCancelRW(evContext opaqueCtx,evStreamID id)151ee65b806SJan Lentfer evCancelRW(evContext opaqueCtx, evStreamID id) {
152ee65b806SJan Lentfer 	evContext_p *ctx = opaqueCtx.opaque;
153ee65b806SJan Lentfer 	evStream *old = id.opaque;
154ee65b806SJan Lentfer 
155ee65b806SJan Lentfer 	/*
156ee65b806SJan Lentfer 	 * The streams list is doubly threaded.  First, there's ctx->streams
157ee65b806SJan Lentfer 	 * that's used by evDestroy() to find and cancel all streams.  Second,
158ee65b806SJan Lentfer 	 * there's ctx->strDone (head) and ctx->strLast (tail) which thread
159ee65b806SJan Lentfer 	 * through the potentially smaller number of "IO completed" streams,
160ee65b806SJan Lentfer 	 * used in evGetNext() to avoid scanning the entire list.
161ee65b806SJan Lentfer 	 */
162ee65b806SJan Lentfer 
163ee65b806SJan Lentfer 	/* Unlink from ctx->streams. */
164ee65b806SJan Lentfer 	if (old->prev != NULL)
165ee65b806SJan Lentfer 		old->prev->next = old->next;
166ee65b806SJan Lentfer 	else
167ee65b806SJan Lentfer 		ctx->streams = old->next;
168ee65b806SJan Lentfer 	if (old->next != NULL)
169ee65b806SJan Lentfer 		old->next->prev = old->prev;
170ee65b806SJan Lentfer 
171ee65b806SJan Lentfer 	/*
172ee65b806SJan Lentfer 	 * If 'old' is on the ctx->strDone list, remove it.  Update
173ee65b806SJan Lentfer 	 * ctx->strLast if necessary.
174ee65b806SJan Lentfer 	 */
175ee65b806SJan Lentfer 	if (old->prevDone == NULL && old->nextDone == NULL) {
176ee65b806SJan Lentfer 		/*
177ee65b806SJan Lentfer 		 * Either 'old' is the only item on the done list, or it's
178ee65b806SJan Lentfer 		 * not on the done list.  If the former, then we unlink it
179ee65b806SJan Lentfer 		 * from the list.  If the latter, we leave the list alone.
180ee65b806SJan Lentfer 		 */
181ee65b806SJan Lentfer 		if (ctx->strDone == old) {
182ee65b806SJan Lentfer 			ctx->strDone = NULL;
183ee65b806SJan Lentfer 			ctx->strLast = NULL;
184ee65b806SJan Lentfer 		}
185ee65b806SJan Lentfer 	} else {
186ee65b806SJan Lentfer 		if (old->prevDone != NULL)
187ee65b806SJan Lentfer 			old->prevDone->nextDone = old->nextDone;
188ee65b806SJan Lentfer 		else
189ee65b806SJan Lentfer 			ctx->strDone = old->nextDone;
190ee65b806SJan Lentfer 		if (old->nextDone != NULL)
191ee65b806SJan Lentfer 			old->nextDone->prevDone = old->prevDone;
192ee65b806SJan Lentfer 		else
193ee65b806SJan Lentfer 			ctx->strLast = old->prevDone;
194ee65b806SJan Lentfer 	}
195ee65b806SJan Lentfer 
196ee65b806SJan Lentfer 	/* Deallocate the stream. */
197ee65b806SJan Lentfer 	if (old->file.opaque)
198ee65b806SJan Lentfer 		evDeselectFD(opaqueCtx, old->file);
199ee65b806SJan Lentfer 	memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount);
200ee65b806SJan Lentfer 	FREE(old);
201ee65b806SJan Lentfer 	return (0);
202ee65b806SJan Lentfer }
203ee65b806SJan Lentfer 
204ee65b806SJan Lentfer /* Copy a scatter/gather vector and initialize a stream handler's IO. */
205ee65b806SJan Lentfer static int
copyvec(evStream * str,const struct iovec * iov,int iocnt)206ee65b806SJan Lentfer copyvec(evStream *str, const struct iovec *iov, int iocnt) {
207ee65b806SJan Lentfer 	int i;
208ee65b806SJan Lentfer 
209ee65b806SJan Lentfer 	str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt);
210ee65b806SJan Lentfer 	if (str->iovOrig == NULL) {
211ee65b806SJan Lentfer 		errno = ENOMEM;
212ee65b806SJan Lentfer 		return (-1);
213ee65b806SJan Lentfer 	}
214ee65b806SJan Lentfer 	str->ioTotal = 0;
215ee65b806SJan Lentfer 	for (i = 0; i < iocnt; i++) {
216ee65b806SJan Lentfer 		str->iovOrig[i] = iov[i];
217ee65b806SJan Lentfer 		str->ioTotal += iov[i].iov_len;
218ee65b806SJan Lentfer 	}
219ee65b806SJan Lentfer 	str->iovOrigCount = iocnt;
220ee65b806SJan Lentfer 	str->iovCur = str->iovOrig;
221ee65b806SJan Lentfer 	str->iovCurCount = str->iovOrigCount;
222ee65b806SJan Lentfer 	str->ioDone = 0;
223ee65b806SJan Lentfer 	return (0);
224ee65b806SJan Lentfer }
225ee65b806SJan Lentfer 
226ee65b806SJan Lentfer /* Pull off or truncate lead iovec(s). */
227ee65b806SJan Lentfer static void
consume(evStream * str,size_t bytes)228ee65b806SJan Lentfer consume(evStream *str, size_t bytes) {
229ee65b806SJan Lentfer 	while (bytes > 0U) {
230ee65b806SJan Lentfer 		if (bytes < (size_t)str->iovCur->iov_len) {
231ee65b806SJan Lentfer 			str->iovCur->iov_len -= bytes;
232ee65b806SJan Lentfer 			str->iovCur->iov_base = (void *)
233ee65b806SJan Lentfer 				((u_char *)str->iovCur->iov_base + bytes);
234ee65b806SJan Lentfer 			str->ioDone += bytes;
235ee65b806SJan Lentfer 			bytes = 0;
236ee65b806SJan Lentfer 		} else {
237ee65b806SJan Lentfer 			bytes -= str->iovCur->iov_len;
238ee65b806SJan Lentfer 			str->ioDone += str->iovCur->iov_len;
239ee65b806SJan Lentfer 			str->iovCur++;
240ee65b806SJan Lentfer 			str->iovCurCount--;
241ee65b806SJan Lentfer 		}
242ee65b806SJan Lentfer 	}
243ee65b806SJan Lentfer }
244ee65b806SJan Lentfer 
245ee65b806SJan Lentfer /* Add a stream to Done list and deselect the FD. */
246ee65b806SJan Lentfer static void
done(evContext opaqueCtx,evStream * str)247ee65b806SJan Lentfer done(evContext opaqueCtx, evStream *str) {
248ee65b806SJan Lentfer 	evContext_p *ctx = opaqueCtx.opaque;
249ee65b806SJan Lentfer 
250ee65b806SJan Lentfer 	if (ctx->strLast != NULL) {
251ee65b806SJan Lentfer 		str->prevDone = ctx->strLast;
252ee65b806SJan Lentfer 		ctx->strLast->nextDone = str;
253ee65b806SJan Lentfer 		ctx->strLast = str;
254ee65b806SJan Lentfer 	} else {
255ee65b806SJan Lentfer 		INSIST(ctx->strDone == NULL);
256ee65b806SJan Lentfer 		ctx->strDone = ctx->strLast = str;
257ee65b806SJan Lentfer 	}
258ee65b806SJan Lentfer 	evDeselectFD(opaqueCtx, str->file);
259ee65b806SJan Lentfer 	str->file.opaque = NULL;
260ee65b806SJan Lentfer 	/* evDrop() will call evCancelRW() on us. */
261ee65b806SJan Lentfer }
262ee65b806SJan Lentfer 
263ee65b806SJan Lentfer /* Dribble out some bytes on the stream.  (Called by evDispatch().) */
264ee65b806SJan Lentfer static void
writable(evContext opaqueCtx,void * uap,int fd,int evmask)265ee65b806SJan Lentfer writable(evContext opaqueCtx, void *uap, int fd, int evmask) {
266ee65b806SJan Lentfer 	evStream *str = uap;
267ee65b806SJan Lentfer 	int bytes;
268ee65b806SJan Lentfer 
269ee65b806SJan Lentfer 	UNUSED(evmask);
270ee65b806SJan Lentfer 
271ee65b806SJan Lentfer 	bytes = writev(fd, str->iovCur, str->iovCurCount);
272ee65b806SJan Lentfer 	if (bytes > 0) {
273ee65b806SJan Lentfer 		if ((str->flags & EV_STR_TIMEROK) != 0)
274ee65b806SJan Lentfer 			evTouchIdleTimer(opaqueCtx, str->timer);
275ee65b806SJan Lentfer 		consume(str, bytes);
276ee65b806SJan Lentfer 	} else {
277ee65b806SJan Lentfer 		if (bytes < 0 && errno != EINTR) {
278ee65b806SJan Lentfer 			str->ioDone = -1;
279ee65b806SJan Lentfer 			str->ioErrno = errno;
280ee65b806SJan Lentfer 		}
281ee65b806SJan Lentfer 	}
282ee65b806SJan Lentfer 	if (str->ioDone == -1 || str->ioDone == str->ioTotal)
283ee65b806SJan Lentfer 		done(opaqueCtx, str);
284ee65b806SJan Lentfer }
285ee65b806SJan Lentfer 
286ee65b806SJan Lentfer /* Scoop up some bytes from the stream.  (Called by evDispatch().) */
287ee65b806SJan Lentfer static void
readable(evContext opaqueCtx,void * uap,int fd,int evmask)288ee65b806SJan Lentfer readable(evContext opaqueCtx, void *uap, int fd, int evmask) {
289ee65b806SJan Lentfer 	evStream *str = uap;
290ee65b806SJan Lentfer 	int bytes;
291ee65b806SJan Lentfer 
292ee65b806SJan Lentfer 	UNUSED(evmask);
293ee65b806SJan Lentfer 
294ee65b806SJan Lentfer 	bytes = readv(fd, str->iovCur, str->iovCurCount);
295ee65b806SJan Lentfer 	if (bytes > 0) {
296ee65b806SJan Lentfer 		if ((str->flags & EV_STR_TIMEROK) != 0)
297ee65b806SJan Lentfer 			evTouchIdleTimer(opaqueCtx, str->timer);
298ee65b806SJan Lentfer 		consume(str, bytes);
299ee65b806SJan Lentfer 	} else {
300ee65b806SJan Lentfer 		if (bytes == 0)
301ee65b806SJan Lentfer 			str->ioDone = 0;
302ee65b806SJan Lentfer 		else {
303ee65b806SJan Lentfer 			if (errno != EINTR) {
304ee65b806SJan Lentfer 				str->ioDone = -1;
305ee65b806SJan Lentfer 				str->ioErrno = errno;
306ee65b806SJan Lentfer 			}
307ee65b806SJan Lentfer 		}
308ee65b806SJan Lentfer 	}
309ee65b806SJan Lentfer 	if (str->ioDone <= 0 || str->ioDone == str->ioTotal)
310ee65b806SJan Lentfer 		done(opaqueCtx, str);
311ee65b806SJan Lentfer }
312ee65b806SJan Lentfer #endif /* !_LIBC */
313ee65b806SJan Lentfer 
314ee65b806SJan Lentfer /*! \file */
315