[libc-commits] [libc] 2603acc - [libc] Add sys/timerfd.h and timerfd_create on Linux. (#243360)
via libc-commits
libc-commits at lists.llvm.org
Tue Nov 17 14:11:49 PST 2026
Author: Alexey Samsonov
Date: 2026-11-17T14:11:44-08:00
New Revision: 2603acc621665d61de65c86edc2e823dea26374e
URL: https://github.com/llvm/llvm-project/commit/2603acc621665d61de65c86edc2e823dea26374e
DIFF: https://github.com/llvm/llvm-project/commit/2603acc621665d61de65c86edc2e823dea26374e.diff
LOG: [libc] Add sys/timerfd.h and timerfd_create on Linux. (#243360)
* Add `<sys/timerfd.h>` header on Linux platforms.
* Provide macro definitions for `TFD_*` constants, and ensure `CLOCK_*`
values are also available.
* Implement one of the syscall wrappers defined in this file -
timerfd_create (as a straightforward syscall wrapper).
* Add a simple unit test that validates that returned fd has correct
flags and can be read/closed.
See https://man7.org/linux/man-pages/man2/timerfd_create.2.html for the
docs.
Added:
libc/hdr/sys_timerfd_macros.h
libc/include/llvm-libc-macros/linux/sys-timerfd-macros.h
libc/include/llvm-libc-macros/sys-timerfd-macros.h
libc/include/sys/timerfd.yaml
libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_create.h
libc/src/sys/timerfd/CMakeLists.txt
libc/src/sys/timerfd/linux/CMakeLists.txt
libc/src/sys/timerfd/linux/timerfd_create.cpp
libc/src/sys/timerfd/timerfd_create.h
libc/test/src/sys/timerfd/CMakeLists.txt
libc/test/src/sys/timerfd/linux/CMakeLists.txt
libc/test/src/sys/timerfd/linux/timerfd_create_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/aarch64/headers.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/riscv/headers.txt
libc/config/linux/x86_64/entrypoints.txt
libc/config/linux/x86_64/headers.txt
libc/hdr/CMakeLists.txt
libc/include/CMakeLists.txt
libc/include/llvm-libc-macros/CMakeLists.txt
libc/include/llvm-libc-macros/linux/CMakeLists.txt
libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
libc/src/sys/CMakeLists.txt
libc/test/src/sys/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index f3c59e6fd4e14e..c115a3b6708917 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -404,6 +404,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/sysinfo.h entrypoints
libc.src.sys.sysinfo.sysinfo
+ # sys/timerfd.h entrypoints
+ libc.src.sys.timerfd.timerfd_create
+
# sys/utsname.h entrypoints
libc.src.sys.utsname.uname
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index fb149f1f58e6c9..d003d672f0c3e4 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -80,6 +80,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_sysmacros
libc.include.sys_syslog
libc.include.sys_time
+ libc.include.sys_timerfd
libc.include.sys_types
libc.include.sys_uio
libc.include.sys_un
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 1f00ce729169df..cf3de959f57ae4 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -447,6 +447,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/time.h entrypoints
libc.src.sys.time.utimes
+ # sys/timerfd.h entrypoints
+ libc.src.sys.timerfd.timerfd_create
+
# sys/sysinfo.h entrypoints
libc.src.sys.sysinfo.sysinfo
diff --git a/libc/config/linux/riscv/headers.txt b/libc/config/linux/riscv/headers.txt
index 5b24f1c70ff9d3..0262eb12781637 100644
--- a/libc/config/linux/riscv/headers.txt
+++ b/libc/config/linux/riscv/headers.txt
@@ -83,6 +83,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_sysmacros
libc.include.sys_syslog
libc.include.sys_time
+ libc.include.sys_timerfd
libc.include.sys_types
libc.include.sys_ucontext
libc.include.sys_uio
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 2eb84751b51174..35d23c3b9977b7 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -447,6 +447,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/time.h entrypoints
libc.src.sys.time.utimes
+ # sys/timerfd.h entrypoints
+ libc.src.sys.timerfd.timerfd_create
+
# sys/sysinfo.h entrypoints
libc.src.sys.sysinfo.sysinfo
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index 8d2c0a3ca2990b..caed1ed0e6db78 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -83,6 +83,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_sysmacros
libc.include.sys_syslog
libc.include.sys_time
+ libc.include.sys_timerfd
libc.include.sys_types
libc.include.sys_ucontext
libc.include.sys_uio
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index 72b459471111da..15b03c616fe5bb 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -320,6 +320,14 @@ add_proxy_header_library(
libc.include.llvm-libc-macros.sys_time_macros
)
+add_proxy_header_library(
+ sys_timerfd_macros
+ HDRS
+ sys_timerfd_macros.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-macros.sys_timerfd_macros
+)
+
add_proxy_header_library(
sys_wait_macros
HDRS
diff --git a/libc/hdr/sys_timerfd_macros.h b/libc/hdr/sys_timerfd_macros.h
new file mode 100644
index 00000000000000..31946fd9c61666
--- /dev/null
+++ b/libc/hdr/sys_timerfd_macros.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Proxy header for macro values defined in sys/timerfd.h.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_SYS_TIMERFD_MACROS_H
+#define LLVM_LIBC_HDR_SYS_TIMERFD_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/sys-timerfd-macros.h"
+
+#else // Overlay mode
+
+#include <sys/timerfd.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_SYS_TIMERFD_MACROS_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index ef3e64059d2410..2261acf58095df 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -1049,6 +1049,16 @@ add_header_macro(
.llvm-libc-types.time_t
)
+add_header_macro(
+ sys_timerfd
+ ../libc/include/sys/timerfd.yaml
+ sys/timerfd.h
+ DEPENDS
+ .llvm_libc_common_h
+ .llvm-libc-macros.sys_timerfd_macros
+ .llvm-libc-macros.time_macros
+)
+
add_header_macro(
sys_types
../libc/include/sys/types.yaml
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index cae9a7613c9ccd..a71a370dd59ac3 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -425,6 +425,12 @@ add_macro_header(
sys-time-macros.h
)
+add_macro_header(
+ sys_timerfd_macros
+ HDR
+ sys-timerfd-macros.h
+)
+
add_macro_header(
sys_wait_macros
HDR
diff --git a/libc/include/llvm-libc-macros/linux/CMakeLists.txt b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
index d1d466d3501c12..30c79487d163cc 100644
--- a/libc/include/llvm-libc-macros/linux/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
@@ -172,6 +172,14 @@ add_header(
sys-time-macros.h
)
+add_header(
+ sys_timerfd_macros
+ HDR
+ sys-timerfd-macros.h
+ DEPENDS
+ .fcntl_macros
+)
+
add_header(
unistd_macros
HDR
diff --git a/libc/include/llvm-libc-macros/linux/sys-timerfd-macros.h b/libc/include/llvm-libc-macros/linux/sys-timerfd-macros.h
new file mode 100644
index 00000000000000..ea23305b83446d
--- /dev/null
+++ b/libc/include/llvm-libc-macros/linux/sys-timerfd-macros.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux specific declarations of macros from sys/timerfd.h.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_LINUX_SYS_TIMERFD_MACROS_H
+#define LLVM_LIBC_MACROS_LINUX_SYS_TIMERFD_MACROS_H
+
+#include "fcntl-macros.h"
+
+// Flags for timerfd_create.
+#define TFD_CLOEXEC O_CLOEXEC
+#define TFD_NONBLOCK O_NONBLOCK
+
+// Flags for timerfd_settime.
+#define TFD_TIMER_ABSTIME (1 << 0)
+#define TFD_TIMER_CANCEL_ON_SET (1 << 1)
+
+#endif // LLVM_LIBC_MACROS_LINUX_SYS_TIMERFD_MACROS_H
diff --git a/libc/include/llvm-libc-macros/sys-timerfd-macros.h b/libc/include/llvm-libc-macros/sys-timerfd-macros.h
new file mode 100644
index 00000000000000..1c1a7924c18683
--- /dev/null
+++ b/libc/include/llvm-libc-macros/sys-timerfd-macros.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Macros defined in sys/timerfd.h header file.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_SYS_TIMERFD_MACROS_H
+#define LLVM_LIBC_MACROS_SYS_TIMERFD_MACROS_H
+
+#ifdef __linux__
+#include "linux/sys-timerfd-macros.h"
+#endif
+
+#endif // LLVM_LIBC_MACROS_SYS_TIMERFD_MACROS_H
diff --git a/libc/include/sys/timerfd.yaml b/libc/include/sys/timerfd.yaml
new file mode 100644
index 00000000000000..285bc5aa16c1c9
--- /dev/null
+++ b/libc/include/sys/timerfd.yaml
@@ -0,0 +1,31 @@
+header: sys/timerfd.h
+standards:
+ - linux
+macros:
+ - macro_name: CLOCK_BOOTTIME
+ macro_header: time-macros.h
+ - macro_name: CLOCK_BOOTTIME_ALARM
+ macro_header: time-macros.h
+ - macro_name: CLOCK_MONOTONIC
+ macro_header: time-macros.h
+ - macro_name: CLOCK_REALTIME
+ macro_header: time-macros.h
+ - macro_name: CLOCK_REALTIME_ALARM
+ macro_header: time-macros.h
+ - macro_name: TFD_CLOEXEC
+ macro_header: sys-timerfd-macros.h
+ - macro_name: TFD_NONBLOCK
+ macro_header: sys-timerfd-macros.h
+ - macro_name: TFD_TIMER_ABSTIME
+ macro_header: sys-timerfd-macros.h
+ - macro_name: TFD_TIMER_CANCEL_ON_SET
+ macro_header: sys-timerfd-macros.h
+types: []
+enums: []
+objects: []
+functions:
+ - name: timerfd_create
+ return_type: int
+ arguments:
+ - type: int
+ - type: int
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index ca82428cdc847b..cd2f92ce59e718 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -2661,3 +2661,15 @@ add_header_library(
libc.src.__support.error_or
libc.src.__support.macros.config
)
+
+add_header_library(
+ timerfd_create
+ HDRS
+ timerfd_create.h
+ DEPENDS
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_create.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_create.h
new file mode 100644
index 00000000000000..03adce2a46e428
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_create.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for timerfd_create.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_CREATE_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_CREATE_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> timerfd_create(int clockid, int flags) {
+ return syscall_checked<int>(SYS_timerfd_create, clockid, flags);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_CREATE_H
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index 603eb78b140ca0..c891c471ed4f13 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -17,6 +17,7 @@ add_subdirectory(stat)
add_subdirectory(statfs)
add_subdirectory(statvfs)
add_subdirectory(time)
+add_subdirectory(timerfd)
add_subdirectory(utsname)
add_subdirectory(wait)
add_subdirectory(prctl)
diff --git a/libc/src/sys/timerfd/CMakeLists.txt b/libc/src/sys/timerfd/CMakeLists.txt
new file mode 100644
index 00000000000000..69c51ec8919a4c
--- /dev/null
+++ b/libc/src/sys/timerfd/CMakeLists.txt
@@ -0,0 +1,10 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+ timerfd_create
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.timerfd_create
+)
diff --git a/libc/src/sys/timerfd/linux/CMakeLists.txt b/libc/src/sys/timerfd/linux/CMakeLists.txt
new file mode 100644
index 00000000000000..b1a485a907a61f
--- /dev/null
+++ b/libc/src/sys/timerfd/linux/CMakeLists.txt
@@ -0,0 +1,13 @@
+add_entrypoint_object(
+ timerfd_create
+ SRCS
+ timerfd_create.cpp
+ HDRS
+ ../timerfd_create.h
+ DEPENDS
+ libc.src.__support.OSUtil.linux.syscall_wrappers.timerfd_create
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.errno.errno
+)
diff --git a/libc/src/sys/timerfd/linux/timerfd_create.cpp b/libc/src/sys/timerfd/linux/timerfd_create.cpp
new file mode 100644
index 00000000000000..a92955a3a24ae4
--- /dev/null
+++ b/libc/src/sys/timerfd/linux/timerfd_create.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of timerfd_create.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/timerfd/timerfd_create.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/timerfd_create.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, timerfd_create, (int clockid, int flags)) {
+ ErrorOr<int> ret = linux_syscalls::timerfd_create(clockid, flags);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return *ret;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/timerfd/timerfd_create.h b/libc/src/sys/timerfd/timerfd_create.h
new file mode 100644
index 00000000000000..9fd8026dadb3ce
--- /dev/null
+++ b/libc/src/sys/timerfd/timerfd_create.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for timerfd_create.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_CREATE_H
+#define LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_CREATE_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int timerfd_create(int clockid, int flags);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_CREATE_H
diff --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index 621d5736815ffb..6650f0842f3ae9 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -19,6 +19,7 @@ add_subdirectory(inotify)
add_subdirectory(ipc)
add_subdirectory(uio)
add_subdirectory(time)
+add_subdirectory(timerfd)
add_subdirectory(ioctl)
add_subdirectory(sem)
add_subdirectory(shm)
diff --git a/libc/test/src/sys/timerfd/CMakeLists.txt b/libc/test/src/sys/timerfd/CMakeLists.txt
new file mode 100644
index 00000000000000..b4bbe81c92ff2e
--- /dev/null
+++ b/libc/test/src/sys/timerfd/CMakeLists.txt
@@ -0,0 +1,3 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${LIBC_TARGET_OS})
+endif()
diff --git a/libc/test/src/sys/timerfd/linux/CMakeLists.txt b/libc/test/src/sys/timerfd/linux/CMakeLists.txt
new file mode 100644
index 00000000000000..b8ba88008737e7
--- /dev/null
+++ b/libc/test/src/sys/timerfd/linux/CMakeLists.txt
@@ -0,0 +1,23 @@
+add_libc_testsuite(libc-sys-timerfd-tests)
+
+add_libc_test(
+ timerfd_create_test
+ SUITE
+ libc-sys-timerfd-tests
+ SRCS
+ timerfd_create_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fcntl_macros
+ libc.hdr.stdint_proxy
+ libc.hdr.sys_timerfd_macros
+ libc.hdr.time_macros
+ libc.hdr.types.ssize_t
+ libc.src.__support.CPP.scope
+ libc.src.fcntl.fcntl
+ libc.src.sys.timerfd.timerfd_create
+ libc.src.unistd.close
+ libc.src.unistd.read
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
diff --git a/libc/test/src/sys/timerfd/linux/timerfd_create_test.cpp b/libc/test/src/sys/timerfd/linux/timerfd_create_test.cpp
new file mode 100644
index 00000000000000..8295aa8c4b4aae
--- /dev/null
+++ b/libc/test/src/sys/timerfd/linux/timerfd_create_test.cpp
@@ -0,0 +1,81 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unit tests for timerfd_create.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "hdr/sys_timerfd_macros.h"
+#include "hdr/time_macros.h"
+#include "hdr/types/ssize_t.h"
+#include "src/__support/CPP/scope.h"
+#include "src/fcntl/fcntl.h"
+#include "src/sys/timerfd/timerfd_create.h"
+#include "src/unistd/close.h"
+#include "src/unistd/read.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LIBC_NAMESPACE::cpp::scope_exit;
+using LlvmLibcTimerfdCreateTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcTimerfdCreateTest, ZeroFlags) {
+ int fd;
+ ASSERT_THAT(fd = LIBC_NAMESPACE::timerfd_create(CLOCK_MONOTONIC, 0),
+ returns(GE(0)).with_errno(EQ(0)));
+ scope_exit close_fd(
+ [&] { EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); });
+
+ int flags;
+ ASSERT_THAT(flags = LIBC_NAMESPACE::fcntl(fd, F_GETFD),
+ returns(GE(0)).with_errno(EQ(0)));
+ EXPECT_EQ(flags & FD_CLOEXEC, 0);
+
+ ASSERT_THAT(flags = LIBC_NAMESPACE::fcntl(fd, F_GETFL),
+ returns(GE(0)).with_errno(EQ(0)));
+ EXPECT_EQ(flags & O_NONBLOCK, 0);
+}
+
+TEST_F(LlvmLibcTimerfdCreateTest, CloexecAndNonblockRead) {
+ int fd;
+ ASSERT_THAT(fd = LIBC_NAMESPACE::timerfd_create(CLOCK_REALTIME,
+ TFD_CLOEXEC | TFD_NONBLOCK),
+ returns(GE(0)).with_errno(EQ(0)));
+ scope_exit close_fd(
+ [&] { EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); });
+
+ int flags;
+ ASSERT_THAT(flags = LIBC_NAMESPACE::fcntl(fd, F_GETFD),
+ returns(GE(0)).with_errno(EQ(0)));
+ EXPECT_NE(flags & FD_CLOEXEC, 0);
+
+ ASSERT_THAT(flags = LIBC_NAMESPACE::fcntl(fd, F_GETFL),
+ returns(GE(0)).with_errno(EQ(0)));
+ EXPECT_NE(flags & O_NONBLOCK, 0);
+
+ uint64_t val = 0;
+ // Reading smaller than sizeof(uint64_t) from a timerfd fails with EINVAL.
+ EXPECT_THAT(LIBC_NAMESPACE::read(fd, &val, sizeof(val) - 1),
+ Fails<ssize_t>(EINVAL));
+ // Disarmed non-blocking timerfd has no expirations and fails with EAGAIN
+ // instead of blocking.
+ EXPECT_THAT(LIBC_NAMESPACE::read(fd, &val, sizeof(val)),
+ Fails<ssize_t>(EAGAIN));
+}
+
+TEST_F(LlvmLibcTimerfdCreateTest, InvalidArgs) {
+ EXPECT_THAT(LIBC_NAMESPACE::timerfd_create(-1, 0), Fails(EINVAL));
+ EXPECT_THAT(LIBC_NAMESPACE::timerfd_create(CLOCK_MONOTONIC, -1),
+ Fails(EINVAL));
+}
More information about the libc-commits
mailing list