poll.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <unistd.h> /* standard Unix definitions */
  57. #include <sys/types.h> /* system types */
  58. #include <sys/time.h> /* time definitions */
  59. #include <assert.h> /* assertion macros */
  60. #include <string.h> /* string functions */
  61. #include <asterisk/poll-compat.h> /* this package */
  62. /*---------------------------------------------------------------------------*\
  63. Macros
  64. \*---------------------------------------------------------------------------*/
  65. #ifndef MAX
  66. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  67. #endif
  68. /*---------------------------------------------------------------------------*\
  69. Private Functions
  70. \*---------------------------------------------------------------------------*/
  71. static int map_poll_spec
  72. #if __STDC__ > 0
  73. (struct pollfd *pArray,
  74. unsigned long n_fds,
  75. fd_set *pReadSet,
  76. fd_set *pWriteSet,
  77. fd_set *pExceptSet)
  78. #else
  79. (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
  80. struct pollfd *pArray;
  81. unsigned long n_fds;
  82. fd_set *pReadSet;
  83. fd_set *pWriteSet;
  84. fd_set *pExceptSet;
  85. #endif
  86. {
  87. register unsigned long i; /* loop control */
  88. register struct pollfd *pCur; /* current array element */
  89. register int max_fd = -1; /* return value */
  90. /*
  91. Map the poll() structures into the file descriptor sets required
  92. by select().
  93. */
  94. for (i = 0, pCur = pArray; i < n_fds; i++, pCur++)
  95. {
  96. /* Skip any bad FDs in the array. */
  97. if (pCur->fd < 0)
  98. continue;
  99. if (pCur->events & POLLIN)
  100. {
  101. /* "Input Ready" notification desired. */
  102. FD_SET (pCur->fd, pReadSet);
  103. }
  104. if (pCur->events & POLLOUT)
  105. {
  106. /* "Output Possible" notification desired. */
  107. FD_SET (pCur->fd, pWriteSet);
  108. }
  109. if (pCur->events & POLLPRI)
  110. {
  111. /*
  112. "Exception Occurred" notification desired. (Exceptions
  113. include out of band data.
  114. */
  115. FD_SET (pCur->fd, pExceptSet);
  116. }
  117. max_fd = MAX (max_fd, pCur->fd);
  118. }
  119. return max_fd;
  120. }
  121. static struct timeval *map_timeout
  122. #if __STDC__ > 0
  123. (int poll_timeout, struct timeval *pSelTimeout)
  124. #else
  125. (poll_timeout, pSelTimeout)
  126. int poll_timeout;
  127. struct timeval *pSelTimeout;
  128. #endif
  129. {
  130. struct timeval *pResult;
  131. /*
  132. Map the poll() timeout value into a select() timeout. The possible
  133. values of the poll() timeout value, and their meanings, are:
  134. VALUE MEANING
  135. -1 wait indefinitely (until signal occurs)
  136. 0 return immediately, don't block
  137. >0 wait specified number of milliseconds
  138. select() uses a "struct timeval", which specifies the timeout in
  139. seconds and microseconds, so the milliseconds value has to be mapped
  140. accordingly.
  141. */
  142. assert (pSelTimeout != (struct timeval *) NULL);
  143. switch (poll_timeout)
  144. {
  145. case -1:
  146. /*
  147. A NULL timeout structure tells select() to wait indefinitely.
  148. */
  149. pResult = (struct timeval *) NULL;
  150. break;
  151. case 0:
  152. /*
  153. "Return immediately" (test) is specified by all zeros in
  154. a timeval structure.
  155. */
  156. pSelTimeout->tv_sec = 0;
  157. pSelTimeout->tv_usec = 0;
  158. pResult = pSelTimeout;
  159. break;
  160. default:
  161. /* Wait the specified number of milliseconds. */
  162. pSelTimeout->tv_sec = poll_timeout / 1000; /* get seconds */
  163. poll_timeout %= 1000; /* remove seconds */
  164. pSelTimeout->tv_usec = poll_timeout * 1000; /* get microseconds */
  165. pResult = pSelTimeout;
  166. break;
  167. }
  168. return pResult;
  169. }
  170. static void map_select_results
  171. #if __STDC__ > 0
  172. (struct pollfd *pArray,
  173. unsigned long n_fds,
  174. fd_set *pReadSet,
  175. fd_set *pWriteSet,
  176. fd_set *pExceptSet)
  177. #else
  178. (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
  179. struct pollfd *pArray;
  180. unsigned long n_fds;
  181. fd_set *pReadSet;
  182. fd_set *pWriteSet;
  183. fd_set *pExceptSet;
  184. #endif
  185. {
  186. register unsigned long i; /* loop control */
  187. register struct pollfd *pCur; /* current array element */
  188. for (i = 0, pCur = pArray; i < n_fds; i++, pCur++)
  189. {
  190. /* Skip any bad FDs in the array. */
  191. if (pCur->fd < 0)
  192. continue;
  193. /* Exception events take priority over input events. */
  194. pCur->revents = 0;
  195. if (FD_ISSET (pCur->fd, pExceptSet))
  196. pCur->revents |= POLLPRI;
  197. else if (FD_ISSET (pCur->fd, pReadSet))
  198. pCur->revents |= POLLIN;
  199. if (FD_ISSET (pCur->fd, pWriteSet))
  200. pCur->revents |= POLLOUT;
  201. }
  202. return;
  203. }
  204. /*---------------------------------------------------------------------------*\
  205. Public Functions
  206. \*---------------------------------------------------------------------------*/
  207. int poll
  208. #if __STDC__ > 0
  209. (struct pollfd *pArray, unsigned long n_fds, int timeout)
  210. #else
  211. (pArray, n_fds, timeout)
  212. struct pollfd *pArray;
  213. unsigned long n_fds;
  214. int timeout;
  215. #endif
  216. {
  217. fd_set read_descs; /* input file descs */
  218. fd_set write_descs; /* output file descs */
  219. fd_set except_descs; /* exception descs */
  220. struct timeval stime; /* select() timeout value */
  221. int ready_descriptors; /* function result */
  222. int max_fd; /* maximum fd value */
  223. struct timeval *pTimeout; /* actually passed */
  224. FD_ZERO (&read_descs);
  225. FD_ZERO (&write_descs);
  226. FD_ZERO (&except_descs);
  227. assert (pArray != (struct pollfd *) NULL);
  228. /* Map the poll() file descriptor list in the select() data structures. */
  229. max_fd = map_poll_spec (pArray, n_fds,
  230. &read_descs, &write_descs, &except_descs);
  231. /* Map the poll() timeout value in the select() timeout structure. */
  232. pTimeout = map_timeout (timeout, &stime);
  233. /* Make the select() call. */
  234. ready_descriptors = select (max_fd + 1, &read_descs, &write_descs,
  235. &except_descs, pTimeout);
  236. if (ready_descriptors >= 0)
  237. {
  238. map_select_results (pArray, n_fds,
  239. &read_descs, &write_descs, &except_descs);
  240. }
  241. return ready_descriptors;
  242. }