2278aa8b31a4de43751c01f0906aca005dd579ca
[gnulib.git] / lib / poll.c
1 /* Emulation for poll(2)
2    Contributed by Paolo Bonzini.
3
4    Copyright 2001, 2002, 2003, 2006, 2007 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 #ifndef EOVERFLOW
47 #define EOVERFLOW EINVAL
48 #endif
49
50 int
51 poll (pfd, nfd, timeout)
52      struct pollfd *pfd;
53      nfds_t nfd;
54      int timeout;
55 {
56   fd_set rfds, wfds, efds;
57   struct timeval tv, *ptv;
58   int maxfd, rc;
59   nfds_t i;
60
61 #ifdef _SC_OPEN_MAX
62   if (nfd > sysconf (_SC_OPEN_MAX))
63     {
64       errno = EINVAL;
65       return -1;
66     }
67 #else /* !_SC_OPEN_MAX */
68 #ifdef OPEN_MAX
69   if (nfd > OPEN_MAX)
70     {
71       errno = EINVAL;
72       return -1;
73     }
74 #endif /* OPEN_MAX -- else, no check is needed */
75 #endif /* !_SC_OPEN_MAX */
76
77   /* EFAULT is not necessary to implement, but let's do it in the
78      simplest case. */
79   if (!pfd)
80     {
81       errno = EFAULT;
82       return -1;
83     }
84
85   /* convert timeout number into a timeval structure */
86   ptv = &tv;
87   if (timeout >= 0)
88     {
89       /* return immediately or after timeout */
90       ptv->tv_sec = timeout / 1000;
91       ptv->tv_usec = (timeout % 1000) * 1000;
92     }
93   else if (timeout == INFTIM)
94     /* wait forever */
95     ptv = NULL;
96   else
97     {
98       errno = EINVAL;
99       return -1;
100     }
101
102   /* create fd sets and determine max fd */
103   maxfd = -1;
104   FD_ZERO (&rfds);
105   FD_ZERO (&wfds);
106   FD_ZERO (&efds);
107   for (i = 0; i < nfd; i++)
108     {
109       if (pfd[i].fd < 0)
110         continue;
111
112       if (pfd[i].events & (POLLIN | POLLRDNORM))
113         FD_SET (pfd[i].fd, &rfds);
114
115       /* see select(2): "the only exceptional condition detectable
116          is out-of-band data received on a socket", hence we push
117          POLLWRBAND events onto wfds instead of efds. */
118       if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND))
119         FD_SET (pfd[i].fd, &wfds);
120       if (pfd[i].events & (POLLPRI | POLLRDBAND))
121         FD_SET (pfd[i].fd, &efds);
122       if (pfd[i].fd >= maxfd
123           && (pfd[i].events & (POLLIN | POLLOUT | POLLPRI
124                                | POLLRDNORM | POLLRDBAND
125                                | POLLWRNORM | POLLWRBAND)))
126         {
127           maxfd = pfd[i].fd;
128           if (maxfd > FD_SETSIZE)
129             {
130               errno = EOVERFLOW;
131               return -1;
132             }
133         }
134     }
135
136   /* examine fd sets */
137   rc = select (maxfd + 1, &rfds, &wfds, &efds, ptv);
138   if (rc < 0)
139     return rc;
140
141   /* establish results */
142   rc = 0;
143   for (i = 0; i < nfd; i++)
144     if (pfd[i].fd < 0)
145       pfd[i].revents = 0;
146     else
147       {
148         int happened = 0, sought = pfd[i].events;
149         if (FD_ISSET (pfd[i].fd, &rfds))
150           {
151             int r;
152
153 #if defined __MACH__ && defined __APPLE__
154             /* There is a bug in Mac OS X that causes it to ignore MSG_PEEK
155                for some kinds of descriptors.  Detect if this descriptor is a
156                connected socket, a server socket, or something else using a
157                0-byte recv, and use ioctl(2) to detect POLLHUP.  */
158             r = recv (pfd[i].fd, NULL, 0, MSG_PEEK);
159             if (r == 0 || errno == ENOTSOCK)
160               ioctl(pfd[i].fd, FIONREAD, &r);
161 #else
162             char data[64];
163             r = recv (pfd[i].fd, data, sizeof (data), MSG_PEEK);
164 #endif
165             if (r == 0)
166               happened |= POLLHUP;
167
168             /* If the event happened on an unconnected server socket,
169                that's fine. */
170             else if (r > 0 || ( /* (r == -1) && */ errno == ENOTCONN))
171               happened |= (POLLIN | POLLRDNORM) & sought;
172
173             /* Distinguish hung-up sockets from other errors.  */
174             else if (errno == ESHUTDOWN || errno == ECONNRESET
175                      || errno == ECONNABORTED || errno == ENETRESET)
176               happened |= POLLHUP;
177
178             else
179               happened |= POLLERR;
180           }
181
182         if (FD_ISSET (pfd[i].fd, &wfds))
183           happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought;
184
185         if (FD_ISSET (pfd[i].fd, &efds))
186           happened |= (POLLPRI | POLLRDBAND) & sought;
187
188         if (happened)
189           {
190             pfd[i].revents = happened;
191             rc++;
192           }
193       }
194
195   return rc;
196 }