pselect.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. return INLINE_SYSCALL(pselect6, 6, nfds, readfds, writefds, exceptfds, timeout, sigmask);
  49. #else
  50. struct timeval tval;
  51. int retval;
  52. sigset_t savemask;
  53. /* Change nanosecond number to microseconds. This might mean losing
  54. precision and therefore the `pselect` should be available. But
  55. for now it is hardly found. */
  56. if (timeout != NULL)
  57. TIMESPEC_TO_TIMEVAL (&tval, timeout);
  58. /* The setting and restoring of the signal mask and the select call
  59. should be an atomic operation. This can't be done without kernel
  60. help. */
  61. if (sigmask != NULL)
  62. sigprocmask (SIG_SETMASK, sigmask, &savemask);
  63. /* The comment below does not apply on uClibc, since we use __select_nocancel */
  64. /* Note the pselect() is a cancellation point. But since we call
  65. select() which itself is a cancellation point we do not have
  66. to do anything here. */
  67. retval = __NC(select)(nfds, readfds, writefds, exceptfds,
  68. timeout != NULL ? &tval : NULL);
  69. if (sigmask != NULL)
  70. sigprocmask (SIG_SETMASK, &savemask, NULL);
  71. return retval;
  72. #endif
  73. }
  74. CANCELLABLE_SYSCALL(int, pselect, (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  75. const struct timespec *timeout, const sigset_t *sigmask),
  76. (nfds, readfds, writefds, exceptfds, timeout, sigmask))
  77. #endif