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#include "port_before.h"
23#include "fd_setsize.h"
24
25#include <sys/types.h>
26#include <sys/uio.h>
27
28#include <errno.h>
29
30#include <isc/eventlib.h>
31#include <isc/assertions.h>
32#include "eventlib_p.h"
33
34#include "port_after.h"
35
36static int	copyvec(evStream *str, const struct iovec *iov, int iocnt);
37static void	consume(evStream *str, size_t bytes);
38static void	done(evContext opaqueCtx, evStream *str);
39static void	writable(evContext opaqueCtx, void *uap, int fd, int evmask);
40static void	readable(evContext opaqueCtx, void *uap, int fd, int evmask);
41
42struct iovec
43evConsIovec(void *buf, size_t cnt) {
44	struct iovec ret;
45
46	memset(&ret, 0xf5, sizeof ret);
47	ret.iov_base = buf;
48	ret.iov_len = cnt;
49	return (ret);
50}
51
52int
53evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
54	evStreamFunc func, void *uap, evStreamID *id)
55{
56	evContext_p *ctx = opaqueCtx.opaque;
57	evStream *new;
58	int save;
59
60	OKNEW(new);
61	new->func = func;
62	new->uap = uap;
63	new->fd = fd;
64	new->flags = 0;
65	if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0)
66		goto free;
67	if (copyvec(new, iov, iocnt) < 0)
68		goto free;
69	new->prevDone = NULL;
70	new->nextDone = NULL;
71	if (ctx->streams != NULL)
72		ctx->streams->prev = new;
73	new->prev = NULL;
74	new->next = ctx->streams;
75	ctx->streams = new;
76	if (id != NULL)
77		id->opaque = new;
78	return (0);
79 free:
80	save = errno;
81	FREE(new);
82	errno = save;
83	return (-1);
84}
85
86int
87evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
88       evStreamFunc func, void *uap, evStreamID *id)
89{
90	evContext_p *ctx = opaqueCtx.opaque;
91	evStream *new;
92	int save;
93
94	OKNEW(new);
95	new->func = func;
96	new->uap = uap;
97	new->fd = fd;
98	new->flags = 0;
99	if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0)
100		goto free;
101	if (copyvec(new, iov, iocnt) < 0)
102		goto free;
103	new->prevDone = NULL;
104	new->nextDone = NULL;
105	if (ctx->streams != NULL)
106		ctx->streams->prev = new;
107	new->prev = NULL;
108	new->next = ctx->streams;
109	ctx->streams = new;
110	if (id)
111		id->opaque = new;
112	return (0);
113 free:
114	save = errno;
115	FREE(new);
116	errno = save;
117	return (-1);
118}
119
120int
121evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ {
122	evStream *str = id.opaque;
123
124	UNUSED(opaqueCtx);
125
126	str->timer = timer;
127	str->flags |= EV_STR_TIMEROK;
128	return (0);
129}
130
131int
132evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ {
133	evStream *str = id.opaque;
134
135	UNUSED(opaqueCtx);
136
137	str->flags &= ~EV_STR_TIMEROK;
138	return (0);
139}
140
141int
142evCancelRW(evContext opaqueCtx, evStreamID id) {
143	evContext_p *ctx = opaqueCtx.opaque;
144	evStream *old = id.opaque;
145
146	/*
147	 * The streams list is doubly threaded.  First, there's ctx->streams
148	 * that's used by evDestroy() to find and cancel all streams.  Second,
149	 * there's ctx->strDone (head) and ctx->strLast (tail) which thread
150	 * through the potentially smaller number of "IO completed" streams,
151	 * used in evGetNext() to avoid scanning the entire list.
152	 */
153
154	/* Unlink from ctx->streams. */
155	if (old->prev != NULL)
156		old->prev->next = old->next;
157	else
158		ctx->streams = old->next;
159	if (old->next != NULL)
160		old->next->prev = old->prev;
161
162	/*
163	 * If 'old' is on the ctx->strDone list, remove it.  Update
164	 * ctx->strLast if necessary.
165	 */
166	if (old->prevDone == NULL && old->nextDone == NULL) {
167		/*
168		 * Either 'old' is the only item on the done list, or it's
169		 * not on the done list.  If the former, then we unlink it
170		 * from the list.  If the latter, we leave the list alone.
171		 */
172		if (ctx->strDone == old) {
173			ctx->strDone = NULL;
174			ctx->strLast = NULL;
175		}
176	} else {
177		if (old->prevDone != NULL)
178			old->prevDone->nextDone = old->nextDone;
179		else
180			ctx->strDone = old->nextDone;
181		if (old->nextDone != NULL)
182			old->nextDone->prevDone = old->prevDone;
183		else
184			ctx->strLast = old->prevDone;
185	}
186
187	/* Deallocate the stream. */
188	if (old->file.opaque)
189		evDeselectFD(opaqueCtx, old->file);
190	memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount);
191	FREE(old);
192	return (0);
193}
194
195/* Copy a scatter/gather vector and initialize a stream handler's IO. */
196static int
197copyvec(evStream *str, const struct iovec *iov, int iocnt) {
198	int i;
199
200	str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt);
201	if (str->iovOrig == NULL) {
202		errno = ENOMEM;
203		return (-1);
204	}
205	str->ioTotal = 0;
206	for (i = 0; i < iocnt; i++) {
207		str->iovOrig[i] = iov[i];
208		str->ioTotal += iov[i].iov_len;
209	}
210	str->iovOrigCount = iocnt;
211	str->iovCur = str->iovOrig;
212	str->iovCurCount = str->iovOrigCount;
213	str->ioDone = 0;
214	return (0);
215}
216
217/* Pull off or truncate lead iovec(s). */
218static void
219consume(evStream *str, size_t bytes) {
220	while (bytes > 0U) {
221		if (bytes < (size_t)str->iovCur->iov_len) {
222			str->iovCur->iov_len -= bytes;
223			str->iovCur->iov_base = (void *)
224				((u_char *)str->iovCur->iov_base + bytes);
225			str->ioDone += bytes;
226			bytes = 0;
227		} else {
228			bytes -= str->iovCur->iov_len;
229			str->ioDone += str->iovCur->iov_len;
230			str->iovCur++;
231			str->iovCurCount--;
232		}
233	}
234}
235
236/* Add a stream to Done list and deselect the FD. */
237static void
238done(evContext opaqueCtx, evStream *str) {
239	evContext_p *ctx = opaqueCtx.opaque;
240
241	if (ctx->strLast != NULL) {
242		str->prevDone = ctx->strLast;
243		ctx->strLast->nextDone = str;
244		ctx->strLast = str;
245	} else {
246		INSIST(ctx->strDone == NULL);
247		ctx->strDone = ctx->strLast = str;
248	}
249	evDeselectFD(opaqueCtx, str->file);
250	str->file.opaque = NULL;
251	/* evDrop() will call evCancelRW() on us. */
252}
253
254/* Dribble out some bytes on the stream.  (Called by evDispatch().) */
255static void
256writable(evContext opaqueCtx, void *uap, int fd, int evmask) {
257	evStream *str = uap;
258	int bytes;
259
260	UNUSED(evmask);
261
262	bytes = writev(fd, str->iovCur, str->iovCurCount);
263	if (bytes > 0) {
264		if ((str->flags & EV_STR_TIMEROK) != 0)
265			evTouchIdleTimer(opaqueCtx, str->timer);
266		consume(str, bytes);
267	} else {
268		if (bytes < 0 && errno != EINTR) {
269			str->ioDone = -1;
270			str->ioErrno = errno;
271		}
272	}
273	if (str->ioDone == -1 || str->ioDone == str->ioTotal)
274		done(opaqueCtx, str);
275}
276
277/* Scoop up some bytes from the stream.  (Called by evDispatch().) */
278static void
279readable(evContext opaqueCtx, void *uap, int fd, int evmask) {
280	evStream *str = uap;
281	int bytes;
282
283	UNUSED(evmask);
284
285	bytes = readv(fd, str->iovCur, str->iovCurCount);
286	if (bytes > 0) {
287		if ((str->flags & EV_STR_TIMEROK) != 0)
288			evTouchIdleTimer(opaqueCtx, str->timer);
289		consume(str, bytes);
290	} else {
291		if (bytes == 0)
292			str->ioDone = 0;
293		else {
294			if (errno != EINTR) {
295				str->ioDone = -1;
296				str->ioErrno = errno;
297			}
298		}
299	}
300	if (str->ioDone <= 0 || str->ioDone == str->ioTotal)
301		done(opaqueCtx, str);
302}
303
304/*! \file */
305