1 /* Linux select implementation.
2 Copyright (C) 2017-2021 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>
24 #include <time64-support.h>
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. */
33 __select64 (int nfds
, fd_set
*readfds
, fd_set
*writefds
, fd_set
*exceptfds
,
34 struct __timeval64
*timeout
)
36 struct __timespec64 ts64
, *pts64
= NULL
;
39 ts64
= timeval64_to_timespec64 (*timeout
);
43 #ifndef __NR_pselect6_time64
44 # define __NR_pselect6_time64 __NR_pselect6
47 if (supports_time64 ())
49 r
= SYSCALL_CANCEL (pselect6_time64
, nfds
, readfds
, writefds
, exceptfds
,
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
)
58 TIMEVAL_TO_TIMESPEC (timeout
, &ts64
);
62 mark_time64_unsupported ();
65 #ifndef __ASSUME_TIME64_SYSCALLS
66 struct timespec ts32
, *pts32
= NULL
;
69 if (! in_time_t_range (timeout
->tv_sec
))
74 ts32
= valid_timespec64_to_timespec (ts64
);
77 # ifndef __ASSUME_PSELECT
78 # ifdef __NR__newselect
80 # define __NR_select __NR__newselect
82 r
= SYSCALL_CANCEL (select
, nfds
, readfds
, writefds
, exceptfds
, pts32
);
84 r
= SYSCALL_CANCEL (pselect6
, nfds
, readfds
, writefds
, exceptfds
, pts32
,
87 if (r
>= 0 && timeout
!= NULL
)
88 *timeout
= valid_timespec_to_timeval64 (ts32
);
95 libc_hidden_def (__select64
)
98 __select (int nfds
, fd_set
*readfds
, fd_set
*writefds
, fd_set
*exceptfds
,
99 struct timeval
*timeout
)
101 struct __timeval64 tv64
, *ptv64
= NULL
;
104 tv64
= valid_timeval_to_timeval64 (*timeout
);
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
);
114 libc_hidden_def (__select
)
116 weak_alias (__select
, select
)
117 weak_alias (__select
, __libc_select
)