select.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * select() for uClibc
  4. *
  5. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  6. *
  7. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  8. */
  9. #include <sys/syscall.h>
  10. #include <sys/select.h>
  11. #include <stdint.h>
  12. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  13. #include <sysdep-cancel.h>
  14. #else
  15. #define SINGLE_THREAD_P 1
  16. #endif
  17. #define USEC_PER_SEC 1000000L
  18. extern __typeof(select) __libc_select;
  19. #if !defined(__NR__newselect) && !defined(__NR_select) && defined __USE_XOPEN2K
  20. # define __NR___libc_pselect6 __NR_pselect6
  21. static _syscall6(int, __libc_pselect6, int, n, fd_set *, readfds, fd_set *, writefds,
  22. fd_set *, exceptfds, const struct timespec *, timeout,
  23. const sigset_t *, sigmask)
  24. int __libc_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  25. struct timeval *timeout)
  26. {
  27. struct timespec _ts, *ts = 0;
  28. if (timeout) {
  29. uint32_t usec;
  30. _ts.tv_sec = timeout->tv_sec;
  31. /* GNU extension: allow for timespec values where the sub-sec
  32. * field is equal to or more than 1 second. The kernel will
  33. * reject this on us, so take care of the time shift ourself.
  34. * Some applications (like readline and linphone) do this.
  35. * See 'clarification on select() type calls and invalid timeouts'
  36. * on the POSIX general list for more information.
  37. */
  38. usec = timeout->tv_usec;
  39. if (usec >= USEC_PER_SEC) {
  40. _ts.tv_sec += usec / USEC_PER_SEC;
  41. usec %= USEC_PER_SEC;
  42. }
  43. _ts.tv_nsec = usec * 1000;
  44. ts = &_ts;
  45. }
  46. if (SINGLE_THREAD_P)
  47. return __libc_pselect6(n, readfds, writefds, exceptfds, ts, 0);
  48. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  49. int oldtype = LIBC_CANCEL_ASYNC ();
  50. int result = __libc_pselect6(n, readfds, writefds, exceptfds, ts, 0);
  51. LIBC_CANCEL_RESET (oldtype);
  52. return result;
  53. #endif
  54. }
  55. #else
  56. #ifdef __NR__newselect
  57. # define __NR___syscall_select __NR__newselect
  58. #else
  59. # define __NR___syscall_select __NR_select
  60. #endif
  61. static _syscall5(int, __syscall_select, int, n, fd_set *, readfds,
  62. fd_set *, writefds, fd_set *, exceptfds, struct timeval *, timeout);
  63. int __libc_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  64. struct timeval *timeout)
  65. {
  66. if (SINGLE_THREAD_P)
  67. return __syscall_select(n, readfds, writefds, exceptfds, timeout);
  68. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  69. int oldtype = LIBC_CANCEL_ASYNC ();
  70. int result = __syscall_select(n, readfds, writefds, exceptfds, timeout);
  71. LIBC_CANCEL_RESET (oldtype);
  72. return result;
  73. #endif
  74. }
  75. #endif
  76. weak_alias(__libc_select,select)
  77. libc_hidden_weak(select)