[libc-commits] [libc] [libc] Add syscall wrappers for prlimit64 and sysinfo (PR #204141)
Jeff Bailey via libc-commits
libc-commits at lists.llvm.org
Wed Jun 17 05:26:32 PDT 2026
https://github.com/kaladron updated https://github.com/llvm/llvm-project/pull/204141
>From b98ebb1f39adcf7a28661b3ac04cd90864b5a612 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] [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 36af7ffa898f1..f45361b860b1e 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -759,3 +759,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
More information about the libc-commits
mailing list