pselect.c 3.2 KB

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