|
4 | 4 | #include <errno.h>
|
5 | 5 | #include "syscall.h"
|
6 | 6 |
|
7 |
| -#define IS32BIT(x) !((x)+0x80000000ULL>>32) |
8 |
| -#define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63)) |
9 |
| - |
10 | 7 | int select(int n, fd_set *restrict rfds, fd_set *restrict wfds, fd_set *restrict efds, struct timeval *restrict tv)
|
11 | 8 | {
|
12 |
| - time_t s = tv ? tv->tv_sec : 0; |
13 |
| - suseconds_t us = tv ? tv->tv_usec : 0; |
14 |
| - long ns; |
15 |
| - const time_t max_time = (1ULL<<8*sizeof(time_t)-1)-1; |
16 |
| - |
17 |
| - if (s<0 || us<0) return __syscall_ret(-EINVAL); |
18 |
| - if (us/1000000 > max_time - s) { |
19 |
| - s = max_time; |
20 |
| - us = 999999; |
21 |
| - ns = 999999999; |
22 |
| - } else { |
23 |
| - s += us/1000000; |
24 |
| - us %= 1000000; |
25 |
| - ns = us*1000; |
26 |
| - } |
27 |
| - |
28 |
| -#ifdef SYS_pselect6_time64 |
29 |
| - int r = -ENOSYS; |
30 |
| - if (SYS_pselect6 == SYS_pselect6_time64 || !IS32BIT(s)) |
31 |
| - r = __syscall_cp(SYS_pselect6_time64, n, rfds, wfds, efds, |
32 |
| - tv ? ((long long[]){s, ns}) : 0, |
33 |
| - ((syscall_arg_t[]){ 0, _NSIG/8 })); |
34 |
| - if (SYS_pselect6 == SYS_pselect6_time64 || r!=-ENOSYS) |
35 |
| - return __syscall_ret(r); |
36 |
| - s = CLAMP(s); |
37 |
| -#endif |
38 | 9 | #ifdef SYS_select
|
39 |
| - return syscall_cp(SYS_select, n, rfds, wfds, efds, |
40 |
| - tv ? ((long[]){s, us}) : 0); |
| 10 | + return syscall_cp(SYS_select, n, rfds, wfds, efds, tv); |
41 | 11 | #else
|
42 |
| - return syscall_cp(SYS_pselect6, n, rfds, wfds, efds, |
43 |
| - tv ? ((long[]){s, ns}) : 0, ((syscall_arg_t[]){ 0, _NSIG/8 })); |
| 12 | + syscall_arg_t data[2] = { 0, _NSIG/8 }; |
| 13 | + struct timespec ts; |
| 14 | + if (tv) { |
| 15 | + if (tv->tv_sec < 0 || tv->tv_usec < 0) |
| 16 | + return __syscall_ret(-EINVAL); |
| 17 | + time_t extra_secs = tv->tv_usec / 1000000; |
| 18 | + ts.tv_nsec = tv->tv_usec % 1000000 * 1000; |
| 19 | + const time_t max_time = (1ULL<<8*sizeof(time_t)-1)-1; |
| 20 | + ts.tv_sec = extra_secs > max_time - tv->tv_sec ? |
| 21 | + max_time : tv->tv_sec + extra_secs; |
| 22 | + } |
| 23 | + return syscall_cp(SYS_pselect6, n, rfds, wfds, efds, tv ? &ts : 0, data); |
44 | 24 | #endif
|
45 | 25 | }
|
0 commit comments