]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bind/lib/isc/ev_files.c
This commit was generated by cvs2svn to compensate for changes in r52284,
[FreeBSD/FreeBSD.git] / contrib / bind / lib / isc / ev_files.c
1 /* Copyright (c) 1995, 1996, 1997, 1998 by Internet Software Consortium
2  *
3  * Permission to use, copy, modify, and distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
8  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
9  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
10  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
11  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
12  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
13  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
14  * SOFTWARE.
15  */
16
17 /* ev_files.c - implement asynch file IO for the eventlib
18  * vix 11sep95 [initial]
19  */
20
21 #if !defined(LINT) && !defined(CODECENTER)
22 static const char rcsid[] = "$Id: ev_files.c,v 1.15 1998/02/06 01:53:52 halley Exp $";
23 #endif
24
25 #include "port_before.h"
26 #include "fd_setsize.h"
27
28 #include <sys/types.h>
29 #include <sys/time.h>
30
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34
35 #include <isc/eventlib.h>
36 #include "eventlib_p.h"
37
38 #include "port_after.h"
39
40 static evFile *FindFD(const evContext_p *ctx, int fd, int eventmask);
41
42 int
43 evSelectFD(evContext opaqueCtx,
44            int fd,
45            int eventmask,
46            evFileFunc func,
47            void *uap,
48            evFileID *opaqueID
49 ) {
50         evContext_p *ctx = opaqueCtx.opaque;
51         evFile *id;
52         int mode;
53
54         evPrintf(ctx, 1,
55                  "evSelectFD(ctx %#x, fd %d, mask 0x%x, func %#x, uap %#x)\n",
56                  ctx, fd, eventmask, func, uap);
57         if (eventmask == 0 || (eventmask & ~EV_MASK_ALL) != 0)
58                 ERR(EINVAL);
59         if (fd >= FD_SETSIZE)
60                 ERR(EINVAL);
61         OK(mode = fcntl(fd, F_GETFL, NULL));    /* side effect: validate fd. */
62
63         /*
64          * The first time we touch a file descriptor, we need to check to see
65          * if the application already had it in O_NONBLOCK mode and if so, all
66          * of our deselect()'s have to leave it in O_NONBLOCK.  If not, then
67          * all but our last deselect() has to leave it in O_NONBLOCK.
68          */
69         id = FindFD(ctx, fd, EV_MASK_ALL);
70         if (id == NULL) {
71                 if (mode & O_NONBLOCK)
72                         FD_SET(fd, &ctx->nonblockBefore);
73                 else {
74                         OK(fcntl(fd, F_SETFL, mode | O_NONBLOCK));
75                         FD_CLR(fd, &ctx->nonblockBefore);
76                 }
77         }
78
79         /*
80          * If this descriptor is already in use, search for it again to see
81          * if any of the eventmask bits we want to set are already captured.
82          * We cannot usefully capture the same fd event more than once in the
83          * same context.
84          */
85         if (id != NULL && FindFD(ctx, fd, eventmask) != NULL)
86                 ERR(ETOOMANYREFS);
87
88         /* Allocate and fill. */
89         OKNEW(id);
90         id->func = func;
91         id->uap = uap;
92         id->fd = fd;
93         id->eventmask = eventmask;
94
95         /*
96          * Insert at head.  Order could be important for performance if we
97          * believe that evGetNext()'s accesses to the fd_sets will be more
98          * serial and therefore more cache-lucky if the list is ordered by
99          * ``fd.''  We do not believe these things, so we don't do it.
100          *
101          * The interesting sequence is where GetNext() has cached a select()
102          * result and the caller decides to evSelectFD() on some descriptor.
103          * Since GetNext() starts at the head, it can miss new entries we add
104          * at the head.  This is not a serious problem since the event being
105          * evSelectFD()'d for has to occur before evSelectFD() is called for
106          * the file event to be considered "missed" -- a real corner case.
107          * Maintaining a "tail" pointer for ctx->files would fix this, but I'm
108          * not sure it would be ``more correct.''
109          */
110         if (ctx->files != NULL)
111                 ctx->files->prev = id;
112         id->prev = NULL;
113         id->next = ctx->files;
114         ctx->files = id;
115
116         /* Insert into fd table. */
117         if (ctx->fdTable[fd] != NULL)
118                 ctx->fdTable[fd]->fdprev = id;
119         id->fdprev = NULL;
120         id->fdnext = ctx->fdTable[fd];
121         ctx->fdTable[fd] = id;
122
123         /* Turn on the appropriate bits in the {rd,wr,ex}Next fd_set's. */
124         if (eventmask & EV_READ)
125                 FD_SET(fd, &ctx->rdNext);
126         if (eventmask & EV_WRITE)
127                 FD_SET(fd, &ctx->wrNext);
128         if (eventmask & EV_EXCEPT)
129                 FD_SET(fd, &ctx->exNext);
130
131         /* Update fdMax. */
132         if (fd > ctx->fdMax)
133                 ctx->fdMax = fd;
134
135         /* Remember the ID if the caller provided us a place for it. */
136         if (opaqueID)
137                 opaqueID->opaque = id;
138
139         evPrintf(ctx, 5,
140                 "evSelectFD(fd %d, mask 0x%x): new masks: 0x%lx 0x%lx 0x%lx\n",
141                  fd, eventmask,
142                  (u_long)ctx->rdNext.fds_bits[0],
143                  (u_long)ctx->wrNext.fds_bits[0],
144                  (u_long)ctx->exNext.fds_bits[0]);
145
146         return (0);
147 }
148
149 int
150 evDeselectFD(evContext opaqueCtx, evFileID opaqueID) {
151         evContext_p *ctx = opaqueCtx.opaque;
152         evFile *del = opaqueID.opaque;
153         evFile *old, *cur;
154         int mode, eventmask;
155
156         if (!del) {
157                 evPrintf(ctx, 11, "evDeselectFD(NULL) ignored\n");
158                 errno = EINVAL;
159                 return (-1);
160         }
161
162         evPrintf(ctx, 1, "evDeselectFD(fd %d, mask 0x%x)\n",
163                  del->fd, del->eventmask);
164
165         /* Get the mode.  Unless the file has been closed, errors are bad. */
166         mode = fcntl(del->fd, F_GETFL, NULL);
167         if (mode == -1 && errno != EBADF)
168                 ERR(errno);
169
170         /* Remove from the list of files. */
171         if (del->prev != NULL)
172                 del->prev->next = del->next;
173         else
174                 ctx->files = del->next;
175         if (del->next != NULL)
176                 del->next->prev = del->prev;
177
178         /* Remove from the fd table. */
179         if (del->fdprev != NULL)
180                 del->fdprev->fdnext = del->fdnext;
181         else
182                 ctx->fdTable[del->fd] = del->fdnext;
183         if (del->fdnext != NULL)
184                 del->fdnext->fdprev = del->fdprev;
185
186         /*
187          * If the file descriptor does not appear in any other select() entry,
188          * and if !EV_WASNONBLOCK, and if we got no EBADF when we got the mode
189          * earlier, then: restore the fd to blocking status.
190          */
191         if (!(cur = FindFD(ctx, del->fd, EV_MASK_ALL)) &&
192             !FD_ISSET(del->fd, &ctx->nonblockBefore) &&
193             mode != -1) {
194                 /*
195                  * Note that we won't return an error status to the caller if
196                  * this fcntl() fails since (a) we've already done the work
197                  * and (b) the caller didn't ask us anything about O_NONBLOCK.
198                  */
199                 (void) fcntl(del->fd, F_SETFL, mode & ~O_NONBLOCK);
200         }
201
202         /*
203          * Now find all other uses of this descriptor and OR together an event
204          * mask so that we don't turn off {rd,wr,ex}Next bits that some other
205          * file event is using.  As an optimization, stop if the event mask
206          * fills.
207          */
208         eventmask = 0;
209         for ((void)NULL;
210              cur != NULL && eventmask != EV_MASK_ALL;
211              cur = cur->next)
212                 if (cur->fd == del->fd)
213                         eventmask |= cur->eventmask;
214
215         /* OK, now we know which bits we can clear out. */
216         if (!(eventmask & EV_READ)) {
217                 FD_CLR(del->fd, &ctx->rdNext);
218                 if (FD_ISSET(del->fd, &ctx->rdLast)) {
219                         FD_CLR(del->fd, &ctx->rdLast);
220                         ctx->fdCount--;
221                 }
222         }
223         if (!(eventmask & EV_WRITE)) {
224                 FD_CLR(del->fd, &ctx->wrNext);
225                 if (FD_ISSET(del->fd, &ctx->wrLast)) {
226                         FD_CLR(del->fd, &ctx->wrLast);
227                         ctx->fdCount--;
228                 }
229         }
230         if (!(eventmask & EV_EXCEPT)) {
231                 FD_CLR(del->fd, &ctx->exNext);
232                 if (FD_ISSET(del->fd, &ctx->exLast)) {
233                         FD_CLR(del->fd, &ctx->exLast);
234                         ctx->fdCount--;
235                 }
236         }
237
238         /* If this was the maxFD, find the new one. */
239         if (del->fd == ctx->fdMax) {
240                 ctx->fdMax = -1;
241                 for (cur = ctx->files; cur; cur = cur->next)
242                         if (cur->fd > ctx->fdMax)
243                                 ctx->fdMax = cur->fd;
244         }
245
246         /* If this was the fdNext, cycle that to the next entry. */
247         if (del == ctx->fdNext)
248                 ctx->fdNext = del->next;
249
250         evPrintf(ctx, 5,
251               "evDeselectFD(fd %d, mask 0x%x): new masks: 0x%lx 0x%lx 0x%lx\n",
252                  del->fd, eventmask,
253                  (u_long)ctx->rdNext.fds_bits[0],
254                  (u_long)ctx->wrNext.fds_bits[0],
255                  (u_long)ctx->exNext.fds_bits[0]);
256
257         /* Couldn't free it before now since we were using fields out of it. */
258         FREE(del);
259
260         return (0);
261 }
262
263 static evFile *
264 FindFD(const evContext_p *ctx, int fd, int eventmask) {
265         evFile *id;
266
267         for (id = ctx->fdTable[fd]; id != NULL; id = id->fdnext)
268                 if (id->fd == fd && (id->eventmask & eventmask) != 0)
269                         break;
270         return (id);
271 }