1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/* Emulation for poll(2)
Contributed by Paolo Bonzini.
Copyright 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
This file is part of gnulib.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#include "config.h"
#include <sys/types.h>
#include "poll.h"
#include <errno.h>
#include <limits.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <unistd.h>
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#ifndef INFTIM
#define INFTIM (-1)
#endif
#ifndef EOVERFLOW
#define EOVERFLOW EINVAL
#endif
int
poll (pfd, nfd, timeout)
struct pollfd *pfd;
nfds_t nfd;
int timeout;
{
fd_set rfds, wfds, efds;
struct timeval tv, *ptv;
int maxfd, rc, happened;
nfds_t i;
#ifdef _SC_OPEN_MAX
if (nfd > sysconf (_SC_OPEN_MAX))
{
errno = EINVAL;
return -1;
}
#else /* !_SC_OPEN_MAX */
#ifdef OPEN_MAX
if (nfd > OPEN_MAX)
{
errno = EINVAL;
return -1;
}
#endif /* OPEN_MAX -- else, no check is needed */
#endif /* !_SC_OPEN_MAX */
/* EFAULT is not necessary to implement, but let's do it in the
simplest case. */
if (!pfd)
{
errno = EFAULT;
return -1;
}
/* convert timeout number into a timeval structure */
ptv = &tv;
if (timeout >= 0)
{
/* return immediately or after timeout */
ptv->tv_sec = timeout / 1000;
ptv->tv_usec = (timeout % 1000) * 1000;
}
else if (timeout == INFTIM)
/* wait forever */
ptv = NULL;
else
{
errno = EINVAL;
return -1;
}
/* create fd sets and determine max fd */
maxfd = -1;
FD_ZERO (&rfds);
FD_ZERO (&wfds);
FD_ZERO (&efds);
for (i = 0; i < nfd; i++)
{
if (pfd[i].fd < 0)
continue;
if (pfd[i].events & (POLLIN | POLLRDNORM))
FD_SET (pfd[i].fd, &rfds);
/* see select(2): "the only exceptional condition detectable
is out-of-band data received on a socket", hence we push
POLLWRBAND events onto wfds instead of efds. */
if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND))
FD_SET (pfd[i].fd, &wfds);
if (pfd[i].events & (POLLPRI | POLLRDBAND))
FD_SET (pfd[i].fd, &efds);
if (pfd[i].fd >= maxfd
&& (pfd[i].events & (POLLIN | POLLOUT | POLLPRI
| POLLRDNORM | POLLRDBAND
| POLLWRNORM | POLLWRBAND)))
{
maxfd = pfd[i].fd;
if (maxfd > FD_SETSIZE)
{
errno = EOVERFLOW;
return -1;
}
}
}
/* examine fd sets */
rc = select (maxfd + 1, &rfds, &wfds, &efds, ptv);
/* establish results */
if (rc > 0)
{
rc = 0;
for (i = 0; i < nfd; i++)
{
pfd[i].revents = 0;
if (pfd[i].fd < 0)
continue;
happened = 0;
if (FD_ISSET (pfd[i].fd, &rfds))
{
int r;
long avail = -1;
/* support for POLLHUP. */
#if defined __MACH__ && defined __APPLE__
/* There is a bug in Mac OS X that causes it to ignore MSG_PEEK for
some kinds of descriptors. Use FIONREAD to emulate POLLHUP.
It is still not completely POSIX compliant (it does not fully
work on TTYs), but at least it does not delete data! For other
platforms, we still use MSG_PEEK because it was proved to be
reliable, and I a leery of changing it. */
do
r = ioctl (pfd[i].fd, FIONREAD, &avail);
while (r == -1 && (errno == EAGAIN || errno == EINTR));
if (avail < 0)
avail = 0;
#else
char data[64];
r = recv (pfd[i].fd, data, 64, MSG_PEEK);
if (r == -1)
{
avail = (errno == ESHUTDOWN || errno == ECONNRESET ||
errno == ECONNABORTED || errno == ENETRESET) ? 0 : -1;
errno = 0;
}
else
avail = r;
#endif
/* An hung up descriptor does not increase the return value! */
if (avail == 0)
pfd[i].revents |= POLLHUP;
else if (avail == -1)
pfd[i].revents |= POLLERR;
else
happened |= POLLIN | POLLRDNORM;
}
if (FD_ISSET (pfd[i].fd, &wfds))
happened |= POLLOUT | POLLWRNORM | POLLWRBAND;
if (FD_ISSET (pfd[i].fd, &efds))
happened |= POLLPRI | POLLRDBAND;
pfd[i].revents |= pfd[i].events & happened;
rc += (happened > 0);
}
}
return rc;
}
|