[libc-commits] [libc] [libc] Add capability functions (PR #244053)

via libc-commits libc-commits at lists.llvm.org
Wed Nov 18 16:03:18 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Michael Jones (michaelrj-google)

<details>
<summary>Changes</summary>

The capget and capset functions are weird, the current linux API expects
that they are provided by the system libc, but not exposed via headers.
Based on that, the lack of a yaml change is intentional.

Assisted-by: Automated tooling, human reviewed.


---

Patch is 21.20 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/244053.diff


21 Files Affected:

- (modified) libc/config/linux/aarch64/entrypoints.txt (+4) 
- (modified) libc/config/linux/arm/entrypoints.txt (+4) 
- (modified) libc/config/linux/riscv/entrypoints.txt (+4) 
- (modified) libc/config/linux/x86_64/entrypoints.txt (+4) 
- (modified) libc/hdr/types/CMakeLists.txt (+12) 
- (added) libc/hdr/types/cap_user_data_t.h (+21) 
- (added) libc/hdr/types/cap_user_header_t.h (+21) 
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+28) 
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/capget.h (+36) 
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/capset.h (+37) 
- (modified) libc/src/sys/CMakeLists.txt (+1) 
- (added) libc/src/sys/capability/CMakeLists.txt (+17) 
- (added) libc/src/sys/capability/capget.h (+27) 
- (added) libc/src/sys/capability/capset.h (+27) 
- (added) libc/src/sys/capability/linux/CMakeLists.txt (+33) 
- (added) libc/src/sys/capability/linux/capget.cpp (+36) 
- (added) libc/src/sys/capability/linux/capset.cpp (+36) 
- (modified) libc/test/src/sys/CMakeLists.txt (+1) 
- (added) libc/test/src/sys/capability/CMakeLists.txt (+32) 
- (added) libc/test/src/sys/capability/capget_test.cpp (+41) 
- (added) libc/test/src/sys/capability/capset_test.cpp (+57) 


``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index b43c7c670db82..0d4df04238174 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -289,6 +289,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.vsprintf
     libc.src.stdio.vasprintf
 
+    # sys/capability.h entrypoints
+    libc.src.sys.capability.capget
+    libc.src.sys.capability.capset
+
     # sys/epoll.h entrypoints
     libc.src.sys.epoll.epoll_create
     libc.src.sys.epoll.epoll_create1
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 956a154cf0d5e..55785f1ab7177 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -211,6 +211,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     # stdio.h entrypoints
     libc.src.stdio.tmpfile
 
+    # sys/capability.h entrypoints
+    libc.src.sys.capability.capget
+    libc.src.sys.capability.capset
+
     # sys/eventfd.h entrypoints
     libc.src.sys.eventfd.eventfd
     libc.src.sys.eventfd.eventfd_read
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 6354889fa0edd..e7038e6b2967e 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -319,6 +319,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.vsprintf
     libc.src.stdio.vasprintf
 
+    # sys/capability.h entrypoints
+    libc.src.sys.capability.capget
+    libc.src.sys.capability.capset
+
     # sys/epoll.h entrypoints
     libc.src.sys.epoll.epoll_create
     libc.src.sys.epoll.epoll_create1
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 0e7ae76c11457..64ee14e130a96 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -319,6 +319,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.vsprintf
     libc.src.stdio.vasprintf
 
+    # sys/capability.h entrypoints
+    libc.src.sys.capability.capget
+    libc.src.sys.capability.capset
+
     # sys/epoll.h entrypoints
     libc.src.sys.epoll.epoll_create
     libc.src.sys.epoll.epoll_create1
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 61fc9c948a638..be4a5b0e3f793 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -1445,3 +1445,15 @@ add_proxy_header_library(
   FULL_BUILD_DEPENDS
     libc.include.llvm-libc-types.eventfd_t
 )
+
+add_proxy_header_library(
+  cap_user_header_t
+  HDRS
+    cap_user_header_t.h
+)
+
+add_proxy_header_library(
+  cap_user_data_t
+  HDRS
+    cap_user_data_t.h
+)
diff --git a/libc/hdr/types/cap_user_data_t.h b/libc/hdr/types/cap_user_data_t.h
new file mode 100644
index 0000000000000..edf32021f0f7a
--- /dev/null
+++ b/libc/hdr/types/cap_user_data_t.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 header for cap_user_data_t.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_CAP_USER_DATA_T_H
+#define LLVM_LIBC_HDR_TYPES_CAP_USER_DATA_T_H
+
+#ifdef __linux__
+#include <linux/capability.h>
+#endif // __linux__
+
+#endif // LLVM_LIBC_HDR_TYPES_CAP_USER_DATA_T_H
diff --git a/libc/hdr/types/cap_user_header_t.h b/libc/hdr/types/cap_user_header_t.h
new file mode 100644
index 0000000000000..0de35b6e315c1
--- /dev/null
+++ b/libc/hdr/types/cap_user_header_t.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 header for cap_user_header_t.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_CAP_USER_HEADER_T_H
+#define LLVM_LIBC_HDR_TYPES_CAP_USER_HEADER_T_H
+
+#ifdef __linux__
+#include <linux/capability.h>
+#endif // __linux__
+
+#endif // LLVM_LIBC_HDR_TYPES_CAP_USER_HEADER_T_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index bb9d3a1846bef..d7a75d9172e06 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -2701,3 +2701,31 @@ add_header_library(
     libc.src.__support.error_or
     libc.src.__support.macros.config
 )
+
+add_header_library(
+  capget
+  HDRS
+    capget.h
+  DEPENDS
+    libc.hdr.types.cap_user_data_t
+    libc.hdr.types.cap_user_header_t
+    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(
+  capset
+  HDRS
+    capset.h
+  DEPENDS
+    libc.hdr.types.cap_user_data_t
+    libc.hdr.types.cap_user_header_t
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+)
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/capget.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/capget.h
new file mode 100644
index 0000000000000..1e4b2d98bd497
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/capget.h
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 capget.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CAPGET_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CAPGET_H
+
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.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> capget(cap_user_header_t hdrp, cap_user_data_t datap) {
+  return syscall_checked<int>(SYS_capget, hdrp, datap);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CAPGET_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/capset.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/capset.h
new file mode 100644
index 0000000000000..e9b8e9e87aabd
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/capset.h
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 capset.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CAPSET_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CAPSET_H
+
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.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> capset(cap_user_header_t hdrp,
+                                const cap_user_data_t datap) {
+  return syscall_checked<int>(SYS_capset, hdrp, datap);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CAPSET_H
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index c891c471ed4f1..d43daa8411cfe 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_subdirectory(auxv)
+add_subdirectory(capability)
 add_subdirectory(epoll)
 add_subdirectory(eventfd)
 add_subdirectory(file)
diff --git a/libc/src/sys/capability/CMakeLists.txt b/libc/src/sys/capability/CMakeLists.txt
new file mode 100644
index 0000000000000..0db9a3bd7ed8a
--- /dev/null
+++ b/libc/src/sys/capability/CMakeLists.txt
@@ -0,0 +1,17 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+  capget
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.capget
+)
+
+add_entrypoint_object(
+  capset
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.capset
+)
diff --git a/libc/src/sys/capability/capget.h b/libc/src/sys/capability/capget.h
new file mode 100644
index 0000000000000..66d50a1109a1d
--- /dev/null
+++ b/libc/src/sys/capability/capget.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
+/// Implementation header for capget.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_CAPABILITY_CAPGET_H
+#define LLVM_LIBC_SRC_SYS_CAPABILITY_CAPGET_H
+
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int capget(cap_user_header_t hdrp, cap_user_data_t datap);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_CAPABILITY_CAPGET_H
diff --git a/libc/src/sys/capability/capset.h b/libc/src/sys/capability/capset.h
new file mode 100644
index 0000000000000..96dcd2c56c092
--- /dev/null
+++ b/libc/src/sys/capability/capset.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
+/// Implementation header for capset.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_CAPABILITY_CAPSET_H
+#define LLVM_LIBC_SRC_SYS_CAPABILITY_CAPSET_H
+
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_CAPABILITY_CAPSET_H
diff --git a/libc/src/sys/capability/linux/CMakeLists.txt b/libc/src/sys/capability/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..8f778ba9b6d20
--- /dev/null
+++ b/libc/src/sys/capability/linux/CMakeLists.txt
@@ -0,0 +1,33 @@
+add_entrypoint_object(
+  capget
+  SRCS
+    capget.cpp
+  HDRS
+    ../capget.h
+  DEPENDS
+    libc.hdr.types.cap_user_data_t
+    libc.hdr.types.cap_user_header_t
+    libc.src.__support.OSUtil.linux.syscall_wrappers.capget
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  capset
+  SRCS
+    capset.cpp
+  HDRS
+    ../capset.h
+  DEPENDS
+    libc.hdr.types.cap_user_data_t
+    libc.hdr.types.cap_user_header_t
+    libc.src.__support.OSUtil.linux.syscall_wrappers.capset
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
diff --git a/libc/src/sys/capability/linux/capget.cpp b/libc/src/sys/capability/linux/capget.cpp
new file mode 100644
index 0000000000000..d9f7b2fae718b
--- /dev/null
+++ b/libc/src/sys/capability/linux/capget.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 capget.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/capability/capget.h"
+
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/capget.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, capget,
+                   (cap_user_header_t hdrp, cap_user_data_t datap)) {
+  auto result = linux_syscalls::capget(hdrp, datap);
+  if (!result.has_value()) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/capability/linux/capset.cpp b/libc/src/sys/capability/linux/capset.cpp
new file mode 100644
index 0000000000000..facbd53ecd25d
--- /dev/null
+++ b/libc/src/sys/capability/linux/capset.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 capset.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/capability/capset.h"
+
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/capset.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, capset,
+                   (cap_user_header_t hdrp, const cap_user_data_t datap)) {
+  auto result = linux_syscalls::capset(hdrp, datap);
+  if (!result.has_value()) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index 6650f0842f3ae..5b8642a304e3a 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -12,6 +12,7 @@ add_subdirectory(utsname)
 add_subdirectory(wait)
 add_subdirectory(prctl)
 add_subdirectory(auxv)
+add_subdirectory(capability)
 add_subdirectory(epoll)
 add_subdirectory(eventfd)
 add_subdirectory(file)
diff --git a/libc/test/src/sys/capability/CMakeLists.txt b/libc/test/src/sys/capability/CMakeLists.txt
new file mode 100644
index 0000000000000..1c2eedf602560
--- /dev/null
+++ b/libc/test/src/sys/capability/CMakeLists.txt
@@ -0,0 +1,32 @@
+add_libc_testsuite(libc-sys-capability-tests)
+
+add_libc_test(
+  capget_test
+  SUITE
+    libc-sys-capability-tests
+  SRCS
+    capget_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.types.cap_user_data_t
+    libc.hdr.types.cap_user_header_t
+    libc.src.sys.capability.capget
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
+add_libc_test(
+  capset_test
+  SUITE
+    libc-sys-capability-tests
+  SRCS
+    capset_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.types.cap_user_data_t
+    libc.hdr.types.cap_user_header_t
+    libc.src.sys.capability.capget
+    libc.src.sys.capability.capset
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
diff --git a/libc/test/src/sys/capability/capget_test.cpp b/libc/test/src/sys/capability/capget_test.cpp
new file mode 100644
index 0000000000000..fc43f634d084a
--- /dev/null
+++ b/libc/test/src/sys/capability/capget_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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for capget.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/sys/capability/capget.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include <linux/capability.h>
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcCapgetTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcCapgetTest, VersionProbeAndCurrent) {
+  __user_cap_header_struct hdr = {0, 0};
+  ASSERT_THAT(LIBC_NAMESPACE::capget(&hdr, nullptr), Succeeds(0));
+  ASSERT_EQ(hdr.version, static_cast<__u32>(_LINUX_CAPABILITY_VERSION_3));
+
+  __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3] = {};
+  ASSERT_THAT(LIBC_NAMESPACE::capget(&hdr, data), Succeeds(0));
+}
+
+TEST_F(LlvmLibcCapgetTest, InvalidPid) {
+  __user_cap_header_struct hdr = {_LINUX_CAPABILITY_VERSION_3, -1};
+  __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3] = {};
+  ASSERT_THAT(LIBC_NAMESPACE::capget(&hdr, data), Fails(EINVAL));
+}
diff --git a/libc/test/src/sys/capability/capset_test.cpp b/libc/test/src/sys/capability/capset_test.cpp
new file mode 100644
index 0000000000000..f597a3110b285
--- /dev/null
+++ b/libc/test/src/sys/capability/capset_test.cpp
@@ -0,0 +1,57 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 capset.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/sys/capability/capget.h"
+#include "src/sys/capability/capset.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include <linux/capability.h>
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::any_of;
+using LIBC_NAMESPACE::testi...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/244053


More information about the libc-commits mailing list