| 1 | //===-- Implementation of poll --------------------------------------------===// |
| 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/poll/poll.h" |
| 10 | |
| 11 | #include "hdr/types/nfds_t.h" |
| 12 | #include "hdr/types/struct_pollfd.h" |
| 13 | #include "hdr/types/struct_timespec.h" |
| 14 | #include "src/__support/OSUtil/syscall.h" // syscall_impl |
| 15 | #include "src/__support/common.h" |
| 16 | #include "src/__support/libc_errno.h" |
| 17 | #include "src/__support/macros/config.h" |
| 18 | |
| 19 | #include <sys/syscall.h> // SYS_poll, SYS_ppoll |
| 20 | |
| 21 | namespace LIBC_NAMESPACE_DECL { |
| 22 | |
| 23 | LLVM_LIBC_FUNCTION(int, poll, (pollfd * fds, nfds_t nfds, int timeout)) { |
| 24 | int ret = 0; |
| 25 | |
| 26 | #if defined(SYS_poll) |
| 27 | ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_poll, fds, nfds, timeout); |
| 28 | #else // no SYS_poll |
| 29 | timespec ts, *tsp; |
| 30 | if (timeout >= 0) { |
| 31 | ts.tv_sec = timeout / 1000; |
| 32 | ts.tv_nsec = (timeout % 1000) * 1000000; |
| 33 | tsp = &ts; |
| 34 | } else { |
| 35 | tsp = nullptr; |
| 36 | } |
| 37 | #if defined(SYS_ppoll) |
| 38 | ret = |
| 39 | LIBC_NAMESPACE::syscall_impl<int>(SYS_ppoll, fds, nfds, tsp, nullptr, 0); |
| 40 | #elif defined(SYS_ppoll_time64) |
| 41 | ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ppoll_time64, fds, nfds, tsp, |
| 42 | nullptr, 0); |
| 43 | #else |
| 44 | #error "poll, ppoll, ppoll_time64 syscalls not available." |
| 45 | #endif // defined(SYS_ppoll) || defined(SYS_ppoll_time64) |
| 46 | #endif // defined(SYS_poll) |
| 47 | |
| 48 | if (ret < 0) { |
| 49 | libc_errno = -ret; |
| 50 | return -1; |
| 51 | } |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | } // namespace LIBC_NAMESPACE_DECL |
| 56 | |