yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 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 | |
| 10 | #include <sys/syscall.h> |
| 11 | #include <sys/select.h> |
| 12 | #include <stdint.h> |
| 13 | |
| 14 | #ifdef __UCLIBC_HAS_THREADS_NATIVE__ |
| 15 | #include <sysdep-cancel.h> |
| 16 | #else |
| 17 | #define SINGLE_THREAD_P 1 |
| 18 | #endif |
| 19 | |
| 20 | #define USEC_PER_SEC 1000000L |
| 21 | |
| 22 | extern __typeof(select) __libc_select; |
| 23 | |
| 24 | #if !defined(__NR__newselect) && !defined(__NR_select) && defined __USE_XOPEN2K |
| 25 | # define __NR___libc_pselect6 __NR_pselect6 |
| 26 | static _syscall6(int, __libc_pselect6, int, n, fd_set *, readfds, fd_set *, writefds, |
| 27 | fd_set *, exceptfds, const struct timespec *, timeout, |
| 28 | const sigset_t *, sigmask) |
| 29 | |
| 30 | int __libc_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
| 31 | struct timeval *timeout) |
| 32 | { |
| 33 | struct timespec _ts, *ts = 0; |
| 34 | if (timeout) { |
| 35 | uint32_t usec; |
| 36 | _ts.tv_sec = timeout->tv_sec; |
| 37 | |
| 38 | /* GNU extension: allow for timespec values where the sub-sec |
| 39 | * field is equal to or more than 1 second. The kernel will |
| 40 | * reject this on us, so take care of the time shift ourself. |
| 41 | * Some applications (like readline and linphone) do this. |
| 42 | * See 'clarification on select() type calls and invalid timeouts' |
| 43 | * on the POSIX general list for more information. |
| 44 | */ |
| 45 | usec = timeout->tv_usec; |
| 46 | if (usec >= USEC_PER_SEC) { |
| 47 | _ts.tv_sec += usec / USEC_PER_SEC; |
| 48 | usec %= USEC_PER_SEC; |
| 49 | } |
| 50 | _ts.tv_nsec = usec * 1000; |
| 51 | |
| 52 | ts = &_ts; |
| 53 | } |
| 54 | |
| 55 | if (SINGLE_THREAD_P) |
| 56 | return __libc_pselect6(n, readfds, writefds, exceptfds, ts, 0); |
| 57 | #ifdef __UCLIBC_HAS_THREADS_NATIVE__ |
| 58 | int oldtype = LIBC_CANCEL_ASYNC (); |
| 59 | int result = __libc_pselect6(n, readfds, writefds, exceptfds, ts, 0); |
| 60 | LIBC_CANCEL_RESET (oldtype); |
| 61 | return result; |
| 62 | #endif |
| 63 | |
| 64 | } |
| 65 | |
| 66 | #else |
| 67 | |
| 68 | #ifdef __NR__newselect |
| 69 | # define __NR___syscall_select __NR__newselect |
| 70 | #else |
| 71 | # define __NR___syscall_select __NR_select |
| 72 | #endif |
| 73 | |
| 74 | static _syscall5(int, __syscall_select, int, n, fd_set *, readfds, |
| 75 | fd_set *, writefds, fd_set *, exceptfds, struct timeval *, timeout); |
| 76 | |
| 77 | int __libc_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
| 78 | struct timeval *timeout) |
| 79 | { |
| 80 | if (SINGLE_THREAD_P) |
| 81 | return __syscall_select(n, readfds, writefds, exceptfds, timeout); |
| 82 | |
| 83 | #ifdef __UCLIBC_HAS_THREADS_NATIVE__ |
| 84 | int oldtype = LIBC_CANCEL_ASYNC (); |
| 85 | int result = __syscall_select(n, readfds, writefds, exceptfds, timeout); |
| 86 | LIBC_CANCEL_RESET (oldtype); |
| 87 | return result; |
| 88 | #endif |
| 89 | } |
| 90 | |
| 91 | #endif |
| 92 | |
| 93 | weak_alias(__libc_select,select) |
| 94 | libc_hidden_weak(select) |