[libc-commits] [libc] [libc] Implement fchownat in unistd (PR #227974)
Aman Maurya via libc-commits
libc-commits at lists.llvm.org
Wed Sep 30 23:28:07 PDT 2026
https://github.com/amanmaurya92 created https://github.com/llvm/llvm-project/pull/227974
Implement the standard POSIX.1-2008 / POSIX.1-2024 function `fchownat` in `<unistd.h>`, bringing the POSIX `*at` family of functions in `<unistd.h>` to 100% completion.
Fixes #227848
Assisted by Antigravity and reviewed by Aman Maurya
>From 1c70c88944f70bd632a8f37ce0a18a3176407d0c Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Thu, 1 Oct 2026 11:28:10 +0530
Subject: [PATCH] [libc][unistd] Implement fchownat in unistd
Assisted by Antigravity and reviewed by Aman Maurya
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/arm/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/include/unistd.yaml | 10 ++
.../linux/syscall_wrappers/CMakeLists.txt | 14 +++
.../OSUtil/linux/syscall_wrappers/fchownat.h | 37 ++++++
libc/src/unistd/CMakeLists.txt | 7 ++
libc/src/unistd/fchownat.h | 28 +++++
libc/src/unistd/linux/CMakeLists.txt | 15 +++
libc/src/unistd/linux/fchownat.cpp | 34 +++++
libc/test/src/unistd/CMakeLists.txt | 22 ++++
libc/test/src/unistd/fchownat_test.cpp | 119 ++++++++++++++++++
13 files changed, 290 insertions(+)
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/fchownat.h
create mode 100644 libc/src/unistd/fchownat.h
create mode 100644 libc/src/unistd/linux/fchownat.cpp
create mode 100644 libc/test/src/unistd/fchownat_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 9bb9407a7624b..93a7041beacff 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -453,6 +453,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fchown
+ libc.src.unistd.fchownat
libc.src.unistd.fdatasync
libc.src.unistd.fpathconf
libc.src.unistd.fsync
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 716ce89d2e6c5..f7714b2049a27 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -255,6 +255,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fchown
+ libc.src.unistd.fchownat
libc.src.unistd.fdatasync
libc.src.unistd.fsync
libc.src.unistd.ftruncate
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 475c6bbd5fbac..d2f8d6e5abfa8 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -472,6 +472,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fchown
+ libc.src.unistd.fchownat
libc.src.unistd.fdatasync
libc.src.unistd.fpathconf
libc.src.unistd.fsync
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 1007ba4775cd7..01f806b11c756 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -492,6 +492,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fchown
+ libc.src.unistd.fchownat
libc.src.unistd.fdatasync
libc.src.unistd.fpathconf
libc.src.unistd.fsync
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 46d02001dc6a2..92951152b7ca7 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -252,6 +252,16 @@ functions:
- type: int
- type: uid_t
- type: gid_t
+ - name: fchownat
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: int
+ - type: const char *
+ - type: uid_t
+ - type: gid_t
+ - type: int
- name: fdatasync
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 2a24b6b28cab0..1adad38b86e10 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -775,6 +775,20 @@ add_header_library(
libc.src.__support.macros.config
)
+add_header_library(
+ fchownat
+ HDRS
+ fchownat.h
+ DEPENDS
+ libc.hdr.types.gid_t
+ libc.hdr.types.uid_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(
ftruncate
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/fchownat.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/fchownat.h
new file mode 100644
index 0000000000000..894621b0ff26a
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/fchownat.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 fchownat.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FCHOWNAT_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FCHOWNAT_H
+
+#include "hdr/types/gid_t.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> fchownat(int dirfd, const char *pathname, uid_t owner,
+ gid_t group, int flags) {
+ return syscall_checked<int>(SYS_fchownat, dirfd, pathname, owner, group,
+ flags);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FCHOWNAT_H
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 3eee00978b6fc..a9228513c0312 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -119,6 +119,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.fchown
)
+add_entrypoint_object(
+ fchownat
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.fchownat
+)
+
add_entrypoint_object(
fdatasync
ALIAS
diff --git a/libc/src/unistd/fchownat.h b/libc/src/unistd/fchownat.h
new file mode 100644
index 0000000000000..fee8bc03650b2
--- /dev/null
+++ b/libc/src/unistd/fchownat.h
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 fchownat.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_FCHOWNAT_H
+#define LLVM_LIBC_SRC_UNISTD_FCHOWNAT_H
+
+#include "hdr/types/gid_t.h"
+#include "hdr/types/uid_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group,
+ int flags);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_FCHOWNAT_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 33ce14751979d..2555c9b700982 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -140,6 +140,21 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ fchownat
+ SRCS
+ fchownat.cpp
+ HDRS
+ ../fchownat.h
+ DEPENDS
+ libc.hdr.types.gid_t
+ libc.hdr.types.uid_t
+ libc.src.__support.OSUtil.linux.syscall_wrappers.fchownat
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+)
+
add_entrypoint_object(
fdatasync
SRCS
diff --git a/libc/src/unistd/linux/fchownat.cpp b/libc/src/unistd/linux/fchownat.cpp
new file mode 100644
index 0000000000000..48690ec1d85be
--- /dev/null
+++ b/libc/src/unistd/linux/fchownat.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 fchownat.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/fchownat.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/fchownat.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, fchownat,
+ (int dirfd, const char *pathname, uid_t owner, gid_t group,
+ int flags)) {
+ auto ret = linux_syscalls::fchownat(dirfd, pathname, owner, group, flags);
+ if (!ret.has_value()) {
+ 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 8f7fbc5607cb2..7588440c3d8df 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -204,6 +204,28 @@ add_libc_test(
libc.test.UnitTest.ErrnoSetterMatcher
)
+add_libc_test(
+ fchownat_test
+ SUITE
+ libc_unistd_unittests
+ SRCS
+ fchownat_test.cpp
+ DEPENDS
+ libc.hdr.sys_stat_macros
+ libc.hdr.fcntl_macros
+ libc.include.unistd
+ libc.src.__support.CPP.scope
+ libc.src.errno.errno
+ libc.src.unistd.fchownat
+ libc.src.unistd.close
+ libc.src.unistd.unlink
+ libc.src.fcntl.open
+ libc.src.unistd.getuid
+ libc.src.unistd.getgid
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
add_libc_test(
fdatasync_test
SUITE
diff --git a/libc/test/src/unistd/fchownat_test.cpp b/libc/test/src/unistd/fchownat_test.cpp
new file mode 100644
index 0000000000000..e5cd89835b381
--- /dev/null
+++ b/libc/test/src/unistd/fchownat_test.cpp
@@ -0,0 +1,119 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 fchownat.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/fcntl_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "src/__support/CPP/scope.h"
+#include "src/fcntl/open.h"
+#include "src/unistd/close.h"
+#include "src/unistd/fchownat.h"
+#include "src/unistd/getgid.h"
+#include "src/unistd/getuid.h"
+#include "src/unistd/unlink.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcFchownatTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcFchownatTest, FchownatSuccessAtFdCwd) {
+ uid_t my_uid = LIBC_NAMESPACE::getuid();
+ gid_t my_gid = LIBC_NAMESPACE::getgid();
+ constexpr const char *FILENAME = "fchownat_at_fdcwd.test";
+ auto TEST_FILE = libc_make_test_file_path(FILENAME);
+
+ int write_fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(write_fd, 0);
+ ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0));
+
+ LIBC_NAMESPACE::cpp::scope_exit cleanup(
+ [&] { EXPECT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0)); });
+
+ // Change ownership to current user/group with AT_FDCWD.
+ ASSERT_THAT(LIBC_NAMESPACE::fchownat(AT_FDCWD, TEST_FILE, my_uid, my_gid, 0),
+ Succeeds(0));
+
+ // Calling with AT_SYMLINK_NOFOLLOW on a regular file succeeds.
+ ASSERT_THAT(LIBC_NAMESPACE::fchownat(AT_FDCWD, TEST_FILE, my_uid, my_gid,
+ AT_SYMLINK_NOFOLLOW),
+ Succeeds(0));
+
+ // Passing -1 for owner and group leaves them unchanged without error.
+ ASSERT_THAT(LIBC_NAMESPACE::fchownat(AT_FDCWD, TEST_FILE,
+ static_cast<uid_t>(-1),
+ static_cast<gid_t>(-1), 0),
+ Succeeds(0));
+}
+
+TEST_F(LlvmLibcFchownatTest, FchownatSuccessWithDirFd) {
+ uid_t my_uid = LIBC_NAMESPACE::getuid();
+ gid_t my_gid = LIBC_NAMESPACE::getgid();
+ auto TEST_DIR = libc_make_test_file_path("testdata");
+ constexpr const char *TEST_FILE_BASENAME = "fchownat_dir.test";
+ auto TEST_FILE_PATH = libc_make_test_file_path("testdata/fchownat_dir.test");
+
+ int write_fd =
+ LIBC_NAMESPACE::open(TEST_FILE_PATH, O_WRONLY | O_CREAT, S_IRWXU);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(write_fd, 0);
+ ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0));
+
+ LIBC_NAMESPACE::cpp::scope_exit cleanup_file([&] {
+ EXPECT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_PATH), Succeeds(0));
+ });
+
+ int dirfd = LIBC_NAMESPACE::open(TEST_DIR, O_DIRECTORY | O_RDONLY);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(dirfd, 0);
+
+ LIBC_NAMESPACE::cpp::scope_exit cleanup_dir(
+ [&] { EXPECT_THAT(LIBC_NAMESPACE::close(dirfd), Succeeds(0)); });
+
+ // Change ownership through directory file descriptor and relative basename.
+ ASSERT_THAT(
+ LIBC_NAMESPACE::fchownat(dirfd, TEST_FILE_BASENAME, my_uid, my_gid, 0),
+ Succeeds(0));
+}
+
+TEST_F(LlvmLibcFchownatTest, FchownatNonExistentFile) {
+ auto BAD_PATH = libc_make_test_file_path("non_existent_file_fchownat.test");
+ ASSERT_THAT(LIBC_NAMESPACE::fchownat(AT_FDCWD, BAD_PATH, 1000, 1000, 0),
+ Fails(ENOENT));
+}
+
+TEST_F(LlvmLibcFchownatTest, FchownatInvalidDirFd) {
+ ASSERT_THAT(LIBC_NAMESPACE::fchownat(-1, "relative_path_fchownat.test", 1000,
+ 1000, 0),
+ Fails(EBADF));
+}
+
+TEST_F(LlvmLibcFchownatTest, FchownatInvalidFlags) {
+ uid_t my_uid = LIBC_NAMESPACE::getuid();
+ gid_t my_gid = LIBC_NAMESPACE::getgid();
+ constexpr const char *FILENAME = "fchownat_invalid_flags.test";
+ auto TEST_FILE = libc_make_test_file_path(FILENAME);
+
+ int write_fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(write_fd, 0);
+ ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0));
+
+ LIBC_NAMESPACE::cpp::scope_exit cleanup(
+ [&] { EXPECT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0)); });
+
+ // Passing invalid flags should fail with EINVAL.
+ ASSERT_THAT(LIBC_NAMESPACE::fchownat(AT_FDCWD, TEST_FILE, my_uid, my_gid, ~0),
+ Fails(EINVAL));
+}
More information about the libc-commits
mailing list