2 * Copyright (c) 1995-1999 by Internet Software Consortium
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
18 /* ev_files.c - implement asynch file IO for the eventlib
19 * vix 11sep95 [initial]
22 #if !defined(LINT) && !defined(CODECENTER)
23 static const char rcsid[] = "$Id: ev_files.c,v 1.19 1999/10/07 20:44:04 vixie Exp $";
26 #include "port_before.h"
27 #include "fd_setsize.h"
29 #include <sys/types.h>
36 #include <isc/eventlib.h>
37 #include "eventlib_p.h"
39 #include "port_after.h"
41 static evFile *FindFD(const evContext_p *ctx, int fd, int eventmask);
44 evSelectFD(evContext opaqueCtx,
51 evContext_p *ctx = opaqueCtx.opaque;
56 "evSelectFD(ctx %#x, fd %d, mask 0x%x, func %#x, uap %#x)\n",
57 ctx, fd, eventmask, func, uap);
58 if (eventmask == 0 || (eventmask & ~EV_MASK_ALL) != 0)
60 if (fd > ctx->highestFD)
62 OK(mode = fcntl(fd, F_GETFL, NULL)); /* side effect: validate fd. */
65 * The first time we touch a file descriptor, we need to check to see
66 * if the application already had it in O_NONBLOCK mode and if so, all
67 * of our deselect()'s have to leave it in O_NONBLOCK. If not, then
68 * all but our last deselect() has to leave it in O_NONBLOCK.
70 id = FindFD(ctx, fd, EV_MASK_ALL);
72 if (mode & PORT_NONBLOCK)
73 FD_SET(fd, &ctx->nonblockBefore);
75 OK(fcntl(fd, F_SETFL, mode | PORT_NONBLOCK));
76 FD_CLR(fd, &ctx->nonblockBefore);
81 * If this descriptor is already in use, search for it again to see
82 * if any of the eventmask bits we want to set are already captured.
83 * We cannot usefully capture the same fd event more than once in the
86 if (id != NULL && FindFD(ctx, fd, eventmask) != NULL)
89 /* Allocate and fill. */
94 id->eventmask = eventmask;
97 * Insert at head. Order could be important for performance if we
98 * believe that evGetNext()'s accesses to the fd_sets will be more
99 * serial and therefore more cache-lucky if the list is ordered by
100 * ``fd.'' We do not believe these things, so we don't do it.
102 * The interesting sequence is where GetNext() has cached a select()
103 * result and the caller decides to evSelectFD() on some descriptor.
104 * Since GetNext() starts at the head, it can miss new entries we add
105 * at the head. This is not a serious problem since the event being
106 * evSelectFD()'d for has to occur before evSelectFD() is called for
107 * the file event to be considered "missed" -- a real corner case.
108 * Maintaining a "tail" pointer for ctx->files would fix this, but I'm
109 * not sure it would be ``more correct.''
111 if (ctx->files != NULL)
112 ctx->files->prev = id;
114 id->next = ctx->files;
117 /* Insert into fd table. */
118 if (ctx->fdTable[fd] != NULL)
119 ctx->fdTable[fd]->fdprev = id;
121 id->fdnext = ctx->fdTable[fd];
122 ctx->fdTable[fd] = id;
124 /* Turn on the appropriate bits in the {rd,wr,ex}Next fd_set's. */
125 if (eventmask & EV_READ)
126 FD_SET(fd, &ctx->rdNext);
127 if (eventmask & EV_WRITE)
128 FD_SET(fd, &ctx->wrNext);
129 if (eventmask & EV_EXCEPT)
130 FD_SET(fd, &ctx->exNext);
136 /* Remember the ID if the caller provided us a place for it. */
138 opaqueID->opaque = id;
141 "evSelectFD(fd %d, mask 0x%x): new masks: 0x%lx 0x%lx 0x%lx\n",
143 (u_long)ctx->rdNext.fds_bits[0],
144 (u_long)ctx->wrNext.fds_bits[0],
145 (u_long)ctx->exNext.fds_bits[0]);
151 evDeselectFD(evContext opaqueCtx, evFileID opaqueID) {
152 evContext_p *ctx = opaqueCtx.opaque;
153 evFile *del = opaqueID.opaque;
158 evPrintf(ctx, 11, "evDeselectFD(NULL) ignored\n");
163 evPrintf(ctx, 1, "evDeselectFD(fd %d, mask 0x%x)\n",
164 del->fd, del->eventmask);
166 /* Get the mode. Unless the file has been closed, errors are bad. */
167 mode = fcntl(del->fd, F_GETFL, NULL);
168 if (mode == -1 && errno != EBADF)
171 /* Remove from the list of files. */
172 if (del->prev != NULL)
173 del->prev->next = del->next;
175 ctx->files = del->next;
176 if (del->next != NULL)
177 del->next->prev = del->prev;
179 /* Remove from the fd table. */
180 if (del->fdprev != NULL)
181 del->fdprev->fdnext = del->fdnext;
183 ctx->fdTable[del->fd] = del->fdnext;
184 if (del->fdnext != NULL)
185 del->fdnext->fdprev = del->fdprev;
188 * If the file descriptor does not appear in any other select() entry,
189 * and if !EV_WASNONBLOCK, and if we got no EBADF when we got the mode
190 * earlier, then: restore the fd to blocking status.
192 if (!(cur = FindFD(ctx, del->fd, EV_MASK_ALL)) &&
193 !FD_ISSET(del->fd, &ctx->nonblockBefore) &&
196 * Note that we won't return an error status to the caller if
197 * this fcntl() fails since (a) we've already done the work
198 * and (b) the caller didn't ask us anything about O_NONBLOCK.
200 (void) fcntl(del->fd, F_SETFL, mode & ~PORT_NONBLOCK);
204 * Now find all other uses of this descriptor and OR together an event
205 * mask so that we don't turn off {rd,wr,ex}Next bits that some other
206 * file event is using. As an optimization, stop if the event mask
211 cur != NULL && eventmask != EV_MASK_ALL;
213 if (cur->fd == del->fd)
214 eventmask |= cur->eventmask;
216 /* OK, now we know which bits we can clear out. */
217 if (!(eventmask & EV_READ)) {
218 FD_CLR(del->fd, &ctx->rdNext);
219 if (FD_ISSET(del->fd, &ctx->rdLast)) {
220 FD_CLR(del->fd, &ctx->rdLast);
224 if (!(eventmask & EV_WRITE)) {
225 FD_CLR(del->fd, &ctx->wrNext);
226 if (FD_ISSET(del->fd, &ctx->wrLast)) {
227 FD_CLR(del->fd, &ctx->wrLast);
231 if (!(eventmask & EV_EXCEPT)) {
232 FD_CLR(del->fd, &ctx->exNext);
233 if (FD_ISSET(del->fd, &ctx->exLast)) {
234 FD_CLR(del->fd, &ctx->exLast);
239 /* If this was the maxFD, find the new one. */
240 if (del->fd == ctx->fdMax) {
242 for (cur = ctx->files; cur; cur = cur->next)
243 if (cur->fd > ctx->fdMax)
244 ctx->fdMax = cur->fd;
247 /* If this was the fdNext, cycle that to the next entry. */
248 if (del == ctx->fdNext)
249 ctx->fdNext = del->next;
252 "evDeselectFD(fd %d, mask 0x%x): new masks: 0x%lx 0x%lx 0x%lx\n",
254 (u_long)ctx->rdNext.fds_bits[0],
255 (u_long)ctx->wrNext.fds_bits[0],
256 (u_long)ctx->exNext.fds_bits[0]);
258 /* Couldn't free it before now since we were using fields out of it. */
265 FindFD(const evContext_p *ctx, int fd, int eventmask) {
268 for (id = ctx->fdTable[fd]; id != NULL; id = id->fdnext)
269 if (id->fd == fd && (id->eventmask & eventmask) != 0)