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 license:
40 Copyright (c) 1995-2002 Brian M. Clapper
43 Redistribution and use in source and binary forms are
44 permitted provided that: (1) source distributions retain
45 this entire copyright notice and comment; (2) modifications
46 made to the software are prominently mentioned, and a copy
47 of the original software (or a pointer to its location) are
48 included; and (3) distributions including binaries display
49 the following acknowledgement: "This product includes
50 software developed by Brian M. Clapper <bmc@clapper.org>"
51 in the documentation or other materials provided with the
52 distribution. The name of the author may not be used to
53 endorse or promote products derived from this software
54 without specific prior written permission.
56 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS
57 OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE
58 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
61 Effectively, this means you can do what you want with the software
62 except remove this notice or take advantage of the author's name.
63 If you modify the software and redistribute your modified version,
64 you must indicate that your version is a modification of the
65 original, and you must provide either a pointer to or a copy of the
67 \*---------------------------------------------------------------------------*/
70 /*---------------------------------------------------------------------------*\
72 \*---------------------------------------------------------------------------*/
76 #include <unistd.h> /* standard Unix definitions */
77 #include <sys/types.h> /* system types */
78 #include <sys/time.h> /* time definitions */
79 #include <assert.h> /* assertion macros */
80 #include <string.h> /* string functions */
82 #include "asterisk/poll-compat.h" /* this package */
84 #ifdef AST_POLL_COMPAT
86 /*---------------------------------------------------------------------------*\
88 \*---------------------------------------------------------------------------*/
90 static int map_poll_spec
92 (struct pollfd *pArray,
98 (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
99 struct pollfd *pArray;
106 register unsigned long i; /* loop control */
107 register struct pollfd *pCur; /* current array element */
108 register int max_fd = -1; /* return value */
111 * Map the poll() structures into the file descriptor sets required
114 for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
115 /* Skip any bad FDs in the array. */
120 if (pCur->events & POLLIN) {
121 /* "Input Ready" notification desired. */
122 FD_SET(pCur->fd, pReadSet);
125 if (pCur->events & POLLOUT) {
126 /* "Output Possible" notification desired. */
127 FD_SET(pCur->fd, pWriteSet);
130 if (pCur->events & POLLPRI) {
132 * "Exception Occurred" notification desired. (Exceptions
133 * include out of band data.)
135 FD_SET(pCur->fd, pExceptSet);
138 max_fd = MAX(max_fd, pCur->fd);
144 static struct timeval *map_timeout
146 (int poll_timeout, struct timeval *pSelTimeout)
148 (poll_timeout, pSelTimeout)
150 struct timeval *pSelTimeout;
153 struct timeval *pResult;
156 * Map the poll() timeout value into a select() timeout. The possible
157 * values of the poll() timeout value, and their meanings, are:
161 * -1 wait indefinitely (until signal occurs)
162 * 0 return immediately, don't block
163 * >0 wait specified number of milliseconds
165 * select() uses a "struct timeval", which specifies the timeout in
166 * seconds and microseconds, so the milliseconds value has to be mapped
170 assert(pSelTimeout != (struct timeval *) NULL);
172 switch (poll_timeout) {
175 * A NULL timeout structure tells select() to wait indefinitely.
177 pResult = (struct timeval *) NULL;
182 * "Return immediately" (test) is specified by all zeros in
183 * a timeval structure.
185 pSelTimeout->tv_sec = 0;
186 pSelTimeout->tv_usec = 0;
187 pResult = pSelTimeout;
191 /* Wait the specified number of milliseconds. */
192 pSelTimeout->tv_sec = poll_timeout / 1000; /* get seconds */
193 poll_timeout %= 1000; /* remove seconds */
194 pSelTimeout->tv_usec = poll_timeout * 1000; /* get microseconds */
195 pResult = pSelTimeout;
202 static void map_select_results
204 (struct pollfd *pArray,
210 (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
211 struct pollfd *pArray;
218 register unsigned long i; /* loop control */
219 register struct pollfd *pCur; /* current array element */
221 for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
222 /* Skip any bad FDs in the array. */
228 /* Exception events take priority over input events. */
231 if (FD_ISSET (pCur->fd, pExceptSet)) {
232 pCur->revents |= POLLPRI;
233 } else if (FD_ISSET (pCur->fd, pReadSet)) {
234 pCur->revents |= POLLIN;
237 if (FD_ISSET (pCur->fd, pWriteSet)) {
238 pCur->revents |= POLLOUT;
245 /*---------------------------------------------------------------------------*\
247 \*---------------------------------------------------------------------------*/
249 int ast_internal_poll(struct pollfd *pArray, unsigned long n_fds, int timeout)
251 fd_set read_descs; /* input file descs */
252 fd_set write_descs; /* output file descs */
253 fd_set except_descs; /* exception descs */
254 struct timeval stime; /* select() timeout value */
255 int ready_descriptors; /* function result */
256 int max_fd = 0; /* maximum fd value */
257 struct timeval *pTimeout; /* actually passed */
259 FD_ZERO (&read_descs);
260 FD_ZERO (&write_descs);
261 FD_ZERO (&except_descs);
263 /* Map the poll() file descriptor list in the select() data structures. */
266 max_fd = map_poll_spec (pArray, n_fds,
267 &read_descs, &write_descs, &except_descs);
270 /* Map the poll() timeout value in the select() timeout structure. */
271 pTimeout = map_timeout(timeout, &stime);
273 /* Make the select() call. */
274 ready_descriptors = select(max_fd + 1, &read_descs, &write_descs,
275 &except_descs, pTimeout);
277 if (ready_descriptors >= 0) {
278 map_select_results(pArray, n_fds, &read_descs, &write_descs, &except_descs);
281 return ready_descriptors;
284 #endif /* AST_POLL_COMPAT */