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