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