pselect.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright (C) 1996-2019 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 <errno.h>
  16. #include <signal.h>
  17. #include <stddef.h> /* For NULL. */
  18. #include <sys/time.h>
  19. #include <sys/select.h>
  20. #include <sysdep-cancel.h>
  21. /* Check the first NFDS descriptors each in READFDS (if not NULL) for read
  22. readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS
  23. (if not NULL) for exceptional conditions. If TIMEOUT is not NULL, time out
  24. after waiting the interval specified therein. Additionally set the sigmask
  25. SIGMASK for this call. Returns the number of ready descriptors, or -1 for
  26. errors. */
  27. int
  28. __pselect (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  29. const struct timespec *timeout, const sigset_t *sigmask)
  30. {
  31. struct timeval tval;
  32. int retval;
  33. sigset_t savemask;
  34. /* Change nanosecond number to microseconds. This might mean losing
  35. precision and therefore the `pselect` should be available. But
  36. for now it is hardly found. */
  37. if (timeout != NULL)
  38. {
  39. /* Catch bugs which would be hidden by the TIMESPEC_TO_TIMEVAL
  40. computations. The division by 1000 truncates values. */
  41. if (__glibc_unlikely (timeout->tv_nsec < 0))
  42. {
  43. __set_errno (EINVAL);
  44. return -1;
  45. }
  46. TIMESPEC_TO_TIMEVAL (&tval, timeout);
  47. }
  48. /* The setting and restoring of the signal mask and the select call
  49. should be an atomic operation. This can't be done without kernel
  50. help. */
  51. if (sigmask != NULL)
  52. __sigprocmask (SIG_SETMASK, sigmask, &savemask);
  53. /* Note the pselect() is a cancellation point. But since we call
  54. select() which itself is a cancellation point we do not have
  55. to do anything here. */
  56. retval = __select (nfds, readfds, writefds, exceptfds,
  57. timeout != NULL ? &tval : NULL);
  58. if (sigmask != NULL)
  59. __sigprocmask (SIG_SETMASK, &savemask, NULL);
  60. return retval;
  61. }
  62. #ifndef __pselect
  63. weak_alias (__pselect, pselect)
  64. #endif