fakepoll.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright 2002,2003 Nick Mathewson, Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file fakepoll.c
  6. *
  7. * \brief On systems where poll() doesn't exist, fake it with select().
  8. **/
  9. #include "orconfig.h"
  10. #include "fakepoll.h"
  11. #define MAXCONNECTIONS 10000 /* XXXX copied from or.h */
  12. #define FD_SETSIZE MAXCONNECTIONS
  13. #ifdef HAVE_SYS_TYPES_H
  14. #include <sys/types.h>
  15. #endif
  16. #ifdef HAVE_UNISTD_H
  17. #include <unistd.h>
  18. #endif
  19. #ifdef HAVE_STRING_H
  20. #include <string.h>
  21. #endif
  22. #ifdef HAVE_SYS_TIME_H
  23. #include <sys/time.h>
  24. #endif
  25. #include <assert.h>
  26. #include <stdlib.h>
  27. #include "util.h"
  28. #include "log.h"
  29. #ifndef USE_FAKE_POLL
  30. int
  31. tor_poll(struct pollfd *ufds, unsigned int nfds, int timeout)
  32. {
  33. unsigned int i;
  34. for (i=0;i<nfds;++i) {
  35. tor_assert(ufds[i].fd >= 0);
  36. }
  37. return poll(ufds,nfds,timeout);
  38. }
  39. #else
  40. int
  41. tor_poll(struct pollfd *ufds, unsigned int nfds, int timeout)
  42. {
  43. unsigned int idx;
  44. int maxfd, fd;
  45. int r;
  46. #ifdef MS_WINDOWS
  47. int any_fds_set = 0;
  48. #endif
  49. fd_set readfds, writefds, exceptfds;
  50. #ifdef USING_FAKE_TIMEVAL
  51. #undef timeval
  52. #undef tv_sec
  53. #undef tv_usec
  54. #endif
  55. struct timeval _timeout;
  56. _timeout.tv_sec = timeout/1000;
  57. _timeout.tv_usec = (timeout%1000)*1000;
  58. FD_ZERO(&readfds);
  59. FD_ZERO(&writefds);
  60. FD_ZERO(&exceptfds);
  61. maxfd = -1;
  62. for (idx = 0; idx < nfds; ++idx) {
  63. ufds[idx].revents = 0;
  64. fd = ufds[idx].fd;
  65. tor_assert(fd >= 0);
  66. if (fd > maxfd) {
  67. maxfd = fd;
  68. #ifdef MS_WINDOWS
  69. any_fds_set = 1;
  70. #endif
  71. }
  72. if (ufds[idx].events & POLLIN)
  73. FD_SET(fd, &readfds);
  74. if (ufds[idx].events & POLLOUT)
  75. FD_SET(fd, &writefds);
  76. FD_SET(fd, &exceptfds);
  77. }
  78. #ifdef MS_WINDOWS
  79. if (!any_fds_set) {
  80. Sleep(timeout);
  81. return 0;
  82. }
  83. #endif
  84. r = select(maxfd+1, &readfds, &writefds, &exceptfds,
  85. timeout == -1 ? NULL : &_timeout);
  86. if (r <= 0)
  87. return r;
  88. r = 0;
  89. for (idx = 0; idx < nfds; ++idx) {
  90. fd = ufds[idx].fd;
  91. if (FD_ISSET(fd, &readfds))
  92. ufds[idx].revents |= POLLIN;
  93. if (FD_ISSET(fd, &writefds))
  94. ufds[idx].revents |= POLLOUT;
  95. if (FD_ISSET(fd, &exceptfds))
  96. ufds[idx].revents |= POLLERR;
  97. if (ufds[idx].revents)
  98. ++r;
  99. }
  100. return r;
  101. }
  102. #endif