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