1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 3*0Sstevel@tonic-gate * Use is subject to license terms. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate /* Copyright (c) 1995-1999 by Internet Software Consortium 7*0Sstevel@tonic-gate * 8*0Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 9*0Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 10*0Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 11*0Sstevel@tonic-gate * 12*0Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 13*0Sstevel@tonic-gate * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 14*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 15*0Sstevel@tonic-gate * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 16*0Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 17*0Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 18*0Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 19*0Sstevel@tonic-gate * SOFTWARE. 20*0Sstevel@tonic-gate */ 21*0Sstevel@tonic-gate 22*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 23*0Sstevel@tonic-gate 24*0Sstevel@tonic-gate /* ev_files.c - implement asynch file IO for the eventlib 25*0Sstevel@tonic-gate * vix 11sep95 [initial] 26*0Sstevel@tonic-gate */ 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER) 29*0Sstevel@tonic-gate static const char rcsid[] = "$Id: ev_files.c,v 1.22 2002/07/08 05:50:07 marka Exp $"; 30*0Sstevel@tonic-gate #endif 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate #include "port_before.h" 33*0Sstevel@tonic-gate #include "fd_setsize.h" 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #include <sys/types.h> 36*0Sstevel@tonic-gate #include <sys/time.h> 37*0Sstevel@tonic-gate #include <sys/ioctl.h> 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate #include <errno.h> 40*0Sstevel@tonic-gate #include <fcntl.h> 41*0Sstevel@tonic-gate #include <unistd.h> 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate #include <isc/eventlib.h> 44*0Sstevel@tonic-gate #include "eventlib_p.h" 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate #include "port_after.h" 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate static evFile *FindFD(const evContext_p *ctx, int fd, int eventmask); 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate int 51*0Sstevel@tonic-gate evSelectFD(evContext opaqueCtx, 52*0Sstevel@tonic-gate int fd, 53*0Sstevel@tonic-gate int eventmask, 54*0Sstevel@tonic-gate evFileFunc func, 55*0Sstevel@tonic-gate void *uap, 56*0Sstevel@tonic-gate evFileID *opaqueID 57*0Sstevel@tonic-gate ) { 58*0Sstevel@tonic-gate evContext_p *ctx = opaqueCtx.opaque; 59*0Sstevel@tonic-gate evFile *id; 60*0Sstevel@tonic-gate int mode; 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate evPrintf(ctx, 1, 63*0Sstevel@tonic-gate "evSelectFD(ctx %p, fd %d, mask 0x%x, func %p, uap %p)\n", 64*0Sstevel@tonic-gate ctx, fd, eventmask, func, uap); 65*0Sstevel@tonic-gate if (eventmask == 0 || (eventmask & ~EV_MASK_ALL) != 0) 66*0Sstevel@tonic-gate EV_ERR(EINVAL); 67*0Sstevel@tonic-gate #ifdef SUNW_POLL 68*0Sstevel@tonic-gate #else 69*0Sstevel@tonic-gate if (fd > ctx->highestFD) 70*0Sstevel@tonic-gate EV_ERR(EINVAL); 71*0Sstevel@tonic-gate #endif /* SUNW_POLL */ 72*0Sstevel@tonic-gate OK(mode = fcntl(fd, F_GETFL, NULL)); /* side effect: validate fd. */ 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate /* 75*0Sstevel@tonic-gate * The first time we touch a file descriptor, we need to check to see 76*0Sstevel@tonic-gate * if the application already had it in O_NONBLOCK mode and if so, all 77*0Sstevel@tonic-gate * of our deselect()'s have to leave it in O_NONBLOCK. If not, then 78*0Sstevel@tonic-gate * all but our last deselect() has to leave it in O_NONBLOCK. 79*0Sstevel@tonic-gate */ 80*0Sstevel@tonic-gate #ifdef SUNW_POLL 81*0Sstevel@tonic-gate /* Make sure both ctx->pollfds[] and ctx->fdTable[] are large enough */ 82*0Sstevel@tonic-gate if (fd >= ctx->maxnfds) 83*0Sstevel@tonic-gate evPollfdRealloc(ctx, 1, fd); 84*0Sstevel@tonic-gate #endif /* SUNW_POLL */ 85*0Sstevel@tonic-gate id = FindFD(ctx, fd, EV_MASK_ALL); 86*0Sstevel@tonic-gate if (id == NULL) { 87*0Sstevel@tonic-gate if (mode & PORT_NONBLOCK) 88*0Sstevel@tonic-gate FD_SET(fd, &ctx->nonblockBefore); 89*0Sstevel@tonic-gate else { 90*0Sstevel@tonic-gate #ifdef USE_FIONBIO_IOCTL 91*0Sstevel@tonic-gate int on = 1; 92*0Sstevel@tonic-gate OK(ioctl(fd, FIONBIO, (char *)&on)); 93*0Sstevel@tonic-gate #else 94*0Sstevel@tonic-gate OK(fcntl(fd, F_SETFL, mode | PORT_NONBLOCK)); 95*0Sstevel@tonic-gate #endif 96*0Sstevel@tonic-gate FD_CLR(fd, &ctx->nonblockBefore); 97*0Sstevel@tonic-gate } 98*0Sstevel@tonic-gate } 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate /* 101*0Sstevel@tonic-gate * If this descriptor is already in use, search for it again to see 102*0Sstevel@tonic-gate * if any of the eventmask bits we want to set are already captured. 103*0Sstevel@tonic-gate * We cannot usefully capture the same fd event more than once in the 104*0Sstevel@tonic-gate * same context. 105*0Sstevel@tonic-gate */ 106*0Sstevel@tonic-gate if (id != NULL && FindFD(ctx, fd, eventmask) != NULL) 107*0Sstevel@tonic-gate EV_ERR(ETOOMANYREFS); 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate /* Allocate and fill. */ 110*0Sstevel@tonic-gate OKNEW(id); 111*0Sstevel@tonic-gate id->func = func; 112*0Sstevel@tonic-gate id->uap = uap; 113*0Sstevel@tonic-gate id->fd = fd; 114*0Sstevel@tonic-gate id->eventmask = eventmask; 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate /* 117*0Sstevel@tonic-gate * Insert at head. Order could be important for performance if we 118*0Sstevel@tonic-gate * believe that evGetNext()'s accesses to the fd_sets will be more 119*0Sstevel@tonic-gate * serial and therefore more cache-lucky if the list is ordered by 120*0Sstevel@tonic-gate * ``fd.'' We do not believe these things, so we don't do it. 121*0Sstevel@tonic-gate * 122*0Sstevel@tonic-gate * The interesting sequence is where GetNext() has cached a select() 123*0Sstevel@tonic-gate * result and the caller decides to evSelectFD() on some descriptor. 124*0Sstevel@tonic-gate * Since GetNext() starts at the head, it can miss new entries we add 125*0Sstevel@tonic-gate * at the head. This is not a serious problem since the event being 126*0Sstevel@tonic-gate * evSelectFD()'d for has to occur before evSelectFD() is called for 127*0Sstevel@tonic-gate * the file event to be considered "missed" -- a real corner case. 128*0Sstevel@tonic-gate * Maintaining a "tail" pointer for ctx->files would fix this, but I'm 129*0Sstevel@tonic-gate * not sure it would be ``more correct.'' 130*0Sstevel@tonic-gate */ 131*0Sstevel@tonic-gate if (ctx->files != NULL) 132*0Sstevel@tonic-gate ctx->files->prev = id; 133*0Sstevel@tonic-gate id->prev = NULL; 134*0Sstevel@tonic-gate id->next = ctx->files; 135*0Sstevel@tonic-gate ctx->files = id; 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate /* Insert into fd table. */ 138*0Sstevel@tonic-gate if (ctx->fdTable[fd] != NULL) 139*0Sstevel@tonic-gate ctx->fdTable[fd]->fdprev = id; 140*0Sstevel@tonic-gate id->fdprev = NULL; 141*0Sstevel@tonic-gate id->fdnext = ctx->fdTable[fd]; 142*0Sstevel@tonic-gate ctx->fdTable[fd] = id; 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate /* Turn on the appropriate bits in the {rd,wr,ex}Next fd_set's. */ 145*0Sstevel@tonic-gate if (eventmask & EV_READ) 146*0Sstevel@tonic-gate FD_SET(fd, &ctx->rdNext); 147*0Sstevel@tonic-gate if (eventmask & EV_WRITE) 148*0Sstevel@tonic-gate FD_SET(fd, &ctx->wrNext); 149*0Sstevel@tonic-gate if (eventmask & EV_EXCEPT) 150*0Sstevel@tonic-gate FD_SET(fd, &ctx->exNext); 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate /* Update fdMax. */ 153*0Sstevel@tonic-gate if (fd > ctx->fdMax) 154*0Sstevel@tonic-gate ctx->fdMax = fd; 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate /* Remember the ID if the caller provided us a place for it. */ 157*0Sstevel@tonic-gate if (opaqueID) 158*0Sstevel@tonic-gate opaqueID->opaque = id; 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate #ifdef SUNW_POLL 161*0Sstevel@tonic-gate #else 162*0Sstevel@tonic-gate evPrintf(ctx, 5, 163*0Sstevel@tonic-gate "evSelectFD(fd %d, mask 0x%x): new masks: 0x%lx 0x%lx 0x%lx\n", 164*0Sstevel@tonic-gate fd, eventmask, 165*0Sstevel@tonic-gate (u_long)ctx->rdNext.fds_bits[0], 166*0Sstevel@tonic-gate (u_long)ctx->wrNext.fds_bits[0], 167*0Sstevel@tonic-gate (u_long)ctx->exNext.fds_bits[0]); 168*0Sstevel@tonic-gate #endif /* SUNW_POLL */ 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate return (0); 171*0Sstevel@tonic-gate } 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate int 174*0Sstevel@tonic-gate evDeselectFD(evContext opaqueCtx, evFileID opaqueID) { 175*0Sstevel@tonic-gate evContext_p *ctx = opaqueCtx.opaque; 176*0Sstevel@tonic-gate evFile *del = opaqueID.opaque; 177*0Sstevel@tonic-gate evFile *cur; 178*0Sstevel@tonic-gate int mode, eventmask; 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate if (!del) { 181*0Sstevel@tonic-gate evPrintf(ctx, 11, "evDeselectFD(NULL) ignored\n"); 182*0Sstevel@tonic-gate errno = EINVAL; 183*0Sstevel@tonic-gate return (-1); 184*0Sstevel@tonic-gate } 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate evPrintf(ctx, 1, "evDeselectFD(fd %d, mask 0x%x)\n", 187*0Sstevel@tonic-gate del->fd, del->eventmask); 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate /* Get the mode. Unless the file has been closed, errors are bad. */ 190*0Sstevel@tonic-gate mode = fcntl(del->fd, F_GETFL, NULL); 191*0Sstevel@tonic-gate if (mode == -1 && errno != EBADF) 192*0Sstevel@tonic-gate EV_ERR(errno); 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate /* Remove from the list of files. */ 195*0Sstevel@tonic-gate if (del->prev != NULL) 196*0Sstevel@tonic-gate del->prev->next = del->next; 197*0Sstevel@tonic-gate else 198*0Sstevel@tonic-gate ctx->files = del->next; 199*0Sstevel@tonic-gate if (del->next != NULL) 200*0Sstevel@tonic-gate del->next->prev = del->prev; 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate /* Remove from the fd table. */ 203*0Sstevel@tonic-gate if (del->fdprev != NULL) 204*0Sstevel@tonic-gate del->fdprev->fdnext = del->fdnext; 205*0Sstevel@tonic-gate else 206*0Sstevel@tonic-gate ctx->fdTable[del->fd] = del->fdnext; 207*0Sstevel@tonic-gate if (del->fdnext != NULL) 208*0Sstevel@tonic-gate del->fdnext->fdprev = del->fdprev; 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate /* 211*0Sstevel@tonic-gate * If the file descriptor does not appear in any other select() entry, 212*0Sstevel@tonic-gate * and if !EV_WASNONBLOCK, and if we got no EBADF when we got the mode 213*0Sstevel@tonic-gate * earlier, then: restore the fd to blocking status. 214*0Sstevel@tonic-gate */ 215*0Sstevel@tonic-gate if (!(cur = FindFD(ctx, del->fd, EV_MASK_ALL)) && 216*0Sstevel@tonic-gate !FD_ISSET(del->fd, &ctx->nonblockBefore) && 217*0Sstevel@tonic-gate mode != -1) { 218*0Sstevel@tonic-gate /* 219*0Sstevel@tonic-gate * Note that we won't return an error status to the caller if 220*0Sstevel@tonic-gate * this fcntl() fails since (a) we've already done the work 221*0Sstevel@tonic-gate * and (b) the caller didn't ask us anything about O_NONBLOCK. 222*0Sstevel@tonic-gate */ 223*0Sstevel@tonic-gate #ifdef USE_FIONBIO_IOCTL 224*0Sstevel@tonic-gate int off = 1; 225*0Sstevel@tonic-gate (void) ioctl(del->fd, FIONBIO, (char *)&off); 226*0Sstevel@tonic-gate #else 227*0Sstevel@tonic-gate (void) fcntl(del->fd, F_SETFL, mode & ~PORT_NONBLOCK); 228*0Sstevel@tonic-gate #endif 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gate /* 232*0Sstevel@tonic-gate * Now find all other uses of this descriptor and OR together an event 233*0Sstevel@tonic-gate * mask so that we don't turn off {rd,wr,ex}Next bits that some other 234*0Sstevel@tonic-gate * file event is using. As an optimization, stop if the event mask 235*0Sstevel@tonic-gate * fills. 236*0Sstevel@tonic-gate */ 237*0Sstevel@tonic-gate eventmask = 0; 238*0Sstevel@tonic-gate for ((void)NULL; 239*0Sstevel@tonic-gate cur != NULL && eventmask != EV_MASK_ALL; 240*0Sstevel@tonic-gate cur = cur->next) 241*0Sstevel@tonic-gate if (cur->fd == del->fd) 242*0Sstevel@tonic-gate eventmask |= cur->eventmask; 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gate /* OK, now we know which bits we can clear out. */ 245*0Sstevel@tonic-gate if (!(eventmask & EV_READ)) { 246*0Sstevel@tonic-gate FD_CLR(del->fd, &ctx->rdNext); 247*0Sstevel@tonic-gate if (FD_ISSET(del->fd, &ctx->rdLast)) { 248*0Sstevel@tonic-gate FD_CLR(del->fd, &ctx->rdLast); 249*0Sstevel@tonic-gate ctx->fdCount--; 250*0Sstevel@tonic-gate } 251*0Sstevel@tonic-gate } 252*0Sstevel@tonic-gate if (!(eventmask & EV_WRITE)) { 253*0Sstevel@tonic-gate FD_CLR(del->fd, &ctx->wrNext); 254*0Sstevel@tonic-gate if (FD_ISSET(del->fd, &ctx->wrLast)) { 255*0Sstevel@tonic-gate FD_CLR(del->fd, &ctx->wrLast); 256*0Sstevel@tonic-gate ctx->fdCount--; 257*0Sstevel@tonic-gate } 258*0Sstevel@tonic-gate } 259*0Sstevel@tonic-gate if (!(eventmask & EV_EXCEPT)) { 260*0Sstevel@tonic-gate FD_CLR(del->fd, &ctx->exNext); 261*0Sstevel@tonic-gate if (FD_ISSET(del->fd, &ctx->exLast)) { 262*0Sstevel@tonic-gate FD_CLR(del->fd, &ctx->exLast); 263*0Sstevel@tonic-gate ctx->fdCount--; 264*0Sstevel@tonic-gate } 265*0Sstevel@tonic-gate } 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate /* If this was the maxFD, find the new one. */ 268*0Sstevel@tonic-gate if (del->fd == ctx->fdMax) { 269*0Sstevel@tonic-gate ctx->fdMax = -1; 270*0Sstevel@tonic-gate for (cur = ctx->files; cur; cur = cur->next) 271*0Sstevel@tonic-gate if (cur->fd > ctx->fdMax) 272*0Sstevel@tonic-gate ctx->fdMax = cur->fd; 273*0Sstevel@tonic-gate } 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gate /* If this was the fdNext, cycle that to the next entry. */ 276*0Sstevel@tonic-gate if (del == ctx->fdNext) 277*0Sstevel@tonic-gate ctx->fdNext = del->next; 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate #ifdef SUNW_POLL 280*0Sstevel@tonic-gate #else 281*0Sstevel@tonic-gate evPrintf(ctx, 5, 282*0Sstevel@tonic-gate "evDeselectFD(fd %d, mask 0x%x): new masks: 0x%lx 0x%lx 0x%lx\n", 283*0Sstevel@tonic-gate del->fd, eventmask, 284*0Sstevel@tonic-gate (u_long)ctx->rdNext.fds_bits[0], 285*0Sstevel@tonic-gate (u_long)ctx->wrNext.fds_bits[0], 286*0Sstevel@tonic-gate (u_long)ctx->exNext.fds_bits[0]); 287*0Sstevel@tonic-gate #endif /* SUNW_POLL */ 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate /* Couldn't free it before now since we were using fields out of it. */ 290*0Sstevel@tonic-gate FREE(del); 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate return (0); 293*0Sstevel@tonic-gate } 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gate static evFile * 296*0Sstevel@tonic-gate FindFD(const evContext_p *ctx, int fd, int eventmask) { 297*0Sstevel@tonic-gate evFile *id; 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gate for (id = ctx->fdTable[fd]; id != NULL; id = id->fdnext) 300*0Sstevel@tonic-gate if (id->fd == fd && (id->eventmask & eventmask) != 0) 301*0Sstevel@tonic-gate break; 302*0Sstevel@tonic-gate return (id); 303*0Sstevel@tonic-gate } 304