xref: /llvm-project/libc/src/sys/epoll/linux/epoll_wait.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
1 //===---------- Linux implementation of the epoll_wait function -----------===//
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/epoll/epoll_wait.h"
10 
11 #include "hdr/signal_macros.h" // for NSIG
12 #include "hdr/types/sigset_t.h"
13 #include "hdr/types/struct_epoll_event.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/__support/macros/sanitizer.h"
18 #include "src/errno/libc_errno.h"
19 
20 #include <sys/syscall.h> // For syscall numbers.
21 
22 namespace LIBC_NAMESPACE_DECL {
23 
24 LLVM_LIBC_FUNCTION(int, epoll_wait,
25                    (int epfd, struct epoll_event *events, int maxevents,
26                     int timeout)) {
27 #ifdef SYS_epoll_wait
28   int ret = LIBC_NAMESPACE::syscall_impl<int>(
29       SYS_epoll_wait, epfd, reinterpret_cast<long>(events), maxevents, timeout);
30 #elif defined(SYS_epoll_pwait)
31   int ret = LIBC_NAMESPACE::syscall_impl<int>(
32       SYS_epoll_pwait, epfd, reinterpret_cast<long>(events), maxevents, timeout,
33       reinterpret_cast<long>(nullptr), NSIG / 8);
34 #else
35 #error "epoll_wait and epoll_pwait are unavailable. Unable to build epoll_wait."
36 #endif
37   // A negative return value indicates an error with the magnitude of the
38   // value being the error code.
39   if (ret < 0) {
40     libc_errno = -ret;
41     return -1;
42   }
43 
44   MSAN_UNPOISON(events, ret * sizeof(struct epoll_event));
45 
46   return ret;
47 }
48 
49 } // namespace LIBC_NAMESPACE_DECL
50