poll.c 8.9 KB

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