17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3*9525b14bSRao Shoaib * Copyright (c) 1995-1999 by Internet Software Consortium
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate *
9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate /* ev_files.c - implement asynch file IO for the eventlib
197c478bd9Sstevel@tonic-gate * vix 11sep95 [initial]
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate #include "port_before.h"
237c478bd9Sstevel@tonic-gate #include "fd_setsize.h"
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate #include <sys/types.h>
267c478bd9Sstevel@tonic-gate #include <sys/time.h>
277c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <fcntl.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include <isc/eventlib.h>
347c478bd9Sstevel@tonic-gate #include "eventlib_p.h"
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include "port_after.h"
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate static evFile *FindFD(const evContext_p *ctx, int fd, int eventmask);
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate int
evSelectFD(evContext opaqueCtx,int fd,int eventmask,evFileFunc func,void * uap,evFileID * opaqueID)417c478bd9Sstevel@tonic-gate evSelectFD(evContext opaqueCtx,
427c478bd9Sstevel@tonic-gate int fd,
437c478bd9Sstevel@tonic-gate int eventmask,
447c478bd9Sstevel@tonic-gate evFileFunc func,
457c478bd9Sstevel@tonic-gate void *uap,
467c478bd9Sstevel@tonic-gate evFileID *opaqueID
477c478bd9Sstevel@tonic-gate ) {
487c478bd9Sstevel@tonic-gate evContext_p *ctx = opaqueCtx.opaque;
497c478bd9Sstevel@tonic-gate evFile *id;
507c478bd9Sstevel@tonic-gate int mode;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate evPrintf(ctx, 1,
537c478bd9Sstevel@tonic-gate "evSelectFD(ctx %p, fd %d, mask 0x%x, func %p, uap %p)\n",
547c478bd9Sstevel@tonic-gate ctx, fd, eventmask, func, uap);
557c478bd9Sstevel@tonic-gate if (eventmask == 0 || (eventmask & ~EV_MASK_ALL) != 0)
567c478bd9Sstevel@tonic-gate EV_ERR(EINVAL);
57*9525b14bSRao Shoaib #ifndef USE_POLL
587c478bd9Sstevel@tonic-gate if (fd > ctx->highestFD)
597c478bd9Sstevel@tonic-gate EV_ERR(EINVAL);
60*9525b14bSRao Shoaib #endif
61*9525b14bSRao Shoaib OK(mode = fcntl(fd, F_GETFL, NULL)); /*%< side effect: validate fd. */
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate * The first time we touch a file descriptor, we need to check to see
647c478bd9Sstevel@tonic-gate * if the application already had it in O_NONBLOCK mode and if so, all
657c478bd9Sstevel@tonic-gate * of our deselect()'s have to leave it in O_NONBLOCK. If not, then
667c478bd9Sstevel@tonic-gate * all but our last deselect() has to leave it in O_NONBLOCK.
677c478bd9Sstevel@tonic-gate */
68*9525b14bSRao Shoaib #ifdef USE_POLL
697c478bd9Sstevel@tonic-gate /* Make sure both ctx->pollfds[] and ctx->fdTable[] are large enough */
70*9525b14bSRao Shoaib if (fd >= ctx->maxnfds && evPollfdRealloc(ctx, 1, fd) != 0)
71*9525b14bSRao Shoaib EV_ERR(ENOMEM);
72*9525b14bSRao Shoaib #endif /* USE_POLL */
737c478bd9Sstevel@tonic-gate id = FindFD(ctx, fd, EV_MASK_ALL);
747c478bd9Sstevel@tonic-gate if (id == NULL) {
757c478bd9Sstevel@tonic-gate if (mode & PORT_NONBLOCK)
767c478bd9Sstevel@tonic-gate FD_SET(fd, &ctx->nonblockBefore);
777c478bd9Sstevel@tonic-gate else {
787c478bd9Sstevel@tonic-gate #ifdef USE_FIONBIO_IOCTL
797c478bd9Sstevel@tonic-gate int on = 1;
807c478bd9Sstevel@tonic-gate OK(ioctl(fd, FIONBIO, (char *)&on));
817c478bd9Sstevel@tonic-gate #else
827c478bd9Sstevel@tonic-gate OK(fcntl(fd, F_SETFL, mode | PORT_NONBLOCK));
837c478bd9Sstevel@tonic-gate #endif
847c478bd9Sstevel@tonic-gate FD_CLR(fd, &ctx->nonblockBefore);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate * If this descriptor is already in use, search for it again to see
907c478bd9Sstevel@tonic-gate * if any of the eventmask bits we want to set are already captured.
917c478bd9Sstevel@tonic-gate * We cannot usefully capture the same fd event more than once in the
927c478bd9Sstevel@tonic-gate * same context.
937c478bd9Sstevel@tonic-gate */
947c478bd9Sstevel@tonic-gate if (id != NULL && FindFD(ctx, fd, eventmask) != NULL)
957c478bd9Sstevel@tonic-gate EV_ERR(ETOOMANYREFS);
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate /* Allocate and fill. */
987c478bd9Sstevel@tonic-gate OKNEW(id);
997c478bd9Sstevel@tonic-gate id->func = func;
1007c478bd9Sstevel@tonic-gate id->uap = uap;
1017c478bd9Sstevel@tonic-gate id->fd = fd;
1027c478bd9Sstevel@tonic-gate id->eventmask = eventmask;
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate * Insert at head. Order could be important for performance if we
1067c478bd9Sstevel@tonic-gate * believe that evGetNext()'s accesses to the fd_sets will be more
1077c478bd9Sstevel@tonic-gate * serial and therefore more cache-lucky if the list is ordered by
1087c478bd9Sstevel@tonic-gate * ``fd.'' We do not believe these things, so we don't do it.
1097c478bd9Sstevel@tonic-gate *
1107c478bd9Sstevel@tonic-gate * The interesting sequence is where GetNext() has cached a select()
1117c478bd9Sstevel@tonic-gate * result and the caller decides to evSelectFD() on some descriptor.
1127c478bd9Sstevel@tonic-gate * Since GetNext() starts at the head, it can miss new entries we add
1137c478bd9Sstevel@tonic-gate * at the head. This is not a serious problem since the event being
1147c478bd9Sstevel@tonic-gate * evSelectFD()'d for has to occur before evSelectFD() is called for
1157c478bd9Sstevel@tonic-gate * the file event to be considered "missed" -- a real corner case.
1167c478bd9Sstevel@tonic-gate * Maintaining a "tail" pointer for ctx->files would fix this, but I'm
1177c478bd9Sstevel@tonic-gate * not sure it would be ``more correct.''
1187c478bd9Sstevel@tonic-gate */
1197c478bd9Sstevel@tonic-gate if (ctx->files != NULL)
1207c478bd9Sstevel@tonic-gate ctx->files->prev = id;
1217c478bd9Sstevel@tonic-gate id->prev = NULL;
1227c478bd9Sstevel@tonic-gate id->next = ctx->files;
1237c478bd9Sstevel@tonic-gate ctx->files = id;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate /* Insert into fd table. */
1267c478bd9Sstevel@tonic-gate if (ctx->fdTable[fd] != NULL)
1277c478bd9Sstevel@tonic-gate ctx->fdTable[fd]->fdprev = id;
1287c478bd9Sstevel@tonic-gate id->fdprev = NULL;
1297c478bd9Sstevel@tonic-gate id->fdnext = ctx->fdTable[fd];
1307c478bd9Sstevel@tonic-gate ctx->fdTable[fd] = id;
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate /* Turn on the appropriate bits in the {rd,wr,ex}Next fd_set's. */
1337c478bd9Sstevel@tonic-gate if (eventmask & EV_READ)
1347c478bd9Sstevel@tonic-gate FD_SET(fd, &ctx->rdNext);
1357c478bd9Sstevel@tonic-gate if (eventmask & EV_WRITE)
1367c478bd9Sstevel@tonic-gate FD_SET(fd, &ctx->wrNext);
1377c478bd9Sstevel@tonic-gate if (eventmask & EV_EXCEPT)
1387c478bd9Sstevel@tonic-gate FD_SET(fd, &ctx->exNext);
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /* Update fdMax. */
1417c478bd9Sstevel@tonic-gate if (fd > ctx->fdMax)
1427c478bd9Sstevel@tonic-gate ctx->fdMax = fd;
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate /* Remember the ID if the caller provided us a place for it. */
1457c478bd9Sstevel@tonic-gate if (opaqueID)
1467c478bd9Sstevel@tonic-gate opaqueID->opaque = id;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate return (0);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate int
evDeselectFD(evContext opaqueCtx,evFileID opaqueID)1527c478bd9Sstevel@tonic-gate evDeselectFD(evContext opaqueCtx, evFileID opaqueID) {
1537c478bd9Sstevel@tonic-gate evContext_p *ctx = opaqueCtx.opaque;
1547c478bd9Sstevel@tonic-gate evFile *del = opaqueID.opaque;
1557c478bd9Sstevel@tonic-gate evFile *cur;
1567c478bd9Sstevel@tonic-gate int mode, eventmask;
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate if (!del) {
1597c478bd9Sstevel@tonic-gate evPrintf(ctx, 11, "evDeselectFD(NULL) ignored\n");
1607c478bd9Sstevel@tonic-gate errno = EINVAL;
1617c478bd9Sstevel@tonic-gate return (-1);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate evPrintf(ctx, 1, "evDeselectFD(fd %d, mask 0x%x)\n",
1657c478bd9Sstevel@tonic-gate del->fd, del->eventmask);
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate /* Get the mode. Unless the file has been closed, errors are bad. */
1687c478bd9Sstevel@tonic-gate mode = fcntl(del->fd, F_GETFL, NULL);
1697c478bd9Sstevel@tonic-gate if (mode == -1 && errno != EBADF)
1707c478bd9Sstevel@tonic-gate EV_ERR(errno);
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate /* Remove from the list of files. */
1737c478bd9Sstevel@tonic-gate if (del->prev != NULL)
1747c478bd9Sstevel@tonic-gate del->prev->next = del->next;
1757c478bd9Sstevel@tonic-gate else
1767c478bd9Sstevel@tonic-gate ctx->files = del->next;
1777c478bd9Sstevel@tonic-gate if (del->next != NULL)
1787c478bd9Sstevel@tonic-gate del->next->prev = del->prev;
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate /* Remove from the fd table. */
1817c478bd9Sstevel@tonic-gate if (del->fdprev != NULL)
1827c478bd9Sstevel@tonic-gate del->fdprev->fdnext = del->fdnext;
1837c478bd9Sstevel@tonic-gate else
1847c478bd9Sstevel@tonic-gate ctx->fdTable[del->fd] = del->fdnext;
1857c478bd9Sstevel@tonic-gate if (del->fdnext != NULL)
1867c478bd9Sstevel@tonic-gate del->fdnext->fdprev = del->fdprev;
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate * If the file descriptor does not appear in any other select() entry,
1907c478bd9Sstevel@tonic-gate * and if !EV_WASNONBLOCK, and if we got no EBADF when we got the mode
1917c478bd9Sstevel@tonic-gate * earlier, then: restore the fd to blocking status.
1927c478bd9Sstevel@tonic-gate */
1937c478bd9Sstevel@tonic-gate if (!(cur = FindFD(ctx, del->fd, EV_MASK_ALL)) &&
1947c478bd9Sstevel@tonic-gate !FD_ISSET(del->fd, &ctx->nonblockBefore) &&
1957c478bd9Sstevel@tonic-gate mode != -1) {
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate * Note that we won't return an error status to the caller if
1987c478bd9Sstevel@tonic-gate * this fcntl() fails since (a) we've already done the work
1997c478bd9Sstevel@tonic-gate * and (b) the caller didn't ask us anything about O_NONBLOCK.
2007c478bd9Sstevel@tonic-gate */
2017c478bd9Sstevel@tonic-gate #ifdef USE_FIONBIO_IOCTL
202*9525b14bSRao Shoaib int off = 0;
2037c478bd9Sstevel@tonic-gate (void) ioctl(del->fd, FIONBIO, (char *)&off);
2047c478bd9Sstevel@tonic-gate #else
2057c478bd9Sstevel@tonic-gate (void) fcntl(del->fd, F_SETFL, mode & ~PORT_NONBLOCK);
2067c478bd9Sstevel@tonic-gate #endif
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate * Now find all other uses of this descriptor and OR together an event
2117c478bd9Sstevel@tonic-gate * mask so that we don't turn off {rd,wr,ex}Next bits that some other
2127c478bd9Sstevel@tonic-gate * file event is using. As an optimization, stop if the event mask
2137c478bd9Sstevel@tonic-gate * fills.
2147c478bd9Sstevel@tonic-gate */
2157c478bd9Sstevel@tonic-gate eventmask = 0;
2167c478bd9Sstevel@tonic-gate for ((void)NULL;
2177c478bd9Sstevel@tonic-gate cur != NULL && eventmask != EV_MASK_ALL;
2187c478bd9Sstevel@tonic-gate cur = cur->next)
2197c478bd9Sstevel@tonic-gate if (cur->fd == del->fd)
2207c478bd9Sstevel@tonic-gate eventmask |= cur->eventmask;
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate /* OK, now we know which bits we can clear out. */
2237c478bd9Sstevel@tonic-gate if (!(eventmask & EV_READ)) {
2247c478bd9Sstevel@tonic-gate FD_CLR(del->fd, &ctx->rdNext);
2257c478bd9Sstevel@tonic-gate if (FD_ISSET(del->fd, &ctx->rdLast)) {
2267c478bd9Sstevel@tonic-gate FD_CLR(del->fd, &ctx->rdLast);
2277c478bd9Sstevel@tonic-gate ctx->fdCount--;
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate if (!(eventmask & EV_WRITE)) {
2317c478bd9Sstevel@tonic-gate FD_CLR(del->fd, &ctx->wrNext);
2327c478bd9Sstevel@tonic-gate if (FD_ISSET(del->fd, &ctx->wrLast)) {
2337c478bd9Sstevel@tonic-gate FD_CLR(del->fd, &ctx->wrLast);
2347c478bd9Sstevel@tonic-gate ctx->fdCount--;
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate if (!(eventmask & EV_EXCEPT)) {
2387c478bd9Sstevel@tonic-gate FD_CLR(del->fd, &ctx->exNext);
2397c478bd9Sstevel@tonic-gate if (FD_ISSET(del->fd, &ctx->exLast)) {
2407c478bd9Sstevel@tonic-gate FD_CLR(del->fd, &ctx->exLast);
2417c478bd9Sstevel@tonic-gate ctx->fdCount--;
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate /* If this was the maxFD, find the new one. */
2467c478bd9Sstevel@tonic-gate if (del->fd == ctx->fdMax) {
2477c478bd9Sstevel@tonic-gate ctx->fdMax = -1;
2487c478bd9Sstevel@tonic-gate for (cur = ctx->files; cur; cur = cur->next)
2497c478bd9Sstevel@tonic-gate if (cur->fd > ctx->fdMax)
2507c478bd9Sstevel@tonic-gate ctx->fdMax = cur->fd;
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate /* If this was the fdNext, cycle that to the next entry. */
2547c478bd9Sstevel@tonic-gate if (del == ctx->fdNext)
2557c478bd9Sstevel@tonic-gate ctx->fdNext = del->next;
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate /* Couldn't free it before now since we were using fields out of it. */
2587c478bd9Sstevel@tonic-gate FREE(del);
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate return (0);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate static evFile *
FindFD(const evContext_p * ctx,int fd,int eventmask)2647c478bd9Sstevel@tonic-gate FindFD(const evContext_p *ctx, int fd, int eventmask) {
2657c478bd9Sstevel@tonic-gate evFile *id;
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate for (id = ctx->fdTable[fd]; id != NULL; id = id->fdnext)
2687c478bd9Sstevel@tonic-gate if (id->fd == fd && (id->eventmask & eventmask) != 0)
2697c478bd9Sstevel@tonic-gate break;
2707c478bd9Sstevel@tonic-gate return (id);
2717c478bd9Sstevel@tonic-gate }
272*9525b14bSRao Shoaib
273*9525b14bSRao Shoaib /*! \file */
274