select.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. extern __typeof(select) __libc_select;
  13. #define USEC_PER_SEC 1000000L
  14. #if !defined(__NR__newselect) && !defined(__NR_select) && defined __USE_XOPEN2K
  15. # define __NR___libc_pselect6 __NR_pselect6
  16. _syscall6(int, __libc_pselect6, int, n, fd_set *, readfds, fd_set *, writefds,
  17. fd_set *, exceptfds, const struct timespec *, timeout,
  18. const sigset_t *, sigmask)
  19. int __libc_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  20. struct timeval *timeout)
  21. {
  22. struct timespec _ts, *ts = 0;
  23. if (timeout) {
  24. uint32_t usec;
  25. _ts.tv_sec = timeout->tv_sec;
  26. /* GNU extension: allow for timespec values where the sub-sec
  27. * field is equal to or more than 1 second. The kernel will
  28. * reject this on us, so take care of the time shift ourself.
  29. * Some applications (like readline and linphone) do this.
  30. * See 'clarification on select() type calls and invalid timeouts'
  31. * on the POSIX general list for more information.
  32. */
  33. usec = timeout->tv_usec;
  34. if (usec >= USEC_PER_SEC) {
  35. _ts.tv_sec += usec / USEC_PER_SEC;
  36. usec %= USEC_PER_SEC;
  37. }
  38. _ts.tv_nsec = usec * 1000;
  39. ts = &_ts;
  40. }
  41. return __libc_pselect6(n, readfds, writefds, exceptfds, ts, 0);
  42. }
  43. #else
  44. #ifdef __NR__newselect
  45. # define __NR___libc_select __NR__newselect
  46. #else
  47. # define __NR___libc_select __NR_select
  48. #endif
  49. _syscall5(int, __libc_select, int, n, fd_set *, readfds, fd_set *, writefds,
  50. fd_set *, exceptfds, struct timeval *, timeout)
  51. #endif
  52. weak_alias(__libc_select,select)
  53. libc_hidden_weak(select)