2N/A * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 2N/A * Copyright (c) 1995-1999 by Internet Software Consortium 2N/A * Permission to use, copy, modify, and distribute this software for any 2N/A * purpose with or without fee is hereby granted, provided that the above 2N/A * copyright notice and this permission notice appear in all copies. 2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 2N/A * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 2N/A * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 2N/A * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 2N/A * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 2N/A * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 2N/A * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 2N/A * vix 11sep95 [initial] 2N/A "evSelectFD(ctx %p, fd %d, mask 0x%x, func %p, uap %p)\n",
2N/A * The first time we touch a file descriptor, we need to check to see 2N/A * if the application already had it in O_NONBLOCK mode and if so, all 2N/A * of our deselect()'s have to leave it in O_NONBLOCK. If not, then 2N/A * all but our last deselect() has to leave it in O_NONBLOCK. 2N/A /* Make sure both ctx->pollfds[] and ctx->fdTable[] are large enough */ 2N/A#
endif /* USE_POLL */ 2N/A * If this descriptor is already in use, search for it again to see 2N/A * if any of the eventmask bits we want to set are already captured. 2N/A * We cannot usefully capture the same fd event more than once in the 2N/A /* Allocate and fill. */ 2N/A * Insert at head. Order could be important for performance if we 2N/A * believe that evGetNext()'s accesses to the fd_sets will be more 2N/A * serial and therefore more cache-lucky if the list is ordered by 2N/A * ``fd.'' We do not believe these things, so we don't do it. 2N/A * The interesting sequence is where GetNext() has cached a select() 2N/A * result and the caller decides to evSelectFD() on some descriptor. 2N/A * Since GetNext() starts at the head, it can miss new entries we add 2N/A * at the head. This is not a serious problem since the event being 2N/A * evSelectFD()'d for has to occur before evSelectFD() is called for 2N/A * the file event to be considered "missed" -- a real corner case. 2N/A * Maintaining a "tail" pointer for ctx->files would fix this, but I'm 2N/A * not sure it would be ``more correct.'' 2N/A /* Insert into fd table. */ 2N/A /* Turn on the appropriate bits in the {rd,wr,ex}Next fd_set's. */ 2N/A /* Remember the ID if the caller provided us a place for it. */ 2N/A /* Get the mode. Unless the file has been closed, errors are bad. */ 2N/A /* Remove from the list of files. */ 2N/A /* Remove from the fd table. */ 2N/A * If the file descriptor does not appear in any other select() entry, 2N/A * and if !EV_WASNONBLOCK, and if we got no EBADF when we got the mode 2N/A * earlier, then: restore the fd to blocking status. 2N/A * Note that we won't return an error status to the caller if 2N/A * this fcntl() fails since (a) we've already done the work 2N/A * and (b) the caller didn't ask us anything about O_NONBLOCK. 2N/A * Now find all other uses of this descriptor and OR together an event 2N/A * mask so that we don't turn off {rd,wr,ex}Next bits that some other 2N/A * file event is using. As an optimization, stop if the event mask 2N/A /* OK, now we know which bits we can clear out. */ 2N/A /* If this was the maxFD, find the new one. */ 2N/A /* If this was the fdNext, cycle that to the next entry. */ 2N/A /* Couldn't free it before now since we were using fields out of it. */