xref: /onnv-gate/usr/src/lib/libresolv2/common/isc/ev_files.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3*11038SRao.Shoaib@Sun.COM  * Copyright (c) 1995-1999 by Internet Software Consortium
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate /* ev_files.c - implement asynch file IO for the eventlib
190Sstevel@tonic-gate  * vix 11sep95 [initial]
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
23*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: ev_files.c,v 1.8 2005/07/28 06:51:48 marka Exp $";
240Sstevel@tonic-gate #endif
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include "port_before.h"
270Sstevel@tonic-gate #include "fd_setsize.h"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/time.h>
310Sstevel@tonic-gate #include <sys/ioctl.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <isc/eventlib.h>
380Sstevel@tonic-gate #include "eventlib_p.h"
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include "port_after.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate static evFile *FindFD(const evContext_p *ctx, int fd, int eventmask);
430Sstevel@tonic-gate 
440Sstevel@tonic-gate int
evSelectFD(evContext opaqueCtx,int fd,int eventmask,evFileFunc func,void * uap,evFileID * opaqueID)450Sstevel@tonic-gate evSelectFD(evContext opaqueCtx,
460Sstevel@tonic-gate 	   int fd,
470Sstevel@tonic-gate 	   int eventmask,
480Sstevel@tonic-gate 	   evFileFunc func,
490Sstevel@tonic-gate 	   void *uap,
500Sstevel@tonic-gate 	   evFileID *opaqueID
510Sstevel@tonic-gate ) {
520Sstevel@tonic-gate 	evContext_p *ctx = opaqueCtx.opaque;
530Sstevel@tonic-gate 	evFile *id;
540Sstevel@tonic-gate 	int mode;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	evPrintf(ctx, 1,
570Sstevel@tonic-gate 		 "evSelectFD(ctx %p, fd %d, mask 0x%x, func %p, uap %p)\n",
580Sstevel@tonic-gate 		 ctx, fd, eventmask, func, uap);
590Sstevel@tonic-gate 	if (eventmask == 0 || (eventmask & ~EV_MASK_ALL) != 0)
600Sstevel@tonic-gate 		EV_ERR(EINVAL);
61*11038SRao.Shoaib@Sun.COM #ifndef USE_POLL
620Sstevel@tonic-gate 	if (fd > ctx->highestFD)
630Sstevel@tonic-gate 		EV_ERR(EINVAL);
64*11038SRao.Shoaib@Sun.COM #endif
65*11038SRao.Shoaib@Sun.COM 	OK(mode = fcntl(fd, F_GETFL, NULL));	/*%< side effect: validate fd. */
660Sstevel@tonic-gate 	/*
670Sstevel@tonic-gate 	 * The first time we touch a file descriptor, we need to check to see
680Sstevel@tonic-gate 	 * if the application already had it in O_NONBLOCK mode and if so, all
690Sstevel@tonic-gate 	 * of our deselect()'s have to leave it in O_NONBLOCK.  If not, then
700Sstevel@tonic-gate 	 * all but our last deselect() has to leave it in O_NONBLOCK.
710Sstevel@tonic-gate 	 */
72*11038SRao.Shoaib@Sun.COM #ifdef USE_POLL
730Sstevel@tonic-gate 	/* Make sure both ctx->pollfds[] and ctx->fdTable[] are large enough */
74*11038SRao.Shoaib@Sun.COM 	if (fd >= ctx->maxnfds && evPollfdRealloc(ctx, 1, fd) != 0)
75*11038SRao.Shoaib@Sun.COM 		EV_ERR(ENOMEM);
76*11038SRao.Shoaib@Sun.COM #endif /* USE_POLL */
770Sstevel@tonic-gate 	id = FindFD(ctx, fd, EV_MASK_ALL);
780Sstevel@tonic-gate 	if (id == NULL) {
790Sstevel@tonic-gate 		if (mode & PORT_NONBLOCK)
800Sstevel@tonic-gate 			FD_SET(fd, &ctx->nonblockBefore);
810Sstevel@tonic-gate 		else {
820Sstevel@tonic-gate #ifdef USE_FIONBIO_IOCTL
830Sstevel@tonic-gate 			int on = 1;
840Sstevel@tonic-gate 			OK(ioctl(fd, FIONBIO, (char *)&on));
850Sstevel@tonic-gate #else
860Sstevel@tonic-gate 			OK(fcntl(fd, F_SETFL, mode | PORT_NONBLOCK));
870Sstevel@tonic-gate #endif
880Sstevel@tonic-gate 			FD_CLR(fd, &ctx->nonblockBefore);
890Sstevel@tonic-gate 		}
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	/*
930Sstevel@tonic-gate 	 * If this descriptor is already in use, search for it again to see
940Sstevel@tonic-gate 	 * if any of the eventmask bits we want to set are already captured.
950Sstevel@tonic-gate 	 * We cannot usefully capture the same fd event more than once in the
960Sstevel@tonic-gate 	 * same context.
970Sstevel@tonic-gate 	 */
980Sstevel@tonic-gate 	if (id != NULL && FindFD(ctx, fd, eventmask) != NULL)
990Sstevel@tonic-gate 		EV_ERR(ETOOMANYREFS);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	/* Allocate and fill. */
1020Sstevel@tonic-gate 	OKNEW(id);
1030Sstevel@tonic-gate 	id->func = func;
1040Sstevel@tonic-gate 	id->uap = uap;
1050Sstevel@tonic-gate 	id->fd = fd;
1060Sstevel@tonic-gate 	id->eventmask = eventmask;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	/*
1090Sstevel@tonic-gate 	 * Insert at head.  Order could be important for performance if we
1100Sstevel@tonic-gate 	 * believe that evGetNext()'s accesses to the fd_sets will be more
1110Sstevel@tonic-gate 	 * serial and therefore more cache-lucky if the list is ordered by
1120Sstevel@tonic-gate 	 * ``fd.''  We do not believe these things, so we don't do it.
1130Sstevel@tonic-gate 	 *
1140Sstevel@tonic-gate 	 * The interesting sequence is where GetNext() has cached a select()
1150Sstevel@tonic-gate 	 * result and the caller decides to evSelectFD() on some descriptor.
1160Sstevel@tonic-gate 	 * Since GetNext() starts at the head, it can miss new entries we add
1170Sstevel@tonic-gate 	 * at the head.  This is not a serious problem since the event being
1180Sstevel@tonic-gate 	 * evSelectFD()'d for has to occur before evSelectFD() is called for
1190Sstevel@tonic-gate 	 * the file event to be considered "missed" -- a real corner case.
1200Sstevel@tonic-gate 	 * Maintaining a "tail" pointer for ctx->files would fix this, but I'm
1210Sstevel@tonic-gate 	 * not sure it would be ``more correct.''
1220Sstevel@tonic-gate 	 */
1230Sstevel@tonic-gate 	if (ctx->files != NULL)
1240Sstevel@tonic-gate 		ctx->files->prev = id;
1250Sstevel@tonic-gate 	id->prev = NULL;
1260Sstevel@tonic-gate 	id->next = ctx->files;
1270Sstevel@tonic-gate 	ctx->files = id;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	/* Insert into fd table. */
1300Sstevel@tonic-gate 	if (ctx->fdTable[fd] != NULL)
1310Sstevel@tonic-gate 		ctx->fdTable[fd]->fdprev = id;
1320Sstevel@tonic-gate 	id->fdprev = NULL;
1330Sstevel@tonic-gate 	id->fdnext = ctx->fdTable[fd];
1340Sstevel@tonic-gate 	ctx->fdTable[fd] = id;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	/* Turn on the appropriate bits in the {rd,wr,ex}Next fd_set's. */
1370Sstevel@tonic-gate 	if (eventmask & EV_READ)
1380Sstevel@tonic-gate 		FD_SET(fd, &ctx->rdNext);
1390Sstevel@tonic-gate 	if (eventmask & EV_WRITE)
1400Sstevel@tonic-gate 		FD_SET(fd, &ctx->wrNext);
1410Sstevel@tonic-gate 	if (eventmask & EV_EXCEPT)
1420Sstevel@tonic-gate 		FD_SET(fd, &ctx->exNext);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	/* Update fdMax. */
1450Sstevel@tonic-gate 	if (fd > ctx->fdMax)
1460Sstevel@tonic-gate 		ctx->fdMax = fd;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/* Remember the ID if the caller provided us a place for it. */
1490Sstevel@tonic-gate 	if (opaqueID)
1500Sstevel@tonic-gate 		opaqueID->opaque = id;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	return (0);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate int
evDeselectFD(evContext opaqueCtx,evFileID opaqueID)1560Sstevel@tonic-gate evDeselectFD(evContext opaqueCtx, evFileID opaqueID) {
1570Sstevel@tonic-gate 	evContext_p *ctx = opaqueCtx.opaque;
1580Sstevel@tonic-gate 	evFile *del = opaqueID.opaque;
1590Sstevel@tonic-gate 	evFile *cur;
1600Sstevel@tonic-gate 	int mode, eventmask;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if (!del) {
1630Sstevel@tonic-gate 		evPrintf(ctx, 11, "evDeselectFD(NULL) ignored\n");
1640Sstevel@tonic-gate 		errno = EINVAL;
1650Sstevel@tonic-gate 		return (-1);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	evPrintf(ctx, 1, "evDeselectFD(fd %d, mask 0x%x)\n",
1690Sstevel@tonic-gate 		 del->fd, del->eventmask);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	/* Get the mode.  Unless the file has been closed, errors are bad. */
1720Sstevel@tonic-gate 	mode = fcntl(del->fd, F_GETFL, NULL);
1730Sstevel@tonic-gate 	if (mode == -1 && errno != EBADF)
1740Sstevel@tonic-gate 		EV_ERR(errno);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/* Remove from the list of files. */
1770Sstevel@tonic-gate 	if (del->prev != NULL)
1780Sstevel@tonic-gate 		del->prev->next = del->next;
1790Sstevel@tonic-gate 	else
1800Sstevel@tonic-gate 		ctx->files = del->next;
1810Sstevel@tonic-gate 	if (del->next != NULL)
1820Sstevel@tonic-gate 		del->next->prev = del->prev;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	/* Remove from the fd table. */
1850Sstevel@tonic-gate 	if (del->fdprev != NULL)
1860Sstevel@tonic-gate 		del->fdprev->fdnext = del->fdnext;
1870Sstevel@tonic-gate 	else
1880Sstevel@tonic-gate 		ctx->fdTable[del->fd] = del->fdnext;
1890Sstevel@tonic-gate 	if (del->fdnext != NULL)
1900Sstevel@tonic-gate 		del->fdnext->fdprev = del->fdprev;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	/*
1930Sstevel@tonic-gate 	 * If the file descriptor does not appear in any other select() entry,
1940Sstevel@tonic-gate 	 * and if !EV_WASNONBLOCK, and if we got no EBADF when we got the mode
1950Sstevel@tonic-gate 	 * earlier, then: restore the fd to blocking status.
1960Sstevel@tonic-gate 	 */
1970Sstevel@tonic-gate 	if (!(cur = FindFD(ctx, del->fd, EV_MASK_ALL)) &&
1980Sstevel@tonic-gate 	    !FD_ISSET(del->fd, &ctx->nonblockBefore) &&
1990Sstevel@tonic-gate 	    mode != -1) {
2000Sstevel@tonic-gate 		/*
2010Sstevel@tonic-gate 		 * Note that we won't return an error status to the caller if
2020Sstevel@tonic-gate 		 * this fcntl() fails since (a) we've already done the work
2030Sstevel@tonic-gate 		 * and (b) the caller didn't ask us anything about O_NONBLOCK.
2040Sstevel@tonic-gate 		 */
2050Sstevel@tonic-gate #ifdef USE_FIONBIO_IOCTL
206*11038SRao.Shoaib@Sun.COM 		int off = 0;
2070Sstevel@tonic-gate 		(void) ioctl(del->fd, FIONBIO, (char *)&off);
2080Sstevel@tonic-gate #else
2090Sstevel@tonic-gate 		(void) fcntl(del->fd, F_SETFL, mode & ~PORT_NONBLOCK);
2100Sstevel@tonic-gate #endif
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	/*
2140Sstevel@tonic-gate 	 * Now find all other uses of this descriptor and OR together an event
2150Sstevel@tonic-gate 	 * mask so that we don't turn off {rd,wr,ex}Next bits that some other
2160Sstevel@tonic-gate 	 * file event is using.  As an optimization, stop if the event mask
2170Sstevel@tonic-gate 	 * fills.
2180Sstevel@tonic-gate 	 */
2190Sstevel@tonic-gate 	eventmask = 0;
2200Sstevel@tonic-gate 	for ((void)NULL;
2210Sstevel@tonic-gate 	     cur != NULL && eventmask != EV_MASK_ALL;
2220Sstevel@tonic-gate 	     cur = cur->next)
2230Sstevel@tonic-gate 		if (cur->fd == del->fd)
2240Sstevel@tonic-gate 			eventmask |= cur->eventmask;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	/* OK, now we know which bits we can clear out. */
2270Sstevel@tonic-gate 	if (!(eventmask & EV_READ)) {
2280Sstevel@tonic-gate 		FD_CLR(del->fd, &ctx->rdNext);
2290Sstevel@tonic-gate 		if (FD_ISSET(del->fd, &ctx->rdLast)) {
2300Sstevel@tonic-gate 			FD_CLR(del->fd, &ctx->rdLast);
2310Sstevel@tonic-gate 			ctx->fdCount--;
2320Sstevel@tonic-gate 		}
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 	if (!(eventmask & EV_WRITE)) {
2350Sstevel@tonic-gate 		FD_CLR(del->fd, &ctx->wrNext);
2360Sstevel@tonic-gate 		if (FD_ISSET(del->fd, &ctx->wrLast)) {
2370Sstevel@tonic-gate 			FD_CLR(del->fd, &ctx->wrLast);
2380Sstevel@tonic-gate 			ctx->fdCount--;
2390Sstevel@tonic-gate 		}
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 	if (!(eventmask & EV_EXCEPT)) {
2420Sstevel@tonic-gate 		FD_CLR(del->fd, &ctx->exNext);
2430Sstevel@tonic-gate 		if (FD_ISSET(del->fd, &ctx->exLast)) {
2440Sstevel@tonic-gate 			FD_CLR(del->fd, &ctx->exLast);
2450Sstevel@tonic-gate 			ctx->fdCount--;
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	/* If this was the maxFD, find the new one. */
2500Sstevel@tonic-gate 	if (del->fd == ctx->fdMax) {
2510Sstevel@tonic-gate 		ctx->fdMax = -1;
2520Sstevel@tonic-gate 		for (cur = ctx->files; cur; cur = cur->next)
2530Sstevel@tonic-gate 			if (cur->fd > ctx->fdMax)
2540Sstevel@tonic-gate 				ctx->fdMax = cur->fd;
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/* If this was the fdNext, cycle that to the next entry. */
2580Sstevel@tonic-gate 	if (del == ctx->fdNext)
2590Sstevel@tonic-gate 		ctx->fdNext = del->next;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/* Couldn't free it before now since we were using fields out of it. */
2620Sstevel@tonic-gate 	FREE(del);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	return (0);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate static evFile *
FindFD(const evContext_p * ctx,int fd,int eventmask)2680Sstevel@tonic-gate FindFD(const evContext_p *ctx, int fd, int eventmask) {
2690Sstevel@tonic-gate 	evFile *id;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	for (id = ctx->fdTable[fd]; id != NULL; id = id->fdnext)
2720Sstevel@tonic-gate 		if (id->fd == fd && (id->eventmask & eventmask) != 0)
2730Sstevel@tonic-gate 			break;
2740Sstevel@tonic-gate 	return (id);
2750Sstevel@tonic-gate }
276*11038SRao.Shoaib@Sun.COM 
277*11038SRao.Shoaib@Sun.COM /*! \file */
278