[libc-commits] [libc] [libc] Implement timerfd_(s|g)ettime on Linux. (PR #243657)
Alexey Samsonov via libc-commits
libc-commits at lists.llvm.org
Tue Nov 17 16:07:56 PST 2026
https://github.com/vonosmas updated https://github.com/llvm/llvm-project/pull/243657
>From 493407cad10e91818d35d52a33f7426e1c1c0639 Mon Sep 17 00:00:00 2001
From: Alexey Samsonov <vonosmas at gmail.com>
Date: Tue, 17 Nov 2026 23:30:24 +0000
Subject: [PATCH 1/2] [libc] Implement timerfd_(s|g)ettime on Linux.
---
libc/config/linux/aarch64/entrypoints.txt | 2 +
libc/config/linux/riscv/entrypoints.txt | 2 +
libc/config/linux/x86_64/entrypoints.txt | 2 +
libc/include/CMakeLists.txt | 1 +
libc/include/sys/syscall.h.def | 8 +
libc/include/sys/timerfd.yaml | 15 +-
.../linux/syscall_wrappers/CMakeLists.txt | 28 +++
.../linux/syscall_wrappers/timerfd_gettime.h | 50 ++++++
.../linux/syscall_wrappers/timerfd_settime.h | 53 ++++++
libc/src/sys/timerfd/CMakeLists.txt | 14 ++
libc/src/sys/timerfd/linux/CMakeLists.txt | 30 ++++
.../src/sys/timerfd/linux/timerfd_gettime.cpp | 34 ++++
.../src/sys/timerfd/linux/timerfd_settime.cpp | 36 ++++
libc/src/sys/timerfd/timerfd_gettime.h | 26 +++
libc/src/sys/timerfd/timerfd_settime.h | 27 +++
.../test/src/sys/timerfd/linux/CMakeLists.txt | 26 +++
.../timerfd/linux/timerfd_gettime_test.cpp | 159 ++++++++++++++++++
17 files changed, 512 insertions(+), 1 deletion(-)
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_gettime.h
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.h
create mode 100644 libc/src/sys/timerfd/linux/timerfd_gettime.cpp
create mode 100644 libc/src/sys/timerfd/linux/timerfd_settime.cpp
create mode 100644 libc/src/sys/timerfd/timerfd_gettime.h
create mode 100644 libc/src/sys/timerfd/timerfd_settime.h
create mode 100644 libc/test/src/sys/timerfd/linux/timerfd_gettime_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index c115a3b6708917..35b9f6ac7f1b4b 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -406,6 +406,8 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/timerfd.h entrypoints
libc.src.sys.timerfd.timerfd_create
+ libc.src.sys.timerfd.timerfd_gettime
+ libc.src.sys.timerfd.timerfd_settime
# sys/utsname.h entrypoints
libc.src.sys.utsname.uname
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index cf3de959f57ae4..d97e9d2aebc96f 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -449,6 +449,8 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/timerfd.h entrypoints
libc.src.sys.timerfd.timerfd_create
+ libc.src.sys.timerfd.timerfd_gettime
+ libc.src.sys.timerfd.timerfd_settime
# sys/sysinfo.h entrypoints
libc.src.sys.sysinfo.sysinfo
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 35d23c3b9977b7..50b658f83feba5 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -449,6 +449,8 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/timerfd.h entrypoints
libc.src.sys.timerfd.timerfd_create
+ libc.src.sys.timerfd.timerfd_gettime
+ libc.src.sys.timerfd.timerfd_settime
# sys/sysinfo.h entrypoints
libc.src.sys.sysinfo.sysinfo
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 2261acf58095df..1525c1662acda5 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -1057,6 +1057,7 @@ add_header_macro(
.llvm_libc_common_h
.llvm-libc-macros.sys_timerfd_macros
.llvm-libc-macros.time_macros
+ .llvm-libc-types.struct_itimerspec
)
add_header_macro(
diff --git a/libc/include/sys/syscall.h.def b/libc/include/sys/syscall.h.def
index 5c2b1eab9c2857..5e80ad3c20d945 100644
--- a/libc/include/sys/syscall.h.def
+++ b/libc/include/sys/syscall.h.def
@@ -2253,10 +2253,18 @@
#define SYS_timerfd_gettime __NR_timerfd_gettime
#endif
+#ifdef __NR_timerfd_gettime64
+#define SYS_timerfd_gettime64 __NR_timerfd_gettime64
+#endif
+
#ifdef __NR_timerfd_settime
#define SYS_timerfd_settime __NR_timerfd_settime
#endif
+#ifdef __NR_timerfd_settime64
+#define SYS_timerfd_settime64 __NR_timerfd_settime64
+#endif
+
#ifdef __NR_times
#define SYS_times __NR_times
#endif
diff --git a/libc/include/sys/timerfd.yaml b/libc/include/sys/timerfd.yaml
index 285bc5aa16c1c9..f840b1b837505a 100644
--- a/libc/include/sys/timerfd.yaml
+++ b/libc/include/sys/timerfd.yaml
@@ -20,7 +20,8 @@ macros:
macro_header: sys-timerfd-macros.h
- macro_name: TFD_TIMER_CANCEL_ON_SET
macro_header: sys-timerfd-macros.h
-types: []
+types:
+ - type_name: struct_itimerspec
enums: []
objects: []
functions:
@@ -29,3 +30,15 @@ functions:
arguments:
- type: int
- type: int
+ - name: timerfd_gettime
+ return_type: int
+ arguments:
+ - type: int
+ - type: struct itimerspec *
+ - name: timerfd_settime
+ return_type: int
+ arguments:
+ - type: int
+ - type: int
+ - type: const struct itimerspec *
+ - type: struct itimerspec *
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index cd2f92ce59e718..bb9d3a1846bef6 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -2673,3 +2673,31 @@ add_header_library(
libc.src.__support.error_or
libc.src.__support.macros.config
)
+
+add_header_library(
+ timerfd_gettime
+ HDRS
+ timerfd_gettime.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.hdr.types.struct_itimerspec
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ timerfd_settime
+ HDRS
+ timerfd_settime.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.hdr.types.struct_itimerspec
+ 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_gettime.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_gettime.h
new file mode 100644
index 00000000000000..7f7a7089cce1b0
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_gettime.h
@@ -0,0 +1,50 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_gettime.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_GETTIME_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_GETTIME_H
+
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/struct_itimerspec.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_gettime(int fd,
+ struct itimerspec *curr_value) {
+#if defined(SYS_timerfd_gettime64)
+ static_assert(
+ sizeof(timespec::tv_sec) == sizeof(int64_t),
+ "SYS_timerfd_gettime64 requires struct timespec with 64-bit members.");
+ return syscall_checked<int>(SYS_timerfd_gettime64, fd, curr_value);
+#elif defined(SYS_timerfd_gettime)
+ static_assert(
+ sizeof(timespec::tv_nsec) == sizeof(long),
+ "This legacy syscall fallback is only safe on platforms where tv_nsec "
+ "matches the register size (long). It is unsafe on 32-bit platforms "
+ "with 64-bit tv_nsec.");
+ return syscall_checked<int>(SYS_timerfd_gettime, fd, curr_value);
+#else
+#error "SYS_timerfd_gettime and SYS_timerfd_gettime64 syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_GETTIME_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.h
new file mode 100644
index 00000000000000..8c9a5530a4dba4
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.h
@@ -0,0 +1,53 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_settime.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_SETTIME_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_SETTIME_H
+
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/struct_itimerspec.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_settime(int fd, int flags,
+ const struct itimerspec *new_value,
+ struct itimerspec *old_value) {
+#if defined(SYS_timerfd_settime64)
+ static_assert(
+ sizeof(timespec::tv_sec) == sizeof(int64_t),
+ "SYS_timerfd_settime64 requires struct timespec with 64-bit members.");
+ return syscall_checked<int>(SYS_timerfd_settime64, fd, flags, new_value,
+ old_value);
+#elif defined(SYS_timerfd_settime)
+ static_assert(
+ sizeof(timespec::tv_nsec) == sizeof(long),
+ "This legacy syscall fallback is only safe on platforms where tv_nsec "
+ "matches the register size (long). It is unsafe on 32-bit platforms "
+ "with 64-bit tv_nsec.");
+ return syscall_checked<int>(SYS_timerfd_settime, fd, flags, new_value,
+ old_value);
+#else
+#error "SYS_timerfd_settime and SYS_timerfd_settime64 syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_SETTIME_H
diff --git a/libc/src/sys/timerfd/CMakeLists.txt b/libc/src/sys/timerfd/CMakeLists.txt
index 69c51ec8919a4c..d8668558f524cc 100644
--- a/libc/src/sys/timerfd/CMakeLists.txt
+++ b/libc/src/sys/timerfd/CMakeLists.txt
@@ -8,3 +8,17 @@ add_entrypoint_object(
DEPENDS
.${LIBC_TARGET_OS}.timerfd_create
)
+
+add_entrypoint_object(
+ timerfd_gettime
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.timerfd_gettime
+)
+
+add_entrypoint_object(
+ timerfd_settime
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.timerfd_settime
+)
diff --git a/libc/src/sys/timerfd/linux/CMakeLists.txt b/libc/src/sys/timerfd/linux/CMakeLists.txt
index b1a485a907a61f..b8c90d52bd608b 100644
--- a/libc/src/sys/timerfd/linux/CMakeLists.txt
+++ b/libc/src/sys/timerfd/linux/CMakeLists.txt
@@ -11,3 +11,33 @@ add_entrypoint_object(
libc.src.__support.macros.config
libc.src.errno.errno
)
+
+add_entrypoint_object(
+ timerfd_gettime
+ SRCS
+ timerfd_gettime.cpp
+ HDRS
+ ../timerfd_gettime.h
+ DEPENDS
+ libc.hdr.types.struct_itimerspec
+ libc.src.__support.OSUtil.linux.syscall_wrappers.timerfd_gettime
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.errno.errno
+)
+
+add_entrypoint_object(
+ timerfd_settime
+ SRCS
+ timerfd_settime.cpp
+ HDRS
+ ../timerfd_settime.h
+ DEPENDS
+ libc.hdr.types.struct_itimerspec
+ libc.src.__support.OSUtil.linux.syscall_wrappers.timerfd_settime
+ 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_gettime.cpp b/libc/src/sys/timerfd/linux/timerfd_gettime.cpp
new file mode 100644
index 00000000000000..76b2ef719b42f1
--- /dev/null
+++ b/libc/src/sys/timerfd/linux/timerfd_gettime.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_gettime.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/timerfd/timerfd_gettime.h"
+
+#include "hdr/types/struct_itimerspec.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/timerfd_gettime.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_gettime,
+ (int fd, struct itimerspec *curr_value)) {
+ ErrorOr<int> ret = linux_syscalls::timerfd_gettime(fd, curr_value);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return *ret;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/timerfd/linux/timerfd_settime.cpp b/libc/src/sys/timerfd/linux/timerfd_settime.cpp
new file mode 100644
index 00000000000000..16557c823f2a4d
--- /dev/null
+++ b/libc/src/sys/timerfd/linux/timerfd_settime.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_settime.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/timerfd/timerfd_settime.h"
+
+#include "hdr/types/struct_itimerspec.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.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_settime,
+ (int fd, int flags, const struct itimerspec *new_value,
+ struct itimerspec *old_value)) {
+ ErrorOr<int> ret =
+ linux_syscalls::timerfd_settime(fd, flags, new_value, old_value);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return *ret;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/timerfd/timerfd_gettime.h b/libc/src/sys/timerfd/timerfd_gettime.h
new file mode 100644
index 00000000000000..c42568b6909b92
--- /dev/null
+++ b/libc/src/sys/timerfd/timerfd_gettime.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_gettime.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_GETTIME_H
+#define LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_GETTIME_H
+
+#include "hdr/types/struct_itimerspec.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int timerfd_gettime(int fd, struct itimerspec *curr_value);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_GETTIME_H
diff --git a/libc/src/sys/timerfd/timerfd_settime.h b/libc/src/sys/timerfd/timerfd_settime.h
new file mode 100644
index 00000000000000..5a0ff0eb75d1e7
--- /dev/null
+++ b/libc/src/sys/timerfd/timerfd_settime.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
+/// Implementation header for timerfd_settime.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_SETTIME_H
+#define LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_SETTIME_H
+
+#include "hdr/types/struct_itimerspec.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int timerfd_settime(int fd, int flags, const struct itimerspec *new_value,
+ struct itimerspec *old_value);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_SETTIME_H
diff --git a/libc/test/src/sys/timerfd/linux/CMakeLists.txt b/libc/test/src/sys/timerfd/linux/CMakeLists.txt
index b8ba88008737e7..769dcf3e9b5879 100644
--- a/libc/test/src/sys/timerfd/linux/CMakeLists.txt
+++ b/libc/test/src/sys/timerfd/linux/CMakeLists.txt
@@ -21,3 +21,29 @@ add_libc_test(
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)
+
+add_libc_test(
+ timerfd_gettime_test
+ SUITE
+ libc-sys-timerfd-tests
+ SRCS
+ timerfd_gettime_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.stdint_proxy
+ libc.hdr.sys_timerfd_macros
+ libc.hdr.time_macros
+ libc.hdr.types.ssize_t
+ libc.hdr.types.struct_itimerspec
+ libc.hdr.types.struct_timespec
+ libc.hdr.types.time_t
+ libc.src.__support.CPP.scope
+ libc.src.__support.time.clock_gettime
+ libc.src.sys.timerfd.timerfd_create
+ libc.src.sys.timerfd.timerfd_gettime
+ libc.src.sys.timerfd.timerfd_settime
+ 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_gettime_test.cpp b/libc/test/src/sys/timerfd/linux/timerfd_gettime_test.cpp
new file mode 100644
index 00000000000000..741ce7705b1fc1
--- /dev/null
+++ b/libc/test/src/sys/timerfd/linux/timerfd_gettime_test.cpp
@@ -0,0 +1,159 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_gettime and timerfd_settime.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_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 "hdr/types/struct_itimerspec.h"
+#include "hdr/types/struct_timespec.h"
+#include "hdr/types/time_t.h"
+#include "src/__support/CPP/scope.h"
+#include "src/__support/time/clock_gettime.h"
+#include "src/sys/timerfd/timerfd_create.h"
+#include "src/sys/timerfd/timerfd_gettime.h"
+#include "src/sys/timerfd/timerfd_settime.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 LlvmLibcTimerfdGettimeTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+using NsecT = decltype(timespec::tv_nsec);
+
+TEST_F(LlvmLibcTimerfdGettimeTest, SetAndGetRelative) {
+ int fd;
+ ASSERT_THAT(fd = LIBC_NAMESPACE::timerfd_create(CLOCK_MONOTONIC,
+ TFD_CLOEXEC | TFD_NONBLOCK),
+ returns(GE(0)).with_errno(EQ(0)));
+ scope_exit close_fd(
+ [&] { EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); });
+
+ // Newly created timer should be disarmed.
+ struct itimerspec curr_val = {{1, 1}, {1, 1}};
+ ASSERT_THAT(LIBC_NAMESPACE::timerfd_gettime(fd, &curr_val), Succeeds(0));
+ EXPECT_EQ(curr_val.it_value.tv_sec, static_cast<time_t>(0));
+ EXPECT_EQ(curr_val.it_value.tv_nsec, static_cast<NsecT>(0));
+ EXPECT_EQ(curr_val.it_interval.tv_sec, static_cast<time_t>(0));
+ EXPECT_EQ(curr_val.it_interval.tv_nsec, static_cast<NsecT>(0));
+
+ // Arm the timer with a relative timeout and periodic interval.
+ struct itimerspec new_val = {};
+ new_val.it_value.tv_sec = 60;
+ new_val.it_value.tv_nsec = 0;
+ new_val.it_interval.tv_sec = 2;
+ new_val.it_interval.tv_nsec = 500000000;
+
+ struct itimerspec old_val = {{1, 1}, {1, 1}};
+ ASSERT_THAT(LIBC_NAMESPACE::timerfd_settime(fd, 0, &new_val, &old_val),
+ Succeeds(0));
+ EXPECT_EQ(old_val.it_value.tv_sec, static_cast<time_t>(0));
+ EXPECT_EQ(old_val.it_value.tv_nsec, static_cast<NsecT>(0));
+ EXPECT_EQ(old_val.it_interval.tv_sec, static_cast<time_t>(0));
+ EXPECT_EQ(old_val.it_interval.tv_nsec, static_cast<NsecT>(0));
+
+ ASSERT_THAT(LIBC_NAMESPACE::timerfd_gettime(fd, &curr_val), Succeeds(0));
+ EXPECT_TRUE(curr_val.it_value.tv_sec > 0 || curr_val.it_value.tv_nsec > 0);
+ EXPECT_LE(curr_val.it_value.tv_sec, static_cast<time_t>(60));
+ EXPECT_EQ(curr_val.it_interval.tv_sec, static_cast<time_t>(2));
+ EXPECT_EQ(curr_val.it_interval.tv_nsec, static_cast<NsecT>(500000000));
+
+ // Disarm the timer and verify old_value returns the previous setting.
+ struct itimerspec disarm_val = {};
+ ASSERT_THAT(LIBC_NAMESPACE::timerfd_settime(fd, 0, &disarm_val, &old_val),
+ Succeeds(0));
+ EXPECT_TRUE(old_val.it_value.tv_sec > 0 || old_val.it_value.tv_nsec > 0);
+ EXPECT_EQ(old_val.it_interval.tv_sec, static_cast<time_t>(2));
+ EXPECT_EQ(old_val.it_interval.tv_nsec, static_cast<NsecT>(500000000));
+
+ ASSERT_THAT(LIBC_NAMESPACE::timerfd_gettime(fd, &curr_val), Succeeds(0));
+ EXPECT_EQ(curr_val.it_value.tv_sec, static_cast<time_t>(0));
+ EXPECT_EQ(curr_val.it_value.tv_nsec, static_cast<NsecT>(0));
+ EXPECT_EQ(curr_val.it_interval.tv_sec, static_cast<time_t>(0));
+ EXPECT_EQ(curr_val.it_interval.tv_nsec, static_cast<NsecT>(0));
+}
+
+TEST_F(LlvmLibcTimerfdGettimeTest, SetAndGetAbsolute) {
+ int fd;
+ ASSERT_THAT(fd = LIBC_NAMESPACE::timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC),
+ returns(GE(0)).with_errno(EQ(0)));
+ scope_exit close_fd(
+ [&] { EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); });
+
+ struct timespec now = {};
+ auto clock_res =
+ LIBC_NAMESPACE::internal::clock_gettime(CLOCK_MONOTONIC, &now);
+ ASSERT_TRUE(clock_res.has_value());
+
+ struct itimerspec new_val = {};
+ new_val.it_value.tv_sec = now.tv_sec + 60;
+ new_val.it_value.tv_nsec = now.tv_nsec;
+ ASSERT_THAT(
+ LIBC_NAMESPACE::timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_val, nullptr),
+ Succeeds(0));
+
+ struct itimerspec curr_val = {};
+ ASSERT_THAT(LIBC_NAMESPACE::timerfd_gettime(fd, &curr_val), Succeeds(0));
+ EXPECT_TRUE(curr_val.it_value.tv_sec > 0 || curr_val.it_value.tv_nsec > 0);
+ EXPECT_LE(curr_val.it_value.tv_sec, static_cast<time_t>(60));
+ EXPECT_EQ(curr_val.it_interval.tv_sec, static_cast<time_t>(0));
+ EXPECT_EQ(curr_val.it_interval.tv_nsec, static_cast<NsecT>(0));
+
+ // Set an absolute one-shot timer in the past so it expires immediately.
+ struct itimerspec past_val = {};
+ past_val.it_value.tv_sec = 0;
+ past_val.it_value.tv_nsec = 1;
+ ASSERT_THAT(LIBC_NAMESPACE::timerfd_settime(fd, TFD_TIMER_ABSTIME, &past_val,
+ nullptr),
+ Succeeds(0));
+
+ uint64_t expirations = 0;
+ ASSERT_THAT(LIBC_NAMESPACE::read(fd, &expirations, sizeof(expirations)),
+ Succeeds<ssize_t>(sizeof(expirations)));
+ EXPECT_EQ(expirations, static_cast<uint64_t>(1));
+
+ // One-shot timer should be disarmed after expiring.
+ ASSERT_THAT(LIBC_NAMESPACE::timerfd_gettime(fd, &curr_val), Succeeds(0));
+ EXPECT_EQ(curr_val.it_value.tv_sec, static_cast<time_t>(0));
+ EXPECT_EQ(curr_val.it_value.tv_nsec, static_cast<NsecT>(0));
+}
+
+TEST_F(LlvmLibcTimerfdGettimeTest, InvalidArgs) {
+ struct itimerspec val = {};
+ EXPECT_THAT(LIBC_NAMESPACE::timerfd_gettime(-1, &val), Fails(EBADF));
+ EXPECT_THAT(LIBC_NAMESPACE::timerfd_settime(-1, 0, &val, nullptr),
+ Fails(EBADF));
+
+ int fd;
+ ASSERT_THAT(fd = LIBC_NAMESPACE::timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC),
+ returns(GE(0)).with_errno(EQ(0)));
+ scope_exit close_fd(
+ [&] { EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); });
+
+ EXPECT_THAT(LIBC_NAMESPACE::timerfd_settime(fd, -1, &val, nullptr),
+ Fails(EINVAL));
+
+ struct itimerspec bad_nsec = {};
+ bad_nsec.it_value.tv_sec = 1;
+ bad_nsec.it_value.tv_nsec = 1000000000;
+ EXPECT_THAT(LIBC_NAMESPACE::timerfd_settime(fd, 0, &bad_nsec, nullptr),
+ Fails(EINVAL));
+
+ bad_nsec.it_value.tv_nsec = -1;
+ EXPECT_THAT(LIBC_NAMESPACE::timerfd_settime(fd, 0, &bad_nsec, nullptr),
+ Fails(EINVAL));
+}
>From 3a682e066bc0925b3497d9e9829c408e82a8540b Mon Sep 17 00:00:00 2001
From: Alexey Samsonov <vonosmas at gmail.com>
Date: Wed, 18 Nov 2026 00:07:42 +0000
Subject: [PATCH 2/2] Fix comments for SYS_timerfd_gettime validation
---
.../OSUtil/linux/syscall_wrappers/timerfd_gettime.h | 5 ++---
.../OSUtil/linux/syscall_wrappers/timerfd_settime.h | 5 ++---
2 files changed, 4 insertions(+), 6 deletions(-)
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_gettime.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_gettime.h
index 7f7a7089cce1b0..d36f3525101fc8 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_gettime.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_gettime.h
@@ -35,9 +35,8 @@ LIBC_INLINE ErrorOr<int> timerfd_gettime(int fd,
#elif defined(SYS_timerfd_gettime)
static_assert(
sizeof(timespec::tv_nsec) == sizeof(long),
- "This legacy syscall fallback is only safe on platforms where tv_nsec "
- "matches the register size (long). It is unsafe on 32-bit platforms "
- "with 64-bit tv_nsec.");
+ "SYS_timerfd_gettime requires struct timespec::tv_nsec to match long; "
+ "use SYS_timerfd_gettime64 on 32-bit platforms with 64-bit time_t.");
return syscall_checked<int>(SYS_timerfd_gettime, fd, curr_value);
#else
#error "SYS_timerfd_gettime and SYS_timerfd_gettime64 syscalls not available."
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.h
index 8c9a5530a4dba4..028c3c70eba85d 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.h
@@ -37,9 +37,8 @@ LIBC_INLINE ErrorOr<int> timerfd_settime(int fd, int flags,
#elif defined(SYS_timerfd_settime)
static_assert(
sizeof(timespec::tv_nsec) == sizeof(long),
- "This legacy syscall fallback is only safe on platforms where tv_nsec "
- "matches the register size (long). It is unsafe on 32-bit platforms "
- "with 64-bit tv_nsec.");
+ "SYS_timerfd_settime requires struct timespec::tv_nsec to match long; "
+ "use SYS_timerfd_settime64 on 32-bit platforms with 64-bit time_t.");
return syscall_checked<int>(SYS_timerfd_settime, fd, flags, new_value,
old_value);
#else
More information about the libc-commits
mailing list