|
|
1.1 ! root 1: #include <poll.h> ! 2: #include <sys/select.h> ! 3: #include <sys/time.h> ! 4: #include <kernel/param.h> ! 5: ! 6: #if (NOFILE < FD_SETSIZE) ! 7: #define FD_MAX NOFILE ! 8: #else ! 9: #define FD_MAX FD_SETSIZE ! 10: #endif ! 11: ! 12: extern int errno; ! 13: ! 14: /* ! 15: * Simulate BSD select() system call with poll(). ! 16: */ ! 17: int ! 18: select(nfd, rfds, wfds, xfds, to) ! 19: int nfd; ! 20: fd_set *rfds, *wfds, *xfds; ! 21: struct timeval * to; ! 22: { ! 23: int timeout; ! 24: struct pollfd pfd[FD_MAX]; ! 25: int fd; ! 26: struct pollfd * pfp; ! 27: int ret; ! 28: int i, npfd, events; ! 29: int npoll; ! 30: ! 31: if (nfd > FD_MAX) /* should report an error here */ ! 32: nfd = FD_MAX; ! 33: ! 34: /* ! 35: * "to" is NULL - blocking poll ! 36: * "to" is zero - return immediately ! 37: */ ! 38: if (to) { ! 39: timeout = to->tv_sec*1000 + to->tv_usec/1000; ! 40: } else ! 41: timeout = -1; ! 42: ! 43: /* ! 44: * Set fd and events fields in pfd. ! 45: */ ! 46: for (fd = 0, pfp = pfd, npfd = 0; fd < nfd; fd++) { ! 47: events = 0; ! 48: if (rfds && FD_ISSET(fd, rfds)) ! 49: events |= POLLIN; ! 50: if (wfds && FD_ISSET(fd, wfds)) ! 51: events |= POLLOUT; ! 52: if (events) { ! 53: pfp->events = events; ! 54: pfp->fd = fd; ! 55: npfd++; ! 56: pfp++; ! 57: } ! 58: } ! 59: ! 60: /* ! 61: * Do a poll() system call. ! 62: */ ! 63: if ((npoll=poll(pfd, npfd, timeout)) == -1) { ! 64: ret = -1; ! 65: goto done; ! 66: } ! 67: ! 68: /* ! 69: * Set return value and return bits in rfds and wfds. ! 70: * Punt on xfds. ! 71: */ ! 72: if (rfds) ! 73: FD_ZERO(rfds) ! 74: if (wfds) ! 75: FD_ZERO(wfds) ! 76: if (xfds) ! 77: FD_ZERO(xfds) ! 78: ret = 0; ! 79: for (i = 0, pfp = pfd; i < npfd; i++, pfp++) { ! 80: fd = pfp->fd; ! 81: if (fd >= 0 && fd < nfd) { ! 82: if (rfds && (pfp->revents & POLLIN)) { ! 83: FD_SET(fd, rfds); ! 84: ret++; ! 85: } ! 86: if (wfds && (pfp->revents & POLLOUT)) { ! 87: FD_SET(fd, wfds); ! 88: ret++; ! 89: } ! 90: } ! 91: } ! 92: ! 93: /* ! 94: * Bye. ! 95: */ ! 96: done: ! 97: return ret; ! 98: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.