pselect.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* pselect - synchronous I/O multiplexing
  2. Copyright 2011-2015 Free Software Foundation, Inc.
  3. This file is part of gnulib.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, see <http://www.gnu.org/licenses/>. */
  14. /* written by Paul Eggert */
  15. #include <config.h>
  16. #include <sys/select.h>
  17. #include <errno.h>
  18. #include <signal.h>
  19. /* Examine the size-NFDS file descriptor sets in RFDS, WFDS, and XFDS
  20. to see whether some of their descriptors are ready for reading,
  21. ready for writing, or have exceptions pending. Wait for at most
  22. TIMEOUT seconds, and use signal mask SIGMASK while waiting. A null
  23. pointer parameter stands for no descriptors, an infinite timeout,
  24. or an unaffected signal mask. */
  25. #if !HAVE_PSELECT
  26. int
  27. pselect (int nfds, fd_set *restrict rfds,
  28. fd_set *restrict wfds, fd_set *restrict xfds,
  29. struct timespec const *restrict timeout,
  30. sigset_t const *restrict sigmask)
  31. {
  32. int select_result;
  33. sigset_t origmask;
  34. struct timeval tv, *tvp;
  35. if (timeout)
  36. {
  37. if (! (0 <= timeout->tv_nsec && timeout->tv_nsec < 1000000000))
  38. {
  39. errno = EINVAL;
  40. return -1;
  41. }
  42. tv.tv_sec = timeout->tv_sec;
  43. tv.tv_usec = (timeout->tv_nsec + 999) / 1000;
  44. tvp = &tv;
  45. }
  46. else
  47. tvp = NULL;
  48. /* Signal mask munging should be atomic, but this is the best we can
  49. do in this emulation. */
  50. if (sigmask)
  51. pthread_sigmask (SIG_SETMASK, sigmask, &origmask);
  52. select_result = select (nfds, rfds, wfds, xfds, tvp);
  53. if (sigmask)
  54. {
  55. int select_errno = errno;
  56. pthread_sigmask (SIG_SETMASK, &origmask, NULL);
  57. errno = select_errno;
  58. }
  59. return select_result;
  60. }
  61. #else /* HAVE_PSELECT */
  62. # include <unistd.h>
  63. # undef pselect
  64. int
  65. rpl_pselect (int nfds, fd_set *restrict rfds,
  66. fd_set *restrict wfds, fd_set *restrict xfds,
  67. struct timespec const *restrict timeout,
  68. sigset_t const *restrict sigmask)
  69. {
  70. int i;
  71. /* FreeBSD 8.2 has a bug: it does not always detect invalid fds. */
  72. if (nfds < 0 || nfds > FD_SETSIZE)
  73. {
  74. errno = EINVAL;
  75. return -1;
  76. }
  77. for (i = 0; i < nfds; i++)
  78. {
  79. if (((rfds && FD_ISSET (i, rfds))
  80. || (wfds && FD_ISSET (i, wfds))
  81. || (xfds && FD_ISSET (i, xfds)))
  82. && dup2 (i, i) != i)
  83. return -1;
  84. }
  85. return pselect (nfds, rfds, wfds, xfds, timeout, sigmask);
  86. }
  87. #endif