1 /*---------------------------------------------------------------------------*\
6 poll - select(2)-based poll() emulation function for BSD systems.
18 int poll (struct pollfd *pArray, unsigned long n_fds, int timeout)
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.
31 Stevens, W. Richard. Unix Network Programming. Prentice-Hall, 1990.
34 1. This software requires an ANSI C compiler.
38 This software is released under the following BSD license, adapted from
39 http://opensource.org/licenses/bsd-license.php
41 Copyright (c) 1995-2011, Brian M. Clapper
44 Redistribution and use in source and binary forms, with or without
45 modification, are permitted provided that the following conditions are met:
47 * Redistributions of source code must retain the above copyright notice,
48 this list of conditions and the following disclaimer.
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.
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.
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 \*---------------------------------------------------------------------------*/
72 /*---------------------------------------------------------------------------*\
74 \*---------------------------------------------------------------------------*/
76 #include <dix-config.h>
78 #include <unistd.h> /* standard Unix definitions */
79 #include <sys/types.h> /* system types */
80 #include <sys/time.h> /* time definitions */
81 #include <assert.h> /* assertion macros */
82 #include <string.h> /* string functions */
84 #if defined(WIN32) && !defined(__CYGWIN__)
85 #include <X11/Xwinsock.h>
88 #include "os/xserver_poll.h"
90 /*---------------------------------------------------------------------------*\
92 \*---------------------------------------------------------------------------*/
95 #define MAX(a,b) ((a) > (b) ? (a) : (b))
98 /*---------------------------------------------------------------------------*\
100 \*---------------------------------------------------------------------------*/
102 static int map_poll_spec
103 (struct pollfd
*pArray
,
109 register nfds_t i
; /* loop control */
110 register struct pollfd
*pCur
; /* current array element */
111 register int max_fd
= -1; /* return value */
114 Map the poll() structures into the file descriptor sets required
117 for (i
= 0, pCur
= pArray
; i
< n_fds
; i
++, pCur
++)
119 /* Skip any bad FDs in the array. */
124 if (pCur
->events
& POLLIN
)
126 /* "Input Ready" notification desired. */
127 FD_SET (pCur
->fd
, pReadSet
);
130 if (pCur
->events
& POLLOUT
)
132 /* "Output Possible" notification desired. */
133 FD_SET (pCur
->fd
, pWriteSet
);
136 if (pCur
->events
& POLLPRI
)
139 "Exception Occurred" notification desired. (Exceptions
140 include out of band data.
142 FD_SET (pCur
->fd
, pExceptSet
);
145 max_fd
= MAX (max_fd
, pCur
->fd
);
151 static struct timeval
*map_timeout
152 (int poll_timeout
, struct timeval
*pSelTimeout
)
154 struct timeval
*pResult
;
157 Map the poll() timeout value into a select() timeout. The possible
158 values of the poll() timeout value, and their meanings, are:
162 -1 wait indefinitely (until signal occurs)
163 0 return immediately, don't block
164 >0 wait specified number of milliseconds
166 select() uses a "struct timeval", which specifies the timeout in
167 seconds and microseconds, so the milliseconds value has to be mapped
171 assert (pSelTimeout
!= (struct timeval
*) NULL
);
173 switch (poll_timeout
)
177 A NULL timeout structure tells select() to wait indefinitely.
179 pResult
= (struct timeval
*) NULL
;
184 "Return immediately" (test) is specified by all zeros in
187 pSelTimeout
->tv_sec
= 0;
188 pSelTimeout
->tv_usec
= 0;
189 pResult
= pSelTimeout
;
193 /* Wait the specified number of milliseconds. */
194 pSelTimeout
->tv_sec
= poll_timeout
/ 1000; /* get seconds */
195 poll_timeout
%= 1000; /* remove seconds */
196 pSelTimeout
->tv_usec
= poll_timeout
* 1000; /* get microseconds */
197 pResult
= pSelTimeout
;
205 static void map_select_results
206 (struct pollfd
*pArray
,
212 register unsigned long i
; /* loop control */
213 register struct pollfd
*pCur
; /* current array element */
215 for (i
= 0, pCur
= pArray
; i
< n_fds
; i
++, pCur
++)
217 /* Skip any bad FDs in the array. */
222 /* Exception events take priority over input events. */
225 if (FD_ISSET (pCur
->fd
, pExceptSet
))
226 pCur
->revents
|= POLLPRI
;
228 else if (FD_ISSET (pCur
->fd
, pReadSet
))
229 pCur
->revents
|= POLLIN
;
231 if (FD_ISSET (pCur
->fd
, pWriteSet
))
232 pCur
->revents
|= POLLOUT
;
238 /*---------------------------------------------------------------------------*\
240 \*---------------------------------------------------------------------------*/
243 (struct pollfd
*pArray
, unsigned long n_fds
, int timeout
)
245 fd_set read_descs
; /* input file descs */
246 fd_set write_descs
; /* output file descs */
247 fd_set except_descs
; /* exception descs */
248 struct timeval stime
; /* select() timeout value */
249 int ready_descriptors
; /* function result */
250 int max_fd
; /* maximum fd value */
251 struct timeval
*pTimeout
; /* actually passed */
253 FD_ZERO (&read_descs
);
254 FD_ZERO (&write_descs
);
255 FD_ZERO (&except_descs
);
257 assert (pArray
!= (struct pollfd
*) NULL
);
259 /* Map the poll() file descriptor list in the select() data structures. */
261 max_fd
= map_poll_spec (pArray
, n_fds
,
262 &read_descs
, &write_descs
, &except_descs
);
264 /* Map the poll() timeout value in the select() timeout structure. */
266 pTimeout
= map_timeout (timeout
, &stime
);
268 /* Make the select() call. */
270 ready_descriptors
= select (max_fd
+ 1, &read_descs
, &write_descs
,
271 &except_descs
, pTimeout
);
273 if (ready_descriptors
>= 0)
275 map_select_results (pArray
, n_fds
,
276 &read_descs
, &write_descs
, &except_descs
);
279 return ready_descriptors
;