Import bind-9.3.4
[dragonfly.git] / contrib / bind-9.3 / lib / bind / isc / eventlib.c
blob11120ecadd5de60023b5aa75f3230db1ff43f99f
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1995-1999 by Internet Software Consortium
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 /* eventlib.c - implement glue for the eventlib
19 * vix 09sep95 [initial]
22 #if !defined(LINT) && !defined(CODECENTER)
23 static const char rcsid[] = "$Id: eventlib.c,v 1.2.2.1.4.6 2006/03/10 00:17:21 marka Exp $";
24 #endif
26 #include "port_before.h"
27 #include "fd_setsize.h"
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/stat.h>
32 #ifdef SOLARIS2
33 #include <limits.h>
34 #endif /* SOLARIS2 */
36 #include <errno.h>
37 #include <signal.h>
38 #include <stdarg.h>
39 #include <stdlib.h>
40 #include <unistd.h>
42 #include <isc/eventlib.h>
43 #include <isc/assertions.h>
44 #include "eventlib_p.h"
46 #include "port_after.h"
48 int __evOptMonoTime;
50 #ifdef USE_POLL
51 #define pselect Pselect
52 #endif /* USE_POLL */
54 /* Forward. */
56 #if defined(NEED_PSELECT) || defined(USE_POLL)
57 static int pselect(int, void *, void *, void *,
58 struct timespec *,
59 const sigset_t *);
60 #endif
62 int __evOptMonoTime;
64 /* Public. */
66 int
67 evCreate(evContext *opaqueCtx) {
68 evContext_p *ctx;
70 /* Make sure the memory heap is initialized. */
71 if (meminit(0, 0) < 0 && errno != EEXIST)
72 return (-1);
74 OKNEW(ctx);
76 /* Global. */
77 ctx->cur = NULL;
79 /* Debugging. */
80 ctx->debug = 0;
81 ctx->output = NULL;
83 /* Connections. */
84 ctx->conns = NULL;
85 INIT_LIST(ctx->accepts);
87 /* Files. */
88 #ifdef USE_POLL
89 ctx->pollfds = NULL;
90 ctx->maxnfds = 0;
91 ctx->firstfd = 0;
92 emulMaskInit(ctx, rdLast, EV_READ, 1);
93 emulMaskInit(ctx, rdNext, EV_READ, 0);
94 emulMaskInit(ctx, wrLast, EV_WRITE, 1);
95 emulMaskInit(ctx, wrNext, EV_WRITE, 0);
96 emulMaskInit(ctx, exLast, EV_EXCEPT, 1);
97 emulMaskInit(ctx, exNext, EV_EXCEPT, 0);
98 emulMaskInit(ctx, nonblockBefore, EV_WASNONBLOCKING, 0);
99 #endif /* USE_POLL */
100 ctx->files = NULL;
101 FD_ZERO(&ctx->rdNext);
102 FD_ZERO(&ctx->wrNext);
103 FD_ZERO(&ctx->exNext);
104 FD_ZERO(&ctx->nonblockBefore);
105 ctx->fdMax = -1;
106 ctx->fdNext = NULL;
107 ctx->fdCount = 0; /* Invalidate {rd,wr,ex}Last. */
108 #ifndef USE_POLL
109 ctx->highestFD = FD_SETSIZE - 1;
110 memset(ctx->fdTable, 0, sizeof ctx->fdTable);
111 #else
112 ctx->highestFD = INT_MAX / sizeof(struct pollfd);
113 ctx->fdTable = NULL;
114 #endif
115 #ifdef EVENTLIB_TIME_CHECKS
116 ctx->lastFdCount = 0;
117 #endif
119 /* Streams. */
120 ctx->streams = NULL;
121 ctx->strDone = NULL;
122 ctx->strLast = NULL;
124 /* Timers. */
125 ctx->lastEventTime = evNowTime();
126 #ifdef EVENTLIB_TIME_CHECKS
127 ctx->lastSelectTime = ctx->lastEventTime;
128 #endif
129 ctx->timers = evCreateTimers(ctx);
130 if (ctx->timers == NULL)
131 return (-1);
133 /* Waits. */
134 ctx->waitLists = NULL;
135 ctx->waitDone.first = ctx->waitDone.last = NULL;
136 ctx->waitDone.prev = ctx->waitDone.next = NULL;
138 opaqueCtx->opaque = ctx;
139 return (0);
142 void
143 evSetDebug(evContext opaqueCtx, int level, FILE *output) {
144 evContext_p *ctx = opaqueCtx.opaque;
146 ctx->debug = level;
147 ctx->output = output;
151 evDestroy(evContext opaqueCtx) {
152 evContext_p *ctx = opaqueCtx.opaque;
153 int revs = 424242; /* Doug Adams. */
154 evWaitList *this_wl, *next_wl;
155 evWait *this_wait, *next_wait;
157 /* Connections. */
158 while (revs-- > 0 && ctx->conns != NULL) {
159 evConnID id;
161 id.opaque = ctx->conns;
162 (void) evCancelConn(opaqueCtx, id);
164 INSIST(revs >= 0);
166 /* Streams. */
167 while (revs-- > 0 && ctx->streams != NULL) {
168 evStreamID id;
170 id.opaque = ctx->streams;
171 (void) evCancelRW(opaqueCtx, id);
174 /* Files. */
175 while (revs-- > 0 && ctx->files != NULL) {
176 evFileID id;
178 id.opaque = ctx->files;
179 (void) evDeselectFD(opaqueCtx, id);
181 INSIST(revs >= 0);
183 /* Timers. */
184 evDestroyTimers(ctx);
186 /* Waits. */
187 for (this_wl = ctx->waitLists;
188 revs-- > 0 && this_wl != NULL;
189 this_wl = next_wl) {
190 next_wl = this_wl->next;
191 for (this_wait = this_wl->first;
192 revs-- > 0 && this_wait != NULL;
193 this_wait = next_wait) {
194 next_wait = this_wait->next;
195 FREE(this_wait);
197 FREE(this_wl);
199 for (this_wait = ctx->waitDone.first;
200 revs-- > 0 && this_wait != NULL;
201 this_wait = next_wait) {
202 next_wait = this_wait->next;
203 FREE(this_wait);
206 FREE(ctx);
207 return (0);
211 evGetNext(evContext opaqueCtx, evEvent *opaqueEv, int options) {
212 evContext_p *ctx = opaqueCtx.opaque;
213 struct timespec nextTime;
214 evTimer *nextTimer;
215 evEvent_p *new;
216 int x, pselect_errno, timerPast;
217 #ifdef EVENTLIB_TIME_CHECKS
218 struct timespec interval;
219 #endif
221 /* Ensure that exactly one of EV_POLL or EV_WAIT was specified. */
222 x = ((options & EV_POLL) != 0) + ((options & EV_WAIT) != 0);
223 if (x != 1)
224 EV_ERR(EINVAL);
226 /* Get the time of day. We'll do this again after select() blocks. */
227 ctx->lastEventTime = evNowTime();
229 again:
230 /* Finished accept()'s do not require a select(). */
231 if (!EMPTY(ctx->accepts)) {
232 OKNEW(new);
233 new->type = Accept;
234 new->u.accept.this = HEAD(ctx->accepts);
235 UNLINK(ctx->accepts, HEAD(ctx->accepts), link);
236 opaqueEv->opaque = new;
237 return (0);
240 /* Stream IO does not require a select(). */
241 if (ctx->strDone != NULL) {
242 OKNEW(new);
243 new->type = Stream;
244 new->u.stream.this = ctx->strDone;
245 ctx->strDone = ctx->strDone->nextDone;
246 if (ctx->strDone == NULL)
247 ctx->strLast = NULL;
248 opaqueEv->opaque = new;
249 return (0);
252 /* Waits do not require a select(). */
253 if (ctx->waitDone.first != NULL) {
254 OKNEW(new);
255 new->type = Wait;
256 new->u.wait.this = ctx->waitDone.first;
257 ctx->waitDone.first = ctx->waitDone.first->next;
258 if (ctx->waitDone.first == NULL)
259 ctx->waitDone.last = NULL;
260 opaqueEv->opaque = new;
261 return (0);
264 /* Get the status and content of the next timer. */
265 if ((nextTimer = heap_element(ctx->timers, 1)) != NULL) {
266 nextTime = nextTimer->due;
267 timerPast = (evCmpTime(nextTime, ctx->lastEventTime) <= 0);
268 } else
269 timerPast = 0; /* Make gcc happy. */
271 evPrintf(ctx, 9, "evGetNext: fdCount %d\n", ctx->fdCount);
272 if (ctx->fdCount == 0) {
273 static const struct timespec NoTime = {0, 0L};
274 enum { JustPoll, Block, Timer } m;
275 struct timespec t, *tp;
277 /* Are there any events at all? */
278 if ((options & EV_WAIT) != 0 && !nextTimer && ctx->fdMax == -1)
279 EV_ERR(ENOENT);
281 /* Figure out what select()'s timeout parameter should be. */
282 if ((options & EV_POLL) != 0) {
283 m = JustPoll;
284 t = NoTime;
285 tp = &t;
286 } else if (nextTimer == NULL) {
287 m = Block;
288 /* ``t'' unused. */
289 tp = NULL;
290 } else if (timerPast) {
291 m = JustPoll;
292 t = NoTime;
293 tp = &t;
294 } else {
295 m = Timer;
296 /* ``t'' filled in later. */
297 tp = &t;
299 #ifdef EVENTLIB_TIME_CHECKS
300 if (ctx->debug > 0) {
301 interval = evSubTime(ctx->lastEventTime,
302 ctx->lastSelectTime);
303 if (interval.tv_sec > 0 || interval.tv_nsec > 0)
304 evPrintf(ctx, 1,
305 "time between pselect() %u.%09u count %d\n",
306 interval.tv_sec, interval.tv_nsec,
307 ctx->lastFdCount);
309 #endif
310 do {
311 #ifndef USE_POLL
312 /* XXX need to copy only the bits we are using. */
313 ctx->rdLast = ctx->rdNext;
314 ctx->wrLast = ctx->wrNext;
315 ctx->exLast = ctx->exNext;
316 #else
318 * The pollfd structure uses separate fields for
319 * the input and output events (corresponding to
320 * the ??Next and ??Last fd sets), so there's no
321 * need to copy one to the other.
323 #endif /* USE_POLL */
324 if (m == Timer) {
325 INSIST(tp == &t);
326 t = evSubTime(nextTime, ctx->lastEventTime);
329 /* XXX should predict system's earliness and adjust. */
330 x = pselect(ctx->fdMax+1,
331 &ctx->rdLast, &ctx->wrLast, &ctx->exLast,
332 tp, NULL);
333 pselect_errno = errno;
335 #ifndef USE_POLL
336 evPrintf(ctx, 4, "select() returns %d (err: %s)\n",
337 x, (x == -1) ? strerror(errno) : "none");
338 #else
339 evPrintf(ctx, 4, "poll() returns %d (err: %s)\n",
340 x, (x == -1) ? strerror(errno) : "none");
341 #endif /* USE_POLL */
342 /* Anything but a poll can change the time. */
343 if (m != JustPoll)
344 ctx->lastEventTime = evNowTime();
346 /* Select() likes to finish about 10ms early. */
347 } while (x == 0 && m == Timer &&
348 evCmpTime(ctx->lastEventTime, nextTime) < 0);
349 #ifdef EVENTLIB_TIME_CHECKS
350 ctx->lastSelectTime = ctx->lastEventTime;
351 #endif
352 if (x < 0) {
353 if (pselect_errno == EINTR) {
354 if ((options & EV_NULL) != 0)
355 goto again;
356 OKNEW(new);
357 new->type = Null;
358 /* No data. */
359 opaqueEv->opaque = new;
360 return (0);
362 if (pselect_errno == EBADF) {
363 for (x = 0; x <= ctx->fdMax; x++) {
364 struct stat sb;
366 if (FD_ISSET(x, &ctx->rdNext) == 0 &&
367 FD_ISSET(x, &ctx->wrNext) == 0 &&
368 FD_ISSET(x, &ctx->exNext) == 0)
369 continue;
370 if (fstat(x, &sb) == -1 &&
371 errno == EBADF)
372 evPrintf(ctx, 1, "EBADF: %d\n",
375 abort();
377 EV_ERR(pselect_errno);
379 if (x == 0 && (nextTimer == NULL || !timerPast) &&
380 (options & EV_POLL))
381 EV_ERR(EWOULDBLOCK);
382 ctx->fdCount = x;
383 #ifdef EVENTLIB_TIME_CHECKS
384 ctx->lastFdCount = x;
385 #endif
387 INSIST(nextTimer || ctx->fdCount);
389 /* Timers go first since we'd like them to be accurate. */
390 if (nextTimer && !timerPast) {
391 /* Has anything happened since we blocked? */
392 timerPast = (evCmpTime(nextTime, ctx->lastEventTime) <= 0);
394 if (nextTimer && timerPast) {
395 OKNEW(new);
396 new->type = Timer;
397 new->u.timer.this = nextTimer;
398 opaqueEv->opaque = new;
399 return (0);
402 /* No timers, so there should be a ready file descriptor. */
403 x = 0;
404 while (ctx->fdCount > 0) {
405 evFile *fid;
406 int fd, eventmask;
408 if (ctx->fdNext == NULL) {
409 if (++x == 2) {
411 * Hitting the end twice means that the last
412 * select() found some FD's which have since
413 * been deselected.
415 * On some systems, the count returned by
416 * selects is the total number of bits in
417 * all masks that are set, and on others it's
418 * the number of fd's that have some bit set,
419 * and on others, it's just broken. We
420 * always assume that it's the number of
421 * bits set in all masks, because that's what
422 * the man page says it should do, and
423 * the worst that can happen is we do an
424 * extra select().
426 ctx->fdCount = 0;
427 break;
429 ctx->fdNext = ctx->files;
431 fid = ctx->fdNext;
432 ctx->fdNext = fid->next;
434 fd = fid->fd;
435 eventmask = 0;
436 if (FD_ISSET(fd, &ctx->rdLast))
437 eventmask |= EV_READ;
438 if (FD_ISSET(fd, &ctx->wrLast))
439 eventmask |= EV_WRITE;
440 if (FD_ISSET(fd, &ctx->exLast))
441 eventmask |= EV_EXCEPT;
442 eventmask &= fid->eventmask;
443 if (eventmask != 0) {
444 if ((eventmask & EV_READ) != 0) {
445 FD_CLR(fd, &ctx->rdLast);
446 ctx->fdCount--;
448 if ((eventmask & EV_WRITE) != 0) {
449 FD_CLR(fd, &ctx->wrLast);
450 ctx->fdCount--;
452 if ((eventmask & EV_EXCEPT) != 0) {
453 FD_CLR(fd, &ctx->exLast);
454 ctx->fdCount--;
456 OKNEW(new);
457 new->type = File;
458 new->u.file.this = fid;
459 new->u.file.eventmask = eventmask;
460 opaqueEv->opaque = new;
461 return (0);
464 if (ctx->fdCount < 0) {
466 * select()'s count is off on a number of systems, and
467 * can result in fdCount < 0.
469 evPrintf(ctx, 4, "fdCount < 0 (%d)\n", ctx->fdCount);
470 ctx->fdCount = 0;
473 /* We get here if the caller deselect()'s an FD. Gag me with a goto. */
474 goto again;
478 evDispatch(evContext opaqueCtx, evEvent opaqueEv) {
479 evContext_p *ctx = opaqueCtx.opaque;
480 evEvent_p *ev = opaqueEv.opaque;
481 #ifdef EVENTLIB_TIME_CHECKS
482 void *func;
483 struct timespec start_time;
484 struct timespec interval;
485 #endif
487 #ifdef EVENTLIB_TIME_CHECKS
488 if (ctx->debug > 0)
489 start_time = evNowTime();
490 #endif
491 ctx->cur = ev;
492 switch (ev->type) {
493 case Accept: {
494 evAccept *this = ev->u.accept.this;
496 evPrintf(ctx, 5,
497 "Dispatch.Accept: fd %d -> %d, func %p, uap %p\n",
498 this->conn->fd, this->fd,
499 this->conn->func, this->conn->uap);
500 errno = this->ioErrno;
501 (this->conn->func)(opaqueCtx, this->conn->uap, this->fd,
502 &this->la, this->lalen,
503 &this->ra, this->ralen);
504 #ifdef EVENTLIB_TIME_CHECKS
505 func = this->conn->func;
506 #endif
507 break;
509 case File: {
510 evFile *this = ev->u.file.this;
511 int eventmask = ev->u.file.eventmask;
513 evPrintf(ctx, 5,
514 "Dispatch.File: fd %d, mask 0x%x, func %p, uap %p\n",
515 this->fd, this->eventmask, this->func, this->uap);
516 (this->func)(opaqueCtx, this->uap, this->fd, eventmask);
517 #ifdef EVENTLIB_TIME_CHECKS
518 func = this->func;
519 #endif
520 break;
522 case Stream: {
523 evStream *this = ev->u.stream.this;
525 evPrintf(ctx, 5,
526 "Dispatch.Stream: fd %d, func %p, uap %p\n",
527 this->fd, this->func, this->uap);
528 errno = this->ioErrno;
529 (this->func)(opaqueCtx, this->uap, this->fd, this->ioDone);
530 #ifdef EVENTLIB_TIME_CHECKS
531 func = this->func;
532 #endif
533 break;
535 case Timer: {
536 evTimer *this = ev->u.timer.this;
538 evPrintf(ctx, 5, "Dispatch.Timer: func %p, uap %p\n",
539 this->func, this->uap);
540 (this->func)(opaqueCtx, this->uap, this->due, this->inter);
541 #ifdef EVENTLIB_TIME_CHECKS
542 func = this->func;
543 #endif
544 break;
546 case Wait: {
547 evWait *this = ev->u.wait.this;
549 evPrintf(ctx, 5,
550 "Dispatch.Wait: tag %p, func %p, uap %p\n",
551 this->tag, this->func, this->uap);
552 (this->func)(opaqueCtx, this->uap, this->tag);
553 #ifdef EVENTLIB_TIME_CHECKS
554 func = this->func;
555 #endif
556 break;
558 case Null: {
559 /* No work. */
560 #ifdef EVENTLIB_TIME_CHECKS
561 func = NULL;
562 #endif
563 break;
565 default: {
566 abort();
569 #ifdef EVENTLIB_TIME_CHECKS
570 if (ctx->debug > 0) {
571 interval = evSubTime(evNowTime(), start_time);
573 * Complain if it took longer than 50 milliseconds.
575 * We call getuid() to make an easy to find mark in a kernel
576 * trace.
578 if (interval.tv_sec > 0 || interval.tv_nsec > 50000000)
579 evPrintf(ctx, 1,
580 "dispatch interval %u.%09u uid %d type %d func %p\n",
581 interval.tv_sec, interval.tv_nsec,
582 getuid(), ev->type, func);
584 #endif
585 ctx->cur = NULL;
586 evDrop(opaqueCtx, opaqueEv);
587 return (0);
590 void
591 evDrop(evContext opaqueCtx, evEvent opaqueEv) {
592 evContext_p *ctx = opaqueCtx.opaque;
593 evEvent_p *ev = opaqueEv.opaque;
595 switch (ev->type) {
596 case Accept: {
597 FREE(ev->u.accept.this);
598 break;
600 case File: {
601 /* No work. */
602 break;
604 case Stream: {
605 evStreamID id;
607 id.opaque = ev->u.stream.this;
608 (void) evCancelRW(opaqueCtx, id);
609 break;
611 case Timer: {
612 evTimer *this = ev->u.timer.this;
613 evTimerID opaque;
615 /* Check to see whether the user func cleared the timer. */
616 if (heap_element(ctx->timers, this->index) != this) {
617 evPrintf(ctx, 5, "Dispatch.Timer: timer rm'd?\n");
618 break;
621 * Timer is still there. Delete it if it has expired,
622 * otherwise set it according to its next interval.
624 if (this->inter.tv_sec == (time_t)0 &&
625 this->inter.tv_nsec == 0L) {
626 opaque.opaque = this;
627 (void) evClearTimer(opaqueCtx, opaque);
628 } else {
629 opaque.opaque = this;
630 (void) evResetTimer(opaqueCtx, opaque, this->func,
631 this->uap,
632 evAddTime((this->mode & EV_TMR_RATE) ?
633 this->due :
634 ctx->lastEventTime,
635 this->inter),
636 this->inter);
638 break;
640 case Wait: {
641 FREE(ev->u.wait.this);
642 break;
644 case Null: {
645 /* No work. */
646 break;
648 default: {
649 abort();
652 FREE(ev);
656 evMainLoop(evContext opaqueCtx) {
657 evEvent event;
658 int x;
660 while ((x = evGetNext(opaqueCtx, &event, EV_WAIT)) == 0)
661 if ((x = evDispatch(opaqueCtx, event)) < 0)
662 break;
663 return (x);
667 evHighestFD(evContext opaqueCtx) {
668 evContext_p *ctx = opaqueCtx.opaque;
670 return (ctx->highestFD);
673 void
674 evPrintf(const evContext_p *ctx, int level, const char *fmt, ...) {
675 va_list ap;
677 va_start(ap, fmt);
678 if (ctx->output != NULL && ctx->debug >= level) {
679 vfprintf(ctx->output, fmt, ap);
680 fflush(ctx->output);
682 va_end(ap);
686 evSetOption(evContext *opaqueCtx, const char *option, int value) {
687 /* evContext_p *ctx = opaqueCtx->opaque; */
689 UNUSED(opaqueCtx);
690 UNUSED(value);
691 #ifndef CLOCK_MONOTONIC
692 UNUSED(option);
693 #endif
695 #ifdef CLOCK_MONOTONIC
696 if (strcmp(option, "monotime") == 0) {
697 if (opaqueCtx != NULL)
698 errno = EINVAL;
699 if (value == 0 || value == 1) {
700 __evOptMonoTime = value;
701 return (0);
702 } else {
703 errno = EINVAL;
704 return (-1);
707 #endif
708 errno = ENOENT;
709 return (-1);
713 evGetOption(evContext *opaqueCtx, const char *option, int *value) {
714 /* evContext_p *ctx = opaqueCtx->opaque; */
716 UNUSED(opaqueCtx);
717 #ifndef CLOCK_MONOTONIC
718 UNUSED(value);
719 UNUSED(option);
720 #endif
722 #ifdef CLOCK_MONOTONIC
723 if (strcmp(option, "monotime") == 0) {
724 if (opaqueCtx != NULL)
725 errno = EINVAL;
726 *value = __evOptMonoTime;
727 return (0);
729 #endif
730 errno = ENOENT;
731 return (-1);
734 #if defined(NEED_PSELECT) || defined(USE_POLL)
735 /* XXX needs to move to the porting library. */
736 static int
737 pselect(int nfds, void *rfds, void *wfds, void *efds,
738 struct timespec *tsp,
739 const sigset_t *sigmask)
741 struct timeval tv, *tvp;
742 sigset_t sigs;
743 int n;
744 #ifdef USE_POLL
745 int polltimeout = INFTIM;
746 evContext_p *ctx;
747 struct pollfd *fds;
748 nfds_t pnfds;
750 UNUSED(nfds);
751 #endif /* USE_POLL */
753 if (tsp) {
754 tvp = &tv;
755 tv = evTimeVal(*tsp);
756 #ifdef USE_POLL
757 polltimeout = 1000 * tv.tv_sec + tv.tv_usec / 1000;
758 #endif /* USE_POLL */
759 } else
760 tvp = NULL;
761 if (sigmask)
762 sigprocmask(SIG_SETMASK, sigmask, &sigs);
763 #ifndef USE_POLL
764 n = select(nfds, rfds, wfds, efds, tvp);
765 #else
767 * rfds, wfds, and efds should all be from the same evContext_p,
768 * so any of them will do. If they're all NULL, the caller is
769 * presumably calling us to block.
771 if (rfds != NULL)
772 ctx = ((__evEmulMask *)rfds)->ctx;
773 else if (wfds != NULL)
774 ctx = ((__evEmulMask *)wfds)->ctx;
775 else if (efds != NULL)
776 ctx = ((__evEmulMask *)efds)->ctx;
777 else
778 ctx = NULL;
779 if (ctx != NULL && ctx->fdMax != -1) {
780 fds = &(ctx->pollfds[ctx->firstfd]);
781 pnfds = ctx->fdMax - ctx->firstfd + 1;
782 } else {
783 fds = NULL;
784 pnfds = 0;
786 n = poll(fds, pnfds, polltimeout);
787 if (n > 0) {
788 int i, e;
790 INSIST(ctx != NULL);
791 for (e = 0, i = ctx->firstfd; i <= ctx->fdMax; i++) {
792 if (ctx->pollfds[i].fd < 0)
793 continue;
794 if (FD_ISSET(i, &ctx->rdLast))
795 e++;
796 if (FD_ISSET(i, &ctx->wrLast))
797 e++;
798 if (FD_ISSET(i, &ctx->exLast))
799 e++;
801 n = e;
803 #endif /* USE_POLL */
804 if (sigmask)
805 sigprocmask(SIG_SETMASK, &sigs, NULL);
806 if (tsp)
807 *tsp = evTimeSpec(tv);
808 return (n);
810 #endif
812 #ifdef USE_POLL
814 evPollfdRealloc(evContext_p *ctx, int pollfd_chunk_size, int fd) {
816 int i, maxnfds;
817 void *pollfds, *fdTable;
819 if (fd < ctx->maxnfds)
820 return (0);
822 /* Don't allow ridiculously small values for pollfd_chunk_size */
823 if (pollfd_chunk_size < 20)
824 pollfd_chunk_size = 20;
826 maxnfds = (1 + (fd/pollfd_chunk_size)) * pollfd_chunk_size;
828 pollfds = realloc(ctx->pollfds, maxnfds * sizeof(*ctx->pollfds));
829 if (pollfds != NULL)
830 ctx->pollfds = pollfds;
831 fdTable = realloc(ctx->fdTable, maxnfds * sizeof(*ctx->fdTable));
832 if (fdTable != NULL)
833 ctx->fdTable = fdTable;
835 if (pollfds == NULL || fdTable == NULL) {
836 evPrintf(ctx, 2, "pollfd() realloc (%ld) failed\n",
837 (long)maxnfds*sizeof(struct pollfd));
838 return (-1);
841 for (i = ctx->maxnfds; i < maxnfds; i++) {
842 ctx->pollfds[i].fd = -1;
843 ctx->pollfds[i].events = 0;
844 ctx->fdTable[i] = 0;
847 ctx->maxnfds = maxnfds;
849 return (0);
852 /* Find the appropriate 'events' or 'revents' field in the pollfds array */
853 short *
854 __fd_eventfield(int fd, __evEmulMask *maskp) {
856 evContext_p *ctx = (evContext_p *)maskp->ctx;
858 if (!maskp->result || maskp->type == EV_WASNONBLOCKING)
859 return (&(ctx->pollfds[fd].events));
860 else
861 return (&(ctx->pollfds[fd].revents));
864 /* Translate to poll(2) event */
865 short
866 __poll_event(__evEmulMask *maskp) {
868 switch ((maskp)->type) {
869 case EV_READ:
870 return (POLLRDNORM);
871 case EV_WRITE:
872 return (POLLWRNORM);
873 case EV_EXCEPT:
874 return (POLLRDBAND | POLLPRI | POLLWRBAND);
875 case EV_WASNONBLOCKING:
876 return (POLLHUP);
877 default:
878 return (0);
883 * Clear the events corresponding to the specified mask. If this leaves
884 * the events mask empty (apart from the POLLHUP bit), set the fd field
885 * to -1 so that poll(2) will ignore this fd.
887 void
888 __fd_clr(int fd, __evEmulMask *maskp) {
890 evContext_p *ctx = maskp->ctx;
892 *__fd_eventfield(fd, maskp) &= ~__poll_event(maskp);
893 if ((ctx->pollfds[fd].events & ~POLLHUP) == 0) {
894 ctx->pollfds[fd].fd = -1;
895 if (fd == ctx->fdMax)
896 while (ctx->fdMax > ctx->firstfd &&
897 ctx->pollfds[ctx->fdMax].fd < 0)
898 ctx->fdMax--;
899 if (fd == ctx->firstfd)
900 while (ctx->firstfd <= ctx->fdMax &&
901 ctx->pollfds[ctx->firstfd].fd < 0)
902 ctx->firstfd++;
904 * Do we have a empty set of descriptors?
906 if (ctx->firstfd > ctx->fdMax) {
907 ctx->fdMax = -1;
908 ctx->firstfd = 0;
914 * Set the events bit(s) corresponding to the specified mask. If the events
915 * field has any other bits than POLLHUP set, also set the fd field so that
916 * poll(2) will watch this fd.
918 void
919 __fd_set(int fd, __evEmulMask *maskp) {
921 evContext_p *ctx = maskp->ctx;
923 *__fd_eventfield(fd, maskp) |= __poll_event(maskp);
924 if ((ctx->pollfds[fd].events & ~POLLHUP) != 0) {
925 ctx->pollfds[fd].fd = fd;
926 if (fd < ctx->firstfd || ctx->fdMax == -1)
927 ctx->firstfd = fd;
928 if (fd > ctx->fdMax)
929 ctx->fdMax = fd;
932 #endif /* USE_POLL */
934 /*! \file */