pselect.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #if defined(__UCLIBC_USE_TIME64__)
  23. #include "internal/time64_helpers.h"
  24. #endif
  25. static int __NC(pselect)(int nfds, fd_set *readfds, fd_set *writefds,
  26. fd_set *exceptfds, const struct timespec *timeout,
  27. const sigset_t *sigmask)
  28. {
  29. #ifdef __NR_pselect6
  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. struct timespec _ts;
  34. /* Note: the system call expects 7 values but on most architectures
  35. we can only pass in 6 directly. If there is an architecture with
  36. support for more parameters a new version of this file needs to
  37. be created. */
  38. struct {
  39. __kernel_ulong_t ss;
  40. __kernel_size_t ss_len;
  41. } data;
  42. if (timeout != NULL) {
  43. _ts = *timeout;
  44. timeout = &_ts;
  45. }
  46. if (sigmask != NULL) {
  47. data.ss = (__kernel_ulong_t) sigmask;
  48. data.ss_len = __SYSCALL_SIGSET_T_SIZE;
  49. sigmask = (void *)&data;
  50. }
  51. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_pselect6_time64)
  52. return INLINE_SYSCALL(pselect6_time64, 6, nfds, readfds, writefds, exceptfds, TO_TS64_P(timeout), sigmask);
  53. #else
  54. return INLINE_SYSCALL(pselect6, 6, nfds, readfds, writefds, exceptfds, timeout, sigmask);
  55. #endif
  56. #else
  57. struct timeval tval;
  58. int retval;
  59. sigset_t savemask;
  60. /* Change nanosecond number to microseconds. This might mean losing
  61. precision and therefore the `pselect` should be available. But
  62. for now it is hardly found. */
  63. if (timeout != NULL)
  64. TIMESPEC_TO_TIMEVAL (&tval, timeout);
  65. /* The setting and restoring of the signal mask and the select call
  66. should be an atomic operation. This can't be done without kernel
  67. help. */
  68. if (sigmask != NULL)
  69. sigprocmask (SIG_SETMASK, sigmask, &savemask);
  70. /* The comment below does not apply on uClibc, since we use __select_nocancel */
  71. /* Note the pselect() is a cancellation point. But since we call
  72. select() which itself is a cancellation point we do not have
  73. to do anything here. */
  74. retval = __NC(select)(nfds, readfds, writefds, exceptfds,
  75. timeout != NULL ? &tval : NULL);
  76. if (sigmask != NULL)
  77. sigprocmask (SIG_SETMASK, &savemask, NULL);
  78. return retval;
  79. #endif
  80. }
  81. CANCELLABLE_SYSCALL(int, pselect, (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  82. const struct timespec *timeout, const sigset_t *sigmask),
  83. (nfds, readfds, writefds, exceptfds, timeout, sigmask))
  84. #endif