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