[libc-commits] [libc] f19509b - [libc] Add capability functions (#244053)
via libc-commits
libc-commits at lists.llvm.org
Wed Nov 18 23:06:04 PST 2026
Author: Michael Jones
Date: 2026-11-18T23:05:58-08:00
New Revision: f19509b0393bc186173b51fe2c9bce751e8d63e6
URL: https://github.com/llvm/llvm-project/commit/f19509b0393bc186173b51fe2c9bce751e8d63e6
DIFF: https://github.com/llvm/llvm-project/commit/f19509b0393bc186173b51fe2c9bce751e8d63e6.diff
LOG: [libc] Add capability functions (#244053)
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.
Added:
libc/hdr/types/cap_user_data_t.h
libc/hdr/types/cap_user_header_t.h
libc/src/__support/OSUtil/linux/syscall_wrappers/capget.h
libc/src/__support/OSUtil/linux/syscall_wrappers/capset.h
libc/src/sys/capability/CMakeLists.txt
libc/src/sys/capability/capget.h
libc/src/sys/capability/capset.h
libc/src/sys/capability/linux/CMakeLists.txt
libc/src/sys/capability/linux/capget.cpp
libc/src/sys/capability/linux/capset.cpp
libc/test/src/sys/capability/CMakeLists.txt
libc/test/src/sys/capability/capget_test.cpp
libc/test/src/sys/capability/capset_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/arm/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/hdr/types/CMakeLists.txt
libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
libc/src/sys/CMakeLists.txt
libc/test/src/sys/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index aa2752d1950bb..e7130cdfd46fa 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 71ec054c71361..f632b8af60789 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 03a1ffd74aefa..a95b888dc99cf 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 4f9c020c3b02f..a6cd732ce793a 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 46d92a07e138a..ecfdfd7416bbd 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -1453,3 +1453,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::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcCapsetTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcCapsetTest, Current) {
+ __user_cap_header_struct hdr = {_LINUX_CAPABILITY_VERSION_3, 0};
+ __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3] = {};
+ ASSERT_THAT(LIBC_NAMESPACE::capget(&hdr, data), Succeeds(0));
+
+ int ret = LIBC_NAMESPACE::capset(&hdr, data);
+ if (ret == 0) {
+ ASSERT_ERRNO_SUCCESS();
+ } else {
+ ASSERT_THAT(ret, Fails(any_of(EPERM, ENOSYS)));
+ }
+}
+
+TEST_F(LlvmLibcCapsetTest, Failure) {
+ __user_cap_header_struct bad_hdr = {0, 0};
+ __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3] = {};
+ ASSERT_THAT(LIBC_NAMESPACE::capset(&bad_hdr, data),
+ Fails(any_of(EINVAL, EPERM, ENOSYS)));
+
+ __user_cap_header_struct hdr = {_LINUX_CAPABILITY_VERSION_3, 0};
+ ASSERT_THAT(LIBC_NAMESPACE::capget(&hdr, data), Succeeds(0));
+ if ((data[0].permitted & (1U << CAP_SETUID)) == 0) {
+ data[0].effective |= (1U << CAP_SETUID);
+ data[0].permitted |= (1U << CAP_SETUID);
+ ASSERT_THAT(LIBC_NAMESPACE::capset(&hdr, data),
+ Fails(any_of(EPERM, ENOSYS)));
+ }
+}
More information about the libc-commits
mailing list