[libc-commits] [libc] [libc] Clean up sysconf implementation and tests (PR #204130)
Jeff Bailey via libc-commits
libc-commits at lists.llvm.org
Tue Jun 16 07:13:00 PDT 2026
https://github.com/kaladron updated https://github.com/llvm/llvm-project/pull/204130
>From 24562d1a6697d1908258f5cd370c074a863ebc7b Mon Sep 17 00:00:00 2001
From: Jeff Bailey <jbailey at raspberryginger.com>
Date: Tue, 16 Jun 2026 12:45:49 +0100
Subject: [PATCH 1/2] [libc] Add syscall wrappers for prlimit64 and sysinfo
Added inline syscall wrappers for prlimit64 and sysinfo under
src/__support/OSUtil/linux/syscall_wrappers/ and registered them in CMake.
Added unit tests to verify the syscall wrappers.
Assisted-by: Automated tooling, human reviewed.
---
libc/hdr/CMakeLists.txt | 9 ++++
libc/hdr/sys_resource_macros.h | 27 +++++++++++
.../linux/syscall_wrappers/CMakeLists.txt | 24 ++++++++++
.../OSUtil/linux/syscall_wrappers/prlimit64.h | 35 ++++++++++++++
.../OSUtil/linux/syscall_wrappers/sysinfo.h | 34 ++++++++++++++
.../src/__support/OSUtil/linux/CMakeLists.txt | 10 ++++
.../OSUtil/linux/syscall_wrappers_test.cpp | 47 +++++++++++++++++++
7 files changed, 186 insertions(+)
create mode 100644 libc/hdr/sys_resource_macros.h
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/prlimit64.h
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h
create mode 100644 libc/test/src/__support/OSUtil/linux/syscall_wrappers_test.cpp
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index 0bc0202022680..c24b826a6ef14 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -287,6 +287,15 @@ add_proxy_header_library(
libc.include.sys_auxv
)
+add_proxy_header_library(
+ sys_resource_macros
+ HDRS
+ sys_resource_macros.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-macros.sys_resource_macros
+ libc.include.sys_resource
+)
+
add_header_library(wchar_overlay HDRS wchar_overlay.h)
add_header_library(uchar_overlay HDRS uchar_overlay.h)
diff --git a/libc/hdr/sys_resource_macros.h b/libc/hdr/sys_resource_macros.h
new file mode 100644
index 0000000000000..845584bfeb714
--- /dev/null
+++ b/libc/hdr/sys_resource_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
+/// Definition of macros from sys/resource.h
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_SYS_RESOURCE_MACROS_H
+#define LLVM_LIBC_HDR_SYS_RESOURCE_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/sys-resource-macros.h"
+
+#else // Overlay mode
+
+#include <sys/resource.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_SYS_RESOURCE_MACROS_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 97848b1848660..8c109a6b9b110 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -745,3 +745,27 @@ add_header_library(
libc.hdr.types.off_t
libc.include.sys_syscall
)
+
+add_header_library(
+ prlimit64
+ HDRS
+ prlimit64.h
+ DEPENDS
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.include.sys_syscall
+)
+
+add_header_library(
+ sysinfo
+ HDRS
+ sysinfo.h
+ DEPENDS
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.include.sys_syscall
+)
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/prlimit64.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/prlimit64.h
new file mode 100644
index 0000000000000..b2bf99d4bd774
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/prlimit64.h
@@ -0,0 +1,35 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 prlimit64.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PRLIMIT64_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PRLIMIT64_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> prlimit64(long pid, int resource,
+ const void *new_limit, void *old_limit) {
+ return syscall_checked<int>(SYS_prlimit64, pid, resource, new_limit,
+ old_limit);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PRLIMIT64_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h
new file mode 100644
index 0000000000000..7487d05353c5b
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h
@@ -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
+/// Syscall wrapper for sysinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SYSINFO_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SYSINFO_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 <linux/sysinfo.h> // For struct sysinfo
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> sysinfo(struct ::sysinfo *info) {
+ return syscall_checked<int>(SYS_sysinfo, info);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SYSINFO_H
diff --git a/libc/test/src/__support/OSUtil/linux/CMakeLists.txt b/libc/test/src/__support/OSUtil/linux/CMakeLists.txt
index 8e6958941f289..281eaf7c26adc 100644
--- a/libc/test/src/__support/OSUtil/linux/CMakeLists.txt
+++ b/libc/test/src/__support/OSUtil/linux/CMakeLists.txt
@@ -32,3 +32,13 @@ add_libc_test(
libc.src.signal.sigaction
libc.src.signal.raise
)
+
+add_libc_test(
+ syscall_wrappers_test
+ SUITE libc-osutil-tests
+ SRCS syscall_wrappers_test.cpp
+ DEPENDS
+ libc.src.__support.OSUtil.linux.syscall_wrappers.prlimit64
+ libc.src.__support.OSUtil.linux.syscall_wrappers.sysinfo
+ libc.hdr.sys_resource_macros
+)
diff --git a/libc/test/src/__support/OSUtil/linux/syscall_wrappers_test.cpp b/libc/test/src/__support/OSUtil/linux/syscall_wrappers_test.cpp
new file mode 100644
index 0000000000000..349e0fed3f0f9
--- /dev/null
+++ b/libc/test/src/__support/OSUtil/linux/syscall_wrappers_test.cpp
@@ -0,0 +1,47 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/sys_resource_macros.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/prlimit64.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h"
+#include "test/UnitTest/Test.h"
+#include <stdint.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+TEST(LlvmLibcSyscallWrappersTest, SysinfoTest) {
+ struct ::sysinfo info;
+ auto result = linux_syscalls::sysinfo(&info);
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(result.value(), 0);
+ EXPECT_GT(info.uptime, 0L);
+ EXPECT_GT(info.totalram, 0UL);
+}
+
+// Local structure definition to match the syscall layout on all platforms
+struct rlimit64 {
+ uint64_t rlim_cur;
+ uint64_t rlim_max;
+};
+
+TEST(LlvmLibcSyscallWrappersTest, Prlimit64Test) {
+ struct rlimit64 limits;
+ auto result = linux_syscalls::prlimit64(0, RLIMIT_NOFILE, nullptr, &limits);
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(result.value(), 0);
+ EXPECT_GT(limits.rlim_cur, uint64_t(0));
+
+ // Test invalid PID to verify error propagation (ESRCH)
+ auto result_err =
+ linux_syscalls::prlimit64(99999999, RLIMIT_NOFILE, nullptr, &limits);
+ ASSERT_FALSE(result_err.has_value());
+ EXPECT_EQ(result_err.error(), ESRCH);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
>From a670ee277317c0dbd2202554f146cd5b3d2da25c Mon Sep 17 00:00:00 2001
From: Jeff Bailey <jbailey at raspberryginger.com>
Date: Tue, 16 Jun 2026 12:40:56 +0100
Subject: [PATCH 2/2] [libc] Clean up sysconf implementation and tests
Refactored sysconf.cpp to use a switch-case block instead of an if-else chain.
Delegated the logic for existing options to helper functions (get_page_size,
get_nprocessors_conf, get_nprocessors_onln) in an anonymous namespace to
reduce cognitive complexity.
Updated file headers in sysconf.cpp and sysconf_test.cpp to the standard
three-section format.
Modernized suffix literals in sysconf_test.cpp.
Updated RLIM_INFINITY definition in sys-resource-macros.h to be 64-bit
safe (~0ULL) to prevent comparison failures on 32-bit platforms where
rlim_t is 64-bit but RLIM_INFINITY was 32-bit.
Assisted-by: Automated tooling, human reviewed.
---
.../linux/sys-resource-macros.h | 2 +-
libc/src/unistd/linux/sysconf.cpp | 58 ++++++++++++-------
libc/test/src/unistd/sysconf_test.cpp | 13 +++--
3 files changed, 48 insertions(+), 25 deletions(-)
diff --git a/libc/include/llvm-libc-macros/linux/sys-resource-macros.h b/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
index c9d93c30c35a4..ff7eb42a5ba6c 100644
--- a/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
@@ -26,6 +26,6 @@
#define RLIMIT_RTPRIO 14
#define RLIMIT_RTTIME 15
-#define RLIM_INFINITY (~0UL)
+#define RLIM_INFINITY (~0ULL)
#endif // LLVM_LIBC_MACROS_LINUX_SYS_RESOURCE_MACROS_H
diff --git a/libc/src/unistd/linux/sysconf.cpp b/libc/src/unistd/linux/sysconf.cpp
index dff64cb8140d3..6b808150ab83d 100644
--- a/libc/src/unistd/linux/sysconf.cpp
+++ b/libc/src/unistd/linux/sysconf.cpp
@@ -1,10 +1,15 @@
-//===-- Linux implementation of sysconf -----------------------------------===//
+//===----------------------------------------------------------------------===//
//
// 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 sysconf
+///
+//===----------------------------------------------------------------------===//
#include "src/unistd/sysconf.h"
@@ -19,29 +24,42 @@
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(long, sysconf, (int name)) {
- if (name == _SC_PAGESIZE) {
- cpp::optional<unsigned long> page_size = auxv::get(AT_PAGESZ);
- if (page_size)
- return static_cast<long>(*page_size);
- libc_errno = EINVAL;
- return -1;
- }
+namespace { // Anonymous namespace for internal helpers
+
+long get_page_size() {
+ cpp::optional<unsigned long> page_size = auxv::get(AT_PAGESZ);
+ if (page_size)
+ return static_cast<long>(*page_size);
+ libc_errno = EINVAL;
+ return -1;
+}
- if (name == _SC_NPROCESSORS_CONF)
- return static_cast<long>(
- sysinfo::parse_nproc_with_fallback_from(sysinfo::POSSIBLE_NPROC_PATH));
+long get_nprocessors_conf() {
+ return static_cast<long>(
+ sysinfo::parse_nproc_with_fallback_from(sysinfo::POSSIBLE_NPROC_PATH));
+}
- if (name == _SC_NPROCESSORS_ONLN)
- return static_cast<long>(
- sysinfo::parse_nproc_with_fallback_from(sysinfo::ONLINE_NPROC_PATH));
+long get_nprocessors_onln() {
+ return static_cast<long>(
+ sysinfo::parse_nproc_with_fallback_from(sysinfo::ONLINE_NPROC_PATH));
+}
- if (name == _SC_THREADS)
- return _POSIX_THREADS;
+} // anonymous namespace
- // TODO: Complete the rest of the sysconf options.
- libc_errno = EINVAL;
- return -1;
+LLVM_LIBC_FUNCTION(long, sysconf, (int name)) {
+ switch (name) {
+ case _SC_PAGESIZE:
+ return get_page_size();
+ case _SC_NPROCESSORS_CONF:
+ return get_nprocessors_conf();
+ case _SC_NPROCESSORS_ONLN:
+ return get_nprocessors_onln();
+ case _SC_THREADS:
+ return _POSIX_THREADS;
+ default:
+ libc_errno = EINVAL;
+ return -1;
+ }
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/unistd/sysconf_test.cpp b/libc/test/src/unistd/sysconf_test.cpp
index 0c37eef13b283..16882714cdf1c 100644
--- a/libc/test/src/unistd/sysconf_test.cpp
+++ b/libc/test/src/unistd/sysconf_test.cpp
@@ -1,10 +1,15 @@
-//===-- Unittests for sysconf ---------------------------------------------===//
+//===----------------------------------------------------------------------===//
//
// 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
+/// Unittests for sysconf
+///
+//===----------------------------------------------------------------------===//
#include "src/unistd/sysconf.h"
#include "test/UnitTest/Test.h"
@@ -13,17 +18,17 @@
TEST(LlvmLibcSysconfTest, PagesizeTest) {
long pagesize = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
- ASSERT_GT(pagesize, 0l);
+ ASSERT_GT(pagesize, 0L);
}
TEST(LlvmLibcSysconfTest, NprocessorsConfTest) {
long sysconf_count = LIBC_NAMESPACE::sysconf(_SC_NPROCESSORS_CONF);
- ASSERT_GT(sysconf_count, 0l);
+ ASSERT_GT(sysconf_count, 0L);
}
TEST(LlvmLibcSysconfTest, NprocessorsOnlnTest) {
long sysconf_count = LIBC_NAMESPACE::sysconf(_SC_NPROCESSORS_ONLN);
- ASSERT_GT(sysconf_count, 0l);
+ ASSERT_GT(sysconf_count, 0L);
}
TEST(LlvmLibcSysconfTest, ThreadsTest) {
More information about the libc-commits
mailing list