1d4af9e69SDag-Erling Smørgrav /*
2d4af9e69SDag-Erling Smørgrav * Copyright (c) 2004, 2005, 2007 Darren Tucker (dtucker at zip com au).
3d4af9e69SDag-Erling Smørgrav *
4d4af9e69SDag-Erling Smørgrav * Permission to use, copy, modify, and distribute this software for any
5d4af9e69SDag-Erling Smørgrav * purpose with or without fee is hereby granted, provided that the above
6d4af9e69SDag-Erling Smørgrav * copyright notice and this permission notice appear in all copies.
7d4af9e69SDag-Erling Smørgrav *
8d4af9e69SDag-Erling Smørgrav * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9d4af9e69SDag-Erling Smørgrav * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10d4af9e69SDag-Erling Smørgrav * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11d4af9e69SDag-Erling Smørgrav * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12d4af9e69SDag-Erling Smørgrav * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13d4af9e69SDag-Erling Smørgrav * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14d4af9e69SDag-Erling Smørgrav * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15d4af9e69SDag-Erling Smørgrav */
16d4af9e69SDag-Erling Smørgrav
17d4af9e69SDag-Erling Smørgrav #include "includes.h"
181323ec57SEd Maste #if !defined(HAVE_PPOLL) || !defined(HAVE_POLL) || defined(BROKEN_POLL)
19d4af9e69SDag-Erling Smørgrav
20f7167e0eSDag-Erling Smørgrav #include <sys/types.h>
21f7167e0eSDag-Erling Smørgrav #include <sys/time.h>
221323ec57SEd Maste #ifdef HAVE_SYS_PARAM_H
231323ec57SEd Maste # include <sys/param.h>
241323ec57SEd Maste #endif
25d4af9e69SDag-Erling Smørgrav #ifdef HAVE_SYS_SELECT_H
26d4af9e69SDag-Erling Smørgrav # include <sys/select.h>
27d4af9e69SDag-Erling Smørgrav #endif
28d4af9e69SDag-Erling Smørgrav
29d4af9e69SDag-Erling Smørgrav #include <errno.h>
301323ec57SEd Maste #include <signal.h>
31f7167e0eSDag-Erling Smørgrav #include <stdlib.h>
32f7167e0eSDag-Erling Smørgrav #include <unistd.h>
33d4af9e69SDag-Erling Smørgrav #include "bsd-poll.h"
34d4af9e69SDag-Erling Smørgrav
351323ec57SEd Maste #if !defined(HAVE_PPOLL) || defined(BROKEN_POLL)
36d4af9e69SDag-Erling Smørgrav /*
371323ec57SEd Maste * A minimal implementation of ppoll(2), built on top of pselect(2).
38d4af9e69SDag-Erling Smørgrav *
391323ec57SEd Maste * Only supports POLLIN, POLLOUT and POLLPRI flags in pfd.events and
401323ec57SEd Maste * revents. Notably POLLERR, POLLHUP and POLLNVAL are not supported.
41d4af9e69SDag-Erling Smørgrav *
42d4af9e69SDag-Erling Smørgrav * Supports pfd.fd = -1 meaning "unused" although it's not standard.
43d4af9e69SDag-Erling Smørgrav */
44d4af9e69SDag-Erling Smørgrav
45d4af9e69SDag-Erling Smørgrav int
ppoll(struct pollfd * fds,nfds_t nfds,const struct timespec * tmoutp,const sigset_t * sigmask)461323ec57SEd Maste ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmoutp,
471323ec57SEd Maste const sigset_t *sigmask)
48d4af9e69SDag-Erling Smørgrav {
49d4af9e69SDag-Erling Smørgrav nfds_t i;
50*f374ba41SEd Maste int ret, fd, maxfd = 0;
51*f374ba41SEd Maste fd_set readfds, writefds, exceptfds;
52d4af9e69SDag-Erling Smørgrav
53d4af9e69SDag-Erling Smørgrav for (i = 0; i < nfds; i++) {
54cce7d346SDag-Erling Smørgrav fd = fds[i].fd;
551323ec57SEd Maste if (fd != -1 && fd >= FD_SETSIZE) {
56d4af9e69SDag-Erling Smørgrav errno = EINVAL;
57d4af9e69SDag-Erling Smørgrav return -1;
58d4af9e69SDag-Erling Smørgrav }
59cce7d346SDag-Erling Smørgrav maxfd = MAX(maxfd, fd);
60d4af9e69SDag-Erling Smørgrav }
61d4af9e69SDag-Erling Smørgrav
62d4af9e69SDag-Erling Smørgrav /* populate event bit vectors for the events we're interested in */
63*f374ba41SEd Maste FD_ZERO(&readfds);
64*f374ba41SEd Maste FD_ZERO(&writefds);
65*f374ba41SEd Maste FD_ZERO(&exceptfds);
66d4af9e69SDag-Erling Smørgrav for (i = 0; i < nfds; i++) {
67d4af9e69SDag-Erling Smørgrav fd = fds[i].fd;
68d4af9e69SDag-Erling Smørgrav if (fd == -1)
69d4af9e69SDag-Erling Smørgrav continue;
701323ec57SEd Maste if (fds[i].events & POLLIN)
71*f374ba41SEd Maste FD_SET(fd, &readfds);
721323ec57SEd Maste if (fds[i].events & POLLOUT)
73*f374ba41SEd Maste FD_SET(fd, &writefds);
741323ec57SEd Maste if (fds[i].events & POLLPRI)
75*f374ba41SEd Maste FD_SET(fd, &exceptfds);
76d4af9e69SDag-Erling Smørgrav }
77d4af9e69SDag-Erling Smørgrav
78*f374ba41SEd Maste ret = pselect(maxfd + 1, &readfds, &writefds, &exceptfds, tmoutp, sigmask);
79d4af9e69SDag-Erling Smørgrav
80d4af9e69SDag-Erling Smørgrav /* scan through select results and set poll() flags */
81d4af9e69SDag-Erling Smørgrav for (i = 0; i < nfds; i++) {
82d4af9e69SDag-Erling Smørgrav fd = fds[i].fd;
83d4af9e69SDag-Erling Smørgrav fds[i].revents = 0;
84d4af9e69SDag-Erling Smørgrav if (fd == -1)
85d4af9e69SDag-Erling Smørgrav continue;
86*f374ba41SEd Maste if ((fds[i].events & POLLIN) && FD_ISSET(fd, &readfds))
87d4af9e69SDag-Erling Smørgrav fds[i].revents |= POLLIN;
88*f374ba41SEd Maste if ((fds[i].events & POLLOUT) && FD_ISSET(fd, &writefds))
89d4af9e69SDag-Erling Smørgrav fds[i].revents |= POLLOUT;
90*f374ba41SEd Maste if ((fds[i].events & POLLPRI) && FD_ISSET(fd, &exceptfds))
911323ec57SEd Maste fds[i].revents |= POLLPRI;
92d4af9e69SDag-Erling Smørgrav }
93d4af9e69SDag-Erling Smørgrav
94d4af9e69SDag-Erling Smørgrav return ret;
95d4af9e69SDag-Erling Smørgrav }
961323ec57SEd Maste #endif /* !HAVE_PPOLL || BROKEN_POLL */
971323ec57SEd Maste
981323ec57SEd Maste #if !defined(HAVE_POLL) || defined(BROKEN_POLL)
991323ec57SEd Maste int
poll(struct pollfd * fds,nfds_t nfds,int timeout)1001323ec57SEd Maste poll(struct pollfd *fds, nfds_t nfds, int timeout)
1011323ec57SEd Maste {
1021323ec57SEd Maste struct timespec ts, *tsp = NULL;
1031323ec57SEd Maste
1041323ec57SEd Maste /* poll timeout is msec, ppoll is timespec (sec + nsec) */
1051323ec57SEd Maste if (timeout >= 0) {
1061323ec57SEd Maste ts.tv_sec = timeout / 1000;
1071323ec57SEd Maste ts.tv_nsec = (timeout % 1000) * 1000000;
1081323ec57SEd Maste tsp = &ts;
1091323ec57SEd Maste }
1101323ec57SEd Maste
1111323ec57SEd Maste return ppoll(fds, nfds, tsp, NULL);
1121323ec57SEd Maste }
1131323ec57SEd Maste #endif /* !HAVE_POLL || BROKEN_POLL */
1141323ec57SEd Maste
1151323ec57SEd Maste #endif /* !HAVE_PPOLL || !HAVE_POLL || BROKEN_POLL */
116