source: rtems-libbsd/freebsd/lib/libc/isc/ev_streams.c @ e599318

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since e599318 was e599318, checked in by Sebastian Huber <sebastian.huber@…>, on 10/09/13 at 20:52:54

Update files to match FreeBSD layout

Add compatibility with Newlib header files. Some FreeBSD header files
are mapped by the translation script:

o rtems/bsd/sys/_types.h
o rtems/bsd/sys/errno.h
o rtems/bsd/sys/lock.h
o rtems/bsd/sys/param.h
o rtems/bsd/sys/resource.h
o rtems/bsd/sys/time.h
o rtems/bsd/sys/timespec.h
o rtems/bsd/sys/types.h
o rtems/bsd/sys/unistd.h

It is now possible to include <sys/socket.h> directly for example.

Generate one Makefile which builds everything including tests.

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