1 /*---------------------------------------------------------------------------*\
2 $Id$
3
4 NAME
5
6 poll - select(2)-based poll() emulation function for BSD systems.
7
8 SYNOPSIS
9 #include "poll.h"
10
11 struct pollfd
12 {
13 int fd;
14 short events;
15 short revents;
16 }
17
18 int poll (struct pollfd *pArray, unsigned long n_fds, int timeout)
19
20 DESCRIPTION
21
22 This file, and the accompanying "poll.h", implement the System V
23 poll(2) system call for BSD systems (which typically do not provide
24 poll()). Poll() provides a method for multiplexing input and output
25 on multiple open file descriptors; in traditional BSD systems, that
26 capability is provided by select(). While the semantics of select()
27 differ from those of poll(), poll() can be readily emulated in terms
28 of select() -- which is how this function is implemented.
29
30 REFERENCES
31 Stevens, W. Richard. Unix Network Programming. Prentice-Hall, 1990.
32
33 NOTES
34 1. This software requires an ANSI C compiler.
35
36 LICENSE
37
38 This software is released under the following BSD license, adapted from
39 http://opensource.org/licenses/bsd-license.php
40
41 Copyright (c) 1995-2011, Brian M. Clapper
42 All rights reserved.
43
44 Redistribution and use in source and binary forms, with or without
45 modification, are permitted provided that the following conditions are met:
46
47 * Redistributions of source code must retain the above copyright notice,
48 this list of conditions and the following disclaimer.
49
50 * Redistributions in binary form must reproduce the above copyright
51 notice, this list of conditions and the following disclaimer in the
52 documentation and/or other materials provided with the distribution.
53
54 * Neither the name of the clapper.org nor the names of its contributors
55 may be used to endorse or promote products derived from this software
56 without specific prior written permission.
57
58 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
59 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
60 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
62 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
63 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
64 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
65 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
66 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
67 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
68 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 \*---------------------------------------------------------------------------*/
70
71
72 /*---------------------------------------------------------------------------*\
73 Includes
74 \*---------------------------------------------------------------------------*/
75
76 #ifdef HAVE_DIX_CONFIG_H
77 #include <dix-config.h>
78 #endif
79
80 #include <unistd.h> /* standard Unix definitions */
81 #include <sys/types.h> /* system types */
82 #include <sys/time.h> /* time definitions */
83 #include <assert.h> /* assertion macros */
84 #include <string.h> /* string functions */
85 #include "xserver_poll.h"
86
87 /*---------------------------------------------------------------------------*\
88 Macros
89 \*---------------------------------------------------------------------------*/
90
91 #ifndef MAX
92 #define MAX(a,b) ((a) > (b) ? (a) : (b))
93 #endif
94
95 /*---------------------------------------------------------------------------*\
96 Private Functions
97 \*---------------------------------------------------------------------------*/
98
map_poll_spec(struct pollfd * pArray,nfds_t n_fds,fd_set * pReadSet,fd_set * pWriteSet,fd_set * pExceptSet)99 static int map_poll_spec
100 (struct pollfd *pArray,
101 nfds_t n_fds,
102 fd_set *pReadSet,
103 fd_set *pWriteSet,
104 fd_set *pExceptSet)
105 {
106 register nfds_t i; /* loop control */
107 register struct pollfd *pCur; /* current array element */
108 register int max_fd = -1; /* return value */
109
110 /*
111 Map the poll() structures into the file descriptor sets required
112 by select().
113 */
114 for (i = 0, pCur = pArray; i < n_fds; i++, pCur++)
115 {
116 /* Skip any bad FDs in the array. */
117
118 if (pCur->fd < 0)
119 continue;
120
121 if (pCur->events & POLLIN)
122 {
123 /* "Input Ready" notification desired. */
124 FD_SET (pCur->fd, pReadSet);
125 }
126
127 if (pCur->events & POLLOUT)
128 {
129 /* "Output Possible" notification desired. */
130 FD_SET (pCur->fd, pWriteSet);
131 }
132
133 if (pCur->events & POLLPRI)
134 {
135 /*
136 "Exception Occurred" notification desired. (Exceptions
137 include out of band data.
138 */
139 FD_SET (pCur->fd, pExceptSet);
140 }
141
142 max_fd = MAX (max_fd, pCur->fd);
143 }
144
145 return max_fd;
146 }
147
map_timeout(int poll_timeout,struct timeval * pSelTimeout)148 static struct timeval *map_timeout
149 (int poll_timeout, struct timeval *pSelTimeout)
150 {
151 struct timeval *pResult;
152
153 /*
154 Map the poll() timeout value into a select() timeout. The possible
155 values of the poll() timeout value, and their meanings, are:
156
157 VALUE MEANING
158
159 -1 wait indefinitely (until signal occurs)
160 0 return immediately, don't block
161 >0 wait specified number of milliseconds
162
163 select() uses a "struct timeval", which specifies the timeout in
164 seconds and microseconds, so the milliseconds value has to be mapped
165 accordingly.
166 */
167
168 assert (pSelTimeout != (struct timeval *) NULL);
169
170 switch (poll_timeout)
171 {
172 case -1:
173 /*
174 A NULL timeout structure tells select() to wait indefinitely.
175 */
176 pResult = (struct timeval *) NULL;
177 break;
178
179 case 0:
180 /*
181 "Return immediately" (test) is specified by all zeros in
182 a timeval structure.
183 */
184 pSelTimeout->tv_sec = 0;
185 pSelTimeout->tv_usec = 0;
186 pResult = pSelTimeout;
187 break;
188
189 default:
190 /* Wait the specified number of milliseconds. */
191 pSelTimeout->tv_sec = poll_timeout / 1000; /* get seconds */
192 poll_timeout %= 1000; /* remove seconds */
193 pSelTimeout->tv_usec = poll_timeout * 1000; /* get microseconds */
194 pResult = pSelTimeout;
195 break;
196 }
197
198
199 return pResult;
200 }
201
map_select_results(struct pollfd * pArray,unsigned long n_fds,fd_set * pReadSet,fd_set * pWriteSet,fd_set * pExceptSet)202 static void map_select_results
203 (struct pollfd *pArray,
204 unsigned long n_fds,
205 fd_set *pReadSet,
206 fd_set *pWriteSet,
207 fd_set *pExceptSet)
208 {
209 register unsigned long i; /* loop control */
210 register struct pollfd *pCur; /* current array element */
211
212 for (i = 0, pCur = pArray; i < n_fds; i++, pCur++)
213 {
214 /* Skip any bad FDs in the array. */
215
216 if (pCur->fd < 0)
217 continue;
218
219 /* Exception events take priority over input events. */
220
221 pCur->revents = 0;
222 if (FD_ISSET (pCur->fd, pExceptSet))
223 pCur->revents |= POLLPRI;
224
225 else if (FD_ISSET (pCur->fd, pReadSet))
226 pCur->revents |= POLLIN;
227
228 if (FD_ISSET (pCur->fd, pWriteSet))
229 pCur->revents |= POLLOUT;
230 }
231
232 return;
233 }
234
235 /*---------------------------------------------------------------------------*\
236 Public Functions
237 \*---------------------------------------------------------------------------*/
238
xserver_poll(struct pollfd * pArray,unsigned long n_fds,int timeout)239 int xserver_poll
240 (struct pollfd *pArray, unsigned long n_fds, int timeout)
241 {
242 fd_set read_descs; /* input file descs */
243 fd_set write_descs; /* output file descs */
244 fd_set except_descs; /* exception descs */
245 struct timeval stime; /* select() timeout value */
246 int ready_descriptors; /* function result */
247 int max_fd; /* maximum fd value */
248 struct timeval *pTimeout; /* actually passed */
249
250 FD_ZERO (&read_descs);
251 FD_ZERO (&write_descs);
252 FD_ZERO (&except_descs);
253
254 assert (pArray != (struct pollfd *) NULL);
255
256 /* Map the poll() file descriptor list in the select() data structures. */
257
258 max_fd = map_poll_spec (pArray, n_fds,
259 &read_descs, &write_descs, &except_descs);
260
261 /* Map the poll() timeout value in the select() timeout structure. */
262
263 pTimeout = map_timeout (timeout, &stime);
264
265 /* Make the select() call. */
266
267 ready_descriptors = select (max_fd + 1, &read_descs, &write_descs,
268 &except_descs, pTimeout);
269
270 if (ready_descriptors >= 0)
271 {
272 map_select_results (pArray, n_fds,
273 &read_descs, &write_descs, &except_descs);
274 }
275
276 return ready_descriptors;
277 }
278