select.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Linux select implementation.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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 <sys/time.h>
  16. #include <sys/types.h>
  17. #include <sys/select.h>
  18. #include <errno.h>
  19. #include <sysdep-cancel.h>
  20. /* Check the first NFDS descriptors each in READFDS (if not NULL) for read
  21. readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS
  22. (if not NULL) for exceptional conditions. If TIMEOUT is not NULL, time out
  23. after waiting the interval specified therein. Returns the number of ready
  24. descriptors, or -1 for errors. */
  25. #ifdef __NR__newselect
  26. # undef __NR_select
  27. # define __NR_select __NR__newselect
  28. #endif
  29. int
  30. __select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  31. struct timeval *timeout)
  32. {
  33. #ifdef __NR_select
  34. return SYSCALL_CANCEL (select, nfds, readfds, writefds, exceptfds,
  35. timeout);
  36. #else
  37. int result;
  38. struct timespec ts, *tsp = NULL;
  39. if (timeout)
  40. {
  41. TIMEVAL_TO_TIMESPEC (timeout, &ts);
  42. tsp = &ts;
  43. }
  44. result = SYSCALL_CANCEL (pselect6, nfds, readfds, writefds, exceptfds, tsp,
  45. NULL);
  46. if (timeout)
  47. {
  48. /* Linux by default will update the timeout after a pselect6 syscall
  49. (though the pselect() glibc call suppresses this behavior).
  50. Since select() on Linux has the same behavior as the pselect6
  51. syscall, we update the timeout here. */
  52. TIMESPEC_TO_TIMEVAL (timeout, &ts);
  53. }
  54. return result;
  55. #endif
  56. }
  57. libc_hidden_def (__select)
  58. weak_alias (__select, select)
  59. weak_alias (__select, __libc_select)