1 /* 2 * Copyright (c) 2004, 2005, 2007 Darren Tucker (dtucker at zip com au). 3 * 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. 7 * 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. 15 */ 16 17 #include "includes.h" 18 #if !defined(HAVE_PPOLL) || !defined(HAVE_POLL) || defined(BROKEN_POLL) 19 20 #include <sys/types.h> 21 #include <sys/time.h> 22 #ifdef HAVE_SYS_PARAM_H 23 # include <sys/param.h> 24 #endif 25 #ifdef HAVE_SYS_SELECT_H 26 # include <sys/select.h> 27 #endif 28 29 #include <errno.h> 30 #include <signal.h> 31 #include <stdlib.h> 32 #include <unistd.h> 33 #include "bsd-poll.h" 34 35 #if !defined(HAVE_PPOLL) || defined(BROKEN_POLL) 36 /* 37 * A minimal implementation of ppoll(2), built on top of pselect(2). 38 * 39 * Only supports POLLIN, POLLOUT and POLLPRI flags in pfd.events and 40 * revents. Notably POLLERR, POLLHUP and POLLNVAL are not supported. 41 * 42 * Supports pfd.fd = -1 meaning "unused" although it's not standard. 43 */ 44 45 int 46 ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmoutp, 47 const sigset_t *sigmask) 48 { 49 nfds_t i; 50 int ret, fd, maxfd = 0; 51 fd_set readfds, writefds, exceptfds; 52 53 for (i = 0; i < nfds; i++) { 54 fd = fds[i].fd; 55 if (fd != -1 && fd >= FD_SETSIZE) { 56 errno = EINVAL; 57 return -1; 58 } 59 maxfd = MAX(maxfd, fd); 60 } 61 62 /* populate event bit vectors for the events we're interested in */ 63 FD_ZERO(&readfds); 64 FD_ZERO(&writefds); 65 FD_ZERO(&exceptfds); 66 for (i = 0; i < nfds; i++) { 67 fd = fds[i].fd; 68 if (fd == -1) 69 continue; 70 if (fds[i].events & POLLIN) 71 FD_SET(fd, &readfds); 72 if (fds[i].events & POLLOUT) 73 FD_SET(fd, &writefds); 74 if (fds[i].events & POLLPRI) 75 FD_SET(fd, &exceptfds); 76 } 77 78 ret = pselect(maxfd + 1, &readfds, &writefds, &exceptfds, tmoutp, sigmask); 79 80 /* scan through select results and set poll() flags */ 81 for (i = 0; i < nfds; i++) { 82 fd = fds[i].fd; 83 fds[i].revents = 0; 84 if (fd == -1) 85 continue; 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; 92 } 93 94 return ret; 95 } 96 #endif /* !HAVE_PPOLL || BROKEN_POLL */ 97 98 #if !defined(HAVE_POLL) || defined(BROKEN_POLL) 99 int 100 poll(struct pollfd *fds, nfds_t nfds, int timeout) 101 { 102 struct timespec ts, *tsp = NULL; 103 104 /* poll timeout is msec, ppoll is timespec (sec + nsec) */ 105 if (timeout >= 0) { 106 ts.tv_sec = timeout / 1000; 107 ts.tv_nsec = (timeout % 1000) * 1000000; 108 tsp = &ts; 109 } 110 111 return ppoll(fds, nfds, tsp, NULL); 112 } 113 #endif /* !HAVE_POLL || BROKEN_POLL */ 114 115 #endif /* !HAVE_PPOLL || !HAVE_POLL || BROKEN_POLL */ 116