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 \*---------------------------------------------------------------------------*/
74 #include <unistd.h> /* standard Unix definitions */
75 #include <sys/types.h> /* system types */
76 #include <sys/time.h> /* time definitions */
77 #include <assert.h> /* assertion macros */
78 #include <string.h> /* string functions */
80 #include "asterisk/poll-compat.h" /* this package */
82 /*---------------------------------------------------------------------------*\
84 \*---------------------------------------------------------------------------*/
87 #define MAX(a,b) ((a) > (b) ? (a) : (b))
91 /*---------------------------------------------------------------------------*\
93 \*---------------------------------------------------------------------------*/
95 static int map_poll_spec
97 (struct pollfd
*pArray
,
103 (pArray
, n_fds
, pReadSet
, pWriteSet
, pExceptSet
)
104 struct pollfd
*pArray
;
111 register unsigned long i
; /* loop control */
112 register struct pollfd
*pCur
; /* current array element */
113 register int max_fd
= -1; /* return value */
116 Map the poll() structures into the file descriptor sets required
119 for (i
= 0, pCur
= pArray
; i
< n_fds
; i
++, pCur
++)
121 /* Skip any bad FDs in the array. */
126 if (pCur
->events
& POLLIN
)
128 /* "Input Ready" notification desired. */
129 FD_SET (pCur
->fd
, pReadSet
);
132 if (pCur
->events
& POLLOUT
)
134 /* "Output Possible" notification desired. */
135 FD_SET (pCur
->fd
, pWriteSet
);
138 if (pCur
->events
& POLLPRI
)
141 "Exception Occurred" notification desired. (Exceptions
142 include out of band data.
144 FD_SET (pCur
->fd
, pExceptSet
);
147 max_fd
= MAX (max_fd
, pCur
->fd
);
153 static struct timeval
*map_timeout
155 (int poll_timeout
, struct timeval
*pSelTimeout
)
157 (poll_timeout
, pSelTimeout
)
159 struct timeval
*pSelTimeout
;
162 struct timeval
*pResult
;
165 Map the poll() timeout value into a select() timeout. The possible
166 values of the poll() timeout value, and their meanings, are:
170 -1 wait indefinitely (until signal occurs)
171 0 return immediately, don't block
172 >0 wait specified number of milliseconds
174 select() uses a "struct timeval", which specifies the timeout in
175 seconds and microseconds, so the milliseconds value has to be mapped
179 assert (pSelTimeout
!= (struct timeval
*) NULL
);
181 switch (poll_timeout
)
185 A NULL timeout structure tells select() to wait indefinitely.
187 pResult
= (struct timeval
*) NULL
;
192 "Return immediately" (test) is specified by all zeros in
195 pSelTimeout
->tv_sec
= 0;
196 pSelTimeout
->tv_usec
= 0;
197 pResult
= pSelTimeout
;
201 /* Wait the specified number of milliseconds. */
202 pSelTimeout
->tv_sec
= poll_timeout
/ 1000; /* get seconds */
203 poll_timeout
%= 1000; /* remove seconds */
204 pSelTimeout
->tv_usec
= poll_timeout
* 1000; /* get microseconds */
205 pResult
= pSelTimeout
;
213 static void map_select_results
215 (struct pollfd
*pArray
,
221 (pArray
, n_fds
, pReadSet
, pWriteSet
, pExceptSet
)
222 struct pollfd
*pArray
;
229 register unsigned long i
; /* loop control */
230 register struct pollfd
*pCur
; /* current array element */
232 for (i
= 0, pCur
= pArray
; i
< n_fds
; i
++, pCur
++)
234 /* Skip any bad FDs in the array. */
239 /* Exception events take priority over input events. */
242 if (FD_ISSET (pCur
->fd
, pExceptSet
))
243 pCur
->revents
|= POLLPRI
;
245 else if (FD_ISSET (pCur
->fd
, pReadSet
))
246 pCur
->revents
|= POLLIN
;
248 if (FD_ISSET (pCur
->fd
, pWriteSet
))
249 pCur
->revents
|= POLLOUT
;
255 /*---------------------------------------------------------------------------*\
257 \*---------------------------------------------------------------------------*/
262 (struct pollfd
*pArray
, unsigned long n_fds
, int timeout
)
264 (pArray
, n_fds
, timeout
)
265 struct pollfd
*pArray
;
271 fd_set read_descs
; /* input file descs */
272 fd_set write_descs
; /* output file descs */
273 fd_set except_descs
; /* exception descs */
274 struct timeval stime
; /* select() timeout value */
275 int ready_descriptors
; /* function result */
276 int max_fd
= 0; /* maximum fd value */
277 struct timeval
*pTimeout
; /* actually passed */
279 FD_ZERO (&read_descs
);
280 FD_ZERO (&write_descs
);
281 FD_ZERO (&except_descs
);
283 /* Map the poll() file descriptor list in the select() data structures. */
286 max_fd
= map_poll_spec (pArray
, n_fds
,
287 &read_descs
, &write_descs
, &except_descs
);
290 /* Map the poll() timeout value in the select() timeout structure. */
292 pTimeout
= map_timeout (timeout
, &stime
);
294 /* Make the select() call. */
296 ready_descriptors
= select (max_fd
+ 1, &read_descs
, &write_descs
,
297 &except_descs
, pTimeout
);
299 if (ready_descriptors
>= 0)
301 map_select_results (pArray
, n_fds
,
302 &read_descs
, &write_descs
, &except_descs
);
305 return ready_descriptors
;