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