bsd-poll.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2004, 2005, 2007 Darren Tucker (dtucker at zip com au).
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "includes.h"
  17. #if !defined(HAVE_POLL)
  18. #include <sys/types.h>
  19. #include <sys/time.h>
  20. #ifdef HAVE_SYS_SELECT_H
  21. # include <sys/select.h>
  22. #endif
  23. #include <errno.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include "bsd-poll.h"
  27. /*
  28. * A minimal implementation of poll(2), built on top of select(2).
  29. *
  30. * Only supports POLLIN and POLLOUT flags in pfd.events, and POLLIN, POLLOUT
  31. * and POLLERR flags in revents.
  32. *
  33. * Supports pfd.fd = -1 meaning "unused" although it's not standard.
  34. */
  35. int
  36. poll(struct pollfd *fds, nfds_t nfds, int timeout)
  37. {
  38. nfds_t i;
  39. int saved_errno, ret, fd, maxfd = 0;
  40. fd_set *readfds = NULL, *writefds = NULL, *exceptfds = NULL;
  41. size_t nmemb;
  42. struct timeval tv, *tvp = NULL;
  43. for (i = 0; i < nfds; i++) {
  44. fd = fds[i].fd;
  45. if (fd >= FD_SETSIZE) {
  46. errno = EINVAL;
  47. return -1;
  48. }
  49. maxfd = MAX(maxfd, fd);
  50. }
  51. nmemb = howmany(maxfd + 1 , NFDBITS);
  52. if ((readfds = calloc(nmemb, sizeof(fd_mask))) == NULL ||
  53. (writefds = calloc(nmemb, sizeof(fd_mask))) == NULL ||
  54. (exceptfds = calloc(nmemb, sizeof(fd_mask))) == NULL) {
  55. saved_errno = ENOMEM;
  56. ret = -1;
  57. goto out;
  58. }
  59. /* populate event bit vectors for the events we're interested in */
  60. for (i = 0; i < nfds; i++) {
  61. fd = fds[i].fd;
  62. if (fd == -1)
  63. continue;
  64. if (fds[i].events & POLLIN) {
  65. FD_SET(fd, readfds);
  66. FD_SET(fd, exceptfds);
  67. }
  68. if (fds[i].events & POLLOUT) {
  69. FD_SET(fd, writefds);
  70. FD_SET(fd, exceptfds);
  71. }
  72. }
  73. /* poll timeout is msec, select is timeval (sec + usec) */
  74. if (timeout >= 0) {
  75. tv.tv_sec = timeout / 1000;
  76. tv.tv_usec = (timeout % 1000) * 1000;
  77. tvp = &tv;
  78. }
  79. ret = select(maxfd + 1, readfds, writefds, exceptfds, tvp);
  80. saved_errno = errno;
  81. /* scan through select results and set poll() flags */
  82. for (i = 0; i < nfds; i++) {
  83. fd = fds[i].fd;
  84. fds[i].revents = 0;
  85. if (fd == -1)
  86. continue;
  87. if (FD_ISSET(fd, readfds)) {
  88. fds[i].revents |= POLLIN;
  89. }
  90. if (FD_ISSET(fd, writefds)) {
  91. fds[i].revents |= POLLOUT;
  92. }
  93. if (FD_ISSET(fd, exceptfds)) {
  94. fds[i].revents |= POLLERR;
  95. }
  96. }
  97. out:
  98. free(readfds);
  99. free(writefds);
  100. free(exceptfds);
  101. if (ret == -1)
  102. errno = saved_errno;
  103. return ret;
  104. }
  105. #endif