1 /* Copyright (c) 1995, 1996, 1997, 1998 by Internet Software Consortium
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.
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
17 /* ev_files.c - implement asynch file IO for the eventlib
18 * vix 11sep95 [initial]
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 $";
25 #include "port_before.h"
26 #include "fd_setsize.h"
28 #include <sys/types.h>
35 #include <isc/eventlib.h>
36 #include "eventlib_p.h"
38 #include "port_after.h"
40 static evFile *FindFD(const evContext_p *ctx, int fd, int eventmask);
43 evSelectFD(evContext opaqueCtx,
50 evContext_p *ctx = opaqueCtx.opaque;
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)
61 OK(mode = fcntl(fd, F_GETFL, NULL)); /* side effect: validate fd. */
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.
69 id = FindFD(ctx, fd, EV_MASK_ALL);
71 if (mode & O_NONBLOCK)
72 FD_SET(fd, &ctx->nonblockBefore);
74 OK(fcntl(fd, F_SETFL, mode | O_NONBLOCK));
75 FD_CLR(fd, &ctx->nonblockBefore);
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
85 if (id != NULL && FindFD(ctx, fd, eventmask) != NULL)
88 /* Allocate and fill. */
93 id->eventmask = eventmask;
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.
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.''
110 if (ctx->files != NULL)
111 ctx->files->prev = id;
113 id->next = ctx->files;
116 /* Insert into fd table. */
117 if (ctx->fdTable[fd] != NULL)
118 ctx->fdTable[fd]->fdprev = id;
120 id->fdnext = ctx->fdTable[fd];
121 ctx->fdTable[fd] = id;
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);
135 /* Remember the ID if the caller provided us a place for it. */
137 opaqueID->opaque = id;
140 "evSelectFD(fd %d, mask 0x%x): new masks: 0x%lx 0x%lx 0x%lx\n",
142 (u_long)ctx->rdNext.fds_bits[0],
143 (u_long)ctx->wrNext.fds_bits[0],
144 (u_long)ctx->exNext.fds_bits[0]);
150 evDeselectFD(evContext opaqueCtx, evFileID opaqueID) {
151 evContext_p *ctx = opaqueCtx.opaque;
152 evFile *del = opaqueID.opaque;
157 evPrintf(ctx, 11, "evDeselectFD(NULL) ignored\n");
162 evPrintf(ctx, 1, "evDeselectFD(fd %d, mask 0x%x)\n",
163 del->fd, del->eventmask);
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)
170 /* Remove from the list of files. */
171 if (del->prev != NULL)
172 del->prev->next = del->next;
174 ctx->files = del->next;
175 if (del->next != NULL)
176 del->next->prev = del->prev;
178 /* Remove from the fd table. */
179 if (del->fdprev != NULL)
180 del->fdprev->fdnext = del->fdnext;
182 ctx->fdTable[del->fd] = del->fdnext;
183 if (del->fdnext != NULL)
184 del->fdnext->fdprev = del->fdprev;
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.
191 if (!(cur = FindFD(ctx, del->fd, EV_MASK_ALL)) &&
192 !FD_ISSET(del->fd, &ctx->nonblockBefore) &&
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.
199 (void) fcntl(del->fd, F_SETFL, mode & ~O_NONBLOCK);
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
210 cur != NULL && eventmask != EV_MASK_ALL;
212 if (cur->fd == del->fd)
213 eventmask |= cur->eventmask;
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);
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);
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);
238 /* If this was the maxFD, find the new one. */
239 if (del->fd == ctx->fdMax) {
241 for (cur = ctx->files; cur; cur = cur->next)
242 if (cur->fd > ctx->fdMax)
243 ctx->fdMax = cur->fd;
246 /* If this was the fdNext, cycle that to the next entry. */
247 if (del == ctx->fdNext)
248 ctx->fdNext = del->next;
251 "evDeselectFD(fd %d, mask 0x%x): new masks: 0x%lx 0x%lx 0x%lx\n",
253 (u_long)ctx->rdNext.fds_bits[0],
254 (u_long)ctx->wrNext.fds_bits[0],
255 (u_long)ctx->exNext.fds_bits[0]);
257 /* Couldn't free it before now since we were using fields out of it. */
264 FindFD(const evContext_p *ctx, int fd, int eventmask) {
267 for (id = ctx->fdTable[fd]; id != NULL; id = id->fdnext)
268 if (id->fd == fd && (id->eventmask & eventmask) != 0)