[libc] Implement faccessat  (#161065)

#160404

- Implement POSIX function "faccessat"
- Remove redundant param in facessat syscall in access implementation,
faccessat syscall does not take a flags arg

GitOrigin-RevId: 44d471e70679c3056fb68d2fc826bbaa41f4df3a
diff --git a/config/linux/aarch64/entrypoints.txt b/config/linux/aarch64/entrypoints.txt
index ae8deab..cfb7791 100644
--- a/config/linux/aarch64/entrypoints.txt
+++ b/config/linux/aarch64/entrypoints.txt
@@ -325,6 +325,7 @@
     libc.src.unistd.dup2
     libc.src.unistd.dup3
     libc.src.unistd.execve
+    libc.src.unistd.faccessat
     libc.src.unistd.fchdir
     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 bf2ad4a..87b78a3 100644
--- a/config/linux/x86_64/entrypoints.txt
+++ b/config/linux/x86_64/entrypoints.txt
@@ -331,6 +331,7 @@
     libc.src.unistd.dup2
     libc.src.unistd.dup3
     libc.src.unistd.execve
+    libc.src.unistd.faccessat
     libc.src.unistd.fchdir
     libc.src.unistd.fpathconf
     libc.src.unistd.fsync
diff --git a/include/llvm-libc-macros/linux/fcntl-macros.h b/include/llvm-libc-macros/linux/fcntl-macros.h
index aec8a0d..74d406f 100644
--- a/include/llvm-libc-macros/linux/fcntl-macros.h
+++ b/include/llvm-libc-macros/linux/fcntl-macros.h
@@ -61,6 +61,9 @@
 // Allow empty relative pathname.
 #define AT_EMPTY_PATH 0x1000
 
+// Perform access checks using the effective user and group IDs.
+#define AT_EACCESS 0x200
+
 // Values of SYS_fcntl commands.
 #define F_DUPFD 0
 #define F_GETFD 1
diff --git a/include/sys/syscall.h.def b/include/sys/syscall.h.def
index 6d74cc6..60e5024 100644
--- a/include/sys/syscall.h.def
+++ b/include/sys/syscall.h.def
@@ -309,6 +309,10 @@
 #define SYS_faccessat __NR_faccessat
 #endif
 
+#ifdef __NR_faccessat2
+#define SYS_faccessat2 __NR_faccessat2
+#endif
+
 #ifdef __NR_fadvise64
 #define SYS_fadvise64 __NR_fadvise64
 #endif
diff --git a/include/unistd.yaml b/include/unistd.yaml
index 3ba3ec7..2ff86ea 100644
--- a/include/unistd.yaml
+++ b/include/unistd.yaml
@@ -96,6 +96,15 @@
       - type: const char *
       - type: __exec_argv_t
       - type: __exec_envp_t
+  - name: faccessat
+    standards:
+      - POSIX
+    return_type: int
+    arguments:
+      - type: int
+      - type: const char *
+      - type: int
+      - type: int
   - name: fchdir
     standards:
       - POSIX
diff --git a/src/unistd/CMakeLists.txt b/src/unistd/CMakeLists.txt
index c66a3a4..78c3bf8 100644
--- a/src/unistd/CMakeLists.txt
+++ b/src/unistd/CMakeLists.txt
@@ -56,6 +56,13 @@
 )
 
 add_entrypoint_object(
+  faccessat
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.faccessat
+)
+
+add_entrypoint_object(
   fchdir
   ALIAS
   DEPENDS
diff --git a/src/unistd/faccessat.h b/src/unistd/faccessat.h
new file mode 100644
index 0000000..0dc834d
--- /dev/null
+++ b/src/unistd/faccessat.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for faccessat ---------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_FACCESSAT_H
+#define LLVM_LIBC_SRC_UNISTD_FACCESSAT_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int faccessat(int fd, const char *path, int amode, int flag);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_FACCESSAT_H
diff --git a/src/unistd/linux/CMakeLists.txt b/src/unistd/linux/CMakeLists.txt
index 2d510f3..dff6ba2 100644
--- a/src/unistd/linux/CMakeLists.txt
+++ b/src/unistd/linux/CMakeLists.txt
@@ -81,6 +81,19 @@
 )
 
 add_entrypoint_object(
+  faccessat
+  SRCS
+    faccessat.cpp
+  HDRS
+    ../faccessat.h
+  DEPENDS
+    libc.hdr.fcntl_macros
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
   fchdir
   SRCS
     fchdir.cpp
diff --git a/src/unistd/linux/access.cpp b/src/unistd/linux/access.cpp
index 55cd6ad..f06eec5 100644
--- a/src/unistd/linux/access.cpp
+++ b/src/unistd/linux/access.cpp
@@ -23,7 +23,7 @@
   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_access, path, mode);
 #elif defined(SYS_faccessat)
   int ret =
-      LIBC_NAMESPACE::syscall_impl<int>(SYS_faccessat, AT_FDCWD, path, mode, 0);
+      LIBC_NAMESPACE::syscall_impl<int>(SYS_faccessat, AT_FDCWD, path, mode);
 #else
 #error "access and faccessat syscalls not available."
 #endif
diff --git a/src/unistd/linux/faccessat.cpp b/src/unistd/linux/faccessat.cpp
new file mode 100644
index 0000000..7a2a29c
--- /dev/null
+++ b/src/unistd/linux/faccessat.cpp
@@ -0,0 +1,37 @@
+//===-- Linux implementation of faccessat ---------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/faccessat.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include "hdr/fcntl_macros.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, faccessat,
+                   (int fd, const char *path, int amode, int flag)) {
+#ifdef SYS_faccessat2
+  int ret =
+      LIBC_NAMESPACE::syscall_impl<int>(SYS_faccessat2, fd, path, amode, flag);
+#else
+#error "faccessat2 syscall is not available."
+#endif
+
+  if (ret < 0) {
+    libc_errno = -ret;
+    return -1;
+  }
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/test/src/unistd/CMakeLists.txt b/test/src/unistd/CMakeLists.txt
index 6630a7e..44f28ff 100644
--- a/test/src/unistd/CMakeLists.txt
+++ b/test/src/unistd/CMakeLists.txt
@@ -94,6 +94,23 @@
 )
 
 add_libc_unittest(
+  faccessat_test
+  SUITE
+    libc_unistd_unittests
+  SRCS
+    faccessat_test.cpp
+  DEPENDS
+    libc.include.unistd
+    libc.src.errno.errno
+    libc.src.fcntl.open
+    libc.src.unistd.faccessat
+    libc.src.unistd.close
+    libc.src.unistd.unlink
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
+add_libc_unittest(
   fchdir_test
   SUITE
     libc_unistd_unittests
diff --git a/test/src/unistd/faccessat_test.cpp b/test/src/unistd/faccessat_test.cpp
new file mode 100644
index 0000000..6280b14
--- /dev/null
+++ b/test/src/unistd/faccessat_test.cpp
@@ -0,0 +1,115 @@
+//===-- Unittests for faccessat -------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/fcntl/open.h"
+#include "src/unistd/close.h"
+#include "src/unistd/faccessat.h"
+#include "src/unistd/unlink.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+namespace {
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+
+using LlvmLibcFaccessatTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcFaccessatTest, WithAtFdcwd) {
+  // Test access checks on a file with AT_FDCWD and no flags, equivalent to
+  // access().
+  constexpr const char *FILENAME = "faccessat_basic.test";
+  auto TEST_FILE = libc_make_test_file_path(FILENAME);
+
+  // Check permissions on a file with full permissions
+  int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(fd, 0);
+  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+
+  ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, F_OK, 0),
+              Succeeds(0));
+  ASSERT_THAT(
+      LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, X_OK | W_OK | R_OK, 0),
+      Succeeds(0));
+  ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
+
+  // Check permissions on a file with execute-only permission
+  fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IXUSR);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(fd, 0);
+  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+
+  ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, F_OK, 0),
+              Succeeds(0));
+  ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, X_OK, 0),
+              Succeeds(0));
+  ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, R_OK, 0),
+              Fails(EACCES));
+  ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, W_OK, 0),
+              Fails(EACCES));
+  ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
+}
+
+TEST_F(LlvmLibcFaccessatTest, NonExistentFile) {
+  ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, "faccessat_nonexistent.test",
+                                        F_OK, 0),
+              Fails(ENOENT));
+}
+
+TEST_F(LlvmLibcFaccessatTest, AtEaccess) {
+  // With AT_EACCESS, faccessat checks permissions using the effective user ID,
+  // but the effective and real user ID will be the same here and changing that
+  // is not feasible in a test, so this is just a basic sanity check.
+  constexpr const char *FILENAME = "faccessat_eaccess.test";
+  auto TEST_FILE = libc_make_test_file_path(FILENAME);
+
+  int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(fd, 0);
+  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+
+  ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, X_OK | W_OK | R_OK,
+                                        AT_EACCESS),
+              Succeeds(0));
+
+  ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
+}
+
+TEST_F(LlvmLibcFaccessatTest, AtEmptyPath) {
+  constexpr const char *FILENAME = "faccessat_atemptypath.test";
+  auto TEST_FILE = libc_make_test_file_path(FILENAME);
+
+  int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(fd, 0);
+
+  // Check permissions on the file referred to by fd
+  ASSERT_THAT(LIBC_NAMESPACE::faccessat(fd, "", F_OK, AT_EMPTY_PATH),
+              Succeeds(0));
+  ASSERT_THAT(
+      LIBC_NAMESPACE::faccessat(fd, "", X_OK | W_OK | R_OK, AT_EMPTY_PATH),
+      Succeeds(0));
+
+  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+  ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
+
+  // Check permissions on the current working directory
+  ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, "", F_OK, AT_EMPTY_PATH),
+              Succeeds(0));
+  ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, "", X_OK | W_OK | R_OK,
+                                        AT_EMPTY_PATH),
+              Succeeds(0));
+}
+
+} // namespace