1 /* Linux select implementation.
2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
20 #include <sys/types.h>
21 #include <sys/select.h>
23 #include <sysdep-cancel.h>
25 /* Check the first NFDS descriptors each in READFDS (if not NULL) for read
26 readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS
27 (if not NULL) for exceptional conditions. If TIMEOUT is not NULL, time out
28 after waiting the interval specified therein. Returns the number of ready
29 descriptors, or -1 for errors. */
32 __select64 (int nfds
, fd_set
*readfds
, fd_set
*writefds
, fd_set
*exceptfds
,
33 struct __timeval64
*timeout
)
35 __time64_t s
= timeout
!= NULL
? timeout
->tv_sec
: 0;
36 int32_t us
= timeout
!= NULL
? timeout
->tv_usec
: 0;
40 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL
);
42 /* Normalize the timeout, as legacy Linux __NR_select and __NR__newselect.
43 Different than syscall, it also handle possible overflow. */
44 if (us
/ USEC_PER_SEC
> INT64_MAX
- s
)
47 ns
= NSEC_PER_SEC
- 1;
51 s
+= us
/ USEC_PER_SEC
;
52 us
= us
% USEC_PER_SEC
;
53 ns
= us
* NSEC_PER_USEC
;
56 struct __timespec64 ts64
, *pts64
= NULL
;
64 #ifndef __NR_pselect6_time64
65 # define __NR_pselect6_time64 __NR_pselect6
68 #ifdef __ASSUME_TIME64_SYSCALLS
69 int r
= SYSCALL_CANCEL (pselect6_time64
, nfds
, readfds
, writefds
, exceptfds
,
72 TIMESPEC_TO_TIMEVAL (timeout
, pts64
);
75 bool need_time64
= timeout
!= NULL
&& !in_int32_t_range (timeout
->tv_sec
);
78 int r
= SYSCALL_CANCEL (pselect6_time64
, nfds
, readfds
, writefds
,
79 exceptfds
, pts64
, NULL
);
80 if ((r
>= 0 || errno
!= ENOSYS
) && timeout
!= NULL
)
82 TIMESPEC_TO_TIMEVAL (timeout
, &ts64
);
85 __set_errno (EOVERFLOW
);
89 # ifdef __ASSUME_PSELECT
90 struct timespec ts32
, *pts32
= NULL
;
93 ts32
.tv_sec
= pts64
->tv_sec
;
94 ts32
.tv_nsec
= pts64
->tv_nsec
;
98 int r
= SYSCALL_CANCEL (pselect6
, nfds
, readfds
, writefds
, exceptfds
, pts32
,
101 TIMESPEC_TO_TIMEVAL (timeout
, pts32
);
104 struct timeval tv32
, *ptv32
= NULL
;
107 tv32
= valid_timespec64_to_timeval (*pts64
);
111 int r
= SYSCALL_CANCEL (_newselect
, nfds
, readfds
, writefds
, exceptfds
, ptv32
);
113 *timeout
= valid_timeval_to_timeval64 (tv32
);
115 # endif /* __ASSUME_PSELECT */
120 libc_hidden_def (__select64
)
123 __select (int nfds
, fd_set
*readfds
, fd_set
*writefds
, fd_set
*exceptfds
,
124 struct timeval
*timeout
)
126 struct __timeval64 tv64
, *ptv64
= NULL
;
129 tv64
= valid_timeval_to_timeval64 (*timeout
);
132 int r
= __select64 (nfds
, readfds
, writefds
, exceptfds
, ptv64
);
134 /* The remanining timeout will be always less the input TIMEOUT. */
135 *timeout
= valid_timeval64_to_timeval (tv64
);
139 libc_hidden_def (__select
)
141 weak_alias (__select
, select
)
142 weak_alias (__select
, __libc_select
)