2 * Copyright (c) 2004, 2005, 2007 Darren Tucker (dtucker at zip com au).
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #if !defined(HAVE_PPOLL) || !defined(HAVE_POLL) || defined(BROKEN_POLL)
20 #include <sys/types.h>
22 #ifdef HAVE_SYS_PARAM_H
23 # include <sys/param.h>
25 #ifdef HAVE_SYS_SELECT_H
26 # include <sys/select.h>
35 #if !defined(HAVE_PPOLL) || defined(BROKEN_POLL)
37 * A minimal implementation of ppoll(2), built on top of pselect(2).
39 * Only supports POLLIN, POLLOUT and POLLPRI flags in pfd.events and
40 * revents. Notably POLLERR, POLLHUP and POLLNVAL are not supported.
42 * Supports pfd.fd = -1 meaning "unused" although it's not standard.
46 ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmoutp,
47 const sigset_t *sigmask)
50 int ret, fd, maxfd = 0;
51 fd_set readfds, writefds, exceptfds;
53 for (i = 0; i < nfds; i++) {
55 if (fd != -1 && fd >= FD_SETSIZE) {
59 maxfd = MAX(maxfd, fd);
62 /* populate event bit vectors for the events we're interested in */
66 for (i = 0; i < nfds; i++) {
70 if (fds[i].events & POLLIN)
72 if (fds[i].events & POLLOUT)
73 FD_SET(fd, &writefds);
74 if (fds[i].events & POLLPRI)
75 FD_SET(fd, &exceptfds);
78 ret = pselect(maxfd + 1, &readfds, &writefds, &exceptfds, tmoutp, sigmask);
80 /* scan through select results and set poll() flags */
81 for (i = 0; i < nfds; i++) {
86 if ((fds[i].events & POLLIN) && FD_ISSET(fd, &readfds))
87 fds[i].revents |= POLLIN;
88 if ((fds[i].events & POLLOUT) && FD_ISSET(fd, &writefds))
89 fds[i].revents |= POLLOUT;
90 if ((fds[i].events & POLLPRI) && FD_ISSET(fd, &exceptfds))
91 fds[i].revents |= POLLPRI;
96 #endif /* !HAVE_PPOLL || BROKEN_POLL */
98 #if !defined(HAVE_POLL) || defined(BROKEN_POLL)
100 poll(struct pollfd *fds, nfds_t nfds, int timeout)
102 struct timespec ts, *tsp = NULL;
104 /* poll timeout is msec, ppoll is timespec (sec + nsec) */
106 ts.tv_sec = timeout / 1000;
107 ts.tv_nsec = (timeout % 1000) * 1000000;
111 return ppoll(fds, nfds, tsp, NULL);
113 #endif /* !HAVE_POLL || BROKEN_POLL */
115 #endif /* !HAVE_PPOLL || !HAVE_POLL || BROKEN_POLL */