[libc-commits] [libc] [libc] Implement getresuid and getresgid in unistd (PR #235274)
Aman Maurya via libc-commits
libc-commits at lists.llvm.org
Mon Oct 26 02:34:28 PDT 2026
https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/235274
>From 2cd035c182044337cfe7df97b8506aa4d30f8554 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sat, 24 Oct 2026 15:17:27 +0530
Subject: [PATCH] [libc] Implement getresuid and getresgid in unistd
Fixes #235268
---
libc/config/linux/aarch64/entrypoints.txt | 2 +
libc/config/linux/arm/entrypoints.txt | 2 +
libc/config/linux/riscv/entrypoints.txt | 2 +
libc/config/linux/x86_64/entrypoints.txt | 2 +
libc/include/unistd.yaml | 16 ++++++++
.../linux/syscall_wrappers/CMakeLists.txt | 26 ++++++++++++
.../OSUtil/linux/syscall_wrappers/getresgid.h | 40 +++++++++++++++++++
.../OSUtil/linux/syscall_wrappers/getresuid.h | 40 +++++++++++++++++++
libc/src/unistd/CMakeLists.txt | 14 +++++++
libc/src/unistd/getresgid.h | 26 ++++++++++++
libc/src/unistd/getresuid.h | 26 ++++++++++++
libc/src/unistd/linux/CMakeLists.txt | 24 +++++++++++
libc/src/unistd/linux/getresgid.cpp | 33 +++++++++++++++
libc/src/unistd/linux/getresuid.cpp | 33 +++++++++++++++
libc/test/src/unistd/CMakeLists.txt | 30 ++++++++++++++
libc/test/src/unistd/getresgid_test.cpp | 38 ++++++++++++++++++
libc/test/src/unistd/getresuid_test.cpp | 38 ++++++++++++++++++
17 files changed, 392 insertions(+)
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/getresgid.h
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/getresuid.h
create mode 100644 libc/src/unistd/getresgid.h
create mode 100644 libc/src/unistd/getresuid.h
create mode 100644 libc/src/unistd/linux/getresgid.cpp
create mode 100644 libc/src/unistd/linux/getresuid.cpp
create mode 100644 libc/test/src/unistd/getresgid_test.cpp
create mode 100644 libc/test/src/unistd/getresuid_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index ae4d060ad2e65c..f6eaeccbbe488b 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 e35ca9c7211ab3..73884d99bba615 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 f8906e3adbfd30..de1ac3e82d8b84 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 5d67548617ea5b..c132e3826f12f6 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 c7c1f4b9d204f1..7e3f886f2f4aeb 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