1 //===-- Linux implementation of select ------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "src/sys/select/select.h" 10 11 #include "hdr/types/sigset_t.h" 12 #include "hdr/types/struct_timespec.h" 13 #include "src/__support/CPP/limits.h" 14 #include "src/__support/OSUtil/syscall.h" // For internal syscall function. 15 #include "src/__support/common.h" 16 #include "src/__support/macros/config.h" 17 #include "src/errno/libc_errno.h" 18 19 #include <stddef.h> // For size_t 20 #include <sys/syscall.h> // For syscall numbers. 21 22 namespace LIBC_NAMESPACE_DECL { 23 24 struct pselect6_sigset_t { 25 sigset_t *ss; 26 size_t ss_len; 27 }; 28 29 LLVM_LIBC_FUNCTION(int, select, 30 (int nfds, fd_set *__restrict read_set, 31 fd_set *__restrict write_set, fd_set *__restrict error_set, 32 struct timeval *__restrict timeout)) { 33 // Linux has a SYS_select syscall but it is not available on all 34 // architectures. So, we use the SYS_pselect6 syscall which is more 35 // widely available. However, SYS_pselect6 takes a struct timespec argument 36 // instead of a struct timeval argument. Also, it takes an additional 37 // argument which is a pointer to an object of a type defined above as 38 // "pselect6_sigset_t". 39 struct timespec ts { 40 0, 0 41 }; 42 if (timeout != nullptr) { 43 // In general, if the tv_sec and tv_usec in |timeout| are correctly set, 44 // then converting tv_usec to nanoseconds will not be a problem. However, 45 // if tv_usec in |timeout| is more than a second, it can lead to overflows. 46 // So, we detect such cases and adjust. 47 constexpr time_t TIME_MAX = cpp::numeric_limits<time_t>::max(); 48 if ((TIME_MAX - timeout->tv_sec) < (timeout->tv_usec / 1000000)) { 49 ts.tv_sec = TIME_MAX; 50 ts.tv_nsec = 999999999; 51 } else { 52 ts.tv_sec = timeout->tv_sec + timeout->tv_usec / 1000000; 53 ts.tv_nsec = timeout->tv_usec * 1000; 54 } 55 } 56 pselect6_sigset_t pss{nullptr, sizeof(sigset_t)}; 57 #if SYS_pselect6 58 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pselect6, nfds, read_set, 59 write_set, error_set, &ts, &pss); 60 #elif defined(SYS_pselect6_time64) 61 int ret = LIBC_NAMESPACE::syscall_impl<int>( 62 SYS_pselect6_time64, nfds, read_set, write_set, error_set, &ts, &pss); 63 #else 64 #error "SYS_pselect6 and SYS_pselect6_time64 syscalls not available." 65 #endif 66 if (ret < 0) { 67 libc_errno = -ret; 68 return -1; 69 } 70 return ret; 71 } 72 73 } // namespace LIBC_NAMESPACE_DECL 74