[libc] Implement fchownat in unistd (#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

GitOrigin-RevId: 4428e953e2a5da4f870d819a30de3aaba08cf0af
diff --git a/config/linux/aarch64/entrypoints.txt b/config/linux/aarch64/entrypoints.txt
index c7ded2f..83747d3 100644
--- a/config/linux/aarch64/entrypoints.txt
+++ b/config/linux/aarch64/entrypoints.txt
@@ -453,6 +453,7 @@
     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/config/linux/arm/entrypoints.txt b/config/linux/arm/entrypoints.txt
index 716ce89..f7714b2 100644
--- a/config/linux/arm/entrypoints.txt
+++ b/config/linux/arm/entrypoints.txt
@@ -255,6 +255,7 @@
     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/config/linux/riscv/entrypoints.txt b/config/linux/riscv/entrypoints.txt
index 296985e..2180767 100644
--- a/config/linux/riscv/entrypoints.txt
+++ b/config/linux/riscv/entrypoints.txt
@@ -472,6 +472,7 @@
     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/config/linux/x86_64/entrypoints.txt b/config/linux/x86_64/entrypoints.txt
index 7928a35..711ee9a 100644
--- a/config/linux/x86_64/entrypoints.txt
+++ b/config/linux/x86_64/entrypoints.txt
@@ -492,6 +492,7 @@
     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/include/unistd.yaml b/include/unistd.yaml
index 46d0200..9295115 100644
--- a/include/unistd.yaml
+++ b/include/unistd.yaml
@@ -252,6 +252,16 @@
       - 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/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 2a24b6b..1adad38 100644
--- a/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -776,6 +776,20 @@
 )
 
 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
     ftruncate.h
diff --git a/src/__support/OSUtil/linux/syscall_wrappers/fchownat.h b/src/__support/OSUtil/linux/syscall_wrappers/fchownat.h
new file mode 100644
index 0000000..894621b
--- /dev/null
+++ b/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/src/unistd/CMakeLists.txt b/src/unistd/CMakeLists.txt
index 3eee009..a922851 100644
--- a/src/unistd/CMakeLists.txt
+++ b/src/unistd/CMakeLists.txt
@@ -120,6 +120,13 @@
 )
 
 add_entrypoint_object(
+  fchownat
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.fchownat
+)
+
+add_entrypoint_object(
   fdatasync
   ALIAS
   DEPENDS
diff --git a/src/unistd/fchownat.h b/src/unistd/fchownat.h
new file mode 100644
index 0000000..fee8bc0
--- /dev/null
+++ b/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/src/unistd/linux/CMakeLists.txt b/src/unistd/linux/CMakeLists.txt
index 33ce147..2555c9b 100644
--- a/src/unistd/linux/CMakeLists.txt
+++ b/src/unistd/linux/CMakeLists.txt
@@ -141,6 +141,21 @@
 )
 
 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
     fdatasync.cpp
diff --git a/src/unistd/linux/fchownat.cpp b/src/unistd/linux/fchownat.cpp
new file mode 100644
index 0000000..48690ec
--- /dev/null
+++ b/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/test/src/unistd/CMakeLists.txt b/test/src/unistd/CMakeLists.txt
index 8f7fbc5..7588440 100644
--- a/test/src/unistd/CMakeLists.txt
+++ b/test/src/unistd/CMakeLists.txt
@@ -205,6 +205,28 @@
 )
 
 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
     libc_unistd_unittests
diff --git a/test/src/unistd/fchownat_test.cpp b/test/src/unistd/fchownat_test.cpp
new file mode 100644
index 0000000..e5cd898
--- /dev/null
+++ b/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));
+}