Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / contrib / bind-9.2.4rc7 / 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.2 2004/03/09 09:17:35 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         if (fd > ctx->highestFD)
62                 EV_ERR(EINVAL);
63         OK(mode = fcntl(fd, F_GETFL, NULL));    /* side effect: validate fd. */
64
65         /*
66          * The first time we touch a file descriptor, we need to check to see
67          * if the application already had it in O_NONBLOCK mode and if so, all
68          * of our deselect()'s have to leave it in O_NONBLOCK.  If not, then
69          * all but our last deselect() has to leave it in O_NONBLOCK.
70          */
71         id = FindFD(ctx, fd, EV_MASK_ALL);
72         if (id == NULL) {
73                 if (mode & PORT_NONBLOCK)
74                         FD_SET(fd, &ctx->nonblockBefore);
75                 else {
76 #ifdef USE_FIONBIO_IOCTL
77                         int on = 1;
78                         OK(ioctl(fd, FIONBIO, (char *)&on));
79 #else
80                         OK(fcntl(fd, F_SETFL, mode | PORT_NONBLOCK));
81 #endif
82                         FD_CLR(fd, &ctx->nonblockBefore);
83                 }
84         }
85
86         /*
87          * If this descriptor is already in use, search for it again to see
88          * if any of the eventmask bits we want to set are already captured.
89          * We cannot usefully capture the same fd event more than once in the
90          * same context.
91          */
92         if (id != NULL && FindFD(ctx, fd, eventmask) != NULL)
93                 EV_ERR(ETOOMANYREFS);
94
95         /* Allocate and fill. */
96         OKNEW(id);
97         id->func = func;
98         id->uap = uap;
99         id->fd = fd;
100         id->eventmask = eventmask;
101
102         /*
103          * Insert at head.  Order could be important for performance if we
104          * believe that evGetNext()'s accesses to the fd_sets will be more
105          * serial and therefore more cache-lucky if the list is ordered by
106          * ``fd.''  We do not believe these things, so we don't do it.
107          *
108          * The interesting sequence is where GetNext() has cached a select()
109          * result and the caller decides to evSelectFD() on some descriptor.
110          * Since GetNext() starts at the head, it can miss new entries we add
111          * at the head.  This is not a serious problem since the event being
112          * evSelectFD()'d for has to occur before evSelectFD() is called for
113          * the file event to be considered "missed" -- a real corner case.
114          * Maintaining a "tail" pointer for ctx->files would fix this, but I'm
115          * not sure it would be ``more correct.''
116          */
117         if (ctx->files != NULL)
118                 ctx->files->prev = id;
119         id->prev = NULL;
120         id->next = ctx->files;
121         ctx->files = id;
122
123         /* Insert into fd table. */
124         if (ctx->fdTable[fd] != NULL)
125                 ctx->fdTable[fd]->fdprev = id;
126         id->fdprev = NULL;
127         id->fdnext = ctx->fdTable[fd];
128         ctx->fdTable[fd] = id;
129
130         /* Turn on the appropriate bits in the {rd,wr,ex}Next fd_set's. */
131         if (eventmask & EV_READ)
132                 FD_SET(fd, &ctx->rdNext);
133         if (eventmask & EV_WRITE)
134                 FD_SET(fd, &ctx->wrNext);
135         if (eventmask & EV_EXCEPT)
136                 FD_SET(fd, &ctx->exNext);
137
138         /* Update fdMax. */
139         if (fd > ctx->fdMax)
140                 ctx->fdMax = fd;
141
142         /* Remember the ID if the caller provided us a place for it. */
143         if (opaqueID)
144                 opaqueID->opaque = id;
145
146         evPrintf(ctx, 5,
147                 "evSelectFD(fd %d, mask 0x%x): new masks: 0x%lx 0x%lx 0x%lx\n",
148                  fd, eventmask,
149                  (u_long)ctx->rdNext.fds_bits[0],
150                  (u_long)ctx->wrNext.fds_bits[0],
151                  (u_long)ctx->exNext.fds_bits[0]);
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 = 1;
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         evPrintf(ctx, 5,
263               "evDeselectFD(fd %d, mask 0x%x): new masks: 0x%lx 0x%lx 0x%lx\n",
264                  del->fd, eventmask,
265                  (u_long)ctx->rdNext.fds_bits[0],
266                  (u_long)ctx->wrNext.fds_bits[0],
267                  (u_long)ctx->exNext.fds_bits[0]);
268
269         /* Couldn't free it before now since we were using fields out of it. */
270         FREE(del);
271
272         return (0);
273 }
274
275 static evFile *
276 FindFD(const evContext_p *ctx, int fd, int eventmask) {
277         evFile *id;
278
279         for (id = ctx->fdTable[fd]; id != NULL; id = id->fdnext)
280                 if (id->fd == fd && (id->eventmask & eventmask) != 0)
281                         break;
282         return (id);
283 }