pselect.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright (C) 1996-1998,2001,2002,2003,2006 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <features.h>
  16. #ifdef __USE_XOPEN2K
  17. #include <sys/syscall.h>
  18. #include <sys/select.h>
  19. #include <sys/time.h>
  20. #include <signal.h>
  21. #include <cancel.h>
  22. static int __NC(pselect)(int nfds, fd_set *readfds, fd_set *writefds,
  23. fd_set *exceptfds, const struct timespec *timeout,
  24. const sigset_t *sigmask)
  25. {
  26. #ifdef __NR_pselect6
  27. #define NSEC_PER_SEC 1000000000L
  28. struct timespec _ts, *ts = 0;
  29. if (timeout) {
  30. /* The Linux kernel can in some situations update the timeout value.
  31. * We do not want that so use a local variable.
  32. */
  33. _ts = *timeout;
  34. /* GNU extension: allow for timespec values where the sub-sec
  35. * field is equal to or more than 1 second. The kernel will
  36. * reject this on us, so take care of the time shift ourself.
  37. * Some applications (like readline and linphone) do this.
  38. * See 'clarification on select() type calls and invalid timeouts'
  39. * on the POSIX general list for more information.
  40. */
  41. if (_ts.tv_nsec >= NSEC_PER_SEC) {
  42. _ts.tv_sec += _ts.tv_nsec / NSEC_PER_SEC;
  43. _ts.tv_nsec %= NSEC_PER_SEC;
  44. }
  45. ts = &_ts;
  46. }
  47. /* The pselect6 syscall API is strange. It wants a 7th arg to be
  48. * the sizeof(*sigmask). However syscalls with > 6 arguments aren't
  49. * supported on linux. So arguments 6 and 7 are stuffed in a struct
  50. * and a pointer to that struct is passed as the 6th argument to
  51. * the syscall.
  52. * Glibc stuffs arguments 6 and 7 in a ulong[2]. Linux reads
  53. * them as if there were a struct { sigset_t*; size_t } in
  54. * userspace. There woudl be trouble if userspace and the kernel are
  55. * compiled differently enough that size_t isn't the same as ulong,
  56. * but not enough to trigger the compat layer in linux. I can't
  57. * think of such a case, so I'm using linux's struct.
  58. * Furthermore Glibc sets the sigsetsize to _NSIG/8. However linux
  59. * checks for sizeof(sigset_t), which internally is a ulong array.
  60. * This means that if _NSIG isn't a multiple of BITS_PER_LONG then
  61. * linux will refuse glibc's value. So I prefer sizeof(sigset_t) for
  62. * the value of sigsetsize.
  63. */
  64. struct {
  65. const sigset_t *sigmask;
  66. size_t sigsetsize;
  67. } args67 = {
  68. sigmask,
  69. sizeof(sigset_t),
  70. };
  71. return INLINE_SYSCALL(pselect6, 6, nfds, readfds, writefds, exceptfds, ts, &args67);
  72. #else
  73. struct timeval tval;
  74. int retval;
  75. sigset_t savemask;
  76. /* Change nanosecond number to microseconds. This might mean losing
  77. precision and therefore the `pselect` should be available. But
  78. for now it is hardly found. */
  79. if (timeout != NULL)
  80. TIMESPEC_TO_TIMEVAL (&tval, timeout);
  81. /* The setting and restoring of the signal mask and the select call
  82. should be an atomic operation. This can't be done without kernel
  83. help. */
  84. if (sigmask != NULL)
  85. sigprocmask (SIG_SETMASK, sigmask, &savemask);
  86. /* The comment below does not apply on uClibc, since we use __select_nocancel */
  87. /* Note the pselect() is a cancellation point. But since we call
  88. select() which itself is a cancellation point we do not have
  89. to do anything here. */
  90. retval = __NC(select)(nfds, readfds, writefds, exceptfds,
  91. timeout != NULL ? &tval : NULL);
  92. if (sigmask != NULL)
  93. sigprocmask (SIG_SETMASK, &savemask, NULL);
  94. return retval;
  95. #endif
  96. }
  97. CANCELLABLE_SYSCALL(int, pselect, (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  98. const struct timespec *timeout, const sigset_t *sigmask),
  99. (nfds, readfds, writefds, exceptfds, timeout, sigmask))
  100. #endif