bsd-poll.c revision 262566
197403Sobrien/* $Id: bsd-poll.c,v 1.5 2013/11/08 10:12:58 dtucker Exp $ */ 297403Sobrien 397403Sobrien/* 497403Sobrien * Copyright (c) 2004, 2005, 2007 Darren Tucker (dtucker at zip com au). 597403Sobrien * 697403Sobrien * Permission to use, copy, modify, and distribute this software for any 797403Sobrien * purpose with or without fee is hereby granted, provided that the above 897403Sobrien * copyright notice and this permission notice appear in all copies. 997403Sobrien * 1097403Sobrien * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1197403Sobrien * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1297403Sobrien * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1397403Sobrien * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1497403Sobrien * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1597403Sobrien * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1697403Sobrien * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1797403Sobrien */ 1897403Sobrien 1997403Sobrien#include "includes.h" 2097403Sobrien#if !defined(HAVE_POLL) 2197403Sobrien 2297403Sobrien#include <sys/types.h> 2397403Sobrien#include <sys/time.h> 2497403Sobrien#ifdef HAVE_SYS_SELECT_H 2597403Sobrien# include <sys/select.h> 2697403Sobrien#endif 2797403Sobrien 2897403Sobrien#include <errno.h> 2997403Sobrien#include <stdlib.h> 3097403Sobrien#include <unistd.h> 3197403Sobrien#include "bsd-poll.h" 3297403Sobrien 3397403Sobrien/* 3497403Sobrien * A minimal implementation of poll(2), built on top of select(2). 3597403Sobrien * 3697403Sobrien * Only supports POLLIN and POLLOUT flags in pfd.events, and POLLIN, POLLOUT 3797403Sobrien * and POLLERR flags in revents. 3897403Sobrien * 39102782Skan * Supports pfd.fd = -1 meaning "unused" although it's not standard. 40102782Skan */ 41102782Skan 42102782Skanint 43102782Skanpoll(struct pollfd *fds, nfds_t nfds, int timeout) 4497403Sobrien{ 45102782Skan nfds_t i; 4697403Sobrien int saved_errno, ret, fd, maxfd = 0; 47102782Skan fd_set *readfds = NULL, *writefds = NULL, *exceptfds = NULL; 48102782Skan size_t nmemb; 49102782Skan struct timeval tv, *tvp = NULL; 50102782Skan 51102782Skan for (i = 0; i < nfds; i++) { 52102782Skan fd = fds[i].fd; 53102782Skan if (fd >= FD_SETSIZE) { 54102782Skan errno = EINVAL; 55102782Skan return -1; 56102782Skan } 57102782Skan maxfd = MAX(maxfd, fd); 58102782Skan } 59102782Skan 60102782Skan nmemb = howmany(maxfd + 1 , NFDBITS); 61102782Skan if ((readfds = calloc(nmemb, sizeof(fd_mask))) == NULL || 6297403Sobrien (writefds = calloc(nmemb, sizeof(fd_mask))) == NULL || 63 (exceptfds = calloc(nmemb, sizeof(fd_mask))) == NULL) { 64 saved_errno = ENOMEM; 65 ret = -1; 66 goto out; 67 } 68 69 /* populate event bit vectors for the events we're interested in */ 70 for (i = 0; i < nfds; i++) { 71 fd = fds[i].fd; 72 if (fd == -1) 73 continue; 74 if (fds[i].events & POLLIN) { 75 FD_SET(fd, readfds); 76 FD_SET(fd, exceptfds); 77 } 78 if (fds[i].events & POLLOUT) { 79 FD_SET(fd, writefds); 80 FD_SET(fd, exceptfds); 81 } 82 } 83 84 /* poll timeout is msec, select is timeval (sec + usec) */ 85 if (timeout >= 0) { 86 tv.tv_sec = timeout / 1000; 87 tv.tv_usec = (timeout % 1000) * 1000; 88 tvp = &tv; 89 } 90 91 ret = select(maxfd + 1, readfds, writefds, exceptfds, tvp); 92 saved_errno = errno; 93 94 /* scan through select results and set poll() flags */ 95 for (i = 0; i < nfds; i++) { 96 fd = fds[i].fd; 97 fds[i].revents = 0; 98 if (fd == -1) 99 continue; 100 if (FD_ISSET(fd, readfds)) { 101 fds[i].revents |= POLLIN; 102 } 103 if (FD_ISSET(fd, writefds)) { 104 fds[i].revents |= POLLOUT; 105 } 106 if (FD_ISSET(fd, exceptfds)) { 107 fds[i].revents |= POLLERR; 108 } 109 } 110 111out: 112 if (readfds != NULL) 113 free(readfds); 114 if (writefds != NULL) 115 free(writefds); 116 if (exceptfds != NULL) 117 free(exceptfds); 118 if (ret == -1) 119 errno = saved_errno; 120 return ret; 121} 122#endif 123