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