[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:33:50 PDT 2026
https://github.com/kaladron updated https://github.com/llvm/llvm-project/pull/204141
>From de6e46f99b26ce337902ec11fba3453559dfc748 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 wrapper for sysinfo and tests for
prlimit/sysinfo
---
libc/hdr/CMakeLists.txt | 9 ++++
libc/hdr/sys_resource_macros.h | 27 ++++++++++++
.../linux/syscall_wrappers/CMakeLists.txt | 11 +++++
.../OSUtil/linux/syscall_wrappers/sysinfo.h | 34 +++++++++++++++
.../src/__support/OSUtil/linux/CMakeLists.txt | 10 +++++
.../OSUtil/linux/syscall_wrappers_test.cpp | 41 +++++++++++++++++++
6 files changed, 132 insertions(+)
create mode 100644 libc/hdr/sys_resource_macros.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 731e5a1d462ab..a52be9676a3ca 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -774,3 +774,14 @@ add_header_library(
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/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..e438c25e9fbfc
--- /dev/null
+++ b/libc/test/src/__support/OSUtil/linux/syscall_wrappers_test.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "hdr/types/struct_rlimit.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/prlimit.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h"
+#include "test/UnitTest/Test.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);
+}
+
+TEST(LlvmLibcSyscallWrappersTest, PrlimitTest) {
+ struct rlimit limits;
+ auto result = linux_syscalls::prlimit(0, RLIMIT_NOFILE, nullptr, &limits);
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(result.value(), 0);
+ EXPECT_GT(limits.rlim_cur, static_cast<rlim_t>(0));
+
+ // Test invalid PID to verify error propagation (ESRCH)
+ auto result_err =
+ linux_syscalls::prlimit(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