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 saved_errno, ret, fd, maxfd = 0; 51 fd_set *readfds = NULL, *writefds = NULL, *exceptfds = NULL; 52 size_t nmemb; 53 54 for (i = 0; i < nfds; i++) { 55 fd = fds[i].fd; 56 if (fd != -1 && fd >= FD_SETSIZE) { 57 errno = EINVAL; 58 return -1; 59 } 60 maxfd = MAX(maxfd, fd); 61 } 62 63 nmemb = howmany(maxfd + 1 , NFDBITS); 64 if ((readfds = calloc(nmemb, sizeof(fd_mask))) == NULL || 65 (writefds = calloc(nmemb, sizeof(fd_mask))) == NULL || 66 (exceptfds = calloc(nmemb, sizeof(fd_mask))) == NULL) { 67 saved_errno = ENOMEM; 68 ret = -1; 69 goto out; 70 } 71 72 /* populate event bit vectors for the events we're interested in */ 73 for (i = 0; i < nfds; i++) { 74 fd = fds[i].fd; 75 if (fd == -1) 76 continue; 77 if (fds[i].events & POLLIN) 78 FD_SET(fd, readfds); 79 if (fds[i].events & POLLOUT) 80 FD_SET(fd, writefds); 81 if (fds[i].events & POLLPRI) 82 FD_SET(fd, exceptfds); 83 } 84 85 ret = pselect(maxfd + 1, readfds, writefds, exceptfds, tmoutp, sigmask); 86 saved_errno = errno; 87 88 /* scan through select results and set poll() flags */ 89 for (i = 0; i < nfds; i++) { 90 fd = fds[i].fd; 91 fds[i].revents = 0; 92 if (fd == -1) 93 continue; 94 if ((fds[i].events & POLLIN) && FD_ISSET(fd, readfds)) 95 fds[i].revents |= POLLIN; 96 if ((fds[i].events & POLLOUT) && FD_ISSET(fd, writefds)) 97 fds[i].revents |= POLLOUT; 98 if ((fds[i].events & POLLPRI) && FD_ISSET(fd, exceptfds)) 99 fds[i].revents |= POLLPRI; 100 } 101 102 out: 103 free(readfds); 104 free(writefds); 105 free(exceptfds); 106 if (ret == -1) 107 errno = saved_errno; 108 return ret; 109 } 110 #endif /* !HAVE_PPOLL || BROKEN_POLL */ 111 112 #if !defined(HAVE_POLL) || defined(BROKEN_POLL) 113 int 114 poll(struct pollfd *fds, nfds_t nfds, int timeout) 115 { 116 struct timespec ts, *tsp = NULL; 117 118 /* poll timeout is msec, ppoll is timespec (sec + nsec) */ 119 if (timeout >= 0) { 120 ts.tv_sec = timeout / 1000; 121 ts.tv_nsec = (timeout % 1000) * 1000000; 122 tsp = &ts; 123 } 124 125 return ppoll(fds, nfds, tsp, NULL); 126 } 127 #endif /* !HAVE_POLL || BROKEN_POLL */ 128 129 #endif /* !HAVE_PPOLL || !HAVE_POLL || BROKEN_POLL */ 130