Merge from vendor branch TNFTP:
[dragonfly.git] / contrib / bind-9.3 / lib / bind / isc / ev_files.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1995-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_files.c - implement asynch file IO for the eventlib
19  * vix 11sep95 [initial]
20  */
21
22 #if !defined(LINT) && !defined(CODECENTER)
23 static const char rcsid[] = "$Id: ev_files.c,v 1.3.2.1.4.3 2005/07/28 07:43:19 marka Exp $";
24 #endif
25
26 #include "port_before.h"
27 #include "fd_setsize.h"
28
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/ioctl.h>
32
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36
37 #include <isc/eventlib.h>
38 #include "eventlib_p.h"
39
40 #include "port_after.h"
41
42 static evFile *FindFD(const evContext_p *ctx, int fd, int eventmask);
43
44 int
45 evSelectFD(evContext opaqueCtx,
46            int fd,
47            int eventmask,
48            evFileFunc func,
49            void *uap,
50            evFileID *opaqueID
51 ) {
52         evContext_p *ctx = opaqueCtx.opaque;
53         evFile *id;
54         int mode;
55
56         evPrintf(ctx, 1,
57                  "evSelectFD(ctx %p, fd %d, mask 0x%x, func %p, uap %p)\n",
58                  ctx, fd, eventmask, func, uap);
59         if (eventmask == 0 || (eventmask & ~EV_MASK_ALL) != 0)
60                 EV_ERR(EINVAL);
61 #ifndef USE_POLL
62         if (fd > ctx->highestFD)
63                 EV_ERR(EINVAL);
64 #endif
65         OK(mode = fcntl(fd, F_GETFL, NULL));    /* side effect: validate fd. */
66
67         /*
68          * The first time we touch a file descriptor, we need to check to see
69          * if the application already had it in O_NONBLOCK mode and if so, all
70          * of our deselect()'s have to leave it in O_NONBLOCK.  If not, then
71          * all but our last deselect() has to leave it in O_NONBLOCK.
72          */
73 #ifdef USE_POLL
74         /* Make sure both ctx->pollfds[] and ctx->fdTable[] are large enough */
75         if (fd >= ctx->maxnfds && evPollfdRealloc(ctx, 1, fd) != 0)
76                 EV_ERR(ENOMEM);
77 #endif /* USE_POLL */
78         id = FindFD(ctx, fd, EV_MASK_ALL);
79         if (id == NULL) {
80                 if (mode & PORT_NONBLOCK)
81                         FD_SET(fd, &ctx->nonblockBefore);
82                 else {
83 #ifdef USE_FIONBIO_IOCTL
84                         int on = 1;
85                         OK(ioctl(fd, FIONBIO, (char *)&on));
86 #else
87                         OK(fcntl(fd, F_SETFL, mode | PORT_NONBLOCK));
88 #endif
89                         FD_CLR(fd, &ctx->nonblockBefore);
90                 }
91         }
92
93         /*
94          * If this descriptor is already in use, search for it again to see
95          * if any of the eventmask bits we want to set are already captured.
96          * We cannot usefully capture the same fd event more than once in the
97          * same context.
98          */
99         if (id != NULL && FindFD(ctx, fd, eventmask) != NULL)
100                 EV_ERR(ETOOMANYREFS);
101
102         /* Allocate and fill. */
103         OKNEW(id);
104         id->func = func;
105         id->uap = uap;
106         id->fd = fd;
107         id->eventmask = eventmask;
108
109         /*
110          * Insert at head.  Order could be important for performance if we
111          * believe that evGetNext()'s accesses to the fd_sets will be more
112          * serial and therefore more cache-lucky if the list is ordered by
113          * ``fd.''  We do not believe these things, so we don't do it.
114          *
115          * The interesting sequence is where GetNext() has cached a select()
116          * result and the caller decides to evSelectFD() on some descriptor.
117          * Since GetNext() starts at the head, it can miss new entries we add
118          * at the head.  This is not a serious problem since the event being
119          * evSelectFD()'d for has to occur before evSelectFD() is called for
120          * the file event to be considered "missed" -- a real corner case.
121          * Maintaining a "tail" pointer for ctx->files would fix this, but I'm
122          * not sure it would be ``more correct.''
123          */
124         if (ctx->files != NULL)
125                 ctx->files->prev = id;
126         id->prev = NULL;
127         id->next = ctx->files;
128         ctx->files = id;
129
130         /* Insert into fd table. */
131         if (ctx->fdTable[fd] != NULL)
132                 ctx->fdTable[fd]->fdprev = id;
133         id->fdprev = NULL;
134         id->fdnext = ctx->fdTable[fd];
135         ctx->fdTable[fd] = id;
136
137         /* Turn on the appropriate bits in the {rd,wr,ex}Next fd_set's. */
138         if (eventmask & EV_READ)
139                 FD_SET(fd, &ctx->rdNext);
140         if (eventmask & EV_WRITE)
141                 FD_SET(fd, &ctx->wrNext);
142         if (eventmask & EV_EXCEPT)
143                 FD_SET(fd, &ctx->exNext);
144
145         /* Update fdMax. */
146         if (fd > ctx->fdMax)
147                 ctx->fdMax = fd;
148
149         /* Remember the ID if the caller provided us a place for it. */
150         if (opaqueID)
151                 opaqueID->opaque = id;
152
153         return (0);
154 }
155
156 int
157 evDeselectFD(evContext opaqueCtx, evFileID opaqueID) {
158         evContext_p *ctx = opaqueCtx.opaque;
159         evFile *del = opaqueID.opaque;
160         evFile *cur;
161         int mode, eventmask;
162
163         if (!del) {
164                 evPrintf(ctx, 11, "evDeselectFD(NULL) ignored\n");
165                 errno = EINVAL;
166                 return (-1);
167         }
168
169         evPrintf(ctx, 1, "evDeselectFD(fd %d, mask 0x%x)\n",
170                  del->fd, del->eventmask);
171
172         /* Get the mode.  Unless the file has been closed, errors are bad. */
173         mode = fcntl(del->fd, F_GETFL, NULL);
174         if (mode == -1 && errno != EBADF)
175                 EV_ERR(errno);
176
177         /* Remove from the list of files. */
178         if (del->prev != NULL)
179                 del->prev->next = del->next;
180         else
181                 ctx->files = del->next;
182         if (del->next != NULL)
183                 del->next->prev = del->prev;
184
185         /* Remove from the fd table. */
186         if (del->fdprev != NULL)
187                 del->fdprev->fdnext = del->fdnext;
188         else
189                 ctx->fdTable[del->fd] = del->fdnext;
190         if (del->fdnext != NULL)
191                 del->fdnext->fdprev = del->fdprev;
192
193         /*
194          * If the file descriptor does not appear in any other select() entry,
195          * and if !EV_WASNONBLOCK, and if we got no EBADF when we got the mode
196          * earlier, then: restore the fd to blocking status.
197          */
198         if (!(cur = FindFD(ctx, del->fd, EV_MASK_ALL)) &&
199             !FD_ISSET(del->fd, &ctx->nonblockBefore) &&
200             mode != -1) {
201                 /*
202                  * Note that we won't return an error status to the caller if
203                  * this fcntl() fails since (a) we've already done the work
204                  * and (b) the caller didn't ask us anything about O_NONBLOCK.
205                  */
206 #ifdef USE_FIONBIO_IOCTL
207                 int off = 0;
208                 (void) ioctl(del->fd, FIONBIO, (char *)&off);
209 #else
210                 (void) fcntl(del->fd, F_SETFL, mode & ~PORT_NONBLOCK);
211 #endif
212         }
213
214         /*
215          * Now find all other uses of this descriptor and OR together an event
216          * mask so that we don't turn off {rd,wr,ex}Next bits that some other
217          * file event is using.  As an optimization, stop if the event mask
218          * fills.
219          */
220         eventmask = 0;
221         for ((void)NULL;
222              cur != NULL && eventmask != EV_MASK_ALL;
223              cur = cur->next)
224                 if (cur->fd == del->fd)
225                         eventmask |= cur->eventmask;
226
227         /* OK, now we know which bits we can clear out. */
228         if (!(eventmask & EV_READ)) {
229                 FD_CLR(del->fd, &ctx->rdNext);
230                 if (FD_ISSET(del->fd, &ctx->rdLast)) {
231                         FD_CLR(del->fd, &ctx->rdLast);
232                         ctx->fdCount--;
233                 }
234         }
235         if (!(eventmask & EV_WRITE)) {
236                 FD_CLR(del->fd, &ctx->wrNext);
237                 if (FD_ISSET(del->fd, &ctx->wrLast)) {
238                         FD_CLR(del->fd, &ctx->wrLast);
239                         ctx->fdCount--;
240                 }
241         }
242         if (!(eventmask & EV_EXCEPT)) {
243                 FD_CLR(del->fd, &ctx->exNext);
244                 if (FD_ISSET(del->fd, &ctx->exLast)) {
245                         FD_CLR(del->fd, &ctx->exLast);
246                         ctx->fdCount--;
247                 }
248         }
249
250         /* If this was the maxFD, find the new one. */
251         if (del->fd == ctx->fdMax) {
252                 ctx->fdMax = -1;
253                 for (cur = ctx->files; cur; cur = cur->next)
254                         if (cur->fd > ctx->fdMax)
255                                 ctx->fdMax = cur->fd;
256         }
257
258         /* If this was the fdNext, cycle that to the next entry. */
259         if (del == ctx->fdNext)
260                 ctx->fdNext = del->next;
261
262         /* Couldn't free it before now since we were using fields out of it. */
263         FREE(del);
264
265         return (0);
266 }
267
268 static evFile *
269 FindFD(const evContext_p *ctx, int fd, int eventmask) {
270         evFile *id;
271
272         for (id = ctx->fdTable[fd]; id != NULL; id = id->fdnext)
273                 if (id->fd == fd && (id->eventmask & eventmask) != 0)
274                         break;
275         return (id);
276 }