[libc-commits] [libc] 419580c - [libc] Add implementation of POSIX function "access".

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Wed Sep 14 00:44:57 PDT 2022


Author: Siva Chandra Reddy
Date: 2022-09-14T07:44:47Z
New Revision: 419580c699481de40f1e819c396fe07a63885b43

URL: https://github.com/llvm/llvm-project/commit/419580c699481de40f1e819c396fe07a63885b43
DIFF: https://github.com/llvm/llvm-project/commit/419580c699481de40f1e819c396fe07a63885b43.diff

LOG: [libc] Add implementation of POSIX function "access".

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D133814

Added: 
    libc/include/llvm-libc-macros/linux/unistd-macros.h
    libc/include/llvm-libc-macros/unistd-macros.h
    libc/src/unistd/access.h
    libc/src/unistd/linux/access.cpp
    libc/test/src/unistd/access_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/CMakeLists.txt
    libc/include/llvm-libc-macros/CMakeLists.txt
    libc/include/llvm-libc-macros/linux/CMakeLists.txt
    libc/include/unistd.h.def
    libc/spec/posix.td
    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 adb4602cbfe2b..34f3978647a5c 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -111,6 +111,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.stat.mkdirat
 
     # unistd.h entrypoints
+    libc.src.unistd.access
     libc.src.unistd.chdir
     libc.src.unistd.close
     libc.src.unistd.dup

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index bd384637122e2..f6816bce44964 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -111,6 +111,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.stat.mkdirat
 
     # unistd.h entrypoints
+    libc.src.unistd.access
     libc.src.unistd.chdir
     libc.src.unistd.close
     libc.src.unistd.dup

diff  --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 248c164e7d667..1a31bd8463a57 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -168,6 +168,7 @@ add_gen_header(
   DEPENDS
     .llvm_libc_common_h
     .llvm-libc-macros.file_seek_macros
+    .llvm-libc-macros.unistd_macros
     .llvm-libc-types.size_t
     .llvm-libc-types.ssize_t
 )

diff  --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index e1907694e52ab..4d19ed6c092df 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -21,3 +21,11 @@ add_header(
   DEPENDS
     .linux.sys_stat_macros
 )
+
+add_header(
+  unistd_macros
+  HDR
+    unistd-macros.h
+  DEPENDS
+    .linux.unistd_macros
+)

diff  --git a/libc/include/llvm-libc-macros/linux/CMakeLists.txt b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
index 09b9ba2199d45..199f9b6edc330 100644
--- a/libc/include/llvm-libc-macros/linux/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
@@ -9,3 +9,9 @@ add_header(
   HDR
     sys-stat-macros.h
 )
+
+add_header(
+  unistd_macros
+  HDR
+    unistd-macros.h
+)

diff  --git a/libc/include/llvm-libc-macros/linux/unistd-macros.h b/libc/include/llvm-libc-macros/linux/unistd-macros.h
new file mode 100644
index 0000000000000..41f8f917ced79
--- /dev/null
+++ b/libc/include/llvm-libc-macros/linux/unistd-macros.h
@@ -0,0 +1,18 @@
+//===-- Definition of macros from unistd.h --------------------------------===//
+//
+// 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_MACROS_LINUX_UNISTD_MACROS_H
+#define __LLVM_LIBC_MACROS_LINUX_UNISTD_MACROS_H
+
+// Values for mode argument to the access(...) function.
+#define F_OK 0
+#define X_OK 1
+#define W_OK 2
+#define R_OK 4
+
+#endif // __LLVM_LIBC_MACROS_LINUX_UNISTD_MACROS_H

diff  --git a/libc/include/llvm-libc-macros/unistd-macros.h b/libc/include/llvm-libc-macros/unistd-macros.h
new file mode 100644
index 0000000000000..d00942fd58281
--- /dev/null
+++ b/libc/include/llvm-libc-macros/unistd-macros.h
@@ -0,0 +1,8 @@
+#ifndef __LLVM_LIBC_MACROS_UNISTD_MACROS_H
+#define __LLVM_LIBC_MACROS_UNISTD_MACROS_H
+
+#ifdef __unix__
+#include "linux/unistd-macros.h"
+#endif
+
+#endif // __LLVM_LIBC_MACROS_UNISTD_MACROS_H

diff  --git a/libc/include/unistd.h.def b/libc/include/unistd.h.def
index d7a2de93a03d0..fa10af653fae2 100644
--- a/libc/include/unistd.h.def
+++ b/libc/include/unistd.h.def
@@ -11,6 +11,7 @@
 
 #include <__llvm-libc-common.h>
 #include <llvm-libc-macros/file-seek-macros.h>
+#include <llvm-libc-macros/unistd-macros.h>
 
 %%public_api()
 

diff  --git a/libc/spec/posix.td b/libc/spec/posix.td
index 12dc68b65dc39..877ae614fce5e 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -261,6 +261,11 @@ def POSIX : StandardSpec<"POSIX"> {
     ],
     [], // Enumerations
     [
+        FunctionSpec<
+          "access",
+          RetValSpec<IntType>,
+          [ArgSpec<ConstCharPtr>, ArgSpec<IntType>]
+        >,
         FunctionSpec<
           "chdir",
           RetValSpec<IntType>,

diff  --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index f03a1ac7b7a03..0f8fffaeb2aaf 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -2,6 +2,13 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
   add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
 endif()
 
+add_entrypoint_object(
+  access
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.access
+)
+
 add_entrypoint_object(
   chdir
   ALIAS

diff  --git a/libc/src/unistd/access.h b/libc/src/unistd/access.h
new file mode 100644
index 0000000000000..2454e805731f9
--- /dev/null
+++ b/libc/src/unistd/access.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for access ------------------------*- 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_ACCESS_H
+#define LLVM_LIBC_SRC_UNISTD_ACCESS_H
+
+namespace __llvm_libc {
+
+int access(const char *path, int mode);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_UNISTD_ACCESS_H

diff  --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 812c7ef7a60ee..8b2f44c561164 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -1,3 +1,16 @@
+add_entrypoint_object(
+  access
+  SRCS
+    access.cpp
+  HDRS
+    ../access.h
+  DEPENDS
+    libc.include.unistd
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   chdir
   SRCS

diff  --git a/libc/src/unistd/linux/access.cpp b/libc/src/unistd/linux/access.cpp
new file mode 100644
index 0000000000000..8c7034424fc0d
--- /dev/null
+++ b/libc/src/unistd/linux/access.cpp
@@ -0,0 +1,36 @@
+//===-- Linux implementation of access ------------------------------------===//
+//
+// 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/access.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, access, (const char *path, int mode)) {
+#ifdef SYS_access
+  long ret = __llvm_libc::syscall(SYS_access, path, mode);
+#elif defined(SYS_accessat)
+  long ret = __llvm_libc::syscall(SYS_accessat, AT_FDCWD, path, mode);
+#else
+#error "access syscalls not available."
+#endif
+
+  if (ret < 0) {
+    errno = -ret;
+    return -1;
+  }
+  return 0;
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 9ac884ccff271..f848d8d340e56 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -2,6 +2,21 @@ add_libc_testsuite(libc_unistd_unittests)
 
 add_subdirectory(testdata)
 
+add_libc_unittest(
+  access_test
+  SUITE
+    libc_unistd_unittests
+  SRCS
+    access_test.cpp
+  DEPENDS
+    libc.include.errno
+    libc.include.unistd
+    libc.src.fcntl.open
+    libc.src.unistd.access
+    libc.src.unistd.close
+    libc.src.unistd.unlink
+)
+
 add_libc_unittest(
   chdir_test
   SUITE

diff  --git a/libc/test/src/unistd/access_test.cpp b/libc/test/src/unistd/access_test.cpp
new file mode 100644
index 0000000000000..fc158b3e481e7
--- /dev/null
+++ b/libc/test/src/unistd/access_test.cpp
@@ -0,0 +1,58 @@
+//===-- Unittests for access ----------------------------------------------===//
+//
+// 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/access.h"
+#include "src/unistd/close.h"
+#include "src/unistd/unlink.h"
+#include "test/ErrnoSetterMatcher.h"
+#include "utils/UnitTest/Test.h"
+#include "utils/testutils/FDReader.h"
+
+#include <errno.h>
+#include <unistd.h>
+
+TEST(LlvmLibcAccessTest, CreateAndTest) {
+  // The test strategy is to repeatedly create a file in 
diff erent modes and
+  // test that it is accessable in those modes but not in others.
+  errno = 0;
+  using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
+  constexpr const char *TEST_FILE = "testdata/access.test";
+  int fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+  ASSERT_EQ(errno, 0);
+  ASSERT_GT(fd, 0);
+  ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
+
+  ASSERT_EQ(__llvm_libc::access(TEST_FILE, F_OK), 0);
+  ASSERT_EQ(errno, 0);
+  ASSERT_EQ(__llvm_libc::access(TEST_FILE, X_OK | W_OK | R_OK), 0);
+  ASSERT_EQ(errno, 0);
+  ASSERT_THAT(__llvm_libc::unlink(TEST_FILE), Succeeds(0));
+
+  fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IXUSR);
+  ASSERT_EQ(errno, 0);
+  ASSERT_GT(fd, 0);
+  ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
+  ASSERT_EQ(__llvm_libc::access(TEST_FILE, F_OK), 0);
+  ASSERT_EQ(errno, 0);
+  ASSERT_EQ(__llvm_libc::access(TEST_FILE, X_OK), 0);
+  ASSERT_EQ(errno, 0);
+  ASSERT_EQ(__llvm_libc::access(TEST_FILE, R_OK), -1);
+  ASSERT_EQ(errno, EACCES);
+  errno = 0;
+  ASSERT_EQ(__llvm_libc::access(TEST_FILE, W_OK), -1);
+  ASSERT_EQ(errno, EACCES);
+  errno = 0;
+  ASSERT_THAT(__llvm_libc::unlink(TEST_FILE), Succeeds(0));
+}
+
+TEST(LlvmLibcAccessTest, AccessNonExistentFile) {
+  using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
+  ASSERT_THAT(__llvm_libc::access("testdata/non-existent-file", F_OK),
+              Fails(ENOENT));
+}


        


More information about the libc-commits mailing list