[libc-commits] [libc] 867097d - [libc] Implement getresuid and getresgid in unistd (#235274)
via libc-commits
libc-commits at lists.llvm.org
Mon Oct 26 06:52:12 PDT 2026
Author: Aman Maurya
Date: 2026-10-26T19:22:07+05:30
New Revision: 867097d2546adcf9ccfc78d21c54bf100c1d7887
URL: https://github.com/llvm/llvm-project/commit/867097d2546adcf9ccfc78d21c54bf100c1d7887
DIFF: https://github.com/llvm/llvm-project/commit/867097d2546adcf9ccfc78d21c54bf100c1d7887.diff
LOG: [libc] Implement getresuid and getresgid in unistd (#235274)
Implement the standard Linux extension functions `getresuid` and
`getresgid` in `<unistd.h>`.
Fixes #235268.
Assisted by Antigravity and reviewed by Aman Maurya.
Added:
libc/src/__support/OSUtil/linux/syscall_wrappers/getresgid.h
libc/src/__support/OSUtil/linux/syscall_wrappers/getresuid.h
libc/src/unistd/getresgid.h
libc/src/unistd/getresuid.h
libc/src/unistd/linux/getresgid.cpp
libc/src/unistd/linux/getresuid.cpp
libc/test/src/unistd/getresgid_test.cpp
libc/test/src/unistd/getresuid_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/include/unistd.yaml
libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
libc/src/unistd/CMakeLists.txt
libc/src/unistd/linux/CMakeLists.txt
libc/test/src/unistd/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 93e4a812f39c53..7a94716e74032c 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -481,6 +481,8 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.getpgrp
libc.src.unistd.getpid
libc.src.unistd.getppid
+ libc.src.unistd.getresgid
+ libc.src.unistd.getresuid
libc.src.unistd.getsid
libc.src.unistd.gettid
libc.src.unistd.getgid
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 8c7ebf61d5786d..31561b52bdca51 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -277,6 +277,8 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.getpgrp
libc.src.unistd.getpid
libc.src.unistd.getppid
+ libc.src.unistd.getresgid
+ libc.src.unistd.getresuid
libc.src.unistd.getsid
libc.src.unistd.gettid
libc.src.unistd.getgid
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 89560e59f10c52..8c31aaad07015f 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -505,6 +505,8 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.getpgrp
libc.src.unistd.getpid
libc.src.unistd.getppid
+ libc.src.unistd.getresgid
+ libc.src.unistd.getresuid
libc.src.unistd.getsid
libc.src.unistd.gettid
libc.src.unistd.getgid
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7fa1fb4bb69da5..02aef4a1297186 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -525,6 +525,8 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.getpgrp
libc.src.unistd.getpid
libc.src.unistd.getppid
+ libc.src.unistd.getresgid
+ libc.src.unistd.getresuid
libc.src.unistd.getsid
libc.src.unistd.gettid
libc.src.unistd.getgid
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 96ce15eee40b9a..af8fab19c94307 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -404,6 +404,22 @@ functions:
return_type: int
arguments:
- type: void
+ - name: getresgid
+ standards:
+ - linux
+ return_type: int
+ arguments:
+ - type: gid_t *
+ - type: gid_t *
+ - type: gid_t *
+ - name: getresuid
+ standards:
+ - linux
+ return_type: int
+ arguments:
+ - type: uid_t *
+ - type: uid_t *
+ - type: uid_t *
- name: getsid
standards:
- posix
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index ca476f95b0cc8c..9bcf448223efa4 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -972,6 +972,32 @@ add_header_library(
libc.src.__support.OSUtil.osutil
)
+add_header_library(
+ getresgid
+ HDRS
+ getresgid.h
+ DEPENDS
+ libc.hdr.types.gid_t
+ libc.include.sys_syscall
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.src.__support.OSUtil.osutil
+)
+
+add_header_library(
+ getresuid
+ HDRS
+ getresuid.h
+ DEPENDS
+ libc.hdr.types.uid_t
+ libc.include.sys_syscall
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.src.__support.OSUtil.osutil
+)
+
add_header_library(
setresgid
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/getresgid.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/getresgid.h
new file mode 100644
index 00000000000000..d7810b3e71bd3f
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/getresgid.h
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 getresgid.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETRESGID_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETRESGID_H
+
+#include "hdr/types/gid_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // 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> getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid) {
+#if defined(SYS_getresgid32)
+ return syscall_checked<int>(SYS_getresgid32, rgid, egid, sgid);
+#elif defined(SYS_getresgid)
+ return syscall_checked<int>(SYS_getresgid, rgid, egid, sgid);
+#else
+#error "SYS_getresgid and SYS_getresgid32 syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETRESGID_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/getresuid.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/getresuid.h
new file mode 100644
index 00000000000000..b4506e3030e24b
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/getresuid.h
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 getresuid.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETRESUID_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETRESUID_H
+
+#include "hdr/types/uid_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // 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> getresuid(uid_t *ruid, uid_t *euid, uid_t *suid) {
+#if defined(SYS_getresuid32)
+ return syscall_checked<int>(SYS_getresuid32, ruid, euid, suid);
+#elif defined(SYS_getresuid)
+ return syscall_checked<int>(SYS_getresuid, ruid, euid, suid);
+#else
+#error "SYS_getresuid and SYS_getresuid32 syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETRESUID_H
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 0424641543d93d..14b59190cb2db4 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -273,6 +273,20 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.getppid
)
+add_entrypoint_object(
+ getresgid
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.getresgid
+)
+
+add_entrypoint_object(
+ getresuid
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.getresuid
+)
+
add_entrypoint_object(
getsid
ALIAS
diff --git a/libc/src/unistd/getresgid.h b/libc/src/unistd/getresgid.h
new file mode 100644
index 00000000000000..d3a49d71681b30
--- /dev/null
+++ b/libc/src/unistd/getresgid.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 getresgid function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_GETRESGID_H
+#define LLVM_LIBC_SRC_UNISTD_GETRESGID_H
+
+#include "hdr/types/gid_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_GETRESGID_H
diff --git a/libc/src/unistd/getresuid.h b/libc/src/unistd/getresuid.h
new file mode 100644
index 00000000000000..1fe1f49d754f89
--- /dev/null
+++ b/libc/src/unistd/getresuid.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 getresuid function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_GETRESUID_H
+#define LLVM_LIBC_SRC_UNISTD_GETRESUID_H
+
+#include "hdr/types/uid_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_GETRESUID_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index f3a8d8f011ac97..14930948183757 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -481,6 +481,30 @@ add_entrypoint_object(
libc.src.__support.OSUtil.osutil
)
+add_entrypoint_object(
+ getresgid
+ SRCS
+ getresgid.cpp
+ HDRS
+ ../getresgid.h
+ DEPENDS
+ libc.hdr.types.gid_t
+ libc.src.__support.OSUtil.linux.syscall_wrappers.getresgid
+ libc.src.errno.errno
+)
+
+add_entrypoint_object(
+ getresuid
+ SRCS
+ getresuid.cpp
+ HDRS
+ ../getresuid.h
+ DEPENDS
+ libc.hdr.types.uid_t
+ libc.src.__support.OSUtil.linux.syscall_wrappers.getresuid
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
getsid
SRCS
diff --git a/libc/src/unistd/linux/getresgid.cpp b/libc/src/unistd/linux/getresgid.cpp
new file mode 100644
index 00000000000000..a01191c3ceed79
--- /dev/null
+++ b/libc/src/unistd/linux/getresgid.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 getresgid.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/getresgid.h"
+
+#include "hdr/types/gid_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/getresgid.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, getresgid, (gid_t * rgid, gid_t *egid, gid_t *sgid)) {
+ auto ret = linux_syscalls::getresgid(rgid, egid, sgid);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/unistd/linux/getresuid.cpp b/libc/src/unistd/linux/getresuid.cpp
new file mode 100644
index 00000000000000..bfbf0f0631977c
--- /dev/null
+++ b/libc/src/unistd/linux/getresuid.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 getresuid.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/getresuid.h"
+
+#include "hdr/types/uid_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/getresuid.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, getresuid, (uid_t * ruid, uid_t *euid, uid_t *suid)) {
+ auto ret = linux_syscalls::getresuid(ruid, euid, suid);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index abbe66fcf36d77..db172be16e1840 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -925,6 +925,36 @@ add_libc_test(
libc.src.unistd.getppid
)
+add_libc_test(
+ getresgid_test
+ SUITE
+ libc-unistd-tests
+ SRCS
+ getresgid_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.src.unistd.getegid
+ libc.src.unistd.getgid
+ libc.src.unistd.getresgid
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
+add_libc_test(
+ getresuid_test
+ SUITE
+ libc-unistd-tests
+ SRCS
+ getresuid_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.src.unistd.geteuid
+ libc.src.unistd.getuid
+ libc.src.unistd.getresuid
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
add_libc_test(
getsid_test
SUITE
diff --git a/libc/test/src/unistd/getresgid_test.cpp b/libc/test/src/unistd/getresgid_test.cpp
new file mode 100644
index 00000000000000..77380be4e85123
--- /dev/null
+++ b/libc/test/src/unistd/getresgid_test.cpp
@@ -0,0 +1,38 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 getresgid.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "src/unistd/getegid.h"
+#include "src/unistd/getgid.h"
+#include "src/unistd/getresgid.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcGetResGidTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcGetResGidTest, GetCurrentResGid) {
+ gid_t rgid = static_cast<gid_t>(-1);
+ gid_t egid = static_cast<gid_t>(-1);
+ gid_t sgid = static_cast<gid_t>(-1);
+ ASSERT_THAT(LIBC_NAMESPACE::getresgid(&rgid, &egid, &sgid), Succeeds());
+ EXPECT_EQ(rgid, LIBC_NAMESPACE::getgid());
+ EXPECT_EQ(egid, LIBC_NAMESPACE::getegid());
+ EXPECT_NE(sgid, static_cast<gid_t>(-1));
+}
+
+TEST_F(LlvmLibcGetResGidTest, InvalidPointer) {
+ ASSERT_THAT(LIBC_NAMESPACE::getresgid(nullptr, nullptr, nullptr),
+ Fails(EFAULT));
+}
diff --git a/libc/test/src/unistd/getresuid_test.cpp b/libc/test/src/unistd/getresuid_test.cpp
new file mode 100644
index 00000000000000..f5781238abad58
--- /dev/null
+++ b/libc/test/src/unistd/getresuid_test.cpp
@@ -0,0 +1,38 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 getresuid.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "src/unistd/geteuid.h"
+#include "src/unistd/getresuid.h"
+#include "src/unistd/getuid.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcGetResUidTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcGetResUidTest, GetCurrentResUid) {
+ uid_t ruid = static_cast<uid_t>(-1);
+ uid_t euid = static_cast<uid_t>(-1);
+ uid_t suid = static_cast<uid_t>(-1);
+ ASSERT_THAT(LIBC_NAMESPACE::getresuid(&ruid, &euid, &suid), Succeeds());
+ EXPECT_EQ(ruid, LIBC_NAMESPACE::getuid());
+ EXPECT_EQ(euid, LIBC_NAMESPACE::geteuid());
+ EXPECT_NE(suid, static_cast<uid_t>(-1));
+}
+
+TEST_F(LlvmLibcGetResUidTest, InvalidPointer) {
+ ASSERT_THAT(LIBC_NAMESPACE::getresuid(nullptr, nullptr, nullptr),
+ Fails(EFAULT));
+}
More information about the libc-commits
mailing list