1 | /* Linux select implementation. |
2 | Copyright (C) 2017-2021 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library. If not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <sys/time.h> |
20 | #include <sys/types.h> |
21 | #include <sys/select.h> |
22 | #include <errno.h> |
23 | #include <sysdep-cancel.h> |
24 | #include <time64-support.h> |
25 | |
26 | /* Check the first NFDS descriptors each in READFDS (if not NULL) for read |
27 | readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS |
28 | (if not NULL) for exceptional conditions. If TIMEOUT is not NULL, time out |
29 | after waiting the interval specified therein. Returns the number of ready |
30 | descriptors, or -1 for errors. */ |
31 | |
32 | int |
33 | __select64 (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
34 | struct __timeval64 *timeout) |
35 | { |
36 | struct __timespec64 ts64, *pts64 = NULL; |
37 | if (timeout != NULL) |
38 | { |
39 | ts64 = timeval64_to_timespec64 (*timeout); |
40 | pts64 = &ts64; |
41 | } |
42 | |
43 | #ifndef __NR_pselect6_time64 |
44 | # define __NR_pselect6_time64 __NR_pselect6 |
45 | #endif |
46 | int r; |
47 | if (supports_time64 ()) |
48 | { |
49 | r = SYSCALL_CANCEL (pselect6_time64, nfds, readfds, writefds, exceptfds, |
50 | pts64, NULL); |
51 | /* Linux by default will update the timeout after a pselect6 syscall |
52 | (though the pselect() glibc call suppresses this behavior). |
53 | Since select() on Linux has the same behavior as the pselect6 |
54 | syscall, we update the timeout here. */ |
55 | if (r == 0 || errno != ENOSYS) |
56 | { |
57 | if (timeout != NULL) |
58 | TIMEVAL_TO_TIMESPEC (timeout, &ts64); |
59 | return r; |
60 | } |
61 | |
62 | mark_time64_unsupported (); |
63 | } |
64 | |
65 | #ifndef __ASSUME_TIME64_SYSCALLS |
66 | struct timespec ts32, *pts32 = NULL; |
67 | if (timeout != NULL) |
68 | { |
69 | if (! in_time_t_range (timeout->tv_sec)) |
70 | { |
71 | __set_errno (EINVAL); |
72 | return -1; |
73 | } |
74 | ts32 = valid_timespec64_to_timespec (ts64); |
75 | pts32 = &ts32; |
76 | } |
77 | # ifndef __ASSUME_PSELECT |
78 | # ifdef __NR__newselect |
79 | # undef __NR_select |
80 | # define __NR_select __NR__newselect |
81 | # endif |
82 | r = SYSCALL_CANCEL (select, nfds, readfds, writefds, exceptfds, pts32); |
83 | # else |
84 | r = SYSCALL_CANCEL (pselect6, nfds, readfds, writefds, exceptfds, pts32, |
85 | NULL); |
86 | # endif |
87 | if (r >= 0 && timeout != NULL) |
88 | *timeout = valid_timespec_to_timeval64 (ts32); |
89 | #endif |
90 | |
91 | return r; |
92 | } |
93 | |
94 | #if __TIMESIZE != 64 |
95 | libc_hidden_def (__select64) |
96 | |
97 | int |
98 | __select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
99 | struct timeval *timeout) |
100 | { |
101 | struct __timeval64 tv64, *ptv64 = NULL; |
102 | if (timeout != NULL) |
103 | { |
104 | tv64 = valid_timeval_to_timeval64 (*timeout); |
105 | ptv64 = &tv64; |
106 | } |
107 | int r = __select64 (nfds, readfds, writefds, exceptfds, ptv64); |
108 | if (r >= 0 && timeout != NULL) |
109 | /* The remanining timeout will be always less the input TIMEOUT. */ |
110 | *timeout = valid_timeval64_to_timeval (tv64); |
111 | return r; |
112 | } |
113 | #endif |
114 | libc_hidden_def (__select) |
115 | |
116 | weak_alias (__select, select) |
117 | weak_alias (__select, __libc_select) |
118 | |