[libc-commits] [libc] [libc] Implement sysinfo (PR #217471)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Fri Aug 21 11:15:17 PDT 2026


https://github.com/michaelrj-google updated https://github.com/llvm/llvm-project/pull/217471

>From 305baf61468f19cfca6b73899f8f183a74f37ae8 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Wed, 19 Aug 2026 21:46:29 +0000
Subject: [PATCH 1/3] [libc] Implement sysinfo

Adds the public sysinfo function and tests. The internal wrapper already
existed. I decided to avoid defining `struct sysinfo` since it's a
kernel struct defined in the UAPI headers. Can revisit in future if
necessary.

Assisted-by: Automated tooling, human reviewed.
---
 libc/config/linux/aarch64/entrypoints.txt     |  3 ++
 libc/config/linux/aarch64/headers.txt         |  1 +
 libc/config/linux/arm/entrypoints.txt         |  3 ++
 libc/config/linux/arm/headers.txt             |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  3 ++
 libc/config/linux/riscv/headers.txt           |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  3 ++
 libc/config/linux/x86_64/headers.txt          |  1 +
 libc/hdr/types/CMakeLists.txt                 |  9 ++++
 libc/hdr/types/struct_sysinfo.h               | 27 ++++++++++
 libc/include/CMakeLists.txt                   |  9 ++++
 libc/include/llvm-libc-types/CMakeLists.txt   |  1 +
 libc/include/llvm-libc-types/struct_sysinfo.h | 22 ++++++++
 libc/include/sys/sysinfo.yaml                 | 12 +++++
 .../linux/syscall_wrappers/CMakeLists.txt     |  1 +
 .../OSUtil/linux/syscall_wrappers/sysinfo.h   |  6 +--
 libc/src/sys/CMakeLists.txt                   |  1 +
 libc/src/sys/sysinfo/CMakeLists.txt           | 10 ++++
 libc/src/sys/sysinfo/linux/CMakeLists.txt     | 11 ++++
 libc/src/sys/sysinfo/linux/sysinfo.cpp        | 33 ++++++++++++
 libc/src/sys/sysinfo/sysinfo.h                | 26 ++++++++++
 libc/src/unistd/linux/CMakeLists.txt          |  1 +
 libc/src/unistd/linux/sysconf.cpp             |  2 +-
 libc/test/src/sys/CMakeLists.txt              |  1 +
 libc/test/src/sys/sysinfo/CMakeLists.txt      | 17 +++++++
 libc/test/src/sys/sysinfo/sysinfo_test.cpp    | 51 +++++++++++++++++++
 26 files changed, 252 insertions(+), 4 deletions(-)
 create mode 100644 libc/hdr/types/struct_sysinfo.h
 create mode 100644 libc/include/llvm-libc-types/struct_sysinfo.h
 create mode 100644 libc/include/sys/sysinfo.yaml
 create mode 100644 libc/src/sys/sysinfo/CMakeLists.txt
 create mode 100644 libc/src/sys/sysinfo/linux/CMakeLists.txt
 create mode 100644 libc/src/sys/sysinfo/linux/sysinfo.cpp
 create mode 100644 libc/src/sys/sysinfo/sysinfo.h
 create mode 100644 libc/test/src/sys/sysinfo/CMakeLists.txt
 create mode 100644 libc/test/src/sys/sysinfo/sysinfo_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 3af5943717e84..1a1b09c526192 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -349,6 +349,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.statvfs.fstatvfs
     libc.src.sys.statvfs.statvfs
 
+    # sys/sysinfo.h entrypoints
+    libc.src.sys.sysinfo.sysinfo
+
     # sys/utsname.h entrypoints
     libc.src.sys.utsname.uname
 
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index ccc6cfd38fd6e..b2346c568c986 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -65,6 +65,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.sys_stat
     libc.include.sys_statfs
     libc.include.sys_statvfs
+    libc.include.sys_sysinfo
     libc.include.sys_syscall
     libc.include.sys_sysmacros
     libc.include.sys_time
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index b6a24f4488089..124bc861a25d4 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -206,6 +206,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # sys/prctl.h entrypoints
     libc.src.sys.prctl.prctl
 
+    # sys/sysinfo.h entrypoints
+    libc.src.sys.sysinfo.sysinfo
+
     # sys/personality.h entrypoints
     libc.src.sys.personality.personality
 
diff --git a/libc/config/linux/arm/headers.txt b/libc/config/linux/arm/headers.txt
index 6ec36bc7f3511..f8426f32a3bfc 100644
--- a/libc/config/linux/arm/headers.txt
+++ b/libc/config/linux/arm/headers.txt
@@ -28,6 +28,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.wctype
     libc.include.sys_param
     libc.include.sys_personality
+    libc.include.sys_sysinfo
     libc.include.sys_unistd
     # Disabled due to epoll_wait syscalls not being available on this platform.
     # libc.include.sys_epoll
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 2a40f55076178..fad2d7ca08091 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -383,6 +383,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # sys/time.h entrypoints
     libc.src.sys.time.utimes
 
+    # sys/sysinfo.h entrypoints
+    libc.src.sys.sysinfo.sysinfo
+
     # sys/utsname.h entrypoints
     libc.src.sys.utsname.uname
 
diff --git a/libc/config/linux/riscv/headers.txt b/libc/config/linux/riscv/headers.txt
index 54ae65b6943eb..3677ecd2b657d 100644
--- a/libc/config/linux/riscv/headers.txt
+++ b/libc/config/linux/riscv/headers.txt
@@ -66,6 +66,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.sys_stat
     libc.include.sys_statfs
     libc.include.sys_statvfs
+    libc.include.sys_sysinfo
     libc.include.sys_syscall
     libc.include.sys_time
     libc.include.sys_types
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index cef595fc67465..2bfcc711fec15 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -388,6 +388,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # sys/time.h entrypoints
     libc.src.sys.time.utimes
 
+    # sys/sysinfo.h entrypoints
+    libc.src.sys.sysinfo.sysinfo
+
     # sys/utsname.h entrypoints
     libc.src.sys.utsname.uname
 
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index 475883a18165f..ee41a988d1d7f 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -68,6 +68,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.sys_stat
     libc.include.sys_statfs
     libc.include.sys_statvfs
+    libc.include.sys_sysinfo
     libc.include.sys_syscall
     libc.include.sys_sysmacros
     libc.include.sys_time
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 4983109aa82f8..e182f2dc71142 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -711,6 +711,15 @@ add_proxy_header_library(
     libc.include.netinet_udp
 )
 
+add_proxy_header_library(
+  struct_sysinfo
+  HDRS
+    struct_sysinfo.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.struct_sysinfo
+    libc.include.sys_sysinfo
+)
+
 add_proxy_header_library(
   struct_utsname
   HDRS
diff --git a/libc/hdr/types/struct_sysinfo.h b/libc/hdr/types/struct_sysinfo.h
new file mode 100644
index 0000000000000..bd77ec127f145
--- /dev/null
+++ b/libc/hdr/types/struct_sysinfo.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
+/// Proxy for struct sysinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_SYSINFO_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_SYSINFO_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_sysinfo.h"
+
+#else // Overlay mode
+
+#include <sys/sysinfo.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_SYSINFO_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 91d601d50a711..dd7e0d4fdbbe5 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -956,6 +956,15 @@ add_header_macro(
     .llvm-libc-types.ushort
 )
 
+add_header_macro(
+  sys_sysinfo
+  ../libc/include/sys/sysinfo.yaml
+  sys/sysinfo.h
+  DEPENDS
+    .llvm_libc_common_h
+    .llvm-libc-types.struct_sysinfo
+)
+
 add_header_macro(
   sys_utsname
   ../libc/include/sys/utsname.yaml
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 1c999bfd0b857..bb871f505a606 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -214,6 +214,7 @@ add_header(
 )
 add_header(struct_tm HDR struct_tm.h)
 add_header(struct_passwd HDR struct_passwd.h DEPENDS .uid_t .gid_t)
+add_header(struct_sysinfo HDR struct_sysinfo.h)
 add_header(struct_utsname HDR struct_utsname.h)
 add_header(thrd_start_t HDR thrd_start_t.h)
 add_header(thrd_t HDR thrd_t.h DEPENDS .__thread_type)
diff --git a/libc/include/llvm-libc-types/struct_sysinfo.h b/libc/include/llvm-libc-types/struct_sysinfo.h
new file mode 100644
index 0000000000000..408d2f705883e
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_sysinfo.h
@@ -0,0 +1,22 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 struct sysinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_STRUCT_SYSINFO_H
+#define LLVM_LIBC_TYPES_STRUCT_SYSINFO_H
+
+// Kernel struct defined in the UAPI headers. Include it instead of defining it
+// ourselves.
+
+#include <linux/sysinfo.h>
+
+#endif // LLVM_LIBC_TYPES_STRUCT_SYSINFO_H
diff --git a/libc/include/sys/sysinfo.yaml b/libc/include/sys/sysinfo.yaml
new file mode 100644
index 0000000000000..fb621c4d88195
--- /dev/null
+++ b/libc/include/sys/sysinfo.yaml
@@ -0,0 +1,12 @@
+header: sys/sysinfo.h
+standards:
+  - linux
+types:
+  - type_name: struct_sysinfo
+functions:
+  - name: sysinfo
+    standards:
+      - linux
+    return_type: int
+    arguments:
+      - type: struct sysinfo *
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 997e199460ab7..7d536e4b91abb 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -894,6 +894,7 @@ add_header_library(
   HDRS
     sysinfo.h
   DEPENDS
+    libc.hdr.types.struct_sysinfo
     libc.src.__support.OSUtil.osutil
     libc.src.__support.common
     libc.src.__support.error_or
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h
index 7487d05353c5b..ef2b7a3f582e5 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h
@@ -14,17 +14,17 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SYSINFO_H
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SYSINFO_H
 
+#include "hdr/types/struct_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
+#include <sys/syscall.h> // For syscall numbers
 
 namespace LIBC_NAMESPACE_DECL {
 namespace linux_syscalls {
 
-LIBC_INLINE ErrorOr<int> sysinfo(struct ::sysinfo *info) {
+LIBC_INLINE ErrorOr<int> sysinfo(struct sysinfo *info) {
   return syscall_checked<int>(SYS_sysinfo, info);
 }
 
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index 5917b91b33eaa..4249f72b4d78a 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -20,3 +20,4 @@ add_subdirectory(ptrace)
 add_subdirectory(uio)
 add_subdirectory(ioctl)
 add_subdirectory(sysmacros)
+add_subdirectory(sysinfo)
diff --git a/libc/src/sys/sysinfo/CMakeLists.txt b/libc/src/sys/sysinfo/CMakeLists.txt
new file mode 100644
index 0000000000000..eb19566892ecc
--- /dev/null
+++ b/libc/src/sys/sysinfo/CMakeLists.txt
@@ -0,0 +1,10 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+  sysinfo
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sysinfo
+)
diff --git a/libc/src/sys/sysinfo/linux/CMakeLists.txt b/libc/src/sys/sysinfo/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..c29252482cc90
--- /dev/null
+++ b/libc/src/sys/sysinfo/linux/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_entrypoint_object(
+  sysinfo
+  SRCS
+    sysinfo.cpp
+  HDRS
+    ../sysinfo.h
+  DEPENDS
+    libc.hdr.types.struct_sysinfo
+    libc.src.__support.OSUtil.linux.syscall_wrappers.sysinfo
+    libc.src.errno.errno
+)
diff --git a/libc/src/sys/sysinfo/linux/sysinfo.cpp b/libc/src/sys/sysinfo/linux/sysinfo.cpp
new file mode 100644
index 0000000000000..1b9504e99f1a5
--- /dev/null
+++ b/libc/src/sys/sysinfo/linux/sysinfo.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 sysinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/sysinfo/sysinfo.h"
+
+#include "hdr/types/struct_sysinfo.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/sysinfo.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, sysinfo, (struct sysinfo * info)) {
+  auto result = linux_syscalls::sysinfo(info);
+  if (!result) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/sysinfo/sysinfo.h b/libc/src/sys/sysinfo/sysinfo.h
new file mode 100644
index 0000000000000..154243823a3e3
--- /dev/null
+++ b/libc/src/sys/sysinfo/sysinfo.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 sysinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_SYSINFO_SYSINFO_H
+#define LLVM_LIBC_SRC_SYS_SYSINFO_SYSINFO_H
+
+#include "hdr/types/struct_sysinfo.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int sysinfo(struct sysinfo *info);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_SYSINFO_SYSINFO_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 7a8b51e9514aa..a04c43ce784d5 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -606,6 +606,7 @@ add_entrypoint_object(
     libc.hdr.sys_auxv_macros
     libc.hdr.sys_resource_macros
     libc.hdr.types.struct_rlimit
+    libc.hdr.types.struct_sysinfo
     libc.src.__support.libc_errno
     libc.src.__support.macros.config
     libc.src.__support.OSUtil.linux.sysinfo
diff --git a/libc/src/unistd/linux/sysconf.cpp b/libc/src/unistd/linux/sysconf.cpp
index 4785a8bac478d..7382e513d9c9f 100644
--- a/libc/src/unistd/linux/sysconf.cpp
+++ b/libc/src/unistd/linux/sysconf.cpp
@@ -18,6 +18,7 @@
 #include "hdr/sys_auxv_macros.h"
 #include "hdr/sys_resource_macros.h"
 #include "hdr/types/struct_rlimit.h"
+#include "hdr/types/struct_sysinfo.h"
 #include "hdr/unistd_macros.h"
 #include "src/__support/OSUtil/linux/auxv.h"
 #include "src/__support/OSUtil/linux/syscall_wrappers/prlimit.h"
@@ -26,7 +27,6 @@
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
 #include <linux/limits.h>
-#include <linux/sysinfo.h>
 
 // In overlay mode, system headers (like glibc's <bits/local_lim.h>) may
 // explicitly undefine ARG_MAX to indicate it is dynamic. We define a fallback
diff --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index 75b733d1c525c..17bf10f2b3d07 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -19,3 +19,4 @@ add_subdirectory(time)
 add_subdirectory(ioctl)
 add_subdirectory(sem)
 add_subdirectory(sysmacros)
+add_subdirectory(sysinfo)
diff --git a/libc/test/src/sys/sysinfo/CMakeLists.txt b/libc/test/src/sys/sysinfo/CMakeLists.txt
new file mode 100644
index 0000000000000..e975f58a7505e
--- /dev/null
+++ b/libc/test/src/sys/sysinfo/CMakeLists.txt
@@ -0,0 +1,17 @@
+add_custom_target(libc_sys_sysinfo_unittests)
+
+add_libc_test(
+  sysinfo_test
+  SUITE
+    libc_sys_sysinfo_unittests
+  SRCS
+    sysinfo_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.types.struct_sysinfo
+    libc.include.sys_sysinfo
+    libc.src.errno.errno
+    libc.src.__support.common
+    libc.src.sys.sysinfo.sysinfo
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
diff --git a/libc/test/src/sys/sysinfo/sysinfo_test.cpp b/libc/test/src/sys/sysinfo/sysinfo_test.cpp
new file mode 100644
index 0000000000000..1aa2482397b43
--- /dev/null
+++ b/libc/test/src/sys/sysinfo/sysinfo_test.cpp
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 sysinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/struct_sysinfo.h"
+#include "src/sys/sysinfo/sysinfo.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+
+TEST(LlvmLibcSysinfoTest, ValidBuffer) {
+  struct sysinfo info;
+  ASSERT_THAT(LIBC_NAMESPACE::sysinfo(&info), Succeeds(0));
+
+  EXPECT_GT(info.uptime, 0l);
+  EXPECT_GE(info.loads[0], 0ul);
+  EXPECT_GE(info.loads[1], 0ul);
+  EXPECT_GE(info.loads[2], 0ul);
+  EXPECT_GT(info.totalram, 0ul);
+  EXPECT_GT(info.freeram, 0ul);
+  EXPECT_LE(info.freeram, info.totalram);
+  EXPECT_GE(info.sharedram, 0ul);
+  EXPECT_LE(info.sharedram, info.totalram);
+  EXPECT_GE(info.bufferram, 0ul);
+  EXPECT_LE(info.bufferram, info.totalram);
+  EXPECT_GE(info.totalswap, 0ul);
+  EXPECT_GE(info.freeswap, 0ul);
+  EXPECT_LE(info.freeswap, info.totalswap);
+  EXPECT_GT(info.procs, static_cast<unsigned short>(0));
+  EXPECT_GE(info.pad, static_cast<unsigned short>(0));
+  EXPECT_GE(info.totalhigh, 0ul);
+  EXPECT_GE(info.freehigh, 0ul);
+  EXPECT_LE(info.freehigh, info.totalhigh);
+  EXPECT_GT(info.mem_unit, 0u);
+}
+
+TEST(LlvmLibcSysinfoTest, NullptrBuffer) {
+  EXPECT_THAT(LIBC_NAMESPACE::sysinfo(nullptr), Fails(EFAULT));
+}

>From 690fb86cdd071b86e3645af71608d316b0cc0709 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Thu, 20 Aug 2026 21:32:10 +0000
Subject: [PATCH 2/3] address comments

---
 libc/include/llvm-libc-types/CMakeLists.txt   |  6 +++++-
 .../llvm-libc-types/linux/CMakeLists.txt      |  6 ++++++
 .../llvm-libc-types/linux/struct_sysinfo.h    | 19 +++++++++++++++++++
 libc/include/llvm-libc-types/struct_sysinfo.h |  7 +++----
 libc/include/sys/sysinfo.yaml                 |  2 --
 libc/test/src/sys/sysinfo/sysinfo_test.cpp    | 18 +-----------------
 6 files changed, 34 insertions(+), 24 deletions(-)
 create mode 100644 libc/include/llvm-libc-types/linux/struct_sysinfo.h

diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index bb871f505a606..fb6991ff53a6e 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -214,7 +214,11 @@ add_header(
 )
 add_header(struct_tm HDR struct_tm.h)
 add_header(struct_passwd HDR struct_passwd.h DEPENDS .uid_t .gid_t)
-add_header(struct_sysinfo HDR struct_sysinfo.h)
+if(TARGET libc.include.llvm-libc-types.${LIBC_TARGET_OS}.struct_sysinfo)
+  add_header(struct_sysinfo HDR struct_sysinfo.h DEPENDS .${LIBC_TARGET_OS}.struct_sysinfo)
+else()
+  add_header(struct_sysinfo HDR struct_sysinfo.h)
+endif()
 add_header(struct_utsname HDR struct_utsname.h)
 add_header(thrd_start_t HDR thrd_start_t.h)
 add_header(thrd_t HDR thrd_t.h DEPENDS .__thread_type)
diff --git a/libc/include/llvm-libc-types/linux/CMakeLists.txt b/libc/include/llvm-libc-types/linux/CMakeLists.txt
index ccb986a7b54c8..d937ffe30e6e8 100644
--- a/libc/include/llvm-libc-types/linux/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/linux/CMakeLists.txt
@@ -3,3 +3,9 @@ add_header(
   HDR
     loff_t.h
 )
+
+add_header(
+  struct_sysinfo
+  HDR
+    struct_sysinfo.h
+)
diff --git a/libc/include/llvm-libc-types/linux/struct_sysinfo.h b/libc/include/llvm-libc-types/linux/struct_sysinfo.h
new file mode 100644
index 0000000000000..a8bcc93facfab
--- /dev/null
+++ b/libc/include/llvm-libc-types/linux/struct_sysinfo.h
@@ -0,0 +1,19 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 struct sysinfo for Linux.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_LINUX_STRUCT_SYSINFO_H
+#define LLVM_LIBC_TYPES_LINUX_STRUCT_SYSINFO_H
+
+#include <linux/sysinfo.h>
+
+#endif // LLVM_LIBC_TYPES_LINUX_STRUCT_SYSINFO_H
diff --git a/libc/include/llvm-libc-types/struct_sysinfo.h b/libc/include/llvm-libc-types/struct_sysinfo.h
index 408d2f705883e..996ddd1423f00 100644
--- a/libc/include/llvm-libc-types/struct_sysinfo.h
+++ b/libc/include/llvm-libc-types/struct_sysinfo.h
@@ -14,9 +14,8 @@
 #ifndef LLVM_LIBC_TYPES_STRUCT_SYSINFO_H
 #define LLVM_LIBC_TYPES_STRUCT_SYSINFO_H
 
-// Kernel struct defined in the UAPI headers. Include it instead of defining it
-// ourselves.
-
-#include <linux/sysinfo.h>
+#if defined(__linux__)
+#include "linux/struct_sysinfo.h"
+#endif
 
 #endif // LLVM_LIBC_TYPES_STRUCT_SYSINFO_H
diff --git a/libc/include/sys/sysinfo.yaml b/libc/include/sys/sysinfo.yaml
index fb621c4d88195..77533eb7b02b8 100644
--- a/libc/include/sys/sysinfo.yaml
+++ b/libc/include/sys/sysinfo.yaml
@@ -5,8 +5,6 @@ types:
   - type_name: struct_sysinfo
 functions:
   - name: sysinfo
-    standards:
-      - linux
     return_type: int
     arguments:
       - type: struct sysinfo *
diff --git a/libc/test/src/sys/sysinfo/sysinfo_test.cpp b/libc/test/src/sys/sysinfo/sysinfo_test.cpp
index 1aa2482397b43..48af165dd4fe0 100644
--- a/libc/test/src/sys/sysinfo/sysinfo_test.cpp
+++ b/libc/test/src/sys/sysinfo/sysinfo_test.cpp
@@ -25,25 +25,9 @@ TEST(LlvmLibcSysinfoTest, ValidBuffer) {
   ASSERT_THAT(LIBC_NAMESPACE::sysinfo(&info), Succeeds(0));
 
   EXPECT_GT(info.uptime, 0l);
-  EXPECT_GE(info.loads[0], 0ul);
-  EXPECT_GE(info.loads[1], 0ul);
-  EXPECT_GE(info.loads[2], 0ul);
   EXPECT_GT(info.totalram, 0ul);
-  EXPECT_GT(info.freeram, 0ul);
-  EXPECT_LE(info.freeram, info.totalram);
-  EXPECT_GE(info.sharedram, 0ul);
-  EXPECT_LE(info.sharedram, info.totalram);
-  EXPECT_GE(info.bufferram, 0ul);
-  EXPECT_LE(info.bufferram, info.totalram);
-  EXPECT_GE(info.totalswap, 0ul);
-  EXPECT_GE(info.freeswap, 0ul);
-  EXPECT_LE(info.freeswap, info.totalswap);
-  EXPECT_GT(info.procs, static_cast<unsigned short>(0));
-  EXPECT_GE(info.pad, static_cast<unsigned short>(0));
-  EXPECT_GE(info.totalhigh, 0ul);
-  EXPECT_GE(info.freehigh, 0ul);
-  EXPECT_LE(info.freehigh, info.totalhigh);
   EXPECT_GT(info.mem_unit, 0u);
+  EXPECT_GT(info.procs, static_cast<unsigned short>(0));
 }
 
 TEST(LlvmLibcSysinfoTest, NullptrBuffer) {

>From 7d0268d71d41cbbf75d3450b575b07885369f3a3 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Fri, 21 Aug 2026 18:14:55 +0000
Subject: [PATCH 3/3] disable to unblock QEMU tests

---
 libc/config/linux/arm/entrypoints.txt   | 3 ++-
 libc/config/linux/riscv/entrypoints.txt | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 124bc861a25d4..b493e26717186 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -207,7 +207,8 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.prctl.prctl
 
     # sys/sysinfo.h entrypoints
-    libc.src.sys.sysinfo.sysinfo
+    # Disabled due to QEMU test failures.
+    # libc.src.sys.sysinfo.sysinfo
 
     # sys/personality.h entrypoints
     libc.src.sys.personality.personality
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index fad2d7ca08091..69a474ddcf8c0 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -384,7 +384,8 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.time.utimes
 
     # sys/sysinfo.h entrypoints
-    libc.src.sys.sysinfo.sysinfo
+    # Disabled due to QEMU test failures.
+    # libc.src.sys.sysinfo.sysinfo
 
     # sys/utsname.h entrypoints
     libc.src.sys.utsname.uname



More information about the libc-commits mailing list