poll.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*---------------------------------------------------------------------------*\
  2. $Id$
  3. NAME
  4. poll - select(2)-based poll() emulation function for BSD systems.
  5. SYNOPSIS
  6. #include "poll.h"
  7. struct pollfd
  8. {
  9. int fd;
  10. short events;
  11. short revents;
  12. }
  13. int poll (struct pollfd *pArray, unsigned long n_fds, int timeout)
  14. DESCRIPTION
  15. This file, and the accompanying "poll.h", implement the System V
  16. poll(2) system call for BSD systems (which typically do not provide
  17. poll()). Poll() provides a method for multiplexing input and output
  18. on multiple open file descriptors; in traditional BSD systems, that
  19. capability is provided by select(). While the semantics of select()
  20. differ from those of poll(), poll() can be readily emulated in terms
  21. of select() -- which is how this function is implemented.
  22. REFERENCES
  23. Stevens, W. Richard. Unix Network Programming. Prentice-Hall, 1990.
  24. NOTES
  25. 1. This software requires an ANSI C compiler.
  26. LICENSE
  27. This software is released under the following license:
  28. Copyright (c) 1995-2002 Brian M. Clapper
  29. All rights reserved.
  30. Redistribution and use in source and binary forms are
  31. permitted provided that: (1) source distributions retain
  32. this entire copyright notice and comment; (2) modifications
  33. made to the software are prominently mentioned, and a copy
  34. of the original software (or a pointer to its location) are
  35. included; and (3) distributions including binaries display
  36. the following acknowledgement: "This product includes
  37. software developed by Brian M. Clapper <bmc@clapper.org>"
  38. in the documentation or other materials provided with the
  39. distribution. The name of the author may not be used to
  40. endorse or promote products derived from this software
  41. without specific prior written permission.
  42. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS
  43. OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE
  44. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  45. PARTICULAR PURPOSE.
  46. Effectively, this means you can do what you want with the software
  47. except remove this notice or take advantage of the author's name.
  48. If you modify the software and redistribute your modified version,
  49. you must indicate that your version is a modification of the
  50. original, and you must provide either a pointer to or a copy of the
  51. original.
  52. \*---------------------------------------------------------------------------*/
  53. /*---------------------------------------------------------------------------*\
  54. Includes
  55. \*---------------------------------------------------------------------------*/
  56. #include "asterisk.h"
  57. #include <unistd.h> /* standard Unix definitions */
  58. #include <sys/types.h> /* system types */
  59. #include <sys/time.h> /* time definitions */
  60. #include <assert.h> /* assertion macros */
  61. #include <string.h> /* string functions */
  62. #include "asterisk/utils.h" /* this package */
  63. #include "asterisk/poll-compat.h" /* this package */
  64. #ifdef AST_POLL_COMPAT
  65. /*---------------------------------------------------------------------------*\
  66. Private Functions
  67. \*---------------------------------------------------------------------------*/
  68. static int map_poll_spec
  69. #if __STDC__ > 0
  70. (struct pollfd *pArray,
  71. unsigned long n_fds,
  72. fd_set *pReadSet,
  73. fd_set *pWriteSet,
  74. fd_set *pExceptSet)
  75. #else
  76. (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
  77. struct pollfd *pArray;
  78. unsigned long n_fds;
  79. fd_set *pReadSet;
  80. fd_set *pWriteSet;
  81. fd_set *pExceptSet;
  82. #endif
  83. {
  84. register unsigned long i; /* loop control */
  85. register struct pollfd *pCur; /* current array element */
  86. register int max_fd = -1; /* return value */
  87. /*!\note
  88. * Map the poll() structures into the file descriptor sets required
  89. * by select().
  90. */
  91. for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
  92. /* Skip any bad FDs in the array. */
  93. if (pCur->fd < 0)
  94. continue;
  95. if (pCur->events & POLLIN) {
  96. /* "Input Ready" notification desired. */
  97. FD_SET(pCur->fd, pReadSet);
  98. }
  99. if (pCur->events & POLLOUT) {
  100. /* "Output Possible" notification desired. */
  101. FD_SET(pCur->fd, pWriteSet);
  102. }
  103. if (pCur->events & POLLPRI) {
  104. /*!\note
  105. * "Exception Occurred" notification desired. (Exceptions
  106. * include out of band data.)
  107. */
  108. FD_SET(pCur->fd, pExceptSet);
  109. }
  110. max_fd = MAX(max_fd, pCur->fd);
  111. }
  112. return max_fd;
  113. }
  114. static struct timeval *map_timeout
  115. #if __STDC__ > 0
  116. (int poll_timeout, struct timeval *pSelTimeout)
  117. #else
  118. (poll_timeout, pSelTimeout)
  119. int poll_timeout;
  120. struct timeval *pSelTimeout;
  121. #endif
  122. {
  123. struct timeval *pResult;
  124. /*!\note
  125. * Map the poll() timeout value into a select() timeout. The possible
  126. * values of the poll() timeout value, and their meanings, are:
  127. *
  128. * VALUE MEANING
  129. *
  130. * -1 wait indefinitely (until signal occurs)
  131. * 0 return immediately, don't block
  132. * >0 wait specified number of milliseconds
  133. *
  134. * select() uses a "struct timeval", which specifies the timeout in
  135. * seconds and microseconds, so the milliseconds value has to be mapped
  136. * accordingly.
  137. */
  138. assert(pSelTimeout != (struct timeval *) NULL);
  139. switch (poll_timeout) {
  140. case -1:
  141. /*
  142. * A NULL timeout structure tells select() to wait indefinitely.
  143. */
  144. pResult = (struct timeval *) NULL;
  145. break;
  146. case 0:
  147. /*
  148. * "Return immediately" (test) is specified by all zeros in
  149. * a timeval structure.
  150. */
  151. pSelTimeout->tv_sec = 0;
  152. pSelTimeout->tv_usec = 0;
  153. pResult = pSelTimeout;
  154. break;
  155. default:
  156. /* Wait the specified number of milliseconds. */
  157. pSelTimeout->tv_sec = poll_timeout / 1000; /* get seconds */
  158. poll_timeout %= 1000; /* remove seconds */
  159. pSelTimeout->tv_usec = poll_timeout * 1000; /* get microseconds */
  160. pResult = pSelTimeout;
  161. break;
  162. }
  163. return pResult;
  164. }
  165. static void map_select_results
  166. #if __STDC__ > 0
  167. (struct pollfd *pArray,
  168. unsigned long n_fds,
  169. fd_set *pReadSet,
  170. fd_set *pWriteSet,
  171. fd_set *pExceptSet)
  172. #else
  173. (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
  174. struct pollfd *pArray;
  175. unsigned long n_fds;
  176. fd_set *pReadSet;
  177. fd_set *pWriteSet;
  178. fd_set *pExceptSet;
  179. #endif
  180. {
  181. register unsigned long i; /* loop control */
  182. register struct pollfd *pCur; /* current array element */
  183. for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
  184. /* Skip any bad FDs in the array. */
  185. if (pCur->fd < 0) {
  186. continue;
  187. }
  188. /* Exception events take priority over input events. */
  189. pCur->revents = 0;
  190. if (FD_ISSET (pCur->fd, pExceptSet)) {
  191. pCur->revents |= POLLPRI;
  192. } else if (FD_ISSET (pCur->fd, pReadSet)) {
  193. pCur->revents |= POLLIN;
  194. }
  195. if (FD_ISSET (pCur->fd, pWriteSet)) {
  196. pCur->revents |= POLLOUT;
  197. }
  198. }
  199. return;
  200. }
  201. /*---------------------------------------------------------------------------*\
  202. Public Functions
  203. \*---------------------------------------------------------------------------*/
  204. int ast_internal_poll(struct pollfd *pArray, unsigned long n_fds, int timeout)
  205. {
  206. fd_set read_descs; /* input file descs */
  207. fd_set write_descs; /* output file descs */
  208. fd_set except_descs; /* exception descs */
  209. struct timeval stime; /* select() timeout value */
  210. int ready_descriptors; /* function result */
  211. int max_fd = 0; /* maximum fd value */
  212. struct timeval *pTimeout; /* actually passed */
  213. FD_ZERO (&read_descs);
  214. FD_ZERO (&write_descs);
  215. FD_ZERO (&except_descs);
  216. /* Map the poll() file descriptor list in the select() data structures. */
  217. if (pArray) {
  218. max_fd = map_poll_spec (pArray, n_fds,
  219. &read_descs, &write_descs, &except_descs);
  220. }
  221. /* Map the poll() timeout value in the select() timeout structure. */
  222. pTimeout = map_timeout(timeout, &stime);
  223. /* Make the select() call. */
  224. ready_descriptors = select(max_fd + 1, &read_descs, &write_descs,
  225. &except_descs, pTimeout);
  226. if (ready_descriptors >= 0) {
  227. map_select_results(pArray, n_fds, &read_descs, &write_descs, &except_descs);
  228. }
  229. return ready_descriptors;
  230. }
  231. #endif /* AST_POLL_COMPAT */