[llvm-branch-commits] [libc] [llvm] [libc] Use a syscall wrapper for clock_settime (PR #238738)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 4 02:47:45 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Connector Switch (c8ef)
<details>
<summary>Changes</summary>
This PR move clock_settime behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.
---
Full diff: https://github.com/llvm/llvm-project/pull/238738.diff
5 Files Affected:
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+14)
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/clock_settime.h (+46)
- (modified) libc/src/__support/time/linux/CMakeLists.txt (+3-3)
- (modified) libc/src/__support/time/linux/clock_settime.cpp (+2-26)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+35)
``````````diff
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 21300c4d5b630..4c73e69bf905d 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1916,6 +1916,20 @@ add_header_library(
libc.src.__support.macros.config
)
+add_header_library(
+ clock_settime
+ HDRS
+ clock_settime.h
+ DEPENDS
+ libc.hdr.types.clockid_t
+ libc.hdr.types.struct_timespec
+ 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(
getrusage
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/clock_settime.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/clock_settime.h
new file mode 100644
index 0000000000000..7170163804fea
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/clock_settime.h
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 clock_settime syscall wrapper.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOCK_SETTIME_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOCK_SETTIME_H
+
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h>
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> clock_settime(clockid_t clockid, const timespec *ts) {
+#if defined(SYS_clock_settime64)
+ return syscall_checked<int>(SYS_clock_settime64, clockid, ts);
+#elif defined(SYS_clock_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_clock_settime, clockid, ts);
+#else
+#error "SYS_clock_settime and SYS_clock_settime64 syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOCK_SETTIME_H
diff --git a/libc/src/__support/time/linux/CMakeLists.txt b/libc/src/__support/time/linux/CMakeLists.txt
index 4a8be1d2db924..2d5d9f10faa44 100644
--- a/libc/src/__support/time/linux/CMakeLists.txt
+++ b/libc/src/__support/time/linux/CMakeLists.txt
@@ -22,10 +22,10 @@ add_object_library(
SRCS
clock_settime.cpp
DEPENDS
- libc.include.sys_syscall
- libc.hdr.types.struct_timespec
libc.hdr.types.clockid_t
+ libc.hdr.types.struct_timespec
+ libc.src.__support.OSUtil.linux.syscall_wrappers.clock_settime
libc.src.__support.common
libc.src.__support.error_or
- libc.src.__support.OSUtil.osutil
+ libc.src.__support.macros.config
)
diff --git a/libc/src/__support/time/linux/clock_settime.cpp b/libc/src/__support/time/linux/clock_settime.cpp
index ec96828b42352..47af1c5848615 100644
--- a/libc/src/__support/time/linux/clock_settime.cpp
+++ b/libc/src/__support/time/linux/clock_settime.cpp
@@ -9,39 +9,15 @@
#include "src/__support/time/clock_settime.h"
#include "hdr/types/clockid_t.h"
#include "hdr/types/struct_timespec.h"
-#include "src/__support/OSUtil/syscall.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/clock_settime.h"
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/macros/config.h"
-#include <sys/syscall.h>
-
-#if defined(SYS_clock_settime64)
-#include <linux/time_types.h>
-#endif
namespace LIBC_NAMESPACE_DECL {
namespace internal {
ErrorOr<int> clock_settime(clockid_t clockid, const timespec *ts) {
- int ret;
-#if defined(SYS_clock_settime64)
- ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_settime64,
- static_cast<long>(clockid),
- reinterpret_cast<long>(ts));
-#elif defined(SYS_clock_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.");
- ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_settime,
- static_cast<long>(clockid),
- reinterpret_cast<long>(ts));
-#else
-#error "SYS_clock_settime and SYS_clock_settime64 syscalls not available."
-#endif
- if (ret < 0)
- return Error(-ret);
- return ret;
+ return linux_syscalls::clock_settime(clockid, ts);
}
} // namespace internal
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index cb35dc10dd84c..e5c15197ed700 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4375,6 +4375,23 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_osutil_linux_syscall_wrappers_clock_settime",
+ hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/clock_settime.h"],
+ target_compatible_with = select({
+ "@platforms//os:linux": [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+ deps = [
+ ":__support_common",
+ ":__support_error_or",
+ ":__support_macros_config",
+ ":__support_osutil_syscall",
+ ":types_clockid_t",
+ ":types_struct_timespec",
+ ],
+)
+
libc_support_library(
name = "__support_osutil_linux_syscall_wrappers_close",
hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/close.h"],
@@ -5425,6 +5442,24 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_time_clock_settime",
+ srcs = ["src/__support/time/linux/clock_settime.cpp"],
+ hdrs = ["src/__support/time/clock_settime.h"],
+ target_compatible_with = select({
+ "@platforms//os:linux": [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+ deps = [
+ ":__support_common",
+ ":__support_error_or",
+ ":__support_macros_config",
+ ":__support_osutil_linux_syscall_wrappers_clock_settime",
+ ":types_clockid_t",
+ ":types_struct_timespec",
+ ],
+)
+
libc_support_library(
name = "__support_wctype_utils",
hdrs = ["src/__support/wctype_utils.h"],
``````````
</details>
https://github.com/llvm/llvm-project/pull/238738
More information about the llvm-branch-commits
mailing list