Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / isc / ev_files.c
blob46c26c59918c136f999a9229d8631bbc415e4294
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1995-1999 by Internet Software Consortium
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.
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.
20 /* ev_files.c - implement asynch file IO for the eventlib
21 * vix 11sep95 [initial]
24 #if !defined(LINT) && !defined(CODECENTER)
25 static const char rcsid[] = "Id: ev_files.c,v 1.8 2005/07/28 06:51:48 marka Exp";
26 #endif
28 #include "port_before.h"
29 #include "fd_setsize.h"
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <sys/ioctl.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <unistd.h>
39 #include <isc/eventlib.h>
40 #include "eventlib_p.h"
42 #include "port_after.h"
44 static evFile *FindFD(const evContext_p *ctx, int fd, int eventmask);
46 int
47 evSelectFD(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;
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. */
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.
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);
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.
100 if (id != NULL && FindFD(ctx, fd, eventmask) != NULL)
101 EV_ERR(ETOOMANYREFS);
103 /* Allocate and fill. */
104 OKNEW(id);
105 id->func = func;
106 id->uap = uap;
107 id->fd = fd;
108 id->eventmask = eventmask;
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.
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.''
125 if (ctx->files != NULL)
126 ctx->files->prev = id;
127 id->prev = NULL;
128 id->next = ctx->files;
129 ctx->files = id;
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;
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);
146 /* Update fdMax. */
147 if (fd > ctx->fdMax)
148 ctx->fdMax = fd;
150 /* Remember the ID if the caller provided us a place for it. */
151 if (opaqueID)
152 opaqueID->opaque = id;
154 return (0);
158 evDeselectFD(evContext opaqueCtx, evFileID opaqueID) {
159 evContext_p *ctx = opaqueCtx.opaque;
160 evFile *del = opaqueID.opaque;
161 evFile *cur;
162 int mode, eventmask;
164 if (!del) {
165 evPrintf(ctx, 11, "evDeselectFD(NULL) ignored\n");
166 errno = EINVAL;
167 return (-1);
170 evPrintf(ctx, 1, "evDeselectFD(fd %d, mask 0x%x)\n",
171 del->fd, del->eventmask);
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);
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;
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;
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.
199 if (!(cur = FindFD(ctx, del->fd, EV_MASK_ALL)) &&
200 !FD_ISSET(del->fd, &ctx->nonblockBefore) &&
201 mode != -1) {
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.
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
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.
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;
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--;
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--;
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--;
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;
259 /* If this was the fdNext, cycle that to the next entry. */
260 if (del == ctx->fdNext)
261 ctx->fdNext = del->next;
263 /* Couldn't free it before now since we were using fields out of it. */
264 FREE(del);
266 return (0);
269 static evFile *
270 FindFD(const evContext_p *ctx, int fd, int eventmask) {
271 evFile *id;
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);
279 /*! \file */